···2929 _draw.ellipse_filled
3030 _draw.ellipse_rect_filled
3131 _draw.ellipse_rect
3232- _draw.flood_fill
3332 _draw.hline
3433 _draw.image
3534 _draw.line
···3938 _draw.rect_filled
4039 _draw.rounded_rect
4140 _draw.rounded_rect_filled
4242- _draw.text
4341 _draw.vline
44424543]]
···5654 setmetatable(_draw, rocklib_image)
57555856 -- Internal Constants
5959- local _LCD = rb.lcd_framebuffer()
6060- local LCD_W, LCD_H = rb.LCD_WIDTH, rb.LCD_HEIGHT
6157 local BSAND = 8 -- blits color to dst if src <> 0
6258 local _NIL = nil -- nil placeholder
63596464- local _abs = math.abs
6060+6561 local _clear = rocklib_image.clear
6662 local _copy = rocklib_image.copy
6763 local _ellipse = rocklib_image.ellipse
6864 local _get = rocklib_image.get
6965 local _line = rocklib_image.line
7070- local _marshal = rocklib_image.marshal
7166 local _min = math.min
7267 local _newimg = rb.new_image
7373- local _points = rocklib_image.points
74687569 -- line
7670 _draw.line = function(img, x1, y1, x2, y2, color, bClip)
···8781 _line(img, x, y, _NIL, y + length, color, bClip)
8882 end
89839090- -- draws a non-filled figure based on points in t-points
9191- local function polyline(img, x, y, t_points, color, bClosed, bClip)
9292- if #t_points < 2 then error("not enough points", 3) end
9393-9494- local pt_first_last
9595-9696- if bClosed then
9797- pt_first_last = t_points[1]
9898- else
9999- pt_first_last = t_points[#t_points]
100100- end
101101-102102- for i = 1, #t_points, 1 do
103103- local pt1 = t_points[i]
104104-105105- local pt2 = t_points[i + 1] or pt_first_last-- first and last point
106106-8484+ -- draws a non-filled rect based on points in t-points
8585+ local function polyrect(img, x, y, t_points, color, bClip)
8686+ local pt_first_last = t_points[1]
8787+ local pt1, pt2
8888+ for i = 1, 4, 1 do
8989+ pt1 = t_points[i]
9090+ pt2 = t_points[i + 1] or pt_first_last-- first and last point
10791 _line(img, pt1[1] + x, pt1[2] + y, pt2[1] + x, pt2[2] + y, color, bClip)
10892 end
109109-11093 end
1119411295 -- rectangle
11396 local function rect(img, x, y, width, height, color, bClip)
11497 if width == 0 or height == 0 then return end
11598116116- polyline(img, x, y, {{0, 0}, {width, 0}, {width, height}, {0, height}}, color, true, bClip)
9999+ polyrect(img, x, y, {{0, 0}, {width, 0}, {width, height}, {0, height}}, color, bClip)
117100118101 end
119102···240223 _copy(dst, src, x, y, 1, 1, _NIL, _NIL, bClip)
241224 end
242225243243- -- floods an area of targetclr with fillclr x, y specifies the start seed
244244- _draw.flood_fill = function(img, x, y, targetclr, fillclr)
245245- -- scanline 4-way flood algorithm
246246- -- ^
247247- -- <--------x--->
248248- -- v
249249- -- check that target color doesn't = fill and the first point is target color
250250- if targetclr == fillclr or targetclr ~= _get(img, x, y, true) then return end
251251- local max_w = img:width()
252252- local max_h = img:height()
253253-254254- local qpt = {} -- FIFO queue
255255- -- rather than moving elements around in our FIFO queue
256256- -- for each read; increment 'qhead' by 2
257257- -- set both elements to nil and let the
258258- -- garbage collector worry about it
259259- -- for each write; increment 'qtail' by 2
260260- -- x coordinates are in odd indices while
261261- -- y coordinates are in even indices
262262-263263- local qtail = 0
264264-265265- local function check_ns(val, x, y)
266266- if targetclr == val then
267267- y = y - 1
268268- if targetclr == _get(img, x, y, true) then -- north
269269- qtail = qtail + 2
270270- qpt[qtail - 1] = x
271271- qpt[qtail] = y
272272- end
273273- y = y + 2
274274- if targetclr == _get(img, x, y, true) then -- south
275275- qtail = qtail + 2
276276- qpt[qtail - 1] = x
277277- qpt[qtail] = y
278278- end
279279- return fillclr
280280- end
281281- return _NIL -- signal marshal to stop
282282- end
283283-284284- local function seed_pt(x, y)
285285- -- should never hit max but make sure not to end early
286286- for qhead = 2, 0x40000000, 2 do
287287-288288- if targetclr == _get(img, x, y, true) then
289289- _marshal(img, x, y, 1, y, _NIL, _NIL, true, check_ns) -- west
290290- _marshal(img, x + 1, y, max_w, y, _NIL, _NIL, true, check_ns) -- east
291291- end
292292-293293- x = qpt[qhead - 1]
294294- qpt[qhead - 1] = _NIL
295295-296296- if not x then break end
297297-298298- y = qpt[qhead]
299299- qpt[qhead] = _NIL
300300- end
301301- end
302302-303303- seed_pt(x, y) -- Begin
304304- end -- flood_fill
305305-306306- -- draws a closed figure based on points in t_points
307307- _draw.polygon = function(img, x, y, t_points, color, fillcolor, bClip)
308308- if #t_points < 2 then error("not enough points", 3) end
309309-310310- if fillcolor then
311311- local x_min, x_max = 0, 0
312312- local y_min, y_max = 0, 0
313313- local w, h = 0, 0
314314- -- find boundries of polygon
315315- for i = 1, #t_points, 1 do
316316- local pt = t_points[i]
317317- if pt[1] < x_min then x_min = pt[1] end
318318- if pt[1] > x_max then x_max = pt[1] end
319319- if pt[2] < y_min then y_min = pt[2] end
320320- if pt[2] > y_max then y_max = pt[2] end
321321- end
322322- w = _abs(x_max) + _abs(x_min)
323323- h = _abs(y_max) + _abs(y_min)
324324- x_min = x_min - 2 -- leave a border to use flood_fill
325325- y_min = y_min - 2
326326-327327- local fill_img = _newimg(w + 3, h + 3)
328328- _clear(fill_img, 0x1)
329329-330330- for i = 1, #t_points, 1 do
331331- local pt1 = t_points[i]
332332- local pt2 = t_points[i + 1] or t_points[1]-- first and last point
333333- _line(fill_img, pt1[1] - x_min, pt1[2] - y_min,
334334- pt2[1]- x_min, pt2[2] - y_min, 0)
335335-336336- end
337337- _draw.flood_fill(fill_img, fill_img:width(), fill_img:height() , 0x1, 0x0)
338338- _copy(img, fill_img, x - 1, y - 1, _NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor)
339339- end
340340-341341- polyline(img, x, y, t_points, color, true, bClip)
342342- end
343343-344344- -- draw text onto image if width/height are supplied text is centered
345345- _draw.text = function(img, x, y, width, height, font, color, text)
346346- font = font or rb.FONT_UI
347347-348348- local opts = {x = 0, y = 0, width = LCD_W - 1, height = LCD_H - 1,
349349- font = font, drawmode = 3, fg_pattern = 0x1, bg_pattern = 0}
350350-351351- if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
352352- --vp.drawmode = bit.bxor(vp.drawmode, 4)
353353- opts.fg_pattern = 3 - opts.fg_pattern
354354- opts.bg_pattern = 3 - opts.bg_pattern
355355- end
356356- rb.set_viewport(opts)
357357-358358- local res, w, h = rb.font_getstringsize(text, font)
359359-360360- if not width then
361361- width = 0
362362- else
363363- width = (width - w) / 2
364364- end
365365-366366- if not height then
367367- height = 0
368368- else
369369- height = (height - h) / 2
370370- end
371371-372372- -- make a copy of the current screen for later
373373- local screen_img = _newimg(LCD_W, LCD_H)
374374- _copy(screen_img, _LCD)
375375-376376- -- check if the screen buffer is supplied image if so set img to the copy
377377- if img == _LCD then
378378- img = screen_img
379379- end
380380-381381- -- we will be printing the text to the screen then blitting into img
382382- rb.lcd_clear_display()
383383-384384- if w > LCD_W then -- text is too long for the screen do it in chunks
385385- local l = 1
386386- local resp, wp, hp
387387- local lenr = text:len()
388388-389389- while lenr > 1 do
390390- l = lenr
391391- resp, wp, hp = rb.font_getstringsize(text:sub(1, l), font)
392392-393393- while wp >= LCD_W and l > 1 do
394394- l = l - 1
395395- resp, wp, hp = rb.font_getstringsize(text:sub( 1, l), font)
396396- end
397397-398398- rb.lcd_putsxy(0, 0, text:sub(1, l))
399399- text = text:sub(l)
400400-401401- if x + width > img:width() or y + height > img:height() then
402402- break
403403- end
404404-405405- -- using the mask we made blit color into img
406406- _copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
407407- x = x + wp
408408- rb.lcd_clear_display()
409409- lenr = text:len()
410410- end
411411- else --w <= LCD_W
412412- rb.lcd_putsxy(0, 0, text)
413413-414414- -- using the mask we made blit color into img
415415- _copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
416416- end
417417-418418- _copy(_LCD, screen_img) -- restore screen
419419- rb.set_viewport() -- set viewport default
420420- return res, w, h
421421- end
422422-423226 -- expose internal functions to the outside through _draw table
424227 _draw.hline = hline
425228 _draw.vline = vline
426426- _draw.polyline = polyline
427229 _draw.rect = rect
428230 _draw.rounded_rect = rounded_rect
429231end -- _draw functions
+95
apps/plugins/lua/include_lua/draw_floodfill.lua
···11+--[[ Lua Floodfill function
22+/***************************************************************************
33+ * __________ __ ___.
44+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
55+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
66+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
77+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
88+ * \/ \/ \/ \/ \/
99+ * $Id$
1010+ *
1111+ * Copyright (C) 2017 William Wilgus
1212+ *
1313+ * This program is free software; you can redistribute it and/or
1414+ * modify it under the terms of the GNU General Public License
1515+ * as published by the Free Software Foundation; either version 2
1616+ * of the License, or (at your option) any later version.
1717+ *
1818+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1919+ * KIND, either express or implied.
2020+ *
2121+ ****************************************************************************/
2222+]]
2323+-- floods an area of targetclr with fillclr x, y specifies the start seed
2424+-- flood_fill(img, x, y, targetclr, fillclr)
2525+if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
2626+do
2727+2828+ local rocklib_image = getmetatable(rb.lcd_framebuffer())
2929+ local _NIL = nil -- nil placeholder
3030+ local _get = rocklib_image.get
3131+ local _line = rocklib_image.line
3232+ local _marshal = rocklib_image.marshal
3333+3434+ return function(img, x, y, targetclr, fillclr)
3535+ -- scanline 4-way flood algorithm
3636+ -- ^
3737+ -- <--------x--->
3838+ -- v
3939+ -- check that target color doesn't = fill and the first point is target color
4040+ if targetclr == fillclr or targetclr ~= _get(img, x, y, true) then return end
4141+ local max_w = img:width()
4242+ local max_h = img:height()
4343+4444+ local qpt = {} -- FIFO queue
4545+ -- rather than moving elements around in our FIFO queue
4646+ -- for each read; increment 'qhead' by 2
4747+ -- set both elements to nil and let the
4848+ -- garbage collector worry about it
4949+ -- for each write; increment 'qtail' by 2
5050+ -- x coordinates are in odd indices while
5151+ -- y coordinates are in even indices
5252+5353+ local qtail = 0
5454+5555+ local function check_ns(val, x, y)
5656+ if targetclr == val then
5757+ y = y - 1
5858+ if targetclr == _get(img, x, y, true) then -- north
5959+ qtail = qtail + 2
6060+ qpt[qtail - 1] = x
6161+ qpt[qtail] = y
6262+ end
6363+ y = y + 2
6464+ if targetclr == _get(img, x, y, true) then -- south
6565+ qtail = qtail + 2
6666+ qpt[qtail - 1] = x
6767+ qpt[qtail] = y
6868+ end
6969+ return fillclr
7070+ end
7171+ return _NIL -- signal marshal to stop
7272+ end
7373+7474+ local function seed_pt(x, y)
7575+ -- should never hit max but make sure not to end early
7676+ for qhead = 2, 0x40000000, 2 do
7777+7878+ if targetclr == _get(img, x, y, true) then
7979+ _marshal(img, x, y, 1, y, _NIL, _NIL, true, check_ns) -- west
8080+ _marshal(img, x + 1, y, max_w, y, _NIL, _NIL, true, check_ns) -- east
8181+ end
8282+8383+ x = qpt[qhead - 1]
8484+ qpt[qhead - 1] = _NIL
8585+8686+ if not x then break end
8787+8888+ y = qpt[qhead]
8989+ qpt[qhead] = _NIL
9090+ end
9191+ end
9292+9393+ seed_pt(x, y) -- Begin
9494+ end -- flood_fill
9595+end
+103
apps/plugins/lua/include_lua/draw_poly.lua
···11+--[[ Lua Poly Drawing functions
22+/***************************************************************************
33+ * __________ __ ___.
44+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
55+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
66+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
77+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
88+ * \/ \/ \/ \/ \/
99+ * $Id$
1010+ *
1111+ * Copyright (C) 2017 William Wilgus
1212+ *
1313+ * This program is free software; you can redistribute it and/or
1414+ * modify it under the terms of the GNU General Public License
1515+ * as published by the Free Software Foundation; either version 2
1616+ * of the License, or (at your option) any later version.
1717+ *
1818+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1919+ * KIND, either express or implied.
2020+ *
2121+ ****************************************************************************/
2222+]]
2323+2424+--[[ Exposed Functions
2525+ _poly.polygon
2626+ _poly.polyline
2727+]]
2828+2929+if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
3030+3131+local _poly = {} do
3232+ local BSAND = 8 -- blits color to dst if src <> 0
3333+ local _NIL = nil -- nil placeholder
3434+3535+ local _abs = math.abs
3636+ local _copy = rocklib_image.copy
3737+ local _line = rocklib_image.line
3838+ local flood_fill = require("draw_floodfill")
3939+4040+ -- draws a non-filled figure based on points in t-points
4141+ local function polyline(img, x, y, t_points, color, bClosed, bClip)
4242+ if #t_points < 2 then error("not enough points", 3) end
4343+4444+ local pt_first_last
4545+4646+ if bClosed then
4747+ pt_first_last = t_points[1]
4848+ else
4949+ pt_first_last = t_points[#t_points]
5050+ end
5151+5252+ for i = 1, #t_points, 1 do
5353+ local pt1 = t_points[i]
5454+5555+ local pt2 = t_points[i + 1] or pt_first_last-- first and last point
5656+5757+ _line(img, pt1[1] + x, pt1[2] + y, pt2[1] + x, pt2[2] + y, color, bClip)
5858+ end
5959+6060+ end
6161+6262+ -- draws a closed figure based on points in t_points
6363+ _poly.polygon = function(img, x, y, t_points, color, fillcolor, bClip)
6464+ if #t_points < 2 then error("not enough points", 3) end
6565+6666+ if fillcolor then
6767+ local x_min, x_max = 0, 0
6868+ local y_min, y_max = 0, 0
6969+ local w, h = 0, 0
7070+ -- find boundries of polygon
7171+ for i = 1, #t_points, 1 do
7272+ local pt = t_points[i]
7373+ if pt[1] < x_min then x_min = pt[1] end
7474+ if pt[1] > x_max then x_max = pt[1] end
7575+ if pt[2] < y_min then y_min = pt[2] end
7676+ if pt[2] > y_max then y_max = pt[2] end
7777+ end
7878+ w = _abs(x_max) + _abs(x_min)
7979+ h = _abs(y_max) + _abs(y_min)
8080+ x_min = x_min - 2 -- leave a border to use flood_fill
8181+ y_min = y_min - 2
8282+8383+ local fill_img = _newimg(w + 3, h + 3)
8484+ _clear(fill_img, 0x1)
8585+8686+ for i = 1, #t_points, 1 do
8787+ local pt1 = t_points[i]
8888+ local pt2 = t_points[i + 1] or t_points[1]-- first and last point
8989+ _line(fill_img, pt1[1] - x_min, pt1[2] - y_min,
9090+ pt2[1]- x_min, pt2[2] - y_min, 0)
9191+9292+ end
9393+ flood_fill(fill_img, fill_img:width(), fill_img:height() , 0x1, 0x0)
9494+ _copy(img, fill_img, x - 1, y - 1, _NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor)
9595+ end
9696+9797+ polyline(img, x, y, t_points, color, true, bClip)
9898+ end
9999+100100+ -- expose internal functions to the outside through _poly table
101101+ _poly.polyline = polyline
102102+end
103103+return _poly
+121
apps/plugins/lua/include_lua/draw_text.lua
···11+--[[ Lua Draw Text function
22+/***************************************************************************
33+ * __________ __ ___.
44+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
55+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
66+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
77+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
88+ * \/ \/ \/ \/ \/
99+ * $Id$
1010+ *
1111+ * Copyright (C) 2017 William Wilgus
1212+ *
1313+ * This program is free software; you can redistribute it and/or
1414+ * modify it under the terms of the GNU General Public License
1515+ * as published by the Free Software Foundation; either version 2
1616+ * of the License, or (at your option) any later version.
1717+ *
1818+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1919+ * KIND, either express or implied.
2020+ *
2121+ ****************************************************************************/
2222+]]
2323+-- draw text onto image if width/height are supplied text is centered
2424+2525+if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
2626+2727+do
2828+ -- Internal Constants
2929+ local rocklib_image = getmetatable(rb.lcd_framebuffer())
3030+ local _LCD = rb.lcd_framebuffer()
3131+ local LCD_W, LCD_H = rb.LCD_WIDTH, rb.LCD_HEIGHT
3232+ local BSAND = 8 -- blits color to dst if src <> 0
3333+ local _NIL = nil -- nil placeholder
3434+3535+ local _clear = rocklib_image.clear
3636+ local _copy = rocklib_image.copy
3737+ local _newimg = rb.new_image
3838+3939+4040+ return function(img, x, y, width, height, font, color, text)
4141+ font = font or rb.FONT_UI
4242+4343+ local opts = {x = 0, y = 0, width = LCD_W - 1, height = LCD_H - 1,
4444+ font = font, drawmode = 3, fg_pattern = 0x1, bg_pattern = 0}
4545+4646+ if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
4747+ --vp.drawmode = bit.bxor(vp.drawmode, 4)
4848+ opts.fg_pattern = 3 - opts.fg_pattern
4949+ opts.bg_pattern = 3 - opts.bg_pattern
5050+ end
5151+ rb.set_viewport(opts)
5252+5353+ local res, w, h = rb.font_getstringsize(text, font)
5454+5555+ if not width then
5656+ width = 0
5757+ else
5858+ width = (width - w) / 2
5959+ end
6060+6161+ if not height then
6262+ height = 0
6363+ else
6464+ height = (height - h) / 2
6565+ end
6666+6767+ -- make a copy of the current screen for later
6868+ --local screen_img = _newimg(LCD_W, LCD_H)
6969+ local screen_img = _newimg(LCD_W, h * 2)
7070+ _copy(screen_img, _LCD)
7171+7272+ -- check if the screen buffer is supplied image if so set img to the copy
7373+ if img == _LCD then
7474+ img = screen_img
7575+ end
7676+7777+ -- we will be printing the text to the screen then blitting into img
7878+ --rb.lcd_clear_display()
7979+ _clear(_LCD, opts.bg_pattern or 0, 1, 1, LCD_W, h * 2)
8080+8181+ if w > LCD_W then -- text is too long for the screen do it in chunks
8282+ local l = 1
8383+ local resp, wp, hp
8484+ local lenr = text:len()
8585+8686+ while lenr > 1 do
8787+ l = lenr
8888+ resp, wp, hp = rb.font_getstringsize(text:sub(1, l), font)
8989+9090+ while wp >= LCD_W and l > 1 do
9191+ l = l - 1
9292+ resp, wp, hp = rb.font_getstringsize(text:sub( 1, l), font)
9393+ end
9494+9595+ rb.lcd_putsxy(0, 0, text:sub(1, l))
9696+ text = text:sub(l)
9797+9898+ if x + width > img:width() or y + height > img:height() then
9999+ break
100100+ end
101101+102102+ -- using the mask we made blit color into img
103103+ _copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
104104+ x = x + wp
105105+ --rb.lcd_clear_display()
106106+ _clear(_LCD, opts.bg_pattern or 0, 1, 1, LCD_W, h * 2)
107107+108108+ lenr = text:len()
109109+ end
110110+ else --w <= LCD_W
111111+ rb.lcd_putsxy(0, 0, text)
112112+113113+ -- using the mask we made blit color into img
114114+ _copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
115115+ end
116116+117117+ _copy(_LCD, screen_img) -- restore screen
118118+ rb.set_viewport() -- set viewport default
119119+ return res, w, h
120120+ end
121121+end
-184
apps/plugins/lua/include_lua/image.lua
···23232424--[[ Exposed Functions
25252626- _img.save
2726 _img.search
2827 _img.rotate
2928 _img.resize
···155154 _marshal(r_img, 1, 1, dw, dh, _NIL, _NIL, false, rot_trans)
156155 return r_img
157156 end
158158-159159- -- saves img to file: name
160160- _img.save = function(img, name)
161161- -- bmp saving derived from rockbox - screendump.c
162162- -- bitdepth is limited by the device
163163- -- eg. device displays greyscale, rgb images are saved greyscale
164164- local file
165165- local bbuffer = {} -- concat buffer for s_bytes
166166- local fbuffer = {} -- concat buffer for file writes, reused
167167-168168- local function s_bytesLE(bits, value)
169169- -- bits must be multiples of 8 (sizeof byte)
170170- local byte
171171- local nbytes = bit.rshift(bits, 3)
172172- for b = 1, nbytes do
173173- if value > 0 then
174174- byte = value % 256
175175- value = (value - byte) / 256
176176- else
177177- byte = 0
178178- end
179179- bbuffer[b] = string.char(byte)
180180- end
181181- return table.concat(bbuffer, _NIL, 1, nbytes)
182182- end
183183-184184- local function s_bytesBE(bits, value)
185185- -- bits must be multiples of 8 (sizeof byte)
186186- local byte
187187- local nbytes = bit.rshift(bits, 3)
188188- for b = nbytes, 1, -1 do
189189- if value > 0 then
190190- byte = value % 256
191191- value = (value - byte) / 256
192192- else
193193- byte = 0
194194- end
195195- bbuffer[b] = string.char(byte)
196196- end
197197- return table.concat(bbuffer, _NIL, 1, nbytes)
198198- end
199199-200200- local cmp = {["r"] = function(c) return bit.band(bit.rshift(c, 16), 0xFF) end,
201201- ["g"] = function(c) return bit.band(bit.rshift(c, 08), 0xFF) end,
202202- ["b"] = function(c) return bit.band(c, 0xFF) end}
203203-204204- local function bmp_color(color)
205205- return s_bytesLE(8, cmp.b(color))..
206206- s_bytesLE(8, cmp.g(color))..
207207- s_bytesLE(8, cmp.r(color))..
208208- s_bytesLE(8, 0) .. ""
209209- end -- c_cmp(color, c.r))
210210-211211- local function bmp_color_mix(c1, c2, num, den)
212212- -- mixes c1 and c2 as ratio of numerator / denominator
213213- -- used 2x each save results
214214- local bc1, gc1, rc1 = cmp.b(c1), cmp.g(c1), cmp.r(c1)
215215-216216- return s_bytesLE(8, cmp.b(c2) - bc1 * num / den + bc1)..
217217- s_bytesLE(8, cmp.g(c2) - gc1 * num / den + gc1)..
218218- s_bytesLE(8, cmp.r(c2) - rc1 * num / den + rc1)..
219219- s_bytesLE(8, 0) .. ""
220220- end
221221-222222- local w, h = img:width(), img:height()
223223- local depth = tonumber(img:__tostring(6)) -- RLI_INFO_DEPTH = 0x6
224224- local format = tonumber(img:__tostring(7)) -- RLI_INFO_FORMAT = 0x7
225225-226226- local bpp, bypl -- bits per pixel, bytes per line
227227- -- bypl, pad rows to a multiple of 4 bytes
228228- if depth <= 4 then
229229- bpp = 8 -- 256 color image
230230- bypl = (w + 3)
231231- elseif depth <= 16 then
232232- bpp = 16
233233- bypl = (w * 2 + 3)
234234- elseif depth <= 24 then
235235- bpp = 24
236236- bypl = (w * 3 + 3)
237237- else
238238- bpp = 32
239239- bypl = (w * 4 + 3)
240240- end
241241-242242- local linebytes = bit.band(bypl, bit.bnot(3))
243243-244244- local bytesperpixel = bit.rshift(bpp, 3)
245245- local headersz = 54
246246- local imgszpad = h * linebytes
247247-248248- local compression, n_colors = 0, 0
249249- local h_ppm, v_ppm = 0x00000EC4, 0x00000EC4 --Pixels Per Meter ~ 96 dpi
250250-251251- if depth == 16 then
252252- compression = 3 -- BITFIELDS
253253- n_colors = 3
254254- elseif depth <= 8 then
255255- n_colors = bit.lshift(1, depth)
256256- end
257257-258258- headersz = headersz + (4 * n_colors)
259259-260260- file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag
261261-262262- if not file then
263263- rb.splash(rb.HZ, "Error opening /" .. name)
264264- return
265265- end
266266- -- create a bitmap header 'rope' with image details -- concatenated at end
267267- local bmpheader = fbuffer
268268-269269- bmpheader[01] = "BM"
270270- bmpheader[02] = s_bytesLE(32, headersz + imgszpad)
271271- bmpheader[03] = "\0\0\0\0" -- WORD reserved 1 & 2
272272- bmpheader[04] = s_bytesLE(32, headersz) -- BITMAPCOREHEADER size
273273- bmpheader[05] = s_bytesLE(32, 40) -- BITMAPINFOHEADER size
274274-275275- bmpheader[06] = s_bytesLE(32, w)
276276- bmpheader[07] = s_bytesLE(32, h)
277277- bmpheader[08] = "\1\0" -- WORD color planes ALWAYS 1
278278- bmpheader[09] = s_bytesLE(16, bpp) -- bits/pixel
279279- bmpheader[10] = s_bytesLE(32, compression)
280280- bmpheader[11] = s_bytesLE(32, imgszpad)
281281- bmpheader[12] = s_bytesLE(32, h_ppm) -- biXPelsPerMeter
282282- bmpheader[13] = s_bytesLE(32, v_ppm) -- biYPelsPerMeter
283283- bmpheader[14] = s_bytesLE(32, n_colors)
284284- bmpheader[15] = s_bytesLE(32, n_colors)
285285-286286- -- Color Table (#n_colors entries)
287287- if depth == 1 then -- assuming positive display
288288- bmpheader[#bmpheader + 1] = bmp_color(0xFFFFFF)
289289- bmpheader[#bmpheader + 1] = bmp_color(0x0)
290290- elseif depth == 2 then
291291- bmpheader[#bmpheader + 1] = bmp_color(0xFFFFFF)
292292- bmpheader[#bmpheader + 1] = bmp_color_mix(0xFFFFFF, 0, 1, 3)
293293- bmpheader[#bmpheader + 1] = bmp_color_mix(0xFFFFFF, 0, 2, 3)
294294- bmpheader[#bmpheader + 1] = bmp_color(0x0)
295295- elseif depth == 16 then
296296- if format == 555 then
297297- -- red bitfield mask
298298- bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x00007C00)
299299- -- green bitfield mask
300300- bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x000003E0)
301301- -- blue bitfield mask
302302- bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x0000001F)
303303- else --565
304304- -- red bitfield mask
305305- bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x0000F800)
306306- -- green bitfield mask
307307- bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x000007E0)
308308- -- blue bitfield mask
309309- bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x0000001F)
310310- end
311311- end
312312-313313- file:write(table.concat(fbuffer))-- write the header to the file now
314314- for i=1, #fbuffer do fbuffer[i] = _NIL end -- reuse table
315315-316316- local imgdata = fbuffer
317317- -- pad rows to a multiple of 4 bytes
318318- local bytesleft = linebytes - (bytesperpixel * w)
319319- local t_data = {}
320320- local fs_bytes_E = s_bytesLE -- default save in Little Endian
321321-322322- if format == 3553 then -- RGB565SWAPPED
323323- fs_bytes_E = s_bytesBE -- Saves in Big Endian
324324- end
325325-326326- -- Bitmap lines start at bottom unless biHeight is negative
327327- for point in _points(img, 1, h, w + bytesleft, 1) do
328328- imgdata[#imgdata + 1] = fs_bytes_E(bpp, point or 0)
329329-330330- if #fbuffer >= 31 then -- buffered write, increase # for performance
331331- file:write(table.concat(fbuffer))
332332- for i=1, #fbuffer do fbuffer[i] = _NIL end -- reuse table
333333- end
334334-335335- end
336336- file:write(table.concat(fbuffer)) --write leftovers to file
337337- fbuffer = _NIL
338338-339339- file:close()
340340- end -- save(img, name)
341157342158 --searches an image for target color
343159 _img.search = function(img, x1, y1, x2, y2, targetclr, variation, stepx, stepy)
+215
apps/plugins/lua/include_lua/image_save.lua
···11+--[[ Lua Image save
22+/***************************************************************************
33+ * __________ __ ___.
44+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
55+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
66+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
77+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
88+ * \/ \/ \/ \/ \/
99+ * $Id$
1010+ *
1111+ * Copyright (C) 2017 William Wilgus
1212+ *
1313+ * This program is free software; you can redistribute it and/or
1414+ * modify it under the terms of the GNU General Public License
1515+ * as published by the Free Software Foundation; either version 2
1616+ * of the License, or (at your option) any later version.
1717+ *
1818+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1919+ * KIND, either express or implied.
2020+ *
2121+ ****************************************************************************/
2222+]]
2323+-- save(img, path/name)
2424+-- bmp saving derived from rockbox - screendump.c
2525+-- bitdepth is limited by the device
2626+-- eg. device displays greyscale, rgb images are saved greyscale
2727+if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
2828+2929+do
3030+ local rocklib_image = getmetatable(rb.lcd_framebuffer())
3131+3232+ -- internal constants
3333+ local _NIL = nil -- _NIL placeholder
3434+ local _points = rocklib_image.points
3535+3636+ -- saves img to file: name
3737+ return function(img, name)
3838+ local file
3939+ local bbuffer = {} -- concat buffer for s_bytes
4040+ local fbuffer = {} -- concat buffer for file writes, reused
4141+4242+ local function s_bytesLE(bits, value)
4343+ -- bits must be multiples of 8 (sizeof byte)
4444+ local byte
4545+ local nbytes = bit.rshift(bits, 3)
4646+ for b = 1, nbytes do
4747+ if value > 0 then
4848+ byte = value % 256
4949+ value = (value - byte) / 256
5050+ else
5151+ byte = 0
5252+ end
5353+ bbuffer[b] = string.char(byte)
5454+ end
5555+ return table.concat(bbuffer, _NIL, 1, nbytes)
5656+ end
5757+5858+ local function s_bytesBE(bits, value)
5959+ -- bits must be multiples of 8 (sizeof byte)
6060+ local byte
6161+ local nbytes = bit.rshift(bits, 3)
6262+ for b = nbytes, 1, -1 do
6363+ if value > 0 then
6464+ byte = value % 256
6565+ value = (value - byte) / 256
6666+ else
6767+ byte = 0
6868+ end
6969+ bbuffer[b] = string.char(byte)
7070+ end
7171+ return table.concat(bbuffer, _NIL, 1, nbytes)
7272+ end
7373+7474+ local cmp = {["r"] = function(c) return bit.band(bit.rshift(c, 16), 0xFF) end,
7575+ ["g"] = function(c) return bit.band(bit.rshift(c, 08), 0xFF) end,
7676+ ["b"] = function(c) return bit.band(c, 0xFF) end}
7777+7878+ local function bmp_color(color)
7979+ return s_bytesLE(8, cmp.b(color))..
8080+ s_bytesLE(8, cmp.g(color))..
8181+ s_bytesLE(8, cmp.r(color))..
8282+ s_bytesLE(8, 0) .. ""
8383+ end -- c_cmp(color, c.r))
8484+8585+ local function bmp_color_mix(c1, c2, num, den)
8686+ -- mixes c1 and c2 as ratio of numerator / denominator
8787+ -- used 2x each save results
8888+ local bc1, gc1, rc1 = cmp.b(c1), cmp.g(c1), cmp.r(c1)
8989+9090+ return s_bytesLE(8, cmp.b(c2) - bc1 * num / den + bc1)..
9191+ s_bytesLE(8, cmp.g(c2) - gc1 * num / den + gc1)..
9292+ s_bytesLE(8, cmp.r(c2) - rc1 * num / den + rc1)..
9393+ s_bytesLE(8, 0) .. ""
9494+ end
9595+9696+ local w, h = img:width(), img:height()
9797+ local depth = tonumber(img:__tostring(6)) -- RLI_INFO_DEPTH = 0x6
9898+ local format = tonumber(img:__tostring(7)) -- RLI_INFO_FORMAT = 0x7
9999+100100+ local bpp, bypl -- bits per pixel, bytes per line
101101+ -- bypl, pad rows to a multiple of 4 bytes
102102+ if depth <= 4 then
103103+ bpp = 8 -- 256 color image
104104+ bypl = (w + 3)
105105+ elseif depth <= 16 then
106106+ bpp = 16
107107+ bypl = (w * 2 + 3)
108108+ elseif depth <= 24 then
109109+ bpp = 24
110110+ bypl = (w * 3 + 3)
111111+ else
112112+ bpp = 32
113113+ bypl = (w * 4 + 3)
114114+ end
115115+116116+ local linebytes = bit.band(bypl, bit.bnot(3))
117117+118118+ local bytesperpixel = bit.rshift(bpp, 3)
119119+ local headersz = 54
120120+ local imgszpad = h * linebytes
121121+122122+ local compression, n_colors = 0, 0
123123+ local h_ppm, v_ppm = 0x00000EC4, 0x00000EC4 --Pixels Per Meter ~ 96 dpi
124124+125125+ if depth == 16 then
126126+ compression = 3 -- BITFIELDS
127127+ n_colors = 3
128128+ elseif depth <= 8 then
129129+ n_colors = bit.lshift(1, depth)
130130+ end
131131+132132+ headersz = headersz + (4 * n_colors)
133133+134134+ file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag
135135+136136+ if not file then
137137+ rb.splash(rb.HZ, "Error opening /" .. name)
138138+ return
139139+ end
140140+ -- create a bitmap header 'rope' with image details -- concatenated at end
141141+ local bmpheader = fbuffer
142142+143143+ bmpheader[01] = "BM"
144144+ bmpheader[02] = s_bytesLE(32, headersz + imgszpad)
145145+ bmpheader[03] = "\0\0\0\0" -- WORD reserved 1 & 2
146146+ bmpheader[04] = s_bytesLE(32, headersz) -- BITMAPCOREHEADER size
147147+ bmpheader[05] = s_bytesLE(32, 40) -- BITMAPINFOHEADER size
148148+149149+ bmpheader[06] = s_bytesLE(32, w)
150150+ bmpheader[07] = s_bytesLE(32, h)
151151+ bmpheader[08] = "\1\0" -- WORD color planes ALWAYS 1
152152+ bmpheader[09] = s_bytesLE(16, bpp) -- bits/pixel
153153+ bmpheader[10] = s_bytesLE(32, compression)
154154+ bmpheader[11] = s_bytesLE(32, imgszpad)
155155+ bmpheader[12] = s_bytesLE(32, h_ppm) -- biXPelsPerMeter
156156+ bmpheader[13] = s_bytesLE(32, v_ppm) -- biYPelsPerMeter
157157+ bmpheader[14] = s_bytesLE(32, n_colors)
158158+ bmpheader[15] = s_bytesLE(32, n_colors)
159159+160160+ -- Color Table (#n_colors entries)
161161+ if depth == 1 then -- assuming positive display
162162+ bmpheader[#bmpheader + 1] = bmp_color(0xFFFFFF)
163163+ bmpheader[#bmpheader + 1] = bmp_color(0x0)
164164+ elseif depth == 2 then
165165+ bmpheader[#bmpheader + 1] = bmp_color(0xFFFFFF)
166166+ bmpheader[#bmpheader + 1] = bmp_color_mix(0xFFFFFF, 0, 1, 3)
167167+ bmpheader[#bmpheader + 1] = bmp_color_mix(0xFFFFFF, 0, 2, 3)
168168+ bmpheader[#bmpheader + 1] = bmp_color(0x0)
169169+ elseif depth == 16 then
170170+ if format == 555 then
171171+ -- red bitfield mask
172172+ bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x00007C00)
173173+ -- green bitfield mask
174174+ bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x000003E0)
175175+ -- blue bitfield mask
176176+ bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x0000001F)
177177+ else --565
178178+ -- red bitfield mask
179179+ bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x0000F800)
180180+ -- green bitfield mask
181181+ bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x000007E0)
182182+ -- blue bitfield mask
183183+ bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x0000001F)
184184+ end
185185+ end
186186+187187+ file:write(table.concat(fbuffer))-- write the header to the file now
188188+ for i=1, #fbuffer do fbuffer[i] = _NIL end -- reuse table
189189+190190+ local imgdata = fbuffer
191191+ -- pad rows to a multiple of 4 bytes
192192+ local bytesleft = linebytes - (bytesperpixel * w)
193193+ local t_data = {}
194194+ local fs_bytes_E = s_bytesLE -- default save in Little Endian
195195+196196+ if format == 3553 then -- RGB565SWAPPED
197197+ fs_bytes_E = s_bytesBE -- Saves in Big Endian
198198+ end
199199+200200+ -- Bitmap lines start at bottom unless biHeight is negative
201201+ for point in _points(img, 1, h, w + bytesleft, 1) do
202202+ imgdata[#imgdata + 1] = fs_bytes_E(bpp, point or 0)
203203+204204+ if #fbuffer >= 31 then -- buffered write, increase # for performance
205205+ file:write(table.concat(fbuffer))
206206+ for i=1, #fbuffer do fbuffer[i] = _NIL end -- reuse table
207207+ end
208208+209209+ end
210210+ file:write(table.concat(fbuffer)) --write leftovers to file
211211+ fbuffer = _NIL
212212+213213+ file:close()
214214+ end -- save(img, name)
215215+end
···11+--[[
22+ __________ __ ___.
33+ Open \______ \ ____ ____ | | _\_ |__ _______ ___
44+ Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
55+ Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
66+ Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
77+ \/ \/ \/ \/ \/
88+ $Id$
99+ Example Lua RBIMAGE script
1010+ Copyright (C) 2009 by Maurus Cuelenaere -- some prior work
1111+ Copyright (C) 2017 William Wilgus
1212+ This program is free software; you can redistribute it and/or
1313+ modify it under the terms of the GNU General Public License
1414+ as published by the Free Software Foundation; either version 2
1515+ of the License, or (at your option) any later version.
1616+ This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1717+ KIND, either express or implied.
1818+]]--
1919+2020+require("actions") -- Contains rb.actions & rb.contexts
2121+-- require("buttons") -- Contains rb.buttons -- not needed for this example
2222+2323+local _math = require("math_ex") -- missing math sine cosine, sqrt, clamp functions
2424+local _timer = require("timer")
2525+local _clr = require("color") -- clrset, clrinc provides device independent colors
2626+local _lcd = require("lcd") -- lcd helper functions
2727+local _print = require("print") -- advanced text printing
2828+local _img = require("image") -- image manipulation save, rotate, resize, tile, new, load
2929+local _img_save = require("image_save")
3030+local _blit = require("blit") -- handy list of blit operations
3131+local _draw = require("draw") -- draw all the things (primitives)
3232+local _draw_floodfill = require("draw_floodfill")
3333+local _draw_text = require("draw_text")
3434+3535+--local scrpath = rb.current_path()"
3636+3737+--package.path = scrpath .. "/?.lua;" .. package.path --add lua_scripts directory to path
3838+3939+require("printmenu") --menu
4040+4141+--[[ RBIMAGE library functions
4242+NOTE!! on x, y coordinates + width & height
4343+----------------------------------------------
4444+When making a new image you specify the width and height say (10, 20).
4545+Intutively (at least to me) (0,0) (offset addressing) would reference the first
4646+pixel (top left) and the last pixel (bottom, right) would be (w-1, h-1) or (9, 19)
4747+but the original rbimage library (like lua) uses 1-based arrays (ordinal addressing)
4848+for the image data so the first pixel is (1,1) and the last pixel is (w, h)
4949+this presents a bit of a problem when interfacing with the internal rockbox
5050+functions as you must remeber to convert all lua coordinates to 0 based coordinates.
5151+I have opted to not change this in the name of compatibility with old scripts
5252+5353+NOTE2!! on width & height versus offset_x & offset_y :copy()
5454+------------------------------------------------------------------------
5555+The only place where RBIMAGE deviates from (ordinal addressing) is in the blit
5656+/ copy function sx, sy and dx, dy still refer to the same pixel as all the
5757+other functions but offset_x, and offset_y are zero based
5858+Example (assuming same sized images)
5959+dst:copy(src, 1,1, 1,1, 0, 0)
6060+would copy the first pixel from src to the first pixel of dst
6161+dst:copy(src, 1,1, 1,1, dst:width()-1, dst:height()-1) would copy all of src to dst
6262+this might be a bit confusing but makes reversing images (negative offsets) easier.
6363+Since offsets are auto calculated if empty or out of range you could call it as
6464+dst:copy(src, 1,1, 1,1, dst:width(), dst:height()) or even
6565+dst:copy(src, 1,1, 1,1) if you prefered and get the same result
6666+]]
6767+6868+--'CONSTANTS' (in lua there really is no such thing as all vars are mutable)
6969+--------------------------------------------------------
7070+--colors for fg/bg ------------------------
7171+local WHITE = _clr.set(-1, 255, 255, 255)
7272+local BLACK = _clr.set(0, 0, 0, 0)
7373+local RED = _clr.set(WHITE, 255)
7474+local GREEN = _clr.set(WHITE, 0, 255)
7575+local BLUE = _clr.set(WHITE, 0, 0, 255)
7676+-------------------------------------------
7777+local clrs
7878+local CANCEL_BUTTON = rb.actions.PLA_CANCEL
7979+8080+-- EXAMPLES ---------------------------------------------------------------------- EXAMPLES---------------------------------------------------------------------
8181+function my_blit(dst_val, dx, dy, src_val, sx, sy)
8282+ -- user defined blit operation
8383+ --this function gets called for every pixel you specify
8484+ --you may change pixels in both the source and dest image
8585+ --return nil to stop early
8686+8787+ if _lcd.DEPTH < 2 then
8888+ return src_val
8989+ end
9090+9191+ if dst_val == WHITE and bit.band(dx, 1) == 1 and bit.band(dy, 1) == 1 then
9292+ return BLACK;
9393+ elseif dst_val == WHITE then
9494+ return src_val
9595+ end
9696+9797+ return dst_val
9898+end
9999+100100+function create_logo()
101101+ --[[creates a small logo from data array]]
102102+103103+ -- moves scope of white and black from global to local
104104+ local WHITE = WHITE
105105+ local BLACK = BLACK
106106+107107+ --[[small array with 16 bits of data per element (16-bits in y direction)
108108+ while the number of elements (32) is the x direction.
109109+ in other words 16 rows X 32 columns, totally abritrary actually
110110+ you could easily rotate or flip it by changing the for loops below ]]
111111+ local logo = {0xFFFF, 0xFFFF, 0x8001, 0x8001, 0x9E7F, 0x9E7F, 0x9E7F, 0x9E7F,
112112+ 0x9E3F, 0x9E3F, 0x804F, 0x804F, 0xC0E1, 0xC0F1, 0xFFFD, 0xFFFF,
113113+ 0xFFFF, 0xFFFF, 0x8001, 0x8001, 0xFF01, 0xFF01, 0xFEFB, 0xFEFB,
114114+ 0xFDFD, 0xFDFD, 0xFDFD, 0xFCF9, 0xFE03, 0xFE03, 0xFFFF, 0xFFFF}
115115+116116+ local img, img1, img2, img3
117117+118118+ img = _img.new(_lcd.W, _lcd.H)
119119+ img:clear(BLACK) --[[always clear an image after you create it if you
120120+ intend to do any thing besides copy something
121121+ entirely to it as there is no guarantee what
122122+ state the data within is in, it could be all
123123+ 0's or it could be every digit of PI ]]
124124+125125+ -- check for an error condition bail if error
126126+ if(not img or not logo) then
127127+ return nil
128128+ end
129129+130130+ local logosz = table.getn(logo)
131131+ local bits = 16 -- each element contains 16 pixels
132132+ local data = 0
133133+134134+ for i=1, math.min(logosz, _lcd.W) do
135135+ for j=0, math.min(bits, _lcd.H) do
136136+137137+ if bit.band(bit.lshift(1, bits - j), logo[i]) > 0 then
138138+ data = WHITE
139139+ else
140140+ data = BLACK
141141+ end
142142+ -- Set the pixel at position i, j+1 to the copied data
143143+ img:set(i, j + 1, data)
144144+ end
145145+ end
146146+147147+ -- make a new image the size of our generated logo
148148+ img1 = _img.new(logosz + 1, bits + 1)
149149+150150+ -- img.copy(dest, source, dx, dy, sx, sy, [w, h])
151151+ img1:copy(img, 1, 1, 1, 1)
152152+153153+ --[[lua does auto garbage collection, but it is still
154154+ a good idea to set large items to nil when done anyways]]
155155+ img = nil
156156+157157+ local sl -- new image size
158158+ if _lcd.W < _lcd.H then
159159+ sl = _lcd.W / 3
160160+ else
161161+ sl = _lcd.H / 3
162162+ end
163163+164164+ -- make sl always even by subtracting 1 if needed
165165+ sl = bit.band(sl, bit.bnot(1))
166166+ if sl < 16 then
167167+ sl = 16
168168+ end
169169+170170+ img2 = _img.new(sl, sl)
171171+ --img2:clear(BLACK) -- doesn't need cleared since we copy to it entirely
172172+173173+ --[[we are going to resize the image since the data supplied is 32 x 16
174174+ pixels its really tiny on most screens]]
175175+ _img.resize(img2, img1)
176176+177177+ img1 = nil
178178+179179+ img3 = _img.new(sl, sl)
180180+ img3:clear(BLACK)
181181+182182+ if IS_COLOR_TARGET == true then
183183+ local c_yellow = _clr.set(WHITE, 0xFC, 0xC0, 0x00)
184184+ local c_grey = _clr.set(WHITE, 0xD4, 0xE3, 0xF3)
185185+ local c_dkgrey = _clr.set(WHITE, 0xB4, 0xC3, 0xD3)
186186+ -- if dest pixel == source pixel make dest pixel yellow
187187+ img3:copy(img2, 1, 1, 1, 1, nil, nil, false, _blit.BDEQS, c_yellow)
188188+ -- xor src pixel to dest pixel if both are 0 or both are 1 dest = 0
189189+ img2:copy(img3, 1, 1, 2, 1, nil, nil, false, _blit.BXOR)
190190+ -- if dest pixel color > src pixel color copy grey to dest
191191+ img2:copy(img3, 1, 1, 1, 1, nil, nil, false, _blit.BDGTS, c_grey)
192192+ -- set img3 to grey
193193+ img3:clear(c_dkgrey)
194194+ end
195195+196196+ -- make a WHITE square in the middle
197197+198198+ img3:clear(WHITE, 4,4, img3:width() - 3, img3:height() - 3)
199199+200200+ img3:copy(img2, 1, 1, 1, 1, nil, nil, false, _blit.CUSTOM, my_blit)
201201+ img2 = nil
202202+ _img_save(img3, "pix.bmp")
203203+ return img3
204204+end -- copy_logo
205205+206206+-- draws an olive erm ball and returns it
207207+function create_ball()
208208+ local sl -- image size
209209+ if _lcd.W < _lcd.H then
210210+ sl = _lcd.W / 5
211211+ else
212212+ sl = _lcd.H / 5
213213+ end
214214+215215+ -- make sl always even by subtracting 1 if needed
216216+ sl = bit.band(sl, bit.bnot(1))
217217+ if sl < 16 then
218218+ sl = 16
219219+ end
220220+ local img = _img.new(sl, sl)
221221+ img:clear(0)
222222+ _draw.circle_filled(img, sl/2, sl/2, sl/2 - 1, _clr.set(-1, 255), _clr.set(0, 100))
223223+ _draw.circle_filled(img, sl/3, sl/3, sl/10, _clr.set(-1, 255, 0, 0))
224224+ return img
225225+end
226226+227227+-- bounces img around on screen
228228+function bounce_image(img)
229229+ local timer = _timer() -- creates a new timer -- saves index
230230+ local wait
231231+ -- make a copy of the current screen for later
232232+ local screen_img = _lcd:duplicate()
233233+234234+ local img_sqy = _img.new(img:width() + img:width() / 8, img:height())
235235+ local img_sqx = _img.new(img:width(), img:height() + img:height() / 8)
236236+ _img.resize(img_sqy, img)
237237+ _img.resize(img_sqx, img)
238238+239239+ -- moves definition of CANCEL_BUTTON from global to local
240240+ local CANCEL_BUTTON = CANCEL_BUTTON
241241+--------------------------------------------------------
242242+ local imgn = img
243243+ local hold = 0
244244+ local sx = 1 -- starting x
245245+ local sy = 1 -- starting y
246246+247247+ local ex = _lcd.W - img:width() - 2
248248+ local ey = _lcd.H - img:width() - 2
249249+250250+ -- threshold resets speed, inverts image
251251+ local tx = ex / 5
252252+ local ty = ey / 5
253253+254254+ local last_x = sx
255255+ local last_y = sy
256256+257257+ local x = sx
258258+ local y = sy
259259+260260+ local dx = 1
261261+ local dy = 1
262262+ -- negative width\height cause the image to be drawn from the opposite end
263263+ local fx = _lcd.W
264264+ local fy = _lcd.H
265265+266266+ local function invert_images()
267267+ img:invert();
268268+ img_sqx:invert()
269269+ img_sqy:invert()
270270+ end
271271+272272+ local loops = (_lcd.W * _lcd.H) / 2
273273+ while (loops > 0) do
274274+275275+ if IS_COLOR_TARGET then
276276+ if bit.band(loops, 128) == 128 then
277277+ _lcd:copy(imgn, x, y, 1, 1, fx, fy, false, _blit.BOR)
278278+ _lcd:copy(screen_img, x, y, x, y, imgn:width(), imgn:height(),
279279+ false, _blit.BDEQC, imgn:get(1,1))
280280+ else
281281+ _lcd:copy(imgn, x, y, 1, 1, fx, fy, false, _blit.BSNEC, imgn:get(1,1))
282282+ end
283283+ else
284284+ local blitop
285285+286286+ if imgn:get(1,1) ~= 0 then
287287+ blitop = _blit.BSNOT
288288+ else
289289+ blitop = _blit.BXOR
290290+ end
291291+292292+ _lcd:copy(imgn, x, y, 1, 1, fx, fy, false, blitop, WHITE)
293293+ end
294294+295295+ if hold < 1 then
296296+ imgn = img
297297+ else
298298+ hold = hold - 1
299299+ end
300300+ _lcd:update()
301301+302302+ x = x + dx
303303+ y = y + dy
304304+305305+ if y >= ey or y <= sy then
306306+ dy = (-dy)
307307+ fy = (-fy)
308308+ imgn = img_sqy
309309+ hold = 3
310310+ if dx < 0 and y < 10 then
311311+ dx = dx - 5
312312+ end
313313+ if dx > tx then
314314+ dx = 5;
315315+ dy = 5;
316316+ invert_images()
317317+ end
318318+ end
319319+320320+ if x >= ex or x <= sx then
321321+ dx = (-dx)
322322+ fx = (-fx)
323323+ imgn = img_sqx
324324+ hold = 3
325325+ if dy < 0 and x < 10 then
326326+ dy = dy - 5
327327+ end
328328+ if dy > ty then
329329+ dx = 5;
330330+ dy = 5;
331331+ invert_images()
332332+ end
333333+ end
334334+335335+ x = _math.clamp(x, sx, ex)
336336+ y = _math.clamp(y, sy, ey)
337337+338338+ -- copy original image back to screen
339339+ _lcd:copy(screen_img)
340340+341341+ loops = loops -1
342342+343343+ wait = timer:check(true) --checks timer and updates last time
344344+ if wait >= 5 then
345345+ wait = 0
346346+ elseif wait < 5 then
347347+ wait = 5 - wait
348348+ end
349349+ -- 0 = timeout immediately
350350+ -- ( -1 would be never timeout, and >0 is amount of 'ticks' before timeout)
351351+ if rb.get_plugin_action(wait) == CANCEL_BUTTON then
352352+ break;
353353+ end
354354+ end
355355+356356+ timer:stop() -- destroys timer, also returns time since last time
357357+358358+ -- leave the screen how we found it
359359+ _lcd:copy(screen_img)
360360+end -- image_bounce
361361+362362+-- draws a gradient using available colors
363363+function draw_gradient(img, x, y, w, h, direction, clrs)
364364+ x = x or 1
365365+ y = y or 1
366366+ w = w or img:width() - x
367367+ h = h or img:height() - y
368368+ local zstep = 0
369369+ local step = 1
370370+ if IS_COLOR_TARGET == true then -- Only do this when we're on a color target
371371+ local z = 1
372372+ local c = 1
373373+ clrs = clrs or {255,255,255}
374374+ local function gradient_rgb(p, c1, c2)
375375+ -- tried squares of difference but blends were very abrupt
376376+ local r = c1.r + (p * (c2.r - c1.r) / 10500)
377377+ local g = c1.g + (p * (c2.g - c1.g) / 10000)
378378+ local b = c1.b + (p * (c2.b - c1.b) / 09999)
379379+ return _clr.set(nil, r, g, b)
380380+ end
381381+ local function check_z()
382382+ if z > 10000 and c < #clrs - 1 then
383383+ z = 1
384384+ c = c + 1
385385+ elseif z > 10000 then
386386+ z = 10000
387387+ end
388388+ end
389389+ if direction == "V" then
390390+ zstep = math.max(1, (10000 / ((w - 1) / (#clrs - 1)) + bit.band(#clrs, 1)))
391391+ for i=x, w + x do
392392+ check_z()
393393+ _draw.vline(img, i, y, h, gradient_rgb(z, clrs[c], clrs[c + 1]))
394394+ z = z + zstep
395395+ end
396396+ else
397397+ zstep = math.max(1, (10000 / ((h - 1) / (#clrs - 1)) + bit.band(#clrs, 1)))
398398+ for j=y, h + y do
399399+ check_z()
400400+ _draw.hline(img, x, j, w, gradient_rgb(z, clrs[c], clrs[c + 1]))
401401+ z = z + zstep
402402+ end
403403+ end
404404+ else -- not a color target but might be greyscale
405405+ local clr = _clr.set(-1)
406406+ for i=x, w + x do
407407+ for j=y, h + y do
408408+ -- Set the pixel at position i, j to the specified color
409409+ img:set(i, j, clr)
410410+ end
411411+ clr = _clr.inc(clr, 1)
412412+ end
413413+ end
414414+--rb.sleep(1000)
415415+end -- draw_gradient
416416+417417+function twist(img)
418418+--[[ local fg
419419+ if rb.lcd_get_foreground then
420420+ fg = rb.lcd_get_foreground()
421421+ end]]
422422+423423+ local ims = {}
424424+ ims.strip = _img.tile(img, img:width(), _lcd.H + img:height())
425425+ ims.y_init = {img:height(), 1}
426426+ ims.y_finl = {1, img:height()}
427427+ ims.y_inc = {-2, 4}
428428+ ims.y_pos = nil
429429+430430+ -- together define the width of the overall object
431431+ local x_off=(_lcd.W/2)
432432+ local m = _lcd.W
433433+ local z = -m
434434+ local zi = 1
435435+436436+ -- angles of rotation for each leaf
437437+ local ANGLES = {0, 45, 90, 135, 180, 225, 270, 315}
438438+ local elems = #ANGLES
439439+ local _XCOORD = {}
440440+441441+ -- color alternates each leaf
442442+ local colors = { WHITE, _clr.set(0, 0, 0, 0) }
443443+ local c = 0
444444+445445+ -- calculated position of each point in the sine wave(s)
446446+ local xs, xe
447447+448448+ --[[--Profiling code
449449+ local timer = _timer.start()]]
450450+451451+ for rot = 0, 6000, 4 do
452452+ _lcd:clear(BLACK)
453453+454454+ for y=1, _lcd.H do
455455+456456+ local function get_sines()
457457+ local sc = m + z
458458+ if sc == 0 then
459459+ sc = 1 -- prevent divide by 0
460460+ elseif sc + z > _lcd.W then
461461+ zi = -1
462462+ colors[2] = _clr.inc(colors[2], 1, 0, 50, 0)
463463+ elseif sc + z < -(_lcd.W) then
464464+ zi = 1
465465+ colors[1] = _clr.inc(colors[1], -1, 0, 10, 0)
466466+ end
467467+ if colors[2] == colors[1] then
468468+ colors[2] = _clr.inc(colors[2], 1)
469469+ end
470470+ for j = 1, elems do
471471+ _XCOORD[j] = _math.d_sin(y + ANGLES[j] + rot) / sc + x_off
472472+ end
473473+ _XCOORD[0] = _XCOORD[elems] -- loop table for efficient wrap
474474+ end
475475+476476+ get_sines()
477477+ for k = 1, elems do
478478+ xs = _XCOORD[k]
479479+ xe = _XCOORD[(k+1) % elems]
480480+ if xs < 1 or xe > _lcd.W then
481481+ while xs < 1 or xe > _lcd.W do
482482+ m = m + 1 -- shift m in order to scale object min/max
483483+ get_sines()
484484+ xs = _XCOORD[k]
485485+ xe = _XCOORD[(k+1) % elems]
486486+ end
487487+ end
488488+ c = (c % 2) + 1
489489+ if xs < xe then
490490+ -- defines the direction of leaves & fills them
491491+492492+ _lcd:set(xs, y, colors[c])
493493+ _lcd:set(xe, y, colors[c])
494494+ _lcd:line(xs + 1, y, xe - 1, y, colors[3 - c], true)
495495+ end
496496+ end
497497+498498+ end
499499+500500+ do -- stripes and shifts image strips; blits it into the colors(1) leaves
501501+ local y
502502+ local y_col = 1
503503+ local w = ims.strip:width() - 1
504504+ local h = ims.strip:height() - 1
505505+ if ims.y_pos ~= nil then
506506+ for i = 1, #ims.y_pos do
507507+ ims.y_pos[i] = ims.y_pos[i] + ims.y_inc[i]
508508+509509+ if (ims.y_inc[i] > 0 and ims.y_pos[i] > ims.y_finl[i])
510510+ or (ims.y_inc[i] < 0 and ims.y_pos[i] < ims.y_finl[i]) then
511511+ ims.y_pos[i] = ims.y_init[i]
512512+ end
513513+ end
514514+ else
515515+ ims.y_pos = {ims.y_init[1], ims.y_init[2]}
516516+ end
517517+518518+ for ix = 1, _lcd.W, w do
519519+ y_col = y_col + 1
520520+ y = ims.y_pos[(y_col % 2) + 1]
521521+ if _lcd.DEPTH > 1 then
522522+ _lcd:copy(ims.strip, ix, 1, 1, y, w, h, false, _blit.BDEQC, colors[1])
523523+ else
524524+ _lcd:copy(ims.strip, ix, 1, 1, y, w, h, false, _blit.BSAND)
525525+ end
526526+ end
527527+ end
528528+529529+ _lcd:update()
530530+ z = z + zi
531531+532532+ if rb.get_plugin_action(0) == CANCEL_BUTTON then
533533+ break
534534+ end
535535+ collectgarbage("step")
536536+ end
537537+ --[[--Profiling code
538538+ _print.f("%d", _timer.stop(timer))
539539+ rb.sleep(rb.HZ * 10)]]
540540+end -- twist
541541+542542+function draw_target(img)
543543+544544+ local clr = _clr.set(0, 0, 0, 0)
545545+546546+ -- make a copy of original screen for restoration
547547+ local screen_img = _lcd:duplicate()
548548+549549+ rad_m = math.min(_lcd.W, _lcd.H)
550550+551551+ for s = -_lcd.W /4, 16 do
552552+ img:copy(screen_img)
553553+ s = math.max(1, math.abs(s))
554554+ for r = 1, rad_m /2 - 10, s do
555555+ clr = _clr.inc(clr, 1, r * 5, r * 10, r * 20)
556556+ _draw.circle(img, _lcd.CX, _lcd.CY, r, clr)
557557+ end
558558+559559+ _lcd:update()
560560+ if rb.get_plugin_action( 20) == CANCEL_BUTTON then
561561+ z = 16;
562562+ break;
563563+ end
564564+ end
565565+566566+end -- draw_target
567567+568568+function draw_sweep(img, cx, cy, radius, color)
569569+ local timer = _timer() --creates a new timer saves index
570570+ local wait
571571+ local x
572572+ local y
573573+ --make a copy of original screen for restoration
574574+ local screen_img = _lcd:duplicate()
575575+ _draw.circle(img, cx, cy, radius, color)
576576+ for d = 630, 270, - 5 do
577577+ if d % 45 == 0 then
578578+ img:copy(screen_img)
579579+ _draw.circle(img, cx, cy, radius, color)
580580+ l = 0
581581+ end
582582+ x = cx + radius * _math.d_cos(d) / 10000
583583+ y = cy + radius * _math.d_sin(d) / 10000
584584+585585+586586+ _draw.line(img, cx, cy, x, y, color)
587587+ l = l + 1
588588+ if l > 1 then
589589+ x1 = cx + (radius - 1) * _math.d_cos(d + 1) / 10000
590590+ y1 = cy + (radius - 1) * _math.d_sin(d + 1) / 10000
591591+ _draw_floodfill(img, x1, y1, BLACK, color)
592592+ end
593593+ _lcd:update()
594594+ wait = timer:check(true) --checks timer and updates last time
595595+ if wait >= 50 then
596596+ wait = 0
597597+ elseif wait < 50 then
598598+ wait = 50 - wait
599599+ end
600600+ if rb.get_plugin_action( wait) == CANCEL_BUTTON then
601601+ break
602602+ end
603603+ end
604604+ timer:stop() --destroys timer, also returns time since last time
605605+ screen_img = nil
606606+end -- draw_sweep
607607+608608+function rotate_image(img)
609609+ local blitop = _blit.BOR
610610+ local i = 1
611611+ local d = 0
612612+ local ximg
613613+ local x, y, w, h, xr, yr
614614+615615+ ximg = _img.rotate(img, 45) -- image will be largest at this point
616616+ w = ximg:width()
617617+ h = ximg:height()
618618+ xr = (_lcd.W - w) / 2
619619+ yr = (_lcd.H - h) / 2
620620+ --make a copy of original screen for restoration
621621+ local screen_img -- = _lcd:duplicate()
622622+ screen_img =_img.new(w, h)
623623+ screen_img :copy(_LCD, 1, 1, xr, yr, w, h)
624624+ --_print.f("CW")
625625+626626+ --[[--Profiling code
627627+ local timer = _timer.start()]]
628628+629629+ while d >= 0 do
630630+ ximg = _img.rotate(img, d)
631631+ w = ximg:width()
632632+ h = ximg:height()
633633+ x = (_lcd.W - w) / 2
634634+ y = (_lcd.H - h) / 2
635635+636636+ -- copy our rotated image onto the background
637637+ _lcd:copy(ximg, x, y, 1, 1, w, h, false, blitop)
638638+ _lcd:update()
639639+ --restore the portion of the background we destroyed
640640+ _lcd:copy(screen_img, xr, yr, 1, 1)
641641+642642+ if d > 0 and d % 360 == 0 then
643643+ _lcd:copy(ximg, x, y, 1, 1, w, h)
644644+ _lcd:update()
645645+ if i == 1 then i = 0 end
646646+ if d == 1440 or i < 0 then
647647+ if i < 0 then
648648+ i = i - 5
649649+ else
650650+ i = - 5
651651+ end
652652+ blitop = _blit.BXOR
653653+ --_print.f("CCW")
654654+ --rb.sleep(rb.HZ)
655655+ else
656656+ i = i + 5
657657+ --_print.f("CW")
658658+ --rb.sleep(rb.HZ)
659659+ end
660660+661661+ end
662662+ d = d + i
663663+664664+ if rb.get_plugin_action(0) == CANCEL_BUTTON then
665665+ break;
666666+ end
667667+ end
668668+669669+ _lcd:copy(ximg, x, y, 1, 1, w, h)
670670+ --[[-- Profiling code
671671+ _print.f("%d", _timer.stop(timer))
672672+ rb.sleep(rb.HZ * 10)]]
673673+end -- rotate_image
674674+675675+-- shows blitting with a mask
676676+function blit_mask(dst)
677677+ local timer = _timer()
678678+ local r = math.min(_lcd.CX, _lcd.CY) / 5
679679+680680+ local bmask = _img.new(_lcd.W, _lcd.H)
681681+ bmask:clear(0)
682682+683683+ _draw.circle_filled(bmask, _lcd.CX, _lcd.CY, r, WHITE)
684684+ local color = _clr.set(0, 0, 0 ,0)
685685+ for z = 0, 100 do
686686+ z = z + timer:check(true)
687687+ color = _clr.inc(color, 1, z * 5, z * 10, z * 20)
688688+ dst:copy(bmask, 1, 1, 1, 1, nil, nil, false, _blit.BSAND, color)
689689+ _lcd:update()
690690+691691+ if rb.get_plugin_action(0) == CANCEL_BUTTON then
692692+ break
693693+ end
694694+ end
695695+end -- blit_mask
696696+697697+-- draws an X on the screen
698698+function draw_x()
699699+ _draw.line(_LCD, 1, 1, _lcd.W, _lcd.H, WHITE)
700700+ _draw.line(_LCD, _lcd.W, 1, 1, _lcd.H, WHITE)
701701+702702+ _draw.hline(_LCD, 10, _lcd.CY , _lcd.W - 21, WHITE)
703703+704704+ _draw.vline(_LCD, _lcd.CX, 20 , _lcd.H - 41, WHITE)
705705+706706+ _draw.rect(_LCD, _lcd.CX - 17, _lcd.CY - 17, 34, 34, WHITE)
707707+ _lcd:update()
708708+ rb.sleep(100)
709709+end -- draw_x
710710+711711+--fills an image with random colors
712712+function random_img(img)
713713+ local min = _clr.set(0, 0, 0, 0)
714714+ local max = _clr.set(-1, 255, 255, 255)
715715+ math.randomseed(rb.current_tick())
716716+ for x = 1, img:width() do
717717+ for y = 1, img:height() do
718718+ img:set(x, y, math.random(min, max))
719719+ end
720720+ end
721721+end -- random_img
722722+723723+function rainbow_img(img)
724724+--draw a gradient using available colors
725725+ if IS_COLOR_TARGET == true then
726726+ --R O Y G B I V
727727+ clrs = {
728728+ {r = 255, g = 255, b = 255}, -- white
729729+ {r = 000, g = 000, b = 000}, -- black
730730+ {r = 200, g = 000, b = 000}, -- red
731731+ {r = 255, g = 000, b = 000}, -- red
732732+ {r = 255, g = 100, b = 033}, -- orange
733733+ {r = 255, g = 127, b = 000}, -- orange
734734+ {r = 255, g = 200, b = 033}, -- yellow
735735+ {r = 255, g = 255, b = 000}, -- yellow
736736+ {r = 050, g = 255, b = 000}, -- green
737737+ {r = 000, g = 125, b = 125}, -- green
738738+ {r = 000, g = 000, b = 255}, -- blue
739739+ {r = 033, g = 025, b = 200}, -- indigo
740740+ {r = 075, g = 000, b = 150}, -- indigo
741741+ {r = 127, g = 000, b = 150}, -- violet
742742+ {r = 150, g = 000, b = 255}, -- violet
743743+ {r = 255, g = 255, b = 255}, -- white
744744+ {r = 000, g = 000, b = 000}, -- black
745745+ }
746746+ else
747747+ end
748748+ draw_gradient(img, 1, 1, nil, nil, "V", clrs)
749749+end -- rainbow_img
750750+751751+-- draws a rounded rectangle with text
752752+function rock_lua()
753753+ local res, w, h = _lcd:text_extent("W", rb.FONT_UI)
754754+ w = _lcd.W - 10
755755+ h = h + 4
756756+ _draw.rounded_rect_filled(_LCD, 5, 5, w, h, 15, WHITE)
757757+ _draw_text(_LCD, 5, 5, w, h, nil, BLACK, "ROCKlua!")
758758+ _lcd:update()
759759+ rb.sleep(100)
760760+end -- rock_lua
761761+762762+-- draws a rounded rectangle with long text
763763+function long_text()
764764+ local txt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
765765+ local res, w, h = _lcd:text_extent(txt, rb.FONT_UI)
766766+ local resp, wp, hp = _lcd:text_extent(" ", rb.FONT_UI)
767767+ local wait = 0
768768+ w = w + wp * 3
769769+ h = h + 4
770770+ local img = _img.new(w + 1, h)
771771+ img:clear(BLACK)
772772+ _draw.rounded_rect_filled(img, 1, 1, w, h, 15, WHITE)
773773+ _draw_text(img, 1, 2, nil, nil, nil, BLACK, txt)
774774+775775+ for p = -w + 1, w - 1 do
776776+ wait = 0
777777+ p = math.abs(p)
778778+ _lcd:copy(img, 1, _lcd.H - h, w - p, 1)
779779+ _lcd:update()
780780+ if p == 0 or w - p == 1 then wait = 100; rb.sleep(50) end
781781+ if rb.get_plugin_action(wait) == CANCEL_BUTTON then
782782+ break
783783+ end
784784+ end
785785+786786+end -- long_text
787787+788788+-- creates or loads an image to use as logo
789789+function get_logo()
790790+ local logo_img = _img.load("/pix.bmp") --loads the previously saved image (if we saved it before)
791791+792792+ --create a logo if an image wasn't saved previously
793793+ if(not logo_img) then
794794+ logo_img = create_logo()
795795+ end
796796+797797+ --fallback
798798+ if(not logo_img) then
799799+ -- Load a BMP file in the variable backdrop
800800+ local base = "/.rockbox/icons/"
801801+ local backdrop = _img.load("/image.bmp") --user supplied image
802802+ or _img.load(base .. "tango_small_viewers.bmp")
803803+ or _img.load(base .. "tango_small_viewers_mono.bmp")
804804+ or _img.load(base .. "tango_small_mono.bmp")
805805+ or _img.load(base .. "tango_icons.16x16.bmp")
806806+ or _img.load(base .. "tango_icons_viewers.16x16.bmp")
807807+ or _img.load(base .. "viewers.bmp")
808808+ or _img.load(base .. "viewers.6x8x1.bmp")
809809+ or _img.load(base .. "viewers.6x8x2.bmp")
810810+ or _img.load(base .. "viewers.6x8x10.bmp")
811811+ or _img.load(base .. "viewers.6x8x16.bmp")
812812+ logo_img = backdrop
813813+ end
814814+ return logo_img
815815+end -- get_logo
816816+817817+-- uses print_table to display a menu
818818+function main_menu()
819819+820820+ local mt = {
821821+ [1] = "Rocklua RLI Example",
822822+ [2] = "The X",
823823+ [3] = "Blit Mask",
824824+ [4] = "Target",
825825+ [5] = "Sweep",
826826+ [6] = "Bouncing Ball (olive)",
827827+ [7] = "The Twist",
828828+ [8] = "Image Rotation",
829829+ [9] = "Long Text",
830830+ [10] = "Rainbow Image",
831831+ [11] = "Random Image",
832832+ [12] = "Clear Screen",
833833+ [13] = "Save Screen",
834834+ [14] = "Exit"
835835+ }
836836+ local ft = {
837837+ [0] = exit_now, --if user cancels do this function
838838+ [1] = function(TITLE) return true end, -- shouldn't happen title occupies this slot
839839+ [2] = function(THE_X) draw_x() end,
840840+ [3] = function(BLITM) blit_mask(_lcd()) end,
841841+ [4] = function(TARGT) draw_target(_lcd()) end,
842842+ [5] = function(SWEEP)
843843+ local r = math.min(_lcd.CX, _lcd.CY) - 20
844844+ draw_sweep(_lcd(), _lcd.CX, _lcd.CY, r, _clr.set(-1, 0, 255, 0))
845845+ end,
846846+ [6] = function(BOUNC) bounce_image(create_ball()) end,
847847+ [7] = function(TWIST) twist(get_logo()) end,
848848+ [8] = function(ROTAT) rotate_image(get_logo()) end,
849849+ [9] = long_text,
850850+ [10] = function(RAINB)
851851+ rainbow_img(_lcd()); _lcd:update(); rb.sleep(rb.HZ)
852852+ end,
853853+ [11] = function(RANDM)
854854+ random_img(_lcd()); _lcd:update(); rb.sleep(rb.HZ)
855855+ end,
856856+ [12] = function(CLEAR) _lcd:clear(BLACK); rock_lua() end,
857857+ [13] = function(SAVEI) _LCD:invert(); _img_save(_LCD, "/rocklua.bmp") end,
858858+ [14] = function(EXIT_) return true end
859859+ }
860860+861861+ if _lcd.DEPTH < 2 then
862862+ table.remove(mt, 10)
863863+ table.remove(ft, 10)
864864+ end
865865+866866+ print_menu(mt, ft)
867867+868868+end
869869+--------------------------------------------------------------------------------
870870+function exit_now()
871871+ _lcd:update()
872872+ _lcd:splashf(rb.HZ * 5, "ran for %d seconds", _timer.stop("main") / rb.HZ)
873873+ os.exit()
874874+end -- exit_now
875875+--------------------------------------------------------------------------------
876876+877877+--MAIN PROGRAM------------------------------------------------------------------
878878+_timer("main") -- keep track of how long the program ran
879879+880880+-- Clear the screen
881881+_lcd:clear(BLACK)
882882+883883+if _lcd.DEPTH > 1 then
884884+--draw a gradient using available colors
885885+if IS_COLOR_TARGET == true then
886886+ clrs = {
887887+ {r = 255, g = 000, b = 000}, -- red
888888+ {r = 000, g = 255, b = 000}, -- green
889889+ {r = 000, g = 000, b = 255}, -- blue
890890+ }
891891+else
892892+end
893893+ local w = bit.rshift(_lcd.W, 2)
894894+ local h = bit.rshift(_lcd.H, 2)
895895+ draw_gradient(_lcd(), (_lcd.W - w)/2 - 1, (_lcd.H - h)/3 - 1, w, h, "H", clrs)
896896+ _lcd:update()
897897+ rb.sleep(rb.HZ)
898898+end
899899+900900+do
901901+local img = _img.load("/rocklua.bmp")
902902+ if not img then
903903+ rock_lua()
904904+ else
905905+ _lcd:image(img)
906906+ end
907907+end
908908+_lcd:update()
909909+rb.sleep(rb.HZ / 2)
910910+911911+if rb.cpu_boost then rb.cpu_boost(true) end
912912+913913+main_menu()
914914+915915+if rb.cpu_boost then rb.cpu_boost(false) end
916916+917917+exit_now()
918918+919919+-- For a reference list of all functions available, see apps/plugins/lua/rocklib.c (and apps/plugin.h)