diff --git a/src/Rosetta/layered/aseprite/init.luau b/src/Rosetta/layered/aseprite/init.luau index bafac03..0a2e637 100644 --- a/src/Rosetta/layered/aseprite/init.luau +++ b/src/Rosetta/layered/aseprite/init.luau @@ -1,17 +1,7 @@ --!strict --- layered/aseprite: Aseprite .ase / .aseprite sprites (aseprite docs/ase-file-specs.md, v1.3). Supports: RGBA, --- grayscale and indexed sprites (to rgba8; the transparent index is clear except on background layers); every --- frame (duration -> delay); layers (normal and group, child levels -> a tree; visibility, blend mode, opacity, --- flags, UUIDs skipped; a group of normal blend at full opacity is "passThrough"); cels (raw, zlib-compressed, linked: a linked cel shares the Image of the frame it points to --- and records `link`), cel opacity (-> the leaf's fillOpacity), z-index; the palette (new 0x2019 chunk and the old --- 0x0004 / 0x0011 ones; entry names), tags (meta.tags, frames 1-based), user data text / colour on layers, cels and --- tags, the pixel ratio and grid. A Layered value: layers = frame 1's tree, frames[i] = { layers = frame i's tree, --- delay }. --- Encode: RGBA (or grayscale / indexed when opts.depth or meta.colorDepth asks and the pixels fit), one layer chunk --- per node of frame 1's tree, zlib cels, linked cels where a frame reuses an earlier frame's Image at the same place, --- palette, tags. --- Not supported: tilemap layers / tilesets (skipped: meta.skipped counts them), slices, external files, colour --- profiles (sRGB assumed). +-- layered/aseprite: Aseprite .ase / .aseprite (aseprite docs/ase-file-specs.md, v1.3). Supports: RGBA / grayscale / +-- indexed, frames, layer tree (groups, blend, opacity), raw / zlib / linked cels, palettes (0x2019, 0x0004 / 0x0011), +-- tags, user data. Not supported: tilemaps / tilesets (meta.skipped), slices, external files, colour profiles (sRGB). -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") @@ -30,13 +20,12 @@ local Aseprite = { } export type DecodeOptions = { - maxPixels: number?, -- refuse sprites / cels larger than this (default 2^26) + maxPixels: number?, -- default 2^26 maxFrames: number?, -- default 65535 } export type EncodeOptions = { - depth: number?, -- 32 (RGBA), 16 (grayscale) or 8 (indexed, with layered.meta.palette); default meta.colorDepth if - -- the pixels fit it, else 32 - level: number?, -- zlib level for the cels (default 6) + depth: number?, -- 32, 16 or 8; default meta.colorDepth if the pixels fit, else 32 + level: number?, -- default 6 } -- ------------------------------------------------------------------------------------------------ constants @@ -67,7 +56,6 @@ local function str(r: Bytes.Reader): string return r:string(r:u16le()) end --- cel pixels (depth bytes per pixel) -> rgba8 local function toRGBA(src: buffer, so: number, n: number, depth: number, palette: buffer, transparent: number, background: boolean): buffer local out = buffer.create(n * 4) if depth == 32 then @@ -131,8 +119,8 @@ function Aseprite.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Layered? local tags: { any } = {} local frames: { { cels: { [number]: any }, delay: number } } = {} local skipped = 0 - local lastUser: any = nil -- the object the next user data chunk belongs to - local tagUser = 0 -- tags waiting for their user data chunks + local lastUser: any = nil + local tagUser = 0 for fi = 1, nframes do local fstart = r.pos local fbytes = r:u32le() @@ -324,10 +312,9 @@ function Aseprite.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Layered? r.pos = fstart + fbytes end local _ = ncolors - -- the layer tree of one frame local function build(fi: number): { Types.Layer } local root: { Types.Layer } = {} - local stack: { { Types.Layer } } = { root } -- stack[level + 1] = the children list at that level + local stack: { { Types.Layer } } = { root } for li, info in infos do local level = math.min(info.level, #stack - 1) local list = stack[level + 1] @@ -353,7 +340,6 @@ function Aseprite.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Layered? l.opacity = 1 end table.insert(list, l) - -- deeper layers go into this group for k = #stack, level + 2, -1 do stack[k] = nil end @@ -392,9 +378,8 @@ function Aseprite.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Layered? end return root end - -- cel images (shared by linked cels) for _, f in frames do - for li, cel in pairs(f.cels) do -- sparse: pairs, not ipairs-style iteration + for li, cel in pairs(f.cels) do if cel.raw then local info = infos[li] local img = Types.Image.new(cel.w, cel.h) @@ -470,7 +455,6 @@ function Aseprite.encode(layered: Types.Layered, opts: EncodeOptions?): (buffer? end local meta: any = if type(layered.meta) == "table" then layered.meta else {} local level = opts and opts.level or 6 - -- frames: the layered frames, or one frame of the layers local frameList: { any } = if type(layered.frames) == "table" and #layered.frames > 0 then layered.frames else { { layers = layered.layers, delay = 0.1 } } local struct: { Node } = {} if not nodes(frameList[1].layers, 0, struct) then @@ -498,7 +482,6 @@ function Aseprite.encode(layered: Types.Layered, opts: EncodeOptions?): (buffer? end perFrame[fi] = ns end - -- the colour depth: requested, or the source's when every pixel fits local depth = opts and opts.depth or meta.colorDepth or 32 local explicit = opts ~= nil and opts.depth ~= nil if depth ~= 32 and depth ~= 16 and depth ~= 8 then @@ -614,7 +597,6 @@ function Aseprite.encode(layered: Types.Layered, opts: EncodeOptions?): (buffer? chunk(fw, 0x2020, c) return 1 end - -- where each (layer, frame) cel's image was first written: linked cels local firstAt: { [number]: { [any]: { frame: number, x: number, y: number, fill: number } } } = {} for fi, f in frameList do local fw = Bytes.writer(4096) diff --git a/src/Rosetta/layered/ora/init.luau b/src/Rosetta/layered/ora/init.luau index 4258578..4ec4903 100644 --- a/src/Rosetta/layered/ora/init.luau +++ b/src/Rosetta/layered/ora/init.luau @@ -1,13 +1,7 @@ --!strict --- layered/ora: OpenRaster (the OpenRaster 0.0.6 specification, freedesktop.org; new code): a zip (containers/Zip) with --- a stored "mimetype" first, stack.xml (codecs/text/Xml), PNG layers, mergedimage.png and Thumbnails/thumbnail.png. --- Decode: the stack tree (nested = groups: isolation="auto" is passThrough; name / src / x / y / --- opacity / visibility / composite-op, svg:* ops mapped to the codecs/image/Blend names, unknown ones kept as written), --- layers bottom to top as Types.Layered, the PNGs through the api hook opts.decodeImage (F.load passes it; without it: --- UNSUPPORTED), mergedimage.png as meta.composite, the resolution (meta.dpi). Encode: a Layered (rgba8 layers, groups) --- with every layer, the merged image (codecs/image/Blend flattens the tree, or opts.composite) and a thumbnail (up to --- 256 px, nearest) as PNGs through opts.encodeImage. Not supported: non-PNG layer sources, the / / --- elements (ignored). +-- layered/ora: OpenRaster 0.0.6 (freedesktop.org; new code): a zip with a stored "mimetype" first, stack.xml, PNG +-- layers, mergedimage.png, Thumbnails/thumbnail.png. Supports: nested stacks (isolation="auto" = passThrough), layer +-- attributes, svg:* ops; PNGs via opts.decodeImage / encodeImage. Not supported: non-PNG sources, / . -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") @@ -29,14 +23,14 @@ local Ora = { -- ------------------------------------------------------------------------------------------------ types export type DecodeOptions = { - decodeImage: ((buffer) -> (Types.Image?, Err.Err?))?, -- decodes the PNG layers (the api passes one) - composite: boolean?, -- decode mergedimage.png into meta.composite (default true) + decodeImage: ((buffer) -> (Types.Image?, Err.Err?))?, + composite: boolean?, -- default true maxLayers: number?, -- default 10000 } export type EncodeOptions = { - encodeImage: ((Types.Image, string) -> (buffer?, Err.Err?))?, -- writes the PNGs (the api passes one) - composite: Types.Image?, -- the merged image (default: the layers flattened with codecs/image/Blend) - thumbnail: boolean?, -- write Thumbnails/thumbnail.png (default true) + encodeImage: ((Types.Image, string) -> (buffer?, Err.Err?))?, + composite: Types.Image?, -- default: the layers flattened + thumbnail: boolean?, -- default true } -- ------------------------------------------------------------------------------------------------ constants @@ -110,7 +104,7 @@ function Ora.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Layered?, Err end return img, nil end - -- a 's children, top first in the file -> bottom to top + -- stack.xml lists a 's children top first local function stack(node: any, ox: number, oy: number): ({ Types.Layer }?, Err.Err?) local out: { Types.Layer } = {} for _, ch in node.children do @@ -179,7 +173,6 @@ function Ora.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Layered?, Err end -- ------------------------------------------------------------------------------------------------ encode --- nearest-neighbour downscale to fit max x max local function thumbnail(img: Types.Image, max: number): Types.Image local w, h = img.width, img.height local s = math.min(1, max / math.max(w, h)) @@ -218,7 +211,6 @@ function Ora.encode(doc: Types.Layered, opts: EncodeOptions?): (buffer?, Err.Err end return data, nil end - -- layers bottom to top -> xml children top first local function stack(layers: { Types.Layer }): ({ any }?, Err.Err?) local kids = {} for i = #layers, 1, -1 do diff --git a/src/Rosetta/layered/psd/Channels.luau b/src/Rosetta/layered/psd/Channels.luau index 035c17e..d9d6886 100644 --- a/src/Rosetta/layered/psd/Channels.luau +++ b/src/Rosetta/layered/psd/Channels.luau @@ -1,9 +1,7 @@ --!strict --!native --- layered/psd/Channels: PSD / PSB channel data. Decodes one channel (raw, PackBits RLE, ZIP, ZIP with prediction) to a --- plane of big-endian samples, converts planes to rgba8 for every colour mode, and PackBits-encodes rows for the --- writer. Adobe Photoshop File Formats Specification (2016), "Channel image data" / "Image data section". --- Never throws: every read is range-checked first; failures return nil, "message". +-- layered/psd/Channels: PSD / PSB channel data (Photoshop File Formats Specification 2016, "Channel image data" / +-- "Image data section"): raw, PackBits RLE, ZIP, ZIP with prediction; colour modes to rgba8; PackBits rows. local Deflate = require("@rosetta/codecs/compress/Deflate") local PackBits = require("@rosetta/codecs/compress/PackBits") @@ -14,7 +12,6 @@ local readf32, writef32, bcreate, bcopy, blen, bfill = buffer.readf32, buffer.wr local floor, min, max = math.floor, math.min, math.max local byteswap = bit32.byteswap --- rowBytes(w, depth) -> bytes per row of one channel function Channels.rowBytes(w: number, depth: number): number if depth == 1 then return (w + 7) // 8 @@ -22,7 +19,6 @@ function Channels.rowBytes(w: number, depth: number): number return w * (depth // 8) end --- rleCounts(src, p, limit, rows, psb) -> { counts }, offset after them | nil function Channels.rleCounts(src: buffer, p: number, limit: number, rows: number, psb: boolean): ({ number }?, number) local size = if psb then 4 else 2 if rows < 0 or p + rows * size > limit then @@ -40,7 +36,6 @@ function Channels.rleCounts(src: buffer, p: number, limit: number, rows: number, return counts, p end --- rle(src, p, limit, counts, first, rows, rowBytes, dst, d) -> offset after the rows | nil, message function Channels.rle(src: buffer, p: number, limit: number, counts: { number }, first: number, rows: number, rowBytes: number, dst: buffer, d: number): (number?, string?) for i = 0, rows - 1 do local c = counts[first + i] or 0 @@ -57,7 +52,6 @@ function Channels.rle(src: buffer, p: number, limit: number, counts: { number }, return p, nil end --- unpredict(plane, w, h, depth): undo ZIP-with-prediction deltas in place local function unpredict(plane: buffer, w: number, h: number, depth: number) if depth == 8 then for y = 0, h - 1 do @@ -101,8 +95,6 @@ local function unpredict(plane: buffer, w: number, h: number, depth: number) end end --- channel(src, p, limit, w, h, depth, psb) -> plane (h rows of rowBytes) | nil, message. src[p, limit) holds the --- 2-byte compression code and the data of one layer channel. function Channels.channel(src: buffer, p: number, limit: number, w: number, h: number, depth: number, psb: boolean): (buffer?, string?) local rb = Channels.rowBytes(w, depth) local plane = bcreate(rb * h) @@ -143,31 +135,29 @@ function Channels.channel(src: buffer, p: number, limit: number, w: number, h: n return plane, nil end --- sample8(plane, i, depth) -> sample i as 0..255 local function sample8(plane: buffer, i: number, depth: number): number if depth == 8 then return readu8(plane, i) elseif depth == 16 then return floor((readu8(plane, i * 2) * 256 + readu8(plane, i * 2 + 1)) / 257 + 0.5) elseif depth == 32 then - local f = readf32(plane, i * 4) -- the plane holds big-endian floats: swapped by the caller + local f = readf32(plane, i * 4) if f ~= f then return 0 end return floor(max(0, min(1, f)) * 255 + 0.5) end - -- 1-bit (rows are padded, so this is only right for whole-byte widths; toRGBA reads bitmaps itself): 1 = black + -- 1-bit: 1 = black (whole-byte widths only; toRGBA reads bitmaps itself) return if readu8(plane, i // 8) // 2 ^ (7 - i % 8) % 2 == 1 then 0 else 255 end --- swap32(plane): big-endian f32 samples to native order, in place function Channels.swap32(plane: buffer) for i = 0, blen(plane) // 4 - 1 do writeu32(plane, i * 4, byteswap(readu32(plane, i * 4))) end end --- lab(L, a, b) 0..255 each (PSD Lab) -> sRGB r, g, b 0..255 (D50 -> D65 approximated by D65 white) +-- PSD Lab (0..255 each) to sRGB, the D50 white approximated by D65 local function srgb(c: number): number c = if c <= 0.0031308 then 12.92 * c else 1.055 * c ^ (1 / 2.4) - 0.055 return floor(max(0, min(1, c)) * 255 + 0.5) @@ -188,8 +178,7 @@ local function lab(l8: number, a8: number, b8: number): (number, number, number) return srgb(r), srgb(g), srgb(bl) end --- toRGBA(planes, w, h, depth, mode, palette?, dst?) -> rgba8 buffer. planes: { [1..k] = colour planes, alpha = plane? } --- mode: PSD colour mode (0 bitmap, 1 grey, 2 indexed, 3 RGB, 4 CMYK, 7 multichannel, 8 duotone, 9 Lab) +-- PSD colour modes: 0 bitmap, 1 grey, 2 indexed, 3 RGB, 4 CMYK, 7 multichannel, 8 duotone, 9 Lab function Channels.toRGBA(planes: { [any]: buffer }, w: number, h: number, depth: number, mode: number, palette: buffer?): buffer local n = w * h local out = bcreate(n * 4) @@ -197,7 +186,6 @@ function Channels.toRGBA(planes: { [any]: buffer }, w: number, h: number, depth: local alpha = planes.alpha local bitmapW = (w + 7) // 8 if depth == 8 and (mode == 3 or mode == 1) and c1 and blen(c1) >= n then - -- the common case, without a call per sample local g8 = if mode == 3 and c2 and blen(c2) >= n then c2 else c1 local b8 = if mode == 3 and c3 and blen(c3) >= n then c3 else c1 local a8 = if alpha and blen(alpha) >= n then alpha else nil @@ -250,7 +238,6 @@ function Channels.toRGBA(planes: { [any]: buffer }, w: number, h: number, depth: return out end --- gray8(plane, w, h, depth) -> 1 byte per pixel function Channels.gray8(plane: buffer, w: number, h: number, depth: number): buffer local n = w * h local out = bcreate(n) @@ -277,7 +264,6 @@ function Channels.unmatte(rgba: buffer, n: number) end end --- matte(rgba, n) -> a copy matted with white (the writer's merged image) function Channels.matte(rgba: buffer, n: number): buffer local out = bcreate(n * 4) bcopy(out, 0, rgba, 0, n * 4) @@ -295,12 +281,10 @@ function Channels.matte(rgba: buffer, n: number): buffer end -- ------------------------------------------------------------------------------------------------ writer helpers --- packRow(src, o, n, out, d) -> bytes written: PackBits of src[o, o + n) into out at d (out needs n + n // 128 + 1) function Channels.packRow(src: buffer, o: number, n: number, out: buffer, d: number): number local start = d local i = 0 while i < n do - -- a run of at least 3 equal bytes? local v = readu8(src, o + i) local run = 1 while i + run < n and run < 128 and readu8(src, o + i + run) == v do @@ -312,7 +296,6 @@ function Channels.packRow(src: buffer, o: number, n: number, out: buffer, d: num d += 2 i += run else - -- literals up to the next run of 3 local j = i while j < n and j - i < 128 do if j + 2 < n and readu8(src, o + j) == readu8(src, o + j + 1) and readu8(src, o + j) == readu8(src, o + j + 2) then @@ -330,7 +313,6 @@ function Channels.packRow(src: buffer, o: number, n: number, out: buffer, d: num return d - start end --- planes8(rgba, w, h) -> r, g, b, a planes (1 byte per pixel each) function Channels.planes8(rgba: buffer, w: number, h: number): (buffer, buffer, buffer, buffer) local n = w * h local r, g, b, a = bcreate(n), bcreate(n), bcreate(n), bcreate(n) @@ -347,7 +329,6 @@ function Channels.planes8(rgba: buffer, w: number, h: number): (buffer, buffer, return r, g, b, a end --- rleChannel(plane, w, h, psb) -> the RLE data of one channel (counts, then rows), without the compression code function Channels.rleChannel(plane: buffer, w: number, h: number, psb: boolean): buffer local cs = if psb then 4 else 2 local out = bcreate(h * cs + h * (w + w // 128 + 2)) diff --git a/src/Rosetta/layered/psd/init.luau b/src/Rosetta/layered/psd/init.luau index 919fe91..f65a879 100644 --- a/src/Rosetta/layered/psd/init.luau +++ b/src/Rosetta/layered/psd/init.luau @@ -1,15 +1,7 @@ --!strict --- layered/psd: Adobe Photoshop PSD and PSB (Adobe Photoshop File Formats Specification, 2016). Supports: decode of --- bitmap, greyscale, indexed, RGB, CMYK, Lab, duotone and multichannel documents at 1 / 8 / 16 / 32 bits; layers --- (raw, PackBits RLE, ZIP and ZIP-with-prediction channels; 16 / 32-bit layers in Lr16 / Lr32), their blend mode, --- opacity, fill opacity (iOpa), visibility, clipping, Unicode names (luni), user masks (as grey masks), groups (lsct / --- lsdk section dividers -> group layers with children; pass-through kept as the "passThrough" blend); the merged image --- (meta.composite); image resources (meta.resources, ICC profile in meta.icc). Everything converts to rgba8. --- Encode: an 8-bit RGB PSD (PSB when a side is over 30000) with the RLE merged composite (ops-free: codecs/image/Blend --- flattens the tree, or opts.composite) and every layer as RLE RGBA channels, groups as section dividers, masks, --- clipping, opacity / fill, hidden flags and Unicode names. --- Not supported: adjustment / fill / type / smart-object layer contents (their pixels, if any, are used), layer --- effects, vector masks, colour management, 16 / 32-bit or non-RGB output. +-- layered/psd: Adobe Photoshop PSD / PSB (Photoshop File Formats Specification, 2016). Supports: every colour mode at +-- 1 / 8 / 16 / 32 bits, layers (RLE / ZIP channels, Lr16 / Lr32, iOpa, luni, masks, lsct groups), the merged image, +-- resources; encode: 8-bit RGB. Not supported: adjustment / type / smart-object contents, effects, vector masks. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@rosetta/core/Err") @@ -30,12 +22,12 @@ local Psd = { } export type DecodeOptions = { - layers: boolean?, -- read the layers (default true; false reads only the merged image) - maxPixels: number?, -- refuse documents / layers larger than this (default 2^26) + layers: boolean?, -- default true (false: the merged image only) + maxPixels: number?, -- default 2^26 maxLayers: number?, -- default 10000 } export type EncodeOptions = { - composite: Types.Image?, -- the merged image to store (default: the layers flattened with codecs/image/Blend) + composite: Types.Image?, -- default: the layers flattened } -- ------------------------------------------------------------------------------------------------ constants @@ -101,7 +93,6 @@ local function utf16be(s: string): string return (string.gsub(Text.utf16(buffer.fromstring(s), 0, #s, true), "%z", "")) end --- the layer records + channel data at r (a Reader over the layer info, after its length): flat records | nil, Err local function layerRecords(r: Bytes.Reader, c: Ctx): ({ any }?, boolean, Err.Err?) local count = r:i16be() local absAlpha = count < 0 @@ -225,7 +216,7 @@ local function layerRecords(r: Bytes.Reader, c: Ctx): ({ any }?, boolean, Err.Er end end if ch.size < 2 then - continue -- an empty channel + continue end local plane, perr = Channels.channel(b, p, p + ch.size, cw, chh, c.depth, c.psb) if not plane then @@ -251,7 +242,6 @@ local function layerRecords(r: Bytes.Reader, c: Ctx): ({ any }?, boolean, Err.Er return recs, absAlpha, nil end --- records (bottom to top) -> a layer tree local function tree(recs: { any }): { Types.Layer } local root: { Types.Layer } = {} local stack: { { Types.Layer } } = {} @@ -467,7 +457,7 @@ function Psd.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Layered?, Err planes[k] = plane end elseif comp == 2 or comp == 3 then - -- one zlib stream per channel would be Channels.channel; Photoshop writes all channels in one stream + -- Photoshop writes all channels in one zlib stream local all = Channels.channel(b, r.pos - 2, r.limit, w, h * nread, depth, c.psb) if not all then return nil, Err.new(Err.CORRUPT, "merged image: bad ZIP data", r.pos)