2029a0dc17677ed84ecb2372440c828e91c62f19
[platform/core/api/image-util.git] / src / image_util_encode.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
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>
22
23 #include <image_util.h>
24 #include <image_util_private.h>
25
26 static int _image_util_encode_get_gif_frame(mm_gif_file_h gif_data, unsigned int index, mm_gif_image_h *frame)
27 {
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");
31
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);
36         }
37         if (*frame == NULL) {
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);
42                 }
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);
48                 }
49         }
50         return err;
51 }
52
53 static void _image_util_encode_destroy_image_handle(decode_encode_s * handle)
54 {
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");
58
59         if (handle->image_type == IMAGE_UTIL_GIF)
60                 mm_util_gif_encode_destroy(image_handle);
61
62         IMAGE_UTIL_SAFE_FREE(image_handle);
63 }
64
65 static int _image_util_encode_create_jpeg_handle(decode_encode_s * handle)
66 {
67         int err = MM_UTIL_ERROR_NONE;
68
69         image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
70
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);
73
74         handle->image_h = (mm_util_imgp_h) _handle;
75         handle->colorspace = IMAGE_UTIL_COLORSPACE_RGBA8888;
76         handle->quality = 75;
77
78         return err;
79 }
80
81 static int _image_util_encode_create_png_handle(decode_encode_s * handle)
82 {
83         int err = MM_UTIL_ERROR_NONE;
84
85         image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
86
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);
89
90         mm_util_init_encode_png(_handle);
91
92         handle->image_h = (mm_util_imgp_h) _handle;
93
94         return err;
95 }
96
97 static int _image_util_encode_create_gif_handle(decode_encode_s * handle)
98 {
99         int err = MM_UTIL_ERROR_NONE;
100         mm_gif_file_h _handle = NULL;
101
102         image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
103
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);
106
107         handle->image_h = (mm_util_imgp_h) _handle;
108
109         return err;
110 }
111
112 static int _image_util_encode_create_bmp_handle(decode_encode_s * handle)
113 {
114         int err = MM_UTIL_ERROR_NONE;
115
116         image_util_retvm_if((handle == NULL), MM_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
117
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);
120
121         handle->image_h = (mm_util_imgp_h) _handle;
122
123         return err;
124 }
125
126 int image_util_encode_create(image_util_type_e image_type, image_util_encode_h * handle)
127 {
128         int err = MM_UTIL_ERROR_NONE;
129
130         image_util_debug("image_util_encode_create");
131
132         image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
133
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);
136
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;
147
148         switch (image_type) {
149         case IMAGE_UTIL_JPEG:
150                 err = _image_util_encode_create_jpeg_handle(_handle);
151                 break;
152         case IMAGE_UTIL_PNG:
153                 err = _image_util_encode_create_png_handle(_handle);
154                 break;
155         case IMAGE_UTIL_GIF:
156                 err = _image_util_encode_create_gif_handle(_handle);
157                 break;
158         case IMAGE_UTIL_BMP:
159                 err = _image_util_encode_create_bmp_handle(_handle);
160                 break;
161         default:
162                 err = MM_UTIL_ERROR_INVALID_PARAMETER;
163                 break;
164         }
165
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);
170         }
171
172         *handle = (image_util_encode_h) _handle;
173
174         return _convert_image_util_error_code(__func__, err);
175 }
176
177 int image_util_encode_set_resolution(image_util_encode_h handle, unsigned long width, unsigned long height)
178 {
179         int err = IMAGE_UTIL_ERROR_NONE;
180         decode_encode_s *_handle = (decode_encode_s *) handle;
181
182         if (_handle == NULL || _handle->is_decode == TRUE) {
183                 image_util_error("Invalid Handle");
184                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
185         }
186         image_util_retvm_if((_image_util_check_resolution(width, height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
187
188         switch (_handle->image_type) {
189         case IMAGE_UTIL_JPEG:
190                 {
191                         mm_util_jpeg_yuv_data *jpeg_data;
192
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;
197                         }
198                         jpeg_data->width = width;
199                         jpeg_data->height = height;
200                 }
201                 break;
202         case IMAGE_UTIL_PNG:
203                 {
204                         mm_util_png_data *png_data;
205
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;
210                         }
211                         mm_util_png_encode_set_width(png_data, width);
212                         mm_util_png_encode_set_height(png_data, height);
213                 }
214                 break;
215         case IMAGE_UTIL_GIF:
216                 {
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;
221                         }
222
223                         image_util_retvm_if((width > INT_MAX) || (height > INT_MAX), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
224
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);
229                         }
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);
235                         }
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);
240                         }
241                         _handle->current_resolution_count++;
242
243                         return err;
244                 }
245                 break;
246         case IMAGE_UTIL_BMP:
247                 {
248                         mm_util_bmp_data *bmp_data;
249
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;
254                         }
255                         mm_util_bmp_encode_set_width(bmp_data, width);
256                         mm_util_bmp_encode_set_height(bmp_data, height);
257                 }
258                 break;
259         default:
260                 err = IMAGE_UTIL_ERROR_INVALID_PARAMETER;
261                 break;
262         }
263
264         _handle->width = width;
265         _handle->height = height;
266
267         return err;
268 }
269
270 int image_util_encode_set_colorspace(image_util_encode_h handle, image_util_colorspace_e colorspace)
271 {
272         int err = IMAGE_UTIL_ERROR_NONE;
273         decode_encode_s *_handle = (decode_encode_s *) handle;
274
275         if (_handle == NULL || _handle->is_decode == TRUE) {
276                 image_util_error("Invalid Handle");
277                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
278         }
279
280         image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
281
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");
285         } else {
286                 image_util_error("Invalid image type");
287                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
288         }
289
290         _handle->colorspace = colorspace;
291
292         return err;
293 }
294
295 int image_util_encode_set_quality(image_util_encode_h handle, int quality)
296 {
297         int err = IMAGE_UTIL_ERROR_NONE;
298         decode_encode_s *_handle = (decode_encode_s *) handle;
299
300         if (_handle == NULL || _handle->is_decode == TRUE) {
301                 image_util_error("Invalid Handle");
302                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
303         }
304         if (_handle->image_type != IMAGE_UTIL_JPEG) {
305                 image_util_error("Wrong image format");
306                 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
307         }
308         image_util_retvm_if((quality <= 0 || quality > 100), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid quality");
309
310         _handle->quality = quality;
311
312         return err;
313 }
314
315 int image_util_encode_set_png_compression(image_util_encode_h handle, image_util_png_compression_e compression)
316 {
317         int err = IMAGE_UTIL_ERROR_NONE;
318         decode_encode_s *_handle = (decode_encode_s *) handle;
319         mm_util_png_data *png_data;
320
321         if (_handle == NULL || _handle->is_decode == TRUE) {
322                 image_util_error("Invalid Handle");
323                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
324         }
325         if (_handle->image_type != IMAGE_UTIL_PNG) {
326                 image_util_error("Wrong image format");
327                 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
328         }
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");
330
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;
335         }
336
337         mm_util_png_encode_set_compression_level(png_data, compression);
338
339         return err;
340 }
341
342 int image_util_encode_set_gif_frame_delay_time(image_util_encode_h handle, unsigned long long delay_time)
343 {
344         int err = IMAGE_UTIL_ERROR_NONE;
345         decode_encode_s *_handle = (decode_encode_s *) handle;
346         mm_gif_file_h gif_data = NULL;
347
348         if (_handle == NULL || _handle->is_decode == TRUE) {
349                 image_util_error("Invalid Handle");
350                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
351         }
352         if (_handle->image_type != IMAGE_UTIL_GIF) {
353                 image_util_error("Wrong image format");
354                 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
355         }
356
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;
361         }
362
363         image_util_retvm_if((delay_time > INT_MAX), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid delay time");
364
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);
370         }
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);
375         }
376         _handle->current_delay_count++;
377
378         return err;
379 }
380
381 int image_util_encode_set_input_buffer(image_util_encode_h handle, const unsigned char *src_buffer)
382 {
383         int err = IMAGE_UTIL_ERROR_NONE;
384         decode_encode_s *_handle = (decode_encode_s *) handle;
385
386         if (_handle == NULL || _handle->is_decode == TRUE) {
387                 image_util_error("Invalid Handle");
388                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
389         }
390         if (src_buffer == NULL) {
391                 image_util_error("Invalid input buffer");
392                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
393         }
394
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;
402                 }
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);
407                 }
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);
412                 }
413                 _handle->current_buffer_count++;
414         } else {
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");
418
419                 _handle->src_buffer[_handle->current_buffer_count] = (void *)src_buffer;
420         }
421
422         return err;
423 }
424
425 int image_util_encode_set_output_path(image_util_encode_h handle, const char *path)
426 {
427         int err = IMAGE_UTIL_ERROR_NONE;
428         decode_encode_s *_handle = (decode_encode_s *) handle;
429
430         if (_handle == NULL || _handle->is_decode == TRUE) {
431                 image_util_error("Invalid Handle");
432                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
433         }
434         image_util_retvm_if((path == NULL || strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
435
436         if (_handle->dst_buffer)
437                 _handle->dst_buffer = NULL;
438
439         IMAGE_UTIL_SAFE_FREE(_handle->path);
440
441         _handle->path = g_strndup(path, strlen(path));
442
443         return err;
444 }
445
446 int image_util_encode_set_output_buffer(image_util_encode_h handle, unsigned char **dst_buffer)
447 {
448         int err = IMAGE_UTIL_ERROR_NONE;
449         decode_encode_s *_handle = (decode_encode_s *) handle;
450
451         if (_handle == NULL || _handle->is_decode == TRUE) {
452                 image_util_error("Invalid Handle");
453                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
454         }
455         if (dst_buffer == NULL) {
456                 image_util_error("Invalid output buffer");
457                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
458         }
459
460         IMAGE_UTIL_SAFE_FREE(_handle->path);
461
462         _handle->dst_buffer = (void **)dst_buffer;
463
464         return err;
465 }
466
467 static int _image_util_encode_internal(decode_encode_s * _handle)
468 {
469         int err = MM_UTIL_ERROR_NONE;
470
471         switch (_handle->image_type) {
472         case IMAGE_UTIL_JPEG:
473                 {
474                         if (_handle->path)
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);
476                         else
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);
478                 }
479                 break;
480         case IMAGE_UTIL_PNG:
481                 {
482                         mm_util_png_data *png_data;
483
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;
488                         }
489
490                         if (_handle->path)
491                                 err = mm_util_encode_to_png_file(&(_handle->src_buffer[0]), png_data, _handle->path);
492                         else
493                                 err = mm_util_encode_to_png_memory(&(_handle->src_buffer[0]), png_data);
494
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;
501                         }
502                 }
503                 break;
504         case IMAGE_UTIL_GIF:
505                 {
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;
511                         }
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;
519                         }
520                         if (_handle->path)
521                                 err = mm_util_gif_encode_set_file(gif_data, _handle->path);
522                         else
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);
527                         }
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);
532                         }
533                         if (encoded_buffer_size != 0)
534                                 _handle->dst_size = (unsigned long long)encoded_buffer_size;
535                 }
536                 break;
537         case IMAGE_UTIL_BMP:
538                 {
539                         mm_util_bmp_data *bmp_data;
540
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;
545                         }
546
547                         bmp_data->data = _handle->src_buffer[0];
548                         if (_handle->path) {
549                                 err = mm_util_encode_bmp_to_file(bmp_data, _handle->path);
550                         } else {
551                                 size_t size = 0;
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;
555                                 else
556                                         bmp_data->size = 0;
557                         }
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;
564                         }
565                 }
566                 break;
567         default:
568                 err = MM_UTIL_ERROR_INVALID_PARAMETER;
569                 break;
570         }
571
572         return err;
573 }
574
575 int image_util_encode_run(image_util_encode_h handle, unsigned long long *size)
576 {
577         int err = MM_UTIL_ERROR_NONE;
578         decode_encode_s *_handle = (decode_encode_s *) handle;
579
580         if (_handle == NULL || _handle->is_decode == TRUE) {
581                 image_util_error("Invalid Handle");
582                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
583         }
584         if (_handle->path == NULL && _handle->dst_buffer == NULL) {
585                 image_util_error("Invalid output");
586                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
587         }
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;
591         }
592         image_util_retvm_if((_image_util_check_resolution(_handle->width, _handle->height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
593
594         err = _image_util_encode_internal(_handle);
595         _handle->is_encoded = TRUE;
596
597         if (err != MM_UTIL_ERROR_NONE) {
598                 image_util_error("Error - encode run");
599                 return _convert_image_util_error_code(__func__, err);
600         }
601
602         if (size)
603                 *size = _handle->dst_size;
604
605         return _convert_image_util_error_code(__func__, err);
606 }
607
608 gpointer _image_util_encode_thread(gpointer data)
609 {
610         decode_encode_s *_handle = (decode_encode_s *) data;
611         int err = MM_UTIL_ERROR_NONE;
612
613         if (!_handle) {
614                 image_util_error("[ERROR] - handle");
615                 return NULL;
616         }
617
618         err = _image_util_encode_internal(_handle);
619         if (err == MM_UTIL_ERROR_NONE)
620                 image_util_debug("Success - encode_internal");
621         else
622                 image_util_error("Error - encode_internal");
623
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);
627         }
628
629         IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
630         _handle->thread = NULL;
631         _handle->is_encoded = TRUE;
632         image_util_debug("exit thread");
633
634         return NULL;
635 }
636
637 static int _image_util_encode_create_thread(decode_encode_s * handle)
638 {
639         int ret = MM_UTIL_ERROR_NONE;
640
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");
643
644         /*create threads */
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");
648
649                 return MM_UTIL_ERROR_INVALID_OPERATION;
650         }
651
652         return ret;
653 }
654
655 int image_util_encode_run_async(image_util_encode_h handle, image_util_encode_completed_cb completed_cb, void *user_data)
656 {
657         int err = MM_UTIL_ERROR_NONE;
658         decode_encode_s *_handle = (decode_encode_s *) handle;
659
660         if (_handle == NULL || _handle->is_decode == TRUE) {
661                 image_util_error("Invalid Handle");
662                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
663         }
664         if (_handle->path == NULL && _handle->dst_buffer == NULL) {
665                 image_util_error("Invalid output");
666                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
667         }
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;
671         }
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");
675
676         if (_handle->_encode_cb != NULL) {
677                 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
678                 _handle->_encode_cb = NULL;
679         }
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");
682
683         _handle->_encode_cb->user_data = user_data;
684         _handle->_encode_cb->image_encode_completed_cb = completed_cb;
685
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;
690         }
691
692         return _convert_image_util_error_code(__func__, err);
693 }
694
695 int image_util_encode_destroy(image_util_encode_h handle)
696 {
697         int err = IMAGE_UTIL_ERROR_NONE;
698         decode_encode_s *_handle = (decode_encode_s *) handle;
699
700         image_util_debug("image_util_encode_destroy");
701
702         if (_handle == NULL || _handle->is_decode == TRUE) {
703                 image_util_error("Invalid Handle");
704                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
705         }
706
707         _image_util_encode_destroy_image_handle(_handle);
708
709         /* g_thread_exit(handle->thread); */
710         if (_handle->thread) {
711                 g_thread_join(_handle->thread);
712                 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
713         }
714
715         IMAGE_UTIL_SAFE_FREE(_handle->path);
716         IMAGE_UTIL_SAFE_FREE(_handle->src_buffer);
717         IMAGE_UTIL_SAFE_FREE(_handle);
718
719         return err;
720 }
721
722 int image_util_encode_jpeg(const unsigned char *buffer, int width, int height, image_util_colorspace_e colorspace, int quality, const char *path)
723 {
724         int err = MM_UTIL_ERROR_NONE;
725
726         DEPRECATION_LOGW("image_util_encode_jpeg()", "image_util_encode_create()");
727
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");
733
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);
736 }
737
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)
739 {
740         int err = MM_UTIL_ERROR_NONE;
741
742         DEPRECATION_LOGW("image_util_encode_jpeg_to_memory()", "image_util_encode_create()");
743
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");
749
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);
752 }
753