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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright 2008 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::{
        detector::WhiteRectangleDetector, BitMatrix, DefaultGridSampler, GridSampler, Result,
    },
    point, Exceptions, Point,
};

use super::DatamatrixDetectorResult;

/**
 * <p>Encapsulates logic that can detect a Data Matrix Code in an image, even if the Data Matrix Code
 * is rotated or skewed, or partially obscured.</p>
 *
 * @author Sean Owen
 */
pub struct Detector<'a> {
    image: &'a BitMatrix,
    rectangleDetector: WhiteRectangleDetector<'a>,
}
impl<'a> Detector<'_> {
    pub fn new(image: &'a BitMatrix) -> Result<Detector<'a>> {
        Ok(Detector {
            rectangleDetector: WhiteRectangleDetector::new_from_image(image)?,
            image,
        })
    }

    /**
     * <p>Detects a Data Matrix Code in an image.</p>
     *
     * @return {@link DetectorRXingResult} encapsulating results of detecting a Data Matrix Code
     * @throws NotFoundException if no Data Matrix Code can be found
     */
    pub fn detect(&self) -> Result<DatamatrixDetectorResult> {
        let cornerPoints = self.rectangleDetector.detect()?;

        let mut points = self.detectSolid1(cornerPoints);
        points = self.detectSolid2(points);
        if let Some(point) = self.correctTopRight(&points) {
            points[3] = point;
        } else {
            return Err(Exceptions::notFoundWith("point 4 unfound"));
        }
        // points[3] = self.correctTopRight(&points);
        // if points[3] == null {
        //   throw NotFoundException.getNotFoundInstance();
        // }
        points = self.shiftToModuleCenter(points);

        let topLeft = points[0];
        let bottomLeft = points[1];
        let bottomRight = points[2];
        let topRight = points[3];

        let mut dimensionTop = self.transitionsBetween(topLeft, topRight) + 1;
        let mut dimensionRight = self.transitionsBetween(bottomRight, topRight) + 1;
        if (dimensionTop & 0x01) == 1 {
            dimensionTop += 1;
        }
        if (dimensionRight & 0x01) == 1 {
            dimensionRight += 1;
        }

        if 4 * dimensionTop < 6 * dimensionRight && 4 * dimensionRight < 6 * dimensionTop {
            // The matrix is square
            dimensionTop = dimensionTop.max(dimensionRight);
            dimensionRight = dimensionTop.max(dimensionRight);
        }

        let bits = Self::sampleGrid(
            self.image,
            topLeft,
            bottomLeft,
            bottomRight,
            topRight,
            dimensionTop,
            dimensionRight,
        )?;

        Ok(DatamatrixDetectorResult::new(
            bits,
            vec![topLeft, bottomLeft, bottomRight, topRight],
        ))
    }

    fn shiftPoint(p: Point, to: Point, div: u32) -> Point {
        let x = (to.x - p.x) / (div as f32 + 1.0);
        let y = (to.y - p.y) / (div as f32 + 1.0);
        point(p.x + x, p.y + y)
    }

    fn moveAway(p: Point, fromX: f32, fromY: f32) -> Point {
        let mut x = p.x;
        let mut y = p.y;

        if x < fromX {
            x -= 1.0;
        } else {
            x += 1.0;
        }

        if y < fromY {
            y -= 1.0;
        } else {
            y += 1.0;
        }

        point(x, y)
    }

    /**
     * Detect a solid side which has minimum transition.
     */
    fn detectSolid1(&self, cornerPoints: [Point; 4]) -> [Point; 4] {
        // 0  2
        // 1  3
        let pointA = cornerPoints[0];
        let pointB = cornerPoints[1];
        let pointC = cornerPoints[3];
        let pointD = cornerPoints[2];

        let trAB = self.transitionsBetween(pointA, pointB);
        let trBC = self.transitionsBetween(pointB, pointC);
        let trCD = self.transitionsBetween(pointC, pointD);
        let trDA = self.transitionsBetween(pointD, pointA);

        // 0..3
        // :  :
        // 1--2
        let mut min = trAB;
        let mut points = [pointD, pointA, pointB, pointC];
        if min > trBC {
            min = trBC;
            points[0] = pointA;
            points[1] = pointB;
            points[2] = pointC;
            points[3] = pointD;
        }
        if min > trCD {
            min = trCD;
            points[0] = pointB;
            points[1] = pointC;
            points[2] = pointD;
            points[3] = pointA;
        }
        if min > trDA {
            points[0] = pointC;
            points[1] = pointD;
            points[2] = pointA;
            points[3] = pointB;
        }

        points
    }

    /**
     * Detect a second solid side next to first solid side.
     */
    fn detectSolid2(&self, points: [Point; 4]) -> [Point; 4] {
        // A..D
        // :  :
        // B--C
        let pointA = points[0];
        let pointB = points[1];
        let pointC = points[2];
        let pointD = points[3];

        // Transition detection on the edge is not stable.
        // To safely detect, shift the points to the module center.
        let tr = self.transitionsBetween(pointA, pointD);
        let pointBs = Self::shiftPoint(pointB, pointC, (tr + 1) * 4);
        let pointCs = Self::shiftPoint(pointC, pointB, (tr + 1) * 4);
        let trBA = self.transitionsBetween(pointBs, pointA);
        let trCD = self.transitionsBetween(pointCs, pointD);

        // 0..3
        // |  :
        // 1--2
        if trBA < trCD {
            // solid sides: A-B-C
            [pointA, pointB, pointC, pointD]
            // points[0] = pointA;
            // points[1] = pointB;
            // points[2] = pointC;
            // points[3] = pointD;
        } else {
            // solid sides: B-C-D
            [pointB, pointC, pointD, pointA]
            // points[0] = pointB;
            // points[1] = pointC;
            // points[2] = pointD;
            // points[3] = pointA;
        }
    }

    /**
     * Calculates the corner position of the white top right module.
     */
    fn correctTopRight(&self, points: &[Point; 4]) -> Option<Point> {
        // A..D
        // |  :
        // B--C
        let pointA = points[0];
        let pointB = points[1];
        let pointC = points[2];
        let pointD = points[3];

        // shift points for safe transition detection.
        let mut trTop = self.transitionsBetween(pointA, pointD);
        let mut trRight = self.transitionsBetween(pointB, pointD);
        let pointAs = Self::shiftPoint(pointA, pointB, (trRight + 1) * 4);
        let pointCs = Self::shiftPoint(pointC, pointB, (trTop + 1) * 4);

        trTop = self.transitionsBetween(pointAs, pointD);
        trRight = self.transitionsBetween(pointCs, pointD);

        let candidate1 = point(
            pointD.x + (pointC.x - pointB.x) / (trTop as f32 + 1.0),
            pointD.y + (pointC.y - pointB.y) / (trTop as f32 + 1.0),
        );
        let candidate2 = point(
            pointD.x + (pointA.x - pointB.x) / (trRight as f32 + 1.0),
            pointD.y + (pointA.y - pointB.y) / (trRight as f32 + 1.0),
        );

        if !self.isValid(candidate1) {
            if self.isValid(candidate2) {
                return Some(candidate2);
            }
            return None;
        }
        if !self.isValid(candidate2) {
            return Some(candidate1);
        }

        let sumc1 = self.transitionsBetween(pointAs, candidate1)
            + self.transitionsBetween(pointCs, candidate1);
        let sumc2 = self.transitionsBetween(pointAs, candidate2)
            + self.transitionsBetween(pointCs, candidate2);

        if sumc1 > sumc2 {
            Some(candidate1)
        } else {
            Some(candidate2)
        }
    }

    /**
     * Shift the edge points to the module center.
     */
    fn shiftToModuleCenter(&self, points: [Point; 4]) -> [Point; 4] {
        // A..D
        // |  :
        // B--C
        let mut pointA = points[0];
        let mut pointB = points[1];
        let mut pointC = points[2];
        let mut pointD = points[3];

        // calculate pseudo dimensions
        let mut dimH = self.transitionsBetween(pointA, pointD) + 1;
        let mut dimV = self.transitionsBetween(pointC, pointD) + 1;

        // shift points for safe dimension detection
        let mut pointAs = Self::shiftPoint(pointA, pointB, dimV * 4);
        let mut pointCs = Self::shiftPoint(pointC, pointB, dimH * 4);

        //  calculate more precise dimensions
        dimH = self.transitionsBetween(pointAs, pointD) + 1;
        dimV = self.transitionsBetween(pointCs, pointD) + 1;
        if (dimH & 0x01) == 1 {
            dimH += 1;
        }
        if (dimV & 0x01) == 1 {
            dimV += 1;
        }

        // WhiteRectangleDetector returns points inside of the rectangle.
        // I want points on the edges.
        let centerX = (pointA.x + pointB.x + pointC.x + pointD.x) / 4.0;
        let centerY = (pointA.y + pointB.y + pointC.y + pointD.y) / 4.0;
        pointA = Self::moveAway(pointA, centerX, centerY);
        pointB = Self::moveAway(pointB, centerX, centerY);
        pointC = Self::moveAway(pointC, centerX, centerY);
        pointD = Self::moveAway(pointD, centerX, centerY);

        let mut pointBs;
        let mut pointDs;

        // shift points to the center of each modules
        pointAs = Self::shiftPoint(pointA, pointB, dimV * 4);
        pointAs = Self::shiftPoint(pointAs, pointD, dimH * 4);
        pointBs = Self::shiftPoint(pointB, pointA, dimV * 4);
        pointBs = Self::shiftPoint(pointBs, pointC, dimH * 4);
        pointCs = Self::shiftPoint(pointC, pointD, dimV * 4);
        pointCs = Self::shiftPoint(pointCs, pointB, dimH * 4);
        pointDs = Self::shiftPoint(pointD, pointC, dimV * 4);
        pointDs = Self::shiftPoint(pointDs, pointA, dimH * 4);

        [pointAs, pointBs, pointCs, pointDs]
    }

    fn isValid(&self, p: Point) -> bool {
        p.x >= 0.0
            && p.x <= self.image.getWidth() as f32 - 1.0
            && p.y > 0.0
            && p.y <= self.image.getHeight() as f32 - 1.0
    }

    fn sampleGrid(
        image: &BitMatrix,
        topLeft: Point,
        bottomLeft: Point,
        bottomRight: Point,
        topRight: Point,
        dimensionX: u32,
        dimensionY: u32,
    ) -> Result<BitMatrix> {
        let sampler = DefaultGridSampler::default();

        sampler.sample_grid_detailed(
            image,
            dimensionX,
            dimensionY,
            0.5,
            0.5,
            dimensionX as f32 - 0.5,
            0.5,
            dimensionX as f32 - 0.5,
            dimensionY as f32 - 0.5,
            0.5,
            dimensionY as f32 - 0.5,
            topLeft.x,
            topLeft.y,
            topRight.x,
            topRight.y,
            bottomRight.x,
            bottomRight.y,
            bottomLeft.x,
            bottomLeft.y,
        )
    }

    /**
     * Counts the number of black/white transitions between two points, using something like Bresenham's algorithm.
     */
    fn transitionsBetween(&self, from: Point, to: Point) -> u32 {
        // See QR Code Detector, sizeOfBlackWhiteBlackRun()
        let mut fromX = from.x.floor() as i32;
        let mut fromY = from.y.floor() as i32;
        let mut toX = to.x.floor() as i32;
        let mut toY = (self.image.getHeight() - 1).min(to.y.floor() as u32) as i32;

        let steep = (toY - fromY).abs() > (toX - fromX).abs();
        if steep {
            std::mem::swap(&mut fromX, &mut fromY);
            std::mem::swap(&mut toX, &mut toY);
        }

        let dx = (toX - fromX).abs();
        let dy = (toY - fromY).abs();
        let mut error = -dx / 2;
        let ystep = if fromY < toY { 1 } else { -1 };
        let xstep = if fromX < toX { 1 } else { -1 };
        let mut transitions = 0;
        let mut inBlack = self.image.get(
            if steep { fromY as u32 } else { fromX as u32 },
            if steep { fromX as u32 } else { fromY as u32 },
        );
        let mut x = fromX;
        let mut y = fromY;
        while x != toX {
            // for (int x = fromX, y = fromY; x != toX; x += xstep) {
            let isBlack = self.image.get(
                if steep { y as u32 } else { x as u32 },
                if steep { x as u32 } else { y as u32 },
            );
            if isBlack != inBlack {
                transitions += 1;
                inBlack = isBlack;
            }
            error += dy;
            if error > 0 {
                if y == toY {
                    break;
                }
                y += ystep;
                error -= dx;
            }

            x += xstep;
        }
        transitions
    }
}