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
/*
 * Copyright 2009 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.
 */

// package com.google.zxing.common;

// import com.google.zxing.Binarizer;
// import com.google.zxing.LuminanceSource;
// import com.google.zxing.NotFoundException;

use std::borrow::Cow;

use once_cell::unsync::OnceCell;

use crate::common::Result;
use crate::{Binarizer, Exceptions, LuminanceSource};

use super::{BitArray, BitMatrix};

const LUMINANCE_BITS: usize = 5;
const LUMINANCE_SHIFT: usize = 8 - LUMINANCE_BITS;
const LUMINANCE_BUCKETS: usize = 1 << LUMINANCE_BITS;

/**
 * This Binarizer implementation uses the old ZXing global histogram approach. It is suitable
 * for low-end mobile devices which don't have enough CPU or memory to use a local thresholding
 * algorithm. However, because it picks a global black point, it cannot handle difficult shadows
 * and gradients.
 *
 * Faster mobile devices and all desktop applications should probably use HybridBinarizer instead.
 *
 * @author dswitkin@google.com (Daniel Switkin)
 * @author Sean Owen
 */
pub struct GlobalHistogramBinarizer<LS: LuminanceSource> {
    //_luminances: Vec<u8>,
    width: usize,
    height: usize,
    source: LS,
    black_matrix: OnceCell<BitMatrix>,
    black_row_cache: Vec<OnceCell<BitArray>>,
}

impl<LS: LuminanceSource> Binarizer for GlobalHistogramBinarizer<LS> {
    type Source = LS;

    fn get_luminance_source(&self) -> &Self::Source {
        &self.source
    }

    // Applies simple sharpening to the row data to improve performance of the 1D Readers.
    fn get_black_row(&self, y: usize) -> Result<Cow<BitArray>> {
        let row = self.black_row_cache[y].get_or_try_init(|| {
            let source = self.get_luminance_source();
            let width = source.get_width();
            let mut row = BitArray::with_size(width);

            // self.initArrays(width);
            let localLuminances = source.get_row(y);
            let mut localBuckets = [0; LUMINANCE_BUCKETS]; //self.buckets.clone();
            for x in 0..width {
                // for (int x = 0; x < width; x++) {
                localBuckets[((localLuminances[x]) >> LUMINANCE_SHIFT) as usize] += 1;
            }
            let blackPoint = Self::estimateBlackPoint(&localBuckets)?;

            if width < 3 {
                // Special case for very small images
                for (x, lum) in localLuminances.iter().enumerate().take(width) {
                    // for x in 0..width {
                    //   for (int x = 0; x < width; x++) {
                    if (*lum as u32) < blackPoint {
                        row.set(x);
                    }
                }
            } else {
                let mut left = localLuminances[0]; // & 0xff;
                let mut center = localLuminances[1]; // & 0xff;
                for x in 1..width - 1 {
                    //   for (int x = 1; x < width - 1; x++) {
                    let right = localLuminances[x + 1];
                    // A simple -1 4 -1 box filter with a weight of 2.
                    if ((center as i64 * 4) - left as i64 - right as i64) / 2 < blackPoint as i64 {
                        row.set(x);
                    }
                    left = center;
                    center = right;
                }
            }

            Ok(row)
        })?;

        Ok(Cow::Borrowed(row))
    }

    // Does not sharpen the data, as this call is intended to only be used by 2D Readers.
    fn get_black_matrix(&self) -> Result<&BitMatrix> {
        let matrix = self
            .black_matrix
            .get_or_try_init(|| Self::build_black_matrix(&self.source))?;
        Ok(matrix)
    }

    fn create_binarizer(&self, source: LS) -> Self {
        Self::new(source)
    }

    fn get_width(&self) -> usize {
        self.width
    }

    fn get_height(&self) -> usize {
        self.height
    }
}

impl<LS: LuminanceSource> GlobalHistogramBinarizer<LS> {
    // const EMPTY: [u8; 0] = [0; 0];

    pub fn new(source: LS) -> Self {
        Self {
            //_luminances: vec![0; source.getWidth()],
            width: source.get_width(),
            height: source.get_height(),
            black_matrix: OnceCell::new(),
            black_row_cache: vec![OnceCell::default(); source.get_height()],
            source,
        }
    }

