Linux – How do I display full-color images using XCB or X11?

How do I display full-color images using XCB or X11?… here is a solution to the problem.

How do I display full-color images using XCB or X11?

I can

load PNG images into memory and get raw pixel data from them using libpng, and I can also create windows with blank backgrounds using XCB or normal X11.

What should I do next to display the image in the window?

Solution

The XLib method is as follows:

  • Create an XImage structure with a depth that fits your visual effect
  • Convert raw pixel data into a format that corresponds to the combination of depth and visual levels you have
  • Call XPutImage

The second step can be achieved by calling XPutPixel for each individual pixel. You must convert RGB values to pixel values. For 15-, -16, -24, or 32-bit visuals, this is a simple operation of bitmasking (using visual->red_mask to determine where to place the red component, etc.). If you want to support 8-bit depth, you must use dithering and assign and use the appropriate colormap, possibly a color cube of 216 elements. Fortunately, hardware with only 8 bits is now rare.

If calling XPutPixel is too slow for you, you will have to perform its functions online. Use e.g. this source for guidance.

xcb has a library called xcb-util-image that functions similarly to XImage. I’m not familiar with it.

Related Problems and Solutions