sampler.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_SAMPLER_HPP
  9. #define BOOST_GIL_EXTENSION_NUMERIC_SAMPLER_HPP
  10. #include <boost/gil/extension/numeric/pixel_numeric_operations.hpp>
  11. #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>
  12. namespace boost { namespace gil {
  13. // Nearest-neighbor and bilinear image samplers.
  14. // NOTE: The code is for example use only. It is not optimized for performance
  15. ///////////////////////////////////////////////////////////////////////////
  16. ////
  17. //// resample_pixels: set each pixel in the destination view as the result of a sampling function over the transformed coordinates of the source view
  18. ////
  19. ///////////////////////////////////////////////////////////////////////////
  20. /*
  21. template <typename Sampler>
  22. concept SamplerConcept {
  23. template <typename DstP, // Models PixelConcept
  24. typename SrcView, // Models RandomAccessNDImageViewConcept
  25. typename S_COORDS> // Models PointNDConcept, where S_COORDS::num_dimensions == SrcView::num_dimensions
  26. bool sample(const Sampler& s, const SrcView& src, const S_COORDS& p, DstP result);
  27. };
  28. */
  29. /// \brief A sampler that sets the destination pixel to the closest one in the source. If outside the bounds, it doesn't change the destination
  30. /// \ingroup ImageAlgorithms
  31. struct nearest_neighbor_sampler {};
  32. template <typename DstP, typename SrcView, typename F>
  33. bool sample(nearest_neighbor_sampler, SrcView const& src, point<F> const& p, DstP& result)
  34. {
  35. typename SrcView::point_t center(iround(p));
  36. if (center.x >= 0 && center.y >= 0 && center.x < src.width() && center.y < src.height())
  37. {
  38. result=src(center.x,center.y);
  39. return true;
  40. }
  41. return false;
  42. }
  43. struct cast_channel_fn {
  44. template <typename SrcChannel, typename DstChannel>
  45. void operator()(const SrcChannel& src, DstChannel& dst) {
  46. using dst_value_t = typename channel_traits<DstChannel>::value_type;
  47. dst = dst_value_t(src);
  48. }
  49. };
  50. template <typename SrcPixel, typename DstPixel>
  51. void cast_pixel(const SrcPixel& src, DstPixel& dst) {
  52. static_for_each(src,dst,cast_channel_fn());
  53. }
  54. namespace detail {
  55. template <typename Weight>
  56. struct add_dst_mul_src_channel {
  57. Weight _w;
  58. add_dst_mul_src_channel(Weight w) : _w(w) {}
  59. template <typename SrcChannel, typename DstChannel>
  60. void operator()(const SrcChannel& src, DstChannel& dst) const {
  61. dst += DstChannel(src*_w);
  62. }
  63. };
  64. // dst += DST_TYPE(src * w)
  65. template <typename SrcP,typename Weight,typename DstP>
  66. struct add_dst_mul_src {
  67. void operator()(const SrcP& src, Weight weight, DstP& dst) const {
  68. static_for_each(src,dst, add_dst_mul_src_channel<Weight>(weight));
  69. // pixel_assigns_t<DstP,DstP&>()(
  70. // pixel_plus_t<DstP,DstP,DstP>()(
  71. // pixel_multiplies_scalar_t<SrcP,Weight,DstP>()(src,weight),
  72. // dst),
  73. // dst);
  74. }
  75. };
  76. } // namespace detail
  77. /// \brief A sampler that sets the destination pixel as the bilinear interpolation of the four closest pixels from the source.
  78. /// If outside the bounds, it doesn't change the destination
  79. /// \ingroup ImageAlgorithms
  80. struct bilinear_sampler {};
  81. template <typename DstP, typename SrcView, typename F>
  82. bool sample(bilinear_sampler, SrcView const& src, point<F> const& p, DstP& result)
  83. {
  84. using SrcP = typename SrcView::value_type;
  85. point_t p0(ifloor(p.x), ifloor(p.y)); // the closest integer coordinate top left from p
  86. point<F> frac(p.x-p0.x, p.y-p0.y);
  87. if (p0.x < -1 || p0.y < -1 || p0.x>=src.width() || p0.y>=src.height())
  88. {
  89. return false;
  90. }
  91. pixel<F,devicen_layout_t<num_channels<SrcView>::value> > mp(0); // suboptimal
  92. typename SrcView::xy_locator loc=src.xy_at(p0.x,p0.y);
  93. if (p0.x == -1)
  94. {
  95. if (p0.y == -1)
  96. {
  97. // the top-left corner pixel
  98. ++loc.y();
  99. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], 1 ,mp);
  100. }
  101. else if (p0.y+1<src.height())
  102. {
  103. // on the first column, but not the top-left nor bottom-left corner pixel
  104. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], (1-frac.y),mp);
  105. ++loc.y();
  106. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], frac.y ,mp);
  107. }
  108. else
  109. {
  110. // the bottom-left corner pixel
  111. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], 1 ,mp);
  112. }
  113. }
  114. else if (p0.x+1<src.width())
  115. {
  116. if (p0.y == -1)
  117. {
  118. // on the first row, but not the top-left nor top-right corner pixel
  119. ++loc.y();
  120. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.x) ,mp);
  121. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], frac.x ,mp);
  122. }
  123. else if (p0.y+1<src.height())
  124. {
  125. // most common case - inside the image, not on the frist nor last row/column
  126. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.x)*(1-frac.y),mp);
  127. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], frac.x *(1-frac.y),mp);
  128. ++loc.y();
  129. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.x)* frac.y ,mp);
  130. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], frac.x * frac.y ,mp);
  131. }
  132. else
  133. {
  134. // on the last row, but not the bottom-left nor bottom-right corner pixel
  135. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.x) ,mp);
  136. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], frac.x ,mp);
  137. }
  138. }
  139. else
  140. {
  141. if (p0.y == -1)
  142. {
  143. // the top-right corner pixel
  144. ++loc.y();
  145. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, 1 ,mp);
  146. }
  147. else if (p0.y+1<src.height())
  148. {
  149. // on the last column, but not the top-right nor bottom-right corner pixel
  150. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.y),mp);
  151. ++loc.y();
  152. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, frac.y ,mp);
  153. }
  154. else
  155. {
  156. // the bottom-right corner pixel
  157. detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, 1 ,mp);
  158. }
  159. }
  160. // Convert from floating point average value to the source type
  161. SrcP src_result;
  162. cast_pixel(mp,src_result);
  163. color_convert(src_result, result);
  164. return true;
  165. }
  166. }} // namespace boost::gil
  167. #endif