-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.cpp
46 lines (38 loc) · 912 Bytes
/
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
#include "Image.h"
Image::Image(int width, int height)
: width(width), height(height)
{
data = new Color* [height];
for (int y = 0; y < height; ++y)
{
data[y] = new Color [width];
}
}
//
// Set the value of the pixel at the given column and row
//
void Image::setPixelValue(int col, int row, const Color& color)
{
data[col][row] = color;
}
/* Takes the image name as a file and saves it as a ppm file. */
void Image::saveImage(const char *imageName) const
{
FILE *output;
output = fopen(imageName, "w");
fprintf(output, "P3\n");
fprintf(output, "%d %d\n", width, height);
fprintf(output, "255\n");
for(int y = 0 ; y < height; y++)
{
for(int x = 0 ; x < width; x++)
{
for (int c = 0; c < 3; ++c)
{
fprintf(output, "%d ", data[y][x].channel[c]);
}
}
fprintf(output, "\n");
}
fclose(output);
}