gfx.scale

index · gfx

Overview

The smooth scale: a source drawn into a new surface of another size.

A blit's dw, dh scale by nearest neighbour, which drops most of an image it shrinks. scaled averages instead. The caller scales once, keeps the surface, and blits it, or slices of it, with any blend, tint or flip.

When part of the source changes, the caller fills the part of the kept surface it reaches, into and dx, dy, dw, dh. The part holds the bytes the whole scale gives there, so a picture refilled part by part equals the picture scaled whole.

Functions

NameSignature
scaledscaled(src, w, h, opts) -> surface, err

scaled(src, w, h, opts) -> surface, err

Scale a source smoothly into a new surface

src is a canvas, a sprite or a raw surface; the result is a raw surface of wxh, which canvas:blit, gfx.sprite and gfx.sheet take as a source.

opts.sx, sy, sw, sh pick the source rect, as on canvas:blit; left out, it is the source's own. The rect must lie inside the source surface.

Each axis is scaled on its own. An axis that shrinks takes the exact average over each destination pixel's area of the source. One that grows takes the bilinear blend of the two nearest source pixels. No pixel outside the rect is read: an edge pixel stands in past the edge. So a cell of a sheet does not pick up its neighbour's colour.

The surface holds straight alpha, as the source does. A pixel's alpha is the mean of the alphas under it, and its colour the mean of the colours weighted by their alphas. So a clear pixel lends no colour to its neighbours. A pixel whose alpha rounds to 0 is stored as 0, 0, 0, 0. A size equal to the rect's copies the rect, with that one change.

opts.into is a raw surface of wxh to fill instead of a new one, such as one an earlier scaled answered; scaled then answers it. opts.dx, dy, dw, dh name the part of the destination to fill, from 1, as sx, sy, sw, sh name the source's rect; left out, it is the whole. Each pixel of the part is the pixel the whole scale gives there: the weights are the whole mapping's, not the part's own. No pixel outside the part is written. A destination pixel reads the source pixels its area covers on a shrink, and the two nearest on a growth, so a change to a source rect reaches only the destination pixels whose spans meet it.

A w or h that is no integer in 1–32767 raises, and so does a source that is none of the three, or a rect not inside it. So do an into that is no raw surface, one of another size, or the source's own surface, and a part not inside wxh. Nil and an error when the memory cannot be had.