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