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