lua add rlimg.lua example script split large includes to separate files

Change-Id: I67cac5bc4ce5525ab30abf9443f6cc1a33190512
This commit is contained in:
William Wilgus 2019-08-04 09:48:09 -05:00
parent 4209c09770
commit f85df30e3e
8 changed files with 1464 additions and 393 deletions

View file

@ -29,7 +29,6 @@
_draw.ellipse_filled
_draw.ellipse_rect_filled
_draw.ellipse_rect
_draw.flood_fill
_draw.hline
_draw.image
_draw.line
@ -39,7 +38,6 @@
_draw.rect_filled
_draw.rounded_rect
_draw.rounded_rect_filled
_draw.text
_draw.vline
]]
@ -56,21 +54,17 @@ local _draw = {} do
setmetatable(_draw, rocklib_image)
-- Internal Constants
local _LCD = rb.lcd_framebuffer()
local LCD_W, LCD_H = rb.LCD_WIDTH, rb.LCD_HEIGHT
local BSAND = 8 -- blits color to dst if src <> 0
local _NIL = nil -- nil placeholder
local _abs = math.abs
local _clear = rocklib_image.clear
local _copy = rocklib_image.copy
local _ellipse = rocklib_image.ellipse
local _get = rocklib_image.get
local _line = rocklib_image.line
local _marshal = rocklib_image.marshal
local _min = math.min
local _newimg = rb.new_image
local _points = rocklib_image.points
-- line
_draw.line = function(img, x1, y1, x2, y2, color, bClip)
@ -87,33 +81,22 @@ local _draw = {} do
_line(img, x, y, _NIL, y + length, color, bClip)
end
-- draws a non-filled figure based on points in t-points
local function polyline(img, x, y, t_points, color, bClosed, bClip)
if #t_points < 2 then error("not enough points", 3) end
local pt_first_last
if bClosed then
pt_first_last = t_points[1]
else
pt_first_last = t_points[#t_points]
end
for i = 1, #t_points, 1 do
local pt1 = t_points[i]
local pt2 = t_points[i + 1] or pt_first_last-- first and last point
-- draws a non-filled rect based on points in t-points
local function polyrect(img, x, y, t_points, color, bClip)
local pt_first_last = t_points[1]
local pt1, pt2
for i = 1, 4, 1 do
pt1 = t_points[i]
pt2 = t_points[i + 1] or pt_first_last-- first and last point
_line(img, pt1[1] + x, pt1[2] + y, pt2[1] + x, pt2[2] + y, color, bClip)
end
end
-- rectangle
local function rect(img, x, y, width, height, color, bClip)
if width == 0 or height == 0 then return end
polyline(img, x, y, {{0, 0}, {width, 0}, {width, height}, {0, height}}, color, true, bClip)
polyrect(img, x, y, {{0, 0}, {width, 0}, {width, height}, {0, height}}, color, bClip)
end
@ -240,190 +223,9 @@ local _draw = {} do
_copy(dst, src, x, y, 1, 1, _NIL, _NIL, bClip)
end
-- floods an area of targetclr with fillclr x, y specifies the start seed
_draw.flood_fill = function(img, x, y, targetclr, fillclr)
-- scanline 4-way flood algorithm
-- ^
-- <--------x--->
-- v
-- check that target color doesn't = fill and the first point is target color
if targetclr == fillclr or targetclr ~= _get(img, x, y, true) then return end
local max_w = img:width()
local max_h = img:height()
local qpt = {} -- FIFO queue
-- rather than moving elements around in our FIFO queue
-- for each read; increment 'qhead' by 2
-- set both elements to nil and let the
-- garbage collector worry about it
-- for each write; increment 'qtail' by 2
-- x coordinates are in odd indices while
-- y coordinates are in even indices
local qtail = 0
local function check_ns(val, x, y)
if targetclr == val then
y = y - 1
if targetclr == _get(img, x, y, true) then -- north
qtail = qtail + 2
qpt[qtail - 1] = x
qpt[qtail] = y
end
y = y + 2
if targetclr == _get(img, x, y, true) then -- south
qtail = qtail + 2
qpt[qtail - 1] = x
qpt[qtail] = y
end
return fillclr
end
return _NIL -- signal marshal to stop
end
local function seed_pt(x, y)
-- should never hit max but make sure not to end early
for qhead = 2, 0x40000000, 2 do
if targetclr == _get(img, x, y, true) then
_marshal(img, x, y, 1, y, _NIL, _NIL, true, check_ns) -- west
_marshal(img, x + 1, y, max_w, y, _NIL, _NIL, true, check_ns) -- east
end
x = qpt[qhead - 1]
qpt[qhead - 1] = _NIL
if not x then break end
y = qpt[qhead]
qpt[qhead] = _NIL
end
end
seed_pt(x, y) -- Begin
end -- flood_fill
-- draws a closed figure based on points in t_points
_draw.polygon = function(img, x, y, t_points, color, fillcolor, bClip)
if #t_points < 2 then error("not enough points", 3) end
if fillcolor then
local x_min, x_max = 0, 0
local y_min, y_max = 0, 0
local w, h = 0, 0
-- find boundries of polygon
for i = 1, #t_points, 1 do
local pt = t_points[i]
if pt[1] < x_min then x_min = pt[1] end
if pt[1] > x_max then x_max = pt[1] end
if pt[2] < y_min then y_min = pt[2] end
if pt[2] > y_max then y_max = pt[2] end
end
w = _abs(x_max) + _abs(x_min)
h = _abs(y_max) + _abs(y_min)
x_min = x_min - 2 -- leave a border to use flood_fill
y_min = y_min - 2
local fill_img = _newimg(w + 3, h + 3)
_clear(fill_img, 0x1)
for i = 1, #t_points, 1 do
local pt1 = t_points[i]
local pt2 = t_points[i + 1] or t_points[1]-- first and last point
_line(fill_img, pt1[1] - x_min, pt1[2] - y_min,
pt2[1]- x_min, pt2[2] - y_min, 0)
end
_draw.flood_fill(fill_img, fill_img:width(), fill_img:height() , 0x1, 0x0)
_copy(img, fill_img, x - 1, y - 1, _NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor)
end
polyline(img, x, y, t_points, color, true, bClip)
end
-- draw text onto image if width/height are supplied text is centered
_draw.text = function(img, x, y, width, height, font, color, text)
font = font or rb.FONT_UI
local opts = {x = 0, y = 0, width = LCD_W - 1, height = LCD_H - 1,
font = font, drawmode = 3, fg_pattern = 0x1, bg_pattern = 0}
if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
--vp.drawmode = bit.bxor(vp.drawmode, 4)
opts.fg_pattern = 3 - opts.fg_pattern
opts.bg_pattern = 3 - opts.bg_pattern
end
rb.set_viewport(opts)
local res, w, h = rb.font_getstringsize(text, font)
if not width then
width = 0
else
width = (width - w) / 2
end
if not height then
height = 0
else
height = (height - h) / 2
end
-- make a copy of the current screen for later
local screen_img = _newimg(LCD_W, LCD_H)
_copy(screen_img, _LCD)
-- check if the screen buffer is supplied image if so set img to the copy
if img == _LCD then
img = screen_img
end
-- we will be printing the text to the screen then blitting into img
rb.lcd_clear_display()
if w > LCD_W then -- text is too long for the screen do it in chunks
local l = 1
local resp, wp, hp
local lenr = text:len()
while lenr > 1 do
l = lenr
resp, wp, hp = rb.font_getstringsize(text:sub(1, l), font)
while wp >= LCD_W and l > 1 do
l = l - 1
resp, wp, hp = rb.font_getstringsize(text:sub( 1, l), font)
end
rb.lcd_putsxy(0, 0, text:sub(1, l))
text = text:sub(l)
if x + width > img:width() or y + height > img:height() then
break
end
-- using the mask we made blit color into img
_copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
x = x + wp
rb.lcd_clear_display()
lenr = text:len()
end
else --w <= LCD_W
rb.lcd_putsxy(0, 0, text)
-- using the mask we made blit color into img
_copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
end
_copy(_LCD, screen_img) -- restore screen
rb.set_viewport() -- set viewport default
return res, w, h
end
-- expose internal functions to the outside through _draw table
_draw.hline = hline
_draw.vline = vline
_draw.polyline = polyline
_draw.rect = rect
_draw.rounded_rect = rounded_rect
end -- _draw functions

View file

@ -0,0 +1,95 @@
--[[ Lua Floodfill function
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2017 William Wilgus
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
]]
-- floods an area of targetclr with fillclr x, y specifies the start seed
-- flood_fill(img, x, y, targetclr, fillclr)
if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
do
local rocklib_image = getmetatable(rb.lcd_framebuffer())
local _NIL = nil -- nil placeholder
local _get = rocklib_image.get
local _line = rocklib_image.line
local _marshal = rocklib_image.marshal
return function(img, x, y, targetclr, fillclr)
-- scanline 4-way flood algorithm
-- ^
-- <--------x--->
-- v
-- check that target color doesn't = fill and the first point is target color
if targetclr == fillclr or targetclr ~= _get(img, x, y, true) then return end
local max_w = img:width()
local max_h = img:height()
local qpt = {} -- FIFO queue
-- rather than moving elements around in our FIFO queue
-- for each read; increment 'qhead' by 2
-- set both elements to nil and let the
-- garbage collector worry about it
-- for each write; increment 'qtail' by 2
-- x coordinates are in odd indices while
-- y coordinates are in even indices
local qtail = 0
local function check_ns(val, x, y)
if targetclr == val then
y = y - 1
if targetclr == _get(img, x, y, true) then -- north
qtail = qtail + 2
qpt[qtail - 1] = x
qpt[qtail] = y
end
y = y + 2
if targetclr == _get(img, x, y, true) then -- south
qtail = qtail + 2
qpt[qtail - 1] = x
qpt[qtail] = y
end
return fillclr
end
return _NIL -- signal marshal to stop
end
local function seed_pt(x, y)
-- should never hit max but make sure not to end early
for qhead = 2, 0x40000000, 2 do
if targetclr == _get(img, x, y, true) then
_marshal(img, x, y, 1, y, _NIL, _NIL, true, check_ns) -- west
_marshal(img, x + 1, y, max_w, y, _NIL, _NIL, true, check_ns) -- east
end
x = qpt[qhead - 1]
qpt[qhead - 1] = _NIL
if not x then break end
y = qpt[qhead]
qpt[qhead] = _NIL
end
end
seed_pt(x, y) -- Begin
end -- flood_fill
end

View file

@ -0,0 +1,103 @@
--[[ Lua Poly Drawing functions
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2017 William Wilgus
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
]]
--[[ Exposed Functions
_poly.polygon
_poly.polyline
]]
if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
local _poly = {} do
local BSAND = 8 -- blits color to dst if src <> 0
local _NIL = nil -- nil placeholder
local _abs = math.abs
local _copy = rocklib_image.copy
local _line = rocklib_image.line
local flood_fill = require("draw_floodfill")
-- draws a non-filled figure based on points in t-points
local function polyline(img, x, y, t_points, color, bClosed, bClip)
if #t_points < 2 then error("not enough points", 3) end
local pt_first_last
if bClosed then
pt_first_last = t_points[1]
else
pt_first_last = t_points[#t_points]
end
for i = 1, #t_points, 1 do
local pt1 = t_points[i]
local pt2 = t_points[i + 1] or pt_first_last-- first and last point
_line(img, pt1[1] + x, pt1[2] + y, pt2[1] + x, pt2[2] + y, color, bClip)
end
end
-- draws a closed figure based on points in t_points
_poly.polygon = function(img, x, y, t_points, color, fillcolor, bClip)
if #t_points < 2 then error("not enough points", 3) end
if fillcolor then
local x_min, x_max = 0, 0
local y_min, y_max = 0, 0
local w, h = 0, 0
-- find boundries of polygon
for i = 1, #t_points, 1 do
local pt = t_points[i]
if pt[1] < x_min then x_min = pt[1] end
if pt[1] > x_max then x_max = pt[1] end
if pt[2] < y_min then y_min = pt[2] end
if pt[2] > y_max then y_max = pt[2] end
end
w = _abs(x_max) + _abs(x_min)
h = _abs(y_max) + _abs(y_min)
x_min = x_min - 2 -- leave a border to use flood_fill
y_min = y_min - 2
local fill_img = _newimg(w + 3, h + 3)
_clear(fill_img, 0x1)
for i = 1, #t_points, 1 do
local pt1 = t_points[i]
local pt2 = t_points[i + 1] or t_points[1]-- first and last point
_line(fill_img, pt1[1] - x_min, pt1[2] - y_min,
pt2[1]- x_min, pt2[2] - y_min, 0)
end
flood_fill(fill_img, fill_img:width(), fill_img:height() , 0x1, 0x0)
_copy(img, fill_img, x - 1, y - 1, _NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor)
end
polyline(img, x, y, t_points, color, true, bClip)
end
-- expose internal functions to the outside through _poly table
_poly.polyline = polyline
end
return _poly

View file

@ -0,0 +1,121 @@
--[[ Lua Draw Text function
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2017 William Wilgus
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
]]
-- draw text onto image if width/height are supplied text is centered
if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
do
-- Internal Constants
local rocklib_image = getmetatable(rb.lcd_framebuffer())
local _LCD = rb.lcd_framebuffer()
local LCD_W, LCD_H = rb.LCD_WIDTH, rb.LCD_HEIGHT
local BSAND = 8 -- blits color to dst if src <> 0
local _NIL = nil -- nil placeholder
local _clear = rocklib_image.clear
local _copy = rocklib_image.copy
local _newimg = rb.new_image
return function(img, x, y, width, height, font, color, text)
font = font or rb.FONT_UI
local opts = {x = 0, y = 0, width = LCD_W - 1, height = LCD_H - 1,
font = font, drawmode = 3, fg_pattern = 0x1, bg_pattern = 0}
if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
--vp.drawmode = bit.bxor(vp.drawmode, 4)
opts.fg_pattern = 3 - opts.fg_pattern
opts.bg_pattern = 3 - opts.bg_pattern
end
rb.set_viewport(opts)
local res, w, h = rb.font_getstringsize(text, font)
if not width then
width = 0
else
width = (width - w) / 2
end
if not height then
height = 0
else
height = (height - h) / 2
end
-- make a copy of the current screen for later
--local screen_img = _newimg(LCD_W, LCD_H)
local screen_img = _newimg(LCD_W, h * 2)
_copy(screen_img, _LCD)
-- check if the screen buffer is supplied image if so set img to the copy
if img == _LCD then
img = screen_img
end
-- we will be printing the text to the screen then blitting into img
--rb.lcd_clear_display()
_clear(_LCD, opts.bg_pattern or 0, 1, 1, LCD_W, h * 2)
if w > LCD_W then -- text is too long for the screen do it in chunks
local l = 1
local resp, wp, hp
local lenr = text:len()
while lenr > 1 do
l = lenr
resp, wp, hp = rb.font_getstringsize(text:sub(1, l), font)
while wp >= LCD_W and l > 1 do
l = l - 1
resp, wp, hp = rb.font_getstringsize(text:sub( 1, l), font)
end
rb.lcd_putsxy(0, 0, text:sub(1, l))
text = text:sub(l)
if x + width > img:width() or y + height > img:height() then
break
end
-- using the mask we made blit color into img
_copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
x = x + wp
--rb.lcd_clear_display()
_clear(_LCD, opts.bg_pattern or 0, 1, 1, LCD_W, h * 2)
lenr = text:len()
end
else --w <= LCD_W
rb.lcd_putsxy(0, 0, text)
-- using the mask we made blit color into img
_copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
end
_copy(_LCD, screen_img) -- restore screen
rb.set_viewport() -- set viewport default
return res, w, h
end
end

View file

@ -23,7 +23,6 @@
--[[ Exposed Functions
_img.save
_img.search
_img.rotate
_img.resize
@ -156,189 +155,6 @@ local _img = {} do
return r_img
end
-- saves img to file: name
_img.save = function(img, name)
-- bmp saving derived from rockbox - screendump.c
-- bitdepth is limited by the device
-- eg. device displays greyscale, rgb images are saved greyscale
local file
local bbuffer = {} -- concat buffer for s_bytes
local fbuffer = {} -- concat buffer for file writes, reused
local function s_bytesLE(bits, value)
-- bits must be multiples of 8 (sizeof byte)
local byte
local nbytes = bit.rshift(bits, 3)
for b = 1, nbytes do
if value > 0 then
byte = value % 256
value = (value - byte) / 256
else
byte = 0
end
bbuffer[b] = string.char(byte)
end
return table.concat(bbuffer, _NIL, 1, nbytes)
end
local function s_bytesBE(bits, value)
-- bits must be multiples of 8 (sizeof byte)
local byte
local nbytes = bit.rshift(bits, 3)
for b = nbytes, 1, -1 do
if value > 0 then
byte = value % 256
value = (value - byte) / 256
else
byte = 0
end
bbuffer[b] = string.char(byte)
end
return table.concat(bbuffer, _NIL, 1, nbytes)
end
local cmp = {["r"] = function(c) return bit.band(bit.rshift(c, 16), 0xFF) end,
["g"] = function(c) return bit.band(bit.rshift(c, 08), 0xFF) end,
["b"] = function(c) return bit.band(c, 0xFF) end}
local function bmp_color(color)
return s_bytesLE(8, cmp.b(color))..
s_bytesLE(8, cmp.g(color))..
s_bytesLE(8, cmp.r(color))..
s_bytesLE(8, 0) .. ""
end -- c_cmp(color, c.r))
local function bmp_color_mix(c1, c2, num, den)
-- mixes c1 and c2 as ratio of numerator / denominator
-- used 2x each save results
local bc1, gc1, rc1 = cmp.b(c1), cmp.g(c1), cmp.r(c1)
return s_bytesLE(8, cmp.b(c2) - bc1 * num / den + bc1)..
s_bytesLE(8, cmp.g(c2) - gc1 * num / den + gc1)..
s_bytesLE(8, cmp.r(c2) - rc1 * num / den + rc1)..
s_bytesLE(8, 0) .. ""
end
local w, h = img:width(), img:height()
local depth = tonumber(img:__tostring(6)) -- RLI_INFO_DEPTH = 0x6
local format = tonumber(img:__tostring(7)) -- RLI_INFO_FORMAT = 0x7
local bpp, bypl -- bits per pixel, bytes per line
-- bypl, pad rows to a multiple of 4 bytes
if depth <= 4 then
bpp = 8 -- 256 color image
bypl = (w + 3)
elseif depth <= 16 then
bpp = 16
bypl = (w * 2 + 3)
elseif depth <= 24 then
bpp = 24
bypl = (w * 3 + 3)
else
bpp = 32
bypl = (w * 4 + 3)
end
local linebytes = bit.band(bypl, bit.bnot(3))
local bytesperpixel = bit.rshift(bpp, 3)
local headersz = 54
local imgszpad = h * linebytes
local compression, n_colors = 0, 0
local h_ppm, v_ppm = 0x00000EC4, 0x00000EC4 --Pixels Per Meter ~ 96 dpi
if depth == 16 then
compression = 3 -- BITFIELDS
n_colors = 3
elseif depth <= 8 then
n_colors = bit.lshift(1, depth)
end
headersz = headersz + (4 * n_colors)
file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag
if not file then
rb.splash(rb.HZ, "Error opening /" .. name)
return
end
-- create a bitmap header 'rope' with image details -- concatenated at end
local bmpheader = fbuffer
bmpheader[01] = "BM"
bmpheader[02] = s_bytesLE(32, headersz + imgszpad)
bmpheader[03] = "\0\0\0\0" -- WORD reserved 1 & 2
bmpheader[04] = s_bytesLE(32, headersz) -- BITMAPCOREHEADER size
bmpheader[05] = s_bytesLE(32, 40) -- BITMAPINFOHEADER size
bmpheader[06] = s_bytesLE(32, w)
bmpheader[07] = s_bytesLE(32, h)
bmpheader[08] = "\1\0" -- WORD color planes ALWAYS 1
bmpheader[09] = s_bytesLE(16, bpp) -- bits/pixel
bmpheader[10] = s_bytesLE(32, compression)
bmpheader[11] = s_bytesLE(32, imgszpad)
bmpheader[12] = s_bytesLE(32, h_ppm) -- biXPelsPerMeter
bmpheader[13] = s_bytesLE(32, v_ppm) -- biYPelsPerMeter
bmpheader[14] = s_bytesLE(32, n_colors)
bmpheader[15] = s_bytesLE(32, n_colors)
-- Color Table (#n_colors entries)
if depth == 1 then -- assuming positive display
bmpheader[#bmpheader + 1] = bmp_color(0xFFFFFF)
bmpheader[#bmpheader + 1] = bmp_color(0x0)
elseif depth == 2 then
bmpheader[#bmpheader + 1] = bmp_color(0xFFFFFF)
bmpheader[#bmpheader + 1] = bmp_color_mix(0xFFFFFF, 0, 1, 3)
bmpheader[#bmpheader + 1] = bmp_color_mix(0xFFFFFF, 0, 2, 3)
bmpheader[#bmpheader + 1] = bmp_color(0x0)
elseif depth == 16 then
if format == 555 then
-- red bitfield mask
bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x00007C00)
-- green bitfield mask
bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x000003E0)
-- blue bitfield mask
bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x0000001F)
else --565
-- red bitfield mask
bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x0000F800)
-- green bitfield mask
bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x000007E0)
-- blue bitfield mask
bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x0000001F)
end
end
file:write(table.concat(fbuffer))-- write the header to the file now
for i=1, #fbuffer do fbuffer[i] = _NIL end -- reuse table
local imgdata = fbuffer
-- pad rows to a multiple of 4 bytes
local bytesleft = linebytes - (bytesperpixel * w)
local t_data = {}
local fs_bytes_E = s_bytesLE -- default save in Little Endian
if format == 3553 then -- RGB565SWAPPED
fs_bytes_E = s_bytesBE -- Saves in Big Endian
end
-- Bitmap lines start at bottom unless biHeight is negative
for point in _points(img, 1, h, w + bytesleft, 1) do
imgdata[#imgdata + 1] = fs_bytes_E(bpp, point or 0)
if #fbuffer >= 31 then -- buffered write, increase # for performance
file:write(table.concat(fbuffer))
for i=1, #fbuffer do fbuffer[i] = _NIL end -- reuse table
end
end
file:write(table.concat(fbuffer)) --write leftovers to file
fbuffer = _NIL
file:close()
end -- save(img, name)
--searches an image for target color
_img.search = function(img, x1, y1, x2, y2, targetclr, variation, stepx, stepy)

View file

@ -0,0 +1,215 @@
--[[ Lua Image save
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2017 William Wilgus
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
]]
-- save(img, path/name)
-- bmp saving derived from rockbox - screendump.c
-- bitdepth is limited by the device
-- eg. device displays greyscale, rgb images are saved greyscale
if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
do
local rocklib_image = getmetatable(rb.lcd_framebuffer())
-- internal constants
local _NIL = nil -- _NIL placeholder
local _points = rocklib_image.points
-- saves img to file: name
return function(img, name)
local file
local bbuffer = {} -- concat buffer for s_bytes
local fbuffer = {} -- concat buffer for file writes, reused
local function s_bytesLE(bits, value)
-- bits must be multiples of 8 (sizeof byte)
local byte
local nbytes = bit.rshift(bits, 3)
for b = 1, nbytes do
if value > 0 then
byte = value % 256
value = (value - byte) / 256
else
byte = 0
end
bbuffer[b] = string.char(byte)
end
return table.concat(bbuffer, _NIL, 1, nbytes)
end
local function s_bytesBE(bits, value)
-- bits must be multiples of 8 (sizeof byte)
local byte
local nbytes = bit.rshift(bits, 3)
for b = nbytes, 1, -1 do
if value > 0 then
byte = value % 256
value = (value - byte) / 256
else
byte = 0
end
bbuffer[b] = string.char(byte)
end
return table.concat(bbuffer, _NIL, 1, nbytes)
end
local cmp = {["r"] = function(c) return bit.band(bit.rshift(c, 16), 0xFF) end,
["g"] = function(c) return bit.band(bit.rshift(c, 08), 0xFF) end,
["b"] = function(c) return bit.band(c, 0xFF) end}
local function bmp_color(color)
return s_bytesLE(8, cmp.b(color))..
s_bytesLE(8, cmp.g(color))..
s_bytesLE(8, cmp.r(color))..
s_bytesLE(8, 0) .. ""
end -- c_cmp(color, c.r))
local function bmp_color_mix(c1, c2, num, den)
-- mixes c1 and c2 as ratio of numerator / denominator
-- used 2x each save results
local bc1, gc1, rc1 = cmp.b(c1), cmp.g(c1), cmp.r(c1)
return s_bytesLE(8, cmp.b(c2) - bc1 * num / den + bc1)..
s_bytesLE(8, cmp.g(c2) - gc1 * num / den + gc1)..
s_bytesLE(8, cmp.r(c2) - rc1 * num / den + rc1)..
s_bytesLE(8, 0) .. ""
end
local w, h = img:width(), img:height()
local depth = tonumber(img:__tostring(6)) -- RLI_INFO_DEPTH = 0x6
local format = tonumber(img:__tostring(7)) -- RLI_INFO_FORMAT = 0x7
local bpp, bypl -- bits per pixel, bytes per line
-- bypl, pad rows to a multiple of 4 bytes
if depth <= 4 then
bpp = 8 -- 256 color image
bypl = (w + 3)
elseif depth <= 16 then
bpp = 16
bypl = (w * 2 + 3)
elseif depth <= 24 then
bpp = 24
bypl = (w * 3 + 3)
else
bpp = 32
bypl = (w * 4 + 3)
end
local linebytes = bit.band(bypl, bit.bnot(3))
local bytesperpixel = bit.rshift(bpp, 3)
local headersz = 54
local imgszpad = h * linebytes
local compression, n_colors = 0, 0
local h_ppm, v_ppm = 0x00000EC4, 0x00000EC4 --Pixels Per Meter ~ 96 dpi
if depth == 16 then
compression = 3 -- BITFIELDS
n_colors = 3
elseif depth <= 8 then
n_colors = bit.lshift(1, depth)
end
headersz = headersz + (4 * n_colors)
file = io.open('/' .. name, "w+") -- overwrite, rb ignores the 'b' flag
if not file then
rb.splash(rb.HZ, "Error opening /" .. name)
return
end
-- create a bitmap header 'rope' with image details -- concatenated at end
local bmpheader = fbuffer
bmpheader[01] = "BM"
bmpheader[02] = s_bytesLE(32, headersz + imgszpad)
bmpheader[03] = "\0\0\0\0" -- WORD reserved 1 & 2
bmpheader[04] = s_bytesLE(32, headersz) -- BITMAPCOREHEADER size
bmpheader[05] = s_bytesLE(32, 40) -- BITMAPINFOHEADER size
bmpheader[06] = s_bytesLE(32, w)
bmpheader[07] = s_bytesLE(32, h)
bmpheader[08] = "\1\0" -- WORD color planes ALWAYS 1
bmpheader[09] = s_bytesLE(16, bpp) -- bits/pixel
bmpheader[10] = s_bytesLE(32, compression)
bmpheader[11] = s_bytesLE(32, imgszpad)
bmpheader[12] = s_bytesLE(32, h_ppm) -- biXPelsPerMeter
bmpheader[13] = s_bytesLE(32, v_ppm) -- biYPelsPerMeter
bmpheader[14] = s_bytesLE(32, n_colors)
bmpheader[15] = s_bytesLE(32, n_colors)
-- Color Table (#n_colors entries)
if depth == 1 then -- assuming positive display
bmpheader[#bmpheader + 1] = bmp_color(0xFFFFFF)
bmpheader[#bmpheader + 1] = bmp_color(0x0)
elseif depth == 2 then
bmpheader[#bmpheader + 1] = bmp_color(0xFFFFFF)
bmpheader[#bmpheader + 1] = bmp_color_mix(0xFFFFFF, 0, 1, 3)
bmpheader[#bmpheader + 1] = bmp_color_mix(0xFFFFFF, 0, 2, 3)
bmpheader[#bmpheader + 1] = bmp_color(0x0)
elseif depth == 16 then
if format == 555 then
-- red bitfield mask
bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x00007C00)
-- green bitfield mask
bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x000003E0)
-- blue bitfield mask
bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x0000001F)
else --565
-- red bitfield mask
bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x0000F800)
-- green bitfield mask
bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x000007E0)
-- blue bitfield mask
bmpheader[#bmpheader + 1] = s_bytesLE(32, 0x0000001F)
end
end
file:write(table.concat(fbuffer))-- write the header to the file now
for i=1, #fbuffer do fbuffer[i] = _NIL end -- reuse table
local imgdata = fbuffer
-- pad rows to a multiple of 4 bytes
local bytesleft = linebytes - (bytesperpixel * w)
local t_data = {}
local fs_bytes_E = s_bytesLE -- default save in Little Endian
if format == 3553 then -- RGB565SWAPPED
fs_bytes_E = s_bytesBE -- Saves in Big Endian
end
-- Bitmap lines start at bottom unless biHeight is negative
for point in _points(img, 1, h, w + bytesleft, 1) do
imgdata[#imgdata + 1] = fs_bytes_E(bpp, point or 0)
if #fbuffer >= 31 then -- buffered write, increase # for performance
file:write(table.concat(fbuffer))
for i=1, #fbuffer do fbuffer[i] = _NIL end -- reuse table
end
end
file:write(table.concat(fbuffer)) --write leftovers to file
fbuffer = _NIL
file:close()
end -- save(img, name)
end