any_image_view.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_ANY_IMAGE_VIEW_HPP
  9. #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_ANY_IMAGE_VIEW_HPP
  10. #include <boost/gil/dynamic_step.hpp>
  11. #include <boost/gil/image.hpp>
  12. #include <boost/gil/image_view.hpp>
  13. #include <boost/gil/point.hpp>
  14. #include <boost/gil/detail/mp11.hpp>
  15. #include <boost/variant.hpp>
  16. namespace boost { namespace gil {
  17. template <typename View>
  18. struct dynamic_xy_step_transposed_type;
  19. namespace detail {
  20. template <typename View>
  21. struct get_const_t { using type = typename View::const_t; };
  22. template <typename Views>
  23. struct views_get_const_t : mp11::mp_transform<get_const_t, Views> {};
  24. // works for both image_view and image
  25. struct any_type_get_num_channels
  26. {
  27. using result_type = int;
  28. template <typename T>
  29. result_type operator()(const T&) const { return num_channels<T>::value; }
  30. };
  31. // works for both image_view and image
  32. struct any_type_get_dimensions
  33. {
  34. using result_type = point<std::ptrdiff_t>;
  35. template <typename T>
  36. result_type operator()(const T& v) const { return v.dimensions(); }
  37. };
  38. // works for image_view
  39. struct any_type_get_size
  40. {
  41. using result_type = std::size_t;
  42. template <typename T>
  43. result_type operator()(const T& v) const { return v.size(); }
  44. };
  45. } // namespace detail
  46. ////////////////////////////////////////////////////////////////////////////////////////
  47. /// CLASS any_image_view
  48. ///
  49. /// \ingroup ImageViewModel
  50. /// \brief Represents a run-time specified image view. Models HasDynamicXStepTypeConcept, HasDynamicYStepTypeConcept, Note that this class does NOT model ImageViewConcept
  51. ///
  52. /// Represents a view whose type (color space, layout, planar/interleaved organization, etc) can be specified at run time.
  53. /// It is the runtime equivalent of \p image_view.
  54. /// Some of the requirements of ImageViewConcept, such as the \p value_type alias cannot be fulfilled, since the language does not allow runtime type specification.
  55. /// Other requirements, such as access to the pixels, would be inefficient to provide. Thus \p any_image_view does not fully model ImageViewConcept.
  56. /// However, many algorithms provide overloads taking runtime specified views and thus in many cases \p any_image_view can be used in places taking a view.
  57. ///
  58. /// To perform an algorithm on any_image_view, put the algorithm in a function object and invoke it by calling \p apply_operation(runtime_view, algorithm_fn);
  59. ////////////////////////////////////////////////////////////////////////////////////////
  60. template <typename Views>
  61. class any_image_view : public make_variant_over<Views>::type
  62. {
  63. using parent_t = typename make_variant_over<Views>::type;
  64. public:
  65. using const_t = any_image_view<detail::views_get_const_t<Views>>;
  66. using x_coord_t = std::ptrdiff_t;
  67. using y_coord_t = std::ptrdiff_t;
  68. using point_t = point<std::ptrdiff_t>;
  69. using size_type = std::size_t;
  70. any_image_view() = default;
  71. any_image_view(any_image_view const& view) : parent_t((parent_t const&)view) {}
  72. template <typename View>
  73. explicit any_image_view(View const& view) : parent_t(view) {}
  74. template <typename OtherViews>
  75. any_image_view(any_image_view<OtherViews> const& view)
  76. : parent_t((typename make_variant_over<OtherViews>::type const&)view)
  77. {}
  78. any_image_view& operator=(any_image_view const& view)
  79. {
  80. parent_t::operator=((parent_t const&)view);
  81. return *this;
  82. }
  83. template <typename View>
  84. any_image_view& operator=(View const& view)
  85. {
  86. parent_t::operator=(view);
  87. return *this;
  88. }
  89. template <typename OtherViews>
  90. any_image_view& operator=(any_image_view<OtherViews> const& view)
  91. {
  92. parent_t::operator=((typename make_variant_over<OtherViews>::type const&)view);
  93. return *this;
  94. }
  95. std::size_t num_channels() const { return apply_operation(*this, detail::any_type_get_num_channels()); }
  96. point_t dimensions() const { return apply_operation(*this, detail::any_type_get_dimensions()); }
  97. size_type size() const { return apply_operation(*this, detail::any_type_get_size()); }
  98. x_coord_t width() const { return dimensions().x; }
  99. y_coord_t height() const { return dimensions().y; }
  100. };
  101. /////////////////////////////
  102. // HasDynamicXStepTypeConcept
  103. /////////////////////////////
  104. template <typename Views>
  105. struct dynamic_x_step_type<any_image_view<Views>>
  106. {
  107. private:
  108. // FIXME: Remove class name injection with gil:: qualification
  109. // Required as workaround for Boost.MP11 issue that treats unqualified metafunction
  110. // in the class definition of the same name as the specialization (Peter Dimov):
  111. // invalid template argument for template parameter 'F', expected a class template
  112. template <typename T>
  113. using dynamic_step_view = typename gil::dynamic_x_step_type<T>::type;
  114. public:
  115. using type = any_image_view<mp11::mp_transform<dynamic_step_view, Views>>;
  116. };
  117. /////////////////////////////
  118. // HasDynamicYStepTypeConcept
  119. /////////////////////////////
  120. template <typename Views>
  121. struct dynamic_y_step_type<any_image_view<Views>>
  122. {
  123. private:
  124. // FIXME: Remove class name injection with gil:: qualification
  125. // Required as workaround for Boost.MP11 issue that treats unqualified metafunction
  126. // in the class definition of the same name as the specialization (Peter Dimov):
  127. // invalid template argument for template parameter 'F', expected a class template
  128. template <typename T>
  129. using dynamic_step_view = typename gil::dynamic_y_step_type<T>::type;
  130. public:
  131. using type = any_image_view<mp11::mp_transform<dynamic_step_view, Views>>;
  132. };
  133. template <typename Views>
  134. struct dynamic_xy_step_type<any_image_view<Views>>
  135. {
  136. private:
  137. // FIXME: Remove class name injection with gil:: qualification
  138. // Required as workaround for Boost.MP11 issue that treats unqualified metafunction
  139. // in the class definition of the same name as the specialization (Peter Dimov):
  140. // invalid template argument for template parameter 'F', expected a class template
  141. template <typename T>
  142. using dynamic_step_view = typename gil::dynamic_xy_step_type<T>::type;
  143. public:
  144. using type = any_image_view<mp11::mp_transform<dynamic_step_view, Views>>;
  145. };
  146. template <typename Views>
  147. struct dynamic_xy_step_transposed_type<any_image_view<Views>>
  148. {
  149. private:
  150. // FIXME: Remove class name injection with gil:: qualification
  151. // Required as workaround for Boost.MP11 issue that treats unqualified metafunction
  152. // in the class definition of the same name as the specialization (Peter Dimov):
  153. // invalid template argument for template parameter 'F', expected a class template
  154. template <typename T>
  155. using dynamic_step_view = typename gil::dynamic_xy_step_type<T>::type;
  156. public:
  157. using type = any_image_view<mp11::mp_transform<dynamic_step_view, Views>>;
  158. };
  159. }} // namespace boost::gil
  160. #endif