summaryrefslogtreecommitdiff
path: root/test/test_grab.cpp
blob: 06165cb2193cad57d2d3ceab9c2b5f47d5ac7e19 (plain)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <gtest/gtest.h>

#include <X11/Xutil.h>
#include <Imlib2.h>

#include "config.h"
#include "test_common.h"

typedef struct {
   Display            *dpy;
   Window              root;
   Visual             *vis;
   Colormap            cmap;
   Visual             *argb_vis;
   Colormap            argb_cmap;
   int                 depth;
   int                 scale;
   const char         *test;
} xd_t;

static xd_t         xd;
int                 debug = 0;

#define D(...)  if (debug) printf(__VA_ARGS__)
#define D2(...) if (debug > 1) printf(__VA_ARGS__)

static Visual      *
_x11_vis_argb(void)
{
   XVisualInfo        *xvi, xvit;
   int                 i, num;
   Visual             *vis;

   xvit.screen = DefaultScreen(xd.dpy);
   xvit.depth = 32;
   xvit.c_class = TrueColor;

   xvi = XGetVisualInfo(xd.dpy,
                        VisualScreenMask | VisualDepthMask | VisualClassMask,
                        &xvit, &num);
   if (!xvi)
      return NULL;

   for (i = 0; i < num; i++)
     {
        if (xvi[i].depth == 32)
           break;
     }

   vis = (i < num) ? xvi[i].visual : NULL;

   XFree(xvi);

   xd.argb_vis = vis;
   xd.argb_cmap = XCreateColormap(xd.dpy, xd.root, vis, AllocNone);

   return vis;
}

static void
_x11_init(int depth)
{
   xd.dpy = XOpenDisplay(NULL);
   if (!xd.dpy)
     {
        fprintf(stderr, "Can't open display\n");
        exit(1);
     }

   xd.root = DefaultRootWindow(xd.dpy);
   if (depth == 32)
     {
        xd.vis = _x11_vis_argb();
        xd.cmap = xd.argb_cmap;
     }
   else
     {
        xd.vis = DefaultVisual(xd.dpy, DefaultScreen(xd.dpy));
        xd.cmap = DefaultColormap(xd.dpy, DefaultScreen(xd.dpy));
     }

   imlib_context_set_display(xd.dpy);
   imlib_context_set_visual(xd.vis);
   imlib_context_set_colormap(xd.cmap);
}

static void
_x11_fini(void)
{
   XCloseDisplay(xd.dpy);
}

static              Pixmap
_pmap_mk_fill_solid(int w, int h, unsigned int color)
{
   XGCValues           gcv;
   GC                  gc;
   Pixmap              pmap;

   pmap = XCreatePixmap(xd.dpy, xd.root, w, h, xd.depth);

   gcv.foreground = color;
   gcv.graphics_exposures = False;
   gc = XCreateGC(xd.dpy, pmap, GCForeground | GCGraphicsExposures, &gcv);

   XFillRectangle(xd.dpy, pmap, gc, 0, 0, w, h);

   XFreeGC(xd.dpy, gc);

   return pmap;
}

static void
_img_dump(Imlib_Image im, const char *file)
{
   static int          seqn = 0;
   char                buf[1024];

   snprintf(buf, sizeof(buf), file, seqn++);
   imlib_context_set_image(im);
   imlib_save_image(buf);
}

DATA32              color = 0;

static void
_test_grab_1(int w, int h, int x0, int y0)
{
   Imlib_Image         im;
   DATA32             *dptr, pix, col;
   int                 x, y, err;
   int                 xs, ys, ws, hs;
   char                buf[128];

   xs = x0;
   ws = w;
   ys = y0;
   hs = h;
   if (xd.scale > 0)
     {
        xs *= xd.scale;
        ws *= xd.scale;
        ys *= xd.scale;
        hs *= xd.scale;
     }
   if (xd.scale < 0)
     {
        xs = (xs & ~1) / -xd.scale;
        ws /= -xd.scale;
        ys = (ys & ~1) / -xd.scale;
        hs /= -xd.scale;
     }

   D("%s: %3dx%3d(%3d,%3d) -> %3dx%3d(%d,%d)\n", __func__,
     w, h, x0, y0, ws, hs, xs, ys);

   if (xd.scale == 0)
      im = imlib_create_image_from_drawable(None, x0, y0, w, h, 0);
   else
      im = imlib_create_scaled_image_from_drawable(None, x0, y0, w, h,
                                                   ws, hs, 0, 0);
   imlib_context_set_image(im);
   snprintf(buf, sizeof(buf), "%s/%s-%%d.png", IMG_GEN, xd.test);
   _img_dump(im, buf);

   dptr = imlib_image_get_data_for_reading_only();
   err = 0;
   for (y = 0; y < hs; y++)
     {
        for (x = 0; x < ws; x++)
          {
             pix = *dptr++;
             if (xs + x < 0 || ys + y < 0 || xs + x >= ws || ys + y >= hs)
               {
#if 1
                  // FIXME - Pixels outside source drawable are not properly initialized
                  if (x0 != 0 || y0 != 0)
                     continue;
#endif
                  col = 0x00000000;     // Use 0xff000000 if non-alpa
               }
             else
               {
                  col = color;
               }
             if (pix != col)
               {
                  D2("%3d,%3d (%3d,%3d): %08x != %08x\n",
                     x, y, x0, y0, pix, col);
                  err = 1;
               }
          }
     }

   EXPECT_FALSE(err);

   imlib_free_image_and_decache();
}

