| 1 | // |
| 2 | // Copyright 2005-2007 Adobe Systems Incorporated |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0 |
| 5 | // See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt |
| 7 | // |
| 8 | |
| 9 | #include <boost/gil/extension/io/jpeg.hpp> |
| 10 | |
| 11 | // Demonstrates how to compute gradients along the x-axis |
| 12 | // This example converts the input image to a greyscale view via color_converted_view, |
| 13 | // and then relies on the function static_transform to apply the operation halfdiff_cast_channels. |
| 14 | // The result is captured in a view, initially blacked out via a call to fill_pixels (defined in |
| 15 | // include/boost/gil/algorithm.hpp) |
| 16 | // static_transform is defined in include/boost/gil/color_based_algorithm.hpp and applies an operation |
| 17 | // to either a single source or two sources and a destination (as is the case here). |
| 18 | // In this example, the gradient is calculated as half the difference between the two pixels surrounding x |
| 19 | // in the loop in x_gradient. |
| 20 | |
| 21 | using namespace boost::gil; |
| 22 | |
| 23 | template <typename Out> |
| 24 | struct halfdiff_cast_channels { |
| 25 | template <typename T> Out operator()(const T& in1, const T& in2) const { |
| 26 | return Out((in2-in1)/2); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | template <typename SrcView, typename DstView> |
| 32 | void x_gradient(SrcView const& src, DstView const& dst) |
| 33 | { |
| 34 | using dst_channel_t = typename channel_type<DstView>::type; |
| 35 | |
| 36 | for (int y = 0; y < src.height(); ++y) |
| 37 | { |
| 38 | typename SrcView::x_iterator src_it = src.row_begin(y); |
| 39 | typename DstView::x_iterator dst_it = dst.row_begin(y); |
| 40 | |
| 41 | for (int x = 1; x < src.width() - 1; ++x) |
| 42 | { |
| 43 | static_transform(src_it[x - 1], src_it[x + 1], dst_it[x], |
| 44 | halfdiff_cast_channels<dst_channel_t>()); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | template <typename SrcView, typename DstView> |
| 50 | void x_luminosity_gradient(SrcView const& src, DstView const& dst) |
| 51 | { |
| 52 | using gray_pixel_t = pixel<typename channel_type<SrcView>::type, gray_layout_t>; |
| 53 | x_gradient(color_converted_view<gray_pixel_t>(src), dst); |
| 54 | } |
| 55 | |
| 56 | int main() |
| 57 | { |
| 58 | rgb8_image_t img; |
| 59 | read_image(file_name: "test.jpg" ,img, tag: jpeg_tag{}); |
| 60 | |
| 61 | gray8s_image_t img_out(img.dimensions()); |
| 62 | fill_pixels(view: view(img&: img_out),value: int8_t(0)); |
| 63 | |
| 64 | x_luminosity_gradient(src: const_view(img), dst: view(img&: img_out)); |
| 65 | write_view(file_name: "out-x_gradient.jpg" ,view: color_converted_view<gray8_pixel_t>(src: const_view(img: img_out)), tag: jpeg_tag{}); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |