1
0
Fork 0

Added parsing function into CCPI

This commit is contained in:
Casey 2024-09-13 01:11:34 +03:00
parent 52f2a55cc1
commit 2fbb51cefe
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 16 additions and 8 deletions

View File

@ -64,10 +64,7 @@ decoders[1] = function(image, fp)
return true
end
local function load(path)
local fp, err = io.open(path, "rb")
if not fp then return nil, err end
local function parse(fp)
local res
local image = { w = 0, h = 0, scale = 1.0, palette = {}, lines = {} }
@ -86,11 +83,19 @@ local function load(path)
return nil, "Invalid header: expected CCPI got " .. magic
end
fp:close()
if not res then return false, err end
return image
end
local function load(path)
local fp, err = io.open(path, "rb")
if not fp then return nil, err end
local img
img, err = parse(fp)
fp:close()
return img, err
end
local function draw(img, ox, oy, monitor)
-- todo: add expect()
local t = monitor or term.current()
@ -101,7 +106,7 @@ local function draw(img, ox, oy, monitor)
return nil, "setPaletteColor is not supported on this term"
end
if not t.setTextScale then
if not t.setTextScale and img.scale != 1 then
return nil, "setTextScale is not supported on this term"
end
@ -109,7 +114,9 @@ local function draw(img, ox, oy, monitor)
t.setPaletteColor(bit.blshift(1, i - 1), img.palette[i])
end
t.setTextScale(img.scale)
if img.scale != 1 then
t.setTextScale(img.scale)
end
for y = 1, img.h do
t.setCursorPos(ox, oy + y - 1)
@ -119,5 +126,6 @@ end
return {
load = load,
draw = draw
draw = draw,
parse = parse
}