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

vigra/initimage.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
    00037 #ifndef VIGRA_INITIMAGE_HXX
    00038 #define VIGRA_INITIMAGE_HXX
    00039
    00040 #include "utilities.hxx"
    00041 #include "iteratortraits.hxx"
    00042 #include "functortraits.hxx"
    00043
    00044 namespace vigra {
    00045
    00046 /** \addtogroup InitAlgo Algorithms to Initialize Images
    00047
    00048     Init images or image borders
    00049 */
    00050 //@{
    00051
    00052 /********************************************************/
    00053 /*                                                      */
    00054 /*                       initLine                       */
    00055 /*                                                      */
    00056 /********************************************************/
    00057
    00058 template <class DestIterator, class DestAccessor, class VALUETYPE>
    00059 inline void
    00060 initLineImpl(DestIterator d, DestIterator dend, DestAccessor dest,
    00061              VALUETYPE const & v, VigraFalseType)
    00062 {
    00063     for(; d != dend; ++d)
    00064         dest.set(v, d);
    00065 }
    00066
    00067 template <class DestIterator, class DestAccessor, class FUNCTOR>
    00068 inline void
    00069 initLineImpl(DestIterator d, DestIterator dend, DestAccessor dest,
    00070              FUNCTOR const & f, VigraTrueType)
    00071 {
    00072     for(; d != dend; ++d)
    00073         dest.set(f(), d);
    00074 }
    00075
    00076 template <class DestIterator, class DestAccessor, class VALUETYPE>
    00077 inline void
    00078 initLine(DestIterator d, DestIterator dend, DestAccessor dest,
    00079          VALUETYPE const & v)
    00080 {
    00081     initLineImpl(d, dend, dest, v, typename FunctorTraits<VALUETYPE>::isInitializer());
    00082 }
    00083
    00084 template <class DestIterator, class DestAccessor, class FUNCTOR>
    00085 inline void
    00086 initLineFunctor(DestIterator d, DestIterator dend, DestAccessor dest,
    00087          FUNCTOR & f)
    00088 {
    00089     for(; d != dend; ++d)
    00090         dest.set(f(), d);
    00091 }
    00092
    00093 template <class DestIterator, class DestAccessor,
    00094           class MaskIterator, class MaskAccessor,
    00095           class VALUETYPE>
    00096 inline void
    00097 initLineIfImpl(DestIterator d, DestIterator dend, DestAccessor dest,
    00098                MaskIterator m, MaskAccessor mask,
    00099                VALUETYPE const & v, VigraFalseType)
    00100 {
    00101     for(; d != dend; ++d, ++m)
    00102         if(mask(m))
    00103             dest.set(v, d);
    00104 }
    00105
    00106 template <class DestIterator, class DestAccessor,
    00107           class MaskIterator, class MaskAccessor,
    00108           class FUNCTOR>
    00109 inline void
    00110 initLineIfImpl(DestIterator d, DestIterator dend, DestAccessor dest,
    00111                MaskIterator m, MaskAccessor mask,
    00112                FUNCTOR const & f, VigraTrueType)
    00113 {
    00114     for(; d != dend; ++d, ++m)
    00115         if(mask(m))
    00116             dest.set(f(), d);
    00117 }
    00118
    00119 template <class DestIterator, class DestAccessor,
    00120           class MaskIterator, class MaskAccessor,
    00121           class VALUETYPE>
    00122 inline void
    00123 initLineIf(DestIterator d, DestIterator dend, DestAccessor dest,
    00124            MaskIterator m, MaskAccessor mask,
    00125            VALUETYPE const & v)
    00126 {
    00127     initLineIfImpl(d, dend, dest, m, mask, v, typename FunctorTraits<VALUETYPE>::isInitializer());
    00128 }
    00129
    00130 template <class DestIterator, class DestAccessor,
    00131           class MaskIterator, class MaskAccessor,
    00132           class FUNCTOR>
    00133 inline void
    00134 initLineFunctorIf(DestIterator d, DestIterator dend, DestAccessor dest,
    00135                   MaskIterator m, MaskAccessor mask,
    00136                   FUNCTOR & f)
    00137 {
    00138     for(; d != dend; ++d, ++m)
    00139         if(mask(m))
    00140             dest.set(f(), d);
    00141 }
    00142
    00143 /********************************************************/
    00144 /*                                                      */
    00145 /*                        initImage                     */
    00146 /*                                                      */
    00147 /********************************************************/
    00148
    00149 /** \brief Write a value to every pixel in an image or rectangular ROI.
    00150
    00151     This function can be used to init the image.
    00152     It uses an accessor to access the pixel data.
    00153
    00154     The initial value can either be a constant of appropriate type (compatible with
    00155     the destination's value_type), or a functor with compatible result_type. These two
    00156     cases are automatically distinguished when <tt>FunctorTraits<FUNCTOR>::isInitializer</tt>
    00157     yields <tt>VigraTrueType</tt>. Since the functor is passed by <tt>const</tt> reference, its
    00158     <tt>operator()</tt> must be const, and its internal state may need to be <tt>mutable</tt>.
    00159
    00160     <b> Declarations:</b>
    00161
    00162     pass arguments explicitly:
    00163     \code
    00164     namespace vigra {
    00165         template <class ImageIterator, class Accessor, class VALUETYPE>
    00166         void initImage(ImageIterator upperleft, ImageIterator lowerright,
    00167                        Accessor a, VALUETYPE const & v);
    00168
    00169         template <class ImageIterator, class Accessor, class FUNCTOR>
    00170         void initImage(ImageIterator upperleft, ImageIterator lowerright,
    00171                        Accessor a, FUNCTOR const & v);
    00172     }
    00173     \endcode
    00174
    00175     use argument objects in conjunction with \ref ArgumentObjectFactories :
    00176     \code
    00177     namespace vigra {
    00178         template <class ImageIterator, class Accessor, class VALUETYPE>
    00179         void initImage(triple<ImageIterator, ImageIterator, Accessor> img, VALUETYPE const & v);
    00180
    00181         template <class ImageIterator, class Accessor, class FUNCTOR>
    00182         void initImage(triple<ImageIterator, ImageIterator, Accessor> img, FUNCTOR const & v);
    00183     }
    00184     \endcode
    00185
    00186     <b> Usage:</b>
    00187
    00188         <b>\#include</b> <<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>><br>
    00189         Namespace: vigra
    00190
    00191     Initialize with a constant:
    00192
    00193     \code
    00194     vigra::BImage img(100, 100);
    00195
    00196     // init the image with the value 128
    00197     vigra::initImage(destImageRange(img), 128);
    00198     \endcode
    00199
    00200     Initialize with a functor:
    00201
    00202     \code
    00203     struct Counter {
    00204         Counter() : count(0) {}
    00205
    00206         int operator()() const { return count++; }
    00207
    00208         mutable int count;
    00209     };
    00210
    00211     vigra::IImage img(100, 100);
    00212
    00213     // write the current count in every pixel
    00214     vigra::initImage(destImageRange(img), Counter());
    00215     \endcode
    00216
    00217
    00218     <b> Required Interface:</b>
    00219
    00220     \code
    00221     ImageIterator upperleft, lowerright;
    00222     ImageIterator::row_iterator ix = upperleft.rowIterator();
    00223
    00224     Accessor accessor;
    00225     VALUETYPE v;
    00226
    00227     accessor.set(v, ix);
    00228     \endcode
    00229
    00230 */
    00231 doxygen_overloaded_function(template <...> void initImage)
    00232
    00233 template <class ImageIterator, class Accessor, class VALUETYPE>
    00234 void
    00235 initImage(ImageIterator upperleft, ImageIterator lowerright,
    00236           Accessor a,  VALUETYPE const & v)
    00237 {
    00238     int w = lowerright.x - upperleft.x;
    00239
    00240     for(; upperleft.y < lowerright.y; ++upperleft.y)
    00241     {
    00242         initLineImpl(upperleft.rowIterator(), upperleft.rowIterator() + w, a,
    00243                      v, typename FunctorTraits<VALUETYPE>::isInitializer());
    00244     }
    00245 }
    00246
    00247 template <class ImageIterator, class Accessor, class VALUETYPE>
    00248 inline
    00249 void
    00250 initImage(triple<ImageIterator, ImageIterator, Accessor> img, VALUETYPE const & v)
    00251 {
    00252     initImage(img.first, img.second, img.third, v);
    00253 }
    00254
    00255 /********************************************************/
    00256 /*                                                      */
    00257 /*                 initImageWithFunctor                 */
    00258 /*                                                      */
    00259 /********************************************************/
    00260
    00261 /** \brief Write the result of a functor call to every pixel in an image or rectangular ROI.
    00262
    00263     This function can be used to init the image by calling the given
    00264     functor for each pixel. It uses an accessor to access the pixel data. The functor is
    00265     passed by reference, so that its internal state can be updated in each call.
    00266
    00267     <b> Declarations:</b>
    00268
    00269     pass arguments explicitly:
    00270     \code
    00271     namespace vigra {
    00272         template <class ImageIterator, class Accessor, class FUNCTOR>
    00273         void
    00274         initImageWithFunctor(ImageIterator upperleft, ImageIterator lowerright,
    00275                   Accessor a,  FUNCTOR & f);
    00276     }
    00277     \endcode
    00278
    00279     use argument objects in conjunction with \ref ArgumentObjectFactories :
    00280     \code
    00281     namespace vigra {
    00282         template <class ImageIterator, class Accessor, class FUNCTOR>
    00283         void
    00284         initImageWithFunctor(triple<ImageIterator, ImageIterator, Accessor> img, FUNCTOR & f);
    00285     }
    00286     \endcode
    00287
    00288     <b> Usage:</b>
    00289
    00290         <b>\#include</b> <<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>><br>
    00291         Namespace: vigra
    00292
    00293     \code
    00294     struct Counter {
    00295         Counter() : count(0) {}
    00296
    00297         int operator()() const { return count++; }
    00298
    00299         mutable int count;
    00300     };
    00301
    00302     vigra::IImage img(100, 100);
    00303
    00304     // write the current count in every pixel
    00305     Counter counter;
    00306     vigra::initImageWithFunctor(destImageRange(img), counter);
    00307     \endcode
    00308
    00309     <b> Required Interface:</b>
    00310
    00311     \code
    00312     ImageIterator upperleft, lowerright;
    00313     ImageIterator::row_iterator ix = upperleft.rowIterator();
    00314
    00315     Accessor accessor;
    00316     Functor f;
    00317
    00318     accessor.set(f(), ix);
    00319     \endcode
    00320
    00321 */
    00322 doxygen_overloaded_function(template <...> void initImageWithFunctor)
    00323
    00324 template <class ImageIterator, class Accessor, class FUNCTOR>
    00325 void
    00326 initImageWithFunctor(ImageIterator upperleft, ImageIterator lowerright,
    00327           Accessor a,  FUNCTOR & f)
    00328 {
    00329     int w = lowerright.x - upperleft.x;
    00330
    00331     for(; upperleft.y < lowerright.y; ++upperleft.y)
    00332     {
    00333         initLineFunctor(upperleft.rowIterator(), upperleft.rowIterator() + w, a, f);
    00334     }
    00335 }
    00336
    00337 template <class ImageIterator, class Accessor, class FUNCTOR>
    00338 inline
    00339 void
    00340 initImageWithFunctor(triple<ImageIterator, ImageIterator, Accessor> img, FUNCTOR & f)
    00341 {
    00342     initImageWithFunctor(img.first, img.second, img.third, f);
    00343 }
    00344
    00345 /********************************************************/
    00346 /*                                                      */
    00347 /*                      initImageIf                     */
    00348 /*                                                      */
    00349 /********************************************************/
    00350
    00351 /** \brief Write value to pixel in the image if mask is true.
    00352
    00353     This function can be used to init a region-of-interest of the image.
    00354     It uses an accessor to access the pixel data.
    00355
    00356     The initial value can either be a constant of appropriate type (compatible with
    00357     the destination's value_type), or a functor with compatible result_type. These two
    00358     cases are automatically distinguished when <tt>FunctorTraits<FUNCTOR>::isInitializer</tt>
    00359     yields <tt>VigraTrueType</tt>. Since the functor is passed by <tt>const</tt> reference, its
    00360     <tt>operator()</tt> must be const, and its internal state may need to be <tt>mutable</tt>.
    00361
    00362     <b> Declarations:</b>
    00363
    00364     pass arguments explicitly:
    00365     \code
    00366     namespace vigra {
    00367         template <class ImageIterator, class Accessor,
    00368                   class MaskImageIterator, class MaskAccessor,
    00369                   class VALUETYPE>
    00370         void initImageIf(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
    00371                          MaskImageIterator mask_upperleft, MaskAccessor ma,
    00372                          VALUETYPE const & v);
    00373
    00374         template <class ImageIterator, class Accessor,
    00375                   class MaskImageIterator, class MaskAccessor,
    00376                   class FUNCTOR>
    00377         void initImageIf(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
    00378                          MaskImageIterator mask_upperleft, MaskAccessor ma,
    00379                          FUNCTOR const & v);
    00380     }
    00381     \endcode
    00382
    00383     use argument objects in conjunction with \ref ArgumentObjectFactories :
    00384     \code
    00385     namespace vigra {
    00386         template <class ImageIterator, class Accessor,
    00387                   class MaskImageIterator, class MaskAccessor,
    00388                   class VALUETYPE>
    00389         void initImageIf(triple<ImageIterator, ImageIterator, Accessor> img,
    00390                          pair<MaskImageIterator, MaskAccessor> mask,
    00391                          VALUETYPE const & v);
    00392
    00393         template <class ImageIterator, class Accessor,
    00394                   class MaskImageIterator, class MaskAccessor,
    00395                   class FUNCTOR>
    00396         void initImageIf(triple<ImageIterator, ImageIterator, Accessor> img,
    00397                          pair<MaskImageIterator, MaskAccessor> mask,
    00398                          FUNCTOR const & v);
    00399     }
    00400     \endcode
    00401
    00402     <b> Usage:</b>
    00403
    00404         <b>\#include</b> <<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>><br>
    00405         Namespace: vigra
    00406
    00407     \code
    00408     vigra::BImage img(100, 100);
    00409     vigra::BImage mask(100, 100);
    00410
    00411     // zero the ROI
    00412     vigra::initImageIf(destImageRange(img),
    00413                 maskImage(mask),
    00414                 vigra::NumericTraits<vigra::BImage::PixelType>::zero());
    00415     \endcode
    00416
    00417     <b> Required Interface:</b>
    00418
    00419     \code
    00420     ImageIterator upperleft, lowerright;
    00421     MaskImageIterator mask_upperleft;
    00422     ImageIterator::row_iterator ix = upperleft.rowIterator();
    00423     MaskImageIterator::row_iterator mx = mask_upperleft.rowIterator();
    00424
    00425     Accessor accessor;
    00426     MaskAccessor mask_accessor;
    00427     VALUETYPE v;
    00428
    00429     if(mask_accessor(mx)) accessor.set(v, ix);
    00430     \endcode
    00431
    00432 */
    00433 doxygen_overloaded_function(template <...> void initImageIf)
    00434
    00435 template <class ImageIterator, class Accessor,
    00436           class MaskImageIterator, class MaskAccessor,
    00437           class VALUETYPE>
    00438 void
    00439 initImageIf(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
    00440           MaskImageIterator mask_upperleft, MaskAccessor ma,
    00441           VALUETYPE const & v)
    00442 {
    00443     int w = lowerright.x - upperleft.x;
    00444
    00445     for(; upperleft.y < lowerright.y; ++upperleft.y, ++mask_upperleft.y)
    00446     {
    00447         initLineIfImpl(upperleft.rowIterator(),
    00448                    upperleft.rowIterator() + w, a,
    00449                    mask_upperleft.rowIterator(), ma,
    00450                    v, typename FunctorTraits<VALUETYPE>::isInitializer());
    00451     }
    00452 }
    00453
    00454 template <class ImageIterator, class Accessor,
    00455           class MaskImageIterator, class MaskAccessor,
    00456           class VALUETYPE>
    00457 inline
    00458 void
    00459 initImageIf(triple<ImageIterator, ImageIterator, Accessor> img,
    00460             pair<MaskImageIterator, MaskAccessor> mask,
    00461             VALUETYPE const & v)
    00462 {
    00463     initImageIf(img.first, img.second, img.third, mask.first, mask.second, v);
    00464 }
    00465
    00466 /********************************************************/
    00467 /*                                                      */
    00468 /*                    initImageBorder                   */
    00469 /*                                                      */
    00470 /********************************************************/
    00471
    00472 /** \brief Write value to the specified border pixels in the image.
    00473
    00474     A pixel is initialized if its distance to the border
    00475     is at most 'borderwidth'. It uses an accessor to access the pixel data.
    00476
    00477     The initial value can either be a constant of appropriate type (compatible with
    00478     the destination's value_type), or a functor with compatible result_type. These two
    00479     cases are automatically distinguished when <tt>FunctorTraits<FUNCTOR>::isInitializer</tt>
    00480     yields <tt>VigraTrueType</tt>. Since the functor is passed by <tt>const</tt> reference, its
    00481     <tt>operator()</tt> must be const, and its internal state may need to be <tt>mutable</tt>.
    00482
    00483     <b> Declarations:</b>
    00484
    00485     pass arguments explicitly:
    00486     \code
    00487     namespace vigra {
    00488         template <class ImageIterator, class Accessor, class VALUETYPE>
    00489         void initImageBorder(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
    00490                              int border_width, VALUETYPE const & v);
    00491
    00492         template <class ImageIterator, class Accessor, class FUNCTOR>
    00493         void initImageBorder(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
    00494                              int border_width, FUNCTOR const & v);
    00495     }
    00496     \endcode
    00497
    00498     use argument objects in conjunction with \ref ArgumentObjectFactories :
    00499     \code
    00500     namespace vigra {
    00501         template <class ImageIterator, class Accessor, class VALUETYPE>
    00502         void initImageBorder(triple<ImageIterator, ImageIterator, Accessor> img,
    00503                              int border_width, VALUETYPE const & v);
    00504
    00505         template <class ImageIterator, class Accessor, class FUNCTOR>
    00506         void initImageBorder(triple<ImageIterator, ImageIterator, Accessor> img,
    00507                              int border_width, FUNCTOR const & v);
    00508     }
    00509     \endcode
    00510
    00511     <b> Usage:</b>
    00512
    00513         <b>\#include</b> <<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>><br>
    00514         Namespace: vigra
    00515
    00516     \code
    00517     vigra::BImage img(100, 100);
    00518
    00519     // zero a border of 5 pixel
    00520     vigra::initImageBorder(destImageRange(img),
    00521                     5, vigra::NumericTraits<vigra::BImage::PixelType>::zero());
    00522     \endcode
    00523
    00524     <b> Required Interface:</b>
    00525
    00526     see \ref initImage()
    00527
    00528 */
    00529 doxygen_overloaded_function(template <...> void initImageBorder)
    00530
    00531 template <class ImageIterator, class Accessor, class VALUETYPE>
    00532 inline
    00533 void
    00534 initImageBorder(ImageIterator upperleft, ImageIterator lowerright,
    00535                 Accessor a,  int border_width, VALUETYPE const & v)
    00536 {
    00537     int w = lowerright.x - upperleft.x;
    00538     int h = lowerright.y - upperleft.y;
    00539
    00540     int hb = (border_width > h) ? h : border_width;
    00541     int wb = (border_width > w) ? w : border_width;
    00542
    00543     initImage(upperleft, upperleft+Diff2D(w,hb), a, v);
    00544     initImage(upperleft, upperleft+Diff2D(wb,h), a, v);
    00545     initImage(upperleft+Diff2D(0,h-hb), lowerright, a, v);
    00546     initImage(upperleft+Diff2D(w-wb,0), lowerright, a, v);
    00547 }
    00548
    00549 template <class ImageIterator, class Accessor, class VALUETYPE>
    00550 inline
    00551 void
    00552 initImageBorder(triple<ImageIterator, ImageIterator, Accessor> img,
    00553                 int border_width, VALUETYPE const & v)
    00554 {
    00555     initImageBorder(img.first, img.second, img.third, border_width, v);
    00556 }
    00557
    00558 //@}
    00559
    00560
    00561 } // namespace vigra
    00562
    00563 #endif // VIGRA_INITIMAGE_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.7.1 (3 Dec 2010)