| Bsoft 2.0.5 Bernard's software package

bcolour.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "utilities.h"
11 #include "options.h"
12 #include "timer.h"
13
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16
17 // Usage assistance
18 const char* use[] = {
19 " ",
20 "Usage: bcolour [options] input.img output.img",
21 "---------------------------------------------",
22 "Generates nice colour images.",
23 "Only image file formats capable of handling RGB can be used for output",
24 " (TIFF, PNG, JPEG).",
25 " ",
26 "Actions:",
27 "-invert Invert image.",
28 "-red -4.5,88.3 Convert a grayscale range to red.",
29 "-green -4.5,88.3 Convert a grayscale range to green.",
30 "-blue -4.5,88.3 Convert a grayscale range to blue.",
31 "-spectrum 1,22.4 Convert a grayscale range to a spectral colour scale.",
32 "-rwb 0,56,167,245 Convert a grayscale range to a red, white and blue image.",
33 "-phases Colour the phases in a polar map or complex transform.",
34 "-intensities Calculates the intensities without colours.",
35 "-pure Calculates pure colours without intensity.",
36 "-alpha Add an alpha (transparency) channel.",
37 "-noalpha Remove the alpha (transparency) channel.",
38 "-torgb Convert grayscale or CMYK to RGB.",
39 "-tocmyk Convert grayscale or RGB to CMYK.",
40 "-split Split the channels into individual images.",
41 "-combine 3 Combine channels from individual images.",
42 " ",
43 "Parameters:",
44 "-verbose 7 Verbosity of output.",
45 "-datatype u Force writing of a new data type.",
46 "-grayextremes Flag to keep gray extremes beyond colorization limits.",
47 " ",
48 "Input:",
49 "-add file.tif Add this file to the input file (must be the same size).",
50 " ",
51 "Output:",
52 "-scale file.tif Output spectral scale image.",
53 "-phasewheel pcw.png Output a phase color wheel (size 512,512,1)."
54 " ",
55 NULL
56 };
57
58 int main(int argc, char **argv)
59 {
60 // Initialize variables
61 DataType nudatatype(Unknown_Type); // Conversion to new type
62 int setimg(-1); // Selects all images
63 int setinvert(0);
64 int gray_extremes(0); // No gray scale extremes
65 int setred(0);
66 int setgreen(0);
67 int setblue(0);
68 int setspectrum(0);
69 int setrwb(0);
70 int setalpha(0); // No alpha channel change
71 int to_cmyk(0);
72 int to_rgb(0);
73 int colour_phases(0);
74 int calcintensity(0);
75 int calcpure(0);
76 int split(0);
77 int combine(0);
78 double cmin(0), cmax(0); // Min/max for conversion to spectrum
79 double red_min(0), white_min(0), white_max(0), blue_max(0);
80 Bstring add_image; // Image to add to input file
81 Bstring scale_image; // Spectral scale image
82 Bstring pcw; // Phase color wheel image
83
84 int optind;
85 Boption* option = get_option_list(use, argc, argv, optind);
86 Boption* curropt;
87 for ( curropt = option; curropt; curropt = curropt->next ) {
88 if ( curropt->tag == "datatype" )
89 nudatatype = curropt->datatype();
90 if ( curropt->tag == "invert" )
91 setinvert = 1;
92 if ( curropt->tag == "red" ) {
93 if ( curropt->values(cmin, cmax) < 2 )
94 cerr << "-red: A grayscale range must be specified!" << endl;
95 else
96 setred = 1;
97 }
98 if ( curropt->tag == "green" ) {
99 if ( curropt->values(cmin, cmax) < 2 )
100 cerr << "-green: A grayscale range must be specified!" << endl;
101 else
102 setgreen = 1;
103 }
104 if ( curropt->tag == "blue" ) {
105 if ( curropt->values(cmin, cmax) < 2 )
106 cerr << "-blue: A grayscale range must be specified!" << endl;
107 else
108 setblue = 1;
109 }
110 if ( curropt->tag == "spectrum" ) {
111 if ( curropt->values(cmin, cmax) < 2 )
112 cerr << "-spectrum: A grayscale range must be specified!" << endl;
113 else
114 setspectrum = 1;
115 }
116 if ( curropt->tag == "rwb" ) {
117 if ( curropt->values(red_min, white_min,
118 white_max, blue_max) < 4 )
119 cerr << "-rwb: 4 grayscale values must be specified!" << endl;
120 else
121 setrwb = 1;
122 }
123 if ( curropt->tag == "phases" )
124 colour_phases = 1;
125 if ( curropt->tag == "intensities" )
126 calcintensity = 1;
127 if ( curropt->tag == "pure" )
128 calcpure = 1;
129 if ( curropt->tag == "alpha" )
130 setalpha = 1;
131 if ( curropt->tag == "noalpha" )
132 setalpha = -1;
133 if ( curropt->tag == "torgb" )
134 to_rgb = 1;
135 if ( curropt->tag == "tocmyk" )
136 to_cmyk = 1;
137 if ( curropt->tag == "split" )
138 split = 1;
139 if ( curropt->tag == "combine" )
140 if ( ( combine = curropt->value.integer() ) < 1 )
141 cerr << "-combine: The number of channels must be specified!" << endl;
142 if ( curropt->tag == "grayextremes" )
143 gray_extremes = 1;
144 if ( curropt->tag == "add" )
145 add_image = curropt->filename();
146 if ( curropt->tag == "scale" )
147 scale_image = curropt->filename();
148 if ( curropt->tag == "phasewheel" )
149 pcw = curropt->filename();
150 }
151 option_kill(option);
152
153 double ti = timer_start();
154
155 if ( pcw.length() ) {
156 Bimage* pnu = new Bimage(UCharacter, TRGB, 512, 512, 1, 1);
157 pnu->phase_colour_wheel();
158 write_img(pcw, pnu, 0);
159 delete pnu;
160 }
161
162 if ( optind >= argc ) bexit(0);
163
164 // Read image file
165 int dataflag(0);
166 if ( optind < argc - 1 ) dataflag = 1;
167 Bimage* p = read_img(argv[optind++], dataflag, setimg);
168 if ( p == NULL ) {
169 cerr << "Error: No input file read!" << endl;
170 bexit(-1);
171 }
172
173 Bimage* p2 = NULL;
174 Bimage* pscale = NULL;
175
176 if ( optind < argc ) {
177 if ( setinvert ) p->invert();
178
179 if ( to_cmyk ) {
180 p->simple_to_rgb();
181 p->rgb_to_cmyk();
182 }
183
184 if ( to_rgb ) {
185 p->simple_to_rgb();
186 p->cmyk_to_rgb();
187 }
188
189 if ( setred ) p->color_red(cmin, cmax, gray_extremes);
190
191 if ( setgreen ) p->color_green(cmin, cmax, gray_extremes);
192
193 if ( setblue ) p->color_blue(cmin, cmax, gray_extremes);
194
195 if ( setspectrum ) pscale = p->color_spectrum(cmin, cmax);
196
197 if ( setrwb )
198 pscale = p->red_white_blue(red_min, white_min, white_max, blue_max);
199
200 if ( colour_phases ) {
201 p2 = p->intensities_phase_colored(0);
202 if ( p2 ) {
203 delete p;
204 p = p2;
205 }
206 }
207
208 if ( calcintensity ) p->color_to_simple();
209
210 if ( calcpure ) p->pure_color();
211
212 if ( setalpha > 0 ) p->rgb_to_rgba();
213 else if ( setalpha < 0 ) p->rgba_to_rgb();
214
215 if ( add_image.length() ) {
216 p2 = read_img(add_image, 1, -1);
217 p->color_combine(p2);
218 delete p2;
219 }
220
221 if ( split ) p->split_channels();
222
223 if ( combine ) p->combine_channels(combine);
224
225 p->change_type(nudatatype);
226
227 p->label(get_cmd_line(argc, argv));
228
229 write_img(argv[optind], p, 0);
230 }
231
232 if ( scale_image.length() && pscale ) {
233 pscale->label(get_cmd_line(argc, argv));
234 write_img(scale_image, pscale, 0);
235 delete pscale;
236 }
237
238 delete p;
239
240 if ( verbose & VERB_TIME )
241 timer_report(ti);
242
243 bexit(0);
244 }
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bimage::color_spectrum
Bimage * color_spectrum(double cmin, double cmax)
Colorizes an image to a spectrum.
Definition: Bimage_color.cpp:403
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3108
utilities.h
Header file for general utilities.
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
Bimage::pure_color
int pure_color()
Generates a pure color image without intensity.
Definition: Bimage_color.cpp:340
Bimage::color_red
int color_red(double cmin, double cmax, int gray_extremes=0)
Converts a gray-scale image to red.
Definition: Bimage_color.cpp:295
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bimage::simple_to_rgb
void simple_to_rgb()
A simple image is converted to a color image.
Definition: Bimage_color.cpp:21
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Boption::filename
Bstring filename()
Definition: options.h:85
Bimage::color_combine
int color_combine(Bimage *p)
Combines two colored images.
Definition: Bimage_color.cpp:370
timer.h
Utilities for timing functions.
Bimage::rgb_to_cmyk
void rgb_to_cmyk()
Converts an RGB color image to CMYK.
Definition: Bimage_color.cpp:176
Bimage
General image parameter class.
Definition: Bimage.h:163
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
Bimage::red_white_blue
Bimage * red_white_blue(double red_min, double white_min, double white_max, double blue_max)
Colorizes an image with blue positive and red negative.
Definition: Bimage_color.cpp:466
Bimage::color_blue
int color_blue(double cmin, double cmax, int gray_extremes=0)
Converts a gray-scale image to blue.
Definition: Bimage_color.cpp:325
Bimage::rgb_to_rgba
void rgb_to_rgba()
An alpha channel is added to a RGB color image.
Definition: Bimage_color.cpp:122
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
main
int main(int argc, char **argv)
Definition: bcolour.cpp:58
Bimage::cmyk_to_rgb
void cmyk_to_rgb()
Converts a CMYK color image to RGB.
Definition: Bimage_color.cpp:206
Bimage::phase_colour_wheel
int phase_colour_wheel()
Generates a phase color wheel.
Definition: Bimage_color.cpp:562
TRGB
Definition: Bimage.h:62
Boption::datatype
DataType datatype()
Definition: options.h:93
Bimage::rgba_to_rgb
void rgba_to_rgb()
The alpha channel is delete from an RGBA color image.
Definition: Bimage_color.cpp:149
Boption::next
Boption * next
Definition: options.h:28
UCharacter
Definition: utilities.h:149
Bstring
Definition: Bstring.h:29
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bimage::color_green
int color_green(double cmin, double cmax, int gray_extremes=0)
Converts a gray-scale image to green.
Definition: Bimage_color.cpp:310
Bstring::integer
long integer()
Definition: Bstring.cpp:539
use
const char * use[]
Definition: bcolour.cpp:18
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
Bimage::combine_channels
void combine_channels(long nc)
The channels are composed from individual images.
Definition: Bimage.cpp:1965
Bimage::split_channels
void split_channels()
The channels are split into individual images.
Definition: Bimage.cpp:1924
Bimage::intensities_phase_colored
Bimage * intensities_phase_colored(double scale)
Generates a power spectrum with phases colored according to a color wheel.
Definition: Bimage_complex.cpp:1358
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
verbose
int verbose