merge ws/perf-image
This commit is contained in:
commit
d5276798c3
24 files changed
+5399
-2126
No files matched your search
@@ -175,21 +175,16 @@ local function subset(ns: number, part: number, i: number): number
|
||||
return 0
|
||||
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)
|
||||
w0, w1, w2, w3 = 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
|
||||
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 bits(at: number, n: number): number
|
||||
if n == 0 then
|
||||
return 0
|
||||
end
|
||||
local wi, sh = rshift(at, 5), band(at, 31)
|
||||
local v = rshift(word(wi), sh)
|
||||
if sh + n > 32 and wi < 3 then
|
||||
v = bor(v, lshift(word(wi + 1), 32 - sh))
|
||||
local wi, sh = rshift(at, 5) + 1, band(at, 31)
|
||||
local v = rshift(W[wi], sh)
|
||||
if sh + n > 32 then
|
||||
v = bor(v, lshift(W[wi + 1], 32 - sh))
|
||||
end
|
||||
return band(v, lshift(1, n) - 1)
|
||||
end
|
||||
@@ -412,7 +407,7 @@ end
|
||||
|
||||
function Decode.bc6h(src: buffer, p: number, blk: buffer, signed: boolean)
|
||||
load(src, p)
|
||||
local m = band(w0, 31)
|
||||
local m = band(W[1], 31)
|
||||
local mode, bit, epbits, ib
|
||||
local low = band(m, 3)
|
||||
if low < 2 then
|
||||
|
||||
@@ -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,100 +41,192 @@ 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)
|
||||
for i = 0, 14, 2 do
|
||||
writeu16(plane, o + i, v)
|
||||
end
|
||||
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 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
|
||||
local t0, t1, t2, t3 = w7, w5, w3, w1
|
||||
z1 = t0 + t3
|
||||
local z2 = t1 + t2
|
||||
local z3 = t0 + t2
|
||||
local z4 = t1 + t3
|
||||
z1 = w7 + w1
|
||||
local z2 = w5 + w3
|
||||
local z3 = w7 + w3
|
||||
local z4 = w5 + w1
|
||||
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 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 5 x 5 (zigzag index <= 14): the same sums without the zero terms
|
||||
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 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, 4 do
|
||||
local o = off + c * 2
|
||||
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 and c4 == 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
|
||||
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 tmp11, tmp12 = tmp1 + tmp2, tmp1 - 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
|
||||
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)
|
||||
local hd = sd / 2 + center * sd
|
||||
for r = 0, 7 do
|
||||
local b = r * 8
|
||||
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
|
||||
if w1 == 0 and w2 == 0 and w3 == 0 and w4 == 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 tmp2 = w2 * 4433
|
||||
local tmp3 = tmp2 + 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
|
||||
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 +336,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 <= 14 then
|
||||
idct8x5(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 +379,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)
|
||||
|
||||
@@ -142,7 +142,127 @@ 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)
|
||||
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])
|
||||
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
|
||||
@@ -156,13 +276,16 @@ function Color.run(P: Params)
|
||||
end
|
||||
local s0, b0 = fetch(comps[1], y, outW)
|
||||
if space == 0 or nc < 3 then
|
||||
for x = 0, outW - 1 do
|
||||
local v = readu16(s0, b0 + x * 2)
|
||||
if toByte then
|
||||
v = readu8(toByte, v)
|
||||
if toByte then
|
||||
for x = 0, outW - 1 do
|
||||
writeu32(out, o, 0xFF000000 + readu8(toByte, readu16(s0, b0 + x * 2)) * 0x10101)
|
||||
o += 4
|
||||
end
|
||||
else
|
||||
for i = b0, b0 + outW * 2 - 2, 2 do
|
||||
writeu32(out, o, 0xFF000000 + readu16(s0, i) * 0x10101)
|
||||
o += 4
|
||||
end
|
||||
writeu32(out, o, 0xFF000000 + v * 0x10101)
|
||||
o += 4
|
||||
end
|
||||
else
|
||||
local s1, b1 = fetch(comps[2], y, outW)
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -402,43 +402,45 @@ function Quantize.map(pixels: buffer, w: number, h: number, palette: { Color },
|
||||
if tidx and readu8(pixels, o + 3) < 128 then
|
||||
writeu8(out, i, tbyte)
|
||||
else
|
||||
writeu8(out, i, pi[nearest(readu8(pixels, o), readu8(pixels, o + 1), readu8(pixels, o + 2))])
|
||||
local r, g, b = readu8(pixels, o), readu8(pixels, o + 1), readu8(pixels, o + 2)
|
||||
local hit = cache[r * 65536 + g * 256 + b]
|
||||
writeu8(out, i, pi[hit or nearest(r, g, b)])
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
local rowLen = (w + 2) * 24
|
||||
local cur, nxt = buffer.create(rowLen), buffer.create(rowLen)
|
||||
-- Floyd-Steinberg with the error sums kept as the buffered version adds them: the right neighbour's share is
|
||||
-- carried (its upper-row sum + 7/16 e), and each lower-row slot is (e[x-1] / 16 + 5/16 e[x]) + 3/16 e[x+1]
|
||||
local cur, nxt = table.create(w * 3 + 6, 0), table.create(w * 3 + 6, 0)
|
||||
for y = 0, h - 1 do
|
||||
buffer.fill(nxt, 0, 0, rowLen)
|
||||
local cr, cg, cb = 0, 0, 0 -- 7/16 of the left pixel's error
|
||||
local p0r, p0g, p0b = 0, 0, 0 -- slot x of the next row: e[x-1] / 16
|
||||
local p1r, p1g, p1b = 0, 0, 0 -- slot x - 1 of the next row: e[x-2] / 16 + 5/16 e[x-1]
|
||||
local i = y * w
|
||||
for x = 0, w - 1 do
|
||||
local i = y * w + x
|
||||
local o = i * 4
|
||||
local q = x * 3 + 1
|
||||
local er, eg, eb = 0, 0, 0
|
||||
if tidx and readu8(pixels, o + 3) < 128 then
|
||||
writeu8(out, i, tbyte)
|
||||
else
|
||||
local e = (x + 1) * 24
|
||||
local r = math.clamp(floor(readu8(pixels, o) + readf64(cur, e) + 0.5), 0, 255)
|
||||
local g = math.clamp(floor(readu8(pixels, o + 1) + readf64(cur, e + 8) + 0.5), 0, 255)
|
||||
local b = math.clamp(floor(readu8(pixels, o + 2) + readf64(cur, e + 16) + 0.5), 0, 255)
|
||||
local k = nearest(r, g, b)
|
||||
local r = math.clamp(floor(readu8(pixels, o) + (cur[q] + cr) + 0.5), 0, 255)
|
||||
local g = math.clamp(floor(readu8(pixels, o + 1) + (cur[q + 1] + cg) + 0.5), 0, 255)
|
||||
local b = math.clamp(floor(readu8(pixels, o + 2) + (cur[q + 2] + cb) + 0.5), 0, 255)
|
||||
local k = cache[r * 65536 + g * 256 + b] or nearest(r, g, b)
|
||||
writeu8(out, i, pi[k])
|
||||
local er, eg, eb = r - pr[k], g - pg[k], b - pb[k]
|
||||
-- 7/16 right, 3/16 down-left, 5/16 down, 1/16 down-right
|
||||
writef64(cur, e + 24, readf64(cur, e + 24) + er * 0.4375)
|
||||
writef64(cur, e + 32, readf64(cur, e + 32) + eg * 0.4375)
|
||||
writef64(cur, e + 40, readf64(cur, e + 40) + eb * 0.4375)
|
||||
writef64(nxt, e - 24, readf64(nxt, e - 24) + er * 0.1875)
|
||||
writef64(nxt, e - 16, readf64(nxt, e - 16) + eg * 0.1875)
|
||||
writef64(nxt, e - 8, readf64(nxt, e - 8) + eb * 0.1875)
|
||||
writef64(nxt, e, readf64(nxt, e) + er * 0.3125)
|
||||
writef64(nxt, e + 8, readf64(nxt, e + 8) + eg * 0.3125)
|
||||
writef64(nxt, e + 16, readf64(nxt, e + 16) + eb * 0.3125)
|
||||
writef64(nxt, e + 24, readf64(nxt, e + 24) + er * 0.0625)
|
||||
writef64(nxt, e + 32, readf64(nxt, e + 32) + eg * 0.0625)
|
||||
writef64(nxt, e + 40, readf64(nxt, e + 40) + eb * 0.0625)
|
||||
er, eg, eb = r - pr[k], g - pg[k], b - pb[k]
|
||||
end
|
||||
cr, cg, cb = er * 0.4375, eg * 0.4375, eb * 0.4375
|
||||
if x > 0 then
|
||||
nxt[q - 3], nxt[q - 2], nxt[q - 1] = p1r + er * 0.1875, p1g + eg * 0.1875, p1b + eb * 0.1875
|
||||
end
|
||||
p1r, p1g, p1b = p0r + er * 0.3125, p0g + eg * 0.3125, p0b + eb * 0.3125
|
||||
p0r, p0g, p0b = er * 0.0625, eg * 0.0625, eb * 0.0625
|
||||
i += 1
|
||||
end
|
||||
local q = (w - 1) * 3 + 1
|
||||
nxt[q], nxt[q + 1], nxt[q + 2] = p1r, p1g, p1b
|
||||
cur, nxt = nxt, cur
|
||||
end
|
||||
return out
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 i, col, row = 0, 0, 0
|
||||
local gG, gR, gB, gA, gD = groups[1], groups[2], groups[3], groups[4], groups[5]
|
||||
local tab = d.tab
|
||||
local function setGroup()
|
||||
if himg then
|
||||
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
|
||||
setGroup()
|
||||
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
|
||||
local r = readSymbol(d, gR)
|
||||
local b = readSymbol(d, gB)
|
||||
local a = readSymbol(d, gA)
|
||||
if d.bitcnt < d.over * 8 then
|
||||
if bc < MAX_LEN then
|
||||
d.bitbuf, d.bitcnt = bb, bc
|
||||
fill(d)
|
||||
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
|
||||
end
|
||||
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
|
||||
end
|
||||
elseif code < lenLimit then
|
||||
d.bitbuf, d.bitcnt = bb, bc
|
||||
local ls = code - NUM_LITERAL
|
||||
local length
|
||||
if ls < 4 then
|
||||
@@ -422,6 +491,7 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
|
||||
setGroup()
|
||||
end
|
||||
elseif code < cacheLimit and cache then
|
||||
d.bitbuf, d.bitcnt = bb, bc
|
||||
if d.bitcnt < d.over * 8 then
|
||||
break
|
||||
end
|
||||
@@ -444,8 +514,9 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- per-byte sums mod 256 (the low seven bits added, the top bits xored in)
|
||||
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
|
||||
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)
|
||||
writeu32(data, (row + k) * 4, L)
|
||||
end
|
||||
elseif mode == 2 then
|
||||
for k = x, xe - 1 do
|
||||
writeu32(data, (row + k) * 4, addPixels(readu32(data, (row + k) * 4), readu32(data, (up + k) * 4)))
|
||||
elseif mode == 2 or mode == 3 or mode == 4 then
|
||||
-- T, TR, TL: the upper row only
|
||||
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
|
||||
else
|
||||
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 r2b = sint8(band(rshift(m, 16), 255))
|
||||
local xe = math.min(w, lshift(rshift(x, bits) + 1, bits))
|
||||
for k = y * w + x, y * w + xe - 1 do
|
||||
local argb = readu32(data, k * 4)
|
||||
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, k * 4, band(argb, 0xff00ff00) + r * 65536 + b)
|
||||
if g2r == 0 and g2b == 0 and r2b == 0 then
|
||||
x = xe -- the identity
|
||||
continue
|
||||
end
|
||||
if r2b == 0 then
|
||||
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
|
||||
x = xe
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -8,8 +8,14 @@ local Codec = {}
|
||||
|
||||
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?)
|
||||
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 o = 0
|
||||
local stop = n * 4
|
||||
@@ -19,62 +25,63 @@ function Codec.decode(b: buffer, p: number, limit: number, out: buffer, n: numbe
|
||||
end
|
||||
local op = readu8(b, p)
|
||||
p += 1
|
||||
if op == 0xFE then
|
||||
if p + 3 > limit then
|
||||
return false, "truncated"
|
||||
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
|
||||
if op >= 0xC0 then
|
||||
if op == 0xFE then
|
||||
if p + 3 > 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
|
||||
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 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 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
|
||||
writeu32(out, o, v)
|
||||
o += 4
|
||||
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
|
||||
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
|
||||
writeu32(out, o, r + g * 256 + bl * 65536 + a * 16777216)
|
||||
o += 4
|
||||
local h = ((r * 3 + g * 5 + bl * 7 + a * 11) % 64) * 4
|
||||
writeu8(index, h, r)
|
||||
writeu8(index, h + 1, g)
|
||||
writeu8(index, h + 2, bl)
|
||||
writeu8(index, h + 3, a)
|
||||
local h = (r * 3 + g * 5 + bl * 7 + a * 11) % 64 + 1
|
||||
ir[h], ig[h], ib[h], ia[h] = r, g, bl, a
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
@@ -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)
|
||||
local n = w * h
|
||||
local i = 0
|
||||
local function put(v: number)
|
||||
local row, col = i // w, i % w
|
||||
if bottomUp then
|
||||
row = h - 1 - row
|
||||
end
|
||||
if rtl then
|
||||
col = w - 1 - col
|
||||
end
|
||||
writeu32(out, (row * w + col) * 4, v)
|
||||
i += 1
|
||||
-- the output position of pixel i: row by row (bottom-up flips rows), each row left to right or right to left
|
||||
local step = if rtl then -4 else 4
|
||||
local row, col = 0, 0
|
||||
local function rowStart(r: number): number
|
||||
return ((if bottomUp then h - 1 - r else r) * w + (if rtl then w - 1 else 0)) * 4
|
||||
end
|
||||
local o = rowStart(0)
|
||||
if not rle then
|
||||
local avail = math.min(n, (limit - p) // bpp)
|
||||
for _ = 1, avail do
|
||||
put(pixel(b, p, kind, alpha1, pal, palN))
|
||||
p += bpp
|
||||
while i < avail do
|
||||
local e = math.min(avail - i, w - col)
|
||||
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
|
||||
return i, avail < n
|
||||
end
|
||||
@@ -58,23 +80,49 @@ function Codec.decode(b: buffer, p: number, limit: number, w: number, h: number,
|
||||
end
|
||||
local hd = readu8(b, p)
|
||||
p += 1
|
||||
local count = hd % 128 + 1
|
||||
if hd >= 128 then
|
||||
local count = math.min(hd % 128 + 1, n - i)
|
||||
local packed = hd >= 128
|
||||
local v = 0
|
||||
if packed then
|
||||
if p + bpp > limit then
|
||||
return i, true
|
||||
end
|
||||
local v = pixel(b, p, kind, alpha1, pal, palN)
|
||||
v = pixel(b, p, kind, alpha1, pal, palN)
|
||||
p += bpp
|
||||
for _ = 1, math.min(count, n - i) do
|
||||
put(v)
|
||||
end
|
||||
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
|
||||
else
|
||||
for _ = 1, math.min(count, n - i) do
|
||||
if p + bpp > limit then
|
||||
return i, true
|
||||
i += e
|
||||
col += e
|
||||
count -= e
|
||||
if col == w then
|
||||
col = 0
|
||||
row += 1
|
||||
if row < h then
|
||||
o = rowStart(row)
|
||||
end
|
||||
put(pixel(b, p, kind, alpha1, pal, palN))
|
||||
p += bpp
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user