2 * Copyright (c) 2014 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.
18 #include <media_info_private.h>
22 static int __media_face_check_media_id(const char *media_id)
24 int ret = MEDIA_CONTENT_ERROR_NONE;
25 char *query_str = NULL;
26 sqlite3_stmt *stmt = NULL;
29 media_content_retvm_if(!STRING_VALID(media_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid media_id");
32 query_str = sqlite3_mprintf(SELECT_IMAGE_COUNT_FROM_MEDIA_BY_ID, media_id);
33 ret = _content_query_prepare(&stmt, query_str, NULL, NULL);
34 SQLITE3_SAFE_FREE(query_str);
35 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
37 if (sqlite3_step(stmt) == SQLITE_ROW)
38 item_count = (int)sqlite3_column_int(stmt, 0);
40 SQLITE3_FINALIZE(stmt);
42 media_content_retvm_if(item_count == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
44 return MEDIA_CONTENT_ERROR_NONE;
47 int media_face_destroy(media_face_h face)
49 media_face_s *_face = (media_face_s*)face;
51 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
53 SAFE_FREE(_face->media_id);
54 SAFE_FREE(_face->face_tag);
57 return MEDIA_CONTENT_ERROR_NONE;
60 int media_face_clone(media_face_h *dst, media_face_h src)
62 media_face_s *_src = (media_face_s*)src;
64 media_content_retvm_if(src == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid src handle");
66 media_face_s *_dst = (media_face_s *)calloc(1, sizeof(media_face_s));
67 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
69 if (STRING_VALID(_src->media_id)) {
70 _dst->media_id = strdup(_src->media_id);
71 if (_dst->media_id == NULL) {
72 media_face_destroy((media_face_h)_dst);
73 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
74 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
78 _dst->face_id = _src->face_id;
79 _dst->face_rect_x = _src->face_rect_x;
80 _dst->face_rect_y = _src->face_rect_y;
81 _dst->face_rect_w = _src->face_rect_w;
82 _dst->face_rect_h = _src->face_rect_h;
83 _dst->orientation = _src->orientation;
85 if (STRING_VALID(_src->face_tag)) {
86 _dst->face_tag = strdup(_src->face_tag);
87 if (_dst->face_tag == NULL) {
88 media_face_destroy((media_face_h)_dst);
89 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
90 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
94 *dst = (media_face_h)_dst;
96 return MEDIA_CONTENT_ERROR_NONE;
99 static void __media_face_convert_itoa(int face_id, char **face_strid)
101 char buf[MAX_SIZE] = {0, };
103 snprintf(buf, MAX_SIZE, "%d", face_id);
104 *face_strid = strndup(buf, strlen(buf));
107 int media_face_get_face_id(media_face_h face, char **face_id)
109 media_face_s* _face = (media_face_s*)face;
111 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
113 if (_face->face_id > 0) {
114 __media_face_convert_itoa(_face->face_id, face_id);
115 media_content_retvm_if(*face_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
120 return MEDIA_CONTENT_ERROR_NONE;
123 int media_face_get_media_id(media_face_h face, char **media_id)
125 media_face_s* _face = (media_face_s*)face;
127 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
129 if (STRING_VALID(_face->media_id)) {
130 *media_id = strdup(_face->media_id);
131 media_content_retvm_if(*media_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
136 return MEDIA_CONTENT_ERROR_NONE;
139 int media_face_get_face_rect(media_face_h face, unsigned int *rect_x, unsigned int *rect_y, unsigned int *rect_w, unsigned int *rect_h)
141 media_face_s* _face = (media_face_s*)face;
143 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
144 media_content_retvm_if(!(rect_x && rect_y && rect_w && rect_h), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect");
146 *rect_x = _face->face_rect_x;
147 *rect_y = _face->face_rect_y;
148 *rect_w = _face->face_rect_w;
149 *rect_h = _face->face_rect_h;
151 return MEDIA_CONTENT_ERROR_NONE;
154 int media_face_get_orientation(media_face_h face, media_content_orientation_e *orientation)
156 media_face_s* _face = (media_face_s*)face;
158 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
159 media_content_retvm_if(orientation == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid orientation");
161 *orientation = _face->orientation;
163 return MEDIA_CONTENT_ERROR_NONE;
166 int media_face_get_tag(media_face_h face, char **tag)
168 media_face_s* _face = (media_face_s*)face;
170 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
172 if (STRING_VALID(_face->face_tag)) {
173 *tag = strdup(_face->face_tag);
174 media_content_retvm_if(*tag == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
179 return MEDIA_CONTENT_ERROR_NONE;
182 int media_face_create(const char *media_id, media_face_h *face)
184 int ret = MEDIA_CONTENT_ERROR_NONE;
186 media_content_retvm_if(!STRING_VALID(media_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid media_id");
187 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
189 ret = __media_face_check_media_id(media_id);
190 media_content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "media_id does not exist or is not an image");
192 media_face_s* _face = calloc(1, sizeof(media_face_s));
193 media_content_retvm_if(_face == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "Out of memory");
195 _face->media_id = strdup(media_id);
197 *face = (media_face_h)_face;
199 return MEDIA_CONTENT_ERROR_NONE;
202 int media_face_set_face_rect(media_face_h face, unsigned int rect_x, unsigned int rect_y, unsigned int rect_w, unsigned int rect_h)
204 media_face_s* _face = (media_face_s*)face;
206 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
207 media_content_retvm_if(rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
208 media_content_retvm_if(rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
210 _face->face_rect_x = rect_x;
211 _face->face_rect_y = rect_y;
212 _face->face_rect_w = rect_w;
213 _face->face_rect_h = rect_h;
215 return MEDIA_CONTENT_ERROR_NONE;
218 int media_face_set_orientation(media_face_h face, media_content_orientation_e orientation)
220 media_face_s* _face = (media_face_s*)face;
222 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
224 _face->orientation = orientation;
226 return MEDIA_CONTENT_ERROR_NONE;
229 int media_face_set_tag(media_face_h face, const char *tag)
231 media_face_s* _face = (media_face_s*)face;
233 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
235 if (STRING_VALID(tag))
236 _face->face_tag = strdup(tag);
238 _face->face_tag = NULL;
240 return MEDIA_CONTENT_ERROR_NONE;
243 int media_face_insert_to_db(media_face_h face)
245 int ret = MEDIA_CONTENT_ERROR_NONE;
246 char *query_str = NULL;
247 sqlite3_stmt *stmt = NULL;
249 media_face_s* _face = (media_face_s*)face;
251 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
252 media_content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
253 media_content_retvm_if(_face->face_rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
254 media_content_retvm_if(_face->face_rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
256 query_str = sqlite3_mprintf(INSERT_FACE_TO_FACE, _face->media_id, _face->face_rect_x, _face->face_rect_y, _face->face_rect_w, _face->face_rect_h, _face->orientation, _face->face_tag);
258 ret = _content_query_sql(query_str);
259 SQLITE3_SAFE_FREE(query_str);
261 query_str = sqlite3_mprintf(SELECT_FACE_ID, _face->media_id, _face->face_rect_x, _face->face_rect_y, _face->face_rect_w, _face->face_rect_h, _face->orientation);
262 ret = _content_query_prepare(&stmt, query_str, NULL, NULL);
263 SQLITE3_SAFE_FREE(query_str);
264 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
266 if (sqlite3_step(stmt) == SQLITE_ROW)
267 _face->face_id = (int)sqlite3_column_int(stmt, 0);
269 SQLITE3_FINALIZE(stmt);
275 int media_face_update_to_db(media_face_h face)
277 int ret = MEDIA_CONTENT_ERROR_NONE;
278 char *query_str = NULL;
280 media_face_s* _face = (media_face_s*)face;
282 media_content_retvm_if(face == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid handle");
283 media_content_retvm_if(_face->face_id == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid face_id");
284 media_content_retvm_if(_face->media_id == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid media_id");
285 media_content_retvm_if(_face->face_rect_w == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_w");
286 media_content_retvm_if(_face->face_rect_h == 0, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid rect_h");
288 query_str = sqlite3_mprintf(UPDATE_FACE_TO_FACE, _face->face_rect_x, _face->face_rect_y, _face->face_rect_w, _face->face_rect_h, _face->orientation, _face->face_tag, _face->face_id);
290 ret = _content_query_sql(query_str);
291 SQLITE3_SAFE_FREE(query_str);
296 static int __media_face_safe_atoi(const char *buffer, int *si)
300 media_content_retvm_if(buffer == NULL || si == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid parameter");
302 const long sl = strtol(buffer, &end, 10);
304 media_content_retvm_if(end == buffer, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "not a decimal number");
305 media_content_retvm_if('\0' != *end, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "extra characters at end of input: %s", end);
306 media_content_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "out of range of type long");
307 media_content_retvm_if(sl > INT_MAX, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "greater than INT_MAX");
308 media_content_retvm_if(sl < INT_MIN, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "less than INT_MIN");
312 return MEDIA_CONTENT_ERROR_NONE;
315 int media_face_delete_from_db(const char *face_id)
317 int ret = MEDIA_CONTENT_ERROR_NONE;
318 char *query_str = NULL;
319 int query_face_id = 0;
320 media_content_retvm_if(!STRING_VALID(face_id), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid face_id");
322 ret = __media_face_safe_atoi(face_id, &query_face_id);
323 media_content_retvm_if(ret != MEDIA_CONTENT_ERROR_NONE, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid face_id");
325 /*Update modified_time to 0.. It will restore the deleted face when media_info_start_face_detection() is called */
326 query_str = sqlite3_mprintf(UPDATE_MEDIA_INFO_IN_FACE_SCAN_LIST, query_face_id);
327 ret = _content_query_sql(query_str);
328 SQLITE3_SAFE_FREE(query_str);
329 media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
331 query_str = sqlite3_mprintf(DELETE_FACE_FROM_FACE, query_face_id);
332 ret = _content_query_sql(query_str);
333 SQLITE3_SAFE_FREE(query_str);
338 int media_face_get_face_count_from_db(filter_h filter, int *face_count)
340 int ret = MEDIA_CONTENT_ERROR_NONE;
342 if (face_count == NULL) {
343 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
344 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
347 ret = _media_db_get_group_count(filter, MEDIA_GROUP_FACE, face_count);
352 int media_face_foreach_face_from_db(filter_h filter, media_face_cb callback, void *user_data)
354 int ret = MEDIA_CONTENT_ERROR_NONE;
356 media_content_retvm_if(callback == NULL, MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid paramter");
358 ret = _media_db_get_face(NULL, filter, callback, user_data);