How to read a PGM image properly in C

c p2 pgm
sibly22 picture sibly22 · Mar 4, 2016 · Viewed 10.3k times · Source

I'm here to ask you a little help. I'd like to write a C code that reads a PGM file (P2, not binary) and I have found many ways to do it on the Web. The problem is that every time I try to read some PGM images I have as examples on my PC, I'm not even able to read the header properly, because it never recognizes the right P2 PGM format. I always get errors like: "not valid pgm file type" or "format unsupported". Here's the (last) code I'm trying:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    typedef struct pgm {
      int w;
      int h;
      int max;
      int* pData;
    } pgm;


    int main(int argc, char * argv[]){

      char* filename = argv[0];
      pgm* pPgm;
      FILE* ifp;
      int word;
      int nRead = 0;
      char readChars[256];

      //open the file, check if successful
      ifp = fopen( filename, "r" );
      if (!ifp) {
        printf("Error: Unable to open file %s.\n\n", filename);
        exit(1);
      }

      pPgm = (pgm *) malloc (sizeof(pgm));

      //read headers from file
      printf ("Reading PGM file: %s...\n", filename);
      fscanf (ifp, "%s", readChars);

      if (strcmp(readChars, "P2") == 0) {
        //valid file type
        //get a word from the file
        printf("VALID TYPE.\n");
        fscanf (ifp, "%s", readChars);
        while (readChars[0] == '#') {
          //if a comment, get the rest of the line and a new word
          fgets (readChars, 255, ifp);
          fscanf (ifp, "%s", readChars);
        }

        //ok, comments are gone
        //get width, height, color depth
        sscanf (readChars, "%d", &pPgm->w);
        fscanf (ifp, "%d", &pPgm->h);
        fscanf (ifp, "%d", &pPgm->max);
        printf("WIDTH: %d, HEIGHT: %d\n", pPgm->w, pPgm->h);

        // allocate some memory, note that on the HandyBoard you want to 
        // use constant memory and NOT use calloc/malloc
        pPgm->pData = (int*)malloc(sizeof(int) * pPgm->w * pPgm->h);

        // now read in the image data itself    
        for (nRead = 0; nRead < pPgm->w * pPgm->h; nRead++) {
          fscanf(ifp, "%d" ,&word);
          pPgm->pData[nRead] = word;
          // printf("nRead = %d %d\n",nRead,pPgm->pData[nRead]);
        }

        printf ("Loaded PGM. Size: %dx%d, Greyscale: %d \n", 
        pPgm->w, pPgm->h, pPgm->max + 1);
      }
      else {
        printf ("Error: %s. Format unsupported.\n\n", readChars);
        exit(1);
      }
      fclose(ifp);

      return 0;
    }

Answer

user1919235 picture user1919235 · Mar 4, 2016

It seems that there are libraries that can do this: libnetpbm from netpbm or PGMA_IO. If you cannot use an external library for some reason, a look at the source code may help you to figure out how the header is read. By the way, have you already had a look at the answer to this question: how-to-read-a-pgm-image-file-in-a-2d-double-array-in-c?