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