ws-cleanup-types: archive, audio, data, font, image, layered, music, playlist, subtitle, vector, video: annotations, casts, one value per re-assignment where the solver loses the type

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 11:00:25 -05:00
1 parent b0d0a99fec
commit 9af7138b2e
44 files changed
+172 -119

No files matched your search

+4 -3
View File
@@ -177,7 +177,8 @@ local function folder(r: Bytes.Reader): Folder
check(r)
local nin, nout = 1, 1
if bit32.band(flags, 0x10) ~= 0 then
nin, nout = number(r), number(r)
nin = number(r)
nout = number(r)
if nin > 64 or nout > 64 then
bad(r, "bad coder stream count")
end
@@ -416,11 +417,11 @@ local function filesInfo(r: Bytes.Reader): { File }
if n > 1000000 then
bad(r, "too many files", Err.LIMIT)
end
local files: { File } = table.create(n)
local files: { File } = table.create(n) :: { File }
for i = 1, n do
files[i] = { name = "", emptyStream = false, emptyFile = false, anti = false }
end
local empties = {}
local empties: { number } = {}
while true do
local t = byte(r)
if t == K.End then
+10 -6
View File
@@ -144,7 +144,9 @@ local function decodeFolder(ctx: Ctx, s: Header.Streams, f: Header.Folder): (buf
if not b then
return nil, e
end
src, so, sl = b, 0, buffer.len(b)
src = b
so = 0
sl = buffer.len(b)
else
local k = table.find(f.packed, inIdx)
if not k then
@@ -162,7 +164,7 @@ local function decodeFolder(ctx: Ctx, s: Header.Streams, f: Header.Folder): (buf
src = ctx.buf
end
local id = c.id
local opts = { maxBytes = ctx.maxBytes }
local opts: { maxBytes: number?, verify: boolean? } = { maxBytes = ctx.maxBytes }
if id == "\0" then
if sl < size then
return nil, Err.new(Err.TRUNCATED, "short stored stream")
@@ -226,7 +228,7 @@ local function folderMethod(f: Header.Folder): string
end
function SevenZip.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive?, Err.Err?)
local o = opts or {}
local o: DecodeOptions = opts or {}
local maxBytes = if type(o.maxBytes) == "number" then o.maxBytes else 2 ^ 30
local checkCrc = o.checkCrc ~= false
local b, base, limit = r.buf, r.base, r.limit
@@ -280,17 +282,19 @@ function SevenZip.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive?
if f.crc and Crc32.raw(hb, 0, buffer.len(hb)) ~= f.crc then
return nil, Err.new(Err.CHECKSUM, "encoded header CRC-32 mismatch")
end
hbuf, hoff, hlen = hb, 0, buffer.len(hb)
hbuf = hb
hoff = 0
hlen = buffer.len(hb)
end
if not parsed then
return nil, Err.new(Err.CORRUPT, "nested encoded headers")
end
local s = parsed.streams or { packPos = 0, packSizes = {}, folders = {}, subSizes = {}, subCrcs = {} }
local s: Header.Streams = parsed.streams or { packPos = 0, packSizes = {}, folders = {}, subSizes = {}, subCrcs = {} }
a.meta.folders = #s.folders
a.meta.solid = false
-- folder caches and the substream -> folder map
local cache: { [number]: buffer } = {}
local subFolder, subOffset = {}, {}
local subFolder: { number }, subOffset: { number } = {}, {}
do
local si = 1
for fi, f in s.folders do
+4 -4
View File
@@ -49,7 +49,7 @@ local function header(b: buffer, o: number, e: number): { [string]: any }
if bit32.band(flg, 4) ~= 0 and q + 2 <= e then
q += 2 + buffer.readu16(b, q)
end
for _, f in { { 8, "name" }, { 16, "comment" } } do
for _, f in { { 8, "name" }, { 16, "comment" } } :: { { any } } do
if bit32.band(flg, f[1]) ~= 0 then
local s = q
while q < e and buffer.readu8(b, q) ~= 0 do
@@ -85,7 +85,7 @@ function Gzip.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
size = buffer.len(out),
compressedSize = r.limit - r.base,
method = "deflate",
mtime = if h.mtime and h.mtime > 0 then h.mtime else nil,
mtime = if h.mtime and h.mtime > 0 then h.mtime :: number else nil,
dir = false,
data = out,
})
@@ -94,7 +94,7 @@ end
-- ------------------------------------------------------------------------------------------------ encode
function Gzip.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local o = opts or {}
local o: EncodeOptions = opts or {}
local level = if o.level == nil then 6 else o.level
if type(level) ~= "number" or level < 0 or level > 9 or level % 1 ~= 0 then
return nil, Err.new(Err.ARG, "level must be 0..9")
@@ -111,7 +111,7 @@ function Gzip.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
if not items then
return nil, Err.new(Err.ARG, "expected an Archive, a buffer or a string")
end
local files = {}
local files: typeof(items) = {}
for _, it in items do
if not it.entry.dir then
table.insert(files, it)
+1 -1
View File
@@ -70,7 +70,7 @@ end
-- ------------------------------------------------------------------------------------------------ decode
function Xz.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive?, Err.Err?)
local o = opts or {}
local o: DecodeOptions = opts or {}
local n = r.limit - r.base
local out, res
local method
+2 -2
View File
@@ -350,7 +350,7 @@ local function source(r: Bytes.Reader, p: Parsed): (Source?, Err.Err?)
return nil, Err.new(Err.CORRUPT, "compressed CAF without a packet table")
end
end
local offsets = table.create(#sizes, 0)
local offsets = table.create(#(sizes :: { number }), 0)
local o = p.dataStart
for i, s in sizes :: { number } do
offsets[i] = o
@@ -585,7 +585,7 @@ end
-- encode(audio | decoder, opts?) -> buffer | nil, Err
function Caf.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local o = opts or {}
local o: EncodeOptions = opts or {}
if type(value) ~= "table" or type(value.sampleRate) ~= "number" or type(value.channels) ~= "number" then
return nil, Err.new(Err.ARG, "expected an Audio or a Decoder")
end
+4 -4
View File
@@ -121,7 +121,7 @@ end
-- open(r, opts?) -> Types.Decoder | nil, Err
function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.Err?)
local o = opts or {}
local o: DecodeOptions = opts or {}
local movie, err = IsoBmff.parse(r, { maxSamples = o.maxSamples })
if not movie then
return nil, err
@@ -169,9 +169,9 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E
-- iTunes gapless info (fdkaac, iTunes) when there is no edit list: " 0 <priming> <padding> <samples> ..." (hex)
local smpb = tags.iTunSMPB
if gapless and not t.edits and smpb then
local f = {}
local f: { number } = {}
for w in string.gmatch(smpb, "%x+") do
table.insert(f, tonumber(w, 16))
table.insert(f, tonumber(w, 16) :: number)
end
if #f >= 4 and f[2] and f[4] and f[4] > 0 and f[2] + f[4] <= total then
skip = f[2]
@@ -337,7 +337,7 @@ end
-- ------------------------------------------------------------------------------------------------ encode
-- encode(audio | decoder, opts?) -> buffer | nil, Err: an ALAC M4A
function M4a.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local o = opts or {}
local o: EncodeOptions = opts or {}
if type(value) ~= "table" or type(value.sampleRate) ~= "number" or type(value.channels) ~= "number" then
return nil, Err.new(Err.ARG, "expected an Audio or a Decoder")
end
+14 -5
View File
@@ -561,7 +561,8 @@ local function intensityStereo(h1: number, h3: number, gr: number)
kr = 1
end
end
kl, kr = f32(kl * s), f32(kr * s)
kl = f32(kl * s)
kr = f32(kr * s)
for k = left * 4, (left + n - 1) * 4, 4 do
local v = rf(GRBUF, k)
wf(GRBUF, k + 2304, v * kr)
@@ -1183,8 +1184,14 @@ local function synth(xl: number, dstl: number, nch: number, lins: number)
a3 += z3 * w0 - y3 * w1
vz -= 256
vy += 256
z0, z1, z2, z3 = rf(SYN, vz), rf(SYN, vz + 4), rf(SYN, vz + 8), rf(SYN, vz + 12)
y0, y1, y2, y3 = rf(SYN, vy), rf(SYN, vy + 4), rf(SYN, vy + 8), rf(SYN, vy + 12)
z0 = rf(SYN, vz)
z1 = rf(SYN, vz + 4)
z2 = rf(SYN, vz + 8)
z3 = rf(SYN, vz + 12)
y0 = rf(SYN, vy)
y1 = rf(SYN, vy + 4)
y2 = rf(SYN, vy + 8)
y3 = rf(SYN, vy + 12)
b0 += z0 * w3 + y0 * w2
b1 += z1 * w3 + y1 * w2
b2 += z2 * w3 + y2 * w2
@@ -1212,8 +1219,10 @@ local function synth(xl: number, dstl: number, nch: number, lins: number)
a2 += z2 * w0 - y2 * w1
vz -= 256
vy += 256
z0, z2 = rf(SYN, vz), rf(SYN, vz + 8)
y0, y2 = rf(SYN, vy), rf(SYN, vy + 8)
z0 = rf(SYN, vz)
z2 = rf(SYN, vz + 8)
y0 = rf(SYN, vy)
y2 = rf(SYN, vy + 8)
b0 += z0 * w3 + y0 * w2
b2 += z2 * w3 + y2 * w2
a0 += y0 * w3 - z0 * w2
+3 -1
View File
@@ -375,7 +375,9 @@ function Encode2.encode(src: Source, opts: Options): (buffer?, string?)
local q = floor((x * inv + 1) * L / 2)
return if q < 0 then 0 elseif q > L - 1 then L - 1 else q
end
q0, q1, q2 = quant(sb[o + 1]), quant(sb[o + 2]), quant(sb[o + 3])
q0 = quant(sb[o + 1])
q1 = quant(sb[o + 2])
q2 = quant(sb[o + 3])
end
if ba >= 17 then
put(w, q0 + L * (q1 + L * q2), tripleBits(ba))
+5 -3
View File
@@ -179,7 +179,8 @@ local function parseV2(b: buffer, o: number, size: number, tags: any)
local p, e = o + 10, bodyEnd
if band(flags, 0x80) ~= 0 and ver < 4 then
body = deunsync(b, p, e)
p, e = 0, buffer.len(body)
p = 0
e = buffer.len(body)
end
if band(flags, 0x40) ~= 0 and ver >= 3 and p + 4 <= e then
-- extended header: v2.3 size excludes its own 4 bytes, v2.4 includes them (syncsafe)
@@ -216,7 +217,8 @@ local function parseV2(b: buffer, o: number, size: number, tags: any)
end
if not skip and band(fflags, 0x0002) ~= 0 and fs < fe then
fb = deunsync(body, fs, fe)
fs, fe = 0, buffer.len(fb)
fs = 0
fe = buffer.len(fb)
end
end
if not skip and fs < fe then
@@ -273,7 +275,7 @@ function Tags.parse(b: buffer): (any, number, number)
if size >= 128 and buffer.readstring(b, start + size - 128, 3) == "TAG" then
local o = start + size - 128
tags.id3v1 = true
local v1 = {
local v1: { [string]: any } = {
title = v1String(b, o + 3, 30),
artist = v1String(b, o + 33, 30),
album = v1String(b, o + 63, 30),
+5 -5
View File
@@ -60,7 +60,7 @@ local function bigText(s: string): string
end
-- text + 1 for a decimal string
local function incText(s: string): string
local digits = table.create(#s)
local digits: { number } = table.create(#s) :: { number }
for i = 1, #s do
digits[i] = string.byte(s, i) - 48
end
@@ -73,7 +73,7 @@ local function incText(s: string): string
digits[i] = 0
i -= 1
end
local out = table.create(#digits)
local out: { string } = table.create(#digits) :: { string }
for k, d in digits do
out[k] = string.char(48 + d)
end
@@ -296,11 +296,11 @@ function item(st: St, depth: number): (any, string?)
table.insert(t, v)
end
end
if n > r:remaining() then
if (n :: number) > r:remaining() then
return nil, "array count past the end"
end
local t = table.create(n)
for k = 1, n do
local t = table.create(n :: number)
for k = 1, n :: number do
local v, ve = item(st, depth + 1)
if ve then
return nil, ve
+1 -1
View File
@@ -57,7 +57,7 @@ function Csv.sniff(r: Bytes.Reader): number
end
for _, d in { ",", "\t", ";" } do
local _, c1 = gsub(l1, d, "")
local _, c2 = gsub(l2, d, "")
local _, c2 = gsub(l2 :: string, d, "")
if c1 > 0 and c1 == c2 then
return 0.15
end
+2 -2
View File
@@ -137,11 +137,11 @@ function Ini.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
seenKeys[cur] = seen
end
if seen[key :: string] and o.strict then
return nil, Err.new(Err.CORRUPT, "duplicate key " .. key, lineStart)
return nil, Err.new(Err.CORRUPT, "duplicate key " .. (key :: string), lineStart)
end
seen[key :: string] = true
if cur == root and type(root[key]) == "table" then
return nil, Err.new(Err.CORRUPT, "top-level key " .. key .. " has the name of a section", lineStart)
return nil, Err.new(Err.CORRUPT, "top-level key " .. (key :: string) .. " has the name of a section", lineStart)
end
Types.set(cur, key, value)
lastKey, lastIndent, lastTable = key, indent, cur
+5 -5
View File
@@ -214,7 +214,7 @@ function Decode.decode(s: string, opts: Options?): (any, any, number?)
-- a (dotted) key: { parts }, next | nil
local function key(i: number): ({ string }?, number)
local keys = {}
local keys: { string } = {}
while true do
i = ws(i)
local c = byte(s, i)
@@ -240,7 +240,7 @@ function Decode.decode(s: string, opts: Options?): (any, any, number?)
if k == nil then
return nil, 0
end
table.insert(keys, k)
table.insert(keys, k :: string)
i = ws(i)
if byte(s, i) == 46 then
i += 1
@@ -313,7 +313,7 @@ function Decode.decode(s: string, opts: Options?): (any, any, number?)
end
return if sign == "-" then -v else v, nexti
end
local text = sign .. digits
local text: string = (sign :: string) .. digits
local frac, r2 = match(rest :: string, "^%.([%d_]*)(.*)$")
if frac then
if not digitsOk(frac, "%d") then
@@ -324,10 +324,10 @@ function Decode.decode(s: string, opts: Options?): (any, any, number?)
end
if rest ~= "" then
local es, ed = match(rest :: string, "^[eE]([%+%-]?)([%d_]*)$")
if not es or not digitsOk(ed, "%d") then
if not es or not digitsOk(ed :: string, "%d") then
return fail("bad float", i), 0
end
text ..= "e" .. es .. gsub(ed, "_", "")
text ..= "e" .. es .. gsub(ed :: string, "_", "")
end
local v = tonumber(text)
if v == nil then
+6 -6
View File
@@ -107,13 +107,13 @@ function Parse.text(s: string): (Desc?, string?)
local a: { [string]: string } = {}
-- key=value, key="quoted value"
local i = 1
while i <= #rest do
local k, q, e = string.match(rest, '^%s*([%w_]+)="([^"]*)"()', i)
while i <= #(rest :: string) do
local k, q, e = string.match(rest :: string, '^%s*([%w_]+)="([^"]*)"()', i)
if k then
a[k] = q
i = e
else
local k2, v2, e2 = string.match(rest, "^%s*([%w_]+)=(%S*)()", i)
local k2, v2, e2 = string.match(rest :: string, "^%s*([%w_]+)=(%S*)()", i)
if not k2 then
break
end
@@ -135,7 +135,7 @@ function Parse.text(s: string): (Desc?, string?)
end
-- ------------------------------------------------------------------------------------------------ XML
local ENTITIES = { quot = '"', amp = "&", lt = "<", gt = ">", apos = "'" }
local ENTITIES: { [string]: string } = { quot = '"', amp = "&", lt = "<", gt = ">", apos = "'" }
local function unescape(s: string): string
return (
string.gsub(s, "&(#?x?%w+);", function(e: string): string?
@@ -159,8 +159,8 @@ function Parse.xml(s: string): (Desc?, string?)
seenFont = true
end
local a: { [string]: string } = {}
for k, q, v in string.gmatch(attrs, "([%w_]+)%s*=%s*([\"'])(.-)%2") do
a[k] = unescape(v)
for k, q, v in string.gmatch(attrs :: string, "([%w_]+)%s*=%s*([\"'])(.-)%2") do
a[k] = unescape(v :: string)
local _ = q
end
local err = apply(d, tag, a)
+8 -6
View File
@@ -287,26 +287,28 @@ function Bmfont.bake(font: any, opts: BakeOptions?): (any, Err.Err?)
if type(font) ~= "table" or type(font.rasterize) ~= "function" or type(font.glyphIndex) ~= "function" then
return nil, Err.new(Err.ARG, "bake: expected an outline Font")
end
local o = opts or {}
local o: BakeOptions = opts or {}
local px = if type(o.px) == "number" and o.px > 0 and o.px <= 1024 then o.px else 32
local pad = if type(o.padding) == "number" and o.padding >= 0 then math.floor(o.padding) else 1
local scale = px / font.unitsPerEm
local glyphs = {}
local glyphs: { { cp: number, g: number, img: Types.Image } } = {}
local area = 0
local seen = {}
for _, cp in codepoints(o.chars) do
local g = font:glyphIndex(cp)
local g = (font :: any):glyphIndex(cp)
if (g ~= 0 or cp == 0) and not seen[cp] then
seen[cp] = true
local img = font:rasterize(g, px)
local img: Types.Image? = (font :: any):rasterize(g, px)
if img then
table.insert(glyphs, { cp = cp, g = g, img = img })
area += (img.width + pad) * (img.height + pad)
end
end
end
local size = o.pageSize
if type(size) ~= "number" then
local size: number
if type(o.pageSize) == "number" then
size = o.pageSize
else
size = 64
while size * size < area * 1.3 and size < 4096 do
size *= 2
+12 -5
View File
@@ -150,9 +150,11 @@ local function glyf(b: buffer, p: number, n: number, maxBytes: number): (any?, s
fl = band(fl, 0x7F)
local dx, dy
if fl < 10 then
dx, dy = 0, withSign(fl, band(fl, 14) * 128 + su8(glyphS))
dx = 0
dy = withSign(fl, band(fl, 14) * 128 + su8(glyphS))
elseif fl < 20 then
dx, dy = withSign(fl, band(fl - 10, 14) * 128 + su8(glyphS)), 0
dx = withSign(fl, band(fl - 10, 14) * 128 + su8(glyphS))
dy = 0
elseif fl < 84 then
local b0 = fl - 20
local b1 = su8(glyphS)
@@ -181,12 +183,17 @@ local function glyf(b: buffer, p: number, n: number, maxBytes: number): (any?, s
end
local x0, y0, x1, y1
if hasBox(g) then
x0, y0, x1, y1 = si16(bboxS), si16(bboxS), si16(bboxS), si16(bboxS)
x0 = si16(bboxS)
y0 = si16(bboxS)
x1 = si16(bboxS)
y1 = si16(bboxS)
else
x0, y0, x1, y1 = 32767, 32767, -32768, -32768
for k = 1, total do
x0, y0 = math.min(x0, xs[k]), math.min(y0, ys[k])
x1, y1 = math.max(x1, xs[k]), math.max(y1, ys[k])
x0 = math.min(x0, xs[k])
y0 = math.min(y0, ys[k])
x1 = math.max(x1, xs[k])
y1 = math.max(y1, ys[k])
end
if total == 0 then
x0, y0, x1, y1 = 0, 0, 0, 0
+8 -3
View File
@@ -209,7 +209,9 @@ function Bmp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
if comp == 3 or comp == 6 then
hasMasks = true
if hs >= 52 then
rm, gm, bm = readu32(buf, 54), readu32(buf, 58), readu32(buf, 62)
rm = readu32(buf, 54)
gm = readu32(buf, 58)
bm = readu32(buf, 62)
if hs >= 56 then
am = readu32(buf, 66)
end
@@ -218,7 +220,9 @@ function Bmp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
if palStart + count * 4 > n then
return nil, Err.new(Err.TRUNCATED, "truncated bit masks")
end
rm, gm, bm = readu32(buf, palStart), readu32(buf, palStart + 4), readu32(buf, palStart + 8)
rm = readu32(buf, palStart)
gm = readu32(buf, palStart + 4)
bm = readu32(buf, palStart + 8)
if comp == 6 then
am = readu32(buf, palStart + 12)
end
@@ -385,7 +389,8 @@ function Bmp.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
local dpi = img.meta and img.meta.dpi
local xp: number, yp: number = 3780, 3780 -- 96 dpi, as Pillow
if type(dpi) == "table" and type(dpi.x) == "number" and type(dpi.y) == "number" and dpi.x > 0 and dpi.y > 0 then
xp, yp = math.floor((dpi.x :: number) * 39.3701 + 0.5), math.floor((dpi.y :: number) * 39.3701 + 0.5)
xp = math.floor((dpi.x :: number) * 39.3701 + 0.5)
yp = math.floor((dpi.y :: number) * 39.3701 + 0.5)
end
o:i32le(xp)
o:i32le(yp)
+3 -3
View File
@@ -50,7 +50,7 @@ export type EncodeOptions = {
-- ------------------------------------------------------------------------------------------------ constants
local readu8 = buffer.readu8
local DISPOSE = { [0] = "none", [1] = "none", [2] = "background", [3] = "previous" }
local DISPOSE_CODE = { none = 1, background = 2, previous = 3 }
local DISPOSE_CODE: { [string]: number } = { none = 1, background = 2, previous = 3 }
-- ------------------------------------------------------------------------------------------------ sniff
function Gif.sniff(r: Bytes.Reader): number
@@ -62,7 +62,7 @@ end
-- a colour table at p (n entries) -> 256 u32 rgba entries (missing ones opaque black) and the { r, g, b, a } list
local function colorTable(b: buffer, p: number, n: number): (buffer, { { number } })
local t = buffer.create(1024)
local list = table.create(n)
local list: { { number } } = table.create(n) :: { { number } }
for i = 0, 255 do
buffer.writeu32(t, i * 4, 0xFF000000)
end
@@ -404,7 +404,7 @@ function Gif.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
end
-- palettes
local shared = (opts and opts.globalPalette) or not animated
local qopts = { method = method, dither = dither }
local qopts: Quantize.PaletteOptions = { method = method, dither = dither }
local globalPal: { { number } }? = nil
local globalTrans = false
if shared then
+2 -2
View File
@@ -106,7 +106,7 @@ function Hdr.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
return nil, Err.new(Err.TRUNCATED, "no resolution line", p)
end
local s1, a1, n1, s2, a2, n2 = string.match(res, "^%s*([%+%-])([XY])%s+(%d+)%s+([%+%-])([XY])%s+(%d+)%s*$")
if not s1 or a1 == a2 or #n1 > 9 or #n2 > 9 then
if not s1 or a1 == a2 or #(n1 :: string) > 9 or #(n2 :: string) > 9 then
return nil, Err.new(Err.CORRUPT, "bad resolution line", p)
end
local outer, inner = tonumber(n1) :: number, tonumber(n2) :: number
@@ -170,7 +170,7 @@ function Hdr.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
if truncated then
img.meta.truncated = true
end
img.meta.source = { format = "hdr", bitDepth = 32, colorType = "rgbe", program = string.sub(first, 3), orientation = string.format("%s%s %s%s", s1, a1, s2, a2) }
img.meta.source = { format = "hdr", bitDepth = 32, colorType = "rgbe", program = string.sub(first, 3), orientation = string.format("%s%s %s%s", s1, a1 :: string, s2 :: string, a2 :: string) }
return img, nil
end
+1 -1
View File
@@ -175,7 +175,7 @@ function Ico.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
if typ == 2 then
im.meta.hotspot = { x = f1, y = f2 }
end
table.insert(frames, { image = im, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" })
table.insert(frames, { image = im, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" } :: Types.Frame)
local score = im.width * im.height * 64 + depth
if score > bestScore then
best, bestScore = im, score
+11 -3
View File
@@ -26,19 +26,27 @@ function Codec.decode(b: buffer, p: number, limit: number, out: buffer, n: numbe
if p + 3 > limit then
return false, "truncated"
end
r, g, bl = readu8(b, p), readu8(b, p + 1), readu8(b, p + 2)
r = readu8(b, p)
g = readu8(b, p + 1)
bl = readu8(b, p + 2)
p += 3
elseif op == 0xFF then
if p + 4 > limit then
return false, "truncated"
end
r, g, bl, a = readu8(b, p), readu8(b, p + 1), readu8(b, p + 2), readu8(b, p + 3)
r = readu8(b, p)
g = readu8(b, p + 1)
bl = readu8(b, p + 2)
a = readu8(b, p + 3)
p += 4
else
local tag = op // 64
if tag == 0 then
local q = op * 4
r, g, bl, a = readu8(index, q), readu8(index, q + 1), readu8(index, q + 2), readu8(index, q + 3)
r = readu8(index, q)
g = readu8(index, q + 1)
bl = readu8(index, q + 2)
a = readu8(index, q + 3)
elseif tag == 1 then
r = (r + (op // 16) % 4 - 2) % 256
g = (g + (op // 4) % 4 - 2) % 256
+1 -1
View File
@@ -109,7 +109,7 @@ function Qoi.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
buffer.writestring(out, 0, "qoif")
buffer.writeu32(out, 4, bit32.byteswap(w))
buffer.writeu32(out, 8, bit32.byteswap(h))
buffer.writeu8(out, 12, channels)
buffer.writeu8(out, 12, channels :: number)
buffer.writeu8(out, 13, if linear then 1 else 0)
local o = Codec.encode(img.pixels, n, bpp, out, 14)
buffer.writestring(out, o, "\0\0\0\0\0\0\0\1")
+6 -2
View File
@@ -384,7 +384,9 @@ function Pixels.convert(P: Params)
r, g, bl = 0, 0, 0
end
elseif photo == 2 then
r, g, bl = norm(0, x, y), norm(1, x, y), norm(2, x, y)
r = norm(0, x, y)
g = norm(1, x, y)
bl = norm(2, x, y)
elseif photo == 5 then
local c, m, ye, k = norm(0, x, y), norm(1, x, y), norm(2, x, y), norm(3, x, y)
r, g, bl = (1 - c) * (1 - k), (1 - m) * (1 - k), (1 - ye) * (1 - k)
@@ -399,7 +401,9 @@ function Pixels.convert(P: Params)
a = norm(alpha, x, y)
if assoc then
if a > 0 then
r, g, bl = math.min(r / a, 1), math.min(g / a, 1), math.min(bl / a, 1)
r = math.min(r / a, 1)
g = math.min(g / a, 1)
bl = math.min(bl / a, 1)
else
r, g, bl = 0, 0, 0
end
+4 -4
View File
@@ -322,7 +322,7 @@ local function decodePage(c: Ctx, t: { [number]: Entry }, opts: DecodeOptions?):
if not offs or #offs < perPlane * nplanes then
return nil, Err.new(Err.CORRUPT, "missing or short strip / tile offsets")
end
local planes = table.create(nplanes)
local planes: { buffer } = table.create(nplanes) :: { buffer }
for i = 1, nplanes do
planes[i] = buffer.create(rb * h + 8)
end
@@ -583,7 +583,7 @@ function Tiff.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.
end
local img, perr = decodePage(c, t, opts)
if img then
img.meta.source.pages = #list
(img.meta.source :: { [string]: any }).pages = #list
end
return img, perr
end
@@ -608,7 +608,7 @@ function Tiff.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.
table.insert(frames, { image = img, delay = 0, x = 0, y = 0, dispose = "none", blend = "source" })
end
local img = first :: Types.Image
img.meta.source.pages = #frames
(img.meta.source :: { [string]: any }).pages = #frames
if #frames > 1 then
img.frames = frames :: any
end
@@ -722,7 +722,7 @@ local function encodePage(img: Types.Image, o: EncodeOptions, comp: number): ({
end
local text = img.meta and img.meta.text
if type(text) == "table" then
for key, tag in { description = 270, software = 305, datetime = 306, artist = 315, copyright = 33432 } do
for key, tag in { description = 270, software = 305, datetime = 306, artist = 315, copyright = 33432 } :: { [string]: number } do
if type(text[key]) == "string" then
table.insert(tags, { tag = tag, typ = 2, data = buffer.fromstring(text[key] .. "\0") })
end
+7 -3
View File
@@ -57,7 +57,7 @@ function Channels.rleCounts(src: buffer, p: number, limit: number, rows: number,
if rows < 0 or p + rows * size > limit then
return nil, p
end
local counts = table.create(rows)
local counts: { number } = table.create(rows) :: { number }
for i = 1, rows do
if psb then
counts[i] = byteswap(readu32(src, p))
@@ -247,11 +247,15 @@ function Channels.toRGBA(planes: { [any]: buffer }, w: number, h: number, depth:
local mm = if c2 then sample8(c2, i, depth) else 255
local yy = if c3 then sample8(c3, i, depth) else 255
local kk = if c4 then sample8(c4, i, depth) else 255
r, g, b = floor(cc * kk / 255 + 0.5), floor(mm * kk / 255 + 0.5), floor(yy * kk / 255 + 0.5)
r = floor(cc * kk / 255 + 0.5)
g = floor(mm * kk / 255 + 0.5)
b = floor(yy * kk / 255 + 0.5)
elseif mode == 2 then
local k = if c1 then readu8(c1, i) else 0
if palette and blen(palette) >= 768 then
r, g, b = readu8(palette, k), readu8(palette, 256 + k), readu8(palette, 512 + k)
r = readu8(palette, k)
g = readu8(palette, 256 + k)
b = readu8(palette, 512 + k)
else
r, g, b = k, k, k
end
+2 -2
View File
@@ -153,7 +153,7 @@ function Render.timeline(midi: Types.Midi): Timeline
end
return a[3] < b[3]
end)
local tl: Timeline = { time = table.create(#items), status = table.create(#items), d1 = table.create(#items), d2 = table.create(#items), length = Render.length(midi) }
local tl: Timeline = { time = table.create(#items) :: { number }, status = table.create(#items) :: { number }, d1 = table.create(#items) :: { number }, d2 = table.create(#items) :: { number }, length = Render.length(midi) }
for i, it in items do
tl.time[i], tl.status[i], tl.d1[i], tl.d2[i] = it[1], it[4], it[5] % 128, it[6] % 128
end
@@ -192,7 +192,7 @@ function Render.decoder(midi: Types.Midi, opts: RenderOptions?): (Types.Decoder?
local tail = o.tail or 1
local frames = math.max(0, math.ceil((tl.length + tail) * rate))
local nev = #tl.time
local evFrame = table.create(nev)
local evFrame: { number } = table.create(nev) :: { number }
for i = 1, nev do
evFrame[i] = math.floor(tl.time[i] * rate + 0.5)
end
+2 -2
View File
@@ -25,7 +25,7 @@ export type WriteOptions = {
}
local function bytesOf(b: buffer, p: number, n: number): { number }
local t: { number } = table.create(n)
local t: { number } = table.create(n) :: { number }
for i = 0, n - 1 do
t[i + 1] = readu8(b, p + i)
end
@@ -309,7 +309,7 @@ function Smf.write(midi: Types.Midi, opts: WriteOptions?): (buffer?, Err.Err?)
end
last = ev.tick
end
local list = events
local list: { any } = events
if not sorted then
list = table.clone(events)
local order: { [any]: number } = {}
+2 -1
View File
@@ -204,7 +204,8 @@ function S3m.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
vol = math.min(pr:u8(), 64) + 1
end
if bit32.band(what, 0x80) ~= 0 then
cmd, info = pr:u8(), pr:u8()
cmd = pr:u8()
info = pr:u8()
if cmd > 26 then
cmd, info = 0, 0
end
+5 -1
View File
@@ -149,7 +149,11 @@ function Xm.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
param = pr:u8()
end
else
note, ins, vol, fx, param = b, pr:u8(), pr:u8(), pr:u8(), pr:u8()
note = b
ins = pr:u8()
vol = pr:u8()
fx = pr:u8()
param = pr:u8()
end
if pr.err then
break -- a short pattern: the rest stays empty
+1 -1
View File
@@ -136,7 +136,7 @@ end
-- ------------------------------------------------------------------------------------------------ decode
local function parsePls(ls: { string }, base: string?, baseScheme: string?, warnings: { string }): Result
local res: Result = { kind = "playlist", format = "pls", entries = {}, warnings = warnings, meta = {} }
local files, titles, lengths = {}, {}, {}
local files: { [number]: string }, titles: { [number]: string }, lengths: { [number]: string } = {}, {}, {}
local declared = nil
for _, line in ls do
local l = Text.trim(line)
+1 -1
View File
@@ -121,7 +121,7 @@ function Xspf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, E
for _, list in elements(root, "trackList") do
for i, track in elements(list, "track") do
local tbase, tscheme = withXmlBase(track, pbase, pscheme)
local locs = {}
local locs: { string } = {}
for _, l in elements(track, "location") do
local s = trim(Xml.text(l))
if s ~= "" then
+1 -1
View File
@@ -189,7 +189,7 @@ function Ass.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Subtitles?, E
stylesName = name
section = "styles"
elseif section ~= "script info" and section ~= "events" then
table.insert(sections, { name = name, lines = {} })
table.insert(sections, { name = name, lines = {} :: { string } })
end
continue
end
+9 -9
View File
@@ -48,7 +48,7 @@ local LOCAL: { [string]: boolean } = {
filter = true,
}
local UNSUPPORTED = { text = true, image = true, pattern = true, mask = true, marker = true, filter = true, foreignObject = true, textPath = true, tspan = true }
local UNSUPPORTED: { [string]: boolean } = { text = true, image = true, pattern = true, mask = true, marker = true, filter = true, foreignObject = true, textPath = true, tspan = true }
local CONTAINERS = { g = true, a = true, switch = true }
local SHAPES = { path = true, rect = true, circle = true, ellipse = true, line = true, polyline = true, polygon = true }
@@ -103,7 +103,7 @@ local function specified(n: Node, ctx: Ctx): { [string]: string }
for c in string.gmatch(attr(n, "class") or "", "%S+") do
classes[c] = true
end
local hits = {}
local hits: { Style.Rule } = {}
for _, r in ctx.rules do
if Style.matches(r.selector, local_(n), attr(n, "id"), classes) then
table.insert(hits, r)
@@ -296,7 +296,7 @@ local function shape(n: Node, st: { [string]: string }, ctx: Ctx): { PathData.Cm
if w <= 0 or h <= 0 then
return nil
end
local rx, ry = len(attr(n, "rx"), vw, st), len(attr(n, "ry"), vh, st)
local rx: number?, ry: number? = len(attr(n, "rx"), vw, st), len(attr(n, "ry"), vh, st)
if rx and rx < 0 then
rx = nil
end
@@ -305,9 +305,9 @@ local function shape(n: Node, st: { [string]: string }, ctx: Ctx): { PathData.Cm
end
rx = rx or ry or 0
ry = ry or rx
rx, ry = math.min(rx :: number, w / 2), math.min(ry :: number, h / 2)
local rx: number, ry: number = math.min(rx :: number, w / 2), math.min(ry :: number, h / 2)
if rx <= 0 or ry <= 0 then
return { { op = "M", x, y }, { op = "L", x + w, y }, { op = "L", x + w, y + h }, { op = "L", x, y + h }, { op = "Z" } }
return { { op = "M", x, y }, { op = "L", x + w, y }, { op = "L", x + w, y + h }, { op = "L", x, y + h }, { op = "Z" } } :: { PathData.Cmd }
end
local out: { PathData.Cmd } = { { op = "M", x + rx, y }, { op = "L", x + w - rx, y } }
PathData.arc(x + w - rx, y, rx, ry, 0, false, true, x + w, y + ry, out)
@@ -317,7 +317,7 @@ local function shape(n: Node, st: { [string]: string }, ctx: Ctx): { PathData.Cm
PathData.arc(x + rx, y + h, rx, ry, 0, false, true, x, y + h - ry, out)
table.insert(out, { op = "L", x, y + ry })
PathData.arc(x, y + ry, rx, ry, 0, false, true, x + rx, y, out)
table.insert(out, { op = "Z" })
table.insert(out, { op = "Z" } :: PathData.Cmd)
return out
elseif kind == "circle" or kind == "ellipse" then
local cx, cy = len(attr(n, "cx"), vw, st, 0) :: number, len(attr(n, "cy"), vh, st, 0) :: number
@@ -338,12 +338,12 @@ local function shape(n: Node, st: { [string]: string }, ctx: Ctx): { PathData.Cm
PathData.arc(cx, cy + ry, rx, ry, 0, false, true, cx - rx, cy, out)
PathData.arc(cx - rx, cy, rx, ry, 0, false, true, cx, cy - ry, out)
PathData.arc(cx, cy - ry, rx, ry, 0, false, true, cx + rx, cy, out)
table.insert(out, { op = "Z" })
table.insert(out, { op = "Z" } :: PathData.Cmd)
return out
elseif kind == "line" then
local x1, y1 = len(attr(n, "x1"), vw, st, 0) :: number, len(attr(n, "y1"), vh, st, 0) :: number
local x2, y2 = len(attr(n, "x2"), vw, st, 0) :: number, len(attr(n, "y2"), vh, st, 0) :: number
return { { op = "M", x1, y1 }, { op = "L", x2, y2 } }
return { { op = "M", x1, y1 }, { op = "L", x2, y2 } } :: { PathData.Cmd }
elseif kind == "polyline" or kind == "polygon" then
local v = Style.numbers(attr(n, "points"))
if #v < 4 then
@@ -354,7 +354,7 @@ local function shape(n: Node, st: { [string]: string }, ctx: Ctx): { PathData.Cm
table.insert(out, { op = "L", v[i], v[i + 1] })
end
if kind == "polygon" then
table.insert(out, { op = "Z" })
table.insert(out, { op = "Z" } :: PathData.Cmd)
end
return out
end
+2 -2
View File
@@ -91,7 +91,7 @@ function PathData.parse(d: string): ({ Cmd }, boolean)
local last = "" -- previous command letter (upper case)
local cmd: string? = nil
local rel = false
local nums: { number } = table.create(7)
local nums: { number } = table.create(7) :: { number }
local function ws()
while i <= n do
local c = byte(d, i)
@@ -175,7 +175,7 @@ function PathData.parse(d: string): ({ Cmd }, boolean)
rel = c ~= up
i += 1
if up == "Z" then
table.insert(out, { op = "Z" })
table.insert(out, { op = "Z" } :: Cmd)
cx, cy = sx, sy
last = "Z"
cmd = nil
+3 -3
View File
@@ -194,7 +194,7 @@ local function scan(s: string, i: number): (number?, number)
end
-- length(s, ref?, fontSize?) -> number in user units | nil. ref: what 100% is (default 100)
local UNITS = { px = 1, pt = 4 / 3, pc = 16, mm = 96 / 25.4, cm = 96 / 2.54, ["in"] = 96, q = 96 / 101.6 }
local UNITS: { [string]: number } = { px = 1, pt = 4 / 3, pc = 16, mm = 96 / 25.4, cm = 96 / 2.54, ["in"] = 96, q = 96 / 101.6 }
function Style.length(s: string?, ref: number?, fontSize: number?): number?
if s == nil then
return nil
@@ -355,11 +355,11 @@ function Style.css(text: string, rules: { Rule }?): { Rule }
if not s then
break
end
text = string.sub(text, 1, s - 1) .. string.sub(text, e + 1)
text = string.sub(text, 1, s - 1) .. string.sub(text, (e :: number) + 1)
end
text = string.gsub(text, "@[%w-]+[^;{]*;", "")
for sels, body in string.gmatch(text, "([^{}]+)(%b{})") do
local decls = Style.declarations(string.sub(body, 2, -2))
local decls = Style.declarations(string.sub(body :: string, 2, -2))
for one in string.gmatch(sels, "[^,]+") do
local sel = selector(one)
if sel then
+1 -1
View File
@@ -163,7 +163,7 @@ function Write.vector(v: Types.Vector): (string?, string?)
for k, d in p.strokeDashArray do
parts[k] = num(tonumber(d) or 0)
end
table.insert(attrs, { "stroke-dasharray", table.concat(parts, " ") })
table.insert(attrs, { "stroke-dasharray", table.concat(parts :: { string }, " ") })
local off = tonumber(p.strokeDashOffset)
table.insert(attrs, { "stroke-dashoffset", if off and off ~= 0 then off else nil })
end
+2 -2
View File
@@ -42,11 +42,11 @@ function Data.base64(text: string, compression: string?): ({ number }?, string?,
return nil, "compression " .. compression .. " is not supported", true
end
local n = buffer.len(bytes) // 4
local out = table.create(n)
local out: { number } = table.create(n) :: { number }
for i = 0, n - 1 do
out[i + 1] = buffer.readu32(bytes, i * 4)
end
return out, nil
return out, nil, nil
end
-- toBase64(gids, compression?, level?) -> text | nil, message
+1 -1
View File
@@ -144,7 +144,7 @@ function Tiled.encode(map: Types.TileMap, opts: EncodeOptions?): (buffer?, Err.E
if o.compression and o.compression ~= "zlib" and o.compression ~= "gzip" then
return nil, Err.new(Err.ARG, "compression must be zlib or gzip")
end
local wo = { encoding = o.encoding, compression = o.compression, level = o.level }
local wo: Tmx.WriteOptions = { encoding = o.encoding :: string?, compression = o.compression, level = o.level }
if fmt == "tmx" or fmt == "tsx" or fmt == "xml" then
local doc: any, err: string?
if tilesetOnly then
+2 -2
View File
@@ -279,7 +279,7 @@ function AviFormat.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
for _, item in Source.interleave(streams) do
table.insert(chunks, { item[1], item[2].data, true })
end
local ok, buf = pcall(Avi.write, { streams = wstreams, microSecPerFrame = usPerFrame, width = width, height = height }, chunks)
local ok, buf = pcall(Avi.write, { streams = wstreams, microSecPerFrame = usPerFrame, width = width, height = height } :: Avi.WOptions, chunks)
if not ok then
return nil, Err.new(Err.ARG, "internal error: " .. tostring(buf))
end
@@ -293,7 +293,7 @@ function AviFormat.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecode
if not dmx then
return nil, err
end
return Video.open(dmx, opts)
return Video.open(dmx, opts :: Video.OpenOptions?)
end
return AviFormat
+2 -2
View File
@@ -127,7 +127,7 @@ function Mkv.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Er
if not dmx then
return nil, err
end
return Video.open(dmx, opts)
return Video.open(dmx, opts :: Video.OpenOptions?)
end
-- ------------------------------------------------------------------------------------------------ extras
@@ -151,7 +151,7 @@ function Mkv.subtitles(dmx: any, trackId: number?): Types.Subtitles?
if p then
local start = p.pts / track.timescale
local stop = start + (p.duration or 0) / track.timescale
table.insert(subs.cues, { start = start, stop = stop, text = buffer.tostring(p.data) })
table.insert(subs.cues, { start = start, stop = stop, text = buffer.tostring(p.data) } :: Types.Cue)
end
end
return subs
+1 -1
View File
@@ -318,7 +318,7 @@ function Mp4.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Er
if not dmx then
return nil, err
end
return Video.open(dmx, opts)
return Video.open(dmx, opts :: Video.OpenOptions?)
end
return Mp4
+2 -2
View File
@@ -145,7 +145,7 @@ function Ps.demux(r: Bytes.Reader, _opts: OpenOptions?): (any, Err.Err?)
codecTag = string.format("0x%02x", id % 256),
timescale = 90000,
streamId = id,
})
} :: Types.Track)
end
local dmx = Demux.new("mpegps", tracks, r.buf, { mpeg2 = file.mpeg2, errors = file.errors })
for i, id in file.order do
@@ -186,7 +186,7 @@ function Ps.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Err
if not dmx then
return nil, err
end
return Video.open(dmx, opts)
return Video.open(dmx, opts :: Video.OpenOptions?)
end
return Ps
+2 -2
View File
@@ -154,7 +154,7 @@ function Ts.demux(r: Bytes.Reader, _opts: OpenOptions?): (any, Err.Err?)
streamType = st,
}
byPid[pid] = t
table.insert(tracks, t)
table.insert(tracks, t :: Types.Track)
return t
end
for _, s in d.streams do
@@ -239,7 +239,7 @@ function Ts.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Err
if not dmx then
return nil, err
end
return Video.open(dmx, opts)
return Video.open(dmx, opts :: Video.OpenOptions?)
end
return Ts
+2 -2
View File
@@ -105,7 +105,7 @@ function Y4m.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?)
h = tonumber(v) or 0
elseif k == "F" then
local a, b = string.match(v, "^(%d+):(%d+)$")
if a and b and tonumber(a) > 0 and tonumber(b) > 0 then
if a and b and (tonumber(a) :: number) > 0 and (tonumber(b) :: number) > 0 then
fn, fd = tonumber(a) :: number, tonumber(b) :: number
end
elseif k == "C" then
@@ -203,7 +203,7 @@ function Y4m.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Er
if not dmx then
return nil, err
end
return Video.open(dmx, opts)
return Video.open(dmx, opts :: Video.OpenOptions?)
end
return Y4m