181 lines
7.9 KiB
Luau
181 lines
7.9 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("@rosetta/Types")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local LayerOps = require("@rosetta/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 jstr(s: string): string
|
|
return '"' .. string.gsub(s, '[%c"\\]', function(c: string): string
|
|
return string.format("\\u%04x", string.byte(c))
|
|
end) .. '"'
|
|
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, jstr(k) .. ":" .. json(x))
|
|
end
|
|
return "{" .. table.concat(t, ",") .. "}"
|
|
elseif type(v) == "string" then
|
|
return jstr(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
|
|
|
|
-- an animated sprite (tests/layered/aseprite.luau builds the same one)
|
|
local function aseDoc(): Types.Layered
|
|
local L = Types.Layered
|
|
local doc = L.new(24, 16)
|
|
local bgImg = image(24, 16, 1)
|
|
local shared = image(10, 8, 2, true)
|
|
local frames = {}
|
|
for f = 1, 3 do
|
|
local bg = L.layer("Background", bgImg)
|
|
local mul = L.layer("Mul", if f == 2 then image(10, 8, 7, true) else shared, 3 + (if f == 2 then 1 else 0), 2)
|
|
mul.blend = "multiply"
|
|
local half = L.layer("Half", image(12, 9, 3 + f, true), f * 2, 4)
|
|
half.fillOpacity = 128 / 255
|
|
local g = L.group("Group", { mul, half })
|
|
local top = L.layer("Top", image(6, 6, 9 + f, true), 16 - f, 8)
|
|
top.opacity = 200 / 255
|
|
frames[f] = { layers = { bg, g, top }, delay = 0.05 * f }
|
|
end
|
|
doc.frames = frames
|
|
doc.layers = frames[1].layers
|
|
doc.meta.tags = { { name = "all", from = 1, to = 3, direction = "pingpong", ["repeat"] = 0 } }
|
|
return doc
|
|
end
|
|
|
|
-- a Tiled map (tools/fixtures/tiled.py has Tiled read our TMX and TMJ of it)
|
|
local function tiledMap(): Types.TileMap
|
|
local m: any = Types.TileMap.new(5, 4, 16, 16)
|
|
m.nextLayerId, m.nextObjectId = 5, 4
|
|
m.properties = { name = "ours", n = 7, f = 0.5, ok = false, c = "#ff112233" }
|
|
m.propertyTypes = { c = "color" }
|
|
m.tilesets = { { firstGid = 1, name = "tiles", tileWidth = 16, tileHeight = 16, columns = 4, count = 8, spacing = 0, margin = 0, imageSource = "tiles.png", imageWidth = 64, imageHeight = 32, properties = {}, tiles = { { id = 2, class = "Wall", properties = { hard = true } }, { id = 4, properties = {}, animation = { { tileId = 4, duration = 120 }, { tileId = 5, duration = 80 } } } } } }
|
|
local data = {}
|
|
for i = 1, 20 do
|
|
data[i] = (i * 3) % 9
|
|
end
|
|
data[2] += 0x80000000
|
|
data[10] += 0x40000000
|
|
local tiles = { type = "tiles", id = 1, name = "ground", width = 5, height = 4, data = data, visible = true, opacity = 0.8, offsetX = 2, offsetY = 0, properties = { deep = 3 }, encoding = "base64", compression = "zlib" }
|
|
local objs = {
|
|
{ id = 1, name = "a", class = "Spawn", x = 4, y = 5.5, width = 8, height = 8, rotation = 30, visible = true, shape = "rectangle", properties = { team = "red" } },
|
|
{ id = 2, name = "b", class = "", x = 20, y = 10, width = 0, height = 0, rotation = 0, visible = true, shape = "polyline", points = { { 0, 0 }, { 8, 4 }, { 16, 0 } }, properties = {} },
|
|
{ id = 3, name = "c", class = "", x = 30, y = 30, width = 60, height = 14, rotation = 0, visible = false, shape = "text", text = { text = "hi there", fontFamily = "sans-serif", pixelSize = 14, wrap = true, color = "#00ff00", bold = false, italic = true, underline = false, strikeout = false, kerning = true, halign = "right", valign = "top" }, properties = {} },
|
|
}
|
|
local og = { type = "objects", id = 3, name = "things", visible = true, opacity = 1, drawOrder = "topdown", objects = objs, properties = {} }
|
|
local img = { type = "image", id = 4, name = "sky", visible = true, opacity = 1, imageSource = "sky.png", imageWidth = 40, imageHeight = 20, repeatX = true, repeatY = false, properties = {} }
|
|
local group = { type = "group", id = 2, name = "grp", visible = true, opacity = 0.5, layers = { og, img }, properties = {} }
|
|
m.layers = { tiles, group }
|
|
return m
|
|
end
|
|
|
|
local which, arg = ...
|
|
if which == "psd" then
|
|
local Psd = require("@rosetta/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 }))
|
|
elseif which == "aseprite" then
|
|
local Ase = require("@rosetta/layered/aseprite")
|
|
local doc = aseDoc()
|
|
local data = Ase.encode(doc) :: buffer
|
|
local frames = {}
|
|
for f = 1, 3 do
|
|
frames[f] = hex((LayerOps.flatten(doc, { frame = f }) :: Types.Image).pixels)
|
|
end
|
|
print(json({ data = hex(data), nframes = 3, frames = frames }))
|
|
elseif which == "tiled" then
|
|
local Tiled = require("@rosetta/vector/tiled")
|
|
local m = tiledMap()
|
|
local tmx = Tiled.encode(m, { format = "tmx" }) :: buffer
|
|
local tmj = Tiled.encode(m, { format = "tmj" }) :: buffer
|
|
print(json({ tmx = hex(tmx), tmj = hex(tmj) }))
|
|
elseif which == "svg" then
|
|
-- the SVG text given on the command line, decoded and written back by our encoder
|
|
local Svg = require("@rosetta/vector/svg")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local v = Svg.decode(Bytes.reader(buffer.fromstring(arg)))
|
|
local out = if v then Svg.encode(v) else nil
|
|
print(json({ svg = if out then buffer.tostring(out) else "" }))
|
|
end
|