File: ImageColorConverter.h

package info (click to toggle)
primrose 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,304 kB
  • sloc: cpp: 27,318; php: 765; ansic: 636; objc: 272; sh: 136; makefile: 92; perl: 67
file content (573 lines) | stat: -rw-r--r-- 14,334 bytes parent folder | download | duplicates (30)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
 * Modification History
 *
 * 2001-February-26		Jason Rohrer
 * Created. 
 * Added functions for converting to and from gray byte arrays.
 *
 * 2001-September-19		Jason Rohrer
 * Added an RGB->HSB conversion function, which was copied
 * from minorGems/ai/robotics/ImageStatistics.
 * Added RGB<->YIQ functions.
 *
 * 2001-September-20		Jason Rohrer
 * Fixed a bug in the YIQ conversion.
 * Got rid of this bug fix, as it distorts the YIQ space,
 * and there is no way to prevent colors from going out of the
 * [0,1] range all of the time anyway.
 *
 * 2001-September-24		Jason Rohrer
 * Added RGB<->YCbCr functions.
 * Abstracted out a common coefficient multiplying function.
 */

#ifndef IMAGE_COLOR_CONVERTER_INCLUDED
#define IMAGE_COLOR_CONVERTER_INCLUDED

#include "Image.h"


/**
 * A container class for static functions that convert
 * images between various color spaces.
 *
 * @author Jason Rohrer
 */ 
class ImageColorConverter {
	
	public:
		
		/**
		 * Converts a 3-channel RGB image to a 1-channel grayscale
		 * image using an NTSC luminosity standard.
		 *
		 * @param inImage the RGB image to convert.  Must be destroyed
		 *   by caller.
		 *
		 * @return a new, grayscale version of inImage.  Must be
		 *   destroyed by the caller.  Returns NULL if inImage
		 *   is not a 3-channel image.
		 */
		static Image *RGBToGrayscale( Image *inImage );
		
		/**
		 * Converts a 1-channel grayscae image to a 3-channel RGB
		 * image.
		 *
		 * @param inImage the grayscale image to convert.  Must be destroyed
		 *   by caller.
		 *
		 * @return a new, RGB version of inImage.  Must be
		 *   destroyed by the caller.  Returns NULL if inImage
		 *   is not a 1-channel image.
		 */
		static Image *grayscaleToRGB( Image *inImage );
		

		/**
		 * Converts a 1-channel grayscae image to a 1-channel byte array.
		 *
		 * @param inImage the grayscale image to convert.  Must be destroyed
		 *   by caller.
		 * @param inChannelNumber the channel number to use as the 
		 *   gray channel.  Defaults to 0;
		 *
		 * @return a new byte array with one byte per image pixel,
		 *   and image [0,1] values mapped to [0,255].
		 */
		static unsigned char *grayscaleToByteArray( Image *inImage,
			int inChannelNumber = 0 );
		
		/**
		 * Converts a byte array to a 1-channel grayscale
		 * image.
		 *
		 * @param inBytes the byte array to convert.  Must be destroyed
		 *   by caller.
		 * @param inWidth the width of the image contained in the byte array.
		 * @param inHeight the height of the image contained in the byte array.
		 *
		 * @return a new, grayscale image version of the byte array.  Must be
		 *   destroyed by the caller.
		 */
		static Image *byteArrayToGrayscale( unsigned char *inBytes,
			int inWidth, int inHeight );


		
		/**
		 * Converts an RGB image to HSB.
		 *
		 * @param inRGBImage the rgb image to convert.
		 *   Must be destroyed by caller.
		 *
		 * @return a new image that is the HSB conversion of the
		 *   RGB image.  Must be destroyed by caller.
		 */
		static Image *RGBToHSB( Image *inRGBImage );



		/**
		 * Converts an RGB image to YIQ.
		 *
		 * Note that color values in the resulting YIQ
		 * image may lie outside of the range [0,1].
		 *
		 * @param inRGBImage the rgb image to convert.
		 *   Must be destroyed by caller.
		 *
		 * @return a new image that is the YIQ conversion of the
		 *   RGB image.  Must be destroyed by caller.
		 */
		static Image *RGBToYIQ( Image *inRGBImage );



		/**
		 * Converts a YIQ image to RGB.
		 *
		 *
		 * Note that color values in the resulting RGB
		 * image may lie outside of the range [0,1].
		 *
		 * @param inYIQImage the rgb image to convert.
		 *   Must be destroyed by caller.
		 *
		 * @return a new image that is the RGB conversion of the
		 *   YIQ image.  Must be destroyed by caller.
		 */
		static Image *YIQToRGB( Image *inYIQImage );



