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);
139 int cam_utils_get_battery_level(void)
141 debug_fenter(LOG_SYS);
143 gint battery_level = -1;
145 /* VCONF_BATTERY_LEVEL or VCONFKEY_INDICATOR_MODULE_BATTERY */
146 if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, &battery_level)) {
147 cam_info(LOG_SYS, "battery_level = %d", battery_level);
148 return battery_level;
150 cam_info(LOG_SYS, "get setting failed %s",
151 VCONFKEY_SYSMAN_BATTERY_CAPACITY);
154 return battery_level;
157 gint cam_utils_get_charging_status(void)
159 debug_fenter(LOG_SYS);
163 /* VCONF_BATTERY_CHARGE_NOW or VCONFKEY_INDICATOR_MODULE_CHARGE */
164 if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &charging)) {
165 cam_info(LOG_SYS, "charging = %d", charging);
167 cam_info(LOG_SYS, "get setting failed %s",
168 VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW);
174 guint64 cam_system_get_remain_rec_time(void *data)
176 struct appdata *ad = (struct appdata *)data;
180 CamAppData *camapp = ad->camapp_handle;
181 if (camapp == NULL) {
184 guint64 remain_time = 0;
187 gint64 free_space = 0;
188 guint a_bitrate = 0, v_bitrate = 0;
189 gchar* target_path = (gchar*)cam_app_get_target_path();
190 if (target_path == NULL) {
193 free_space = cam_get_free_space(target_path);
195 a_bitrate = cam_app_get_aenc_bitrate(ad);
196 v_bitrate = cam_app_get_venc_bitrate(ad, camapp->video_quality);
199 (guint64)((free_space * 8) / (a_bitrate + v_bitrate));
205 gint64 cam_system_get_still_count_by_resolution(void *data)
207 debug_fenter(LOG_CAM);
209 struct appdata *ad = (struct appdata *)data;
213 CamAppData *camapp = ad->camapp_handle;
214 if (camapp == NULL) {
218 gint64 free_space = 0;
219 gint64 remained_count = 0;
220 gint64 avg_file_size = -1;
221 gchar* target_path = (gchar*)cam_app_get_target_path();
222 if (target_path == NULL) {
225 free_space = cam_get_free_space(target_path);
226 DEBUG_TRACE("FREE SPACE =%d", free_space);
227 free_space = (free_space - STILL_MINIMAL_SPACE);
229 /* TODO: Quality factor should be calculated later!
231 switch (camapp->photo_resolution) {
232 case CAM_RESOLUTION_3264x2448:
233 case CAM_RESOLUTION_3264x2176:
234 avg_file_size = JPEG_8M_APPROXIMATELY_SIZE;
237 case CAM_RESOLUTION_3264x1960:
238 case CAM_RESOLUTION_3264x1836:
239 avg_file_size = JPEG_W6M_APPROXIMATELY_SIZE;
242 case CAM_RESOLUTION_2800x1920:
243 case CAM_RESOLUTION_2560x1920:
244 avg_file_size = JPEG_5M_APPROXIMATELY_SIZE;
247 case CAM_RESOLUTION_2560x1536:
248 avg_file_size = JPEG_W4M_APPROXIMATELY_SIZE;
251 case CAM_RESOLUTION_2560x1440:
252 case CAM_RESOLUTION_2048x1536:
253 avg_file_size = JPEG_3M_APPROXIMATELY_SIZE;
256 case CAM_RESOLUTION_2048x1152:
257 avg_file_size = JPEG_W2M_APPROXIMATELY_SIZE;
260 case CAM_RESOLUTION_1920x1080:
261 case CAM_RESOLUTION_1600x1200:
262 case CAM_RESOLUTION_1392x1392:
263 avg_file_size = JPEG_2M_APPROXIMATELY_SIZE;
266 case CAM_RESOLUTION_1280x720:
267 case CAM_RESOLUTION_1280x960:
268 avg_file_size = JPEG_1M_APPROXIMATELY_SIZE;
271 case CAM_RESOLUTION_SVGA:
272 avg_file_size = 150 * 1024 * 2;
275 case CAM_RESOLUTION_WVGA:
276 avg_file_size = 130 * 1024 * 2;
279 case CAM_RESOLUTION_VGA:
280 avg_file_size = JPEG_VGA_APPROXIMATELY_SIZE;
283 case CAM_RESOLUTION_WQVGA:
284 avg_file_size = 100 * 1024 * 2;
287 case CAM_RESOLUTION_QVGA:
288 avg_file_size = 100 * 1024 * 1.5;
291 case CAM_RESOLUTION_CIF:
292 avg_file_size = 25 * 1024 * 2;
296 cam_critical(LOG_CAM, "unhandled resolution:%dx%d", HIWORD(camapp->photo_resolution), LOWORD(camapp->photo_resolution));
300 remained_count = free_space / avg_file_size;
301 DEBUG_TRACE("FREE SPACE =%d", free_space);
302 DEBUG_TRACE("avg_file_size =%d", avg_file_size);
303 DEBUG_TRACE("remained_count =%d", remained_count);
305 return (int)MAX(remained_count, 0);
308 gint64 cam_get_free_space(const gchar *path)
311 if (-1 == statfs(path, &fs))
313 return (gint64) fs.f_bsize * fs.f_bavail;
317 gboolean cam_utils_set_guide_rect_color(void *data) {
319 struct appdata *ad = (struct appdata *)data;
320 CamAppData *camapp = NULL;
322 cam_retv_if(ad == NULL, FALSE);
323 camapp = ad->camapp_handle;
324 cam_retv_if(camapp == NULL, FALSE);
328 int capture_video_format = CAMERA_PIXEL_FORMAT_INVALID;
329 cam_mm_get_video_source_format(NULL, &capture_video_format);
330 DEBUG_TRACE("capture_video_format =%d", capture_video_format);
332 switch (capture_video_format) {
333 case CAMERA_PIXEL_FORMAT_INVALID:
335 cam_debug(LOG_UI,"can not set preview format & shapshot format");
338 case CAMERA_PIXEL_FORMAT_UYVY:
339 /*case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:*//*TODO:there is no this value in capi*/
341 camapp->guide_rect_green = UYVY_GUIDE_RECT_GREEN;
342 camapp->guide_rect_orange = UYVY_GUIDE_RECT_ORANGE;
343 camapp->guide_rect_red = UYVY_GUIDE_RECT_RED;
344 camapp->guide_rect_white = UYVY_GUIDE_RECT_WHITE;
347 case CAMERA_PIXEL_FORMAT_YUYV:
349 camapp->guide_rect_green= YUYV_GUIDE_RECT_GREEN;
350 camapp->guide_rect_orange= YUYV_GUIDE_RECT_ORANGE;
351 camapp->guide_rect_red= YUYV_GUIDE_RECT_RED;
352 camapp->guide_rect_white = YUYV_GUIDE_RECT_WHITE;
356 cam_debug(LOG_UI,"can not set preview format & shapshot format");
365 gint64 cam_get_capacity_space(const gchar *path)
368 if (-1 == statfs(path, &fs))
370 return (gint64) fs.f_bsize * fs.f_blocks;
376 cam_utils_draw_guide_rectangle(void *data, void *frame, int x_org, int y_org,
377 int width, int height)
379 struct appdata *ad = (struct appdata *)data;
380 CamAppData *camapp = NULL;
382 cam_retm_if(ad == NULL, "appdata is NULL");
383 camapp = ad->camapp_handle;
384 cam_retm_if(camapp == NULL, "cam_handle is NULL");
386 int left, right, top, bottom, offset, y;
387 int xStart, xEnd, draw_width;
389 if (width == 0 && height == 0) {
390 cam_debug(LOG_UI, " width,height of rectangle are zero");
394 left = 2 * (x_org / 2); /* to draw on YUV422 or YUV420, we must start at even x */
395 right = left + 2 * (width / 2);
397 bottom = y_org + height;
399 int preview_w = 0; /* make sure this is even */
401 gboolean ret = FALSE;
403 ret = cam_mm_get_video_size( &preview_w, &preview_h);
404 cam_ret_if(ret == FALSE || preview_w == 0 || preview_h == 0);
406 xStart = MAX(left, 0);
407 xEnd = MIN(right, preview_w);
409 draw_width = xEnd - xStart; /* for top and bottom */
413 int line_thickness = FIT_TO_RESOLUTION(2, 4); /* make sure this is even */
416 int frame_size = preview_w * preview_h;
418 guchar guide_rect_colorY =
419 (guchar) ((camapp->guide_rect_color >> 8) & 0xFF);
420 guchar guide_rect_colorU = (guchar) ((camapp->guide_rect_color) & 0xFF);
421 guchar guide_rect_colorV =
422 (guchar) ((camapp->guide_rect_color >> 16) & 0xFF);
424 top = 2 * (top / 2); /* to draw on YUV420, we must start at even y */
425 bottom = 2 * (bottom / 2);
428 for (y = MAX(top, 0); y < top + line_thickness && y < preview_h; y += 2) { /* line_thickness, and preview_h must be even */
429 offset = y * preview_w + xStart;
430 memset(((guchar *) frame) + offset, guide_rect_colorY,
432 memset(((guchar *) frame) + offset + preview_w,
433 guide_rect_colorY, draw_width);
434 offset = y * preview_w / 4 + xStart / 2;
435 memset(((guchar *) frame) + frame_size + offset,
436 guide_rect_colorU, draw_width / 2);
437 memset(((guchar *) frame) + frame_size + frame_size / 4 +
438 offset, guide_rect_colorV, draw_width / 2);
442 for (y = MAX(bottom - line_thickness, 0);
443 y < bottom && y < preview_h; y += 2) {
444 offset = y * preview_w + xStart;
445 memset(((guchar *) frame) + offset, guide_rect_colorY,
447 memset(((guchar *) frame) + offset + preview_w,
448 guide_rect_colorY, draw_width);
449 offset = y * preview_w / 4 + xStart / 2;
450 memset(((guchar *) frame) + frame_size + offset,
451 guide_rect_colorU, draw_width / 2);
452 memset(((guchar *) frame) + frame_size + frame_size / 4 +
453 offset, guide_rect_colorV, draw_width / 2);
457 if (left + line_thickness > 0 && left < preview_w) {
458 draw_width = MIN(MIN(line_thickness, line_thickness + left), preview_w - left); /* now calculated for left */
459 for (y = MAX(top, 0); y < bottom && y < preview_h; y += 2) {
460 offset = y * preview_w + xStart;
461 memset(((guchar *) frame) + offset, guide_rect_colorY,
463 memset(((guchar *) frame) + offset + preview_w,
464 guide_rect_colorY, draw_width);
465 offset = y * preview_w / 4 + xStart / 2;
466 memset(((guchar *) frame) + frame_size + offset,
467 guide_rect_colorU, draw_width / 2);
468 memset(((guchar *) frame) + frame_size +
469 frame_size / 4 + offset, guide_rect_colorV,
474 if (right > 0 && right - line_thickness < preview_w) {
475 draw_width = MIN(MIN(line_thickness, right), preview_w - right + line_thickness); /* now calculated for right */
476 for (y = MAX(top, 0); y < bottom && y < preview_h; y += 2) {
477 offset = y * preview_w + MAX(right - line_thickness, 0);
478 memset(((guchar *) frame) + offset, guide_rect_colorY,
480 memset(((guchar *) frame) + offset + preview_w,
481 guide_rect_colorY, draw_width);
483 y * preview_w / 4 + MAX(right - line_thickness,
485 memset(((guchar *) frame) + frame_size + offset,
486 guide_rect_colorU, draw_width / 2);
487 memset(((guchar *) frame) + frame_size +
488 frame_size / 4 + offset, guide_rect_colorV,
497 for (y = top; y < top + line_thickness && y < preview_h; y++) {
500 offset = y * preview_w + xStart;
501 for (x = 0; x < draw_width; x += 2) {
502 ((gulong *) frame)[(offset + x) / 2] =
503 camapp->guide_rect_color;
508 for (y = bottom - line_thickness; y < bottom && y < preview_h; y++) {
511 offset = y * preview_w + xStart;
512 for (x = 0; x < draw_width; x += 2) {
513 ((gulong *) frame)[(offset + x) / 2] =
514 camapp->guide_rect_color;
519 if (left + line_thickness >= 0 && left < preview_w) {
520 draw_width = MIN(MIN(line_thickness, line_thickness + left), preview_w - left); /* now calculated for left */
521 for (y = MAX(top, 0); y < bottom && y < preview_h; y++) {
522 offset = y * preview_w + xStart;
523 for (x = 0; x < draw_width; x += 2) {
524 ((gulong *) frame)[(offset + x) / 2] =
525 camapp->guide_rect_color;
530 if (right >= 0 && right - line_thickness < preview_w) {
531 draw_width = MIN(MIN(line_thickness, right), preview_w - right + line_thickness); /* now calculated for right */
532 for (y = MAX(top, 0); y < bottom && y < preview_h; y++) {
533 offset = y * preview_w + MAX(right - line_thickness, 0);
534 for (x = 0; x < draw_width; x += 2) {
535 ((gulong *) frame)[(offset + x) / 2] =
536 camapp->guide_rect_color;
545 cam_utils_convert_YUYV_to_UYVY(unsigned char *dst, unsigned char *src,
550 memset(dst, 0x00, length);
551 memcpy(dst, src + 1, length - 1);
553 for (i = 0; i < length; i++) {
561 void cam_utils_convert_UYVY_to_YUYV(char *dst, char *src, gint length)
565 memset(dst, 0x00, length);
566 memcpy((char *)dst + 1, (char *)src, length - 1);
568 for (i = 0; i < length; i++) {
569 if ((i % 2)) { /* even */
579 cam_utils_convert_YUYV_to_YUV420P(unsigned char *pInBuf, unsigned char *pOutBuf,
580 int width, int height)
582 unsigned char *pInY, *pInU, *pInV;
583 unsigned char *pOutY, *pOutU, *pOutV;
585 int nRowIters = height / 2;
586 int nColIters = width / 2;
594 pOutU = pOutBuf + width * height;
595 pOutV = pOutBuf + width * height * 5 / 4;
597 /* Iterate over half the number of rows, because inside there are 2 loops on columns */
598 for (rows = 0; rows < nRowIters; rows++) {
600 Iterate over half the number of columns, copy 2 pixels each time */
601 for (cols = 0; cols < nColIters; cols++) {
602 /* Copy Y of first pixel */
606 /* Copy Y of second pixel */
610 /* Copy U of all 4 pixels */
614 /* Copy V of all 4 pixels */
620 Iterate over half the number of columns, copy 2 pixels each time */
621 for (cols = 0; cols < nColIters; cols++) {
622 /* Copy Y of third pixel */
626 /* Copy Y of fourth pixel */
631 /* Skip U, V of third, fourth pixel */
640 cam_utils_convert_YUYV_to_YUV420P(byte *src, byte *dst, gint width, gint height)
643 int YUYV_length = 0, YUV420P_length = 0;
644 int temp_length = 0, temp_length2 = 0;
645 int pos_u = 0, pos_v = 0;
647 /* cam_debug( LOG_UI, " start" ); */
649 temp_length = width * height;
650 temp_length2 = temp_length >> 2;
651 YUYV_length = temp_length << 1;
652 YUV420P_length = (temp_length * 3) >> 1;
655 cam_debug( LOG_UI, " size[%dx%d], length YUYV[%d],YUV420P[%d]",
656 width, height, YUYV_length, YUV420P_length );
660 cam_critical(LOG_SYS, " dst is NULL");
664 for (i = 0; i < temp_length; i++) {
666 dst[i] = src[i << 1];
668 if (i % (width << 1) < width) {
671 dst[temp_length + pos_u] = src[(i << 1) + 1];
675 dst[temp_length + temp_length2 + pos_v] =
685 void *cam_utils_YUV422_to_ARGB(byte *frame, int width, int height)
687 /* source yuv is FOURCC YUYV, sampling format YUV 422 .
689 Byte Ordering (lowest byte) Y0, U0, Y1, V0 */
690 byte *frame_argb = malloc(width * height * 4); /* for ARGB */
692 if (frame_argb == NULL) {
695 memset(frame_argb,0,width * height * 4);
697 int i = 0, j = 0; /* row, column */
700 unsigned long pixel_idx = 0, rgb_index = 0;
704 for (i = 0; i < height; i++) {
705 for (j = 0; j < width; j++) {
707 if (j % 2) { /* odd */
710 /* u_idx = -1; v_idx = 1; */
714 /* u_idx = 1; v_idx = 3; */
717 y = frame[pixel_idx];
718 v = frame[pixel_idx + v_idx];
719 u = frame[pixel_idx + u_idx];
721 YuvToRgb(y, u, v, &r, &g, &b);
724 frame_argb[rgb_index++] = (byte)CLIPING(r);
725 frame_argb[rgb_index++] = (byte)CLIPING(g);
726 frame_argb[rgb_index++] = (byte)CLIPING(b);
727 frame_argb[rgb_index++] = 0xff;
729 pixel_idx += 2; /* yuv422, 4byte is 2 pixel */
733 return (void *)frame_argb;
736 void *cam_utils_IYUV_to_ARGB(byte *frame, int width, int height)
738 /* source yuv is FOURCC IYUV or I420, sampling format YUV 420. */
739 /* IYUV format is http://www.fourcc.org/yuv.php#IYUV */
741 byte *frame_argb = malloc(width * height * 4); /* for ARGB */
742 if (frame_argb == NULL) {
745 memset(frame_argb,0,width * height * 4);
747 int h = 0, w = 0; /* row, column */
750 unsigned long rgb_index = 0; /* ,pixel_idx=0; */
753 for (h = 0; h < height; h++) {
754 for (w = 0; w < width; w++) {
756 y = frame[h * height + w];
757 u = frame[(width * height) + idx];
758 v = frame[(width * height) + ((width * height) / 4) +
764 YuvToRgb(y, u, v, &r, &g, &b);
767 frame_argb[rgb_index++] = (byte)CLIPING(r);
768 frame_argb[rgb_index++] = (byte)CLIPING(g);
769 frame_argb[rgb_index++] = (byte)CLIPING(b);
770 frame_argb[rgb_index++] = 0;
774 if ((w == width - 1) && (!(h % 2)))
778 return (void *)frame_argb;
782 cam_utils_save_to_jpg_file(int storage_id, gchar *filename, void *frame,
783 int width, int height, GError **error)
785 cam_debug(LOG_MM, " [%dx%d] %s", width, height, filename);
787 int ret = CAMERA_ERROR_NONE;
789 CAM_TA_ACUM_ITEM_BEGIN("cam_utils_check_mmc_for_writing", 0);
790 if (storage_id == CAM_STORAGE_EXTERNAL) {
791 if (!cam_utils_check_mmc_for_writing(error)) {
795 CAM_TA_ACUM_ITEM_END("cam_utils_check_mmc_for_writing", 0);
797 cam_debug(LOG_SYS, " \n\n\n\n\n START JPEG ENCODING \n\n\n\n\n");
799 CAM_TA_ACUM_ITEM_BEGIN("image_util_encode_jpeg", 0);
800 ret = image_util_encode_jpeg(frame, width, height,
801 IMAGE_UTIL_COLORSPACE_YUYV, 90, filename);
802 CAM_TA_ACUM_ITEM_END("image_util_encode_jpeg", 0);
804 cam_debug(LOG_SYS, " \n\n\n\n\n END JPEG ENCODING \n\n\n\n\n");
807 cam_critical(LOG_MM, " image_util_encode_jpeg Failed [%x]", ret);
815 cam_utils_save_to_jpg_memory(byte **memory, unsigned int *size, void *src_frame,
816 int width, int height)
818 cam_debug(LOG_MM, " [%dx%d]", width, height);
820 int ret = CAMERA_ERROR_NONE;
822 cam_debug(LOG_SYS, " \n\n\n\n\n START JPEG ENCODING \n\n\n\n\n");
824 CAM_TA_ACUM_ITEM_BEGIN("image_util_encode_jpeg_to_memory", 0);
825 ret = image_util_encode_jpeg_to_memory(src_frame, width, height,
826 IMAGE_UTIL_COLORSPACE_YUYV, 90, (unsigned char **)memory, size);
827 CAM_TA_ACUM_ITEM_END("image_util_encode_jpeg_to_memory", 0);
829 cam_debug(LOG_SYS, " \n\n\n\n\n END JPEG ENCODING \n\n\n\n\n");
832 cam_critical(LOG_MM, "image_util_encode_jpeg_to_memory Failed [%x]", ret);
839 void *cam_utils_load_temp_file(gchar *filepath, gint *pfilesize)
841 struct stat fileinfo;
845 ret = stat(filepath, &fileinfo);
847 cam_critical(LOG_CAM, "can't get file infomation - error[%d]", ret);
851 gint filesize = fileinfo.st_size;
853 cam_critical(LOG_CAM, "can't get file infomation");
857 cam_debug(LOG_CAM, "temp file's file_path =%s, file_size =%d",
860 void *data = (void *)malloc(filesize);
865 memset(data, 0, filesize);
867 cam_debug(LOG_CAM, "data = %p ", data);
869 if ((fp = fopen(filepath, "r")) == NULL) {
874 cam_critical(LOG_CAM, "can't open file infomation");
877 if (fread(data, filesize, 1, fp) != 1) {
883 cam_critical(LOG_CAM, "can't read file infomation");
887 *pfilesize = filesize;
894 gboolean cam_utils_check_mmc_for_writing(GError **error)
898 const gchar *error_msg = NULL;
901 m_mmc_path = (char *)cam_file_get_external_image_path();
903 if (!g_file_test(m_mmc_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
904 error_code = CAM_ERROR_STORAGE_UNAVAILABLE;
905 error_msg = dgettext(PACKAGE, "IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
909 if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_state)) {
910 if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED) {
911 error_code = CAM_ERROR_STORAGE_UNAVAILABLE;
912 error_msg = dgettext(PACKAGE, "IDS_CAM_POP_MEMORY_CARD_REMOVED");
924 *error = cam_error_new_literal(error_code, error_msg);/*note:fix warnning*/
931 gboolean cam_utils_check_mmc_for_inserted_stats(void *data)
934 g_return_val_if_fail(data, FALSE);
936 struct appdata *ad = (struct appdata *)data;
937 cam_retv_if(ad == NULL, FALSE);
939 CamAppData *camapp = NULL;
940 camapp = ad->camapp_handle;
942 camapp = ad->camapp_handle;
943 cam_retv_if(camapp == NULL, FALSE);
945 if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_state)) {
946 if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED
947 && camapp->storage == CAM_STORAGE_EXTERNAL) {
955 int cam_utils_check_mmc_status(void)
959 if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status)) {
960 cam_warning(LOG_SYS, " Get MMC status failed");
966 int cam_utils_get_storage_id_from_filepath(const char *filepath)
968 char *strtemp = NULL;
970 strtemp = (char *)cam_file_get_internal_image_path();
973 if (!strncmp(filepath, strtemp, strlen(strtemp)))
974 return CAM_STORAGE_INTERNAL;
977 strtemp = (char *)cam_file_get_external_image_path();
979 if (!strncmp(filepath, strtemp, strlen(strtemp)))
980 return CAM_STORAGE_EXTERNAL;
983 return CAM_STORAGE_DCF;
988 cam_utils_safety_file_copy(const char *dst, const char *src, GError **error)
992 const gchar *error_msg = NULL;
998 cam_debug(LOG_FILE, " dst file name = %s", dst);
999 cam_debug(LOG_FILE, " dst file name = %s", src);
1001 fp_src = fopen(src, "r");
1002 if (fp_src == NULL) {
1003 error_code = CAM_ERROR_FILE_NOT_EXISTS;
1004 error_msg = dgettext(PACKAGE, "IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
1008 cam_debug(LOG_FILE, "start check mmc");
1010 if (cam_utils_get_storage_id_from_filepath(dst) == CAM_STORAGE_EXTERNAL) {
1011 if (!cam_utils_check_mmc_for_writing(error))
1015 cam_debug(LOG_FILE, "end check mmc");
1017 fp_dst = fopen(dst, "wb");
1018 if (fp_dst == NULL) {
1019 error_code = CAM_ERROR_FILE_REGISTER_FAILED;
1020 error_msg = dgettext(PACKAGE, "IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
1023 cam_debug(LOG_FILE, "start copy");
1025 unsigned long filesize = 0;
1026 unsigned long filecopyblocksize = 1024 * 512;
1027 unsigned long copysize = 0;
1028 struct stat fileinfo;
1030 void *buff = malloc(filecopyblocksize); /* 512k */
1034 /* get source file size */
1035 ret = stat(src, &fileinfo);
1037 cam_critical(LOG_CAM, "can't get file infomation - error[%d]", ret);
1045 filesize = fileinfo.st_size;
1046 cam_debug(LOG_FILE, "source file size %lu", filesize);
1050 if (filesize >= filecopyblocksize) {
1051 copysize = filecopyblocksize;
1053 copysize = filesize;
1056 cam_debug(LOG_FILE, "copy size %lu", copysize);
1057 if (fread(buff, copysize, 1, fp_src) != 1) {
1058 cam_debug(LOG_FILE, "read error!");
1066 if (buff && fwrite(buff, copysize, 1, fp_dst) != 1) {
1067 cam_debug(LOG_FILE, "file read or write error");
1068 error_code = CAM_ERROR_FILE;
1069 error_msg = dgettext(PACKAGE, "IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
1077 filesize -= copysize;
1078 } while (filesize > 0);
1080 error_code = CAM_ERROR_PANORAMA_LIB;
1081 error_msg = dgettext(PACKAGE, "IDS_CAM_BODY_FAILED_TO_BUILD_PANORAMIC_IMAGES_TRY_LATER");
1091 stat(filepath, &fileinfo);
1093 gint filesize = fileinfo.st_size;
1095 if (fwrite(g_result_panorama_image->bits, g_result_panorama_image->size, 1, fp) != 1)
1097 free(camapp->filename);
1098 camapp->filename = NULL;
1105 /*fsync(fp_dst->_fileno);*//*block for increasing formance of shot-to-shot */
1117 cam_debug(LOG_FILE, "file copy error, error number=%x", error_code);
1128 *error = cam_error_new_literal(error_code, error_msg);/*note:fix warnning*/
1135 static char *cam_utils_trim(char *str)
1139 for (; *i_buf && isspace(*i_buf); ++i_buf) ;
1144 gboolean cam_utils_parse_args(int argc, char *argv[], CamExeArgs *args)
1147 const char *delimiters = ",";
1151 for (index = 0; index < argc; index++) {
1155 if (argv[index][0] == '-' && argv[index][1] != '\0') {
1156 char *r_argv = strdup(cam_utils_trim((argv[index] + 2)));
1157 switch (argv[index][1]) {
1159 case 'd': /* daemon launch */
1162 CAM_LAUNCHING_MODE_DAEMON;
1166 case 'h': /* help */
1169 CAM_LAUNCHING_MODE_HELP;
1174 case 'v': /* review */
1176 args->review = TRUE;
1179 case 'r': /* resolution */
1182 char *result = NULL;
1183 char *save_string = NULL;
1184 result = strtok_r(r_argv, delimiters,&save_string);
1186 args->width = atoi(result);
1187 result = strtok_r(NULL, delimiters,&save_string);
1189 args->height = atoi(result);
1195 case 'm': /* cam_mode */
1198 if (0 == strcmp(r_argv, "IMAGE")) {
1199 args->cam_mode = CAM_CAMERA_MODE;
1200 } else if (0 == strcmp(r_argv, "VIDEO")) {
1201 args->cam_mode = CAM_CAMCORDER_MODE;
1203 if (r_argv != NULL) {
1214 case '-': /* launching appl.'s name */
1217 char *qualifier_to_find = "calling-app=";
1218 char *qualifier_field_start = NULL;
1221 qualifier_field_start = strstr(r_argv, qualifier_to_find);
1222 if (qualifier_field_start) {
1223 qualifier_field_start += strlen(qualifier_to_find);
1224 name = cam_utils_trim(qualifier_field_start);
1225 args->caller = strdup(name);
1227 cam_debug(LOG_SYS, "unknown arg : %s", argv[index]);
1234 case 'l': /* size limit */
1237 args->size_limit = atoi(r_argv);
1243 default: /* unknown option */
1244 cam_debug(LOG_SYS, "Unknown Option : %c",
1248 if (r_argv != NULL) {
1253 cam_debug(LOG_SYS, "unknown arg : %s", argv[index]);
1257 cam_debug(LOG_SYS, "cam_mode : %d", args->cam_mode);
1258 cam_debug(LOG_SYS, "resolution : [%dx%d]", args->width, args->height);
1259 cam_debug(LOG_SYS, "limit : %d", args->size_limit);
1260 cam_debug(LOG_SYS, "caller : %s", args->caller);
1264 gboolean cam_utils_check_wide_resolution(int resol_w, int resol_h)
1266 if ((ABS((gfloat)((resol_w * 3.0) / (resol_h * 4.0)) - 1.0 ) < CAM_EPSINON )
1267 || (ABS((gfloat)((resol_w * 25.0) / (resol_h * 36.0)) - 1.0) < CAM_EPSINON)) {
1268 cam_debug(LOG_UI, "Not Wide Resolution : [%d]x[%d]", resol_w,
1276 gboolean cam_utils_check_fake_image_exist(const char *fake_image_path)
1278 if (!g_file_test(fake_image_path, G_FILE_TEST_EXISTS)) {
1286 cam_utils_image_rotate(char *src, int src_width, int src_height, char *dst,
1287 int *dst_width, int *dst_height, int degree)
1290 cam_retv_if(src == NULL || dst == NULL || src_width == 0
1291 || src_height == 0, FALSE);
1292 cam_retv_if(degree != 0 && degree != 90 && degree != 180
1293 && degree != 270, FALSE);
1298 int *from = (int *)src;
1299 int *to = (int *)dst;
1302 *dst_width = src_width;
1303 *dst_height = src_height;
1304 memcpy(dst, src, src_width * src_height * 4);
1308 for (j = 0; j < src_height; j++) {
1309 for (i = 0; i < src_width; i++) {
1312 *dst_width = src_height;
1313 *dst_height = src_width;
1315 *(to + i * src_height + (src_height - j)) =
1316 *(from + j * src_width + i);
1317 } else if (degree == 180) {
1318 *dst_width = src_width;
1319 *dst_height = src_height;
1321 *(to + (src_height - j) * src_width +
1323 *(from + j * src_width + i);
1324 } else if (degree == 270) {
1325 *dst_width = src_height;
1326 *dst_height = src_width;
1328 *(to + (src_width - i) * src_height + j) =
1329 *(from + j * src_width + i);
1340 void cam_utils_set_windows_xy_to_videos_xy(CamVideoRectangle src,
1341 CamVideoRectangle *result,
1344 cam_retm_if(data == NULL, "data is null");
1345 struct appdata *ad = (struct appdata *)data;
1348 switch (ad->camcorder_rotate) {
1349 case CAMERA_ROTATION_NONE:
1350 result->x = src.x - ad->preview_offset_x;
1351 result->y = src.y - ad->preview_offset_y;
1355 case CAMERA_ROTATION_90:
1356 result->x = ad->win_height - ad->preview_offset_y - src.x;
1357 result->y = src.y - ad->preview_offset_y;
1361 case CAMERA_ROTATION_180:
1362 result->x = ad->win_width - src.x - ad->preview_offset_x;
1363 result->y = ad->win_height - src.y - ad->preview_offset_y;
1367 case CAMERA_ROTATION_270:
1368 result->x = src.x - ad->preview_offset_y;
1369 result->y = ad->win_width - src.y - ad->preview_offset_x;
1374 DEBUG_TRACE("REACHE UN-REACHED CODES");
1376 DEBUG_TRACE("songfeng result->x %d result->y %d",result->x, result->y);
1381 void cam_utils_set_videos_xy_to_windows_xy(CamVideoRectangle src,
1382 CamVideoRectangle *result,
1385 cam_retm_if(data == NULL, "data is null");
1386 struct appdata *ad = (struct appdata *)data;
1390 switch (ad->camcorder_rotate) {
1391 case CAMERA_ROTATION_NONE:
1393 * win: (x,y) -------->x video:(x,y) -------->x(width)
1399 result->x = src.x + ad->preview_offset_x;
1400 result->y = src.y + ad->preview_offset_y;
1401 /*notes:here,result->w: horizontal;result->h:vertical*/
1405 case CAMERA_ROTATION_90:
1406 /* win: (x,y) -------->x video:(x,y) (height)y<-----
1412 result->x = ad->win_height - (src.y + ad->preview_offset_y + src.w);
1413 result->y = src.x + ad->preview_offset_x;
1414 /*notes:here,result->w: horizontal;result->h:vertical*/
1418 case CAMERA_ROTATION_180:
1419 /* win: (x,y) -------->x video:(x,y) y(height)
1423 * -y (width)x<------
1425 result->x = ad->win_width - (src.x + ad->preview_offset_x + src.w);
1426 result->y = ad->win_height - (src.y + ad->preview_offset_y + src.h);
1427 /*notes:here,result->w: horizontal;result->h:vertical*/
1431 case CAMERA_ROTATION_270:
1432 cam_critical(LOG_CAM, "CAMERA_ROTATION_270");
1433 /* win: (x,y) -------->x video:(x,y) x(width)
1437 * -y -------------y(height)
1439 result->x = src.y + ad->preview_offset_y;
1440 result->y = ad->win_width - (src.x + ad->preview_offset_x + src.w);
1441 /*notes:here,result->w: horizontal;result->h:vertical*/
1446 DEBUG_TRACE("REACHE UN-REACHED CODES");
1451 gboolean cam_utils_request_main_pipe_handler(void *data, void *pipe_data, int cmd)
1453 struct appdata *ad = (struct appdata *)data;
1454 cam_retvm_if(ad == NULL, FALSE, "appdata is NULL");
1455 Ecore_Pipe_Data_Info pipe_info = {0,};
1456 pipe_info.data = pipe_data;
1457 pipe_info.cmd = cmd;
1458 return ecore_pipe_write(ad->main_pipe, (void *)&pipe_info, sizeof(Ecore_Pipe_Data_Info));