2 * Copyright © 2008 Keith Packard <keithp@keithp.com>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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.
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.
34 #include "../aux/xcb_aux.h"
35 #include "xcb_image.h"
37 xcb_image_format_t formats[] = {
38 XCB_IMAGE_FORMAT_Z_PIXMAP,
39 XCB_IMAGE_FORMAT_XY_PIXMAP,
40 XCB_IMAGE_FORMAT_XY_BITMAP,
42 #define SIZE(a) (sizeof (a) / sizeof (a[0]))
43 #define NFORMAT SIZE(formats)
48 #define NBPP SIZE(bpps)
53 #define NUNIT SIZE(units)
55 xcb_image_order_t byte_orders[] = {
56 XCB_IMAGE_ORDER_LSB_FIRST,
57 XCB_IMAGE_ORDER_MSB_FIRST
59 #define NBYTE_ORDER SIZE(byte_orders)
62 uint32_t pixel_mask (int bpp)
66 return (1 << bpp) - 1;
70 compare_image(xcb_image_t *a, xcb_image_t *b)
73 uint32_t a_pixel, b_pixel;
74 uint32_t mask = pixel_mask (a->bpp) & pixel_mask (b->bpp);
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);
93 create_test_image (void)
95 xcb_image_t *test_image;
98 test_image = xcb_image_create(test_width, test_height,
99 XCB_IMAGE_FORMAT_Z_PIXMAP,
104 XCB_IMAGE_ORDER_LSB_FIRST,
105 XCB_IMAGE_ORDER_LSB_FIRST,
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);
118 convert_test (xcb_image_t *test, xcb_image_t *a)
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));
128 order_name (xcb_image_order_t order) {
129 if (order == XCB_IMAGE_ORDER_MSB_FIRST)
136 print_format (xcb_image_t *image)
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;
143 fprintf (stderr, " pad: %d bpp: %d depth: %d unit: %d planemask: 0x%08x",
144 image->scanline_pad, image->bpp, image->depth, image->unit,
146 fprintf (stderr, " byte order: %s bit order: %s stride: %d\n",
147 order_name (image->byte_order), order_name (image->bit_order),
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;
163 test_image = create_test_image ();
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)
178 if (dst_bpp > 4 && dst_unit != dst_bpp)
181 if (dst_format == XCB_IMAGE_FORMAT_XY_BITMAP && dst_bpp != 1)
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)
188 if (src_bpp > 4 && src_unit != src_bpp)
191 if (src_format == XCB_IMAGE_FORMAT_XY_BITMAP && src_bpp != 1)
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)
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)
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);
215 xcb_image_convert (src_image, dst_image);
216 if (!compare_image (src_image, dst_image)) {
218 * Call the conversion function again so that debugging
221 fprintf (stderr, "Conversion failure:\n");
222 fprintf (stderr, "src format: "); print_format(src_image);
223 fprintf (stderr, "dst format: "); print_format(dst_image);
226 xcb_image_destroy (src_image);
227 xcb_image_destroy (dst_image);