// // Copyright 2005-2007 Adobe Systems Incorporated // Copyright 2021 Pranam Lashkari // // Distributed under the Boost Software License, Version 1.0 // See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt // #ifndef BOOST_GIL_METAFUNCTIONS_HPP #define BOOST_GIL_METAFUNCTIONS_HPP #include #include #include #include #include #include #include namespace boost { namespace gil { // forward declarations template struct pixel; template struct packed_pixel; template struct planar_pixel_reference; template struct planar_pixel_iterator; template class memory_based_step_iterator; template class memory_based_2d_locator; template class image_view; template > class image; template struct channel_type; template struct color_space_type; template struct channel_mapping_type; template struct is_iterator_adaptor; template struct iterator_adaptor_get_base; template struct bit_aligned_pixel_reference; ////////////////////////////////////////////////// /// /// TYPE ANALYSIS METAFUNCTIONS /// Predicate metafunctions determining properties of GIL types /// ////////////////////////////////////////////////// /// \defgroup GILIsBasic xxx_is_basic /// \ingroup TypeAnalysis /// \brief Determines if GIL constructs are basic. /// Basic constructs are the ones that can be generated with the type /// factory methods pixel_reference_type, iterator_type, locator_type, view_type and image_type /// They can be mutable/immutable, planar/interleaved, step/nonstep. They must use GIL-provided models. /// \brief Determines if a given pixel reference is basic /// Basic references must use gil::pixel& (if interleaved), gil::planar_pixel_reference (if planar). They must use the standard constness rules. /// \ingroup GILIsBasic template struct pixel_reference_is_basic : public std::false_type {}; template struct pixel_reference_is_basic&> : std::true_type {}; template struct pixel_reference_is_basic&> : std::true_type {}; template struct pixel_reference_is_basic> : std::true_type {}; template struct pixel_reference_is_basic> : std::true_type {}; /// \brief Determines if a given pixel iterator is basic /// 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. /// \ingroup GILIsBasic template struct iterator_is_basic : std::false_type {}; /// \tparam T mutable interleaved pixel type template struct iterator_is_basic*> : std::true_type {}; /// \tparam T immutable interleaved pixel type template struct iterator_is_basic const*> : std::true_type {}; /// \tparam T mutable planar pixel type template struct iterator_is_basic> : std::true_type {}; /// \tparam T immutable planar pixel type template struct iterator_is_basic> : std::true_type {}; /// \tparam T mutable interleaved step template struct iterator_is_basic*>> : std::true_type {}; /// \tparam T immutable interleaved step template struct iterator_is_basic const*>> : std::true_type {}; /// \tparam T mutable planar step template struct iterator_is_basic>> : std::true_type {}; /// \tparam T immutable planar step template struct iterator_is_basic>> : std::true_type {}; /// \ingroup GILIsBasic /// \brief Determines if a given locator is basic. A basic locator is memory-based and has basic x_iterator and y_iterator template struct locator_is_basic : std::false_type {}; template struct locator_is_basic < memory_based_2d_locator> > : iterator_is_basic {}; /// \ingroup GILIsBasic /// \brief Basic views must be over basic locators template struct view_is_basic : std::false_type {}; template struct view_is_basic> : locator_is_basic {}; /// \ingroup GILIsBasic /// \brief Basic images must use basic views and std::allocator template struct image_is_basic : std::false_type {}; template struct image_is_basic> : std::true_type {}; /// \defgroup GILIsStep xxx_is_step /// \ingroup TypeAnalysis /// \brief Determines if the given iterator/locator/view has a step that could be set dynamically template struct iterator_is_step; namespace detail { template struct iterator_is_step_impl; // iterator that has the same type as its dynamic_x_step_type must be a step iterator template struct iterator_is_step_impl : std::true_type {}; // base iterator can never be a step iterator template struct iterator_is_step_impl : std::false_type {}; // for an iterator adaptor, see if its base is step template struct iterator_is_step_impl : public iterator_is_step::type> {}; } // namespace detail /// \ingroup GILIsStep /// \brief Determines if the given iterator has a step that could be set dynamically template struct iterator_is_step : detail::iterator_is_step_impl < I, !is_iterator_adaptor::value, std::is_same::type >::value > {}; /// \ingroup GILIsStep /// \brief Determines if the given locator has a horizontal step that could be set dynamically template struct locator_is_step_in_x : public iterator_is_step {}; /// \ingroup GILIsStep /// \brief Determines if the given locator has a vertical step that could be set dynamically template struct locator_is_step_in_y : public iterator_is_step {}; /// \ingroup GILIsStep /// \brief Determines if the given view has a horizontal step that could be set dynamically template struct view_is_step_in_x : public locator_is_step_in_x {}; /// \ingroup GILIsStep /// \brief Determines if the given view has a vertical step that could be set dynamically template struct view_is_step_in_y : public locator_is_step_in_y {}; /// \brief Determines whether the given pixel reference is a proxy class or a native C++ reference /// \ingroup TypeAnalysis template struct pixel_reference_is_proxy : mp11::mp_not < std::is_same < typename detail::remove_const_and_reference::type, typename detail::remove_const_and_reference::type::value_type > > {}; /// \brief Given a model of a pixel, determines whether the model represents a pixel reference (as opposed to pixel value) /// \ingroup TypeAnalysis template struct pixel_is_reference : mp11::mp_or, pixel_reference_is_proxy> {}; /// \defgroup GILIsMutable xxx_is_mutable /// \ingroup TypeAnalysis /// \brief Determines if the given pixel reference/iterator/locator/view is mutable (i.e. its pixels can be changed) /// \ingroup GILIsMutable /// \brief Determines if the given pixel reference is mutable (i.e. its channels can be changed) /// /// Note that built-in C++ references obey the const qualifier but reference proxy classes do not. template struct pixel_reference_is_mutable : std::integral_constant::type::is_mutable> {}; template struct pixel_reference_is_mutable : mp11::mp_and, pixel_reference_is_mutable> {}; /// \ingroup GILIsMutable /// \brief Determines if the given locator is mutable (i.e. its pixels can be changed) template struct locator_is_mutable : public iterator_is_mutable {}; /// \ingroup GILIsMutable /// \brief Determines if the given view is mutable (i.e. its pixels can be changed) template struct view_is_mutable : public iterator_is_mutable {}; ////////////////////////////////////////////////// /// /// TYPE FACTORY METAFUNCTIONS /// Metafunctions returning GIL types from other GIL types /// ////////////////////////////////////////////////// /// \defgroup TypeFactoryFromElements xxx_type /// \ingroup TypeFactory /// \brief Returns the type of a homogeneous GIL construct given its elements (channel, layout, whether it is planar, step, mutable, etc.) /// \defgroup TypeFactoryFromPixel xxx_type_from_pixel /// \ingroup TypeFactory /// \brief Returns the type of a GIL construct given its pixel type, whether it is planar, step, mutable, etc. /// \defgroup TypeFactoryDerived derived_xxx_type /// \ingroup TypeFactory /// \brief Returns the type of a homogeneous GIL construct given a related construct by changing some of its properties /// \ingroup TypeFactoryFromElements /// \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 template struct pixel_reference_type{}; template struct pixel_reference_type { using type = pixel&; }; template struct pixel_reference_type { using type = pixel const&; }; template struct pixel_reference_type { using type = planar_pixel_reference::reference,typename color_space_type::type> const; }; // TODO: Assert M=identity template struct pixel_reference_type { using type = planar_pixel_reference::const_reference,typename color_space_type::type> const; };// TODO: Assert M=identity /// \ingroup TypeFactoryFromPixel /// \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 template struct iterator_type_from_pixel{}; template struct iterator_type_from_pixel { using type = Pixel *; }; template struct iterator_type_from_pixel { using type = const Pixel *; }; template struct iterator_type_from_pixel { using type = planar_pixel_iterator::type>::pointer,typename color_space_type::type>; }; template struct iterator_type_from_pixel { using type = planar_pixel_iterator::type>::const_pointer,typename color_space_type::type>; }; template struct iterator_type_from_pixel { using type = memory_based_step_iterator::type>; }; /// \ingroup TypeFactoryFromElements /// \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 template struct iterator_type{}; template struct iterator_type { using type = pixel*; }; template struct iterator_type { using type = pixel const*; }; template struct iterator_type { using type = planar_pixel_iterator; }; // TODO: Assert M=identity template struct iterator_type { using type = planar_pixel_iterator; }; // TODO: Assert M=identity template struct iterator_type { using type = memory_based_step_iterator::type>; }; /// \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 /// \ingroup TypeFactory template struct type_from_x_iterator { using step_iterator_t = memory_based_step_iterator; using xy_locator_t = memory_based_2d_locator; using view_t = image_view; }; namespace detail { template struct packed_channel_reference_type { using type = packed_channel_reference < BitField, FirstBit::value, NumBits::value, true > const; }; template class packed_channel_references_vector_type { template using reference_type = typename packed_channel_reference_type::type; // If ChannelBitSizesVector is mp11::mp_list_c // Then first_bits_vector will be mp11::mp_list_c using first_bit_list = mp11::mp_fold_q < ChannelBitSizes, mp11::mp_list>, mp11::mp_bind < mp11::mp_push_back, mp11::_1, mp11::mp_bind < mp11::mp_plus, mp11::mp_bind, mp11::_2 > > >; static_assert(mp11::mp_at_c::value == 0, "packed channel first bit must be 0"); public: using type = mp11::mp_transform < reference_type, mp_pop_back, ChannelBitSizes >; }; } // namespace detail /// \ingroup TypeFactoryFromElements /// \brief Returns the type of a packed pixel given its bitfield type, the bit size of its channels and its layout. /// /// A packed pixel has channels that cover bit ranges but itself is byte aligned. RGB565 pixel is an example. /// /// The size of ChannelBitSizes must equal the number of channels in the given layout /// 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). /// If it is less than the number of bits in BitField, the last bits will be unused. template struct packed_pixel_type { using type = packed_pixel < BitField, typename detail::packed_channel_references_vector_type < BitField, ChannelBitSizes >::type, Layout>; }; /// \defgroup TypeFactoryPacked packed_image_type,bit_aligned_image_type /// \ingroup TypeFactoryFromElements /// \brief Returns the type of an image whose channels are not byte-aligned. /// /// A packed image is an image whose pixels are byte aligned, such as "rgb565".
/// A bit-aligned image is an image whose pixels are not byte aligned, such as "rgb222".
/// /// The sum of the bit sizes of all channels cannot exceed 64. /// \ingroup TypeFactoryPacked /// \brief Returns the type of an interleaved packed image: an image whose channels may not be byte-aligned, but whose pixels are byte aligned. template > struct packed_image_type { using type = image::type,false,Alloc>; }; /// \ingroup TypeFactoryPacked /// \brief Returns the type of a single-channel image given its bitfield type, the bit size of its channel and its layout template > struct packed_image1_type : packed_image_type, Layout, Alloc> {}; /// \ingroup TypeFactoryPacked /// \brief Returns the type of a two channel image given its bitfield type, the bit size of its channels and its layout template > struct packed_image2_type : packed_image_type, Layout, Alloc> {}; /// \ingroup TypeFactoryPacked /// \brief Returns the type of a three channel image given its bitfield type, the bit size of its channels and its layout template > struct packed_image3_type : packed_image_type, Layout, Alloc> {}; /// \ingroup TypeFactoryPacked /// \brief Returns the type of a four channel image given its bitfield type, the bit size of its channels and its layout template > struct packed_image4_type : packed_image_type, Layout, Alloc> {}; /// \ingroup TypeFactoryPacked /// \brief Returns the type of a five channel image given its bitfield type, the bit size of its channels and its layout template > struct packed_image5_type : packed_image_type, Layout, Alloc> {}; /// \ingroup TypeFactoryPacked /// \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. /// /// 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 /// image whose rows are byte-aligned, use 8 as the alignment parameter, not 1. /// template < typename ChannelBitSizes, typename Layout, typename Alloc = std::allocator > struct bit_aligned_image_type { private: static constexpr int bit_size = mp11::mp_fold < ChannelBitSizes, std::integral_constant, mp11::mp_plus >::value; using bitfield_t = typename detail::min_fast_uint::type; using bit_alignedref_t = bit_aligned_pixel_reference const; public: using type = image; }; /// \ingroup TypeFactoryPacked /// \brief Returns the type of a single-channel bit-aligned image given the bit size of its channel and its layout template > struct bit_aligned_image1_type : bit_aligned_image_type, Layout, Alloc> {}; /// \ingroup TypeFactoryPacked /// \brief Returns the type of a two channel bit-aligned image given the bit size of its channels and its layout template > struct bit_aligned_image2_type : bit_aligned_image_type, Layout, Alloc> {}; /// \ingroup TypeFactoryPacked /// \brief Returns the type of a three channel bit-aligned image given the bit size of its channels and its layout template > struct bit_aligned_image3_type : bit_aligned_image_type, Layout, Alloc> {}; /// \ingroup TypeFactoryPacked /// \brief Returns the type of a four channel bit-aligned image given the bit size of its channels and its layout template > struct bit_aligned_image4_type : bit_aligned_image_type, Layout, Alloc> {}; /// \ingroup TypeFactoryPacked /// \brief Returns the type of a five channel bit-aligned image given the bit size of its channels and its layout template > struct bit_aligned_image5_type : bit_aligned_image_type, Layout, Alloc> {}; /// \ingroup TypeFactoryFromElements /// \brief Returns the type of a homogeneous pixel given the channel type and layout template struct pixel_value_type { // by default use gil::pixel. Specializations are provided for using type = pixel; }; // Specializations for packed channels template struct pixel_value_type, Layout> : packed_pixel_type, Layout> {}; template struct pixel_value_type const, Layout> : packed_pixel_type, Layout> {}; template struct pixel_value_type, Layout> : packed_pixel_type, Layout> {}; template struct pixel_value_type const, Layout> : packed_pixel_type, Layout> {}; template struct pixel_value_type, Layout> : packed_pixel_type::type, mp11::mp_list_c, Layout> {}; /// \ingroup TypeFactoryFromElements /// \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 template struct locator_type { using type = typename type_from_x_iterator < typename iterator_type::type >::xy_locator_type; }; /// \ingroup TypeFactoryFromElements /// \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 template struct view_type { using type = typename type_from_x_iterator < typename iterator_type::type >::view_t; }; /// \ingroup TypeFactoryFromElements /// \brief Returns the type of a homogeneous image given the channel type, layout, and whether it operates on planar data template > struct image_type { using type = image, IsPlanar, Alloc>; }; /// \ingroup TypeFactoryFromPixel /// \brief Returns the type of a view the pixel type, whether it operates on planar data and whether it has a step horizontally template struct view_type_from_pixel { using type = typename type_from_x_iterator::type>::view_t; }; /// \brief Constructs a pixel reference type from a source pixel reference type by changing some of the properties. /// \ingroup TypeFactoryDerived /// Use use_default for the properties of the source view that you want to keep template < typename Ref, typename T = use_default, typename L = use_default, typename IsPlanar = use_default, typename IsMutable = use_default> class derived_pixel_reference_type { using pixel_t = typename std::remove_reference::type; using channel_t = typename mp11::mp_if < std::is_same, typename channel_type::type, T >::type; using layout_t = typename mp11::mp_if < std::is_same, layout < typename color_space_type::type, typename channel_mapping_type::type >, L >::type; static bool const mut = mp11::mp_if < std::is_same, pixel_reference_is_mutable, IsMutable >::value; static bool const planar = mp11::mp_if < std::is_same, is_planar, IsPlanar >::value; public: using type = typename pixel_reference_type::type; }; /// \brief Constructs a pixel iterator type from a source pixel iterator type by changing some of the properties. /// \ingroup TypeFactoryDerived /// Use use_default for the properties of the source view that you want to keep template < typename Iterator, typename T = use_default, typename L = use_default, typename IsPlanar = use_default, typename IsStep = use_default, typename IsMutable = use_default > class derived_iterator_type { using channel_t = typename mp11::mp_if < std::is_same, typename channel_type::type, T >::type; using layout_t = typename mp11::mp_if < std::is_same, layout < typename color_space_type::type, typename channel_mapping_type::type >, L >::type; static const bool mut = mp11::mp_if < std::is_same, iterator_is_mutable, IsMutable >::value; static bool const planar = mp11::mp_if < std::is_same, is_planar, IsPlanar >::value; static bool const step = mp11::mp_if < std::is_same, iterator_is_step, IsStep >::type::value; public: using type = typename iterator_type::type; }; /// \brief Constructs an image view type from a source view type by changing some of the properties. /// \ingroup TypeFactoryDerived /// Use use_default for the properties of the source view that you want to keep template class derived_view_type { using channel_t = typename mp11::mp_if < std::is_same, typename channel_type::type, T >; using layout_t = typename mp11::mp_if < std::is_same, layout < typename color_space_type::type, typename channel_mapping_type::type >, L >; static bool const mut = mp11::mp_if < std::is_same, view_is_mutable, IsMutable >::value; static bool const planar = mp11::mp_if < std::is_same, is_planar, IsPlanar >::value; static bool const step = mp11::mp_if < std::is_same, view_is_step_in_x, StepX >::value; public: using type = typename view_type::type; }; /// \brief Constructs a homogeneous image type from a source image type by changing some of the properties. /// \ingroup TypeFactoryDerived /// Use use_default for the properties of the source image that you want to keep template class derived_image_type { using channel_t = typename mp11::mp_if < std::is_same, typename channel_type::type, T >::type; using layout_t = typename mp11::mp_if < std::is_same, layout < typename color_space_type::type, typename channel_mapping_type::type>, L >::type; static bool const planar = mp11::mp_if < std::is_same, is_planar, IsPlanar >::value; public: using type = typename image_type::type; }; }} // namespace boost::gil #endif