From c436f4d265067cd353603944fc398857703ec3e1 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 10:52:04 -0500 Subject: [PATCH] ws-cleanup-types: codecs: type table.create results, string-keyed tables and multi-assigned locals Co-Authored-By: Claude Opus 5.5 --- src/Formats/codecs/Aac/Decode.luau | 4 ++-- src/Formats/codecs/Alac/Decode.luau | 4 ++-- src/Formats/codecs/Blend.luau | 6 +++--- src/Formats/codecs/Cff/init.luau | 2 +- src/Formats/codecs/Dct/init.luau | 4 ++-- src/Formats/codecs/Deflate/Inflate.luau | 8 ++++---- src/Formats/codecs/Jpeg/Decode.luau | 12 ++++++------ src/Formats/codecs/Jpeg/Encode.luau | 4 ++-- src/Formats/codecs/MeshFlatten.luau | 8 ++++---- src/Formats/codecs/Quantize/init.luau | 12 ++++++------ src/Formats/codecs/Raster.luau | 6 +++--- src/Formats/codecs/SoundFont/Parse.luau | 6 +++--- src/Formats/codecs/TrueType/Glyf.luau | 18 +++++++++--------- src/Formats/codecs/TrueType/init.luau | 2 +- src/Formats/codecs/Video/init.luau | 18 +++++++++--------- src/Formats/codecs/Vp8L/Decode.luau | 4 ++-- src/Formats/codecs/Xml/init.luau | 2 +- src/Formats/codecs/Yuv.luau | 2 +- src/Formats/codecs/Zstd/Xxh64.luau | 8 ++++---- src/Formats/containers/IsoBmff/Demux.luau | 4 ++-- src/Formats/containers/Zip/init.luau | 2 +- src/Formats/ops/ImageOps/Kernels.luau | 10 +++++----- 22 files changed, 73 insertions(+), 73 deletions(-) diff --git a/src/Formats/codecs/Aac/Decode.luau b/src/Formats/codecs/Aac/Decode.luau index ce4a88b..48ec969 100644 --- a/src/Formats/codecs/Aac/Decode.luau +++ b/src/Formats/codecs/Aac/Decode.luau @@ -132,7 +132,7 @@ export type Channel = { bandType: { number }, -- [g * maxSfb + sfb + 1] sf: { number }, -- tns - tnsFilters: { { number } }, -- per window: { length, order, direction, coefs ... } flattened per filter + tnsFilters: { { { number } } }, -- per window: { length, order, direction, coefs ... } flattened per filter } export type State = { @@ -447,7 +447,7 @@ local function spectralData(st: State, c: Channel, pulses: { number }?): string? return "bad spectral code" end -- unpack the symbol into dim values (first value most significant) - local vals1, vals2, vals3, vals4 + local vals1: number, vals2: number, vals3: number, vals4: number if dim == 4 then vals4 = sym % mod - off sym = (sym - (sym % mod)) / mod diff --git a/src/Formats/codecs/Alac/Decode.luau b/src/Formats/codecs/Alac/Decode.luau index 932d7e7..4b0a71c 100644 --- a/src/Formats/codecs/Alac/Decode.luau +++ b/src/Formats/codecs/Alac/Decode.luau @@ -267,8 +267,8 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number local x = bitsAt(b, pos, 8) local y = bitsAt(b, pos + 8, 8) pos += 16 - mode[c], denShift[c] = rshift(x, 4), band(x, 15) - pbFactor[c], order[c] = rshift(y, 5), band(y, 31) + mode[c], denShift[c] = rshift(x, 4), band(x, 15) :: number + pbFactor[c], order[c] = rshift(y, 5), band(y, 31) :: number if pos + order[c] * 16 > limit then return nil, "frame runs past the end" end diff --git a/src/Formats/codecs/Blend.luau b/src/Formats/codecs/Blend.luau index a3db2a9..d8b2f6a 100644 --- a/src/Formats/codecs/Blend.luau +++ b/src/Formats/codecs/Blend.luau @@ -182,11 +182,11 @@ local function composite(dst: buffer, dw: number, dh: number, src: buffer, sw: n local mbuf: buffer? = nil local mx0, my0, mw, mh, mdef = 0, 0, 0, 0, 1 if type(mask) == "table" and type(mask.pixels) == "buffer" and type(mask.width) == "number" and type(mask.height) == "number" then - mw, mh = floor(mask.width), floor(mask.height) + mw, mh = floor(mask.width), floor(mask.height) :: number if mw > 0 and mh > 0 and blen(mask.pixels) >= mw * mh then mbuf = mask.pixels end - mx0, my0 = floor(tonumber(mask.x) or 0), floor(tonumber(mask.y) or 0) + mx0, my0 = floor(tonumber(mask.x) or 0), floor(tonumber(mask.y) or 0) :: number mdef = (tonumber(mask.default) or 255) / 255 elseif type(mask) == "table" then mdef = (tonumber(mask.default) or 255) / 255 @@ -224,7 +224,7 @@ local function composite(dst: buffer, dw: number, dh: number, src: buffer, sw: n local br, bg, bb = readu8(dst, do_) / 255, readu8(dst, do_ + 1) / 255, readu8(dst, do_ + 2) / 255 local mr, mg, mb = sr, sg, sb if m >= 2 and m ~= 6 and m ~= 11 and m < 23 then - mr, mg, mb = sep(m, br, sr), sep(m, bg, sg), sep(m, bb, sb) + mr, mg, mb = sep(m, br, sr), sep(m, bg, sg), sep(m, bb, sb) :: number elseif m == 6 then if lum(sr, sg, sb) >= lum(br, bg, bb) then mr, mg, mb = br, bg, bb diff --git a/src/Formats/codecs/Cff/init.luau b/src/Formats/codecs/Cff/init.luau index bdec1dc..48d855a 100644 --- a/src/Formats/codecs/Cff/init.luau +++ b/src/Formats/codecs/Cff/init.luau @@ -297,7 +297,7 @@ function Cff.outline(f: Font, g: number): ({ any }?, string?) local cmds: { any } = {} local s: { number } = table.create(48, 0) -- operand stack, 1-based; sp = count local sp = 0 - local trans: { number } = table.create(32, 0) + local trans = table.create(32, 0) :: { number } local x, y = 0, 0 local open = false local maskbits = 0 diff --git a/src/Formats/codecs/Dct/init.luau b/src/Formats/codecs/Dct/init.luau index c46923c..cb643bb 100644 --- a/src/Formats/codecs/Dct/init.luau +++ b/src/Formats/codecs/Dct/init.luau @@ -296,8 +296,8 @@ function Dct.fdctQuantize(src: buffer, stride: number, x: number, y: number, w: local d0: number, d1: number, d2: number, d3: number, d4: number, d5: number, d6: number, d7: number if x + 7 <= lastX then local p = row + x - d0, d1, d2, d3 = readu8(src, p), readu8(src, p + 1), readu8(src, p + 2), readu8(src, p + 3) - d4, d5, d6, d7 = readu8(src, p + 4), readu8(src, p + 5), readu8(src, p + 6), readu8(src, p + 7) + d0, d1, d2, d3 = readu8(src, p), readu8(src, p + 1), readu8(src, p + 2), readu8(src, p + 3) :: number + d4, d5, d6, d7 = readu8(src, p + 4), readu8(src, p + 5), readu8(src, p + 6), readu8(src, p + 7) :: number else d0 = readu8(src, row + math.min(x, lastX)) d1 = readu8(src, row + math.min(x + 1, lastX)) diff --git a/src/Formats/codecs/Deflate/Inflate.luau b/src/Formats/codecs/Deflate/Inflate.luau index 87f3e5f..ab9a237 100644 --- a/src/Formats/codecs/Deflate/Inflate.luau +++ b/src/Formats/codecs/Deflate/Inflate.luau @@ -38,7 +38,7 @@ local CLCAP = Huffman.capacity(19, CLBITS, 7) -- fixed Huffman tables local FLIT, FDIST = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) -local FLITS, FDISTS +local FLITS: number, FDISTS: number do local l = bcreate(288) bfill(l, 0, 8, 144) @@ -69,7 +69,7 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex local op = 0 local pos = start local bitbuf, bitcnt, over = 0, 0, 0 - local lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) + local lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) :: buffer local clt = bcreate(CLCAP * 4) local lens = bcreate(320) local final = 0 @@ -132,13 +132,13 @@ function Inflate.raw(src: buffer, start: number, endpos: number, cap: number, ex pos += n continue end - local lsub, dsub + local lsub: number, dsub: number if btype == 1 then lt, dt = FLIT, FDIST lsub, dsub = FLITS, FDISTS elseif btype == 2 then if lt == FLIT then - lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) + lt, dt = bcreate(LITCAP * 4), bcreate(DISTCAP * 4) :: buffer end local hlit = band(bitbuf, 31) + 257 local hdist = band(rshift(bitbuf, 5), 31) + 1 diff --git a/src/Formats/codecs/Jpeg/Decode.luau b/src/Formats/codecs/Jpeg/Decode.luau index 6cae5ff..833e66f 100644 --- a/src/Formats/codecs/Jpeg/Decode.luau +++ b/src/Formats/codecs/Jpeg/Decode.luau @@ -146,12 +146,12 @@ local function parseSof(b: buffer, p: number, e: number, m: number): (Frame?, st if e - p < 6 + nf * 3 then return nil, "corrupt", "short SOF" end - local comps = {} + local comps: { Comp } = {} local hmax, vmax = 1, 1 for i = 0, nf - 1 do local q = p + 6 + i * 3 local hv = readu8(b, q + 1) - local h, v, tq = rshift(hv, 4), band(hv, 15), readu8(b, q + 2) + local h, v, tq = rshift(hv, 4), band(hv, 15), readu8(b, q + 2) :: number if h < 1 or h > 4 or v < 1 or v > 4 then return nil, "corrupt", "bad sampling factors " .. h .. "x" .. v end @@ -164,7 +164,7 @@ local function parseSof(b: buffer, p: number, e: number, m: number): (Frame?, st return nil, "corrupt", "duplicate component id " .. id end end - hmax, vmax = math.max(hmax, h), math.max(vmax, v) + hmax, vmax = math.max(hmax, h), math.max(vmax, v) :: number table.insert(comps, { id = id, h = h, v = v, tq = tq, bpl = 0, bpc = 0, bw = 0, bh = 0, dcTbl = 0, acTbl = 0, pred = 0, ctx = 0 } :: Comp) end local mcusX = math.ceil(width / (8 * hmax)) @@ -186,7 +186,7 @@ local function parseSof(b: buffer, p: number, e: number, m: number): (Frame?, st vmax = vmax, mcusX = mcusX, mcusY = mcusY, - } + }, nil, nil end local function colorSpaceOf(frame: Frame, adobe: number?, jfif: boolean): string @@ -243,7 +243,7 @@ function Decode.parse(b: buffer, base: number, n: number, opts: any, headerOnly: if scale ~= 1 and scale ~= 2 and scale ~= 4 and scale ~= 8 then return nil, "arg", "scale must be 1, 2, 4 or 8" end - local maxPixels = (opts and opts.maxPixels) or 2 ^ 26 + local maxPixels: number = (opts and opts.maxPixels) or 2 ^ 26 local quant: { [number]: buffer } = {} local dcT: { [number]: buffer } = {} local acT: { [number]: buffer } = {} @@ -445,7 +445,7 @@ function Decode.parse(b: buffer, base: number, n: number, opts: any, headerOnly: if ns < 1 or ns > 4 or e - p < 4 + ns * 2 then return nil, "corrupt", "bad SOS segment" end - local sc = {} + local sc: { Comp } = {} local blocks = 0 for i = 0, ns - 1 do local id, t = readu8(b, p + 1 + i * 2), readu8(b, p + 2 + i * 2) diff --git a/src/Formats/codecs/Jpeg/Encode.luau b/src/Formats/codecs/Jpeg/Encode.luau index 642b737..49e003e 100644 --- a/src/Formats/codecs/Jpeg/Encode.luau +++ b/src/Formats/codecs/Jpeg/Encode.luau @@ -301,7 +301,7 @@ end -- progressive state (jcphuff.c) local EOBRUN = 0 local BE = 0 -local CORR: { number } = table.create(1000, 0) +local CORR = table.create(1000, 0) :: { number } local curAc: Derived = { code = buffer.create(512), size = buffer.create(256) } local curAcFreq: { number } = {} @@ -803,7 +803,7 @@ function Encode.run(px: buffer, w: number, h: number, bpp: number, o: Options): end local driSent = false for _, sc in script do - local sComps = {} + local sComps: { Comp } = {} for _, ci in sc.comps do table.insert(sComps, comps[ci]) end diff --git a/src/Formats/codecs/MeshFlatten.luau b/src/Formats/codecs/MeshFlatten.luau index 298a577..8d2ab73 100644 --- a/src/Formats/codecs/MeshFlatten.luau +++ b/src/Formats/codecs/MeshFlatten.luau @@ -61,7 +61,7 @@ function Flatten.numbers(acc: any, raw: boolean?): ({ number }?, string?) if buffer.len(d) < n * size then return nil, "accessor data shorter than count * components" end - local out = table.create(n) + local out = table.create(n) :: { number } if t == "f32" then for i = 0, n - 1 do out[i + 1] = readf32(d, i * 4) @@ -142,7 +142,7 @@ local function localMatrix(node: any): ({ number }?, string?) if not numList(node.matrix, 16) then return nil, "node matrix is not 16 numbers" end - local m = table.create(16) + local m = table.create(16) :: { number } for i = 1, 16 do m[i] = node.matrix[i] end @@ -304,7 +304,7 @@ local function faces(prim: any, nv: number): (any, any, string?) end idx = v else - idx = table.create(nv) + idx = table.create(nv) :: { number } for i = 1, nv do idx[i] = i - 1 end @@ -318,7 +318,7 @@ local function faces(prim: any, nv: number): (any, any, string?) if mode == "triangles" then local n = #idx - #idx % 3 if n ~= #idx then - local c = table.create(n) + local c = table.create(n) :: { number } table.move(idx, 1, n, 1, c) idx = c end diff --git a/src/Formats/codecs/Quantize/init.luau b/src/Formats/codecs/Quantize/init.luau index df4dd53..3086d63 100644 --- a/src/Formats/codecs/Quantize/init.luau +++ b/src/Formats/codecs/Quantize/init.luau @@ -134,7 +134,7 @@ local function medianCut(cnt: buffer, sums: buffer, maxColors: number): { Color local box = boxes[pick] local lo, hi, c = box[1], box[2], box[3] -- sort the range by the channel, ties by bin number: unique numeric keys, no comparator - local keys = table.create(hi - lo + 1) + local keys = table.create(hi - lo + 1) :: { number } for i = lo, hi do keys[i - lo + 1] = channel(bins[i], c) * BINS + bins[i] end @@ -157,7 +157,7 @@ local function medianCut(cnt: buffer, sums: buffer, maxColors: number): { Color boxes[pick] = scored(lo, cut) table.insert(boxes, scored(cut + 1, hi)) end - local pal = table.create(#boxes) + local pal = table.create(#boxes) :: { Color } for i, box in boxes do pal[i] = mean(cnt, sums, bins, box[1], box[2]) end @@ -247,7 +247,7 @@ local function octree(cnt: buffer, sums: buffer, maxColors: number): { Color } break end local list = byLevel[lv] - local order = table.create(#list) + local order = table.create(#list) :: { { any } } for i, node in list do order[i] = { node, subtotal(node), i } end @@ -338,7 +338,7 @@ function Quantize.map(pixels: buffer, w: number, h: number, palette: { Color }, local dither = opts and opts.dither or false local tidx = opts and opts.transparentIndex -- the searchable entries (the transparent slot is excluded) - local pr, pg, pb, pi = {}, {}, {}, {} + local pr: { number }, pg: { number }, pb: { number }, pi: { number } = {}, {}, {}, {} for i, c in palette do if tidx == nil or i - 1 ~= tidx then table.insert(pr, c[1] or 0) @@ -350,12 +350,12 @@ function Quantize.map(pixels: buffer, w: number, h: number, palette: { Color }, local np = #pr -- sorted by green (the heaviest weight), so the search can stop once 4 * dg^2 alone is worse than the best do - local order = table.create(np) + local order = table.create(np) :: { number } for k = 1, np do order[k] = pg[k] * 65536 + k end table.sort(order) - local r2, g2, b2, i2 = table.create(np), table.create(np), table.create(np), table.create(np) + local r2, g2, b2, i2 = table.create(np) :: { number }, table.create(np) :: { number }, table.create(np) :: { number }, table.create(np) :: { number } for k = 1, np do local j = order[k] % 65536 r2[k], g2[k], b2[k], i2[k] = pr[j], pg[j], pb[j], pi[j] diff --git a/src/Formats/codecs/Raster.luau b/src/Formats/codecs/Raster.luau index 4f06477..ba93fda 100644 --- a/src/Formats/codecs/Raster.luau +++ b/src/Formats/codecs/Raster.luau @@ -111,7 +111,7 @@ function Builder.cubicTo(self: Builder, c1x: number, c1y: number, c2x: number, c return self end function Builder.close(self: Builder): Builder - table.insert(self.cmds, { op = "close" }) + table.insert(self.cmds, { op = "close" } :: Cmd) return self end -- rect(x, y, w, h), ellipse(cx, cy, rx, ry): closed subpaths @@ -481,7 +481,7 @@ function Raster.fill(img: any, path: any, paint: any, opts: Options?): (boolean, if buffer.len(px) < iw * ih * 4 then return false, "fill: image pixels too short" end - local o: Options = table.clone(opts or {}) + local o: Options = table.clone(opts or {} :: Options) local clip = o.clip if type(clip) == "table" and type(clip[1]) == "number" and type(clip[2]) == "number" and type(clip[3]) == "number" and type(clip[4]) == "number" then o.clip = { max(0, clip[1]), max(0, clip[2]), min(iw, clip[3]), min(ih, clip[4]) } @@ -572,7 +572,7 @@ local function polygon(out: Path, pts: { number }) table.insert(out, { op = "lineTo", pts[i], pts[i + 1] }) end end - table.insert(out, { op = "close" }) + table.insert(out, { op = "close" } :: Cmd) end local function arcPoints(cx: number, cy: number, r: number, a0: number, a1: number, tol: number, pts: { number }) diff --git a/src/Formats/codecs/SoundFont/Parse.luau b/src/Formats/codecs/SoundFont/Parse.luau index 90cdfcc..df39807 100644 --- a/src/Formats/codecs/SoundFont/Parse.luau +++ b/src/Formats/codecs/SoundFont/Parse.luau @@ -296,7 +296,7 @@ function Parse.parse(r: Bytes.Reader, opts: Options?): (SoundFont?, Err.Err?) if layout and not hydra[sid] then local rs = RECSIZE[sid] local n = sub:remaining() // rs - local recs: { { number } } = table.create(n) + local recs = table.create(n) :: { { number } } for i = 1, n do local rec = {} for _, w in layout do @@ -487,7 +487,7 @@ function Parse.parse(r: Bytes.Reader, opts: Options?): (SoundFont?, Err.Err?) for _, p in presets do (p :: any)._order = nil end - local headers: { { [string]: any } } = table.create(nShdr) + local headers = table.create(nShdr) :: { { [string]: any } } for i = 1, nShdr do local sh = shdr[i] headers[i] = { @@ -503,7 +503,7 @@ function Parse.parse(r: Bytes.Reader, opts: Options?): (SoundFont?, Err.Err?) type = sh[10], } end - local instruments: { { name: string, zones: number } } = table.create(nInst) + local instruments = table.create(nInst) :: { { name: string, zones: number } } for i = 1, nInst do instruments[i] = { name = instNames[i], zones = math.max(0, (inst[i + 1][2] or 0) - inst[i][2]) } end diff --git a/src/Formats/codecs/TrueType/Glyf.luau b/src/Formats/codecs/TrueType/Glyf.luau index 65392f6..5d598a9 100644 --- a/src/Formats/codecs/TrueType/Glyf.luau +++ b/src/Formats/codecs/TrueType/Glyf.luau @@ -54,7 +54,7 @@ function Glyf.range(f: Face, g: number): (number?, number) if (g + 2) * 4 > f.locaLen then return nil, 0 end - a, z = u32(f.buf, f.loca + g * 4), u32(f.buf, f.loca + g * 4 + 4) + a, z = u32(f.buf, f.loca + g * 4), u32(f.buf, f.loca + g * 4 + 4) :: number else if (g + 2) * 2 > f.locaLen then return nil, 0 @@ -84,7 +84,7 @@ local MAX_POINTS = 65536 local function simple(b: buffer, o: number, len: number, nc: number): ({ Contour }?, string?) local e = o + len - local ends = table.create(nc) + local ends = table.create(nc) :: { number } local p = o + 10 if p + nc * 2 + 2 > e then return nil, "glyph header past the end" @@ -175,7 +175,7 @@ local function simple(b: buffer, o: number, len: number, nc: number): ({ Contour end ys[k] = y end - local out: { Contour } = table.create(nc) + local out = table.create(nc) :: { Contour } local s = 1 for c = 1, nc do local z = ends[c] + 1 @@ -212,9 +212,9 @@ local function composite(f: Face, o: number, len: number, depth: number): ({ Con return nil, "component past the end" end if band(flags, 2) ~= 0 then - a1, a2 = i16(b, p), i16(b, p + 2) + a1, a2 = i16(b, p), i16(b, p + 2) :: number else - a1, a2 = u16(b, p), u16(b, p + 2) + a1, a2 = u16(b, p), u16(b, p + 2) :: number end p += 4 else @@ -222,9 +222,9 @@ local function composite(f: Face, o: number, len: number, depth: number): ({ Con return nil, "component past the end" end if band(flags, 2) ~= 0 then - a1, a2 = readi8(b, p), readi8(b, p + 1) + a1, a2 = readi8(b, p), readi8(b, p + 1) :: number else - a1, a2 = readu8(b, p), readu8(b, p + 1) + a1, a2 = readu8(b, p), readu8(b, p + 1) :: number end p += 2 end @@ -248,9 +248,9 @@ local function composite(f: Face, o: number, len: number, depth: number): ({ Con return nil, err end -- transform the child's points - local tc: { Contour } = table.create(#child) + local tc = table.create(#child) :: { Contour } for i, c in child do - local nx, ny = table.create(#c.x), table.create(#c.x) + local nx, ny = table.create(#c.x) :: { number }, table.create(#c.x) :: { number } for k = 1, #c.x do local x, y = c.x[k], c.y[k] nx[k] = m1 * x + m3 * y diff --git a/src/Formats/codecs/TrueType/init.luau b/src/Formats/codecs/TrueType/init.luau index 4e7948d..3752c3f 100644 --- a/src/Formats/codecs/TrueType/init.luau +++ b/src/Formats/codecs/TrueType/init.luau @@ -183,7 +183,7 @@ function Face.rasterize(self: any, g: number, px: number, opts: RasterOptions?): if not cmds then return nil end - local o = opts or {} + local o: RasterOptions = opts or {} local sx = if type(o.scaleX) == "number" then o.scaleX else px / self.unitsPerEm local sy = if type(o.scaleY) == "number" then o.scaleY else sx local tx, ty = o.shiftX or 0, o.shiftY or 0 diff --git a/src/Formats/codecs/Video/init.luau b/src/Formats/codecs/Video/init.luau index 7c38620..425ea25 100644 --- a/src/Formats/codecs/Video/init.luau +++ b/src/Formats/codecs/Video/init.luau @@ -95,7 +95,7 @@ local function rawPlanar(data: buffer, pf: string, bits: number, w: number, h: n if pf == "gray" then u, v = 128, 128 elseif pf == "yuv420p" then - u, v = sample(uo + i * bps), sample(vo + i * bps) + u, v = sample(uo + i * bps), sample(vo + i * bps) :: number elseif pf == "yuv422p" then local y0, y1 = cy * 2, math.min(cy * 2 + 1, h - 1) u = (sample(uo + (y0 * scw + cx) * bps) + sample(uo + (y1 * scw + cx) * bps) + 1) // 2 @@ -166,7 +166,7 @@ local function rawDecoder(track: Types.Track, _opts: any?): (Types.VideoCodecDec return rawPlanar(data, pf, bits, w, h), nil end 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: number = track.stride or w * bpp if n < stride * (h - 1) + w * bpp then return nil, Err.new(Err.TRUNCATED, "raw frame too short") end @@ -186,7 +186,7 @@ local function rawDecoder(track: Types.Track, _opts: any?): (Types.VideoCodecDec end end return img, nil - end) + end), nil end local function vp8Decoder(_track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?) @@ -210,21 +210,21 @@ local function vp8Decoder(_track: Types.Track, opts: any?): (Types.VideoCodecDec else dec = Vp8.newDecoder() end - end) + end), nil end -- Motion JPEG: every packet one JPEG (or two fields), yuv420p straight from the planes (full range) when the layout -- allows, else rgba8 local function mjpegDecoder(_track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?) local Jpeg = require("@formats/codecs/Jpeg") - local o = { yuv = true, maxPixels = opts and opts.maxPixels } + local o: Jpeg.DecodeOptions = { yuv = true, maxPixels = opts and opts.maxPixels } return Video.simpleDecoder(function(data: buffer): (Types.Image?, Err.Err?) local img, err = Jpeg.decode(data, 0, buffer.len(data), o) if img and img.format == "yuv420p" then img.meta.yuv = { range = "full", matrix = "bt601" } end return img, err - end) + end), nil end -- the MJPEG encoder (writers): JPEG is full-range, so limited-range yuv420p frames are remapped first @@ -444,7 +444,7 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec end local index, flushed, skipUntil = 0, false, -math.huge local tid = if track then track.id else -1 - local scale = if track and track.timescale and track.timescale > 0 then track.timescale else 1000 + local scale: number = if track and track.timescale and track.timescale > 0 then track.timescale else 1000 local start = demuxer.startTime or 0 v.startTime = start local defaultDur = if v.frameRate > 0 then 1 / v.frameRate else 0 @@ -461,8 +461,8 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec if t + 1e-9 >= skipUntil then index += 1 local d = defaultDur - if lastPts and t > lastPts and defaultDur == 0 then - d = t - lastPts + if lastPts and t > (lastPts :: number) and defaultDur == 0 then + d = t - (lastPts :: number) end lastPts = t if o.rgba then diff --git a/src/Formats/codecs/Vp8L/Decode.luau b/src/Formats/codecs/Vp8L/Decode.luau index 6f88783..547167c 100644 --- a/src/Formats/codecs/Vp8L/Decode.luau +++ b/src/Formats/codecs/Vp8L/Decode.luau @@ -676,7 +676,7 @@ local function readCodes(d: State, xs: number, ys: number, cacheBits: number, le end if numMax > 1000 or numMax > xs * ys then -- remap the used groups to [0, numGroups); unused ones are read and checked but not kept - local map: { number } = table.create(numMax, -1) + local map = table.create(numMax, -1) :: { number } numGroups = 0 for k = 0, n - 1 do local g = readu32(img, k * 4) @@ -695,7 +695,7 @@ local function readCodes(d: State, xs: number, ys: number, cacheBits: number, le if eos(d) then return nil, nil, 0, 0, "truncated: meta codes" end - local groups: { number } = table.create(numGroups * 5, 0) + local groups = table.create(numGroups * 5, 0) :: { number } local cacheSize = if cacheBits > 0 then lshift(1, cacheBits) else 0 for i = 0, numMax - 1 do local slot = if mapping then mapping[i + 1] else i diff --git a/src/Formats/codecs/Xml/init.luau b/src/Formats/codecs/Xml/init.luau index ac0afa9..fcb574b 100644 --- a/src/Formats/codecs/Xml/init.luau +++ b/src/Formats/codecs/Xml/init.luau @@ -219,7 +219,7 @@ function Xml.descendants(node: Node, name: string?): { Node } local function walk(n: Node) for _, ch in n.children do if type(ch) == "table" and string.byte(ch.name) ~= 35 then - if name == nil or ch.name == name or (not find(name, ":", 1, true) and match(ch.name, ":(.*)$") == name) then + if name == nil or ch.name == name or (not find(name :: string, ":", 1, true) and match(ch.name, ":(.*)$") == name) then table.insert(out, ch) end walk(ch) diff --git a/src/Formats/codecs/Yuv.luau b/src/Formats/codecs/Yuv.luau index f6926e4..cfa8258 100644 --- a/src/Formats/codecs/Yuv.luau +++ b/src/Formats/codecs/Yuv.luau @@ -20,7 +20,7 @@ local Yuv = {} -- coefficients (x 65536): r = y' + crv * v; g = y' - cgu * u - cgv * v; b = y' + cbu * u -- limited: y' = (y - 16) * 255 / 219, chroma scaled by 255 / 224 -local MATRIX = { +local MATRIX: { [string]: { kr: number, kb: number } } = { bt601 = { kr = 0.299, kb = 0.114 }, bt709 = { kr = 0.2126, kb = 0.0722 }, } diff --git a/src/Formats/codecs/Zstd/Xxh64.luau b/src/Formats/codecs/Zstd/Xxh64.luau index 5aa48e8..c189970 100644 --- a/src/Formats/codecs/Zstd/Xxh64.luau +++ b/src/Formats/codecs/Zstd/Xxh64.luau @@ -122,7 +122,7 @@ function Xxh64.hash(b: buffer, offset: number, length: number): (number, number) hh, hl = add(hh, hl, math.floor(length / T32) % T32, length % T32) while p + 8 <= e do local kh, kl = round(0, 0, readu32(b, p + 4), readu32(b, p)) - hh, hl = bxor(hh, kh), bxor(hl, kl) + hh, hl = bxor(hh, kh), bxor(hl, kl) :: number hh, hl = rotl(hh, hl, 27) hh, hl = mul(hh, hl, P1H, P1L) hh, hl = add(hh, hl, P4H, P4L) @@ -130,7 +130,7 @@ function Xxh64.hash(b: buffer, offset: number, length: number): (number, number) end if p + 4 <= e then local kh, kl = mul(0, readu32(b, p), P1H, P1L) - hh, hl = bxor(hh, kh), bxor(hl, kl) + hh, hl = bxor(hh, kh), bxor(hl, kl) :: number hh, hl = rotl(hh, hl, 23) hh, hl = mul(hh, hl, P2H, P2L) hh, hl = add(hh, hl, P3H, P3L) @@ -138,7 +138,7 @@ function Xxh64.hash(b: buffer, offset: number, length: number): (number, number) end while p < e do local kh, kl = mul(0, readu8(b, p), P5H, P5L) - hh, hl = bxor(hh, kh), bxor(hl, kl) + hh, hl = bxor(hh, kh), bxor(hl, kl) :: number hh, hl = rotl(hh, hl, 11) hh, hl = mul(hh, hl, P1H, P1L) p += 1 @@ -146,7 +146,7 @@ function Xxh64.hash(b: buffer, offset: number, length: number): (number, number) -- avalanche hl = bxor(hl, rshift(hh, 1)) -- h ^= h >> 33 hh, hl = mul(hh, hl, P2H, P2L) - hh, hl = bxor(hh, rshift(hh, 29)), bxor(hl, bor(rshift(hl, 29), lshift(hh, 3))) -- h ^= h >> 29 + hh, hl = bxor(hh, rshift(hh, 29)), bxor(hl, bor(rshift(hl, 29), lshift(hh, 3))) :: number -- h ^= h >> 29 hh, hl = mul(hh, hl, P3H, P3L) hl = bxor(hl, hh) -- h ^= h >> 32 return hh, hl diff --git a/src/Formats/containers/IsoBmff/Demux.luau b/src/Formats/containers/IsoBmff/Demux.luau index 0160249..7f8689d 100644 --- a/src/Formats/containers/IsoBmff/Demux.luau +++ b/src/Formats/containers/IsoBmff/Demux.luau @@ -328,9 +328,9 @@ local function track(b: buffer, trak: Box.Box, movieTimescale: number, trex: { [ for _ = 1, n do local dur: number, mt: number if v == 1 then - dur, mt = r:u64be(), r:i64be() + dur, mt = r:u64be(), r:i64be() :: number else - dur, mt = r:u32be(), r:i32be() + dur, mt = r:u32be(), r:i32be() :: number end local rate = r:i32be() / 65536 if r.err then diff --git a/src/Formats/containers/Zip/init.luau b/src/Formats/containers/Zip/init.luau index 1545ce2..df38c88 100644 --- a/src/Formats/containers/Zip/init.luau +++ b/src/Formats/containers/Zip/init.luau @@ -152,7 +152,7 @@ local function parse(b: buffer): (any, Err.Err?) return nil, Err.new(Err.CORRUPT, "central directory not found") end end - local entries: { any } = table.create(math.min(count, 65536)) + local entries = table.create(math.min(count, 65536)) :: { any } local p = cdOff + delta for i = 1, count do if not sigAt(b, p, SIG_CD) or p + 46 > n then diff --git a/src/Formats/ops/ImageOps/Kernels.luau b/src/Formats/ops/ImageOps/Kernels.luau index cf14954..c55ae9e 100644 --- a/src/Formats/ops/ImageOps/Kernels.luau +++ b/src/Formats/ops/ImageOps/Kernels.luau @@ -81,13 +81,13 @@ function K.convert(src: buffer, sf: number, dst: buffer, df: number, n: number) local r: number, g: number, b: number, a: number if sf == 1 then local o = i * 4 - r, g, b, a = readu8(src, o), readu8(src, o + 1), readu8(src, o + 2), readu8(src, o + 3) + r, g, b, a = readu8(src, o), readu8(src, o + 1), readu8(src, o + 2), readu8(src, o + 3) :: number elseif sf == 2 then local o = i * 8 - r, g, b, a = readu16(src, o), readu16(src, o + 2), readu16(src, o + 4), readu16(src, o + 6) + r, g, b, a = readu16(src, o), readu16(src, o + 2), readu16(src, o + 4), readu16(src, o + 6) :: number elseif sf == 3 then local o = i * 16 - r, g, b, a = readf32(src, o), readf32(src, o + 4), readf32(src, o + 8), readf32(src, o + 12) + r, g, b, a = readf32(src, o), readf32(src, o + 4), readf32(src, o + 8), readf32(src, o + 12) :: number elseif sf == 4 then local o = i * 3 r, g, b, a = readu8(src, o), readu8(src, o + 1), readu8(src, o + 2), 255 @@ -99,7 +99,7 @@ function K.convert(src: buffer, sf: number, dst: buffer, df: number, n: number) r, g, b, a = v, v, v, 65535 elseif sf == 7 then local v = readu8(src, i * 2) - r, g, b, a = v, v, v, readu8(src, i * 2 + 1) + r, g, b, a = v, v, v, readu8(src, i * 2 + 1) :: number else r, g, b, a = 255, 255, 255, readu8(src, i) end @@ -118,7 +118,7 @@ function K.convert(src: buffer, sf: number, dst: buffer, df: number, n: number) elseif df == 8 then writeu8(dst, i, depthConv(a, sd, 8)) else - r, g, b = depthConv(r, sd, dd), depthConv(g, sd, dd), depthConv(b, sd, dd) + r, g, b = depthConv(r, sd, dd), depthConv(g, sd, dd), depthConv(b, sd, dd) :: number if df == 1 then local o = i * 4 writeu8(dst, o, r)