summaryrefslogtreecommitdiff
path: root/apps/plugins/lua
diff options
context:
space:
mode:
authorWilliam Wilgus <me.theuser@yahoo.com>2018-08-24 00:44:32 +0200
committerWilliam Wilgus <me.theuser@yahoo.com>2018-09-14 01:00:35 +0200
commit733c20d5d3d696a60a7fd7098ca45cb545e78043 (patch)
treef43cc3536114ffbd3811f1491a412b53f57eb259 /apps/plugins/lua
parentbe801c61bbe95d75e4285da1982017da7fa1736b (diff)
downloadrockbox-733c20d5d3d696a60a7fd7098ca45cb545e78043.tar.gz
rockbox-733c20d5d3d696a60a7fd7098ca45cb545e78043.zip
lua move RLIMAGE to own file
Change-Id: Icd10e4c348deec7729d4a6e2bf1152e1dfc70243
Diffstat (limited to 'apps/plugins/lua')
-rw-r--r--apps/plugins/lua/SOURCES1
-rw-r--r--apps/plugins/lua/rocklib.c1416
-rw-r--r--apps/plugins/lua/rocklib.h14
-rw-r--r--apps/plugins/lua/rocklib_img.c1452
-rw-r--r--apps/plugins/lua/rocklib_img.h31
5 files changed, 1503 insertions, 1411 deletions
diff --git a/apps/plugins/lua/SOURCES b/apps/plugins/lua/SOURCES
index baebfabe65..1c4dcc4a36 100644
--- a/apps/plugins/lua/SOURCES
+++ b/apps/plugins/lua/SOURCES
@@ -28,6 +28,7 @@ lvm.c
28lzio.c 28lzio.c
29rockaux.c 29rockaux.c
30rocklib.c 30rocklib.c
31rocklib_img.c
31tlsf_helper.c 32tlsf_helper.c
32fscanf.c 33fscanf.c
33gmtime.c 34gmtime.c
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index ba8a576e8c..94de2b560d 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -9,7 +9,6 @@
9 * 9 *
10 * Copyright (C) 2008 Dan Everton (safetydan) 10 * Copyright (C) 2008 Dan Everton (safetydan)
11 * Copyright (C) 2009 Maurus Cuelenaere 11 * Copyright (C) 2009 Maurus Cuelenaere
12 * Copyright (C) 2017 William Wilgus
13 * 12 *
14 * This program is free software; you can redistribute it and/or 13 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License 14 * modify it under the terms of the GNU General Public License
@@ -27,6 +26,7 @@
27#include "lua.h" 26#include "lua.h"
28 27
29#include "lauxlib.h" 28#include "lauxlib.h"
29#include "rocklib_img.h"
30#include "rocklib.h" 30#include "rocklib.h"
31#include "lib/helper.h" 31#include "lib/helper.h"
32#include "lib/pluginlib_actions.h" 32#include "lib/pluginlib_actions.h"
@@ -46,1226 +46,6 @@
46 * prevent namespace collisions and adding duplicate wrappers. 46 * prevent namespace collisions and adding duplicate wrappers.
47 */ 47 */
48 48
49
50/*
51 * -----------------------------------------------------------------------------
52 *
53 * Rockbox Lua image wrapper
54 *
55 * Some devices(1-bit / 2-bit displays) have packed bit formats that
56 * need to be unpacked in order to work on them at a pixel level.
57 *
58 * The internal formats of these devices do not follow the same paradigm
59 * for image sizes either; We still display the actual width and height to
60 * the user but store stride based on the native values
61 *
62 * Conversion between native addressing and per pixel addressing
63 * incurs extra overhead but it is much faster to do it
64 * on the 'C' side rather than in lua.
65 *
66 * -----------------------------------------------------------------------------
67 */
68
69#ifdef HAVE_LCD_BITMAP
70#define RLI_EXTENDED
71#endif
72
73#define ROCKLUA_IMAGE "rb.image"
74#define ERR_IDX_RANGE "index out of range"
75#define ERR_DATA_OVF "data overflow"
76
77/* mark for RLI to LUA Interface functions (luaState *L) is the only argument */
78#define RLI_LUA static int
79
80#ifndef ABS
81#define ABS(a)(((a) < 0) ? - (a) :(a))
82#endif
83
84#ifndef LCD_BLACK
85#define LCD_BLACK 0x0
86#endif
87
88struct rocklua_image
89{
90 int width;
91 int height;
92 int stride;
93 size_t elems;
94 fb_data *data;
95 fb_data dummy[1];
96};
97
98/* holds iterator data for rlimages */
99struct rli_iter_d
100{
101 struct rocklua_image *img;
102 fb_data *elem;
103 int x , y;
104 int x1, y1;
105 int x2, y2;
106 int dx, dy;
107};
108
109/* __tostring information enums */
110enum rli_info {RLI_INFO_ALL = 0, RLI_INFO_TYPE, RLI_INFO_WIDTH,
111 RLI_INFO_HEIGHT, RLI_INFO_ELEMS, RLI_INFO_BYTES,
112 RLI_INFO_DEPTH, RLI_INFO_FORMAT, RLI_INFO_ADDRESS};
113
114#ifdef HAVE_LCD_COLOR
115
116static inline fb_data invert_color(fb_data rgb)
117{
118 uint8_t r = 0xFFU - FB_UNPACK_RED(rgb);
119 uint8_t g = 0xFFU - FB_UNPACK_RED(rgb);
120 uint8_t b = 0xFFU - FB_UNPACK_RED(rgb);
121
122 return FB_RGBPACK(r, g, b);
123}
124#else /* !HAVE_LCD_COLOR */
125
126#define invert_color(c) (~c)
127
128#endif /* HAVE_LCD_COLOR */
129
130
131#if (LCD_DEPTH > 2) /* no native to pixel mapping needed */
132
133#define pixel_to_fb(x, y, o, n) {(void) x; (void) y; do { } while (0);}
134#define pixel_to_native(x, y, xn, yn) {*xn = x; *yn = y;}
135#define init_pixelmask(x, y, m, p) do { } while (0)
136
137
138#else /* some devices need x | y coords shifted to match native format */
139
140static fb_data x_shift = FB_SCALARPACK(0);
141static fb_data y_shift = FB_SCALARPACK(0);
142static fb_data xy_mask = FB_SCALARPACK(0);
143static const fb_data *pixelmask = NULL;
144
145/* conversion between packed native formats and individual pixel addressing */
146static inline void init_pixelmask(fb_data *x_shift, fb_data *y_shift,
147 fb_data *xy_mask, const fb_data **pixelmask)
148{
149
150#if(LCD_PIXELFORMAT == VERTICAL_PACKING) && LCD_DEPTH == 1
151 static const fb_data pixelmask_v1[8] =
152 {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
153 *pixelmask = pixelmask_v1;
154
155 (void) x_shift;
156 *y_shift = 3U;
157 *xy_mask = ((1 << (*y_shift)) - 1);
158#elif(LCD_PIXELFORMAT == VERTICAL_PACKING) && LCD_DEPTH == 2
159 static const fb_data pixelmask_v2[4] = {0x03, 0x0C, 0x30, 0xC0};
160 *pixelmask = pixelmask_v2;
161
162 (void) x_shift;
163 *y_shift = 2U;
164 *xy_mask = ((1 << (*y_shift)) - 1);
165#elif(LCD_PIXELFORMAT == VERTICAL_INTERLEAVED) && LCD_DEPTH == 2
166 static const fb_data pixelmask_vi2[8] =
167 {0x0101, 0x0202, 0x0404, 0x0808, 0x1010, 0x2020, 0x4040, 0x8080};
168 *pixelmask = pixelmask_vi2;
169
170 (void) x_shift;
171 *y_shift = 3U;
172 *xy_mask = ((1 << (*y_shift)) - 1);
173#elif(LCD_PIXELFORMAT == HORIZONTAL_PACKING) && LCD_DEPTH == 2
174 /* MSB on left */
175 static const fb_data pixelmask_h2[4] = {0x03, 0x0C, 0x30, 0xC0};
176 *pixelmask = pixelmask_h2;
177
178 (void) y_shift;
179 *x_shift = 2U;
180 *xy_mask = ((1 << (*x_shift)) - 1);
181#else
182 #warning Unknown Pixel Format
183#endif /* LCD_PIXELFORMAT */
184
185} /* init_pixelmask */
186
187static inline void pixel_to_native(int x, int y, int *x_native, int *y_native)
188{
189 *x_native = ((x - 1) >> x_shift) + 1;
190 *y_native = ((y - 1) >> y_shift) + 1;
191} /* pixel_to_native */
192
193static inline fb_data set_masked_pixel(fb_data old,
194 fb_data new,
195 fb_data mask,
196 int bit_n)
197{
198 /*equivalent of: (old & (~mask)) | ((new << bit_n) & mask);*/
199 return old ^ ((old ^ (new << bit_n)) & mask);
200
201} /* set_masked_pixel */
202
203static inline fb_data get_masked_pixel(fb_data val, fb_data mask, int bit_n)
204{
205 val = val & mask;
206 return val >> bit_n;
207} /* get_masked_pixel */
208
209/* conversion between packed native formats and individual pixel addressing */
210static void pixel_to_fb(int x, int y, fb_data *oldv, fb_data *newv)
211{
212 fb_data mask;
213 int bit_n;
214
215#if(LCD_PIXELFORMAT == VERTICAL_INTERLEAVED) && LCD_DEPTH == 2
216 (void) x;
217 const uint16_t greymap_vi2[4] = {0x0000, 0x0001, 0x0100, 0x0101};
218
219 bit_n = (y - 1) & xy_mask;
220 mask = pixelmask[bit_n];
221
222 *newv = greymap_vi2[*newv &(0x3)]; /* [0-3] => greymap */
223 *newv = set_masked_pixel(*oldv, *newv, mask, bit_n);
224
225 *oldv = get_masked_pixel(*oldv, mask, bit_n);
226
227 if((*oldv) > 1) /* greymap => [0-3] */
228 *oldv = ((*oldv) & 0x1U) + 2U; /* 2, 3 */
229 else
230 *oldv &= 1U; /* 0, 1 */
231
232#elif(LCD_DEPTH <= 2)
233 if(y_shift)
234 bit_n = (y - 1) & xy_mask;
235 else if(x_shift)
236 bit_n = xy_mask - ((x - 1) & xy_mask); /*MSB on left*/
237
238 if(y_shift || x_shift)
239 {
240 mask = pixelmask[bit_n];
241 bit_n *= LCD_DEPTH;
242
243 *newv = set_masked_pixel(*oldv, *newv, mask, bit_n);
244
245 *oldv = get_masked_pixel(*oldv, mask, bit_n);
246 }
247#else
248 #error Unknown Pixel Format
249#endif /* LCD_PIXELFORMAT == VERTICAL_INTERLEAVED && LCD_DEPTH == 2 */
250} /* pixel_to_fb */
251
252#endif /* (LCD_DEPTH > 2) no native to pixel mapping needed */
253
254/* Internal worker functions for image data array *****************************/
255
256static inline void swap_int(bool swap, int *v1, int *v2)
257{
258 if(swap)
259 {
260 int val = *v1;
261 *v1 = *v2;
262 *v2 = val;
263 }
264} /* swap_int */
265
266/* Throws error if x or y are out of bounds notifies user which narg indice
267 the out of bound variable originated */
268static void bounds_check_xy(lua_State *L, struct rocklua_image *img,
269 int nargx, int x, int nargy, int y)
270{
271 luaL_argcheck(L, x <= img->width && x > 0, nargx, ERR_IDX_RANGE);
272 luaL_argcheck(L, y <= img->height && y > 0, nargy, ERR_IDX_RANGE);
273} /* bounds_check_xy */
274
275static struct rocklua_image* rli_checktype(lua_State *L, int arg)
276{
277 void *ud = luaL_checkudata(L, arg, ROCKLUA_IMAGE);
278
279 luaL_argcheck(L, ud != NULL, arg, "'" ROCKLUA_IMAGE "' expected");
280
281 return (struct rocklua_image*) ud;
282} /* rli_checktype */
283
284static struct rocklua_image * alloc_rlimage(lua_State *L, bool alloc_data,
285 int width, int height)
286{
287 /* rliimage is pushed on the stack it is up to you to pop it */
288 struct rocklua_image *img;
289
290 const size_t sz_header = sizeof(struct rocklua_image);
291 size_t sz_data = 0;
292 size_t n_elems;
293
294 int w_native;
295 int h_native;
296
297 pixel_to_native(width, height, &w_native, &h_native);
298
299 n_elems = (size_t)(w_native * h_native);
300
301 if(alloc_data) /* if this a new image we need space for image data */
302 sz_data = n_elems * sizeof(fb_data);
303
304 /* newuserdata pushes the userdata onto the stack */
305 img = (struct rocklua_image *) lua_newuserdata(L, sz_header + sz_data);
306
307 luaL_getmetatable(L, ROCKLUA_IMAGE);
308 lua_setmetatable(L, -2);
309
310 /* apparent w/h is stored but behind the scenes native w/h is used */
311 img->width = width;
312 img->height = height;
313 img->stride = w_native;
314 img->elems = n_elems;
315
316 return img;
317} /* alloc_rlimage */
318
319static inline void rli_wrap(lua_State *L, fb_data *src, int width, int height)
320{
321 /* rliimage is pushed on the stack it is up to you to pop it */
322 struct rocklua_image *a = alloc_rlimage(L, false, width, height);
323
324 a->data = src;
325} /* rli_wrap */
326
327static inline fb_data* rli_alloc(lua_State *L, int width, int height)
328{
329 /* rliimage is pushed on the stack it is up to you to pop it */
330 struct rocklua_image *a = alloc_rlimage(L, true, width, height);
331
332 a->data = &a->dummy[0]; /* ref to beginning of alloc'd img data */
333
334 return a->data;
335} /* rli_alloc */
336
337static inline fb_data data_setget(fb_data *elem, int x, int y, fb_data *val)
338{
339 fb_data old_val = FB_SCALARPACK(0);
340 fb_data new_val;
341
342 if(elem)
343 {
344 old_val = *elem;
345
346 if(val)
347 {
348 new_val = *val;
349 pixel_to_fb(x, y, &old_val, &new_val);
350 *elem = new_val;
351 }
352 else
353 pixel_to_fb(x, y, &old_val, &new_val);
354 }
355
356 return old_val;
357} /* data_setget */
358
359static inline fb_data data_set(fb_data *elem, int x, int y, fb_data *new_val)
360{
361 /* get and set share the same underlying function */
362 return data_setget(elem, x, y, new_val);
363} /* data_set */
364
365static inline fb_data data_get(fb_data *elem, int x, int y)
366{
367 /* get and set share the same underlying function */
368 return data_setget(elem, x, y, NULL);
369} /* data_get */
370
371static fb_data* rli_get_element(struct rocklua_image* img, int x, int y)
372{
373 int stride = img->stride;
374 size_t elements = img->elems;
375 fb_data *data = img->data;
376
377 pixel_to_native(x, y, &x, &y);
378
379 /* row major address */
380 size_t data_address = (stride * (y - 1)) + (x - 1);
381
382 /* x needs bound between 0 and stride otherwise overflow to prev/next y */
383 if(x <= 0 || x > stride || data_address >= elements)
384 return NULL; /* data overflow */
385
386 return &data[data_address]; /* return element address */
387} /* rli_get_element */
388
389/* Lua to C Interface for pixel set and get functions */
390static int rli_setget(lua_State *L, bool is_get)
391{
392 /*(set) (dst*, [x1, y1, clr, clip]) */
393 /*(get) (dst*, [x1, y1, clip]) */
394 struct rocklua_image *a = rli_checktype(L, 1);
395 int x = luaL_checkint(L, 2);
396 int y = luaL_checkint(L, 3);
397
398 fb_data clr = FB_SCALARPACK(0); /* Arg 4 is color if set element */
399 fb_data *p_clr = &clr;
400
401 int clip_narg;
402
403 if(is_get) /* get element */
404 {
405 p_clr = NULL;
406 clip_narg = 4;
407 }
408 else /* set element */
409 {
410 clr = FB_SCALARPACK((unsigned) luaL_checknumber(L, 4));
411 clip_narg = 5;
412 }
413
414 fb_data *element = rli_get_element(a, x, y);
415
416 if(!element)
417 {
418 if(!luaL_optboolean(L, clip_narg, false)) /* Error if !clip */
419 bounds_check_xy(L, a, 2, x, 3, y);
420
421 lua_pushnil(L);
422 return 1;
423 }
424
425 lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(data_setget(element, x, y, p_clr)));
426
427 /* returns old value */
428 return 1;
429} /* rli_setget */
430
431#ifdef RLI_EXTENDED
432static bool init_rli_iter(struct rli_iter_d *d,
433 struct rocklua_image *img,
434 int x1, int y1,
435 int x2, int y2,
436 int dx, int dy,
437 bool swx, bool swy)
438{
439
440 swap_int((swx), &x1, &x2);
441 swap_int((swy), &y1, &y2);
442
443 /* stepping in the correct x direction ? */
444 if((dx > 0 && x1 > x2) || (dx < 0 && x1 < x2))
445 dx = -dx;
446
447 /* stepping in the correct y direction ? */
448 if((dy > 0 && y1 > y2) || (dy < 0 && y1 < y2))
449 dy = -dy;
450
451 d->img = img;
452 d->x = x1;
453 d->y = y1;
454 d->x1 = x1;
455 d->y1 = y1;
456 d->x2 = x2;
457 d->y2 = y2;
458 d->dx = dx;
459 d->dy = dy;
460 d->elem = rli_get_element(img, d->x, d->y);
461
462 return(dx != 0 || dy != 0);
463} /* init_rli_iter */
464
465/* steps to the next point(x, y) by delta x/y, stores pointer to element
466 returns true if x & y haven't reached x2 & y2
467 if limit reached - element set to NULL, deltas set to 0 & false returned
468*/
469static bool next_rli_iter(struct rli_iter_d *d)
470{
471 if((d->dx > 0 && d->x < d->x2) || (d->dx < 0 && d->x > d->x2))
472 d->x += d->dx;
473 else if((d->dy > 0 && d->y < d->y2) || (d->dy < 0 && d->y > d->y2))
474 {
475 d->x = d->x1; /* Reset x*/
476 d->y += d->dy;
477 }
478 else
479 {
480 d->elem = NULL;
481 d->dx = 0;
482 d->dy = 0;
483 return false;
484 }
485
486 d->elem = rli_get_element(d->img, d->x, d->y);
487
488 return true;
489} /* next_rli_iter */
490
491static int d_line(struct rocklua_image *img,
492 int x1, int y1,
493 int x2, int y2,
494 fb_data *clr,
495 bool clip)
496{
497 /* NOTE! clr passed as pointer */
498 /* Bresenham midpoint line algorithm */
499 fb_data *element;
500
501 int r_a = x2 - x1; /* range of x direction called 'a' for now */
502 int r_b = y2 - y1; /* range of y direction called 'b' for now */
503
504 int s_a = 1; /* step of a direction */
505 int s_b = 1; /* step of b direction */
506
507 int d_err;
508 int numpixels;
509
510 int *a1 = &x1; /* pointer to the x var 'a' */
511 int *b1 = &y1; /* pointer to the y var 'b' */
512
513 if(r_a < 0) /* instead of negative range we will switch step instead */
514 {
515 r_a = -r_a;
516 s_a = -s_a;
517 }
518
519 if(r_b < 0) /* instead of negative range we will switch step instead */
520 {
521 r_b = -r_b;
522 s_b = -s_b;
523 }
524
525 x2 += s_a; /* add 1 extra point to make the whole line */
526 y2 += s_b; /* add 1 extra point */
527
528 if(r_b > r_a) /*if rangeY('b') > rangeX('a') swap their identities */
529 {
530 a1 = &y1;
531 b1 = &x1;
532 swap_int((true), &r_a, &r_b);
533 swap_int((true), &s_a, &s_b);
534 }
535
536 d_err = ((r_b << 1) - r_a) >> 1; /* preload err of 1 step (px centered) */
537
538 numpixels = r_a + 1;
539
540 r_a -= r_b; /* pre-subtract 'a' - 'b' */
541
542 for(;numpixels > 0; numpixels--)
543 {
544 element = rli_get_element(img, x1, y1);
545 if(element || clip)
546 data_set(element, x1, y1, clr);
547 else
548 return numpixels + 1; /* Error */
549
550 if(d_err >= 0) /* 0 is our target midpoint(exact point on the line) */
551 {
552 *b1 += s_b; /* whichever axis is in 'b' stepped(-1 or +1) */
553 d_err -= r_a;
554 }
555 else
556 d_err += r_b; /* only add 'b' when d_err < 0 */
557
558 *a1 += s_a; /* whichever axis is in 'a' stepped(-1 or +1) */
559 }
560
561 return 0;
562} /* d_line */
563
564/* ellipse worker function */
565static int d_ellipse_elements(struct rocklua_image * img,
566 int x1, int y1,
567 int x2, int y2,
568 int sx, int sy,
569 fb_data *clr,
570 fb_data *fillclr,
571 bool clip)
572{
573 int ret = 0;
574 fb_data *element1, *element2, *element3, *element4;
575
576 if(fillclr && x1 - sx != x2 + sx)
577 {
578 ret |= d_line(img, x1, y1, x2, y1, fillclr, clip); /* I. II.*/
579 ret |= d_line(img, x1, y2, x2, y2, fillclr, clip); /* III.IV.*/
580 }
581
582 x1 -= sx; /* shift x & y */
583 y1 -= sy;
584 x2 += sx;
585 y2 += sy;
586
587 element1 = rli_get_element(img, x2, y1);
588 element2 = rli_get_element(img, x1, y1);
589 element3 = rli_get_element(img, x1, y2);
590 element4 = rli_get_element(img, x2, y2);
591
592 if(clip || (element1 && element2 && element3 && element4))
593 {
594 data_set(element1, x2, y1, clr); /* I. Quadrant +x +y */
595 data_set(element2, x1, y1, clr); /* II. Quadrant -x +y */
596 data_set(element3, x1, y2, clr); /* III. Quadrant -x -y */
597 data_set(element4, x2, y2, clr); /* IV. Quadrant +x -y */
598 }
599 else
600 ret = 2; /* ERROR */
601
602 return ret;
603} /* d_ellipse_elements */
604
605static int d_ellipse(struct rocklua_image *img,
606 int x1, int y1,
607 int x2, int y2,
608 fb_data *clr,
609 fb_data *fillclr,
610 bool clip)
611{
612 /* NOTE! clr and fillclr passed as pointers */
613 /* Rasterizing algorithm derivative of work by Alois Zing */
614#if LCD_WIDTH > 1024 || LCD_HEIGHT > 1024
615 /* Prevents overflow on large screens */
616 double dx, dy, err, e2;
617#else
618 long dx, dy, err, e2;
619#endif
620
621 int ret = 0;
622
623 int a = ABS(x2 - x1); /* diameter */
624 int b = ABS(y2 - y1); /* diameter */
625
626 if(a == 0 || b == 0 || !clr)
627 return ret; /* not an error but nothing to display */
628
629 int b1 = (b & 1);
630 b = b - (1 - b1);
631
632 int a2 = (a * a);
633 int b2 = (b * b);
634
635 dx = ((1 - a) * b2) >> 1; /* error increment */
636 dy = (b1 * a2) >> 1; /* error increment */
637
638 err = dx + dy + b1 * a2; /* error of 1.step */
639
640 /* if called with swapped points .. exchange them */
641 swap_int((x1 > x2), &x1, &x2);
642 swap_int((y1 > y2), &y1, &y2);
643
644 y1 += (b + 1) >> 1;
645 y2 = y1 - b1;
646
647 do
648 {
649 ret = d_ellipse_elements(img, x1, y1, x2, y2, 0, 0, clr, fillclr, clip);
650
651 e2 = err;
652
653 /* using division because you can't use bit shift on doubles.. */
654 if(e2 <= (dy / 2)) /* target midpoint - y step */
655 {
656 y1++;
657 y2--;
658 dy += a2;
659 err += dy;
660 }
661
662 if(e2 >= (dx / 2) || err > (dy / 2)) /* target midpoint - x step */
663 {
664 x1++;
665 x2--;
666 dx += b2;
667 err += dx;
668 }
669
670 } while(ret == 0 && x1 <= x2);
671
672 while (ret == 0 && y1 - y2 < b) /* early stop of flat ellipse a=1 finish tip */
673 {
674 ret = d_ellipse_elements(img, x1, y1, x2, y2, 1, 0, clr, fillclr, clip);
675
676 y1++;
677 y2--;
678 }
679
680 return ret;
681} /* d_ellipse */
682
683/* Lua to C Interface for line and ellipse */
684static int rli_line_ellipse(lua_State *L, bool is_ellipse)
685{
686 struct rocklua_image *a = rli_checktype(L, 1);
687
688 int x1 = luaL_checkint(L, 2);
689 int y1 = luaL_checkint(L, 3);
690 int x2 = luaL_optint(L, 4, x1);
691 int y2 = luaL_optint(L, 5, y1);
692
693 fb_data clr = FB_SCALARPACK((unsigned) luaL_checknumber(L, 6));
694
695 fb_data fillclr; /* fill color is index 7 if is_ellipse */
696 fb_data *p_fillclr = NULL;
697
698 bool clip;
699 int clip_narg;
700
701 if(is_ellipse)
702 clip_narg = 8;
703 else
704 clip_narg = 7;
705
706 clip = luaL_optboolean(L, clip_narg, false);
707
708 if(!clip)
709 {
710 bounds_check_xy(L, a, 2, x1, 3, y1);
711 bounds_check_xy(L, a, 4, x2, 5, y2);
712 }
713
714 if(is_ellipse)
715 {
716 if(!lua_isnoneornil(L, 7))
717 {
718 fillclr = FB_SCALARPACK((unsigned) luaL_checkint(L, 7));
719 p_fillclr = &fillclr;
720 }
721
722 luaL_argcheck(L, d_ellipse(a, x1, y1, x2, y2, &clr, p_fillclr, clip) == 0,
723 1, ERR_DATA_OVF);
724 }
725 else
726 luaL_argcheck(L, d_line(a, x1, y1, x2, y2, &clr, clip) == 0,
727 1, ERR_DATA_OVF);
728
729 return 0;
730} /* rli_line_ellipse */
731
732/* Pushes lua function from Stack at position narg to top of stack
733 and puts a reference in the global registry for later use */
734static inline int register_luafunc(lua_State *L, int narg_funct)
735{
736 lua_pushvalue(L, narg_funct); /* lua function */
737 int lf_ref = luaL_ref(L, LUA_REGISTRYINDEX);
738 lua_settop(L, 0); /* clear C stack for use by lua function */
739 return lf_ref;
740} /* register_luafunc */
741
742/* User defined pixel manipulations through rli_copy, rli_marshal */
743static int custom_transform(lua_State *L,
744 struct rli_iter_d *ds,
745 struct rli_iter_d *ss,
746 int op,
747 fb_data *color)
748{
749 (void) color;
750
751 fb_data dst;
752 fb_data src;
753
754 unsigned int params = 3;
755 int ret = 0;
756
757 lua_rawgeti(L, LUA_REGISTRYINDEX, op);
758
759 dst = data_get(ds->elem, ds->x, ds->y);
760 lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(dst));
761 lua_pushnumber(L, ds->x);
762 lua_pushnumber(L, ds->y);
763
764 if(ss) /* Allows src to be omitted */
765 {
766 params += 3;
767 src = data_get(ss->elem, ss->x, ss->y);
768 lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(src));
769 lua_pushnumber(L, ss->x);
770 lua_pushnumber(L, ss->y);
771 }
772
773 lua_call(L, params, 2); /* call custom function w/ n-params & 2 ret */
774
775 if(!lua_isnoneornil(L, -2))
776 {
777 ret = 1;
778 dst = FB_SCALARPACK((unsigned) luaL_checknumber(L, -2));
779 data_set(ds->elem, ds->x, ds->y, &dst);
780 }
781
782 if(!lua_isnoneornil(L, -1) && ss)
783 {
784 ret |= 2;
785 src = FB_SCALARPACK((unsigned) luaL_checknumber(L, -1));
786 data_set(ss->elem, ss->x, ss->y, &src);
787 }
788
789 lua_pop(L, 2);
790 return ret; /* 0 signals iterator to stop */
791} /* custom_transform */
792
793/* Pre defined pixel manipulations through rli_copy */
794static int blit_transform(lua_State *L,
795 struct rli_iter_d *ds,
796 struct rli_iter_d *ss,
797 int op,
798 fb_data *color)
799{
800 (void) L;
801 unsigned clr = FB_UNPACK_SCALAR_LCD(*color);
802 unsigned dst = FB_UNPACK_SCALAR_LCD(data_get(ds->elem, ds->x, ds->y));
803 unsigned src;
804
805 /* Reuse 0 - 7 for src / clr blits*/
806 if(op >= 30 && op <= 37)
807 {
808 op -= 30;
809 src = clr;
810 }
811 else
812 src = FB_UNPACK_SCALAR_LCD(data_get(ss->elem, ss->x, ss->y));
813
814 switch(op)
815 {
816 default:
817 /* case 30: */
818 case 0: { dst = src; break; }/* copyS/C */
819 /* case 31: */
820 case 1: { dst = src | dst; break; }/* DorS/C */
821 /* case 32: */
822 case 2: { dst = src ^ dst; break; }/* DxorS/C */
823 /* case 33: */
824 case 3: { dst = ~(src | dst); break; }/* nDorS/C */
825 /* case 34: */
826 case 4: { dst = (~src) | dst; break; }/* DornS/C */
827 /* case 35: */
828 case 5: { dst = src & dst; break; }/* DandS/C */
829 /* case 36: */
830 case 6: { dst = src & (~dst); break; }/* nDandS/C */
831 /* case 37: */
832 case 7: { dst = ~src; break; }/* notS/C */
833
834 /* mask blits */
835 case 8: { if(src != 0) { dst = clr; } break; }/* Sand */
836 case 9: { if(src == 0) { dst = clr; } break; }/* Snot */
837
838 case 10: { dst = src | clr; break; }/* SorC */
839 case 11: { dst = src ^ clr; break; }/* SxorC */
840 case 12: { dst = ~(src | clr); break; }/* nSorC */
841 case 13: { dst = src | (~clr); break; }/* SornC */
842 case 14: { dst = src & clr; break; }/* SandC */
843 case 15: { dst = (~src) & clr; break; }/* nSandC */
844 case 16: { dst |= (~src) | clr; break; }/* DornSorC */
845 case 17: { dst ^= (src & (dst ^ clr)); break; }/* DxorSandDxorC */
846
847 case 18: { if(src != clr) { dst = src; } break; }
848 case 19: { if(src == clr) { dst = src; } break; }
849 case 20: { if(src > clr) { dst = src; } break; }
850 case 21: { if(src < clr) { dst = src; } break; }
851
852 case 22: { if(dst != clr) { dst = src; } break; }
853 case 23: { if(dst == clr) { dst = src; } break; }
854 case 24: { if(dst > clr) { dst = src; } break; }
855 case 25: { if(dst < clr) { dst = src; } break; }
856
857 case 26: { if(dst != src) { dst = clr; } break; }
858 case 27: { if(dst == src) { dst = clr; } break; }
859 case 28: { if(dst > src) { dst = clr; } break; }
860 case 29: { if(dst < src) { dst = clr; } break; }
861
862 }/*switch op*/
863 fb_data data = FB_SCALARPACK(dst);
864 data_set(ds->elem, ds->x, ds->y, &data);
865 return 1;
866} /* blit_transform */
867
868static int invert_transform(lua_State *L,
869 struct rli_iter_d *ds,
870 struct rli_iter_d *ss,
871 int op,
872 fb_data *color)
873{
874 (void) L;
875 (void) color;
876 (void) op;
877 (void) ss;
878
879 fb_data val = invert_color(data_get(ds->elem, ds->x, ds->y));
880 data_set(ds->elem, ds->x, ds->y, &val);
881
882 return 1;
883} /* invert_transform */
884#endif /* RLI_EXTENDED */
885
886/* RLI to LUA Interface functions *********************************************/
887RLI_LUA rli_new(lua_State *L)
888{ /* [width, height] */
889 int width = luaL_optint(L, 1, LCD_WIDTH);
890 int height = luaL_optint(L, 2, LCD_HEIGHT);
891
892 luaL_argcheck(L, width > 0, 1, ERR_IDX_RANGE);
893 luaL_argcheck(L, height > 0, 2, ERR_IDX_RANGE);
894
895 rli_alloc(L, width, height);
896
897 return 1;
898}
899
900RLI_LUA rli_set(lua_State *L)
901{
902 /*(set) (dst*, [x1, y1, clr, clip]) */
903 /* get and set share the same underlying function */
904 return rli_setget(L, false);
905}
906
907RLI_LUA rli_get(lua_State *L)
908{
909 /*(get) (dst*, [x1, y1, clip]) */
910 /* get and set share the same underlying function */
911 return rli_setget(L, true);
912}
913
914RLI_LUA rli_height(lua_State *L)
915{
916 struct rocklua_image *a = rli_checktype(L, 1);
917 lua_pushnumber(L, a->height);
918 return 1;
919}
920
921RLI_LUA rli_width(lua_State *L)
922{
923 struct rocklua_image *a = rli_checktype(L, 1);
924 lua_pushnumber(L, a->width);
925 return 1;
926}
927
928RLI_LUA rli_equal(lua_State *L)
929{
930 struct rocklua_image *a = rli_checktype(L, 1);
931 struct rocklua_image *b = rli_checktype(L, 2);
932 lua_pushboolean(L, a->data == b->data);
933 return 1;
934}
935
936RLI_LUA rli_size(lua_State *L)
937{
938 struct rocklua_image *a = rli_checktype(L, 1);
939 lua_pushnumber(L, a->elems);
940 return 1;
941}
942
943RLI_LUA rli_raw(lua_State *L)
944{
945 /*val = (img*, index, [new_val]) */
946 struct rocklua_image *a = rli_checktype(L, 1);
947
948 size_t i = (unsigned) luaL_checkint(L, 2);
949
950 fb_data val;
951
952 luaL_argcheck(L, i > 0 && i <= (a->elems), 2, ERR_IDX_RANGE);
953
954 lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(a->data[i-1]));
955
956 if(!lua_isnoneornil(L, 3))
957 {
958 val = FB_SCALARPACK((unsigned) luaL_checknumber(L, 3));
959 a->data[i-1] = val;
960 }
961
962 return 1;
963}
964
965RLI_LUA rli_tostring(lua_State *L)
966{
967 /* (img, [infoitem]) */
968 struct rocklua_image *a = rli_checktype(L, 1);
969
970 int item = (unsigned) luaL_optint(L, 2, 0);
971 size_t bytes = a->elems * sizeof(fb_data);
972
973 switch(item)
974 {
975 default:
976 case RLI_INFO_ALL:
977 {
978 lua_pushfstring(L,
979 ROCKLUA_IMAGE ": %dx%d, %d elements, %d bytes, %d-bit depth, %d pack",
980 a->width, a->height, a->elems, bytes, LCD_DEPTH, LCD_PIXELFORMAT);
981 break;
982 }
983 case RLI_INFO_TYPE: { lua_pushfstring(L, ROCKLUA_IMAGE ); break; }
984 case RLI_INFO_WIDTH: { lua_pushfstring(L, "%d", a->width ); break; }
985 case RLI_INFO_HEIGHT: { lua_pushfstring(L, "%d", a->height ); break; }
986 case RLI_INFO_ELEMS: { lua_pushfstring(L, "%d", a->elems ); break; }
987 case RLI_INFO_BYTES: { lua_pushfstring(L, "%d", bytes ); break; }
988 case RLI_INFO_DEPTH: { lua_pushfstring(L, "%d", LCD_DEPTH ); break; }
989 case RLI_INFO_FORMAT: { lua_pushfstring(L, "%d", LCD_PIXELFORMAT); break; }
990 case RLI_INFO_ADDRESS: { lua_pushfstring(L, "%p", a->data); break; }
991 }
992
993 return 1;
994}
995
996#ifdef RLI_EXTENDED
997RLI_LUA rli_ellipse(lua_State *L)
998{
999 /* (dst*, x1, y1, x2, y2, [clr, fillclr, clip]) */
1000 /* line and ellipse share the same init function */
1001 return rli_line_ellipse(L, true);
1002}
1003
1004RLI_LUA rli_line(lua_State *L)
1005{
1006 /* (dst*, x1, y1, [x2, y2, clr, clip]) */
1007 /* line and ellipse share the same init function */
1008 return rli_line_ellipse(L, false);
1009}
1010
1011RLI_LUA rli_iterator(lua_State *L) {
1012 /* see rli_iterator_factory */
1013 struct rli_iter_d *ds;
1014 ds = (struct rli_iter_d *) lua_touserdata(L, lua_upvalueindex(1));
1015
1016 if(ds->dx != 0 || ds->dy != 0)
1017 {
1018 lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(data_get(ds->elem, ds->x, ds->y)));
1019
1020 lua_pushinteger(L, ds->x);
1021 lua_pushinteger(L, ds->y);
1022
1023 next_rli_iter(ds); /* load next element */
1024
1025 return 3;
1026 }
1027 return 0; /* nothing left to do */
1028}
1029
1030RLI_LUA rli_iterator_factory(lua_State *L) {
1031 /* (src*, [x1, y1, x2, y2, dx, dy]) */
1032 struct rocklua_image *a = rli_checktype(L, 1); /*image we wish to iterate*/
1033
1034 struct rli_iter_d *ds;
1035
1036 int x1 = luaL_optint(L, 2, 1);
1037 int y1 = luaL_optint(L, 3, 1);
1038 int x2 = luaL_optint(L, 4, a->width - x1 + 1);
1039 int y2 = luaL_optint(L, 5, a->height - y1 + 1);
1040 int dx = luaL_optint(L, 6, 1);
1041 int dy = luaL_optint(L, 7, 1);
1042
1043 /* create new iter + pushed onto stack */
1044 ds = (struct rli_iter_d *) lua_newuserdata(L, sizeof(struct rli_iter_d));
1045
1046 init_rli_iter(ds, a, x1, y1, x2, y2, dx, dy, false, false);
1047
1048 /* returns the iter function with embedded iter data(up values) */
1049 lua_pushcclosure(L, &rli_iterator, 1);
1050
1051 return 1;
1052}
1053
1054RLI_LUA rli_marshal(lua_State *L) /* also invert */
1055{
1056 /* (img*, [x1, y1, x2, y2, dx, dy, clip, function]) */
1057 struct rocklua_image *a = rli_checktype(L, 1);
1058
1059 struct rli_iter_d ds;
1060
1061 int x1 = luaL_optint(L, 2, 1);
1062 int y1 = luaL_optint(L, 3, 1);
1063 int x2 = luaL_optint(L, 4, a->width);
1064 int y2 = luaL_optint(L, 5, a->height);
1065 int dx = luaL_optint(L, 6, 1);
1066 int dy = luaL_optint(L, 7, 1);
1067 bool clip = luaL_optboolean(L, 8, false);
1068
1069 int lf_ref = LUA_NOREF; /* de-ref'd without consequence */
1070
1071 int (*rli_trans)(lua_State *, struct rli_iter_d *, struct rli_iter_d *, int, fb_data *);
1072 rli_trans = invert_transform; /* default transformation */
1073
1074 if(!clip)
1075 {
1076 bounds_check_xy(L, a, 2, x1, 3, y1);
1077 bounds_check_xy(L, a, 4, x2, 5, y2);
1078 }
1079
1080 init_rli_iter(&ds, a, x1, y1, x2, y2, dx, dy, false, false);
1081
1082 if(lua_isfunction(L, 9)) /* custom function */
1083 {
1084 rli_trans = custom_transform;
1085 lf_ref = register_luafunc(L, 9);
1086 }
1087
1088 do
1089 {
1090 luaL_argcheck(L, clip || (ds.elem != NULL), 1, ERR_DATA_OVF);
1091
1092 if(!(*rli_trans)(L, &ds, NULL, lf_ref, NULL))
1093 break; /* Custom op can quit early */
1094
1095 } while(next_rli_iter(&ds));
1096
1097 luaL_unref(L, LUA_REGISTRYINDEX, lf_ref); /* de-reference custom function */
1098 return 0;
1099}
1100
1101RLI_LUA rli_copy(lua_State *L)
1102{
1103 /* (dst*, src*, [d_x, d_y, s_x, s_y, x_off, y_off, clip, [op, funct/clr]]) */
1104 struct rocklua_image *d = rli_checktype(L, 1); /*dst*/
1105 struct rocklua_image *s = rli_checktype(L, 2); /*src*/
1106
1107 struct rli_iter_d ds; /*dst*/
1108 struct rli_iter_d ss; /*src*/
1109
1110 /* copy whole image if possible */
1111 if(s->elems == d->elems && s->width == d->width && lua_gettop(L) < 3)
1112 {
1113 memcpy(d->data, s->data, d->elems * sizeof(fb_data));
1114 return 0;
1115 }
1116
1117 int d_x = luaL_optint(L, 3, 1);
1118 int d_y = luaL_optint(L, 4, 1);
1119 int s_x = luaL_optint(L, 5, 1);
1120 int s_y = luaL_optint(L, 6, 1);
1121
1122 int w = MIN(d->width - d_x, s->width - s_x);
1123 int h = MIN(d->height - d_y, s->height - s_y);
1124
1125 int x_off = luaL_optint(L, 7, w);
1126 int y_off = luaL_optint(L, 8, h);
1127
1128 bool clip = luaL_optboolean(L, 9, false);
1129 int op = luaL_optint(L, 10, 0);
1130 fb_data clr = FB_SCALARPACK(0); /* 11 is custom function | color */
1131
1132 bool d_swx = (x_off < 0); /* dest swap */
1133 bool d_swy = (y_off < 0);
1134 bool s_swx = false; /* src swap */
1135 bool s_swy = false;
1136
1137 int (*rli_trans)(lua_State *, struct rli_iter_d *, struct rli_iter_d *, int, fb_data *);
1138 rli_trans = blit_transform; /* default transformation */
1139
1140 int lf_ref = LUA_NOREF; /* de-ref'd without consequence */
1141
1142 if(!clip) /* Out of bounds is not allowed */
1143 {
1144 bounds_check_xy(L, d, 3, d_x, 4, d_y);
1145 bounds_check_xy(L, s, 5, s_x, 6, s_y);
1146 }
1147 else if (w < 0 || h < 0) /* not an error but nothing to display */
1148 return 0;
1149
1150 w = MIN(w, ABS(x_off));
1151 h = MIN(h, ABS(y_off));
1152
1153 /* if(s->data == d->data) need to care about fill direction */
1154 if(d_x > s_x)
1155 {
1156 d_swx = !d_swx;
1157 s_swx = !s_swx;
1158 }
1159
1160 if(d_y > s_y)
1161 {
1162 d_swy = !d_swy;
1163 s_swy = !s_swy;
1164 }
1165
1166 init_rli_iter(&ds, d, d_x, d_y, d_x + w, d_y + h, 1, 1, d_swx, d_swy);
1167
1168 init_rli_iter(&ss, s, s_x, s_y, s_x + w, s_y + h, 1, 1, s_swx, s_swy);
1169
1170 if (op == 0xFF && lua_isfunction(L, 11)) /* custom function specified.. */
1171 {
1172 rli_trans = custom_transform;
1173 lf_ref = register_luafunc(L, 11);
1174 op = lf_ref;
1175 }
1176 else
1177 clr = FB_SCALARPACK((unsigned) luaL_optnumber(L, 11, LCD_BLACK));
1178
1179 do
1180 {
1181 if(!clip)
1182 {
1183 luaL_argcheck(L, ss.elem != NULL, 2, ERR_DATA_OVF);
1184 luaL_argcheck(L, ds.elem != NULL, 1, ERR_DATA_OVF);
1185 }
1186
1187 if(!(*rli_trans)(L, &ds, &ss, op, &clr))
1188 break; /* Custom op can quit early */
1189
1190 } while(next_rli_iter(&ds) && next_rli_iter(&ss));
1191
1192 luaL_unref(L, LUA_REGISTRYINDEX, lf_ref); /* de-reference custom function */
1193 return 0;
1194}
1195
1196RLI_LUA rli_clear(lua_State *L)
1197{
1198 /* (dst*, [color, x1, y1, x2, y2, clip]) */
1199 struct rocklua_image *a = rli_checktype(L, 1);
1200
1201 struct rli_iter_d ds;
1202
1203 fb_data clr = FB_SCALARPACK((unsigned) luaL_optnumber(L, 2, LCD_BLACK));
1204 int x1 = luaL_optint(L, 3, 1);
1205 int y1 = luaL_optint(L, 4, 1);
1206 int x2 = luaL_optint(L, 5, a->width);
1207 int y2 = luaL_optint(L, 6, a->height);
1208 bool clip = luaL_optboolean(L, 7, false);
1209
1210 if(!clip)
1211 {
1212 bounds_check_xy(L, a, 3, x1, 4, y1);
1213 bounds_check_xy(L, a, 5, x2, 6, y2);
1214 }
1215
1216 init_rli_iter(&ds, a, x1, y1, x2, y2, 1, 1, false, false);
1217
1218 do
1219 {
1220 luaL_argcheck(L, clip || (ds.elem != NULL), 1, ERR_DATA_OVF);
1221
1222 data_set(ds.elem, ds.x, ds.y, &clr);
1223
1224 } while(next_rli_iter(&ds));
1225
1226 return 0;
1227}
1228#endif /* RLI_EXTENDED */
1229
1230/* Rli Image methods exported to lua */
1231static const struct luaL_reg rli_lib [] =
1232{
1233 {"__tostring", rli_tostring},
1234 {"_data", rli_raw},
1235 {"__len", rli_size},
1236 {"__eq", rli_equal},
1237 {"width", rli_width},
1238 {"height", rli_height},
1239 {"set", rli_set},
1240 {"get", rli_get},
1241
1242#ifdef RLI_EXTENDED
1243 {"copy", rli_copy},
1244 {"clear", rli_clear},
1245 {"invert", rli_marshal},
1246 {"marshal", rli_marshal},
1247 {"points", rli_iterator_factory},
1248 {"line", rli_line},
1249 {"ellipse", rli_ellipse},
1250#endif /* RLI_EXTENDED */
1251 {NULL, NULL}
1252};
1253
1254static inline void rli_init(lua_State *L)
1255{
1256 /* some devices need x | y coords shifted to match native format */
1257 /* conversion between packed native formats and individual pixel addressing */
1258 init_pixelmask(&x_shift, &y_shift, &xy_mask, &pixelmask);
1259
1260 luaL_newmetatable(L, ROCKLUA_IMAGE);
1261
1262 lua_pushstring(L, "__index");
1263 lua_pushvalue(L, -2); /* pushes the metatable */
1264 lua_settable(L, -3); /* metatable.__index = metatable */
1265
1266 luaL_register(L, NULL, rli_lib);
1267}
1268
1269/* 49/*
1270 * ----------------------------- 50 * -----------------------------
1271 * 51 *
@@ -1337,117 +117,6 @@ RB_WRAP(clear_viewport)
1337 return 0; 117 return 0;
1338} 118}
1339 119
1340#ifdef HAVE_LCD_BITMAP
1341RB_WRAP(lcd_framebuffer)
1342{
1343 rli_wrap(L, rb->lcd_framebuffer, LCD_WIDTH, LCD_HEIGHT);
1344 return 1;
1345}
1346
1347RB_WRAP(lcd_mono_bitmap_part)
1348{
1349 struct rocklua_image *src = rli_checktype(L, 1);
1350 int src_x = luaL_checkint(L, 2);
1351 int src_y = luaL_checkint(L, 3);
1352 int stride = luaL_checkint(L, 4);
1353 int x = luaL_checkint(L, 5);
1354 int y = luaL_checkint(L, 6);
1355 int width = luaL_checkint(L, 7);
1356 int height = luaL_checkint(L, 8);
1357 int screen = luaL_optint(L, 9, SCREEN_MAIN);
1358
1359 rb->screens[screen]->mono_bitmap_part((const unsigned char *)src->data, src_x, src_y, stride, x, y, width, height);
1360 return 0;
1361}
1362
1363RB_WRAP(lcd_mono_bitmap)
1364{
1365 struct rocklua_image *src = rli_checktype(L, 1);
1366 int x = luaL_checkint(L, 2);
1367 int y = luaL_checkint(L, 3);
1368 int width = luaL_checkint(L, 4);
1369 int height = luaL_checkint(L, 5);
1370 int screen = luaL_optint(L, 6, SCREEN_MAIN);
1371
1372 rb->screens[screen]->mono_bitmap((const unsigned char *)src->data, x, y, width, height);
1373 return 0;
1374}
1375
1376#if LCD_DEPTH > 1
1377RB_WRAP(lcd_bitmap_part)
1378{
1379 struct rocklua_image *src = rli_checktype(L, 1);
1380 int src_x = luaL_checkint(L, 2);
1381 int src_y = luaL_checkint(L, 3);
1382 int stride = luaL_checkint(L, 4);
1383 int x = luaL_checkint(L, 5);
1384 int y = luaL_checkint(L, 6);
1385 int width = luaL_checkint(L, 7);
1386 int height = luaL_checkint(L, 8);
1387 int screen = luaL_optint(L, 9, SCREEN_MAIN);
1388
1389 rb->screens[screen]->bitmap_part(src->data, src_x, src_y, stride, x, y, width, height);
1390 return 0;
1391}
1392
1393RB_WRAP(lcd_bitmap)
1394{
1395 struct rocklua_image *src = rli_checktype(L, 1);
1396 int x = luaL_checkint(L, 2);
1397 int y = luaL_checkint(L, 3);
1398 int width = luaL_checkint(L, 4);
1399 int height = luaL_checkint(L, 5);
1400 int screen = luaL_optint(L, 6, SCREEN_MAIN);
1401
1402 rb->screens[screen]->bitmap(src->data, x, y, width, height);
1403 return 0;
1404}
1405
1406RB_WRAP(lcd_get_backdrop)
1407{
1408 fb_data* backdrop = rb->lcd_get_backdrop();
1409 if(backdrop == NULL)
1410 lua_pushnil(L);
1411 else
1412 rli_wrap(L, backdrop, LCD_WIDTH, LCD_HEIGHT);
1413
1414 return 1;
1415}
1416#endif /* LCD_DEPTH > 1 */
1417
1418#if LCD_DEPTH == 16
1419RB_WRAP(lcd_bitmap_transparent_part)
1420{
1421 struct rocklua_image *src = rli_checktype(L, 1);
1422 int src_x = luaL_checkint(L, 2);
1423 int src_y = luaL_checkint(L, 3);
1424 int stride = luaL_checkint(L, 4);
1425 int x = luaL_checkint(L, 5);
1426 int y = luaL_checkint(L, 6);
1427 int width = luaL_checkint(L, 7);
1428 int height = luaL_checkint(L, 8);
1429 int screen = luaL_optint(L, 9, SCREEN_MAIN);
1430
1431 rb->screens[screen]->transparent_bitmap_part(src->data, src_x, src_y, stride, x, y, width, height);
1432 return 0;
1433}
1434
1435RB_WRAP(lcd_bitmap_transparent)
1436{
1437 struct rocklua_image *src = rli_checktype(L, 1);
1438 int x = luaL_checkint(L, 2);
1439 int y = luaL_checkint(L, 3);
1440 int width = luaL_checkint(L, 4);
1441 int height = luaL_checkint(L, 5);
1442 int screen = luaL_optint(L, 6, SCREEN_MAIN);
1443
1444 rb->screens[screen]->transparent_bitmap(src->data, x, y, width, height);
1445 return 0;
1446}
1447#endif /* LCD_DEPTH == 16 */
1448
1449#endif /* defined(LCD_BITMAP) */
1450
1451RB_WRAP(current_tick) 120RB_WRAP(current_tick)
1452{ 121{
1453 lua_pushinteger(L, *rb->current_tick); 122 lua_pushinteger(L, *rb->current_tick);
@@ -1524,59 +193,6 @@ RB_WRAP(font_getstringsize)
1524 return 3; 193 return 3;
1525} 194}
1526 195
1527#ifdef HAVE_LCD_COLOR
1528RB_WRAP(lcd_rgbpack)
1529{
1530 int r = luaL_checkint(L, 1);
1531 int g = luaL_checkint(L, 2);
1532 int b = luaL_checkint(L, 3);
1533 int result = LCD_RGBPACK(r, g, b);
1534 lua_pushinteger(L, result);
1535 return 1;
1536}
1537
1538RB_WRAP(lcd_rgbunpack)
1539{
1540 int rgb = luaL_checkint(L, 1);
1541 lua_pushinteger(L, RGB_UNPACK_RED(rgb));
1542 lua_pushinteger(L, RGB_UNPACK_GREEN(rgb));
1543 lua_pushinteger(L, RGB_UNPACK_BLUE(rgb));
1544 return 3;
1545}
1546#endif
1547
1548RB_WRAP(read_bmp_file)
1549{
1550 struct bitmap bm;
1551 const char* filename = luaL_checkstring(L, 1);
1552 bool dither = luaL_optboolean(L, 2, true);
1553 bool transparent = luaL_optboolean(L, 3, false);
1554 int format = FORMAT_NATIVE;
1555
1556 if(dither)
1557 format |= FORMAT_DITHER;
1558
1559 if(transparent)
1560 format |= FORMAT_TRANSPARENT;
1561
1562 int result = rb->read_bmp_file(filename, &bm, 0, format | FORMAT_RETURN_SIZE, NULL);
1563
1564 if(result > 0)
1565 {
1566 bm.data = (unsigned char*) rli_alloc(L, bm.width, bm.height);
1567 if(rb->read_bmp_file(filename, &bm, result, format, NULL) < 0)
1568 {
1569 /* Error occured, drop newly allocated image from stack */
1570 lua_pop(L, 1);
1571 lua_pushnil(L);
1572 }
1573 }
1574 else
1575 lua_pushnil(L);
1576
1577 return 1;
1578}
1579
1580RB_WRAP(current_path) 196RB_WRAP(current_path)
1581{ 197{
1582 const char *current_path = get_current_path(L, 1); 198 const char *current_path = get_current_path(L, 1);
@@ -1596,7 +212,7 @@ static void fill_text_message(lua_State *L, struct text_message * message,
1596 int n = luaL_getn(L, pos); 212 int n = luaL_getn(L, pos);
1597 const char **lines = (const char**) tlsf_malloc(n * sizeof(const char*)); 213 const char **lines = (const char**) tlsf_malloc(n * sizeof(const char*));
1598 if(lines == NULL) 214 if(lines == NULL)
1599 luaL_error(L, "Can't allocate %d bytes!", n * sizeof(const char*)); 215 luaL_error(L, ERR_NO_ALLOC_DBYTES, n * sizeof(const char*));
1600 for(i=1; i<=n; i++) 216 for(i=1; i<=n; i++)
1601 { 217 {
1602 lua_rawgeti(L, pos, i); 218 lua_rawgeti(L, pos, i);
@@ -1645,7 +261,7 @@ RB_WRAP(do_menu)
1645 n = luaL_getn(L, 2); 261 n = luaL_getn(L, 2);
1646 items = (const char**) tlsf_malloc(n * sizeof(const char*)); 262 items = (const char**) tlsf_malloc(n * sizeof(const char*));
1647 if(items == NULL) 263 if(items == NULL)
1648 luaL_error(L, "Can't allocate %d bytes!", n * sizeof(const char*)); 264 luaL_error(L, ERR_NO_ALLOC_DBYTES, n * sizeof(const char*));
1649 for(i=1; i<=n; i++) 265 for(i=1; i<=n; i++)
1650 { 266 {
1651 lua_rawgeti(L, 2, i); /* Push item on the stack */ 267 lua_rawgeti(L, 2, i); /* Push item on the stack */
@@ -1760,26 +376,6 @@ RB_WRAP(get_plugin_action)
1760#define R(NAME) {#NAME, rock_##NAME} 376#define R(NAME) {#NAME, rock_##NAME}
1761static const luaL_Reg rocklib[] = 377static const luaL_Reg rocklib[] =
1762{ 378{
1763 /* Graphics */
1764#ifdef HAVE_LCD_BITMAP
1765 R(lcd_framebuffer),
1766 R(lcd_mono_bitmap_part),
1767 R(lcd_mono_bitmap),
1768#if LCD_DEPTH > 1
1769 R(lcd_get_backdrop),
1770 R(lcd_bitmap_part),
1771 R(lcd_bitmap),
1772#endif
1773#if LCD_DEPTH == 16
1774 R(lcd_bitmap_transparent_part),
1775 R(lcd_bitmap_transparent),
1776#endif
1777#endif
1778#ifdef HAVE_LCD_COLOR
1779 R(lcd_rgbpack),
1780 R(lcd_rgbunpack),
1781#endif
1782
1783 /* Kernel */ 379 /* Kernel */
1784 R(current_tick), 380 R(current_tick),
1785 381
@@ -1792,7 +388,6 @@ static const luaL_Reg rocklib[] =
1792 R(kbd_input), 388 R(kbd_input),
1793 389
1794 R(font_getstringsize), 390 R(font_getstringsize),
1795 R(read_bmp_file),
1796 R(set_viewport), 391 R(set_viewport),
1797 R(clear_viewport), 392 R(clear_viewport),
1798 R(current_path), 393 R(current_path),
@@ -1820,13 +415,11 @@ static const luaL_Reg rocklib[] =
1820#endif 415#endif
1821 R(get_plugin_action), 416 R(get_plugin_action),
1822 417
1823 {"new_image", rli_new},
1824
1825 {NULL, NULL} 418 {NULL, NULL}
1826}; 419};
1827#undef R 420#undef R
1828extern const luaL_Reg rocklib_aux[]; 421extern const luaL_Reg rocklib_aux[];
1829 422extern const luaL_Reg rocklib_img[];
1830 423
1831#define RB_CONSTANT(x) lua_pushinteger(L, x); lua_setfield(L, -2, #x); 424#define RB_CONSTANT(x) lua_pushinteger(L, x); lua_setfield(L, -2, #x);
1832#define RB_STRING_CONSTANT(x) lua_pushstring(L, x); lua_setfield(L, -2, #x); 425#define RB_STRING_CONSTANT(x) lua_pushstring(L, x); lua_setfield(L, -2, #x);
@@ -1837,6 +430,7 @@ LUALIB_API int luaopen_rock(lua_State *L)
1837{ 430{
1838 luaL_register(L, LUA_ROCKLIBNAME, rocklib); 431 luaL_register(L, LUA_ROCKLIBNAME, rocklib);
1839 luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux); 432 luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux);
433 luaL_register(L, LUA_ROCKLIBNAME, rocklib_img);
1840 434
1841 RB_CONSTANT(HZ); 435 RB_CONSTANT(HZ);
1842 436
diff --git a/apps/plugins/lua/rocklib.h b/apps/plugins/lua/rocklib.h
index 84b5fd2de0..83d82e56ef 100644
--- a/apps/plugins/lua/rocklib.h
+++ b/apps/plugins/lua/rocklib.h
@@ -23,6 +23,20 @@
23#define _ROCKLIB_H_ 23#define _ROCKLIB_H_
24 24
25#define LUA_ROCKLIBNAME "rb" 25#define LUA_ROCKLIBNAME "rb"
26
27#ifndef ERR_IDX_RANGE
28#define ERR_IDX_RANGE "index out of range"
29#endif
30
31#ifndef ERR_DATA_OVF
32#define ERR_DATA_OVF "data overflow"
33#endif
34
35#ifndef ERR_NO_ALLOC_DBYTES
36#define ERR_NO_ALLOC_DBYTES "Can't allocate %d bytes!"
37#endif
38
39
26LUALIB_API int (luaopen_rock) (lua_State *L); 40LUALIB_API int (luaopen_rock) (lua_State *L);
27const char* get_current_path(lua_State *L, int level); 41const char* get_current_path(lua_State *L, int level);
28 42
diff --git a/apps/plugins/lua/rocklib_img.c b/apps/plugins/lua/rocklib_img.c
new file mode 100644
index 0000000000..083679cdbb
--- /dev/null
+++ b/apps/plugins/lua/rocklib_img.c
@@ -0,0 +1,1452 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2008 Dan Everton (safetydan)
11 * Copyright (C) 2009 Maurus Cuelenaere
12 * Copyright (C) 2017 William Wilgus
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23
24#define lrockimg_c
25#define LUA_LIB
26
27#include "lua.h"
28#include "lauxlib.h"
29#include "rocklib.h"
30#include "rocklib_img.h"
31
32/*
33 * -----------------------------------------------------------------------------
34 *
35 * Rockbox Lua image wrapper
36 *
37 * Some devices(1-bit / 2-bit displays) have packed bit formats that
38 * need to be unpacked in order to work on them at a pixel level.
39 *
40 * The internal formats of these devices do not follow the same paradigm
41 * for image sizes either; We still display the actual width and height to
42 * the user but store stride based on the native values
43 *
44 * Conversion between native addressing and per pixel addressing
45 * incurs extra overhead but it is much faster to do it
46 * on the 'C' side rather than in lua.
47 *
48 * -----------------------------------------------------------------------------
49 */
50
51#define ROCKLUA_IMAGE LUA_ROCKLIBNAME ".image"
52
53/* mark for RLI to LUA Interface functions (luaState *L) is the only argument */
54#define RLI_LUA static int
55
56#ifndef ABS
57#define ABS(a)(((a) < 0) ? - (a) :(a))
58#endif
59
60#ifndef LCD_BLACK
61#define LCD_BLACK 0x0
62#endif
63
64struct rocklua_image
65{
66 int width;
67 int height;
68 int stride;
69 size_t elems;
70 fb_data *data;
71 fb_data dummy[1];
72};
73
74/* holds iterator data for rlimages */
75struct rli_iter_d
76{
77 struct rocklua_image *img;
78 fb_data *elem;
79 int x , y;
80 int x1, y1;
81 int x2, y2;
82 int dx, dy;
83};
84
85/* __tostring information enums */
86enum rli_info {RLI_INFO_ALL = 0, RLI_INFO_TYPE, RLI_INFO_WIDTH,
87 RLI_INFO_HEIGHT, RLI_INFO_ELEMS, RLI_INFO_BYTES,
88 RLI_INFO_DEPTH, RLI_INFO_FORMAT, RLI_INFO_ADDRESS};
89
90#ifdef HAVE_LCD_COLOR
91
92static inline fb_data invert_color(fb_data rgb)
93{
94 uint8_t r = 0xFFU - FB_UNPACK_RED(rgb);
95 uint8_t g = 0xFFU - FB_UNPACK_RED(rgb);
96 uint8_t b = 0xFFU - FB_UNPACK_RED(rgb);
97
98 return FB_RGBPACK(r, g, b);
99}
100#else /* !HAVE_LCD_COLOR */
101
102#define invert_color(c) (~c)
103
104#endif /* HAVE_LCD_COLOR */
105
106
107#if (LCD_DEPTH > 2) /* no native to pixel mapping needed */
108
109#define pixel_to_fb(x, y, o, n) {(void) x; (void) y; do { } while (0);}
110#define pixel_to_native(x, y, xn, yn) {*xn = x; *yn = y;}
111#define init_pixelmask(x, y, m, p) do { } while (0)
112
113
114#else /* some devices need x | y coords shifted to match native format */
115
116static fb_data x_shift = FB_SCALARPACK(0);
117static fb_data y_shift = FB_SCALARPACK(0);
118static fb_data xy_mask = FB_SCALARPACK(0);
119static const fb_data *pixelmask = NULL;
120
121/* conversion between packed native formats and individual pixel addressing */
122static inline void init_pixelmask(fb_data *x_shift, fb_data *y_shift,
123 fb_data *xy_mask, const fb_data **pixelmask)
124{
125
126#if(LCD_PIXELFORMAT == VERTICAL_PACKING) && LCD_DEPTH == 1
127 static const fb_data pixelmask_v1[8] =
128 {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
129 *pixelmask = pixelmask_v1;
130
131 (void) x_shift;
132 *y_shift = 3U;
133 *xy_mask = ((1 << (*y_shift)) - 1);
134#elif(LCD_PIXELFORMAT == VERTICAL_PACKING) && LCD_DEPTH == 2
135 static const fb_data pixelmask_v2[4] = {0x03, 0x0C, 0x30, 0xC0};
136 *pixelmask = pixelmask_v2;
137
138 (void) x_shift;
139 *y_shift = 2U;
140 *xy_mask = ((1 << (*y_shift)) - 1);
141#elif(LCD_PIXELFORMAT == VERTICAL_INTERLEAVED) && LCD_DEPTH == 2
142 static const fb_data pixelmask_vi2[8] =
143 {0x0101, 0x0202, 0x0404, 0x0808, 0x1010, 0x2020, 0x4040, 0x8080};
144 *pixelmask = pixelmask_vi2;
145
146 (void) x_shift;
147 *y_shift = 3U;
148 *xy_mask = ((1 << (*y_shift)) - 1);
149#elif(LCD_PIXELFORMAT == HORIZONTAL_PACKING) && LCD_DEPTH == 2
150 /* MSB on left */
151 static const fb_data pixelmask_h2[4] = {0x03, 0x0C, 0x30, 0xC0};
152 *pixelmask = pixelmask_h2;
153
154 (void) y_shift;
155 *x_shift = 2U;
156 *xy_mask = ((1 << (*x_shift)) - 1);
157#else
158 #warning Unknown Pixel Format
159#endif /* LCD_PIXELFORMAT */
160
161} /* init_pixelmask */
162
163static inline void pixel_to_native(int x, int y, int *x_native, int *y_native)
164{
165 *x_native = ((x - 1) >> x_shift) + 1;
166 *y_native = ((y - 1) >> y_shift) + 1;
167} /* pixel_to_native */
168
169static inline fb_data set_masked_pixel(fb_data old,
170 fb_data new,
171 fb_data mask,
172 int bit_n)
173{
174 /*equivalent of: (old & (~mask)) | ((new << bit_n) & mask);*/
175 return old ^ ((old ^ (new << bit_n)) & mask);
176
177} /* set_masked_pixel */
178
179static inline fb_data get_masked_pixel(fb_data val, fb_data mask, int bit_n)
180{
181 val = val & mask;
182 return val >> bit_n;
183} /* get_masked_pixel */
184
185/* conversion between packed native formats and individual pixel addressing */
186static void pixel_to_fb(int x, int y, fb_data *oldv, fb_data *newv)
187{
188 fb_data mask;
189 int bit_n;
190
191#if(LCD_PIXELFORMAT == VERTICAL_INTERLEAVED) && LCD_DEPTH == 2
192 (void) x;
193 const uint16_t greymap_vi2[4] = {0x0000, 0x0001, 0x0100, 0x0101};
194
195 bit_n = (y - 1) & xy_mask;
196 mask = pixelmask[bit_n];
197
198 *newv = greymap_vi2[*newv &(0x3)]; /* [0-3] => greymap */
199 *newv = set_masked_pixel(*oldv, *newv, mask, bit_n);
200
201 *oldv = get_masked_pixel(*oldv, mask, bit_n);
202
203 if((*oldv) > 1) /* greymap => [0-3] */
204 *oldv = ((*oldv) & 0x1U) + 2U; /* 2, 3 */
205 else
206 *oldv &= 1U; /* 0, 1 */
207
208#elif(LCD_DEPTH <= 2)
209 if(y_shift)
210 bit_n = (y - 1) & xy_mask;
211 else if(x_shift)
212 bit_n = xy_mask - ((x - 1) & xy_mask); /*MSB on left*/
213
214 if(y_shift || x_shift)
215 {
216 mask = pixelmask[bit_n];
217 bit_n *= LCD_DEPTH;
218
219 *newv = set_masked_pixel(*oldv, *newv, mask, bit_n);
220
221 *oldv = get_masked_pixel(*oldv, mask, bit_n);
222 }
223#else
224 #error Unknown Pixel Format
225#endif /* LCD_PIXELFORMAT == VERTICAL_INTERLEAVED && LCD_DEPTH == 2 */
226} /* pixel_to_fb */
227
228#endif /* (LCD_DEPTH > 2) no native to pixel mapping needed */
229
230/* Internal worker functions for image data array *****************************/
231
232static inline void swap_int(bool swap, int *v1, int *v2)
233{
234 if(swap)
235 {
236 int val = *v1;
237 *v1 = *v2;
238 *v2 = val;
239 }
240} /* swap_int */
241
242/* Throws error if x or y are out of bounds notifies user which narg indice
243 the out of bound variable originated */
244static void bounds_check_xy(lua_State *L, struct rocklua_image *img,
245 int nargx, int x, int nargy, int y)
246{
247 luaL_argcheck(L, x <= img->width && x > 0, nargx, ERR_IDX_RANGE);
248 luaL_argcheck(L, y <= img->height && y > 0, nargy, ERR_IDX_RANGE);
249} /* bounds_check_xy */
250
251static struct rocklua_image* rli_checktype(lua_State *L, int arg)
252{
253 void *ud = luaL_checkudata(L, arg, ROCKLUA_IMAGE);
254
255 luaL_argcheck(L, ud != NULL, arg, "'" ROCKLUA_IMAGE "' expected");
256
257 return (struct rocklua_image*) ud;
258} /* rli_checktype */
259
260static struct rocklua_image * alloc_rlimage(lua_State *L, bool alloc_data,
261 int width, int height)
262{
263 /* rliimage is pushed on the stack it is up to you to pop it */
264 struct rocklua_image *img;
265
266 const size_t sz_header = sizeof(struct rocklua_image);
267 size_t sz_data = 0;
268 size_t n_elems;
269
270 int w_native;
271 int h_native;
272
273 pixel_to_native(width, height, &w_native, &h_native);
274
275 n_elems = (size_t)(w_native * h_native);
276
277 if(alloc_data) /* if this a new image we need space for image data */
278 sz_data = n_elems * sizeof(fb_data);
279
280 /* newuserdata pushes the userdata onto the stack */
281 img = (struct rocklua_image *) lua_newuserdata(L, sz_header + sz_data);
282
283 luaL_getmetatable(L, ROCKLUA_IMAGE);
284 lua_setmetatable(L, -2);
285
286 /* apparent w/h is stored but behind the scenes native w/h is used */
287 img->width = width;
288 img->height = height;
289 img->stride = w_native;
290 img->elems = n_elems;
291
292 return img;
293} /* alloc_rlimage */
294
295static inline void rli_wrap(lua_State *L, fb_data *src, int width, int height)
296{
297 /* rliimage is pushed on the stack it is up to you to pop it */
298 struct rocklua_image *a = alloc_rlimage(L, false, width, height);
299
300 a->data = src;
301} /* rli_wrap */
302
303static inline fb_data* rli_alloc(lua_State *L, int width, int height)
304{
305 /* rliimage is pushed on the stack it is up to you to pop it */
306 struct rocklua_image *a = alloc_rlimage(L, true, width, height);
307
308 a->data = &a->dummy[0]; /* ref to beginning of alloc'd img data */
309
310 return a->data;
311} /* rli_alloc */
312
313static inline fb_data data_setget(fb_data *elem, int x, int y, fb_data *val)
314{
315 fb_data old_val = FB_SCALARPACK(0);
316 fb_data new_val;
317
318 if(elem)
319 {
320 old_val = *elem;
321
322 if(val)
323 {
324 new_val = *val;
325 pixel_to_fb(x, y, &old_val, &new_val);
326 *elem = new_val;
327 }
328 else
329 pixel_to_fb(x, y, &old_val, &new_val);
330 }
331
332 return old_val;
333} /* data_setget */
334
335static inline fb_data data_set(fb_data *elem, int x, int y, fb_data *new_val)
336{
337 /* get and set share the same underlying function */
338 return data_setget(elem, x, y, new_val);
339} /* data_set */
340
341static inline fb_data data_get(fb_data *elem, int x, int y)
342{
343 /* get and set share the same underlying function */
344 return data_setget(elem, x, y, NULL);
345} /* data_get */
346
347static fb_data* rli_get_element(struct rocklua_image* img, int x, int y)
348{
349 int stride = img->stride;
350 size_t elements = img->elems;
351 fb_data *data = img->data;
352
353 pixel_to_native(x, y, &x, &y);
354
355 /* row major address */
356 size_t data_address = (stride * (y - 1)) + (x - 1);
357
358 /* x needs bound between 0 and stride otherwise overflow to prev/next y */
359 if(x <= 0 || x > stride || data_address >= elements)
360 return NULL; /* data overflow */
361
362 return &data[data_address]; /* return element address */
363} /* rli_get_element */
364
365/* Lua to C Interface for pixel set and get functions */
366static int rli_setget(lua_State *L, bool is_get)
367{
368 /*(set) (dst*, [x1, y1, clr, clip]) */
369 /*(get) (dst*, [x1, y1, clip]) */
370 struct rocklua_image *a = rli_checktype(L, 1);
371 int x = luaL_checkint(L, 2);
372 int y = luaL_checkint(L, 3);
373
374 fb_data clr = FB_SCALARPACK(0); /* Arg 4 is color if set element */
375 fb_data *p_clr = &clr;
376
377 int clip_narg;
378
379 if(is_get) /* get element */
380 {
381 p_clr = NULL;
382 clip_narg = 4;
383 }
384 else /* set element */
385 {
386 clr = FB_SCALARPACK((unsigned) luaL_checknumber(L, 4));
387 clip_narg = 5;
388 }
389
390 fb_data *element = rli_get_element(a, x, y);
391
392 if(!element)
393 {
394 if(!luaL_optboolean(L, clip_narg, false)) /* Error if !clip */
395 bounds_check_xy(L, a, 2, x, 3, y);
396
397 lua_pushnil(L);
398 return 1;
399 }
400
401 lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(data_setget(element, x, y, p_clr)));
402
403 /* returns old value */
404 return 1;
405} /* rli_setget */
406
407#ifdef RLI_EXTENDED
408static bool init_rli_iter(struct rli_iter_d *d,
409 struct rocklua_image *img,
410 int x1, int y1,
411 int x2, int y2,
412 int dx, int dy,
413 bool swx, bool swy)
414{
415
416 swap_int((swx), &x1, &x2);
417 swap_int((swy), &y1, &y2);
418
419 /* stepping in the correct x direction ? */
420 if((dx > 0 && x1 > x2) || (dx < 0 && x1 < x2))
421 dx = -dx;
422
423 /* stepping in the correct y direction ? */
424 if((dy > 0 && y1 > y2) || (dy < 0 && y1 < y2))
425 dy = -dy;
426
427 d->img = img;
428 d->x = x1;
429 d->y = y1;
430 d->x1 = x1;
431 d->y1 = y1;
432 d->x2 = x2;
433 d->y2 = y2;
434 d->dx = dx;
435 d->dy = dy;
436 d->elem = rli_get_element(img, d->x, d->y);
437
438 return(dx != 0 || dy != 0);
439} /* init_rli_iter */
440
441/* steps to the next point(x, y) by delta x/y, stores pointer to element
442 returns true if x & y haven't reached x2 & y2
443 if limit reached - element set to NULL, deltas set to 0 & false returned
444*/
445static bool next_rli_iter(struct rli_iter_d *d)
446{
447 if((d->dx > 0 && d->x < d->x2) || (d->dx < 0 && d->x > d->x2))
448 d->x += d->dx;
449 else if((d->dy > 0 && d->y < d->y2) || (d->dy < 0 && d->y > d->y2))
450 {
451 d->x = d->x1; /* Reset x*/
452 d->y += d->dy;
453 }
454 else
455 {
456 d->elem = NULL;
457 d->dx = 0;
458 d->dy = 0;
459 return false;
460 }
461
462 d->elem = rli_get_element(d->img, d->x, d->y);
463
464 return true;
465} /* next_rli_iter */
466
467static int d_line(struct rocklua_image *img,
468 int x1, int y1,
469 int x2, int y2,
470 fb_data *clr,
471 bool clip)
472{
473 /* NOTE! clr passed as pointer */
474 /* Bresenham midpoint line algorithm */
475 fb_data *element;
476
477 int r_a = x2 - x1; /* range of x direction called 'a' for now */
478 int r_b = y2 - y1; /* range of y direction called 'b' for now */
479
480 int s_a = 1; /* step of a direction */
481 int s_b = 1; /* step of b direction */
482
483 int d_err;
484 int numpixels;
485
486 int *a1 = &x1; /* pointer to the x var 'a' */
487 int *b1 = &y1; /* pointer to the y var 'b' */
488
489 if(r_a < 0) /* instead of negative range we will switch step instead */
490 {
491 r_a = -r_a;
492 s_a = -s_a;
493 }
494
495 if(r_b < 0) /* instead of negative range we will switch step instead */
496 {
497 r_b = -r_b;
498 s_b = -s_b;
499 }
500
501 x2 += s_a; /* add 1 extra point to make the whole line */
502 y2 += s_b; /* add 1 extra point */
503
504 if(r_b > r_a) /*if rangeY('b') > rangeX('a') swap their identities */
505 {
506 a1 = &y1;
507 b1 = &x1;
508 swap_int((true), &r_a, &r_b);
509 swap_int((true), &s_a, &s_b);
510 }
511
512 d_err = ((r_b << 1) - r_a) >> 1; /* preload err of 1 step (px centered) */
513
514 numpixels = r_a + 1;
515
516 r_a -= r_b; /* pre-subtract 'a' - 'b' */
517
518 for(;numpixels > 0; numpixels--)
519 {
520 element = rli_get_element(img, x1, y1);
521 if(element || clip)
522 data_set(element, x1, y1, clr);
523 else
524 return numpixels + 1; /* Error */
525
526 if(d_err >= 0) /* 0 is our target midpoint(exact point on the line) */
527 {
528 *b1 += s_b; /* whichever axis is in 'b' stepped(-1 or +1) */
529 d_err -= r_a;
530 }
531 else
532 d_err += r_b; /* only add 'b' when d_err < 0 */
533
534 *a1 += s_a; /* whichever axis is in 'a' stepped(-1 or +1) */
535 }
536
537 return 0;
538} /* d_line */
539
540/* ellipse worker function */
541static int d_ellipse_elements(struct rocklua_image * img,
542 int x1, int y1,
543 int x2, int y2,
544 int sx, int sy,
545 fb_data *clr,
546 fb_data *fillclr,
547 bool clip)
548{
549 int ret = 0;
550 fb_data *element1, *element2, *element3, *element4;
551
552 if(fillclr && x1 - sx != x2 + sx)
553 {
554 ret |= d_line(img, x1, y1, x2, y1, fillclr, clip); /* I. II.*/
555 ret |= d_line(img, x1, y2, x2, y2, fillclr, clip); /* III.IV.*/
556 }
557
558 x1 -= sx; /* shift x & y */
559 y1 -= sy;
560 x2 += sx;
561 y2 += sy;
562
563 element1 = rli_get_element(img, x2, y1);
564 element2 = rli_get_element(img, x1, y1);
565 element3 = rli_get_element(img, x1, y2);
566 element4 = rli_get_element(img, x2, y2);
567
568 if(clip || (element1 && element2 && element3 && element4))
569 {
570 data_set(element1, x2, y1, clr); /* I. Quadrant +x +y */
571 data_set(element2, x1, y1, clr); /* II. Quadrant -x +y */
572 data_set(element3, x1, y2, clr); /* III. Quadrant -x -y */
573 data_set(element4, x2, y2, clr); /* IV. Quadrant +x -y */
574 }
575 else
576 ret = 2; /* ERROR */
577
578 return ret;
579} /* d_ellipse_elements */
580
581static int d_ellipse(struct rocklua_image *img,
582 int x1, int y1,
583 int x2, int y2,
584 fb_data *clr,
585 fb_data *fillclr,
586 bool clip)
587{
588 /* NOTE! clr and fillclr passed as pointers */
589 /* Rasterizing algorithm derivative of work by Alois Zingl */
590#if LCD_WIDTH > 1024 || LCD_HEIGHT > 1024
591 /* Prevents overflow on large screens */
592 double dx, dy, err, e2;
593#else
594 long dx, dy, err, e2;
595#endif
596
597 int ret = 0;
598
599 int a = ABS(x2 - x1); /* diameter */
600 int b = ABS(y2 - y1); /* diameter */
601
602 if(a == 0 || b == 0 || !clr)
603 return ret; /* not an error but nothing to display */
604
605 int b1 = (b & 1);
606 b = b - (1 - b1);
607
608 int a2 = (a * a);
609 int b2 = (b * b);
610
611 dx = ((1 - a) * b2) >> 1; /* error increment */
612 dy = (b1 * a2) >> 1; /* error increment */
613
614 err = dx + dy + b1 * a2; /* error of 1.step */
615
616 /* if called with swapped points .. exchange them */
617 swap_int((x1 > x2), &x1, &x2);
618 swap_int((y1 > y2), &y1, &y2);
619
620 y1 += (b + 1) >> 1;
621 y2 = y1 - b1;
622
623 do
624 {
625 ret = d_ellipse_elements(img, x1, y1, x2, y2, 0, 0, clr, fillclr, clip);
626
627 e2 = err;
628
629 /* using division because you can't use bit shift on doubles.. */
630 if(e2 <= (dy / 2)) /* target midpoint - y step */
631 {
632 y1++;
633 y2--;
634 dy += a2;
635 err += dy;
636 }
637
638 if(e2 >= (dx / 2) || err > (dy / 2)) /* target midpoint - x step */
639 {
640 x1++;
641 x2--;
642 dx += b2;
643 err += dx;
644 }
645
646 } while(ret == 0 && x1 <= x2);
647
648 while (ret == 0 && y1 - y2 < b) /* early stop of flat ellipse a=1 finish tip */
649 {
650 ret = d_ellipse_elements(img, x1, y1, x2, y2, 1, 0, clr, fillclr, clip);
651
652 y1++;
653 y2--;
654 }
655
656 return ret;
657} /* d_ellipse */
658
659/* Lua to C Interface for line and ellipse */
660static int rli_line_ellipse(lua_State *L, bool is_ellipse)
661{
662 struct rocklua_image *a = rli_checktype(L, 1);
663
664 int x1 = luaL_checkint(L, 2);
665 int y1 = luaL_checkint(L, 3);
666 int x2 = luaL_optint(L, 4, x1);
667 int y2 = luaL_optint(L, 5, y1);
668
669 fb_data clr = FB_SCALARPACK((unsigned) luaL_checknumber(L, 6));
670
671 fb_data fillclr; /* fill color is index 7 if is_ellipse */
672 fb_data *p_fillclr = NULL;
673
674 bool clip;
675 int clip_narg;
676
677 if(is_ellipse)
678 clip_narg = 8;
679 else
680 clip_narg = 7;
681
682 clip = luaL_optboolean(L, clip_narg, false);
683
684 if(!clip)
685 {
686 bounds_check_xy(L, a, 2, x1, 3, y1);
687 bounds_check_xy(L, a, 4, x2, 5, y2);
688 }
689
690 if(is_ellipse)
691 {
692 if(!lua_isnoneornil(L, 7))
693 {
694 fillclr = FB_SCALARPACK((unsigned) luaL_checkint(L, 7));
695 p_fillclr = &fillclr;
696 }
697
698 luaL_argcheck(L, d_ellipse(a, x1, y1, x2, y2, &clr, p_fillclr, clip) == 0,
699 1, ERR_DATA_OVF);
700 }
701 else
702 luaL_argcheck(L, d_line(a, x1, y1, x2, y2, &clr, clip) == 0,
703 1, ERR_DATA_OVF);
704
705 return 0;
706} /* rli_line_ellipse */
707
708/* Pushes lua function from Stack at position narg to top of stack
709 and puts a reference in the global registry for later use */
710static inline int register_luafunc(lua_State *L, int narg_funct)
711{
712 lua_pushvalue(L, narg_funct); /* lua function */
713 int lf_ref = luaL_ref(L, LUA_REGISTRYINDEX);
714 lua_settop(L, 0); /* clear C stack for use by lua function */
715 return lf_ref;
716} /* register_luafunc */
717
718/* User defined pixel manipulations through rli_copy, rli_marshal */
719static int custom_transform(lua_State *L,
720 struct rli_iter_d *ds,
721 struct rli_iter_d *ss,
722 int op,
723 fb_data *color)
724{
725 (void) color;
726
727 fb_data dst;
728 fb_data src;
729
730 unsigned int params = 3;
731 int ret = 0;
732
733 lua_rawgeti(L, LUA_REGISTRYINDEX, op);
734
735 dst = data_get(ds->elem, ds->x, ds->y);
736 lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(dst));
737 lua_pushnumber(L, ds->x);
738 lua_pushnumber(L, ds->y);
739
740 if(ss) /* Allows src to be omitted */
741 {
742 params += 3;
743 src = data_get(ss->elem, ss->x, ss->y);
744 lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(src));
745 lua_pushnumber(L, ss->x);
746 lua_pushnumber(L, ss->y);
747 }
748
749 lua_call(L, params, 2); /* call custom function w/ n-params & 2 ret */
750
751 if(!lua_isnoneornil(L, -2))
752 {
753 ret = 1;
754 dst = FB_SCALARPACK((unsigned) luaL_checknumber(L, -2));
755 data_set(ds->elem, ds->x, ds->y, &dst);
756 }
757
758 if(!lua_isnoneornil(L, -1) && ss)
759 {
760 ret |= 2;
761 src = FB_SCALARPACK((unsigned) luaL_checknumber(L, -1));
762 data_set(ss->elem, ss->x, ss->y, &src);
763 }
764
765 lua_pop(L, 2);
766 return ret; /* 0 signals iterator to stop */
767} /* custom_transform */
768
769/* Pre defined pixel manipulations through rli_copy */
770static int blit_transform(lua_State *L,
771 struct rli_iter_d *ds,
772 struct rli_iter_d *ss,
773 int op,
774 fb_data *color)
775{
776 (void) L;
777 unsigned clr = FB_UNPACK_SCALAR_LCD(*color);
778 unsigned dst = FB_UNPACK_SCALAR_LCD(data_get(ds->elem, ds->x, ds->y));
779 unsigned src;
780
781 /* Reuse 0 - 7 for src / clr blits*/
782 if(op >= 30 && op <= 37)
783 {
784 op -= 30;
785 src = clr;
786 }
787 else
788 src = FB_UNPACK_SCALAR_LCD(data_get(ss->elem, ss->x, ss->y));
789
790 switch(op)
791 {
792 default:
793 /* case 30: */
794 case 0: { dst = src; break; }/* copyS/C */
795 /* case 31: */
796 case 1: { dst = src | dst; break; }/* DorS/C */
797 /* case 32: */
798 case 2: { dst = src ^ dst; break; }/* DxorS/C */
799 /* case 33: */
800 case 3: { dst = ~(src | dst); break; }/* nDorS/C */
801 /* case 34: */
802 case 4: { dst = (~src) | dst; break; }/* DornS/C */
803 /* case 35: */
804 case 5: { dst = src & dst; break; }/* DandS/C */
805 /* case 36: */
806 case 6: { dst = src & (~dst); break; }/* nDandS/C */
807 /* case 37: */
808 case 7: { dst = ~src; break; }/* notS/C */
809
810 /* mask blits */
811 case 8: { if(src != 0) { dst = clr; } break; }/* Sand */
812 case 9: { if(src == 0) { dst = clr; } break; }/* Snot */
813
814 case 10: { dst = src | clr; break; }/* SorC */
815 case 11: { dst = src ^ clr; break; }/* SxorC */
816 case 12: { dst = ~(src | clr); break; }/* nSorC */
817 case 13: { dst = src | (~clr); break; }/* SornC */
818 case 14: { dst = src & clr; break; }/* SandC */
819 case 15: { dst = (~src) & clr; break; }/* nSandC */
820 case 16: { dst |= (~src) | clr; break; }/* DornSorC */
821 case 17: { dst ^= (src & (dst ^ clr)); break; }/* DxorSandDxorC */
822
823 case 18: { if(src != clr) { dst = src; } break; }
824 case 19: { if(src == clr) { dst = src; } break; }
825 case 20: { if(src > clr) { dst = src; } break; }
826 case 21: { if(src < clr) { dst = src; } break; }
827
828 case 22: { if(dst != clr) { dst = src; } break; }
829 case 23: { if(dst == clr) { dst = src; } break; }
830 case 24: { if(dst > clr) { dst = src; } break; }
831 case 25: { if(dst < clr) { dst = src; } break; }
832
833 case 26: { if(dst != src) { dst = clr; } break; }
834 case 27: { if(dst == src) { dst = clr; } break; }
835 case 28: { if(dst > src) { dst = clr; } break; }
836 case 29: { if(dst < src) { dst = clr; } break; }
837
838 }/*switch op*/
839 fb_data data = FB_SCALARPACK(dst);
840 data_set(ds->elem, ds->x, ds->y, &data);
841 return 1;
842} /* blit_transform */
843
844static int invert_transform(lua_State *L,
845 struct rli_iter_d *ds,
846 struct rli_iter_d *ss,
847 int op,
848 fb_data *color)
849{
850 (void) L;
851 (void) color;
852 (void) op;
853 (void) ss;
854
855 fb_data val = invert_color(data_get(ds->elem, ds->x, ds->y));
856 data_set(ds->elem, ds->x, ds->y, &val);
857
858 return 1;
859} /* invert_transform */
860#endif /* RLI_EXTENDED */
861
862/* RLI to LUA Interface functions *********************************************/
863RLI_LUA rli_new(lua_State *L)
864{ /* [width, height] */
865 int width = luaL_optint(L, 1, LCD_WIDTH);
866 int height = luaL_optint(L, 2, LCD_HEIGHT);
867
868 luaL_argcheck(L, width > 0, 1, ERR_IDX_RANGE);
869 luaL_argcheck(L, height > 0, 2, ERR_IDX_RANGE);
870
871 rli_alloc(L, width, height);
872
873 return 1;
874}
875
876RLI_LUA rli_set(lua_State *L)
877{
878 /*(set) (dst*, [x1, y1, clr, clip]) */
879 /* get and set share the same underlying function */
880 return rli_setget(L, false);
881}
882
883RLI_LUA rli_get(lua_State *L)
884{
885 /*(get) (dst*, [x1, y1, clip]) */
886 /* get and set share the same underlying function */
887 return rli_setget(L, true);
888}
889
890RLI_LUA rli_height(lua_State *L)
891{
892 struct rocklua_image *a = rli_checktype(L, 1);
893 lua_pushnumber(L, a->height);
894 return 1;
895}
896
897RLI_LUA rli_width(lua_State *L)
898{
899 struct rocklua_image *a = rli_checktype(L, 1);
900 lua_pushnumber(L, a->width);
901 return 1;
902}
903
904RLI_LUA rli_equal(lua_State *L)
905{
906 struct rocklua_image *a = rli_checktype(L, 1);
907 struct rocklua_image *b = rli_checktype(L, 2);
908 lua_pushboolean(L, a->data == b->data);
909 return 1;
910}
911
912RLI_LUA rli_size(lua_State *L)
913{
914 struct rocklua_image *a = rli_checktype(L, 1);
915 lua_pushnumber(L, a->elems);
916 return 1;
917}
918
919RLI_LUA rli_raw(lua_State *L)
920{
921 /*val = (img*, index, [new_val]) */
922 struct rocklua_image *a = rli_checktype(L, 1);
923
924 size_t i = (unsigned) luaL_checkint(L, 2);
925
926 fb_data val;
927
928 luaL_argcheck(L, i > 0 && i <= (a->elems), 2, ERR_IDX_RANGE);
929
930 lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(a->data[i-1]));
931
932 if(!lua_isnoneornil(L, 3))
933 {
934 val = FB_SCALARPACK((unsigned) luaL_checknumber(L, 3));
935 a->data[i-1] = val;
936 }
937
938 return 1;
939}
940
941RLI_LUA rli_tostring(lua_State *L)
942{
943 /* (img, [infoitem]) */
944 struct rocklua_image *a = rli_checktype(L, 1);
945
946 int item = (unsigned) luaL_optint(L, 2, 0);
947 size_t bytes = a->elems * sizeof(fb_data);
948
949 switch(item)
950 {
951 default:
952 case RLI_INFO_ALL:
953 {
954 lua_pushfstring(L,
955 ROCKLUA_IMAGE ": %dx%d, %d elements, %d bytes, %d-bit depth, %d pack",
956 a->width, a->height, a->elems, bytes, LCD_DEPTH, LCD_PIXELFORMAT);
957 break;
958 }
959 case RLI_INFO_TYPE: { lua_pushfstring(L, ROCKLUA_IMAGE ); break; }
960 case RLI_INFO_WIDTH: { lua_pushfstring(L, "%d", a->width ); break; }
961 case RLI_INFO_HEIGHT: { lua_pushfstring(L, "%d", a->height ); break; }
962 case RLI_INFO_ELEMS: { lua_pushfstring(L, "%d", a->elems ); break; }
963 case RLI_INFO_BYTES: { lua_pushfstring(L, "%d", bytes ); break; }
964 case RLI_INFO_DEPTH: { lua_pushfstring(L, "%d", LCD_DEPTH ); break; }
965 case RLI_INFO_FORMAT: { lua_pushfstring(L, "%d", LCD_PIXELFORMAT); break; }
966 case RLI_INFO_ADDRESS: { lua_pushfstring(L, "%p", a->data); break; }
967 }
968
969 return 1;
970}
971
972#ifdef RLI_EXTENDED
973RLI_LUA rli_ellipse(lua_State *L)
974{
975 /* (dst*, x1, y1, x2, y2, [clr, fillclr, clip]) */
976 /* line and ellipse share the same init function */
977 return rli_line_ellipse(L, true);
978}
979
980RLI_LUA rli_line(lua_State *L)
981{
982 /* (dst*, x1, y1, [x2, y2, clr, clip]) */
983 /* line and ellipse share the same init function */
984 return rli_line_ellipse(L, false);
985}
986
987RLI_LUA rli_iterator(lua_State *L) {
988 /* see rli_iterator_factory */
989 struct rli_iter_d *ds;
990 ds = (struct rli_iter_d *) lua_touserdata(L, lua_upvalueindex(1));
991
992 if(ds->dx != 0 || ds->dy != 0)
993 {
994 lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(data_get(ds->elem, ds->x, ds->y)));
995
996 lua_pushinteger(L, ds->x);
997 lua_pushinteger(L, ds->y);
998
999 next_rli_iter(ds); /* load next element */
1000
1001 return 3;
1002 }
1003 return 0; /* nothing left to do */
1004}
1005
1006RLI_LUA rli_iterator_factory(lua_State *L) {
1007 /* (src*, [x1, y1, x2, y2, dx, dy]) */
1008 struct rocklua_image *a = rli_checktype(L, 1); /*image we wish to iterate*/
1009
1010 struct rli_iter_d *ds;
1011
1012 int x1 = luaL_optint(L, 2, 1);
1013 int y1 = luaL_optint(L, 3, 1);
1014 int x2 = luaL_optint(L, 4, a->width - x1 + 1);
1015 int y2 = luaL_optint(L, 5, a->height - y1 + 1);
1016 int dx = luaL_optint(L, 6, 1);
1017 int dy = luaL_optint(L, 7, 1);
1018
1019 /* create new iter + pushed onto stack */
1020 ds = (struct rli_iter_d *) lua_newuserdata(L, sizeof(struct rli_iter_d));
1021
1022 init_rli_iter(ds, a, x1, y1, x2, y2, dx, dy, false, false);
1023
1024 /* returns the iter function with embedded iter data(up values) */
1025 lua_pushcclosure(L, &rli_iterator, 1);
1026
1027 return 1;
1028}
1029
1030RLI_LUA rli_marshal(lua_State *L) /* also invert */
1031{
1032 /* (img*, [x1, y1, x2, y2, dx, dy, clip, function]) */
1033 struct rocklua_image *a = rli_checktype(L, 1);
1034
1035 struct rli_iter_d ds;
1036
1037 int x1 = luaL_optint(L, 2, 1);
1038 int y1 = luaL_optint(L, 3, 1);
1039 int x2 = luaL_optint(L, 4, a->width);
1040 int y2 = luaL_optint(L, 5, a->height);
1041 int dx = luaL_optint(L, 6, 1);
1042 int dy = luaL_optint(L, 7, 1);
1043 bool clip = luaL_optboolean(L, 8, false);
1044
1045 int lf_ref = LUA_NOREF; /* de-ref'd without consequence */
1046
1047 int (*rli_trans)(lua_State *, struct rli_iter_d *, struct rli_iter_d *, int, fb_data *);
1048 rli_trans = invert_transform; /* default transformation */
1049
1050 if(!clip)
1051 {
1052 bounds_check_xy(L, a, 2, x1, 3, y1);
1053 bounds_check_xy(L, a, 4, x2, 5, y2);
1054 }
1055
1056 init_rli_iter(&ds, a, x1, y1, x2, y2, dx, dy, false, false);
1057
1058 if(lua_isfunction(L, 9)) /* custom function */
1059 {
1060 rli_trans = custom_transform;
1061 lf_ref = register_luafunc(L, 9);
1062 }
1063
1064 do
1065 {
1066 luaL_argcheck(L, clip || (ds.elem != NULL), 1, ERR_DATA_OVF);
1067
1068 if(!(*rli_trans)(L, &ds, NULL, lf_ref, NULL))
1069 break; /* Custom op can quit early */
1070
1071 } while(next_rli_iter(&ds));
1072
1073 luaL_unref(L, LUA_REGISTRYINDEX, lf_ref); /* de-reference custom function */
1074 return 0;
1075}
1076
1077RLI_LUA rli_copy(lua_State *L)
1078{
1079 /* (dst*, src*, [d_x, d_y, s_x, s_y, x_off, y_off, clip, [op, funct/clr]]) */
1080 struct rocklua_image *d = rli_checktype(L, 1); /*dst*/
1081 struct rocklua_image *s = rli_checktype(L, 2); /*src*/
1082
1083 struct rli_iter_d ds; /*dst*/
1084 struct rli_iter_d ss; /*src*/
1085
1086 /* copy whole image if possible */
1087 if(s->elems == d->elems && s->width == d->width && lua_gettop(L) < 3)
1088 {
1089 rb->memcpy(d->data, s->data, d->elems * sizeof(fb_data));
1090 return 0;
1091 }
1092
1093 int d_x = luaL_optint(L, 3, 1);
1094 int d_y = luaL_optint(L, 4, 1);
1095 int s_x = luaL_optint(L, 5, 1);
1096 int s_y = luaL_optint(L, 6, 1);
1097
1098 int w = MIN(d->width - d_x, s->width - s_x);
1099 int h = MIN(d->height - d_y, s->height - s_y);
1100
1101 int x_off = luaL_optint(L, 7, w);
1102 int y_off = luaL_optint(L, 8, h);
1103
1104 bool clip = luaL_optboolean(L, 9, false);
1105 int op = luaL_optint(L, 10, 0);
1106 fb_data clr = FB_SCALARPACK(0); /* 11 is custom function | color */
1107
1108 bool d_swx = (x_off < 0); /* dest swap */
1109 bool d_swy = (y_off < 0);
1110 bool s_swx = false; /* src swap */
1111 bool s_swy = false;
1112
1113 int (*rli_trans)(lua_State *, struct rli_iter_d *, struct rli_iter_d *, int, fb_data *);
1114 rli_trans = blit_transform; /* default transformation */
1115
1116 int lf_ref = LUA_NOREF; /* de-ref'd without consequence */
1117
1118 if(!clip) /* Out of bounds is not allowed */
1119 {
1120 bounds_check_xy(L, d, 3, d_x, 4, d_y);
1121 bounds_check_xy(L, s, 5, s_x, 6, s_y);
1122 }
1123 else if (w < 0 || h < 0) /* not an error but nothing to display */
1124 return 0;
1125
1126 w = MIN(w, ABS(x_off));
1127 h = MIN(h, ABS(y_off));
1128
1129 /* if(s->data == d->data) need to care about fill direction */
1130 if(d_x > s_x)
1131 {
1132 d_swx = !d_swx;
1133 s_swx = !s_swx;
1134 }
1135
1136 if(d_y > s_y)
1137 {
1138 d_swy = !d_swy;
1139 s_swy = !s_swy;
1140 }
1141
1142 init_rli_iter(&ds, d, d_x, d_y, d_x + w, d_y + h, 1, 1, d_swx, d_swy);
1143
1144 init_rli_iter(&ss, s, s_x, s_y, s_x + w, s_y + h, 1, 1, s_swx, s_swy);
1145
1146 if (op == 0xFF && lua_isfunction(L, 11)) /* custom function specified.. */
1147 {
1148 rli_trans = custom_transform;
1149 lf_ref = register_luafunc(L, 11);
1150 op = lf_ref;
1151 }
1152 else
1153 clr = FB_SCALARPACK((unsigned) luaL_optnumber(L, 11, LCD_BLACK));
1154
1155 do
1156 {
1157 if(!clip)
1158 {
1159 luaL_argcheck(L, ss.elem != NULL, 2, ERR_DATA_OVF);
1160 luaL_argcheck(L, ds.elem != NULL, 1, ERR_DATA_OVF);
1161 }
1162
1163 if(!(*rli_trans)(L, &ds, &ss, op, &clr))
1164 break; /* Custom op can quit early */
1165
1166 } while(next_rli_iter(&ds) && next_rli_iter(&ss));
1167
1168 luaL_unref(L, LUA_REGISTRYINDEX, lf_ref); /* de-reference custom function */
1169 return 0;
1170}
1171
1172RLI_LUA rli_clear(lua_State *L)
1173{
1174 /* (dst*, [color, x1, y1, x2, y2, clip]) */
1175 struct rocklua_image *a = rli_checktype(L, 1);
1176
1177 struct rli_iter_d ds;
1178
1179 fb_data clr = FB_SCALARPACK((unsigned) luaL_optnumber(L, 2, LCD_BLACK));
1180 int x1 = luaL_optint(L, 3, 1);
1181 int y1 = luaL_optint(L, 4, 1);
1182 int x2 = luaL_optint(L, 5, a->width);
1183 int y2 = luaL_optint(L, 6, a->height);
1184 bool clip = luaL_optboolean(L, 7, false);
1185
1186 if(!clip)
1187 {
1188 bounds_check_xy(L, a, 3, x1, 4, y1);
1189 bounds_check_xy(L, a, 5, x2, 6, y2);
1190 }
1191
1192 init_rli_iter(&ds, a, x1, y1, x2, y2, 1, 1, false, false);
1193
1194 do
1195 {
1196 luaL_argcheck(L, clip || (ds.elem != NULL), 1, ERR_DATA_OVF);
1197
1198 data_set(ds.elem, ds.x, ds.y, &clr);
1199
1200 } while(next_rli_iter(&ds));
1201
1202 return 0;
1203}
1204#endif /* RLI_EXTENDED */
1205
1206/* Rli Image methods exported to lua */
1207const struct luaL_reg rli_lib [] =
1208{
1209 {"__tostring", rli_tostring},
1210 {"_data", rli_raw},
1211 {"__len", rli_size},
1212 {"__eq", rli_equal},
1213 {"width", rli_width},
1214 {"height", rli_height},
1215 {"set", rli_set},
1216 {"get", rli_get},
1217
1218#ifdef RLI_EXTENDED
1219 {"copy", rli_copy},
1220 {"clear", rli_clear},
1221 {"invert", rli_marshal},
1222 {"marshal", rli_marshal},
1223 {"points", rli_iterator_factory},
1224 {"line", rli_line},
1225 {"ellipse", rli_ellipse},
1226#endif /* RLI_EXTENDED */
1227
1228 {NULL, NULL}
1229};
1230
1231LUALIB_API int rli_init(lua_State *L)
1232{
1233 /* some devices need x | y coords shifted to match native format */
1234 /* conversion between packed native formats and individual pixel addressing */
1235 init_pixelmask(&x_shift, &y_shift, &xy_mask, &pixelmask);
1236
1237 luaL_newmetatable(L, ROCKLUA_IMAGE);
1238
1239 lua_pushstring(L, "__index");
1240 lua_pushvalue(L, -2); /* pushes the metatable */
1241 lua_settable(L, -3); /* metatable.__index = metatable */
1242
1243 luaL_register(L, NULL, rli_lib);
1244
1245#ifdef RLI_EXTENDED
1246 luaL_register(L, ROCKLUA_IMAGE, rli_lib);
1247#endif
1248 return 1;
1249}
1250
1251/*
1252 * -----------------------------
1253 *
1254 * Rockbox wrappers start here!
1255 *
1256 * -----------------------------
1257 */
1258
1259#define RB_WRAP(M) static int rock_##M(lua_State UNUSED_ATTR *L)
1260#ifdef HAVE_LCD_BITMAP
1261RB_WRAP(lcd_framebuffer)
1262{
1263 rli_wrap(L, rb->lcd_framebuffer, LCD_WIDTH, LCD_HEIGHT);
1264 return 1;
1265}
1266
1267RB_WRAP(lcd_mono_bitmap_part)
1268{
1269 struct rocklua_image *src = rli_checktype(L, 1);
1270 int src_x = luaL_checkint(L, 2);
1271 int src_y = luaL_checkint(L, 3);
1272 int stride = luaL_checkint(L, 4);
1273 int x = luaL_checkint(L, 5);
1274 int y = luaL_checkint(L, 6);
1275 int width = luaL_checkint(L, 7);
1276 int height = luaL_checkint(L, 8);
1277 int screen = luaL_optint(L, 9, SCREEN_MAIN);
1278
1279 rb->screens[screen]->mono_bitmap_part((const unsigned char *)src->data, src_x, src_y, stride, x, y, width, height);
1280 return 0;
1281}
1282
1283RB_WRAP(lcd_mono_bitmap)
1284{
1285 struct rocklua_image *src = rli_checktype(L, 1);
1286 int x = luaL_checkint(L, 2);
1287 int y = luaL_checkint(L, 3);
1288 int width = luaL_checkint(L, 4);
1289 int height = luaL_checkint(L, 5);
1290 int screen = luaL_optint(L, 6, SCREEN_MAIN);
1291
1292 rb->screens[screen]->mono_bitmap((const unsigned char *)src->data, x, y, width, height);
1293 return 0;
1294}
1295
1296#if LCD_DEPTH > 1
1297RB_WRAP(lcd_bitmap_part)
1298{
1299 struct rocklua_image *src = rli_checktype(L, 1);
1300 int src_x = luaL_checkint(L, 2);
1301 int src_y = luaL_checkint(L, 3);
1302 int stride = luaL_checkint(L, 4);
1303 int x = luaL_checkint(L, 5);
1304 int y = luaL_checkint(L, 6);
1305 int width = luaL_checkint(L, 7);
1306 int height = luaL_checkint(L, 8);
1307 int screen = luaL_optint(L, 9, SCREEN_MAIN);
1308
1309 rb->screens[screen]->bitmap_part(src->data, src_x, src_y, stride, x, y, width, height);
1310 return 0;
1311}
1312
1313RB_WRAP(lcd_bitmap)
1314{
1315 struct rocklua_image *src = rli_checktype(L, 1);
1316 int x = luaL_checkint(L, 2);
1317 int y = luaL_checkint(L, 3);
1318 int width = luaL_checkint(L, 4);
1319 int height = luaL_checkint(L, 5);
1320 int screen = luaL_optint(L, 6, SCREEN_MAIN);
1321
1322 rb->screens[screen]->bitmap(src->data, x, y, width, height);
1323 return 0;
1324}
1325
1326RB_WRAP(lcd_get_backdrop)
1327{
1328 fb_data* backdrop = rb->lcd_get_backdrop();
1329 if(backdrop == NULL)
1330 lua_pushnil(L);
1331 else
1332 rli_wrap(L, backdrop, LCD_WIDTH, LCD_HEIGHT);
1333
1334 return 1;
1335}
1336#endif /* LCD_DEPTH > 1 */
1337
1338#if LCD_DEPTH == 16
1339RB_WRAP(lcd_bitmap_transparent_part)
1340{
1341 struct rocklua_image *src = rli_checktype(L, 1);
1342 int src_x = luaL_checkint(L, 2);
1343 int src_y = luaL_checkint(L, 3);
1344 int stride = luaL_checkint(L, 4);
1345 int x = luaL_checkint(L, 5);
1346 int y = luaL_checkint(L, 6);
1347 int width = luaL_checkint(L, 7);
1348 int height = luaL_checkint(L, 8);
1349 int screen = luaL_optint(L, 9, SCREEN_MAIN);
1350
1351 rb->screens[screen]->transparent_bitmap_part(src->data, src_x, src_y, stride, x, y, width, height);
1352 return 0;
1353}
1354
1355RB_WRAP(lcd_bitmap_transparent)
1356{
1357 struct rocklua_image *src = rli_checktype(L, 1);
1358 int x = luaL_checkint(L, 2);
1359 int y = luaL_checkint(L, 3);
1360 int width = luaL_checkint(L, 4);
1361 int height = luaL_checkint(L, 5);
1362 int screen = luaL_optint(L, 6, SCREEN_MAIN);
1363
1364 rb->screens[screen]->transparent_bitmap(src->data, x, y, width, height);
1365 return 0;
1366}
1367#endif /* LCD_DEPTH == 16 */
1368
1369#endif /* defined(LCD_BITMAP) */
1370
1371#ifdef HAVE_LCD_COLOR
1372RB_WRAP(lcd_rgbpack)
1373{
1374 int r = luaL_checkint(L, 1);
1375 int g = luaL_checkint(L, 2);
1376 int b = luaL_checkint(L, 3);
1377 int result = LCD_RGBPACK(r, g, b);
1378 lua_pushinteger(L, result);
1379 return 1;
1380}
1381
1382RB_WRAP(lcd_rgbunpack)
1383{
1384 int rgb = luaL_checkint(L, 1);
1385 lua_pushinteger(L, RGB_UNPACK_RED(rgb));
1386 lua_pushinteger(L, RGB_UNPACK_GREEN(rgb));
1387 lua_pushinteger(L, RGB_UNPACK_BLUE(rgb));
1388 return 3;
1389}
1390#endif
1391
1392RB_WRAP(read_bmp_file)
1393{
1394 struct bitmap bm;
1395 const char* filename = luaL_checkstring(L, 1);
1396 bool dither = luaL_optboolean(L, 2, true);
1397 bool transparent = luaL_optboolean(L, 3, false);
1398 int format = FORMAT_NATIVE;
1399
1400 if(dither)
1401 format |= FORMAT_DITHER;
1402
1403 if(transparent)
1404 format |= FORMAT_TRANSPARENT;
1405
1406 int result = rb->read_bmp_file(filename, &bm, 0, format | FORMAT_RETURN_SIZE, NULL);
1407
1408 if(result > 0)
1409 {
1410 bm.data = (unsigned char*) rli_alloc(L, bm.width, bm.height);
1411 if(rb->read_bmp_file(filename, &bm, result, format, NULL) < 0)
1412 {
1413 /* Error occured, drop newly allocated image from stack */
1414 lua_pop(L, 1);
1415 lua_pushnil(L);
1416 }
1417 }
1418 else
1419 lua_pushnil(L);
1420
1421 return 1;
1422}
1423
1424#define R(NAME) {#NAME, rock_##NAME}
1425const luaL_Reg rocklib_img[] =
1426{
1427 /* Graphics */
1428#ifdef HAVE_LCD_BITMAP
1429 R(lcd_framebuffer),
1430 R(lcd_mono_bitmap_part),
1431 R(lcd_mono_bitmap),
1432#if LCD_DEPTH > 1
1433 R(lcd_get_backdrop),
1434 R(lcd_bitmap_part),
1435 R(lcd_bitmap),
1436#endif
1437#if LCD_DEPTH == 16
1438 R(lcd_bitmap_transparent_part),
1439 R(lcd_bitmap_transparent),
1440#endif
1441#endif /*HAVE_LCD_BITMAP*/
1442#ifdef HAVE_LCD_COLOR
1443 R(lcd_rgbpack),
1444 R(lcd_rgbunpack),
1445#endif
1446 R(read_bmp_file),
1447 {"new_image", rli_new},
1448
1449 {NULL, NULL}
1450};
1451#undef R
1452
diff --git a/apps/plugins/lua/rocklib_img.h b/apps/plugins/lua/rocklib_img.h
new file mode 100644
index 0000000000..8b22806862
--- /dev/null
+++ b/apps/plugins/lua/rocklib_img.h
@@ -0,0 +1,31 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2008 Dan Everton (safetydan)
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#ifndef _ROCKLIB_IMG_H_
23#define _ROCKLIB_IMG_H_
24
25#ifdef HAVE_LCD_BITMAP
26#define RLI_EXTENDED
27#endif
28
29LUALIB_API int rli_init(lua_State *L);
30
31#endif /* _ROCKLIB_IMG_H_ */