stdimg

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Commands = []CommandSpec{
	{
		Name:        "resize",
		Args:        []ArgSpec{{"width", "int", true, "", "output width"}, {"height", "int", true, "", "output height"}},
		Usage:       "resize <width> <height>",
		Description: "Resize image using Lanczos resampling (a=3).",
	},
	{
		Name:        "rotate",
		Args:        []ArgSpec{{"degrees", "float", true, "", "rotation degrees"}},
		Usage:       "rotate <degrees>",
		Description: "Rotate image using inverse mapping with bilinear sampling.",
	},
	{
		Name:        "blur",
		Args:        []ArgSpec{{"sigma", "float", true, "", "gaussian sigma"}},
		Usage:       "blur <sigma>",
		Description: "Separable Gaussian blur.",
	},
	{
		Name:        "medianFilter",
		Args:        []ArgSpec{{"radius", "int", true, "", "median radius"}},
		Usage:       "medianFilter <radius>",
		Description: "Median filter (sliding-window histogram).",
	},
	{
		Name:        "despeckle",
		Args:        []ArgSpec{{"radius", "int", false, "1", "optional radius"}},
		Usage:       "despeckle [radius]",
		Description: "Despeckle (wrapper around median filter).",
	},
	{
		Name:        "level",
		Args:        []ArgSpec{{"blackPoint", "float", true, "", "black point"}, {"gamma", "float", true, "", "gamma"}, {"whitePoint", "float", true, "", "white point"}},
		Usage:       "level <blackPoint> <gamma> <whitePoint>",
		Description: "Adjust levels (black/gamma/white).",
	},
	{
		Name:        "normalize",
		Args:        []ArgSpec{},
		Usage:       "normalize",
		Description: "Stretch per-channel extremes to full [0,255].",
	},
	{
		Name:        "autoLevel",
		Args:        []ArgSpec{},
		Usage:       "autoLevel",
		Description: "Automatic level normalization.",
	},
	{
		Name:        "autoGamma",
		Args:        []ArgSpec{},
		Usage:       "autoGamma",
		Description: "Automatic gamma correction.",
	},
	{
		Name:        "gamma",
		Args:        []ArgSpec{{"gamma", "float", true, "", "gamma value"}},
		Usage:       "gamma <gamma>",
		Description: "Apply gamma correction.",
	},
	{
		Name:        "negate",
		Args:        []ArgSpec{{"onlyGray", "bool", false, "false", "only invert grayscale pixels"}},
		Usage:       "negate [onlyGray]",
		Description: "Invert colors (optional only-gray).",
	},
	{
		Name:        "threshold",
		Args:        []ArgSpec{{"value", "float", true, "", "threshold value"}, {"perChannel", "bool", false, "false", "apply per-channel"}},
		Usage:       "threshold <value> [perChannel]",
		Description: "Threshold image by value (luminance or per-channel).",
	},
	{
		Name:        "modulate",
		Args:        []ArgSpec{{"brightness", "float", true, "", "brightness percent (e.g., 100)"}, {"saturation", "float", true, "", "saturation percent"}, {"hue", "float", true, "", "hue degrees"}},
		Usage:       "modulate <brightness> <saturation> <hue>",
		Description: "Adjust brightness, saturation and hue.",
	},
	{
		Name:        "vignette",
		Args:        []ArgSpec{{"radius", "float", true, "", "radius"}, {"sigma", "float", true, "", "sigma"}, {"x", "int", true, "", "center x"}, {"y", "int", true, "", "center y"}, {"strength", "float", false, "1.0", "0..1 or percent like 50%"}},
		Usage:       "vignette <radius> <sigma> <x> <y> [strength]",
		Description: "Apply vignette effect centered at (x,y).",
	},
	{
		Name:        "grayscale",
		Args:        []ArgSpec{},
		Usage:       "grayscale",
		Description: "Convert to luminance (Rec.709).",
	},
	{
		Name:        "edge",
		Args:        []ArgSpec{{"sigma", "float", false, "0.0", "pre-blur sigma"}, {"scale", "float", false, "1.0", "edge scale multiplier"}, {"threshold", "float", false, "0.0", "threshold value"}, {"binary", "bool", false, "false", "binary output"}},
		Usage:       "edge [sigma] [scale] [threshold] [binary]",
		Description: "Sobel-based edge detector with options.",
	},
	{
		Name:        "adaptiveBlur",
		Args:        []ArgSpec{{"radius", "float", false, "1.0", "variance neighborhood radius"}, {"sigmaMin", "float", false, "0.5", "min sigma (for high variance)"}, {"sigmaMax", "float", false, "1.0", "max sigma (for low variance)"}, {"levels", "int", false, "6", "discrete levels to precompute"}},
		Usage:       "adaptiveBlur [radius] [sigmaMin] [sigmaMax] [levels]",
		Description: "Variance-driven per-pixel adaptive blur.",
	},

	{
		Name:        "adaptiveResize",
		Args:        []ArgSpec{{"width", "int", false, "0", "target width (0 = preserve aspect)"}, {"height", "int", false, "0", "target height (0 = preserve aspect)"}, {"a", "float", false, "3.0", "Lanczos a parameter (3 recommended)"}},
		Usage:       "adaptiveResize [width] [height] [a]",
		Description: "Resize using Lanczos resampling with aspect-preserve semantics.",
	},
	{
		Name:        "adaptiveSharpen",
		Args:        []ArgSpec{{"radius", "float", false, "0.0", "blur radius (0 = auto)"}, {"sigma", "float", false, "1.0", "sigma for blur"}, {"amount", "float", false, "1.0", "sharpen amount"}},
		Usage:       "adaptiveSharpen [radius] [sigma] [amount]",
		Description: "Sharpen using unsharp-mask (approximation).",
	},
	{
		Name:        "adaptiveThreshold",
		Args:        []ArgSpec{{"window_width", "int", false, "15", "local window width (odd)"}, {"window_height", "int", false, "15", "local window height (odd)"}, {"offset", "float", false, "0.0", "threshold offset (subtract from mean)"}},
		Usage:       "adaptiveThreshold [window_width] [window_height] [offset]",
		Description: "Local threshold using moving window mean (bilevel output).",
	},
	{
		Name:        "addNoise",
		Args:        []ArgSpec{{"type", "enum", false, "GAUSSIAN", "noise type (GAUSSIAN|UNIFORM|POISSON)"}, {"amount", "float", false, "10.0", "noise strength (stddev or range)"}, {"seed", "int", false, "0", "random seed (0 = time-based)"}},
		Usage:       "addNoise [type] [amount] [seed]",
		Description: "Add noise to image; supports GAUSSIAN, UNIFORM (POISSON optional).",
	},
	{
		Name:        "crop",
		Args:        []ArgSpec{{"width", "int", true, "", "crop width"}, {"height", "int", true, "", "crop height"}, {"x", "int", true, "", "x offset"}, {"y", "int", true, "", "y offset"}},
		Usage:       "crop <width> <height> <x> <y>",
		Description: "Crop image (intersected with bounds).",
	},
	{
		Name:        "flip",
		Args:        []ArgSpec{},
		Usage:       "flip",
		Description: "Vertical flip.",
	},
	{
		Name:        "flop",
		Args:        []ArgSpec{},
		Usage:       "flop",
		Description: "Horizontal flip.",
	},
	{
		Name:        "histogram",
		Args:        []ArgSpec{{"bins", "int", false, "256", "number of bins"}},
		Usage:       "histogram [bins]",
		Description: "Render a histogram image (returns image).",
	},
	{
		Name:        "equalize",
		Args:        []ArgSpec{},
		Usage:       "equalize",
		Description: "Equalize histogram per-channel.",
	},
	{
		Name:        "trim",
		Args:        []ArgSpec{{"fuzz", "float_or_percent", true, "", "fuzz numeric or percent (e.g. 5 or 5%)"}},
		Usage:       "trim <fuzz>",
		Description: "Trim borders within fuzz tolerance.",
	},
	{
		Name:        "floodfillPaint",
		Args:        []ArgSpec{{"fillColor", "string", true, "", "CSS color or hex (e.g. #ff0000)"}, {"fuzz", "float_or_percent", true, "", "fuzz as Lab delta-E or percent (e.g. 5 or 50%)"}, {"borderColor", "string", false, "", "CSS color or hex for border or empty string"}, {"x", "int", true, "", "start x"}, {"y", "int", true, "", "start y"}, {"invert", "bool", false, "false", "invert fill region"}},
		Usage:       "floodfillPaint <fillColor> <fuzz> <borderColor> <x> <y> [invert]",
		Description: "Flood-fill region starting at (x,y) using perceptual fuzz (Lab delta-E).",
	},
	{
		Name:        "annotate",
		Args:        []ArgSpec{{"text", "string", true, "", "text to draw"}, {"fontPath", "path_or_empty", false, "", "font path (optional)"}, {"size", "float", true, "", "font size"}, {"x", "int", true, "", "x position"}, {"y", "int", true, "", "y position"}, {"color", "string", true, "", "CSS hex or name (e.g. #ff0000)"}},
		Usage:       "annotate <text> [fontPath] <size> <x> <y> <color>",
		Description: "Draw text; supports 5 or 6 args (font optional).",
	},
	{
		Name:        "composite",
		Args:        []ArgSpec{{"srcImagePath", "path", true, "", "path to source image"}, {"operator", "string", true, "", "compose operator (e.g. OVER)"}, {"x", "int", true, "", "x offset"}, {"y", "int", true, "", "y offset"}},
		Usage:       "composite <srcImagePath> <operator> <x> <y>",
		Description: "Composite an image loaded from disk at offset using operator.",
	},
	{
		Name:        "identify",
		Args:        []ArgSpec{},
		Usage:       "identify",
		Description: "Print width, height and color model; returns nil image.",
	},
	{
		Name:        "strip",
		Args:        []ArgSpec{},
		Usage:       "strip",
		Description: "No-op in memory; metadata handled at encode time.",
	},
	{
		Name:        "sepia",
		Args:        []ArgSpec{{"percentage", "float_or_percent", false, "70%", "sepia intensity (0..100% or 0..1)"}, {"midtoneCenter", "float", false, "50", "midtone center L (0..100)"}, {"midtoneSigma", "float", false, "20", "midtone width (sigma)"}, {"highlightThreshold", "float", false, "80", "L at which protection starts"}, {"highlightSoftness", "float", false, "10", "softness for highlight protection"}, {"curve", "float", false, "0.12", "filmic S-curve strength (0..1)"}},
		Usage:       "sepia [percentage] [midtoneCenter] [midtoneSigma] [highlightThreshold] [highlightSoftness] [curve]",
		Description: "Apply Sepia tone with optional intensity and tonal controls.",
	},
}

