C2, the hierarchy (tools/move.py, every reference rewritten mechanically): - codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder; codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url; codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err Crc32 Adler32 Base64 Md5 Input Sniff. - LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md). - Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes. C3, generic code out of format folders: - containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first, trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64 through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it. - codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position, priming and end trimming) that aac, caf and m4a each carried (~70 lines each). - codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of its own (the AND mask / alpha rule stay ICO's). - codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model), Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the fixtures' bind shapes). - README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes: FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.
145 lines
6.4 KiB
Luau
145 lines
6.4 KiB
Luau
-- ops/LayerOps + codecs/image/Blend tests: blend mode formulas on single pixels against hand-computed W3C values, opacity,
|
|
-- masks, clipping, isolated vs pass-through groups, hidden layers, frames, leaves, bad arguments. (Whole-document
|
|
-- flattening against psd-tools is in tests/layered/psd.luau.)
|
|
local T = require("@tests/T")
|
|
local LayerOps = require("@formats/ops/LayerOps")
|
|
local Blend = require("@formats/codecs/image/Blend")
|
|
local Types = require("@formats/Types")
|
|
local Err = require("@formats/core/Err")
|
|
|
|
local t = T.suite("layerops")
|
|
|
|
local function px(r: number, g: number, b: number, a: number): Types.Image
|
|
local img = Types.Image.new(1, 1)
|
|
buffer.writeu8(img.pixels, 0, r)
|
|
buffer.writeu8(img.pixels, 1, g)
|
|
buffer.writeu8(img.pixels, 2, b)
|
|
buffer.writeu8(img.pixels, 3, a)
|
|
return img
|
|
end
|
|
local function get(img: any): (number, number, number, number)
|
|
local p = img.pixels
|
|
return buffer.readu8(p, 0), buffer.readu8(p, 1), buffer.readu8(p, 2), buffer.readu8(p, 3)
|
|
end
|
|
local function doc(layers: { any }): any
|
|
local d = Types.Layered.new(1, 1)
|
|
d.layers = layers
|
|
return d
|
|
end
|
|
local L = Types.Layered.layer
|
|
|
|
-- separable modes on opaque pixels: backdrop (200, 100, 50), source (100, 150, 250); values from the W3C formulas
|
|
local cases = {
|
|
normal = { 100, 150, 250 },
|
|
multiply = { 78, 59, 49 },
|
|
screen = { 222, 191, 251 },
|
|
darken = { 100, 100, 50 },
|
|
lighten = { 200, 150, 250 },
|
|
difference = { 100, 50, 200 },
|
|
exclusion = { 143, 132, 202 },
|
|
linearDodge = { 255, 250, 255 },
|
|
linearBurn = { 45, 0, 45 },
|
|
subtract = { 100, 0, 0 },
|
|
overlay = { 188, 118, 98 },
|
|
hardLight = { 157, 127, 247 },
|
|
colorDodge = { 255, 243, 255 },
|
|
colorBurn = { 115, 0, 46 },
|
|
hardMix = { 255, 0, 255 },
|
|
}
|
|
for mode, want in cases do
|
|
local top = L("top", px(100, 150, 250, 255))
|
|
top.blend = mode
|
|
local img = LayerOps.flatten(doc({ L("bottom", px(200, 100, 50, 255)), top })) :: any
|
|
local r, g, b, a = get(img)
|
|
t:check(math.abs(r - want[1]) <= 1 and math.abs(g - want[2]) <= 1 and math.abs(b - want[3]) <= 1 and a == 255, string.format("%s: got %d %d %d, want %d %d %d", mode, r, g, b, want[1], want[2], want[3]))
|
|
end
|
|
-- non-separable: luminosity of the source on the backdrop's hue
|
|
do
|
|
local top = L("top", px(255, 255, 255, 255))
|
|
top.blend = "luminosity"
|
|
local r, g, b = get(LayerOps.flatten(doc({ L("bottom", px(255, 0, 0, 255)), top })))
|
|
t:check(r == 255 and g == 255 and b == 255, "luminosity of white")
|
|
top.blend = "color"
|
|
local r2, g2, b2 = get(LayerOps.flatten(doc({ L("bottom", px(128, 128, 128, 255)), top })))
|
|
t:check(r2 == g2 and g2 == b2 and math.abs(r2 - 128) <= 1, "colour of a grey source keeps the grey")
|
|
end
|
|
-- opacity and alpha: 50% red over opaque blue
|
|
do
|
|
local top = L("top", px(255, 0, 0, 255))
|
|
top.opacity = 0.5
|
|
local r, g, b, a = get(LayerOps.flatten(doc({ L("bottom", px(0, 0, 255, 255)), top })))
|
|
t:check(math.abs(r - 128) <= 1 and g == 0 and math.abs(b - 128) <= 1 and a == 255, "opacity mix")
|
|
local r2, _, _, a2 = get(LayerOps.flatten(doc({ top })))
|
|
t:check(r2 == 255 and math.abs(a2 - 128) <= 1, "over transparent: alpha scales")
|
|
end
|
|
-- hidden layers, masks, clipping
|
|
do
|
|
local top = L("top", px(255, 0, 0, 255))
|
|
top.visible = false
|
|
local r = get(LayerOps.flatten(doc({ L("bottom", px(0, 0, 255, 255)), top })))
|
|
t:eq(r, 0, "hidden layer skipped")
|
|
local r2 = get(LayerOps.flatten(doc({ L("bottom", px(0, 0, 255, 255)), top }), { hidden = true }))
|
|
t:eq(r2, 255, "hidden = true includes it")
|
|
top.visible = true
|
|
local m = Types.Image.new(1, 1, "gray8")
|
|
top.mask = { image = m, x = 0, y = 0, default = 255 }
|
|
t:eq((get(LayerOps.flatten(doc({ L("bottom", px(0, 0, 255, 255)), top })))), 0, "black mask hides")
|
|
top.mask = { image = m, x = 5, y = 5, default = 255 }
|
|
t:eq((get(LayerOps.flatten(doc({ L("bottom", px(0, 0, 255, 255)), top })))), 255, "outside the mask: default white")
|
|
top.mask = nil
|
|
local base = L("base", px(0, 255, 0, 0)) -- transparent base: the clipped layer shows nowhere
|
|
top.clip = true
|
|
local r3, g3, b3, a3 = get(LayerOps.flatten(doc({ base, top })))
|
|
t:check(a3 == 0, "clipped to a transparent base: " .. r3 .. g3 .. b3 .. a3)
|
|
base = L("base", px(0, 255, 0, 255))
|
|
local r4, g4 = get(LayerOps.flatten(doc({ base, top })))
|
|
t:check(r4 == 255 and g4 == 0, "clipped to an opaque base")
|
|
end
|
|
-- groups: an isolated multiply group multiplies its composite; pass-through multiplies each child onto the backdrop
|
|
do
|
|
local child = L("child", px(128, 128, 128, 255))
|
|
child.blend = "multiply"
|
|
local g = Types.Layered.group("g", { child })
|
|
local bottom = L("bottom", px(200, 200, 200, 255))
|
|
local r = get(LayerOps.flatten(doc({ bottom, g })))
|
|
t:check(math.abs(r - 100) <= 1, "pass-through: child multiplies the backdrop: " .. r)
|
|
g.blend = "normal"
|
|
local r2 = get(LayerOps.flatten(doc({ bottom, g })))
|
|
t:eq(r2, 128, "isolated group: the child multiplies nothing")
|
|
g.blend = "passThrough"
|
|
g.opacity = 0.5
|
|
local r3 = get(LayerOps.flatten(doc({ bottom, g })))
|
|
t:check(math.abs(r3 - 150) <= 1, "pass-through opacity mixes before / after: " .. r3)
|
|
end
|
|
-- frames, leaves, flattenFrames
|
|
do
|
|
local d = doc({ L("a", px(1, 2, 3, 255)) })
|
|
d.frames = { { layers = d.layers, delay = 0.1 }, { layers = { L("b", px(9, 8, 7, 255)) }, delay = 0.2 } }
|
|
t:eq((get(LayerOps.flatten(d, { frame = 2 }))), 9, "frame 2")
|
|
local anim = LayerOps.flattenFrames(d) :: any
|
|
t:check(anim.frames and #anim.frames == 2 and anim.frames[2].delay == 0.2, "flattenFrames")
|
|
local _, e = LayerOps.flatten(d, { frame = 5 })
|
|
t:check(e and e.code == Err.ARG, "missing frame")
|
|
local names = {}
|
|
for l, depth in LayerOps.leaves({ L("x", px(0, 0, 0, 0)), Types.Layered.group("g", { L("y", px(0, 0, 0, 0)), Types.Layered.group("h", { L("z", px(0, 0, 0, 0)) }) }) }) do
|
|
table.insert(names, (l :: any).name .. depth)
|
|
end
|
|
t:eq(table.concat(names, " "), "x0 y1 z2", "leaves with depth")
|
|
local _, e2 = LayerOps.flatten(5 :: any)
|
|
t:check(e2 and e2.code == Err.ARG, "flatten of a non-Layered")
|
|
end
|
|
-- Blend.composite clips its rectangles (no throw, nothing outside)
|
|
do
|
|
local dst = buffer.create(4 * 4 * 4)
|
|
local src = buffer.create(3 * 3 * 4)
|
|
buffer.fill(src, 0, 255)
|
|
for _, pos in { { -2, -2 }, { 3, 3 }, { -10, 0 }, { 100, 100 } } do
|
|
Blend.composite(dst, 4, 4, src, 3, 3, pos[1], pos[2], 1, "multiply")
|
|
end
|
|
Blend.composite(dst, 4, 4, src, 3, 3, 0, 0, 1, "nope")
|
|
Blend.composite(dst, 4, 4, src, 30, 30, 0, 0, 1, "normal") -- src too short: ignored
|
|
t:eq(buffer.readu8(dst, 0), 255, "composite clipping")
|
|
t:check(Blend.MODES.softLight and not Blend.MODES.nope, "MODES")
|
|
end
|
|
t:done()
|