Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conversion from RGB to signed CMYK #522

Merged
merged 6 commits into from
Oct 10, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions include/boost/gil/color_convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,44 +150,46 @@ 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 g = get_color(src, green_t());
src_t const b = get_color(src, blue_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());
mloskot marked this conversation as resolved.
Show resolved Hide resolved

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 dst_us_t = typename channel_type< cmyk8_pixel_t >::type;
mloskot marked this conversation as resolved.
Show resolved Hide resolved
dst_us_t c = channel_invert(channel_convert<dst_us_t>(r)); // c = 1 - r
dst_us_t m = channel_invert(channel_convert<dst_us_t>(g)); // m = 1 - g
dst_us_t y = channel_invert(channel_convert<dst_us_t>(b)); // y = 1 - b
dst_us_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;
dst_us_t const dst_max = channel_traits<dst_us_t>::max_value();
dst_us_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<dst_us_t>::min_value();
m = channel_traits<dst_us_t>::min_value();
y = channel_traits<dst_us_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);
}
};

Expand Down