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

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

No files matched your search

+12 -12
View File
@@ -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,20 +74,20 @@ 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)
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)
@@ -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)
+27 -27
View File
@@ -57,16 +57,16 @@ local function check(img: any): (boolean?, Err.Err?)
if buffer.len(img.pixels) < need then
return arg("pixel buffer too small for " .. img.width .. " x " .. img.height .. " " .. img.format)
end
return true
return true, nil
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
return true
return true, nil
end
-- a new Image of the same kind with a copy of meta's plain fields (source, palette...) but no frames
@@ -93,7 +93,7 @@ function ImageOps.new(w: number, h: number, format: string?): (Image?, Err.Err?)
if not ok then
return nil, e
end
return Types.Image.new(w, h, fmt :: any)
return Types.Image.new(w, h, fmt :: any), nil
end
-- copy(img) -> a deep copy of the pixels (frames are shared)
@@ -105,7 +105,7 @@ function ImageOps.copy(img: Image): (Image?, Err.Err?)
local out = like(img, img.width, img.height)
buffer.copy(out.pixels, 0, img.pixels, 0, buffer.len(out.pixels))
out.frames = img.frames
return out
return out, nil
end
-- ------------------------------------------------------------------------------------------------ convert
@@ -127,7 +127,7 @@ function ImageOps.convert(img: Image, format: string): (Image?, Err.Err?)
local rgba = like(img, w, h, "rgba8")
K.yuvToRgba(img.pixels, w, h, rgba.pixels)
if format == "rgba8" then
return rgba
return rgba, nil
end
src = rgba
end
@@ -139,10 +139,10 @@ function ImageOps.convert(img: Image, format: string): (Image?, Err.Err?)
K.convert(src.pixels, K.ID[src.format], rgba.pixels, K.ID.rgba8, w * h)
end
K.rgbaToYuv(rgba.pixels, w, h, out.pixels)
return out
return out, nil
end
K.convert(src.pixels, K.ID[src.format], out.pixels, K.ID[format], w * h)
return out
return out, nil
end
-- ops on yuv420p run on rgba8 and convert back
@@ -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?)
@@ -179,7 +179,7 @@ function ImageOps.crop(img: Image, x: number, y: number, w: number, h: number):
local bpp = Types.BPP[im.format] :: number
local out = like(im, x1 - x0, y1 - y0)
K.copyRect(out.pixels, out.width, im.pixels, im.width, x0, y0, 0, 0, out.width, out.height, bpp)
return out
return out, nil
end)
end
@@ -195,7 +195,7 @@ function ImageOps.flip(img: Image, axis: string): (Image?, Err.Err?)
return viaRgba(img, function(im: Image): (Image?, Err.Err?)
local out = like(im, im.width, im.height)
K.flip(im.pixels, out.pixels, im.width, im.height, Types.BPP[im.format] :: number, axis == "h")
return out
return out, nil
end)
end
@@ -216,7 +216,7 @@ function ImageOps.rotate90(img: Image, turns: number): (Image?, Err.Err?)
local swap = t ~= 2
local out = like(im, if swap then im.height else im.width, if swap then im.width else im.height)
K.rotate90(im.pixels, out.pixels, im.width, im.height, Types.BPP[im.format] :: number, t)
return out
return out, nil
end)
end
@@ -245,7 +245,7 @@ function ImageOps.resize(img: Image, w: number, h: number, filter: string?, opts
local out = like(im, w, h)
if f == "nearest" then
K.nearest(im.pixels, im.width, im.height, bpp, out.pixels, w, h)
return out
return out, nil
end
local kind, ch = K.KIND[fmt], K.CHANNELS[fmt]
local src, sw, sh = im.pixels, im.width, im.height
@@ -264,14 +264,14 @@ function ImageOps.resize(img: Image, w: number, h: number, filter: string?, opts
K.resamplePass(src, sw, sh, ch, kind, out.pixels, h, K.coeffs(sh, h, f), false)
elseif src == im.pixels or (w == im.width and alpha) then
buffer.copy(out.pixels, 0, im.pixels, 0, w * h * bpp)
return out
return out, nil
else
out.pixels = src
end
if alpha then
K.premultiply(out.pixels, w * h, alpha, kind, true)
end
return out
return out, nil
end)
end
@@ -333,12 +333,12 @@ function ImageOps.rotate(img: Image, degrees: number, opts: { expand: boolean?,
if alpha then
K.premultiply(out.pixels, nw * nh, alpha, kind, true)
end
return out
return out, nil
end)
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? }
@@ -371,7 +371,7 @@ function ImageOps.blit(dst: Image, src: Image, x: number, y: number, opts: BlitO
local w = math.min(src.width - sx, dst.width - dx)
local h = math.min(src.height - sy, dst.height - dy)
if w <= 0 or h <= 0 then
return dst
return dst, nil
end
if dst.format == "yuv420p" or (blend == "over" and not RGBA[dst.format]) then
-- work on an rgba copy of the region, then write it back in dst's format
@@ -387,7 +387,7 @@ function ImageOps.blit(dst: Image, src: Image, x: number, y: number, opts: BlitO
local back = ImageOps.convert(work, dst.format) :: Image
K.copyRect(dst.pixels, dst.width, back.pixels, back.width, 0, 0, dx, dy, w, h, Types.BPP[dst.format] :: number)
end
return dst
return dst, nil
end
local s = src
if src.format ~= dst.format then
@@ -400,7 +400,7 @@ function ImageOps.blit(dst: Image, src: Image, x: number, y: number, opts: BlitO
else
K.overFloat(dst.pixels, dst.width, s.pixels, s.width, sx, sy, dx, dy, w, h, opacity, K.KIND[dst.format])
end
return dst
return dst, nil
end
-- composite(dst, src, x?, y?, opts?) -> a new Image: a copy of dst with src blitted onto it
@@ -423,7 +423,7 @@ local function premul(img: Image, un: boolean): (Image?, Err.Err?)
if ch then
K.premultiply(out.pixels, img.width * img.height, ch, K.KIND[img.format], un)
end
return out
return out, nil
end
-- premultiply(img) / unpremultiply(img) -> a new Image (formats without alpha are copied unchanged)
@@ -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.
@@ -543,7 +543,7 @@ function ImageOps.setPixel(img: Image, x: number, y: number, r: number, g: numbe
else
wu8(px, i, q(al))
end
return true
return true, nil
end
-- fill(img, r, g, b, a?) -> img (in place) with every pixel set (values as setPixel)
@@ -567,7 +567,7 @@ function ImageOps.fill(img: Image, r: number, g: number, b: number, a: number?):
buffer.copy(img.pixels, filled, img.pixels, 0, c)
filled += c
end
return img
return img, nil
end
-- ------------------------------------------------------------------------------------------------ animation
@@ -602,7 +602,7 @@ function ImageOps.coalesce(img: Image): ({ Image }?, Err.Err?)
end
local frames = img.frames
if type(frames) ~= "table" or #frames == 0 then
return { img }
return { img }, nil
end
local fmt = if RGBA[img.format] then img.format else "rgba8"
local canvas = Types.Image.new(img.width, img.height, fmt :: any)
@@ -647,7 +647,7 @@ function ImageOps.coalesce(img: Image): ({ Image }?, Err.Err?)
canvas.pixels = saved
end
end
return out
return out, nil
end
-- fromFrames(images, delays?) -> an animated Image: img.frames = the images as full-canvas frames (x = y = 0,
@@ -683,7 +683,7 @@ function ImageOps.fromFrames(images: { Image }, delays: (number | { number })?):
buffer.copy(out.pixels, 0, first.pixels, 0, buffer.len(out.pixels))
out.frames = frames :: any
out.loops = 0
return out
return out, nil
end
return ImageOps
+1 -1
View File
@@ -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
+3 -3
View File
@@ -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