Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving xlib XImage to PNG

Tags:

c

linux

x11

xorg

xlib

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?

like image 871
digitalfoo Avatar asked Dec 16 '10 22:12

digitalfoo


1 Answers

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:

  • create an Xlib surface pointed at your drawable
  • save to PNG http://cairographics.org/manual/cairo-PNG-Support.html

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.

like image 160
Havoc P Avatar answered Oct 03 '22 03:10

Havoc P