0
点赞
收藏
分享

微信扫一扫

在YUV帧图像上使用指定颜色画一个矩形

大柚子top 2022-07-18 阅读 38


李国帅 写于2010


YUV_drawRect(pBuffer, VideoWidth, VideoHeight, lPitch, 100, 100, 200, 200, RGB(255, 0, 0));
void YUV_drawRect(unsigned char *dst_buf, int w, int h, int lpitch, int x0, int y0, int x1, int y1, unsigned long rgba)
{
unsigned char r, g, b, y, cb, cr;
b = (rgba >> 8) & 0xFF;
g = (rgba >> 16) & 0xFF;
r = (rgba >> 24) & 0xFF;
y = 16 + 0.257*r + 0.504*g + 0.098*b;
cb = 128 - 0.148*r - 0.291*g + 0.439*b;
cr = 128 + 0.439*r - 0.368*g - 0.071*b;

int rect_width, rect_height;
x0 = x0 & 0xFFFE;
y0 = y0 & 0xFFFE;
x1 = x1 & 0xFFFE;
y1 = y1 & 0xFFFE;
rect_width = x1 - x0;
rect_height = y1 - y0;

unsigned char *xoff;
unsigned char *yoff;
xoff = dst_buf + y0*lpitch + x0;
memset(xoff, y, rect_width);//rowline
xoff = dst_buf + (y0 + 1)*lpitch + x0;
memset(xoff, y, rect_width);

xoff = dst_buf + (y1 - 1)*lpitch + x0;
memset(xoff, y, rect_width);//colline
xoff = dst_buf + y1*lpitch + x0;
memset(xoff, y, rect_width);

//y first
for (int i = 0; i < rect_height; i++)
{
yoff = dst_buf + (i + y0)*lpitch + x0;
*yoff = y;
yoff = dst_buf + (i + y0)*lpitch + x0 + 1;
*yoff = y;

yoff = dst_buf + (i + y0)*lpitch + x1 - 1;
*yoff = y;
yoff = dst_buf + (i + y0)*lpitch + x1;
*yoff = y;
}

//cb next
xoff = dst_buf + lpitch * h + y0*lpitch / 4 + x0 / 2;
memset(xoff, cb, rect_width / 2);

xoff = dst_buf + lpitch * h + y1*lpitch / 4 + x0 / 2;
memset(xoff, cb, rect_width / 2);

for (int i = 0; i < rect_height; i += 2)
{
yoff = dst_buf + lpitch * h + (i + y0)*lpitch / 4 + x0 / 2;
*yoff = cb;

yoff = dst_buf + lpitch * h + (i + y0)*lpitch / 4 + x1 / 2;
*yoff = cb;
}

//cr last
xoff = dst_buf + lpitch * h + lpitch * h / 4 + y0*lpitch / 4 + x0 / 2;
memset(xoff, cr, rect_width / 2);

xoff = dst_buf + lpitch * h + lpitch * h / 4 + y1*lpitch / 4 + x0 / 2;
memset(xoff, cr, rect_width / 2);

for (int i = 0; i < rect_height; i += 2)
{
yoff = dst_buf + lpitch * h + lpitch * h / 4 + (i + y0)*lpitch / 4 + x0 / 2;
*yoff = cr;

yoff = dst_buf + lpitch * h + lpitch * h / 4 + (i + y0)*lpitch / 4 + x1 / 2;
*yoff = cr;
}
}

举报

相关推荐

0 条评论