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;
		}
	}
}
这是一个用于C++ MFC开发的Bitmap图片操作类,在文件中叫CBitmapEx,可用于放大,缩小,翻转,过渡和其他有用的功能,有兴趣的朋友可以下载看看。 部分public method: // // void Create(long width, long height); // void Create(CBitmapEx& bitmapEx); // void Load(LPTSTR lpszBitmapFile); // void Save(LPTSTR lpszBitmapFile); // void Scale(long horizontalPercent=100, long verticalPercent=100); // void Rotate(long degrees=0, _PIXEL bgColor=_RGB(0,0,0)); // void FlipHorizontal(); // void FlipVertical(); // void MirrorLeft(); // void MirrorRight(); // void MirrorTop(); // void MirrorBottom(); // void Clear(_PIXEL clearColor=_RGB(0,0,0)); // void Negative(); // void Grayscale(); // void Sepia(long depth=34); // void Emboss(); // void Engrave(); // void Pixelize(long size=4); // void Draw(HDC hDC); // void Draw(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY); // void Draw(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, long alpha); // void Draw(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight); // void Draw(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, // long srcX, long srcY, long srcWidth, long srcHeight, long alpha); // void DrawTransparent(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, long alpha, // _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, // _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, // long alpha, _PIXEL transparentColor=_RGB(0,0,0)); // LPBI
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值