c++imagemagickpngmagick++bit-depth

Magick++ - reduce bit-depth of grayscale image


I'm trying to reduce the channel-bit-depth of PNGs from 8 to 4 using Magick++. I'm using the following code to do this:

Image * img = new Image("./image8.png");
img->channelDepth(ChannelType::AllChannels,4);
img->write("./image4.png");

For color-images, this works great. But for grayscale images, ImageMagick does something strange. It squeezes the whole content into the left half of the image and displays random pixels in the right half.

For example this baboon here: enter image description here

turns into this with the code above:enter image description here

Does anybody have an explanation for this behavior and/or an idea how I can solve this?

Or is this maybe even a bug of image magick?


Solution

  • Looks like a bug in your version of ImageMagick. The current version (6.9.2-8) doesn't exhibit the bug when running it from the commandline: "convert 5koQD.png -depth 4 depth4.png"