Boost logo

Boost :

From: Lubomir Bourdev (lbourdev_at_[hidden])
Date: 2006-10-12 15:38:46

Several people suggested to me in email that I should post some GIL
sample code, so they get a feel of what GIL is like.

My biggest recommendation: If you write a review, please spend an hour
to watch the GIL video presentation. That will give you a better feel
for GIL.


You can also get extra sample GIL code snippets here:

- in the tutorial (the Mandelbrot view section is not in the

- section 15 of the design guide





Here are a few more:




// To fill a view with blue

template <typename View>

void fill_blue(const View& view) {

    typename View::pixel_t blue;





// To fill the top left quadrant of the image with blue

void fill_blue_quadrant() {

    rgb8_image_t img;






// Find the location of the grayscale pixel with highest magnitude

// (or the first in a top-bottom left-right traversal, if multiple)

template <typename GView> // Models Grayscale View

typename GView::point_t max_gray_channel_coords(const GView& view) {


    typename GView::difference_type index =
std::max_element(view.begin(),view.end()) - view.begin();

    return typename GView::point_t(index % view.width(), index /



// Find the location of the highest-intensity pixel in the view

template <typename View> // Models ImageViewConcept

typename View::point_t max_intensity_coords(const View& view) {




// Find the location in the grayscale view with the largest horizontal
gradient magnitude

template <typename GView> // Models grayscale image view

typename GView::point_t max_gray_xgradient_coords(const GView& src) {

    typedef typename GView::channel_t channel_t;


    // the type of a grayscale image with the same channel as src

    typedef typename image_type<channel_t, gray_t>::type grad_image_t;


    // create a temp image of the same size as src

    grad_image_t buffer(get_dimensions(src));


    // clear it (our sample x_gradient code doesn't touch the first and
last column)



    // call the generic x_gradient (see the tutorial for the code)

    x_gradient(src, view(buffer));


    return max_gray_channel_coords(const_view(buffer));



void test_max_gradient() {

    typedef point2<std::ptrdiff_t> point_t;

    rgb8_planar_image_t img;



    point_t coords =

    cout << "Largest horizontal gradient of the RED CHANNEL:


    coords =

    cout << "Largest horizontal gradient of the LUMINOSITY:




Of course, the above functions like fill_blue, max_intensity_coords are
fully generic. They work with images in any color space, any channel
depth, interleaved and planar. Putting them in function objects will
make them work for images with run-time specified types.




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