merge ws/downscale

This commit is contained in:
4DBug committed 2026-11-02 23:41:47 -06:00
commit f1d98ec904
24 files changed
+3432 -1340

No files matched your search

+519 -101
View File
@@ -1523,7 +1523,7 @@ local errs= {}
data = resolve(tex.uri)
end
if data ~= nil then
local img, err = F.load(data, { category = "image", name = tex.uri, data = true })
local img, err = F.load(data, { category = "image", name = tex.uri, data = true, maxSide = opts and opts.maxSide, yield = opts and opts.yield, yieldRows = opts and opts.yieldRows, maxPixels = opts and opts.maxPixels })
if img then
tex.image = img
elseif err then
@@ -65028,6 +65028,93 @@ local b, o, l, aerr = args("Deflate.zlib", buf, offset, length)
return out, consumed + 4
end
-- A zlib stream split over byte ranges of buf (PNG IDAT / fdAT payloads), inflated through a window of 32 KB of history
-- plus `window` bytes (default 256 KB): sink(out, from, to) gets each run of new bytes out[from, to) and returns how many
-- bytes before `to` it has not consumed yet (they are handed back before the next run); a negative return aborts. The
-- input is read through a 128 KB window when there is more than one range. Returns the inflated size.
local INWIN = 131072
function Deflate.zlibStream(buf, ranges, sink, window)
if type(buf) ~= "buffer" or type(ranges) ~= "table" or #ranges == 0 or type(sink) ~= "function" then
return nil, Err.new(Err.ARG, "Deflate.zlibStream: expected a buffer, a list of { offset, length } and a sink")
end
local total = 0
for _, r in __iter(ranges) do
if type(r[1]) ~= "number" or type(r[2]) ~= "number" or r[1] < 0 or r[2] < 0 or r[1] + r[2] > buffer.len(buf) then
return nil, Err.new(Err.ARG, "Deflate.zlibStream: range out of bounds")
end
total =total+ r[2]
end
if total < 6 then
return nil, Err.new(Err.TRUNCATED, "zlib: truncated header", ranges[1][1])
end
local src, pos, endpos = buf, ranges[1][1], ranges[1][1] + ranges[1][2]
local feed= nil
if #ranges > 1 then
local win = buffer.create(INWIN)
local ri, ro, fill = 1, 0, 0
feed = function(at)
local keep = math.max(at - 8, 0)
if keep > 0 then
buffer.copy(win, 0, win, keep, fill - keep)
fill =fill- keep
end
while fill < INWIN and ri <= #ranges do
local r = ranges[ri]
local k = math.min(r[2] - ro, INWIN - fill)
buffer.copy(win, fill, buf, r[1] + ro, k)
fill =fill+ k
ro =ro+ k
if ro >= r[2] then
ri =ri+ 1
ro = 0
end
end
return at - keep, fill
end
src = win
pos, endpos = (feed )(0)
end
if endpos - pos < 2 then
return nil, Err.new(Err.TRUNCATED, "zlib: truncated header", ranges[1][1])
end
local cmf, flg = buffer.readu8(src, pos), buffer.readu8(src, pos + 1)
if cmf % 16 ~= 8 or math.floor(cmf / 16 )> 7 then
return nil, Err.new(Err.CORRUPT, "zlib: not a deflate stream (CMF " .. cmf .. ")", ranges[1][1])
end
if (cmf * 256 + flg) % 31 ~= 0 then
return nil, Err.new(Err.CORRUPT, "zlib: header check failed", ranges[1][1])
end
if bit32.band(flg, 0x20) ~= 0 then
return nil, Err.new(Err.UNSUPPORTED, "zlib: preset dictionaries are not supported", ranges[1][1])
end
local adler, size = 1, 0
local function counted(out, from, to)
adler = Adler32.raw(out, from, to - from, adler)
size =size+ (to - from
) return sink(out, from, to)
end
local out, n, consumed = Inflate.raw(src, pos + 2, endpos, 32768 + (window or 262144), false, false, nil, counted, feed)
if not out then
if n == "aborted" then
return nil, Err.new(Err.CORRUPT, "inflate: aborted by the sink")
end
return nil, inflateErr(tostring(n))
end
local at = consumed
if feed and at + 4 > endpos then
at, endpos = feed(at)
end
if at + 4 > endpos then
return nil, Err.new(Err.TRUNCATED, "zlib: missing Adler-32")
end
local want = bit32.byteswap(buffer.readu32(src, at))
if adler ~= want then
return nil, Err.new(Err.CHECKSUM, string.format("zlib: Adler-32 mismatch (%08x, expected %08x)", adler, want))
end
return size, nil
end
local function gzipBody(b, p, e)
local flg = buffer.readu8(b, p + 3)
if flg >= 0x20 then
@@ -65793,7 +65880,7 @@ do
FDISTS = Huffman.buildLsbMap(d, 0, 30, FDIST, DISTBITS, DMAP)
end
-- scratch tables for dynamic blocks, shared by every call (raw never yields)
-- scratch tables for dynamic blocks, shared by every call that has no sink (only a sink may yield)
local SLIT, SDIST, SCL, SLENS = bcreate(LITCAP * 4), bcreate(DISTCAP * 4), bcreate(CLCAP * 4), bcreate(320)
local Inflate = {}
@@ -65805,7 +65892,26 @@ local cap = math.max(blen(out) * 2, need, 1024)
return nb, cap
end
function Inflate.raw(src, start, endpos, cap, exact, prefix, dict)
-- streaming: sink(out, from, to) takes the new bytes [from, to) and returns how many bytes before to it has not
-- consumed yet (< 0 aborts); out keeps those and 32 KB of history. feed(pos) slides the input window: it keeps 8 bytes
-- before pos (the bit buffer's) and returns the new pos and endpos.
local function drain(out, op, seen, need, sink)
local pending = sink(out, seen, op)
if pending < 0 then
return out, -1
end
local from = op - math.max(math.min(pending, op), 32768)
if from > 0 then
bcopy(out, 0, out, from, op - from)
op =op- from
end
if op + need > blen(out) then
out = grow(out, op, op + need)
end
return out, op
end
function Inflate.raw(src, start, endpos, cap, exact, prefix, dict, sink, feed)
local op = 0
if dict then
op = blen(dict)
@@ -65817,9 +65923,12 @@ local op = 0
end
local pos = start
local bitbuf, bitcnt, over = 0, 0, 0
local lt, dt = SLIT, SDIST
local clt = SCL
local lens = SLENS
local slit, sdist, clt, lens = SLIT, SDIST, SCL, SLENS
if sink then
slit, sdist, clt, lens = bcreate(LITCAP * 4), bcreate(DISTCAP * 4), bcreate(CLCAP * 4), bcreate(320)
end
local lt, dt = slit, sdist
local seen = 0
local final = 0
repeat
local __DARKLUA_CONTINUE_4=false repeat -- block header (3 bits); also up to 14 bits of a dynamic header
@@ -65830,6 +65939,9 @@ local __DARKLUA_CONTINUE_4=false repeat -- block header (3 bits); also up to 14
pos =pos+ nb
bitcnt =bitcnt+ nb * 8
else
if feed then
pos, endpos = feed(pos)
end
while bitcnt <= 24 do
if pos < endpos then
bitbuf = bor(bitbuf, lshift(readu8(src, pos), bitcnt))
@@ -65858,6 +65970,9 @@ local __DARKLUA_CONTINUE_4=false repeat -- block header (3 bits); also up to 14
end
pos =pos- real
bitbuf, bitcnt, over = 0, 0, 0
if feed and pos + 65540 > endpos then
pos, endpos = feed(pos)
end
if pos + 4 > endpos then
return nil, "truncated input"
end
@@ -65876,7 +65991,16 @@ local __DARKLUA_CONTINUE_4=false repeat -- block header (3 bits); also up to 14
end
return nil, "output larger than expected"
end
out, cap = grow(out, op, op + n)
if sink then
out, op = drain(out, op, seen, n, sink)
if op < 0 then
return nil, "aborted"
end
seen = op
cap = blen(out)
else
out, cap = grow(out, op, op + n)
end
end
bcopy(out, op, src, pos, n)
op =op+ n
@@ -65888,7 +66012,7 @@ if btype == 1 then
lt, dt = FLIT, FDIST
lsub, dsub = FLITS, FDISTS
elseif btype == 2 then
lt, dt = SLIT, SDIST
lt, dt = slit, sdist
local hlit = band(bitbuf, 31) + 257
local hdist = band(rshift(bitbuf, 5), 31) + 1
local hclen = band(rshift(bitbuf, 10), 15) + 4
@@ -65906,6 +66030,9 @@ if btype == 1 then
pos =pos+ nb
bitcnt =bitcnt+ nb * 8
else
if feed then
pos, endpos = feed(pos)
end
while bitcnt <= 24 do
if pos < endpos then
bitbuf = bor(bitbuf, lshift(readu8(src, pos), bitcnt))
@@ -65937,6 +66064,9 @@ if btype == 1 then
pos =pos+ nb
bitcnt =bitcnt+ nb * 8
else
if feed then
pos, endpos = feed(pos)
end
while bitcnt <= 24 do
if pos < endpos then
bitbuf = bor(bitbuf, lshift(readu8(src, pos), bitcnt))
@@ -66011,6 +66141,9 @@ if btype == 1 then
pos =pos+ nb
bitcnt =bitcnt+ nb * 8
else
if feed then
pos, endpos = feed(pos)
end
while bitcnt <= 24 do
if pos < endpos then
bitbuf = bor(bitbuf, lshift(readu8(src, pos), bitcnt))
@@ -66048,7 +66181,16 @@ if btype == 1 then
end
return nil, "output larger than expected"
end
out, cap = grow(out, op, op + 1)
if sink then
out, op = drain(out, op, seen, 1, sink)
if op < 0 then
return nil, "aborted"
end
seen = op
cap = blen(out)
else
out, cap = grow(out, op, op + 1)
end
end
writeu8(out, op, sym)
op =op+ 1
@@ -66065,6 +66207,9 @@ if btype == 1 then
pos =pos+ nb
bitcnt =bitcnt+ nb * 8
else
if feed then
pos, endpos = feed(pos)
end
while bitcnt <= 24 do
if pos < endpos then
bitbuf = bor(bitbuf, lshift(readu8(src, pos), bitcnt))
@@ -66111,6 +66256,9 @@ if btype == 1 then
pos =pos+ nb
bitcnt =bitcnt+ nb * 8
else
if feed then
pos, endpos = feed(pos)
end
while bitcnt <= 24 do
if pos < endpos then
bitbuf = bor(bitbuf, lshift(readu8(src, pos), bitcnt))
@@ -66139,7 +66287,16 @@ if btype == 1 then
end
return nil, "output larger than expected"
end
out, cap = grow(out, op, op + len)
if sink then
out, op = drain(out, op, seen, len, sink)
if op < 0 then
return nil, "aborted"
end
seen = op
cap = blen(out)
else
out, cap = grow(out, op, op + len)
end
end
local s = op - dist
if dist >= len then
@@ -66179,6 +66336,9 @@ __DARKLUA_CONTINUE_4=true until true if not __DARKLUA_CONTINUE_4 then break end
if consumed > endpos then
return nil, "truncated input"
end
if sink and sink(out, seen, op) < 0 then
return nil, "aborted"
end
return out, op, consumed
end
@@ -77968,7 +78128,7 @@ local ns = (p < e and{(readu8(b, p) )}or{0
clean = buffer.create(n - base + Scan.PAD)
end
local cb = clean
local nextPos, cleanLen, nrst = Scan.destuff(b, pos, n, cb, rst)
local nextPos, cleanLen, nrst = Scan.destuff(b, pos, n, cb, rst, st.hook)
if nextPos >= n then
st.truncated = true -- no EOI
end
@@ -79204,37 +79364,45 @@ local t = buffer.create(Scan.TABLE_SIZE)
return t
end
function Scan.destuff(src, pos, n, dst, rst)
function Scan.destuff(src, pos, n, dst, rst, hook)
local o, nr = 0, 0
while pos < n do
local b = readu8(src, pos)
if b ~= 0xFF then
writeu8(dst, o, b)
o =o+ 1
pos =pos+ 1
else
local q = pos + 1
while q < n and readu8(src, q) == 0xFF do
q =q+ 1
end
if q >= n then
pos = n
break
end
local m = readu8(src, q)
if m == 0 then
writeu8(dst, o, 0xFF)
local done = false
while not done and pos < n do
local lim = (hook and{(math.min(n, pos + 65536) )}or{n
})[1] while pos < lim do
local b = readu8(src, pos)
if b ~= 0xFF then
writeu8(dst, o, b)
o =o+ 1
pos = q + 1
elseif m >= 0xD0 and m <= 0xD7 then
nr =nr+ 1
rst[nr] = o
pos = q + 1
pos =pos+ 1
else
pos = q - 1
break
local q = pos + 1
while q < n and readu8(src, q) == 0xFF do
q =q+ 1
end
if q >= n then
pos = n
break
end
local m = readu8(src, q)
if m == 0 then
writeu8(dst, o, 0xFF)
o =o+ 1
pos = q + 1
elseif m >= 0xD0 and m <= 0xD7 then
nr =nr+ 1
rst[nr] = o
pos = q + 1
else
pos = q - 1
done = true
break
end
end
end
if hook and not done and pos < n then
pcall(hook)
end
end
buffer.fill(dst, o, 0, Scan.PAD)
return pos, o, nr
@@ -148578,6 +148746,8 @@ local Jpeg = {
function Jpeg.sniff(r)
@@ -148594,13 +148764,34 @@ function Jpeg.decode(r, opts)
if opts ~= nil and type(opts) ~= "table" then
return nil, Err.new(Err.ARG, "options must be a table")
end
local o = opts or {} -- 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,
local o = opts or {}
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 = (type(scale) == "number" and{scale }or{1
})[1] while s < 8 and (math.floor((info.width + s - 1) / s )> m or math.floor((info.height + s - 1) / s )> m) do
s =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 = scale,
orient = o.orient,
native = o.native,
maxPixels = o.maxPixels,
yield = o.yield,
fields = false,
})
end
@@ -149733,8 +149924,9 @@ end
__FILES["image/png"] = function()
local __compat = __compat local buffer, bit32, type, __iter = __compat.buffer, __compat.bit32, __compat.type, __compat.__iter --!strict
-- image/png: PNG (ISO/IEC 15948, W3C PNG 3rd ed.) and APNG 1.0; from png-luau 1.0.0 (same author). Supports: every
-- colour type and bit depth, tRNS, Adam7, tEXt, acTL / fcTL / fdAT; writes RGBA / RGB / grey(+alpha) and APNG. Not
-- supported: colour management (iCCP kept raw), palette / 16-bit / interlaced output.
-- colour type and bit depth, tRNS, Adam7, tEXt, acTL / fcTL / fdAT, row-streaming decode with power-of-two box
-- reduction; writes RGBA / RGB / grey(+alpha) and APNG. Not supported: colour management (iCCP kept raw), palette /
-- 16-bit / interlaced output.
-- ------------------------------------------------------------------------------------------------ requires
local Err = __include("core/Err")
@@ -149768,6 +149960,10 @@ local Png = {
local SIGNATURE = "\137PNG\r\n\26\n"
local VALID = {
[0] = { [1] = true, [2] = true, [4] = true, [8] = true, [16] = true },
@@ -149840,50 +150036,97 @@ end
local DISPOSE= { [0] = "none", [1] = "background", [2] = "previous" }
local function pixels(b, chunks, total, w, ht, depth, ct, interlace, pal, tr, out) -- the inflated size: every (pass) row has a filter byte
local channels = Decode.CHANNELS[ct]
local expected = 0
local function pixels(b, chunks, w, ht, depth, ct, interlace, pal, tr, out, rd)
local k = rd.k
local channels = Decode.CHANNELS[ct]
if interlace == 0 then
expected = ht * (Decode.rowBytes(w, channels, depth) + 1)
else
for _, pass in __iter(Decode.PASSES) do
local x0, y0, dx, dy = pass[1], pass[2], pass[3], pass[4]
local pw = (w > x0 and{(math.floor((w - x0 + dx - 1) / dx ))}or{0
})[1] local ph = (ht > y0 and{(math.floor((ht - y0 + dy - 1) / dy ))}or{0
})[1] if pw > 0 and ph > 0 then
expected =expected+ ph * (Decode.rowBytes(pw, channels, depth) + 1)
end
local stride = Decode.rowBytes(w, channels, depth) + 1
local window = math.min(rd.hook and 65536 or 262144, ht * stride)
local every = (rd.every > 0 and{rd.every }or{(math.max(1, math.floor(65536 / stride))
)})[1] 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
return Err.new(Err.CORRUPT, derr)
elseif zerr then
return zerr
elseif y < ht then
return Err.new(Err.CORRUPT, string.format("image data ends after %d of %d rows", y, ht))
end
return nil
end
-- Adam7: the whole inflated stream (every pass row has a filter byte), then the full image when reducing
local expected = 0
for _, pass in __iter(Decode.PASSES) do
local x0, y0, dx, dy = pass[1], pass[2], pass[3], pass[4]
local pw = (w > x0 and{(math.floor((w - x0 + dx - 1) / dx ))}or{0
})[1] local ph = (ht > y0 and{(math.floor((ht - y0 + dy - 1) / dy ))}or{0
})[1] if pw > 0 and ph > 0 then
expected =expected+ ph * (Decode.rowBytes(pw, channels, depth) + 1)
end
end
local z
if #chunks == 1 then
z = b
else
z = buffer.create(total)
local w0 = 0
for _, c in __iter(chunks) do
buffer.copy(z, w0, b, c[1], c[2])
w0 =w0+ c[2]
local raw = buffer.create(expected)
local got = 0
local hook = rd.hook
local _, zerr = Deflate.zlibStream(b, chunks, function(win, from, to)
local n = math.min(to - from, expected - got)
if n > 0 then
buffer.copy(raw, got, win, from, n)
got =got+ n
end
end
local zo = (#chunks == 1 and{chunks[1][1] }or{0
})[1] local raw, zerr = Deflate.zlib(z, zo, total, expected)
if not raw then
-- some encoders leave extra bytes after the rows: inflate without the exact size, keep the rows
local loose = Deflate.zlib(z, zo, total)
if not loose or buffer.len(loose) < expected then
return zerr or Err.new(Err.CORRUPT, "bad image data")
if hook then
pcall(hook)
end
raw = loose
return 0
end, math.min(hook and 65536 or 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 ok, derr = Decode.run(raw , w, ht, depth, ct, interlace, pal, tr, out)
local full = (k > 1 and{(buffer.create(w * ht * 4) )}or{out
})[1] local ok, derr = Decode.run(raw, w, ht, depth, ct, pal, tr, full, hook, (rd.every > 0 and{rd.every }or{(math.max(1, math.floor(65536 / (w * 4))))})[1])
if not ok then
return Err.new(Err.CORRUPT, tostring(derr))
end
if k > 1 then
Decode.reduce(full, w, ht, k, out, Decode.opaque(ct, tr, pal))
end
return nil
end
local function reduction(w, ht, opts)
local o= opts or {}
local k = 1
if o.reduce ~= nil then
local r = o.reduce
if type(r) ~= "number" or r < 1 or r > 2 ^ 31 or r % 1 ~= 0 or bit32.band(r, r - 1) ~= 0 then
return nil, Err.new(Err.ARG, "reduce must be a power of two")
end
k = r
end
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
while math.floor((w + k - 1) / k )> m or math.floor((ht + k - 1) / k )> m do
k =k* 2
end
end
local hook = o.yield
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 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
end
function Png.decode(r, opts)
local checkCrc = opts and opts.checkCrc or false
local maxPixels = opts and opts.maxPixels or 2 ^ 26
@@ -149895,6 +150138,11 @@ local checkCrc = opts and opts.checkCrc or false
if w * ht > maxPixels then
return nil, Err.new(Err.LIMIT, string.format("%d x %d is over the %d pixel limit", w, ht, maxPixels))
end
local rd, rerr = reduction(w, ht, opts)
if not rd then
return nil, rerr
end
local k = rd.k
local b, n = r.buf, r.limit
local pal = buffer.create(1024)
for i = 0, 255 do
@@ -149903,7 +150151,6 @@ local checkCrc = opts and opts.checkCrc or false
local palCount = 0
local tr = -1
local idat= {}
local idatLen = 0
local text= {}
local iccCompressed= nil
-- APNG: acTL, then fcTL (+ IDAT for frame 1 when it is the default image) / fdAT, one sequence counter
@@ -149957,7 +150204,6 @@ return readu8(b, d + q) * 256 + readu8(b, d + q + 1)
end
elseif typ == "IDAT" then
table.insert(idat, { d, len })
idatLen =idatLen+ len
if #fctls == 1 and #idat == 1 then
idatInAnim = true
end
@@ -150035,8 +150281,8 @@ return readu8(b, d + q) * 256 + readu8(b, d + q + 1)
if ct == 3 and palCount == 0 then
return nil, Err.new(Err.CORRUPT, "palette image without PLTE")
end
local img = Types.Image.new(w, ht, "rgba8")
local ierr = pixels(b, idat, idatLen, w, ht, h.bitDepth, ct, h.interlace, pal, tr, img.pixels)
local img = Types.Image.new(math.floor((w + k - 1) / k), math.floor((ht + k - 1) / k), "rgba8")
local ierr = pixels(b, idat, w, ht, h.bitDepth, ct, h.interlace, pal, tr, img.pixels, rd)
if ierr then
return nil, ierr
end
@@ -150049,14 +150295,14 @@ return readu8(b, d + q) * 256 + readu8(b, d + q + 1)
for i, f in __iter(fctls) do
local fimg
if i == 1 and idatInAnim then
fimg = Types.Image.new(w, ht, "rgba8")
fimg = Types.Image.new(img.width, img.height, "rgba8")
buffer.copy(fimg.pixels, 0, img.pixels)
else
if #f.chunks == 0 then
return nil, Err.new(Err.CORRUPT, "APNG frame " .. i .. " has no data")
end
fimg = Types.Image.new(f.width, f.height, "rgba8")
local ferr = pixels(b, f.chunks, f.len, f.width, f.height, h.bitDepth, ct, h.interlace, pal, tr, fimg.pixels)
fimg = Types.Image.new(math.floor((f.width + k - 1) / k), math.floor((f.height + k - 1) / k), "rgba8")
local ferr = pixels(b, f.chunks, f.width, f.height, h.bitDepth, ct, h.interlace, pal, tr, fimg.pixels, rd)
if ferr then
ferr.message = "frame " .. i .. ": " .. ferr.message
return nil, ferr
@@ -150065,8 +150311,8 @@ if i == 1 and idatInAnim then
frames[i] = {
image = fimg,
delay = f.num / (f.den == 0 and 100 or f.den),
x = f.x,
y = f.y,
x = math.floor(f.x / k),
y = math.floor(f.y / k),
dispose = DISPOSE[f.dispose] ,
blend = f.blend == 1 and"over" or"source" ,
}
@@ -150086,7 +150332,7 @@ if i == 1 and idatInAnim then
img.meta.text = text
end
img.meta.iccCompressed = iccCompressed
img.meta.source = { format = "png", bitDepth = depth, colorType = COLOR_NAMES[ct], interlace = h.interlace }
img.meta.source = { format = "png", bitDepth = depth, colorType = COLOR_NAMES[ct], interlace = h.interlace, width = w, height = ht, reduce = k }
return img, nil
end
@@ -150495,6 +150741,30 @@ local function paeth3(raw, cur, e, stride, pt)
end
end
-- 16-bit RGB: two three-byte lanes carried as in paeth3
local function paeth6(raw, cur, e, stride, pt)
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, cur, e, stride, bpp, pt)
for i = cur + bpp, e - 1 do
@@ -150513,10 +150783,10 @@ local function paeth1(raw, cur, e, stride, pt)
end
end
local function unfilter(raw, p, ph, rb, bpp)
-- top = 0: the first row has no previous row; 1: the row before p is its previous row
local function unfilter(raw, p, ph, rb, bpp, pt, top)
local stride = rb + 1
local pt = paethFor(ph * rb)
for r = 0, ph - 1 do
for r = top, top + ph - 1 do
local f = readu8(raw, p)
local cur = p + 1
local e = cur + rb
@@ -150606,6 +150876,8 @@ local stride = rb + 1
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
@@ -150726,34 +150998,180 @@ local function convert(raw, ro, pw, out, oo, ostep, ct, depth, pal, tr)
end
end
function Decode.run(raw, w, h, depth, ct, interlace, pal, tr, out)
-- the k x k box reduction (k a power of two; edge blocks are smaller): the alpha-weighted mean, colour =
-- round(sum(c * a) / sum(a)) and alpha = round(sum(a) / n), halves up; a fully transparent block is 0, 0, 0, 0. sums
-- holds sum(c * a) and sum(a) per output pixel, or plain sums of r, g, b when the image is opaque (the same result).
local function accumRgba(row, ro, w, k, sums, opaque)
local i = 1
for x0 = 0, w - 1, k do
local e = ro + (math.min(x0 + k, w) - 1) * 4
local r, g, b, a = 0, 0, 0, 0
if opaque then
for q = ro + x0 * 4, e, 4 do
local v = readu32(row, q)
r =r+ band(v, 255)
g =g+ band(rshift(v, 8), 255)
b =b+ band(rshift(v, 16), 255)
end
else
for q = ro + x0 * 4, e, 4 do
local v = readu32(row, q)
local al = rshift(v, 24)
r =r+ band(v, 255) * al
g =g+ band(rshift(v, 8), 255) * al
b =b+ band(rshift(v, 16), 255) * al
a =a+ al
end
end
sums[i]=sums[i]+ r
do local __DARKLUA_VAR=i + 1 sums[__DARKLUA_VAR]=sums[__DARKLUA_VAR]+ g
end do local __DARKLUA_VAR0=i + 2 sums[__DARKLUA_VAR0]=sums[__DARKLUA_VAR0]+ b
end do local __DARKLUA_VAR1=i + 3 sums[__DARKLUA_VAR1]=sums[__DARKLUA_VAR1]+ a
end i =i+ 4
end
end
-- truecolour without tRNS, straight from the unfiltered row (16-bit samples: the high byte, as convert does)
local function accumRgb(raw, ro, w, k, sums, depth)
local step = depth == 16 and 6 or 3
local go = math.floor(step / 3
) local bo = go * 2
local i = 1
for x0 = 0, w - 1, k do
local r, g, b = 0, 0, 0
for q = ro + x0 * step, ro + (math.min(x0 + k, w) - 1) * step, step do
r =r+ readu8(raw, q)
g =g+ readu8(raw, q + go)
b =b+ readu8(raw, q + bo)
end
sums[i]=sums[i]+ r
do local __DARKLUA_VAR=i + 1 sums[__DARKLUA_VAR]=sums[__DARKLUA_VAR]+ g
end do local __DARKLUA_VAR0=i + 2 sums[__DARKLUA_VAR0]=sums[__DARKLUA_VAR0]+ b
end i =i+ 4
end
end
local function emit(sums, out, oo, w, k, bh, opaque)
local i = 1
for x0 = 0, w - 1, k do
local n = (math.min(x0 + k, w) - x0) * bh
local r, g, b, a = sums[i], sums[i + 1], sums[i + 2], sums[i + 3]
local v = 0
if opaque then
local hn = math.floor(n / 2
) v = math.floor((r + hn) / n )+ math.floor((g + hn) / n )* 256 + math.floor((b + hn) / n )* 65536 + 0xFF000000
elseif a > 0 then
local ha = math.floor(a / 2
) v = math.floor((r + ha) / a )+ math.floor((g + ha) / a )* 256 + math.floor((b + ha) / a )* 65536 + math.floor((a + math.floor(n / 2)) / n )* 16777216
end
writeu32(out, oo, v)
oo =oo+ 4
sums[i], sums[i + 1], sums[i + 2], sums[i + 3] = 0, 0, 0, 0
i =i+ 4
end
end
function Decode.opaque(ct, tr, pal)
if ct == 4 or ct == 6 then
return false
elseif ct == 3 then
for i = 3, 1023, 4 do
if readu8(pal, i) ~= 255 then
return false
end
end
return true
end
return tr < 0
end
function Decode.reduce(src, w, h, k, out, opaque)
local ow = math.floor((w + k - 1) / k
) local sums = table.create(ow * 4, 0)
for y = 0, h - 1 do
accumRgba(src, y * w * 4, w, k, sums, opaque)
if (y + 1) % k == 0 or y == h - 1 then
emit(sums, out, (math.floor(y / k)) * ow * 4, w, k, y % k + 1, opaque)
end
end
end
-- a row-streaming decoder of a non-interlaced image for a streaming inflate sink: unfilters each row against the
-- previous one, writes it (k = 1) or box-reduces it into out; hook is called every `every` rows
function Decode.stream(w, h, depth, ct, pal, tr, k, out, hook, every)
local channels = CHANNELS[ct]
local bpp = math.max(1, math.floor((channels * depth) / 8))
local rb = Decode.rowBytes(w, channels, depth)
local stride = rb + 1
local pt = paethFor(h * rb)
local scratch = buffer.create(stride * 2)
local row = buffer.create((k > 1 and((w * 4 ))or(0)))
local ow = math.floor((w + k - 1) / k
) local sums = table.create((k > 1 and((ow * 4 ))or(0)), 0)
local opaque = Decode.opaque(ct, tr, pal)
local direct = ct == 2 and tr < 0
local y, pending = 0, 0
local err= nil
local function sink(win, from, to)
local p = from - pending
while y < h and to - p >= stride do
bcopy(scratch, stride, win, p, stride)
local ok, f = unfilter(scratch, stride, 1, rb, bpp, pt, y == 0 and 0 or 1)
if not ok then
err = "bad filter type " .. f
return -1
end
if k == 1 then
convert(scratch, stride + 1, w, out, y * w * 4, 4, ct, depth, pal, tr)
else
if direct then
accumRgb(scratch, stride + 1, w, k, sums, depth)
else
convert(scratch, stride + 1, w, row, 0, 4, ct, depth, pal, tr)
accumRgba(row, 0, w, k, sums, opaque)
end
if (y + 1) % k == 0 or y == h - 1 then
emit(sums, out, (math.floor(y / k)) * ow * 4, w, k, y % k + 1, opaque)
end
end
bcopy(scratch, 0, scratch, stride, stride)
y =y+ 1
p =p+ stride
if hook and y % every == 0 then
pcall(hook )
end
end
pending = (y < h and((to - p ))or(0
)) return pending
end
return sink, function()
return y, err
end
end
-- Adam7 (the whole inflated stream in raw)
function Decode.run(raw, w, h, depth, ct, pal, tr, out, hook, every)
local channels = CHANNELS[ct]
local bpp = math.max(1, math.floor((channels * depth) / 8))
local p = 0
if interlace == 0 then
local rb = Decode.rowBytes(w, channels, depth)
local ok, f = unfilter(raw, 0, h, rb, bpp)
if not ok then
return false, "bad filter type " .. f
end
for y = 0, h - 1 do
convert(raw, y * (rb + 1) + 1, w, out, y * w * 4, 4, ct, depth, pal, tr)
end
return true, nil
end
local rows = 0
for _, pass in __iter(PASSES) do
local x0, y0, dx, dy = pass[1], pass[2], pass[3], pass[4]
local pw = (w > x0 and{(math.floor((w - x0 + dx - 1) / dx ))}or{0
})[1] local ph = (h > y0 and{(math.floor((h - y0 + dy - 1) / dy ))}or{0
})[1] if pw > 0 and ph > 0 then
local rb = Decode.rowBytes(pw, channels, depth)
local ok, f = unfilter(raw, p, ph, rb, bpp)
local ok, f = unfilter(raw, p, ph, rb, bpp, paethFor(ph * rb), 0)
if not ok then
return false, "bad filter type " .. f
end
for r = 0, ph - 1 do
local y = y0 + r * dy
convert(raw, p + r * (rb + 1) + 1, pw, out, (y * w + x0) * 4, dx * 4, ct, depth, pal, tr)
rows =rows+ 1
if hook and rows % every == 0 then
pcall(hook)
end
end
p =p+ ph * (rb + 1)
end