tcElevationOptimization::loadSnapShot: simplify reliability expression
[tecorrec.git] / geo / tcPixelData.h
blob2112e96bcad8a34da9e21d7bcb417f7bd3ca5738
1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 #ifndef _tcPixelData_h_
21 #define _tcPixelData_h_
23 /**
24 * @file tcPixelData.h
25 * @brief A block of pixel data.
28 #include "CountedReference.h"
30 #include <QtGlobal>
32 #include <GL/gl.h>
34 #include <cstring>
36 /// Abstract pixel data, independent of type.
37 class tcAbstractPixelData : public ReferenceCounted
39 public:
42 * Types
45 /// Delete when no more references.
46 typedef Referencer<tcAbstractPixelData> DefaultReferencer;
49 * Constructors + destructor
52 /// Primary constructor.
53 tcAbstractPixelData(int width, int height);
55 /// Destructor.
56 virtual ~tcAbstractPixelData();
59 * Accessors
62 /// Get the width of the image.
63 int width() const;
65 /// Get the height of the image.
66 int height() const;
68 /// Get (creating if necessary) a GL texture for this image.
69 GLuint texture();
71 /// Discard of any cached texture id for this image.
72 void discardTexture();
74 /// Sample a byte value from the image.
75 virtual GLubyte sampleUByte(float x, float y) const
77 return 0;
80 /// Sample a floating point value from the image.
81 virtual float sampleFloat(float x, float y) const
83 return 0.0f;
86 protected:
89 * Virtual interface for derived classes to implement.
92 /// Load the pixel data into a GL texture.
93 virtual GLuint loadTexture();
95 protected:
98 * Variables
101 /// Size of pixel data.
102 int m_size[2];
104 /// Also keep reference to an OpenGL texture id for this texture.
105 GLuint m_textureId;
108 /// A block of fairly abstract but typed pixel data.
109 template <typename T>
110 class tcTypedPixelData : public tcAbstractPixelData
112 public:
115 * Constructors + destructor
118 /// Default constructor.
119 tcTypedPixelData()
120 : tcAbstractPixelData(0, 0)
121 , m_data(0)
125 /// Primary constructor.
126 tcTypedPixelData(int width, int height)
127 : tcAbstractPixelData(width, height)
128 , m_data(new T[width*height])
132 /// Destructor.
133 virtual ~tcTypedPixelData()
135 delete [] m_data;
139 * Accessors
142 /// Get pointer to the buffer.
143 T* buffer() const
145 return m_data;
148 T interpolateLinear(float x, float y) const
150 if (x < 0.0) x = 0.0;
151 if (x > 1.0) x = 1.0;
152 if (y < 0.0) y = 0.0;
153 if (y > 1.0) y = 1.0;
155 float dx = x*(width()-1);
156 float dy = y*(height()-1);
157 int xi = (int)dx;
158 int yi = (int)dy;
159 dx -= xi;
160 dy -= yi;
161 if (xi >= width()-1)
163 --xi;
164 dx = 1.0f;
166 if (yi >= height()-1)
168 --yi;
169 dy = 1.0f;
172 return buffer()[yi*width() + xi] * (1.0f - dx) * (1.0f - dy) +
173 buffer()[yi*width() + xi+1] * dx * (1.0f - dy) +
174 buffer()[(yi+1)*width() + xi] * (1.0f - dx) * dy +
175 buffer()[(yi+1)*width() + xi+1] * dx * dy;
178 private:
181 * Variables
184 /// Parent pixel data into which we are looking.
185 T* m_data;
188 /// A block of pixel data.
189 template <typename T>
190 class tcPixelData : public tcTypedPixelData<T>
192 private:
195 * Types
198 // Parent type.
199 typedef tcTypedPixelData<T> Parent;
201 public:
204 * Constructors + destructor
207 /// Default constructor.
208 tcPixelData()
209 : Parent()
213 /// Primary constructor.
214 tcPixelData(int width, int height)
215 : Parent(width, height)
219 /// Destructor.
220 virtual ~tcPixelData()
225 * Accessors
228 // Reimplemented
229 virtual GLubyte sampleUByte(float x, float y) const
231 return (GLubyte)tcPixelData::sampleFloat(x,y);
234 // Reimplemented
235 virtual float sampleFloat(float x, float y) const
237 if (x < 0.0) x = 0.0;
238 if (x > 1.0) x = 1.0;
239 if (y < 0.0) y = 0.0;
240 if (y > 1.0) y = 1.0;
242 float dx = x*(Parent::width()-1);
243 float dy = y*(Parent::height()-1);
244 int xi = (int)dx;
245 int yi = (int)dy;
246 dx -= xi;
247 dy -= yi;
248 if (xi >= Parent::width()-1)
250 --xi;
251 dx = 1.0f;
253 if (yi >= Parent::height()-1)
255 --yi;
256 dy = 1.0f;
259 return (float)Parent::buffer()[yi*Parent::width() + xi] * (1.0f - dx) * (1.0f - dy) +
260 (float)Parent::buffer()[yi*Parent::width() + xi+1] * dx * (1.0f - dy) +
261 (float)Parent::buffer()[(yi+1)*Parent::width() + xi] * (1.0f - dx) * dy +
262 (float)Parent::buffer()[(yi+1)*Parent::width() + xi+1] * dx * dy;
265 protected:
268 * Virtual interface for derived classes to implement.
271 // Reimplemented
272 virtual GLuint loadTexture();
275 #endif