Making a screenshot using Xlib and Cairo libs [fail]

后端 未结 2 2043
野趣味
野趣味 2020-12-18 15:59

I\'m trying to make a screenshot using Xlib and Cairo, however I\'m not sure to do it the good way, \"stride\" is really confusing me.

相关标签:
2条回答
  • 2020-12-18 16:47

    Instead of doing all this complicated magic, let cairo do it for you:

    #include <cairo.h>
    #include <cairo-xlib.h>
    #include <X11/Xlib.h>
    
    int main(int argc, char** argv) {
        Display *disp;
        Window root;
        cairo_surface_t *surface;
        int scr;
    
        disp = XOpenDisplay(NULL);
        scr = DefaultScreen(disp);
        root = DefaultRootWindow(disp);
    
        surface = cairo_xlib_surface_create(disp, root, DefaultVisual(disp, scr),
                DisplayWidth(disp, scr), DisplayHeight(disp, scr));
        cairo_surface_write_to_png(
                surface,
                "test.png");
        cairo_surface_destroy(surface);
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-18 16:51

    TFM:

    CAIRO_FORMAT_RGB24
        each pixel is a 32-bit quantity, with the upper 8 bits unused
    

    TFM:

    stride = cairo_format_stride_for_width (format, width);
    data = malloc (stride * height);
    

    Hence, the correct index calculation is

    data[y * stride + x * 4 + 0] = blue;
    data[y * stride + x * 4 + 1] = green;
    data[y * stride + x * 4 + 2] = red;    /* yes, in this order */
    

    Also, masks are taken from the image and shifts are hard-coded, which makes absolutely no sense. Calculate the shifts from the masks.

    0 讨论(0)
提交回复
热议问题