Updating INI configuration file to work with most cameras.
[profile/ivi/camera.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://www.tizenopensource.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_2M_APPROXIMATELY_SIZE      566000
34 #define JPEG_1M_APPROXIMATELY_SIZE      283000
35 #define JPEG_VGA_APPROXIMATELY_SIZE     135000
36
37 #define         CLIPING(data)  ((data) < 0 ? 0 : ((data) > 255) ? 255 : (data))
38 /*#define               SUPPORT_WINK            //use wink library */
39
40 void static YuvToRgb(int Y, int U, int V, int *R, int *G, int *B)
41 {
42         *B = CLIPING((76284 * (Y - 16) + 132252 * (U - 128)) >> 16);
43         *G = CLIPING((76284 * (Y - 16) - 53281 * (V - 128) -
44                       25625 * (U - 128)) >> 16);
45         *R = CLIPING((76284 * (Y - 16) + 104595 * (V - 128)) >> 16);
46 }
47
48 #define FAKE_YUV_IMAGE_WIDTH            640
49 #define FAKE_YUV_IMAGE_HEIGHT   480
50 #define FAKE_YUV_IMAGE_PATH             PREFIX"/share/camera/fake.yuv"
51
52 /* #define SUPPORT_WINK */
53 #define CAPTUERD_IMAGE_SAVE_PATH "/tmp/captured_image.jpg"
54
55 #ifndef YUV422_SIZE
56 #define YUV422_SIZE(width, height) ((width) * (height) *  2)
57 #endif                          /* YUV422_SIZE */
58
59 #ifndef YUV420_SIZE
60 #define YUV420_SIZE(width, height) ((width) * (height) * 3 / 2)
61 #endif                          /* YUV420_SIZE */
62
63 #ifdef MAX_PATH
64 #define MAX_PATH 256
65 #endif
66
67 char *m_mmc_path = NULL;
68
69 /*get the torchlight on/off value*/
70 gboolean cam_utils_check_torchlight_status(void *data)
71 {
72         struct appdata *ad = (struct appdata *)data;
73         cam_retv_if(ad == NULL, FALSE);
74         int ret = -1; /*error, success is 0*/
75         int key_value = -1;
76         ret = vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TORCH_LIGHT, &key_value); /*ret [-1: error, 0:succes], key_value[on:1 , off:0]*/
77         DEBUG_TRACE("VCONFKEY_SETAPPL_ACCESSIBILITY_TORCH_LIGHT is changed to %d", key_value);
78         if (ret == -1) {
79                 DEBUG_TRACE("vconf_get_int failed");
80                 return FALSE;
81         }
82         if (key_value) {
83                 /*torchlight is working*/
84                 ad->torchlight_on = TRUE;
85         } else {
86                 ad->torchlight_on = FALSE;
87         }
88         return TRUE;
89 }
90
91 /*get the battery warning low state*/
92 gboolean cam_utils_check_battery_warning_low(void)
93 {
94         int low_status = -1;
95
96         if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &low_status)) {
97                 cam_debug(LOG_UI, "battery status low = %d", low_status);
98                 if (low_status <= VCONFKEY_SYSMAN_BAT_WARNING_LOW)
99                         return TRUE;
100         } else {
101                 cam_warning(LOG_UI, "get setting failed %s",
102                             VCONFKEY_SYSMAN_BATTERY_STATUS_LOW);
103         }
104
105         return FALSE;
106 }
107 /*get the battery critical low state*/
108 gboolean cam_utils_check_battery_critical_low(void)
109 {
110         int low_status = -1;
111
112         if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &low_status)) {
113                 cam_debug(LOG_UI, "battery status low = %d", low_status);
114                 if (low_status <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
115                         return TRUE;
116         } else {
117                 cam_warning(LOG_UI, "get setting failed %s",
118                             VCONFKEY_SYSMAN_BAT_CRITICAL_LOW);
119         }
120
121         return FALSE;
122 }
123
124
125 int cam_utils_get_battery_level(void)
126 {
127         debug_fenter(LOG_SYS);
128
129         gint battery_level = -1;
130
131         /*  VCONF_BATTERY_LEVEL or VCONFKEY_INDICATOR_MODULE_BATTERY */
132         if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, &battery_level)) {
133                 cam_info(LOG_SYS, "battery_level = %d", battery_level);
134                 return battery_level;
135         } else {
136                 cam_info(LOG_SYS, "get setting failed %s",
137                          VCONFKEY_SYSMAN_BATTERY_CAPACITY);
138         }
139
140         return battery_level;
141 }
142
143 gint cam_utils_get_charging_status(void)
144 {
145         debug_fenter(LOG_SYS);
146
147         gint charging = -1;
148
149         /*  VCONF_BATTERY_CHARGE_NOW or VCONFKEY_INDICATOR_MODULE_CHARGE */
150         if (!vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &charging)) {
151                 cam_info(LOG_SYS, "charging = %d", charging);
152         } else {
153                 cam_info(LOG_SYS, "get setting failed %s",
154                          VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW);
155         }
156
157         return charging;
158 }
159
160 guint64 cam_system_get_remain_rec_time(void *data)
161 {
162         struct appdata *ad = (struct appdata *)data;
163         if (ad == NULL) {
164                 return 0;
165         }
166         CamAppData *camapp = ad->camapp_handle;
167         if (camapp == NULL) {
168                 return 0;
169         }
170         guint64 remain_time = 0;
171
172         if (camapp->camera_mode == CAM_CAMCORDER_MODE) {
173                 gint64 free_space = 0;
174                 guint a_bitrate = 0, v_bitrate = 0;
175                 gchar* target_path = (gchar*)cam_app_get_target_path();
176                 if (target_path == NULL) {
177                         return 0;
178                 }
179                 free_space = cam_get_free_space(target_path);
180
181                 a_bitrate = cam_app_get_aenc_bitrate(ad);
182                 v_bitrate = cam_app_get_venc_bitrate(ad, camapp->video_quality);
183
184                 remain_time =
185                     (guint64)((free_space * 8) / (a_bitrate + v_bitrate));
186
187         }
188         return remain_time;
189 }
190
191 gint64 cam_system_get_still_count_by_resolution(void *data)
192 {
193         debug_fenter(LOG_CAM);
194
195         struct appdata *ad = (struct appdata *)data;
196         if (ad == NULL) {
197                 return 0;
198         }
199         CamAppData *camapp = ad->camapp_handle;
200         if (camapp == NULL) {
201                 return 0;
202         }
203
204         gint64 free_space = 0;
205         gint64 remained_count = 0;
206         gint64 avg_file_size = -1;
207         gchar* target_path = (gchar*)cam_app_get_target_path();
208         if (target_path == NULL) {
209                 return 0;
210         }
211         free_space = cam_get_free_space(target_path);
212         DEBUG_TRACE("FREE SPACE =%d", free_space);
213         free_space = (free_space - STILL_MINIMAL_SPACE);
214
215         /* TODO: Quality factor should be calculated later!
216          */
217         switch (camapp->resolution) {
218         case CAM_RESOLUTION_3264x2448:
219                 avg_file_size = JPEG_8M_APPROXIMATELY_SIZE;
220                 break;
221
222         case CAM_RESOLUTION_3264x1960:
223         case CAM_RESOLUTION_3264x1836:
224                 avg_file_size = JPEG_W6M_APPROXIMATELY_SIZE;
225                 break;
226
227         case CAM_RESOLUTION_2800x1920:
228         case CAM_RESOLUTION_2560x1920:
229                 avg_file_size = JPEG_5M_APPROXIMATELY_SIZE;
230                 break;
231
232         case CAM_RESOLUTION_2560x1536:
233                 avg_file_size = JPEG_W4M_APPROXIMATELY_SIZE;
234                 break;
235
236         case CAM_RESOLUTION_2560x1440:
237         case CAM_RESOLUTION_2048x1536:
238                 avg_file_size = JPEG_3M_APPROXIMATELY_SIZE;
239                 break;
240
241         case CAM_RESOLUTION_1920x1080:
242         case CAM_RESOLUTION_1600x1200:
243                 avg_file_size = JPEG_2M_APPROXIMATELY_SIZE;
244                 break;
245
246         case CAM_RESOLUTION_1280x720:
247         case CAM_RESOLUTION_1280x960:
248                 avg_file_size = JPEG_1M_APPROXIMATELY_SIZE;
249                 break;
250
251         case CAM_RESOLUTION_SVGA:
252                 avg_file_size = 150 * 1024 * 2;
253                 break;
254
255         case CAM_RESOLUTION_WVGA:
256                 avg_file_size = 130 * 1024 * 2;
257                 break;
258
259         case CAM_RESOLUTION_VGA:
260                 avg_file_size = JPEG_VGA_APPROXIMATELY_SIZE;
261                 break;
262
263         case CAM_RESOLUTION_WQVGA:
264                 avg_file_size = 100 * 1024 * 2;
265                 break;
266
267         case CAM_RESOLUTION_QVGA:
268                 avg_file_size = 100 * 1024 * 1.5;
269                 break;
270
271         case CAM_RESOLUTION_CIF:
272                 avg_file_size = 25 * 1024 * 2;
273                 break;
274
275         default:
276                 critical_msg(LOG_CAM, "unhandled resolution:%dx%d",
277                              HIWORD(camapp->resolution),
278                              LOWORD(camapp->resolution));
279                 return -1;
280         }
281
282         remained_count = free_space / avg_file_size;
283         DEBUG_TRACE("FREE SPACE =%d", free_space);
284         DEBUG_TRACE("avg_file_size =%d", avg_file_size);
285         DEBUG_TRACE("remained_count =%d", remained_count);
286
287         return (int)MAX(remained_count, 0);
288 }
289
290 gint64 cam_get_free_space(const gchar *path)
291 {
292         struct statfs fs;
293         if (-1 == statfs(path, &fs))
294                 return -1;
295         return (gint64) fs.f_bsize * fs.f_bavail;
296 }
297
298
299 gboolean cam_utils_set_guide_rect_color(void *data) {
300
301         struct appdata *ad = (struct appdata *)data;
302         CamAppData *camapp = NULL;
303
304         cam_retv_if(ad == NULL, FALSE);
305         camapp = ad->camapp_handle;
306         cam_retv_if(camapp == NULL, FALSE);
307
308         DEBUG_TRACE(" ");
309
310         int capture_video_format = CAMERA_PIXEL_FORMAT_INVALID;
311         cam_mm_get_video_source_format(NULL, &capture_video_format);
312         DEBUG_TRACE("capture_video_format =%d", capture_video_format);
313
314         switch (capture_video_format) {
315         case CAMERA_PIXEL_FORMAT_INVALID:
316                 {
317                         cam_debug(LOG_UI,"can not set preview format & shapshot format");
318                 }
319                 return FALSE;
320         case CAMERA_PIXEL_FORMAT_UYVY:
321         /*case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:*//*TODO:there is no this value in capi*/
322                 {
323                         camapp->guide_rect_green = UYVY_GUIDE_RECT_GREEN;
324                         camapp->guide_rect_orange = UYVY_GUIDE_RECT_ORANGE;
325                         camapp->guide_rect_red = UYVY_GUIDE_RECT_RED;
326                         camapp->guide_rect_white = UYVY_GUIDE_RECT_WHITE;
327                 }
328                 break;
329         case CAMERA_PIXEL_FORMAT_YUYV:
330                 {
331                         camapp->guide_rect_green= YUYV_GUIDE_RECT_GREEN;
332                         camapp->guide_rect_orange= YUYV_GUIDE_RECT_ORANGE;
333                         camapp->guide_rect_red= YUYV_GUIDE_RECT_RED;
334                         camapp->guide_rect_white = YUYV_GUIDE_RECT_WHITE;
335                 }
336                 break;
337         default:
338                 cam_debug(LOG_UI,"can not set preview format & shapshot format");
339                 return FALSE;
340         }
341         return TRUE;
342 }
343
344
345
346
347 gint64 cam_get_capacity_space(const gchar *path)
348 {
349         struct statfs fs;
350         if (-1 == statfs(path, &fs))
351                 return -1;
352         return (gint64) fs.f_bsize * fs.f_blocks;
353 }
354
355
356
357 void
358 cam_utils_draw_guide_rectangle(void *data, void *frame, int x_org, int y_org,
359                                int width, int height)
360 {
361         struct appdata *ad = (struct appdata *)data;
362         CamAppData *camapp = NULL;
363
364         cam_retm_if(ad == NULL, "appdata is NULL");
365         camapp = ad->camapp_handle;
366         cam_retm_if(camapp == NULL, "cam_handle is NULL");
367
368         int left, right, top, bottom, offset, y;
369         int xStart, xEnd, draw_width;
370
371         if (width == 0 && height == 0) {
372                 cam_debug(LOG_UI, " width,height of rectangle are zero");
373                 return;
374         }
375
376         left = 2 * (x_org / 2); /*  to draw on YUV422 or YUV420, we must start at even x */
377         right = left + 2 * (width / 2);
378         top = y_org;
379         bottom = y_org + height;
380
381         int preview_w = 0;      /* make sure this is even */
382         int preview_h = 0;
383         gboolean ret = FALSE;
384
385         ret = cam_mm_get_video_size( &preview_w, &preview_h);
386         cam_ret_if(ret == FALSE || preview_w == 0 || preview_h == 0);
387
388         xStart = MAX(left, 0);
389         xEnd = MIN(right, preview_w);
390
391         draw_width = xEnd - xStart;     /* for top and bottom */
392         if (draw_width <= 0)
393                 return;
394
395         int line_thickness = FIT_TO_RESOLUTION(2, 4);   /* make sure this is even */
396
397 #if 0
398         int frame_size = preview_w * preview_h;
399
400         guchar guide_rect_colorY =
401             (guchar) ((camapp->guide_rect_color >> 8) & 0xFF);
402         guchar guide_rect_colorU = (guchar) ((camapp->guide_rect_color) & 0xFF);
403         guchar guide_rect_colorV =
404             (guchar) ((camapp->guide_rect_color >> 16) & 0xFF);
405
406         top = 2 * (top / 2);    /* to draw on YUV420, we must start at even y */
407         bottom = 2 * (bottom / 2);
408
409         /* top */
410         for (y = MAX(top, 0); y < top + line_thickness && y < preview_h; y += 2) {      /* line_thickness, and preview_h must be even */
411                 offset = y * preview_w + xStart;
412                 memset(((guchar *) frame) + offset, guide_rect_colorY,
413                        draw_width);
414                 memset(((guchar *) frame) + offset + preview_w,
415                        guide_rect_colorY, draw_width);
416                 offset = y * preview_w / 4 + xStart / 2;
417                 memset(((guchar *) frame) + frame_size + offset,
418                        guide_rect_colorU, draw_width / 2);
419                 memset(((guchar *) frame) + frame_size + frame_size / 4 +
420                        offset, guide_rect_colorV, draw_width / 2);
421         }
422
423         /* bottom */
424         for (y = MAX(bottom - line_thickness, 0);
425              y < bottom && y < preview_h; y += 2) {
426                 offset = y * preview_w + xStart;
427                 memset(((guchar *) frame) + offset, guide_rect_colorY,
428                        draw_width);
429                 memset(((guchar *) frame) + offset + preview_w,
430                        guide_rect_colorY, draw_width);
431                 offset = y * preview_w / 4 + xStart / 2;
432                 memset(((guchar *) frame) + frame_size + offset,
433                        guide_rect_colorU, draw_width / 2);
434                 memset(((guchar *) frame) + frame_size + frame_size / 4 +
435                        offset, guide_rect_colorV, draw_width / 2);
436         }
437
438         /* left */
439         if (left + line_thickness > 0 && left < preview_w) {
440                 draw_width = MIN(MIN(line_thickness, line_thickness + left), preview_w - left); /* now calculated for left */
441                 for (y = MAX(top, 0); y < bottom && y < preview_h; y += 2) {
442                         offset = y * preview_w + xStart;
443                         memset(((guchar *) frame) + offset, guide_rect_colorY,
444                                draw_width);
445                         memset(((guchar *) frame) + offset + preview_w,
446                                guide_rect_colorY, draw_width);
447                         offset = y * preview_w / 4 + xStart / 2;
448                         memset(((guchar *) frame) + frame_size + offset,
449                                guide_rect_colorU, draw_width / 2);
450                         memset(((guchar *) frame) + frame_size +
451                                frame_size / 4 + offset, guide_rect_colorV,
452                                draw_width / 2);
453                 }
454         }
455         /* right */
456         if (right > 0 && right - line_thickness < preview_w) {
457                 draw_width = MIN(MIN(line_thickness, right), preview_w - right + line_thickness);       /* now calculated for right */
458                 for (y = MAX(top, 0); y < bottom && y < preview_h; y += 2) {
459                         offset = y * preview_w + MAX(right - line_thickness, 0);
460                         memset(((guchar *) frame) + offset, guide_rect_colorY,
461                                draw_width);
462                         memset(((guchar *) frame) + offset + preview_w,
463                                guide_rect_colorY, draw_width);
464                         offset =
465                             y * preview_w / 4 + MAX(right - line_thickness,
466                                                     0) / 2;
467                         memset(((guchar *) frame) + frame_size + offset,
468                                guide_rect_colorU, draw_width / 2);
469                         memset(((guchar *) frame) + frame_size +
470                                frame_size / 4 + offset, guide_rect_colorV,
471                                draw_width / 2);
472                 }
473         }
474 #else
475
476         int x;
477
478         /* top */
479         for (y = top; y < top + line_thickness && y < preview_h; y++) {
480                 if (y < 0)
481                         continue;
482                 offset = y * preview_w + xStart;
483                 for (x = 0; x < draw_width; x += 2) {
484                         ((gulong *) frame)[(offset + x) / 2] =
485                             camapp->guide_rect_color;
486                 }
487         }
488
489         /* bottom */
490         for (y = bottom - line_thickness; y < bottom && y < preview_h; y++) {
491                 if (y < 0)
492                         continue;
493                 offset = y * preview_w + xStart;
494                 for (x = 0; x < draw_width; x += 2) {
495                         ((gulong *) frame)[(offset + x) / 2] =
496                             camapp->guide_rect_color;
497                 }
498         }
499
500         /* left */
501         if (left + line_thickness >= 0 && left < preview_w) {
502                 draw_width = MIN(MIN(line_thickness, line_thickness + left), preview_w - left); /*  now calculated for left */
503                 for (y = MAX(top, 0); y < bottom && y < preview_h; y++) {
504                         offset = y * preview_w + xStart;
505                         for (x = 0; x < draw_width; x += 2) {
506                                 ((gulong *) frame)[(offset + x) / 2] =
507                                     camapp->guide_rect_color;
508                         }
509                 }
510         }
511         /* right */
512         if (right >= 0 && right - line_thickness < preview_w) {
513                 draw_width = MIN(MIN(line_thickness, right), preview_w - right + line_thickness);       /* now calculated for right */
514                 for (y = MAX(top, 0); y < bottom && y < preview_h; y++) {
515                         offset = y * preview_w + MAX(right - line_thickness, 0);
516                         for (x = 0; x < draw_width; x += 2) {
517                                 ((gulong *) frame)[(offset + x) / 2] =
518                                     camapp->guide_rect_color;
519                         }
520                 }
521         }
522 #endif
523
524 }
525
526 void
527 cam_utils_convert_YUYV_to_UYVY(unsigned char *dst, unsigned char *src,
528                                gint length)
529 {
530         int i = 0;
531
532         memset(dst, 0x00, length);
533         memcpy(dst, src + 1, length - 1);
534
535         for (i = 0; i < length; i++) {
536                 if (!(i % 2)) {
537                         dst[i + 1] = src[i];
538                 }
539         }
540
541 }
542
543 void cam_utils_convert_UYVY_to_YUYV(char *dst, char *src, gint length)
544 {
545         int i = 0;
546
547         memset(dst, 0x00, length);
548         memcpy((char *)dst + 1, (char *)src, length - 1);
549
550         for (i = 0; i < length; i++) {
551                 if ((i % 2)) {  /* even */
552                         dst[i - 1] = src[i];
553                 }
554         }
555
556 }
557
558 #if 1
559
560 void
561 cam_utils_convert_YUYV_to_YUV420P(unsigned char *pInBuf, unsigned char *pOutBuf,
562                                   int width, int height)
563 {
564         unsigned char *pInY, *pInU, *pInV;
565         unsigned char *pOutY, *pOutU, *pOutV;
566
567         int nRowIters = height / 2;
568         int nColIters = width / 2;
569
570         int rows, cols;
571
572         pInY = pInBuf;
573         pInU = pInBuf + 1;
574         pInV = pInBuf + 3;
575         pOutY = pOutBuf;
576         pOutU = pOutBuf + width * height;
577         pOutV = pOutBuf + width * height * 5 / 4;
578
579         /*  Iterate over half the number of rows, because inside there are 2 loops on columns */
580         for (rows = 0; rows < nRowIters; rows++) {
581                 /* Even rows
582                    Iterate over half the number of columns, copy 2 pixels each time */
583                 for (cols = 0; cols < nColIters; cols++) {
584                         /* Copy Y of first pixel */
585                         *pOutY = *pInY;
586                         pOutY++;
587                         pInY += 2;
588                         /* Copy Y of second pixel */
589                         *pOutY = *pInY;
590                         pOutY++;
591                         pInY += 2;
592                         /* Copy U of all 4 pixels */
593                         *pOutU = *pInU;
594                         pOutU++;
595                         pInU += 4;
596                         /* Copy V of all 4 pixels */
597                         *pOutV = *pInV;
598                         pOutV++;
599                         pInV += 4;
600                 }
601                 /* Odd rows
602                    Iterate over half the number of columns, copy 2 pixels each time */
603                 for (cols = 0; cols < nColIters; cols++) {
604                         /* Copy Y of third pixel */
605                         *pOutY = *pInY;
606                         pOutY++;
607                         pInY += 2;
608                         /* Copy Y of fourth pixel */
609                         *pOutY = *pInY;
610                         pOutY++;
611                         pInY += 2;
612                 }
613                 /* Skip U, V of third, fourth pixel */
614                 pInU += width * 2;
615                 pInV += width * 2;
616         }
617 }
618
619 #else
620
621 void
622 cam_utils_convert_YUYV_to_YUV420P(byte *src, byte *dst, gint width, gint height)
623 {
624         int i = 0;
625         int YUYV_length = 0, YUV420P_length = 0;
626         int temp_length = 0, temp_length2 = 0;
627         int pos_u = 0, pos_v = 0;
628
629         /* cam_debug( LOG_UI, " start" ); */
630
631         temp_length = width * height;
632         temp_length2 = temp_length >> 2;
633         YUYV_length = temp_length << 1;
634         YUV420P_length = (temp_length * 3) >> 1;
635
636         /*
637            cam_debug( LOG_UI, " size[%dx%d], length YUYV[%d],YUV420P[%d]",
638            width, height, YUYV_length, YUV420P_length );
639          */
640
641         if (dst == NULL) {
642                 cam_critical(LOG_SYS, " dst is NULL");
643                 return;
644         }
645
646         for (i = 0; i < temp_length; i++) {
647                 /*  Y data */
648                 dst[i] = src[i << 1];
649
650                 if (i % (width << 1) < width) {
651                         if (i % 2 == 0) {
652                                 /* U data */
653                                 dst[temp_length + pos_u] = src[(i << 1) + 1];
654                                 pos_u++;
655                         } else {
656                                 /*  V data */
657                                 dst[temp_length + temp_length2 + pos_v] =
658                                     src[(i << 1) + 1];
659                                 pos_v++;
660                         }
661                 }
662         }
663 }
664
665 #endif
666
667 void *cam_utils_YUV422_to_ARGB(byte *frame, int width, int height)
668 {
669         /* source yuv is FOURCC YUYV, sampling format YUV 422 .
670            yuv422 format
671            Byte Ordering (lowest byte) Y0, U0, Y1, V0 */
672         byte *frame_argb = malloc(width * height * 4);  /* for ARGB */
673
674         if (frame_argb == NULL) {
675                 return NULL;
676         }
677         memset(frame_argb,0,width * height * 4);
678
679         int i = 0, j = 0;       /*  row, column */
680         int y, u, v;
681         int r, g, b;
682         unsigned long pixel_idx = 0, rgb_index = 0;
683         short v_idx = 0;
684         short u_idx = 0;
685
686         for (i = 0; i < height; i++) {
687                 for (j = 0; j < width; j++) {
688
689                         if (j % 2) {    /* odd */
690                                 v_idx = -1;
691                                 u_idx = 1;
692                                 /* u_idx = -1; v_idx = 1; */
693                         } else {
694                                 v_idx = 1;
695                                 u_idx = 3;
696                                 /* u_idx = 1; v_idx = 3; */
697                         }
698
699                         y = frame[pixel_idx];
700                         v = frame[pixel_idx + v_idx];
701                         u = frame[pixel_idx + u_idx];
702
703                         YuvToRgb(y, u, v, &r, &g, &b);
704
705                         /* ARGB */
706                         frame_argb[rgb_index++] = (byte)CLIPING(r);
707                         frame_argb[rgb_index++] = (byte)CLIPING(g);
708                         frame_argb[rgb_index++] = (byte)CLIPING(b);
709                         frame_argb[rgb_index++] = 0xff;
710
711                         pixel_idx += 2; /* yuv422, 4byte is 2 pixel */
712                 }
713         }
714
715         return (void *)frame_argb;
716 }
717
718 void *cam_utils_IYUV_to_ARGB(byte *frame, int width, int height)
719 {
720         /* source yuv is FOURCC IYUV or I420, sampling format YUV 420. */
721         /* IYUV format is http://www.fourcc.org/yuv.php#IYUV */
722
723         byte *frame_argb = malloc(width * height * 4);  /* for ARGB */
724         if (frame_argb == NULL) {
725                 return NULL;
726         }
727         memset(frame_argb,0,width * height * 4);
728
729         int h = 0, w = 0;       /* row, column */
730         int y, u, v;
731         int r, g, b;
732         unsigned long rgb_index = 0;    /* ,pixel_idx=0; */
733         int idx = 0;
734
735         for (h = 0; h < height; h++) {
736                 for (w = 0; w < width; w++) {
737
738                         y = frame[h * height + w];
739                         u = frame[(width * height) + idx];
740                         v = frame[(width * height) + ((width * height) / 4) +
741                                   idx];
742
743                         if (w % 2)
744                                 idx++;
745
746                         YuvToRgb(y, u, v, &r, &g, &b);
747
748                         /* ARGB */
749                         frame_argb[rgb_index++] = (byte)CLIPING(r);
750                         frame_argb[rgb_index++] = (byte)CLIPING(g);
751                         frame_argb[rgb_index++] = (byte)CLIPING(b);
752                         frame_argb[rgb_index++] = 0;
753
754                 }
755
756                 if ((w == width - 1) && (!(h % 2)))
757                         idx -= width / 2;
758         }
759
760         return (void *)frame_argb;
761 }
762
763 gboolean
764 cam_utils_save_to_jpg_file(int storage_id, gchar *filename, void *frame,
765                            int width, int height, GError **error)
766 {
767         cam_debug(LOG_MM, " [%dx%d] %s", width, height, filename);
768
769         int ret = CAMERA_ERROR_NONE;
770
771         CAM_TA_ACUM_ITEM_BEGIN("cam_utils_check_mmc_for_writing", 0);
772         if (storage_id == CAM_STORAGE_EXTERNAL) {
773                 if (!cam_utils_check_mmc_for_writing(error)) {
774                         return FALSE;
775                 }
776         }
777         CAM_TA_ACUM_ITEM_END("cam_utils_check_mmc_for_writing", 0);
778
779         cam_debug(LOG_SYS, " \n\n\n\n\n START JPEG ENCODING \n\n\n\n\n");
780
781         CAM_TA_ACUM_ITEM_BEGIN("image_util_encode_jpeg", 0);
782         ret = image_util_encode_jpeg(frame, width, height,
783                         IMAGE_UTIL_COLORSPACE_YUYV, 90, filename);
784         CAM_TA_ACUM_ITEM_END("image_util_encode_jpeg", 0);
785
786         cam_debug(LOG_SYS, " \n\n\n\n\n END JPEG ENCODING \n\n\n\n\n");
787
788         if (ret != 0) {
789                 cam_critical(LOG_MM, " image_util_encode_jpeg Failed [%x]", ret);
790                 return FALSE;
791         }
792
793         return TRUE;
794 }
795
796 gboolean
797 cam_utils_save_to_jpg_memory(byte **memory, unsigned int *size, void *src_frame,
798                              int width, int height)
799 {
800         cam_debug(LOG_MM, " [%dx%d]", width, height);
801
802         int ret = CAMERA_ERROR_NONE;
803
804         cam_debug(LOG_SYS, " \n\n\n\n\n START JPEG ENCODING \n\n\n\n\n");
805
806         CAM_TA_ACUM_ITEM_BEGIN("image_util_encode_jpeg_to_memory", 0);
807         ret = image_util_encode_jpeg_to_memory(src_frame, width, height,
808                         IMAGE_UTIL_COLORSPACE_YUYV, 90, (unsigned char **)memory, size);
809         CAM_TA_ACUM_ITEM_END("image_util_encode_jpeg_to_memory", 0);
810
811         cam_debug(LOG_SYS, " \n\n\n\n\n END JPEG ENCODING \n\n\n\n\n");
812
813         if (ret != 0) {
814                 cam_critical(LOG_MM, "image_util_encode_jpeg_to_memory Failed [%x]", ret);
815                 return FALSE;
816         }
817
818         return TRUE;
819 }
820
821 void *cam_utils_load_temp_file(gchar *filepath, gint *pfilesize)
822 {
823         struct stat fileinfo;
824         FILE *fp = NULL;
825
826         stat(filepath, &fileinfo);
827
828         gint filesize = fileinfo.st_size;
829         if (filesize < 0) {
830                 cam_critical(LOG_CAM, "can't get file infomation");
831                 return NULL;
832         }
833
834         cam_debug(LOG_CAM, "temp file's  file_path =%s, file_size =%d",
835                   filepath, filesize);
836
837         void *data = (void *)malloc(filesize);
838
839         if (data == NULL) {
840                 return NULL;
841         }
842         memset(data,0,filesize);
843
844         cam_debug(LOG_CAM, "data = %p ", data);
845
846         if ((fp = fopen(filepath, "r")) == NULL) {
847                 perror("fopen");
848                 cam_critical(LOG_CAM, "can't open file infomation");
849                 return NULL;
850         }
851         if (fread(data, filesize, 1, fp) != 1) {
852                 perror("fread");
853                 fclose(fp);
854                 cam_critical(LOG_CAM, "can't read file infomation");
855                 return NULL;
856         }
857
858         *pfilesize = filesize;
859
860         fclose(fp);
861
862         return data;
863 }
864
865 gboolean cam_utils_check_mmc_for_writing(GError **error)
866 {
867         gint error_code = 0;
868         int mmc_state = -1;
869         const gchar *error_msg = NULL;
870
871         if (!m_mmc_path)
872                 m_mmc_path = (char *)cam_file_get_external_image_path();
873
874         if (!g_file_test(m_mmc_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
875                 error_code = CAM_ERROR_STORAGE_UNAVAILABLE;
876                 error_msg = _("IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
877                 goto ERROR;
878         }
879
880         if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_state)) {
881                 if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED) {
882                         error_code = CAM_ERROR_STORAGE_UNAVAILABLE;
883                         error_msg = _("IDS_CAM_POP_MEMORY_CARD_REMOVED");
884                         goto ERROR;
885                 }
886         }
887
888         return TRUE;
889
890  ERROR:
891
892         /* cam_set_error */
893         if (error_msg) {
894                 if (*error) {
895                         *error = cam_error_new_literal(error_code, error_msg);/*note:fix warnning*/
896                 }
897         }
898         return FALSE;
899
900 }
901
902 gboolean cam_utils_check_mmc_for_inserted_stats(void *data)
903 {
904         int mmc_state = -1;
905         g_return_val_if_fail(data, FALSE);
906
907         struct appdata *ad = (struct appdata *)data;
908         cam_retv_if(ad == NULL, FALSE);
909
910         CamAppData *camapp = NULL;
911         camapp = ad->camapp_handle;
912
913         camapp = ad->camapp_handle;
914         cam_retv_if(camapp == NULL, FALSE);
915
916         if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_state)) {
917                 if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED
918                     && camapp->storage == CAM_STORAGE_EXTERNAL) {
919                         return FALSE;
920                 }
921         }
922
923         return TRUE;
924 }
925
926 int cam_utils_check_mmc_status(void)
927 {
928         int status = -1;
929
930         if (!vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status)) {
931                 cam_warning(LOG_SYS, " Get MMC status failed");
932         }
933
934         return status;
935 }
936
937 int cam_utils_get_storage_id_from_filepath(const char *filepath)
938 {
939         char *strtemp = NULL;
940
941         strtemp = (char *)cam_file_get_internal_image_path();
942
943         if (strtemp) {
944                 if (!strncmp(filepath, strtemp, sizeof(strtemp)))
945                         return CAM_STORAGE_INTERNAL;
946         }
947
948         strtemp = (char *)cam_file_get_external_image_path();
949         if (strtemp) {
950                 if (!strncmp(filepath, strtemp, sizeof(strtemp)))
951                         return CAM_STORAGE_EXTERNAL;
952         }
953
954         return CAM_STORAGE_DCF;
955
956 }
957
958 gboolean
959 cam_utils_safety_file_copy(const char *dst, const char *src, GError **error)
960 {
961
962         gint error_code = 0;
963         const gchar *error_msg = NULL;
964
965         FILE *fp_src = NULL;
966         FILE *fp_dst = NULL;
967
968         cam_debug(LOG_FILE, " dst file name = %s", dst);
969         cam_debug(LOG_FILE, " dst file name = %s", src);
970
971         fp_src = fopen(src, "r");
972         if (fp_src == NULL) {
973                 error_code = CAM_ERROR_FILE_NOT_EXISTS;
974                 error_msg = _("IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
975                 goto ERROR;
976         } else {
977
978                 cam_debug(LOG_FILE, "start check mmc");
979                 /* check mmc */
980                 if (cam_utils_get_storage_id_from_filepath(dst) ==
981                     CAM_STORAGE_EXTERNAL) {
982                         if (!cam_utils_check_mmc_for_writing(error))
983                                 goto ERROR;
984                 }
985
986                 cam_debug(LOG_FILE, "end check mmc");
987
988                 fp_dst = fopen(dst, "wb");
989                 if (fp_dst == NULL) {
990                         error_code = CAM_ERROR_FILE_REGISTER_FAILED;
991                         error_msg =
992                             _("IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
993                         goto ERROR;
994                 } else {
995                         cam_debug(LOG_FILE, "start copy");
996                         /* copy */
997                         unsigned long filesize = 0;
998                         unsigned long filecopyblocksize = 1024 * 512;
999                         unsigned long copysize = 0;
1000                         struct stat fileinfo;
1001
1002                         void *buff = malloc(filecopyblocksize); /* 512k */
1003                         if (buff == NULL)
1004                                 goto ERROR;
1005
1006                         /* get source file size */
1007                         stat(src, &fileinfo);
1008
1009                         filesize = fileinfo.st_size;
1010                         cam_debug(LOG_FILE, "source file size %lu", filesize);
1011
1012                         if (filesize > 0) {
1013                                 do {
1014
1015                                         if (filesize >= filecopyblocksize) {
1016                                                 copysize = filecopyblocksize;
1017                                         } else {
1018                                                 copysize = filesize;
1019                                         }
1020
1021                                         cam_debug(LOG_FILE, "copy size %lu",
1022                                                   copysize);
1023                                         if (fread(buff, copysize, 1, fp_src) != 1) {
1024                                                 cam_debug(LOG_FILE, "read error!");
1025                                                 if (buff) {
1026                                                         free(buff);
1027                                                         buff = NULL;
1028                                                 }
1029                                                 goto ERROR;
1030                                         }
1031
1032                                         if (buff
1033                                             && fwrite(buff, copysize, 1,
1034                                                       fp_dst) != 1) {
1035                                                 cam_debug(LOG_FILE,
1036                                                           "file read or write error");
1037                                                 error_code = CAM_ERROR_FILE;
1038                                                 error_msg =
1039                                                     _
1040                                                     ("IDS_CAM_BODY_UNABLE_TO_SAVE_NOT_ENOUGH_MEMORY");
1041                                                 if (buff) {
1042                                                         free(buff);
1043                                                         buff = NULL;
1044                                                 }
1045                                                 goto ERROR;
1046                                         }
1047
1048                                         filesize -= copysize;
1049
1050                                 } while (filesize > 0);
1051
1052                         }
1053
1054                         if (buff) {
1055                                 free(buff);
1056                                 buff = NULL;
1057                         }
1058                         /*fsync(fp_dst->_fileno);*//*block for increasing formance of shot-to-shot */
1059                         fclose(fp_dst);
1060                 }
1061
1062                 fclose(fp_src);
1063
1064         }
1065
1066         return TRUE;
1067
1068  ERROR:
1069
1070         cam_debug(LOG_FILE, "file copy error, error number=%x", error_code);
1071
1072         if (fp_dst)
1073                 fclose(fp_dst);
1074
1075         if (fp_src)
1076                 fclose(fp_src);
1077
1078         /* cam_set_error  */
1079         if (error_msg) {
1080                 if (*error) {
1081                         *error = cam_error_new_literal(error_code, error_msg);/*note:fix warnning*/
1082                 }
1083         }
1084         return FALSE;
1085
1086 }
1087
1088 static char *cam_utils_trim(char *str)
1089 {
1090         char *i_buf = str;
1091
1092         for (; *i_buf && isspace(*i_buf); ++i_buf) ;
1093
1094         return i_buf;
1095 }
1096
1097 gboolean cam_utils_parse_args(int argc, char *argv[], CamExeArgs *args)
1098 {
1099         int index = 0;
1100         const char *delimiters = ",";
1101
1102         if (args == NULL)
1103                 return FALSE;
1104
1105         for (index = 0; index < argc; index++) {
1106                 if (argv == NULL)
1107                         return FALSE;
1108
1109                 if (argv[index][0] == '-' && argv[index][1] != '\0') {
1110                         char *r_argv = strdup(cam_utils_trim((argv[index] + 2)));
1111                         switch (argv[index][1]) {
1112 #if 0
1113                         case 'd':       /* daemon launch */
1114                                 {
1115                                         args->launch_type =
1116                                             CAM_LAUNCHING_MODE_DAEMON;
1117                                         return TRUE;
1118                                 }
1119                                 break;
1120                         case 'h':       /* help */
1121                                 {
1122                                         args->launch_type =
1123                                             CAM_LAUNCHING_MODE_HELP;
1124                                         return TRUE;
1125                                 }
1126                                 break;
1127 #endif
1128                         case 'v':       /* review */
1129                                 {
1130                                         args->review = TRUE;
1131                                 }
1132                                 break;
1133                         case 'r':       /* resolution */
1134                                 {
1135                                         if (r_argv) {
1136                                                 char *result = NULL;
1137                                                 char *save_string = NULL;
1138                                                 result = strtok_r(r_argv, delimiters,&save_string);
1139                                                 if (result != NULL)
1140                                                         args->width = atoi(result);
1141                                                 result = strtok_r(NULL, delimiters,&save_string);
1142                                                 if (result != NULL)
1143                                                         args->height = atoi(result);
1144                                         } else {
1145                                                 return FALSE;
1146                                         }
1147                                 }
1148                                 break;
1149                         case 'm':       /* cam_mode */
1150                                 {
1151                                         if (r_argv) {
1152                                                 if (0 == strcmp(r_argv, "IMAGE")) {
1153                                                         args->cam_mode = CAM_CAMERA_MODE;
1154                                                 } else if (0 == strcmp(r_argv, "VIDEO")) {
1155                                                         args->cam_mode = CAM_CAMCORDER_MODE;
1156                                                 } else {
1157                                                         if (r_argv != NULL) {
1158                                                                 free(r_argv);
1159                                                                 r_argv = NULL;
1160                                                         }
1161                                                         return FALSE;
1162                                                 }
1163                                         } else {
1164                                                 return FALSE;
1165                                         }
1166                                 }
1167                                 break;
1168                         case '-':       /* launching appl.'s name */
1169                                 {
1170                                         if (r_argv) {
1171                                                 char *qualifier_to_find = "calling-app=";
1172                                                 char *qualifier_field_start = NULL;
1173                                                 char *name = NULL;
1174
1175                                                 qualifier_field_start = strstr(r_argv, qualifier_to_find);
1176                                                 if (qualifier_field_start) {
1177                                                         qualifier_field_start += strlen(qualifier_to_find);
1178                                                         name = cam_utils_trim(qualifier_field_start);
1179                                                         args->caller = strdup(name);
1180                                                 } else {
1181                                                         cam_debug(LOG_SYS, "unknown arg : %s", argv[index]);
1182                                                 }
1183                                         } else {
1184                                                 return FALSE;
1185                                         }
1186                                 }
1187                                 break;
1188                         case 'l':       /* size limit */
1189                                 {
1190                                         if (r_argv) {
1191                                                 args->size_limit = atoi(r_argv);
1192                                         } else {
1193                                                 return FALSE;
1194                                         }
1195                                 }
1196                                 break;
1197                         default:        /* unknown option */
1198                                 cam_debug(LOG_SYS, "Unknown Option : %c",
1199                                           argv[index][1]);
1200                                 break;
1201                         }
1202                         if (r_argv != NULL) {
1203                                 free(r_argv);
1204                                 r_argv = NULL;
1205                         }
1206                 } else {
1207                         cam_debug(LOG_SYS, "unknown arg : %s", argv[index]);
1208                 }
1209         }
1210
1211         cam_debug(LOG_SYS, "cam_mode : %d", args->cam_mode);
1212         cam_debug(LOG_SYS, "resolution : [%dx%d]", args->width, args->height);
1213         cam_debug(LOG_SYS, "limit : %d", args->size_limit);
1214         cam_debug(LOG_SYS, "caller : %s", args->caller);
1215         return TRUE;
1216 }
1217
1218 gboolean cam_utils_check_wide_resolution(int resol_w, int resol_h)
1219 {
1220         if ((ABS((gfloat)((resol_w * 3.0) / (resol_h * 4.0)) - 1.0 ) < CAM_EPSINON )
1221             || (ABS((gfloat)((resol_w * 25.0) / (resol_h * 36.0)) - 1.0) < CAM_EPSINON)) {
1222                 cam_debug(LOG_UI, "Not Wide Resolution : [%d]x[%d]", resol_w,
1223                           resol_h);
1224                 return FALSE;
1225         }
1226
1227         return TRUE;
1228 }
1229
1230 gboolean cam_utils_check_fake_image_exist(const char *fake_image_path)
1231 {
1232         if (!g_file_test(fake_image_path, G_FILE_TEST_EXISTS)) {
1233                 return FALSE;
1234         }
1235
1236         return TRUE;
1237 }
1238
1239 int cam_utils_captured_image_select(void *data, const char *file_path)
1240 {
1241         cam_retv_if(data == NULL, FALSE);
1242         struct appdata *ad = (struct appdata *)data;
1243         CamAppData *camapp = NULL;
1244         camapp = ad->camapp_handle;
1245         cam_retv_if(camapp == NULL, FALSE);
1246
1247         return 0;
1248 }
1249
1250 gboolean
1251 cam_utils_image_rotate(char *src, int src_width, int src_height, char *dst,
1252                        int *dst_width, int *dst_height, int degree)
1253 {
1254
1255         cam_retv_if(src == NULL || dst == NULL || src_width == 0
1256                     || src_height == 0, FALSE);
1257         cam_retv_if(degree != 0 && degree != 90 && degree != 180
1258                     && degree != 270, FALSE);
1259
1260         int i = 0;
1261         int j = 0;
1262
1263         int *from = (int *)src;
1264         int *to = (int *)dst;
1265
1266         if (degree == 0) {
1267                 *dst_width = src_width;
1268                 *dst_height = src_height;
1269                 memcpy(dst, src, src_width * src_height * 4);
1270                 return TRUE;
1271         }
1272
1273         for (j = 0; j < src_height; j++) {
1274                 for (i = 0; i < src_width; i++) {
1275
1276                         if (degree == 90) {
1277                                 *dst_width = src_height;
1278                                 *dst_height = src_width;
1279
1280                                 *(to + i * src_height + (src_height - j)) =
1281                                     *(from + j * src_width + i);
1282                         } else if (degree == 180) {
1283                                 *dst_width = src_width;
1284                                 *dst_height = src_height;
1285
1286                                 *(to + (src_height - j) * src_width +
1287                                   (src_width - i)) =
1288 *(from + j * src_width + i);
1289                         } else if (degree == 270) {
1290                                 *dst_width = src_height;
1291                                 *dst_height = src_width;
1292
1293                                 *(to + (src_width - i) * src_height + j) =
1294                                     *(from + j * src_width + i);
1295                         }
1296
1297                 }
1298         }
1299
1300         return TRUE;
1301
1302 }
1303
1304
1305 void cam_utils_set_windows_xy_to_videos_xy(CamVideoRectangle src,
1306                                                                 CamVideoRectangle *result,
1307                                                                 void *data)
1308 {
1309         cam_retm_if(data == NULL, "data is null");
1310         struct appdata *ad = (struct appdata *)data;
1311         if (!ad || !result)
1312                 return;
1313         gdouble width_ratio = 0.0;
1314         gdouble height_ratio = 0.0;
1315         width_ratio = (gdouble) ad->camfw_video_width/ad->preview_w;
1316         height_ratio = (gdouble) ad->camfw_video_height/ad->preview_h;
1317         DEBUG_TRACE("result->x %d result->y %d",result->x, result->y);
1318         switch (ad->camcorder_rotate) {
1319         case CAMERA_ROTATION_NONE:
1320                 /* win: (x,y)  -------->x (width)
1321                 *  video:(x,y) -
1322                 *                -
1323                 *                -
1324                 *                -y(height)
1325                 */
1326                 result->x = width_ratio * (src.x - ad->preview_offset_x);
1327                 result->y = height_ratio * (src.y - ad->preview_offset_y);
1328                 /*notes:here,result->w: horizontal;result->h:vertical*/
1329                 result->w = src.w * width_ratio;
1330                 result->h = src.h * height_ratio;
1331                 break;
1332         case CAMERA_ROTATION_90:
1333                 /* win: (x,y)  -------->x   video:(x,y) (height)y<-----
1334                 *                -                                              -
1335                 *                -                                              -
1336                 *                -                                              -
1337                 *                -y                                     x(width)
1338                 */
1339                 result->x = height_ratio * (ad->win_height - ad->preview_offset_y - src.x);
1340                 result->y = width_ratio * (src.y - ad->preview_offset_y);
1341                 /*notes:here,result->w: horizontal;result->h:vertical*/
1342                 result->w = src.w * height_ratio;
1343                 result->h = src.h * width_ratio;
1344                 break;
1345         case CAMERA_ROTATION_180:
1346                 /* win: (x,y)  -------->x   video:(x,y)                 y(height)
1347                 *                -                                              -
1348                 *                -                                              -
1349                 *                -                                              -
1350                 *                -y                     (width)x<------
1351                 */
1352                 result->x = width_ratio * (ad->win_width - src.x - ad->preview_offset_x);
1353                 result->y = height_ratio * (ad->win_height - src.y - ad->preview_offset_y);
1354                 /*notes:here,result->w: horizontal;result->h:vertical*/
1355                 result->w = src.w * width_ratio;
1356                 result->h = src.h * height_ratio;
1357                 break;
1358         case CAMERA_ROTATION_270:
1359                 /* win: (x,y)  -------->x   video:(x,y) x(width)
1360                 *                -                               -
1361                 *                -                               -
1362                 *                -                               -
1363                 *                -y                      -------------y(height)
1364                 */
1365                 result->x = height_ratio * (src.x - ad->preview_offset_y);
1366                 result->y = width_ratio * (ad->win_width - src.y - ad->preview_offset_x);
1367                 /*notes:here,result->w: horizontal;result->h:vertical*/
1368                 result->w = src.w * height_ratio;
1369                 result->h = src.h * width_ratio;
1370                 break;
1371         default:
1372                 DEBUG_TRACE("REACHE UN-REACHED CODES");
1373         }
1374         DEBUG_TRACE("songfeng result->x %d result->y %d",result->x, result->y);
1375
1376 }
1377
1378
1379 void cam_utils_set_videos_xy_to_windows_xy(CamVideoRectangle src,
1380                                                                 CamVideoRectangle *result,
1381                                                                 void *data)
1382 {
1383         cam_retm_if(data == NULL, "data is null");
1384         struct appdata *ad = (struct appdata *)data;
1385         if (!ad || !result)
1386                 return;
1387         gdouble width_ratio = 0.0;
1388         gdouble height_ratio = 0.0;
1389         width_ratio = (gdouble) ad->preview_w/ad->camfw_video_width;
1390         height_ratio = (gdouble) ad->preview_h/ad->camfw_video_height;
1391
1392         switch (ad->camcorder_rotate) {
1393         case CAMERA_ROTATION_NONE:
1394                 /*
1395                 *       win: (x,y)  -------->x          video:(x,y) -------->x(width)
1396                 *                -                                      -
1397                 *                -                                      -
1398                 *                -                                      -
1399                 *                -y                             y(height)
1400                 */
1401                 result->x = width_ratio * src.x + ad->preview_offset_x;
1402                 result->y = height_ratio * src.y + ad->preview_offset_y;
1403                 /*notes:here,result->w: horizontal;result->h:vertical*/
1404                 result->w = src.w * width_ratio;
1405                 result->h = src.h * height_ratio;
1406                 break;
1407         case CAMERA_ROTATION_90:
1408                 /* win: (x,y)  -------->x   video:(x,y) (height)y<-----
1409                 *                -                                              -
1410                 *                -                                              -
1411                 *                -                                              -
1412                 *                -y                                     x(width)
1413                 */
1414                 result->x = ad->win_height - (height_ratio * src.y + ad->preview_offset_y + height_ratio * src.w);
1415                 result->y = width_ratio * src.x + ad->preview_offset_x;
1416                 /*notes:here,result->w: horizontal;result->h:vertical*/
1417                 result->w = src.w * height_ratio;
1418                 result->h = src.h * width_ratio;
1419                 break;
1420         case CAMERA_ROTATION_180:
1421                 /* win: (x,y)  -------->x   video:(x,y)                 y(height)
1422                 *                -                                              -
1423                 *                -                                              -
1424                 *                -                                              -
1425                 *                -y                     (width)x<------
1426                 */
1427                 result->x = ad->win_width - (width_ratio * src.x + ad->preview_offset_x + width_ratio *src.w);
1428                 result->y = ad->win_height - (height_ratio * src.y + ad->preview_offset_y + height_ratio *src.h);
1429                 /*notes:here,result->w: horizontal;result->h:vertical*/
1430                 result->w = src.w * width_ratio;
1431                 result->h = src.h * height_ratio;
1432                 break;
1433         case CAMERA_ROTATION_270:
1434                 /* win: (x,y)  -------->x   video:(x,y) x(width)
1435                 *                -                               -
1436                 *                -                               -
1437                 *                -                               -
1438                 *                -y                      -------------y(height)
1439                 */
1440                 result->x = height_ratio * src.y + ad->preview_offset_y;
1441                 result->y = ad->win_width - (width_ratio * src.x + ad->preview_offset_x + width_ratio * src.w);
1442                 /*notes:here,result->w: horizontal;result->h:vertical*/
1443                 result->w = src.w * height_ratio;
1444                 result->h = src.h * width_ratio;
1445                 break;
1446         default:
1447                 DEBUG_TRACE("REACHE UN-REACHED CODES");
1448         }
1449
1450 }
1451