Boost logo

Boost :

Subject: [boost] [GIL] default_color_converter from float to integer should clamp
From: Fabien Castan (fabcastan_at_[hidden])
Date: 2012-10-14 07:33:50


Hi,

The conversion from floating point image to 8 bits or 16 bits image should
clamp instead of writing arbitrary values...

// a floating point image, with some pixel values < 0.0 and > 1.0
boost::gil::rgba32f_view_t HDR;

// use default_color_converter to convert this HDR image to 8 bits
boost::gil::png_write_view(
"LDR.png", boost::gil::color_converted_view<boost::gil::rgba8_pixel_t>( HDR
) );

The 8 bits LDR image contains arbitrary cast conversion values, that are
not very nice. The conversion from floating point to integer values has no
other valid solution than clamping.
So I suggest this patch, for the file "boost/gil/channel_algorithm.hpp":

@@ -272,7 +272,13 @@
template <typename DstChannelV> struct
channel_converter_unsigned<bits32f,DstChannelV> : public
std::unary_function<bits32f,DstChannelV> {
     DstChannelV operator()(bits32f x) const
     {
         typedef typename detail::unsigned_integral_max_value< DstChannelV
>::value_type dst_integer_t;
- return DstChannelV( static_cast< dst_integer_t
>(x*channel_traits<DstChannelV>::max_value()+0.5f ));
+
+ const bits32f convertedValue = x *
channel_traits<DstChannelV>::max_value() + 0.5f;
+ const bits32f clampedValue = std::min(
+ (bits32f)channel_traits<DstChannelV>::max_value(),
+ std::max(
(bits32f)channel_traits<DstChannelV>::min_value(), convertedValue ) );
+
+ return DstChannelV( static_cast< dst_integer_t >(clampedValue) );
     }
 };

Regards,
Fabien Castan


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk