7.8.3. STBIMAGE-03 — Image Transforms

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

7.8.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.

7.8.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()

7.8.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)

7.8.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. 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)

7.8.3.5. Creating Blank Canvases

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

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