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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use rxing_one_d_proc_derive::OneDReader;
use crate::common::{BitArray, Result};
use crate::{point, BarcodeFormat, Exceptions, RXingResult};
use super::{one_d_reader, OneDReader};
#[derive(OneDReader)]
pub struct Code39Reader {
usingCheckDigit: bool,
extendedMode: bool,
decodeRowRXingResult: String,
}
impl Default for Code39Reader {
fn default() -> Self {
Self::with_use_check_digit(false)
}
}
impl OneDReader for Code39Reader {
fn decode_row(
&mut self,
rowNumber: u32,
row: &crate::common::BitArray,
_hints: &DecodingHintDictionary,
) -> Result<crate::RXingResult> {
let mut counters = [0_u32; 9];
self.decodeRowRXingResult.clear();
let start = Self::findAsteriskPattern(row, &mut counters)?;
let mut nextStart = row.getNextSet(start[1] as usize);
let end = row.get_size();
let mut decodedChar;
let mut lastStart;
loop {
one_d_reader::record_pattern(row, nextStart, &mut counters)?;
let pattern = Self::toNarrowWidePattern(&counters);
if pattern < 0 {
return Err(Exceptions::NOT_FOUND);
}
decodedChar = Self::patternToChar(pattern as u32)?;
self.decodeRowRXingResult.push(decodedChar);
lastStart = nextStart;
for counter in &counters {
nextStart += *counter as usize;
}
nextStart = row.getNextSet(nextStart);
if decodedChar == '*' {
break;
}
}
self.decodeRowRXingResult
.truncate(self.decodeRowRXingResult.len() - 1); let lastPatternSize = counters.iter().sum::<u32>();
let whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize as usize;
if nextStart != end && (whiteSpaceAfterEnd * 2) < lastPatternSize as usize {
return Err(Exceptions::NOT_FOUND);
}
if self.usingCheckDigit {
let max = self.decodeRowRXingResult.chars().count() - 1;
let mut total = 0;
for i in 0..max {
if let Some(pos) = Self::ALPHABET_STRING.find(
self.decodeRowRXingResult
.chars()
.nth(i)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?,
) {
total += pos;
}
}
if self
.decodeRowRXingResult
.chars()
.nth(max)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
!= Self::ALPHABET_STRING
.chars()
.nth(total % 43)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
{
return Err(Exceptions::NOT_FOUND);
}
self.decodeRowRXingResult.truncate(max);
}
if self.decodeRowRXingResult.chars().count() == 0 {
return Err(Exceptions::NOT_FOUND);
}
let resultString = if self.extendedMode {
Self::decodeExtended(&self.decodeRowRXingResult)?
} else {
self.decodeRowRXingResult.clone()
};
let left = (start[1] + start[0]) as f32 / 2.0;
let right = (lastStart + lastPatternSize as usize) as f32 / 2.0;
let mut resultObject = RXingResult::new(
&resultString,
Vec::new(),
vec![
point(left, rowNumber as f32),
point(right, rowNumber as f32),
],
BarcodeFormat::CODE_39,
);
resultObject.putMetadata(
RXingResultMetadataType::SYMBOLOGY_IDENTIFIER,
RXingResultMetadataValue::SymbologyIdentifier("]A0".to_owned()),
);
Ok(resultObject)
}
}
impl Code39Reader {
pub const ALPHABET_STRING: &str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";
pub const CHARACTER_ENCODINGS: [u32; 43] = [
0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064, 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C, 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016, 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, 0x085, 0x184, 0x0C4, 0x0A8, 0x0A2, 0x08A, 0x02A, ];
pub const ASTERISK_ENCODING: u32 = 0x094;
pub fn new() -> Self {
Self::with_use_check_digit(false)
}
pub fn with_use_check_digit(usingCheckDigit: bool) -> Self {
Self::with_all_config(usingCheckDigit, false)
}
pub fn with_all_config(usingCheckDigit: bool, extendedMode: bool) -> Self {
Self {
usingCheckDigit,
extendedMode,
decodeRowRXingResult: String::with_capacity(20),
}
}
fn findAsteriskPattern(row: &BitArray, counters: &mut [u32]) -> Result<Vec<u32>> {
let width = row.get_size();
let rowOffset = row.getNextSet(0);
let mut counterPosition = 0;
let mut patternStart = rowOffset;
let mut isWhite = false;
let patternLength = counters.len();
for i in rowOffset..width {
if row.get(i) != isWhite {
counters[counterPosition] += 1;
} else {
if counterPosition == patternLength - 1 {
if Self::toNarrowWidePattern(counters) == (Self::ASTERISK_ENCODING as i32)
&& row.isRange(
0.max(
patternStart as isize - ((i as isize - patternStart as isize) / 2),
) as usize,
patternStart,
false,
)?
{
return Ok(vec![patternStart as u32, i as u32]);
}
patternStart += (counters[0] + counters[1]) as usize;
counters.copy_within(2..(counterPosition - 1 + 2), 0);
counters[counterPosition - 1] = 0;
counters[counterPosition] = 0;
counterPosition -= 1;
} else {
counterPosition += 1;
}
counters[counterPosition] = 1;
isWhite = !isWhite;
}
}
Err(Exceptions::NOT_FOUND)
}
fn toNarrowWidePattern(counters: &[u32]) -> i32 {
let numCounters = counters.len();
let mut maxNarrowCounter = 0;
let mut wideCounters;
loop {
let mut minCounter = u32::MAX;
for counter in counters {
if counter < &minCounter && counter > &maxNarrowCounter {
minCounter = *counter;
}
}
maxNarrowCounter = minCounter;
wideCounters = 0;
let mut totalWideCountersWidth = 0;
let mut pattern = 0;
for (i, counter) in counters.iter().enumerate().take(numCounters) {
if *counter > maxNarrowCounter {
pattern |= 1 << (numCounters - 1 - i);
wideCounters += 1;
totalWideCountersWidth += *counter;
}
}
if wideCounters == 3 {
let mut i = 0;
while i < numCounters && wideCounters > 0 {
let counter = counters[i];
if counter > maxNarrowCounter {
wideCounters -= 1;
if (counter * 2) >= totalWideCountersWidth {
return -1;
}
}
i += 1;
}
return pattern;
}
if wideCounters <= 3 {
break;
}
}
-1
}
fn patternToChar(pattern: u32) -> Result<char> {
for i in 0..Self::CHARACTER_ENCODINGS.len() {
if Self::CHARACTER_ENCODINGS[i] == pattern {
return Self::ALPHABET_STRING
.chars()
.nth(i)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS);
}
}
if pattern == Self::ASTERISK_ENCODING {
return Ok('*');
}
Err(Exceptions::NOT_FOUND)
}
fn decodeExtended(encoded: &str) -> Result<String> {
let length = encoded.chars().count();
let mut decoded = String::with_capacity(length); let mut i = 0;
while i < length {
let c = encoded
.chars()
.nth(i)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
if c == '+' || c == '$' || c == '%' || c == '/' {
let next = encoded
.chars()
.nth(i + 1)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
let mut decodedChar = '\0';
match c {
'+' => {
if ('A'..='Z').contains(&next) {
decodedChar = char::from_u32(next as u32 + 32)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
} else {
return Err(Exceptions::NOT_FOUND);
}
}
'$' => {
if ('A'..='Z').contains(&next) {
decodedChar = char::from_u32(next as u32 - 64)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
} else {
return Err(Exceptions::NOT_FOUND);
}
}
'%' => {
if ('A'..='E').contains(&next) {
decodedChar = char::from_u32(next as u32 - 38)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
} else if ('F'..='J').contains(&next) {
decodedChar = char::from_u32(next as u32 - 11)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
} else if ('K'..='O').contains(&next) {
decodedChar = char::from_u32(next as u32 + 16)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
} else if ('P'..='T').contains(&next) {
decodedChar = char::from_u32(next as u32 + 43)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
} else if next == 'U' {
decodedChar = 0 as char;
} else if next == 'V' {
decodedChar = '@';
} else if next == 'W' {
decodedChar = '`';
} else if next == 'X' || next == 'Y' || next == 'Z' {
decodedChar = 127 as char;
} else {
return Err(Exceptions::NOT_FOUND);
}
}
'/' => {
if ('A'..='O').contains(&next) {
decodedChar = char::from_u32(next as u32 - 32)
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
} else if next == 'Z' {
decodedChar = ':';
} else {
return Err(Exceptions::NOT_FOUND);
}
}
_ => {}
}
decoded.push(decodedChar);
i += 1;
} else {
decoded.push(c);
}
i += 1;
}
Ok(decoded)
}
}
#[cfg(test)]
mod code_39_extended_mode_test_case {
use std::collections::HashMap;
use crate::{
common::BitMatrix,
oned::{Code39Reader, OneDReader},
};
#[test]
fn testDecodeExtendedMode() {
doTest("\u{0000}\u{0001}\u{0002}\u{0003}\u{0004}\u{0005}\u{0006}\u{0007}\u{0008}\t\n\u{000b}\u{000C}\r\u{000e}\u{000f}\u{0010}\u{0011}\u{0012}\u{0013}\u{0014}\u{0015}\u{0016}\u{0017}\u{0018}\u{0019}\u{001a}\u{001b}\u{001c}\u{001d}\u{001e}\u{001f}",
"000001001011011010101001001001011001010101101001001001010110101001011010010010010101011010010110100100100101011011010010101001001001010101011001011010010010010101101011001010100100100101010110110010101001001001010101010011011010010010010101101010011010100100100101010110100110101001001001010101011001101010010010010101101010100110100100100101010110101001101001001001010110110101001010010010010101010110100110100100100101011010110100101001001001010101101101001010010010010101010101100110100100100101011010101100101001001001010101101011001010010010010101010110110010100100100101011001010101101001001001010100110101011010010010010101100110101010100100100101010010110101101001001001010110010110101010010010010101001101101010101001001001011010100101101010010010010101101001011010100100100101101101001010101001001001010101100101101010010010010110101100101010010110110100000");
doTest(" !\"#$%&'()*+,-./0123456789:;<=>?",
"00000100101101101010011010110101001001010010110101001011010010010100101011010010110100100101001011011010010101001001010010101011001011010010010100101101011001010100100101001010110110010101001001010010101010011011010010010100101101010011010100100101001010110100110101001001010010101011001101010010010100101101010100110100100101001010110101001101001010110110110010101101010010010100101101011010010101001101101011010010101101011001010110110110010101010100110101101101001101010101100110101010100101101101101001011010101100101101010010010100101001101101010101001001001010110110010101010010010010101010011011010100100100101101010011010101001001001010110100110101010010010010101011001101010010110110100000");
doTest("@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_",
"000010010110110101010010010010100110101011011010100101101011010010110110110100101010101100101101101011001010101101100101010101001101101101010011010101101001101010101100110101101010100110101101010011011011010100101010110100110110101101001010110110100101010101100110110101011001010110101100101010110110010110010101011010011010101101100110101010100101101011011001011010101001101101010101001001001011010101001101010010010010101101010011010100100100101101101010010101001001001010101101001101010010010010110101101001010010110110100000");
doTest("`abcdefghijklmnopqrstuvwxyz{|}~",
"000001001011011010101001001001011001101010101001010010010110101001011010010100100101011010010110100101001001011011010010101001010010010101011001011010010100100101101011001010100101001001010110110010101001010010010101010011011010010100100101101010011010100101001001010110100110101001010010010101011001101010010100100101101010100110100101001001010110101001101001010010010110110101001010010100100101010110100110100101001001011010110100101001010010010101101101001010010100100101010101100110100101001001011010101100101001010010010101101011001010010100100101010110110010100101001001011001010101101001010010010100110101011010010100100101100110101010100101001001010010110101101001010010010110010110101010010100100101001101101010101001001001010110110100101010010010010101010110011010100100100101101010110010101001001001010110101100101010010010010101011011001010010110110100000");
}
fn doTest(expectedRXingResult: &str, encodedRXingResult: &str) {
let mut sut = Code39Reader::with_all_config(false, true);
let matrix =
BitMatrix::parse_strings(encodedRXingResult, "1", "0").expect("bitmatrix parse");
let row = matrix.getRow(0);
let result = sut
.decode_row(0, &row, &HashMap::new())
.expect("decode row");
assert_eq!(expectedRXingResult, result.getText());
}
}