-- tools/fixtures/_tex_encode.luau: our texture encoders' output for the reference tools to read (run by -- tools/fixtures/{dds,ktx,exr}.py as `luau tools/fixtures/_tex_encode.luau -a `). Prints "name\thex" lines. -- The source images are generated here (tools/fixtures/_tex_reference.py makes the same ones: source()). local Types = require("@formats/Types") local which = ({ ... } :: { string })[1] or "dds" 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 -- the test pattern (x, y, channel) -> 0..1, the same formula as _tex_reference.source() local function value(x: number, y: number, c: number, w: number, h: number, salt: number): number if c == 0 then return x / math.max(1, w - 1) elseif c == 1 then return y / math.max(1, h - 1) elseif c == 2 then return ((x * 7 + y * 13 + salt * 31) % 64) / 63 end return ((x + y + salt) % 5) / 4 end local function image(w: number, h: number, fmt: string, salt: number, scale: number?): Types.Image local img = Types.Image.new(w, h, fmt :: any) for y = 0, h - 1 do for x = 0, w - 1 do for c = 0, 3 do local v = value(x, y, c, w, h, salt) local o = (y * w + x) * 4 + c if fmt == "rgba8" then buffer.writeu8(img.pixels, o, math.floor(v * 255 + 0.5)) elseif fmt == "rgba16" then buffer.writeu16(img.pixels, o * 2, math.floor(v * 65535 + 0.5)) else buffer.writef32(img.pixels, o * 4, if c < 3 then v * (scale or 1) else v) end end end end return img end local function emit(name: string, b: buffer?, err: any?) if not b then error(name .. ": " .. tostring(err)) end print(name .. "\t" .. hex(b)) end if which == "dds" then local Dds = require("@formats/image/dds") local img = image(37, 23, "rgba8", 0) for _, f in { "rgba8", "bgra8", "bc1", "bc3", "bc4", "bc5" } do emit("ours_" .. f .. ".dds", Dds.encode(img, { pixelFormat = f :: any })) emit("ours_" .. f .. "_mips.dds", Dds.encode(img, { pixelFormat = f :: any, mips = true })) end emit("ours_rgba8_dx10.dds", Dds.encode(img, { dx10 = true, mips = true })) emit("ours_rgba16.dds", Dds.encode(image(37, 23, "rgba16", 0), { mips = true })) emit("ours_rgba32f.dds", Dds.encode(image(37, 23, "rgbaf32", 0, 4), { mips = true })) emit("ours_rgba16f.dds", Dds.encode(image(37, 23, "rgbaf32", 0, 4), { pixelFormat = "rgba16f" })) local cube = image(16, 16, "rgba8", 0) cube.frames = {} for i = 1, 6 do cube.frames[i] = { image = image(16, 16, "rgba8", i), delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } end cube.meta.layout = "cube" emit("ours_cube.dds", Dds.encode(cube, { mips = true })) cube.meta.layout = "array" emit("ours_array_bc1.dds", Dds.encode(cube, { pixelFormat = "bc1" })) end if which == "ktx" then local Ktx = require("@formats/image/ktx") local img = image(37, 23, "rgba8", 0) emit("ours_rgba8.ktx2", Ktx.encode(img)) emit("ours_rgba8_mips.ktx2", Ktx.encode(img, { mips = true })) emit("ours_srgb_zlib.ktx2", Ktx.encode(img, { mips = true, srgb = true, zlib = 6 })) emit("ours_rgba16.ktx2", Ktx.encode(image(37, 23, "rgba16", 0), { mips = true })) emit("ours_rgba32f.ktx2", Ktx.encode(image(37, 23, "rgbaf32", 0, 4), { mips = true })) emit("ours_rgba16f.ktx2", Ktx.encode(image(37, 23, "rgbaf32", 0, 4), { pixelFormat = "rgba16f" })) local cube = image(16, 16, "rgba8", 0) cube.frames = {} for i = 1, 6 do cube.frames[i] = { image = image(16, 16, "rgba8", i), delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } end cube.meta.layout = "cube" emit("ours_cube.ktx2", Ktx.encode(cube, { mips = true })) cube.meta.layout = "array" emit("ours_array.ktx2", Ktx.encode(cube)) end if which == "exr" then local Exr = require("@formats/image/exr") local f = image(37, 23, "rgbaf32", 0, 4) for _, c in { "none", "zips", "zip" } do emit("ours_half_" .. c .. ".exr", Exr.encode(f, { compression = c :: any })) emit("ours_float_" .. c .. ".exr", Exr.encode(f, { compression = c :: any, pixelType = "float" })) end local opaque = image(20, 40, "rgbaf32", 1, 2) for i = 0, 20 * 40 - 1 do buffer.writef32(opaque.pixels, i * 16 + 12, 1) end emit("ours_rgb_zip.exr", Exr.encode(opaque)) emit("ours_from_rgba8.exr", Exr.encode(image(16, 16, "rgba8", 2))) end -- the simple raster writers: a 37 x 23 photo-like image (opaque and with alpha) and a 12-colour one local function photo(alpha: boolean): Types.Image local img = image(37, 23, "rgba8", 0) if not alpha then for i = 0, 37 * 23 - 1 do buffer.writeu8(img.pixels, i * 4 + 3, 255) end end return img end local function few(): Types.Image local img = Types.Image.new(30, 20) for y = 0, 19 do for x = 0, 29 do local k = (x // 3 + y // 2) % 12 buffer.writeu32(img.pixels, (y * 30 + x) * 4, (k * 20) + (255 - k * 20) * 256 + ((k * 77) % 256) * 65536 + 0xFF000000) end end return img end local function simple(): any if which == "pcx" then return require("@formats/image/pcx") elseif which == "sgi" then return require("@formats/image/sgi") elseif which == "sunras" then return require("@formats/image/sunras") elseif which == "xbm" then return require("@formats/image/xbm") elseif which == "xpm" then return require("@formats/image/xpm") elseif which == "ilbm" then return require("@formats/image/ilbm") end return nil end local Fmt = simple() if Fmt then for name, img in { opaque = photo(false), alpha = photo(true), few = few() } do local out, err = Fmt.encode(img) if out then emit("ours_" .. name .. "." .. which, out) else print("skip\t" .. name .. "\t" .. tostring(err)) end end end if which == "icns" then -- square icons 16 / 32 / 48 / 128 as RLE entries (no encodeImage hook here) local Icns = require("@formats/image/icns") local base = image(128, 128, "rgba8", 1) base.frames = {} for i, s in { 16, 32, 48, 128 } do base.frames[i] = { image = image(s, s, "rgba8", i), delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } end base.pixels = base.frames[4].image.pixels emit("ours_legacy.icns", Icns.encode(base)) end if which == "ora" then -- a layered document: background, an offset half-opaque multiply layer, a hidden one, a group with a screen layer local Ora = require("@formats/layered/ora") local Png = require("@formats/image/png") local doc = Types.Layered.new(40, 30) local bg = Types.Layered.layer("background", image(40, 30, "rgba8", 0)) for i = 0, 40 * 30 - 1 do buffer.writeu8(bg.image.pixels, i * 4 + 3, 255) end local mid = Types.Layered.layer("mid", image(20, 10, "rgba8", 1), 5, 7) mid.opacity, mid.blend = 0.5, "multiply" local hidden = Types.Layered.layer("hidden", image(8, 8, "rgba8", 2), 1, 1) hidden.visible = false local inner = Types.Layered.layer("inner", image(10, 10, "rgba8", 3), 20, 15) inner.blend = "screen" local g = Types.Layered.group("group", { inner }) g.opacity = 0.75 doc.layers = { bg, mid, hidden, g } emit("ours_doc.ora", Ora.encode(doc, { encodeImage = function(img: any, _f: string): (any, any) return Png.encode(img) end })) end