1 /* $Id: colormac.h,v 1.12 2003/03/01 01:50:20 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 * Color-related macros
44 #define BYTE_TO_CHAN(b) ((b) < 0 ? 0 : (GLchan) (b))
45 #define UBYTE_TO_CHAN(b) (b)
46 #define SHORT_TO_CHAN(s) ((s) < 0 ? 0 : (GLchan) ((s) >> 7))
47 #define USHORT_TO_CHAN(s) ((GLchan) ((s) >> 8))
48 #define INT_TO_CHAN(i) ((i) < 0 ? 0 : (GLchan) ((i) >> 23))
49 #define UINT_TO_CHAN(i) ((GLchan) ((i) >> 24))
51 #define CHAN_TO_UBYTE(c) (c)
52 #define CHAN_TO_FLOAT(c) UBYTE_TO_FLOAT(c)
54 #define CLAMPED_FLOAT_TO_CHAN(c, f) CLAMPED_FLOAT_TO_UBYTE(c, f)
55 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_UBYTE(c, f)
57 #define COPY_CHAN4(DST, SRC) COPY_4UBV(DST, SRC)
59 #define CHAN_PRODUCT(a, b) ((GLubyte) (((GLint)(a) * ((GLint)(b) + 1)) >> 8))
64 #define BYTE_TO_CHAN(b) ((b) < 0 ? 0 : (((GLchan) (b)) * 516))
65 #define UBYTE_TO_CHAN(b) ((((GLchan) (b)) << 8) | ((GLchan) (b)))
66 #define SHORT_TO_CHAN(s) ((s) < 0 ? 0 : (GLchan) (s))
67 #define USHORT_TO_CHAN(s) (s)
68 #define INT_TO_CHAN(i) ((i) < 0 ? 0 : (GLchan) ((i) >> 15))
69 #define UINT_TO_CHAN(i) ((GLchan) ((i) >> 16))
71 #define CHAN_TO_UBYTE(c) ((c) >> 8)
72 #define CHAN_TO_FLOAT(c) ((GLfloat) ((c) * (1.0 / CHAN_MAXF)))
74 #define CLAMPED_FLOAT_TO_CHAN(c, f) CLAMPED_FLOAT_TO_USHORT(c, f)
75 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_USHORT(c, f)
77 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
79 #define CHAN_PRODUCT(a, b) ((GLchan) ((((GLuint) (a)) * ((GLuint) (b))) / 65535))
84 /* XXX floating-point color channels not fully thought-out */
85 #define BYTE_TO_CHAN(b) ((GLfloat) ((b) * (1.0F / 127.0F)))
86 #define UBYTE_TO_CHAN(b) ((GLfloat) ((b) * (1.0F / 255.0F)))
87 #define SHORT_TO_CHAN(s) ((GLfloat) ((s) * (1.0F / 32767.0F)))
88 #define USHORT_TO_CHAN(s) ((GLfloat) ((s) * (1.0F / 65535.0F)))
89 #define INT_TO_CHAN(i) ((GLfloat) ((i) * (1.0F / 2147483647.0F)))
90 #define UINT_TO_CHAN(i) ((GLfloat) ((i) * (1.0F / 4294967295.0F)))
92 #define CHAN_TO_UBYTE(c) FLOAT_TO_UBYTE(c)
93 #define CHAN_TO_FLOAT(c) (c)
95 #define CLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
96 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
98 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
100 #define CHAN_PRODUCT(a, b) ((a) * (b))
104 #error unexpected CHAN_BITS size
111 * Convert 3 channels at once.
113 #define UNCLAMPED_FLOAT_TO_RGB_CHAN(dst, f) \
115 UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
116 UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
117 UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
122 * Convert 4 channels at once.
124 #define UNCLAMPED_FLOAT_TO_RGBA_CHAN(dst, f) \
126 UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
127 UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
128 UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
129 UNCLAMPED_FLOAT_TO_CHAN(dst[3], f[3]); \
134 /* Generic color packing macros
135 * XXX We may move these into texutil.h at some point.
138 #define PACK_COLOR_8888( a, b, c, d ) \
139 (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
141 #define PACK_COLOR_888( a, b, c ) \
142 (((a) << 16) | ((b) << 8) | (c))
144 #define PACK_COLOR_565( a, b, c ) \
145 ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
147 #define PACK_COLOR_1555( a, b, c, d ) \
148 ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) | \
151 #define PACK_COLOR_4444( a, b, c, d ) \
152 ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
154 #define PACK_COLOR_88( a, b ) \
157 #define PACK_COLOR_332( a, b, c ) \
158 (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
161 #ifdef MESA_BIG_ENDIAN
163 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( d, c, b, a )
165 #define PACK_COLOR_565_LE( a, b, c ) \
166 (((a) & 0xf8) | (((b) & 0xe0) >> 5) | (((b) & 0x1c) << 11) | \
169 #define PACK_COLOR_1555_LE( a, b, c, d ) \
170 ((((b) & 0xf8) >> 1) | (((c) & 0xc0) >> 6) | (((c) & 0x38) << 10) | \
171 (((d) & 0xf8) << 5) | ((a) ? 0x80 : 0))
173 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( c, d, a, b )
175 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( b, a )
177 #else /* little endian */
179 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( a, b, c, d )
181 #define PACK_COLOR_565_LE( a, b, c ) PACK_COLOR_565( a, b, c )
183 #define PACK_COLOR_1555_LE( a, b, c, d ) PACK_COLOR_1555( a, b, c, d )
185 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( a, b, c, d )
187 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( a, b )
189 #endif /* endianness */
192 #endif /* COLORMAC_H */