not finished yet, but better than before
[MicroTrace.git] / Image.cxx
1 #include <cassert>
2 #include <fstream>
3 #include <iostream>
4
5 #include "Image.hxx"
6
7 Image::Image(int resX, int resY)
8 :
9 m_resX(resX),
10 m_resY(resY)
11 {
12 assert (resX > 0 && resY > 0);
13 std::cerr << "(Image): ResX = " << m_resX << ", resY = " << m_resY << std::endl;
14 m_pixel = new Vec3f[m_resX*m_resY];
15 }
16
17 Image::~Image()
18 {
19 delete [] m_pixel;
20 }
21
22 Image::Image()
23 {
24 }
25
26 Image::Image(const Image& o)
27 {
28 operator=(o);
29 }
30
31 Image&
32 Image::operator=(const Image& o)
33 {
34 return *this;
35 }
36
37 Vec3f&
38 Image::operator()(int x, int y)
39 {
40 assert(x >= 0 && x < m_resX);
41 assert(y >= 0 && y < m_resY);
42
43 return m_pixel[y*m_resX+x];
44 }
45
46 void Image::WritePPM(const std::string& fileName)
47 {
48 std::cerr << "(Image): Writing to file " << fileName << std::endl;
49 std::ofstream file(fileName.c_str());
50
51 if(!file.is_open())
52 {
53 std::cerr << "(Image): Could not open file " << fileName << std::endl;
54 return;
55 }
56
57 file << "P3" << std::endl;
58 file << m_resX << " " << m_resY << " " << 255 << std::endl;
59 for (int y=m_resY-1;y>=0;y--)
60 {
61 for (int x=0;x<m_resX;x++)
62 {
63 if ((*this)(x,y)[0] < 0) (*this)(x,y)[0] = 0;
64 if ((*this)(x,y)[1] < 0) (*this)(x,y)[1] = 0;
65 if ((*this)(x,y)[2] < 0) (*this)(x,y)[2] = 0;
66 if ((*this)(x,y)[0] > 255) (*this)(x,y)[0] = 255;
67 if ((*this)(x,y)[1] > 255) (*this)(x,y)[1] = 255;
68 if ((*this)(x,y)[2] > 255) (*this)(x,y)[2] = 255;
69 file
70 << (int)(255.99999999 * (*this)(x,y)[0]) << " "
71 << (int)(255.99999999 * (*this)(x,y)[1]) << " "
72 << (int)(255.99999999 * (*this)(x,y)[2]) << " "
73 << "\t";
74 }
75 file << std::endl;
76 file << std::flush;
77 }
78 file.close();
79 }
This page took 0.046724 seconds and 5 git commands to generate.