Commands is the authoritative list of commands implemented by the stdlib engine. Keep this synchronized with ApplyCommandStdlib in pkg/stdimg/engine.go.

Functions

func AdaptiveBlur

func AdaptiveBlur(src *image.NRGBA, radius, sigma float64) *image.NRGBA

AdaptiveBlur approximates an adaptive blur by blending a blurred image with the original using an edge-based mask. This function remains as a simple approximation and calls AdaptiveBlurPerPixel for a true variance-driven implementation when appropriate.

func AdaptiveBlurPerPixel

func AdaptiveBlurPerPixel(src *image.NRGBA, radius, sigmaMin, sigmaMax float64, levels int) *image.NRGBA

AdaptiveBlurPerPixel implements a variance-driven per-pixel adaptive blur. radius controls the neighborhood used to compute local variance (in pixels). sigmaMin/sigmaMax define the range of Gaussian sigmas to apply (sigmaMin for high-variance regions, sigmaMax for low-variance areas). levels is the number of discrete sigma levels to precompute and is used to approximate per-pixel variable sigma efficiently.

func AdaptiveResize

func AdaptiveResize(src *image.NRGBA, width, height int, a float64) *image.NRGBA

AdaptiveResize resizes src to width x height using ResampleLanczos. If width or height is 0 the aspect ratio is preserved. If both are 0 a clone of the source is returned. The parameter a controls the Lanczos window (commonly 3.0).

