--!strict -- 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") local Bytes = require("@rosetta/core/Bytes") local Types = require("@rosetta/Types") local Bcn = require("@rosetta/codecs/image/Bcn") local Texel = require("@rosetta/codecs/image/Texel") -- ------------------------------------------------------------------------------------------------ contract local Vtf = { name = "vtf", category = "image", extensions = { "vtf" }, mime = { "image/x-vtf" }, deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Bcn", "codecs/image/Texel" }, caps = { decode = true, encode = false, alpha = true, animation = true, versions = { "7.0", "7.1", "7.2", "7.3", "7.4", "7.5" } }, } -- ------------------------------------------------------------------------------------------------ types export type DecodeOptions = { 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 local FORMATS: { [number]: any } = { [0] = "rgba8", -- RGBA8888 [1] = M(4, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF), -- ABGR8888 [2] = "rgb8", -- RGB888 [3] = "bgr8", -- BGR888 [4] = "r5g6b5", -- RGB565: red in the low bits (as VTFLib / srctools read it) [5] = "l8", -- I8 [6] = "la8", -- IA88 [8] = "a8", -- A8 [9] = { texel = "rgb8", bluescreen = true }, -- RGB888_BLUESCREEN [10] = { texel = "bgr8", bluescreen = true }, -- BGR888_BLUESCREEN [11] = M(4, 0xFF000000, 0x000000FF, 0x0000FF00, 0x00FF0000), -- ARGB8888: VTFLib's layout (G, B, A, R in memory) [12] = "bgra8", -- BGRA8888 [13] = "bc1", -- DXT1 [14] = "bc2", -- DXT3 [15] = "bc3", -- DXT5 [16] = "bgrx8", -- BGRX8888 [17] = "b5g6r5", -- BGR565: blue in the low bits [18] = "b5g5r5x1", -- BGRX5551 [19] = "b4g4r4a4", -- BGRA4444 [20] = "bc1a", -- DXT1_ONEBITALPHA [21] = "b5g5r5a1", -- BGRA5551 [22] = "rg8", -- UV88 [23] = "rgba8", -- UVWQ8888 [24] = "rgba16f", -- RGBA16161616F [25] = "rgba16", -- RGBA16161616 [26] = "rgba8", -- UVLX8888 [27] = "r32f", -- R32F [28] = "rgb32f", -- RGB323232F [29] = "rgba32f", -- RGBA32323232F [34] = "bc5", -- ATI2N (Alien Swarm and later) [35] = "bc4", -- ATI1N (Alien Swarm and later) [37] = "bc5", -- ATI2N (Source 2013) [38] = "bc4", -- ATI1N (Source 2013) } local BCN: { [string]: boolean } = { bc1 = true, bc1a = true, bc2 = true, bc3 = true, bc4 = true, bc5 = true } local NAMES: { [number]: string } = { [0] = "RGBA8888", "ABGR8888", "RGB888", "BGR888", "RGB565", "I8", "IA88", "P8", "A8", "RGB888_BLUESCREEN", "BGR888_BLUESCREEN", "ARGB8888", "BGRA8888", "DXT1", "DXT3", "DXT5", "BGRX8888", "BGR565", "BGRX5551", "BGRA4444", "DXT1_ONEBITALPHA", "BGRA5551", "UV88", "UVWQ8888", "RGBA16161616F", "RGBA16161616", "UVLX8888", "R32F", "RGB323232F", "RGBA32323232F", "NV_DST16", "NV_DST24", "NV_INTZ", "NV_RAWZ", "ATI2N", "ATI1N", "NV_NULL", "ATI2N", "ATI1N" } local ENVMAP = 0x4000 -- ------------------------------------------------------------------------------------------------ sniff function Vtf.sniff(r: Bytes.Reader): number return if r:peek(4) == "VTF\0" and r:peekU8(4) == 7 then 1 else 0 end -- ------------------------------------------------------------------------------------------------ decode 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 end return Texel.size(if type(f) == "table" and f.texel then f.texel else f, w, h) or 0 end function Vtf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.Err?) local size = r.limit - r.base local b, base = r.buf, r.base if size < 64 then return nil, Err.new(Err.TRUNCATED, "header", 0) end if buffer.readstring(b, base, 4) ~= "VTF\0" then return nil, Err.new(Err.CORRUPT, "bad magic", 0) end local major, minor = buffer.readu32(b, base + 4), buffer.readu32(b, base + 8) if major ~= 7 or minor > 5 then return nil, Err.new(Err.UNSUPPORTED, "VTF version " .. major .. "." .. minor, 4) end local headerSize = buffer.readu32(b, base + 12) local w, h = buffer.readu16(b, base + 16), buffer.readu16(b, base + 18) local flags = buffer.readu32(b, base + 20) local frames, firstFrame = buffer.readu16(b, base + 24), buffer.readu16(b, base + 26) local refl = { buffer.readf32(b, base + 32), buffer.readf32(b, base + 36), buffer.readf32(b, base + 40) } local bump = buffer.readf32(b, base + 48) local fmtId = buffer.readi32(b, base + 52) local mips = buffer.readu8(b, base + 56) local lowFmt = buffer.readi32(b, base + 57) local lowW, lowH = buffer.readu8(b, base + 61), buffer.readu8(b, base + 62) local depth = if minor >= 2 and size >= 65 then math.max(1, buffer.readu16(b, base + 63)) else 1 if w == 0 or h == 0 then return nil, Err.new(Err.CORRUPT, "zero size", 16) end if w * h > (opts and opts.maxPixels or 2 ^ 26) then return nil, Err.new(Err.LIMIT, "image too big") end local f: any = FORMATS[fmtId] if not f then return nil, Err.new(Err.UNSUPPORTED, "texel format " .. (NAMES[fmtId] or tostring(fmtId)), 52) end frames = math.max(1, frames) mips = math.max(1, mips) local faces = 1 if bit32.band(flags, ENVMAP) ~= 0 then faces = if minor < 5 and firstFrame ~= 0xFFFF then 7 else 6 end -- where the high-resolution data starts local dataAt: number? = nil if minor >= 3 then if size < 80 then return nil, Err.new(Err.TRUNCATED, "resource directory", 68) end local nres = buffer.readu32(b, base + 68) if nres > 32 or 80 + nres * 8 > size then return nil, Err.new(Err.CORRUPT, "bad resource count", 68) end for i = 0, nres - 1 do local o = base + 80 + i * 8 if buffer.readu8(b, o) == 0x30 and buffer.readu8(b, o + 1) == 0 and buffer.readu8(b, o + 2) == 0 then dataAt = buffer.readu32(b, o + 4) end end if not dataAt then return nil, Err.new(Err.CORRUPT, "no high-resolution image resource", 80) end else local lf = FORMATS[lowFmt] local lowSize = if lowFmt >= 0 and lowW > 0 and lowH > 0 and lf then imageSize(lf, lowW, lowH) else 0 dataAt = headerSize + lowSize end 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) local off = dataAt :: number for m = mips - 1, mip + 1, -1 do local mw, mh, md = math.max(1, w // 2 ^ m), math.max(1, h // 2 ^ m), math.max(1, depth // 2 ^ m) off += imageSize(f, mw, mh) * md * faces * frames end local mw, mh, md = math.max(1, w // 2 ^ mip), math.max(1, h // 2 ^ mip), math.max(1, depth // 2 ^ mip) local one = imageSize(f, mw, mh) local function image(frame: number): (Types.Image?, Err.Err?) local at = off + ((frame * faces + face) * md + math.min(slice, md - 1)) * one if at + one > size or at < 0 then return nil, Err.new(Err.TRUNCATED, "image data past the end", at) end local img = Types.Image.new(mw, mh) if type(f) == "string" and BCN[f] then local kind = if f == "bc1a" then "bc1" else f local px, e = Bcn.decode(kind, b, base + at, mw, mh) if not px then return nil, Err.from(e) end buffer.copy(img.pixels, 0, px, 0, mw * mh * 4) if f == "bc1" then -- plain DXT1 carries no alpha: the transparent colour of 3-colour blocks reads as opaque black for i = 0, mw * mh - 1 do buffer.writeu8(img.pixels, i * 4 + 3, 255) end end else local blue = type(f) == "table" and f.bluescreen == true local px, e = Texel.decode(if blue then f.texel else f, b, base + at, mw, mh) if not px then return nil, Err.from(e) end buffer.copy(img.pixels, 0, px, 0, mw * mh * 4) if blue then -- pure blue is the transparent key for i = 0, mw * mh - 1 do if buffer.readu32(img.pixels, i * 4) == 0xFFFF0000 then buffer.writeu32(img.pixels, i * 4, 0) end end end end return img, nil end local img, err = image(0) if not img then return nil, err end if frames > 1 and not (opts and opts.frames == false) then local delay = opts and opts.delay or 0.1 local list: { Types.Frame } = { { image = img, delay = delay, x = 0, y = 0, dispose = "none", blend = "source" } :: any } for fr = 1, frames - 1 do local fi, ferr = image(fr) if not fi then return nil, ferr end table.insert(list, { image = fi, delay = delay, x = 0, y = 0, dispose = "none", blend = "source" } :: any) end img.frames = list img.loops = 0 end img.meta.source = { format = "vtf", version = major .. "." .. minor, flags = flags, frames = frames, faces = faces, depth = depth, mips = mips, texel = NAMES[fmtId], reflectivity = refl, bumpScale = bump, } return img, nil end return Vtf