From 0107d35b1b11f5fe60c5087f575a79ec6540bfc5 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sat, 28 Jan 2023 17:04:17 -0600 Subject: [PATCH] backport changes from c++ for datamatrix --- src/datamatrix/decoder/data_block.rs | 3 ++- src/datamatrix/decoder/datamatrix_decoder.rs | 26 ++++++++++++++++--- .../zxing_cpp_detector/bitmatrix_cursor.rs | 12 +++++---- .../zxing_cpp_detector/edge_tracer.rs | 4 +++ 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/datamatrix/decoder/data_block.rs b/src/datamatrix/decoder/data_block.rs index 5125599..eeed3f3 100644 --- a/src/datamatrix/decoder/data_block.rs +++ b/src/datamatrix/decoder/data_block.rs @@ -51,6 +51,7 @@ impl DataBlock { pub fn getDataBlocks( rawCodewords: &[u8], version: &Version, + fix259: bool, ) -> Result, Exceptions> { // Figure out the number and size of data blocks used by this version let ecBlocks = version.getECBlocks(); @@ -121,7 +122,7 @@ impl DataBlock { // for (int i = longerBlocksNumDataCodewords; i < max; i++) { for j in 0..numRXingResultBlocks { // for (int j = 0; j < numRXingResultBlocks; j++) { - let jOffset = if specialVersion { + let jOffset = if specialVersion && fix259 { (j + 8) % numRXingResultBlocks } else { j diff --git a/src/datamatrix/decoder/datamatrix_decoder.rs b/src/datamatrix/decoder/datamatrix_decoder.rs index 7c3e5c0..a4757f0 100644 --- a/src/datamatrix/decoder/datamatrix_decoder.rs +++ b/src/datamatrix/decoder/datamatrix_decoder.rs @@ -40,6 +40,19 @@ impl Decoder { // rsDecoder = new ReedSolomonDecoder(GenericGF.DATA_MATRIX_FIELD_256); } + /** + *

Decodes a Data Matrix Code represented as a {@link BitMatrix}. A 1 or "true" is taken + * to mean a black module.

+ * + * @param bits booleans representing white/black Data Matrix Code modules + * @return text and bytes encoded within the Data Matrix Code + * @throws FormatException if the Data Matrix Code cannot be decoded + * @throws ChecksumException if error correction fails + */ + pub fn decode(&self, bits: &BitMatrix) -> Result { + self.perform_decode(bits, false) + } + /** *

Convenience method that can decode a Data Matrix Code represented as a 2D array of booleans. * "true" is taken to mean a black module.

@@ -50,7 +63,7 @@ impl Decoder { * @throws ChecksumException if error correction fails */ pub fn decode_bools(&self, image: &Vec>) -> Result { - self.decode(&BitMatrix::parse_bools(image)) + self.perform_decode(&BitMatrix::parse_bools(image),false) } /** @@ -62,7 +75,7 @@ impl Decoder { * @throws FormatException if the Data Matrix Code cannot be decoded * @throws ChecksumException if error correction fails */ - pub fn decode(&self, bits: &BitMatrix) -> Result { + fn perform_decode(&self, bits: &BitMatrix, fix259: bool) -> Result { // Construct a parser and read version, error-correction level let mut parser = BitMatrixParser::new(bits)?; @@ -72,7 +85,7 @@ impl Decoder { let version = parser.getVersion(); // Separate into data blocks - let dataBlocks = DataBlock::getDataBlocks(&codewords, version)?; + let dataBlocks = DataBlock::getDataBlocks(&codewords, version, fix259)?; // Count total number of data bytes let totalBytes = dataBlocks @@ -88,7 +101,12 @@ impl Decoder { let dataBlock = &dataBlocks[j]; let mut codewordBytes = dataBlock.getCodewords().to_vec(); let numDataCodewords = dataBlock.getNumDataCodewords() as usize; - self.correctErrors(&mut codewordBytes, numDataCodewords as u32)?; + let errors_corrected = self.correctErrors(&mut codewordBytes, numDataCodewords as u32); + if errors_corrected.is_err() && !fix259 { + return self.perform_decode(bits, true); + }else if errors_corrected.is_err() { + return Err(errors_corrected.err().unwrap()) + } for i in 0..numDataCodewords { // for (int i = 0; i < numDataCodewords; i++) { // De-interlace data blocks. diff --git a/src/datamatrix/detector/zxing_cpp_detector/bitmatrix_cursor.rs b/src/datamatrix/detector/zxing_cpp_detector/bitmatrix_cursor.rs index e4d94a3..43c37ef 100644 --- a/src/datamatrix/detector/zxing_cpp_detector/bitmatrix_cursor.rs +++ b/src/datamatrix/detector/zxing_cpp_detector/bitmatrix_cursor.rs @@ -139,16 +139,18 @@ pub trait BitMatrixCursor { ret } - fn countEdges(&mut self, range: Option) -> i32 { - let mut range = if let Some(r) = range { r } else { 0 }; + fn countEdges(&mut self, range: i32) -> i32 { let mut res = 0; + let mut range = range; - let mut steps = self.stepToEdge(Some(1), Some(range), None); + let mut steps; - while steps > 0 { + while { + steps = if range == 0 { 0 } else {self.stepToEdge(Some(1), Some(range), None)}; + steps > 0 + } { range -= steps; res += 1; - steps = self.stepToEdge(Some(1), Some(range), None); } res diff --git a/src/datamatrix/detector/zxing_cpp_detector/edge_tracer.rs b/src/datamatrix/detector/zxing_cpp_detector/edge_tracer.rs index debe34e..412b171 100644 --- a/src/datamatrix/detector/zxing_cpp_detector/edge_tracer.rs +++ b/src/datamatrix/detector/zxing_cpp_detector/edge_tracer.rs @@ -326,6 +326,10 @@ impl<'a> EdgeTracer<'_> { return Ok(false); } + // re-evaluate line with all the points up to here before projecting + if (!line.evaluate_max_distance(Some(1.5), None)) + {return Ok(false);} + let mut np = line.project(&self.p); // make sure we are making progress even when back-projecting: // consider a 90deg corner, rotated 45deg. we step away perpendicular from the line and get