Merge branch 'main' into pr/point_refactor

This commit is contained in:
Vukašin Stepanović
2023-02-16 07:19:36 +00:00
161 changed files with 859 additions and 906 deletions

View File

@@ -17,7 +17,7 @@
use std::collections::HashMap;
use crate::{
common::{DecoderRXingResult, DetectorRXingResult},
common::{DecoderRXingResult, DetectorRXingResult, Result},
exceptions::Exceptions,
BarcodeFormat, BinaryBitmap, DecodeHintType, DecodeHintValue, RXingResult,
RXingResultMetadataType, RXingResultMetadataValue, Reader,
@@ -41,7 +41,7 @@ impl Reader for AztecReader {
* @throws NotFoundException if a Data Matrix code cannot be found
* @throws FormatException if a Data Matrix code cannot be decoded
*/
fn decode(&mut self, image: &mut BinaryBitmap) -> Result<RXingResult, Exceptions> {
fn decode(&mut self, image: &mut BinaryBitmap) -> Result<RXingResult> {
self.decode_with_hints(image, &HashMap::new())
}
@@ -49,7 +49,7 @@ impl Reader for AztecReader {
&mut self,
image: &mut BinaryBitmap,
hints: &HashMap<DecodeHintType, DecodeHintValue>,
) -> Result<RXingResult, Exceptions> {
) -> Result<RXingResult> {
// let notFoundException = None;
// let formatException = None;
let mut detector = Detector::new(image.getBlackMatrix());

View File

@@ -19,8 +19,9 @@ use std::collections::HashMap;
use encoding::EncodingRef;
use crate::{
common::BitMatrix, exceptions::Exceptions, BarcodeFormat, EncodeHintType, EncodeHintValue,
Writer,
common::{BitMatrix, Result},
exceptions::Exceptions,
BarcodeFormat, EncodeHintType, EncodeHintValue, Writer,
};
use super::encoder::{aztec_encoder, AztecCode};
@@ -38,7 +39,7 @@ impl Writer for AztecWriter {
format: &crate::BarcodeFormat,
width: i32,
height: i32,
) -> Result<crate::common::BitMatrix, crate::exceptions::Exceptions> {
) -> Result<crate::common::BitMatrix> {
self.encode_with_hints(contents, format, width, height, &HashMap::new())
}
@@ -49,7 +50,7 @@ impl Writer for AztecWriter {
width: i32,
height: i32,
hints: &std::collections::HashMap<crate::EncodeHintType, crate::EncodeHintValue>,
) -> Result<crate::common::BitMatrix, crate::exceptions::Exceptions> {
) -> Result<crate::common::BitMatrix> {
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;
@@ -93,7 +94,7 @@ fn encode(
charset: Option<EncodingRef>,
ecc_percent: u32,
layers: i32,
) -> Result<BitMatrix, Exceptions> {
) -> Result<BitMatrix> {
if format != BarcodeFormat::AZTEC {
return Err(Exceptions::IllegalArgumentException(Some(format!(
"can only encode AZTEC, but got {format:?}"
@@ -108,7 +109,7 @@ fn encode(
renderRXingResult(&aztec, width, height)
}
fn renderRXingResult(code: &AztecCode, width: u32, height: u32) -> Result<BitMatrix, Exceptions> {
fn renderRXingResult(code: &AztecCode, width: u32, height: u32) -> Result<BitMatrix> {
let input = code.getMatrix();
let input_width = input.getWidth();

View File

@@ -19,7 +19,7 @@ use crate::{
reedsolomon::{
get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonDecoder,
},
BitMatrix, CharacterSetECI, DecoderRXingResult, DetectorRXingResult,
BitMatrix, CharacterSetECI, DecoderRXingResult, DetectorRXingResult, Result,
},
exceptions::Exceptions,
};
@@ -73,9 +73,7 @@ const DIGIT_TABLE: [&str; 16] = [
// private AztecDetectorRXingResult ddata;
pub fn decode(
detectorRXingResult: &AztecDetectorRXingResult,
) -> Result<DecoderRXingResult, Exceptions> {
pub fn decode(detectorRXingResult: &AztecDetectorRXingResult) -> Result<DecoderRXingResult> {
//let mut detectorRXingResult = detectorRXingResult.clone();
let matrix = detectorRXingResult.getBits();
let rawbits = extract_bits(detectorRXingResult, matrix);
@@ -94,7 +92,7 @@ pub fn decode(
}
/// This method is used for testing the high-level encoder
pub fn highLevelDecode(correctedBits: &[bool]) -> Result<String, Exceptions> {
pub fn highLevelDecode(correctedBits: &[bool]) -> Result<String> {
get_encoded_data(correctedBits)
}
@@ -103,7 +101,7 @@ pub fn highLevelDecode(correctedBits: &[bool]) -> Result<String, Exceptions> {
*
* @return the decoded string
*/
fn get_encoded_data(corrected_bits: &[bool]) -> Result<String, Exceptions> {
fn get_encoded_data(corrected_bits: &[bool]) -> Result<String> {
let end_index = corrected_bits.len();
let mut latch_table = Table::Upper; // table most recently latched to
let mut shift_table = Table::Upper; // table to use for the next read
@@ -288,7 +286,7 @@ fn getTable(t: char) -> Table {
* @param table the table used
* @param code the code of the character
*/
fn get_character(table: Table, code: u32) -> Result<&'static str, Exceptions> {
fn get_character(table: Table, code: u32) -> Result<&'static str> {
match table {
Table::Upper => Ok(UPPER_TABLE[code as usize]),
Table::Lower => Ok(LOWER_TABLE[code as usize]),
@@ -337,7 +335,7 @@ impl CorrectedBitsRXingResult {
fn correct_bits(
ddata: &AztecDetectorRXingResult,
rawbits: &[bool],
) -> Result<CorrectedBitsRXingResult, Exceptions> {
) -> Result<CorrectedBitsRXingResult> {
let gf: GenericGFRef;
let codeword_size;

View File

@@ -20,7 +20,7 @@ use crate::{
common::{
detector::{MathUtils, WhiteRectangleDetector},
reedsolomon::{self, ReedSolomonDecoder},
BitMatrix, DefaultGridSampler, GridSampler,
BitMatrix, DefaultGridSampler, GridSampler, Result,
},
exceptions::Exceptions,
RXingResultPoint, ResultPoint,
@@ -64,7 +64,7 @@ impl<'a> Detector<'_> {
}
}
pub fn detect_false(&mut self) -> Result<AztecDetectorRXingResult, Exceptions> {
pub fn detect_false(&mut self) -> Result<AztecDetectorRXingResult> {
self.detect(false)
}
@@ -75,7 +75,7 @@ impl<'a> Detector<'_> {
* @return {@link AztecDetectorRXingResult} encapsulating results of detecting an Aztec Code
* @throws NotFoundException if no Aztec Code can be found
*/
pub fn detect(&mut self, is_mirror: bool) -> Result<AztecDetectorRXingResult, Exceptions> {
pub fn detect(&mut self, is_mirror: bool) -> Result<AztecDetectorRXingResult> {
// dbg!(self.image.to_string());
// 1. Get the center of the aztec matrix
let p_center = self.get_matrix_center();
@@ -118,10 +118,7 @@ impl<'a> Detector<'_> {
* @param bullsEyeCorners the array of bull's eye corners
* @throws NotFoundException in case of too many errors or invalid parameters
*/
fn extractParameters(
&mut self,
bulls_eye_corners: &[RXingResultPoint],
) -> Result<(), Exceptions> {
fn extractParameters(&mut self, bulls_eye_corners: &[RXingResultPoint]) -> Result<()> {
if !self.is_valid(bulls_eye_corners[0])
|| !self.is_valid(bulls_eye_corners[1])
|| !self.is_valid(bulls_eye_corners[2])
@@ -179,7 +176,7 @@ impl<'a> Detector<'_> {
Ok(())
}
fn get_rotation(sides: &[u32], length: u32) -> Result<u32, Exceptions> {
fn get_rotation(sides: &[u32], length: u32) -> Result<u32> {
// In a normal pattern, we expect to See
// ** .* D A
// * *
@@ -222,7 +219,7 @@ impl<'a> Detector<'_> {
* @param compact true if this is a compact Aztec code
* @throws NotFoundException if the array contains too many errors
*/
fn get_corrected_parameter_data(parameterData: u64, compact: bool) -> Result<u32, Exceptions> {
fn get_corrected_parameter_data(parameterData: u64, compact: bool) -> Result<u32> {
let mut parameter_data = parameterData;
let num_codewords: i32;
@@ -269,10 +266,7 @@ impl<'a> Detector<'_> {
* @return The corners of the bull-eye
* @throws NotFoundException If no valid bull-eye can be found
*/
fn get_bulls_eye_corners(
&mut self,
pCenter: Point,
) -> Result<[RXingResultPoint; 4], Exceptions> {
fn get_bulls_eye_corners(&mut self, pCenter: Point) -> Result<[RXingResultPoint; 4]> {
let mut pina = pCenter;
let mut pinb = pCenter;
let mut pinc = pCenter;
@@ -504,7 +498,7 @@ impl<'a> Detector<'_> {
top_right: RXingResultPoint,
bottom_right: RXingResultPoint,
bottom_left: RXingResultPoint,
) -> Result<BitMatrix, Exceptions> {
) -> Result<BitMatrix> {
let sampler = DefaultGridSampler::default();
let dimension = self.get_dimension();

View File

@@ -21,7 +21,7 @@ use crate::{
reedsolomon::{
get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonEncoder,
},
BitArray, BitMatrix,
BitArray, BitMatrix, Result,
},
exceptions::Exceptions,
};
@@ -50,7 +50,7 @@ pub const WORD_SIZE: [u32; 33] = [
* @param data input data string; must be encodable as ISO/IEC 8859-1 (Latin-1)
* @return Aztec symbol matrix with metadata
*/
pub fn encode_simple(data: &str) -> Result<AztecCode, Exceptions> {
pub fn encode_simple(data: &str) -> Result<AztecCode> {
let Ok(bytes) = encoding::all::ISO_8859_1
.encode(data, encoding::EncoderTrap::Replace) else {
return Err(Exceptions::IllegalArgumentException(Some(format!("'{data}' cannot be encoded as ISO_8859_1"))));
@@ -67,11 +67,7 @@ pub fn encode_simple(data: &str) -> Result<AztecCode, Exceptions> {
* @param userSpecifiedLayers if non-zero, a user-specified value for the number of layers
* @return Aztec symbol matrix with metadata
*/
pub fn encode(
data: &str,
minECCPercent: u32,
userSpecifiedLayers: i32,
) -> Result<AztecCode, Exceptions> {
pub fn encode(data: &str, minECCPercent: u32, userSpecifiedLayers: i32) -> Result<AztecCode> {
if let Ok(bytes) = encoding::all::ISO_8859_1.encode(data, encoding::EncoderTrap::Strict) {
encode_bytes(&bytes, minECCPercent, userSpecifiedLayers)
} else {
@@ -98,7 +94,7 @@ pub fn encode_with_charset(
minECCPercent: u32,
userSpecifiedLayers: i32,
charset: encoding::EncodingRef,
) -> Result<AztecCode, Exceptions> {
) -> Result<AztecCode> {
if let Ok(bytes) = charset.encode(data, encoding::EncoderTrap::Strict) {
encode_bytes_with_charset(&bytes, minECCPercent, userSpecifiedLayers, charset)
} else {
@@ -114,7 +110,7 @@ pub fn encode_with_charset(
* @param data input data string
* @return Aztec symbol matrix with metadata
*/
pub fn encode_bytes_simple(data: &[u8]) -> Result<AztecCode, Exceptions> {
pub fn encode_bytes_simple(data: &[u8]) -> Result<AztecCode> {
encode_bytes(data, DEFAULT_EC_PERCENT, DEFAULT_AZTEC_LAYERS)
}
@@ -131,7 +127,7 @@ pub fn encode_bytes(
data: &[u8],
minECCPercent: u32,
userSpecifiedLayers: i32,
) -> Result<AztecCode, Exceptions> {
) -> Result<AztecCode> {
encode_bytes_with_charset(
data,
minECCPercent,
@@ -156,7 +152,7 @@ pub fn encode_bytes_with_charset(
min_eccpercent: u32,
user_specified_layers: i32,
charset: encoding::EncodingRef,
) -> Result<AztecCode, Exceptions> {
) -> Result<AztecCode> {
// High-level encode
let bits = HighLevelEncoder::with_charset(data.into(), charset).encode()?;
@@ -378,7 +374,7 @@ pub fn generateModeMessage(
compact: bool,
layers: u32,
messageSizeInWords: u32,
) -> Result<BitArray, Exceptions> {
) -> Result<BitArray> {
let mut mode_message = BitArray::new();
if compact {
mode_message.appendBits(layers - 1, 2)?;
@@ -431,11 +427,7 @@ fn drawModeMessage(matrix: &mut BitMatrix, compact: bool, matrixSize: u32, modeM
}
}
fn generateCheckWords(
bitArray: &BitArray,
totalBits: usize,
wordSize: usize,
) -> Result<BitArray, Exceptions> {
fn generateCheckWords(bitArray: &BitArray, totalBits: usize, wordSize: usize) -> Result<BitArray> {
// 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)?)?;
@@ -475,7 +467,7 @@ fn bitsToWords(stuffedBits: &BitArray, wordSize: usize, totalWords: usize) -> Ve
message
}
fn getGF(wordSize: usize) -> Result<GenericGFRef, Exceptions> {
fn getGF(wordSize: usize) -> Result<GenericGFRef> {
match wordSize {
4 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecParam)),
6 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecData6)),
@@ -488,7 +480,7 @@ fn getGF(wordSize: usize) -> Result<GenericGFRef, Exceptions> {
}
}
pub fn stuffBits(bits: &BitArray, word_size: usize) -> Result<BitArray, Exceptions> {
pub fn stuffBits(bits: &BitArray, word_size: usize) -> Result<BitArray> {
let mut out = BitArray::new();
let n = bits.getSize() as isize;

View File

@@ -16,7 +16,7 @@
use std::fmt;
use crate::{common::BitArray, Exceptions};
use crate::common::{BitArray, Result};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BinaryShiftToken {
@@ -32,7 +32,7 @@ impl BinaryShiftToken {
}
}
pub fn appendTo(&self, bit_array: &mut BitArray, text: &[u8]) -> Result<(), Exceptions> {
pub fn appendTo(&self, bit_array: &mut BitArray, text: &[u8]) -> Result<()> {
let bsbc = self.binary_shift_byte_count as usize;
for i in 0..bsbc {
// for (int i = 0; i < bsbc; i++) {

View File

@@ -15,7 +15,7 @@
*/
use crate::{
common::{BitArray, CharacterSetECI},
common::{BitArray, CharacterSetECI, Result},
exceptions::Exceptions,
};
@@ -240,7 +240,7 @@ impl HighLevelEncoder {
/**
* @return text represented by this encoder encoded as a {@link BitArray}
*/
pub fn encode(&self) -> Result<BitArray, Exceptions> {
pub fn encode(&self) -> Result<BitArray> {
let mut initial_state = State::new(Token::new(), Self::MODE_UPPER as u32, 0, 0);
if let Some(eci) = CharacterSetECI::getCharacterSetECI(self.charset) {
if eci != CharacterSetECI::ISO8859_1 {

View File

@@ -16,7 +16,7 @@
use std::fmt;
use crate::{common::BitArray, Exceptions};
use crate::common::{BitArray, Result};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SimpleToken {
@@ -33,7 +33,7 @@ impl SimpleToken {
}
}
pub fn appendTo(&self, bit_array: &mut BitArray, _text: &[u8]) -> Result<(), Exceptions> {
pub fn appendTo(&self, bit_array: &mut BitArray, _text: &[u8]) -> Result<()> {
bit_array.appendBits(self.value as u32, self.bit_count as usize)
}

View File

@@ -18,7 +18,10 @@ use std::fmt;
use encoding::Encoding;
use crate::{common::BitArray, exceptions::Exceptions};
use crate::{
common::{BitArray, Result},
exceptions::Exceptions,
};
use super::{HighLevelEncoder, Token};
@@ -70,7 +73,7 @@ impl State {
self.bit_count
}
pub fn appendFLGn(self, eci: u32) -> Result<Self, Exceptions> {
pub fn appendFLGn(self, eci: u32) -> Result<Self> {
let bit_count = self.bit_count;
let mode = self.mode;
let result = self.shiftAndAppend(HighLevelEncoder::MODE_PUNCT as u32, 0); // 0: FLG(n)
@@ -207,7 +210,7 @@ impl State {
new_mode_bit_count <= other.bit_count
}
pub fn toBitArray(self, text: &[u8]) -> Result<BitArray, Exceptions> {
pub fn toBitArray(self, text: &[u8]) -> Result<BitArray> {
let mut symbols = Vec::new();
let tok = self.endBinaryShift(text.len() as u32).token;
for tkn in tok.into_iter() {

View File

@@ -14,7 +14,10 @@
* limitations under the License.
*/
use crate::{common::BitArray, Exceptions};
use crate::{
common::{BitArray, Result},
Exceptions,
};
use super::{BinaryShiftToken, SimpleToken};
@@ -26,7 +29,7 @@ pub enum TokenType {
}
impl TokenType {
pub fn appendTo(&self, bit_array: &mut BitArray, text: &[u8]) -> Result<(), Exceptions> {
pub fn appendTo(&self, bit_array: &mut BitArray, text: &[u8]) -> Result<()> {
// let token = &self.tokens[self.current_pointer];
match self {
TokenType::Simple(a) => a.appendTo(bit_array, text),