ws-tex: image/icns (read RLE / ARGB / 1-bit / PNG-hook entries, JP2 skipped; write PNG via encodeImage or RLE)
Tested against libicns (png2icns / icns2png -x), Pillow's writer and a hand-written file.
This commit is contained in:
1 parent
ce4845ae13
commit
9e114fba8b
6 files changed
+519
No files matched your search
@@ -10,6 +10,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
|||||||
| image | exr | .exr | yes | yes | alpha: true, float: true, hdr: true, depths: 16, 32, multipart: true, tiled: true | `tests/image/exr.luau` |
|
| image | exr | .exr | yes | yes | alpha: true, float: true, hdr: true, depths: 16, 32, multipart: true, tiled: true | `tests/image/exr.luau` |
|
||||||
| image | gif | .gif | yes | yes | animation: true, alpha: true, palette: true, depths: 1, 2, 4, 8 | `tests/image/gif.luau` |
|
| image | gif | .gif | yes | yes | animation: true, alpha: true, palette: true, depths: 1, 2, 4, 8 | `tests/image/gif.luau` |
|
||||||
| image | hdr | .hdr .pic .rgbe | yes | yes | alpha: false, float: true, depths: 32 | `tests/image/hdr.luau` |
|
| image | hdr | .hdr .pic .rgbe | yes | yes | alpha: false, float: true, depths: 32 | `tests/image/hdr.luau` |
|
||||||
|
| image | icns | .icns | yes | yes | alpha: true, frames: true, depths: 1, 24, 32 | `tests/image/icns.luau` |
|
||||||
| image | ico | .ico .cur | yes | yes | alpha: true, frames: true, depths: 1, 4, 8, 16, 24, 32 | `tests/image/ico.luau` |
|
| image | ico | .ico .cur | yes | yes | alpha: true, frames: true, depths: 1, 4, 8, 16, 24, 32 | `tests/image/ico.luau` |
|
||||||
| image | ilbm | .iff .ilbm .lbm .pbm | yes | yes | alpha: true, palette: true, depths: 1, 2, 3, 4, 5, 6, 7, 8, 24, 32 | `tests/image/ilbm.luau` |
|
| image | ilbm | .iff .ilbm .lbm .pbm | yes | yes | alpha: true, palette: true, depths: 1, 2, 3, 4, 5, 6, 7, 8, 24, 32 | `tests/image/ilbm.luau` |
|
||||||
| image | jpeg | .jpg .jpeg .jpe .jfif | yes | yes | lossy: true, alpha: false, depths: 8, 12, progressive: true, arithmetic: true | `tests/image/jpeg.luau` |
|
| image | jpeg | .jpg .jpeg .jpe .jfif | yes | yes | lossy: true, alpha: false, depths: 8, 12, progressive: true, arithmetic: true | `tests/image/jpeg.luau` |
|
||||||
|
|||||||
@@ -74,6 +74,18 @@ local list: { Entry } = {
|
|||||||
return require("@formats/image/hdr")
|
return require("@formats/image/hdr")
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name = "icns",
|
||||||
|
category = "image",
|
||||||
|
path = "image/icns",
|
||||||
|
extensions = { "icns" },
|
||||||
|
mime = { "image/icns", "image/x-icns" },
|
||||||
|
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "containers/Chunks" },
|
||||||
|
caps = { decode = true, encode = true, alpha = true, frames = true, depths = { 1, 24, 32 } },
|
||||||
|
load = function()
|
||||||
|
return require("@formats/image/icns")
|
||||||
|
end,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name = "ico",
|
name = "ico",
|
||||||
category = "image",
|
category = "image",
|
||||||
|
|||||||
@@ -0,0 +1,337 @@
|
|||||||
|
--!strict
|
||||||
|
-- image/icns: Apple Icon Image files (the icns resource type; new code; chunks through containers/Chunks). Decode:
|
||||||
|
-- the legacy RGB entries is32 / il32 / ih32 / it32 (the icns RLE, or raw) with their s8mk / l8mk / h8mk / t8mk alpha
|
||||||
|
-- masks, ic04 / ic05 ARGB entries, the 1-bit ics# / ICN# / ich# / icm# icons with their masks, and PNG entries in any
|
||||||
|
-- type (icp4 .. ic14) through the api hook opts.decodeImage. JPEG 2000 entries are skipped (meta.skipped lists them),
|
||||||
|
-- like 4 / 8-bit palette entries and nested dark-mode / TOC chunks. The image is the largest entry; img.frames lists
|
||||||
|
-- every decoded entry (each with meta.source = { type = "ic07", ... }); a file of JPEG 2000 entries only returns
|
||||||
|
-- UNSUPPORTED. Encode: the image and every img.frames image (square, 16 .. 1024): PNG entries through opts.encodeImage
|
||||||
|
-- (icp4 / icp5 / icp6 / ic07 / ic08 / ic09 / ic10), or without the hook (or opts.legacy) the RLE entries for 16 / 32 /
|
||||||
|
-- 48 / 128 with masks.
|
||||||
|
|
||||||
|
-- ------------------------------------------------------------------------------------------------ requires
|
||||||
|
local Err = require("@formats/core/Err")
|
||||||
|
local Bytes = require("@formats/core/Bytes")
|
||||||
|
local Sniff = require("@formats/core/Sniff")
|
||||||
|
local Types = require("@formats/Types")
|
||||||
|
local Chunks = require("@formats/containers/Chunks")
|
||||||
|
|
||||||
|
-- ------------------------------------------------------------------------------------------------ contract
|
||||||
|
local Icns = {
|
||||||
|
name = "icns",
|
||||||
|
category = "image",
|
||||||
|
extensions = { "icns" },
|
||||||
|
mime = { "image/icns", "image/x-icns" },
|
||||||
|
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "containers/Chunks" },
|
||||||
|
caps = { decode = true, encode = true, alpha = true, frames = true, depths = { 1, 24, 32 } },
|
||||||
|
}
|
||||||
|
|
||||||
|
-- ------------------------------------------------------------------------------------------------ types
|
||||||
|
export type DecodeOptions = {
|
||||||
|
decodeImage: ((buffer) -> (Types.Image?, Err.Err?))?, -- decodes PNG entries (the api passes one)
|
||||||
|
maxPixels: number?, -- refuse entries larger than this (default 2^22)
|
||||||
|
}
|
||||||
|
export type EncodeOptions = {
|
||||||
|
encodeImage: ((Types.Image, string) -> (buffer?, Err.Err?))?, -- writes PNG entries (the api passes one)
|
||||||
|
legacy: boolean?, -- RLE entries (16 / 32 / 48 / 128) even when encodeImage is there (default false)
|
||||||
|
}
|
||||||
|
|
||||||
|
-- ------------------------------------------------------------------------------------------------ constants
|
||||||
|
local RGB: { [string]: { any } } = { is32 = { 16, "s8mk" }, il32 = { 32, "l8mk" }, ih32 = { 48, "h8mk" }, it32 = { 128, "t8mk" } }
|
||||||
|
local MASKS: { [string]: boolean } = { s8mk = true, l8mk = true, h8mk = true, t8mk = true }
|
||||||
|
local ARGB: { [string]: number } = { ic04 = 16, ic05 = 32 }
|
||||||
|
local MONO: { [string]: { number } } = { ["ics#"] = { 16, 16 }, ["ICN#"] = { 32, 32 }, ["ich#"] = { 48, 48 }, ["icm#"] = { 16, 12 } }
|
||||||
|
local PNG_TYPES: { [number]: string } = { [16] = "icp4", [32] = "icp5", [64] = "icp6", [128] = "ic07", [256] = "ic08", [512] = "ic09", [1024] = "ic10" }
|
||||||
|
local LEGACY: { [number]: { string } } = { [16] = { "is32", "s8mk" }, [32] = { "il32", "l8mk" }, [48] = { "ih32", "h8mk" }, [128] = { "it32", "t8mk" } }
|
||||||
|
local LAYOUT: Chunks.Layout = {
|
||||||
|
bigEndian = true,
|
||||||
|
resize = function(_id: string, declared: number): number?
|
||||||
|
return math.max(0, declared - 8)
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
local readu8 = buffer.readu8
|
||||||
|
|
||||||
|
-- ------------------------------------------------------------------------------------------------ sniff
|
||||||
|
function Icns.sniff(r: Bytes.Reader): number
|
||||||
|
return Sniff.magic(r, "icns")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- ------------------------------------------------------------------------------------------------ decode
|
||||||
|
-- the icns RLE of n channels of count bytes each from [p, stop) into out at channel offsets (interleaved, step 4)
|
||||||
|
local function unrle(b: buffer, p: number, stop: number, out: buffer, channels: { number }, count: number): boolean
|
||||||
|
for _, ch in channels do
|
||||||
|
local i = 0
|
||||||
|
while i < count do
|
||||||
|
if p >= stop then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
local c = readu8(b, p)
|
||||||
|
p += 1
|
||||||
|
if c >= 128 then
|
||||||
|
local n = c - 125
|
||||||
|
if p >= stop then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
local v = readu8(b, p)
|
||||||
|
p += 1
|
||||||
|
for _ = 1, math.min(n, count - i) do
|
||||||
|
buffer.writeu8(out, i * 4 + ch, v)
|
||||||
|
i += 1
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local n = c + 1
|
||||||
|
if p + n > stop then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
for k = 0, math.min(n, count - i) - 1 do
|
||||||
|
buffer.writeu8(out, (i + k) * 4 + ch, readu8(b, p + k))
|
||||||
|
end
|
||||||
|
i += math.min(n, count - i)
|
||||||
|
p += n
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
local function isPng(b: buffer, p: number, n: number): boolean
|
||||||
|
return n >= 8 and buffer.readstring(b, p, 8) == "\137PNG\r\n\26\n"
|
||||||
|
end
|
||||||
|
local function isJp2(b: buffer, p: number, n: number): boolean
|
||||||
|
return n >= 12 and (buffer.readstring(b, p, 12) == "\0\0\0\12jP \r\n\135\n" or buffer.readstring(b, p, 4) == "\255\79\255\81")
|
||||||
|
end
|
||||||
|
|
||||||
|
function Icns.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?)
|
||||||
|
local b, o, limit = r.buf, r.base, r.limit
|
||||||
|
if limit - o < 8 or buffer.readstring(b, o, 4) ~= "icns" then
|
||||||
|
return nil, Err.new(Err.CORRUPT, "not an icns file", o)
|
||||||
|
end
|
||||||
|
local total = bit32.byteswap(buffer.readu32(b, o + 4))
|
||||||
|
local stop = math.min(limit, o + math.max(total, 8))
|
||||||
|
local list = Chunks.walk(b, o + 8, stop, LAYOUT)
|
||||||
|
local decodeImage = opts and opts.decodeImage
|
||||||
|
local maxPixels = opts and opts.maxPixels or 2 ^ 22
|
||||||
|
local masks: { [string]: Chunks.Chunk } = {}
|
||||||
|
for _, c in list do
|
||||||
|
if MASKS[c.id] then
|
||||||
|
masks[c.id] = c
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local frames: { Types.Frame } = {}
|
||||||
|
local skipped: { string } = {}
|
||||||
|
local function add(im: Types.Image, typ: string, extra: { [string]: any }?)
|
||||||
|
local src: { [string]: any } = { format = "icns", type = typ }
|
||||||
|
if extra then
|
||||||
|
for k, v in extra do
|
||||||
|
src[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
im.meta.source = src :: any
|
||||||
|
table.insert(frames, { image = im, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } :: Types.Frame)
|
||||||
|
end
|
||||||
|
for _, c in list do
|
||||||
|
local id, p, n = c.id, c.offset, c.avail
|
||||||
|
if c.truncated then
|
||||||
|
table.insert(skipped, id .. " (truncated)")
|
||||||
|
elseif RGB[id] then
|
||||||
|
local size = RGB[id][1]
|
||||||
|
local im = Types.Image.new(size, size, "rgba8")
|
||||||
|
local px = im.pixels
|
||||||
|
local q = if id == "it32" then p + 4 else p
|
||||||
|
local count = size * size
|
||||||
|
local ok
|
||||||
|
if n == count * 3 then
|
||||||
|
for i = 0, count - 1 do
|
||||||
|
for k = 0, 2 do
|
||||||
|
buffer.writeu8(px, i * 4 + k, readu8(b, p + k * count + i))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
ok = true
|
||||||
|
else
|
||||||
|
ok = unrle(b, q, p + n, px, { 0, 1, 2 }, count)
|
||||||
|
end
|
||||||
|
local m = masks[RGB[id][2]]
|
||||||
|
for i = 0, count - 1 do
|
||||||
|
buffer.writeu8(px, i * 4 + 3, if m and m.avail >= count then readu8(b, m.offset + i) else 255)
|
||||||
|
end
|
||||||
|
if ok then
|
||||||
|
add(im, id, { bitDepth = 24, mask = m ~= nil })
|
||||||
|
else
|
||||||
|
table.insert(skipped, id .. " (bad RLE)")
|
||||||
|
end
|
||||||
|
elseif ARGB[id] and n >= 4 and buffer.readstring(b, p, 4) == "ARGB" then
|
||||||
|
local size = ARGB[id]
|
||||||
|
local im = Types.Image.new(size, size, "rgba8")
|
||||||
|
if unrle(b, p + 4, p + n, im.pixels, { 3, 0, 1, 2 }, size * size) then
|
||||||
|
add(im, id, { bitDepth = 32 })
|
||||||
|
else
|
||||||
|
table.insert(skipped, id .. " (bad RLE)")
|
||||||
|
end
|
||||||
|
elseif MONO[id] then
|
||||||
|
local w, h = MONO[id][1], MONO[id][2]
|
||||||
|
local bytes = w * h // 8
|
||||||
|
if n >= bytes * 2 then
|
||||||
|
local im = Types.Image.new(w, h, "rgba8")
|
||||||
|
for i = 0, w * h - 1 do
|
||||||
|
local bit = bit32.band(bit32.rshift(readu8(b, p + bit32.rshift(i, 3)), 7 - bit32.band(i, 7)), 1)
|
||||||
|
local mbit = bit32.band(bit32.rshift(readu8(b, p + bytes + bit32.rshift(i, 3)), 7 - bit32.band(i, 7)), 1)
|
||||||
|
buffer.writeu32(im.pixels, i * 4, (if bit == 1 then 0 else 0xFFFFFF) + mbit * 0xFF000000)
|
||||||
|
end
|
||||||
|
add(im, id, { bitDepth = 1 })
|
||||||
|
end
|
||||||
|
elseif isPng(b, p, n) then
|
||||||
|
if decodeImage then
|
||||||
|
local sub = buffer.create(n)
|
||||||
|
buffer.copy(sub, 0, b, p, n)
|
||||||
|
local okc, im, ierr = pcall(decodeImage, sub)
|
||||||
|
if okc and im then
|
||||||
|
if im.width * im.height <= maxPixels then
|
||||||
|
add(im :: Types.Image, id, { png = true })
|
||||||
|
else
|
||||||
|
table.insert(skipped, id .. " (over maxPixels)")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
table.insert(skipped, id .. " (PNG: " .. tostring(if okc then ierr else im) .. ")")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
table.insert(skipped, id .. " (PNG: no decodeImage hook)")
|
||||||
|
end
|
||||||
|
elseif isJp2(b, p, n) then
|
||||||
|
table.insert(skipped, id .. " (JPEG 2000)")
|
||||||
|
elseif not MASKS[id] and id ~= "TOC " and id ~= "icnV" and id ~= "name" and id ~= "info" then
|
||||||
|
table.insert(skipped, id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if #frames == 0 then
|
||||||
|
if #skipped > 0 then
|
||||||
|
return nil, Err.new(Err.UNSUPPORTED, "no decodable entry (" .. table.concat(skipped, ", ") .. ")")
|
||||||
|
end
|
||||||
|
return nil, Err.new(Err.CORRUPT, "no icon entries", o + 8)
|
||||||
|
end
|
||||||
|
local main = frames[1].image
|
||||||
|
for _, f in frames do
|
||||||
|
if f.image.width * f.image.height > main.width * main.height then
|
||||||
|
main = f.image
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local img = Types.Image.new(0, 0, "rgba8")
|
||||||
|
img.width, img.height, img.pixels = main.width, main.height, main.pixels
|
||||||
|
if #frames > 1 then
|
||||||
|
img.frames = frames
|
||||||
|
end
|
||||||
|
if #skipped > 0 then
|
||||||
|
img.meta.skipped = skipped
|
||||||
|
end
|
||||||
|
img.meta.source = { format = "icns", type = main.meta.source and main.meta.source.type, entries = #frames }
|
||||||
|
return img, nil
|
||||||
|
end
|
||||||
|
|
||||||
|
-- ------------------------------------------------------------------------------------------------ encode
|
||||||
|
-- the icns RLE of one channel (count bytes, step 4 from ch) into w
|
||||||
|
local function rle(w: Bytes.Writer, px: buffer, ch: number, count: number)
|
||||||
|
local i = 0
|
||||||
|
while i < count do
|
||||||
|
local v = readu8(px, i * 4 + ch)
|
||||||
|
local run = 1
|
||||||
|
while i + run < count and run < 130 and readu8(px, (i + run) * 4 + ch) == v do
|
||||||
|
run += 1
|
||||||
|
end
|
||||||
|
if run >= 3 then
|
||||||
|
w:u8(run + 125)
|
||||||
|
w:u8(v)
|
||||||
|
i += run
|
||||||
|
else
|
||||||
|
local lit = 0
|
||||||
|
while i + lit < count and lit < 128 do
|
||||||
|
local a = readu8(px, (i + lit) * 4 + ch)
|
||||||
|
if i + lit + 2 < count and readu8(px, (i + lit + 1) * 4 + ch) == a and readu8(px, (i + lit + 2) * 4 + ch) == a then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
lit += 1
|
||||||
|
end
|
||||||
|
lit = math.max(lit, 1)
|
||||||
|
w:u8(lit - 1)
|
||||||
|
for k = 0, lit - 1 do
|
||||||
|
w:u8(readu8(px, (i + k) * 4 + ch))
|
||||||
|
end
|
||||||
|
i += lit
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Icns.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||||
|
if type(img) ~= "table" or type(img.pixels) ~= "buffer" then
|
||||||
|
return nil, Err.new(Err.ARG, "needs an Image")
|
||||||
|
end
|
||||||
|
local images: { Types.Image } = {}
|
||||||
|
local seen: { [number]: boolean } = {}
|
||||||
|
local function take(im: Types.Image): Err.Err?
|
||||||
|
if type(im) ~= "table" or im.format ~= "rgba8" or im.width ~= im.height then
|
||||||
|
return Err.new(Err.ARG, "icons are square rgba8 images")
|
||||||
|
end
|
||||||
|
if not seen[im.width] then
|
||||||
|
seen[im.width] = true
|
||||||
|
table.insert(images, im)
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
local e = take(img)
|
||||||
|
if e then
|
||||||
|
return nil, e
|
||||||
|
end
|
||||||
|
if type(img.frames) == "table" then
|
||||||
|
for _, f in img.frames do
|
||||||
|
local fe = take(f.image)
|
||||||
|
if fe then
|
||||||
|
return nil, fe
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.sort(images, function(a: Types.Image, c: Types.Image): boolean
|
||||||
|
return a.width < c.width
|
||||||
|
end)
|
||||||
|
local encodeImage = opts and opts.encodeImage
|
||||||
|
local legacy = (opts and opts.legacy) or encodeImage == nil
|
||||||
|
local body = Bytes.writer()
|
||||||
|
local function chunk(id: string, data: buffer)
|
||||||
|
body:string(id)
|
||||||
|
body:u32be(buffer.len(data) + 8)
|
||||||
|
body:bytes(data)
|
||||||
|
end
|
||||||
|
for _, im in images do
|
||||||
|
local s = im.width
|
||||||
|
if legacy and LEGACY[s] then
|
||||||
|
local types = LEGACY[s]
|
||||||
|
local w = Bytes.writer()
|
||||||
|
if s == 128 then
|
||||||
|
w:u32be(0)
|
||||||
|
end
|
||||||
|
for ch = 0, 2 do
|
||||||
|
rle(w, im.pixels, ch, s * s)
|
||||||
|
end
|
||||||
|
chunk(types[1], w:finish())
|
||||||
|
local m = buffer.create(s * s)
|
||||||
|
for i = 0, s * s - 1 do
|
||||||
|
buffer.writeu8(m, i, readu8(im.pixels, i * 4 + 3))
|
||||||
|
end
|
||||||
|
chunk(types[2], m)
|
||||||
|
elseif encodeImage and PNG_TYPES[s] then
|
||||||
|
local ok, png, perr = pcall(encodeImage, im, "png")
|
||||||
|
if not ok or not png then
|
||||||
|
return nil, Err.new(Err.ARG, "PNG entry " .. s .. ": " .. tostring(if ok then perr else png))
|
||||||
|
end
|
||||||
|
chunk(PNG_TYPES[s], png :: buffer)
|
||||||
|
else
|
||||||
|
return nil, Err.new(Err.ARG, string.format("no icns entry for %d x %d%s", s, s, if encodeImage then "" else " without an encodeImage hook (16 / 32 / 48 / 128 only)"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local data = body:finish()
|
||||||
|
local out = Bytes.writer(buffer.len(data) + 8)
|
||||||
|
out:string("icns")
|
||||||
|
out:u32be(buffer.len(data) + 8)
|
||||||
|
out:bytes(data)
|
||||||
|
return out:finish(), nil
|
||||||
|
end
|
||||||
|
|
||||||
|
return Icns
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
-- image/icns tests: a libicns file (RLE entries + masks, JPEG 2000 entries skipped), a Pillow file (PNG entries through
|
||||||
|
-- the decodeImage hook) and a hand-written one (ics#, raw is32, ic04 ARGB) against icns2png's / the written pixels;
|
||||||
|
-- our RLE file read back by icns2png; PNG entries through encodeImage; errors; the fuzz no-throw pass. Fixtures:
|
||||||
|
-- tools/fixtures/icns.py.
|
||||||
|
local T = require("@tests/T")
|
||||||
|
local Icns = require("@formats/image/icns")
|
||||||
|
local Png = require("@formats/image/png")
|
||||||
|
local Bytes = require("@formats/core/Bytes")
|
||||||
|
local Err = require("@formats/core/Err")
|
||||||
|
local Types = require("@formats/Types")
|
||||||
|
local R = require("@tests/image/_raster")
|
||||||
|
local Fx = require("@tests/gen/icns")
|
||||||
|
|
||||||
|
local t = T.suite("icns")
|
||||||
|
t:eq(Icns.name, "icns", "name")
|
||||||
|
t:check(Icns.caps.decode and Icns.caps.encode, "caps")
|
||||||
|
|
||||||
|
local hooks = {
|
||||||
|
decodeImage = function(b: buffer): (any, any)
|
||||||
|
return Png.decode(Bytes.reader(b))
|
||||||
|
end,
|
||||||
|
encodeImage = function(img: any, _fmt: string): (any, any)
|
||||||
|
return Png.encode(img)
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
local function check(c: any)
|
||||||
|
local img, err = Icns.decode(Bytes.reader(buffer.fromstring(c.data)), hooks)
|
||||||
|
t:check(img, c.name .. ": " .. tostring(err))
|
||||||
|
local frames = img.frames or { { image = img } }
|
||||||
|
t:check(#frames >= #c.entries, c.name .. ": entries") -- icns2png writes one file per size (@2x entries collide)
|
||||||
|
for _, e in c.entries do
|
||||||
|
local found = nil
|
||||||
|
for _, f in frames do
|
||||||
|
local im = f.image
|
||||||
|
if im.width == e.w and im.height == e.h and (e.type == "" or im.meta.source.type == e.type) then
|
||||||
|
found = im
|
||||||
|
end
|
||||||
|
end
|
||||||
|
t:check(found, c.name .. ": entry " .. e.type .. " " .. e.w)
|
||||||
|
if found then
|
||||||
|
local d = R.maxDiff(found.pixels, buffer.fromstring(e.rgba), e.w * e.h)
|
||||||
|
t:eq(d, 0, string.format("%s: %s %dx%d pixels", c.name, e.type, e.w, e.h))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
t:eq(if img.meta.skipped then #img.meta.skipped else 0, c.skipped or 0, c.name .. ": skipped entries")
|
||||||
|
return img
|
||||||
|
end
|
||||||
|
for _, c in Fx.cases do
|
||||||
|
check(c)
|
||||||
|
end
|
||||||
|
for _, o in Fx.ours do
|
||||||
|
check(o)
|
||||||
|
end
|
||||||
|
t:note("%d files and our encoder's file match", #Fx.cases)
|
||||||
|
|
||||||
|
do
|
||||||
|
-- PNG entries through the hooks, legacy entries without them
|
||||||
|
local img = Types.Image.new(64, 64)
|
||||||
|
for i = 0, 64 * 64 - 1 do
|
||||||
|
buffer.writeu32(img.pixels, i * 4, i * 2654435761 % 4294967296)
|
||||||
|
end
|
||||||
|
local small = Types.Image.new(16, 16)
|
||||||
|
buffer.fill(small.pixels, 0, 200)
|
||||||
|
img.frames = { { image = img, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" }, { image = small, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } }
|
||||||
|
local enc = Icns.encode(img, hooks) :: buffer
|
||||||
|
local back = Icns.decode(Bytes.reader(enc), hooks) :: any
|
||||||
|
t:check(back and back.width == 64 and buffer.tostring(back.pixels) == buffer.tostring(img.pixels), "PNG entries round trip")
|
||||||
|
t:eq(#back.frames, 2, "two sizes")
|
||||||
|
local _, e = Icns.encode(img)
|
||||||
|
t:check(e and e.code == Err.ARG, "64 x 64 needs the PNG hook")
|
||||||
|
local leg = Icns.encode(small) :: buffer
|
||||||
|
local lb = Icns.decode(Bytes.reader(leg)) :: any
|
||||||
|
t:eq(buffer.tostring(lb.pixels), buffer.tostring(small.pixels), "legacy round trip")
|
||||||
|
local _, e2 = Icns.decode(Bytes.reader(enc))
|
||||||
|
t:check(e2 and e2.code == Err.UNSUPPORTED, "PNG entries without the hook: UNSUPPORTED")
|
||||||
|
t:eq(Icns.sniff(Bytes.reader(enc)), 1, "sniff")
|
||||||
|
local _, e3 = Icns.encode(Types.Image.new(10, 12))
|
||||||
|
t:check(e3 and e3.code == Err.ARG, "not square")
|
||||||
|
end
|
||||||
|
t:fuzz("decode", function(b: buffer)
|
||||||
|
return Icns.decode(Bytes.reader(b), hooks)
|
||||||
|
end, R.samples(Fx.cases), { mutations = 6 })
|
||||||
|
t:done()
|
||||||
@@ -156,3 +156,15 @@ if Fmt then
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if which == "icns" then
|
||||||
|
-- square icons 16 / 32 / 48 / 128 as RLE entries (no encodeImage hook here)
|
||||||
|
local Icns = require("@formats/image/icns")
|
||||||
|
local base = image(128, 128, "rgba8", 1)
|
||||||
|
base.frames = {}
|
||||||
|
for i, s in { 16, 32, 48, 128 } do
|
||||||
|
base.frames[i] = { image = image(s, s, "rgba8", i), delay = 0, x = 0, y = 0, dispose = "none", blend = "source" }
|
||||||
|
end
|
||||||
|
base.pixels = base.frames[4].image.pixels
|
||||||
|
emit("ours_legacy.icns", Icns.encode(base))
|
||||||
|
end
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
# ICNS fixtures (tools/fixtures.py): a libicns png2icns file (is32 / il32 / ih32 RLE entries with their masks, ic07 /
|
||||||
|
# ic08 PNG entries), a JPEG 2000 entry that must be skipped (hand-written into hand.icns), a Pillow file (PNG entries), a hand-written one (ics# / ICN#
|
||||||
|
# 1-bit icons, an ic04 ARGB entry, an uncompressed is32); every entry's reference pixels from icns2png -x (libicns)
|
||||||
|
# or written here; our encoder's RLE file (tools/fixtures/_tex_encode.luau -a icns) read back by icns2png.
|
||||||
|
# tests/gen/icns.luau { cases = { { name, data, entries = { { type, w, h, rgba } }, skipped } }, ours = { ... } }
|
||||||
|
import importlib.util, os, re, struct
|
||||||
|
|
||||||
|
def _load(n):
|
||||||
|
spec = importlib.util.spec_from_file_location(n, os.path.join(os.path.dirname(os.path.abspath(__file__)), n + ".py"))
|
||||||
|
m = importlib.util.module_from_spec(spec)
|
||||||
|
spec.loader.exec_module(m)
|
||||||
|
return m
|
||||||
|
|
||||||
|
R, S, T = _load("_raster_ref"), _load("_simple"), _load("_tex_reference")
|
||||||
|
INPUTS = ["tools/fixtures/_tex_encode.luau", "src/Formats/image/icns"]
|
||||||
|
|
||||||
|
|
||||||
|
def extract(ctx, w, name):
|
||||||
|
"""icns2png -x name -> { 'WxH': rgba } (one per extracted entry)"""
|
||||||
|
log = w.sh("libicns", ["mkdir -p x_%s && cd x_%s && icns2png -x ../%s" % (name, name, name)]).decode()
|
||||||
|
files = sorted(f for f in os.listdir(w.path("x_" + name)) if f.endswith(".png"))
|
||||||
|
out = {}
|
||||||
|
for f, (mode, ww, hh, raw) in zip(files, T.pillow(ctx, [w.read(os.path.join("x_" + name, f)) for f in files])):
|
||||||
|
out["%dx%d" % (ww, hh)] = T.rgba_from(mode, raw)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def generate(ctx):
|
||||||
|
w = R.Work(ctx)
|
||||||
|
try:
|
||||||
|
for s in (16, 32, 48, 128, 256):
|
||||||
|
w.png("i%d.png" % s, s, s, R.photo(s, s, s, True))
|
||||||
|
w.sh("libicns", ["png2icns libicns.icns i16.png i32.png i48.png i128.png i256.png"])
|
||||||
|
w.png("p64.png", 64, 64, R.photo(64, 64, 9, True))
|
||||||
|
ctx.python(["pillow"], "from PIL import Image\nimport os\nos.chdir(%r)\nImage.open('p64.png').save('pillow.icns')\n" % w.dir)
|
||||||
|
cases = []
|
||||||
|
lib = extract(ctx, w, "libicns.icns")
|
||||||
|
cases.append({"name": "libicns.icns", "data": w.read("libicns.icns"), "skipped": 0,
|
||||||
|
"entries": [{"type": t, "w": s, "h": s, "rgba": lib["%dx%d" % (s, s)]} for t, s in (("is32", 16), ("il32", 32), ("ih32", 48), ("ic07", 128), ("ic08", 256))]})
|
||||||
|
pil = extract(ctx, w, "pillow.icns")
|
||||||
|
cases.append({"name": "pillow.icns", "data": w.read("pillow.icns"), "skipped": 0,
|
||||||
|
"entries": [{"type": "", "w": int(k.split("x")[0]), "h": int(k.split("x")[1]), "rgba": v} for k, v in sorted(pil.items())]})
|
||||||
|
# hand-written: ics# (16x16 1-bit + mask), an uncompressed is32 + s8mk, an ic04 ARGB with RLE runs
|
||||||
|
bits = bytes((0xF0 if (i // 2) % 2 else 0x0F) for i in range(32))
|
||||||
|
mask = bytes((0xFF if i < 24 else 0x00) for i in range(32))
|
||||||
|
mono = []
|
||||||
|
for i in range(256):
|
||||||
|
b = (bits[i // 8] >> (7 - i % 8)) & 1
|
||||||
|
m = (mask[i // 8] >> (7 - i % 8)) & 1
|
||||||
|
mono += [0, 0, 0] if b else [255, 255, 255]
|
||||||
|
mono.append(255 if m else 0)
|
||||||
|
raw = bytes((i * 3 + k * 50) % 256 for k in range(3) for i in range(256))
|
||||||
|
alpha = bytes((i * 7) % 256 for i in range(256))
|
||||||
|
rgb = bytes(v for i in range(256) for v in (raw[i], raw[256 + i], raw[512 + i], alpha[i]))
|
||||||
|
def ch(t, d):
|
||||||
|
return t + struct.pack(">I", len(d) + 8) + d
|
||||||
|
# ARGB channels: A = 256 x 77 (two runs of 128), then R / G / B as runs
|
||||||
|
argb = b"ARGB" + bytes((253, 77, 253, 77)) + bytes((253, 10, 253, 10)) + bytes((253, 20, 253, 20)) + bytes((253, 30, 253, 30))
|
||||||
|
jp2 = b"\0\0\0\x0cjP \r\n\x87\n" + b"\0" * 20
|
||||||
|
body = ch(b"ics#", bits + mask) + ch(b"is32", raw) + ch(b"s8mk", alpha) + ch(b"ic04", argb) + ch(b"ic09", jp2)
|
||||||
|
hand = b"icns" + struct.pack(">I", len(body) + 8) + body
|
||||||
|
cases.append({"name": "hand.icns", "data": hand, "skipped": 1, "entries": [
|
||||||
|
{"type": "ics#", "w": 16, "h": 16, "rgba": bytes(mono)}, {"type": "is32", "w": 16, "h": 16, "rgba": rgb},
|
||||||
|
{"type": "ic04", "w": 16, "h": 16, "rgba": bytes((10, 20, 30, 77)) * 256}]})
|
||||||
|
mine = S.ours(ctx, "icns")
|
||||||
|
ours = []
|
||||||
|
for n, d in mine.items():
|
||||||
|
w.write(n, d)
|
||||||
|
ex = extract(ctx, w, n)
|
||||||
|
ours.append({"name": n, "data": d, "entries": [{"type": "", "w": int(k.split("x")[0]), "h": int(k.split("x")[1]), "rgba": v} for k, v in sorted(ex.items())]})
|
||||||
|
ctx.emit("icns", {"cases": cases, "ours": ours})
|
||||||
|
finally:
|
||||||
|
w.close()
|
||||||
Reference in new issue
Block a user