2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 <image_util.h>
21 #include "camera_utils.h"
22 #include "cam_debug.h"
23 #include "cam_error.h"
28 #define JPEG_8M_APPROXIMATELY_SIZE 2096000
29 #define JPEG_W6M_APPROXIMATELY_SIZE 1699000
30 #define JPEG_5M_APPROXIMATELY_SIZE 1436000
31 #define JPEG_W4M_APPROXIMATELY_SIZE 1210000
32 #define JPEG_3M_APPROXIMATELY_SIZE 849500
33 #define JPEG_2M_APPROXIMATELY_SIZE 566000
34 #define JPEG_1M_APPROXIMATELY_SIZE 283000
35 #define JPEG_VGA_APPROXIMATELY_SIZE 135000
37 #define CLIPING(data) ((data) < 0 ? 0 : ((data) > 255) ? 255 : (data))
38 /*#define SUPPORT_WINK //use wink library */
40 void static YuvToRgb(int Y, int U, int V, int *R, int *G, int *B)
42 *B = CLIPING((76284 * (Y - 16) + 132252 * (U - 128)) >> 16);
43 *G = CLIPING((76284 * (Y - 16) - 53281 * (V - 128) -
44 25625 * (U - 128)) >> 16);
45 *R = CLIPING((76284 * (Y - 16) + 104595 * (V - 128)) >> 16);
48 #define FAKE_YUV_IMAGE_WIDTH 640
49 #define FAKE_YUV_IMAGE_HEIGHT 480
50 #define FAKE_YUV_IMAGE_PATH PREFIX"/share/camera/fake.yuv"
52 /* #define SUPPORT_WINK */
53 #define CAPTUERD_IMAGE_SAVE_PATH "/tmp/captured_image.jpg"
56 #define YUV422_SIZE(width, height) ((width) * (height) * 2)
57 #endif /* YUV422_SIZE */
60 #define YUV420_SIZE(width, height) ((width) * (height) * 3 / 2)
61 #endif /* YUV420_SIZE */
67 char *m_mmc_path = NULL;
69 /*get the torchlight on/off value*/
70 gboolean cam_utils_check_torchlight_status(void *data)
72 struct appdata *ad = (struct appdata *)data;
73 cam_retv_if(ad == NULL, FALSE);
74 int ret = -1; /*error, success is 0*/
76 ret = vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TORCH_LIGHT, &key_value); /*ret [-1: error, 0:succes], key_value[on:1 , off:0]*/
77 DEBUG_TRACE("VCONFKEY_SETAPPL_ACCESSIBILITY_TORCH_LIGHT is changed to %d", key_value);
79 DEBUG_TRACE("vconf_get_int failed");
83 /*torchlight is working*/
84 ad->torchlight_on = TRUE;
86 ad->torchlight_on = FALSE;
91 /*get the battery warning low state*/
92 gboolean cam_utils_check_battery_warning_low(void)
96 if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &low_status)) {
97 cam_debug(LOG_UI, "battery status low = %d", low_status);
98 if (low_status <= VCONFKEY_SYSMAN_BAT_WARNING_LOW)
101 cam_warning(LOG_UI, "get setting failed %s",
102 VCONFKEY_SYSMAN_BATTERY_STATUS_LOW);
107 /*get the battery critical low state*/
108 gboolean cam_utils_check_battery_critical_low(void)
112 if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &low_status)) {
113 cam_debug(LOG_UI, "battery status low = %d", low_status);
114 if (low_status <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
117 cam_warning(LOG_UI, "get setting failed %s",
118 VCONFKEY_SYSMAN_BAT_CRITICAL_LOW);
125 int cam_utils_get_battery_level(void)
127 debug_fenter(LOG_SYS);
129 gint battery_level = -1;
131 /* VCONF_BATTERY_LEVEL or VCONFKEY_INDICATOR_MODULE_BATTERY */
132 if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, &battery_level)) {
133 cam_info(LOG_SYS, "battery_level = %d", battery_level);
134 return battery_level;
136 cam_info(LOG_SYS, "get setting failed %s",
137 VCONFKEY_SYSMAN_BATTERY_CAPACITY);
140 return battery_level;
143 gint cam_utils_get_charging_status(void)
145 debug_fenter(LOG_SYS);
149 /* VCONF_BATTERY_CHARGE_NOW or VCONFKEY_INDICATOR_MODULE_CHARGE */
150 if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &charging)) {
151 cam_info(LOG_SYS, "charging = %d", charging);
153 cam_info(LOG_SYS, "get setting failed %s",
154 VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW);
160 guint64 cam_system_get_remain_rec_time(void *data)
162 struct appdata *ad = (struct appdata *)data;
166 CamAppData *camapp = ad->camapp_handle;
167 if (camapp == NULL) {
170 guint64 remain_time = 0;
172 if (camapp->camera_mode == CAM_CAMCORDER_MODE) {
173 gint64 free_space = 0;
174 guint a_bitrate = 0, v_bitrate = 0;
175 gchar* target_path = (gchar*)cam_app_get_target_path();
176 if (target_path == NULL) {
179 free_space = cam_get_free_space(target_path);
181 a_bitrate = cam_app_get_aenc_bitrate(ad);
182 v_bitrate = cam_app_get_venc_bitrate(ad, camapp->video_quality);
185 (guint64)((free_space * 8) / (a_bitrate + v_bitrate));
191 gint64 cam_system_get_still_count_by_resolution(void *data)
193 debug_fenter(LOG_CAM);
195 struct appdata *ad = (struct appdata *)data;
199 CamAppData *camapp = ad->camapp_handle;
200 if (camapp == NULL) {
204 gint64 free_space = 0;
205 gint64 remained_count = 0;
206 gint64 avg_file_size = -1;
207 gchar* target_path = (gchar*)cam_app_get_target_path();
208 if (target_path == NULL) {
211 free_space = cam_get_free_space(target_path);
212 DEBUG_TRACE("FREE SPACE =%d", free_space);
213 free_space = (free_space - STILL_MINIMAL_SPACE);
215 /* TODO: Quality factor should be calculated later!
217 switch (camapp->resolution) {
218 case CAM_RESOLUTION_3264x2448:
219 avg_file_size = JPEG_8M_APPROXIMATELY_SIZE;
222 case CAM_RESOLUTION_3264x1960:
223 case CAM_RESOLUTION_3264x1836:
224 avg_file_size = JPEG_W6M_APPROXIMATELY_SIZE;
227 case CAM_RESOLUTION_2800x1920:
228 case CAM_RESOLUTION_2560x1920:
229 avg_file_size = JPEG_5M_APPROXIMATELY_SIZE;
232 case CAM_RESOLUTION_2560x1536:
233 avg_file_size = JPEG_W4M_APPROXIMATELY_SIZE;
236 case CAM_RESOLUTION_2560x1440:
237 case CAM_RESOLUTION_2048x1536:
238 avg_file_size = JPEG_3M_APPROXIMATELY_SIZE;
241 case CAM_RESOLUTION_1920x1080:
242 case CAM_RESOLUTION_1600x1200:
243 avg_file_size = JPEG_2M_APPROXIMATELY_SIZE;
246 case CAM_RESOLUTION_1280x720:
247 case CAM_RESOLUTION_1280x960:
248 avg_file_size = JPEG_1M_APPROXIMATELY_SIZE;
251 case CAM_RESOLUTION_SVGA:
252 avg_file_size = 150 * 1024 * 2;
255 case CAM_RESOLUTION_WVGA:
256 avg_file_size = 130 * 1024 * 2;
259 case CAM_RESOLUTION_VGA:
260 avg_file_size = JPEG_VGA_APPROXIMATELY_SIZE;
263 case CAM_RESOLUTION_WQVGA:
264 avg_file_size = 100 * 1024 * 2;
267 case CAM_RESOLUTION_QVGA:
268 avg_file_size = 100 * 1024 * 1.5;
271 case CAM_RESOLUTION_CIF:
272 avg_file_size = 25 * 1024 * 2;
276 critical_msg(LOG_CAM, "unhandled resolution:%dx%d",
277 HIWORD(camapp->resolution),
278 LOWORD(camapp->resolution));
282 remained_count = free_space / avg_file_size;
283 DEBUG_TRACE("FREE SPACE =%d", free_space);
284 DEBUG_TRACE("avg_file_size =%d", avg_file_size);
285 DEBUG_TRACE("remained_count =%d", remained_count);
287 return (int)MAX(remained_count, 0);
290 gint64 cam_get_free_space(const gchar *path)
293 if (-1 == statfs(path, &fs))
295 return (gint64) fs.f_bsize * fs.f_bavail;
299 gboolean cam_utils_set_guide_rect_color(void *data) {
301 struct appdata *ad = (struct appdata *)data;
302 CamAppData *camapp = NULL;
304 cam_retv_if(ad == NULL, FALSE);
305 camapp = ad->camapp_handle;
306 cam_retv_if(camapp == NULL, FALSE);
310 int capture_video_format = CAMERA_PIXEL_FORMAT_INVALID;
311 cam_mm_get_video_source_format(NULL, &capture_video_format);
312 DEBUG_TRACE("capture_video_format =%d", capture_video_format);
314 switch (capture_video_format) {
315 case CAMERA_PIXEL_FORMAT_INVALID:
317 cam_debug(LOG_UI,"can not set preview format & shapshot format");
320 case CAMERA_PIXEL_FORMAT_UYVY:
321 /*case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:*//*TODO:there is no this value in capi*/
323 camapp->guide_rect_green = UYVY_GUIDE_RECT_GREEN;
324 camapp->guide_rect_orange = UYVY_GUIDE_RECT_ORANGE;
325 camapp->guide_rect_red = UYVY_GUIDE_RECT_RED;
326 camapp->guide_rect_white = UYVY_GUIDE_RECT_WHITE;
329 case CAMERA_PIXEL_FORMAT_YUYV:
331 camapp->guide_rect_green= YUYV_GUIDE_RECT_GREEN;
332 camapp->guide_rect_orange= YUYV_GUIDE_RECT_ORANGE;
333 camapp->guide_rect_red= YUYV_GUIDE_RECT_RED;
334 camapp->guide_rect_white = YUYV_GUIDE_RECT_WHITE;
338 cam_debug(LOG_UI,"can not set preview format & shapshot format");
347 gint64 cam_get_capacity_space(const gchar *path)
350 if (-1 == statfs(path, &fs))
352 return (gint64) fs.f_bsize * fs.f_blocks;
358 cam_utils_draw_guide_rectangle(void *data, void *frame, int x_org, int y_org,
359 int width, int height)
361 struct appdata *ad = (struct appdata *)data;
362 CamAppData *camapp = NULL;
364 cam_retm_if(ad == NULL, "appdata is NULL");
365 camapp = ad->camapp_handle;
366 cam_retm_if(camapp == NULL, "cam_handle is NULL");
368 int left, right, top, bottom, offset, y;
369 int xStart, xEnd, draw_width;
371 if (width == 0 && height == 0) {
372 cam_debug(LOG_UI, " width,height of rectangle are zero");
376 left = 2 * (x_org / 2); /* to draw on YUV422 or YUV420, we must start at even x */
377 right = left + 2 * (width / 2);
379 bottom = y_org + height;
381 int preview_w = 0; /* make sure this is even */
383 gboolean ret = FALSE;
385 ret = cam_mm_get_video_size( &preview_w, &preview_h);
386 cam_ret_if(ret == FALSE || preview_w == 0 || preview_h == 0);
388 xStart = MAX(left, 0);
389 xEnd = MIN(right, preview_w);
391 draw_width = xEnd - xStart; /* for top and bottom */
395 int line_thickness = FIT_TO_RESOLUTION(2, 4); /* make sure this is even */
398 int frame_size = preview_w * preview_h;
400 guchar guide_rect_colorY =
401 (guchar) ((camapp->guide_rect_color >> 8) & 0xFF);
402 guchar guide_rect_colorU = (guchar) ((camapp->guide_rect_color) & 0xFF);
403 guchar guide_rect_colorV =
404 (guchar) ((camapp->guide_rect_color >> 16) & 0xFF);
406 top = 2 * (top / 2); /* to draw on YUV420, we must start at even y */
407 bottom = 2 * (bottom / 2);
410 for (y = MAX(top, 0); y < top + line_thickness && y < preview_h; y += 2) { /* line_thickness, and preview_h must be even */
411 offset = y * preview_w + xStart;
412 memset(((guchar *) frame) + offset, guide_rect_colorY,
414 memset(((guchar *) frame) + offset + preview_w,
415 guide_rect_colorY, draw_width);
416 offset = y * preview_w / 4 + xStart / 2;
417 memset(((guchar *) frame) + frame_size + offset,
418 guide_rect_colorU, draw_width / 2);
419 memset(((guchar *) frame) + frame_size + frame_size / 4 +
420 offset, guide_rect_colorV, draw_width / 2);
424 for (y = MAX(bottom - line_thickness, 0);
425 y < bottom && y < preview_h; y += 2) {
426 offset = y * preview_w + xStart;
427 memset(((guchar *) frame) + offset, guide_rect_colorY,
429 memset(((guchar *) frame) + offset + preview_w,
430 guide_rect_colorY, draw_width);
431 offset = y * preview_w / 4 + xStart / 2;
432 memset(((guchar *) frame) + frame_size + offset,
433 guide_rect_colorU, draw_width / 2);
434 memset(((guchar *) frame) + frame_size + frame_size / 4 +
435 offset, guide_rect_colorV, draw_width / 2);
439 if (left + line_thickness > 0 && left < preview_w) {
440 draw_width = MIN(MIN(line_thickness, line_thickness + left), preview_w - left); /* now calculated for left */
441 for (y = MAX(top, 0); y < bottom && y < preview_h; y += 2) {
442 offset = y * preview_w + xStart;
443 memset(((guchar *) frame) + offset, guide_rect_colorY,
445 memset(((guchar *) frame) + offset + preview_w,
446 guide_rect_colorY, draw_width);
447 offset = y * preview_w / 4 + xStart / 2;
448 memset(((guchar *) frame) + frame_size + offset,
449 guide_rect_colorU, draw_width / 2);
450 memset(((guchar *) frame) + frame_size +
451 frame_size / 4 + offset, guide_rect_colorV,
456 if (right > 0 && right - line_thickness < preview_w) {
457 draw_width = MIN(MIN(line_thickness, right), preview_w - right + line_thickness); /* now calculated for right */
458 for (y = MAX(top, 0); y < bottom && y < preview_h; y += 2) {
459 offset = y * preview_w + MAX(right - line_thickness, 0);
460 memset(((guchar *) frame) + offset, guide_rect_colorY,
462 memset(((guchar *) frame) + offset + preview_w,
463 guide_rect_colorY, draw_width);
465 y * preview_w / 4 + MAX(right - line_thickness,
467 memset(((guchar *) frame) + frame_size + offset,
468 guide_rect_colorU, draw_width / 2);
469 memset(((guchar *) frame) + frame_size +
470 frame_size / 4 + offset, guide_rect_colorV,
479 for (y = top; y < top + line_thickness && y < preview_h; y++) {
482 offset = y * preview_w + xStart;
483 for (x = 0; x < draw_width; x += 2) {
484 ((gulong *) frame)[(offset + x) / 2] =
485 camapp->guide_rect_color;
490 for (y = bottom - line_thickness; y < bottom && y < preview_h; y++) {
493 offset = y * preview_w + xStart;
494 for (x = 0; x < draw_width; x += 2) {
495 ((gulong *) frame)[(offset + x) / 2] =
496 camapp->guide_rect_color;
501 if (left + line_thickness >= 0 && left < preview_w) {
502 draw_width = MIN(MIN(line_thickness, line_thickness + left), preview_w - left); /* now calculated for left */
503 for (y = MAX(top, 0); y < bottom && y < preview_h; y++) {
504 offset = y * preview_w + xStart;
505 for (x = 0; x < draw_width; x += 2) {
506 ((gulong *) frame)[(offset + x) / 2] =
507 camapp->guide_rect_color;
512 if (right >= 0 && right - line_thickness < preview_w) {
513 draw_width = MIN(MIN(line_thickness, right), preview_w - right + line_thickness); /* now calculated for right */
514 for (y = MAX(top, 0); y < bottom && y < preview_h; y++) {
515 offset = y * preview_w + MAX(right - line_thickness, 0);
516 for (x = 0; x < draw_width; x += 2) {
517 ((gulong *) frame)[(offset + x) / 2] =
518 camapp->guide_rect_color;
527 cam_utils_convert_YUYV_to_UYVY(unsigned char *dst, unsigned char *src,
532 memset(dst, 0x00, length);
533 memcpy(dst, src + 1, length - 1);
535 for (i = 0; i < length; i++) {
543 void cam_utils_convert_UYVY_to_YUYV(char *dst, char *src, gint length)
547 memset(dst, 0x00, length);
548 memcpy((char *)dst + 1, (char *)src, length - 1);
550 for (i = 0; i < length; i++) {
551 if ((i % 2)) { /* even */
561 cam_utils_convert_YUYV_to_YUV420P(unsigned char *pInBuf, unsigned char *pOutBuf,
562 int width, int height)
564 unsigned char *pInY, *pInU, *pInV;
565 unsigned char *pOutY, *pOutU, *pOutV;
567 int nRowIters = height / 2;
568 int nColIters = width / 2;
576 pOutU = pOutBuf + width * height;
577 pOutV = pOutBuf + width * height * 5 / 4;
579 /* Iterate over half the number of rows, because inside there are 2 loops on columns */
580 for (rows = 0; rows < nRowIters; rows++) {
582 Iterate over half the number of columns, copy 2 pixels each time */
583 for (cols = 0; cols < nColIters; cols++) {
584 /* Copy Y of first pixel */
588 /* Copy Y of second pixel */
592 /* Copy U of all 4 pixels */
596 /* Copy V of all 4 pixels */
602 Iterate over half the number of columns, copy 2 pixels each time */
603 for (cols = 0; cols < nColIters; cols++) {
604 /* Copy Y of third pixel */
608 /* Copy Y of fourth pixel */
613 /* Skip U, V of third, fourth pixel */
622 cam_utils_convert_YUYV_to_YUV420P(byte *src, byte *dst, gint width, gint height)
625 int YUYV_length = 0, YUV420P_length = 0;
626 int temp_length = 0, temp_length2 = 0;
627 int pos_u = 0, pos_v = 0;
629 /* cam_debug( LOG_UI, " start" ); */
631 temp_length = width * height;
632 temp_length2 = temp_length >> 2;
633 YUYV_length = temp_length << 1;
634 YUV420P_length = (temp_length * 3) >> 1;
637 cam_debug( LOG_UI, " size[%dx%d], length YUYV[%d],YUV420P[%d]",
638 width, height, YUYV_length, YUV420P_length );
642 cam_critical(LOG_SYS, " dst is NULL");
646 for (i = 0; i < temp_length; i++) {
648 dst[i] = src[i << 1];
650 if (i % (width << 1) < width) {
653 dst[temp_length + pos_u] = src[(i << 1) + 1];
657 dst[temp_length + temp_length2 + pos_v] =
667 void *cam_utils_YUV422_to_ARGB(byte *frame, int width, int height)
669 /* source yuv is FOURCC YUYV, sampling format YUV 422 .
671 Byte Ordering (lowest byte) Y0, U0, Y1, V0 */
672 byte *frame_argb = malloc(width * height * 4); /* for ARGB */
674 if (frame_argb == NULL) {
677 memset(frame_argb,0,width * height * 4);
679 int i = 0, j = 0; /* row, column */
682 unsigned long pixel_idx = 0, rgb_index = 0;
686 for (i = 0; i < height; i++) {
687 for (j = 0; j < width; j++) {
689 if (j % 2) { /* odd */
692 /* u_idx = -1; v_idx = 1; */
696 /* u_idx = 1; v_idx = 3; */
699 y = frame[pixel_idx];
700 v = frame[pixel_idx + v_idx];
701 u = frame[pixel_idx + u_idx];
703 YuvToRgb(y, u, v, &r, &g, &b);
706 frame_argb[rgb_index++] = (byte)CLIPING(r);
707 frame_argb[rgb_index++] = (byte)CLIPING(g);
708 frame_argb[rgb_index++] = (byte)CLIPING(b);
709 frame_argb[rgb_index++] = 0xff;
711 pixel_idx += 2; /* yuv422, 4byte is 2 pixel */
715 return (void *)frame_argb;
718 void *cam_utils_IYUV_to_ARGB(byte *frame, int width, int height)
720 /* source yuv is FOURCC IYUV or I420, sampling format YUV 420. */
721 /* IYUV format is http://www.fourcc.org/yuv.php#IYUV */
723 byte *frame_argb = malloc(width * height * 4); /* for ARGB */
724 if (frame_argb == NULL) {
727 memset(frame_argb,0,width * height * 4);
729 int h = 0, w = 0; /* row, column */
732 unsigned long rgb_index = 0; /* ,pixel_idx=0; */
735 for (h = 0; h < height; h++) {
736 for (w = 0; w < width; w++) {
738 y = frame[h * height + w];
739 u = frame[(width * height) + idx];
740 v = frame[(width * height) + ((width * height) / 4) +
746 YuvToRgb(y, u, v, &r, &g, &b);
749 frame_argb[rgb_index++] = (byte)CLIPING(r);
750 frame_argb[rgb_index++] = (byte)CLIPING(g);
751 frame_argb[rgb_index++] = (byte)CLIPING(b);
752 frame_argb[rgb_index++] = 0;
756 if ((w == width - 1) && (!(h % 2)))
760 return (void *)frame_argb;
764 cam_utils_save_to_jpg_file(int storage_id, gchar *filename, void *frame,
765 int width, int height, GError **error)
767 cam_debug(LOG_MM, " [%dx%d] %s", width, height, filename);
769 int ret = CAMERA_ERROR_NONE;
771 CAM_TA_ACUM_ITEM_BEGIN("cam_utils_check_mmc_for_writing", 0);
772 if (storage_id == CAM_STORAGE_EXTERNAL) {
773 if (!cam_utils_check_mmc_for_writing(error)) {
777 CAM_TA_ACUM_ITEM_END("cam_utils_check_mmc_for_writing", 0);
779 cam_debug(LOG_SYS, " \n\n\n\n\n START JPEG ENCODING \n\n\n\n\n");
781 CAM_TA_ACUM_ITEM_BEGIN("image_util_encode_jpeg", 0);
782 ret = image_util_encode_jpeg(frame, width, height,
783 IMAGE_UTIL_COLORSPACE_YUYV, 90, filename);
784 CAM_TA_ACUM_ITEM_END("image_util_encode_jpeg", 0);
786 cam_debug(LOG_SYS, " \n\n\n\n\n END JPEG ENCODING \n\n\n\n\n");
789 cam_critical(LOG_MM, " image_util_encode_jpeg Failed [%x]", ret);
797 cam_utils_save_to_jpg_memory(byte **memory, unsigned int *size, void *src_frame,
798 int width, int height)
800 cam_debug(LOG_MM, " [%dx%d]", width, height);
802 int ret = CAMERA_ERROR_NONE;
804 cam_debug(LOG_SYS, " \n\n\n\n\n START JPEG ENCODING \n\n\n\n\n");
806 CAM_TA_ACUM_ITEM_BEGIN("image_util_encode_jpeg_to_memory", 0);
807 ret = image_util_encode_jpeg_to_memory(src_frame, width, height,
808 IMAGE_UTIL_COLORSPACE_YUYV, 90, (unsigned char **)memory, size);
809 CAM_TA_ACUM_ITEM_END("image_util_encode_jpeg_to_memory", 0);
811 cam_debug(LOG_SYS, " \n\n\n\n\n END JPEG ENCODING \n\n\n\n\n");
814 cam_critical(LOG_MM, "image_util_encode_jpeg_to_memory Failed [%x]", ret);
821 void *cam_utils_load_temp_file(gchar *filepath, gint *pfilesize)
823 struct stat fileinfo;
826 stat(filepath, &fileinfo);
828 gint filesize = fileinfo.st_size;
830 cam_critical(LOG_CAM, "can't get file infomation");
834 cam_debug(LOG_CAM, "temp file's file_path =%s, file_size =%d",
837 void *data = (void *)malloc(filesize);
842 memset(data,0,filesize);
844 cam_debug(LOG_CAM, "data = %p ", data);
846 if ((fp = fopen(filepath, "r")) == NULL) {
848 cam_critical(LOG_CAM, "can't open file infomation");
851 if (fread(data, filesize, 1, fp) != 1) {
854 cam_critical(LOG_CAM, "can't read file infomation");
858 *pfilesize = filesize;
865 gboolean cam_utils_check_mmc_for_writing(GError **error)
869 const gchar *error_msg = NULL;
872 m_mmc_path = (char *)cam_file_get_external_image_path();
874 if (!g_file_test(m_mmc_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
875 error_code = CAM_ERROR_STORAGE_UNAVAILABLE;
876 error_msg = _("IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
880 if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_state)) {
881 if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED) {
882 error_code = CAM_ERROR_STORAGE_UNAVAILABLE;
883 error_msg = _("IDS_CAM_POP_MEMORY_CARD_REMOVED");
895 *error = cam_error_new_literal(error_code, error_msg);/*note:fix warnning*/
902 gboolean cam_utils_check_mmc_for_inserted_stats(void *data)
905 g_return_val_if_fail(data, FALSE);
907 struct appdata *ad = (struct appdata *)data;
908 cam_retv_if(ad == NULL, FALSE);
910 CamAppData *camapp = NULL;
911 camapp = ad->camapp_handle;
913 camapp = ad->camapp_handle;
914 cam_retv_if(camapp == NULL, FALSE);
916 if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_state)) {
917 if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED
918 && camapp->storage == CAM_STORAGE_EXTERNAL) {
926 int cam_utils_check_mmc_status(void)
930 if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status)) {
931 cam_warning(LOG_SYS, " Get MMC status failed");
937 int cam_utils_get_storage_id_from_filepath(const char *filepath)
939 char *strtemp = NULL;
941 strtemp = (char *)cam_file_get_internal_image_path();
944 if (!strncmp(filepath, strtemp, sizeof(strtemp)))
945 return CAM_STORAGE_INTERNAL;
948 strtemp = (char *)cam_file_get_external_image_path();
950 if (!strncmp(filepath, strtemp, sizeof(strtemp)))
951 return CAM_STORAGE_EXTERNAL;
954 return CAM_STORAGE_DCF;
959 cam_utils_safety_file_copy(const char *dst, const char *src, GError **error)
963 const gchar *error_msg = NULL;
968 cam_debug(LOG_FILE, " dst file name = %s", dst);
969 cam_debug(LOG_FILE, " dst file name = %s", src);
971 fp_src = fopen(src, "r");
972 if (fp_src == NULL) {
973 error_code = CAM_ERROR_FILE_NOT_EXISTS;
974 error_msg = _("IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
978 cam_debug(LOG_FILE, "start check mmc");
980 if (cam_utils_get_storage_id_from_filepath(dst) ==
981 CAM_STORAGE_EXTERNAL) {
982 if (!cam_utils_check_mmc_for_writing(error))
986 cam_debug(LOG_FILE, "end check mmc");
988 fp_dst = fopen(dst, "wb");
989 if (fp_dst == NULL) {
990 error_code = CAM_ERROR_FILE_REGISTER_FAILED;
992 _("IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
995 cam_debug(LOG_FILE, "start copy");
997 unsigned long filesize = 0;
998 unsigned long filecopyblocksize = 1024 * 512;
999 unsigned long copysize = 0;
1000 struct stat fileinfo;
1002 void *buff = malloc(filecopyblocksize); /* 512k */
1006 /* get source file size */
1007 stat(src, &fileinfo);
1009 filesize = fileinfo.st_size;
1010 cam_debug(LOG_FILE, "source file size %lu", filesize);
1015 if (filesize >= filecopyblocksize) {
1016 copysize = filecopyblocksize;
1018 copysize = filesize;
1021 cam_debug(LOG_FILE, "copy size %lu",
1023 if (fread(buff, copysize, 1, fp_src) != 1) {
1024 cam_debug(LOG_FILE, "read error!");
1033 && fwrite(buff, copysize, 1,
1036 "file read or write error");
1037 error_code = CAM_ERROR_FILE;
1040 ("IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
1048 filesize -= copysize;
1050 } while (filesize > 0);
1058 /*fsync(fp_dst->_fileno);*//*block for increasing formance of shot-to-shot */
1070 cam_debug(LOG_FILE, "file copy error, error number=%x", error_code);
1081 *error = cam_error_new_literal(error_code, error_msg);/*note:fix warnning*/
1088 static char *cam_utils_trim(char *str)
1092 for (; *i_buf && isspace(*i_buf); ++i_buf) ;
1097 gboolean cam_utils_parse_args(int argc, char *argv[], CamExeArgs *args)
1100 const char *delimiters = ",";
1105 for (index = 0; index < argc; index++) {
1109 if (argv[index][0] == '-' && argv[index][1] != '\0') {
1110 char *r_argv = strdup(cam_utils_trim((argv[index] + 2)));
1111 switch (argv[index][1]) {
1113 case 'd': /* daemon launch */
1116 CAM_LAUNCHING_MODE_DAEMON;
1120 case 'h': /* help */
1123 CAM_LAUNCHING_MODE_HELP;
1128 case 'v': /* review */
1130 args->review = TRUE;
1133 case 'r': /* resolution */
1136 char *result = NULL;
1137 char *save_string = NULL;
1138 result = strtok_r(r_argv, delimiters,&save_string);
1140 args->width = atoi(result);
1141 result = strtok_r(NULL, delimiters,&save_string);
1143 args->height = atoi(result);
1149 case 'm': /* cam_mode */
1152 if (0 == strcmp(r_argv, "IMAGE")) {
1153 args->cam_mode = CAM_CAMERA_MODE;
1154 } else if (0 == strcmp(r_argv, "VIDEO")) {
1155 args->cam_mode = CAM_CAMCORDER_MODE;
1157 if (r_argv != NULL) {
1168 case '-': /* launching appl.'s name */
1171 char *qualifier_to_find = "calling-app=";
1172 char *qualifier_field_start = NULL;
1175 qualifier_field_start = strstr(r_argv, qualifier_to_find);
1176 if (qualifier_field_start) {
1177 qualifier_field_start += strlen(qualifier_to_find);
1178 name = cam_utils_trim(qualifier_field_start);
1179 args->caller = strdup(name);
1181 cam_debug(LOG_SYS, "unknown arg : %s", argv[index]);
1188 case 'l': /* size limit */
1191 args->size_limit = atoi(r_argv);
1197 default: /* unknown option */
1198 cam_debug(LOG_SYS, "Unknown Option : %c",
1202 if (r_argv != NULL) {
1207 cam_debug(LOG_SYS, "unknown arg : %s", argv[index]);
1211 cam_debug(LOG_SYS, "cam_mode : %d", args->cam_mode);
1212 cam_debug(LOG_SYS, "resolution : [%dx%d]", args->width, args->height);
1213 cam_debug(LOG_SYS, "limit : %d", args->size_limit);
1214 cam_debug(LOG_SYS, "caller : %s", args->caller);
1218 gboolean cam_utils_check_wide_resolution(int resol_w, int resol_h)
1220 if ((ABS((gfloat)((resol_w * 3.0) / (resol_h * 4.0)) - 1.0 ) < CAM_EPSINON )
1221 || (ABS((gfloat)((resol_w * 25.0) / (resol_h * 36.0)) - 1.0) < CAM_EPSINON)) {
1222 cam_debug(LOG_UI, "Not Wide Resolution : [%d]x[%d]", resol_w,
1230 gboolean cam_utils_check_fake_image_exist(const char *fake_image_path)
1232 if (!g_file_test(fake_image_path, G_FILE_TEST_EXISTS)) {
1239 int cam_utils_captured_image_select(void *data, const char *file_path)
1241 cam_retv_if(data == NULL, FALSE);
1242 struct appdata *ad = (struct appdata *)data;
1243 CamAppData *camapp = NULL;
1244 camapp = ad->camapp_handle;
1245 cam_retv_if(camapp == NULL, FALSE);
1251 cam_utils_image_rotate(char *src, int src_width, int src_height, char *dst,
1252 int *dst_width, int *dst_height, int degree)
1255 cam_retv_if(src == NULL || dst == NULL || src_width == 0
1256 || src_height == 0, FALSE);
1257 cam_retv_if(degree != 0 && degree != 90 && degree != 180
1258 && degree != 270, FALSE);
1263 int *from = (int *)src;
1264 int *to = (int *)dst;
1267 *dst_width = src_width;
1268 *dst_height = src_height;
1269 memcpy(dst, src, src_width * src_height * 4);
1273 for (j = 0; j < src_height; j++) {
1274 for (i = 0; i < src_width; i++) {
1277 *dst_width = src_height;
1278 *dst_height = src_width;
1280 *(to + i * src_height + (src_height - j)) =
1281 *(from + j * src_width + i);
1282 } else if (degree == 180) {
1283 *dst_width = src_width;
1284 *dst_height = src_height;
1286 *(to + (src_height - j) * src_width +
1288 *(from + j * src_width + i);
1289 } else if (degree == 270) {
1290 *dst_width = src_height;
1291 *dst_height = src_width;
1293 *(to + (src_width - i) * src_height + j) =
1294 *(from + j * src_width + i);
1305 void cam_utils_set_windows_xy_to_videos_xy(CamVideoRectangle src,
1306 CamVideoRectangle *result,
1309 cam_retm_if(data == NULL, "data is null");
1310 struct appdata *ad = (struct appdata *)data;
1313 gdouble width_ratio = 0.0;
1314 gdouble height_ratio = 0.0;
1315 width_ratio = (gdouble) ad->camfw_video_width/ad->preview_w;
1316 height_ratio = (gdouble) ad->camfw_video_height/ad->preview_h;
1317 DEBUG_TRACE("result->x %d result->y %d",result->x, result->y);
1318 switch (ad->camcorder_rotate) {
1319 case CAMERA_ROTATION_NONE:
1320 /* win: (x,y) -------->x (width)
1326 result->x = width_ratio * (src.x - ad->preview_offset_x);
1327 result->y = height_ratio * (src.y - ad->preview_offset_y);
1328 /*notes:here,result->w: horizontal;result->h:vertical*/
1329 result->w = src.w * width_ratio;
1330 result->h = src.h * height_ratio;
1332 case CAMERA_ROTATION_90:
1333 /* win: (x,y) -------->x video:(x,y) (height)y<-----
1339 result->x = height_ratio * (ad->win_height - ad->preview_offset_y - src.x);
1340 result->y = width_ratio * (src.y - ad->preview_offset_y);
1341 /*notes:here,result->w: horizontal;result->h:vertical*/
1342 result->w = src.w * height_ratio;
1343 result->h = src.h * width_ratio;
1345 case CAMERA_ROTATION_180:
1346 /* win: (x,y) -------->x video:(x,y) y(height)
1350 * -y (width)x<------
1352 result->x = width_ratio * (ad->win_width - src.x - ad->preview_offset_x);
1353 result->y = height_ratio * (ad->win_height - src.y - ad->preview_offset_y);
1354 /*notes:here,result->w: horizontal;result->h:vertical*/
1355 result->w = src.w * width_ratio;
1356 result->h = src.h * height_ratio;
1358 case CAMERA_ROTATION_270:
1359 /* win: (x,y) -------->x video:(x,y) x(width)
1363 * -y -------------y(height)
1365 result->x = height_ratio * (src.x - ad->preview_offset_y);
1366 result->y = width_ratio * (ad->win_width - src.y - ad->preview_offset_x);
1367 /*notes:here,result->w: horizontal;result->h:vertical*/
1368 result->w = src.w * height_ratio;
1369 result->h = src.h * width_ratio;
1372 DEBUG_TRACE("REACHE UN-REACHED CODES");
1374 DEBUG_TRACE("songfeng result->x %d result->y %d",result->x, result->y);
1379 void cam_utils_set_videos_xy_to_windows_xy(CamVideoRectangle src,
1380 CamVideoRectangle *result,
1383 cam_retm_if(data == NULL, "data is null");
1384 struct appdata *ad = (struct appdata *)data;
1387 gdouble width_ratio = 0.0;
1388 gdouble height_ratio = 0.0;
1389 width_ratio = (gdouble) ad->preview_w/ad->camfw_video_width;
1390 height_ratio = (gdouble) ad->preview_h/ad->camfw_video_height;
1392 switch (ad->camcorder_rotate) {
1393 case CAMERA_ROTATION_NONE:
1395 * win: (x,y) -------->x video:(x,y) -------->x(width)
1401 result->x = width_ratio * src.x + ad->preview_offset_x;
1402 result->y = height_ratio * src.y + ad->preview_offset_y;
1403 /*notes:here,result->w: horizontal;result->h:vertical*/
1404 result->w = src.w * width_ratio;
1405 result->h = src.h * height_ratio;
1407 case CAMERA_ROTATION_90:
1408 /* win: (x,y) -------->x video:(x,y) (height)y<-----
1414 result->x = ad->win_height - (height_ratio * src.y + ad->preview_offset_y + height_ratio * src.w);
1415 result->y = width_ratio * src.x + ad->preview_offset_x;
1416 /*notes:here,result->w: horizontal;result->h:vertical*/
1417 result->w = src.w * height_ratio;
1418 result->h = src.h * width_ratio;
1420 case CAMERA_ROTATION_180:
1421 /* win: (x,y) -------->x video:(x,y) y(height)
1425 * -y (width)x<------
1427 result->x = ad->win_width - (width_ratio * src.x + ad->preview_offset_x + width_ratio *src.w);
1428 result->y = ad->win_height - (height_ratio * src.y + ad->preview_offset_y + height_ratio *src.h);
1429 /*notes:here,result->w: horizontal;result->h:vertical*/
1430 result->w = src.w * width_ratio;
1431 result->h = src.h * height_ratio;
1433 case CAMERA_ROTATION_270:
1434 /* win: (x,y) -------->x video:(x,y) x(width)
1438 * -y -------------y(height)
1440 result->x = height_ratio * src.y + ad->preview_offset_y;
1441 result->y = ad->win_width - (width_ratio * src.x + ad->preview_offset_x + width_ratio * src.w);
1442 /*notes:here,result->w: horizontal;result->h:vertical*/
1443 result->w = src.w * height_ratio;
1444 result->h = src.h * width_ratio;
1447 DEBUG_TRACE("REACHE UN-REACHED CODES");