38 std::ifstream imgFile( file );
42 std::cerr <<
"invalid filename: \"" << file <<
"\"" << std::endl;
46 if (
mVerbosity ) std::cerr <<
"reading image from file \"" << file <<
"\"" << std::endl;
52 imgFile.getline( buf, 256 );
53 while ( buf[0] ==
'#' ) imgFile.getline( buf, 256 );
57 mHeight = atoi( strpbrk( buf,
" \t" ) );
67 imgFile.getline( buf, 256 );
83 if (
mVerbosity ) std::cerr <<
"GrayScale RAWBITs PGM format]";
89 if (
mVerbosity ) std::cerr <<
"GrayScale ASCII PGM format]";
92 for ( j = 0; j <
mWidth; j++ )
96 mPixels[i][j] = (
unsigned char)( pix /
pow( 2, (
double)(mNumBits-8) ) );
108 if (
mVerbosity ) std::cerr <<
"RGB RAWBITs PPM format]";
109 for ( i = 0; i <
mHeight; i++ )
110 for ( j = 0; j <
mWidth; j++ )
112 unsigned char rgb[3];
113 imgFile.read( (
char *)rgb,3 );
114 mRGB[0][i][j] = (int)rgb[0];
115 mRGB[1][i][j] = (int)rgb[1];
116 mRGB[2][i][j] = (int)rgb[2];
121 if (
mVerbosity ) std::cerr <<
"RGB ASCII PPM format]";
122 for ( i = 0; i <
mHeight; i++ )
123 for ( j = 0; j <
mWidth; j++ )
125 imgFile >>
mRGB[0][i][j];
126 imgFile >> mRGB[1][i][j];
127 imgFile >> mRGB[2][i][j];
138 if (
mVerbosity ) std::cerr <<
"Binary RAWBITs PBM format]";
139 for ( i = 0; i <
mHeight; i++ )
140 for ( j = 0; j <
mWidth; j += 8 )
143 imgFile.read( pix, 1 );
144 unsigned int x = (
unsigned int)pix[0];
145 for (
int k = 0; k < 8; k++ )
147 unsigned int y = x/( (
unsigned int)
pow( 2, (
double)(7-k) ) );
152 x -= y*( (
unsigned int)
pow( 2, (
double)(7-k) ) );
158 if (
mVerbosity ) std::cerr <<
"Binary ASCII PBM format]";
159 for ( i = 0; i <
mHeight; i++ )
160 for ( j = 0; j <
mWidth; j++ )
176 std::cerr << std::endl;
177 std::cerr <<
"\twidth = " <<
mWidth << std::endl;
178 std::cerr <<
"\theight = " << mHeight << std::endl;
179 std::cerr <<
"\t#pixels = " <<
mNumPixels << std::endl;
192 std::ofstream outfile( file );
200 outfile <<
"# grayscale image" << std::endl;
204 for (
int i = 0; i <
mHeight; i++ )
230 for( j = 0; j <
mWidth; j++ )
231 mPixels[i][j] = (
unsigned char)output[i][j];
240 std::ofstream outfile( filename );
241 unsigned char rgb[3];
246 outfile <<
"P6" << std::endl;
247 outfile <<
"# color image" << std::endl;
248 outfile << width <<
" " << height << std::endl;
249 outfile << 255 << std::endl;
251 for (
int i = 0; i < height; i++ )
252 for (
int j = 0; j < width; j++ )
254 rgb[0] = (
unsigned char)(pixels[0][i][j]*255.0);
255 rgb[1] = (
unsigned char)(pixels[1][i][j]*255.0);
256 rgb[2] = (
unsigned char)(pixels[2][i][j]*255.0);
257 outfile.write( (
char *)rgb, 3 );
267 void PGMImage::Write(
char* filename,
float** pixels,
int height,
int width,
int channel )
269 std::ofstream outfile( filename );
270 unsigned char rgb[3];
277 outfile <<
"P6" << std::endl;
278 outfile <<
"# color image" << std::endl;
279 outfile << width <<
" " << height << std::endl;
280 outfile << 255 << std::endl;
283 max = min = pixels[0][0];
284 for( i = 0; i < height; i++ )
285 for( j = 0; j < width; j++ )
287 if( pixels[i][j] > max ) max = pixels[i][j];
288 if( pixels[i][j] < min ) min = pixels[i][j];
293 for ( i = 0; i < height; i++ )
295 for ( j = 0; j < width; j++ )
297 rgb[0] = (
unsigned char)(255.0 * ( (pixels[i][j] - min ) / maxmin ));
301 outfile.write( (
char *)rgb, 3 );
304 else if ( channel == 1 )
305 for ( i = 0; i < height; i++ )
307 for ( j = 0; j < width; j++ )
310 rgb[1] = (
unsigned char)(255.0 * ( (pixels[i][j] - min ) / maxmin ));
312 outfile.write( (
char *)rgb, 3 );
316 for ( i = 0; i < height; i++ )
318 for ( j = 0; j < width; j++ )
322 rgb[2] = (
unsigned char)(255.0 * ( (pixels[i][j] - min ) / maxmin ));
323 outfile.write( (
char *)rgb, 3 );
351 max = min = output[0][0];
353 for( j = 0; j <
mWidth; j++ )
355 if( output[i][j] > max ) max = output[i][j];
356 if( output[i][j] < min ) min = output[i][j];
361 for( j = 0; j <
mWidth; j++ )
362 mPixels[i][j] = (
unsigned char)(255.0 * ( (output[i][j] -
min ) / maxmin ));
float pow(float a, double b)
void Allocate(int dataset)
vigra::RGBValue< T, RIDX, GIDX, BIDX > log(vigra::RGBValue< T, RIDX, GIDX, BIDX > const &v)
component-wise logarithm
void WriteScaled(char *filename, float **output, int height, int width)