From 38cce4226afae0d142bd1ccf1db174a056dfa571 Mon Sep 17 00:00:00 2001 From: hkc Date: Fri, 13 Sep 2024 04:31:19 +0300 Subject: [PATCH] ccpi.lua now can accept handles --- cc-pic.py | 2 +- ccpi.lua | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) mode change 100644 => 100755 cc-pic.py diff --git a/cc-pic.py b/cc-pic.py old mode 100644 new mode 100755 index ced7495..3b7f840 --- a/cc-pic.py +++ b/cc-pic.py @@ -78,7 +78,7 @@ class Converter: if isinstance(palette, list): img_pal = Image.new("P", (1, 1)) img_pal.putpalette(palette) - self._img = image.quantize(16, palette=img_pal) + self._img = image.quantize(len(palette) // 3, palette=img_pal) else: self._img = image.convert("P", palette=palette, colors=16) diff --git a/ccpi.lua b/ccpi.lua index 4f22c6d..849182c 100644 --- a/ccpi.lua +++ b/ccpi.lua @@ -3,9 +3,9 @@ local decoders = {} local function read_palette_full(palette, fp) for i = 1, 16 do - palette[i] = bit.blshift(string.byte(fp:read(1)), 16) - palette[i] = bit.bor(palette[i], bit.blshift(string.byte(fp:read(1)), 8)) - palette[i] = bit.bor(palette[i], string.byte(fp:read(1))) + palette[i] = bit.blshift(string.byte(fp.read(1)), 16) + palette[i] = bit.bor(palette[i], bit.blshift(string.byte(fp.read(1)), 8)) + palette[i] = bit.bor(palette[i], string.byte(fp.read(1))) end end @@ -13,7 +13,7 @@ local function read_pixeldata_v0(image, fp) for y = 1, image.h do local line = { s = "", bg = "", fg = "" } for x = 1, image.w do - local data = fp:read(2) + local data = fp.read(2) if data == nil or #data == 0 then return nil, string.format("Failed to read character at x=%d y=%d", x, y) end @@ -38,7 +38,7 @@ local function read_varint(fp) local offset = 0 repeat if offset >= 5 then return nil, "varint too long" end - current = string.byte(fp:read(1)) + current = string.byte(fp.read(1)) value = bit.bor(value, bit.blshift(bit.band(current, 0x7f), offset * 7)) offset = offset + 1 until bit.band(current, 0x80) == 0 @@ -46,8 +46,8 @@ local function read_varint(fp) end decoders[0] = function(image, fp) - image.w, image.h = string.byte(fp:read(1)), string.byte(fp:read(1)) - image.scale = 0.5 + string.byte(fp:read(1)) * 5 / 255 + image.w, image.h = string.byte(fp.read(1)), string.byte(fp.read(1)) + image.scale = 0.5 + string.byte(fp.read(1)) * 5 / 255 read_palette_full(image.palette, fp) local success, err = read_pixeldata_v0(image, fp) if not success then return false, err end @@ -68,7 +68,7 @@ local function parse(fp) local res local image = { w = 0, h = 0, scale = 1.0, palette = {}, lines = {} } - local magic = fp:read(4) + local magic = fp.read(4) if magic == "CCPI" then res, err = decoders[0](image, fp) elseif magic:sub(1, 3) == "CPI" then @@ -89,7 +89,7 @@ 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) + img, err = parse(fp._handle) fp:close() return img, err end