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