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://floralicense.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_W2M_APPROXIMATELY_SIZE 693910
34 #define JPEG_2M_APPROXIMATELY_SIZE 566000
35 #define JPEG_1M_APPROXIMATELY_SIZE 283000
36 #define JPEG_VGA_APPROXIMATELY_SIZE 135000
38 #define CLIPING(data) ((data) < 0 ? 0 : ((data) > 255) ? 255 : (data))
39 /*#define SUPPORT_WINK //use wink library */
41 static void YuvToRgb(int Y, int U, int V, int *R, int *G, int *B)
43 *B = CLIPING((76284 * (Y - 16) + 132252 * (U - 128)) >> 16);
44 *G = CLIPING((76284 * (Y - 16) - 53281 * (V - 128) -
45 25625 * (U - 128)) >> 16);
46 *R = CLIPING((76284 * (Y - 16) + 104595 * (V - 128)) >> 16);
49 /* #define SUPPORT_WINK */
50 #define CAPTUERD_IMAGE_SAVE_PATH "/tmp/captured_image.jpg"
53 #define YUV422_SIZE(width, height) ((width) * (height) * 2)
54 #endif /* YUV422_SIZE */
57 #define YUV420_SIZE(width, height) ((width) * (height) * 3 / 2)
58 #endif /* YUV420_SIZE */
64 char *m_mmc_path = NULL;
66 /*get the torchlight on/off value*/
67 gboolean cam_utils_check_torchlight_status(void *data)
69 struct appdata *ad = (struct appdata *)data;
70 cam_retv_if(ad == NULL, FALSE);
71 int ret = -1; /*error, success is 0*/
73 ret = vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TORCH_LIGHT, &key_value); /*ret [-1: error, 0:succes], key_value[on:1 , off:0]*/
74 DEBUG_TRACE("VCONFKEY_SETAPPL_ACCESSIBILITY_TORCH_LIGHT is changed to %d", key_value);
76 DEBUG_TRACE("vconf_get_int failed");
80 /*torchlight is working*/
81 ad->torchlight_on = TRUE;
83 ad->torchlight_on = FALSE;
88 /*get the battery warning low state*/
89 gboolean cam_utils_check_battery_warning_low(void)
93 if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &low_status)) {
94 cam_debug(LOG_UI, "battery status low = %d", low_status);
95 if (low_status <= VCONFKEY_SYSMAN_BAT_WARNING_LOW)
98 cam_warning(LOG_UI, "get setting failed %s",
99 VCONFKEY_SYSMAN_BATTERY_STATUS_LOW);
104 /*get the battery critical low state*/
105 gboolean cam_utils_check_battery_critical_low(void)
109 if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &low_status)) {
110 cam_debug(LOG_UI, "battery status low = %d", low_status);
111 if (low_status <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
114 cam_warning(LOG_UI, "get setting failed %s",
115 VCONFKEY_SYSMAN_BAT_CRITICAL_LOW);
122 gboolean cam_utils_check_call_running(void)
126 if (!vconf_get_int(VCONFKEY_CALL_STATE, &call_state)) {
127 cam_debug(LOG_UI, "call state = %d", call_state);
128 if (call_state == VCONFKEY_CALL_OFF)
131 cam_warning(LOG_UI, "get setting failed %s",
132 VCONFKEY_CALL_STATE);
138 int cam_utils_get_battery_level(void)
140 debug_fenter(LOG_SYS);
142 gint battery_level = -1;
144 /* VCONF_BATTERY_LEVEL or VCONFKEY_INDICATOR_MODULE_BATTERY */
145 if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, &battery_level)) {
146 cam_info(LOG_SYS, "battery_level = %d", battery_level);
147 return battery_level;
149 cam_info(LOG_SYS, "get setting failed %s",
150 VCONFKEY_SYSMAN_BATTERY_CAPACITY);
153 return battery_level;
156 gint cam_utils_get_charging_status(void)
158 debug_fenter(LOG_SYS);
162 /* VCONF_BATTERY_CHARGE_NOW or VCONFKEY_INDICATOR_MODULE_CHARGE */
163 if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &charging)) {
164 cam_info(LOG_SYS, "charging = %d", charging);
166 cam_info(LOG_SYS, "get setting failed %s",
167 VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW);
173 guint64 cam_system_get_remain_rec_time(void *data)
175 struct appdata *ad = (struct appdata *)data;
179 CamAppData *camapp = ad->camapp_handle;
180 if (camapp == NULL) {
183 guint64 remain_time = 0;
186 gint64 free_space = 0;
187 guint a_bitrate = 0, v_bitrate = 0;
188 gchar* target_path = (gchar*)cam_app_get_target_path();
189 if (target_path == NULL) {
192 free_space = cam_get_free_space(target_path);
194 a_bitrate = cam_app_get_aenc_bitrate(ad);
195 v_bitrate = cam_app_get_venc_bitrate(ad, camapp->video_quality);
198 (guint64)((free_space * 8) / (a_bitrate + v_bitrate));
204 gint64 cam_system_get_still_count_by_resolution(void *data)
206 debug_fenter(LOG_CAM);
208 struct appdata *ad = (struct appdata *)data;
212 CamAppData *camapp = ad->camapp_handle;
213 if (camapp == NULL) {
217 gint64 free_space = 0;
218 gint64 remained_count = 0;
219 gint64 avg_file_size = -1;
220 gchar* target_path = (gchar*)cam_app_get_target_path();
221 if (target_path == NULL) {
224 free_space = cam_get_free_space(target_path);
225 DEBUG_TRACE("FREE SPACE =%d", free_space);
226 free_space = (free_space - STILL_MINIMAL_SPACE);
228 /* TODO: Quality factor should be calculated later!
230 switch (camapp->photo_resolution) {
231 case CAM_RESOLUTION_3264x2448:
232 case CAM_RESOLUTION_3264x2176:
233 avg_file_size = JPEG_8M_APPROXIMATELY_SIZE;
236 case CAM_RESOLUTION_3264x1960:
237 case CAM_RESOLUTION_3264x1836:
238 avg_file_size = JPEG_W6M_APPROXIMATELY_SIZE;
241 case CAM_RESOLUTION_2800x1920:
242 case CAM_RESOLUTION_2560x1920:
243 avg_file_size = JPEG_5M_APPROXIMATELY_SIZE;
246 case CAM_RESOLUTION_2560x1536:
247 avg_file_size = JPEG_W4M_APPROXIMATELY_SIZE;
250 case CAM_RESOLUTION_2560x1440:
251 case CAM_RESOLUTION_2048x1536:
252 avg_file_size = JPEG_3M_APPROXIMATELY_SIZE;
255 case CAM_RESOLUTION_2048x1152:
256 avg_file_size = JPEG_W2M_APPROXIMATELY_SIZE;
259 case CAM_RESOLUTION_1920x1080:
260 case CAM_RESOLUTION_1600x1200:
261 case CAM_RESOLUTION_1392x1392:
262 avg_file_size = JPEG_2M_APPROXIMATELY_SIZE;
265 case CAM_RESOLUTION_1280x720:
266 case CAM_RESOLUTION_1280x960:
267 avg_file_size = JPEG_1M_APPROXIMATELY_SIZE;
270 case CAM_RESOLUTION_SVGA:
271 avg_file_size = 150 * 1024 * 2;
274 case CAM_RESOLUTION_WVGA:
275 avg_file_size = 130 * 1024 * 2;
278 case CAM_RESOLUTION_VGA:
279 avg_file_size = JPEG_VGA_APPROXIMATELY_SIZE;
282 case CAM_RESOLUTION_WQVGA:
283 avg_file_size = 100 * 1024 * 2;
286 case CAM_RESOLUTION_QVGA:
287 avg_file_size = 100 * 1024 * 1.5;
290 case CAM_RESOLUTION_CIF:
291 avg_file_size = 25 * 1024 * 2;
295 cam_critical(LOG_CAM, "unhandled resolution:%dx%d", HIWORD(camapp->photo_resolution), LOWORD(camapp->photo_resolution));
299 remained_count = free_space / avg_file_size;
300 DEBUG_TRACE("FREE SPACE =%d", free_space);
301 DEBUG_TRACE("avg_file_size =%d", avg_file_size);
302 DEBUG_TRACE("remained_count =%d", remained_count);
304 return (int)MAX(remained_count, 0);
307 gint64 cam_get_free_space(const gchar *path)
310 if (-1 == statfs(path, &fs))
312 return (gint64) fs.f_bsize * fs.f_bavail;
316 gboolean cam_utils_set_guide_rect_color(void *data) {
318 struct appdata *ad = (struct appdata *)data;
319 CamAppData *camapp = NULL;
321 cam_retv_if(ad == NULL, FALSE);
322 camapp = ad->camapp_handle;
323 cam_retv_if(camapp == NULL, FALSE);
327 int capture_video_format = CAMERA_PIXEL_FORMAT_INVALID;
328 cam_mm_get_video_source_format(NULL, &capture_video_format);
329 DEBUG_TRACE("capture_video_format =%d", capture_video_format);
331 switch (capture_video_format) {
332 case CAMERA_PIXEL_FORMAT_INVALID:
334 cam_debug(LOG_UI,"can not set preview format & shapshot format");
337 case CAMERA_PIXEL_FORMAT_UYVY:
338 /*case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:*//*TODO:there is no this value in capi*/
340 camapp->guide_rect_green = UYVY_GUIDE_RECT_GREEN;
341 camapp->guide_rect_orange = UYVY_GUIDE_RECT_ORANGE;
342 camapp->guide_rect_red = UYVY_GUIDE_RECT_RED;
343 camapp->guide_rect_white = UYVY_GUIDE_RECT_WHITE;
346 case CAMERA_PIXEL_FORMAT_YUYV:
348 camapp->guide_rect_green= YUYV_GUIDE_RECT_GREEN;
349 camapp->guide_rect_orange= YUYV_GUIDE_RECT_ORANGE;
350 camapp->guide_rect_red= YUYV_GUIDE_RECT_RED;
351 camapp->guide_rect_white = YUYV_GUIDE_RECT_WHITE;
355 cam_debug(LOG_UI,"can not set preview format & shapshot format");
364 gint64 cam_get_capacity_space(const gchar *path)
367 if (-1 == statfs(path, &fs))
369 return (gint64) fs.f_bsize * fs.f_blocks;
375 cam_utils_draw_guide_rectangle(void *data, void *frame, int x_org, int y_org,
376 int width, int height)
378 struct appdata *ad = (struct appdata *)data;
379 CamAppData *camapp = NULL;
381 cam_retm_if(ad == NULL, "appdata is NULL");
382 camapp = ad->camapp_handle;
383 cam_retm_if(camapp == NULL, "cam_handle is NULL");
385 int left, right, top, bottom, offset, y;
386 int xStart, xEnd, draw_width;
388 if (width == 0 && height == 0) {
389 cam_debug(LOG_UI, " width,height of rectangle are zero");
393 left = 2 * (x_org / 2); /* to draw on YUV422 or YUV420, we must start at even x */
394 right = left + 2 * (width / 2);
396 bottom = y_org + height;
398 int preview_w = 0; /* make sure this is even */
400 gboolean ret = FALSE;
402 ret = cam_mm_get_video_size( &preview_w, &preview_h);
403 cam_ret_if(ret == FALSE || preview_w == 0 || preview_h == 0);
405 xStart = MAX(left, 0);
406 xEnd = MIN(right, preview_w);
408 draw_width = xEnd - xStart; /* for top and bottom */
412 int line_thickness = FIT_TO_RESOLUTION(2, 4); /* make sure this is even */
415 int frame_size = preview_w * preview_h;
417 guchar guide_rect_colorY =
418 (guchar) ((camapp->guide_rect_color >> 8) & 0xFF);
419 guchar guide_rect_colorU = (guchar) ((camapp->guide_rect_color) & 0xFF);
420 guchar guide_rect_colorV =
421 (guchar) ((camapp->guide_rect_color >> 16) & 0xFF);
423 top = 2 * (top / 2); /* to draw on YUV420, we must start at even y */
424 bottom = 2 * (bottom / 2);
427 for (y = MAX(top, 0); y < top + line_thickness && y < preview_h; y += 2) { /* line_thickness, and preview_h must be even */
428 offset = y * preview_w + xStart;
429 memset(((guchar *) frame) + offset, guide_rect_colorY,
431 memset(((guchar *) frame) + offset + preview_w,
432 guide_rect_colorY, draw_width);
433 offset = y * preview_w / 4 + xStart / 2;
434 memset(((guchar *) frame) + frame_size + offset,
435 guide_rect_colorU, draw_width / 2);
436 memset(((guchar *) frame) + frame_size + frame_size / 4 +
437 offset, guide_rect_colorV, draw_width / 2);
441 for (y = MAX(bottom - line_thickness, 0);
442 y < bottom && y < preview_h; y += 2) {
443 offset = y * preview_w + xStart;
444 memset(((guchar *) frame) + offset, guide_rect_colorY,
446 memset(((guchar *) frame) + offset + preview_w,
447 guide_rect_colorY, draw_width);
448 offset = y * preview_w / 4 + xStart / 2;
449 memset(((guchar *) frame) + frame_size + offset,
450 guide_rect_colorU, draw_width / 2);
451 memset(((guchar *) frame) + frame_size + frame_size / 4 +
452 offset, guide_rect_colorV, draw_width / 2);
456 if (left + line_thickness > 0 && left < preview_w) {
457 draw_width = MIN(MIN(line_thickness, line_thickness + left), preview_w - left); /* now calculated for left */
458 for (y = MAX(top, 0); y < bottom && y < preview_h; y += 2) {
459 offset = y * preview_w + xStart;
460 memset(((guchar *) frame) + offset, guide_rect_colorY,
462 memset(((guchar *) frame) + offset + preview_w,
463 guide_rect_colorY, draw_width);
464 offset = y * preview_w / 4 + xStart / 2;
465 memset(((guchar *) frame) + frame_size + offset,
466 guide_rect_colorU, draw_width / 2);
467 memset(((guchar *) frame) + frame_size +
468 frame_size / 4 + offset, guide_rect_colorV,
473 if (right > 0 && right - line_thickness < preview_w) {
474 draw_width = MIN(MIN(line_thickness, right), preview_w - right + line_thickness); /* now calculated for right */
475 for (y = MAX(top, 0); y < bottom && y < preview_h; y += 2) {
476 offset = y * preview_w + MAX(right - line_thickness, 0);
477 memset(((guchar *) frame) + offset, guide_rect_colorY,
479 memset(((guchar *) frame) + offset + preview_w,
480 guide_rect_colorY, draw_width);
482 y * preview_w / 4 + MAX(right - line_thickness,
484 memset(((guchar *) frame) + frame_size + offset,
485 guide_rect_colorU, draw_width / 2);
486 memset(((guchar *) frame) + frame_size +
487 frame_size / 4 + offset, guide_rect_colorV,
496 for (y = top; y < top + line_thickness && y < preview_h; y++) {
499 offset = y * preview_w + xStart;
500 for (x = 0; x < draw_width; x += 2) {
501 ((gulong *) frame)[(offset + x) / 2] =
502 camapp->guide_rect_color;
507 for (y = bottom - line_thickness; y < bottom && y < preview_h; y++) {
510 offset = y * preview_w + xStart;
511 for (x = 0; x < draw_width; x += 2) {
512 ((gulong *) frame)[(offset + x) / 2] =
513 camapp->guide_rect_color;
518 if (left + line_thickness >= 0 && left < preview_w) {
519 draw_width = MIN(MIN(line_thickness, line_thickness + left), preview_w - left); /* now calculated for left */
520 for (y = MAX(top, 0); y < bottom && y < preview_h; y++) {
521 offset = y * preview_w + xStart;
522 for (x = 0; x < draw_width; x += 2) {
523 ((gulong *) frame)[(offset + x) / 2] =
524 camapp->guide_rect_color;
529 if (right >= 0 && right - line_thickness < preview_w) {
530 draw_width = MIN(MIN(line_thickness, right), preview_w - right + line_thickness); /* now calculated for right */
531 for (y = MAX(top, 0); y < bottom && y < preview_h; y++) {
532 offset = y * preview_w + MAX(right - line_thickness, 0);
533 for (x = 0; x < draw_width; x += 2) {
534 ((gulong *) frame)[(offset + x) / 2] =
535 camapp->guide_rect_color;
544 cam_utils_convert_YUYV_to_UYVY(unsigned char *dst, unsigned char *src,
549 memset(dst, 0x00, length);
550 memcpy(dst, src + 1, length - 1);
552 for (i = 0; i < length; i++) {
560 void cam_utils_convert_UYVY_to_YUYV(char *dst, char *src, gint length)
564 memset(dst, 0x00, length);
565 memcpy((char *)dst + 1, (char *)src, length - 1);
567 for (i = 0; i < length; i++) {
568 if ((i % 2)) { /* even */
578 cam_utils_convert_YUYV_to_YUV420P(unsigned char *pInBuf, unsigned char *pOutBuf,
579 int width, int height)
581 unsigned char *pInY, *pInU, *pInV;
582 unsigned char *pOutY, *pOutU, *pOutV;
584 int nRowIters = height / 2;
585 int nColIters = width / 2;
593 pOutU = pOutBuf + width * height;
594 pOutV = pOutBuf + width * height * 5 / 4;
596 /* Iterate over half the number of rows, because inside there are 2 loops on columns */
597 for (rows = 0; rows < nRowIters; rows++) {
599 Iterate over half the number of columns, copy 2 pixels each time */
600 for (cols = 0; cols < nColIters; cols++) {
601 /* Copy Y of first pixel */
605 /* Copy Y of second pixel */
609 /* Copy U of all 4 pixels */
613 /* Copy V of all 4 pixels */
619 Iterate over half the number of columns, copy 2 pixels each time */
620 for (cols = 0; cols < nColIters; cols++) {
621 /* Copy Y of third pixel */
625 /* Copy Y of fourth pixel */
630 /* Skip U, V of third, fourth pixel */
639 cam_utils_convert_YUYV_to_YUV420P(byte *src, byte *dst, gint width, gint height)
642 int YUYV_length = 0, YUV420P_length = 0;
643 int temp_length = 0, temp_length2 = 0;
644 int pos_u = 0, pos_v = 0;
646 /* cam_debug( LOG_UI, " start" ); */
648 temp_length = width * height;
649 temp_length2 = temp_length >> 2;
650 YUYV_length = temp_length << 1;
651 YUV420P_length = (temp_length * 3) >> 1;
654 cam_debug( LOG_UI, " size[%dx%d], length YUYV[%d],YUV420P[%d]",
655 width, height, YUYV_length, YUV420P_length );
659 cam_critical(LOG_SYS, " dst is NULL");
663 for (i = 0; i < temp_length; i++) {
665 dst[i] = src[i << 1];
667 if (i % (width << 1) < width) {
670 dst[temp_length + pos_u] = src[(i << 1) + 1];
674 dst[temp_length + temp_length2 + pos_v] =
684 void *cam_utils_YUV422_to_ARGB(byte *frame, int width, int height)
686 /* source yuv is FOURCC YUYV, sampling format YUV 422 .
688 Byte Ordering (lowest byte) Y0, U0, Y1, V0 */
689 byte *frame_argb = malloc(width * height * 4); /* for ARGB */
691 if (frame_argb == NULL) {
694 memset(frame_argb,0,width * height * 4);
696 int i = 0, j = 0; /* row, column */
699 unsigned long pixel_idx = 0, rgb_index = 0;
703 for (i = 0; i < height; i++) {
704 for (j = 0; j < width; j++) {
706 if (j % 2) { /* odd */
709 /* u_idx = -1; v_idx = 1; */
713 /* u_idx = 1; v_idx = 3; */
716 y = frame[pixel_idx];
717 v = frame[pixel_idx + v_idx];
718 u = frame[pixel_idx + u_idx];
720 YuvToRgb(y, u, v, &r, &g, &b);
723 frame_argb[rgb_index++] = (byte)CLIPING(r);
724 frame_argb[rgb_index++] = (byte)CLIPING(g);
725 frame_argb[rgb_index++] = (byte)CLIPING(b);
726 frame_argb[rgb_index++] = 0xff;
728 pixel_idx += 2; /* yuv422, 4byte is 2 pixel */
732 return (void *)frame_argb;
735 void *cam_utils_IYUV_to_ARGB(byte *frame, int width, int height)
737 /* source yuv is FOURCC IYUV or I420, sampling format YUV 420. */
738 /* IYUV format is http://www.fourcc.org/yuv.php#IYUV */
740 byte *frame_argb = malloc(width * height * 4); /* for ARGB */
741 if (frame_argb == NULL) {
744 memset(frame_argb,0,width * height * 4);
746 int h = 0, w = 0; /* row, column */
749 unsigned long rgb_index = 0; /* ,pixel_idx=0; */
752 for (h = 0; h < height; h++) {
753 for (w = 0; w < width; w++) {
755 y = frame[h * height + w];
756 u = frame[(width * height) + idx];
757 v = frame[(width * height) + ((width * height) / 4) +
763 YuvToRgb(y, u, v, &r, &g, &b);
766 frame_argb[rgb_index++] = (byte)CLIPING(r);
767 frame_argb[rgb_index++] = (byte)CLIPING(g);
768 frame_argb[rgb_index++] = (byte)CLIPING(b);
769 frame_argb[rgb_index++] = 0;
773 if ((w == width - 1) && (!(h % 2)))
777 return (void *)frame_argb;
781 cam_utils_save_to_jpg_file(int storage_id, gchar *filename, void *frame,
782 int width, int height, GError **error)
784 cam_debug(LOG_MM, " [%dx%d] %s", width, height, filename);
786 int ret = CAMERA_ERROR_NONE;
788 CAM_TA_ACUM_ITEM_BEGIN("cam_utils_check_mmc_for_writing", 0);
789 if (storage_id == CAM_STORAGE_EXTERNAL) {
790 if (!cam_utils_check_mmc_for_writing(error)) {
794 CAM_TA_ACUM_ITEM_END("cam_utils_check_mmc_for_writing", 0);
796 cam_debug(LOG_SYS, " \n\n\n\n\n START JPEG ENCODING \n\n\n\n\n");
798 CAM_TA_ACUM_ITEM_BEGIN("image_util_encode_jpeg", 0);
799 ret = image_util_encode_jpeg(frame, width, height,
800 IMAGE_UTIL_COLORSPACE_YUYV, 90, filename);
801 CAM_TA_ACUM_ITEM_END("image_util_encode_jpeg", 0);
803 cam_debug(LOG_SYS, " \n\n\n\n\n END JPEG ENCODING \n\n\n\n\n");
806 cam_critical(LOG_MM, " image_util_encode_jpeg Failed [%x]", ret);
814 cam_utils_save_to_jpg_memory(byte **memory, unsigned int *size, void *src_frame,
815 int width, int height)
817 cam_debug(LOG_MM, " [%dx%d]", width, height);
819 int ret = CAMERA_ERROR_NONE;
821 cam_debug(LOG_SYS, " \n\n\n\n\n START JPEG ENCODING \n\n\n\n\n");
823 CAM_TA_ACUM_ITEM_BEGIN("image_util_encode_jpeg_to_memory", 0);
824 ret = image_util_encode_jpeg_to_memory(src_frame, width, height,
825 IMAGE_UTIL_COLORSPACE_YUYV, 90, (unsigned char **)memory, size);
826 CAM_TA_ACUM_ITEM_END("image_util_encode_jpeg_to_memory", 0);
828 cam_debug(LOG_SYS, " \n\n\n\n\n END JPEG ENCODING \n\n\n\n\n");
831 cam_critical(LOG_MM, "image_util_encode_jpeg_to_memory Failed [%x]", ret);
838 void *cam_utils_load_temp_file(gchar *filepath, gint *pfilesize)
840 struct stat fileinfo;
844 ret = stat(filepath, &fileinfo);
846 cam_critical(LOG_CAM, "can't get file infomation - error[%d]", ret);
850 gint filesize = fileinfo.st_size;
852 cam_critical(LOG_CAM, "can't get file infomation");
856 cam_debug(LOG_CAM, "temp file's file_path =%s, file_size =%d",
859 void *data = (void *)malloc(filesize);
864 memset(data, 0, filesize);
866 cam_debug(LOG_CAM, "data = %p ", data);
868 if ((fp = fopen(filepath, "r")) == NULL) {
873 cam_critical(LOG_CAM, "can't open file infomation");
876 if (fread(data, filesize, 1, fp) != 1) {
882 cam_critical(LOG_CAM, "can't read file infomation");
886 *pfilesize = filesize;
893 gboolean cam_utils_check_mmc_for_writing(GError **error)
897 const gchar *error_msg = NULL;
900 m_mmc_path = (char *)cam_file_get_external_image_path();
902 if (!g_file_test(m_mmc_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
903 error_code = CAM_ERROR_STORAGE_UNAVAILABLE;
904 error_msg = dgettext(PACKAGE, "IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
908 if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_state)) {
909 if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED) {
910 error_code = CAM_ERROR_STORAGE_UNAVAILABLE;
911 error_msg = dgettext(PACKAGE, "IDS_CAM_POP_MEMORY_CARD_REMOVED");
923 *error = cam_error_new_literal(error_code, error_msg);/*note:fix warnning*/
930 gboolean cam_utils_check_mmc_for_inserted_stats(void *data)
933 g_return_val_if_fail(data, FALSE);
935 struct appdata *ad = (struct appdata *)data;
936 cam_retv_if(ad == NULL, FALSE);
938 CamAppData *camapp = NULL;
939 camapp = ad->camapp_handle;
941 camapp = ad->camapp_handle;
942 cam_retv_if(camapp == NULL, FALSE);
944 if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_state)) {
945 if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED
946 && camapp->storage == CAM_STORAGE_EXTERNAL) {
954 int cam_utils_check_mmc_status(void)
958 if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status)) {
959 cam_warning(LOG_SYS, " Get MMC status failed");
965 int cam_utils_get_storage_id_from_filepath(const char *filepath)
967 char *strtemp = NULL;
969 strtemp = (char *)cam_file_get_internal_image_path();
972 if (!strncmp(filepath, strtemp, strlen(strtemp)))
973 return CAM_STORAGE_INTERNAL;
976 strtemp = (char *)cam_file_get_external_image_path();
978 if (!strncmp(filepath, strtemp, strlen(strtemp)))
979 return CAM_STORAGE_EXTERNAL;
982 return CAM_STORAGE_DCF;
987 cam_utils_safety_file_copy(const char *dst, const char *src, GError **error)
991 const gchar *error_msg = NULL;
997 cam_debug(LOG_FILE, " dst file name = %s", dst);
998 cam_debug(LOG_FILE, " dst file name = %s", src);
1000 fp_src = fopen(src, "r");
1001 if (fp_src == NULL) {
1002 error_code = CAM_ERROR_FILE_NOT_EXISTS;
1003 error_msg = dgettext(PACKAGE, "IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
1007 cam_debug(LOG_FILE, "start check mmc");
1009 if (cam_utils_get_storage_id_from_filepath(dst) == CAM_STORAGE_EXTERNAL) {
1010 if (!cam_utils_check_mmc_for_writing(error))
1014 cam_debug(LOG_FILE, "end check mmc");
1016 fp_dst = fopen(dst, "wb");
1017 if (fp_dst == NULL) {
1018 error_code = CAM_ERROR_FILE_REGISTER_FAILED;
1019 error_msg = dgettext(PACKAGE, "IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
1022 cam_debug(LOG_FILE, "start copy");
1024 unsigned long filesize = 0;
1025 unsigned long filecopyblocksize = 1024 * 512;
1026 unsigned long copysize = 0;
1027 struct stat fileinfo;
1029 void *buff = malloc(filecopyblocksize); /* 512k */
1033 /* get source file size */
1034 ret = stat(src, &fileinfo);
1036 cam_critical(LOG_CAM, "can't get file infomation - error[%d]", ret);
1044 filesize = fileinfo.st_size;
1045 cam_debug(LOG_FILE, "source file size %lu", filesize);
1049 if (filesize >= filecopyblocksize) {
1050 copysize = filecopyblocksize;
1052 copysize = filesize;
1055 cam_debug(LOG_FILE, "copy size %lu", copysize);
1056 if (fread(buff, copysize, 1, fp_src) != 1) {
1057 cam_debug(LOG_FILE, "read error!");
1065 if (buff && fwrite(buff, copysize, 1, fp_dst) != 1) {
1066 cam_debug(LOG_FILE, "file read or write error");
1067 error_code = CAM_ERROR_FILE;
1068 error_msg = dgettext(PACKAGE, "IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
1076 filesize -= copysize;
1077 } while (filesize > 0);
1079 error_code = CAM_ERROR_PANORAMA_LIB;
1080 error_msg = dgettext(PACKAGE, "IDS_CAM_BODY_FAILED_TO_BUILD_PANORAMIC_IMAGES_TRY_LATER");
1090 stat(filepath, &fileinfo);
1092 gint filesize = fileinfo.st_size;
1094 if (fwrite(g_result_panorama_image->bits, g_result_panorama_image->size, 1, fp) != 1)
1096 free(camapp->filename);
1097 camapp->filename = NULL;
1104 /*fsync(fp_dst->_fileno);*//*block for increasing formance of shot-to-shot */
1116 cam_debug(LOG_FILE, "file copy error, error number=%x", error_code);
1127 *error = cam_error_new_literal(error_code, error_msg);/*note:fix warnning*/
1134 static char *cam_utils_trim(char *str)
1138 for (; *i_buf && isspace(*i_buf); ++i_buf) ;
1143 gboolean cam_utils_parse_args(int argc, char *argv[], CamExeArgs *args)
1146 const char *delimiters = ",";
1150 for (index = 0; index < argc; index++) {
1154 if (argv[index][0] == '-' && argv[index][1] != '\0') {
1155 char *r_argv = strdup(cam_utils_trim((argv[index] + 2)));
1156 switch (argv[index][1]) {
1158 case 'd': /* daemon launch */
1161 CAM_LAUNCHING_MODE_DAEMON;
1165 case 'h': /* help */
1168 CAM_LAUNCHING_MODE_HELP;
1173 case 'v': /* review */
1175 args->review = TRUE;
1178 case 'r': /* resolution */
1181 char *result = NULL;
1182 char *save_string = NULL;
1183 result = strtok_r(r_argv, delimiters,&save_string);
1185 args->width = atoi(result);
1186 result = strtok_r(NULL, delimiters,&save_string);
1188 args->height = atoi(result);
1194 case 'm': /* cam_mode */
1197 if (0 == strcmp(r_argv, "IMAGE")) {
1198 args->cam_mode = CAM_CAMERA_MODE;
1199 } else if (0 == strcmp(r_argv, "VIDEO")) {
1200 args->cam_mode = CAM_CAMCORDER_MODE;
1202 if (r_argv != NULL) {
1213 case '-': /* launching appl.'s name */
1216 char *qualifier_to_find = "calling-app=";
1217 char *qualifier_field_start = NULL;
1220 qualifier_field_start = strstr(r_argv, qualifier_to_find);
1221 if (qualifier_field_start) {
1222 qualifier_field_start += strlen(qualifier_to_find);
1223 name = cam_utils_trim(qualifier_field_start);
1224 args->caller = strdup(name);
1226 cam_debug(LOG_SYS, "unknown arg : %s", argv[index]);
1233 case 'l': /* size limit */
1236 args->size_limit = atoi(r_argv);
1242 default: /* unknown option */
1243 cam_debug(LOG_SYS, "Unknown Option : %c",
1247 if (r_argv != NULL) {
1252 cam_debug(LOG_SYS, "unknown arg : %s", argv[index]);
1256 cam_debug(LOG_SYS, "cam_mode : %d", args->cam_mode);
1257 cam_debug(LOG_SYS, "resolution : [%dx%d]", args->width, args->height);
1258 cam_debug(LOG_SYS, "limit : %d", args->size_limit);
1259 cam_debug(LOG_SYS, "caller : %s", args->caller);
1263 gboolean cam_utils_check_wide_resolution(int resol_w, int resol_h)
1265 if ((ABS((gfloat)((resol_w * 3.0) / (resol_h * 4.0)) - 1.0 ) < CAM_EPSINON )
1266 || (ABS((gfloat)((resol_w * 25.0) / (resol_h * 36.0)) - 1.0) < CAM_EPSINON)) {
1267 cam_debug(LOG_UI, "Not Wide Resolution : [%d]x[%d]", resol_w,
1275 gboolean cam_utils_check_fake_image_exist(const char *fake_image_path)
1277 if (!g_file_test(fake_image_path, G_FILE_TEST_EXISTS)) {
1285 cam_utils_image_rotate(char *src, int src_width, int src_height, char *dst,
1286 int *dst_width, int *dst_height, int degree)
1289 cam_retv_if(src == NULL || dst == NULL || src_width == 0
1290 || src_height == 0, FALSE);
1291 cam_retv_if(degree != 0 && degree != 90 && degree != 180
1292 && degree != 270, FALSE);
1297 int *from = (int *)src;
1298 int *to = (int *)dst;
1301 *dst_width = src_width;
1302 *dst_height = src_height;
1303 memcpy(dst, src, src_width * src_height * 4);
1307 for (j = 0; j < src_height; j++) {
1308 for (i = 0; i < src_width; i++) {
1311 *dst_width = src_height;
1312 *dst_height = src_width;
1314 *(to + i * src_height + (src_height - j)) =
1315 *(from + j * src_width + i);
1316 } else if (degree == 180) {
1317 *dst_width = src_width;
1318 *dst_height = src_height;
1320 *(to + (src_height - j) * src_width +
1322 *(from + j * src_width + i);
1323 } else if (degree == 270) {
1324 *dst_width = src_height;
1325 *dst_height = src_width;
1327 *(to + (src_width - i) * src_height + j) =
1328 *(from + j * src_width + i);
1339 void cam_utils_set_windows_xy_to_videos_xy(CamVideoRectangle src,
1340 CamVideoRectangle *result,
1343 cam_retm_if(data == NULL, "data is null");
1344 struct appdata *ad = (struct appdata *)data;
1347 switch (ad->camcorder_rotate) {
1348 case CAMERA_ROTATION_NONE:
1349 result->x = src.x - ad->preview_offset_x;
1350 result->y = src.y - ad->preview_offset_y;
1354 case CAMERA_ROTATION_90:
1355 result->x = ad->win_height - ad->preview_offset_y - src.x;
1356 result->y = src.y - ad->preview_offset_y;
1360 case CAMERA_ROTATION_180:
1361 result->x = ad->win_width - src.x - ad->preview_offset_x;
1362 result->y = ad->win_height - src.y - ad->preview_offset_y;
1366 case CAMERA_ROTATION_270:
1367 result->x = src.x - ad->preview_offset_y;
1368 result->y = ad->win_width - src.y - ad->preview_offset_x;
1373 DEBUG_TRACE("REACHE UN-REACHED CODES");
1375 DEBUG_TRACE("songfeng result->x %d result->y %d",result->x, result->y);
1380 void cam_utils_set_videos_xy_to_windows_xy(CamVideoRectangle src,
1381 CamVideoRectangle *result,
1384 cam_retm_if(data == NULL, "data is null");
1385 struct appdata *ad = (struct appdata *)data;
1389 switch (ad->camcorder_rotate) {
1390 case CAMERA_ROTATION_NONE:
1392 * win: (x,y) -------->x video:(x,y) -------->x(width)
1398 result->x = src.x + ad->preview_offset_x;
1399 result->y = src.y + ad->preview_offset_y;
1400 /*notes:here,result->w: horizontal;result->h:vertical*/
1404 case CAMERA_ROTATION_90:
1405 /* win: (x,y) -------->x video:(x,y) (height)y<-----
1411 result->x = ad->win_height - (src.y + ad->preview_offset_y + src.w);
1412 result->y = src.x + ad->preview_offset_x;
1413 /*notes:here,result->w: horizontal;result->h:vertical*/
1417 case CAMERA_ROTATION_180:
1418 /* win: (x,y) -------->x video:(x,y) y(height)
1422 * -y (width)x<------
1424 result->x = ad->win_width - (src.x + ad->preview_offset_x + src.w);
1425 result->y = ad->win_height - (src.y + ad->preview_offset_y + src.h);
1426 /*notes:here,result->w: horizontal;result->h:vertical*/
1430 case CAMERA_ROTATION_270:
1431 cam_critical(LOG_CAM, "CAMERA_ROTATION_270");
1432 /* win: (x,y) -------->x video:(x,y) x(width)
1436 * -y -------------y(height)
1438 result->x = src.y + ad->preview_offset_y;
1439 result->y = ad->win_width - (src.x + ad->preview_offset_x + src.w);
1440 /*notes:here,result->w: horizontal;result->h:vertical*/
1445 DEBUG_TRACE("REACHE UN-REACHED CODES");
1450 gboolean cam_utils_request_main_pipe_handler(void *data, void *pipe_data, int cmd)
1452 struct appdata *ad = (struct appdata *)data;
1453 cam_retvm_if(ad == NULL, FALSE, "appdata is NULL");
1454 Ecore_Pipe_Data_Info pipe_info = {0,};
1455 pipe_info.data = pipe_data;
1456 pipe_info.cmd = cmd;
1457 return ecore_pipe_write(ad->main_pipe, (void *)&pipe_info, sizeof(Ecore_Pipe_Data_Info));