Rotate and flip¶
| Fact | Value |
|---|---|
| Also called | Quarter turn, Mirror, Flip horizontal, Flip vertical |
| Status | Implemented and tested |
| Family | Utilities |
| Engine | Browser |
| Type | Linear, geometric, pixel permutation |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | 0 |
| Code | web/src/core/image/transform.ts, rotate |
| Operator code | web/src/core/image/transform.ts, rotate |
Overview¶
Rotate and flip turns the image by 90 or 180 degrees, or mirrors it. Open it from Image > Rotate and flip, or use the five commands of the Image menu directly. Whole pixels move to new places, so no value changes and nothing is lost. Four quarter turns give the image back exactly. The selection moves with the image.
How it works¶
- Find the new place of each pixel.
- Copy the pixel there, as it is.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(W, H\): The width and height of the input
- \(I'\): The output image
W and H are the width and height of the input. A clockwise quarter turn makes an image H pixels wide and W high. Its pixel x, y comes from column y and row H - 1 - x of the input. A horizontal flip takes column W - 1 - x. The other turns and the vertical flip follow the same pattern.
The quarter turns and the mirrors are permutations of the pixel grid. With the half turn and the two diagonal mirrors, which are a quarter turn followed by a flip, they form the eight symmetries of a square. Any angle that is not a multiple of 90 degrees moves pixels between grid points, so it needs resampling. That is Rotate by angle.
Kernel or operator¶
There is no kernel. Each output pixel is one input pixel.
Algorithm¶
- Create the output, with width and height swapped for a quarter turn.
- Copy each pixel to its new place, as one 32-bit word.
- Move the selection the same way.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Operation operation |
Rotate 90 degrees clockwise | Rotate 90 degrees clockwise, Rotate 90 degrees counterclockwise, Rotate 180 degrees, Flip horizontal (left to right), Flip vertical (top to bottom) | Which turn or mirror to apply. |
Implementation¶
rotate and flip in web/src/core/image/transform.ts copy each pixel as one 32-bit word. The dialog runs the matching command of the Image menu, which also moves the selection and records the step in the history.
Worked example¶
An image of 3 x 2 pixels whose red values number the pixels:
The clockwise turn makes the image 2 pixels wide and 3 high. Its first row is the first column of the input, read from the bottom up.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels.
Artifacts and limitations¶
Limitations
- Only steps of 90 degrees. Other angles need Rotate by angle, which resamples.
Artifacts
- None.
Tests¶
The unit tests check every turn and both mirrors on the example above, with hand-computed results.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018.