| 1 |
/* |
| 2 |
* Modification History |
| 3 |
* |
| 4 |
* 2001-September-24 Jason Rohrer |
| 5 |
* Created. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "minorGems/graphics/Image.h" |
| 9 |
#include "minorGems/graphics/ImageColorConverter.h" |
| 10 |
|
| 11 |
#include "minorGems/graphics/converters/TGAImageConverter.h" |
| 12 |
|
| 13 |
#include "minorGems/io/file/File.h" |
| 14 |
#include "minorGems/io/file/FileInputStream.h" |
| 15 |
#include "minorGems/io/file/FileOutputStream.h" |
| 16 |
|
| 17 |
#include <stdio.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
void usage( char *inAppName ); |
| 24 |
Image *loadTGAImage( char *inFileName ); |
| 25 |
void writeTGAImage( char *inFileName, Image *inImage ); |
| 26 |
|
| 27 |
// normalizes an image into the pixel range [0,1] if some |
| 28 |
// pixels are out of range |
| 29 |
void normalizeImage( Image *inImage ); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
// a test program that converts a tga image between |
| 34 |
// parameterizable color formats |
| 35 |
int main( char inNumArgs, char **inArgs ) { |
| 36 |
|
| 37 |
if( inNumArgs != 5 ) { |
| 38 |
usage( inArgs[0] ); |
| 39 |
} |
| 40 |
|
| 41 |
Image *inImage = loadTGAImage( inArgs[1] ); |
| 42 |
|
| 43 |
Image *outImage; |
| 44 |
|
| 45 |
// convert inImage to outImage based on the color space strings |
| 46 |
if( !strcmp( inArgs[3], "rgb" ) ) { |
| 47 |
if( !strcmp( inArgs[4], "yiq" ) ) { |
| 48 |
outImage = ImageColorConverter::RGBToYIQ( inImage ); |
| 49 |
} |
| 50 |
if( !strcmp( inArgs[4], "ycbcr" ) ) { |
| 51 |
outImage = ImageColorConverter::RGBToYCbCr( inImage ); |
| 52 |
} |
| 53 |
} |
| 54 |
if( !strcmp( inArgs[3], "yiq" ) ) { |
| 55 |
if( !strcmp( inArgs[4], "rgb" ) ) { |
| 56 |
outImage = ImageColorConverter::YIQToRGB( inImage ); |
| 57 |
} |
| 58 |
if( !strcmp( inArgs[4], "ycbcr" ) ) { |
| 59 |
Image *tempImage = ImageColorConverter::YIQToRGB( inImage ); |
| 60 |
|
| 61 |
outImage = ImageColorConverter::RGBToYCbCr( tempImage ); |
| 62 |
|
| 63 |
delete tempImage; |
| 64 |
} |
| 65 |
} |
| 66 |
if( !strcmp( inArgs[3], "ycbcr" ) ) { |
| 67 |
if( !strcmp( inArgs[4], "rgb" ) ) { |
| 68 |
outImage = ImageColorConverter::YCbCrToRGB( inImage ); |
| 69 |
} |
| 70 |
if( !strcmp( inArgs[4], "yiq" ) ) { |
| 71 |
Image *tempImage = ImageColorConverter::YCbCrToRGB( inImage ); |
| 72 |
|
| 73 |
outImage = ImageColorConverter::RGBToYIQ( tempImage ); |
| 74 |
|
| 75 |
delete tempImage; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
writeTGAImage( inArgs[2], outImage ); |
| 80 |
|
| 81 |
delete inImage; |
| 82 |
delete outImage; |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
void usage( char *inAppName ) { |
| 89 |
printf( "Usage:\n" ); |
| 90 |
printf( "\t%s in_image_tga out_image_tga in_format out_format\n", |
| 91 |
inAppName ); |
| 92 |
printf( "Example:\n" ); |
| 93 |
printf( "\t%s testRGB.tga testYIQ.bmp rgb yiq\n", inAppName ); |
| 94 |
|
| 95 |
printf( "The following color formats are supported:\n" ); |
| 96 |
printf( "\trgb, yiq, ycbcr\n" ); |
| 97 |
|
| 98 |
exit( 1 ); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
void normalizeImage( Image *inImage ) { |
| 104 |
// now normalize the image so that pixels are in the range [0,1] |
| 105 |
|
| 106 |
int numPixels = inImage->getWidth() * inImage->getHeight(); |
| 107 |
int numChannels = inImage->getNumChannels(); |
| 108 |
double minValue = 1000.0; |
| 109 |
double maxValue = -1000.0; |
| 110 |
int c; |
| 111 |
|
| 112 |
// search for min and max values |
| 113 |
for( c=0; c<numChannels; c++ ) { |
| 114 |
double *channel = inImage->getChannel( c ); |
| 115 |
for( int p=0; p<numPixels; p++ ) { |
| 116 |
if( channel[p] < minValue ) { |
| 117 |
minValue = channel[p]; |
| 118 |
} |
| 119 |
else if( channel[p] > maxValue ) { |
| 120 |
maxValue = channel[p]; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
double diff = maxValue - minValue; |
| 126 |
|
| 127 |
if( minValue >= 0 && diff + minValue <= 1 ) { |
| 128 |
// no need to normalize, as values are already |
| 129 |
// in range |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
double scale = 1.0 / diff; |
| 134 |
|
| 135 |
// now normalize all pixels |
| 136 |
for( c=0; c<numChannels; c++ ) { |
| 137 |
double *channel = inImage->getChannel( c ); |
| 138 |
for( int p=0; p<numPixels; p++ ) { |
| 139 |
channel[p] = ( channel[p] - minValue ) * scale; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
Image *loadTGAImage( char *inFileName ) { |
| 147 |
File *imageFile = new File( NULL, inFileName, |
| 148 |
strlen( inFileName ) ); |
| 149 |
FileInputStream *imageStream |
| 150 |
= new FileInputStream( imageFile ); |
| 151 |
|
| 152 |
TGAImageConverter *converter = new TGAImageConverter(); |
| 153 |
|
| 154 |
Image *returnImage = converter->deformatImage( imageStream ); |
| 155 |
|
| 156 |
delete imageFile; |
| 157 |
delete imageStream; |
| 158 |
delete converter; |
| 159 |
|
| 160 |
return returnImage; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
void writeTGAImage( char *inFileName, Image *inImage ) { |
| 166 |
File *imageFile = new File( NULL, inFileName, |
| 167 |
strlen( inFileName ) ); |
| 168 |
FileOutputStream *imageStream |
| 169 |
= new FileOutputStream( imageFile ); |
| 170 |
|
| 171 |
TGAImageConverter *converter = new TGAImageConverter(); |
| 172 |
|
| 173 |
converter->formatImage( inImage, imageStream ); |
| 174 |
|
| 175 |
delete imageFile; |
| 176 |
delete imageStream; |
| 177 |
delete converter; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|