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

+13 -27
View File
@@ -1,15 +1,7 @@
--!strict
-- image/ktx: Khronos texture containers (KTX 1.1 and KTX 2.0 specifications; new code). Decode: KTX1 (OpenGL
-- glInternalFormat / glFormat + glType: the S3TC / RGTC / BPTC / ETC1 / ETC2 / EAC compressed formats and the
-- uncompressed ones with 4-byte row alignment; big-endian files) and KTX2 (VkFormat: BCn, ETC2 / EAC and every
-- uncompressed format codecs/image/Texel knows; Zstandard (codecs/compress/Zstd) and ZLIB supercompression); mip
-- levels, cube maps, arrays and 3D textures, the key / value data (meta.keyValue) and KTXorientation (T=u rows are
-- flipped). Like image/dds: the first surface is the image, img.frames lists every face / layer / slice when there is
-- more than one (meta.layout), opts.mips decodes the smaller levels into img.meta.mipmaps; rgba8 unless opts.native.
-- Encode: KTX2, uncompressed: rgba8 (R8G8B8A8_UNORM, or _SRGB when meta.colorSpace is "srgb"), rgba16
-- (R16G16B16A16_UNORM), rgbaf32 (R32G32B32A32_SFLOAT or R16G16B16A16_SFLOAT), with a full DFD, optional mip chain,
-- frames as layers or cube faces, optional ZLIB supercompression. Not supported: Basis Universal (BasisLZ / ETC1S and
-- UASTC: UNSUPPORTED), ASTC and PVRTC, writing KTX1 or Zstandard.
-- image/ktx: Khronos KTX 1.1 / KTX 2.0. Supports: KTX1 (GL compressed and uncompressed, big-endian), KTX2 (VkFormat BCn
-- / ETC2 / EAC / uncompressed; Zstd and ZLIB supercompression), mips, cubes, arrays, 3D, KTXorientation; writes
-- uncompressed KTX2. Not supported: Basis Universal (BasisLZ / UASTC), ASTC, PVRTC.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -33,18 +25,18 @@ local Ktx = {
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
native: boolean?, -- keep rgbaf32 / rgba16 for float / 16-bit / BC6H / EAC sources (default false: rgba8)
mips: boolean?, -- decode the smaller mip levels of the first surface into img.meta.mipmaps (default false)
surfaces: boolean?, -- decode every face / layer / slice into img.frames (default true)
maxPixels: number?, -- refuse larger images (default 2^26, over every decoded surface)
native: boolean?, -- default false: rgba8
mips: boolean?, -- default false
surfaces: boolean?, -- default true
maxPixels: number?, -- default 2^26, over every decoded surface
}
export type EncodeOptions = {
pixelFormat: ("rgba8" | "rgba16" | "rgba16f" | "rgba32f")?, -- default: the image's own (rgbaf32 -> rgba32f)
srgb: boolean?, -- rgba8 as R8G8B8A8_SRGB (default: meta.colorSpace == "srgb")
mips: boolean?, -- a full mip chain (box filter; default false)
cube: boolean?, -- img.frames as cube faces (default: meta.layout == "cube")
zlib: number?, -- ZLIB-supercompress every level at this level 1..9 (default none)
keyValue: { [string]: string }?, -- extra key / value pairs (KTXwriter is always written)
srgb: boolean?, -- default: meta.colorSpace == "srgb"
mips: boolean?, -- default false
cube: boolean?, -- default: meta.layout == "cube"
zlib: number?, -- 1..9, default none
keyValue: { [string]: string }?, -- KTXwriter is always written
}
-- ------------------------------------------------------------------------------------------------ constants
@@ -118,7 +110,7 @@ export type Info = {
layers: number,
faces: number,
levels: number,
format: string?, -- the texture format name (nil when unsupported: see unsupported)
format: string?,
unsupported: string?,
srgb: boolean,
opaque: boolean, -- BC1 RGB
@@ -289,7 +281,6 @@ local function info2(r: Bytes.Reader): (Info?, Err.Err?)
return info, nil
end
-- info(r) -> Info | nil, Err (the headers only)
function Ktx.info(r: Bytes.Reader): (Info?, Err.Err?)
local id = r:peek(12)
if id == ID2 then
@@ -300,7 +291,6 @@ function Ktx.info(r: Bytes.Reader): (Info?, Err.Err?)
return nil, Err.new(Err.CORRUPT, "not a KTX file", r.base)
end
-- reverses the bytes of every typeSize-wide element of a big-endian KTX1 image
local function swapped(b: buffer, p: number, n: number, size: number): buffer
local out = buffer.create(n)
buffer.copy(out, 0, b, p, n)
@@ -352,7 +342,6 @@ function Ktx.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
local w, h, d = info.width, info.height, info.depth
for level = 0, info.levels - 1 do
local want = level == 0 or wantMips
-- the level's data: buf, offset, the image stride and row pitch
local lb, lp = b, 0
local rowPitch: number? = nil
local imgSize = Texture.size(fmt, w, h) :: number
@@ -492,7 +481,6 @@ function Ktx.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
end
-- ------------------------------------------------------------------------------------------------ encode
-- format -> VkFormat, bytes per texel, component bytes, kind
local OUT: { [string]: { number } } = {
rgba8 = { 37, 4, 1 },
rgba16 = { 91, 8, 2 },
@@ -582,7 +570,6 @@ function Ktx.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
if zl ~= nil and (type(zl) ~= "number" or zl < 1 or zl > 9) then
return nil, Err.new(Err.ARG, "zlib level must be 1..9")
end
-- level data: the images of every layer / face per level
local levelData: { buffer } = {}
local raw: { number } = {}
local cur = table.clone(images)
@@ -602,7 +589,6 @@ function Ktx.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
end
levelData[lv] = lb
end
-- key / value data
local kvw = Bytes.writer()
local pairs_: { { string } } = { { "KTXwriter", "rosetta" } }
if opts and opts.keyValue then