/************************************************************************/ /* */ /* Copyright 1998-2004 by Ullrich Koethe */ /* Cognitive Systems Group, University of Hamburg, Germany */ /* */ /* This file is part of the VIGRA computer vision library. */ /* The VIGRA Website is */ /* http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/ */ /* Please direct questions, bug reports, and contributions to */ /* koethe@informatik.uni-hamburg.de or */ /* vigra@kogs1.informatik.uni-hamburg.de */ /* */ /* Permission is hereby granted, free of charge, to any person */ /* obtaining a copy of this software and associated documentation */ /* files (the "Software"), to deal in the Software without */ /* restriction, including without limitation the rights to use, */ /* copy, modify, merge, publish, distribute, sublicense, and/or */ /* sell copies of the Software, and to permit persons to whom the */ /* Software is furnished to do so, subject to the following */ /* conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the */ /* Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */ /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */ /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */ /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */ /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */ /* OTHER DEALINGS IN THE SOFTWARE. */ /* */ /************************************************************************/ #ifndef VIGRA_RESAMPLING_CONVOLUTION_HXX #define VIGRA_RESAMPLING_CONVOLUTION_HXX #include #include "vigra/stdimage.hxx" #include "vigra/array_vector.hxx" #include "vigra/rational.hxx" #include "vigra/functortraits.hxx" namespace vigra { namespace resampling_detail { struct MapTargetToSourceCoordinate { MapTargetToSourceCoordinate(Rational const & samplingRatio, Rational const & offset) : a(samplingRatio.denominator()*offset.denominator()), b(samplingRatio.numerator()*offset.numerator()), c(samplingRatio.numerator()*offset.denominator()) {} // the following funcions are more efficient realizations of: // rational_cast(i / samplingRatio + offset); // we need efficiency because this may be called in the inner loop int operator()(int i) const { return (i * a + b) / c; } double toDouble(int i) const { return double(i * a + b) / c; } Rational toRational(int i) const { return Rational(i * a + b, c); } int a, b, c; }; } // namespace resampling_detail template <> class FunctorTraits : public FunctorTraitsBase { public: typedef VigraTrueType isUnaryFunctor; }; template void resamplingConvolveLine(SrcIter s, SrcIter send, SrcAcc src, DestIter d, DestIter dend, DestAcc dest, KernelArray const & kernels, Functor mapTargetToSourceCoordinate) { typedef typename NumericTraits::RealPromote TmpType; typedef typename KernelArray::value_type Kernel; typedef typename Kernel::const_iterator KernelIter; int wo = send - s; int wn = dend - d; int wo2 = 2*wo - 2; int i; typename KernelArray::const_iterator kernel = kernels.begin(); for(i=0; i::zero(); int lbound = is - kernel->right(), hbound = is - kernel->left(); KernelIter k = kernel->center() + kernel->right(); if(lbound < 0 || hbound >= wo) { vigra_precondition(-lbound < wo && wo2 - hbound >= 0, "resamplingConvolveLine(): kernel or offset larger than image."); for(int m=lbound; m <= hbound; ++m, --k) { int mm = (m < 0) ? -m : (m >= wo) ? wo2 - m : m; sum += *k * src(s, mm); } } else { SrcIter ss = s + lbound; SrcIter ssend = s + hbound; for(; ss <= ssend; ++ss, --k) { sum += *k * src(ss); } } dest.set(sum, d); } } template void createResamplingKernels(Kernel const & kernel, MapCoordinate const & mapCoordinate, KernelArray & kernels) { for(unsigned int idest = 0; idest < kernels.size(); ++idest) { int isrc = mapCoordinate(idest); double idsrc = mapCoordinate.toDouble(idest); double offset = idsrc - isrc; double radius = kernel.radius(); int left = int(ceil(-radius - offset)); int right = int(floor(radius - offset)); kernels[idest].initExplicitly(left, right); double x = left + offset; for(int i = left; i <= right; ++i, ++x) kernels[idest][i] = kernel(x); kernels[idest].normalize(1.0, kernel.derivativeOrder(), offset); } } /** \addtogroup ResamplingConvolutionFilters Resampling Convolution Filters These functions implement the convolution operation when the source and target images have different sizes. This is realized by accessing a continous kernel at the appropriate non-integer positions. The technique is, for example, described in D. Schumacher: General Filtered Image Rescaling, in: Graphics Gems III, Academic Press, 1992. */ //@{ /********************************************************/ /* */ /* resamplingConvolveX */ /* */ /********************************************************/ /** \brief Apply a resampling filter in the x-direction. This function implements a convolution operation in x-direction (i.e. applies a 1D filter to every row) where the width of the source and destination images differ. This is typically used to avoid aliasing if the image is scaled down, or to interpolate smoothly if the image is scaled up. The target coordinates are transformed into source coordinates by \code xsource = (xtarget - offset) / samplingRatio \endcode The samplingRatio and offset must be given as \ref vigra::Rational in order to avoid rounding errors in this transformation. It is required that for all pixels of the target image, xsource remains within the range of the source image (i.e. 0 <= xsource <= sourceWidth-1. Since xsource is in general not an integer, the kernel must be a functor that can be accessed at arbitrary (double) coordinates. It must also provide a member function radius() which specifies the support (non-zero interval) of the kernel. VIGRA already provides a number of suitable functors, e.g. \ref vigra::Gaussian, \ref vigra::BSpline \ref vigra::CatmullRomSpline, and \ref vigra::CoscotFunction. The function \ref resizeImageSplineInterpolation() is implemented by means resamplingConvolveX() and resamplingConvolveY(). Declarations: pass arguments explicitly: \code namespace vigra { template void resamplingConvolveX(SrcIter sul, SrcIter slr, SrcAcc src, DestIter dul, DestIter dlr, DestAcc dest, Kernel const & kernel, Rational const & samplingRatio, Rational const & offset); } \endcode use argument objects in conjunction with \ref ArgumentObjectFactories: \code namespace vigra { template void resamplingConvolveX(triple src, triple dest, Kernel const & kernel, Rational const & samplingRatio, Rational const & offset); } \endcode Usage: \#include "vigra/resampling_convolution.hxx" \code Rational ratio(2), offset(0); FImage src(w,h), dest(rational_cast(ratio*w), h); float sigma = 2.0; Gaussian smooth(sigma); ... // simpultaneously enlarge and smooth source image resamplingConvolveX(srcImageRange(src), destImageRange(dest), smooth, ratio, offset); \endcode Required Interface: \code Kernel kernel; int kernelRadius = kernel.radius(); double x = ...; // must be <= radius() double value = kernel(x); \endcode */ template void resamplingConvolveX(SrcIter sul, SrcIter slr, SrcAcc src, DestIter dul, DestIter dlr, DestAcc dest, Kernel const & kernel, Rational const & samplingRatio, Rational const & offset) { int wold = slr.x - sul.x; int wnew = dlr.x - dul.x; vigra_precondition(!samplingRatio.is_inf() && samplingRatio > 0, "resamplingConvolveX(): sampling ratio must be > 0 and < infinity"); vigra_precondition(!offset.is_inf(), "resamplingConvolveX(): offset must be < infinity"); int period = lcm(samplingRatio.numerator(), samplingRatio.denominator()); resampling_detail::MapTargetToSourceCoordinate mapCoordinate(samplingRatio, offset); ArrayVector > kernels(period); createResamplingKernels(kernel, mapCoordinate, kernels); for(; sul.y < slr.y; ++sul.y, ++dul.y) { typename SrcIter::row_iterator sr = sul.rowIterator(); typename DestIter::row_iterator dr = dul.rowIterator(); resamplingConvolveLine(sr, sr+wold, src, dr, dr+wnew, dest, kernels, mapCoordinate); } } template inline void resamplingConvolveX(triple src, triple dest, Kernel const & kernel, Rational const & samplingRatio, Rational const & offset) { resamplingConvolveX(src.first, src.second, src.third, dest.first, dest.second, dest.third, kernel, samplingRatio, offset); } /********************************************************/ /* */ /* resamplingConvolveY */ /* */ /********************************************************/ /** \brief Apply a resampling filter in the y-direction. This function implements a convolution operation in y-direction (i.e. applies a 1D filter to every column) where the height of the source and destination images differ. This is typically used to avoid aliasing if the image is scaled down, or to interpolate smoothly if the image is scaled up. The target coordinates are transformed into source coordinates by \code ysource = (ytarget - offset) / samplingRatio \endcode The samplingRatio and offset must be given as \ref vigra::Rational in order to avoid rounding errors in this transformation. It is required that for all pixels of the target image, ysource remains within the range of the source image (i.e. 0 <= ysource <= sourceHeight-1. Since ysource is in general not an integer, the kernel must be a functor that can be accessed at arbitrary (double) coordinates. It must also provide a member function radius() which specifies the support (non-zero interval) of the kernel. VIGRA already provides a number of suitable functors, e.g. \ref vigra::Gaussian, \ref vigra::BSpline \ref vigra::CatmullRomSpline, and \ref vigra::CoscotFunction. The function \ref resizeImageSplineInterpolation() is implemented by means resamplingConvolveX() and resamplingConvolveY(). Declarations: pass arguments explicitly: \code namespace vigra { template void resamplingConvolveY(SrcIter sul, SrcIter slr, SrcAcc src, DestIter dul, DestIter dlr, DestAcc dest, Kernel const & kernel, Rational const & samplingRatio, Rational const & offset); } \endcode use argument objects in conjunction with \ref ArgumentObjectFactories: \code namespace vigra { template void resamplingConvolveY(triple src, triple dest, Kernel const & kernel, Rational const & samplingRatio, Rational const & offset); } \endcode Usage: \#include "vigra/resampling_convolution.hxx" \code Rational ratio(2), offset(0); FImage src(w,h), dest(w, rational_cast(ratio*h)); float sigma = 2.0; Gaussian smooth(sigma); ... // simpultaneously enlarge and smooth source image resamplingConvolveY(srcImageRange(src), destImageRange(dest), smooth, ratio, offset); \endcode Required Interface: \code Kernel kernel; int kernelRadius = kernel.radius(); double y = ...; // must be <= radius() double value = kernel(y); \endcode */ template void resamplingConvolveY(SrcIter sul, SrcIter slr, SrcAcc src, DestIter dul, DestIter dlr, DestAcc dest, Kernel const & kernel, Rational const & samplingRatio, Rational const & offset) { int hold = slr.y - sul.y; int hnew = dlr.y - dul.y; vigra_precondition(!samplingRatio.is_inf() && samplingRatio > 0, "resamplingConvolveY(): sampling ratio must be > 0 and < infinity"); vigra_precondition(!offset.is_inf(), "resamplingConvolveY(): offset must be < infinity"); int period = lcm(samplingRatio.numerator(), samplingRatio.denominator()); resampling_detail::MapTargetToSourceCoordinate mapCoordinate(samplingRatio, offset); ArrayVector > kernels(period); createResamplingKernels(kernel, mapCoordinate, kernels); for(; sul.x < slr.x; ++sul.x, ++dul.x) { typename SrcIter::column_iterator sc = sul.columnIterator(); typename DestIter::column_iterator dc = dul.columnIterator(); resamplingConvolveLine(sc, sc+hold, src, dc, dc+hnew, dest, kernels, mapCoordinate); } } template inline void resamplingConvolveY(triple src, triple dest, Kernel const & kernel, Rational const & samplingRatio, Rational const & offset) { resamplingConvolveY(src.first, src.second, src.third, dest.first, dest.second, dest.third, kernel, samplingRatio, offset); } /********************************************************/ /* */ /* resamplingConvolveImage */ /* */ /********************************************************/ /** \brief Apply two separable resampling filters successively, the first in x-direction, the second in y-direction. This function is a shorthand for the concatenation of a call to \link ResamplingConvolutionFilters#resamplingConvolveX resamplingConvolveX\endlink() and \link ResamplingConvolutionFilters#resamplingConvolveY resamplingConvolveY\endlink() with the given kernels. See there for detailed documentation. Declarations: pass arguments explicitly: \code namespace vigra { template void resamplingConvolveImage(SrcIterator sul,SrcIterator slr, SrcAccessor src, DestIterator dul, DestIterator dlr, DestAccessor dest, KernelX const & kx, Rational const & samplingRatioX, Rational const & offsetX, KernelY const & ky, Rational const & samplingRatioY, Rational const & offsetY); } \endcode use argument objects in conjunction with \ref ArgumentObjectFactories: \code namespace vigra { template void resamplingConvolveImage(triple src, triple dest, KernelX const & kx, Rational const & samplingRatioX, Rational const & offsetX, KernelY const & ky, Rational const & samplingRatioY, Rational const & offsetY); } \endcode Usage: \#include "vigra/resampling_convolution.hxx" \code Rational xratio(2), yratio(3), offset(0); FImage src(w,h), dest(rational_cast(xratio*w), rational_cast(yratio*h)); float sigma = 2.0; Gaussian smooth(sigma); ... // simpultaneously enlarge and smooth source image resamplingConvolveImage(srcImageRange(src), destImageRange(dest), smooth, xratio, offset, smooth, yratio, offset); \endcode */ template void resamplingConvolveImage(SrcIterator sul,SrcIterator slr, SrcAccessor src, DestIterator dul, DestIterator dlr, DestAccessor dest, KernelX const & kx, Rational const & samplingRatioX, Rational const & offsetX, KernelY const & ky, Rational const & samplingRatioY, Rational const & offsetY) { typedef typename NumericTraits::RealPromote TmpType; BasicImage tmp(dlr.x - dul.x, slr.y - sul.y); resamplingConvolveX(srcIterRange(sul, slr, src), destImageRange(tmp), kx, samplingRatioX, offsetX); resamplingConvolveY(srcImageRange(tmp), destIterRange(dul, dlr, dest), ky, samplingRatioY, offsetY); } template inline void resamplingConvolveImage(triple src, triple dest, KernelX const & kx, Rational const & samplingRatioX, Rational const & offsetX, KernelY const & ky, Rational const & samplingRatioY, Rational const & offsetY) { resamplingConvolveImage(src.first, src.second, src.third, dest.first, dest.second, dest.third, kx, samplingRatioX, offsetX, ky, samplingRatioY, offsetY); } } // namespace vigra #endif /* VIGRA_RESAMPLING_CONVOLUTION_HXX */