x_gradient.cpp 1.7 KB

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