comments: init.luau docstrings out (usage is in docs/API.md and the guide)
This commit is contained in:
1 parent
07c874574c
commit
9427e6e5fb
1 file changed
+13
-97
+13
-97
@@ -1,20 +1,7 @@
|
||||
--!strict
|
||||
-- Rosetta: read, write and convert file formats in pure Luau, on Roblox and on LuaJIT / LÖVE (through tools/build.py).
|
||||
-- Every format of a category maps onto one standard type (src/Rosetta/Types.luau): a PNG and a WebP both load as an
|
||||
-- Image. Formats are found through the generated Registry: by content (each format's sniff), then by extension.
|
||||
--
|
||||
-- local F = require(Rosetta) -- Roblox: the ModuleScript; LuaJIT: require("rosetta"); luau: "@rosetta"
|
||||
-- local img, err = F.load(src, opts?) -- src: buffer | string | path (hosts with files); err: core/Err
|
||||
-- local img = F.image.load(src) -- the same, restricted to one category (F.audio, F.model, ...)
|
||||
-- local dec = F.audio.open(src) -- streaming decoders (audio / video)
|
||||
-- local buf = F.save(img, nil, { format = "png" }) -- a buffer; without format: the source format
|
||||
-- F.save(img, "out.png") -- a file (the format from the extension), on hosts with files
|
||||
-- local s = F.saveString(img, { format = "png" })
|
||||
-- local buf = F.convert(src, "png", opts?) -- load + save
|
||||
-- F.formats() -- the capability list (also docs/FORMATS.md)
|
||||
-- F.ops.image.resize(img, 64, 64) -- editing: F.ops.image / mesh / layers / audio / music / vector
|
||||
--
|
||||
-- Never throws on bad data or bad arguments: every function returns nil, Err (Err.code, Err.message, tostring(err)).
|
||||
-- Formats are found through the generated Registry: by content (each format's sniff), then by extension. Never throws
|
||||
-- on bad data or bad arguments: every function returns nil, Err.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Input = require("@rosetta/core/Input")
|
||||
@@ -32,12 +19,11 @@ F.version = "0.1.0"
|
||||
F.Err = Err
|
||||
F.Types = Types
|
||||
F.Bytes = Bytes
|
||||
F.Rbx = Rbx -- the Roblox instance tree helpers (codecs/model/Rbx: tree, instance, add, set, get, find, descendants)
|
||||
F.Rbx = Rbx
|
||||
F.null = Types.null
|
||||
F.host = host
|
||||
F.plugins = {} :: { [string]: any } -- injected optional dependencies (plugins.brep for cad/step)
|
||||
|
||||
-- value.kind -> category
|
||||
local KIND_CATEGORY: { [string]: string } = {
|
||||
image = "image",
|
||||
audio = "audio",
|
||||
@@ -56,7 +42,6 @@ local KIND_CATEGORY: { [string]: string } = {
|
||||
playlist = "playlist",
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ registry access
|
||||
local loaded: { [string]: any } = {}
|
||||
|
||||
local function module(entry: Registry.Entry): (any, Err.Err?)
|
||||
@@ -76,7 +61,6 @@ local function inCategory(entry: Registry.Entry, category: string?): boolean
|
||||
return category == nil or entry.category == category
|
||||
end
|
||||
|
||||
-- find(nameOrExt, category?) -> the entry whose name, then whose extension, matches
|
||||
local function find(key: string, category: string?): Registry.Entry?
|
||||
local k = string.lower(key)
|
||||
for _, e in Registry do
|
||||
@@ -92,7 +76,6 @@ local function find(key: string, category: string?): Registry.Entry?
|
||||
return nil
|
||||
end
|
||||
|
||||
-- detect(buf, name?, category?, format?) -> entry | nil, Err: opts.format, else the best sniff, else the extension
|
||||
local function detect(buf: buffer, name: string?, category: string?, format: string?): (Registry.Entry?, Err.Err?)
|
||||
if format then
|
||||
local e = find(format, category)
|
||||
@@ -132,9 +115,7 @@ local function detect(buf: buffer, name: string?, category: string?, format: str
|
||||
return nil, Err.new(Err.UNKNOWN_FORMAT, "unrecognised data" .. (if name then " (" .. name .. ")" else "") .. (if category then " for " .. category else ""))
|
||||
end
|
||||
|
||||
-- detectFor(buf, name, o) -> entry | nil, Err, rendered: detect with o.category / o.format; every music file that
|
||||
-- renders (MOD, S3M, XM, IT, MIDI) is also audio, so an audio lookup that finds nothing falls back to them (rendered =
|
||||
-- true: open the format, which renders it, instead of decoding it)
|
||||
-- music that renders (MOD, S3M, XM, IT, MIDI) is also audio: an audio lookup falls back to it (rendered = true)
|
||||
local function detectFor(buf: buffer, name: string?, o: { [string]: any }): (Registry.Entry?, Err.Err?, boolean)
|
||||
local entry, err = detect(buf, name, o.category, o.format)
|
||||
if not entry and o.category == "audio" then
|
||||
@@ -159,7 +140,6 @@ local function tag(value: any, formatName: string)
|
||||
end
|
||||
end
|
||||
|
||||
-- full decode through a streaming decoder (audio formats with open but no decode)
|
||||
local function drain(dec: any): any
|
||||
local channels = dec.channels
|
||||
local parts, total = {}, 0
|
||||
@@ -183,7 +163,7 @@ local function drain(dec: any): any
|
||||
return a
|
||||
end
|
||||
|
||||
-- siblings(path) -> resolve(uri): reads files relative to path's directory (no absolute paths, no "..", no URLs)
|
||||
-- siblings resolve relative to path's directory only (no absolute paths, no "..", no URLs)
|
||||
local function siblings(path: string): (uri: string) -> buffer?
|
||||
local dir = string.match(path, "^(.*[/\\])") or ""
|
||||
return function(uri: string): buffer?
|
||||
@@ -203,13 +183,10 @@ local function siblings(path: string): (uri: string) -> buffer?
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ API
|
||||
export type Options = { [string]: any } -- format (name or extension), category, path, data, name, + format options
|
||||
|
||||
local load: (src: any, opts: Options?) -> (any, Err.Err?)
|
||||
|
||||
-- bitmap fonts (font/bmfont) name their page images: attach a lazy loader, opts.loadPage(file, page) -> Image | buffer
|
||||
-- | string, else the files next to the descriptor on hosts with a file system
|
||||
local function pageLoader(value: any, name: string?, o: Options)
|
||||
if type(value) ~= "table" or value.kind ~= "font" or type(value.meta) ~= "table" or type(value.meta.pageFiles) ~= "table" then
|
||||
return
|
||||
@@ -230,7 +207,7 @@ local function pageLoader(value: any, name: string?, o: Options)
|
||||
src = (dir :: string) .. file
|
||||
end
|
||||
if type(src) == "table" then
|
||||
return src -- already an Image
|
||||
return src
|
||||
end
|
||||
if src == nil then
|
||||
return nil, Err.new(Err.IO, "no data for page " .. file)
|
||||
@@ -239,11 +216,8 @@ local function pageLoader(value: any, name: string?, o: Options)
|
||||
end
|
||||
end
|
||||
|
||||
-- single-stream archives (gzip / zst / xz / bz2 / br: meta.stream) whose one entry sniffs as another archive (a
|
||||
-- .tar.gz) load as that inner archive, with meta.compression = the outer format. opts.unwrap = false keeps the outer
|
||||
-- one; an explicit opts.format also does. Package archives name their payload entry in meta.payload (a .deb's
|
||||
-- data.tar.xz): they load as that inner archive too, with meta.container = the outer format and meta.outer = the
|
||||
-- outer archive (its control data). A single-stream archive holding a .pcf / .psf(u) / .bdf font loads as the font.
|
||||
-- a single-stream archive (meta.stream) holding another archive or a .pcf / .psf(u) / .bdf font loads as that, with
|
||||
-- meta.compression = the outer format; package archives load as meta.payload (meta.container / meta.outer)
|
||||
local function unwrap(value: any, formatName: string, o: Options): any
|
||||
if o.unwrap == false or o.format ~= nil or (o._depth or 0) >= 3 then
|
||||
return value
|
||||
@@ -292,18 +266,8 @@ local function unwrap(value: any, formatName: string, o: Options): any
|
||||
return v
|
||||
end
|
||||
|
||||
-- Embedded images (PNG inside ICO, JPEG inside TIFF, thumbnails, textures): a format never requires another format,
|
||||
-- so the api hands decoders opts.decodeImage(buf) -> Image | nil, Err and encoders opts.encodeImage(img, format) ->
|
||||
-- buffer | nil, Err (unless the caller passed its own). Formats work without them (returning Err.UNSUPPORTED for
|
||||
-- the embedded parts) when called directly.
|
||||
-- They also see the injected plug-ins as opts.plugins (F.plugins: the B-rep kernel for cad/step) unless given their own.
|
||||
--
|
||||
-- Tracker modules inside other files (music/umx): opts.decodeModule(buf, format?) -> Module | nil, Err, the bytes loaded
|
||||
-- as music (sniffed, or as the given format).
|
||||
--
|
||||
-- Audio tracks of video containers: opts.openAudio(demuxer, track) -> audio Decoder | nil, Err. The track's packets
|
||||
-- become a stream an audio format reads (containers/AudioWrap: PCM -> WAV, Opus / Vorbis -> Ogg, AAC -> ADTS, MP3 /
|
||||
-- FLAC / AC-3 as they are), opened through F.open with that format.
|
||||
-- api hooks (a format never requires another format): opts.decodeImage / encodeImage, opts.plugins, opts.decodeModule,
|
||||
-- opts.openAudio (containers/AudioWrap: PCM -> WAV, Opus / Vorbis -> Ogg, AAC -> ADTS, MP3 / FLAC / AC-3 as they are)
|
||||
local function openAudio(demuxer: any, track: any): (any, Err.Err?)
|
||||
local buf, fmt, err = AudioWrap.wrap(demuxer, track)
|
||||
if not buf then
|
||||
@@ -345,7 +309,6 @@ local function withHooks(o: Options): Options
|
||||
end
|
||||
end
|
||||
if c.decodeModule == nil then
|
||||
-- tracker modules inside other files (music/umx: the module of an Unreal package), as their own format
|
||||
c.decodeModule = function(b: buffer, format: string?): (any, Err.Err?)
|
||||
return F.load(b, { category = "music", format = format })
|
||||
end
|
||||
@@ -353,7 +316,6 @@ local function withHooks(o: Options): Options
|
||||
return c
|
||||
end
|
||||
|
||||
-- load(src, opts?) -> value | nil, Err
|
||||
function F.load(src: any, opts: Options?): (any, Err.Err?)
|
||||
local o: Options = opts or {}
|
||||
local buf, ierr, name = Input.resolve(src, o, host)
|
||||
@@ -373,7 +335,6 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
|
||||
return nil, merr
|
||||
end
|
||||
if o.resolve == nil and name and name == (type(src) == "string" and src or nil) and host.hasFiles then
|
||||
-- files next to the one loaded (OBJ .mtl, glTF .bin / images): opts.resolve(uri) for the decoder
|
||||
o = table.clone(o)
|
||||
o.resolve = siblings(name)
|
||||
end
|
||||
@@ -405,7 +366,6 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
|
||||
end
|
||||
load = F.load
|
||||
|
||||
-- open(src, opts?) -> a streaming decoder (audio Decoder / VideoDecoder / Demuxer) | nil, Err
|
||||
function F.open(src: any, opts: Options?): (any, Err.Err?)
|
||||
local o: Options = opts or {}
|
||||
local buf, ierr, name = Input.resolve(src, o, host)
|
||||
@@ -448,10 +408,6 @@ end
|
||||
|
||||
local encodeTextures: (scene: any) -> Err.Err?
|
||||
|
||||
-- videoToImage(v, opts) -> an animated rgba8 Image of the video's frames (for the GIF / APNG / WebP writers) | nil,
|
||||
-- Err. opts.frames: how many frames to keep (default all, up to the cap); opts.fps: the output frame rate (frames are
|
||||
-- picked at 1 / fps steps, each held as long as it covers; default the source's own timing); opts.maxFrames: the cap
|
||||
-- (default 300); opts.maxPixels: a cap on frames * width * height (default 2^25). Reads the decoder from where it is.
|
||||
local function videoToImage(v: any, o: Options): (any, Err.Err?)
|
||||
if type(v.nextFrame) ~= "function" or type(v.toRGBA) ~= "function" then
|
||||
return nil, Err.new(Err.ARG, "save: not a VideoDecoder")
|
||||
@@ -510,9 +466,6 @@ local function videoToImage(v: any, o: Options): (any, Err.Err?)
|
||||
return img, nil
|
||||
end
|
||||
|
||||
-- renderMusic(value, opts) -> the audio Decoder of a Module (codecs/audio/Tracker, or the Module's own render method
|
||||
-- for other engines) or a Midi (music/midi render) |
|
||||
-- nil, Err. opts are the render options: sampleRate, soundfont, gain, tail, interpolation, stereoSeparation, ...
|
||||
local TRACKER_FAMILY = { mod = true, s3m = true, xm = true, it = true }
|
||||
local function renderMusic(value: any, o: Options): (any, Err.Err?)
|
||||
local ok, dec, err
|
||||
@@ -523,8 +476,7 @@ local function renderMusic(value: any, o: Options): (any, Err.Err?)
|
||||
end
|
||||
ok, dec, err = pcall(m.render, value, o)
|
||||
elseif TRACKER_FAMILY[value.engine or value.format] == nil and type(value.render) == "function" then
|
||||
-- a Module with its own engine (AHX / HVL: codecs/audio/Hively): its render method (a failed render is an
|
||||
-- empty Decoder carrying .err)
|
||||
-- AHX / HVL render through the Module's own method (a failed render is an empty Decoder carrying .err)
|
||||
ok, dec = pcall(value.render, value, o)
|
||||
if ok and type(dec) == "table" and dec.err then
|
||||
return nil, dec.err
|
||||
@@ -542,7 +494,6 @@ local function renderMusic(value: any, o: Options): (any, Err.Err?)
|
||||
return dec, nil
|
||||
end
|
||||
|
||||
-- the writable format for a value: opts.format, the destination's extension, or the source format
|
||||
local function encoderFor(value: any, dest: string?, o: Options): (Registry.Entry?, any, Err.Err?, string?)
|
||||
local category = o.category
|
||||
if category == nil and type(value) == "table" and type(value.kind) == "string" then
|
||||
@@ -602,17 +553,12 @@ function encodeTextures(scene: any): Err.Err?
|
||||
return nil
|
||||
end
|
||||
|
||||
-- save(value, dest?, opts?) -> buffer (no dest) | true (written to dest) | nil, Err. Formats that write extra files
|
||||
-- (caps.extraFiles: bmfont pages) return them without a dest as a third value { [file name] = bytes } (or fill the
|
||||
-- caller's opts.files); with a dest they are written next to it.
|
||||
function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?, { [string]: any }?)
|
||||
local o: Options = opts or {}
|
||||
local entry, m, err, key = encoderFor(value, dest, o)
|
||||
if not entry then
|
||||
return nil, err
|
||||
end
|
||||
-- the encoder sees the format key it was picked by ("glb" vs "gltf"), the destination's name, and (writing a file)
|
||||
-- an opts.files table for the extra files it writes (OBJ .mtl, glTF .bin)
|
||||
o = table.clone(o)
|
||||
o.format = o.format or key
|
||||
if dest then
|
||||
@@ -621,7 +567,7 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?, { [s
|
||||
o.files = {}
|
||||
end
|
||||
elseif o.files == nil and entry.caps.extraFiles then
|
||||
o.files = {} -- returned as the third value: F.save(ttf, nil, { format = "bmfont" }) -> fnt, nil, pages
|
||||
o.files = {}
|
||||
end
|
||||
if entry.category == "video" and type(value) == "table" and value.kind == "image" then
|
||||
local v, verr = F.video.fromImage(value)
|
||||
@@ -638,7 +584,7 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?, { [s
|
||||
value = img
|
||||
end
|
||||
if entry.category == "audio" and type(value) == "table" and (value.kind == "music" or value.kind == "midi") then
|
||||
local dec, rerr = renderMusic(value, o) -- the render options (sampleRate, soundfont, loop, ...) come from opts
|
||||
local dec, rerr = renderMusic(value, o)
|
||||
if not dec then
|
||||
return nil, rerr
|
||||
end
|
||||
@@ -679,7 +625,6 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?, { [s
|
||||
return buf
|
||||
end
|
||||
|
||||
-- saveString(value, opts?) -> string | nil, Err
|
||||
function F.saveString(value: any, opts: Options?): (string?, Err.Err?)
|
||||
local buf, err = F.save(value, nil, opts)
|
||||
if not buf then
|
||||
@@ -688,7 +633,6 @@ function F.saveString(value: any, opts: Options?): (string?, Err.Err?)
|
||||
return buffer.tostring(buf), nil
|
||||
end
|
||||
|
||||
-- convert(src, format, opts?) -> buffer (or true with opts.out = a path) | nil, Err. opts go to both load and save.
|
||||
function F.convert(src: any, format: string, opts: Options?): (any, Err.Err?, { [string]: any }?)
|
||||
local o: Options = opts or {}
|
||||
local value, err = F.load(src, o)
|
||||
@@ -701,7 +645,6 @@ function F.convert(src: any, format: string, opts: Options?): (any, Err.Err?, {
|
||||
return F.save(value, o.out, so)
|
||||
end
|
||||
|
||||
-- detect(src, opts?) -> format name | nil, Err
|
||||
function F.detect(src: any, opts: Options?): (string?, Err.Err?)
|
||||
local o: Options = opts or {}
|
||||
local buf, ierr, name = Input.resolve(src, o, host)
|
||||
@@ -725,7 +668,6 @@ end
|
||||
|
||||
export type FormatInfo = { name: string, category: string, extensions: { string }, mime: { string }, caps: { [string]: any } }
|
||||
|
||||
-- formats(category?) -> { FormatInfo } (no format module is loaded)
|
||||
function F.formats(category: string?): { FormatInfo }
|
||||
local out = {}
|
||||
for _, e in Registry do
|
||||
@@ -736,7 +678,6 @@ function F.formats(category: string?): { FormatInfo }
|
||||
return out
|
||||
end
|
||||
|
||||
-- format(name) -> the format module itself (its contract table: sniff / decode / encode / extras such as png.info)
|
||||
function F.format(name: string): (any, Err.Err?)
|
||||
local e = find(name)
|
||||
if not e then
|
||||
@@ -745,8 +686,6 @@ function F.format(name: string): (any, Err.Err?)
|
||||
return module(e)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ category facades
|
||||
-- F.image.load / save / open / formats, F.audio..., F.model... : the same calls restricted to one category
|
||||
local function with(opts: Options?, category: string): Options
|
||||
local o: Options = if opts then table.clone(opts) else {}
|
||||
o.category = category
|
||||
@@ -771,9 +710,6 @@ for category in Types.CATEGORIES do
|
||||
end
|
||||
F.data.null = Types.null
|
||||
|
||||
-- F.cad.toBodies(cad, opts?) -> bodies, result | nil, Err; F.cad.fromBodies(bodies, opts?) -> Cad | nil, Err;
|
||||
-- F.cad.tessellate(cad | bodies, opts?) -> Scene | nil, Err: cad/step's B-rep calls with the injected kernel
|
||||
-- (F.plugins.brep, unless opts.brep / opts.plugins is given). Without a plug-in they return Err.UNSUPPORTED.
|
||||
for _, fn in { "toBodies", "fromBodies", "tessellate" } do
|
||||
local name: string = fn
|
||||
F.cad[name] = function(value: any, opts: Options?): (any, any)
|
||||
@@ -795,16 +731,12 @@ for _, fn in { "toBodies", "fromBodies", "tessellate" } do
|
||||
return a, b
|
||||
end
|
||||
end
|
||||
-- F.music.new / addPattern / setCell / getCell / addSample / addInstrument / note: building and editing tracker
|
||||
-- Modules (ops/MusicOps, loaded on first use): F.music.new({ format = "mod", channels = 4 }) -> Module
|
||||
for _, fn in { "new", "addPattern", "setCell", "getCell", "addSample", "addInstrument", "note" } do
|
||||
local name: string = fn
|
||||
F.music[name] = function(...: any): (any, Err.Err?)
|
||||
return (F.ops.music :: any)[name](...)
|
||||
end
|
||||
end
|
||||
-- F.video.fromImage(img, opts?) -> a frames-backed VideoDecoder: an animated GIF / APNG / WebP (img.frames, composited
|
||||
-- onto full canvases by ImageOps.coalesce) or a still image (one frame). opts.rgba is accepted (frames are rgba8).
|
||||
F.video.fromImage = function(img: any, opts: Options?): (any, Err.Err?)
|
||||
if type(img) ~= "table" or img.kind ~= "image" then
|
||||
return nil, Err.new(Err.ARG, "fromImage: expected an Image")
|
||||
@@ -830,18 +762,11 @@ F.video.fromImage = function(img: any, opts: Options?): (any, Err.Err?)
|
||||
local src = type(img.meta) == "table" and img.meta.source
|
||||
return Video.fromFrames(frames, delays, { format = type(src) == "table" and src.format or "image", loops = img.loops, meta = img.meta })
|
||||
end
|
||||
-- F.video.fromFrames(images, delays?, opts?) -> a VideoDecoder over a list of same-size images (an image sequence:
|
||||
-- F.save(F.video.fromFrames(list, 1 / 25), "out.avi")); delays in seconds (a number = every frame, default 0.1)
|
||||
F.video.fromFrames = function(images: any, delays: any?, opts: Options?): (any, Err.Err?)
|
||||
return Video.fromFrames(images, delays, opts)
|
||||
end
|
||||
-- F.video.codecs: the video codec registry (codecs/video/Video: register / entry / fromFourcc / fromMatroska / decoder)
|
||||
F.video.codecs = Video
|
||||
|
||||
-- F.model.loadTextures(scene, opts?) -> scene, { [index]: Err }: decodes each texture's encoded bytes (texture.data, or
|
||||
-- texture.uri through opts.resolve) into texture.image with F.image.load. Model formats never decode images
|
||||
-- themselves (a format does not require another format); this is the lazy step at the api layer. Textures whose
|
||||
-- image format is missing or broken keep image = nil and report an Err.
|
||||
F.model.loadTextures = function(scene: any, opts: Options?): (any, { [number]: Err.Err })
|
||||
local errs: { [number]: Err.Err } = {}
|
||||
if type(scene) ~= "table" or type(scene.textures) ~= "table" then
|
||||
@@ -867,15 +792,6 @@ F.model.loadTextures = function(scene: any, opts: Options?): (any, { [number]: E
|
||||
return scene, errs
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ ops
|
||||
-- F.ops: editing on the standard types, loaded on first use.
|
||||
-- F.ops.image ops/ImageOps convert, crop, resize, flip, rotate, blit / composite, coalesce, ...
|
||||
-- F.ops.mesh ops/MeshOps triangulate, transform, computeNormals / Tangents, weld, merge, meshVoxels, ...
|
||||
-- F.ops.layers ops/LayerOps flatten / flattenFrames a Layered (PSD, Aseprite), leaves
|
||||
-- F.ops.audio ops/AudioOps toAudio, gain / gainDb / normalize, mix, remix / downmix / upmix, resample, trim,
|
||||
-- concat, fade, peak, rms
|
||||
-- F.ops.music ops/MusicOps new, addPattern, setCell / getCell, addSample, addInstrument, note (also on F.music)
|
||||
-- F.ops.vector.rasterize(vector, w?, h?, opts?) -> rgba8 Image: the SVG renderer (vector/svg) for any Vector
|
||||
local OPS: { [string]: () -> any } = {
|
||||
image = function()
|
||||
return ImageOps
|
||||
|
||||
Reference in new issue
Block a user