perf(image): VP8L literal symbols inline, cross-colour identity tiles skipped, T / TR / TL predictors and SWAR addPixels; JPEG IDCT 5x5 path, gray / 4:4:4 colour fast paths; QOI index as tables; TGA rows without per-pixel div/mod; BC7 / BC6H bit reads from a word table; bench_image: bcn / exr

This commit is contained in:
4DBug committed 2026-11-01 19:41:47 -06:00
1 parent 9855a7edd6
commit 903e239a0e
7 files changed
+293 -121

No files matched your search

+8 -13
View File
@@ -175,21 +175,16 @@ local function subset(ns: number, part: number, i: number): number
return 0 return 0
end end
local w0, w1, w2, w3 = 0, 0, 0, 0 -- the block's four u32 words, then zero words (a field reaching past bit 127 reads zeros)
local W = { 0, 0, 0, 0, 0, 0 }
local function load(src: buffer, p: number) local function load(src: buffer, p: number)
w0, w1, w2, w3 = readu32(src, p), readu32(src, p + 4), readu32(src, p + 8), readu32(src, p + 12) W[1], W[2], W[3], W[4] = readu32(src, p), readu32(src, p + 4), readu32(src, p + 8), readu32(src, p + 12)
end
local function word(i: number): number
return if i == 0 then w0 elseif i == 1 then w1 elseif i == 2 then w2 else w3
end end
local function bits(at: number, n: number): number local function bits(at: number, n: number): number
if n == 0 then local wi, sh = rshift(at, 5) + 1, band(at, 31)
return 0 local v = rshift(W[wi], sh)
end if sh + n > 32 then
local wi, sh = rshift(at, 5), band(at, 31) v = bor(v, lshift(W[wi + 1], 32 - sh))
local v = rshift(word(wi), sh)
if sh + n > 32 and wi < 3 then
v = bor(v, lshift(word(wi + 1), 32 - sh))
end end
return band(v, lshift(1, n) - 1) return band(v, lshift(1, n) - 1)
end end
@@ -412,7 +407,7 @@ end
function Decode.bc6h(src: buffer, p: number, blk: buffer, signed: boolean) function Decode.bc6h(src: buffer, p: number, blk: buffer, signed: boolean)
load(src, p) load(src, p)
local m = band(w0, 31) local m = band(W[1], 31)
local mode, bit, epbits, ib local mode, bit, epbits, ib
local low = band(m, 3) local low = band(m, 3)
if low < 2 then if low < 2 then
+18 -14
View File
@@ -124,18 +124,18 @@ local function idct8(coef: buffer, off: number, qt: { number }, plane: buffer, o
end end
end end
-- idct8 with every nonzero coefficient in the top-left 4 x 4 (zigzag index <= 9): the same sums without the zero terms -- idct8 with every nonzero coefficient in the top-left 5 x 5 (zigzag index <= 14): the same sums without the zero terms
local function idct8x4(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) local function idct8x5(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
local ws = ws local ws = ws
local s1 = 2 ^ (13 - p1) local s1 = 2 ^ (13 - p1)
local h1 = s1 / 2 local h1 = s1 / 2
local s2 = 2 ^ (16 + p1) local s2 = 2 ^ (16 + p1)
local h2 = s2 / 2 + center * s2 local h2 = s2 / 2 + center * s2
local dcm = 2 ^ p1 local dcm = 2 ^ p1
for c = 0, 3 do for c = 0, 4 do
local o = off + c * 2 local o = off + c * 2
local c1, c2, c3 = readi16(coef, o + 16), readi16(coef, o + 32), readi16(coef, o + 48) local c1, c2, c3, c4 = readi16(coef, o + 16), readi16(coef, o + 32), readi16(coef, o + 48), readi16(coef, o + 64)
if c1 == 0 and c2 == 0 and c3 == 0 then if c1 == 0 and c2 == 0 and c3 == 0 and c4 == 0 then
local dc = readi16(coef, o) * qt[c + 1] * dcm local dc = readi16(coef, o) * qt[c + 1] * dcm
ws[c + 1] = dc ws[c + 1] = dc
ws[c + 9] = dc ws[c + 9] = dc
@@ -149,15 +149,18 @@ local function idct8x4(coef: buffer, off: number, qt: { number }, plane: buffer,
local z2 = c2 * qt[c + 17] local z2 = c2 * qt[c + 17]
local tmp2 = z2 * 4433 local tmp2 = z2 * 4433
local tmp3 = tmp2 + z2 * 6270 local tmp3 = tmp2 + z2 * 6270
local tmp0 = readi16(coef, o) * qt[c + 1] * 8192 + h1 z2 = readi16(coef, o) * qt[c + 1]
local z3 = c4 * qt[c + 33]
local tmp0 = (z2 + z3) * 8192 + h1
local tmp1 = (z2 - z3) * 8192 + h1
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3 local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
local tmp11, tmp12 = tmp0 + tmp2, tmp0 - tmp2 local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2
local t2 = c3 * qt[c + 25] local t2 = c3 * qt[c + 25]
local t3 = c1 * qt[c + 9] local t3 = c1 * qt[c + 9]
local z5 = (t2 + t3) * 9633 local z5 = (t2 + t3) * 9633
local z1 = t3 * -7373 local z1 = t3 * -7373
z2 = t2 * -20995 z2 = t2 * -20995
local z3 = t2 * -16069 + z5 z3 = t2 * -16069 + z5
local z4 = t3 * -3196 + z5 local z4 = t3 * -3196 + z5
local t0 = z1 + z3 local t0 = z1 + z3
local t1 = z2 + z4 local t1 = z2 + z4
@@ -177,9 +180,9 @@ local function idct8x4(coef: buffer, off: number, qt: { number }, plane: buffer,
local hd = sd / 2 + center * sd local hd = sd / 2 + center * sd
for r = 0, 7 do for r = 0, 7 do
local b = r * 8 local b = r * 8
local w0, w1, w2, w3 = ws[b + 1], ws[b + 2], ws[b + 3], ws[b + 4] local w0, w1, w2, w3, w4 = ws[b + 1], ws[b + 2], ws[b + 3], ws[b + 4], ws[b + 5]
local o = (out + r * stride) * 2 local o = (out + r * stride) * 2
if w1 == 0 and w2 == 0 and w3 == 0 then if w1 == 0 and w2 == 0 and w3 == 0 and w4 == 0 then
local v = clamp((w0 + hd) // sd, 0, maxv) * 65537 local v = clamp((w0 + hd) // sd, 0, maxv) * 65537
writeu32(plane, o, v) writeu32(plane, o, v)
writeu32(plane, o + 4, v) writeu32(plane, o + 4, v)
@@ -188,9 +191,10 @@ local function idct8x4(coef: buffer, off: number, qt: { number }, plane: buffer,
else else
local tmp2 = w2 * 4433 local tmp2 = w2 * 4433
local tmp3 = tmp2 + w2 * 6270 local tmp3 = tmp2 + w2 * 6270
local tmp0 = w0 * 8192 + h2 local tmp0 = (w0 + w4) * 8192 + h2
local tmp1 = (w0 - w4) * 8192 + h2
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3 local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
local tmp11, tmp12 = tmp0 + tmp2, tmp0 - tmp2 local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2
local z5 = (w3 + w1) * 9633 local z5 = (w3 + w1) * 9633
local z1 = w1 * -7373 local z1 = w1 * -7373
local z2 = w3 * -20995 local z2 = w3 * -20995
@@ -347,8 +351,8 @@ function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, si
local k = readu8(nz, bi) local k = readu8(nz, bi)
if k == 0 then if k == 0 then
idct8dc(coef, off, qt, plane, out, stride, p1, center, maxv) idct8dc(coef, off, qt, plane, out, stride, p1, center, maxv)
elseif k <= 9 then elseif k <= 14 then
idct8x4(coef, off, qt, plane, out, stride, p1, center, maxv) idct8x5(coef, off, qt, plane, out, stride, p1, center, maxv)
else else
idct8(coef, off, qt, plane, out, stride, p1, center, maxv) idct8(coef, off, qt, plane, out, stride, p1, center, maxv)
end end
+10 -10
View File
@@ -211,10 +211,7 @@ local function ycc8(P: Params): boolean
local d = cb0 - yb local d = cb0 - yb
for i = yb, yb + outW * 2 - 2, 2 do for i = yb, yb + outW * 2 - 2, 2 do
local yv, cb, cr = readu16(py, i), readu16(pcb, i + d), readu16(pcr, i + d) local yv, cb, cr = readu16(py, i), readu16(pcb, i + d), readu16(pcr, i + d)
local r = clamp(yv + (FIX_R * cr + KR) // 65536, 0, 255) writeu32(out, o, LR[yv + (FIX_R * cr + KRo) // 65536] + LG[yv + (KGo - FIX_GB * cb - FIX_GR * cr) // 65536] + LB[yv + (FIX_B * cb + KBo) // 65536])
local g = clamp(yv + (KG - FIX_GB * cb - FIX_GR * cr) // 65536, 0, 255)
local b = clamp(yv + (FIX_B * cb + KB) // 65536, 0, 255)
writeu32(out, o, 0xFF000000 + r + g * 256 + b * 65536)
o += 4 o += 4
end end
continue continue
@@ -279,13 +276,16 @@ function Color.run(P: Params)
end end
local s0, b0 = fetch(comps[1], y, outW) local s0, b0 = fetch(comps[1], y, outW)
if space == 0 or nc < 3 then if space == 0 or nc < 3 then
for x = 0, outW - 1 do if toByte then
local v = readu16(s0, b0 + x * 2) for x = 0, outW - 1 do
if toByte then writeu32(out, o, 0xFF000000 + readu8(toByte, readu16(s0, b0 + x * 2)) * 0x10101)
v = readu8(toByte, v) o += 4
end
else
for i = b0, b0 + outW * 2 - 2, 2 do
writeu32(out, o, 0xFF000000 + readu16(s0, i) * 0x10101)
o += 4
end end
writeu32(out, o, 0xFF000000 + v * 0x10101)
o += 4
end end
else else
local s1, b1 = fetch(comps[2], y, outW) local s1, b1 = fetch(comps[2], y, outW)
+102 -15
View File
@@ -337,6 +337,7 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
local hmask = if himg then lshift(1, hbits) - 1 else -1 local hmask = if himg then lshift(1, hbits) - 1 else -1
local i, col, row = 0, 0, 0 local i, col, row = 0, 0, 0
local gG, gR, gB, gA, gD = groups[1], groups[2], groups[3], groups[4], groups[5] local gG, gR, gB, gA, gD = groups[1], groups[2], groups[3], groups[4], groups[5]
local tab = d.tab
local function setGroup() local function setGroup()
if himg then if himg then
local g = readu32(himg, (hxs * rshift(row, hbits) + rshift(col, hbits)) * 4) * 5 local g = readu32(himg, (hxs * rshift(row, hbits) + rshift(col, hbits)) * 4) * 5
@@ -347,12 +348,79 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
if band(col, hmask) == 0 then if band(col, hmask) == 0 then
setGroup() setGroup()
end end
local code = readSymbol(d, gG) -- readSymbol inlined for the literal path (the bit state in locals, written back before any call)
local bb, bc = d.bitbuf, d.bitcnt
if bc < MAX_LEN then
d.bitbuf, d.bitcnt = bb, bc
fill(d)
bb, bc = d.bitbuf, d.bitcnt
end
local ecode = readu32(tab, (gG + band(bb, 255)) * 4)
local ncode = band(ecode, 31)
if ncode > ROOT_BITS then
local idx = gG + band(bb, 255)
bb = rshift(bb, ROOT_BITS)
bc -= ROOT_BITS
ecode = readu32(tab, (idx + rshift(ecode, 5) + band(bb, lshift(1, ncode - ROOT_BITS) - 1)) * 4)
ncode = band(ecode, 31)
end
bb = rshift(bb, ncode)
bc -= ncode
local code = rshift(ecode, 5)
if code < NUM_LITERAL then if code < NUM_LITERAL then
local r = readSymbol(d, gR) if bc < MAX_LEN then
local b = readSymbol(d, gB) d.bitbuf, d.bitcnt = bb, bc
local a = readSymbol(d, gA) fill(d)
if d.bitcnt < d.over * 8 then bb, bc = d.bitbuf, d.bitcnt
end
local er = readu32(tab, (gR + band(bb, 255)) * 4)
local nr = band(er, 31)
if nr > ROOT_BITS then
local idx = gR + band(bb, 255)
bb = rshift(bb, ROOT_BITS)
bc -= ROOT_BITS
er = readu32(tab, (idx + rshift(er, 5) + band(bb, lshift(1, nr - ROOT_BITS) - 1)) * 4)
nr = band(er, 31)
end
bb = rshift(bb, nr)
bc -= nr
local r = rshift(er, 5)
if bc < MAX_LEN then
d.bitbuf, d.bitcnt = bb, bc
fill(d)
bb, bc = d.bitbuf, d.bitcnt
end
local eb = readu32(tab, (gB + band(bb, 255)) * 4)
local nb = band(eb, 31)
if nb > ROOT_BITS then
local idx = gB + band(bb, 255)
bb = rshift(bb, ROOT_BITS)
bc -= ROOT_BITS
eb = readu32(tab, (idx + rshift(eb, 5) + band(bb, lshift(1, nb - ROOT_BITS) - 1)) * 4)
nb = band(eb, 31)
end
bb = rshift(bb, nb)
bc -= nb
local b = rshift(eb, 5)
if bc < MAX_LEN then
d.bitbuf, d.bitcnt = bb, bc
fill(d)
bb, bc = d.bitbuf, d.bitcnt
end
local ea = readu32(tab, (gA + band(bb, 255)) * 4)
local na = band(ea, 31)
if na > ROOT_BITS then
local idx = gA + band(bb, 255)
bb = rshift(bb, ROOT_BITS)
bc -= ROOT_BITS
ea = readu32(tab, (idx + rshift(ea, 5) + band(bb, lshift(1, na - ROOT_BITS) - 1)) * 4)
na = band(ea, 31)
end
bb = rshift(bb, na)
bc -= na
local a = rshift(ea, 5)
d.bitbuf, d.bitcnt = bb, bc
if bc < d.over * 8 then
break break
end end
local argb = a * 16777216 + r * 65536 + code * 256 + b local argb = a * 16777216 + r * 65536 + code * 256 + b
@@ -367,6 +435,7 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
row += 1 row += 1
end end
elseif code < lenLimit then elseif code < lenLimit then
d.bitbuf, d.bitcnt = bb, bc
local ls = code - NUM_LITERAL local ls = code - NUM_LITERAL
local length local length
if ls < 4 then if ls < 4 then
@@ -422,6 +491,7 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
setGroup() setGroup()
end end
elseif code < cacheLimit and cache then elseif code < cacheLimit and cache then
d.bitbuf, d.bitcnt = bb, bc
if d.bitcnt < d.over * 8 then if d.bitcnt < d.over * 8 then
break break
end end
@@ -444,8 +514,9 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
return true, nil return true, nil
end end
-- per-byte sums mod 256 (the low seven bits added, the top bits xored in)
local function addPixels(a: number, b: number): number local function addPixels(a: number, b: number): number
return band(band(a, 0xff00ff00) + band(b, 0xff00ff00), 0xff00ff00) + band(band(a, 0x00ff00ff) + band(b, 0x00ff00ff), 0x00ff00ff) return bxor(band(a, 0x7f7f7f7f) + band(b, 0x7f7f7f7f), band(bxor(a, b), 0x80808080))
end end
Decode.addPixels = addPixels Decode.addPixels = addPixels
@@ -553,9 +624,11 @@ local function inversePredictor(data: buffer, w: number, h: number, bits: number
L = addPixels(readu32(data, (row + k) * 4), L) L = addPixels(readu32(data, (row + k) * 4), L)
writeu32(data, (row + k) * 4, L) writeu32(data, (row + k) * 4, L)
end end
elseif mode == 2 then elseif mode == 2 or mode == 3 or mode == 4 then
for k = x, xe - 1 do -- T, TR, TL: the upper row only
writeu32(data, (row + k) * 4, addPixels(readu32(data, (row + k) * 4), readu32(data, (up + k) * 4))) local d = (up - row + (if mode == 2 then 0 elseif mode == 3 then 1 else -1)) * 4
for o = (row + x) * 4, (row + xe - 1) * 4, 4 do
writeu32(data, o, addPixels(readu32(data, o), readu32(data, o + d)))
end end
else else
for k = x, xe - 1 do for k = x, xe - 1 do
@@ -584,12 +657,26 @@ local function inverseCrossColor(data: buffer, w: number, h: number, bits: numbe
local g2b = sint8(band(rshift(m, 8), 255)) local g2b = sint8(band(rshift(m, 8), 255))
local r2b = sint8(band(rshift(m, 16), 255)) local r2b = sint8(band(rshift(m, 16), 255))
local xe = math.min(w, lshift(rshift(x, bits) + 1, bits)) local xe = math.min(w, lshift(rshift(x, bits) + 1, bits))
for k = y * w + x, y * w + xe - 1 do if g2r == 0 and g2b == 0 and r2b == 0 then
local argb = readu32(data, k * 4) x = xe -- the identity
local g = sint8(band(rshift(argb, 8), 255)) continue
local r = band(band(rshift(argb, 16), 255) + ((g2r * g) // 32), 255) end
local b = band(band(argb, 255) + ((g2b * g) // 32) + ((r2b * sint8(r)) // 32), 255) if r2b == 0 then
writeu32(data, k * 4, band(argb, 0xff00ff00) + r * 65536 + b) for o = (y * w + x) * 4, (y * w + xe - 1) * 4, 4 do
local argb = readu32(data, o)
local g = sint8(band(rshift(argb, 8), 255))
local r = band(band(rshift(argb, 16), 255) + ((g2r * g) // 32), 255)
local b = band(band(argb, 255) + ((g2b * g) // 32), 255)
writeu32(data, o, band(argb, 0xff00ff00) + r * 65536 + b)
end
else
for o = (y * w + x) * 4, (y * w + xe - 1) * 4, 4 do
local argb = readu32(data, o)
local g = sint8(band(rshift(argb, 8), 255))
local r = band(band(rshift(argb, 16), 255) + ((g2r * g) // 32), 255)
local b = band(band(argb, 255) + ((g2b * g) // 32) + ((r2b * sint8(r)) // 32), 255)
writeu32(data, o, band(argb, 0xff00ff00) + r * 65536 + b)
end
end end
x = xe x = xe
end end
+52 -45
View File
@@ -8,8 +8,14 @@ local Codec = {}
local index = buffer.create(256) local index = buffer.create(256)
-- the decoder's index as four tables (1-based by hash + 1)
local IR, IG, IB, IA = table.create(64, 0), table.create(64, 0), table.create(64, 0), table.create(64, 0)
function Codec.decode(b: buffer, p: number, limit: number, out: buffer, n: number): (boolean, string?) function Codec.decode(b: buffer, p: number, limit: number, out: buffer, n: number): (boolean, string?)
buffer.fill(index, 0, 0, 256) local ir, ig, ib, ia = IR, IG, IB, IA
for i = 1, 64 do
ir[i], ig[i], ib[i], ia[i] = 0, 0, 0, 0
end
local r, g, bl, a = 0, 0, 0, 255 local r, g, bl, a = 0, 0, 0, 255
local o = 0 local o = 0
local stop = n * 4 local stop = n * 4
@@ -19,62 +25,63 @@ function Codec.decode(b: buffer, p: number, limit: number, out: buffer, n: numbe
end end
local op = readu8(b, p) local op = readu8(b, p)
p += 1 p += 1
if op == 0xFE then if op >= 0xC0 then
if p + 3 > limit then if op == 0xFE then
return false, "truncated" if p + 3 > limit then
end
r = readu8(b, p)
g = readu8(b, p + 1)
bl = readu8(b, p + 2)
p += 3
elseif op == 0xFF then
if p + 4 > limit then
return false, "truncated"
end
r = readu8(b, p)
g = readu8(b, p + 1)
bl = readu8(b, p + 2)
a = readu8(b, p + 3)
p += 4
else
local tag = op // 64
if tag == 0 then
local q = op * 4
r = readu8(index, q)
g = readu8(index, q + 1)
bl = readu8(index, q + 2)
a = readu8(index, q + 3)
elseif tag == 1 then
r = (r + (op // 16) % 4 - 2) % 256
g = (g + (op // 4) % 4 - 2) % 256
bl = (bl + op % 4 - 2) % 256
elseif tag == 2 then
if p >= limit then
return false, "truncated" return false, "truncated"
end end
local d2 = readu8(b, p) r = readu8(b, p)
p += 1 g = readu8(b, p + 1)
local dg = op % 64 - 32 bl = readu8(b, p + 2)
g = (g + dg) % 256 p += 3
r = (r + dg + d2 // 16 - 8) % 256 elseif op == 0xFF then
bl = (bl + dg + d2 % 16 - 8) % 256 if p + 4 > limit then
return false, "truncated"
end
r = readu8(b, p)
g = readu8(b, p + 1)
bl = readu8(b, p + 2)
a = readu8(b, p + 3)
p += 4
else else
local run = op % 64 -- 1..62 more copies of the previous pixel (63 / 64 are RGB / RGBA) -- a run of 1..62 copies of the previous pixel (63 / 64 are RGB / RGBA); every op stores its pixel at its
-- hash, so the index holds it already unless the run is the first op
local v = r + g * 256 + bl * 65536 + a * 16777216 local v = r + g * 256 + bl * 65536 + a * 16777216
local e = math.min(o + run * 4, stop - 4) local first = o == 0
local e = math.min(o + (op - 0xBF) * 4, stop)
while o < e do while o < e do
writeu32(out, o, v) writeu32(out, o, v)
o += 4 o += 4
end end
if not first then
continue
end
local h = (r * 3 + g * 5 + bl * 7 + a * 11) % 64 + 1
ir[h], ig[h], ib[h], ia[h] = r, g, bl, a
continue
end end
elseif op < 0x40 then
local q = op + 1
r, g, bl, a = ir[q], ig[q], ib[q], ia[q]
elseif op < 0x80 then
r = (r + (op // 16) % 4 - 2) % 256
g = (g + (op // 4) % 4 - 2) % 256
bl = (bl + op % 4 - 2) % 256
else
if p >= limit then
return false, "truncated"
end
local d2 = readu8(b, p)
p += 1
local dg = op % 64 - 32
g = (g + dg) % 256
r = (r + dg + d2 // 16 - 8) % 256
bl = (bl + dg + d2 % 16 - 8) % 256
end end
writeu32(out, o, r + g * 256 + bl * 65536 + a * 16777216) writeu32(out, o, r + g * 256 + bl * 65536 + a * 16777216)
o += 4 o += 4
local h = ((r * 3 + g * 5 + bl * 7 + a * 11) % 64) * 4 local h = (r * 3 + g * 5 + bl * 7 + a * 11) % 64 + 1
writeu8(index, h, r) ir[h], ig[h], ib[h], ia[h] = r, g, bl, a
writeu8(index, h + 1, g)
writeu8(index, h + 2, bl)
writeu8(index, h + 3, a)
end end
return true, nil return true, nil
end end
+72 -24
View File
@@ -33,22 +33,44 @@ Codec.pixel = pixel
function Codec.decode(b: buffer, p: number, limit: number, w: number, h: number, bpp: number, kind: number, alpha1: boolean, rle: boolean, bottomUp: boolean, rtl: boolean, pal: buffer, palN: number, out: buffer): (number, boolean) function Codec.decode(b: buffer, p: number, limit: number, w: number, h: number, bpp: number, kind: number, alpha1: boolean, rle: boolean, bottomUp: boolean, rtl: boolean, pal: buffer, palN: number, out: buffer): (number, boolean)
local n = w * h local n = w * h
local i = 0 local i = 0
local function put(v: number) -- the output position of pixel i: row by row (bottom-up flips rows), each row left to right or right to left
local row, col = i // w, i % w local step = if rtl then -4 else 4
if bottomUp then local row, col = 0, 0
row = h - 1 - row local function rowStart(r: number): number
end return ((if bottomUp then h - 1 - r else r) * w + (if rtl then w - 1 else 0)) * 4
if rtl then
col = w - 1 - col
end
writeu32(out, (row * w + col) * 4, v)
i += 1
end end
local o = rowStart(0)
if not rle then if not rle then
local avail = math.min(n, (limit - p) // bpp) local avail = math.min(n, (limit - p) // bpp)
for _ = 1, avail do while i < avail do
put(pixel(b, p, kind, alpha1, pal, palN)) local e = math.min(avail - i, w - col)
p += bpp if kind == 3 then
for _ = 1, e do
writeu32(out, o, readu8(b, p + 2) + readu8(b, p + 1) * 256 + readu8(b, p) * 65536 + 0xFF000000)
p += 3
o += step
end
elseif kind == 4 then
for _ = 1, e do
local v = readu32(b, p)
writeu32(out, o, v - v % 16777216 + (v // 65536) % 256 + v % 65536 - v % 256 + (v % 256) * 65536)
p += 4
o += step
end
else
for _ = 1, e do
writeu32(out, o, pixel(b, p, kind, alpha1, pal, palN))
p += bpp
o += step
end
end
i += e
col += e
if col == w then
col = 0
row += 1
o = rowStart(row)
end
end end
return i, avail < n return i, avail < n
end end
@@ -58,23 +80,49 @@ function Codec.decode(b: buffer, p: number, limit: number, w: number, h: number,
end end
local hd = readu8(b, p) local hd = readu8(b, p)
p += 1 p += 1
local count = hd % 128 + 1 local count = math.min(hd % 128 + 1, n - i)
if hd >= 128 then local packed = hd >= 128
local v = 0
if packed then
if p + bpp > limit then if p + bpp > limit then
return i, true return i, true
end end
local v = pixel(b, p, kind, alpha1, pal, palN) v = pixel(b, p, kind, alpha1, pal, palN)
p += bpp p += bpp
for _ = 1, math.min(count, n - i) do end
put(v) while count > 0 do
local e = math.min(count, w - col)
if packed then
for _ = 1, e do
writeu32(out, o, v)
o += step
end
else
if p + e * bpp > limit then
-- the pixels that are there, then truncated
local k = (limit - p) // bpp
for _ = 1, k do
writeu32(out, o, pixel(b, p, kind, alpha1, pal, palN))
p += bpp
o += step
end
return i + k, true
end
for _ = 1, e do
writeu32(out, o, pixel(b, p, kind, alpha1, pal, palN))
p += bpp
o += step
end
end end
else i += e
for _ = 1, math.min(count, n - i) do col += e
if p + bpp > limit then count -= e
return i, true if col == w then
col = 0
row += 1
if row < h then
o = rowStart(row)
end end
put(pixel(b, p, kind, alpha1, pal, palN))
p += bpp
end end
end end
end end
+31
View File
@@ -180,3 +180,34 @@ if want("qoi", "tga", "bmp", "tiff") then
Tiff.decode(Bytes.reader(tl)) Tiff.decode(Bytes.reader(tl))
end) end)
end end
if want("bcn", "exr") then
local Bcn = require("@rosetta/codecs/image/Bcn")
local BB = require("@tests/gen/bcn_bench")
local w, h = BB.w, BB.h
local px = buffer.fromstring(BB.rgba)
for _, kind in { "bc1", "bc3", "bc5" } do
local blocks = Bcn.encode(kind, px, w, h) :: buffer
bench("bcn decode " .. kind .. " 256x256", 5, w * h * 4, function()
Bcn.decode(kind, blocks, 0, w, h)
end)
bench("bcn encode " .. kind .. " 256x256", 2, w * h * 4, function()
Bcn.encode(kind, px, w, h)
end)
end
local b7, b6 = buffer.fromstring(BB.bc7), buffer.fromstring(BB.bc6h)
bench("bcn decode bc7 256x256", 5, w * h * 4, function()
Bcn.decode("bc7", b7, 0, w, h)
end)
bench("bcn decode bc6h 256x256", 5, w * h * 16, function()
Bcn.decode("bc6h", b6, 0, w, h)
end)
local Exr = require("@rosetta/image/exr")
local EB = require("@tests/gen/exr_bench")
local n = 512 * 256 * 16
for _, k in { "zip", "piz" } do
local data = buffer.fromstring(EB[k])
bench("exr decode " .. k .. " 512x256 half", 3, n, function()
Exr.decode(Bytes.reader(data), { native = true })
end)
end
end