More color macro clean-ups.
[mesa.git] / src / mesa / main / colormac.h
1 /* $Id: colormac.h,v 1.6 2001/01/03 15:59:30 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28
29 /*
30 * Color-related macros
31 */
32
33 #ifndef COLORMAC_H
34 #define COLORMAC_H
35
36
37 #include "glheader.h"
38 #include "config.h"
39 #include "macros.h"
40 #include "mmath.h"
41 /* Do not reference mtypes.h from this file.
42 */
43
44
45 #if CHAN_BITS == 8
46
47 #define BYTE_TO_CHAN(b) ((b) < 0 ? 0 : (GLchan) (b))
48 #define UBYTE_TO_CHAN(b) (b)
49 #define SHORT_TO_CHAN(s) ((s) < 0 ? 0 : (GLchan) ((s) >> 7))
50 #define USHORT_TO_CHAN(s) ((GLchan) ((s) >> 8))
51 #define INT_TO_CHAN(i) ((i) < 0 ? 0 : (GLchan) ((i) >> 23))
52 #define UINT_TO_CHAN(i) ((GLchan) ((i) >> 24))
53
54 #define CHAN_TO_FLOAT(c) UBYTE_TO_FLOAT(c)
55
56 #define CLAMPED_FLOAT_TO_CHAN(c, f) CLAMPED_FLOAT_TO_UBYTE(c, f)
57 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_UBYTE(c, f)
58
59 #define COPY_CHAN4(DST, SRC) COPY_4UBV(DST, SRC)
60
61 #define CHAN_PRODUCT(a, b) ((GLubyte) (((GLint)(a) * ((GLint)(b) + 1)) >> 8))
62
63
64 #elif CHAN_BITS == 16
65
66 #define BYTE_TO_CHAN(b) ((b) < 0 ? 0 : (GLchan) ((b) * 516))
67 #define UBYTE_TO_CHAN(b) ((GLchan) (((b) << 8) | (b)))
68 #define SHORT_TO_CHAN(s) ((s) < 0 ? 0 : (GLchan) (s))
69 #define USHORT_TO_CHAN(s) (s)
70 #define INT_TO_CHAN(i) ((i) < 0 ? 0 : (GLchan) ((i) >> 15))
71 #define UINT_TO_CHAN(i) ((GLchan) ((i) >> 16))
72
73 #define CHAN_TO_FLOAT(c) ((GLfloat) ((c) * (1.0 / CHAN_MAXF) + 0.5F))
74
75 #define CLAMPED_FLOAT_TO_CHAN(c, f) \
76 c = ((GLchan) IROUND((f) * CHAN_MAXF))
77 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) \
78 c = ( (GLchan) IROUND( CLAMP(f, 0.0, 1.0) * CHAN_MAXF) )
79
80 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
81
82 #define CHAN_PRODUCT(a, b) ((GLchan) ((((GLint) (a)) * ((GLint) (b))) / 65535))
83
84
85 #elif CHAN_BITS == 32
86
87 /* XXX floating-point color channels not fully thought-out */
88 #define BYTE_TO_CHAN(b) ((GLfloat) ((b) * (1.0F / 127.0F)))
89 #define UBYTE_TO_CHAN(b) ((GLfloat) ((b) * (1.0F / 255.0F)))
90 #define SHORT_TO_CHAN(s) ((GLfloat) ((s) * (1.0F / 32767.0F)))
91 #define USHORT_TO_CHAN(s) ((GLfloat) ((s) * (1.0F / 65535.0F)))
92 #define INT_TO_CHAN(i) ((GLfloat) ((i) * (1.0F / 2147483647.0F)))
93 #define UINT_TO_CHAN(i) ((GLfloat) ((i) * (1.0F / 4294967295.0F)))
94
95 #define CHAN_TO_FLOAT(c) (c)
96
97 #define CLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
98 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
99
100 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
101
102 #define CHAN_PRODUCT(a, b) ((a) * (b))
103
104 #else
105
106 #error unexpected CHAN_BITS size
107
108 #endif
109
110
111
112 /*
113 * Convert 3 channels at once.
114 */
115 #define UNCLAMPED_FLOAT_TO_RGB_CHAN(dst, f) \
116 do { \
117 UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
118 UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
119 UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
120 } while (0)
121
122
123 /*
124 * Convert 4 channels at once.
125 */
126 #define UNCLAMPED_FLOAT_TO_RGBA_CHAN(dst, f) \
127 do { \
128 UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
129 UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
130 UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
131 UNCLAMPED_FLOAT_TO_CHAN(dst[3], f[3]); \
132 } while (0)
133
134
135 #endif /* COLORMAC_H */