I am trying read the Netpbm image format, following the specification explained here. The ascii types for the format (which have P1, P2 and P3 as magic number), I can read without problems. But I have issues reading the binary data in these files (whose with P4, P5 and P6 as magic number) - the header for the file (which is ascii) I am able to read without problem.
In the link, it is stated that:
In the binary formats, PBM uses 1 bit per pixel, PGM uses 8 or 16 bits per pixel, and PPM uses 24 bits per pixel: 8 for red, 8 for green, 8 for blue. Some readers and writers may support 48 bits per pixel (16 each for R,G,B), but this is still rare.
with this, I try use this answer to read the data, bit by bit, and got this code:
if(*this->magicNumber == "P4") {
  this->pixels = new Matrix<int>(this->width, this->height);
  vector<int> p;
  while(getline(file, line_pixels)) {
    if(line_pixels.size() > 0 && line_pixels.at(0) != '#') {
      string byte;
      stringstream ss(line_pixels);
      while(getline(ss, byte)) {
        unsigned char c = (unsigned char)byte.at(0);
        for(int x=0; x != 8; x++) p.push_back( (c & (1 << x)) != 0 );
      }
    }
  }
  int count = 0;
  for(int i=0; i<height; i++) {
    for(int j=0; j<width; j++) {
      this->pixels->set(i, j, p[count++]);
    }
  }
}
but when I try read the image named as sample_640×426.pbm in this link, I should get this result:
but I am getting this result instead:
For the binary format for PGM and PPM images, when I try open the image, I got a segmentation fault error when I try increment count at some point in the execution of the loop. I think somehow the size of vector<int> p is ending bigger than the expected product width x height.
the code for the PGM format:
if(*this->magicNumber == "P5") {
  this->pixels = new Matrix<int>(this->width, this->height);
  vector<int> p;
  while(getline(file, line_pixels)) {
    if(line_pixels.size() > 0 && line_pixels.at(0) != '#') {
      string number;
      stringstream ss(line_pixels);
      while(getline(ss, number)) {
        unsigned char data = (unsigned char)number.at(0);
        p.push_back((int)data);
      }
    }
  }
  int count = 0;
  for(int i=0; i<height; i++) {
    for(int j=0; j<width; j++) {
      this->pixels->set(i, j, p[count++]);
    }
  }
}
the code for the PPM format:
if(*this->magicNumber == "P6") {
  this->pixels = new Matrix<struct Pixel>(this->width, this->height);
  vector<int> p;
  while(getline(file, line_pixels)) {
    if(line_pixels.size() > 0 && line_pixels.at(0) != '#') {
      string byte;
      stringstream ss(line_pixels);
      while(getline(ss, byte)) {
        unsigned char data = (unsigned char)byte.at(0);
        p.push_back((int)data);
      }
    }
  }
  int count = 0;
  for(int i=0; i<height; i++) {
    for(int j=0; j<width; j++) {
      struct Pixel pixel;
      pixel.r = p[count++];
      pixel.g = p[count++];
      pixel.b = p[count++];
      this->pixels->set(i, j, pixel);
    }
  }
}
Anyone can give a hint of what I am doing wrong here?

