在YUV格式图像上画点/画线/画矩形
1.YUY2格式描述
V4L2_PIX_FMT_YUYV格式也就是YUYV格式在Window环境中被称为YUY2,这种格式中每四个字节表示一个两个像素,其中包含两个Y,一个Cb(U)一个Cr(V)。也就是说,在这种格式中,两个像素的Y共用一组CbCr。其数据格式中Y与UV交错排布存储,其4x4图像数据存储如下,其中 Y’00、Y’01 共用Cb00 和Cr00 ,可以发现其数据量大小=宽x高x2:
start + 0: Y’00 Cb00 Y’01 Cr00 Y’02 Cb01 Y’03 Cr01
start + 8: Y’10 Cb10 Y’11 Cr10 Y’12 Cb11 Y’13 Cr11
start + 16: Y’20 Cb20 Y’21 Cr20 Y’22 Cb21 Y’23 Cr21
start + 24: Y’30 Cb30 Y’31 Cr30 Y’32 Cb31 Y’33 Cr31
2.NV12格式描述
与V4L2_PIX_FMT_YUYV不同,V4L2_PIX_FMT_NV12格式也就是NV12格式中每四个Y共用一组CbCr,其4x4图像数据存储如下所示,其中Y’00、Y’01、 Y’10、 Y’11共用Cb00 Cr00。除此之外图像的数据存储格式也不一样,NV12采用平面存储格式,其数据格式中先将Y分量单独存放在一平面中,然后再把UV数据分量交错排布存储在另一个平面,可以发现其数据量大小=宽x高 + 宽 x 高 / 2:
start + 0: Y’00 Y’01 Y’02 Y’03
start + 4: Y’10 Y’11 Y’12 Y’13
start + 8: Y’20 Y’21 Y’22 Y’23
start + 12: Y’30 Y’31 Y’32 Y’33
start + 16: Cb00 Cr00 Cb01 Cr01
start + 20: Cb10 Cr10 Cb11 Cr11
3.程序实现:
实现思路:先画点,再画线,最后组成矩形
typedef struct{
int left;
int top;
int right;
int bottom;
}RECT;
typedef struct
{
char Y;
char U;
char V;
}stYuvColor;
static stYuvColor s_color_table[2] =
{
{0x51, 0x5A, 0xFA},
// red
{0x28, 0xff, 0x00},
// blue
};
static int YUY_Draw_Piont(unsigned char * YuvBuf, int ImgW, int ImgH, int fmt,int PntX, int PntY, int Color)
{
int ret = -1;
if(YuvBuf == NULL)
return ret;
switch(fmt)
{
case V4L2_PIX_FMT_YUYV:
{
int Yoffset = PntY * ImgW * 2 + (PntX * 2);
int Uoffset,Voffset;
/*Y U Y V Y U Y V
pexl 0 1 2 3
data 0 1 2 3 4 5 6 7
*Y U Y V Y U Y V */
if(PntX % 2 == 0)
{
Uoffset = PntY * ImgW * 2 + (PntX * 2 + 1);
Voffset = PntY * ImgW * 2 + (PntX * 2 + 3);
}
else
{
Uoffset = PntY * ImgW * 2 + (PntX * 2 - 1);
Voffset = PntY * ImgW * 2 + (PntX * 2 + 1);
}
YuvBuf[Yoffset] = s_color_table[Color].Y;
YuvBuf[Uoffset] = s_color_table[Color].U;
YuvBuf[Voffset] = s_color_table[Color].V;
}
ret = 0;
break;
case V4L2_PIX_FMT_NV12:
{
int Yoffset = PntY * ImgW + PntX;
int Uoffset,Voffset;
Uoffset = ImgH * ImgW + (PntY / 2) * ImgW + PntX / 2 * 2 ;
Voffset = Uoffset + 1;
YuvBuf[Yoffset] = s_color_table[Color].Y;
YuvBuf[Uoffset] = s_color_table[Color].U;
YuvBuf[Voffset] = s_color_table[Color].V;
}
ret = 0;
break;
default:
ret = -2;
break;
}
return ret;
}
static int YUY_Draw_Line(unsigned char * YuvBuf, int ImgW, int ImgH, int fmt,int SPntX, int SPntY,int EPntX, int EPntY, int Color,int lineWidth)
{
int ret = -1;
if(YuvBuf == NULL)
return ret;
if(lineWidth == 0)
lineWidth = 1;
SPntX = (SPntX + lineWidth >= ImgW) ? (ImgW - lineWidth) : (SPntX < 0 ? 0 : SPntX);
EPntX = (EPntX + lineWidth >= ImgW) ? (ImgW - lineWidth) : (EPntX < 0 ? 0 : EPntX);
SPntY = (SPntY + lineWidth >= ImgH) ? (ImgH - lineWidth) : (SPntY < 0 ? 0 : SPntY);
EPntY = (EPntY + lineWidth >= ImgH) ? (ImgH - lineWidth) : (EPntY < 0 ? 0 : EPntY);
int dx = (SPntX > EPntX) ? (SPntX - EPntX) : (EPntX - SPntX);
int dy = (SPntY > EPntY) ? (SPntY - EPntY) : (EPntY - SPntY);
int xstep = (SPntX < EPntX) ? 1 : -1;
int ystep = (SPntY < EPntY) ? 1 : -1;
int nstep = 0, eps = 0;
int pointX = SPntX;
int pointY = SPntY;
if(dx > dy)
{
while(nstep <= dx)
{
ret = YUY_Draw_Piont(YuvBuf, ImgW, ImgH, fmt, pointX, pointY, Color);
if(ret < 0)
break;
eps += dy;
if( (eps << 1) >= dx )
{
pointY += ystep;
eps -= dx;
}
pointX += xstep;
nstep++;
}
}
else
{
while(nstep <= dy)
{
ret = YUY_Draw_Piont(YuvBuf, ImgW, ImgH, fmt, pointX, pointY, Color);
if(ret < 0)
break;
eps += dx;
if( (eps << 1) >= dy )
{
pointX += xstep;
eps -= dy;
}
pointY += ystep;
nstep++;
}
}
return ret;
}
static int YUV_Draw_Rect(unsigned char *YuvBuf, int ImgW, int ImgH, int fmt,RECT* Rect, int Color,int lineWidth)
{
int ret = -1;
if(YuvBuf == NULL)
return ret;
if(Rect->bottom == Rect->top || Rect->left == Rect->right)
return ret;
YUY_Draw_Line(YuvBuf, ImgW, ImgH,fmt, Rect->left, Rect->top , Rect->right, Rect->top, Color,lineWidth);
YUY_Draw_Line(YuvBuf, ImgW, ImgH,fmt, Rect->right, Rect->top, Rect->right, Rect->bottom, Color,lineWidth);
YUY_Draw_Line(YuvBuf, ImgW, ImgH,fmt, Rect->left, Rect->top , Rect->left, Rect->bottom, Color,lineWidth);
YUY_Draw_Line(YuvBuf, ImgW, ImgH,fmt, Rect->left, Rect->bottom, Rect->right, Rect->bottom, Color,lineWidth);
return 0;
}
注:线宽暂未实现,后续有时间再处理