/* this code was written by Donald Quon and Hai Pham for use in CISC 859 These routines are for the PGM,PBM (8 bit greyscale) images*/ #include #include #include void readpbm(char *fname,long **image,int *dx,int *dy){ /* input: fname : name of input file output: **image: pointer to store image (memory will be allocated in this function; *dx,*dy; width and height of image*/ FILE *fptr; int temp; char buffer[100]; /*line shouldn't be over 70 characters*/ long w,h; long count; int numcolor; fptr=fopen(fname,"r"); if (fptr==NULL){ printf("error opening input file\n"); exit(0); } /* filter out comment lines */ buffer[0]='#'; while (buffer[0]=='#') fscanf(fptr,"%s",buffer); /*check MAGIC NUMBER "P5"*/ if ((buffer[0]!='P')&&(buffer[1]!='5')){ printf("not a 255 Greyscale PGM (PBM raw) file\n"); fclose(fptr); exit (0); } /* filter out comment lines */ buffer[0]='#'; while (buffer[0]=='#'){ fscanf(fptr,"%s",buffer); if (buffer[0]=='#') { fgets (&buffer[1],90,fptr); } } sscanf(buffer,"%ld",&w); buffer[0]='#'; while (buffer[0]=='#'){ fscanf(fptr,"%s",buffer); if (buffer[0]=='#'){ fgets(&buffer[1],90,fptr); } } sscanf(buffer,"%ld",&h); buffer[0]='#'; while (buffer[0]=='#'){ fscanf(fptr,"%s",buffer); if (buffer[0]=='#'){ fgets(&buffer[1],90,fptr); } } sscanf(buffer,"%d",&numcolor); if ((numcolor<0)||(numcolor>255)){ printf("not a 255 Greyscale PGM (PBM raw) file\n"); fclose(fptr); exit (0); } *image=(long *)malloc(w*h*sizeof(long)); if (*image==NULL){ printf("error allocating memory\n"); fclose(fptr); exit(0); } for (count=0;count