YUYV转RGB888,RGB565
我想用ARM9的板子采集摄像头输出(YUYV格式)的视频,并在LCD(显示格式RGB565)上显示,我下了一个程序,能够实现RGB888显示,为了能在LCD上显示,我把RGB888转换为RGB565,转换函数为:
unsigned short ProcessImage::RGB888toRGB565(uchar red, uchar green, uchar blue)
{
unsigned short B = (blue >> 3) & 0x001F;
unsigned short G = ((green >> 2) << 5) & 0x07E0;
unsigned short R = ((red >> 3) << 11) & 0xF800;
return (unsigned short) (R | G | B);
}
该函数是YUYV转换为RGB888格式 int ProcessImage::convert_yuv_to_rgb_pixel(int y, int u, int v)
验证成功的
下面是我修改的,
for(in = 0; in < width * height * 2; in += 4)
{
pixel_16 =
yuv[in + 3] << 24 |
yuv[in + 2] << 16 |
yuv[in + 1] << 8 |
yuv[in + 0];
y0 = (pixel_16 & 0x000000ff);
u = (pixel_16 & 0x0000ff00) >> 8;
y1 = (pixel_16 & 0x00ff0000) >> 16;
v = (pixel_16 & 0xff000000) >> 24;
pixel32 = convert_yuv_to_rgb_pixel(y0, u, v);
pixel_24[0] = (pixel32 & 0x000000ff);
pixel_24[1] = (pixel32 & 0x0000ff00) >> 8;
pixel_24[2] = (pixel32 & 0x00ff0000) >> 16;
// rgb[out++] = pixel_24[0]; 这是原来例程中的函数,提取R,G,B分量
//rgb[out++] = pixel_24[1];
// rgb[out++] = pixel_24[2];
travel=RGB888toRGB565(pixel_24[2],pixel_24[1], pixel_24[0]);
memcpy((void *)rgb,(void *)&travel,2);
// rgb[out++]=travel & 0xff;
//rgb[out++]=(travel>>8)& 0xff;
pixel32 = convert_yuv_to_rgb_pixel(y1, u, v);
pixel_24[0] = (pixel32 & 0x000000ff);
pixel_24[1] = (pixel32 & 0x0000ff00) >> 8;
pixel_24[2] = (pixel32 & 0x00ff0000) >> 16;
// rgb[out++] = pixel_24[0];
//rgb[out++] = pixel_24[1];
//rgb[out++] = pixel_24[2];
travel=RGB888toRGB565(pixel_24[2],pixel_24[1], pixel_24[0]); //我修改的,RGB888转RGB565
rgb+=2;
memcpy((void *)rgb,(void *)&travel,2);
// rgb[out++]=travel & 0xff;
//rgb[out++]=(travel>>8)& 0xff;
}
convert_yuv_to_rgb_buffer(p,pp,320,240/*QWidget::width(),QWidget::height()*/);
frame->loadFromData((uchar *)pp,/*len*/320 *240 * 2 * sizeof(char));
........
painter = new QPainter(this);
frame = new QImage(pp,320,240,QImage::Format_RGB16);