// // Copyright 2005-2007 Adobe Systems Incorporated // Copyright 2019 Mateusz Loskot // // 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_COLOR_BASE_ALGORITHM_HPP #define BOOST_GIL_COLOR_BASE_ALGORITHM_HPP #include #include #include #include #include #include namespace boost { namespace gil { /////////////////////////////////////// /// size: Semantic channel size /////////////////////////////////////// /** \defgroup ColorBaseAlgorithmSize size \ingroup ColorBaseAlgorithm \brief Returns an integral constant type specifying the number of elements in a color base Example: \code static_assert(size::value == 3, ""); static_assert(size::value == 4, ""); \endcode */ /// \brief Returns an integral constant type specifying the number of elements in a color base /// \ingroup ColorBaseAlgorithmSize template struct size : public mp11::mp_size {}; /////////////////////////////////////// /// semantic_at_c: Semantic channel accessors /////////////////////////////////////// /** \defgroup ColorBaseAlgorithmSemanticAtC kth_semantic_element_type, kth_semantic_element_reference_type, kth_semantic_element_const_reference_type, semantic_at_c \ingroup ColorBaseAlgorithm \brief Support for accessing the elements of a color base by semantic index The semantic index of an element is the index of its color in the color space. Semantic indexing allows for proper pairing of elements of color bases independent on their layout. For example, red is the first semantic element of a color base regardless of whether it has an RGB layout or a BGR layout. All GIL color base algorithms taking multiple color bases use semantic indexing to access their elements. Example: \code // 16-bit BGR pixel, 4 bits for the blue, 3 bits for the green, 2 bits for the red channel and 7 unused bits using bgr432_pixel_t = packed_pixel_type, bgr_layout_t>::type; // A reference to its red channel. Although the red channel is the third, its semantic index is 0 in the RGB color space using red_channel_reference_t = kth_semantic_element_reference_type::type; // Initialize the pixel to black bgr432_pixel_t red_pixel(0,0,0); // Set the red channel to 100% red_channel_reference_t red_channel = semantic_at_c<0>(red_pixel); red_channel = channel_traits::max_value(); \endcode */ /// \brief Specifies the type of the K-th semantic element of a color base /// \ingroup ColorBaseAlgorithmSemanticAtC template struct kth_semantic_element_type { using channel_mapping_t = typename ColorBase::layout_t::channel_mapping_t; static_assert(K < mp11::mp_size::value, "K index should be less than size of channel_mapping_t sequence"); static constexpr int semantic_index = mp11::mp_at_c::type::value; using type = typename kth_element_type::type; }; /// \brief Specifies the return type of the mutable semantic_at_c(color_base); /// \ingroup ColorBaseAlgorithmSemanticAtC template struct kth_semantic_element_reference_type { using channel_mapping_t = typename ColorBase::layout_t::channel_mapping_t; static_assert(K < mp11::mp_size::value, "K index should be less than size of channel_mapping_t sequence"); static constexpr int semantic_index = mp11::mp_at_c::type::value; using type = typename kth_element_reference_type::type; static type get(ColorBase& cb) { return gil::at_c(cb); } }; /// \brief Specifies the return type of the constant semantic_at_c(color_base); /// \ingroup ColorBaseAlgorithmSemanticAtC template struct kth_semantic_element_const_reference_type { using channel_mapping_t = typename ColorBase::layout_t::channel_mapping_t; static_assert(K < mp11::mp_size::value, "K index should be less than size of channel_mapping_t sequence"); static constexpr int semantic_index = mp11::mp_at_c::type::value; using type = typename kth_element_const_reference_type::type; static type get(const ColorBase& cb) { return gil::at_c(cb); } }; /// \brief A mutable accessor to the K-th semantic element of a color base /// \ingroup ColorBaseAlgorithmSemanticAtC template inline auto semantic_at_c(ColorBase& p) -> typename std::enable_if < !std::is_const::value, typename kth_semantic_element_reference_type::type >::type { return kth_semantic_element_reference_type::get(p); } /// \brief A constant accessor to the K-th semantic element of a color base /// \ingroup ColorBaseAlgorithmSemanticAtC template inline auto semantic_at_c(ColorBase const& p) -> typename kth_semantic_element_const_reference_type::type { return kth_semantic_element_const_reference_type::get(p); } /////////////////////////////////////// /// get_color: Named channel accessors /////////////////////////////////////// /** \defgroup ColorBaseAlgorithmColor color_element_type, color_element_reference_type, color_element_const_reference_type, get_color, contains_color \ingroup ColorBaseAlgorithm \brief Support for accessing the elements of a color base by color name Example: A function that takes a generic pixel containing a red channel and sets it to 100%: \code template void set_red_to_max(Pixel& pixel) { boost::function_requires >(); static_assert(contains_color::value, ""); using red_channel_t = typename color_element_type::type; get_color(pixel, red_t()) = channel_traits::max_value(); } \endcode */ /// \brief A predicate metafunction determining whether a given color base contains a given color /// \ingroup ColorBaseAlgorithmColor template struct contains_color : mp11::mp_contains {}; template struct color_index_type : public detail::type_to_index {}; /// \brief Specifies the type of the element associated with a given color tag /// \ingroup ColorBaseAlgorithmColor template struct color_element_type : public kth_semantic_element_type::value> {}; /// \brief Specifies the return type of the mutable element accessor by color name, get_color(color_base, Color()); /// \ingroup ColorBaseAlgorithmColor template struct color_element_reference_type : public kth_semantic_element_reference_type::value> {}; /// \brief Specifies the return type of the constant element accessor by color name, get_color(color_base, Color()); /// \ingroup ColorBaseAlgorithmColor template struct color_element_const_reference_type : public kth_semantic_element_const_reference_type::value> {}; /// \brief Mutable accessor to the element associated with a given color name /// \ingroup ColorBaseAlgorithmColor template typename color_element_reference_type::type get_color(ColorBase& cb, Color=Color()) { return color_element_reference_type::get(cb); } /// \brief Constant accessor to the element associated with a given color name /// \ingroup ColorBaseAlgorithmColor template typename color_element_const_reference_type::type get_color(const ColorBase& cb, Color=Color()) { return color_element_const_reference_type::get(cb); } /////////////////////////////////////// /// /// element_type, element_reference_type, element_const_reference_type: Support for homogeneous color bases /// /////////////////////////////////////// /** \defgroup ColorBaseAlgorithmHomogeneous element_type, element_reference_type, element_const_reference_type \ingroup ColorBaseAlgorithm \brief Types for homogeneous color bases Example: \code using element_t = element_type::type; static_assert(std::is_same::value, ""); \endcode */ /// \brief Specifies the element type of a homogeneous color base /// \ingroup ColorBaseAlgorithmHomogeneous template struct element_type : public kth_element_type {}; /// \brief Specifies the return type of the mutable element accessor at_c of a homogeneous color base /// \ingroup ColorBaseAlgorithmHomogeneous template struct element_reference_type : public kth_element_reference_type {}; /// \brief Specifies the return type of the constant element accessor at_c of a homogeneous color base /// \ingroup ColorBaseAlgorithmHomogeneous template struct element_const_reference_type : public kth_element_const_reference_type {}; namespace detail { // compile-time recursion for per-element operations on color bases template struct element_recursion { #if defined(BOOST_GCC) && (BOOST_GCC >= 40900) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wconversion" #pragma GCC diagnostic ignored "-Wfloat-equal" #endif template static bool static_equal(const P1& p1, const P2& p2) { return element_recursion::static_equal(p1,p2) && semantic_at_c(p1)==semantic_at_c(p2); } template static void static_copy(const P1& p1, P2& p2) { element_recursion::static_copy(p1,p2); semantic_at_c(p2)=semantic_at_c(p1); } template static void static_fill(P& p, T2 v) { element_recursion::static_fill(p,v); semantic_at_c(p)=v; } template static void static_generate(Dst& dst, Op op) { element_recursion::static_generate(dst,op); semantic_at_c(dst)=op(); } #if defined(BOOST_GCC) && (BOOST_GCC >= 40900) #pragma GCC diagnostic pop #endif //static_for_each with one source template static Op static_for_each(P1& p1, Op op) { Op op2(element_recursion::static_for_each(p1,op)); op2(semantic_at_c(p1)); return op2; } template static Op static_for_each(const P1& p1, Op op) { Op op2(element_recursion::static_for_each(p1,op)); op2(semantic_at_c(p1)); return op2; } //static_for_each with two sources template static Op static_for_each(P1& p1, P2& p2, Op op) { Op op2(element_recursion::static_for_each(p1,p2,op)); op2(semantic_at_c(p1), semantic_at_c(p2)); return op2; } template static Op static_for_each(P1& p1, const P2& p2, Op op) { Op op2(element_recursion::static_for_each(p1,p2,op)); op2(semantic_at_c(p1), semantic_at_c(p2)); return op2; } template static Op static_for_each(const P1& p1, P2& p2, Op op) { Op op2(element_recursion::static_for_each(p1,p2,op)); op2(semantic_at_c(p1), semantic_at_c(p2)); return op2; } template static Op static_for_each(const P1& p1, const P2& p2, Op op) { Op op2(element_recursion::static_for_each(p1,p2,op)); op2(semantic_at_c(p1), semantic_at_c(p2)); return op2; } //static_for_each with three sources template static Op static_for_each(P1& p1, P2& p2, P3& p3, Op op) { Op op2(element_recursion::static_for_each(p1,p2,p3,op)); op2(semantic_at_c(p1), semantic_at_c(p2), semantic_at_c(p3)); return op2; } template static Op static_for_each(P1& p1, P2& p2, const P3& p3, Op op) { Op op2(element_recursion::static_for_each(p1,p2,p3,op)); op2(semantic_at_c(p1), semantic_at_c(p2), semantic_at_c(p3)); return op2; } template static Op static_for_each(P1& p1, const P2& p2, P3& p3, Op op) { Op op2(element_recursion::static_for_each(p1,p2,p3,op)); op2(semantic_at_c(p1), semantic_at_c(p2), semantic_at_c(p3)); return op2; } template static Op static_for_each(P1& p1, const P2& p2, const P3& p3, Op op) { Op op2(element_recursion::static_for_each(p1,p2,p3,op)); op2(semantic_at_c(p1), semantic_at_c(p2), semantic_at_c(p3)); return op2; } template static Op static_for_each(const P1& p1, P2& p2, P3& p3, Op op) { Op op2(element_recursion::static_for_each(p1,p2,p3,op)); op2(semantic_at_c(p1), semantic_at_c(p2), semantic_at_c(p3)); return op2; } template static Op static_for_each(const P1& p1, P2& p2, const P3& p3, Op op) { Op op2(element_recursion::static_for_each(p1,p2,p3,op)); op2(semantic_at_c(p1), semantic_at_c(p2), semantic_at_c(p3)); return op2; } template static Op static_for_each(const P1& p1, const P2& p2, P3& p3, Op op) { Op op2(element_recursion::static_for_each(p1,p2,p3,op)); op2(semantic_at_c(p1), semantic_at_c(p2), semantic_at_c(p3)); return op2; } template static Op static_for_each(const P1& p1, const P2& p2, const P3& p3, Op op) { Op op2(element_recursion::static_for_each(p1,p2,p3,op)); op2(semantic_at_c(p1), semantic_at_c(p2), semantic_at_c(p3)); return op2; } //static_transform with one source template static Op static_transform(P1& src, Dst& dst, Op op) { Op op2(element_recursion::static_transform(src,dst,op)); semantic_at_c(dst)=op2(semantic_at_c(src)); return op2; } template static Op static_transform(const P1& src, Dst& dst, Op op) { Op op2(element_recursion::static_transform(src,dst,op)); semantic_at_c(dst)=op2(semantic_at_c(src)); return op2; } //static_transform with two sources template static Op static_transform(P1& src1, P2& src2, Dst& dst, Op op) { Op op2(element_recursion::static_transform(src1,src2,dst,op)); semantic_at_c(dst)=op2(semantic_at_c(src1), semantic_at_c(src2)); return op2; } template static Op static_transform(P1& src1, const P2& src2, Dst& dst, Op op) { Op op2(element_recursion::static_transform(src1,src2,dst,op)); semantic_at_c(dst)=op2(semantic_at_c(src1), semantic_at_c(src2)); return op2; } template static Op static_transform(const P1& src1, P2& src2, Dst& dst, Op op) { Op op2(element_recursion::static_transform(src1,src2,dst,op)); semantic_at_c(dst)=op2(semantic_at_c(src1), semantic_at_c(src2)); return op2; } template static Op static_transform(const P1& src1, const P2& src2, Dst& dst, Op op) { Op op2(element_recursion::static_transform(src1,src2,dst,op)); semantic_at_c(dst)=op2(semantic_at_c(src1), semantic_at_c(src2)); return op2; } }; // Termination condition of the compile-time recursion for element operations on a color base template<> struct element_recursion<0> { //static_equal template static bool static_equal(const P1&, const P2&) { return true; } //static_copy template static void static_copy(const P1&, const P2&) {} //static_fill template static void static_fill(const P&, T2) {} //static_generate template static void static_generate(const Dst&,Op){} //static_for_each with one source template static Op static_for_each(const P1&,Op op){return op;} //static_for_each with two sources template static Op static_for_each(const P1&,const P2&,Op op){return op;} //static_for_each with three sources template static Op static_for_each(const P1&,const P2&,const P3&,Op op){return op;} //static_transform with one source template static Op static_transform(const P1&,const Dst&,Op op){return op;} //static_transform with two sources template static Op static_transform(const P1&,const P2&,const Dst&,Op op){return op;} }; // std::min and std::max don't have the mutable overloads... template inline const Q& mutable_min(const Q& x, const Q& y) { return x inline Q& mutable_min( Q& x, Q& y) { return x inline const Q& mutable_max(const Q& x, const Q& y) { return x inline Q& mutable_max( Q& x, Q& y) { return x struct min_max_recur { template static typename element_const_reference_type

