ws-cleanup-types: Types, api, host: honest annotations; return the full annotated value count

This commit is contained in:
4DBug committed 2026-10-31 10:43:19 -05:00
1 parent 59a0352ca6
commit f209801579
22 files changed
+147 -147

No files matched your search

+23 -23
View File
@@ -57,7 +57,7 @@ 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
@@ -66,7 +66,7 @@ local function limit(w: number, h: number, opts: any): (boolean?, Err.Err?)
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
@@ -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,7 +333,7 @@ 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
@@ -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)
@@ -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