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