aboutsummaryrefslogtreecommitdiffstats
path: root/src/mesa/main/colormac.h
blob: d64cc8d1c3a55e7bb94d49f3f7693a1e5b05a490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * Mesa 3-D graphics library
 * Version:  7.3
 *
 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


/**
 * \file colormac.h
 * Color-related macros
 */


#ifndef COLORMAC_H
#define COLORMAC_H


#include "config.h"
#include "macros.h"
#include "mtypes.h"


/** \def CHAN_TO_UBYTE
 * Convert from GLchan to GLubyte */

/** \def CHAN_TO_FLOAT
 * Convert from GLchan to GLfloat */

/** \def CLAMPED_FLOAT_TO_CHAN
 * Convert from GLclampf to GLchan */

/** \def UNCLAMPED_FLOAT_TO_CHAN
 * Convert from GLfloat to GLchan */

/** \def COPY_CHAN4
 * Copy a GLchan[4] array */

#if CHAN_BITS == 8

#define CHAN_TO_UBYTE(c)  (c)
#define CHAN_TO_USHORT(c) (((c) << 8) | (c))
#define CHAN_TO_SHORT(c)  (((c) << 7) | ((c) >> 1))
#define CHAN_TO_FLOAT(c)  UBYTE_TO_FLOAT(c)

#define CLAMPED_FLOAT_TO_CHAN(c, f)    CLAMPED_FLOAT_TO_UBYTE(c, f)
#define UNCLAMPED_FLOAT_TO_CHAN(c, f)  UNCLAMPED_FLOAT_TO_UBYTE(c, f)

#define COPY_CHAN4(DST, SRC)  COPY_4UBV(DST, SRC)

#elif CHAN_BITS == 16

#define CHAN_TO_UBYTE(c)  ((c) >> 8)
#define CHAN_TO_USHORT(c) (c)
#define CHAN_TO_SHORT(c)  ((c) >> 1)
#define CHAN_TO_FLOAT(c)  ((GLfloat) ((c) * (1.0 / CHAN_MAXF)))

#define CLAMPED_FLOAT_TO_CHAN(c, f)    CLAMPED_FLOAT_TO_USHORT(c, f)
#define UNCLAMPED_FLOAT_TO_CHAN(c, f)  UNCLAMPED_FLOAT_TO_USHORT(c, f)

#define COPY_CHAN4(DST, SRC)  COPY_4V(DST, SRC)

#elif CHAN_BITS == 32

#define CHAN_TO_UBYTE(c)  FLOAT_TO_UBYTE(c)
#define CHAN_TO_USHORT(c) ((GLushort) (CLAMP((c), 0.0f, 1.0f) * 65535.0))
#define CHAN_TO_SHORT(c)  ((GLshort) (CLAMP((c), 0.0f, 1.0f) * 32767.0))
#define CHAN_TO_FLOAT(c)  (c)

#define CLAMPED_FLOAT_TO_CHAN(c, f)  c = (f)
#define UNCLAMPED_FLOAT_TO_CHAN(c, f)      c = (f)

#define COPY_CHAN4(DST, SRC)  COPY_4V(DST, SRC)

#else

#error unexpected CHAN_BITS size

#endif


/**
 * Convert 3 channels at once.
 *
 * \param dst pointer to destination GLchan[3] array.
 * \param f pointer to source GLfloat[3] array.
 *
 * \sa #UNCLAMPED_FLOAT_TO_CHAN.
 */
#define UNCLAMPED_FLOAT_TO_RGB_CHAN(dst, f)	\
do {						\
   UNCLAMPED_FLOAT_TO_CHAN((dst)[0], (f)[0]);	\
   UNCLAMPED_FLOAT_TO_CHAN((dst)[1], (f)[1]);	\
   UNCLAMPED_FLOAT_TO_CHAN((dst)[2], (f)[2]);	\
} while (0)


/**
 * Convert 4 channels at once.
 *
 * \param dst pointer to destination GLchan[4] array.
 * \param f pointer to source GLfloat[4] array.
 *
 * \sa #UNCLAMPED_FLOAT_TO_CHAN.
 */
