metafunctions.hpp 31 KB

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