How to read in data from a pgm file in C++
Asked Answered
F

2

4

So far I can read every line and print it out to the console:

void readFile(){

   string line;
   ifstream myfile("example1.pgm");

   if (myfile.is_open()){
       while (myfile.good()){
         getline (myfile,line);
         cout << line;
       }
   }

However a pgm file apparently will always have the following at the start before the data:

P2
# test.pgm
24 7
15

How can i adapt my code so that it checks that "P2" is present, ignores any comments (#), and stores the variables and subsequent pixel data?

I'm a bit lost and new to c++ so any help is appreicated.

Thanks

Fandango answered 14/11, 2011 at 19:15 Comment(0)
D
10

There are a lot of different ways to parse a file. For something like this, you could look at the answers on this site. Personally, I would go with a loop of getline() and test/parse every line (stored in the variable "line"), you can also use a stringstream since it is easier to use with multiple values :

Idea

First line : test that P2 (Portable graymap) is present, maybe with something like

if(line.compare("P2")) ...

Second line : do nothing, you can go on with the next getline()

Third line : store the size of the image; with a stringstream you could do this

int w,h;
ss >> w >> h;

Following lines : store the pixel data until you reach the end of the file

Resulting code

You can try this code and adapt it to your needs :

#include <iostream> // cout, cerr
#include <fstream> // ifstream
#include <sstream> // stringstream
using namespace std;

int main() {
  int row = 0, col = 0, numrows = 0, numcols = 0;
  ifstream infile("file.pgm");
  stringstream ss;
  string inputLine = "";

  // First line : version
  getline(infile,inputLine);
  if(inputLine.compare("P2") != 0) cerr << "Version error" << endl;
  else cout << "Version : " << inputLine << endl;

  // Second line : comment
  getline(infile,inputLine);
  cout << "Comment : " << inputLine << endl;

  // Continue with a stringstream
  ss << infile.rdbuf();
  // Third line : size
  ss >> numcols >> numrows;
  cout << numcols << " columns and " << numrows << " rows" << endl;

  int array[numrows][numcols];

  // Following lines : data
  for(row = 0; row < numrows; ++row)
    for (col = 0; col < numcols; ++col) ss >> array[row][col];

  // Now print the array to see the result
  for(row = 0; row < numrows; ++row) {
    for(col = 0; col < numcols; ++col) {
      cout << array[row][col] << " ";
    }
    cout << endl;
  }
  infile.close();
}

EDIT

Here is a good tutorial on how to use stringstreams.

Discontent answered 14/11, 2011 at 19:25 Comment(2)
Thanks thats really helpful; How will the loop know when it is at the third line though? Do i need to introduce a counter, or what would you recomend?Fandango
I just edited my post with a piece of code that you could try.Discontent
T
2

A way of simplifying PNM (PBM/PGM/PPM) header processing is to build up a header string line-by-line until you have captured all of the requisite data. It doesn't take too much code to do this using only the standard C++ libraries...

#include <string>
#include <iostream>
#include <sstream>
#include <stdexcept>
...
std::string header, magic;
int width=0, height=0, maxsample=0, samples=0, bits=0, bytes=0;

do {
   try { getline(is,magic); } catch ( const std::ios_base::failure & ) {}
   if ( !magic.empty() && magic[0] != '#' ) header += magic+" ";
   if ( !( std::stringstream(header+" 1") >> magic >> width >> height >> maxsample ).eof() ) break;
   if ( ( (magic=="P1"||magic=="P4") && maxsample==1 ) || !is.good() ) break;
   } while ( true );

samples = magic=="P1"?1:magic=="P2"?1:magic=="P3"?3:magic=="P4"?1:magic=="P5"?1:magic=="P6"?3:0;
bits = (magic=="P1"||magic=="P4")?1:maxsample<256?8:maxsample<256*256?16:0, bytes = (width*samples*bits+7)>>3;
if ( width<=0 || height<=0 || maxsample<=0 || samples<=0 || bits<=0 ) throw std::runtime_error("invalid PNM header");

This handles comments (if present) and the special-case of PBM (no 'maxsample') -- and it works regardless of whether or not exceptions are enabled on the input stream.

Once you've read the header, reading the image data is usually a simple matter since the format is defined to just be a sequential data dump (which may be either ASCII or binary depending on the 'magic' value). In the case of 16-bit binary-encoded samples, the format specification indicates that "The most significant byte is first" (big endian), so this case may require some platform-specific handling.

As written, this requires C++11 -- probably due to the way I'm using stringstream as a temporary.

One caveat: In a pathological case, it is possible for this to waste a lot of time/RAM while attempting to read an invalid header -- since the getline call isn't inherently bounded. There's a relatively simple solution (replace getline with something more robust), but it requires a bit more code.

For production-quality applications, consider using libnetpbm.

Teleprinter answered 14/6, 2013 at 6:6 Comment(2)
A small wrapper around istream& std::istream::getline (char* s, streamsize n ); could solve the unbounded-read problem.Teleprinter
In retrospect, it's a bit confusing as to why I'm using magic in the first two lines of the loop. To clarify, I'm just using that variable as a generic input buffer at that stage of the loop -- in order to avoid declaring a separate variable for that purpose. I was going for conciseness (perhaps too much so).Teleprinter

© 2022 - 2024 — McMap. All rights reserved.