-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.cpp
59 lines (50 loc) · 1.28 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
58
59
#include "Image.hpp"
Image::Image(int width, int height) : width(width), height(height)
{
this->width = width;
this->height = height;
createImageCanvas();
}
void Image::createImageCanvas()
{
pixels.resize(height);
for (int i = 0; i < height; ++i)
{
pixels[i].resize(width);
}
}
int Image::toByte(double character)
{
return round(max(min(character * 255.0, 255.0), 0.0));
}
void Image::setPixel(int x, int y, Color color)
{
if(y >= 0 && y < pixels.size() && x >= 0 && x < pixels[y].size())
{
//cout << "Pixel added at (" << x << ", " << y << ")" << endl;
pixels[y][x] = color;
}
else
{
cout << "Invalid pixel coordinates (" << x << ", " << y << ")" << endl;
}
}
void Image::exportImage(const string& filename)
{
ofstream file(filename);
if(!file.is_open())
{
cerr << "Error opening file: " << filename << endl;
return;
}
file << "P3 " << width << " " << height << "\n255\n";
for (auto row : pixels)
{
for (auto color : row)
{
file << toByte(color.r) << " " << toByte(color.g) << " " << toByte(color.b) << " ";
}
file << "\n";
}
file.close();
}