ws-cleanup: merge ws/cleanup-types (type annotations)

This commit is contained in:
4DBug committed 2026-10-31 11:27:42 -05:00
commit 4478332a61
115 files changed
+583 -515

No files matched your search

+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
+5 -5
View File
@@ -123,7 +123,7 @@ function Codec.pack(s: buffer, n: number, ch: number, maxval: number, invert: bo
r, g, b, a = get(q), get(q + 1), get(q + 2), get(q + 3)
end
if fmt == 0 then
writeu32(dst, i * 4, r + g * 256 + b * 65536 + a * 16777216)
writeu32(dst, i * 4, r + g * 256 + b * 65536 + (a :: number) * 16777216)
elseif fmt == 1 then
writeu8(dst, i * 3, r)
writeu8(dst, i * 3 + 1, g)
@@ -132,12 +132,12 @@ function Codec.pack(s: buffer, n: number, ch: number, maxval: number, invert: bo
writeu8(dst, i, r)
elseif fmt == 3 then
writeu8(dst, i * 2, r)
writeu8(dst, i * 2 + 1, a)
writeu8(dst, i * 2 + 1, a :: number)
elseif fmt == 4 then
writeu16(dst, i * 8, r)
writeu16(dst, i * 8 + 2, g)
writeu16(dst, i * 8 + 4, b)
writeu16(dst, i * 8 + 6, a)
writeu16(dst, i * 8 + 6, a :: number)
elseif fmt == 5 then
writeu16(dst, i * 2, r)
end
@@ -217,9 +217,9 @@ function Codec.extract(px: buffer, fmt: number, n: number, ch: number, wide: boo
for c = 0, vals - 1 do
local v
if ch <= 2 then
v = if c == 0 then gray else a
v = if c == 0 then gray else a :: number
else
v = if c == 0 then r elseif c == 1 then g elseif c == 2 then b else a
v = if c == 0 then r elseif c == 1 then g elseif c == 2 then b else a :: number
end
if wide and not w16 then
v *= 257
+14 -13
View File
@@ -38,8 +38,8 @@ export type EncodeOptions = {
}
-- ------------------------------------------------------------------------------------------------ constants
local FMT_CODE = { rgba8 = 0, rgb8 = 1, gray8 = 2, graya8 = 3, rgba16 = 4, gray16 = 5, rgbaf32 = 6 }
local BPP = { rgba8 = 4, rgb8 = 3, gray8 = 1, graya8 = 2, rgba16 = 8, gray16 = 2, rgbaf32 = 16 }
local FMT_CODE: { [string]: number } = { rgba8 = 0, rgb8 = 1, gray8 = 2, graya8 = 3, rgba16 = 4, gray16 = 5, rgbaf32 = 6 }
local BPP: { [string]: number } = { rgba8 = 4, rgb8 = 3, gray8 = 1, graya8 = 2, rgba16 = 8, gray16 = 2, rgbaf32 = 16 }
local readu8 = buffer.readu8
-- ------------------------------------------------------------------------------------------------ sniff
@@ -106,7 +106,7 @@ function Pnm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
end
local kind = string.char(readu8(b, r.base + 1))
local p = r.base + 2
local w, h, maxval, ch, mode
local w: number?, h: number?, maxval: number?, ch: number, mode: number
local bits = false -- PBM: 1 = black
local bw = false -- PAM BLACKANDWHITE: 1 = white
local tupl: string? = nil
@@ -137,9 +137,9 @@ function Pnm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
end
ch = if kind == "3" or kind == "6" or kind == "F" then 3 else 1
bits = kind == "1" or kind == "4"
mode = if kind == "1" then 4 elseif kind == "2" or kind == "3" then 3 elseif kind == "4" then 0 elseif maxval > 255 then 2 else 1
mode = if kind == "1" then 4 elseif kind == "2" or kind == "3" then 3 elseif kind == "4" then 0 elseif (maxval :: number) > 255 then 2 else 1
elseif kind == "7" then
local depth
local depth: number?
while true do
local key
key, p = token(b, p, limit)
@@ -175,15 +175,16 @@ function Pnm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
if w == nil or h == nil or depth == nil or maxval == nil then
return nil, Err.new(Err.CORRUPT, "PAM header misses WIDTH / HEIGHT / DEPTH / MAXVAL", p)
end
if depth < 1 or depth > 4 then
return nil, Err.new(Err.UNSUPPORTED, "PAM depth " .. depth)
if (depth :: number) < 1 or (depth :: number) > 4 then
return nil, Err.new(Err.UNSUPPORTED, "PAM depth " .. (depth :: number))
end
ch = depth
bw = tupl ~= nil and string.sub(tupl, 1, 13) == "BLACKANDWHITE"
mode = if maxval > 255 then 2 else 1
ch = depth :: number
bw = tupl ~= nil and string.sub(tupl :: string, 1, 13) == "BLACKANDWHITE"
mode = if (maxval :: number) > 255 then 2 else 1
else
return nil, Err.new(Err.CORRUPT, "not a Netpbm file", r.base)
end
local w, h, maxval = w :: number, h :: number, maxval :: number -- each branch above returns when one is nil
if w < 1 or h < 1 or maxval < 1 or maxval > 65535 then
return nil, Err.new(Err.CORRUPT, string.format("bad size %d x %d or maxval %d", w, h, maxval))
end
@@ -221,7 +222,7 @@ function Pnm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
if not ok then
return nil, Err.new(if e == "truncated" then Err.TRUNCATED else Err.CORRUPT, tostring(e))
end
local fmt = "rgba8"
local fmt: Types.PixelFormat = "rgba8"
if native then
local wide = maxval > 255
if bits or bw or ch == 1 then
@@ -260,7 +261,7 @@ function Pnm.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
if fc == nil then
return nil, Err.new(Err.ARG, "unsupported pixel format " .. tostring(img.format))
end
local w, h = img.width, img.height
local w: number, h: number = img.width, img.height
if type(w) ~= "number" or type(h) ~= "number" or w < 1 or h < 1 or w % 1 ~= 0 or h % 1 ~= 0 or buffer.len(img.pixels) < w * h * BPP[img.format] then
return nil, Err.new(Err.ARG, "bad image size or pixel buffer")
end
@@ -301,7 +302,7 @@ function Pnm.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
end
local ascii = o.ascii == true
local wideSrc = img.format == "gray16" or img.format == "rgba16" or img.format == "rgbaf32"
local maxval = o.maxval or (if wideSrc then 65535 else 255)
local maxval: number = o.maxval or (if wideSrc then 65535 else 255)
if type(maxval) ~= "number" or maxval < 1 or maxval > 65535 or maxval % 1 ~= 0 then
return nil, Err.new(Err.ARG, "maxval must be 1..65535")
end
+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
+9 -9
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)
len = math.max(0, math.min(len, c.limit - p))
if p < c.base or p >= c.limit then
return 0
return 0, nil
end
end
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
local n = math.min(len, size)
buffer.copy(out, o, src, sp, n)
return n
return n, nil
elseif comp == 5 then
local n = Lzw.decode(src, sp, len, out, o, size, "tiff")
return n
return n, nil
elseif comp == 8 or comp == 32946 then
local raw = Deflate.zlib(src, sp, len, size)
if not raw then
@@ -235,9 +235,9 @@ local function inflateChunk(c: Ctx, comp: number, p: number, len: number, out: b
end
local n = math.min(buffer.len(raw :: buffer), size)
buffer.copy(out, o, raw :: buffer, 0, n)
return n
return n, nil
elseif comp == 32773 then
return Pixels.packbits(src, sp, len, out, o, size)
return Pixels.packbits(src, sp, len, out, o, size), nil
end
return nil, Err.new(Err.UNSUPPORTED, "compression " .. comp)
end
@@ -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