/
ppm.cc
35 lines (28 loc) · 953 Bytes
/
ppm.cc
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
#include <fstream>
#include "ppm.h"
static const char* kMagic = "P3";
static const unsigned char kMaxVal = 255;
namespace raytracer {
// http://netpbm.sourceforge.net/doc/ppm.html
bool PpmFile::WriteToFile(const std::string& filename) const {
if (height_ == 0 || width_ == 0)
return false;
std::ofstream fout(filename + ".ppm");
if (!fout.is_open())
return false;
fout << kMagic << " " << width_ << " " << height_ << " "
<< static_cast<unsigned int>(kMaxVal) << "\n";
for (int i = 0; i < height_; ++i) {
for (int j = 0; j < width_; ++j) {
DColor c = GetPixel(i, j).GetDColor();
if (fout.good())
fout << static_cast<unsigned int>(c.r) << " "
<< static_cast<unsigned int>(c.g) << " "
<< static_cast<unsigned int>(c.b) << " ";
}
if (fout.good())
fout << "\n";
}
return fout.good();
}
} // namespace raytracer