forked from hkc/cc-stuff
1
0
Fork 0

ccpi.lua now can accept handles

This commit is contained in:
Casey 2024-09-13 04:31:19 +03:00
parent a3b079e638
commit 38cce4226a
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
2 changed files with 10 additions and 10 deletions

2
cc-pic.py Normal file → Executable file
View File

@ -78,7 +78,7 @@ class Converter:
if isinstance(palette, list): if isinstance(palette, list):
img_pal = Image.new("P", (1, 1)) img_pal = Image.new("P", (1, 1))
img_pal.putpalette(palette) img_pal.putpalette(palette)
self._img = image.quantize(16, palette=img_pal) self._img = image.quantize(len(palette) // 3, palette=img_pal)
else: else:
self._img = image.convert("P", palette=palette, colors=16) self._img = image.convert("P", palette=palette, colors=16)

View File

@ -3,9 +3,9 @@ local decoders = {}
local function read_palette_full(palette, fp) local function read_palette_full(palette, fp)
for i = 1, 16 do for i = 1, 16 do
palette[i] = bit.blshift(string.byte(fp:read(1)), 16) 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], bit.blshift(string.byte(fp.read(1)), 8))
palette[i] = bit.bor(palette[i], string.byte(fp:read(1))) palette[i] = bit.bor(palette[i], string.byte(fp.read(1)))
end end
end end
@ -13,7 +13,7 @@ local function read_pixeldata_v0(image, fp)
for y = 1, image.h do for y = 1, image.h do
local line = { s = "", bg = "", fg = "" } local line = { s = "", bg = "", fg = "" }
for x = 1, image.w do for x = 1, image.w do
local data = fp:read(2) local data = fp.read(2)
if data == nil or #data == 0 then if data == nil or #data == 0 then
return nil, string.format("Failed to read character at x=%d y=%d", x, y) return nil, string.format("Failed to read character at x=%d y=%d", x, y)
end end
@ -38,7 +38,7 @@ local function read_varint(fp)
local offset = 0 local offset = 0
repeat repeat
if offset >= 5 then return nil, "varint too long" end 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)) value = bit.bor(value, bit.blshift(bit.band(current, 0x7f), offset * 7))
offset = offset + 1 offset = offset + 1
until bit.band(current, 0x80) == 0 until bit.band(current, 0x80) == 0
@ -46,8 +46,8 @@ local function read_varint(fp)
end end
decoders[0] = function(image, fp) decoders[0] = function(image, fp)
image.w, image.h = string.byte(fp:read(1)), string.byte(fp:read(1)) 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.scale = 0.5 + string.byte(fp.read(1)) * 5 / 255
read_palette_full(image.palette, fp) read_palette_full(image.palette, fp)
local success, err = read_pixeldata_v0(image, fp) local success, err = read_pixeldata_v0(image, fp)
if not success then return false, err end if not success then return false, err end
@ -68,7 +68,7 @@ local function parse(fp)
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 = {} }
local magic = fp:read(4) local magic = fp.read(4)
if magic == "CCPI" then if magic == "CCPI" then
res, err = decoders[0](image, fp) res, err = decoders[0](image, fp)
elseif magic:sub(1, 3) == "CPI" then elseif magic:sub(1, 3) == "CPI" then
@ -89,7 +89,7 @@ local function load(path)
local fp, err = io.open(path, "rb") local fp, err = io.open(path, "rb")
if not fp then return nil, err end if not fp then return nil, err end
local img local img
img, err = parse(fp) img, err = parse(fp._handle)
fp:close() fp:close()
return img, err return img, err
end end