merge ws/imgA: jpeg gif tiff tga qoi pnm ico hdr, codecs Dct/Lzw/Quantize, ops/ImageOps, api image hooks
This commit is contained in:
commit
2db77a7db8
55 files changed
+13401
-2
No files matched your search
+24
-2
@@ -231,6 +231,28 @@ local function unwrap(value: any, formatName: string, o: Options): any
|
||||
return v
|
||||
end
|
||||
|
||||
-- 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 {}
|
||||
@@ -252,7 +274,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
|
||||
@@ -347,7 +369,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