imgA-simple: image/qoi (full spec read/write; byte-identical to Pillow's encoder) with Pillow fixtures
This commit is contained in:
1 parent
2eee0bc1a6
commit
985bb5364d
6 files changed
+428
No files matched your search
@@ -26,6 +26,18 @@ local list: { Entry } = {
|
||||
return require("@formats/image/png")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "qoi",
|
||||
category = "image",
|
||||
path = "image/qoi",
|
||||
extensions = { "qoi" },
|
||||
mime = { "image/qoi", "image/x-qoi" },
|
||||
deps = { "core/Err", "core/Bytes", "Types" },
|
||||
caps = { decode = true, encode = true, alpha = true, depths = { 8 } },
|
||||
load = function()
|
||||
return require("@formats/image/qoi")
|
||||
end,
|
||||
},
|
||||
}
|
||||
|
||||
return list
|
||||
@@ -0,0 +1,144 @@
|
||||
--!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, g, bl = readu8(b, p), readu8(b, p + 1), readu8(b, p + 2)
|
||||
p += 3
|
||||
elseif op == 0xFF then
|
||||
if p + 4 > limit then
|
||||
return false, "truncated"
|
||||
end
|
||||
r, g, bl, a = readu8(b, p), readu8(b, p + 1), readu8(b, p + 2), readu8(b, p + 3)
|
||||
p += 4
|
||||
else
|
||||
local tag = op // 64
|
||||
if tag == 0 then
|
||||
local q = op * 4
|
||||
r, g, bl, a = readu8(index, q), readu8(index, q + 1), readu8(index, q + 2), 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
|
||||
@@ -0,0 +1,122 @@
|
||||
--!strict
|
||||
-- image/qoi: QOI, the Quite OK Image format (specification 1.0, qoiformat.org). New code written from the spec.
|
||||
-- Supports: decode of RGB and RGBA files (either colour space tag) to rgba8 (opts.native: rgb8 for 3-channel files);
|
||||
-- encode of rgba8 (4 channels) and rgb8 (3 channels), lossless; meta.source.colorSpace keeps the sRGB / linear tag.
|
||||
-- Not supported: nothing in the spec is left out.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Codec = require("@formats/image/qoi/Codec")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Qoi = {
|
||||
name = "qoi",
|
||||
category = "image",
|
||||
extensions = { "qoi" },
|
||||
mime = { "image/qoi", "image/x-qoi" },
|
||||
deps = { "core/Err", "core/Bytes", "Types" },
|
||||
caps = { decode = true, encode = true, alpha = true, depths = { 8 } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
native: boolean?, -- rgb8 for 3-channel files (default false: always rgba8)
|
||||
maxPixels: number?, -- refuse larger images (default 2^26)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
channels: number?, -- 3 or 4 (default: 3 for rgb8 images or opaque rgba8, else 4)
|
||||
linear: boolean?, -- tag the file as linear instead of sRGB (default: meta.source.colorSpace == "linear")
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Qoi.sniff(r: Bytes.Reader): number
|
||||
return if r:peek(4) == "qoif" then 1 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Qoi.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
|
||||
local maxPixels = opts and opts.maxPixels or 2 ^ 26
|
||||
local magic = r:string(4)
|
||||
local w, h = r:u32be(), r:u32be()
|
||||
local channels, cs = r:u8(), r:u8()
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
if magic ~= "qoif" then
|
||||
return nil, Err.new(Err.CORRUPT, "bad magic", r.base)
|
||||
end
|
||||
if w == 0 or h == 0 or (channels ~= 3 and channels ~= 4) or cs > 1 then
|
||||
return nil, Err.new(Err.CORRUPT, "bad header", r.base + 4)
|
||||
end
|
||||
if w * h > maxPixels then
|
||||
return nil, Err.new(Err.LIMIT, string.format("%d x %d is over the %d pixel limit", w, h, maxPixels))
|
||||
end
|
||||
local img = Types.Image.new(w, h, "rgba8")
|
||||
local ok, e = Codec.decode(r.buf, r.pos, r.limit, img.pixels, w * h)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.TRUNCATED, tostring(e))
|
||||
end
|
||||
if opts and opts.native and channels == 3 then
|
||||
local rgb = Types.Image.new(w, h, "rgb8")
|
||||
local src, dst = img.pixels, rgb.pixels
|
||||
for i = 0, w * h - 1 do
|
||||
buffer.writeu16(dst, i * 3, buffer.readu16(src, i * 4))
|
||||
buffer.writeu8(dst, i * 3 + 2, buffer.readu8(src, i * 4 + 2))
|
||||
end
|
||||
img = rgb
|
||||
end
|
||||
img.meta.source = { format = "qoi", bitDepth = 8, colorType = if channels == 4 then "rgba" else "rgb", channels = channels, colorSpace = if cs == 1 then "linear" else "srgb" }
|
||||
if cs == 1 then
|
||||
img.meta.colorSpace = "linear"
|
||||
end
|
||||
return img, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Qoi.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if type(img) ~= "table" or type(img.pixels) ~= "buffer" or (img.format ~= "rgba8" and img.format ~= "rgb8") then
|
||||
return nil, Err.new(Err.ARG, "needs an rgba8 or rgb8 Image")
|
||||
end
|
||||
local w, h = img.width, img.height
|
||||
local bpp = if img.format == "rgba8" then 4 else 3
|
||||
if type(w) ~= "number" or type(h) ~= "number" or w < 1 or h < 1 or w % 1 ~= 0 or h % 1 ~= 0 or buffer.len(img.pixels) < w * h * bpp then
|
||||
return nil, Err.new(Err.ARG, "bad image size or pixel buffer")
|
||||
end
|
||||
local channels = opts and opts.channels
|
||||
if channels == nil then
|
||||
channels = 3
|
||||
if bpp == 4 then
|
||||
local px = img.pixels
|
||||
for i = 0, w * h - 1 do
|
||||
if buffer.readu8(px, i * 4 + 3) ~= 255 then
|
||||
channels = 4
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif channels ~= 3 and channels ~= 4 then
|
||||
return nil, Err.new(Err.ARG, "channels must be 3 or 4")
|
||||
end
|
||||
local linear = opts and opts.linear
|
||||
if linear == nil then
|
||||
local src = img.meta and img.meta.source
|
||||
linear = (type(src) == "table" and src.colorSpace == "linear") or (img.meta and img.meta.colorSpace == "linear") or false
|
||||
end
|
||||
local n = w * h
|
||||
local out = buffer.create(14 + n * (bpp + 1) + 8)
|
||||
buffer.writestring(out, 0, "qoif")
|
||||
buffer.writeu32(out, 4, bit32.byteswap(w))
|
||||
buffer.writeu32(out, 8, bit32.byteswap(h))
|
||||
buffer.writeu8(out, 12, channels)
|
||||
buffer.writeu8(out, 13, if linear then 1 else 0)
|
||||
local o = Codec.encode(img.pixels, n, bpp, out, 14)
|
||||
buffer.writestring(out, o, "\0\0\0\0\0\0\0\1")
|
||||
o += 8
|
||||
local res = buffer.create(o)
|
||||
buffer.copy(res, 0, out, 0, o)
|
||||
return res, nil
|
||||
end
|
||||
|
||||
return Qoi
|
||||
Reference in new issue
Block a user