OGLplus (0.52.0) a C++ wrapper for OpenGL

image.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_IMAGES_IMAGE_1107121519_HPP
14 #define OGLPLUS_IMAGES_IMAGE_1107121519_HPP
15 
16 #include <limits>
17 #include <cassert>
18 #include <cstddef>
19 #include <cstring>
20 #include <oglplus/math/vector.hpp>
21 #include <oglplus/data_type.hpp>
22 #include <oglplus/pixel_data.hpp>
23 #include <oglplus/detail/aligned_pod_array.hpp>
24 
25 namespace oglplus {
26 namespace images {
27 
39 
45 class Image
46 {
47 private:
48  GLsizei _width, _height, _depth, _channels;
49  PixelDataType _type;
50  oglplus::aux::AlignedPODArray _storage;
51  double (*_convert)(void*);
52 
53  template <typename T>
54  static double _do_convert(void* ptr)
55  {
56  assert(ptr != nullptr);
57  const double v = double(*static_cast<T*>(ptr));
58  const double n = double(_one((T*)nullptr));
59  return v / n;
60  }
61 
62  bool _is_initialized(void) const;
63 
64  static PixelDataFormat _get_def_pdf(unsigned N);
65  static PixelDataInternalFormat _get_def_pdif(unsigned N);
66 
67 protected:
68  PixelDataFormat _format;
69  PixelDataInternalFormat _internal;
70 
71  template <typename T>
72  static T _one(T*)
73  {
74  return std::numeric_limits<T>::max();
75  }
76 
77  static float _one(float*)
78  {
79  return 1.0f;
80  }
81 
82  static double _one(double*)
83  {
84  return 1.0;
85  }
86 
87  template <typename T>
88  bool _type_ok(void) const
89  {
90  return _type == PixelDataType(GetDataType<T>());
91  }
92 
93  template <typename T>
94  T* _begin(void)
95  {
96  assert(_is_initialized());
97  assert(_type_ok<T>());
98  return static_cast<T*>(_storage.begin());
99  }
100 
101  unsigned char* _begin_ub(void)
102  {
103  return _begin<unsigned char>();
104  }
105 
106  template <typename T>
107  T* _end(void)
108  {
109  assert(_is_initialized());
110  assert(_type_ok<T>());
111  return static_cast<T*>(_storage.end());
112  }
113 
114  unsigned char* _end_ub(void)
115  {
116  return _end<unsigned char>();
117  }
118 
119  template <typename T>
120  T& _at(unsigned x, unsigned y=0, unsigned z=0)
121  {
122  return *static_cast<T*>(_storage.at(PixelPos(x, y, z)));
123  }
124 
125  void _bzero(void)
126  {
127  _storage.fill(0x00);
128  }
129 
130  Image(void)
131  : _width(0)
132  , _height(0)
133  , _depth(0)
134  , _channels(0)
135  , _convert(nullptr)
136  { }
137 
138 public:
139  Image(const Image& that)
140  : _width(that._width)
141  , _height(that._height)
142  , _depth(that._depth)
143  , _channels(that._channels)
144  , _type(that._type)
145  , _storage(that._storage)
146  , _convert(that._convert)
147  , _format(that._format)
148  , _internal(that._internal)
149  { }
150 
151  Image(Image&& tmp)
152  : _width(tmp._width)
153  , _height(tmp._height)
154  , _depth(tmp._depth)
155  , _channels(tmp._channels)
156  , _type(tmp._type)
157  , _storage(std::move(tmp._storage))
158  , _convert(tmp._convert)
159  , _format(tmp._format)
160  , _internal(tmp._internal)
161  { }
162 
163  template <typename T>
164  Image(
165  GLsizei width,
166  GLsizei height,
167  GLsizei depth,
168  GLsizei channels,
169  const T* data
170  ): _width(width)
171  , _height(height)
172  , _depth(depth)
173  , _channels(channels)
174  , _type(PixelDataType(GetDataType<T>()))
175  , _storage(oglplus::aux::AlignedPODArray(data, _width*_height*_depth*_channels))
176  , _convert(&_do_convert<T>)
177  , _format(_get_def_pdf(channels))
178  , _internal(_get_def_pdif(channels))
179  { }
180 
181  template <typename T>
182  Image(
183  GLsizei width,
184  GLsizei height,
185  GLsizei depth,
186  GLsizei channels,
187  const T* data,
188  PixelDataFormat format,
189  PixelDataInternalFormat internal
190  ): _width(width)
191  , _height(height)
192  , _depth(depth)
193  , _channels(channels)
194  , _type(PixelDataType(GetDataType<T>()))
195  , _storage(oglplus::aux::AlignedPODArray(data, _width*_height*_depth*_channels))
196  , _convert(&_do_convert<T>)
197  , _format(format)
198  , _internal(internal)
199  { }
200 
201  Image& operator = (Image&& tmp)
202  {
203  _width = tmp._width;
204  _height = tmp._height;
205  _depth = tmp._depth;
206  _channels = tmp._channels;
207  _type = tmp._type;
208  _storage = std::move(tmp._storage);
209  _convert = tmp._convert;
210  _format = tmp._format;
211  _internal = tmp._internal;
212  return *this;
213  }
214 
216 
222  GLsizei Dimension(std::size_t i) const
223  {
224  if(i == 0) return Width();
225  if(i == 1) return Height();
226  if(i == 2) return Depth();
227  if(i == 3) return Channels();
228  assert(!"Invalid image dimension specified");
229  return -1;
230  }
231 
232 
234  GLsizei Width(void) const
235  {
236  return _width;
237  }
238 
240  GLsizei Height(void) const
241  {
242  return _height;
243  }
244 
246  GLsizei Depth(void) const
247  {
248  return _depth;
249  }
250 
252  GLsizei Channels(void) const
253  {
254  return _channels;
255  }
256 
258  PixelDataType Type(void) const
259  {
260  assert(_is_initialized());
261  return _type;
262  }
263 
266  {
267  assert(_is_initialized());
268  return _format;
269  }
270 
273  {
274  assert(_is_initialized());
275  return _internal;
276  }
277 
279  template <typename T>
280  const T* Data(void) const
281  {
282  assert(_is_initialized());
283  assert(_type_ok<T>());
284  return static_cast<T*>(_storage.begin());
285  }
286 
288  const void* RawData(void) const
289  {
290  assert(_is_initialized());
291  return _storage.begin();
292  }
293 
295  std::size_t DataSize(void) const
296  {
297  assert(_is_initialized());
298  return _storage.size();
299  }
300 
301  std::size_t PixelPos(
302  GLsizei width,
303  GLsizei height,
304  GLsizei depth
305  ) const
306  {
307  assert(_is_initialized());
308  assert(width >= 0);
309  assert(height >= 0);
310  assert(depth >= 0);
311  assert(width < Width());
312  assert(height < Height());
313  assert(depth < Depth());
314  GLsizei ppos = depth*Height()*Width()+height*Width()+width;
315  return std::size_t(ppos*Channels());
316  }
317 
320  GLsizei width,
321  GLsizei height,
322  GLsizei depth
323  ) const
324  {
325  assert(_convert);
326 
327  std::size_t ppos = PixelPos(width, height, depth);
328  return Vector<double, 4>(
329  _channels>0?_convert(_storage.at(ppos+0)):0.0,
330  _channels>1?_convert(_storage.at(ppos+1)):0.0,
331  _channels>2?_convert(_storage.at(ppos+2)):0.0,
332  _channels>3?_convert(_storage.at(ppos+3)):0.0
333  );
334  }
335 
336  std::size_t ComponentPos(
337  GLsizei width,
338  GLsizei height,
339  GLsizei depth,
340  GLsizei component
341  ) const
342  {
343  std::size_t ppos = PixelPos(width, height, depth);
344  return std::size_t(ppos+component);
345  }
346 
348  double Component(
349  GLsizei width,
350  GLsizei height,
351  GLsizei depth,
352  GLsizei component
353  ) const
354  {
355  if(component >= Channels()) return 0.0;
356  assert(_convert);
357  return _convert(_storage.at(ComponentPos(
358  width,
359  height,
360  depth,
361  component
362  )));
363  }
364 
366  template <typename T>
368  GLsizei width,
369  GLsizei height,
370  GLsizei depth,
371  GLsizei component
372  ) const
373  {
374  assert(_type_ok<T>());
375  if(component >= Channels()) return T(0);
376  return *static_cast<T*>(_storage.at(ComponentPos(
377  width,
378  height,
379  depth,
380  component
381  )));
382  }
383 };
384 
385 } // namespace images
386 } // namespace oglplus
387 
388 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
389 #include <oglplus/images/image.ipp>
390 #endif
391 
392 #endif // include guard
const void * RawData(void) const
Returns an untyped pointer to the data.
Definition: image.hpp:288
double Component(GLsizei width, GLsizei height, GLsizei depth, GLsizei component) const
Returns the component of the pixel at the specified coordinates.
Definition: image.hpp:348
PixelDataInternalFormat
OpenGL pixel data internal format enumeration.
Definition: pixel_data.hpp:79
GLsizei Channels(void) const
Returns the number of channels.
Definition: image.hpp:252
GLsizei Depth(void) const
Returns the depth of the image.
Definition: image.hpp:246
PixelDataType
OpenGL pixel data type enumeration.
Definition: pixel_data.hpp:29
GLsizei Width(void) const
Returns the width of the image.
Definition: image.hpp:234
PixelDataFormat
OpenGL pixel data format enumeration.
Definition: pixel_data.hpp:50
GLsizei Dimension(std::size_t i) const
Returns the i-th dimension of the image.
Definition: image.hpp:222
Data type-related declarations.
Pixel data-related declarations.
PixelDataFormat Format(void) const
Return the pixel data format.
Definition: image.hpp:265
Vector< double, 4 > Pixel(GLsizei width, GLsizei height, GLsizei depth) const
Returns the pixel at the specified coordinates.
Definition: image.hpp:319
GLsizei Height(void) const
Returns the height of the image.
Definition: image.hpp:240
Wrapper for (texture) image data.
Definition: image.hpp:45
PixelDataType Type(void) const
Returns the pixel data type.
Definition: image.hpp:258
T ComponentAs(GLsizei width, GLsizei height, GLsizei depth, GLsizei component) const
Returns the component of the pixel at the specified coordinates.
Definition: image.hpp:367
PixelDataInternalFormat InternalFormat(void) const
Return a suitable pixel data internal format.
Definition: image.hpp:272
const T * Data(void) const
Returns a pointer to the data.
Definition: image.hpp:280
std::size_t DataSize(void) const
Returns the size of data in bytes.
Definition: image.hpp:295
Basic template for vector types.
Definition: fwd.hpp:43
A vector class.

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).