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