perf(image): JPEG fused YCbCr upsampling/colour (8-bit), IDCT DC-only / 4x4 paths from per-block nonzero extent, progressive refine skips zero tails; PNG Paeth table + SWAR sub/avg, rgb8 convert, encoder filter costs by table; GIF palette table + opaque draw copy; WebP fancy upsampler inlined; ImageOps 8-bit RGBA resample, opaque premultiply skip, convert / nearest fast paths; tools/bench_image.luau
This commit is contained in:
1 parent
01c044805f
commit
9855a7edd6
12 files changed
+997
-193
No files matched your search
@@ -4,14 +4,23 @@
|
||||
-- codecs/image/Dct: the 8x8 DCTs JPEG, Motion-JPEG and MPEG-1 share (native; never throws).
|
||||
-- libjpeg's islow (jidctint.c / jfdctint.c), the reduced IDCTs (jidctred.c) and jcdctmgr.c's rounding quantiser, bit-exact
|
||||
-- with libjpeg-turbo. From jpeg-luau 1.0 (same author); IJG code: licenses/libjpeg-turbo-LICENSE.md.
|
||||
local readi16, readu16, writeu16, readu8, writei16 = buffer.readi16, buffer.readu16, buffer.writeu16, buffer.readu8, buffer.writei16
|
||||
local readi16, readu16, writeu16, writeu32, readu8, writei16 = buffer.readi16, buffer.readu16, buffer.writeu16, buffer.writeu32, buffer.readu8, buffer.writei16
|
||||
local clamp = math.clamp
|
||||
|
||||
local Dct = {}
|
||||
|
||||
local ws = table.create(64, 0)
|
||||
|
||||
local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
local function dequant(q: buffer): { number }
|
||||
local t = table.create(64, 0)
|
||||
for i = 0, 63 do
|
||||
t[i + 1] = readu16(q, i * 2)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
local function idct8(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
local ws = ws
|
||||
local s1 = 2 ^ (13 - p1)
|
||||
local h1 = s1 / 2
|
||||
local s2 = 2 ^ (16 + p1)
|
||||
@@ -19,11 +28,10 @@ local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: n
|
||||
local dcm = 2 ^ p1
|
||||
for c = 0, 7 do
|
||||
local o = off + c * 2
|
||||
local qo = c * 2
|
||||
local c1, c2, c3 = readi16(coef, o + 16), readi16(coef, o + 32), readi16(coef, o + 48)
|
||||
local c4, c5, c6, c7 = readi16(coef, o + 64), readi16(coef, o + 80), readi16(coef, o + 96), readi16(coef, o + 112)
|
||||
if c1 == 0 and c2 == 0 and c3 == 0 and c4 == 0 and c5 == 0 and c6 == 0 and c7 == 0 then
|
||||
local dc = readi16(coef, o) * readu16(q, qo) * dcm
|
||||
local dc = readi16(coef, o) * qt[c + 1] * dcm
|
||||
ws[c + 1] = dc
|
||||
ws[c + 9] = dc
|
||||
ws[c + 17] = dc
|
||||
@@ -33,48 +41,136 @@ local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: n
|
||||
ws[c + 49] = dc
|
||||
ws[c + 57] = dc
|
||||
else
|
||||
-- even part
|
||||
local z2 = c2 * readu16(q, qo + 32)
|
||||
local z3 = c6 * readu16(q, qo + 96)
|
||||
-- even part (the pass-1 rounding folded into tmp0 / tmp1)
|
||||
local z2 = c2 * qt[c + 17]
|
||||
local z3 = c6 * qt[c + 49]
|
||||
local z1 = (z2 + z3) * 4433
|
||||
local tmp2 = z1 - z3 * 15137
|
||||
local tmp3 = z1 + z2 * 6270
|
||||
z2 = readi16(coef, o) * readu16(q, qo)
|
||||
z3 = c4 * readu16(q, qo + 64)
|
||||
local tmp0 = (z2 + z3) * 8192
|
||||
local tmp1 = (z2 - z3) * 8192
|
||||
z2 = readi16(coef, o) * qt[c + 1]
|
||||
z3 = c4 * qt[c + 33]
|
||||
local tmp0 = (z2 + z3) * 8192 + h1
|
||||
local tmp1 = (z2 - z3) * 8192 + h1
|
||||
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
|
||||
local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2
|
||||
-- odd part
|
||||
local t0 = c7 * readu16(q, qo + 112)
|
||||
local t1 = c5 * readu16(q, qo + 80)
|
||||
local t2 = c3 * readu16(q, qo + 48)
|
||||
local t3 = c1 * readu16(q, qo + 16)
|
||||
local t0 = c7 * qt[c + 57]
|
||||
local t1 = c5 * qt[c + 41]
|
||||
local t2 = c3 * qt[c + 25]
|
||||
local t3 = c1 * qt[c + 9]
|
||||
z1 = t0 + t3
|
||||
z2 = t1 + t2
|
||||
z3 = t0 + t2
|
||||
local z4 = t1 + t3
|
||||
local z5 = (z3 + z4) * 9633
|
||||
t0 *= 2446
|
||||
t1 *= 16819
|
||||
t2 *= 25172
|
||||
t3 *= 12299
|
||||
z1 *= -7373
|
||||
z2 *= -20995
|
||||
z3 = z3 * -16069 + z5
|
||||
z4 = z4 * -3196 + z5
|
||||
t0 += z1 + z3
|
||||
t1 += z2 + z4
|
||||
t2 += z2 + z3
|
||||
t3 += z1 + z4
|
||||
ws[c + 1] = (tmp10 + t3 + h1) // s1
|
||||
ws[c + 57] = (tmp10 - t3 + h1) // s1
|
||||
ws[c + 9] = (tmp11 + t2 + h1) // s1
|
||||
ws[c + 49] = (tmp11 - t2 + h1) // s1
|
||||
ws[c + 17] = (tmp12 + t1 + h1) // s1
|
||||
ws[c + 41] = (tmp12 - t1 + h1) // s1
|
||||
ws[c + 25] = (tmp13 + t0 + h1) // s1
|
||||
ws[c + 33] = (tmp13 - t0 + h1) // s1
|
||||
t0 = t0 * 2446 + z1 + z3
|
||||
t1 = t1 * 16819 + z2 + z4
|
||||
t2 = t2 * 25172 + z2 + z3
|
||||
t3 = t3 * 12299 + z1 + z4
|
||||
ws[c + 1] = (tmp10 + t3) // s1
|
||||
ws[c + 57] = (tmp10 - t3) // s1
|
||||
ws[c + 9] = (tmp11 + t2) // s1
|
||||
ws[c + 49] = (tmp11 - t2) // s1
|
||||
ws[c + 17] = (tmp12 + t1) // s1
|
||||
ws[c + 41] = (tmp12 - t1) // s1
|
||||
ws[c + 25] = (tmp13 + t0) // s1
|
||||
ws[c + 33] = (tmp13 - t0) // s1
|
||||
end
|
||||
end
|
||||
local sd = 2 ^ (p1 + 3)
|
||||
local hd = sd / 2 + center * sd
|
||||
-- u16 pairs written as one u32 (little endian: the left sample in the low half)
|
||||
for r = 0, 7 do
|
||||
local b = r * 8
|
||||
local w0, w1, w2, w3 = ws[b + 1], ws[b + 2], ws[b + 3], ws[b + 4]
|
||||
local w4, w5, w6, w7 = ws[b + 5], ws[b + 6], ws[b + 7], ws[b + 8]
|
||||
local o = (out + r * stride) * 2
|
||||
if w1 == 0 and w2 == 0 and w3 == 0 and w4 == 0 and w5 == 0 and w6 == 0 and w7 == 0 then
|
||||
local v = clamp((w0 + hd) // sd, 0, maxv) * 65537
|
||||
writeu32(plane, o, v)
|
||||
writeu32(plane, o + 4, v)
|
||||
writeu32(plane, o + 8, v)
|
||||
writeu32(plane, o + 12, v)
|
||||
else
|
||||
local z1 = (w2 + w6) * 4433
|
||||
local tmp2 = z1 - w6 * 15137
|
||||
local tmp3 = z1 + w2 * 6270
|
||||
local tmp0 = (w0 + w4) * 8192 + h2
|
||||
local tmp1 = (w0 - w4) * 8192 + h2
|
||||
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
|
||||
local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2
|
||||
z1 = w7 + w1
|
||||
local z2 = w5 + w3
|
||||
local z3 = w7 + w3
|
||||
local z4 = w5 + w1
|
||||
local z5 = (z3 + z4) * 9633
|
||||
z1 *= -7373
|
||||
z2 *= -20995
|
||||
z3 = z3 * -16069 + z5
|
||||
z4 = z4 * -3196 + z5
|
||||
local t0 = w7 * 2446 + z1 + z3
|
||||
local t1 = w5 * 16819 + z2 + z4
|
||||
local t2 = w3 * 25172 + z2 + z3
|
||||
local t3 = w1 * 12299 + z1 + z4
|
||||
writeu32(plane, o, clamp((tmp10 + t3) // s2, 0, maxv) + clamp((tmp11 + t2) // s2, 0, maxv) * 65536)
|
||||
writeu32(plane, o + 4, clamp((tmp12 + t1) // s2, 0, maxv) + clamp((tmp13 + t0) // s2, 0, maxv) * 65536)
|
||||
writeu32(plane, o + 8, clamp((tmp13 - t0) // s2, 0, maxv) + clamp((tmp12 - t1) // s2, 0, maxv) * 65536)
|
||||
writeu32(plane, o + 12, clamp((tmp11 - t2) // s2, 0, maxv) + clamp((tmp10 - t3) // s2, 0, maxv) * 65536)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- idct8 with every nonzero coefficient in the top-left 4 x 4 (zigzag index <= 9): the same sums without the zero terms
|
||||
local function idct8x4(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
local ws = ws
|
||||
local s1 = 2 ^ (13 - p1)
|
||||
local h1 = s1 / 2
|
||||
local s2 = 2 ^ (16 + p1)
|
||||
local h2 = s2 / 2 + center * s2
|
||||
local dcm = 2 ^ p1
|
||||
for c = 0, 3 do
|
||||
local o = off + c * 2
|
||||
local c1, c2, c3 = readi16(coef, o + 16), readi16(coef, o + 32), readi16(coef, o + 48)
|
||||
if c1 == 0 and c2 == 0 and c3 == 0 then
|
||||
local dc = readi16(coef, o) * qt[c + 1] * dcm
|
||||
ws[c + 1] = dc
|
||||
ws[c + 9] = dc
|
||||
ws[c + 17] = dc
|
||||
ws[c + 25] = dc
|
||||
ws[c + 33] = dc
|
||||
ws[c + 41] = dc
|
||||
ws[c + 49] = dc
|
||||
ws[c + 57] = dc
|
||||
else
|
||||
local z2 = c2 * qt[c + 17]
|
||||
local tmp2 = z2 * 4433
|
||||
local tmp3 = tmp2 + z2 * 6270
|
||||
local tmp0 = readi16(coef, o) * qt[c + 1] * 8192 + h1
|
||||
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
|
||||
local tmp11, tmp12 = tmp0 + tmp2, tmp0 - tmp2
|
||||
local t2 = c3 * qt[c + 25]
|
||||
local t3 = c1 * qt[c + 9]
|
||||
local z5 = (t2 + t3) * 9633
|
||||
local z1 = t3 * -7373
|
||||
z2 = t2 * -20995
|
||||
local z3 = t2 * -16069 + z5
|
||||
local z4 = t3 * -3196 + z5
|
||||
local t0 = z1 + z3
|
||||
local t1 = z2 + z4
|
||||
t2 = t2 * 25172 + z2 + z3
|
||||
t3 = t3 * 12299 + z1 + z4
|
||||
ws[c + 1] = (tmp10 + t3) // s1
|
||||
ws[c + 57] = (tmp10 - t3) // s1
|
||||
ws[c + 9] = (tmp11 + t2) // s1
|
||||
ws[c + 49] = (tmp11 - t2) // s1
|
||||
ws[c + 17] = (tmp12 + t1) // s1
|
||||
ws[c + 41] = (tmp12 - t1) // s1
|
||||
ws[c + 25] = (tmp13 + t0) // s1
|
||||
ws[c + 33] = (tmp13 - t0) // s1
|
||||
end
|
||||
end
|
||||
local sd = 2 ^ (p1 + 3)
|
||||
@@ -82,51 +178,51 @@ local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: n
|
||||
for r = 0, 7 do
|
||||
local b = r * 8
|
||||
local w0, w1, w2, w3 = ws[b + 1], ws[b + 2], ws[b + 3], ws[b + 4]
|
||||
local w4, w5, w6, w7 = ws[b + 5], ws[b + 6], ws[b + 7], ws[b + 8]
|
||||
local o = (out + r * stride) * 2
|
||||
if w1 == 0 and w2 == 0 and w3 == 0 and w4 == 0 and w5 == 0 and w6 == 0 and w7 == 0 then
|
||||
local v = clamp((w0 + hd) // sd, 0, maxv)
|
||||
for i = 0, 14, 2 do
|
||||
writeu16(plane, o + i, v)
|
||||
end
|
||||
if w1 == 0 and w2 == 0 and w3 == 0 then
|
||||
local v = clamp((w0 + hd) // sd, 0, maxv) * 65537
|
||||
writeu32(plane, o, v)
|
||||
writeu32(plane, o + 4, v)
|
||||
writeu32(plane, o + 8, v)
|
||||
writeu32(plane, o + 12, v)
|
||||
else
|
||||
local z1 = (w2 + w6) * 4433
|
||||
local tmp2 = z1 - w6 * 15137
|
||||
local tmp3 = z1 + w2 * 6270
|
||||
local tmp0 = (w0 + w4) * 8192
|
||||
local tmp1 = (w0 - w4) * 8192
|
||||
local tmp2 = w2 * 4433
|
||||
local tmp3 = tmp2 + w2 * 6270
|
||||
local tmp0 = w0 * 8192 + h2
|
||||
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
|
||||
local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2
|
||||
local t0, t1, t2, t3 = w7, w5, w3, w1
|
||||
z1 = t0 + t3
|
||||
local z2 = t1 + t2
|
||||
local z3 = t0 + t2
|
||||
local z4 = t1 + t3
|
||||
local z5 = (z3 + z4) * 9633
|
||||
t0 *= 2446
|
||||
t1 *= 16819
|
||||
t2 *= 25172
|
||||
t3 *= 12299
|
||||
z1 *= -7373
|
||||
z2 *= -20995
|
||||
z3 = z3 * -16069 + z5
|
||||
z4 = z4 * -3196 + z5
|
||||
t0 += z1 + z3
|
||||
t1 += z2 + z4
|
||||
t2 += z2 + z3
|
||||
t3 += z1 + z4
|
||||
writeu16(plane, o, clamp((tmp10 + t3 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 14, clamp((tmp10 - t3 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 2, clamp((tmp11 + t2 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 12, clamp((tmp11 - t2 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 4, clamp((tmp12 + t1 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 10, clamp((tmp12 - t1 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 6, clamp((tmp13 + t0 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 8, clamp((tmp13 - t0 + h2) // s2, 0, maxv))
|
||||
local tmp11, tmp12 = tmp0 + tmp2, tmp0 - tmp2
|
||||
local z5 = (w3 + w1) * 9633
|
||||
local z1 = w1 * -7373
|
||||
local z2 = w3 * -20995
|
||||
local z3 = w3 * -16069 + z5
|
||||
local z4 = w1 * -3196 + z5
|
||||
local t0 = z1 + z3
|
||||
local t1 = z2 + z4
|
||||
local t2 = w3 * 25172 + z2 + z3
|
||||
local t3 = w1 * 12299 + z1 + z4
|
||||
writeu32(plane, o, clamp((tmp10 + t3) // s2, 0, maxv) + clamp((tmp11 + t2) // s2, 0, maxv) * 65536)
|
||||
writeu32(plane, o + 4, clamp((tmp12 + t1) // s2, 0, maxv) + clamp((tmp13 + t0) // s2, 0, maxv) * 65536)
|
||||
writeu32(plane, o + 8, clamp((tmp13 - t0) // s2, 0, maxv) + clamp((tmp12 - t1) // s2, 0, maxv) * 65536)
|
||||
writeu32(plane, o + 12, clamp((tmp11 - t2) // s2, 0, maxv) + clamp((tmp10 - t3) // s2, 0, maxv) * 65536)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- a block with only its DC coefficient: one value (the column and row passes' DC shortcuts)
|
||||
local function idct8dc(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
local sd = 2 ^ (p1 + 3)
|
||||
local v = clamp((readi16(coef, off) * qt[1] * 2 ^ p1 + sd / 2 + center * sd) // sd, 0, maxv) * 65537
|
||||
local o = out * 2
|
||||
local rs = stride * 2
|
||||
for _ = 0, 7 do
|
||||
writeu32(plane, o, v)
|
||||
writeu32(plane, o + 4, v)
|
||||
writeu32(plane, o + 8, v)
|
||||
writeu32(plane, o + 12, v)
|
||||
o += rs
|
||||
end
|
||||
end
|
||||
|
||||
local function idct4(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
local s1 = 2 ^ (14 - p1)
|
||||
local h1 = s1 / 2
|
||||
@@ -236,17 +332,36 @@ local function idct2(coef: buffer, off: number, q: buffer, plane: buffer, out: n
|
||||
end
|
||||
|
||||
-- p1 = PASS1_BITS (2 for 8-bit, 1 for 12-bit)
|
||||
function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number, hook: (() -> ())?)
|
||||
-- nz (optional): u8 per block, the highest zigzag index that may be nonzero
|
||||
function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number, hook: (() -> ())?, nz: buffer?)
|
||||
local stride = bpl * size
|
||||
local qt = dequant(q)
|
||||
for by = 0, bpc - 1 do
|
||||
if hook and by > 0 then
|
||||
pcall(hook)
|
||||
end
|
||||
if size == 8 and nz then
|
||||
local bi = by * bpl
|
||||
local out = by * 8 * stride
|
||||
for off = bi * 128, (bi + bpl - 1) * 128, 128 do
|
||||
local k = readu8(nz, bi)
|
||||
if k == 0 then
|
||||
idct8dc(coef, off, qt, plane, out, stride, p1, center, maxv)
|
||||
elseif k <= 9 then
|
||||
idct8x4(coef, off, qt, plane, out, stride, p1, center, maxv)
|
||||
else
|
||||
idct8(coef, off, qt, plane, out, stride, p1, center, maxv)
|
||||
end
|
||||
bi += 1
|
||||
out += 8
|
||||
end
|
||||
continue
|
||||
end
|
||||
for bx = 0, bpl - 1 do
|
||||
local off = (by * bpl + bx) * 128
|
||||
local out = by * size * stride + bx * size
|
||||
if size == 8 then
|
||||
idct8(coef, off, q, plane, out, stride, p1, center, maxv)
|
||||
idct8(coef, off, qt, plane, out, stride, p1, center, maxv)
|
||||
elseif size == 4 then
|
||||
idct4(coef, off, q, plane, out, stride, p1, center, maxv)
|
||||
elseif size == 2 then
|
||||
@@ -260,7 +375,7 @@ function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, si
|
||||
end
|
||||
|
||||
function Dct.idctBlock(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
idct8(coef, off, q, plane, out, stride, p1, center, maxv)
|
||||
idct8(coef, off, dequant(q), plane, out, stride, p1, center, maxv)
|
||||
end
|
||||
|
||||
local fw = table.create(64, 0)
|
||||
|
||||
@@ -142,7 +142,130 @@ local function fetch(c: Comp, y: number, outW: number): (buffer, number)
|
||||
end
|
||||
end
|
||||
|
||||
-- 8-bit YCbCr with luma at full size and both chroma planes FULL / H2V1 / H2V2: upsampling fused into the conversion.
|
||||
-- jdcolor.c's tables folded into one floor each: r = y + (fix(1.402) * cr + KR) >> 16, b alike, and
|
||||
-- g = y + (-fix(0.34414) * cb - fix(0.71414) * cr + KG) >> 16.
|
||||
local FIX_R, FIX_B, FIX_GB, FIX_GR = 91881, 116130, 22554, 46802
|
||||
local KR = 32768 - FIX_R * 128
|
||||
local KB = 32768 - FIX_B * 128
|
||||
local KG = 32768 + (FIX_GB + FIX_GR) * 128
|
||||
local KRo, KGo, KBo = KR + 256 * 65536, KG + 256 * 65536, KB + 256 * 65536
|
||||
local LR, LG, LB = table.create(1024, 0), table.create(1024, 0), table.create(1024, 0)
|
||||
for i = 1, 1024 do
|
||||
local v = math.clamp(i - 256, 0, 255)
|
||||
LR[i] = v + 0xFF000000
|
||||
LG[i] = v * 256
|
||||
LB[i] = v * 65536
|
||||
end
|
||||
|
||||
-- chroma of output column x of an H2V1 / H2V2 row (H2V1: b1 == b0, biases 4 / 8; H2V2: 8 / 7)
|
||||
local function chromaAt(p: buffer, b0: number, b1: number, w: number, x: number, ba: number, bb: number): number
|
||||
local j = x // 2
|
||||
local this = readu16(p, b0 + j * 2) * 3 + readu16(p, b1 + j * 2)
|
||||
if x % 2 == 0 then
|
||||
if j == 0 then
|
||||
return (this * 4 + 8) // 16
|
||||
end
|
||||
return (this * 3 + readu16(p, b0 + j * 2 - 2) * 3 + readu16(p, b1 + j * 2 - 2) + ba) // 16
|
||||
end
|
||||
if j == w - 1 then
|
||||
return (this * 4 + 7) // 16
|
||||
end
|
||||
return (this * 3 + readu16(p, b0 + j * 2 + 2) * 3 + readu16(p, b1 + j * 2 + 2) + bb) // 16
|
||||
end
|
||||
|
||||
local function ycc8Pixel(out: buffer, o: number, yv: number, cb: number, cr: number)
|
||||
local r = clamp(yv + (FIX_R * cr + KR) // 65536, 0, 255)
|
||||
local g = clamp(yv + (KG - FIX_GB * cb - FIX_GR * cr) // 65536, 0, 255)
|
||||
local b = clamp(yv + (FIX_B * cb + KB) // 65536, 0, 255)
|
||||
writeu32(out, o, 0xFF000000 + r + g * 256 + b * 65536)
|
||||
end
|
||||
|
||||
local function ycc8(P: Params): boolean
|
||||
local comps = P.comps
|
||||
if #comps ~= 3 then
|
||||
return false
|
||||
end
|
||||
local cy, ccb, ccr = comps[1], comps[2], comps[3]
|
||||
local method = ccb.method
|
||||
if P.space ~= 1 or P.maxv ~= 255 or P.toByte or cy.method ~= 0 or ccr.method ~= method or ccr.stride ~= ccb.stride or ccr.downW ~= ccb.downW or ccr.downH ~= ccb.downH or ccr.rows ~= ccb.rows then
|
||||
return false
|
||||
end
|
||||
if method ~= 0 and method ~= 2 and method ~= 3 then
|
||||
return false
|
||||
end
|
||||
local outW, outH, out = P.outW, P.outH, P.pixels
|
||||
local py, pcb, pcr = cy.plane, ccb.plane, ccr.plane
|
||||
local ys, cs = cy.stride * 2, ccb.stride * 2
|
||||
local yRows, cRows, w, downH = cy.rows, ccb.rows, ccb.downW, ccb.downH
|
||||
local hook = P.hook
|
||||
local ba, bb = if method == 3 then 8 else 4, if method == 3 then 7 else 8
|
||||
local o = 0
|
||||
for y = 0, outH - 1 do
|
||||
if hook and y > 0 and y % 8 == 0 then
|
||||
pcall(hook)
|
||||
end
|
||||
local yb = min(y, yRows - 1) * ys
|
||||
if method == 0 then
|
||||
local cb0 = min(y, cRows - 1) * cs
|
||||
local d = cb0 - yb
|
||||
for i = yb, yb + outW * 2 - 2, 2 do
|
||||
local yv, cb, cr = readu16(py, i), readu16(pcb, i + d), readu16(pcr, i + d)
|
||||
local r = clamp(yv + (FIX_R * cr + KR) // 65536, 0, 255)
|
||||
local g = clamp(yv + (KG - FIX_GB * cb - FIX_GR * cr) // 65536, 0, 255)
|
||||
local b = clamp(yv + (FIX_B * cb + KB) // 65536, 0, 255)
|
||||
writeu32(out, o, 0xFF000000 + r + g * 256 + b * 65536)
|
||||
o += 4
|
||||
end
|
||||
continue
|
||||
end
|
||||
local b0, b1
|
||||
if method == 3 then
|
||||
local iy = min(y // 2, downH - 1)
|
||||
local oy = if y % 2 == 0 then max(iy - 1, 0) else min(iy + 1, downH - 1)
|
||||
b0, b1 = iy * cs, oy * cs
|
||||
else
|
||||
b0 = min(y, cRows - 1) * cs
|
||||
b1 = b0
|
||||
end
|
||||
-- pixels 0 and 1, then pairs (2i, 2i + 1) for i = 1 .. iEnd, then the rest
|
||||
local iEnd = min(w - 2, (outW - 2) // 2)
|
||||
for x = 0, min(outW, 2) - 1 do
|
||||
ycc8Pixel(out, o + x * 4, readu16(py, yb + x * 2), chromaAt(pcb, b0, b1, w, x, ba, bb), chromaAt(pcr, b0, b1, w, x, ba, bb))
|
||||
end
|
||||
local cbL = readu16(pcb, b0) * 3 + readu16(pcb, b1)
|
||||
local crL = readu16(pcr, b0) * 3 + readu16(pcr, b1)
|
||||
local cbT = readu16(pcb, b0 + 2) * 3 + readu16(pcb, b1 + 2)
|
||||
local crT = readu16(pcr, b0 + 2) * 3 + readu16(pcr, b1 + 2)
|
||||
local po = o + 8
|
||||
local yo = yb + 4
|
||||
local d = b1 - b0
|
||||
for p = b0 + 4, b0 + iEnd * 2 + 2, 2 do
|
||||
local cbN = readu16(pcb, p) * 3 + readu16(pcb, p + d)
|
||||
local crN = readu16(pcr, p) * 3 + readu16(pcr, p + d)
|
||||
local cb3, cr3 = cbT * 3, crT * 3
|
||||
local cb, cr = (cb3 + cbL + ba) // 16, (cr3 + crL + ba) // 16
|
||||
local yv = readu16(py, yo)
|
||||
writeu32(out, po, LR[yv + (FIX_R * cr + KRo) // 65536] + LG[yv + (KGo - FIX_GB * cb - FIX_GR * cr) // 65536] + LB[yv + (FIX_B * cb + KBo) // 65536])
|
||||
cb, cr = (cb3 + cbN + bb) // 16, (cr3 + crN + bb) // 16
|
||||
yv = readu16(py, yo + 2)
|
||||
writeu32(out, po + 4, LR[yv + (FIX_R * cr + KRo) // 65536] + LG[yv + (KGo - FIX_GB * cb - FIX_GR * cr) // 65536] + LB[yv + (FIX_B * cb + KBo) // 65536])
|
||||
cbL, cbT, crL, crT = cbT, cbN, crT, crN
|
||||
po += 8
|
||||
yo += 4
|
||||
end
|
||||
for x = max(iEnd * 2 + 2, 2), outW - 1 do
|
||||
ycc8Pixel(out, o + x * 4, readu16(py, yb + x * 2), chromaAt(pcb, b0, b1, w, x, ba, bb), chromaAt(pcr, b0, b1, w, x, ba, bb))
|
||||
end
|
||||
o += outW * 4
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function Color.run(P: Params)
|
||||
if ycc8(P) then
|
||||
return
|
||||
end
|
||||
local comps = P.comps
|
||||
local nc = #comps
|
||||
local space, outW, outH, maxv, out = P.space, P.outW, P.outH, P.maxv, P.pixels
|
||||
@@ -177,7 +300,10 @@ function Color.run(P: Params)
|
||||
local c0, c1, c2 = readu16(s0, b0 + i), readu16(s1, b1 + i), readu16(s2, b2 + i)
|
||||
local c3 = if nc >= 4 then readu16(s3, b3 + i) else maxv
|
||||
if toByte then
|
||||
c0, c1, c2, c3 = readu8(toByte, c0), readu8(toByte, c1), readu8(toByte, c2), readu8(toByte, c3)
|
||||
c0 = readu8(toByte, c0)
|
||||
c1 = readu8(toByte, c1)
|
||||
c2 = readu8(toByte, c2)
|
||||
c3 = readu8(toByte, c3)
|
||||
end
|
||||
writeu32(out, o, c0 + c1 * 256 + c2 * 65536 + c3 * 16777216)
|
||||
o += 4
|
||||
@@ -223,7 +349,9 @@ function Color.run(P: Params)
|
||||
end
|
||||
end
|
||||
if toByte then
|
||||
r, g, b = readu8(toByte, r), readu8(toByte, g), readu8(toByte, b)
|
||||
r = readu8(toByte, r)
|
||||
g = readu8(toByte, g)
|
||||
b = readu8(toByte, b)
|
||||
end
|
||||
writeu32(out, o, 0xFF000000 + r + g * 256 + b * 65536)
|
||||
o += 4
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user