		/**
		 * Converts an RGB image to YCbCr.
		 *
		 * Note that in the YCbCr standard, Y is in the range
		 * [0,1], while Cb and Cr are both in the range [-0.5, 0.5].
		 * This function returns Cb and Cr components shifted
		 * into the range [0,1].
		 *
		 * @param inRGBImage the rgb image to convert.
		 *   Must be destroyed by caller.
		 *
		 * @return a new image that is the YCbCr conversion of the
		 *   RGB image.  Must be destroyed by caller.
		 */
		static Image *RGBToYCbCr( Image *inRGBImage );



		/**
		 * Converts a YCbCr image to RGB.
		 *
		 *
		 * Note that in the YCbCr standard, Y is in the range
		 * [0,1], while Cb and Cr are both in the range [-0.5, 0.5].
		 * This function expects input Cb and Cr components to be shifted
		 * into the range [0,1].
		 *
		 * @param inYCbCrImage the rgb image to convert.
		 *   Must be destroyed by caller.
		 *
		 * @return a new image that is the RGB conversion of the
		 *   YCbCr image.  Must be destroyed by caller.
		 */
		static Image *YCbCrToRGB( Image *inYCbCrImage );


		
	protected:

		/**
		 * Converts an 3-channel image to another 3-channel
		 * image using a matrix of conversion coefficients.
		 *
		 * The following formulae are used;
		 * outChan0 = inC00 * inChan0 + inC01 * inChan1 + inC02 * inChan2
		 * outChan1 = inC10 * inChan0 + inC11 * inChan1 + inC12 * inChan2
		 * outChan2 = inC20 * inChan0 + inC21 * inChan1 + inC22 * inChan2
		 *
		 * @param inImage the image to convert.
		 *   Must be destroyed by caller.
		 *
		 * @return a new image that is inImage converted, or NULL if
		 *   conversion failed (usually because inImage does not
		 *   contain 3 channels).
		 *   Must be destroyed by caller.
		 */
		static Image *coefficientConvert( Image *inImage,
										  double inC00,
										  double inC01,
										  double inC02,
										  double inC10,
										  double inC11,
										  double inC12,
										  double inC20,
										  double inC21,
										  double inC22 );
											 
	};		



inline Image *ImageColorConverter::RGBToGrayscale( Image *inImage ) {
	int w = inImage->getWidth();
	int h = inImage->getHeight();
	int numPixels = w * h;
	
	Image *grayImage = new Image( w, h, 1 );
	
	double *red = inImage->getChannel( 0 );
	double *green = inImage->getChannel( 1 );
	double *blue = inImage->getChannel( 2 );
	
	double *gray = grayImage->getChannel( 0 );
	
	for( int i=0; i<numPixels; i++ ) {
		// NTSC luminosity formula
		gray[i] = .299 * red[i] + .587 * green[i] + .114 * blue[i];
		}
		
	return grayImage;
	}
		
		

inline Image *ImageColorConverter::grayscaleToRGB( Image *inImage ) {
	int w = inImage->getWidth();
	int h = inImage->getHeight();
	int numPixels = w * h;
	
	Image *rgbImage = new Image( w, h, 3 );
	
	double *red = rgbImage->getChannel( 0 );
	double *green = rgbImage->getChannel( 1 );
	double *blue = rgbImage->getChannel( 2 );
	
	double *gray = inImage->getChannel( 0 );
	
	for( int i=0; i<numPixels; i++ ) {
		red[i] = gray[i];
		green[i] = gray[i];
		blue[i] = gray[i];
		}
		
	return rgbImage;
	}


inline unsigned char *ImageColorConverter::
	grayscaleToByteArray( Image *inImage, int inChannelNumber ) {
	
	int w = inImage->getWidth();
	int h = inImage->getHeight();
	int numPixels = w * h;
	
	unsigned char *bytes = new unsigned char[ numPixels ];
	
	double *gray = inImage->getChannel( inChannelNumber );
	
	for( int i=0; i<numPixels; i++ ) {
		bytes[i] = (unsigned char)( 255 * gray[i] );
		}
		
	return bytes;
	}



inline Image *ImageColorConverter::
	byteArrayToGrayscale( unsigned char *inBytes,
	int inWidth, int inHeight ) {
	
	int w = inWidth;
	int h = inHeight;
	int numPixels = w * h;
	
	Image *grayImage = new Image( w, h, 1 );
	
	
	double *gray = grayImage->getChannel( 0 );
	
	for( int i=0; i<numPixels; i++ ) {
		gray[i] = (double)( inBytes[i] ) / 255.0;
		}
		
	return grayImage;
	}



