#include <cassert>
#include <boost/gil/gil_all.hpp>
namespace gil = boost::gil;
// Our objective here is simple -- write a generic algorithm
// that set the red channel to max. Let's assume 8-bit depth for now.
// This only works for rgb8, bgr8
// This feels cleaner than the for_each_pixel approach below
// Question: I wonder if there's a way to make this work?
template <typename SrcView>
void boost_red(const SrcView & view)
{
typedef gil::nth_channel_view_type<SrcView>::type NView;
// XXX This will incorrectly overwrite the alpha channel
// instead of the red channel if SrcView is something
// like argb8_view_t
NView nview = gil::nth_channel_view(view, 0);
gil::fill_pixels(nview, NView::value_type(255));
}
// Alternative implementation with get_color()
// This works for argb8, rgb8, bgr8, etc.
template <typename SrcView>
void boost_red_with_get_color(const SrcView & view)
{
gil::for_each_pixel(view, [](SrcView::value_type & p)
{
gil::get_color(p, gil::red_t()) = 255;
});
}
int main()
{
// XXX This compiles (MSVC10 and G++ 4.5.1), why?
gil::gil_function_requires<gil::ImageViewConcept<int> >();
{
gil::rgb8_image_t img(1, 1);
gil::rgb8_view_t view = gil::view(img);
gil::fill_pixels(view, gil::rgb8_pixel_t(0, 0, 0));
boost_red(view);
assert((view.pixels()[0] == gil::rgb8_pixel_t(255, 0, 0)));
}
{
gil::argb8_image_t img(1, 1);
gil::argb8_view_t view = gil::view(img);
gil::fill_pixels(view, gil::argb8_pixel_t(255, 0, 0, 0));
// Doesn't work -- boost_red will overwrite the alpha channel
boost_red(view);
// ka-boom!
assert((view.pixels()[0] == gil::argb8_pixel_t(255, 255, 0, 0)));
}
return 0;
}