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