rxing/maxicode/decoder/
maxicode_decoder.rs

1/*
2 * Copyright 2011 ZXing authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use once_cell::sync::Lazy;
18
19use crate::{
20    common::{
21        reedsolomon::{get_predefined_genericgf, PredefinedGenericGF, ReedSolomonDecoder},
22        BitMatrix, DecoderRXingResult, Result,
23    },
24    DecodeHints, Exceptions,
25};
26
27use super::{decoded_bit_stream_parser, BitMatrixParser};
28
29/*
30 * <p>The main class which implements MaxiCode decoding -- as opposed to locating and extracting
31 * the MaxiCode from an image.</p>
32 *
33 * @author Manuel Kasten
34 */
35
36const ALL: u32 = 0;
37const EVEN: u32 = 1;
38const ODD: u32 = 2;
39
40static RS_DECODER: Lazy<ReedSolomonDecoder> = Lazy::new(|| {
41    ReedSolomonDecoder::new(get_predefined_genericgf(
42        PredefinedGenericGF::MaxicodeField64,
43    ))
44});
45
46pub fn decode(bits: &BitMatrix) -> Result<DecoderRXingResult> {
47    decode_with_hints(bits, &DecodeHints::default())
48}
49
50pub fn decode_with_hints(bits: &BitMatrix, _hints: &DecodeHints) -> Result<DecoderRXingResult> {
51    let parser = BitMatrixParser::new(bits);
52    let mut codewords = parser.readCodewords();
53
54    correctErrors(&mut codewords, 0, 10, 10, ALL)?;
55    let mode = codewords[0] & 0x0F;
56    let mut datawords;
57    match mode {
58        2..=4 => {
59            correctErrors(&mut codewords, 20, 84, 40, EVEN)?;
60            correctErrors(&mut codewords, 20, 84, 40, ODD)?;
61            datawords = vec![0u8; 94];
62        }
63        5 => {
64            correctErrors(&mut codewords, 20, 68, 56, EVEN)?;
65            correctErrors(&mut codewords, 20, 68, 56, ODD)?;
66            datawords = vec![0u8; 78];
67        }
68        _ => return Err(Exceptions::NOT_FOUND),
69    }
70
71    datawords[0..10].clone_from_slice(&codewords[0..10]);
72    // System.arraycopy(codewords, 0, datawords, 0, 10);
73    let datawords_len = datawords.len();
74    datawords[10..datawords_len].clone_from_slice(&codewords[20..datawords_len + 10]);
75    // System.arraycopy(codewords, 20, datawords, 10, datawords.length - 10);
76
77    decoded_bit_stream_parser::decode(&datawords, mode)
78}
79
80fn correctErrors(
81    codewordBytes: &mut [u8],
82    start: u32,
83    dataCodewords: u32,
84    ecCodewords: u32,
85    mode: u32,
86) -> Result<()> {
87    let codewords = dataCodewords + ecCodewords;
88
89    // in EVEN or ODD mode only half the codewords
90    let divisor = if mode == ALL { 1 } else { 2 };
91
92    // First read into an array of ints
93    let mut codewordsInts = vec![0; (codewords / divisor) as usize];
94    for i in 0..codewords {
95        if (mode == ALL) || (i % 2 == (mode - 1)) {
96            codewordsInts[(i / divisor) as usize] = codewordBytes[(i + start) as usize] as i32;
97        }
98    }
99
100    RS_DECODER.decode(&mut codewordsInts, (ecCodewords / divisor) as i32)?;
101
102    // Copy back into array of bytes -- only need to worry about the bytes that were data
103    // We don't care about errors in the error-correction codewords
104    for i in 0..dataCodewords {
105        // for (int i = 0; i < dataCodewords; i++) {
106        if (mode == ALL) || (i % 2 == (mode - 1)) {
107            codewordBytes[(i + start) as usize] = codewordsInts[(i / divisor) as usize] as u8;
108        }
109    }
110    Ok(())
111}