Wave distortion¶
| Fact | Value |
|---|---|
| Also called | Sine wave warp, Waves |
| Status | Implemented and tested |
| Family | Artistic and geometric effects |
| Engine | Browser |
| Type | Linear, geometric, axis sine displacement |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/artistic/distort.ts, waveFilter |
| Operator code | web/src/core/filters/artistic/distort.ts, waveMapping |
| Channels | RGB |
Overview¶
The wave distortion shifts rows sideways, columns up and down, or both, by a sine wave. Straight lines turn into waves, as in a flag or behind rippled glass. Its rule is PixelNexus's own.
How it works¶
- For each output pixel, shift its x by the sine of its row, its y by the sine of its column, or both.
- Read the source at the shifted position.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(x, y\): The output pixel position in pixels
- \(x_s, y_s\): The source position it reads
- \(A, \lambda, \phi\): The amplitude, the wavelength and the phase
With rows moving sideways, each row reads the source shifted by a sine of its own row number, so vertical lines become waves. With columns moving, the same happens with x and y swapped. With both, both shifts apply. Positions are in pixels, counted from the top-left pixel. This rule is PixelNexus's own. Each output pixel reads the source at the mapped position with bilinear interpolation, the inverse mapping of Gonzalez and Woods, so no holes appear.
Moving each row by its own amount keeps every row intact but bends the columns. The shift has the shape of the sine wave itself, so a vertical line becomes one full wave every wavelength. With both directions, the two shifts combine into a grid of bumps.
Kernel or operator¶
The Inspector shows the mapping with the current settings, then the bilinear read of the source.
Algorithm¶
- For each output pixel, shift its x by the sine of its row, its y by the sine of its column, or both.
- Read the source there with bilinear interpolation, using the border mode outside the image.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Amplitude amplitude |
6 | 0 to 50 px | How far rows or columns move. |
Wavelength wavelength |
40 | 2 to 400 px | The distance between two wave crests. |
Phase phase |
0 | 0 to 360 deg | The phase of the sine wave. |
Direction direction |
Rows move sideways | Rows move sideways, Columns move up and down, Both | Which way the pixels move. |
Border mode border |
Reflect 101 (default) | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Implementation¶
waveFilter in web/src/core/filters/artistic/distort.ts. It reads the source with sampleBilinear through warpPlane. The filter is global, and it sets movesAlpha, so the runner moves the alpha plane with the same mapping and transparency stays with its colors.
Worked example¶
| Row y | 0 | 10 | 20 | 30 | 40 |
|---|---|---|---|---|---|
| Shift of the row | 0 | 6 | 0 | -6 | 0 |
How far each row reads to the side, with amplitude 6 and wavelength 40. The shift follows one full sine wave every 40 rows.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels. Each output pixel reads four source pixels.
Artifacts and limitations¶
Limitations
- A single sine wave. Real water has many waves at once.
Artifacts
- Edge pixels from the border fill along the sides that move inward.
Tests¶
The reference computes the same mapping with NumPy and reads the source with scipy.ndimage.map_coordinates at order 1, an independent bilinear sampler, for several settings and border modes. Unit tests check that amplitude 0 changes nothing, and that alpha moves with the colors on a transparent image.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/artistic/artistic.test.ts
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 2, geometric transformations and image interpolation.