Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 15 additions & 25 deletions pnm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1132,37 +1132,27 @@ image<bit_pixel, Alloc> read_pbm_ascii(const std::string& fname)
std::string line;
std::getline(ifs, line);
line.erase(std::find(line.begin(), line.end(), '#'), line.end());
line.erase(std::remove_if(line.begin(), line.end(),
[](unsigned char x) { return std::isspace(x); }),
line.end());
if(line.empty()){continue;}

int pix;
std::istringstream iss(line);
while(!iss.eof())
char pix_read;
while(!line.empty())
{
iss >> pix;
if(iss.fail())
{
std::string dummy;
iss >> dummy;
if(!std::all_of(dummy.begin(), dummy.end(), [](const char c){
return std::isspace(static_cast<int>(c));
}))
{
throw std::runtime_error("pnm::read_pbm_ascii: "
"file contains invalid token: " + dummy);
}
}
else
pix_read = line.front();
line.erase(0,1);
pix = (pix_read == '0')? 0 : 1;
if(idx >= x * y)
{
if(idx >= x * y)
{
throw std::runtime_error("pnm::read_pbm_ascii: file " +
fname + " contains too many pixels: "_str +
std::to_string(idx) + " pixels for "_str +
std::to_string(x) + "x"_str +
std::to_string(y) + " image"_str);
}
img.raw_access(idx++) = bit_pixel(pix != 0);
throw std::runtime_error("pnm::read_pbm_ascii: file " +
fname + " contains too many pixels: "_str +
std::to_string(idx) + " pixels for "_str +
std::to_string(x) + "x"_str +
std::to_string(y) + " image"_str);
}
img.raw_access(idx++) = bit_pixel(pix != 0);
}
}
return img;
Expand Down