inline Image *ImageColorConverter::RGBToHSB( Image *inRGBImage ) {
	// idea modeled after Java Color class.
	
	if( inRGBImage->getNumChannels() != 3 ) {
		printf(
			"RGBtoHSB requires a 3-channel image as input.\n" );
		return NULL;
		}

	int w = inRGBImage->getWidth();
	int h = inRGBImage->getHeight();

	int numPixels = w * h;
				
	Image *hsbImage = new Image( w, h, 3 );

	double *redChannel = inRGBImage->getChannel( 0 );
	double *greenChannel = inRGBImage->getChannel( 1 );
	double *blueChannel = inRGBImage->getChannel( 2 );

	double *hueChannel = hsbImage->getChannel( 0 );
	double *satChannel = hsbImage->getChannel( 1 );
	double *brightChannel = hsbImage->getChannel( 2 );

				
	for( int i=0; i<numPixels; i++ ) {
		int r = (int)( 255 * redChannel[i] );
		int g = (int)( 255 * greenChannel[i] );
		int b = (int)( 255 * blueChannel[i] );

		double hue, sat, bright;
				
		int cmax = (r > g) ? r : g;
		if (b > cmax) {
			cmax = b;
			}

		int cmin = (r < g) ? r : g;
		if (b < cmin) {
			cmin = b;
			}
								
		bright = ( (double)cmax ) / 255.0;
		if( cmax != 0 ) {
			sat = ( (double)( cmax - cmin ) ) / ( (double) cmax );
			}
		else {
			sat = 0;
			}
		if( sat == 0 ) {
			hue = 0;
			}
		else {
			double redc =
				( (double)( cmax - r ) ) / ( (double)( cmax - cmin ) );
			double greenc =
				( (double) ( cmax - g ) ) / ( (double)( cmax - cmin ) );
			double bluec =
				( (double)( cmax - b ) ) / ( (double)( cmax - cmin ) );

			if( r == cmax ) {
				hue = bluec - greenc;
				}
			else if( g == cmax ) {
				hue = 2.0 + redc - bluec;
				}
			else {
				hue = 4.0 + greenc - redc;
				}
			hue = hue / 6.0;
												
			if( hue < 0 ) {
				hue = hue + 1.0;
				}
			}

		hueChannel[i] = hue;
		satChannel[i] = sat;
		brightChannel[i] = bright;
		}

	return hsbImage;
	}



inline Image *ImageColorConverter::RGBToYIQ( Image *inRGBImage ) {
	if( inRGBImage->getNumChannels() != 3 ) {
		printf(
			"RGBtoYIQ requires a 3-channel image as input.\n" );
		return NULL;
		}

	Image *yiqImage = coefficientConvert( inRGBImage,
										  0.299,  0.587,  0.114,
										  0.596, -0.274, -0.322,
										  0.212, -0.523,  0.311 );

	return yiqImage;
	}



inline Image *ImageColorConverter::YIQToRGB( Image *inYIQImage ) {
	if( inYIQImage->getNumChannels() != 3 ) {
		printf(
			"YIQtoRGB requires a 3-channel image as input.\n" );
		return NULL;
		}

	Image *rgbImage = coefficientConvert( inYIQImage,
										  1.0,  0.956,  0.621,
										  1.0, -0.272, -0.647,
										  1.0, -1.105,  1.702 );
	return rgbImage;
	}



inline Image *ImageColorConverter::RGBToYCbCr( Image *inRGBImage ) {
	if( inRGBImage->getNumChannels() != 3 ) {
		printf(
			"RGBtoYCbCr requires a 3-channel image as input.\n" );
		return NULL;
		}

	// coefficients taken from the color space faq
	/*
	  RGB -> YCbCr (with Rec 601-1 specs)
	  Y  =  0.2989 * Red + 0.5866 * Green + 0.1145 * Blue
	  Cb = -0.1687 * Red - 0.3312 * Green + 0.5000 * Blue
	  Cr =  0.5000 * Red - 0.4183 * Green - 0.0816 * Blue

	  YCbCr (with Rec 601-1 specs) -> RGB
	  Red   = Y + 0.0000 * Cb + 1.4022 * Cr
	  Green = Y - 0.3456 * Cb - 0.7145 * Cr
	  Blue  = Y + 1.7710 * Cb + 0.0000 * Cr
	*/
	
	Image *ycbcrImage = coefficientConvert( inRGBImage,
											 0.2989,  0.5866,  0.1145,
											-0.1687, -0.3312,  0.5000,
											 0.5000, -0.4183, -0.0816 );

	// adjust the Cb and Cr channels so they are in the range [0,1]
	int numPixels = ycbcrImage->getWidth() * ycbcrImage->getHeight();
	double *cbChannel = ycbcrImage->getChannel( 1 );
	double *crChannel = ycbcrImage->getChannel( 2 );
	for( int i=0; i<numPixels; i++ ) {
		cbChannel[i] += 0.5;
		crChannel[i] += 0.5;
		}

	// no need to clip pixels to the range [0,1], since
	// all possible rgb pixel values are represented by in-range
	// yCbCr pixel values

	
	return ycbcrImage;
	}



