Support for swappable tnl modules.
[mesa.git] / src / mesa / main / colormac.h
1 /* $Id: colormac.h,v 1.9 2001/03/11 18:49:11 gareth Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 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_UBYTE(c) (c)
55 #define CHAN_TO_FLOAT(c) UBYTE_TO_FLOAT(c)
56
57 #define CLAMPED_FLOAT_TO_CHAN(c, f) CLAMPED_FLOAT_TO_UBYTE(c, f)
58 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_UBYTE(c, f)
59
60 #define COPY_CHAN4(DST, SRC) COPY_4UBV(DST, SRC)
61
62 #define CHAN_PRODUCT(a, b) ((GLubyte) (((GLint)(a) * ((GLint)(b) + 1)) >> 8))
63
64
65 #elif CHAN_BITS == 16
66
67 #define BYTE_TO_CHAN(b) ((b) < 0 ? 0 : (((GLchan) (b)) * 516))
68 #define UBYTE_TO_CHAN(b) ((((GLchan) (b)) << 8) | ((GLchan) (b)))
69 #define SHORT_TO_CHAN(s) ((s) < 0 ? 0 : (GLchan) (s))
70 #define USHORT_TO_CHAN(s) (s)
71 #define INT_TO_CHAN(i) ((i) < 0 ? 0 : (GLchan) ((i) >> 15))
72 #define UINT_TO_CHAN(i) ((GLchan) ((i) >> 16))
73
74 #define CHAN_TO_UBYTE(c) ((c) >> 8)
75 #define CHAN_TO_FLOAT(c) ((GLfloat) ((c) * (1.0 / CHAN_MAXF)))
76
77 #define CLAMPED_FLOAT_TO_CHAN(c, f) \
78 c = ((GLchan) IROUND((f) * CHAN_MAXF))
79 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) \
80 c = ( (GLchan) IROUND( CLAMP(f, 0.0, 1.0) * CHAN_MAXF) )
81
82 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
83
84 #define CHAN_PRODUCT(a, b) ((GLchan) ((((GLint) (a)) * ((GLint) (b))) / 65535))
85
86
87 #elif CHAN_BITS == 32
88
89 /* XXX floating-point color channels not fully thought-out */
90 #define BYTE_TO_CHAN(b) ((GLfloat) ((b) * (1.0F / 127.0F)))
91 #define UBYTE_TO_CHAN(b) ((GLfloat) ((b) * (1.0F / 255.0F)))
92 #define SHORT_TO_CHAN(s) ((GLfloat) ((s) * (1.0F / 32767.0F)))
93 #define USHORT_TO_CHAN(s) ((GLfloat) ((s) * (1.0F / 65535.0F)))
94 #define INT_TO_CHAN(i) ((GLfloat) ((i) * (1.0F / 2147483647.0F)))
95 #define UINT_TO_CHAN(i) ((GLfloat) ((i) * (1.0F / 4294967295.0F)))
96
97 #define CHAN_TO_UBYTE(c) FLOAT_TO_UBYTE(c)
98 #define CHAN_TO_FLOAT(c) (c)
99
100 #define CLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
101 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
102
103 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
104
105 #define CHAN_PRODUCT(a, b) ((a) * (b))
106
107 #else
108
109 #error unexpected CHAN_BITS size
110
111 #endif
112
113
114
115 /*
116 * Convert 3 channels at once.
117 */
118 #define UNCLAMPED_FLOAT_TO_RGB_CHAN(dst, f) \
119 do { \
120 UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
121 UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
122 UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
123 } while (0)
124
125
126 /*
127 * Convert 4 channels at once.
128 */
129 #define UNCLAMPED_FLOAT_TO_RGBA_CHAN(dst, f) \
130 do { \
131 UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
132 UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
133 UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
134 UNCLAMPED_FLOAT_TO_CHAN(dst[3], f[3]); \
135 } while (0)
136
137
138 #endif /* COLORMAC_H */