windows mobile 上面固定比例图像缩放
最近一段时间太忙,上篇博客说是把图像缩放的算法放上来,今天放上来。这个算法参考的是线性插值法来实现,也就是最简单的方法,在手机上面受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;
}
}
}
浙公网安备 33010602011771号