-
Notifications
You must be signed in to change notification settings - Fork 0
/
Film.cpp
executable file
·70 lines (47 loc) · 1.62 KB
/
Film.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
#include "Film.h"
#include "FreeImage.h"
#include <iostream>
using namespace std;
/* Constructors */
Film::Film(unsigned int imageWidth, unsigned int imageHeight) {
sampleGrid = new vector<rgb>*[imageWidth];
for(unsigned int i = 0; i < imageWidth; i++)
sampleGrid[i] = new vector<rgb>[imageHeight];
pixelWidth = imageWidth;
pixelHeight = imageHeight;
}
/* Destructor */
Film::~Film() {
for (unsigned int i = 0; i < pixelWidth; i++)
delete [] sampleGrid[i];
delete [] sampleGrid;
}
/* Instance methods */
void Film::commit(const Sample& samp, const rgb& color) {
unsigned int i = (unsigned int)samp.horiz;
unsigned int j = (unsigned int)samp.vert;
sampleGrid[i][j].push_back(color);
}
void Film::writeImage(string filename) {
filename += ".png";
// [START] WRITE IMAGE
cout << "Writing to file \"" << filename << "\"...";
FIBITMAP* image = FreeImage_Allocate(pixelWidth, pixelHeight, 24);
RGBQUAD pixel;
for (unsigned int i = 0; i < pixelWidth; i++)
for (unsigned int j = 0; j < pixelHeight; j++) {
vector<rgb> colorSamples = sampleGrid[i][j];
rgb pixelColor(0,0,0);
for (unsigned int k = 0; k < colorSamples.size(); k++)
pixelColor += colorSamples[k];
pixelColor /= colorSamples.size();
pixelColor.normalize();
pixel.rgbRed = (BYTE)(pixelColor[0] * 255);
pixel.rgbGreen = (BYTE)(pixelColor[1] * 255);
pixel.rgbBlue = (BYTE)(pixelColor[2] * 255);
FreeImage_SetPixelColor(image, i, j, &pixel);
}
FreeImage_Save(FIF_PNG, image, filename.c_str(), PNG_IGNOREGAMMA);
// [END] WRITE IMAGE
cout << "DONE" << endl;
}