mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 20:32:34 +00:00
Cleanup of many components of the code.
Many unwraps, expects, and panics removed or handled. More rustification of the codebase. Squashed commit of the following: commit 80c6483cf8c819496a7b781f9e3a149d1f18d5fe Author: Henry <henry@azule.info> Date: Fri Feb 10 20:36:57 2023 -0600 add other parsed result option commit 412d92ae585e7bf194697f5282dbc045e448bace Author: Henry <henry@azule.info> Date: Fri Feb 10 19:06:34 2023 -0600 fix some panics commit 9108d3e931aadec00fc0bf346ebc52d24e5d8146 Author: Henry <henry@azule.info> Date: Fri Feb 10 18:56:30 2023 -0600 clippy --fix commit 8cd89d314323f54f342f15fc73297b62d418ac79 Author: Henry <henry@azule.info> Date: Fri Feb 10 18:55:45 2023 -0600 some more cleanup in aztec commit fb7f9242e273b85a4bb212f22a185a0dcbfafc36 Author: Henry <henry@azule.info> Date: Fri Feb 10 18:19:54 2023 -0600 cleanup root commit 4fb7c0639830869149332b29be789e891eade3e7 Author: Henry Schimke <henry@azule.info> Date: Fri Feb 10 16:50:15 2023 -0600 format commit 3ac271af657b25fbe5ee39cbdf8859e5aa38ea41 Author: Henry Schimke <henry@azule.info> Date: Fri Feb 10 16:43:14 2023 -0600 clippy --fix commit 0c5b4c1477159db93a9da9645039f51cd1a7c574 Author: Henry Schimke <henry@azule.info> Date: Fri Feb 10 16:04:31 2023 -0600 qrcode cleanup complete commit 70c1d107cc672e45d46567c650c46fece7e7da9c Author: Henry Schimke <henry@azule.info> Date: Fri Feb 10 13:28:25 2023 -0600 clippy --fix commit e13263d0f1f2a13dbb59b6e60d8b871a32babc92 Author: Henry Schimke <henry@azule.info> Date: Fri Feb 10 13:27:44 2023 -0600 qrcode detector cleanup commit f7f2855099e469fe1091868c6099196705d0ae83 Author: Henry Schimke <henry@azule.info> Date: Fri Feb 10 11:48:13 2023 -0600 qrcode decoder commit bdbaaba597b3479639c2ed3726465a69e3004471 Author: Henry Schimke <henry@azule.info> Date: Fri Feb 10 11:13:18 2023 -0600 clippy --fix commit fd6dbcaeb1f45dc91ae98f0e7c58aad48d67df78 Author: Henry Schimke <henry@azule.info> Date: Fri Feb 10 11:12:28 2023 -0600 pdf417 cleanup complete commit 7bbd47604c2639a5eb61bdb5533e71be3bce95a1 Author: Henry Schimke <henry@azule.info> Date: Fri Feb 10 10:39:28 2023 -0600 clippy --fix commit 8f4962b7bb5f133524fe4d3d4948e5672a88c545 Author: Henry Schimke <henry@azule.info> Date: Fri Feb 10 10:39:00 2023 -0600 cleanup on pdf417 hie commit 254c8243953776d3c8704dd81529aadd05d5a94e Author: Henry <henry@azule.info> Date: Fri Feb 10 02:51:21 2023 +0000 partway through pdf417 hle commit d4a5b00fde8c0282b0c6e4175a21c1e111b11533 Author: Henry <henry@azule.info> Date: Wed Feb 8 21:35:25 2023 +0000 clippy --fix commit 2bd4a8e218aa963a88997b50bc5a198f9a88205e Author: Henry <henry@azule.info> Date: Wed Feb 8 21:31:55 2023 +0000 pdf417 decode and detect cleanup commit 05130ac54a72c2303045d64507bf6185aa74e531 Author: Henry Schimke <henry@azule.info> Date: Tue Feb 7 08:06:25 2023 -0600 at decodeTextCompaction in decoded_bit_straem_parser commit 924c023a7d2339dfffa6ef0c4a805fb192576796 Author: Henry Schimke <henry@azule.info> Date: Mon Feb 6 18:22:02 2023 -0600 clippy --fix commit 5cfb81a885c22b4b5ec937a79be31d0815def211 Author: Henry Schimke <henry@azule.info> Date: Mon Feb 6 18:20:22 2023 -0600 oned cleanup commit 0649b64854baf56a8e3314728d4f6cf4063db8b2 Author: Henry Schimke <henry@azule.info> Date: Mon Feb 6 11:47:28 2023 -0600 rss cleanup commit 2d3127898fc11f7e65d590e983cda799b798b646 Author: Henry Schimke <henry@azule.info> Date: Mon Feb 6 10:19:56 2023 -0600 multi cleanup commit 7e8b43a159083a2096123e6204d4d1d359c59981 Author: Henry <henry@azule.info> Date: Sun Feb 5 21:41:59 2023 -0600 cargo fmt commit df8285e8da07b4bffd48ca6e596de3f010eb8d9e Author: Henry <henry@azule.info> Date: Sun Feb 5 21:41:29 2023 -0600 maxicode cleanup commit a51f923a14016eb416ca202b2194258808dde3e7 Author: Henry <henry@azule.info> Date: Sun Feb 5 21:27:57 2023 -0600 cargo fmt commit 332d7eef4a25318611025a3fcb0b70c604d917be Author: Henry <henry@azule.info> Date: Sun Feb 5 21:27:48 2023 -0600 cleanup datamatrix commit eb2311c476a9c9993f271ab6cf9fd359aedca26f Author: Henry <henry@azule.info> Date: Sun Feb 5 21:27:26 2023 -0600 cargo clippy --fix commit 2abaa4c51d287f619d4c7a438f85ba2a30f159cf Author: Henry <henry@azule.info> Date: Sun Feb 5 19:13:02 2023 -0600 finish cleanup in parsers commit 716aa2fbf728716cff36657a5fd80e7bca5276b7 Author: Henry Schimke <henry@azule.info> Date: Sun Feb 5 17:44:56 2023 -0600 cleanup continues commit 7d843788a49fa9cc82c4616d226c0f882e73612b Author: Henry Schimke <henry@azule.info> Date: Sun Feb 5 15:15:09 2023 -0600 common cleanup commit fd251866c68f22d450dd428c93d7a3620bf1be27 Author: Henry Schimke <henry@azule.info> Date: Sun Feb 5 10:24:54 2023 -0600 cleanup reedsolomon commit 132e27ed1468f5c1ce998516ff5a00687217c3e6 Author: Henry Schimke <henry@azule.info> Date: Sun Feb 5 09:52:04 2023 -0600 make MathUtils more generic commit d8352b7fff4cb4f9ecc8e8a969710d26ef410e8b Author: Henry Schimke <henry@azule.info> Date: Sun Feb 5 09:29:33 2023 -0600 cleanup some string handling in ResultParser There may still be some edge cases where multi-byte characters break the parser
This commit is contained in:
@@ -50,7 +50,12 @@ pub fn buildBitArrayFromString(data: &str) -> Result<BitArray, Exceptions> {
|
||||
// for (int i = 0; i < dotsAndXs.length(); ++i) {
|
||||
if i % 9 == 0 {
|
||||
// spaces
|
||||
if dotsAndXs.chars().nth(i).unwrap() != ' ' {
|
||||
if dotsAndXs
|
||||
.chars()
|
||||
.nth(i)
|
||||
.ok_or(Exceptions::ParseException(None))?
|
||||
!= ' '
|
||||
{
|
||||
return Err(Exceptions::IllegalStateException(Some(
|
||||
"space expected".to_owned(),
|
||||
)));
|
||||
@@ -58,7 +63,10 @@ pub fn buildBitArrayFromString(data: &str) -> Result<BitArray, Exceptions> {
|
||||
continue;
|
||||
}
|
||||
|
||||
let currentChar = dotsAndXs.chars().nth(i).unwrap();
|
||||
let currentChar = dotsAndXs
|
||||
.chars()
|
||||
.nth(i)
|
||||
.ok_or(Exceptions::ParseException(None))?;
|
||||
if currentChar == 'X' || currentChar == 'x' {
|
||||
binary.set(counter);
|
||||
}
|
||||
@@ -75,12 +83,17 @@ pub fn buildBitArrayFromStringWithoutSpaces(data: &str) -> Result<BitArray, Exce
|
||||
.replace_all(&ONE.replace_all(data, "X"), ".")
|
||||
.to_string();
|
||||
let mut current = 0;
|
||||
while current < dotsAndXs.chars().count() {
|
||||
let dotsAndXs_length = dotsAndXs.chars().count();
|
||||
while current < dotsAndXs_length {
|
||||
sb.push(' ');
|
||||
let mut i = 0;
|
||||
while i < 8 && current < dotsAndXs.chars().count() {
|
||||
// for (int i = 0; i < 8 && current < dotsAndXs.length(); ++i) {
|
||||
sb.push(dotsAndXs.chars().nth(current).unwrap());
|
||||
while i < 8 && current < dotsAndXs_length {
|
||||
sb.push(
|
||||
dotsAndXs
|
||||
.chars()
|
||||
.nth(current)
|
||||
.ok_or(Exceptions::ParseException(None))?,
|
||||
);
|
||||
current += 1;
|
||||
|
||||
i += 1;
|
||||
|
||||
@@ -33,19 +33,24 @@ use super::ExpandedPair;
|
||||
* @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es)
|
||||
*/
|
||||
|
||||
pub fn buildBitArray(pairs: &Vec<ExpandedPair>) -> BitArray {
|
||||
pub fn buildBitArray(pairs: &Vec<ExpandedPair>) -> Option<BitArray> {
|
||||
let mut charNumber = (pairs.len() * 2) - 1;
|
||||
if pairs.last().unwrap().getRightChar().is_none() {
|
||||
charNumber -= 1;
|
||||
if let Some(pair) = pairs.last() {
|
||||
if pair.getRightChar().is_none() {
|
||||
charNumber -= 1;
|
||||
}
|
||||
}
|
||||
// if pairs.last().unwrap().getRightChar().is_none() {
|
||||
// charNumber -= 1;
|
||||
// }
|
||||
|
||||
let size = 12 * charNumber;
|
||||
|
||||
let mut binary = BitArray::with_size(size);
|
||||
let mut accPos = 0;
|
||||
|
||||
let firstPair = pairs.get(0).unwrap();
|
||||
let Some(rp) = firstPair.getRightChar() else { panic!("first char must exist");};
|
||||
let firstPair = pairs.get(0)?;
|
||||
let rp = firstPair.getRightChar().as_ref()?;
|
||||
let firstValue = rp.getValue();
|
||||
let mut i = 11;
|
||||
while i >= 0 {
|
||||
@@ -60,8 +65,8 @@ pub fn buildBitArray(pairs: &Vec<ExpandedPair>) -> BitArray {
|
||||
|
||||
for i in 1..pairs.len() {
|
||||
// for (int i = 1; i < pairs.size(); ++i) {
|
||||
let currentPair = pairs.get(i).unwrap();
|
||||
let Some(lv) = currentPair.getLeftChar() else { panic!("I'm not sure how we get here ");};
|
||||
let currentPair = pairs.get(i)?;
|
||||
let lv = currentPair.getLeftChar().as_ref()?;
|
||||
let leftValue = lv.getValue();
|
||||
let mut j = 11;
|
||||
while j >= 0 {
|
||||
@@ -88,7 +93,7 @@ pub fn buildBitArray(pairs: &Vec<ExpandedPair>) -> BitArray {
|
||||
}
|
||||
}
|
||||
}
|
||||
binary
|
||||
Some(binary)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,6 +144,6 @@ mod BitArrayBuilderTest {
|
||||
pairs.push(expandedPair);
|
||||
}
|
||||
|
||||
super::buildBitArray(&pairs)
|
||||
super::buildBitArray(&pairs).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ pub struct AI01AndOtherAIs<'a>(&'a BitArray, GeneralAppIdDecoder<'a>);
|
||||
impl AI01decoder for AI01AndOtherAIs<'_> {}
|
||||
impl AbstractExpandedDecoder for AI01AndOtherAIs<'_> {
|
||||
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
|
||||
let mut buff = String::new(); //new StringBuilder();
|
||||
let mut buff = String::new();
|
||||
|
||||
buff.push_str("(01)");
|
||||
let initialGtinPosition = buff.chars().count();
|
||||
@@ -59,9 +59,7 @@ impl<'a> AI01AndOtherAIs<'_> {
|
||||
AI01AndOtherAIs(information, GeneralAppIdDecoder::new(information))
|
||||
}
|
||||
|
||||
const HEADER_SIZE: usize = 1 + 1 + 2; //first bit encodes the linkage flag,
|
||||
//the second one is the encodation method, and the other two are for the variable length
|
||||
// AI01AndOtherAIs(BitArray information) {
|
||||
// super(information);
|
||||
// }
|
||||
//first bit encodes the linkage flag,
|
||||
//the second one is the encodation method, and the other two are for the variable length
|
||||
const HEADER_SIZE: usize = 1 + 1 + 2;
|
||||
}
|
||||
|
||||
@@ -65,10 +65,12 @@ pub trait AI01decoder: AbstractExpandedDecoder {
|
||||
}
|
||||
}
|
||||
|
||||
/// Append the checkdigit to the string buffer
|
||||
///
|
||||
/// Panics is buf is less than 13 characters in length
|
||||
pub(super) fn appendCheckDigit(buf: &mut String, currentPos: usize) {
|
||||
let mut checkDigit = 0;
|
||||
for i in 0..13 {
|
||||
// for (int i = 0; i < 13; i++) {
|
||||
let digit = buf.chars().nth(i + currentPos).unwrap() as u32 - '0' as u32;
|
||||
checkDigit += if (i & 0x01) == 0 { 3 * digit } else { digit };
|
||||
}
|
||||
|
||||
@@ -40,7 +40,6 @@ pub trait AI01weightDecoder: AI01decoder {
|
||||
|
||||
let mut currentDivisor = 100000;
|
||||
for _i in 0..5 {
|
||||
// for (int i = 0; i < 5; ++i) {
|
||||
if weightNumeric / currentDivisor == 0 {
|
||||
buf.push('0');
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@ pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String, Excep
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let firstThreeDigits: String = rawInformation.chars().take(3).collect(); //rawInformation.substring(0, 3);
|
||||
let firstThreeDigits: String = rawInformation.chars().take(3).collect();
|
||||
let threeDigitDataLength = THREE_DIGIT_DATA_LENGTH.get(&firstThreeDigits);
|
||||
if let Some(tddl) = threeDigitDataLength {
|
||||
if tddl.variable {
|
||||
@@ -230,7 +230,7 @@ fn processVariableAI(
|
||||
variableFieldSize: usize,
|
||||
rawInformation: &str,
|
||||
) -> Result<String, Exceptions> {
|
||||
let ai: String = rawInformation.chars().take(aiSize).collect(); //rawInformation.substring(0, aiSize);
|
||||
let ai: String = rawInformation.chars().take(aiSize).collect();
|
||||
let maxSize = rawInformation
|
||||
.chars()
|
||||
.count()
|
||||
|
||||
@@ -132,7 +132,6 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
) -> u32 {
|
||||
let mut value = 0;
|
||||
for i in 0..bits {
|
||||
// for (int i = 0; i < bits; ++i) {
|
||||
if information.get(pos + i as usize) {
|
||||
value |= 1 << (bits - i - 1);
|
||||
}
|
||||
@@ -197,8 +196,8 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
}
|
||||
} //while (!isFinished);
|
||||
|
||||
if result.getDecodedInformation().is_some() {
|
||||
Ok(result.getDecodedInformation().as_ref().unwrap().clone())
|
||||
if let Some(r) = result.getDecodedInformation() {
|
||||
Ok(r.clone())
|
||||
} else {
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
@@ -346,7 +345,8 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
if (5..15).contains(&fiveBitValue) {
|
||||
return Ok(DecodedChar::new(
|
||||
pos + 5,
|
||||
char::from_u32('0' as u32 + fiveBitValue - 5).unwrap(),
|
||||
char::from_u32('0' as u32 + fiveBitValue - 5)
|
||||
.ok_or(Exceptions::ParseException(None))?,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -355,14 +355,14 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
if (64..90).contains(&sevenBitValue) {
|
||||
return Ok(DecodedChar::new(
|
||||
pos + 7,
|
||||
char::from_u32(sevenBitValue + 1).unwrap(),
|
||||
char::from_u32(sevenBitValue + 1).ok_or(Exceptions::ParseException(None))?,
|
||||
));
|
||||
}
|
||||
|
||||
if (90..116).contains(&sevenBitValue) {
|
||||
return Ok(DecodedChar::new(
|
||||
pos + 7,
|
||||
char::from_u32(sevenBitValue + 7).unwrap(),
|
||||
char::from_u32(sevenBitValue + 7).ok_or(Exceptions::ParseException(None))?,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -424,7 +424,8 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
if (5..15).contains(&fiveBitValue) {
|
||||
return Ok(DecodedChar::new(
|
||||
pos + 5,
|
||||
char::from_u32('0' as u32 + fiveBitValue - 5).unwrap(),
|
||||
char::from_u32('0' as u32 + fiveBitValue - 5)
|
||||
.ok_or(Exceptions::ParseException(None))?,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -433,7 +434,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
if (32..58).contains(&sixBitValue) {
|
||||
return Ok(DecodedChar::new(
|
||||
pos + 6,
|
||||
char::from_u32(sixBitValue + 33).unwrap(),
|
||||
char::from_u32(sixBitValue + 33).ok_or(Exceptions::ParseException(None))?,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -460,7 +461,6 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
|
||||
let mut i = 0;
|
||||
while i < 5 && i + pos < self.information.getSize() {
|
||||
// for (int i = 0; i < 5 && i + pos < this.information.getSize(); ++i) {
|
||||
if i == 2 {
|
||||
if !self.information.get(pos + 2) {
|
||||
return false;
|
||||
@@ -482,7 +482,6 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
}
|
||||
|
||||
for i in pos..pos + 3 {
|
||||
// for (int i = pos; i < pos + 3; ++i) {
|
||||
if self.information.get(i) {
|
||||
return false;
|
||||
}
|
||||
@@ -500,7 +499,6 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
|
||||
let mut i = 0;
|
||||
while i < 4 && i + pos < self.information.getSize() {
|
||||
// for (int i = 0; i < 4 && i + pos < this.information.getSize(); ++i) {
|
||||
if self.information.get(pos + i) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -190,6 +190,6 @@ fn assertCorrectImage2binary(fileName: &str, expected: &str) {
|
||||
// fail(re.toString());
|
||||
// return;
|
||||
// }
|
||||
let binary = bit_array_builder::buildBitArray(&pairs);
|
||||
let binary = bit_array_builder::buildBitArray(&pairs).unwrap();
|
||||
assert_eq!(expected, binary.to_string());
|
||||
}
|
||||
|
||||
@@ -36,8 +36,8 @@ use crate::{
|
||||
},
|
||||
OneDReader,
|
||||
},
|
||||
BarcodeFormat, DecodeHintType, DecodingHintDictionary, Exceptions, RXingResult,
|
||||
RXingResultMetadataType, RXingResultMetadataValue, Reader,
|
||||
BarcodeFormat, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions,
|
||||
RXingResult, RXingResultMetadataType, RXingResultMetadataValue, Reader,
|
||||
};
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
@@ -144,9 +144,9 @@ pub struct RSSExpandedReader {
|
||||
oddCounts: [u32; 4],
|
||||
evenCounts: [u32; 4],
|
||||
|
||||
pub(super) pairs: Vec<ExpandedPair>, //new ArrayList<>(MAX_PAIRS);
|
||||
pub(super) rows: Vec<ExpandedRow>, // new ArrayList<>();
|
||||
startEnd: [u32; 2], // new int[2];
|
||||
pub(super) pairs: Vec<ExpandedPair>,
|
||||
pub(super) rows: Vec<ExpandedRow>,
|
||||
startEnd: [u32; 2],
|
||||
startFromEven: bool,
|
||||
}
|
||||
impl AbstractRSSReaderTrait for RSSExpandedReader {}
|
||||
@@ -161,19 +161,11 @@ impl OneDReader for RSSExpandedReader {
|
||||
// So lets try twice
|
||||
self.pairs.clear();
|
||||
self.startFromEven = false;
|
||||
// try {
|
||||
if let Ok(decoded_two_pairs) = self.decodeRow2pairs(rowNumber, row) {
|
||||
if let Ok(possible_result) = Self::constructRXingResult(&decoded_two_pairs) {
|
||||
return Ok(possible_result);
|
||||
}
|
||||
}
|
||||
// let possible = Self::constructRXingResult(&self.decodeRow2pairs(rowNumber, row)?);
|
||||
// if possible.is_ok() {
|
||||
// return possible;
|
||||
// }
|
||||
// } catch (NotFoundException e) {
|
||||
// OK
|
||||
// }
|
||||
|
||||
self.pairs.clear();
|
||||
self.startFromEven = true;
|
||||
@@ -197,7 +189,10 @@ impl Reader for RSSExpandedReader {
|
||||
if let Ok(res) = self.doDecode(image, hints) {
|
||||
Ok(res)
|
||||
} else {
|
||||
let tryHarder = hints.contains_key(&DecodeHintType::TRY_HARDER);
|
||||
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)?;
|
||||
@@ -221,23 +216,14 @@ impl Reader for RSSExpandedReader {
|
||||
RXingResultMetadataValue::Orientation(orientation),
|
||||
);
|
||||
// Update result points
|
||||
// let points = result.getRXingResultPoints();
|
||||
// if points != null {
|
||||
let height = rotatedImage.getHeight();
|
||||
// for point in result.getRXingResultPointsMut().iter_mut() {
|
||||
|
||||
let total_points = result.getRXingResultPoints().len();
|
||||
let points = result.getRXingResultPointsMut();
|
||||
for point in points.iter_mut().take(total_points) {
|
||||
// for i in 0..total_points {
|
||||
// for (int i = 0; i < points.length; i++) {
|
||||
std::mem::swap(&mut point.x, &mut point.y);
|
||||
point.x = height as f32 - point.x - 1.0;
|
||||
// points[i] = RXingResultPoint::new(
|
||||
// height as f32 - points[i].getY() - 1.0,
|
||||
// points[i].getX(),
|
||||
// );
|
||||
}
|
||||
// }
|
||||
|
||||
Ok(result)
|
||||
} else {
|
||||
@@ -311,23 +297,14 @@ impl RSSExpandedReader {
|
||||
if let Ok(to_add) = to_add_res {
|
||||
self.pairs.push(to_add);
|
||||
} else if self.pairs.is_empty() {
|
||||
return Err(to_add_res.err().unwrap());
|
||||
return Err(to_add_res
|
||||
.err()
|
||||
.unwrap_or(Exceptions::IllegalStateException(None)));
|
||||
} else {
|
||||
// exit this loop when retrieveNextPair() fails and throws
|
||||
done = true;
|
||||
}
|
||||
// self.pairs = previousPairs;
|
||||
}
|
||||
// try {
|
||||
// this.pairs.add(retrieveNextPair(row, this.pairs, rowNumber));
|
||||
// } catch (NotFoundException nfe) {
|
||||
// if (this.pairs.isEmpty()) {
|
||||
// throw nfe;
|
||||
// }
|
||||
// // exit this loop when retrieveNextPair() fails and throws
|
||||
// done = true;
|
||||
// }
|
||||
// }
|
||||
|
||||
// TODO: verify sequence of finder patterns as in checkPairSequence()
|
||||
if self.checkChecksum() {
|
||||
@@ -339,14 +316,19 @@ impl RSSExpandedReader {
|
||||
if tryStackedDecode {
|
||||
// When the image is 180-rotated, then rows are sorted in wrong direction.
|
||||
// Try twice with both the directions.
|
||||
let ps = self.checkRows(false);
|
||||
if let Some(ps) = ps {
|
||||
return Ok(ps);
|
||||
}
|
||||
let ps = self.checkRows(true);
|
||||
let ps = self.checkRows(false).or(self.checkRows(true));
|
||||
if let Some(ps) = ps {
|
||||
return Ok(ps);
|
||||
}
|
||||
|
||||
// let ps = self.checkRows(false);
|
||||
// if let Some(ps) = ps {
|
||||
// return Ok(ps);
|
||||
// }
|
||||
// let ps = self.checkRows(true);
|
||||
// if let Some(ps) = ps {
|
||||
// return Ok(ps);
|
||||
// }
|
||||
}
|
||||
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
@@ -367,11 +349,12 @@ impl RSSExpandedReader {
|
||||
}
|
||||
|
||||
let mut c_rows = Vec::new();
|
||||
let ps = if let Ok(res) = self.checkRowsDetails(&mut c_rows, 0) {
|
||||
Some(res)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let ps = self.checkRowsDetails(&mut c_rows, 0).ok();
|
||||
// let ps = if let Ok(res) = self.checkRowsDetails(&mut c_rows, 0) {
|
||||
// Some(res)
|
||||
// } else {
|
||||
// None
|
||||
// };
|
||||
// } catch (NotFoundException e) {
|
||||
// OK
|
||||
// }
|
||||
@@ -392,7 +375,10 @@ impl RSSExpandedReader {
|
||||
) -> Result<Vec<ExpandedPair>, Exceptions> {
|
||||
for i in currentRow..self.rows.len() {
|
||||
// for (int i = currentRow; i < rows.size(); i++) {
|
||||
let row = self.rows.get(i).unwrap();
|
||||
let row = self
|
||||
.rows
|
||||
.get(i)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
|
||||
self.pairs.clear();
|
||||
for collectedRow in &collectedRows.clone() {
|
||||
// for (ExpandedRow collectedRow : collectedRows) {
|
||||
@@ -423,12 +409,15 @@ impl RSSExpandedReader {
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
// Whether the pairs form a valid find pattern sequence,
|
||||
// either complete or a prefix
|
||||
/// Whether the pairs form a valid find pattern sequence,
|
||||
/// either complete or a prefix
|
||||
///
|
||||
/// Could potentially panic in a very out of bounds situation
|
||||
fn isValidSequence(pairs: &[ExpandedPair]) -> bool {
|
||||
for i in 0..FINDER_PATTERN_SEQUENCES.len() {
|
||||
for sequence in FINDER_PATTERN_SEQUENCES.iter() {
|
||||
// for i in 0..FINDER_PATTERN_SEQUENCES.len() {
|
||||
// for sequence in &FINDER_PATTERN_SEQUENCES.iter() {
|
||||
let sequence = FINDER_PATTERN_SEQUENCES.get(i).unwrap();
|
||||
// let sequence = FINDER_PATTERN_SEQUENCES.get(i).unwrap();
|
||||
// for (int[] sequence : FINDER_PATTERN_SEQUENCES) {
|
||||
if pairs.len() <= sequence.len() {
|
||||
let mut stop = true;
|
||||
@@ -462,13 +451,14 @@ impl RSSExpandedReader {
|
||||
let mut prevIsSame = false;
|
||||
let mut nextIsSame = false;
|
||||
while insertPos < self.rows.len() {
|
||||
let erow = self.rows.get(insertPos).unwrap();
|
||||
if erow.getRowNumber() > rowNumber {
|
||||
nextIsSame = erow.isEquivalent(&self.pairs);
|
||||
break;
|
||||
if let Some(erow) = self.rows.get(insertPos) {
|
||||
if erow.getRowNumber() > rowNumber {
|
||||
nextIsSame = erow.isEquivalent(&self.pairs);
|
||||
break;
|
||||
}
|
||||
prevIsSame = erow.isEquivalent(&self.pairs);
|
||||
insertPos += 1;
|
||||
}
|
||||
prevIsSame = erow.isEquivalent(&self.pairs);
|
||||
insertPos += 1;
|
||||
}
|
||||
if nextIsSame || prevIsSame {
|
||||
return;
|
||||
@@ -506,40 +496,15 @@ impl RSSExpandedReader {
|
||||
true
|
||||
}
|
||||
});
|
||||
// let row_search = rows.clone();
|
||||
// for i in 0..row_search.len() {
|
||||
// // for r in rows {
|
||||
// // for (Iterator<ExpandedRow> iterator = rows.iterator(); iterator.hasNext();) {
|
||||
// // ExpandedRow r = iterator.next();
|
||||
// let r = row_search.get(i).unwrap();
|
||||
// if r.getPairs().len() != pairs.len() {
|
||||
// let mut allFound = true;
|
||||
// for p in r.getPairs() {
|
||||
// // for (ExpandedPair p : r.getPairs()) {
|
||||
// if !pairs.contains(p) {
|
||||
// allFound = false;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// if allFound {
|
||||
// // 'pairs' contains all the pairs from the row 'r'
|
||||
// // iterator.remove();
|
||||
// rows.remove(i);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
// Returns true when one of the rows already contains all the pairs
|
||||
fn isPartialRow(pairs: &[ExpandedPair], rows: &[ExpandedRow]) -> bool {
|
||||
for r in rows {
|
||||
// for (ExpandedRow r : rows) {
|
||||
let mut allFound = true;
|
||||
for p in pairs {
|
||||
// for (ExpandedPair p : pairs) {
|
||||
let mut found = false;
|
||||
for pp in r.getPairs() {
|
||||
// for (ExpandedPair pp : r.getPairs()) {
|
||||
if p == pp {
|
||||
found = true;
|
||||
break;
|
||||
@@ -571,24 +536,25 @@ impl RSSExpandedReader {
|
||||
|
||||
// Not private for unit testing
|
||||
pub(crate) fn constructRXingResult(pairs: &[ExpandedPair]) -> Result<RXingResult, Exceptions> {
|
||||
let binary = bit_array_builder::buildBitArray(&pairs.to_vec());
|
||||
let binary = bit_array_builder::buildBitArray(&pairs.to_vec())
|
||||
.ok_or(Exceptions::IllegalStateException(None))?;
|
||||
|
||||
let mut decoder = abstract_expanded_decoder::createDecoder(&binary)?;
|
||||
let resultingString = decoder.parseInformation()?;
|
||||
|
||||
let firstPoints = pairs
|
||||
.get(0)
|
||||
.unwrap()
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.getFinderPattern()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.getRXingResultPoints();
|
||||
let lastPoints = pairs
|
||||
.last()
|
||||
.unwrap()
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.getFinderPattern()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.getRXingResultPoints();
|
||||
|
||||
let mut result = RXingResult::new(
|
||||
@@ -607,23 +573,24 @@ impl RSSExpandedReader {
|
||||
}
|
||||
|
||||
fn checkChecksum(&self) -> bool {
|
||||
let firstPair = self.pairs.get(0).unwrap();
|
||||
let Some(firstPair) = self.pairs.get(0) else {
|
||||
return false;
|
||||
};
|
||||
let checkCharacter = firstPair.getLeftChar();
|
||||
let Some(firstCharacter) = firstPair.getRightChar() else {
|
||||
return false;
|
||||
};
|
||||
return false;
|
||||
};
|
||||
|
||||
let mut checksum = firstCharacter.getChecksumPortion();
|
||||
let mut s = 2;
|
||||
|
||||
// for i in 1..self.pairs.len() {
|
||||
// for (int i = 1; i < this.pairs.size(); ++i) {
|
||||
for currentPair in self.pairs.iter().skip(1) {
|
||||
// let currentPair = self.pairs.get(i).unwrap();
|
||||
checksum += currentPair.getLeftChar().unwrap().getChecksumPortion();
|
||||
let Some(currentPairLeftChar) = currentPair.getLeftChar() else {
|
||||
return false;
|
||||
};
|
||||
checksum += currentPairLeftChar.getChecksumPortion();
|
||||
s += 1;
|
||||
if let Some(currentRightChar) = currentPair.getRightChar() {
|
||||
// if (currentRightChar != null) {
|
||||
checksum += currentRightChar.getChecksumPortion();
|
||||
s += 1;
|
||||
}
|
||||
@@ -633,7 +600,11 @@ impl RSSExpandedReader {
|
||||
|
||||
let checkCharacterValue = (211 * (s as i64 - 4) + checksum as i64) as u32;
|
||||
|
||||
checkCharacterValue == checkCharacter.unwrap().getValue()
|
||||
if let Some(checkCharacter) = checkCharacter {
|
||||
checkCharacterValue == checkCharacter.getValue()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn getNextSecondBar(row: &BitArray, initialPos: usize) -> usize {
|
||||
@@ -675,30 +646,39 @@ impl RSSExpandedReader {
|
||||
if !keepFinding {
|
||||
break;
|
||||
}
|
||||
} //while (keepFinding);
|
||||
}
|
||||
|
||||
// When stacked symbol is split over multiple rows, there's no way to guess if this pair can be last or not.
|
||||
// boolean mayBeLast = checkPairSequence(previousPairs, pattern);
|
||||
|
||||
let leftChar =
|
||||
self.decodeDataCharacter(row, pattern.as_ref().unwrap(), isOddPattern, true)?;
|
||||
let leftChar = self.decodeDataCharacter(
|
||||
row,
|
||||
pattern
|
||||
.as_ref()
|
||||
.ok_or(Exceptions::NotFoundException(None))?,
|
||||
isOddPattern,
|
||||
true,
|
||||
)?;
|
||||
|
||||
if !previousPairs.is_empty() && previousPairs.last().unwrap().mustBeLast() {
|
||||
if !previousPairs.is_empty()
|
||||
&& previousPairs
|
||||
.last()
|
||||
.ok_or(Exceptions::NotFoundException(None))?
|
||||
.mustBeLast()
|
||||
{
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let rightChar = if let Ok(ch) =
|
||||
self.decodeDataCharacter(row, pattern.as_ref().unwrap(), isOddPattern, false)
|
||||
{
|
||||
Some(ch)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// try {
|
||||
// rightChar = this.decodeDataCharacter(row, pattern, isOddPattern, false);
|
||||
// } catch (NotFoundException ignored) {
|
||||
// rightChar = null;
|
||||
// }
|
||||
let rightChar = self
|
||||
.decodeDataCharacter(
|
||||
row,
|
||||
pattern
|
||||
.as_ref()
|
||||
.ok_or(Exceptions::NotFoundException(None))?,
|
||||
isOddPattern,
|
||||
false,
|
||||
)
|
||||
.ok();
|
||||
|
||||
Ok(ExpandedPair::new(Some(leftChar), rightChar, pattern))
|
||||
}
|
||||
@@ -710,10 +690,11 @@ impl RSSExpandedReader {
|
||||
forcedOffset: i32,
|
||||
) -> Result<(), Exceptions> {
|
||||
let counters = &mut self.decodeFinderCounters;
|
||||
counters[0] = 0;
|
||||
counters[1] = 0;
|
||||
counters[2] = 0;
|
||||
counters[3] = 0;
|
||||
counters.fill(0);
|
||||
// counters[0] = 0;
|
||||
// counters[1] = 0;
|
||||
// counters[2] = 0;
|
||||
// counters[3] = 0;
|
||||
|
||||
let width = row.getSize();
|
||||
|
||||
@@ -723,8 +704,14 @@ impl RSSExpandedReader {
|
||||
} else if previousPairs.is_empty() {
|
||||
rowOffset = 0;
|
||||
} else {
|
||||
let lastPair = previousPairs.last().unwrap();
|
||||
rowOffset = lastPair.getFinderPattern().as_ref().unwrap().getStartEnd()[1] as i32;
|
||||
let lastPair = previousPairs
|
||||
.last()
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
|
||||
rowOffset = lastPair
|
||||
.getFinderPattern()
|
||||
.as_ref()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.getStartEnd()[1] as i32;
|
||||
}
|
||||
let mut searchingEvenPair = previousPairs.len() % 2 != 0;
|
||||
if self.startFromEven {
|
||||
@@ -779,11 +766,11 @@ impl RSSExpandedReader {
|
||||
}
|
||||
|
||||
fn reverseCounters(counters: &mut [u32]) {
|
||||
let length = counters.len();
|
||||
for i in 0..length / 2 {
|
||||
// for (int i = 0; i < length / 2; ++i) {
|
||||
counters.swap(i, length - i - 1);
|
||||
}
|
||||
counters.reverse();
|
||||
// let length = counters.len();
|
||||
// for i in 0..length / 2 {
|
||||
// counters.swap(i, length - i - 1);
|
||||
// }
|
||||
}
|
||||
|
||||
fn parseFoundFinderPattern(
|
||||
@@ -826,9 +813,7 @@ impl RSSExpandedReader {
|
||||
// System.arraycopy(counters, 0, counters, 1, counters.length - 1);
|
||||
|
||||
counters[0] = firstCounter as u32;
|
||||
let Ok(value) = Self::parseFinderValue(&counters, &Self::FINDER_PATTERNS) else {
|
||||
return None
|
||||
};
|
||||
let value = Self::parseFinderValue(&counters, &Self::FINDER_PATTERNS).ok()?;
|
||||
|
||||
Some(FinderPattern::new(
|
||||
value,
|
||||
@@ -854,34 +839,29 @@ impl RSSExpandedReader {
|
||||
} else {
|
||||
recordPattern(row, pattern.getStartEnd()[1], counters)?;
|
||||
// reverse it
|
||||
let mut i = 0;
|
||||
let mut j = counters.len() - 1;
|
||||
while i < j {
|
||||
// for (int i = 0, j = counters.length - 1; i < j; i++, j--) {
|
||||
counters.reverse();
|
||||
// let mut i = 0;
|
||||
// let mut j = counters.len() - 1;
|
||||
// while i < j {
|
||||
// counters.swap(i, j);
|
||||
|
||||
counters.swap(i, j);
|
||||
|
||||
i += 1;
|
||||
j -= 1;
|
||||
}
|
||||
// i += 1;
|
||||
// j -= 1;
|
||||
// }
|
||||
} //counters[] has the pixels of the module
|
||||
|
||||
let numModules = 17; //left and right data characters have all the same length
|
||||
// let elementWidth: f32 = MathUtils::sum(counters) / numModules;
|
||||
//left and right data characters have all the same length
|
||||
let numModules = 17;
|
||||
|
||||
let elementWidth: f32 = (counters.iter().sum::<u32>() as f32) / numModules as f32;
|
||||
|
||||
// Sanity check: element width for pattern and the character should match
|
||||
// Logic check: element width for pattern and the character should match
|
||||
let expectedElementWidth: f32 =
|
||||
(pattern.getStartEnd()[1] - pattern.getStartEnd()[0]) as f32 / 15.0;
|
||||
if (elementWidth - expectedElementWidth).abs() / expectedElementWidth > 0.3 {
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
// let oddCounts = &mut self.oddCounts;
|
||||
// let evenCounts = &mut self.evenCounts;
|
||||
// let oddRoundingErrors = &mut self.oddRoundingErrors;
|
||||
// let evenRoundingErrors = &mut self.evenRoundingErrors;
|
||||
|
||||
for (i, counter) in counters.iter().enumerate() {
|
||||
// for (int i = 0; i < counters.length; i++) {
|
||||
let value: f32 = 1.0 * (*counter as f32) / elementWidth;
|
||||
@@ -911,13 +891,12 @@ impl RSSExpandedReader {
|
||||
|
||||
let weightRowNumber = (4 * pattern.getValue() as isize
|
||||
+ (if isOddPattern { 0 } else { 2 })
|
||||
+ isize::from(!leftChar)//(if leftChar { 0 } else { 1 })
|
||||
+ isize::from(!leftChar)
|
||||
- 1) as usize;
|
||||
|
||||
let mut oddSum = 0;
|
||||
let mut oddChecksumPortion = 0;
|
||||
for i in (0..self.oddCounts.len()).rev() {
|
||||
// for (int i = oddCounts.length - 1; i >= 0; i--) {
|
||||
if Self::isNotA1left(pattern, isOddPattern, leftChar) {
|
||||
let weight = Self::WEIGHTS[weightRowNumber][2 * i];
|
||||
oddChecksumPortion += self.oddCounts[i] * weight;
|
||||
@@ -926,7 +905,6 @@ impl RSSExpandedReader {
|
||||
}
|
||||
let mut evenChecksumPortion = 0;
|
||||
for i in (0..self.evenCounts.len()).rev() {
|
||||
// for (int i = evenCounts.length - 1; i >= 0; i--) {
|
||||
if Self::isNotA1left(pattern, isOddPattern, leftChar) {
|
||||
let weight = Self::WEIGHTS[weightRowNumber][2 * i + 1];
|
||||
evenChecksumPortion += self.evenCounts[i] * weight;
|
||||
@@ -950,14 +928,13 @@ impl RSSExpandedReader {
|
||||
Ok(DataCharacter::new(value, checksumPortion))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn isNotA1left(pattern: &FinderPattern, isOddPattern: bool, leftChar: bool) -> bool {
|
||||
// A1: pattern.getValue is 0 (A), and it's an oddPattern, and it is a left char
|
||||
!(pattern.getValue() == 0 && isOddPattern && leftChar)
|
||||
}
|
||||
|
||||
fn adjustOddEvenCounts(&mut self, numModules: u32) -> Result<(), Exceptions> {
|
||||
// let oddSum = MathUtils::sum(&self.oddCounts);
|
||||
// let evenSum = MathUtils::sum(&self.evenCounts);
|
||||
let oddSum = self.oddCounts.iter().sum::<u32>();
|
||||
let evenSum = self.evenCounts.iter().sum::<u32>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user