added some more macros, showcase as an image viewer of h.r. giger pics
[ego.git] / src / lib / image.c
blob8a8eff0a42c50cf8d238fd54532ae7c5f6e932ca
1 #include "class.h"
2 #include "object.h"
3 #include "image.h"
4 #include "macro.h"
5 #include <Evas.h>
7 /*
8 * image function methods
9 */
11 // convenience function, which fits image to its size
12 static int
13 limage_fn_fit (lua_State * L)
15 /* 1: image
17 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
18 // get geometry
19 Evas_Coord w, h;
20 evas_object_geometry_get (obj->data, NULL, NULL, &w, &h);
21 // resize
22 evas_object_image_fill_set (obj->data, 0, 0, w, h);
23 return 0;
26 static int
27 limage_fn_reload (lua_State * L)
29 /* 1: image
31 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
32 evas_object_image_reload (obj->data);
33 return 0;
36 static int
37 limage_fn_save (lua_State * L)
39 /* 1: image
40 * 2: table {file,key,flags}
42 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
43 lua_rawgeti (L, 2, 1);
44 lua_rawgeti (L, 2, 2);
45 lua_rawgeti (L, 2, 3);
46 evas_object_image_save (obj->data,
47 luaL_checkstring (L, -3),
48 luaL_checkstring (L, -2), luaL_checkstring (L, -1));
49 return 0;
53 * image get methods
56 static int
57 limage_get_file (lua_State * L)
59 /* 1: image
61 const char *file, *key;
62 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
63 evas_object_image_file_get (obj->data, &file, &key);
65 lua_pushstring (L, key);
66 return 1;
69 static int
70 limage_get_border (lua_State * L)
72 /* 1: image
74 int l, r, t, b;
75 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
76 evas_object_image_border_get (obj->data, &l, &r, &t, &b);
78 lua_newtable (L);
79 lua_pushnumber (L, l);
80 lua_rawseti (L, -2, 1);
81 lua_pushnumber (L, r);
82 lua_rawseti (L, -2, 2);
83 lua_pushnumber (L, t);
84 lua_rawseti (L, -2, 3);
85 lua_pushnumber (L, b);
86 lua_rawseti (L, -2, 4);
87 return 1;
90 static int
91 limage_get_border_center_fill (lua_State * L)
93 /* 1: image
95 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
96 lua_pushboolean (L,
97 (int) evas_object_image_border_center_fill_get (obj->
98 data));
99 return 1;
102 static int
103 limage_get_fill (lua_State * L)
105 /* 1: image
107 int x, y, w, h;
108 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
109 evas_object_image_fill_get (obj->data, &x, &y, &w, &h);
110 lua_newtable (L);
111 lua_pushnumber (L, x);
112 lua_rawseti (L, -2, 1);
113 lua_pushnumber (L, y);
114 lua_rawseti (L, -2, 2);
115 lua_pushnumber (L, w);
116 lua_rawseti (L, -2, 3);
117 lua_pushnumber (L, h);
118 lua_rawseti (L, -2, 4);
119 return 1;
122 static int
123 limage_get_fill_spread (lua_State * L)
125 /* 1: image
127 int x, y, w, h;
128 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
129 lua_pushnumber (L,
130 evas_object_image_fill_spread_get (obj->data));
131 return 1;
134 static int
135 limage_get_size (lua_State * L)
137 /* 1: image
139 int w, h;
140 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
141 evas_object_image_size_get (obj->data, &w, &h);
142 lua_newtable (L);
143 lua_pushnumber (L, w);
144 lua_rawseti (L, -2, 1);
145 lua_pushnumber (L, h);
146 lua_rawseti (L, -2, 2);
147 return 1;
150 static int
151 limage_get_stride (lua_State * L)
153 /* 1: image
155 int w, h;
156 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
157 lua_pushnumber (L,
158 evas_object_image_stride_get (obj->data));
159 return 1;
162 static int
163 limage_get_load_error (lua_State * L)
165 /* 1: image
167 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
168 lua_pushnumber (L, evas_object_image_load_error_get (obj->data));
169 return 1;
172 static int
173 limage_get_alpha (lua_State * L)
175 /* 1: image
177 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
178 lua_pushboolean (L, (int) evas_object_image_alpha_get (obj->data));
179 return 1;
182 static int
183 limage_get_smooth_scale (lua_State * L)
185 /* 1: image
187 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
188 lua_pushboolean (L, (int) evas_object_image_smooth_scale_get (obj->data));
189 return 1;
192 static int
193 limage_get_pixels_dirty (lua_State * L)
195 /* 1: image
197 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
198 lua_pushboolean (L, (int) evas_object_image_pixels_dirty_get (obj->data));
199 return 1;
202 static int
203 limage_get_load_dpi (lua_State * L)
205 /* 1: image
207 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
208 lua_pushnumber (L, (int) evas_object_image_load_dpi_get (obj->data));
209 return 1;
212 static int
213 limage_get_load_size (lua_State * L)
215 /* 1: image
217 int w, h;
218 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
219 evas_object_image_load_size_get (obj->data, &w, &h);
220 lua_newtable (L);
221 lua_pushnumber (L, w);
222 lua_rawseti (L, -2, 1);
223 lua_pushnumber (L, h);
224 lua_rawseti (L, -2, 2);
225 return 1;
228 static int
229 limage_get_load_scale_down (lua_State * L)
231 /* 1: image
233 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
234 lua_pushnumber (L,
235 (int) evas_object_image_load_scale_down_get (obj->data));
236 return 1;
239 static int
240 limage_get_colorspace (lua_State * L)
242 /* 1: image
244 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
245 lua_pushboolean (L, (int) evas_object_image_colorspace_get (obj->data));
246 return 1;
249 static int
250 limage_get_data (lua_State * L)
252 /* 1: image
254 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
255 int x, y, w, h;
256 int *pixels = evas_object_image_data_get (obj->data, 0);
257 evas_object_image_size_get (obj->data, &w, &h);
258 lua_newtable (L);
259 for (x = 0; x < w; x++)
261 lua_newtable (L);
262 for (y = 0; y < h; y++)
264 lua_pushnumber (L, pixels[x*w + y]);
265 lua_rawseti (L, -2, y + 1);
267 lua_rawseti (L, -2, x + 1);
269 return 1;
272 static int
273 limage_get_fill_transform (lua_State * L)
275 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
276 Evas_Transform *trans = NULL;
277 evas_object_image_fill_transform_get (obj->data, trans);
278 // FIXME registry
279 lua_pushnil (L);
280 return 1;
284 * image set methods
287 static int
288 limage_set_file (lua_State * L)
290 /* 1: image
291 * 2: image id
293 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
294 int key = luaL_checknumber (L, 2);
295 char buf [256];
296 sprintf (buf, "images/%i", key);
297 lua_getfield (L, LUA_REGISTRYINDEX, "ego_file");
298 char *file = luaL_checkstring (L, -1);
299 evas_object_image_file_set (obj->data, file, buf);
300 return 0;
303 static int
304 limage_set_border (lua_State * L)
306 /* 1: image
307 * 2: tab
309 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
310 lua_rawgeti (L, 2, 1);
311 lua_rawgeti (L, 2, 2);
312 lua_rawgeti (L, 2, 3);
313 lua_rawgeti (L, 2, 4);
314 evas_object_image_border_set (obj->data,
315 (int) luaL_checknumber (L, -4),
316 (int) luaL_checknumber (L, -3),
317 (int) luaL_checknumber (L, -2),
318 (int) luaL_checknumber (L, -1));
319 return 0;
322 static int
323 limage_set_border_center_fill (lua_State * L)
325 /* 1: image
326 * 2: border_center_fill
328 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
329 evas_object_image_border_center_fill_set (obj->data,
330 (int) lua_toboolean (L, 2));
331 return 0;
334 static int
335 limage_set_fill (lua_State * L)
337 /* 1: image
338 * 2: tab
340 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
341 lua_rawgeti (L, 2, 1);
342 lua_rawgeti (L, 2, 2);
343 lua_rawgeti (L, 2, 3);
344 lua_rawgeti (L, 2, 4);
345 evas_object_image_fill_set (obj->data,
346 (int) luaL_checknumber (L, -4),
347 (int) luaL_checknumber (L, -3),
348 (int) luaL_checknumber (L, -2),
349 (int) luaL_checknumber (L, -1));
350 return 0;
353 static int
354 limage_set_fill_spread (lua_State * L)
356 /* 1: image
357 * 2: int tile_mode
359 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
360 evas_object_image_fill_spread_set (obj->data,
361 (int) luaL_checknumber (L, 2));
362 return 0;
365 static int
366 limage_set_size (lua_State * L)
368 /* 1: image
369 * 2: tab
371 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
372 lua_rawgeti (L, 2, 1);
373 lua_rawgeti (L, 2, 2);
374 evas_object_image_size_set (obj->data,
375 (int) luaL_checknumber (L, -2),
376 (int) luaL_checknumber (L, -1));
377 return 0;
380 static int
381 limage_set_alpha (lua_State * L)
383 /* 1: image
384 * 2: alpha
386 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
387 evas_object_image_alpha_set (obj->data, (int) lua_toboolean (L, 2));
388 return 0;
391 static int
392 limage_set_smooth_scale (lua_State * L)
394 /* 1: image
395 * 2: smooth_scale
397 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
398 evas_object_image_smooth_scale_set (obj->data, (int) lua_toboolean (L, 2));
399 return 0;
402 static int
403 limage_set_pixels_dirty (lua_State * L)
405 /* 1: image
406 * 2: pixels_dirty
408 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
409 evas_object_image_pixels_dirty_set (obj->data, (int) lua_toboolean (L, 2));
410 return 0;
413 static int
414 limage_set_load_dpi (lua_State * L)
416 /* 1: image
417 * 2: load_dpi
419 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
420 evas_object_image_load_dpi_set (obj->data, luaL_checknumber (L, 2));
421 return 0;
424 static int
425 limage_set_load_size (lua_State * L)
427 /* 1: image
428 * 2: tab
430 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
431 lua_rawgeti (L, 2, 1);
432 lua_rawgeti (L, 2, 2);
433 evas_object_image_load_size_set (obj->data,
434 (int) luaL_checknumber (L, -2),
435 (int) luaL_checknumber (L, -1));
436 return 0;
439 static int
440 limage_set_load_scale_down (lua_State * L)
442 /* 1: image
443 * 2: load_scale_sown
445 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
446 evas_object_image_load_scale_down_set (obj->data,
447 luaL_checkint (L, 2));
448 return 0;
451 static int
452 limage_set_colorspace (lua_State * L)
454 /* 1: image
455 * 2: colorspace
457 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
458 evas_object_image_colorspace_set (obj->data, (int) lua_toboolean (L, 2));
459 return 0;
462 static int
463 limage_set_data (lua_State * L)
465 /* 1: image
466 * 2: tab
468 luaobj_Object *obj = (luaobj_Object *) lua_touserdata (L, 1);
469 int *data;
470 // FIXME
471 evas_object_image_data_copy_set (obj->data, data);
472 return 0;
475 SET_OBJ (limage_set_fill_transform, evas_object_image_fill_transform_set);
478 * image function definitions
481 const luaL_Reg lImage_fn [] = {
482 {"fit", limage_fn_fit},
483 {"reload", limage_fn_reload},
484 {"save", limage_fn_save},
485 {NULL, NULL} // sentinel
489 * image get definitions
492 const struct luaL_Reg lImage_get [] = {
493 {"file", limage_get_file},
494 {"border", limage_get_border},
495 {"border_center_fill", limage_get_border_center_fill},
496 {"fill", limage_get_fill},
497 {"fill_spread", limage_get_fill_spread},
498 {"size", limage_get_size},
499 {"stride", limage_get_stride},
500 {"load_error", limage_get_load_error},
501 {"alpha", limage_get_alpha},
502 {"smooth_scale", limage_get_smooth_scale},
503 {"pixels_dirty", limage_get_pixels_dirty},
504 {"load_dpi", limage_get_load_dpi},
505 {"load_size", limage_get_load_size},
506 {"load_scale_down", limage_get_load_scale_down},
507 {"colorspace", limage_get_colorspace},
508 {"data", limage_get_data},
509 {"fill_transform", limage_get_fill_transform},
510 {NULL, NULL} // sentinel
514 * image set definitions
517 const struct luaL_Reg lImage_set[] = {
518 {"file", limage_set_file},
519 {"border", limage_set_border},
520 {"border_center_fill", limage_set_border_center_fill},
521 {"fill", limage_set_fill},
522 {"fill_spread", limage_set_fill_spread},
523 {"fill_transform", limage_set_fill_transform},
524 {"size", limage_set_size},
525 {"alpha", limage_set_alpha},
526 {"smooth_scale", limage_set_smooth_scale},
527 {"pixels_dirty", limage_set_pixels_dirty},
528 {"load_dpi", limage_set_load_dpi},
529 {"load_size", limage_set_load_size},
530 {"load_scale_down", limage_set_load_scale_down},
531 {"colorspace", limage_set_colorspace},
532 {"data", limage_set_data},
533 {"data_copy", limage_set_data},
534 {NULL, NULL} // sentinel
538 * image definitions
541 const luaL_Reg lImage_nil [] = {
542 {NULL, NULL} // sentinel
545 const luaobj_Reg mImage = {
546 lImage_nil,
547 lImage_get,
548 lImage_set,
549 lImage_fn
552 const luaobj_Reg *cImage[] = {
553 &mClass,
554 &mObject,
555 &mImage,
556 NULL // sentinel