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