-
Notifications
You must be signed in to change notification settings - Fork 1
/
libspng_demo.lua
87 lines (74 loc) · 2.09 KB
/
libspng_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
local spng = require'libspng'
local testui = require'testui'
local glue = require'glue'
local fs = require'fs'
local ffi = require'ffi'
require'unit' --dir
local filesets = {
good_files = dir'media/png/good/*.png',
bad_files = dir'media/png/bad/*.png',
}
local files = 'good_files'
local bottom_up = false
local stride_aligned = false
local max_cut_size = 1024 * 6
local cut_size = max_cut_size
local pixel_formats = {bgra8 = true}
local gamma = false
function testui:repaint()
self:checkerboard()
self:pushgroup'right'
local _
_,files = self:choose('files', {'good_files', 'bad_files'}, files)
_,bottom_up = self:button('bottom_up', bottom_up)
_,stride_aligned = self:button('stride_aligned', stride_aligned)
_,cut_size = self:slide('cut_size', cut_size, 0, max_cut_size, 1)
_,pixel_formats = self:choose('format', {
'rgb8', 'rgba8', 'bgra8', 'rgba16', 'g8', 'ga8', 'ga16',
}, pixel_formats)
_,gamma = self:button('gamma', gamma)
self:nextgroup()
for i,path in ipairs(filesets[files]) do
local f = assert(fs.open(path))
local bufread = f:buffered_read()
local left = cut_size
local function read(buf, sz)
if left == 0 then return 0 end
local readsz, err = bufread(buf, math.min(left, sz))
if not readsz then return nil, err end
left = left - readsz
return readsz
end
local img, bmp, err
img, err = spng.open{read = read}
if img then
bmp, err = img:load{
accept = glue.update({
bottom_up = bottom_up,
stride_aligned = stride_aligned,
}, pixel_formats),
gamma = gamma,
}
end
if bmp and self.x + bmp.w >= self.window:client_size() then --wrap
self:nextgroup()
end
if bmp then
self:pushgroup'down'
self:label(img.format)
self:image(bmp)
self:label(bmp.format)
self:popgroup()
self:rect(0, bmp.h * 2.5)
else
--self:rect(cx, cy, w, h, 'error_bg')
--self:textbox(cx, cy, w, h,
-- string.format('%s', err:match('^(.-)\n'):match(': ([^:]-)$')),
-- 14, 'normal_fg', 'center', 'center')
end
if img then img:free() end
end
end
testui:init()
testui:continuous_repaint(1/0)
testui:run()