-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
buffer.h
221 lines (195 loc) · 4.91 KB
/
buffer.h
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* Author 2011-2012 Eugene Minov <minov.eug@gmail.com>
*/
using namespace std;
#include <cstring>
#include <math.h>
#include <sstream>
#include <string>
#include <iomanip>
/**
* Class to represent a image in form of 2D array
* @Spec - type o the each element of the array
*/
template<class Spec>
class Buffer
{
Spec **data; // Image's pixel data
uint32_t w; // Width of the image
uint32_t h; // Height of the image
public:
/**
* Constructor from the object of the same type
* @value - object of the same class to init from
*/
Buffer<Spec>(const Buffer<Spec> &value)
{
w = value.w;
h = value.h;
//data = (Spec **)malloc(sizeof(Spec *) * h);
data = (Spec **)new Spec *[h];
for(uint32_t i=0; i<h; i++)
{
//data[i] = (Spec *)malloc(sizeof(Spec) * w);
data[i] = (Spec *)new Spec[w];
memcpy(data[i], value.data[i], sizeof(Spec) * w);
}
}
/**
* Constructor - initializator
* @w - width of the image
* @h - height of the image
*/
Buffer<Spec>(uint32_t w = 0, uint32_t h = 0)
{
this->w = w;
this->h = h;
if(!w || !h)
{
data = NULL;
return;
}
//data = (Spec **)malloc(sizeof(Spec *) * h);
data = (Spec **)new Spec *[h];
for(uint32_t i=0; i<h; i++)
//data[i] = (Spec *)malloc(sizeof(Spec) * w);
data[i] = (Spec *)new Spec[w];
}
/**
* Resize current image (all data will be destroyed)
* @w - width of the image
* @h - height of the image
*/
void resize(uint32_t w, uint32_t h)
{
clear();
this->w = w;
this->h = h;
if(!w || !h)
{
data = NULL;
return;
}
//data = (Spec **)malloc(sizeof(Spec *) * h);
data = (Spec **)new Spec *[h];
for(uint32_t i=0; i<h; i++)
//data[i] = (Spec *)malloc(sizeof(Spec) * w);
data[i] = (Spec *)new Spec[w];
}
/**
* Delete all pixel data and reinit image
*/
void clear()
{
if(!data)
return;
for(uint32_t i=0; i<h; i++)
delete[] data[i];
delete[] data;
data = NULL;
this->w = 0;
this->h = 0;
}
/**
* Destructor
*/
~Buffer()
{
clear();
}
public:
/**
* Operator to access pixel on given coordinates in image space
* @x - X coordinate
* @y - Y coordinate
* @return - pixel data of specified type
*/
__inline Spec &operator ()(uint32_t x, uint32_t y)
{
return data[y][x];
}
/**
* Assign from the object of the same type
* @value - object of the same class to init from
* @return - reference to self
*/
const Buffer<Spec> &operator =(const Buffer<Spec> &value)
{
this->~Buffer();
w = value.w;
h = value.h;
//data = (Spec **)malloc(sizeof(Spec *) * h);
data = (Spec **)new Spec *[h];
for(uint32_t i=0; i<h; i++)
{
//data[i] = (Spec *)malloc(sizeof(Spec) * w);
data[i] = (Spec *)new Spec[w];
memcpy(data[i], value.data[i], sizeof(Spec) * w);
}
return *this;
}
public:
/**
* Check for non void object
* @return - true if there's at least one pixel stored or false otherwise
*/
__inline operator bool () const { return data; }
public:
/**
* Get image's width
* @return - width of the image
*/
__inline uint32_t getW() const { return w; }
/**
* Get image's height
* @return - height of the image
*/
__inline uint32_t getH() const { return h; }
};
/**
* Simple structure to store red, green, blue and alpha (transparency)
* components of the color
*/
struct BufferRGBA
{
uint32_t rgba;
BufferRGBA() {};
BufferRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255) {
operator[](0) = r; operator[](1) = g; operator[](2) = b; operator[](3) = a;
};
void contrast(float factor) {
for(int i=0; i<3; ++i) {
uint8_t &c = operator[](i);
float f =(float(c) - 127) * factor;
c = uint8_t(std::min(255.0f, std::max(0.0f, f + 127)));
}
}
float diff(BufferRGBA &v) {
float ret = 0;
for(int i=0; i<3; ++i) {
ret += fabsf((float)operator[](i) - (float)v[i]);
}
return ret;
}
string toWebColor() {
ostringstream osstream;
osstream << setfill('0') << hex
<< setw(2) << (uint32_t) operator[](0)
<< setw(2) << (uint32_t) operator[](1)
<< setw(2) << (uint32_t) operator[](2)
<< setw(2) << (uint32_t) operator[](3);
return osstream.str();
}
/**
* Component access by index operator
* @index - component index (0 - r; 1 - g; 2 - b; 3 - a)
* @return reference to 8-bit value represented desired component
*/
__inline uint8_t &operator[](uint32_t index) { return ((uint8_t *)&rgba)[index]; }
bool operator<(const BufferRGBA& rhs) const {
return rgba < rhs.rgba;
}
};
#define IndexedBuffer Buffer<uint8_t>
#define ColoredBuffer Buffer<BufferRGBA>
#define FloatBuffer Buffer<float>