resample.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #ifndef BOOST_GIL_EXTENSION_NUMERIC_RESAMPLE_HPP
  9. #define BOOST_GIL_EXTENSION_NUMERIC_RESAMPLE_HPP
  10. #include <boost/gil/extension/numeric/affine.hpp>
  11. #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>
  12. #include <algorithm>
  13. #include <functional>
  14. namespace boost { namespace gil {
  15. // Support for generic image resampling
  16. // NOTE: The code is for example use only. It is not optimized for performance
  17. ///////////////////////////////////////////////////////////////////////////
  18. ////
  19. //// resample_pixels: set each pixel in the destination view as the result of a sampling function over the transformed coordinates of the source view
  20. ////
  21. ///////////////////////////////////////////////////////////////////////////
  22. template <typename MapFn> struct mapping_traits {};
  23. /// \brief Set each pixel in the destination view as the result of a sampling function over the transformed coordinates of the source view
  24. /// \ingroup ImageAlgorithms
  25. ///
  26. /// The provided implementation works for 2D image views only
  27. template <typename Sampler, // Models SamplerConcept
  28. typename SrcView, // Models RandomAccess2DImageViewConcept
  29. typename DstView, // Models MutableRandomAccess2DImageViewConcept
  30. typename MapFn> // Models MappingFunctionConcept
  31. void resample_pixels(const SrcView& src_view, const DstView& dst_view, const MapFn& dst_to_src, Sampler sampler=Sampler())
  32. {
  33. typename DstView::point_t dst_dims=dst_view.dimensions();
  34. typename DstView::point_t dst_p;
  35. for (dst_p.y=0; dst_p.y<dst_dims.y; ++dst_p.y) {
  36. typename DstView::x_iterator xit = dst_view.row_begin(dst_p.y);
  37. for (dst_p.x=0; dst_p.x<dst_dims.x; ++dst_p.x) {
  38. sample(sampler, src_view, transform(dst_to_src, dst_p), xit[dst_p.x]);
  39. }
  40. }
  41. }
  42. ///////////////////////////////////////////////////////////////////////////
  43. ////
  44. //// resample_pixels when one or both image views are run-time instantiated.
  45. ////
  46. ///////////////////////////////////////////////////////////////////////////
  47. namespace detail {
  48. template <typename Sampler, typename MapFn>
  49. struct resample_pixels_fn : public binary_operation_obj<resample_pixels_fn<Sampler,MapFn> > {
  50. MapFn _dst_to_src;
  51. Sampler _sampler;
  52. resample_pixels_fn(const MapFn& dst_to_src, const Sampler& sampler) : _dst_to_src(dst_to_src), _sampler(sampler) {}
  53. template <typename SrcView, typename DstView> BOOST_FORCEINLINE void apply_compatible(const SrcView& src, const DstView& dst) const {
  54. resample_pixels(src, dst, _dst_to_src, _sampler);
  55. }
  56. };
  57. }
  58. /// \brief resample_pixels when the source is run-time specified
  59. /// If invoked on incompatible views, throws std::bad_cast()
  60. /// \ingroup ImageAlgorithms
  61. template <typename Sampler, typename ...Types1, typename V2, typename MapFn>
  62. void resample_pixels(const any_image_view<Types1...>& src, const V2& dst, const MapFn& dst_to_src, Sampler sampler=Sampler())
  63. {
  64. apply_operation(src, std::bind(
  65. detail::resample_pixels_fn<Sampler, MapFn>(dst_to_src, sampler),
  66. std::placeholders::_1,
  67. dst));
  68. }
  69. /// \brief resample_pixels when the destination is run-time specified
  70. /// If invoked on incompatible views, throws std::bad_cast()
  71. /// \ingroup ImageAlgorithms
  72. template <typename Sampler, typename V1, typename ...Types2, typename MapFn>
  73. void resample_pixels(const V1& src, const any_image_view<Types2...>& dst, const MapFn& dst_to_src, Sampler sampler=Sampler())
  74. {
  75. using namespace std::placeholders;
  76. apply_operation(dst, std::bind(
  77. detail::resample_pixels_fn<Sampler, MapFn>(dst_to_src, sampler),
  78. src,
  79. std::placeholders::_1));
  80. }
  81. /// \brief resample_pixels when both the source and the destination are run-time specified
  82. /// If invoked on incompatible views, throws std::bad_cast()
  83. /// \ingroup ImageAlgorithms
  84. template <typename Sampler, typename ...SrcTypes, typename ...DstTypes, typename MapFn>
  85. void resample_pixels(const any_image_view<SrcTypes...>& src, const any_image_view<DstTypes...>& dst, const MapFn& dst_to_src, Sampler sampler=Sampler()) {
  86. apply_operation(src,dst,detail::resample_pixels_fn<Sampler,MapFn>(dst_to_src,sampler));
  87. }
  88. ///////////////////////////////////////////////////////////////////////////
  89. ////
  90. //// resample_subimage: copy into the destination a rotated rectangular region from the source, rescaling it to fit into the destination
  91. ////
  92. ///////////////////////////////////////////////////////////////////////////
  93. // Extract into dst the rotated bounds [src_min..src_max] rotated at 'angle' from the source view 'src'
  94. // The source coordinates are in the coordinate space of the source image
  95. // Note that the views could also be variants (i.e. any_image_view)
  96. template <typename Sampler, typename SrcMetaView, typename DstMetaView>
  97. void resample_subimage(const SrcMetaView& src, const DstMetaView& dst,
  98. double src_min_x, double src_min_y,
  99. double src_max_x, double src_max_y,
  100. double angle, const Sampler& sampler=Sampler()) {
  101. double src_width = std::max<double>(src_max_x - src_min_x - 1,1);
  102. double src_height = std::max<double>(src_max_y - src_min_y - 1,1);
  103. double dst_width = std::max<double>((double)(dst.width()-1),1);
  104. double dst_height = std::max<double>((double)(dst.height()-1),1);
  105. matrix3x2<double> mat =
  106. matrix3x2<double>::get_translate(-dst_width/2.0, -dst_height/2.0) *
  107. matrix3x2<double>::get_scale(src_width / dst_width, src_height / dst_height)*
  108. matrix3x2<double>::get_rotate(-angle)*
  109. matrix3x2<double>::get_translate(src_min_x + src_width/2.0, src_min_y + src_height/2.0);
  110. resample_pixels(src,dst,mat,sampler);
  111. }
  112. ///////////////////////////////////////////////////////////////////////////
  113. ////
  114. //// resize_view: Copy the source view into the destination, scaling to fit
  115. ////
  116. ///////////////////////////////////////////////////////////////////////////
  117. template <typename Sampler, typename SrcMetaView, typename DstMetaView>
  118. void resize_view(const SrcMetaView& src, const DstMetaView& dst, const Sampler& sampler=Sampler()) {
  119. resample_subimage(src,dst,0.0,0.0,(double)src.width(),(double)src.height(),0.0,sampler);
  120. }
  121. } } // namespace boost::gil
  122. #endif // BOOST_GIL_EXTENSION_NUMERIC_RESAMPLE_HPP