2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <mm_util_imgp.h>
20 #include <mm_util_jpeg.h>
21 #include <mm_util_png.h>
22 #include <mm_util_gif.h>
23 #include <mm_util_bmp.h>
24 #include <image_util.h>
25 #include <image_util_private.h>
29 #define IMAGE_UTIL_STRING_VALID(str) \
30 ((str != NULL && strlen(str) > 0) ? true : false)
32 #define IMAGE_UTIL_SAFE_FREE(src) { if (src) {free(src); src = NULL; } }
34 static int _convert_colorspace_tbl[] = {
35 MM_UTIL_IMG_FMT_YUV420, /* IMAGE_UTIL_COLORSPACE_YUV420 */
36 MM_UTIL_IMG_FMT_YUV422, /* IMAGE_UTIL_COLORSPACE_YUV422 */
37 MM_UTIL_IMG_FMT_I420, /* IMAGE_UTIL_COLORSPACE_I420 */
38 MM_UTIL_IMG_FMT_NV12, /* IMAGE_UTIL_COLORSPACE_NV12 */
39 MM_UTIL_IMG_FMT_UYVY, /* IMAGE_UTIL_COLORSPACE_UYVY */
40 MM_UTIL_IMG_FMT_YUYV, /* IMAGE_UTIL_COLORSPACE_YUYV */
41 MM_UTIL_IMG_FMT_RGB565, /* IMAGE_UTIL_COLORSPACE_RGB565 */
42 MM_UTIL_IMG_FMT_RGB888, /* IMAGE_UTIL_COLORSPACE_RGB888 */
43 MM_UTIL_IMG_FMT_ARGB8888, /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
44 MM_UTIL_IMG_FMT_BGRA8888, /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
45 MM_UTIL_IMG_FMT_RGBA8888, /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
46 MM_UTIL_IMG_FMT_BGRX8888, /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
47 MM_UTIL_JPEG_FMT_NV21, /* IMAGE_UTIL_COLORSPACE_NV12 */
48 MM_UTIL_JPEG_FMT_NV16, /* IMAGE_UTIL_COLORSPACE_NV16 */
49 MM_UTIL_JPEG_FMT_NV61, /* IMAGE_UTIL_COLORSPACE_NV61 */
52 static int _convert_encode_colorspace_tbl[] = {
53 MM_UTIL_JPEG_FMT_YUV420, /* IMAGE_UTIL_COLORSPACE_YUV420 */
54 -1, /* IMAGE_UTIL_COLORSPACE_YUV422 */
55 MM_UTIL_JPEG_FMT_YUV420, /* IMAGE_UTIL_COLORSPACE_I420 */
56 MM_UTIL_JPEG_FMT_NV12, /* IMAGE_UTIL_COLORSPACE_NV12 */
57 -1, /* IMAGE_UTIL_COLORSPACE_UYVY */
58 -1, /* IMAGE_UTIL_COLORSPACE_YUYV */
59 -1, /* IMAGE_UTIL_COLORSPACE_RGB565 */
60 MM_UTIL_JPEG_FMT_RGB888, /* IMAGE_UTIL_COLORSPACE_RGB888 */
61 MM_UTIL_JPEG_FMT_ARGB8888, /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
62 MM_UTIL_JPEG_FMT_BGRA8888, /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
63 MM_UTIL_JPEG_FMT_RGBA8888, /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
64 -1, /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
65 -1, /* IMAGE_UTIL_COLORSPACE_NV21 */
66 -1, /* IMAGE_UTIL_COLORSPACE_NV16 */
67 -1, /* IMAGE_UTIL_COLORSPACE_NV61 */
70 static int _convert_jpeg_colorspace_tbl[] = {
71 MM_UTIL_JPEG_FMT_YUV420, /* IMAGE_UTIL_COLORSPACE_YUV420 */
72 -1, /* IMAGE_UTIL_COLORSPACE_YUV422 */
73 MM_UTIL_JPEG_FMT_YUV420, /* IMAGE_UTIL_COLORSPACE_I420 */
74 MM_UTIL_JPEG_FMT_NV12, /* IMAGE_UTIL_COLORSPACE_NV12 */
75 -1, /* IMAGE_UTIL_COLORSPACE_UYVY */
76 -1, /* IMAGE_UTIL_COLORSPACE_YUYV */
77 -1, /* IMAGE_UTIL_COLORSPACE_RGB565 */
78 MM_UTIL_JPEG_FMT_RGB888, /* IMAGE_UTIL_COLORSPACE_RGB888 */
79 MM_UTIL_JPEG_FMT_ARGB8888, /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
80 MM_UTIL_JPEG_FMT_BGRA8888, /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
81 MM_UTIL_JPEG_FMT_RGBA8888, /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
82 -1, /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
83 -1, /* IMAGE_UTIL_COLORSPACE_NV21 */
84 -1, /* IMAGE_UTIL_COLORSPACE_NV16 */
85 -1, /* IMAGE_UTIL_COLORSPACE_NV61 */
88 static int _convert_png_colorspace_tbl[] = {
89 -1, /* IMAGE_UTIL_COLORSPACE_YUV420 */
90 -1, /* IMAGE_UTIL_COLORSPACE_YUV422 */
91 -1, /* IMAGE_UTIL_COLORSPACE_I420 */
92 -1, /* IMAGE_UTIL_COLORSPACE_NV12 */
93 -1, /* IMAGE_UTIL_COLORSPACE_UYVY */
94 -1, /* IMAGE_UTIL_COLORSPACE_YUYV */
95 -1, /* IMAGE_UTIL_COLORSPACE_RGB565 */
96 -1, /* IMAGE_UTIL_COLORSPACE_RGB888 */
97 -1, /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
98 -1, /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
99 MM_UTIL_PNG_COLOR_TYPE_RGB_ALPHA, /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
100 -1, /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
101 -1, /* IMAGE_UTIL_COLORSPACE_NV21 */
102 -1, /* IMAGE_UTIL_COLORSPACE_NV16 */
103 -1, /* IMAGE_UTIL_COLORSPACE_NV61 */
106 static int _convert_gif_colorspace_tbl[] = {
107 -1, /* IMAGE_UTIL_COLORSPACE_YUV420 */
108 -1, /* IMAGE_UTIL_COLORSPACE_YUV422 */
109 -1, /* IMAGE_UTIL_COLORSPACE_I420 */
110 -1, /* IMAGE_UTIL_COLORSPACE_NV12 */
111 -1, /* IMAGE_UTIL_COLORSPACE_UYVY */
112 -1, /* IMAGE_UTIL_COLORSPACE_YUYV */
113 -1, /* IMAGE_UTIL_COLORSPACE_RGB565 */
114 -1, /* IMAGE_UTIL_COLORSPACE_RGB888 */
115 -1, /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
116 -1, /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
117 MM_UTIL_GIF_FMT_RGBA8888, /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
118 -1, /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
119 -1, /* IMAGE_UTIL_COLORSPACE_NV21 */
120 -1, /* IMAGE_UTIL_COLORSPACE_NV16 */
121 -1, /* IMAGE_UTIL_COLORSPACE_NV61 */
124 static int _convert_bmp_colorspace_tbl[] = {
125 -1, /* IMAGE_UTIL_COLORSPACE_YUV420 */
126 -1, /* IMAGE_UTIL_COLORSPACE_YUV422 */
127 -1, /* IMAGE_UTIL_COLORSPACE_I420 */
128 -1, /* IMAGE_UTIL_COLORSPACE_NV12 */
129 -1, /* IMAGE_UTIL_COLORSPACE_UYVY */
130 -1, /* IMAGE_UTIL_COLORSPACE_YUYV */
131 -1, /* IMAGE_UTIL_COLORSPACE_RGB565 */
132 -1, /* IMAGE_UTIL_COLORSPACE_RGB888 */
133 -1, /* IMAGE_UTIL_COLORSPACE_ARGB8888 */
134 -1, /* IMAGE_UTIL_COLORSPACE_BGRA8888 */
135 MM_UTIL_BMP_FMT_RGBA8888, /* IMAGE_UTIL_COLORSPACE_RGBA8888 */
136 -1, /* IMAGE_UTIL_COLORSPACE_BGRX8888 */
137 -1, /* IMAGE_UTIL_COLORSPACE_NV21 */
138 -1, /* IMAGE_UTIL_COLORSPACE_NV16 */
139 -1, /* IMAGE_UTIL_COLORSPACE_NV61 */
142 static int _convert_decode_scale_tbl[] = {
143 MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1,
144 MM_UTIL_JPEG_DECODE_DOWNSCALE_1_2,
145 MM_UTIL_JPEG_DECODE_DOWNSCALE_1_4,
146 MM_UTIL_JPEG_DECODE_DOWNSCALE_1_8,
149 static int _convert_image_util_error_code(const char *func, int code)
151 int ret = IMAGE_UTIL_ERROR_INVALID_OPERATION;
152 char *errorstr = NULL;
154 case MM_UTIL_ERROR_NONE:
155 ret = IMAGE_UTIL_ERROR_NONE;
156 errorstr = strdup("ERROR_NONE");
158 case MM_UTIL_ERROR_NO_SUCH_FILE:
159 ret = IMAGE_UTIL_ERROR_NO_SUCH_FILE;
160 errorstr = strdup("NO_SUCH_FILE");
162 case MM_UTIL_ERROR_INVALID_PARAMETER:
163 ret = IMAGE_UTIL_ERROR_INVALID_PARAMETER;
164 errorstr = strdup("INVALID_PARAMETER");
166 case MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT:
167 ret = IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
168 errorstr = strdup("NOT_SUPPORTED_FORMAT");
170 case MM_UTIL_ERROR_OUT_OF_MEMORY:
171 ret = IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
172 errorstr = strdup("OUT_OF_MEMORY");
174 case MM_UTIL_ERROR_INVALID_OPERATION:
176 ret = IMAGE_UTIL_ERROR_INVALID_OPERATION;
177 errorstr = strdup("INVALID_OPERATION");
181 image_util_debug("[%s] %s(0x%08x)", func, errorstr, ret);
182 IMAGE_UTIL_SAFE_FREE(errorstr);
186 static image_util_error_e _image_util_error_convert(int error)
189 case MM_UTIL_ERROR_NONE:
190 image_util_debug("Error None");
191 return IMAGE_UTIL_ERROR_NONE;
192 case MM_UTIL_ERROR_INVALID_PARAMETER:
193 image_util_error("INVALID_PARAMETER(0x%08x)", IMAGE_UTIL_ERROR_INVALID_PARAMETER);
194 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
195 case MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT:
196 image_util_error("NOT_SUPPORTED_FORMAT(0x%08x)", IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT);
197 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
198 case MM_UTIL_ERROR_OUT_OF_MEMORY:
199 image_util_error("OUT_OF_MEMORY(0x%08x)", IMAGE_UTIL_ERROR_OUT_OF_MEMORY);
200 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
202 case MM_UTIL_ERROR_INVALID_OPERATION:
204 image_util_error("INVALID_OPERATION(0x%08x)", error);
205 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
209 static void _image_util_transform_completed_cb(media_packet_h * dst, int error, void *user_data)
211 int error_value = IMAGE_UTIL_ERROR_NONE;
212 image_util_cb_s *_util_cb = (image_util_cb_s *) user_data;
214 if ((_util_cb != NULL) && (_util_cb->image_processing_completed_cb != NULL)) {
215 error_value = _image_util_error_convert(error);
216 _util_cb->image_processing_completed_cb(dst, error_value, _util_cb->user_data);
222 static int _image_util_create_transform_handle(transformation_s * handle)
224 int err = MM_UTIL_ERROR_NONE;
225 mm_util_imgp_h image_h;
227 err = mm_util_create(&image_h);
228 if (err != MM_UTIL_ERROR_NONE) {
229 image_util_error("Error - mm_util_create");
230 return _convert_image_util_error_code(__func__, err);
233 handle->image_h = image_h;
235 return _convert_image_util_error_code(__func__, err);
238 static bool _image_util_check_resolution(int width, int height)
241 image_util_error("invalid width [%d]", width);
246 image_util_error("invalid height [%d]", height);
253 int image_util_foreach_supported_jpeg_colorspace(image_util_supported_jpeg_colorspace_cb callback, void *user_data)
257 DEPRECATION_LOGW("image_util_foreach_supported_jpeg_colorspace()", "image_util_foreach_supported_colorspace()");
259 image_util_retvm_if((callback == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "callback is null");
261 /* RGB has higher precedence than YUV */
262 for (i = sizeof(_convert_encode_colorspace_tbl) / sizeof(int) - 1; i >= 0; i--) {
263 if (_convert_encode_colorspace_tbl[i] != -1)
264 if (false == callback(i, user_data))
265 return IMAGE_UTIL_ERROR_NONE;
269 return IMAGE_UTIL_ERROR_NONE;
272 int image_util_transform_create(transformation_h * handle)
274 int err = MM_UTIL_ERROR_NONE;
276 image_util_debug("image_util_create");
278 image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
280 transformation_s *_handle = (transformation_s *) calloc(1, sizeof(transformation_s));
281 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", IMAGE_UTIL_ERROR_OUT_OF_MEMORY);
283 _handle->colorspace = -1;
284 _handle->_util_cb = NULL;
285 _handle->image_h = 0;
286 _handle->hardware_acceleration = false;
287 _handle->set_convert = false;
288 _handle->set_resize = false;
289 _handle->set_rotate = false;
290 _handle->set_crop = false;
292 err = _image_util_create_transform_handle(_handle);
293 if (err != MM_UTIL_ERROR_NONE) {
294 image_util_error("Error - create transform handle");
295 IMAGE_UTIL_SAFE_FREE(_handle);
296 return _convert_image_util_error_code(__func__, err);
299 *handle = (transformation_h) _handle;
301 return _convert_image_util_error_code(__func__, err);
304 int image_util_transform_set_hardware_acceleration(transformation_h handle, bool mode)
306 int err = MM_UTIL_ERROR_NONE;
307 transformation_s *_handle = (transformation_s *) handle;
309 image_util_debug("Set hardware_acceleration %d", mode);
311 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
312 #ifndef ENABLE_HW_ACCELERATION
313 image_util_retvm_if((mode == true), IMAGE_UTIL_ERROR_NOT_SUPPORTED, "hardware acceleration is not supported");
316 err = mm_util_set_hardware_acceleration(_handle->image_h, mode);
317 if (err != MM_UTIL_ERROR_NONE) {
318 image_util_error("Error - Set hardware_acceleration");
319 return _convert_image_util_error_code(__func__, err);
322 image_util_debug("Set hardware_acceleration %d", mode);
323 _handle->hardware_acceleration = mode;
325 return _convert_image_util_error_code(__func__, err);
328 int image_util_transform_set_colorspace(transformation_h handle, image_util_colorspace_e colorspace)
330 int err = MM_UTIL_ERROR_NONE;
331 transformation_s *_handle = (transformation_s *) handle;
333 image_util_debug("Set colorspace_convert_info [%d]", colorspace);
335 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
337 err = mm_util_set_colorspace_convert(_handle->image_h, colorspace);
338 if (err != MM_UTIL_ERROR_NONE) {
339 image_util_error("Error - Set colorspace convert");
340 return _convert_image_util_error_code(__func__, err);
343 _handle->colorspace = colorspace;
344 _handle->set_convert = true;
346 return _convert_image_util_error_code(__func__, err);
349 int image_util_transform_set_resolution(transformation_h handle, unsigned int width, unsigned int height)
351 int err = MM_UTIL_ERROR_NONE;
352 transformation_s *_handle = (transformation_s *) handle;
354 image_util_debug("Set resize_info w[%d] h[%d]", width, height);
356 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
357 image_util_retvm_if((_handle->set_crop), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Crop and Resize can't do at the same time");
358 image_util_retvm_if((_image_util_check_resolution(width, height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
360 err = mm_util_set_resolution(_handle->image_h, width, height);
361 if (err != MM_UTIL_ERROR_NONE) {
362 image_util_error("Error - Set resolution");
363 return _convert_image_util_error_code(__func__, err);
365 _handle->width = width;
366 _handle->height = height;
367 _handle->set_resize = true;
369 return _convert_image_util_error_code(__func__, err);
372 int image_util_transform_set_rotation(transformation_h handle, image_util_rotation_e rotation)
374 int err = MM_UTIL_ERROR_NONE;
375 transformation_s *_handle = (transformation_s *) handle;
377 image_util_debug("Set rotate_info [%d]", rotation);
379 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
381 err = mm_util_set_rotation(_handle->image_h, rotation);
382 if (err != MM_UTIL_ERROR_NONE) {
383 image_util_error("Error - Set rotation");
384 return _convert_image_util_error_code(__func__, err);
386 _handle->rotation = rotation;
387 _handle->set_rotate = true;
389 return _convert_image_util_error_code(__func__, err);
392 int image_util_transform_set_crop_area(transformation_h handle, unsigned int start_x, unsigned int start_y, unsigned int end_x, unsigned int end_y)
394 int err = MM_UTIL_ERROR_NONE;
395 transformation_s *_handle = (transformation_s *) handle;
399 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
400 image_util_retvm_if((_handle->set_resize), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Crop and Resize can't do at the same time");
402 dest_width = end_x - start_x;
403 dest_height = end_y - start_y;
405 image_util_debug("Set crop_info x[%d] y[%d] w[%d] h[%d]", start_x, start_y, dest_width, dest_height);
407 image_util_retvm_if((_image_util_check_resolution(dest_width, dest_height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dest resolution");
408 err = mm_util_set_crop_area(_handle->image_h, start_x, start_y, end_x, end_y);
409 if (err != MM_UTIL_ERROR_NONE) {
410 image_util_error("Error - Set crop area");
411 return _convert_image_util_error_code(__func__, err);
413 _handle->start_x = start_x;
414 _handle->start_y = start_y;
415 _handle->end_x = end_x;
416 _handle->end_y = end_y;
417 _handle->set_crop = true;
419 return _convert_image_util_error_code(__func__, err);
422 int image_util_transform_get_colorspace(transformation_h handle, image_util_colorspace_e * colorspace)
424 int ret = IMAGE_UTIL_ERROR_NONE;
425 transformation_s *_handle = (transformation_s *) handle;
427 if (_handle == NULL) {
428 image_util_error("Invalid Handle");
429 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
433 image_util_error("colorspace area parameter error");
434 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
437 if (!_handle->set_convert) {
438 image_util_error("Did not set colorspace before");
439 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
442 image_util_debug("Get colorspace_convert_info [%d]", _handle->colorspace);
444 *colorspace = _handle->colorspace;
449 int image_util_transform_get_resolution(transformation_h handle, unsigned int *width, unsigned int *height)
451 int ret = IMAGE_UTIL_ERROR_NONE;
452 transformation_s *_handle = (transformation_s *) handle;
454 if (_handle == NULL) {
455 image_util_error("Invalid Handle");
456 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
459 if (!width || !height) {
460 image_util_error("resolution area parameter error");
461 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
464 if (!_handle->set_resize) {
465 image_util_error("Did not set resolution before");
466 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
469 image_util_debug("Get resize_info w[%ui] h[%ui]", _handle->width, _handle->height);
471 *width = _handle->width;
472 *height = _handle->height;
477 int image_util_transform_get_rotation(transformation_h handle, image_util_rotation_e * rotation)
479 int ret = IMAGE_UTIL_ERROR_NONE;
480 transformation_s *_handle = (transformation_s *) handle;
482 if (_handle == NULL) {
483 image_util_error("Invalid Handle");
484 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
488 image_util_error("rotation area parameter error");
489 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
492 if (!_handle->set_rotate) {
493 image_util_error("Did not set rotation before");
494 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
497 image_util_debug("Get rotate_info [%d]", _handle->rotation);
499 *rotation = _handle->rotation;
504 int image_util_transform_get_crop_area(transformation_h handle, unsigned int *start_x, unsigned int *start_y, unsigned int *end_x, unsigned int *end_y)
506 int ret = IMAGE_UTIL_ERROR_NONE;
507 transformation_s *_handle = (transformation_s *) handle;
509 if (_handle == NULL) {
510 image_util_error("Invalid Handle");
511 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
514 if (!start_x || !start_y || !end_x || !end_y) {
515 image_util_error("crop area parameter error");
516 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
519 if (!_handle->set_crop) {
520 image_util_error("Did not set crop area before");
521 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
524 *start_x = _handle->start_x;
525 *start_y = _handle->start_y;
526 *end_x = _handle->end_x;
527 *end_y = _handle->end_y;
532 int image_util_transform_run(transformation_h handle, media_packet_h src, image_util_transform_completed_cb completed_cb, void *user_data)
534 int err = MM_UTIL_ERROR_NONE;
535 transformation_s *_handle = (transformation_s *) handle;
537 image_util_debug("image_util_transform");
539 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
540 image_util_retvm_if((completed_cb == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid callback");
541 image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid source");
542 image_util_retvm_if((!_handle->set_convert && !_handle->set_resize && !_handle->set_rotate && !_handle->set_crop), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid transform");
544 if (_handle->_util_cb != NULL) {
545 IMAGE_UTIL_SAFE_FREE(_handle->_util_cb);
546 _handle->_util_cb = NULL;
548 _handle->_util_cb = (image_util_cb_s *) calloc(1, sizeof(image_util_cb_s));
549 image_util_retvm_if((_handle->_util_cb == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Out of memory");
551 _handle->_util_cb->user_data = user_data;
552 _handle->_util_cb->image_processing_completed_cb = completed_cb;
554 if (_handle->_util_cb)
555 err = mm_util_transform(_handle->image_h, src, (mm_util_completed_callback) _image_util_transform_completed_cb, (void *)_handle->_util_cb);
557 return _convert_image_util_error_code(__func__, err);
560 int image_util_transform_destroy(transformation_h handle)
562 int err = MM_UTIL_ERROR_NONE;
563 transformation_s *_handle = (transformation_s *) handle;
565 image_util_debug("image_util_destroy");
567 if (_handle == NULL) {
568 image_util_error("Invalid Handle");
569 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
572 err = mm_util_destroy(_handle->image_h);
574 IMAGE_UTIL_SAFE_FREE(_handle->_util_cb);
575 IMAGE_UTIL_SAFE_FREE(_handle);
577 return _convert_image_util_error_code(__func__, err);
580 int image_util_convert_colorspace(unsigned char *dest, image_util_colorspace_e dest_colorspace, const unsigned char *src, int width, int height, image_util_colorspace_e src_colorspace)
582 int err = MM_UTIL_ERROR_NONE;
584 image_util_retvm_if((dest == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest is null");
585 image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "src is null");
586 image_util_retvm_if((dest_colorspace < 0 || dest_colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dst_colorspace");
587 image_util_retvm_if((src_colorspace < 0 || src_colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid src_colorspace");
589 err = mm_util_convert_colorspace(src, width, height, _convert_colorspace_tbl[src_colorspace], dest, _convert_colorspace_tbl[dest_colorspace]);
591 return _convert_image_util_error_code(__func__, err);
594 int image_util_calculate_buffer_size(int width, int height, image_util_colorspace_e colorspace, unsigned int *size)
596 int err = MM_UTIL_ERROR_NONE;
598 image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
599 image_util_retvm_if((width <= 0 || height <= 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid width or Invalid height");
600 image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null");
602 err = mm_util_get_image_size(_convert_colorspace_tbl[colorspace], width, height, size);
603 return _convert_image_util_error_code(__func__, err);
606 int image_util_resize(unsigned char *dest, int *dest_width, int *dest_height, const unsigned char *src, int src_width, int src_height, image_util_colorspace_e colorspace)
608 int err = MM_UTIL_ERROR_NONE;
610 image_util_retvm_if((dest == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest is null");
611 image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "src is null");
612 image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
613 image_util_retvm_if((dest_width == NULL || dest_height == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest_width or dest_height is null");
614 image_util_retvm_if((*dest_width <= 0 || *dest_height <= 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid dest_width or Invalid dest_height");
616 unsigned int dest_w, dest_h;
617 dest_w = *dest_width;
618 dest_h = *dest_height;
619 err = mm_util_resize_image(src, src_width, src_height, _convert_colorspace_tbl[colorspace], dest, &dest_w, &dest_h);
620 if (err == MM_UTIL_ERROR_NONE) {
621 *dest_width = (int)dest_w;
622 *dest_height = (int)dest_h;
625 return _convert_image_util_error_code(__func__, err);
628 int image_util_rotate(unsigned char *dest, int *dest_width, int *dest_height, image_util_rotation_e dest_rotation, const unsigned char *src, int src_width, int src_height, image_util_colorspace_e colorspace)
630 int err = MM_UTIL_ERROR_NONE;
632 image_util_retvm_if((dest == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest is null");
633 image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "src is null");
634 image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
635 image_util_retvm_if((dest_rotation < 0 || dest_rotation > IMAGE_UTIL_ROTATION_FLIP_VERT), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid rotation");
636 image_util_retvm_if((dest_width == NULL || dest_height == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest_width or dest_height is null");
638 unsigned int dest_w, dest_h;
639 err = mm_util_rotate_image(src, src_width, src_height, _convert_colorspace_tbl[colorspace], dest, &dest_w, &dest_h, dest_rotation);
640 if (err == MM_UTIL_ERROR_NONE) {
641 *dest_width = (int)dest_w;
642 *dest_height = (int)dest_h;
644 return _convert_image_util_error_code(__func__, err);
647 int image_util_crop(unsigned char *dest, int x, int y, int *width, int *height, const unsigned char *src, int src_width, int src_height, image_util_colorspace_e colorspace)
649 int err = MM_UTIL_ERROR_NONE;
651 image_util_retvm_if((dest == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "dest is null");
652 image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "src is null");
653 image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
654 image_util_retvm_if((width == NULL || height == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "width or height is null");
655 image_util_retvm_if((src_width <= x || src_height <= y || src_width < x + *width || src_height < y + *height), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid crop area");
657 unsigned int dest_w, dest_h;
660 err = mm_util_crop_image(src, src_width, src_height, _convert_colorspace_tbl[colorspace], x, y, &dest_w, &dest_h, dest);
661 if (err == MM_UTIL_ERROR_NONE) {
662 *width = (int)dest_w;
663 *height = (int)dest_h;
666 return _convert_image_util_error_code(__func__, err);
669 int image_util_decode_jpeg(const char *path, image_util_colorspace_e colorspace, unsigned char **image_buffer, int *width, int *height, unsigned int *size)
671 int err = MM_UTIL_ERROR_NONE;
673 DEPRECATION_LOGW("image_util_decode_jpeg()", "image_util_decode_create()");
675 image_util_retvm_if((path == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "path is null");
676 image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
677 image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null");
678 image_util_retvm_if((strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
679 image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
680 image_util_retvm_if((_convert_encode_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
682 mm_util_jpeg_yuv_data decoded;
683 memset(&decoded, 0, sizeof(mm_util_jpeg_yuv_data));
685 err = mm_util_decode_from_jpeg_file(&decoded, path, _convert_encode_colorspace_tbl[colorspace]);
686 if (err == MM_UTIL_ERROR_NONE) {
687 *image_buffer = decoded.data;
689 *width = decoded.width;
691 *height = decoded.height;
693 *size = (unsigned int)decoded.size;
695 return _convert_image_util_error_code(__func__, err);
698 int image_util_decode_jpeg_from_memory(const unsigned char *jpeg_buffer, int jpeg_size, image_util_colorspace_e colorspace, unsigned char **image_buffer, int *width, int *height, unsigned int *size)
700 int err = MM_UTIL_ERROR_NONE;
702 DEPRECATION_LOGW("image_util_decode_jpeg_from_memory()", "image_util_decode_create()");
704 image_util_retvm_if((jpeg_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "jpeg_buffer is null");
705 image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
706 image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null");
707 image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
708 image_util_retvm_if((_convert_encode_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
710 mm_util_jpeg_yuv_data decoded;
711 memset(&decoded, 0, sizeof(mm_util_jpeg_yuv_data));
713 err = mm_util_decode_from_jpeg_memory(&decoded, (void *)jpeg_buffer, jpeg_size, _convert_encode_colorspace_tbl[colorspace]);
715 if (err == MM_UTIL_ERROR_NONE) {
716 *image_buffer = decoded.data;
718 *width = decoded.width;
720 *height = decoded.height;
722 *size = (unsigned int)decoded.size;
725 return _convert_image_util_error_code(__func__, err);
728 int image_util_decode_jpeg_with_downscale(const char *path, image_util_colorspace_e colorspace, image_util_scale_e downscale, unsigned char **image_buffer, int *width, int *height, unsigned int *size)
730 int err = MM_UTIL_ERROR_NONE;
732 DEPRECATION_LOGW("image_util_decode_jpeg_with_downscale()", "image_util_decode_create()");
734 image_util_retvm_if((path == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "path is null");
735 image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
736 image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null");
737 image_util_retvm_if((strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
738 image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
739 image_util_retvm_if((_convert_encode_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
740 image_util_retvm_if((downscale < 0 || downscale >= sizeof(_convert_decode_scale_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "downsacle is invalid");
742 mm_util_jpeg_yuv_data decoded;
743 memset(&decoded, 0, sizeof(mm_util_jpeg_yuv_data));
745 err = mm_util_decode_from_jpeg_file_with_downscale(&decoded, path, _convert_encode_colorspace_tbl[colorspace], _convert_decode_scale_tbl[downscale]);
746 if (err == MM_UTIL_ERROR_NONE) {
747 *image_buffer = decoded.data;
749 *width = decoded.width;
751 *height = decoded.height;
753 *size = (unsigned int)decoded.size;
755 return _convert_image_util_error_code(__func__, err);
758 int image_util_decode_jpeg_from_memory_with_downscale(const unsigned char *jpeg_buffer, int jpeg_size, image_util_colorspace_e colorspace, image_util_scale_e downscale, unsigned char **image_buffer, int *width, int *height, unsigned int *size)
760 int err = MM_UTIL_ERROR_NONE;
762 DEPRECATION_LOGW("image_util_decode_jpeg_from_memory_with_downscale()", "image_util_decode_create()");
764 image_util_retvm_if((jpeg_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "jpeg_buffer is null");
765 image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
766 image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null");
767 image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
768 image_util_retvm_if((_convert_encode_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
769 image_util_retvm_if((downscale < 0 || downscale >= sizeof(_convert_decode_scale_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "downsacle is invalid");
771 mm_util_jpeg_yuv_data decoded;
772 memset(&decoded, 0, sizeof(mm_util_jpeg_yuv_data));
774 err = mm_util_decode_from_jpeg_memory_with_downscale(&decoded, (void *)jpeg_buffer, jpeg_size, _convert_encode_colorspace_tbl[colorspace], _convert_decode_scale_tbl[downscale]);
776 if (err == MM_UTIL_ERROR_NONE) {
777 *image_buffer = decoded.data;
779 *width = decoded.width;
781 *height = decoded.height;
783 *size = (unsigned int)decoded.size;
786 return _convert_image_util_error_code(__func__, err);
789 int image_util_encode_jpeg(const unsigned char *buffer, int width, int height, image_util_colorspace_e colorspace, int quality, const char *path)
791 int err = MM_UTIL_ERROR_NONE;
793 DEPRECATION_LOGW("image_util_encode_jpeg()", "image_util_encode_create()");
795 image_util_retvm_if((path == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "path is null");
796 image_util_retvm_if((buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "buffer is null");
797 image_util_retvm_if((strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
798 image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
799 image_util_retvm_if((_convert_encode_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
801 err = mm_util_jpeg_encode_to_file(path, (void *)buffer, width, height, _convert_encode_colorspace_tbl[colorspace], quality);
802 return _convert_image_util_error_code(__func__, err);
805 int image_util_encode_jpeg_to_memory(const unsigned char *image_buffer, int width, int height, image_util_colorspace_e colorspace, int quality, unsigned char **jpeg_buffer, unsigned int *jpeg_size)
807 int err = MM_UTIL_ERROR_NONE;
810 DEPRECATION_LOGW("image_util_encode_jpeg_to_memory()", "image_util_encode_create()");
812 image_util_retvm_if((jpeg_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "jpeg_buffer is null");
813 image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
814 image_util_retvm_if((jpeg_size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "jpeg_size is null");
815 image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
816 image_util_retvm_if((_convert_encode_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
818 err = mm_util_jpeg_encode_to_memory((void **)jpeg_buffer, &isize, (void *)image_buffer, width, height, _convert_encode_colorspace_tbl[colorspace], quality);
819 if (err == MM_UTIL_ERROR_NONE)
820 *jpeg_size = (unsigned int)isize;
822 return _convert_image_util_error_code(__func__, err);
825 int image_util_extract_color_from_memory(const unsigned char *image_buffer, int width, int height, unsigned char *rgb_r, unsigned char *rgb_g, unsigned char *rgb_b)
827 int ret = MM_UTIL_ERROR_NONE;
829 image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
831 GModule *module = NULL;
832 ModuleFunc mmutil_imgcv_module_func = NULL;
833 module = g_module_open(PATH_MMUTIL_IMGCV_LIB, G_MODULE_BIND_LAZY);
834 image_util_retvm_if((module == NULL), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "fail to open module");
836 g_module_symbol(module, IMGCV_FUNC_NAME, (gpointer *)&mmutil_imgcv_module_func);
837 if (!mmutil_imgcv_module_func)
838 g_module_close(module);
840 image_util_retvm_if((mmutil_imgcv_module_func == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "fail to get symbol");
842 unsigned char r_color, g_color, b_color;
843 ret = mmutil_imgcv_module_func((void *)image_buffer, width, height, &r_color, &g_color, &b_color);
850 g_module_close(module);
854 return _convert_image_util_error_code(__func__, ret);
857 int image_util_foreach_supported_colorspace(image_util_type_e image_type, image_util_supported_colorspace_cb callback, void *user_data)
861 image_util_retvm_if((callback == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "callback is null");
863 switch (image_type) {
864 case IMAGE_UTIL_JPEG:
865 /* RGB has higher precedence than YUV */
866 for (i = sizeof(_convert_jpeg_colorspace_tbl) / sizeof(int) - 1; i >= 0; i--) {
867 if (_convert_jpeg_colorspace_tbl[i] != -1)
868 if (false == callback(i, user_data))
869 return IMAGE_UTIL_ERROR_NONE;
874 for (i = sizeof(_convert_png_colorspace_tbl) / sizeof(int) - 1; i >= 0; i--) {
875 if (_convert_png_colorspace_tbl[i] != -1)
876 if (false == callback(i, user_data))
877 return IMAGE_UTIL_ERROR_NONE;
882 for (i = sizeof(_convert_gif_colorspace_tbl) / sizeof(int) - 1; i >= 0; i--) {
883 if (_convert_gif_colorspace_tbl[i] != -1)
884 if (false == callback(i, user_data))
885 return IMAGE_UTIL_ERROR_NONE;
890 for (i = sizeof(_convert_bmp_colorspace_tbl) / sizeof(int) - 1; i >= 0; i--) {
891 if (_convert_bmp_colorspace_tbl[i] != -1)
892 if (false == callback(i, user_data))
893 return IMAGE_UTIL_ERROR_NONE;
898 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
901 return IMAGE_UTIL_ERROR_NONE;
904 static void _image_util_decode_destroy_image_handle(decode_encode_s * handle)
906 image_util_retm_if((handle == NULL), "Invalid Handle");
907 void *image_handle = (void *)(handle->image_h);
908 image_util_retm_if((image_handle == NULL), "Invalid image handle");
910 IMAGE_UTIL_SAFE_FREE(image_handle);
913 static int _image_util_decode_create_jpeg_handle(decode_encode_s * handle)
915 int err = MM_UTIL_ERROR_NONE;
917 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
919 /* It is needed to reuse decode handle for C#, so it shoud be reallocated */
920 _image_util_decode_destroy_image_handle(handle);
922 mm_util_jpeg_yuv_data *_handle = (mm_util_jpeg_yuv_data *) calloc(1, sizeof(mm_util_jpeg_yuv_data));
923 image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY);
925 handle->image_h = (mm_util_imgp_h) _handle;
926 handle->colorspace = IMAGE_UTIL_COLORSPACE_RGBA8888;
927 handle->down_scale = sizeof(image_util_scale_e);
932 static int _image_util_decode_create_png_handle(decode_encode_s * handle)
934 int err = MM_UTIL_ERROR_NONE;
936 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
938 /* It is needed to reuse decode handle for C#, so it shoud be reallocated */
939 _image_util_decode_destroy_image_handle(handle);
941 mm_util_png_data *_handle = (mm_util_png_data *) calloc(1, sizeof(mm_util_png_data));
942 image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY);
944 mm_util_init_decode_png(_handle);
946 handle->image_h = (mm_util_imgp_h) _handle;
951 static int _image_util_decode_create_gif_handle(decode_encode_s * handle)
953 int err = MM_UTIL_ERROR_NONE;
955 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
957 /* It is needed to reuse decode handle for C#, so it shoud be reallocated */
958 _image_util_decode_destroy_image_handle(handle);
960 mm_util_gif_data *_handle = (mm_util_gif_data *) calloc(1, sizeof(mm_util_gif_data));
961 image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY);
963 handle->image_h = (mm_util_imgp_h) _handle;
968 static int _image_util_decode_create_bmp_handle(decode_encode_s * handle)
970 int err = MM_UTIL_ERROR_NONE;
972 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
974 /* It is needed to reuse decode handle for C#, so it shoud be reallocated */
975 _image_util_decode_destroy_image_handle(handle);
977 mm_util_bmp_data *_handle = (mm_util_bmp_data *) calloc(1, sizeof(mm_util_bmp_data));
978 image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY);
980 handle->image_h = (mm_util_imgp_h) _handle;
985 int image_util_decode_create(image_util_decode_h * handle)
987 int err = MM_UTIL_ERROR_NONE;
989 image_util_debug("image_util_decode_create");
991 image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
993 decode_encode_s *_handle = (decode_encode_s *) calloc(1, sizeof(decode_encode_s));
994 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", IMAGE_UTIL_ERROR_OUT_OF_MEMORY);
996 _handle->src_buffer = NULL;
997 _handle->dst_buffer = NULL;
998 _handle->path = NULL;
999 _handle->image_h = 0;
1000 _handle->is_decode = TRUE;
1001 _handle->image_type = -1;
1002 _handle->image_count = 1;
1004 *handle = (image_util_decode_h) _handle;
1006 return _convert_image_util_error_code(__func__, err);
1009 static char _JPEG_HEADER[] = { 0xFF, 0xD8 };
1010 static char _PNG_HEADER[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
1011 static char _GIF_HEADER[] = { 'G', 'I', 'F' };
1012 static char _BMP_HEADER[] = { 'B', 'M' };
1014 static int _image_util_decode_create_image_handle(image_util_decode_h handle, const unsigned char *src_buffer)
1016 image_util_type_e image_type = -1;
1020 image_util_type_e image_type;
1021 } image_header[] = {
1023 _JPEG_HEADER, sizeof(_JPEG_HEADER), IMAGE_UTIL_JPEG}
1025 _PNG_HEADER, sizeof(_PNG_HEADER), IMAGE_UTIL_PNG}
1027 _GIF_HEADER, sizeof(_GIF_HEADER), IMAGE_UTIL_GIF}
1029 _BMP_HEADER, sizeof(_BMP_HEADER), IMAGE_UTIL_BMP}
1032 int err = MM_UTIL_ERROR_NONE;
1033 decode_encode_s *_handle = (decode_encode_s *) handle;
1035 if (_handle == NULL || _handle->is_decode == FALSE) {
1036 image_util_error("Invalid Handle");
1037 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1039 if (src_buffer == NULL) {
1040 image_util_error("Invalid input buffer");
1041 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1044 for (i = 0; i < sizeof(image_header) / sizeof(image_header[0]); i++) {
1045 if (strncmp((const char *)src_buffer, image_header[i].header, image_header[i].size) == 0) {
1046 image_type = image_header[i].image_type;
1051 _handle->image_type = image_type;
1053 switch (image_type) {
1054 case IMAGE_UTIL_JPEG:
1055 err = _image_util_decode_create_jpeg_handle(_handle);
1057 case IMAGE_UTIL_PNG:
1058 err = _image_util_decode_create_png_handle(_handle);
1060 case IMAGE_UTIL_GIF:
1061 err = _image_util_decode_create_gif_handle(_handle);
1063 case IMAGE_UTIL_BMP:
1064 err = _image_util_decode_create_bmp_handle(_handle);
1067 err = MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
1071 if (err != MM_UTIL_ERROR_NONE) {
1072 image_util_error("Error - create image handle");
1073 return _convert_image_util_error_code(__func__, err);
1076 return _convert_image_util_error_code(__func__, err);
1079 int image_util_decode_set_input_path(image_util_decode_h handle, const char *path)
1081 int err = IMAGE_UTIL_ERROR_NONE;
1082 decode_encode_s *_handle = (decode_encode_s *) handle;
1084 unsigned char *src_buffer = NULL;
1086 if (_handle == NULL || _handle->is_decode == FALSE) {
1087 image_util_error("Invalid Handle");
1088 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1090 image_util_retvm_if((path == NULL || strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
1092 if (_handle->src_buffer)
1093 _handle->src_buffer = NULL;
1095 fp = fopen(path, "r");
1097 image_util_error("File open failed %s", path);
1098 return IMAGE_UTIL_ERROR_NO_SUCH_FILE;
1100 src_buffer = (void *)malloc(sizeof(_PNG_HEADER));
1101 if (!fread(src_buffer, 1, sizeof(_PNG_HEADER), fp)) {
1102 image_util_error("File read failed");
1106 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
1109 err = _image_util_decode_create_image_handle(_handle, src_buffer);
1110 if (err != IMAGE_UTIL_ERROR_NONE) {
1111 image_util_error("_image_util_decode_create_image_handle failed");
1122 _handle->path = g_strndup(path, strlen(path));
1127 int image_util_decode_set_input_buffer(image_util_decode_h handle, const unsigned char *src_buffer, unsigned long long src_size)
1129 int err = IMAGE_UTIL_ERROR_NONE;
1130 decode_encode_s *_handle = (decode_encode_s *) handle;
1132 if (_handle == NULL || _handle->is_decode == FALSE) {
1133 image_util_error("Invalid Handle");
1134 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1136 if (src_buffer == NULL || src_size == 0) {
1137 image_util_error("Invalid input buffer");
1138 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1141 IMAGE_UTIL_SAFE_FREE(_handle->path);
1143 err = _image_util_decode_create_image_handle(_handle, src_buffer);
1144 if (err != IMAGE_UTIL_ERROR_NONE) {
1145 image_util_error("_image_util_decode_create_image_handle failed");
1149 IMAGE_UTIL_SAFE_FREE(_handle->src_buffer);
1151 _handle->src_buffer = (void *)calloc(1, sizeof(void *));
1152 if (_handle->src_buffer == NULL) {
1153 image_util_error("The memory of input buffer was not allocated");
1154 _image_util_decode_destroy_image_handle(_handle);
1155 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
1158 _handle->src_buffer[0] = (void *)src_buffer;
1159 _handle->src_size = src_size;
1164 int image_util_decode_set_output_buffer(image_util_decode_h handle, unsigned char **dst_buffer)
1166 int err = IMAGE_UTIL_ERROR_NONE;
1167 decode_encode_s *_handle = (decode_encode_s *) handle;
1169 if (_handle == NULL || _handle->is_decode == FALSE) {
1170 image_util_error("Invalid Handle");
1171 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1173 if (dst_buffer == NULL) {
1174 image_util_error("Invalid output buffer");
1175 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1178 _handle->dst_buffer = (void **)dst_buffer;
1183 int image_util_decode_set_colorspace(image_util_encode_h handle, image_util_colorspace_e colorspace)
1185 int err = IMAGE_UTIL_ERROR_NONE;
1186 decode_encode_s *_handle = (decode_encode_s *) handle;
1188 if (_handle == NULL || _handle->is_decode == FALSE) {
1189 image_util_error("Invalid Handle");
1190 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1193 image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_jpeg_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
1194 switch (_handle->image_type) {
1195 case IMAGE_UTIL_JPEG:
1196 image_util_retvm_if((_convert_jpeg_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
1198 case IMAGE_UTIL_PNG:
1199 image_util_retvm_if((_convert_png_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
1201 case IMAGE_UTIL_GIF:
1202 image_util_retvm_if((_convert_gif_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
1204 case IMAGE_UTIL_BMP:
1205 image_util_retvm_if((_convert_bmp_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
1208 image_util_error("Invalid image type");
1209 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1212 _handle->colorspace = colorspace;
1217 int image_util_decode_set_jpeg_downscale(image_util_encode_h handle, image_util_scale_e down_scale)
1219 int err = IMAGE_UTIL_ERROR_NONE;
1220 decode_encode_s *_handle = (decode_encode_s *) handle;
1222 if (_handle == NULL || _handle->is_decode == FALSE) {
1223 image_util_error("Invalid Handle");
1224 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1226 if (_handle->image_type != IMAGE_UTIL_JPEG) {
1227 image_util_error("Wrong image format");
1228 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
1230 image_util_retvm_if((down_scale < 0 || down_scale >= sizeof(image_util_scale_e)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "downscale is invalid");
1232 _handle->down_scale = down_scale;
1237 static int _image_util_decode_internal(decode_encode_s * _handle)
1239 int err = MM_UTIL_ERROR_NONE;
1241 switch (_handle->image_type) {
1242 case IMAGE_UTIL_JPEG:
1244 mm_util_jpeg_yuv_data *jpeg_data;
1246 jpeg_data = (mm_util_jpeg_yuv_data *) _handle->image_h;
1247 if (jpeg_data == NULL) {
1248 image_util_error("Invalid jpeg data");
1249 return MM_UTIL_ERROR_INVALID_PARAMETER;
1252 if (_handle->path) {
1253 if (_handle->down_scale < sizeof(image_util_scale_e))
1254 err = mm_util_decode_from_jpeg_file_with_downscale(jpeg_data, _handle->path, _convert_jpeg_colorspace_tbl[_handle->colorspace], _convert_decode_scale_tbl[_handle->down_scale]);
1256 err = mm_util_decode_from_jpeg_file(jpeg_data, _handle->path, _convert_jpeg_colorspace_tbl[_handle->colorspace]);
1258 if (_handle->down_scale < sizeof(image_util_scale_e))
1259 err = mm_util_decode_from_jpeg_memory_with_downscale(jpeg_data, _handle->src_buffer[0], _handle->src_size, _convert_jpeg_colorspace_tbl[_handle->colorspace], _convert_decode_scale_tbl[_handle->down_scale]);
1261 err = mm_util_decode_from_jpeg_memory(jpeg_data, _handle->src_buffer[0], _handle->src_size, _convert_jpeg_colorspace_tbl[_handle->colorspace]);
1264 if (err == MM_UTIL_ERROR_NONE) {
1265 *(_handle->dst_buffer) = jpeg_data->data;
1266 _handle->dst_size = (unsigned long long)jpeg_data->size;
1267 _handle->width = jpeg_data->width;
1268 _handle->height = jpeg_data->height;
1272 case IMAGE_UTIL_PNG:
1274 mm_util_png_data *png_data;
1276 png_data = (mm_util_png_data *) _handle->image_h;
1277 if (png_data == NULL) {
1278 image_util_error("Invalid png data");
1279 return MM_UTIL_ERROR_INVALID_PARAMETER;
1283 err = mm_util_decode_from_png_file(png_data, _handle->path);
1285 err = mm_util_decode_from_png_memory(png_data, &_handle->src_buffer[0], _handle->src_size);
1287 if (err == MM_UTIL_ERROR_NONE) {
1288 *(_handle->dst_buffer) = png_data->data;
1289 _handle->dst_size = png_data->size;
1290 _handle->width = png_data->width;
1291 _handle->height = png_data->height;
1295 case IMAGE_UTIL_GIF:
1297 mm_util_gif_data *gif_data;
1299 gif_data = (mm_util_gif_data *) _handle->image_h;
1300 if (gif_data == NULL) {
1301 image_util_error("Invalid gif data");
1302 return MM_UTIL_ERROR_INVALID_PARAMETER;
1306 err = mm_util_decode_from_gif_file(gif_data, _handle->path);
1308 err = mm_util_decode_from_gif_memory(gif_data, &_handle->src_buffer[0]);
1310 if (err == MM_UTIL_ERROR_NONE) {
1311 *(_handle->dst_buffer) = gif_data->frames[0]->data;
1312 _handle->dst_size = gif_data->size;
1313 _handle->width = gif_data->width;
1314 _handle->height = gif_data->height;
1318 case IMAGE_UTIL_BMP:
1320 mm_util_bmp_data *bmp_data;
1322 bmp_data = (mm_util_bmp_data *) _handle->image_h;
1323 if (bmp_data == NULL) {
1324 image_util_error("Invalid bmp data");
1325 return MM_UTIL_ERROR_INVALID_PARAMETER;
1329 err = mm_util_decode_from_bmp_file(bmp_data, _handle->path);
1331 err = mm_util_decode_from_bmp_memory(bmp_data, &_handle->src_buffer[0], _handle->src_size);
1333 if (err == MM_UTIL_ERROR_NONE) {
1334 *(_handle->dst_buffer) = bmp_data->data;
1335 _handle->dst_size = bmp_data->size;
1336 _handle->width = bmp_data->width;
1337 _handle->height = bmp_data->height;
1342 err = MM_UTIL_ERROR_INVALID_PARAMETER;
1349 int image_util_decode_run(image_util_decode_h handle, unsigned long *width, unsigned long *height, unsigned long long *size)
1351 int err = MM_UTIL_ERROR_NONE;
1352 decode_encode_s *_handle = (decode_encode_s *) handle;
1354 if (_handle == NULL || _handle->is_decode == FALSE) {
1355 image_util_error("Invalid Handle");
1356 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1358 if ((_handle->path == NULL && _handle->src_buffer == NULL) || _handle->dst_buffer == NULL) {
1359 image_util_error("Invalid input/output");
1360 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1363 err = _image_util_decode_internal(_handle);
1365 if (err != MM_UTIL_ERROR_NONE) {
1366 image_util_error("Error - decode run");
1367 return _convert_image_util_error_code(__func__, err);
1371 *width = _handle->width;
1373 *height = _handle->height;
1375 *size = _handle->dst_size;
1377 return _convert_image_util_error_code(__func__, err);
1380 gpointer _image_util_decode_thread(gpointer data)
1382 decode_encode_s *_handle = (decode_encode_s *) data;
1383 int err = MM_UTIL_ERROR_NONE;
1386 image_util_error("[ERROR] - handle");
1390 err = _image_util_decode_internal(_handle);
1391 if (err == MM_UTIL_ERROR_NONE)
1392 image_util_debug("Success - decode_internal");
1394 image_util_error("Error - decode_internal");
1396 if (_handle->_decode_cb) {
1397 image_util_debug("completed_cb");
1398 _handle->_decode_cb->image_decode_completed_cb(_convert_image_util_error_code(__func__, err), _handle->_decode_cb->user_data, _handle->width, _handle->height, _handle->dst_size);
1401 image_util_debug("exit thread");
1406 static int _image_util_decode_create_thread(decode_encode_s * handle)
1408 int err = MM_UTIL_ERROR_NONE;
1410 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
1411 image_util_retvm_if((handle->thread != NULL), MM_UTIL_ERROR_INVALID_OPERATION, "A thread is alread running");
1414 handle->thread = g_thread_new("decode_thread", (GThreadFunc) _image_util_decode_thread, (gpointer) handle);
1415 if (!handle->thread) {
1416 image_util_error("ERROR - create thread");
1417 return MM_UTIL_ERROR_INVALID_OPERATION;
1423 int image_util_decode_run_async(image_util_decode_h handle, image_util_decode_completed_cb completed_cb, void *user_data)
1425 int err = MM_UTIL_ERROR_NONE;
1426 decode_encode_s *_handle = (decode_encode_s *) handle;
1428 if (_handle == NULL || _handle->is_decode == FALSE) {
1429 image_util_error("Invalid Handle");
1430 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1432 if ((_handle->path == NULL && _handle->src_buffer == NULL) || _handle->dst_buffer == NULL) {
1433 image_util_error("Invalid input/output");
1434 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1436 image_util_retvm_if((completed_cb == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid callback");
1437 image_util_retvm_if((_handle->thread != NULL), MM_UTIL_ERROR_INVALID_OPERATION, "A thread is alread running");
1439 if (_handle->_decode_cb != NULL) {
1440 IMAGE_UTIL_SAFE_FREE(_handle->_decode_cb);
1441 _handle->_decode_cb = NULL;
1444 _handle->_decode_cb = (decode_cb_s *) calloc(1, sizeof(decode_cb_s));
1445 image_util_retvm_if((_handle->_decode_cb == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Out of memory");
1447 _handle->_decode_cb->user_data = user_data;
1448 _handle->_decode_cb->image_decode_completed_cb = completed_cb;
1450 err = _image_util_decode_create_thread(_handle);
1451 if (err != MM_UTIL_ERROR_NONE) {
1452 IMAGE_UTIL_SAFE_FREE(_handle->_decode_cb);
1453 _handle->_decode_cb = NULL;
1456 return _convert_image_util_error_code(__func__, err);
1459 int image_util_decode_destroy(image_util_decode_h handle)
1461 int err = IMAGE_UTIL_ERROR_NONE;
1462 decode_encode_s *_handle = (decode_encode_s *) handle;
1464 image_util_debug("image_util_decode_destroy");
1466 if (_handle == NULL || _handle->is_decode == FALSE) {
1467 image_util_error("Invalid Handle");
1468 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1471 _image_util_decode_destroy_image_handle(_handle);
1473 /* g_thread_exit(handle->thread); */
1474 if (_handle->thread) {
1475 g_thread_join(_handle->thread);
1476 IMAGE_UTIL_SAFE_FREE(_handle->_decode_cb);
1478 IMAGE_UTIL_SAFE_FREE(_handle->src_buffer);
1479 IMAGE_UTIL_SAFE_FREE(_handle);
1484 static int _image_util_encode_create_gif_buffer(void *data)
1486 mm_util_gif_data *gif_data = (mm_util_gif_data *) data;
1487 image_util_retvm_if((gif_data == NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "Invalid Operation");
1489 gif_data->frames = (mm_util_gif_frame_data **) calloc(1, sizeof(mm_util_gif_frame_data *));
1490 if (gif_data->frames == NULL) {
1491 image_util_error("Error - OUT_OF_MEMORY");
1492 IMAGE_UTIL_SAFE_FREE(gif_data);
1493 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
1495 gif_data->frames[0] = (mm_util_gif_frame_data *) calloc(1, sizeof(mm_util_gif_frame_data));
1496 if (gif_data->frames[0] == NULL) {
1497 image_util_error("Error - OUT_OF_MEMORY");
1498 IMAGE_UTIL_SAFE_FREE(gif_data->frames);
1499 IMAGE_UTIL_SAFE_FREE(gif_data);
1500 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
1503 return IMAGE_UTIL_ERROR_NONE;
1506 static void _image_util_encode_destroy_gif_buffer(void *data)
1508 mm_util_gif_data *gif_data = (mm_util_gif_data *)data;
1510 image_util_retm_if((gif_data == NULL), "Invalid GIF data");
1512 for (i = 1; i < gif_data->image_count; i++)
1513 IMAGE_UTIL_SAFE_FREE(gif_data->frames[i]);
1514 IMAGE_UTIL_SAFE_FREE(gif_data->frames[0]);
1515 IMAGE_UTIL_SAFE_FREE(gif_data->frames);
1518 static void _image_util_encode_destroy_image_handle(decode_encode_s * handle)
1520 image_util_retm_if((handle == NULL), "Invalid Handle");
1521 void *image_handle = (void *)(handle->image_h);
1522 image_util_retm_if((image_handle == NULL), "Invalid image handle");
1524 if (handle->image_type == IMAGE_UTIL_GIF) {
1525 mm_util_encode_close_gif((mm_util_gif_data *) image_handle);
1526 _image_util_encode_destroy_gif_buffer(image_handle);
1528 IMAGE_UTIL_SAFE_FREE(image_handle);
1531 static int _image_util_encode_create_jpeg_handle(decode_encode_s * handle)
1533 int err = MM_UTIL_ERROR_NONE;
1535 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
1537 mm_util_jpeg_yuv_data *_handle = (mm_util_jpeg_yuv_data *) calloc(1, sizeof(mm_util_jpeg_yuv_data));
1538 image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY);
1540 handle->image_h = (mm_util_imgp_h) _handle;
1541 handle->colorspace = IMAGE_UTIL_COLORSPACE_RGBA8888;
1542 handle->quality = 75;
1547 static int _image_util_encode_create_png_handle(decode_encode_s * handle)
1549 int err = MM_UTIL_ERROR_NONE;
1551 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
1553 mm_util_png_data *_handle = (mm_util_png_data *) calloc(1, sizeof(mm_util_png_data));
1554 image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY);
1556 mm_util_init_encode_png(_handle);
1558 handle->image_h = (mm_util_imgp_h) _handle;
1563 static int _image_util_encode_create_gif_handle(decode_encode_s * handle)
1565 int err = MM_UTIL_ERROR_NONE;
1567 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
1569 mm_util_gif_data *_handle = (mm_util_gif_data *) calloc(1, sizeof(mm_util_gif_data));
1570 image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY);
1572 err = _image_util_encode_create_gif_buffer(_handle);
1573 if (err != IMAGE_UTIL_ERROR_NONE) {
1574 image_util_error("Error - _image_util_encode_init_gif_buffer is failed (%d)", err);
1575 IMAGE_UTIL_SAFE_FREE(_handle);
1579 handle->image_h = (mm_util_imgp_h) _handle;
1584 static int _image_util_encode_create_bmp_handle(decode_encode_s * handle)
1586 int err = MM_UTIL_ERROR_NONE;
1588 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
1590 mm_util_bmp_data *_handle = (mm_util_bmp_data *) calloc(1, sizeof(mm_util_bmp_data));
1591 image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY);
1593 handle->image_h = (mm_util_imgp_h) _handle;
1598 int image_util_encode_create(image_util_type_e image_type, image_util_encode_h * handle)
1600 int err = MM_UTIL_ERROR_NONE;
1602 image_util_debug("image_util_encode_create");
1604 image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
1606 decode_encode_s *_handle = (decode_encode_s *) calloc(1, sizeof(decode_encode_s));
1607 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", IMAGE_UTIL_ERROR_OUT_OF_MEMORY);
1609 _handle->image_type = image_type;
1610 _handle->src_buffer = NULL;
1611 _handle->dst_buffer = NULL;
1612 _handle->path = NULL;
1613 _handle->image_h = 0;
1614 _handle->is_decode = FALSE;
1615 _handle->is_encoded = FALSE;
1616 _handle->image_count = 1;
1617 _handle->current_buffer_count = 0;
1618 _handle->current_resolution_count = 0;
1619 _handle->current_delay_count = 0;
1621 switch (image_type) {
1622 case IMAGE_UTIL_JPEG:
1623 err = _image_util_encode_create_jpeg_handle(_handle);
1625 case IMAGE_UTIL_PNG:
1626 err = _image_util_encode_create_png_handle(_handle);
1628 case IMAGE_UTIL_GIF:
1629 err = _image_util_encode_create_gif_handle(_handle);
1631 case IMAGE_UTIL_BMP:
1632 err = _image_util_encode_create_bmp_handle(_handle);
1635 err = MM_UTIL_ERROR_INVALID_PARAMETER;
1639 if (err != MM_UTIL_ERROR_NONE) {
1640 image_util_error("Error - create image handle");
1641 IMAGE_UTIL_SAFE_FREE(_handle);
1642 return _convert_image_util_error_code(__func__, err);
1645 *handle = (image_util_encode_h) _handle;
1647 return _convert_image_util_error_code(__func__, err);
1650 int image_util_encode_set_resolution(image_util_encode_h handle, unsigned long width, unsigned long height)
1652 int err = IMAGE_UTIL_ERROR_NONE;
1653 decode_encode_s *_handle = (decode_encode_s *) handle;
1655 if (_handle == NULL || _handle->is_decode == TRUE) {
1656 image_util_error("Invalid Handle");
1657 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1659 image_util_retvm_if((_image_util_check_resolution(width, height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
1661 switch (_handle->image_type) {
1662 case IMAGE_UTIL_JPEG:
1664 mm_util_jpeg_yuv_data *jpeg_data;
1666 jpeg_data = (mm_util_jpeg_yuv_data *) _handle->image_h;
1667 if (jpeg_data == NULL) {
1668 image_util_error("Invalid jpeg data");
1669 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1671 jpeg_data->width = width;
1672 jpeg_data->height = height;
1675 case IMAGE_UTIL_PNG:
1677 mm_util_png_data *png_data;
1679 png_data = (mm_util_png_data *) _handle->image_h;
1680 if (png_data == NULL) {
1681 image_util_error("Invalid png data");
1682 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1684 mm_util_png_encode_set_width(png_data, width);
1685 mm_util_png_encode_set_height(png_data, height);
1688 case IMAGE_UTIL_GIF:
1690 mm_util_gif_data *gif_data;
1692 gif_data = (mm_util_gif_data *) _handle->image_h;
1693 if (gif_data == NULL) {
1694 image_util_error("Invalid gif data");
1695 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1698 if (!_handle->current_resolution_count) {
1699 mm_util_gif_encode_set_width(gif_data, width);
1700 mm_util_gif_encode_set_height(gif_data, height);
1701 _handle->width = width;
1702 _handle->height = height;
1703 } else if ((width > gif_data->frames[0]->width) || (height > gif_data->frames[0]->height)) {
1704 image_util_error("Image resolution cannot be more than the resolution of the first image");
1705 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1708 if (_handle->image_count <= _handle->current_resolution_count) {
1709 gif_data->frames = (mm_util_gif_frame_data **) realloc(gif_data->frames, (_handle->image_count + 1) * sizeof(mm_util_gif_frame_data *));
1710 if (gif_data->frames == NULL) {
1711 image_util_error("Error - OUT_OF_MEMORY");
1712 IMAGE_UTIL_SAFE_FREE(_handle);
1713 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
1715 gif_data->frames[_handle->image_count] = (mm_util_gif_frame_data *) calloc(1, sizeof(mm_util_gif_frame_data));
1716 if (gif_data->frames[_handle->image_count] == NULL) {
1717 image_util_error("Error - OUT_OF_MEMORY");
1718 IMAGE_UTIL_SAFE_FREE(_handle);
1719 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
1721 _handle->image_count++;
1724 gif_data->frames[_handle->current_resolution_count]->width = width;
1725 gif_data->frames[_handle->current_resolution_count]->height = height;
1726 _handle->current_resolution_count++;
1731 case IMAGE_UTIL_BMP:
1733 mm_util_bmp_data *bmp_data;
1735 bmp_data = (mm_util_bmp_data *) _handle->image_h;
1736 if (bmp_data == NULL) {
1737 image_util_error("Invalid bmp data");
1738 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1740 mm_util_bmp_encode_set_width(bmp_data, width);
1741 mm_util_bmp_encode_set_height(bmp_data, height);
1745 err = IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1749 _handle->width = width;
1750 _handle->height = height;
1755 int image_util_encode_set_colorspace(image_util_encode_h handle, image_util_colorspace_e colorspace)
1757 int err = IMAGE_UTIL_ERROR_NONE;
1758 decode_encode_s *_handle = (decode_encode_s *) handle;
1760 if (_handle == NULL || _handle->is_decode == TRUE) {
1761 image_util_error("Invalid Handle");
1762 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1765 image_util_retvm_if((colorspace < 0 || colorspace >= sizeof(_convert_jpeg_colorspace_tbl) / sizeof(int)), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
1766 switch (_handle->image_type) {
1767 case IMAGE_UTIL_JPEG:
1768 image_util_retvm_if((_convert_jpeg_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
1770 case IMAGE_UTIL_PNG:
1771 image_util_retvm_if((_convert_png_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
1773 case IMAGE_UTIL_GIF:
1774 image_util_retvm_if((_convert_gif_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
1776 case IMAGE_UTIL_BMP:
1777 image_util_retvm_if((_convert_bmp_colorspace_tbl[colorspace] == -1), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
1780 image_util_error("Invalid image type");
1781 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1784 _handle->colorspace = colorspace;
1789 int image_util_encode_set_quality(image_util_encode_h handle, int quality)
1791 int err = IMAGE_UTIL_ERROR_NONE;
1792 decode_encode_s *_handle = (decode_encode_s *) handle;
1794 if (_handle == NULL || _handle->is_decode == TRUE) {
1795 image_util_error("Invalid Handle");
1796 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1798 if (_handle->image_type != IMAGE_UTIL_JPEG) {
1799 image_util_error("Wrong image format");
1800 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
1802 image_util_retvm_if((quality <= 0 || quality > 100), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid quality");
1804 _handle->quality = quality;
1809 int image_util_encode_set_png_compression(image_util_encode_h handle, image_util_png_compression_e compression)
1811 int err = IMAGE_UTIL_ERROR_NONE;
1812 decode_encode_s *_handle = (decode_encode_s *) handle;
1813 mm_util_png_data *png_data;
1815 if (_handle == NULL || _handle->is_decode == TRUE) {
1816 image_util_error("Invalid Handle");
1817 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1819 if (_handle->image_type != IMAGE_UTIL_PNG) {
1820 image_util_error("Wrong image format");
1821 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
1823 image_util_retvm_if((compression < IMAGE_UTIL_PNG_COMPRESSION_0 || compression > IMAGE_UTIL_PNG_COMPRESSION_9), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid compression value");
1825 png_data = (mm_util_png_data *) _handle->image_h;
1826 if (png_data == NULL) {
1827 image_util_error("Invalid png data");
1828 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1831 mm_util_png_encode_set_compression_level(png_data, compression);
1836 int image_util_encode_set_gif_frame_delay_time(image_util_encode_h handle, unsigned long long delay_time)
1838 int err = IMAGE_UTIL_ERROR_NONE;
1839 decode_encode_s *_handle = (decode_encode_s *) handle;
1840 mm_util_gif_data *gif_data;
1842 if (_handle == NULL || _handle->is_decode == TRUE) {
1843 image_util_error("Invalid Handle");
1844 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1846 if (_handle->image_type != IMAGE_UTIL_GIF) {
1847 image_util_error("Wrong image format");
1848 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
1851 gif_data = (mm_util_gif_data *) _handle->image_h;
1852 if (gif_data == NULL) {
1853 image_util_error("Invalid gif data");
1854 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1856 if (gif_data->frames == NULL) {
1857 image_util_error("Error allocating gif frames.");
1858 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1860 if (_handle->image_count <= _handle->current_delay_count) {
1861 gif_data->frames = (mm_util_gif_frame_data **) realloc(gif_data->frames, (_handle->image_count + 1) * sizeof(mm_util_gif_frame_data *));
1862 if (gif_data->frames == NULL) {
1863 image_util_error("Error - OUT_OF_MEMORY");
1864 IMAGE_UTIL_SAFE_FREE(_handle);
1865 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
1867 gif_data->frames[_handle->image_count] = (mm_util_gif_frame_data *) calloc(1, sizeof(mm_util_gif_frame_data));
1868 if (gif_data->frames[_handle->image_count] == NULL) {
1869 image_util_error("Error - OUT_OF_MEMORY");
1870 IMAGE_UTIL_SAFE_FREE(_handle);
1871 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
1873 _handle->image_count++;
1876 mm_util_gif_encode_set_frame_delay_time(gif_data->frames[_handle->current_delay_count], delay_time);
1877 _handle->current_delay_count++;
1882 int image_util_encode_set_input_buffer(image_util_encode_h handle, const unsigned char *src_buffer)
1884 int err = IMAGE_UTIL_ERROR_NONE;
1885 decode_encode_s *_handle = (decode_encode_s *) handle;
1887 if (_handle == NULL || _handle->is_decode == TRUE) {
1888 image_util_error("Invalid Handle");
1889 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1891 if (src_buffer == NULL) {
1892 image_util_error("Invalid input buffer");
1893 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1896 /* initialize buffer and value for source buffer */
1897 if (_handle->image_type == IMAGE_UTIL_GIF) {
1898 if (_handle->is_encoded) {
1899 _image_util_encode_destroy_gif_buffer(_handle->image_h);
1900 err = _image_util_encode_create_gif_buffer(_handle->image_h);
1901 if (err != IMAGE_UTIL_ERROR_NONE) {
1902 image_util_error("Error - _image_util_encode_init_gif_buffer is failed (%d)", err);
1905 _handle->is_encoded = FALSE;
1906 _handle->image_count = 1;
1907 _handle->current_buffer_count = 0;
1908 _handle->current_resolution_count = 0;
1909 _handle->current_delay_count = 0;
1911 _handle->src_buffer = (void *)realloc(_handle->src_buffer, (_handle->current_buffer_count + 1) * sizeof(void *));
1912 _handle->src_buffer[_handle->current_buffer_count] = (void *)src_buffer;
1914 if (_handle->src_buffer == NULL)
1915 _handle->src_buffer = (void *)calloc(1, sizeof(void *));
1916 _handle->src_buffer[_handle->current_buffer_count] = (void *)src_buffer;
1919 /* put the source buffer for animated-gif into array */
1920 if (_handle->image_type == IMAGE_UTIL_GIF) {
1921 mm_util_gif_data *gif_data;
1923 gif_data = (mm_util_gif_data *) _handle->image_h;
1925 if (gif_data->frames == NULL) {
1926 image_util_error("Error allocating gif frames.");
1927 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1929 if (_handle->image_count <= _handle->current_buffer_count) {
1930 gif_data->frames = (mm_util_gif_frame_data **) realloc(gif_data->frames, (_handle->image_count + 1) * sizeof(mm_util_gif_frame_data *));
1931 if (gif_data->frames == NULL) {
1932 image_util_error("Error - OUT_OF_MEMORY");
1933 IMAGE_UTIL_SAFE_FREE(_handle);
1934 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
1936 gif_data->frames[_handle->image_count] = (mm_util_gif_frame_data *) calloc(1, sizeof(mm_util_gif_frame_data));
1937 if (gif_data->frames[_handle->image_count] == NULL) {
1938 image_util_error("Error - OUT_OF_MEMORY");
1939 IMAGE_UTIL_SAFE_FREE(_handle);
1940 return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
1942 _handle->image_count++;
1945 gif_data->frames[_handle->current_buffer_count]->data = _handle->src_buffer[_handle->current_buffer_count];
1946 _handle->current_buffer_count++;
1952 int image_util_encode_set_output_path(image_util_encode_h handle, const char *path)
1954 int err = IMAGE_UTIL_ERROR_NONE;
1955 decode_encode_s *_handle = (decode_encode_s *) handle;
1957 if (_handle == NULL || _handle->is_decode == TRUE) {
1958 image_util_error("Invalid Handle");
1959 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1961 image_util_retvm_if((path == NULL || strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
1963 if (_handle->dst_buffer)
1964 _handle->dst_buffer = NULL;
1966 IMAGE_UTIL_SAFE_FREE(_handle->path);
1968 _handle->path = g_strndup(path, strlen(path));
1970 if (_handle->image_type == IMAGE_UTIL_GIF) {
1971 mm_util_gif_data *gif_data;
1973 gif_data = (mm_util_gif_data *) _handle->image_h;
1975 if (gif_data->frames == NULL) {
1976 image_util_error("Error allocating gif frames.");
1977 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1979 mm_util_encode_open_gif_file(gif_data, path);
1985 int image_util_encode_set_output_buffer(image_util_encode_h handle, unsigned char **dst_buffer)
1987 int err = IMAGE_UTIL_ERROR_NONE;
1988 decode_encode_s *_handle = (decode_encode_s *) handle;
1990 if (_handle == NULL || _handle->is_decode == TRUE) {
1991 image_util_error("Invalid Handle");
1992 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1994 if (dst_buffer == NULL) {
1995 image_util_error("Invalid output buffer");
1996 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
1999 IMAGE_UTIL_SAFE_FREE(_handle->path);
2001 _handle->dst_buffer = (void **)dst_buffer;
2002 if (_handle->image_type == IMAGE_UTIL_GIF) {
2003 mm_util_gif_data *gif_data;
2005 gif_data = (mm_util_gif_data *) _handle->image_h;
2007 if (gif_data->frames == NULL) {
2008 image_util_error("Error allocating gif frames.");
2009 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
2011 mm_util_encode_open_gif_memory(gif_data, _handle->dst_buffer);
2017 static int _image_util_encode_internal(decode_encode_s * _handle)
2019 int err = MM_UTIL_ERROR_NONE;
2021 switch (_handle->image_type) {
2022 case IMAGE_UTIL_JPEG:
2025 err = mm_util_jpeg_encode_to_file(_handle->path, _handle->src_buffer[0], _handle->width, _handle->height, _convert_jpeg_colorspace_tbl[_handle->colorspace], _handle->quality);
2027 err = mm_util_jpeg_encode_to_memory(_handle->dst_buffer, (int *)&(_handle->dst_size), _handle->src_buffer[0], _handle->width, _handle->height, _convert_jpeg_colorspace_tbl[_handle->colorspace], _handle->quality);
2030 case IMAGE_UTIL_PNG:
2032 mm_util_png_data *png_data;
2034 png_data = (mm_util_png_data *) _handle->image_h;
2035 if (png_data == NULL) {
2036 image_util_error("Invalid png data");
2037 return MM_UTIL_ERROR_INVALID_PARAMETER;
2041 err = mm_util_encode_to_png_file(&(_handle->src_buffer[0]), png_data, _handle->path);
2043 err = mm_util_encode_to_png_memory(&(_handle->src_buffer[0]), png_data);
2045 if (err == MM_UTIL_ERROR_NONE) {
2046 if (_handle->dst_buffer)
2047 *(_handle->dst_buffer) = png_data->data;
2048 _handle->dst_size = png_data->size;
2049 _handle->width = png_data->width;
2050 _handle->height = png_data->height;
2054 case IMAGE_UTIL_GIF:
2056 mm_util_gif_data *gif_data;
2058 gif_data = (mm_util_gif_data *) _handle->image_h;
2059 if (gif_data == NULL) {
2060 image_util_error("Invalid gif data");
2061 return MM_UTIL_ERROR_INVALID_PARAMETER;
2063 if ((_handle->image_count > 1) && ((_handle->image_count != _handle->current_buffer_count) || (_handle->image_count != _handle->current_resolution_count) || (_handle->image_count != _handle->current_delay_count))) {
2064 image_util_error("Total frame count does not match with the data set, for animated gif encoding");
2065 return MM_UTIL_ERROR_INVALID_OPERATION;
2068 mm_util_gif_encode_set_image_count(gif_data, _handle->image_count);
2069 err = mm_util_encode_gif(gif_data);
2071 if (err == MM_UTIL_ERROR_NONE) {
2072 _handle->dst_size = gif_data->size;
2073 _handle->width = gif_data->width;
2074 _handle->height = gif_data->height;
2078 case IMAGE_UTIL_BMP:
2080 mm_util_bmp_data *bmp_data;
2082 bmp_data = (mm_util_bmp_data *) _handle->image_h;
2083 if (bmp_data == NULL) {
2084 image_util_error("Invalid bmp data");
2085 return MM_UTIL_ERROR_INVALID_PARAMETER;
2088 bmp_data->data = _handle->src_buffer[0];
2089 if (_handle->path) {
2090 err = mm_util_encode_bmp_to_file(bmp_data, _handle->path);
2093 err = mm_util_encode_bmp_to_memory(bmp_data, &(bmp_data->data), &size);
2094 if (err == MM_UTIL_ERROR_NONE)
2095 bmp_data->size = (unsigned long long)size;
2099 if (err == MM_UTIL_ERROR_NONE) {
2100 if (_handle->dst_buffer)
2101 *(_handle->dst_buffer) = bmp_data->data;
2102 _handle->dst_size = bmp_data->size;
2103 _handle->width = bmp_data->width;
2104 _handle->height = bmp_data->height;
2109 err = MM_UTIL_ERROR_INVALID_PARAMETER;
2116 int image_util_encode_run(image_util_encode_h handle, unsigned long long *size)
2118 int err = MM_UTIL_ERROR_NONE;
2119 decode_encode_s *_handle = (decode_encode_s *) handle;
2121 if (_handle == NULL || _handle->is_decode == TRUE) {
2122 image_util_error("Invalid Handle");
2123 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
2125 if ((_handle->path == NULL && _handle->dst_buffer == NULL) || _handle->src_buffer == NULL) {
2126 image_util_error("Invalid input/output");
2127 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
2129 image_util_retvm_if((_image_util_check_resolution(_handle->width, _handle->height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
2131 err = _image_util_encode_internal(_handle);
2132 _handle->is_encoded = TRUE;
2134 if (err != MM_UTIL_ERROR_NONE) {
2135 image_util_error("Error - encode run");
2136 return _convert_image_util_error_code(__func__, err);
2140 *size = _handle->dst_size;
2142 return _convert_image_util_error_code(__func__, err);
2145 gpointer _image_util_encode_thread(gpointer data)
2147 decode_encode_s *_handle = (decode_encode_s *) data;
2148 int err = MM_UTIL_ERROR_NONE;
2151 image_util_error("[ERROR] - handle");
2155 err = _image_util_encode_internal(_handle);
2156 if (err == MM_UTIL_ERROR_NONE)
2157 image_util_debug("Success - encode_internal");
2159 image_util_error("Error - encode_internal");
2161 if (_handle->_encode_cb) {
2162 image_util_debug("completed_cb");
2163 _handle->_encode_cb->image_encode_completed_cb(_convert_image_util_error_code(__func__, err), _handle->_encode_cb->user_data, _handle->dst_size);
2166 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
2167 _handle->thread = NULL;
2168 _handle->is_encoded = TRUE;
2169 image_util_debug("exit thread");
2174 static int _image_util_encode_create_thread(decode_encode_s * handle)
2176 int ret = MM_UTIL_ERROR_NONE;
2178 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
2179 image_util_retvm_if((handle->thread != NULL), MM_UTIL_ERROR_INVALID_OPERATION, "The thread is alread running");
2182 handle->thread = g_thread_new("encode_thread", (GThreadFunc) _image_util_encode_thread, (gpointer) handle);
2183 if (!handle->thread) {
2184 image_util_error("ERROR - create thread");
2186 return MM_UTIL_ERROR_INVALID_OPERATION;
2192 int image_util_encode_run_async(image_util_encode_h handle, image_util_encode_completed_cb completed_cb, void *user_data)
2194 int err = MM_UTIL_ERROR_NONE;
2195 decode_encode_s *_handle = (decode_encode_s *) handle;
2197 if (_handle == NULL || _handle->is_decode == TRUE) {
2198 image_util_error("Invalid Handle");
2199 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
2201 if ((_handle->path == NULL && _handle->dst_buffer == NULL) || _handle->src_buffer == NULL) {
2202 image_util_error("Invalid input/output");
2203 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
2205 image_util_retvm_if((_image_util_check_resolution(_handle->width, _handle->height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
2206 image_util_retvm_if((completed_cb == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid callback");
2207 image_util_retvm_if((_handle->thread != NULL), MM_UTIL_ERROR_INVALID_OPERATION, "The thread is alread running");
2209 if (_handle->_encode_cb != NULL) {
2210 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
2211 _handle->_encode_cb = NULL;
2213 _handle->_encode_cb = (encode_cb_s *) calloc(1, sizeof(encode_cb_s));
2214 image_util_retvm_if((_handle->_encode_cb == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Out of memory");
2216 _handle->_encode_cb->user_data = user_data;
2217 _handle->_encode_cb->image_encode_completed_cb = completed_cb;
2219 err = _image_util_encode_create_thread(_handle);
2220 if (err != MM_UTIL_ERROR_NONE) {
2221 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
2222 _handle->_encode_cb = NULL;
2225 return _convert_image_util_error_code(__func__, err);
2228 int image_util_encode_destroy(image_util_encode_h handle)
2230 int err = IMAGE_UTIL_ERROR_NONE;
2231 decode_encode_s *_handle = (decode_encode_s *) handle;
2233 image_util_debug("image_util_encode_destroy");
2235 if (_handle == NULL || _handle->is_decode == TRUE) {
2236 image_util_error("Invalid Handle");
2237 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
2240 _image_util_encode_destroy_image_handle(_handle);
2242 /* g_thread_exit(handle->thread); */
2243 if (_handle->thread) {
2244 g_thread_join(_handle->thread);
2245 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
2248 IMAGE_UTIL_SAFE_FREE(_handle->path);
2249 IMAGE_UTIL_SAFE_FREE(_handle->src_buffer);
2250 IMAGE_UTIL_SAFE_FREE(_handle);