Files
rosetta/tools/fixtures/_ours.luau
T

103 lines
3.7 KiB
Luau

-- tools/fixtures/_ours.luau: our encoders' output for the reference tools to read (run by tools/fixtures/*.py through
-- `luau tools/fixtures/_ours.luau -a <format>`). Prints one JSON object: { "data": hex of the encoded file, ... the
-- facts the python side checks the reference's reading against }.
local Types = require("@formats/Types")
local Crc32 = require("@formats/core/Crc32")
local LayerOps = require("@formats/ops/LayerOps")
local function hex(b: buffer): string
local t = table.create(buffer.len(b))
for i = 0, buffer.len(b) - 1 do
t[i + 1] = string.format("%02x", buffer.readu8(b, i))
end
return table.concat(t)
end
local function json(v: any): string
if type(v) == "table" then
if #v > 0 or next(v) == nil then
local t = {}
for i, x in v do
t[i] = json(x)
end
return "[" .. table.concat(t, ",") .. "]"
end
local t = {}
for k, x in v do
table.insert(t, string.format("%q", k) .. ":" .. json(x))
end
return "{" .. table.concat(t, ",") .. "}"
elseif type(v) == "string" then
return string.format("%q", v)
elseif type(v) == "boolean" then
return if v then "true" else "false"
end
return tostring(v)
end
-- a deterministic rgba8 image
local function image(w: number, h: number, seed: number, alpha: boolean?): Types.Image
local img = Types.Image.new(w, h)
for y = 0, h - 1 do
for x = 0, w - 1 do
local o = (y * w + x) * 4
buffer.writeu8(img.pixels, o, (x * (seed + 3) * 7 + y * 5) % 256)
buffer.writeu8(img.pixels, o + 1, (y * (seed + 1) * 9 + x) % 256)
buffer.writeu8(img.pixels, o + 2, ((x + seed) * (y + 1) * 3) % 256)
buffer.writeu8(img.pixels, o + 3, if alpha and (x + y) % 3 == 0 then (x * 40 + seed) % 256 else 255)
end
end
return img
end
local function gray(w: number, h: number): Types.Image
local img = Types.Image.new(w, h, "gray8")
for i = 0, w * h - 1 do
buffer.writeu8(img.pixels, i, (i * 13) % 256)
end
return img
end
-- the Layered document tests/layered/psd.luau also builds (keep them in step)
local function psdDoc(): Types.Layered
local L = Types.Layered
local doc = L.new(36, 24)
local bg = L.layer("Background", image(36, 24, 1))
local mul = L.layer("Multiply", image(20, 12, 2, true), 4, 3)
mul.blend = "multiply"
mul.opacity = 200 / 255
local scr = L.layer("Screen", image(18, 18, 3, true), 12, 4)
scr.blend = "screen"
local iso = L.group("Isolated", { scr })
iso.blend = "normal"
iso.opacity = 180 / 255
local hid = L.layer("Hidden", image(8, 8, 4), 1, 1)
hid.visible = false
local diff = L.layer("Diff \u{e9}\u{65e5}", image(14, 10, 5, true), 18, 12)
diff.blend = "difference"
local pass = L.group("Pass", { hid, diff })
local base = L.layer("Base", image(10, 8, 6), 24, 14)
local clip = L.layer("Clipped", image(14, 12, 7, true), 20, 10)
clip.clip = true
local masked = L.layer("Masked", image(16, 10, 8), 2, 12)
masked.mask = { image = gray(12, 8), x = 4, y = 13, default = 0 }
local fill = L.layer("Fill", image(8, 24, 9, true), 28, 0)
fill.fillOpacity = 128 / 255
fill.blend = "lighten"
doc.layers = { bg, mul, iso, pass, base, clip, masked, fill }
return doc
end
local which = ...
if which == "psd" then
local Psd = require("@formats/layered/psd")
local doc = psdDoc()
local data = Psd.encode(doc) :: buffer
local flat = LayerOps.flatten(doc) :: Types.Image
local layers = {}
for l, depth in LayerOps.leaves(doc.layers) do
local lay = l :: any
table.insert(layers, { name = lay.name, crc = Crc32.of(lay.image.pixels), left = lay.x, top = lay.y, width = lay.image.width, height = lay.image.height, blend = lay.blend, visible = lay.visible, clip = lay.clip == true, opacity = math.floor(lay.opacity * 255 + 0.5), depth = depth })
end
print(json({ data = hex(data), composite = Crc32.of(flat.pixels), layers = layers }))
end