Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. PGM *pgm_read(const char *file) {
  2.  
  3.   int rows = 0, cols = 0;
  4.   unsigned char byte;
  5.   PGM *img;
  6.   FILE *fp;
  7.   int ptype;
  8.   int max;
  9.  
  10.   if(!(fp = fopen(file, "r"))) {
  11.       fprintf(stderr, "couldn't open file\n");
  12.       return(NULL);
  13.   }
  14.  
  15.   // READS IN HEADER INFO
  16.  
  17.   fscanf(fp, "P%d\n", &ptype);
  18.   fscanf(fp, "%d %d\n", &cols, &rows);
  19.   fscanf(fp, "%d\n", &max);
  20.  
  21.   if (!(img = pgm_alloc(rows, cols))) {
  22.     fprintf(stderr, "couldn't allocate image\n");
  23.     return (NULL);
  24.   }
  25.  
  26.   img->h = rows;
  27.   img->w = cols;
  28.   img->max = max;
  29.   img->ptype = ptype;
  30.  
  31.   //READS PIXELS
  32.   for (int i = 0; i < cols; ++i) {
  33.     for (int j = 0; j < rows; ++j) {
  34.       fread(&byte, sizeof(unsigned char), 1, fp);
  35.       img->gpix[i * cols + j] = (float)byte / (float)img->max;
  36.     }
  37.   }
  38.  
  39.     fclose(fp);
  40.   return (img);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement