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
use crate::{pdf417::pdf_417_common, ResultPoint};
use super::{
BarcodeMetadata, BarcodeValue, Codeword, DetectionRXingResultColumn,
DetectionRXingResultColumnTrait,
};
pub trait DetectionRXingResultRowIndicatorColumn: DetectionRXingResultColumnTrait {
fn adjustCompleteIndicatorColumnRowNumbers(&mut self, barcodeMetadata: &BarcodeMetadata);
fn getRowHeights(&mut self) -> Option<Vec<u32>>;
fn getBarcodeMetadata(&mut self) -> Option<BarcodeMetadata>;
fn isLeft(&self) -> bool;
}
impl DetectionRXingResultRowIndicatorColumn for DetectionRXingResultColumn {
fn adjustCompleteIndicatorColumnRowNumbers(&mut self, barcodeMetadata: &BarcodeMetadata) {
setRowNumbers(self.getCodewordsMut());
let isLeft = self.isLeft.unwrap();
removeIncorrectCodewords(self.getCodewordsMut(), barcodeMetadata, isLeft);
let boundingBox = self.getBoundingBox();
let top = if self.isLeft() {
boundingBox.getTopLeft()
} else {
boundingBox.getTopRight()
};
let bottom = if self.isLeft() {
boundingBox.getBottomLeft()
} else {
boundingBox.getBottomRight()
};
let firstRow = self.imageRowToCodewordIndex(top.getY() as u32);
let lastRow = self.imageRowToCodewordIndex(bottom.getY() as u32);
let mut barcodeRow = -1;
let mut maxRowHeight = 1;
let mut currentRowHeight = 0;
for codewordsRow in firstRow..lastRow {
if let Some(codeword) = self.getCodewordsMut()[codewordsRow] {
let rowDifference = codeword.getRowNumber() - barcodeRow;
if rowDifference == 0 {
currentRowHeight += 1;
} else if rowDifference == 1 {
maxRowHeight = maxRowHeight.max(currentRowHeight);
currentRowHeight = 1;
barcodeRow = codeword.getRowNumber();
} else if rowDifference < 0
|| codeword.getRowNumber() >= barcodeMetadata.getRowCount() as i32
|| rowDifference > codewordsRow as i32
{
self.getCodewordsMut()[codewordsRow] = None;
} else {
let checkedRows = if maxRowHeight > 2 {
(maxRowHeight - 2) * rowDifference
} else {
rowDifference
};
let mut closePreviousCodewordFound = checkedRows >= codewordsRow as i32;
let mut i = 1;
while i <= checkedRows && !closePreviousCodewordFound {
closePreviousCodewordFound =
self.getCodewords()[codewordsRow - i as usize].is_some();
i += 1;
}
if closePreviousCodewordFound {
self.getCodewordsMut()[codewordsRow] = None;
} else {
barcodeRow = codeword.getRowNumber();
currentRowHeight = 1;
}
}
} else {
continue;
}
}
}
fn getRowHeights(&mut self) -> Option<Vec<u32>> {
if let Some(barcodeMetadata) = self.getBarcodeMetadata() {
adjustIncompleteIndicatorColumnRowNumbers(self, &barcodeMetadata);
let mut result = vec![0; barcodeMetadata.getRowCount() as usize];
for codeword_opt in self.getCodewords() {
if let Some(codeword) = codeword_opt {
let rowNumber = codeword.getRowNumber();
if rowNumber as usize >= result.len() {
continue;
}
result[rowNumber as usize] += 1;
}
else {
continue;
}
}
Some(result)
} else {
None
}
}
fn getBarcodeMetadata(&mut self) -> Option<BarcodeMetadata> {
let isLeft = self.isLeft.unwrap();
let codewords = self.getCodewordsMut();
let mut barcodeColumnCount = BarcodeValue::new();
let mut barcodeRowCountUpperPart = BarcodeValue::new();
let mut barcodeRowCountLowerPart = BarcodeValue::new();
let mut barcodeECLevel = BarcodeValue::new();
for codeword_opt in codewords.iter_mut() {
if let Some(codeword) = codeword_opt {
codeword.setRowNumberAsRowIndicatorColumn();
let rowIndicatorValue = codeword.getValue() % 30;
let mut codewordRowNumber = codeword.getRowNumber();
if !isLeft {
codewordRowNumber += 2;
}
match codewordRowNumber % 3 {
0 => barcodeRowCountUpperPart.setValue(rowIndicatorValue * 3 + 1),
1 => {
barcodeECLevel.setValue(rowIndicatorValue / 3);
barcodeRowCountLowerPart.setValue(rowIndicatorValue % 3);
}
2 => barcodeColumnCount.setValue(rowIndicatorValue + 1),
_ => {}
}
} else {
continue;
}
}
if barcodeColumnCount.getValue().is_empty()
|| barcodeRowCountUpperPart.getValue().is_empty()
|| barcodeRowCountLowerPart.getValue().is_empty()
|| barcodeECLevel.getValue().is_empty()
|| barcodeColumnCount.getValue()[0] < 1
|| barcodeRowCountUpperPart.getValue()[0] + barcodeRowCountLowerPart.getValue()[0]
< pdf_417_common::MIN_ROWS_IN_BARCODE
|| barcodeRowCountUpperPart.getValue()[0] + barcodeRowCountLowerPart.getValue()[0]
> pdf_417_common::MAX_ROWS_IN_BARCODE
{
return None;
}
let barcodeMetadata = BarcodeMetadata::new(
barcodeColumnCount.getValue()[0],
barcodeRowCountUpperPart.getValue()[0],
barcodeRowCountLowerPart.getValue()[0],
barcodeECLevel.getValue()[0],
);
removeIncorrectCodewords(codewords, &barcodeMetadata, isLeft);
Some(barcodeMetadata)
}
fn isLeft(&self) -> bool {
self.isLeft.unwrap()
}
}
fn setRowNumbers(code_words: &mut [Option<Codeword>]) {
for codeword in code_words.iter_mut().flatten() {
codeword.setRowNumberAsRowIndicatorColumn();
}
}
fn removeIncorrectCodewords(
codewords: &mut [Option<Codeword>],
barcodeMetadata: &BarcodeMetadata,
isLeft: bool,
) {
for codeword_row in codewords.iter_mut() {
if let Some(codeword) = codeword_row {
let rowIndicatorValue = codeword.getValue() % 30;
let mut codewordRowNumber = codeword.getRowNumber();
if codewordRowNumber > barcodeMetadata.getRowCount() as i32 {
*codeword_row = None;
continue;
}
if !isLeft {
codewordRowNumber += 2;
}
match codewordRowNumber % 3 {
0 => {
if rowIndicatorValue * 3 + 1 != barcodeMetadata.getRowCountUpperPart() {
*codeword_row = None;
}
}
1 => {
if rowIndicatorValue / 3 != barcodeMetadata.getErrorCorrectionLevel()
|| rowIndicatorValue % 3 != barcodeMetadata.getRowCountLowerPart()
{
*codeword_row = None;
}
}
2 => {
if rowIndicatorValue + 1 != barcodeMetadata.getColumnCount() {
*codeword_row = None;
}
}
_ => {}
}
} else {
continue;
}
}
}
fn adjustIncompleteIndicatorColumnRowNumbers(
col: &mut DetectionRXingResultColumn,
barcodeMetadata: &BarcodeMetadata,
) {
let boundingBox = col.getBoundingBox();
let top = if col.isLeft() {
boundingBox.getTopLeft()
} else {
boundingBox.getTopRight()
};
let bottom = if col.isLeft() {
boundingBox.getBottomLeft()
} else {
boundingBox.getBottomRight()
};
let firstRow = col.imageRowToCodewordIndex(top.getY() as u32);
let lastRow = col.imageRowToCodewordIndex(bottom.getY() as u32);
let codewords = col.getCodewordsMut();
let mut barcodeRow = -1;
let mut maxRowHeight = 1;
let mut currentRowHeight = 0;
for codword_opt in codewords.iter_mut().take(lastRow).skip(firstRow) {
if let Some(codeword) = codword_opt {
codeword.setRowNumberAsRowIndicatorColumn();
let rowDifference = codeword.getRowNumber() - barcodeRow;
if rowDifference == 0 {
currentRowHeight += 1;
} else if rowDifference == 1 {
maxRowHeight = maxRowHeight.max(currentRowHeight);
currentRowHeight = 1;
barcodeRow = codeword.getRowNumber();
} else if codeword.getRowNumber() >= barcodeMetadata.getRowCount() as i32 {
*codword_opt = None;
} else {
barcodeRow = codeword.getRowNumber();
currentRowHeight = 1;
}
} else {
continue;
}
}
}