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
/*
 * 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 std::collections::HashMap;

use crate::{
    common::{BitMatrix, DecoderRXingResult, DetectorRXingResult, Result},
    point_f, BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, Exceptions, Point,
    RXingResult, RXingResultMetadataType, RXingResultMetadataValue, Reader,
};

use super::{
    decoder::Decoder,
    detector::{zxing_cpp_detector, Detector},
};

use once_cell::sync::Lazy;

static DECODER: Lazy<Decoder> = Lazy::new(Decoder::new);

/**
 * This implementation can detect and decode Data Matrix codes in an image.
 *
 * @author bbrown@google.com (Brian Brown)
 */
#[derive(Default)]
pub struct DataMatrixReader;

// private static final Point[] NO_POINTS = new Point[0];

// private final Decoder decoder = new Decoder();

impl Reader for DataMatrixReader {
    /**
     * Locates and decodes a Data Matrix code in an image.
     *
     * @return a String representing the content encoded by the Data Matrix code
     * @throws NotFoundException if a Data Matrix code cannot be found
     * @throws FormatException if a Data Matrix code cannot be decoded
     * @throws ChecksumException if error correction fails
     */
    fn decode<B: Binarizer>(
        &mut self,
        image: &mut crate::BinaryBitmap<B>,
    ) -> Result<crate::RXingResult> {
        self.decode_with_hints(image, &HashMap::new())
    }

    /**
     * Locates and decodes a Data Matrix code in an image.
     *
     * @return a String representing the content encoded by the Data Matrix code
     * @throws NotFoundException if a Data Matrix code cannot be found
     * @throws FormatException if a Data Matrix code cannot be decoded
     * @throws ChecksumException if error correction fails
     */
    fn decode_with_hints<B: Binarizer>(
        &mut self,
        image: &mut crate::BinaryBitmap<B>,
        hints: &crate::DecodingHintDictionary,
    ) -> Result<crate::RXingResult> {
        let try_harder = matches!(
            hints.get(&DecodeHintType::TRY_HARDER),
            Some(DecodeHintValue::TryHarder(true))
        );
        let decoderRXingResult;
        let mut points = Vec::new();
        if hints.contains_key(&DecodeHintType::PURE_BARCODE) {
            let bits = self.extractPureBits(image.get_black_matrix())?;
            decoderRXingResult = DECODER.decode(&bits)?;
            points.clear();
        } else {
            //Result<DatamatrixDetectorResult, Exceptions>
            decoderRXingResult = if let Ok(fnd) = || -> Result<DecoderRXingResult> {
                let detectorRXingResult =
                    zxing_cpp_detector::detect(image.get_black_matrix(), try_harder, true)?;
                let decoded = DECODER.decode(detectorRXingResult.getBits())?;
                points = detectorRXingResult.getPoints().to_vec();
                Ok(decoded)
            }() {
                fnd
            } else if try_harder {
                if let Ok(fnd) = || -> Result<DecoderRXingResult> {
                    let detectorRXingResult = Detector::new(image.get_black_matrix())?.detect()?;
                    let decoded = DECODER.decode(detectorRXingResult.getBits())?;
                    points = detectorRXingResult.getPoints().to_vec();
                    Ok(decoded)
                }() {
                    fnd
                } else {
                    let bits = self.extractPureBits(image.get_black_matrix())?;
                    DECODER.decode(&bits)?
                }
            } else {
                return Err(Exceptions::NOT_FOUND);
            };

            // decoderRXingResult = DECODER.decode(detectorRXingResult.getBits())?;
        }

        let mut result = RXingResult::new(
            decoderRXingResult.getText(),
            decoderRXingResult.getRawBytes().clone(),
            points.clone(),
            BarcodeFormat::DATA_MATRIX,
        );
        let byteSegments = decoderRXingResult.getByteSegments();
        if !byteSegments.is_empty() {
            result.putMetadata(
                RXingResultMetadataType::BYTE_SEGMENTS,
                RXingResultMetadataValue::ByteSegments(byteSegments.clone()),
            );
        }
        let ecLevel = decoderRXingResult.getECLevel();
        if !ecLevel.is_empty() {
            result.putMetadata(
                RXingResultMetadataType::ERROR_CORRECTION_LEVEL,
                RXingResultMetadataValue::ErrorCorrectionLevel(ecLevel.to_string()),
            );
        }
        let other_meta = decoderRXingResult.getOther();
        if let Some(other) = other_meta {
            if let Some(dcr) = other.downcast_ref::<String>() {
                result.putMetadata(
                    RXingResultMetadataType::OTHER,
                    RXingResultMetadataValue::OTHER(dcr.to_owned()),
                );
            }
        }
        let contentType = decoderRXingResult.getContentType();
        if !contentType.is_empty() {
            result.putMetadata(
                RXingResultMetadataType::CONTENT_TYPE,
                RXingResultMetadataValue::ContentType(contentType.to_owned()),
            );
        }

        let mirrored = decoderRXingResult.getIsMirrored();
        if mirrored {
            result.putMetadata(
                RXingResultMetadataType::IS_MIRRORED,
                RXingResultMetadataValue::IsMirrored(mirrored),
            );
        }

        result.putMetadata(
            RXingResultMetadataType::SYMBOLOGY_IDENTIFIER,
            RXingResultMetadataValue::SymbologyIdentifier(format!(
                "]d{}",
                decoderRXingResult.getSymbologyModifier()
            )),
        );

        Ok(result)
    }

