imgA: codecs/Lzw (GIF LSB + TIFF MSB early-change, old-style TIFF LZW) with Pillow / libtiff fixtures; api passes opts.decodeImage / encodeImage hooks to formats (embedded PNG in ICO, JPEG in TIFF)
Change outside the ws folders: src/Formats/init.luau gains withHooks(): F.load / F.save hand decoders / encoders opts.decodeImage(buf) and opts.encodeImage(img, format) unless the caller passed its own, so a format can decode an embedded image of another format without requiring it (LAYERS). Backward compatible: formats ignore unknown opts.
This commit is contained in:
1 parent
ccb06775c5
commit
2eee0bc1a6
5 files changed
+496
-2
No files matched your search
+24
-2
@@ -164,6 +164,28 @@ end
|
||||
-- ------------------------------------------------------------------------------------------------ API
|
||||
export type Options = { [string]: any } -- format (name or extension), category, path, data, name, + format options
|
||||
|
||||
-- Embedded images (PNG inside ICO, JPEG inside TIFF, thumbnails, textures): a format never requires another format,
|
||||
-- so the api hands decoders opts.decodeImage(buf) -> Image | nil, Err and encoders opts.encodeImage(img, format) ->
|
||||
-- buffer | nil, Err (unless the caller passed its own). Formats work without them (returning Err.UNSUPPORTED for
|
||||
-- the embedded parts) when called directly.
|
||||
local function withHooks(o: Options): Options
|
||||
if o.decodeImage ~= nil and o.encodeImage ~= nil then
|
||||
return o
|
||||
end
|
||||
local c = table.clone(o)
|
||||
if c.decodeImage == nil then
|
||||
c.decodeImage = function(b: buffer): (any, Err.Err?)
|
||||
return F.load(b, { category = "image" })
|
||||
end
|
||||
end
|
||||
if c.encodeImage == nil then
|
||||
c.encodeImage = function(img: any, format: string): (any, Err.Err?)
|
||||
return F.save(img, nil, { format = format, category = "image" })
|
||||
end
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
-- load(src, opts?) -> value | nil, Err
|
||||
function F.load(src: any, opts: Options?): (any, Err.Err?)
|
||||
local o = opts or {}
|
||||
@@ -181,7 +203,7 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
|
||||
end
|
||||
local value, err
|
||||
if m.decode then
|
||||
local ok, v, e = pcall(m.decode, Bytes.reader(buf), o)
|
||||
local ok, v, e = pcall(m.decode, Bytes.reader(buf), withHooks(o))
|
||||
if not ok then
|
||||
return nil, Err.wrap(Err.new(Err.CORRUPT, "decoder threw: " .. tostring(v)), entry.name)
|
||||
end
|
||||
@@ -274,7 +296,7 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
|
||||
if not entry then
|
||||
return nil, err
|
||||
end
|
||||
local ok, buf, e = pcall(m.encode, value, o)
|
||||
local ok, buf, e = pcall(m.encode, value, withHooks(o))
|
||||
if not ok then
|
||||
return nil, Err.wrap(Err.new(Err.ARG, "encoder threw: " .. tostring(buf)), entry.name)
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user