Panda3D
imageResize.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file imageResize.cxx
10  * @author drose
11  * @date 2003-03-13
12  */
13 
14 #include "imageResize.h"
15 #include "string_utils.h"
16 
17 /**
18  *
19  */
20 ImageResize::
21 ImageResize() : ImageFilter(true) {
22  set_program_brief("resize an image file");
23  set_program_description
24  ("This program reads an image file and resizes it to a larger or smaller "
25  "image file.");
26 
27  add_option
28  ("x", "xsize", 0,
29  "Specify the width of the output image in pixels, or as a percentage "
30  "of the original width (if a trailing percent sign is included). "
31  "If this is omitted, the ratio is taken from the ysize parameter.",
32  &ImageResize::dispatch_size_request, nullptr, &_x_size);
33 
34  add_option
35  ("y", "ysize", 0,
36  "Specify the height of the output image in pixels, or as a percentage "
37  "of the original height (if a trailing percent sign is included). "
38  "If this is omitted, the ratio is taken from the xsize parameter.",
39  &ImageResize::dispatch_size_request, nullptr, &_y_size);
40 
41  add_option
42  ("g", "radius", 0,
43  "Use Gaussian filtering to resize the image, with the indicated radius.",
44  &ImageResize::dispatch_double, &_use_gaussian_filter, &_filter_radius);
45 
46  add_option
47  ("1", "", 0,
48  "This option is ignored. It is provided only for backward compatibility "
49  "with a previous version of image-resize.",
50  &ImageResize::dispatch_none, nullptr, nullptr);
51 
52  _filter_radius = 1.0;
53 }
54 
55 /**
56  *
57  */
58 void ImageResize::
59 run() {
60  if (_x_size.get_type() == RT_none && _y_size.get_type() == RT_none) {
61  _x_size.set_ratio(1.0);
62  _y_size.set_ratio(1.0);
63  } else if (_x_size.get_type() == RT_none) {
64  _x_size.set_ratio(_y_size.get_ratio(_image.get_y_size()));
65  } else if (_y_size.get_type() == RT_none) {
66  _y_size.set_ratio(_x_size.get_ratio(_image.get_x_size()));
67  }
68 
69  int x_size = _x_size.get_pixel_size(_image.get_x_size());
70  int y_size = _y_size.get_pixel_size(_image.get_y_size());
71 
72  nout << "Resizing to " << x_size << " x " << y_size << "\n";
73  PNMImage new_image(x_size, y_size,
74  _image.get_num_channels(),
75  _image.get_maxval(), _image.get_type());
76 
77  if (_use_gaussian_filter) {
78  new_image.gaussian_filter_from(_filter_radius, _image);
79  } else {
80  new_image.quick_filter_from(_image);
81  }
82 
83  write_image(new_image);
84 }
85 
86 /**
87  * Interprets the -x or -y parameters.
88  */
89 bool ImageResize::
90 dispatch_size_request(const std::string &opt, const std::string &arg, void *var) {
91  SizeRequest *ip = (SizeRequest *)var;
92  if (!arg.empty() && arg[arg.length() - 1] == '%') {
93  // A ratio.
94  std::string str = arg.substr(0, arg.length() - 1);
95  double ratio;
96  if (!string_to_double(str, ratio)) {
97  nout << "Invalid ratio for -" << opt << ": "
98  << str << "\n";
99  return false;
100  }
101  ip->set_ratio(ratio / 100.0);
102 
103  } else {
104  // A pixel size.
105  int pixel_size;
106  if (!string_to_int(arg, pixel_size)) {
107  nout << "Invalid pixel size for -" << opt << ": "
108  << arg << "\n";
109  return false;
110  }
111  ip->set_pixel_size(pixel_size);
112  }
113 
114  return true;
115 }
116 
117 
118 int main(int argc, char *argv[]) {
119  ImageResize prog;
120  prog.parse_command_line(argc, argv);
121  prog.run();
122  return 0;
123 }
int string_to_int(const string &str, string &tail)
A string-interface wrapper around the C library strtol().
double string_to_double(const string &str, string &tail)
A string-interface wrapper around the C library strtol().
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
Definition: pnmImage.h:58
virtual void parse_command_line(int argc, char **argv)
Dispatches on each of the options on the command line, and passes the remaining parameters to handle_...
This is the base class for a program that reads an image file, operates on it, and writes another ima...
Definition: imageFilter.h:26
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void gaussian_filter_from(float radius, const PNMImage &copy)
Makes a resized copy of the indicated image into this one using the indicated filter.
void write_image()
Writes the generated to the user's specified output filename.
Definition: imageWriter.I:18
A program to read an image file and resize it to a larger or smaller image file.
Definition: imageResize.h:25
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.