1/*
2 * Copyright © 2002 Keith Packard
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Keith Packard not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission.  Keith Packard makes no
11 * representations about the suitability of this software for any purpose.  It
12 * is provided "as is" without express or implied warranty.
13 *
14 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#include "xftint.h"
24
25_X_HIDDEN int
26XftNativeByteOrder (void)
27{
28    int	    whichbyte = 1;
29
30    if (*((char *) &whichbyte))
31	return LSBFirst;
32    return MSBFirst;
33}
34
35/* byte swap a 32-bit value */
36#define swapl(x, n) { \
37		 n = ((char *) (x))[0];\
38		 ((char *) (x))[0] = ((char *) (x))[3];\
39		 ((char *) (x))[3] = n;\
40		 n = ((char *) (x))[1];\
41		 ((char *) (x))[1] = ((char *) (x))[2];\
42		 ((char *) (x))[2] = n; }
43
44/* byte swap a short */
45#define swaps(x, n) { \
46		 n = ((char *) (x))[0];\
47		 ((char *) (x))[0] = ((char *) (x))[1];\
48		 ((char *) (x))[1] = n; }
49
50/* byte swap a three-byte unit */
51#define swapt(x, n) { \
52		 n = ((char *) (x))[0];\
53		 ((char *) (x))[0] = ((char *) (x))[2];\
54		 ((char *) (x))[2] = n; }
55
56_X_HIDDEN void
57XftSwapCARD32 (CARD32 *data, int u)
58{
59    char    n;
60    while (u--)
61    {
62	swapl (data, n);
63	data++;
64    }
65}
66
67_X_HIDDEN void
68XftSwapCARD24 (CARD8 *data, int width, int height)
69{
70    int	    units, u;
71    char    n;
72    CARD8   *d;
73
74    units = width / 3;
75    while (height--)
76    {
77	d = data;
78	data += width;
79	u = units;
80	while (u--)
81	{
82	    swapt (d, n);
83	    d += 3;
84	}
85    }
86}
87
88_X_HIDDEN void
89XftSwapCARD16 (CARD16 *data, int u)
90{
91    char    n;
92    while (u--)
93    {
94	swaps (data, n);
95	data++;
96    }
97}
98
99_X_HIDDEN void
100XftSwapImage (XImage *image)
101{
102    switch (image->bits_per_pixel) {
103    case 32:
104	XftSwapCARD32 ((CARD32 *) image->data,
105		       image->height * image->bytes_per_line >> 2);
106	break;
107    case 24:
108	XftSwapCARD24 ((CARD8 *) image->data,
109		       image->bytes_per_line,
110		       image->height);
111	break;
112    case 16:
113	XftSwapCARD16 ((CARD16 *) image->data,
114		       image->height * image->bytes_per_line >> 1);
115	break;
116    default:
117	break;
118    }
119}
120