From 738e7680562ed1f1cfadd9bbd68c3a77ef2ec144 Mon Sep 17 00:00:00 2001 From: "freddy.li" Date: Thu, 18 Nov 2021 17:32:27 +0800 Subject: [PATCH] update jpeg --- jpeg_operation.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ jpeg_operation.h | 3 ++ 2 files changed, 77 insertions(+) diff --git a/jpeg_operation.c b/jpeg_operation.c index dc9e3e9..6b3e81c 100644 --- a/jpeg_operation.c +++ b/jpeg_operation.c @@ -484,4 +484,78 @@ int yuv420_NV12_to_jpg(char *filename, int img_width, int img_height, unsigned c } +int yuv422_UYVY_to_jpg(char *filename, int img_width, int img_height, unsigned char *pYUVBuffer, int width, int height) +{ + HeliosFILE *pJpegFile = NULL; + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; + JSAMPROW row_pointer[1]; + int i = 0, j = 0; + unsigned char *ptr; + unsigned char yuvbuf[width * 3]; + uint_32 yuv_i = 0,yuv_j = 0; + + float zoom_out_val_w_f = 0.0; + float zoom_out_val_h_f = 0.0; + int index = 0; + + cinfo.err = jpeg_std_error(&jerr);//用于错误信息 + jpeg_create_compress(&cinfo); //初始化压缩对象 + + if ((pJpegFile = Helios_fopen(filename, "wb")) == NULL) + { + return -1; + } + jpeg_stdio_dest(&cinfo, pJpegFile); + + cinfo.image_width = img_width;//设置输入图片宽度 + cinfo.image_height = img_height;//设置图片高度 + cinfo.input_components = 3; + cinfo.in_color_space = JCS_YCbCr;//设置输入图片的格式,支持RGB/YUV/YCC等等 + jpeg_set_defaults(&cinfo);//其它参数设置为默认的! + jpeg_set_quality(&cinfo, 80, TRUE);//设置转化图片质量,范围0-100 + jpeg_start_compress(&cinfo, TRUE); + + memset(yuvbuf, 0, img_width*3); + + zoom_out_val_w_f = (float)((float)width / (float)img_width); + zoom_out_val_h_f = (float)((float)height / (float)img_height); + + JPEG_LOG("zoom %f, %f\n", zoom_out_val_w_f, zoom_out_val_h_f); + + j = 1; + + ptr = pYUVBuffer; + + while (cinfo.next_scanline < cinfo.image_height) { + index = 0; + for (i = 0; i < img_width; i ++){//输入的YUV图片格式为标准的YUV444格式,所以需要把YUV422转化成YUV444. + + yuv_i = (uint_32)(i * zoom_out_val_w_f); + + yuvbuf[index++] = *(ptr + (yuv_i*2)+1); + yuvbuf[index++] = *(ptr + (yuv_i/2)*4); + yuvbuf[index++] = *(ptr + (yuv_i/2)*4 + 2); + + } + + row_pointer[0] = yuvbuf; + (void)jpeg_write_scanlines(&cinfo, row_pointer, 1);//单行图片转换压缩 + yuv_j = (uint_32)(j * zoom_out_val_h_f); + ptr = pYUVBuffer + yuv_j*width*2; + j++; + } + + + + jpeg_finish_compress(&cinfo); + jpeg_destroy_compress(&cinfo); + + if(0 != Helios_fclose(pJpegFile)) { + JPEG_LOG("fs close failed\n"); + return -1; + } + return 0; +} + diff --git a/jpeg_operation.h b/jpeg_operation.h index 15525b0..9c7ee2a 100644 --- a/jpeg_operation.h +++ b/jpeg_operation.h @@ -32,6 +32,9 @@ int yuv420_nv12_to_rgb888(uint_8 *pRgb888Data, uint_32 rgbWidth, uint_32 rgbHeig int yuv420_NV12_to_jpg(char *filename, int img_width, int img_height, unsigned char *pYUVBuffer, int width, int height); +int yuv422_UYVY_to_jpg(char *filename, int img_width, int img_height, unsigned char *pYUVBuffer, int width, int height); + + #ifdef __cplusplus } #endif -- Gitee