4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: YoungHun Kim <yh8004.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *ranklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <unistd.h> /* fsync() */
29 #include "mm_util_jpeg.h"
30 #include "mm_util_imgp.h"
31 #include "mm_util_private.h"
33 /* maximum width of encoding */
34 #define ENC_MAX_LEN 8192
36 struct my_error_mgr_s {
37 struct jpeg_error_mgr pub; /* "public" fields */
38 jmp_buf setjmp_buffer; /* for return to caller */
41 typedef struct my_error_mgr_s *my_error_ptr;
43 static void __my_error_exit(j_common_ptr cinfo)
45 my_error_ptr myerr = (my_error_ptr) cinfo->err; /* cinfo->err really points to a my_error_mgr_s struct, so coerce pointer */
46 (*cinfo->err->output_message) (cinfo); /* Always display the message. We could postpone this until after returning, if we chose. */
47 longjmp(myerr->setjmp_buffer, 1); /* Return control to the setjmp point */
53 } mm_util_jpeg_cont_format_e;
55 static gboolean __is_supported_color_format_with_libjpeg(mm_util_color_format_e color_format)
57 gboolean _bool = FALSE;
59 if (color_format == MM_UTIL_COLOR_RGB24 ||
60 color_format == MM_UTIL_COLOR_RGBA ||
61 color_format == MM_UTIL_COLOR_BGRA ||
62 color_format == MM_UTIL_COLOR_ARGB ||
63 color_format == MM_UTIL_COLOR_YUV420 ||
64 color_format == MM_UTIL_COLOR_YUV422 ||
65 color_format == MM_UTIL_COLOR_UYVY ||
66 color_format == MM_UTIL_COLOR_GRAYSCALE) {
71 mm_util_error("not supported color format %d", color_format);
76 static gboolean _mm_util_is_supported_color_format(mm_util_color_format_e color_format)
78 gboolean _bool = FALSE;
80 if (__is_supported_color_format_with_libjpeg(color_format) ||
81 color_format == MM_UTIL_COLOR_NV12) {
86 mm_util_error("not supported color format %d", color_format);
91 static int __mm_image_encode_with_libjpeg(mm_util_jpeg_cont_format_e control_format, mm_util_image_h decoded, int quality, FILE *fp, void **mem, size_t *csize)
93 int ret = MM_UTIL_ERROR_NONE;
94 mm_image_info_s *_decoded = (mm_image_info_s *)decoded;
96 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);
97 mm_util_retvm_if((control_format == MM_UTIL_JPEG_FILE) && (fp == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fp");
98 mm_util_retvm_if((control_format == MM_UTIL_JPEG_MEM) && ((mem == NULL) || (csize == NULL)), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src or csize");
99 mm_util_retvm_if(!IS_VALID_IMAGE(decoded), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image");
101 JSAMPROW y[16], cb[16], cr[16]; /* y[2][5] = color sample of row 2 and pixel column 5; (one plane) */
102 JSAMPARRAY data[3]; /* t[0][2][5] = color sample 0 of row 2 and column 5 */
104 struct jpeg_compress_struct cinfo;
105 struct jpeg_error_mgr jerr;
106 int i, j, flag, _width, _height;
107 unsigned long size = 0;
113 mm_util_debug("quality[%d]", quality);
115 cinfo.err = jpeg_std_error(&jerr); /* Errors get written to stderr */
117 jpeg_create_compress(&cinfo);
119 if (control_format == MM_UTIL_JPEG_FILE) {
120 jpeg_stdio_dest(&cinfo, fp);
121 mm_util_debug("jpeg_stdio_dest");
123 jpeg_mem_dest(&cinfo, (unsigned char **)mem, &size);
124 mm_util_debug("jpeg_mem_dest");
127 _width = cinfo.image_width = _decoded->width;
128 _height = cinfo.image_height = _decoded->height;
129 if (_decoded->color == MM_UTIL_COLOR_YUV420 || _decoded->color == MM_UTIL_COLOR_YUV422 || _decoded->color == MM_UTIL_COLOR_UYVY) {
130 flag = cinfo.image_height - MM_UTIL_ROUND_DOWN_16(_height);
132 cinfo.input_components = 3;
133 cinfo.in_color_space = JCS_YCbCr;
134 jpeg_set_defaults(&cinfo);
135 mm_util_debug("jpeg_set_defaults");
137 cinfo.raw_data_in = TRUE; /* Supply downsampled data */
138 cinfo.do_fancy_downsampling = FALSE;
140 cinfo.comp_info[0].h_samp_factor = 2;
141 if (_decoded->color == MM_UTIL_COLOR_YUV420)
142 cinfo.comp_info[0].v_samp_factor = 2;
143 else if (_decoded->color == MM_UTIL_COLOR_YUV422 || _decoded->color == MM_UTIL_COLOR_UYVY)
144 cinfo.comp_info[0].v_samp_factor = 1;
145 cinfo.comp_info[1].h_samp_factor = 1;
146 cinfo.comp_info[1].v_samp_factor = 1;
147 cinfo.comp_info[2].h_samp_factor = 1;
148 cinfo.comp_info[2].v_samp_factor = 1;
150 jpeg_set_quality(&cinfo, quality, TRUE);
151 mm_util_debug("jpeg_set_quality");
152 cinfo.dct_method = JDCT_FASTEST;
154 jpeg_start_compress(&cinfo, TRUE);
155 mm_util_debug("jpeg_start_compress");
158 void *large_rect = calloc(1, _width);
159 void *small_rect = calloc(1, _width);
161 memset(large_rect, 0x10, _width);
163 MMUTIL_SAFE_FREE(small_rect);
164 mm_util_error("large rectangle memory");
165 return MM_UTIL_ERROR_INVALID_PARAMETER;
168 memset(small_rect, 0x80, _width);
170 MMUTIL_SAFE_FREE(large_rect);
171 mm_util_error("small rectangle memory");
172 return MM_UTIL_ERROR_INVALID_PARAMETER;
175 for (j = 0; j < _height; j += 16) {
176 for (i = 0; i < 16; i++) {
177 y[i] = (JSAMPROW)_decoded->data + _width * (i + j);
179 cb[i / 2] = (JSAMPROW)_decoded->data + _width * _height + _width / 2 * ((i + j) / 2);
180 cr[i / 2] = (JSAMPROW)_decoded->data + _width * _height + _width * _height / 4 + _width / 2 * ((i + j) / 2);
183 jpeg_write_raw_data(&cinfo, data, 16);
185 for (i = 0; i < flag; i++) {
186 y[i] = (JSAMPROW)_decoded->data + _width * (i + j);
188 cb[i / 2] = (JSAMPROW)_decoded->data + _width * _height + _width / 2 * ((i + j) / 2);
189 cr[i / 2] = (JSAMPROW)_decoded->data + _width * _height + _width * _height / 4 + _width / 2 * ((i + j) / 2);
192 for (; i < 16; i++) {
193 y[i] = (JSAMPROW)large_rect;
195 cb[i / 2] = (JSAMPROW)small_rect;
196 cr[i / 2] = (JSAMPROW)small_rect;
199 jpeg_write_raw_data(&cinfo, data, 16);
200 MMUTIL_SAFE_FREE(large_rect);
201 MMUTIL_SAFE_FREE(small_rect);
203 for (j = 0; j < _height; j += 16) {
204 for (i = 0; i < 16; i++) {
205 y[i] = (JSAMPROW)_decoded->data + _width * (i + j);
207 cb[i / 2] = (JSAMPROW)_decoded->data + _width * _height + _width / 2 * ((i + j) / 2);
208 cr[i / 2] = (JSAMPROW)_decoded->data + _width * _height + _width * _height / 4 + _width / 2 * ((i + j) / 2);
211 jpeg_write_raw_data(&cinfo, data, 16);
214 mm_util_debug("#for loop#");
216 jpeg_finish_compress(&cinfo);
217 mm_util_debug("jpeg_finish_compress");
219 jpeg_destroy_compress(&cinfo);
220 mm_util_debug("jpeg_destroy_compress");
223 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) {
224 JSAMPROW row_pointer[1];
225 unsigned int iRowStride = 0;
227 if (_decoded->color == MM_UTIL_COLOR_RGB24) {
228 cinfo.input_components = 3;
229 cinfo.in_color_space = JCS_RGB;
230 mm_util_debug("JCS_RGB");
231 } else if (_decoded->color == MM_UTIL_COLOR_GRAYSCALE) {
232 cinfo.input_components = 1; /* one colour component */
233 cinfo.in_color_space = JCS_GRAYSCALE;
234 mm_util_debug("JCS_GRAYSCALE");
235 } else if (_decoded->color == MM_UTIL_COLOR_RGBA) {
236 cinfo.input_components = 4;
237 cinfo.in_color_space = JCS_EXT_RGBA;
238 mm_util_debug("JCS_EXT_RGBA");
239 } else if (_decoded->color == MM_UTIL_COLOR_BGRA) {
240 cinfo.input_components = 4;
241 cinfo.in_color_space = JCS_EXT_BGRA;
242 mm_util_debug("JCS_EXT_BGRA");
243 } else if (_decoded->color == MM_UTIL_COLOR_ARGB) {
244 cinfo.input_components = 4;
245 cinfo.in_color_space = JCS_EXT_ARGB;
246 mm_util_debug("JCS_EXT_ARGB");
249 jpeg_set_defaults(&cinfo);
250 mm_util_debug("jpeg_set_defaults");
251 jpeg_set_quality(&cinfo, quality, TRUE);
252 mm_util_debug("jpeg_set_quality");
253 jpeg_start_compress(&cinfo, TRUE);
254 mm_util_debug("jpeg_start_compress");
255 if (_decoded->color == MM_UTIL_COLOR_RGB24)
256 iRowStride = _width * 3;
257 else if (_decoded->color == MM_UTIL_COLOR_GRAYSCALE)
259 else if (_decoded->color == MM_UTIL_COLOR_RGBA || _decoded->color == MM_UTIL_COLOR_BGRA || _decoded->color == MM_UTIL_COLOR_ARGB)
260 iRowStride = _width * 4;
262 JSAMPLE *image_buffer = (JSAMPLE *)_decoded->data;
263 while (cinfo.next_scanline < cinfo.image_height) {
264 row_pointer[0] = &image_buffer[cinfo.next_scanline * iRowStride];
265 jpeg_write_scanlines(&cinfo, row_pointer, 1);
267 mm_util_debug("while");
269 jpeg_finish_compress(&cinfo);
270 mm_util_debug("jpeg_finish_compress");
272 jpeg_destroy_compress(&cinfo);
273 mm_util_debug("jpeg_destroy_compress");
275 mm_util_error("We can't encode the IMAGE format");
276 return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
279 if (control_format == MM_UTIL_JPEG_MEM)
280 *csize = (size_t)size;
285 static int __mm_image_decode_with_libjpeg(mm_util_jpeg_cont_format_e control_format, FILE *fp, void *src, size_t size, mm_util_color_format_e color_format, mm_util_jpeg_decode_downscale downscale, mm_util_image_h *decoded)
287 int ret = MM_UTIL_ERROR_NONE;
288 struct jpeg_decompress_struct dinfo;
289 struct my_error_mgr_s jerr;
290 JSAMPARRAY buffer; /* Output row buffer */
291 int row_stride = 0; /* physical row width in output buffer */
292 JSAMPROW image, u_image, v_image;
293 JSAMPROW row; /* point to buffer[0] */
299 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);
300 mm_util_retvm_if((control_format == MM_UTIL_JPEG_FILE) && (fp == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fp");
301 mm_util_retvm_if((control_format == MM_UTIL_JPEG_MEM) && ((src == NULL) || (size == 0)), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src or size");
302 mm_util_retvm_if(!decoded, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image handle");
304 /* allocate and initialize JPEG decompression object We set up the normal JPEG error routines, then override error_exit. */
305 dinfo.err = jpeg_std_error(&jerr.pub);
306 mm_util_debug("jpeg_std_error ");
307 jerr.pub.error_exit = __my_error_exit;
308 mm_util_debug("jerr.pub.error_exit ");
310 /* Establish the setjmp return context for __my_error_exit to use. */
311 if (setjmp(jerr.setjmp_buffer)) {
312 /* 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.*/
313 mm_util_error("ERROR setjmp");
314 jpeg_destroy_decompress(&dinfo);
315 return MM_UTIL_ERROR_NO_SUCH_FILE;
318 mm_util_debug("if (setjmp)");
319 /* Now we can initialize the JPEG decompression object. */
320 jpeg_create_decompress(&dinfo);
321 mm_util_debug("jpeg_create_decompress");
323 /*specify data source (eg, a file) */
324 if (control_format == MM_UTIL_JPEG_FILE) {
325 jpeg_stdio_src(&dinfo, fp);
326 mm_util_debug("jpeg_stdio_src");
328 jpeg_mem_src(&dinfo, src, (unsigned long)size);
329 mm_util_debug("jpeg_mem_src");
332 /*read file parameters with jpeg_read_header() */
333 jpeg_read_header(&dinfo, TRUE);
334 mm_util_debug("jpeg_read_header");
336 mm_util_debug("image width: %d height: %d", dinfo.image_width, dinfo.image_height);
337 if (dinfo.image_width > ENC_MAX_LEN || dinfo.image_height > ENC_MAX_LEN) {
339 dinfo.scale_denom = 8;
340 dinfo.do_fancy_upsampling = FALSE;
341 dinfo.do_block_smoothing = FALSE;
342 dinfo.dither_mode = JDITHER_ORDERED;
343 } else if (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1) {
345 dinfo.scale_denom = (unsigned int)downscale;
346 dinfo.do_fancy_upsampling = FALSE;
347 dinfo.do_block_smoothing = FALSE;
348 dinfo.dither_mode = JDITHER_ORDERED;
351 dinfo.dct_method = JDCT_FASTEST;
353 /* set parameters for decompression */
354 if (color_format == MM_UTIL_COLOR_RGB24) {
355 dinfo.out_color_space = JCS_RGB;
356 mm_util_debug("cinfo.out_color_space = JCS_RGB");
357 } else if (color_format == MM_UTIL_COLOR_YUV420 || color_format == MM_UTIL_COLOR_YUV422 || color_format == MM_UTIL_COLOR_UYVY) {
358 dinfo.out_color_space = JCS_YCbCr;
359 mm_util_debug("cinfo.out_color_space = JCS_YCbCr");
360 } else if (color_format == MM_UTIL_COLOR_GRAYSCALE) {
361 dinfo.out_color_space = JCS_GRAYSCALE;
362 mm_util_debug("cinfo.out_color_space = JCS_GRAYSCALE");
363 } else if (color_format == MM_UTIL_COLOR_RGBA) {
364 dinfo.out_color_space = JCS_EXT_RGBA;
365 mm_util_debug("cinfo.out_color_space = JCS_EXT_RGBA");
366 } else if (color_format == MM_UTIL_COLOR_BGRA) {
367 dinfo.out_color_space = JCS_EXT_BGRA;
368 mm_util_debug("cinfo.out_color_space = JCS_EXT_BGRA");
369 } else if (color_format == MM_UTIL_COLOR_ARGB) {
370 dinfo.out_color_space = JCS_EXT_ARGB;
371 mm_util_debug("cinfo.out_color_space = JCS_EXT_ARGB");
374 /* Start decompressor */
375 jpeg_start_decompress(&dinfo);
376 mm_util_debug("jpeg_start_decompress");
378 /* byte-align for YUV format */
379 if (color_format == MM_UTIL_COLOR_YUV420 || color_format == MM_UTIL_COLOR_YUV422) {
380 if (dinfo.output_width % 2 != 0)
381 dinfo.output_width = MM_UTIL_ROUND_DOWN_2(dinfo.output_width);
382 if (dinfo.output_height % 2 != 0)
383 dinfo.output_height = MM_UTIL_ROUND_DOWN_2(dinfo.output_height);
386 /* JSAMPLEs per row in output buffer */
387 row_stride = dinfo.output_width * dinfo.output_components;
389 /* Make a one-row-high sample array that will go away when done with image */
390 buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo, JPOOL_IMAGE, row_stride, 1);
391 mm_util_debug("JPOOL_IMAGE BUFFER (color_format: %d)", color_format);
393 if (color_format == MM_UTIL_COLOR_RGB24 || color_format == MM_UTIL_COLOR_RGBA || color_format == MM_UTIL_COLOR_BGRA || color_format == MM_UTIL_COLOR_ARGB) {
394 _size = dinfo.output_height * row_stride;
395 } else if (color_format == MM_UTIL_COLOR_YUV420) {
396 _size = dinfo.output_height * row_stride / 2;
397 } else if (color_format == MM_UTIL_COLOR_YUV422 || color_format == MM_UTIL_COLOR_UYVY) {
398 _size = dinfo.output_height * dinfo.output_width * 2;
399 } else if (color_format == MM_UTIL_COLOR_GRAYSCALE) {
400 _size = dinfo.output_height * dinfo.output_width;
402 jpeg_finish_decompress(&dinfo);
403 jpeg_destroy_decompress(&dinfo);
404 mm_util_error("[%d] We can't decode the IMAGE format", color_format);
405 return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
408 _data = (void *) calloc(1, _size);
410 jpeg_finish_decompress(&dinfo);
411 jpeg_destroy_decompress(&dinfo);
412 mm_util_error("_data is NULL");
413 return MM_UTIL_ERROR_OUT_OF_MEMORY;
415 mm_util_debug("decoded_data->data");
417 /* while (scan lines remain to be read) jpeg_read_scanlines(...); */
418 if (color_format == MM_UTIL_COLOR_YUV420 || color_format == MM_UTIL_COLOR_YUV422 || color_format == MM_UTIL_COLOR_UYVY) {
420 u_image = image + (dinfo.output_width * dinfo.output_height);
421 v_image = u_image + (dinfo.output_width*dinfo.output_height)/4;
425 while (dinfo.output_scanline < dinfo.output_height) {
426 jpeg_read_scanlines(&dinfo, buffer, 1);
427 for (i = 0; i < row_stride; i += 3) {
430 u_image[(i/3)/2] = row[i+1];
431 v_image[(i/3)/2] = row[i+2];
434 image += row_stride/3;
436 u_image += dinfo.output_width / 2;
437 v_image += dinfo.output_width / 2;
440 } 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) {
442 while (dinfo.output_scanline < dinfo.output_height) {
443 /* jpeg_read_scanlines expects an array of pointers to scanlines. Here the array is only one element long, but you could ask formore than one scanline at a time if that's more convenient. */
444 jpeg_read_scanlines(&dinfo, buffer, 1);
446 memcpy(_data + state, buffer[0], row_stride);
449 mm_util_debug("jpeg_read_scanlines");
452 ret = mm_image_create_image(dinfo.output_width, dinfo.output_height, color_format, _data, _size, decoded);
453 MMUTIL_SAFE_FREE(_data);
455 /* Finish decompression */
456 jpeg_finish_decompress(&dinfo);
457 mm_util_debug("jpeg_finish_decompress");
459 /* Release JPEG decompression object */
460 jpeg_destroy_decompress(&dinfo);
461 mm_util_debug("jpeg_destroy_decompress");
468 int mm_util_jpeg_encode_to_file(mm_util_image_h decoded, int quality, const char *file_path)
470 int ret = MM_UTIL_ERROR_NONE;
471 mm_image_info_s *_decoded = (mm_image_info_s *)decoded;
472 mm_util_image_h _converted_image = NULL;
476 mm_util_retvm_if(!file_path, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid file_path");
477 mm_util_retvm_if(!IS_VALID_IMAGE(decoded), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image");
478 mm_util_retvm_if((!_mm_util_is_supported_color_format(_decoded->color)), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported fmt [%d]", _decoded->color);
479 mm_util_retvm_if((quality < 1) || (quality > 100), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid quality [%d]", quality);
482 ret = mm_util_safe_fopen(file_path, "wb", &fp);
483 mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "mm_util_safe_fopen fail (%d)", ret);
485 if (_decoded->color == MM_UTIL_COLOR_NV12) {
486 ret = mm_util_convert_colorspace(decoded, MM_UTIL_COLOR_YUV420, &_converted_image);
487 if (ret != MM_UTIL_ERROR_NONE) {
488 mm_util_error("mm_util_convert_image failed (%d)", ret);
489 mm_util_safe_fclose(fp);
493 ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_FILE, _converted_image, quality, fp, NULL, NULL);
494 mm_image_destroy_image(_converted_image);
496 ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_FILE, decoded, quality, fp, NULL, NULL);
499 fsync((int)(fp->_fileno));
500 mm_util_safe_fclose(fp);
507 int mm_util_jpeg_encode_to_memory(void **buffer, unsigned int *size, unsigned char *src, unsigned int width, unsigned int height, mm_util_color_format_e color, int quality)
509 int ret = MM_UTIL_ERROR_NONE;
510 size_t encoded_size = 0;
511 mm_util_image_h decoded = NULL;
513 mm_util_retvm_if(!size, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid size");
515 ret = mm_image_create_image(width, height, color, src, TEMP_DATA_SIZE, &decoded);
516 mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "mm_image_create_image fail (%d)", ret);
518 ret = mm_util_encode_to_jpeg_memory(decoded, quality, buffer, &encoded_size);
519 mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "mm_util_encode_to_jpeg_memory fail (%d)", ret);
521 *size = (unsigned int)encoded_size;
526 int mm_util_encode_to_jpeg_memory(mm_util_image_h decoded, int quality, void **buffer, size_t *size)
528 int ret = MM_UTIL_ERROR_NONE;
529 mm_image_info_s *_decoded = (mm_image_info_s *)decoded;
530 mm_util_image_h _converted_image = NULL;
534 mm_util_retvm_if(!buffer, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid buffer");
535 mm_util_retvm_if(!size, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid size");
536 mm_util_retvm_if(!IS_VALID_IMAGE(decoded), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image");
537 mm_util_retvm_if((!_mm_util_is_supported_color_format(_decoded->color)), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported color [%d]", _decoded->color);
538 mm_util_retvm_if((quality < 1) || (quality > 100), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid quality [%d]", quality);
540 if (_decoded->color == MM_UTIL_COLOR_NV12) {
541 ret = mm_util_convert_colorspace(decoded, MM_UTIL_COLOR_YUV420, &_converted_image);
542 mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "mm_util_convert_image fail (%d)", ret);
544 ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_MEM, _converted_image, quality, NULL, buffer, size);
545 mm_image_destroy_image(_converted_image);
547 ret = __mm_image_encode_with_libjpeg(MM_UTIL_JPEG_MEM, decoded, quality, NULL, buffer, size);
555 int mm_util_decode_from_jpeg_file(const char *file_path, mm_util_color_format_e fmt, mm_util_jpeg_decode_downscale downscale, mm_util_image_h *decoded)
557 int ret = MM_UTIL_ERROR_NONE;
559 mm_util_image_h _decoded = NULL;
563 mm_util_retvm_if(!MMUTIL_STRING_VALID(file_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid file_path");
564 mm_util_retvm_if((IS_VALID_COLOR(fmt) == FALSE), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fmt [%d]", fmt);
565 mm_util_retvm_if((!_mm_util_is_supported_color_format(fmt)), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported fmt [%d]", fmt);
566 mm_util_retvm_if(!decoded, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image handle");
568 if ((downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1) && (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_2)
569 && (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_4) && (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_8)) {
570 mm_util_error("#ERROR# downscale value [%d]", downscale);
571 return MM_UTIL_ERROR_INVALID_PARAMETER;
574 ret = mm_util_safe_fopen(file_path, "rb", &fp);
575 mm_util_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "mm_util_safe_fopen fail (%d)", ret);
577 if (fmt == MM_UTIL_COLOR_NV12) {
578 ret = __mm_image_decode_with_libjpeg(MM_UTIL_JPEG_FILE, fp, NULL, 0, MM_UTIL_COLOR_YUV420, downscale, &_decoded);
579 if (ret == MM_UTIL_ERROR_NONE)
580 ret = mm_util_convert_colorspace(_decoded, MM_UTIL_COLOR_NV12, decoded);
581 mm_image_destroy_image(_decoded);
583 ret = __mm_image_decode_with_libjpeg(MM_UTIL_JPEG_FILE, fp, NULL, 0, fmt, downscale, decoded);
586 mm_util_safe_fclose(fp);
593 int mm_util_decode_from_jpeg_memory(void *memory, const size_t src_size, mm_util_color_format_e fmt, mm_util_jpeg_decode_downscale downscale, mm_util_image_h *decoded)
595 int ret = MM_UTIL_ERROR_NONE;
596 mm_util_image_h _decoded = NULL;
600 mm_util_retvm_if(!memory, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid jpeg image");
601 mm_util_retvm_if(!src_size, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src_size");
602 mm_util_retvm_if((IS_VALID_COLOR(fmt) == FALSE), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid fmt [%d]", fmt);
603 mm_util_retvm_if((!_mm_util_is_supported_color_format(fmt)), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported fmt [%d]", fmt);
604 mm_util_retvm_if(!decoded, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image handle");
606 if ((downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1) && (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_2)
607 && (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_4) && (downscale != MM_UTIL_JPEG_DECODE_DOWNSCALE_1_8)) {
608 mm_util_error("#ERROR# downscale value [%d]", downscale);
609 return MM_UTIL_ERROR_INVALID_PARAMETER;
612 if (fmt == MM_UTIL_COLOR_NV12) {
613 ret = __mm_image_decode_with_libjpeg(MM_UTIL_JPEG_MEM, NULL, memory, src_size, MM_UTIL_COLOR_YUV420, downscale, &_decoded);
614 if (ret == MM_UTIL_ERROR_NONE)
615 ret = mm_util_convert_colorspace(_decoded, MM_UTIL_COLOR_NV12, decoded);
616 mm_image_destroy_image(_decoded);
618 ret = __mm_image_decode_with_libjpeg(MM_UTIL_JPEG_MEM, NULL, memory, src_size, fmt, downscale, decoded);