Question

I'm almost there with my little "window background from PNG image" project in Linux. I use pure X11 API and the minimal LodePNG to load the image. The problem is that the background is the negative of the original PNG image and I don't know what could be the problem.

This is basically the code that loads the image then creates the pixmap and applies the background to the window:

// required headers
// global variables
Display *display;
Window window;
int window_width = 600;
int window_height = 400;

// main entry point

// load the image with lodePNG (I didn't modify its code)
vector<unsigned char> image;
unsigned width, height;

//decode
unsigned error = lodepng::decode(image, width, height, "bg.png");
if(!error)
{
    // And here is where I apply the image to the background
    Screen* screen = NULL;
    screen = DefaultScreenOfDisplay(display);

    // Creating the pixmap
    Pixmap pixmap = XCreatePixmap(
        display, 
        XDefaultRootWindow(display), 
        width, 
        height, 
        DefaultDepth(display, 0)
    );

    // Creating the graphic context
    XGCValues gr_values;
    gr_values.function = GXcopy;
    gr_values.background = WhitePixelOfScreen(display);

    // Creating the image from the decoded PNG image
    XImage *ximage = XCreateImage(
        display, 
        CopyFromParent, 
        DisplayPlanes(display, 0), 
        ZPixmap, 
        0, 
        (char*)&image, 
        width, 
        height, 
        32, 
        4 * width
    );

    // Place the image into the pixmap
    XPutImage(
        display, 
        pixmap, 
        gr_context, 
        ximage, 
        0, 0, 
        0, 0, 
        window_width, 
        window_height
    );

    // Set the window background
    XSetWindowBackgroundPixmap(display, window, pixmap);

    // Free up used resources
    XFreePixmap(display, pixmap);
    XFreeGC(display, gr_context);
}

The image is decoded (and there's the possibility to be badly decoded) then it is applied to the background but, as I said, the image colors are inversed and I don't know why.

MORE INFO

After decoding I encoded the same image into a PNG file which is identical to the decoded one, so it looks like the problem is not related to LodePNG but to the way I play with XLib in order to place it on the window.

EVEN MORE INFO Now I compared the inverted image with the original one and found out that somewhere in my code the RGB is converted to BGR. If one pixel on the original image is 95, 102, 119 on the inverted one it is 119, 102, 95.

Was it helpful?

Solution

I found the solution here. I am not sure if is the best way but the simpler for sure.

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