title: Read PPM image issues
body: <p>I've been having tons of trouble getting my algorithm to succesfully read a PPM image... 
  It works perfectly with some images but fails miserably with others, resulting in a half gray (RGB 205, 205, 205) image.</p>
  
  <p>I have tried everything I could find and researched for hours. I've been stuck for a week now... </p>
  
  <p>I hope you guys can help.</p>
  
  <pre><code>    Image* pnm_read(char* filePath)
      {
          FILE* file;
          char token[20];
          int imageWidth, imageHeight, maximumColorValue;
          Image* image;
  
          /* Abre arquivo PNM. */
          file = fopen(filePath, "r");
          if (file == NULL)
          {
              fprintf(stderr, "Não foi possível localizar o arquivo de imagem %s.\n", filePath);
              return 0;
          }
  
          /* Lê Magic Number do cabecalho e vê se é P6*/
          pnm_get_token(file, token, sizeof token);
          if (strcmp(token, "P6"))
          {
              fprintf(stderr, "%s não é um arquivo PPM válido.\n", filePath);
              fclose(file);
              return 0;
          }
  
          //Lê widht, height e valor máximo rgb
          if (sscanf(pnm_get_token(file, token, sizeof token), "%d", &amp;imageWidth)        != 1 ||
              sscanf(pnm_get_token(file, token, sizeof token), "%d", &amp;imageHeight)       != 1 ||
              sscanf(pnm_get_token(file, token, sizeof token), "%d", &amp;maximumColorValue) != 1)
          {
              fprintf(stderr, "%s não é um arquivo PNM válido.\n", filePath);
              fclose(file);
              return 0;
          }
  
          //Se não for RGB com componentes de 8 bits (0-255) dá erro
          if (maximumColorValue != 255)
          {
              fprintf(stderr, "%s does not have 8-bit components: maximumColorValue=%d\n", filePath, maximumColorValue);
              fclose(file);
              return 0;
          }
  
          image = new Image(imageWidth, imageHeight);
          unsigned char* pixelComponents = new unsigned char[imageWidth * imageHeight * 3];
          fread(pixelComponents, sizeof(unsigned char), imageWidth * imageHeight * 3, file);
          fclose(file);
  
          int r, g, b, pixel;
  
          for(int i = 3; i &lt;= imageWidth * imageHeight * 3; i += 3)
          {   
              r = pixelComponents[i-3] &amp; 0xff;
              g = pixelComponents[i-2] &amp; 0xff;
              b = pixelComponents[i-1] &amp; 0xff;
  
              /*fread(&amp;r, sizeof(char), sizeof(char), file);
              fread(&amp;g, sizeof(char), sizeof(char), file);
              fread(&amp;b, sizeof(char), sizeof(char), file);
  
              r = r &amp; 0xff;
              g = g &amp; 0xff;
              b = b &amp; 0xff;*/
  
              pixel = (255 &lt;&lt; 24) | (r &lt;&lt; 16) | (g &lt;&lt; 8) | b;             
  
              // Atribuindo os pixels e virando imagem de cabeca para baixo
              image-&gt;pixels[ (imageWidth * imageHeight) - (i/3) - 1] = pixel;
          }
  
          printf("Lido arquivo PNM (%s): %dx%d pixels.\n", filePath, image-&gt;width, image-&gt;height);
  
          return image;
      }
  </code></pre>