Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

pandatool/src/imageprogs/imageResize.cxx

Go to the documentation of this file.
00001 // Filename: imageResize.cxx
00002 // Created by:  drose (13Mar03)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "imageResize.h"
00020 #include "string_utils.h"
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function: ImageResize::Constructor
00024 //       Access: Public
00025 //  Description:
00026 ////////////////////////////////////////////////////////////////////
00027 ImageResize::
00028 ImageResize() : ImageFilter(true) {
00029   set_program_description
00030     ("This program reads an image file and resizes it to a larger or smaller "
00031      "image file.");
00032 
00033   add_option
00034     ("x", "xsize", 0,
00035      "Specify the width of the output image in pixels, or as a percentage "
00036      "of the original width (if a trailing percent sign is included).  "
00037      "If this is omitted, the ratio is taken from the ysize parameter.",
00038      &ImageResize::dispatch_size_request, NULL, &_x_size);
00039 
00040   add_option
00041     ("y", "ysize", 0,
00042      "Specify the height of the output image in pixels, or as a percentage "
00043      "of the original height (if a trailing percent sign is included).  "
00044      "If this is omitted, the ratio is taken from the xsize parameter.",
00045      &ImageResize::dispatch_size_request, NULL, &_y_size);
00046 
00047   add_option
00048     ("g", "radius", 0,
00049      "Use Gaussian filtering to resize the image, with the indicated radius.",
00050      &ImageResize::dispatch_double, &_use_gaussian_filter, &_filter_radius);
00051 
00052   add_option
00053     ("1", "", 0,
00054      "This option is ignored.  It is provided only for backward compatibility "
00055      "with a previous version of image-resize.",
00056      &ImageResize::dispatch_none, NULL, NULL);
00057 
00058   _filter_radius = 1.0;
00059 }
00060 
00061 ////////////////////////////////////////////////////////////////////
00062 //     Function: ImageResize::run
00063 //       Access: Public
00064 //  Description:
00065 ////////////////////////////////////////////////////////////////////
00066 void ImageResize::
00067 run() {
00068   if (_x_size.get_type() == RT_none && _y_size.get_type() == RT_none) {
00069     _x_size.set_ratio(1.0);
00070     _y_size.set_ratio(1.0);
00071   } else if (_x_size.get_type() == RT_none) {
00072     _x_size.set_ratio(_y_size.get_ratio(_image.get_y_size()));
00073   } else if (_y_size.get_type() == RT_none) {
00074     _y_size.set_ratio(_x_size.get_ratio(_image.get_x_size()));
00075   }
00076 
00077   int x_size = _x_size.get_pixel_size(_image.get_x_size());
00078   int y_size = _y_size.get_pixel_size(_image.get_y_size());
00079 
00080   nout << "Resizing to " << x_size << " x " << y_size << "\n";
00081   PNMImage new_image(x_size, y_size,
00082                      _image.get_num_channels(), 
00083                      _image.get_maxval(), _image.get_type());
00084 
00085   if (_use_gaussian_filter) {
00086     new_image.gaussian_filter_from(_filter_radius, _image);
00087   } else {
00088     new_image.quick_filter_from(_image);
00089   }
00090 
00091   write_image(new_image);
00092 }
00093 
00094 ////////////////////////////////////////////////////////////////////
00095 //     Function: ImageResize::dispatch_size_request
00096 //       Access: Private, Static
00097 //  Description: Interprets the -x or -y parameters.
00098 ////////////////////////////////////////////////////////////////////
00099 bool ImageResize::
00100 dispatch_size_request(const string &opt, const string &arg, void *var) {
00101   SizeRequest *ip = (SizeRequest *)var;
00102   if (!arg.empty() && arg[arg.length() - 1] == '%') {
00103     // A ratio.
00104     string str = arg.substr(0, arg.length() - 1);
00105     double ratio;
00106     if (!string_to_double(str, ratio)) {
00107       nout << "Invalid ratio for -" << opt << ": "
00108            << str << "\n";
00109       return false;
00110     }
00111     ip->set_ratio(ratio / 100.0);
00112 
00113   } else {
00114     // A pixel size.
00115     int pixel_size;
00116     if (!string_to_int(arg, pixel_size)) {
00117       nout << "Invalid pixel size for -" << opt << ": "
00118            << arg << "\n";
00119       return false;
00120     }
00121     ip->set_pixel_size(pixel_size);
00122   }
00123 
00124   return true;
00125 }
00126 
00127 
00128 int main(int argc, char *argv[]) {
00129   ImageResize prog;
00130   prog.parse_command_line(argc, argv);
00131   prog.run();
00132   return 0;
00133 }

Generated on Fri May 2 03:20:06 2003 for Panda-Tool by doxygen1.3