RESOLVED: Color lookup table with FxImage method

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
evank

RESOLVED: Color lookup table with FxImage method

Post by evank »

I'm trying to replicate a command-line imagemagick operation utilizing an fx operator, and apparently Imagick doesn't work in quite the way I'd expected it to.

The following command essentially applies a color lookup table (clut.png, a vertical gradient image) to another image (input.png, a greyscale image) to produce a colorized output image:

Code: Select all

convert input.png clut.png -fx "v.p{0,u*v.h}" output_cmd.png
I thought I could load the input and color lookup images into a sequence, then apply the fx operator like so:

Code: Select all

<?php

# load input file and color lookup file in a sequence
$im = new Imagick(array('input.png', 'clut.png'));

# apply fx operator (can't use clutImage for backwards compatibility)
$im->fxImage("v.p{0,u*v.h}");

# write output file
$im->writeImage('output_php.png');
However, this just outputs the last image in the sequence (the color lookup), and the fx expression doesn't seem to do anything. What am I doing wrong here?

I know there's a clutImage method specifically for this, but one of the environments I'm targeting for this has an older version of Imagemagick, so I wanted this for backward compatibility.

UPDATE: The reason fxImage seemed to be doing nothing was that I didn't realize it returns a new Imagick object rather than modifying the existing one. With that moment of stupidity realized, I grabbed the return value of the fxImage method, which seemed to be returning the expected image, but cropped to the dimensions of the color lookup table.

Code: Select all

# apply fx operator (can't use clutImage for backwards compatibility)
$im = $im->fxImage("u.p{0,v*u.h}");
Curious, but progress, so I tried alternately switching the order of images in the sequence:

Code: Select all

# load input file and color lookup file in a sequence
$im = new Imagick(array('clut.png', 'input.png'));
And switching the order of images in the expression

Code: Select all

# apply fx operator (can't use clutImage for backwards compatibility)
$im = $im->fxImage("u.p{0,v*u.h}");
Doing both of the above seems to do the trick. I don't totally understand why, but it works now.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: RESOLVED: Color lookup table with FxImage method

Post by anthony »

The first (zeroth) image is regarded by FX as the destination image. As such this image provides not only the final size of the results, but it also provides the final images meta-data, including virtual offsets (which is available but not directly used by FX).

All the other images given are regarded by FX purely as secondary source images for data, such as for DIY composition, Color Lookups, or even pattern lookups.
The 'v' is a convenience variable to refer to the second image. Other images can also be referenced via images indexes EG: 'u[4]' to refer to the fifth image.

It does not have to reference other images and in fact image distortion methods are developed using FX first! See the Verbose output from -distort for examples!
Also see the FX distortion examples that were developed before -distort was created... DIY perspective Distortion using FX

Fx returns ONE image that is meant to be regarded as the merger of all the given images. It has to be a completely new and separate image, as FX can and does lookup ANY pixel from ANY source image. If it was the same image FX could end up referring to pixels it has already modified!!!

For more details of FX see...
http://www.imagemagick.org/Usage/transform/#fx

Many operators were developed from FX, -distort for example, -clut, some new -compose methods, and many gradient mathematical functions -evaluate, and -function.

At this very moment new operators for handling Fast Fourier Transform Images (FFT) is being developed, as part of the new -fft and -ift operators (a example section is being created for this, right now).

Sorry I am rambling.


PS: watch your boundary conditions with CLUT rememebr images are referenced from '0' to 'h-1' and not 'h'. And also watch for -interpolation, and -virtual-pixel effects on the FX lookup colors (same as when using CLUT).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply