Skip to content

Commit

Permalink
Cleanup of many components of the code.
Browse files Browse the repository at this point in the history
Many unwraps, expects, and panics removed or handled.

More rustification of the codebase.

Squashed commit of the following:

commit 80c6483
Author: Henry <[email protected]>
Date:   Fri Feb 10 20:36:57 2023 -0600

    add other parsed result option

commit 412d92a
Author: Henry <[email protected]>
Date:   Fri Feb 10 19:06:34 2023 -0600

    fix some panics

commit 9108d3e
Author: Henry <[email protected]>
Date:   Fri Feb 10 18:56:30 2023 -0600

    clippy --fix

commit 8cd89d3
Author: Henry <[email protected]>
Date:   Fri Feb 10 18:55:45 2023 -0600

    some more cleanup in aztec

commit fb7f924
Author: Henry <[email protected]>
Date:   Fri Feb 10 18:19:54 2023 -0600

    cleanup root

commit 4fb7c06
Author: Henry Schimke <[email protected]>
Date:   Fri Feb 10 16:50:15 2023 -0600

    format

commit 3ac271a
Author: Henry Schimke <[email protected]>
Date:   Fri Feb 10 16:43:14 2023 -0600

    clippy --fix

commit 0c5b4c1
Author: Henry Schimke <[email protected]>
Date:   Fri Feb 10 16:04:31 2023 -0600

    qrcode cleanup complete

commit 70c1d10
Author: Henry Schimke <[email protected]>
Date:   Fri Feb 10 13:28:25 2023 -0600

    clippy --fix

commit e13263d
Author: Henry Schimke <[email protected]>
Date:   Fri Feb 10 13:27:44 2023 -0600

    qrcode detector cleanup

commit f7f2855
Author: Henry Schimke <[email protected]>
Date:   Fri Feb 10 11:48:13 2023 -0600

    qrcode decoder

commit bdbaaba
Author: Henry Schimke <[email protected]>
Date:   Fri Feb 10 11:13:18 2023 -0600

    clippy --fix

commit fd6dbca
Author: Henry Schimke <[email protected]>
Date:   Fri Feb 10 11:12:28 2023 -0600

    pdf417 cleanup complete

commit 7bbd476
Author: Henry Schimke <[email protected]>
Date:   Fri Feb 10 10:39:28 2023 -0600

    clippy --fix

commit 8f4962b
Author: Henry Schimke <[email protected]>
Date:   Fri Feb 10 10:39:00 2023 -0600

    cleanup on pdf417 hie

commit 254c824
Author: Henry <[email protected]>
Date:   Fri Feb 10 02:51:21 2023 +0000

    partway through pdf417 hle

commit d4a5b00
Author: Henry <[email protected]>
Date:   Wed Feb 8 21:35:25 2023 +0000

    clippy --fix

commit 2bd4a8e
Author: Henry <[email protected]>
Date:   Wed Feb 8 21:31:55 2023 +0000

    pdf417 decode and detect cleanup

commit 05130ac
Author: Henry Schimke <[email protected]>
Date:   Tue Feb 7 08:06:25 2023 -0600

    at decodeTextCompaction in decoded_bit_straem_parser

commit 924c023
Author: Henry Schimke <[email protected]>
Date:   Mon Feb 6 18:22:02 2023 -0600

    clippy --fix

commit 5cfb81a
Author: Henry Schimke <[email protected]>
Date:   Mon Feb 6 18:20:22 2023 -0600

    oned cleanup

commit 0649b64
Author: Henry Schimke <[email protected]>
Date:   Mon Feb 6 11:47:28 2023 -0600

    rss cleanup

commit 2d31278
Author: Henry Schimke <[email protected]>
Date:   Mon Feb 6 10:19:56 2023 -0600

    multi cleanup

commit 7e8b43a
Author: Henry <[email protected]>
Date:   Sun Feb 5 21:41:59 2023 -0600

    cargo fmt

commit df8285e
Author: Henry <[email protected]>
Date:   Sun Feb 5 21:41:29 2023 -0600

    maxicode cleanup

commit a51f923
Author: Henry <[email protected]>
Date:   Sun Feb 5 21:27:57 2023 -0600

    cargo fmt

commit 332d7ee
Author: Henry <[email protected]>
Date:   Sun Feb 5 21:27:48 2023 -0600

    cleanup datamatrix

