Magick++  7.1.2-29
Convert, Edit, Or Compose Bitmap Images
colorHistogram.cpp
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 2003
4 //
5 // Test STL colorHistogram function
6 //
7 
8 #undef USE_VECTOR
9 #define USE_MAP
10 
11 #include <Magick++.h>
12 #include <string>
13 #include <iostream>
14 #include <iomanip>
15 #if defined(USE_VECTOR)
16 # include <vector>
17 # include <utility>
18 #endif
19 #if defined(USE_MAP)
20 # include <map>
21 #endif
22 
23 using namespace std;
24 
25 using namespace Magick;
26 
27 int main( int /*argc*/, char ** argv)
28 {
29 
30  // Initialize ImageMagick install location for Windows
31  MagickPlusPlusGenesis genesis(*argv);
32 
33  int failures=0;
34 
35  try {
36 
37  string srcdir("");
38  if(getenv("SRCDIR") != 0)
39  srcdir = getenv("SRCDIR");
40 
41  // Read image
42  Image image;
43  image.read( srcdir + "test_image.miff" );
44 
45  // Create histogram vector
46 #if defined(USE_MAP)
47  std::map<Color,size_t> histogram;
48 #elif defined(USE_VECTOR)
49  std::vector<std::pair<Color,size_t> > histogram;
50 #endif
51 
52  colorHistogram( &histogram, image );
53 
54  // Print out histogram
55 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
56  int quantum_width=3;
57 #elif (MAGICKCORE_QUANTUM_DEPTH == 16)
58  int quantum_width=5;
59 #else
60  int quantum_width=10;
61 #endif
62 
63  cout << "Histogram for file \"" << image.fileName() << "\"" << endl
64  << histogram.size() << " entries:" << endl;
65 
66 #if defined(USE_MAP)
67  std::map<Color,size_t>::const_iterator p=histogram.begin();
68 #elif defined(USE_VECTOR)
69  std::vector<std::pair<Color,size_t> >::const_iterator p=histogram.begin();
70 #endif
71  while (p != histogram.end())
72  {
73  cout << setw(10) << (int)p->second << ": ("
74  << setw(quantum_width) << (int)p->first.quantumRed() << ","
75  << setw(quantum_width) << (int)p->first.quantumGreen() << ","
76  << setw(quantum_width) << (int)p->first.quantumBlue() << ")"
77  << endl;
78  p++;
79  }
80  }
81 
82  catch( Exception &error_ )
83  {
84  cout << "Caught exception: " << error_.what() << endl;
85  return 1;
86  }
87  catch( exception &error_ )
88  {
89  cout << "Caught exception: " << error_.what() << endl;
90  return 1;
91  }
92 
93  if ( failures )
94  {
95  cout << failures << " failures" << endl;
96  return 1;
97  }
98 
99  return 0;
100 }
101