OGLplus (0.52.0) a C++ wrapper for OpenGL

filtered.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_IMAGES_FILTERED_1107121519_HPP
14 #define OGLPLUS_IMAGES_FILTERED_1107121519_HPP
15 
16 #include <oglplus/images/image.hpp>
17 #include <oglplus/math/vector.hpp>
18 #include <oglplus/math/matrix.hpp>
19 
20 #include <cassert>
21 #include <cmath>
22 
23 namespace oglplus {
24 namespace images {
25 
27 
31 template <typename T, unsigned CH>
33  : public Image
34 {
35 private:
36  static_assert(
37  CH > 0 && CH <= 4,
38  "Number of channels must be between 1 and 4"
39  );
40 private:
41  template <typename Filter, typename Sampler, typename Extractor>
42  void _calculate(
43  const Image& input,
44  Filter filter,
45  Sampler sampler,
46  Extractor extractor,
47  T one
48  )
49  {
50  sampler.SetInput(input);
51  auto p = this->_begin<T>();
52  unsigned w = input.Width(), h = input.Height(), d = input.Depth();
53 
54  for(unsigned k=0; k!=d; ++k)
55  for(unsigned j=0; j!=h; ++j)
56  for(unsigned i=0; i!=w; ++i)
57  {
58  sampler.SetOrigin(i, j, k);
59 
60  Vector<T, CH> outv = filter(extractor, sampler, one);
61 
62  for(unsigned ci=0; ci!=CH; ++ci)
63  {
64  assert(p != this->_end<T>());
65  *p = outv.At(ci);
66  ++p;
67  }
68  }
69  assert(p == this->_end<T>());
70  }
71 public:
72  struct DefaultFilter
73  {
74  template <typename Extractor, typename Sampler>
75  Vector<T, CH> operator()(
76  const Extractor& extractor,
77  const Sampler& sampler,
78  T one
79  ) const
80  {
81  return Vector<T, CH>(extractor(sampler(0, 0, 0))*one);
82  }
83  };
84 
85  struct NoCoordTransform
86  {
87  void operator()(int, int, int, unsigned, unsigned, unsigned) const
88  {
89  }
90  };
91 
92  class MatrixCoordTransform
93  {
94  private:
95  Matrix<double, 4, 4> _transf;
96  public:
97  MatrixCoordTransform(const Matrix<double, 4, 4>& transf)
98  : _transf(transf)
99  { }
100 
101  void operator()(
102  int& x, int& y, int& z,
103  double w, double h, double d
104  ) const
105  {
106  Vector<double, 4> in((x+0.5)/w, (y+0.5)/h, (z+0.5)/d, 1);
107  Vector<double, 4> out = _transf * in;
108 
109  x = int(std::floor(out.x()*w));
110  y = int(std::floor(out.y()*h));
111  z = int(std::floor(out.z()*d));
112  }
113  };
114 
115  struct RepeatSample
116  {
117  Vector<GLdouble, 4> operator()(
118  const Image& image,
119  unsigned width,
120  unsigned height,
121  unsigned depth,
122  int xpos,
123  int ypos,
124  int zpos
125  ) const
126  {
127  if(xpos >= int(width)) xpos %= width;
128  while(xpos < 0) xpos += width;
129 
130  if(ypos >= int(height)) ypos %= height;
131  while(ypos < 0) ypos += height;
132 
133  if(zpos >= int(depth)) zpos %= depth;
134  while(zpos < 0) zpos += depth;
135 
136  assert((xpos >= 0) && (xpos < int(width)));
137  assert((ypos >= 0) && (ypos < int(height)));
138  assert((zpos >= 0) && (zpos < int(depth)));
139 
140  return image.Pixel(xpos, ypos, zpos);
141  }
142  };
143 
144  template <typename Transform, typename SampleFunc>
145  class SamplerTpl
146  {
147  private:
148  Transform _transf;
149  SampleFunc _sample;
150 
151  const Image* _image;
152  int _ori_x, _ori_y, _ori_z;
153  public:
154  SamplerTpl(
155  const Transform& transf = Transform(),
156  const SampleFunc& sample = SampleFunc()
157  ): _transf(transf)
158  , _sample(sample)
159  , _image(nullptr)
160  , _ori_x(0)
161  , _ori_y(0)
162  , _ori_z(0)
163  { }
164 
165  void SetInput(const Image& image)
166  {
167  _image = &image;
168  }
169 
170  void SetOrigin(
171  unsigned x,
172  unsigned y,
173  unsigned z
174  )
175  {
176  _ori_x = int(x);
177  _ori_y = int(y);
178  _ori_z = int(z);
179 
180  assert(_image);
181 
182  _transf(
183  _ori_x,
184  _ori_y,
185  _ori_z,
186  _image->Width(),
187  _image->Height(),
188  _image->Depth()
189  );
190  }
191 
192  Vector<GLdouble, 4> operator()(
193  int xoffs,
194  int yoffs,
195  int zoffs
196  ) const
197  {
198  assert(_image != nullptr);
199  return _sample(
200  *_image,
201  _image->Width(),
202  _image->Height(),
203  _image->Depth(),
204  _ori_x+xoffs,
205  _ori_y+yoffs,
206  _ori_z+zoffs
207  );
208  }
209  };
210 
211  template <typename SampleFunc>
212  struct SimpleSampler
213  : SamplerTpl<NoCoordTransform, SampleFunc>
214  {
215  SimpleSampler(const SampleFunc& sample = SampleFunc())
216  : SamplerTpl<NoCoordTransform, SampleFunc>(
217  NoCoordTransform(),
218  sample
219  )
220  { }
221  };
222 
223  struct DefaultSampler
224  : SimpleSampler<RepeatSample>
225  { };
226 
227  template <typename SampleFunc>
228  struct MatrixTransformSampler
229  : SamplerTpl<MatrixCoordTransform, SampleFunc>
230  {
231  MatrixTransformSampler(
232  const Matrix<double, 4, 4>& transf,
233  const SampleFunc& sample = SampleFunc()
234  ): SamplerTpl<MatrixCoordTransform, SampleFunc>(
235  MatrixCoordTransform(transf),
236  sample
237  )
238  { }
239  };
240 
242  template <unsigned I>
244  {
245  double operator()(const Vector<double, 4>& v) const
246  {
247  return v.At(I);
248  }
249  };
250 
251  template <unsigned N>
252  struct FirstNComponents
253  {
254  Vector<double, N> operator()(const Vector<double, 4>& v) const
255  {
256  return Vector<double, N>(v);
257  }
258  };
259 
261  typedef FromComponentI<0> FromRed;
263  typedef FromComponentI<1> FromGreen;
265  typedef FromComponentI<2> FromBlue;
267  typedef FromComponentI<3> FromAlpha;
268 
269  typedef FirstNComponents<3> FromRGB;
270  typedef FirstNComponents<4> FromRGBA;
271 
272  template <typename Filter, typename Sampler, typename Extractor>
274  const Image& input,
275  Filter filter,
276  Sampler sampler,
277  Extractor extractor
278  ): Image(input.Width(), input.Height(), input.Depth(), CH, (T*)0)
279  {
280  _calculate(input, filter, sampler, extractor, this->_one((T*)0));
281  }
282 };
283 
284 } // images
285 } // oglplus
286 
287 #endif // include guard
Extractor that allows to specify which component to use as input.
Definition: filtered.hpp:243
Object< SamplerOps > Sampler
An oglplus_object encapsulating the OpenGL sampler functionality.
Definition: sampler.hpp:877
T At(const Vector &vector, std::size_t i) const T x(void) const
Returns the value of the i-th coordinate of the vector.
Definition: vector.hpp:221
FromComponentI< 0 > FromRed
Extractor selecting the Red component of the input image.
Definition: filtered.hpp:261
GLsizei Depth(void) const
Returns the depth of the image.
Definition: image.hpp:246
T z(void) const
Returns the 2-nd component.
Definition: vector.hpp:237
GLsizei Width(void) const
Returns the width of the image.
Definition: image.hpp:234
Base class for various image filters.
Definition: filtered.hpp:32
T y(void) const
Returns the 1-st component.
Definition: vector.hpp:231
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
Basic template for vector types.
Definition: fwd.hpp:43
FromComponentI< 2 > FromBlue
Extractor selecting the Blue component of the input image.
Definition: filtered.hpp:265
FromComponentI< 3 > FromAlpha
Extractor selecting the Alpha component of the input image.
Definition: filtered.hpp:267
A vector class.
FromComponentI< 1 > FromGreen
Extractor selecting the Green component of the input image.
Definition: filtered.hpp:263
Image data wrapper.
A matrix 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).