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