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