Commit ff4c659d authored by kraus's avatar kraus
Browse files

binary reader was still not working flawlessly; now fixed

parent 24678321
......@@ -2,6 +2,7 @@
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
PortableBitmapReader::PortableBitmapReader(const std::string &input) {
......@@ -65,6 +66,9 @@ void PortableBitmapReader::readHeader(std::istream &in) {
conv.str(tmp);
conv >> height_m;
}
char tmp;
in.read(&tmp, 1);
}
void PortableBitmapReader::readImageAscii(std::istream &in) {
......@@ -81,29 +85,43 @@ void PortableBitmapReader::readImageAscii(std::istream &in) {
pixels_m[i] = (c == '1');
++ i;
}
}
}
void PortableBitmapReader::readImageBinary(std::istream &in) {
static const unsigned int sizeChar = sizeof(char) * 8;
unsigned int numBytes = width_m / sizeChar;
if (numBytes * sizeChar != width_m)
++ numBytes;
unsigned int numPixels = 0;
unsigned char c = 0;
for (unsigned int row = 0; row < height_m; ++ row) {
for (unsigned int col = 0; col < width_m; ++ col) {
if ( col % sizeChar == 0) {
char c2 = 0;
in.read(&c2, 1);
c = (unsigned char) c2;
}
unsigned int k = sizeChar - 1 - (col % sizeChar);
pixels_m[numPixels] = (c >> k & 1);
++ numPixels;
}
}
}
void PortableBitmapReader::print(std::ostream &out) const {
for (unsigned int i = 0; i < height_m; ++ i) {
for (unsigned int j = 0; j < numBytes; ++ j) {
unsigned char c;
in >> c;
for (unsigned int j = sizeChar; j > 0 ; -- j) {
pixels_m[numPixels ++] = ((c >> (j - 1)) & 1);
if (numPixels % width_m == 0) {
break;
for (unsigned int j = 0; j < width_m; ++ j) {
if (j % 8 == 0) {
unsigned int byte = 0;
for (unsigned int k = 0; k < 8 && j + k < width_m; ++ k) {
unsigned int idx = getIdx(i, j + k);
if (pixels_m[idx])
byte = byte | (1 << (7 - k));
}
std::cout << " " << std::hex << std::setw(2) << std::setfill('0') << byte << ": ";
}
unsigned int idx = getIdx(i, j);
std::cout << pixels_m[idx];
}
std::cout << std::endl;
}
}
\ No newline at end of file
......@@ -15,6 +15,7 @@ public:
unsigned int getHeight() const;
bool isBlack(unsigned int i, unsigned int j) const;
void print(std::ostream &out) const;
private:
void readHeader(std::istream &in);
......@@ -34,8 +35,6 @@ private:
FileType type_m;
// static unsigned int sizeChar_s;
std::vector<bool> pixels_m;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment