Boost logo

Boost :

From: Lubomir Bourdev (lbourdev_at_[hidden])
Date: 2006-10-17 19:42:56

Tim Shead wrote:
> Suppose that
> I actually want to convert floating-point RGBA data to 8bpp
> integer RGB (I'm going to display the alpha separately, say).
> ... <skipped>
> How would you handle this on a case-by-case basis?

Hi Tim,

This is described in Section 16 in the design guide. You can create your
own color converters with arbitrary conversion policy. If you want to
change the color conversion in only a few cases, you simply subclass
GIL's default color converter. Color conversion functions take an
optional color conversion object. Pass your converter there.



// make the default use GIL's default
template <typename T1, typename C1, // source channel type and
base color space
          typename T2, typename C2> // destination channel type and
base color space
struct my_color_converter_impl : public
color_converter_default_impl<T1,C1,T2,C2> {

// provide specializations only for cases you care about
template <typename T1, typename T2>
struct my_color_converter_impl<T1,rgba_t,T2,rgb_t> {
    template <typename P1, typename P2>
    void operator()(const P1& src, P2& dst) const { = channel_convert<T2>(; = channel_convert<T2>(; = channel_convert<T2>(;

// create a color converter object that dispatches to your own
struct my_color_converter {
    template <typename SrcP,typename DstP>
    void operator()(const SrcP& src,DstP& dst) const {
        typedef typename pixel_traits<SrcP>::channel_t SrcChannel;
        typedef typename pixel_traits<DstP>::channel_t DstChannel;
        typedef typename pixel_traits<SrcP>::color_space_t::base
        typedef typename pixel_traits<DstP>::color_space_t::base

int main(int argc, unsigned char* argv[])
    rgba8_image_t a(100, 100);
    rgb8_image_t b(100, 100);
    copy_and_convert_pixels(const_view(a), view(b),

Boost list run by bdawes at, gregod at, cpdaniel at, john at