5
votes

I am using xlib.

I have an XImage structure filled with information from an XGetImage() call. Is there a popular method to get from XImage to something more meaningful.. namely PNG?

I have looked at libpng, but have heard from pretty much everyone that it's a beast to tame. Would this still be the recommended path to take?

1

1 Answers

5
votes

See also How to save XImage as bitmap? though that person had the constraint that they couldn't use a library.

If you can use a library, Cairo is a good one that will do this for you I believe. It has PNG saving dealing with the libpng mess for you, and it has code to get the pixels from X. However, it may make it hard to get pixels from an XImage; it will want to get them from a window or pixmap. If you can just replace your XGetImage() with cairo, then it might work fine. The way you would do things roughly in cairo I think is:

You could also use the Xlib surface as source to draw to an image surface, and then do other stuff with the image surface (scale or paint on it or whatever) if you wanted, before saving as PNG.

If you're using any kind of UI toolkit, it probably has code for this too, e.g. GTK+ has gdk_pixbuf_get_from_drawable() etc.