forked from jcelaya/hdrmerge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArray2D.hpp
168 lines (155 loc) · 5.02 KB
/
Array2D.hpp
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
/*
* HDRMerge - HDR exposure merging software.
* Copyright 2012 Javier Celaya
*
* This file is part of HDRMerge.
*
* HDRMerge is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HDRMerge is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HDRMerge. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef _ARRAY2D_HPP_
#define _ARRAY2D_HPP_
#include <memory>
#include <cstdint>
#include <algorithm>
namespace hdrmerge {
template <typename T>
class Array2D {
public:
Array2D(size_t w, size_t h) { resize(w, h); }
Array2D() : Array2D(0, 0) {}
Array2D(const Array2D<T> & copy) {
(*this) = copy;
}
template <typename Y> Array2D(const Array2D<Y> & copy) {
(*this) = copy;
}
Array2D(Array2D<T> && move) noexcept {
(*this) = std::move(move);
}
virtual ~Array2D() {}
Array2D<T> & operator=(Array2D<T> && move) noexcept {
data = std::move(move.data);
alignedData = move.alignedData;
width = move.width;
height = move.height;
dx = move.dx;
dy = move.dy;
move.resize(0, 0);
return *this;
}
Array2D<T> & operator=(const Array2D<T> & copy) {
resize(copy.getWidth(), copy.getHeight());
std::copy_n(copy.data.get(), width*height, data.get());
displace(copy.getDeltaX(), copy.getDeltaY());
return *this;
}
template <typename Y> Array2D<T> & operator=(const Array2D<Y> & copy) {
resize(copy.getWidth(), copy.getHeight());
for (size_t i = 0; i < width*height; ++i) {
data[i] = copy[i];
}
displace(copy.getDeltaX(), copy.getDeltaY());
return *this;
}
void resize(size_t w, size_t h) {
width = w;
height = h;
dx = dy = 0;
data.reset(new T[w*h]);
alignedData = data.get();
}
size_t getWidth() const {
return width;
}
size_t getHeight() const {
return height;
}
size_t size() const {
return width*height;
}
int getDeltaX() const {
return dx;
}
int getDeltaY() const {
return dy;
}
const T & operator[](size_t i) const {
return data[i];
}
T & operator[](size_t i) {
return data[i];
}
const T & operator()(size_t x, size_t y) const {
return alignedData[y*width + x];
}
T & operator()(size_t x, size_t y) {
return alignedData[y*width + x];
}
bool contains(int x, int y) const {
return x >= dx && x < (int)width + dx && y >= dy && y < (int)height + dy;
}
void displace(int newDx, int newDy) {
dx += newDx;
dy += newDy;
alignedData = &data[-dy*width - dx];
}
void fillBorders( T val ) {
if(dy > 0) {
for(size_t i = 0; i < dy; ++i)
for(size_t j = 0; j < width; ++j)
data[i*width + j] = val;
}
if(dx > 0) {
for(size_t i = 0; i < height; ++i)
for(size_t j = 0; j < dx; ++j)
data[i*width + j] = val;
}
}
typedef T * iterator;
typedef const T * const_iterator;
iterator begin() { return data.get(); }
iterator end() { return data.get() + width*height; }
const_iterator cbegin() const { return data.get(); }
const_iterator cend() const { return data.get() + width*height; }
template <typename F> void traceCircle(int x, int y, int radius, const F & function) {
int r2 = radius * radius;
int ymin = std::max(-y, -radius), ymax = std::min((int)height - y, radius + 1);
int xmin = std::max(-x, -radius), xmax = std::min((int)width - x, radius + 1);
for (int row = ymin, rrow = y + row; row < ymax; ++row, ++rrow) {
for (int col = xmin, rcol = x + col; col < xmax; ++col, ++rcol) {
if (row*row + col*col <= r2) {
function(rcol, rrow, operator()(rcol, rrow));
}
}
}
}
template <typename F> void traceSquare(int x, int y, int radius, const F & function) {
int ymin = std::max(-y, -radius), ymax = std::min((int)height - y, radius + 1);
int xmin = std::max(-x, -radius), xmax = std::min((int)width - x, radius + 1);
for (int rrow = y + ymin; rrow < y + ymax; ++rrow) {
for (int rcol = x + xmin; rcol < x + xmax; ++rcol) {
function(rcol, rrow, operator()(rcol, rrow));
}
}
}
protected:
std::unique_ptr<T[]> data;
T * alignedData;
size_t width, height;
int dx, dy;
};
} // namespace hdrmerge
#endif // _ARRAY2D_HPP_