func AdaptiveSharpen

func AdaptiveSharpen(src *image.NRGBA, radius, sigma, amount float64) *image.NRGBA

AdaptiveSharpen uses UnsharpMask/Sharpen as an approximation for ImageMagick's adaptive sharpen. Parameters: radius (unused here), sigma (blur sigma) and amount.

func AdaptiveThreshold

func AdaptiveThreshold(src *image.NRGBA, windowW, windowH int, offset float64) *image.NRGBA

AdaptiveThreshold applies a local mean threshold over a window of windowW x windowH. Pixels above mean - offset become white, otherwise black. Returns a bilevel NRGBA image.

func AddNoise

func AddNoise(src *image.NRGBA, typ string, amount float64, seed int64) *image.NRGBA

AddNoise adds noise to src. typ may be "GAUSSIAN", "UNIFORM", or "POISSON". amount controls strength (stddev for gaussian, max deviation for uniform, scale for poisson). seed allows deterministic output for tests (seed==0 uses a fixed seed).

func Annotate

func Annotate(src *image.NRGBA, text string, fontPath string, size float64, x, y int, col color.Color) (*image.NRGBA, error)

Annotate draws text onto the image at given position x,y (pixel coords) with given font size and color. fontPath may be empty to use a built-in basic font. Size is in points (if using TTF), ignored for basic font.

