From 7395bb2c29958301385ce4980c4cce401f2bea8e Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Wed, 12 Oct 2022 19:14:47 -0500 Subject: [PATCH] fixed issue with ec results not being used --- src/common/mod.rs | 42 ++++++++++++------- src/common/reedsolomon/mod.rs | 22 +++++----- src/qrcode/decoder/decoder.rs | 7 +++- .../detector/alignment_pattern_finder.rs | 2 +- src/qrcode/detector/detector.rs | 4 +- src/qrcode/detector/finder_pattern.rs | 2 +- src/qrcode/detector/finder_pattern_finder.rs | 4 +- tests/QRCodeBlackBox1TestCase.rs | 5 +-- 8 files changed, 51 insertions(+), 37 deletions(-) diff --git a/src/common/mod.rs b/src/common/mod.rs index 2a00f22..84ebd7f 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -181,7 +181,7 @@ impl StringUtils { break; } - let value = bytes[i] & 0xFF; + let value = bytes[i]; // UTF-8 stuff if can_be_utf8 { @@ -1545,7 +1545,7 @@ impl BitSource { return Err(Exceptions::IllegalArgumentException(numBits.to_string())); } - let mut result:u32 = 0; + let mut result: u32 = 0; let mut num_bits = numBits; @@ -1850,7 +1850,12 @@ pub struct DecoderRXingResult { } impl DecoderRXingResult { - pub fn new(rawBytes: Vec, text: String, byteSegments: Vec>, ecLevel: String) -> Self { + pub fn new( + rawBytes: Vec, + text: String, + byteSegments: Vec>, + ecLevel: String, + ) -> Self { Self::with_all(rawBytes, text, byteSegments, ecLevel, -2, -2, 0) } @@ -1894,7 +1899,7 @@ impl DecoderRXingResult { pub fn with_all( rawBytes: Vec, text: String, - byteSegments:Vec>, + byteSegments: Vec>, ecLevel: String, saSequence: i32, saParity: i32, @@ -2243,11 +2248,11 @@ pub trait GridSampler { } // Check and nudge points from end: nudged = true; - let mut offset = points.len() - 2; + let mut offset = points.len() as isize - 2; while offset >= 0 && nudged { // for (int offset = points.length - 2; offset >= 0 && nudged; offset -= 2) { - let x = points[offset] as i32; - let y = points[offset + 1] as i32; + let x = points[offset as usize] as i32; + let y = points[offset as usize + 1] as i32; if x < -1 || x > width.try_into().unwrap() || y < -1 || y > height.try_into().unwrap() { return Err(Exceptions::NotFoundException( "getNotFoundInstance".to_owned(), @@ -2255,20 +2260,20 @@ pub trait GridSampler { } nudged = false; if x == -1 { - points[offset] = 0.0f32; + points[offset as usize] = 0.0f32; nudged = true; - } else if (x == width.try_into().unwrap()) { - points[offset] = width as f32 - 1f32; + } else if x == width.try_into().unwrap() { + points[offset as usize] = width as f32 - 1f32; nudged = true; } if y == -1 { - points[offset + 1] = 0.0f32; + points[offset as usize + 1] = 0.0f32; nudged = true; - } else if (y == height.try_into().unwrap()) { - points[offset + 1] = height as f32 - 1f32; + } else if y == height.try_into().unwrap() { + points[offset as usize + 1] = height as f32 - 1f32; nudged = true; } - offset += 2; + offset += -2; } Ok(()) } @@ -2363,6 +2368,13 @@ impl GridSampler for DefaultGridSampler { let mut x = 0; while x < max { // for (int x = 0; x < max; x += 2) { + if points[x].floor() as u32 >= image.getWidth() + || points[x + 1].floor() as u32 >= image.getHeight() + { + return Err(Exceptions::NotFoundException( + "index out of bounds, see documentation in file for explanation".to_owned(), + )); + } if image.get(points[x].floor() as u32, points[x + 1].floor() as u32) { // Black(-ish) pixel bits.set(x as u32 / 2, y); @@ -4127,4 +4139,4 @@ impl HybridBinarizer { } blackPoints } -} \ No newline at end of file +} diff --git a/src/common/reedsolomon/mod.rs b/src/common/reedsolomon/mod.rs index 309323e..37dcf8f 100644 --- a/src/common/reedsolomon/mod.rs +++ b/src/common/reedsolomon/mod.rs @@ -701,7 +701,7 @@ impl ReedSolomonDecoder { } let syndrome = match GenericGFPoly::new(self.field, &syndromeCoefficients) { Ok(res) => res, - Err(fail) => { + Err(_fail) => { return Err(Exceptions::ReedSolomonException( "IllegalArgumentException".to_owned(), )) @@ -722,13 +722,13 @@ impl ReedSolomonDecoder { if log_value > received.len() as i32 - 1 { return Ok(()); } - let position: usize = received.len() - 1 - log_value as usize; + let position: isize = received.len() as isize - 1 - log_value as isize; if position < 0 { return Err(Exceptions::ReedSolomonException( "Bad error location".to_owned(), )); } - received[position] = GenericGF::addOrSubtract(received[position], errorMagnitudes[i]); + received[position as usize] = GenericGF::addOrSubtract(received[position as usize], errorMagnitudes[i]); } Ok(()) } @@ -774,7 +774,7 @@ impl ReedSolomonDecoder { let denominatorLeadingTerm = rLast.getCoefficient(rLast.getDegree()); let dltInverse = match self.field.inverse(denominatorLeadingTerm) { Ok(inv) => inv, - Err(err) => { + Err(_err) => { return Err(Exceptions::ReedSolomonException( "ArithmetricException".to_owned(), )) @@ -787,7 +787,7 @@ impl ReedSolomonDecoder { .multiply(r.getCoefficient(r.getDegree()), dltInverse); q = match q.addOrSubtract(&GenericGF::buildMonomial(self.field, degreeDiff, scale)) { Ok(res) => res, - Err(err) => { + Err(_err) => { return Err(Exceptions::ReedSolomonException( "IllegalArgumentException".to_owned(), )) @@ -795,14 +795,14 @@ impl ReedSolomonDecoder { }; r = match r.addOrSubtract(&match rLast.multiply_by_monomial(degreeDiff, scale) { Ok(res) => res, - Err(err) => { + Err(_err) => { return Err(Exceptions::ReedSolomonException( "IllegalArgumentException".to_owned(), )) } }) { Ok(res) => res, - Err(err) => { + Err(_err) => { return Err(Exceptions::ReedSolomonException( "IllegalArgumentException".to_owned(), )) @@ -812,7 +812,7 @@ impl ReedSolomonDecoder { t = match (match q.multiply(&tLast) { Ok(res) => res, - Err(err) => { + Err(_err) => { return Err(Exceptions::ReedSolomonException( "IllegalArgumentException".to_owned(), )) @@ -821,7 +821,7 @@ impl ReedSolomonDecoder { .addOrSubtract(&tLastLast) { Ok(res) => res, - Err(err) => { + Err(_err) => { return Err(Exceptions::ReedSolomonException( "IllegalArgumentException".to_owned(), )) @@ -845,7 +845,7 @@ impl ReedSolomonDecoder { let inverse = match self.field.inverse(sigmaTildeAtZero) { Ok(res) => res, - Err(err) => { + Err(_err) => { return Err(Exceptions::ReedSolomonException( "ArithmetricException".to_owned(), )) @@ -874,7 +874,7 @@ impl ReedSolomonDecoder { if errorLocator.evaluateAt(i) == 0 { result[e] = match self.field.inverse(i.try_into().unwrap()) { Ok(res) => res.try_into().unwrap(), - Err(err) => { + Err(_err) => { return Err(Exceptions::ReedSolomonException( "ArithmetricException".to_owned(), )) diff --git a/src/qrcode/decoder/decoder.rs b/src/qrcode/decoder/decoder.rs index 98e3403..f260d11 100644 --- a/src/qrcode/decoder/decoder.rs +++ b/src/qrcode/decoder/decoder.rs @@ -197,8 +197,11 @@ fn correctErrors(codewordBytes: &mut [u8], numDataCodewords: usize) -> Result<() // for (int i = 0; i < numCodewords; i++) { codewordsInts[i] = codewordBytes[i]; // & 0xFF; } + + let mut sending_code_words : Vec = codewordsInts.iter().map(|x| *x as i32).collect(); + if let Err(e) = rsDecoder.decode( - &mut codewordsInts.iter().map(|x| *x as i32).collect(), + &mut sending_code_words, (codewordBytes.len() - numDataCodewords) as i32, ) { if let Exceptions::ReedSolomonException(error_str) = e { @@ -210,7 +213,7 @@ fn correctErrors(codewordBytes: &mut [u8], numDataCodewords: usize) -> Result<() // We don't care about errors in the error-correction codewords for i in 0..numDataCodewords { // for (int i = 0; i < numDataCodewords; i++) { - codewordBytes[i] = codewordsInts[i]; + codewordBytes[i] = sending_code_words[i] as u8; } Ok(()) } diff --git a/src/qrcode/detector/alignment_pattern_finder.rs b/src/qrcode/detector/alignment_pattern_finder.rs index 5ec5c39..033abd1 100644 --- a/src/qrcode/detector/alignment_pattern_finder.rs +++ b/src/qrcode/detector/alignment_pattern_finder.rs @@ -290,7 +290,7 @@ impl AlignmentPatternFinder { let centerJ = Self::centerFromEnd(stateCount, j); let centerI = self.crossCheckVertical( i, - centerJ.floor() as u32, + centerJ.round() as u32, 2 * stateCount[1], stateCountTotal, ); diff --git a/src/qrcode/detector/detector.rs b/src/qrcode/detector/detector.rs index d262973..e1fe7dc 100644 --- a/src/qrcode/detector/detector.rs +++ b/src/qrcode/detector/detector.rs @@ -462,13 +462,13 @@ impl Detector { // Look for an alignment pattern (3 modules in size) around where it // should be let allowance = (allowanceFactor * overallEstModuleSize) as u32; - let alignmentAreaLeftX = 0.max(estAlignmentX - allowance); + let alignmentAreaLeftX = 0.max(estAlignmentX as i32- allowance as i32) as u32; let alignmentAreaRightX = (self.image.getWidth() - 1).min(estAlignmentX + allowance); if ((alignmentAreaRightX - alignmentAreaLeftX) as f32) < overallEstModuleSize * 3.0 { return Err(Exceptions::NotFoundException("not found".to_owned())); } - let alignmentAreaTopY = 0.max(estAlignmentY - allowance); + let alignmentAreaTopY = 0.max(estAlignmentY as i32 - allowance as i32) as u32; let alignmentAreaBottomY = (self.image.getHeight() - 1).min(estAlignmentY + allowance); if alignmentAreaBottomY - alignmentAreaTopY < overallEstModuleSize as u32 * 3 { return Err(Exceptions::NotFoundException("not found".to_owned())); diff --git a/src/qrcode/detector/finder_pattern.rs b/src/qrcode/detector/finder_pattern.rs index a79c2ad..487eed9 100644 --- a/src/qrcode/detector/finder_pattern.rs +++ b/src/qrcode/detector/finder_pattern.rs @@ -92,7 +92,7 @@ impl FinderPattern { combinedX, combinedY, combinedModuleSize, - combinedCount.floor() as usize, + combinedCount.round() as usize, ) } } diff --git a/src/qrcode/detector/finder_pattern_finder.rs b/src/qrcode/detector/finder_pattern_finder.rs index 00261a1..4edd1dc 100755 --- a/src/qrcode/detector/finder_pattern_finder.rs +++ b/src/qrcode/detector/finder_pattern_finder.rs @@ -135,7 +135,7 @@ impl FinderPatternFinder { // Skip by rowSkip, but back off by stateCount[2] (size of last center // of pattern we saw) to be conservative, and also back off by iSkip which // is about to be re-added - i += rowSkip - stateCount[2] - iSkip; + i += (rowSkip as i32 - stateCount[2] as i32 - iSkip as i32).max(0) as u32; j = maxJ - 1; } } @@ -620,7 +620,7 @@ impl FinderPatternFinder { return (((fnp.getX() - center.getX().abs()) - (fnp.getY() - center.getY()).abs()) / 2.0) - .floor() as u32; + .round() as u32; } } } diff --git a/tests/QRCodeBlackBox1TestCase.rs b/tests/QRCodeBlackBox1TestCase.rs index 8c3c240..cfd3c5e 100644 --- a/tests/QRCodeBlackBox1TestCase.rs +++ b/tests/QRCodeBlackBox1TestCase.rs @@ -38,9 +38,8 @@ fn QRCodeBlackBox1TestCase() { tester.testBlackBox(); } -// 15 - 180 should pass -// 19 - 180 should pass -// 20 - 90 should pass +// 16 - 0 +// 19 - 0 // TEST CASE 15 FAILING AT allignment patter finder! (line 88) FROM detector 486