Skip to content

Commit

Permalink
Fix conversion of rgb to signed cmyk(#479) (#522)
Browse files Browse the repository at this point in the history
* Fix conversion of rgb to signed cmyk(#479)

* changed naming of dst_us_t to uint_t and undid the formatting change

* test for conversion of rgb to cmyk, PR #522.

* small formatting changes/fixes

* Build configuration update for PR #522

* removed unused header file
  • Loading branch information
harshitpant1 authored Oct 10, 2020
1 parent f6a3553 commit f7e83e0
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 19 deletions.
39 changes: 21 additions & 18 deletions include/boost/gil/color_convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,47 +150,50 @@ struct default_color_converter_impl<rgb_t,gray_t> {
/// by Burger, Wilhelm, Burge, Mark J.
/// and it is a gross approximation not precise enough for professional work.
///
/// \todo FIXME: The original implementation did not handle properly signed CMYK pixels as destination
///
template <>
struct default_color_converter_impl<rgb_t, cmyk_t>
{
template <typename SrcPixel, typename DstPixel>
void operator()(SrcPixel const& src, DstPixel& dst) const
{
using src_t = typename channel_type<SrcPixel>::type;
src_t const r = get_color(src, red_t());
src_t const r = get_color(src, red_t());
src_t const g = get_color(src, green_t());
src_t const b = get_color(src, blue_t());

using dst_t = typename channel_type<DstPixel>::type;
dst_t const c = channel_invert(channel_convert<dst_t>(r)); // c = 1 - r
dst_t const m = channel_invert(channel_convert<dst_t>(g)); // m = 1 - g
dst_t const y = channel_invert(channel_convert<dst_t>(b)); // y = 1 - b
dst_t const k = (std::min)(c, (std::min)(m, y)); // k = minimum(c, m, y)
using uint_t = typename channel_type<cmyk8_pixel_t>::type;
uint_t c = channel_invert(channel_convert<uint_t>(r)); // c = 1 - r
uint_t m = channel_invert(channel_convert<uint_t>(g)); // m = 1 - g
uint_t y = channel_invert(channel_convert<uint_t>(b)); // y = 1 - b
uint_t k = (std::min)(c,(std::min)(m,y)); // k = minimum(c, m, y)

// Apply color correction, strengthening, reducing non-zero components by
// s = 1 / (1 - k) for k < 1, where 1 denotes dst_t max, otherwise s = 1 (literal).
dst_t const dst_max = channel_traits<dst_t>::max_value();
dst_t const s_div = dst_max - k;
uint_t const dst_max = channel_traits<uint_t>::max_value();
uint_t const s_div = dst_max - k;
if (s_div != 0)
{
double const s = dst_max / static_cast<double>(s_div);
get_color(dst, cyan_t()) = static_cast<dst_t>((c - k) * s);
get_color(dst, magenta_t()) = static_cast<dst_t>((m - k) * s);
get_color(dst, yellow_t()) = static_cast<dst_t>((y - k) * s);
double const s = dst_max / static_cast<double>(s_div);
c = (c - k) * s;
m = (m - k) * s;
y = (y - k) * s;
}
else
{
// Black only for k = 1 (max of dst_t)
get_color(dst, cyan_t()) = channel_traits<dst_t>::min_value();
get_color(dst, magenta_t()) = channel_traits<dst_t>::min_value();
get_color(dst, yellow_t()) = channel_traits<dst_t>::min_value();
c = channel_traits<uint_t>::min_value();
m = channel_traits<uint_t>::min_value();
y = channel_traits<uint_t>::min_value();
}
get_color(dst, black_t()) = k;
using dst_t = typename channel_type<DstPixel>::type;
get_color(dst, cyan_t()) = channel_convert<dst_t>(c);
get_color(dst, magenta_t()) = channel_convert<dst_t>(m);
get_color(dst, yellow_t()) = channel_convert<dst_t>(y);
get_color(dst, black_t()) = channel_convert<dst_t>(k);
}
};


/// \ingroup ColorConvert
/// \brief CMYK to RGB (not the fastest code in the world)
///
Expand Down
3 changes: 2 additions & 1 deletion test/core/color/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
foreach(_name
concepts
color_spaces_are_compatible
default_color_converter_impl)
default_color_converter_impl
default_color_converter_rgb_to_cmyk)
set(_test t_core_color_${_name})
set(_target test_core_color_${_name})

Expand Down
1 change: 1 addition & 0 deletions test/core/color/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ compile color_spaces_are_compatible.cpp ;
compile-fail default_color_converter_impl_fail.cpp ;

run default_color_converter_impl.cpp ;
run default_color_converter_rgb_to_cmyk.cpp ;
Loading

0 comments on commit f7e83e0

Please sign in to comment.