Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

png - Making a screenshot using Xlib and Cairo libs [fail]

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.

Here's the code :

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <cairo.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

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);
}

When I compile and run the program, everything seems to work just fine. However here's the resulting image :

quite a mess

quite a mess right ?.. What am I possibly doing wrong ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...