commit eb2311c
Author: Henry <[email protected]>
Date:   Sun Feb 5 21:27:26 2023 -0600

    cargo clippy --fix

commit 2abaa4c
Author: Henry <[email protected]>
Date:   Sun Feb 5 19:13:02 2023 -0600

    finish cleanup in parsers

commit 716aa2f
Author: Henry Schimke <[email protected]>
Date:   Sun Feb 5 17:44:56 2023 -0600

    cleanup continues

commit 7d84378
Author: Henry Schimke <[email protected]>
Date:   Sun Feb 5 15:15:09 2023 -0600

    common cleanup

commit fd25186
Author: Henry Schimke <[email protected]>
Date:   Sun Feb 5 10:24:54 2023 -0600

    cleanup reedsolomon

commit 132e27e
Author: Henry Schimke <[email protected]>
Date:   Sun Feb 5 09:52:04 2023 -0600

    make MathUtils more generic

commit d8352b7
Author: Henry Schimke <[email protected]>
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
  • Loading branch information
hschimke committed Feb 11, 2023
1 parent 5222bb2 commit 28723be
Show file tree
Hide file tree
Showing 180 changed files with 3,251 additions and 4,120 deletions.
2 changes: 1 addition & 1 deletion src/aztec/DecoderTest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{
RXingResultPoint,
};

use super::{decoder, AztecDetectorResult::AztecDetectorRXingResult};
use super::{aztec_detector_result::AztecDetectorRXingResult, decoder};

