metafunctions.hpp 29 KB

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