MM_UTIL_JPEG_MEM,
} mm_util_jpeg_cont_format_e;
-struct my_error_mgr_s {
+typedef struct my_error_mgr_s {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
} my_error_mgr_s;
typedef struct my_error_mgr_s *my_error_ptr;
+typedef struct jpeg_error_mgr *jpeg_error_ptr;
static void __my_error_exit(j_common_ptr cinfo)
{
return _bool;
}
+static int __jpeg_set_error_handler(my_error_ptr jerr, jpeg_error_ptr *err)
+{
+ (jerr->pub).error_exit = __my_error_exit;
+ mm_util_debug("jerr.pub.error_exit ");
+
+ *err = jpeg_std_error((jpeg_error_ptr)&(jerr->pub));
+ mm_util_debug("jpeg_std_error ");
+
+ /* Establish the setjmp return context for __my_error_exit to use. */
+ if (setjmp(jerr->setjmp_buffer)) {
+ /* If we get here, the JPEG code has signaled an error. We need to clean up the JPEG object, close the input file, and return.*/
+ mm_util_error("ERROR setjmp");
+ return MM_UTIL_ERROR_INVALID_OPERATION;
+ }
+ mm_util_debug("if (setjmp)");
+
+ return MM_UTIL_ERROR_NONE;
+}
+
static int _mm_util_jpeg_encode(mm_util_jpeg_cont_format_e control_format, mm_util_image_h decoded, int quality, FILE *fp, void **mem, size_t *csize)
{
int ret = MM_UTIL_ERROR_NONE;
JSAMPARRAY data[3]; /* t[0][2][5] = color sample 0 of row 2 and column 5 */
struct jpeg_compress_struct cinfo;
- struct my_error_mgr_s jerr;
+ my_error_mgr_s jerr;
int i, j, flag, _width, _height;
unsigned long size = 0;
mm_util_debug("quality[%d]", quality);
- /* Errors get written to stderr */
- cinfo.err = jpeg_std_error(&jerr.pub);
- mm_util_debug("jpeg_std_error ");
- jerr.pub.error_exit = __my_error_exit;
- mm_util_debug("jerr.pub.error_exit ");
-
- /* Establish the setjmp return context for __my_error_exit to use. */
- if (setjmp(jerr.setjmp_buffer)) {
- /* If we get here, the JPEG code has signaled an error. We need to clean up the JPEG object, close the input file, and return.*/
- mm_util_error("ERROR setjmp");
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
+ ret = __jpeg_set_error_handler(&jerr, &cinfo.err);
+ mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "Can't set error handler");
jpeg_create_compress(&cinfo);
}
}
mm_util_debug("#for loop#");
-
- jpeg_finish_compress(&cinfo);
- mm_util_debug("jpeg_finish_compress");
-
- jpeg_destroy_compress(&cinfo);
- mm_util_debug("jpeg_destroy_compress");
}
else if (_decoded->color == MM_UTIL_COLOR_RGB24 || _decoded->color == MM_UTIL_COLOR_GRAYSCALE || _decoded->color == MM_UTIL_COLOR_RGBA || _decoded->color == MM_UTIL_COLOR_BGRA || _decoded->color == MM_UTIL_COLOR_ARGB) {
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("jpeg_destroy_compress");
} else {
mm_util_error("We can't encode the IMAGE format");
return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
}
+ jpeg_finish_compress(&cinfo);
+ mm_util_debug("jpeg_finish_compress");
+
+ /* The size should been updated after compress finished. */
if (control_format == MM_UTIL_JPEG_MEM)
*csize = (size_t)size;
+ jpeg_destroy_compress(&cinfo);
+ mm_util_debug("jpeg_destroy_compress");
+
return ret;
}
{
int ret = MM_UTIL_ERROR_NONE;
struct jpeg_decompress_struct dinfo;
- struct my_error_mgr_s jerr;
+ my_error_mgr_s jerr;
JSAMPARRAY buffer; /* Output row buffer */
int row_stride = 0; /* physical row width in output buffer */
JSAMPROW image, u_image, v_image;
mm_util_retvm_if(!decoded, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image handle");
/* 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_std_error ");
- jerr.pub.error_exit = __my_error_exit;
- mm_util_debug("jerr.pub.error_exit ");
+ ret = __jpeg_set_error_handler(&jerr, &dinfo.err);
+ mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "Can't set error handler");
- /* Establish the setjmp return context for __my_error_exit to use. */
- if (setjmp(jerr.setjmp_buffer)) {
- /* If we get here, the JPEG code has signaled an error. We need to clean up the JPEG object, close the input file, and return.*/
- mm_util_error("ERROR setjmp");
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
-
- mm_util_debug("if (setjmp)");
/* Now we can initialize the JPEG decompression object. */
jpeg_create_decompress(&dinfo);
mm_util_debug("jpeg_create_decompress");
_data = (void *) calloc(1, _size);
if (!_data) {
- jpeg_finish_decompress(&dinfo);
- jpeg_destroy_decompress(&dinfo);
mm_util_error("_data is NULL");
- return MM_UTIL_ERROR_OUT_OF_MEMORY;
+ ret = MM_UTIL_ERROR_OUT_OF_MEMORY;
+ goto END;
}
mm_util_debug("decoded_data->data");
ret = mm_image_create_image(dinfo.output_width, dinfo.output_height, color_format, _data, _size, decoded);
MMUTIL_SAFE_FREE(_data);
+END:
/* Finish decompression */
jpeg_finish_decompress(&dinfo);
mm_util_debug("jpeg_finish_decompress");