func ApplyCommandStdlib

func ApplyCommandStdlib(img image.Image, commandName string, args []string) (image.Image, error)

ApplyCommandStdlib applies basic commands to an image.NRGBA and returns a new image. It implements a subset of the original ImageMagick-backed commands: resize, rotate, blur, sharpen, crop, flip, flop, grayscale, strip, identify (returns nil image and prints info).

func AutoGamma

func AutoGamma(src *image.NRGBA) *image.NRGBA

AutoGamma estimates a gamma to map the average luminance to 0.5 and applies it.

func AutoLevel

func AutoLevel(src *image.NRGBA) *image.NRGBA

AutoLevel: convenience wrapper for Normalize

func AutoOrient

func AutoOrient(img image.Image, orientation int) image.Image

AutoOrient applies EXIF orientation to an image.Image and returns a new image.Image. orientation follows EXIF spec (1..8). If orientation is 1 or unknown, the image is returned as-is.

func CloneNRGBA

func CloneNRGBA(src *image.NRGBA) *image.NRGBA

CloneNRGBA returns a copy of the provided image.NRGBA

func Composite

func Composite(dst *image.NRGBA, src image.Image, op string, xoff, yoff int) *image.NRGBA

Composite overlays src onto dst at offset (xoff,yoff) using operator op. dst is modified in place and returned.

func ComputeHistogram

func ComputeHistogram(src *image.NRGBA, bins int) ([]int, []int, []int)

ComputeHistogram computes per-channel histograms with `bins` bins (e.g., 256). Returns three slices for R, G, B counts.

func Despeckle

func Despeckle(src *image.NRGBA, radius int) *image.NRGBA

Despeckle removes small speckles; simple wrapper around MedianFilter with a small radius.

func Edge

func Edge(src *image.NRGBA, scale float64) *image.NRGBA

Edge is a compatibility wrapper using EdgeEx with defaults (no pre-blur, scale applied directly).

func EdgeEx

func EdgeEx(src *image.NRGBA, sigma, scale, threshold float64, binary bool) *image.NRGBA

Edge applies a simple Sobel edge detector and returns a grayscale image representing edge magnitude. This improved version supports optional pre-blur (sigma), scale multiplier, thresholding, and binary output.

func Equalize

func Equalize(src *image.NRGBA) *image.NRGBA

Equalize performs histogram equalization per channel and returns a new image.

func FlipNRGBA

func FlipNRGBA(src *image.NRGBA) *image.NRGBA

helper transforms operating on *image.NRGBA

func FloodfillPaint added in v0.1.0

func FloodfillPaint(src *image.NRGBA, fillColor color.NRGBA, fuzz float64, borderColor color.NRGBA, x, y int, invert bool) *image.NRGBA

FloodfillPaint fills a region starting at (x,y) with fillColor using a fuzz tolerance. Optimized for large images by using a bitset for the mask (1 bit per pixel). If borderColor has non-zero alpha or non-zero RGB, it is treated as a boundary that cannot be crossed (within fuzz). If invert is true, fill the inverse of the matched region.

func FlopNRGBA

func FlopNRGBA(src *image.NRGBA) *image.NRGBA

func Gamma

func Gamma(src *image.NRGBA, gamma float64) *image.NRGBA

Gamma applies per-channel gamma correction (gamma>0). gamma==1 -> no-op

func Level

func Level(src *image.NRGBA, blackPoint, gamma, whitePoint float64) *image.NRGBA

Level applies levels adjustment. blackPoint and whitePoint are in [0..255] range, gamma is a multiplier (ImageMagick uses gamma as midtone adjustment). We'll implement mapping: normalized = (v - black)/(white - black) clamped to [0,1]; if gamma != 0, apply pow(normalized, 1.0/gamma).

func MedianFilter

func MedianFilter(src *image.NRGBA, radius int) *image.NRGBA

MedianFilter applies a median filter with given radius (window radius). radius==1 -> 3x3 window. Uses a sliding-window histogram per row for O(w*h*256) worst-case but amortized much faster than per-pixel sorting for moderate radii.

func Modulate

func Modulate(src *image.NRGBA, brightnessPct, saturationPct, hueDegrees float64) *image.NRGBA

Modulate adjusts brightness (percent), saturation (percent), and hue (degrees). brightness and saturation are given as percentages where 100 means unchanged. hue is in degrees and will be added to the hue channel.

func Negate

func Negate(src *image.NRGBA, onlyGray bool) *image.NRGBA

