-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.cpp
executable file
·57 lines (50 loc) · 1.66 KB
/
Image.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
#include "Image.h"
#include <math.h>
Image::Image(int w, int h, float widthMatrix, int samples, float gammaCorrection, float blur) : width(w), height(h), widthMatrix(widthMatrix), SamplesPerPixel(samples), gammaCorrection(gammaCorrection), blur(blur)
{
dimPixel = widthMatrix / width;
matrix.resize(height);
for (int i = 0; i < height; ++i)
{
matrix[i].resize(width);
}
initializeImage();
}
int Image::printImage(std::string name)
{
std::ofstream output;
std::time_t t = std::time(0);
name = std::to_string((int)((t - 1645644194) / 100)) + name;
output.open("Output/" + name + ".ppm", std::ios::out);
if (!output.is_open())
{
std::cout << "couldn't find the file " << name << "in the Output folder" << std::endl;
return 0;
}
// header
output << "P3 \n"
<< width << " " << height << "\n255 \n";
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
double r = pow(matrix[i][j].red / SamplesPerPixel, gammaCorrection);
double g = pow(matrix[i][j].green / SamplesPerPixel, gammaCorrection);
double b = pow(matrix[i][j].blue / SamplesPerPixel, gammaCorrection);
output << static_cast<int>(256 * std::clamp(r, 0.0, 0.999)) << ' ' << static_cast<int>(256 * std::clamp(g, 0.0, 0.999)) << ' ' << static_cast<int>(256 * std::clamp(b, 0.0, 0.999)) << ' ';
}
output << "\n";
}
output.close();
return 0;
}
void Image::initializeImage()
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
matrix[i][j] = Color(0, 0, 0);
}
}
}