image formats: comments are specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
b5546e208e
40 files changed
+246
-626
No files matched your search
@@ -1,17 +1,7 @@
|
||||
--!strict
|
||||
-- image/dds: DirectDraw Surface (Microsoft "DDS" programming guide: DDS_HEADER, DDS_PIXELFORMAT, DDS_HEADER_DXT10; new
|
||||
-- code). Decode: every DXGI format with a pixel layout (BC1..BC7 through codecs/image/Bcn; the uncompressed UNORM /
|
||||
-- SNORM / UINT / SINT / FLOAT / packed / shared-exponent ones and YUY2 / AYUV / RGBG through codecs/image/Texel), the
|
||||
-- legacy FourCC codes (DXT1..DXT5, ATI1 / BC4U / BC4S, ATI2 / BC5U / BC5S, RXGB, YUY2 / UYVY / RGBG / GRGB, the
|
||||
-- D3DFMT float / 16-bit numbers) and legacy bit-mask RGB / luminance / alpha formats; mip chains, cube maps (all six
|
||||
-- faces or a subset), texture arrays (DX10 arraySize) and volume textures. The first surface is the image; when there
|
||||
-- are more (faces, layers, depth slices) img.frames lists every one of them (meta.layout = "2d" / "cube" / "array" /
|
||||
-- "volume", meta.faces), and with opts.mips the smaller levels of the first surface go to img.meta.mipmaps. rgba8 by
|
||||
-- default (BC6H and float formats clamped to 0..1); opts.native keeps rgbaf32 / rgba16 where the source has more.
|
||||
-- Encode: rgba8 as uncompressed (R8G8B8A8 / B8G8R8A8), BC1 / BC3 / BC4 / BC5 (legacy FourCC headers), rgba16 / rgbaf32
|
||||
-- as DX10 R16G16B16A16_UNORM / R32G32B32A32_FLOAT / R16G16B16A16_FLOAT; optional mip chains (box filter) and the
|
||||
-- frames as a texture array or a cube map. Not supported: planar video formats (NV12, P010, ...), palettes (P8),
|
||||
-- 1-bit R1_UNORM, the depth-stencil formats with stencil channels, BC encoders other than 1 / 3 / 4 / 5.
|
||||
-- image/dds: DirectDraw Surface (Microsoft DDS guide: DDS_HEADER, DDS_PIXELFORMAT, DDS_HEADER_DXT10). Supports: DXGI
|
||||
-- formats with a pixel layout, legacy FourCC and bit-mask formats, mips, cube maps, arrays, volumes; writes rgba8 / BC1
|
||||
-- / BC3 / BC4 / BC5 / rgba16 / rgbaf32. Not supported: planar video (NV12, P010), P8, R1_UNORM, stencil formats.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -34,18 +24,17 @@ local Dds = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
native: boolean?, -- keep rgbaf32 / rgba16 for float / 16-bit / BC6H 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, counted 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" | "bgra8" | "bc1" | "bc3" | "bc4" | "bc5" | "rgba16" | "rgba16f" | "rgba32f")?, -- default:
|
||||
-- rgba8 for rgba8 images, rgba16 / rgba32f for rgba16 / rgbaf32 ones
|
||||
mips: boolean?, -- write a full mip chain (default false)
|
||||
dx10: boolean?, -- always write the DX10 header (default: only when the format needs it, or for arrays)
|
||||
cube: boolean?, -- write img.frames (6) as a cube map (default: when img.meta.layout == "cube")
|
||||
alphaThreshold: number?, -- BC1: alpha below this is transparent (default 128)
|
||||
pixelFormat: ("rgba8" | "bgra8" | "bc1" | "bc3" | "bc4" | "bc5" | "rgba16" | "rgba16f" | "rgba32f")?,
|
||||
mips: boolean?, -- default false
|
||||
dx10: boolean?, -- default: when the format needs it, or for arrays
|
||||
cube: boolean?, -- default: when img.meta.layout == "cube"
|
||||
alphaThreshold: number?, -- default 128
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -101,10 +90,10 @@ export type Info = {
|
||||
height: number,
|
||||
depth: number,
|
||||
mips: number,
|
||||
format: string, -- the texture format name (codecs/image/Texture)
|
||||
format: string,
|
||||
layout: string, -- "2d" | "cube" | "array" | "volume"
|
||||
layers: number,
|
||||
faces: { string }, -- cube faces present (in file order), else { }
|
||||
faces: { string }, -- in file order
|
||||
srgb: boolean,
|
||||
dxgi: number?,
|
||||
fourcc: string?,
|
||||
@@ -118,7 +107,6 @@ local function u32(b: buffer, o: number): number
|
||||
return buffer.readu32(b, o)
|
||||
end
|
||||
|
||||
-- info(r) -> Info | nil, Err (the headers only)
|
||||
function Dds.info(r: Bytes.Reader): (Info?, Err.Err?)
|
||||
local b, o, n = r.buf, r.base, r.limit - r.base
|
||||
if n < 128 or buffer.readstring(b, o, 4) ~= MAGIC then
|
||||
@@ -228,7 +216,6 @@ function Dds.info(r: Bytes.Reader): (Info?, Err.Err?)
|
||||
if not d then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "pixel format masks", o + 92)
|
||||
end
|
||||
-- the common masks map onto named texel formats (fast paths, sane names)
|
||||
local key = string.format("%d:%x:%x:%x:%x:%s", bitCount, rm, gm, bm, a, tostring(lum))
|
||||
local named = ({
|
||||
["32:ff:ff00:ff0000:ff000000:false"] = "rgba8",
|
||||
@@ -265,7 +252,6 @@ function Dds.info(r: Bytes.Reader): (Info?, Err.Err?)
|
||||
return info, nil
|
||||
end
|
||||
|
||||
-- the bytes of one surface of format f
|
||||
local function surfaceSize(info: any, w: number, h: number): number
|
||||
if info.format == "masked" then
|
||||
return (info.masked :: Texel.Desc).bytes * w * h
|
||||
@@ -359,7 +345,6 @@ end
|
||||
local BYTES_PF: { [string]: number } = { rgba8 = 4, bgra8 = 4, rgba16 = 8, rgba16f = 8, rgba32f = 16 }
|
||||
local DX10_OUT: { [string]: number } = { rgba8 = 28, bgra8 = 87, bc1 = 71, bc3 = 77, bc4 = 80, bc5 = 83, rgba16 = 11, rgba16f = 10, rgba32f = 2 }
|
||||
|
||||
-- the surface bytes of one image in format f
|
||||
local function surface(img: Types.Image, f: string, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local w, h, px = img.width, img.height, img.pixels
|
||||
if f == "bc1" or f == "bc3" or f == "bc4" or f == "bc5" then
|
||||
@@ -448,7 +433,6 @@ function Dds.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if pf ~= needs then
|
||||
return nil, Err.new(Err.ARG, string.format("pixelFormat %s needs an %s Image", f, needs))
|
||||
end
|
||||
-- surfaces: the frames (array / cube faces) or the image
|
||||
local images: { Types.Image } = { img }
|
||||
if img.frames and #img.frames > 1 then
|
||||
images = {}
|
||||
|
||||
Reference in new issue
Block a user