Skip to content

Free rotation

Fact Value
Also called Rotate by angle, Arbitrary rotation
Status Implemented and tested
Family Utilities
Engine Browser
Type Linear, geometric, inverse mapping with interpolation
Output Image
Operator shown as algorithm (exact)
Support radius the whole image
Code web/src/core/image/resample.ts, rotateImage
Operator code web/src/core/image/resample.ts, rotateImage

Overview

Free rotation turns the image by any angle around its center. Open it with Image > Rotate by angle. Positive angles turn counterclockwise. The image can grow to hold the turned corners, or keep its size and crop them. The new corners are transparent, white or black.

Three close-ups of a checkerboard turned by 30 degrees. With nearest neighbor the square edges are jagged steps. With bilinear and bicubic the edges are smooth, and bicubic is a little crisper. Three close-ups of a checkerboard turned by 30 degrees. With nearest neighbor the square edges are jagged steps. With bilinear and bicubic the edges are smooth, and bicubic is a little crisper.
Nearest neighbor leaves jagged edges. Bilinear and bicubic read between pixels and smooth them.

How it works

  1. For each new pixel, turn its position back by the angle into the old image.
  2. That spot usually falls between old pixels.
  3. Read a value there: copy the nearest pixel, or mix the 4 or 16 pixels around it.
  4. Where the spot falls outside the old image, the new pixel is transparent, then gets the fill color.

Mathematical foundation

\[ \begin{gathered} \begin{pmatrix} u \\ v \end{pmatrix} = \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix} \begin{pmatrix} x + \tfrac{1}{2} - \tfrac{W'}{2} \\ y + \tfrac{1}{2} - \tfrac{H'}{2} \end{pmatrix} + \begin{pmatrix} \tfrac{W}{2} - \tfrac{1}{2} \\ \tfrac{H}{2} - \tfrac{1}{2} \end{pmatrix}, \qquad \theta = 15^{\circ} \end{gathered} \]

The formula shows the default parameter values.

where

  • \((x, y)\): Output pixel index
  • \((u, v)\): Source position in pixel-center units
  • \(\theta\): Rotation angle, counterclockwise
  • \(W, H\): Source width and height
  • \(W', H'\): Output width and height

For each output pixel, the center is turned back by the angle into the source image. The value at that source position (u, v) is read by the chosen interpolation. Turning back, not forward, gives every output pixel exactly one value and leaves no holes. With y growing downward, this matrix turns the picture counterclockwise on screen.

This is inverse mapping. Mapping forward, from source to output, would leave holes between the turned pixels and pile several pixels onto others. Mapping back gives every output pixel exactly one value.

Bilinear interpolation at the source position \((u, v)\) mixes the 4 surrounding pixels with the weights \((1 - \alpha)(1 - \beta)\), \(\alpha (1 - \beta)\), \((1 - \alpha)\beta\) and \(\alpha \beta\), where \(\alpha\) and \(\beta\) are the fractional parts of \(u\) and \(v\). Bicubic interpolation mixes the 16 surrounding pixels with the product of two Keys kernels, as in Resize. Nearest neighbor takes the pixel whose center is closest.

Colors are multiplied by alpha before mixing. Outside the image the premultiplied values are 0, so the edge of the turned image fades into the transparent corners without a dark fringe.

Kernel or operator

Free rotation has no kernel of its own. It reads values with the interpolation kernels shown on the Resize page, without the stretch, because rotation does not change the pixel spacing.

Algorithm

  1. Multiply the colors by alpha.
  2. Find the output size, grown to fit the turned corners or kept.
  3. For every output pixel, turn its center back by the angle into the source.
  4. Read the value there, with nearest, bilinear or bicubic interpolation. Outside the source the value is 0.
  5. Lay the fill color under the result, divide the colors by alpha and round to 8 bits.

Parameters

Parameter Default Values Meaning
Angle angle 15 -180 to 180 deg The turn in degrees. Positive turns counterclockwise.
Interpolation interpolation Bicubic Nearest neighbor, Bilinear, Bicubic How a value is read between source pixels.
Canvas canvas Grow to fit Grow to fit, Keep the size Whether the image grows to hold the whole rotated picture.
New corners fill Transparent Transparent, White, Black The color of the area that the rotated image does not cover.

Implementation

rotateImage in web/src/core/image/resample.ts computes the output size, maps each output center back and samples there in floating point. The output size that holds the whole image is \(\lceil W |\cos\theta| + H |\sin\theta| \rceil\) by \(\lceil W |\sin\theta| + H |\cos\theta| \rceil\), so turns by 90 degrees keep the exact size. The selection turns with the image, and its new corners are always unselected.

Like Resize, free rotation changes the image size, so its catalog entry names the command image.rotate-free instead of running in the filter engine.

Worked example

For an 8 x 8 image turned by 30 degrees with the size kept, output pixel (5, 2) has its center at (5.5, 2.5). Relative to the image center (4, 4) that is (1.5, -1.5). Turned back by 30 degrees and moved to pixel-center units, it lands at u = 5.549, v = 2.951 in the source. Bilinear interpolation then mixes source pixels (5, 2) to (6, 3) with weights from the fractions 0.549 and 0.951.

Complexity

  • Time: \(O(W'\,H'\,t)\)
  • Memory: \(O(W\,H + W'\,H')\)

t is the number of source pixels read per output pixel. 1 for nearest, 4 for bilinear and 16 for bicubic.

Artifacts and limitations

Limitations

  • Every turn that is not a multiple of 90 degrees softens the image a little, because values are read between pixels.
  • Turning several times in a row adds up the softening. Turn once by the total angle instead.

Artifacts

  • Jagged edges with nearest neighbor.
  • Slight softening with bilinear, and thin halos at sharp edges with bicubic.

Tests

The reference is a NumPy version of the same definitions in tools/fixtures_geometry.py. The fixture script checks its bilinear and nearest results against scipy.ndimage.map_coordinates with orders 1 and 0 and zero padding. The test compares six rotations, with every interpolation, both canvas options and three fills, within 1 level. Other tests check exact quarter turns and the fill colors.

References