metafunctions.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. Copyright 2005-2007 Adobe Systems Incorporated
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. See http://opensource.adobe.com/gil for most recent version including documentation.
  7. */
  8. /*************************************************************************************************/
  9. #ifndef GIL_METAFUNCTIONS_HPP
  10. #define GIL_METAFUNCTIONS_HPP
  11. ////////////////////////////////////////////////////////////////////////////////////////
  12. /// \file
  13. /// \brief metafunctions that construct types or return type properties
  14. /// \author Lubomir Bourdev and Hailin Jin \n
  15. /// Adobe Systems Incorporated
  16. ///
  17. /// \date 2005-2007 \n Last updated on February 6, 2007
  18. ///
  19. ////////////////////////////////////////////////////////////////////////////////////////
  20. #include <iterator>
  21. #include <boost/mpl/accumulate.hpp>
  22. #include <boost/mpl/back.hpp>
  23. #include <boost/mpl/bool.hpp>
  24. #include <boost/mpl/if.hpp>
  25. #include <boost/mpl/pop_back.hpp>
  26. #include <boost/mpl/push_back.hpp>
  27. #include <boost/mpl/transform.hpp>
  28. #include <boost/mpl/vector.hpp>
  29. #include <boost/type_traits.hpp>
  30. #include "gil_config.hpp"
  31. #include "gil_concept.hpp"
  32. #include "channel.hpp"
  33. namespace boost { namespace gil {
  34. // forward declarations
  35. template <typename T, typename L> struct pixel;
  36. template <typename BitField,typename ChannelRefVec,typename Layout> struct packed_pixel;
  37. template <typename T, typename C> struct planar_pixel_reference;
  38. template <typename IC, typename C> struct planar_pixel_iterator;
  39. template <typename I> class memory_based_step_iterator;
  40. template <typename I> class memory_based_2d_locator;
  41. template <typename L> class image_view;
  42. template <typename Pixel, bool IsPlanar, typename Alloc> class image;
  43. template <typename T> struct channel_type;
  44. template <typename T> struct color_space_type;
  45. template <typename T> struct channel_mapping_type;
  46. template <typename It> struct is_iterator_adaptor;
  47. template <typename It> struct iterator_adaptor_get_base;
  48. template <typename BitField, typename ChannelBitSizes, typename Layout, bool IsMutable> struct bit_aligned_pixel_reference;
  49. //////////////////////////////////////////////////
  50. ///
  51. /// TYPE ANALYSIS METAFUNCTIONS
  52. /// Predicate metafunctions determining properties of GIL types
  53. ///
  54. //////////////////////////////////////////////////
  55. /// \defgroup GILIsBasic xxx_is_basic
  56. /// \ingroup TypeAnalysis
  57. /// \brief Determines if GIL constructs are basic.
  58. /// Basic constructs are the ones that can be generated with the type
  59. /// factory methods pixel_reference_type, iterator_type, locator_type, view_type and image_type
  60. /// They can be mutable/immutable, planar/interleaved, step/nonstep. They must use GIL-provided models.
  61. /// \brief Determines if a given pixel reference is basic
  62. /// Basic references must use gil::pixel& (if interleaved), gil::planar_pixel_reference (if planar). They must use the standard constness rules.
  63. /// \ingroup GILIsBasic
  64. template <typename PixelRef> struct pixel_reference_is_basic : public mpl::false_ {};
  65. template <typename T, typename L> struct pixel_reference_is_basic< pixel<T,L>&> : public mpl::true_ {};
  66. template <typename T, typename L> struct pixel_reference_is_basic<const pixel<T,L>&> : public mpl::true_ {};
  67. template <typename TR, typename Cs> struct pixel_reference_is_basic<planar_pixel_reference<TR,Cs> > : public mpl::true_ {};
  68. template <typename TR, typename Cs> struct pixel_reference_is_basic<const planar_pixel_reference<TR,Cs> > : public mpl::true_ {};
  69. /// \brief Determines if a given pixel iterator is basic
  70. /// Basic iterators must use gil::pixel (if interleaved), gil::planar_pixel_iterator (if planar) and gil::memory_based_step_iterator (if step). They must use the standard constness rules.
  71. /// \ingroup GILIsBasic
  72. template <typename Iterator>
  73. struct iterator_is_basic : public mpl::false_ {};
  74. template <typename T, typename L> // mutable interleaved
  75. struct iterator_is_basic< pixel<T,L>* > : public mpl::true_ {};
  76. template <typename T, typename L> // immutable interleaved
  77. struct iterator_is_basic<const pixel<T,L>* > : public mpl::true_ {};
  78. template <typename T, typename Cs> // mutable planar
  79. struct iterator_is_basic<planar_pixel_iterator< T*,Cs> > : public mpl::true_ {};
  80. template <typename T, typename Cs> // immutable planar
  81. struct iterator_is_basic<planar_pixel_iterator<const T*,Cs> > : public mpl::true_ {};
  82. template <typename T, typename L> // mutable interleaved step
  83. struct iterator_is_basic<memory_based_step_iterator< pixel<T,L>*> > : public mpl::true_ {};
  84. template <typename T, typename L> // immutable interleaved step
  85. struct iterator_is_basic<memory_based_step_iterator<const pixel<T,L>*> > : public mpl::true_ {};
  86. template <typename T, typename Cs> // mutable planar step
  87. struct iterator_is_basic<memory_based_step_iterator<planar_pixel_iterator< T*,Cs> > > : public mpl::true_ {};
  88. template <typename T, typename Cs> // immutable planar step
  89. struct iterator_is_basic<memory_based_step_iterator<planar_pixel_iterator<const T*,Cs> > > : public mpl::true_ {};
  90. /// \ingroup GILIsBasic
  91. /// \brief Determines if a given locator is basic. A basic locator is memory-based and has basic x_iterator and y_iterator
  92. template <typename Loc> struct locator_is_basic : public mpl::false_ {};
  93. template <typename Iterator> struct locator_is_basic<memory_based_2d_locator<memory_based_step_iterator<Iterator> > > : public iterator_is_basic<Iterator> {};
  94. /// \ingroup GILIsBasic
  95. /// \brief Basic views must be over basic locators
  96. template <typename View> struct view_is_basic : public mpl::false_ {};
  97. template <typename Loc> struct view_is_basic<image_view<Loc> > : public locator_is_basic<Loc> {};
  98. /// \ingroup GILIsBasic
  99. /// \brief Basic images must use basic views and std::allocator of char
  100. template <typename Img> struct image_is_basic : public mpl::false_ {};
  101. template <typename Pixel, bool IsPlanar, typename Alloc> struct image_is_basic<image<Pixel,IsPlanar,Alloc> > : public mpl::true_ {};
  102. /// \defgroup GILIsStep xxx_is_step
  103. /// \ingroup TypeAnalysis
  104. /// \brief Determines if the given iterator/locator/view has a step that could be set dynamically
  105. template <typename I> struct iterator_is_step;
  106. namespace detail {
  107. template <typename It, bool IsBase, bool EqualsStepType> struct iterator_is_step_impl;
  108. // iterator that has the same type as its dynamic_x_step_type must be a step iterator
  109. template <typename It, bool IsBase> struct iterator_is_step_impl<It,IsBase,true> : public mpl::true_{};
  110. // base iterator can never be a step iterator
  111. template <typename It> struct iterator_is_step_impl<It,true,false> : public mpl::false_{};
  112. // for an iterator adaptor, see if its base is step
  113. template <typename It> struct iterator_is_step_impl<It,false,false>
  114. : public iterator_is_step<typename iterator_adaptor_get_base<It>::type>{};
  115. }
  116. /// \ingroup GILIsStep
  117. /// \brief Determines if the given iterator has a step that could be set dynamically
  118. template <typename I> struct iterator_is_step
  119. : public detail::iterator_is_step_impl<I,
  120. !is_iterator_adaptor<I>::type::value,
  121. is_same<I,typename dynamic_x_step_type<I>::type>::value >{};
  122. /// \ingroup GILIsStep
  123. /// \brief Determines if the given locator has a horizontal step that could be set dynamically
  124. template <typename L> struct locator_is_step_in_x : public iterator_is_step<typename L::x_iterator> {};
  125. /// \ingroup GILIsStep
  126. /// \brief Determines if the given locator has a vertical step that could be set dynamically
  127. template <typename L> struct locator_is_step_in_y : public iterator_is_step<typename L::y_iterator> {};
  128. /// \ingroup GILIsStep
  129. /// \brief Determines if the given view has a horizontal step that could be set dynamically
  130. template <typename V> struct view_is_step_in_x : public locator_is_step_in_x<typename V::xy_locator> {};
  131. /// \ingroup GILIsStep
  132. /// \brief Determines if the given view has a vertical step that could be set dynamically
  133. template <typename V> struct view_is_step_in_y : public locator_is_step_in_y<typename V::xy_locator> {};
  134. /// \brief Determines whether the given pixel reference is a proxy class or a native C++ reference
  135. /// \ingroup TypeAnalysis
  136. template <typename PixelReference>
  137. struct pixel_reference_is_proxy
  138. : public mpl::not_<is_same<typename remove_const_and_reference<PixelReference>::type,
  139. typename remove_const_and_reference<PixelReference>::type::value_type> > {};
  140. /// \brief Given a model of a pixel, determines whether the model represents a pixel reference (as opposed to pixel value)
  141. /// \ingroup TypeAnalysis
  142. template <typename Pixel>
  143. struct pixel_is_reference : public mpl::or_<is_reference<Pixel>, pixel_reference_is_proxy<Pixel> > {};
  144. /// \defgroup GILIsMutable xxx_is_mutable
  145. /// \ingroup TypeAnalysis
  146. /// \brief Determines if the given pixel reference/iterator/locator/view is mutable (i.e. its pixels can be changed)
  147. /// \ingroup GILIsMutable
  148. /// \brief Determines if the given pixel reference is mutable (i.e. its channels can be changed)
  149. ///
  150. /// Note that built-in C++ references obey the const qualifier but reference proxy classes do not.
  151. template <typename R> struct pixel_reference_is_mutable : public mpl::bool_<remove_reference<R>::type::is_mutable> {};
  152. template <typename R> struct pixel_reference_is_mutable<const R&>
  153. : public mpl::and_<pixel_reference_is_proxy<R>, pixel_reference_is_mutable<R> > {};
  154. /// \ingroup GILIsMutable
  155. /// \brief Determines if the given locator is mutable (i.e. its pixels can be changed)
  156. template <typename L> struct locator_is_mutable : public iterator_is_mutable<typename L::x_iterator> {};
  157. /// \ingroup GILIsMutable
  158. /// \brief Determines if the given view is mutable (i.e. its pixels can be changed)
  159. template <typename V> struct view_is_mutable : public iterator_is_mutable<typename V::x_iterator> {};
  160. //////////////////////////////////////////////////
  161. ///
  162. /// TYPE FACTORY METAFUNCTIONS
  163. /// Metafunctions returning GIL types from other GIL types
  164. ///
  165. //////////////////////////////////////////////////
  166. /// \defgroup TypeFactoryFromElements xxx_type
  167. /// \ingroup TypeFactory
  168. /// \brief Returns the type of a homogeneous GIL construct given its elements (channel, layout, whether it is planar, step, mutable, etc.)
  169. /// \defgroup TypeFactoryFromPixel xxx_type_from_pixel
  170. /// \ingroup TypeFactory
  171. /// \brief Returns the type of a GIL construct given its pixel type, whether it is planar, step, mutable, etc.
  172. /// \defgroup TypeFactoryDerived derived_xxx_type
  173. /// \ingroup TypeFactory
  174. /// \brief Returns the type of a homogeneous GIL construct given a related construct by changing some of its properties
  175. /// \ingroup TypeFactoryFromElements
  176. /// \brief Returns the type of a homogeneous pixel reference given the channel type, layout, whether it operates on planar data and whether it is mutable
  177. template <typename T, typename L, bool IsPlanar=false, bool IsMutable=true> struct pixel_reference_type{};
  178. template <typename T, typename L> struct pixel_reference_type<T,L,false,true > { typedef pixel<T,L>& type; };
  179. template <typename T, typename L> struct pixel_reference_type<T,L,false,false> { typedef const pixel<T,L>& type; };
  180. template <typename T, typename L> struct pixel_reference_type<T,L,true,true> { typedef const planar_pixel_reference<typename channel_traits<T>::reference,typename color_space_type<L>::type> type; }; // TODO: Assert M=identity
  181. template <typename T, typename L> struct pixel_reference_type<T,L,true,false> { typedef const planar_pixel_reference<typename channel_traits<T>::const_reference,typename color_space_type<L>::type> type; };// TODO: Assert M=identity
  182. /// \ingroup TypeFactoryFromPixel
  183. /// \brief Returns the type of a pixel iterator given the pixel type, whether it operates on planar data, whether it is a step iterator, and whether it is mutable
  184. template <typename Pixel, bool IsPlanar=false, bool IsStep=false, bool IsMutable=true> struct iterator_type_from_pixel{};
  185. template <typename Pixel> struct iterator_type_from_pixel<Pixel,false,false,true > { typedef Pixel* type; };
  186. template <typename Pixel> struct iterator_type_from_pixel<Pixel,false,false,false> { typedef const Pixel* type; };
  187. template <typename Pixel> struct iterator_type_from_pixel<Pixel,true,false,true> {
  188. typedef planar_pixel_iterator<typename channel_traits<typename channel_type<Pixel>::type>::pointer,typename color_space_type<Pixel>::type> type;
  189. };
  190. template <typename Pixel> struct iterator_type_from_pixel<Pixel,true,false,false> {
  191. typedef planar_pixel_iterator<typename channel_traits<typename channel_type<Pixel>::type>::const_pointer,typename color_space_type<Pixel>::type> type;
  192. };
  193. template <typename Pixel, bool IsPlanar, bool IsMutable> struct iterator_type_from_pixel<Pixel,IsPlanar,true,IsMutable> {
  194. typedef memory_based_step_iterator<typename iterator_type_from_pixel<Pixel,IsPlanar,false,IsMutable>::type> type;
  195. };
  196. /// \ingroup TypeFactoryFromElements
  197. /// \brief Returns the type of a homogeneous iterator given the channel type, layout, whether it operates on planar data, whether it is a step iterator, and whether it is mutable
  198. template <typename T, typename L, bool IsPlanar=false, bool IsStep=false, bool IsMutable=true> struct iterator_type{};
  199. template <typename T, typename L> struct iterator_type<T,L,false,false,true > { typedef pixel<T,L>* type; };
  200. template <typename T, typename L> struct iterator_type<T,L,false,false,false> { typedef const pixel<T,L>* type; };
  201. template <typename T, typename L> struct iterator_type<T,L,true,false,true> { typedef planar_pixel_iterator<T*,typename L::color_space_t> type; }; // TODO: Assert M=identity
  202. template <typename T, typename L> struct iterator_type<T,L,true,false,false> { typedef planar_pixel_iterator<const T*,typename L::color_space_t> type; }; // TODO: Assert M=identity
  203. template <typename T, typename L, bool IsPlanar, bool IsMutable> struct iterator_type<T,L,IsPlanar,true,IsMutable> {
  204. typedef memory_based_step_iterator<typename iterator_type<T,L,IsPlanar,false,IsMutable>::type> type;
  205. };
  206. /// \brief Given a pixel iterator defining access to pixels along a row, returns the types of the corresponding built-in step_iterator, xy_locator, image_view
  207. /// \ingroup TypeFactory
  208. template <typename XIterator>
  209. struct type_from_x_iterator {
  210. typedef memory_based_step_iterator<XIterator> step_iterator_t;
  211. typedef memory_based_2d_locator<step_iterator_t> xy_locator_t;
  212. typedef image_view<xy_locator_t> view_t;
  213. };
  214. namespace detail {
  215. template <typename BitField, typename FirstBit, typename NumBits>
  216. struct packed_channel_reference_type {
  217. typedef const packed_channel_reference<BitField,FirstBit::value,NumBits::value,true> type;
  218. };
  219. template <typename BitField, typename ChannelBitSizesVector>
  220. class packed_channel_references_vector_type {
  221. // If ChannelBitSizesVector is mpl::vector<int,7,7,2>
  222. // Then first_bits_vector will be mpl::vector<int,0,7,14,16>
  223. typedef typename mpl::accumulate<ChannelBitSizesVector, mpl::vector1<mpl::int_<0> >,
  224. mpl::push_back<mpl::_1, mpl::plus<mpl::back<mpl::_1>, mpl::_2> > >::type first_bits_vector;
  225. public:
  226. typedef typename mpl::transform<typename mpl::pop_back<first_bits_vector>::type, ChannelBitSizesVector,
  227. packed_channel_reference_type<BitField, mpl::_1,mpl::_2> >::type type;
  228. };
  229. }
  230. /// \ingroup TypeFactoryFromElements
  231. /// \brief Returns the type of a packed pixel given its bitfield type, the bit size of its channels and its layout.
  232. ///
  233. /// A packed pixel has channels that cover bit ranges but itself is byte aligned. RGB565 pixel is an example.
  234. ///
  235. /// The size of ChannelBitSizeVector must equal the number of channels in the given layout
  236. /// The sum of bit sizes for all channels must be less than or equal to the number of bits in BitField (and cannot exceed 64).
  237. /// If it is less than the number of bits in BitField, the last bits will be unused.
  238. template <typename BitField, typename ChannelBitSizeVector, typename Layout>
  239. struct packed_pixel_type {
  240. typedef packed_pixel<BitField, typename detail::packed_channel_references_vector_type<BitField,ChannelBitSizeVector>::type, Layout> type;
  241. };
  242. /// \defgroup TypeFactoryPacked packed_image_type,bit_aligned_image_type
  243. /// \ingroup TypeFactoryFromElements
  244. /// \brief Returns the type of an image whose channels are not byte-aligned.
  245. ///
  246. /// A packed image is an image whose pixels are byte aligned, such as "rgb565". <br>
  247. /// A bit-aligned image is an image whose pixels are not byte aligned, such as "rgb222". <br>
  248. ///
  249. /// The sum of the bit sizes of all channels cannot exceed 64.
  250. /// \ingroup TypeFactoryPacked
  251. /// \brief Returns the type of an interleaved packed image: an image whose channels may not be byte-aligned, but whose pixels are byte aligned.
  252. template <typename BitField, typename ChannelBitSizeVector, typename Layout, typename Alloc=std::allocator<unsigned char> >
  253. struct packed_image_type {
  254. typedef image<typename packed_pixel_type<BitField,ChannelBitSizeVector,Layout>::type,false,Alloc> type;
  255. };
  256. /// \ingroup TypeFactoryPacked
  257. /// \brief Returns the type of a single-channel image given its bitfield type, the bit size of its channel and its layout
  258. template <typename BitField, unsigned Size1, typename Layout, typename Alloc=std::allocator<unsigned char> >
  259. struct packed_image1_type : public packed_image_type<BitField, mpl::vector1_c<unsigned, Size1>, Layout, Alloc> {};
  260. /// \ingroup TypeFactoryPacked
  261. /// \brief Returns the type of a two channel image given its bitfield type, the bit size of its channels and its layout
  262. template <typename BitField, unsigned Size1, unsigned Size2, typename Layout, typename Alloc=std::allocator<unsigned char> >
  263. struct packed_image2_type : public packed_image_type<BitField, mpl::vector2_c<unsigned, Size1, Size2>, Layout, Alloc> {};
  264. /// \ingroup TypeFactoryPacked
  265. /// \brief Returns the type of a three channel image given its bitfield type, the bit size of its channels and its layout
  266. template <typename BitField, unsigned Size1, unsigned Size2, unsigned Size3, typename Layout, typename Alloc=std::allocator<unsigned char> >
  267. struct packed_image3_type : public packed_image_type<BitField, mpl::vector3_c<unsigned, Size1, Size2, Size3>, Layout, Alloc> {};
  268. /// \ingroup TypeFactoryPacked
  269. /// \brief Returns the type of a four channel image given its bitfield type, the bit size of its channels and its layout
  270. template <typename BitField, unsigned Size1, unsigned Size2, unsigned Size3, unsigned Size4, typename Layout, typename Alloc=std::allocator<unsigned char> >
  271. struct packed_image4_type : public packed_image_type<BitField, mpl::vector4_c<unsigned, Size1, Size2, Size3, Size4>, Layout, Alloc> {};
  272. /// \ingroup TypeFactoryPacked
  273. /// \brief Returns the type of a five channel image given its bitfield type, the bit size of its channels and its layout
  274. template <typename BitField, unsigned Size1, unsigned Size2, unsigned Size3, unsigned Size4, unsigned Size5, typename Layout, typename Alloc=std::allocator<unsigned char> >
  275. struct packed_image5_type : public packed_image_type<BitField, mpl::vector5_c<unsigned, Size1, Size2, Size3, Size4, Size5>, Layout, Alloc> {};
  276. /// \ingroup TypeFactoryPacked
  277. /// \brief Returns the type of a packed image whose pixels may not be byte aligned. For example, an "rgb222" image is bit-aligned because its pixel spans six bits.
  278. ///
  279. /// Note that the alignment parameter in the constructor of bit-aligned images is in bit units. For example, if you want to construct a bit-aligned
  280. /// image whose rows are byte-aligned, use 8 as the alignment parameter, not 1.
  281. template <typename ChannelBitSizeVector, typename Layout, typename Alloc=std::allocator<unsigned char> >
  282. struct bit_aligned_image_type {
  283. private:
  284. BOOST_STATIC_CONSTANT(int, bit_size = (mpl::accumulate<ChannelBitSizeVector, mpl::int_<0>, mpl::plus<mpl::_1, mpl::_2> >::type::value));
  285. typedef typename detail::min_fast_uint<bit_size+7>::type bitfield_t;
  286. typedef const bit_aligned_pixel_reference<bitfield_t, ChannelBitSizeVector, Layout, true> bit_alignedref_t;
  287. public:
  288. typedef image<bit_alignedref_t,false,Alloc> type;
  289. };
  290. /// \ingroup TypeFactoryPacked
  291. /// \brief Returns the type of a single-channel bit-aligned image given the bit size of its channel and its layout
  292. template <unsigned Size1, typename Layout, typename Alloc=std::allocator<unsigned char> >
  293. struct bit_aligned_image1_type : public bit_aligned_image_type<mpl::vector1_c<unsigned, Size1>, Layout, Alloc> {};
  294. /// \ingroup TypeFactoryPacked
  295. /// \brief Returns the type of a two channel bit-aligned image given the bit size of its channels and its layout
  296. template <unsigned Size1, unsigned Size2, typename Layout, typename Alloc=std::allocator<unsigned char> >
  297. struct bit_aligned_image2_type : public bit_aligned_image_type<mpl::vector2_c<unsigned, Size1, Size2>, Layout, Alloc> {};
  298. /// \ingroup TypeFactoryPacked
  299. /// \brief Returns the type of a three channel bit-aligned image given the bit size of its channels and its layout
  300. template <unsigned Size1, unsigned Size2, unsigned Size3, typename Layout, typename Alloc=std::allocator<unsigned char> >
  301. struct bit_aligned_image3_type : public bit_aligned_image_type<mpl::vector3_c<unsigned, Size1, Size2, Size3>, Layout, Alloc> {};
  302. /// \ingroup TypeFactoryPacked
  303. /// \brief Returns the type of a four channel bit-aligned image given the bit size of its channels and its layout
  304. template <unsigned Size1, unsigned Size2, unsigned Size3, unsigned Size4, typename Layout, typename Alloc=std::allocator<unsigned char> >
  305. struct bit_aligned_image4_type : public bit_aligned_image_type<mpl::vector4_c<unsigned, Size1, Size2, Size3, Size4>, Layout, Alloc> {};
  306. /// \ingroup TypeFactoryPacked
  307. /// \brief Returns the type of a five channel bit-aligned image given the bit size of its channels and its layout
  308. template <unsigned Size1, unsigned Size2, unsigned Size3, unsigned Size4, unsigned Size5, typename Layout, typename Alloc=std::allocator<unsigned char> >
  309. struct bit_aligned_image5_type : public bit_aligned_image_type<mpl::vector5_c<unsigned, Size1, Size2, Size3, Size4, Size5>, Layout, Alloc> {};
  310. /// \ingroup TypeFactoryFromElements
  311. /// \brief Returns the type of a homogeneous pixel given the channel type and layout
  312. template <typename Channel, typename Layout>
  313. struct pixel_value_type {
  314. typedef pixel<Channel,Layout> type; // by default use gil::pixel. Specializations are provided for
  315. };
  316. // Specializations for packed channels
  317. template <typename BitField, int NumBits, bool IsMutable, typename Layout>
  318. struct pixel_value_type< packed_dynamic_channel_reference<BitField,NumBits,IsMutable>,Layout> :
  319. public packed_pixel_type<BitField, mpl::vector1_c<unsigned,NumBits>, Layout> {};
  320. template <typename BitField, int NumBits, bool IsMutable, typename Layout>
  321. struct pixel_value_type<const packed_dynamic_channel_reference<BitField,NumBits,IsMutable>,Layout> :
  322. public packed_pixel_type<BitField, mpl::vector1_c<unsigned,NumBits>, Layout> {};
  323. template <typename BitField, int FirstBit, int NumBits, bool IsMutable, typename Layout>
  324. struct pixel_value_type< packed_channel_reference<BitField,FirstBit,NumBits,IsMutable>,Layout> :
  325. public packed_pixel_type<BitField, mpl::vector1_c<unsigned,NumBits>, Layout> {};
  326. template <typename BitField, int FirstBit, int NumBits, bool IsMutable, typename Layout>
  327. struct pixel_value_type<const packed_channel_reference<BitField,FirstBit,NumBits,IsMutable>,Layout> :
  328. public packed_pixel_type<BitField, mpl::vector1_c<unsigned,NumBits>, Layout> {};
  329. template <int NumBits, typename Layout>
  330. struct pixel_value_type<packed_channel_value<NumBits>,Layout> :
  331. public packed_pixel_type<typename detail::min_fast_uint<NumBits>::type, mpl::vector1_c<unsigned,NumBits>, Layout> {};
  332. /// \ingroup TypeFactoryFromElements
  333. /// \brief Returns the type of a homogeneous locator given the channel type, layout, whether it operates on planar data and whether it has a step horizontally
  334. template <typename T, typename L, bool IsPlanar=false, bool IsStepX=false, bool IsMutable=true>
  335. struct locator_type {
  336. typedef typename type_from_x_iterator<typename iterator_type<T,L,IsPlanar,IsStepX,IsMutable>::type>::xy_locator_type type;
  337. };
  338. /// \ingroup TypeFactoryFromElements
  339. /// \brief Returns the type of a homogeneous view given the channel type, layout, whether it operates on planar data and whether it has a step horizontally
  340. template <typename T, typename L, bool IsPlanar=false, bool IsStepX=false, bool IsMutable=true>
  341. struct view_type {
  342. typedef typename type_from_x_iterator<typename iterator_type<T,L,IsPlanar,IsStepX,IsMutable>::type>::view_t type;
  343. };
  344. /// \ingroup TypeFactoryFromElements
  345. /// \brief Returns the type of a homogeneous image given the channel type, layout, and whether it operates on planar data
  346. template <typename T, typename L, bool IsPlanar=false, typename Alloc=std::allocator<unsigned char> >
  347. struct image_type {
  348. typedef image<pixel<T,L>, IsPlanar, Alloc> type;
  349. };
  350. /// \ingroup TypeFactoryFromPixel
  351. /// \brief Returns the type of a view the pixel type, whether it operates on planar data and whether it has a step horizontally
  352. template <typename Pixel, bool IsPlanar=false, bool IsStepX=false, bool IsMutable=true>
  353. struct view_type_from_pixel {
  354. typedef typename type_from_x_iterator<typename iterator_type_from_pixel<Pixel,IsPlanar,IsStepX,IsMutable>::type>::view_t type;
  355. };
  356. /// \brief Constructs a pixel reference type from a source pixel reference type by changing some of the properties.
  357. /// \ingroup TypeFactoryDerived
  358. /// Use use_default for the properties of the source view that you want to keep
  359. template <typename Ref, typename T=use_default, typename L=use_default, typename IsPlanar=use_default, typename IsMutable=use_default>
  360. class derived_pixel_reference_type {
  361. typedef typename remove_reference<Ref>::type pixel_t;
  362. typedef typename mpl::if_<is_same<T, use_default>, typename channel_type<pixel_t>::type, T >::type channel_t;
  363. typedef typename mpl::if_<is_same<L, use_default>,
  364. layout<typename color_space_type<pixel_t>::type, typename channel_mapping_type<pixel_t>::type>, L>::type layout_t;
  365. static const bool mut =mpl::if_<is_same<IsMutable,use_default>, pixel_reference_is_mutable<Ref>, IsMutable>::type::value;
  366. static const bool planar=mpl::if_<is_same<IsPlanar,use_default>, is_planar<pixel_t>, IsPlanar>::type::value;
  367. public:
  368. typedef typename pixel_reference_type<channel_t, layout_t, planar, mut>::type type;
  369. };
  370. /// \brief Constructs a pixel iterator type from a source pixel iterator type by changing some of the properties.
  371. /// \ingroup TypeFactoryDerived
  372. /// Use use_default for the properties of the source view that you want to keep
  373. template <typename Iterator, typename T=use_default, typename L=use_default, typename IsPlanar=use_default, typename IsStep=use_default, typename IsMutable=use_default>
  374. class derived_iterator_type {
  375. typedef typename mpl::if_<is_same<T ,use_default>, typename channel_type<Iterator>::type, T >::type channel_t;
  376. typedef typename mpl::if_<is_same<L,use_default>,
  377. layout<typename color_space_type<Iterator>::type, typename channel_mapping_type<Iterator>::type>, L>::type layout_t;
  378. static const bool mut =mpl::if_<is_same<IsMutable,use_default>, iterator_is_mutable<Iterator>, IsMutable>::type::value;
  379. static const bool planar=mpl::if_<is_same<IsPlanar,use_default>, is_planar<Iterator>, IsPlanar>::type::value;
  380. static const bool step =mpl::if_<is_same<IsStep ,use_default>, iterator_is_step<Iterator>, IsStep>::type::value;
  381. public:
  382. typedef typename iterator_type<channel_t, layout_t, planar, step, mut>::type type;
  383. };
  384. /// \brief Constructs an image view type from a source view type by changing some of the properties.
  385. /// \ingroup TypeFactoryDerived
  386. /// Use use_default for the properties of the source view that you want to keep
  387. template <typename View, typename T=use_default, typename L=use_default, typename IsPlanar=use_default, typename StepX=use_default, typename IsMutable=use_default>
  388. class derived_view_type {
  389. typedef typename mpl::if_<is_same<T ,use_default>, typename channel_type<View>::type, T>::type channel_t;
  390. typedef typename mpl::if_<is_same<L,use_default>,
  391. layout<typename color_space_type<View>::type, typename channel_mapping_type<View>::type>, L>::type layout_t;
  392. static const bool mut =mpl::if_<is_same<IsMutable,use_default>, view_is_mutable<View>, IsMutable>::type::value;
  393. static const bool planar=mpl::if_<is_same<IsPlanar,use_default>, is_planar<View>, IsPlanar>::type::value;
  394. static const bool step =mpl::if_<is_same<StepX ,use_default>, view_is_step_in_x<View>,StepX>::type::value;
  395. public:
  396. typedef typename view_type<channel_t, layout_t, planar, step, mut>::type type;
  397. };
  398. /// \brief Constructs a homogeneous image type from a source image type by changing some of the properties.
  399. /// \ingroup TypeFactoryDerived
  400. /// Use use_default for the properties of the source image that you want to keep
  401. template <typename Image, typename T=use_default, typename L=use_default, typename IsPlanar=use_default>
  402. class derived_image_type {
  403. typedef typename mpl::if_<is_same<T ,use_default>, typename channel_type<Image>::type, T >::type channel_t;
  404. typedef typename mpl::if_<is_same<L,use_default>,
  405. layout<typename color_space_type<Image>::type, typename channel_mapping_type<Image>::type>, L>::type layout_t;
  406. static const bool planar=mpl::if_<is_same<IsPlanar,use_default>, is_planar<Image>, IsPlanar>::type::value;
  407. public:
  408. typedef typename image_type<channel_t, layout_t, planar>::type type;
  409. };
  410. } } // namespace boost::gil
  411. #endif