Removed all RCS / CVS tags (Id, Header, Date, etc.) from everything.
[mesa.git] / src / mesa / main / colormac.h
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 5.1
5 *
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27
28 /*
29 * Color-related macros
30 */
31
32 #ifndef COLORMAC_H
33 #define COLORMAC_H
34
35
36 #include "imports.h"
37 #include "config.h"
38 #include "macros.h"
39
40
41 #if CHAN_BITS == 8
42
43 #define BYTE_TO_CHAN(b) ((b) < 0 ? 0 : (GLchan) (b))
44 #define UBYTE_TO_CHAN(b) (b)
45 #define SHORT_TO_CHAN(s) ((s) < 0 ? 0 : (GLchan) ((s) >> 7))
46 #define USHORT_TO_CHAN(s) ((GLchan) ((s) >> 8))
47 #define INT_TO_CHAN(i) ((i) < 0 ? 0 : (GLchan) ((i) >> 23))
48 #define UINT_TO_CHAN(i) ((GLchan) ((i) >> 24))
49
50 #define CHAN_TO_UBYTE(c) (c)
51 #define CHAN_TO_FLOAT(c) UBYTE_TO_FLOAT(c)
52
53 #define CLAMPED_FLOAT_TO_CHAN(c, f) CLAMPED_FLOAT_TO_UBYTE(c, f)
54 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_UBYTE(c, f)
55
56 #define COPY_CHAN4(DST, SRC) COPY_4UBV(DST, SRC)
57
58 #define CHAN_PRODUCT(a, b) ((GLubyte) (((GLint)(a) * ((GLint)(b) + 1)) >> 8))
59
60
61 #elif CHAN_BITS == 16
62
63 #define BYTE_TO_CHAN(b) ((b) < 0 ? 0 : (((GLchan) (b)) * 516))
64 #define UBYTE_TO_CHAN(b) ((((GLchan) (b)) << 8) | ((GLchan) (b)))
65 #define SHORT_TO_CHAN(s) ((s) < 0 ? 0 : (GLchan) (s))
66 #define USHORT_TO_CHAN(s) (s)
67 #define INT_TO_CHAN(i) ((i) < 0 ? 0 : (GLchan) ((i) >> 15))
68 #define UINT_TO_CHAN(i) ((GLchan) ((i) >> 16))
69
70 #define CHAN_TO_UBYTE(c) ((c) >> 8)
71 #define CHAN_TO_FLOAT(c) ((GLfloat) ((c) * (1.0 / CHAN_MAXF)))
72
73 #define CLAMPED_FLOAT_TO_CHAN(c, f) CLAMPED_FLOAT_TO_USHORT(c, f)
74 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_USHORT(c, f)
75
76 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
77
78 #define CHAN_PRODUCT(a, b) ((GLchan) ((((GLuint) (a)) * ((GLuint) (b))) / 65535))
79
80
81 #elif CHAN_BITS == 32
82
83 /* XXX floating-point color channels not fully thought-out */
84 #define BYTE_TO_CHAN(b) ((GLfloat) ((b) * (1.0F / 127.0F)))
85 #define UBYTE_TO_CHAN(b) ((GLfloat) ((b) * (1.0F / 255.0F)))
86 #define SHORT_TO_CHAN(s) ((GLfloat) ((s) * (1.0F / 32767.0F)))
87 #define USHORT_TO_CHAN(s) ((GLfloat) ((s) * (1.0F / 65535.0F)))
88 #define INT_TO_CHAN(i) ((GLfloat) ((i) * (1.0F / 2147483647.0F)))
89 #define UINT_TO_CHAN(i) ((GLfloat) ((i) * (1.0F / 4294967295.0F)))
90
91 #define CHAN_TO_UBYTE(c) FLOAT_TO_UBYTE(c)
92 #define CHAN_TO_FLOAT(c) (c)
93
94 #define CLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
95 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
96
97 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
98
99 #define CHAN_PRODUCT(a, b) ((a) * (b))
100
101 #else
102
103 #error unexpected CHAN_BITS size
104
105 #endif
106
107
108
109 /*
110 * Convert 3 channels at once.
111 */
112 #define UNCLAMPED_FLOAT_TO_RGB_CHAN(dst, f) \
113 do { \
114 UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
115 UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
116 UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
117 } while (0)
118
119
120 /*
121 * Convert 4 channels at once.
122 */
123 #define UNCLAMPED_FLOAT_TO_RGBA_CHAN(dst, f) \
124 do { \
125 UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
126 UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
127 UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
128 UNCLAMPED_FLOAT_TO_CHAN(dst[3], f[3]); \
129 } while (0)
130
131
132
133 /* Generic color packing macros
134 * XXX We may move these into texutil.h at some point.
135 */
136
137 #define PACK_COLOR_8888( a, b, c, d ) \
138 (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
139
140 #define PACK_COLOR_888( a, b, c ) \
141 (((a) << 16) | ((b) << 8) | (c))
142
143 #define PACK_COLOR_565( a, b, c ) \
144 ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
145
146 #define PACK_COLOR_1555( a, b, c, d ) \
147 ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) | \
148 ((a) ? 0x8000 : 0))
149
150 #define PACK_COLOR_4444( a, b, c, d ) \
151 ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
152
153 #define PACK_COLOR_88( a, b ) \
154 (((a) << 8) | (b))
155
156 #define PACK_COLOR_332( a, b, c ) \
157 (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
158
159
160 #ifdef MESA_BIG_ENDIAN
161
162 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( d, c, b, a )
163
164 #define PACK_COLOR_565_LE( a, b, c ) \
165 (((a) & 0xf8) | (((b) & 0xe0) >> 5) | (((b) & 0x1c) << 11) | \
166 (((c) & 0xf8) << 5))
167
168 #define PACK_COLOR_1555_LE( a, b, c, d ) \
169 ((((b) & 0xf8) >> 1) | (((c) & 0xc0) >> 6) | (((c) & 0x38) << 10) | \
170 (((d) & 0xf8) << 5) | ((a) ? 0x80 : 0))
171
172 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( c, d, a, b )
173
174 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( b, a )
175
176 #else /* little endian */
177
178 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( a, b, c, d )
179
180 #define PACK_COLOR_565_LE( a, b, c ) PACK_COLOR_565( a, b, c )
181
182 #define PACK_COLOR_1555_LE( a, b, c, d ) PACK_COLOR_1555( a, b, c, d )
183
184 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( a, b, c, d )
185
186 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( a, b )
187
188 #endif /* endianness */
189
190
191 #endif /* COLORMAC_H */