forked from BoboTiG/python-mss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-linux.c
More file actions
162 lines (142 loc) · 5.43 KB
/
test-linux.c
File metadata and controls
162 lines (142 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* This is the MSS python's module implementation in C.
*
* Build: gcc -O3 -lX11 -lXrandr test-linux.c -o test-linux
* Test : python test-raw.py data.raw width height
*
* http://cgit.freedesktop.org/xorg/lib/libX11/tree/src/ImUtil.c#n444
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
void full_screen(void) {
struct timeval start, end;
Display *display;
Window screen, root;
XWindowAttributes gwa;
XImage *ximage;
int left, top;
unsigned int x, y, width, height, offset;
unsigned long allplanes, pixel;
unsigned char *pixels, *addr;
gettimeofday(&start, NULL);
display = XOpenDisplay(NULL);
screen = XDefaultScreen(display);
root = XDefaultRootWindow(display);
XGetWindowAttributes(display, root, &gwa);
left = gwa.x;
top = gwa.y;
width = gwa.width;
height = gwa.height;
allplanes = XAllPlanes();
ximage = XGetImage(display, root, left, top, width, height, allplanes, ZPixmap);
pixels = malloc(sizeof(unsigned char) * width * height * 3);
/*printf("display = %d\n", display);
printf("screen = %d\n", screen);
printf("root = %d\n", root);
printf("left = %d\n", left);
printf("top = %d\n", top);
printf("width = %d\n", width);
printf("height = %d\n", height);
printf("allplanes = %u\n", allplanes);
printf("bits_per_pixel = %d\n", ximage->bits_per_pixel);
printf("bytes_per_line = %d\n", ximage->bytes_per_line);
printf("depth = %d\n", ximage->depth);
*/
// Processus habituel
for ( x = 0; x < width; ++x ) {
for ( y = 0; y < height; ++y ) {
pixel = XGetPixel(ximage, x, y);
offset = width * y * 3;
pixels[x * 3 + offset] = (pixel & ximage->red_mask) >> 16;
pixels[x * 3 + offset + 1] = (pixel & ximage->green_mask) >> 8;
pixels[x * 3 + offset + 2] = pixel & ximage->blue_mask;
}
}
// Processus sans passer par XGetPixel (ça se vaut)
/*
for ( x = 0; x < width; ++x ) {
for ( y = 0; y < height; ++y ) {
offset = width * y * 3;
addr = &(ximage->data)[y * ximage->bytes_per_line + (x << 2)];
pixel = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
pixels[x * 3 + offset] = (pixel & ximage->red_mask) >> 16;
pixels[x * 3 + offset + 1] = (pixel & ximage->green_mask) >> 8;
pixels[x * 3 + offset + 2] = pixel & ximage->blue_mask;
}
}
*/
XDestroyImage(ximage);
XCloseDisplay(display);
gettimeofday(&end, NULL);
printf("Fullscreen: %dx%d %u msec\n", width, height, (1000000 * end.tv_sec + end.tv_usec) - (1000000 * start.tv_sec + start.tv_usec));
FILE* fh = fopen("test-linux_fullscreen.raw", "wb");
fwrite(pixels, sizeof(unsigned char), sizeof(unsigned char) * width * height * 3, fh);
fclose(fh);
return;
}
void each_screen(void) {
struct timeval start, end;
Display *display;
Window root;
XRRScreenResources *monitors;
XRRCrtcInfo *crtc_info;
XImage *ximage;
int left, top;
unsigned int n, x, y, width, height, offset;
unsigned long allplanes, pixel;
unsigned char *pixels;
display = XOpenDisplay(NULL);
root = XDefaultRootWindow(display);
monitors = XRRGetScreenResources(display, root);
for ( n = 0; n < monitors->ncrtc; ++n ) {
gettimeofday(&start, NULL);
crtc_info = XRRGetCrtcInfo(display, monitors, monitors->crtcs[n]);
/*printf("motior n°%d\n", n);
printf(" x = %d\n", crtc_info->x);
printf(" y = %d\n", crtc_info->y);
printf(" width = %d\n", crtc_info->width);
printf(" height = %d\n", crtc_info->height);
printf(" mode = %d\n", crtc_info->mode);
printf(" rotation = %d\n", crtc_info->rotation);*/
left = crtc_info->x;
top = crtc_info->y;
width = crtc_info->width;
height = crtc_info->height;
allplanes = XAllPlanes();
ximage = XGetImage(display, root, left, top, width, height, allplanes, ZPixmap);
pixels = malloc(sizeof(unsigned char) * width * height * 3);
for ( x = 0; x < width; ++x ) {
for ( y = 0; y < height; ++y ) {
offset = width * y * 3;
pixel = XGetPixel(ximage, x, y);
pixels[x * 3 + offset] = (pixel & ximage->red_mask) >> 16;
pixels[x * 3 + offset + 1] = (pixel & ximage->green_mask) >> 8;
pixels[x * 3 + offset + 2] = pixel & ximage->blue_mask;
}
}
XDestroyImage(ximage);
XRRFreeCrtcInfo(crtc_info);
gettimeofday(&end, NULL);
printf("Screen %d: %dx%d @ %u msec\n", n, width, height, (1000000 * end.tv_sec + end.tv_usec) - (1000000 * start.tv_sec + start.tv_usec));
char output[128];
sprintf(output, "test-linux_screen-%d.raw", n);
FILE* fh = fopen(output, "wb");
fwrite(pixels, sizeof(unsigned char), sizeof(unsigned char) * width * height * 3, fh);
fclose(fh);
}
XRRFreeScreenResources(monitors);
XCloseDisplay(display);
return;
}
int main(void) {
printf("To test raw data: python test-raw.py data.raw width height\n\n");
/* The full screen capture */
full_screen();
/* A capture for each screen */
each_screen();
return 0;
}