Tizen 2.0 Release
[apps/home/mobileprint.git] / mobileprint / previewgen / lib / image_scaler.c
1 /*
2 *  Mobileprint
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.0 (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 <Ecore.h>
21 #include <Evas.h>
22
23 #include "evas_render.h"
24 #include "paper_size.h"
25 #include "pgen_debug.h"
26 #include "preview_util.h"
27
28 #include "image_scaler.h"
29
30 #define DOWNSCALE_LEVEL 2
31
32 int downscale_image(const char *fname, const char *outfname_ppm,
33                                         const struct size_px *available_size)
34 {
35         Evas *canvas;
36         Evas_Object *img;
37         int rw, rh;
38         struct size_pts rsize;
39         struct size_px pic_size = {400, 400};
40         struct size_px size_limits;
41
42         PGEN_TRACE_BEGIN;
43         PTS_RETV_IF(fname == NULL || outfname_ppm == NULL || available_size == NULL, -1, "Invalid argument");
44
45         PGEN_DEBUG("available_size(%d,%d)",available_size->x, available_size->y);
46
47         size_limits.x = available_size->x / DOWNSCALE_LEVEL;
48         size_limits.y = available_size->y / DOWNSCALE_LEVEL;
49
50         if (get_image_resolution(fname, &rw, &rh) < 0) {
51                 PGEN_TRACE_END;
52                 return -1;
53         }
54
55         PTS_RETV_IF(rw <= 0 || rh <= 0, -1, "Failed to get resolution");
56
57         rsize.x = rw;
58         rsize.y = rh;
59         PGEN_DEBUG("size_limits before: (%d, %d)",
60                            size_limits.x, size_limits.y);
61         if (rsize.x > rsize.y) {
62                 if (size_limits.x < size_limits.y) {
63                         switch_size_px_coords(&size_limits);
64                 }
65         } else if (size_limits.x > size_limits.y) {
66                 switch_size_px_coords(&size_limits);
67         }
68         PGEN_DEBUG("size_limits after: (%d, %d)",
69                            size_limits.x, size_limits.y);
70         pts_size2px(&rsize, &size_limits, &pic_size);
71
72         PGEN_DEBUG("new canvas");
73         PGEN_DEBUG("canvas init, (%d; %d)", pic_size.x, pic_size.y);
74
75         canvas = create_canvas(&pic_size);
76         PTS_RETV_IF(canvas == NULL, -1, "canvas is NULL");
77
78         /*PGEN_DEBUG("new image");*/
79         img = evas_object_image_filled_add(canvas);
80         if (NULL == img) {
81                 destroy_canvas(canvas);
82                 PGEN_TRACE_END;
83                 return -1;
84         }
85         /*PGEN_DEBUG("setting image file %s", fname);*/
86         evas_object_image_file_set(img, fname, NULL);
87
88         evas_object_resize(img, pic_size.x, pic_size.y);
89         evas_object_move(img, 0, 0);
90         evas_object_show(img);
91
92         /*PGEN_DEBUG("canvas draw");*/
93         draw_scene(canvas);
94         /*PGEN_DEBUG("saving canvas in %s", outfname_ppm);*/
95         save_scene(canvas, outfname_ppm);
96         destroy_canvas(canvas);
97
98         PGEN_TRACE_END;
99         return 0;
100 }
101