#define UNCLAMPED_FLOAT_TO_RGBA_CHAN(dst, f)	\
do {						\
   UNCLAMPED_FLOAT_TO_CHAN((dst)[0], (f)[0]);	\
   UNCLAMPED_FLOAT_TO_CHAN((dst)[1], (f)[1]);	\
   UNCLAMPED_FLOAT_TO_CHAN((dst)[2], (f)[2]);	\
   UNCLAMPED_FLOAT_TO_CHAN((dst)[3], (f)[3]);	\
} while (0)


/**
 * Convert four float values in [0,1] to ubytes in [0,255] with clamping.
 */
static inline void
_mesa_unclamped_float_rgba_to_ubyte(GLubyte dst[4], const GLfloat src[4])
{
   int i;
   for (i = 0; i < 4; i++)
      UNCLAMPED_FLOAT_TO_UBYTE(dst[i], src[i]);
}


/**
 * \name Generic color packing macros.  All inputs should be GLubytes.
 *
 * \todo We may move these into texstore.h at some point.
 */
/*@{*/

#define PACK_COLOR_8888( X, Y, Z, W ) \
   (((X) << 24) | ((Y) << 16) | ((Z) << 8) | (W))

#define PACK_COLOR_8888_REV( X, Y, Z, W ) \
   (((W) << 24) | ((Z) << 16) | ((Y) << 8) | (X))

#define PACK_COLOR_888( X, Y, Z ) \
   (((X) << 16) | ((Y) << 8) | (Z))

#define PACK_COLOR_565( X, Y, Z )                                  \
   ((((X) & 0xf8) << 8) | (((Y) & 0xfc) << 3) | (((Z) & 0xf8) >> 3))

#define PACK_COLOR_565_REV( X, Y, Z ) \
   (((X) & 0xf8) | ((Y) & 0xe0) >> 5 | (((Y) & 0x1c) << 11) | (((Z) & 0xf8) << 5))

#define PACK_COLOR_5551( R, G, B, A )					\
   ((((R) & 0xf8) << 8) | (((G) & 0xf8) << 3) | (((B) & 0xf8) >> 2) |	\
    ((A) ? 1 : 0))

#define PACK_COLOR_1555( A, B, G, R )					\
   ((((B) & 0xf8) << 7) | (((G) & 0xf8) << 2) | (((R) & 0xf8) >> 3) |	\
    ((A) ? 0x8000 : 0))

#define PACK_COLOR_1555_REV( A, B, G, R )					\
   ((((B) & 0xf8) >> 1) | (((G) & 0xc0) >> 6) | (((G) & 0x38) << 10) | (((R) & 0xf8) << 5) |	\
    ((A) ? 0x80 : 0))

#define PACK_COLOR_2101010_UB( A, B, G, R )					\
   (((B) << 22) | ((G) << 12) | ((R) << 2) |	\
    (((A) & 0xc0) << 24))

#define PACK_COLOR_2101010_US( A, B, G, R )					\
   ((((B) >> 6) << 20) | (((G) >> 6) << 10) | ((R) >> 6) |	\
    (((A) >> 14) << 30))

#define PACK_COLOR_4444( R, G, B, A )					\
   ((((R) & 0xf0) << 8) | (((G) & 0xf0) << 4) | ((B) & 0xf0) | ((A) >> 4))

#define PACK_COLOR_4444_REV( R, G, B, A )				\
   ((((B) & 0xf0) << 8) | (((A) & 0xf0) << 4) | ((R) & 0xf0) | ((G) >> 4))

#define PACK_COLOR_44( L, A )						\
   (((L) & 0xf0) | (((A) & 0xf0) >> 4))

#define PACK_COLOR_88( L, A )						\
   (((L) << 8) | (A))

#define PACK_COLOR_88_REV( L, A )					\
   (((A) << 8) | (L))

#define PACK_COLOR_1616( L, A )						\
   (((L) << 16) | (A))

#define PACK_COLOR_1616_REV( L, A )					\
   (((A) << 16) | (L))

#define PACK_COLOR_332( R, G, B )					\
   (((R) & 0xe0) | (((G) & 0xe0) >> 3) | (((B) & 0xc0) >> 6))

#define PACK_COLOR_233( B, G, R )					\
   (((B) & 0xc0) | (((G) & 0xe0) >> 2) | (((R) & 0xe0) >> 5))

/*@}*/


#endif /* COLORMAC_H */