00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "imageResize.h"
00020 #include "string_utils.h"
00021
00022
00023
00024
00025
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
00063
00064
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
00096
00097
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
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
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 }