2

I am trying to set a background image for one of my windows created with Xlib. I would like the image to be a JPEG or PNG. I downloaded DevIL (which I prefer to use because it supports a lot of formats).

So, my questions, is, how do I do it? I can't find any specific tutorial or help.
I understand how I can use DevIL to load an image into a stream, but how do I put that on the window? I found an answer here: Load image onto a window using xlib but I don't know how and which function should receive the image bytes. As I also understand, I should have a XImage that would hold all the image, and which I would use with XPutImage. What I don't understand is how do I send the image's bytes from DevIL to XImage.

Does somebody know any helpful page or maybe some clues about how I should do it?
Thanks!

Community
  • 1
  • 1
ali
  • 10,927
  • 20
  • 89
  • 138

1 Answers1

2

The Xlib function used to create an XImage is XCreateImage, and its usage looks like this (you can read a full description in the link):

XImage *XCreateImage(display, visual, depth, format, offset, data, 
                    width, height, bitmap_pad, bytes_per_line)

where the relevant argument for your specific question would be data, a char* that points to where you keep the image data loaded in with DevIL. With this, you should then be able to follow the steps in the other answer you already found.

Edited to add:

You still have to tell DevIL how to format your image data so that XCreateImage can understand it. For example the following pair of function calls will create an XImage that appears correctly:

ilCopyPixels(
    0, 0, 0, 
    image_width, image_height, 1, 
    IL_BGRA, IL_UNSIGNED_BYTE, 
    image_data
);

// ...

XImage* background = XCreateImage(
    display,
    XDefaultVisual(display, XDefaultScreen(display)), 
    XDefaultDepth(display, XDefaultScreen(display)),
    ZPixmap,
    0,
    image_data,
    image_width,
    image_height,
    32,
    0
);

, if you instead chose IL_RGBA, the colors will be off!

Community
  • 1
  • 1
tecu
  • 540
  • 2
  • 7
  • So, the DevIL function that loads the image leaves the data into a variable which is ready to be used by XCreateImage? I mean, the bytes that are stored at that address can be understood by XCreateImage, regardless of the image format DevIL has loaded. Is that so? THANKS! – ali Sep 18 '12 at 11:38
  • @ali I added a little clarification in response to your comment. – tecu Sep 18 '12 at 13:39
  • Thanks so much! This is what I wanted to know! – ali Sep 18 '12 at 14:11