summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/rocklib_img.c
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/rocklib_img.c
parentbe801c61bbe95d75e4285da1982017da7fa1736b (diff)
downloadrockbox-733c20d5d3d696a60a7fd7098ca45cb545e78043.tar.gz
rockbox-733c20d5d3d696a60a7fd7098ca45cb545e78043.zip
lua move RLIMAGE to own file
Change-Id: Icd10e4c348deec7729d4a6e2bf1152e1dfc70243
Diffstat (limited to 'apps/plugins/lua/rocklib_img.c')
-rw-r--r--apps/plugins/lua/rocklib_img.c1452
1 files changed, 1452 insertions, 0 deletions
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