I've been trying to read a greyscale image file(pgm) and store the pixel values in an array. .pgm files have first three lines containing infos on the type(binary or ASCII) the size(64 by 64 for this image), and maximum pixel value(255). And from the fourth line, the data(pixel values in binary) begins. So I am trying to read from the fourth line.
However, when I run the code, what I get is just some unreadable broken text. The image file I am trying to open is about 5kb and can be downloaded at https://drive.google.com/file/d/1DoaaU9tPc_kO9Uu3TKjBmxTQVP3b-SO8/view?usp=sharing
Any help/suggestions would be appreciated. Thank you very much!
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
ifstream::pos_type _Start, _End, _Size, _Size2;
ifstream::off_type _newStart;
char * memblock;
std::ifstream inf;
inf.open("Aaron_Eckhart_0001.pgm", ios_base::in | ios_base::binary);
if (inf) {
std::string line;
for (int i = 0; i<3; i++)//skip the first 3 lines that contain the info
{
std::getline(inf, line);
}
_Start = inf.tellg();//starting point just after those 3 lines
inf.seekg(0, ios::end);
_End = inf.tellg();
_Size = (_End - _Start);
cout << _Size << endl;
memblock = new char[_Size];
_newStart = (ifstream::off_type) _Start;//casting _Start to off_type to be passes as offset, to start reading the file after the first three lines, idon't know if that idea is right or not
cout << _newStart << endl;
inf.seekg(_newStart, ios::beg);
inf.read(memblock, _Size);
cout << memblock << endl;
}
system("pause");
inf.close();
}
I want to store the pixel values in an array so that I can make some changes and manipulate them.