15 #include "imageTrans.h" 16 #include "string_utils.h" 26 set_program_brief(
"apply transformations to an image file");
27 set_program_description
28 (
"This program reads an image file and writes a similar " 29 "image file to the output. It can implicitly convert from one image " 30 "file format to another; it uses the extension of the output filename " 31 "to specify the destination file format.");
34 (
"chan",
"channels", 50,
35 "Elevate (or truncate) the image to the indicated number of channels. " 36 "This may be 1, 2, 3, or 4. You may also specify one of the keywords " 37 "l, la, rgb, or rgba, respectively, or any of the keywords r, g, b, or " 38 "a to extract out just the indicated channel as a single grayscale " 40 &ImageTrans::dispatch_channels, NULL, &_channels);
43 (
"cscale",
"r,g,b[,a]", 50,
44 "Apply the indicated color scale to each pixel of the image.",
45 &ImageTrans::dispatch_color, &_has_color_scale, &_color_scale);
49 "Flip the image vertically.",
50 &ImageTrans::dispatch_none, &_flip);
54 "Reverse the image horizontally.",
55 &ImageTrans::dispatch_none, &_mirror);
59 "Rotate the image 90 degrees clockwise.",
60 &ImageTrans::dispatch_none, &_cw);
64 "Rotate the image 90 degrees counter-clockwise.",
65 &ImageTrans::dispatch_none, &_ccw);
67 _channels = C_default;
68 _color_scale.set(1.0f, 1.0f, 1.0f, 1.0f);
86 _image.set_num_channels((
int)_channels);
90 _image.make_grayscale(1.0, 0.0, 0.0);
91 _image.remove_alpha();
95 _image.make_grayscale(0.0, 1.0, 0.0);
96 _image.remove_alpha();
100 _image.make_grayscale(0.0, 0.0, 1.0);
101 _image.remove_alpha();
109 if (_has_color_scale) {
110 if (_color_scale[0] != 1.0f ||
111 _color_scale[1] != 1.0f ||
112 _color_scale[2] != 1.0f) {
113 for (
int yi = 0; yi < _image.get_y_size(); ++yi) {
114 for (
int xi = 0; xi < _image.get_x_size(); ++xi) {
116 _image.set_xel(xi, yi,
117 rgb[0] * _color_scale[0],
118 rgb[1] * _color_scale[1],
119 rgb[2] * _color_scale[2]);
123 if (_image.has_alpha() && _color_scale[3] != 1.0f) {
124 for (
int yi = 0; yi < _image.get_y_size(); ++yi) {
125 for (
int xi = 0; xi < _image.get_x_size(); ++xi) {
126 PN_stdfloat a = _image.get_alpha(xi, yi);
127 _image.set_alpha(xi, yi, a * _color_scale[3]);
133 bool transpose =
false;
136 transpose = !transpose;
140 transpose = !transpose;
142 if (_flip || _mirror || transpose) {
143 _image.flip(_mirror, _flip, transpose);
155 dispatch_channels(
const string &opt,
const string &arg,
void *var) {
156 Channels *ip = (Channels *)var;
157 if (cmp_nocase(arg,
"l") == 0) {
159 }
else if (cmp_nocase(arg,
"la") == 0) {
161 }
else if (cmp_nocase(arg,
"rgb") == 0) {
163 }
else if (cmp_nocase(arg,
"rgba") == 0) {
165 }
else if (cmp_nocase(arg,
"r") == 0) {
167 }
else if (cmp_nocase(arg,
"g") == 0) {
169 }
else if (cmp_nocase(arg,
"b") == 0) {
171 }
else if (cmp_nocase(arg,
"a") == 0) {
175 if (!string_to_int(arg, value)) {
176 nout <<
"Invalid parameter for -" << opt <<
": " 180 if (value < 1 || value > 4) {
181 nout <<
"Number of channels must be one of 1, 2, 3, or 4.\n";
184 (*ip) = (Channels)value;
198 if (!_image.has_alpha()) {
199 nout <<
"Source image does not have an alpha channel!\n";
200 _image.make_grayscale();
205 _image.make_grayscale();
206 for (
int y = 0; y < _image.get_y_size(); y++) {
207 for (
int x = 0; x < _image.get_x_size(); x++) {
208 _image.set_gray_val(x, y, _image.get_alpha_val(x, y));
211 _image.remove_alpha();
215 int main(
int argc,
char *argv[]) {
This is the base class for all three-component vectors and points.
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_...
A program to read an image file and write an equivalent image file, possibly performing some minor op...
This is the base class for a program that reads an image file, operates on it, and writes another ima...
void write_image()
Writes the generated to the user's specified output filename.