Skip to content

Commit 590e947

Browse files
author
BoboTiG
committed
tests: update alternative to XGetPixel()
1 parent f4bcc8e commit 590e947

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

test/test-linux.c

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ void full_screen(void) {
2020
Display *display;
2121
Window screen, root;
2222
XWindowAttributes gwa;
23-
XImage *image;
23+
XImage *ximage;
2424
int left, top;
2525
unsigned int x, y, width, height, offset;
2626
unsigned long allplanes, pixel;
27-
unsigned char *pixels;
27+
unsigned char *pixels, *addr;
2828

2929
gettimeofday(&start, NULL);
3030

@@ -37,7 +37,7 @@ void full_screen(void) {
3737
width = gwa.width;
3838
height = gwa.height;
3939
allplanes = XAllPlanes();
40-
image = XGetImage(display, root, left, top, width, height, allplanes, ZPixmap);
40+
ximage = XGetImage(display, root, left, top, width, height, allplanes, ZPixmap);
4141
pixels = malloc(sizeof(unsigned char) * width * height * 3);
4242

4343
/*printf("display = %d\n", display);
@@ -48,51 +48,37 @@ void full_screen(void) {
4848
printf("width = %d\n", width);
4949
printf("height = %d\n", height);
5050
printf("allplanes = %u\n", allplanes);
51-
printf("bits_per_pixel = %d\n", image->bits_per_pixel);
52-
printf("bytes_per_line = %d\n", image->bytes_per_line);
53-
printf("depth = %d\n", image->depth);
51+
printf("bits_per_pixel = %d\n", ximage->bits_per_pixel);
52+
printf("bytes_per_line = %d\n", ximage->bytes_per_line);
53+
printf("depth = %d\n", ximage->depth);
5454
*/
5555

5656
// Processus habituel
5757
for ( x = 0; x < width; ++x ) {
5858
for ( y = 0; y < height; ++y ) {
59-
pixel = XGetPixel(image, x, y);
59+
pixel = XGetPixel(ximage, x, y);
6060
offset = width * y * 3;
61-
pixels[x * 3 + offset] = (pixel & image->red_mask) >> 16;
62-
pixels[x * 3 + offset + 1] = (pixel & image->green_mask) >> 8;
63-
pixels[x * 3 + offset + 2] = pixel & image->blue_mask;
61+
pixels[x * 3 + offset] = (pixel & ximage->red_mask) >> 16;
62+
pixels[x * 3 + offset + 1] = (pixel & ximage->green_mask) >> 8;
63+
pixels[x * 3 + offset + 2] = pixel & ximage->blue_mask;
6464
}
6565
}
6666

67-
// Processus sans passer par XGetPixel (pas vraiment mieux...)
67+
// Processus sans passer par XGetPixel (ça se vaut)
6868
/*
69-
unsigned int shift = 0xffffffff;
70-
if ( image->depth == 24 ) {
71-
shift = 0x00ffffff;
72-
}
73-
unsigned long px;
74-
register char *src, *dst;
75-
register int i, j;
7669
for ( x = 0; x < width; ++x ) {
7770
for ( y = 0; y < height; ++y ) {
78-
offset = (y * image->bytes_per_line + ((x * image->bits_per_pixel) >> 3)) + 4;
79-
src = &image->data[offset];
80-
dst = (char*)&px;
81-
px = 0;
82-
for ( i = (image->bits_per_pixel + 7) >> 3; --i >= 0; )
83-
*dst++ = *src++;
84-
pixel = 0;
85-
for ( i = sizeof(unsigned long); --i >= 0; )
86-
pixel = (pixel << 8) | ((unsigned char *)&px)[i];
8771
offset = width * y * 3;
88-
pixels[x * 3 + offset] = (pixel & image->red_mask) >> 16;
89-
pixels[x * 3 + offset + 1] = (pixel & image->green_mask) >> 8;
90-
pixels[x * 3 + offset + 2] = pixel & image->blue_mask;
72+
addr = &(ximage->data)[y * ximage->bytes_per_line + (x << 2)];
73+
pixel = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
74+
pixels[x * 3 + offset] = (pixel & ximage->red_mask) >> 16;
75+
pixels[x * 3 + offset + 1] = (pixel & ximage->green_mask) >> 8;
76+
pixels[x * 3 + offset + 2] = pixel & ximage->blue_mask;
9177
}
9278
}
9379
*/
9480

95-
XDestroyImage(image);
81+
XDestroyImage(ximage);
9682
XCloseDisplay(display);
9783

9884
gettimeofday(&end, NULL);
@@ -110,8 +96,7 @@ void each_screen(void) {
11096
Window root;
11197
XRRScreenResources *monitors;
11298
XRRCrtcInfo *crtc_info;
113-
XWindowAttributes gwa;
114-
XImage *image;
99+
XImage *ximage;
115100
int left, top;
116101
unsigned int n, x, y, width, height, offset;
117102
unsigned long allplanes, pixel;
@@ -132,25 +117,24 @@ void each_screen(void) {
132117
printf(" mode = %d\n", crtc_info->mode);
133118
printf(" rotation = %d\n", crtc_info->rotation);*/
134119

135-
XGetWindowAttributes(display, root, &gwa);
136120
left = crtc_info->x;
137121
top = crtc_info->y;
138122
width = crtc_info->width;
139123
height = crtc_info->height;
140124
allplanes = XAllPlanes();
141-
image = XGetImage(display, root, left, top, width, height, allplanes, ZPixmap);
125+
ximage = XGetImage(display, root, left, top, width, height, allplanes, ZPixmap);
142126
pixels = malloc(sizeof(unsigned char) * width * height * 3);
143127

144128
for ( x = 0; x < width; ++x ) {
145129
for ( y = 0; y < height; ++y ) {
146-
pixel = XGetPixel(image, x, y);
147130
offset = width * y * 3;
148-
pixels[x * 3 + offset] = (pixel & image->red_mask) >> 16;
149-
pixels[x * 3 + offset + 1] = (pixel & image->green_mask) >> 8;
150-
pixels[x * 3 + offset + 2] = pixel & image->blue_mask;
131+
pixel = XGetPixel(ximage, x, y);
132+
pixels[x * 3 + offset] = (pixel & ximage->red_mask) >> 16;
133+
pixels[x * 3 + offset + 1] = (pixel & ximage->green_mask) >> 8;
134+
pixels[x * 3 + offset + 2] = pixel & ximage->blue_mask;
151135
}
152136
}
153-
XDestroyImage(image);
137+
XDestroyImage(ximage);
154138
XRRFreeCrtcInfo(crtc_info);
155139

156140
gettimeofday(&end, NULL);

0 commit comments

Comments
 (0)