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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
load "iupe-dbuffer.lua"
dalosp.framebuffer = {
get_settings = function (self)
local extra = self.extra
return {
width = extra.width,
height = extra.height,
bpp = extra.bpp,
zoom = extra.zoom,
ppdir = extra.ppdir,
flipx = extra.flipx,
flipy = extra.flipy,
}
end,
input_change = function (self, ind)
local h = self:get_linked_input(ind)
if ind == 2 then
if h then
local cfg, s = h:readfile()
s, cfg = pcall(loadstring, "local cfg = " .. cfg .. "\nreturn cfg\n")
if s then
self:apply_config(cfg)
self.extra.fb:prepare()
self.extra.fb:draw()
end
end
return
end
if h then h.getnextbyte = dalosp.framebuffer.getnextbyte end
self:apply_config{}
self.extra.fb:prepare()
self.extra.fb:draw()
end,
activate = function (self)
self.extra.dlg:show()
end,
configure = function (self)
local extra = self.extra
invert_lookup = {
[1] = 0,
[2] = 1,
[4] = 2,
[8] = 3,
[24] = 4,
[32] = 5,
}
lookup = {
[0] = 1,
[1] = 2,
[2] = 4,
[3] = 8,
[4] = 24,
[5] = 32,
}
local owidth, oheight, obpp, oppdir, oflipx, oflipy = extra.width, extra.height, extra.bpp, extra.ppdir, not not extra.flipx, not not extra.flipy
local cb = function (dlg, pi)
if pi < 0 then return end
local width, height, bpp, dir
width = iup.GetParamParam(dlg, 1).value + 0
height = iup.GetParamParam(dlg, 2).value + 0
bpp = iup.GetParamParam(dlg, 3).value + 0
dir = iup.GetParamParam(dlg, 4).value + 0
flipx = (iup.GetParamParam(dlg, 5).value + 0) ~= 0
flipy = (iup.GetParamParam(dlg, 6).value + 0) ~= 0
self:apply_config{ width = width, height = height, bpp = lookup[bpp], dir = dir, flipx = flipx, flipy = flipy }
self.extra.fb:prepare()
self.extra.fb:draw()
end
local s, nname = iup.GetParam(self.name .. " configuration", cb, [[
New name: %s
Width: %i
Height: %i
Bpp: %l|1 bpp|2 bpp|4 bpp|8 bpp|24 bpp|32 bpp|
Packed Pixels: %l|Little Endian|Big Endian|
Flip X axis: %b
Flip Y axis: %b
]], self.name, owidth, oheight, invert_lookup[obpp], oppdir, oflipx and 1 or 0, oflipy and 1 or 0)
if not s then
self:apply_config{ width = owidth, height = oheight, bpp = obpp, ppdir = oppdir, flipx = oflipx, flipy = oflipy }
self.extra.fb:prepare()
self.extra.fb:draw()
else
self.name = nname
end
end,
apply_config = function (self, cfg)
local extra = self.extra
local fb = extra.fb
local h = self:get_linked_input(1)
if cfg.width then extra.width = cfg.width end
if cfg.height then extra.height = cfg.height end
if cfg.bpp then extra.bpp = cfg.bpp end
if cfg.ppdir then extra.ppdir = cfg.ppdir end
if cfg.flipx then extra.flipx = cfg.flipx end
if cfg.flipy then extra.flipy = cfg.flipy end
if cfg.zoom then extra.zoom = cfg.zoom end
local zoom = 2 ^ extra.zoom
local posx = fb.posx
local posy = fb.posy
local dx = fb.dx + 0
local dy = fb.dy + 0
local fw = math.floor(extra.width * zoom)
local fh = math.floor(extra.height * zoom)
if dx >= (fb.xmax - fb.xmin) then posx = math.floor((fw - dx) / 2) end
if dy >= (fb.ymax - fb.ymin) then posy = math.floor((fh - dy) / 2) end
fb.xmax = fw
fb.ymax = fh
cfg = {
h = h,
width = extra.width,
height = extra.height,
bpp = extra.bpp,
ppdir = extra.ppdir,
flipx = extra.flipx,
flipy = extra.flipy,
zoom = zoom,
x = -posx,
y = -posy,
fw = fw,
fh = fh,
}
fb.cfg = cfg
local cvwidth, cvheight = fb.width or 0, fb.height or 0
cvwidth, cvheight = cvwidth + 0, cvheight + 0
end,
getnextbyte = function (h)
return h:tell() ~= h:getsize() and h:readU8() or 0
end,
getbit = function (cfg)
local rc
local h = cfg.h
local stateful = cfg.stateful
if cfg.bpp >= 8 then return end
stateful = cfg.stateful
if not stateful or bit.band(stateful, 0x7f) == 0 then
stateful = h:getnextbyte() * 2 + 1
else
stateful = stateful * 2
end
cfg.stateful = stateful
return bit.band(bit.rshift(stateful, 8), 1)
end,
getnextpixel = function (cfg)
local rc
local r, g, b
local h = cfg.h
local getbit = function() return dalosp.framebuffer.getbit(cfg) end
if cfg.bpp == 1 then
rc = getbit() * 255
r, g, b = rc, rc, rc
elseif cfg.bpp == 2 then
rc = getbit() * 2
rc = (rc + getbit()) * 255 / 3
r, g, b = rc, rc, rc
elseif cfg.bpp == 4 then
rc = getbit() * 8
rc = rc + getbit() * 4
rc = rc + getbit() * 2
rc = (rc + getbit()) * 255 / 15
r, g, b = rc, rc, rc
elseif cfg.bpp == 8 then
rc = h:getnextbyte()
r, g, b = rc, rc, rc
elseif cfg.bpp == 24 then
r = h:getnextbyte()
g = h:getnextbyte()
b = h:getnextbyte()
elseif cfg.bpp == 32 then
r = h:getnextbyte()
g = h:getnextbyte()
b = h:getnextbyte()
h:getnextbyte()
end
return r, g, b
end,
prepare = function (self)
local cfg = self.cfg
local h = cfg.h
local width = cfg.width
local height = cfg.height
local bpp = cfg.bpp
local ppdir = cfg.ppdir
local flipx = cfg.flipx
local flipy = not cfg.flipy
local cim = im.ImageCreate(width, height, im.RGB, im.BYTE)
self.cim = cim
cim:Clear()
if not h then return iup.DEFAULT end
local getnextpixel = self.getnextpixel
h:seek(0)
cfg.stateful = 0
local r, g, b = cim[0], cim[1], cim[2]
for y = 0, height - 1 do
for x = 0, width - 1 do
local px, py = flipx and width - x - 1 or x, flipy and height - y - 1 or y
r[py][px], g[py][px], b[py][px] = getnextpixel(cfg)
end
end
return iup.DEFAULT
end,
draw = function (self)
local cvdb = self.cvdb
if not cvdb then return iup.DEFAULT end
cvdb:Clear()
local cim = self.cim
local cfg = self.cfg
cim:cdCanvasPutImageRect(cvdb, cfg.x, cvdb:InvertYAxis(cfg.y + cfg.fh), cfg.fw, cfg.fh, 0, 0, 0, 0)
cvdb:Flush()
end,
resize_cb = function (self, width, height)
self.dx = width
self.dy = height
self.posx = self.posx
self.posy = self.posy
iupep.dbuffer.resize_cb(self, width, height)
self.obj:apply_config{}
self:draw()
end,
fimg2canvas = function (self, ix, iy)
local cfg = self.cfg
local cx, cy
return ix + cfg.x, iy + cfg.y
end,
canvas2fimg = function (self, cx, cy)
local cfg = self.cfg
return cx - cfg.x, cy - cfg.y
end,
img2canvas = function (self, ix, iy)
local cfg = self.cfg
return math.floor(ix * cfg.zoom + cfg.x), math.floor(iy * cfg.zoom + cfg.y)
end,
canvas2img = function (self, cx, cy)
local cfg = self.cfg
return math.floor((cx - cfg.x) / cfg.zoom), math.floor((cy - cfg.y) / cfg.zoom)
end,
scroll_cb = function (self, op, posx, posy)
self:resize_cb(self.width, self.height)
end,
wheel_cb = function (self, delta, x, y, status)
local ox, oy = self:canvas2img(x, y)
local opx, opy = self.cfg.x, self.cfg.y
self.obj:apply_config { zoom = self.obj.extra.zoom + (delta / 10) }
local nx, ny = self:img2canvas(ox, oy)
local dx, dy = ox - nx, oy - ny
self.posx, self.posy = -opx - dx, -opy - dy
self:resize_cb(self.width, self.height)
end,
motion_cb = function (self, x, y)
local ox, oy = self:canvas2img(x, y)
local nx, ny = self:img2canvas(ox, oy)
end,
create = function (d, tab, settings)
tab.ninputs = 2
tab.noutputs = 0
tab.otype = dalos.objtype.LUA_VIEWER
tab.activate = dalosp.framebuffer.activate
tab.configure = dalosp.framebuffer.configure
tab.input_change = dalosp.framebuffer.input_change
tab.default_name = "Framebuffer"
tab.ntype = "Framebuffer"
tab.get_settings = dalosp.framebuffer.get_settings
local extra = {
width = settings.width or 256,
height = settings.height or 256,
bpp = settings.bpp or 32,
ppdir = settings.dir or 0,
flipx = settings.flipx,
flipy = settings.flipy,
zoom = settings.zoom or 0,
}
local obj = dalos.object(d, tab, extra)
obj.apply_config = dalosp.framebuffer.apply_config
local fb = iup.canvas {
expand = "Yes",
font = "Courier, 8",
action = iupep.dbuffer.action,
map_cb = iupep.dbuffer.map_cb,
unmap_cb = iupep.dbuffer.unmap_cb,
resize_cb = dalosp.framebuffer.resize_cb,
motion_cb = dalosp.framebuffer.motion_cb,
draw = dalosp.framebuffer.draw,
scroll_cb = dalosp.framebuffer.scroll_cb,
wheel_cb = dalosp.framebuffer.wheel_cb,
prepare = dalosp.framebuffer.prepare,
img2canvas = dalosp.framebuffer.img2canvas,
canvas2img = dalosp.framebuffer.canvas2img,
fimg2canvas = dalosp.framebuffer.img2canvas,
canvas2fimg = dalosp.framebuffer.canvas2img,
getnextpixel = dalosp.framebuffer.getnextpixel,
rastersize = extra.width .. "x" .. extra.height,
scrollbar = "Yes",
dx = 1,
dy = 1,
linex = 1,
liney = 1,
xmax = 0,
ymax = 0,
xautohide = "No",
yautohide = "No",
obj = obj,
}
local dlg = iup.dialog {
iup.vbox {
fb,
iup.hbox {
iup.fill{},
iup.button {
action = function () fb.posx, fb.posy = 0, 0 obj:apply_config { zoom = -4 } fb:resize_cb(fb.width, fb.height) end,
title = "1:16",
},
iup.fill{},
iup.button {
action = function () fb.posx, fb.posy = 0, 0 obj:apply_config { zoom = -3 } fb:resize_cb(fb.width, fb.height) end,
title = "1:8",
},
iup.fill{},
iup.button {
action = function () fb.posx, fb.posy = 0, 0 obj:apply_config { zoom = -2 } fb:resize_cb(fb.width, fb.height) end,
title = "1:4",
},
iup.fill{},
iup.button {
action = function () fb.posx, fb.posy = 0, 0 obj:apply_config { zoom = -1 } fb:resize_cb(fb.width, fb.height) end,
title = "1:2",
},
iup.fill{},
iup.button {
action = function () fb.posx, fb.posy = 0, 0 obj:apply_config { zoom = 0 } fb:resize_cb(fb.width, fb.height) end,
title = "1:1",
},
iup.fill{},
iup.button {
action = function () fb.posx, fb.posy = 0, 0 obj:apply_config { zoom = 1 } fb:resize_cb(fb.width, fb.height) end,
title = "2:1",
},
iup.fill{},
iup.button {
action = function () fb.posx, fb.posy = 0, 0 obj:apply_config { zoom = 2 } fb:resize_cb(fb.width, fb.height) end,
title = "4:1",
},
iup.fill{},
iup.button {
action = function () fb.posx, fb.posy = 0, 0 obj:apply_config { zoom = 3 } fb:resize_cb(fb.width, fb.height) end,
title = "8:1",
},
iup.fill{},
iup.button {
action = function () fb.posx, fb.posy = 0, 0 obj:apply_config { zoom = 4 } fb:resize_cb(fb.width, fb.height) end,
title = "16:1",
},
iup.fill{},
},
},
size = "320x200",
title = obj.name,
shrink = "Yes",
}
extra.dlg = dlg
extra.fb = fb
obj:apply_config{}
fb:prepare()
fb:draw()
return obj
end,
}
dalos.framebuffer = dalosp.framebuffer.create
dalos:register_obj("Framebuffer", dalos.framebuffer, "Basic Viewers")
|