/**
* Tests {@link Decoder}.
Expand Down
8 changes: 4 additions & 4 deletions src/aztec/DetectorTest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn test_error_in_parameter_locator(data: &str) {
// Zooms a bit matrix so that each bit is factor x factor
fn make_larger(input: &BitMatrix, factor: u32) -> BitMatrix {
let width = input.getWidth();
let mut output = BitMatrix::with_single_dimension(width * factor);
let mut output = BitMatrix::with_single_dimension(width * factor).expect("new");
for inputY in 0..width {
// for (int inputY = 0; inputY < width; inputY++) {
for inputX in 0..width {
Expand Down Expand Up @@ -216,7 +216,7 @@ fn get_rotations(matrix0: &BitMatrix) -> Vec<BitMatrix> {
// Rotates a square BitMatrix to the right by 90 degrees
fn rotate_right(input: &BitMatrix) -> BitMatrix {
let width = input.getWidth();
let mut result = BitMatrix::with_single_dimension(width);
let mut result = BitMatrix::with_single_dimension(width).expect("new");
for x in 0..width {
// for (int x = 0; x < width; x++) {
for y in 0..width {
Expand All @@ -233,7 +233,7 @@ fn rotate_right(input: &BitMatrix) -> BitMatrix {
// matrix to the right, and then flipping it left-to-right
fn transpose(input: &BitMatrix) -> BitMatrix {
let width = input.getWidth();
let mut result = BitMatrix::with_single_dimension(width);
let mut result = BitMatrix::with_single_dimension(width).expect("new");
for x in 0..width {
// for (int x = 0; x < width; x++) {
for y in 0..width {
Expand All @@ -248,7 +248,7 @@ fn transpose(input: &BitMatrix) -> BitMatrix {

fn clone(input: &BitMatrix) -> BitMatrix {
let width = input.getWidth();
let mut result = BitMatrix::with_single_dimension(width);
let mut result = BitMatrix::with_single_dimension(width).expect("new");
for x in 0..width {
// for (int x = 0; x < width; x++) {
for y in 0..width {
Expand Down
4 changes: 2 additions & 2 deletions src/aztec/EncoderTest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ use encoding::EncodingRef;

use crate::{
aztec::{
aztec_detector_result::AztecDetectorRXingResult,
decoder,
encoder::HighLevelEncoder,
shared_test_methods::{stripSpace, toBitArray, toBooleanArray},
AztecDetectorResult::AztecDetectorRXingResult,
},
BarcodeFormat, EncodeHintType, EncodeHintValue, RXingResultPoint,
};
Expand Down Expand Up @@ -810,7 +810,7 @@ fn testModeMessageComplex(compact: bool, layers: u32, words: u32, expected: &str

fn testStuffBits(wordSize: usize, bits: &str, expected: &str) {
let indata = toBitArray(bits);
let stuffed = aztec_encoder::stuffBits(&indata, wordSize);
let stuffed = aztec_encoder::stuffBits(&indata, wordSize).unwrap();
assert_eq!(
stripSpace(expected),
stripSpace(&stuffed.to_string()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
*/
pub struct AztecDetectorRXingResult {
bits: BitMatrix,
points: Vec<RXingResultPoint>,
points: [RXingResultPoint; 4],
compact: bool,
nbDatablocks: u32,
nbLayers: u32,
Expand All @@ -59,7 +59,7 @@ impl AztecDetectorRXingResult {
) -> Self {
Self {
bits,
points: points.to_vec(),
points,
compact,
nbDatablocks,
nbLayers,
Expand Down
4 changes: 0 additions & 4 deletions src/aztec/aztec_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,4 @@ impl Reader for AztecReader {

Ok(result)
}

fn reset(&mut self) {
// do nothing
}
}
55 changes: 17 additions & 38 deletions src/aztec/aztec_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,25 @@ impl Writer for AztecWriter {
let mut charset = None; // Do not add any ECI code by default
let mut ecc_percent = aztec_encoder::DEFAULT_EC_PERCENT;
let mut layers = aztec_encoder::DEFAULT_AZTEC_LAYERS;
if hints.contains_key(&EncodeHintType::CHARACTER_SET) {
if let EncodeHintValue::CharacterSet(cset_name) = hints
.get(&EncodeHintType::CHARACTER_SET)
.expect("already knonw presence")
{
if cset_name != "iso-8859-1" {
charset = Some(encoding::label::encoding_from_whatwg_label(cset_name).unwrap());
}
// dbg!(cset_name);
// dbg!(encoding::label::encoding_from_whatwg_label(cset_name).unwrap().name(), encoding::label::encoding_from_whatwg_label(cset_name).unwrap().whatwg_name());
if let Some(EncodeHintValue::CharacterSet(cset_name)) =
hints.get(&EncodeHintType::CHARACTER_SET)
{
if cset_name.to_lowercase() != "iso-8859-1" {
charset = Some(
encoding::label::encoding_from_whatwg_label(cset_name)
.ok_or(Exceptions::IllegalArgumentException(None))?,
);
}
// charset = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString());
}
if hints.contains_key(&EncodeHintType::ERROR_CORRECTION) {
if let EncodeHintValue::ErrorCorrection(ecc_level) = hints
.get(&EncodeHintType::ERROR_CORRECTION)
.expect("key exists")
{
ecc_percent = ecc_level.parse().expect("should convert to int");
}
// eccPercent = Integer.parseInt(hints.get(EncodeHintType::ERROR_CORRECTION).toString());
if let Some(EncodeHintValue::ErrorCorrection(ecc_level)) =
hints.get(&EncodeHintType::ERROR_CORRECTION)
{
ecc_percent = ecc_level.parse().unwrap_or(23);
}
if hints.contains_key(&EncodeHintType::AZTEC_LAYERS) {
if let EncodeHintValue::AztecLayers(az_layers) = hints
.get(&EncodeHintType::AZTEC_LAYERS)
.expect("key exists")
{
layers = *az_layers;
}
// layers = Integer.parseInt(hints.get(EncodeHintType.AZTEC_LAYERS).toString());
if let Some(EncodeHintValue::AztecLayers(az_layers)) =
hints.get(&EncodeHintType::AZTEC_LAYERS)
{
layers = *az_layers;
}
encode(
contents,
Expand Down Expand Up @@ -121,9 +110,7 @@ fn encode(

fn renderRXingResult(code: &AztecCode, width: u32, height: u32) -> Result<BitMatrix, Exceptions> {
let input = code.getMatrix();
// if input == null {
// throw new IllegalStateException();
// }

let input_width = input.getWidth();
let input_height = input.getHeight();
let output_width = width.max(input_width);
Expand Down Expand Up @@ -152,13 +139,5 @@ fn renderRXingResult(code: &AztecCode, width: u32, height: u32) -> Result<BitMat
input_y += 1;
output_y += multiple
}
// for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
// // Write the contents of this row of the barcode
// for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
// if (input.get(inputX, inputY)) {
// output.setRegion(outputX, outputY, multiple, multiple);
// }
// }
// }
Ok(output)
}
17 changes: 13 additions & 4 deletions src/aztec/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
exceptions::Exceptions,
};

use super::AztecDetectorResult::AztecDetectorRXingResult;
use super::aztec_detector_result::AztecDetectorRXingResult;

/**
* <p>The main class which implements Aztec Code decoding -- as opposed to locating and extracting
Expand Down Expand Up @@ -164,7 +164,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String, Exceptions> {
result.push_str(
&encdr
.decode(&decoded_bytes, encoding::DecoderTrap::Strict)
.unwrap(),
.map_err(|a| Exceptions::IllegalStateException(Some(a.to_string())))?,
);

decoded_bytes.clear();
Expand Down Expand Up @@ -210,8 +210,17 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String, Exceptions> {
// That's including when that mode is a shift.
// Our test case dlusbs.png for issue #642 exercises that.
latch_table = shift_table; // Latch the current mode, so as to return to Upper after U/S B/S
shift_table = getTable(str.chars().nth(5).unwrap());
if str.chars().nth(6).unwrap() == 'L' {
shift_table = getTable(
str.chars()
.nth(5)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
);
if str
.chars()
.nth(6)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
== 'L'
{
latch_table = shift_table;
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/aztec/detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
RXingResultPoint, ResultPoint,
};

use super::AztecDetectorResult::AztecDetectorRXingResult;
use super::aztec_detector_result::AztecDetectorRXingResult;

const EXPECTED_CORNER_BITS: [u32; 4] = [
0xee0, // 07340 XXX .XX X.. ...
Expand Down Expand Up @@ -731,11 +731,11 @@ impl<'a> Detector<'_> {
}

fn distance_points(a: &Point, b: &Point) -> f32 {
MathUtils::distance_int(a.get_x(), a.get_y(), b.get_x(), b.get_y())
MathUtils::distance(a.get_x(), a.get_y(), b.get_x(), b.get_y())
}

fn distance(a: &RXingResultPoint, b: &RXingResultPoint) -> f32 {
MathUtils::distance_float(a.getX(), a.getY(), b.getX(), b.getY())
MathUtils::distance(a.getX(), a.getY(), b.getX(), b.getY())
}

fn get_dimension(&self) -> u32 {
Expand Down
59 changes: 16 additions & 43 deletions src/aztec/encoder/aztec_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub fn encode_bytes_with_charset(
total_bits_in_layer_var = total_bits_in_layer(layers, compact);
word_size = WORD_SIZE[layers as usize];
let usable_bits_in_layers = total_bits_in_layer_var - (total_bits_in_layer_var % word_size);
stuffed_bits = stuffBits(&bits, word_size as usize);
stuffed_bits = stuffBits(&bits, word_size as usize)?;
if stuffed_bits.getSize() as u32 + ecc_bits > usable_bits_in_layers {
return Err(Exceptions::IllegalArgumentException(Some(
"Data to large for user specified layer".to_owned(),
Expand Down Expand Up @@ -222,7 +222,7 @@ pub fn encode_bytes_with_charset(
// wordSize has changed
if stuffed_bits.getSize() == 0 || word_size != WORD_SIZE[layers as usize] {
word_size = WORD_SIZE[layers as usize];
stuffed_bits = stuffBits(&bits, word_size as usize);
stuffed_bits = stuffBits(&bits, word_size as usize)?;
}
let usable_bits_in_layers =
total_bits_in_layer_var - (total_bits_in_layer_var % word_size);
Expand Down Expand Up @@ -267,7 +267,7 @@ pub fn encode_bytes_with_charset(
alignmentMap[origCenter + i] = center + newOffset + 1;
}
}
let mut matrix = BitMatrix::with_single_dimension(matrixSize);
let mut matrix = BitMatrix::with_single_dimension(matrixSize)?;

// dbg!(matrix.to_string());

Expand Down Expand Up @@ -307,13 +307,9 @@ pub fn encode_bytes_with_charset(
rowOffset += rowSize * 8;
}

// dbg!(matrix.to_string());

// draw mode message
drawModeMessage(&mut matrix, compact, matrixSize, modeMessage);

// dbg!(matrix.to_string());

// draw alignment marks
if compact {
drawBullsEye(&mut matrix, matrixSize / 2, 5);
Expand Down Expand Up @@ -385,20 +381,12 @@ pub fn generateModeMessage(
) -> Result<BitArray, Exceptions> {
let mut mode_message = BitArray::new();
if compact {
mode_message
.appendBits(layers - 1, 2)
.expect("should append");
mode_message
.appendBits(messageSizeInWords - 1, 6)
.expect("should append");
mode_message.appendBits(layers - 1, 2)?;
mode_message.appendBits(messageSizeInWords - 1, 6)?;
mode_message = generateCheckWords(&mode_message, 28, 4)?;
} else {
mode_message
.appendBits(layers - 1, 5)
.expect("should append");
mode_message
.appendBits(messageSizeInWords - 1, 11)
.expect("should append");
mode_message.appendBits(layers - 1, 5)?;
mode_message.appendBits(messageSizeInWords - 1, 11)?;
mode_message = generateCheckWords(&mode_message, 40, 4)?;
}
Ok(mode_message)
Expand All @@ -407,7 +395,7 @@ pub fn generateModeMessage(
fn drawModeMessage(matrix: &mut BitMatrix, compact: bool, matrixSize: u32, modeMessage: BitArray) {
let center = matrixSize / 2;
if compact {
for i in 0..7usize {
for i in 0..7_usize {
// for (int i = 0; i < 7; i++) {
let offset = (center as usize - 3 + i) as u32;
if modeMessage.get(i) {
Expand All @@ -424,7 +412,7 @@ fn drawModeMessage(matrix: &mut BitMatrix, compact: bool, matrixSize: u32, modeM
}
}
} else {
for i in 0..10usize {
for i in 0..10_usize {
// for (int i = 0; i < 10; i++) {
let offset = (center as usize - 5 + i + i / 5) as u32;
if modeMessage.get(i) {
Expand All @@ -450,13 +438,13 @@ fn generateCheckWords(
) -> Result<BitArray, Exceptions> {
// bitArray is guaranteed to be a multiple of the wordSize, so no padding needed
let message_size_in_words = bitArray.getSize() / wordSize;
let mut rs = ReedSolomonEncoder::new(getGF(wordSize)?);
let mut rs = ReedSolomonEncoder::new(getGF(wordSize)?)?;
let total_words = totalBits / wordSize;
let mut message_words = bitsToWords(bitArray, wordSize, total_words);
rs.encode(&mut message_words, total_words - message_size_in_words)?;
let start_pad = totalBits % wordSize;
let mut message_bits = BitArray::new();
message_bits.appendBits(0, start_pad).expect("must append");
message_bits.appendBits(0, start_pad)?;
for message_word in message_words {
// for (int messageWord : messageWords) {
message_bits.appendBits(message_word as u32, wordSize)?
Expand Down Expand Up @@ -498,23 +486,9 @@ fn getGF(wordSize: usize) -> Result<GenericGFRef, Exceptions> {
"Unsupported word size {wordSize}"
)))),
}
// switch (wordSize) {
// case 4:
// return GenericGF.AZTEC_PARAM;
// case 6:
// return GenericGF.AZTEC_DATA_6;
// case 8:
// return GenericGF.AZTEC_DATA_8;
// case 10:
// return GenericGF.AZTEC_DATA_10;
// case 12:
// return GenericGF.AZTEC_DATA_12;
// default:
// throw new IllegalArgumentException("Unsupported word size " + wordSize);
// }
}

pub fn stuffBits(bits: &BitArray, word_size: usize) -> BitArray {
pub fn stuffBits(bits: &BitArray, word_size: usize) -> Result<BitArray, Exceptions> {
let mut out = BitArray::new();

let n = bits.getSize() as isize;
Expand All @@ -530,21 +504,20 @@ pub fn stuffBits(bits: &BitArray, word_size: usize) -> BitArray {
}
}
if (word & mask) == mask {
out.appendBits(word & mask, word_size).unwrap();
out.appendBits(word & mask, word_size)?;
i -= 1;
} else if (word & mask) == 0 {
out.appendBits(word | 1, word_size).unwrap();
out.appendBits(word | 1, word_size)?;
i -= 1;
} else {
out.appendBits(word, word_size).unwrap();
out.appendBits(word, word_size)?;
}

i += word_size as isize;
}
out
Ok(out)
}

fn total_bits_in_layer(layers: u32, compact: bool) -> u32 {
((if compact { 88 } else { 112 }) + 16 * layers) * layers
// return ((compact ? 88 : 112) + 16 * layers) * layers;
}
Loading

0 comments on commit 28723be

Please sign in to comment.