metafunctions.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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_METAFUNCTIONS_HPP
  9. #define BOOST_GIL_METAFUNCTIONS_HPP
  10. #include <boost/gil/channel.hpp>
  11. #include <boost/gil/dynamic_step.hpp>
  12. #include <boost/gil/concepts.hpp>
  13. #include <boost/gil/concepts/detail/type_traits.hpp>
  14. #include <boost/gil/detail/mp11.hpp>
  15. #include <iterator>
  16. #include <type_traits>
  17. namespace boost { namespace gil {
  18. // forward declarations
  19. template <typename T, typename L> struct pixel;
  20. template <typename BitField,typename ChannelRefs,typename Layout> struct packed_pixel;
  21. template <typename T, typename C> struct planar_pixel_reference;
  22. template <typename IC, typename C> struct planar_pixel_iterator;
  23. template <typename I> class memory_based_step_iterator;
  24. template <typename I> class memory_based_2d_locator;
  25. template <typename L> class image_view;
  26. template <typename Pixel, bool IsPlanar, typename Alloc> class image;
  27. template <typename T> struct channel_type;
  28. template <typename T> struct color_space_type;
  29. template <typename T> struct channel_mapping_type;
  30. template <typename It> struct is_iterator_adaptor;
  31. template <typename It> struct iterator_adaptor_get_base;
  32. template <typename BitField, typename ChannelBitSizes, typename Layout, bool IsMutable> struct bit_aligned_pixel_reference;
  33. //////////////////////////////////////////////////
  34. ///
  35. /// TYPE ANALYSIS METAFUNCTIONS
  36. /// Predicate metafunctions determining properties of GIL types
  37. ///
  38. //////////////////////////////////////////////////
  39. /// \defgroup GILIsBasic xxx_is_basic
  40. /// \ingroup TypeAnalysis
  41. /// \brief Determines if GIL constructs are basic.
  42. /// Basic constructs are the ones that can be generated with the type
  43. /// factory methods pixel_reference_type, iterator_type, locator_type, view_type and image_type
  44. /// They can be mutable/immutable, planar/interleaved, step/nonstep. They must use GIL-provided models.
  45. /// \brief Determines if a given pixel reference is basic
  46. /// Basic references must use gil::pixel& (if interleaved), gil::planar_pixel_reference (if planar). They must use the standard constness rules.
  47. /// \ingroup GILIsBasic
  48. template <typename PixelRef>
  49. struct pixel_reference_is_basic : public std::false_type {};
  50. template <typename T, typename L>
  51. struct pixel_reference_is_basic<pixel<T, L>&> : std::true_type {};
  52. template <typename T, typename L>
  53. struct pixel_reference_is_basic<const pixel<T, L>&> : std::true_type {};
  54. template <typename TR, typename CS>
  55. struct pixel_reference_is_basic<planar_pixel_reference<TR, CS>> : std::true_type {};
  56. template <typename TR, typename CS>
  57. struct pixel_reference_is_basic<const planar_pixel_reference<TR, CS>> : std::true_type {};
  58. /// \brief Determines if a given pixel iterator is basic
  59. /// 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.
  60. /// \ingroup GILIsBasic
  61. template <typename Iterator>
  62. struct iterator_is_basic : std::false_type {};
  63. /// \tparam T mutable interleaved pixel type
  64. template <typename T, typename L>
  65. struct iterator_is_basic<pixel<T, L>*> : std::true_type {};
  66. /// \tparam T immutable interleaved pixel type
  67. template <typename T, typename L>
  68. struct iterator_is_basic<pixel<T, L> const*> : std::true_type {};
  69. /// \tparam T mutable planar pixel type
  70. template <typename T, typename CS>
  71. struct iterator_is_basic<planar_pixel_iterator<T*, CS>> : std::true_type {};
  72. /// \tparam T immutable planar pixel type
  73. template <typename T, typename CS>
  74. struct iterator_is_basic<planar_pixel_iterator<T const*, CS>> : std::true_type {};
  75. /// \tparam T mutable interleaved step
  76. template <typename T, typename L>
  77. struct iterator_is_basic<memory_based_step_iterator<pixel<T, L>*>> : std::true_type {};
  78. /// \tparam T immutable interleaved step
  79. template <typename T, typename L>
  80. struct iterator_is_basic<memory_based_step_iterator<pixel<T, L> const*>> : std::true_type {};
  81. /// \tparam T mutable planar step
  82. template <typename T, typename CS>
  83. struct iterator_is_basic<memory_based_step_iterator<planar_pixel_iterator<T*, CS>>>
  84. : std::true_type
  85. {};
  86. /// \tparam T immutable planar step
  87. template <typename T, typename CS>
  88. struct iterator_is_basic<memory_based_step_iterator<planar_pixel_iterator<T const*, CS>>>
  89. : std::true_type
  90. {};
  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>
  94. struct locator_is_basic : std::false_type {};
  95. template <typename Iterator>
  96. struct locator_is_basic
  97. <
  98. memory_based_2d_locator<memory_based_step_iterator<Iterator>>
  99. > : iterator_is_basic<Iterator>
  100. {};
  101. /// \ingroup GILIsBasic
  102. /// \brief Basic views must be over basic locators
  103. template <typename View>
  104. struct view_is_basic : std::false_type {};
  105. template <typename Loc>
  106. struct view_is_basic<image_view<Loc>> : locator_is_basic<Loc> {};
  107. /// \ingroup GILIsBasic
  108. /// \brief Basic images must use basic views and std::allocator
  109. template <typename Img>
  110. struct image_is_basic : std::false_type {};
  111. template <typename Pixel, bool IsPlanar, typename Alloc>
  112. struct image_is_basic<image<Pixel, IsPlanar, Alloc>> : std::true_type {};
  113. /// \defgroup GILIsStep xxx_is_step
  114. /// \ingroup TypeAnalysis
  115. /// \brief Determines if the given iterator/locator/view has a step that could be set dynamically
  116. template <typename I>
  117. struct iterator_is_step;
  118. namespace detail {
  119. template <typename It, bool IsBase, bool EqualsStepType>
  120. struct iterator_is_step_impl;
  121. // iterator that has the same type as its dynamic_x_step_type must be a step iterator
  122. template <typename It, bool IsBase>
  123. struct iterator_is_step_impl<It, IsBase, true> : std::true_type {};
  124. // base iterator can never be a step iterator
  125. template <typename It>
  126. struct iterator_is_step_impl<It, true, false> : std::false_type {};
  127. // for an iterator adaptor, see if its base is step
  128. template <typename It>
  129. struct iterator_is_step_impl<It, false, false>
  130. : public iterator_is_step<typename iterator_adaptor_get_base<It>::type> {};
  131. } // namespace detail
  132. /// \ingroup GILIsStep
  133. /// \brief Determines if the given iterator has a step that could be set dynamically
  134. template <typename I>
  135. struct iterator_is_step
  136. : detail::iterator_is_step_impl
  137. <
  138. I,
  139. !is_iterator_adaptor<I>::value,
  140. std::is_same<I, typename dynamic_x_step_type<I>::type
  141. >::value
  142. >
  143. {};
  144. /// \ingroup GILIsStep
  145. /// \brief Determines if the given locator has a horizontal step that could be set dynamically
  146. template <typename L> struct locator_is_step_in_x : public iterator_is_step<typename L::x_iterator> {};
  147. /// \ingroup GILIsStep
  148. /// \brief Determines if the given locator has a vertical step that could be set dynamically
  149. template <typename L> struct locator_is_step_in_y : public iterator_is_step<typename L::y_iterator> {};
  150. /// \ingroup GILIsStep
  151. /// \brief Determines if the given view has a horizontal step that could be set dynamically
  152. template <typename V> struct view_is_step_in_x : public locator_is_step_in_x<typename V::xy_locator> {};
  153. /// \ingroup GILIsStep
  154. /// \brief Determines if the given view has a vertical step that could be set dynamically
  155. template <typename V> struct view_is_step_in_y : public locator_is_step_in_y<typename V::xy_locator> {};
  156. /// \brief Determines whether the given pixel reference is a proxy class or a native C++ reference
  157. /// \ingroup TypeAnalysis
  158. template <typename PixelReference>
  159. struct pixel_reference_is_proxy
  160. : mp11::mp_not
  161. <
  162. std::is_same
  163. <
  164. typename detail::remove_const_and_reference<PixelReference>::type,
  165. typename detail::remove_const_and_reference<PixelReference>::type::value_type
  166. >
  167. >
  168. {};
  169. /// \brief Given a model of a pixel, determines whether the model represents a pixel reference (as opposed to pixel value)
  170. /// \ingroup TypeAnalysis
  171. template <typename Pixel>
  172. struct pixel_is_reference
  173. : mp11::mp_or<is_reference<Pixel>, pixel_reference_is_proxy<Pixel>> {};
  174. /// \defgroup GILIsMutable xxx_is_mutable
  175. /// \ingroup TypeAnalysis
  176. /// \brief Determines if the given pixel reference/iterator/locator/view is mutable (i.e. its pixels can be changed)
  177. /// \ingroup GILIsMutable
  178. /// \brief Determines if the given pixel reference is mutable (i.e. its channels can be changed)
  179. ///
  180. /// Note that built-in C++ references obey the const qualifier but reference proxy classes do not.
  181. template <typename R>
  182. struct pixel_reference_is_mutable
  183. : std::integral_constant<bool, std::remove_reference<R>::type::is_mutable>
  184. {};
  185. template <typename R>
  186. struct pixel_reference_is_mutable<R const&>
  187. : mp11::mp_and<pixel_reference_is_proxy<R>, pixel_reference_is_mutable<R>>
  188. {};
  189. /// \ingroup GILIsMutable
  190. /// \brief Determines if the given locator is mutable (i.e. its pixels can be changed)
  191. template <typename L> struct locator_is_mutable : public iterator_is_mutable<typename L::x_iterator> {};
  192. /// \ingroup GILIsMutable
  193. /// \brief Determines if the given view is mutable (i.e. its pixels can be changed)
  194. template <typename V> struct view_is_mutable : public iterator_is_mutable<typename V::x_iterator> {};
  195. //////////////////////////////////////////////////
  196. ///
  197. /// TYPE FACTORY METAFUNCTIONS
  198. /// Metafunctions returning GIL types from other GIL types
  199. ///
  200. //////////////////////////////////////////////////
  201. /// \defgroup TypeFactoryFromElements xxx_type
  202. /// \ingroup TypeFactory
  203. /// \brief Returns the type of a homogeneous GIL construct given its elements (channel, layout, whether it is planar, step, mutable, etc.)
  204. /// \defgroup TypeFactoryFromPixel xxx_type_from_pixel
  205. /// \ingroup TypeFactory
  206. /// \brief Returns the type of a GIL construct given its pixel type, whether it is planar, step, mutable, etc.
  207. /// \defgroup TypeFactoryDerived derived_xxx_type
  208. /// \ingroup TypeFactory
  209. /// \brief Returns the type of a homogeneous GIL construct given a related construct by changing some of its properties
  210. /// \ingroup TypeFactoryFromElements
  211. /// \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
  212. template <typename T, typename L, bool IsPlanar=false, bool IsMutable=true> struct pixel_reference_type{};
  213. template <typename T, typename L> struct pixel_reference_type<T,L,false,true > { using type = pixel<T,L>&; };
  214. template <typename T, typename L> struct pixel_reference_type<T,L,false,false> { using type = pixel<T,L> const&; };
  215. template <typename T, typename L> struct pixel_reference_type<T,L,true,true> { using type = planar_pixel_reference<typename channel_traits<T>::reference,typename color_space_type<L>::type> const; }; // TODO: Assert M=identity
  216. template <typename T, typename L> struct pixel_reference_type<T,L,true,false> { using type = planar_pixel_reference<typename channel_traits<T>::const_reference,typename color_space_type<L>::type> const; };// TODO: Assert M=identity
  217. /// \ingroup TypeFactoryFromPixel
  218. /// \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
  219. template <typename Pixel, bool IsPlanar=false, bool IsStep=false, bool IsMutable=true> struct iterator_type_from_pixel{};
  220. template <typename Pixel> struct iterator_type_from_pixel<Pixel,false,false,true > { using type = Pixel *; };
  221. template <typename Pixel> struct iterator_type_from_pixel<Pixel,false,false,false> { using type = const Pixel *; };
  222. template <typename Pixel> struct iterator_type_from_pixel<Pixel,true,false,true> {
  223. using type = planar_pixel_iterator<typename channel_traits<typename channel_type<Pixel>::type>::pointer,typename color_space_type<Pixel>::type>;
  224. };
  225. template <typename Pixel> struct iterator_type_from_pixel<Pixel,true,false,false> {
  226. using type = planar_pixel_iterator<typename channel_traits<typename channel_type<Pixel>::type>::const_pointer,typename color_space_type<Pixel>::type>;
  227. };
  228. template <typename Pixel, bool IsPlanar, bool IsMutable> struct iterator_type_from_pixel<Pixel,IsPlanar,true,IsMutable> {
  229. using type = memory_based_step_iterator<typename iterator_type_from_pixel<Pixel,IsPlanar,false,IsMutable>::type>;
  230. };
  231. /// \ingroup TypeFactoryFromElements
  232. /// \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
  233. template <typename T, typename L, bool IsPlanar=false, bool IsStep=false, bool IsMutable=true> struct iterator_type{};
  234. template <typename T, typename L> struct iterator_type<T,L,false,false,true > { using type = pixel<T,L>*; };
  235. template <typename T, typename L> struct iterator_type<T,L,false,false,false> { using type = pixel<T,L> const*; };
  236. template <typename T, typename L> struct iterator_type<T,L,true,false,true> { using type = planar_pixel_iterator<T*,typename L::color_space_t>; }; // TODO: Assert M=identity
  237. template <typename T, typename L> struct iterator_type<T,L,true,false,false> { using type = planar_pixel_iterator<const T*,typename L::color_space_t>; }; // TODO: Assert M=identity
  238. template <typename T, typename L, bool IsPlanar, bool IsMutable> struct iterator_type<T,L,IsPlanar,true,IsMutable> {
  239. using type = memory_based_step_iterator<typename iterator_type<T,L,IsPlanar,false,IsMutable>::type>;
  240. };
  241. /// \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
  242. /// \ingroup TypeFactory
  243. template <typename XIterator>
  244. struct type_from_x_iterator
  245. {
  246. using step_iterator_t = memory_based_step_iterator<XIterator>;
  247. using xy_locator_t = memory_based_2d_locator<step_iterator_t>;
  248. using view_t = image_view<xy_locator_t>;
  249. };
  250. namespace detail {
  251. template <typename BitField, typename FirstBit, typename NumBits>
  252. struct packed_channel_reference_type
  253. {
  254. using type = packed_channel_reference
  255. <
  256. BitField, FirstBit::value, NumBits::value, true
  257. > const;
  258. };
  259. template <typename BitField, typename ChannelBitSizes>
  260. class packed_channel_references_vector_type
  261. {
  262. template <typename FirstBit, typename NumBits>
  263. using reference_type = typename packed_channel_reference_type<BitField, FirstBit, NumBits>::type;
  264. // If ChannelBitSizesVector is mp11::mp_list_c<int,7,7,2>
  265. // Then first_bits_vector will be mp11::mp_list_c<int,0,7,14,16>
  266. using first_bit_list = mp11::mp_fold_q
  267. <
  268. ChannelBitSizes,
  269. mp11::mp_list<std::integral_constant<int, 0>>,
  270. mp11::mp_bind
  271. <
  272. mp11::mp_push_back,
  273. mp11::_1,
  274. mp11::mp_bind
  275. <
  276. mp11::mp_plus,
  277. mp11::mp_bind<mp_back, mp11::_1>,
  278. mp11::_2
  279. >
  280. >
  281. >;
  282. static_assert(mp11::mp_at_c<first_bit_list, 0>::value == 0, "packed channel first bit must be 0");
  283. public:
  284. using type = mp11::mp_transform
  285. <
  286. reference_type,
  287. mp_pop_back<first_bit_list>,
  288. ChannelBitSizes
  289. >;
  290. };
  291. } // namespace detail
  292. /// \ingroup TypeFactoryFromElements
  293. /// \brief Returns the type of a packed pixel given its bitfield type, the bit size of its channels and its layout.
  294. ///
  295. /// A packed pixel has channels that cover bit ranges but itself is byte aligned. RGB565 pixel is an example.
  296. ///
  297. /// The size of ChannelBitSizes must equal the number of channels in the given layout
  298. /// 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).
  299. /// If it is less than the number of bits in BitField, the last bits will be unused.
  300. template <typename BitField, typename ChannelBitSizes, typename Layout>
  301. struct packed_pixel_type
  302. {
  303. using type = packed_pixel
  304. <
  305. BitField,
  306. typename detail::packed_channel_references_vector_type
  307. <
  308. BitField,
  309. ChannelBitSizes
  310. >::type,
  311. Layout>;
  312. };
  313. /// \defgroup TypeFactoryPacked packed_image_type,bit_aligned_image_type
  314. /// \ingroup TypeFactoryFromElements
  315. /// \brief Returns the type of an image whose channels are not byte-aligned.
  316. ///
  317. /// A packed image is an image whose pixels are byte aligned, such as "rgb565". <br>
  318. /// A bit-aligned image is an image whose pixels are not byte aligned, such as "rgb222". <br>
  319. ///
  320. /// The sum of the bit sizes of all channels cannot exceed 64.
  321. /// \ingroup TypeFactoryPacked
  322. /// \brief Returns the type of an interleaved packed image: an image whose channels may not be byte-aligned, but whose pixels are byte aligned.
  323. template <typename BitField, typename ChannelBitSizes, typename Layout, typename Alloc=std::allocator<unsigned char>>
  324. struct packed_image_type
  325. {
  326. using type = image<typename packed_pixel_type<BitField,ChannelBitSizes,Layout>::type,false,Alloc>;
  327. };
  328. /// \ingroup TypeFactoryPacked
  329. /// \brief Returns the type of a single-channel image given its bitfield type, the bit size of its channel and its layout
  330. template <typename BitField, unsigned Size1, typename Layout, typename Alloc = std::allocator<unsigned char>>
  331. struct packed_image1_type
  332. : packed_image_type<BitField, mp11::mp_list_c<unsigned, Size1>, Layout, Alloc>
  333. {};
  334. /// \ingroup TypeFactoryPacked
  335. /// \brief Returns the type of a two channel image given its bitfield type, the bit size of its channels and its layout
  336. template <typename BitField, unsigned Size1, unsigned Size2, typename Layout, typename Alloc = std::allocator<unsigned char>>
  337. struct packed_image2_type
  338. : packed_image_type<BitField, mp11::mp_list_c<unsigned, Size1, Size2>, Layout, Alloc>
  339. {};
  340. /// \ingroup TypeFactoryPacked
  341. /// \brief Returns the type of a three channel image given its bitfield type, the bit size of its channels and its layout
  342. template <typename BitField, unsigned Size1, unsigned Size2, unsigned Size3, typename Layout, typename Alloc = std::allocator<unsigned char>>
  343. struct packed_image3_type
  344. : packed_image_type<BitField, mp11::mp_list_c<unsigned, Size1, Size2, Size3>, Layout, Alloc>
  345. {};
  346. /// \ingroup TypeFactoryPacked
  347. /// \brief Returns the type of a four channel image given its bitfield type, the bit size of its channels and its layout
  348. template <typename BitField, unsigned Size1, unsigned Size2, unsigned Size3, unsigned Size4, typename Layout, typename Alloc = std::allocator<unsigned char>>
  349. struct packed_image4_type
  350. : packed_image_type<BitField, mp11::mp_list_c<unsigned, Size1, Size2, Size3, Size4>, Layout, Alloc>
  351. {};
  352. /// \ingroup TypeFactoryPacked
  353. /// \brief Returns the type of a five channel image given its bitfield type, the bit size of its channels and its layout
  354. template <typename BitField, unsigned Size1, unsigned Size2, unsigned Size3, unsigned Size4, unsigned Size5, typename Layout, typename Alloc = std::allocator<unsigned char>>
  355. struct packed_image5_type
  356. : packed_image_type<BitField, mp11::mp_list_c<unsigned, Size1, Size2, Size3, Size4, Size5>, Layout, Alloc> {};
  357. /// \ingroup TypeFactoryPacked
  358. /// \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.
  359. ///
  360. /// 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
  361. /// image whose rows are byte-aligned, use 8 as the alignment parameter, not 1.
  362. ///
  363. template
  364. <
  365. typename ChannelBitSizes,
  366. typename Layout,
  367. typename Alloc = std::allocator<unsigned char>
  368. >
  369. struct bit_aligned_image_type
  370. {
  371. private:
  372. static constexpr int bit_size =
  373. mp11::mp_fold
  374. <
  375. ChannelBitSizes,
  376. std::integral_constant<int, 0>,
  377. mp11::mp_plus
  378. >::value;
  379. using bitfield_t = typename detail::min_fast_uint<bit_size + 7>::type;
  380. using bit_alignedref_t = bit_aligned_pixel_reference<bitfield_t, ChannelBitSizes, Layout, true> const;
  381. public:
  382. using type = image<bit_alignedref_t,false,Alloc>;
  383. };
  384. /// \ingroup TypeFactoryPacked
  385. /// \brief Returns the type of a single-channel bit-aligned image given the bit size of its channel and its layout
  386. template <unsigned Size1, typename Layout, typename Alloc = std::allocator<unsigned char>>
  387. struct bit_aligned_image1_type : bit_aligned_image_type<mp11::mp_list_c<unsigned, Size1>, Layout, Alloc> {};
  388. /// \ingroup TypeFactoryPacked
  389. /// \brief Returns the type of a two channel bit-aligned image given the bit size of its channels and its layout
  390. template <unsigned Size1, unsigned Size2, typename Layout, typename Alloc = std::allocator<unsigned char>>
  391. struct bit_aligned_image2_type : bit_aligned_image_type<mp11::mp_list_c<unsigned, Size1, Size2>, Layout, Alloc> {};
  392. /// \ingroup TypeFactoryPacked
  393. /// \brief Returns the type of a three channel bit-aligned image given the bit size of its channels and its layout
  394. template <unsigned Size1, unsigned Size2, unsigned Size3, typename Layout, typename Alloc = std::allocator<unsigned char>>
  395. struct bit_aligned_image3_type : bit_aligned_image_type<mp11::mp_list_c<unsigned, Size1, Size2, Size3>, Layout, Alloc> {};
  396. /// \ingroup TypeFactoryPacked
  397. /// \brief Returns the type of a four channel bit-aligned image given the bit size of its channels and its layout
  398. template <unsigned Size1, unsigned Size2, unsigned Size3, unsigned Size4, typename Layout, typename Alloc = std::allocator<unsigned char>>
  399. struct bit_aligned_image4_type : bit_aligned_image_type<mp11::mp_list_c<unsigned, Size1, Size2, Size3, Size4>, Layout, Alloc> {};
  400. /// \ingroup TypeFactoryPacked
  401. /// \brief Returns the type of a five channel bit-aligned image given the bit size of its channels and its layout
  402. template <unsigned Size1, unsigned Size2, unsigned Size3, unsigned Size4, unsigned Size5, typename Layout, typename Alloc = std::allocator<unsigned char>>
  403. struct bit_aligned_image5_type : bit_aligned_image_type<mp11::mp_list_c<unsigned, Size1, Size2, Size3, Size4, Size5>, Layout, Alloc> {};
  404. /// \ingroup TypeFactoryFromElements
  405. /// \brief Returns the type of a homogeneous pixel given the channel type and layout
  406. template <typename Channel, typename Layout>
  407. struct pixel_value_type
  408. {
  409. // by default use gil::pixel. Specializations are provided for
  410. using type = pixel<Channel, Layout>;
  411. };
  412. // Specializations for packed channels
  413. template <typename BitField, int NumBits, bool IsMutable, typename Layout>
  414. struct pixel_value_type<packed_dynamic_channel_reference<BitField, NumBits, IsMutable>, Layout>
  415. : packed_pixel_type<BitField, mp11::mp_list_c<unsigned, NumBits>, Layout>
  416. {};
  417. template <typename BitField, int NumBits, bool IsMutable, typename Layout>
  418. struct pixel_value_type<packed_dynamic_channel_reference<BitField, NumBits, IsMutable> const, Layout>
  419. : packed_pixel_type<BitField, mp11::mp_list_c<unsigned, NumBits>, Layout>
  420. {};
  421. template <typename BitField, int FirstBit, int NumBits, bool IsMutable, typename Layout>
  422. struct pixel_value_type<packed_channel_reference<BitField, FirstBit, NumBits, IsMutable>, Layout>
  423. : packed_pixel_type<BitField, mp11::mp_list_c<unsigned, NumBits>, Layout>
  424. {};
  425. template <typename BitField, int FirstBit, int NumBits, bool IsMutable, typename Layout>
  426. struct pixel_value_type<packed_channel_reference<BitField, FirstBit, NumBits, IsMutable> const, Layout>
  427. : packed_pixel_type<BitField, mp11::mp_list_c<unsigned, NumBits>, Layout>
  428. {};
  429. template <int NumBits, typename Layout>
  430. struct pixel_value_type<packed_channel_value<NumBits>, Layout>
  431. : packed_pixel_type<typename detail::min_fast_uint<NumBits>::type, mp11::mp_list_c<unsigned, NumBits>, Layout>
  432. {};
  433. /// \ingroup TypeFactoryFromElements
  434. /// \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
  435. template <typename T, typename L, bool IsPlanar = false, bool IsStepX = false, bool IsMutable = true>
  436. struct locator_type
  437. {
  438. using type = typename type_from_x_iterator
  439. <
  440. typename iterator_type<T, L, IsPlanar, IsStepX, IsMutable>::type
  441. >::xy_locator_type;
  442. };
  443. /// \ingroup TypeFactoryFromElements
  444. /// \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
  445. template <typename T, typename L, bool IsPlanar = false, bool IsStepX = false, bool IsMutable = true>
  446. struct view_type
  447. {
  448. using type = typename type_from_x_iterator
  449. <
  450. typename iterator_type<T, L, IsPlanar, IsStepX, IsMutable>::type
  451. >::view_t;
  452. };
  453. /// \ingroup TypeFactoryFromElements
  454. /// \brief Returns the type of a homogeneous image given the channel type, layout, and whether it operates on planar data
  455. template <typename T, typename L, bool IsPlanar = false, typename Alloc = std::allocator<unsigned char>>
  456. struct image_type
  457. {
  458. using type = image<pixel<T, L>, IsPlanar, Alloc>;
  459. };
  460. /// \ingroup TypeFactoryFromPixel
  461. /// \brief Returns the type of a view the pixel type, whether it operates on planar data and whether it has a step horizontally
  462. template <typename Pixel, bool IsPlanar=false, bool IsStepX=false, bool IsMutable=true>
  463. struct view_type_from_pixel {
  464. using type = typename type_from_x_iterator<typename iterator_type_from_pixel<Pixel,IsPlanar,IsStepX,IsMutable>::type>::view_t;
  465. };
  466. /// \brief Constructs a pixel reference type from a source pixel reference type by changing some of the properties.
  467. /// \ingroup TypeFactoryDerived
  468. /// Use use_default for the properties of the source view that you want to keep
  469. template
  470. <
  471. typename Ref,
  472. typename T = use_default,
  473. typename L = use_default,
  474. typename IsPlanar = use_default,
  475. typename IsMutable = use_default>
  476. class derived_pixel_reference_type
  477. {
  478. using pixel_t = typename std::remove_reference<Ref>::type;
  479. using channel_t = typename mp11::mp_if
  480. <
  481. std::is_same<T, use_default>,
  482. typename channel_type<pixel_t>::type,
  483. T
  484. >::type;
  485. using layout_t = typename mp11::mp_if
  486. <
  487. std::is_same<L, use_default>,
  488. layout
  489. <
  490. typename color_space_type<pixel_t>::type,
  491. typename channel_mapping_type<pixel_t>::type
  492. >,
  493. L
  494. >::type;
  495. static bool const mut = mp11::mp_if
  496. <
  497. std::is_same<IsMutable, use_default>,
  498. pixel_reference_is_mutable<Ref>,
  499. IsMutable
  500. >::value;
  501. static bool const planar = mp11::mp_if
  502. <
  503. std::is_same<IsPlanar, use_default>,
  504. is_planar<pixel_t>,
  505. IsPlanar
  506. >::value;
  507. public:
  508. using type = typename pixel_reference_type<channel_t, layout_t, planar, mut>::type;
  509. };
  510. /// \brief Constructs a pixel iterator type from a source pixel iterator type by changing some of the properties.
  511. /// \ingroup TypeFactoryDerived
  512. /// Use use_default for the properties of the source view that you want to keep
  513. template
  514. <
  515. typename Iterator,
  516. typename T = use_default,
  517. typename L = use_default,
  518. typename IsPlanar = use_default,
  519. typename IsStep = use_default,
  520. typename IsMutable = use_default
  521. >
  522. class derived_iterator_type
  523. {
  524. using channel_t = typename mp11::mp_if
  525. <
  526. std::is_same<T, use_default>,
  527. typename channel_type<Iterator>::type,
  528. T
  529. >::type;
  530. using layout_t = typename mp11::mp_if
  531. <
  532. std::is_same<L, use_default>,
  533. layout
  534. <
  535. typename color_space_type<Iterator>::type,
  536. typename channel_mapping_type<Iterator>::type
  537. >,
  538. L
  539. >::type;
  540. static const bool mut = mp11::mp_if
  541. <
  542. std::is_same<IsMutable, use_default>,
  543. iterator_is_mutable<Iterator>,
  544. IsMutable
  545. >::value;
  546. static bool const planar = mp11::mp_if
  547. <
  548. std::is_same<IsPlanar, use_default>,
  549. is_planar<Iterator>,
  550. IsPlanar
  551. >::value;
  552. static bool const step = mp11::mp_if
  553. <
  554. std::is_same<IsStep, use_default>,
  555. iterator_is_step<Iterator>,
  556. IsStep
  557. >::type::value;
  558. public:
  559. using type = typename iterator_type<channel_t, layout_t, planar, step, mut>::type;
  560. };
  561. /// \brief Constructs an image view type from a source view type by changing some of the properties.
  562. /// \ingroup TypeFactoryDerived
  563. /// Use use_default for the properties of the source view that you want to keep
  564. template <typename View, typename T = use_default, typename L = use_default, typename IsPlanar = use_default, typename StepX = use_default, typename IsMutable = use_default>
  565. class derived_view_type
  566. {
  567. using channel_t = typename mp11::mp_if
  568. <
  569. std::is_same<T, use_default>,
  570. typename channel_type<View>::type,
  571. T
  572. >;
  573. using layout_t = typename mp11::mp_if
  574. <
  575. std::is_same<L, use_default>,
  576. layout
  577. <
  578. typename color_space_type<View>::type,
  579. typename channel_mapping_type<View>::type
  580. >,
  581. L
  582. >;
  583. static bool const mut = mp11::mp_if
  584. <
  585. std::is_same<IsMutable, use_default>,
  586. view_is_mutable<View>,
  587. IsMutable
  588. >::value;
  589. static bool const planar = mp11::mp_if
  590. <
  591. std::is_same<IsPlanar, use_default>,
  592. is_planar<View>,
  593. IsPlanar
  594. >::value;
  595. static bool const step = mp11::mp_if
  596. <
  597. std::is_same<StepX, use_default>,
  598. view_is_step_in_x<View>,
  599. StepX
  600. >::value;
  601. public:
  602. using type = typename view_type<channel_t, layout_t, planar, step, mut>::type;
  603. };
  604. /// \brief Constructs a homogeneous image type from a source image type by changing some of the properties.
  605. /// \ingroup TypeFactoryDerived
  606. /// Use use_default for the properties of the source image that you want to keep
  607. template <typename Image, typename T = use_default, typename L = use_default, typename IsPlanar = use_default>
  608. class derived_image_type
  609. {
  610. using channel_t = typename mp11::mp_if
  611. <
  612. std::is_same<T, use_default>,
  613. typename channel_type<Image>::type,
  614. T
  615. >::type;
  616. using layout_t = typename mp11::mp_if
  617. <
  618. std::is_same<L, use_default>,
  619. layout
  620. <
  621. typename color_space_type<Image>::type,
  622. typename channel_mapping_type<Image>::type>,
  623. L
  624. >::type;
  625. static bool const planar = mp11::mp_if
  626. <
  627. std::is_same<IsPlanar, use_default>,
  628. is_planar<Image>,
  629. IsPlanar
  630. >::value;
  631. public:
  632. using type = typename image_type<channel_t, layout_t, planar>::type;
  633. };
  634. }} // namespace boost::gil
  635. #endif