下面的代码,实现将一幅YUV422的双通道图片(640x480大小),取中间区域(560x400大小)使用插值的办法放大至(640x480)。这个算法可以用来实现将图片外围区域截掉。
#define X_RATIO (8)
#define Y_RATIO (6)
PUBLIC void YUVScale(JINF_YUV_CHN_T *const yuv_ptr,const uint32 Width,const uint32 Height)
{
uint8 *const src_y_ptr = (uint16 *)yuv_ptr->y_chn_ptr;
uint8 *const src_uv_ptr = (uint16 *)yuv_ptr->u_chn_ptr;
uint32 x,y,w,h=0,xl,yl,dx,dy;
if ((Width%(X_RATIO<<1)!=0)||(Height%(Y_RATIO<<1)!=0))
{
TRACE("%s Width=%d,Height=%d",__func__,Width,Height);
return;
}
ASSERT(PNULL != yuv_ptr);
xl=(Width-(Width/X_RATIO))>>1;
yl=(Height-(Height/Y_RATIO))>>1;
dx=Width / (X_RATIO<<1);
dy=Height / (Y_RATIO<<1);
for (y=0; y<yl; y++,h++)
{
w = 0;
for (x=0; x<xl; x+=2,w+=2)
{
CopyPixel:
//2 Quadrant 2
src_y_ptr[h*Width+w] = src_y_ptr[(dy+y)*Width+dx+x];
src_y_ptr[h*Width+w+1] = src_y_ptr[(dy+y)*Width+dx+x+1];
src_uv_ptr[h*Width+w] = src_uv_ptr[(dy+y)*Width+dx+x];
src_uv_ptr[h*Width+w+1] = src_uv_ptr[(dy+y)*Width+dx+x+1];
//2 Quadrant 1
src_y_ptr[h*Width+(Width-w-1)] = src_y_ptr[(dy+y)*Width+(Width-dx-x-1)];
src_y_ptr[h*Width+(Width-w-2)] = src_y_ptr[(dy+y)*Width+(Width-dx-x-2)];
src_uv_ptr[h*Width+(Width-w-1)] = src_uv_ptr[(dy+y)*Width+(Width-dx-x-1)];
src_uv_ptr[h*Width+(Width-w-2)] = src_uv_ptr[(dy+y)*Width+(Width-dx-x-2)];
//2 Quadrant 4
src_y_ptr[(Height-h-1)*Width+(Width-w-1)] = src_y_ptr[(Height-dy-y-1)*Width+(Width-dx-x-1)];
src_y_ptr[(Height-h-1)*Width+(Width-w-2)] = src_y_ptr[(Height-dy-y-1)*Width+(Width-dx-x-2)];
src_uv_ptr[(Height-h-1)*Width+(Width-w-1)] = src_uv_ptr[(Height-dy-y-1)*Width+(Width-dx-x-1)];
src_uv_ptr[(Height-h-1)*Width+(Width-w-2)] = src_uv_ptr[(Height-dy-y-1)*Width+(Width-dx-x-2)];
//2 Quadrant 3
src_y_ptr[(Height-h-1)*Width+w] = src_y_ptr[(Height-dy-y-1)*Width+dx+x];
src_y_ptr[(Height-h-1)*Width+w+1] = src_y_ptr[(Height-dy-y-1)*Width+dx+x+1];
src_uv_ptr[(Height-h-1)*Width+w] = src_uv_ptr[(Height-dy-y-1)*Width+dx+x];
src_uv_ptr[(Height-h-1)*Width+w+1] = src_uv_ptr[(Height-dy-y-1)*Width+dx+x+1];
if (
((w+2) % (X_RATIO<<1))==
((X_RATIO<<1)-2)
)
{
w+=2;
goto CopyPixel;
}
}
if (((h+1) % (Y_RATIO))==(Y_RATIO-1))
{
h++;
memcpy(src_y_ptr+h*Width,src_y_ptr+(h-1)*Width,Width);
memcpy(src_uv_ptr+h*Width,src_uv_ptr+(h-1)*Width,Width);
memcpy(src_y_ptr+(Height-h-1)*Width,src_y_ptr+(Height-h)*Width,Width);
memcpy(src_uv_ptr+(Height-h-1)*Width,src_uv_ptr+(Height-h)*Width,Width);
}
}
}
编写这个算法的好处是,在不改变图片原始大小的情况下去除照片的边框。