YCbCr与RGB转换
有些平台上的颜色要求是YCbCr的. 通常对颜色值上,RGB用的比较多.
下列是一个转换示例.
核心是两个函数中的各3行. 怎么封装可自行订制.
struct osd_rgb {
int r;
int g;
int b;
};
struct osd_ycbcr {
int y;
int cb;
int cr;
};
int osd_rgb_to_ycbcr(struct osd_ycbcr* ycbcr, const struct osd_rgb* rgb)
{
int ret = 0;
float y = 0.257*(float)(rgb->r) + 0.504*(float)(rgb->g) + 0.098*(float)(rgb->b) + 16;
float cb = -0.148*(float)(rgb->r) - 0.291*(float)(rgb->g) + 0.439*(float)(rgb->b) + 128;
float cr = 0.439*(float)(rgb->r) - 0.368*(float)(rgb->g) - 0.071*(float)(rgb->b) + 128;
ycbcr->y = y;
ycbcr->cb = cb;
ycbcr->cr = cr;
return ret;
}
int osd_ycbcr_to_rgb(struct osd_rgb* rgb, const struct osd_ycbcr* ycbcr)
{
int ret = 0;
float r = 1.164*((float)(ycbcr->y)-16) + 1.596*((float)(ycbcr->cr)-128);
float g = 1.164*((float)(ycbcr->y)-16) - 0.813*((float)(ycbcr->cr)-128) - 0.392*((float)(ycbcr->cb)-128);
float b = 1.164*((float)(ycbcr->y)-16) + 2.017*((float)(ycbcr->cb)-128);
rgb->r = r;
rgb->g = g;
rgb->b = b;
return ret;
}