1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
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. *
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. *
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_
25 * @brief A block of pixel data.
28 #include "CountedReference.h"
36 /// Abstract pixel data, independent of type.
37 class tcAbstractPixelData
: public ReferenceCounted
45 /// Delete when no more references.
46 typedef Referencer
<tcAbstractPixelData
> DefaultReferencer
;
49 * Constructors + destructor
52 /// Primary constructor.
53 tcAbstractPixelData(int width
, int height
);
56 virtual ~tcAbstractPixelData();
62 /// Get the width of the image.
65 /// Get the height of the image.
68 /// Get (creating if necessary) a GL texture for this image.
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
80 /// Sample a floating point value from the image.
81 virtual float sampleFloat(float x
, float y
) const
89 * Virtual interface for derived classes to implement.
92 /// Load the pixel data into a GL texture.
93 virtual GLuint
loadTexture();
101 /// Size of pixel data.
104 /// Also keep reference to an OpenGL texture id for this texture.
108 /// A block of fairly abstract but typed pixel data.
109 template <typename T
>
110 class tcTypedPixelData
: public tcAbstractPixelData
115 * Constructors + destructor
118 /// Default constructor.
120 : tcAbstractPixelData(0, 0)
125 /// Primary constructor.
126 tcTypedPixelData(int width
, int height
)
127 : tcAbstractPixelData(width
, height
)
128 , m_data(new T
[width
*height
])
133 virtual ~tcTypedPixelData()
142 /// Get pointer to the buffer.
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);
166 if (yi
>= height()-1)
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
;
184 /// Parent pixel data into which we are looking.
188 /// A block of pixel data.
189 template <typename T
>
190 class tcPixelData
: public tcTypedPixelData
<T
>
199 typedef tcTypedPixelData
<T
> Parent
;
204 * Constructors + destructor
207 /// Default constructor.
213 /// Primary constructor.
214 tcPixelData(int width
, int height
)
215 : Parent(width
, height
)
220 virtual ~tcPixelData()
229 virtual GLubyte
sampleUByte(float x
, float y
) const
231 return (GLubyte
)tcPixelData::sampleFloat(x
,y
);
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);
248 if (xi
>= Parent::width()-1)
253 if (yi
>= Parent::height()-1)
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
;
268 * Virtual interface for derived classes to implement.
272 virtual GLuint
loadTexture();