RBG转YUV
RGB文件
RGB文件是原始的没有压缩的包含红绿蓝三种颜色的图像文件。
常见的RGB格式例如RGB24,也就是一组RGB像素中的R、G、B各占8比特,即一个字节,一组RGB一共是24个比特。
YUV文件
YUV文件是与RGB文件同样属于原始的没有压缩的图像文件,只是YUV格式存储的是亮度Y、色差信号U和V。
和YUV不同的是,由大面积着色原理,人眼对于亮度信号的敏感度远强于颜色信号的敏感度。因此色度信号可以比亮度信号取得更少,而视觉上差别不大。
因此YUV又有多种取样结构,如4:4:4、4:2:2、4:2:0等。
RGB与YUV相互转换公式
Y=0.299R+0.587G+0.114B
R−Y=0.701R−0.587G−0.114B
B−Y=−0.299R−0.587G+0.886B
U=-0.1684R-0.3316G+0.5B
V=0.5R-0.4187G-0.0813B
RGB2YUV
#include <stdio.h>
#include "stdlib.h"
#include "rgb2yuv_my.h"
static float RGBYUV0_2990[256], RGBYUV0_5870[256], RGBYUV0_1140[256];
static float RGBYUV0_1684[256], RGBYUV0_3316[256];
static float RGBYUV0_4187[256], RGBYUV0_0813[256];
int RGB2YUV (int width, int height, void *bmp, void *y_out, void *u_out, void *v_out)
{
static int init_done = 0;
int i,j,size;
unsigned char *rp, *gp, *bp;
unsigned char *yp, *up, *vp;
unsigned char *pu1, *pu2, *pv1, *pv2, *psu, *psv;
unsigned char *sub_u_buf, *sub_v_buf;
unsigned char *y_buffer, *u_buffer, *v_buffer;
if (init_done == 0)
{
InitLookupTable();
init_done = 1;
}
size=width*height;
bp = (unsigned char *)bmp;
yp = (unsigned char *)y_out;
sub_u_buf = (unsigned char *)u_out;
sub_v_buf = (unsigned char *)v_out;
up = (unsigned char *)malloc(size * sizeof(unsigned char));
vp = (unsigned char *)malloc(size * sizeof(unsigned char));
if (up==NULL || vp==NULL)
{
printf("wrong up || vp\n");
exit(1);
}
y_buffer=yp;
u_buffer=up;
v_buffer=vp;
for (j = 0; j < height; j ++)//to change
{
for (i = 0; i < width; i ++)
{
gp = bp + 1;
rp = bp + 2;
*yp = (unsigned char)( RGBYUV0_2990[*rp] +