Git init
[framework/uifw/xorg/xcb/xcb-util.git] / image / test_swap.c
1 /*
2  * Copyright © 2008 Keith Packard <keithp@keithp.com>
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Except as contained in this notice, the names of the authors or
24  * their institutions shall not be used in advertising or otherwise to
25  * promote the sale, use or other dealings in this Software without
26  * prior written authorization from the authors.
27  */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <xcb/xcb.h>
34 #include "../aux/xcb_aux.h"
35 #include "xcb_image.h"
36
37 xcb_image_format_t  formats[] = {
38     XCB_IMAGE_FORMAT_Z_PIXMAP,
39     XCB_IMAGE_FORMAT_XY_PIXMAP,
40     XCB_IMAGE_FORMAT_XY_BITMAP,
41 };
42 #define SIZE(a) (sizeof (a) / sizeof (a[0]))
43 #define NFORMAT SIZE(formats)
44
45 int bpps[] = {
46     1, 4, 8, 16, 32
47 };
48 #define NBPP SIZE(bpps)
49
50 int units[] = {
51     8, 16, 32
52 };
53 #define NUNIT SIZE(units)
54
55 xcb_image_order_t   byte_orders[] = {
56     XCB_IMAGE_ORDER_LSB_FIRST,
57     XCB_IMAGE_ORDER_MSB_FIRST
58 };
59 #define NBYTE_ORDER SIZE(byte_orders)
60
61 static
62 uint32_t pixel_mask (int bpp)
63 {
64     if (bpp == 32)
65         return 0xffffffff;
66     return (1 << bpp) - 1;
67 }
68
69 int
70 compare_image(xcb_image_t *a, xcb_image_t *b)
71 {
72     int x, y;
73     uint32_t    a_pixel, b_pixel;
74     uint32_t    mask = pixel_mask (a->bpp) & pixel_mask (b->bpp);
75
76     for (y = 0; y < a->height; y++)
77         for (x = 0; x < a->width; x++) {
78             a_pixel = xcb_image_get_pixel (a, x, y) & mask;
79             b_pixel = xcb_image_get_pixel (b, x, y) & mask;
80             if (a_pixel != b_pixel) {
81                 fprintf (stderr, "fail at %d,%d: 0x%x != 0x%x\n",
82                          x, y, a_pixel, b_pixel);
83                 return 0;
84             }
85         }
86     return 1;
87 }
88
89 #define test_width  63
90 #define test_height 2
91
92 static xcb_image_t *
93 create_test_image (void)
94 {
95     xcb_image_t *test_image;
96     int         x, y;
97     uint32_t    pixel;
98     test_image = xcb_image_create(test_width, test_height,
99                                   XCB_IMAGE_FORMAT_Z_PIXMAP,
100                                   32,
101                                   32,
102                                   32,
103                                   32,
104                                   XCB_IMAGE_ORDER_LSB_FIRST,
105                                   XCB_IMAGE_ORDER_LSB_FIRST,
106                                   NULL, 0, NULL);
107
108     pixel = 0;
109     for (y = 0; y < test_height; y++)
110         for (x = 0; x < test_width; x++) {
111             xcb_image_put_pixel (test_image, x, y, pixel);
112             pixel++;
113         }
114     return test_image;
115 }
116
117 static void
118 convert_test (xcb_image_t *test, xcb_image_t *a)
119 {
120     int         x, y;
121
122     for (y = 0; y < test->height; y++)
123         for (x = 0; x < test->width; x++)
124             xcb_image_put_pixel (a, x, y, xcb_image_get_pixel (test, x, y));
125 }
126
127 static char *
128 order_name (xcb_image_order_t order) {
129   if (order == XCB_IMAGE_ORDER_MSB_FIRST)
130     return "MSB";
131   else
132     return "LSB";
133 }
134
135 static void
136 print_format (xcb_image_t *image)
137 {
138   switch (image->format) {
139   case XCB_IMAGE_FORMAT_Z_PIXMAP: fprintf (stderr, "Z pixmap"); break;
140   case XCB_IMAGE_FORMAT_XY_PIXMAP: fprintf (stderr, "XY pixmap"); break;
141   case XCB_IMAGE_FORMAT_XY_BITMAP: fprintf (stderr, "XY bitmap"); break;
142   }
143   fprintf (stderr, " pad: %d bpp: %d depth: %d unit: %d planemask: 0x%08x",
144            image->scanline_pad, image->bpp, image->depth, image->unit,
145            image->plane_mask);
146   fprintf (stderr, " byte order: %s bit order: %s stride: %d\n",
147            order_name (image->byte_order), order_name (image->bit_order),
148            image->stride);
149 }
150
151 int main (int argc, char **argv) {
152   xcb_image_t   *test_image;
153   xcb_image_t   *src_image;
154   xcb_image_t   *dst_image;
155   int           dst_format_i, dst_bpp_i, dst_unit_i, dst_byte_order_i, dst_bit_order_i;
156   int           src_format_i, src_bpp_i, src_unit_i, src_byte_order_i, src_bit_order_i;
157   xcb_image_format_t    dst_format, src_format;
158   int           dst_bpp, src_bpp;
159   int           dst_unit, src_unit;
160   int           dst_byte_order, src_byte_order;
161   int           dst_bit_order, src_bit_order;
162
163   test_image = create_test_image ();
164
165   for (dst_format_i = 0; dst_format_i < NFORMAT; dst_format_i++) {
166     dst_format = formats[dst_format_i];
167     for (src_format_i = 0; src_format_i < NFORMAT; src_format_i++) {
168       src_format = formats[src_format_i];
169       for (dst_bpp_i = 0; dst_bpp_i < NBPP; dst_bpp_i++) {
170         dst_bpp = bpps[dst_bpp_i];
171         for (src_bpp_i = 0; src_bpp_i < NBPP; src_bpp_i++) {
172           src_bpp = bpps[src_bpp_i];
173           for (dst_unit_i = 0; dst_unit_i < NUNIT; dst_unit_i++) {
174             dst_unit = units[dst_unit_i];
175             if (dst_format == XCB_IMAGE_FORMAT_Z_PIXMAP) {
176               if (dst_bpp == 4 && dst_unit != 8)
177                 continue;
178               if (dst_bpp > 4 && dst_unit != dst_bpp)
179                 continue;
180             }
181             if (dst_format == XCB_IMAGE_FORMAT_XY_BITMAP && dst_bpp != 1)
182               continue;
183             for (src_unit_i = 0; src_unit_i < NUNIT; src_unit_i++) {
184               src_unit = units[src_unit_i];
185               if (src_format == XCB_IMAGE_FORMAT_Z_PIXMAP) {
186                 if (src_bpp == 4 && src_unit != 8)
187                   continue;
188                 if (src_bpp > 4 && src_unit != src_bpp)
189                   continue;
190               }
191               if (src_format == XCB_IMAGE_FORMAT_XY_BITMAP && src_bpp != 1)
192                 continue;
193               for (dst_byte_order_i = 0; dst_byte_order_i < NBYTE_ORDER; dst_byte_order_i++) {
194                 dst_byte_order = byte_orders[dst_byte_order_i];
195                 for (src_byte_order_i = 0; src_byte_order_i < NBYTE_ORDER; src_byte_order_i++) {
196                   src_byte_order = byte_orders[src_byte_order_i];
197                   for (dst_bit_order_i = 0; dst_bit_order_i < NBYTE_ORDER; dst_bit_order_i++) {
198                     dst_bit_order = byte_orders[dst_bit_order_i];
199                     if (dst_format == XCB_IMAGE_FORMAT_Z_PIXMAP && dst_bit_order != dst_byte_order)
200                       continue;
201                     for (src_bit_order_i = 0; src_bit_order_i < NBYTE_ORDER; src_bit_order_i++) {
202                       src_bit_order = byte_orders[src_bit_order_i];
203                       if (src_format == XCB_IMAGE_FORMAT_Z_PIXMAP && src_bit_order != src_byte_order)
204                         continue;
205                       src_image = xcb_image_create (test_width, test_height, src_format, 32, src_bpp, src_bpp, src_unit,
206                                                     src_byte_order, src_bit_order, NULL, 0, NULL);
207                       dst_image = xcb_image_create (test_width, test_height, dst_format, 32, dst_bpp, dst_bpp, dst_unit,
208                                                     dst_byte_order, dst_bit_order, NULL, 0, NULL);
209                       convert_test (test_image, src_image);
210                       if (!compare_image (test_image, src_image)) {
211                         fprintf (stderr, "Initialization failure:\n");
212                         fprintf (stderr, "src format: "); print_format(src_image);
213                         exit (1);
214                       }
215                       xcb_image_convert (src_image, dst_image);
216                       if (!compare_image (src_image, dst_image)) {
217                         /*
218                          * Call the conversion function again so that debugging
219                          * is easier
220                          */
221                         fprintf (stderr, "Conversion failure:\n");
222                         fprintf (stderr, "src format: "); print_format(src_image);
223                         fprintf (stderr, "dst format: "); print_format(dst_image);
224                         exit (1);
225                       }
226                       xcb_image_destroy (src_image);
227                       xcb_image_destroy (dst_image);
228                     }
229                   }
230                 }
231               }
232             }
233           }
234         }
235       }
236     }
237   }
238   return 0;
239 }