22 set_program_brief(
"apply transformations to an image file");
23 set_program_description
24 (
"This program reads an image file and writes a similar "
25 "image file to the output. It can implicitly convert from one image "
26 "file format to another; it uses the extension of the output filename "
27 "to specify the destination file format.");
30 (
"chan",
"channels", 50,
31 "Elevate (or truncate) the image to the indicated number of channels. "
32 "This may be 1, 2, 3, or 4. You may also specify one of the keywords "
33 "l, la, rgb, or rgba, respectively, or any of the keywords r, g, b, or "
34 "a to extract out just the indicated channel as a single grayscale "
36 &ImageTrans::dispatch_channels,
nullptr, &_channels);
39 (
"cscale",
"r,g,b[,a]", 50,
40 "Apply the indicated color scale to each pixel of the image.",
41 &ImageTrans::dispatch_color, &_has_color_scale, &_color_scale);
45 "Flip the image vertically.",
46 &ImageTrans::dispatch_none, &_flip);
50 "Reverse the image horizontally.",
51 &ImageTrans::dispatch_none, &_mirror);
55 "Rotate the image 90 degrees clockwise.",
56 &ImageTrans::dispatch_none, &_cw);
60 "Rotate the image 90 degrees counter-clockwise.",
61 &ImageTrans::dispatch_none, &_ccw);
63 _channels = C_default;
64 _color_scale.set(1.0f, 1.0f, 1.0f, 1.0f);
80 _image.set_num_channels((
int)_channels);
84 _image.make_grayscale(1.0, 0.0, 0.0);
85 _image.remove_alpha();
89 _image.make_grayscale(0.0, 1.0, 0.0);
90 _image.remove_alpha();
94 _image.make_grayscale(0.0, 0.0, 1.0);
95 _image.remove_alpha();
103 if (_has_color_scale) {
104 if (_color_scale[0] != 1.0f ||
105 _color_scale[1] != 1.0f ||
106 _color_scale[2] != 1.0f) {
107 for (
int yi = 0; yi < _image.get_y_size(); ++yi) {
108 for (
int xi = 0; xi < _image.get_x_size(); ++xi) {
109 LRGBColorf rgb = _image.get_xel(xi, yi);
110 _image.set_xel(xi, yi,
111 rgb[0] * _color_scale[0],
112 rgb[1] * _color_scale[1],
113 rgb[2] * _color_scale[2]);
117 if (_image.has_alpha() && _color_scale[3] != 1.0f) {
118 for (
int yi = 0; yi < _image.get_y_size(); ++yi) {
119 for (
int xi = 0; xi < _image.get_x_size(); ++xi) {
120 PN_stdfloat a = _image.get_alpha(xi, yi);
121 _image.set_alpha(xi, yi, a * _color_scale[3]);
127 bool transpose =
false;
130 transpose = !transpose;
134 transpose = !transpose;
136 if (_flip || _mirror || transpose) {
137 _image.flip(_mirror, _flip, transpose);
147dispatch_channels(
const std::string &opt,
const std::string &arg,
void *var) {
148 Channels *ip = (Channels *)var;
149 if (cmp_nocase(arg,
"l") == 0) {
151 }
else if (cmp_nocase(arg,
"la") == 0) {
153 }
else if (cmp_nocase(arg,
"rgb") == 0) {
155 }
else if (cmp_nocase(arg,
"rgba") == 0) {
157 }
else if (cmp_nocase(arg,
"r") == 0) {
159 }
else if (cmp_nocase(arg,
"g") == 0) {
161 }
else if (cmp_nocase(arg,
"b") == 0) {
163 }
else if (cmp_nocase(arg,
"a") == 0) {
168 nout <<
"Invalid parameter for -" << opt <<
": "
172 if (value < 1 || value > 4) {
173 nout <<
"Number of channels must be one of 1, 2, 3, or 4.\n";
176 (*ip) = (Channels)value;
187 if (!_image.has_alpha()) {
188 nout <<
"Source image does not have an alpha channel!\n";
189 _image.make_grayscale();
194 _image.make_grayscale();
195 for (
int y = 0; y < _image.get_y_size(); y++) {
196 for (
int x = 0; x < _image.get_x_size(); x++) {
197 _image.set_gray_val(x, y, _image.get_alpha_val(x, y));
200 _image.remove_alpha();
204int main(
int argc,
char *argv[]) {
This is the base class for a program that reads an image file, operates on it, and writes another ima...
A program to read an image file and write an equivalent image file, possibly performing some minor op...
void write_image()
Writes the generated to the user's specified output filename.
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_...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int string_to_int(const string &str, string &tail)
A string-interface wrapper around the C library strtol().
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.