Saving xlib XImage to PNG

大兔子大兔子 提交于 2019-12-14 01:04:42

问题


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:


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.



来源:https://stackoverflow.com/questions/4465796/saving-xlib-ximage-to-png

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!