YCbCr To RGB

This algorithm converts YCbCr color space to RGB color model.



									struct RGB
{
	unsigned char R;
	unsigned char G;
	unsigned char B;
};

struct YCbCr
{
	float Y;
	float Cb;
	float Cr;
};

static float Min(float a, float b) {
	return a <= b ? a : b;
}

static float Max(float a, float b) {
	return a >= b ? a : b;
}

struct RGB YCbCrToRGB(struct YCbCr ycbcr) {
	float r = Max(0.0f, Min(1.0f, (float)(ycbcr.Y + 0.0000 * ycbcr.Cb + 1.4022 * ycbcr.Cr)));
	float g = Max(0.0f, Min(1.0f, (float)(ycbcr.Y - 0.3456 * ycbcr.Cb - 0.7145 * ycbcr.Cr)));
	float b = Max(0.0f, Min(1.0f, (float)(ycbcr.Y + 1.7710 * ycbcr.Cb + 0.0000 * ycbcr.Cr)));

	struct RGB rgb;
	rgb.R = r * 255;
	rgb.G = g * 255;
	rgb.B = b * 255;

	return rgb;
}
								


Example

									struct YCbCr data = { 0.28f, 0.21f, 0.34f };
struct RGB value = YCbCrToRGB(data);
								


Output

									R: 192
G: 0
B: 166