Hard constrain and bilinear/bicubic interpolation in elevation optimisation config
[tecorrec.git] / geo / tcPixelData.h
blobce59925f068dde69e00d28ccfe7727afc656e14c
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 private:
151 * Variables
154 /// Parent pixel data into which we are looking.
155 T* m_data;
158 /// A block of pixel data.
159 template <typename T>
160 class tcPixelData : public tcTypedPixelData<T>
162 private:
165 * Types
168 // Parent type.
169 typedef tcTypedPixelData<T> Parent;
171 public:
174 * Constructors + destructor
177 /// Default constructor.
178 tcPixelData()
179 : Parent()
183 /// Primary constructor.
184 tcPixelData(int width, int height)
185 : Parent(width, height)
189 /// Destructor.
190 virtual ~tcPixelData()
195 * Accessors
198 // Reimplemented
199 virtual GLubyte sampleUByte(float x, float y) const
201 return (GLubyte)tcPixelData::sampleFloat(x,y);
204 // Reimplemented
205 virtual float sampleFloat(float x, float y) const
207 if (x < 0.0) x = 0.0;
208 if (x > 1.0) x = 1.0;
209 if (y < 0.0) y = 0.0;
210 if (y > 1.0) y = 1.0;
212 float dx = x*(Parent::width()-1);
213 float dy = y*(Parent::height()-1);
214 int xi = (int)dx;
215 int yi = (int)dy;
216 dx -= xi;
217 dy -= yi;
218 if (xi >= Parent::width()-1)
220 --xi;
221 dx = 1.0f;
223 if (yi >= Parent::height()-1)
225 --yi;
226 dy = 1.0f;
229 return (float)Parent::buffer()[yi*Parent::width() + xi] * (1.0f - dx) * (1.0f - dy) +
230 (float)Parent::buffer()[yi*Parent::width() + xi+1] * dx * (1.0f - dy) +
231 (float)Parent::buffer()[(yi+1)*Parent::width() + xi] * (1.0f - dx) * dy +
232 (float)Parent::buffer()[(yi+1)*Parent::width() + xi+1] * dx * dy;
235 protected:
238 * Virtual interface for derived classes to implement.
241 // Reimplemented
242 virtual GLuint loadTexture();
245 #endif