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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include <bitset>
#include <cstring>
#include <cmath>
struct Pixel {
float r, g, b;
};
typedef struct Pixel pixel;
class Netpbm {
protected:
std::vector<std::vector<pixel>> pixels;
public:
virtual void read_file(std::string file_name) = 0;
virtual void write_ascii_file(std::string file_name) = 0;
virtual void write_binary_file(std::string file_name) = 0;
int getHeight();
int getWidth();
std::vector<float> toArray();
};
int Netpbm::getHeight() { return pixels.size(); };
int Netpbm::getWidth() { return pixels[0].size(); };
std::vector<float> Netpbm::toArray() {
std::vector<float> result;
size_t h = pixels.size(), w = pixels[0].size();
for(size_t i=0; i<h; i++) {
for(size_t j=0; j<w; j++) {
float x = j/w, y=i/h;
result.push_back(-1 + (2 * x));
result.push_back(1 - (2 * y));
result.push_back(pixels[i][j].r); //error
result.push_back(pixels[i][j].g);
result.push_back(pixels[i][j].b);
}
}
return result;
}
class Bitmap : public Netpbm {
public:
void read_file(std::string file_name);
void write_ascii_file(std::string file_name);
void write_binary_file(std::string file_name);
};
void Bitmap::read_file(std::string file_name) {
std::ifstream file(file_name);
std::string line_one, line_two, line_pixels;
char magicNumber;
std::string width, height;
while(getline(file, line_one)) {
if(line_one.size() > 0 && line_one.at(0) != '#') {
magicNumber = line_one.at(1);
break;
}
}
while(getline(file, line_two)) {
if(line_two.size() > 0 && line_two.at(0) != '#') {
std::stringstream ss(line_two);
getline(ss, width, ' ');
getline(ss, height, ' ');
break;
}
}
if(magicNumber == '1') {
std::vector<pixel> v;
while(getline(file, line_pixels)) {
if(line_pixels.size() > 0 && line_pixels.at(0) != '#') {
std::stringstream ss(line_pixels);
std::string value;
while(getline(ss, value, ' ')) {
pixel p;
p.r = p.g = p.b = stoi(value);
v.push_back(p);
}
}
}
int counter = 0;
for(int i=0; i<stoi(height); i++) {
std::vector<pixel> row;
for(int j=0; j<stoi(width); j++) {
row.push_back(v[counter++]);
}
pixels.push_back(row);
}
}
if(magicNumber == '4') {
std::vector<pixel> v;
while(!file.eof()) {
unsigned char data[1];
file.read((char*)data, 1);
for(int i=0; i<8; i++) {
pixel p;
if(data[0]&(1<<i))
p.r = p.g = p.b = 1;
else
p.r = p.g = p.b = 0;
v.push_back(p);
}
}
int counter = 0;
for(int i=0; i<stoi(height); i++) {
std::vector<pixel> row;
for(int j=0; j<stoi(width); j++) {
row.push_back(v[counter++]);
}
pixels.push_back(row);
}
}
}
void Bitmap::write_ascii_file(std::string file_name) {
//
}
void Bitmap::write_binary_file(std::string file_name) {
//
}
class Image {
private:
float * vertexList;
int width;
int height;
public:
Image(float * v, int w, int h) {
vertexList = v;
width = w;
height = h;
};
void draw() {
std::cout << "height: " << height << std::endl;
std::cout << "width: " << width << std::endl;
std::cout << "size: " << sizeof(vertexList) / sizeof(float) << std::endl;
};
};
class Renderer {
private:
Image * image;
public:
void setImage(float * vertices, int width, int height) {
image = new Image(vertices, width, height);
};
void drawFrame() {
image->draw();
};
};
class Surface {
private:
int width;
int height;
Renderer * renderer;
public:
Surface(std::string windows_title, int width, int height) {
this->width = width;
this->height = height;
this->renderer = new Renderer();
};
~Surface() {
delete renderer;
};
Renderer * getRenderer() {
return renderer;
};
void loop() {
renderer->drawFrame();
};
};
/*
int main() {
std::cout << "height: " << image.getHeight() << std::endl;
std::cout << "width: " << image.getWidth() << std::endl;
std::cout << "size: " << image.toArray().size() << std::endl;
return 0;
}
*/
int main() {
Bitmap image;
image.read_file("teste.pbm");
Surface * view = new Surface("image", image.getWidth(), image.getHeight());
view->getRenderer()->setImage(image.toArray().data(), image.getWidth(), image.getHeight());
view->loop();
delete view;
return 0;
}
| |