OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
PortableGraymapReader.cpp
Go to the documentation of this file.
2
3#include <iostream>
4#include <fstream>
5#include <iomanip>
6#include <sstream>
7#include <cstdint>
8
10
11 std::ifstream in(input);
12 readHeader(in);
13 pixels_m.resize(width_m * height_m);
14
15 if (type_m == ASCII) {
17 } else {
19 }
20}
21
22std::string PortableGraymapReader::getNextPart(std::istream &in) {
23 do {
24 char c = in.get();
25 if (c == '#') {
26 do {
27 c = in.get();
28 } while (c != '\n');
29 } else if (!(c == ' ' ||
30 c == '\t' ||
31 c == '\n' ||
32 c == '\r')) {
33 in.putback(c);
34 break;
35 }
36 } while (true);
37
38 std::string nextPart;
39 in >> nextPart;
40
41 return nextPart;
42}
43
44void PortableGraymapReader::readHeader(std::istream &in) {
45 std::string magicValue = getNextPart(in);
46
47 if (magicValue == "P2") {
48 type_m = ASCII;
49 } else if (magicValue == "P5") {
50 type_m = BINARY;
51 } else {
52 throw OpalException("PortableGraymapReader::readHeader",
53 "Unknown magic value: '" + magicValue + "'");
54 }
55
56 {
57 std::string tmp = getNextPart(in);
58 std::istringstream conv;
59 conv.str(tmp);
60 conv >> width_m;
61 }
62
63 {
64 std::string tmp = getNextPart(in);
65 std::istringstream conv;
66 conv.str(tmp);
67 conv >> height_m;
68 }
69
70 {
71 std::string tmp = getNextPart(in);
72 std::istringstream conv;
73 conv.str(tmp);
74 conv >> depth_m;
75 }
76
77 char tmp;
78 in.read(&tmp, 1);
79}
80
82 unsigned int size = height_m * width_m;
83 unsigned int i = 0;
84 while (i < size) {
85 unsigned int c;
86 in >> c;
87
88 pixels_m[i] = c;
89 ++ i;
90 }
91}
92
94
95 unsigned int numPixels = 0;
96 char c[] = {0, 0};
97 unsigned char uc;
98 for (unsigned int row = 0; row < height_m; ++ row) {
99 for (unsigned int col = 0; col < width_m; ++ col) {
100 if ( depth_m > 255) {
101 in.read(&c[1], 1);
102 in.read(&c[0], 1);
103 const uint16_t *val = reinterpret_cast<const uint16_t*>(&c[0]);
104 pixels_m[numPixels] = val[0];
105 } else {
106 in.read(&c[0], 1);
107 uc = c[0];
108 pixels_m[numPixels] = uc;
109 }
110 ++ numPixels;
111 }
112 }
113}
114
115void PortableGraymapReader::print(std::ostream &/*out*/) const {
116 const unsigned int printWidth = 5;
117 for (unsigned int i = 0; i < height_m; ++ i) {
118 for (unsigned int j = 0; j < width_m; ++ j) {
119 unsigned int idx = getIdx(i, j);
120 std::cout << " " << std::setw(printWidth) << pixels_m[idx];
121 }
122 std::cout << std::endl;
123 }
124}
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:45
std::vector< unsigned short > pixels_m
std::string getNextPart(std::istream &in)
void print(std::ostream &out) const
void readImageBinary(std::istream &in)
unsigned int getIdx(unsigned int h, unsigned int w) const
void readHeader(std::istream &in)
void readImageAscii(std::istream &in)
PortableGraymapReader(const std::string &input)
The base class for all OPAL exceptions.
Definition: OpalException.h:28