-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaskInfo.cpp
296 lines (287 loc) · 8.2 KB
/
MaskInfo.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
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
#include "MaskInfo.h"
MaskInfo::MaskInfo(qrInfo* inf) : info(inf){}
void MaskInfo::fillData(std::vector<bool>& dataFinal, QR_Field& qr_field)
{
std::cout << "Writing QR Code ...\n";
genFormatInfo();
if (info->version + 1 >= 7){
genVersionInfo();
}
for (int i = 0; i < info->size; i++){
qr_field.push_back(std::vector<QR_Module>(info->size, QR_Module()));
}
bool up = true;
int h = info->size - 1, w = info->size - 1;
auto bit = dataFinal.begin();
for (int i = 0; i < (info->size - 1) / 2; i++){
h = up ? info->size - 1 : 0;
for (int j = 0; j < info->size; j++){
if (!qr_field[w][h].isLocked()){
qr_field[w][h].write(*(bit++), M_DATA);
}
if (!qr_field[w - 1][h].isLocked()){
qr_field[w - 1][h].write(*(bit++), M_DATA);
}
h = up ? h - 1 : h + 1;
}
up = !up;
w = w == 8 ? w - 3 : w - 2;
}
if (info->version + 1 >= 7){ // Insert version information string
int p = 17;
for (int j = 0; j < 6; j++){
for (int i = 0; i < 3; i++, p--){
qr_field[info->size - 11 + i][j].unlock();
qr_field[info->size - 11 + i][j].write_lock(versionInfo[p], M_FORMAT);
qr_field[j][info->size - 11 + i].unlock();
qr_field[j][info->size - 11 + i].write_lock(versionInfo[p], M_FORMAT);
}
}
}
dataFinal.clear();
}
void MaskInfo::applyMask(QR_Field& qr_field, int m)
{
// Masking patterns (see standard)
const std::vector<std::function<bool(int, int)>> masking_algorithms = {
[](int i, int j) { return (i + j) % 2 == 0; },
[](int i, [[maybe_unused]]int j) {return i % 2 == 0; },
[]([[maybe_unused]]int i, int j) {return j % 3 == 0; },
[](int i, int j) { return (i + j) % 3 == 0; },
[](int i, int j) { return static_cast<int>(floor(i / 2) + floor(j / 3)) % 2 == 0; },
[](int i, int j) { return (i*j) % 2 + (i*j) % 3 == 0; },
[](int i, int j) { return ((i*j) % 2 + (i*j) % 3) % 2 == 0; },
[](int i, int j) { return ((i + j) % 2 + (i*j) % 3) % 2 == 0; }
};
auto mask_alg = masking_algorithms[m];
for (int i = 0; i < info->size; i++){
for (int j = 0; j < info->size; j++){
qr_field[j][i].write(qr_field[j][i].state() != mask_alg(i, j), M_DATA);
}
}
// Insert format information
int p = formatInfo[m].size() - 1;
for (int i = 0; i < 9; i++){
if (qr_field[8][i].type == M_FORMAT){
qr_field[8][i].unlock();
qr_field[8][i].write_lock(formatInfo[m][p--], M_FORMAT);
}
}
for (int i = 1; i < 9; i++){
if (qr_field[8 - i][8].type == M_FORMAT){
qr_field[8 - i][8].unlock();
qr_field[8 - i][8].write_lock(formatInfo[m][p--], M_FORMAT);
}
}
p = formatInfo[m].size() - 1;
for (int i = 0; i < 8; i++){
if (qr_field[info->size - 1 - i][8].type == M_FORMAT){
qr_field[info->size - 1 - i][8].unlock();
qr_field[info->size - 1 - i][8].write_lock(formatInfo[m][p--], M_FORMAT);
}
}
for (int i = 0; i < 8; i++){
if (qr_field[8][info->size - 8 + i].type == M_FORMAT){
qr_field[8][info->size - 8 + i].unlock();
qr_field[8][info->size - 8 + i].write_lock(formatInfo[m][p--], M_FORMAT);
}
}
}
void MaskInfo::maskEvaluate(QR_Field& qr_field)
{
bool readcolor;
int count = 0;
std::cout << "Applying masks ...\n";
std::vector<int> penalty(8, 0);
for (int M = 0; M < 8; M++){
applyMask(qr_field,M);
// I. Look for subsequent, same coloured pixels in columns, then rows
for (int r = 0; r < 2; r++){
readcolor = qr_field[0][0].state();
for (int i = 0; i < info->size; i++){
for (int j = 0, count = 0; j < info->size; j++){
if ((r == 0 ? qr_field[i][j] : qr_field[j][i]).state() == readcolor){
count++;
if (count == 5) penalty[M] += 3;
else if (count > 5) penalty[M]++;
}
else{
count = 1;
readcolor = readcolor ? false : true;
}
}
}
}
// II. Look for (2x2) blocks of same colour
for (int i = 0; i < info->size - 1; i++){
for (int j = 0; j < info->size - 1; j++){
readcolor = qr_field[i][j].state();
if ( (readcolor == qr_field[i + 1][j].state())
&& (readcolor == qr_field[i][j + 1].state())
&& (readcolor == qr_field[i + 1][j + 1].state())){
penalty[M] += 3;
}
}
}
// III. Look for 10111010000 and 00001011101 patterns => add penalty
constexpr unsigned short pattern1 = 0b10111010000U,
pattern2 = 0b00001011101U;
unsigned short ptr1 = 0x1 << 10, ptr2 = 0x1 << 10;
for (int r = 0; r < 2; r++){
for (int i = 0; i < info->size; i++){
for (int j = 0; j < info->size; j++){
readcolor = (r == 0 ? qr_field[i][j] : qr_field[j][i]).state();
if (readcolor == static_cast<bool>(ptr1 & pattern1)){
ptr1 >>= 1;
if (ptr1 == 0x0){
//printf("A[%d,%d] ",i,j);
penalty[M] += 40;
ptr1 = 0x1 << 10;
}
}
else ptr1 = 0x1 << 10;
if (readcolor == static_cast<bool>(ptr2 & pattern2)){
ptr2 >>= 1;
if (ptr2 == 0x0){
penalty[M] += 40;
ptr2 = 0x1 << 10;
}
}
else{
ptr2 = 0x1 << 10;
}
}
ptr1 = ptr2 = 0x1 << 10;
}
}
// IV. Evaluate White to Black ratio
count = 0;
float m1, m2;
for (int i = 0; i < info->size; i++){
for (int j = 0; j < info->size; j++){
if (qr_field[i][j].state()) count++;
}
}
float black_percent = static_cast<float>(count) / static_cast<float>(info->size*info->size) * 10.0;
if (black_percent - floor(black_percent) >= 0.5){
m1 = ceil(black_percent);
m2 = m1 - 0.5;
m1 *= 10;
m2 *= 10;
}
else{
m1 = floor(black_percent);
m2 = m1 + 0.5;
m1 *= 10;
m2 *= 10;
}
penalty[M] += std::fmin(abs(50.f-m1)/5.f,abs(50.f-m2)) * 10.f;
applyMask(qr_field,M);
}
//std::cout << "Penalties: \n";
//for (int p = 0; p < 8; p++)
// std::cout << p << ' ' << penalty[p] << '\n';
// Apply best Mask
int best = penalty[0], ind = 0;
for (int i = 1; i < 7; i++){
if (penalty[i] < best){
best = penalty[i];
ind = i;
}
}
std::cout << "Mask pattern " << ind << '\n';
applyMask(qr_field,ind);
}
void MaskInfo::genVersionInfo()
{
std::cout << "Generating version strings ...\n";
// 8 Strings for 8 masking patterns
const unsigned int GEN = 0x3E4A0; // 111110010010100000
std::vector<bool> gen, temp, data;
pushBits(GEN, gen, 18);
temp.clear();
data.clear();
pushBits(info->version + 1, temp, 6);
data = temp;
pushBits(0x0, temp, 12);
while (!temp[0]){
temp.erase(temp.begin());
}
while (temp.size() != 12){
// XOR
for (int k = 0; k < temp.size(); k++){
temp[k] = (temp[k] != gen[k]);
}
// Pad
while (!temp[0] && temp.size() > 12){
temp.erase(temp.begin());
}
}
// Assembly
for (auto d : data) versionInfo.push_back(d);
for (auto e : temp) versionInfo.push_back(e);
DEBUG(
for (auto f : versionInfo){
std::cout << (f ? 1 : 0);
}
std::cout << '\n';)
}
void MaskInfo::genFormatInfo()
{
std::cout << "Generating format strings ...\n";
// 8 Strings for 8 masking patterns
const unsigned short XOR = 0x5412; // 101010000010010
const unsigned short GEN = 0x5370; // 101001101110000
std::vector<bool> gen, temp, data;
pushBits(GEN, gen, 15);
if (info->version < 0) return;
Byte ec = 0x0;
switch (info->error_level){ // (ordering required by standard)
case _L_: ec = 0x1; break;
case _M_: ec = 0x0; break;
case _Q_: ec = 0x3; break;
case _H_: ec = 0x2; break;
case _UNDEF_: throw std::string("Error: EC-Level undefined\n"); break;
}
for (unsigned short i = 0; i < 8; i++){
temp.clear();
data.clear();
formatInfo.push_back(std::vector<bool>());
pushBits(ec, temp, 2);
pushBits(i, temp, 3);
// Is data empty ? Can't perform division
if (!std::any_of(temp.begin(), temp.end(), [](bool k){return k; })){
temp.clear();
pushBits(GEN^XOR, temp, 15);
for (auto e : temp) formatInfo[i].push_back(e);
continue;
}
data = temp;
pushBits(0x0, temp, 10);
while (!temp[0]){
temp.erase(temp.begin());
}
while (temp.size() != 10){
// XOR
for (int k = 0; k < temp.size(); k++){
temp[k] = (temp[k] != gen[k]);
}
// Pad
while (!temp[0] && temp.size() > 10){
temp.erase(temp.begin());
}
}
// Assemble and apply XOR mask
for (auto d : data) formatInfo[i].push_back(d);
for (auto e : temp) formatInfo[i].push_back(e);
for (int k = 0; k < formatInfo[i].size(); k++){
formatInfo[i][k] = (formatInfo[i][k] != static_cast<bool>((0x1 << (14 - k)) & XOR));
}
}
DEBUG(for (auto& F : formatInfo){
for (auto f : F){
std::cout << (f ? 1 : 0);
}
std::cout << '\n';
})
}