Negate inverts colors; if onlyGray true, invert only luminance and keep color channels mapped accordingly.

func Normalize

func Normalize(src *image.NRGBA) *image.NRGBA

Normalize stretches per-channel extremes to full [0,255] range.

func Posterize

func Posterize(src *image.NRGBA, levels int) *image.NRGBA

Posterize reduces color levels per channel to `levels`.

func RenderHistogramImage

func RenderHistogramImage(histR, histG, histB []int, width, height int) *image.NRGBA

RenderHistogramImage renders a simple overlaid histogram image for the given histograms. histR/G/B slices length == bins. width/height choose the output image size.

func ResampleLanczos

func ResampleLanczos(src *image.NRGBA, dstW, dstH int, a float64) *image.NRGBA

ResampleLanczos resamples src to dstW x dstH using Lanczos with window a (commonly 3).

func Rotate180NRGBA

func Rotate180NRGBA(src *image.NRGBA) *image.NRGBA

func Rotate90CCWNRGBA

func Rotate90CCWNRGBA(src *image.NRGBA) *image.NRGBA

func Rotate90CWNRGBA

func Rotate90CWNRGBA(src *image.NRGBA) *image.NRGBA

func SeparableGaussianBlur

func SeparableGaussianBlur(src *image.NRGBA, sigma float64) *image.NRGBA

SeparableGaussianBlur applies a separable gaussian blur to src and returns a new *image.NRGBA

func SepiaTone added in v0.2.0

func SepiaTone(src *image.NRGBA, percentage, midtoneCenter, midtoneSigma, highlightThreshold, highlightSoftness, curve float64) *image.NRGBA

SepiaTone applies a sepia color transform to src using Lab-space blending. percentage is in 0..1 where 1.0 is full sepia (target color) and 0.0 returns the original image. It supports midtone weighting, highlight protection and an optional small filmic S-curve on L. Target sepia color chosen: #704214 (a warm mid-brown).

func Sharpen

func Sharpen(src *image.NRGBA, radius float64, sigma float64) *image.NRGBA

Sharpen is a convenience wrapper using UnsharpMask. It accepts radius and sigma and uses amount=1.0

func Threshold

func Threshold(src *image.NRGBA, thresh float64, perChannel bool) *image.NRGBA

Threshold applies a binary threshold on luminance (if perChannel false) or per-channel (if true).

func ToNRGBA

func ToNRGBA(src image.Image) *image.NRGBA

ToNRGBA converts any image.Image to *image.NRGBA (non-premultiplied RGBA).

func Trim

func Trim(src *image.NRGBA, fuzz float64) *image.NRGBA

Trim removes uniform border regions matching the top-left pixel color within a fuzz tolerance. fuzz is an absolute color distance on 0..255 scale (Euclidean distance across RGB).

func UnsharpMask

func UnsharpMask(src *image.NRGBA, radius float64, sigma float64, amount float64, threshold float64) *image.NRGBA

UnsharpMask applies an unsharp mask to src. radius is unused (kept for API parity), sigma controls the gaussian blur, amount is multiplier, threshold is ignored if <=0.

func Vignette

func Vignette(src *image.NRGBA, radius, sigma float64, cx, cy int, strength float64) *image.NRGBA

Vignette applies a radial darkening centered at (cx,cy). radius specifies the maximum radius (in pixels) where effect reaches full strength. sigma controls the smoothness (gaussian falloff). If radius<=0, it's set to half the image diagonal. strength controls maximum darkening where 1.0 means full darkening (mask effect fully applied), 0.0 means no effect. Values outside [0,1] are clamped. The implementation darkens pixels by multiplying RGB by (1 - mask*strength) where mask in [0,1]. The mask is continuous: mask(d) = (1 - exp(-0.5*(d^2)/(sigma^2))) / (1 - exp(-0.5*(radius^2)/(sigma^2))). This yields mask(0)=0 and mask(radius)=1 (normalized), and clamps to [0,1] for d>radius.

Types

type ArgSpec

type ArgSpec struct {
	Name        string // human name
	Type        string // "int", "float", "bool", "string", "path", etc.
	Required    bool
	Default     string // textual default (for help only)
	Description string
}

ArgSpec describes a single argument for a command. Fields are textual and intended for help/validation UI rather than machine-enforced typing.

type CommandSpec

type CommandSpec struct {
	Name        string
	Args        []ArgSpec
	Usage       string // short usage string
	Description string // brief description
}

CommandSpec defines a single command and its expected arguments.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL