png: tests for maxSide / reduce (every PngSuite and generated image, APNG frames, a 2048x2048 16-bit fixture), yield; paeth for 16-bit RGB rows; jpeg: opts.maxSide (scale 1..8) and opts.yield reach the codec; Deflate.zlibStream tests
This commit is contained in:
1 parent
e3e6273799
commit
64a2553c0c
8 files changed
+321
-9
No files matched your search
@@ -22,9 +22,11 @@ local Jpeg = {
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
scale: number?, -- 1 (default), 2, 4 or 8
|
||||
maxSide: number?, -- the smallest scale (up to 8) whose output sides are at most this
|
||||
orient: boolean?, -- default false
|
||||
native: boolean?, -- default false
|
||||
maxPixels: number?, -- default 2^26
|
||||
yield: (() -> ())?, -- pcall'ed every MCU row of each Huffman scan, every block row of the IDCT, every 8 output rows
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
quality: number?, -- 1..100, default 90 (libjpeg scaling of the T.81 annex K tables)
|
||||
@@ -55,12 +57,33 @@ function Jpeg.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.
|
||||
return nil, Err.new(Err.ARG, "options must be a table")
|
||||
end
|
||||
local o = opts or {} :: DecodeOptions
|
||||
local scale = o.scale
|
||||
if o.maxSide ~= nil then
|
||||
local m = o.maxSide
|
||||
if type(m) ~= "number" or not (m >= 1) then
|
||||
return nil, Err.new(Err.ARG, "maxSide must be at least 1")
|
||||
end
|
||||
local info, ierr = Codec.info(r.buf, r.base, r.limit - r.base)
|
||||
if not info then
|
||||
return nil, ierr
|
||||
end
|
||||
-- the output side at scale s is ceil(side / s) (libjpeg-turbo jdmaster.c)
|
||||
local s = if type(scale) == "number" then scale else 1
|
||||
while s < 8 and ((info.width + s - 1) // s > m or (info.height + s - 1) // s > m) do
|
||||
s *= 2
|
||||
end
|
||||
scale = s
|
||||
end
|
||||
if o.yield ~= nil and type(o.yield) ~= "function" then
|
||||
return nil, Err.new(Err.ARG, "yield must be a function")
|
||||
end
|
||||
-- two JPEGs back to back are an image and a preview, as in MPF
|
||||
return Codec.decode(r.buf, r.base, r.limit - r.base, {
|
||||
scale = o.scale,
|
||||
scale = scale,
|
||||
orient = o.orient,
|
||||
native = o.native,
|
||||
maxPixels = o.maxPixels,
|
||||
yield = o.yield,
|
||||
fields = false,
|
||||
})
|
||||
end
|
||||
|
||||
@@ -102,6 +102,30 @@ local function paeth3(raw: buffer, cur: number, e: number, stride: number, pt: b
|
||||
end
|
||||
end
|
||||
|
||||
-- 16-bit RGB: two three-byte lanes carried as in paeth3
|
||||
local function paeth6(raw: buffer, cur: number, e: number, stride: number, pt: buffer)
|
||||
local a0, a1, a2, a3, a4, a5 = readu8(raw, cur), readu8(raw, cur + 1), readu8(raw, cur + 2), readu8(raw, cur + 3), readu8(raw, cur + 4), readu8(raw, cur + 5)
|
||||
local u = cur - stride
|
||||
local c0, c1, c2, c3, c4, c5 = readu8(raw, u), readu8(raw, u + 1), readu8(raw, u + 2), readu8(raw, u + 3), readu8(raw, u + 4), readu8(raw, u + 5)
|
||||
for i = cur + 6, e - 6, 6 do
|
||||
local j = i - stride
|
||||
local b0, b1, b2, b3, b4, b5 = readu8(raw, j), readu8(raw, j + 1), readu8(raw, j + 2), readu8(raw, j + 3), readu8(raw, j + 4), readu8(raw, j + 5)
|
||||
a0 = (readu8(raw, i) + c0 + readi16(pt, (a0 * 511 + b0 - c0 * 512) * 2 + 261120)) % 256
|
||||
a1 = (readu8(raw, i + 1) + c1 + readi16(pt, (a1 * 511 + b1 - c1 * 512) * 2 + 261120)) % 256
|
||||
a2 = (readu8(raw, i + 2) + c2 + readi16(pt, (a2 * 511 + b2 - c2 * 512) * 2 + 261120)) % 256
|
||||
a3 = (readu8(raw, i + 3) + c3 + readi16(pt, (a3 * 511 + b3 - c3 * 512) * 2 + 261120)) % 256
|
||||
a4 = (readu8(raw, i + 4) + c4 + readi16(pt, (a4 * 511 + b4 - c4 * 512) * 2 + 261120)) % 256
|
||||
a5 = (readu8(raw, i + 5) + c5 + readi16(pt, (a5 * 511 + b5 - c5 * 512) * 2 + 261120)) % 256
|
||||
writeu8(raw, i, a0)
|
||||
writeu8(raw, i + 1, a1)
|
||||
writeu8(raw, i + 2, a2)
|
||||
writeu8(raw, i + 3, a3)
|
||||
writeu8(raw, i + 4, a4)
|
||||
writeu8(raw, i + 5, a5)
|
||||
c0, c1, c2, c3, c4, c5 = b0, b1, b2, b3, b4, b5
|
||||
end
|
||||
end
|
||||
|
||||
-- any bpp: the left byte is the one written bpp bytes earlier
|
||||
local function paethN(raw: buffer, cur: number, e: number, stride: number, bpp: number, pt: buffer)
|
||||
for i = cur + bpp, e - 1 do
|
||||
@@ -213,6 +237,8 @@ local function unfilter(raw: buffer, p: number, ph: number, rb: number, bpp: num
|
||||
paeth4(raw, cur, e, stride, pt)
|
||||
elseif bpp == 3 then
|
||||
paeth3(raw, cur, e, stride, pt)
|
||||
elseif bpp == 6 then
|
||||
paeth6(raw, cur, e, stride, pt)
|
||||
elseif bpp == 1 then
|
||||
paeth1(raw, cur, e, stride, pt)
|
||||
else
|
||||
|
||||
@@ -29,8 +29,8 @@ export type DecodeOptions = {
|
||||
maxPixels: number?, -- default 2^26, of the source image
|
||||
maxSide: number?, -- output sides at most this: the smallest power-of-two box reduction that fits
|
||||
reduce: number?, -- box reduction factor, a power of two (default 1)
|
||||
yield: (() -> ())?, -- pcall'ed every yieldRows rows (default 16) and per inflate window when interlaced
|
||||
yieldRows: number?,
|
||||
yield: (() -> ())?, -- pcall'ed every yieldRows rows, and per 64 KB inflated when interlaced
|
||||
yieldRows: number?, -- default: about 64 KB of row data
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
level: number?, -- 0..9, default 6
|
||||
@@ -117,8 +117,10 @@ local function pixels(b: buffer, chunks: { { number } }, w: number, ht: number,
|
||||
local k = rd.k
|
||||
local channels = Decode.CHANNELS[ct]
|
||||
if interlace == 0 then
|
||||
local window = math.min(262144, ht * (Decode.rowBytes(w, channels, depth) + 1))
|
||||
local sink, status = Decode.stream(w, ht, depth, ct, pal, tr, k, out, rd.hook, rd.every)
|
||||
local stride = Decode.rowBytes(w, channels, depth) + 1
|
||||
local window = math.min(if rd.hook then 65536 else 262144, ht * stride)
|
||||
local every = if rd.every > 0 then rd.every else math.max(1, 65536 // stride)
|
||||
local sink, status = Decode.stream(w, ht, depth, ct, pal, tr, k, out, rd.hook, every)
|
||||
local _, zerr = Deflate.zlibStream(b, chunks, sink, window)
|
||||
local y, derr = status()
|
||||
if derr then
|
||||
@@ -153,14 +155,14 @@ local function pixels(b: buffer, chunks: { { number } }, w: number, ht: number,
|
||||
pcall(hook)
|
||||
end
|
||||
return 0
|
||||
end, math.min(262144, expected))
|
||||
end, math.min(if hook then 65536 else 262144, expected))
|
||||
if zerr then
|
||||
return zerr
|
||||
elseif got < expected then
|
||||
return Err.new(Err.CORRUPT, string.format("inflate: output is %d bytes, expected %d", got, expected))
|
||||
end
|
||||
local full = if k > 1 then buffer.create(w * ht * 4) else out
|
||||
local ok, derr = Decode.run(raw, w, ht, depth, ct, pal, tr, full, hook, rd.every)
|
||||
local ok, derr = Decode.run(raw, w, ht, depth, ct, pal, tr, full, hook, if rd.every > 0 then rd.every else math.max(1, 65536 // (w * 4)))
|
||||
if not ok then
|
||||
return Err.new(Err.CORRUPT, tostring(derr))
|
||||
end
|
||||
@@ -193,8 +195,8 @@ local function reduction(w: number, ht: number, opts: DecodeOptions?): (Reduce?,
|
||||
if hook ~= nil and type(hook) ~= "function" then
|
||||
return nil, Err.new(Err.ARG, "yield must be a function")
|
||||
end
|
||||
local every = o.yieldRows or 16
|
||||
if type(every) ~= "number" or not (every >= 1) or every % 1 ~= 0 then
|
||||
local every = o.yieldRows or 0
|
||||
if type(every) ~= "number" or not (every >= 0) or every % 1 ~= 0 then
|
||||
return nil, Err.new(Err.ARG, "yieldRows must be a positive integer")
|
||||
end
|
||||
return { k = k, hook = hook, every = every }, nil
|
||||
|
||||
Reference in new issue
Block a user