Question

I am using CImg combined with libpng in Visual Studio 2010 to display png images. When I display the image using CImg, it appears to not correctly display the transparency. I am not sue if this is me not fully grasping the concepts of CImg, a mistake I'm making, or a combination of both!

image showing the problem described

The image on the left shows the image as opened in windows photo viewer - with the background transparent as expected. The image on the right shows the CImg display that is actually displaying a pure white background with some odd brown areas surrounding the character.

CImg is loading the png with the 4th alpha channel (as shown in right image window title). When mousing over the areas in the CImg display it shows the correct transparency values in the small pop up (bottom left of right image). CImg appears to be drawing everything, regardless of the transparency value?

My question is, how do I get Cimg to correctly display a pgn with transparent sections?

My code for this example is as follows:

#define cimg_use_png
#include <CImg.h>
using namespace cimg_library;

int main()
{
    CImg<unsigned char> image("image.png");
    image.display();
    return 0;
}
Was it helpful?

Solution

The default display viewer of CImg just shows the 3 first channels of your image. It does even ignore that the 4th channel is indeed a transparency channel, it could be something else (the K channel for a CMYK-coded image for instance). CImg doesn't store this kind of information, it lets the user knowing what he manipulates at the end. If you have a 4th-channel for transparency and what to display it in CImg, you have to create a small rendering procedure by yourself, like this :

CImg<unsigned char> render(img.width(),img.height(),1,3,255);
render.draw_image(0,0,0,0,img,img.get_channel(3),1,255);

(assuming 'img' is your 4-channel image). Now 'render' is a RGB color image that is a rendering of your initial (transparent) image over a white background.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top