Upstream merge
[framework/uifw/evas.git] / src / examples / evas-images3.c
1 /**
2  * Simple Evas example illustrating some image objects functions
3  *
4  * You'll need at least one engine built for it (excluding the buffer
5  * one) and the png image loader/saver also built. See stdout/stderr
6  * for output.
7  *
8  * @verbatim
9  * gcc -o evas-images2 evas-images2.c `pkg-config --libs --cflags evas ecore ecore-evas`
10  * @endverbatim
11  */
12
13 #ifdef HAVE_CONFIG_H
14
15 #include "config.h"
16 #else
17
18 #define PACKAGE_EXAMPLES_DIR "."
19 #define __UNUSED__
20
21 #endif
22
23 #include <Ecore.h>
24 #include <Ecore_Evas.h>
25 #include <stdio.h>
26 #include <errno.h>
27
28 #define WIDTH  (320)
29 #define HEIGHT (240)
30
31 static const char *img_path = PACKAGE_EXAMPLES_DIR "/enlightenment.png";
32 static const char *commands = \
33   "commands are:\n"
34   "\tw - write new pixel data to image\n"
35   "\ti - print image info\n"
36   "\ta - save noise image to disk (/tmp dir)\n"
37   "\th - print help\n";
38
39 const char *file_path = "/tmp/evas-images2-example.png";
40 const char *quality_str = "quality=100";
41
42 struct test_data
43 {
44    Ecore_Evas  *ee;
45    Evas        *evas;
46    Evas_Object *bg;
47    Evas_Object *logo, *logo1;
48 };
49
50 static struct test_data d = {0};
51
52 static void
53 _on_destroy(Ecore_Evas *ee __UNUSED__)
54 {
55    ecore_main_loop_quit();
56 }
57
58 /* here just to keep our example's window size and background image's
59  * size in synchrony */
60 static void
61 _canvas_resize_cb(Ecore_Evas *ee)
62 {
63    int w, h;
64
65    ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
66    evas_object_resize(d.bg, w, h);
67    evas_object_resize(d.logo, w / 2, h);
68    evas_object_move(d.logo1, w / 2, 0);
69    evas_object_resize(d.logo1, w / 2, h);
70 }
71
72 static void
73 _on_keydown(void        *data __UNUSED__,
74             Evas        *evas __UNUSED__,
75             Evas_Object *o __UNUSED__,
76             void        *einfo)
77 {
78    Evas_Event_Key_Down *ev = einfo;
79
80    if (strcmp(ev->keyname, "h") == 0) /* print help */
81      {
82         fprintf(stdout, commands);
83         return;
84      }
85
86    if (strcmp(ev->keyname, "i") == 0) /* change proxy's source */
87      {
88         int stride = evas_object_image_stride_get(d.logo);
89         int w, h;
90
91         evas_object_image_size_get(d.logo, &w, &h);
92
93         printf("image size: %dx%d; stride: %d\n", w, h, stride);
94
95         return;
96      }
97
98    if (strcmp(ev->keyname, "w") == 0) /* save noise image to disk */
99      {
100         int i;
101         char *pixels = evas_object_image_data_get(d.logo, EINA_FALSE);
102         char *bufpixels;
103         int w, h;
104         int stride;
105         Eina_Bool equal = EINA_TRUE;
106
107         evas_object_image_size_get(d.logo, &w, &h);
108         stride = evas_object_image_stride_get(d.logo);
109
110         bufpixels = malloc(sizeof(char) * stride * h);
111         memcpy(bufpixels, pixels, sizeof(char) * stride * h);
112
113         pixels = evas_object_image_data_get(d.logo, EINA_TRUE);
114
115         for (i = 0; i < (stride * h); i++)
116           {
117              if (bufpixels[i] != pixels[i])
118                {
119                   equal = EINA_FALSE;
120                   break;
121                }
122           }
123
124         free(bufpixels);
125
126         if (!equal)
127           printf("write pixels different from readonly pixels.\n");
128
129         for (i = ((stride * h) / 4) ; i < ((stride * h) / 2) ; i++)
130           {
131              pixels[i] = 0;
132           }
133
134         // evas_object_image_data_set(d.logo, pixels);
135         evas_object_image_data_update_add(d.logo, 0, 0, w, h);
136         return;
137      }
138 }
139
140 int
141 main(void)
142 {
143    // unsigned int i;
144    // unsigned int pixels[(WIDTH / 4) * (HEIGHT / 4)];
145
146    if (!ecore_evas_init())
147      return EXIT_FAILURE;
148
149    /* this will give you a window with an Evas canvas under the first
150     * engine available */
151    d.ee = ecore_evas_new(NULL, 10, 10, WIDTH, HEIGHT, NULL);
152    if (!d.ee)
153      goto error;
154
155    ecore_evas_callback_destroy_set(d.ee, _on_destroy);
156    ecore_evas_callback_resize_set(d.ee, _canvas_resize_cb);
157    ecore_evas_show(d.ee);
158
159    /* the canvas pointer, de facto */
160    d.evas = ecore_evas_get(d.ee);
161
162    d.bg = evas_object_rectangle_add(d.evas);
163    evas_object_color_set(d.bg, 255, 255, 255, 255); /* white bg */
164    evas_object_move(d.bg, 0, 0); /* at canvas' origin */
165    evas_object_resize(d.bg, WIDTH, HEIGHT); /* covers full canvas */
166    evas_object_show(d.bg);
167
168    evas_object_focus_set(d.bg, EINA_TRUE);
169    evas_object_event_callback_add(
170      d.bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL);
171
172    d.logo = evas_object_image_filled_add(d.evas);
173
174    evas_object_image_file_set(d.logo, img_path, NULL);
175    evas_object_resize(d.logo, WIDTH / 2, HEIGHT);
176    evas_object_show(d.logo);
177
178    d.logo1 = evas_object_image_filled_add(d.evas);
179    evas_object_image_file_set(d.logo1, img_path, NULL);
180    evas_object_resize(d.logo1, WIDTH / 2, HEIGHT);
181    evas_object_move(d.logo1, WIDTH / 2, 0);
182    evas_object_show(d.logo1);
183
184    fprintf(stdout, commands);
185    ecore_main_loop_begin();
186
187    ecore_evas_free(d.ee);
188    ecore_evas_shutdown();
189    return 0;
190
191 error:
192    fprintf(stderr, "you got to have at least one evas engine built and linked"
193                    " up to ecore-evas for this example to run properly.\n");
194    ecore_evas_shutdown();
195    return -1;
196 }