wrong colors with Xv extension and image format id: 0x59565955 (UYVY) from an rgb mapping

Tomas Carnecky tom at dbservice.com
Thu Jan 22 11:34:07 PST 2009


On 01/22/2009 07:35 PM, Amos Tibaldi wrote:
> You both are right, but for now I obtain only solid color that doesn't
> correspond to the desired one; here is the code:
>
> void RGBToUV(unsigned short int r,
> unsigned short int g,
> unsigned short int b,
> unsigned short int * u,
> unsigned short int * v)
> {
> *u = -0.147 * r +
> -0.289 * g +
> 0.436 * b; // min(abs(r * -1214 + g * -2384 + b * 3598 + 4096 + 1048576)
>  >> 13, 240);
> //(unsigned short int)(-0.147f*(float)r-0.289f*(float)g+0.436f*(float)b);
> *v = 0.615 * r +
> -0.515 * g +
> -0.100 * b;
> //min(abs(r * 3598 + g * -3013 + b * -585 + 4096 + 1048576) >> 13, 240);
> //(unsigned short int)(0.615f*(float)r-0.515f*(float)g-0.100f*(float)b);
> }
> void RGBToY(unsigned short int r,
> unsigned short int g,
> unsigned short int b,
> unsigned short int * y
> )
> {
> *y = 0.299 * r +
> 0.587 * g +
> 0.114 * b; // min(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >>
> 13, 235);
> //(unsigned short int)(0.299f*(float)r+0.587f*(float)g+0.114f*(float)b);
> }
>
> void XVWindow::Redraw()
> {
> unsigned short int u, y1, y2, v;
> /*RGBToY(255,0,0,&y1);
> RGBToY(255,0,0,&y2);
> RGBToUV(255,0,0,&u,&v);*/
> /* RGBToY(0,255,0,&y1);
> RGBToY(0,255,0,&y2);
> RGBToUV(0,255,0,&u,&v); */
> RGBToY(0,0,255,&y1);
> RGBToY(0,0,255,&y2);
> RGBToUV(0,0,255,&u,&v);
>
> unsigned char * p = (unsigned char *) BGimage->data;
> for ( int y=0; y<ImageHeight; y++ , p += BGimage->pitches[0] )
> for ( int x=0; x<ImageWidth; x++ )
> {
> p [ (x << 1) + 3 ] = y2;
> p [ (x << 1) + 2 ] = v;
> p [ (x << 1) + 1 ] = y1;
> p [ (x << 1) ] = u;
> }
>
> counter++;
> XvPutImage( xvc.display, xvc.port, window, gc,
> BGimage, 0, 0, ImageWidth, ImageHeight,
> 0, 0, WindowWidth, WindowHeight );
> }
>
> I cannot understand but it works only if I use (x<<1). What can I do to
> associate the colours correctly?

ImageWidth is the width in pixels, but in each iteration you fill in two 
pixels. So you either need 'x<ImageWidth/2' in the for() loop or use 
'x/2' which is what you did.

Also, keep in mind that you are filling only the first line of the whole 
image. The rest of the image probably has undefined/random colors. That 
you are seeing wrong colors could have several causes. Your formulas 
could be wrong (take a look at ffmpeg, xvid or any other projects that 
have rgb-to-yuv functions and borrow their code), endian issues (try 
YUYV or VYVU) or others.

If in doubt, paste the minimal possible sample of your code that 
compiles and runs so we can test it.

tom




More information about the xorg mailing list