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
/*
 * Copyright 2007 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::{
    common::{BitMatrix, Result},
    Exceptions, PointCallback,
};

use super::AlignmentPattern;

/**
 * <p>This class attempts to find alignment patterns in a QR Code. Alignment patterns look like finder
 * patterns but are smaller and appear at regular intervals throughout the image.</p>
 *
 * <p>At the moment this only looks for the bottom-right alignment pattern.</p>
 *
 * <p>This is mostly a simplified copy of {@link FinderPatternFinder}. It is copied,
 * pasted and stripped down here for maximum performance but does unfortunately duplicate
 * some code.</p>
 *
 * <p>This class is thread-safe but not reentrant. Each thread must allocate its own object.</p>
 *
 * @author Sean Owen
 */
pub struct AlignmentPatternFinder {
    image: BitMatrix,
    possibleCenters: Vec<AlignmentPattern>,
    startX: u32,
    startY: u32,
    width: u32,
    height: u32,
    moduleSize: f32,
    crossCheckStateCount: [u32; 3],
    resultPointCallback: Option<PointCallback>,
}

impl AlignmentPatternFinder {
    /**
     * <p>Creates a finder that will look in a portion of the whole image.</p>
     *
     * @param image image to search
     * @param startX left column from which to start searching
     * @param startY top row from which to start searching
     * @param width width of region to search
     * @param height height of region to search
     * @param moduleSize estimated module size so far
     */
    pub fn new(
        image: BitMatrix,
        startX: u32,
        startY: u32,
        width: u32,
        height: u32,
        moduleSize: f32,
        resultPointCallback: Option<PointCallback>,
    ) -> Self {
        Self {
            image,
            possibleCenters: Vec::with_capacity(5),
            startX,
            startY,
            width,
            height,
            moduleSize,
            crossCheckStateCount: [0u32; 3],
            resultPointCallback,
        }
    }

    /**
     * <p>This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since
     * it's pretty performance-critical and so is written to be fast foremost.</p>
     *
     * @return {@link AlignmentPattern} if found
     * @throws NotFoundException if not found
     */
    pub fn find(&mut self) -> Result<AlignmentPattern> {
        let startX = self.startX;
        let height = self.height;
        let maxJ = startX + self.width;
        let middleI = self.startY + (height / 2);
        // We are looking for black/white/black modules in 1:1:1 ratio;
        // this tracks the number of black/white/black modules seen so far
        let mut stateCount = [0u32; 3];
        for iGen in 0..height {
            // Search from middle outwards
            let i = (middleI as i32
                + (if (iGen & 0x01) == 0 {
                    (iGen as i32 + 1) / 2
                } else {
                    -((iGen as i32 + 1) / 2)
                })) as u32;

            stateCount.fill(0);

            let mut j = startX;
            // Burn off leading white pixels before anything else; if we start in the middle of
            // a white run, it doesn't make sense to count its length, since we don't know if the
            // white run continued to the left of the start point
            while j < maxJ && !self.image.get(j, i) {
                j += 1;
            }
            let mut currentState = 0;
            while j < maxJ {
                if self.image.get(j, i) {
                    // Black pixel
                    if currentState == 1 {
                        // Counting black pixels
                        stateCount[1] += 1;
                    } else {
                        // Counting white pixels
                        if currentState == 2 {
                            // A winner?
                            if self.foundPatternCross(&stateCount) {
                                // Yes
                                if let Some(confirmed) =
                                    self.handlePossibleCenter(&stateCount, i, j)
                                {
                                    return Ok(confirmed);
                                }
                            }
                            stateCount[0] = stateCount[2];
                            stateCount[1] = 1;
                            stateCount[2] = 0;
                            currentState = 1;
                        } else {
                            currentState += 1;
                            stateCount[currentState] += 1;
                        }
                    }
                } else {
                    // White pixel
                    if currentState == 1 {
                        // Counting black pixels
                        currentState += 1;
                    }
                    stateCount[currentState] += 1;
                }
                j += 1;
            }
            if self.foundPatternCross(&stateCount) {
                if let Some(confirmed) = self.handlePossibleCenter(&stateCount, i, maxJ) {
                    return Ok(confirmed);
                }
            }
        }

        // Hmm, nothing we saw was observed and confirmed twice. If we had
        // any guess at all, return it.
        if !self.possibleCenters.is_empty() {
            Ok(*(self
                .possibleCenters
                .get(0)
                .ok_or(Exceptions::INDEX_OUT_OF_BOUNDS))?)
        } else {
            Err(Exceptions::NOT_FOUND)
        }
    }

    /**
     * Given a count of black/white/black pixels just seen and an end position,
     * figures the location of the center of this black/white/black run.
     */
    fn centerFromEnd(stateCount: &[u32], end: u32) -> f32 {
        (end as f32 - stateCount[2] as f32) - stateCount[1] as f32 / 2.0
    }

