When I open png file:
CImg<unsigned char> image("image.png");
...I expect to get 4 channel (RGBA) image. But I always get 3 channel (RGB) image instead (even if the image is semitransparent): image.spectrum() returns 3 instead of 4.
In my application, I always need to get 4 channels when I open PNG file. CImg looks like popular library with many useful features (some of which may be useful in the future in my image processing application), so I find it hard to believe that it is impossible to open PNG file with alpha channel without losing it. Any suggestions?
If CImg is not very good tool for working with PNG files, feel free to suggest some other simple to use library. Only important thing for me at this moment is to be able to read and write PNG as 4 channel unsigned char image and to have functions to get image dimensions and data (like image.width() or image.data() ).
CImg is a very good library to work with .png files (incl. 4-channels). For a native support of png files by CImg, you need to link your code with the libpng library, otherwise CImg will use an external call to ImageMagick's convert tool to read the .png file. To enable the full .png support in CImg, just compile your code with the 'cimg_use_png' macro defined, and link it with libpng, and you're done :
g++ -o foo foo.cpp -Dcimg_use_png -lpng -lz -lX11 -lpthread
I found CImg to be one of the library with the most complete support of .png and .tiff files, you only have to define some configuration flags to allow it using the proper libraries. By default, CImg is configured to use a minimal set of libraries.
Suggestion for another library (that I have used and imo is quite easy): http://www.imagemagick.org/script/api.php#c
来源:https://stackoverflow.com/questions/12084048/how-to-open-png-with-cimg-library-without-losing-alpha-channel