9f80eec1bd6572e13c9e8bbc4ff42923c1fe5af8
[framework/graphics/pixman.git] / test / fetch-test.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #include <assert.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include "pixman.h"
9
10 #define SIZE 1024
11
12 static pixman_indexed_t mono_palette =
13 {
14     0, { 0x00000000, 0x00ffffff },
15 };
16
17
18 typedef struct {
19     pixman_format_code_t format;
20     int width, height;
21     int stride;
22     uint32_t src[SIZE];
23     uint32_t dst[SIZE];
24     pixman_indexed_t *indexed;
25 } testcase_t;
26
27 static testcase_t testcases[] =
28 {
29     {
30         PIXMAN_a8r8g8b8,
31         2, 2,
32         8,
33         { 0x00112233, 0x44556677,
34           0x8899aabb, 0xccddeeff },
35         { 0x00112233, 0x44556677,
36           0x8899aabb, 0xccddeeff },
37         NULL,
38     },
39     {
40         PIXMAN_r8g8b8a8,
41         2, 2,
42         8,
43         { 0x11223300, 0x55667744,
44           0x99aabb88, 0xddeeffcc },
45         { 0x00112233, 0x44556677,
46           0x8899aabb, 0xccddeeff },
47         NULL,
48     },
49     {
50         PIXMAN_g1,
51         8, 2,
52         4,
53 #ifdef WORDS_BIGENDIAN
54         {
55             0xaa000000,
56             0x55000000
57         },
58 #else
59         {
60             0x00000055,
61             0x000000aa
62         },
63 #endif
64         {
65             0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000,
66             0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff, 0x00000000, 0x00ffffff
67         },
68         &mono_palette,
69     },
70 #if 0
71     {
72         PIXMAN_g8,
73         4, 2,
74         4,
75         { 0x01234567,
76           0x89abcdef },
77         { 0x00010101, 0x00232323, 0x00454545, 0x00676767,
78           0x00898989, 0x00ababab, 0x00cdcdcd, 0x00efefef, },
79     },
80 #endif
81     /* FIXME: make this work on big endian */
82     {
83         PIXMAN_yv12,
84         8, 2,
85         8,
86 #ifdef WORDS_BIGENDIAN
87         {
88             0x00ff00ff, 0x00ff00ff,
89             0xff00ff00, 0xff00ff00,
90             0x80ff8000,
91             0x800080ff
92         },
93 #else
94         {
95             0xff00ff00, 0xff00ff00,
96             0x00ff00ff, 0x00ff00ff,
97             0x0080ff80,
98             0xff800080
99         },
100 #endif
101         {
102             0xff000000, 0xffffffff, 0xffb80000, 0xffffe113,
103             0xff000000, 0xffffffff, 0xff0023ee, 0xff4affff,
104             0xffffffff, 0xff000000, 0xffffe113, 0xffb80000,
105             0xffffffff, 0xff000000, 0xff4affff, 0xff0023ee,
106         },
107     },
108 };
109
110 int n_test_cases = sizeof(testcases)/sizeof(testcases[0]);
111
112
113 static uint32_t
114 reader (const void *src, int size)
115 {
116     switch (size)
117     {
118     case 1:
119         return *(uint8_t *)src;
120     case 2:
121         return *(uint16_t *)src;
122     case 4:
123         return *(uint32_t *)src;
124     default:
125         assert(0);
126         return 0; /* silence MSVC */
127     }
128 }
129
130
131 static void
132 writer (void *src, uint32_t value, int size)
133 {
134     switch (size)
135     {
136     case 1:
137         *(uint8_t *)src = value;
138         break;
139     case 2:
140         *(uint16_t *)src = value;
141         break;
142     case 4:
143         *(uint32_t *)src = value;
144         break;
145     default:
146         assert(0);
147     }
148 }
149
150
151 int
152 main (int argc, char **argv)
153 {
154     uint32_t dst[SIZE];
155     pixman_image_t *src_img;
156     pixman_image_t *dst_img;
157     int i, j, x, y;
158     int ret = 0;
159
160     for (i = 0; i < n_test_cases; ++i)
161     {
162         for (j = 0; j < 2; ++j)
163         {
164             src_img = pixman_image_create_bits (testcases[i].format,
165                                                 testcases[i].width,
166                                                 testcases[i].height,
167                                                 testcases[i].src,
168                                                 testcases[i].stride);
169             pixman_image_set_indexed(src_img, testcases[i].indexed);
170
171             dst_img = pixman_image_create_bits (PIXMAN_a8r8g8b8,
172                                                 testcases[i].width,
173                                                 testcases[i].height,
174                                                 dst,
175                                                 testcases[i].width*4);
176
177             if (j)
178             {
179                 pixman_image_set_accessors (src_img, reader, writer);
180                 pixman_image_set_accessors (dst_img, reader, writer);
181             }
182
183             pixman_image_composite (PIXMAN_OP_SRC, src_img, NULL, dst_img,
184                                     0, 0, 0, 0, 0, 0, testcases[i].width, testcases[i].height);
185
186             pixman_image_unref (src_img);
187             pixman_image_unref (dst_img);
188
189             for (y = 0; y < testcases[i].height; ++y)
190             {
191                 for (x = 0; x < testcases[i].width; ++x)
192                 {
193                     int offset = y * testcases[i].width + x;
194
195                     if (dst[offset] != testcases[i].dst[offset])
196                     {
197                         printf ("test %i%c: pixel mismatch at (x=%d,y=%d): %08x expected, %08x obtained\n",
198                                 i + 1, 'a' + j,
199                                 x, y,
200                                 testcases[i].dst[offset], dst[offset]);
201                         ret = 1;
202                     }
203                 }
204             }
205         }
206     }
207
208     return ret;
209 }