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
video)

- section 15 of the design guide

- http://opensource.adobe.com/gil/gil_sample_code.cpp

- http://opensource.adobe.com/gil/numeric_example.cpp

- http://opensource.adobe.com/gil/html/beforeafterexample.html

 

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;

    color_convert(rgb8_pixel_t(0,0,255),blue);

    fill_pixels(view,blue);

}

 

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

void fill_blue_quadrant() {

    rgb8_image_t img;

    jpeg_read_image("test.jpg",img);

    fill_blue(subimage_view(view(img),0,0,img.width()/2,
img.height()/2));

    jpeg_write_view("blue_box.jpg",const_view(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) {

    assert(view.size()>0);

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

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

}

 

// 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) {

    return
max_gray_channel_coords(color_converted_view<gray32f_pixel_t>(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)

    fill_pixels(view(buffer),channel_t(0));

    

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

    jpeg_read_image("test.jpg",img);

 

    point_t coords =
max_gray_xgradient_coords(nth_channel_view(const_view(img),0));

    cout << "Largest horizontal gradient of the RED CHANNEL:
("<<coords.x<<","<<coords.y<<")\n";

 

    coords =
max_gray_xgradient_coords(color_converted_view<gray32f_pixel_t>(const_vi
ew(img)));

    cout << "Largest horizontal gradient of the LUMINOSITY:
("<<coords.x<<","<<coords.y<<")\n";

}

 

 

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.

 

Lubomir

 


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