Remove dependency with libmm-common.
[platform/core/multimedia/libmedia-thumbnail.git] / src / media-thumb-internal.c
1 /*
2  * libmedia-thumbnail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@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 "media-thumb-debug.h"
23 #include "media-thumb-util.h"
24 #include "media-thumb-internal.h"
25 #include "media-thumb-ipc.h"
26
27 #include "AGifFrameInfo.h"
28 #include "IfegDecodeAGIF.h"
29 #include "img-codec.h"
30 #include "img-codec-agif.h"
31 #include "img-codec-common.h"
32 #include "img-codec-osal.h"
33 #include "img-codec-parser.h"
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39
40 #include <mm_file.h>
41 #include <mm_util_imgp.h>
42 #include <mm_util_jpeg.h>
43 #include <Evas.h>
44 #include <Ecore_Evas.h>
45 #include <libexif/exif-data.h>
46 #include <sys/stat.h>
47 #include <grp.h>
48 #include <pwd.h>
49
50 #define MEDIA_THUMB_ROUND_UP_8(num) (((num)+7)&~7)
51
52 int _media_thumb_resize_data(unsigned char *src_data,
53                                                         int src_width,
54                                                         int src_height,
55                                                         mm_util_img_format src_format,
56                                                         media_thumb_info *thumb_info,
57                                                         int dst_width,
58                                                         int dst_height);
59
60 int _media_thumb_get_proper_thumb_size(int orig_w, int orig_h,
61                                                                 int *thumb_w, int *thumb_h)
62 {
63         BOOL portrait = FALSE;
64         double ratio;
65
66         if (orig_w < orig_h) {
67                 portrait = TRUE;
68         }
69
70         /* Set smaller length to default size */
71         if (portrait) {
72                 if (orig_w < *thumb_w)
73                         *thumb_w = orig_w;
74                 ratio = (double)orig_h / (double)orig_w;
75                 *thumb_h = *thumb_w * ratio;
76         } else {
77                 if (orig_h < *thumb_h)
78                         *thumb_h = orig_h;
79                 ratio = (double)orig_w / (double)orig_h;
80                 *thumb_w = *thumb_h * ratio;
81         }
82
83         /** CAUTION :: The width of RGB888 raw data has to be rounded by 8 **/
84         *thumb_w = MEDIA_THUMB_ROUND_UP_8(*thumb_w);
85
86         thumb_dbg("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
87
88         return MS_MEDIA_ERR_NONE;
89 }
90
91 int _media_thumb_get_exif_info(ExifData *ed, char *buf, int max_size, int *value, int ifdtype, long tagtype)
92 {
93         ExifEntry *entry;
94         ExifIfd ifd;
95         ExifTag tag;
96
97         if (ed == NULL) {
98                 return MS_MEDIA_ERR_INVALID_PARAMETER;
99         }
100
101         ifd = ifdtype;
102         tag = tagtype;
103
104         entry = exif_content_get_entry(ed->ifd[ifd], tag);
105         if (entry) {
106                 if (tag == EXIF_TAG_ORIENTATION ||
107                                 tag == EXIF_TAG_PIXEL_X_DIMENSION ||
108                                 tag == EXIF_TAG_PIXEL_Y_DIMENSION) {
109
110                         if (value == NULL) {
111                                 thumb_err("value is NULL");
112                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
113                         }
114
115                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
116                         short exif_value = exif_get_short(entry->data, mByteOrder);
117                         *value = (int)exif_value;
118                 } else {
119                         /* Get the contents of the tag in human-readable form */
120                         if (buf == NULL) {
121                                 thumb_err("buf is NULL");
122                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
123                         }
124                         exif_entry_get_value(entry, buf, max_size);
125                         buf[strlen(buf)] = '\0';
126                 }
127         }
128
129         return MS_MEDIA_ERR_NONE;
130 }
131
132 static int _media_thumb_get_data_from_exif(ExifData *ed,
133                                                                                                 void **thumb_data,
134                                                                                                 int *thumb_size,
135                                                                                                 int *thumb_width,
136                                                                                                 int *thumb_height,
137                                                                                                 int *origin_width,
138                                                                                                 int *origin_height)
139 {
140         ExifEntry *entry;
141         ExifIfd ifd;
142         ExifTag tag;
143
144         ExifByteOrder byte_order = exif_data_get_byte_order(ed);
145
146         ifd = EXIF_IFD_1;
147         tag = EXIF_TAG_COMPRESSION;
148
149         entry = exif_content_get_entry(ed->ifd[ifd], tag);
150
151         if (entry) {
152                 /* Get the contents of the tag in human-readable form */
153                 ExifShort value = exif_get_short(entry->data, byte_order);
154                 //thumb_dbg("%s: %d", exif_tag_get_name_in_ifd(tag,ifd), value);
155
156                 if (value == 6) {
157                         thumb_dbg("There's jpeg thumb in this image");
158                 } else {
159                         thumb_dbg("There's NO jpeg thumb in this image");
160                         return MS_MEDIA_ERR_INVALID_PARAMETER;
161                 }
162         } else {
163                 thumb_dbg("entry is NULL");
164                 return MS_MEDIA_ERR_INVALID_PARAMETER;
165         }
166
167         /* copy the real thumbnail data from exif data */
168         if (ed->data && ed->size) {
169                 //thumb_dbg("Size: %d, thumb: 0x%x", ed->size, ed->data);
170                 *thumb_data = (char *)malloc(ed->size);
171
172                 if (*thumb_data == NULL) {
173                         thumb_dbg("malloc failed!");
174                         return MS_MEDIA_ERR_INVALID_PARAMETER;
175                 }
176
177                 memcpy(*thumb_data, (void *)ed->data, ed->size);
178                 *thumb_size = ed->size;
179         } else {
180                 thumb_dbg("data is NULL");
181                 return MS_MEDIA_ERR_INVALID_PARAMETER;
182         }
183
184         /* Get width and height of thumbnail */
185         tag = EXIF_TAG_IMAGE_WIDTH;
186         entry = exif_content_get_entry(ed->ifd[ifd], tag);
187
188         if (entry) {
189                 /* Get the contents of the tag in human-readable form */
190                 char width[10] = {0,};
191                 exif_entry_get_value(entry, width, 10);
192
193                 *thumb_width = atoi(width);
194         } else {
195                 thumb_warn("EXIF_TAG_IMAGE_WIDTH does not exist");
196                 *thumb_width = 0;
197         }
198
199         tag = EXIF_TAG_IMAGE_LENGTH;
200         entry = exif_content_get_entry(ed->ifd[ifd], tag);
201         if (entry) {
202                 /* Get the contents of the tag in human-readable form */
203                 char height[10] = {0, };
204                 exif_entry_get_value(entry, height, 10);
205
206                 *thumb_height = atoi(height);
207         } else {
208                 thumb_warn("EXIF_TAG_IMAGE_LENGTH does not exist");
209                 *thumb_height = 0;
210         }
211
212         thumb_dbg("thumb width : height [%d:%d]", *thumb_width, *thumb_height);
213
214         /* Get width and height of original image from exif */
215         ifd = EXIF_IFD_EXIF;
216         tag = EXIF_TAG_PIXEL_X_DIMENSION;
217         entry = exif_content_get_entry(ed->ifd[ifd], tag);
218
219         if (entry) {
220                 char width[10] = {0,};
221                 exif_entry_get_value(entry, width, 10);
222
223                 *origin_width = atoi(width);
224         } else {
225                 thumb_warn("EXIF_TAG_PIXEL_X_DIMENSION does not exist");
226                 *origin_width = 0;
227         }
228
229         tag = EXIF_TAG_PIXEL_Y_DIMENSION;
230         entry = exif_content_get_entry(ed->ifd[ifd], tag);
231
232         if (entry) {
233                 char height[10] = {0, };
234                 exif_entry_get_value(entry, height, 10);
235
236                 *origin_height = atoi(height);
237         } else {
238                 thumb_warn("EXIF_TAG_PIXEL_Y_DIMENSION does not exist");
239                 *origin_height = 0;
240         }
241
242         return MS_MEDIA_ERR_NONE;
243 }
244
245 int _media_thumb_get_thumb_from_exif(ExifData *ed,
246                                                                 const char *file_full_path,
247                                                                 const char *thumb_path,
248                                                                 int orientation,
249                                                                 int required_width,
250                                                                 int required_height,
251                                                                 media_thumb_info *thumb_info)
252 {
253         int err = MS_MEDIA_ERR_NONE;
254         int size = 0;
255         int thumb_width = 0;
256         int thumb_height = 0;
257         int origin_width = 0;
258         int origin_height = 0;
259         void *thumb = NULL;
260         bool is_rotated = (orientation == ROT_90 || orientation == ROT_180 || orientation == ROT_270) ? TRUE : FALSE;
261         mm_util_jpeg_yuv_data decoded = {0,};
262
263         if (ed == NULL) {
264                 return MS_MEDIA_ERR_INVALID_PARAMETER;
265         }
266
267         err = _media_thumb_get_data_from_exif(ed,
268                                                                                 &thumb,
269                                                                                 &size,
270                                                                                 &thumb_width,
271                                                                                 &thumb_height,
272                                                                                 &origin_width,
273                                                                                 &origin_height);
274
275         if (err != MS_MEDIA_ERR_NONE) {
276                 thumb_err("There is no exif data");
277                 return err;
278         }
279
280         thumb_dbg("thumb width : height [%d:%d]", thumb_width, thumb_height);
281         thumb_dbg("origin width : height [%d:%d]", origin_width, origin_height);
282         thumb_info->origin_height = origin_height;
283         thumb_info->origin_width = origin_width;
284
285         if (thumb_width < required_width) {
286                 thumb_err("Thumb data in exif is too small");
287                 SAFE_FREE(thumb);
288                 return MS_MEDIA_ERR_INVALID_PARAMETER;
289         }
290
291         if (is_rotated) {
292                 err = mm_util_decode_from_jpeg_memory(&decoded, thumb, size, MM_UTIL_JPEG_FMT_RGB888);
293                 SAFE_FREE(thumb);
294                 if (err != MS_MEDIA_ERR_NONE) {
295                         thumb_err("mm_util_decode_from_jpeg_turbo_memory failed : %d", err);
296                         return err;
297                 }
298
299                 thumb_width = decoded.width;
300                 thumb_height = decoded.height;
301
302                 if (is_rotated) {
303                         /* Start to decode to rotate */
304                         unsigned char *rotated = NULL;
305                         unsigned int r_w = decoded.height;
306                         unsigned int r_h = decoded.width;
307                         unsigned int r_size = 0;
308                         mm_util_img_rotate_type rot_type = MM_UTIL_ROTATE_0;
309
310                         int i, rotate_cnt = 0;
311
312                         rot_type = MM_UTIL_ROTATE_90;
313                         if (orientation == ROT_90) {
314                                 rotate_cnt = 1;
315                         } else if (orientation == ROT_180) {
316                                 rotate_cnt = 2;
317                         } else if (orientation == ROT_270) {
318                                 rotate_cnt = 3;
319                         }
320
321                         for (i = 0; i < rotate_cnt; i++) {
322                                 if (i == 1) {
323                                         r_w = decoded.width;
324                                         r_h = decoded.height;
325                                 }
326
327                                 err = mm_util_get_image_size(MM_UTIL_IMG_FMT_RGB888, r_w, r_h, &r_size);
328                                 if (err != MS_MEDIA_ERR_NONE) {
329                                         thumb_err("mm_util_get_image_size failed : %d", err);
330                                         SAFE_FREE(decoded.data);
331                                         return err;
332                                 }
333
334                                 rotated = (unsigned char *)malloc(r_size);
335                                 err = mm_util_rotate_image(decoded.data, decoded.width, decoded.height,
336                                                                                         MM_UTIL_IMG_FMT_RGB888,
337                                                                                         rotated, &r_w, &r_h,
338                                                                                         rot_type);
339
340                                 if (err != MS_MEDIA_ERR_NONE) {
341                                         thumb_err("mm_util_rotate_image failed : %d", err);
342                                         SAFE_FREE(decoded.data);
343                                         SAFE_FREE(rotated);
344                                         return err;
345                                 } else {
346                                         thumb_err("mm_util_rotate_image succeed");
347                                 }
348
349                                 SAFE_FREE(decoded.data);
350                                 decoded.data = rotated;
351                                 decoded.width = r_w;
352                                 decoded.height = r_h;
353                         }
354
355                         //thumb_dbg("Width : %d, Height : %d", r_w, r_h);
356                         thumb_info->data = rotated;
357                         thumb_info->size = r_size;
358                         thumb_info->width = r_w;
359                         thumb_info->height = r_h;
360                 } else {
361                         thumb_warn("Unknown orientation");
362                         SAFE_FREE(decoded.data);
363                         return MS_MEDIA_ERR_INVALID_PARAMETER;
364                 }
365         } else {
366                 /*in this case, just write raw data in file */
367                 thumb_dbg_slog("Thumb is :%s", thumb_path);
368
369                 int nwrite;
370                 int fd = open(thumb_path, O_RDWR | O_CREAT | O_EXCL | O_SYNC, 0644);
371                 if (fd < 0) {
372                         if (errno == EEXIST) {
373                                 thumb_err("thumb alread exist!");
374                         } else {
375                                 thumb_err("open failed");
376                                 SAFE_FREE(thumb);
377                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
378                         }
379                 } else {
380                         nwrite = write(fd, thumb, size);
381                         if (nwrite < 0) {
382                                 thumb_err("write failed");
383                                 close(fd);
384
385                                 SAFE_FREE(thumb);
386                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
387                         }
388                         close(fd);
389                 }
390
391                 SAFE_FREE(thumb);
392                 thumb_info->data = NULL;
393                 thumb_info->size = size;
394                 thumb_info->width = thumb_width;
395                 thumb_info->height = thumb_height;
396                 thumb_info->is_saved = TRUE;
397         }
398
399         return err;
400 }
401
402 int _media_thumb_resize_data(unsigned char *src_data,
403                                                         int src_width,
404                                                         int src_height,
405                                                         mm_util_img_format src_format,
406                                                         media_thumb_info *thumb_info,
407                                                         int dst_width,
408                                                         int dst_height)
409 {
410         int thumb_width = dst_width;
411         int thumb_height = dst_height;
412         unsigned int buf_size = 0;
413
414         if (mm_util_get_image_size(src_format, thumb_width, thumb_height, &buf_size) < 0) {
415                 thumb_err("Failed to get buffer size");
416                 return MS_MEDIA_ERR_INTERNAL;
417         }
418
419         thumb_dbg("mm_util_get_image_size : %d", buf_size);
420
421         unsigned char *dst = (unsigned char *)malloc(buf_size);
422
423         if (dst == NULL) {
424                 thumb_err("malloc fails");
425                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
426         }
427
428         if (mm_util_resize_image((unsigned char *)src_data, src_width,
429                         src_height, src_format,
430                         dst, (unsigned int *)&thumb_width,
431                         (unsigned int *)&thumb_height) < 0) {
432                 thumb_err("Failed to resize the thumbnails");
433
434                 SAFE_FREE(dst);
435
436                 return MS_MEDIA_ERR_INTERNAL;
437         }
438
439         thumb_info->size = buf_size;
440         thumb_info->width = thumb_width;
441         thumb_info->height = thumb_height;
442         thumb_info->data = malloc(buf_size);
443         if (thumb_info->data != NULL) {
444                 memcpy(thumb_info->data, dst, buf_size);
445         } else {
446                 thumb_err("malloc fails");
447                 SAFE_FREE(dst);
448                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
449         }
450         SAFE_FREE(dst);
451
452         return MS_MEDIA_ERR_NONE;
453 }
454
455 int _media_thumb_get_wh_with_evas(const char *origin_path, int *width, int *height)
456 {
457         /* using evas to get w/h */
458         Ecore_Evas *ee = ecore_evas_buffer_new(0, 0);
459         if (!ee) {
460                 thumb_err("ecore_evas_buffer_new fails");
461                 return MS_MEDIA_ERR_INTERNAL;
462         }
463
464         Evas *evas = ecore_evas_get(ee);
465         if (!evas) {
466                 thumb_err("ecore_evas_get fails");
467                 ecore_evas_free(ee);
468                 return MS_MEDIA_ERR_INTERNAL;
469         }
470
471         Evas_Object *image_object = evas_object_image_add(evas);
472         if (!image_object) {
473                 thumb_err("evas_object_image_add fails");
474                 ecore_evas_free(ee);
475                 return MS_MEDIA_ERR_INTERNAL;
476         }
477
478         evas_object_image_file_set(image_object, origin_path, NULL);
479         evas_object_image_size_get(image_object, width, height);
480
481         thumb_dbg("Width:%d, Height:%d", *width, *height);
482
483         ecore_evas_free(ee);
484
485         return MS_MEDIA_ERR_NONE;
486 }
487
488 int _media_thumb_decode_with_evas(const char *origin_path,
489                                         int thumb_width, int thumb_height,
490                                         media_thumb_info *thumb_info, int need_scale, int orientation)
491 {
492         Ecore_Evas *resize_img_ee;
493
494         resize_img_ee = ecore_evas_buffer_new(thumb_width, thumb_height);
495         if (!resize_img_ee) {
496                 thumb_err("ecore_evas_buffer_new failed");
497                 return MS_MEDIA_ERR_INTERNAL;
498         }
499
500         Evas *resize_img_e = ecore_evas_get(resize_img_ee);
501         if (!resize_img_e) {
502                 thumb_err("ecore_evas_get failed");
503                 ecore_evas_free(resize_img_ee);
504                 return MS_MEDIA_ERR_INTERNAL;
505         }
506
507         Evas_Object *source_img = evas_object_image_add(resize_img_e);
508         if (!source_img) {
509                 thumb_err("evas_object_image_add failed");
510                 ecore_evas_free(resize_img_ee);
511                 return MS_MEDIA_ERR_INTERNAL;
512         }
513
514         evas_object_image_file_set(source_img, origin_path, NULL);
515
516         /* Get w/h of original image */
517         int width = 0;
518         int height = 0;
519
520         evas_object_image_size_get(source_img, &width, &height);
521         thumb_info->origin_width = width;
522         thumb_info->origin_height = height;
523         //thumb_dbg("origin width:%d, origin height:%d", width, height);
524
525         if ((need_scale == 1) && (width * height > THUMB_MAX_ALLOWED_MEM_FOR_THUMB)) {
526                 thumb_warn("This is too large image. so this's scale is going to be down");
527                 evas_object_image_load_scale_down_set(source_img, 10);
528         }
529
530         if (orientation != TRANSPOSE)
531                 evas_object_image_load_orientation_set(source_img, 1);
532
533         int rotated_orig_w = 0;
534         int rotated_orig_h = 0;
535
536         if (orientation == ROT_90 || orientation == ROT_270) {
537                 rotated_orig_w = height;
538                 rotated_orig_h = width;
539         } else {
540                 rotated_orig_w = width;
541                 rotated_orig_h = height;
542         }
543         //thumb_dbg("rotated - origin width:%d, origin height:%d", rotated_orig_w, rotated_orig_h);
544
545         int err = MS_MEDIA_ERR_NONE;
546
547         err = _media_thumb_get_proper_thumb_size(rotated_orig_w, rotated_orig_h, &thumb_width, &thumb_height);
548         if (err != MS_MEDIA_ERR_NONE) {
549                 thumb_err("_media_thumb_get_proper_thumb_size failed: %d", err);
550                 ecore_evas_free(resize_img_ee);
551                 return err;
552         }
553
554         ecore_evas_resize(resize_img_ee, thumb_width, thumb_height);
555
556         evas_object_image_load_size_set(source_img, thumb_width, thumb_height);
557         evas_object_image_fill_set(source_img, 0, 0, thumb_width, thumb_height);
558         evas_object_image_filled_set(source_img, 1);
559
560         evas_object_resize(source_img, thumb_width, thumb_height);
561         evas_object_show(source_img);
562
563         /* Set alpha from original */
564         thumb_info->alpha = evas_object_image_alpha_get(source_img);
565         if (thumb_info->alpha) ecore_evas_alpha_set(resize_img_ee, EINA_TRUE);
566
567         /* Create target buffer and copy origin resized img to it */
568         Ecore_Evas *target_ee = ecore_evas_buffer_new(thumb_width, thumb_height);
569         if (!target_ee) {
570                 thumb_err("ecore_evas_buffer_new failed");
571                 ecore_evas_free(resize_img_ee);
572                 return MS_MEDIA_ERR_INTERNAL;
573         }
574
575         Evas *target_evas = ecore_evas_get(target_ee);
576         if (!target_evas) {
577                 thumb_err("ecore_evas_get failed");
578                 ecore_evas_free(resize_img_ee);
579                 ecore_evas_free(target_ee);
580                 return MS_MEDIA_ERR_INTERNAL;
581         }
582
583         Evas_Object *ret_image = evas_object_image_add(target_evas);
584         evas_object_image_size_set(ret_image, thumb_width, thumb_height);
585         evas_object_image_fill_set(ret_image, 0, 0, thumb_width, thumb_height);
586         evas_object_image_filled_set(ret_image, EINA_TRUE);
587
588         evas_object_image_data_set(ret_image, (int *)ecore_evas_buffer_pixels_get(resize_img_ee));
589         evas_object_image_data_update_add(ret_image, 0, 0, thumb_width, thumb_height);
590
591         unsigned int buf_size = 0;
592         if (mm_util_get_image_size(MM_UTIL_IMG_FMT_BGRA8888, thumb_width, thumb_height, &buf_size) < 0) {
593                 thumb_err("mm_util_get_image_size failed");
594
595                 ecore_evas_free(resize_img_ee);
596                 ecore_evas_free(target_ee);
597
598                 return MS_MEDIA_ERR_INTERNAL;
599         }
600         //thumb_dbg("mm_util_get_image_size : %d", buf_size);
601
602         thumb_info->size = buf_size;
603         thumb_info->width = thumb_width;
604         thumb_info->height = thumb_height;
605         thumb_info->data = malloc(buf_size);
606         if (thumb_info->data == NULL) {
607                 thumb_err("Failed to allocate memory");
608                 ecore_evas_free(resize_img_ee);
609                 ecore_evas_free(target_ee);
610
611                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
612         }
613
614         void *image_data = evas_object_image_data_get(ret_image, 1);
615         if (image_data != NULL) {
616                 memcpy(thumb_info->data, image_data, buf_size);
617         } else {
618                 thumb_err("image_data is NULL. evas_object_image_data_get failed");
619         }
620
621         ecore_evas_free(target_ee);
622         ecore_evas_free(resize_img_ee);
623
624         return err;
625 }
626
627 mm_util_img_format _media_thumb_get_format(media_thumb_format src_format)
628 {
629         switch (src_format) {
630                 case MEDIA_THUMB_BGRA:
631                         return MM_UTIL_IMG_FMT_BGRA8888;
632                 case MEDIA_THUMB_RGB888:
633                         return MM_UTIL_IMG_FMT_RGB888;
634                 default:
635                         return MS_MEDIA_ERR_INVALID_PARAMETER;
636         }
637 }
638
639 int _media_thumb_convert_data(media_thumb_info *thumb_info,
640                                                         int thumb_width,
641                                                         int thumb_height,
642                                                         mm_util_img_format src_format,
643                                                         mm_util_img_format dst_format)
644 {
645         int err = MS_MEDIA_ERR_NONE;
646         unsigned int buf_size = 0;
647         unsigned char *src_data = thumb_info->data;
648         unsigned char *dst_data = NULL;
649
650         thumb_dbg("src format:%d, dst format:%d", src_format, dst_format);
651
652         if (mm_util_get_image_size(dst_format, thumb_width, thumb_height, &buf_size) < 0) {
653                 thumb_err("mm_util_get_image_size failed");
654                 return MS_MEDIA_ERR_INTERNAL;
655         }
656
657         thumb_dbg("mm_util_get_image_size : %d", buf_size);
658
659         dst_data = (unsigned char *)malloc(buf_size);
660
661         if (dst_data == NULL) {
662                 thumb_err("Failed to allocate memory");
663                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
664         }
665
666         if (src_format == MM_UTIL_IMG_FMT_RGB888 &&
667                 dst_format == MM_UTIL_IMG_FMT_BGRA8888) {
668
669                 int i = 0, j;
670                 for (j = 0; j < thumb_width * 3 * thumb_height;
671                                 j += 3) {
672                         dst_data[i++] = (src_data[j + 2]);
673                         dst_data[i++] = (src_data[j + 1]);
674                         dst_data[i++] = (src_data[j]);
675                         dst_data[i++] = 0x0;
676                 }
677
678         } else {
679                 err = mm_util_convert_colorspace(src_data,
680                                                 thumb_width,
681                                                 thumb_height,
682                                                 src_format,
683                                                 dst_data,
684                                                 dst_format);
685
686                 if (err < 0) {
687                         thumb_err("Failed to change from rgb888 to argb8888 %d", err);
688                         SAFE_FREE(dst_data);
689                         return MS_MEDIA_ERR_INTERNAL;
690                 }
691         }
692
693         SAFE_FREE(thumb_info->data);
694         thumb_info->data = dst_data;
695         thumb_info->size = buf_size;
696
697         thumb_dbg("_media_thumb_convert_data success");
698
699         return err;
700 }
701
702 int _media_thumb_convert_format(media_thumb_info *thumb_info,
703                                                         media_thumb_format src_format,
704                                                         media_thumb_format dst_format)
705 {
706         int err = MS_MEDIA_ERR_NONE;
707
708         if (src_format == dst_format) {
709                 //thumb_dbg("src_format == dst_format");
710                 return err;
711         }
712
713         mm_util_img_format src_mm_format;
714         mm_util_img_format dst_mm_format;
715
716         src_mm_format = _media_thumb_get_format(src_format);
717         dst_mm_format = _media_thumb_get_format(dst_format);
718
719         if ((int)src_mm_format == -1 || (int)dst_mm_format == -1) {
720                 thumb_err("Format is invalid");
721                 return MS_MEDIA_ERR_INVALID_PARAMETER;
722         }
723
724         err = _media_thumb_convert_data(thumb_info,
725                                         thumb_info->width,
726                                         thumb_info->height,
727                                         src_mm_format,
728                                         dst_mm_format);
729
730         if (err != MS_MEDIA_ERR_NONE) {
731                 thumb_err("media_thumb_convert_format failed : %d", err);
732                 return err;
733         }
734
735         return err;
736 }
737
738 int _media_thumb_agif(const char *origin_path,
739                                         int image_width,
740                                         int image_height,
741                                         int thumb_width,
742                                         int thumb_height,
743                                         media_thumb_format format,
744                                         media_thumb_info *thumb_info)
745 {
746         int err = MS_MEDIA_ERR_NONE;
747         unsigned int *thumb = NULL;
748
749         thumb = ImgGetFirstFrameAGIFAtSize(origin_path, image_width, image_height);
750
751         if (!thumb) {
752                 thumb_err("Frame data is NULL!!");
753                 return MS_MEDIA_ERR_INTERNAL;
754         }
755
756         err = _media_thumb_get_proper_thumb_size(thumb_info->origin_width, thumb_info->origin_height, &thumb_width, &thumb_height);
757         if (err != MS_MEDIA_ERR_NONE) {
758                 thumb_err("_media_thumb_get_proper_thumb_size failed: %d", err);
759                 SAFE_FREE(thumb);
760                 return err;
761         }
762
763         err = _media_thumb_resize_data((unsigned char *)thumb,
764                                                                         image_width,
765                                                                         image_height,
766                                                                         MM_UTIL_IMG_FMT_RGB888,
767                                                                         thumb_info,
768                                                                         thumb_width,
769                                                                         thumb_height);
770
771         if (err != MS_MEDIA_ERR_NONE) {
772                 thumb_err("_media_thumb_resize_data failed: %d", err);
773                 SAFE_FREE(thumb);
774                 return err;
775         }
776
777         SAFE_FREE(thumb);
778
779         err = _media_thumb_convert_format(thumb_info, MEDIA_THUMB_RGB888, format);
780         if (err != MS_MEDIA_ERR_NONE) {
781                 thumb_err("_media_thumb_convert_format falied: %d", err);
782                 SAFE_FREE(thumb_info->data);
783                 return err;
784         }
785
786         return err;
787 }
788
789 int _media_thumb_png(const char *origin_path,
790                                         int thumb_width,
791                                         int thumb_height,
792                                         media_thumb_format format,
793                                         media_thumb_info *thumb_info)
794 {
795         int err = MS_MEDIA_ERR_NONE;
796         err = _media_thumb_decode_with_evas(origin_path, thumb_width, thumb_height, thumb_info, 1, NORMAL);
797
798         if (err != MS_MEDIA_ERR_NONE) {
799                 thumb_err("decode_with_evas failed : %d", err);
800                 return err;
801         }
802
803         err = _media_thumb_convert_format(thumb_info, MEDIA_THUMB_BGRA, format);
804         if (err != MS_MEDIA_ERR_NONE) {
805                 thumb_err("_media_thumb_convert_format falied: %d", err);
806                 SAFE_FREE(thumb_info->data);
807                 return err;
808         }
809
810         return err;
811 }
812
813 int _media_thumb_bmp(const char *origin_path,
814                                         int thumb_width,
815                                         int thumb_height,
816                                         media_thumb_format format,
817                                         media_thumb_info *thumb_info)
818 {
819         int err = MS_MEDIA_ERR_NONE;
820         err = _media_thumb_decode_with_evas(origin_path, thumb_width, thumb_height, thumb_info, 1, NORMAL);
821
822         if (err != MS_MEDIA_ERR_NONE) {
823                 thumb_err("decode_with_evas failed : %d", err);
824                 return err;
825         }
826
827         err = _media_thumb_convert_format(thumb_info, MEDIA_THUMB_BGRA, format);
828         if (err != MS_MEDIA_ERR_NONE) {
829                 thumb_err("_media_thumb_convert_format falied: %d", err);
830                 SAFE_FREE(thumb_info->data);
831                 return err;
832         }
833
834         return err;
835 }
836
837 int _media_thumb_wbmp(const char *origin_path,
838                                         int thumb_width,
839                                         int thumb_height,
840                                         media_thumb_format format,
841                                         media_thumb_info *thumb_info)
842 {
843         int err = MS_MEDIA_ERR_NONE;
844         err = _media_thumb_decode_with_evas(origin_path, thumb_width, thumb_height, thumb_info, 1, NORMAL);
845
846         if (err != MS_MEDIA_ERR_NONE) {
847                 thumb_err("decode_with_evas failed : %d", err);
848                 return err;
849         }
850
851         err = _media_thumb_convert_format(thumb_info, MEDIA_THUMB_BGRA, format);
852         if (err != MS_MEDIA_ERR_NONE) {
853                 thumb_err("_media_thumb_convert_format falied: %d", err);
854                 SAFE_FREE(thumb_info->data);
855                 return err;
856         }
857
858         return err;
859 }
860
861 int _media_thumb_gif(const char *origin_path,
862                                         int thumb_width,
863                                         int thumb_height,
864                                         media_thumb_format format,
865                                         media_thumb_info *thumb_info)
866 {
867         int err = MS_MEDIA_ERR_NONE;
868         err = _media_thumb_decode_with_evas(origin_path, thumb_width, thumb_height, thumb_info, 1, NORMAL);
869
870         if (err != MS_MEDIA_ERR_NONE) {
871                 thumb_err("decode_with_evas failed : %d", err);
872                 return err;
873         }
874
875         err = _media_thumb_convert_format(thumb_info, MEDIA_THUMB_BGRA, format);
876         if (err != MS_MEDIA_ERR_NONE) {
877                 thumb_err("_media_thumb_convert_format falied: %d", err);
878                 SAFE_FREE(thumb_info->data);
879                 return err;
880         }
881
882         return err;
883 }
884
885 int _media_thumb_jpeg(const char *origin_path,
886                                         const char *thumb_path,
887                                         int thumb_width,
888                                         int thumb_height,
889                                         media_thumb_format format,
890                                         media_thumb_info *thumb_info)
891 {
892         int err = MS_MEDIA_ERR_NONE;
893         ExifData *ed = NULL;
894         int thumb_done = 0;
895         int orientation = NORMAL;
896
897         /* Load an ExifData object from an EXIF file */
898         ed = exif_data_new_from_file(origin_path);
899
900         if (ed) {
901                 /* First, Get orientation from exif */
902                 err = _media_thumb_get_exif_info(ed, NULL, 0, &orientation, EXIF_IFD_0, EXIF_TAG_ORIENTATION);
903
904                 if (err != MS_MEDIA_ERR_NONE) {
905                         thumb_warn("_media_thumb_get_exif_info failed");
906                 }
907
908                 /* Second, Get thumb from exif */
909                 err = _media_thumb_get_thumb_from_exif(ed, origin_path, thumb_path, orientation, thumb_width, thumb_height, thumb_info);
910
911                 if (err != MS_MEDIA_ERR_NONE) {
912                         thumb_dbg("_media_thumb_get_thumb_from_exif failed");
913                 } else {
914                         thumb_done = 1;
915                         thumb_dbg("_media_thumb_get_thumb_from_exif succeed");
916
917                         /* The case that original image's size is not in exif header. Use evas to get w/h */
918                         if (thumb_info->origin_width == 0 || thumb_info->origin_height == 0) {
919                                 thumb_warn("original image's size is not in exif header. Use evas to get w/h");
920                                 err = _media_thumb_get_wh_with_evas(origin_path, &(thumb_info->origin_width), &(thumb_info->origin_height));
921                                 if (err != MS_MEDIA_ERR_NONE) {
922                                         thumb_err("Couldn't get w/h using evas : %s", origin_path);
923                                 } else {
924                                         thumb_dbg("origin w : %d, origin h : %d", thumb_info->origin_width, thumb_info->origin_height);
925                                 }
926                         }
927
928                         if (thumb_info->is_saved == FALSE) {
929                                 mm_util_img_format dst_format = _media_thumb_get_format(format);
930
931                                 err = _media_thumb_convert_data(thumb_info,
932                                                                 thumb_info->width,
933                                                                 thumb_info->height,
934                                                                 MM_UTIL_IMG_FMT_RGB888,
935                                                                 dst_format);
936
937                                 if (err != MS_MEDIA_ERR_NONE) {
938                                         thumb_err("_media_thumb_convert_data failed : %d", err);
939                                         exif_data_unref(ed);
940                                         return err;
941                                 }
942                         }
943                 }
944
945                 exif_data_unref(ed);
946         }
947
948         if (!thumb_done) {
949
950                 err = _media_thumb_decode_with_evas(origin_path, thumb_width, thumb_height, thumb_info, 1, orientation);
951
952                 if (err != MS_MEDIA_ERR_NONE) {
953                         thumb_err("decode_with_evas failed : %d", err);
954                         return err;
955                 }
956
957                 err = _media_thumb_convert_format(thumb_info, MEDIA_THUMB_BGRA, format);
958                 if (err != MS_MEDIA_ERR_NONE) {
959                         thumb_err("_media_thumb_convert_format falied: %d", err);
960                         SAFE_FREE(thumb_info->data);
961                         return err;
962                 }
963         }
964
965         return err;
966 }
967
968 int _media_thumb_jpeg_for_raw(const char *origin_path,
969                                         const char *thumb_path,
970                                         int thumb_width,
971                                         int thumb_height,
972                                         media_thumb_format format,
973                                         media_thumb_info *thumb_info)
974 {
975         int err = MS_MEDIA_ERR_NONE;
976         int orientation = NORMAL;
977
978         err = _media_thumb_decode_with_evas(origin_path, thumb_width, thumb_height, thumb_info, 1, orientation);
979
980         if (err != MS_MEDIA_ERR_NONE) {
981                 thumb_err("decode_with_evas failed : %d", err);
982                 return err;
983         }
984
985         err = _media_thumb_convert_format(thumb_info, MEDIA_THUMB_BGRA, format);
986         if (err != MS_MEDIA_ERR_NONE) {
987                 thumb_err("_media_thumb_convert_format falied: %d", err);
988                 SAFE_FREE(thumb_info->data);
989                 return err;
990         }
991         return err;
992 }
993
994 int _media_thumb_image(const char *origin_path,
995                                         const char *thumb_path,
996                                         int thumb_width,
997                                         int thumb_height,
998                                         media_thumb_format format,
999                                         media_thumb_info *thumb_info,
1000                                         bool is_req_raw)
1001 {
1002         int err = MS_MEDIA_ERR_NONE;
1003         ImgCodecType image_type = 0;
1004         unsigned int origin_w = 0;
1005         unsigned int origin_h = 0;
1006
1007         err = ImgGetImageInfoForThumb(origin_path, &image_type, &origin_w, &origin_h);
1008
1009         if (err != MS_MEDIA_ERR_NONE) {
1010                 thumb_warn("Getting image info is failed err: %d", err);
1011         }
1012
1013         thumb_info->origin_width = origin_w;
1014         thumb_info->origin_height = origin_h;
1015
1016         //thumb_dbg("image type is %d, width:%d, height:%d", image_type, origin_w, origin_h);
1017
1018         if ((image_type != IMG_CODEC_JPEG) &&
1019                 (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM_FOR_THUMB)) {
1020                 thumb_warn("This original image is too big");
1021                 return MS_MEDIA_ERR_THUMB_TOO_BIG;
1022         }
1023
1024         if (image_type == IMG_CODEC_AGIF) {
1025                 err = _media_thumb_agif(origin_path, origin_w, origin_h, thumb_width, thumb_height, format, thumb_info);
1026         } else if (image_type == IMG_CODEC_JPEG) {
1027                 if (is_req_raw == TRUE) {
1028                         err = _media_thumb_jpeg_for_raw(origin_path, thumb_path, thumb_width, thumb_height, format, thumb_info);
1029                 } else {
1030                         err = _media_thumb_jpeg(origin_path, thumb_path, thumb_width, thumb_height, format, thumb_info);
1031                 }
1032         } else if (image_type == IMG_CODEC_PNG) {
1033                 err = _media_thumb_png(origin_path, thumb_width, thumb_height, format, thumb_info);
1034         } else if (image_type == IMG_CODEC_GIF) {
1035                 err = _media_thumb_gif(origin_path, thumb_width, thumb_height, format, thumb_info);
1036         } else if (image_type == IMG_CODEC_BMP) {
1037                 err = _media_thumb_bmp(origin_path, thumb_width, thumb_height, format, thumb_info);
1038         } else {
1039                 char file_ext[10];
1040                 err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
1041                 if (err != MS_MEDIA_ERR_NONE) {
1042                         thumb_warn("_media_thumb_get_file_ext failed");
1043                 } else {
1044                         if (strcasecmp(file_ext, "wbmp") == 0) {
1045                                 image_type = IMG_CODEC_WBMP;
1046                                 int wbmp_width = 0;
1047                                 int wbmp_height = 0;
1048
1049                                 err = _media_thumb_get_wh_with_evas(origin_path, &wbmp_width, &wbmp_height);
1050                                 if (err != MS_MEDIA_ERR_NONE) {
1051                                         thumb_err("_media_thumb_get_wh_with_evas in WBMP : %d", err);
1052                                         return err;
1053                                 }
1054
1055                                 if (wbmp_width * wbmp_height > THUMB_MAX_ALLOWED_MEM_FOR_THUMB) {
1056                                         thumb_warn("This original image is too big");
1057                                         return MS_MEDIA_ERR_THUMB_TOO_BIG;
1058                                 }
1059
1060                                 thumb_info->origin_width = wbmp_width;
1061                                 thumb_info->origin_height = wbmp_height;
1062
1063                                 err = _media_thumb_wbmp(origin_path, thumb_width, thumb_height, format, thumb_info);
1064
1065                                 return err;
1066                         }
1067                 }
1068
1069                 thumb_warn("Unsupported image type");
1070                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1071         }
1072
1073         return err;
1074 }
1075
1076 int _media_thumb_video(const char *origin_path,
1077                                         int thumb_width,
1078                                         int thumb_height,
1079                                         media_thumb_format format,
1080                                         media_thumb_info *thumb_info)
1081 {
1082         int err = MS_MEDIA_ERR_NONE;
1083
1084         MMHandleType content = (MMHandleType) NULL;
1085         void *frame = NULL;
1086         int video_track_num = 0;
1087         char *err_msg = NULL;
1088         int is_drm = 0;
1089         int size = 0;
1090         int width = 0;
1091         int height = 0;
1092         bool drm_type = FALSE;
1093
1094         is_drm = drm_type;
1095
1096         /* Get Content Tag attribute for orientatin */
1097         MMHandleType tag = (MMHandleType) NULL;
1098         char *p = NULL;
1099         int cdis_value = 0;
1100         err = mm_file_create_tag_attrs(&tag, origin_path);
1101         mm_util_img_rotate_type rot_type = MM_UTIL_ROTATE_0;
1102
1103         if (err == FILEINFO_ERROR_NONE) {
1104                 err = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1105                 if (err == FILEINFO_ERROR_NONE && size >= 0) {
1106                         if (p == NULL) {
1107                                 rot_type = MM_UTIL_ROTATE_0;
1108                         } else {
1109                                 if (strncmp(p, "90", size) == 0) {
1110                                         rot_type = MM_UTIL_ROTATE_90;
1111                                 } else if (strncmp(p, "180", size) == 0) {
1112                                         rot_type = MM_UTIL_ROTATE_180;
1113                                 } else if (strncmp(p, "270", size) == 0) {
1114                                         rot_type = MM_UTIL_ROTATE_270;
1115                                 } else {
1116                                         rot_type = MM_UTIL_ROTATE_0;
1117                                 }
1118                         }
1119                         thumb_dbg("There is tag rotate : %d", rot_type);
1120                 } else {
1121                         thumb_dbg("There is NOT tag rotate");
1122                         rot_type = MM_UTIL_ROTATE_0;
1123                         SAFE_FREE(err_msg);
1124                 }
1125
1126                 err = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1127                 if (err != FILEINFO_ERROR_NONE) {
1128                         cdis_value = 0;
1129                         SAFE_FREE(err_msg);
1130                 }
1131
1132         } else {
1133                 rot_type = MM_UTIL_ROTATE_0;
1134                 cdis_value = 0;
1135         }
1136
1137         err = mm_file_destroy_tag_attrs(tag);
1138         if (err != FILEINFO_ERROR_NONE) {
1139                 thumb_err("fail to free tag attr - err(%x)", err);
1140         }
1141
1142         if (cdis_value == 1) {
1143                 thumb_warn("This is CDIS vlaue 1");
1144                 err = mm_file_create_content_attrs_safe(&content, origin_path);
1145         } else {
1146                 err = mm_file_create_content_attrs(&content, origin_path);
1147         }
1148
1149         if (err != FILEINFO_ERROR_NONE) {
1150                 thumb_err("mm_file_create_content_attrs fails : %d", err);
1151                 return MS_MEDIA_ERR_INTERNAL;
1152         }
1153
1154         err = mm_file_get_attrs(content, &err_msg, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &video_track_num, NULL);
1155         if (err != FILEINFO_ERROR_NONE) {
1156                 thumb_err("mm_file_get_attrs fails : %s", err_msg);
1157                 SAFE_FREE(err_msg);
1158                 mm_file_destroy_content_attrs(content);
1159                 return MS_MEDIA_ERR_INTERNAL;
1160         }
1161
1162         /* MMF api handle both normal and DRM video */
1163         if (video_track_num > 0 || is_drm) {
1164
1165                 err = mm_file_get_attrs(content, &err_msg,
1166                                         MM_FILE_CONTENT_VIDEO_WIDTH,
1167                                         &width,
1168                                         MM_FILE_CONTENT_VIDEO_HEIGHT,
1169                                         &height,
1170                                         MM_FILE_CONTENT_VIDEO_THUMBNAIL, &frame, /* raw image is RGB888 format */
1171                                         &size, NULL);
1172
1173                 if (err != FILEINFO_ERROR_NONE) {
1174                         thumb_err("mm_file_get_attrs fails : %s", err_msg);
1175                         SAFE_FREE(err_msg);
1176                         mm_file_destroy_content_attrs(content);
1177                         return MS_MEDIA_ERR_INTERNAL;
1178                 }
1179
1180                 thumb_dbg("video width: %d", width);
1181                 thumb_dbg("video height: %d", height);
1182                 thumb_dbg("thumbnail size=%d", size);
1183                 thumb_dbg("frame = 0x%x", frame);
1184
1185                 if (frame == NULL || width == 0 || height == 0) {
1186                         thumb_err("Failed to get frame data");
1187                         mm_file_destroy_content_attrs(content);
1188                         return MS_MEDIA_ERR_INTERNAL;
1189                 }
1190
1191                 thumb_info->origin_width = width;
1192                 thumb_info->origin_height = height;
1193
1194                 err = _media_thumb_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
1195
1196                 if (width > thumb_width || height > thumb_height) {
1197                         err = _media_thumb_resize_data(frame,
1198                                                                                 width,
1199                                                                                 height,
1200                                                                                 MM_UTIL_IMG_FMT_RGB888,
1201                                                                                 thumb_info,
1202                                                                                 thumb_width,
1203                                                                                 thumb_height);
1204
1205                         if (err != MS_MEDIA_ERR_NONE) {
1206                                 thumb_err("_media_thumb_resize_data failed - %d", err);
1207                                 SAFE_FREE(thumb_info->data);
1208                                 mm_file_destroy_content_attrs(content);
1209                                 return err;
1210                         }
1211                 } else {
1212                         thumb_info->size = size;
1213                         thumb_info->width = width;
1214                         thumb_info->height = height;
1215                         thumb_info->data = malloc(size);
1216                         if (thumb_info->data == NULL) {
1217                                 thumb_err("memory allcation failed");
1218                                 mm_file_destroy_content_attrs(content);
1219                                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
1220                         }
1221                         memcpy(thumb_info->data, frame, size);
1222                 }
1223
1224                 mm_file_destroy_content_attrs(content);
1225                 if (rot_type == MM_UTIL_ROTATE_90 || rot_type == MM_UTIL_ROTATE_180 || rot_type == MM_UTIL_ROTATE_270) {
1226                         /* Start to decode to rotate */
1227                         unsigned char *rotated = NULL;
1228                         unsigned int r_w = thumb_info->height;
1229                         unsigned int r_h = thumb_info->width;
1230                         unsigned int r_size = 0;
1231
1232                         if (rot_type == MM_UTIL_ROTATE_180) {
1233                                 r_w = thumb_info->width;
1234                                 r_h = thumb_info->height;
1235                         }
1236
1237                         err = mm_util_get_image_size(MM_UTIL_IMG_FMT_RGB888, r_w, r_h, &r_size);
1238                         if (err != FILEINFO_ERROR_NONE) {
1239                                 thumb_err("mm_util_get_image_size failed : %d", err);
1240                                 SAFE_FREE(thumb_info->data);
1241                                 return err;
1242                         }
1243
1244                         //thumb_dbg("Size of Rotated : %d", r_size);
1245                         rotated = (unsigned char *)malloc(r_size);
1246                         err = mm_util_rotate_image(thumb_info->data, thumb_info->width, thumb_info->height,
1247                                                                                 MM_UTIL_IMG_FMT_RGB888,
1248                                                                                 rotated, &r_w, &r_h,
1249                                                                                 rot_type);
1250
1251                         if (err != FILEINFO_ERROR_NONE) {
1252                                 thumb_err("mm_util_rotate_image failed : %d", err);
1253                                 SAFE_FREE(thumb_info->data);
1254                                 SAFE_FREE(rotated);
1255                                 return err;
1256                         } else {
1257                                 thumb_dbg("mm_util_rotate_image succeed");
1258                         }
1259
1260                         SAFE_FREE(thumb_info->data);
1261                         thumb_info->data = rotated;
1262                         thumb_info->size = r_size;
1263                         thumb_info->width = r_w;
1264                         thumb_info->height = r_h;
1265                 }
1266
1267                 err = _media_thumb_convert_format(thumb_info, MEDIA_THUMB_RGB888, format);
1268                 if (err != MS_MEDIA_ERR_NONE) {
1269                         thumb_err("_media_thumb_convert_format falied: %d", err);
1270                         SAFE_FREE(thumb_info->data);
1271                         return err;
1272                 }
1273         } else {
1274                 thumb_dbg("no contents information");
1275                 frame = NULL;
1276                 mm_file_destroy_content_attrs(content);
1277
1278                 return MS_MEDIA_ERR_INTERNAL;
1279         }
1280
1281         return err;
1282 }
1283
1284 static char* _media_thumb_mmc_get_path(uid_t uid)
1285 {
1286         char *result_psswd = NULL;
1287         struct group *grpinfo = NULL;
1288         if (uid == getuid()) {
1289                 if (THUMB_MMC_PATH != NULL)
1290                         result_psswd = strndup(THUMB_MMC_PATH, strlen(THUMB_MMC_PATH));
1291                 grpinfo = getgrnam("users");
1292                 if (grpinfo == NULL) {
1293                         thumb_err("getgrnam(users) returns NULL !");
1294                         if (result_psswd)
1295                                 free(result_psswd);
1296                         return NULL;
1297                 }
1298         } else {
1299                 struct passwd *userinfo = getpwuid(uid);
1300                 if (userinfo == NULL) {
1301                         thumb_err("getpwuid(%d) returns NULL !", uid);
1302                         return NULL;
1303                 }
1304                 grpinfo = getgrnam("users");
1305                 if (grpinfo == NULL) {
1306                         thumb_err("getgrnam(users) returns NULL !");
1307                         return NULL;
1308                 }
1309                 // Compare git_t type and not group name
1310                 if (grpinfo->gr_gid != userinfo->pw_gid) {
1311                         thumb_err("UID [%d] does not belong to 'users' group!", uid);
1312                         return NULL;
1313                 }
1314                 asprintf(&result_psswd, "%s/share/media/.thumb/mmc", userinfo->pw_dir);
1315         }
1316
1317         return result_psswd;
1318 }
1319
1320 static char* _media_thumb_phone_get_path(uid_t uid)
1321 {
1322         char *result_psswd = NULL;
1323         struct group *grpinfo = NULL;
1324         if (uid == getuid()) {
1325                 if (THUMB_PHONE_PATH != NULL)
1326                         result_psswd = strndup(THUMB_PHONE_PATH, strlen(THUMB_PHONE_PATH));
1327                 grpinfo = getgrnam("users");
1328                 if (grpinfo == NULL) {
1329                         thumb_err("getgrnam(users) returns NULL !");
1330                         if (result_psswd)
1331                                 free(result_psswd);
1332                         return NULL;
1333                 }
1334         } else {
1335                 struct passwd *userinfo = getpwuid(uid);
1336                 if (userinfo == NULL) {
1337                         thumb_err("getpwuid(%d) returns NULL !", uid);
1338                         return NULL;
1339                 }
1340                 grpinfo = getgrnam("users");
1341                 if (grpinfo == NULL) {
1342                         thumb_err("getgrnam(users) returns NULL !");
1343                         return NULL;
1344                 }
1345                 // Compare git_t type and not group name
1346                 if (grpinfo->gr_gid != userinfo->pw_gid) {
1347                         thumb_err("UID [%d] does not belong to 'users' group!", uid);
1348                         return NULL;
1349                 }
1350                 asprintf(&result_psswd, "%s/share/media/.thumb/phone", userinfo->pw_dir);
1351         }
1352
1353         return result_psswd;
1354 }
1355
1356 int _media_thumb_get_hash_name(const char *file_full_path,
1357                                  char *thumb_hash_path, size_t max_thumb_path, uid_t uid)
1358 {
1359         char *hash_name = NULL;
1360         /*char *thumb_dir = NULL;*/
1361         char file_ext[255] = { 0 };
1362         char *get_path = NULL;
1363         int ret_len = 0;
1364         media_thumb_store_type store_type = -1;
1365
1366         if (file_full_path == NULL || thumb_hash_path == NULL || max_thumb_path <= 0) {
1367                 thumb_err("file_full_path==NULL || thumb_hash_path == NULL || max_thumb_path <= 0");
1368                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1369         }
1370
1371         _media_thumb_get_file_ext(file_full_path, file_ext, sizeof(file_ext));
1372
1373         store_type = _media_thumb_get_store_type_by_path(file_full_path);
1374         /*if (store_type == THUMB_PHONE) {
1375                 thumb_dir = _media_thumb_phone_get_path(uid);
1376         } else if (store_type == THUMB_MMC) {
1377                 thumb_dir = _media_thumb_mmc_get_path(uid);
1378         } else {
1379                 thumb_dir = _media_thumb_phone_get_path(uid);
1380         }*/
1381
1382         hash_name = _media_thumb_generate_hash_name(file_full_path);
1383         if (hash_name == NULL) {
1384                 thumb_err("_media_thumb_generate_hash_name fail");
1385                 return MS_MEDIA_ERR_INTERNAL;
1386         }
1387
1388         if (store_type == THUMB_PHONE) {
1389                 get_path = _media_thumb_phone_get_path(uid);
1390                 if (get_path != NULL)
1391                         ret_len = snprintf(thumb_hash_path, max_thumb_path - 1, "%s/.%s-%s.jpg", get_path, file_ext, hash_name);
1392         } else if (store_type == THUMB_MMC) {
1393                 get_path = _media_thumb_mmc_get_path(uid);
1394                 if (get_path != NULL)
1395                         ret_len = snprintf(thumb_hash_path, max_thumb_path - 1, "%s/.%s-%s.jpg", get_path, file_ext, hash_name);
1396         } else {
1397                 get_path = _media_thumb_phone_get_path(uid);
1398                 if (get_path != NULL)
1399                         ret_len = snprintf(thumb_hash_path, max_thumb_path - 1, "%s/.%s-%s.jpg", get_path, file_ext, hash_name);
1400         }
1401
1402         SAFE_FREE(get_path);
1403
1404         if ((ret_len < 0) || (ret_len > (int)max_thumb_path)) {
1405                 thumb_err("invalid hash path ret_len[%d]", ret_len);
1406                 return MS_MEDIA_ERR_INTERNAL;
1407         }
1408         //thumb_dbg("thumb hash : %s", thumb_hash_path);
1409
1410         return MS_MEDIA_ERR_NONE;
1411 }
1412
1413
1414 int _media_thumb_save_to_file_with_evas(unsigned char *data,
1415                                                                                         int w,
1416                                                                                         int h,
1417                                                                                         int alpha,
1418                                                                                         char *thumb_path)
1419 {
1420         Ecore_Evas *ee = ecore_evas_buffer_new(w, h);
1421         if (ee == NULL) {
1422                 thumb_err("ecore_evas_buffer_new failed");
1423                 return MS_MEDIA_ERR_INTERNAL;
1424         }
1425
1426         Evas *evas = ecore_evas_get(ee);
1427         if (evas == NULL) {
1428                 thumb_err("ecore_evas_get failed");
1429                 ecore_evas_free(ee);
1430                 return MS_MEDIA_ERR_INTERNAL;
1431         }
1432
1433         Evas_Object *img = NULL;
1434         img = evas_object_image_add(evas);
1435
1436         if (img == NULL) {
1437                 thumb_err("evas_object_image_add failed");
1438                 ecore_evas_free(ee);
1439                 return MS_MEDIA_ERR_INTERNAL;
1440         }
1441
1442         evas_object_image_colorspace_set(img, EVAS_COLORSPACE_ARGB8888);
1443         evas_object_image_size_set(img, w, h);
1444         evas_object_image_fill_set(img, 0, 0, w, h);
1445
1446         if (alpha) evas_object_image_alpha_set(img, 1);
1447
1448         evas_object_image_data_set(img, data);
1449         evas_object_image_data_update_add(img, 0, 0, w, h);
1450
1451         if (evas_object_image_save(img, thumb_path, NULL,       "quality=90 compress=1")) {
1452                 thumb_dbg("evas_object_image_save success");
1453                 ecore_evas_free(ee);
1454
1455                 return MS_MEDIA_ERR_NONE;
1456         } else {
1457                 thumb_err("evas_object_image_save failed");
1458                 ecore_evas_free(ee);
1459                 return MS_MEDIA_ERR_INTERNAL;
1460         }
1461 }