Bsoft  2.1.0
Bernard's software package
Bimage_color.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "utilities.h"
11 
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14 
22 {
23  if ( compoundtype == TRGB ) return;
24 
25  if ( compoundtype != TSimple ) {
26  cerr << "Error in simple_to_rgb: Conversion must be from simple to RGB!" << endl;
27  return;
28  }
29 
30  double v;
31  double scale = 255.0/(max - min);
32  double shift = -min;
33 
34  long ds = x*y*z*n;
35  RGB<unsigned char>* rgb_data = new RGB<unsigned char>[ds];
36 
37  for ( long j=0; j<ds; j++ ) {
38  v = ((*this)[j] + shift) * scale;
39  rgb_data[j] = RGB<unsigned char>(v, v, v);
40  }
41 
43  compoundtype = TRGB;
44  c = 3;
45 
46  data_assign((unsigned char *) rgb_data);
47 
48  statistics();
49 }
50 
59 {
60  if ( compoundtype == TRGBA ) return;
61 
62  if ( compoundtype != TSimple ) {
63  cerr << "Error in simple_to_rgba: Conversion must be from simple to RGBA!" << endl;
64  return;
65  }
66 
67  double v;
68  double scale = 255.0/(max - min);
69  double shift = -min;
70 
71  long ds = x*y*z*n;
72  RGBA<unsigned char>* rgba_data = new RGBA<unsigned char>[ds];
73 
74  for ( long j=0; j<ds; j++ ) {
75  v = ((*this)[j] + shift) * scale;
76  rgba_data[j] = RGBA<unsigned char>(v, v, v, 255);
77  }
78 
80  compoundtype = TRGBA;
81  c = 4;
82 
83  data_assign((unsigned char *) rgba_data);
84 
85  statistics();
86 }
87 
95 {
96  if ( compoundtype == TSimple ) return;
97  if ( compoundtype == TComplex ) return;
98  if ( compoundtype == TCMYK ) cmyk_to_rgb();
99  if ( compoundtype == TRGBA ) rgba_to_rgb();
100 
101  long ds = x*y*z*n;
102  float* nudata = new float[ds];
103 
104  for ( long j=0; j<ds; j++ )
105  nudata[j] = (rgb(j)).average();
106 
107  data_type(Float);
108  compoundtype = TSimple;
109  c = 1;
110 
111  data_assign((unsigned char *) nudata);
112 
113  statistics();
114 }
115 
123 {
124  if ( compoundtype == TRGBA ) return;
125 
126  if ( compoundtype != TRGB ) {
127  cerr << "Error in Bimage::rgb_to_rgba: Conversion must be from RGB to RGBA!" << endl;
128  return;
129  }
130 
131  long ds = x*y*z*n;
132  RGBA<unsigned char>* rgba_data = new RGBA<unsigned char>[ds];
133 
134  for ( long j=0; j<ds; j++ )
135  rgba_data[j] = RGBA<unsigned char>((rgb(j)).r(), (rgb(j)).g(), (rgb(j)).b(), 255);
136 
138  compoundtype = TRGBA;
139  c = 4;
140 
141  data_assign((unsigned char *) rgba_data);
142 
143  statistics();
144 }
145 
150 {
151  if ( compoundtype == TRGB ) return;
152 
153  if ( compoundtype != TRGBA ) {
154  cerr << "Error in Bimage::rgba_to_rgb: Conversion must be from RGBA to RGB!" << endl;
155  return;
156  }
157 
158  long ds = x*y*z*n;
159  RGB<unsigned char>* rgb_data = new RGB<unsigned char>[ds];
160 
161  for ( long j=0; j<ds; j++ )
162  rgb_data[j] = RGB<unsigned char>((rgba(j)).r(), (rgba(j)).g(), (rgba(j)).b());
163 
165  compoundtype = TRGB;
166  c = 3;
167 
168  data_assign((unsigned char *) rgb_data);
169 
170  statistics();
171 }
172 
177 {
178  if ( compoundtype == TCMYK ) return;
179 
180  if ( compoundtype != TRGB ) {
181  cerr << "Error in Bimage::rgb_to_cmyk: Conversion must be from RGB to CMYK!" << endl;
182  return;
183  }
184 
185  long ds = x*y*z*n;
186  RGB<unsigned char> rgb_data;
187  CMYK<unsigned char>* cmyk_data = new CMYK<unsigned char>[ds];
188 
189  for ( long j=0; j<ds; j++ ) {
190  rgb_data = RGB<unsigned char>((rgb(j)).r(), (rgb(j)).g(), (rgb(j)).b());
191  cmyk_data[j] = CMYK<unsigned char>(rgb_data);
192  }
193 
195  compoundtype = TCMYK;
196  c = 4;
197 
198  data_assign((unsigned char *) cmyk_data);
199 
200  statistics();
201 }
202 
207 {
208  if ( compoundtype == TRGB ) return;
209 
210  if ( compoundtype != TCMYK ) {
211  cerr << "Error in Bimage::rgb_to_cmyk: Conversion must be from CMYK to RGB!" << endl;
212  return;
213  }
214 
215  long ds = x*y*z*n;
216  CMYK<unsigned char> cmyk_data;
217  RGB<unsigned char>* rgb_data = new RGB<unsigned char>[ds];
218 
219  for ( long j=0; j<ds; j++ ) {
220  cmyk_data = CMYK<unsigned char>((cmyk(j)).c(), (cmyk(j)).m(), (cmyk(j))[1], (cmyk(j)).k());
221  rgb_data[j] = RGB<unsigned char>(cmyk_data);
222  }
223 
225  compoundtype = TRGB;
226  c = 3;
227 
228  data_assign((unsigned char *) rgb_data);
229 
230  statistics();
231 }
232 
247 int Bimage::one_color(int color, double cmin, double cmax, int flag)
248 {
249  if ( cmin == cmax ) {
250  cmin = min;
251  cmax = max;
252  }
253 
254  if ( verbose & VERB_PROCESS )
255  cout << "Coloring the image: " << color << endl << endl;
256 
257  long i, cc;
258  double gscale = 255.0/(max - min);
259  double scale = 255.0/(cmax - cmin);
260  double v;
261 
262  RGB<unsigned char>* nudata = new RGB<unsigned char>[datasize];
263 
264  for ( i=0; i<datasize; i++ ) {
265  v = (*this)[i];
266  if ( ( v >= cmin ) && ( v <= cmax ) ) {
267  v = scale*(v - cmin);
268  if ( v < 0 ) v = 0;
269  if ( v > 255 ) v = 255;
270  if ( flag & 1 ) {
271  for ( cc = 0; cc < 3; ++cc ) {
272  if ( cc == color )
273  nudata[i][color] = 255;
274  else
275  nudata[i][cc] = (unsigned char) (255 - v);
276  }
277  } else {
278  nudata[i][color] = (unsigned char) v;
279  }
280  } else if ( flag & 2 ) {
281  v = gscale*(v - min);
282  nudata[i] = RGB<unsigned char>(v,v,v);
283  } else if ( v > cmax ) {
284  nudata[i][color] = 255;
285  }
286  }
287 
290  channels(3);
291 
292  data_assign((unsigned char *) nudata);
293 
294  return 0;
295 }
296 
307 int Bimage::color_red(double cmin, double cmax, int flag)
308 {
309  return one_color(0, cmin, cmax, flag);
310 }
311 
322 int Bimage::color_green(double cmin, double cmax, int flag)
323 {
324  return one_color(1, cmin, cmax, flag);
325 }
326 
337 int Bimage::color_blue(double cmin, double cmax, int flag)
338 {
339  return one_color(2, cmin, cmax, flag);
340 }
341 
353 {
354  if ( compoundtype != TRGB && compoundtype != TRGBA ) return 0;
355 
356  if ( verbose & VERB_LABEL )
357  cout << "Generating a pure color image" << endl << endl;
358 
359  long i, j, cc;
360  double sum, fac;
361 
362  for ( i=0; i<datasize; i+=c ) {
363  for ( sum=cc=0, j=i; cc<3; cc++, j++ ) sum += (*this)[j];
364  if ( sum ) {
365  fac = dtmax/sum;
366  for ( cc=0, j=i; cc<3; cc++, j++ )
367  set(j, (*this)[j] * fac);
368  }
369  }
370 
371  return 0;
372 }
373 
383 {
384  if ( compoundtype != TRGB && compoundtype != TRGBA ) return 0;
385  if ( p->compound_type() != TRGB && p->compound_type() != TRGBA ) return 0;
386 
387  if ( verbose & VERB_LABEL )
388  cout << "Combining two color images" << endl << endl;
389 
390  long i, j, cc;
391  double v;
392 
393  for ( i=0; i<datasize; i+=c ) {
394  for ( cc=0, j=i; cc<c; cc++, j++ ) {
395  v = (*this)[j] + (*p)[j];
396  if ( v > 255 ) v = 255;
397  set(j, v);
398  }
399  }
400 
401  return 0;
402 }
403 
404 
415 Bimage* Bimage::color_spectrum(double cmin, double cmax)
416 {
418 
419  if ( cmin == cmax ) {
420  cmin = min;
421  cmax = max;
422  }
423 
424  if ( verbose & VERB_PROCESS )
425  cout << "Coloring the image" << endl << endl;
426 
427  long i;
428  double value;
429  double gscale(255.0/(max - min));
430 
431  RGB<unsigned char>* nudata = new RGB<unsigned char>[datasize];
432 
433  for ( i=0; i<datasize; i++ ) {
434  value = (*this)[i*c];
435  if ( ( value >= cmin ) && ( value <= cmax ) ) {
436  nudata[i].spectrum(value, cmin, cmax);
437  } else {
438  value = gscale*(value - min);
439  nudata[i] = RGB<unsigned char>(value, value, value);
440  }
441  }
442 
445  channels(3);
446 
447  data_assign((unsigned char *) nudata);
448 
449  // Generate a color scale image
450  if ( verbose & VERB_PROCESS )
451  cout << "Generating a color scale image" << endl << endl;
452 
453  long j, k;
455 
456  Bimage* p = new Bimage(UCharacter, TRGB, 256, 20, 1, 1);
457 
458  for ( i=0; i<p->sizeX(); i++ ) {
459  rgb.spectrum((double)i, 0.0, 255.0);
460  for ( j=0, k=i; j<p->sizeY(); j++, k+=p->sizeX() ) p->set(k, rgb);
461  }
462 
463  return p;
464 }
465 
478 Bimage* Bimage::red_white_blue(double red_min, double white_min,
479  double white_max, double blue_max)
480 {
481  if ( verbose & VERB_PROCESS ) {
482  cout << "Generating a red-white-blue image:" << endl;
483  cout << "Red gradient start and end: " << red_min << " " << white_min << endl;
484  cout << "Blue gradient start and end: " << white_max << " " << blue_max << endl;
485  } else if ( verbose & VERB_LABEL )
486  cout << "Generating a red-white-blue image" << endl << endl;
487 
488  long i, j, k;
489  int red, blu, grn;
490  double v;
491  double blue_scale(255.0/(blue_max - white_max));
492  double red_scale(255.0/(white_min - red_min));
494 
495  RGB<unsigned char>* nudata = new RGB<unsigned char>[datasize];
496 
497  for ( i=0; i<datasize; i++ ) {
498  v = (*this)[i];
499  red = grn = blu = 255; // White part
500  if ( v > blue_max ) { // Blue part
501  red = grn = 0;
502  } else if ( v < red_min ) { // Red part
503  blu = grn = 0;
504  } else if ( v > white_max ) { // Blue gradient
505  red = (int) (blue_scale*(blue_max - v));
506  grn = (int) (blue_scale*(blue_max - v));
507  } else if ( v < white_min ) { // Red gradient
508  blu = (int) (red_scale*(v - red_min));
509  grn = (int) (red_scale*(v - red_min));
510  }
511  if ( red < 0 ) red = 0;
512  if ( red > 255 ) red = 255;
513  if ( grn < 0 ) grn = 0;
514  if ( grn > 255 ) grn = 255;
515  if ( blu < 0 ) blu = 0;
516  if ( blu > 255 ) blu = 255;
517  nudata[i] = RGB<unsigned char>(red, grn, blu);
518  }
519 
522  channels(3);
523 
524  data_assign((unsigned char *) nudata);
525 
526  // Generate a color scale image
527  if ( verbose & VERB_PROCESS )
528  cout << "Generating a color scale image: colorscale.tif" << endl << endl;
529 
530  Bimage* p = new Bimage(UCharacter, TRGB, 256, 20, 1, 1);
531  double scale((max - min)/255);
532 
533 // cout << "min=" << min << tab << "max=" << max << tab << "scale=" << scale << endl;
534 
535  for ( i=0; i<p->sizeX(); i++ ) {
536  red = grn = blu = 255; // White part
537  v = scale*i + min;
538  if ( v > blue_max ) { // Blue part
539  red = grn = 0;
540  } else if ( v < red_min ) { // Red part
541  blu = grn = 0;
542  } else if ( v > white_max ) { // Blue gradient
543  red = (int) (blue_scale*(blue_max - v));
544  grn = (int) (blue_scale*(blue_max - v));
545  } else if ( v < white_min ) { // Red gradient
546  blu = (int) (red_scale*(v - red_min));
547  grn = (int) (red_scale*(v - red_min));
548  }
549  if ( red < 0 ) red = 0;
550  if ( red > 255 ) red = 255;
551  if ( grn < 0 ) grn = 0;
552  if ( grn > 255 ) grn = 255;
553  if ( blu < 0 ) blu = 0;
554  if ( blu > 255 ) blu = 255;
555  rgb = RGB<unsigned char>(red, grn, blu);
556  for ( j=0, k=i; j<p->sizeY(); j++, k+=p->sizeX() ) p->set(k, rgb);
557  }
558 
559  return p;
560 }
561 
575 {
576  long i, xx, yy;
577  double rx, ry, a, r, rmax(256);
578 
579  origin(size()/2);
581 
582  for ( i=yy=0; yy<y; yy++ ) {
583  ry = yy - image->origin()[1];
584  for ( xx=0; xx<x; xx++, i++ ) {
585  rx = xx - image->origin()[0];
586  a = atan2(ry, rx);
587  r = sqrt(rx*rx + ry*ry);
588  if ( r < rmax ) {
589  rgb.phase(a, r);
590  set(i, rgb);
591  } else {
592  set(i, RGB<unsigned char>(0,0,0));
593  }
594  }
595  }
596 
597  return 0;
598 }
599 
600 
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::shift
int shift(Vector3< double > vec, int fill_type=0, double fill=0)
Shifts an image.
Definition: Bimage_transform.cpp:589
UCharacter
@ UCharacter
Definition: utilities.h:153
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bimage::Bimage
Bimage()
Initializes an image.
Definition: Bimage.cpp:43
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::sum
void sum(long m, Bimage **p)
Sums an array of images with their FOM blocks.
Definition: Bimage_combine.cpp:23
TComplex
@ TComplex
Definition: Bimage.h:60
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
RGB
Definition: Color.h:24
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::phase_colour_wheel
int phase_colour_wheel()
Generates a phase color wheel.
Definition: Bimage_color.cpp:574
Bimage::rgba
RGBA< double > rgba(long j)
Returns a color value at the given index.
Definition: Bimage.cpp:1069
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
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
RGB::spectrum
void spectrum(double value, double cmin, double cmax)
Definition: Color.h:89
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
TRGBA
@ TRGBA
Definition: Bimage.h:64
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
CMYK
Definition: Color.h:264
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
Bimage.h
Header file for image class.
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
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
RGBA
Definition: Color.h:162
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
TCMYK
@ TCMYK
Definition: Bimage.h:65
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
Bimage::one_color
int one_color(int color, double cmin, double cmax, int flag=0)
Converts a gray-scale image to a single color.
Definition: Bimage_color.cpp:247
Bimage::simple_to_rgb
void simple_to_rgb()
A simple image is converted to a color image.
Definition: Bimage_color.cpp:21
Bimage::cmyk
CMYK< double > cmyk(long j)
Returns a color value at the given index.
Definition: Bimage.cpp:1083
Bimage::data_assign
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:875
Bimage::color_combine
int color_combine(Bimage *p)
Combines two colored images.
Definition: Bimage_color.cpp:382
Bimage::pure_color
int pure_color()
Generates a pure color image without intensity.
Definition: Bimage_color.cpp:352
Bimage::simple_to_rgba
void simple_to_rgba()
A simple image is converted to a color image.
Definition: Bimage_color.cpp:58
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
Bimage::rgb
RGB< double > rgb(long j)
Returns a color value at the given index.
Definition: Bimage.cpp:1055
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
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