ws-tex: docs/guide sections (textures / HDR, OpenRaster); DDS / KTX pixelFormat option; XBM / XPM names from F.save
F.save sets opts.format to the file format, so the DDS / KTX layout option is pixelFormat; XBM / XPM make a C identifier of opts.name (F.save passes the file name).
This commit is contained in:
1 parent
d618795ce7
commit
918c8310af
10 files changed
+90
-30
No files matched your search
@@ -207,6 +207,42 @@ Read and write unless noted; see [FORMATS.md](../FORMATS.md) for depths and deta
|
||||
| TIFF | pages | yes | 1..32-bit, float |
|
||||
| BMP, TGA, QOI, ICO / CUR, PNM / PAM / PFM | ICO entries | yes | |
|
||||
| HDR | | no | float (`native` gives rgbaf32) |
|
||||
| DDS, KTX / KTX2 | faces / layers / slices | yes | GPU textures: BC1..BC7, ETC / EAC, every uncompressed layout; mips in `meta.mipmaps` |
|
||||
| OpenEXR | parts (`opts.part`) | yes | half / float / uint, NONE / RLE / ZIPS / ZIP / PIZ, tiles; writes NONE / ZIPS / ZIP |
|
||||
| PCX, SGI, Sun Raster, IFF ILBM / PBM, XPM, XBM | | yes (XBM 1-bit) | old paint / workstation formats; ILBM reads HAM and EHB |
|
||||
| ICNS, ANI | ICNS sizes / ANI steps | yes | PNG / ICO entries through the api hooks |
|
||||
|
||||
### Textures and HDR (DDS, KTX, EXR)
|
||||
|
||||
DDS and KTX files hold GPU textures: block-compressed (BC1..BC7, ETC1 / ETC2 / EAC) or plain texel layouts, with mip
|
||||
levels, cube faces and array layers. The image is the first surface; `img.frames` lists every face / layer / depth
|
||||
slice when there is more than one (`meta.layout` is `"2d"`, `"cube"`, `"array"` or `"volume"`), and with `opts.mips` the
|
||||
smaller levels of the first surface go to `meta.mipmaps`. Float and 16-bit sources keep their values with
|
||||
`opts.native` (`rgbaf32` / `rgba16`); otherwise they are clamped into `rgba8`. Writers pick the stored layout with
|
||||
`pixelFormat` (`format` is the file format): DDS writes `rgba8`, `bgra8`, `bc1`, `bc3`, `bc4`, `bc5`, `rgba16`,
|
||||
`rgba16f`, `rgba32f`; KTX2 writes the uncompressed ones.
|
||||
|
||||
```luau
|
||||
local img = assert(F.ops.image.new(64, 64))
|
||||
assert(F.ops.image.fill(img, 200, 120, 40, 255))
|
||||
local dds = assert(F.save(img, nil, { format = "dds", pixelFormat = "bc1", mips = true }))
|
||||
local tex = assert(F.load(dds, { mips = true }))
|
||||
assert(tex.meta.source.pixelFormat == "bc1" and #tex.meta.mipmaps == 6)
|
||||
local ktx = assert(F.save(img, nil, { format = "ktx", mips = true, zlib = 6 })) -- KTX2, ZLIB-supercompressed
|
||||
assert(F.detect(ktx) == "ktx")
|
||||
```
|
||||
|
||||
OpenEXR loads as linear floats with `opts.native` (`rgbaf32`); the default `rgba8` goes through the same 1 / 2.2
|
||||
display curve as Radiance HDR. `opts.part` picks a part of a multipart file, `opts.layer` reads `<layer>.R` / `.G` / ...
|
||||
|
||||
```luau
|
||||
local hdr = assert(F.ops.image.new(8, 8, "rgbaf32"))
|
||||
assert(F.ops.image.fill(hdr, 4, 2, 0.5, 1)) -- floats may exceed 1
|
||||
local exr = assert(F.save(hdr, nil, { format = "exr", compression = "zip", pixelType = "half" }))
|
||||
local linear = assert(F.load(exr, { native = true }))
|
||||
assert(linear.format == "rgbaf32" and math.abs(buffer.readf32(linear.pixels, 0) - 4) < 1e-3)
|
||||
assert(F.load(exr).format == "rgba8") -- the display conversion
|
||||
```
|
||||
|
||||
An animated image also opens as a video (`F.video.open(gifBytes)`), and saves to a video format
|
||||
(`F.convert(gifBytes, "avi")`): see [video.md](video.md).
|
||||
|
||||
+25
-3
@@ -1,8 +1,8 @@
|
||||
# Layered images (PSD, Aseprite)
|
||||
# Layered images (PSD, Aseprite, OpenRaster)
|
||||
|
||||
Photoshop documents and Aseprite sprites load as a `Layered`: a canvas size and a tree of layers, each with its own
|
||||
Photoshop documents, Aseprite sprites and OpenRaster files load as a `Layered`: a canvas size and a tree of layers, each with its own
|
||||
rgba8 [Image](image.md), position, opacity, blend mode and visibility. `F.ops.layers.flatten` composites the tree
|
||||
into one Image; `F.save` writes PSD or Aseprite, layers included. Aseprite animations also carry every frame's tree.
|
||||
into one Image; `F.save` writes PSD, Aseprite or OpenRaster, layers included. Aseprite animations also carry every frame's tree.
|
||||
|
||||
```luau
|
||||
local doc = assert(F.layered.load(psdBytes))
|
||||
@@ -230,6 +230,28 @@ assert(#back.frames == 4 and back.frames[3].layers[1].x == 3)
|
||||
|---|---|---|---|
|
||||
| PSD / PSB | 1 / 8 / 16 / 32-bit, every colour mode, layers, groups, masks, clipping | 8-bit RGB | adjustment, type and smart-object layers keep only their pixels; no layer effects |
|
||||
| Aseprite | RGBA, grayscale, indexed; frames, linked cels, tags, palette, user data | yes | tilemap layers are skipped (`meta.skipped`); slices not kept |
|
||||
| OpenRaster (ORA) | the stack tree (nested stacks = groups), offsets, opacity, visibility, svg composite-ops | yes | layers are PNGs through the api hooks; `meta.composite` = mergedimage.png |
|
||||
|
||||
### OpenRaster
|
||||
|
||||
An `.ora` file is a zip of PNG layers and a `stack.xml` tree (the format GIMP, Krita and MyPaint exchange). It loads
|
||||
like the others; nested `<stack>`s become groups (`isolation="auto"` is the `"passThrough"` blend). Saving writes every
|
||||
layer, the flattened `mergedimage.png` and a thumbnail.
|
||||
|
||||
```luau
|
||||
local doc = F.Types.Layered.new(32, 24)
|
||||
local base = assert(F.ops.image.new(32, 24))
|
||||
assert(F.ops.image.fill(base, 30, 60, 200, 255))
|
||||
local dot = assert(F.ops.image.new(8, 8))
|
||||
assert(F.ops.image.fill(dot, 255, 255, 0, 255))
|
||||
local top = F.Types.Layered.layer("dot", dot, 12, 8)
|
||||
top.blend, top.opacity = "screen", 0.5
|
||||
doc.layers = { F.Types.Layered.layer("base", base), top }
|
||||
local ora = assert(F.save(doc, nil, { format = "ora" }))
|
||||
local back = assert(F.layered.load(ora))
|
||||
assert(#back.layers == 2 and back.layers[2].blend == "screen" and back.layers[2].x == 12)
|
||||
assert(back.meta.composite.width == 32) -- mergedimage.png
|
||||
```
|
||||
|
||||
See [FORMATS.md](../FORMATS.md) for the capability matrix.
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ export type DecodeOptions = {
|
||||
maxPixels: number?, -- refuse larger images (default 2^26, counted over every decoded surface)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
format: ("rgba8" | "bgra8" | "bc1" | "bc3" | "bc4" | "bc5" | "rgba16" | "rgba16f" | "rgba32f")?, -- default:
|
||||
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)
|
||||
@@ -440,13 +440,13 @@ function Dds.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if pf ~= "rgba8" and pf ~= "rgba16" and pf ~= "rgbaf32" then
|
||||
return nil, Err.new(Err.ARG, "needs an rgba8, rgba16 or rgbaf32 Image (got " .. tostring(pf) .. ")")
|
||||
end
|
||||
local f: string = (opts and opts.format) or (if pf == "rgba8" then "rgba8" elseif pf == "rgba16" then "rgba16" else "rgba32f")
|
||||
local f: string = (opts and opts.pixelFormat) or (if pf == "rgba8" then "rgba8" elseif pf == "rgba16" then "rgba16" else "rgba32f")
|
||||
if not DX10_OUT[f] then
|
||||
return nil, Err.new(Err.ARG, "unknown format " .. tostring(f))
|
||||
return nil, Err.new(Err.ARG, "unknown pixelFormat " .. tostring(f))
|
||||
end
|
||||
local needs = if f == "rgba16" then "rgba16" elseif f == "rgba16f" or f == "rgba32f" then "rgbaf32" else "rgba8"
|
||||
if pf ~= needs then
|
||||
return nil, Err.new(Err.ARG, string.format("format %s needs an %s Image", f, needs))
|
||||
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 }
|
||||
|
||||
@@ -39,7 +39,7 @@ export type DecodeOptions = {
|
||||
maxPixels: number?, -- refuse larger images (default 2^26, over every decoded surface)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
format: ("rgba8" | "rgba16" | "rgba16f" | "rgba32f")?, -- default: the image's own (rgbaf32 -> rgba32f)
|
||||
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")
|
||||
@@ -543,14 +543,14 @@ function Ktx.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if pf ~= "rgba8" and pf ~= "rgba16" and pf ~= "rgbaf32" then
|
||||
return nil, Err.new(Err.ARG, "needs an rgba8, rgba16 or rgbaf32 Image (got " .. tostring(pf) .. ")")
|
||||
end
|
||||
local f: string = (opts and opts.format) or (if pf == "rgbaf32" then "rgba32f" else pf)
|
||||
local f: string = (opts and opts.pixelFormat) or (if pf == "rgbaf32" then "rgba32f" else pf)
|
||||
local spec = OUT[f]
|
||||
if not spec then
|
||||
return nil, Err.new(Err.ARG, "unknown format " .. tostring(f))
|
||||
return nil, Err.new(Err.ARG, "unknown pixelFormat " .. tostring(f))
|
||||
end
|
||||
local needs = if f == "rgba16f" or f == "rgba32f" then "rgbaf32" else f
|
||||
if pf ~= needs then
|
||||
return nil, Err.new(Err.ARG, string.format("format %s needs an %s Image", f, needs))
|
||||
return nil, Err.new(Err.ARG, string.format("pixelFormat %s needs an %s Image", f, needs))
|
||||
end
|
||||
local srgb = f == "rgba8" and (if opts and opts.srgb ~= nil then opts.srgb else (img.meta and img.meta.colorSpace == "srgb"))
|
||||
local images: { Types.Image } = { img }
|
||||
|
||||
@@ -26,7 +26,7 @@ export type DecodeOptions = {
|
||||
maxPixels: number?, -- refuse larger images (default 2^26)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
name: string?, -- the C identifier prefix (default "image")
|
||||
name: string?, -- the C identifier prefix (default "image"; an extension is dropped, other characters become _)
|
||||
threshold: number?, -- luminance below this is black (default 128)
|
||||
}
|
||||
|
||||
@@ -120,9 +120,10 @@ function Xbm.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if w < 1 or h < 1 then
|
||||
return nil, Err.new(Err.ARG, "bad size")
|
||||
end
|
||||
local name = opts and opts.name or "image"
|
||||
if not string.match(name, "^[%a_][%w_]*$") then
|
||||
return nil, Err.new(Err.ARG, "name must be a C identifier")
|
||||
-- a C identifier from opts.name (F.save passes the file name: "out.xbm" -> out)
|
||||
local name = string.gsub(string.gsub(opts and opts.name or "image", "%.[^.]*$", ""), "[^%w_]", "_")
|
||||
if name == "" or string.match(name, "^%d") then
|
||||
name = "_" .. name
|
||||
end
|
||||
local th = opts and opts.threshold or 128
|
||||
local px = img.pixels
|
||||
|
||||
@@ -28,7 +28,7 @@ export type DecodeOptions = {
|
||||
maxPixels: number?, -- refuse larger images (default 2^26)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
name: string?, -- the C identifier (default "image")
|
||||
name: string?, -- the C identifier (default "image"; an extension is dropped, other characters become _)
|
||||
}
|
||||
|
||||
-- the characters of pixel codes (printable ASCII without '"' and '\')
|
||||
@@ -214,9 +214,10 @@ function Xpm.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if w < 1 or h < 1 then
|
||||
return nil, Err.new(Err.ARG, "bad size")
|
||||
end
|
||||
local name = opts and opts.name or "image"
|
||||
if not string.match(name, "^[%a_][%w_]*$") then
|
||||
return nil, Err.new(Err.ARG, "name must be a C identifier")
|
||||
-- a C identifier from opts.name (F.save passes the file name: "out.xbm" -> out)
|
||||
local name = string.gsub(string.gsub(opts and opts.name or "image", "%.[^.]*$", ""), "[^%w_]", "_")
|
||||
if name == "" or string.match(name, "^%d") then
|
||||
name = "_" .. name
|
||||
end
|
||||
local px = img.pixels
|
||||
local index: { [number]: number } = {}
|
||||
|
||||
@@ -163,7 +163,7 @@ do
|
||||
local img = Types.Image.new(37, 23)
|
||||
img.pixels = src
|
||||
for _, f in { "rgba8", "bgra8" } do
|
||||
local back = dec(Dds.encode(img, { format = f :: any }) :: buffer)
|
||||
local back = dec(Dds.encode(img, { pixelFormat = f :: any }) :: buffer)
|
||||
t:eq(buffer.tostring(back.pixels), buffer.tostring(src), f .. " round trip")
|
||||
end
|
||||
local f = Types.Image.new(3, 2, "rgbaf32")
|
||||
@@ -172,9 +172,9 @@ do
|
||||
end
|
||||
local back = dec(Dds.encode(f) :: buffer, { native = true })
|
||||
t:eq(buffer.tostring(back.pixels), buffer.tostring(f.pixels), "rgba32f round trip")
|
||||
local _, e1 = Dds.encode(f, { format = "bc1" })
|
||||
local _, e1 = Dds.encode(f, { pixelFormat = "bc1" })
|
||||
t:check(e1 and e1.code == Err.ARG, "bc1 needs rgba8")
|
||||
local _, e2 = Dds.encode(img, { format = "bc7" :: any })
|
||||
local _, e2 = Dds.encode(img, { pixelFormat = "bc7" :: any })
|
||||
t:check(e2 and e2.code == Err.ARG, "no bc7 encoder")
|
||||
end
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ do
|
||||
end
|
||||
local back = dec(Ktx.encode(f) :: buffer, { native = true })
|
||||
t:eq(buffer.tostring(back.pixels), buffer.tostring(f.pixels), "rgba32f round trip")
|
||||
local _, e = Ktx.encode(f, { format = "rgba8" })
|
||||
local _, e = Ktx.encode(f, { pixelFormat = "rgba8" })
|
||||
t:check(e and e.code == Err.ARG, "format mismatch")
|
||||
local _, e2 = Ktx.encode(img, { zlib = 12 })
|
||||
t:check(e2 and e2.code == Err.ARG, "zlib level")
|
||||
|
||||
@@ -34,8 +34,8 @@ do
|
||||
t:eq(Xbm.sniff(Bytes.reader(buffer.fromstring("#define c_width 2\n"))), 0.9, "sniff")
|
||||
local _, e = Xbm.decode(Bytes.reader(buffer.fromstring("static char x[] = {};")))
|
||||
t:check(e and e.code == Err.CORRUPT, "no defines")
|
||||
local _, e2 = Xbm.encode(R.image(1, 1, "\0\0\0\255"), { name = "1bad" })
|
||||
t:check(e2 and e2.code == Err.ARG, "bad name")
|
||||
local named = Xbm.encode(R.image(1, 1, "\0\0\0\255"), { name = "1 bad.xbm" }) :: buffer
|
||||
t:check(string.find(buffer.tostring(named), "#define _1_bad_width 1", 1, true) ~= nil, "name made an identifier")
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Xbm.decode(Bytes.reader(b))
|
||||
|
||||
@@ -56,13 +56,13 @@ if which == "dds" then
|
||||
local Dds = require("@formats/image/dds")
|
||||
local img = image(37, 23, "rgba8", 0)
|
||||
for _, f in { "rgba8", "bgra8", "bc1", "bc3", "bc4", "bc5" } do
|
||||
emit("ours_" .. f .. ".dds", Dds.encode(img, { format = f :: any }))
|
||||
emit("ours_" .. f .. "_mips.dds", Dds.encode(img, { format = f :: any, mips = true }))
|
||||
emit("ours_" .. f .. ".dds", Dds.encode(img, { pixelFormat = f :: any }))
|
||||
emit("ours_" .. f .. "_mips.dds", Dds.encode(img, { pixelFormat = f :: any, mips = true }))
|
||||
end
|
||||
emit("ours_rgba8_dx10.dds", Dds.encode(img, { dx10 = true, mips = true }))
|
||||
emit("ours_rgba16.dds", Dds.encode(image(37, 23, "rgba16", 0), { mips = true }))
|
||||
emit("ours_rgba32f.dds", Dds.encode(image(37, 23, "rgbaf32", 0, 4), { mips = true }))
|
||||
emit("ours_rgba16f.dds", Dds.encode(image(37, 23, "rgbaf32", 0, 4), { format = "rgba16f" }))
|
||||
emit("ours_rgba16f.dds", Dds.encode(image(37, 23, "rgbaf32", 0, 4), { pixelFormat = "rgba16f" }))
|
||||
local cube = image(16, 16, "rgba8", 0)
|
||||
cube.frames = {}
|
||||
for i = 1, 6 do
|
||||
@@ -71,7 +71,7 @@ if which == "dds" then
|
||||
cube.meta.layout = "cube"
|
||||
emit("ours_cube.dds", Dds.encode(cube, { mips = true }))
|
||||
cube.meta.layout = "array"
|
||||
emit("ours_array_bc1.dds", Dds.encode(cube, { format = "bc1" }))
|
||||
emit("ours_array_bc1.dds", Dds.encode(cube, { pixelFormat = "bc1" }))
|
||||
end
|
||||
|
||||
if which == "ktx" then
|
||||
@@ -82,7 +82,7 @@ if which == "ktx" then
|
||||
emit("ours_srgb_zlib.ktx2", Ktx.encode(img, { mips = true, srgb = true, zlib = 6 }))
|
||||
emit("ours_rgba16.ktx2", Ktx.encode(image(37, 23, "rgba16", 0), { mips = true }))
|
||||
emit("ours_rgba32f.ktx2", Ktx.encode(image(37, 23, "rgbaf32", 0, 4), { mips = true }))
|
||||
emit("ours_rgba16f.ktx2", Ktx.encode(image(37, 23, "rgbaf32", 0, 4), { format = "rgba16f" }))
|
||||
emit("ours_rgba16f.ktx2", Ktx.encode(image(37, 23, "rgbaf32", 0, 4), { pixelFormat = "rgba16f" }))
|
||||
local cube = image(16, 16, "rgba8", 0)
|
||||
cube.frames = {}
|
||||
for i = 1, 6 do
|
||||
|
||||
Reference in new issue
Block a user