Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / gil / doc / html / _downloads / histogram.cpp
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.hpp>
9 #include <boost/gil/extension/io/jpeg.hpp>
10
11 #include <algorithm>
12 #include <fstream>
13
14 // Example file to demonstrate a way to compute histogram
15
16 using namespace boost::gil;
17
18 template <typename GrayView, typename R>
19 void gray_image_hist(GrayView const& img_view, R& hist)
20 {
21     for (auto it = img_view.begin(); it != img_view.end(); ++it)
22         ++hist[*it];
23
24     // Alternatively, prefer the algorithm with lambda
25     // for_each_pixel(img_view, [&hist](gray8_pixel_t const& pixel) {
26     //     ++hist[pixel];
27     // });
28 }
29
30 template <typename V, typename R>
31 void get_hist(const V& img_view, R& hist) {
32     gray_image_hist(color_converted_view<gray8_pixel_t>(img_view), hist);
33 }
34
35 int main() {
36     rgb8_image_t img;
37     read_image("test.jpg", img, jpeg_tag());
38
39     int histogram[256];
40     std::fill(histogram,histogram + 256, 0);
41     get_hist(const_view(img), histogram);
42
43     std::fstream histo_file("out-histogram.txt", std::ios::out);
44     for(std::size_t ii = 0; ii < 256; ++ii)
45         histo_file << histogram[ii] << std::endl;
46     histo_file.close();
47
48     return 0;
49 }