static void
_test_grab(const char *test, int depth, int scale, int opt)
{
   Pixmap              pmap;
   int                 w, h, d;

   color = 0xffff0000;

   _x11_init(depth);

   xd.depth = depth;
   xd.scale = scale;
   xd.test = test;

   w = 32;
   h = 45;

   pmap = _pmap_mk_fill_solid(w, h, color);
   imlib_context_set_drawable(pmap);

   switch (opt)
     {
     case 0:
        _test_grab_1(w, h, 0, 0);
        break;
     case 1:
        d = 1;
        _test_grab_1(w, h, -d, -d);
        _test_grab_1(w, h, -d, d);
        _test_grab_1(w, h, d, d);
        _test_grab_1(w, h, d, -d);
        if (scale < 0)
          {
             d = 2;
             _test_grab_1(w, h, -d, -d);
             _test_grab_1(w, h, -d, d);
             _test_grab_1(w, h, d, d);
             _test_grab_1(w, h, d, -d);
          }
        break;
     }

   XFreePixmap(xd.dpy, pmap);

   _x11_fini();
}

TEST(GRAB, grab_noof_24_s0)
{
   _test_grab("grab_noof_24_s0", 24, 0, 0);
}

TEST(GRAB, grab_noof_24_s1)
{
   _test_grab("grab_noof_24_s1", 24, 1, 0);
}

TEST(GRAB, grab_noof_24_su2)
{
   _test_grab("grab_noof_24_su2", 24, 2, 0);
}

TEST(GRAB, grab_noof_24_sd2)
{
   _test_grab("grab_noof_24_sd2", 24, -2, 0);
}

TEST(GRAB, grab_noof_32_s0)
{
   _test_grab("grab_noof_32_s0", 32, 0, 0);
}

TEST(GRAB, grab_noof_32_s1)
{
   _test_grab("grab_noof_32_s1", 32, 1, 0);
}

TEST(GRAB, grab_noof_32_su2)
{
   _test_grab("grab_noof_32_su2", 32, 2, 0);
}

TEST(GRAB, grab_noof_32_sd2)
{
   _test_grab("grab_noof_32_sd2", 32, -2, 0);
}

TEST(GRAB, grab_offs_24_s0)
{
   _test_grab("grab_offs_24_s0", 24, 0, 1);
}

TEST(GRAB, grab_offs_24_s1)
{
   _test_grab("grab_offs_24_s1", 24, 1, 1);
}

TEST(GRAB, grab_offs_24_su2)
{
   _test_grab("grab_offs_24_su2", 24, 2, 1);
}

TEST(GRAB, grab_offs_24_sd2)
{
   _test_grab("grab_offs_24_sd2", 24, -2, 1);
}

TEST(GRAB, grab_offs_32_s0)
{
   _test_grab("grab_offs_32_s0", 32, 0, 1);
}

TEST(GRAB, grab_offs_32_s1)
{
   _test_grab("grab_offs_32_s1", 32, 1, 1);
}

TEST(GRAB, grab_offs_32_su2)
{
   _test_grab("grab_offs_32_su2", 32, 2, 1);
}

TEST(GRAB, grab_offs_32_sd2)
{
   _test_grab("grab_offs_32_sd2", 32, -2, 1);
}

int
main(int argc, char **argv)
{
   const char         *s;

   ::testing::InitGoogleTest(&argc, argv);

   for (argc--, argv++; argc > 0; argc--, argv++)
     {
        s = argv[0];
        if (*s++ != '-')
           break;
        switch (*s)
          {
          case 'd':
             while (*s++ == 'd')
                debug++;
             break;
          }
     }

   mkdir(IMG_GEN, 0755);

   return RUN_ALL_TESTS();
}