最近一段時間太忙,上篇部落格說是把映像縮放的演算法放上來,今天放上來。這個演算法參考的是線性插值法來實現,也就是最簡單的方法,在手機上面受cpu計算能力的限制還有對圖片品質要求不高,所以採用這種演算法。如果需要高品質的話請搜尋濾波像素重採樣。這個代碼輸入和輸入的都是像素矩陣,大家在映像操作的時候應該都是會轉換成的。如果需要全套代碼可以mail我(jessz@live.com)或者留言在這裡,我會把整個dib.cpp檔案發給你,他包括映像解碼,縮放,圖片儲存之類的操作。
//////////////////////////////////////////////////////////////////////////// 功能: 根據固定比例縮放演算法// add by jessezhao/////////////////////////////////////////////////////////////////////////void CDib::PicZoomFixSize( const TPicRegion& Dst,const TPicRegion& Src ){ if ((0==Dst.width)||(0==Dst.height)||(0==Src.width)||(0==Src.height)) return; // 根據高寬比確認一個最好的縮放比例double dWidth = IMAGE_OPTION_WIDTH;double dHeight = IMAGE_OPTION_HIGHT;double dAspectRatio = (double)((double)dWidth/(double)dHeight);double dPictureWidth = Src.width;double dPictureHeight = Src.height;double dPictureAspectRatio = (double)((double)dPictureWidth/(double)dPictureHeight); int nCenteringFactor=0;int nNewHeight = 0;int nNewWidth = 0;if (dPictureAspectRatio > dAspectRatio){nNewHeight = (int)(dWidth/dPictureWidth*dPictureHeight);nCenteringFactor = (IMAGE_OPTION_HIGHT - nNewHeight) / 2;}else if (dPictureAspectRatio < dAspectRatio){nNewWidth = (int)(dHeight/dPictureHeight*dPictureWidth);nCenteringFactor = (IMAGE_OPTION_WIDTH - nNewWidth) / 2;} // 高度合適 if (nNewWidth != 0) {unsigned long xrIntFloat_16=(Src.width<<16)/nNewWidth+1; unsigned long yrIntFloat_16=(Src.height<<16)/Dst.height+1;unsigned long dst_width=nNewWidth;TARGB32* pDstLine=Dst.pdata;unsigned long srcy_16=0;for (unsigned long y=0;y<Dst.height;++y){TARGB32* pSrcLine=((TARGB32*)((TUInt8*)Src.pdata+Src.byte_width*(srcy_16>>16)));unsigned long srcx_16=0;for (unsigned long x=0;x<nNewWidth;++x){//nCenteringFactor調整在靶心圖表中開始寫入的位置pDstLine[x+nCenteringFactor]=pSrcLine[srcx_16>>16];srcx_16+=xrIntFloat_16;}srcy_16+=yrIntFloat_16;((TUInt8*&)pDstLine)+=Dst.byte_width;}}//寬度合適else if (nNewHeight!=0){unsigned long xrIntFloat_16=(Src.width<<16)/Dst.width+1; unsigned long yrIntFloat_16=(Src.height<<16)/nNewHeight+1;unsigned long dst_width=Dst.width;TARGB32* pDstLine=Dst.pdata;unsigned long srcy_16=0; //nCenteringFactor調整在靶心圖表中開始寫入的位置((TUInt8*&)pDstLine)+=Dst.byte_width*nCenteringFactor; for (unsigned long y=0;y<nNewHeight;++y){TARGB32* pSrcLine=((TARGB32*)((TUInt8*)Src.pdata+Src.byte_width*(srcy_16>>16)));unsigned long srcx_16=0;for (unsigned long x=0;x<dst_width;++x){pDstLine[x]=pSrcLine[srcx_16>>16];srcx_16+=xrIntFloat_16;}srcy_16+=yrIntFloat_16;((TUInt8*&)pDstLine)+=Dst.byte_width;}}//長寬等比例縮放符合螢幕比例else{unsigned long xrIntFloat_16=(Src.width<<16)/Dst.width+1; unsigned long yrIntFloat_16=(Src.height<<16)/Dst.height+1;unsigned long dst_width=Dst.width;TARGB32* pDstLine=Dst.pdata;unsigned long srcy_16=0; for (unsigned long y=0;y<Dst.height;++y){TARGB32* pSrcLine=((TARGB32*)((TUInt8*)Src.pdata+Src.byte_width*(srcy_16>>16)));unsigned long srcx_16=0;for (unsigned long x=0;x<dst_width;++x){pDstLine[x]=pSrcLine[srcx_16>>16];srcx_16+=xrIntFloat_16;}srcy_16+=yrIntFloat_16;((TUInt8*&)pDstLine)+=Dst.byte_width;}}}