Modify converting to capi error and simplify error type use
[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), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
30         image_util_retvm_if((frame == NULL), IMAGE_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 _image_error_capi(ERR_TYPE_ENCODE, 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 _image_error_capi(ERR_TYPE_ENCODE, 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 _image_error_capi(ERR_TYPE_ENCODE, err);
48                 }
49         }
50         return _image_error_capi(ERR_TYPE_ENCODE, 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         image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
68
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");
71
72         handle->image_h = (mm_util_imgp_h) _handle;
73         handle->colorspace = IMAGE_UTIL_COLORSPACE_RGBA8888;
74         handle->quality = 75;
75
76         return IMAGE_UTIL_ERROR_NONE;
77 }
78
79 static int _image_util_encode_create_png_handle(decode_encode_s * handle)
80 {
81         image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
82
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");
85
86         mm_util_init_encode_png(_handle);
87
88         handle->image_h = (mm_util_imgp_h) _handle;
89
90         return IMAGE_UTIL_ERROR_NONE;
91 }
92
93 static int _image_util_encode_create_gif_handle(decode_encode_s * handle)
94 {
95         int err = MM_UTIL_ERROR_NONE;
96         mm_gif_file_h _handle = NULL;
97
98         image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
99
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);
102
103         handle->image_h = (mm_util_imgp_h) _handle;
104
105         return IMAGE_UTIL_ERROR_NONE;
106 }
107
108 static int _image_util_encode_create_bmp_handle(decode_encode_s * handle)
109 {
110         image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
111
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");
114
115         handle->image_h = (mm_util_imgp_h) _handle;
116
117         return IMAGE_UTIL_ERROR_NONE;
118 }
119
120 int image_util_encode_create(image_util_type_e image_type, image_util_encode_h * handle)
121 {
122         int err = IMAGE_UTIL_ERROR_NONE;
123
124         image_util_debug("image_util_encode_create");
125
126         image_util_retvm_if((handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle");
127
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);
130
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;
141
142         switch (image_type) {
143         case IMAGE_UTIL_JPEG:
144                 err = _image_util_encode_create_jpeg_handle(_handle);
145                 break;
146         case IMAGE_UTIL_PNG:
147                 err = _image_util_encode_create_png_handle(_handle);
148                 break;
149         case IMAGE_UTIL_GIF:
150                 err = _image_util_encode_create_gif_handle(_handle);
151                 break;
152         case IMAGE_UTIL_BMP:
153                 err = _image_util_encode_create_bmp_handle(_handle);
154                 break;
155         default:
156                 err = IMAGE_UTIL_ERROR_INVALID_PARAMETER;
157                 break;
158         }
159
160         if (err != IMAGE_UTIL_ERROR_NONE) {
161                 image_util_error("Error - create image handle");
162                 IMAGE_UTIL_SAFE_FREE(_handle);
163                 return err;
164         }
165
166         *handle = (image_util_encode_h) _handle;
167
168         return err;
169 }
170
171 int image_util_encode_set_resolution(image_util_encode_h handle, unsigned long width, unsigned long height)
172 {
173         int err = IMAGE_UTIL_ERROR_NONE;
174         decode_encode_s *_handle = (decode_encode_s *) handle;
175
176         if (_handle == NULL || _handle->is_decode == TRUE) {
177                 image_util_error("Invalid Handle");
178                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
179         }
180         image_util_retvm_if((_image_util_check_resolution(width, height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
181
182         switch (_handle->image_type) {
183         case IMAGE_UTIL_JPEG:
184                 {
185                         mm_util_jpeg_yuv_data *jpeg_data;
186
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;
191                         }
192                         jpeg_data->width = width;
193                         jpeg_data->height = height;
194                 }
195                 break;
196         case IMAGE_UTIL_PNG:
197                 {
198                         mm_util_png_data *png_data;
199
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;
204                         }
205                         mm_util_png_encode_set_width(png_data, width);
206                         mm_util_png_encode_set_height(png_data, height);
207                 }
208                 break;
209         case IMAGE_UTIL_GIF:
210                 {
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;
215                         }
216
217                         image_util_retvm_if((width > INT_MAX) || (height > INT_MAX), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
218
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);
223                         }
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);
228                                 return err;
229                         }
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);
234                         }
235                         _handle->current_resolution_count++;
236
237                         return err;
238                 }
239                 break;
240         case IMAGE_UTIL_BMP:
241                 {
242                         mm_util_bmp_data *bmp_data;
243
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;
248                         }
249                         mm_util_bmp_encode_set_width(bmp_data, width);
250                         mm_util_bmp_encode_set_height(bmp_data, height);
251                 }
252                 break;
253         default:
254                 err = IMAGE_UTIL_ERROR_INVALID_PARAMETER;
255                 break;
256         }
257
258         _handle->width = width;
259         _handle->height = height;
260
261         return err;
262 }
263
264 int image_util_encode_set_colorspace(image_util_encode_h handle, image_util_colorspace_e colorspace)
265 {
266         int err = IMAGE_UTIL_ERROR_NONE;
267         decode_encode_s *_handle = (decode_encode_s *) handle;
268
269         if (_handle == NULL || _handle->is_decode == TRUE) {
270                 image_util_error("Invalid Handle");
271                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
272         }
273
274         image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace");
275
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");
279         } else {
280                 image_util_error("Invalid image type");
281                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
282         }
283
284         _handle->colorspace = colorspace;
285
286         return err;
287 }
288
289 int image_util_encode_set_quality(image_util_encode_h handle, int quality)
290 {
291         int err = IMAGE_UTIL_ERROR_NONE;
292         decode_encode_s *_handle = (decode_encode_s *) handle;
293
294         if (_handle == NULL || _handle->is_decode == TRUE) {
295                 image_util_error("Invalid Handle");
296                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
297         }
298         if (_handle->image_type != IMAGE_UTIL_JPEG) {
299                 image_util_error("Wrong image format");
300                 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
301         }
302         image_util_retvm_if((quality <= 0 || quality > 100), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid quality");
303
304         _handle->quality = quality;
305
306         return err;
307 }
308
309 int image_util_encode_set_png_compression(image_util_encode_h handle, image_util_png_compression_e compression)
310 {
311         int err = IMAGE_UTIL_ERROR_NONE;
312         decode_encode_s *_handle = (decode_encode_s *) handle;
313         mm_util_png_data *png_data;
314
315         if (_handle == NULL || _handle->is_decode == TRUE) {
316                 image_util_error("Invalid Handle");
317                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
318         }
319         if (_handle->image_type != IMAGE_UTIL_PNG) {
320                 image_util_error("Wrong image format");
321                 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
322         }
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");
324
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;
329         }
330
331         mm_util_png_encode_set_compression_level(png_data, compression);
332
333         return err;
334 }
335
336 int image_util_encode_set_gif_frame_delay_time(image_util_encode_h handle, unsigned long long delay_time)
337 {
338         int err = IMAGE_UTIL_ERROR_NONE;
339         decode_encode_s *_handle = (decode_encode_s *) handle;
340         mm_gif_file_h gif_data = NULL;
341
342         if (_handle == NULL || _handle->is_decode == TRUE) {
343                 image_util_error("Invalid Handle");
344                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
345         }
346         if (_handle->image_type != IMAGE_UTIL_GIF) {
347                 image_util_error("Wrong image format");
348                 return IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
349         }
350
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;
355         }
356
357         image_util_retvm_if((delay_time > INT_MAX), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid delay time");
358
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);
363                 return err;
364         }
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);
369         }
370         _handle->current_delay_count++;
371
372         return err;
373 }
374
375 int image_util_encode_set_input_buffer(image_util_encode_h handle, const unsigned char *src_buffer)
376 {
377         int err = IMAGE_UTIL_ERROR_NONE;
378         decode_encode_s *_handle = (decode_encode_s *) handle;
379
380         if (_handle == NULL || _handle->is_decode == TRUE) {
381                 image_util_error("Invalid Handle");
382                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
383         }
384         if (src_buffer == NULL) {
385                 image_util_error("Invalid input buffer");
386                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
387         }
388
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;
396                 }
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);
400                         return err;
401                 }
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);
406                 }
407                 _handle->current_buffer_count++;
408         } else {
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");
412
413                 _handle->src_buffer[_handle->current_buffer_count] = (void *)src_buffer;
414         }
415
416         return err;
417 }
418
419 int image_util_encode_set_output_path(image_util_encode_h handle, const char *path)
420 {
421         int err = IMAGE_UTIL_ERROR_NONE;
422         decode_encode_s *_handle = (decode_encode_s *) handle;
423
424         if (_handle == NULL || _handle->is_decode == TRUE) {
425                 image_util_error("Invalid Handle");
426                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
427         }
428         image_util_retvm_if((path == NULL || strlen(path) == 0), IMAGE_UTIL_ERROR_NO_SUCH_FILE, "Invalid path");
429
430         if (_handle->dst_buffer)
431                 _handle->dst_buffer = NULL;
432
433         IMAGE_UTIL_SAFE_FREE(_handle->path);
434
435         _handle->path = g_strndup(path, strlen(path));
436
437         return err;
438 }
439
440 int image_util_encode_set_output_buffer(image_util_encode_h handle, unsigned char **dst_buffer)
441 {
442         int err = IMAGE_UTIL_ERROR_NONE;
443         decode_encode_s *_handle = (decode_encode_s *) handle;
444
445         if (_handle == NULL || _handle->is_decode == TRUE) {
446                 image_util_error("Invalid Handle");
447                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
448         }
449         if (dst_buffer == NULL) {
450                 image_util_error("Invalid output buffer");
451                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
452         }
453
454         IMAGE_UTIL_SAFE_FREE(_handle->path);
455
456         _handle->dst_buffer = (void **)dst_buffer;
457
458         return err;
459 }
460
461 static int _image_util_encode_internal(decode_encode_s * _handle)
462 {
463         int err = MM_UTIL_ERROR_NONE;
464
465         switch (_handle->image_type) {
466         case IMAGE_UTIL_JPEG:
467                 {
468                         if (_handle->path)
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);
470                         else
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);
472                 }
473                 break;
474         case IMAGE_UTIL_PNG:
475                 {
476                         mm_util_png_data *png_data;
477
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;
482                         }
483
484                         if (_handle->path)
485                                 err = mm_util_encode_to_png_file(&(_handle->src_buffer[0]), png_data, _handle->path);
486                         else
487                                 err = mm_util_encode_to_png_memory(&(_handle->src_buffer[0]), png_data);
488
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;
495                         }
496                 }
497                 break;
498         case IMAGE_UTIL_GIF:
499                 {
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;
505                         }
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;
513                         }
514                         if (_handle->path)
515                                 err = mm_util_gif_encode_set_file(gif_data, _handle->path);
516                         else
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);
521                         }
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);
526                         }
527                         if (encoded_buffer_size != 0)
528                                 _handle->dst_size = (unsigned long long)encoded_buffer_size;
529                 }
530                 break;
531         case IMAGE_UTIL_BMP:
532                 {
533                         mm_util_bmp_data *bmp_data;
534
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;
539                         }
540
541                         bmp_data->data = _handle->src_buffer[0];
542                         if (_handle->path) {
543                                 err = mm_util_encode_bmp_to_file(bmp_data, _handle->path);
544                         } else {
545                                 size_t size = 0;
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;
549                                 else
550                                         bmp_data->size = 0;
551                         }
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;
558                         }
559                 }
560                 break;
561         default:
562                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
563                 break;
564         }
565
566         return _image_error_capi(ERR_TYPE_ENCODE, err);
567 }
568
569 int image_util_encode_run(image_util_encode_h handle, unsigned long long *size)
570 {
571         int err = IMAGE_UTIL_ERROR_NONE;
572         decode_encode_s *_handle = (decode_encode_s *) handle;
573
574         if (_handle == NULL || _handle->is_decode == TRUE) {
575                 image_util_error("Invalid Handle");
576                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
577         }
578         if (_handle->path == NULL && _handle->dst_buffer == NULL) {
579                 image_util_error("Invalid output");
580                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
581         }
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;
585         }
586         image_util_retvm_if((_image_util_check_resolution(_handle->width, _handle->height) == false), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid resolution");
587
588         err = _image_util_encode_internal(_handle);
589         _handle->is_encoded = TRUE;
590
591         if (err != IMAGE_UTIL_ERROR_NONE) {
592                 image_util_error("Error - encode run");
593                 return err;
594         }
595
596         if (size)
597                 *size = _handle->dst_size;
598
599         return err;
600 }
601
602 gpointer _image_util_encode_thread(gpointer data)
603 {
604         decode_encode_s *_handle = (decode_encode_s *) data;
605         int err = IMAGE_UTIL_ERROR_NONE;
606
607         if (!_handle) {
608                 image_util_error("[ERROR] - handle");
609                 return NULL;
610         }
611
612         err = _image_util_encode_internal(_handle);
613         if (err == IMAGE_UTIL_ERROR_NONE)
614                 image_util_debug("Success - encode_internal");
615         else
616                 image_util_error("Error - encode_internal");
617
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);
621         }
622
623         IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
624         _handle->thread = NULL;
625         _handle->is_encoded = TRUE;
626         image_util_debug("exit thread");
627
628         return NULL;
629 }
630
631 static int _image_util_encode_create_thread(decode_encode_s * handle)
632 {
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");
635
636         /*create threads */
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");
640
641                 return IMAGE_UTIL_ERROR_INVALID_OPERATION;
642         }
643
644         return IMAGE_UTIL_ERROR_NONE;
645 }
646
647 int image_util_encode_run_async(image_util_encode_h handle, image_util_encode_completed_cb completed_cb, void *user_data)
648 {
649         int err = IMAGE_UTIL_ERROR_NONE;
650         decode_encode_s *_handle = (decode_encode_s *) handle;
651
652         if (_handle == NULL || _handle->is_decode == TRUE) {
653                 image_util_error("Invalid Handle");
654                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
655         }
656         if (_handle->path == NULL && _handle->dst_buffer == NULL) {
657                 image_util_error("Invalid output");
658                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
659         }
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;
663         }
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");
667
668         if (_handle->_encode_cb != NULL) {
669                 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
670                 _handle->_encode_cb = NULL;
671         }
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");
674
675         _handle->_encode_cb->user_data = user_data;
676         _handle->_encode_cb->image_encode_completed_cb = completed_cb;
677
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;
682         }
683
684         return err;
685 }
686
687 int image_util_encode_destroy(image_util_encode_h handle)
688 {
689         int err = IMAGE_UTIL_ERROR_NONE;
690         decode_encode_s *_handle = (decode_encode_s *) handle;
691
692         image_util_debug("image_util_encode_destroy");
693
694         if (_handle == NULL || _handle->is_decode == TRUE) {
695                 image_util_error("Invalid Handle");
696                 return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
697         }
698
699         _image_util_encode_destroy_image_handle(_handle);
700
701         /* g_thread_exit(handle->thread); */
702         if (_handle->thread) {
703                 g_thread_join(_handle->thread);
704                 IMAGE_UTIL_SAFE_FREE(_handle->_encode_cb);
705         }
706
707         IMAGE_UTIL_SAFE_FREE(_handle->path);
708         IMAGE_UTIL_SAFE_FREE(_handle->src_buffer);
709         IMAGE_UTIL_SAFE_FREE(_handle);
710
711         return err;
712 }
713
714 int image_util_encode_jpeg(const unsigned char *buffer, int width, int height, image_util_colorspace_e colorspace, int quality, const char *path)
715 {
716         int err = MM_UTIL_ERROR_NONE;
717
718         DEPRECATION_LOGW("image_util_encode_jpeg()", "image_util_encode_create()");
719
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");
725
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);
728 }
729
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)
731 {
732         int err = MM_UTIL_ERROR_NONE;
733
734         DEPRECATION_LOGW("image_util_encode_jpeg_to_memory()", "image_util_encode_create()");
735
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");
741
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);
744 }
745