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