-
Notifications
You must be signed in to change notification settings - Fork 165
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
mloskot
merged 6 commits into
boostorg:develop
from
harshitpant1:bugfix/fixed-conversion-of-rgb-to-signed-cmyk-issue#479
Oct 10, 2020
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
01d9599
Fix conversion of rgb to signed cmyk(boostorg#479)
harshitpant1 3f54d00
changed naming of dst_us_t to uint_t and undid the formatting change
harshitpant1 463f831
test for conversion of rgb to cmyk, PR #522.
harshitpant1 576af18
small formatting changes/fixes
harshitpant1 7a83e80
Build configuration update for PR #522
harshitpant1 8b11d70
removed unused header file
harshitpant1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: missing space before |
||
|
||
// 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) | ||
/// | ||
|
325 changes: 325 additions & 0 deletions
325
test/core/color/default_color_converter_rgb_to_cmyk.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,325 @@ | ||
#include <boost/gil.hpp> | ||
#include <boost/gil/color_convert.hpp> | ||
#include <boost/gil/rgb.hpp> | ||
#include <boost/gil/cmyk.hpp> | ||
#include <boost/mp11.hpp> | ||
#include <boost/core/lightweight_test.hpp> | ||
#include <type_traits> | ||
|
||
#include "test_utility_output_stream.hpp" | ||
#include "test_fixture.hpp" | ||
|
||
namespace gil = boost::gil; | ||
|
||
void test_unsigned_rgb_to_unsigned_cmyk() | ||
{ | ||
// black | ||
{ | ||
gil::rgb8_pixel_t src{0, 0, 0}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{0,0,0,255}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
// white | ||
{ | ||
gil::rgb8_pixel_t src{255, 255, 255}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{0, 0, 0, 0}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
// blue | ||
{ | ||
gil::rgb8_pixel_t src{0, 0, 255}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{255, 255, 0, 0}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
// yellow | ||
{ | ||
gil::rgb8_pixel_t src{255, 255, 0}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{0, 0, 255, 0}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 1 | ||
{ | ||
gil::rgb8_pixel_t src{1, 2, 3}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{170, 85, 0, 252}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 2 | ||
{ | ||
gil::rgb8_pixel_t src{11, 117, 250}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{243, 135, 0, 5}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 3 | ||
{ | ||
gil::rgb8_pixel_t src{230, 45, 100}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{0, 205, 144, 25}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 4 | ||
{ | ||
gil::rgb8_pixel_t src{200, 255, 53}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{55, 0, 202, 0}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
} | ||
|
||
void test_signed_rgb_to_unsigned_cmyk() | ||
{ | ||
// black | ||
{ | ||
gil::rgb8s_pixel_t src{-128, -128, -128}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{0,0,0,255}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
// white | ||
{ | ||
gil::rgb8s_pixel_t src{127, 127, 127}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{0, 0, 0, 0}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
// blue | ||
{ | ||
gil::rgb8s_pixel_t src{-128, -128, 127}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{255, 255, 0, 0}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
// yellow | ||
{ | ||
gil::rgb8s_pixel_t src{127, 127, -128}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{0, 0, 255, 0}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 1 | ||
{ | ||
gil::rgb8s_pixel_t src{-127, -126, -125}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{170, 85, 0, 252}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 2 | ||
{ | ||
gil::rgb8s_pixel_t src{-117, -11, 122}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{243, 135, 0, 5}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 3 | ||
{ | ||
gil::rgb8s_pixel_t src{102, -83, -28}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{0, 205, 144, 25}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 4 | ||
{ | ||
gil::rgb8s_pixel_t src{72, 127, -75}; | ||
gil::cmyk8_pixel_t dst; | ||
gil::cmyk8_pixel_t expected_dst{55, 0, 202, 0}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
} | ||
|
||
void test_unsigned_rgb_to_signed_cmyk() | ||
{ | ||
// black | ||
{ | ||
gil::rgb8_pixel_t src{0, 0, 0}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{-128, -128, -128, 127}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
// white | ||
{ | ||
gil::rgb8_pixel_t src{255, 255, 255}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{-128, -128, -128, -128}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
// blue | ||
{ | ||
gil::rgb8_pixel_t src{0, 0, 255}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{127, 127, -128, -128}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
// yellow | ||
{ | ||
gil::rgb8_pixel_t src{255, 255, 0}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{-128, -128, 127, -128}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 1 | ||
{ | ||
gil::rgb8_pixel_t src{1, 2, 3}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{42, -43, -128, 124}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 2 | ||
{ | ||
gil::rgb8_pixel_t src{11, 117, 250}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{115, 7, -128, -123}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 3 | ||
{ | ||
gil::rgb8_pixel_t src{230, 45, 100}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{-128, 77, 16, -103}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 4 | ||
{ | ||
gil::rgb8_pixel_t src{200, 255, 53}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{-73, -128, 74, -128}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
} | ||
|
||
void test_signed_rgb_to_signed_cmyk() | ||
{ | ||
// black | ||
{ | ||
gil::rgb8s_pixel_t src{-128, -128, -128}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{-128,-128,-128, 127}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
// white | ||
{ | ||
gil::rgb8s_pixel_t src{127, 127, 127}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{-128, -128, -128, -128}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
// blue | ||
{ | ||
gil::rgb8s_pixel_t src{-128, -128, 127}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{127, 127, -128, -128}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
// yellow | ||
{ | ||
gil::rgb8s_pixel_t src{127, 127, -128}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{-128, -128, 127, -128}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 1 | ||
{ | ||
gil::rgb8s_pixel_t src{-127, -126, -125}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{42, -43, -128, 124}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 2 | ||
{ | ||
gil::rgb8s_pixel_t src{-117, -11, 122}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{115, 7, -128, -123}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 3 | ||
{ | ||
gil::rgb8s_pixel_t src{102, -83, -28}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{-128, 77, 16, -103}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
//random pixel values 4 | ||
{ | ||
gil::rgb8s_pixel_t src{72, 127, -75}; | ||
gil::cmyk8s_pixel_t dst; | ||
gil::cmyk8s_pixel_t expected_dst{-73, -128, 74, -128}; | ||
|
||
gil::color_convert(src, dst); | ||
BOOST_TEST_EQ(dst, expected_dst); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
test_unsigned_rgb_to_unsigned_cmyk(); | ||
test_signed_rgb_to_unsigned_cmyk(); | ||
test_unsigned_rgb_to_signed_cmyk(); | ||
test_signed_rgb_to_signed_cmyk(); | ||
return ::boost::report_errors(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: trailing space after
r
should be removed