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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <png.h>
#include "test.h"
#define MAX_DELTA 3
int pixel_difference(uint32_t a, uint32_t b)
{
int max = 0;
int i;
for (i = 0; i < 32; i += 8) {
uint8_t ac = (a >> i) & 0xff;
uint8_t bc = (b >> i) & 0xff;
int d;
if (ac > bc)
d = ac - bc;
else
d = bc - ac;
if (d > max)
max = d;
}
return max;
}
static void
show_pixels(char *buf,
const XImage *out, const XImage *ref,
int x, int y, int w, int h)
{
int i, j, len = 0;
for (j = y - 2; j <= y + 2; j++) {
if (j < 0 || j >= h)
continue;
for (i = x - 2; i <= x + 2; i++) {
if (i < 0 || i >= w)
continue;
len += sprintf(buf+len,
"%08x ",
*(uint32_t*)(out->data +
j*out->bytes_per_line +
i*out->bits_per_pixel/8));
}
len += sprintf(buf+len, "\t");
for (i = x - 2; i <= x + 2; i++) {
if (i < 0 || i >= w)
continue;
len += sprintf(buf+len,
"%08x ",
*(uint32_t*)(ref->data +
j*out->bytes_per_line +
i*out->bits_per_pixel/8));
}
len += sprintf(buf+len, "\n");
}
}
static void test_compare_fallback(struct test *t,
Drawable out_draw, XRenderPictFormat *out_format,
Drawable ref_draw, XRenderPictFormat *ref_format,
int x, int y, int w, int h)
{
XImage *out_image, *ref_image;
char *out, *ref;
char buf[600];
uint32_t mask;
int i, j;
die_unless(out_format->depth == ref_format->depth);
out_image = XGetImage(t->out.dpy, out_draw,
x, y, w, h,
AllPlanes, ZPixmap);
out = out_image->data;
ref_image = XGetImage(t->ref.dpy, ref_draw,
x, y, w, h,
AllPlanes, ZPixmap);
ref = ref_image->data;
mask = depth_mask(out_image->depth);
/* Start with an exact comparison. However, one quicky desires
* a fuzzy comparator to hide hardware inaccuracies...
*/
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
uint32_t a = ((uint32_t *)out)[i] & mask;
uint32_t b = ((uint32_t *)ref)[i] & mask;
if (a != b && pixel_difference(a, b) > MAX_DELTA) {
show_pixels(buf,
out_image, ref_image,
i, j, w, h);
die("discrepancy found at (%d+%d, %d+%d): found %08x, expected %08x (delta: %d)\n%s",
x,i, y,j, a, b, pixel_difference(a, b), buf);
}
}
out += out_image->bytes_per_line;
ref += ref_image->bytes_per_line;
}
XDestroyImage(out_image);
XDestroyImage(ref_image);
}
static void
unpremultiply_data (png_structp png, png_row_infop row_info, png_bytep data)
{
unsigned int i;
for (i = 0; i < row_info->rowbytes; i += 4) {
uint8_t *b = &data[i];
uint32_t pixel;
uint8_t alpha;
memcpy (&pixel, b, sizeof (uint32_t));
alpha = (pixel & 0xff000000) >> 24;
if (alpha == 0) {
b[0] = (pixel & 0xff0000) >> 16;
b[1] = (pixel & 0x00ff00) >> 8;
b[2] = (pixel & 0x0000ff) >> 0;
b[3] = 0xff;
} else {
b[0] = (((pixel & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
b[1] = (((pixel & 0x00ff00) >> 8) * 255 + alpha / 2) / alpha;
b[2] = (((pixel & 0x0000ff) >> 0) * 255 + alpha / 2) / alpha;
b[3] = alpha;
}
}
}
static void save_image(XImage *image, const char *filename)
{
FILE *file;
png_struct *png = NULL;
png_info *info = NULL;
png_byte **rows = NULL;
int i;
file = fopen(filename, "w");
if (file == NULL)
return;
png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png == NULL)
goto out;
info = png_create_info_struct(png);
if (info == NULL)
goto out;
rows = png_malloc(png, sizeof(png_byte *) * image->height);
if (rows == NULL)
goto out;
for (i = 0; i < image->height; i++)
rows[i] = (png_byte *)(image->data + image->bytes_per_line * i);
if (setjmp(png_jmpbuf(png)))
goto out;
png_set_IHDR(png, info,
image->width, image->height, 8,
PNG_COLOR_TYPE_RGB_ALPHA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_init_io(png, file);
png_write_info(png, info);
png_set_write_user_transform_fn(png, unpremultiply_data);
png_write_image(png, rows);
png_write_end(png, info);
out:
if (rows)
png_free(png, rows);
png_destroy_write_struct(&png, &info);
fclose(file);
}
void test_compare(struct test *t,
Drawable out_draw, XRenderPictFormat *out_format,
Drawable ref_draw, XRenderPictFormat *ref_format,
int x, int y, int w, int h,
const char *info)
{
XImage out_image, ref_image;
uint32_t *out, *ref;
char buf[600];
uint32_t mask;
int i, j;
if (w * h * 4 > t->out.max_shm_size)
return test_compare_fallback(t,
out_draw, out_format,
ref_draw, ref_format,
x, y, w, h);
test_init_image(&out_image, &t->out.shm, out_format, w, h);
test_init_image(&ref_image, &t->ref.shm, ref_format, w, h);
die_unless(out_image.depth == ref_image.depth);
die_unless(out_image.bits_per_pixel == ref_image.bits_per_pixel);
die_unless(out_image.bits_per_pixel == 32);
XShmGetImage(t->out.dpy, out_draw, &out_image, x, y, AllPlanes);
out = (uint32_t *)out_image.data;
XShmGetImage(t->ref.dpy, ref_draw, &ref_image, x, y, AllPlanes);
ref = (uint32_t *)ref_image.data;
/* Start with an exact comparison. However, one quicky desires
* a fuzzy comparator to hide hardware inaccuracies...
*/
mask = depth_mask(out_image.depth);
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
uint32_t a = out[i] & mask;
uint32_t b = ref[i] & mask;
if (a != b && pixel_difference(a, b) > MAX_DELTA) {
show_pixels(buf,
&out_image, &ref_image,
i, j, w, h);
save_image(&out_image, "out.png");
save_image(&ref_image, "ref.png");
die("discrepancy found at (%d+%d, %d+%d): found %08x, expected %08x (delta: %d)\n%s%s\n",
x,i, y,j, a, b, pixel_difference(a, b), buf, info);
}
}
out = (uint32_t *)((char *)out + out_image.bytes_per_line);
ref = (uint32_t *)((char *)ref + ref_image.bytes_per_line);
}
}
static int
_native_byte_order_lsb(void)
{
int x = 1;
return *((char *) &x) == 1;
}
void
test_init_image(XImage *ximage,
XShmSegmentInfo *shm,
XRenderPictFormat *format,
int width, int height)
{
int native_byte_order = _native_byte_order_lsb() ? LSBFirst : MSBFirst;
ximage->width = width;
ximage->height = height;
ximage->format = ZPixmap;
ximage->data = shm->shmaddr;
ximage->obdata = (void *)shm;
ximage->byte_order = native_byte_order;
ximage->bitmap_unit = 32;
ximage->bitmap_bit_order = native_byte_order;
ximage->bitmap_pad = 32;
ximage->depth = format->depth;
ximage->bytes_per_line = 4*width;
ximage->bits_per_pixel = 32;
ximage->red_mask = 0xff << 16;
ximage->green_mask = 0xff << 8;
ximage->blue_mask = 0xff << 0;
ximage->xoffset = 0;
XInitImage(ximage);
}
|