@std/image/image.ms#
Import each required symbol with import Name from "@std/image/image.ms".
Chroma Enum#
Chroma resolution of an encoded JPEG.
enum Chroma {
Full
Quarter
}Chroma.Full#
4:4:4, no subsampling.
FullChroma.Quarter#
4:2:0, chroma at half width and height (smaller files).
QuarterColorMode Enum#
The channels stored for each pixel.
enum ColorMode {
Gray
GrayAlpha
Rgb
Rgba
}ColorMode.Gray#
One luminance byte per pixel, 0 black to 255 white.
GrayColorMode.GrayAlpha#
Two bytes per pixel: luminance, then alpha (0 transparent to 255 opaque).
GrayAlphaColorMode.Rgb#
Three bytes per pixel: red, green, blue, each 0 to 255.
RgbColorMode.Rgba#
Red, green, blue, then alpha (not premultiplied).
RgbaColorMode.channels#
Returns the number of byte channels per pixel in this color mode.
fn channels() IntImage 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.
type Image {
width Int
height Int
mode ColorMode
pixels List[UInt8]
}Image.height#
Rows, at least 1.
height IntImage.mode#
The channel layout of pixels.
mode ColorModeImage.pixels#
width * height * channels bytes, row-major.
pixels List[UInt8]Image.width#
Columns, at least 1.
width IntImage.channel_count#
Returns the number of byte channels per pixel.
fn channel_count() IntImage.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.
fn convert(target ColorMode) ImageImage.crop#
Returns a copy of the pixels inside area. An area that is empty, has a negative
corner or leaves the image returns ERANGE.
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.
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.
fn encode_jpeg(options JpegOptions = JpegOptions()) Result[Bytes, Error]Image.flip_horizontal#
Returns the image mirrored left to right.
fn flip_horizontal() ImageImage.flip_vertical#
Returns the image mirrored top to bottom.
fn flip_vertical() ImageImage.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.
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.
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.
fn rotate180() ImageImage.rotate270#
Returns the image rotated three quarter turns clockwise, which is one quarter turn
counter-clockwise; the result is height wide and width tall.
fn rotate270() ImageImage.rotate90#
Returns the image rotated a quarter turn clockwise; the result is height wide and
width tall.
fn rotate90() ImageImage.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.
var fn set_pixel(x Int, y Int, color Rgba) BoolImageFormat Enum#
The file formats decode_image recognizes and Image.encode writes.
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.
BmpImageFormat.Jpeg#
JPEG: baseline and progressive decode (grayscale, YCbCr, CMYK/YCCK), baseline encode.
JpegImageFormat.Png#
PNG: every color type and bit depth, Adam7 interlacing; 16-bit samples keep their high byte.
PngImageFormat.Ppm#
Netpbm binary P5 (gray) and P6 (RGB); decode scales any maxval up to 65535 to 8 bits, encode writes 8-bit.
PpmJpegOptions Type#
Settings for Image.encode_jpeg.
type JpegOptions {
quality Int = 85
chroma Chroma = Chroma.Quarter
restart_interval Int = 0
}JpegOptions.chroma#
Chroma resolution for color images; gray images ignore it.
chroma Chroma = Chroma.QuarterJpegOptions.quality#
1 (smallest file) to 100 (best quality); outside that range Image.encode_jpeg
returns EINVAL.
quality Int = 85JpegOptions.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.
restart_interval Int = 0Rect Type#
A rectangle in pixels: top-left corner and size.
type Rect {
x Int
y Int
width Int
height Int
}Rect.height#
Rows covered; at least 1.
height IntRect.width#
Columns covered; at least 1.
width IntRect.x#
Column of the left edge, counted from 0.
x IntRect.y#
Row of the top edge, counted from 0.
y IntResampling Enum#
How resize samples the source.
enum Resampling {
Nearest
Bilinear
}Resampling.Bilinear#
Weighted average of the four nearest pixels.
BilinearResampling.Nearest#
Fastest; keeps hard edges.
NearestRgba Type#
One RGBA sample, each channel 0 to 255.
type Rgba {
red Int
green Int
blue Int
alpha Int = 255
}Rgba.alpha#
Opacity, 0 transparent to 255 opaque (the default).
alpha Int = 255Rgba.blue#
Blue level, 0 to 255; writes clamp.
blue IntRgba.green#
Green level, 0 to 255; writes clamp.
green IntRgba.red#
Red level, 0 to 255; writes clamp.
red Intdecode_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.
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).
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.
fn new_image(width Int, height Int, mode ColorMode, max_pixels Int? = null) Result[Image, Error] { ... }