    fn build_black_matrix(source: &LS) -> Result<BitMatrix> {
        // let source = source.getLuminanceSource();
        let width = source.get_width();
        let height = source.get_height();
        let mut matrix = BitMatrix::new(width as u32, height as u32)?;

        // Quickly calculates the histogram by sampling four rows from the image. This proved to be
        // more robust on the blackbox tests than sampling a diagonal as we used to do.
        // self.initArrays(width);
        let mut localBuckets = [0; LUMINANCE_BUCKETS]; //self.buckets.clone();
        for y in 1..5 {
            // for (int y = 1; y < 5; y++) {
            let row = height * y / 5;
            let localLuminances = source.get_row(row);
            let right = (width * 4) / 5;
            let mut x = width / 5;
            while x < right {
                //   for (int x = width / 5; x < right; x++) {
                let pixel = localLuminances[x];
                localBuckets[(pixel >> LUMINANCE_SHIFT) as usize] += 1;
                x += 1;
            }
        }
        let blackPoint = Self::estimateBlackPoint(&localBuckets)?;

        // We delay reading the entire image luminance until the black point estimation succeeds.
        // Although we end up reading four rows twice, it is consistent with our motto of
        // "fail quickly" which is necessary for continuous scanning.
        let localLuminances = source.get_matrix();
        for y in 0..height {
            // for (int y = 0; y < height; y++) {
            let offset = y * width;
            for x in 0..width {
                //   for (int x = 0; x < width; x++) {
                let pixel = localLuminances[offset + x];
                if (pixel as u32) < blackPoint {
                    matrix.set(x as u32, y as u32);
                }
            }
        }

        Ok(matrix)
    }

    // fn initArrays(&mut self, luminanceSize: usize) {
    //     // if self.luminances.len() < luminanceSize {
    //     //     self.luminances = ;
    //     // }
    //     // // for x in 0..GlobalHistogramBinarizer::LUMINANCE_BUCKETS {
    //     //     // for (int x = 0; x < LUMINANCE_BUCKETS; x++) {
    //     //     self.buckets[x] = 0;
    //     // }
    // }

    fn estimateBlackPoint(buckets: &[u32]) -> Result<u32> {
        // Find the tallest peak in the histogram.
        let numBuckets = buckets.len();
        let mut maxBucketCount = 0;
        let mut firstPeak = 0;
        let mut firstPeakSize = 0;
        for (x, bucket) in buckets.iter().enumerate().take(numBuckets) {
            // for x in 0..numBuckets {
            // for (int x = 0; x < numBuckets; x++) {
            if *bucket > firstPeakSize {
                firstPeak = x;
                firstPeakSize = *bucket;
            }
            if *bucket > maxBucketCount {
                maxBucketCount = *bucket;
            }
        }

        // Find the second-tallest peak which is somewhat far from the tallest peak.
        let mut secondPeak = 0;
        let mut secondPeakScore = 0;
        for (x, bucket) in buckets.iter().enumerate().take(numBuckets) {
            // for x in 0..numBuckets {
            // for (int x = 0; x < numBuckets; x++) {
            let distanceToBiggest = (x as i32 - firstPeak as i32).unsigned_abs();
            // Encourage more distant second peaks by multiplying by square of distance.
            let score = *bucket * distanceToBiggest * distanceToBiggest;
            if score > secondPeakScore {
                secondPeak = x;
                secondPeakScore = score;
            }
        }

        // Make sure firstPeak corresponds to the black peak.
        if firstPeak > secondPeak {
            std::mem::swap(&mut firstPeak, &mut secondPeak);
        }

        // If there is too little contrast in the image to pick a meaningful black point, throw rather
        // than waste time trying to decode the image, and risk false positives.
        if secondPeak - firstPeak <= numBuckets / 16 {
            return Err(Exceptions::not_found_with(
                "secondPeak - firstPeak <= numBuckets / 16 ",
            ));
        }

        // Find a valley between them that is low and closer to the white peak.
        let mut bestValley = secondPeak - 1;
        let mut bestValleyScore = -1;
        let mut x = secondPeak;
        while x > firstPeak {
            // for (int x = secondPeak - 1; x > firstPeak; x--) {
            let fromFirst = x - firstPeak;
            let score =
                fromFirst * fromFirst * (secondPeak - x) * (maxBucketCount - buckets[x]) as usize;
            if score as i32 > bestValleyScore {
                bestValley = x;
                bestValleyScore = score as i32;
            }
            x -= 1;
        }

        Ok((bestValley as u32) << LUMINANCE_SHIFT)
    }
}