Yuv420转Jpeg(C语言实现)

YUV2Jpg(in_Y,in_U,in_V,0x400,0x1b0,75,0x420,pData,&dwSize); 

     参数说明:in_Y:输入的420的Y分量;in_U:输入的420的U分量,in_V:输入420的V分量;width:表示图像的宽;height表示图像的高;quality表示量化因子,n_Stride表示Yuv的跨度,即Yuv的宽,因为该函数只能处理16的整数倍,所以width为0x400;pOut表示转化后的数据;pnOutSize为转化后的jpeg文件的大小;

 

       int YUV2Jpg(PBYTE in_Y,PBYTE in_U,PBYTE in_V,int width,int height,int quality,int nStride,PBYTE pOut,DWORD *pnOutSize)  //

{

     PBYTE pYBuf,pUBuf,pVBuf;

     int nYLen = nStride  * height;

     int nUVLen = nStride  * height / 4;

 

     int  nDataLen;

     JPEGINFO JpgInfo;

     ZeroMemory(&JpgInfo,sizeof(JPEGINFO));

 

     JpgInfo.bytenew = 0;

     JpgInfo.bytepos = 7;

 

     pYBuf = (PBYTE)malloc(nYLen);                      //获取Y的值

 

     memcpy(pYBuf,in_Y,nYLen);

 

     pUBuf = (PBYTE)malloc(nYLen);

     pVBuf = (PBYTE)malloc(nYLen);

    

     ProcessUV(pUBuf,in_U,width,height,nStride);        //获取U的值

     ProcessUV(pVBuf,in_V,width,height,nStride);        //获取V

 

     //   GetDataFromSource(pYBuf,pUBuf,pVBuf,in_Y,in_U,in_V,width);

     DivBuff(pYBuf,width,height,nStride,DCTSIZE,DCTSIZE);

     DivBuff(pUBuf,width,height,nStride,DCTSIZE,DCTSIZE);

     DivBuff(pVBuf,width,height,nStride,DCTSIZE,DCTSIZE);

     quality = QualityScaling(quality);

 

     SetQuantTable(std_Y_QT,JpgInfo.YQT, quality); // 设置Y量化表

     SetQuantTable(std_UV_QT,JpgInfo.UVQT,quality); // 设置UV量化表

    

     InitQTForAANDCT(&JpgInfo);                       // 初始化AA&N需要的量化表

     JpgInfo.pVLITAB=JpgInfo.VLI_TAB + 2048;       // 设置VLI_TAB的别名

     BuildVLITable(&JpgInfo);                         // 计算VLI表 

    

    

     nDataLen = 0;

     // 写入各段

     nDataLen = WriteSOI(pOut,nDataLen);

     nDataLen = WriteAPP0(pOut,nDataLen);

     nDataLen = WriteDQT(&JpgInfo,pOut,nDataLen);

     nDataLen = WriteSOF(pOut,nDataLen,width,height);

     nDataLen = WriteDHT(pOut,nDataLen);

     nDataLen = WriteSOS(pOut,nDataLen);

    

     // 计算Y/UV信号的交直分量的huffman表,这里使用标准的huffman表,并不是计算得出,缺点是文件略长,但是速度快

     BuildSTDHuffTab(STD_DC_Y_NRCODES,STD_DC_Y_VALUES,JpgInfo.STD_DC_Y_HT);

     BuildSTDHuffTab(STD_AC_Y_NRCODES,STD_AC_Y_VALUES,JpgInfo.STD_AC_Y_HT);

     BuildSTDHuffTab(STD_DC_UV_NRCODES,STD_DC_UV_VALUES,JpgInfo.STD_DC_UV_HT);

     BuildSTDHuffTab(STD_AC_UV_NRCODES,STD_AC_UV_VALUES,JpgInfo.STD_AC_UV_HT);

    

     // 处理单元数据

     nDataLen = ProcessData(&JpgInfo,pYBuf,pUBuf,pVBuf,width,height,pOut,nDataLen); 

     nDataLen = WriteEOI(pOut,nDataLen);

    

     free(pYBuf);

     free(pUBuf);

     free(pVBuf);

     *pnOutSize = nDataLen;

     return 0;

}

 

具体代码参考YUV2Jpg程序;

JPEG格式的图片换为YUV420SP格式,可以使用libjpeg库来读取JPEG图片,然后使用以下代码将RGB格式的图片换为YUV420SP格式: ```c #include <stdio.h> #include <stdlib.h> #include <jpeglib.h> // Convert RGB to YUV420SP void rgb2yuv(unsigned char *rgb, unsigned char *yuv, int width, int height) { int frameSize = width * height; int yIndex = 0; int uvIndex = frameSize; int r, g, b, y, u, v; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { r = rgb[j * width * 3 + i * 3]; g = rgb[j * width * 3 + i * 3 + 1]; b = rgb[j * width * 3 + i * 3 + 2]; y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16; u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128; v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128; y = (y < 16) ? 16 : ((y > 255) ? 255 : y); u = (u < 0) ? 0 : ((u > 255) ? 255 : u); v = (v < 0) ? 0 : ((v > 255) ? 255 : v); yuv[yIndex++] = (unsigned char)y; if (j % 2 == 0 && i % 2 == 0) { yuv[uvIndex++] = (unsigned char)u; yuv[uvIndex++] = (unsigned char)v; } } } } int main(int argc, char *argv[]) { if (argc != 3) { printf("Usage: %s input.jpg output.yuv\n", argv[0]); return 0; } char *input_file = argv[1]; char *output_file = argv[2]; // Read JPEG image FILE *file = fopen(input_file, "rb"); if (!file) { printf("Error: Unable to open input file %s\n", input_file); return 0; } struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, file); jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); int width = cinfo.output_width; int height = cinfo.output_height; int numChannels = cinfo.output_components; int row_stride = width * numChannels; unsigned char *rgb = (unsigned char *)malloc(width * height * numChannels); unsigned char *yuv = (unsigned char *)malloc(width * height * 3 / 2); JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, row_stride, 1); int row = 0; while (cinfo.output_scanline < cinfo.output_height) { jpeg_read_scanlines(&cinfo, buffer, 1); for (int i = 0; i < row_stride; i += numChannels) { rgb[row * row_stride + i] = buffer[0][i]; rgb[row * row_stride + i + 1] = buffer[0][i + 1]; rgb[row * row_stride + i + 2] = buffer[0][i + 2]; } row++; } jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(file); // Convert RGB to YUV420SP rgb2yuv(rgb, yuv, width, height); // Write YUV420SP image file = fopen(output_file, "wb"); if (!file) { printf("Error: Unable to open output file %s\n", output_file); return 0; } fwrite(yuv, 1, width * height * 3 / 2, file); fclose(file); free(rgb); free(yuv); return 0; } ``` 在程序中,首先使用libjpeg库读取JPEG图片,然后将RGB格式的图片换为YUV420SP格式,最后将YUV420SP格式的图片写入文件。注意,在YUV420SP格式中,Y分量占据前width * height个字节,U和V分量共占据(width * height) / 2个字节。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值