fixup float/ushort conversion macros
[mesa.git] / src / mesa / main / colormac.h
1 /* $Id: colormac.h,v 1.11 2002/04/04 16:51:52 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 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) CLAMPED_FLOAT_TO_USHORT(c, f)
78 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) UNCLAMPED_FLOAT_TO_USHORT(c, f)
79
80 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
81
82 #define CHAN_PRODUCT(a, b) ((GLchan) ((((GLuint) (a)) * ((GLuint) (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_UBYTE(c) FLOAT_TO_UBYTE(c)
96 #define CHAN_TO_FLOAT(c) (c)
97
98 #define CLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
99 #define UNCLAMPED_FLOAT_TO_CHAN(c, f) c = (f)
100
101 #define COPY_CHAN4(DST, SRC) COPY_4V(DST, SRC)
102
103 #define CHAN_PRODUCT(a, b) ((a) * (b))
104
105 #else
106
107 #error unexpected CHAN_BITS size
108
109 #endif
110
111
112
113 /*
114 * Convert 3 channels at once.
115 */
116 #define UNCLAMPED_FLOAT_TO_RGB_CHAN(dst, f) \
117 do { \
118 UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
119 UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
120 UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
121 } while (0)
122
123
124 /*
125 * Convert 4 channels at once.
126 */
127 #define UNCLAMPED_FLOAT_TO_RGBA_CHAN(dst, f) \
128 do { \
129 UNCLAMPED_FLOAT_TO_CHAN(dst[0], f[0]); \
130 UNCLAMPED_FLOAT_TO_CHAN(dst[1], f[1]); \
131 UNCLAMPED_FLOAT_TO_CHAN(dst[2], f[2]); \
132 UNCLAMPED_FLOAT_TO_CHAN(dst[3], f[3]); \
133 } while (0)
134
135
136 #endif /* COLORMAC_H */