153 lines
4.3 KiB
Luau
153 lines
4.3 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- image/qoi/Codec: the QOI op loops (native; never throws). Written from the QOI specification 1.0 (qoiformat.org).
|
|
-- decode reads ops from b[p, limit) into an rgba8 buffer; encode writes ops for rgba8 / rgb8 pixels. Every input read
|
|
-- is checked against limit; the output buffer has exactly w * h * 4 bytes and the loop stops when it is full.
|
|
local readu8, writeu8, writeu32 = buffer.readu8, buffer.writeu8, buffer.writeu32
|
|
|
|
local Codec = {}
|
|
|
|
local index = buffer.create(256)
|
|
|
|
-- decode(b, p, limit, out, n) -> true | false, "truncated" (n pixels into out)
|
|
function Codec.decode(b: buffer, p: number, limit: number, out: buffer, n: number): (boolean, string?)
|
|
buffer.fill(index, 0, 0, 256)
|
|
local r, g, bl, a = 0, 0, 0, 255
|
|
local o = 0
|
|
local stop = n * 4
|
|
while o < stop do
|
|
if p >= limit then
|
|
return false, "truncated"
|
|
end
|
|
local op = readu8(b, p)
|
|
p += 1
|
|
if op == 0xFE then
|
|
if p + 3 > limit then
|
|
return false, "truncated"
|
|
end
|
|
r = readu8(b, p)
|
|
g = readu8(b, p + 1)
|
|
bl = readu8(b, p + 2)
|
|
p += 3
|
|
elseif op == 0xFF then
|
|
if p + 4 > limit then
|
|
return false, "truncated"
|
|
end
|
|
r = readu8(b, p)
|
|
g = readu8(b, p + 1)
|
|
bl = readu8(b, p + 2)
|
|
a = readu8(b, p + 3)
|
|
p += 4
|
|
else
|
|
local tag = op // 64
|
|
if tag == 0 then
|
|
local q = op * 4
|
|
r = readu8(index, q)
|
|
g = readu8(index, q + 1)
|
|
bl = readu8(index, q + 2)
|
|
a = readu8(index, q + 3)
|
|
elseif tag == 1 then
|
|
r = (r + (op // 16) % 4 - 2) % 256
|
|
g = (g + (op // 4) % 4 - 2) % 256
|
|
bl = (bl + op % 4 - 2) % 256
|
|
elseif tag == 2 then
|
|
if p >= limit then
|
|
return false, "truncated"
|
|
end
|
|
local d2 = readu8(b, p)
|
|
p += 1
|
|
local dg = op % 64 - 32
|
|
g = (g + dg) % 256
|
|
r = (r + dg + d2 // 16 - 8) % 256
|
|
bl = (bl + dg + d2 % 16 - 8) % 256
|
|
else
|
|
local run = op % 64 -- 1..62 more copies of the previous pixel (63 / 64 are RGB / RGBA)
|
|
local v = r + g * 256 + bl * 65536 + a * 16777216
|
|
local e = math.min(o + run * 4, stop - 4)
|
|
while o < e do
|
|
writeu32(out, o, v)
|
|
o += 4
|
|
end
|
|
end
|
|
end
|
|
writeu32(out, o, r + g * 256 + bl * 65536 + a * 16777216)
|
|
o += 4
|
|
local h = ((r * 3 + g * 5 + bl * 7 + a * 11) % 64) * 4
|
|
writeu8(index, h, r)
|
|
writeu8(index, h + 1, g)
|
|
writeu8(index, h + 2, bl)
|
|
writeu8(index, h + 3, a)
|
|
end
|
|
return true, nil
|
|
end
|
|
|
|
-- encode(px, n, bpp, out, o) -> new offset: n pixels of bpp (3 or 4) bytes; out has room for n * (bpp + 1) bytes
|
|
function Codec.encode(px: buffer, n: number, bpp: number, out: buffer, o: number): number
|
|
buffer.fill(index, 0, 0, 256)
|
|
local pr, pg, pb, pa = 0, 0, 0, 255
|
|
local run = 0
|
|
local hasA = bpp == 4
|
|
for i = 0, n - 1 do
|
|
local q = i * bpp
|
|
local r, g, b = readu8(px, q), readu8(px, q + 1), readu8(px, q + 2)
|
|
local a = if hasA then readu8(px, q + 3) else 255
|
|
if r == pr and g == pg and b == pb and a == pa then
|
|
run += 1
|
|
if run == 62 or i == n - 1 then
|
|
writeu8(out, o, 0xC0 + run - 1)
|
|
o += 1
|
|
run = 0
|
|
end
|
|
else
|
|
if run > 0 then
|
|
writeu8(out, o, 0xC0 + run - 1)
|
|
o += 1
|
|
run = 0
|
|
end
|
|
local h = (r * 3 + g * 5 + b * 7 + a * 11) % 64
|
|
local hq = h * 4
|
|
if readu8(index, hq) == r and readu8(index, hq + 1) == g and readu8(index, hq + 2) == b and readu8(index, hq + 3) == a then
|
|
writeu8(out, o, h)
|
|
o += 1
|
|
else
|
|
writeu8(index, hq, r)
|
|
writeu8(index, hq + 1, g)
|
|
writeu8(index, hq + 2, b)
|
|
writeu8(index, hq + 3, a)
|
|
if a == pa then
|
|
local dr = (r - pr + 128) % 256 - 128
|
|
local dg = (g - pg + 128) % 256 - 128
|
|
local db = (b - pb + 128) % 256 - 128
|
|
local dgr, dgb = dr - dg, db - dg
|
|
if dr >= -2 and dr <= 1 and dg >= -2 and dg <= 1 and db >= -2 and db <= 1 then
|
|
writeu8(out, o, 0x40 + (dr + 2) * 16 + (dg + 2) * 4 + db + 2)
|
|
o += 1
|
|
elseif dg >= -32 and dg <= 31 and dgr >= -8 and dgr <= 7 and dgb >= -8 and dgb <= 7 then
|
|
writeu8(out, o, 0x80 + dg + 32)
|
|
writeu8(out, o + 1, (dgr + 8) * 16 + dgb + 8)
|
|
o += 2
|
|
else
|
|
writeu8(out, o, 0xFE)
|
|
writeu8(out, o + 1, r)
|
|
writeu8(out, o + 2, g)
|
|
writeu8(out, o + 3, b)
|
|
o += 4
|
|
end
|
|
else
|
|
writeu8(out, o, 0xFF)
|
|
writeu8(out, o + 1, r)
|
|
writeu8(out, o + 2, g)
|
|
writeu8(out, o + 3, b)
|
|
writeu8(out, o + 4, a)
|
|
o += 5
|
|
end
|
|
end
|
|
pr, pg, pb, pa = r, g, b, a
|
|
end
|
|
end
|
|
return o
|
|
end
|
|
|
|
return Codec
|