嵌入式linux中YUV转换成RGB算法

2019-07-13 02:06发布

在图像采集中,经常需要将YUV格式转换成RGB格式,本文例举YUV转RGB的算法
void yuvtorgb ( double *rgb,unsigned char *yuv) { int i; rgb[0] = 1.0 * yuv[0] + 0 + 1.402 * (yuv[2] - 128); // r rgb[1] = 1.0 * yuv[0] - 0.34413 * (yuv[1] - 128) - 0.71414 * (yuv[2]-128); // g rgb[2] = 1.0 * yuv[0] + 1.772 * (yuv[1]-128) + 0; // b for(i=0;i<3;i++) { if(rgb[i]>255) rgb[i] = 255; if(rgb[i]<0) rgb[i] = 0; } }
void yuv422convertrgb(unsigned char *yuv_ptr,unsigned char *rgb_ptr,int width,int height) { int i,j,k; int framesize_rgb; double rgb[3]; unsigned char yuv[3]; unsigned char *prgb,*pyuv; framesize_rgb = width * height * 3; prgb = rgb_ptr; pyuv = yuv_ptr; prgb = rgb_ptr + framesize_rgb - width * 3; pyuv++; for(j=0;j
BMP格式转JPEG格式 int Bmp2Jpg(const char *bmp_data, const char *jeg_file, const int width, const int height) { int ret; int depth = 3; JSAMPROW * row_pointer; long rgb_index = 0; int i=0; struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; FILE *outfile; //Convert BMP to JPG cinfo.err = jpeg_std_error(&jerr); //* Now we can initialize the JPEG compression object. jpeg_create_compress(&cinfo); if ((outfile = fopen(jeg_file, "wb")) == NULL) { fprintf(stderr, "can't open %s ", jeg_file); return -1; } jpeg_stdio_dest(&cinfo, outfile); cinfo.image_width = width; //* image width and height, in pixels cinfo.image_height = height; cinfo.input_components = depth; //* # of color components per pixel cinfo.in_color_space = JCS_RGB; //* colorspace of input image jpeg_set_defaults(&cinfo); //Now you can set any non-default parameters you wish to. //Here we just illustrate the use of quality (quantization table) scaling: jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE ); //* limit to baseline-JPEG values jpeg_start_compress(&cinfo, TRUE); //一次写入 int j=0; row_pointer = malloc(height*width*3); char * line[1000]; for(i=0;i