2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <thumbnail_util.h>
18 #include <thumbnail_util_private.h>
22 #include <media-thumbnail.h>
23 #include <media-util.h>
27 #include <mm_util_magick.h>
31 #define MAX_PATH_SIZE 4096
33 int __thumbnail_util_replace_path(const char *path, char *replace_path)
35 if (!STRING_VALID(path)) {
36 thumbnail_util_error("Invalid path");
37 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
40 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL_OLD, strlen(MEDIA_ROOT_PATH_INTERNAL_OLD)) == 0) {
41 thumbnail_util_sec_debug("Old path[%s]", path);
42 snprintf(replace_path, MAX_PATH_SIZE, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(MEDIA_ROOT_PATH_INTERNAL_OLD));
44 snprintf(replace_path, MAX_PATH_SIZE, "%s", path);
47 if (!STRING_VALID(replace_path)) {
48 thumbnail_util_error("replace failed");
49 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
52 return THUMBNAIL_UTIL_ERROR_NONE;
55 int __thumbnail_util_error_capi(int internal_error)
57 switch (internal_error) {
58 case MS_MEDIA_ERR_NONE:
59 return THUMBNAIL_UTIL_ERROR_NONE;
60 case MS_MEDIA_ERR_INVALID_PARAMETER:
61 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
62 case MS_MEDIA_ERR_OUT_OF_MEMORY:
63 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
64 case MS_MEDIA_ERR_PERMISSION_DENIED:
65 return THUMBNAIL_UTIL_ERROR_PERMISSION_DENIED;
66 case MS_MEDIA_ERR_THUMB_TOO_BIG:
67 case MS_MEDIA_ERR_THUMB_UNSUPPORTED:
68 return THUMBNAIL_UTIL_ERROR_UNSUPPORTED_CONTENT;
70 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
74 void __thumbnail_util_convert_itoa(int request_id, char **req_str)
76 char buf[MAX_SIZE] = {0, };
78 snprintf(buf, MAX_SIZE, "%d", request_id);
79 *req_str = strndup(buf, strlen(buf));
81 void __thumbnail_util_extract_completed_cb(int error, int request_id, const char *path, int thumb_width, int thumb_height, unsigned char *thumb_data, int thumb_size, void *user_data)
83 thumbnail_extract_cb_s *_thumb_cb = (thumbnail_extract_cb_s *)user_data;
84 char *request_id_str = NULL;
86 if (_thumb_cb != NULL) {
87 __thumbnail_util_convert_itoa(request_id, &request_id_str);
88 if (_thumb_cb->thumb_extract_cb)
89 _thumb_cb->thumb_extract_cb(__thumbnail_util_error_capi(error), request_id_str, thumb_width, thumb_height, thumb_data, thumb_size, _thumb_cb->user_data);
90 SAFE_FREE(request_id_str);
95 int thumbnail_util_create(thumbnail_h *thumb)
97 int ret = THUMBNAIL_UTIL_ERROR_NONE;
98 thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_create() is deprecated and will be removed from next release.");
101 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
102 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
105 thumbnail_s *_thumb = (thumbnail_s *)malloc(sizeof(thumbnail_s));
106 if (_thumb == NULL) {
107 thumbnail_util_error("OUT_OF_MEMORY(0x%08x)", THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY);
108 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
111 _thumb->request_id = 0;
112 _thumb->file_path = NULL;
113 _thumb->dst_width = 0;
114 _thumb->dst_height = 0;
116 *thumb = (thumbnail_h)_thumb;
121 int thumbnail_util_extract(thumbnail_h thumb, thumbnail_extracted_cb callback, void *user_data, char **request_id)
123 int ret = THUMBNAIL_UTIL_ERROR_NONE;
124 thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_extract() is deprecated and will be removed from next release. Use thumbnail_util_extract_to_file() or thumbnail_util_extract_to_buffer() instead.");
126 static int g_thumbnail_req_id = 0;
127 thumbnail_s *_thumb = (thumbnail_s *)thumb;
129 if (_thumb != NULL && STRING_VALID(_thumb->file_path)) {
130 g_thumbnail_req_id++;
131 _thumb->request_id = g_thumbnail_req_id;
132 __thumbnail_util_convert_itoa(_thumb->request_id, request_id);
133 thumbnail_extract_cb_s *_thumb_cb = (thumbnail_extract_cb_s *)calloc(1, sizeof(thumbnail_extract_cb_s));
135 if (_thumb_cb != NULL) {
136 _thumb_cb->user_data = user_data;
137 _thumb_cb->thumb_extract_cb = callback;
140 if (_thumb->dst_width == 0 || _thumb->dst_height == 0) {
141 _thumb->dst_width = 320;
142 _thumb->dst_height = 240;
145 res = thumbnail_request_extract_raw_data_async(_thumb->request_id, _thumb->file_path, _thumb->dst_width, _thumb->dst_height, (ThumbRawFunc)__thumbnail_util_extract_completed_cb, (void *)_thumb_cb, tzplatform_getuid(TZ_USER_NAME));
146 ret = __thumbnail_util_error_capi(res);
147 if (ret != THUMBNAIL_UTIL_ERROR_NONE)
150 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
151 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
157 int thumbnail_util_set_path(thumbnail_h thumb, const char *path)
159 int ret = THUMBNAIL_UTIL_ERROR_NONE;
160 thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_set_path() is deprecated and will be removed from next release.");
161 char repl_path[MAX_PATH_SIZE + 1] = {0, };
162 thumbnail_s *_thumb = (thumbnail_s *)thumb;
164 if (_thumb != NULL && path != NULL) {
165 SAFE_FREE(_thumb->file_path);
166 memset(repl_path, 0, sizeof(repl_path));
167 ret = __thumbnail_util_replace_path(path, repl_path);
168 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
169 thumbnail_util_error("Convert path failed");
170 _thumb->file_path = NULL;
172 _thumb->file_path = strndup(repl_path, strlen(repl_path));
175 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
176 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
182 int thumbnail_util_set_size(thumbnail_h thumb, int width, int height)
184 int ret = THUMBNAIL_UTIL_ERROR_NONE;
185 thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_set_size() is deprecated and will be removed from next release.");
186 thumbnail_s *_thumb = (thumbnail_s *)thumb;
188 if (_thumb != NULL && width > 0 && height > 0) {
189 _thumb->dst_width = width;
190 _thumb->dst_height = height;
192 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
193 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
199 int thumbnail_util_cancel(thumbnail_h thumb, const char *request_id)
201 int ret = THUMBNAIL_UTIL_ERROR_NONE;
202 thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_cancel() is deprecated and will be removed from next release.");
203 thumbnail_s *_thumb = (thumbnail_s *)thumb;
205 if (_thumb != NULL && STRING_VALID(request_id)) {
206 unsigned int request_id_integer = atoi(request_id);
207 ret = thumbnail_request_cancel_raw_data(request_id_integer);
209 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
210 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
216 int thumbnail_util_destroy(thumbnail_h thumb)
218 thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_destroy() is deprecated and will be removed from next release.");
219 int ret = THUMBNAIL_UTIL_ERROR_NONE;
220 thumbnail_s *_thumb = (thumbnail_s *)thumb;
223 SAFE_FREE(_thumb->file_path);
226 thumbnail_util_error("INVALID_PARAMETER(0x%08x)", THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
227 ret = THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
233 ////////////////////////////////////////// Sync
235 static void __thumbnail_util_destroy_thumb_data(thumbnail_data_s *thumb)
237 SAFE_FREE(thumb->path);
238 SAFE_FREE(thumb->thumbnail_path);
239 SAFE_FREE(thumb->buffer);
243 static void __thumbnail_util_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h, unsigned int *thumb_w, unsigned int *thumb_h)
245 bool portrait = false;
248 thumbnail_util_retm_if(orig_w == 0, "Invalid orig_w");
249 thumbnail_util_retm_if(orig_h == 0, "Invalid orig_h");
250 thumbnail_util_retm_if(!thumb_w, "Invalid thumb_w");
251 thumbnail_util_retm_if(!thumb_h, "Invalid thumb_h");
256 /* Set smaller length to default size */
258 if (orig_w < *thumb_w)
260 ratio = (double)orig_h / (double)orig_w;
261 *thumb_h = *thumb_w * ratio;
263 if (orig_h < *thumb_h)
265 ratio = (double)orig_w / (double)orig_h;
266 *thumb_w = *thumb_h * ratio;
269 thumbnail_util_debug("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
273 static int __thumbnail_util_extract_video(thumbnail_data_s *thumb)
275 int ret = THUMBNAIL_UTIL_ERROR_NONE;
276 MMHandleType content = NULL;
277 MMHandleType tag = NULL;
279 int video_track_num = 0;
281 unsigned int width = 0;
282 unsigned int height = 0;
283 unsigned int thumb_width = 0;
284 unsigned int thumb_height = 0;
286 mm_util_image_h img = NULL;
288 thumbnail_util_retvm_if(thumb == NULL, THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER, "Data is NULL");
290 thumb_width = thumb->width;
291 thumb_height = thumb->height;
294 ret = mm_file_create_tag_attrs(&tag, thumb->path);
295 if (ret == FILEINFO_ERROR_NONE) {
296 ret = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
297 if (ret != FILEINFO_ERROR_NONE)
303 ret = mm_file_destroy_tag_attrs(tag);
304 if (ret != FILEINFO_ERROR_NONE) {
305 thumbnail_util_error("fail to free tag attr - err(%x)", ret);
308 thumbnail_util_warn("CDIS vlaue[%d]", cdis_value);
310 ret = mm_file_create_content_attrs_safe(&content, thumb->path);
312 ret = mm_file_create_content_attrs(&content, thumb->path);
314 thumbnail_util_retvm_if(ret != FILEINFO_ERROR_NONE, THUMBNAIL_UTIL_ERROR_INVALID_OPERATION, "mm_file_create_content_attrs fails");
317 ret = mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &video_track_num, NULL);
318 if (ret != FILEINFO_ERROR_NONE) {
319 thumbnail_util_error("mm_file_get_attrs fails : %d", ret);
323 if (video_track_num > 0) {
324 ret = mm_file_get_attrs(content,
325 MM_FILE_CONTENT_VIDEO_WIDTH,
327 MM_FILE_CONTENT_VIDEO_HEIGHT,
329 MM_FILE_CONTENT_VIDEO_THUMBNAIL, &frame,
332 if (ret != FILEINFO_ERROR_NONE) {
333 thumbnail_util_error("mm_file_get_attrs fails : %d", ret);
337 thumbnail_util_debug("W[%d] H[%d] Size[%d] Frame[%p]", width, height, size, frame);
339 if (frame == NULL || width == 0 || height == 0) {
340 thumbnail_util_error("Failed to get frame data");
344 ret = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, frame, size, &img);
345 if (ret != MM_UTIL_ERROR_NONE) {
346 thumbnail_util_error("Failed to mm_image_create_image");
350 /* check thumb size */
351 __thumbnail_util_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
353 if (thumb->extract_type == THUMBNAIL_UTIL_FILE) {
354 ret = mm_util_resize_B_P(img, thumb_width, thumb_height, thumb->thumbnail_path);
355 mm_image_destroy_image(img);
356 if (ret != MM_UTIL_ERROR_NONE)
359 mm_util_image_h res_img = NULL;
360 unsigned char *res_buf = NULL;
361 unsigned int res_width = 0;
362 unsigned int res_height = 0;
363 size_t res_buf_size = 0;
364 mm_util_color_format_e res_format = MM_UTIL_COLOR_NUM;
366 ret = mm_util_resize_B_B(img, thumb_width, thumb_height, &res_img);
367 mm_image_destroy_image(img);
368 if (ret != MM_UTIL_ERROR_NONE)
370 ret = mm_image_get_image(res_img, &res_width, &res_height, &res_format, &res_buf, &res_buf_size);
371 mm_image_destroy_image(res_img);
372 if (ret != MM_UTIL_ERROR_NONE)
375 thumb->buffer = malloc(res_buf_size * sizeof(unsigned char));
376 if (thumb->buffer != NULL) {
377 memcpy(thumb->buffer, res_buf, res_buf_size);
378 thumb->buffer_size = res_buf_size;
379 thumb->width = res_width;
380 thumb->height = res_height;
390 mm_file_destroy_content_attrs(content);
391 return THUMBNAIL_UTIL_ERROR_NONE;
393 mm_file_destroy_content_attrs(content);
394 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
397 static int __thumbnail_util_extract(thumbnail_data_s *thumb)
399 int ret = THUMBNAIL_UTIL_ERROR_NONE;
400 unsigned int orig_width = 0;
401 unsigned int orig_height = 0;
402 unsigned int thumb_width = 0;
403 unsigned int thumb_height = 0;
404 mm_util_img_codec_type type = IMG_CODEC_UNKNOWN_TYPE;
406 thumbnail_util_retvm_if(thumb == NULL, THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER, "Data is NULL");
408 if (thumb->media_type == THUMBNAIL_UTIL_IMAGE) {
409 thumb_width = thumb->width;
410 thumb_height = thumb->height;
411 /* Get original resolution */
412 ret = mm_util_extract_image_info(thumb->path, &type, &orig_width, &orig_height);
413 thumbnail_util_retv_if(ret != MM_UTIL_ERROR_NONE, THUMBNAIL_UTIL_ERROR_INVALID_OPERATION);
415 __thumbnail_util_get_proper_thumb_size(orig_width, orig_height, &thumb_width, &thumb_height);
417 if (thumb->extract_type == THUMBNAIL_UTIL_FILE) {
418 ret = mm_util_resize_P_P(thumb->path, thumb_width, thumb_height, thumb->thumbnail_path);
419 thumbnail_util_retv_if(ret != MM_UTIL_ERROR_NONE, THUMBNAIL_UTIL_ERROR_INVALID_OPERATION);
422 mm_util_image_h res_img = NULL;
423 unsigned char *buf = NULL;
424 unsigned int width = 0;
425 unsigned int height = 0;
427 mm_util_color_format_e format = MM_UTIL_COLOR_NUM;
429 ret = mm_util_resize_P_B(thumb->path, thumb_width, thumb_height, MM_UTIL_COLOR_BGRA, &res_img);
430 thumbnail_util_retv_if(ret != MM_UTIL_ERROR_NONE, THUMBNAIL_UTIL_ERROR_INVALID_OPERATION);
432 ret = mm_image_get_image(res_img, &width, &height, &format, &buf, &buf_size);
433 mm_image_destroy_image(res_img);
434 thumbnail_util_retv_if(ret != MM_UTIL_ERROR_NONE, THUMBNAIL_UTIL_ERROR_INVALID_OPERATION);
436 thumb->buffer = malloc(buf_size * sizeof(unsigned char));
437 if (thumb->buffer != NULL) {
438 memcpy(thumb->buffer, buf, buf_size);
439 thumb->buffer_size = buf_size;
440 thumb->width = width;
441 thumb->height = height;
444 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
450 ret = __thumbnail_util_extract_video(thumb);
451 thumbnail_util_retvm_if(ret != THUMBNAIL_UTIL_ERROR_NONE, ret, "__thumbnail_util_extract_video failed");
454 return THUMBNAIL_UTIL_ERROR_NONE;
457 int __thumbnail_util_get_file_ext(const char *file_path, char *file_ext, int max_len)
461 for (i = (int)strlen(file_path); i >= 0; i--) {
462 if ((file_path[i] == '.') && (i < (int)strlen(file_path))) {
463 strncpy(file_ext, &file_path[i + 1], max_len);
464 return THUMBNAIL_UTIL_ERROR_NONE;
467 /* meet the dir. no ext */
468 if (file_path[i] == '/')
469 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
472 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
475 static int __thumbnail_util_check_media_type(const char *path, thumbnail_util_media_type_e *type)
477 int ret = THUMBNAIL_UTIL_ERROR_NONE;
478 char mimetype[255] = {0,};
479 const char *unsupported_type = "image/tiff";
480 const char *supported_type = "application/vnd.ms-asf";
482 // Check file is existed
483 thumbnail_util_retv_if(path == NULL, THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
484 if (access(path, R_OK) < 0) {
485 if (errno == EACCES || errno == EPERM) {
486 thumbnail_util_error("Fail to open path: Permission Denied [%s]", path);
487 return THUMBNAIL_UTIL_ERROR_PERMISSION_DENIED;
489 thumbnail_util_error("Fail to open path: Invalid Path [%s]", path);
490 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
495 ret = aul_get_mime_from_file(path, mimetype, sizeof(mimetype));
496 thumbnail_util_retvm_if(ret < 0, THUMBNAIL_UTIL_ERROR_INVALID_OPERATION, "aul_get_mime_from_file failed");
498 thumbnail_util_debug("mime type : %s", mimetype);
500 /* categorize from mimetype */
501 if (strstr(mimetype, "image") != NULL) {
502 thumbnail_util_retvm_if(!strcmp(mimetype, unsupported_type), THUMBNAIL_UTIL_ERROR_UNSUPPORTED_CONTENT, "This is unsupport file type");
503 *type = THUMBNAIL_UTIL_IMAGE;
504 return THUMBNAIL_UTIL_ERROR_NONE;
505 } else if (strstr(mimetype, "video") != NULL) {
506 *type = THUMBNAIL_UTIL_VIDEO;
507 return THUMBNAIL_UTIL_ERROR_NONE;
508 } else if (strstr(mimetype, supported_type) != NULL) {
509 *type = THUMBNAIL_UTIL_VIDEO;
510 return THUMBNAIL_UTIL_ERROR_NONE;
513 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
516 static bool __thumbnail_util_is_support_img(const char *path)
518 int ret = THUMBNAIL_UTIL_ERROR_NONE;
519 mm_util_img_codec_type t = IMG_CODEC_UNKNOWN_TYPE;
523 ret = mm_util_extract_image_info(path, &t, &w, &h);
524 if (ret != MM_UTIL_ERROR_NONE || t == IMG_CODEC_UNKNOWN_TYPE)
530 int thumbnail_util_extract_to_buffer(const char *path, unsigned int width, unsigned int height, unsigned char **thumb_buffer, size_t *thumb_size, unsigned int *thumb_width, unsigned int *thumb_height)
532 int ret = THUMBNAIL_UTIL_ERROR_NONE;
533 thumbnail_data_s *thumb = NULL;
534 thumbnail_util_media_type_e type = -1;
536 thumbnail_util_retvm_if(!STRING_VALID(path), THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER, "Wrong path");
537 thumbnail_util_retvm_if((width > 2000 || width == 0) || (height > 2000 || width == 0), THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER, "Wrong width/height");
538 thumbnail_util_retvm_if(thumb_buffer == NULL || thumb_size == NULL || thumb_width == NULL || thumb_height == NULL, THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER, "Out param is NULL");
540 /* check media type */
541 ret = __thumbnail_util_check_media_type(path, &type);
542 thumbnail_util_retvm_if(ret != THUMBNAIL_UTIL_ERROR_NONE, ret, "__thumbnail_util_check_media_type failed");
544 /* If image, check support format */
545 if (type == THUMBNAIL_UTIL_IMAGE) {
546 if (__thumbnail_util_is_support_img(path) == false) {
547 thumbnail_util_error("This image format is not supported");
548 return THUMBNAIL_UTIL_ERROR_UNSUPPORTED_CONTENT;
552 thumb = calloc(1, sizeof(thumbnail_data_s));
553 thumb->extract_type = THUMBNAIL_UTIL_BUFFER;
554 thumb->media_type = type;
555 thumb->path = g_strdup(path);
556 thumb->width = width;
557 thumb->height = height;
559 if (thumb->path == NULL) {
560 __thumbnail_util_destroy_thumb_data(thumb);
561 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
564 ret = __thumbnail_util_extract(thumb);
565 if (ret != THUMBNAIL_UTIL_ERROR_NONE) {
566 thumbnail_util_error("Extract failed");
568 *thumb_buffer = malloc(thumb->buffer_size);
569 memcpy(*thumb_buffer, thumb->buffer, thumb->buffer_size);
570 *thumb_size = thumb->buffer_size;
571 *thumb_width = thumb->width;
572 *thumb_height = thumb->height;
575 __thumbnail_util_destroy_thumb_data(thumb);
580 int thumbnail_util_extract_to_file(const char *path, unsigned int width, unsigned int height, const char *thumbnail_path)
582 int ret = THUMBNAIL_UTIL_ERROR_NONE;
583 char *check_str = NULL;
584 thumbnail_data_s *thumb = NULL;
585 thumbnail_util_media_type_e type = -1;
587 thumbnail_util_retvm_if(!STRING_VALID(path), THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER, "Wrong path");
588 thumbnail_util_retvm_if((width > 2000 || width == 0) || (height > 2000 || height == 0), THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER, "Wrong width/height");
589 thumbnail_util_retvm_if(!STRING_VALID(thumbnail_path), THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER, "Wrong thumbnail_path");
591 /* check media type */
592 ret = __thumbnail_util_check_media_type(path, &type);
593 thumbnail_util_retvm_if(ret != THUMBNAIL_UTIL_ERROR_NONE, ret, "__thumbnail_util_check_media_type failed");
595 /* If image, check support format */
596 if (type == THUMBNAIL_UTIL_IMAGE) {
597 if (__thumbnail_util_is_support_img(path) == false) {
598 thumbnail_util_error("This image format is not supported");
599 return THUMBNAIL_UTIL_ERROR_UNSUPPORTED_CONTENT;
603 /* check thumbnail path is writable */
604 check_str = g_path_get_dirname(thumbnail_path);
605 if (check_str != NULL) {
606 if (access(check_str, W_OK) != 0) {
607 thumbnail_util_error("No permission to write[%s]", check_str);
608 SAFE_FREE(check_str);
609 return THUMBNAIL_UTIL_ERROR_PERMISSION_DENIED;
611 SAFE_FREE(check_str);
615 /* If video file, thumbnail extension is only JPEG */
616 if (type == THUMBNAIL_UTIL_VIDEO) {
617 char ext[255] = { 0 };
618 ret = __thumbnail_util_get_file_ext(thumbnail_path, ext, sizeof(ext));
619 thumbnail_util_retvm_if(ret != THUMBNAIL_UTIL_ERROR_NONE, ret, "__thumbnail_util_get_file_ext failed");
620 if (strcasecmp(ext, "JPG") != 0 && strcasecmp(ext, "JPEG") != 0) {
621 thumbnail_util_error("Wrong file name[%s]", thumbnail_path);
622 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
626 thumb = calloc(1, sizeof(thumbnail_data_s));
627 thumb->extract_type = THUMBNAIL_UTIL_FILE;
628 thumb->media_type = type;
629 thumb->path = g_strdup(path);
630 thumb->width = width;
631 thumb->height = height;
632 thumb->thumbnail_path = g_strdup(thumbnail_path);
634 if (thumb->path == NULL || thumb->thumbnail_path == NULL) {
635 __thumbnail_util_destroy_thumb_data(thumb);
636 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
639 ret = __thumbnail_util_extract(thumb);
640 if (ret != THUMBNAIL_UTIL_ERROR_NONE)
641 thumbnail_util_error("Extract failed");
643 __thumbnail_util_destroy_thumb_data(thumb);