-
Notifications
You must be signed in to change notification settings - Fork 3
/
image.cc
141 lines (121 loc) · 3.31 KB
/
image.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// image.cc
#include "image.h"
#include <iostream>
#include <cstdlib>
using namespace std;
Image::Image()
{}
Image::Image(int width, int height)
{
nx=width;
ny=height;
// alloc mem 4 raster
raster = new rgb*[nx];
for (int i = 0; i < nx; i++)
raster[i] = new rgb[ny];
}
Image::Image(int width, int height, rgb background)
{
nx=width;
ny=height;
// alloc mem 4 raster
raster = new rgb*[nx];
for (int i = 0; i < nx; i++)
{
raster[i] = new rgb[ny];
for (int j = 0; j < ny; j++)
raster[i][j] = background;
}
}
bool Image::set(int x, int y, const rgb& color)
{
// check out of bounds err
// if (0 > x || x > nx) return false;
// if (0 > y || x > ny) return false;
raster[x][y] = color;
return true;
}
void Image::gammaCorrect(float gamma)
{
rgb tmp;
float power = 1.0 / gamma;
for (int i = 0; i < nx; i++)
for (int j = 0; j < ny; j++)
{
tmp = raster[i][j];
raster[i][j] = rgb(pow(tmp.r(), power),
pow(tmp.g(), power),
pow(tmp.b(), power));
}
}
void Image::writePPM(ostream& out)
{
// PPM header
out << "P6\n";
out << nx << ' ' << ny << '\n';
out << "255\n";
int i, j;
unsigned int ired, igreen, iblue;
unsigned char red, green, blue;
// output clamped values, range [0...255]
// PPM start in the upper left of the picture
// A raster of Height rows, in order from top to bottom.
// Each row consists of Width pixels, in order from left to right.
// the outer(first loop has to be the height
// so the inner(second) loop has to be the width
for (i = ny-1; i >= 0; i--)
for (j = 0; j < nx; j++)
{
raster[j][i].clamp();
ired = (unsigned int) (256*raster[j][i].r());
igreen = (unsigned int) (256*raster[j][i].g());
iblue = (unsigned int) (256*raster[j][i].b());
if ( ired > 255) ired = 255;
if ( igreen > 255) igreen = 255;
if ( iblue > 255) iblue = 255;
red = (unsigned char) (ired);
green = (unsigned char) (igreen);
blue = (unsigned char) (iblue);
out.put(red);
out.put(green);
out.put(blue);
}
}
// read in a binary PPM
void Image::readPPM(string file_name)
{
// open file
ifstream in;
in.open(file_name.c_str());
if (!in.is_open())
{
cerr << " Error:\t couldn't open file\'" << file_name << "\'.\n";
exit(-1);
}
char ch, type;
char red, green, blue;
int i, j, cols, rows, num;
// read header
in.get(ch);
in.get(type);
in >> cols >> rows >> num;
nx = cols;
ny = rows;
// alloc raster
raster = new rgb*[nx]; // array of pointers;
for (i=0; i < nx; i++)
raster[i] = new rgb[ny];
// consume newline
in.get(ch);
// write pixel-values to raster
for (i = ny-1; i >= 0; i--)
for (j = 0; j < nx; j++)
{
in.get(red);
in.get(green);
in.get(blue);
raster[j][i] = rgb((float)((unsigned char)red)/255.0,
(float)((unsigned char)green)/255.0,
(float)((unsigned char)blue)/255.0);
}
}