summaryrefslogtreecommitdiff
path: root/lib/pixman/test/check-formats.c
blob: 7edc198c12b0f3a61dbf9d4b7b5b32f258db8707 (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
352
#include <ctype.h>
#include "utils.h"

static int
check_op (pixman_op_t          op,
          pixman_format_code_t src_format,
          pixman_format_code_t dest_format)
{
    uint32_t src_alpha_mask, src_green_mask;
    uint32_t dest_alpha_mask, dest_green_mask;
    pixel_checker_t src_checker, dest_checker;
    pixman_image_t *si, *di;
    uint32_t sa, sg, da, dg;
    uint32_t s, d;
    int retval = 0;

    pixel_checker_init (&src_checker, src_format);
    pixel_checker_init (&dest_checker, dest_format);

    pixel_checker_get_masks (
        &src_checker, &src_alpha_mask, NULL, &src_green_mask, NULL);
    pixel_checker_get_masks (
        &dest_checker, &dest_alpha_mask, NULL, &dest_green_mask, NULL);

    /* printf ("masks: %x %x %x %x\n", */
    /* 	    src_alpha_mask, src_green_mask, */
    /* 	    dest_alpha_mask, dest_green_mask); */

    si = pixman_image_create_bits (src_format, 1, 1, &s, 4);
    di = pixman_image_create_bits (dest_format, 1, 1, &d, 4);

    sa = 0;
    do
    {
        sg = 0;
        do
        {
            da = 0;
            do
            {
                dg = 0;
                do
                {
                    color_t src_color, dest_color, result_color;
                    uint32_t orig_d;

                    s = sa | sg;
                    d = da | dg;

                    orig_d = d;

		    pixel_checker_convert_pixel_to_color (&src_checker, s, &src_color);
		    pixel_checker_convert_pixel_to_color (&dest_checker, d, &dest_color);

		    do_composite (op, &src_color, NULL, &dest_color, &result_color, FALSE);


		    if (!is_little_endian())
                    {
			s <<= 32 - PIXMAN_FORMAT_BPP (src_format);
			d <<= 32 - PIXMAN_FORMAT_BPP (dest_format);
                    }

		    pixman_image_composite32 (op, si, NULL, di,
					      0, 0, 0, 0, 0, 0, 1, 1);

		    if (!is_little_endian())
                        d >>= (32 - PIXMAN_FORMAT_BPP (dest_format));

                    if (!pixel_checker_check (&dest_checker, d, &result_color))
                    {
                        printf ("---- test failed ----\n");
                        printf ("operator: %-32s\n", operator_name (op));
                        printf ("source:   %-12s pixel: %08x\n", format_name (src_format), s);
                        printf ("dest:     %-12s pixel: %08x\n", format_name (dest_format), orig_d);
                        printf ("got:      %-12s pixel: %08x\n", format_name (dest_format), d);

                        retval = 1;
                    }

                    dg -= dest_green_mask;
                    dg &= dest_green_mask;
                }
                while (dg != 0);

                da -= dest_alpha_mask;
                da &= dest_alpha_mask;
            }
            while (da != 0);

            sg -= src_green_mask;
            sg &= src_green_mask;
        }
        while (sg != 0);

        sa -= src_alpha_mask;
        sa &= src_alpha_mask;
    }
    while (sa != 0);

    pixman_image_unref (si);
    pixman_image_unref (di);

    return retval;
}

static const pixman_op_t op_list[] =
{
    PIXMAN_OP_CLEAR,
    PIXMAN_OP_SRC,
    PIXMAN_OP_DST,
    PIXMAN_OP_OVER,
    PIXMAN_OP_OVER_REVERSE,
    PIXMAN_OP_IN,
    PIXMAN_OP_IN_REVERSE,
    PIXMAN_OP_OUT,
    PIXMAN_OP_OUT_REVERSE,
    PIXMAN_OP_ATOP,
    PIXMAN_OP_ATOP_REVERSE,
    PIXMAN_OP_XOR,
    PIXMAN_OP_ADD,
    PIXMAN_OP_SATURATE,

    PIXMAN_OP_DISJOINT_CLEAR,
    PIXMAN_OP_DISJOINT_SRC,
    PIXMAN_OP_DISJOINT_DST,
    PIXMAN_OP_DISJOINT_OVER,
    PIXMAN_OP_DISJOINT_OVER_REVERSE,
    PIXMAN_OP_DISJOINT_IN,
    PIXMAN_OP_DISJOINT_IN_REVERSE,
    PIXMAN_OP_DISJOINT_OUT,
    PIXMAN_OP_DISJOINT_OUT_REVERSE,
    PIXMAN_OP_DISJOINT_ATOP,
    PIXMAN_OP_DISJOINT_ATOP_REVERSE,
    PIXMAN_OP_DISJOINT_XOR,

    PIXMAN_OP_CONJOINT_CLEAR,
    PIXMAN_OP_CONJOINT_SRC,
    PIXMAN_OP_CONJOINT_DST,
    PIXMAN_OP_CONJOINT_OVER,
    PIXMAN_OP_CONJOINT_OVER_REVERSE,
    PIXMAN_OP_CONJOINT_IN,
    PIXMAN_OP_CONJOINT_IN_REVERSE,
    PIXMAN_OP_CONJOINT_OUT,
    PIXMAN_OP_CONJOINT_OUT_REVERSE,
    PIXMAN_OP_CONJOINT_ATOP,
    PIXMAN_OP_CONJOINT_ATOP_REVERSE,
    PIXMAN_OP_CONJOINT_XOR,
};

static const pixman_format_code_t format_list[] =
{
    PIXMAN_a8r8g8b8,
    PIXMAN_x8r8g8b8,
    PIXMAN_a8b8g8r8,
    PIXMAN_x8b8g8r8,
    PIXMAN_b8g8r8a8,
    PIXMAN_b8g8r8x8,
    PIXMAN_r8g8b8a8,
    PIXMAN_r8g8b8x8,
    PIXMAN_x14r6g6b6,
    PIXMAN_x2r10g10b10,
    PIXMAN_a2r10g10b10,
    PIXMAN_x2b10g10r10,
    PIXMAN_a2b10g10r10,
    PIXMAN_a8r8g8b8_sRGB,
    PIXMAN_r8g8b8,
    PIXMAN_b8g8r8,
    PIXMAN_r5g6b5,
    PIXMAN_b5g6r5,
    PIXMAN_a1r5g5b5,
    PIXMAN_x1r5g5b5,
    PIXMAN_a1b5g5r5,
    PIXMAN_x1b5g5r5,
    PIXMAN_a4r4g4b4,
    PIXMAN_x4r4g4b4,
    PIXMAN_a4b4g4r4,
    PIXMAN_x4b4g4r4,
    PIXMAN_a8,
    PIXMAN_r3g3b2,
    PIXMAN_b2g3r3,
    PIXMAN_a2r2g2b2,
    PIXMAN_a2b2g2r2,
    PIXMAN_x4a4,
    PIXMAN_a4,
    PIXMAN_r1g2b1,
    PIXMAN_b1g2r1,
    PIXMAN_a1r1g1b1,
    PIXMAN_a1b1g1r1,
    PIXMAN_a1,
};

static pixman_format_code_t
format_from_string (const char *s)
{
    int i;

    for (i = 0; i < ARRAY_LENGTH (format_list); ++i)
    {
        if (strcasecmp (format_name (format_list[i]), s) == 0)
            return format_list[i];
    }

    return PIXMAN_null;
}

static void
emit (const char *s, int *n_chars)
{
    *n_chars += printf ("%s,", s);
    if (*n_chars > 60)
    {
        printf ("\n    ");
        *n_chars = 0;
    }
    else
    {
        printf (" ");
        (*n_chars)++;
    }
}

static void
list_formats (void)
{
    int n_chars;
    int i;

    printf ("Formats:\n    ");

    n_chars = 0;
    for (i = 0; i < ARRAY_LENGTH (format_list); ++i)
        emit (format_name (format_list[i]), &n_chars);

    printf ("\n\n");
}

static void
list_operators (void)
{
    char short_name [128] = { 0 };
    int i, n_chars;

    printf ("Operators:\n    ");

    n_chars = 0;
    for (i = 0; i < ARRAY_LENGTH (op_list); ++i)
    {
        pixman_op_t op = op_list[i];
        int j;

        snprintf (short_name, sizeof (short_name) - 1, "%s",
                  operator_name (op) + strlen ("PIXMAN_OP_"));

        for (j = 0; short_name[j] != '\0'; ++j)
            short_name[j] = tolower (short_name[j]);

        emit (short_name, &n_chars);
    }

    printf ("\n\n");
}

static pixman_op_t
operator_from_string (const char *s)
{
    char full_name[128] = { 0 };
    int i;

    snprintf (full_name, (sizeof full_name) - 1, "PIXMAN_OP_%s", s);

    for (i = 0; i < ARRAY_LENGTH (op_list); ++i)
    {
        pixman_op_t op = op_list[i];

        if (strcasecmp (operator_name (op), full_name) == 0)
            return op;
    }

    return PIXMAN_OP_NONE;
}

int
main (int argc, char **argv)
{
    enum { OPTION_OP, OPTION_SRC, OPTION_DEST, LAST_OPTION } option;
    pixman_format_code_t src_fmt, dest_fmt;
    pixman_op_t op;

    op = PIXMAN_OP_NONE;
    src_fmt = PIXMAN_null;
    dest_fmt = PIXMAN_null;

    argc--;
    argv++;

    for (option = OPTION_OP; option < LAST_OPTION; ++option)
    {
        char *arg = NULL;

        if (argc)
        {
            argc--;
            arg = *argv++;
        }

        switch (option)
        {
        case OPTION_OP:
            if (!arg)
                printf ("  - missing operator\n");
            else if ((op = operator_from_string (arg)) == PIXMAN_OP_NONE)
                printf ("  - unknown operator %s\n", arg);
            break;

        case OPTION_SRC:
            if (!arg)
                printf ("  - missing source format\n");
            else if ((src_fmt = format_from_string (arg)) == PIXMAN_null)
                printf ("  - unknown source format %s\n", arg);
            break;

        case OPTION_DEST:
            if (!arg)
                printf ("  - missing destination format\n");
            else if ((dest_fmt = format_from_string (arg)) == PIXMAN_null)
                printf ("  - unknown destination format %s\n", arg);
            break;

        default:
            assert (0);
            break;
        }
    }

    while (argc--)
    {
        op = PIXMAN_OP_NONE;
        printf ("  - unexpected argument: %s\n", *argv++);
    }

    if (op == PIXMAN_OP_NONE || src_fmt == PIXMAN_null || dest_fmt == PIXMAN_null)
    {
        printf ("\nUsage:\n    check-formats <operator> <src-format> <dest-format>\n\n");
        list_operators();
        list_formats();

        return -1;
    }

    return check_op (op, src_fmt, dest_fmt);
}