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 #include <boost/gil/extension/io/jpeg.hpp>
9
10 // Example to demonstrate a way to compute gradients along x-axis
11
12 using namespace boost::gil;
13
14 template <typename Out>
15 struct halfdiff_cast_channels {
operator ()halfdiff_cast_channels16 template <typename T> Out operator()(const T& in1, const T& in2) const {
17 return Out((in2-in1)/2);
18 }
19 };
20
21
22 template <typename SrcView, typename DstView>
x_gradient(SrcView const & src,DstView const & dst)23 void x_gradient(SrcView const& src, DstView const& dst)
24 {
25 using dst_channel_t = typename channel_type<DstView>::type;
26
27 for (int y = 0; y < src.height(); ++y)
28 {
29 typename SrcView::x_iterator src_it = src.row_begin(y);
30 typename DstView::x_iterator dst_it = dst.row_begin(y);
31
32 for (int x = 1; x < src.width() - 1; ++x)
33 {
34 static_transform(src_it[x - 1], src_it[x + 1], dst_it[x],
35 halfdiff_cast_channels<dst_channel_t>());
36 }
37 }
38 }
39
40 template <typename SrcView, typename DstView>
x_luminosity_gradient(SrcView const & src,DstView const & dst)41 void x_luminosity_gradient(SrcView const& src, DstView const& dst)
42 {
43 using gray_pixel_t = pixel<typename channel_type<SrcView>::type, gray_layout_t>;
44 x_gradient(color_converted_view<gray_pixel_t>(src), dst);
45 }
46
main()47 int main()
48 {
49 rgb8_image_t img;
50 read_image("test.jpg",img, jpeg_tag{});
51
52 gray8s_image_t img_out(img.dimensions());
53 fill_pixels(view(img_out),int8_t(0));
54
55 x_luminosity_gradient(const_view(img), view(img_out));
56 write_view("out-x_gradient.jpg",color_converted_view<gray8_pixel_t>(const_view(img_out)), jpeg_tag{});
57
58 return 0;
59 }
60