-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
229 lines (197 loc) · 4.92 KB
/
Matrix.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
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
222
223
224
225
226
227
228
229
//
// Matrix.cpp
// Convolve
#include <assert.h>
#include <iostream>
#include "Matrix.h"
using namespace Dog3d;
using namespace std;
Matrix::~Matrix()
{
if (m_data)
{
delete [] m_data;
m_data = NULL;
m_width = 0;
m_height = 0;
}
}
void Matrix::init(int width, int height)
{
m_width = width;
m_height = height;
m_data = new uchar[width * height];
assert(m_data);
}
void Matrix::identity() {
// cannot set identity in non square matrix
assert(m_width == m_height);
zero();
for( int i = 0; i < m_height; i++)
{
m_data[i * m_height + i] = 1;
}
}
void Matrix::greyStreak()
{
// cannot set identity in non square matrix
assert(m_width == m_height);
zero();
for( int i = 0; i < m_height; i++)
{
m_data[i * m_height + i] = 100;
}
}
void Matrix::random()
{
uchar *data = m_data;
uchar *end = data + m_height * m_width;
while(data != end)
{
*data++ = rand();
}
}
// prevent underflow
// overflow is not an issue
static inline uchar clampDifference(unsigned int lower, unsigned int upper)
{
// make sure we have space for uchar computation
// don't underflow
int newValue = upper - lower;
if (newValue < 0)
{
return 0;
}
else
{
return newValue;
}
}
void Matrix::computeDx( Matrix &m, uchar &dxMin, uchar &dxMax ) const
{
dxMin = 255;
dxMax = 0;
for(int i = 0; i < m_height; i++)
{
const uchar *source_row = getRow(i);
uchar *dest_row = m.getRow(i);
// first column
{
uchar value = clampDifference(*(source_row), *(source_row+1));
if (value > dxMax)
{
dxMax = value;
}
if (value < dxMin)
{
dxMin = value;
}
*dest_row++ = value;
source_row++;
}
// everything in between
for(int j = 1; j < m_width - 1; j++)
{
uchar value = clampDifference(*(source_row-1), *(source_row+1));
if (value > dxMax)
{
dxMax = value;
}
if (value < dxMin)
{
dxMin = value;
}
*dest_row++ = value;
source_row++;
}
// last column
{
uchar value = clampDifference(*(source_row-1), *source_row);
if (value > dxMax)
{
dxMax = value;
}
if (value < dxMin)
{
dxMin = value;
}
*dest_row++ = value;
}
}
}
void Matrix::computeDy( Matrix &m, uchar &dyMin, uchar &dyMax ) const
{
dyMin = 255;
dyMax = 0;
// compute top row's dY using column's 0,1
{
const uchar *source_row_lower = getRow(0);
const uchar *source_row_upper = getRow(1);
uchar *dest_row = m.getRow(0);
for(int j = 0; j < m_width; j++)
{
uchar value = clampDifference(*(source_row_lower++), *(source_row_upper++));
if (dyMin > value)
{
dyMin = value;
}
if (dyMax < value)
{
dyMax = value;
}
*dest_row++ = value;
}
}
// compute middle rows
for(int i = 1; i < m_height-1; i++)
{
const uchar *source_row_lower = getRow(i-1);
const uchar *source_row_upper = getRow(i+1);
uchar *dest_row = m.getRow(i);
for(int j = 0; j < m_width; j++)
{
uchar value = clampDifference(*(source_row_lower++), *(source_row_upper++));
if (dyMin > value)
{
dyMin = value;
}
if (dyMax < value)
{
dyMax = value;
}
*dest_row++ = value;
}
}
// compute bottom row's dy using columns n-2, n-1
{
const uchar *source_row_lower = getRow(m.height()-2);
const uchar *source_row_upper = getRow(m.height()-1);
uchar *dest_row = m.getRow(m.height()-1);
for(int j = 0; j < m_width; j++)
{
uchar value = clampDifference(*(source_row_lower++), *(source_row_upper++));
if (dyMin > value)
{
dyMin = value;
}
if (dyMax < value)
{
dyMax = value;
}
*dest_row++ = value;
}
}
}
ostream &operator<<(ostream &stream, const Matrix &m)
{
for(int i = 0; i < m.height(); i++)
{
const uchar *row = m.getRow(i);
for(int j = 0; j < m.width(); j++)
{
stream << ((int) *row++) << " ";
}
stream << std::endl;
}
return stream;
}