longjmp(myerr->setjmp_buffer, 1); /* Return control to the setjmp point */
}
-static int __mm_image_encode_to_jpeg_file_with_libjpeg(const char *pFileName, void *rawdata, int width, int height, mm_util_color_format_e fmt, int quality)
+typedef enum {
+ MM_UTIL_JPEG_FILE,
+ MM_UTIL_JPEG_MEM,
+} mm_util_jpeg_cont_format_e;
+
+static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_format, FILE *fp, void **mem, unsigned int *csize, void *rawdata, int width, int height, mm_util_color_format_e color_format, int quality)
{
int iErrorCode = MM_UTIL_ERROR_NONE;
- struct jpeg_compress_struct cinfo;
- struct jpeg_error_mgr jerr;
- int i, j, flag, _height;
- FILE *fpWriter;
+ mm_util_retvm_if((control_format != MM_UTIL_JPEG_FILE) && (control_format != MM_UTIL_JPEG_MEM), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid control_format [%u]", control_format);
+ mm_util_retvm_if((control_format == MM_UTIL_JPEG_FILE) && (fp == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fp");
+ mm_util_retvm_if((control_format == MM_UTIL_JPEG_MEM) && ((mem == NULL) || (csize == 0)), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src or csize");
+ mm_util_retvm_if(rawdata == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid rawdata");
JSAMPROW y[16], cb[16], cr[16]; /* y[2][5] = color sample of row 2 and pixel column 5; (one plane) */
JSAMPARRAY data[3]; /* t[0][2][5] = color sample 0 of row 2 and column 5 */
- mm_util_retvm_if(!MMUTIL_STRING_VALID(pFileName), MM_UTIL_ERROR_NO_SUCH_FILE, "invalid path");
+ struct jpeg_compress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+ int i, j, flag, _height;
+ unsigned long size = 0;
data[0] = y;
data[1] = cb;
data[2] = cr;
- mm_util_fenter();
+ mm_util_debug("rawdata[%p] width[%d] height[%d] color_format[%u] quality[%d]", rawdata, width, height, color_format, quality);
- cinfo.err = jpeg_std_error(&jerr);
+ cinfo.err = jpeg_std_error(&jerr); /* Errors get written to stderr */
jpeg_create_compress(&cinfo);
- iErrorCode = mm_util_safe_fopen(pFileName, "wb", &fpWriter);
- if (iErrorCode != MM_UTIL_ERROR_NONE) {
- mm_util_error("mm_util_safe_fopen failed (%d)", iErrorCode);
- jpeg_finish_compress(&cinfo);
- mm_util_debug("jpeg_finish_compress");
-
- jpeg_destroy_compress(&cinfo);
- mm_util_debug("jpeg_destroy_compress");
- return iErrorCode;
+ if (control_format == MM_UTIL_JPEG_FILE) {
+ jpeg_stdio_dest(&cinfo, fp);
+ mm_util_debug("jpeg_stdio_dest");
+ } else {
+ jpeg_mem_dest(&cinfo, (unsigned char **)mem, &size);
+ mm_util_debug("jpeg_mem_dest");
}
- jpeg_stdio_dest(&cinfo, fpWriter);
cinfo.image_width = width;
cinfo.image_height = height;
- if (fmt == MM_UTIL_COLOR_YUV420 || fmt == MM_UTIL_COLOR_YUV422 || fmt == MM_UTIL_COLOR_UYVY) {
- mm_util_debug("[__mm_image_encode_to_jpeg_file_with_libjpeg] jpeg_stdio_dest for YUV");
- mm_util_debug("[Height] %d", height);
+ if (color_format == MM_UTIL_COLOR_YUV420 || color_format == MM_UTIL_COLOR_YUV422 || color_format == MM_UTIL_COLOR_UYVY) {
_height = MM_UTIL_ROUND_DOWN_16(height);
flag = height - _height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_YCbCr;
- mm_util_debug("JCS_YCbCr [%d] ", flag);
-
jpeg_set_defaults(&cinfo);
mm_util_debug("jpeg_set_defaults");
cinfo.do_fancy_downsampling = FALSE;
cinfo.comp_info[0].h_samp_factor = 2;
- if (fmt == MM_UTIL_COLOR_YUV420)
+ if (color_format == MM_UTIL_COLOR_YUV420)
cinfo.comp_info[0].v_samp_factor = 2;
- else if (fmt == MM_UTIL_COLOR_YUV422 || fmt == MM_UTIL_COLOR_UYVY)
+ else if (color_format == MM_UTIL_COLOR_YUV422 || color_format == MM_UTIL_COLOR_UYVY)
cinfo.comp_info[0].v_samp_factor = 1;
cinfo.comp_info[1].h_samp_factor = 1;
cinfo.comp_info[1].v_samp_factor = 1;
memset(large_rect, 0x10, width);
} else {
MMUTIL_SAFE_FREE(small_rect);
- mm_util_safe_fclose(fpWriter);
mm_util_error("large rectangle memory");
return MM_UTIL_ERROR_INVALID_PARAMETER;
}
memset(small_rect, 0x80, width);
} else {
MMUTIL_SAFE_FREE(large_rect);
- mm_util_safe_fclose(fpWriter);
mm_util_error("small rectangle memory");
return MM_UTIL_ERROR_INVALID_PARAMETER;
}
mm_util_debug("jpeg_destroy_compress");
}
- else if (fmt == MM_UTIL_COLOR_RGB24 || fmt == MM_UTIL_COLOR_GRAYSCALE || fmt == MM_UTIL_COLOR_RGBA || fmt == MM_UTIL_COLOR_BGRA || fmt == MM_UTIL_COLOR_ARGB) {
+ else if (color_format == MM_UTIL_COLOR_RGB24 || color_format == MM_UTIL_COLOR_GRAYSCALE || color_format == MM_UTIL_COLOR_RGBA || color_format == MM_UTIL_COLOR_BGRA || color_format == MM_UTIL_COLOR_ARGB) {
JSAMPROW row_pointer[1];
int iRowStride = 0;
- if (fmt == MM_UTIL_COLOR_RGB24) {
+ if (color_format == MM_UTIL_COLOR_RGB24) {
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
mm_util_debug("JCS_RGB");
- } else if (fmt == MM_UTIL_COLOR_GRAYSCALE) {
+ } else if (color_format == MM_UTIL_COLOR_GRAYSCALE) {
cinfo.input_components = 1; /* one colour component */
cinfo.in_color_space = JCS_GRAYSCALE;
mm_util_debug("JCS_GRAYSCALE");
- } else if (fmt == MM_UTIL_COLOR_RGBA) {
- cinfo.input_components = 4; /* one colour component */
+ } else if (color_format == MM_UTIL_COLOR_RGBA) {
+ cinfo.input_components = 4;
cinfo.in_color_space = JCS_EXT_RGBA;
mm_util_debug("JCS_EXT_RGBA");
- } else if (fmt == MM_UTIL_COLOR_BGRA) {
+ } else if (color_format == MM_UTIL_COLOR_BGRA) {
cinfo.input_components = 4;
cinfo.in_color_space = JCS_EXT_BGRA;
mm_util_debug("JCS_EXT_BGRA");
- } else if (fmt == MM_UTIL_COLOR_ARGB) {
+ } else if (color_format == MM_UTIL_COLOR_ARGB) {
cinfo.input_components = 4;
cinfo.in_color_space = JCS_EXT_ARGB;
mm_util_debug("JCS_EXT_ARGB");
mm_util_debug("jpeg_set_quality");
jpeg_start_compress(&cinfo, TRUE);
mm_util_debug("jpeg_start_compress");
- if (fmt == MM_UTIL_COLOR_RGB24)
+ if (color_format == MM_UTIL_COLOR_RGB24)
iRowStride = width * 3;
- else if (fmt == MM_UTIL_COLOR_GRAYSCALE)
+ else if (color_format == MM_UTIL_COLOR_GRAYSCALE)
iRowStride = width;
- else if (fmt == MM_UTIL_COLOR_RGBA || fmt == MM_UTIL_COLOR_BGRA || fmt == MM_UTIL_COLOR_ARGB)
+ else if (color_format == MM_UTIL_COLOR_RGBA || color_format == MM_UTIL_COLOR_BGRA || color_format == MM_UTIL_COLOR_ARGB)
iRowStride = width * 4;
JSAMPLE *image_buffer = (JSAMPLE *)rawdata;
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
mm_util_debug("while");
+
jpeg_finish_compress(&cinfo);
mm_util_debug("jpeg_finish_compress");
mm_util_debug("jpeg_destroy_compress");
} else {
mm_util_error("We can't encode the IMAGE format");
- mm_util_safe_fclose(fpWriter);
return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
}
- fsync((int)(fpWriter->_fileno));
- mm_util_debug("[fsync] FILE");
- mm_util_safe_fclose(fpWriter);
- return iErrorCode;
-}
-static int __mm_image_encode_to_jpeg_memory_with_libjpeg(void **mem, unsigned int *csize, void *rawdata, int width, int height, mm_util_color_format_e fmt, int quality)
-{
- int iErrorCode = MM_UTIL_ERROR_NONE;
+ if (control_format == MM_UTIL_JPEG_MEM)
+ *csize = (unsigned int)size;
- JSAMPROW y[16], cb[16], cr[16]; /* y[2][5] = color sample of row 2 and pixel column 5; (one plane) */
- JSAMPARRAY data[3]; /* t[0][2][5] = color sample 0 of row 2 and column 5 */
-
- struct jpeg_compress_struct cinfo;
- struct jpeg_error_mgr jerr;
- int i, j, flag, _height;
- unsigned long size = 0;
- *csize = 0;
- data[0] = y;
- data[1] = cb;
- data[2] = cr;
-
- mm_util_debug("#Before Enter#, mem: %p\t rawdata:%p\t width: %d\t height: %d\t fmt: %d\t quality: %d", mem, rawdata, width, height, fmt, quality);
-
- mm_util_retvm_if(mem == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid mem");
- mm_util_retvm_if(rawdata == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid rawdata");
-
- cinfo.err = jpeg_std_error(&jerr); /* Errors get written to stderr */
-
- jpeg_create_compress(&cinfo);
-
- mm_util_debug("[__mm_image_encode_to_jpeg_memory_with_libjpeg] #After jpeg_mem_dest#, mem: %p\t width: %d\t height: %d\t fmt: %d\t quality: %d"
- , mem, width, height, fmt, quality);
-
- jpeg_mem_dest(&cinfo, (unsigned char **)mem, &size);
- cinfo.image_width = width;
- cinfo.image_height = height;
- if (fmt == MM_UTIL_COLOR_YUV420 || fmt == MM_UTIL_COLOR_YUV422 || fmt == MM_UTIL_COLOR_UYVY) {
- mm_util_debug("[__mm_image_encode_to_jpeg_file_with_libjpeg] jpeg_stdio_dest for YUV");
- _height = MM_UTIL_ROUND_DOWN_16(height);
- flag = height - _height;
-
- cinfo.input_components = 3;
- cinfo.in_color_space = JCS_YCbCr;
- jpeg_set_defaults(&cinfo);
- mm_util_debug("jpeg_set_defaults");
-
- cinfo.raw_data_in = TRUE; /* Supply downsampled data */
- cinfo.do_fancy_downsampling = FALSE;
-
- cinfo.comp_info[0].h_samp_factor = 2;
- if (fmt == MM_UTIL_COLOR_YUV420)
- cinfo.comp_info[0].v_samp_factor = 2;
- else if (fmt == MM_UTIL_COLOR_YUV422 || fmt == MM_UTIL_COLOR_UYVY)
- cinfo.comp_info[0].v_samp_factor = 1;
- cinfo.comp_info[1].h_samp_factor = 1;
- cinfo.comp_info[1].v_samp_factor = 1;
- cinfo.comp_info[2].h_samp_factor = 1;
- cinfo.comp_info[2].v_samp_factor = 1;
-
- jpeg_set_quality(&cinfo, quality, TRUE);
- mm_util_debug("jpeg_set_quality");
- cinfo.dct_method = JDCT_FASTEST;
-
- jpeg_start_compress(&cinfo, TRUE);
- mm_util_debug("jpeg_start_compress");
-
- if (flag) {
- void *large_rect = calloc(1, width);
- void *small_rect = calloc(1, width);
- if (large_rect) {
- memset(large_rect, 0x10, width);
- } else {
- MMUTIL_SAFE_FREE(small_rect);
- mm_util_error("large rectangle memory");
- return MM_UTIL_ERROR_INVALID_PARAMETER;
- }
- if (small_rect) {
- memset(small_rect, 0x80, width);
- } else {
- MMUTIL_SAFE_FREE(large_rect);
- mm_util_error("small rectangle memory");
- return MM_UTIL_ERROR_INVALID_PARAMETER;
- }
-
- for (j = 0; j < _height; j += 16) {
- for (i = 0; i < 16; i++) {
- y[i] = (JSAMPROW)rawdata + width * (i + j);
- if (i % 2 == 0) {
- cb[i / 2] = (JSAMPROW)rawdata + width * height + width / 2 * ((i + j) / 2);
- cr[i / 2] = (JSAMPROW)rawdata + width * height + width * height / 4 + width / 2 * ((i + j) / 2);
- }
- }
- jpeg_write_raw_data(&cinfo, data, 16);
- }
- for (i = 0; i < flag; i++) {
- y[i] = (JSAMPROW)rawdata + width * (i + j);
- if (i % 2 == 0) {
- cb[i / 2] = (JSAMPROW)rawdata + width * height + width / 2 * ((i + j) / 2);
- cr[i / 2] = (JSAMPROW)rawdata + width * height + width * height / 4 + width / 2 * ((i + j) / 2);
- }
- }
- for (; i < 16; i++) {
- y[i] = (JSAMPROW)large_rect;
- if (i % 2 == 0) {
- cb[i / 2] = (JSAMPROW)small_rect;
- cr[i / 2] = (JSAMPROW)small_rect;
- }
- }
- jpeg_write_raw_data(&cinfo, data, 16);
- MMUTIL_SAFE_FREE(large_rect);
- MMUTIL_SAFE_FREE(small_rect);
- } else {
- for (j = 0; j < height; j += 16) {
- for (i = 0; i < 16; i++) {
- y[i] = (JSAMPROW)rawdata + width * (i + j);
- if (i % 2 == 0) {
- cb[i / 2] = (JSAMPROW)rawdata + width * height + width / 2 * ((i + j) / 2);
- cr[i / 2] = (JSAMPROW)rawdata + width * height + width * height / 4 + width / 2 * ((i + j) / 2);
- }
- }
- jpeg_write_raw_data(&cinfo, data, 16);
- }
- }
- mm_util_debug("#for loop#");
-
- jpeg_finish_compress(&cinfo);
- mm_util_debug("jpeg_finish_compress");
- jpeg_destroy_compress(&cinfo);
- mm_util_debug("Exit jpeg_destroy_compress, mem: %p\t size:%u", *mem, *csize);
- }
-
- else if (fmt == MM_UTIL_COLOR_RGB24 || fmt == MM_UTIL_COLOR_GRAYSCALE || fmt == MM_UTIL_COLOR_RGBA || fmt == MM_UTIL_COLOR_BGRA || fmt == MM_UTIL_COLOR_ARGB) {
- JSAMPROW row_pointer[1];
- int iRowStride = 0;
-
- mm_util_debug("MM_UTIL_COLOR_RGB24");
- if (fmt == MM_UTIL_COLOR_RGB24) {
- cinfo.input_components = 3;
- cinfo.in_color_space = JCS_RGB;
- mm_util_debug("JCS_RGB");
- } else if (fmt == MM_UTIL_COLOR_GRAYSCALE) {
- cinfo.input_components = 1; /* one colour component */
- cinfo.in_color_space = JCS_GRAYSCALE;
- mm_util_debug("JCS_GRAYSCALE");
- } else if (fmt == MM_UTIL_COLOR_RGBA) {
- cinfo.input_components = 4;
- cinfo.in_color_space = JCS_EXT_RGBA;
- mm_util_debug("JCS_EXT_RGBA");
- } else if (fmt == MM_UTIL_COLOR_BGRA) {
- cinfo.input_components = 4;
- cinfo.in_color_space = JCS_EXT_BGRA;
- mm_util_debug("JCS_EXT_BGRA");
- } else if (fmt == MM_UTIL_COLOR_ARGB) {
- cinfo.input_components = 4;
- cinfo.in_color_space = JCS_EXT_ARGB;
- mm_util_debug("JCS_EXT_ARGB");
- }
-
- jpeg_set_defaults(&cinfo);
- mm_util_debug("jpeg_set_defaults");
- jpeg_set_quality(&cinfo, quality, TRUE);
- mm_util_debug("jpeg_set_quality");
- jpeg_start_compress(&cinfo, TRUE);
- mm_util_debug("jpeg_start_compress");
- if (fmt == MM_UTIL_COLOR_RGB24)
- iRowStride = width * 3;
- else if (fmt == MM_UTIL_COLOR_GRAYSCALE)
- iRowStride = width;
- else if (fmt == MM_UTIL_COLOR_RGBA || fmt == MM_UTIL_COLOR_BGRA || fmt == MM_UTIL_COLOR_ARGB)
- iRowStride = width * 4;
-
- JSAMPLE *image_buffer = (JSAMPLE *)rawdata;
- while (cinfo.next_scanline < cinfo.image_height) {
- row_pointer[0] = &image_buffer[cinfo.next_scanline * iRowStride];
- jpeg_write_scanlines(&cinfo, row_pointer, 1);
- }
- mm_util_debug("while");
- jpeg_finish_compress(&cinfo);
- mm_util_debug("jpeg_finish_compress");
-
- jpeg_destroy_compress(&cinfo);
-
- mm_util_debug("Exit");
- } else {
- mm_util_error("We can't encode the IMAGE format");
- return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
- }
- *csize = (unsigned int)size;
return iErrorCode;
}
-typedef enum {
- MM_UTIL_JPEG_FILE,
- MM_UTIL_JPEG_MEM,
-} mm_util_jpeg_input_format_e;
-
-static int __mm_image_decode_with_libjpeg(mm_util_jpeg_input_format_e input_format, mm_util_jpeg_yuv_data *decoded_data, FILE *fp, void *src, unsigned int size, mm_util_color_format_e color_format, mm_util_jpeg_decode_downscale downscale)
+static int __mm_image_decode_with_libjpeg(mm_util_jpeg_cont_format_e control_format, mm_util_jpeg_yuv_data *decoded_data, FILE *fp, void *src, unsigned int size, mm_util_color_format_e color_format, mm_util_jpeg_decode_downscale downscale)
{
int iErrorCode = MM_UTIL_ERROR_NONE;
struct jpeg_decompress_struct dinfo;
mm_util_fenter();
mm_util_retvm_if(decoded_data == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid decoded_data");
- mm_util_retvm_if((input_format != MM_UTIL_JPEG_FILE) && (input_format != MM_UTIL_JPEG_MEM), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid input_format [%u]", input_format);
- mm_util_retvm_if((input_format == MM_UTIL_JPEG_FILE) && (fp == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fp");
- mm_util_retvm_if((input_format == MM_UTIL_JPEG_MEM) && ((src == NULL) || (size == 0)), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src or size");
+ mm_util_retvm_if((control_format != MM_UTIL_JPEG_FILE) && (control_format != MM_UTIL_JPEG_MEM), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid control_format [%u]", control_format);
+ mm_util_retvm_if((control_format == MM_UTIL_JPEG_FILE) && (fp == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fp");
+ mm_util_retvm_if((control_format == MM_UTIL_JPEG_MEM) && ((src == NULL) || (size == 0)), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src or size");
/* allocate and initialize JPEG decompression object We set up the normal JPEG error routines, then override error_exit. */
dinfo.err = jpeg_std_error(&jerr.pub);
mm_util_debug("jpeg_create_decompress");
/*specify data source (eg, a file) */
- if (input_format == MM_UTIL_JPEG_FILE) {
+ if (control_format == MM_UTIL_JPEG_FILE) {
jpeg_stdio_src(&dinfo, fp);
mm_util_debug("jpeg_stdio_src");
} else {
ret = __mm_image_encode_to_jpeg_file_with_libjpeg_turbo(filename, src, width, height, fmt, quality);
#else
mm_util_debug("#START# LIBJPEG");
+ FILE *fp = NULL;
+ ret = mm_util_safe_fopen(filename, "wb", &fp);
+ if (ret != MM_UTIL_ERROR_NONE) {
+ mm_util_error("mm_util_safe_fopen failed (%d)", ret);
+ return ret;
+ }
+
if (fmt == MM_UTIL_COLOR_NV12) {
unsigned int res_w = 0;
unsigned int res_h = 0;
unsigned char *dst = NULL;
ret = mm_util_convert_colorspace(src, width, height, MM_UTIL_COLOR_NV12, MM_UTIL_COLOR_YUV420, &dst, &res_w, &res_h, &res_buffer_size);
- ret = __mm_image_encode_to_jpeg_file_with_libjpeg(filename, dst, width, height, MM_UTIL_COLOR_YUV420, quality);
+ ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_FILE, fp, NULL, NULL, dst, width, height, MM_UTIL_COLOR_YUV420, quality);
MMUTIL_SAFE_FREE(dst);
} else if (fmt == MM_UTIL_COLOR_NV21) {
- return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
+ mm_util_error("Not supported format NV12");
+ ret = MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
+
} else {
- ret = __mm_image_encode_to_jpeg_file_with_libjpeg(filename, src, width, height, fmt, quality);
+ ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_FILE, fp, NULL, NULL, src, width, height, fmt, quality);
}
+
+ fsync((int)(fp->_fileno));
+ mm_util_safe_fclose(fp);
#endif
mm_util_debug("#End# libjpeg, Success!! ret: %d", ret);
unsigned char *dst = NULL;
ret = mm_util_convert_colorspace(src, width, height, MM_UTIL_COLOR_NV12, MM_UTIL_COLOR_YUV420, &dst, &res_w, &res_h, &res_buffer_size);
- ret = __mm_image_encode_to_jpeg_memory_with_libjpeg(mem, size, dst, width, height, MM_UTIL_COLOR_YUV420, quality);
+ ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_MEM, NULL, mem, size, dst, width, height, MM_UTIL_COLOR_YUV420, quality);
MMUTIL_SAFE_FREE(dst);
} else if (fmt == MM_UTIL_COLOR_NV21) {
return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
} else {
- ret = __mm_image_encode_to_jpeg_memory_with_libjpeg(mem, size, src, width, height, fmt, quality);
+ ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_MEM, NULL, mem, size, src, width, height, fmt, quality);
}
#endif /* LIBJPEG_TURBO */
mm_util_debug("#END# libjpeg, Success!! ret: %d", ret);