fputc(img[i][j],fp);
fclose(fp);
}
unsigned char **pgm_file_to_img(char *file_name,int *pNC,int *pNR)
#define MAXC 80
{
FILE *fp;
unsigned char **img;
char cline[MAXC];
int i,j,NR,NC;
int maxgray;
if((fp=fopen(file_name,"r")) == NULL) {
/* open image file */
printf(" *** pgm_file_to_img -- can't open file: %s ****",file_name);
exit(0); }
#ifdef WIN
_setmode( _fileno(fp), _O_BINARY); /* be sure to read files in binary mode */
#endif
comment_loop0: /* skip any comments */
fgets(cline,MAXC,fp); /* get next line from file */
if(cline[0]=='#') goto comment_loop0;
if(cline[0]!='P'||cline[1]!='5')
{
printf(" **** pgm_file_to_img -- not a pgm file: %s ****",file_name);
exit(0);
}
comment_loop1: /* skip any comments */
fgets(cline,MAXC,fp); /* get next line from file */
if(cline[0]=='#') goto comment_loop1;
sscanf(cline,"%d %d ",&NC,&NR);
comment_loop2: /* skip any comments */
fgets(cline,MAXC,fp); /* get next line from file */
if(cline[0]=='#') goto comment_loop2;
sscanf(cline,"%d ",&maxgray); /* get maxgray */
/* return size of image to caller */
*pNC=NC;
*pNR=NR;
img=alloc_img(NC,NR); /* allocate image array */
for(i=0;i
for(j=0;j
{
img[i][j]=fgetc(fp); /* read image data from file */
}
fclose(fp);
return(img);
}