d2::align: Cast with lrint() for lod count, as plain (unsigned int) seems to be broke...
[Ale.git] / d2 / pixel.h
blob26d9cf55983a4a510ad1b69b7a1bd41d7890a838
1 // Copyright 2002 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>
4 /* This file is part of the Anti-Lamenessing Engine.
6 The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with the Anti-Lamenessing Engine; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef __pixel_h__
22 #define __pixel_h__
25 * Structure to describe a pixel
28 class pixel {
29 private:
30 ale_real x[3];
32 public:
33 pixel() {
34 x[0] = 0;
35 x[1] = 0;
36 x[2] = 0;
39 pixel(ale_real x0, ale_real x1, ale_real x2) {
40 x[0] = x0;
41 x[1] = x1;
42 x[2] = x2;
45 // Due to automatic typecasts and automatic int <==> ale_real *
46 // conversions, this can cause some really weird bugs.
48 // pixel(ale_real *_x) {
49 // x[0] = _x[0];
50 // x[1] = _x[1];
51 // x[2] = _x[2];
52 // }
54 const ale_real &operator[](unsigned int i) const {
55 assert (i < 3);
56 return x[i];
59 ale_real &operator[](unsigned int i) {
60 assert (i < 3);
61 return x[i];
64 pixel operator+(pixel p) const {
65 return pixel(p[0] + x[0], p[1] + x[1], p[2] + x[2]);
68 pixel operator-(pixel p) const {
69 return pixel(x[0] - p[0], x[1] - p[1], x[2] - p[2]);
72 pixel operator-() const {
73 return pixel(-x[0], -x[1], -x[2]);
76 pixel operator/(pixel p) const {
77 return pixel(x[0] / p[0], x[1] / p[1], x[2] / p[2]);
80 pixel operator/(ale_real d) const {
81 return pixel(x[0] / d, x[1] / d, x[2] / d);
84 pixel operator*(pixel p) const {
85 return pixel(x[0] * p[0], x[1] * p[1], x[2] * p[2]);
88 pixel mult(ale_real d) const {
89 return pixel(x[0] * d, x[1] * d, x[2] * d);
92 pixel operator+=(pixel p) {
93 return pixel(x[0] += p[0], x[1] += p[1], x[2] += p[2]);
96 pixel operator*=(pixel p) {
97 return pixel(x[0] *= p[0], x[1] *= p[1], x[2] *= p[2]);
100 pixel operator*=(ale_real d) {
101 return pixel(x[0] *= d, x[1] *= d, x[2] *= d);
104 pixel operator/=(pixel p) {
105 return pixel(x[0] /= p[0], x[1] /= p[1], x[2] /= p[2]);
108 pixel operator/=(ale_real d) {
109 return pixel(x[0] /= d, x[1] /= d, x[2] /= d);
112 pixel clamp() const {
113 pixel result;
115 for (int i = 0; i < 3; i++)
116 if (x[i] > 1.0)
117 result[i] = 1.0;
118 else if (x[i] < 0.0)
119 result[i] = 0.0;
120 else
121 result[i] = x[i];
123 return result;
127 pixel abs() {
128 return pixel(fabs(x[0]), fabs(x[1]), fabs(x[2]));
131 ale_real normsq() {
132 return x[0] * x[0] + x[1] * x[1] + x[2] * x[2];
135 ale_real norm() {
136 return sqrt(normsq());
139 ale_real lnorm() {
140 return x[0] + x[1] + x[2];
143 ale_real maxabs_norm() {
144 ale_real m = fabs(x[0]);
145 if (fabs(x[1]) > m)
146 m = fabs(x[1]);
147 if (fabs(x[2]) > m)
148 m = fabs(x[2]);
150 return m;
153 ale_real minabs_norm() {
154 ale_real m = fabs(x[0]);
155 if (fabs(x[1]) < m)
156 m = fabs(x[1]);
157 if (fabs(x[2]) < m)
158 m = fabs(x[2]);
160 return m;
163 ale_real min_norm() {
164 ale_real m = x[0];
165 if (x[1] < m)
166 m = x[1];
167 if (x[2] < m)
168 m = x[2];
170 return m;
173 ale_real max_norm() {
174 ale_real m = x[0];
175 if (x[1] > m)
176 m = x[1];
177 if (x[2] > m)
178 m = x[2];
180 return m;
183 static pixel zero() {
184 return pixel(0, 0, 0);
187 static pixel one() {
188 return pixel(1, 1, 1);
191 int operator==(const pixel &p) {
192 return x[0] == p[0]
193 && x[1] == p[1]
194 && x[2] == p[2];
197 int operator!=(const pixel &p) {
198 return !operator==(p);
201 int finite() {
202 return ::finite(x[0]) && ::finite(x[1]) && ::finite(x[2]);
205 static pixel undefined() {
206 ale_real zero = 0;
207 return pixel(zero / zero, zero / zero, zero / zero);
212 inline pixel operator*(const pixel &p, float d) {
213 return p.mult(d);
215 inline pixel operator*(const pixel &p, double d) {
216 return p.mult(d);
218 inline pixel operator*(double d, const pixel &p) {
219 return p.mult(d);
221 inline pixel operator*(float d, const pixel &p) {
222 return p.mult(d);
225 inline std::ostream &operator<<(std::ostream &o, const pixel &p) {
226 o << "[" << p[0] << " " << p[1] << " " << p[2] << "]";
227 return o;
230 inline pixel ppow(pixel p, double d) {
231 return pixel(
232 pow((double) p[0], d),
233 pow((double) p[1], d),
234 pow((double) p[2], d));
237 inline pixel ppow(pixel p, float d) {
238 return pixel(
239 pow((double) p[0], d),
240 pow((double) p[1], d),
241 pow((double) p[2], d));
244 inline pixel pexp(pixel p) {
245 return pixel(
246 exp((double) p[0]),
247 exp((double) p[1]),
248 exp((double) p[2]));
251 #endif