899ac6f21e651bffb14849822705c34e6027994e
[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 Vec3f cur = (*this)(x,y);
64 if(cur[0] < 0) cur[0] = 0;
65 if(cur[1] < 0) cur[1] = 0;
66 if(cur[2] < 0) cur[2] = 0;
67 if(cur[0] > 255) cur[0] = 255;
68 if(cur[1] > 255) cur[1] = 255;
69 if(cur[2] > 255) cur[2] = 255;
70
71 file
72 << (int)(255.99999999 * cur[0]) << " "
73 << (int)(255.99999999 * cur[1]) << " "
74 << (int)(255.99999999 * cur[2]) << " "
75 << "\t";
76 }
77 file << std::endl;
78 file << std::flush;
79 }
80 file.close();
81 }
This page took 0.044875 seconds and 3 git commands to generate.