Manuscript Documentation
v2026.12.0

@std/image/image.ms#

Import each required symbol with import Name from "@std/image/image.ms".

Chroma Enum#

Chroma resolution of an encoded JPEG.

Manuscript
enum Chroma {
    Full
    Quarter
}

Chroma.Full#

4:4:4, no subsampling.

Manuscript
Full

Chroma.Quarter#

4:2:0, chroma at half width and height (smaller files).

Manuscript
Quarter

ColorMode Enum#

The channels stored for each pixel.

Manuscript
enum ColorMode {
    Gray
    GrayAlpha
    Rgb
    Rgba
}

ColorMode.Gray#

One luminance byte per pixel, 0 black to 255 white.

Manuscript
Gray

ColorMode.GrayAlpha#

Two bytes per pixel: luminance, then alpha (0 transparent to 255 opaque).

Manuscript
GrayAlpha

ColorMode.Rgb#

Three bytes per pixel: red, green, blue, each 0 to 255.

Manuscript
Rgb

ColorMode.Rgba#

Red, green, blue, then alpha (not premultiplied).

Manuscript
Rgba

ColorMode.channels#

Returns the number of byte channels per pixel in this color mode.

Manuscript
fn channels() Int

Image Type#

A width-by-height raster. Build one with new_image, decode_image, or the transforms below. Example: img := new_image(2, 2, ColorMode.Rgb).get(); img.pixel(0, 0) is opaque black.

Manuscript
type Image {
    width Int
    height Int
    mode ColorMode
    pixels List[UInt8]
}

Image.height#

Rows, at least 1.

Manuscript
height Int

Image.mode#

The channel layout of pixels.

Manuscript
mode ColorMode

Image.pixels#

width * height * channels bytes, row-major.

Manuscript
pixels List[UInt8]

Image.width#

Columns, at least 1.

Manuscript
width Int

Image.channel_count#

Returns the number of byte channels per pixel.

Manuscript
fn channel_count() Int

Image.convert#

Returns the image converted to target. Gray from color uses Rec. 601 luma, gray expands by repeating its value into red, green and blue, and missing alpha becomes opaque. Dropping alpha keeps the stored color and does not composite it.

Manuscript
fn convert(target ColorMode) Image

Image.crop#

Returns a copy of the pixels inside area. An area that is empty, has a negative corner or leaves the image returns ERANGE.

Manuscript
fn crop(area Rect) Result[Image, Error]

Image.encode#

Encodes this image as format. JPEG uses the default JpegOptions; call encode_jpeg to set quality or chroma. JPEG and PPM cannot store alpha and return EINVAL for GrayAlpha and Rgba images; convert them first.

Manuscript
fn encode(format ImageFormat) Result[Bytes, Error]

Image.encode_jpeg#

Encodes baseline JPEG: gray stays gray, and Rgb uses YCbCr with the chosen chroma resolution. Alpha images, a quality outside 1 through 100, and a restart_interval outside 0 through 65535 return EINVAL; convert or composite alpha images first. A width or height above 65535 returns EOVERFLOW.

Manuscript
fn encode_jpeg(options JpegOptions = JpegOptions()) Result[Bytes, Error]

Image.flip_horizontal#

Returns the image mirrored left to right.

Manuscript
fn flip_horizontal() Image

Image.flip_vertical#

Returns the image mirrored top to bottom.

Manuscript
fn flip_vertical() Image

Image.pixel#

Returns the pixel at (x, y) as RGBA, or null outside the image. Gray modes repeat the luminance in red, green and blue, and modes without alpha report 255.

Manuscript
fn pixel(x Int, y Int) Rgba?

Image.resize#

Scales to width by height with method and returns a new image. A non-positive size returns EINVAL, as new_image does, and a pixel count past max_pixels or the 2^30 hard ceiling returns EOVERFLOW.

Manuscript
fn resize(width Int, height Int, method Resampling = Resampling.Bilinear, max_pixels Int? = null) Result[Image, Error]

Image.rotate180#

Returns the image rotated a half turn.

Manuscript
fn rotate180() Image

Image.rotate270#

Returns the image rotated three quarter turns clockwise, which is one quarter turn counter-clockwise; the result is height wide and width tall.

Manuscript
fn rotate270() Image

