Uh oh!
There was an error while loading. Please reload this page.
forked from zxing-cpp/zxing-cpp
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitMatrix.h
More file actions
Latest commit
326 lines (284 loc) · 8.82 KB
/
Copy pathBitMatrix.h
File metadata and controls
326 lines (284 loc) · 8.82 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#pragma once
/*
* Copyright 2016 Nu-book Inc.
* Copyright 2016 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include"Matrix.h"
#include"Point.h"
#include"ZXConfig.h"
#include<algorithm>
#include<cstdint>
#include<utility>
#include<vector>
namespaceZXing {
classBitArray;
classByteMatrix;
/**
* <p>Represents a 2D matrix of bits. In function arguments below, and throughout the common
* module, x is the column position, and y is the row position. The ordering is always x, y.
* The origin is at the top-left.</p>
*
* <p>Internally the bits are represented in a 1-D array of 32-bit ints. However, each row begins
* with a new int. This is done intentionally so that we can copy out a row into a BitArray very
* efficiently.</p>
*
* <p>The ordering of bits is row-major. Within each int, the least significant bits are used first,
* meaning they represent lower x values. This is compatible with BitArray's implementation.</p>
*
* @author Sean Owen
* @author dswitkin@google.com (Daniel Switkin)
*/
classBitMatrix
{
int _width = 0;
int _height = 0;
int _rowSize = 0;
#ifdef ZX_FAST_BIT_STORAGE
usingdata_t = uint8_t;
//TODO: c++17 inline
staticconstexprdata_tSET_V = 0xff; // allows playing with SIMD binarization
staticconstexprdata_tUNSET_V = 0;
#else
usingdata_t = uint32_t;
#endif
std::vector<data_t> _bits;
// There is nothing wrong to support this but disable to make it explicit since we may copy something very big here.
// Use copy() below.
BitMatrix(const BitMatrix&) = default;
BitMatrix& operator=(const BitMatrix&) = delete;
constdata_t& get(int i) const {
#if1
return _bits.at(i);
#else
return _bits[i];
#endif
}
data_t& get(int i) { returnconst_cast<data_t&>(static_cast<const BitMatrix*>(this)->get(i)); }
public:
BitMatrix() = default;
#ifdef ZX_FAST_BIT_STORAGE
staticboolisSet(data_t v) { return v != 0; } // see SET_V above
BitMatrix(int width, int height) : _width(width), _height(height), _rowSize(width), _bits(width * height, UNSET_V) {}
#else
BitMatrix(int width, int height) : _width(width), _height(height), _rowSize((width + 31) / 32), _bits(((width + 31) / 32) * _height, 0) {}
#endif
explicitBitMatrix(int dimension) : BitMatrix(dimension, dimension) {} // Construct a square matrix.
BitMatrix(BitMatrix&& other) noexcept : _width(other._width), _height(other._height), _rowSize(other._rowSize), _bits(std::move(other._bits)) {}
BitMatrix& operator=(BitMatrix&& other) noexcept {
_width = other._width;
_height = other._height;
_rowSize = other._rowSize;
_bits = std::move(other._bits);
return *this;
}
BitMatrix copy() const {
return *this;
}
#ifdef ZX_FAST_BIT_STORAGE
// experimental iterator based access
template<typename iterator>
structRow
{
iterator _begin, _end;
iterator begin() noexcept { return _begin; }
iterator end() noexcept { return _end; }
};
Row<data_t*> row(int y) { return {_bits.data() + y * _width, _bits.data() + (y + 1) * _width}; }
Row<constdata_t*> row(int y) const { return {_bits.data() + y * _width, _bits.data() + (y + 1) * _width}; }
#endif
/**
* <p>Gets the requested bit, where true means black.</p>
*
* @param x The horizontal component (i.e. which column)
* @param y The vertical component (i.e. which row)
* @return value of given bit in matrix
*/
boolget(int x, int y) const {
#ifdef ZX_FAST_BIT_STORAGE
returnisSet(get(y * _width + x));
#else
return ((get(y * _rowSize + (x / 32)) >> (x & 0x1f)) & 1) != 0;
#endif
}
/**
* <p>Sets the given bit to true.</p>
*
* @param x The horizontal component (i.e. which column)
* @param y The vertical component (i.e. which row)
*/
voidset(int x, int y) {
#ifdef ZX_FAST_BIT_STORAGE
get(y * _width + x) = SET_V;
#else
get(y * _rowSize + (x / 32)) |= 1 << (x & 0x1f);
#endif
}
voidunset(int x, int y) {
#ifdef ZX_FAST_BIT_STORAGE
get(y * _width + x) = UNSET_V;
#else
get(y * _rowSize + (x / 32)) &= ~(1 << (x & 0x1f));
#endif
}
voidset(int x, int y, bool val) {
#ifdef ZX_FAST_BIT_STORAGE
get(y * _width + x) = val ? SET_V : UNSET_V;
#else
val ? set(x, y) : unset(x, y);
#endif
}
/**
* <p>Flips the given bit.</p>
*
* @param x The horizontal component (i.e. which column)
* @param y The vertical component (i.e. which row)
*/
voidflip(int x, int y) {
#ifdef ZX_FAST_BIT_STORAGE
auto& v =get(y * _width + x);
v = !v;
#else
get(y * _rowSize + (x / 32)) ^= 1 << (x & 0x1f);
#endif
}
voidflipAll() {
for (auto& i : _bits) {
i = ~i;
}
}
/**
* Clears all bits (sets to false).
*/
voidclear() {
std::fill(_bits.begin(), _bits.end(), 0);
}
/**
* <p>Sets a square region of the bit matrix to true.</p>
*
* @param left The horizontal position to begin at (inclusive)
* @param top The vertical position to begin at (inclusive)
* @param width The width of the region
* @param height The height of the region
*/
voidsetRegion(int left, int top, int width, int height);
/**
* A fast method to retrieve one row of data from the matrix as a BitArray.
*
* @param y The row to retrieve
* @param row An optional caller-allocated BitArray, will be allocated if null or too small
* @return The resulting BitArray - this reference should always be used even when passing
* your own row
*/
voidgetRow(int y, BitArray& row) const;
/**
* Modifies this {@code BitMatrix} to represent the same but rotated 90 degrees clockwise
*/
voidrotate90();
/**
* Modifies this {@code BitMatrix} to represent the same but rotated 180 degrees
*/
voidrotate180();
voidmirror();
/**
* Find the rectangle that contains all non-white pixels. Useful for detection of 'pure' barcodes.
*
* @return True iff this rectangle is at least minWidth x minHeight pixels big
*/
boolfindBoundingBox(int &left, int& top, int& width, int& height, int minSize = 1) const;
/**
* This is useful in detecting a corner of a 'pure' barcode.
*
* @return {@code x,y} coordinate of top-left-most 1 bit, or null if it is all white
*/
boolgetTopLeftOnBit(int &left, int& top) const;
boolgetBottomRightOnBit(int &right, int& bottom) const;
#ifdef ZX_FAST_BIT_STORAGE
voidgetPatternRow(int r, std::vector<uint16_t>& p_row) const;
#endif
/**
* @return The width of the matrix
*/
intwidth() const {
return _width;
}
/**
* @return The height of the matrix
*/
intheight() const {
return _height;
}
/**
* @return The row size of the matrix. That is the number of 32-bits blocks that one row takes.
*/
introwSize() const {
return _rowSize;
}
boolempty() const {
return _bits.empty();
}
friendbooloperator==(const BitMatrix& a, const BitMatrix& b)
{
return a._width == b._width && a._height == b._height && a._rowSize == b._rowSize && a._bits == b._bits;
}
template <typename T>
boolisIn(PointT<T> p, int b = 0) constnoexcept
{
return b <= p.x && p.x < width() - b && b <= p.y && p.y < height() - b;
}
boolget(PointI p) const { returnget(p.x, p.y); }
boolget(PointF p) const { returnget(PointI(p)); }
voidset(PointI p, bool v = true) { set(p.x, p.y, v); }
voidset(PointF p, bool v = true) { set(PointI(p), v); }
};
/**
* @brief Inflate scales a BitMatrix up and adds a quiet Zone plus padding
* @param matrix input to be expanded
* @param width new width in bits (pixel)
* @param height new height in bits (pixel)
* @param quietZone size of quiet zone to add in modules
* @return expanded BitMatrix, maybe move(input) if size did not change
*/
BitMatrix Inflate(BitMatrix&& input, int width, int height, int quietZone);
/**
* @brief Deflate (crop + subsample) a bit matrix
* @param matrix
* @param width new width
* @param height new height
* @param top cropping starts at top row
* @param left cropping starts at left col
* @param subSampling typically the module size
* @return deflated input
*/
BitMatrix Deflate(const BitMatrix& matrix, int width, int height, float top, float left, float subSampling);
template<typename T>
BitMatrix ToBitMatrix(const Matrix<T>& in, T trueValue = {true})
{
BitMatrix out(in.width(), in.height());
for (int y = 0; y < in.height(); ++y)
for (int x = 0; x < in.width(); ++x)
if (in.get(x, y) == trueValue)
out.set(x, y);
return out;
}
template<typename T>
Matrix<T> ToMatrix(const BitMatrix& in, T black = 0, T white = ~0)
{
Matrix<T> res(in.width(), in.height());
for (int y = 0; y < in.height(); ++y)
for (int x = 0; x < in.width(); ++x)
res.set(x, y, in.get(x, y) ? black : white);
return res;
}
} // ZXing