248 lines
8.6 KiB
Luau
248 lines
8.6 KiB
Luau
--!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
|