Yuv2Bgr

inline unsigned char clip(int valid)
{
return (valid < 0 ? 0 : (valid > 255 ? 255 : valid));
}

void Yuv2Bgr(unsigned char *bgr24, unsigned char *yuv420, int width, int height)
{
unsigned char Y, U, V;
unsigned char *ybase, *ubase, *vbase;
int x, y, hx, hy, i = 0, hw = (width >> 1);

ybase = yuv420;
ubase = ybase + width * height;
vbase = ubase + ((width * height) >> 2);

for(y = 0; y < height; ++y)
{
hy = (y >> 1);
for(x = 0; x < width; ++x)
{
hx = (x >> 1);
Y = ybase[y * width + x];
U = ubase[hy * hw + hx];
V = vbase[hy * hw + hx];

bgr24[i++] = clip((int)(Y + 1.772 * (U - 128)));
bgr24[i++] = clip((int)(Y - 0.34414 * (U - 128) - 0.71414 * (V - 128)));
bgr24[i++] = clip((int)(Y + 1.402 * (V - 128)));
}
}
}

/*======================邪恶分割线======================*/

#define clip(n) (unsigned char)((0 <= (n) && (n) <= 255) ? (n) : ((n) > 255 ? 255 : 0))

float U1[256], U2[256];
float V1[256], V2[256];
void Init_Yuv2Bgr_Table()
{
for(short i = 0; i < 256; ++i)
{
U1[i] = (float)(1.772 * (i - 128));
U2[i] = (float)(0.34414 * (i - 128));
V1[i] = (float)(0.71414 * (i - 128));
V2[i] = (float)(1.402 * (i - 128));
}
}

void Yuv2Bgr(unsigned char *bgr24, unsigned char *yuv420, int width, int height)
{
float R, G, B;
unsigned char Y, U, V;
unsigned char *ybase, *ubase, *vbase;

const int hw = (width >> 1);
int y, hx, hy = 0, m = 0, n = 0, i = 0;

ybase = yuv420;
ubase = ybase + width * height;
vbase = ubase + ((width * height) >> 2);

for(y = 0; y < height; )
{
for(hx = 0; hx < hw; ++hx)
{
Y = ybase[n];
U = ubase[m];
V = vbase[m];

B = Y + U1[U];
G = Y - U2[U] - V1[V];
R = Y + V2[V];

bgr24[i++] = clip(B);
bgr24[i++] = clip(G);
bgr24[i++] = clip(R);

Y = ybase[++n];

B = Y + U1[U];
G = Y - U2[U] - V1[V];
R = Y + V2[V];

bgr24[i++] = clip(B);
bgr24[i++] = clip(G);
bgr24[i++] = clip(R);

++m, ++n;
}
if(++y % 2 == 0) ++hy;
else m -= hw;
}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值