::type max_(const P& p) { return mutable_max(min_max_recur::max_(p),semantic_at_c(p)); } template static typename element_reference_type

::type max_( P& p) { return mutable_max(min_max_recur::max_(p),semantic_at_c(p)); } template static typename element_const_reference_type

::type min_(const P& p) { return mutable_min(min_max_recur::min_(p),semantic_at_c(p)); } template static typename element_reference_type

::type min_( P& p) { return mutable_min(min_max_recur::min_(p),semantic_at_c(p)); } }; // termination condition of the compile-time recursion for min/max element template <> struct min_max_recur<1> { template static typename element_const_reference_type

::type max_(const P& p) { return semantic_at_c<0>(p); } template static typename element_reference_type

::type max_( P& p) { return semantic_at_c<0>(p); } template static typename element_const_reference_type

::type min_(const P& p) { return semantic_at_c<0>(p); } template static typename element_reference_type

::type min_( P& p) { return semantic_at_c<0>(p); } }; } // namespace detail /// \defgroup ColorBaseAlgorithmMinMax static_min, static_max /// \ingroup ColorBaseAlgorithm /// \brief Equivalents to std::min_element and std::max_element for homogeneous color bases /// /// Example: /// \code /// rgb8_pixel_t pixel(10,20,30); /// assert(pixel[2] == 30); /// static_max(pixel) = static_min(pixel); /// assert(pixel[2] == 10); /// \endcode /// \{ template BOOST_FORCEINLINE typename element_const_reference_type

