Loads a PGM format file... Author: Rosario Peluso
218{
223 int colors, pix, i, j, p2, p5=0;
224 char Id[10], Buffer[BUFSIZ];
225 FILE *in = fopen(
file,
"rb" );
226 int NRows, NCols;
227 int save = 32;
228
229 fgets( Id, 10, in );
230 if( (p2 = strncmp(Id, "P2", 2)) && (p5 = strncmp(Id, "P5", 2)) )
231 {
232 printf("LoadPGM: Error Id\n"); return 0;
233 }
234
235 while( (fgets(Buffer, BUFSIZ, in) !=
NULL) && (*Buffer ==
'#') )
236 ;
237
238 if( sscanf(Buffer, "%d %d", &NRows, &NCols) != 2 )
239 { printf("LoadPGM: Error NRows, &NCols\n"); return 0; }
240
244
245 if( fscanf(in, "%d\n", &colors) != 1 )
246 { printf("LoadPGM: Error\n"); return 0; }
247
249
250 if( colors != 255 )
251 printf("LoadPGM: Warning: colors = %d \n", colors);
252
253 char *
Pixel =
new char[NRows * NCols + save];
254
255 int counter = 0;
256 if( !p2 )
257 {
258 for( j = 0; j < NRows; j++ )
259 for( i = 0; i < NCols; i++ )
260 {
261 if( fscanf(in, "%d", &pix) != 1 ) printf("LoadPGM: Error\n");
262 Pixel[j * NCols + i] = pix;
263 counter++;
264 }
265 }
266 else if( !p5 && ((counter = fread((
void *)
Pixel, 1, NRows * NCols, in)) !=
eBytes) )
267 {
268 printf( "LoadPGM: Error loading binary file\n" );
269 return( 0 );
270 }
271
273
275
276 printf(
"EdbImage::LoadPGM: file %s is readed...\n",
file);
277
278 return counter;
279}
#define NULL
Definition: nidaqmx.h:84