采用双线性插值算法 放大或者缩小RGB24

//
//放大或者缩小RGB24,采用双线性插值算法 
//nSrcWidth:原始宽度、nSrcHeight:原始高度
//fRateW:目的宽度/nSrcWidth
//fRateH:目的高度/nSrcHeight
//
void ZoomBitMap(unsigned char *pSrcImg, unsigned char *pDstImg, int nSrcWidth, int nSrcHeight, double fRateW,double fRateH)
{
	int i = 0;
	int j = 0;

	double fX, fY;
	
	int iStepSrcImg = nSrcWidth;
	int iStepDstImg = nSrcWidth * fRateW;

	int iX, iY;

	unsigned char bUpLeft, bUpRight, bDownLeft, bDownRight;
	unsigned char gUpLeft, gUpRight, gDownLeft, gDownRight;
	unsigned char rUpLeft, rUpRight, rDownLeft, rDownRight;

	unsigned char b, g, r;
 
 /*  int w=nHeight * fRateH;
   int h=nWidth * fRateW;*/
	for(i = 0; i < nSrcHeight * fRateH ; i++)
	{
		for(j = 0; j < nSrcWidth * fRateW ; j++)
		{
			fX = ((double)j) /fRateW;
			fY = ((double)i) /fRateH;

			iX = (int)fX;
			iY = (int)fY;

			bUpLeft  = pSrcImg[iY * iStepSrcImg * 3 + iX * 3 + 0];
			bUpRight = pSrcImg[iY * iStepSrcImg * 3 + (iX + 1) * 3 + 0];

			bDownLeft  = pSrcImg[(iY + 1) * iStepSrcImg * 3 + iX * 3 + 0];
			bDownRight = pSrcImg[(iY + 1) * iStepSrcImg * 3 + (iX + 1) * 3 + 0];

			gUpLeft  = pSrcImg[iY * iStepSrcImg * 3 + iX * 3 + 1];
			gUpRight = pSrcImg[iY * iStepSrcImg * 3 + (iX + 1) * 3 + 1];

			gDownLeft  = pSrcImg[(iY + 1) * iStepSrcImg * 3 + iX * 3 + 1];
			gDownRight = pSrcImg[(iY + 1) * iStepSrcImg * 3 + (iX + 1) * 3 + 1];

			rUpLeft  = pSrcImg[iY * iStepSrcImg * 3 + iX * 3 + 2];
			rUpRight = pSrcImg[iY * iStepSrcImg * 3 + (iX + 1) * 3 + 2];

			rDownLeft  = pSrcImg[(iY + 1) * iStepSrcImg * 3 + iX * 3 + 2];
			rDownRight = pSrcImg[(iY + 1) * iStepSrcImg * 3 + (iX + 1) * 3 + 2];

			b = bUpLeft * (iX + 1 - fX) * (iY + 1 - fY) + bUpRight * (fX - iX) * (iY + 1 - fY)
				+ bDownLeft * (iX + 1 - fX) * (fY - iY) + bDownRight * (fX - iX) * (fY - iY);

			g = gUpLeft * (iX + 1 - fX) * (iY + 1 - fY) + gUpRight * (fX - iX) * (iY + 1 - fY)
				+ gDownLeft * (iX + 1 - fX) * (fY - iY) + gDownRight * (fX - iX) * (fY - iY);

			r = rUpLeft * (iX + 1 - fX) * (iY + 1 - fY) + rUpRight * (fX - iX) * (iY + 1 - fY)
				+ rDownLeft * (iX + 1 - fX) * (fY - iY) + rDownRight * (fX - iX) * (fY - iY);

			if(iY >= 0 && iY <= nSrcHeight * 2 && iX >= 0 && iX <= nSrcWidth * 2)
			{
				pDstImg[i * iStepDstImg * 3 + j * 3 + 0] = b;        //B
				pDstImg[i * iStepDstImg * 3 + j * 3 + 1] = g;        //G
				pDstImg[i * iStepDstImg * 3 + j * 3 + 2] = r;        //R
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值