TypedImage LoadPpm(std::ifstream& bFile) { // Parse header std::string ppm_type = ""; int num_colors = 0; int w = 0; int h = 0; bFile >> ppm_type; PpmConsumeWhitespaceAndComments(bFile); bFile >> w; PpmConsumeWhitespaceAndComments(bFile); bFile >> h; PpmConsumeWhitespaceAndComments(bFile); bFile >> num_colors; bFile.ignore(1,'\n'); if(!bFile.fail() && w > 0 && h > 0) { TypedImage img(w, h, PpmFormat(ppm_type, num_colors) ); // Read in data for(size_t r=0; r<img.h; ++r) { bFile.read( (char*)img.ptr + r*img.pitch, img.pitch ); } if(!bFile.fail()) { return img; } } throw std::runtime_error("Unable to load PPM file."); }
TypedImage LoadPpm(std::ifstream& bFile) { // Parse header std::string ppm_type = ""; int num_colors = 0; int w = 0; int h = 0; bFile >> ppm_type; PpmConsumeWhitespaceAndComments(bFile); bFile >> w; PpmConsumeWhitespaceAndComments(bFile); bFile >> h; PpmConsumeWhitespaceAndComments(bFile); bFile >> num_colors; bFile.ignore(1,'\n'); TypedImage img; bool success = !bFile.fail() && w > 0 && h > 0; if(success) { img.Alloc(w, h, PpmFormat(ppm_type, num_colors) ); // Read in data for(size_t r=0; r<img.h; ++r) { bFile.read( (char*)img.ptr + r*img.pitch, img.pitch ); } success = !bFile.fail(); } if(!success) { img.Dealloc(); } return img; }