-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPicture.cpp
85 lines (69 loc) · 1.36 KB
/
Picture.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
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
#include <iostream>
#include <fstream>
#include "Picture.h"
#include "Pixel.h"
#include "Color.h"
Picture::Picture()
: width(0), height(0)
{
}
Picture::Picture(int width, int height)
: width(width), height(height)
{
pixels = new Pixel[width * height];
}
void Picture::addPixelColor(int w, int h, Color color)
{
pixels[width * h + w].addColor(color);
}
Color Picture::getPixelColor(int w, int h) const
{
return pixels[width * h + w].getAverageColor();
}
int Picture::getWidth() const
{
return width;
}
int Picture::getHeight() const
{
return height;
}
void Picture::printToFile(const char* fileName) const
{
std::ofstream pic;
pic.open(fileName);
pic << "P3" << std::endl << width << " " << height << " 255" << std::endl;
int size = width * height;
for (int i = 0; i < size; i++)
{
pic << pixels[i].getAverageColor();
}
pic.close();
}
void Picture::readFromFile(const char* fileName)
{
std::ifstream imgFile(fileName, std::ios::binary);
char c;
imgFile >> c;
imgFile >> c;
int widthVal, heightVal, maxSize;
imgFile >> widthVal;
imgFile >> heightVal;
imgFile >> maxSize;
imgFile.get(c);
width = widthVal;
height = heightVal;
int picSize = width * height;
pixels = new Pixel[picSize];
char val;
Color color;
for (int i = 0; i < picSize; i++)
{
imgFile >> color;
pixels[i].addColor(color);
}
}
void Picture::clearPixels()
{
delete[] pixels;
}