1. Changed license year
[apps/home/mobileprint.git] / mobileprint / previewgen / lib / preview_util.c
1 /*
2 *  Mobileprint
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://floralicense.org/license/
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 #include <string.h>
21
22 #include <Evas.h>
23 #include <Ecore_Evas.h>
24 //#include <Evas_Engine_Buffer.h>
25
26 #include "pgen_debug.h"
27
28 #include "preview_util.h"
29
30
31 int get_image_resolution(const char *path, int *resx, int *resy)
32 {
33         PGEN_TRACE_BEGIN;
34         int width = 0;
35         int height = 0;
36
37         Evas *canvas;
38         Ecore_Evas *ee;
39
40         Evas_Object *img;
41
42         PGEN_RETV_IF(path == NULL || resx == NULL || resy == NULL, -1 , "Invalid argument");
43
44         ee = ecore_evas_buffer_new(1, 1);
45         PGEN_RETV_IF(ee == NULL, -1 , "ee is NULL");
46
47         canvas = ecore_evas_get(ee);
48         img = evas_object_image_add(canvas);
49
50         evas_object_image_file_set(img, NULL, NULL);
51         evas_object_image_load_orientation_set(img, EINA_TRUE);
52         evas_object_image_load_scale_down_set(img, 0);
53
54         evas_object_image_file_set(img, path, NULL);
55         Evas_Load_Error error = evas_object_image_load_error_get(img);
56         if (error != EVAS_LOAD_ERROR_NONE) {
57                 PGEN_DEBUG("Decoding Error(%d) : %s", error, path);
58                 evas_object_del(img);
59                 ecore_evas_free(ee);
60                 return -1;
61         }
62
63         evas_object_image_size_get(img, &width, &height);
64
65         evas_object_image_file_set(img, NULL, NULL);
66         evas_object_del(img);
67
68         ecore_evas_free(ee);
69
70         *resx = width;
71         *resy = height;
72
73         PGEN_DEBUG("width & height is [%d, %d]",  width, height);
74
75         PGEN_TRACE_END;
76         return 0;
77 }
78
79
80 /**
81  * @brief       Rotate image on 90 degrees
82  * @param[in]   img     image to rotate
83  */
84 int rotate90_image(Evas_Object *img)
85 {
86         int *old_data;
87         int *new_data;
88         int *outp; /* current writing pointer */
89         int w;
90         int h;
91         int data_size;
92         int off; /* offset from output beginning */
93
94         int ix;
95         int iy;
96
97         PGEN_TRACE_BEGIN;
98         PGEN_RETV_IF(img == NULL, -1, "Invalid argument");
99
100         /* allocate temporary memory */
101         evas_object_image_size_get(img, &w, &h);
102         PGEN_RETV_IF(w == 0 || h == 0 , -1, "Failed to allocate memory");
103         PGEN_DEBUG("w = %d h = %d", w, h);
104
105         data_size = w * h;
106         old_data = evas_object_image_data_get(img, EINA_FALSE);
107         PGEN_RETV_IF(old_data == NULL, -1 , "Failed to get image data");
108
109         new_data = malloc(sizeof(int) * (data_size + 1));
110         PGEN_RETV_IF(new_data == NULL, -1 , "Failed to malloc");
111
112         /* write rotated image data */
113         for (iy = 0; iy < h; ++iy)
114                 for (ix = 0; ix < w; ++ix) {
115                         off = h * (ix + 1) - 1 - iy;
116                         if (off < 0 || off >= data_size) {
117                                 PGEN_DEBUG("ERROR: off = %d, lim: %d", off, data_size);
118                         }
119                         outp = new_data + off;
120                         *outp = old_data[ix + w * iy];
121                 }
122
123         /* store rotated image */
124         evas_object_image_size_set(img, h, w);
125         evas_object_image_data_copy_set(img, new_data);
126         PGEN_IF_FREE_MEM(new_data);
127
128         PGEN_TRACE_END;
129         return 0;
130 }
131