| 1 |
/* |
| 2 |
* Modification History |
| 3 |
* |
| 4 |
* 2001-September-19 Jason Rohrer |
| 5 |
* Created. |
| 6 |
* |
| 7 |
* 2001-September-20 Jason Rohrer |
| 8 |
* Finished initial implementation. |
| 9 |
* Changed so that images are normalized to [0,1] before |
| 10 |
* being output. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "minorGems/graphics/Image.h" |
| 14 |
#include "minorGems/graphics/ImageColorConverter.h" |
| 15 |
|
| 16 |
#include "minorGems/graphics/converters/BMPImageConverter.h" |
| 17 |
|
| 18 |
#include "minorGems/io/file/File.h" |
| 19 |
#include "minorGems/io/file/FileInputStream.h" |
| 20 |
#include "minorGems/io/file/FileOutputStream.h" |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
void usage( char *inAppName ); |
| 28 |
Image *loadBMPImage( char *inFileName ); |
| 29 |
void writeBMPImage( char *inFileName, Image *inImage ); |
| 30 |
|
| 31 |
// normalizes an image into the pixel range [0,1] if some |
| 32 |
// pixels are out of range |
| 33 |
void normalizeImage( Image *inImage ); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
// a test program that converts an rgb BMP to a yiq BMP |
| 38 |
int main( char inNumArgs, char **inArgs ) { |
| 39 |
|
| 40 |
if( inNumArgs != 3 ) { |
| 41 |
usage( inArgs[0] ); |
| 42 |
} |
| 43 |
|
| 44 |
Image *rgbImage = loadBMPImage( inArgs[1] ); |
| 45 |
|
| 46 |
Image *yiqImage = ImageColorConverter::RGBToYIQ( rgbImage ); |
| 47 |
|
| 48 |
normalizeImage( yiqImage ); |
| 49 |
|
| 50 |
|
| 51 |
writeBMPImage( inArgs[2], yiqImage ); |
| 52 |
|
| 53 |
delete rgbImage; |
| 54 |
delete yiqImage; |
| 55 |
|
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
void usage( char *inAppName ) { |
| 61 |
printf( "Usage:\n" ); |
| 62 |
printf( "\t%s rgb_image_bmp yiq_image_bmp\n", inAppName ); |
| 63 |
printf( "Example:\n" ); |
| 64 |
printf( "\t%s testRGB.bmp testYIQ.bmp\n", inAppName ); |
| 65 |
|
| 66 |
exit( 1 ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
void normalizeImage( Image *inImage ) { |
| 72 |
// now normalize the image so that pixels are in the range [0,1] |
| 73 |
|
| 74 |
int numPixels = inImage->getWidth() * inImage->getHeight(); |
| 75 |
int numChannels = inImage->getNumChannels(); |
| 76 |
double minValue = 1000.0; |
| 77 |
double maxValue = -1000.0; |
| 78 |
int c; |
| 79 |
|
| 80 |
// search for min and max values |
| 81 |
for( c=0; c<numChannels; c++ ) { |
| 82 |
double *channel = inImage->getChannel( c ); |
| 83 |
for( int p=0; p<numPixels; p++ ) { |
| 84 |
if( channel[p] < minValue ) { |
| 85 |
minValue = channel[p]; |
| 86 |
} |
| 87 |
else if( channel[p] > maxValue ) { |
| 88 |
maxValue = channel[p]; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
double diff = maxValue - minValue; |
| 94 |
|
| 95 |
if( minValue >= 0 && diff + minValue <= 1 ) { |
| 96 |
// no need to normalize, as values are already |
| 97 |
// in range |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
double scale = 1.0 / diff; |
| 102 |
|
| 103 |
// now normalize all pixels |
| 104 |
for( c=0; c<numChannels; c++ ) { |
| 105 |
double *channel = inImage->getChannel( c ); |
| 106 |
for( int p=0; p<numPixels; p++ ) { |
| 107 |
channel[p] = ( channel[p] - minValue ) * scale; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
Image *loadBMPImage( char *inFileName ) { |
| 115 |
File *imageFile = new File( NULL, inFileName, |
| 116 |
strlen( inFileName ) ); |
| 117 |
FileInputStream *imageStream |
| 118 |
= new FileInputStream( imageFile ); |
| 119 |
|
| 120 |
BMPImageConverter *converter = new BMPImageConverter(); |
| 121 |
|
| 122 |
Image *returnImage = converter->deformatImage( imageStream ); |
| 123 |
|
| 124 |
delete imageFile; |
| 125 |
delete imageStream; |
| 126 |
delete converter; |
| 127 |
|
| 128 |
return returnImage; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
void writeBMPImage( char *inFileName, Image *inImage ) { |
| 134 |
File *imageFile = new File( NULL, inFileName, |
| 135 |
strlen( inFileName ) ); |
| 136 |
FileOutputStream *imageStream |
| 137 |
= new FileOutputStream( imageFile ); |
| 138 |
|
| 139 |
BMPImageConverter *converter = new BMPImageConverter(); |
| 140 |
|
| 141 |
converter->formatImage( inImage, imageStream ); |
| 142 |
|
| 143 |
delete imageFile; |
| 144 |
delete imageStream; |
| 145 |
delete converter; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|