Search code examples
linuximagex11xcb

How to display a full color image with XCB or X11?


I can load a PNG image into memory and get the raw pixel data from that using libpng, and I can also create windows with a blank background using XCB or plain X11.

What should I do next to display an image inside a window?


Solution

  • The XLib method is as follows:

    • Create an XImage structure with depth appropriate to your visual
    • Convert raw pixel data to the format corresponding to the combination of depth and visual-class you have
    • call XPutImage

    The second step can be achieved by calling XPutPixel for each individual pixel. You will have to convert RGB values to pixel values. For 15,- 16,- 24- or 32-bit visuals this is a trivial manipulation with bitmasks (use visual->red_mask to determine where to put the red component, etc). If you want to support 8-bit depth you have to use dithering, and allocate and use an appropriate colormap, probably a 216-element colour cube. Fortunately 8-bit-only hardware is rare these days.

    If calling XPutPixel is too slow for you, you will have to implement what it does in line. Use e.g. this source for guidance.

    xcb has a library called xcb-util-image with the functionality parallel to that of XImage. I'm not familiar with it.