Magick++ 7.1.0

zoom.cpp
Go to the documentation of this file.
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 2001, 2002, 2003
4 //
5 // Copyright @ 2013 ImageMagick Studio LLC, a non-profit organization
6 // dedicated to making software imaging solutions freely available.
7 //
8 // Resize image using specified resize algorithm with Magick++ API
9 //
10 // Usage: zoom [-density resolution] [-filter algorithm] [-geometry geometry]
11 // [-resample resolution] input_file output_file
12 //
13
14 #include <Magick++.h>
15 #include <iostream>
16 #include <string>
17 using namespace std;
18 using namespace Magick;
19
20 static void Usage ( char **argv )
21 {
22 cout << "Usage: " << argv[0]
23 << " [-density resolution] [-filter algorithm] [-geometry geometry]"
24 << " [-resample resolution] input_file output_file" << endl
25 << " algorithm - bessel blackman box catrom cubic gaussian hamming hanning" << endl
26 << " hermite lanczos mitchell point quadratic sample scale sinc triangle" << endl;
27 exit(1);
28 }
29
30 static void ParseError (int position, char **argv)
31 {
32 cout << "Argument \"" << argv[position] << "\" at position" << position
33 << "incorrect" << endl;
34 Usage(argv);
35 }
36
37 int main(int argc,char **argv)
38 {
39 // Initialize ImageMagick install location for Windows
40 InitializeMagick(*argv);
41
42 if ( argc < 2 )
43 Usage(argv);
44
45 enum ResizeAlgorithm
46 {
47 Zoom,
48 Scale,
49 Sample
50 };
51
52 {
53 Geometry geometry;
54 Magick::FilterType filter(LanczosFilter);
55 Point density;
56 Point resample;
57 ResizeAlgorithm resize_algorithm=Zoom;
58
59 int argv_index=1;
60 while ((argv_index < argc - 2) && (*argv[argv_index] == '-'))
61 {
62 std::string command(argv[argv_index]);
63 if (command.compare("-density") == 0)
64 {
65 argv_index++;
66 try {
67 density=Geometry(argv[argv_index]);
68 }
69 catch( exception &/* error_ */)
70 {
71 ParseError(argv_index,argv);
72 }
73 argv_index++;
74 continue;
75 }
76 else if (command.compare("-filter") == 0)
77 {
78 argv_index++;
79 std::string algorithm(argv[argv_index]);
80 if (algorithm.compare("point") == 0)
81 filter=PointFilter;
82 else if (algorithm.compare("box") == 0)
83 filter=BoxFilter;
84 else if (algorithm.compare("triangle") == 0)
85 filter=TriangleFilter;
86 else if (algorithm.compare("hermite") == 0)
87 filter=HermiteFilter;
88 else if (algorithm.compare("hanning") == 0)
89 filter=HanningFilter;
90 else if (algorithm.compare("hamming") == 0)
91 filter=HammingFilter;
92 else if (algorithm.compare("blackman") == 0)
93 filter=BlackmanFilter;
94 else if (algorithm.compare("gaussian") == 0)
95 filter=GaussianFilter;
96 else if (algorithm.compare("quadratic") == 0)
97 filter=QuadraticFilter;
98 else if (algorithm.compare("cubic") == 0)
99 filter=CubicFilter;
100 else if (algorithm.compare("catrom") == 0)
101 filter=CatromFilter;
102 else if (algorithm.compare("mitchell") == 0)
103 filter=MitchellFilter;
104 else if (algorithm.compare("lanczos") == 0)
105 filter=LanczosFilter;
106 else if (algorithm.compare("bessel") == 0)
107 filter=BesselFilter;
108 else if (algorithm.compare("sinc") == 0)
109 filter=SincFilter;
110 else if (algorithm.compare("sample") == 0)
111 resize_algorithm=Sample;
112 else if (algorithm.compare("scale") == 0)
113 resize_algorithm=Scale;
114 else
115 ParseError(argv_index,argv);
116 argv_index++;
117 continue;
118 }
119 else if (command.compare("-geometry") == 0)
120 {
121 argv_index++;
122 try {
123 geometry=Geometry(argv[argv_index]);
124 }
125 catch( exception &/* error_ */)
126 {
127 ParseError(argv_index,argv);
128 }
129 argv_index++;
130 continue;
131 }
132 else if (command.compare("-resample") == 0)
133 {
134 argv_index++;
135 try {
136 resample=Geometry(argv[argv_index]);
137 }
138 catch( exception &/* error_ */)
139 {
140 ParseError(argv_index,argv);
141 }
142 argv_index++;
143 continue;
144 }
145 ParseError(argv_index,argv);
146 }
147
148 if (argv_index>argc-1)
149 ParseError(argv_index,argv);
150 std::string input_file(argv[argv_index]);
151 argv_index++;
152 if (argv_index>argc)
153 ParseError(argv_index,argv);
154 std::string output_file(argv[argv_index]);
155
156 try {
157 Image image(input_file);
158 if (density.isValid())
159 image.density(density);
160 density=image.density();
161
162 if (resample.isValid())
163 {
164 geometry =
165 Geometry(static_cast<size_t>
166 (image.columns()*((double)resample.x()/density.x())+0.5),
167 static_cast<size_t>
168 (image.rows()*((double)resample.y()/density.y())+0.5));
169 image.density(resample);
170 }
171 switch (resize_algorithm)
172 {
173 case Sample:
174 image.sample(geometry);
175 break;
176 case Scale:
177 image.scale(geometry);
178 break;
179 case Zoom:
180 image.filterType(filter);
181 image.zoom(geometry);
182 break;
183 }
184 image.write(output_file);
185 }
186 catch( exception &error_ )
187 {
188 cout << "Caught exception: " << error_.what() << endl;
189 return 1;
190 }
191 }
192
193 return 0;
194 }
Magick::Point
Definition: Geometry.h:208
Magick::Image::density
void density(const Point &density_)
Definition: Image.cpp:653
Magick::Point::isValid
bool isValid() const
Definition: Geometry.cpp:698
std
STL namespace.
Magick::Image::write
void write(Blob *blob_)
Definition: Image.cpp:4896
Magick::Image::zoom
void zoom(const Geometry &geometry_)
Definition: Image.cpp:4997
Magick++.h
Magick::Image::filterType
void filterType(const FilterType filterType_)
Definition: Image.cpp:841
ParseError
static void ParseError(int position, char **argv)
Definition: zoom.cpp:30
Magick::Geometry
class MagickPPExport Geometry
Definition: Geometry.h:21
Magick::Image::scale
void scale(const Geometry &geometry_)
Definition: Image.cpp:4283
Magick::Geometry
Definition: Geometry.h:37
Magick::Point::x
double x(void) const
Definition: Geometry.cpp:703
Magick::Image::sample
void sample(const Geometry &geometry_)
Definition: Image.cpp:4261
Magick::Image::rows
size_t rows(void) const
Definition: Image.cpp:1352
Magick::Image::columns
size_t columns(void) const
Definition: Image.cpp:590
Usage
static void Usage(char **argv)
Definition: zoom.cpp:20
Magick::Point::y
double y(void) const
Definition: Geometry.cpp:708
main
int main(int argc, char **argv)
Definition: zoom.cpp:37
Magick
Definition: Blob.h:17
Magick::InitializeMagick
MagickPPExport void InitializeMagick(const char *path_)
Definition: Functions.cpp:45
Magick::Image
Definition: Image.h:55