image formats: comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:54:53 -05:00
1 parent 0bd7b87d2b
commit b5546e208e
40 files changed
+246 -626

No files matched your search

+8 -20
View File
@@ -1,16 +1,7 @@
--!strict
-- image/vtf: Valve Texture Format (VTF 7.0 - 7.5: Source engine textures), read. Written from the Valve Developer
-- Community "VTF (Valve Texture Format)" page; no code taken.
-- Decode: the high-resolution image (located by the 7.3+ resource directory or after the low-resolution thumbnail
-- for 7.0 - 7.2), mip 0 of slice 0 and face 0 (opts.mip / opts.face / opts.slice pick others), frame 1 as the Image
-- and every frame of an animated texture in img.frames (opts.frames ~= false; delay opts.delay, default 0.1 s), to
-- rgba8. Texel formats: RGBA8888, ABGR8888, RGB888, BGR888, RGB565, I8, IA88, A8, RGB888 / BGR888 bluescreen (pure
-- blue transparent), ARGB8888, BGRA8888, BGRX8888, BGR565, BGRX5551, BGRA5551, BGRA4444, UV88, UVWQ8888, UVLX8888,
-- RGBA16161616 (integers) / RGBA16161616F (halves, clamped), R32F, RGB323232F, RGBA32323232F (all through
-- codecs/image/Texel), DXT1 (+ one-bit alpha),
-- DXT3, DXT5, ATI1N, ATI2N (ids 35 / 34 since Alien Swarm, 38 / 37 in Source 2013) through codecs/image/Bcn. meta.source = { format, version, flags, frames, faces, depth, mips,
-- texel, reflectivity, bumpScale }.
-- Not supported: P8 (never implemented by Valve), the newer Strata / Source 2 formats, writing.
-- image/vtf: Valve Texture Format 7.0 - 7.5 (Source engine), from the Valve Developer Community VTF page. Supports: the
-- high-resolution image, mips / faces / slices / frames, uncompressed texels, DXT1 / DXT3 / DXT5, ATI1N / ATI2N (35 /
-- 34 Alien Swarm, 38 / 37 Source 2013). Not supported: P8, Strata / Source 2 formats, writing.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -31,17 +22,16 @@ local Vtf = {
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
mip: number?, -- mip level (0 = full size, default)
face: number?, -- cube map face (0-based, default 0)
slice: number?, -- volume slice (0-based, default 0)
frames: boolean?, -- decode every frame into img.frames (default true)
delay: number?, -- frame delay in seconds (default 0.1)
mip: number?, -- 0 = full size, default
face: number?, -- 0-based, default 0
slice: number?, -- 0-based, default 0
frames: boolean?, -- default true
delay: number?, -- seconds, default 0.1
maxPixels: number?, -- default 2^26
}
-- ------------------------------------------------------------------------------------------------ constants
local M = Texel.masked
-- format id -> a codecs/image/Texel format (name or bit-mask descriptor) | { texel, bluescreen = true } | a BCn kind
local FORMATS: { [number]: any } = {
[0] = "rgba8", -- RGBA8888
[1] = M(4, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF), -- ABGR8888
@@ -90,7 +80,6 @@ function Vtf.sniff(r: Bytes.Reader): number
end
-- ------------------------------------------------------------------------------------------------ decode
-- bytes of one w x h image in format f
local function imageSize(f: any, w: number, h: number): number
if type(f) == "string" and BCN[f] then
return Bcn.size(if f == "bc1a" then "bc1" else f, w, h) or 0
@@ -162,7 +151,6 @@ function Vtf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
local lowSize = if lowFmt >= 0 and lowW > 0 and lowH > 0 and lf then imageSize(lf, lowW, lowH) else 0
dataAt = headerSize + lowSize
end
-- the chain: mips smallest first; per mip: frames, faces, slices
local mip = math.clamp(math.floor(opts and opts.mip or 0), 0, mips - 1)
local face = math.clamp(math.floor(opts and opts.face or 0), 0, faces - 1)
local slice = math.clamp(math.floor(opts and opts.slice or 0), 0, depth - 1)