TODO: add entries for centralizing licensing and authorship information and incorpora...
[Ale.git] / d2 / pixel.h
blob1d31c3c9e8ab74dc03a09d10c32a77845b037834
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 2 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[](int i) const {
55 assert (i >= 0);
56 assert (i < 3);
58 return x[i];
61 ale_real &operator[](int i) {
62 assert (i >= 0);
63 assert (i < 3);
65 return x[i];
68 pixel operator+(pixel p) const {
69 return pixel(p[0] + x[0], p[1] + x[1], p[2] + x[2]);
72 pixel operator-(pixel p) const {
73 return pixel(x[0] - p[0], x[1] - p[1], x[2] - p[2]);
76 pixel operator-() const {
77 return pixel(-x[0], -x[1], -x[2]);
80 pixel operator/(pixel p) const {
81 return pixel(x[0] / p[0], x[1] / p[1], x[2] / p[2]);
84 pixel operator/(ale_real d) const {
85 return pixel(x[0] / d, x[1] / d, x[2] / d);
88 pixel operator*(pixel p) const {
89 return pixel(x[0] * p[0], x[1] * p[1], x[2] * p[2]);
92 pixel mult(ale_real d) const {
93 return pixel(x[0] * d, x[1] * d, x[2] * d);
96 pixel operator+=(pixel p) {
97 return pixel(x[0] += p[0], x[1] += p[1], x[2] += p[2]);
100 pixel operator*=(pixel p) {
101 return pixel(x[0] *= p[0], x[1] *= p[1], x[2] *= p[2]);
104 pixel operator*=(ale_real d) {
105 return pixel(x[0] *= d, x[1] *= d, x[2] *= d);
108 pixel operator/=(pixel p) {
109 return pixel(x[0] /= p[0], x[1] /= p[1], x[2] /= p[2]);
112 pixel operator/=(ale_real d) {
113 return pixel(x[0] /= d, x[1] /= d, x[2] /= d);
116 pixel clamp() const {
117 pixel result;
119 for (int i = 0; i < 3; i++)
120 if (x[i] > 1.0)
121 result[i] = 1.0;
122 else if (x[i] < 0.0)
123 result[i] = 0.0;
124 else
125 result[i] = x[i];
127 return result;
131 pixel abs() {
132 return pixel(fabs(x[0]), fabs(x[1]), fabs(x[2]));
135 ale_real normsq() {
136 return x[0] * x[0] + x[1] * x[1] + x[2] * x[2];
139 ale_real norm() {
140 return sqrt(normsq());
143 ale_real lnorm() {
144 return x[0] + x[1] + x[2];
147 ale_real maxabs_norm() {
148 ale_real m = fabs(x[0]);
149 if (fabs(x[1]) > m)
150 m = fabs(x[1]);
151 if (fabs(x[2]) > m)
152 m = fabs(x[2]);
154 return m;
157 ale_real minabs_norm() {
158 ale_real m = fabs(x[0]);
159 if (fabs(x[1]) < m)
160 m = fabs(x[1]);
161 if (fabs(x[2]) < m)
162 m = fabs(x[2]);
164 return m;
167 ale_real min_norm() {
168 ale_real m = x[0];
169 if (x[1] < m)
170 m = x[1];
171 if (x[2] < m)
172 m = x[2];
174 return m;
177 ale_real max_norm() {
178 ale_real m = x[0];
179 if (x[1] > m)
180 m = x[1];
181 if (x[2] > m)
182 m = x[2];
184 return m;
187 static pixel zero() {
188 return pixel(0, 0, 0);
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