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), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
30 image_util_retvm_if((frame == NULL), IMAGE_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 _image_error_capi(ERR_TYPE_ENCODE, 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 _image_error_capi(ERR_TYPE_ENCODE, 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 _image_error_capi(ERR_TYPE_ENCODE, err);
50 return _image_error_capi(ERR_TYPE_ENCODE, 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 image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
69 mm_util_jpeg_yuv_data *_handle = (mm_util_jpeg_yuv_data *) calloc(1, sizeof(mm_util_jpeg_yuv_data));
70 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
72 handle->image_h = (mm_util_imgp_h) _handle;
73 handle->colorspace = IMAGE_UTIL_COLORSPACE_RGBA8888;
76 return IMAGE_UTIL_ERROR_NONE;
79 static int _image_util_encode_create_png_handle(decode_encode_s * handle)
81 image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
83 mm_util_png_data *_handle = (mm_util_png_data *) calloc(1, sizeof(mm_util_png_data));
84 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
86 mm_util_init_encode_png(_handle);
88 handle->image_h = (mm_util_imgp_h) _handle;
90 return IMAGE_UTIL_ERROR_NONE;
93 static int _image_util_encode_create_gif_handle(decode_encode_s * handle)
95 int err = MM_UTIL_ERROR_NONE;
96 mm_gif_file_h _handle = NULL;
98 image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
100 err = mm_util_gif_encode_create(&_handle);
101 image_util_retvm_if((err != MM_UTIL_ERROR_NONE), _image_error_capi(ERR_TYPE_ENCODE, err), "Error - mm_util_gif_encode_create is failed (%d)", err);
103 handle->image_h = (mm_util_imgp_h) _handle;
105 return IMAGE_UTIL_ERROR_NONE;
108 static int _image_util_encode_create_bmp_handle(decode_encode_s * handle)
110 image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
112 mm_util_bmp_data *_handle = (mm_util_bmp_data *) calloc(1, sizeof(mm_util_bmp_data));
113 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
115 handle->image_h = (mm_util_imgp_h) _handle;
117 return IMAGE_UTIL_ERROR_NONE;
120 int image_util_encode_create(image_util_type_e image_type, image_util_encode_h * handle)
122 int err = IMAGE_UTIL_ERROR_NONE;
124 image_util_debug("image_util_encode_create");
126 image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
128 decode_encode_s *_handle = (decode_encode_s *) calloc(1, sizeof(decode_encode_s));
129 image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY(0x%08x)", IMAGE_UTIL_ERROR_OUT_OF_MEMORY);
131 _handle->image_type = image_type;
132 _handle->src_buffer = NULL;
133 _handle->dst_buffer = NULL;
134 _handle->path = NULL;
135 _handle->image_h = 0;
136 _handle->is_decode = FALSE;
137 _handle->is_encoded = FALSE;
138 _handle->current_buffer_count = 0;
139 _handle->current_resolution_count = 0;
140 _handle->current_delay_count = 0;
142 switch (image_type) {
143 case IMAGE_UTIL_JPEG:
144 err = _image_util_encode_create_jpeg_handle(_handle);
147 err = _image_util_encode_create_png_handle(_handle);
150 err = _image_util_encode_create_gif_handle(_handle);
153 err = _image_util_encode_create_bmp_handle(_handle);
156 err = IMAGE_UTIL_ERROR_INVALID_PARAMETER;
160 if (err != IMAGE_UTIL_ERROR_NONE) {
161 image_util_error("Error - create image handle");
162 IMAGE_UTIL_SAFE_FREE(_handle);
166 *handle = (image_util_encode_h) _handle;
171 int image_util_encode_set_resolution(image_util_encode_h handle, unsigned long width, unsigned long height)
173 int err = IMAGE_UTIL_ERROR_NONE;
174 decode_encode_s *_handle = (decode_encode_s *) handle;
176 if (_handle == NULL || _handle->is_decode == TRUE) {
177 image_util_error("Invalid Handle");
178 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
180 image_util_retvm_if((_image_util_check_resolution(width, height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
182 switch (_handle->image_type) {
183 case IMAGE_UTIL_JPEG:
185 mm_util_jpeg_yuv_data *jpeg_data;
187 jpeg_data = (mm_util_jpeg_yuv_data *) _handle->image_h;
188 if (jpeg_data == NULL) {
189 image_util_error("Invalid jpeg data");
190 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
192 jpeg_data->width = width;
193 jpeg_data->height = height;
198 mm_util_png_data *png_data;
200 png_data = (mm_util_png_data *) _handle->image_h;
201 if (png_data == NULL) {
202 image_util_error("Invalid png data");
203 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
205 mm_util_png_encode_set_width(png_data, width);
206 mm_util_png_encode_set_height(png_data, height);
211 mm_gif_file_h gif_data = (mm_gif_file_h)_handle->image_h;
212 if (gif_data == NULL) {
213 image_util_error("Invalid gif data");
214 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
217 image_util_retvm_if((width > INT_MAX) || (height > INT_MAX), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
219 if (_handle->current_resolution_count == 0) {
220 _handle->width = width;
221 _handle->height = height;
222 mm_util_gif_encode_set_resolution(gif_data, width, height);
224 mm_gif_image_h frame = NULL;
225 err = _image_util_encode_get_gif_frame(gif_data, _handle->current_resolution_count, &frame);
226 if (err != IMAGE_UTIL_ERROR_NONE) {
227 image_util_error("_image_util_encode_get_gif_frame is failed %d", err);
230 err = mm_util_gif_image_set_position(frame, 0, 0, (int)width, (int)height);
231 if (err != MM_UTIL_ERROR_NONE) {
232 image_util_error("mm_util_gif_image_set_position is failed %d", err);
233 return _image_error_capi(ERR_TYPE_ENCODE, err);
235 _handle->current_resolution_count++;
242 mm_util_bmp_data *bmp_data;
244 bmp_data = (mm_util_bmp_data *) _handle->image_h;
245 if (bmp_data == NULL) {
246 image_util_error("Invalid bmp data");
247 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
249 mm_util_bmp_encode_set_width(bmp_data, width);
250 mm_util_bmp_encode_set_height(bmp_data, height);
254 err = IMAGE_UTIL_ERROR_INVALID_PARAMETER;
258 _handle->width = width;
259 _handle->height = height;
264 int image_util_encode_set_colorspace(image_util_encode_h handle, image_util_colorspace_e colorspace)
266 int err = IMAGE_UTIL_ERROR_NONE;
267 decode_encode_s *_handle = (decode_encode_s *) handle;
269 if (_handle == NULL || _handle->is_decode == TRUE) {
270 image_util_error("Invalid Handle");
271 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
274 image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
276 if ((_handle->image_type == IMAGE_UTIL_JPEG) || (_handle->image_type == IMAGE_UTIL_PNG)
277 || (_handle->image_type == IMAGE_UTIL_GIF) || (_handle->image_type == IMAGE_UTIL_BMP)) {
278 image_util_retvm_if((is_supported_colorspace(colorspace, _handle->image_type) == FALSE), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
280 image_util_error("Invalid image type");
281 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
284 _handle->colorspace = colorspace;
289 int image_util_encode_set_quality(image_util_encode_h handle, int quality)
291 int err = IMAGE_UTIL_ERROR_NONE;
292 decode_encode_s *_handle = (decode_encode_s *) handle;
294 if (_handle == NULL || _handle->is_decode == TRUE) {
295 image_util_error("Invalid Handle");
296 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
298 if (_handle->image_type != IMAGE_UTIL_JPEG) {
299 image_util_error("Wrong image format");
300 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
302 image_util_retvm_if((quality <= 0 || quality > 100), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid quality");
304 _handle->quality = quality;
309 int image_util_encode_set_png_compression(image_util_encode_h handle, image_util_png_compression_e compression)
311 int err = IMAGE_UTIL_ERROR_NONE;
312 decode_encode_s *_handle = (decode_encode_s *) handle;
313 mm_util_png_data *png_data;
315 if (_handle == NULL || _handle->is_decode == TRUE) {
316 image_util_error("Invalid Handle");
317 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
319 if (_handle->image_type != IMAGE_UTIL_PNG) {
320 image_util_error("Wrong image format");
321 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
323 image_util_retvm_if((compression < IMAGE_UTIL_PNG_COMPRESSION_0 || compression > IMAGE_UTIL_PNG_COMPRESSION_9), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid compression value");
325 png_data = (mm_util_png_data *) _handle->image_h;
326 if (png_data == NULL) {
327 image_util_error("Invalid png data");
328 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
331 mm_util_png_encode_set_compression_level(png_data, compression);
336 int image_util_encode_set_gif_frame_delay_time(image_util_encode_h handle, unsigned long long delay_time)
338 int err = IMAGE_UTIL_ERROR_NONE;
339 decode_encode_s *_handle = (decode_encode_s *) handle;
340 mm_gif_file_h gif_data = NULL;
342 if (_handle == NULL || _handle->is_decode == TRUE) {
343 image_util_error("Invalid Handle");
344 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
346 if (_handle->image_type != IMAGE_UTIL_GIF) {
347 image_util_error("Wrong image format");
348 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
351 gif_data = (mm_gif_file_h) _handle->image_h;
352 if (gif_data == NULL) {
353 image_util_error("Invalid gif data");
354 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
357 image_util_retvm_if((delay_time > INT_MAX), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid delay time");
359 mm_gif_image_h frame = NULL;
360 err = _image_util_encode_get_gif_frame(gif_data, _handle->current_delay_count, &frame);
361 if (err != IMAGE_UTIL_ERROR_NONE) {
362 image_util_error("_image_util_encode_get_gif_frame is failed %d", err);
365 err = mm_util_gif_image_set_delay_time(frame, (int)delay_time);
366 if (err != MM_UTIL_ERROR_NONE) {
367 image_util_error("mm_util_gif_image_set_delay_time is failed %d", err);
368 return _image_error_capi(ERR_TYPE_ENCODE, err);
370 _handle->current_delay_count++;
375 int image_util_encode_set_input_buffer(image_util_encode_h handle, const unsigned char *src_buffer)
377 int err = IMAGE_UTIL_ERROR_NONE;
378 decode_encode_s *_handle = (decode_encode_s *) handle;
380 if (_handle == NULL || _handle->is_decode == TRUE) {
381 image_util_error("Invalid Handle");
382 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
384 if (src_buffer == NULL) {
385 image_util_error("Invalid input buffer");
386 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
389 /* initialize buffer and value for source buffer */
390 if (_handle->image_type == IMAGE_UTIL_GIF) {
391 mm_gif_file_h gif_data = (mm_gif_file_h) _handle->image_h;
392 mm_gif_image_h frame = NULL;
393 if (gif_data == NULL) {
394 image_util_error("Invalid gif data");
395 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
397 err = _image_util_encode_get_gif_frame(gif_data, _handle->current_buffer_count, &frame);
398 if (err != IMAGE_UTIL_ERROR_NONE) {
399 image_util_error("_image_util_encode_get_gif_frame is failed %d", err);
402 err = mm_util_gif_image_set_image(frame, src_buffer);
403 if (err != MM_UTIL_ERROR_NONE) {
404 image_util_error("mm_util_gif_image_set_delay_time is failed %d", err);
405 return _image_error_capi(ERR_TYPE_ENCODE, err);
407 _handle->current_buffer_count++;
409 if (_handle->src_buffer == NULL)
410 _handle->src_buffer = (void *)calloc(1, sizeof(void *));
411 image_util_retvm_if(_handle->src_buffer == NULL, IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "calloc fail");
413 _handle->src_buffer[_handle->current_buffer_count] = (void *)src_buffer;
419 int image_util_encode_set_output_path(image_util_encode_h handle, const char *path)
421 int err = IMAGE_UTIL_ERROR_NONE;
422 decode_encode_s *_handle = (decode_encode_s *) handle;
424 if (_handle == NULL || _handle->is_decode == TRUE) {
425 image_util_error("Invalid Handle");
426 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
428 image_util_retvm_if((path == NULL || strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
430 if (_handle->dst_buffer)
431 _handle->dst_buffer = NULL;
433 IMAGE_UTIL_SAFE_FREE(_handle->path);
435 _handle->path = g_strndup(path, strlen(path));
440 int image_util_encode_set_output_buffer(image_util_encode_h handle, unsigned char **dst_buffer)
442 int err = IMAGE_UTIL_ERROR_NONE;
443 decode_encode_s *_handle = (decode_encode_s *) handle;
445 if (_handle == NULL || _handle->is_decode == TRUE) {
446 image_util_error("Invalid Handle");
447 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
449 if (dst_buffer == NULL) {
450 image_util_error("Invalid output buffer");
451 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
454 IMAGE_UTIL_SAFE_FREE(_handle->path);
456 _handle->dst_buffer = (void **)dst_buffer;
461 static int _image_util_encode_internal(decode_encode_s * _handle)
463 int err = MM_UTIL_ERROR_NONE;
465 switch (_handle->image_type) {
466 case IMAGE_UTIL_JPEG:
469 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);
471 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);
476 mm_util_png_data *png_data;
478 png_data = (mm_util_png_data *) _handle->image_h;
479 if (png_data == NULL) {
480 image_util_error("Invalid png data");
481 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
485 err = mm_util_encode_to_png_file(&(_handle->src_buffer[0]), png_data, _handle->path);
487 err = mm_util_encode_to_png_memory(&(_handle->src_buffer[0]), png_data);
489 if (err == MM_UTIL_ERROR_NONE) {
490 if (_handle->dst_buffer)
491 *(_handle->dst_buffer) = png_data->data;
492 _handle->dst_size = png_data->size;
493 _handle->width = png_data->width;
494 _handle->height = png_data->height;
500 mm_gif_file_h gif_data = (mm_gif_file_h)_handle->image_h;
501 unsigned long encoded_buffer_size = 0;
502 if (gif_data == NULL) {
503 image_util_error("Invalid gif data");
504 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
506 image_util_debug("[Count] buffer:%d, resolution:%d, delay:%d", _handle->current_buffer_count, _handle->current_resolution_count, _handle->current_delay_count);
507 if ((_handle->current_buffer_count > 1) && ((_handle->current_buffer_count != _handle->current_resolution_count) || (_handle->current_buffer_count != _handle->current_delay_count))) {
508 image_util_error("Total frame count does not match with the data set, for animated gif encoding");
509 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
510 } else if ((_handle->current_buffer_count > 0) && ((_handle->current_buffer_count != _handle->current_resolution_count))) {
511 image_util_error("Total frame count does not match with the data set, for gif encoding");
512 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
515 err = mm_util_gif_encode_set_file(gif_data, _handle->path);
517 err = mm_util_gif_encode_set_mem(gif_data, _handle->dst_buffer, &encoded_buffer_size);
518 if (err != MM_UTIL_ERROR_NONE) {
519 image_util_error("mm_util_gif_encode_set_file | mm_util_gif_encode_set_mem is failed(%d)", err);
520 return _image_error_capi(ERR_TYPE_ENCODE, err);
522 err = mm_util_gif_encode(gif_data);
523 if (err != MM_UTIL_ERROR_NONE) {
524 image_util_error("mm_util_gif_encode is failed(%d)", err);
525 return _image_error_capi(ERR_TYPE_ENCODE, err);
527 if (encoded_buffer_size != 0)
528 _handle->dst_size = (unsigned long long)encoded_buffer_size;
533 mm_util_bmp_data *bmp_data;
535 bmp_data = (mm_util_bmp_data *) _handle->image_h;
536 if (bmp_data == NULL) {
537 image_util_error("Invalid bmp data");
538 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
541 bmp_data->data = _handle->src_buffer[0];
543 err = mm_util_encode_bmp_to_file(bmp_data, _handle->path);
546 err = mm_util_encode_bmp_to_memory(bmp_data, &(bmp_data->data), &size);
547 if (err == MM_UTIL_ERROR_NONE)
548 bmp_data->size = (unsigned long long)size;
552 if (err == MM_UTIL_ERROR_NONE) {
553 if (_handle->dst_buffer)
554 *(_handle->dst_buffer) = bmp_data->data;
555 _handle->dst_size = bmp_data->size;
556 _handle->width = bmp_data->width;
557 _handle->height = bmp_data->height;
562 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
566 return _image_error_capi(ERR_TYPE_ENCODE, err);
569 int image_util_encode_run(image_util_encode_h handle, unsigned long long *size)
571 int err = IMAGE_UTIL_ERROR_NONE;
572 decode_encode_s *_handle = (decode_encode_s *) handle;
574 if (_handle == NULL || _handle->is_decode == TRUE) {
575 image_util_error("Invalid Handle");
576 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
578 if (_handle->path == NULL && _handle->dst_buffer == NULL) {
579 image_util_error("Invalid output");
580 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
582 if (_handle->image_type != IMAGE_UTIL_GIF && _handle->src_buffer == NULL) {
583 image_util_error("Invalid input");
584 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
586 image_util_retvm_if((_image_util_check_resolution(_handle->width, _handle->height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
588 err = _image_util_encode_internal(_handle);
589 _handle->is_encoded = TRUE;
591 if (err != IMAGE_UTIL_ERROR_NONE) {
592 image_util_error("Error - encode run");
597 *size = _handle->dst_size;
602 gpointer _image_util_encode_thread(gpointer data)
604 decode_encode_s *_handle = (decode_encode_s *) data;
605 int err = IMAGE_UTIL_ERROR_NONE;
608 image_util_error("[ERROR] - handle");
612 err = _image_util_encode_internal(_handle);
613 if (err == IMAGE_UTIL_ERROR_NONE)
614 image_util_debug("Success - encode_internal");
616 image_util_error("Error - encode_internal");
618 if (_handle->_encode_cb) {
619 image_util_debug("completed_cb");
620 _handle->_encode_cb->image_encode_completed_cb(err, _handle->_encode_cb->user_data, _handle->dst_size);
623 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
624 _handle->thread = NULL;
625 _handle->is_encoded = TRUE;
626 image_util_debug("exit thread");
631 static int _image_util_encode_create_thread(decode_encode_s * handle)
633 image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
634 image_util_retvm_if((handle->thread != NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "The thread is alread running");
637 handle->thread = g_thread_new("encode_thread", (GThreadFunc) _image_util_encode_thread, (gpointer) handle);
638 if (!handle->thread) {
639 image_util_error("ERROR - create thread");
641 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
644 return IMAGE_UTIL_ERROR_NONE;
647 int image_util_encode_run_async(image_util_encode_h handle, image_util_encode_completed_cb completed_cb, void *user_data)
649 int err = IMAGE_UTIL_ERROR_NONE;
650 decode_encode_s *_handle = (decode_encode_s *) handle;
652 if (_handle == NULL || _handle->is_decode == TRUE) {
653 image_util_error("Invalid Handle");
654 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
656 if (_handle->path == NULL && _handle->dst_buffer == NULL) {
657 image_util_error("Invalid output");
658 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
660 if (_handle->image_type != IMAGE_UTIL_GIF && _handle->src_buffer == NULL) {
661 image_util_error("Invalid input");
662 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
664 image_util_retvm_if((_image_util_check_resolution(_handle->width, _handle->height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
665 image_util_retvm_if((completed_cb == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid callback");
666 image_util_retvm_if((_handle->thread != NULL), IMAGE_UTIL_ERROR_INVALID_OPERATION, "The thread is alread running");
668 if (_handle->_encode_cb != NULL) {
669 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
670 _handle->_encode_cb = NULL;
672 _handle->_encode_cb = (encode_cb_s *) calloc(1, sizeof(encode_cb_s));
673 image_util_retvm_if((_handle->_encode_cb == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Out of memory");
675 _handle->_encode_cb->user_data = user_data;
676 _handle->_encode_cb->image_encode_completed_cb = completed_cb;
678 err = _image_util_encode_create_thread(_handle);
679 if (err != IMAGE_UTIL_ERROR_NONE) {
680 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
681 _handle->_encode_cb = NULL;
687 int image_util_encode_destroy(image_util_encode_h handle)
689 int err = IMAGE_UTIL_ERROR_NONE;
690 decode_encode_s *_handle = (decode_encode_s *) handle;
692 image_util_debug("image_util_encode_destroy");
694 if (_handle == NULL || _handle->is_decode == TRUE) {
695 image_util_error("Invalid Handle");
696 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
699 _image_util_encode_destroy_image_handle(_handle);
701 /* g_thread_exit(handle->thread); */
702 if (_handle->thread) {
703 g_thread_join(_handle->thread);
704 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
707 IMAGE_UTIL_SAFE_FREE(_handle->path);
708 IMAGE_UTIL_SAFE_FREE(_handle->src_buffer);
709 IMAGE_UTIL_SAFE_FREE(_handle);
714 int image_util_encode_jpeg(const unsigned char *buffer, int width, int height, image_util_colorspace_e colorspace, int quality, const char *path)
716 int err = MM_UTIL_ERROR_NONE;
718 DEPRECATION_LOGW("image_util_encode_jpeg()", "image_util_encode_create()");
720 image_util_retvm_if((path == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "path is null");
721 image_util_retvm_if((buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "buffer is null");
722 image_util_retvm_if((strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
723 image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
724 image_util_retvm_if((is_supported_colorspace(colorspace, IMAGE_UTIL_JPEG) == FALSE), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
726 err = mm_util_jpeg_encode_to_file(path, (void *)buffer, width, height, TYPECAST_COLOR_BY_TYPE(colorspace, IMAGE_UTIL_JPEG), quality);
727 return _image_error_capi(ERR_TYPE_ENCODE, err);
730 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)
732 int err = MM_UTIL_ERROR_NONE;
734 DEPRECATION_LOGW("image_util_encode_jpeg_to_memory()", "image_util_encode_create()");
736 image_util_retvm_if((jpeg_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "jpeg_buffer is null");
737 image_util_retvm_if((image_buffer == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "image_buffer is null");
738 image_util_retvm_if((jpeg_size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "jpeg_size is null");
739 image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
740 image_util_retvm_if((is_supported_colorspace(colorspace, IMAGE_UTIL_JPEG) == FALSE), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format");
742 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);
743 return _image_error_capi(ERR_TYPE_ENCODE, err);