summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorWilliam Wilgus <me.theuser@yahoo.com>2018-09-23 18:25:31 +0200
committerWilliam Wilgus <me.theuser@yahoo.com>2018-10-09 01:02:25 +0200
commitc0682e094414d0748456d0b01000727039ff701c (patch)
tree2cc08bfdfe967bbe18fade1ed835d54a3f91cd23 /apps
parente4b843335bc34810c11ed405d473e6f7c94a153f (diff)
downloadrockbox-c0682e094414d0748456d0b01000727039ff701c.tar.gz
rockbox-c0682e094414d0748456d0b01000727039ff701c.tar.bz2
rockbox-c0682e094414d0748456d0b01000727039ff701c.zip
Clean up rocklib_img
optimize both size and speed fix invert for color screens Change-Id: I7edecae32dcb3daf5b3ed984a0e5b3d463269e60
Diffstat (limited to 'apps')
-rw-r--r--apps/plugins/lua/include_lua/blit.lua2
-rw-r--r--apps/plugins/lua/include_lua/color.lua31
-rw-r--r--apps/plugins/lua/include_lua/draw.lua229
-rw-r--r--apps/plugins/lua/include_lua/image.lua117
-rw-r--r--apps/plugins/lua/include_lua/lcd.lua24
-rw-r--r--apps/plugins/lua/include_lua/print.lua80
-rw-r--r--apps/plugins/lua/rocklib_img.c632
7 files changed, 513 insertions, 602 deletions
diff --git a/apps/plugins/lua/include_lua/blit.lua b/apps/plugins/lua/include_lua/blit.lua
index 6c5ea377e4..3505ae4943 100644
--- a/apps/plugins/lua/include_lua/blit.lua
+++ b/apps/plugins/lua/include_lua/blit.lua
@@ -36,7 +36,7 @@ if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
local _blit ={} do
- _blit.CUSTOM = 0xFF --user defined blit function func(dst_val, x, y, src_val, x, y)
+ _blit.CUSTOM = nil --user defined blit function func(dst_val, x, y, src_val, x, y)
_blit.BCOPY = 0x0 --copy (use :copy() instead it is slightly faster
_blit.BOR = 0x1 --OR source and dest pixels
_blit.BXOR = 0x2 --XOR source and dest pixels
diff --git a/apps/plugins/lua/include_lua/color.lua b/apps/plugins/lua/include_lua/color.lua
index ed2e4f865e..fd321edd9d 100644
--- a/apps/plugins/lua/include_lua/color.lua
+++ b/apps/plugins/lua/include_lua/color.lua
@@ -49,17 +49,9 @@ local _clr = {} do
maxstate = (bit.lshift(1, 24) - 1)
end
- local function init(v)
- return v or 0
- end
-
-- clamps value to >= min and <= max rolls over to opposite
local function clamp_roll(val, min, max)
- if min > max then
- local swap = min
- min, max = max, swap
- end
-
+ -- Warning doesn't check if min < max
if val < min then
val = max
elseif val > max then
@@ -72,12 +64,12 @@ local _clr = {} do
-- sets color -- monochrome / greyscale use 'set' -- color targets 'r,b,g'
-- on monochrome/ greyscale targets:
-- '-1' sets the highest 'color' state & 0 is the minimum 'color' state
- local function clrset(set, r, g, b)
+ _clr.set = function(set, r, g, b)
local color = set or 0
if IS_COLOR_TARGET then
- if (r ~= _NIL or g ~= _NIL or b ~= _NIL) then
- r, g, b = init(r), init(g), init(b)
+ if (r or g or b) then
+ r, g, b = (r or 0), (g or 0), (b or 0)
color = rb.lcd_rgbpack(r, g, b)
end
end
@@ -86,21 +78,21 @@ local _clr = {} do
end -- clrset
-- de/increments current color by 'inc' -- optionally color targets by 'r,g,b'
- local function clrinc(current, inc, r, g, b)
+ _clr.inc = function(current, inc, r, g, b)
local color = 0
current = current or color
inc = inc or 1
if IS_COLOR_TARGET then
local ru, gu, bu = rb.lcd_rgbunpack(current);
- if (r ~= _NIL or g ~= _NIL or b ~= _NIL) then
- r, g, b = init(r), init(g), init(b)
+ if (r or g or b) then
+ r, g, b = (r or 0), (g or 0), (b or 0)
ru = ru + r; gu = gu + g; bu = bu + b
- color = rb.lcd_rgbpack(ru, gu, bu)
else
- ru = ru + inc; gu = gu + inc; bu = bu + inc
- color = rb.lcd_rgbpack(ru, gu, bu)
+ ru = ru + inc; gu = gu + inc; bu = bu + inc
end
+
+ color = rb.lcd_rgbpack(ru, gu, bu)
else
color = current + inc
end
@@ -108,9 +100,6 @@ local _clr = {} do
return clamp_roll(color, 0, maxstate)
end -- clrinc
- -- expose functions to the outside through _clr table
- _clr.set = clrset
- _clr.inc = clrinc
end -- color functions
return _clr
diff --git a/apps/plugins/lua/include_lua/draw.lua b/apps/plugins/lua/include_lua/draw.lua
index 4409ab7838..7b239339ff 100644
--- a/apps/plugins/lua/include_lua/draw.lua
+++ b/apps/plugins/lua/include_lua/draw.lua
@@ -52,35 +52,39 @@ if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
local _draw = {} do
+ local rocklib_image = getmetatable(rb.lcd_framebuffer())
+ 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 function set_viewport(vp)
- if not vp then rb.set_viewport() return end
- if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
- --vp.drawmode = bit.bxor(vp.drawmode, 4)
- vp.fg_pattern = 3 - vp.fg_pattern
- vp.bg_pattern = 3 - vp.bg_pattern
- end
- rb.set_viewport(vp)
- end
+ 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
- local function line(img, x1, y1, x2, y2, color, bClip)
- img:line(x1, y1, x2, y2, color, bClip)
+ _draw.line = function(img, x1, y1, x2, y2, color, bClip)
+ _line(img, x1, y1, x2, y2, color, bClip)
end
-- horizontal line; x, y define start point; length in horizontal direction
local function hline(img, x, y , length, color, bClip)
- img:line(x, y, x + length, _NIL, color, bClip)
+ _line(img, x, y, x + length, _NIL, color, bClip)
end
-- vertical line; x, y define start point; length in vertical direction
local function vline(img, x, y , length, color, bClip)
- img:line(x, y, _NIL, y + length, color, bClip)
+ _line(img, x, y, _NIL, y + length, color, bClip)
end
-- draws a non-filled figure based on points in t-points
@@ -100,7 +104,7 @@ local _draw = {} do
local pt2 = t_points[i + 1] or pt_first_last-- first and last point
- img:line(pt1[1] + x, pt1[2] + y, pt2[1]+x, pt2[2]+y, color, bClip)
+ _line(img, pt1[1] + x, pt1[2] + y, pt2[1] + x, pt2[2] + y, color, bClip)
end
end
@@ -109,90 +113,80 @@ local _draw = {} do
local function rect(img, x, y, width, height, color, bClip)
if width == 0 or height == 0 then return end
- local ppt = {{0, 0}, {width, 0}, {width, height}, {0, height}}
- polyline(img, x, y, ppt, color, true, bClip)
- --[[
- vline(img, x, y, height, color, bClip);
- vline(img, x + width, y, height, color, bClip);
- hline(img, x, y, width, color, bClip);
- hline(img, x, y + height, width, color, bClip);]]
+ polyline(img, x, y, {{0, 0}, {width, 0}, {width, height}, {0, height}}, color, true, bClip)
+
end
-- filled rect, fillcolor is color if left empty
- local function rect_filled(img, x, y, width, height, color, fillcolor, bClip)
+ _draw.rect_filled = function(img, x, y, width, height, color, fillcolor, bClip)
if width == 0 or height == 0 then return end
if not fillcolor then
- img:clear(color, x, y, x + width, y + height, bClip)
+ _clear(img, color, x, y, x + width, y + height, bClip)
else
- img:clear(fillcolor, x, y, x + width, y + height, bClip)
+ _clear(img, fillcolor, x, y, x + width, y + height, bClip)
rect(img, x, y, width, height, color, bClip)
end
end
-- circle cx,cy define center point
- local function circle(img, cx, cy, radius, color, bClip)
+ _draw.circle = function(img, cx, cy, radius, color, bClip)
local r = radius
- img:ellipse(cx - r, cy - r, cx + r, cy + r, color, _NIL, bClip)
+ _ellipse(img, cx - r, cy - r, cx + r, cy + r, color, _NIL, bClip)
end
-- filled circle cx,cy define center, fillcolor is color if left empty
- local function circle_filled(img, cx, cy, radius, color, fillcolor, bClip)
+ _draw.circle_filled = function(img, cx, cy, radius, color, fillcolor, bClip)
fillcolor = fillcolor or color
local r = radius
- img:ellipse(cx - r, cy - r, cx + r, cy + r, color, fillcolor, bClip)
+ _ellipse(img, cx - r, cy - r, cx + r, cy + r, color, fillcolor, bClip)
end
-- ellipse that fits into defined rect
- local function ellipse_rect(img, x1, y1, x2, y2, color, bClip)
- img:ellipse(x1, y1, x2, y2, color, _NIL, bClip)
+ _draw.ellipse_rect = function(img, x1, y1, x2, y2, color, bClip)
+ _ellipse(img, x1, y1, x2, y2, color, _NIL, bClip)
end
--ellipse that fits into defined rect, fillcolor is color if left empty
- local function ellipse_rect_filled(img, x1, y1, x2, y2, color, fillcolor, bClip)
+ _draw.ellipse_rect_filled = function(img, x1, y1, x2, y2, color, fillcolor, bClip)
if not fillcolor then fillcolor = color end
- img:ellipse(x1, y1, x2, y2, color, fillcolor, bClip)
+ _ellipse(img, x1, y1, x2, y2, color, fillcolor, bClip)
end
-- ellipse cx, cy define center point; a, b the major/minor axis
- local function ellipse(img, cx, cy, a, b, color, bClip)
- img:ellipse(cx - a, cy - b, cx + a, cy + b, color, _NIL, bClip)
+ _draw.ellipse = function(img, cx, cy, a, b, color, bClip)
+ _ellipse(img, cx - a, cy - b, cx + a, cy + b, color, _NIL, bClip)
end
-- filled ellipse cx, cy define center point; a, b the major/minor axis
-- fillcolor is color if left empty
- local function ellipse_filled(img, cx, cy, a, b, color, fillcolor, bClip)
+ _draw.ellipse_filled = function(img, cx, cy, a, b, color, fillcolor, bClip)
if not fillcolor then fillcolor = color end
- img:ellipse(cx - a, cy - b, cx + a, cy + b, color, fillcolor, bClip)
+ _ellipse(img, cx - a, cy - b, cx + a, cy + b, color, fillcolor, bClip)
end
-- rounded rectangle
local function rounded_rect(img, x, y, w, h, radius, color, bClip)
local c_img
-
- local function blit(dx, dy, sx, sy, ox, oy)
- img:copy(c_img, dx, dy, sx, sy, ox, oy, bClip, BSAND, color)
- end
-
if w == 0 or h == 0 then return end
-- limit the radius of the circle otherwise it will overtake the rect
- radius = math.min(w / 2, radius)
- radius = math.min(h / 2, radius)
+ radius = _min(w / 2, radius)
+ radius = _min(h / 2, radius)
local r = radius
- c_img = rb.new_image(r * 2 + 1, r * 2 + 1)
- c_img:clear(0)
- circle(c_img, r + 1, r + 1, r, 0xFFFFFF)
+ c_img = _newimg(r * 2 + 1, r * 2 + 1)
+ _clear(c_img, 0)
+ _ellipse(c_img, 1, 1, 1 + r + r, 1 + r + r, 0x1, _NIL, bClip)
-- copy 4 pieces of circle to their respective corners
- blit(x, y, _NIL, _NIL, r + 1, r + 1) --TL
- blit(x + w - r - 2, y, r, _NIL, r + 1, r + 1) --TR
- blit(x , y + h - r - 2, _NIL, r, r + 1, _NIL) --BL
- blit(x + w - r - 2, y + h - r - 2, r, r, r + 1, r + 1)--BR
+ _copy(img, c_img, x, y, _NIL, _NIL, r + 1, r + 1, bClip, BSAND, color) --TL
+ _copy(img, c_img, x + w - r - 2, y, r, _NIL, r + 1, r + 1, bClip, BSAND, color) --TR
+ _copy(img, c_img, x , y + h - r - 2, _NIL, r, r + 1, _NIL, bClip, BSAND, color) --BL
+ _copy(img, c_img, x + w - r - 2, y + h - r - 2, r, r, r + 1, r + 1, bClip, BSAND, color)--BR
c_img = _NIL
vline(img, x, y + r, h - r * 2, color, bClip);
@@ -202,38 +196,34 @@ local _draw = {} do
end
-- rounded rectangle fillcolor is color if left empty
- local function rounded_rect_filled(img, x, y, w, h, radius, color, fillcolor, bClip)
+ _draw.rounded_rect_filled = function(img, x, y, w, h, radius, color, fillcolor, bClip)
local c_img
- local function blit(dx, dy, sx, sy, ox, oy)
- img:copy(c_img, dx, dy, sx, sy, ox, oy, bClip, BSAND, fillcolor)
- end
-
if w == 0 or h == 0 then return end
if not fillcolor then fillcolor = color end
-- limit the radius of the circle otherwise it will overtake the rect
- radius = math.min(w / 2, radius)
- radius = math.min(h / 2, radius)
+ radius = _min(w / 2, radius)
+ radius = _min(h / 2, radius)
local r = radius
- c_img = rb.new_image(r * 2 + 1, r * 2 + 1)
- c_img:clear(0)
- circle_filled(c_img, r + 1, r + 1, r, fillcolor)
+ c_img = _newimg(r * 2 + 1, r * 2 + 1)
+ _clear(c_img, 0)
+ _ellipse(c_img, 1, 1, 1 + r + r, 1 + r + r, 0x1, 0x1, bClip)
-- copy 4 pieces of circle to their respective corners
- blit(x, y, _NIL, _NIL, r + 1, r + 1) --TL
- blit(x + w - r - 2, y, r, _NIL, r + 1, r + 1) --TR
- blit(x, y + h - r - 2, _NIL, r, r + 1, _NIL) --BL
- blit(x + w - r - 2, y + h - r - 2, r, r, r + 1, r + 1) --BR
+ _copy(img, c_img, x, y, _NIL, _NIL, r + 1, r + 1, bClip, BSAND, fillcolor) --TL
+ _copy(img, c_img, x + w - r - 2, y, r, _NIL, r + 1, r + 1, bClip, BSAND, fillcolor) --TR
+ _copy(img, c_img, x, y + h - r - 2, _NIL, r, r + 1, _NIL, bClip, BSAND, fillcolor) --BL
+ _copy(img, c_img, x + w - r - 2, y + h - r - 2, r, r, r + 1, r + 1, bClip, BSAND, fillcolor) --BR
c_img = _NIL
-- finish filling areas circles didn't cover
- img:clear(fillcolor, x + r, y, x + w - r, y + h - 1, bClip)
- img:clear(fillcolor, x, y + r, x + r, y + h - r, bClip)
- img:clear(fillcolor, x + w - r, y + r, x + w - 1, y + h - r - 1, bClip)
+ _clear(img, fillcolor, x + r, y, x + w - r, y + h - 1, bClip)
+ _clear(img, fillcolor, x, y + r, x + r, y + h - r, bClip)
+ _clear(img, fillcolor, x + w - r, y + r, x + w - 1, y + h - r - 1, bClip)
if fillcolor ~= color then
rounded_rect(img, x, y, w, h, r, color, bClip)
@@ -241,23 +231,23 @@ local _draw = {} do
end
-- draws an image at xy coord in dest image
- local function image(dst, src, x, y, bClip)
+ _draw.image = function(dst, src, x, y, bClip)
if not src then --make sure an image was passed, otherwise bail
rb.splash(rb.HZ, "No Image!")
return _NIL
end
- dst:copy(src, x, y, 1, 1, _NIL, _NIL, bClip)
+ _copy(dst, src, x, y, 1, 1, _NIL, _NIL, bClip)
end
-- floods an area of targetclr with fillclr x, y specifies the start seed
- function flood_fill(img, x, y, targetclr, fillclr)
+ _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 ~= img:get(x,y, true) then return end
+ if targetclr == fillclr or targetclr ~= _get(img, x, y, true) then return end
local max_w = img:width()
local max_h = img:height()
@@ -271,21 +261,20 @@ local _draw = {} do
-- y coordinates are in even indices
local qtail = 0
- local iter_n; -- North iteration
- local iter_s; -- South iteration
local function check_ns(val, x, y)
if targetclr == val then
- if targetclr == iter_n() then
+ y = y - 1
+ if targetclr == _get(img, x, y, true) then -- north
qtail = qtail + 2
qpt[qtail - 1] = x
- qpt[qtail] = (y - 1)
+ qpt[qtail] = y
end
-
- if targetclr == iter_s() then
+ y = y + 2
+ if targetclr == _get(img, x, y, true) then -- south
qtail = qtail + 2
qpt[qtail - 1] = x
- qpt[qtail] = (y + 1)
+ qpt[qtail] = y
end
return fillclr
end
@@ -293,17 +282,12 @@ local _draw = {} do
end
local function seed_pt(x, y)
- -- will never hit max_w * max_h^2 but make sure not to end early
- for qhead = 2, max_w * max_h * max_w * max_h, 2 do
-
- if targetclr == img:get(x, y, true) then
- iter_n = img:points(x, y - 1, 1, y - 1)
- iter_s = img:points(x, y + 1, 1, y + 1)
- img:marshal(x, y, 1, y, _NIL, _NIL, true, check_ns)
+ -- should never hit max but make sure not to end early
+ for qhead = 2, 0x40000000, 2 do
- iter_n = img:points(x + 1, y - 1, max_w, y - 1)
- iter_s = img:points(x + 1, y + 1, max_w, y + 1)
- img:marshal(x + 1, y, max_w, y, _NIL, _NIL, true, check_ns)
+ 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]
@@ -320,7 +304,7 @@ local _draw = {} do
end -- flood_fill
-- draws a closed figure based on points in t_points
- local function polygon(img, x, y, t_points, color, fillcolor, bClip)
+ _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
@@ -335,35 +319,41 @@ local _draw = {} do
if pt[2] < y_min then y_min = pt[2] end
if pt[2] > y_max then y_max = pt[2] end
end
- w = math.abs(x_max) + math.abs(x_min)
- h = math.abs(y_max) + math.abs(y_min)
+ 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 = rb.new_image(w + 3, h + 3)
- fill_img:clear(0xFFFFFF)
+ 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
- fill_img:line(pt1[1] - x_min, pt1[2] - y_min,
+ _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() , 1, 0)
- img:copy(fill_img, x - 1, y - 1, _NIL, _NIL, _NIL, _NIL, bClip, BSAND, fillcolor)
+ _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
- local function text(img, x, y, width, height, font, color, text)
+ _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 = 0xFFFFFF, bg_pattern = 0}
- set_viewport(opts)
+ 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)
+ vp.fg_pattern = 3 - vp.fg_pattern
+ vp.bg_pattern = 3 - vp.bg_pattern
+ end
+ rb.set_viewport(opts)
local res, w, h = rb.font_getstringsize(text, font)
@@ -380,8 +370,8 @@ local _draw = {} do
end
-- make a copy of the current screen for later
- local screen_img = rb.new_image(LCD_W, LCD_H)
- screen_img:copy(_LCD)
+ 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
@@ -391,10 +381,6 @@ local _draw = {} do
-- we will be printing the text to the screen then blitting into img
rb.lcd_clear_display()
- local function blit(dx, dy)
- img:copy(_LCD, dx, dy, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
- end
-
if w > LCD_W then -- text is too long for the screen do it in chunks
local l = 1
local resp, wp, hp
@@ -417,7 +403,7 @@ local _draw = {} do
end
-- using the mask we made blit color into img
- blit(x + width, y + height)
+ _copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
x = x + wp
rb.lcd_clear_display()
lenr = text:len()
@@ -426,43 +412,20 @@ local _draw = {} do
rb.lcd_putsxy(0, 0, text)
-- using the mask we made blit color into img
- blit(x + width, y + height)
+ _copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
end
- _LCD:copy(screen_img) -- restore screen
- set_viewport() -- set viewport default
+ _copy(_LCD, screen_img) -- restore screen
+ rb.set_viewport() -- set viewport default
return res, w, h
end
- -- expose functions to the outside through _draw table
- _draw.image = image
- _draw.text = text
- _draw.line = line
+ -- expose internal functions to the outside through _draw table
_draw.hline = hline
_draw.vline = vline
- _draw.polygon = polygon
_draw.polyline = polyline
_draw.rect = rect
- _draw.circle = circle
- _draw.ellipse = ellipse
- _draw.flood_fill = flood_fill
- _draw.ellipse_rect = ellipse_rect
_draw.rounded_rect = rounded_rect
- -- filled functions use color as fillcolor if fillcolor is left empty...
- _draw.rect_filled = rect_filled
- _draw.circle_filled = circle_filled
- _draw.ellipse_filled = ellipse_filled
- _draw.ellipse_rect_filled = ellipse_rect_filled
- _draw.rounded_rect_filled = rounded_rect_filled
-
- -- adds the above _draw functions into the metatable for RLI_IMAGE
- local ex = getmetatable(rb.lcd_framebuffer())
- for k, v in pairs(_draw) do
- if ex[k] == _NIL then
- ex[k] = v
- end
- end
-
end -- _draw functions
return _draw
diff --git a/apps/plugins/lua/include_lua/image.lua b/apps/plugins/lua/include_lua/image.lua
index c8d7aad9dd..2b2a1ce19f 100644
--- a/apps/plugins/lua/include_lua/image.lua
+++ b/apps/plugins/lua/include_lua/image.lua
@@ -65,35 +65,43 @@ if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
local _img = {} do
+ local rocklib_image = getmetatable(rb.lcd_framebuffer())
+ setmetatable(_img, rocklib_image)
+
-- internal constants
local _NIL = nil -- _NIL placeholder
local _math = require("math_ex") -- math functions needed
local LCD_W, LCD_H = rb.LCD_WIDTH, rb.LCD_HEIGHT
+ local _copy = rocklib_image.copy
+ local _get = rocklib_image.get
+ local _marshal = rocklib_image.marshal
+ local _points = rocklib_image.points
+
-- returns new image -of- img sized to fit w/h tiling to fit if needed
- local function tile(img, w, h)
+ _img.tile = function(img, w, h)
local hs , ws = img:height(), img:width()
local t_img = rb.new_image(w, h)
- for x = 1, w, ws do t_img:copy(img, x, 1, 1, 1) end
- for y = hs, h, hs do t_img:copy(t_img, 1, y, 1, 1, w, hs) end
+ for x = 1, w, ws do _copy(t_img, img, x, 1, 1, 1) end
+ for y = hs, h, hs do _copy(t_img, t_img, 1, y, 1, 1, w, hs) end
return t_img
end
-- resizes src to size of dst
- local function resize(dst, src)
+ _img.resize = function(dst, src)
-- simple nearest neighbor resize derived from rockbox - pluginlib_bmp.c
-- pretty rough results highly recommend building one more suited..
local dw, dh = dst:width(), dst:height()
- local xstep = (bit.lshift(src:width(),8) / (dw)) + 1
- local ystep = (bit.lshift(src:height(),8) / (dh))
+ local xstep = (bit.lshift(src:width(), 8) / (dw)) + 1
+ local ystep = (bit.lshift(src:height(), 8) / (dh))
local xpos, ypos = 0, 0
local src_x, src_y
-- walk the dest get src pixel
- function rsz_trans(val, x, y)
+ local function rsz_trans(val, x, y)
if x == 1 then
src_y = bit.rshift(ypos,8) + 1
xpos = xstep - bit.rshift(xstep,4) + 1
@@ -101,14 +109,14 @@ local _img = {} do
end
src_x = bit.rshift(xpos,8) + 1
xpos = xpos + xstep
- return (src:get(src_x, src_y, true) or 0)
+ return (_get(src, src_x, src_y, true) or 0)
end
--/* (dst*, [x1, y1, x2, y2, dx, dy, clip, function]) */
- dst:marshal(1, 1, dw, dh, _NIL, _NIL, false, rsz_trans)
+ _marshal(dst, 1, 1, dw, dh, _NIL, _NIL, false, rsz_trans)
end
-- returns new image -of- img rotated in whole degrees 0 - 360
- local function rotate(img, degrees)
+ _img.rotate = function(img, degrees)
-- we do this backwards as if dest was the unrotated object
degrees = 360 - degrees
local c, s = _math.d_cos(degrees), _math.d_sin(degrees)
@@ -133,7 +141,7 @@ local _img = {} do
|_____| |_______| |_______| ]]
-- walk the dest get translated src pixel, oversamples src to fill gaps
- function rot_trans(val, x, y)
+ local function rot_trans(val, x, y)
-- move center x/y to the origin
local xtran = x - d_xctr;
local ytran = y - d_yctr;
@@ -142,68 +150,56 @@ local _img = {} do
local yrot = ((xtran * s) + (ytran * c)) / 10000 + s_yctr
local xrot = ((xtran * c) - (ytran * s)) / 10000 + s_xctr
-- upper left of src image back to origin, copy src pixel
- return img:get(xrot, yrot, true) or 0
+ return _get(img, xrot, yrot, true) or 0
end
- r_img:marshal(1, 1, dw, dh, _NIL, _NIL, false, rot_trans)
+ _marshal(r_img, 1, 1, dw, dh, _NIL, _NIL, false, rot_trans)
return r_img
end
-- saves img to file: name
- local function save(img, 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 dump_fbuffer(thresh)
- if #fbuffer >= thresh then
- file:write(table.concat(fbuffer))
- for i=1, #fbuffer do fbuffer[i] = _NIL end -- reuse table
- end
- end
-
local function s_bytesLE(bits, value)
-- bits must be multiples of 8 (sizeof byte)
local byte
- local result = ""
- for b = 1, bit.rshift(bits, 3) do
+ local nbytes = bit.rshift(bits, 3)
+ for b = 1, nbytes do
if value > 0 then
byte = value % 256
value = (value - byte) / 256
- result = result .. string.char(byte)
else
- result = result .. string.char(0)
+ byte = 0
end
+ bbuffer[b] = string.char(byte)
end
- return result
+ 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 result = ""
- for b = 1, bit.rshift(bits, 3) do
+ local nbytes = bit.rshift(bits, 3)
+ for b = nbytes, 1, -1 do
if value > 0 then
byte = value % 256
value = (value - byte) / 256
- result = string.char(byte) .. result
else
- result = string.char(0) .. result
+ byte = 0
end
+ bbuffer[b] = string.char(byte)
end
- return result
- end
-
- local function c_cmp(color, shift)
- -- [RR][GG][BB]
- return bit.band(bit.rshift(color, shift), 0xFF)
+ return table.concat(bbuffer, _NIL, 1, nbytes)
end
- local cmp = {["r"] = function(c) return c_cmp(c, 16) end,
- ["g"] = function(c) return c_cmp(c, 08) end,
- ["b"] = function(c) return c_cmp(c, 00) 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))..
@@ -314,7 +310,8 @@ local _img = {} do
end
end
- dump_fbuffer(0) -- write the header to the file now
+ 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
@@ -327,18 +324,23 @@ local _img = {} do
end
-- Bitmap lines start at bottom unless biHeight is negative
- for point in img:points(1, h, w + bytesleft, 1) do
+ for point in _points(img, 1, h, w + bytesleft, 1) do
imgdata[#imgdata + 1] = fs_bytes_E(bpp, point or 0)
- dump_fbuffer(31) -- buffered write, increase # for performance
- end
- dump_fbuffer(0) --write leftovers to file
+ 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
- local function search(img, x1, y1, x2, y2, targetclr, variation, stepx, stepy)
+ _img.search = function(img, x1, y1, x2, y2, targetclr, variation, stepx, stepy)
if variation > 128 then variation = 128 end
if variation < -128 then variation = -128 end
@@ -352,7 +354,7 @@ local _img = {} do
targetl = swap
end
- for point, x, y in img:points(x1, y1, x2, y2, stepx, stepy) do
+ for point, x, y in _points(img, x1, y1, x2, y2, stepx, stepy) do
if point >= targetl and point <= targeth then
return point, x, y
end
@@ -362,12 +364,12 @@ local _img = {} do
--[[ we won't be extending these into RLI_IMAGE]]
-- creates a new rbimage size w x h
- local function new(w, h)
+ _img.new = function(w, h)
return rb.new_image(w, h)
end
-- returns new image -of- file: name (_NIL if error)
- local function load(name)
+ _img.load = function(name)
return rb.read_bmp_file("/" .. name)
end
@@ -381,23 +383,6 @@ local _img = {} do
_img.RLI_INFO_DEPTH = 0x6
_img.RLI_INFO_FORMAT = 0x7
_img.RLI_INFO_ADDRESS = 0x8
-
- -- expose functions to the outside through _img table
- _img.save = save
- _img.search = search
- _img.rotate = rotate
- _img.resize = resize
- _img.tile = tile
-
- -- adds the above _img functions into the metatable for RLI_IMAGE
- local ex = getmetatable(rb.lcd_framebuffer())
- for k, v in pairs(_img) do
- if ex[k] == _NIL then ex[k] = v end
- end
- -- not exposed through RLI_IMAGE
- _img.new = new
- _img.load = load
-
end -- _img functions
return _img
diff --git a/apps/plugins/lua/include_lua/lcd.lua b/apps/plugins/lua/include_lua/lcd.lua
index e0a3c5895a..f4bb0db695 100644
--- a/apps/plugins/lua/include_lua/lcd.lua
+++ b/apps/plugins/lua/include_lua/lcd.lua
@@ -51,7 +51,6 @@ local _lcd = {} do
--internal constants
local _NIL = nil -- _NIL placeholder
- local LCD_W, LCD_H = rb.LCD_WIDTH, rb.LCD_HEIGHT
-- clamps value to >= min and <= max
local function clamp(val, min, max)
@@ -65,21 +64,21 @@ local _lcd = {} do
end
-- return a copy of lcd screen
- local function duplicate(t, screen_img)
+ _lcd.duplicate = function(t, screen_img)
screen_img = screen_img or rb.new_image()
screen_img:copy(rb.lcd_framebuffer())
return screen_img
end
-- updates screen in specified rectangle
- local function update_rect(t, x, y, w, h)
+ _lcd.update_rect = function(t, x, y, w, h)
rb.lcd_update_rect(x - 1, y - 1,
- clamp(x + w, 1, LCD_W) - 1,
- clamp(y + h, 1, LCD_H) - 1)
+ clamp(x + w, 1, rb.LCD_WIDTH) - 1,
+ clamp(y + h, 1, rb.LCD_HEIGHT) - 1)
end
-- clears lcd, optional.. ([color, x1, y1, x2, y2, clip])
- local function clear(t, clr, ...)
+ _lcd.clear = function(t, clr, ...)
rb.lcd_scroll_stop() --rb really doesn't like bg change while scroll
if clr == _NIL and ... == _NIL then
rb.lcd_clear_display()
@@ -89,7 +88,7 @@ local _lcd = {} do
end
-- loads an image to the screen
- local function image(t, src, x, y)
+ _lcd.image = function(t, src, x, y)
if not src then --make sure an image was passed, otherwise bail
rb.splash(rb.HZ, "No Image!")
return _NIL
@@ -98,19 +97,19 @@ local _lcd = {} do
end
-- Formattable version of splash
- local function splashf(t, timeout, ...)
+ _lcd.splashf = function(t, timeout, ...)
rb.splash(timeout, string.format(...))
end
-- Gets size of text
- local function text_extent(t, msg, font)
+ _lcd.text_extent = function(t, msg, font)
font = font or rb.FONT_UI
return rb.font_getstringsize(msg, font)
end
-- Sets viewport size
- local function set_viewport(t, vp)
+ _lcd.set_viewport = function(t, vp)
if not vp then rb.set_viewport() return end
if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
--vp.drawmode = bit.bxor(vp.drawmode, 4)
@@ -133,14 +132,17 @@ local _lcd = {} do
end
--expose functions to the outside through _lcd table
+--[[
_lcd.text_extent = text_extent
_lcd.set_viewport = set_viewport
_lcd.duplicate = duplicate
- _lcd.update = rb.lcd_update
+ _lcd.update = _update
_lcd.update_rect = update_rect
_lcd.clear = clear
_lcd.splashf = splashf
_lcd.image = image
+]]
+ _lcd.update = rb.lcd_update
_lcd.DEPTH = rb.LCD_DEPTH
_lcd.W = rb.LCD_WIDTH
_lcd.H = rb.LCD_HEIGHT
diff --git a/apps/plugins/lua/include_lua/print.lua b/apps/plugins/lua/include_lua/print.lua
index 9b21dafb9d..3a11e7c3dc 100644
--- a/apps/plugins/lua/include_lua/print.lua
+++ b/apps/plugins/lua/include_lua/print.lua
@@ -49,13 +49,12 @@ local _print = {} do
local _NIL = nil -- _NIL placeholder
local _LCD = rb.lcd_framebuffer()
- local LCD_W, LCD_H = rb.LCD_WIDTH, rb.LCD_HEIGHT
local WHITE = _clr.set(-1, 255, 255, 255)
local BLACK = _clr.set(0, 0, 0, 0)
local DRMODE_SOLID = 3
local col_buf, s_lines = {}, {}
local _p_opts = _NIL
-
+ local tabstring = string.rep(" ", 2)
-- print internal helper functions
--------------------------------------------------------------------------------
-- clamps value to >= min and <= max
@@ -69,46 +68,20 @@ local _print = {} do
return max
end
- -- updates screen in specified rectangle
- local function update_rect(x, y, w, h)
- rb.lcd_update_rect(x - 1, y - 1,
- clamp(x + w, 1, LCD_W) - 1,
- clamp(y + h, 1, LCD_H) - 1)
- end
-
-- Gets size of text
local function text_extent(msg, font)
- font = font or rb.FONT_UI
-- res, w, h
- return rb.font_getstringsize(msg, font)
- end
-
- -- Sets viewport size
- local function set_viewport(vp)
- if not vp then rb.set_viewport() return end
-
- if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
- --vp.drawmode = bit.bxor(vp.drawmode, 4)
- vp.fg_pattern = 3 - vp.fg_pattern
- vp.bg_pattern = 3 - vp.bg_pattern
- end
- rb.set_viewport(vp)
- end
-
- -- shallow copy of table
- function table_clone(t)
- local copy = {}
- for k, v in pairs(t) do
- copy[k] = v
- end
- return copy
+ return rb.font_getstringsize(msg, font or rb.FONT_UI)
end
-- Updates a single line on the screen
local function update_line(enabled, opts, line, h)
if enabled ~= true then return end
local o = opts or _p_opts
- update_rect(o.x, o.y + line * h + 1, o.width, h)
+ -- updates screen in specified rectangle
+ rb.lcd_update_rect(o.x - 1, o.y + line * h,
+ clamp(o.x + o.width, 1, rb.LCD_WIDTH) - 1,
+ clamp(o.y + line * h + 1 + h, 1, rb.LCD_HEIGHT) - 1)
end
-- Clears a single line on the screen
@@ -129,22 +102,23 @@ local _print = {} do
local function col_buf_insert(msg, line, _p_opts)
--if _p_opts.line <= 1 then col_buf = {} end
if not col_buf[line] then
- table.insert(col_buf, line, msg) end
+ table.insert(col_buf, line, msg)
+ end
end
- --replaces / strips escape characters
+ --replaces / strips tab characters
local function check_escapes(o, msg)
+--[[ --for replacing a variety of escapes
local tabsz = 2
local tabstr = string.rep(" ", tabsz)
-
local function repl(esc)
local ret = ""
if esc:sub(1,1) == "\t" then ret = string.rep(tabstr, esc:len()) end
return ret
end
-
msg = msg:gsub("(%c+)", repl)
-
+]]
+ msg = msg:gsub("\t", tabstring)
local res, w, h = text_extent(msg, o.font)
return w, h, msg
end
@@ -154,8 +128,8 @@ local _print = {} do
local function set_defaults()
_p_opts = { x = 1,
y = 1,
- width = LCD_W - 1,
- height = LCD_H - 1,
+ width = rb.LCD_WIDTH - 1,
+ height = rb.LCD_HEIGHT - 1,
font = rb.FONT_UI,
drawmode = DRMODE_SOLID,
fg_pattern = WHITE,
@@ -178,7 +152,15 @@ local _print = {} do
-- if bByRef is _NIL or false then a copy is returned
local function get_settings(bByRef)
_p_opts = _p_opts or set_defaults()
- if not bByRef then return table_clone(_p_opts) end
+ if not bByRef then
+ -- shallow copy of table
+ local copy = {}
+ for k, v in pairs(_p_opts) do
+ copy[k] = v
+ end
+ return copy
+ end
+
return _p_opts
end
@@ -216,7 +198,16 @@ local _print = {} do
-- alternative selection method
--msg = "> " .. msg
end
- set_viewport(o)
+
+ if not o then rb.set_viewport() return end
+
+ if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
+ o.fg_pattern = 3 - o.fg_pattern
+ o.bg_pattern = 3 - o.bg_pattern
+ end
+
+ rb.set_viewport(o)
+
o = _NIL
end
@@ -251,8 +242,8 @@ local _print = {} do
-- sets print area
local function set_area(x, y, w, h)
local o = get_settings(true)
- o.x, o.y = clamp(x, 1, LCD_W), clamp(y, 1, LCD_H)
- o.width, o.height = clamp(w, 1, LCD_W - o.x), clamp(h, 1, LCD_H - o.y)
+ o.x, o.y = clamp(x, 1, rb.LCD_WIDTH), clamp(y, 1, rb.LCD_HEIGHT)
+ o.width, o.height = clamp(w, 1, rb.LCD_WIDTH - o.x), clamp(h, 1, rb.LCD_HEIGHT - o.y)
o.max_line = max_lines(_p_opts)
clear()
@@ -312,6 +303,7 @@ local _print = {} do
local o = get_settings(true)
local w, h, msg
local line = o.line - 1 -- rb is 0-based lua is 1-based
+
if not (...) or (...) == "\n" then -- handles blank line / single '\n'
local res, w, h = text_extent(" ", o.font)
diff --git a/apps/plugins/lua/rocklib_img.c b/apps/plugins/lua/rocklib_img.c
index 083679cdbb..35c2699b5d 100644
--- a/apps/plugins/lua/rocklib_img.c
+++ b/apps/plugins/lua/rocklib_img.c
@@ -57,10 +57,6 @@
#define ABS(a)(((a) < 0) ? - (a) :(a))
#endif
-#ifndef LCD_BLACK
-#define LCD_BLACK 0x0
-#endif
-
struct rocklua_image
{
int width;
@@ -68,18 +64,18 @@ struct rocklua_image
int stride;
size_t elems;
fb_data *data;
- fb_data dummy[1];
+ fb_data dummy[1][1];
};
/* holds iterator data for rlimages */
struct rli_iter_d
{
- struct rocklua_image *img;
- fb_data *elem;
int x , y;
int x1, y1;
int x2, y2;
int dx, dy;
+ fb_data *elem;
+ struct rocklua_image *img;
};
/* __tostring information enums */
@@ -92,8 +88,8 @@ enum rli_info {RLI_INFO_ALL = 0, RLI_INFO_TYPE, RLI_INFO_WIDTH,
static inline fb_data invert_color(fb_data rgb)
{
uint8_t r = 0xFFU - FB_UNPACK_RED(rgb);
- uint8_t g = 0xFFU - FB_UNPACK_RED(rgb);
- uint8_t b = 0xFFU - FB_UNPACK_RED(rgb);
+ uint8_t g = 0xFFU - FB_UNPACK_GREEN(rgb);
+ uint8_t b = 0xFFU - FB_UNPACK_BLUE(rgb);
return FB_RGBPACK(r, g, b);
}
@@ -244,17 +240,45 @@ static inline void swap_int(bool swap, int *v1, int *v2)
static void bounds_check_xy(lua_State *L, struct rocklua_image *img,
int nargx, int x, int nargy, int y)
{
- luaL_argcheck(L, x <= img->width && x > 0, nargx, ERR_IDX_RANGE);
- luaL_argcheck(L, y <= img->height && y > 0, nargy, ERR_IDX_RANGE);
+ int narg;
+
+ if(x > img->width || x < 1)
+ narg = nargx;
+ else if(y <= img->height && y > 0)
+ return; /* note -- return if no error */
+ else
+ narg = nargy;
+
+ luaL_argerror(L, narg, ERR_IDX_RANGE);
} /* bounds_check_xy */
static struct rocklua_image* rli_checktype(lua_State *L, int arg)
{
- void *ud = luaL_checkudata(L, arg, ROCKLUA_IMAGE);
+#if 0
+ return (struct rocklua_image*) luaL_checkudata(L, arg, ROCKLUA_IMAGE);
+#else /* cache result */
+ static struct rocklua_image* last = NULL;
+ void *ud = lua_touserdata(L, arg);
- luaL_argcheck(L, ud != NULL, arg, "'" ROCKLUA_IMAGE "' expected");
+ if(ud != NULL)
+ {
+ if(ud == last)
+ return last;
+ else if (lua_getmetatable(L, arg))
+ { /* does it have a metatable? */
+ luaL_getmetatable(L, ROCKLUA_IMAGE); /* get correct metatable */
+ if (lua_rawequal(L, -1, -2))
+ { /* does it have the correct mt? */
+ lua_pop(L, 2); /* remove both metatables */
+ last = (struct rocklua_image*) ud;
+ return last;
+ }
+ }
+ }
- return (struct rocklua_image*) ud;
+ luaL_typerror(L, arg, ROCKLUA_IMAGE); /* else error */
+ return NULL; /* to avoid warnings */
+#endif
} /* rli_checktype */
static struct rocklua_image * alloc_rlimage(lua_State *L, bool alloc_data,
@@ -286,7 +310,7 @@ static struct rocklua_image * alloc_rlimage(lua_State *L, bool alloc_data,
/* apparent w/h is stored but behind the scenes native w/h is used */
img->width = width;
img->height = height;
- img->stride = w_native;
+ img->stride = STRIDE_MAIN(w_native, h_native);
img->elems = n_elems;
return img;
@@ -305,14 +329,14 @@ static inline fb_data* rli_alloc(lua_State *L, int width, int height)
/* rliimage is pushed on the stack it is up to you to pop it */
struct rocklua_image *a = alloc_rlimage(L, true, width, height);
- a->data = &a->dummy[0]; /* ref to beginning of alloc'd img data */
+ a->data = &a->dummy[0][0]; /* ref to beginning of alloc'd img data */
return a->data;
} /* rli_alloc */
-static inline fb_data data_setget(fb_data *elem, int x, int y, fb_data *val)
+static inline fb_data data_set(fb_data *elem, int x, int y, fb_data *val)
{
- fb_data old_val = FB_SCALARPACK(0);
+ fb_data old_val;
fb_data new_val;
if(elem)
@@ -328,23 +352,18 @@ static inline fb_data data_setget(fb_data *elem, int x, int y, fb_data *val)
else
pixel_to_fb(x, y, &old_val, &new_val);
}
+ else
+ old_val = FB_SCALARPACK(0);
return old_val;
-} /* data_setget */
-
-static inline fb_data data_set(fb_data *elem, int x, int y, fb_data *new_val)
-{
- /* get and set share the same underlying function */
- return data_setget(elem, x, y, new_val);
} /* data_set */
static inline fb_data data_get(fb_data *elem, int x, int y)
{
- /* get and set share the same underlying function */
- return data_setget(elem, x, y, NULL);
+ return data_set(elem, x, y, NULL);
} /* data_get */
-static fb_data* rli_get_element(struct rocklua_image* img, int x, int y)
+static inline fb_data* rli_get_element(struct rocklua_image* img, int x, int y)
{
int stride = img->stride;
size_t elements = img->elems;
@@ -352,60 +371,61 @@ static fb_data* rli_get_element(struct rocklua_image* img, int x, int y)
pixel_to_native(x, y, &x, &y);
+#if defined(LCD_STRIDEFORMAT) && LCD_STRIDEFORMAT == VERTICAL_STRIDE
+ /* column major address */
+ size_t data_address = (stride * (x - 1)) + (y - 1);
+
+ /* y needs bound between 0 and stride otherwise overflow to prev/next x */
+ if(y <= 0 || y > stride || data_address >= elements)
+ return NULL; /* data overflow */
+#else
/* row major address */
size_t data_address = (stride * (y - 1)) + (x - 1);
/* x needs bound between 0 and stride otherwise overflow to prev/next y */
if(x <= 0 || x > stride || data_address >= elements)
return NULL; /* data overflow */
+#endif
return &data[data_address]; /* return element address */
} /* rli_get_element */
/* Lua to C Interface for pixel set and get functions */
-static int rli_setget(lua_State *L, bool is_get)
+static int rli_setget(lua_State *L, bool is_get, int narg_clip)
{
/*(set) (dst*, [x1, y1, clr, clip]) */
/*(get) (dst*, [x1, y1, clip]) */
struct rocklua_image *a = rli_checktype(L, 1);
- int x = luaL_checkint(L, 2);
- int y = luaL_checkint(L, 3);
-
- fb_data clr = FB_SCALARPACK(0); /* Arg 4 is color if set element */
- fb_data *p_clr = &clr;
+ int x = lua_tointeger(L, 2);
+ int y = lua_tointeger(L, 3);
- int clip_narg;
-
- if(is_get) /* get element */
- {
- p_clr = NULL;
- clip_narg = 4;
- }
- else /* set element */
- {
- clr = FB_SCALARPACK((unsigned) luaL_checknumber(L, 4));
- clip_narg = 5;
- }
+ fb_data clr; /* Arg 4 is color if set element */
fb_data *element = rli_get_element(a, x, y);
if(!element)
{
- if(!luaL_optboolean(L, clip_narg, false)) /* Error if !clip */
+ if(!lua_toboolean(L, narg_clip)) /* Error if !clip */
bounds_check_xy(L, a, 2, x, 3, y);
lua_pushnil(L);
return 1;
}
- lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(data_setget(element, x, y, p_clr)));
+ if(is_get) /* get element */
+ lua_pushinteger(L, FB_UNPACK_SCALAR_LCD(data_get(element, x, y)));
+ else /* set element */
+ {
+ clr = FB_SCALARPACK((unsigned) lua_tointeger(L, 4));
+ lua_pushinteger(L, FB_UNPACK_SCALAR_LCD(data_set(element, x, y, &clr)));
+ }
/* returns old value */
return 1;
} /* rli_setget */
#ifdef RLI_EXTENDED
-static bool init_rli_iter(struct rli_iter_d *d,
+static bool rli_iter_init(struct rli_iter_d *d,
struct rocklua_image *img,
int x1, int y1,
int x2, int y2,
@@ -417,11 +437,11 @@ static bool init_rli_iter(struct rli_iter_d *d,
swap_int((swy), &y1, &y2);
/* stepping in the correct x direction ? */
- if((dx > 0 && x1 > x2) || (dx < 0 && x1 < x2))
+ if((dx ^ (x2 - x1)) < 0)
dx = -dx;
/* stepping in the correct y direction ? */
- if((dy > 0 && y1 > y2) || (dy < 0 && y1 < y2))
+ if((dy ^ (y2 - y1)) < 0)
dy = -dy;
d->img = img;
@@ -436,7 +456,33 @@ static bool init_rli_iter(struct rli_iter_d *d,
d->elem = rli_get_element(img, d->x, d->y);
return(dx != 0 || dy != 0);
-} /* init_rli_iter */
+} /* rli_iter_init */
+
+static struct rli_iter_d * rli_iter_create(lua_State *L)
+{
+ struct rocklua_image *a = rli_checktype(L, 1);
+ int x1 = luaL_optint(L, 2, 1);
+ int y1 = luaL_optint(L, 3, 1);
+ int x2 = luaL_optint(L, 4, a->width);
+ int y2 = luaL_optint(L, 5, a->height);
+ int dx = luaL_optint(L, 6, 1);
+ int dy = luaL_optint(L, 7, 1);
+ bool clip = lua_toboolean(L, 8);
+
+ if(!clip)
+ {
+ bounds_check_xy(L, a, 2, x1, 3, y1);
+ bounds_check_xy(L, a, 4, x2, 5, y2);
+ }
+
+ struct rli_iter_d *ds;
+ /* create new iter + pushed onto stack */
+ ds = (struct rli_iter_d *) lua_newuserdata(L, sizeof(struct rli_iter_d));
+
+ rli_iter_init(ds, a, x1, y1, x2, y2, dx, dy, false, false);
+
+ return ds;
+}
/* steps to the next point(x, y) by delta x/y, stores pointer to element
returns true if x & y haven't reached x2 & y2
@@ -464,11 +510,10 @@ static bool next_rli_iter(struct rli_iter_d *d)
return true;
} /* next_rli_iter */
-static int d_line(struct rocklua_image *img,
- int x1, int y1,
- int x2, int y2,
- fb_data *clr,
- bool clip)
+static void d_line(struct rocklua_image *img,
+ int x1, int y1,
+ int x2, int y2,
+ fb_data *clr)
{
/* NOTE! clr passed as pointer */
/* Bresenham midpoint line algorithm */
@@ -477,8 +522,8 @@ static int d_line(struct rocklua_image *img,
int r_a = x2 - x1; /* range of x direction called 'a' for now */
int r_b = y2 - y1; /* range of y direction called 'b' for now */
- int s_a = 1; /* step of a direction */
- int s_b = 1; /* step of b direction */
+ int s_a = (r_a > 0) - (r_a < 0); /* step of a direction -1, 0, 1 */
+ int s_b = (r_b > 0) - (r_b < 0); /* step of b direction -1, 0, 1 */
int d_err;
int numpixels;
@@ -486,42 +531,28 @@ static int d_line(struct rocklua_image *img,
int *a1 = &x1; /* pointer to the x var 'a' */
int *b1 = &y1; /* pointer to the y var 'b' */
- if(r_a < 0) /* instead of negative range we will switch step instead */
- {
- r_a = -r_a;
- s_a = -s_a;
- }
-
- if(r_b < 0) /* instead of negative range we will switch step instead */
- {
- r_b = -r_b;
- s_b = -s_b;
- }
-
- x2 += s_a; /* add 1 extra point to make the whole line */
- y2 += s_b; /* add 1 extra point */
+ r_a = ABS(r_a);/* instead of negative range we switch step */
+ r_b = ABS(r_b);/* instead of negative range we switch step */
if(r_b > r_a) /*if rangeY('b') > rangeX('a') swap their identities */
{
- a1 = &y1;
- b1 = &x1;
+ a1 = &y1; /* pointer to the y var 'a' */
+ b1 = &x1; /* pointer to the x var 'b' */
swap_int((true), &r_a, &r_b);
swap_int((true), &s_a, &s_b);
}
d_err = ((r_b << 1) - r_a) >> 1; /* preload err of 1 step (px centered) */
+ /* add 1 extra point to make the whole line */
numpixels = r_a + 1;
r_a -= r_b; /* pre-subtract 'a' - 'b' */
- for(;numpixels > 0; numpixels--)
+ for(; numpixels > 0; numpixels--)
{
element = rli_get_element(img, x1, y1);
- if(element || clip)
- data_set(element, x1, y1, clr);
- else
- return numpixels + 1; /* Error */
+ data_set(element, x1, y1, clr);
if(d_err >= 0) /* 0 is our target midpoint(exact point on the line) */
{
@@ -534,73 +565,60 @@ static int d_line(struct rocklua_image *img,
*a1 += s_a; /* whichever axis is in 'a' stepped(-1 or +1) */
}
- return 0;
} /* d_line */
/* ellipse worker function */
-static int d_ellipse_elements(struct rocklua_image * img,
- int x1, int y1,
- int x2, int y2,
- int sx, int sy,
- fb_data *clr,
- fb_data *fillclr,
- bool clip)
+static void d_ellipse_elements(struct rocklua_image * img,
+ int x1, int y1,
+ int x2, int y2,
+ fb_data *clr,
+ fb_data *fillclr)
{
- int ret = 0;
- fb_data *element1, *element2, *element3, *element4;
+ fb_data *element;
- if(fillclr && x1 - sx != x2 + sx)
+ if(fillclr)
{
- ret |= d_line(img, x1, y1, x2, y1, fillclr, clip); /* I. II.*/
- ret |= d_line(img, x1, y2, x2, y2, fillclr, clip); /* III.IV.*/
+ d_line(img, x1, y1, x2, y1, fillclr); /* I. II.*/
+ d_line(img, x1, y2, x2, y2, fillclr); /* III.IV.*/
}
- x1 -= sx; /* shift x & y */
- y1 -= sy;
- x2 += sx;
- y2 += sy;
+ element = rli_get_element(img, x2, y1);
+ data_set(element, x2, y1, clr); /* I. Quadrant +x +y */
- element1 = rli_get_element(img, x2, y1);
- element2 = rli_get_element(img, x1, y1);
- element3 = rli_get_element(img, x1, y2);
- element4 = rli_get_element(img, x2, y2);
+ element = rli_get_element(img, x1, y1);
+ data_set(element, x1, y1, clr); /* II. Quadrant -x +y */
- if(clip || (element1 && element2 && element3 && element4))
- {
- data_set(element1, x2, y1, clr); /* I. Quadrant +x +y */
- data_set(element2, x1, y1, clr); /* II. Quadrant -x +y */
- data_set(element3, x1, y2, clr); /* III. Quadrant -x -y */
- data_set(element4, x2, y2, clr); /* IV. Quadrant +x -y */
- }
- else
- ret = 2; /* ERROR */
+ element = rli_get_element(img, x1, y2);
+ data_set(element, x1, y2, clr); /* III. Quadrant -x -y */
+
+ element = rli_get_element(img, x2, y2);
+ data_set(element, x2, y2, clr); /* IV. Quadrant +x -y */
- return ret;
} /* d_ellipse_elements */
-static int d_ellipse(struct rocklua_image *img,
+static void d_ellipse(struct rocklua_image *img,
int x1, int y1,
int x2, int y2,
fb_data *clr,
- fb_data *fillclr,
- bool clip)
+ fb_data *fillclr)
{
/* NOTE! clr and fillclr passed as pointers */
/* Rasterizing algorithm derivative of work by Alois Zingl */
-#if LCD_WIDTH > 1024 || LCD_HEIGHT > 1024
+#if (LCD_WIDTH > 1024 || LCD_HEIGHT > 1024) && defined(INT64_MAX)
/* Prevents overflow on large screens */
- double dx, dy, err, e2;
+ int64_t dx, dy, err, e1;
#else
- long dx, dy, err, e2;
+ int32_t dx, dy, err, e1;
#endif
+ /* if called with swapped points .. exchange them */
+ swap_int((x1 > x2), &x1, &x2);
+ swap_int((y1 > y2), &y1, &y2);
- int ret = 0;
-
- int a = ABS(x2 - x1); /* diameter */
- int b = ABS(y2 - y1); /* diameter */
+ int a = x2 - x1; /* diameter */
+ int b = y2 - y1; /* diameter */
- if(a == 0 || b == 0 || !clr)
- return ret; /* not an error but nothing to display */
+ if(a == 0 || b == 0)
+ return; /* not an error but nothing to display */
int b1 = (b & 1);
b = b - (1 - b1);
@@ -613,21 +631,16 @@ static int d_ellipse(struct rocklua_image *img,
err = dx + dy + b1 * a2; /* error of 1.step */
- /* if called with swapped points .. exchange them */
- swap_int((x1 > x2), &x1, &x2);
- swap_int((y1 > y2), &y1, &y2);
-
y1 += (b + 1) >> 1;
y2 = y1 - b1;
do
{
- ret = d_ellipse_elements(img, x1, y1, x2, y2, 0, 0, clr, fillclr, clip);
+ d_ellipse_elements(img, x1, y1, x2, y2, clr, fillclr);
- e2 = err;
+ e1 = err;
- /* using division because you can't use bit shift on doubles.. */
- if(e2 <= (dy / 2)) /* target midpoint - y step */
+ if(e1 <= (dy >> 1)) /* target midpoint - y step */
{
y1++;
y2--;
@@ -635,7 +648,7 @@ static int d_ellipse(struct rocklua_image *img,
err += dy;
}
- if(e2 >= (dx / 2) || err > (dy / 2)) /* target midpoint - x step */
+ if(e1 >= (dx >> 1) || err > (dy >> 1)) /* target midpoint - x step */
{
x1++;
x2--;
@@ -643,21 +656,23 @@ static int d_ellipse(struct rocklua_image *img,
err += dx;
}
- } while(ret == 0 && x1 <= x2);
+ } while(x1 <= x2);
+
+ if (fillclr && x1 - x2 <= 2)
+ fillclr = clr;
- while (ret == 0 && y1 - y2 < b) /* early stop of flat ellipse a=1 finish tip */
+ while (y1 - y2 < b) /* early stop of flat ellipse a=1 finish tip */
{
- ret = d_ellipse_elements(img, x1, y1, x2, y2, 1, 0, clr, fillclr, clip);
+ d_ellipse_elements(img, x1, y1, x2, y2, clr, fillclr);
y1++;
y2--;
}
- return ret;
} /* d_ellipse */
/* Lua to C Interface for line and ellipse */
-static int rli_line_ellipse(lua_State *L, bool is_ellipse)
+static int rli_line_ellipse(lua_State *L, bool is_ellipse, int narg_clip)
{
struct rocklua_image *a = rli_checktype(L, 1);
@@ -666,20 +681,12 @@ static int rli_line_ellipse(lua_State *L, bool is_ellipse)
int x2 = luaL_optint(L, 4, x1);
int y2 = luaL_optint(L, 5, y1);
- fb_data clr = FB_SCALARPACK((unsigned) luaL_checknumber(L, 6));
+ fb_data clr = FB_SCALARPACK((unsigned) lua_tointeger(L, 6));
fb_data fillclr; /* fill color is index 7 if is_ellipse */
fb_data *p_fillclr = NULL;
- bool clip;
- int clip_narg;
-
- if(is_ellipse)
- clip_narg = 8;
- else
- clip_narg = 7;
-
- clip = luaL_optboolean(L, clip_narg, false);
+ bool clip = lua_toboolean(L, narg_clip);
if(!clip)
{
@@ -689,31 +696,27 @@ static int rli_line_ellipse(lua_State *L, bool is_ellipse)
if(is_ellipse)
{
- if(!lua_isnoneornil(L, 7))
+ if(lua_type(L, 7) == LUA_TNUMBER)
{
- fillclr = FB_SCALARPACK((unsigned) luaL_checkint(L, 7));
+ fillclr = FB_SCALARPACK((unsigned) lua_tointeger(L, 7));
p_fillclr = &fillclr;
}
- luaL_argcheck(L, d_ellipse(a, x1, y1, x2, y2, &clr, p_fillclr, clip) == 0,
- 1, ERR_DATA_OVF);
+ d_ellipse(a, x1, y1, x2, y2, &clr, p_fillclr);
}
else
- luaL_argcheck(L, d_line(a, x1, y1, x2, y2, &clr, clip) == 0,
- 1, ERR_DATA_OVF);
+ d_line(a, x1, y1, x2, y2, &clr);
return 0;
} /* rli_line_ellipse */
-/* Pushes lua function from Stack at position narg to top of stack
- and puts a reference in the global registry for later use */
-static inline int register_luafunc(lua_State *L, int narg_funct)
+static inline int rli_pushpixel(lua_State *L, fb_data color, int x, int y)
{
- lua_pushvalue(L, narg_funct); /* lua function */
- int lf_ref = luaL_ref(L, LUA_REGISTRYINDEX);
- lua_settop(L, 0); /* clear C stack for use by lua function */
- return lf_ref;
-} /* register_luafunc */
+ lua_pushinteger(L, FB_UNPACK_SCALAR_LCD(color));
+ lua_pushinteger(L, x);
+ lua_pushinteger(L, y);
+ return 3;
+}
/* User defined pixel manipulations through rli_copy, rli_marshal */
static int custom_transform(lua_State *L,
@@ -723,42 +726,40 @@ static int custom_transform(lua_State *L,
fb_data *color)
{
(void) color;
-
+ (void) op;
fb_data dst;
fb_data src;
- unsigned int params = 3;
+ int params;
int ret = 0;
- lua_rawgeti(L, LUA_REGISTRYINDEX, op);
+ if (!lua_isfunction(L, -1))
+ return ret; /* error */
+
+ lua_pushvalue(L, -1); /* make a copy of the lua function */
dst = data_get(ds->elem, ds->x, ds->y);
- lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(dst));
- lua_pushnumber(L, ds->x);
- lua_pushnumber(L, ds->y);
+ params = rli_pushpixel(L, dst, ds->x, ds->y);
if(ss) /* Allows src to be omitted */
{
- params += 3;
src = data_get(ss->elem, ss->x, ss->y);
- lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(src));
- lua_pushnumber(L, ss->x);
- lua_pushnumber(L, ss->y);
+ params += rli_pushpixel(L, src, ss->x, ss->y);
}
lua_call(L, params, 2); /* call custom function w/ n-params & 2 ret */
- if(!lua_isnoneornil(L, -2))
+ if(lua_type(L, -2) == LUA_TNUMBER)
{
- ret = 1;
- dst = FB_SCALARPACK((unsigned) luaL_checknumber(L, -2));
+ ret |= 1;
+ dst = FB_SCALARPACK((unsigned) lua_tointeger(L, -2));
data_set(ds->elem, ds->x, ds->y, &dst);
}
- if(!lua_isnoneornil(L, -1) && ss)
+ if(ss && (lua_type(L, -1) == LUA_TNUMBER))
{
ret |= 2;
- src = FB_SCALARPACK((unsigned) luaL_checknumber(L, -1));
+ src = FB_SCALARPACK((unsigned) lua_tointeger(L, -1));
data_set(ss->elem, ss->x, ss->y, &src);
}
@@ -834,10 +835,21 @@ static int blit_transform(lua_State *L,
case 27: { if(dst == src) { dst = clr; } break; }
case 28: { if(dst > src) { dst = clr; } break; }
case 29: { if(dst < src) { dst = clr; } break; }
+#if 0
+ /* src unneeded */
+ case 30: { dst = clr; break; }/* copyC */
+ case 31: { dst = clr | dst; break; }/* DorC */
+ case 32: { dst = clr ^ dst; break; }/* DxorC */
+ case 33: { dst = ~(clr | dst); break; }/* nDorC */
+ case 34: { dst = (~clr) | dst; break; }/* DornC */
+ case 35: { dst = clr & dst; break; }/* DandC */
+ case 36: { dst = clr & (~dst); break; }/* nDandC */
+ case 37: { dst = ~clr; break; }/* notC */
+#endif
}/*switch op*/
- fb_data data = FB_SCALARPACK(dst);
- data_set(ds->elem, ds->x, ds->y, &data);
+ fb_data val = FB_SCALARPACK(dst);
+ data_set(ds->elem, ds->x, ds->y, &val);
return 1;
} /* blit_transform */
@@ -857,6 +869,22 @@ static int invert_transform(lua_State *L,
return 1;
} /* invert_transform */
+
+static int clear_transform(lua_State *L,
+ struct rli_iter_d *ds,
+ struct rli_iter_d *ss,
+ int op,
+ fb_data *color)
+{
+ (void) L;
+ (void) op;
+ (void) ss;
+
+ data_set(ds->elem, ds->x, ds->y, color);
+
+ return 1;
+} /* clear_transform */
+
#endif /* RLI_EXTENDED */
/* RLI to LUA Interface functions *********************************************/
@@ -865,8 +893,7 @@ RLI_LUA rli_new(lua_State *L)
int width = luaL_optint(L, 1, LCD_WIDTH);
int height = luaL_optint(L, 2, LCD_HEIGHT);
- luaL_argcheck(L, width > 0, 1, ERR_IDX_RANGE);
- luaL_argcheck(L, height > 0, 2, ERR_IDX_RANGE);
+ luaL_argcheck(L, width > 0 && height > 0, (width <= 0) ? 1 : 2, ERR_IDX_RANGE);
rli_alloc(L, width, height);
@@ -876,43 +903,41 @@ RLI_LUA rli_new(lua_State *L)
RLI_LUA rli_set(lua_State *L)
{
/*(set) (dst*, [x1, y1, clr, clip]) */
- /* get and set share the same underlying function */
- return rli_setget(L, false);
+ return rli_setget(L, false, 5);
}
RLI_LUA rli_get(lua_State *L)
{
/*(get) (dst*, [x1, y1, clip]) */
- /* get and set share the same underlying function */
- return rli_setget(L, true);
+ return rli_setget(L, true, 4);
}
-RLI_LUA rli_height(lua_State *L)
+RLI_LUA rli_equal(lua_State *L)
{
struct rocklua_image *a = rli_checktype(L, 1);
- lua_pushnumber(L, a->height);
+ struct rocklua_image *b = rli_checktype(L, 2);
+ lua_pushboolean(L, a->data == b->data);
return 1;
}
-RLI_LUA rli_width(lua_State *L)
+RLI_LUA rli_height(lua_State *L)
{
struct rocklua_image *a = rli_checktype(L, 1);
- lua_pushnumber(L, a->width);
+ lua_pushinteger(L, a->height);
return 1;
}
-RLI_LUA rli_equal(lua_State *L)
+RLI_LUA rli_width(lua_State *L)
{
struct rocklua_image *a = rli_checktype(L, 1);
- struct rocklua_image *b = rli_checktype(L, 2);
- lua_pushboolean(L, a->data == b->data);
+ lua_pushinteger(L, a->width);
return 1;
}
RLI_LUA rli_size(lua_State *L)
{
struct rocklua_image *a = rli_checktype(L, 1);
- lua_pushnumber(L, a->elems);
+ lua_pushinteger(L, a->elems);
return 1;
}
@@ -921,17 +946,17 @@ RLI_LUA rli_raw(lua_State *L)
/*val = (img*, index, [new_val]) */
struct rocklua_image *a = rli_checktype(L, 1);
- size_t i = (unsigned) luaL_checkint(L, 2);
+ size_t i = (unsigned) lua_tointeger(L, 2);
fb_data val;
luaL_argcheck(L, i > 0 && i <= (a->elems), 2, ERR_IDX_RANGE);
- lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(a->data[i-1]));
+ lua_pushinteger(L, FB_UNPACK_SCALAR_LCD(a->data[i-1]));
- if(!lua_isnoneornil(L, 3))
+ if(lua_type(L, 3) == LUA_TNUMBER)
{
- val = FB_SCALARPACK((unsigned) luaL_checknumber(L, 3));
+ val = FB_SCALARPACK((unsigned) lua_tointeger(L, 3));
a->data[i-1] = val;
}
@@ -943,7 +968,7 @@ RLI_LUA rli_tostring(lua_State *L)
/* (img, [infoitem]) */
struct rocklua_image *a = rli_checktype(L, 1);
- int item = (unsigned) luaL_optint(L, 2, 0);
+ int item = lua_tointeger(L, 2);
size_t bytes = a->elems * sizeof(fb_data);
switch(item)
@@ -956,16 +981,19 @@ RLI_LUA rli_tostring(lua_State *L)
a->width, a->height, a->elems, bytes, LCD_DEPTH, LCD_PIXELFORMAT);
break;
}
- case RLI_INFO_TYPE: { lua_pushfstring(L, ROCKLUA_IMAGE ); break; }
- case RLI_INFO_WIDTH: { lua_pushfstring(L, "%d", a->width ); break; }
- case RLI_INFO_HEIGHT: { lua_pushfstring(L, "%d", a->height ); break; }
- case RLI_INFO_ELEMS: { lua_pushfstring(L, "%d", a->elems ); break; }
- case RLI_INFO_BYTES: { lua_pushfstring(L, "%d", bytes ); break; }
- case RLI_INFO_DEPTH: { lua_pushfstring(L, "%d", LCD_DEPTH ); break; }
- case RLI_INFO_FORMAT: { lua_pushfstring(L, "%d", LCD_PIXELFORMAT); break; }
- case RLI_INFO_ADDRESS: { lua_pushfstring(L, "%p", a->data); break; }
+ case RLI_INFO_TYPE: { lua_pushfstring(L, ROCKLUA_IMAGE); break; }
+ case RLI_INFO_WIDTH: { lua_pushinteger(L, a->width); break; }
+ case RLI_INFO_HEIGHT: { lua_pushinteger(L, a->height); break; }
+ case RLI_INFO_ELEMS: { lua_pushinteger(L, a->elems); break; }
+ case RLI_INFO_BYTES: { lua_pushinteger(L, bytes); break; }
+ case RLI_INFO_DEPTH: { lua_pushinteger(L, LCD_DEPTH ); break; }
+ case RLI_INFO_FORMAT: { lua_pushinteger(L, LCD_PIXELFORMAT); break; }
+ case RLI_INFO_ADDRESS: { lua_pushfstring(L, "%p", a->data); break; }
}
+ /* lua_pushstring(L, lua_tostring(L, -1)); */
+ lua_tostring(L, -1); /* converts item at index to string */
+
return 1;
}
@@ -974,52 +1002,38 @@ RLI_LUA rli_ellipse(lua_State *L)
{
/* (dst*, x1, y1, x2, y2, [clr, fillclr, clip]) */
/* line and ellipse share the same init function */
- return rli_line_ellipse(L, true);
+ return rli_line_ellipse(L, true, 8);
}
RLI_LUA rli_line(lua_State *L)
{
/* (dst*, x1, y1, [x2, y2, clr, clip]) */
/* line and ellipse share the same init function */
- return rli_line_ellipse(L, false);
+ return rli_line_ellipse(L, false, 7);
}
RLI_LUA rli_iterator(lua_State *L) {
/* see rli_iterator_factory */
+ int params = 0;
struct rli_iter_d *ds;
ds = (struct rli_iter_d *) lua_touserdata(L, lua_upvalueindex(1));
if(ds->dx != 0 || ds->dy != 0)
{
- lua_pushnumber(L, FB_UNPACK_SCALAR_LCD(data_get(ds->elem, ds->x, ds->y)));
-
- lua_pushinteger(L, ds->x);
- lua_pushinteger(L, ds->y);
+ params = rli_pushpixel(L, data_get(ds->elem, ds->x, ds->y), ds->x, ds->y);
next_rli_iter(ds); /* load next element */
-
- return 3;
}
- return 0; /* nothing left to do */
+ return params; /* nothing left to do */
}
-RLI_LUA rli_iterator_factory(lua_State *L) {
- /* (src*, [x1, y1, x2, y2, dx, dy]) */
- struct rocklua_image *a = rli_checktype(L, 1); /*image we wish to iterate*/
-
- struct rli_iter_d *ds;
-
- int x1 = luaL_optint(L, 2, 1);
- int y1 = luaL_optint(L, 3, 1);
- int x2 = luaL_optint(L, 4, a->width - x1 + 1);
- int y2 = luaL_optint(L, 5, a->height - y1 + 1);
- int dx = luaL_optint(L, 6, 1);
- int dy = luaL_optint(L, 7, 1);
+RLI_LUA rli_iterator_factory(lua_State *L)
+{
+ /* (points) (img*, [x1, y1, x2, y2, dx, dy, clip]) */
+ /* (indices 1-8 are used by rli_iter_create) */
/* create new iter + pushed onto stack */
- ds = (struct rli_iter_d *) lua_newuserdata(L, sizeof(struct rli_iter_d));
-
- init_rli_iter(ds, a, x1, y1, x2, y2, dx, dy, false, false);
+ rli_iter_create(L);
/* returns the iter function with embedded iter data(up values) */
lua_pushcclosure(L, &rli_iterator, 1);
@@ -1027,66 +1041,54 @@ RLI_LUA rli_iterator_factory(lua_State *L) {
return 1;
}
-RLI_LUA rli_marshal(lua_State *L) /* also invert */
+RLI_LUA rli_marshal(lua_State *L) /* also invert, clear */
{
- /* (img*, [x1, y1, x2, y2, dx, dy, clip, function]) */
- struct rocklua_image *a = rli_checktype(L, 1);
-
- struct rli_iter_d ds;
-
- int x1 = luaL_optint(L, 2, 1);
- int y1 = luaL_optint(L, 3, 1);
- int x2 = luaL_optint(L, 4, a->width);
- int y2 = luaL_optint(L, 5, a->height);
- int dx = luaL_optint(L, 6, 1);
- int dy = luaL_optint(L, 7, 1);
- bool clip = luaL_optboolean(L, 8, false);
-
- int lf_ref = LUA_NOREF; /* de-ref'd without consequence */
+ /* (marshal/invert/clear) (img*, [x1, y1, x2, y2, dx, dy, clip, function]) */
+ /* (indices 1-8 are used by rli_iter_create) */
+ fb_data clr;
int (*rli_trans)(lua_State *, struct rli_iter_d *, struct rli_iter_d *, int, fb_data *);
- rli_trans = invert_transform; /* default transformation */
+ int ltype = lua_type (L, 9);
- if(!clip)
+ /* create new iter + pushed onto stack */
+ struct rli_iter_d *ds = rli_iter_create(L);
+
+ if (ltype == LUA_TNUMBER)
{
- bounds_check_xy(L, a, 2, x1, 3, y1);
- bounds_check_xy(L, a, 4, x2, 5, y2);
+ clr = FB_SCALARPACK((unsigned) lua_tointeger(L, 9));
+ rli_trans = clear_transform;
}
-
- init_rli_iter(&ds, a, x1, y1, x2, y2, dx, dy, false, false);
-
- if(lua_isfunction(L, 9)) /* custom function */
+ else if(ltype == LUA_TFUNCTION) /* custom function */
{
rli_trans = custom_transform;
- lf_ref = register_luafunc(L, 9);
+ lua_pushvalue(L, 9); /* ensure lua function on top of stack */
}
+ else
+ rli_trans = invert_transform; /* default transformation */
do
{
- luaL_argcheck(L, clip || (ds.elem != NULL), 1, ERR_DATA_OVF);
-
- if(!(*rli_trans)(L, &ds, NULL, lf_ref, NULL))
+ if(!(*rli_trans)(L, ds, NULL, 0, &clr))
break; /* Custom op can quit early */
- } while(next_rli_iter(&ds));
+ } while(next_rli_iter(ds));
- luaL_unref(L, LUA_REGISTRYINDEX, lf_ref); /* de-reference custom function */
return 0;
}
RLI_LUA rli_copy(lua_State *L)
{
/* (dst*, src*, [d_x, d_y, s_x, s_y, x_off, y_off, clip, [op, funct/clr]]) */
- struct rocklua_image *d = rli_checktype(L, 1); /*dst*/
- struct rocklua_image *s = rli_checktype(L, 2); /*src*/
+ struct rocklua_image *dst = rli_checktype(L, 1); /* dst */
+ struct rocklua_image *src = rli_checktype(L, 2); /* src */
- struct rli_iter_d ds; /*dst*/
- struct rli_iter_d ss; /*src*/
+ struct rli_iter_d ds; /* dst */
+ struct rli_iter_d ss; /* src */
/* copy whole image if possible */
- if(s->elems == d->elems && s->width == d->width && lua_gettop(L) < 3)
+ if(src->elems == dst->elems && src->width == dst->width && lua_gettop(L) < 3)
{
- rb->memcpy(d->data, s->data, d->elems * sizeof(fb_data));
+ rb->memcpy(dst->data, src->data, dst->elems * sizeof(fb_data));
return 0;
}
@@ -1095,38 +1097,41 @@ RLI_LUA rli_copy(lua_State *L)
int s_x = luaL_optint(L, 5, 1);
int s_y = luaL_optint(L, 6, 1);
- int w = MIN(d->width - d_x, s->width - s_x);
- int h = MIN(d->height - d_y, s->height - s_y);
+ int w = MIN(dst->width - d_x, src->width - s_x);
+ int h = MIN(dst->height - d_y, src->height - s_y);
int x_off = luaL_optint(L, 7, w);
int y_off = luaL_optint(L, 8, h);
- bool clip = luaL_optboolean(L, 9, false);
- int op = luaL_optint(L, 10, 0);
- fb_data clr = FB_SCALARPACK(0); /* 11 is custom function | color */
+ bool clip = lua_toboolean(L, 9);
+ int op; /* 10 is operation for blit */
+ fb_data clr; /* 11 is custom function | color */
bool d_swx = (x_off < 0); /* dest swap */
bool d_swy = (y_off < 0);
- bool s_swx = false; /* src swap */
+ bool s_swx = false; /* src swap */
bool s_swy = false;
int (*rli_trans)(lua_State *, struct rli_iter_d *, struct rli_iter_d *, int, fb_data *);
- rli_trans = blit_transform; /* default transformation */
-
- int lf_ref = LUA_NOREF; /* de-ref'd without consequence */
if(!clip) /* Out of bounds is not allowed */
{
- bounds_check_xy(L, d, 3, d_x, 4, d_y);
- bounds_check_xy(L, s, 5, s_x, 6, s_y);
+ bounds_check_xy(L, dst, 3, d_x, 4, d_y);
+ bounds_check_xy(L, src, 5, s_x, 6, s_y);
+ w = MIN(w, ABS(x_off));
+ h = MIN(h, ABS(y_off));
+ bounds_check_xy(L, dst, 7, d_x + w, 8, d_y + h);
+ bounds_check_xy(L, src, 7, s_x + w, 8, s_y + h);
+ }
+ else
+ {
+ if (w < 0 || h < 0) /* not an error but nothing to display */
+ return 0;
+ w = MIN(w, ABS(x_off));
+ h = MIN(h, ABS(y_off));
}
- else if (w < 0 || h < 0) /* not an error but nothing to display */
- return 0;
-
- w = MIN(w, ABS(x_off));
- h = MIN(h, ABS(y_off));
- /* if(s->data == d->data) need to care about fill direction */
+ /* if src->data == dst->data need to care about fill direction */
if(d_x > s_x)
{
d_swx = !d_swx;
@@ -1139,67 +1144,46 @@ RLI_LUA rli_copy(lua_State *L)
s_swy = !s_swy;
}
- init_rli_iter(&ds, d, d_x, d_y, d_x + w, d_y + h, 1, 1, d_swx, d_swy);
+ rli_iter_init(&ds, dst, d_x, d_y, d_x + w, d_y + h, 1, 1, d_swx, d_swy);
- init_rli_iter(&ss, s, s_x, s_y, s_x + w, s_y + h, 1, 1, s_swx, s_swy);
+ rli_iter_init(&ss, src, s_x, s_y, s_x + w, s_y + h, 1, 1, s_swx, s_swy);
- if (op == 0xFF && lua_isfunction(L, 11)) /* custom function specified.. */
+ if (lua_type(L, 11) == LUA_TFUNCTION) /* custom function supplied.. */
{
rli_trans = custom_transform;
- lf_ref = register_luafunc(L, 11);
- op = lf_ref;
+ lua_settop(L, 11); /* ensure lua function on top of stack */
+ clr = 0;
+ op = 0;
}
else
- clr = FB_SCALARPACK((unsigned) luaL_optnumber(L, 11, LCD_BLACK));
+ {
+ rli_trans = blit_transform; /* default transformation */
+ clr = FB_SCALARPACK((unsigned) lua_tointeger(L, 11));
+ op = lua_tointeger(L, 10);
+ }
do
{
- if(!clip)
- {
- luaL_argcheck(L, ss.elem != NULL, 2, ERR_DATA_OVF);
- luaL_argcheck(L, ds.elem != NULL, 1, ERR_DATA_OVF);
- }
-
if(!(*rli_trans)(L, &ds, &ss, op, &clr))
break; /* Custom op can quit early */
} while(next_rli_iter(&ds) && next_rli_iter(&ss));
- luaL_unref(L, LUA_REGISTRYINDEX, lf_ref); /* de-reference custom function */
return 0;
}
RLI_LUA rli_clear(lua_State *L)
{
- /* (dst*, [color, x1, y1, x2, y2, clip]) */
- struct rocklua_image *a = rli_checktype(L, 1);
-
- struct rli_iter_d ds;
+ /* (clear) (dst*, [color, x1, y1, x2, y2, clip, dx, dy]) */
+ lua_settop(L, 9);
- fb_data clr = FB_SCALARPACK((unsigned) luaL_optnumber(L, 2, LCD_BLACK));
- int x1 = luaL_optint(L, 3, 1);
- int y1 = luaL_optint(L, 4, 1);
- int x2 = luaL_optint(L, 5, a->width);
- int y2 = luaL_optint(L, 6, a->height);
- bool clip = luaL_optboolean(L, 7, false);
+ lua_pushvalue(L, 7); /* clip -- index 8 */
+ lua_remove(L, 7);
- if(!clip)
- {
- bounds_check_xy(L, a, 3, x1, 4, y1);
- bounds_check_xy(L, a, 5, x2, 6, y2);
- }
+ lua_pushinteger(L, lua_tointeger(L, 2)); /*color -- index 9*/
+ lua_remove(L, 2);
- init_rli_iter(&ds, a, x1, y1, x2, y2, 1, 1, false, false);
-
- do
- {
- luaL_argcheck(L, clip || (ds.elem != NULL), 1, ERR_DATA_OVF);
-
- data_set(ds.elem, ds.x, ds.y, &clr);
-
- } while(next_rli_iter(&ds));
-
- return 0;
+ return rli_marshal(L); /* (img*, [x1, y1, x2, y2, dx, dy, clip, function]) */
}
#endif /* RLI_EXTENDED */
@@ -1236,15 +1220,11 @@ LUALIB_API int rli_init(lua_State *L)
luaL_newmetatable(L, ROCKLUA_IMAGE);
- lua_pushstring(L, "__index");
- lua_pushvalue(L, -2); /* pushes the metatable */
- lua_settable(L, -3); /* metatable.__index = metatable */
+ lua_pushvalue(L, -1); /* pushes the metatable */
+ lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
luaL_register(L, NULL, rli_lib);
-#ifdef RLI_EXTENDED
- luaL_register(L, ROCKLUA_IMAGE, rli_lib);
-#endif
return 1;
}
@@ -1256,7 +1236,7 @@ LUALIB_API int rli_init(lua_State *L)
* -----------------------------
*/
-#define RB_WRAP(M) static int rock_##M(lua_State UNUSED_ATTR *L)
+#define RB_WRAP(func) static int rock_##func(lua_State UNUSED_ATTR *L)
#ifdef HAVE_LCD_BITMAP
RB_WRAP(lcd_framebuffer)
{