1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#include <algorithm>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
using pxArray = std::vector<float>;
template <class T>
class Matrix {
private:
size_t height {};
size_t width {};
T** values {};
public:
Matrix() = default;
Matrix(size_t h, size_t w) : height(h), width(w), values(new T* [height]) {
for (size_t ht {}; ht < h; ++ht)
values[ht] = new T[width] {};
}
Matrix(const Matrix& old) : Matrix(old.height, old.width) {
for (size_t ht {}; ht < height; ++ht)
std::copy_n(old.values[ht], width, values[ht]);
}
Matrix(Matrix&& old) noexcept {
swap(old);
}
Matrix& operator=(Matrix old) noexcept {
swap(old);
return *this;
}
size_t getHeight() const {
return height;
}
size_t getWidth() const {
return width;
}
~Matrix() {
clear();
}
void swap(Matrix& old) noexcept {
std::swap(height, old.height);
std::swap(width, old.width);
std::swap(values, old.values);
}
void clear() {
for (size_t i {}; i < height; ++i)
delete[] values[i];
delete[] values;
height = width = 0;
values = nullptr;
}
T* operator[](size_t index) {
return values[index];
}
const T* operator[](size_t index) const {
return values[index];
}
};
struct Pixel {
float r {}, g {}, b {};
};
class Netpbm {
protected:
char magicNumber {};
Matrix<Pixel> pixels;
public:
virtual ~Netpbm() {}
char getMagicNumber() const {
return magicNumber;
}
size_t getWidth() const {
return pixels.getWidth();
}
size_t getHeight() const {
return pixels.getHeight();
}
pxArray toArray() const;
virtual bool read_file(const std::string& file_name) = 0;
//virtual void write_file(const std::string& file_name) = 0;
};
class Bitmap : public Netpbm {
public:
Bitmap(const std::string& file_name) {
readOK = read_file(file_name);
}
// OR use throw with exception handling
bool openOK() const {
return readOK;
}
//void write_file(const std::string& file_name) override;
private:
bool readOK {};
bool read_file(const std::string& file_name) override;
};
pxArray Netpbm::toArray() const {
pxArray result(pixels.getWidth() * pixels.getHeight() * 5);
for (size_t i {}, count {}; i < pixels.getHeight(); ++i)
for (size_t j {}; j < pixels.getWidth(); ++j) {
// NOTE THIS IS INTEGER DIVISION!!!!
const auto x { float(j) / pixels.getWidth()}, y {float(i) / pixels.getHeight()};
result[count++] = - 1 + (2 * x);
result[count++] = 1 - (2 * y);
result[count++] = pixels[i][j].r;
result[count++] = pixels[i][j].g;
result[count++] = pixels[i][j].b;
}
return result;
}
bool Bitmap::read_file(const std::string& file_name) {
std::ifstream file(file_name);
if (!file)
return false;
size_t h {}, w {};
for (std::string line_one; std::getline(file, line_one); )
if (line_one.size() >= 2 && line_one[0] != '#') {
magicNumber = line_one[1];
break;
}
for (std::string line_two; std::getline(file, line_two); )
if (!line_two.empty() && line_two[0] != '#') {
std::stringstream ss(line_two);
ss >> w >> h;
pixels = Matrix<Pixel>(h, w);
break;
}
// Make sure we're read some values!
if (magicNumber == char {} || h == 0 || w == 0)
return false;
if (magicNumber == '1' || magicNumber == '4') {
std::vector<Pixel> v;
if (magicNumber == '1') {
for (std::string line_pixels; std::getline(file, line_pixels); )
if (!line_pixels.empty() && line_pixels[0] != '#') {
std::stringstream ss(line_pixels);
for (float p; ss >> p; v.emplace_back(p, p, p));
}
} else
for (unsigned char data {}; file.read(reinterpret_cast<char*>(&data), 1); )
for (int i {}; i < 8; ++i)
if (data & (1 << i))
v.emplace_back(1.f, 1.f, 1.f);
else
v.emplace_back(0.f, 0.f, 0.f);
if (v.size() < h * w)
return false;
for (size_t i {}, counter {}; i < h; ++i)
for (size_t j {}; j < w; ++j)
pixels[i][j] = v[counter++];
}
return true;
}
|