    fn reset(&mut self) {
        // do nothing
    }
}

impl DataMatrixReader {
    /**
     * This method detects a code in a "pure" image -- that is, pure monochrome image
     * which contains only an unrotated, unskewed, image of a code, with some white border
     * around it. This is a specialized method that works exceptionally fast in this special
     * case.
     */
    fn extractPureBits(&self, image: &BitMatrix) -> Result<BitMatrix> {
        let Some(leftTopBlack) = image.getTopLeftOnBit() else {
            return Err(Exceptions::NOT_FOUND);
        };
        let Some(rightBottomBlack) = image.getBottomRightOnBit() else {
            return Err(Exceptions::NOT_FOUND);
        };

        let moduleSize = Self::moduleSize(leftTopBlack, image)?;

        let mut top = leftTopBlack.y;
        let bottom = rightBottomBlack.y;
        let mut left = leftTopBlack.x;
        let right = rightBottomBlack.x;

        let matrixWidth = (right as i32 - left as i32 + 1) / moduleSize as i32;
        let matrixHeight = (bottom as i32 - top as i32 + 1) / moduleSize as i32;
        if matrixWidth <= 0 || matrixHeight <= 0 {
            return Err(Exceptions::NOT_FOUND);
            // throw NotFoundException.getNotFoundInstance();
        }

        let matrixWidth = matrixWidth as u32;
        let matrixHeight = matrixHeight as u32;

        // Push in the "border" by half the module width so that we start
        // sampling in the middle of the module. Just in case the image is a
        // little off, this will help recover.
        let nudge = moduleSize as f32 / 2.0;
        top += nudge;
        left += nudge;

        // Now just read off the bits
        let mut bits = BitMatrix::new(matrixWidth, matrixHeight)?;
        for y in 0..matrixHeight {
            // for (int y = 0; y < matrixHeight; y++) {
            let iOffset = top + y as f32 * moduleSize as f32;
            for x in 0..matrixWidth {
                // for (int x = 0; x < matrixWidth; x++) {
                if image.get_point(point_f(left + x as f32 * moduleSize as f32, iOffset)) {
                    bits.set(x, y);
                }
            }
        }
        Ok(bits)
    }

    fn moduleSize(leftTopBlack: Point, image: &BitMatrix) -> Result<u32> {
        let width = image.getWidth();
        let mut x = leftTopBlack.x as u32;
        let y = leftTopBlack.y as u32;
        while x < width && image.get(x, y) {
            x += 1;
        }
        if x == width {
            return Err(Exceptions::NOT_FOUND);
        }

        let moduleSize = x - leftTopBlack.x as u32;
        if moduleSize == 0 {
            return Err(Exceptions::NOT_FOUND);
        }

        Ok(moduleSize)
    }
}