summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/include_lua/draw_floodfill.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/include_lua/draw_floodfill.lua')
-rw-r--r--apps/plugins/lua/include_lua/draw_floodfill.lua95
1 files changed, 95 insertions, 0 deletions
diff --git a/apps/plugins/lua/include_lua/draw_floodfill.lua b/apps/plugins/lua/include_lua/draw_floodfill.lua
new file mode 100644
index 0000000000..0303e711f2
--- /dev/null
+++ b/apps/plugins/lua/include_lua/draw_floodfill.lua
@@ -0,0 +1,95 @@
1--[[ Lua Floodfill function
2/***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
10 *
11 * Copyright (C) 2017 William Wilgus
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22]]
23-- floods an area of targetclr with fillclr x, y specifies the start seed
24-- flood_fill(img, x, y, targetclr, fillclr)
25if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
26do
27
28 local rocklib_image = getmetatable(rb.lcd_framebuffer())
29 local _NIL = nil -- nil placeholder
30 local _get = rocklib_image.get
31 local _line = rocklib_image.line
32 local _marshal = rocklib_image.marshal
33
34 return function(img, x, y, targetclr, fillclr)
35 -- scanline 4-way flood algorithm
36 -- ^
37 -- <--------x--->
38 -- v
39 -- check that target color doesn't = fill and the first point is target color
40 if targetclr == fillclr or targetclr ~= _get(img, x, y, true) then return end
41 local max_w = img:width()
42 local max_h = img:height()
43
44 local qpt = {} -- FIFO queue
45 -- rather than moving elements around in our FIFO queue
46 -- for each read; increment 'qhead' by 2
47 -- set both elements to nil and let the
48 -- garbage collector worry about it
49 -- for each write; increment 'qtail' by 2
50 -- x coordinates are in odd indices while
51 -- y coordinates are in even indices
52
53 local qtail = 0
54
55 local function check_ns(val, x, y)
56 if targetclr == val then
57 y = y - 1
58 if targetclr == _get(img, x, y, true) then -- north
59 qtail = qtail + 2
60 qpt[qtail - 1] = x
61 qpt[qtail] = y
62 end
63 y = y + 2
64 if targetclr == _get(img, x, y, true) then -- south
65 qtail = qtail + 2
66 qpt[qtail - 1] = x
67 qpt[qtail] = y
68 end
69 return fillclr
70 end
71 return _NIL -- signal marshal to stop
72 end
73
74 local function seed_pt(x, y)
75 -- should never hit max but make sure not to end early
76 for qhead = 2, 0x40000000, 2 do
77
78 if targetclr == _get(img, x, y, true) then
79 _marshal(img, x, y, 1, y, _NIL, _NIL, true, check_ns) -- west
80 _marshal(img, x + 1, y, max_w, y, _NIL, _NIL, true, check_ns) -- east
81 end
82
83 x = qpt[qhead - 1]
84 qpt[qhead - 1] = _NIL
85
86 if not x then break end
87
88 y = qpt[qhead]
89 qpt[qhead] = _NIL
90 end
91 end
92
93 seed_pt(x, y) -- Begin
94 end -- flood_fill
95end