37 #ifndef VIGRA_RESIZEIMAGE_HXX
38 #define VIGRA_RESIZEIMAGE_HXX
41 #include "utilities.hxx"
42 #include "numerictraits.hxx"
43 #include "stdimage.hxx"
44 #include "recursiveconvolution.hxx"
45 #include "separableconvolution.hxx"
46 #include "resampling_convolution.hxx"
47 #include "splines.hxx"
48 #include "multi_shape.hxx"
134 return prefilterCoefficients_;
144 ArrayVector<double> CoscotFunction<T>::prefilterCoefficients_;
165 template <
class SrcIterator,
class SrcAccessor,
166 class DestIterator,
class DestAccessor>
168 resizeLineNoInterpolation(SrcIterator i1, SrcIterator iend, SrcAccessor as,
169 DestIterator
id, DestIterator idend, DestAccessor ad)
171 int wold = iend - i1;
172 int wnew = idend - id;
180 double dx = (double)(wold - 1) / (wnew - 1);
182 for(;
id != idend; ++id, x += dx)
185 ad.set(as(i1, ix),
id);
279 doxygen_overloaded_function(template <...>
void resizeImageNoInterpolation)
281 template <
class SrcIterator,
class SrcAccessor,
282 class DestIterator,
class DestAccessor>
285 DestIterator
id, DestIterator idend, DestAccessor da)
287 int w = iend.x - is.x;
288 int h = iend.y - is.y;
290 int wnew = idend.x -
id.x;
291 int hnew = idend.y -
id.y;
293 vigra_precondition((w > 1) && (h > 1),
294 "resizeImageNoInterpolation(): "
295 "Source image too small.\n");
296 vigra_precondition((wnew > 1) && (hnew > 1),
297 "resizeImageNoInterpolation(): "
298 "Destination image too small.\n");
300 typedef BasicImage<typename SrcAccessor::value_type> TmpImage;
301 typedef typename TmpImage::traverser TmpImageIterator;
303 TmpImage tmp(w, hnew);
305 TmpImageIterator yt = tmp.upperLeft();
307 for(
int x=0; x<w; ++x, ++is.x, ++yt.x)
309 typename SrcIterator::column_iterator c1 = is.columnIterator();
310 typename TmpImageIterator::column_iterator ct = yt.columnIterator();
312 resizeLineNoInterpolation(c1, c1 + h, sa, ct, ct + hnew, tmp.accessor());
315 yt = tmp.upperLeft();
317 for(
int y=0; y < hnew; ++y, ++yt.y, ++
id.y)
319 typename DestIterator::row_iterator rd =
id.rowIterator();
320 typename TmpImageIterator::row_iterator rt = yt.rowIterator();
322 resizeLineNoInterpolation(rt, rt + w, tmp.accessor(), rd, rd + wnew, da);
326 template <
class SrcIterator,
class SrcAccessor,
327 class DestIterator,
class DestAccessor>
330 triple<DestIterator, DestIterator, DestAccessor> dest)
333 dest.first, dest.second, dest.third);
336 template <
class T1,
class S1,
340 MultiArrayView<2, T2, S2> dest)
343 destImageRange(dest));
352 template <
class SrcIterator,
class SrcAccessor,
353 class DestIterator,
class DestAccessor>
355 resizeLineLinearInterpolation(SrcIterator i1, SrcIterator iend, SrcAccessor as,
356 DestIterator
id, DestIterator idend, DestAccessor ad)
358 int wold = iend - i1;
359 int wnew = idend - id;
361 if((wold <= 1) || (wnew <= 1))
return;
364 NumericTraits<typename DestAccessor::value_type> DestTraits;
365 typedef typename DestTraits::RealPromote RealPromote;
367 ad.set(DestTraits::fromRealPromote(as(i1)),
id);
371 ad.set(DestTraits::fromRealPromote(as(iend)), idend);
373 double dx = (double)(wold - 1) / (wnew - 1);
376 for(;
id != idend; ++id, x += dx)
386 ad.set(DestTraits::fromRealPromote(RealPromote(x1 * as(i1) + x * as(i1, 1))),
id);
494 doxygen_overloaded_function(template <...>
void resizeImageLinearInterpolation)
496 template <
class SrcIterator,
class SrcAccessor,
497 class DestIterator,
class DestAccessor>
500 DestIterator
id, DestIterator idend, DestAccessor da)
502 int w = iend.x - is.x;
503 int h = iend.y - is.y;
505 int wnew = idend.x -
id.x;
506 int hnew = idend.y -
id.y;
508 vigra_precondition((w > 1) && (h > 1),
509 "resizeImageLinearInterpolation(): "
510 "Source image too small.\n");
511 vigra_precondition((wnew > 1) && (hnew > 1),
512 "resizeImageLinearInterpolation(): "
513 "Destination image too small.\n");
515 double const scale = 2.0;
517 typedef typename SrcAccessor::value_type SRCVT;
518 typedef typename NumericTraits<SRCVT>::RealPromote TMPTYPE;
519 typedef BasicImage<TMPTYPE> TmpImage;
520 typedef typename TmpImage::traverser TmpImageIterator;
522 BasicImage<TMPTYPE> tmp(w, hnew);
523 BasicImage<TMPTYPE> line((h > w) ? h : w, 1);
527 typename BasicImage<TMPTYPE>::Iterator yt = tmp.upperLeft();
528 typename TmpImageIterator::row_iterator lt = line.upperLeft().rowIterator();
530 for(x=0; x<w; ++x, ++is.x, ++yt.x)
532 typename SrcIterator::column_iterator c1 = is.columnIterator();
533 typename TmpImageIterator::column_iterator ct = yt.columnIterator();
538 lt, line.accessor(), (double)h/hnew/scale);
540 resizeLineLinearInterpolation(lt, lt + h, line.accessor(),
541 ct, ct + hnew, tmp.accessor());
545 resizeLineLinearInterpolation(c1, c1 + h, sa,
546 ct, ct + hnew, tmp.accessor());
550 yt = tmp.upperLeft();
552 for(y=0; y < hnew; ++y, ++yt.y, ++
id.y)
554 typename DestIterator::row_iterator rd =
id.rowIterator();
555 typename TmpImageIterator::row_iterator rt = yt.rowIterator();
560 lt, line.accessor(), (double)w/wnew/scale);
562 resizeLineLinearInterpolation(lt, lt + w, line.accessor(),
567 resizeLineLinearInterpolation(rt, rt + w, tmp.accessor(),
573 template <
class SrcIterator,
class SrcAccessor,
574 class DestIterator,
class DestAccessor>
577 triple<DestIterator, DestIterator, DestAccessor> dest)
580 dest.first, dest.second, dest.third);
583 template <
class T1,
class S1,
587 MultiArrayView<2, T2, S2> dest)
590 destImageRange(dest));
725 doxygen_overloaded_function(template <...>
void resizeImageSplineInterpolation)
727 template <
class SrcIterator,
class SrcAccessor,
728 class DestIterator,
class DestAccessor,
732 SrcIterator src_iter, SrcIterator src_iter_end, SrcAccessor src_acc,
733 DestIterator dest_iter, DestIterator dest_iter_end, DestAccessor dest_acc,
734 SPLINE
const & spline)
737 int width_old = src_iter_end.x - src_iter.x;
738 int height_old = src_iter_end.y - src_iter.y;
740 int width_new = dest_iter_end.x - dest_iter.x;
741 int height_new = dest_iter_end.y - dest_iter.y;
743 vigra_precondition((width_old > 1) && (height_old > 1),
744 "resizeImageSplineInterpolation(): "
745 "Source image too small.\n");
747 vigra_precondition((width_new > 1) && (height_new > 1),
748 "resizeImageSplineInterpolation(): "
749 "Destination image too small.\n");
751 Rational<int> xratio(width_new - 1, width_old - 1);
752 Rational<int> yratio(height_new - 1, height_old - 1);
753 Rational<int> offset(0);
754 resampling_detail::MapTargetToSourceCoordinate xmapCoordinate(xratio, offset);
755 resampling_detail::MapTargetToSourceCoordinate ymapCoordinate(yratio, offset);
756 int xperiod =
lcm(xratio.numerator(), xratio.denominator());
757 int yperiod =
lcm(yratio.numerator(), yratio.denominator());
759 double const scale = 2.0;
761 typedef typename SrcAccessor::value_type SRCVT;
762 typedef typename NumericTraits<SRCVT>::RealPromote TMPTYPE;
763 typedef BasicImage<TMPTYPE> TmpImage;
764 typedef typename TmpImage::traverser TmpImageIterator;
766 BasicImage<TMPTYPE> tmp(width_old, height_new);
768 BasicImage<TMPTYPE> line((height_old > width_old) ? height_old : width_old, 1);
769 typename BasicImage<TMPTYPE>::Accessor tmp_acc = tmp.accessor();
770 ArrayVector<double>
const & prefilterCoeffs = spline.prefilterCoefficients();
774 ArrayVector<Kernel1D<double> > kernels(yperiod);
775 createResamplingKernels(spline, ymapCoordinate, kernels);
777 typename BasicImage<TMPTYPE>::Iterator y_tmp = tmp.upperLeft();
778 typename TmpImageIterator::row_iterator line_tmp = line.upperLeft().rowIterator();
780 for(x=0; x<width_old; ++x, ++src_iter.x, ++y_tmp.x)
783 typename SrcIterator::column_iterator c_src = src_iter.columnIterator();
784 typename TmpImageIterator::column_iterator c_tmp = y_tmp.columnIterator();
786 if(prefilterCoeffs.size() == 0)
788 if(height_new >= height_old)
791 c_tmp, c_tmp + height_new, tmp_acc,
792 kernels, ymapCoordinate);
797 line_tmp, line.accessor(), (double)height_old/height_new/scale);
799 c_tmp, c_tmp + height_new, tmp_acc,
800 kernels, ymapCoordinate);
806 line_tmp, line.accessor(),
807 prefilterCoeffs[0], BORDER_TREATMENT_REFLECT);
808 for(
unsigned int b = 1; b < prefilterCoeffs.size(); ++b)
811 line_tmp, line.accessor(),
812 prefilterCoeffs[b], BORDER_TREATMENT_REFLECT);
814 if(height_new < height_old)
817 line_tmp, line.accessor(), (double)height_old/height_new/scale);
820 c_tmp, c_tmp + height_new, tmp_acc,
821 kernels, ymapCoordinate);
825 y_tmp = tmp.upperLeft();
827 kernels.resize(xperiod);
828 createResamplingKernels(spline, xmapCoordinate, kernels);
830 for(y=0; y < height_new; ++y, ++y_tmp.y, ++dest_iter.y)
832 typename DestIterator::row_iterator r_dest = dest_iter.rowIterator();
833 typename TmpImageIterator::row_iterator r_tmp = y_tmp.rowIterator();
835 if(prefilterCoeffs.size() == 0)
837 if(width_new >= width_old)
840 r_dest, r_dest + width_new, dest_acc,
841 kernels, xmapCoordinate);
846 line_tmp, line.accessor(), (double)width_old/width_new/scale);
848 r_dest, r_dest + width_new, dest_acc,
849 kernels, xmapCoordinate);
855 line_tmp, line.accessor(),
856 prefilterCoeffs[0], BORDER_TREATMENT_REFLECT);
857 for(
unsigned int b = 1; b < prefilterCoeffs.size(); ++b)
860 line_tmp, line.accessor(),
861 prefilterCoeffs[b], BORDER_TREATMENT_REFLECT);
863 if(width_new < width_old)
866 line_tmp, line.accessor(), (double)width_old/width_new/scale);
869 r_dest, r_dest + width_new, dest_acc,
870 kernels, xmapCoordinate);
875 template <
class SrcIterator,
class SrcAccessor,
876 class DestIterator,
class DestAccessor>
879 DestIterator
id, DestIterator idend, DestAccessor da)
884 template <
class SrcIterator,
class SrcAccessor,
885 class DestIterator,
class DestAccessor,
889 triple<DestIterator, DestIterator, DestAccessor> dest,
890 SPLINE
const & spline)
893 dest.first, dest.second, dest.third, spline);
896 template <
class SrcIterator,
class SrcAccessor,
897 class DestIterator,
class DestAccessor>
900 triple<DestIterator, DestIterator, DestAccessor> dest)
903 dest.first, dest.second, dest.third);
906 template <
class T1,
class S1,
911 MultiArrayView<2, T2, S2> dest,
912 SPLINE
const & spline)
915 destImageRange(dest), spline);
918 template <
class T1,
class S1,
922 MultiArrayView<2, T2, S2> dest)
925 destImageRange(dest));
987 doxygen_overloaded_function(template <...>
void resizeImageCatmullRomInterpolation)
989 template <
class SrcIterator,
class SrcAccessor,
990 class DestIterator,
class DestAccessor>
993 DestIterator dest_iter, DestIterator dest_iter_end, DestAccessor dest_acc)
996 CatmullRomSpline<double>());
999 template <
class SrcIterator,
class SrcAccessor,
1000 class DestIterator,
class DestAccessor>
1003 triple<DestIterator, DestIterator, DestAccessor> dest)
1006 dest.first, dest.second, dest.third);
1009 template <
class T1,
class S1,
1013 MultiArrayView<2, T2, S2> dest)
1016 destImageRange(dest));
1079 doxygen_overloaded_function(template <...>
void resizeImageCoscotInterpolation)
1081 template <
class SrcIterator,
class SrcAccessor,
1082 class DestIterator,
class DestAccessor>
1085 DestIterator dest_iter, DestIterator dest_iter_end, DestAccessor dest_acc)
1088 CoscotFunction<double>());
1091 template <
class SrcIterator,
class SrcAccessor,
1092 class DestIterator,
class DestAccessor>
1095 triple<DestIterator, DestIterator, DestAccessor> dest)
1098 dest.first, dest.second, dest.third);
1101 template <
class T1,
class S1,
1105 MultiArrayView<2, T2, S2> dest)
1108 destImageRange(dest));
1115 #endif // VIGRA_RESIZEIMAGE_HXX
Definition: resizeimage.hxx:82
result_type operator()(argument_type x) const
Definition: resizeimage.hxx:103
linalg::TemporaryMatrix< T > sin(MultiArrayView< 2, T, C > const &v)
void recursiveSmoothLine(...)
Convolves the image with a 1-dimensional exponential filter.
double radius() const
Definition: resizeimage.hxx:121
unsigned int derivativeOrder() const
Definition: resizeimage.hxx:126
Definition: accessor.hxx:43
T argument_type
Definition: resizeimage.hxx:91
value_type operator[](value_type x) const
Definition: resizeimage.hxx:115
void recursiveFilterLine(...)
Performs a 1-dimensional recursive convolution of the source signal.
IntType lcm(IntType n, IntType m)
Definition: rational.hxx:122
T result_type
Definition: resizeimage.hxx:94
T value_type
Definition: resizeimage.hxx:88
FFTWComplex< R >::NormType abs(const FFTWComplex< R > &a)
absolute value (= magnitude)
Definition: fftw3.hxx:1002
ArrayVector< double > const & prefilterCoefficients() const
Definition: resizeimage.hxx:132
linalg::TemporaryMatrix< T > tan(MultiArrayView< 2, T, C > const &v)
linalg::TemporaryMatrix< T > cos(MultiArrayView< 2, T, C > const &v)
void resamplingConvolveLine(...)
Performs a 1-dimensional resampling convolution of the source signal using the given set of kernels...