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