-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.cpp
executable file
·136 lines (113 loc) · 3.53 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
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
#include "Image.hpp"
#include <iostream>
#include <cstring>
#include <lodepng/lodepng.h>
const uint Image::m_colorComponents = 3; // Red, blue, green
//---------------------------------------------------------------------------------------
Image::Image()
: m_width(0),
m_height(0),
m_data(0)
{
}
//---------------------------------------------------------------------------------------
Image::Image(
uint width,
uint height
)
: m_width(width),
m_height(height)
{
size_t numElements = m_width * m_height * m_colorComponents;
m_data = new double[numElements];
memset(m_data, 0, numElements*sizeof(double));
}
//---------------------------------------------------------------------------------------
Image::Image(const Image & other)
: m_width(other.m_width),
m_height(other.m_height),
m_data(other.m_data ? new double[m_width * m_height * m_colorComponents] : 0)
{
if (m_data) {
std::memcpy(m_data, other.m_data,
m_width * m_height * m_colorComponents * sizeof(double));
}
}
//---------------------------------------------------------------------------------------
Image::~Image()
{
delete [] m_data;
}
//---------------------------------------------------------------------------------------
Image & Image::operator=(const Image& other)
{
delete [] m_data;
m_width = other.m_width;
m_height = other.m_height;
m_data = (other.m_data ? new double[m_width * m_height * m_colorComponents] : 0);
if (m_data) {
std::memcpy(m_data,
other.m_data,
m_width * m_height * m_colorComponents * sizeof(double)
);
}
return *this;
}
//---------------------------------------------------------------------------------------
uint Image::width() const
{
return m_width;
}
//---------------------------------------------------------------------------------------
uint Image::height() const
{
return m_height;
}
//---------------------------------------------------------------------------------------
double Image::operator()(uint x, uint y, uint i) const
{
return m_data[m_colorComponents * (m_width * y + x) + i];
}
//---------------------------------------------------------------------------------------
double & Image::operator()(uint x, uint y, uint i)
{
return m_data[m_colorComponents * (m_width * y + x) + i];
}
//---------------------------------------------------------------------------------------
static double clamp(double x, double a, double b)
{
return x < a ? a : (x > b ? b : x);
}
//---------------------------------------------------------------------------------------
bool Image::savePng(const std::string & filename) const
{
std::vector<unsigned char> image;
image.resize(m_width * m_height * m_colorComponents);
double color;
for (uint y(0); y < m_height; y++) {
for (uint x(0); x < m_width; x++) {
for (uint i(0); i < m_colorComponents; ++i) {
color = m_data[m_colorComponents * (m_width * y + x) + i];
color = clamp(color, 0.0, 1.0);
image[m_colorComponents * (m_width * y + x) + i] = (unsigned char)(255 * color);
}
}
}
// Encode the image
unsigned error = lodepng::encode(filename, image, m_width, m_height, LCT_RGB);
if(error) {
std::cerr << "encoder error " << error << ": " << lodepng_error_text(error)
<< std::endl;
}
return true;
}
//---------------------------------------------------------------------------------------
const double * Image::data() const
{
return m_data;
}
//---------------------------------------------------------------------------------------
double * Image::data()
{
return m_data;
}