zoom.cpp revision 3ed852eea50f9d4cd633efb8c2b054b8e33c2530
1793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// This may look like C code, but it is really -*- C++ -*-
2793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
3793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Copyright Bob Friesenhahn, 2001, 2002, 2003
4793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
5793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Resize image using specified resize algorithm with Magick++ API
6793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
7793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Usage: zoom [-density resolution] [-filter algorithm] [-geometry geometry]
8793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//             [-resample resolution] input_file output_file
9793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
10793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
11793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include <Magick++.h>
12793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include <iostream>
13793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include <string>
14793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerusing namespace std;
15793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerusing namespace Magick;
16793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
17793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerstatic void Usage ( char **argv )
18793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
19793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cout << "Usage: " << argv[0]
20793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler       << " [-density resolution] [-filter algorithm] [-geometry geometry]"
21793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler       << " [-resample resolution] input_file output_file" << endl
22793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler       << "   algorithm - bessel blackman box catrom cubic gaussian hamming hanning" << endl
23793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler       << "     hermite lanczos mitchell point quadratic sample scale sinc triangle" << endl;
24793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  exit(1);
25793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
26793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
27793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerstatic void ParseError (int position, char **argv)
28793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
29793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  cout << "Argument \"" <<  argv[position] << "\" at position" << position
30793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler       << "incorrect" << endl;
31793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  Usage(argv);
32793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
33793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
34793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerint main(int argc,char **argv)
35793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
36793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // Initialize ImageMagick install location for Windows
37793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  InitializeMagick(*argv);
38793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
39793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  if ( argc < 2 )
40793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Usage(argv);
41793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
42793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  enum ResizeAlgorithm
43793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  {
44793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Zoom,
45793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Scale,
46793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Sample
47793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  };
48793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
49793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  {
50793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Geometry density;
51793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Geometry geometry;
52793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Geometry resample;
53793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Magick::FilterTypes filter(LanczosFilter);
54793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ResizeAlgorithm resize_algorithm=Zoom;
55793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
56793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    int argv_index=1;
57793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    while ((argv_index < argc - 2) && (*argv[argv_index] == '-'))
58793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler      {
59793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        std::string command(argv[argv_index]);
60793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        if (command.compare("-density") == 0)
61793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler          {
62793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            argv_index++;
63793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            try {
64793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler              density=Geometry(argv[argv_index]);
65793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            }
66793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            catch( exception &/* error_ */)
67793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler              {
68793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                ParseError(argv_index,argv);
69793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler              }
70793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            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<unsigned int>
163                     (image.columns()*((double)resample.width()/density.width())+0.5),
164                     static_cast<unsigned int>
165                     (image.rows()*((double)resample.height()/density.height())+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