EsoErik

Monday, April 25, 2011

 

Overriding boost::gil's from-RGBA Color Converter

When converting to formats without an alpha channel, the Boost Generic Image Library interprets RGBA images as using premultiplied alpha. An application that I am writing stores non-graphical data in the alpha channel. Converting from such images to formats without an alpha channel requires simply discarding alpha channel data, rather than using it to reverse premultiplication of the RGB channels. To do this in a generic manner that overrides only from-RGBA conversion, I wrote the following custom conversion functor:

// Default to color converters provided by GIL
template<typename TSrcColorSpace, typename TDstColorSpace>
struct ConvertRGBA_impl
: public default_color_converter_impl<TSrcColorSpace, TDstColorSpace> {};

// Override GIL's from-RGBA color converter. GIL's from-RGBA color converter assumes premultiplied alpha, which we do not
// use.
template<typename TDstColorSpace>
struct ConvertRGBA_impl<rgba_t, TDstColorSpace>
{
template<typename TSrcPixel, typename TDstPixel>
void operator () (const TSrcPixel& srcPixel, TDstPixel& dstPixel) const;
};

struct ConvertRGBA
{
template<typename TSrcPixel, typename TDstPixel>
void operator () (const TSrcPixel& srcPixel, TDstPixel& dstPixel) const;
};

template<typename TDstColorSpace>
template<typename TSrcPixel, typename TDstPixel>
void ConvertRGBA_impl<rgba_t, TDstColorSpace>::operator () (const TSrcPixel& srcPixel, TDstPixel& dstPixel) const
{
default_color_converter_impl<rgb_t, TDstColorSpace>()
(
pixel<typename channel_type<TSrcPixel>::type, rgb_layout_t>
(
get_color(srcPixel, red_t()),
get_color(srcPixel, green_t()),
get_color(srcPixel, blue_t())
),
dstPixel
);
}

template<typename TSrcPixel, typename TDstPixel>
void ConvertRGBA::operator () (const TSrcPixel& srcPixel, TDstPixel& dstPixel) const
{
typedef typename color_space_type<TSrcPixel>::type TSrcColorSpace;
typedef typename color_space_type<TDstPixel>::type TDstColorSpace;
ConvertRGBA_impl<TSrcColorSpace, TDstColorSpace>()(srcPixel, dstPixel);
}

This may be used, for example, by calling copy_and_convert_pixels with ConvertRGBA() supplied as the third parameter.

Labels:


Archives

July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   September 2010   December 2010   January 2011   February 2011   April 2011   June 2011   August 2011   February 2012   June 2012   July 2012   August 2012   October 2012   November 2012   January 2014   April 2014   June 2014   August 2014   September 2014   October 2014   January 2015   March 2015   April 2015   June 2015   November 2015   December 2015   January 2016   June 2016   August 2016   January 2017   March 2017   April 2018   April 2019   June 2019   January 2020  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]