Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:37:00

0001 //
0002 // Copyright 2005-2007 Adobe Systems Incorporated
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 #ifndef BOOST_GIL_EXTENSION_NUMERIC_RESAMPLE_HPP
0009 #define BOOST_GIL_EXTENSION_NUMERIC_RESAMPLE_HPP
0010 
0011 #include <boost/gil/extension/numeric/affine.hpp>
0012 #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>
0013 
0014 #include <algorithm>
0015 #include <functional>
0016 
0017 namespace boost { namespace gil {
0018 
0019 // Support for generic image resampling
0020 // NOTE: The code is for example use only. It is not optimized for performance
0021 
0022 ///////////////////////////////////////////////////////////////////////////
0023 ////
0024 ////   resample_pixels: set each pixel in the destination view as the result of a sampling function over the transformed coordinates of the source view
0025 ////
0026 ///////////////////////////////////////////////////////////////////////////
0027 
0028 template <typename MapFn> struct mapping_traits {};
0029 
0030 /// \brief Set each pixel in the destination view as the result of a sampling function over the transformed coordinates of the source view
0031 /// \ingroup ImageAlgorithms
0032 ///
0033 /// The provided implementation works for 2D image views only
0034 template <typename Sampler,        // Models SamplerConcept
0035           typename SrcView,        // Models RandomAccess2DImageViewConcept
0036           typename DstView,        // Models MutableRandomAccess2DImageViewConcept
0037           typename MapFn>        // Models MappingFunctionConcept
0038 void resample_pixels(const SrcView& src_view, const DstView& dst_view, const MapFn& dst_to_src, Sampler sampler=Sampler())
0039 {
0040     typename DstView::point_t dst_dims=dst_view.dimensions();
0041     typename DstView::point_t dst_p;
0042 
0043     for (dst_p.y=0; dst_p.y<dst_dims.y; ++dst_p.y) {
0044         typename DstView::x_iterator xit = dst_view.row_begin(dst_p.y);
0045         for (dst_p.x=0; dst_p.x<dst_dims.x; ++dst_p.x) {
0046             sample(sampler, src_view, transform(dst_to_src, dst_p), xit[dst_p.x]);
0047         }
0048     }
0049 }
0050 
0051 ///////////////////////////////////////////////////////////////////////////
0052 ////
0053 ////   resample_pixels when one or both image views are run-time instantiated.
0054 ////
0055 ///////////////////////////////////////////////////////////////////////////
0056 
0057 namespace detail {
0058     template <typename Sampler, typename MapFn>
0059     struct resample_pixels_fn : public binary_operation_obj<resample_pixels_fn<Sampler,MapFn> > {
0060         MapFn  _dst_to_src;
0061         Sampler _sampler;
0062         resample_pixels_fn(const MapFn& dst_to_src, const Sampler& sampler) : _dst_to_src(dst_to_src), _sampler(sampler) {}
0063 
0064         template <typename SrcView, typename DstView> BOOST_FORCEINLINE void apply_compatible(const SrcView& src, const DstView& dst)  const {
0065             resample_pixels(src, dst, _dst_to_src, _sampler);
0066         }
0067     };
0068 }
0069 
0070 /// \brief resample_pixels when the source is run-time specified
0071 ///        If invoked on incompatible views, throws std::bad_cast()
0072 /// \ingroup ImageAlgorithms
0073 template <typename Sampler, typename ...Types1, typename V2, typename MapFn>
0074 void resample_pixels(const any_image_view<Types1...>& src, const V2& dst, const MapFn& dst_to_src, Sampler sampler=Sampler())
0075 {
0076     variant2::visit(std::bind(
0077         detail::resample_pixels_fn<Sampler, MapFn>(dst_to_src, sampler),
0078         std::placeholders::_1,
0079         dst), src);
0080 }
0081 
0082 /// \brief resample_pixels when the destination is run-time specified
0083 ///        If invoked on incompatible views, throws std::bad_cast()
0084 /// \ingroup ImageAlgorithms
0085 template <typename Sampler, typename V1, typename ...Types2, typename MapFn>
0086 void resample_pixels(const V1& src, const any_image_view<Types2...>& dst, const MapFn& dst_to_src, Sampler sampler=Sampler())
0087 {
0088     using namespace std::placeholders;
0089     variant2::visit(std::bind(
0090         detail::resample_pixels_fn<Sampler, MapFn>(dst_to_src, sampler),
0091         src,
0092         std::placeholders::_1), dst);
0093 }
0094 
0095 /// \brief resample_pixels when both the source and the destination are run-time specified
0096 ///        If invoked on incompatible views, throws std::bad_cast()
0097 /// \ingroup ImageAlgorithms
0098 template <typename Sampler, typename ...SrcTypes, typename ...DstTypes, typename MapFn>
0099 void resample_pixels(const any_image_view<SrcTypes...>& src, const any_image_view<DstTypes...>& dst, const MapFn& dst_to_src, Sampler sampler=Sampler()) {
0100     variant2::visit(detail::resample_pixels_fn<Sampler,MapFn>(dst_to_src,sampler), src, dst);
0101 }
0102 
0103 ///////////////////////////////////////////////////////////////////////////
0104 ////
0105 ////   resample_subimage: copy into the destination a rotated rectangular region from the source, rescaling it to fit into the destination
0106 ////
0107 ///////////////////////////////////////////////////////////////////////////
0108 
0109 // Extract into dst the rotated bounds [src_min..src_max] rotated at 'angle' from the source view 'src'
0110 // The source coordinates are in the coordinate space of the source image
0111 // Note that the views could also be variants (i.e. any_image_view)
0112 template <typename Sampler, typename SrcMetaView, typename DstMetaView>
0113 void resample_subimage(const SrcMetaView& src, const DstMetaView& dst,
0114                          double src_min_x, double src_min_y,
0115                          double src_max_x, double src_max_y,
0116                          double angle, const Sampler& sampler=Sampler()) {
0117     double src_width  = std::max<double>(src_max_x - src_min_x - 1,1);
0118     double src_height = std::max<double>(src_max_y - src_min_y - 1,1);
0119     double dst_width  = std::max<double>((double)(dst.width()-1),1);
0120     double dst_height = std::max<double>((double)(dst.height()-1),1);
0121 
0122     matrix3x2<double> mat =
0123         matrix3x2<double>::get_translate(-dst_width/2.0, -dst_height/2.0) *
0124         matrix3x2<double>::get_scale(src_width / dst_width, src_height / dst_height)*
0125         matrix3x2<double>::get_rotate(-angle)*
0126         matrix3x2<double>::get_translate(src_min_x + src_width/2.0, src_min_y + src_height/2.0);
0127     resample_pixels(src,dst,mat,sampler);
0128 }
0129 
0130 ///////////////////////////////////////////////////////////////////////////
0131 ////
0132 ////   resize_view: Copy the source view into the destination, scaling to fit
0133 ////
0134 ///////////////////////////////////////////////////////////////////////////
0135 
0136 template <typename Sampler, typename SrcMetaView, typename DstMetaView>
0137 void resize_view(const SrcMetaView& src, const DstMetaView& dst, const Sampler& sampler=Sampler()) {
0138     resample_subimage(src,dst,0.0,0.0,(double)src.width(),(double)src.height(),0.0,sampler);
0139 }
0140 
0141 } }  // namespace boost::gil
0142 
0143 #endif // BOOST_GIL_EXTENSION_NUMERIC_RESAMPLE_HPP