c++imagemagickmagick++

Magick++ find out whether an image has transparency


I'm trying to find out whether an Image (the Magick++-class) is opaque/has transparent pixels. My current test code looks like this:

    Image orig;
    orig.read(inputPath.c_str());

    bool hasAlpha = orig.alpha();
    printf("Alpha: %s %s\n", inputPath.c_str(), hasAlpha ? "yes" : "no");

This correctly outputs "no" for a jpg and "yes" for a png with transparency, but unfortunately also reports "yes" for a PNG with no transparent pixels.

With the command identify -format '%[opaque]' image.png, imagemagick is able to detect this correctly for all files, so it is able to find this out, but I'd like to avoid calling an external program for various reasons and I can't find no fitting method in the docs or via Google. How can I find this out in code via Magick++?

Thank you!


Solution

  • With ImageMagick-7, I believe the method you need is Magick::Image::isOpaque(); which calls the same MagickCore method to calculate '%[opaque]'.

    
    bool hasAlpha = !orig.isOpaque();
    printf("Alpha: %s %s\n", inputPath.c_str(), hasAlpha ? "yes" : "no");