Initial generics

This commit is contained in:
Steve Cook
2023-03-01 22:08:12 -05:00
parent 26325928e7
commit a9bc58108c
65 changed files with 1013 additions and 883 deletions

View File

@@ -33,8 +33,11 @@ pub trait AbstractRSSReaderTrait: OneDReader {
fn parseFinderValue(counters: &[u32], finderPatterns: &[[u32; 4]]) -> Result<u32> {
for (value, pattern) in finderPatterns.iter().enumerate() {
if one_d_reader::patternMatchVariance(counters, pattern, Self::MAX_INDIVIDUAL_VARIANCE)
< Self::MAX_AVG_VARIANCE
if one_d_reader::pattern_match_variance(
counters,
pattern,
Self::MAX_INDIVIDUAL_VARIANCE,
) < Self::MAX_AVG_VARIANCE
{
return Ok(value as u32);
}

View File

@@ -38,7 +38,7 @@ pub struct AI01392xDecoder<'a> {
impl AI01decoder for AI01392xDecoder<'_> {}
impl AbstractExpandedDecoder for AI01392xDecoder<'_> {
fn parseInformation(&mut self) -> Result<String> {
if self.information.getSize() < Self::HEADER_SIZE + Self::GTIN_SIZE as usize {
if self.information.get_size() < Self::HEADER_SIZE + Self::GTIN_SIZE as usize {
return Err(crate::Exceptions::NOT_FOUND);
}

View File

@@ -38,7 +38,7 @@ pub struct AI01393xDecoder<'a> {
impl AI01decoder for AI01393xDecoder<'_> {}
impl AbstractExpandedDecoder for AI01393xDecoder<'_> {
fn parseInformation(&mut self) -> Result<String> {
if self.information.getSize() < Self::HEADER_SIZE + Self::GTIN_SIZE as usize {
if self.information.get_size() < Self::HEADER_SIZE + Self::GTIN_SIZE as usize {
return Err(crate::Exceptions::NOT_FOUND);
}

View File

@@ -54,7 +54,7 @@ impl AI01weightDecoder for AI013x0x1xDecoder<'_> {
impl AI01decoder for AI013x0x1xDecoder<'_> {}
impl AbstractExpandedDecoder for AI013x0x1xDecoder<'_> {
fn parseInformation(&mut self) -> Result<String> {
if self.information.getSize()
if self.information.get_size()
!= Self::HEADER_SIZE + Self::GTIN_SIZE as usize + Self::WEIGHT_SIZE + Self::DATE_SIZE
{
return Err(crate::Exceptions::NOT_FOUND);

View File

@@ -50,7 +50,7 @@ impl AI01weightDecoder for AI013x0xDecoder<'_> {
}
impl AbstractExpandedDecoder for AI013x0xDecoder<'_> {
fn parseInformation(&mut self) -> Result<String> {
if self.information.getSize()
if self.information.get_size()
!= Self::HEADER_SIZE + Self::GTIN_SIZE as usize + Self::WEIGHT_SIZE
{
return Err(crate::Exceptions::NOT_FOUND);

View File

@@ -82,8 +82,8 @@ impl<'a> GeneralAppIdDecoder<'_> {
fn isStillNumeric(&self, pos: usize) -> bool {
// It's numeric if it still has 7 positions
// and one of the first 4 bits is "1".
if pos + 7 > self.information.getSize() {
return pos + 4 <= self.information.getSize();
if pos + 7 > self.information.get_size() {
return pos + 4 <= self.information.get_size();
}
for i in pos..pos + 3 {
@@ -97,17 +97,17 @@ impl<'a> GeneralAppIdDecoder<'_> {
}
fn decodeNumeric(&self, pos: usize) -> Result<DecodedNumeric> {
if pos + 7 > self.information.getSize() {
if pos + 7 > self.information.get_size() {
let numeric = self.extractNumericValueFromBitArray(pos, 4);
if numeric == 0 {
return DecodedNumeric::new(
self.information.getSize(),
self.information.get_size(),
DecodedNumeric::FNC1,
DecodedNumeric::FNC1,
);
}
return DecodedNumeric::new(
self.information.getSize(),
self.information.get_size(),
numeric - 1,
DecodedNumeric::FNC1,
);
@@ -263,10 +263,10 @@ impl<'a> GeneralAppIdDecoder<'_> {
self.current.incrementPosition(3);
self.current.setNumeric();
} else if self.isAlphaTo646ToAlphaLatch(self.current.getPosition()) {
if self.current.getPosition() + 5 < self.information.getSize() {
if self.current.getPosition() + 5 < self.information.get_size() {
self.current.incrementPosition(5);
} else {
self.current.setPosition(self.information.getSize());
self.current.setPosition(self.information.get_size());
}
self.current.setAlpha();
@@ -295,10 +295,10 @@ impl<'a> GeneralAppIdDecoder<'_> {
self.current.incrementPosition(3);
self.current.setNumeric();
} else if self.isAlphaTo646ToAlphaLatch(self.current.getPosition()) {
if self.current.getPosition() + 5 < self.information.getSize() {
if self.current.getPosition() + 5 < self.information.get_size() {
self.current.incrementPosition(5);
} else {
self.current.setPosition(self.information.getSize());
self.current.setPosition(self.information.get_size());
}
self.current.setIsoIec646();
@@ -308,7 +308,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
}
fn isStillIsoIec646(&self, pos: usize) -> bool {
if pos + 5 > self.information.getSize() {
if pos + 5 > self.information.get_size() {
return false;
}
@@ -317,7 +317,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
return true;
}
if pos + 7 > self.information.getSize() {
if pos + 7 > self.information.get_size() {
return false;
}
@@ -326,7 +326,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
return true;
}
if pos + 8 > self.information.getSize() {
if pos + 8 > self.information.get_size() {
return false;
}
@@ -394,7 +394,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
}
fn isStillAlpha(&self, pos: usize) -> bool {
if pos + 5 > self.information.getSize() {
if pos + 5 > self.information.get_size() {
return false;
}
@@ -404,7 +404,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
return true;
}
if pos + 6 > self.information.getSize() {
if pos + 6 > self.information.get_size() {
return false;
}
@@ -452,12 +452,12 @@ impl<'a> GeneralAppIdDecoder<'_> {
}
fn isAlphaTo646ToAlphaLatch(&self, pos: usize) -> bool {
if pos + 1 > self.information.getSize() {
if pos + 1 > self.information.get_size() {
return false;
}
let mut i = 0;
while i < 5 && i + pos < self.information.getSize() {
while i < 5 && i + pos < self.information.get_size() {
if i == 2 {
if !self.information.get(pos + 2) {
return false;
@@ -474,7 +474,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
fn isAlphaOr646ToNumericLatch(&self, pos: usize) -> bool {
// Next is alphanumeric if there are 3 positions and they are all zeros
if pos + 3 > self.information.getSize() {
if pos + 3 > self.information.get_size() {
return false;
}
@@ -490,12 +490,12 @@ impl<'a> GeneralAppIdDecoder<'_> {
fn isNumericToAlphaNumericLatch(&self, pos: usize) -> bool {
// Next is alphanumeric if there are 4 positions and they are all zeros, or
// if there is a subset of this just before the end of the symbol
if pos + 1 > self.information.getSize() {
if pos + 1 > self.information.get_size() {
return false;
}
let mut i = 0;
while i < 4 && i + pos < self.information.getSize() {
while i < 4 && i + pos < self.information.get_size() {
if self.information.get(pos + i) {
return false;
}

View File

@@ -177,8 +177,8 @@ fn assertCorrectImage2binary(fileName: &str, expected: &str) {
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
BufferedImageLuminanceSource::new(image),
))));
let rowNumber = binaryMap.getHeight() / 2;
let row = binaryMap.getBlackRow(rowNumber).expect("row");
let rowNumber = binaryMap.get_height() / 2;
let row = binaryMap.get_black_row(rowNumber).expect("row");
// let pairs = Vec::new();
// try {

View File

@@ -74,12 +74,12 @@ fn assertCorrectImage2result(fileName: &str, expected: ExpandedProductParsedRXin
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
BufferedImageLuminanceSource::new(image),
))));
let rowNumber = binaryMap.getHeight() / 2;
let row = binaryMap.getBlackRow(rowNumber).expect("get row");
let rowNumber = binaryMap.get_height() / 2;
let row = binaryMap.get_black_row(rowNumber).expect("get row");
let mut rssExpandedReader = RSSExpandedReader::new();
let theRXingResult = rssExpandedReader
.decodeRow(rowNumber as u32, &row, &HashMap::new())
.decode_row(rowNumber as u32, &row, &HashMap::new())
.expect("must decode");
assert_eq!(

View File

@@ -187,12 +187,12 @@ fn assertCorrectImage2string(fileName: &str, expected: &str) {
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
BufferedImageLuminanceSource::new(image),
))));
let rowNumber = binaryMap.getHeight() / 2;
let row = binaryMap.getBlackRow(rowNumber).expect("get row");
let rowNumber = binaryMap.get_height() / 2;
let row = binaryMap.get_black_row(rowNumber).expect("get row");
let mut rssExpandedReader = RSSExpandedReader::new();
let result = rssExpandedReader
.decodeRow(rowNumber as u32, &row, &HashMap::new())
.decode_row(rowNumber as u32, &row, &HashMap::new())
.expect("should decode");
assert_eq!(&BarcodeFormat::RSS_EXPANDED, result.getBarcodeFormat());

View File

@@ -45,8 +45,8 @@ fn testFindFinderPatterns() {
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
BufferedImageLuminanceSource::new(image),
))));
let rowNumber = binaryMap.getHeight() as u32 / 2;
let row = binaryMap.getBlackRow(rowNumber as usize).expect("ok");
let rowNumber = binaryMap.get_height() as u32 / 2;
let row = binaryMap.get_black_row(rowNumber as usize).expect("ok");
let mut previousPairs = Vec::new(); //new ArrayList<>();
let mut rssExpandedReader = RSSExpandedReader::new();
@@ -91,8 +91,8 @@ fn testRetrieveNextPairPatterns() {
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
BufferedImageLuminanceSource::new(image),
))));
let rowNumber = binaryMap.getHeight() as u32 / 2;
let row = binaryMap.getBlackRow(rowNumber as usize).expect("create");
let rowNumber = binaryMap.get_height() as u32 / 2;
let row = binaryMap.get_black_row(rowNumber as usize).expect("create");
let mut previousPairs = Vec::new(); //new ArrayList<>();
let mut rssExpandedReader = RSSExpandedReader::new();
@@ -120,7 +120,7 @@ fn testDecodeCheckCharacter() {
BufferedImageLuminanceSource::new(image.clone()),
))));
let row = binaryMap
.getBlackRow(binaryMap.getHeight() / 2)
.get_black_row(binaryMap.get_height() / 2)
.expect("create");
let startEnd = [145, 243]; //image pixels where the A1 pattern starts (at 124) and ends (at 214)
@@ -148,7 +148,7 @@ fn testDecodeDataCharacter() {
BufferedImageLuminanceSource::new(image.clone()),
))));
let row = binaryMap
.getBlackRow(binaryMap.getHeight() / 2)
.get_black_row(binaryMap.get_height() / 2)
.expect("create");
let startEnd = [145, 243]; //image pixels where the A1 pattern starts (at 124) and ends (at 214)

View File

@@ -29,14 +29,14 @@ use std::collections::HashMap;
use crate::{
common::{BitArray, Result},
oned::{
recordPattern, recordPatternInReverse,
record_pattern, record_pattern_in_reverse,
rss::{
rss_utils, AbstractRSSReaderTrait, DataCharacter, DataCharacterTrait, FinderPattern,
Pair,
},
OneDReader,
},
BarcodeFormat, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions,
BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions,
RXingResult, RXingResultMetadataType, RXingResultMetadataValue, Reader,
};
@@ -151,7 +151,7 @@ pub struct RSSExpandedReader {
}
impl AbstractRSSReaderTrait for RSSExpandedReader {}
impl OneDReader for RSSExpandedReader {
fn decodeRow(
fn decode_row(
&mut self,
rowNumber: u32,
row: &crate::common::BitArray,
@@ -173,26 +173,26 @@ impl OneDReader for RSSExpandedReader {
}
}
impl Reader for RSSExpandedReader {
fn decode(&mut self, image: &mut crate::BinaryBitmap) -> Result<crate::RXingResult> {
fn decode<B: Binarizer>(&mut self, image: &mut crate::BinaryBitmap<B>) -> Result<RXingResult> {
self.decode_with_hints(image, &HashMap::new())
}
// Note that we don't try rotation without the try harder flag, even if rotation was supported.
fn decode_with_hints(
fn decode_with_hints<B: Binarizer>(
&mut self,
image: &mut crate::BinaryBitmap,
image: &mut crate::BinaryBitmap<B>,
hints: &DecodingHintDictionary,
) -> Result<crate::RXingResult> {
if let Ok(res) = self.doDecode(image, hints) {
if let Ok(res) = self._do_decode(image, hints) {
Ok(res)
} else {
let tryHarder = matches!(
hints.get(&DecodeHintType::TRY_HARDER),
Some(DecodeHintValue::TryHarder(true))
);
if tryHarder && image.isRotateSupported() {
let mut rotatedImage = image.rotateCounterClockwise();
let mut result = self.doDecode(&mut rotatedImage, hints)?;
if tryHarder && image.is_rotate_supported() {
let mut rotatedImage = image.rotate_counter_clockwise();
let mut result = self._do_decode(&mut rotatedImage, hints)?;
// Record that we found it rotated 90 degrees CCW / 270 degrees CW
let metadata = result.getRXingResultMetadata();
let mut orientation = 270;
@@ -213,7 +213,7 @@ impl Reader for RSSExpandedReader {
RXingResultMetadataValue::Orientation(orientation),
);
// Update result points
let height = rotatedImage.getHeight();
let height = rotatedImage.get_height();
let total_points = result.getPoints().len();
let points = result.getPointsMut();
@@ -684,7 +684,7 @@ impl RSSExpandedReader {
// counters[2] = 0;
// counters[3] = 0;
let width = row.getSize();
let width = row.get_size();
let mut rowOffset;
if forcedOffset >= 0 {
@@ -823,9 +823,9 @@ impl RSSExpandedReader {
counters.fill(0);
if leftChar {
recordPatternInReverse(row, pattern.getStartEnd()[0], counters)?;
record_pattern_in_reverse(row, pattern.getStartEnd()[0], counters)?;
} else {
recordPattern(row, pattern.getStartEnd()[1], counters)?;
record_pattern(row, pattern.getStartEnd()[1], counters)?;
// reverse it
counters.reverse();
// let mut i = 0;

View File

@@ -39,8 +39,8 @@ fn testDecodingRowByRow() {
let binaryMap = test_case_util::getBinaryBitmap("1000.png");
let firstRowNumber = binaryMap.getHeight() / 3;
let firstRow = binaryMap.getBlackRow(firstRowNumber).expect("get row");
let firstRowNumber = binaryMap.get_height() / 3;
let firstRow = binaryMap.get_black_row(firstRowNumber).expect("get row");
// let tester = ;
@@ -72,9 +72,9 @@ fn testDecodingRowByRow() {
.unwrap()
.getStartEndMut()[1] = 0;
let secondRowNumber = 2 * binaryMap.getHeight() / 3;
let secondRowNumber = 2 * binaryMap.get_height() / 3;
let mut secondRow = binaryMap
.getBlackRow(secondRowNumber)
.get_black_row(secondRowNumber)
.expect("get row")
.into_owned();

View File

@@ -19,8 +19,8 @@ use std::collections::HashMap;
use crate::{
common::{BitArray, Result},
oned::{one_d_reader, OneDReader},
point, BarcodeFormat, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions,
RXingResult, RXingResultMetadataType, RXingResultMetadataValue, Reader,
point, BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, DecodingHintDictionary,
Exceptions, RXingResult, RXingResultMetadataType, RXingResultMetadataValue, Reader,
};
use super::{
@@ -45,12 +45,12 @@ pub struct RSS14Reader {
impl AbstractRSSReaderTrait for RSS14Reader {}
impl OneDReader for RSS14Reader {
fn decodeRow(
fn decode_row(
&mut self,
rowNumber: u32,
row: &crate::common::BitArray,
hints: &crate::DecodingHintDictionary,
) -> Result<crate::RXingResult> {
row: &BitArray,
hints: &DecodingHintDictionary,
) -> Result<RXingResult> {
let mut row = row.clone();
let leftPair = self.decodePair(&row, false, rowNumber, hints);
Self::addOrTally(&mut self.possibleLeftPairs, leftPair);
@@ -73,26 +73,26 @@ impl OneDReader for RSS14Reader {
}
}
impl Reader for RSS14Reader {
fn decode(&mut self, image: &mut crate::BinaryBitmap) -> Result<crate::RXingResult> {
fn decode<B: Binarizer>(&mut self, image: &mut crate::BinaryBitmap<B>) -> Result<RXingResult> {
self.decode_with_hints(image, &HashMap::new())
}
// Note that we don't try rotation without the try harder flag, even if rotation was supported.
fn decode_with_hints(
fn decode_with_hints<B: Binarizer>(
&mut self,
image: &mut crate::BinaryBitmap,
image: &mut crate::BinaryBitmap<B>,
hints: &DecodingHintDictionary,
) -> Result<crate::RXingResult> {
if let Ok(res) = self.doDecode(image, hints) {
if let Ok(res) = self._do_decode(image, hints) {
Ok(res)
} else {
let tryHarder = matches!(
hints.get(&DecodeHintType::TRY_HARDER),
Some(DecodeHintValue::TryHarder(true))
);
if tryHarder && image.isRotateSupported() {
let mut rotatedImage = image.rotateCounterClockwise();
let mut result = self.doDecode(&mut rotatedImage, hints)?;
if tryHarder && image.is_rotate_supported() {
let mut rotatedImage = image.rotate_counter_clockwise();
let mut result = self._do_decode(&mut rotatedImage, hints)?;
// Record that we found it rotated 90 degrees CCW / 270 degrees CW
let metadata = result.getRXingResultMetadata();
let mut orientation = 270;
@@ -113,7 +113,7 @@ impl Reader for RSS14Reader {
RXingResultMetadataValue::Orientation(orientation),
);
// Update result points
let height = rotatedImage.getHeight();
let height = rotatedImage.get_height();
let total_points = result.getPoints().len();
let points = result.getPointsMut();
for point in points.iter_mut().take(total_points) {
@@ -257,7 +257,7 @@ impl RSS14Reader {
let mut center: f32 = (startEnd[0] + startEnd[1] - 1) as f32 / 2.0;
if right {
// row is actually reversed
center = row.getSize() as f32 - 1.0 - center;
center = row.get_size() as f32 - 1.0 - center;
}
cb(point(center, rowNumber as f32));
}
@@ -287,9 +287,9 @@ impl RSS14Reader {
counters.fill(0);
if outsideChar {
one_d_reader::recordPatternInReverse(row, pattern.getStartEnd()[0], counters)?;
one_d_reader::record_pattern_in_reverse(row, pattern.getStartEnd()[0], counters)?;
} else {
one_d_reader::recordPattern(row, pattern.getStartEnd()[1], counters)?;
one_d_reader::record_pattern(row, pattern.getStartEnd()[1], counters)?;
// reverse it
counters.reverse();
// let mut i = 0;
@@ -379,7 +379,7 @@ impl RSS14Reader {
let counters = &mut self.decodeFinderCounters;
counters.fill(0);
let width = row.getSize();
let width = row.get_size();
let mut isWhite = false;
let mut rowOffset = 0;
while rowOffset < width {
@@ -445,8 +445,8 @@ impl RSS14Reader {
let mut end = startEnd[1];
if right {
// row is actually reversed
start = row.getSize() - 1 - start;
end = row.getSize() - 1 - end;
start = row.get_size() - 1 - start;
end = row.get_size() - 1 - end;
}
Ok(FinderPattern::new(