Closes app now when you hit the back button. This is a temp fix to keep media workin...
[profile/ivi/ug-camera-efl.git] / src / camera_utils.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
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
7  *
8  *        http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <image_util.h>
19 #include <malloc.h>
20 #include <ctype.h>
21 #include "camera_utils.h"
22 #include "cam_debug.h"
23 #include "cam_error.h"
24 #include "cam_ta.h"
25 #include "cam_mm.h"
26 #include "cam_file.h"
27
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
37
38 #define         CLIPING(data)  ((data) < 0 ? 0 : ((data) > 255) ? 255 : (data))
39 /*#define               SUPPORT_WINK            //use wink library */
40
41 static void YuvToRgb(int Y, int U, int V, int *R, int *G, int *B)
42 {
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);
47 }
48
49 /* #define SUPPORT_WINK */
50 #define CAPTUERD_IMAGE_SAVE_PATH "/tmp/captured_image.jpg"
51
52 #ifndef YUV422_SIZE
53 #define YUV422_SIZE(width, height) ((width) * (height) *  2)
54 #endif                          /* YUV422_SIZE */
55
56 #ifndef YUV420_SIZE
57 #define YUV420_SIZE(width, height) ((width) * (height) * 3 / 2)
58 #endif                          /* YUV420_SIZE */
59
60 #ifdef MAX_PATH
61 #define MAX_PATH 256
62 #endif
63
64 char *m_mmc_path = NULL;
65
66 /*get the torchlight on/off value*/
67 gboolean cam_utils_check_torchlight_status(void *data)
68 {
69         struct appdata *ad = (struct appdata *)data;
70         cam_retv_if(ad == NULL, FALSE);
71         int ret = -1; /*error, success is 0*/
72         int key_value = -1;
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);
75         if (ret == -1) {
76                 DEBUG_TRACE("vconf_get_int failed");
77                 return FALSE;
78         }
79         if (key_value) {
80                 /*torchlight is working*/
81                 ad->torchlight_on = TRUE;
82         } else {
83                 ad->torchlight_on = FALSE;
84         }
85         return TRUE;
86 }
87
88 /*get the battery warning low state*/
89 gboolean cam_utils_check_battery_warning_low(void)
90 {
91         int low_status = -1;
92
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)
96                         return TRUE;
97         } else {
98                 cam_warning(LOG_UI, "get setting failed %s",
99                             VCONFKEY_SYSMAN_BATTERY_STATUS_LOW);
100         }
101
102         return FALSE;
103 }
104 /*get the battery critical low state*/
105 gboolean cam_utils_check_battery_critical_low(void)
106 {
107         int low_status = -1;
108
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)
112                         return TRUE;
113         } else {
114                 cam_warning(LOG_UI, "get setting failed %s",
115                             VCONFKEY_SYSMAN_BAT_CRITICAL_LOW);
116         }
117
118         return FALSE;
119 }
120
121
122 gboolean cam_utils_check_call_running(void)
123 {
124         int call_state = -1;
125
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)
129                         return FALSE;
130         } else {
131                 cam_warning(LOG_UI, "get setting failed %s",
132                             VCONFKEY_CALL_STATE);
133                 return FALSE;
134         }
135
136         return TRUE;
137 }
138
139 int cam_utils_get_battery_level(void)
140 {
141         debug_fenter(LOG_SYS);
142
143         gint battery_level = -1;
144
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;
149         } else {
150                 cam_info(LOG_SYS, "get setting failed %s",
151                          VCONFKEY_SYSMAN_BATTERY_CAPACITY);
152         }
153
154         return battery_level;
155 }
156
157 gint cam_utils_get_charging_status(void)
158 {
159         debug_fenter(LOG_SYS);
160
161         gint charging = -1;
162
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);
166         } else {
167                 cam_info(LOG_SYS, "get setting failed %s",
168                          VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW);
169         }
170
171         return charging;
172 }
173
174 guint64 cam_system_get_remain_rec_time(void *data)
175 {
176         struct appdata *ad = (struct appdata *)data;
177         if (ad == NULL) {
178                 return 0;
179         }
180         CamAppData *camapp = ad->camapp_handle;
181         if (camapp == NULL) {
182                 return 0;
183         }
184         guint64 remain_time = 0;
185
186         {
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) {
191                         return 0;
192                 }
193                 free_space = cam_get_free_space(target_path);
194
195                 a_bitrate = cam_app_get_aenc_bitrate(ad);
196                 v_bitrate = cam_app_get_venc_bitrate(ad, camapp->video_quality);
197
198                 remain_time =
199                     (guint64)((free_space * 8) / (a_bitrate + v_bitrate));
200
201         }
202         return remain_time;
203 }
204
205 gint64 cam_system_get_still_count_by_resolution(void *data)
206 {
207         debug_fenter(LOG_CAM);
208
209         struct appdata *ad = (struct appdata *)data;
210         if (ad == NULL) {
211                 return 0;
212         }
213         CamAppData *camapp = ad->camapp_handle;
214         if (camapp == NULL) {
215                 return 0;
216         }
217
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) {
223                 return 0;
224         }
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);
228
229         /* TODO: Quality factor should be calculated later!
230          */
231         switch (camapp->photo_resolution) {
232         case CAM_RESOLUTION_3264x2448:
233         case CAM_RESOLUTION_3264x2176:
234                 avg_file_size = JPEG_8M_APPROXIMATELY_SIZE;
235                 break;
236
237         case CAM_RESOLUTION_3264x1960:
238         case CAM_RESOLUTION_3264x1836:
239                 avg_file_size = JPEG_W6M_APPROXIMATELY_SIZE;
240                 break;
241
242         case CAM_RESOLUTION_2800x1920:
243         case CAM_RESOLUTION_2560x1920:
244                 avg_file_size = JPEG_5M_APPROXIMATELY_SIZE;
245                 break;
246
247         case CAM_RESOLUTION_2560x1536:
248                 avg_file_size = JPEG_W4M_APPROXIMATELY_SIZE;
249                 break;
250
251         case CAM_RESOLUTION_2560x1440:
252         case CAM_RESOLUTION_2048x1536:
253                 avg_file_size = JPEG_3M_APPROXIMATELY_SIZE;
254                 break;
255
256         case CAM_RESOLUTION_2048x1152:
257                 avg_file_size = JPEG_W2M_APPROXIMATELY_SIZE;
258                 break;
259
260         case CAM_RESOLUTION_1920x1080:
261         case CAM_RESOLUTION_1600x1200:
262         case CAM_RESOLUTION_1392x1392:
263                 avg_file_size = JPEG_2M_APPROXIMATELY_SIZE;
264                 break;
265
266         case CAM_RESOLUTION_1280x720:
267         case CAM_RESOLUTION_1280x960:
268                 avg_file_size = JPEG_1M_APPROXIMATELY_SIZE;
269                 break;
270
271         case CAM_RESOLUTION_SVGA:
272                 avg_file_size = 150 * 1024 * 2;
273                 break;
274
275         case CAM_RESOLUTION_WVGA:
276                 avg_file_size = 130 * 1024 * 2;
277                 break;
278
279         case CAM_RESOLUTION_VGA:
280                 avg_file_size = JPEG_VGA_APPROXIMATELY_SIZE;
281                 break;
282
283         case CAM_RESOLUTION_WQVGA:
284                 avg_file_size = 100 * 1024 * 2;
285                 break;
286
287         case CAM_RESOLUTION_QVGA:
288                 avg_file_size = 100 * 1024 * 1.5;
289                 break;
290
291         case CAM_RESOLUTION_CIF:
292                 avg_file_size = 25 * 1024 * 2;
293                 break;
294
295         default:
296                 cam_critical(LOG_CAM, "unhandled resolution:%dx%d", HIWORD(camapp->photo_resolution), LOWORD(camapp->photo_resolution));
297                 return -1;
298         }
299
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);
304
305         return (int)MAX(remained_count, 0);
306 }
307
308 gint64 cam_get_free_space(const gchar *path)
309 {
310         struct statfs fs;
311         if (-1 == statfs(path, &fs))
312                 return -1;
313         return (gint64) fs.f_bsize * fs.f_bavail;
314 }
315
316
317 gboolean cam_utils_set_guide_rect_color(void *data) {
318
319         struct appdata *ad = (struct appdata *)data;
320         CamAppData *camapp = NULL;
321
322         cam_retv_if(ad == NULL, FALSE);
323         camapp = ad->camapp_handle;
324         cam_retv_if(camapp == NULL, FALSE);
325
326         DEBUG_TRACE(" ");
327
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);
331
332         switch (capture_video_format) {
333         case CAMERA_PIXEL_FORMAT_INVALID:
334                 {
335                         cam_debug(LOG_UI,"can not set preview format & shapshot format");
336                 }
337                 return FALSE;
338         case CAMERA_PIXEL_FORMAT_UYVY:
339         /*case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:*//*TODO:there is no this value in capi*/
340                 {
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;
345                 }
346                 break;
347         case CAMERA_PIXEL_FORMAT_YUYV:
348                 {
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;
353                 }
354                 break;
355         default:
356                 cam_debug(LOG_UI,"can not set preview format & shapshot format");
357                 return FALSE;
358         }
359         return TRUE;
360 }
361
362
363
364
365 gint64 cam_get_capacity_space(const gchar *path)
366 {
367         struct statfs fs;
368         if (-1 == statfs(path, &fs))
369                 return -1;
370         return (gint64) fs.f_bsize * fs.f_blocks;
371 }
372
373
374
375 void
376 cam_utils_draw_guide_rectangle(void *data, void *frame, int x_org, int y_org,
377                                int width, int height)
378 {
379         struct appdata *ad = (struct appdata *)data;
380         CamAppData *camapp = NULL;
381
382         cam_retm_if(ad == NULL, "appdata is NULL");
383         camapp = ad->camapp_handle;
384         cam_retm_if(camapp == NULL, "cam_handle is NULL");
385
386         int left, right, top, bottom, offset, y;
387         int xStart, xEnd, draw_width;
388
389         if (width == 0 && height == 0) {
390                 cam_debug(LOG_UI, " width,height of rectangle are zero");
391                 return;
392         }
393
394         left = 2 * (x_org / 2); /*  to draw on YUV422 or YUV420, we must start at even x */
395         right = left + 2 * (width / 2);
396         top = y_org;
397         bottom = y_org + height;
398
399         int preview_w = 0;      /* make sure this is even */
400         int preview_h = 0;
401         gboolean ret = FALSE;
402
403         ret = cam_mm_get_video_size( &preview_w, &preview_h);
404         cam_ret_if(ret == FALSE || preview_w == 0 || preview_h == 0);
405
406         xStart = MAX(left, 0);
407         xEnd = MIN(right, preview_w);
408
409         draw_width = xEnd - xStart;     /* for top and bottom */
410         if (draw_width <= 0)
411                 return;
412
413         int line_thickness = FIT_TO_RESOLUTION(2, 4);   /* make sure this is even */
414
415 #if 0
416         int frame_size = preview_w * preview_h;
417
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);
423
424         top = 2 * (top / 2);    /* to draw on YUV420, we must start at even y */
425         bottom = 2 * (bottom / 2);
426
427         /* top */
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,
431                        draw_width);
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);
439         }
440
441         /* bottom */
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,
446                        draw_width);
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);
454         }
455
456         /* left */
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,
462                                draw_width);
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,
470                                draw_width / 2);
471                 }
472         }
473         /* right */
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,
479                                draw_width);
480                         memset(((guchar *) frame) + offset + preview_w,
481                                guide_rect_colorY, draw_width);
482                         offset =
483                             y * preview_w / 4 + MAX(right - line_thickness,
484                                                     0) / 2;
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,
489                                draw_width / 2);
490                 }
491         }
492 #else
493
494         int x;
495
496         /* top */
497         for (y = top; y < top + line_thickness && y < preview_h; y++) {
498                 if (y < 0)
499                         continue;
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;
504                 }
505         }
506
507         /* bottom */
508         for (y = bottom - line_thickness; y < bottom && y < preview_h; y++) {
509                 if (y < 0)
510                         continue;
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;
515                 }
516         }
517
518         /* left */
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;
526                         }
527                 }
528         }
529         /* right */
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;
537                         }
538                 }
539         }
540 #endif
541
542 }
543
544 void
545 cam_utils_convert_YUYV_to_UYVY(unsigned char *dst, unsigned char *src,
546                                gint length)
547 {
548         int i = 0;
549
550         memset(dst, 0x00, length);
551         memcpy(dst, src + 1, length - 1);
552
553         for (i = 0; i < length; i++) {
554                 if (!(i % 2)) {
555                         dst[i + 1] = src[i];
556                 }
557         }
558
559 }
560
561 void cam_utils_convert_UYVY_to_YUYV(char *dst, char *src, gint length)
562 {
563         int i = 0;
564
565         memset(dst, 0x00, length);
566         memcpy((char *)dst + 1, (char *)src, length - 1);
567
568         for (i = 0; i < length; i++) {
569                 if ((i % 2)) {  /* even */
570                         dst[i - 1] = src[i];
571                 }
572         }
573
574 }
575
576 #if 1
577
578 void
579 cam_utils_convert_YUYV_to_YUV420P(unsigned char *pInBuf, unsigned char *pOutBuf,
580                                   int width, int height)
581 {
582         unsigned char *pInY, *pInU, *pInV;
583         unsigned char *pOutY, *pOutU, *pOutV;
584
585         int nRowIters = height / 2;
586         int nColIters = width / 2;
587
588         int rows, cols;
589
590         pInY = pInBuf;
591         pInU = pInBuf + 1;
592         pInV = pInBuf + 3;
593         pOutY = pOutBuf;
594         pOutU = pOutBuf + width * height;
595         pOutV = pOutBuf + width * height * 5 / 4;
596
597         /*  Iterate over half the number of rows, because inside there are 2 loops on columns */
598         for (rows = 0; rows < nRowIters; rows++) {
599                 /* Even 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 */
603                         *pOutY = *pInY;
604                         pOutY++;
605                         pInY += 2;
606                         /* Copy Y of second pixel */
607                         *pOutY = *pInY;
608                         pOutY++;
609                         pInY += 2;
610                         /* Copy U of all 4 pixels */
611                         *pOutU = *pInU;
612                         pOutU++;
613                         pInU += 4;
614                         /* Copy V of all 4 pixels */
615                         *pOutV = *pInV;
616                         pOutV++;
617                         pInV += 4;
618                 }
619                 /* Odd rows
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 */
623                         *pOutY = *pInY;
624                         pOutY++;
625                         pInY += 2;
626                         /* Copy Y of fourth pixel */
627                         *pOutY = *pInY;
628                         pOutY++;
629                         pInY += 2;
630                 }
631                 /* Skip U, V of third, fourth pixel */
632                 pInU += width * 2;
633                 pInV += width * 2;
634         }
635 }
636
637 #else
638
639 void
640 cam_utils_convert_YUYV_to_YUV420P(byte *src, byte *dst, gint width, gint height)
641 {
642         int i = 0;
643         int YUYV_length = 0, YUV420P_length = 0;
644         int temp_length = 0, temp_length2 = 0;
645         int pos_u = 0, pos_v = 0;
646
647         /* cam_debug( LOG_UI, " start" ); */
648
649         temp_length = width * height;
650         temp_length2 = temp_length >> 2;
651         YUYV_length = temp_length << 1;
652         YUV420P_length = (temp_length * 3) >> 1;
653
654         /*
655            cam_debug( LOG_UI, " size[%dx%d], length YUYV[%d],YUV420P[%d]",
656            width, height, YUYV_length, YUV420P_length );
657          */
658
659         if (dst == NULL) {
660                 cam_critical(LOG_SYS, " dst is NULL");
661                 return;
662         }
663
664         for (i = 0; i < temp_length; i++) {
665                 /*  Y data */
666                 dst[i] = src[i << 1];
667
668                 if (i % (width << 1) < width) {
669                         if (i % 2 == 0) {
670                                 /* U data */
671                                 dst[temp_length + pos_u] = src[(i << 1) + 1];
672                                 pos_u++;
673                         } else {
674                                 /*  V data */
675                                 dst[temp_length + temp_length2 + pos_v] =
676                                     src[(i << 1) + 1];
677                                 pos_v++;
678                         }
679                 }
680         }
681 }
682
683 #endif
684
685 void *cam_utils_YUV422_to_ARGB(byte *frame, int width, int height)
686 {
687         /* source yuv is FOURCC YUYV, sampling format YUV 422 .
688            yuv422 format
689            Byte Ordering (lowest byte) Y0, U0, Y1, V0 */
690         byte *frame_argb = malloc(width * height * 4);  /* for ARGB */
691
692         if (frame_argb == NULL) {
693                 return NULL;
694         }
695         memset(frame_argb,0,width * height * 4);
696
697         int i = 0, j = 0;       /*  row, column */
698         int y, u, v;
699         int r, g, b;
700         unsigned long pixel_idx = 0, rgb_index = 0;
701         short v_idx = 0;
702         short u_idx = 0;
703
704         for (i = 0; i < height; i++) {
705                 for (j = 0; j < width; j++) {
706
707                         if (j % 2) {    /* odd */
708                                 v_idx = -1;
709                                 u_idx = 1;
710                                 /* u_idx = -1; v_idx = 1; */
711                         } else {
712                                 v_idx = 1;
713                                 u_idx = 3;
714                                 /* u_idx = 1; v_idx = 3; */
715                         }
716
717                         y = frame[pixel_idx];
718                         v = frame[pixel_idx + v_idx];
719                         u = frame[pixel_idx + u_idx];
720
721                         YuvToRgb(y, u, v, &r, &g, &b);
722
723                         /* ARGB */
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;
728
729                         pixel_idx += 2; /* yuv422, 4byte is 2 pixel */
730                 }
731         }
732
733         return (void *)frame_argb;
734 }
735
736 void *cam_utils_IYUV_to_ARGB(byte *frame, int width, int height)
737 {
738         /* source yuv is FOURCC IYUV or I420, sampling format YUV 420. */
739         /* IYUV format is http://www.fourcc.org/yuv.php#IYUV */
740
741         byte *frame_argb = malloc(width * height * 4);  /* for ARGB */
742         if (frame_argb == NULL) {
743                 return NULL;
744         }
745         memset(frame_argb,0,width * height * 4);
746
747         int h = 0, w = 0;       /* row, column */
748         int y, u, v;
749         int r, g, b;
750         unsigned long rgb_index = 0;    /* ,pixel_idx=0; */
751         int idx = 0;
752
753         for (h = 0; h < height; h++) {
754                 for (w = 0; w < width; w++) {
755
756                         y = frame[h * height + w];
757                         u = frame[(width * height) + idx];
758                         v = frame[(width * height) + ((width * height) / 4) +
759                                   idx];
760
761                         if (w % 2)
762                                 idx++;
763
764                         YuvToRgb(y, u, v, &r, &g, &b);
765
766                         /* ARGB */
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;
771
772                 }
773
774                 if ((w == width - 1) && (!(h % 2)))
775                         idx -= width / 2;
776         }
777
778         return (void *)frame_argb;
779 }
780
781 gboolean
782 cam_utils_save_to_jpg_file(int storage_id, gchar *filename, void *frame,
783                            int width, int height, GError **error)
784 {
785         cam_debug(LOG_MM, " [%dx%d] %s", width, height, filename);
786
787         int ret = CAMERA_ERROR_NONE;
788
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)) {
792                         return FALSE;
793                 }
794         }
795         CAM_TA_ACUM_ITEM_END("cam_utils_check_mmc_for_writing", 0);
796
797         cam_debug(LOG_SYS, " \n\n\n\n\n START JPEG ENCODING \n\n\n\n\n");
798
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);
803
804         cam_debug(LOG_SYS, " \n\n\n\n\n END JPEG ENCODING \n\n\n\n\n");
805
806         if (ret != 0) {
807                 cam_critical(LOG_MM, " image_util_encode_jpeg Failed [%x]", ret);
808                 return FALSE;
809         }
810
811         return TRUE;
812 }
813
814 gboolean
815 cam_utils_save_to_jpg_memory(byte **memory, unsigned int *size, void *src_frame,
816                              int width, int height)
817 {
818         cam_debug(LOG_MM, " [%dx%d]", width, height);
819
820         int ret = CAMERA_ERROR_NONE;
821
822         cam_debug(LOG_SYS, " \n\n\n\n\n START JPEG ENCODING \n\n\n\n\n");
823
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);
828
829         cam_debug(LOG_SYS, " \n\n\n\n\n END JPEG ENCODING \n\n\n\n\n");
830
831         if (ret != 0) {
832                 cam_critical(LOG_MM, "image_util_encode_jpeg_to_memory Failed [%x]", ret);
833                 return FALSE;
834         }
835
836         return TRUE;
837 }
838
839 void *cam_utils_load_temp_file(gchar *filepath, gint *pfilesize)
840 {
841         struct stat fileinfo;
842         FILE *fp = NULL;
843         int ret;
844
845         ret = stat(filepath, &fileinfo);
846         if (ret == -1) {
847                 cam_critical(LOG_CAM, "can't get file infomation - error[%d]", ret);
848                 return NULL;
849         }
850
851         gint filesize = fileinfo.st_size;
852         if (filesize < 0) {
853                 cam_critical(LOG_CAM, "can't get file infomation");
854                 return NULL;
855         }
856
857         cam_debug(LOG_CAM, "temp file's  file_path =%s, file_size =%d",
858                   filepath, filesize);
859
860         void *data = (void *)malloc(filesize);
861
862         if (data == NULL) {
863                 return NULL;
864         }
865         memset(data, 0, filesize);
866
867         cam_debug(LOG_CAM, "data = %p ", data);
868
869         if ((fp = fopen(filepath, "r")) == NULL) {
870                 perror("fopen");
871                 if (data)
872                         free(data);
873
874                 cam_critical(LOG_CAM, "can't open file infomation");
875                 return NULL;
876         }
877         if (fread(data, filesize, 1, fp) != 1) {
878                 perror("fread");
879                 fclose(fp);
880                 if (data)
881                         free(data);
882
883                 cam_critical(LOG_CAM, "can't read file infomation");
884                 return NULL;
885         }
886
887         *pfilesize = filesize;
888
889         fclose(fp);
890
891         return data;
892 }
893
894 gboolean cam_utils_check_mmc_for_writing(GError **error)
895 {
896         gint error_code = 0;
897         int mmc_state = -1;
898         const gchar *error_msg = NULL;
899
900         if (!m_mmc_path)
901                 m_mmc_path = (char *)cam_file_get_external_image_path();
902
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");
906                 goto ERROR;
907         }
908
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");
913                         goto ERROR;
914                 }
915         }
916
917         return TRUE;
918
919  ERROR:
920
921         /* cam_set_error */
922         if (error_msg) {
923                 if (*error) {
924                         *error = cam_error_new_literal(error_code, error_msg);/*note:fix warnning*/
925                 }
926         }
927         return FALSE;
928
929 }
930
931 gboolean cam_utils_check_mmc_for_inserted_stats(void *data)
932 {
933         int mmc_state = -1;
934         g_return_val_if_fail(data, FALSE);
935
936         struct appdata *ad = (struct appdata *)data;
937         cam_retv_if(ad == NULL, FALSE);
938
939         CamAppData *camapp = NULL;
940         camapp = ad->camapp_handle;
941
942         camapp = ad->camapp_handle;
943         cam_retv_if(camapp == NULL, FALSE);
944
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) {
948                         return FALSE;
949                 }
950         }
951
952         return TRUE;
953 }
954
955 int cam_utils_check_mmc_status(void)
956 {
957         int status = -1;
958
959         if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status)) {
960                 cam_warning(LOG_SYS, " Get MMC status failed");
961         }
962
963         return status;
964 }
965
966 int cam_utils_get_storage_id_from_filepath(const char *filepath)
967 {
968         char *strtemp = NULL;
969
970         strtemp = (char *)cam_file_get_internal_image_path();
971
972         if (strtemp) {
973                 if (!strncmp(filepath, strtemp, strlen(strtemp)))
974                         return CAM_STORAGE_INTERNAL;
975         }
976
977         strtemp = (char *)cam_file_get_external_image_path();
978         if (strtemp) {
979                 if (!strncmp(filepath, strtemp, strlen(strtemp)))
980                         return CAM_STORAGE_EXTERNAL;
981         }
982
983         return CAM_STORAGE_DCF;
984
985 }
986
987 gboolean
988 cam_utils_safety_file_copy(const char *dst, const char *src, GError **error)
989 {
990
991         gint error_code = 0;
992         const gchar *error_msg = NULL;
993
994         FILE *fp_src = NULL;
995         FILE *fp_dst = NULL;
996         int ret;
997
998         cam_debug(LOG_FILE, " dst file name = %s", dst);
999         cam_debug(LOG_FILE, " dst file name = %s", src);
1000
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");
1005                 goto ERROR;
1006         } else {
1007
1008                 cam_debug(LOG_FILE, "start check mmc");
1009                 /* check mmc */
1010                 if (cam_utils_get_storage_id_from_filepath(dst) == CAM_STORAGE_EXTERNAL) {
1011                         if (!cam_utils_check_mmc_for_writing(error))
1012                                 goto ERROR;
1013                 }
1014
1015                 cam_debug(LOG_FILE, "end check mmc");
1016
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");
1021                         goto ERROR;
1022                 } else {
1023                         cam_debug(LOG_FILE, "start copy");
1024                         /* copy */
1025                         unsigned long filesize = 0;
1026                         unsigned long filecopyblocksize = 1024 * 512;
1027                         unsigned long copysize = 0;
1028                         struct stat fileinfo;
1029
1030                         void *buff = malloc(filecopyblocksize); /* 512k */
1031                         if (buff == NULL)
1032                                 goto ERROR;
1033
1034                         /* get source file size */
1035                         ret = stat(src, &fileinfo);
1036                         if (ret == -1) {
1037                                 cam_critical(LOG_CAM, "can't get file infomation - error[%d]", ret);
1038                                 if (buff) {
1039                                         free(buff);
1040                                         buff = NULL;
1041                                 }
1042                                 goto ERROR;
1043                         }
1044
1045                         filesize = fileinfo.st_size;
1046                         cam_debug(LOG_FILE, "source file size %lu", filesize);
1047
1048                         if (filesize > 0) {
1049                                 do {
1050                                         if (filesize >= filecopyblocksize) {
1051                                                 copysize = filecopyblocksize;
1052                                         } else {
1053                                                 copysize = filesize;
1054                                         }
1055
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!");
1059                                                 if (buff) {
1060                                                         free(buff);
1061                                                         buff = NULL;
1062                                                 }
1063                                                 goto ERROR;
1064                                         }
1065
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");
1070                                                 if (buff) {
1071                                                         free(buff);
1072                                                         buff = NULL;
1073                                                 }
1074                                                 goto ERROR;
1075                                         }
1076
1077                                         filesize -= copysize;
1078                                 } while (filesize > 0);
1079                         } else {
1080                                 error_code = CAM_ERROR_PANORAMA_LIB;
1081                                 error_msg = dgettext(PACKAGE, "IDS_CAM_BODY_FAILED_TO_BUILD_PANORAMIC_IMAGES_TRY_LATER");
1082                                 if (buff) {
1083                                         free(buff);
1084                                         buff = NULL;
1085                                 }
1086                                 goto ERROR;
1087                         }
1088
1089                         /*
1090
1091                            stat(filepath, &fileinfo);
1092
1093                            gint filesize = fileinfo.st_size;
1094
1095                            if (fwrite(g_result_panorama_image->bits, g_result_panorama_image->size, 1, fp) != 1)
1096                            {
1097                            free(camapp->filename);
1098                            camapp->filename = NULL;
1099                            }
1100                          */
1101                         if (buff) {
1102                                 free(buff);
1103                                 buff = NULL;
1104                         }
1105                         /*fsync(fp_dst->_fileno);*//*block for increasing formance of shot-to-shot */
1106                         fclose(fp_dst);
1107                 }
1108
1109                 fclose(fp_src);
1110
1111         }
1112
1113         return TRUE;
1114
1115  ERROR:
1116
1117         cam_debug(LOG_FILE, "file copy error, error number=%x", error_code);
1118
1119         if (fp_dst)
1120                 fclose(fp_dst);
1121
1122         if (fp_src)
1123                 fclose(fp_src);
1124
1125         /* cam_set_error  */
1126         if (error_msg) {
1127                 if (*error) {
1128                         *error = cam_error_new_literal(error_code, error_msg);/*note:fix warnning*/
1129                 }
1130         }
1131         return FALSE;
1132
1133 }
1134
1135 static char *cam_utils_trim(char *str)
1136 {
1137         char *i_buf = str;
1138
1139         for (; *i_buf && isspace(*i_buf); ++i_buf) ;
1140
1141         return i_buf;
1142 }
1143
1144 gboolean cam_utils_parse_args(int argc, char *argv[], CamExeArgs *args)
1145 {
1146         int index = 0;
1147         const char *delimiters = ",";
1148
1149         if (args == NULL)
1150                 return FALSE;
1151         for (index = 0; index < argc; index++) {
1152                 if (argv == NULL)
1153                         return FALSE;
1154
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]) {
1158 #if 0
1159                         case 'd':       /* daemon launch */
1160                                 {
1161                                         args->launch_type =
1162                                             CAM_LAUNCHING_MODE_DAEMON;
1163                                         return TRUE;
1164                                 }
1165                                 break;
1166                         case 'h':       /* help */
1167                                 {
1168                                         args->launch_type =
1169                                             CAM_LAUNCHING_MODE_HELP;
1170                                         return TRUE;
1171                                 }
1172                                 break;
1173 #endif
1174                         case 'v':       /* review */
1175                                 {
1176                                         args->review = TRUE;
1177                                 }
1178                                 break;
1179                         case 'r':       /* resolution */
1180                                 {
1181                                         if (r_argv) {
1182                                                 char *result = NULL;
1183                                                 char *save_string = NULL;
1184                                                 result = strtok_r(r_argv, delimiters,&save_string);
1185                                                 if (result != NULL)
1186                                                         args->width = atoi(result);
1187                                                 result = strtok_r(NULL, delimiters,&save_string);
1188                                                 if (result != NULL)
1189                                                         args->height = atoi(result);
1190                                         } else {
1191                                                 return FALSE;
1192                                         }
1193                                 }
1194                                 break;
1195                         case 'm':       /* cam_mode */
1196                                 {
1197                                         if (r_argv) {
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;
1202                                                 } else {
1203                                                         if (r_argv != NULL) {
1204                                                                 free(r_argv);
1205                                                                 r_argv = NULL;
1206                                                         }
1207                                                         return FALSE;
1208                                                 }
1209                                         } else {
1210                                                 return FALSE;
1211                                         }
1212                                 }
1213                                 break;
1214                         case '-':       /* launching appl.'s name */
1215                                 {
1216                                         if (r_argv) {
1217                                                 char *qualifier_to_find = "calling-app=";
1218                                                 char *qualifier_field_start = NULL;
1219                                                 char *name = NULL;
1220
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);
1226                                                 } else {
1227                                                         cam_debug(LOG_SYS, "unknown arg : %s", argv[index]);
1228                                                 }
1229                                         } else {
1230                                                 return FALSE;
1231                                         }
1232                                 }
1233                                 break;
1234                         case 'l':       /* size limit */
1235                                 {
1236                                         if (r_argv) {
1237                                                 args->size_limit = atoi(r_argv);
1238                                         } else {
1239                                                 return FALSE;
1240                                         }
1241                                 }
1242                                 break;
1243                         default:        /* unknown option */
1244                                 cam_debug(LOG_SYS, "Unknown Option : %c",
1245                                           argv[index][1]);
1246                                 break;
1247                         }
1248                         if (r_argv != NULL) {
1249                                 free(r_argv);
1250                                 r_argv = NULL;
1251                         }
1252                 } else {
1253                         cam_debug(LOG_SYS, "unknown arg : %s", argv[index]);
1254                 }
1255         }
1256
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);
1261         return TRUE;
1262 }
1263
1264 gboolean cam_utils_check_wide_resolution(int resol_w, int resol_h)
1265 {
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,
1269                           resol_h);
1270                 return FALSE;
1271         }
1272
1273         return TRUE;
1274 }
1275
1276 gboolean cam_utils_check_fake_image_exist(const char *fake_image_path)
1277 {
1278         if (!g_file_test(fake_image_path, G_FILE_TEST_EXISTS)) {
1279                 return FALSE;
1280         }
1281
1282         return TRUE;
1283 }
1284
1285 gboolean
1286 cam_utils_image_rotate(char *src, int src_width, int src_height, char *dst,
1287                        int *dst_width, int *dst_height, int degree)
1288 {
1289
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);
1294
1295         int i = 0;
1296         int j = 0;
1297
1298         int *from = (int *)src;
1299         int *to = (int *)dst;
1300
1301         if (degree == 0) {
1302                 *dst_width = src_width;
1303                 *dst_height = src_height;
1304                 memcpy(dst, src, src_width * src_height * 4);
1305                 return TRUE;
1306         }
1307
1308         for (j = 0; j < src_height; j++) {
1309                 for (i = 0; i < src_width; i++) {
1310
1311                         if (degree == 90) {
1312                                 *dst_width = src_height;
1313                                 *dst_height = src_width;
1314
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;
1320
1321                                 *(to + (src_height - j) * src_width +
1322                                   (src_width - i)) =
1323 *(from + j * src_width + i);
1324                         } else if (degree == 270) {
1325                                 *dst_width = src_height;
1326                                 *dst_height = src_width;
1327
1328                                 *(to + (src_width - i) * src_height + j) =
1329                                     *(from + j * src_width + i);
1330                         }
1331
1332                 }
1333         }
1334
1335         return TRUE;
1336
1337 }
1338
1339
1340 void cam_utils_set_windows_xy_to_videos_xy(CamVideoRectangle src,
1341                                                                 CamVideoRectangle *result,
1342                                                                 void *data)
1343 {
1344         cam_retm_if(data == NULL, "data is null");
1345         struct appdata *ad = (struct appdata *)data;
1346         if (!ad || !result)
1347                 return;
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;
1352                 result->w = src.w;
1353                 result->h = src.h;
1354                 break;
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;
1358                 result->w = src.h;
1359                 result->h = src.w;
1360                 break;
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;
1364                 result->w = src.w;
1365                 result->h = src.h;
1366                 break;
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;
1370                 result->w = src.h;
1371                 result->h = src.w;
1372                 break;
1373         default:
1374                 DEBUG_TRACE("REACHE UN-REACHED CODES");
1375         }
1376         DEBUG_TRACE("songfeng result->x %d result->y %d",result->x, result->y);
1377
1378 }
1379
1380
1381 void cam_utils_set_videos_xy_to_windows_xy(CamVideoRectangle src,
1382                                                                 CamVideoRectangle *result,
1383                                                                 void *data)
1384 {
1385         cam_retm_if(data == NULL, "data is null");
1386         struct appdata *ad = (struct appdata *)data;
1387         if (!ad || !result)
1388                 return;
1389
1390         switch (ad->camcorder_rotate) {
1391         case CAMERA_ROTATION_NONE:
1392                 /*
1393                 *       win: (x,y)  -------->x          video:(x,y) -------->x(width)
1394                 *                -                                      -
1395                 *                -                                      -
1396                 *                -                                      -
1397                 *                -y                             y(height)
1398                 */
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*/
1402                 result->w = src.w;
1403                 result->h = src.h;
1404                 break;
1405         case CAMERA_ROTATION_90:
1406                 /* win: (x,y)  -------->x   video:(x,y) (height)y<-----
1407                 *                -                                              -
1408                 *                -                                              -
1409                 *                -                                              -
1410                 *                -y                                     x(width)
1411                 */
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*/
1415                 result->w = src.h;
1416                 result->h = src.w;
1417                 break;
1418         case CAMERA_ROTATION_180:
1419                 /* win: (x,y)  -------->x   video:(x,y)                 y(height)
1420                 *                -                                              -
1421                 *                -                                              -
1422                 *                -                                              -
1423                 *                -y                     (width)x<------
1424                 */
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*/
1428                 result->w = src.w;
1429                 result->h = src.h;
1430                 break;
1431         case CAMERA_ROTATION_270:
1432                 cam_critical(LOG_CAM, "CAMERA_ROTATION_270");
1433                 /* win: (x,y)  -------->x   video:(x,y) x(width)
1434                 *                -                               -
1435                 *                -                               -
1436                 *                -                               -
1437                 *                -y                      -------------y(height)
1438                 */
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*/
1442                 result->w = src.h;
1443                 result->h = src.w;
1444                 break;
1445         default:
1446                 DEBUG_TRACE("REACHE UN-REACHED CODES");
1447         }
1448
1449 }
1450
1451 gboolean cam_utils_request_main_pipe_handler(void *data, void *pipe_data, int cmd)
1452 {
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));
1459
1460 }
1461
1462 //end file