7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 * Convert from GLbyte to GLchan */
44 /** \def UBYTE_TO_CHAN
45 * Convert from GLubyte to GLchan */
47 /** \def SHORT_TO_CHAN
48 * Convert from GLshort to GLchan */
50 /** \def USHORT_TO_CHAN
51 * Convert from GLushort to GLchan */
54 * Convert from GLint to GLchan */
57 * Convert from GLuint to GLchan */
59 /** \def CHAN_TO_UBYTE
60 * Convert from GLchan to GLubyte */
62 /** \def CHAN_TO_FLOAT
63 * Convert from GLchan to GLfloat */
65 /** \def CLAMPED_FLOAT_TO_CHAN
66 * Convert from GLclampf to GLchan */
68 /** \def UNCLAMPED_FLOAT_TO_CHAN
69 * Convert from GLfloat to GLchan */
72 * Copy a GLchan[4] array */
75 * Scaled product (usually approximated) between two GLchan arguments */
79 #define BYTE_TO_CHAN(b) ((b) < 0 ? 0 : (GLchan) (b))
80 #define UBYTE_TO_CHAN(b) (b)
81 #define SHORT_TO_CHAN(s) ((s) < 0 ? 0 : (GLchan) ((s) >> 7))
82 #define USHORT_TO_CHAN(s) ((GLchan) ((s) >> 8))
83 #define INT_TO_CHAN(i) ((i) < 0 ? 0 : (GLchan) ((i) >> 23))
84 #define UINT_TO_CHAN(i) ((GLchan) ((i) >> 24))
86 #define CHAN_TO_UBYTE(c) (c)
87 #define CHAN_TO_FLOAT(c) UBYTE_TO_FLOAT(c)
89 #define CLAMPED_FLOAT_TO_CHAN(c, f) CLAMPED_FLOAT_TO_UBYTE(c, f)
90 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_UBYTE(c, f)
92 #define COPY_CHAN4(DST, SRC) COPY_4UBV(DST, SRC)
94 #define CHAN_PRODUCT(a, b) ((GLubyte) (((GLint)(a) * ((GLint)(b) + 1)) >> 8))
98 #define BYTE_TO_CHAN(b) ((b) < 0 ? 0 : (((GLchan) (b)) * 516))
99 #define UBYTE_TO_CHAN(b) ((((GLchan) (b)) << 8) | ((GLchan) (b)))
100 #define SHORT_TO_CHAN(s) ((s) < 0 ? 0 : (GLchan) (s))
101 #define USHORT_TO_CHAN(s) (s)
102 #define INT_TO_CHAN(i) ((i) < 0 ? 0 : (GLchan) ((i) >> 15))
103 #define UINT_TO_CHAN(i) ((GLchan) ((i) >> 16))
105 #define CHAN_TO_UBYTE(c) ((c) >> 8)
106 #define CHAN_TO_FLOAT(c) ((GLfloat) ((c) * (1.0 / CHAN_MAXF)))
108 #define CLAMPED_FLOAT_TO_CHAN(c, f) CLAMPED_FLOAT_TO_USHORT(c, f)
109 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_USHORT(c, f)
111 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
113 #define CHAN_PRODUCT(a, b) ((GLchan) ((((GLuint) (a)) * ((GLuint) (b))) / 65535))
115 #elif CHAN_BITS == 32
117 /* XXX floating-point color channels not fully thought-out */
118 #define BYTE_TO_CHAN(b) ((GLfloat) ((b) * (1.0F / 127.0F)))
119 #define UBYTE_TO_CHAN(b) ((GLfloat) ((b) * (1.0F / 255.0F)))
120 #define SHORT_TO_CHAN(s) ((GLfloat) ((s) * (1.0F / 32767.0F)))
121 #define USHORT_TO_CHAN(s) ((GLfloat) ((s) * (1.0F / 65535.0F)))
122 #define INT_TO_CHAN(i) ((GLfloat) ((i) * (1.0F / 2147483647.0F)))
123 #define UINT_TO_CHAN(i) ((GLfloat) ((i) * (1.0F / 4294967295.0F)))
125 #define CHAN_TO_UBYTE(c) FLOAT_TO_UBYTE(c)
126 #define CHAN_TO_FLOAT(c) (c)
128 #define CLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
129 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
131 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
133 #define CHAN_PRODUCT(a, b) ((a) * (b))
137 #error unexpected CHAN_BITS size
143 * Convert 3 channels at once.
145 * \param dst pointer to destination GLchan[3] array.
146 * \param f pointer to source GLfloat[3] array.
148 * \sa #UNCLAMPED_FLOAT_TO_CHAN.
150 #define UNCLAMPED_FLOAT_TO_RGB_CHAN(dst, f) \
152 UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
153 UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
154 UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
159 * Convert 4 channels at once.
161 * \param dst pointer to destination GLchan[4] array.
162 * \param f pointer to source GLfloat[4] array.
164 * \sa #UNCLAMPED_FLOAT_TO_CHAN.
166 #define UNCLAMPED_FLOAT_TO_RGBA_CHAN(dst, f) \
168 UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
169 UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
170 UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
171 UNCLAMPED_FLOAT_TO_CHAN(dst[3], f[3]); \
177 * \name Generic color packing macros
179 * \todo We may move these into texutil.h at some point.
183 #define PACK_COLOR_8888( a, b, c, d ) \
184 (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
186 #define PACK_COLOR_888( a, b, c ) \
187 (((a) << 16) | ((b) << 8) | (c))
189 #define PACK_COLOR_565( a, b, c ) \
190 ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
192 #define PACK_COLOR_1555( a, b, c, d ) \
193 ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) | \
196 #define PACK_COLOR_4444( a, b, c, d ) \
197 ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
199 #define PACK_COLOR_88( a, b ) \
202 #define PACK_COLOR_332( a, b, c ) \
203 (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
206 #ifdef MESA_BIG_ENDIAN
208 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( d, c, b, a )
210 #define PACK_COLOR_565_LE( a, b, c ) \
211 (((a) & 0xf8) | (((b) & 0xe0) >> 5) | (((b) & 0x1c) << 11) | \
214 #define PACK_COLOR_1555_LE( a, b, c, d ) \
215 ((((b) & 0xf8) >> 1) | (((c) & 0xc0) >> 6) | (((c) & 0x38) << 10) | \
216 (((d) & 0xf8) << 5) | ((a) ? 0x80 : 0))
218 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( c, d, a, b )
220 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( b, a )
222 #else /* little endian */
224 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( a, b, c, d )
226 #define PACK_COLOR_565_LE( a, b, c ) PACK_COLOR_565( a, b, c )
228 #define PACK_COLOR_1555_LE( a, b, c, d ) PACK_COLOR_1555( a, b, c, d )
230 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( a, b, c, d )
232 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( a, b )
234 #endif /* endianness */
239 #endif /* COLORMAC_H */