TuttleOFX  1
transform_pixels.hpp
Go to the documentation of this file.
00001 #ifndef _TERRY_ALGORITHM_TRANSFORM_PIXELS_HPP_
00002 #define _TERRY_ALGORITHM_TRANSFORM_PIXELS_HPP_
00003 
00004 #include <terry/math/Rect.hpp>
00005 #include <boost/gil/algorithm.hpp>
00006 
00007 namespace terry {
00008 namespace algorithm {
00009 
00010 // 1 source/dest without region
00011 
00012 /// \ingroup ImageViewSTLAlgorithmsTransformPixels
00013 /// \brief std::transform for image views
00014 template <typename View, typename F> GIL_FORCEINLINE
00015 F transform_pixels( const View& dst, F& fun )
00016 {
00017         for( std::ptrdiff_t y = 0; y < dst.height( ); ++y )
00018         {
00019                 typename View::x_iterator dstIt = dst.row_begin( y );
00020                 for( std::ptrdiff_t x = 0; x < dst.width( ); ++x )
00021                         fun( dstIt[x] );
00022         }
00023         return fun;
00024 }
00025 
00026 /// \ingroup ImageViewSTLAlgorithmsTransformPixels
00027 /// \brief std::transform for image views
00028 template <typename View, typename F> GIL_FORCEINLINE
00029 F transform_pixels( const View& dst, const F& fun )
00030 {
00031         for( std::ptrdiff_t y = 0; y < dst.height( ); ++y )
00032         {
00033                 typename View::x_iterator dstIt = dst.row_begin( y );
00034                 for( std::ptrdiff_t x = 0; x < dst.width( ); ++x )
00035                         fun( dstIt[x] );
00036         }
00037         return fun;
00038 }
00039 
00040 
00041 ////////////////////////////////////////////////////////////////////////////////
00042 ///
00043 /// transform_pixels_locator
00044 ///
00045 /// \defgroup ImageViewSTLAlgorithmsTransformPixelLocator transform_pixels_locator
00046 /// \ingroup ImageViewSTLAlgorithms
00047 /// \brief for image views (passes locators to the function object, instead of pixel references)
00048 ////////////////////////////////////////////////////////////////////////////////
00049 
00050 // 1 source/dest without region
00051 
00052 /// \ingroup ImageViewSTLAlgorithmsTransformPixels
00053 /// \brief std::transform for image views
00054 template <typename View, typename F> GIL_FORCEINLINE
00055 F transform_pixels_locator( const View& dst, F& fun )
00056 {
00057         typename View::xy_locator dloc = dst.xy_at( 0, 0 );
00058         for( std::ptrdiff_t y = 0; y < dst.height(); ++y )
00059         {
00060                 for( std::ptrdiff_t x = 0;
00061                      x < dst.width();
00062                      ++x, ++dloc.x() )
00063                 {
00064                         fun( dloc );
00065                 }
00066                 dloc.x() -= dst.width(); ++dloc.y();
00067         }
00068         return fun;
00069 }
00070 
00071 /// \ingroup ImageViewSTLAlgorithmsTransformPixels
00072 /// \brief std::transform for image views
00073 template <typename View, typename F> GIL_FORCEINLINE
00074 F transform_pixels_locator( const View& dst, const F& fun )
00075 {
00076         typename View::xy_locator dloc = dst.xy_at( 0, 0 );
00077         for( std::ptrdiff_t y = 0; y < dst.height(); ++y )
00078         {
00079                 for( std::ptrdiff_t x = 0;
00080                      x < dst.width();
00081                      ++x, ++dloc.x() )
00082                 {
00083                         fun( dloc );
00084                 }
00085                 dloc.x() -= dst.width(); ++dloc.y();
00086         }
00087         return fun;
00088 }
00089 
00090 
00091 // 1 source/dest with regions
00092 
00093 /// \ingroup ImageViewSTLAlgorithmsTransformPixels
00094 /// \brief std::transform for image views
00095 template <typename View, typename F>
00096 GIL_FORCEINLINE
00097 F transform_pixels_locator( const View& dst, const Rect<std::ptrdiff_t>& dstRod,
00098                                                         const Rect<std::ptrdiff_t>& renderWin, F& fun )
00099 {
00100         const std::ptrdiff_t renderWidth = renderWin.x2 - renderWin.x1;
00101         typename View::xy_locator dloc = dst.xy_at( renderWin.x1-dstRod.x1, renderWin.y1-dstRod.y1 );
00102         for( std::ptrdiff_t y = renderWin.y1; y < renderWin.y2; ++y )
00103         {
00104                 for( std::ptrdiff_t x = renderWin.x1;
00105                      x < renderWin.x2;
00106                      ++x, ++dloc.x() )
00107                 {
00108                         fun( dloc );
00109                 }
00110                 dloc.x() -= renderWidth; ++dloc.y();
00111         }
00112         return fun;
00113 }
00114 
00115 /// \ingroup ImageViewSTLAlgorithmsTransformPixels
00116 /// \brief std::transform for image views
00117 template <typename View, typename F>
00118 GIL_FORCEINLINE
00119 F transform_pixels_locator( const View& dst, const Rect<std::ptrdiff_t>& dstRod,
00120                                                         const Rect<std::ptrdiff_t>& renderWin, const F& fun )
00121 {
00122         const std::ptrdiff_t renderWidth = renderWin.x2 - renderWin.x1;
00123         typename View::xy_locator dloc = dst.xy_at( renderWin.x1-dstRod.x1, renderWin.y1-dstRod.y1 );
00124         for( std::ptrdiff_t y = renderWin.y1; y < renderWin.y2; ++y )
00125         {
00126                 for( std::ptrdiff_t x = renderWin.x1;
00127                      x < renderWin.x2;
00128                      ++x, ++dloc.x() )
00129                 {
00130                         fun( dloc );
00131                 }
00132                 dloc.x() -= renderWidth; ++dloc.y();
00133         }
00134         return fun;
00135 }
00136 
00137 
00138 // 1 source, 1 dest
00139 
00140 /// \ingroup ImageViewSTLAlgorithmsTransformPixels
00141 /// \brief std::transform for image views
00142 template <typename View, typename ViewDst, typename F>
00143 GIL_FORCEINLINE
00144 F transform_pixels_locator( const View& src, const Rect<std::ptrdiff_t>& srcRod,
00145                                                         const ViewDst& dst, const Rect<std::ptrdiff_t>& dstRod,
00146                                                         const Rect<std::ptrdiff_t>& renderWin, F& fun )
00147 {
00148         const std::ptrdiff_t renderWidth = renderWin.x2 - renderWin.x1;
00149         typename View::xy_locator sloc = src.xy_at( renderWin.x1-srcRod.x1, renderWin.y1-srcRod.y1 );
00150         for( std::ptrdiff_t y = renderWin.y1; y < renderWin.y2; ++y )
00151         {
00152                 typename ViewDst::x_iterator dstIt = dst.x_at( renderWin.x1-dstRod.x1, y-dstRod.y1 );
00153                 for( std::ptrdiff_t x = renderWin.x1;
00154                      x < renderWin.x2;
00155                      ++x, ++sloc.x(), ++dstIt )
00156                 {
00157                         *dstIt = fun( sloc );
00158                 }
00159                 sloc.x() -= renderWidth; ++sloc.y();
00160         }
00161         return fun;
00162 }
00163 
00164 /// \ingroup ImageViewSTLAlgorithmsTransformPixels
00165 /// \brief std::transform for image views
00166 template <typename View, typename ViewDst, typename F>
00167 GIL_FORCEINLINE
00168 F transform_pixels_locator( const View& src, const Rect<std::ptrdiff_t>& srcRod,
00169                                                         const ViewDst& dst, const Rect<std::ptrdiff_t>& dstRod,
00170                                                         const Rect<std::ptrdiff_t>& renderWin, const F& fun )
00171 {
00172         const std::ptrdiff_t renderWidth = renderWin.x2 - renderWin.x1;
00173         typename View::xy_locator sloc = src.xy_at( renderWin.x1-srcRod.x1, renderWin.y1-srcRod.y1 );
00174         for( std::ptrdiff_t y = renderWin.y1; y < renderWin.y2; ++y )
00175         {
00176                 typename ViewDst::x_iterator dstIt = dst.x_at( renderWin.x1-dstRod.x1, y-dstRod.y1 );
00177                 for( std::ptrdiff_t x = renderWin.x1;
00178                      x < renderWin.x2;
00179                      ++x, ++sloc.x(), ++dstIt )
00180                 {
00181                         *dstIt = fun( sloc );
00182                 }
00183                 sloc.x() -= renderWidth; ++sloc.y();
00184         }
00185         return fun;
00186 }
00187 
00188 
00189 // 2 sources, 1 dest
00190 
00191 /// \ingroup ImageViewSTLAlgorithmsTransformPixels
00192 /// \brief std::transform for image views
00193 template <typename View1, typename View2, typename ViewDst, typename F>
00194 GIL_FORCEINLINE
00195 F transform_pixels_locator( const View1& src1, const Rect<std::ptrdiff_t>& src1Rod,
00196                                                         const View2& src2, const Rect<std::ptrdiff_t>& src2Rod,
00197                                                         const ViewDst& dst, const Rect<std::ptrdiff_t>& dstRod,
00198                                                         const Rect<std::ptrdiff_t>& renderWin, F& fun )
00199 {
00200         const std::ptrdiff_t renderWidth = renderWin.x2 - renderWin.x1;
00201         typename View1::xy_locator s1loc = src1.xy_at( renderWin.x1-src1Rod.x1, renderWin.y1-src1Rod.y1 );
00202         typename View2::xy_locator s2loc = src2.xy_at( renderWin.x1-src2Rod.x1, renderWin.y1-src2Rod.y1 );
00203         for( std::ptrdiff_t y = renderWin.y1; y < renderWin.y2; ++y )
00204         {
00205                 typename ViewDst::x_iterator dstIt = dst.x_at( renderWin.x1-dstRod.x1, y-dstRod.y1 );
00206                 for( std::ptrdiff_t x = renderWin.x1;
00207                      x < renderWin.x2;
00208                      ++x, ++s1loc.x(), ++s2loc.x(), ++dstIt )
00209                 {
00210                         *dstIt = fun( s1loc, s2loc );
00211                 }
00212                 s1loc.x() -= renderWidth; ++s1loc.y();
00213                 s2loc.x() -= renderWidth; ++s2loc.y();
00214         }
00215         return fun;
00216 }
00217 
00218 /// \ingroup ImageViewSTLAlgorithmsTransformPixels
00219 /// \brief std::transform for image views
00220 template <typename View1, typename View2, typename ViewDst, typename F>
00221 GIL_FORCEINLINE
00222 F transform_pixels_locator( const View1& src1, const Rect<std::ptrdiff_t>& src1Rod,
00223                                                         const View2& src2, const Rect<std::ptrdiff_t>& src2Rod,
00224                                                         const ViewDst& dst, const Rect<std::ptrdiff_t>& dstRod,
00225                                                         const Rect<std::ptrdiff_t>& renderWin, const F& fun )
00226 {
00227         const std::ptrdiff_t renderWidth = renderWin.x2 - renderWin.x1;
00228         typename View1::xy_locator s1loc = src1.xy_at( renderWin.x1-src1Rod.x1, renderWin.y1-src1Rod.y1 );
00229         typename View2::xy_locator s2loc = src2.xy_at( renderWin.x1-src2Rod.x1, renderWin.y1-src2Rod.y1 );
00230         for( std::ptrdiff_t y = renderWin.y1; y < renderWin.y2; ++y )
00231         {
00232                 typename ViewDst::x_iterator dstIt = dst.x_at( dstRod.x1, y-dstRod.y1 );
00233                 for( std::ptrdiff_t x = renderWin.x1;
00234                      x < renderWin.x2;
00235                      ++x, ++s1loc.x(), ++s2loc.x(), ++dstIt )
00236                 {
00237                         *dstIt = fun( s1loc, s2loc );
00238                 }
00239                 s1loc.x() -= renderWidth; ++s1loc.y();
00240                 s2loc.x() -= renderWidth; ++s2loc.y();
00241         }
00242         return fun;
00243 }
00244 
00245 
00246 // 3 sources, 1 dest
00247 
00248 /// \ingroup ImageViewSTLAlgorithmsTransformPixels
00249 /// \brief std::transform for image views
00250 template <typename View1, typename View2, typename View3, typename ViewDst, typename F>
00251 GIL_FORCEINLINE
00252 F transform_pixels_locator( const View1& src1, const Rect<std::ptrdiff_t>& src1Rod,
00253                                                         const View2& src2, const Rect<std::ptrdiff_t>& src2Rod,
00254                                                         const View2& src3, const Rect<std::ptrdiff_t>& src3Rod,
00255                                                         const ViewDst& dst, const Rect<std::ptrdiff_t>& dstRod,
00256                                                         const Rect<std::ptrdiff_t>& renderWin, F& fun )
00257 {
00258         const std::ptrdiff_t renderWidth = renderWin.x2 - renderWin.x1;
00259         typename View1::xy_locator s1loc = src1.xy_at( renderWin.x1-src1Rod.x1, renderWin.y1-src1Rod.y1 );
00260         typename View2::xy_locator s2loc = src2.xy_at( renderWin.x1-src2Rod.x1, renderWin.y1-src2Rod.y1 );
00261         typename View3::xy_locator s3loc = src3.xy_at( renderWin.x1-src3Rod.x1, renderWin.y1-src3Rod.y1 );
00262         for( std::ptrdiff_t y = renderWin.y1; y < renderWin.y2; ++y )
00263         {
00264                 typename ViewDst::x_iterator dstIt = dst.x_at( dstRod.x1, y-dstRod.y1 );
00265                 for( std::ptrdiff_t x = renderWin.x1;
00266                      x < renderWin.x2;
00267                      ++x, ++s1loc.x(), ++s2loc.x(), ++s3loc.x(), ++dstIt )
00268                 {
00269                         *dstIt = fun( s1loc, s2loc, s3loc );
00270                 }
00271                 s1loc.x() -= renderWidth; ++s1loc.y();
00272                 s2loc.x() -= renderWidth; ++s2loc.y();
00273                 s3loc.x() -= renderWidth; ++s3loc.y();
00274         }
00275         return fun;
00276 }
00277 
00278 /// \ingroup ImageViewSTLAlgorithmsTransformPixels
00279 /// \brief std::transform for image views
00280 template <typename View1, typename View2, typename View3, typename ViewDst, typename F>
00281 GIL_FORCEINLINE
00282 F transform_pixels_locator( const View1& src1, const Rect<std::ptrdiff_t>& src1Rod,
00283                                                         const View2& src2, const Rect<std::ptrdiff_t>& src2Rod,
00284                                                         const View2& src3, const Rect<std::ptrdiff_t>& src3Rod,
00285                                                         const ViewDst& dst, const Rect<std::ptrdiff_t>& dstRod,
00286                                                         const Rect<std::ptrdiff_t>& renderWin, const F& fun )
00287 {
00288         const std::ptrdiff_t renderWidth = renderWin.x2 - renderWin.x1;
00289         typename View1::xy_locator s1loc = src1.xy_at( renderWin.x1-src1Rod.x1, renderWin.y1-src1Rod.y1 );
00290         typename View2::xy_locator s2loc = src2.xy_at( renderWin.x1-src2Rod.x1, renderWin.y1-src2Rod.y1 );
00291         typename View3::xy_locator s3loc = src3.xy_at( renderWin.x1-src3Rod.x1, renderWin.y1-src3Rod.y1 );
00292         for( std::ptrdiff_t y = renderWin.y1; y < renderWin.y2; ++y )
00293         {
00294                 typename ViewDst::x_iterator dstIt = dst.x_at( dstRod.x1, y-dstRod.y1 );
00295                 for( std::ptrdiff_t x = renderWin.x1;
00296                      x < renderWin.x2;
00297                      ++x, ++s1loc.x(), ++s2loc.x(), ++s3loc.x(), ++dstIt )
00298                 {
00299                         *dstIt = fun( s1loc, s2loc, s3loc );
00300                 }
00301                 s1loc.x() -= renderWidth; ++s1loc.y();
00302                 s2loc.x() -= renderWidth; ++s2loc.y();
00303                 s3loc.x() -= renderWidth; ++s3loc.y();
00304         }
00305         return fun;
00306 }
00307 
00308 
00309 }
00310 }
00311 
00312 #endif
00313