TuttleOFX  1
resample.hpp
Go to the documentation of this file.
00001 /*
00002   Copyright 2005-2007 Adobe Systems Incorporated
00003   Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
00004   or a copy at http://opensource.adobe.com/licenses.html)
00005 */
00006 
00007 /*************************************************************************************************/
00008 
00009 #ifndef _TERRY_SAMPLER_RESAMPLE_HPP_
00010 #define _TERRY_SAMPLER_RESAMPLE_HPP_
00011 
00012 #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>
00013 
00014 #include <boost/lambda/lambda.hpp>
00015 #include <boost/lambda/bind.hpp>
00016 
00017 ////////////////////////////////////////////////////////////////////////////////////////
00018 /// @file               
00019 /// @brief support for generic image resampling
00020 ///        NOTE: The code is for example use only. It is not optimized for performance
00021 /// @author Lubomir Bourdev and Hailin Jin \n
00022 ///         Adobe Systems Incorporated
00023 /// @date   2005-2007 \n October 30, 2006
00024 ///
00025 ////////////////////////////////////////////////////////////////////////////////////////
00026 
00027 namespace terry {
00028 
00029 using namespace boost::gil;
00030 
00031 ///////////////////////////////////////////////////////////////////////////
00032 ////
00033 ////   resample_pixels: set each pixel in the destination view as the result of a sampling function over the transformed coordinates of the source view
00034 ////
00035 ///////////////////////////////////////////////////////////////////////////
00036 
00037 template <typename MapFn> struct mapping_traits {};
00038 
00039 /**
00040  * @brief Set each pixel in the destination view as the result of a sampling function over the transformed coordinates of the source view
00041  * @ingroup ImageAlgorithms
00042  *
00043  * The provided implementation works for 2D image views only
00044  */
00045 template <typename Sampler, // Models SamplerConcept
00046           typename SrcView, // Models RandomAccess2DImageViewConcept
00047           typename DstView, // Models MutableRandomAccess2DImageViewConcept
00048           typename MapFn>   // Models MappingFunctionConcept
00049 void resample_pixels( const SrcView& src_view, const DstView& dst_view, const MapFn& dst_to_src, Sampler sampler=Sampler(), const sampler::EParamFilterOutOfImage outOfImageProcess = sampler::eParamFilterOutBlack )
00050 {
00051     typename DstView::point_t dst_dims = dst_view.dimensions();
00052     typename DstView::point_t dst_p;
00053     //typename mapping_traits<MapFn>::result_type src_p;
00054 
00055     for( dst_p.y=0; dst_p.y<dst_dims.y; ++dst_p.y )
00056     {
00057         typename DstView::x_iterator xit = dst_view.row_begin(dst_p.y);
00058         for( dst_p.x=0; dst_p.x<dst_dims.x; ++dst_p.x )
00059         {
00060             sample(sampler, src_view, transform(dst_to_src, dst_p), xit[dst_p.x], outOfImageProcess);
00061         }
00062     }
00063 }
00064 
00065 ///////////////////////////////////////////////////////////////////////////
00066 ////
00067 ////   resample_pixels when one or both image views are run-time instantiated. 
00068 ////
00069 ///////////////////////////////////////////////////////////////////////////
00070 
00071 namespace detail {
00072     template <typename Sampler, typename MapFn>
00073     struct resample_pixels_fn : public binary_operation_obj<resample_pixels_fn<Sampler,MapFn> >
00074     {
00075         MapFn  _dst_to_src;
00076         Sampler _sampler;
00077         
00078         resample_pixels_fn( const MapFn& dst_to_src, const Sampler& sampler )
00079                 : _dst_to_src(dst_to_src)
00080                 , _sampler(sampler)
00081         {}
00082 
00083         template <typename SrcView, typename DstView>
00084         GIL_FORCEINLINE void apply_compatible( const SrcView& src, const DstView& dst ) const
00085         {
00086             resample_pixels( src, dst, _dst_to_src, _sampler );
00087         }
00088     };
00089 }
00090 
00091 /**
00092  * @brief resample_pixels when the source is run-time specified
00093  *        If invoked on incompatible views, throws std::bad_cast()
00094  * @ingroup ImageAlgorithms
00095  */
00096 template <typename Sampler, typename Types1, typename V2, typename MapFn>
00097 void resample_pixels( const any_image_view<Types1>& src, const V2& dst, const MapFn& dst_to_src, Sampler sampler=Sampler() )
00098 {
00099     apply_operation( src, bind(detail::resample_pixels_fn<Sampler,MapFn>(dst_to_src,sampler), _1, dst) );
00100 }
00101 
00102 /**
00103  * @brief resample_pixels when the destination is run-time specified
00104  *        If invoked on incompatible views, throws std::bad_cast()
00105  * @ingroup ImageAlgorithms
00106  */
00107 template <typename Sampler, typename V1, typename Types2, typename MapFn>
00108 void resample_pixels( const V1& src, const any_image_view<Types2>& dst, const MapFn& dst_to_src, Sampler sampler=Sampler() )
00109 {
00110     apply_operation( dst, bind(detail::resample_pixels_fn<Sampler,MapFn>(dst_to_src,sampler), src, _1) );
00111 }
00112 
00113 /**
00114  * @brief resample_pixels when both the source and the destination are run-time specified
00115  *        If invoked on incompatible views, throws std::bad_cast()
00116  * @ingroup ImageAlgorithms
00117  */
00118 template <typename Sampler, typename SrcTypes, typename DstTypes, typename MapFn> 
00119 void resample_pixels( const any_image_view<SrcTypes>& src, const any_image_view<DstTypes>& dst, const MapFn& dst_to_src, Sampler sampler=Sampler() )
00120 {
00121     apply_operation(src,dst,detail::resample_pixels_fn<Sampler,MapFn>(dst_to_src,sampler));
00122 }
00123 
00124 }
00125 
00126 #endif
00127