diff --git a/src/Rosetta/ops/ImageOps/init.luau b/src/Rosetta/ops/ImageOps/init.luau index 4fe7742..3df4e4e 100644 --- a/src/Rosetta/ops/ImageOps/init.luau +++ b/src/Rosetta/ops/ImageOps/init.luau @@ -1,15 +1,6 @@ --!strict --- ops/ImageOps: editing on the standard Image (layer 5.5: core, Types and codecs only). Pixel format conversion, --- crop, resize (nearest / bilinear / Lanczos-3, as Pillow), flips, quarter turns and arbitrary rotation (bilinear, as --- Pillow's rotate), alpha compositing (Pillow's alpha_composite for rgba8), premultiplication, pixel access, and --- helpers for animations (Image.frames). New code; the resampling and compositing follow Pillow's C sources. --- --- local Ops = require("@rosetta/ops/ImageOps") --- local small = Ops.resize(img, 320, 240, "lanczos") -- every call returns a new Image | nil, Err (never throws) --- local grey = Ops.convert(img, "gray8") --- Ops.blit(canvas, sprite, 10, 20) -- in place on canvas, straight-alpha "over" --- for i, frame in Ops.coalesce(gif) do ... end -- full-canvas frames of an animation --- +-- ops/ImageOps: editing on the standard Image: conversion, crop, resize, flips, rotation, compositing, pixel access, +-- animation helpers. New code; the resampling and compositing follow Pillow's C sources. -- Conventions: angles are degrees counter-clockwise and rotate90 turns are counter-clockwise quarter turns (as -- Pillow). Resizing and rotating filter premultiplied alpha (as Pillow does for RGBA / LA). Grey is Pillow's -- L = (19595 R + 38470 G + 7471 B + 32768) >> 16. 8 <-> 16 bit is * 257 and @@ -25,7 +16,6 @@ local MAX_PIXELS = 2 ^ 26 type Image = Types.Image --- ------------------------------------------------------------------------------------------------ validation local function arg(msg: string): (nil, Err.Err) return nil, Err.new(Err.ARG, msg) end @@ -42,7 +32,6 @@ local function bytesOf(fmt: string, w: number, h: number): number? return if bpp then w * h * bpp else nil end --- check(img) -> true | nil, Err: a well-formed Image (known format, sizes, buffer long enough) local function check(img: any): (boolean?, Err.Err?) if type(img) ~= "table" or type(img.pixels) ~= "buffer" then return arg("expected an Image") @@ -69,7 +58,6 @@ local function limit(w: number, h: number, opts: any): (boolean?, Err.Err?) return true, nil end --- a new Image of the same kind with a copy of meta's plain fields (source, palette...) but no frames local function like(img: Image, w: number, h: number, fmt: string?): Image local out = Types.Image.new(w, h, (fmt or img.format) :: any) for k, v in img.meta do @@ -79,8 +67,6 @@ local function like(img: Image, w: number, h: number, fmt: string?): Image return out end --- ------------------------------------------------------------------------------------------------ basics --- new(w, h, format?) -> a zero-filled Image | nil, Err function ImageOps.new(w: number, h: number, format: string?): (Image?, Err.Err?) local fmt = format or "rgba8" if not isInt(w) or not isInt(h) or w < 1 or h < 1 then @@ -96,7 +82,6 @@ function ImageOps.new(w: number, h: number, format: string?): (Image?, Err.Err?) return Types.Image.new(w, h, fmt :: any), nil end --- copy(img) -> a deep copy of the pixels (frames are shared) function ImageOps.copy(img: Image): (Image?, Err.Err?) local ok, e = check(img) if not ok then @@ -108,8 +93,6 @@ function ImageOps.copy(img: Image): (Image?, Err.Err?) return out, nil end --- ------------------------------------------------------------------------------------------------ convert --- convert(img, format) -> a new Image in format (a copy when it is already) function ImageOps.convert(img: Image, format: string): (Image?, Err.Err?) local ok, e = check(img) if not ok then @@ -158,10 +141,8 @@ local function viaRgba(img: Image, f: (Image) -> (Image?, Err.Err?)): (Image?, E return ImageOps.convert(res, "yuv420p") end --- ------------------------------------------------------------------------------------------------ geometry 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?) local ok, e = check(img) if not ok then @@ -183,7 +164,6 @@ function ImageOps.crop(img: Image, x: number, y: number, w: number, h: number): end) end --- flip(img, "h" | "v") -> mirrored horizontally (left-right) or vertically (top-bottom) function ImageOps.flip(img: Image, axis: string): (Image?, Err.Err?) local ok, e = check(img) if not ok then @@ -199,7 +179,6 @@ function ImageOps.flip(img: Image, axis: string): (Image?, Err.Err?) end) end --- rotate90(img, turns) -> turned by turns counter-clockwise quarter turns (any integer; negative = clockwise) function ImageOps.rotate90(img: Image, turns: number): (Image?, Err.Err?) local ok, e = check(img) if not ok then @@ -222,7 +201,6 @@ end export type ResizeFilter = "nearest" | "bilinear" | "lanczos" --- resize(img, w, h, filter?, opts?) -> w x h (filter default "bilinear"; opts.maxPixels) function ImageOps.resize(img: Image, w: number, h: number, filter: string?, opts: any?): (Image?, Err.Err?) local ok, e = check(img) if not ok then @@ -275,9 +253,6 @@ function ImageOps.resize(img: Image, w: number, h: number, filter: string?, opts end) end --- rotate(img, degrees, opts?) -> rotated counter-clockwise about the centre with bilinear sampling (as Pillow's --- rotate(angle, BILINEAR, expand)): opts.expand (default true) grows the canvas to hold the whole image; uncovered --- pixels are zero (transparent). opts.maxPixels. function ImageOps.rotate(img: Image, degrees: number, opts: { expand: boolean?, maxPixels: number? }?): (Image?, Err.Err?) local ok, e = check(img) if not ok then @@ -337,14 +312,10 @@ function ImageOps.rotate(img: Image, degrees: number, opts: { expand: boolean?, end) end --- ------------------------------------------------------------------------------------------------ compositing local RGBA: { [string]: boolean } = { rgba8 = true, rgba16 = true, rgbaf32 = true } export type BlitOptions = { blend: ("over" | "source")?, opacity: number? } --- blit(dst, src, x, y, opts?) -> dst (modified in place) | nil, Err. src is clipped to dst; it is converted to dst's --- format first when they differ. blend "over" (default: Porter-Duff over, straight alpha; Pillow's alpha_composite --- for rgba8) or "source" (copy); opts.opacity 0..1 scales src alpha ("over" only). function ImageOps.blit(dst: Image, src: Image, x: number, y: number, opts: BlitOptions?): (Image?, Err.Err?) local ok, e = check(dst) if not ok then @@ -365,7 +336,6 @@ function ImageOps.blit(dst: Image, src: Image, x: number, y: number, opts: BlitO if type(opacity) ~= "number" or not (opacity >= 0 and opacity <= 1) then return arg("blit: opacity must be 0..1") end - -- clip local sx, sy = math.max(0, -x), math.max(0, -y) local dx, dy = math.max(0, x), math.max(0, y) local w = math.min(src.width - sx, dst.width - dx) @@ -403,7 +373,6 @@ function ImageOps.blit(dst: Image, src: Image, x: number, y: number, opts: BlitO return dst, nil end --- composite(dst, src, x?, y?, opts?) -> a new Image: a copy of dst with src blitted onto it function ImageOps.composite(dst: Image, src: Image, x: number?, y: number?, opts: BlitOptions?): (Image?, Err.Err?) local out, e = ImageOps.copy(dst) if not out then @@ -426,7 +395,6 @@ local function premul(img: Image, un: boolean): (Image?, Err.Err?) return out, nil end --- premultiply(img) / unpremultiply(img) -> a new Image (formats without alpha are copied unchanged) function ImageOps.premultiply(img: Image): (Image?, Err.Err?) return premul(img, false) end @@ -434,11 +402,8 @@ function ImageOps.unpremultiply(img: Image): (Image?, Err.Err?) return premul(img, true) end --- ------------------------------------------------------------------------------------------------ pixels 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. function ImageOps.getPixel(img: Image, x: number, y: number): (any, any, number?, number?) local ok, e = check(img) if not ok then @@ -478,8 +443,6 @@ function ImageOps.getPixel(img: Image, x: number, y: number): (any, any, number? return ru8(rgba.pixels, j), ru8(rgba.pixels, j + 1), ru8(rgba.pixels, j + 2), 255 end --- setPixel(img, x, y, r, g, b, a?) -> true (in place) | nil, Err. Values in the format's scale (clamped and --- rounded for integer formats); grey formats take Pillow's luma of r, g, b; a defaults to opaque. Not for yuv420p. function ImageOps.setPixel(img: Image, x: number, y: number, r: number, g: number, b: number, a: number?): (boolean?, Err.Err?) local ok, e = check(img) if not ok then @@ -546,7 +509,6 @@ function ImageOps.setPixel(img: Image, x: number, y: number, r: number, g: numbe return true, nil end --- fill(img, r, g, b, a?) -> img (in place) with every pixel set (values as setPixel) function ImageOps.fill(img: Image, r: number, g: number, b: number, a: number?): (Image?, Err.Err?) local ok, e = check(img) if not ok then @@ -570,8 +532,6 @@ function ImageOps.fill(img: Image, r: number, g: number, b: number, a: number?): return img, nil end --- ------------------------------------------------------------------------------------------------ animation --- frameCount(img) -> the number of frames (1 for a still image) function ImageOps.frameCount(img: Image): number if type(img) == "table" and type(img.frames) == "table" and #img.frames > 0 then return #img.frames @@ -579,7 +539,6 @@ function ImageOps.frameCount(img: Image): number return 1 end --- durations(img) -> { seconds } per frame ({ 0 } for a still image) function ImageOps.durations(img: Image): { number } local out = {} if type(img) == "table" and type(img.frames) == "table" and #img.frames > 0 then @@ -591,10 +550,7 @@ function ImageOps.durations(img: Image): { number } return { 0 } end --- coalesce(img) -> { Image } | nil, Err: every frame composited onto a width x height canvas (rgba8, or rgba16 / --- rgbaf32 when the image is), honouring each frame's x, y, blend ("over" / "source") and the previous frame's --- dispose ("none" keeps the canvas, "background" clears the frame's rectangle to transparent, "previous" restores --- the canvas from before the frame). A still image gives { img }. +-- coalesce: dispose "none" keeps the canvas, "background" clears the frame's rectangle, "previous" restores it function ImageOps.coalesce(img: Image): ({ Image }?, Err.Err?) local ok, e = check(img) if not ok then @@ -650,9 +606,6 @@ function ImageOps.coalesce(img: Image): ({ Image }?, Err.Err?) return out, nil end --- fromFrames(images, delays?) -> an animated Image: img.frames = the images as full-canvas frames (x = y = 0, --- dispose "none", blend "source"), img.pixels = a copy of frame 1. delays: a number (every frame) or { seconds } --- (default 0.1). All images need the same size and format. function ImageOps.fromFrames(images: { Image }, delays: (number | { number })?): (Image?, Err.Err?) if type(images) ~= "table" or #images == 0 then return arg("fromFrames: a non-empty list of Images")