-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImage.cpp
57 lines (47 loc) · 1.1 KB
/
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
47
48
49
50
51
52
53
54
55
56
/*
* File: Image.cpp
* Author: caos
*
* Created on June 19, 2015, 12:28 AM
*/
#include "Image.h"
Image::Image(int _w, int _h) : w(_w), h(_h)
{
srand(time(NULL));
pixels = new Color[w*h];
}
void Image::set(int x, int y, Color c)
{
if(x >= 0 && x < w && y>=0 && y < h)
pixels[y*w + x] = c;
else
throw std::invalid_argument("[Image::set] index out of bound!");
}
Color Image::get(int x, int y)
{
if(x >= 0 && x < w && y>=0 && y < h)
return pixels[y*w + x];
else
throw std::invalid_argument("[Image::get] index out of bound!");
}
void Image::writeToFile(std::string fileName)
{
/* Write the header */
std::ofstream out(fileName.c_str());
out << "P3" << std::endl;
out << w << std::endl;
out << h << std::endl;
out << "255" << std::endl;
Color col;
for(int i=0; i < h; i++)
for(int j=0; j < w; j++)
{
col = pixels[i*w + j];
out << col.getR() << " " << col.getG() << " " << col.getB() << std::endl;
}
out.close();
}
Image::~Image()
{
delete[] pixels;
}