Introduction
Our favorite free image editor, GIMP already comes with a ton of built-in filters, but today we will be taking a look at an incredibly useful filter called Cartesian Transform that is only available when you install the free filter suite, GREYC’S Magic for Image Computing, also known simply as G’MIC. In order to use G’MIC as easily as possible, you should have GIMP installed as well. This allows you to use the plug-in version called GMIC-qt that works using a graphical interface rather than a command line prompt. This is more of an information repository related to the Cartesian Transform filter than it is an actual tutorial, but the filter is capable of many different types of warps, distortions, translations, and perspective effects that are useful for making old school graphics and artwork.
The Cartesian Transform filter is a very specific type of filter that uses mathematical expressions to perform ‘pixel operations‘ in order to warp or displace an image. Once you have G’MIC installed, you can find it under Filters at the very bottom of the drop-down menu using the name G’MIC-qt. Type Cartesian Transform into the filter search bar to bring up the filter we are going to be using. You can also find it by expanding the Deformations category in the filter list. By default, whenever you first open the Cartesian Transform filter, there will be a simple example expression showing how the filter can be used to produce a wavy distortion.
Looking at the example above, you can see a basic expression entered into both the X-Warping and Y-Warping fields of the filter settings. You can also see that the Relative Warping box has been checked. By ticking or unticking this box, you can produce different types of effects, as the math expressions will be applied in different ways. If the box is unticked, operations on the pixels will only take into account their original pixel coordinates in the image, while ticking the box means the defined warp is to be performed relative to any pixels that have been warped or displaced before the current step. Feel free to experiment with ticking and unticking the box to get different results. Really quickly, let’s break down that expression:
- (w+h)/20 – The first part of each expression is essentially adding the width and the height of the image (w+h) and dividing the result by 20.
- cos(y*20/h) – Applies a cosine function to the y coordinate of each pixel. The y coordinate is multiplied by (20/h) to control the frequency and wavelength of the cosine wave.
- sin(x*20/w) – Applies a sine function to the x coordinate of each pixel. The x coordinate is multiplied by (20/w) to control the frequency and wavelength of the sine wave.
With Relative Warping turned on, you ensure that the expression is applied procedurally to each pixel in the image based on the previous displaced or transformed pixel coordinates, rather than each pixel’s original coordinates.
The main problem you will find is that there is not much documentation related to how to actually use this filter or what any of those expressions even mean, and it is even harder to find math expressions to use. But never fear, after weeks of testing and experimenting, I’m finally able to say: “I also don’t quite know what it all means, but I’m here to help!”
Variables and Constants
It doesn’t say this anywhere, but the Cartesian Transform filter is able to use most of the mathematical functions, constants, or variables that are listed in the main GMIC documentation (HERE) Not all of these constants or functions are available and some may actually cause an error message to pop up, so I have collected the most useful ones here! It is also good to note that the Cartesian Transform filter does not seem to be able to alter color information in the image, but it is possible to reference color channels or values in the image inside of expressions.
The settings panel for the Cartesian Transform filter has two important fields where you can enter expressions. These are the X-Warping and the Y-Warping expression fields, and each one affects how the current evaluated pixel is transformed. The X-Warping expression affects the pixel along the horizontal axis, while the Y-Warping expression affects the pixel along the vertical axis. By entering different mathematical expressions into these two fields, it is possible to produce a vast number of unique effects.
The way this filter works is that each pixel in the image is ‘evaluated,’ and the expression that has been entered into the X-Warping or Y-Warping fields will be applied to that pixel’s coordinates before moving on to the next one. This is a bit similar to how Convolution Filters work, but here we are working with mathematical expressions rather than ‘kernels.’
Constant Values:
- w – Width of the image.
- h – Height of the image.
- wh – Quicker way to express ‘image width * image height’
- x – Coordinate X of the currently processed pixel.
- y – Coordinate Y of the currently processed pixel.
- e – Euler’s Number. (2.71828..)
- pi – The value of PI. (3.14159..)
- u – Random value between 0 and 1.
- g – Random gaussian value, meaning values follow a bell curve.
- i – The currently processed pixel value.
Math Functions:
- sin() – Mathematical sine function. (Typically used with the x-axis)
- cos() – Mathematical cosine function. (Typically used with the y-axis)
- tan() – Mathematical tangent function. (Can be used to stretch/shear)
- atan() – Mathematical arctangent function. (The inverse of the ‘tan()’ function)
- abs() – Absolute value. (Can be used to bring negative values to positive)
- floor() – Round a value down. (Can be used for pixelation or mosaic effects)
- sqrt() – Square root of a value. (Can also be used to normalize values.)
This is not necessarily every available constant or math function, but the majority of effects in the next few sections of this tutorial tend to use these the most. Another important aspect of the mathematical expressions you are able to enter into the X-Warping or Y-Warping fields is that they can also be comprised of multiple steps, separated by using a semi-colon (;) so you are not limited to simple expressions. In fact, you can even assign values to variables and use them later in the same expression. Take a look at the example expression below:
a = 1+1; x+a
The first step of the expression (a = 1+1) would set the variable ‘a‘ to a value of 2, and the second step (x+a) would then add the value of ‘x‘ to the value of ‘a‘ giving a final result. Several of the filters in the next few sections of the tutorial give examples of how these multi-part expressions can be used.
The Boundary setting allows you to determine what happens to any pixels that fall outside the boundary of the image itself. In some cases, the warp or displacement effect will cause parts of the image to extend past the borders of the image.
- Transparent – Ignores any warped or transformed pixels outside the boundary of the image and leaves empty space or transparency where there are none. Useful in some cases when the warp or transform is producing a specific shape.
- Periodic – Tiles or repeats the image along the edges in any direction wherever there would be empty space. This is useful for creating seamless tiling images.
- Mirror – Similar to periodic, though instead of tiling from the edge, the image or texture is reflected along the seam. Useful for mirrored or kaleidoscopic effects.
- Nearest – Uses and extends the color value of the nearest pixel at the seam.
The Interpolation setting determines how the pixels are smoothed in the final image. The Nearest Neighbor interpolation uses the closest pixel to determine the color of any intermediate pixels and has a more pixelated or blocky appearance, while the Linear interpolation setting tends to look smoother, but can lose some detail. Some filters look better with one or the other, so the choice is really up to you.
Each of these settings can have a different effect on how the filter transforms or outputs the final result. The examples in the following sections of the tutorial will indicate which Boundary or Interpolation setting works best for each filter.
Perspective Transforms
This first set of expressions affect the way the image is displayed by adjusting the perspective or the orientation, and tend to be useful for quick effects like tiling or de-tiling an image, flipping or mirroring an image along its axes, or shifting the image to produce different angles.
- Image Shift – Moves the image along the x or y axis by a specific amount. You can use numbers, variables, or fixed amounts (Such as the image height / 8) to determine the amount of shift.
X-Warping: x + w/8
Y-Warping: y
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
X-Warping: x
Y-Warping: y – h/8
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- Image Flip – Causes the image to flip along the specified axis. Subtracts the current pixel coordinate (x or y) from the height or width (h or w) of the image.
X-Warping: w – x
Y-Warping: y
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Transparent/None
X-Warping: x
Y-Warping: h – y
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Transparent/None
- Image Tile – Causes the image to tile along the specified axis (or both.) from the upper left corner of the image. This particular expression is multiplying the x and the y values by 2. Remove the ‘*2’ from the x-warping or y-warping expression to tile along one axis, or increase the value to multiply the tiling effect.
X-Warping: x * 2
Y-Warping: y * 2
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
X-Warping: x / 2 OR x * 0.5
Y-Warping: y / 2 OR y * 0.5
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Transparent/None
- Image Tile from Center – Tiles the image by resizing around the center of the image instead of the upper left corner. Increase the ‘*2’ multiplier to increase the tiling effect. This be used to perfectly scale an image to the center of the canvas if the boundary mode is set to Transparent.
X-Warping: w / 2 + (x – w / 2) *2
Y-Warping: h / 2 + (y – h / 2) *2
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- Checker Tile – Tiles the upper left corner of the image across the entire canvas.
X-Warping: x – w/2 * floor(2 * x /w)
Y-Warping: y – h/2 * floor(2 * y/h)
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- Checker Rotate – Rotates sections of the image by dividing it into a grid. Change the value ‘4’ in the following expression to increase or decrease the number of squares.
X-Warping: (x – (x % (w/4)) + (y % (h/4))) % w
Y-Warping: (y – (y % (h/4)) + (x % (w/4))) % h
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- Mirror/Reflect – Reflects and tiles the image along the desired axis. Change the X-Warping expression to ‘x’ to mirror only along the x-axis, or change the Y-Warping expression to ‘y’ to mirror only along the y-axis.
X-Warping: (w/2) – abs(w/2-x)
Y-Warping: y
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
X-Warping: x
Y-Warping: (h/2) – abs(h/2-y)
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
X-Warping: (w/2) – (w/2-x)
Y-Warping: (h/2) – (h/2-y)
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- Radial Slice – Splits the image into slices and rotates them around the center of the image. Adjust the ‘(t-4) and ‘(pi/4)’ values to change the slice angles or amounts.
X-Warping: R=sqrt((x-w/2)^2+(y-h/2)^2);a=atan2(y-h/2,x-w/2);t=floor(a/(pi/4));R*cos(t*(pi/4)-a)
Y-Warping: R=sqrt((x-w/2)^2+(y-h/2)^2);a=atan2(y-h/2,x-w/2);t=floor(a/(pi/4));R*sin(t*(pi/4)-a)
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
X-Warping: R=sqrt((x-w/2)^2+(y-h/2)^2);a=atan2(y-h/2,x-w/2);t=floor(a/(pi/4));offset=1*(t-4)*(pi/4);R*cos(a+offset)
Y-Warping: R=sqrt((x-w/2)^2+(y-h/2)^2);a=atan2(y-h/2,x-w/2);t=floor(a/(pi/4));offset=1*(t-4)*(pi/4);R*sin(a+offset)
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- 45-Degree Internal Rotation – Performs a 1:1 pixel shift causing the image to rotate by 45 degrees to the left or right. Useful for working with or creating isometric graphics.
X-Warping: (x-1)-(y-1)
Y-Warping: (y-1)+(x-1)
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
X-Warping: (x-1)+(y-1)
Y-Warping: (y-1)-(x-1)
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- Isometric Internal Rotation (Floors) – Shifts the pixels in the image over by 2 and up by 1, producing a 2:1 isometric rotation. Useful for working with or creating isometric floor tiles or textures.
X-Warping: (x-1)+(y-1)*2
Y-Warping: (y-1)*2-(x-1)
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
X-Warping: (x-1)-(y-1)*2
Y-Warping: (y-1)*2-(x-1)
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- Isometric Internal Rotation (Walls) – Shifts the pixels in the image over by 2 and up by 1, producing a 2:1 isometric rotation. Useful for working with or creating isometric wall tiles or textures.
X-Warping: x
Y-Warping: y+x/2
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
X-Warping: x
Y-Warping: y-x/2
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
Distortions and Warps
The next set of expressions deal with warping the image in various ways, including displacement, pixelation, or curving the image. Some of these expressions are a bit more complicated and I do not have a very deep understanding of exactly how some of them function, but I am keeping the expressions here for everyone to reference.
- Sine Wave Ripple – Produces a sine wave distortion across the image by using the x coordinate of the pixel to determine how high or low the pixel is redrawn. The ‘0.1’ value controls the strength (amplitude) of the wave.
X-Warping: x
Y-Warping: y + h * 0.1 * sin(2 * pi * x / w)
Relative Warping: OFF
Interpolation: Either
Boundary: Periodic
X-Warping: x + w * 0.1 * sin(2 * pi * y / h)
Y-Warping: y
Relative Warping: OFF
Interpolation: Either
Boundary: Periodic
- Sinus Warp – Warps the image along both the x-axis and the y-axis using a sine wave function.
X-Warping: x + (w/2) * sin(2*pi * y / h)
Y-Warping: y + (h/2) * sin(2*pi * x / w)
Relative Warping: OFF
Interpolation: Linear
Boundary: Periodic
- Circular Ripple – Produces a round water drop effect with concentric rings. Increase or decrease the ‘0.2’ value to modify the ripple strength.
X-Warping: x + 0.1 * (x – w/2) * cos(0.1 * sqrt((x – w/2)^2 + (y – h/2)^2))
Y-Warping: y + 0.1 * (y – h/2) * sin(0.1 * sqrt((x – w/2)^2 + (y – h/2)^2))
Relative Warping: OFF
Interpolation: Linear
Boundary: Periodic
- Spiral Ripple – Produces a spiral version of the water drop-style ripple. Increase or decrease the ‘/16’ at the end of the expression to increase or decrease the effect.
X-Warping: x + (w/2) * cos(atan2(y – h / 2, x – w / 2) + sqrt((x – w/2)^2 + (y – h/2)^2) / 16)
Y-Warping: y
Relative Warping: OFF
Interpolation: Linear
Boundary: Periodic
- Glass Refract – Stretches and squashes the image in various locations producing a refracting glass type effect. Adjust the strength of the effect by changing any of the number values in the example expressions.
X-Warping: x + 5 * cos(x/20)
Y-Warping: y + 5 * sin(y/20)
Relative Warping: OFF
Interpolation: Linear
Boundary: Periodic
X-Warping: x + 3 * sin(x/12) * sin(y/5)
Y-Warping: y + 3 * cos(x/12) * cos(y/5)
Relative Warping: OFF
Interpolation: Linear
Boundary: Periodic
- Cylinder Warp – Causes the texture to stretch along the top and bottom edges to produce a pseudo cylindrical projection.
X-Warping: w/2-(cos(asin(((h/2)-y) / (h/2)))*(w/2-x))
Y-Warping: y
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- Fuzzy Pixelate – Moves the pixels along the x and y axes to create a fuzzy, pixelated distortion effect.
X-Warping: w/2-(cos(asin(((h/2)-y) / (h/2)))*(w/2-x))
Y-Warping: y
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- Stair Displace – Divides the image into rows and shifts each row over to the right by an equal amount. Change the ‘8’ value to adjust how many rows there are or the amount of shifting.
X-Warping: x – (w / 8) * floor(y / (h / 8))
Y-Warping: y
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- Pixelate (Mosaic) – Quantizes the number of pixels in the image to produce a pixelation or mosaic effect. Adjust the ’48’ or the ’16’ values in the example expressions to adjust the amount of pixelation.
X-Warping: floor(x / (w/48)) * (w/48)
Y-Warping: floor(y / (h/48)) * (h/48)
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
X-Warping: x – x % 16
Y-Warping: y – y % 16
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- Circle Transform – Causes the pixels along the top and bottom edges and corners to pull inwards along the top row of pixels producing a circular image.
X-Warping: r=w/2; R=sqrt(r^2-(y-h/2)^2); (x-r)/R * r+r
Y-Warping: y
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Transparent/None
- Spherize – Warps the image into a pseudo-sphere by converting the four corners into curves.
X-Warping: r=w/2; R=sqrt(r^2-(y-h/2)^2); (x-r)/R*r+r – 20*(floor(10*R/w)%2)/i
Y-Warping: r=h/2; R=sqrt(r^2-(x-w/2)^2); (y-r)/R*r+r – 20*(floor(10*R/h)%2)/i
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Transparent/None
- Concave Warp – Warps the image so that it appears to have a concave depression in the center. Change the ’30’ or ‘100’ values in the example expressions to control how deep or how wide the concave depression is.
X-Warping: x+30*sin(2*pi*(x-w/2)/w) * exp(-((x-w/2)^2+(y-h/2)^2) / (2 * 100^2))
Y-Warping: y+30*sin(2*pi*(y-h/2) / h) * exp(-((x-w/2)^2+(y-h/2)^2) / (2 * 100^2))
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
- Zoom Bulge – Causes the image to bulge outwards in the center the edges of the image tile and get smaller. Change the ‘0.003’ value to increase or decrease the strength of the zoom effect.
X-Warping: (x-w/2) / (1-0.003*sqrt((x-w/2)^2 + (y-h/2)^2)) + w/2
Y-Warping: (y-h/2) / (1-0.003*sqrt((x-w/2)^2 + (y-h/2)^2)) + h/2
Relative Warping: OFF
Interpolation: Nearest Neighbor
Boundary: Periodic
There are countless other effects that can be done using the Cartesian Transform filter, it is just a matter of experimenting or being really good with math (Which I am not.) But hopefully this gives you a better understanding of how to use the Cartesian Transform filter, or helps you to create your own unique effects!
This tutorial will be expanded over time with more expressions and effects!