Bsoft  2.1.0
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 "-subtractive Flag to calculate subtractive one-color conversions.",
47 "-grayextremes Flag to keep gray extremes beyond colorization limits.",
48 " ",
49 "Input:",
50 "-add file.tif Add this file to the input file (must be the same size).",
51 " ",
52 "Output:",
53 "-scale file.tif Output spectral scale image.",
54 "-phasewheel pcw.png Output a phase color wheel (size 512,512,1)."
55 " ",
56 NULL
57 };
58 
59 int main(int argc, char **argv)
60 {
61  // Initialize variables
62  DataType nudatatype(Unknown_Type); // Conversion to new type
63  int setimg(-1); // Selects all images
64  int setinvert(0);
65  int flag(0); // Flag: 1=subtractive, 2=gray scale extremes
66  int setred(0);
67  int setgreen(0);
68  int setblue(0);
69  int setspectrum(0);
70  int setrwb(0);
71  int setalpha(0); // No alpha channel change
72  int to_cmyk(0);
73  int to_rgb(0);
74  int colour_phases(0);
75  int calcintensity(0);
76  int calcpure(0);
77  int split(0);
78  int combine(0);
79  double cmin(0), cmax(0); // Min/max for conversion to spectrum
80  double red_min(0), white_min(0), white_max(0), blue_max(0);
81  Bstring add_image; // Image to add to input file
82  Bstring scale_image; // Spectral scale image
83  Bstring pcw; // Phase color wheel image
84 
85  int optind;
86  Boption* option = get_option_list(use, argc, argv, optind);
87  Boption* curropt;
88  for ( curropt = option; curropt; curropt = curropt->next ) {
89  if ( curropt->tag == "datatype" )
90  nudatatype = curropt->datatype();
91  if ( curropt->tag == "invert" )
92  setinvert = 1;
93  if ( curropt->tag == "red" ) {
94  if ( curropt->values(cmin, cmax) < 2 )
95  cerr << "-red: A grayscale range must be specified!" << endl;
96  else
97  setred = 1;
98  }
99  if ( curropt->tag == "green" ) {
100  if ( curropt->values(cmin, cmax) < 2 )
101  cerr << "-green: A grayscale range must be specified!" << endl;
102  else
103  setgreen = 1;
104  }
105  if ( curropt->tag == "blue" ) {
106  if ( curropt->values(cmin, cmax) < 2 )
107  cerr << "-blue: A grayscale range must be specified!" << endl;
108  else
109  setblue = 1;
110  }
111  if ( curropt->tag == "spectrum" ) {
112  if ( curropt->values(cmin, cmax) < 2 )
113  cerr << "-spectrum: A grayscale range must be specified!" << endl;
114  else
115  setspectrum = 1;
116  }
117  if ( curropt->tag == "rwb" ) {
118  if ( curropt->values(red_min, white_min,
119  white_max, blue_max) < 4 )
120  cerr << "-rwb: 4 grayscale values must be specified!" << endl;
121  else
122  setrwb = 1;
123  }
124  if ( curropt->tag == "phases" )
125  colour_phases = 1;
126  if ( curropt->tag == "intensities" )
127  calcintensity = 1;
128  if ( curropt->tag == "pure" )
129  calcpure = 1;
130  if ( curropt->tag == "alpha" )
131  setalpha = 1;
132  if ( curropt->tag == "noalpha" )
133  setalpha = -1;
134  if ( curropt->tag == "torgb" )
135  to_rgb = 1;
136  if ( curropt->tag == "tocmyk" )
137  to_cmyk = 1;
138  if ( curropt->tag == "split" )
139  split = 1;
140  if ( curropt->tag == "combine" )
141  if ( ( combine = curropt->value.integer() ) < 1 )
142  cerr << "-combine: The number of channels must be specified!" << endl;
143  if ( curropt->tag == "subtractive" )
144  flag |= 1;
145  if ( curropt->tag == "grayextremes" )
146  flag |= 2;
147  if ( curropt->tag == "add" )
148  add_image = curropt->filename();
149  if ( curropt->tag == "scale" )
150  scale_image = curropt->filename();
151  if ( curropt->tag == "phasewheel" )
152  pcw = curropt->filename();
153  }
154  option_kill(option);
155 
156  double ti = timer_start();
157 
158  if ( pcw.length() ) {
159  Bimage* pnu = new Bimage(UCharacter, TRGB, 512, 512, 1, 1);
160  pnu->phase_colour_wheel();
161  write_img(pcw, pnu, 0);
162  delete pnu;
163  }
164 
165  if ( optind >= argc ) bexit(0);
166 
167  // Read image file
168  int dataflag(0);
169  if ( optind < argc - 1 ) dataflag = 1;
170  Bimage* p = read_img(argv[optind++], dataflag, setimg);
171  if ( p == NULL ) {
172  cerr << "Error: No input file read!" << endl;
173  bexit(-1);
174  }
175 
176  Bimage* p2 = NULL;
177  Bimage* pscale = NULL;
178 
179  if ( optind < argc ) {
180  if ( setinvert ) p->invert();
181 
182  if ( to_cmyk ) {
183  p->simple_to_rgb();
184  p->rgb_to_cmyk();
185  }
186 
187  if ( to_rgb ) {
188  p->simple_to_rgb();
189  p->cmyk_to_rgb();
190  }
191 
192  if ( setred ) p->color_red(cmin, cmax, flag);
193 
194  if ( setgreen ) p->color_green(cmin, cmax, flag);
195 
196  if ( setblue ) p->color_blue(cmin, cmax, flag);
197 
198  if ( setspectrum ) pscale = p->color_spectrum(cmin, cmax);
199 
200  if ( setrwb )
201  pscale = p->red_white_blue(red_min, white_min, white_max, blue_max);
202 
203  if ( colour_phases ) {
204  p2 = p->intensities_phase_colored(0);
205  if ( p2 ) {
206  delete p;
207  p = p2;
208  }
209  }
210 
211  if ( calcintensity ) p->color_to_simple();
212 
213  if ( calcpure ) p->pure_color();
214 
215  if ( setalpha > 0 ) p->rgb_to_rgba();
216  else if ( setalpha < 0 ) p->rgba_to_rgb();
217 
218  if ( add_image.length() ) {
219  p2 = read_img(add_image, 1, -1);
220  p->color_combine(p2);
221  delete p2;
222  }
223 
224  if ( split ) p->split_channels();
225 
226  if ( combine ) p->combine_channels(combine);
227 
228  p->change_type(nudatatype);
229 
230 
231  write_img(argv[optind], p, 0);
232  }
233 
234  if ( scale_image.length() && pscale ) {
235  write_img(scale_image, pscale, 0);
236  delete pscale;
237  }
238 
239  delete p;
240 
241  if ( verbose & VERB_TIME )
242  timer_report(ti);
243 
244  bexit(0);
245 }
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::color_green
int color_green(double cmin, double cmax, int flag=0)
Converts a gray-scale image to green.
Definition: Bimage_color.cpp:322
Bimage::rgb_to_cmyk
void rgb_to_cmyk()
Converts an RGB color image to CMYK.
Definition: Bimage_color.cpp:176
Bimage::combine_channels
void combine_channels(long nc)
The channels are composed from individual images.
Definition: Bimage.cpp:1987
UCharacter
@ UCharacter
Definition: utilities.h:153
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
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
options.h
Header file for option handlers.
Bimage::split_channels
void split_channels()
The channels are split into individual images.
Definition: Bimage.cpp:1946
use
const char * use[]
Definition: bcolour.cpp:18
Bimage::phase_colour_wheel
int phase_colour_wheel()
Generates a phase color wheel.
Definition: Bimage_color.cpp:574
Bimage::color_spectrum
Bimage * color_spectrum(double cmin, double cmax)
Colorizes an image to a spectrum.
Definition: Bimage_color.cpp:415
Bimage::rgba_to_rgb
void rgba_to_rgb()
The alpha channel is delete from an RGBA color image.
Definition: Bimage_color.cpp:149
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:478
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Boption::value
Bstring value
Definition: options.h:30
Bimage::cmyk_to_rgb
void cmyk_to_rgb()
Converts a CMYK color image to RGB.
Definition: Bimage_color.cpp:206
TRGB
@ TRGB
Definition: Bimage.h:63
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
Bimage::color_blue
int color_blue(double cmin, double cmax, int flag=0)
Converts a gray-scale image to blue.
Definition: Bimage_color.cpp:337
utilities.h
Header file for general utilities.
verbose
int verbose
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::rgb_to_rgba
void rgb_to_rgba()
An alpha channel is added to a RGB color image.
Definition: Bimage_color.cpp:122
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
rwimg.h
Header file for 2D and 3D image I/O.
Bimage::simple_to_rgb
void simple_to_rgb()
A simple image is converted to a color image.
Definition: Bimage_color.cpp:21
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3111
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:1363
Bimage::color_combine
int color_combine(Bimage *p)
Combines two colored images.
Definition: Bimage_color.cpp:382
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::pure_color
int pure_color()
Generates a pure color image without intensity.
Definition: Bimage_color.cpp:352
main
int main(int argc, char **argv)
Definition: bcolour.cpp:59
Bimage::color_red
int color_red(double cmin, double cmax, int flag=0)
Converts a gray-scale image to red.
Definition: Bimage_color.cpp:307