From eaacf3192328c33efb887f0d233a5dc8daed3d55 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Mon, 19 Dec 2022 17:49:52 -0600 Subject: [PATCH] cargo fmt --- src/pdf417/decoder/barcode_metadata.rs | 64 ++-- src/pdf417/decoder/barcode_value.rs | 91 +++--- src/pdf417/decoder/bounding_box.rs | 298 ++++++++++-------- src/pdf417/decoder/codeword.rs | 95 +++--- src/pdf417/decoder/detection_result_column.rs | 104 +++--- src/pdf417/decoder/mod.rs | 2 +- .../decoder/pdf_417_codeword_decoder.rs | 158 +++++----- 7 files changed, 418 insertions(+), 394 deletions(-) diff --git a/src/pdf417/decoder/barcode_metadata.rs b/src/pdf417/decoder/barcode_metadata.rs index e03462d..4b03d9b 100644 --- a/src/pdf417/decoder/barcode_metadata.rs +++ b/src/pdf417/decoder/barcode_metadata.rs @@ -18,43 +18,45 @@ * @author Guenther Grau */ pub struct BarcodeMetadata { - - columnCount: u32, - errorCorrectionLevel: u32, - rowCountUpperPart: u32, - rowCountLowerPart: u32, - rowCount: u32, + columnCount: u32, + errorCorrectionLevel: u32, + rowCountUpperPart: u32, + rowCountLowerPart: u32, + rowCount: u32, } impl BarcodeMetadata { - - pub fn new( columnCount:u32, rowCountUpperPart:u32, rowCountLowerPart:u32, errorCorrectionLevel:u32) -> Self{ - Self { - columnCount, - errorCorrectionLevel, - rowCountUpperPart, - rowCountLowerPart, - rowCount: rowCountUpperPart + rowCountLowerPart, + pub fn new( + columnCount: u32, + rowCountUpperPart: u32, + rowCountLowerPart: u32, + errorCorrectionLevel: u32, + ) -> Self { + Self { + columnCount, + errorCorrectionLevel, + rowCountUpperPart, + rowCountLowerPart, + rowCount: rowCountUpperPart + rowCountLowerPart, + } } - } - pub fn getColumnCount(&self) -> u32{ - self. columnCount - } + pub fn getColumnCount(&self) -> u32 { + self.columnCount + } - pub fn getErrorCorrectionLevel(&self) -> u32{ - self. errorCorrectionLevel - } + pub fn getErrorCorrectionLevel(&self) -> u32 { + self.errorCorrectionLevel + } - pub fn getRowCount(&self) -> u32 { - self. rowCount - } + pub fn getRowCount(&self) -> u32 { + self.rowCount + } - pub fn getRowCountUpperPart(&self) -> u32{ - self. rowCountUpperPart - } - - pub fn getRowCountLowerPart(&self) -> u32 { - self. rowCountLowerPart - } + pub fn getRowCountUpperPart(&self) -> u32 { + self.rowCountUpperPart + } + pub fn getRowCountLowerPart(&self) -> u32 { + self.rowCountLowerPart + } } diff --git a/src/pdf417/decoder/barcode_value.rs b/src/pdf417/decoder/barcode_value.rs index 6f85d7d..b9c8e30 100644 --- a/src/pdf417/decoder/barcode_value.rs +++ b/src/pdf417/decoder/barcode_value.rs @@ -19,60 +19,59 @@ use std::collections::HashMap; /** * @author Guenther Grau */ -pub struct BarcodeValue(HashMap); - // private final Map values = new HashMap<>(); +pub struct BarcodeValue(HashMap); +// private final Map values = new HashMap<>(); - impl BarcodeValue { -pub fn new() -> Self { - Self::default() -} - - /** - * Add an occurrence of a value - */ - pub fn setValue(&mut self, value:u32) { - let mut confidence = if let Some(value) = self.0.get(&value) { - *value - }else { - 0 - }; - confidence+=1; - self.0.insert(value, confidence); - } - - /** - * Determines the maximum occurrence of a set value and returns all values which were set with this occurrence. - * @return an array of int, containing the values with the highest occurrence, or null, if no value was set - */ - pub fn getValue(&self) -> Vec { - let mut maxConfidence = -1_i32; - let mut result = Vec::new(); - for (key,value) in &self.0 { - // for (Entry entry : values.entrySet()) { - if *value as i32 > maxConfidence { - maxConfidence = *value as i32; - result.clear(); - result.push(*key); - } else if *value as i32 == maxConfidence { - result.push(*key); - } +impl BarcodeValue { + pub fn new() -> Self { + Self::default() } - result - } + /** + * Add an occurrence of a value + */ + pub fn setValue(&mut self, value: u32) { + let mut confidence = if let Some(value) = self.0.get(&value) { + *value + } else { + 0 + }; + confidence += 1; + self.0.insert(value, confidence); + } - pub fn getConfidence(&self, value:u32) -> u32{ - if let Some(v) = self.0.get(&value) { - *v - }else { - 0 - } - } + /** + * Determines the maximum occurrence of a set value and returns all values which were set with this occurrence. + * @return an array of int, containing the values with the highest occurrence, or null, if no value was set + */ + pub fn getValue(&self) -> Vec { + let mut maxConfidence = -1_i32; + let mut result = Vec::new(); + for (key, value) in &self.0 { + // for (Entry entry : values.entrySet()) { + if *value as i32 > maxConfidence { + maxConfidence = *value as i32; + result.clear(); + result.push(*key); + } else if *value as i32 == maxConfidence { + result.push(*key); + } + } + result + } + + pub fn getConfidence(&self, value: u32) -> u32 { + if let Some(v) = self.0.get(&value) { + *v + } else { + 0 + } + } } impl Default for BarcodeValue { fn default() -> Self { Self(Default::default()) } -} \ No newline at end of file +} diff --git a/src/pdf417/decoder/bounding_box.rs b/src/pdf417/decoder/bounding_box.rs index 8e9870c..e7592a4 100644 --- a/src/pdf417/decoder/bounding_box.rs +++ b/src/pdf417/decoder/bounding_box.rs @@ -14,166 +14,190 @@ * limitations under the License. */ -use crate::{common::BitMatrix, RXingResultPoint, Exceptions, ResultPoint}; +use crate::{common::BitMatrix, Exceptions, RXingResultPoint, ResultPoint}; /** * @author Guenther Grau */ #[derive(Clone)] pub struct BoundingBox<'a> { - - image:&'a BitMatrix, - topLeft:RXingResultPoint, - bottomLeft:RXingResultPoint, - topRight:RXingResultPoint, - bottomRight:RXingResultPoint, - minX:u32, - maxX:u32, - minY:u32, - maxY:u32, + image: &'a BitMatrix, + topLeft: RXingResultPoint, + bottomLeft: RXingResultPoint, + topRight: RXingResultPoint, + bottomRight: RXingResultPoint, + minX: u32, + maxX: u32, + minY: u32, + maxY: u32, } impl<'a> BoundingBox<'_> { + pub fn new( + image: &'a BitMatrix, + topLeft: Option, + bottomLeft: Option, + topRight: Option, + bottomRight: Option, + ) -> Result, Exceptions> { + let leftUnspecified = topLeft.is_none() || bottomLeft.is_none(); + let rightUnspecified = topRight.is_none() || bottomRight.is_none(); + if leftUnspecified && rightUnspecified { + return Err(Exceptions::NotFoundException("".to_owned())); + } - pub fn new( image:&'a BitMatrix, - topLeft:Option, - bottomLeft:Option, - topRight:Option, - bottomRight:Option) -> Result,Exceptions> { - let leftUnspecified = topLeft.is_none() || bottomLeft .is_none(); - let rightUnspecified = topRight .is_none() || bottomRight .is_none(); - if leftUnspecified && rightUnspecified { - return Err(Exceptions::NotFoundException("".to_owned())) + let newTopLeft; + let newBottomLeft; + let newTopRight; + let newBottomRight; + + if leftUnspecified { + newTopRight = topRight.unwrap(); + newBottomRight = bottomRight.unwrap(); + newTopLeft = RXingResultPoint::new(0.0, newTopRight.getY()); + newBottomLeft = RXingResultPoint::new(0.0, newBottomRight.getY()); + } else if rightUnspecified { + newTopLeft = topLeft.unwrap(); + newBottomLeft = bottomLeft.unwrap(); + newTopRight = RXingResultPoint::new(image.getWidth() as f32 - 1.0, newTopLeft.getY()); + newBottomRight = + RXingResultPoint::new(image.getWidth() as f32 - 1.0, newBottomLeft.getY()); + } else { + newTopLeft = topLeft.unwrap(); + newTopRight = topRight.unwrap(); + newBottomLeft = bottomLeft.unwrap(); + newBottomRight = bottomRight.unwrap(); + } + + Ok(BoundingBox { + image, + minX: newTopLeft.getX().min(newBottomLeft.getX()) as u32, + maxX: newTopRight.getX().max(newBottomRight.getX()) as u32, + minY: newTopLeft.getY().min(newTopRight.getY()) as u32, + maxY: newBottomLeft.getY().max(newBottomRight.getY()) as u32, + topLeft: newTopLeft, + bottomLeft: newBottomLeft, + topRight: newTopRight, + bottomRight: newBottomRight, + }) } - let newTopLeft; - let newBottomLeft; - let newTopRight; - let newBottomRight; - - if leftUnspecified { - newTopRight = topRight.unwrap(); - newBottomRight = bottomRight.unwrap(); - newTopLeft = RXingResultPoint::new(0.0, newTopRight.getY()); - newBottomLeft = RXingResultPoint::new(0.0, newBottomRight.getY()); - } else if rightUnspecified { - newTopLeft = topLeft.unwrap(); - newBottomLeft = bottomLeft.unwrap(); - newTopRight = RXingResultPoint::new(image.getWidth() as f32 - 1.0, newTopLeft.getY()); - newBottomRight = RXingResultPoint::new(image.getWidth() as f32 - 1.0, newBottomLeft.getY()); - }else { - newTopLeft = topLeft.unwrap(); - newTopRight = topRight.unwrap(); - newBottomLeft = bottomLeft.unwrap(); - newBottomRight = bottomRight.unwrap(); + pub fn from_other(boundingBox: &'a BoundingBox) -> BoundingBox<'a> { + BoundingBox { + image: boundingBox.image, + topLeft: boundingBox.topLeft, + bottomLeft: boundingBox.bottomLeft, + topRight: boundingBox.topRight, + bottomRight: boundingBox.bottomRight, + minX: boundingBox.minX, + maxX: boundingBox.maxX, + minY: boundingBox.minY, + maxY: boundingBox.maxY, + } } - Ok(BoundingBox { - image, - minX: newTopLeft.getX().min(newBottomLeft.getX()) as u32, - maxX: newTopRight.getX().max(newBottomRight.getX()) as u32, - minY: newTopLeft.getY().min(newTopRight.getY()) as u32, - maxY: newBottomLeft.getY().max(newBottomRight.getY()) as u32, - topLeft: newTopLeft, - bottomLeft: newBottomLeft, - topRight: newTopRight, - bottomRight: newBottomRight, - }) - } + pub fn merge( + leftBox: Option<&'a BoundingBox>, + rightBox: Option<&'a BoundingBox>, + ) -> Result, Exceptions> { + if leftBox.is_none() { + return Ok(rightBox.unwrap().clone()); + } + if rightBox.is_none() { + return Ok(leftBox.unwrap().clone()); + } + let leftBox = leftBox.unwrap(); + let rightBox = rightBox.unwrap(); - pub fn from_other( boundingBox:&'a BoundingBox) -> BoundingBox<'a> { - BoundingBox { - image: boundingBox.image, - topLeft: boundingBox.topLeft, - bottomLeft: boundingBox.bottomLeft, - topRight: boundingBox.topRight, - bottomRight: boundingBox.bottomRight, - minX: boundingBox.minX, - maxX: boundingBox.maxX, - minY: boundingBox.minY, - maxY: boundingBox.maxY, - } - } - - pub fn merge( leftBox:Option<&'a BoundingBox>, rightBox:Option<&'a BoundingBox>) -> Result,Exceptions> { - if leftBox.is_none() { - return Ok(rightBox.unwrap().clone()) - } - if rightBox .is_none() { - return Ok(leftBox.unwrap().clone()) - } - let leftBox = leftBox.unwrap(); - let rightBox = rightBox.unwrap(); - - BoundingBox::new(leftBox.image, Some(leftBox.topLeft), Some(leftBox.bottomLeft), Some(rightBox.topRight), Some(rightBox.bottomRight)) - } - - pub fn addMissingRows(&self, missingStartRows:u32, missingEndRows:u32, isLeft:bool) -> Result { - let mut newTopLeft = self.topLeft; - let mut newBottomLeft = self.bottomLeft; - let mut newTopRight = self.topRight; - let mut newBottomRight = self.bottomRight; - - if missingStartRows > 0 { - let top = if isLeft {self.topLeft} else {self.topRight}; - let mut newMinY = top.getY() - missingStartRows as f32; - if newMinY < 0.0 { - newMinY = 0.0; - } - let newTop = RXingResultPoint::new(top.getX(), newMinY); - if isLeft { - newTopLeft = newTop; - } else { - newTopRight = newTop; - } + BoundingBox::new( + leftBox.image, + Some(leftBox.topLeft), + Some(leftBox.bottomLeft), + Some(rightBox.topRight), + Some(rightBox.bottomRight), + ) } - if missingEndRows > 0 { - let bottom = if isLeft { self.bottomLeft} else {self.bottomRight}; - let mut newMaxY = bottom.getY() as u32 + missingEndRows; - if newMaxY >= self.image.getHeight() { - newMaxY = self.image.getHeight() - 1; - } - let newBottom = RXingResultPoint::new(bottom.getX(), newMaxY as f32); - if isLeft { - newBottomLeft = newBottom; - } else { - newBottomRight = newBottom; - } + pub fn addMissingRows( + &self, + missingStartRows: u32, + missingEndRows: u32, + isLeft: bool, + ) -> Result { + let mut newTopLeft = self.topLeft; + let mut newBottomLeft = self.bottomLeft; + let mut newTopRight = self.topRight; + let mut newBottomRight = self.bottomRight; + + if missingStartRows > 0 { + let top = if isLeft { self.topLeft } else { self.topRight }; + let mut newMinY = top.getY() - missingStartRows as f32; + if newMinY < 0.0 { + newMinY = 0.0; + } + let newTop = RXingResultPoint::new(top.getX(), newMinY); + if isLeft { + newTopLeft = newTop; + } else { + newTopRight = newTop; + } + } + + if missingEndRows > 0 { + let bottom = if isLeft { + self.bottomLeft + } else { + self.bottomRight + }; + let mut newMaxY = bottom.getY() as u32 + missingEndRows; + if newMaxY >= self.image.getHeight() { + newMaxY = self.image.getHeight() - 1; + } + let newBottom = RXingResultPoint::new(bottom.getX(), newMaxY as f32); + if isLeft { + newBottomLeft = newBottom; + } else { + newBottomRight = newBottom; + } + } + + BoundingBox::new( + self.image, + Some(newTopLeft), + Some(newBottomLeft), + Some(newTopRight), + Some(newBottomRight), + ) } - BoundingBox::new(self.image, Some(newTopLeft), Some(newBottomLeft), Some(newTopRight), Some(newBottomRight)) - } + pub fn getMinX(&self) -> u32 { + self.minX + } - pub fn getMinX(&self) -> u32{ - self. minX - } + pub fn getMaxX(&self) -> u32 { + self.maxX + } - pub fn getMaxX(&self) -> u32{ - self. maxX - } + pub fn getMinY(&self) -> u32 { + self.minY + } - pub fn getMinY(&self) -> u32{ - self. minY - } + pub fn getMaxY(&self) -> u32 { + self.maxY + } - pub fn getMaxY(&self) -> u32{ - self. maxY - } + pub fn getTopLeft(&self) -> &RXingResultPoint { + &self.topLeft + } - pub fn getTopLeft(&self) -> &RXingResultPoint{ - &self. topLeft - } + pub fn getTopRight(&self) -> &RXingResultPoint { + &self.topRight + } - pub fn getTopRight(&self) -> &RXingResultPoint{ - &self. topRight - } - - pub fn getBottomLeft(&self) -> &RXingResultPoint{ - &self. bottomLeft - } - - pub fn getBottomRight(&self) -> &RXingResultPoint{ - &self. bottomRight - } + pub fn getBottomLeft(&self) -> &RXingResultPoint { + &self.bottomLeft + } + pub fn getBottomRight(&self) -> &RXingResultPoint { + &self.bottomRight + } } diff --git a/src/pdf417/decoder/codeword.rs b/src/pdf417/decoder/codeword.rs index 7626dd0..9ac7ff7 100644 --- a/src/pdf417/decoder/codeword.rs +++ b/src/pdf417/decoder/codeword.rs @@ -16,77 +16,74 @@ use std::fmt::Display; - const BARCODE_ROW_UNKNOWN : i32 = -1; +const BARCODE_ROW_UNKNOWN: i32 = -1; /** * @author Guenther Grau */ #[derive(Clone, Copy)] pub struct Codeword { - - startX:u32, - endX:u32, - bucket:u32, - value:u32, - rowNumber : i32, + startX: u32, + endX: u32, + bucket: u32, + value: u32, + rowNumber: i32, } impl Codeword { - - pub fn new( startX:u32, endX:u32, bucket:u32, value:u32)->Self { - Self { - startX, - endX, - bucket, - value, - rowNumber: BARCODE_ROW_UNKNOWN, + pub fn new(startX: u32, endX: u32, bucket: u32, value: u32) -> Self { + Self { + startX, + endX, + bucket, + value, + rowNumber: BARCODE_ROW_UNKNOWN, + } } - } - pub fn hasValidRowNumber(&self) -> bool{ - self.isValidRowNumber(self.rowNumber) - } + pub fn hasValidRowNumber(&self) -> bool { + self.isValidRowNumber(self.rowNumber) + } - pub fn isValidRowNumber(&self, rowNumber:i32) -> bool{ - rowNumber != BARCODE_ROW_UNKNOWN && self.bucket == (rowNumber as u32 % 3) * 3 - } + pub fn isValidRowNumber(&self, rowNumber: i32) -> bool { + rowNumber != BARCODE_ROW_UNKNOWN && self.bucket == (rowNumber as u32 % 3) * 3 + } - pub fn setRowNumberAsRowIndicatorColumn(&mut self) { - self.rowNumber =( (self.value / 30) * 3 + self.bucket / 3) as i32; - } + pub fn setRowNumberAsRowIndicatorColumn(&mut self) { + self.rowNumber = ((self.value / 30) * 3 + self.bucket / 3) as i32; + } - pub fn getWidth(&self) -> u32{ - self.endX - self.startX - } + pub fn getWidth(&self) -> u32 { + self.endX - self.startX + } - pub fn getStartX(&self) -> u32 { - self. startX - } + pub fn getStartX(&self) -> u32 { + self.startX + } - pub fn getEndX(&self) -> u32{ - self. endX - } + pub fn getEndX(&self) -> u32 { + self.endX + } - pub fn getBucket(&self) -> u32 { - self. bucket - } + pub fn getBucket(&self) -> u32 { + self.bucket + } - pub fn getValue(&self) -> u32 { - self.value - } + pub fn getValue(&self) -> u32 { + self.value + } - pub fn getRowNumber(&self) -> i32{ - self. rowNumber - } - - pub fn setRowNumber(&mut self, rowNumber:i32) { - self.rowNumber = rowNumber; - } + pub fn getRowNumber(&self) -> i32 { + self.rowNumber + } + pub fn setRowNumber(&mut self, rowNumber: i32) { + self.rowNumber = rowNumber; + } } impl Display for Codeword { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f,"{}|{}", self.rowNumber, self.value) + write!(f, "{}|{}", self.rowNumber, self.value) } -} \ No newline at end of file +} diff --git a/src/pdf417/decoder/detection_result_column.rs b/src/pdf417/decoder/detection_result_column.rs index e614d1c..a78e1be 100644 --- a/src/pdf417/decoder/detection_result_column.rs +++ b/src/pdf417/decoder/detection_result_column.rs @@ -18,75 +18,71 @@ use std::fmt::Display; use super::{BoundingBox, Codeword}; - const MAX_NEARBY_DISTANCE :u32 = 5; +const MAX_NEARBY_DISTANCE: u32 = 5; /** * @author Guenther Grau */ pub struct DetectionRXingResultColumn<'a> { - boundingBox: BoundingBox<'a>, - codewords:Vec>, + boundingBox: BoundingBox<'a>, + codewords: Vec>, } impl<'a> DetectionRXingResultColumn<'_> { - - pub fn new( boundingBox:&'a BoundingBox) -> DetectionRXingResultColumn<'a> { - DetectionRXingResultColumn { - boundingBox: BoundingBox::from_other(boundingBox), - codewords: vec![None;(boundingBox.getMaxY() - boundingBox.getMinY() + 1) as usize], - } - // this.boundingBox = new BoundingBox(boundingBox); - // codewords = new Codeword[boundingBox.getMaxY() - boundingBox.getMinY() + 1]; - } - - pub fn getCodewordNearby(&self, imageRow:u32) -> &Option { - let mut codeword = self.getCodeword(imageRow); - if codeword.is_some() { - return codeword; - } - for i in 1..MAX_NEARBY_DISTANCE as usize{ - // for (int i = 1; i < MAX_NEARBY_DISTANCE; i++) { - let mut nearImageRow = self.imageRowToCodewordIndex(imageRow) - i; - if nearImageRow >= 0 { - codeword = &self.codewords[nearImageRow]; - if codeword.is_some() { - return codeword; + pub fn new(boundingBox: &'a BoundingBox) -> DetectionRXingResultColumn<'a> { + DetectionRXingResultColumn { + boundingBox: BoundingBox::from_other(boundingBox), + codewords: vec![None; (boundingBox.getMaxY() - boundingBox.getMinY() + 1) as usize], } - } - nearImageRow = self.imageRowToCodewordIndex(imageRow) + i ; - if nearImageRow < self.codewords.len() { - codeword = &self.codewords[nearImageRow]; - if codeword.is_some() { - return codeword; - } - } + // this.boundingBox = new BoundingBox(boundingBox); + // codewords = new Codeword[boundingBox.getMaxY() - boundingBox.getMinY() + 1]; } - &None - } - pub fn imageRowToCodewordIndex(&self, imageRow:u32) -> usize{ - (imageRow - self.boundingBox.getMinY()) as usize - } + pub fn getCodewordNearby(&self, imageRow: u32) -> &Option { + let mut codeword = self.getCodeword(imageRow); + if codeword.is_some() { + return codeword; + } + for i in 1..MAX_NEARBY_DISTANCE as usize { + // for (int i = 1; i < MAX_NEARBY_DISTANCE; i++) { + let mut nearImageRow = self.imageRowToCodewordIndex(imageRow) - i; + if nearImageRow >= 0 { + codeword = &self.codewords[nearImageRow]; + if codeword.is_some() { + return codeword; + } + } + nearImageRow = self.imageRowToCodewordIndex(imageRow) + i; + if nearImageRow < self.codewords.len() { + codeword = &self.codewords[nearImageRow]; + if codeword.is_some() { + return codeword; + } + } + } + &None + } - pub fn setCodeword(&mut self, imageRow:u32, codeword:Codeword) { - let pos = self.imageRowToCodewordIndex(imageRow); - self.codewords[pos] = Some(codeword); - } + pub fn imageRowToCodewordIndex(&self, imageRow: u32) -> usize { + (imageRow - self.boundingBox.getMinY()) as usize + } - pub fn getCodeword(&self, imageRow:u32) -> &Option{ - &self. codewords[self.imageRowToCodewordIndex(imageRow)] - } - - pub fn getBoundingBox(&self) -> &BoundingBox { - &self. boundingBox - } - - pub fn getCodewords(&self) -> &[Option] { - &self.codewords - } + pub fn setCodeword(&mut self, imageRow: u32, codeword: Codeword) { + let pos = self.imageRowToCodewordIndex(imageRow); + self.codewords[pos] = Some(codeword); + } + pub fn getCodeword(&self, imageRow: u32) -> &Option { + &self.codewords[self.imageRowToCodewordIndex(imageRow)] + } + pub fn getBoundingBox(&self) -> &BoundingBox { + &self.boundingBox + } + pub fn getCodewords(&self) -> &[Option] { + &self.codewords + } } impl Display for DetectionRXingResultColumn<'_> { @@ -108,4 +104,4 @@ impl Display for DetectionRXingResultColumn<'_> { // return formatter.toString(); // } // } -} \ No newline at end of file +} diff --git a/src/pdf417/decoder/mod.rs b/src/pdf417/decoder/mod.rs index 44ce960..3322731 100644 --- a/src/pdf417/decoder/mod.rs +++ b/src/pdf417/decoder/mod.rs @@ -15,4 +15,4 @@ pub use bounding_box::*; mod detection_result_column; pub use detection_result_column::*; -pub mod pdf_417_codeword_decoder; \ No newline at end of file +pub mod pdf_417_codeword_decoder; diff --git a/src/pdf417/decoder/pdf_417_codeword_decoder.rs b/src/pdf417/decoder/pdf_417_codeword_decoder.rs index eea42b5..71442e9 100644 --- a/src/pdf417/decoder/pdf_417_codeword_decoder.rs +++ b/src/pdf417/decoder/pdf_417_codeword_decoder.rs @@ -21,108 +21,114 @@ use crate::pdf417::pdf_417_common; * @author creatale GmbH (christoph.schulz@creatale.de) */ - // const RATIOS_TABLE :[[f32]] = - // [[0.0;pdf_417_common::SYMBOL_TABLE.len()];pdf_417_common::BARS_IN_MODULE]; +// const RATIOS_TABLE :[[f32]] = +// [[0.0;pdf_417_common::SYMBOL_TABLE.len()];pdf_417_common::BARS_IN_MODULE]; - const RATIOS_TABLE : [[f64;pdf_417_common::BARS_IN_MODULE as usize];2787] = { - let mut table = [[0.0;pdf_417_common::BARS_IN_MODULE as usize];pdf_417_common::SYMBOL_TABLE.len()]; +const RATIOS_TABLE: [[f64; pdf_417_common::BARS_IN_MODULE as usize]; 2787] = { + let mut table = + [[0.0; pdf_417_common::BARS_IN_MODULE as usize]; pdf_417_common::SYMBOL_TABLE.len()]; // Pre-computes the symbol ratio table. let mut i = 0_usize; while i < pdf_417_common::SYMBOL_TABLE.len() { - // for (int i = 0; i < PDF417Common.SYMBOL_TABLE.length; i++) { - let mut currentSymbol = pdf_417_common::SYMBOL_TABLE[i]; - let mut currentBit = currentSymbol & 0x1; - let mut j = 0_usize; - while j < pdf_417_common::BARS_IN_MODULE as usize { - // for (int j = 0; j < pdf_417_common::BARS_IN_MODULE; j++) { - let mut size = 0.0; - while (currentSymbol & 0x1) == currentBit { - size += 1.0; - currentSymbol >>= 1; + // for (int i = 0; i < PDF417Common.SYMBOL_TABLE.length; i++) { + let mut currentSymbol = pdf_417_common::SYMBOL_TABLE[i]; + let mut currentBit = currentSymbol & 0x1; + let mut j = 0_usize; + while j < pdf_417_common::BARS_IN_MODULE as usize { + // for (int j = 0; j < pdf_417_common::BARS_IN_MODULE; j++) { + let mut size = 0.0; + while (currentSymbol & 0x1) == currentBit { + size += 1.0; + currentSymbol >>= 1; + } + currentBit = currentSymbol & 0x1; + table[i][pdf_417_common::BARS_IN_MODULE as usize - j - 1] = + size / pdf_417_common::MODULES_IN_CODEWORD as f64; + + j += 1; } - currentBit = currentSymbol & 0x1; - table[i][pdf_417_common::BARS_IN_MODULE as usize - j - 1] = size / pdf_417_common::MODULES_IN_CODEWORD as f64; - j+=1; - } - - i+=1; + i += 1; } table - }; +}; - pub fn getDecodedValue( moduleBitCount:&[u32]) -> u32{ +pub fn getDecodedValue(moduleBitCount: &[u32]) -> u32 { let decodedValue = getDecodedCodewordValue(&sampleBitCounts(moduleBitCount)); if decodedValue != -1 { - return decodedValue as u32; + return decodedValue as u32; } - getClosestDecodedValue(moduleBitCount) as u32 - } + getClosestDecodedValue(moduleBitCount) as u32 +} - fn sampleBitCounts( moduleBitCount:&[u32]) -> [u32;8]{ - let bitCountSum:u32 = moduleBitCount.iter().sum(); //MathUtils.sum(moduleBitCount); - let mut result = [0;pdf_417_common::BARS_IN_MODULE as usize]; +fn sampleBitCounts(moduleBitCount: &[u32]) -> [u32; 8] { + let bitCountSum: u32 = moduleBitCount.iter().sum(); //MathUtils.sum(moduleBitCount); + let mut result = [0; pdf_417_common::BARS_IN_MODULE as usize]; let mut bitCountIndex = 0; let mut sumPreviousBits = 0; for i in 0..pdf_417_common::MODULES_IN_CODEWORD { - // for (int i = 0; i < PDF417Common.MODULES_IN_CODEWORD; i++) { - let sampleIndex:f32 = - bitCountSum as f32/ (2 * pdf_417_common::MODULES_IN_CODEWORD) as f32 + - (i * bitCountSum) as f32 / pdf_417_common::MODULES_IN_CODEWORD as f32; - if sumPreviousBits as f32 + moduleBitCount[bitCountIndex] as f32 <= sampleIndex { - sumPreviousBits += moduleBitCount[bitCountIndex]; - bitCountIndex+=1; - } - result[bitCountIndex]+=1; + // for (int i = 0; i < PDF417Common.MODULES_IN_CODEWORD; i++) { + let sampleIndex: f32 = bitCountSum as f32 + / (2 * pdf_417_common::MODULES_IN_CODEWORD) as f32 + + (i * bitCountSum) as f32 / pdf_417_common::MODULES_IN_CODEWORD as f32; + if sumPreviousBits as f32 + moduleBitCount[bitCountIndex] as f32 <= sampleIndex { + sumPreviousBits += moduleBitCount[bitCountIndex]; + bitCountIndex += 1; + } + result[bitCountIndex] += 1; } - result - } + result +} - fn getDecodedCodewordValue( moduleBitCount:&[u32]) -> i32{ +fn getDecodedCodewordValue(moduleBitCount: &[u32]) -> i32 { let decodedValue = getBitValue(moduleBitCount); - if pdf_417_common::getCodeword(decodedValue as u32) == -1 {-1} else {decodedValue} - } - - fn getBitValue( moduleBitCount:&[u32]) -> i32{ - let mut result : u64 = 0; - for i in 0..moduleBitCount.len() { - // for (int i = 0; i < moduleBitCount.length; i++) { - for _bit in 0..moduleBitCount[i] { - // for (int bit = 0; bit < moduleBitCount[i]; bit++) { - result = (result << 1) | (if i % 2 == 0 {1} else {0}); - } + if pdf_417_common::getCodeword(decodedValue as u32) == -1 { + -1 + } else { + decodedValue } - result as i32 - } +} - fn getClosestDecodedValue( moduleBitCount:&[u32]) -> i32{ - let bitCountSum : u32= moduleBitCount.iter().sum();//MathUtils.sum(moduleBitCount); - let mut bitCountRatios = [0.0;pdf_417_common::BARS_IN_MODULE as usize]; +fn getBitValue(moduleBitCount: &[u32]) -> i32 { + let mut result: u64 = 0; + for i in 0..moduleBitCount.len() { + // for (int i = 0; i < moduleBitCount.length; i++) { + for _bit in 0..moduleBitCount[i] { + // for (int bit = 0; bit < moduleBitCount[i]; bit++) { + result = (result << 1) | (if i % 2 == 0 { 1 } else { 0 }); + } + } + result as i32 +} + +fn getClosestDecodedValue(moduleBitCount: &[u32]) -> i32 { + let bitCountSum: u32 = moduleBitCount.iter().sum(); //MathUtils.sum(moduleBitCount); + let mut bitCountRatios = [0.0; pdf_417_common::BARS_IN_MODULE as usize]; if bitCountSum > 1 { - for i in 0..bitCountRatios.len() { - // for (int i = 0; i < bitCountRatios.length; i++) { - bitCountRatios[i] = moduleBitCount[i] as f64 / bitCountSum as f64; - } + for i in 0..bitCountRatios.len() { + // for (int i = 0; i < bitCountRatios.length; i++) { + bitCountRatios[i] = moduleBitCount[i] as f64 / bitCountSum as f64; + } } let mut bestMatchError = f64::MAX; let mut bestMatch = -1_i32; for j in 0..RATIOS_TABLE.len() { - // for (int j = 0; j < RATIOS_TABLE.length; j++) { - let mut error = 0.0; - let ratioTableRow = RATIOS_TABLE[j]; - for k in 0..pdf_417_common::BARS_IN_MODULE as usize{ - // for (int k = 0; k < PDF417Common.BARS_IN_MODULE; k++) { - let diff = ratioTableRow[k] - bitCountRatios[k]; - error += diff * diff; - if error >= bestMatchError { - break; + // for (int j = 0; j < RATIOS_TABLE.length; j++) { + let mut error = 0.0; + let ratioTableRow = RATIOS_TABLE[j]; + for k in 0..pdf_417_common::BARS_IN_MODULE as usize { + // for (int k = 0; k < PDF417Common.BARS_IN_MODULE; k++) { + let diff = ratioTableRow[k] - bitCountRatios[k]; + error += diff * diff; + if error >= bestMatchError { + break; + } + } + if error < bestMatchError { + bestMatchError = error; + bestMatch = pdf_417_common::SYMBOL_TABLE[j] as i32; } - } - if error < bestMatchError { - bestMatchError = error; - bestMatch = pdf_417_common::SYMBOL_TABLE[j] as i32; - } } - bestMatch - } + bestMatch +}