From e5c0873a799df2fc013644306de6f5360e874df8 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 15:06:05 -0500 Subject: [PATCH] ws-game: image/tim (PlayStation TIM read: 4/8-bit CLUT, 15-bit, 24-bit; write 4/8/16/24-bit) --- docs/FORMATS.md | 1 + src/Formats/Registry.luau | 12 ++ src/Formats/image/tim/init.luau | 247 ++++++++++++++++++++++++++++++++ tests/image/tim.luau | 95 ++++++++++++ tools/fixtures/tim.py | 73 ++++++++++ 5 files changed, 428 insertions(+) create mode 100644 src/Formats/image/tim/init.luau create mode 100644 tests/image/tim.luau create mode 100644 tools/fixtures/tim.py diff --git a/docs/FORMATS.md b/docs/FORMATS.md index bf28c57..2e11a70 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -15,6 +15,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build | image | qoi | .qoi | yes | yes | alpha: true, depths: 8 | `tests/image/qoi.luau` | | image | tga | .tga .icb .vda .vst .tpic | yes | yes | alpha: true, palette: true, depths: 8, 15, 16, 24, 32 | `tests/image/tga.luau` | | image | tiff | .tif .tiff | yes | yes | alpha: true, pages: true, depths: 1, 2, 4, 8, 16, 32, float: true | `tests/image/tiff.luau` | +| image | tim | .tim | yes | yes | alpha: true, palette: true, depths: 4, 8, 16, 24 | `tests/image/tim.luau` | | image | webp | .webp | yes | yes | animation: true, alpha: true, lossy: true, lossless: true, depths: 8 | `tests/image/webp.luau` | | audio | aac | .aac .adts | yes | - | codecs: "aac", streaming: true | `tests/audio/aac.luau` | | audio | aiff | .aiff .aif .aifc .afc | yes | yes | streaming: true, depths: 8, 16, 24, 32, float: true, lossy: false | `tests/audio/aiff.luau` | diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau index a38992b..26445ec 100644 --- a/src/Formats/Registry.luau +++ b/src/Formats/Registry.luau @@ -134,6 +134,18 @@ local list: { Entry } = { return require("@formats/image/tiff") end, }, + { + name = "tim", + category = "image", + path = "image/tim", + extensions = { "tim" }, + mime = { "image/x-tim" }, + deps = { "core/Err", "core/Bytes", "Types" }, + caps = { decode = true, encode = true, alpha = true, palette = true, depths = { 4, 8, 16, 24 } }, + load = function() + return require("@formats/image/tim") + end, + }, { name = "webp", category = "image", diff --git a/src/Formats/image/tim/init.luau b/src/Formats/image/tim/init.luau new file mode 100644 index 0000000..91af052 --- /dev/null +++ b/src/Formats/image/tim/init.luau @@ -0,0 +1,247 @@ +--!strict +-- image/tim: PlayStation TIM images (magic 0x10), read and write. Written from the PSX "File Formats" documentation +-- (Sony, TIM: pmode, CLUT and image blocks with their VRAM positions); no code taken. +-- Decode: 4-bit and 8-bit CLUT images (opts.clut picks the palette row, default 0), 15-bit direct and 24-bit +-- direct, to rgba8: PSX colours are 1-5-5-5 STP-B-G-R, channels expanded as (v << 3) | (v >> 2); the colour 0x0000 +-- is transparent (alpha 0) as on the hardware, others opaque (the STP bit is kept in meta.source.stp = true when +-- any pixel or CLUT entry has it). meta.source = { format, bpp, clut = { x, y, colours, count }?, x, y (VRAM) }. +-- Encode: rgba8 to 16-bit direct (default), 24-bit (opts.bpp = 24, alpha dropped) or 4 / 8-bit CLUT when the image +-- has at most 16 / 256 PSX colours (opts.bpp = 4 / 8; Err.ARG otherwise); alpha below 128 becomes 0x0000; opaque +-- black is written as 0x8000 (STP set) so it stays opaque. Not supported: mixed mode (pmode 4) images. + +-- ------------------------------------------------------------------------------------------------ requires +local Err = require("@formats/core/Err") +local Bytes = require("@formats/core/Bytes") +local Types = require("@formats/Types") + +-- ------------------------------------------------------------------------------------------------ contract +local Tim = { + name = "tim", + category = "image", + extensions = { "tim" }, + mime = { "image/x-tim" }, + deps = { "core/Err", "core/Bytes", "Types" }, + caps = { decode = true, encode = true, alpha = true, palette = true, depths = { 4, 8, 16, 24 } }, +} + +-- ------------------------------------------------------------------------------------------------ types +export type DecodeOptions = { + clut: number?, -- the CLUT row to use for indexed images (0-based, default 0) + maxPixels: number?, -- default 2^26 +} +export type EncodeOptions = { + bpp: number?, -- 4, 8, 16 (default) or 24 + x: number?, -- VRAM position of the image (default 0, 0) + y: number?, + clutX: number?, -- VRAM position of the CLUT (default 0, 480) + clutY: number?, +} + +-- ------------------------------------------------------------------------------------------------ constants +local readu8, readu16, writeu32, writeu16, readu32 = buffer.readu8, buffer.readu16, buffer.writeu32, buffer.writeu16, buffer.readu32 +local BPP = { [0] = 4, [1] = 8, [2] = 16, [3] = 24 } + +-- ------------------------------------------------------------------------------------------------ sniff +function Tim.sniff(r: Bytes.Reader): number + if r:remaining() < 20 or r:peek(4) ~= "\16\0\0\0" then + return 0 + end + local flags = r:peekU8(4) + if flags > 0x0F or (flags % 8) > 3 or r:peek(3, 5) ~= "\0\0\0" then + return 0 + end + return 0.8 +end + +-- ------------------------------------------------------------------------------------------------ decode +local function colour(c: number): number + if c == 0 then + return 0 + end + local r, g, b = c % 32, (c // 32) % 32, (c // 1024) % 32 + r, g, b = r * 8 + r // 4, g * 8 + g // 4, b * 8 + b // 4 + return 0xFF000000 + b * 65536 + g * 256 + r +end + +function Tim.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?) + local magic, flags = r:u32le(), r:u32le() + if r.err then + return nil, r.err + end + if magic ~= 0x10 then + return nil, Err.new(Err.CORRUPT, "bad magic", 0) + end + local pmode = flags % 8 + local bpp = BPP[pmode] + if not bpp then + return nil, Err.new(Err.UNSUPPORTED, "pmode " .. pmode .. " (mixed / unknown)", 4) + end + local stp = false + local pal: buffer? = nil + local clutInfo: any = nil + if bit32.band(flags, 8) ~= 0 then + local start = r:tell() + local bnum, cx, cy, cw, ch = r:u32le(), r:u16le(), r:u16le(), r:u16le(), r:u16le() + if r.err then + return nil, r.err + end + if bnum < 12 or cw * ch * 2 > bnum - 12 then + return nil, Err.new(Err.CORRUPT, "bad CLUT block", start) + end + if not r:need(cw * ch * 2) then + return nil, r.err + end + local row = math.clamp(math.floor(opts and opts.clut or 0), 0, math.max(ch - 1, 0)) + pal = buffer.create(256 * 4) + local p = r.pos + row * cw * 2 + for i = 0, math.min(cw, 256) - 1 do + local c = readu16(r.buf, p + i * 2) + if c >= 0x8000 then + stp = true + end + writeu32(pal :: buffer, i * 4, if c == 0x8000 then 0xFF000000 else colour(c % 0x8000)) + end + clutInfo = { x = cx, y = cy, colours = cw, count = ch, row = row } + r:seek(start + bnum) + if r.err then + return nil, r.err + end + elseif bpp <= 8 then + return nil, Err.new(Err.CORRUPT, "indexed image without a CLUT", 4) + end + local start = r:tell() + local bnum, ix, iy, iw, ih = r:u32le(), r:u16le(), r:u16le(), r:u16le(), r:u16le() + if r.err then + return nil, r.err + end + local w = if bpp == 4 then iw * 4 elseif bpp == 8 then iw * 2 elseif bpp == 16 then iw else (iw * 2) // 3 + local stride = iw * 2 + if w * ih > (opts and opts.maxPixels or 2 ^ 26) then + return nil, Err.new(Err.LIMIT, "image too big") + end + if bnum < 12 or stride * ih > bnum - 12 then + return nil, Err.new(Err.CORRUPT, "bad image block", start) + end + if not r:need(stride * ih) then + return nil, r.err + end + local img = Types.Image.new(w, ih) + local px, src, at = img.pixels, r.buf, r.pos + for y = 0, ih - 1 do + local row = at + y * stride + local d = y * w * 4 + if bpp == 4 then + for x = 0, w - 1 do + local v = readu8(src, row + x // 2) + local i = if x % 2 == 0 then v % 16 else v // 16 + writeu32(px, d + x * 4, readu32(pal :: buffer, i * 4)) + end + elseif bpp == 8 then + for x = 0, w - 1 do + writeu32(px, d + x * 4, readu32(pal :: buffer, readu8(src, row + x) * 4)) + end + elseif bpp == 16 then + for x = 0, w - 1 do + local c = readu16(src, row + x * 2) + if c >= 0x8000 then + stp = true + end + writeu32(px, d + x * 4, if c == 0x8000 then 0xFF000000 else colour(c % 0x8000)) + end + else + for x = 0, w - 1 do + local o = row + x * 3 + writeu32(px, d + x * 4, 0xFF000000 + readu8(src, o + 2) * 65536 + readu8(src, o + 1) * 256 + readu8(src, o)) + end + end + end + img.meta.source = { format = "tim", bpp = bpp, x = ix, y = iy, clut = clutInfo, stp = stp or nil } + return img, nil +end + +-- ------------------------------------------------------------------------------------------------ encode +-- rgba8 pixel -> PSX colour +local function psx(v: number): number + local r, g, b, a = v % 256, (v // 256) % 256, (v // 65536) % 256, v // 16777216 + if a < 128 then + return 0 + end + local c = (b // 8) * 1024 + (g // 8) * 32 + r // 8 + return if c == 0 then 0x8000 else c +end + +function Tim.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?) + if type(img) ~= "table" or img.format ~= "rgba8" or type(img.pixels) ~= "buffer" then + return nil, Err.new(Err.ARG, "expected an rgba8 Image") + end + local w, h = img.width, img.height + local bpp = opts and opts.bpp or 16 + local per = ({ [4] = 4, [8] = 2, [16] = 1, [24] = 2 / 3 } :: { [number]: number })[bpp] + if not per then + return nil, Err.new(Err.ARG, "bpp must be 4, 8, 16 or 24") + end + local iw = if bpp == 24 then (w * 3 + 1) // 2 else math.ceil(w / per) + if iw > 0xFFFF or h > 0xFFFF then + return nil, Err.new(Err.ARG, "image too wide for TIM") + end + local px = img.pixels + local out = Bytes.writer(64 + iw * 2 * h) + out:u32le(0x10) + local colours: { number } = {} + local index: { [number]: number } = {} + if bpp <= 8 then + for i = 0, w * h - 1 do + local c = psx(readu32(px, i * 4)) + if not index[c] then + table.insert(colours, c) + index[c] = #colours - 1 + if #colours > (if bpp == 4 then 16 else 256) then + return nil, Err.new(Err.ARG, "more than " .. (if bpp == 4 then 16 else 256) .. " colours for a " .. bpp .. "-bit TIM") + end + end + end + local n = if bpp == 4 then 16 else 256 + out:u32le(8 + (if bpp == 4 then 0 else 1)) + out:u32le(12 + n * 2) + out:u16le(opts and opts.clutX or 0) + out:u16le(opts and opts.clutY or 480) + out:u16le(n) + out:u16le(1) + for i = 1, n do + out:u16le(colours[i] or 0) + end + else + out:u32le(if bpp == 16 then 2 else 3) + end + out:u32le(12 + iw * 2 * h) + out:u16le(opts and opts.x or 0) + out:u16le(opts and opts.y or 0) + out:u16le(iw) + out:u16le(h) + local at = out:reserve(iw * 2 * h) + local b = out.buf + buffer.fill(b, at, 0, iw * 2 * h) + for y = 0, h - 1 do + local row = at + y * iw * 2 + for x = 0, w - 1 do + local v = readu32(px, (y * w + x) * 4) + if bpp == 4 then + local i = index[psx(v)] + local o = row + x // 2 + buffer.writeu8(b, o, buffer.readu8(b, o) + (if x % 2 == 0 then i else i * 16)) + elseif bpp == 8 then + buffer.writeu8(b, row + x, index[psx(v)]) + elseif bpp == 16 then + writeu16(b, row + x * 2, psx(v)) + else + buffer.writeu8(b, row + x * 3, v % 256) + buffer.writeu8(b, row + x * 3 + 1, (v // 256) % 256) + buffer.writeu8(b, row + x * 3 + 2, (v // 65536) % 256) + end + end + end + out.pos = at + iw * 2 * h + return out:finish(), nil +end + +return Tim diff --git a/tests/image/tim.luau b/tests/image/tim.luau new file mode 100644 index 0000000..6f81fd7 --- /dev/null +++ b/tests/image/tim.luau @@ -0,0 +1,95 @@ +-- image/tim tests: PlayStation TIM files written by tools/fixtures/tim.py (4 / 8-bit CLUT, 15-bit, 24-bit) decode to +-- ImageMagick's RGB, colour 0x0000 is transparent, CLUT rows, the VRAM position; the encoder round-trips 16 / 24 / +-- 8 / 4-bit; errors; the fuzz pass. +local T = require("@tests/T") +local F = require("@formats") +local Tim = require("@formats/image/tim") +local Bytes = require("@formats/core/Bytes") +local Crc32 = require("@formats/core/Crc32") +local Err = require("@formats/core/Err") +local Types = require("@formats/Types") +local Fx = require("@tests/gen/tim") + +local t = T.suite("tim") +t:eq(Tim.name, "tim", "name") + +local function rgb(img: any): buffer + local n = img.width * img.height + local out = buffer.create(n * 3) + for i = 0, n - 1 do + buffer.copy(out, i * 3, img.pixels, i * 4, 3) + end + return out +end + +for _, c in Fx do + local img, err = Tim.decode(Bytes.reader(buffer.fromstring(c.data))) + t:check(img, c.name .. ": " .. tostring(err)) + t:eq(F.detect(c.data), "tim", c.name .. ": F.detect") + t:check(img.width == c.width and img.height == c.height, c.name .. ": size") + -- transparent pixels: compare RGB with ImageMagick where opaque (it reads 0x0000 as black) + t:eq(Crc32.of(rgb(img)), c.rgb[1], c.name .. ": RGB against ImageMagick") + local tr = 0 + for i = 0, img.width * img.height - 1 do + if buffer.readu8(img.pixels, i * 4 + 3) == 0 then + tr += 1 + end + end + t:eq(tr, c.transparent, c.name .. ": transparent pixels") + t:eq(img.meta.source.bpp, c.bpp, c.name .. ": bpp") + t:eq(img.meta.source.x, 320, c.name .. ": VRAM x") + if c.cluts > 1 then + local other = Tim.decode(Bytes.reader(buffer.fromstring(c.data)), { clut = 1 }) :: any + t:check(Crc32.of(other.pixels) ~= Crc32.of(img.pixels) and other.meta.source.clut.row == 1, c.name .. ": second CLUT row") + end + -- round trips: decoded 4 / 8 / 15-bit pixels are PSX colours, so every depth that fits is lossless (24-bit only + -- round-trips as 24-bit) + for _, bpp in (if c.bpp == 24 then { 24 } else { 16, 24, 8, 4 }) do + local enc, eerr = Tim.encode(img, { bpp = bpp }) + if bpp == 24 then + -- 24-bit drops alpha: compare RGB + local back = Tim.decode(Bytes.reader(enc :: buffer)) :: any + t:eq(Crc32.of(rgb(back)), Crc32.of(rgb(img)), c.name .. ": 24-bit round trip (RGB)") + elseif enc then + -- 4 / 8-bit rows hold whole 16-bit units: odd widths come back padded on the right + local back = Tim.decode(Bytes.reader(enc)) :: any + local same = back.height == img.height and back.width >= img.width + for y = 0, img.height - 1 do + for x = 0, img.width - 1 do + same = same and buffer.readu32(back.pixels, (y * back.width + x) * 4) == buffer.readu32(img.pixels, (y * img.width + x) * 4) + end + end + t:check(same, c.name .. ": " .. bpp .. "-bit round trip") + else + t:check(eerr and eerr.code == Err.ARG, c.name .. ": " .. bpp .. "-bit refused (too many colours)") + end + end +end + +do + local img = Types.Image.new(3, 1) + buffer.writeu32(img.pixels, 0, 0xFF000000) -- opaque black + buffer.writeu32(img.pixels, 4, 0x00FFFFFF) -- transparent + buffer.writeu32(img.pixels, 8, 0xFF0000FF) -- red + local back = Tim.decode(Bytes.reader(Tim.encode(img) :: buffer)) :: any + t:eq(buffer.readu32(back.pixels, 0), 0xFF000000, "opaque black stays opaque (STP)") + t:eq(buffer.readu32(back.pixels, 4), 0, "transparent") + t:eq(buffer.readu32(back.pixels, 8), 0xFF0000FF, "red") + local _, e1 = Tim.encode(img, { bpp = 12 }) + t:check(e1 and e1.code == Err.ARG, "bpp 12") + local _, e2 = Tim.decode(Bytes.reader(buffer.fromstring(string.sub(Fx[1].data, 1, 40)))) + t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2)) + local mixed = buffer.fromstring(Fx[3].data) + buffer.writeu8(mixed, 4, 4) + local _, e3 = Tim.decode(Bytes.reader(mixed)) + t:check(e3 and e3.code == Err.UNSUPPORTED, "mixed mode") +end + +local samples = {} +for _, c in Fx do + table.insert(samples, c.data) +end +t:fuzz("decode", function(b: buffer) + return Tim.decode(Bytes.reader(b)) +end, samples, { mutations = 60 }) +t:done() diff --git a/tools/fixtures/tim.py b/tools/fixtures/tim.py new file mode 100644 index 0000000..db9f07c --- /dev/null +++ b/tools/fixtures/tim.py @@ -0,0 +1,73 @@ +# PlayStation TIM fixtures (tools/fixtures.py): TIM files written here (4-bit with two CLUTs, 8-bit, 15-bit direct +# with STP bits and 0x0000 transparency, 24-bit with an odd width, VRAM positions) decoded by ImageMagick's TIM +# reader (nix imagemagick, RGB: it ignores alpha) -> CRC-32 of the RGB bytes per CLUT row, plus the transparent +# pixel count (colour 0x0000) computed here. +# ImageMagick cannot read several CLUT rows: the 2-row file's reference is a copy with row 0 only. +# tests/gen/tim.luau { { name, data, width, height, bpp, rgb = { crc of CLUT row 0 }, transparent, cluts } } +import os, shutil, struct, tempfile, zlib + + +def tim(rng, bpp, w, h, cluts=1): + flags = {4: 0, 8: 1, 16: 2, 24: 3}[bpp] + out = b"" + colours = [] + if bpp <= 8: + n = 16 if bpp == 4 else 256 + flags |= 8 + for _ in range(cluts): + row = [rng.randrange(65536) for _ in range(n)] + row[0] = 0 + row[1] = 0x8000 + colours.append(row) + out += struct.pack(" 1: + n = 16 if bpp == 4 else 256 + ref = struct.pack("