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
/*
 * Copyright 2013 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 crate::pdf417::pdf_417_common;

/**
 * @author Guenther Grau
 * @author creatale GmbH (christoph.schulz@creatale.de)
 */

// const RATIOS_TABLE :[[f32]] =
//   [[0.0;pdf_417_common::SYMBOL_TABLE.len()];pdf_417_common::BARS_IN_MODULE];

const RATIOS_TABLE: [[f32; pdf_417_common::BARS_IN_MODULE as usize]; 2787] = {
    let mut table =
        [[0.0; pdf_417_common::BARS_IN_MODULE as usize]; pdf_417_common::SYMBOL_TABLE.len()];
    // Pre-computes the symbol ratio table.
    let mut i = 0_usize;
    while i < pdf_417_common::SYMBOL_TABLE.len() {
        // for (int i = 0; i < PDF417Common.SYMBOL_TABLE.length; i++) {
        let mut currentSymbol = pdf_417_common::SYMBOL_TABLE[i];
        let mut currentBit = currentSymbol & 0x1;
        let mut j = 0_usize;
        while j < pdf_417_common::BARS_IN_MODULE as usize {
            // for (int j = 0; j < pdf_417_common::BARS_IN_MODULE; j++) {
            let mut size = 0.0;
            while (currentSymbol & 0x1) == currentBit {
                size += 1.0;
                currentSymbol >>= 1;
            }
            currentBit = currentSymbol & 0x1;
            table[i][pdf_417_common::BARS_IN_MODULE as usize - j - 1] =
                size / pdf_417_common::MODULES_IN_CODEWORD as f32;

            j += 1;
        }

        i += 1;
    }

    table
};

pub fn getDecodedValue(moduleBitCount: &[u32]) -> u32 {
    let decodedValue = getDecodedCodewordValue(&sampleBitCounts(moduleBitCount));
    if decodedValue != -1 {
        return decodedValue as u32;
    }
    getClosestDecodedValue(moduleBitCount) as u32
}

fn sampleBitCounts(moduleBitCount: &[u32]) -> [u32; 8] {
    let bitCountSum: u32 = moduleBitCount.iter().sum(); //MathUtils.sum(moduleBitCount);
    let mut result = [0; pdf_417_common::BARS_IN_MODULE as usize];
    let mut bitCountIndex = 0;
    let mut sumPreviousBits = 0;
    for i in 0..pdf_417_common::MODULES_IN_CODEWORD {
        // for (int i = 0; i < PDF417Common.MODULES_IN_CODEWORD; i++) {
        let sampleIndex: f32 = bitCountSum as f32
            / (2.0 * pdf_417_common::MODULES_IN_CODEWORD as f32)
            + (i as f32 * bitCountSum as f32) / pdf_417_common::MODULES_IN_CODEWORD as f32;
        if sumPreviousBits as f32 + moduleBitCount[bitCountIndex] as f32 <= sampleIndex {
            sumPreviousBits += moduleBitCount[bitCountIndex];
            bitCountIndex += 1;
        }
        result[bitCountIndex] += 1;
    }
    result
}

fn getDecodedCodewordValue(moduleBitCount: &[u32]) -> i32 {
    let decodedValue = getBitValue(moduleBitCount);
    if pdf_417_common::getCodeword(decodedValue as u32) == -1 {
        -1
    } else {
        decodedValue
    }
}

fn getBitValue(moduleBitCount: &[u32]) -> i32 {
    let mut result: u64 = 0;
    for (i, mbc) in moduleBitCount.iter().enumerate() {
        // for (int i = 0; i < moduleBitCount.length; i++) {
        for _bit in 0..(*mbc) {
            // for (int bit = 0; bit < moduleBitCount[i]; bit++) {
            result = (result << 1) | u64::from(i % 2 == 0); //(if i % 2 == 0 { 1 } else { 0 });
        }
    }
    result as i32
}

fn getClosestDecodedValue(moduleBitCount: &[u32]) -> i32 {
    let bitCountSum: u32 = moduleBitCount.iter().sum(); //MathUtils.sum(moduleBitCount);
    let mut bitCountRatios = [0.0; pdf_417_common::BARS_IN_MODULE as usize];
    if bitCountSum > 1 {
        for i in 0..bitCountRatios.len() {
            // for (int i = 0; i < bitCountRatios.length; i++) {
            bitCountRatios[i] = moduleBitCount[i] as f32 / bitCountSum as f32;
        }
    }
    let mut bestMatchError = f32::MAX;
    let mut bestMatch = -1_i32;
    for (j, ratioTableRow) in RATIOS_TABLE.iter().enumerate() {
        // for (int j = 0; j < RATIOS_TABLE.length; j++) {
        let mut error = 0.0;
        for k in 0..pdf_417_common::BARS_IN_MODULE as usize {
            // for (int k = 0; k < PDF417Common.BARS_IN_MODULE; k++) {
            let diff = ratioTableRow[k] - bitCountRatios[k];
            error += diff * diff;
            if error >= bestMatchError {
                break;
            }
        }
        if error < bestMatchError {
            bestMatchError = error;
            bestMatch = pdf_417_common::SYMBOL_TABLE[j] as i32;
        }
    }
    bestMatch
}

#[cfg(test)]
mod test {
    use crate::pdf417::decoder::pdf_417_codeword_decoder::getDecodedValue;

    #[test]
    fn test_cw_227() {
        let sample_data = [2, 2, 3, 1, 6, 4, 3, 4];
        assert_ne!(getDecodedValue(&sample_data), 110360);
        assert_eq!(getDecodedValue(&sample_data), 93980);
    }

    #[test]
    fn test_2() {
        let sample = [2, 1, 4, 2, 5, 3, 7, 2];
        assert_ne!(95134, getDecodedValue(&sample));
    }

    #[test]
    fn test_128268() {
        let sample = [7, 2, 1, 2, 2, 5, 3, 3];
        assert_eq!(128268, getDecodedValue(&sample));
    }

    #[test]
    fn test_125304() {
        let sample = [6, 1, 2, 2, 2, 2, 6, 4];
        assert_eq!(125304, getDecodedValue(&sample));
    }

    #[test]
    fn test_125216() {
        let sample = [6, 1, 2, 2, 2, 3, 2, 7];
        assert_eq!(125216, getDecodedValue(&sample));
    }

    #[test]
    fn test_83768() {
        let sample = [1, 2, 1, 4, 5, 3, 5, 4];
        assert_eq!(83768, getDecodedValue(&sample));
    }

    #[test]
    fn test_96060() {
        let sample = [2, 1, 4, 2, 5, 3, 7, 2];
        assert_eq!(96060, getDecodedValue(&sample));
    }

    #[test]
    fn test_97372() {
        let sample = [3, 1, 7, 4, 2, 2, 4, 2];
        assert_eq!(97372, getDecodedValue(&sample));
    }
}