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