summaryrefslogtreecommitdiff
path: root/image/test_swap.c
blob: 800e4518989488fef5a55528d93a7447baa2090b (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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <xcb/xcb.h>
#include "../aux/xcb_aux.h"
#include "xcb_image.h"

xcb_image_format_t  formats[] = {
    XCB_IMAGE_FORMAT_Z_PIXMAP,
    XCB_IMAGE_FORMAT_XY_PIXMAP,
    XCB_IMAGE_FORMAT_XY_BITMAP,
};
#define SIZE(a) (sizeof (a) / sizeof (a[0]))
#define NFORMAT SIZE(formats)

int bpps[] = {
    1, 4, 8, 16, 32
};
#define NBPP SIZE(bpps)

int units[] = {
    8, 16, 32
};
#define NUNIT SIZE(units)

xcb_image_order_t   byte_orders[] = {
    XCB_IMAGE_ORDER_LSB_FIRST,
    XCB_IMAGE_ORDER_MSB_FIRST
};
#define NBYTE_ORDER SIZE(byte_orders)

static
uint32_t pixel_mask (int bpp)
{
    if (bpp == 32)
	return 0xffffffff;
    return (1 << bpp) - 1;
}

int
compare_image(xcb_image_t *a, xcb_image_t *b)
{
    int x, y;
    uint32_t	a_pixel, b_pixel;
    uint32_t	mask = pixel_mask (a->bpp) & pixel_mask (b->bpp);

    for (y = 0; y < a->height; y++)
	for (x = 0; x < a->width; x++) {
	    a_pixel = xcb_image_get_pixel (a, x, y) & mask;
	    b_pixel = xcb_image_get_pixel (b, x, y) & mask;
	    if (a_pixel != b_pixel) {
		fprintf (stderr, "fail at %d,%d: 0x%x != 0x%x\n",
			 x, y, a_pixel, b_pixel);
		return 0;
	    }
	}
    return 1;
}

#define test_width  63
#define test_height 2

static xcb_image_t *
create_test_image (void)
{
    xcb_image_t	*test_image;
    int		x, y;
    uint32_t	pixel;
    test_image = xcb_image_create(test_width, test_height,
				  XCB_IMAGE_FORMAT_Z_PIXMAP,
				  32,
				  32,
				  32,
				  32,
				  XCB_IMAGE_ORDER_LSB_FIRST,
				  XCB_IMAGE_ORDER_LSB_FIRST,
				  NULL, 0, NULL);

    pixel = 0;
    for (y = 0; y < test_height; y++)
	for (x = 0; x < test_width; x++) {
	    xcb_image_put_pixel (test_image, x, y, pixel);
	    pixel++;
	}
    return test_image;
}

static void
convert_test (xcb_image_t *test, xcb_image_t *a)
{
    int		x, y;

    for (y = 0; y < test->height; y++)
	for (x = 0; x < test->width; x++)
	    xcb_image_put_pixel (a, x, y, xcb_image_get_pixel (test, x, y));
}

static char *
order_name (xcb_image_order_t order) {
  if (order == XCB_IMAGE_ORDER_MSB_FIRST)
    return "MSB";
  else
    return "LSB";
}

static void
print_format (xcb_image_t *image)
{
  switch (image->format) {
  case XCB_IMAGE_FORMAT_Z_PIXMAP: fprintf (stderr, "Z pixmap"); break;
  case XCB_IMAGE_FORMAT_XY_PIXMAP: fprintf (stderr, "XY pixmap"); break;
  case XCB_IMAGE_FORMAT_XY_BITMAP: fprintf (stderr, "XY bitmap"); break;
  }
  fprintf (stderr, " pad: %d bpp: %d depth: %d unit: %d planemask: 0x%08x",
	   image->scanline_pad, image->bpp, image->depth, image->unit,
	   image->plane_mask);
  fprintf (stderr, " byte order: %s bit order: %s stride: %d\n",
	   order_name (image->byte_order), order_name (image->bit_order),
	   image->stride);
}

int main (int argc, char **argv) {
  xcb_image_t	*test_image;
  xcb_image_t	*src_image;
  xcb_image_t	*dst_image;
  int		dst_format_i, dst_bpp_i, dst_unit_i, dst_byte_order_i, dst_bit_order_i;
  int		src_format_i, src_bpp_i, src_unit_i, src_byte_order_i, src_bit_order_i;
  xcb_image_format_t	dst_format, src_format;
  int		dst_bpp, src_bpp;
  int		dst_unit, src_unit;
  int		dst_byte_order, src_byte_order;
  int		dst_bit_order, src_bit_order;

  test_image = create_test_image ();

  for (dst_format_i = 0; dst_format_i < NFORMAT; dst_format_i++) {
    dst_format = formats[dst_format_i];
    for (src_format_i = 0; src_format_i < NFORMAT; src_format_i++) {
      src_format = formats[src_format_i];
      for (dst_bpp_i = 0; dst_bpp_i < NBPP; dst_bpp_i++) {
	dst_bpp = bpps[dst_bpp_i];
	for (src_bpp_i = 0; src_bpp_i < NBPP; src_bpp_i++) {
	  src_bpp = bpps[src_bpp_i];
	  for (dst_unit_i = 0; dst_unit_i < NUNIT; dst_unit_i++) {
	    dst_unit = units[dst_unit_i];
	    if (dst_format == XCB_IMAGE_FORMAT_Z_PIXMAP) {
	      if (dst_bpp == 4 && dst_unit != 8)
		continue;
	      if (dst_bpp > 4 && dst_unit != dst_bpp)
		continue;
	    }
	    if (dst_format == XCB_IMAGE_FORMAT_XY_BITMAP && dst_bpp != 1)
	      continue;
	    for (src_unit_i = 0; src_unit_i < NUNIT; src_unit_i++) {
	      src_unit = units[src_unit_i];
	      if (src_format == XCB_IMAGE_FORMAT_Z_PIXMAP) {
		if (src_bpp == 4 && src_unit != 8)
		  continue;
		if (src_bpp > 4 && src_unit != src_bpp)
		  continue;
	      }
	      if (src_format == XCB_IMAGE_FORMAT_XY_BITMAP && src_bpp != 1)
		continue;
	      for (dst_byte_order_i = 0; dst_byte_order_i < NBYTE_ORDER; dst_byte_order_i++) {
		dst_byte_order = byte_orders[dst_byte_order_i];
		for (src_byte_order_i = 0; src_byte_order_i < NBYTE_ORDER; src_byte_order_i++) {
		  src_byte_order = byte_orders[src_byte_order_i];
		  for (dst_bit_order_i = 0; dst_bit_order_i < NBYTE_ORDER; dst_bit_order_i++) {
		    dst_bit_order = byte_orders[dst_bit_order_i];
		    if (dst_format == XCB_IMAGE_FORMAT_Z_PIXMAP && dst_bit_order != dst_byte_order)
		      continue;
		    for (src_bit_order_i = 0; src_bit_order_i < NBYTE_ORDER; src_bit_order_i++) {
		      src_bit_order = byte_orders[src_bit_order_i];
		      if (src_format == XCB_IMAGE_FORMAT_Z_PIXMAP && src_bit_order != src_byte_order)
			continue;
		      src_image = xcb_image_create (test_width, test_height, src_format, 32, src_bpp, src_bpp, src_unit,
						    src_byte_order, src_bit_order, NULL, 0, NULL);
		      dst_image = xcb_image_create (test_width, test_height, dst_format, 32, dst_bpp, dst_bpp, dst_unit,
						    dst_byte_order, dst_bit_order, NULL, 0, NULL);
		      convert_test (test_image, src_image);
		      if (!compare_image (test_image, src_image)) {
			fprintf (stderr, "Initialization failure:\n");
			fprintf (stderr, "src format: "); print_format(src_image);
			exit (1);
		      }
		      xcb_image_convert (src_image, dst_image);
		      if (!compare_image (src_image, dst_image)) {
			/*
			 * Call the conversion function again so that debugging
			 * is easier
			 */
			fprintf (stderr, "Conversion failure:\n");
			fprintf (stderr, "src format: "); print_format(src_image);
			fprintf (stderr, "dst format: "); print_format(dst_image);
			exit (1);
		      }
		      xcb_image_destroy (src_image);
		      xcb_image_destroy (dst_image);
		    }
		  }
		}
	      }
	    }
	  }
	}
      }
    }
  }
  return 0;
}