[GIL] default_color_converter from float to integer should clamp
14 Oct
2012
14 Oct
'12
1:33 p.m.
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
15 Oct
15 Oct
10:15 a.m.
New subject: [GIL] default_color_converter from float to integer should clamp
On Sun, Oct 14, 2012 at 1:33 PM, Fabien Castan <fabcastan@gmail.com> wrote:
+ 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) ); } };
Is a clamp function not available? boost::algorithm has one -- Olaf
3:06 p.m.
New subject: [GIL] default_color_converter from float to integer should clamp
Hi Fabian,
thanks for your patch. I think the default converter was created with
floating point values between 0 and 1 in mind. I suggest you don't use
the default converter but just create a new one. It fairly easy to do
so.
I could imagine to create a clamping default converter for the toolbox.
Thanks,
Christian
On Sun, Oct 14, 2012 at 7:33 AM, Fabien Castan <fabcastan@gmail.com> wrote:
> 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
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
4765
Age (days ago)
4766
Last active (days ago)
2 comments
3 participants
participants (3)
-
Christian Henning -
Fabien Castan -
Olaf van der Spek