diff --git a/src/Formats/Types.luau b/src/Formats/Types.luau index effef54..f0e3a6b 100644 --- a/src/Formats/Types.luau +++ b/src/Formats/Types.luau @@ -28,7 +28,7 @@ local BPP: { [string]: number } = { graya8 = 2, alpha8 = 1, } -Types.BPP = table.freeze(BPP) +Types.BPP = table.freeze(BPP) :: { [string]: number } export type ImageMeta = { colorSpace: string?, -- "srgb" (default), "linear", "display-p3", ... @@ -429,7 +429,7 @@ local COMPONENT_SIZE: { [string]: number } = { snorm8 = 1, snorm16 = 2, } -Types.COMPONENT_SIZE = table.freeze(COMPONENT_SIZE) +Types.COMPONENT_SIZE = table.freeze(COMPONENT_SIZE) :: { [string]: number } -- accessor(data, type, components, count) -> Accessor (data = nil allocates a zero-filled buffer) function Scene.accessor(data: buffer?, type: any, components: number, count: number): Accessor local d = data or buffer.create(count * components * (COMPONENT_SIZE[type] or 4)) diff --git a/src/Formats/containers/Archive.luau b/src/Formats/containers/Archive.luau index ead480e..ef9a63d 100644 --- a/src/Formats/containers/Archive.luau +++ b/src/Formats/containers/Archive.luau @@ -45,7 +45,7 @@ local function public(e: Entry): Types.ArchiveEntry end function Methods.list(self: any): { Types.ArchiveEntry } - local out = table.create(#self._entries) + local out = table.create(#self._entries) :: { Types.ArchiveEntry } for i, e in self._entries do out[i] = public(e) end @@ -91,7 +91,7 @@ end -- write(opts?) -> buffer | nil, Err: the archive's own format (opts.format is handled by the api) function Methods.write(self: any, opts: any?): (buffer?, Err.Err?) - local enc = self._encode + local enc: Encoder? = self._encode if not enc then return nil, Err.new(Err.UNSUPPORTED, self.format .. " archives are read-only") end diff --git a/src/Formats/containers/AudioWrap.luau b/src/Formats/containers/AudioWrap.luau index fa82ebe..0646754 100644 --- a/src/Formats/containers/AudioWrap.luau +++ b/src/Formats/containers/AudioWrap.luau @@ -18,8 +18,13 @@ local Mux = require("@formats/containers/Ogg/Mux") local AudioWrap = {} -local function allPackets(dmx: any, track: Types.Track): { Types.Packet } - local out = {} +type PacketSource = { + count: ((self: any, track: number) -> number)?, + get: ((self: any, track: number, k: number) -> Types.Packet?)?, +} + +local function allPackets(dmx: PacketSource, track: Types.Track): { Types.Packet } + local out: { Types.Packet } = {} local n = if dmx.count then dmx:count(track.id) else 0 if n > 0 and dmx.get then for k = 1, n do @@ -437,11 +442,11 @@ function AudioWrap.wrap(dmx: any, track: Types.Track): (buffer?, string?, Err.Er return granule end local mb = 0 - while 2 ^ mb < #modes do + while 2 ^ mb < #(modes :: { boolean }) do mb += 1 end local m = if mb > 0 and buffer.len(d) * 8 > mb then bits(d, 1, mb) else 0 - local cur = sizes[if modes[m + 1] then 2 else 1] + local cur = sizes[if (modes :: { boolean })[m + 1] then 2 else 1] if prev then granule += (prev + cur) // 4 end diff --git a/src/Formats/containers/Avi/init.luau b/src/Formats/containers/Avi/init.luau index 521dbe9..081e9b9 100644 --- a/src/Formats/containers/Avi/init.luau +++ b/src/Formats/containers/Avi/init.luau @@ -491,7 +491,7 @@ function Avi.write(opts: WOptions, chunks: { { any } }): buffer w:u32le(0) local moviBase = w:tell() w:string("movi") - local index = table.create(#chunks) + local index = table.create(#chunks) :: { { any } } for _, c in chunks do local si, data, key = c[1], c[2], c[3] local s = opts.streams[si] diff --git a/src/Formats/containers/IsoBmff/Demux.luau b/src/Formats/containers/IsoBmff/Demux.luau index 6a5a4cb..0160249 100644 --- a/src/Formats/containers/IsoBmff/Demux.luau +++ b/src/Formats/containers/IsoBmff/Demux.luau @@ -324,9 +324,9 @@ local function track(b: buffer, trak: Box.Box, movieTimescale: number, trex: { [ r:skip(3) local n = r:u32be() if n <= (elst.stop - elst.data) // 12 then - local edits = {} + local edits: { Edit } = {} for _ = 1, n do - local dur, mt + local dur: number, mt: number if v == 1 then dur, mt = r:u64be(), r:i64be() else @@ -522,7 +522,7 @@ local function track(b: buffer, trak: Box.Box, movieTimescale: number, trex: { [ if n * 12 > r:remaining() then return nil, Err.new(Err.TRUNCATED, "stsc table runs past its box", stsc.start) end - local first, per, desc = {}, {}, {} + local first: { number }, per: { number }, desc: { number } = {}, {}, {} for k = 1, n do first[k], per[k], desc[k] = r:u32be(), r:u32be(), r:u32be() end @@ -739,7 +739,7 @@ export type Movie = { -- parse(r, opts) -> Movie | nil, Err: the box tree, ftyp, mvhd and every track (fragments included) function Demux.parse(r: Bytes.Reader, opts: Options?): (Movie?, Err.Err?) - local o = opts or {} + local o: Options = opts or {} local b = r.buf local top, terr = Box.list(b, r.base, r.limit) if #top == 0 then diff --git a/src/Formats/containers/IsoBmff/Meta.luau b/src/Formats/containers/IsoBmff/Meta.luau index a7c44ca..7792142 100644 --- a/src/Formats/containers/IsoBmff/Meta.luau +++ b/src/Formats/containers/IsoBmff/Meta.luau @@ -187,7 +187,7 @@ function Meta.items(b: buffer, meta: Box.Box): (Items?, Err.Err?) it = { id = id, type = "", name = "", hidden = false, construction = 0, baseOffset = 0, extents = {}, properties = {}, essential = {}, refs = {}, - } + } :: Item out.items[id] = it table.insert(out.order, id) end @@ -496,7 +496,7 @@ function Meta.writeIlst(w: Bytes.Writer, tags: { [string]: string }, pictures: { end if pictures and #pictures > 0 then box(w, "covr", function() - for _, p in pictures do + for _, p in pictures :: { Picture } do dataBox(w, if p.mime == "image/png" then 14 elseif p.mime == "image/bmp" then 27 else 13, p.data) end end) diff --git a/src/Formats/containers/MpegPs/init.luau b/src/Formats/containers/MpegPs/init.luau index 53f75be..5229571 100644 --- a/src/Formats/containers/MpegPs/init.luau +++ b/src/Formats/containers/MpegPs/init.luau @@ -193,7 +193,7 @@ function MpegPs.read(b: buffer, base: number, limit: number): (File?, Err.Err?) if ok and q <= stop then local s = file.streams[id] if not s then - s = { id = id, kind = kind, codec = codec, streamType = st, pes = {} } + s = { id = id, kind = kind, codec = codec, streamType = st, pes = {} } :: Stream file.streams[id] = s table.insert(file.order, id) end diff --git a/src/Formats/containers/Tar.luau b/src/Formats/containers/Tar.luau index 5627311..d98afda 100644 --- a/src/Formats/containers/Tar.luau +++ b/src/Formats/containers/Tar.luau @@ -350,7 +350,7 @@ function Tar.write(entries: { WriteEntry }, opts: WriteOptions?): (buffer?, Err. end local data = e.data or buffer.create(0) local typ = e.type or (if e.dir then "5" else "0") - local name = e.name + local name: string = e.name if typ == "5" and string.sub(name, -1) ~= "/" then name ..= "/" end diff --git a/src/Formats/ops/ImageOps/Kernels.luau b/src/Formats/ops/ImageOps/Kernels.luau index 68d0d58..cf14954 100644 --- a/src/Formats/ops/ImageOps/Kernels.luau +++ b/src/Formats/ops/ImageOps/Kernels.luau @@ -13,9 +13,9 @@ local floor, clamp = math.floor, math.clamp local K = {} -- format ids: rgba8 1, rgba16 2, rgbaf32 3, rgb8 4, gray8 5, gray16 6, graya8 7, alpha8 8 -K.ID = { rgba8 = 1, rgba16 = 2, rgbaf32 = 3, rgb8 = 4, gray8 = 5, gray16 = 6, graya8 = 7, alpha8 = 8 } -K.KIND = { rgba8 = 1, rgba16 = 2, rgbaf32 = 4, rgb8 = 1, gray8 = 1, gray16 = 2, graya8 = 1, alpha8 = 1 } -K.CHANNELS = { rgba8 = 4, rgba16 = 4, rgbaf32 = 4, rgb8 = 3, gray8 = 1, gray16 = 1, graya8 = 2, alpha8 = 1 } +K.ID = { rgba8 = 1, rgba16 = 2, rgbaf32 = 3, rgb8 = 4, gray8 = 5, gray16 = 6, graya8 = 7, alpha8 = 8 } :: { [string]: number } +K.KIND = { rgba8 = 1, rgba16 = 2, rgbaf32 = 4, rgb8 = 1, gray8 = 1, gray16 = 2, graya8 = 1, alpha8 = 1 } :: { [string]: number } +K.CHANNELS = { rgba8 = 4, rgba16 = 4, rgbaf32 = 4, rgb8 = 3, gray8 = 1, gray16 = 1, graya8 = 2, alpha8 = 1 } :: { [string]: number } local function rd(b: buffer, o: number, kind: number): number if kind == 1 then @@ -74,11 +74,11 @@ local function luma(r: number, g: number, b: number, depth: number): number end function K.convert(src: buffer, sf: number, dst: buffer, df: number, n: number) - local sd, dd = DEPTH[sf], DEPTH[df] + local sd, dd = DEPTH[sf] :: number, DEPTH[df] :: number local smax = if sd == 8 then 255 elseif sd == 16 then 65535 else 1 local toGray = df == 5 or df == 6 or df == 7 for i = 0, n - 1 do - local r, g, b, a + 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) @@ -281,7 +281,7 @@ function K.coeffs(inSize: number, outSize: number, filter: string): Coeffs local filterscale = math.max(scale, 1) local support = support0 * filterscale local ss = 1 / filterscale - local start, count, weights = table.create(outSize, 0), table.create(outSize, 0), table.create(outSize) + local start, count, weights = table.create(outSize, 0), table.create(outSize, 0), table.create(outSize) :: { { number } } for xx = 0, outSize - 1 do local center = (xx + 0.5) * scale local xmin = math.max(0, trunc(center - support + 0.5)) @@ -311,7 +311,7 @@ function K.resamplePass(src: buffer, w: number, h: number, ch: number, kind: num -- 8-bit: Pillow's 22-bit fixed-point coefficients local iw: { { number } } = weights if fixed then - iw = table.create(outN) + iw = table.create(outN) :: { { number } } for i = 1, outN do local src0 = weights[i] local t = table.create(#src0, 0) diff --git a/src/Formats/ops/ImageOps/init.luau b/src/Formats/ops/ImageOps/init.luau index 5c6abde..6ddc177 100644 --- a/src/Formats/ops/ImageOps/init.luau +++ b/src/Formats/ops/ImageOps/init.luau @@ -62,7 +62,7 @@ end ImageOps.check = check local function limit(w: number, h: number, opts: any): (boolean?, Err.Err?) - local max = (type(opts) == "table" and opts.maxPixels) or MAX_PIXELS + local max: number = (type(opts) == "table" and opts.maxPixels) or MAX_PIXELS 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)) end @@ -159,7 +159,7 @@ local function viaRgba(img: Image, f: (Image) -> (Image?, Err.Err?)): (Image?, E end -- ------------------------------------------------------------------------------------------------ geometry -local ALPHA_CH = { rgba8 = 4, rgba16 = 4, rgbaf32 = 4, graya8 = 2 } +local ALPHA_CH: { [string]: number } = { rgba8 = 4, rgba16 = 4, rgbaf32 = 4, graya8 = 2 } -- crop(img, x, y, w, h) -> the rectangle clipped to the image | nil, Err (empty) function ImageOps.crop(img: Image, x: number, y: number, w: number, h: number): (Image?, Err.Err?) @@ -338,7 +338,7 @@ function ImageOps.rotate(img: Image, degrees: number, opts: { expand: boolean?, end -- ------------------------------------------------------------------------------------------------ compositing -local RGBA = { rgba8 = true, rgba16 = true, rgbaf32 = true } +local RGBA: { [string]: boolean } = { rgba8 = true, rgba16 = true, rgbaf32 = true } export type BlitOptions = { blend: ("over" | "source")?, opacity: number? } @@ -435,7 +435,7 @@ function ImageOps.unpremultiply(img: Image): (Image?, Err.Err?) end -- ------------------------------------------------------------------------------------------------ pixels -local MAXV = { rgba8 = 255, rgba16 = 65535, rgbaf32 = 1, rgb8 = 255, gray8 = 255, gray16 = 65535, graya8 = 255, alpha8 = 255, yuv420p = 255 } +local MAXV: { [string]: number } = { rgba8 = 255, rgba16 = 65535, rgbaf32 = 1, rgb8 = 255, gray8 = 255, gray16 = 65535, graya8 = 255, alpha8 = 255, yuv420p = 255 } -- getPixel(img, x, y) -> r, g, b, a in the format's scale (0..255, 0..65535 or float); grey gives r = g = b; -- alpha8 gives white; yuv420p converts to RGB. nil, Err out of range. diff --git a/src/Formats/ops/MeshOps/Greedy.luau b/src/Formats/ops/MeshOps/Greedy.luau index 26ddb50..b65c507 100644 --- a/src/Formats/ops/MeshOps/Greedy.luau +++ b/src/Formats/ops/MeshOps/Greedy.luau @@ -97,7 +97,7 @@ function Greedy.run(data: buffer, sx: number, sy: number, sz: number, ox: number buffer.copy(nb, 0, b, 0, cap * per) return nb end - pos, nrm, idx, tri = grow2(pos, 48), grow2(nrm, 48), grow2(idx, 4), grow2(tri, 24) + pos, nrm, idx, tri = grow2(pos, 48), grow2(nrm, 48), grow2(idx, 4), grow2(tri, 24) :: buffer cap = ncap end corner[d + 1] = slice + 1 diff --git a/src/Formats/ops/MeshOps/init.luau b/src/Formats/ops/MeshOps/init.luau index 4bcfacd..d3763d2 100644 --- a/src/Formats/ops/MeshOps/init.luau +++ b/src/Formats/ops/MeshOps/init.luau @@ -272,7 +272,7 @@ function MeshOps.triangulate(prim: Primitive, opts: { method: string? }?): (Prim table.insert(out, readu32(ib, (at + a + 1) * 4)) end else - local poly = table.create(k) + local poly = table.create(k) :: { number } for a = 0, k - 1 do poly[a + 1] = readu32(ib, (at + a) * 4) end @@ -856,7 +856,7 @@ function MeshOps.merge(input: any, opts: { byMaterial: boolean?, method: string? end return out, nil end - local all = groups[0] or {} + local all: { Primitive } = groups[0] or {} local first = all[1] return concat(all, first and first.material), nil end @@ -1025,7 +1025,7 @@ function MeshOps.voxelsToPrimitive(v: Types.Voxels, opts: { pivot: string?, scal local col = buffer.create(nv * (if srgb then 4 else 16)) local lut: { { number } } = {} for i = 1, 255 do - local c = pal[i] or { 255, 255, 255, 255 } + local c: { number } = pal[i] or { 255, 255, 255, 255 } if srgb then lut[i] = { c[1] or 0, c[2] or 0, c[3] or 0, c[4] or 255 } else