RGB24的缩放

//////////////////////////////////////////////////////////////////////////
//放大或者缩小RGB24,采用双线性插值算法
//////////////////////////////////////////////////////////////////////////
void ZoomBitMap(unsigned char *pSrcImg, unsigned char *pDstImg, int nWidth, int nHeight, float fRateW,float fRateH)
{
	int i = 0;
	int j = 0;

	float fX, fY;

	int iStepSrcImg = nWidth;
	int iStepDstImg = nWidth * 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;

	for(i = 0; i < nHeight * fRateH; i++)
	{
		for(j = 0; j < nWidth * fRateW; j++)
		{
			fX = ((float)j) /fRateW;
			fY = ((float)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 <= nHeight * 2 && iX >= 0 && iX <= nWidth * 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
			}
		}
	}
}


你可能感兴趣的:(算法,float)