    /**
     * @param stateCount count of black/white/black pixels just read
     * @return true iff the proportions of the counts is close enough to the 1/1/1 ratios
     *         used by alignment patterns to be considered a match
     */
    fn foundPatternCross(&self, stateCount: &[u32]) -> bool {
        let moduleSize = self.moduleSize;
        let maxVariance = moduleSize / 2.0;
        for state in stateCount.iter().take(3) {
            if (moduleSize - *state as f32).abs() >= maxVariance {
                return false;
            }
        }
        true
    }

    /**
     * <p>After a horizontal scan finds a potential alignment pattern, this method
     * "cross-checks" by scanning down vertically through the center of the possible
     * alignment pattern to see if the same proportion is detected.</p>
     *
     * @param startI row where an alignment pattern was detected
     * @param centerJ center of the section that appears to cross an alignment pattern
     * @param maxCount maximum reasonable number of modules that should be
     * observed in any reading state, based on the results of the horizontal scan
     * @return vertical center of alignment pattern, or {@link Float#NaN} if not found
     */
    fn crossCheckVertical(
        &mut self,
        startI: u32,
        centerJ: u32,
        maxCount: u32,
        originalStateCountTotal: u32,
    ) -> f32 {
        let image = &self.image;

        let maxI = image.getHeight();
        self.crossCheckStateCount.fill(0);

        // Start counting up from center
        let mut i = startI as i32;
        while i >= 0 && image.get(centerJ, i as u32) && self.crossCheckStateCount[1] <= maxCount {
            self.crossCheckStateCount[1] += 1;
            i -= 1;
        }
        // If already too many modules in this state or ran off the edge:
        if i < 0 || self.crossCheckStateCount[1] > maxCount {
            return f32::NAN;
        }
        while i >= 0 && !image.get(centerJ, i as u32) && self.crossCheckStateCount[0] <= maxCount {
            self.crossCheckStateCount[0] += 1;
            i -= 1;
        }
        if self.crossCheckStateCount[0] > maxCount {
            return f32::NAN;
        }

        // Now also count down from center
        i = startI as i32 + 1;
        while i < maxI as i32
            && image.get(centerJ, i as u32)
            && self.crossCheckStateCount[1] <= maxCount
        {
            self.crossCheckStateCount[1] += 1;
            i += 1;
        }
        if i == maxI as i32 || self.crossCheckStateCount[1] > maxCount {
            return f32::NAN;
        }
        while i < maxI as i32
            && !image.get(centerJ, i as u32)
            && self.crossCheckStateCount[2] <= maxCount
        {
            self.crossCheckStateCount[2] += 1;
            i += 1;
        }
        if self.crossCheckStateCount[2] > maxCount {
            return f32::NAN;
        }

        let stateCountTotal = self.crossCheckStateCount[0]
            + self.crossCheckStateCount[1]
            + self.crossCheckStateCount[2];
        if 5 * (stateCountTotal as i64 - originalStateCountTotal as i64).unsigned_abs() as u32
            >= 2 * originalStateCountTotal
        {
            return f32::NAN;
        }

        if self.foundPatternCross(&self.crossCheckStateCount) {
            Self::centerFromEnd(&self.crossCheckStateCount, i as u32)
        } else {
            f32::NAN
        }
    }

    /**
     * <p>This is called when a horizontal scan finds a possible alignment pattern. It will
     * cross check with a vertical scan, and if successful, will see if this pattern had been
     * found on a previous horizontal scan. If so, we consider it confirmed and conclude we have
     * found the alignment pattern.</p>
     *
     * @param stateCount reading state module counts from horizontal scan
     * @param i row where alignment pattern may be found
     * @param j end of possible alignment pattern in row
     * @return {@link AlignmentPattern} if we have found the same pattern twice, or null if not
     */
    fn handlePossibleCenter(
        &mut self,
        stateCount: &[u32],
        i: u32,
        j: u32,
    ) -> Option<AlignmentPattern> {
        let stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2];
        let centerJ = Self::centerFromEnd(stateCount, j);
        let centerI = self.crossCheckVertical(
            i,
            centerJ.floor() as u32,
            2 * stateCount[1],
            stateCountTotal,
        );

        if !centerI.is_nan() {
            let estimatedModuleSize = (stateCount[0] + stateCount[1] + stateCount[2]) as f32 / 3.0;
            for center in &self.possibleCenters {
                // Look for about the same center and module size:
                if center.aboutEquals(estimatedModuleSize, centerI, centerJ) {
                    return Some(center.combineEstimate(centerI, centerJ, estimatedModuleSize));
                }
            }
            // Hadn't found this before; save it
            let point = AlignmentPattern::new(centerJ, centerI, estimatedModuleSize);
            if let Some(rpc) = self.resultPointCallback.clone() {
                rpc((&point).into());
            }

            self.possibleCenters.push(point);
        }

        None
    }
}