-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.cpp
More file actions
62 lines (51 loc) · 1.57 KB
/
Image.cpp
File metadata and controls
62 lines (51 loc) · 1.57 KB
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
#include "Image.hpp"
// http://netpbm.sourceforge.net/doc/ppm.html
void Image::writeAsPPM(std::string filename) {
// Skalierung und Maximalwert je Farbkanal
double scale = 255.0;
int maxPossibleValue = (int)scale + 1;
// Headerinformationen in Datei schreiben
std::ofstream of(filename);
of << "P3\n"
<< "# result\n"
<< mWidth << " " << mHeight << "\n"
<< maxPossibleValue << "\n";
for (size_t ii = 0; ii < mValues.size(); ++ii) {
int i = mWidth * mHeight - ii - 1;
of << (int)(scale * mValues[i].r) << " " << (int)(scale * mValues[i].g)
<< " " << (int)(scale * mValues[i].b) << "\n";
}
of.flush();
}
Image::Image(size_t width, size_t height) {
mWidth = width;
mHeight = height;
mValues = std::vector<Color>(width * height);
std::fill(mValues.begin(), mValues.end(), mBackgroundColor);
}
Image::Image(int sizeIdentifier) {
if (sizeIdentifier == SIZE_HUGE) {
Image(1600, 1200);
} else if (sizeIdentifier == SIZE_MEDIUM) {
Image(640, 480);
} else {
Image(320, 240);
}
}
std::vector<Color> &Image::getValues() { return mValues; }
Color Image::getValue(int x, int y) {
if (x < mWidth && y < mHeight) {
return mValues[y * mWidth + x];
}
}
/**
** Setzt den Pixelwert an der Stelle (x,y) auf die Farbe color
** macht nichts wenn (x,y) außerhalb der Dimension des Bildes liegt
**/
void Image::setValue(int x, int y, Color color) {
if (x < mWidth && y < mHeight) {
mValues[y * mWidth + x] = color;
}
}
size_t Image::getWidth() const { return mWidth; }
size_t Image::getHeight() const { return mHeight; }