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