GIL - Fast Alpha Blends

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; } } }

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*)buff
er,width);
...
dst = ((dst + 1) * alpha >> 8) - ((src + 1) * alpha >> 8) + src;
I think that is specific to 8bit channels. What I think is needed is some kind of support for scaled integers, so that alpha blending (or other numeric ops) can be done efficiently for channels that are scaled integers as well as for other types such as float. --John
participants (2)
-
John Femiani
-
Tom Brinkman