int NV21ToBGR_Rect(unsigned char * NV21, int width, int height, int x0, int y0, int x1, int y1, unsigned char * dest, int destW, int destH )
{
float scale_x = 0.0f;
float scale_y = 0.0f;
int CutW = 0;
int CutH = 0;
int i = 0;
int j = 0;
unsigned char * MidPic = (unsigned char *)malloc(sizeof(char) *destW * destH * 3/2) ;
unsigned char * SrcVU = NV21 + width * height;
unsigned char * VUMidPic = MidPic + destW * destH;
float weightX[2] = { 0, 0 };
float weightY[2] = { 0, 0 };
float VUweighX[2] = { 0, 0 };
float VUweighY[2] = { 0, 0 };
CutW = x1 - x0 + 1;
CutH = y1 - y0 + 1;
scale_x = CutW * 1.0f / destW;
scale_y = CutH * 1.0f / destH;
for (i = 0; i < destH; i++)
{
for (j = 0; j < destW; j++)
{
float SrcPicFX = j * scale_x + x0;
float SrcPicFY = i * scale_y + y0;
int SrcPicIX = (int)(SrcPicFX);
int SrcPicIY = (int)(SrcPicFY);
weightX[1] = SrcPicFX - SrcPicIX;
weightX[0] = 1.0f - weightX[1];
weightY[1] = SrcPicFY - SrcPicIY;
weightY[0] = 1.0f - weightY[1];
MidPic[(i * destW + j)] = (NV21[(SrcPicIY * width + SrcPicIX)] * weightX[0] + NV21[(SrcPicIY * width + Clamp(SrcPicIX + 1, 0, width - 1))] * weightX[1]) * weightY[0] +
(NV21[(Clamp(SrcPicIY + 1, 0, height - 1) * width + SrcPicIX)] * weightX[0] + NV21[(Clamp(SrcPicIY + 1, 0, height - 1) * width + Clamp(SrcPicIX + 1, 0, width - 1))] * weightX[1]) * weightY[1];
if(i % 2 == 0 && j % 2 == 0)
{
int VUpicW = width / 2;
int VUpicH = height / 2;
float VUPicFy = i / 2 * scale_y + y0 / 2;
float VUPicFx = j / 2 * scale_x + x0 / 2;
int VUPicIx = (int)(VUPicFx);
int VUPicIy = (int)(VUPicFy);
VUweighX[1] = VUPicFx - VUPicIx;
VUweighX[0] = 1.0f - VUweighX[1];
VUweighY[1] = VUPicFy - VUPicIy;
VUweighY[0] = 1.0f - VUweighY[1];
VUMidPic[((i / 2) * (destW / 2) + (j/2)) * 2 + 0] = (SrcVU[(VUPicIy * VUpicW + VUPicIx) * 2 + 0] * VUweighX[0] + SrcVU[(VUPicIy * VUpicW + Clamp(VUPicIx + 1, 0, VUpicW - 1)) * 2 + 0] * VUweighX[1]) * VUweighY[0] +
(SrcVU[(Clamp(VUPicIy + 1, 0, VUpicH - 1) * VUpicW + VUPicIx) * 2 + 0] * VUweighX[0] + SrcVU[(Clamp(VUPicIy + 1, 0, VUpicH - 1) * VUpicW + Clamp(VUPicIx + 1, 0, VUpicW - 1)) * 2 + 0] * VUweighX[1]) * VUweighY[1];
VUMidPic[((i / 2) * (destW / 2) + (j/2)) * 2 + 1] = (SrcVU[(VUPicIy * VUpicW + VUPicIx) * 2 + 1] * VUweighX[0] + SrcVU[(VUPicIy * VUpicW + Clamp(VUPicIx + 1, 0, VUpicW - 1)) * 2 + 1] * VUweighX[1]) * VUweighY[0] +
(SrcVU[(Clamp(VUPicIy + 1, 0, VUpicH - 1) * VUpicW + VUPicIx) * 2 + 1] * VUweighX[0] + SrcVU[(Clamp(VUPicIy + 1, 0, VUpicH - 1) * VUpicW + Clamp(VUPicIx + 1, 0, VUpicW - 1)) * 2 + 1] * VUweighX[1]) * VUweighY[1];
}
}
}
for (i = 0; i < destH; i++)
{
for (j = 0; j < destW; j++)
{
unsigned char Y, U, V;
int B, G, R;
Y = MidPic[i * destW + j];
V = VUMidPic[(i / 2 * destW / 2 + j / 2) * 2 + 0];
U = VUMidPic[(i / 2 * destW / 2 + j / 2) * 2 + 1];
R = 1.164*(Y - 16) + 1.596*(V - 128);
G = 1.164*(Y - 16) - 0.813*(V - 128) - 0.392*(U - 128);
B = 1.164*(Y - 16) + 2.017*(U - 128);
dest[(i * destW + j) * 3 + 0] = Clamp(B, 0, 255);
dest[(i * destW + j) * 3 + 1] = Clamp(G, 0, 255);
dest[(i * destW + j) * 3 + 2] = Clamp(R, 0, 255);
}
}
//showImage("llllll",dest,200,200, 3);
return 0;
}