ws-cleanup-types: Types, api, host: honest annotations; return the full annotated value count

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 10:43:19 -05:00
1 parent e35de41c9f
commit 2c15b97cd8
22 files changed
+147 -147

No files matched your search

+9 -9
View File
@@ -672,7 +672,7 @@ local OBJECT = { __object = true }
local objectMeta: { [any]: { any } } = setmetatable({}, { __mode = "k" }) :: any -- object -> its key order local objectMeta: { [any]: { any } } = setmetatable({}, { __mode = "k" }) :: any -- object -> its key order
-- object(t?, keys?) -> t marked as an object, keys = its key order (default: none recorded; Types.set appends) -- object(t?, keys?) -> t marked as an object, keys = its key order (default: none recorded; Types.set appends)
function Types.object(t: { [any]: any }?, keys: { any }?): { [any]: any } function Types.object(t: { [any]: any }?, keys: { any }?): { [any]: any }
local o = t or {} local o: { [any]: any } = t or {}
if getmetatable(o) == nil then if getmetatable(o) == nil then
setmetatable(o, OBJECT) setmetatable(o, OBJECT)
end end
@@ -746,13 +746,13 @@ local function p2(n: number): string
return string.format("%02d", n) return string.format("%02d", n)
end end
local function fmtDate(d: any): string local function fmtDate(d: any): string
local s = "" local s: string = ""
if d.year then if d.year then
s = string.format("%04d-%02d-%02d", d.year, d.month or 1, d.day or 1) s = string.format("%04d-%02d-%02d", d.year, d.month or 1, d.day or 1)
end end
if d.hour then if d.hour then
if s ~= "" then if s ~= "" then
s ..= "T" s = s .. "T"
end end
s ..= p2(d.hour) .. ":" .. p2(d.minute or 0) .. ":" .. p2(d.second or 0) s ..= p2(d.hour) .. ":" .. p2(d.minute or 0) .. ":" .. p2(d.second or 0)
local ns = d.nanosecond or 0 local ns = d.nanosecond or 0
@@ -841,11 +841,11 @@ end
-- "07:32:00.999", "1979-05-27 07:32:00Z", lower-case t / z accepted); out-of-range fields return nil -- "07:32:00.999", "1979-05-27 07:32:00Z", lower-case t / z accepted); out-of-range fields return nil
function DateLib.parse(s: string): Date? function DateLib.parse(s: string): Date?
local d: any = {} local d: any = {}
local rest = s local rest: string = s
local y, mo, dd, r1 = string.match(rest, "^(%d%d%d%d)%-(%d%d)%-(%d%d)(.*)$") local y, mo, dd, r1 = string.match(rest, "^(%d%d%d%d)%-(%d%d)%-(%d%d)(.*)$")
if y then if y then
d.year, d.month, d.day = tonumber(y), tonumber(mo), tonumber(dd) d.year, d.month, d.day = tonumber(y), tonumber(mo), tonumber(dd)
rest = r1 rest = r1 :: string
if rest == "" then if rest == "" then
return if DateLib.valid(d) then DateLib.new(d) else nil return if DateLib.valid(d) then DateLib.new(d) else nil
end end
@@ -860,17 +860,17 @@ function DateLib.parse(s: string): Date?
return nil return nil
end end
d.hour, d.minute = tonumber(h), tonumber(mi) d.hour, d.minute = tonumber(h), tonumber(mi)
rest = r2 rest = r2 :: string
local sec, r3 = string.match(rest, "^:(%d%d)(.*)$") local sec, r3 = string.match(rest, "^:(%d%d)(.*)$")
if not sec then if not sec then
return nil return nil
end end
d.second = tonumber(sec) d.second = tonumber(sec)
rest = r3 rest = r3 :: string
local frac, r4 = string.match(rest, "^%.(%d+)(.*)$") local frac, r4 = string.match(rest, "^%.(%d+)(.*)$")
if frac then if frac then
d.nanosecond = tonumber(string.sub(frac .. "000000000", 1, 9)) d.nanosecond = tonumber(string.sub(frac .. "000000000", 1, 9))
rest = r4 rest = r4 :: string
else else
d.nanosecond = 0 d.nanosecond = 0
end end
@@ -1082,6 +1082,6 @@ Types.CATEGORIES = table.freeze({
subtitle = "subtitles", subtitle = "subtitles",
music = "music", music = "music",
playlist = "playlist", playlist = "playlist",
}) } :: { [string]: string })
return Types return Types
+6 -6
View File
@@ -169,7 +169,7 @@ local function decodeFolder(ctx: Ctx, s: Header.Streams, f: Header.Folder): (buf
end end
local b = buffer.create(size) local b = buffer.create(size)
buffer.copy(b, 0, src, so, size) buffer.copy(b, 0, src, so, size)
return b return b, nil
elseif id == "\3\1\1" then elseif id == "\3\1\1" then
if buffer.len(c.props) < 5 then if buffer.len(c.props) < 5 then
return nil, Err.new(Err.CORRUPT, "bad LZMA properties") return nil, Err.new(Err.CORRUPT, "bad LZMA properties")
@@ -195,7 +195,7 @@ local function decodeFolder(ctx: Ctx, s: Header.Streams, f: Header.Folder): (buf
if buffer.len(b) ~= size then if buffer.len(b) ~= size then
return nil, Err.new(Err.CORRUPT, methodName(id) .. " stream decodes to the wrong size") return nil, Err.new(Err.CORRUPT, methodName(id) .. " stream decodes to the wrong size")
end end
return b return b, nil
elseif FILTERS[id] then elseif FILTERS[id] then
if sl ~= size then if sl ~= size then
return nil, Err.new(Err.CORRUPT, "filter size mismatch") return nil, Err.new(Err.CORRUPT, "filter size mismatch")
@@ -206,7 +206,7 @@ local function decodeFolder(ctx: Ctx, s: Header.Streams, f: Header.Folder): (buf
if not ok then if not ok then
return nil, e return nil, e
end end
return b return b, nil
end end
return nil, Err.new(Err.UNSUPPORTED, methodName(id) .. " is not supported") return nil, Err.new(Err.UNSUPPORTED, methodName(id) .. " is not supported")
end end
@@ -311,7 +311,7 @@ function SevenZip.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive?
local function folderData(fi: number): (buffer?, Err.Err?) local function folderData(fi: number): (buffer?, Err.Err?)
local c = cache[fi] local c = cache[fi]
if c then if c then
return c return c, nil
end end
local f = s.folders[fi] local f = s.folders[fi]
local d, e = decodeFolder(ctx, s, f) local d, e = decodeFolder(ctx, s, f)
@@ -322,7 +322,7 @@ function SevenZip.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive?
return nil, Err.new(Err.CHECKSUM, "folder CRC-32 mismatch") return nil, Err.new(Err.CHECKSUM, "folder CRC-32 mismatch")
end end
cache[fi] = d cache[fi] = d
return d return d, nil
end end
local si = 0 local si = 0
for _, file in parsed.files do for _, file in parsed.files do
@@ -376,7 +376,7 @@ function SevenZip.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive?
if checkCrc and want and Crc32.raw(out, 0, size) ~= want then if checkCrc and want and Crc32.raw(out, 0, size) ~= want then
return nil, Err.new(Err.CHECKSUM, "CRC-32 mismatch in " .. file.name) return nil, Err.new(Err.CHECKSUM, "CRC-32 mismatch in " .. file.name)
end end
return out return out, nil
end end
end end
Archive.put(a, e) Archive.put(a, e)
+16 -16
View File
@@ -78,42 +78,42 @@ end
local function sampleFormat(comp: string, bits: number): (string?, string?) local function sampleFormat(comp: string, bits: number): (string?, string?)
if comp == "NONE" or comp == "twos" or comp == "" then if comp == "NONE" or comp == "twos" or comp == "" then
if bits >= 1 and bits <= 8 then if bits >= 1 and bits <= 8 then
return "s8" return "s8", nil
elseif bits <= 16 then elseif bits <= 16 then
return "s16be" return "s16be", nil
elseif bits <= 24 then elseif bits <= 24 then
return "s24be" return "s24be", nil
elseif bits <= 32 then elseif bits <= 32 then
return "s32be" return "s32be", nil
end end
return nil, "bad sample size " .. bits return nil, "bad sample size " .. bits
elseif comp == "sowt" then elseif comp == "sowt" then
if bits >= 1 and bits <= 8 then if bits >= 1 and bits <= 8 then
return "s8" return "s8", nil
elseif bits <= 16 then elseif bits <= 16 then
return "s16le" return "s16le", nil
elseif bits <= 24 then elseif bits <= 24 then
return "s24le" return "s24le", nil
elseif bits <= 32 then elseif bits <= 32 then
return "s32le" return "s32le", nil
end end
return nil, "bad sample size " .. bits return nil, "bad sample size " .. bits
elseif comp == "raw " then elseif comp == "raw " then
return "u8" return "u8", nil
elseif comp == "in24" then elseif comp == "in24" then
return "s24be" return "s24be", nil
elseif comp == "in32" then elseif comp == "in32" then
return "s32be" return "s32be", nil
elseif comp == "42ni" then elseif comp == "42ni" then
return "s32le" return "s32le", nil
elseif comp == "fl32" or comp == "FL32" then elseif comp == "fl32" or comp == "FL32" then
return "f32be" return "f32be", nil
elseif comp == "fl64" or comp == "FL64" then elseif comp == "fl64" or comp == "FL64" then
return "f64be" return "f64be", nil
elseif comp == "ulaw" or comp == "ULAW" then elseif comp == "ulaw" or comp == "ULAW" then
return "ulaw" return "ulaw", nil
elseif comp == "alaw" or comp == "ALAW" then elseif comp == "alaw" or comp == "ALAW" then
return "alaw" return "alaw", nil
end end
return nil, "compression " .. string.format("%q", comp) return nil, "compression " .. string.format("%q", comp)
end end
+6 -6
View File
@@ -429,7 +429,7 @@ function Decode.parse(b: buffer, base: number, n: number, opts: any, headerOnly:
frame = f frame = f
st.frame = f st.frame = f
if headerOnly then if headerOnly then
return st return st, nil, nil
end end
elseif SOF_REFUSED[m] then elseif SOF_REFUSED[m] then
return nil, "unsupported", SOF_REFUSED[m] .. " JPEG" return nil, "unsupported", SOF_REFUSED[m] .. " JPEG"
@@ -559,12 +559,12 @@ function Decode.parse(b: buffer, base: number, n: number, opts: any, headerOnly:
st.icc = icc st.icc = icc
end end
if headerOnly then if headerOnly then
return st return st, nil, nil
end end
if scans == 0 then if scans == 0 then
return nil, "truncated", "no image data" return nil, "truncated", "no image data"
end end
return st return st, nil, nil
end end
local SPACE_IDS = { gray = Color.GRAY, ycbcr = Color.YCC, rgb = Color.RGB, cmyk = Color.CMYK, ycck = Color.YCCK } local SPACE_IDS = { gray = Color.GRAY, ycbcr = Color.YCC, rgb = Color.RGB, cmyk = Color.CMYK, ycck = Color.YCCK }
@@ -697,7 +697,7 @@ end
function Decode.outputYuv(st: State): (number?, number?, buffer?, string?) function Decode.outputYuv(st: State): (number?, number?, buffer?, string?)
local f: Frame = st.frame local f: Frame = st.frame
if st.scale ~= 1 or f.precision ~= 8 then if st.scale ~= 1 or f.precision ~= 8 then
return nil return nil, nil, nil, nil
end end
local comps = f.comps local comps = f.comps
local space = colorSpaceOf(f, st.adobe, st.jfif) local space = colorSpaceOf(f, st.adobe, st.jfif)
@@ -721,11 +721,11 @@ function Decode.outputYuv(st: State): (number?, number?, buffer?, string?)
end end
end end
if not mode then if not mode then
return nil return nil, nil, nil, nil
end end
for _, c in comps do for _, c in comps do
if c.coef and not c.quant then if c.coef and not c.quant then
return nil -- output() reports the missing table return nil, nil, nil, nil -- output() reports the missing table
end end
end end
local w, h = f.width, f.height local w, h = f.width, f.height
+2 -2
View File
@@ -319,7 +319,7 @@ function Lzma.filter(id: number, b: buffer, o: number, n: number, props: any?):
return nil, Err.new(Err.CORRUPT, "bad delta filter properties") return nil, Err.new(Err.CORRUPT, "bad delta filter properties")
end end
Filters.delta(b, o, n, readu8(pb :: buffer, 0) + 1) Filters.delta(b, o, n, readu8(pb :: buffer, 0) + 1)
return true return true, nil
end end
local f = BCJ[id] local f = BCJ[id]
if not f then if not f then
@@ -332,7 +332,7 @@ function Lzma.filter(id: number, b: buffer, o: number, n: number, props: any?):
return nil, Err.new(Err.CORRUPT, "bad BCJ filter properties") return nil, Err.new(Err.CORRUPT, "bad BCJ filter properties")
end end
f(b, o, n, start) f(b, o, n, start)
return true return true, nil
end end
-- ------------------------------------------------------------------------------------------------ xz -- ------------------------------------------------------------------------------------------------ xz
+7 -7
View File
@@ -159,12 +159,12 @@ local function flatten(path: any, transform: Transform?, tolerance: number?, min
local cmds = commands(path) local cmds = commands(path)
local polys: { { number } } = {} local polys: { { number } } = {}
if not cmds then if not cmds then
return polys return polys, nil, nil, nil, nil
end end
local m = transform or Raster.IDENTITY local m = transform or Raster.IDENTITY
local a, b, c, d, e, f = m[1], m[2], m[3], m[4], m[5], m[6] local a, b, c, d, e, f = m[1], m[2], m[3], m[4], m[5], m[6]
if type(a) ~= "number" or type(b) ~= "number" or type(c) ~= "number" or type(d) ~= "number" or type(e) ~= "number" or type(f) ~= "number" then if type(a) ~= "number" or type(b) ~= "number" or type(c) ~= "number" or type(d) ~= "number" or type(e) ~= "number" or type(f) ~= "number" then
return polys return polys, nil, nil, nil, nil
end end
local tol = tolerance or 0.1 local tol = tolerance or 0.1
if type(tol) ~= "number" or not (tol > 0.001) then if type(tol) ~= "number" or not (tol > 0.001) then
@@ -271,7 +271,7 @@ local function flatten(path: any, transform: Transform?, tolerance: number?, min
table.insert(polys, cur) table.insert(polys, cur)
end end
if #polys == 0 or bx0 ~= bx0 or by0 ~= by0 or bx1 ~= bx1 or by1 ~= by1 then if #polys == 0 or bx0 ~= bx0 or by0 ~= by0 or bx1 ~= bx1 or by1 ~= by1 then
return polys return polys, nil, nil, nil, nil
end end
return polys, bx0, by0, bx1, by1 return polys, bx0, by0, bx1, by1
end end
@@ -411,7 +411,7 @@ function Raster.mask(path: any, opts: Options?): (Mask?, string?)
local o: Options = opts or {} local o: Options = opts or {}
local polys, bx0, by0, bx1, by1 = Raster.flatten(path, o.transform, o.tolerance) local polys, bx0, by0, bx1, by1 = Raster.flatten(path, o.transform, o.tolerance)
if not bx0 then if not bx0 then
return nil return nil, nil
end end
if abs(bx0 :: number) > LIMIT or abs(by0 :: number) > LIMIT or abs(bx1 :: number) > LIMIT or abs(by1 :: number) > LIMIT then if abs(bx0 :: number) > LIMIT or abs(by0 :: number) > LIMIT or abs(bx1 :: number) > LIMIT or abs(by1 :: number) > LIMIT then
return nil, "coordinates out of range" return nil, "coordinates out of range"
@@ -427,7 +427,7 @@ function Raster.mask(path: any, opts: Options?): (Mask?, string?)
end end
local W, H = X1 - X0, Y1 - Y0 local W, H = X1 - X0, Y1 - Y0
if W <= 0 or H <= 0 then if W <= 0 or H <= 0 then
return nil return nil, nil
end end
local maxPixels = if type(o.maxPixels) == "number" then o.maxPixels else 2 ^ 26 local maxPixels = if type(o.maxPixels) == "number" then o.maxPixels else 2 ^ 26
if W * H > maxPixels then if W * H > maxPixels then
@@ -464,7 +464,7 @@ function Raster.mask(path: any, opts: Options?): (Mask?, string?)
writeu8(cov, os + x, floor(v * 255 + 0.5)) writeu8(cov, os + x, floor(v * 255 + 0.5))
end end
end end
return { x = X0, y = Y0, width = W, height = H, coverage = cov } return { x = X0, y = Y0, width = W, height = H, coverage = cov }, nil
end end
-- ------------------------------------------------------------------------------------------------ filling -- ------------------------------------------------------------------------------------------------ filling
@@ -533,7 +533,7 @@ function Raster.fill(img: any, path: any, paint: any, opts: Options?): (boolean,
end end
end end
end end
return true return true, nil
end end
-- ------------------------------------------------------------------------------------------------ stroking -- ------------------------------------------------------------------------------------------------ stroking
+7 -7
View File
@@ -51,7 +51,7 @@ function Video.simpleDecoder(decodeOne: (data: buffer, packet: Types.Packet) ->
if q then if q then
return q[1], q[2], q[3] return q[1], q[2], q[3]
end end
return nil return nil, nil, nil
end end
function self.flush(_: any) end function self.flush(_: any) end
function self.reset(_: any) function self.reset(_: any)
@@ -155,15 +155,15 @@ local function rawDecoder(track: Types.Track, _opts: any?): (Types.VideoCodecDec
if pf == "yuv420p" and bits == 8 then if pf == "yuv420p" and bits == 8 then
local img = Types.Image.new(w, h, "yuv420p") local img = Types.Image.new(w, h, "yuv420p")
buffer.copy(img.pixels, 0, data, 0, need) buffer.copy(img.pixels, 0, data, 0, need)
return img return img, nil
elseif pf == "yv12" then elseif pf == "yv12" then
local img = Types.Image.new(w, h, "yuv420p") local img = Types.Image.new(w, h, "yuv420p")
buffer.copy(img.pixels, 0, data, 0, w * h) buffer.copy(img.pixels, 0, data, 0, w * h)
buffer.copy(img.pixels, w * h, data, w * h + cw * ch, cw * ch) buffer.copy(img.pixels, w * h, data, w * h + cw * ch, cw * ch)
buffer.copy(img.pixels, w * h + cw * ch, data, w * h, cw * ch) buffer.copy(img.pixels, w * h + cw * ch, data, w * h, cw * ch)
return img return img, nil
end end
return rawPlanar(data, pf, bits, w, h) return rawPlanar(data, pf, bits, w, h), nil
end end
local bpp = if pf == "bgra" or pf == "rgba" then 4 else 3 local bpp = if pf == "bgra" or pf == "rgba" then 4 else 3
local stride = if track.stride then track.stride else w * bpp local stride = if track.stride then track.stride else w * bpp
@@ -185,7 +185,7 @@ local function rawDecoder(track: Types.Track, _opts: any?): (Types.VideoCodecDec
buffer.writeu32(px, o + x * 4, a + b * 256 + c * 65536 + 0xff000000) buffer.writeu32(px, o + x * 4, a + b * 256 + c * 65536 + 0xff000000)
end end
end end
return img return img, nil
end) end)
end end
@@ -269,7 +269,7 @@ local function mpeg1Decoder(_track: Types.Track, opts: any?): (Types.VideoCodecD
if q then if q then
return q[1], q[2], q[3] return q[1], q[2], q[3]
end end
return nil return nil, nil, nil
end end
function self.flush(_: any) function self.flush(_: any)
for _ = 1, 4 do for _ = 1, 4 do
@@ -324,7 +324,7 @@ function Video.register(entry: Entry): (boolean, Err.Err?)
for _, m in entry.matroska or {} do for _, m in entry.matroska or {} do
byMatroska[m] = entry.id byMatroska[m] = entry.id
end end
return true return true, nil
end end
for _, e in BUILTIN do for _, e in BUILTIN do
Video.register(e) Video.register(e)
+7 -7
View File
@@ -291,33 +291,33 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any)
if kw == "SYSTEM" then if kw == "SYSTEM" then
local q = match(s, "^[ \t\n]+()", p + 6) :: any local q = match(s, "^[ \t\n]+()", p + 6) :: any
if not q then if not q then
return nil return nil, nil, nil
end end
local sys, e = literal(q) local sys, e = literal(q)
if not sys then if not sys then
return nil return nil, nil, nil
end end
return "", sys, e return "", sys, e
elseif kw == "PUBLIC" then elseif kw == "PUBLIC" then
local q = match(s, "^[ \t\n]+()", p + 6) :: any local q = match(s, "^[ \t\n]+()", p + 6) :: any
if not q then if not q then
return nil return nil, nil, nil
end end
local pub, e = literal(q) local pub, e = literal(q)
if not pub or find(pub, "[^ \r\n%w%-'()+,./:=?;!*#@$_%%]") then if not pub or find(pub, "[^ \r\n%w%-'()+,./:=?;!*#@$_%%]") then
return nil return nil, nil, nil
end end
local r = match(s, "^[ \t\n]+()", e) :: any local r = match(s, "^[ \t\n]+()", e) :: any
if not r then if not r then
return nil return nil, nil, nil
end end
local sys, e2 = literal(r) local sys, e2 = literal(r)
if not sys then if not sys then
return nil return nil, nil, nil
end end
return pub, sys, e2 return pub, sys, e2
end end
return nil return nil, nil, nil
end end
-- check an entity value's references and expand its character references (the rest expand when used) -- check an entity value's references and expand its character references (the rest expand when used)
local function entityValue(v: string): string? local function entityValue(v: string): string?
+3 -3
View File
@@ -59,7 +59,7 @@ function Methods.read(self: any, name: string): (buffer?, Err.Err?)
return nil, Err.new(Err.ARG, "no entry named " .. tostring(name)) return nil, Err.new(Err.ARG, "no entry named " .. tostring(name))
end end
if e.data then if e.data then
return e.data return e.data, nil
end end
if e.load then if e.load then
local ok, b, err = pcall(e.load) local ok, b, err = pcall(e.load)
@@ -69,9 +69,9 @@ function Methods.read(self: any, name: string): (buffer?, Err.Err?)
if not b then if not b then
return nil, err or Err.new(Err.CORRUPT, "entry " .. name .. ": decode failed") return nil, err or Err.new(Err.CORRUPT, "entry " .. name .. ": decode failed")
end end
return b return b, nil
end end
return buffer.create(0) return buffer.create(0), nil
end end
-- add(name, data, opts?) -> (): opts.mtime (unix seconds), opts.dir, opts.mode; replaces an entry of the same name -- add(name, data, opts?) -> (): opts.mtime (unix seconds), opts.dir, opts.mode; replaces an entry of the same name
+2 -2
View File
@@ -242,7 +242,7 @@ end
local function vorbisModes(id: buffer, setup: buffer): ({ number }?, { boolean }?) local function vorbisModes(id: buffer, setup: buffer): ({ number }?, { boolean }?)
if buffer.len(id) < 30 or buffer.len(setup) < 8 then if buffer.len(id) < 30 or buffer.len(setup) < 8 then
return nil return nil, nil
end end
local bs = buffer.readu8(id, 28) local bs = buffer.readu8(id, 28)
local sizes = { 2 ^ (bs % 16), 2 ^ (bs // 16) } local sizes = { 2 ^ (bs % 16), 2 ^ (bs // 16) }
@@ -278,7 +278,7 @@ local function vorbisModes(id: buffer, setup: buffer): ({ number }?, { boolean }
return sizes, out return sizes, out
end end
end end
return nil return nil, nil
end end
-- ------------------------------------------------------------------------------------------------ ADTS -- ------------------------------------------------------------------------------------------------ ADTS
+1 -1
View File
@@ -239,7 +239,7 @@ function D.info(self: Demux, track: number, k: number): (number?, number?, numbe
local list = self.byTrack[track] local list = self.byTrack[track]
local i = list and list[k] local i = list and list[k]
if not i then if not i then
return nil return nil, nil, nil, nil, nil
end end
return self.pts[i], self.dts[i], self.dur[i], self.key[i], self.len[i] return self.pts[i], self.dts[i], self.dur[i], self.key[i], self.len[i]
end end
+9 -9
View File
@@ -21,11 +21,11 @@ Ebml.UNKNOWN = -1
-- vint(buf, pos, limit) -> value (marker removed), length | nil. All-ones values (unknown sizes) return -1. -- vint(buf, pos, limit) -> value (marker removed), length | nil. All-ones values (unknown sizes) return -1.
function Ebml.vint(buf: buffer, pos: number, limit: number): (number?, number?) function Ebml.vint(buf: buffer, pos: number, limit: number): (number?, number?)
if pos >= limit then if pos >= limit then
return nil return nil, nil
end end
local b = readu8(buf, pos) local b = readu8(buf, pos)
if b == 0 then if b == 0 then
return nil return nil, nil
end end
local len = 1 local len = 1
local mask = 0x80 local mask = 0x80
@@ -34,7 +34,7 @@ function Ebml.vint(buf: buffer, pos: number, limit: number): (number?, number?)
mask = mask // 2 mask = mask // 2
end end
if pos + len > limit then if pos + len > limit then
return nil return nil, nil
end end
local v = bit32.band(b, mask - 1) local v = bit32.band(b, mask - 1)
local allOnes = v == mask - 1 local allOnes = v == mask - 1
@@ -55,10 +55,10 @@ end
function Ebml.svint(buf: buffer, pos: number, limit: number): (number?, number?) function Ebml.svint(buf: buffer, pos: number, limit: number): (number?, number?)
local v, len = Ebml.vint(buf, pos, limit) local v, len = Ebml.vint(buf, pos, limit)
if v == nil or len == nil then if v == nil or len == nil then
return nil return nil, nil
end end
if v == -1 then if v == -1 then
return nil return nil, nil
end end
return v - (2 ^ (7 * len - 1) - 1), len return v - (2 ^ (7 * len - 1) - 1), len
end end
@@ -66,12 +66,12 @@ end
-- id(buf, pos, limit) -> the element ID with its marker bits, length | nil (IDs are 1..4 bytes) -- id(buf, pos, limit) -> the element ID with its marker bits, length | nil (IDs are 1..4 bytes)
function Ebml.id(buf: buffer, pos: number, limit: number): (number?, number?) function Ebml.id(buf: buffer, pos: number, limit: number): (number?, number?)
if pos >= limit then if pos >= limit then
return nil return nil, nil
end end
local b = readu8(buf, pos) local b = readu8(buf, pos)
local len = if b >= 0x80 then 1 elseif b >= 0x40 then 2 elseif b >= 0x20 then 3 elseif b >= 0x10 then 4 else 0 local len = if b >= 0x80 then 1 elseif b >= 0x40 then 2 elseif b >= 0x20 then 3 elseif b >= 0x10 then 4 else 0
if len == 0 or pos + len > limit then if len == 0 or pos + len > limit then
return nil return nil, nil
end end
local v = b local v = b
for i = 1, len - 1 do for i = 1, len - 1 do
@@ -84,11 +84,11 @@ end
function Ebml.header(buf: buffer, pos: number, limit: number): (number?, number?, number?) function Ebml.header(buf: buffer, pos: number, limit: number): (number?, number?, number?)
local id, il = Ebml.id(buf, pos, limit) local id, il = Ebml.id(buf, pos, limit)
if not id or not il then if not id or not il then
return nil return nil, nil, nil
end end
local size, sl = Ebml.vint(buf, pos + il, limit) local size, sl = Ebml.vint(buf, pos + il, limit)
if not size or not sl then if not size or not sl then
return nil return nil, nil, nil
end end
return id, size, pos + il + sl return id, size, pos + il + sl
end end
+1 -1
View File
@@ -93,7 +93,7 @@ function Face.loadPages(self: any, loader: any?): (boolean?, Err.Err?)
pages[i] = img pages[i] = img
end end
self.pages = pages self.pages = pages
return true return true, nil
end end
local CHANNEL = { [1] = 2, [2] = 1, [4] = 0, [8] = 3 } -- chnl bit -> byte in rgba8 local CHANNEL = { [1] = 2, [2] = 1, [4] = 0, [8] = 3 } -- chnl bit -> byte in rgba8
+9 -9
View File
@@ -34,7 +34,7 @@ function Woff.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?, s
local maxBytes = (opts and opts.maxBytes) or 2 ^ 28 local maxBytes = (opts and opts.maxBytes) or 2 ^ 28
local base = r.base local base = r.base
if not r:match("wOFF") then if not r:match("wOFF") then
return nil, Err.new(Err.CORRUPT, "bad signature", base) return nil, Err.new(Err.CORRUPT, "bad signature", base), nil
end end
local flavor = r:string(4) local flavor = r:string(4)
local length = r:u32be() local length = r:u32be()
@@ -45,16 +45,16 @@ function Woff.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?, s
local metaOffset, metaLength, metaOrig = r:u32be(), r:u32be(), r:u32be() local metaOffset, metaLength, metaOrig = r:u32be(), r:u32be(), r:u32be()
r:skip(8) r:skip(8)
if r.err then if r.err then
return nil, r.err return nil, r.err, nil
end end
if flavor == "ttcf" then if flavor == "ttcf" then
return nil, Err.new(Err.UNSUPPORTED, "font collections in WOFF", base + 4) return nil, Err.new(Err.UNSUPPORTED, "font collections in WOFF", base + 4), nil
end end
if length > r:size() then if length > r:size() then
return nil, Err.new(Err.TRUNCATED, string.format("file is %d bytes, header says %d", r:size(), length), base + 8) return nil, Err.new(Err.TRUNCATED, string.format("file is %d bytes, header says %d", r:size(), length), base + 8), nil
end end
if numTables == 0 or total > maxBytes or total < 12 + 16 * numTables then if numTables == 0 or total > maxBytes or total < 12 + 16 * numTables then
return nil, Err.new(if total > maxBytes then Err.LIMIT else Err.CORRUPT, "bad table count or sfnt size", base + 12) return nil, Err.new(if total > maxBytes then Err.LIMIT else Err.CORRUPT, "bad table count or sfnt size", base + 12), nil
end end
local dir = {} local dir = {}
for i = 1, numTables do for i = 1, numTables do
@@ -62,7 +62,7 @@ function Woff.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?, s
dir[i] = e dir[i] = e
end end
if r.err then if r.err then
return nil, r.err return nil, r.err, nil
end end
local out = buffer.create(total) local out = buffer.create(total)
local w = Bytes.writer(12) local w = Bytes.writer(12)
@@ -80,10 +80,10 @@ function Woff.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?, s
local p = 12 + 16 * numTables local p = 12 + 16 * numTables
for i, e in dir do for i, e in dir do
if e.offset + e.comp > r:size() or e.comp > e.orig then if e.offset + e.comp > r:size() or e.comp > e.orig then
return nil, Err.new(Err.CORRUPT, "table " .. e.tag .. " out of range", base + 44 + (i - 1) * 20) return nil, Err.new(Err.CORRUPT, "table " .. e.tag .. " out of range", base + 44 + (i - 1) * 20), nil
end end
if p + e.orig > total then if p + e.orig > total then
return nil, Err.new(Err.CORRUPT, "tables larger than totalSfntSize", base + 16) return nil, Err.new(Err.CORRUPT, "tables larger than totalSfntSize", base + 16), nil
end end
local d = 12 + (i - 1) * 16 local d = 12 + (i - 1) * 16
buffer.writestring(out, d, e.tag) buffer.writestring(out, d, e.tag)
@@ -93,7 +93,7 @@ function Woff.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?, s
if e.comp < e.orig then if e.comp < e.orig then
local t, err = Deflate.zlib(r.buf, base + e.offset, e.comp, e.orig) local t, err = Deflate.zlib(r.buf, base + e.offset, e.comp, e.orig)
if not t then if not t then
return nil, Err.new((err :: any).code, "table " .. e.tag .. ": " .. (err :: any).message, base + e.offset) return nil, Err.new((err :: any).code, "table " .. e.tag .. ": " .. (err :: any).message, base + e.offset), nil
end end
buffer.copy(out, p, t, 0, e.orig) buffer.copy(out, p, t, 0, e.orig)
else else
+9 -9
View File
@@ -31,7 +31,7 @@ function Host.readFile(path: string): (buffer?, Err.Err?)
if type(s) ~= "string" then if type(s) ~= "string" then
return nil, Err.new(Err.IO, "cannot read " .. path .. ": " .. tostring(msg)) return nil, Err.new(Err.IO, "cannot read " .. path .. ": " .. tostring(msg))
end end
return buffer.fromstring(s) return buffer.fromstring(s), nil
end end
local f, msg = io().open(path, "rb") local f, msg = io().open(path, "rb")
if not f then if not f then
@@ -39,7 +39,7 @@ function Host.readFile(path: string): (buffer?, Err.Err?)
end end
local s = f:read("*a") local s = f:read("*a")
f:close() f:close()
return buffer.fromstring(s) return buffer.fromstring(s), nil
end end
-- writeFile: relative paths go to the save directory (love.filesystem.write), absolute ones through io -- writeFile: relative paths go to the save directory (love.filesystem.write), absolute ones through io
@@ -50,7 +50,7 @@ function Host.writeFile(path: string, data: buffer): (boolean, Err.Err?)
if not ok then if not ok then
return false, Err.new(Err.IO, "cannot write " .. path .. ": " .. tostring(msg)) return false, Err.new(Err.IO, "cannot write " .. path .. ": " .. tostring(msg))
end end
return true return true, nil
end end
local f, msg = io().open(path, "wb") local f, msg = io().open(path, "wb")
if not f then if not f then
@@ -58,7 +58,7 @@ function Host.writeFile(path: string, data: buffer): (boolean, Err.Err?)
end end
f:write(s) f:write(s)
f:close() f:close()
return true return true, nil
end end
function Host.exists(path: string): boolean function Host.exists(path: string): boolean
@@ -79,8 +79,8 @@ function Host.toImageData(img: any): (any, Err.Err?)
return nil, Err.new(Err.ARG, "toImageData: needs rgba8 pixels, got " .. tostring(img.format)) return nil, Err.new(Err.ARG, "toImageData: needs rgba8 pixels, got " .. tostring(img.format))
end end
local n = img.width * img.height * 4 local n = img.width * img.height * 4
local F = ffi() local F: any = ffi()
local B = buffer :: any local B: { toffi: ((buffer) -> any)? } = buffer :: any
if F and B.toffi then if F and B.toffi then
local id = love().image.newImageData(img.width, img.height, "rgba8") local id = love().image.newImageData(img.width, img.height, "rgba8")
F.copy(id:getFFIPointer(), B.toffi(img.pixels), n) F.copy(id:getFFIPointer(), B.toffi(img.pixels), n)
@@ -94,8 +94,8 @@ end
function Host.fromImageData(id: any): Types.Image function Host.fromImageData(id: any): Types.Image
local w, h = id:getDimensions() local w, h = id:getDimensions()
local img = Types.Image.new(w, h, "rgba8") local img = Types.Image.new(w, h, "rgba8")
local F = ffi() local F: any = ffi()
local B = buffer :: any local B: { toffi: ((buffer) -> any)? } = buffer :: any
if F and B.toffi and id:getFormat() == "rgba8" then if F and B.toffi and id:getFormat() == "rgba8" then
F.copy(B.toffi(img.pixels), id:getFFIPointer(), w * h * 4) F.copy(B.toffi(img.pixels), id:getFFIPointer(), w * h * 4)
return img return img
@@ -131,7 +131,7 @@ function Host.toSoundData(a: any): (any, Err.Err?)
end end
local sd = love().sound.newSoundData(math.max(frames, 1), rate, 16, channels) local sd = love().sound.newSoundData(math.max(frames, 1), rate, 16, channels)
local count = frames * channels local count = frames * channels
local F = ffi() local F: any = ffi()
if F then if F then
local p = F.cast("int16_t*", sd:getFFIPointer()) local p = F.cast("int16_t*", sd:getFFIPointer())
for i = 0, count - 1 do for i = 0, count - 1 do
+4 -4
View File
@@ -31,13 +31,13 @@ end
local host: Host local host: Host
if name == "roblox" then if name == "roblox" then
host = require("@formats/host/Roblox") host = require("@formats/host/Roblox") :: any
elseif name == "love" then elseif name == "love" then
host = require("@formats/host/Love") host = require("@formats/host/Love") :: any
elseif name == "luajit" then elseif name == "luajit" then
host = require("@formats/host/LuaJIT") host = require("@formats/host/LuaJIT") :: any
else else
host = require("@formats/host/Luau") host = require("@formats/host/Luau") :: any
end end
-- LuaJIT (plain or LÖVE): the big decoders (Opus CELT, H.264, ...) outgrow the default 1000 traces / 8 MB of machine -- LuaJIT (plain or LÖVE): the big decoders (Opus CELT, H.264, ...) outgrow the default 1000 traces / 8 MB of machine
-- code and fall back to the interpreter (CELT runs about 2.5x faster with these limits). Raising the limits only -- code and fall back to the interpreter (CELT runs about 2.5x faster with these limits). Raising the limits only
+5 -5
View File
@@ -208,7 +208,7 @@ local function inflateChunk(c: Ctx, comp: number, p: number, len: number, out: b
-- a chunk past the end: keep what exists (truncated files) -- a chunk past the end: keep what exists (truncated files)
len = math.max(0, math.min(len, c.limit - p)) len = math.max(0, math.min(len, c.limit - p))
if p < c.base or p >= c.limit then if p < c.base or p >= c.limit then
return 0 return 0, nil
end end
end end
local src, sp = c.b, p local src, sp = c.b, p
@@ -221,10 +221,10 @@ local function inflateChunk(c: Ctx, comp: number, p: number, len: number, out: b
if comp == 1 then if comp == 1 then
local n = math.min(len, size) local n = math.min(len, size)
buffer.copy(out, o, src, sp, n) buffer.copy(out, o, src, sp, n)
return n return n, nil
elseif comp == 5 then elseif comp == 5 then
local n = Lzw.decode(src, sp, len, out, o, size, "tiff") local n = Lzw.decode(src, sp, len, out, o, size, "tiff")
return n return n, nil
elseif comp == 8 or comp == 32946 then elseif comp == 8 or comp == 32946 then
local raw = Deflate.zlib(src, sp, len, size) local raw = Deflate.zlib(src, sp, len, size)
if not raw then if not raw then
@@ -235,9 +235,9 @@ local function inflateChunk(c: Ctx, comp: number, p: number, len: number, out: b
end end
local n = math.min(buffer.len(raw :: buffer), size) local n = math.min(buffer.len(raw :: buffer), size)
buffer.copy(out, o, raw :: buffer, 0, n) buffer.copy(out, o, raw :: buffer, 0, n)
return n return n, nil
elseif comp == 32773 then elseif comp == 32773 then
return Pixels.packbits(src, sp, len, out, o, size) return Pixels.packbits(src, sp, len, out, o, size), nil
end end
return nil, Err.new(Err.UNSUPPORTED, "compression " .. comp) return nil, Err.new(Err.UNSUPPORTED, "compression " .. comp)
end end
+15 -15
View File
@@ -95,7 +95,7 @@ local function detect(buf: buffer, name: string?, category: string?, format: str
if not e then if not e then
return nil, Err.new(Err.UNKNOWN_FORMAT, "no format named " .. format .. (if category then " in " .. category else "")) return nil, Err.new(Err.UNKNOWN_FORMAT, "no format named " .. format .. (if category then " in " .. category else ""))
end end
return e return e, nil
end end
local ext = Input.extension(name) local ext = Input.extension(name)
local best: Registry.Entry? = nil local best: Registry.Entry? = nil
@@ -117,12 +117,12 @@ local function detect(buf: buffer, name: string?, category: string?, format: str
end end
end end
if best then if best then
return best return best, nil
end end
if ext then if ext then
local e = find(ext, category) local e = find(ext, category)
if e then if e then
return e return e, nil
end end
end end
return nil, Err.new(Err.UNKNOWN_FORMAT, "unrecognised data" .. (if name then " (" .. name .. ")" else "") .. (if category then " for " .. category else "")) return nil, Err.new(Err.UNKNOWN_FORMAT, "unrecognised data" .. (if name then " (" .. name .. ")" else "") .. (if category then " for " .. category else ""))
@@ -196,7 +196,7 @@ 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 if type(value) ~= "table" or value.kind ~= "font" or type(value.meta) ~= "table" or type(value.meta.pageFiles) ~= "table" then
return return
end end
local user = o.loadPage local user: any = o.loadPage
if value.pages or (value.meta.pageLoader and type(user) ~= "function") then if value.pages or (value.meta.pageLoader and type(user) ~= "function") then
return return
end end
@@ -207,7 +207,7 @@ local function pageLoader(value: any, name: string?, o: Options)
value.meta.pageLoader = function(file: string, page: number): (any, Err.Err?) value.meta.pageLoader = function(file: string, page: number): (any, Err.Err?)
local src: any local src: any
if type(user) == "function" then if type(user) == "function" then
src = user(file, page) src = (user :: any)(file, page)
else else
src = (dir :: string) .. file src = (dir :: string) .. file
end end
@@ -304,7 +304,7 @@ end
-- load(src, opts?) -> value | nil, Err -- load(src, opts?) -> value | nil, Err
function F.load(src: any, opts: Options?): (any, Err.Err?) function F.load(src: any, opts: Options?): (any, Err.Err?)
local o = opts or {} local o: Options = opts or {}
local buf, ierr, name = Input.resolve(src, o, host) local buf, ierr, name = Input.resolve(src, o, host)
if not buf then if not buf then
return nil, ierr return nil, ierr
@@ -356,7 +356,7 @@ load = F.load
-- open(src, opts?) -> a streaming decoder (audio Decoder / VideoDecoder / Demuxer) | nil, Err -- open(src, opts?) -> a streaming decoder (audio Decoder / VideoDecoder / Demuxer) | nil, Err
function F.open(src: any, opts: Options?): (any, Err.Err?) function F.open(src: any, opts: Options?): (any, Err.Err?)
local o = opts or {} local o: Options = opts or {}
local buf, ierr, name = Input.resolve(src, o, host) local buf, ierr, name = Input.resolve(src, o, host)
if not buf then if not buf then
return nil, ierr return nil, ierr
@@ -453,7 +453,7 @@ end
-- save(value, dest?, opts?) -> buffer (no dest) | true (written to dest) | nil, Err -- save(value, dest?, opts?) -> buffer (no dest) | true (written to dest) | nil, Err
function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?) function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
local o = opts or {} local o: Options = opts or {}
local entry, m, err, key = encoderFor(value, dest, o) local entry, m, err, key = encoderFor(value, dest, o)
if not entry then if not entry then
return nil, err return nil, err
@@ -513,12 +513,12 @@ function F.saveString(value: any, opts: Options?): (string?, Err.Err?)
if not buf then if not buf then
return nil, err return nil, err
end end
return buffer.tostring(buf) return buffer.tostring(buf), nil
end end
-- convert(src, format, opts?) -> buffer (or true with opts.out = a path) | nil, Err. opts go to both load and save. -- 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?) function F.convert(src: any, format: string, opts: Options?): (any, Err.Err?)
local o = opts or {} local o: Options = opts or {}
local value, err = F.load(src, o) local value, err = F.load(src, o)
if value == nil then if value == nil then
return nil, err return nil, err
@@ -531,7 +531,7 @@ end
-- detect(src, opts?) -> format name | nil, Err -- detect(src, opts?) -> format name | nil, Err
function F.detect(src: any, opts: Options?): (string?, Err.Err?) function F.detect(src: any, opts: Options?): (string?, Err.Err?)
local o = opts or {} local o: Options = opts or {}
local buf, ierr, name = Input.resolve(src, o, host) local buf, ierr, name = Input.resolve(src, o, host)
if not buf then if not buf then
return nil, ierr return nil, ierr
@@ -540,7 +540,7 @@ function F.detect(src: any, opts: Options?): (string?, Err.Err?)
if not entry then if not entry then
return nil, err return nil, err
end end
return entry.name return entry.name, nil
end end
export type FormatInfo = { name: string, category: string, extensions: { string }, mime: { string }, caps: { [string]: any } } export type FormatInfo = { name: string, category: string, extensions: { string }, mime: { string }, caps: { [string]: any } }
@@ -568,7 +568,7 @@ end
-- ------------------------------------------------------------------------------------------------ category facades -- ------------------------------------------------------------------------------------------------ category facades
-- F.image.load / save / open / formats, F.audio..., F.model... : the same calls restricted to one category -- 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 function with(opts: Options?, category: string): Options
local o = if opts then table.clone(opts) else {} local o: Options = if opts then table.clone(opts) else {}
o.category = category o.category = category
return o return o
end end
@@ -597,7 +597,7 @@ F.data.null = Types.null
for _, fn in { "toBodies", "fromBodies", "tessellate" } do for _, fn in { "toBodies", "fromBodies", "tessellate" } do
local name: string = fn local name: string = fn
F.cad[name] = function(value: any, opts: Options?): (any, any) F.cad[name] = function(value: any, opts: Options?): (any, any)
local o = if opts then table.clone(opts) else {} local o: Options = if opts then table.clone(opts) else {}
if o.plugins == nil then if o.plugins == nil then
o.plugins = F.plugins o.plugins = F.plugins
end end
@@ -659,7 +659,7 @@ F.model.loadTextures = function(scene: any, opts: Options?): (any, { [number]: E
if type(scene) ~= "table" or type(scene.textures) ~= "table" then if type(scene) ~= "table" or type(scene.textures) ~= "table" then
return scene, errs return scene, errs
end end
local resolve = opts and opts.resolve local resolve: ((any) -> any)? = opts and opts.resolve
for i, tex in scene.textures do for i, tex in scene.textures do
if type(tex) == "table" and tex.image == nil then if type(tex) == "table" and tex.image == nil then
local data = tex.data local data = tex.data
+23 -23
View File
@@ -57,7 +57,7 @@ local function check(img: any): (boolean?, Err.Err?)
if buffer.len(img.pixels) < need then if buffer.len(img.pixels) < need then
return arg("pixel buffer too small for " .. img.width .. " x " .. img.height .. " " .. img.format) return arg("pixel buffer too small for " .. img.width .. " x " .. img.height .. " " .. img.format)
end end
return true return true, nil
end end
ImageOps.check = check ImageOps.check = check
@@ -66,7 +66,7 @@ local function limit(w: number, h: number, opts: any): (boolean?, Err.Err?)
if w * h > max then if w * h > max then
return nil, Err.new(Err.LIMIT, string.format("%d x %d is over the %d pixel limit", w, h, max)) return nil, Err.new(Err.LIMIT, string.format("%d x %d is over the %d pixel limit", w, h, max))
end end
return true return true, nil
end end
-- a new Image of the same kind with a copy of meta's plain fields (source, palette...) but no frames -- a new Image of the same kind with a copy of meta's plain fields (source, palette...) but no frames
@@ -93,7 +93,7 @@ function ImageOps.new(w: number, h: number, format: string?): (Image?, Err.Err?)
if not ok then if not ok then
return nil, e return nil, e
end end
return Types.Image.new(w, h, fmt :: any) return Types.Image.new(w, h, fmt :: any), nil
end end
-- copy(img) -> a deep copy of the pixels (frames are shared) -- copy(img) -> a deep copy of the pixels (frames are shared)
@@ -105,7 +105,7 @@ function ImageOps.copy(img: Image): (Image?, Err.Err?)
local out = like(img, img.width, img.height) local out = like(img, img.width, img.height)
buffer.copy(out.pixels, 0, img.pixels, 0, buffer.len(out.pixels)) buffer.copy(out.pixels, 0, img.pixels, 0, buffer.len(out.pixels))
out.frames = img.frames out.frames = img.frames
return out return out, nil
end end
-- ------------------------------------------------------------------------------------------------ convert -- ------------------------------------------------------------------------------------------------ convert
@@ -127,7 +127,7 @@ function ImageOps.convert(img: Image, format: string): (Image?, Err.Err?)
local rgba = like(img, w, h, "rgba8") local rgba = like(img, w, h, "rgba8")
K.yuvToRgba(img.pixels, w, h, rgba.pixels) K.yuvToRgba(img.pixels, w, h, rgba.pixels)
if format == "rgba8" then if format == "rgba8" then
return rgba return rgba, nil
end end
src = rgba src = rgba
end end
@@ -139,10 +139,10 @@ function ImageOps.convert(img: Image, format: string): (Image?, Err.Err?)
K.convert(src.pixels, K.ID[src.format], rgba.pixels, K.ID.rgba8, w * h) K.convert(src.pixels, K.ID[src.format], rgba.pixels, K.ID.rgba8, w * h)
end end
K.rgbaToYuv(rgba.pixels, w, h, out.pixels) K.rgbaToYuv(rgba.pixels, w, h, out.pixels)
return out return out, nil
end end
K.convert(src.pixels, K.ID[src.format], out.pixels, K.ID[format], w * h) K.convert(src.pixels, K.ID[src.format], out.pixels, K.ID[format], w * h)
return out return out, nil
end end
-- ops on yuv420p run on rgba8 and convert back -- ops on yuv420p run on rgba8 and convert back
@@ -179,7 +179,7 @@ function ImageOps.crop(img: Image, x: number, y: number, w: number, h: number):
local bpp = Types.BPP[im.format] :: number local bpp = Types.BPP[im.format] :: number
local out = like(im, x1 - x0, y1 - y0) local out = like(im, x1 - x0, y1 - y0)
K.copyRect(out.pixels, out.width, im.pixels, im.width, x0, y0, 0, 0, out.width, out.height, bpp) K.copyRect(out.pixels, out.width, im.pixels, im.width, x0, y0, 0, 0, out.width, out.height, bpp)
return out return out, nil
end) end)
end end
@@ -195,7 +195,7 @@ function ImageOps.flip(img: Image, axis: string): (Image?, Err.Err?)
return viaRgba(img, function(im: Image): (Image?, Err.Err?) return viaRgba(img, function(im: Image): (Image?, Err.Err?)
local out = like(im, im.width, im.height) local out = like(im, im.width, im.height)
K.flip(im.pixels, out.pixels, im.width, im.height, Types.BPP[im.format] :: number, axis == "h") K.flip(im.pixels, out.pixels, im.width, im.height, Types.BPP[im.format] :: number, axis == "h")
return out return out, nil
end) end)
end end
@@ -216,7 +216,7 @@ function ImageOps.rotate90(img: Image, turns: number): (Image?, Err.Err?)
local swap = t ~= 2 local swap = t ~= 2
local out = like(im, if swap then im.height else im.width, if swap then im.width else im.height) local out = like(im, if swap then im.height else im.width, if swap then im.width else im.height)
K.rotate90(im.pixels, out.pixels, im.width, im.height, Types.BPP[im.format] :: number, t) K.rotate90(im.pixels, out.pixels, im.width, im.height, Types.BPP[im.format] :: number, t)
return out return out, nil
end) end)
end end
@@ -245,7 +245,7 @@ function ImageOps.resize(img: Image, w: number, h: number, filter: string?, opts
local out = like(im, w, h) local out = like(im, w, h)
if f == "nearest" then if f == "nearest" then
K.nearest(im.pixels, im.width, im.height, bpp, out.pixels, w, h) K.nearest(im.pixels, im.width, im.height, bpp, out.pixels, w, h)
return out return out, nil
end end
local kind, ch = K.KIND[fmt], K.CHANNELS[fmt] local kind, ch = K.KIND[fmt], K.CHANNELS[fmt]
local src, sw, sh = im.pixels, im.width, im.height local src, sw, sh = im.pixels, im.width, im.height
@@ -264,14 +264,14 @@ function ImageOps.resize(img: Image, w: number, h: number, filter: string?, opts
K.resamplePass(src, sw, sh, ch, kind, out.pixels, h, K.coeffs(sh, h, f), false) K.resamplePass(src, sw, sh, ch, kind, out.pixels, h, K.coeffs(sh, h, f), false)
elseif src == im.pixels or (w == im.width and alpha) then elseif src == im.pixels or (w == im.width and alpha) then
buffer.copy(out.pixels, 0, im.pixels, 0, w * h * bpp) buffer.copy(out.pixels, 0, im.pixels, 0, w * h * bpp)
return out return out, nil
else else
out.pixels = src out.pixels = src
end end
if alpha then if alpha then
K.premultiply(out.pixels, w * h, alpha, kind, true) K.premultiply(out.pixels, w * h, alpha, kind, true)
end end
return out return out, nil
end) end)
end end
@@ -333,7 +333,7 @@ function ImageOps.rotate(img: Image, degrees: number, opts: { expand: boolean?,
if alpha then if alpha then
K.premultiply(out.pixels, nw * nh, alpha, kind, true) K.premultiply(out.pixels, nw * nh, alpha, kind, true)
end end
return out return out, nil
end) end)
end end
@@ -371,7 +371,7 @@ function ImageOps.blit(dst: Image, src: Image, x: number, y: number, opts: BlitO
local w = math.min(src.width - sx, dst.width - dx) local w = math.min(src.width - sx, dst.width - dx)
local h = math.min(src.height - sy, dst.height - dy) local h = math.min(src.height - sy, dst.height - dy)
if w <= 0 or h <= 0 then if w <= 0 or h <= 0 then
return dst return dst, nil
end end
if dst.format == "yuv420p" or (blend == "over" and not RGBA[dst.format]) then if dst.format == "yuv420p" or (blend == "over" and not RGBA[dst.format]) then
-- work on an rgba copy of the region, then write it back in dst's format -- work on an rgba copy of the region, then write it back in dst's format
@@ -387,7 +387,7 @@ function ImageOps.blit(dst: Image, src: Image, x: number, y: number, opts: BlitO
local back = ImageOps.convert(work, dst.format) :: Image local back = ImageOps.convert(work, dst.format) :: Image
K.copyRect(dst.pixels, dst.width, back.pixels, back.width, 0, 0, dx, dy, w, h, Types.BPP[dst.format] :: number) K.copyRect(dst.pixels, dst.width, back.pixels, back.width, 0, 0, dx, dy, w, h, Types.BPP[dst.format] :: number)
end end
return dst return dst, nil
end end
local s = src local s = src
if src.format ~= dst.format then if src.format ~= dst.format then
@@ -400,7 +400,7 @@ function ImageOps.blit(dst: Image, src: Image, x: number, y: number, opts: BlitO
else else
K.overFloat(dst.pixels, dst.width, s.pixels, s.width, sx, sy, dx, dy, w, h, opacity, K.KIND[dst.format]) K.overFloat(dst.pixels, dst.width, s.pixels, s.width, sx, sy, dx, dy, w, h, opacity, K.KIND[dst.format])
end end
return dst return dst, nil
end end
-- composite(dst, src, x?, y?, opts?) -> a new Image: a copy of dst with src blitted onto it -- composite(dst, src, x?, y?, opts?) -> a new Image: a copy of dst with src blitted onto it
@@ -423,7 +423,7 @@ local function premul(img: Image, un: boolean): (Image?, Err.Err?)
if ch then if ch then
K.premultiply(out.pixels, img.width * img.height, ch, K.KIND[img.format], un) K.premultiply(out.pixels, img.width * img.height, ch, K.KIND[img.format], un)
end end
return out return out, nil
end end
-- premultiply(img) / unpremultiply(img) -> a new Image (formats without alpha are copied unchanged) -- premultiply(img) / unpremultiply(img) -> a new Image (formats without alpha are copied unchanged)
@@ -543,7 +543,7 @@ function ImageOps.setPixel(img: Image, x: number, y: number, r: number, g: numbe
else else
wu8(px, i, q(al)) wu8(px, i, q(al))
end end
return true return true, nil
end end
-- fill(img, r, g, b, a?) -> img (in place) with every pixel set (values as setPixel) -- fill(img, r, g, b, a?) -> img (in place) with every pixel set (values as setPixel)
@@ -567,7 +567,7 @@ function ImageOps.fill(img: Image, r: number, g: number, b: number, a: number?):
buffer.copy(img.pixels, filled, img.pixels, 0, c) buffer.copy(img.pixels, filled, img.pixels, 0, c)
filled += c filled += c
end end
return img return img, nil
end end
-- ------------------------------------------------------------------------------------------------ animation -- ------------------------------------------------------------------------------------------------ animation
@@ -602,7 +602,7 @@ function ImageOps.coalesce(img: Image): ({ Image }?, Err.Err?)
end end
local frames = img.frames local frames = img.frames
if type(frames) ~= "table" or #frames == 0 then if type(frames) ~= "table" or #frames == 0 then
return { img } return { img }, nil
end end
local fmt = if RGBA[img.format] then img.format else "rgba8" local fmt = if RGBA[img.format] then img.format else "rgba8"
local canvas = Types.Image.new(img.width, img.height, fmt :: any) local canvas = Types.Image.new(img.width, img.height, fmt :: any)
@@ -647,7 +647,7 @@ function ImageOps.coalesce(img: Image): ({ Image }?, Err.Err?)
canvas.pixels = saved canvas.pixels = saved
end end
end end
return out return out, nil
end end
-- fromFrames(images, delays?) -> an animated Image: img.frames = the images as full-canvas frames (x = y = 0, -- fromFrames(images, delays?) -> an animated Image: img.frames = the images as full-canvas frames (x = y = 0,
@@ -683,7 +683,7 @@ function ImageOps.fromFrames(images: { Image }, delays: (number | { number })?):
buffer.copy(out.pixels, 0, first.pixels, 0, buffer.len(out.pixels)) buffer.copy(out.pixels, 0, first.pixels, 0, buffer.len(out.pixels))
out.frames = frames :: any out.frames = frames :: any
out.loops = 0 out.loops = 0
return out return out, nil
end end
return ImageOps return ImageOps
+3 -3
View File
@@ -23,19 +23,19 @@ end
function Data.base64(text: string, compression: string?): ({ number }?, string?, boolean?) function Data.base64(text: string, compression: string?): ({ number }?, string?, boolean?)
local raw, err = Base64.decode(text) local raw, err = Base64.decode(text)
if not raw then if not raw then
return nil, err return nil, err, nil
end end
local bytes: buffer = raw local bytes: buffer = raw
if compression == "zlib" then if compression == "zlib" then
local z, zerr = Deflate.zlib(bytes) local z, zerr = Deflate.zlib(bytes)
if not z then if not z then
return nil, "zlib: " .. tostring(zerr) return nil, "zlib: " .. tostring(zerr), nil
end end
bytes = z bytes = z
elseif compression == "gzip" then elseif compression == "gzip" then
local z, zerr = Deflate.gunzip(bytes) local z, zerr = Deflate.gunzip(bytes)
if not z then if not z then
return nil, "gzip: " .. tostring(zerr) return nil, "gzip: " .. tostring(zerr), nil
end end
bytes = z bytes = z
elseif compression ~= nil and compression ~= "" then elseif compression ~= nil and compression ~= "" then
+2 -2
View File
@@ -87,11 +87,11 @@ local function gids(v: any, encoding: string?, compression: string?): ({ number
local out = {} local out = {}
for _, g in list(v) do for _, g in list(v) do
if type(g) ~= "number" or g < 0 or g >= 4294967296 then if type(g) ~= "number" or g < 0 or g >= 4294967296 then
return nil, "bad gid" return nil, "bad gid", nil
end end
table.insert(out, g) table.insert(out, g)
end end
return out, nil return out, nil, nil
end end
local function object(o: any): any local function object(o: any): any
+1 -1
View File
@@ -79,7 +79,7 @@ local function gids(d: Node, encoding: string?, compression: string?): ({ number
for _, t in Xml.children(d, "tile") do for _, t in Xml.children(d, "tile") do
table.insert(out, num(t, "gid", 0) :: number) table.insert(out, num(t, "gid", 0) :: number)
end end
return out, nil return out, nil, nil
end end
return nil, "unknown encoding " .. encoding, true return nil, "unknown encoding " .. encoding, true
end end