SOLVED: Understanding an fx operator?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
evank

SOLVED: Understanding an fx operator?

Post by evank »

I have a convert command with a special effects image operator:

Code: Select all

convert input.png gradient.png -fx "v.p{0,u*v.h}" output.png
On a high level, I understand what this command is accomplishing: it takes the colors from a gradient image (gradient.png) and applies them as the color palette of the input image (input.png), writing to an output image (output.png).

From what I've figured out, u is the input image, v is the gradient, and it is going through each left-most pixel in the gradient from top to bottom, somehow applying its colors to the input image. But I can't wrap my head around how it's accomplishing that last part, or how to do the same thing in a procedural manner.

I have a piece of software using this command that I'd like to replicate with other image manipulation libraries (to also support anyone in an environment without ImageMagick bindings available). So my question is, can anyone break down exactly what this fx operator is doing in some kind of psuedocode?

Update: Wow, thanks. That pretty much explains it to a tee!
Last edited by evank on 2009-05-11T12:58:42-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Understanding an fx operator?

Post by anthony »

That is (almost) equivelent to a -clut operator ;-)
For more details See IM Examples, Color Modifications, CLUT
http://www.imagemagick.org/Usage/color/#clut

Lets break down the formula...

First FX loops through each pixel of the input image (no control at this time though -geagion has been proposed to limit the area of the input image looped through), and also through each of the three color channels (controled by the -channel value that defaults to just RGB channels (just the color channesl not alpha).
that means the FX formula will be exected width * height * 3 times! that is a lot!

Now as you guessed...
u = color (value) of the current pixel (and channel) in input image
the input image (u) is presumably a greyscale image so each channel value is the same, unless you are doing a histogram adjustment basied on a greyscale gradient in the second 'v' image.

v.h = is the height of the second image.

u*v.h = Multiply the color value (range 0 to 1) with the heigh of the second 'v' image.
that is convert the color into a height index position in the second image.

v.p{} = look up the color from 'v' at position given

v.p{0, u*v.h} get the color from the first column of 'v' at a distance u*v.h from the top.

In other word replace each color value in the input 'u' image, with the color in the second 'v' image indexed by the original color value.

Or A Color Lookup using the second image as the Lookup table.

As mentioned this is (almost) exactly the same as the newer -clut operator that was developed from that specific -fx formula. The only difference is instead of v.p{0,u*v.h} it is actually using v.p{u*v.w,u*v.h} or an indexed lookup along a diagonal line though the second lookup-table-image. this means that the lookup table image can be either single row, or a single column image, and things will still come out correct.

Actually the final formula is a little more complex than that to take into account the current -interpolation setting. It also is effected at extreme values by the -virtual-pixel setting, so it is a good idea to ensure the defualt 'edge' VP setting is in use.

For more details See IM Examples, Color Modifications, CLUT
http://www.imagemagick.org/Usage/color/#clut
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply