Boost logo

Boost :

From: Tom Brinkman (reportbase_at_[hidden])
Date: 2008-06-17 14:57:37


There have been thread before about doing fast alpha blends with GIL
views. Here is what I settled with.

boost::gil::gray8c_view_t grayview =
          boost::gil::interleaved_view(width,height,(gray8_pixel_t*)buffer,width);

copy_view(pixel(0,0,0),grayview,someview);

struct alpha_blend
{
        short alpha;
        alpha_blend(short alpha) : alpha(alpha){}

        template <typename T>
        void operator()(T& dst, const T src)
        {
                dst = ((dst + 1) * alpha >> 8) -
                        ((src + 1) * alpha >> 8) +
                                src;
        }
};

template <typename pixel_t, typename grayview_t, typename view_t> inline
void copy_view(pixel_t pixel, const grayview_t& grayview, const view_t& view)
{
        using namespace boost::gil;
        BOOST_ASSERT(grayview.width() == view.width());

        typedef typename view_t::x_iterator x_iterator_t;
        typedef typename grayview_t::x_iterator x_iterator2_t;
        
        for (int y = 0; y < view.height(); ++y)
        {
                x_iterator_t it_view = view.row_begin(y);
                x_iterator2_t it_gray = grayview.row_begin(y);
                for (int x = 0; x < view.width(); ++x)
                {
                        pixel_t dst = pixel;
                        static_for_each(dst, it_view[x], alpha_blend(it_gray[x]));
                        it_view[x] = dst;
                }
        }
}


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