int main(){
std::fstream myfile; // file object is created
myfile.open ("green.ppm");
std::string line;
unsigned red,green,blue; //to output values on 0 -255 scale.
int width, height = 0;
if (myfile.is_open())
{
std::getline (myfile,line); //type of file, skip, it will always be for this code p6
std::getline (myfile,line); // width and height of the image
std::stringstream lineStream(line); //extract the width and height;
lineStream >> width;
lineStream >> height;
// std::cout<< width << " " << height <<" \n";
getline (myfile,line); //skip magic number
getline (myfile,line); // reach the matrix of numbers
for (int i = 0; i<(width*height*3) ; i= i+3){
char num = line[i]; uint8_t number = num; red = number;
num = line[i+1]; number = num; green = number;
num = line[i+2]; number = num; blue = number;
std::cout<<"pixel " << i/3 << " is " << red << " " << green << " " << blue << std::endl;
}
//char to uint_8t to unsigned is a basic an inefficient way I found that takes the pixel rgb values in my ppm file and allows me to interpret them from a range of 0-255
}
// cout<<counter<<endl;
myfile.close();
return 0;
}
When I run this code over different ppm images it does actually extract the rgb values correctly however the issue is that it doesn't do it entirely. a basic 800 x 800 image has 640000 pixels and this code reads about 40800 and then ends as if it there weren't anymore.
I think this arises from a misunderstanding of the ppm format itself. I thought that beyond the header format which is the file with the type, width and size, and magic number there was only one more line and no more '\n' characters. Therefore the matrix could be read as a contiguous array of chars.
So why is this program stopping at such odd place?