From 2fbb51cefe6a42fca3d305844dd342fbc7c700ca Mon Sep 17 00:00:00 2001 From: hkc Date: Fri, 13 Sep 2024 01:11:34 +0300 Subject: [PATCH] Added parsing function into CCPI --- ccpi.lua | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/ccpi.lua b/ccpi.lua index f82c820..0159850 100644 --- a/ccpi.lua +++ b/ccpi.lua @@ -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 }