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
|
||||
|
||||
Reference in new issue
Block a user