[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

vigra/interpolating_accessor.hxx

00001 /************************************************************************/
00002 /*                                                                      */
00003 /*               Copyright 1998-2002 by Ullrich Koethe                  */
00004 /*       Cognitive Systems Group, University of Hamburg, Germany        */
00005 /*                                                                      */
00006 /*    This file is part of the VIGRA computer vision library.           */
00007 /*    The VIGRA Website is                                              */
00008 /*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
00009 /*    Please direct questions, bug reports, and contributions to        */
00010 /*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
00011 /*        vigra@informatik.uni-hamburg.de                               */
00012 /*                                                                      */
00013 /*    Permission is hereby granted, free of charge, to any person       */
00014 /*    obtaining a copy of this software and associated documentation    */
00015 /*    files (the "Software"), to deal in the Software without           */
00016 /*    restriction, including without limitation the rights to use,      */
00017 /*    copy, modify, merge, publish, distribute, sublicense, and/or      */
00018 /*    sell copies of the Software, and to permit persons to whom the    */
00019 /*    Software is furnished to do so, subject to the following          */
00020 /*    conditions:                                                       */
00021 /*                                                                      */
00022 /*    The above copyright notice and this permission notice shall be    */
00023 /*    included in all copies or substantial portions of the             */
00024 /*    Software.                                                         */
00025 /*                                                                      */
00026 /*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
00027 /*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
00028 /*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
00029 /*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
00030 /*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
00031 /*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
00032 /*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
00033 /*    OTHER DEALINGS IN THE SOFTWARE.                                   */                
00034 /*                                                                      */
00035 /************************************************************************/
00036 
00037 #ifndef VIGRA_INTERPOLATING_ACCESSOR_HXX
00038 #define VIGRA_INTERPOLATING_ACCESSOR_HXX
00039 
00040 
00041 #include "accessor.hxx"
00042 #include "diff2d.hxx"
00043 
00044 namespace vigra {
00045 
00046 /** \addtogroup DataAccessors
00047 */
00048 //@{
00049 
00050 /********************************************************/
00051 /*                                                      */
00052 /*            BilinearInterpolatingAccessor             */
00053 /*                                                      */
00054 /********************************************************/
00055 
00056 /** \brief Bilinear interpolation at non-integer positions.
00057 
00058     This accessor allows an image be accessed at arbitrary non-integer
00059     coordinates and performs an bi-linear interpolation to
00060     obtain a pixel value.
00061     It uses the given ACCESSOR (which is usually the
00062     accessor originally associated with the iterator)
00063     to access data.
00064 
00065     <b>\#include</b> <<a href="accessor_8hxx-source.html">vigra/accessor.hxx</a>>
00066     Namespace: vigra
00067 
00068     <b> Required Interface:</b>
00069 
00070     \code
00071     ITERATOR iter;
00072     ACCESSOR a;
00073     VALUETYPE destvalue;
00074     float s;
00075     int x, y;
00076 
00077     destvalue = s * a(iter, x, y) + s * a(iter, x, y);
00078 
00079     \endcode
00080 */
00081 template <class ACCESSOR, class VALUETYPE>
00082 class BilinearInterpolatingAccessor
00083 {
00084   public:
00085     /** the iterators' pixel type
00086     */
00087     typedef VALUETYPE value_type;
00088 
00089     /** init from given accessor
00090     */
00091     BilinearInterpolatingAccessor(ACCESSOR a)
00092     : a_(a)
00093     {}
00094 
00095     /** Interpolate the data item at a non-integer position.
00096         Ensure that no outside pixels are accessed if
00097         (x, y) is near the image border (as long as
00098         0 <= x <= width-1, 0 <= y <= height-1).
00099     */
00100     template <class ITERATOR>
00101     value_type operator()(ITERATOR const & i, float x, float y) const
00102     {
00103         int ix = int(x);
00104         int iy = int(y);
00105         float dx = x - ix;
00106         float dy = y - iy;
00107 
00108         value_type ret;
00109 
00110         // avoid dereferencing the iterator outside its range
00111         if(dx == 0.0)
00112         {
00113             if(dy == 0.0)
00114             {
00115                 ret = a_(i, Diff2D(ix, iy));
00116             }
00117             else
00118             {
00119                 ret = detail::RequiresExplicitCast<value_type>::cast(
00120                   (1.0 - dy) * a_(i, Diff2D(ix, iy)) +
00121                   dy * a_(i, Diff2D(ix, iy + 1)));
00122             }
00123         }
00124         else
00125         {
00126             if(dy == 0.0)
00127             {
00128                 ret = detail::RequiresExplicitCast<value_type>::cast(
00129                   (1.0 - dx) * a_(i, Diff2D(ix, iy)) +
00130                   dx * a_(i, Diff2D(ix + 1, iy)));
00131             }
00132             else
00133             {
00134                 ret = detail::RequiresExplicitCast<value_type>::cast(
00135                   (1.0 - dx) * (1.0 - dy) * a_(i, Diff2D(ix, iy)) +
00136                   dx * (1.0 - dy) * a_(i, Diff2D(ix + 1, iy)) +
00137                   (1.0 - dx) * dy * a_(i, Diff2D(ix, iy + 1)) +
00138                   dx * dy * a_(i, Diff2D(ix + 1, iy + 1)));
00139             }
00140         }
00141 
00142         return ret;
00143     }
00144 
00145     /** Interpolate the data item at a non-integer position.
00146         This function works as long as 0 <= x < width-1,
00147         0 <= y < height-1. It is slightly faster than <TT>operator()</TT>.
00148     */
00149     template <class ITERATOR>
00150     value_type unchecked(ITERATOR const & i, float x, float y) const
00151     {
00152         int ix = int(x);
00153         int iy = int(y);
00154         float dx = x - ix;
00155         float dy = y - iy;
00156         return detail::RequiresExplicitCast<value_type>::cast(
00157                (1.0 - dx) * (1.0 - dy) * a_(i, Diff2D(ix, iy)) +
00158                dx * (1.0 - dy) * a_(i, Diff2D(ix + 1, iy)) +
00159                (1.0 - dx) * dy * a_(i, Diff2D(ix, iy + 1)) +
00160                dx * dy * a_(i, Diff2D(ix + 1, iy + 1)));
00161     }
00162 
00163   private:
00164     ACCESSOR a_;
00165 };
00166 
00167 //@}
00168 
00169 } // namespace vigra
00170 
00171 #endif /* VIGRA_INTERPOLATING_ACCESSOR_HXX */

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
VIGRA 1.6.0 (5 Nov 2009)