-
Notifications
You must be signed in to change notification settings - Fork 22
/
bmp_demo.lua
99 lines (89 loc) · 2.01 KB
/
bmp_demo.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
local bmp_format = require'bmp'
local ffi = require'ffi'
local fs = require'fs'
local nw = require'nw'
local bitmap = require'bitmap'
local app = nw:app()
local win = app:window{w = 1150, h = 1000, visible = false}
local function perr(fname, err)
print(string.format('%-46s %s', fname, err))
end
local function reader(f)
return function(buf, sz)
assert(sz > 0)
if buf then
return assert(f:read(buf, sz))
else
local pos0 = assert(f:seek())
local pos1 = assert(f:seek(sz))
return pos1 - pos0
end
end
end
function win:repaint()
local wbmp = win:bitmap()
local x, y = 10, 10
local maxh = 0
local function show(fname)
local f = fs.open(fname)
local bmp, err = bmp_format.open(reader(f))
if not bmp then
perr(fname, err)
else
if x + bmp.w + 10 > wbmp.w then
x = 10
y = y + maxh + 10
end
local ok, err = bmp:load(wbmp, x, y)
if not ok then
perr(fname, err)
else
--save a copy of the bitmap so we test the saving API too
local f1 = fs.open(fname:gsub('%.bmp', '-saved.bmp'), 'w')
local bmp_cut = bitmap.sub(wbmp, x, y, bmp.w, bmp.h)
bmp_format.save(bmp_cut, function(buf, sz)
assert(f1:write(buf, sz))
end)
f1:close()
x = x + bmp.w + 10
maxh = math.max(maxh, bmp.h)
end
end
f:close()
end
local function show_iter(fname)
local f = fs.open(fname)
local bmp, err = bmp_format.open(reader(f))
if bmp then
if x + bmp.w + 10 > wbmp.w then
x = 10
y = y + maxh + 10
end
local ok, err = pcall(function()
for j, row_bmp in bmp:rows('bgr8', nil, true) do
bitmap.paint(wbmp, row_bmp, x, y + j)
end
end)
if not ok then
perr(fname, err)
else
x = x + bmp.w + 10
maxh = math.max(maxh, bmp.h)
end
end
f:close()
end
for i,d in ipairs{'good', 'bad', 'questionable'} do
for f in fs.dir('media/bmp/'..d) do
if f:find'%.bmp$' and not f:find'%-saved' then
local f = 'media/bmp/'..d..'/'..f
show(f)
show_iter(f)
end
end
y = y + maxh + 40
x = 10
end
end
win:show()
app:run()