7f5df56a059c663d861df1154f876a4595637649
[platform/core/multimedia/libmm-utility.git] / magick / mm_util_magick.c
1 /*
2  * libmm-utility
3  *
4  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: YoungHun Kim <yh8004.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdbool.h>
23 #include <magick/api.h>
24 #include "mm_util_private.h"
25 #include "mm_util_magick.h"
26
27 static bool __mm_util_check_angle(mm_util_magick_rotate_type angle);
28
29 static void __mm_util_magick_log_method(const ExceptionType excep, const char *message)
30 {
31         /* To exclude time, user time and pid */
32         unsigned int start_idx = 31;
33
34         if ((message == NULL) || (strlen(message) < start_idx)) {
35                 start_idx = 0;
36         }
37
38         if (excep >= ErrorException)
39                 mm_util_error("[GM][Ex:%3u] %s", excep, message + start_idx);
40         else if (excep >= WarningException)
41                 mm_util_warn("[GM][Ex:%3u] %s", excep, message + start_idx);
42         else
43                 mm_util_debug("[GM][Ex:%3u] %s", excep, message + start_idx);
44 }
45
46 static void __mm_util_magick_error_handler(const ExceptionType excep, const char *reason, const char *message)
47 {
48         if (excep >= ErrorException)
49                 mm_util_error("[GM][Ex:%3u][Rs:%s] %s", excep, reason, message);
50         else
51                 mm_util_warn("[GM][Ex:%3u][Rs:%s] %s", excep, reason, message);
52 }
53
54 static void __mm_util_init(ExceptionInfo *exception)
55 {
56         InitializeMagick(NULL);
57         if (exception != NULL)
58                 GetExceptionInfo(exception);
59
60         /* set LogEventMask to show like "all" or "warning, error" */
61         /* <LogEventMask List>
62          * "none", "information", "warning", "error", "fatalerror", "configure", "annotate",
63          * "render", "transform", "locale", "coder", "x11", "cache", "blob", "deprecate",
64          * "user", "resource", "temporaryfile", "exception", "option", "all"
65          */
66 #if (GMAGICK_DEBUG == 1)
67         SetLogEventMask("all");
68 #else
69         SetLogEventMask("warning, error, fatalerror, exception");
70 #endif
71         SetLogMethod(__mm_util_magick_log_method);
72         SetErrorHandler(__mm_util_magick_error_handler);
73         SetFatalErrorHandler(__mm_util_magick_error_handler);
74         SetWarningHandler(__mm_util_magick_error_handler);
75
76         return;
77 }
78
79 static void __mm_util_finalize(Image *image_1, Image *image_2, ImageInfo *imageInfo, ExceptionInfo *exception)
80 {
81
82         if (image_1 != NULL)
83                 DestroyImage(image_1);
84
85         if (image_2 != NULL)
86                 DestroyImage(image_2);
87
88         if (imageInfo != NULL)
89                 DestroyImageInfo(imageInfo);
90
91         if (exception != NULL)
92                 DestroyExceptionInfo(exception);
93
94         DestroyMagick();
95
96         return;
97 }
98
99 static int __mm_util_get_map(mm_util_color_format_e format, char **map)
100 {
101         mm_util_retvm_if(map == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid map");
102
103         switch (format) {
104         case MM_UTIL_COLOR_RGB24:
105                 *map = g_strdup("RGB");
106                 break;
107         case MM_UTIL_COLOR_ARGB:
108                 *map = g_strdup("ARGB");
109                 break;
110         case MM_UTIL_COLOR_BGRA:
111                 *map = g_strdup("BGRA");
112                 break;
113         case MM_UTIL_COLOR_RGBA:
114                 *map = g_strdup("RGBA");
115                 break;
116         default:
117                 mm_util_error("Not supported format. [%d]", format);
118                 return MM_UTIL_ERROR_INVALID_PARAMETER;
119         }
120
121         return MM_UTIL_ERROR_NONE;
122 }
123
124 static Image * __mm_util_constitute_image(mm_util_image_h handle, const char *map)
125 {
126         mm_image_info_s *_handle = (mm_image_info_s*)handle;
127         Image *_image = NULL;
128         ExceptionInfo exception;
129
130         mm_util_fenter();
131
132         mm_util_retvm_if(handle == NULL, NULL, "invalid handle");
133         mm_util_retvm_if(!MMUTIL_STRING_VALID(map), NULL, "invalid map");
134
135         GetExceptionInfo(&exception);
136
137         /* Read image from buffer */
138         _image = ConstituteImage(_handle->width, _handle->height, map, CharPixel, _handle->data, &exception);
139         if (_image == NULL) {
140                 mm_util_error("Error: Getting Image failed.");
141                 if (exception.severity != UndefinedException)
142                         CatchException(&exception);
143         }
144
145         DestroyExceptionInfo(&exception);
146
147         mm_util_fleave();
148
149         return _image;
150 }
151
152 static Image * __mm_util_rotate_image(Image *image, mm_util_magick_rotate_type angle)
153 {
154         Image *_processed_image = NULL;
155         ExceptionInfo exception;
156
157         mm_util_fenter();
158
159         mm_util_retvm_if(image == NULL, NULL, "invalid image");
160
161         GetExceptionInfo(&exception);
162
163         if (angle <= MM_UTIL_ROTATE_270) {
164                 _processed_image = RotateImage(image, angle, &exception);
165         } else if (angle == MM_UTIL_ROTATE_FLIP_HORZ) {
166                 _processed_image = FlopImage(image, &exception);
167         } else if (angle == MM_UTIL_ROTATE_FLIP_VERT) {
168                 _processed_image = FlipImage(image, &exception);
169         } else {
170                 mm_util_error("Invalid angle. [%d]", angle);
171         }
172
173         if (_processed_image == NULL) {
174                 mm_util_error("Error: Image processing failed.");
175                 if (exception.severity != UndefinedException)
176                         CatchException(&exception);
177         }
178
179         DestroyExceptionInfo(&exception);
180
181         mm_util_fleave();
182
183         return _processed_image;
184 }
185
186 static Image * __mm_util_resize_image(Image *image, unsigned int width, unsigned int height)
187 {
188         Image *_processed_image = NULL;
189         Image *_sampled_image = NULL;
190         ExceptionInfo exception;
191         unsigned int check_factor = 3;
192         unsigned int sample_factor = 2;
193
194         mm_util_fenter();
195
196         mm_util_retvm_if(image == NULL, NULL, "invalid image");
197
198         GetExceptionInfo(&exception);
199
200         if ((image->columns > width * check_factor) && (image->rows > height * check_factor)) {
201                 _sampled_image = SampleImage(image, width * sample_factor, height * sample_factor, &exception);
202                 if (_sampled_image == (Image *) NULL)
203                         mm_util_error("Error: Sampling Image failed.");
204         }
205
206         if (_sampled_image != NULL)
207                 _processed_image = ScaleImage(_sampled_image, width, height, &exception);
208         else
209                 _processed_image = ScaleImage(image, width, height, &exception);
210
211         if (_processed_image == NULL) {
212                 mm_util_error("Error: Resizing Image failed.");
213                 if (exception.severity != UndefinedException)
214                         CatchException(&exception);
215         }
216
217         if (_sampled_image != NULL)
218                 DestroyImage(_sampled_image);
219
220         DestroyExceptionInfo(&exception);
221
222         mm_util_fleave();
223
224         return _processed_image;
225 }
226
227 static Image * __mm_util_read_image_from_file(const char *path, ImageInfo **image_info)
228 {
229         ImageInfo *_image_info = NULL;
230         Image *_image = NULL;
231         ExceptionInfo exception;
232
233         mm_util_fenter();
234
235         mm_util_retvm_if(!MMUTIL_STRING_VALID(path), NULL, "invalid path");
236         mm_util_retvm_if(image_info == NULL, NULL, "invalid image_info");
237
238         GetExceptionInfo(&exception);
239
240         _image_info = CloneImageInfo(0);
241         g_strlcpy(_image_info->filename, path, sizeof(_image_info->filename));
242
243         AddDefinition(_image_info, "jpeg", "dct-method", "FASTEST", &exception);
244         AddDefinition(_image_info, "jpeg", "block-smoothing", "FALSE", &exception);
245         AddDefinition(_image_info, "jpeg", "fancy-upsampling", "FALSE", &exception);
246
247         _image = ReadImage(_image_info, &exception);
248
249         if (_image == NULL) {
250                 mm_util_error("Error: Reading Image failed.");
251                 if (exception.severity != UndefinedException)
252                         CatchException(&exception);
253
254                 DestroyImageInfo(_image_info);
255         } else {
256                 *image_info = _image_info;
257         }
258
259         DestroyExceptionInfo(&exception);
260
261         mm_util_fleave();
262
263         return _image;
264 }
265
266 static int __mm_util_write_image_to_file(ImageInfo *image_info, Image *image, const char *out_path)
267 {
268         int ret = MM_UTIL_ERROR_NONE;
269         ExceptionInfo exception;
270
271         mm_util_fenter();
272
273         mm_util_retvm_if(image_info == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image_info");
274         mm_util_retvm_if(image == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image");
275         mm_util_retvm_if(!MMUTIL_STRING_VALID(out_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid out_path");
276
277         GetExceptionInfo(&exception);
278
279         SAFE_STRLCPY(image->filename, out_path, sizeof(image->filename));
280         image->filename[MaxTextExtent-1] = '\0';
281
282         AddDefinition(image_info, "jpeg", "dct-method", "FASTEST", &exception);
283         AddDefinition(image_info, "jpeg", "optimize-coding", "FALSE", &exception);
284         //DeleteImageProfile(image, "EXIF");
285         DeleteImageProfile(image, "8BIM");
286         DeleteImageProfile(image, "ICM");
287         DeleteImageProfile(image, "IPTC");
288         DeleteImageProfile(image, "XMP");
289
290         if (WriteImage (image_info, image) == MagickFalse) {
291                 mm_util_error("Error: Writing Image failed.");
292                 if (exception.severity != UndefinedException)
293                         CatchException(&exception);
294
295                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
296         }
297
298         DestroyExceptionInfo(&exception);
299
300         mm_util_fleave();
301
302         return ret;
303 }
304
305 static void * __mm_util_write_image_to_buffer(Image *image, unsigned int width, unsigned int height, const char *map, size_t *pixels_size)
306 {
307         int ret = MM_UTIL_ERROR_NONE;
308         ExceptionInfo exception;
309         size_t _pixels_size = 0;
310         void *pixels = NULL;
311
312         mm_util_fenter();
313
314         mm_util_retvm_if(image == NULL, NULL, "invalid image");
315         mm_util_retvm_if(!MMUTIL_STRING_VALID(map), NULL, "invalid map");
316
317         GetExceptionInfo(&exception);
318
319         _pixels_size = sizeof(unsigned char) * strlen(map) * width *height;
320         pixels = MagickMalloc(_pixels_size);
321
322         if (pixels == NULL) {
323                 mm_util_error("Error: calloc failed.");
324                 goto ERROR;
325         }
326
327         ret = DispatchImage(image, 0, 0, width, height, map, CharPixel, pixels, &exception);
328         if (ret == MagickFail) {
329                 mm_util_error("Failed to Dispatch Image into dst buffer");
330                 MagickFree(pixels);
331                 pixels = NULL;
332                 goto ERROR;
333         }
334
335         *pixels_size = _pixels_size;
336 ERROR:
337
338         DestroyExceptionInfo(&exception);
339
340         mm_util_fleave();
341
342         return pixels;
343 }
344
345 static bool __mm_util_check_angle(mm_util_magick_rotate_type angle)
346 {
347         if ((angle == MM_UTIL_ROTATE_0) || (angle == MM_UTIL_ROTATE_90) || (angle == MM_UTIL_ROTATE_180) || (angle == MM_UTIL_ROTATE_270) ||
348                 (angle == MM_UTIL_ROTATE_FLIP_HORZ) || (angle == MM_UTIL_ROTATE_FLIP_VERT))
349                 return TRUE;
350         else
351                 return FALSE;
352 }
353
354 int mm_util_rotate_B_B(mm_util_image_h src_handle, mm_util_magick_rotate_type angle, mm_util_image_h *dst_handle)
355 {
356         int ret = MM_UTIL_ERROR_NONE;
357         mm_image_info_s *_src_handle = (mm_image_info_s*)src_handle;
358         char *map = NULL;
359         Image *_image = NULL;
360         Image *_processed_image = NULL;
361         ExceptionInfo exception;
362         size_t pixels_size = 0;
363         void *pixels = 0;
364
365         mm_util_retvm_if(src_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
366         mm_util_retvm_if(!__mm_util_check_angle(angle), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid angle [%d]", angle);
367         mm_util_retvm_if(dst_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_handle");
368
369         mm_util_debug("angle [%d]", angle);
370
371         ret = __mm_util_get_map(_src_handle->color, &map);
372         mm_util_retvm_if((ret != MM_UTIL_ERROR_NONE) || (map == NULL), ret, "fail to get map");
373
374         __mm_util_init(&exception);
375
376         _image = __mm_util_constitute_image(src_handle, map);
377         if (_image == NULL) {
378                 mm_util_error("Error: __mm_util_constitute_image failed.");
379                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
380                 goto ERROR;
381         }
382
383         _processed_image = __mm_util_rotate_image(_image, angle);
384         if (_processed_image == NULL) {
385                 mm_util_error("Error: __mm_util_rotate_image failed.");
386                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
387                 goto ERROR;
388         }
389
390         pixels = __mm_util_write_image_to_buffer(_processed_image, _processed_image->columns, _processed_image->rows, map, &pixels_size);
391         if (pixels == NULL) {
392                 mm_util_error("Error: __mm_util_write_image_to_buffer failed.");
393                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
394                 goto ERROR;
395         }
396
397         ret = mm_image_create_image(_processed_image->columns, _processed_image->rows, _src_handle->color, pixels, pixels_size, dst_handle);
398         if (ret != MM_UTIL_ERROR_NONE)
399                 mm_util_error("Error: mm_image_create_image failed.");
400
401         MagickFree(pixels);
402
403 ERROR:
404
405         __mm_util_finalize(_image, _processed_image, NULL, &exception);
406
407         MMUTIL_SAFE_FREE(map);
408
409         mm_util_fleave();
410
411         return ret;
412 }
413
414 int mm_util_rotate_B_P(mm_util_image_h src_handle, mm_util_magick_rotate_type angle, const char *dst_path)
415 {
416         int ret = MM_UTIL_ERROR_NONE;
417         mm_image_info_s *_src_handle = (mm_image_info_s*)src_handle;
418         char *map = NULL;
419         Image *_image = NULL;
420         Image *_processed_image = NULL;
421         ImageInfo *_image_info = NULL;
422         ExceptionInfo exception;
423
424         mm_util_retvm_if(src_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
425         mm_util_retvm_if(!__mm_util_check_angle(angle), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid angle [%d]", angle);
426         mm_util_retvm_if(!MMUTIL_STRING_VALID(dst_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_path");
427
428         mm_util_sec_debug("angle [%d] dst_path [%s]", angle, dst_path);
429
430         ret = __mm_util_get_map(_src_handle->color, &map);
431         mm_util_retvm_if((ret != MM_UTIL_ERROR_NONE) || (map == NULL), ret, "fail to get map");
432
433         __mm_util_init(&exception);
434
435         _image = __mm_util_constitute_image(src_handle, map);
436         if (_image == NULL) {
437                 mm_util_error("Error: __mm_util_constitute_image failed.");
438                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
439                 goto ERROR;
440         }
441
442         _processed_image = __mm_util_rotate_image(_image, angle);
443         if (_processed_image == NULL) {
444                 mm_util_error("Error: __mm_util_rotate_image failed.");
445                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
446                 goto ERROR;
447         }
448
449         _image_info = CloneImageInfo(0);
450         ret = __mm_util_write_image_to_file(_image_info, _processed_image, dst_path);
451
452 ERROR:
453
454         __mm_util_finalize(_image, _processed_image, _image_info, &exception);
455
456         MMUTIL_SAFE_FREE(map);
457
458         mm_util_fleave();
459
460         return ret;
461 }
462
463 int mm_util_rotate_P_B(const char *src_path, mm_util_magick_rotate_type angle, mm_util_color_format_e req_format, mm_util_image_h *dst_handle)
464 {
465         int ret = MM_UTIL_ERROR_NONE;
466         char *map = NULL;
467         Image *_image = NULL;
468         Image *_processed_image = NULL;
469         ImageInfo *_image_info = NULL;
470         ExceptionInfo exception;
471         size_t pixels_size = 0;
472         void *pixels = 0;
473
474         mm_util_retvm_if(!MMUTIL_STRING_VALID(src_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src_path");
475         mm_util_retvm_if(!__mm_util_check_angle(angle), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid angle [%d]", angle);
476         mm_util_retvm_if(dst_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_handle");
477
478         mm_util_sec_debug("src_path [%s] angle [%d] req_format [%d]", src_path, angle, req_format);
479
480         ret = __mm_util_get_map(req_format, &map);
481         mm_util_retvm_if((ret != MM_UTIL_ERROR_NONE) || (map == NULL), ret, "fail to get map");
482
483         __mm_util_init(&exception);
484
485         _image = __mm_util_read_image_from_file(src_path, &_image_info);
486         if (_image == NULL) {
487                 mm_util_error("Error: __mm_util_read_image_from_file failed.");
488                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
489                 goto ERROR;
490         }
491
492         _processed_image = __mm_util_rotate_image(_image, angle);
493         if (_processed_image == NULL) {
494                 mm_util_error("Error: __mm_util_rotate_image failed.");
495                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
496                 goto ERROR;
497         }
498
499         pixels = __mm_util_write_image_to_buffer(_processed_image, _processed_image->columns, _processed_image->rows, map, &pixels_size);
500         if (pixels == NULL) {
501                 mm_util_error("Error: __mm_util_write_image_to_buffer failed.");
502                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
503                 goto ERROR;
504         }
505
506         ret = mm_image_create_image(_processed_image->columns, _processed_image->rows, req_format, pixels, pixels_size, dst_handle);
507         if (ret != MM_UTIL_ERROR_NONE)
508                 mm_util_error("Error: mm_image_create_image failed.");
509
510         MagickFree(pixels);
511
512 ERROR:
513
514         __mm_util_finalize(_image, _processed_image, _image_info, &exception);
515
516         MMUTIL_SAFE_FREE(map);
517
518         mm_util_fleave();
519
520         return ret;
521 }
522
523
524 int mm_util_rotate_P_P(const char *src_path, mm_util_magick_rotate_type angle, const char *dst_path)
525 {
526         int ret = MM_UTIL_ERROR_NONE;
527         Image *_image = NULL;
528         Image *_processed_image = NULL;
529         ImageInfo *_image_info = NULL;
530         ExceptionInfo exception;
531
532         mm_util_retvm_if(!MMUTIL_STRING_VALID(src_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src_path");
533         mm_util_retvm_if(!__mm_util_check_angle(angle), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid angle [%d]", angle);
534         mm_util_retvm_if(!MMUTIL_STRING_VALID(dst_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_path");
535
536         mm_util_sec_debug("src_path [%s] angle [%d] dst_path [%s]", src_path, angle, dst_path);
537
538         __mm_util_init(&exception);
539
540         _image = __mm_util_read_image_from_file(src_path, &_image_info);
541         if (_image == NULL) {
542                 mm_util_error("Error: __mm_util_read_image_from_file failed.");
543                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
544                 goto ERROR;
545         }
546
547         _processed_image = __mm_util_rotate_image(_image, angle);
548         if (_processed_image == NULL) {
549                 mm_util_error("Error: __mm_util_rotate_image failed.");
550                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
551                 goto ERROR;
552         }
553
554         ret = __mm_util_write_image_to_file(_image_info, _processed_image, dst_path);
555
556 ERROR:
557
558         __mm_util_finalize(_image, _processed_image, _image_info, &exception);
559
560         mm_util_fleave();
561
562         return ret;
563 }
564
565 int mm_util_resize_B_B(mm_util_image_h src_handle, unsigned int req_width, unsigned int req_height, mm_util_image_h *dst_handle)
566 {
567         int ret = MM_UTIL_ERROR_NONE;
568         mm_image_info_s *_src_handle = (mm_image_info_s*)src_handle;
569         char *map = NULL;
570         Image *_image = NULL;
571         Image *_processed_image = NULL;
572         ExceptionInfo exception;
573         size_t pixels_size = 0;
574         void *pixels = 0;
575
576         mm_util_retvm_if(src_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
577         mm_util_retvm_if((req_width == 0) || (req_height == 0), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid size W[%d] H[%d]", req_width, req_height);
578         mm_util_retvm_if(dst_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_handle");
579
580         mm_util_debug("req_width [%u] req_height [%u]", req_width, req_height);
581
582         ret = __mm_util_get_map(_src_handle->color, &map);
583         mm_util_retvm_if((ret != MM_UTIL_ERROR_NONE) || (map == NULL), ret, "fail to get map");
584
585         __mm_util_init(&exception);
586
587         _image = __mm_util_constitute_image(src_handle, map);
588         if (_image == NULL) {
589                 mm_util_error("Error: __mm_util_constitute_image failed.");
590                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
591                 goto ERROR;
592         }
593
594         if ((_image->columns < req_width) || (_image->rows < req_height)) {
595                 mm_util_error("Wrong Size. image [%lu * %lu], request [%u * %u]", _image->columns, _image->rows, req_width, req_height);
596                 ret = MM_UTIL_ERROR_INVALID_PARAMETER;
597                 goto ERROR;
598         }
599
600         _processed_image = __mm_util_resize_image(_image, req_width, req_height);
601         if (_processed_image == NULL) {
602                 mm_util_error("Error: __mm_util_resize_image failed.");
603                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
604                 goto ERROR;
605         }
606
607         pixels = __mm_util_write_image_to_buffer(_processed_image, _processed_image->columns, _processed_image->rows, map, &pixels_size);
608         if (pixels == NULL) {
609                 mm_util_error("Error: __mm_util_write_image_to_buffer failed.");
610                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
611                 goto ERROR;
612         }
613
614         ret = mm_image_create_image(_processed_image->columns, _processed_image->rows, _src_handle->color, pixels, pixels_size, dst_handle);
615         if (ret != MM_UTIL_ERROR_NONE)
616                 mm_util_error("Error: mm_image_create_image failed.");
617
618         MagickFree(pixels);
619
620 ERROR:
621
622         __mm_util_finalize(_image, _processed_image, NULL, &exception);
623
624         MMUTIL_SAFE_FREE(map);
625
626         mm_util_fleave();
627
628         return ret;
629 }
630
631 int mm_util_resize_B_P(mm_util_image_h src_handle, unsigned int req_width, unsigned int req_height, const char *dst_path)
632 {
633         int ret = MM_UTIL_ERROR_NONE;
634         mm_image_info_s *_src_handle = (mm_image_info_s*)src_handle;
635         char *map = NULL;
636         Image *_image = NULL;
637         Image *_processed_image = NULL;
638         ImageInfo *_image_info = NULL;
639         ExceptionInfo exception;
640
641         mm_util_retvm_if(src_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
642         mm_util_retvm_if((req_width == 0) || (req_height == 0), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid size W[%d] H[%d]", req_width, req_height);
643         mm_util_retvm_if(!MMUTIL_STRING_VALID(dst_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_path");
644
645         mm_util_sec_debug("req_width [%u] req_height [%u] dst_path [%s]", req_width, req_height, dst_path);
646
647         ret = __mm_util_get_map(_src_handle->color, &map);
648         mm_util_retvm_if((ret != MM_UTIL_ERROR_NONE) || (map == NULL), ret, "fail to get map");
649
650         __mm_util_init(&exception);
651
652         _image = __mm_util_constitute_image(src_handle, map);
653         if (_image == NULL) {
654                 mm_util_error("Error: __mm_util_constitute_image failed.");
655                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
656                 goto ERROR;
657         }
658
659         if ((_image->columns < req_width) || (_image->rows < req_height)) {
660                 mm_util_error("Wrong Size. image [%lu * %lu], request [%u * %u]", _image->columns, _image->rows, req_width, req_height);
661                 ret = MM_UTIL_ERROR_INVALID_PARAMETER;
662                 goto ERROR;
663         }
664
665         _processed_image = __mm_util_resize_image(_image, req_width, req_height);
666         if (_processed_image == NULL) {
667                 mm_util_error("Error: __mm_util_resize_image failed.");
668                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
669                 goto ERROR;
670         }
671
672         _image_info = CloneImageInfo(0);
673         ret = __mm_util_write_image_to_file(_image_info, _processed_image, dst_path);
674
675 ERROR:
676
677         __mm_util_finalize(_image, _processed_image, _image_info, &exception);
678
679         MMUTIL_SAFE_FREE(map);
680
681         mm_util_fleave();
682
683         return ret;
684 }
685
686 int mm_util_resize_P_B(const char *src_path, unsigned int req_width, unsigned int req_height, mm_util_color_format_e req_format, mm_util_image_h *dst_handle)
687 {
688         int ret = MM_UTIL_ERROR_NONE;
689         char *map = NULL;
690         Image *_image = NULL;
691         Image *_processed_image = NULL;
692         ImageInfo *_image_info = NULL;
693         ExceptionInfo exception;
694         size_t pixels_size = 0;
695         void *pixels = 0;
696
697         mm_util_retvm_if(!MMUTIL_STRING_VALID(src_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src_path");
698         mm_util_retvm_if((req_width == 0) || (req_height == 0), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid size W[%d] H[%d]", req_width, req_height);
699         mm_util_retvm_if(dst_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_handle");
700
701         mm_util_sec_debug("src_path [%s] req_width [%u] req_height [%u]", src_path, req_width, req_height);
702
703         ret = __mm_util_get_map(req_format, &map);
704         mm_util_retvm_if((ret != MM_UTIL_ERROR_NONE) || (map == NULL), ret, "fail to get map");
705
706         __mm_util_init(&exception);
707
708         _image = __mm_util_read_image_from_file(src_path, &_image_info);
709         if (_image == NULL) {
710                 mm_util_error("Error: __mm_util_read_image_from_file failed.");
711                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
712                 goto ERROR;
713         }
714
715         if ((_image->columns < req_width) || (_image->rows < req_height)) {
716                 mm_util_error("Wrong Size. image [%lu * %lu], request [%u * %u]", _image->columns, _image->rows, req_width, req_height);
717                 ret = MM_UTIL_ERROR_INVALID_PARAMETER;
718                 goto ERROR;
719         }
720
721         _processed_image = __mm_util_resize_image(_image, req_width, req_height);
722         if (_processed_image == NULL) {
723                 mm_util_error("Error: __mm_util_resize_image failed.");
724                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
725                 goto ERROR;
726         }
727
728         pixels = __mm_util_write_image_to_buffer(_processed_image, _processed_image->columns, _processed_image->rows, map, &pixels_size);
729         if (pixels == NULL) {
730                 mm_util_error("Error: __mm_util_write_image_to_buffer failed.");
731                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
732                 goto ERROR;
733         }
734
735         ret = mm_image_create_image(_processed_image->columns, _processed_image->rows, req_format, pixels, pixels_size, dst_handle);
736         if (ret != MM_UTIL_ERROR_NONE)
737                 mm_util_error("Error: mm_image_create_image failed.");
738
739         MagickFree(pixels);
740
741 ERROR:
742
743         __mm_util_finalize(_image, _processed_image, _image_info, &exception);
744
745         MMUTIL_SAFE_FREE(map);
746
747         mm_util_fleave();
748
749         return ret;
750 }
751
752 int mm_util_resize_P_P(const char *src_path, unsigned int req_width, unsigned int req_height, const char *dst_path)
753 {
754         int ret = MM_UTIL_ERROR_NONE;
755         Image *_image = NULL;
756         Image *_processed_image = NULL;
757         ImageInfo *_image_info = NULL;
758         ExceptionInfo exception;
759
760         mm_util_retvm_if(!MMUTIL_STRING_VALID(src_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid src_path");
761         mm_util_retvm_if((req_width == 0) || (req_height == 0), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid size W[%d] H[%d]", req_width, req_height);
762         mm_util_retvm_if(!MMUTIL_STRING_VALID(dst_path), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_path");
763
764         mm_util_sec_debug("src_path [%s] req_width [%u] req_height [%u] dst_path [%s]", src_path, req_width, req_height, dst_path);
765
766         __mm_util_init(&exception);
767
768         _image = __mm_util_read_image_from_file(src_path, &_image_info);
769         if (_image == NULL) {
770                 mm_util_error("Error: __mm_util_read_image_from_file failed.");
771                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
772                 goto ERROR;
773         }
774
775         if ((_image->columns < req_width) || (_image->rows < req_height)) {
776                 mm_util_error("Wrong Size. image [%lu * %lu], request [%u * %u]", _image->columns, _image->rows, req_width, req_height);
777                 ret = MM_UTIL_ERROR_INVALID_PARAMETER;
778                 goto ERROR;
779         }
780
781         _processed_image = __mm_util_resize_image(_image, req_width, req_height);
782         if (_processed_image == NULL) {
783                 mm_util_error("Error: __mm_util_resize_image failed.");
784                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
785                 goto ERROR;
786         }
787
788         ret = __mm_util_write_image_to_file(_image_info, _processed_image, dst_path);
789
790 ERROR:
791
792         __mm_util_finalize(_image, _processed_image, _image_info, &exception);
793
794         mm_util_fleave();
795
796         return ret;
797 }
798
799 int mm_util_convert_B_B(mm_util_image_h src_handle, mm_util_color_format_e req_format, mm_util_image_h *dst_handle)
800 {
801         int ret = MM_UTIL_ERROR_NONE;
802         mm_image_info_s *_src_handle = (mm_image_info_s*)src_handle;
803         char *map = NULL;
804         Image *_image = NULL;
805         ExceptionInfo exception;
806         size_t pixels_size = 0;
807         void *pixels = 0;
808
809         mm_util_retvm_if(src_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
810         mm_util_retvm_if(dst_handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_handle");
811
812         mm_util_debug("input format [%d] req_format [%d]", _src_handle->color, req_format);
813
814         ret = __mm_util_get_map(_src_handle->color, &map);
815         mm_util_retvm_if((ret != MM_UTIL_ERROR_NONE) || (map == NULL), ret, "fail to get map");
816
817         __mm_util_init(&exception);
818
819         _image = __mm_util_constitute_image(src_handle, map);
820         if (_image == NULL) {
821                 mm_util_error("Error: __mm_util_constitute_image failed.");
822                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
823                 goto ERROR;
824         }
825
826         MMUTIL_SAFE_FREE(map);
827
828         ret = __mm_util_get_map(req_format, &map);
829         if (ret != MM_UTIL_ERROR_NONE) {
830                 mm_util_error("Error: __mm_util_get_map failed.");
831                 goto ERROR;
832         }
833
834         pixels = __mm_util_write_image_to_buffer(_image, _image->columns, _image->rows, map, &pixels_size);
835         if (pixels == NULL) {
836                 mm_util_error("Error: __mm_util_write_image_to_buffer failed.");
837                 ret = MM_UTIL_ERROR_INVALID_OPERATION;
838                 goto ERROR;
839         }
840
841         ret = mm_image_create_image(_image->columns, _image->rows, _src_handle->color, pixels, pixels_size, dst_handle);
842         if (ret != MM_UTIL_ERROR_NONE)
843                 mm_util_error("Error: mm_image_create_image failed.");
844
845         MagickFree(pixels);
846
847 ERROR:
848
849         __mm_util_finalize(_image, NULL, NULL, &exception);
850
851         MMUTIL_SAFE_FREE(map);
852
853         mm_util_fleave();
854
855         return ret;
856 }