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>
21 #include <media-thumbnail.h>
22 #include <media-util.h>
27 static int __thumbnail_util_error_capi(int internal_error)
29 switch (internal_error) {
30 case MS_MEDIA_ERR_NONE:
31 return THUMBNAIL_UTIL_ERROR_NONE;
32 case MS_MEDIA_ERR_INVALID_PARAMETER:
33 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
34 case MS_MEDIA_ERR_OUT_OF_MEMORY:
35 return THUMBNAIL_UTIL_ERROR_OUT_OF_MEMORY;
36 case MS_MEDIA_ERR_PERMISSION_DENIED:
37 return THUMBNAIL_UTIL_ERROR_PERMISSION_DENIED;
38 case MS_MEDIA_ERR_THUMB_TOO_BIG:
39 case MS_MEDIA_ERR_THUMB_UNSUPPORTED:
40 return THUMBNAIL_UTIL_ERROR_UNSUPPORTED_CONTENT;
42 return THUMBNAIL_UTIL_ERROR_INVALID_OPERATION;
46 int thumbnail_util_create(thumbnail_h *thumb)
48 thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_create() is deprecated and will be removed from next release.");
49 return THUMBNAIL_UTIL_ERROR_NONE;
52 int thumbnail_util_extract(thumbnail_h thumb, thumbnail_extracted_cb callback, void *user_data, char **request_id)
54 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.");
55 return THUMBNAIL_UTIL_ERROR_NONE;
58 int thumbnail_util_set_path(thumbnail_h thumb, const char *path)
60 thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_set_path() is deprecated and will be removed from next release.");
61 return THUMBNAIL_UTIL_ERROR_NONE;
64 int thumbnail_util_set_size(thumbnail_h thumb, int width, int height)
66 thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_set_size() is deprecated and will be removed from next release.");
67 return THUMBNAIL_UTIL_ERROR_NONE;
70 int thumbnail_util_cancel(thumbnail_h thumb, const char *request_id)
72 thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_cancel() is deprecated and will be removed from next release.");
73 return THUMBNAIL_UTIL_ERROR_NONE;
76 int thumbnail_util_destroy(thumbnail_h thumb)
78 thumbnail_util_warn("DEPRECATION WARNING: thumbnail_util_destroy() is deprecated and will be removed from next release.");
79 return THUMBNAIL_UTIL_ERROR_NONE;
82 static int __thumbnail_util_check_media_type(const char *path, thumbnail_util_media_type_e *type)
84 int ret = THUMBNAIL_UTIL_ERROR_NONE;
85 char mimetype[255] = {0,};
86 const char *unsupported_type = "image/tiff";
87 const char *supported_type = "application/vnd.ms-asf";
89 // Check file is existed
90 thumbnail_util_retv_if(path == NULL, THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER);
91 if (access(path, R_OK) < 0) {
92 if (errno == EACCES || errno == EPERM) {
93 thumbnail_util_error("Fail to open path: Permission Denied [%s]", path);
94 return THUMBNAIL_UTIL_ERROR_PERMISSION_DENIED;
96 thumbnail_util_error("Fail to open path: Invalid Path [%s]", path);
97 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
102 ret = aul_get_mime_from_file(path, mimetype, sizeof(mimetype));
103 thumbnail_util_retvm_if(ret < 0, THUMBNAIL_UTIL_ERROR_INVALID_OPERATION, "aul_get_mime_from_file failed");
105 thumbnail_util_debug("mime type : %s", mimetype);
107 /* categorize from mimetype */
108 if (strstr(mimetype, "image") != NULL) {
109 thumbnail_util_retvm_if(!strcmp(mimetype, unsupported_type), THUMBNAIL_UTIL_ERROR_UNSUPPORTED_CONTENT, "This is unsupport file type");
110 *type = THUMBNAIL_UTIL_IMAGE;
111 return THUMBNAIL_UTIL_ERROR_NONE;
112 } else if (strstr(mimetype, "video") != NULL) {
113 *type = THUMBNAIL_UTIL_VIDEO;
114 return THUMBNAIL_UTIL_ERROR_NONE;
115 } else if (strstr(mimetype, supported_type) != NULL) {
116 *type = THUMBNAIL_UTIL_VIDEO;
117 return THUMBNAIL_UTIL_ERROR_NONE;
120 return THUMBNAIL_UTIL_ERROR_UNSUPPORTED_CONTENT;
123 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)
125 int ret = THUMBNAIL_UTIL_ERROR_NONE;
126 thumbnail_util_media_type_e type;
128 /* check media type */
129 ret = __thumbnail_util_check_media_type(path, &type);
130 thumbnail_util_retvm_if(ret != THUMBNAIL_UTIL_ERROR_NONE, ret, "__thumbnail_util_check_media_type failed");
132 /* If image, check support format */
133 if (type == THUMBNAIL_UTIL_IMAGE)
134 ret = create_image_thumbnail_to_buffer(path, width, height, thumb_buffer, thumb_size, thumb_width, thumb_height);
136 ret = create_video_thumbnail_to_buffer(path, width, height, thumb_buffer, thumb_size, thumb_width, thumb_height);
138 return __thumbnail_util_error_capi(ret);
141 int thumbnail_util_extract_to_file(const char *path, unsigned int width, unsigned int height, const char *thumbnail_path)
143 int ret = THUMBNAIL_UTIL_ERROR_NONE;
144 thumbnail_util_media_type_e type;
146 /* check media type */
147 ret = __thumbnail_util_check_media_type(path, &type);
148 thumbnail_util_retvm_if(ret != THUMBNAIL_UTIL_ERROR_NONE, ret, "__thumbnail_util_check_media_type failed");
150 /* If image, check support format */
151 if (type == THUMBNAIL_UTIL_IMAGE) {
152 ret = create_image_thumbnail_to_file(path, width, height, thumbnail_path, false);
154 if (!g_regex_match_simple("[^/]\\.jpe?g$", thumbnail_path, G_REGEX_CASELESS, 0)) {
155 thumbnail_util_error("Unsupported path or extensions [%s]", thumbnail_path);
156 return THUMBNAIL_UTIL_ERROR_INVALID_PARAMETER;
159 ret = create_video_thumbnail_to_file(path, width, height, thumbnail_path, false);
162 return __thumbnail_util_error_capi(ret);