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