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
/*
 * Copyright 2011 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use std::collections::HashMap;

use once_cell::sync::Lazy;

use crate::{
    common::{
        reedsolomon::{get_predefined_genericgf, PredefinedGenericGF, ReedSolomonDecoder},
        BitMatrix, DecoderRXingResult,
    },
    DecodingHintDictionary, Exceptions,
};

use super::{decoded_bit_stream_parser, BitMatrixParser};

/**
 * <p>The main class which implements MaxiCode decoding -- as opposed to locating and extracting
 * the MaxiCode from an image.</p>
 *
 * @author Manuel Kasten
 */

const ALL: u32 = 0;
const EVEN: u32 = 1;
const ODD: u32 = 2;

static RS_DECODER: Lazy<ReedSolomonDecoder> = Lazy::new(|| {
    ReedSolomonDecoder::new(get_predefined_genericgf(
        PredefinedGenericGF::MaxicodeField64,
    ))
});

pub fn decode(bits: BitMatrix) -> Result<DecoderRXingResult, Exceptions> {
    decode_with_hints(bits, &HashMap::new())
}

pub fn decode_with_hints(
    bits: BitMatrix,
    _hints: &DecodingHintDictionary,
) -> Result<DecoderRXingResult, Exceptions> {
    let parser = BitMatrixParser::new(bits);
    let mut codewords = parser.readCodewords();

    correctErrors(&mut codewords, 0, 10, 10, ALL)?;
    let mode = codewords[0] & 0x0F;
    let mut datawords;
    match mode {
        2 | 3 | 4 => {
            correctErrors(&mut codewords, 20, 84, 40, EVEN)?;
            correctErrors(&mut codewords, 20, 84, 40, ODD)?;
            datawords = vec![0u8; 94];
        }
        5 => {
            correctErrors(&mut codewords, 20, 68, 56, EVEN)?;
            correctErrors(&mut codewords, 20, 68, 56, ODD)?;
            datawords = vec![0u8; 78];
        }
        _ => return Err(Exceptions::NotFoundException(None)),
    }

    datawords[0..10].clone_from_slice(&codewords[0..10]);
    // System.arraycopy(codewords, 0, datawords, 0, 10);
    let datawords_len = datawords.len();
    datawords[10..datawords_len].clone_from_slice(&codewords[20..datawords_len + 10]);
    // System.arraycopy(codewords, 20, datawords, 10, datawords.length - 10);

    decoded_bit_stream_parser::decode(&datawords, mode)
}

fn correctErrors(
    codewordBytes: &mut [u8],
    start: u32,
    dataCodewords: u32,
    ecCodewords: u32,
    mode: u32,
) -> Result<(), Exceptions> {
    let codewords = dataCodewords + ecCodewords;

    // in EVEN or ODD mode only half the codewords
    let divisor = if mode == ALL { 1 } else { 2 };

    // First read into an array of ints
    let mut codewordsInts = vec![0i32; (codewords / divisor) as usize];
    for i in 0..codewords {
        // for (int i = 0; i < codewords; i++) {
        if (mode == ALL) || (i % 2 == (mode - 1)) {
            codewordsInts[(i / divisor) as usize] = codewordBytes[(i + start) as usize] as i32;
        }
    }
    // try {
    RS_DECODER.decode(&mut codewordsInts, (ecCodewords / divisor) as i32)?;
    // } catch (ReedSolomonException ignored) {
    //   throw ChecksumException.getChecksumInstance();
    // }
    // Copy back into array of bytes -- only need to worry about the bytes that were data
    // We don't care about errors in the error-correction codewords
    for i in 0..dataCodewords {
        // for (int i = 0; i < dataCodewords; i++) {
        if (mode == ALL) || (i % 2 == (mode - 1)) {
            codewordBytes[(i + start) as usize] = codewordsInts[(i / divisor) as usize] as u8;
        }
    }
    Ok(())
}