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 invalid conversion from RGB8 to CMYK32 due to overflow #470

Merged
merged 1 commit into from
Apr 10, 2020

Conversation

mloskot
Copy link
Member

@mloskot mloskot commented Mar 31, 2020

Description

Correct calculation to correctly map CMYK 0 to minimum and 1 to maximum of input and output channel types.

If float-point division is necessary, use double instead of float which may be too narrow for large operands. cmyk32_pixel_t is based on uint32_t which multiplication by its maximum may yield result too large to fit 32-bit float.

For example, (uint32_t(c) - uint32_t(k)) * float(s) forc = 4294967295, k = 0, s = 1.0
results in 4294967300 value which does not fit uint32_t.

FIXME: The original calculation was broken for signed CMYK target types. This has not been corrected yet, see #479

References

Tasklist

  • Add test case(s) - currently there are no related tests, to be added
  • Ensure all CI builds pass
  • Review and approve

@mloskot mloskot added cat/bug But reports and bug fixes core boost/gil status/work-in-progress Do NOT merge yet until this label has been removed! labels Mar 31, 2020
@mloskot mloskot force-pushed the ml/fix-rgb-to-cmyk32-fp-overflow branch from f8ded25 to 0e0e5b1 Compare March 31, 2020 00:56
include/boost/gil/color_convert.hpp Outdated Show resolved Hide resolved
include/boost/gil/color_convert.hpp Outdated Show resolved Hide resolved
@mloskot mloskot changed the title Fix overflow in RGB to CMYK32 conversion Fix invalid conversion from RGB8 to CMYK32 due to overflow Mar 31, 2020
@mloskot mloskot force-pushed the ml/fix-rgb-to-cmyk32-fp-overflow branch 4 times, most recently from 4390df4 to bc52e32 Compare April 1, 2020 00:16
@mloskot mloskot added status/work-in-progress Do NOT merge yet until this label has been removed! and removed status/work-in-progress Do NOT merge yet until this label has been removed! labels Apr 1, 2020
@mloskot
Copy link
Member Author

mloskot commented Apr 1, 2020

The fix is still not correct as it does not convert well for negative values of signed pixel types like rgb8s_pixel_t. Use of signed integer for pixel components is extremely rare, but since the conversion is fairly type agnostic, it needs to be fixed.

Basically, RGB color value [-128, 127] should be normalized to generic CMYK [0, 1], then 0 should map to minimum and 1 to maximum of target channel type of cmykN[s]_pixel_t.

Correct calculation to correctly map CMYK 0 to minimum
and 1 to maximum of input and output channel types.

If float-point division is necessary, use double instead of float
which may be too narrow for large operands. cmyk32_pixel_t is based on
uint32_t which multiplication by its maximum may yield result too
large to fit 32-bit float.

For example, (uint32_t(c) -  uint32_t(k)) * float(s) for
  c = 4294967295, k = 0, s = 1.0
results in 4294967300 value which does not fit uint32_t.

Fixes boostorg#406
@mloskot mloskot force-pushed the ml/fix-rgb-to-cmyk32-fp-overflow branch from bc52e32 to e545e38 Compare April 9, 2020 00:00
@mloskot mloskot removed the status/work-in-progress Do NOT merge yet until this label has been removed! label Apr 9, 2020
@mloskot mloskot marked this pull request as draft April 9, 2020 21:39
@mloskot mloskot marked this pull request as ready for review April 9, 2020 21:39
@mloskot
Copy link
Member Author

mloskot commented Apr 10, 2020

This just fixes the #406 and conversion issues to signed CMYK received separate issue #479

@mloskot mloskot added this to the Boost 1.74+ milestone Apr 10, 2020
@mloskot mloskot merged commit 446c1a2 into boostorg:develop Apr 10, 2020
@mloskot mloskot deleted the ml/fix-rgb-to-cmyk32-fp-overflow branch April 10, 2020 17:12
mloskot added a commit that referenced this pull request Jul 11, 2020
* develop:
  Collect release notes for Boost 1.74
  Fix missing header in numeric/kernel.hpp to make it self-contained (#502)
  Use perfect forwading from apply_operation to visit (#491)
  Fix typos and replace mpl with mp11 in tutorial (#494)
  Implemented mechanism to reverse kernel_2d (#489)
  added missing const& in extend_boundary parameters (#490)
  Add initializing image constructor (#486)
  Fix interleaved_view factory using point<std::ptrdiff_t> for dimension (#487)
  RELEASES.md: Remove beta tag [ci skip]
  Replace Boost.Variant with Boost.Variant2 (#474)
  Fix error plane_view_t is not a class or namespace name (#481)
  Fix image constructor from other image (#477)
  Fix overflow in RGB to CMYK32 conversion (#470)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat/bug But reports and bug fixes core boost/gil
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid conversion from RGB8 to CMYK32
2 participants