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