-- image/art tests against arttool (eduke32 tools) and an independent Python reading (tools/fixtures/art.py): indices, -- picanm fields, colours through PALETTE.DAT as arttool exports them, shade / palookup remaps, write round trips. local T = require("@tests/T") local Art = require("@rosetta/image/art") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Fx = require("@tests/gen/art") local t = T.suite("art") t:eq(Art.name, "art", "name") t:check(Art.caps.decode and Art.caps.encode, "caps") local data = buffer.fromstring(Fx.art) t:eq(Art.sniff(Bytes.reader(data)), 0.9, "sniff") t:eq(Art.sniff(Bytes.reader(buffer.fromstring("GIF89a........................"))), 0, "sniff other") local TYPES = { [0] = "none", "oscillate", "forward", "backward" } local function s8(v: number): number return if v >= 128 then v - 256 else v end local nat = assert(Art.decode(Bytes.reader(data), { native = true })) local img = assert(Art.decode(Bytes.reader(data), { palette = Fx.palette })) t:eq(#(nat.frames :: any), Fx.last - Fx.start + 1, "one frame per tile") t:eq(img.meta.source.tileStart, Fx.start, "tileStart") t:eq(img.meta.source.tileEnd, Fx.last, "tileEnd") t:check(img.meta.source.palette, "palette used") t:eq(img.meta.shades.count, 32, "shade tables") t:check(img.meta.translucency and buffer.len(img.meta.translucency) == 65536, "translucency table") for i, c in Fx.tiles do local fn = (nat.frames :: any)[i].image local fr = (img.frames :: any)[i].image local label = "tile " .. c.tile t:eq(fn.meta.tile, c.tile, label .. ": number") t:eq(fn.width, c.w, label .. ": width") t:eq(fn.height, c.h, label .. ": height") local p = fn.meta.picanm t:eq(p.frames, c.animFrames, label .. ": anim frames") t:eq(p.type, TYPES[c.animType], label .. ": anim type") t:eq(p.speed, c.animSpeed, label .. ": anim speed") t:eq(p.xoffset, s8(c.xofs), label .. ": x offset") t:eq(p.yoffset, s8(c.yofs), label .. ": y offset") t:eq(Art.picanmWord(p), c.picanm, label .. ": picanm word") if c.idx then t:eq(buffer.tostring(fn.pixels), c.idx, label .. ": indices") local bad = 0 for k = 0, c.w * c.h - 1 do local idx = string.byte(c.idx, k + 1) local px = fr.pixels if idx == 255 then if buffer.readu32(px, k * 4) ~= 0 then bad += 1 end else for ch = 0, 2 do if buffer.readu8(px, k * 4 + ch) ~= string.byte(c.rgb, k * 3 + ch + 1) then bad += 1 end end if buffer.readu8(px, k * 4 + 3) ~= 255 then bad += 1 end end end t:eq(bad, 0, label .. ": colours as arttool exports them") end end t:eq(img.width, Fx.tiles[1].w, "top-level image = first non-empty tile") -- shade and palookup remaps (index 255 stays transparent) do local resolve = function(name: string): buffer? if name == "PALETTE.DAT" then return buffer.fromstring(Fx.palette) elseif name == "LOOKUP.DAT" then return buffer.fromstring(Fx.lookup) end return nil end local sh = assert(Art.decode(Bytes.reader(data), { resolve = resolve, native = true, shade = Fx.shade.n, pal = Fx.remap.pal })) t:check(sh.meta.lookups[1] and sh.meta.lookups[5], "palookups 1 and 5") t:eq(#sh.meta.altPalettes, 5, "alternate palettes") local c = Fx.tiles[1] local fr = (sh.frames :: any)[1].image local bad = 0 for k = 0, c.w * c.h - 1 do local idx = string.byte(c.idx, k + 1) local want = if idx == 255 then 255 else string.byte(Fx.shade.table, string.byte(Fx.remap.table, idx + 1) + 1) if buffer.readu8(fr.pixels, k) ~= want then bad += 1 end end t:eq(bad, 0, "palookup then shade") local _, e = Art.decode(Bytes.reader(data), { resolve = resolve, pal = 9 }) t:check(e and e.code == Err.ARG, "missing palookup") local grey = assert(Art.decode(Bytes.reader(data))) t:eq(grey.meta.source.palette, false, "no palette: grey ramp") end -- write: native round trip is byte exact; rgba8 maps back through the palette do local again = assert(Art.encode(nat)) t:eq(buffer.tostring(again), Fx.art, "native round trip") local again2 = assert(Art.encode(img)) t:eq(buffer.tostring(again2), Fx.art, "rgba8 round trip through the palette") local pref = assert(Art.encode(nat, { buildart = true })) t:eq(buffer.readstring(pref, 0, 8), "BUILDART", "BUILDART prefix") local back = assert(Art.decode(Bytes.reader(pref), { native = true })) t:check(back.meta.source.buildart, "prefix read") t:eq(buffer.tostring(assert(Art.encode(back, { buildart = false }))), Fx.art, "prefix round trip") -- a single image becomes one tile local one = assert(Art.encode((img.frames :: any)[4].image, { palette = img.meta.palette, tileStart = 100 })) local d1 = assert(Art.decode(Bytes.reader(one), { native = true })) t:eq(d1.meta.source.tileStart, 100, "single tile number") t:eq(buffer.tostring(d1.pixels), Fx.tiles[4].idx, "single tile indices") local _, e = Art.encode((img.frames :: any)[1].image) t:check(e and e.code == Err.ARG, "rgba8 without a palette") end -- errors do local _, e = Art.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.art, 1, 10)))) t:check(e and e.code == Err.TRUNCATED, "short header") local _, e2 = Art.decode(Bytes.reader(data), { maxPixels = 10 }) t:check(e2 and e2.code == Err.LIMIT, "limit") local cut = assert(Art.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.art, 1, #Fx.art - 100))))) t:check(cut.meta.truncated, "truncated pixels flagged") end t:fuzz("decode", function(b: buffer) return Art.decode(Bytes.reader(b), { palette = Fx.palette }) end, { Fx.art }, { mutations = 300 }) t:done()