algorithm.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_DYNAMIC_IMAGE_ALGORITHM_HPP
  9. #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_ALGORITHM_HPP
  10. #include <boost/gil/extension/dynamic_image/any_image.hpp>
  11. #include <boost/gil/algorithm.hpp>
  12. #include <functional>
  13. ////////////////////////////////////////////////////////////////////////////////////////
  14. /// \file
  15. /// \brief Some basic STL-style algorithms when applied to runtime type specified image views
  16. /// \author Lubomir Bourdev and Hailin Jin \n
  17. /// Adobe Systems Incorporated
  18. /// \date 2005-2007 \n Last updated on September 24, 2006
  19. ///
  20. ////////////////////////////////////////////////////////////////////////////////////////
  21. namespace boost { namespace gil {
  22. namespace detail {
  23. struct equal_pixels_fn : binary_operation_obj<equal_pixels_fn, bool>
  24. {
  25. template <typename V1, typename V2>
  26. BOOST_FORCEINLINE
  27. bool apply_compatible(V1 const& v1, V2 const& v2) const
  28. {
  29. return equal_pixels(v1, v2);
  30. }
  31. };
  32. } // namespace detail
  33. /// \ingroup ImageViewSTLAlgorithmsEqualPixels
  34. /// \tparam Types Model Boost.MP11-compatible list of models of ImageViewConcept
  35. /// \tparam View Model MutableImageViewConcept
  36. template <typename Types, typename View>
  37. bool equal_pixels(any_image_view<Types> const& src, View const& dst)
  38. {
  39. return apply_operation(
  40. src,
  41. std::bind(detail::equal_pixels_fn(), std::placeholders::_1, dst));
  42. }
  43. /// \ingroup ImageViewSTLAlgorithmsEqualPixels
  44. /// \tparam View Model ImageViewConcept
  45. /// \tparam Types Model Boost.MP11-compatible list of models of MutableImageViewConcept
  46. template <typename View, typename Types>
  47. bool equal_pixels(View const& src, any_image_view<Types> const& dst)
  48. {
  49. return apply_operation(
  50. dst,
  51. std::bind(detail::equal_pixels_fn(), src, std::placeholders::_1));
  52. }
  53. /// \ingroup ImageViewSTLAlgorithmsEqualPixels
  54. /// \tparam Types1 Model Boost.MP11-compatible list of models of ImageViewConcept
  55. /// \tparam Types2 Model Boost.MP11-compatible list of models of MutableImageViewConcept
  56. template <typename Types1, typename Types2>
  57. bool equal_pixels(any_image_view<Types1> const& src, any_image_view<Types2> const& dst)
  58. {
  59. return apply_operation(src, dst, detail::equal_pixels_fn());
  60. }
  61. namespace detail {
  62. struct copy_pixels_fn : public binary_operation_obj<copy_pixels_fn>
  63. {
  64. template <typename View1, typename View2>
  65. BOOST_FORCEINLINE
  66. void apply_compatible(View1 const& src, View2 const& dst) const
  67. {
  68. copy_pixels(src,dst);
  69. }
  70. };
  71. } // namespace detail
  72. /// \ingroup ImageViewSTLAlgorithmsCopyPixels
  73. /// \tparam Types Model Boost.MP11-compatible list of models of ImageViewConcept
  74. /// \tparam View Model MutableImageViewConcept
  75. template <typename Types, typename View>
  76. void copy_pixels(any_image_view<Types> const& src, View const& dst)
  77. {
  78. apply_operation(src, std::bind(detail::copy_pixels_fn(), std::placeholders::_1, dst));
  79. }
  80. /// \ingroup ImageViewSTLAlgorithmsCopyPixels
  81. /// \tparam Types Model Boost.MP11-compatible list of models of MutableImageViewConcept
  82. /// \tparam View Model ImageViewConcept
  83. template <typename Types, typename View>
  84. void copy_pixels(View const& src, any_image_view<Types> const& dst)
  85. {
  86. apply_operation(dst, std::bind(detail::copy_pixels_fn(), src, std::placeholders::_1));
  87. }
  88. /// \ingroup ImageViewSTLAlgorithmsCopyPixels
  89. /// \tparam Types1 Model Boost.MP11-compatible list of models of ImageViewConcept
  90. /// \tparam Types2 Model Boost.MP11-compatible list of models of MutableImageViewConcept
  91. template <typename Types1, typename Types2>
  92. void copy_pixels(any_image_view<Types1> const& src, any_image_view<Types2> const& dst)
  93. {
  94. apply_operation(src, dst, detail::copy_pixels_fn());
  95. }
  96. //forward declaration for default_color_converter (see full definition in color_convert.hpp)
  97. struct default_color_converter;
  98. /// \ingroup ImageViewSTLAlgorithmsCopyAndConvertPixels
  99. /// \tparam Types Model Boost.MP11-compatible list of models of ImageViewConcept
  100. /// \tparam View Model MutableImageViewConcept
  101. /// \tparam CC Model ColorConverterConcept
  102. template <typename Types, typename View, typename CC>
  103. void copy_and_convert_pixels(any_image_view<Types> const& src, View const& dst, CC cc)
  104. {
  105. using cc_fn = detail::copy_and_convert_pixels_fn<CC>;
  106. apply_operation(src, std::bind(cc_fn{cc}, std::placeholders::_1, dst));
  107. }
  108. /// \ingroup ImageViewSTLAlgorithmsCopyAndConvertPixels
  109. /// \tparam Types Model Boost.MP11-compatible list of models of ImageViewConcept
  110. /// \tparam View Model MutableImageViewConcept
  111. template <typename Types, typename View>
  112. void copy_and_convert_pixels(any_image_view<Types> const& src, View const& dst)
  113. {
  114. using cc_fn = detail::copy_and_convert_pixels_fn<default_color_converter>;
  115. apply_operation(src, std::bind(cc_fn{}, std::placeholders::_1, dst));
  116. }
  117. /// \ingroup ImageViewSTLAlgorithmsCopyAndConvertPixels
  118. /// \tparam View Model ImageViewConcept
  119. /// \tparam Types Model Boost.MP11-compatible list of models of MutableImageViewConcept
  120. /// \tparam CC Model ColorConverterConcept
  121. template <typename View, typename Types, typename CC>
  122. void copy_and_convert_pixels(View const& src, any_image_view<Types> const& dst, CC cc)
  123. {
  124. using cc_fn = detail::copy_and_convert_pixels_fn<CC>;
  125. apply_operation(dst, std::bind(cc_fn{cc}, src, std::placeholders::_1));
  126. }
  127. /// \ingroup ImageViewSTLAlgorithmsCopyAndConvertPixels
  128. /// \tparam View Model ImageViewConcept
  129. /// \tparam Type Model Boost.MP11-compatible list of models of MutableImageViewConcept
  130. template <typename View, typename Types>
  131. void copy_and_convert_pixels(View const& src, any_image_view<Types> const& dst)
  132. {
  133. using cc_fn = detail::copy_and_convert_pixels_fn<default_color_converter>;
  134. apply_operation(dst, std::bind(cc_fn{}, src, std::placeholders::_1));
  135. }
  136. /// \ingroup ImageViewSTLAlgorithmsCopyAndConvertPixels
  137. /// \tparam Types1 Model Boost.MP11-compatible list of models of ImageViewConcept
  138. /// \tparam Types2 Model Boost.MP11-compatible list of models of MutableImageViewConcept
  139. /// \tparam CC Model ColorConverterConcept
  140. template <typename Types1, typename Types2, typename CC>
  141. void copy_and_convert_pixels(
  142. any_image_view<Types1> const& src,
  143. any_image_view<Types2> const& dst, CC cc)
  144. {
  145. apply_operation(src, dst, detail::copy_and_convert_pixels_fn<CC>(cc));
  146. }
  147. /// \ingroup ImageViewSTLAlgorithmsCopyAndConvertPixels
  148. /// \tparam Types1 Model Boost.MP11-compatible list of models of ImageViewConcept
  149. /// \tparam Types2 Model Boost.MP11-compatible list of models of MutableImageViewConcept
  150. template <typename Types1, typename Types2>
  151. void copy_and_convert_pixels(
  152. any_image_view<Types1> const& src,
  153. any_image_view<Types2> const& dst)
  154. {
  155. apply_operation(src, dst,
  156. detail::copy_and_convert_pixels_fn<default_color_converter>());
  157. }
  158. namespace detail {
  159. template <bool IsCompatible>
  160. struct fill_pixels_fn1
  161. {
  162. template <typename V, typename Value>
  163. static void apply(V const &src, Value const &val) { fill_pixels(src, val); }
  164. };
  165. // copy_pixels invoked on incompatible images
  166. template <>
  167. struct fill_pixels_fn1<false>
  168. {
  169. template <typename V, typename Value>
  170. static void apply(V const &, Value const &) { throw std::bad_cast();}
  171. };
  172. template <typename Value>
  173. struct fill_pixels_fn
  174. {
  175. fill_pixels_fn(Value const& val) : val_(val) {}
  176. using result_type = void;
  177. template <typename V>
  178. result_type operator()(V const& view) const
  179. {
  180. fill_pixels_fn1
  181. <
  182. pixels_are_compatible
  183. <
  184. typename V::value_type,
  185. Value
  186. >::value
  187. >::apply(view, val_);
  188. }
  189. Value val_;
  190. };
  191. } // namespace detail
  192. /// \ingroup ImageViewSTLAlgorithmsFillPixels
  193. /// \brief fill_pixels for any image view. The pixel to fill with must be compatible with the current view
  194. /// \tparam Types Model Boost.MP11-compatible list of models of MutableImageViewConcept
  195. template <typename Types, typename Value>
  196. void fill_pixels(any_image_view<Types> const& view, Value const& val)
  197. {
  198. apply_operation(view, detail::fill_pixels_fn<Value>(val));
  199. }
  200. }} // namespace boost::gil
  201. #endif