2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Color-related macros
41 /** \def CHAN_TO_UBYTE
42 * Convert from GLchan to GLubyte */
44 /** \def CHAN_TO_FLOAT
45 * Convert from GLchan to GLfloat */
47 /** \def CLAMPED_FLOAT_TO_CHAN
48 * Convert from GLclampf to GLchan */
50 /** \def UNCLAMPED_FLOAT_TO_CHAN
51 * Convert from GLfloat to GLchan */
54 * Copy a GLchan[4] array */
58 #define CHAN_TO_UBYTE(c) (c)
59 #define CHAN_TO_USHORT(c) (((c) << 8) | (c))
60 #define CHAN_TO_SHORT(c) (((c) << 7) | ((c) >> 1))
61 #define CHAN_TO_FLOAT(c) UBYTE_TO_FLOAT(c)
63 #define CLAMPED_FLOAT_TO_CHAN(c, f) CLAMPED_FLOAT_TO_UBYTE(c, f)
64 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_UBYTE(c, f)
66 #define COPY_CHAN4(DST, SRC) COPY_4UBV(DST, SRC)
70 #define CHAN_TO_UBYTE(c) ((c) >> 8)
71 #define CHAN_TO_USHORT(c) (c)
72 #define CHAN_TO_SHORT(c) ((c) >> 1)
73 #define CHAN_TO_FLOAT(c) ((GLfloat) ((c) * (1.0 / CHAN_MAXF)))
75 #define CLAMPED_FLOAT_TO_CHAN(c, f) CLAMPED_FLOAT_TO_USHORT(c, f)
76 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_USHORT(c, f)
78 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
82 #define CHAN_TO_UBYTE(c) FLOAT_TO_UBYTE(c)
83 #define CHAN_TO_USHORT(c) ((GLushort) (CLAMP((c), 0.0f, 1.0f) * 65535.0))
84 #define CHAN_TO_SHORT(c) ((GLshort) (CLAMP((c), 0.0f, 1.0f) * 32767.0))
85 #define CHAN_TO_FLOAT(c) (c)
87 #define CLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
88 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
90 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
94 #error unexpected CHAN_BITS size
100 * Convert 3 channels at once.
102 * \param dst pointer to destination GLchan[3] array.
103 * \param f pointer to source GLfloat[3] array.
105 * \sa #UNCLAMPED_FLOAT_TO_CHAN.
107 #define UNCLAMPED_FLOAT_TO_RGB_CHAN(dst, f) \
109 UNCLAMPED_FLOAT_TO_CHAN((dst)[0], (f)[0]); \
110 UNCLAMPED_FLOAT_TO_CHAN((dst)[1], (f)[1]); \
111 UNCLAMPED_FLOAT_TO_CHAN((dst)[2], (f)[2]); \
116 * Convert 4 channels at once.
118 * \param dst pointer to destination GLchan[4] array.
119 * \param f pointer to source GLfloat[4] array.
121 * \sa #UNCLAMPED_FLOAT_TO_CHAN.
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 * Convert four float values in [0,1] to ubytes in [0,255] with clamping.
136 _mesa_unclamped_float_rgba_to_ubyte(GLubyte dst
[4], const GLfloat src
[4])
139 for (i
= 0; i
< 4; i
++)
140 UNCLAMPED_FLOAT_TO_UBYTE(dst
[i
], src
[i
]);
145 * \name Generic color packing macros. All inputs should be GLubytes.
147 * \todo We may move these into texstore.h at some point.
151 #define PACK_COLOR_8888( X, Y, Z, W ) \
152 (((X) << 24) | ((Y) << 16) | ((Z) << 8) | (W))
154 #define PACK_COLOR_8888_REV( X, Y, Z, W ) \
155 (((W) << 24) | ((Z) << 16) | ((Y) << 8) | (X))
157 #define PACK_COLOR_888( X, Y, Z ) \
158 (((X) << 16) | ((Y) << 8) | (Z))
160 #define PACK_COLOR_565( X, Y, Z ) \
161 ((((X) & 0xf8) << 8) | (((Y) & 0xfc) << 3) | (((Z) & 0xf8) >> 3))
163 #define PACK_COLOR_565_REV( X, Y, Z ) \
164 (((X) & 0xf8) | ((Y) & 0xe0) >> 5 | (((Y) & 0x1c) << 11) | (((Z) & 0xf8) << 5))
166 #define PACK_COLOR_5551( R, G, B, A ) \
167 ((((R) & 0xf8) << 8) | (((G) & 0xf8) << 3) | (((B) & 0xf8) >> 2) | \
170 #define PACK_COLOR_1555( A, B, G, R ) \
171 ((((B) & 0xf8) << 7) | (((G) & 0xf8) << 2) | (((R) & 0xf8) >> 3) | \
174 #define PACK_COLOR_1555_REV( A, B, G, R ) \
175 ((((B) & 0xf8) >> 1) | (((G) & 0xc0) >> 6) | (((G) & 0x38) << 10) | (((R) & 0xf8) << 5) | \
178 #define PACK_COLOR_2101010_UB( A, B, G, R ) \
179 (((B) << 22) | ((G) << 12) | ((R) << 2) | \
180 (((A) & 0xc0) << 24))
182 #define PACK_COLOR_2101010_US( A, B, G, R ) \
183 ((((B) >> 6) << 20) | (((G) >> 6) << 10) | ((R) >> 6) | \
186 #define PACK_COLOR_4444( R, G, B, A ) \
187 ((((R) & 0xf0) << 8) | (((G) & 0xf0) << 4) | ((B) & 0xf0) | ((A) >> 4))
189 #define PACK_COLOR_4444_REV( R, G, B, A ) \
190 ((((B) & 0xf0) << 8) | (((A) & 0xf0) << 4) | ((R) & 0xf0) | ((G) >> 4))
192 #define PACK_COLOR_44( L, A ) \
193 (((L) & 0xf0) | (((A) & 0xf0) >> 4))
195 #define PACK_COLOR_88( L, A ) \
198 #define PACK_COLOR_88_REV( L, A ) \
201 #define PACK_COLOR_1616( L, A ) \
204 #define PACK_COLOR_1616_REV( L, A ) \
207 #define PACK_COLOR_332( R, G, B ) \
208 (((R) & 0xe0) | (((G) & 0xe0) >> 3) | (((B) & 0xc0) >> 6))
210 #define PACK_COLOR_233( B, G, R ) \
211 (((B) & 0xc0) | (((G) & 0xe0) >> 2) | (((R) & 0xe0) >> 5))
216 #endif /* COLORMAC_H */