-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPBM.hpp
More file actions
39 lines (29 loc) · 947 Bytes
/
PBM.hpp
File metadata and controls
39 lines (29 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <fstream>
#include <string>
#include <vector>
struct PBM {
size_t width, height;
std::vector<int> bits;
PBM(size_t width, size_t height) {
this->width = width;
this->height = height;
this->bits = std::vector<int>(width * height);
}
};
PBM read_pbm(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) throw std::runtime_error("Error: Could not open file '" + filename + "'.");
std::string magic;
file >> magic;
if (magic != "P1") throw std::runtime_error("Error: Not a PBM file (P1 format)");
std::string line;
while (file.peek() == '#') std::getline(file, line);
size_t width, height;
file >> width >> height;
PBM pbm(width, height);
for (size_t y = 0; y < height; ++y)
for (size_t x = 0; x < width; ++x)
file >> pbm.bits[y * width + x];
file.close();
return pbm;
}