inline Image *ImageColorConverter::YCbCrToRGB( Image *inYCbCrImage ) {
	if( inYCbCrImage->getNumChannels() != 3 ) {
		printf(
			"YCbCrtoRGB requires a 3-channel image as input.\n" );
		return NULL;
		}

	// adjust the normalized Cb and Cr channels
	// so they are in the range [-0.5,0.5]
	
	int numPixels = inYCbCrImage->getWidth() * inYCbCrImage->getHeight();
	double *cbChannel = inYCbCrImage->getChannel( 1 );
	double *crChannel = inYCbCrImage->getChannel( 2 );
	for( int i=0; i<numPixels; i++ ) {
		cbChannel[i] -= 0.5;
		crChannel[i] -= 0.5;
		}

	// coefficients taken from the color space faq
	/*
	  RGB -> YCbCr (with Rec 601-1 specs)
	  Y  =  0.2989 * Red + 0.5866 * Green + 0.1145 * Blue
	  Cb = -0.1687 * Red - 0.3312 * Green + 0.5000 * Blue
	  Cr =  0.5000 * Red - 0.4183 * Green - 0.0816 * Blue

	  YCbCr (with Rec 601-1 specs) -> RGB
	  Red   = Y + 0.0000 * Cb + 1.4022 * Cr
	  Green = Y - 0.3456 * Cb - 0.7145 * Cr
	  Blue  = Y + 1.7710 * Cb + 0.0000 * Cr
	*/
	
	Image *rgbImage = coefficientConvert( inYCbCrImage,
										  1.0,  0.0000,  1.4022,
										  1.0, -0.3456, -0.7145,
										  1.0,  1.7710,  0.0000 );

	// clip r, g, and b channels to the range [0,1], since
	// some YCbCr pixel values might map out of this range
	// (in other words, some YCbCr values map outside of rgb space)
	for( int c=0; c<3; c++ ) {
		double *channel = rgbImage->getChannel( c );
		
		for( int p=0; p<numPixels; p++ ) {
			if( channel[p] < 0 ) {
				channel[p] = 0;
				}
			else if( channel[p] > 1 ) {
				channel[p] = 1.0;
				}
			}
		}
	
	return rgbImage;
	}



inline Image *ImageColorConverter::coefficientConvert( Image *inImage,
													   double inC00,
													   double inC01,
													   double inC02,
													   double inC10,
													   double inC11,
													   double inC12,
													   double inC20,
													   double inC21,
													   double inC22 ) {
	if( inImage->getNumChannels() != 3 ) {
		return NULL;
		}

	
	int w = inImage->getWidth();
	int h = inImage->getHeight();

	int numPixels = w * h;
				
	Image *outImage = new Image( w, h, 3 );

	double *outChannel0 = outImage->getChannel( 0 );
	double *outChannel1 = outImage->getChannel( 1 );
	double *outChannel2 = outImage->getChannel( 2 );

	double *inChannel0 = inImage->getChannel( 0 );
	double *inChannel1 = inImage->getChannel( 1 );
	double *inChannel2 = inImage->getChannel( 2 );
	
	for( int i=0; i<numPixels; i++ ) {
		outChannel0[i] =
			inC00 * inChannel0[i] +
			inC01 * inChannel1[i] +
			inC02 * inChannel2[i];
		outChannel1[i] =
			inC10 * inChannel0[i] +
			inC11 * inChannel1[i] +
			inC12 * inChannel2[i];
		outChannel2[i] =
			inC20 * inChannel0[i] +
			inC21 * inChannel1[i] +
			inC22 * inChannel2[i];
		}

	return outImage;
	}



#endif