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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// /*
// * Copyright 2016 Nu-book Inc.
// * Copyright 2016 ZXing authors
// */
// // SPDX-License-Identifier: Apache-2.0
use crate::{
Exceptions,
common::{BitMatrix, Result},
qrcode::decoder::{ErrorCorrectionLevel, FormatInformation, Version, VersionRef},
};
use super::{Type, data_mask::GetDataMaskBit, detector::AppendBit};
pub fn getBit(bitMatrix: &BitMatrix, x: u32, y: u32, mirrored: Option<bool>) -> bool {
let mirrored = mirrored.unwrap_or(false);
if mirrored {
bitMatrix.get(y, x)
} else {
bitMatrix.get(x, y)
}
}
pub fn ReadVersion(bitMatrix: &BitMatrix, qr_type: Type) -> Result<VersionRef> {
if !Version::HasValidSize(bitMatrix) {
return Err(Exceptions::FORMAT);
}
let number = Version::Number(bitMatrix);
match qr_type {
Type::Model1 => Version::Model1(number),
Type::Micro => Version::Micro(number),
Type::Model2 => Version::Model2(number),
Type::RectMicro => Version::rMQR(number),
}
}
pub fn ReadFormatInformation(bitMatrix: &BitMatrix) -> Result<FormatInformation> {
if Version::HasValidSizeType(bitMatrix, Type::Micro) {
// Read top-left format info bits
let mut formatInfoBits = 0;
for x in 1..9 {
// for (int x = 1; x < 9; x++)
AppendBit(&mut formatInfoBits, getBit(bitMatrix, x, 8, None));
}
for y in (1..=7).rev() {
// for (int y = 7; y >= 1; y--)
AppendBit(&mut formatInfoBits, getBit(bitMatrix, 8, y, None));
}
return Ok(FormatInformation::DecodeMQR(formatInfoBits as u32));
}
if Version::HasValidSizeType(bitMatrix, Type::RectMicro) {
// Read top-left format info bits
let mut formatInfoBits1 = 0;
for y in (1..=3).rev() {
// for (int y = 3; y >= 1; y--){
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, 11, y, None));
}
for x in (8..=10).rev() {
// for (int x = 10; x >= 8; x--){
for y in (1..=5).rev() {
// for (int y = 5; y >= 1; y--){
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, x, y, None));
}
}
// Read bottom-right format info bits
let mut formatInfoBits2 = 0;
let width = bitMatrix.width();
let height = bitMatrix.height();
for x in 3..=5 {
// for (int x = 3; x <= 5; x++){
AppendBit(
&mut formatInfoBits2,
getBit(bitMatrix, width - x, height - 6, None),
);
}
for x in 6..=8 {
// for (int x = 6; x <= 8; x++){
for y in 2..=6 {
// for (int y = 2; y <= 6; y++){
AppendBit(
&mut formatInfoBits2,
getBit(bitMatrix, width - x, height - y, None),
);
}
}
return Ok(FormatInformation::DecodeRMQR(
formatInfoBits1 as u32,
formatInfoBits2 as u32,
));
}
// Read top-left format info bits
let mut formatInfoBits1 = 0;
for x in 0..6 {
// for (int x = 0; x < 6; x++)
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, x, 8, None));
}
// .. and skip a bit in the timing pattern ...
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, 7, 8, None));
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, 8, 8, None));
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, 8, 7, None));
// .. and skip a bit in the timing pattern ...
for y in (0..=5).rev() {
// for (int y = 5; y >= 0; y--)
AppendBit(&mut formatInfoBits1, getBit(bitMatrix, 8, y, None));
}
// Read the top-right/bottom-left pattern including the 'Dark Module' from the bottom-left
// part that has to be considered separately when looking for mirrored symbols.
// See also FormatInformation::DecodeQR
let dimension = bitMatrix.height();
let mut formatInfoBits2 = 0;
for y in ((dimension - 8)..=(dimension - 1)).rev() {
// for (int y = dimension - 1; y >= dimension - 8; y--)
AppendBit(&mut formatInfoBits2, getBit(bitMatrix, 8, y, None));
}
for x in (dimension - 8)..dimension {
// for (int x = dimension - 8; x < dimension; x++)
AppendBit(&mut formatInfoBits2, getBit(bitMatrix, x, 8, None));
}
Ok(FormatInformation::DecodeQR(
formatInfoBits1 as u32,
formatInfoBits2 as u32,
))
}
pub fn ReadQRCodewords(
bitMatrix: &BitMatrix,
version: VersionRef,
formatInfo: &FormatInformation,
) -> Result<Vec<u8>> {
let functionPattern: BitMatrix = version.buildFunctionPattern()?;
let mut result = Vec::with_capacity(version.getTotalCodewords() as usize);
let mut currentByte = 0;
let mut readingUp = true;
let mut bitsRead = 0;
let dimension = bitMatrix.height();
// Read columns in pairs, from right to left
let mut x = (dimension as i32) - 1;
while x > 0 {
// for (int x = dimension - 1; x > 0; x -= 2) {
// Skip whole column with vertical timing pattern.
if x == 6 {
x -= 1;
}
// Read alternatingly from bottom to top then top to bottom
for row in 0..dimension {
// for (int row = 0; row < dimension; row++) {
let y = if readingUp { dimension - 1 - row } else { row };
for col in 0..2 {
// for (int col = 0; col < 2; col++) {
let xx = (x - col) as u32;
// Ignore bits covered by the function pattern
if !functionPattern.get(xx, y) {
// Read a bit
AppendBit(
&mut currentByte,
GetDataMaskBit(formatInfo.data_mask as u32, xx, y, None)?
!= getBit(bitMatrix, xx, y, Some(formatInfo.isMirrored)),
);
// If we've made a whole byte, save it off
bitsRead += 1;
if bitsRead % 8 == 0 {
result.push(std::mem::take(&mut currentByte));
}
}
}
}
readingUp = !readingUp; // switch directions
x -= 2;
}
if (result.len()) != version.getTotalCodewords() as usize {
return Err(Exceptions::FORMAT);
}
Ok(result.iter().copied().map(|x| x as u8).collect())
}
pub fn ReadMQRCodewords(
bitMatrix: &BitMatrix,
version: VersionRef,
formatInfo: &FormatInformation,
) -> Result<Vec<u8>> {
let functionPattern = version.buildFunctionPattern()?;
// D3 in a Version M1 symbol, D11 in a Version M3-L symbol and D9
// in a Version M3-M symbol is a 2x2 square 4-module block.
// See ISO 18004:2006 6.7.3.
let hasD4mBlock = version.getVersionNumber() % 2 == 1;
let d4mBlockIndex = if version.getVersionNumber() == 1 {
3
} else if formatInfo.error_correction_level == ErrorCorrectionLevel::L {
11
} else {
9
};
let mut result = Vec::with_capacity(version.getTotalCodewords() as usize);
let mut currentByte = 0;
let mut readingUp = true;
let mut bitsRead = 0;
let dimension = bitMatrix.height();
// Read columns in pairs, from right to left
let mut x = dimension - 1;
while x > 0 {
// for (int x = dimension - 1; x > 0; x -= 2) {
// Read alternatingly from bottom to top then top to bottom
for row in 0..dimension {
// for (int row = 0; row < dimension; row++) {
let y = if readingUp { dimension - 1 - row } else { row };
for col in 0..2 {
// for (int col = 0; col < 2; col++) {
let xx = x - col;
// Ignore bits covered by the function pattern
if !functionPattern.get(xx, y) {
// Read a bit
AppendBit(
&mut currentByte,
GetDataMaskBit(formatInfo.data_mask as u32, xx, y, Some(true))?
!= getBit(bitMatrix, xx, y, Some(formatInfo.isMirrored)),
);
bitsRead += 1;
// If we've made a whole byte, save it off; save early if 2x2 data block.
if bitsRead == 8
|| (bitsRead == 4 && hasD4mBlock && (result.len()) == d4mBlockIndex - 1)
{
result.push(std::mem::take(&mut currentByte));
bitsRead = 0;
}
}
}
}
readingUp = !readingUp; // switch directions
x -= 2;
}
if (result.len()) != version.getTotalCodewords() as usize {
return Err(Exceptions::FORMAT);
}
Ok(result.iter().copied().map(|x| x as u8).collect())
}
pub fn ReadQRCodewordsModel1(
bitMatrix: &BitMatrix,
version: VersionRef,
formatInfo: &FormatInformation,
) -> Result<Vec<u8>> {
let mut result = Vec::with_capacity(version.getTotalCodewords() as usize);
let dimension = bitMatrix.height();
let columns = dimension / 4 + 1 + 2;
for j in 0..columns {
// for (int j = 0; j < columns; j++) {
if j <= 1 {
// vertical symbols on the right side
let rows = (dimension - 8) / 4;
for i in 0..rows {
// for (int i = 0; i < rows; i++) {
if j == 0 && i % 2 == 0 && i > 0 && i < rows - 1
// extension
{
continue;
}
let x = (dimension - 1) - (j * 2);
let y = (dimension - 1) - (i * 4);
let mut currentByte = 0;
for b in 0..8 {
// for (int b = 0; b < 8; b++) {
AppendBit(
&mut currentByte,
GetDataMaskBit(formatInfo.data_mask as u32, x - b % 2, y - (b / 2), None)?
!= getBit(
bitMatrix,
x - b % 2,
y - (b / 2),
Some(formatInfo.isMirrored),
),
);
}
result.push(currentByte);
}
} else if columns - j <= 4 {
// vertical symbols on the left side
let rows = (dimension - 16) / 4;
for i in 0..rows {
// for (int i = 0; i < rows; i++) {
let x = (columns - j - 1) * 2 + 1 + (if columns - j == 4 { 1 } else { 0 }); // timing
let y = (dimension - 1) - 8 - (i * 4);
let mut currentByte = 0;
for b in 0..8 {
// for (int b = 0; b < 8; b++) {
AppendBit(
&mut currentByte,
GetDataMaskBit(formatInfo.data_mask as u32, x - b % 2, y - (b / 2), None)?
!= getBit(
bitMatrix,
x - b % 2,
y - (b / 2),
Some(formatInfo.isMirrored),
),
);
}
result.push(currentByte);
}
} else {
// horizontal symbols
let rows = dimension / 2;
for i in 0..rows {
// for (int i = 0; i < rows; i++) {
if j == 2 && i >= rows - 4
// alignment & finder
{
continue;
}
if i == 0 && j % 2 == 1 && j + 1 != columns - 4
// extension
{
continue;
}
let x = (dimension - 1) - (2 * 2) - (j - 2) * 4;
let y = (dimension - 1) - (i * 2) - (if i >= rows - 3 { 1 } else { 0 }); // timing
let mut currentByte = 0;
for b in 0..8 {
// for (int b = 0; b < 8; b++) {
AppendBit(
&mut currentByte,
GetDataMaskBit(formatInfo.data_mask as u32, x - b % 4, y - (b / 4), None)?
!= getBit(
bitMatrix,
x - b % 4,
y - (b / 4),
Some(formatInfo.isMirrored),
),
);
}
result.push(currentByte);
}
}
}
result[0] &= 0xf; // ignore corner
if (result.len()) != version.getTotalCodewords() as usize {
return Err(Exceptions::FORMAT);
}
Ok(result.iter().copied().map(|x| x as u8).collect())
}
pub fn ReadRMQRCodewords(
bitMatrix: &BitMatrix,
version: VersionRef,
formatInfo: &FormatInformation,
) -> Result<Vec<u8>> {
let functionPattern = version.buildFunctionPattern()?;
let mut result = Vec::with_capacity(version.getTotalCodewords() as usize);
let mut currentByte = 0;
let mut readingUp = true;
let mut bitsRead = 0;
let width = bitMatrix.width();
let height = bitMatrix.height();
// Read columns in pairs, from right to left
let mut x = width as i32 - 1 - 1;
while x > 0 {
// for (int x = width - 1 - 1; x > 0; x -= 2) { // Skip right edge alignment
// Read alternatingly from bottom to top then top to bottom
for row in 0..height {
// for (int row = 0; row < height; row++) {
let y = if readingUp { height - 1 - row } else { row };
for col in 0..2 {
// for (int col = 0; col < 2; col++) {
let xx = x - col;
// Ignore bits covered by the function pattern
if !functionPattern.get(xx as u32, y) {
// Read a bit
AppendBit(
&mut currentByte,
GetDataMaskBit(formatInfo.data_mask as u32, xx as u32, y, None)?
!= getBit(bitMatrix, xx as u32, y, Some(formatInfo.isMirrored)),
);
// If we've made a whole byte, save it off
bitsRead += 1;
if bitsRead % 8 == 0 {
// if (++bitsRead % 8 == 0){
result.push(currentByte);
currentByte = 0;
}
}
}
}
readingUp = !readingUp; // switch directions
x -= 2
}
if (result.len()) != version.getTotalCodewords() as usize {
return Err(Exceptions::FORMAT);
}
Ok(result.iter().copied().map(|x| x as u8).collect())
}
pub fn ReadCodewords(
bitMatrix: &BitMatrix,
version: VersionRef,
formatInfo: &FormatInformation,
) -> Result<Vec<u8>> {
match version.qr_type {
Type::Model1 => ReadQRCodewordsModel1(bitMatrix, version, formatInfo),
Type::Model2 => ReadQRCodewords(bitMatrix, version, formatInfo),
Type::Micro => ReadMQRCodewords(bitMatrix, version, formatInfo),
Type::RectMicro => ReadRMQRCodewords(bitMatrix, version, formatInfo),
}
}