c++imagemagickmagick++

Trouble creating grayscale image from array of pixel intensities with ImageMagick


I'm trying to output a png grayscale image using values from an array of intensity values using ImageMagick.

I've used the Image constructor to try to do this, but the image it is creating does not exactly match the given array.

Image grayscaleImage(256, 256, "I", DoublePixel, inputPtr);
grayscaleImage.write("test.png");

The image that's being created has the correct values for all of the black pixels (intensity of 0) but for the non-zero pixels, I'm getting only white; no gray. How can I correct this issue? Or am I using the constructor incorrectly? Thank you!


Solution

  • As emcconville stated, the numbers in the array of integers need to be scaled to be between 0.0 and 1.0 for Magick::DoublePixel. I achieved this by using the following function:

           (b-a)(x - min)
    f(x) = --------------  + a
              max - min
    

    Where a == 0, b == 1, x == inputPtr[index], min == 0, and max == 255.