::type static_max(const P& p) { return detail::min_max_recur::value>::max_(p); } template BOOST_FORCEINLINE typename element_reference_type

::type static_max( P& p) { return detail::min_max_recur::value>::max_(p); } template BOOST_FORCEINLINE typename element_const_reference_type

::type static_min(const P& p) { return detail::min_max_recur::value>::min_(p); } template BOOST_FORCEINLINE typename element_reference_type

::type static_min( P& p) { return detail::min_max_recur::value>::min_(p); } /// \} /// \defgroup ColorBaseAlgorithmEqual static_equal /// \ingroup ColorBaseAlgorithm /// \brief Equivalent to std::equal. Pairs the elements semantically /// /// Example: /// \code /// rgb8_pixel_t rgb_red(255,0,0); /// bgr8_pixel_t bgr_red(0,0,255); /// assert(rgb_red[0]==255 && bgr_red[0]==0); /// /// assert(static_equal(rgb_red,bgr_red)); /// assert(rgb_red==bgr_red); // operator== invokes static_equal /// \endcode /// \{ template BOOST_FORCEINLINE bool static_equal(const P1& p1, const P2& p2) { return detail::element_recursion::value>::static_equal(p1,p2); } /// \} /// \defgroup ColorBaseAlgorithmCopy static_copy /// \ingroup ColorBaseAlgorithm /// \brief Equivalent to std::copy. Pairs the elements semantically /// /// Example: /// \code /// rgb8_pixel_t rgb_red(255,0,0); /// bgr8_pixel_t bgr_red; /// static_copy(rgb_red, bgr_red); // same as bgr_red = rgb_red /// /// assert(rgb_red[0] == 255 && bgr_red[0] == 0); /// assert(rgb_red == bgr_red); /// \endcode /// \{ template BOOST_FORCEINLINE void static_copy(const Src& src, Dst& dst) { detail::element_recursion::value>::static_copy(src, dst); } /// \} /// \defgroup ColorBaseAlgorithmFill static_fill /// \ingroup ColorBaseAlgorithm /// \brief Equivalent to std::fill. /// /// Example: /// \code /// rgb8_pixel_t p; /// static_fill(p, 10); /// assert(p == rgb8_pixel_t(10,10,10)); /// \endcode /// \{ template BOOST_FORCEINLINE void static_fill(P& p, const V& v) { detail::element_recursion::value>::static_fill(p,v); } /// \} /// \defgroup ColorBaseAlgorithmGenerate static_generate /// \ingroup ColorBaseAlgorithm /// \brief Equivalent to std::generate. /// /// Example: Set each channel of a pixel to its semantic index. The channels must be assignable from an integer. /// \code /// struct consecutive_fn { /// int& _current; /// consecutive_fn(int& start) : _current(start) {} /// int operator()() { return _current++; } /// }; /// rgb8_pixel_t p; /// int start=0; /// static_generate(p, consecutive_fn(start)); /// assert(p == rgb8_pixel_t(0,1,2)); /// \endcode /// /// \{ template BOOST_FORCEINLINE void static_generate(P1& dst,Op op) { detail::element_recursion::value>::static_generate(dst,op); } /// \} /// \defgroup ColorBaseAlgorithmTransform static_transform /// \ingroup ColorBaseAlgorithm /// \brief Equivalent to std::transform. Pairs the elements semantically /// /// Example: Write a generic function that adds two pixels into a homogeneous result pixel. /// \code /// template /// struct my_plus { /// template /// Result operator()(T1 f1, T2 f2) const { return f1+f2; } /// }; /// /// template /// void sum_channels(const Pixel1& p1, const Pixel2& p2, Pixel3& result) { /// using result_channel_t = typename channel_type::type; /// static_transform(p1,p2,result,my_plus()); /// } /// /// rgb8_pixel_t p1(1,2,3); /// bgr8_pixel_t p2(3,2,1); /// rgb8_pixel_t result; /// sum_channels(p1,p2,result); /// assert(result == rgb8_pixel_t(2,4,6)); /// \endcode /// \{ //static_transform with one source template BOOST_FORCEINLINE Op static_transform(Src& src,Dst& dst,Op op) { return detail::element_recursion::value>::static_transform(src,dst,op); } template BOOST_FORCEINLINE Op static_transform(const Src& src,Dst& dst,Op op) { return detail::element_recursion::value>::static_transform(src,dst,op); } //static_transform with two sources template BOOST_FORCEINLINE Op static_transform(P2& p2,P3& p3,Dst& dst,Op op) { return detail::element_recursion::value>::static_transform(p2,p3,dst,op); } template BOOST_FORCEINLINE Op static_transform(P2& p2,const P3& p3,Dst& dst,Op op) { return detail::element_recursion::value>::static_transform(p2,p3,dst,op); } template BOOST_FORCEINLINE Op static_transform(const P2& p2,P3& p3,Dst& dst,Op op) { return detail::element_recursion::value>::static_transform(p2,p3,dst,op); } template BOOST_FORCEINLINE Op static_transform(const P2& p2,const P3& p3,Dst& dst,Op op) { return detail::element_recursion::value>::static_transform(p2,p3,dst,op); } /// \} /// \defgroup ColorBaseAlgorithmForEach static_for_each /// \ingroup ColorBaseAlgorithm /// \brief Equivalent to std::for_each. Pairs the elements semantically /// /// Example: Use static_for_each to increment a planar pixel iterator /// \code /// struct increment { /// template /// void operator()(Incrementable& x) const { ++x; } /// }; /// /// template /// void increment_elements(ColorBase& cb) { /// static_for_each(cb, increment()); /// } /// /// uint8_t red[2], green[2], blue[2]; /// rgb8c_planar_ptr_t p1(red,green,blue); /// rgb8c_planar_ptr_t p2=p1; /// increment_elements(p1); /// ++p2; /// assert(p1 == p2); /// \endcode /// \{ //static_for_each with one source template BOOST_FORCEINLINE Op static_for_each( P1& p1, Op op) { return detail::element_recursion::value>::static_for_each(p1,op); } template BOOST_FORCEINLINE Op static_for_each(const P1& p1, Op op) { return detail::element_recursion::value>::static_for_each(p1,op); } //static_for_each with two sources template BOOST_FORCEINLINE Op static_for_each(P1& p1, P2& p2, Op op) { return detail::element_recursion::value>::static_for_each(p1,p2,op); } template BOOST_FORCEINLINE Op static_for_each(P1& p1,const P2& p2, Op op) { return detail::element_recursion::value>::static_for_each(p1,p2,op); } template BOOST_FORCEINLINE Op static_for_each(const P1& p1, P2& p2, Op op) { return detail::element_recursion::value>::static_for_each(p1,p2,op); } template BOOST_FORCEINLINE Op static_for_each(const P1& p1,const P2& p2, Op op) { return detail::element_recursion::value>::static_for_each(p1,p2,op); } //static_for_each with three sources template BOOST_FORCEINLINE Op static_for_each(P1& p1,P2& p2,P3& p3,Op op) { return detail::element_recursion::value>::static_for_each(p1,p2,p3,op); } template BOOST_FORCEINLINE Op static_for_each(P1& p1,P2& p2,const P3& p3,Op op) { return detail::element_recursion::value>::static_for_each(p1,p2,p3,op); } template BOOST_FORCEINLINE Op static_for_each(P1& p1,const P2& p2,P3& p3,Op op) { return detail::element_recursion::value>::static_for_each(p1,p2,p3,op); } template BOOST_FORCEINLINE Op static_for_each(P1& p1,const P2& p2,const P3& p3,Op op) { return detail::element_recursion::value>::static_for_each(p1,p2,p3,op); } template BOOST_FORCEINLINE Op static_for_each(const P1& p1,P2& p2,P3& p3,Op op) { return detail::element_recursion::value>::static_for_each(p1,p2,p3,op); } template BOOST_FORCEINLINE Op static_for_each(const P1& p1,P2& p2,const P3& p3,Op op) { return detail::element_recursion::value>::static_for_each(p1,p2,p3,op); } template BOOST_FORCEINLINE Op static_for_each(const P1& p1,const P2& p2,P3& p3,Op op) { return detail::element_recursion::value>::static_for_each(p1,p2,p3,op); } template BOOST_FORCEINLINE Op static_for_each(const P1& p1,const P2& p2,const P3& p3,Op op) { return detail::element_recursion::value>::static_for_each(p1,p2,p3,op); } ///\} } } // namespace boost::gil #endif