Создание скриншота с использованием библиотек Xlib и Cairo [сбой]

я пытаюсь сделать скриншот с помощьюXlib а такжеКаир, Однако я'Я не уверен, что сделать это хорошо "шаг» действительно смущает меня.

Вот's код:

#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char** argv) {

    int x, y;

    Display *disp;
    Window root;
    XWindowAttributes watts;
    XImage *image;
    cairo_surface_t *surface;
    unsigned int width;
    unsigned int height;
    int stride;

    disp = XOpenDisplay(NULL);
    root = DefaultRootWindow(disp);
    XGetWindowAttributes(disp, root, &watts);
    width = watts.width;
    height = watts.height;

    image = XGetImage(disp, root, watts.x, watts.y, width, height, AllPlanes, ZPixmap);
    stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, width);
    unsigned char *data = malloc(width * height * 3);

    for (y = 0; y < height; ++y)
        for (x = 0; x < width; ++x) {

            unsigned long pixel = XGetPixel(image, x, y);

            unsigned char red = (image->red_mask & pixel);
            unsigned char green = (image->green_mask & pixel) >> 8;
            unsigned char blue = (image->blue_mask & pixel) >> 16;

            data[(y * width + x) * 3] = red;
            data[(y * width + x) * 3 + 1] = green;
            data[(y * width + x) * 3 + 2] = blue;
        }


    surface = cairo_image_surface_create_for_data(
            data,
            CAIRO_FORMAT_RGB24,
            width, height,
            stride);

    cairo_surface_write_to_png(
            surface,
            "test.png");


    cairo_surface_destroy(surface);
    free(data);

    return (EXIT_SUCCESS);
}

Когда я компилирую и запускаю программу, кажется, все работает нормально. Однако здесьПолученное изображение:

довольно беспорядок верно? ..Что я, возможно, делаю не так?

Ответы на вопрос(2)

Ваш ответ на вопрос