[MPlayer-dev-eng] rgb<->yuv convertion question

Jonas Jensen jbj at knef.dk
Thu Jan 15 10:12:19 CET 2004


On Thu, 2004-01-15 at 02:28, mayaray wrote:
> i put my Clamp function:
> unsigned char Clamp(float i) {
> 	if (i>256.0f)
> 		return 256;
> 	if (i<0.0f)
> 		return 0;
> 	return (unsigned char)i;
> }
> like : pYUV->Y = Clamp(0.257f * (float)pRGB[RINDEX] + 0.504f * (float)pRGB[GINDEX] + 0.098f * (float)pRGB[BINDEX] + 16.0f);

You have to clamp it to 255, not 256, to fit in a byte. And you should
be calculating with integers, not floats.

Here are the rgb->yuv macros from bmovl. They are relatively fast while
still being simple. You should use them with int or unsigned char, and
the results don't need to be clamped.

#define rgb2y(R,G,B)  ( (( 263*R + 516*G + 100*B) >> 10) + 16  )
#define rgb2u(R,G,B)  ( ((-152*R - 298*G + 450*B) >> 10) + 128 )
#define rgb2v(R,G,B)  ( (( 450*R - 376*G -  73*B) >> 10) + 128 )

-- 
Jonas Jensen <jbj at knef.dk>




More information about the MPlayer-dev-eng mailing list