341c6a4874c4fb7732aaa22ac493df738d7e3d4e
[platform/core/api/media-content.git] / src / media_image.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17
18 #include <media_info_private.h>
19
20 #define media_content_retv_free_image_if(expr, val, p_str) do { \
21                                 if (expr) {     \
22                                         LOGE(FONT_COLOR_RED"Memory allocation failure"FONT_COLOR_RESET);        \
23                                         image_meta_destroy(p_str);      \
24                                         return (val);   \
25                                 }       \
26                         } while (0)
27
28
29 int image_meta_destroy(image_meta_h image)
30 {
31         image_meta_s *_image = (image_meta_s*)image;
32         media_content_retvm_if(_image == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Image handle is null");
33
34         SAFE_FREE(_image->media_id);
35         SAFE_FREE(_image->date_taken);
36         SAFE_FREE(_image->exposure_time);
37         SAFE_FREE(_image->model);
38         SAFE_FREE(_image);
39
40         return MEDIA_CONTENT_ERROR_NONE;
41 }
42
43 int image_meta_clone(image_meta_h *dst, image_meta_h src)
44 {
45         image_meta_s *_src = (image_meta_s*)src;
46         media_content_retvm_if(_src == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Source handle is null");
47
48         image_meta_s *_dst = (image_meta_s*)calloc(1, sizeof(image_meta_s));
49         media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
50
51         if (STRING_VALID(_src->media_id)) {
52                 _dst->media_id = strdup(_src->media_id);
53                 media_content_retv_free_image_if(_dst->media_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
54         }
55
56         if (STRING_VALID(_src->date_taken)) {
57                 _dst->date_taken = strdup(_src->date_taken);
58                 media_content_retv_free_image_if(_dst->date_taken == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
59         }
60
61         if (STRING_VALID(_src->exposure_time)) {
62                 _dst->exposure_time = strdup(_src->exposure_time);
63                 media_content_retv_free_image_if(_dst->exposure_time == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
64         }
65
66         if (STRING_VALID(_src->model)) {
67                 _dst->model = strdup(_src->model);
68                 media_content_retv_free_image_if(_dst->model == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, (image_meta_h)_dst);
69         }
70
71         _dst->fnumber = _src->fnumber;
72         _dst->iso = _src->iso;
73         _dst->width = _src->width;
74         _dst->height = _src->height;
75         _dst->orientation = _src->orientation;
76
77         *dst = (image_meta_h)_dst;
78
79         return MEDIA_CONTENT_ERROR_NONE;
80 }
81
82 int image_meta_get_media_id(image_meta_h image, char **media_id)
83 {
84         int ret = MEDIA_CONTENT_ERROR_NONE;
85         image_meta_s *_image = (image_meta_s*)image;
86
87         if (_image && media_id) {
88                 if (STRING_VALID(_image->media_id)) {
89                         char *new_string = strdup(_image->media_id);
90                         media_content_retvm_if(new_string == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
91
92                         *media_id = new_string;
93                 } else {
94                         *media_id = NULL;
95                 }
96                 ret = MEDIA_CONTENT_ERROR_NONE;
97
98         } else {
99                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
100                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
101         }
102
103         return ret;
104
105 }
106
107 int image_meta_get_width(image_meta_h image, int *width)
108 {
109         int ret = MEDIA_CONTENT_ERROR_NONE;
110         image_meta_s *_image = (image_meta_s*)image;
111
112         if (_image && width) {
113                 *width = _image->width;
114                 ret = MEDIA_CONTENT_ERROR_NONE;
115         } else {
116                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
117                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
118         }
119
120         return ret;
121 }
122 int image_meta_get_height(image_meta_h image, int *height)
123 {
124         int ret = MEDIA_CONTENT_ERROR_NONE;
125         image_meta_s *_image = (image_meta_s*)image;
126
127         if (_image && height) {
128                 *height = _image->height;
129                 ret = MEDIA_CONTENT_ERROR_NONE;
130         } else {
131                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
132                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
133         }
134
135         return ret;
136 }
137
138 int image_meta_get_orientation(image_meta_h image, media_content_orientation_e* orientation)
139 {
140         int ret = MEDIA_CONTENT_ERROR_NONE;
141         image_meta_s *_image = (image_meta_s*)image;
142
143         if (_image && orientation) {
144                 *orientation = _image->orientation;
145                 ret = MEDIA_CONTENT_ERROR_NONE;
146         } else {
147                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
148                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
149         }
150
151         return ret;
152 }
153
154 int image_meta_get_date_taken(image_meta_h image, char **date_taken)
155 {
156         int ret = MEDIA_CONTENT_ERROR_NONE;
157         image_meta_s *_image = (image_meta_s*)image;
158
159         if (_image && date_taken) {
160                 if (STRING_VALID(_image->date_taken)) {
161                         char *new_string = strdup(_image->date_taken);
162                         media_content_retvm_if(new_string == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
163
164                         *date_taken = new_string;
165                 } else {
166                         *date_taken = NULL;
167                 }
168
169                 ret = MEDIA_CONTENT_ERROR_NONE;
170         } else {
171                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
172                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
173         }
174
175         return ret;
176 }
177
178 int image_meta_get_exposure_time(image_meta_h image, char **exposure_time)
179 {
180         int ret = MEDIA_CONTENT_ERROR_NONE;
181         image_meta_s *_image = (image_meta_s*)image;
182
183         if (_image && exposure_time) {
184                 if (STRING_VALID(_image->exposure_time)) {
185                         *exposure_time = strdup(_image->exposure_time);
186                         media_content_retvm_if(*exposure_time == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
187                 } else {
188                         *exposure_time = NULL;
189                 }
190                 ret = MEDIA_CONTENT_ERROR_NONE;
191         } else {
192                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
193                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
194         }
195
196         return ret;
197 }
198
199 int image_meta_get_fnumber(image_meta_h image, double *fnumber)
200 {
201         int ret = MEDIA_CONTENT_ERROR_NONE;
202         image_meta_s *_image = (image_meta_s*)image;
203
204         if (_image && fnumber) {
205                 *fnumber = _image->fnumber;
206                 ret = MEDIA_CONTENT_ERROR_NONE;
207         } else {
208                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
209                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
210         }
211
212         return ret;
213 }
214
215 int image_meta_get_iso(image_meta_h image, int *iso)
216 {
217         int ret = MEDIA_CONTENT_ERROR_NONE;
218         image_meta_s *_image = (image_meta_s*)image;
219
220         if (_image && iso) {
221                 *iso = _image->iso;
222                 ret = MEDIA_CONTENT_ERROR_NONE;
223         } else {
224                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
225                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
226         }
227
228         return ret;
229 }
230
231 int image_meta_get_model(image_meta_h image, char **model)
232 {
233         int ret = MEDIA_CONTENT_ERROR_NONE;
234         image_meta_s *_image = (image_meta_s*)image;
235
236         if (_image && model) {
237                 if (STRING_VALID(_image->model)) {
238                         *model = strdup(_image->model);
239                         media_content_retvm_if(*model == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
240                 } else {
241                         *model = NULL;
242                 }
243                 ret = MEDIA_CONTENT_ERROR_NONE;
244         } else {
245                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
246                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
247         }
248
249         return ret;
250 }
251
252 int image_meta_set_orientation(image_meta_h image, media_content_orientation_e orientation)
253 {
254         int ret = MEDIA_CONTENT_ERROR_NONE;
255         media_content_warn("DEPRECATION WARNING: image_meta_set_orientation() is deprecated and will be removed from next release.");
256         image_meta_s *_image = (image_meta_s*)image;
257
258         if (_image == NULL) {
259                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
260                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
261         }
262
263         if ((orientation < MEDIA_CONTENT_ORIENTATION_NOT_AVAILABLE) || (orientation > MEDIA_CONTENT_ORIENTATION_ROT_270)) {
264                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
265                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
266         }
267
268         _image->orientation = orientation;
269
270         return ret;
271 }
272
273 int image_meta_update_to_db(image_meta_h image)
274 {
275         int ret = MEDIA_CONTENT_ERROR_NONE;
276         media_content_warn("DEPRECATION WARNING: image_meta_update_to_db() is deprecated and will be removed from next release.");
277         image_meta_s *_image = (image_meta_s*)image;
278         char *sql = NULL;
279         char *storage_id = NULL;
280
281         if (_image != NULL && STRING_VALID(_image->media_id)) {
282                 ret = _media_db_get_storage_id_by_media_id(_image->media_id, &storage_id);
283                 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
284
285                 sql = sqlite3_mprintf(UPDATE_IMAGE_META_FROM_MEDIA, storage_id, _image->orientation, _image->media_id);
286                 ret = _content_query_sql(sql);
287                 SQLITE3_SAFE_FREE(sql);
288                 SAFE_FREE(storage_id);
289         } else {
290                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
291                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
292         }
293
294         return ret;
295 }