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).
12 KiB
Layered images (PSD, Aseprite, OpenRaster)
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, position, opacity, blend mode and visibility. F.ops.layers.flatten composites the tree
into one Image; F.save writes PSD, Aseprite or OpenRaster, layers included. Aseprite animations also carry every frame's tree.
local doc = assert(F.layered.load(psdBytes))
print(doc.width, doc.height, #doc.layers) -- 48 32 3
for i, layer in doc.layers do -- bottom to top
print(i, layer.name, layer.x, layer.y, layer.image.width, layer.image.height)
end
local flat = assert(F.ops.layers.flatten(doc)) -- an rgba8 Image, 48 x 32
local png = assert(F.save(flat, nil, { format = "png" }))
assert(flat.width == 48 and F.detect(png) == "png")
The type
Layered
| Field | Type | Meaning |
|---|---|---|
kind |
"layered" |
|
width, height |
number | the canvas size in pixels |
layers |
{ Layer } |
the layer tree, bottom to top (index 1 is drawn first); for an animation: frame 1's tree |
frames |
{ { layers, delay } }? |
animations (Aseprite): every frame's tree and its delay in seconds; frames[1].layers is layers |
meta |
table | see below |
Layer
| Field | Type | Meaning |
|---|---|---|
name |
string | |
image |
Image |
rgba8 pixels of the layer's own rectangle (straight alpha); a 0 x 0 Image for groups and empty layers |
x, y |
number | the image's top-left corner on the canvas, in pixels (may be negative or past the canvas) |
opacity |
number | 0..1 |
fillOpacity |
number? | 0..1, multiplied with opacity (PSD fill opacity, Aseprite cel opacity) |
blend |
string | a blend mode name (below) |
visible |
boolean | hidden layers are skipped by flatten unless opts.hidden |
children |
{ Layer }? |
set on groups: the group's layers, bottom to top |
clip |
boolean? | a clipping layer: drawn only where the nearest non-clipping layer below it has alpha |
mask |
LayerMask? |
a grey mask |
LayerMask
| Field | Type | Meaning |
|---|---|---|
image |
Image |
gray8 (or rgba8: its red channel); 255 shows the layer, 0 hides it |
x, y |
number | the mask's position on the canvas (not relative to the layer) |
default |
number | 0..255: the mask value outside its rectangle |
disabled |
boolean? | ignore the mask |
Blend modes: normal, dissolve, darken, multiply, colorBurn, linearBurn, darkerColor, lighten,
screen, colorDodge, linearDodge (also add), lighterColor, overlay, softLight, hardLight,
vividLight, linearLight, pinLight, hardMix, difference, exclusion, subtract, divide, hue,
saturation, color, luminosity. Unknown names composite as normal.
Groups. A group with blend = "passThrough" blends its children straight onto what is below (as Photoshop's
pass-through and Aseprite's plain groups). Any other blend first composites the children on their own transparent
canvas, then blends that result with the group's mode and opacity. A group's mask applies either way: on a
pass-through group it limits, pixel by pixel, how much of the children's effect reaches the canvas.
Format extras.
| Format | Extra fields |
|---|---|
| PSD | meta.composite (the merged image the file stores, an rgba8 Image), meta.resources, meta.icc |
| Aseprite | meta.colorDepth (32, 16, 8), meta.palette ({ { r, g, b, a } }), meta.tags ({ name, from, to, direction, repeat, color }, frames 1-based), meta.transparentIndex, meta.pixelRatio, meta.grid; per layer flags, userData, zIndex; on a frame's layer, link = the frame whose cel it reuses |
local sprite = assert(F.layered.load(asepriteBytes))
assert(#sprite.frames == 2 and sprite.frames[2].delay == 0.25) -- seconds
local hero2 = sprite.frames[2].layers[2]
assert(hero2.name == "Hero" and hero2.x == 12) -- the hero moved 8 px
assert(sprite.frames[2].layers[1].link == 1) -- the background cel is frame 1's
local walk = sprite.meta.tags[1]
assert(walk.name == "walk" and walk.from == 1 and walk.to == 2)
Load
F.load detects both formats by their magic numbers. Everything converts to rgba8: PSD bitmap, greyscale, indexed,
CMYK, Lab and 16 / 32-bit documents; Aseprite grayscale and indexed sprites.
| Option | Format | Meaning |
|---|---|---|
layers |
PSD | false reads only the merged image (meta.composite), much faster; layers is then empty |
maxPixels |
both | refuse documents or layers larger than this (default 2^26) |
maxLayers |
PSD | default 10000 |
maxFrames |
Aseprite | default 65535 |
local quick = assert(F.layered.load(psdBytes, { layers = false }))
assert(#quick.layers == 0 and quick.meta.composite.width == 48)
local preview = quick.meta.composite -- Photoshop's own flattening
assert(preview.format == "rgba8")
F.image.load does not read layered formats; load them with F.layered.load (or F.load) and flatten.
Flatten
F.ops.layers.flatten(layered, opts?) returns a new rgba8 Image of width x height:
| Option | Meaning |
|---|---|
frame |
the animation frame to flatten (1-based; default: layered.layers, which is frame 1) |
hidden |
true includes invisible layers |
background |
{ r, g, b, a } (0..255) painted under everything; default transparent |
F.ops.layers.flattenFrames(layered, opts?) flattens every frame into one animated Image (img.frames, delays
from the frames), ready to save as GIF, APNG or WebP. A Layered without frames gives a still image.
local sprite = assert(F.layered.load(asepriteBytes))
local f2 = assert(F.ops.layers.flatten(sprite, { frame = 2 }))
local r, g, b, a = F.ops.image.getPixel(f2, 13, 9) -- inside the hero on frame 2
assert(r == 220 and g == 50 and b == 50 and a == 255)
local anim = assert(F.ops.layers.flattenFrames(sprite))
assert(#anim.frames == 2 and anim.frames[2].delay == 0.25)
local gif = assert(F.save(anim, nil, { format = "gif" }))
assert(F.detect(gif) == "gif")
local onWhite = assert(F.ops.layers.flatten(sprite, { background = { 255, 255, 255, 255 } }))
assert(onWhite.width == 32)
F.ops.layers.leaves(layers) walks the non-group layers of a tree, bottom to top, with their group depth:
local doc = assert(F.load(psdBytes))
local names = {}
for layer, depth in F.ops.layers.leaves(doc.layers) do
table.insert(names, layer.name .. "@" .. depth)
end
assert(table.concat(names, " ") == "Sky@0 Box@0 Glow@0")
Build and edit
F.Types.Layered.new(w, h) makes an empty document, F.Types.Layered.layer(name, image, x?, y?) a visible normal
layer at full opacity, and F.Types.Layered.group(name, children) a pass-through group. Layers are plain tables:
edit their fields directly.
local L = F.Types.Layered
local doc = L.new(32, 32)
local paper = assert(F.ops.image.new(32, 32))
F.ops.image.fill(paper, 240, 240, 230)
local ink = assert(F.ops.image.new(16, 16))
F.ops.image.fill(ink, 30, 60, 200)
local shade = assert(F.ops.image.new(32, 32))
F.ops.image.fill(shade, 255, 0, 0, 128)
local shadeLayer = L.layer("Shade", shade)
shadeLayer.clip = true -- only where Ink has alpha
shadeLayer.blend = "multiply"
local inkLayer = L.layer("Ink", ink, 8, 8)
inkLayer.opacity = 0.8
doc.layers = { L.layer("Paper", paper), L.group("Art", { inkLayer, shadeLayer }) }
local mask = assert(F.ops.image.new(32, 16, "gray8"))
F.ops.image.fill(mask, 255, 255, 255) -- show the top half
doc.layers[2].mask = { image = mask, x = 0, y = 0, default = 0 } -- hide the rest
local flat = assert(F.ops.layers.flatten(doc))
local _, _, _, a = F.ops.image.getPixel(flat, 0, 0)
assert(a == 255)
local r1 = F.ops.image.getPixel(flat, 12, 12) -- ink under the mask's visible half
local r2 = F.ops.image.getPixel(flat, 12, 20) -- ink masked out: paper shows
assert(r1 < 200 and r2 == 240)
Save and convert
F.save writes PSD or Aseprite from any Layered (the source format by default, else opts.format or the
destination's extension). Flatten first to write an ordinary image format.
| Format | Writes | Options |
|---|---|---|
| PSD | 8-bit RGB (PSB when a side is over 30000 px): every layer as RLE RGBA channels, groups, masks, clipping, opacity, fill opacity, hidden flags, Unicode names, and a merged composite | composite (an Image to store as the merged image; default: the layers flattened) |
| Aseprite | RGBA sprites (grayscale / indexed when depth asks and the pixels fit), frame 1's layer tree, every frame's cels (linked where a frame reuses an Image), palette, tags |
depth (32, 16, 8), level (zlib level, default 6) |
local L = F.Types.Layered
local doc = L.new(16, 16)
local a = assert(F.ops.image.new(16, 16))
F.ops.image.fill(a, 0, 128, 255)
local b = assert(F.ops.image.new(4, 4))
F.ops.image.fill(b, 255, 255, 0)
doc.layers = { L.layer("Back", a), L.layer("Dot", b, 6, 6) }
local psd = assert(F.save(doc, nil, { format = "psd" }))
local back = assert(F.load(psd))
assert(#back.layers == 2 and back.layers[2].name == "Dot" and back.layers[2].x == 6)
local ase = assert(F.save(doc, nil, { format = "aseprite" }))
assert(F.detect(ase) == "aseprite")
-- PSD -> Aseprite and back keeps the layers
local converted = assert(F.convert(psdBytes, "aseprite"))
assert(#assert(F.load(converted)).layers == 3)
An animation for Aseprite: give frames (each with its own tree and delay) and set layers = frames[1].layers.
Frames must have the same layer structure as frame 1.
local L = F.Types.Layered
local doc = L.new(8, 8)
local dot = assert(F.ops.image.new(2, 2))
F.ops.image.fill(dot, 255, 0, 0)
doc.frames = {}
for i = 1, 4 do
doc.frames[i] = { layers = { L.layer("Dot", dot, i, i) }, delay = 0.1 }
end
doc.layers = doc.frames[1].layers
local ase = assert(F.save(doc, nil, { format = "aseprite" }))
local back = assert(F.load(ase))
assert(#back.frames == 4 and back.frames[3].layers[1].x == 3)
Formats
| Format | Read | Write | Notes |
|---|---|---|---|
| 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.
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 for the capability matrix.
Gotchas
layers[1]is the bottom layer. Insert at the end to put a layer on top.- Layer and mask positions are canvas coordinates; layer images can extend past the canvas and are clipped when flattened.
meta.compositeis whatever merged image the writing application stored (some store a blank one when "maximize compatibility" is off).flattenalways works from the layers.- Flattening is our own compositing (W3C blend formulas); PSD effects (shadows, strokes) and adjustment layers are
not rendered, so the result can differ from Photoshop's
meta.composite. - Linked Aseprite cels share one Image table between frames: copy it (
F.ops.image.copy) before editing a single frame. - Pixels are straight (not premultiplied) alpha, like every Image.