merge ws/imgA: jpeg gif tiff tga qoi pnm ico hdr, codecs Dct/Lzw/Quantize, ops/ImageOps, api image hooks
This commit is contained in:
commit
2db77a7db8
55 files changed
+13401
-2
No files matched your search
@@ -0,0 +1,392 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Dct: the 8x8 DCTs JPEG, Motion-JPEG and MPEG-1 share (native; never throws).
|
||||
-- Inverse: libjpeg's islow (jidctint.c: the Loeffler-Ligtenberg-Moschytz algorithm, 13-bit constants, two passes with
|
||||
-- PASS1_BITS of scaling between them), plus the reduced 4x4 / 2x2 / 1x1 IDCTs for scaled decodes (jidctred.c); the
|
||||
-- results match libjpeg-turbo's djpeg bit for bit. Absorbed from jpeg-luau 1.0 src/Jpeg/Idct.luau (same author).
|
||||
-- Forward: libjpeg's islow (jfdctint.c) with jcdctmgr.c's rounding quantiser, so an encoder built on it produces
|
||||
-- the same coefficients as cjpeg. Both are Luau translations of IJG code (licenses/libjpeg-turbo-LICENSE.md).
|
||||
-- Values are exact integers in doubles: no overflow even on corrupt coefficients; outputs are clamped to 0..maxv.
|
||||
--
|
||||
-- Dct.idctComponent(coef, q, bpl, bpc, size, plane, p1, center, maxv) -- every block of a component
|
||||
-- Dct.idctBlock(coef, off, q, plane, out, stride, p1, center, maxv) -- one 8x8 block (u16 samples)
|
||||
-- Dct.fdctQuantize(src, stride, x, y, w, h, div, coef, off) -- one block from u8 samples
|
||||
local readi16, readu16, writeu16, readu8, writei16 = buffer.readi16, buffer.readu16, buffer.writeu16, 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 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, 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
|
||||
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
|
||||
-- even part
|
||||
local z2 = c2 * readu16(q, qo + 32)
|
||||
local z3 = c6 * readu16(q, qo + 96)
|
||||
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
|
||||
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)
|
||||
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
|
||||
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 = 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
|
||||
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 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))
|
||||
end
|
||||
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
|
||||
local dcm = 2 ^ p1
|
||||
for c = 0, 7 do
|
||||
if c ~= 4 then
|
||||
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 c5, c6, c7 = readi16(coef, o + 80), readi16(coef, o + 96), readi16(coef, o + 112)
|
||||
if c1 == 0 and c2 == 0 and c3 == 0 and c5 == 0 and c6 == 0 and c7 == 0 then
|
||||
local dc = readi16(coef, o) * readu16(q, qo) * dcm
|
||||
ws[c + 1] = dc
|
||||
ws[c + 9] = dc
|
||||
ws[c + 17] = dc
|
||||
ws[c + 25] = dc
|
||||
else
|
||||
local tmp0 = readi16(coef, o) * readu16(q, qo) * 16384
|
||||
local tmp2 = c2 * readu16(q, qo + 32) * 15137 - c6 * readu16(q, qo + 96) * 6270
|
||||
local tmp10, tmp12 = tmp0 + tmp2, tmp0 - tmp2
|
||||
local z1 = c7 * readu16(q, qo + 112)
|
||||
local z2 = c5 * readu16(q, qo + 80)
|
||||
local z3 = c3 * readu16(q, qo + 48)
|
||||
local z4 = c1 * readu16(q, qo + 16)
|
||||
tmp0 = z1 * -1730 + z2 * 11893 + z3 * -17799 + z4 * 8697
|
||||
tmp2 = z1 * -4176 + z2 * -4926 + z3 * 7373 + z4 * 20995
|
||||
ws[c + 1] = (tmp10 + tmp2 + h1) // s1
|
||||
ws[c + 25] = (tmp10 - tmp2 + h1) // s1
|
||||
ws[c + 9] = (tmp12 + tmp0 + h1) // s1
|
||||
ws[c + 17] = (tmp12 - tmp0 + h1) // s1
|
||||
end
|
||||
end
|
||||
end
|
||||
local s2 = 2 ^ (17 + p1)
|
||||
local h2 = s2 / 2 + center * s2
|
||||
local sd = 2 ^ (p1 + 3)
|
||||
local hd = sd / 2 + center * sd
|
||||
for r = 0, 3 do
|
||||
local b = r * 8
|
||||
local w0, w1, w2, w3 = ws[b + 1], ws[b + 2], ws[b + 3], ws[b + 4]
|
||||
local w5, w6, w7 = 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 w5 == 0 and w6 == 0 and w7 == 0 then
|
||||
local v = clamp((w0 + hd) // sd, 0, maxv)
|
||||
writeu16(plane, o, v)
|
||||
writeu16(plane, o + 2, v)
|
||||
writeu16(plane, o + 4, v)
|
||||
writeu16(plane, o + 6, v)
|
||||
else
|
||||
local tmp0 = w0 * 16384
|
||||
local tmp2 = w2 * 15137 - w6 * 6270
|
||||
local tmp10, tmp12 = tmp0 + tmp2, tmp0 - tmp2
|
||||
tmp0 = w7 * -1730 + w5 * 11893 + w3 * -17799 + w1 * 8697
|
||||
tmp2 = w7 * -4176 + w5 * -4926 + w3 * 7373 + w1 * 20995
|
||||
writeu16(plane, o, clamp((tmp10 + tmp2 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 6, clamp((tmp10 - tmp2 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 2, clamp((tmp12 + tmp0 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 4, clamp((tmp12 - tmp0 + h2) // s2, 0, maxv))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function idct2(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
local s1 = 2 ^ (15 - p1)
|
||||
local h1 = s1 / 2
|
||||
local dcm = 2 ^ p1
|
||||
for c = 0, 7 do
|
||||
if c ~= 2 and c ~= 4 and c ~= 6 then
|
||||
local o = off + c * 2
|
||||
local qo = c * 2
|
||||
local c1, c3 = readi16(coef, o + 16), readi16(coef, o + 48)
|
||||
local c5, c7 = readi16(coef, o + 80), readi16(coef, o + 112)
|
||||
if c1 == 0 and c3 == 0 and c5 == 0 and c7 == 0 then
|
||||
local dc = readi16(coef, o) * readu16(q, qo) * dcm
|
||||
ws[c + 1] = dc
|
||||
ws[c + 9] = dc
|
||||
else
|
||||
local tmp10 = readi16(coef, o) * readu16(q, qo) * 32768
|
||||
local tmp0 = c7 * readu16(q, qo + 112) * -5906
|
||||
+ c5 * readu16(q, qo + 80) * 6967
|
||||
+ c3 * readu16(q, qo + 48) * -10426
|
||||
+ c1 * readu16(q, qo + 16) * 29692
|
||||
ws[c + 1] = (tmp10 + tmp0 + h1) // s1
|
||||
ws[c + 9] = (tmp10 - tmp0 + h1) // s1
|
||||
end
|
||||
end
|
||||
end
|
||||
local s2 = 2 ^ (18 + p1)
|
||||
local h2 = s2 / 2 + center * s2
|
||||
local sd = 2 ^ (p1 + 3)
|
||||
local hd = sd / 2 + center * sd
|
||||
for r = 0, 1 do
|
||||
local b = r * 8
|
||||
local w0, w1, w3, w5, w7 = ws[b + 1], ws[b + 2], ws[b + 4], ws[b + 6], ws[b + 8]
|
||||
local o = (out + r * stride) * 2
|
||||
if w1 == 0 and w3 == 0 and w5 == 0 and w7 == 0 then
|
||||
local v = clamp((w0 + hd) // sd, 0, maxv)
|
||||
writeu16(plane, o, v)
|
||||
writeu16(plane, o + 2, v)
|
||||
else
|
||||
local tmp10 = w0 * 32768
|
||||
local tmp0 = w7 * -5906 + w5 * 6967 + w3 * -10426 + w1 * 29692
|
||||
writeu16(plane, o, clamp((tmp10 + tmp0 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 2, clamp((tmp10 - tmp0 + h2) // s2, 0, maxv))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Inverse-transforms every block of a component: coef (i16, 64 per block, natural order) on a bpl x bpc block
|
||||
-- grid, quantisation table q (64 u16, natural order), into plane (u16 samples, (bpl * size) per row) at size x
|
||||
-- size samples per block. p1 is PASS1_BITS (2 for 8-bit, 1 for 12-bit), center the level shift, maxv the top.
|
||||
function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number)
|
||||
local stride = bpl * size
|
||||
for by = 0, bpc - 1 do
|
||||
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)
|
||||
elseif size == 4 then
|
||||
idct4(coef, off, q, plane, out, stride, p1, center, maxv)
|
||||
elseif size == 2 then
|
||||
idct2(coef, off, q, plane, out, stride, p1, center, maxv)
|
||||
else
|
||||
local v = (readi16(coef, off) * readu16(q, 0) + 4) // 8 + center
|
||||
writeu16(plane, out * 2, clamp(v, 0, maxv))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- idctBlock(coef, off, q, plane, out, stride, p1, center, maxv): one 8x8 block, coef (i16, natural order) at byte
|
||||
-- offset off, dequantised by q (64 u16), into the u16 plane at sample index out with stride samples per row
|
||||
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)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ forward
|
||||
local fw = table.create(64, 0)
|
||||
|
||||
local function quant(coef: buffer, off: number, div: buffer, v: number, i: number)
|
||||
local qv = readu16(div, i * 2) * 8
|
||||
local h = qv // 2
|
||||
if v < 0 then
|
||||
writei16(coef, off + i * 2, -((h - v) // qv))
|
||||
else
|
||||
writei16(coef, off + i * 2, (v + h) // qv)
|
||||
end
|
||||
end
|
||||
|
||||
-- fdctQuantize(src, stride, x, y, w, h, div, coef, off): the 8x8 block of u8 samples at (x, y) in src (stride bytes
|
||||
-- per row; samples past w - 1 / h - 1 repeat the edge), level-shifted by 128, forward DCT (jfdctint.c islow), then
|
||||
-- quantised as jcdctmgr.c: (|v| + q * 4) // (q * 8) with the sign restored, q = div (64 u16, natural order). Writes
|
||||
-- 64 i16 at coef[off] (natural order). The caller keeps x < w, y < h and the src range valid.
|
||||
function Dct.fdctQuantize(src: buffer, stride: number, x: number, y: number, w: number, h: number, div: buffer, coef: buffer, off: number)
|
||||
local lastX, lastY = w - 1, h - 1
|
||||
for r = 0, 7 do
|
||||
local row = math.min(y + r, lastY) * stride
|
||||
local d0: number, d1: number, d2: number, d3: number, d4: number, d5: number, d6: number, d7: number
|
||||
if x + 7 <= lastX then
|
||||
local p = row + x
|
||||
d0, d1, d2, d3 = readu8(src, p), readu8(src, p + 1), readu8(src, p + 2), readu8(src, p + 3)
|
||||
d4, d5, d6, d7 = readu8(src, p + 4), readu8(src, p + 5), readu8(src, p + 6), readu8(src, p + 7)
|
||||
else
|
||||
d0 = readu8(src, row + math.min(x, lastX))
|
||||
d1 = readu8(src, row + math.min(x + 1, lastX))
|
||||
d2 = readu8(src, row + math.min(x + 2, lastX))
|
||||
d3 = readu8(src, row + math.min(x + 3, lastX))
|
||||
d4 = readu8(src, row + math.min(x + 4, lastX))
|
||||
d5 = readu8(src, row + math.min(x + 5, lastX))
|
||||
d6 = readu8(src, row + math.min(x + 6, lastX))
|
||||
d7 = readu8(src, row + math.min(x + 7, lastX))
|
||||
end
|
||||
d0 -= 128
|
||||
d1 -= 128
|
||||
d2 -= 128
|
||||
d3 -= 128
|
||||
d4 -= 128
|
||||
d5 -= 128
|
||||
d6 -= 128
|
||||
d7 -= 128
|
||||
local tmp0, tmp7 = d0 + d7, d0 - d7
|
||||
local tmp1, tmp6 = d1 + d6, d1 - d6
|
||||
local tmp2, tmp5 = d2 + d5, d2 - d5
|
||||
local tmp3, tmp4 = d3 + d4, d3 - d4
|
||||
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
|
||||
local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2
|
||||
local b = r * 8 + 1
|
||||
fw[b] = (tmp10 + tmp11) * 4
|
||||
fw[b + 4] = (tmp10 - tmp11) * 4
|
||||
local z1 = (tmp12 + tmp13) * 4433
|
||||
fw[b + 2] = (z1 + tmp13 * 6270 + 1024) // 2048
|
||||
fw[b + 6] = (z1 - tmp12 * 15137 + 1024) // 2048
|
||||
z1 = tmp4 + tmp7
|
||||
local z2 = tmp5 + tmp6
|
||||
local z3 = tmp4 + tmp6
|
||||
local z4 = tmp5 + tmp7
|
||||
local z5 = (z3 + z4) * 9633
|
||||
tmp4 *= 2446
|
||||
tmp5 *= 16819
|
||||
tmp6 *= 25172
|
||||
tmp7 *= 12299
|
||||
z1 *= -7373
|
||||
z2 *= -20995
|
||||
z3 = z3 * -16069 + z5
|
||||
z4 = z4 * -3196 + z5
|
||||
fw[b + 7] = (tmp4 + z1 + z3 + 1024) // 2048
|
||||
fw[b + 5] = (tmp5 + z2 + z4 + 1024) // 2048
|
||||
fw[b + 3] = (tmp6 + z2 + z3 + 1024) // 2048
|
||||
fw[b + 1] = (tmp7 + z1 + z4 + 1024) // 2048
|
||||
end
|
||||
for c = 1, 8 do
|
||||
local d0, d1, d2, d3 = fw[c], fw[c + 8], fw[c + 16], fw[c + 24]
|
||||
local d4, d5, d6, d7 = fw[c + 32], fw[c + 40], fw[c + 48], fw[c + 56]
|
||||
local tmp0, tmp7 = d0 + d7, d0 - d7
|
||||
local tmp1, tmp6 = d1 + d6, d1 - d6
|
||||
local tmp2, tmp5 = d2 + d5, d2 - d5
|
||||
local tmp3, tmp4 = d3 + d4, d3 - d4
|
||||
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
|
||||
local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2
|
||||
local o0 = (tmp10 + tmp11 + 2) // 4
|
||||
local o4 = (tmp10 - tmp11 + 2) // 4
|
||||
local z1 = (tmp12 + tmp13) * 4433
|
||||
local o2 = (z1 + tmp13 * 6270 + 16384) // 32768
|
||||
local o6 = (z1 - tmp12 * 15137 + 16384) // 32768
|
||||
z1 = tmp4 + tmp7
|
||||
local z2 = tmp5 + tmp6
|
||||
local z3 = tmp4 + tmp6
|
||||
local z4 = tmp5 + tmp7
|
||||
local z5 = (z3 + z4) * 9633
|
||||
tmp4 *= 2446
|
||||
tmp5 *= 16819
|
||||
tmp6 *= 25172
|
||||
tmp7 *= 12299
|
||||
z1 *= -7373
|
||||
z2 *= -20995
|
||||
z3 = z3 * -16069 + z5
|
||||
z4 = z4 * -3196 + z5
|
||||
local o7 = (tmp4 + z1 + z3 + 16384) // 32768
|
||||
local o5 = (tmp5 + z2 + z4 + 16384) // 32768
|
||||
local o3 = (tmp6 + z2 + z3 + 16384) // 32768
|
||||
local o1 = (tmp7 + z1 + z4 + 16384) // 32768
|
||||
local cc = c - 1
|
||||
quant(coef, off, div, o0, cc)
|
||||
quant(coef, off, div, o1, cc + 8)
|
||||
quant(coef, off, div, o2, cc + 16)
|
||||
quant(coef, off, div, o3, cc + 24)
|
||||
quant(coef, off, div, o4, cc + 32)
|
||||
quant(coef, off, div, o5, cc + 40)
|
||||
quant(coef, off, div, o6, cc + 48)
|
||||
quant(coef, off, div, o7, cc + 56)
|
||||
end
|
||||
end
|
||||
|
||||
return Dct
|
||||
@@ -0,0 +1,282 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Lzw: LZW decoder and encoder in the two variants file formats use. A codec (layer 1): image/gif and
|
||||
-- image/tiff build on it. New code (no port), written from the GIF89a spec (appendix F) and TIFF 6.0 section 13.
|
||||
-- gif LSB-first codes, a minimum code size 1..11 (2..8 in GIF), clear = 2^min, EOI = clear + 1, the code width
|
||||
-- grows when the next free code reaches 2^width (max 12 bits; a full table stays frozen until a clear).
|
||||
-- tiff MSB-first codes, 8-bit symbols, clear = 256, EOI = 257, "early change": the width grows one code
|
||||
-- earlier (next free code = 2^width - 1). Old-style (pre-6.0, LSB-first, no early change) TIFF LZW is
|
||||
-- detected by its first bytes (0x00 then an odd byte) and decoded too.
|
||||
--
|
||||
-- local n, err = Lzw.decode(src, off, len, out, outOff, maxOut, "gif", minCodeSize) -- bytes written
|
||||
-- local n, err = Lzw.decode(src, off, len, out, outOff, maxOut, "tiff")
|
||||
-- local codes = Lzw.encode(data, off, len, "gif", minCodeSize) -- buffer
|
||||
-- local codes = Lzw.encode(data, off, len, "tiff")
|
||||
--
|
||||
-- decode writes at most maxOut bytes at out[outOff ..] and returns how many it wrote; it stops at EOI, at the end of
|
||||
-- the input or when out is full. err is nil for a clean stream, "truncated" when the input ended without EOI, or
|
||||
-- "corrupt" for an impossible code (everything decoded up to it is kept). Never throws; the caller checks that
|
||||
-- [outOff, outOff + maxOut) lies inside out and [off, off + len) inside src (decode clamps both anyway).
|
||||
local readu8, writeu8, readu16, writeu16 = buffer.readu8, buffer.writeu8, buffer.readu16, buffer.writeu16
|
||||
local band, rshift, lshift = bit32.band, bit32.rshift, bit32.lshift
|
||||
local blen = buffer.len
|
||||
|
||||
local Lzw = {}
|
||||
|
||||
local MAX_CODES = 4096
|
||||
|
||||
-- per-call tables (allocated once: decode / encode are not re-entrant across coroutines, but never yield)
|
||||
local prefix = buffer.create(MAX_CODES * 2) -- u16: the code this entry extends
|
||||
local suffix = buffer.create(MAX_CODES) -- u8: its last byte
|
||||
local first = buffer.create(MAX_CODES) -- u8: its first byte
|
||||
local length = buffer.create(MAX_CODES * 2) -- u16: its length
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- writes the string of code c (length l) ending at out[o + l - 1]; clipped to [o, lim)
|
||||
local function emit(out: buffer, o: number, c: number, l: number, lim: number)
|
||||
local p = o + l - 1
|
||||
if p < lim then
|
||||
while p >= o do
|
||||
writeu8(out, p, readu8(suffix, c))
|
||||
c = readu16(prefix, c * 2)
|
||||
p -= 1
|
||||
end
|
||||
else
|
||||
while p >= o do
|
||||
if p < lim then
|
||||
writeu8(out, p, readu8(suffix, c))
|
||||
end
|
||||
c = readu16(prefix, c * 2)
|
||||
p -= 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Lzw.decode(src: buffer, off: number, len: number, out: buffer, outOff: number, maxOut: number, variant: string, minCodeSize: number?): (number, string?)
|
||||
local n = blen(src)
|
||||
if off < 0 or off > n then
|
||||
return 0, "corrupt"
|
||||
end
|
||||
local e = math.min(off + math.max(0, len), n)
|
||||
local lim = math.min(outOff + math.max(0, maxOut), blen(out))
|
||||
if outOff < 0 then
|
||||
return 0, "corrupt"
|
||||
end
|
||||
local msb = variant == "tiff"
|
||||
local early = 0 -- 1 for TIFF's early change
|
||||
local min = minCodeSize or 8
|
||||
if msb then
|
||||
min = 8
|
||||
early = 1
|
||||
-- old-style TIFF LZW: LSB-first, no early change (libtiff LZWPreDecode's check)
|
||||
if e - off >= 2 and readu8(src, off) == 0 and band(readu8(src, off + 1), 1) == 1 then
|
||||
msb = false
|
||||
early = 0
|
||||
end
|
||||
end
|
||||
if min < 1 or min > 11 then
|
||||
return 0, "corrupt"
|
||||
end
|
||||
local clear = lshift(1, min)
|
||||
local eoi = clear + 1
|
||||
for i = 0, clear - 1 do
|
||||
writeu16(prefix, i * 2, 0)
|
||||
writeu8(suffix, i, band(i, 255))
|
||||
writeu8(first, i, band(i, 255))
|
||||
writeu16(length, i * 2, 1)
|
||||
end
|
||||
local size = min + 1
|
||||
local mask = lshift(1, size) - 1
|
||||
local nextc = clear + 2
|
||||
local prev = -1
|
||||
local o = outOff
|
||||
local p = off
|
||||
local acc, nb = 0, 0
|
||||
while true do
|
||||
-- refill (at most 12 + 7 bits held: below 2^20)
|
||||
while nb < size do
|
||||
if p >= e then
|
||||
return o - outOff, "truncated"
|
||||
end
|
||||
if msb then
|
||||
acc = acc * 256 + readu8(src, p)
|
||||
else
|
||||
acc += lshift(readu8(src, p), nb)
|
||||
end
|
||||
p += 1
|
||||
nb += 8
|
||||
end
|
||||
local c
|
||||
if msb then
|
||||
nb -= size
|
||||
c = band(rshift(acc, nb), mask)
|
||||
acc = band(acc, lshift(1, nb) - 1)
|
||||
else
|
||||
c = band(acc, mask)
|
||||
acc = rshift(acc, size)
|
||||
nb -= size
|
||||
end
|
||||
if c == clear then
|
||||
size = min + 1
|
||||
mask = lshift(1, size) - 1
|
||||
nextc = clear + 2
|
||||
prev = -1
|
||||
elseif c == eoi then
|
||||
return o - outOff, nil
|
||||
else
|
||||
if o >= lim then
|
||||
return o - outOff, nil -- full: the rest (normally just EOI) is ignored
|
||||
end
|
||||
if prev < 0 then
|
||||
if c >= clear then
|
||||
return o - outOff, "corrupt"
|
||||
end
|
||||
writeu8(out, o, readu8(suffix, c))
|
||||
o += 1
|
||||
prev = c
|
||||
else
|
||||
local l
|
||||
if c < nextc then
|
||||
l = readu16(length, c * 2)
|
||||
emit(out, o, c, l, lim)
|
||||
if nextc < MAX_CODES then
|
||||
writeu16(prefix, nextc * 2, prev)
|
||||
writeu8(suffix, nextc, readu8(first, c))
|
||||
writeu8(first, nextc, readu8(first, prev))
|
||||
writeu16(length, nextc * 2, readu16(length, prev * 2) + 1)
|
||||
nextc += 1
|
||||
end
|
||||
elseif c == nextc and nextc < MAX_CODES then
|
||||
-- KwKwK: the new entry is prev + first(prev)
|
||||
local f = readu8(first, prev)
|
||||
writeu16(prefix, nextc * 2, prev)
|
||||
writeu8(suffix, nextc, f)
|
||||
writeu8(first, nextc, f)
|
||||
l = readu16(length, prev * 2) + 1
|
||||
writeu16(length, nextc * 2, l)
|
||||
nextc += 1
|
||||
emit(out, o, c, l, lim)
|
||||
else
|
||||
return o - outOff, "corrupt"
|
||||
end
|
||||
o = math.min(o + l, lim)
|
||||
prev = c
|
||||
end
|
||||
if nextc + early >= lshift(1, size) and size < 12 then
|
||||
size += 1
|
||||
mask = lshift(1, size) - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local HASH = 8192 -- open addressing over (prefix << 8 | byte) keys: a power of two above 4096 entries
|
||||
local hkey = buffer.create(HASH * 4) -- i32 key + 1 (0 = empty)
|
||||
local hval = buffer.create(HASH * 2) -- u16 code
|
||||
|
||||
-- encode(data, off, len, variant, minCodeSize?) -> the code stream (GIF: without the sub-block framing). Symbols in
|
||||
-- data must be below 2^minCodeSize (GIF; larger ones are masked) or bytes (TIFF).
|
||||
function Lzw.encode(data: buffer, off: number, len: number, variant: string, minCodeSize: number?): buffer
|
||||
local msb = variant == "tiff"
|
||||
local min = if msb then 8 else math.clamp(minCodeSize or 8, 1, 11)
|
||||
local early = if msb then 1 else 0
|
||||
local symMask = lshift(1, min) - 1
|
||||
local clear = lshift(1, min)
|
||||
local eoi = clear + 1
|
||||
local n = blen(data)
|
||||
off = math.clamp(off, 0, n)
|
||||
local e = math.clamp(off + len, off, n)
|
||||
local cap = (e - off) * 2 + 64
|
||||
local out = buffer.create(cap)
|
||||
local op = 0
|
||||
local acc, nb = 0, 0
|
||||
local size = min + 1
|
||||
local function put(code: number)
|
||||
if op + 4 > cap then
|
||||
cap *= 2
|
||||
local nbuf = buffer.create(cap)
|
||||
buffer.copy(nbuf, 0, out, 0, op)
|
||||
out = nbuf
|
||||
end
|
||||
if msb then
|
||||
acc = acc * lshift(1, size) + code
|
||||
nb += size
|
||||
while nb >= 8 do
|
||||
nb -= 8
|
||||
writeu8(out, op, rshift(acc, nb))
|
||||
op += 1
|
||||
acc = band(acc, lshift(1, nb) - 1)
|
||||
end
|
||||
else
|
||||
acc += lshift(code, nb)
|
||||
nb += size
|
||||
while nb >= 8 do
|
||||
writeu8(out, op, band(acc, 255))
|
||||
op += 1
|
||||
acc = rshift(acc, 8)
|
||||
nb -= 8
|
||||
end
|
||||
end
|
||||
end
|
||||
buffer.fill(hkey, 0, 0, HASH * 4)
|
||||
local nextc = clear + 2
|
||||
put(clear)
|
||||
if e > off then
|
||||
local cur = band(readu8(data, off), symMask)
|
||||
for i = off + 1, e - 1 do
|
||||
local b = band(readu8(data, i), symMask)
|
||||
local key = cur * 256 + b + 1
|
||||
local h = band(cur * 31 + b * 4099, HASH - 1)
|
||||
local found = -1
|
||||
while true do
|
||||
local k = buffer.readi32(hkey, h * 4)
|
||||
if k == 0 then
|
||||
break
|
||||
elseif k == key then
|
||||
found = readu16(hval, h * 2)
|
||||
break
|
||||
end
|
||||
h = band(h + 1, HASH - 1)
|
||||
end
|
||||
if found >= 0 then
|
||||
cur = found
|
||||
else
|
||||
put(cur)
|
||||
if nextc >= MAX_CODES - 2 then
|
||||
put(clear)
|
||||
size = min + 1
|
||||
nextc = clear + 2
|
||||
buffer.fill(hkey, 0, 0, HASH * 4)
|
||||
else
|
||||
buffer.writei32(hkey, h * 4, key)
|
||||
writeu16(hval, h * 2, nextc)
|
||||
nextc += 1
|
||||
if nextc + early > lshift(1, size) and size < 12 then
|
||||
size += 1
|
||||
end
|
||||
end
|
||||
cur = b
|
||||
end
|
||||
end
|
||||
put(cur)
|
||||
-- the decoder adds an entry for this last code too: its width may grow before EOI
|
||||
if nextc < MAX_CODES then
|
||||
nextc += 1
|
||||
if nextc + early > lshift(1, size) and size < 12 then
|
||||
size += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
put(eoi)
|
||||
if nb > 0 then
|
||||
writeu8(out, op, if msb then band(lshift(acc, 8 - nb), 255) else band(acc, 255))
|
||||
op += 1
|
||||
end
|
||||
local res = buffer.create(op)
|
||||
buffer.copy(res, 0, out, 0, op)
|
||||
return res
|
||||
end
|
||||
|
||||
return Lzw
|
||||
@@ -0,0 +1,483 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/Quantize: palette quantization of rgba8 pixels. A codec (layer 1): image/gif (and later any palette
|
||||
-- writer) builds on it. New code: Heckbert's median cut and Gervautz-Purgathofer octree, both over a 5-bit-per-channel
|
||||
-- weighted histogram (palette colours are the exact means of the pixels they cover), nearest-colour mapping with
|
||||
-- optional Floyd-Steinberg error diffusion. Deterministic: the same palette and indexes on every runtime (no table
|
||||
-- iteration order is ever relied on).
|
||||
--
|
||||
-- local pal, exact = Quantize.palette(pixels, w, h, 256, { method = "mediancut" }) -- { {r, g, b, 255} }, boolean
|
||||
-- local idx = Quantize.map(pixels, w, h, pal, { dither = true, transparentIndex = 255 }) -- buffer of w * h u8
|
||||
--
|
||||
-- Pixels with alpha < 128 are "transparent": palette() ignores them, map() gives them opts.transparentIndex (0-based)
|
||||
-- when set (otherwise they map like opaque pixels). When the opaque pixels have at most maxColors distinct colours,
|
||||
-- palette() returns exactly those (in first-seen order) and exact = true, and map() is then lossless. The octree
|
||||
-- folds whole nodes, so it may return fewer than maxColors entries; below 8 colours median cut is used instead.
|
||||
-- Never throws: bad arguments give an empty palette / a zero-filled index buffer.
|
||||
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
||||
local readu32, writeu32, readf64, writef64 = buffer.readu32, buffer.writeu32, buffer.readf64, buffer.writef64
|
||||
local floor = math.floor
|
||||
local blen = buffer.len
|
||||
|
||||
local Quantize = {}
|
||||
|
||||
export type Color = { number } -- { r, g, b, a } 0..255
|
||||
export type PaletteOptions = {
|
||||
method: ("mediancut" | "octree")?, -- default "mediancut"
|
||||
}
|
||||
export type MapOptions = {
|
||||
dither: boolean?, -- Floyd-Steinberg error diffusion (default false)
|
||||
transparentIndex: number?, -- 0-based index for pixels with alpha < 128 (default: none)
|
||||
}
|
||||
|
||||
local BINS = 32768 -- 5 bits per channel
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ helpers
|
||||
local function count(pixels: buffer, n: number): (buffer, buffer)
|
||||
-- per bin: u32 count; f64 sums of r, g, b
|
||||
local cnt = buffer.create(BINS * 4)
|
||||
local sums = buffer.create(BINS * 24)
|
||||
for i = 0, n - 1 do
|
||||
local o = i * 4
|
||||
if readu8(pixels, o + 3) >= 128 then
|
||||
local r, g, b = readu8(pixels, o), readu8(pixels, o + 1), readu8(pixels, o + 2)
|
||||
local bin = (r // 8) * 1024 + (g // 8) * 32 + b // 8
|
||||
writeu32(cnt, bin * 4, readu32(cnt, bin * 4) + 1)
|
||||
local s = bin * 24
|
||||
writef64(sums, s, readf64(sums, s) + r)
|
||||
writef64(sums, s + 8, readf64(sums, s + 8) + g)
|
||||
writef64(sums, s + 16, readf64(sums, s + 16) + b)
|
||||
end
|
||||
end
|
||||
return cnt, sums
|
||||
end
|
||||
|
||||
local function mean(cnt: buffer, sums: buffer, bins: { number }, lo: number, hi: number): Color
|
||||
local n, r, g, b = 0, 0, 0, 0
|
||||
for i = lo, hi do
|
||||
local bin = bins[i]
|
||||
n += readu32(cnt, bin * 4)
|
||||
r += readf64(sums, bin * 24)
|
||||
g += readf64(sums, bin * 24 + 8)
|
||||
b += readf64(sums, bin * 24 + 16)
|
||||
end
|
||||
if n == 0 then
|
||||
return { 0, 0, 0, 255 }
|
||||
end
|
||||
return { floor(r / n + 0.5), floor(g / n + 0.5), floor(b / n + 0.5), 255 }
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ median cut
|
||||
local function medianCut(cnt: buffer, sums: buffer, maxColors: number): { Color }
|
||||
local bins: { number } = {}
|
||||
for bin = 0, BINS - 1 do
|
||||
if readu32(cnt, bin * 4) > 0 then
|
||||
table.insert(bins, bin)
|
||||
end
|
||||
end
|
||||
if #bins == 0 then
|
||||
return {}
|
||||
end
|
||||
-- boxes: { lo, hi } ranges of the bins array
|
||||
local boxes: { { number } } = {}
|
||||
local function channel(bin: number, c: number): number
|
||||
if c == 0 then
|
||||
return bin // 1024
|
||||
elseif c == 1 then
|
||||
return (bin // 32) % 32
|
||||
end
|
||||
return bin % 32
|
||||
end
|
||||
local function stats(box: { number }): (number, number, number)
|
||||
-- returns the widest channel, its range, the pixel count
|
||||
local mins, maxs = { 31, 31, 31 }, { 0, 0, 0 }
|
||||
local n = 0
|
||||
for i = box[1], box[2] do
|
||||
local bin = bins[i]
|
||||
n += readu32(cnt, bin * 4)
|
||||
for c = 0, 2 do
|
||||
local v = channel(bin, c)
|
||||
if v < mins[c + 1] then
|
||||
mins[c + 1] = v
|
||||
end
|
||||
if v > maxs[c + 1] then
|
||||
maxs[c + 1] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
local best, range = 0, -1
|
||||
for c = 0, 2 do
|
||||
local rg = maxs[c + 1] - mins[c + 1]
|
||||
if rg > range then
|
||||
best, range = c, rg
|
||||
end
|
||||
end
|
||||
return best, range, n
|
||||
end
|
||||
local function scored(lo: number, hi: number): { number }
|
||||
local c, range, n = stats({ lo, hi })
|
||||
return { lo, hi, c, if hi > lo then range * math.sqrt(n) else 0 }
|
||||
end
|
||||
boxes[1] = scored(1, #bins)
|
||||
while #boxes < maxColors do
|
||||
-- split the box with the largest range * sqrt(count) score (ties: the first)
|
||||
local pick, pickScore = 0, 0
|
||||
for i, box in boxes do
|
||||
if box[4] > pickScore then
|
||||
pick, pickScore = i, box[4]
|
||||
end
|
||||
end
|
||||
if pick == 0 then
|
||||
break
|
||||
end
|
||||
local box = boxes[pick]
|
||||
local lo, hi, c = box[1], box[2], box[3]
|
||||
-- sort the range by the channel, ties by bin number: unique numeric keys, no comparator
|
||||
local keys = table.create(hi - lo + 1)
|
||||
for i = lo, hi do
|
||||
keys[i - lo + 1] = channel(bins[i], c) * BINS + bins[i]
|
||||
end
|
||||
table.sort(keys)
|
||||
local total = 0
|
||||
for i = 1, #keys do
|
||||
local bin = keys[i] % BINS
|
||||
bins[lo + i - 1] = bin
|
||||
total += readu32(cnt, bin * 4)
|
||||
end
|
||||
-- the weighted median, keeping both halves non-empty
|
||||
local acc, cut = 0, lo
|
||||
for i = lo, hi - 1 do
|
||||
acc += readu32(cnt, bins[i] * 4)
|
||||
cut = i
|
||||
if acc * 2 >= total then
|
||||
break
|
||||
end
|
||||
end
|
||||
boxes[pick] = scored(lo, cut)
|
||||
table.insert(boxes, scored(cut + 1, hi))
|
||||
end
|
||||
local pal = table.create(#boxes)
|
||||
for i, box in boxes do
|
||||
pal[i] = mean(cnt, sums, bins, box[1], box[2])
|
||||
end
|
||||
return pal
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ octree
|
||||
-- over the histogram bins: 5 levels (one bit of each channel per level). Nodes are array slots.
|
||||
local function octree(cnt: buffer, sums: buffer, maxColors: number): { Color }
|
||||
local child: { number } = {} -- node * 8 + k -> node (0 = none)
|
||||
local n: { number } = {} -- pixel count
|
||||
local sr: { number }, sg: { number }, sb: { number } = {}, {}, {}
|
||||
local leaf: { boolean } = {}
|
||||
local level: { number } = {}
|
||||
local nodes = 1
|
||||
n[1], sr[1], sg[1], sb[1], leaf[1], level[1] = 0, 0, 0, 0, false, 0
|
||||
for k = 1, 8 do
|
||||
child[k] = 0
|
||||
end
|
||||
local byLevel: { { number } } = { {}, {}, {}, {}, {} } -- inner nodes per level 0..4, creation order
|
||||
table.insert(byLevel[1], 1)
|
||||
local leaves = 0
|
||||
for bin = 0, BINS - 1 do
|
||||
local c = readu32(cnt, bin * 4)
|
||||
if c > 0 then
|
||||
local r5, g5, b5 = bin // 1024, (bin // 32) % 32, bin % 32
|
||||
local node = 1
|
||||
for lv = 0, 4 do
|
||||
local sh = 4 - lv
|
||||
local k = ((r5 // 2 ^ sh) % 2) * 4 + ((g5 // 2 ^ sh) % 2) * 2 + (b5 // 2 ^ sh) % 2
|
||||
local slot = (node - 1) * 8 + k + 1
|
||||
local nx = child[slot]
|
||||
if nx == 0 then
|
||||
nodes += 1
|
||||
nx = nodes
|
||||
child[slot] = nx
|
||||
for j = 1, 8 do
|
||||
child[(nx - 1) * 8 + j] = 0
|
||||
end
|
||||
n[nx], sr[nx], sg[nx], sb[nx], level[nx] = 0, 0, 0, 0, lv + 1
|
||||
leaf[nx] = lv == 4
|
||||
if lv == 4 then
|
||||
leaves += 1
|
||||
else
|
||||
table.insert(byLevel[lv + 2], nx)
|
||||
end
|
||||
end
|
||||
node = nx
|
||||
end
|
||||
n[node] += c
|
||||
sr[node] += readf64(sums, bin * 24)
|
||||
sg[node] += readf64(sums, bin * 24 + 8)
|
||||
sb[node] += readf64(sums, bin * 24 + 16)
|
||||
end
|
||||
end
|
||||
-- fold the deepest inner nodes (fewest pixels first, ties by creation order) until few enough leaves remain
|
||||
local function fold(node: number): number
|
||||
-- merges the children (all leaves) into node; returns how many leaves disappeared
|
||||
local merged = 0
|
||||
for k = 1, 8 do
|
||||
local slot = (node - 1) * 8 + k
|
||||
local ch = child[slot]
|
||||
if ch ~= 0 then
|
||||
n[node] += n[ch]
|
||||
sr[node] += sr[ch]
|
||||
sg[node] += sg[ch]
|
||||
sb[node] += sb[ch]
|
||||
child[slot] = 0
|
||||
merged += 1
|
||||
end
|
||||
end
|
||||
leaf[node] = true
|
||||
return merged - 1
|
||||
end
|
||||
local function subtotal(node: number): number
|
||||
local s = 0
|
||||
for k = 1, 8 do
|
||||
local ch = child[(node - 1) * 8 + k]
|
||||
if ch ~= 0 then
|
||||
s += n[ch]
|
||||
end
|
||||
end
|
||||
return s
|
||||
end
|
||||
for lv = 5, 1, -1 do
|
||||
if leaves <= maxColors then
|
||||
break
|
||||
end
|
||||
local list = byLevel[lv]
|
||||
local order = table.create(#list)
|
||||
for i, node in list do
|
||||
order[i] = { node, subtotal(node), i }
|
||||
end
|
||||
table.sort(order, function(a: { number }, b: { number }): boolean
|
||||
if a[2] ~= b[2] then
|
||||
return a[2] < b[2]
|
||||
end
|
||||
return a[3] < b[3]
|
||||
end)
|
||||
for _, e in order do
|
||||
if leaves <= maxColors then
|
||||
break
|
||||
end
|
||||
leaves -= fold(e[1])
|
||||
end
|
||||
end
|
||||
-- collect leaves depth first in child order
|
||||
local pal: { Color } = {}
|
||||
local function walk(node: number)
|
||||
if leaf[node] then
|
||||
local c = n[node]
|
||||
if c > 0 then
|
||||
table.insert(pal, { floor(sr[node] / c + 0.5), floor(sg[node] / c + 0.5), floor(sb[node] / c + 0.5), 255 })
|
||||
end
|
||||
return
|
||||
end
|
||||
for k = 1, 8 do
|
||||
local ch = child[(node - 1) * 8 + k]
|
||||
if ch ~= 0 then
|
||||
walk(ch)
|
||||
end
|
||||
end
|
||||
end
|
||||
walk(1)
|
||||
-- the root itself can end up a leaf with everything (maxColors 1)
|
||||
while #pal > maxColors do
|
||||
table.remove(pal)
|
||||
end
|
||||
return pal
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ palette
|
||||
-- palette(pixels, w, h, maxColors, opts?) -> palette, exact
|
||||
function Quantize.palette(pixels: buffer, w: number, h: number, maxColors: number, opts: PaletteOptions?): ({ Color }, boolean)
|
||||
if type(pixels) ~= "buffer" or type(w) ~= "number" or type(h) ~= "number" or w < 0 or h < 0 or blen(pixels) < w * h * 4 then
|
||||
return {}, false
|
||||
end
|
||||
local maxc = math.clamp(floor(tonumber(maxColors) or 256), 1, 65536)
|
||||
local npx = w * h
|
||||
-- exact colours, in first-seen order (stops counting past maxColors)
|
||||
local seen: { [number]: boolean } = {}
|
||||
local exact: { Color } = {}
|
||||
local over = false
|
||||
for i = 0, npx - 1 do
|
||||
local o = i * 4
|
||||
if readu8(pixels, o + 3) >= 128 then
|
||||
local r, g, b = readu8(pixels, o), readu8(pixels, o + 1), readu8(pixels, o + 2)
|
||||
local key = r * 65536 + g * 256 + b
|
||||
if not seen[key] then
|
||||
if #exact >= maxc then
|
||||
over = true
|
||||
break
|
||||
end
|
||||
seen[key] = true
|
||||
table.insert(exact, { r, g, b, 255 })
|
||||
end
|
||||
end
|
||||
end
|
||||
if not over then
|
||||
return exact, true
|
||||
end
|
||||
local cnt, sums = count(pixels, npx)
|
||||
local method = opts and opts.method or "mediancut"
|
||||
if method == "octree" and maxc >= 8 then -- below 8 the octree can only fold to a single colour
|
||||
return octree(cnt, sums, maxc), false
|
||||
end
|
||||
return medianCut(cnt, sums, maxc), false
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ map
|
||||
-- map(pixels, w, h, palette, opts?) -> buffer of w * h palette indexes (0-based)
|
||||
function Quantize.map(pixels: buffer, w: number, h: number, palette: { Color }, opts: MapOptions?): buffer
|
||||
if type(pixels) ~= "buffer" or type(w) ~= "number" or type(h) ~= "number" or w < 0 or h < 0 or blen(pixels) < w * h * 4 or type(palette) ~= "table" then
|
||||
return buffer.create(0)
|
||||
end
|
||||
local npx = w * h
|
||||
local out = buffer.create(npx)
|
||||
local dither = opts and opts.dither or false
|
||||
local tidx = opts and opts.transparentIndex
|
||||
-- the searchable entries (the transparent slot is excluded)
|
||||
local pr, pg, pb, pi = {}, {}, {}, {}
|
||||
for i, c in palette do
|
||||
if tidx == nil or i - 1 ~= tidx then
|
||||
table.insert(pr, c[1] or 0)
|
||||
table.insert(pg, c[2] or 0)
|
||||
table.insert(pb, c[3] or 0)
|
||||
table.insert(pi, i - 1)
|
||||
end
|
||||
end
|
||||
local np = #pr
|
||||
-- sorted by green (the heaviest weight), so the search can stop once 4 * dg^2 alone is worse than the best
|
||||
do
|
||||
local order = table.create(np)
|
||||
for k = 1, np do
|
||||
order[k] = pg[k] * 65536 + k
|
||||
end
|
||||
table.sort(order)
|
||||
local r2, g2, b2, i2 = table.create(np), table.create(np), table.create(np), table.create(np)
|
||||
for k = 1, np do
|
||||
local j = order[k] % 65536
|
||||
r2[k], g2[k], b2[k], i2[k] = pr[j], pg[j], pb[j], pi[j]
|
||||
end
|
||||
pr, pg, pb, pi = r2, g2, b2, i2
|
||||
end
|
||||
local startAt = table.create(256, np) -- the first entry with green >= g (1-based), np + 1 past the end
|
||||
do
|
||||
local k = 1
|
||||
for g = 0, 255 do
|
||||
while k <= np and pg[k] < g do
|
||||
k += 1
|
||||
end
|
||||
startAt[g + 1] = k
|
||||
end
|
||||
end
|
||||
if np == 0 then
|
||||
if tidx then
|
||||
buffer.fill(out, 0, math.clamp(tidx, 0, 255), npx)
|
||||
end
|
||||
return out
|
||||
end
|
||||
local cache: { [number]: number } = {}
|
||||
local function nearest(r: number, g: number, b: number): number
|
||||
local key = r * 65536 + g * 256 + b
|
||||
local hit = cache[key]
|
||||
if hit then
|
||||
return hit
|
||||
end
|
||||
local best, bd = 1, math.huge
|
||||
local up = startAt[math.clamp(g, 0, 255) + 1]
|
||||
local down = up - 1
|
||||
while up <= np or down >= 1 do
|
||||
if up <= np then
|
||||
local dg = pg[up] - g
|
||||
if dg * dg * 4 >= bd then
|
||||
up = np + 1
|
||||
else
|
||||
local dr, db = r - pr[up], b - pb[up]
|
||||
local d = dr * dr * 2 + dg * dg * 4 + db * db * 3
|
||||
if d < bd then
|
||||
best, bd = up, d
|
||||
end
|
||||
up += 1
|
||||
end
|
||||
end
|
||||
if down >= 1 then
|
||||
local dg = g - pg[down]
|
||||
if dg * dg * 4 >= bd then
|
||||
down = 0
|
||||
else
|
||||
local dr, db = r - pr[down], b - pb[down]
|
||||
local d = dr * dr * 2 + dg * dg * 4 + db * db * 3
|
||||
if d < bd then
|
||||
best, bd = down, d
|
||||
end
|
||||
down -= 1
|
||||
end
|
||||
end
|
||||
end
|
||||
cache[key] = best
|
||||
return best
|
||||
end
|
||||
local tbyte = if tidx then math.clamp(tidx, 0, 255) else 0
|
||||
if not dither then
|
||||
for i = 0, npx - 1 do
|
||||
local o = i * 4
|
||||
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))])
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
-- Floyd-Steinberg: error rows (f64 r, g, b per pixel, one extra pixel each side)
|
||||
local rowLen = (w + 2) * 24
|
||||
local cur, nxt = buffer.create(rowLen), buffer.create(rowLen)
|
||||
for y = 0, h - 1 do
|
||||
buffer.fill(nxt, 0, 0, rowLen)
|
||||
for x = 0, w - 1 do
|
||||
local i = y * w + x
|
||||
local o = i * 4
|
||||
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)
|
||||
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)
|
||||
end
|
||||
end
|
||||
cur, nxt = nxt, cur
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- quantize(pixels, w, h, maxColors, opts?) -> indexes, palette, exact: palette() then map() (opts of both)
|
||||
function Quantize.quantize(pixels: buffer, w: number, h: number, maxColors: number, opts: any?): (buffer, { Color }, boolean)
|
||||
local pal, exact = Quantize.palette(pixels, w, h, maxColors, opts)
|
||||
local mo = opts
|
||||
if exact and opts and opts.dither then
|
||||
mo = table.clone(opts)
|
||||
mo.dither = false -- nothing to diffuse
|
||||
end
|
||||
return Quantize.map(pixels, w, h, pal, mo), pal, exact
|
||||
end
|
||||
|
||||
return Quantize
|
||||
Reference in new issue
Block a user