3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
29 * Color-related macros
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))
50 #define CHAN_TO_UBYTE(c) (c)
51 #define CHAN_TO_FLOAT(c) UBYTE_TO_FLOAT(c)
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)
56 #define COPY_CHAN4(DST, SRC) COPY_4UBV(DST, SRC)
58 #define CHAN_PRODUCT(a, b) ((GLubyte) (((GLint)(a) * ((GLint)(b) + 1)) >> 8))
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))
70 #define CHAN_TO_UBYTE(c) ((c) >> 8)
71 #define CHAN_TO_FLOAT(c) ((GLfloat) ((c) * (1.0 / CHAN_MAXF)))
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)
76 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
78 #define CHAN_PRODUCT(a, b) ((GLchan) ((((GLuint) (a)) * ((GLuint) (b))) / 65535))
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)))
91 #define CHAN_TO_UBYTE(c) FLOAT_TO_UBYTE(c)
92 #define CHAN_TO_FLOAT(c) (c)
94 #define CLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
95 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
97 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
99 #define CHAN_PRODUCT(a, b) ((a) * (b))
103 #error unexpected CHAN_BITS size
110 * Convert 3 channels at once.
112 #define UNCLAMPED_FLOAT_TO_RGB_CHAN(dst, f) \
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]); \
121 * Convert 4 channels at once.
123 #define UNCLAMPED_FLOAT_TO_RGBA_CHAN(dst, f) \
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]); \
133 /* Generic color packing macros
134 * XXX We may move these into texutil.h at some point.
137 #define PACK_COLOR_8888( a, b, c, d ) \
138 (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
140 #define PACK_COLOR_888( a, b, c ) \
141 (((a) << 16) | ((b) << 8) | (c))
143 #define PACK_COLOR_565( a, b, c ) \
144 ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
146 #define PACK_COLOR_1555( a, b, c, d ) \
147 ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) | \
150 #define PACK_COLOR_4444( a, b, c, d ) \
151 ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
153 #define PACK_COLOR_88( a, b ) \
156 #define PACK_COLOR_332( a, b, c ) \
157 (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
160 #ifdef MESA_BIG_ENDIAN
162 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( d, c, b, a )
164 #define PACK_COLOR_565_LE( a, b, c ) \
165 (((a) & 0xf8) | (((b) & 0xe0) >> 5) | (((b) & 0x1c) << 11) | \
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))
172 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( c, d, a, b )
174 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( b, a )
176 #else /* little endian */
178 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( a, b, c, d )
180 #define PACK_COLOR_565_LE( a, b, c ) PACK_COLOR_565( a, b, c )
182 #define PACK_COLOR_1555_LE( a, b, c, d ) PACK_COLOR_1555( a, b, c, d )
184 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( a, b, c, d )
186 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( a, b )
188 #endif /* endianness */
191 #endif /* COLORMAC_H */