-- layered/psd tests: PSD / PSB files of every colour mode and depth, compression, groups, pass-through, clipping, -- masks, fill opacity and blend mode (written by tools/fixtures/psd.py) plus an ImageMagick PSD, against psd-tools: -- the merged image and every layer's pixels exact (8-bit; 16 / 32-bit within rounding), our flatten (ops/LayerOps) -- against psd-tools' composite by PSNR; our encoder's PSD read back by psd-tools (tools/fixtures/_ours.luau), round -- trips, the contract and the fuzz pass. Fixtures: tools/fixtures/psd.py. local T = require("@tests/T") local Psd = require("@formats/layered/psd") local LayerOps = require("@formats/ops/LayerOps") local Bytes = require("@formats/core/Bytes") local Crc32 = require("@formats/core/Crc32") local Err = require("@formats/core/Err") local Fx = require("@tests/gen/psd") local Ours = require("@tests/gen/psd_ours") local t = T.suite("psd") local function decode(s: string | buffer, opts: any?): (any, any) local b = if type(s) == "string" then buffer.fromstring(s) else s return Psd.decode(Bytes.reader(b), opts) end -- PSNR of two rgba8 buffers, colours premultiplied by alpha (the colour of a transparent pixel does not matter); -- identical -> 99 local function psnr(a: buffer, b: string, opaque: boolean?): number local n = math.min(buffer.len(a), #b) local se = 0 for i = 0, n - 1, 4 do local aa, ab = buffer.readu8(a, i + 3), string.byte(b, i + 4) if opaque then aa, ab = 255, 255 end for k = 0, 2 do local d = (buffer.readu8(a, i + k) * aa - string.byte(b, i + k + 1) * ab) / 255 se += d * d end se += (aa - ab) * (aa - ab) end if se == 0 then return 99 end return 10 * math.log10(255 * 255 / (se / n)) end local function leaves(layers: any): { any } local out = {} for l in LayerOps.leaves(layers) do table.insert(out, l) end return out end t:eq(Psd.name, "psd", "name") t:eq(Psd.category, "layered", "category") t:check(Psd.caps.decode and Psd.caps.encode, "caps") -- PSNR floors for our flatten vs psd-tools' composite: blend modes where psd-tools' formulas differ from the W3C ones -- (Photoshop's own differ from both) get a lower bar local LOW = { ["blend-diss"] = 8, ["blend-hMix"] = 15, ["blend-sLit"] = 30, ["blend-vLit"] = 25, ["blend-lLit"] = 30, ["blend-pLit"] = 30, ["blend-div"] = 30, ["blend-idiv"] = 30, ["blend-fdiv"] = 25, ["blend-hue"] = 25, ["blend-sat"] = 25, ["blend-colr"] = 25, ["blend-lum"] = 25 } local samples = {} for _, c in Fx.cases do local doc, err = decode(c.data) t:check(doc, c.name .. ": " .. tostring(err)) t:eq(doc.kind, "layered", c.name .. ": kind") t:check(doc.width == c.width and doc.height == c.height, c.name .. ": size") local comp = doc.meta.composite if #c.composite > 0 then local src = doc.meta.source local exact = (src.bitDepth == 8 or src.bitDepth == 1) and src.colorMode ~= "lab" and src.colorMode ~= "cmyk" if exact then t:check(buffer.tostring(comp.pixels) == c.composite, c.name .. ": merged image = psd-tools") else -- (PIL turns Lab into RGBA with alpha 0: compare the colours only) local p = psnr(comp.pixels, c.composite, src.colorMode == "lab") -- 16 / 32 bits: rounding; CMYK / Lab: different conversions (ours: naive CMYK, D65 Lab) local floor = if src.colorMode == "lab" then 20 elseif src.colorMode == "cmyk" then 30 else 45 t:check(p > floor, string.format("%s: merged image PSNR %.1f", c.name, p)) t:note("%s: merged image vs psd-tools %.1f dB", c.name, p) end end -- layers, in order, against psd-tools local ours = leaves(doc.layers) local ref = {} for _, l in c.layers do if not l.group then table.insert(ref, l) end end t:eq(#ours, #ref, c.name .. ": leaf count") for i, r in ref do local l = ours[i] t:eq(l.name, r.name, c.name .. ": layer name") if r.width > 0 then t:check(l.x == r.left and l.y == r.top and l.image.width == r.width and l.image.height == r.height, c.name .. ": " .. r.name .. ": bounds") end if r.crc and doc.meta.source.bitDepth == 8 then t:eq(Crc32.of(l.image.pixels), r.crc, c.name .. ": " .. r.name .. ": pixels") end end -- our flatten vs psd-tools' composite if c.flat and #ours > 0 then local flat = LayerOps.flatten(doc) :: any local p = psnr(flat.pixels, c.flat) local floor = LOW[c.name] or 38 t:check(p >= floor, string.format("%s: flatten PSNR %.1f dB (floor %d)", c.name, p, floor)) if p < 99 then t:note("%s: flatten vs psd-tools %.1f dB", c.name, p) end end if #samples < 8 and (c.name == "rgb-layers" or c.name == "psb" or c.name == "rgb16-lr16" or c.name == "gray" or c.name == "indexed" or c.name == "bitmap" or c.name == "rgb32" or c.name == "cmyk") then table.insert(samples, c.data) end end -- the full document's structure do local doc for _, c in Fx.cases do if c.name == "rgb-layers" then doc = decode(c.data) end end local L = doc.layers t:eq(#L, 8, "top level: bg, multiply, isolated, pass, base, clipped, masked, fill") t:eq(L[2].blend, "multiply", "blend") t:eq(math.floor(L[2].opacity * 255 + 0.5), 200, "opacity") t:eq(L[3].name, "Isolated", "group name") t:eq(L[3].blend, "normal", "isolated group blend") t:eq(#L[3].children, 2, "group children") t:eq(L[4].blend, "passThrough", "pass-through group") t:eq(L[4].children[1].name, "Diff \u{e9}\u{65e5}", "unicode name (luni)") t:eq(L[4].children[2].visible, false, "hidden layer") t:eq(L[6].clip, true, "clipping layer") t:check(L[7].mask and L[7].mask.image.format == "gray8" and L[7].mask.x == 8 and L[7].mask.y == 5, "mask") t:eq(math.floor(L[8].fillOpacity * 255 + 0.5), 128, "fill opacity") local only = decode(Fx.cases[1].data, { layers = false }) t:check(only and #only.layers == 0 and only.meta.composite, "layers = false: merged image only") -- round trip: our PSD of the decoded document decodes to the same layers, and its merged image is our flatten local out, eerr = Psd.encode(doc) t:check(out, "encode: " .. tostring(eerr)) local back = decode(out :: buffer) t:check(back, "decode our PSD") local a, b = leaves(doc.layers), leaves(back.layers) t:eq(#b, #a, "round trip: leaves") for i, l in a do t:eq(b[i].name, l.name, "rt name") t:check(buffer.tostring(b[i].image.pixels) == buffer.tostring(l.image.pixels), "rt pixels " .. l.name) t:check(b[i].x == l.x and b[i].y == l.y, "rt position") t:eq(b[i].blend, l.blend, "rt blend") t:eq(b[i].visible, l.visible, "rt visible") t:eq(b[i].clip, l.clip, "rt clip") t:eq(b[i].opacity, l.opacity, "rt opacity") end t:eq(back.layers[4].blend, "passThrough", "rt pass-through group") t:check(back.layers[7].mask and buffer.tostring(back.layers[7].mask.image.pixels) == buffer.tostring(L[7].mask.image.pixels), "rt mask") local flat = LayerOps.flatten(doc) :: any -- the merged image is stored matted with white (as Photoshop does): exact where opaque, rounding elsewhere local p = psnr(back.meta.composite.pixels, buffer.tostring(flat.pixels)) t:check(p > 45, string.format("rt merged = flatten (%.1f dB)", p)) table.insert(samples, buffer.tostring(out :: buffer)) end -- psd-tools reads our encoder's PSD (checked when the fixtures were made) for _, c in Ours.checks do t:check(c.ok, "psd-tools on our PSD: " .. c.label) end t:note("psd-tools agrees with our PSD on %d facts", #Ours.checks) -- errors do local _, e = decode("8BPS\0\1\0\0\0\0\0\0\0\3\0\0\0\10\0\0\0\10\0\8\0\3") t:check(e and e.code == Err.TRUNCATED, "truncated after the header: " .. tostring(e)) local _, e2 = decode("8BPS\0\1\0\0\0\0\0\0\0\3\0\0\0\10\0\0\0\10\0\7\0\3") t:check(e2 and e2.code == Err.CORRUPT, "bad depth: " .. tostring(e2)) local _, e3 = decode(Fx.cases[1].data, { maxPixels = 100 }) t:check(e3 and e3.code == Err.LIMIT, "maxPixels") t:eq(Psd.sniff(Bytes.reader(buffer.fromstring("8BPS\0\1"))), 1, "sniff") t:eq(Psd.sniff(Bytes.reader(buffer.fromstring("8BPS\0\3"))), 0, "sniff version 3") local _, e4 = Psd.encode({ width = 4, height = 4, layers = { { name = "x" } } } :: any) t:check(e4 and e4.code == Err.ARG, "encode: layer without an image") end t:fuzz("decode", function(b: buffer) return Psd.decode(Bytes.reader(b)) end, samples, { mutations = 30, keepHeader = 4 }) t:done()