| Bsoft 2.0.5 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
21 void Bimage::simple_to_rgb()
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
42 data_type(UCharacter);
43 compoundtype = TRGB;
44 c = 3;
45
46 data_assign((unsigned char *) rgb_data);
47
48 statistics();
49 }
50
58 void Bimage::simple_to_rgba()
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
79 data_type(UCharacter);
80 compoundtype = TRGBA;
81 c = 4;
82
83 data_assign((unsigned char *) rgba_data);
84
85 statistics();
86 }
87
94 void Bimage::color_to_simple()
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
122 void Bimage::rgb_to_rgba()
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
137 data_type(UCharacter);
138 compoundtype = TRGBA;
139 c = 4;
140
141 data_assign((unsigned char *) rgba_data);
142
143 statistics();
144 }
145
149 void Bimage::rgba_to_rgb()
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
164 data_type(UCharacter);
165 compoundtype = TRGB;
166 c = 3;
167
168 data_assign((unsigned char *) rgb_data);
169
170 statistics();
171 }
172
176 void Bimage::rgb_to_cmyk()
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
194 data_type(UCharacter);
195 compoundtype = TCMYK;
196 c = 4;
197
198 data_assign((unsigned char *) cmyk_data);
199
200 statistics();
201 }
202
206 void Bimage::cmyk_to_rgb()
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
224 data_type(UCharacter);
225 compoundtype = TRGB;
226 c = 3;
227
228 data_assign((unsigned char *) rgb_data);
229
230 statistics();
231 }
232
244 int Bimage::one_color(int color, double cmin, double cmax, int gray_extremes)
245 {
246 if ( cmin == cmax ) {
247 cmin = min;
248 cmax = max;
249 }
250
251 if ( verbose & VERB_PROCESS )
252 cout << "Coloring the image: " << color << endl << endl;
253
254 long i;
255 double gscale = 255.0/(max - min);
256 double scale = 255.0/(cmax - cmin);
257 double v;
258
259 RGB<unsigned char>* nudata = new RGB<unsigned char>[datasize];
260
261 for ( i=0; i<datasize; i++ ) {
262 v = (*this)[i];
263 if ( ( v >= cmin ) && ( v <= cmax ) ) {
264 v = scale*(v - cmin);
265 if ( v < 0 ) v = 0;
266 if ( v > 255 ) v = 255;
267 nudata[i][color] = (unsigned char) v;
268 } else if ( gray_extremes ) {
269 v = gscale*(v - min);
270 nudata[i] = RGB<unsigned char>(v,v,v);
271 } else if ( v > cmax ) {
272 nudata[i][color] = 255;
273 }
274 }
275
276 data_type(UCharacter);
277 compound_type(TRGB);
278 channels(3);
279
280 data_assign((unsigned char *) nudata);
281
282 return 0;
283 }
284
295 int Bimage::color_red(double cmin, double cmax, int gray_extremes)
296 {
297 return one_color(0, cmin, cmax, gray_extremes);
298 }
299
310 int Bimage::color_green(double cmin, double cmax, int gray_extremes)
311 {
312 return one_color(1, cmin, cmax, gray_extremes);
313 }
314
325 int Bimage::color_blue(double cmin, double cmax, int gray_extremes)
326 {
327 return one_color(2, cmin, cmax, gray_extremes);
328 }
329
340 int Bimage::pure_color()
341 {
342 if ( compoundtype != TRGB && compoundtype != TRGBA ) return 0;
343
344 if ( verbose & VERB_LABEL )
345 cout << "Generating a pure color image" << endl << endl;
346
347 long i, j, cc;
348 double sum, fac;
349
350 for ( i=0; i<datasize; i+=c ) {
351 for ( sum=cc=0, j=i; cc<3; cc++, j++ ) sum += (*this)[j];
352 if ( sum ) {
353 fac = dtmax/sum;
354 for ( cc=0, j=i; cc<3; cc++, j++ )
355 set(j, (*this)[j] * fac);
356 }
357 }
358
359 return 0;
360 }
361
370 int Bimage::color_combine(Bimage* p)
371 {
372 if ( compoundtype != TRGB && compoundtype != TRGBA ) return 0;
373 if ( p->compound_type() != TRGB && p->compound_type() != TRGBA ) return 0;
374
375 if ( verbose & VERB_LABEL )
376 cout << "Combining two color images" << endl << endl;
377
378 long i, j, cc;
379 double v;
380
381 for ( i=0; i<datasize; i+=c ) {
382 for ( cc=0, j=i; cc<c; cc++, j++ ) {
383 v = (*this)[j] + (*p)[j];
384 if ( v > 255 ) v = 255;
385 set(j, v);
386 }
387 }
388
389 return 0;
390 }
391
392
403 Bimage* Bimage::color_spectrum(double cmin, double cmax)
404 {
405 change_type(Float);
406
407 if ( cmin == cmax ) {
408 cmin = min;
409 cmax = max;
410 }
411
412 if ( verbose & VERB_PROCESS )
413 cout << "Coloring the image" << endl << endl;
414
415 long i;
416 double value;
417 double gscale(255.0/(max - min));
418
419 RGB<unsigned char>* nudata = new RGB<unsigned char>[datasize];
420
421 for ( i=0; i<datasize; i++ ) {
422 value = (*this)[i*c];
423 if ( ( value >= cmin ) && ( value <= cmax ) ) {
424 nudata[i].spectrum(value, cmin, cmax);
425 } else {
426 value = gscale*(value - min);
427 nudata[i] = RGB<unsigned char>(value, value, value);
428 }
429 }
430
431 data_type(UCharacter);
432 compound_type(TRGB);
433 channels(3);
434
435 data_assign((unsigned char *) nudata);
436
437 // Generate a color scale image
438 if ( verbose & VERB_PROCESS )
439 cout << "Generating a color scale image" << endl << endl;
440
441 long j, k;
442 RGB<unsigned char> rgb;
443
444 Bimage* p = new Bimage(UCharacter, TRGB, 256, 20, 1, 1);
445
446 for ( i=0; i<p->sizeX(); i++ ) {
447 rgb.spectrum((double)i, 0.0, 255.0);
448 for ( j=0, k=i; j<p->sizeY(); j++, k+=p->sizeX() ) p->set(k, rgb);
449 }
450
451 return p;
452 }
453
466 Bimage* Bimage::red_white_blue(double red_min, double white_min,
467 double white_max, double blue_max)
468 {
469 if ( verbose & VERB_PROCESS ) {
470 cout << "Generating a red-white-blue image:" << endl;
471 cout << "Red gradient start and end: " << red_min << " " << white_min << endl;
472 cout << "Blue gradient start and end: " << white_max << " " << blue_max << endl;
473 } else if ( verbose & VERB_LABEL )
474 cout << "Generating a red-white-blue image" << endl << endl;
475
476 long i, j, k;
477 int red, blu, grn;
478 double v;
479 double blue_scale(255.0/(blue_max - white_max));
480 double red_scale(255.0/(white_min - red_min));
481 RGB<unsigned char> rgb;
482
483 RGB<unsigned char>* nudata = new RGB<unsigned char>[datasize];
484
485 for ( i=0; i<datasize; i++ ) {
486 v = (*this)[i];
487 red = grn = blu = 255; // White part
488 if ( v > blue_max ) { // Blue part
489 red = grn = 0;
490 } else if ( v < red_min ) { // Red part
491 blu = grn = 0;
492 } else if ( v > white_max ) { // Blue gradient
493 red = (int) (blue_scale*(blue_max - v));
494 grn = (int) (blue_scale*(blue_max - v));
495 } else if ( v < white_min ) { // Red gradient
496 blu = (int) (red_scale*(v - red_min));
497 grn = (int) (red_scale*(v - red_min));
498 }
499 if ( red < 0 ) red = 0;
500 if ( red > 255 ) red = 255;
501 if ( grn < 0 ) grn = 0;
502 if ( grn > 255 ) grn = 255;
503 if ( blu < 0 ) blu = 0;
504 if ( blu > 255 ) blu = 255;
505 nudata[i] = RGB<unsigned char>(red, grn, blu);
506 }
507
508 data_type(UCharacter);
509 compound_type(TRGB);
510 channels(3);
511
512 data_assign((unsigned char *) nudata);
513
514 // Generate a color scale image
515 if ( verbose & VERB_PROCESS )
516 cout << "Generating a color scale image: colorscale.tif" << endl << endl;
517
518 Bimage* p = new Bimage(UCharacter, TRGB, 256, 20, 1, 1);
519 double scale((max - min)/255);
520
521 // cout << "min=" << min << tab << "max=" << max << tab << "scale=" << scale << endl;
522
523 for ( i=0; i<p->sizeX(); i++ ) {
524 red = grn = blu = 255; // White part
525 v = scale*i + min;
526 if ( v > blue_max ) { // Blue part
527 red = grn = 0;
528 } else if ( v < red_min ) { // Red part
529 blu = grn = 0;
530 } else if ( v > white_max ) { // Blue gradient
531 red = (int) (blue_scale*(blue_max - v));
532 grn = (int) (blue_scale*(blue_max - v));
533 } else if ( v < white_min ) { // Red gradient
534 blu = (int) (red_scale*(v - red_min));
535 grn = (int) (red_scale*(v - red_min));
536 }
537 if ( red < 0 ) red = 0;
538 if ( red > 255 ) red = 255;
539 if ( grn < 0 ) grn = 0;
540 if ( grn > 255 ) grn = 255;
541 if ( blu < 0 ) blu = 0;
542 if ( blu > 255 ) blu = 255;
543 rgb = RGB<unsigned char>(red, grn, blu);
544 for ( j=0, k=i; j<p->sizeY(); j++, k+=p->sizeX() ) p->set(k, rgb);
545 }
546
547 return p;
548 }
549
562 int Bimage::phase_colour_wheel()
563 {
564 long i, xx, yy;
565 double rx, ry, a, r, rmax(256);
566
567 origin(size()/2);
568 RGB<unsigned char> rgb;
569
570 for ( i=yy=0; yy<y; yy++ ) {
571 ry = yy - image->origin()[1];
572 for ( xx=0; xx<x; xx++, i++ ) {
573 rx = xx - image->origin()[0];
574 a = atan2(ry, rx);
575 r = sqrt(rx*rx + ry*ry);
576 if ( r < rmax ) {
577 rgb.phase(a, r);
578 set(i, rgb);
579 } else {
580 set(i, RGB<unsigned char>(0,0,0));
581 }
582 }
583 }
584
585 return 0;
586 }
587
588
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
Bimage.h
Header file for image class.
Bimage::one_color
int one_color(int color, double cmin, double cmax, int gray_extremes=0)
Converts a gray-scale image to a single color.
Definition: Bimage_color.cpp:244
utilities.h
Header file for general utilities.
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:351
RGBA
Definition: Color.h:11
Bimage::rgba
RGBA< double > rgba(long j)
Returns a color value at the given index.
Definition: Bimage.cpp:1047
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
Bimage::simple_to_rgb
void simple_to_rgb()
A simple image is converted to a color image.
Definition: Bimage_color.cpp:21
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
TComplex
Definition: Bimage.h:59
Bimage::Bimage
Bimage()
Initializes an image.
Definition: Bimage.cpp:44
Bimage::color_combine
int color_combine(Bimage *p)
Combines two colored images.
Definition: Bimage_color.cpp:370
TCMYK
Definition: Bimage.h:64
TSimple
Definition: Bimage.h:58
Float
Definition: utilities.h:157
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
Bimage::shift
int shift(Vector3< double > vec, int fill_type=0, double fill=0)
Shifts an image.
Definition: Bimage_transform.cpp:587
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::cmyk
CMYK< double > cmyk(long j)
Returns a color value at the given index.
Definition: Bimage.cpp:1061
Bimage::data_assign
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:852
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
RGB
Definition: Color.h:24
Bimage::sum
void sum(long m, Bimage **p)
Sums an array of images with their FOM blocks.
Definition: Bimage_combine.cpp:23
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
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bimage::channels
long channels()
Definition: Bimage.h:371
Bimage::rgb
RGB< double > rgb(long j)
Returns a color value at the given index.
Definition: Bimage.cpp:1033
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
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1096
TRGB
Definition: Bimage.h:62
Bimage::rgba_to_rgb
void rgba_to_rgb()
The alpha channel is delete from an RGBA color image.
Definition: Bimage_color.cpp:149
UCharacter
Definition: utilities.h:149
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
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
RGB::spectrum
void spectrum(double value, double cmin, double cmax)
Definition: Color.h:89
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
TRGBA
Definition: Bimage.h:63
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
CMYK
Definition: Color.h:12
verbose
int verbose
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
Bimage::simple_to_rgba
void simple_to_rgba()
A simple image is converted to a color image.
Definition: Bimage_color.cpp:58