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.
17 #include <mm_util_imgp.h>
18 #include <mm_util_jpeg.h>
19 #include <mm_util_png.h>
20 #include <mm_util_gif.h>
21 #include <mm_util_bmp.h>
23 #include <image_util.h>
24 #include <image_util_private.h>
26 static int _image_util_encode_get_gif_frame(mm_gif_file_h gif_data, unsigned int index, mm_gif_image_h *frame)
28 int err = MM_UTIL_ERROR_NONE;
29 image_util_retvm_if((gif_data == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
30 image_util_retvm_if((frame == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
32 err = mm_util_gif_enocde_get_image_handle(gif_data, (int)index, frame);
33 if (err != MM_UTIL_ERROR_NONE) {
34 image_util_error("mm_util_gif_enocde_get_image_handle is failed %d", err);
35 return _convert_image_util_error_code(__func__, err);
38 err = mm_util_gif_image_create(gif_data, frame);
39 if (err != MM_UTIL_ERROR_NONE) {
40 image_util_error("mm_util_gif_image_create is failed %d", err);
41 return _convert_image_util_error_code(__func__, err);
43 err = mm_util_gif_enocde_set_image_handle(gif_data, *frame);
44 if (err != MM_UTIL_ERROR_NONE) {
45 image_util_error("mm_util_gif_enocde_set_image_handle is failed %d", err);
46 mm_util_gif_image_destory(*frame);
47 return _convert_image_util_error_code(__func__, err);
53 static void _image_util_encode_destroy_image_handle(decode_encode_s * handle)
55 image_util_retm_if((handle == NULL), "Invalid Handle");
56 void *image_handle = (void *)(handle->image_h);
57 image_util_retm_if((image_handle == NULL), "Invalid image handle");
59 if (handle->image_type == IMAGE_UTIL_GIF)
60 mm_util_gif_encode_destroy(image_handle);
62 IMAGE_UTIL_SAFE_FREE(image_handle);
65 static int _image_util_encode_create_jpeg_handle(decode_encode_s * handle)
67 int err = MM_UTIL_ERROR_NONE;
69 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
71 mm_util_jpeg_yuv_data *_handle = (mm_util_jpeg_yuv_data *) calloc(1, sizeof(mm_util_jpeg_yuv_data));
72 image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY);
74 handle->image_h = (mm_util_imgp_h) _handle;
75 handle->colorspace = IMAGE_UTIL_COLORSPACE_RGBA8888;
81 static int _image_util_encode_create_png_handle(decode_encode_s * handle)
83 int err = MM_UTIL_ERROR_NONE;
85 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
87 mm_util_png_data *_handle = (mm_util_png_data *) calloc(1, sizeof(mm_util_png_data));
88 image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY);
90 mm_util_init_encode_png(_handle);
92 handle->image_h = (mm_util_imgp_h) _handle;
97 static int _image_util_encode_create_gif_handle(decode_encode_s * handle)
99 int err = MM_UTIL_ERROR_NONE;
100 mm_gif_file_h _handle = NULL;
102 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
104 err = mm_util_gif_encode_create(&_handle);
105 image_util_retvm_if((err != MM_UTIL_ERROR_NONE), _convert_image_util_error_code(__func__, err), "Error - mm_util_gif_encode_create is failed (%d)", err);
107 handle->image_h = (mm_util_imgp_h) _handle;
112 static int _image_util_encode_create_bmp_handle(decode_encode_s * handle)
114 int err = MM_UTIL_ERROR_NONE;
116 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
118 mm_util_bmp_data *_handle = (mm_util_bmp_data *) calloc(1, sizeof(mm_util_bmp_data));
119 image_util_retvm_if((_handle == NULL), MM_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", MM_UTIL_ERROR_OUT_OF_MEMORY);
121 handle->image_h = (mm_util_imgp_h) _handle;
126 int image_util_encode_create(image_util_type_e image_type, image_util_encode_h * handle)
128 int err = MM_UTIL_ERROR_NONE;
130 image_util_debug("image_util_encode_create");
132 image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
134 decode_encode_s *_handle = (decode_encode_s *) calloc(1, sizeof(decode_encode_s));
135 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", IMAGE_UTIL_ERROR_OUT_OF_MEMORY);
137 _handle->image_type = image_type;
138 _handle->src_buffer = NULL;
139 _handle->dst_buffer = NULL;
140 _handle->path = NULL;
141 _handle->image_h = 0;
142 _handle->is_decode = FALSE;
143 _handle->is_encoded = FALSE;
144 _handle->current_buffer_count = 0;
145 _handle->current_resolution_count = 0;
146 _handle->current_delay_count = 0;
148 switch (image_type) {
149 case IMAGE_UTIL_JPEG:
150 err = _image_util_encode_create_jpeg_handle(_handle);
153 err = _image_util_encode_create_png_handle(_handle);
156 err = _image_util_encode_create_gif_handle(_handle);
159 err = _image_util_encode_create_bmp_handle(_handle);
162 err = MM_UTIL_ERROR_INVALID_PARAMETER;
166 if (err != MM_UTIL_ERROR_NONE) {
167 image_util_error("Error - create image handle");
168 IMAGE_UTIL_SAFE_FREE(_handle);
169 return _convert_image_util_error_code(__func__, err);
172 *handle = (image_util_encode_h) _handle;
174 return _convert_image_util_error_code(__func__, err);
177 int image_util_encode_set_resolution(image_util_encode_h handle, unsigned long width, unsigned long height)
179 int err = IMAGE_UTIL_ERROR_NONE;
180 decode_encode_s *_handle = (decode_encode_s *) handle;
182 if (_handle == NULL || _handle->is_decode == TRUE) {
183 image_util_error("Invalid Handle");
184 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
186 image_util_retvm_if((_image_util_check_resolution(width, height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
188 switch (_handle->image_type) {
189 case IMAGE_UTIL_JPEG:
191 mm_util_jpeg_yuv_data *jpeg_data;
193 jpeg_data = (mm_util_jpeg_yuv_data *) _handle->image_h;
194 if (jpeg_data == NULL) {
195 image_util_error("Invalid jpeg data");
196 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
198 jpeg_data->width = width;
199 jpeg_data->height = height;
204 mm_util_png_data *png_data;
206 png_data = (mm_util_png_data *) _handle->image_h;
207 if (png_data == NULL) {
208 image_util_error("Invalid png data");
209 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
211 mm_util_png_encode_set_width(png_data, width);
212 mm_util_png_encode_set_height(png_data, height);
217 mm_gif_file_h gif_data = (mm_gif_file_h)_handle->image_h;
218 if (gif_data == NULL) {
219 image_util_error("Invalid gif data");
220 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
223 image_util_retvm_if((width > INT_MAX) || (height > INT_MAX), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
225 if (_handle->current_resolution_count == 0) {
226 _handle->width = width;
227 _handle->height = height;
228 mm_util_gif_encode_set_resolution(gif_data, width, height);
230 mm_gif_image_h frame = NULL;
231 err = _image_util_encode_get_gif_frame(gif_data, _handle->current_resolution_count, &frame);
232 if (err != MM_UTIL_ERROR_NONE) {
233 image_util_error("_image_util_encode_get_gif_frame is failed %d", err);
234 return _convert_image_util_error_code(__func__, err);
236 err = mm_util_gif_image_set_position(frame, 0, 0, (int)width, (int)height);
237 if (err != MM_UTIL_ERROR_NONE) {
238 image_util_error("mm_util_gif_image_set_position is failed %d", err);
239 return _convert_image_util_error_code(__func__, err);
241 _handle->current_resolution_count++;
248 mm_util_bmp_data *bmp_data;
250 bmp_data = (mm_util_bmp_data *) _handle->image_h;
251 if (bmp_data == NULL) {
252 image_util_error("Invalid bmp data");
253 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
255 mm_util_bmp_encode_set_width(bmp_data, width);
256 mm_util_bmp_encode_set_height(bmp_data, height);
260 err = IMAGE_UTIL_ERROR_INVALID_PARAMETER;
264 _handle->width = width;
265 _handle->height = height;
270 int image_util_encode_set_colorspace(image_util_encode_h handle, image_util_colorspace_e colorspace)
272 int err = IMAGE_UTIL_ERROR_NONE;
273 decode_encode_s *_handle = (decode_encode_s *) handle;
275 if (_handle == NULL || _handle->is_decode == TRUE) {
276 image_util_error("Invalid Handle");
277 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
280 image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
282 if ((_handle->image_type == IMAGE_UTIL_JPEG) || (_handle->image_type == IMAGE_UTIL_PNG)
283 || (_handle->image_type == IMAGE_UTIL_GIF) || (_handle->image_type == IMAGE_UTIL_BMP)) {
284 image_util_retvm_if((is_supported_colorspace(colorspace, _handle->image_type) == FALSE), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
286 image_util_error("Invalid image type");
287 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
290 _handle->colorspace = colorspace;
295 int image_util_encode_set_quality(image_util_encode_h handle, int quality)
297 int err = IMAGE_UTIL_ERROR_NONE;
298 decode_encode_s *_handle = (decode_encode_s *) handle;
300 if (_handle == NULL || _handle->is_decode == TRUE) {
301 image_util_error("Invalid Handle");
302 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
304 if (_handle->image_type != IMAGE_UTIL_JPEG) {
305 image_util_error("Wrong image format");
306 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
308 image_util_retvm_if((quality <= 0 || quality > 100), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid quality");
310 _handle->quality = quality;
315 int image_util_encode_set_png_compression(image_util_encode_h handle, image_util_png_compression_e compression)
317 int err = IMAGE_UTIL_ERROR_NONE;
318 decode_encode_s *_handle = (decode_encode_s *) handle;
319 mm_util_png_data *png_data;
321 if (_handle == NULL || _handle->is_decode == TRUE) {
322 image_util_error("Invalid Handle");
323 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
325 if (_handle->image_type != IMAGE_UTIL_PNG) {
326 image_util_error("Wrong image format");
327 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
329 image_util_retvm_if((compression < IMAGE_UTIL_PNG_COMPRESSION_0 || compression > IMAGE_UTIL_PNG_COMPRESSION_9), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid compression value");
331 png_data = (mm_util_png_data *) _handle->image_h;
332 if (png_data == NULL) {
333 image_util_error("Invalid png data");
334 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
337 mm_util_png_encode_set_compression_level(png_data, compression);
342 int image_util_encode_set_gif_frame_delay_time(image_util_encode_h handle, unsigned long long delay_time)
344 int err = IMAGE_UTIL_ERROR_NONE;
345 decode_encode_s *_handle = (decode_encode_s *) handle;
346 mm_gif_file_h gif_data = NULL;
348 if (_handle == NULL || _handle->is_decode == TRUE) {
349 image_util_error("Invalid Handle");
350 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
352 if (_handle->image_type != IMAGE_UTIL_GIF) {
353 image_util_error("Wrong image format");
354 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
357 gif_data = (mm_gif_file_h) _handle->image_h;
358 if (gif_data == NULL) {
359 image_util_error("Invalid gif data");
360 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
363 image_util_retvm_if((delay_time > INT_MAX), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid delay time");
365 mm_gif_image_h frame = NULL;
366 err = _image_util_encode_get_gif_frame(gif_data, _handle->current_delay_count, &frame);
367 if (err != MM_UTIL_ERROR_NONE) {
368 image_util_error("_image_util_encode_get_gif_frame is failed %d", err);
369 return _convert_image_util_error_code(__func__, err);
371 err = mm_util_gif_image_set_delay_time(frame, (int)delay_time);
372 if (err != MM_UTIL_ERROR_NONE) {
373 image_util_error("mm_util_gif_image_set_delay_time is failed %d", err);
374 return _convert_image_util_error_code(__func__, err);
376 _handle->current_delay_count++;
381 int image_util_encode_set_input_buffer(image_util_encode_h handle, const unsigned char *src_buffer)
383 int err = IMAGE_UTIL_ERROR_NONE;
384 decode_encode_s *_handle = (decode_encode_s *) handle;
386 if (_handle == NULL || _handle->is_decode == TRUE) {
387 image_util_error("Invalid Handle");
388 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
390 if (src_buffer == NULL) {
391 image_util_error("Invalid input buffer");
392 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
395 /* initialize buffer and value for source buffer */
396 if (_handle->image_type == IMAGE_UTIL_GIF) {
397 mm_gif_file_h gif_data = (mm_gif_file_h) _handle->image_h;
398 mm_gif_image_h frame = NULL;
399 if (gif_data == NULL) {
400 image_util_error("Invalid gif data");
401 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
403 err = _image_util_encode_get_gif_frame(gif_data, _handle->current_buffer_count, &frame);
404 if (err != MM_UTIL_ERROR_NONE) {
405 image_util_error("_image_util_encode_get_gif_frame is failed %d", err);
406 return _convert_image_util_error_code(__func__, err);
408 err = mm_util_gif_image_set_image(frame, src_buffer);
409 if (err != MM_UTIL_ERROR_NONE) {
410 image_util_error("mm_util_gif_image_set_delay_time is failed %d", err);
411 return _convert_image_util_error_code(__func__, err);
413 _handle->current_buffer_count++;
415 if (_handle->src_buffer == NULL)
416 _handle->src_buffer = (void *)calloc(1, sizeof(void *));
417 image_util_retvm_if(_handle->src_buffer == NULL, IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "calloc fail");
419 _handle->src_buffer[_handle->current_buffer_count] = (void *)src_buffer;
425 int image_util_encode_set_output_path(image_util_encode_h handle, const char *path)
427 int err = IMAGE_UTIL_ERROR_NONE;
428 decode_encode_s *_handle = (decode_encode_s *) handle;
430 if (_handle == NULL || _handle->is_decode == TRUE) {
431 image_util_error("Invalid Handle");
432 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
434 image_util_retvm_if((path == NULL || strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
436 if (_handle->dst_buffer)
437 _handle->dst_buffer = NULL;
439 IMAGE_UTIL_SAFE_FREE(_handle->path);
441 _handle->path = g_strndup(path, strlen(path));
446 int image_util_encode_set_output_buffer(image_util_encode_h handle, unsigned char **dst_buffer)
448 int err = IMAGE_UTIL_ERROR_NONE;
449 decode_encode_s *_handle = (decode_encode_s *) handle;
451 if (_handle == NULL || _handle->is_decode == TRUE) {
452 image_util_error("Invalid Handle");
453 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
455 if (dst_buffer == NULL) {
456 image_util_error("Invalid output buffer");
457 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
460 IMAGE_UTIL_SAFE_FREE(_handle->path);
462 _handle->dst_buffer = (void **)dst_buffer;
467 static int _image_util_encode_internal(decode_encode_s * _handle)
469 int err = MM_UTIL_ERROR_NONE;
471 switch (_handle->image_type) {
472 case IMAGE_UTIL_JPEG:
475 err = mm_util_jpeg_encode_to_file(_handle->path, _handle->src_buffer[0], _handle->width, _handle->height, TYPECAST_COLOR_BY_TYPE(_handle->colorspace, IMAGE_UTIL_JPEG), _handle->quality);
477 err = mm_util_jpeg_encode_to_memory(_handle->dst_buffer, (unsigned int *)&(_handle->dst_size), _handle->src_buffer[0], _handle->width, _handle->height, TYPECAST_COLOR_BY_TYPE(_handle->colorspace, IMAGE_UTIL_JPEG), _handle->quality);
482 mm_util_png_data *png_data;
484 png_data = (mm_util_png_data *) _handle->image_h;
485 if (png_data == NULL) {
486 image_util_error("Invalid png data");
487 return MM_UTIL_ERROR_INVALID_PARAMETER;
491 err = mm_util_encode_to_png_file(&(_handle->src_buffer[0]), png_data, _handle->path);
493 err = mm_util_encode_to_png_memory(&(_handle->src_buffer[0]), png_data);
495 if (err == MM_UTIL_ERROR_NONE) {
496 if (_handle->dst_buffer)
497 *(_handle->dst_buffer) = png_data->data;
498 _handle->dst_size = png_data->size;
499 _handle->width = png_data->width;
500 _handle->height = png_data->height;
506 mm_gif_file_h gif_data = (mm_gif_file_h)_handle->image_h;
507 unsigned long encoded_buffer_size = 0;
508 if (gif_data == NULL) {
509 image_util_error("Invalid gif data");
510 return MM_UTIL_ERROR_INVALID_PARAMETER;
512 image_util_debug("[Count] buffer:%d, resolution:%d, delay:%d", _handle->current_buffer_count, _handle->current_resolution_count, _handle->current_delay_count);
513 if ((_handle->current_buffer_count > 1) && ((_handle->current_buffer_count != _handle->current_resolution_count) || (_handle->current_buffer_count != _handle->current_delay_count))) {
514 image_util_error("Total frame count does not match with the data set, for animated gif encoding");
515 return MM_UTIL_ERROR_INVALID_OPERATION;
516 } else if ((_handle->current_buffer_count > 0) && ((_handle->current_buffer_count != _handle->current_resolution_count))) {
517 image_util_error("Total frame count does not match with the data set, for gif encoding");
518 return MM_UTIL_ERROR_INVALID_OPERATION;
521 err = mm_util_gif_encode_set_file(gif_data, _handle->path);
523 err = mm_util_gif_encode_set_mem(gif_data, _handle->dst_buffer, &encoded_buffer_size);
524 if (err != MM_UTIL_ERROR_NONE) {
525 image_util_error("mm_util_gif_encode_set_file | mm_util_gif_encode_set_mem is failed(%d)", err);
526 return _convert_image_util_error_code(__func__, err);
528 err = mm_util_gif_encode(gif_data);
529 if (err != MM_UTIL_ERROR_NONE) {
530 image_util_error("mm_util_gif_encode is failed(%d)", err);
531 return _convert_image_util_error_code(__func__, err);
533 if (encoded_buffer_size != 0)
534 _handle->dst_size = (unsigned long long)encoded_buffer_size;
539 mm_util_bmp_data *bmp_data;
541 bmp_data = (mm_util_bmp_data *) _handle->image_h;
542 if (bmp_data == NULL) {
543 image_util_error("Invalid bmp data");
544 return MM_UTIL_ERROR_INVALID_PARAMETER;
547 bmp_data->data = _handle->src_buffer[0];
549 err = mm_util_encode_bmp_to_file(bmp_data, _handle->path);
552 err = mm_util_encode_bmp_to_memory(bmp_data, &(bmp_data->data), &size);
553 if (err == MM_UTIL_ERROR_NONE)
554 bmp_data->size = (unsigned long long)size;
558 if (err == MM_UTIL_ERROR_NONE) {
559 if (_handle->dst_buffer)
560 *(_handle->dst_buffer) = bmp_data->data;
561 _handle->dst_size = bmp_data->size;
562 _handle->width = bmp_data->width;
563 _handle->height = bmp_data->height;
568 err = MM_UTIL_ERROR_INVALID_PARAMETER;
575 int image_util_encode_run(image_util_encode_h handle, unsigned long long *size)
577 int err = MM_UTIL_ERROR_NONE;
578 decode_encode_s *_handle = (decode_encode_s *) handle;
580 if (_handle == NULL || _handle->is_decode == TRUE) {
581 image_util_error("Invalid Handle");
582 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
584 if (_handle->path == NULL && _handle->dst_buffer == NULL) {
585 image_util_error("Invalid output");
586 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
588 if (_handle->image_type != IMAGE_UTIL_GIF && _handle->src_buffer == NULL) {
589 image_util_error("Invalid input");
590 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
592 image_util_retvm_if((_image_util_check_resolution(_handle->width, _handle->height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
594 err = _image_util_encode_internal(_handle);
595 _handle->is_encoded = TRUE;
597 if (err != MM_UTIL_ERROR_NONE) {
598 image_util_error("Error - encode run");
599 return _convert_image_util_error_code(__func__, err);
603 *size = _handle->dst_size;
605 return _convert_image_util_error_code(__func__, err);
608 gpointer _image_util_encode_thread(gpointer data)
610 decode_encode_s *_handle = (decode_encode_s *) data;
611 int err = MM_UTIL_ERROR_NONE;
614 image_util_error("[ERROR] - handle");
618 err = _image_util_encode_internal(_handle);
619 if (err == MM_UTIL_ERROR_NONE)
620 image_util_debug("Success - encode_internal");
622 image_util_error("Error - encode_internal");
624 if (_handle->_encode_cb) {
625 image_util_debug("completed_cb");
626 _handle->_encode_cb->image_encode_completed_cb(_convert_image_util_error_code(__func__, err), _handle->_encode_cb->user_data, _handle->dst_size);
629 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
630 _handle->thread = NULL;
631 _handle->is_encoded = TRUE;
632 image_util_debug("exit thread");
637 static int _image_util_encode_create_thread(decode_encode_s * handle)
639 int ret = MM_UTIL_ERROR_NONE;
641 image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
642 image_util_retvm_if((handle->thread != NULL), MM_UTIL_ERROR_INVALID_OPERATION, "The thread is alread running");
645 handle->thread = g_thread_new("encode_thread", (GThreadFunc) _image_util_encode_thread, (gpointer) handle);
646 if (!handle->thread) {
647 image_util_error("ERROR - create thread");
649 return MM_UTIL_ERROR_INVALID_OPERATION;
655 int image_util_encode_run_async(image_util_encode_h handle, image_util_encode_completed_cb completed_cb, void *user_data)
657 int err = MM_UTIL_ERROR_NONE;
658 decode_encode_s *_handle = (decode_encode_s *) handle;
660 if (_handle == NULL || _handle->is_decode == TRUE) {
661 image_util_error("Invalid Handle");
662 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
664 if (_handle->path == NULL && _handle->dst_buffer == NULL) {
665 image_util_error("Invalid output");
666 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
668 if (_handle->image_type != IMAGE_UTIL_GIF && _handle->src_buffer == NULL) {
669 image_util_error("Invalid input");
670 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
672 image_util_retvm_if((_image_util_check_resolution(_handle->width, _handle->height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
673 image_util_retvm_if((completed_cb == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid callback");
674 image_util_retvm_if((_handle->thread != NULL), MM_UTIL_ERROR_INVALID_OPERATION, "The thread is alread running");
676 if (_handle->_encode_cb != NULL) {
677 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
678 _handle->_encode_cb = NULL;
680 _handle->_encode_cb = (encode_cb_s *) calloc(1, sizeof(encode_cb_s));
681 image_util_retvm_if((_handle->_encode_cb == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Out of memory");
683 _handle->_encode_cb->user_data = user_data;
684 _handle->_encode_cb->image_encode_completed_cb = completed_cb;
686 err = _image_util_encode_create_thread(_handle);
687 if (err != MM_UTIL_ERROR_NONE) {
688 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
689 _handle->_encode_cb = NULL;
692 return _convert_image_util_error_code(__func__, err);
695 int image_util_encode_destroy(image_util_encode_h handle)
697 int err = IMAGE_UTIL_ERROR_NONE;
698 decode_encode_s *_handle = (decode_encode_s *) handle;
700 image_util_debug("image_util_encode_destroy");
702 if (_handle == NULL || _handle->is_decode == TRUE) {
703 image_util_error("Invalid Handle");
704 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
707 _image_util_encode_destroy_image_handle(_handle);
709 /* g_thread_exit(handle->thread); */
710 if (_handle->thread) {
711 g_thread_join(_handle->thread);
712 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
715 IMAGE_UTIL_SAFE_FREE(_handle->path);
716 IMAGE_UTIL_SAFE_FREE(_handle->src_buffer);
717 IMAGE_UTIL_SAFE_FREE(_handle);
722 int image_util_encode_jpeg(const unsigned char *buffer, int width, int height, image_util_colorspace_e colorspace, int quality, const char *path)
724 int err = MM_UTIL_ERROR_NONE;
726 DEPRECATION_LOGW("image_util_encode_jpeg()", "image_util_encode_create()");
728 image_util_retvm_if((path == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "path is null");
729 image_util_retvm_if((buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "buffer is null");
730 image_util_retvm_if((strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
731 image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
732 image_util_retvm_if((is_supported_colorspace(colorspace, IMAGE_UTIL_JPEG) == FALSE), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
734 err = mm_util_jpeg_encode_to_file(path, (void *)buffer, width, height, TYPECAST_COLOR_BY_TYPE(colorspace, IMAGE_UTIL_JPEG), quality);
735 return _convert_image_util_error_code(__func__, err);
738 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)
740 int err = MM_UTIL_ERROR_NONE;
742 DEPRECATION_LOGW("image_util_encode_jpeg_to_memory()", "image_util_encode_create()");
744 image_util_retvm_if((jpeg_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "jpeg_buffer is null");
745 image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
746 image_util_retvm_if((jpeg_size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "jpeg_size is null");
747 image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
748 image_util_retvm_if((is_supported_colorspace(colorspace, IMAGE_UTIL_JPEG) == FALSE), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
750 err = mm_util_jpeg_encode_to_memory((void **)jpeg_buffer, jpeg_size, (void *)image_buffer, width, height, TYPECAST_COLOR_BY_TYPE(colorspace, IMAGE_UTIL_JPEG), quality);
751 return _convert_image_util_error_code(__func__, err);