-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataGrid.cpp
58 lines (44 loc) · 1.4 KB
/
DataGrid.cpp
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
//
// Created by marti on 06-Sep-21.
//
#include "DataGrid.h"
#include "fstream"
#include "iostream"
#include "Helpers.h"
#include "Point.h"
DataGrid::DataGrid(unsigned int in_x_dim, unsigned in_y_dim) {
x_dim = in_x_dim;
y_dim = in_y_dim;
image_grid = new Color *[x_dim];
for (int i = 0; i < x_dim; i++) {
image_grid[i] = new Color[y_dim];
}
}
unsigned int DataGrid::getXDim() {
return x_dim;
}
unsigned int DataGrid::getYDim() {
return y_dim;
}
Color DataGrid::get(unsigned int x, unsigned int y) {
return image_grid[x][y];
}
Color* DataGrid::getRef(unsigned int x, unsigned int y){
return &image_grid[x][y];
}
void DataGrid::set(unsigned int x, unsigned int y, Color input) {
image_grid[x][y] = input;
}
void DataGrid::save_image(std::string folder) {
std::string filename = folder + "/image_" + get_time_string() + ".ppm";
std::ofstream ofs(filename, std::ios_base::out | std::ios_base::binary);
ofs << "P6" << std::endl << x_dim << ' ' << y_dim << std::endl << 255 << std::endl;
for (auto j = 0u; j < y_dim; ++j){
for (auto i = 0u; i < x_dim; ++i){
Color val = image_grid[x_dim - i - 1][j]; //Note: this line here fixes the output, so it matches view correctly
ofs << (char) val.r << (char) val.g << (char) val.b;
}
}
ofs.close();
std::cout << "Image saved to " << filename << std::endl;
}