00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "imageTrans.h"
00020 #include "string_utils.h"
00021
00022
00023
00024
00025
00026
00027 ImageTrans::
00028 ImageTrans() : ImageFilter(true) {
00029 set_program_description
00030 ("This program reads an image file and writes an essentially equivalent "
00031 "image file to the file specified with -o.");
00032
00033 add_option
00034 ("chan", "channels", 50,
00035 "Elevate (or truncate) the image to the indicated number of channels. "
00036 "This may be 1, 2, 3, or 4. You may also specify one of the keywords "
00037 "l, la, rgb, or rgba, respectively, or any of the keywords r, g, b, or "
00038 "a to extract out just the indicated channel as a single grayscale "
00039 "image.",
00040 &ImageTrans::dispatch_channels, NULL, &_channels);
00041
00042 _channels = C_default;
00043 }
00044
00045
00046
00047
00048
00049
00050 void ImageTrans::
00051 run() {
00052 switch (_channels) {
00053 case C_default:
00054 break;
00055
00056 case C_l:
00057 case C_la:
00058 case C_rgb:
00059 case C_rgba:
00060 _image.set_num_channels((int)_channels);
00061 break;
00062
00063 case C_r:
00064 _image.make_grayscale(1.0, 0.0, 0.0);
00065 _image.remove_alpha();
00066 break;
00067
00068 case C_g:
00069 _image.make_grayscale(0.0, 1.0, 0.0);
00070 _image.remove_alpha();
00071 break;
00072
00073 case C_b:
00074 _image.make_grayscale(0.0, 0.0, 1.0);
00075 _image.remove_alpha();
00076 break;
00077
00078 case C_a:
00079 extract_alpha();
00080 break;
00081 }
00082
00083 write_image();
00084 }
00085
00086
00087
00088
00089
00090
00091 bool ImageTrans::
00092 dispatch_channels(const string &opt, const string &arg, void *var) {
00093 Channels *ip = (Channels *)var;
00094 if (cmp_nocase(arg, "l") == 0) {
00095 (*ip) = C_l;
00096 } else if (cmp_nocase(arg, "la") == 0) {
00097 (*ip) = C_la;
00098 } else if (cmp_nocase(arg, "rgb") == 0) {
00099 (*ip) = C_rgb;
00100 } else if (cmp_nocase(arg, "rgba") == 0) {
00101 (*ip) = C_rgba;
00102 } else if (cmp_nocase(arg, "r") == 0) {
00103 (*ip) = C_r;
00104 } else if (cmp_nocase(arg, "g") == 0) {
00105 (*ip) = C_g;
00106 } else if (cmp_nocase(arg, "b") == 0) {
00107 (*ip) = C_b;
00108 } else if (cmp_nocase(arg, "a") == 0) {
00109 (*ip) = C_a;
00110 } else {
00111 int value;
00112 if (!string_to_int(arg, value)) {
00113 nout << "Invalid parameter for -" << opt << ": "
00114 << arg << "\n";
00115 return false;
00116 }
00117 if (value < 1 || value > 4) {
00118 nout << "Number of channels must be one of 1, 2, 3, or 4.\n";
00119 return false;
00120 }
00121 (*ip) = (Channels)value;
00122 }
00123
00124 return true;
00125 }
00126
00127
00128
00129
00130
00131
00132
00133 void ImageTrans::
00134 extract_alpha() {
00135 if (!_image.has_alpha()) {
00136 nout << "Source image does not have an alpha channel!\n";
00137 _image.make_grayscale();
00138 _image.fill();
00139 return;
00140 }
00141
00142 _image.make_grayscale();
00143 for (int y = 0; y < _image.get_y_size(); y++) {
00144 for (int x = 0; x < _image.get_x_size(); x++) {
00145 _image.set_gray_val(x, y, _image.get_alpha_val(x, y));
00146 }
00147 }
00148 _image.remove_alpha();
00149 }
00150
00151
00152 int main(int argc, char *argv[]) {
00153 ImageTrans prog;
00154 prog.parse_command_line(argc, argv);
00155 prog.run();
00156 return 0;
00157 }