From 9855a7edd6e234c2834d2c4a39f75070fc8cf13f Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sun, 1 Nov 2026 19:24:15 -0600 Subject: [PATCH] perf(image): JPEG fused YCbCr upsampling/colour (8-bit), IDCT DC-only / 4x4 paths from per-block nonzero extent, progressive refine skips zero tails; PNG Paeth table + SWAR sub/avg, rgb8 convert, encoder filter costs by table; GIF palette table + opaque draw copy; WebP fancy upsampler inlined; ImageOps 8-bit RGBA resample, opaque premultiply skip, convert / nearest fast paths; tools/bench_image.luau --- src/Rosetta/codecs/image/Dct/init.luau | 259 ++++++++++++++++------ src/Rosetta/codecs/image/Jpeg/Color.luau | 132 ++++++++++- src/Rosetta/codecs/image/Jpeg/Decode.luau | 9 +- src/Rosetta/codecs/image/Jpeg/Scan.luau | 41 +++- src/Rosetta/codecs/image/Vp8/Yuv.luau | 89 +++++--- src/Rosetta/image/gif/Decode.luau | 21 +- src/Rosetta/image/gif/init.luau | 2 +- src/Rosetta/image/png/Decode.luau | 211 ++++++++++++++---- src/Rosetta/image/png/Encode.luau | 98 +++++--- src/Rosetta/ops/ImageOps/Kernels.luau | 138 +++++++++++- src/Rosetta/ops/ImageOps/init.luau | 8 +- tools/bench_image.luau | 182 +++++++++++++++ 12 files changed, 997 insertions(+), 193 deletions(-) create mode 100644 tools/bench_image.luau diff --git a/src/Rosetta/codecs/image/Dct/init.luau b/src/Rosetta/codecs/image/Dct/init.luau index db19b7f..21fe4d8 100644 --- a/src/Rosetta/codecs/image/Dct/init.luau +++ b/src/Rosetta/codecs/image/Dct/init.luau @@ -4,14 +4,23 @@ -- codecs/image/Dct: the 8x8 DCTs JPEG, Motion-JPEG and MPEG-1 share (native; never throws). -- libjpeg's islow (jidctint.c / jfdctint.c), the reduced IDCTs (jidctred.c) and jcdctmgr.c's rounding quantiser, bit-exact -- with libjpeg-turbo. From jpeg-luau 1.0 (same author); IJG code: licenses/libjpeg-turbo-LICENSE.md. -local readi16, readu16, writeu16, readu8, writei16 = buffer.readi16, buffer.readu16, buffer.writeu16, buffer.readu8, buffer.writei16 +local readi16, readu16, writeu16, writeu32, readu8, writei16 = buffer.readi16, buffer.readu16, buffer.writeu16, buffer.writeu32, buffer.readu8, buffer.writei16 local clamp = math.clamp local Dct = {} local ws = table.create(64, 0) -local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) +local function dequant(q: buffer): { number } + local t = table.create(64, 0) + for i = 0, 63 do + t[i + 1] = readu16(q, i * 2) + end + return t +end + +local function idct8(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) + local ws = ws local s1 = 2 ^ (13 - p1) local h1 = s1 / 2 local s2 = 2 ^ (16 + p1) @@ -19,11 +28,10 @@ local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: n local dcm = 2 ^ p1 for c = 0, 7 do local o = off + c * 2 - local qo = c * 2 local c1, c2, c3 = readi16(coef, o + 16), readi16(coef, o + 32), readi16(coef, o + 48) local c4, c5, c6, c7 = readi16(coef, o + 64), readi16(coef, o + 80), readi16(coef, o + 96), readi16(coef, o + 112) if c1 == 0 and c2 == 0 and c3 == 0 and c4 == 0 and c5 == 0 and c6 == 0 and c7 == 0 then - local dc = readi16(coef, o) * readu16(q, qo) * dcm + local dc = readi16(coef, o) * qt[c + 1] * dcm ws[c + 1] = dc ws[c + 9] = dc ws[c + 17] = dc @@ -33,48 +41,136 @@ local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: n ws[c + 49] = dc ws[c + 57] = dc else - -- even part - local z2 = c2 * readu16(q, qo + 32) - local z3 = c6 * readu16(q, qo + 96) + -- even part (the pass-1 rounding folded into tmp0 / tmp1) + local z2 = c2 * qt[c + 17] + local z3 = c6 * qt[c + 49] local z1 = (z2 + z3) * 4433 local tmp2 = z1 - z3 * 15137 local tmp3 = z1 + z2 * 6270 - z2 = readi16(coef, o) * readu16(q, qo) - z3 = c4 * readu16(q, qo + 64) - local tmp0 = (z2 + z3) * 8192 - local tmp1 = (z2 - z3) * 8192 + z2 = readi16(coef, o) * qt[c + 1] + 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 tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2 -- odd part - local t0 = c7 * readu16(q, qo + 112) - local t1 = c5 * readu16(q, qo + 80) - local t2 = c3 * readu16(q, qo + 48) - local t3 = c1 * readu16(q, qo + 16) + local t0 = c7 * qt[c + 57] + local t1 = c5 * qt[c + 41] + local t2 = c3 * qt[c + 25] + local t3 = c1 * qt[c + 9] z1 = t0 + t3 z2 = t1 + t2 z3 = t0 + t2 local z4 = t1 + t3 local z5 = (z3 + z4) * 9633 - t0 *= 2446 - t1 *= 16819 - t2 *= 25172 - t3 *= 12299 z1 *= -7373 z2 *= -20995 z3 = z3 * -16069 + z5 z4 = z4 * -3196 + z5 - t0 += z1 + z3 - t1 += z2 + z4 - t2 += z2 + z3 - t3 += z1 + z4 - ws[c + 1] = (tmp10 + t3 + h1) // s1 - ws[c + 57] = (tmp10 - t3 + h1) // s1 - ws[c + 9] = (tmp11 + t2 + h1) // s1 - ws[c + 49] = (tmp11 - t2 + h1) // s1 - ws[c + 17] = (tmp12 + t1 + h1) // s1 - ws[c + 41] = (tmp12 - t1 + h1) // s1 - ws[c + 25] = (tmp13 + t0 + h1) // s1 - ws[c + 33] = (tmp13 - t0 + h1) // s1 + t0 = t0 * 2446 + z1 + z3 + t1 = t1 * 16819 + z2 + z4 + t2 = t2 * 25172 + z2 + z3 + t3 = t3 * 12299 + z1 + z4 + ws[c + 1] = (tmp10 + t3) // s1 + ws[c + 57] = (tmp10 - t3) // s1 + ws[c + 9] = (tmp11 + t2) // s1 + ws[c + 49] = (tmp11 - t2) // s1 + ws[c + 17] = (tmp12 + t1) // s1 + ws[c + 41] = (tmp12 - t1) // s1 + ws[c + 25] = (tmp13 + t0) // s1 + ws[c + 33] = (tmp13 - t0) // s1 + end + end + local sd = 2 ^ (p1 + 3) + local hd = sd / 2 + center * sd + -- u16 pairs written as one u32 (little endian: the left sample in the low half) + for r = 0, 7 do + local b = r * 8 + local w0, w1, w2, w3 = ws[b + 1], ws[b + 2], ws[b + 3], ws[b + 4] + local w4, w5, w6, w7 = ws[b + 5], ws[b + 6], ws[b + 7], ws[b + 8] + local o = (out + r * stride) * 2 + if w1 == 0 and w2 == 0 and w3 == 0 and w4 == 0 and w5 == 0 and w6 == 0 and w7 == 0 then + local v = clamp((w0 + hd) // sd, 0, maxv) * 65537 + writeu32(plane, o, v) + writeu32(plane, o + 4, v) + writeu32(plane, o + 8, v) + writeu32(plane, o + 12, v) + else + local z1 = (w2 + w6) * 4433 + local tmp2 = z1 - w6 * 15137 + local tmp3 = z1 + w2 * 6270 + local tmp0 = (w0 + w4) * 8192 + h2 + local tmp1 = (w0 - w4) * 8192 + h2 + local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3 + local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2 + z1 = w7 + w1 + local z2 = w5 + w3 + local z3 = w7 + w3 + local z4 = w5 + w1 + local z5 = (z3 + z4) * 9633 + z1 *= -7373 + z2 *= -20995 + z3 = z3 * -16069 + z5 + z4 = z4 * -3196 + z5 + local t0 = w7 * 2446 + z1 + z3 + local t1 = w5 * 16819 + z2 + z4 + local t2 = w3 * 25172 + z2 + z3 + local t3 = w1 * 12299 + z1 + z4 + writeu32(plane, o, clamp((tmp10 + t3) // s2, 0, maxv) + clamp((tmp11 + t2) // s2, 0, maxv) * 65536) + writeu32(plane, o + 4, clamp((tmp12 + t1) // s2, 0, maxv) + clamp((tmp13 + t0) // s2, 0, maxv) * 65536) + writeu32(plane, o + 8, clamp((tmp13 - t0) // s2, 0, maxv) + clamp((tmp12 - t1) // s2, 0, maxv) * 65536) + writeu32(plane, o + 12, clamp((tmp11 - t2) // s2, 0, maxv) + clamp((tmp10 - t3) // s2, 0, maxv) * 65536) + 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 +local function idct8x4(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) + local ws = ws + local s1 = 2 ^ (13 - p1) + local h1 = s1 / 2 + local s2 = 2 ^ (16 + p1) + local h2 = s2 / 2 + center * s2 + local dcm = 2 ^ p1 + for c = 0, 3 do + local o = off + c * 2 + local c1, c2, c3 = readi16(coef, o + 16), readi16(coef, o + 32), readi16(coef, o + 48) + if c1 == 0 and c2 == 0 and c3 == 0 then + local dc = readi16(coef, o) * qt[c + 1] * dcm + ws[c + 1] = dc + ws[c + 9] = dc + ws[c + 17] = dc + ws[c + 25] = dc + ws[c + 33] = dc + ws[c + 41] = dc + ws[c + 49] = dc + ws[c + 57] = dc + else + local z2 = c2 * qt[c + 17] + local tmp2 = z2 * 4433 + local tmp3 = tmp2 + z2 * 6270 + local tmp0 = readi16(coef, o) * qt[c + 1] * 8192 + h1 + local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3 + local tmp11, tmp12 = tmp0 + tmp2, tmp0 - tmp2 + local t2 = c3 * qt[c + 25] + local t3 = c1 * qt[c + 9] + local z5 = (t2 + t3) * 9633 + local z1 = t3 * -7373 + z2 = t2 * -20995 + local z3 = t2 * -16069 + z5 + local z4 = t3 * -3196 + z5 + local t0 = z1 + z3 + local t1 = z2 + z4 + t2 = t2 * 25172 + z2 + z3 + t3 = t3 * 12299 + z1 + z4 + ws[c + 1] = (tmp10 + t3) // s1 + ws[c + 57] = (tmp10 - t3) // s1 + ws[c + 9] = (tmp11 + t2) // s1 + ws[c + 49] = (tmp11 - t2) // s1 + ws[c + 17] = (tmp12 + t1) // s1 + ws[c + 41] = (tmp12 - t1) // s1 + ws[c + 25] = (tmp13 + t0) // s1 + ws[c + 33] = (tmp13 - t0) // s1 end end local sd = 2 ^ (p1 + 3) @@ -82,51 +178,51 @@ local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: n for r = 0, 7 do local b = r * 8 local w0, w1, w2, w3 = ws[b + 1], ws[b + 2], ws[b + 3], ws[b + 4] - local w4, w5, w6, w7 = ws[b + 5], ws[b + 6], ws[b + 7], ws[b + 8] local o = (out + r * stride) * 2 - if w1 == 0 and w2 == 0 and w3 == 0 and w4 == 0 and w5 == 0 and w6 == 0 and w7 == 0 then - local v = clamp((w0 + hd) // sd, 0, maxv) - for i = 0, 14, 2 do - writeu16(plane, o + i, v) - end + if w1 == 0 and w2 == 0 and w3 == 0 then + local v = clamp((w0 + hd) // sd, 0, maxv) * 65537 + writeu32(plane, o, v) + writeu32(plane, o + 4, v) + writeu32(plane, o + 8, v) + writeu32(plane, o + 12, v) else - local z1 = (w2 + w6) * 4433 - local tmp2 = z1 - w6 * 15137 - local tmp3 = z1 + w2 * 6270 - local tmp0 = (w0 + w4) * 8192 - local tmp1 = (w0 - w4) * 8192 + local tmp2 = w2 * 4433 + local tmp3 = tmp2 + w2 * 6270 + local tmp0 = w0 * 8192 + h2 local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3 - local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2 - local t0, t1, t2, t3 = w7, w5, w3, w1 - z1 = t0 + t3 - local z2 = t1 + t2 - local z3 = t0 + t2 - local z4 = t1 + t3 - local z5 = (z3 + z4) * 9633 - t0 *= 2446 - t1 *= 16819 - t2 *= 25172 - t3 *= 12299 - z1 *= -7373 - z2 *= -20995 - z3 = z3 * -16069 + z5 - z4 = z4 * -3196 + z5 - t0 += z1 + z3 - t1 += z2 + z4 - t2 += z2 + z3 - t3 += z1 + z4 - writeu16(plane, o, clamp((tmp10 + t3 + h2) // s2, 0, maxv)) - writeu16(plane, o + 14, clamp((tmp10 - t3 + h2) // s2, 0, maxv)) - writeu16(plane, o + 2, clamp((tmp11 + t2 + h2) // s2, 0, maxv)) - writeu16(plane, o + 12, clamp((tmp11 - t2 + h2) // s2, 0, maxv)) - writeu16(plane, o + 4, clamp((tmp12 + t1 + h2) // s2, 0, maxv)) - writeu16(plane, o + 10, clamp((tmp12 - t1 + h2) // s2, 0, maxv)) - writeu16(plane, o + 6, clamp((tmp13 + t0 + h2) // s2, 0, maxv)) - writeu16(plane, o + 8, clamp((tmp13 - t0 + h2) // s2, 0, maxv)) + local tmp11, tmp12 = tmp0 + tmp2, tmp0 - tmp2 + local z5 = (w3 + w1) * 9633 + local z1 = w1 * -7373 + local z2 = w3 * -20995 + local z3 = w3 * -16069 + z5 + local z4 = w1 * -3196 + z5 + local t0 = z1 + z3 + local t1 = z2 + z4 + local t2 = w3 * 25172 + z2 + z3 + local t3 = w1 * 12299 + z1 + z4 + writeu32(plane, o, clamp((tmp10 + t3) // s2, 0, maxv) + clamp((tmp11 + t2) // s2, 0, maxv) * 65536) + writeu32(plane, o + 4, clamp((tmp12 + t1) // s2, 0, maxv) + clamp((tmp13 + t0) // s2, 0, maxv) * 65536) + writeu32(plane, o + 8, clamp((tmp13 - t0) // s2, 0, maxv) + clamp((tmp12 - t1) // s2, 0, maxv) * 65536) + writeu32(plane, o + 12, clamp((tmp11 - t2) // s2, 0, maxv) + clamp((tmp10 - t3) // s2, 0, maxv) * 65536) end end end +-- a block with only its DC coefficient: one value (the column and row passes' DC shortcuts) +local function idct8dc(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) + local sd = 2 ^ (p1 + 3) + local v = clamp((readi16(coef, off) * qt[1] * 2 ^ p1 + sd / 2 + center * sd) // sd, 0, maxv) * 65537 + local o = out * 2 + local rs = stride * 2 + for _ = 0, 7 do + writeu32(plane, o, v) + writeu32(plane, o + 4, v) + writeu32(plane, o + 8, v) + writeu32(plane, o + 12, v) + o += rs + end +end + local function idct4(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) local s1 = 2 ^ (14 - p1) local h1 = s1 / 2 @@ -236,17 +332,36 @@ local function idct2(coef: buffer, off: number, q: buffer, plane: buffer, out: n end -- p1 = PASS1_BITS (2 for 8-bit, 1 for 12-bit) -function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number, hook: (() -> ())?) +-- nz (optional): u8 per block, the highest zigzag index that may be nonzero +function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number, hook: (() -> ())?, nz: buffer?) local stride = bpl * size + local qt = dequant(q) for by = 0, bpc - 1 do if hook and by > 0 then pcall(hook) end + if size == 8 and nz then + local bi = by * bpl + local out = by * 8 * stride + for off = bi * 128, (bi + bpl - 1) * 128, 128 do + local k = readu8(nz, bi) + if k == 0 then + idct8dc(coef, off, qt, plane, out, stride, p1, center, maxv) + elseif k <= 9 then + idct8x4(coef, off, qt, plane, out, stride, p1, center, maxv) + else + idct8(coef, off, qt, plane, out, stride, p1, center, maxv) + end + bi += 1 + out += 8 + end + continue + end for bx = 0, bpl - 1 do local off = (by * bpl + bx) * 128 local out = by * size * stride + bx * size if size == 8 then - idct8(coef, off, q, plane, out, stride, p1, center, maxv) + idct8(coef, off, qt, plane, out, stride, p1, center, maxv) elseif size == 4 then idct4(coef, off, q, plane, out, stride, p1, center, maxv) elseif size == 2 then @@ -260,7 +375,7 @@ function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, si end function Dct.idctBlock(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) - idct8(coef, off, q, plane, out, stride, p1, center, maxv) + idct8(coef, off, dequant(q), plane, out, stride, p1, center, maxv) end local fw = table.create(64, 0) diff --git a/src/Rosetta/codecs/image/Jpeg/Color.luau b/src/Rosetta/codecs/image/Jpeg/Color.luau index a950a24..6d8fcb4 100644 --- a/src/Rosetta/codecs/image/Jpeg/Color.luau +++ b/src/Rosetta/codecs/image/Jpeg/Color.luau @@ -142,7 +142,130 @@ local function fetch(c: Comp, y: number, outW: number): (buffer, number) end end +-- 8-bit YCbCr with luma at full size and both chroma planes FULL / H2V1 / H2V2: upsampling fused into the conversion. +-- jdcolor.c's tables folded into one floor each: r = y + (fix(1.402) * cr + KR) >> 16, b alike, and +-- g = y + (-fix(0.34414) * cb - fix(0.71414) * cr + KG) >> 16. +local FIX_R, FIX_B, FIX_GB, FIX_GR = 91881, 116130, 22554, 46802 +local KR = 32768 - FIX_R * 128 +local KB = 32768 - FIX_B * 128 +local KG = 32768 + (FIX_GB + FIX_GR) * 128 +local KRo, KGo, KBo = KR + 256 * 65536, KG + 256 * 65536, KB + 256 * 65536 +local LR, LG, LB = table.create(1024, 0), table.create(1024, 0), table.create(1024, 0) +for i = 1, 1024 do + local v = math.clamp(i - 256, 0, 255) + LR[i] = v + 0xFF000000 + LG[i] = v * 256 + LB[i] = v * 65536 +end + +-- chroma of output column x of an H2V1 / H2V2 row (H2V1: b1 == b0, biases 4 / 8; H2V2: 8 / 7) +local function chromaAt(p: buffer, b0: number, b1: number, w: number, x: number, ba: number, bb: number): number + local j = x // 2 + local this = readu16(p, b0 + j * 2) * 3 + readu16(p, b1 + j * 2) + if x % 2 == 0 then + if j == 0 then + return (this * 4 + 8) // 16 + end + return (this * 3 + readu16(p, b0 + j * 2 - 2) * 3 + readu16(p, b1 + j * 2 - 2) + ba) // 16 + end + if j == w - 1 then + return (this * 4 + 7) // 16 + end + return (this * 3 + readu16(p, b0 + j * 2 + 2) * 3 + readu16(p, b1 + j * 2 + 2) + bb) // 16 +end + +local function ycc8Pixel(out: buffer, o: number, yv: number, cb: number, cr: number) + local r = clamp(yv + (FIX_R * cr + KR) // 65536, 0, 255) + 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) +end + +local function ycc8(P: Params): boolean + local comps = P.comps + if #comps ~= 3 then + return false + end + local cy, ccb, ccr = comps[1], comps[2], comps[3] + local method = ccb.method + if P.space ~= 1 or P.maxv ~= 255 or P.toByte or cy.method ~= 0 or ccr.method ~= method or ccr.stride ~= ccb.stride or ccr.downW ~= ccb.downW or ccr.downH ~= ccb.downH or ccr.rows ~= ccb.rows then + return false + end + if method ~= 0 and method ~= 2 and method ~= 3 then + return false + end + local outW, outH, out = P.outW, P.outH, P.pixels + local py, pcb, pcr = cy.plane, ccb.plane, ccr.plane + local ys, cs = cy.stride * 2, ccb.stride * 2 + local yRows, cRows, w, downH = cy.rows, ccb.rows, ccb.downW, ccb.downH + local hook = P.hook + local ba, bb = if method == 3 then 8 else 4, if method == 3 then 7 else 8 + local o = 0 + for y = 0, outH - 1 do + if hook and y > 0 and y % 8 == 0 then + pcall(hook) + end + local yb = min(y, yRows - 1) * ys + if method == 0 then + local cb0 = min(y, cRows - 1) * cs + local d = cb0 - yb + 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 r = clamp(yv + (FIX_R * cr + KR) // 65536, 0, 255) + 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 + end + continue + end + local b0, b1 + if method == 3 then + local iy = min(y // 2, downH - 1) + local oy = if y % 2 == 0 then max(iy - 1, 0) else min(iy + 1, downH - 1) + b0, b1 = iy * cs, oy * cs + else + b0 = min(y, cRows - 1) * cs + b1 = b0 + end + -- pixels 0 and 1, then pairs (2i, 2i + 1) for i = 1 .. iEnd, then the rest + local iEnd = min(w - 2, (outW - 2) // 2) + for x = 0, min(outW, 2) - 1 do + ycc8Pixel(out, o + x * 4, readu16(py, yb + x * 2), chromaAt(pcb, b0, b1, w, x, ba, bb), chromaAt(pcr, b0, b1, w, x, ba, bb)) + end + local cbL = readu16(pcb, b0) * 3 + readu16(pcb, b1) + local crL = readu16(pcr, b0) * 3 + readu16(pcr, b1) + local cbT = readu16(pcb, b0 + 2) * 3 + readu16(pcb, b1 + 2) + local crT = readu16(pcr, b0 + 2) * 3 + readu16(pcr, b1 + 2) + local po = o + 8 + local yo = yb + 4 + local d = b1 - b0 + for p = b0 + 4, b0 + iEnd * 2 + 2, 2 do + local cbN = readu16(pcb, p) * 3 + readu16(pcb, p + d) + local crN = readu16(pcr, p) * 3 + readu16(pcr, p + d) + local cb3, cr3 = cbT * 3, crT * 3 + local cb, cr = (cb3 + cbL + ba) // 16, (cr3 + crL + ba) // 16 + local yv = readu16(py, yo) + writeu32(out, po, LR[yv + (FIX_R * cr + KRo) // 65536] + LG[yv + (KGo - FIX_GB * cb - FIX_GR * cr) // 65536] + LB[yv + (FIX_B * cb + KBo) // 65536]) + cb, cr = (cb3 + cbN + bb) // 16, (cr3 + crN + bb) // 16 + yv = readu16(py, yo + 2) + writeu32(out, po + 4, LR[yv + (FIX_R * cr + KRo) // 65536] + LG[yv + (KGo - FIX_GB * cb - FIX_GR * cr) // 65536] + LB[yv + (FIX_B * cb + KBo) // 65536]) + cbL, cbT, crL, crT = cbT, cbN, crT, crN + po += 8 + yo += 4 + end + for x = max(iEnd * 2 + 2, 2), outW - 1 do + ycc8Pixel(out, o + x * 4, readu16(py, yb + x * 2), chromaAt(pcb, b0, b1, w, x, ba, bb), chromaAt(pcr, b0, b1, w, x, ba, bb)) + end + o += outW * 4 + end + return true +end + function Color.run(P: Params) + if ycc8(P) then + return + end local comps = P.comps local nc = #comps local space, outW, outH, maxv, out = P.space, P.outW, P.outH, P.maxv, P.pixels @@ -177,7 +300,10 @@ function Color.run(P: Params) local c0, c1, c2 = readu16(s0, b0 + i), readu16(s1, b1 + i), readu16(s2, b2 + i) local c3 = if nc >= 4 then readu16(s3, b3 + i) else maxv if toByte then - c0, c1, c2, c3 = readu8(toByte, c0), readu8(toByte, c1), readu8(toByte, c2), readu8(toByte, c3) + c0 = readu8(toByte, c0) + c1 = readu8(toByte, c1) + c2 = readu8(toByte, c2) + c3 = readu8(toByte, c3) end writeu32(out, o, c0 + c1 * 256 + c2 * 65536 + c3 * 16777216) o += 4 @@ -223,7 +349,9 @@ function Color.run(P: Params) end end if toByte then - r, g, b = readu8(toByte, r), readu8(toByte, g), readu8(toByte, b) + r = readu8(toByte, r) + g = readu8(toByte, g) + b = readu8(toByte, b) end writeu32(out, o, 0xFF000000 + r + g * 256 + b * 65536) o += 4 diff --git a/src/Rosetta/codecs/image/Jpeg/Decode.luau b/src/Rosetta/codecs/image/Jpeg/Decode.luau index 655dcca..8a08fce 100644 --- a/src/Rosetta/codecs/image/Jpeg/Decode.luau +++ b/src/Rosetta/codecs/image/Jpeg/Decode.luau @@ -88,6 +88,7 @@ type Comp = { bw: number, bh: number, coef: buffer?, + nz: buffer?, -- u8 per block: the highest zigzag index holding a nonzero coefficient (Huffman scans) quant: buffer?, dct: buffer?, act: buffer?, @@ -499,6 +500,7 @@ function Decode.parse(b: buffer, base: number, n: number, opts: any, headerOnly: end if not c.coef then c.coef = buffer.create(c.bpl * c.bpc * 128) + c.nz = if f.arithmetic then nil else buffer.create(c.bpl * c.bpc) end if not c.quant then c.quant = quant[c.tq] @@ -605,7 +607,7 @@ function Decode.output(st: State): (number?, any, any, string?) end q = zeroQ -- the component never appeared in a scan: mid grey end - Dct.idctComponent(coef, q :: buffer, c.bpl, c.bpc, ssize, plane, p1, center, maxv, st.hook) + Dct.idctComponent(coef, q :: buffer, c.bpl, c.bpc, ssize, plane, p1, center, maxv, st.hook, if c.coef then c.nz else nil) local hIn, hOut = c.h * ssize // minSize, f.hmax local vIn, vOut = c.v * ssize // minSize, f.vmax local downW = math.ceil(f.width * c.h * ssize / (f.hmax * 8)) @@ -636,6 +638,8 @@ function Decode.output(st: State): (number?, any, any, string?) row = buffer.create((math.max(outW, 2 * stride) + 8) * 2), }) c.coef = nil + c.nz = nil + c.nz = nil end local space = colorSpaceOf(f, st.adobe, st.jfif) local spaceId = SPACE_IDS[space] @@ -737,7 +741,7 @@ function Decode.outputYuv(st: State): (number?, number?, buffer?, string?) zeroQ = zeroQ or buffer.create(128) -- the component never appeared in a scan: mid grey q = zeroQ end - Dct.idctComponent(coef, q :: buffer, c.bpl, c.bpc, 8, plane, 2, 128, 255, st.hook) + Dct.idctComponent(coef, q :: buffer, c.bpl, c.bpc, 8, plane, 2, 128, 255, st.hook, if c.coef then c.nz else nil) if i == 1 then Yuv.crop(plane, stride, w, h, out, 0) elseif mode == "420" then @@ -748,6 +752,7 @@ function Decode.outputYuv(st: State): (number?, number?, buffer?, string?) Yuv.box(plane, stride, w, h, out, w * h + (i - 2) * cw * ch) end c.coef = nil + c.nz = nil end if mode == "gray" then buffer.fill(out, w * h, 128, 2 * cw * ch) diff --git a/src/Rosetta/codecs/image/Jpeg/Scan.luau b/src/Rosetta/codecs/image/Jpeg/Scan.luau index fb18936..dbedfdb 100644 --- a/src/Rosetta/codecs/image/Jpeg/Scan.luau +++ b/src/Rosetta/codecs/image/Jpeg/Scan.luau @@ -168,7 +168,7 @@ local function extend(v: number, s: number): number return v end -local function blockSeq(d: buffer, pos: number, coef: buffer, off: number, dct: buffer, act: buffer, pred: number): (number, number) +local function blockSeq(d: buffer, pos: number, coef: buffer, off: number, dct: buffer, act: buffer, pred: number, nz: buffer, bi: number): (number, number) local s s, pos = huff(d, pos, dct) if s ~= 0 then @@ -180,6 +180,7 @@ local function blockSeq(d: buffer, pos: number, coef: buffer, off: number, dct: end writei16(coef, off, pred) local k = 1 + local last = 0 while k < 64 do local w = lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7)) local fa = readi32(act, T_FASTAC + rshift(w, 23) * 4) @@ -191,6 +192,7 @@ local function blockSeq(d: buffer, pos: number, coef: buffer, off: number, dct: break end writei16(coef, off + readu8(ZZ, k) * 2, fa // 256) + last = k k += 1 else local rs @@ -208,10 +210,12 @@ local function blockSeq(d: buffer, pos: number, coef: buffer, off: number, dct: end writei16(coef, off + readu8(ZZ, k) * 2, extend(bits(d, pos, sz), sz)) pos += sz + last = k k += 1 end end end + writeu8(nz, bi, last) return pos, pred end @@ -229,12 +233,13 @@ local function blockDcFirst(d: buffer, pos: number, coef: buffer, off: number, d return pos, pred end -local function blockAcFirst(d: buffer, pos: number, coef: buffer, off: number, act: buffer, ss: number, se: number, al: number, eobrun: number): (number, number) +local function blockAcFirst(d: buffer, pos: number, coef: buffer, off: number, act: buffer, ss: number, se: number, al: number, eobrun: number, nz: buffer, bi: number): (number, number) if eobrun > 0 then return pos, eobrun - 1 end local mul = lshift(1, al) local k = ss + local last = readu8(nz, bi) while k <= se do local w = lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7)) local fa = readi32(act, T_FASTAC + rshift(w, 23) * 4) @@ -246,6 +251,9 @@ local function blockAcFirst(d: buffer, pos: number, coef: buffer, off: number, a break end writei16(coef, off + readu8(ZZ, k) * 2, (fa // 256) * mul) + if k > last then + last = k + end k += 1 else local rs @@ -268,18 +276,24 @@ local function blockAcFirst(d: buffer, pos: number, coef: buffer, off: number, a end writei16(coef, off + readu8(ZZ, k) * 2, extend(bits(d, pos, s), s) * mul) pos += s + if k > last then + last = k + end k += 1 end end end + writeu8(nz, bi, last) return pos, eobrun end -- successive-approximation refinement of AC coefficients ss..se (as stb_image / libjpeg decode_mcu_AC_refine) -local function blockAcRefine(d: buffer, pos: number, coef: buffer, off: number, act: buffer, ss: number, se: number, al: number, eobrun: number): (number, number) +-- coefficients past nz[bi] are zero: in an EOB run (or after the block's EOB) they need no correction bits +local function blockAcRefine(d: buffer, pos: number, coef: buffer, off: number, act: buffer, ss: number, se: number, al: number, eobrun: number, nz: buffer, bi: number): (number, number) local bit = lshift(1, al) + local last = readu8(nz, bi) if eobrun > 0 then - for k = ss, se do + for k = ss, if last < se then last else se do local o = off + readu8(ZZ, k) * 2 local p = readi16(coef, o) if p ~= 0 then @@ -306,6 +320,9 @@ local function blockAcRefine(d: buffer, pos: number, coef: buffer, off: number, pos += r end r = 64 -- the rest of the block only refines + if last < se then + se = last + end end else -- s must be 1; anything else is corrupt and treated as 1 @@ -327,6 +344,9 @@ local function blockAcRefine(d: buffer, pos: number, coef: buffer, off: number, if r == 0 then if val ~= 0 then writei16(coef, o, val) + if k - 1 > last then + last = k - 1 + end end break end @@ -334,12 +354,14 @@ local function blockAcRefine(d: buffer, pos: number, coef: buffer, off: number, end end end + writeu8(nz, bi, last) -- an EOB run that started in this block counts it already (eobrun = run length - 1) return pos, eobrun end export type ScanComp = { coef: buffer, + nz: buffer, bpl: number, h: number, v: number, @@ -414,12 +436,13 @@ function Scan.run(d: buffer, cleanLen: number, rst: { number }, nrst: number, P: else bh, bw, by0, bx0 = c.v, c.h, my * c.v, mx * c.h end - local coef, bpl = c.coef, c.bpl + local coef, bpl, nz = c.coef, c.bpl, c.nz for by = by0, by0 + bh - 1 do for bx = bx0, bx0 + bw - 1 do - local off = (by * bpl + bx) * 128 + local bi = by * bpl + bx + local off = bi * 128 if mode == 0 then - pos, c.pred = blockSeq(d, pos, coef, off, c.dct :: buffer, c.act :: buffer, c.pred) + pos, c.pred = blockSeq(d, pos, coef, off, c.dct :: buffer, c.act :: buffer, c.pred, nz, bi) elseif mode == 1 then pos, c.pred = blockDcFirst(d, pos, coef, off, c.dct :: buffer, c.pred, al) elseif mode == 2 then @@ -429,9 +452,9 @@ function Scan.run(d: buffer, cleanLen: number, rst: { number }, nrst: number, P: writei16(coef, off, bor(readi16(coef, off), bit)) end elseif mode == 3 then - pos, eobrun = blockAcFirst(d, pos, coef, off, c.act :: buffer, ss, se, al, eobrun) + pos, eobrun = blockAcFirst(d, pos, coef, off, c.act :: buffer, ss, se, al, eobrun, nz, bi) else - pos, eobrun = blockAcRefine(d, pos, coef, off, c.act :: buffer, ss, se, al, eobrun) + pos, eobrun = blockAcRefine(d, pos, coef, off, c.act :: buffer, ss, se, al, eobrun, nz, bi) end end end diff --git a/src/Rosetta/codecs/image/Vp8/Yuv.luau b/src/Rosetta/codecs/image/Vp8/Yuv.luau index 089a87c..e12fc57 100644 --- a/src/Rosetta/codecs/image/Vp8/Yuv.luau +++ b/src/Rosetta/codecs/image/Vp8/Yuv.luau @@ -6,28 +6,15 @@ -- (BSD-3-Clause, licenses/libwebp-LICENSE.txt). local rshift = bit32.rshift local readu8, writeu8, writeu32 = buffer.readu8, buffer.writeu8, buffer.writeu32 +local clamp = math.clamp local bcreate = buffer.create local Yuv = {} -local function clip(v: number): number - -- VP8Clip8: v in 14-bit fixed point - if v >= 0 and v < 16384 then - return rshift(v, 6) - end - return if v < 0 then 0 else 255 -end - -local function mulHi(v: number, c: number): number - return rshift(v * c, 8) -end - +-- VP8Clip8(v) = clamp(v >> 6, 0, 255) (v in 14-bit fixed point), MultHi(v, c) = v * c >> 8 (v, c >= 0) local function rgba(y: number, u: number, v: number): number - local yy = mulHi(y, 19077) - local r = clip(yy + mulHi(v, 26149) - 14234) - local g = clip(yy - mulHi(u, 6419) - mulHi(v, 13320) + 8708) - local b = clip(yy + mulHi(u, 33050) - 17685) - return r + g * 256 + b * 65536 + 0xff000000 + local yy = y * 19077 // 256 + return clamp((yy + v * 26149 // 256 - 14234) // 64, 0, 255) + clamp((yy - u * 6419 // 256 - v * 13320 // 256 + 8708) // 64, 0, 255) * 256 + clamp((yy + u * 33050 // 256 - 17685) // 64, 0, 255) * 65536 + 0xFF000000 end Yuv.rgba = rgba @@ -46,23 +33,59 @@ local function upsamplePair(Y: buffer, U: buffer, V: buffer, ty: number, by: num local v0 = rshift(3 * lv + tlv + 2, 2) writeu32(out, db, rgba(readu8(Y, by), u0, v0)) end - for x = 1, last do - local tu1, tv1 = readu8(U, tu + x), readu8(V, tu + x) - local u1, v1 = readu8(U, cu + x), readu8(V, cu + x) - local avgU = tlu + tu1 + lu + u1 + 8 - local avgV = tlv + tv1 + lv + v1 + 8 - local d12u = rshift(avgU + 2 * (tu1 + lu), 3) - local d03u = rshift(avgU + 2 * (tlu + u1), 3) - local d12v = rshift(avgV + 2 * (tv1 + lv), 3) - local d03v = rshift(avgV + 2 * (tlv + v1), 3) - local px = 2 * x - writeu32(out, dt + (px - 1) * 4, rgba(readu8(Y, ty + px - 1), rshift(d12u + tlu, 1), rshift(d12v + tlv, 1))) - writeu32(out, dt + px * 4, rgba(readu8(Y, ty + px), rshift(d03u + tu1, 1), rshift(d03v + tv1, 1))) - if by >= 0 then - writeu32(out, db + (px - 1) * 4, rgba(readu8(Y, by + px - 1), rshift(d03u + lu, 1), rshift(d03v + lv, 1))) - writeu32(out, db + px * 4, rgba(readu8(Y, by + px), rshift(d12u + u1, 1), rshift(d12v + v1, 1))) + -- rgba() inlined + if by >= 0 then + for x = 1, last do + local tu1, tv1 = readu8(U, tu + x), readu8(V, tu + x) + local u1, v1 = readu8(U, cu + x), readu8(V, cu + x) + local avgU = tlu + tu1 + lu + u1 + 8 + local avgV = tlv + tv1 + lv + v1 + 8 + local d12u = (avgU + 2 * (tu1 + lu)) // 8 + local d03u = (avgU + 2 * (tlu + u1)) // 8 + local d12v = (avgV + 2 * (tv1 + lv)) // 8 + local d03v = (avgV + 2 * (tlv + v1)) // 8 + local pt, pb = ty + 2 * x - 1, by + 2 * x - 1 + local ot, ob = dt + (2 * x - 1) * 4, db + (2 * x - 1) * 4 + do + local yy, u, v = readu8(Y, pt) * 19077 // 256, (d12u + tlu) // 2, (d12v + tlv) // 2 + writeu32(out, ot, clamp((yy + v * 26149 // 256 - 14234) // 64, 0, 255) + clamp((yy - u * 6419 // 256 - v * 13320 // 256 + 8708) // 64, 0, 255) * 256 + clamp((yy + u * 33050 // 256 - 17685) // 64, 0, 255) * 65536 + 0xFF000000) + end + do + local yy, u, v = readu8(Y, pt + 1) * 19077 // 256, (d03u + tu1) // 2, (d03v + tv1) // 2 + writeu32(out, ot + 4, clamp((yy + v * 26149 // 256 - 14234) // 64, 0, 255) + clamp((yy - u * 6419 // 256 - v * 13320 // 256 + 8708) // 64, 0, 255) * 256 + clamp((yy + u * 33050 // 256 - 17685) // 64, 0, 255) * 65536 + 0xFF000000) + end + do + local yy, u, v = readu8(Y, pb) * 19077 // 256, (d03u + lu) // 2, (d03v + lv) // 2 + writeu32(out, ob, clamp((yy + v * 26149 // 256 - 14234) // 64, 0, 255) + clamp((yy - u * 6419 // 256 - v * 13320 // 256 + 8708) // 64, 0, 255) * 256 + clamp((yy + u * 33050 // 256 - 17685) // 64, 0, 255) * 65536 + 0xFF000000) + end + do + local yy, u, v = readu8(Y, pb + 1) * 19077 // 256, (d12u + u1) // 2, (d12v + v1) // 2 + writeu32(out, ob + 4, clamp((yy + v * 26149 // 256 - 14234) // 64, 0, 255) + clamp((yy - u * 6419 // 256 - v * 13320 // 256 + 8708) // 64, 0, 255) * 256 + clamp((yy + u * 33050 // 256 - 17685) // 64, 0, 255) * 65536 + 0xFF000000) + end + tlu, tlv, lu, lv = tu1, tv1, u1, v1 + end + else + for x = 1, last do + local tu1, tv1 = readu8(U, tu + x), readu8(V, tu + x) + local u1, v1 = readu8(U, cu + x), readu8(V, cu + x) + local avgU = tlu + tu1 + lu + u1 + 8 + local avgV = tlv + tv1 + lv + v1 + 8 + local d12u = (avgU + 2 * (tu1 + lu)) // 8 + local d03u = (avgU + 2 * (tlu + u1)) // 8 + local d12v = (avgV + 2 * (tv1 + lv)) // 8 + local d03v = (avgV + 2 * (tlv + v1)) // 8 + local pt = ty + 2 * x - 1 + local ot = dt + (2 * x - 1) * 4 + do + local yy, u, v = readu8(Y, pt) * 19077 // 256, (d12u + tlu) // 2, (d12v + tlv) // 2 + writeu32(out, ot, clamp((yy + v * 26149 // 256 - 14234) // 64, 0, 255) + clamp((yy - u * 6419 // 256 - v * 13320 // 256 + 8708) // 64, 0, 255) * 256 + clamp((yy + u * 33050 // 256 - 17685) // 64, 0, 255) * 65536 + 0xFF000000) + end + do + local yy, u, v = readu8(Y, pt + 1) * 19077 // 256, (d03u + tu1) // 2, (d03v + tv1) // 2 + writeu32(out, ot + 4, clamp((yy + v * 26149 // 256 - 14234) // 64, 0, 255) + clamp((yy - u * 6419 // 256 - v * 13320 // 256 + 8708) // 64, 0, 255) * 256 + clamp((yy + u * 33050 // 256 - 17685) // 64, 0, 255) * 65536 + 0xFF000000) + end + tlu, tlv, lu, lv = tu1, tv1, u1, v1 end - tlu, tlv, lu, lv = tu1, tv1, u1, v1 end if len % 2 == 0 then local u0 = rshift(3 * tlu + lu + 2, 2) diff --git a/src/Rosetta/image/gif/Decode.luau b/src/Rosetta/image/gif/Decode.luau index f258699..46efa54 100644 --- a/src/Rosetta/image/gif/Decode.luau +++ b/src/Rosetta/image/gif/Decode.luau @@ -23,21 +23,34 @@ end function Decode.toRgba(indexes: buffer, fw: number, fh: number, interlaced: boolean, palette: buffer, out: buffer) local rows = if interlaced then interlacedRow(fh) else nil + local pal = table.create(256, 0) + for i = 0, 255 do + pal[i + 1] = readu32(palette, i * 4) + end + local o = 0 for y = 0, fh - 1 do local src = (if rows then rows[y + 1] else y) * fw - local dst = y * fw * 4 - for x = 0, fw - 1 do - writeu32(out, dst + x * 4, readu32(palette, readu8(indexes, src + x) * 4)) + for i = src, src + fw - 1 do + writeu32(out, o, pal[readu8(indexes, i) + 1]) + o += 4 end end end -function Decode.draw(canvas: buffer, cw: number, ch: number, frame: buffer, fx: number, fy: number, fw: number, fh: number) +-- opaque: no palette entry has alpha 0 (no transparent index), so rows copy as they are +function Decode.draw(canvas: buffer, cw: number, ch: number, frame: buffer, fx: number, fy: number, fw: number, fh: number, opaque: boolean?) local x0, y0 = math.max(0, fx), math.max(0, fy) local x1, y1 = math.min(cw, fx + fw), math.min(ch, fy + fh) + if x1 <= x0 then + return + end for y = y0, y1 - 1 do local s = ((y - fy) * fw - fx) * 4 local d = y * cw * 4 + if opaque then + buffer.copy(canvas, d + x0 * 4, frame, s + x0 * 4, (x1 - x0) * 4) + continue + end for x = x0, x1 - 1 do local v = readu32(frame, s + x * 4) if v >= 0x01000000 then diff --git a/src/Rosetta/image/gif/init.luau b/src/Rosetta/image/gif/init.luau index 46cf578..5d3018e 100644 --- a/src/Rosetta/image/gif/init.luau +++ b/src/Rosetta/image/gif/init.luau @@ -253,7 +253,7 @@ function Gif.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E local fimg = Types.Image.new(fw, fh, "rgba8") Decode.toRgba(idx, fw, fh, interlaced, t, fimg.pixels) if #frames == 0 then - Decode.draw(img.pixels, cw, ch, fimg.pixels, fx, fy, fw, fh) + Decode.draw(img.pixels, cw, ch, fimg.pixels, fx, fy, fw, fh, trans < 0) end table.insert(frames, { image = fimg, diff --git a/src/Rosetta/image/png/Decode.luau b/src/Rosetta/image/png/Decode.luau index a5d816d..59bc272 100644 --- a/src/Rosetta/image/png/Decode.luau +++ b/src/Rosetta/image/png/Decode.luau @@ -2,8 +2,9 @@ --!native --!optimize 2 -- image/png/Decode: PNG unfiltering and expansion to RGBA8 (absorbed from png-luau src/Png/Decode.luau, same author). -local band, rshift, lshift = bit32.band, bit32.rshift, bit32.lshift -local readu8, writeu8, readu32, writeu32 = buffer.readu8, buffer.writeu8, buffer.readu32, buffer.writeu32 +local band, bxor, rshift, lshift = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift +local abs = math.abs +local readu8, writeu8, readu32, writeu32, readi16, writei16 = buffer.readu8, buffer.writeu8, buffer.readu32, buffer.writeu32, buffer.readi16, buffer.writei16 local bcopy = buffer.copy local Decode = {} @@ -27,27 +28,148 @@ function Decode.rowBytes(pw: number, channels: number, depth: number): number return (pw * channels * depth + 7) // 8 end +-- per-byte sums mod 256 of four packed bytes; floor((a + b) / 2) per byte +local function add4(x: number, y: number): number + return bxor(band(x, 0x7F7F7F7F) + band(y, 0x7F7F7F7F), band(bxor(x, y), 0x80808080)) +end + +-- Paeth as a table: v - c by (a - c, b - c), i16 at ((a - c + 255) * 511 + b - c + 255) * 2; built on the first large image +local PT: buffer? = nil +local function paethTable(): buffer + local t = PT + if t then + return t + end + local nt = buffer.create(511 * 511 * 2) + for da = -255, 255 do + for db = -255, 255 do + local pa, pb, pc = abs(db), abs(da), abs(da + db) + writei16(nt, ((da + 255) * 511 + db + 255) * 2, if pa <= pb and pa <= pc then da elseif pb <= pc then db else 0) + end + end + PT = nt + return nt +end + +-- the table for n bytes of image data: nil (compute instead) for small images while it is not built yet +local function paethFor(n: number): buffer? + return if PT or n >= 262144 then paethTable() else nil +end +Decode.paethFor = paethFor + +local function paeth(a: number, b: number, c: number): number + local pa, pb, pc = abs(b - c), abs(a - c), abs(a + b - c - c) + return if pa <= pb and pa <= pc then a elseif pb <= pc then b else c +end + +-- Paeth on four-byte pixels: the left (a) and upper-left (c) bytes carried, the row read and written as u32 +local function paeth4(raw: buffer, cur: number, e: number, stride: number, pt: buffer) + local a0, a1, a2, a3 = readu8(raw, cur), readu8(raw, cur + 1), readu8(raw, cur + 2), readu8(raw, cur + 3) + local up = readu32(raw, cur - stride) + local c0, c1, c2, c3 = up % 256, up // 256 % 256, up // 65536 % 256, up // 16777216 + for i = cur + 4, e - 4, 4 do + local x, bw = readu32(raw, i), readu32(raw, i - stride) + local b = bw % 256 + a0 = (x + c0 + readi16(pt, (a0 * 511 + b - c0 * 512) * 2 + 261120)) % 256 + c0 = b + b = bw // 256 % 256 + a1 = (x // 256 + c1 + readi16(pt, (a1 * 511 + b - c1 * 512) * 2 + 261120)) % 256 + c1 = b + b = bw // 65536 % 256 + a2 = (x // 65536 + c2 + readi16(pt, (a2 * 511 + b - c2 * 512) * 2 + 261120)) % 256 + c2 = b + b = bw // 16777216 + a3 = (x // 16777216 + c3 + readi16(pt, (a3 * 511 + b - c3 * 512) * 2 + 261120)) % 256 + c3 = b + writeu32(raw, i, a0 + a1 * 256 + a2 * 65536 + a3 * 16777216) + end +end + +local function paeth3(raw: buffer, cur: number, e: number, stride: number, pt: buffer) + local a0, a1, a2 = readu8(raw, cur), readu8(raw, cur + 1), readu8(raw, cur + 2) + local u = cur - stride + local c0, c1, c2 = readu8(raw, u), readu8(raw, u + 1), readu8(raw, u + 2) + for i = cur + 3, e - 3, 3 do + local j = i - stride + local b0, b1, b2 = readu8(raw, j), readu8(raw, j + 1), readu8(raw, j + 2) + 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 + writeu8(raw, i, a0) + writeu8(raw, i + 1, a1) + writeu8(raw, i + 2, a2) + c0, c1, c2 = b0, b1, b2 + 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 + local a, b, c = readu8(raw, i - bpp), readu8(raw, i - stride), readu8(raw, i - stride - bpp) + writeu8(raw, i, readu8(raw, i) + c + readi16(pt, (a * 511 + b - c * 512) * 2 + 261120)) + end +end + +local function paeth1(raw: buffer, cur: number, e: number, stride: number, pt: buffer) + local a, c = readu8(raw, cur), readu8(raw, cur - stride) + for i = cur + 1, e - 1 do + local b = readu8(raw, i - stride) + a = (readu8(raw, i) + c + readi16(pt, (a * 511 + b - c * 512) * 2 + 261120)) % 256 + writeu8(raw, i, a) + c = b + end +end + local function unfilter(raw: buffer, p: number, ph: number, rb: number, bpp: number): (boolean, number) local stride = rb + 1 + local pt = paethFor(ph * rb) for r = 0, ph - 1 do local f = readu8(raw, p) local cur = p + 1 + local e = cur + rb if f == 0 then - elseif f == 1 then - for i = cur + bpp, cur + rb - 1 do - writeu8(raw, i, readu8(raw, i) + readu8(raw, i - bpp)) + elseif f == 1 or (f == 4 and r == 0) then + -- Paeth without a previous row is sub + if bpp >= 4 then + local i = cur + bpp + while i <= e - 4 do + writeu32(raw, i, add4(readu32(raw, i), readu32(raw, i - bpp))) + i += 4 + end + while i < e do + writeu8(raw, i, readu8(raw, i) + readu8(raw, i - bpp)) + i += 1 + end + elseif bpp == 1 then + local a = readu8(raw, cur) + for i = cur + 1, e - 1 do + a = (readu8(raw, i) + a) % 256 + writeu8(raw, i, a) + end + elseif bpp == 3 then + local a0, a1, a2 = readu8(raw, cur), readu8(raw, cur + 1), readu8(raw, cur + 2) + for i = cur + 3, e - 3, 3 do + a0 = (readu8(raw, i) + a0) % 256 + a1 = (readu8(raw, i + 1) + a1) % 256 + a2 = (readu8(raw, i + 2) + a2) % 256 + writeu8(raw, i, a0) + writeu8(raw, i + 1, a1) + writeu8(raw, i + 2, a2) + end + else + for i = cur + bpp, e - 1 do + writeu8(raw, i, readu8(raw, i) + readu8(raw, i - bpp)) + end end elseif f == 2 then if r > 0 then local i = cur - local e4 = cur + rb - 4 + local e4 = e - 4 while i <= e4 do - local x, y = readu32(raw, i), readu32(raw, i - stride) - local lo = band(x, 0x7F7F7F7F) + band(y, 0x7F7F7F7F) - writeu32(raw, i, bit32.bxor(lo, band(bit32.bxor(x, y), 0x80808080))) + writeu32(raw, i, add4(readu32(raw, i), readu32(raw, i - stride))) i += 4 end - while i < cur + rb do + while i < e do writeu8(raw, i, readu8(raw, i) + readu8(raw, i - stride)) i += 1 end @@ -57,42 +179,44 @@ local function unfilter(raw: buffer, p: number, ph: number, rb: number, bpp: num for i = cur, cur + bpp - 1 do writeu8(raw, i, readu8(raw, i) + rshift(readu8(raw, i - stride), 1)) end - for i = cur + bpp, cur + rb - 1 do - writeu8(raw, i, readu8(raw, i) + rshift(readu8(raw, i - bpp) + readu8(raw, i - stride), 1)) + if bpp >= 4 then + local i = cur + bpp + while i <= e - 4 do + local a, b = readu32(raw, i - bpp), readu32(raw, i - stride) + writeu32(raw, i, add4(readu32(raw, i), band(a, b) + rshift(band(bxor(a, b), 0xFEFEFEFE), 1))) + i += 4 + end + while i < e do + writeu8(raw, i, readu8(raw, i) + rshift(readu8(raw, i - bpp) + readu8(raw, i - stride), 1)) + i += 1 + end + else + for i = cur + bpp, e - 1 do + writeu8(raw, i, readu8(raw, i) + rshift(readu8(raw, i - bpp) + readu8(raw, i - stride), 1)) + end end else - for i = cur + bpp, cur + rb - 1 do + for i = cur + bpp, e - 1 do writeu8(raw, i, readu8(raw, i) + rshift(readu8(raw, i - bpp), 1)) end end elseif f == 4 then - if r > 0 then - -- the first pixel: a = c = 0, so Paeth picks b - for i = cur, cur + bpp - 1 do - writeu8(raw, i, readu8(raw, i) + readu8(raw, i - stride)) - end - for i = cur + bpp, cur + rb - 1 do - local a = readu8(raw, i - bpp) - local b = readu8(raw, i - stride) - local c = readu8(raw, i - stride - bpp) - local pa = math.abs(b - c) - local pb = math.abs(a - c) - local pc = math.abs(a + b - c - c) - local v - if pa <= pb and pa <= pc then - v = a - elseif pb <= pc then - v = b - else - v = c - end - writeu8(raw, i, readu8(raw, i) + v) + -- the first pixel: a = c = 0, so Paeth picks b + for i = cur, cur + bpp - 1 do + writeu8(raw, i, readu8(raw, i) + readu8(raw, i - stride)) + end + if not pt then + for i = cur + bpp, e - 1 do + writeu8(raw, i, readu8(raw, i) + paeth(readu8(raw, i - bpp), readu8(raw, i - stride), readu8(raw, i - stride - bpp))) end + elseif bpp == 4 then + paeth4(raw, cur, e, stride, pt) + elseif bpp == 3 then + paeth3(raw, cur, e, stride, pt) + elseif bpp == 1 then + paeth1(raw, cur, e, stride, pt) else - -- no previous row: Paeth is sub - for i = cur + bpp, cur + rb - 1 do - writeu8(raw, i, readu8(raw, i) + readu8(raw, i - bpp)) - end + paethN(raw, cur, e, stride, bpp, pt) end else return false, f @@ -123,7 +247,16 @@ local function convert(raw: buffer, ro: number, pw: number, out: buffer, oo: num end end elseif ct == 2 then - if depth == 8 then + if depth == 8 and tr < 0 then + -- the u32 at a pixel's last byte + 1 is the next pixel's (or the next row's filter byte), except at the end + local o = oo + for s = ro, ro + (pw - 2) * 3, 3 do + writeu32(out, o, readu32(raw, s) % 16777216 + 0xFF000000) + o += ostep + end + local s = ro + (pw - 1) * 3 + writeu32(out, o, readu8(raw, s) + readu8(raw, s + 1) * 256 + readu8(raw, s + 2) * 65536 + 0xFF000000) + elseif depth == 8 then for x = 0, pw - 1 do local s = ro + x * 3 local r, g, b = readu8(raw, s), readu8(raw, s + 1), readu8(raw, s + 2) diff --git a/src/Rosetta/image/png/Encode.luau b/src/Rosetta/image/png/Encode.luau index 03dfe29..9e6e897 100644 --- a/src/Rosetta/image/png/Encode.luau +++ b/src/Rosetta/image/png/Encode.luau @@ -2,7 +2,11 @@ --!native --!optimize 2 -- image/png/Encode: PNG colour type choice and filtering (per-row minimum sum of |signed residuals|, as libpng). -local readu8, writeu8 = buffer.readu8, buffer.writeu8 +local Decode = require("@rosetta/image/png/Decode") + +local readu8, writeu8, readi16 = buffer.readu8, buffer.writeu8, buffer.readi16 +local abs = math.abs +local paethFor = Decode.paethFor local bcreate, bcopy = buffer.create, buffer.copy local Encode = {} @@ -66,36 +70,58 @@ local function paeth(a: number, b: number, c: number): number return c end -local function costs(src: buffer, s: number, rb: number, bpp: number, first: boolean): (number, number, number, number, number) +-- |residual as a signed byte| by residual + 256 (residual in -255 .. 255) +local SABS = table.create(511, 0) +for d = -255, 255 do + local m = d % 256 + SABS[d + 256] = if m < 128 then m else 256 - m +end + +local function costs(src: buffer, s: number, rb: number, bpp: number, first: boolean, pt: buffer?): (number, number, number, number, number) local c0, c1, c2, c3, c4 = 0, 0, 0, 0, 0 local p = s - rb - for i = 0, rb - 1 do - local x = readu8(src, s + i) - local a = if i >= bpp then readu8(src, s + i - bpp) else 0 - local b, c = 0, 0 - if not first then - b = readu8(src, p + i) - if i >= bpp then - c = readu8(src, p + i - bpp) + local sabs = SABS + if first or rb <= bpp or not pt then + for i = 0, rb - 1 do + local x = readu8(src, s + i) + local a = if i >= bpp then readu8(src, s + i - bpp) else 0 + local b, c = 0, 0 + if not first then + b = readu8(src, p + i) + if i >= bpp then + c = readu8(src, p + i - bpp) + end end + c0 += sabs[x + 256] + c1 += sabs[x - a + 256] + c2 += sabs[x - b + 256] + c3 += sabs[x - (a + b) // 2 + 256] + local pa, pb, pc = abs(b - c), abs(a - c), abs(a + b - c - c) + c4 += sabs[x - (if pa <= pb and pa <= pc then a elseif pb <= pc then b else c) + 256] end - local d = if x < 128 then x else 256 - x - c0 += d - d = (x - a) % 256 - c1 += if d < 128 then d else 256 - d - d = (x - b) % 256 - c2 += if d < 128 then d else 256 - d - d = (x - (a + b) // 2) % 256 - c3 += if d < 128 then d else 256 - d - local pa, pb, pc = math.abs(b - c), math.abs(a - c), math.abs(a + b - c - c) - local pr = if pa <= pb and pa <= pc then a elseif pb <= pc then b else c - d = (x - pr) % 256 - c4 += if d < 128 then d else 256 - d + return c0, c1, c2, c3, c4 + end + for i = 0, bpp - 1 do + local x, b = readu8(src, s + i), readu8(src, p + i) + c0 += sabs[x + 256] + c1 += sabs[x + 256] + c2 += sabs[x - b + 256] + c3 += sabs[x - b // 2 + 256] + c4 += sabs[x - b + 256] + end + for i = s + bpp, s + rb - 1 do + local x, a = readu8(src, i), readu8(src, i - bpp) + local b, c = readu8(src, i - rb), readu8(src, i - rb - bpp) + c0 += sabs[x + 256] + c1 += sabs[x - a + 256] + c2 += sabs[x - b + 256] + c3 += sabs[x - (a + b) // 2 + 256] + c4 += sabs[x - c - readi16(pt, (a * 511 + b - c * 512) * 2 + 261120) + 256] end return c0, c1, c2, c3, c4 end -local function apply(src: buffer, s: number, rb: number, bpp: number, first: boolean, f: number, out: buffer, o: number) +local function apply(src: buffer, s: number, rb: number, bpp: number, first: boolean, f: number, out: buffer, o: number, pt: buffer?) writeu8(out, o, f) o += 1 if f == 0 or (f == 2 and first) then @@ -103,6 +129,27 @@ local function apply(src: buffer, s: number, rb: number, bpp: number, first: boo return end local p = s - rb + if not first and rb > bpp and f ~= 3 and pt then + for i = 0, bpp - 1 do + writeu8(out, o + i, readu8(src, s + i) - (if f == 1 then 0 else readu8(src, p + i))) + end + local d = o - s + if f == 1 then + for i = s + bpp, s + rb - 1 do + writeu8(out, i + d, readu8(src, i) - readu8(src, i - bpp)) + end + elseif f == 2 then + for i = s + bpp, s + rb - 1 do + writeu8(out, i + d, readu8(src, i) - readu8(src, i - rb)) + end + else + for i = s + bpp, s + rb - 1 do + local a, b, c = readu8(src, i - bpp), readu8(src, i - rb), readu8(src, i - rb - bpp) + writeu8(out, i + d, readu8(src, i) - c - readi16(pt, (a * 511 + b - c * 512) * 2 + 261120)) + end + end + return + end for i = 0, rb - 1 do local x = readu8(src, s + i) local a = if i >= bpp then readu8(src, s + i - bpp) else 0 @@ -128,10 +175,11 @@ function Encode.filter(px: buffer, w: number, h: number, ct: number): buffer local src = pack(px, w, h, ct) local rb = w * ch local out = bcreate(h * (rb + 1)) + local pt = paethFor(h * rb) for y = 0, h - 1 do local s = y * rb local first = y == 0 - local c0, c1, c2, c3, c4 = costs(src, s, rb, ch, first) + local c0, c1, c2, c3, c4 = costs(src, s, rb, ch, first, pt) local best, bc = 0, c0 if c1 < bc then best, bc = 1, c1 @@ -145,7 +193,7 @@ function Encode.filter(px: buffer, w: number, h: number, ct: number): buffer if c4 < bc then best = 4 end - apply(src, s, rb, ch, first, best, out, y * (rb + 1)) + apply(src, s, rb, ch, first, best, out, y * (rb + 1), pt) end return out end diff --git a/src/Rosetta/ops/ImageOps/Kernels.luau b/src/Rosetta/ops/ImageOps/Kernels.luau index 467c4e4..b039b0c 100644 --- a/src/Rosetta/ops/ImageOps/Kernels.luau +++ b/src/Rosetta/ops/ImageOps/Kernels.luau @@ -5,6 +5,7 @@ -- Sample kinds: 1 = u8, 2 = u16, 4 = f32 (little endian). Resampling follows Pillow's Resample.c (separable, 22-bit -- fixed point for 8-bit) and Geometry.c (affine through pixel centres); compositing is Pillow's AlphaComposite.c. local readu8, writeu8, readu16, writeu16, readf32, writef32 = buffer.readu8, buffer.writeu8, buffer.readu16, buffer.writeu16, buffer.readf32, buffer.writef32 +local readu32, writeu32 = buffer.readu32, buffer.writeu32 local floor, clamp = math.floor, math.clamp local K = {} @@ -69,6 +70,32 @@ local function luma(r: number, g: number, b: number, depth: number): number end function K.convert(src: buffer, sf: number, dst: buffer, df: number, n: number) + if sf == 1 and df == 5 then + for i = 0, n - 1 do + local o = i * 4 + writeu8(dst, i, (readu8(src, o) * 19595 + readu8(src, o + 1) * 38470 + readu8(src, o + 2) * 7471 + 32768) // 65536) + end + return + elseif sf == 1 and df == 4 and n > 0 then + -- a u32 per pixel; its fourth byte is overwritten by the next pixel (the last one is written as 2 + 1 bytes) + for i = 0, n - 2 do + writeu32(dst, i * 3, readu32(src, i * 4)) + end + local o, d = (n - 1) * 4, (n - 1) * 3 + writeu16(dst, d, readu16(src, o)) + writeu8(dst, d + 2, readu8(src, o + 2)) + return + elseif df == 1 and (sf == 4 or sf == 5) then + for i = 0, n - 1 do + if sf == 4 then + local o = i * 3 + writeu32(dst, i * 4, readu8(src, o) + readu8(src, o + 1) * 256 + readu8(src, o + 2) * 65536 + 0xFF000000) + else + writeu32(dst, i * 4, readu8(src, i) * 0x10101 + 0xFF000000) + end + end + return + end local sd, dd = DEPTH[sf] :: number, DEPTH[df] :: number local smax = if sd == 8 then 255 elseif sd == 16 then 65535 else 1 local toGray = df == 5 or df == 6 or df == 7 @@ -295,6 +322,52 @@ function K.coeffs(inSize: number, outSize: number, filter: string): Coeffs return { start = start, count = count, weights = weights } end +-- 8-bit RGBA: the four channels of a tap together, the pixel written as one u32 +local function resample4(src: buffer, w: number, h: number, dst: buffer, outN: number, start: { number }, count: { number }, iw: { { number } }, horizontal: boolean) + local o = 0 + if horizontal then + for y = 0, h - 1 do + local row = y * w * 4 + for xx = 1, outN do + local wt = iw[xx] + local p = row + start[xx] * 4 + local a0, a1, a2, a3 = 2097152, 2097152, 2097152, 2097152 + for k = 1, count[xx] do + local wk = wt[k] + a0 += readu8(src, p) * wk + a1 += readu8(src, p + 1) * wk + a2 += readu8(src, p + 2) * wk + a3 += readu8(src, p + 3) * wk + p += 4 + end + writeu32(dst, o, clamp(a0 // 4194304, 0, 255) + clamp(a1 // 4194304, 0, 255) * 256 + clamp(a2 // 4194304, 0, 255) * 65536 + clamp(a3 // 4194304, 0, 255) * 16777216) + o += 4 + end + end + else + local rs = w * 4 + for yy = 1, outN do + local wt, n = iw[yy], count[yy] + local base = start[yy] * rs + for x = 0, rs - 4, 4 do + local p = base + x + local a0, a1, a2, a3 = 2097152, 2097152, 2097152, 2097152 + for k = 1, n do + local wk = wt[k] + a0 += readu8(src, p) * wk + a1 += readu8(src, p + 1) * wk + a2 += readu8(src, p + 2) * wk + a3 += readu8(src, p + 3) * wk + p += rs + end + writeu32(dst, o, clamp(a0 // 4194304, 0, 255) + clamp(a1 // 4194304, 0, 255) * 256 + clamp(a2 // 4194304, 0, 255) * 65536 + clamp(a3 // 4194304, 0, 255) * 16777216) + o += 4 + end + end + end + local _ = h +end + function K.resamplePass(src: buffer, w: number, h: number, ch: number, kind: number, dst: buffer, outN: number, c: Coeffs, horizontal: boolean) local start, count, weights = c.start, c.count, c.weights local size = kind @@ -313,6 +386,10 @@ function K.resamplePass(src: buffer, w: number, h: number, ch: number, kind: num iw[i] = t end end + if fixed and ch == 4 then + resample4(src, w, h, dst, outN, start, count, iw, horizontal) + return + end if horizontal then for y = 0, h - 1 do local row = y * w @@ -365,11 +442,23 @@ end -- nearest: dst pixel (x, y) = src pixel (floor((x + 0.5) * w / ow), ...) function K.nearest(src: buffer, w: number, h: number, bpp: number, dst: buffer, ow: number, oh: number) local sx, sy = w / ow, h / oh + local xs = table.create(ow, 0) + for x = 0, ow - 1 do + xs[x + 1] = math.min(floor((x + 0.5) * sx), w - 1) * bpp + end + local o = 0 for y = 0, oh - 1 do - local iy = math.min(floor((y + 0.5) * sy), h - 1) - for x = 0, ow - 1 do - local ix = math.min(floor((x + 0.5) * sx), w - 1) - buffer.copy(dst, (y * ow + x) * bpp, src, (iy * w + ix) * bpp, bpp) + local row = math.min(floor((y + 0.5) * sy), h - 1) * w * bpp + if bpp == 4 then + for x = 1, ow do + writeu32(dst, o, readu32(src, row + xs[x])) + o += 4 + end + else + for x = 1, ow do + buffer.copy(dst, o, src, row + xs[x], bpp) + o += bpp + end end end end @@ -424,6 +513,10 @@ function K.overRgba8(dst: buffer, dw: number, src: buffer, sw: number, sx: numbe end if sa == 0 then continue + elseif sa == 255 then + -- coef1 = 255 * 128, coef2 = 0: the source pixel exactly + writeu32(dst, doff, readu32(src, so)) + continue end local da = readu8(dst, doff + 3) local blend = da * (255 - sa) @@ -471,7 +564,44 @@ function K.copyRect(dst: buffer, dw: number, src: buffer, sw: number, sx: number end end +-- a = 255 leaves the colour as it is both ways (MULDIV255(v, 255) = v, v * 255 // 255 = v) +local function premultiply4(b: buffer, n: number, un: boolean) + for o = 3, n * 4 - 1, 4 do + local a = readu8(b, o) + if a == 255 then + continue + end + if a == 0 then + writeu16(b, o - 3, 0) + writeu8(b, o - 1, 0) + continue + end + for c = o - 3, o - 1 do + local v = readu8(b, c) + if un then + writeu8(b, c, math.min(255, (v * 255) // a)) -- Pillow's rgbA2rgba + else + local t = v * a + 128 -- Pillow's MULDIV255 + writeu8(b, c, ((t // 256) + t) // 256) + end + end + end +end + +function K.opaque(b: buffer, n: number): boolean + for o = 3, n * 4 - 1, 4 do + if readu8(b, o) ~= 255 then + return false + end + end + return true +end + function K.premultiply(b: buffer, n: number, ch: number, kind: number, un: boolean) + if kind == 1 and ch == 4 then + premultiply4(b, n, un) + return + end local max = if kind == 1 then 255 elseif kind == 2 then 65535 else 1 local px = ch * kind for i = 0, n - 1 do diff --git a/src/Rosetta/ops/ImageOps/init.luau b/src/Rosetta/ops/ImageOps/init.luau index 3df4e4e..ca7df8b 100644 --- a/src/Rosetta/ops/ImageOps/init.luau +++ b/src/Rosetta/ops/ImageOps/init.luau @@ -228,6 +228,10 @@ function ImageOps.resize(img: Image, w: number, h: number, filter: string?, opts local kind, ch = K.KIND[fmt], K.CHANNELS[fmt] local src, sw, sh = im.pixels, im.width, im.height local alpha = ALPHA_CH[fmt] + local unAlpha = alpha + if alpha == 4 and kind == 1 and K.opaque(src, sw * sh) then + alpha = nil -- premultiplying is the identity; the resampled alpha can still round to 254: unpremultiplied below + end if alpha then src = buffer.create(buffer.len(im.pixels)) buffer.copy(src, 0, im.pixels) @@ -246,8 +250,8 @@ function ImageOps.resize(img: Image, w: number, h: number, filter: string?, opts else out.pixels = src end - if alpha then - K.premultiply(out.pixels, w * h, alpha, kind, true) + if unAlpha then + K.premultiply(out.pixels, w * h, unAlpha, kind, true) end return out, nil end) diff --git a/tools/bench_image.luau b/tools/bench_image.luau new file mode 100644 index 0000000..91238c9 --- /dev/null +++ b/tools/bench_image.luau @@ -0,0 +1,182 @@ +-- Image throughput (ws-perf-image): decoders, encoders and ImageOps on the tools/bench.luau fixtures. MB/s of rgba8. +-- luau -O2 tools/bench_image.luau [-a filter] +local Bytes = require("@rosetta/core/Bytes") +local Crc32 = require("@rosetta/core/Crc32") +local Types = require("@rosetta/Types") + +local runtime = if (_G :: any).jit ~= nil then "luajit" else "luau" +local filter = (...) :: string? +local REP = 5 + +local function want(...: string): boolean + if not filter then + return true + end + for _, k in { ... } do + if string.find(filter :: string, k, 1, true) or string.find(k, filter :: string, 1, true) then + return true + end + end + return false +end + +local function bench(label: string, n: number, bytes: number, f: () -> ()) + if filter and not string.find(label, filter, 1, true) then + return + end + f() + local dt = math.huge + for _ = 1, math.max(n, REP) do + local t0 = os.clock() + f() + dt = math.min(dt, os.clock() - t0) + end + print(string.format("%-8s %-36s %8.2f ms %7.1f MB/s", runtime, label, dt * 1000, bytes / dt / 1e6)) +end + +if want("jpeg") then + local Jpeg = require("@rosetta/image/jpeg") + local JB = require("@tests/gen/jpeg_bench") + for _, c in JB.files do + local b = buffer.fromstring(c.data) + local img = Jpeg.decode(Bytes.reader(b)) + assert(img and Crc32.of(img.pixels) == c.crc, "jpeg bench: " .. c.name .. " does not match djpeg") + bench("jpeg decode " .. c.name, 3, c.width * c.height * 4, function() + Jpeg.decode(Bytes.reader(b)) + end) + end + local b0 = buffer.fromstring(JB.files[1].data) + bench("jpeg decode 1/8 (DC only)", 3, JB.width * JB.height * 4, function() + Jpeg.decode(Bytes.reader(b0), { scale = 8 }) + end) + local src = Types.Image.new(JB.width, JB.height, "rgb8") + buffer.writestring(src.pixels, 0, JB.rgb) + for _, o in { { label = "baseline 4:2:0 q85", quality = 85 }, { label = "progressive 4:2:0 q85", quality = 85, progressive = true }, { label = "baseline 4:4:4 q85 optimized", quality = 85, subsampling = "444", optimize = true } } do + bench("jpeg encode " .. o.label, 2, JB.width * JB.height * 3, function() + Jpeg.encode(src, o :: any) + end) + end +end +if want("png", "imageops") then + local Png = require("@rosetta/image/png") + local PB = require("@tests/gen/png_bench") + for _, c in PB do + local b = buffer.fromstring(c.data) + bench("png decode " .. c.name, 3, c.width * c.height * 4, function() + Png.decode(Bytes.reader(b)) + end) + end + local img = assert(Png.decode(Bytes.reader(buffer.fromstring(PB[1].data)))) + bench("png encode rgba8", 2, img.width * img.height * 4, function() + Png.encode(img) + end) + local Ops = require("@rosetta/ops/ImageOps") + local n = img.width * img.height * 4 + bench("imageops resize 1/2 bilinear", 2, n, function() + Ops.resize(img, img.width // 2, img.height // 2, "bilinear") + end) + bench("imageops resize 1/2 lanczos", 2, n, function() + Ops.resize(img, img.width // 2, img.height // 2, "lanczos") + end) + bench("imageops resize 1/4 nearest", 2, n, function() + Ops.resize(img, img.width // 4, img.height // 4, "nearest") + end) + bench("imageops resize x1.5 bilinear", 1, n, function() + Ops.resize(img, img.width * 3 // 2, img.height * 3 // 2, "bilinear") + end) + bench("imageops rotate90", 3, n, function() + Ops.rotate90(img, 1) + end) + bench("imageops convert rgba8 -> gray8", 3, n, function() + Ops.convert(img, "gray8") + end) + bench("imageops convert rgba8 -> rgb8", 3, n, function() + Ops.convert(img, "rgb8") + end) + local other = assert(Ops.flip(img, "h")) + bench("imageops composite over", 3, n, function() + Ops.composite(img, other) + end) +end +if want("gif", "quantize") then + local Gif = require("@rosetta/image/gif") + local Quantize = require("@rosetta/codecs/image/Quantize") + local GB = require("@tests/gen/gif_bench") + local still = buffer.fromstring(GB.still) + local anim = buffer.fromstring(GB.anim) + local simg = assert(Gif.decode(Bytes.reader(still))) + local aimg = assert(Gif.decode(Bytes.reader(anim))) + local sw, sh = simg.width, simg.height + bench("gif decode 512x384", 5, sw * sh * 4, function() + Gif.decode(Bytes.reader(still)) + end) + bench("gif decode 40 x 128x128 anim", 3, #(aimg.frames :: any) * 128 * 128 * 4, function() + Gif.decode(Bytes.reader(anim)) + end) + bench("gif encode 512x384 (exact pal)", 3, sw * sh * 4, function() + Gif.encode(simg) + end) + local photo = Types.Image.new(sw, sh) + for i = 0, sw * sh - 1 do + local x, y = i % sw, i // sw + buffer.writeu32(photo.pixels, i * 4, (x % 256) + (y % 256) * 256 + ((x * y) % 256) * 65536 + 0xFF000000) + end + bench("quantize mediancut 256", 3, sw * sh * 4, function() + Quantize.palette(photo.pixels, sw, sh, 256) + end) + local pal = Quantize.palette(photo.pixels, sw, sh, 256) + bench("quantize map", 3, sw * sh * 4, function() + Quantize.map(photo.pixels, sw, sh, pal) + end) + bench("quantize map + dither", 3, sw * sh * 4, function() + Quantize.map(photo.pixels, sw, sh, pal, { dither = true }) + end) + bench("gif encode 512x384 (quantize)", 2, sw * sh * 4, function() + Gif.encode(photo) + end) +end +if want("webp") then + local Webp = require("@rosetta/image/webp") + local WB = require("@tests/gen/webp_bench") + local n = WB.width * WB.height * 4 + local lossy, lossless = buffer.fromstring(WB.lossy), buffer.fromstring(WB.lossless) + bench("webp lossy decode 512x512", 3, n, function() + Webp.decode(Bytes.reader(lossy)) + end) + bench("webp lossless decode 512x512", 3, n, function() + Webp.decode(Bytes.reader(lossless)) + end) + local img = assert(Webp.decode(Bytes.reader(lossless))) + bench("webp lossless encode (effort 1)", 1, n, function() + Webp.encode(img, { effort = 1 }) + end) +end +if want("qoi", "tga", "bmp", "tiff") then + local Qoi = require("@rosetta/image/qoi") + local Tga = require("@rosetta/image/tga") + local Bmp = require("@rosetta/image/bmp") + local Tiff = require("@rosetta/image/tiff") + local QB = require("@tests/gen/qoi_bench") + local qd = buffer.fromstring(QB.data) + local n = QB.width * QB.height * 4 + bench("qoi decode 1024x768", 5, n, function() + Qoi.decode(Bytes.reader(qd)) + end) + local img = Types.Image.new(QB.width, QB.height) + img.pixels = buffer.fromstring(QB.pixels) + bench("qoi encode 1024x768", 5, n, function() + Qoi.encode(img) + end) + local tr = Tga.encode(img, { rle = true }) :: buffer + bench("tga decode rle 1024x768", 3, n, function() + Tga.decode(Bytes.reader(tr)) + end) + local bm = Bmp.encode(img) :: buffer + bench("bmp decode 1024x768", 3, n, function() + Bmp.decode(Bytes.reader(bm)) + end) + local tl = Tiff.encode(img, { compression = "lzw" }) :: buffer + bench("tiff decode lzw 1024x768", 3, n, function() + Tiff.decode(Bytes.reader(tl)) + end) +end