forked from nishantb15/Ray-Tracer-Part-2-BVH-and-Mesh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
imageIO.h
92 lines (76 loc) · 1.55 KB
/
imageIO.h
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
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct Pixel {
unsigned char R, G, B; // Blue, Green, Red
};
class ColorImage {
Pixel *pPixel;
int xRes, yRes;
public:
ColorImage();
~ColorImage();
void init(int xSize, int ySize);
void clear(Pixel background);
Pixel readPixel(int x, int y);
void writePixel(int x, int y, Pixel p);
void outputPPM(char *filename);
};
ColorImage::ColorImage()
{
pPixel = 0;
}
ColorImage::~ColorImage()
{
if (pPixel) delete[] pPixel;
pPixel = 0;
}
void ColorImage::init(int xSize, int ySize)
{
Pixel p = {0,0,0};
xRes = xSize;
yRes = ySize;
pPixel = new Pixel[xSize*ySize];
clear(p);
}
void ColorImage::clear(Pixel background)
{
int i;
if (! pPixel) return;
for (i=0; i<xRes*yRes; i++) pPixel[i] = background;
}
Pixel ColorImage::readPixel(int x, int y)
{
assert(pPixel); // die if image not initialized
return pPixel[x + y*yRes];
}
void ColorImage::writePixel(int x, int y, Pixel p)
{
assert(pPixel); // die if image not initialized
pPixel[x + y*xRes] = p;
}
void ColorImage::outputPPM(char *filename)
{
FILE *outFile = fopen(filename, "wb");
assert(outFile); // die if file can't be opened
fprintf(outFile, "P6 %d %d 255\n", xRes, yRes);
fwrite(pPixel, 1, 3*xRes*yRes, outFile );
fclose(outFile);
}
/*
// A test program that generates varying shades of reds.
int main(int argc, char* argv[])
{
ColorImage image;
int x, y;
Pixel p={0,0,0};
image.init(256, 256);
for (y=0; y<256; y++) {
for (x=0; x<256; x++) {
p.R = y;
image.writePixel(x, y, p);
}
}
image.outputPPM("reds.ppm");
}
*/