8.9.3. STBIMAGE-03 — Image Transforms

This tutorial covers resizing, flipping, cropping, and compositing images.

8.9.3.1. Resizing

Image.resize(w, h) returns a new resized image using the default filter. You can also specify a filter from stbir_filter:

var inscope small <- img.resize(16, 16)
var inscope sharp <- img.resize(128, 128, stbir_filter.STBIR_FILTER_POINT_SAMPLE)
var inscope smooth <- img.resize(128, 128, stbir_filter.STBIR_FILTER_MITCHELL)

Available filters: STBIR_FILTER_DEFAULT, STBIR_FILTER_BOX, STBIR_FILTER_TRIANGLE, STBIR_FILTER_CUBICBSPLINE, STBIR_FILTER_CATMULLROM, STBIR_FILTER_MITCHELL, STBIR_FILTER_POINT_SAMPLE.

8.9.3.2. Flipping

flip_vertical() and flip_horizontal() return new flipped images. The original is not modified:

var inscope flipped_v <- img.flip_vertical()
var inscope flipped_h <- img.flip_horizontal()

8.9.3.3. Cropping

Image.crop(x, y, w, h) extracts a rectangular sub-region. Coordinates are clamped to image bounds:

var inscope cropped <- img.crop(24, 24, 16, 16)
// Requesting beyond bounds is safe — the result is clamped
var inscope clamped <- img.crop(50, 50, 32, 32)

8.9.3.4. Blitting

Image.blit(source, x, y) copies source pixels onto the image at position (x, y). Both images must have the same number of channels and the same bpc. Pixels outside the destination bounds are clipped:

var inscope canvas <- make_solid(64, 64, ...)
var inscope overlay <- make_solid(16, 16, ...)
canvas.blit(overlay, 8, 8)

8.9.3.5. Creating Blank Canvases

make_image(width, height, channels, bpc) creates a zero-filled image. bpc defaults to 1 (uint8):

let rgba = make_image(100, 100, 4)       // uint8 RGBA
let hdr = make_image(100, 100, 3, 4)     // float RGB
let grey = make_image(100, 100, 1)       // uint8 greyscale
let img16 = make_image(100, 100, 4, 2)   // uint16 RGBA