SHARE
TWEET

Untitled

a guest Feb 26th, 2020 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top