双线性插值缩放图像

最近做图像处理遇到图像太大,处理实时性交叉的问题。需要将图片进行缩小,以下是参考网上线性插值法进行图像缩小的一种算法,可以对3通道的RGB或者是yuv444进行缩放处理:

/*参数为:
 *返回图片的宽度(w_Dest),
 *返回图片的高度(h_Dest),
 *返回图片的位深(bit_depth),
 *源图片的数据(src),
 *源图片的宽度(w_Src),
 *源图片的高度(h_Src)
 *返回图片数据(dst),
 */
static int do_Stretch_Linear(int w_Dest,int h_Dest,int bit_depth,unsigned char *src,int w_Src,int h_Src,unsigned char *dst)
{
	int sw = w_Src-1, sh = h_Src-1, dw = w_Dest-1, dh = h_Dest-1;
	int B, N, x, y;
	int nPixelSize = bit_depth/8;
	unsigned char *pLinePrev,*pLineNext;
	unsigned char *pDest = (unsigned char *)malloc(w_Dest*h_Dest*bit_depth/8) ;
	unsigned char *tmp;
	unsigned char *pA,*pB,*pC,*pD;
	int i, j, k;
	for(i=0;i<=dh;++i)
	{
		tmp =pDest + i*w_Dest*nPixelSize;
		y = i*sh/dh;
		N = dh - i*sh%dh;
		pLinePrev = src + (y++)*w_Src*nPixelSize;
		pLineNext = (N==dh) ? pLinePrev : src+y*w_Src*nPixelSize;   
		
		for(j=0;j<=dw;++j)
		{
			x = j*sw/dw*nPixelSize;
			B = dw-j*sw%dw;
			pA = pLinePrev+x;
			pB = pA+nPixelSize;
			pC = pLineNext + x;
			pD = pC + nPixelSize;
			if(B == dw)
			{
				pB=pA;
				pD=pC;
			}
			for(k=0;k<nPixelSize;++k)
			{
				*tmp++ = ( unsigned char )( int )(
					( B * N * ( *pA++ - *pB - *pC + *pD ) + dw * N * *pB++
					+ dh * B * *pC++ + ( dw * dh - dh * B - dw * N ) * *pD++
					+ dw * dh / 2 ) / ( dw * dh ) );
			}
		}
	}
	
	memcpy(dst,pDest,w_Dest*h_Dest*nPixelSize);
	free(pDest);
	pDest = NULL;
	return 0;
}
上一篇:OpenGL-实战篇--Breakout--CollisionDetection--49


下一篇:photoshop利用Lab通道快速制作个性油画图片