forked from hkc/cc-stuff
ccpi.lua now can accept handles
This commit is contained in:
parent
a3b079e638
commit
38cce4226a
|
@ -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)
|
||||
|
||||
|
|
18
ccpi.lua
18
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
|
||||
|
|
Loading…
Reference in New Issue