Search code examples
c++linuxjava-native-interfacex11xlib

How to save XImage as bitmap?



i'm trying to create JNI C++ library that will capture desktop video (frames). First step is to simply make a screenshot of desktop. Code is :

#include <iostream>
#include <X11/Xlib.h>

using namespace std;

int main()
{
        Display *display;
        int screen;
        Window root;
        display = XOpenDisplay(0);
        screen = DefaultScreen(display);
        root = RootWindow(display, screen);
        XImage *img = XGetImage(display,root,0,0,400,400,XAllPlanes(),ZPixmap);

        if (img != NULL)
        {
           //save image here
        }
        return 0;
}

But, how to save img as bitmap file ? Because target library is JNI - it must not use third-party libraries. (as i understood).
Please, help.
Thank you.


Solution

  • To do this you have to write a convert routine for all possible XImage formats, or at least all formats your users are likely to have.

    See _get_image_surface() in cairo for example:

    If you can't use a third-party library you will have to reimplement something like that. Note that it's chaining to libpixman for some formats so the code is even more complex than it appears there.