Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImageTransforms.h
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
27 #ifndef _VIGRA_EXT_IMAGETRANSFORMS_H
28 #define _VIGRA_EXT_IMAGETRANSFORMS_H
29 
30 #include <fstream>
31 
32 #include <vigra/basicimage.hxx>
33 #include <vigra_ext/ROIImage.h>
35 
36 #include <hugin_math/hugin_math.h>
37 #include <hugin_utils/utils.h>
39 
40 namespace vigra_ext
41 {
42 
44 template <class T>
46 {
47  if (p < 0) {
48  return vigra::NumericTraits<T>::zero();
49  } else {
50  return p;
51  }
52 }
53 
55 template <class T>
56 vigra::RGBValue<T> zeroNegative(vigra::RGBValue<T> p)
57 {
58  if (p.red() < 0) p.setRed(vigra::NumericTraits<T>::zero());
59  if (p.green() < 0) p.setGreen(vigra::NumericTraits<T>::zero());
60  if (p.blue() < 0) p.setBlue(vigra::NumericTraits<T>::zero());
61  return p;
62 }
63 
64 
90 template <class SrcImageIterator, class SrcAccessor,
91  class DestImageIterator, class DestAccessor,
92  class TRANSFORM,
93  class PixelTransform,
94  class AlphaImageIterator, class AlphaAccessor,
95  class Interpolator>
96 void transformImageIntern(vigra::triple<SrcImageIterator, SrcImageIterator, SrcAccessor> src,
97  vigra::triple<DestImageIterator, DestImageIterator, DestAccessor> dest,
98  std::pair<AlphaImageIterator, AlphaAccessor> alpha,
99  TRANSFORM & transform,
100  PixelTransform & pixelTransform,
101  vigra::Diff2D destUL,
102  Interpolator interp,
103  bool warparound,
104  AppBase::ProgressDisplay* progress,
105  bool singleThreaded)
106 {
107  const vigra::Diff2D destSize = dest.second - dest.first;
108 
109  const int xstart = destUL.x;
110  const int xend = destUL.x + destSize.x;
111  const int ystart = destUL.y;
112  const int yend = destUL.y + destSize.y;
113 
115  interpol(src, interp, warparound);
116 
117  // loop over the image and transform
118 #pragma omp parallel for if(!singleThreaded) schedule(dynamic)
119  for (int y = ystart; y < yend; ++y)
120  {
121  // create x iterators
122  DestImageIterator xd(dest.first);
123  xd.y += y - ystart;
124  AlphaImageIterator xdm(alpha.first);
125  xdm.y += y - ystart;
126  typename SrcAccessor::value_type tempval;
127  for (int x = xstart; x < xend; ++x, ++xd.x, ++xdm.x)
128  {
129  double sx, sy;
130  if (transform.transformImgCoord(sx, sy, x, y)) {
131  if (interpol.operator()(sx, sy, tempval)){
132  // apply pixel transform and write to output
133  dest.third.set(zeroNegative(pixelTransform(tempval, hugin_utils::FDiff2D(sx, sy))), xd);
134  alpha.second.set(pixelTransform.hdrWeight(tempval, vigra::UInt8(255)), xdm);
135  }
136  else {
137  alpha.second.set(0, xdm);
138  }
139  }
140  else {
141  alpha.second.set(0, xdm);
142  }
143  }
144  }
145 }
146 
148 template <class SrcImageIterator, class SrcAccessor,
149  class SrcAlphaIterator, class SrcAlphaAccessor,
150  class DestImageIterator, class DestAccessor,
151  class TRANSFORM,
152  class PixelTransform,
153  class AlphaImageIterator, class AlphaAccessor,
154  class Interpolator>
155 void transformImageAlphaIntern(vigra::triple<SrcImageIterator, SrcImageIterator, SrcAccessor> src,
156  std::pair<SrcAlphaIterator, SrcAlphaAccessor> srcAlpha,
157  vigra::triple<DestImageIterator, DestImageIterator, DestAccessor> dest,
158  std::pair<AlphaImageIterator, AlphaAccessor> alpha,
159  TRANSFORM & transform,
160  PixelTransform & pixelTransform,
161  vigra::Diff2D destUL,
162  Interpolator interp,
163  bool warparound,
164  AppBase::ProgressDisplay* progress,
165  bool singleThreaded)
166 {
167  const vigra::Diff2D destSize = dest.second - dest.first;
168 
169  const int xstart = destUL.x;
170  const int xend = destUL.x + destSize.x;
171  const int ystart = destUL.y;
172  const int yend = destUL.y + destSize.y;
173 
174  vigra_ext::ImageMaskInterpolator<SrcImageIterator, SrcAccessor, SrcAlphaIterator,
175  SrcAlphaAccessor, Interpolator>
176  interpol (src, srcAlpha, interp, warparound);
177 
178  // loop over the image and transform
179 #pragma omp parallel for if(!singleThreaded) schedule(dynamic)
180  for(int y=ystart; y < yend; ++y)
181  {
182  // create x iterators
183  DestImageIterator xd(dest.first);
184  xd.y += y - ystart;
185  AlphaImageIterator xdist(alpha.first);
186  xdist.y += y - ystart;
187  typename SrcAccessor::value_type tempval;
188  typename SrcAlphaAccessor::value_type alphaval;
189  for (int x = xstart; x < xend; ++x, ++xd.x, ++xdist.x)
190  {
191  double sx,sy;
192  if (transform.transformImgCoord(sx,sy,x,y)) {
193  // try to interpolate.
194  if (interpol(sx, sy, tempval, alphaval)) {
195  dest.third.set(zeroNegative(pixelTransform(tempval, hugin_utils::FDiff2D(sx, sy))), xd);
196  alpha.second.set(pixelTransform.hdrWeight(tempval, alphaval), xdist);
197  } else {
198  // point outside of image or mask
199  alpha.second.set(0, xdist);
200  }
201  } else {
202  alpha.second.set(0, xdist);
203  }
204  }
205  }
206 };
207 
232 template <class SrcImageIterator, class SrcAccessor,
233  class DestImageIterator, class DestAccessor,
234  class AlphaImageIterator, class AlphaAccessor,
235  class TRANSFORM,
236  class PixelTransform>
237 void transformImage(vigra::triple<SrcImageIterator, SrcImageIterator, SrcAccessor> src,
238  vigra::triple<DestImageIterator, DestImageIterator, DestAccessor> dest,
239  std::pair<AlphaImageIterator, AlphaAccessor> alpha,
240  vigra::Diff2D destUL,
241  TRANSFORM & transform,
242  PixelTransform & pixelTransform,
243  bool warparound,
244  Interpolator interpol,
245  AppBase::ProgressDisplay* progress, bool singleThreaded = false)
246 {
247  switch (interpol) {
248  case INTERP_CUBIC:
249  DEBUG_DEBUG("using cubic interpolator");
250  transformImageIntern(src, dest, alpha, transform, pixelTransform, destUL,
251  vigra_ext::interp_cubic(), warparound,
252  progress, singleThreaded);
253  break;
254  case INTERP_SPLINE_16:
255  DEBUG_DEBUG("interpolator: spline16");
256  transformImageIntern(src, dest, alpha, transform, pixelTransform, destUL,
257  vigra_ext::interp_spline16(), warparound,
258  progress, singleThreaded);
259  break;
260  case INTERP_SPLINE_36:
261  DEBUG_DEBUG("interpolator: spline36");
262  transformImageIntern(src, dest, alpha, transform, pixelTransform, destUL,
263  vigra_ext::interp_spline36(), warparound,
264  progress, singleThreaded);
265  break;
266  case INTERP_SPLINE_64:
267  DEBUG_DEBUG("interpolator: spline64");
268  transformImageIntern(src, dest, alpha, transform, pixelTransform, destUL,
269  vigra_ext::interp_spline64(), warparound,
270  progress, singleThreaded);
271  break;
272  case INTERP_SINC_256:
273  DEBUG_DEBUG("interpolator: sinc 256");
274  transformImageIntern(src, dest, alpha, transform, pixelTransform, destUL,
275  vigra_ext::interp_sinc<8>(), warparound,
276  progress, singleThreaded);
277  break;
278  case INTERP_BILINEAR:
279  transformImageIntern(src, dest, alpha, transform, pixelTransform, destUL,
280  vigra_ext::interp_bilin(), warparound,
281  progress, singleThreaded);
282  break;
284  transformImageIntern(src, dest, alpha, transform, pixelTransform, destUL,
285  vigra_ext::interp_nearest(), warparound,
286  progress, singleThreaded);
287  break;
288  case INTERP_SINC_1024:
289  transformImageIntern(src, dest, alpha, transform, pixelTransform, destUL,
290  vigra_ext::interp_sinc<32>(), warparound,
291  progress, singleThreaded);
292  break;
293  }
294 }
295 
297 template <class SrcImageIterator, class SrcAccessor,
298  class SrcAlphaIterator, class SrcAlphaAccessor,
299  class DestImageIterator, class DestAccessor,
300  class AlphaImageIterator, class AlphaAccessor,
301  class TRANSFORM, class PixelTransform>
302 void transformImageAlpha(vigra::triple<SrcImageIterator, SrcImageIterator, SrcAccessor> src,
303  std::pair<SrcAlphaIterator, SrcAlphaAccessor> srcAlpha,
304  vigra::triple<DestImageIterator, DestImageIterator, DestAccessor> dest,
305  std::pair<AlphaImageIterator, AlphaAccessor> alpha,
306  vigra::Diff2D destUL,
307  TRANSFORM & transform,
308  PixelTransform & pixelTransform,
309  bool warparound,
310  Interpolator interpol,
311  AppBase::ProgressDisplay* progress, bool singleThreaded = false)
312 {
313  switch (interpol) {
314  case INTERP_CUBIC:
315  DEBUG_DEBUG("using cubic interpolator");
316  transformImageAlphaIntern(src,srcAlpha, dest, alpha, transform, pixelTransform, destUL,
317  vigra_ext::interp_cubic(), warparound,
318  progress, singleThreaded);
319  break;
320  case INTERP_SPLINE_16:
321  DEBUG_DEBUG("interpolator: spline16");
322  transformImageAlphaIntern(src,srcAlpha, dest, alpha, transform, pixelTransform, destUL,
323  vigra_ext::interp_spline16(), warparound,
324  progress, singleThreaded);
325  break;
326  case INTERP_SPLINE_36:
327  DEBUG_DEBUG("interpolator: spline36");
328  transformImageAlphaIntern(src,srcAlpha, dest, alpha, transform, pixelTransform, destUL,
329  vigra_ext::interp_spline36(), warparound,
330  progress, singleThreaded);
331  break;
332  case INTERP_SPLINE_64:
333  DEBUG_DEBUG("interpolator: spline64");
334  transformImageAlphaIntern(src,srcAlpha, dest, alpha, transform, pixelTransform, destUL,
335  vigra_ext::interp_spline64(), warparound,
336  progress, singleThreaded);
337  break;
338  case INTERP_SINC_256:
339  DEBUG_DEBUG("interpolator: sinc 256");
340  transformImageAlphaIntern(src,srcAlpha, dest, alpha, transform, pixelTransform, destUL,
341  vigra_ext::interp_sinc<8>(), warparound,
342  progress, singleThreaded);
343  break;
344  case INTERP_BILINEAR:
345  transformImageAlphaIntern(src,srcAlpha, dest, alpha, transform, pixelTransform, destUL,
346  vigra_ext::interp_bilin(), warparound,
347  progress, singleThreaded);
348  break;
350  transformImageAlphaIntern(src,srcAlpha, dest, alpha, transform, pixelTransform, destUL,
351  vigra_ext::interp_nearest(), warparound,
352  progress, singleThreaded);
353  break;
354  case INTERP_SINC_1024:
355  transformImageAlphaIntern(src,srcAlpha, dest, alpha, transform, pixelTransform, destUL,
356  vigra_ext::interp_sinc<32>(), warparound,
357  progress, singleThreaded);
358  break;
359  }
360 }
361 
362 }; // namespace
363 
364 #endif // _VIGRA_EXT_IMAGETRANSFORMS_H
void transformImage(vigra::triple< SrcImageIterator, SrcImageIterator, SrcAccessor > src, vigra::triple< DestImageIterator, DestImageIterator, DestAccessor > dest, std::pair< AlphaImageIterator, AlphaAccessor > alpha, vigra::Diff2D destUL, TRANSFORM &transform, PixelTransform &pixelTransform, bool warparound, Interpolator interpol, AppBase::ProgressDisplay *progress, bool singleThreaded=false)
Transform an image into the panorama.
simple bilinear interpolation
misc math function &amp; classes used by other parts of the program
&quot;wrapper&quot; for efficient interpolation access to an image
cubic interpolation
void transformImageAlphaIntern(vigra::triple< SrcImageIterator, SrcImageIterator, SrcAccessor > src, std::pair< SrcAlphaIterator, SrcAlphaAccessor > srcAlpha, vigra::triple< DestImageIterator, DestImageIterator, DestAccessor > dest, std::pair< AlphaImageIterator, AlphaAccessor > alpha, TRANSFORM &transform, PixelTransform &pixelTransform, vigra::Diff2D destUL, Interpolator interp, bool warparound, AppBase::ProgressDisplay *progress, bool singleThreaded)
transform input images with alpha channel
sinc interpolation, with variable width
spline16 interpolation
spline36 interpolation
void transformImageIntern(vigra::triple< SrcImageIterator, SrcImageIterator, SrcAccessor > src, vigra::triple< DestImageIterator, DestImageIterator, DestAccessor > dest, std::pair< AlphaImageIterator, AlphaAccessor > alpha, TRANSFORM &transform, PixelTransform &pixelTransform, vigra::Diff2D destUL, Interpolator interp, bool warparound, AppBase::ProgressDisplay *progress, bool singleThreaded)
Transform an image into the panorama.
several classes to calculate interpolator weights,
Definition: Interpolators.h:96
The pano tools interpolators ported to vigra.
spline64 interpolation
void transformImageAlpha(vigra::triple< SrcImageIterator, SrcImageIterator, SrcAccessor > src, std::pair< SrcAlphaIterator, SrcAlphaAccessor > srcAlpha, vigra::triple< DestImageIterator, DestImageIterator, DestAccessor > dest, std::pair< AlphaImageIterator, AlphaAccessor > alpha, vigra::Diff2D destUL, TRANSFORM &transform, PixelTransform &pixelTransform, bool warparound, Interpolator interpol, AppBase::ProgressDisplay *progress, bool singleThreaded=false)
Transform image, and respect a possible alpha channel.
#define DEBUG_DEBUG(msg)
Definition: utils.h:68
&quot;wrapper&quot; for efficient interpolation access to an image
Interpolator
enum with all interpolation methods
Definition: Interpolators.h:78
T zeroNegative(T p)
Set negative elements of a pixel to zero.