blob: b62d100abb4105d6f3f4d34c07b829f5b0210f15 (
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
|
/**
\file G3D/unorm8.h
\maintainer Morgan McGuire, http://graphics.cs.williams.edu
\created 2011-08-11
\edited 2011-08-11
Copyright 2000-2012, Morgan McGuire.
All rights reserved.
*/
#ifndef G3D_unorm8_h
#define G3D_unorm8_h
#include "G3D/platform.h"
#include "G3D/g3dmath.h"
namespace G3D {
/** Represents numbers on [0, 1] in 8 bits as an unsigned normalized
0.8 fixed-point value using the same encoding scheme as OpenGL.
OpenGL specifications can be found here:
<www.opengl.org/registry/specs/ARB/shading_language_packing.txt>
Note that arithmetic operations may over and under-flow, just like
uint8 arithmetic.
*/
G3D_BEGIN_PACKED_CLASS(1)
unorm8 {
private:
uint8 m_bits;
/** Private to prevent illogical conversions without explicitly
stating that the input should be treated as bits; see fromBits. */
explicit unorm8(uint8 b) : m_bits(b) {}
public:
/** Equivalent to: \code unorm8 u = reinterpret_cast<const unorm8&>(255);\endcode */
static unorm8 fromBits(uint8 b) {
return unorm8(b);
}
/** \copydoc fromBits */
static unorm8 reinterpretFrom(uint8 b) {
return unorm8(b);
}
unorm8() : m_bits(0) {}
unorm8(const unorm8& other) : m_bits(other.m_bits) {}
/** Maps f to round(f * 255).*/
explicit unorm8(float f) {
m_bits = (uint8)(clamp(f, 0.0f, 1.0f) * 255.0f + 0.5f);
}
explicit unorm8(double f) {
m_bits = iClamp(int(f * 255.0f + 0.5f), 0, 255);
}
/** Returns a number on [0.0f, 1.0f] */
operator float() const {
return float(m_bits) * (1.0f / 255.0f);
}
operator double() const {
return double(m_bits) * (1.0 / 255.0);
}
static unorm8 one() {
return fromBits(255);
}
static unorm8 zero() {
return fromBits(0);
}
/**\brief Returns the underlying bits in this representation.
Equivalent to:
\code uint8 i = reinterpret_cast<const uint8&>(u); \endcode */
uint8 bits() const {
return m_bits;
}
/** \copydoc bits */
uint8 reinterpretAsUInt8() const {
return m_bits;
}
bool operator>(const unorm8 other) const {
return m_bits > other.m_bits;
}
bool operator<(const unorm8 other) const {
return m_bits < other.m_bits;
}
bool operator>=(const unorm8 other) const {
return m_bits >= other.m_bits;
}
bool operator<=(const unorm8 other) const {
return m_bits <= other.m_bits;
}
bool operator==(const unorm8 other) const {
return m_bits <= other.m_bits;
}
bool operator!=(const unorm8 other) const {
return m_bits != other.m_bits;
}
unorm8 operator+(const unorm8 other) const {
return unorm8(uint8(m_bits + other.m_bits));
}
unorm8& operator+=(const unorm8 other) {
m_bits += other.m_bits;
return *this;
}
unorm8 operator-(const unorm8 other) const {
return unorm8(uint8(m_bits - other.m_bits));
}
unorm8& operator-=(const unorm8 other) {
m_bits -= other.m_bits;
return *this;
}
unorm8 operator*(const int i) const {
return unorm8(uint8(m_bits * i));
}
unorm8& operator*=(const int i) {
m_bits *= i;
return *this;
}
unorm8 operator/(const int i) const {
return unorm8(uint8(m_bits / i));
}
unorm8& operator/=(const int i) {
m_bits /= i;
return *this;
}
unorm8 operator<<(const int i) const {
return unorm8((uint8)(m_bits << i));
}
unorm8& operator<<=(const int i) {
m_bits <<= i;
return *this;
}
unorm8 operator>>(const int i) const {
return unorm8(uint8(m_bits >> i));
}
unorm8& operator>>=(const int i) {
m_bits >>= i;
return *this;
}
}
G3D_END_PACKED_CLASS(1)
} // namespace G3D
#endif // G3D_unorm8
|