Image.rotate90#

Returns the image rotated a quarter turn clockwise; the result is height wide and width tall.

Manuscript
fn rotate90() Image

Image.set_pixel#

Writes color at (x, y), converting to the image’s mode (gray modes store Rec. 601 luma of the color). Returns false, and changes nothing, outside the image.

Manuscript
var fn set_pixel(x Int, y Int, color Rgba) Bool

ImageFormat Enum#

The file formats decode_image recognizes and Image.encode writes.

Manuscript
enum ImageFormat {
    Jpeg
    Png
    Ppm
    Bmp
}

ImageFormat.Bmp#

Windows bitmap: 1, 4, 8, 24 and 32-bit uncompressed decode; 24-bit (or 32-bit with alpha) encode.

Manuscript
Bmp

ImageFormat.Jpeg#

JPEG: baseline and progressive decode (grayscale, YCbCr, CMYK/YCCK), baseline encode.

Manuscript
Jpeg

ImageFormat.Png#

PNG: every color type and bit depth, Adam7 interlacing; 16-bit samples keep their high byte.

Manuscript
Png

ImageFormat.Ppm#

Netpbm binary P5 (gray) and P6 (RGB); decode scales any maxval up to 65535 to 8 bits, encode writes 8-bit.

Manuscript
Ppm

JpegOptions Type#

Settings for Image.encode_jpeg.

Manuscript
type JpegOptions {
    quality Int = 85
    chroma Chroma = Chroma.Quarter
    restart_interval Int = 0
}

JpegOptions.chroma#

Chroma resolution for color images; gray images ignore it.

Manuscript
chroma Chroma = Chroma.Quarter

JpegOptions.quality#

1 (smallest file) to 100 (best quality); outside that range Image.encode_jpeg returns EINVAL.

Manuscript
quality Int = 85

JpegOptions.restart_interval#

MCUs (minimum coded units, the blocks the encoder processes together) between restart markers, 0 through 65535, or 0 for none. Markers let readers recover from damage.

Manuscript
restart_interval Int = 0

Rect Type#

A rectangle in pixels: top-left corner and size.

Manuscript
type Rect {
    x Int
    y Int
    width Int
    height Int
}

Rect.height#

Rows covered; at least 1.

Manuscript
height Int

Rect.width#

Columns covered; at least 1.

Manuscript
width Int

Rect.x#

Column of the left edge, counted from 0.

Manuscript
x Int

Rect.y#

Row of the top edge, counted from 0.

Manuscript
y Int

Resampling Enum#

How resize samples the source.

Manuscript
enum Resampling {
    Nearest
    Bilinear
}

Resampling.Bilinear#

Weighted average of the four nearest pixels.

Manuscript
Bilinear

Resampling.Nearest#

Fastest; keeps hard edges.

Manuscript
Nearest

Rgba Type#

One RGBA sample, each channel 0 to 255.

Manuscript
type Rgba {
    red Int
    green Int
    blue Int
    alpha Int = 255
}

Rgba.alpha#

Opacity, 0 transparent to 255 opaque (the default).

Manuscript
alpha Int = 255

Rgba.blue#

Blue level, 0 to 255; writes clamp.

Manuscript
blue Int

Rgba.green#

Green level, 0 to 255; writes clamp.

Manuscript
green Int

Rgba.red#

Red level, 0 to 255; writes clamp.

Manuscript
red Int

decode_image Function#

Decodes any supported format, detected from the leading bytes with detect_image_format; an unrecognized format returns ENOTSUP. max_pixels rejects larger images with EOVERFLOW before any allocation.

Manuscript
fn decode_image(data Bytes, max_pixels Int? = null) Result[Image, Error] { ... }

detect_image_format Function#

Names the format of data from its leading bytes, or null when it is not JPEG, PNG, BMP or binary PPM/PGM (P5 or P6).

Manuscript
fn detect_image_format(data Bytes) ImageFormat? { ... }

new_image Function#

Creates an opaque black image: alpha modes are fully opaque. A non-positive size returns EINVAL, and a pixel count past max_pixels or the 2^30 hard ceiling returns EOVERFLOW.

Manuscript
fn new_image(width Int, height Int, mode ColorMode, max_pixels Int? = null) Result[Image, Error] { ... }