mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 12:22:34 +00:00
Merge branch 'main' into pr/point_refactor
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
|
||||
use std::{cmp, fmt};
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
static LOAD_FACTOR: f32 = 0.75f32;
|
||||
@@ -165,7 +166,7 @@ impl BitArray {
|
||||
* @param start start of range, inclusive.
|
||||
* @param end end of range, exclusive
|
||||
*/
|
||||
pub fn setRange(&mut self, start: usize, end: usize) -> Result<(), Exceptions> {
|
||||
pub fn setRange(&mut self, start: usize, end: usize) -> Result<()> {
|
||||
let mut end = end;
|
||||
if end < start || end > self.size {
|
||||
return Err(Exceptions::IllegalArgumentException(None));
|
||||
@@ -208,7 +209,7 @@ impl BitArray {
|
||||
* @return true iff all bits are set or not set in range, according to value argument
|
||||
* @throws IllegalArgumentException if end is less than start or the range is not contained in the array
|
||||
*/
|
||||
pub fn isRange(&self, start: usize, end: usize, value: bool) -> Result<bool, Exceptions> {
|
||||
pub fn isRange(&self, start: usize, end: usize, value: bool) -> Result<bool> {
|
||||
let mut end = end;
|
||||
if end < start || end > self.size {
|
||||
return Err(Exceptions::IllegalArgumentException(None));
|
||||
@@ -251,7 +252,7 @@ impl BitArray {
|
||||
* @param value {@code int} containing bits to append
|
||||
* @param numBits bits from value to append
|
||||
*/
|
||||
pub fn appendBits(&mut self, value: u32, num_bits: usize) -> Result<(), Exceptions> {
|
||||
pub fn appendBits(&mut self, value: u32, num_bits: usize) -> Result<()> {
|
||||
if num_bits > 32 {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"num bits must be between 0 and 32".to_owned(),
|
||||
@@ -284,7 +285,7 @@ impl BitArray {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn xor(&mut self, other: &BitArray) -> Result<(), Exceptions> {
|
||||
pub fn xor(&mut self, other: &BitArray) -> Result<()> {
|
||||
if self.size != other.size {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Sizes don't match".to_owned(),
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::{Exceptions, RXingResultPoint};
|
||||
|
||||
use super::BitArray;
|
||||
@@ -53,7 +54,7 @@ impl BitMatrix {
|
||||
*
|
||||
* @param dimension height and width
|
||||
*/
|
||||
pub fn with_single_dimension(dimension: u32) -> Result<Self, Exceptions> {
|
||||
pub fn with_single_dimension(dimension: u32) -> Result<Self> {
|
||||
Self::new(dimension, dimension)
|
||||
}
|
||||
|
||||
@@ -63,7 +64,7 @@ impl BitMatrix {
|
||||
* @param width bit matrix width
|
||||
* @param height bit matrix height
|
||||
*/
|
||||
pub fn new(width: u32, height: u32) -> Result<Self, Exceptions> {
|
||||
pub fn new(width: u32, height: u32) -> Result<Self> {
|
||||
if width < 1 || height < 1 {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Both dimensions must be greater than 0".to_owned(),
|
||||
@@ -120,7 +121,7 @@ impl BitMatrix {
|
||||
string_representation: &str,
|
||||
set_string: &str,
|
||||
unset_string: &str,
|
||||
) -> Result<Self, Exceptions> {
|
||||
) -> Result<Self> {
|
||||
// cannot pass nulls in rust
|
||||
// if (stringRepresentation == null) {
|
||||
// throw new IllegalArgumentException();
|
||||
@@ -308,7 +309,7 @@ impl BitMatrix {
|
||||
*
|
||||
* @param mask XOR mask
|
||||
*/
|
||||
pub fn xor(&mut self, mask: &BitMatrix) -> Result<(), Exceptions> {
|
||||
pub fn xor(&mut self, mask: &BitMatrix) -> Result<()> {
|
||||
if self.width != mask.width || self.height != mask.height || self.row_size != mask.row_size
|
||||
{
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
@@ -350,13 +351,7 @@ impl BitMatrix {
|
||||
* @param width The width of the region
|
||||
* @param height The height of the region
|
||||
*/
|
||||
pub fn setRegion(
|
||||
&mut self,
|
||||
left: u32,
|
||||
top: u32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
) -> Result<(), Exceptions> {
|
||||
pub fn setRegion(&mut self, left: u32, top: u32, width: u32, height: u32) -> Result<()> {
|
||||
// if top < 0 || left < 0 {
|
||||
// return Err(Exceptions::IllegalArgumentException(
|
||||
// "Left and top must be nonnegative".to_owned(),
|
||||
@@ -428,7 +423,7 @@ impl BitMatrix {
|
||||
*
|
||||
* @param degrees number of degrees to rotate through counter-clockwise (0, 90, 180, 270)
|
||||
*/
|
||||
pub fn rotate(&mut self, degrees: u32) -> Result<(), Exceptions> {
|
||||
pub fn rotate(&mut self, degrees: u32) -> Result<()> {
|
||||
match degrees % 360 {
|
||||
0 => Ok(()),
|
||||
90 => {
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
use std::cmp;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
/**
|
||||
@@ -68,7 +69,7 @@ impl BitSource {
|
||||
* bits of the int
|
||||
* @throws IllegalArgumentException if numBits isn't in [1,32] or more than is available
|
||||
*/
|
||||
pub fn readBits(&mut self, numBits: usize) -> Result<u32, Exceptions> {
|
||||
pub fn readBits(&mut self, numBits: usize) -> Result<u32> {
|
||||
if !(1..=32).contains(&numBits) || numBits > self.available() {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
numBits.to_string(),
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
use encoding::EncodingRef;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
/**
|
||||
@@ -215,7 +216,7 @@ impl CharacterSetECI {
|
||||
* unsupported
|
||||
* @throws FormatException if ECI value is invalid
|
||||
*/
|
||||
pub fn getCharacterSetECIByValue(value: u32) -> Result<CharacterSetECI, Exceptions> {
|
||||
pub fn getCharacterSetECIByValue(value: u32) -> Result<CharacterSetECI> {
|
||||
match value {
|
||||
0 | 2 => Ok(CharacterSetECI::Cp437),
|
||||
1 | 3 => Ok(CharacterSetECI::ISO8859_1),
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
// import com.google.zxing.NotFoundException;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{BitMatrix, GridSampler, PerspectiveTransform};
|
||||
@@ -50,7 +51,7 @@ impl GridSampler for DefaultGridSampler {
|
||||
p3FromY: f32,
|
||||
p4FromX: f32,
|
||||
p4FromY: f32,
|
||||
) -> Result<BitMatrix, Exceptions> {
|
||||
) -> Result<BitMatrix> {
|
||||
let transform = PerspectiveTransform::quadrilateralToQuadrilateral(
|
||||
p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX,
|
||||
p2FromY, p3FromX, p3FromY, p4FromX, p4FromY,
|
||||
@@ -65,7 +66,7 @@ impl GridSampler for DefaultGridSampler {
|
||||
dimensionX: u32,
|
||||
dimensionY: u32,
|
||||
transform: &PerspectiveTransform,
|
||||
) -> Result<BitMatrix, Exceptions> {
|
||||
) -> Result<BitMatrix> {
|
||||
if dimensionX == 0 || dimensionY == 0 {
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
@@ -17,7 +17,10 @@
|
||||
|
||||
//package com.google.zxing.common.detector;
|
||||
|
||||
use crate::{common::BitMatrix, Exceptions, RXingResultPoint, ResultPoint};
|
||||
use crate::{
|
||||
common::{BitMatrix, Result},
|
||||
Exceptions, RXingResultPoint, ResultPoint,
|
||||
};
|
||||
|
||||
/**
|
||||
* <p>A somewhat generic detector that looks for a barcode-like rectangular region within an image.
|
||||
@@ -48,7 +51,7 @@ impl<'a> MonochromeRectangleDetector<'_> {
|
||||
* third, the rightmost
|
||||
* @throws NotFoundException if no Data Matrix Code can be found
|
||||
*/
|
||||
pub fn detect(&self) -> Result<[RXingResultPoint; 4], Exceptions> {
|
||||
pub fn detect(&self) -> Result<[RXingResultPoint; 4]> {
|
||||
let height = self.image.getHeight() as i32;
|
||||
let width = self.image.getWidth() as i32;
|
||||
let halfHeight = height / 2;
|
||||
@@ -155,7 +158,7 @@ impl<'a> MonochromeRectangleDetector<'_> {
|
||||
top: i32,
|
||||
bottom: i32,
|
||||
maxWhiteRun: i32,
|
||||
) -> Result<RXingResultPoint, Exceptions> {
|
||||
) -> Result<RXingResultPoint> {
|
||||
let mut lastRange_z: Option<[i32; 2]> = None;
|
||||
let mut y: i32 = centerY;
|
||||
let mut x: i32 = centerX;
|
||||
|
||||
@@ -16,7 +16,10 @@
|
||||
|
||||
//package com.google.zxing.common.detector;
|
||||
|
||||
use crate::{common::BitMatrix, Exceptions, RXingResultPoint, ResultPoint};
|
||||
use crate::{
|
||||
common::{BitMatrix, Result},
|
||||
Exceptions, RXingResultPoint, ResultPoint,
|
||||
};
|
||||
|
||||
use super::MathUtils;
|
||||
|
||||
@@ -43,7 +46,7 @@ pub struct WhiteRectangleDetector<'a> {
|
||||
}
|
||||
|
||||
impl<'a> WhiteRectangleDetector<'_> {
|
||||
pub fn new_from_image(image: &'a BitMatrix) -> Result<WhiteRectangleDetector<'a>, Exceptions> {
|
||||
pub fn new_from_image(image: &'a BitMatrix) -> Result<WhiteRectangleDetector<'a>> {
|
||||
WhiteRectangleDetector::new(
|
||||
image,
|
||||
INIT_SIZE,
|
||||
@@ -64,7 +67,7 @@ impl<'a> WhiteRectangleDetector<'_> {
|
||||
initSize: i32,
|
||||
x: i32,
|
||||
y: i32,
|
||||
) -> Result<WhiteRectangleDetector<'a>, Exceptions> {
|
||||
) -> Result<WhiteRectangleDetector<'a>> {
|
||||
let halfsize = initSize / 2;
|
||||
|
||||
let leftInit = x - halfsize;
|
||||
@@ -105,7 +108,7 @@ impl<'a> WhiteRectangleDetector<'_> {
|
||||
* leftmost and the third, the rightmost
|
||||
* @throws NotFoundException if no Data Matrix Code can be found
|
||||
*/
|
||||
pub fn detect(&self) -> Result<[RXingResultPoint; 4], Exceptions> {
|
||||
pub fn detect(&self) -> Result<[RXingResultPoint; 4]> {
|
||||
let mut left: i32 = self.leftInit;
|
||||
let mut right: i32 = self.rightInit;
|
||||
let mut up: i32 = self.upInit;
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::Exceptions;
|
||||
use crate::common::Result;
|
||||
|
||||
/**
|
||||
* Interface to navigate a sequence of ECIs and bytes.
|
||||
@@ -50,7 +50,7 @@ pub trait ECIInput: Display {
|
||||
* @throws IllegalArgumentException
|
||||
* if the value at the {@code index} argument is an ECI (@see #isECI)
|
||||
*/
|
||||
fn charAt(&self, index: usize) -> Result<char, Exceptions>;
|
||||
fn charAt(&self, index: usize) -> Result<char>;
|
||||
|
||||
/**
|
||||
* Returns a {@code CharSequence} that is a subsequence of this sequence.
|
||||
@@ -72,7 +72,7 @@ pub trait ECIInput: Display {
|
||||
* @throws IllegalArgumentException
|
||||
* if a value in the range {@code start}-{@code end} is an ECI (@see #isECI)
|
||||
*/
|
||||
fn subSequence(&self, start: usize, end: usize) -> Result<Vec<char>, Exceptions>;
|
||||
fn subSequence(&self, start: usize, end: usize) -> Result<Vec<char>>;
|
||||
|
||||
/**
|
||||
* Determines if a value is an ECI
|
||||
@@ -85,7 +85,7 @@ pub trait ECIInput: Display {
|
||||
* if the {@code index} argument is negative or not less than
|
||||
* {@code length()}
|
||||
*/
|
||||
fn isECI(&self, index: u32) -> Result<bool, Exceptions>;
|
||||
fn isECI(&self, index: u32) -> Result<bool>;
|
||||
|
||||
/**
|
||||
* Returns the {@code int} ECI value at the specified index. An index ranges from zero
|
||||
@@ -105,6 +105,6 @@ pub trait ECIInput: Display {
|
||||
* @throws IllegalArgumentException
|
||||
* if the value at the {@code index} argument is not an ECI (@see #isECI)
|
||||
*/
|
||||
fn getECIValue(&self, index: usize) -> Result<i32, Exceptions>;
|
||||
fn haveNCharacters(&self, index: usize, n: usize) -> Result<bool, Exceptions>;
|
||||
fn getECIValue(&self, index: usize) -> Result<i32>;
|
||||
fn haveNCharacters(&self, index: usize, n: usize) -> Result<bool>;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ use std::fmt;
|
||||
|
||||
use encoding::{Encoding, EncodingRef};
|
||||
|
||||
use crate::Exceptions;
|
||||
use crate::common::Result;
|
||||
|
||||
use super::CharacterSetECI;
|
||||
|
||||
@@ -103,7 +103,7 @@ impl ECIStringBuilder {
|
||||
* @param value ECI value to append, as an int
|
||||
* @throws FormatException on invalid ECI value
|
||||
*/
|
||||
pub fn appendECI(&mut self, value: u32) -> Result<(), Exceptions> {
|
||||
pub fn appendECI(&mut self, value: u32) -> Result<()> {
|
||||
self.encodeCurrentBytesIfAny();
|
||||
|
||||
if let Ok(character_set_eci) = CharacterSetECI::getCharacterSetECIByValue(value) {
|
||||
|
||||
@@ -24,6 +24,7 @@ use std::{borrow::Cow, rc::Rc};
|
||||
|
||||
use once_cell::unsync::OnceCell;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::{Binarizer, Exceptions, LuminanceSource};
|
||||
|
||||
use super::{BitArray, BitMatrix};
|
||||
@@ -54,7 +55,7 @@ impl Binarizer for GlobalHistogramBinarizer {
|
||||
}
|
||||
|
||||
// Applies simple sharpening to the row data to improve performance of the 1D Readers.
|
||||
fn getBlackRow(&self, y: usize) -> Result<Cow<BitArray>, Exceptions> {
|
||||
fn getBlackRow(&self, y: usize) -> Result<Cow<BitArray>> {
|
||||
let row = self.black_row_cache[y].get_or_try_init(|| {
|
||||
let source = self.getLuminanceSource();
|
||||
let width = source.getWidth();
|
||||
@@ -101,7 +102,7 @@ impl Binarizer for GlobalHistogramBinarizer {
|
||||
}
|
||||
|
||||
// Does not sharpen the data, as this call is intended to only be used by 2D Readers.
|
||||
fn getBlackMatrix(&self) -> Result<&BitMatrix, Exceptions> {
|
||||
fn getBlackMatrix(&self) -> Result<&BitMatrix> {
|
||||
let matrix = self
|
||||
.black_matrix
|
||||
.get_or_try_init(|| Self::build_black_matrix(&self.source))?;
|
||||
@@ -138,7 +139,7 @@ impl GlobalHistogramBinarizer {
|
||||
}
|
||||
}
|
||||
|
||||
fn build_black_matrix(source: &Box<dyn LuminanceSource>) -> Result<BitMatrix, Exceptions> {
|
||||
fn build_black_matrix(source: &Box<dyn LuminanceSource>) -> Result<BitMatrix> {
|
||||
// let source = source.getLuminanceSource();
|
||||
let width = source.getWidth();
|
||||
let height = source.getHeight();
|
||||
@@ -192,7 +193,7 @@ impl GlobalHistogramBinarizer {
|
||||
// // }
|
||||
// }
|
||||
|
||||
fn estimateBlackPoint(buckets: &[u32]) -> Result<u32, Exceptions> {
|
||||
fn estimateBlackPoint(buckets: &[u32]) -> Result<u32> {
|
||||
// Find the tallest peak in the histogram.
|
||||
let numBuckets = buckets.len();
|
||||
let mut maxBucketCount = 0;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
// import com.google.zxing.NotFoundException;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{BitMatrix, PerspectiveTransform};
|
||||
@@ -108,7 +109,7 @@ pub trait GridSampler {
|
||||
p3FromY: f32,
|
||||
p4FromX: f32,
|
||||
p4FromY: f32,
|
||||
) -> Result<BitMatrix, Exceptions>;
|
||||
) -> Result<BitMatrix>;
|
||||
|
||||
fn sample_grid(
|
||||
&self,
|
||||
@@ -116,7 +117,7 @@ pub trait GridSampler {
|
||||
dimensionX: u32,
|
||||
dimensionY: u32,
|
||||
transform: &PerspectiveTransform,
|
||||
) -> Result<BitMatrix, Exceptions>;
|
||||
) -> Result<BitMatrix>;
|
||||
|
||||
/**
|
||||
* <p>Checks a set of points that have been transformed to sample points on an image against
|
||||
@@ -133,7 +134,7 @@ pub trait GridSampler {
|
||||
* @param points actual points in x1,y1,...,xn,yn form
|
||||
* @throws NotFoundException if an endpoint is lies outside the image boundaries
|
||||
*/
|
||||
fn checkAndNudgePoints(&self, image: &BitMatrix, points: &mut [f32]) -> Result<(), Exceptions> {
|
||||
fn checkAndNudgePoints(&self, image: &BitMatrix, points: &mut [f32]) -> Result<()> {
|
||||
let width = image.getWidth();
|
||||
let height = image.getHeight();
|
||||
// Check and nudge points from start until we see some that are OK:
|
||||
|
||||
@@ -24,7 +24,8 @@ use std::{borrow::Cow, rc::Rc};
|
||||
|
||||
use once_cell::unsync::OnceCell;
|
||||
|
||||
use crate::{Binarizer, Exceptions, LuminanceSource};
|
||||
use crate::common::Result;
|
||||
use crate::{Binarizer, LuminanceSource};
|
||||
|
||||
use super::{BitArray, BitMatrix, GlobalHistogramBinarizer};
|
||||
|
||||
@@ -57,7 +58,7 @@ impl Binarizer for HybridBinarizer {
|
||||
self.ghb.getLuminanceSource()
|
||||
}
|
||||
|
||||
fn getBlackRow(&self, y: usize) -> Result<Cow<BitArray>, Exceptions> {
|
||||
fn getBlackRow(&self, y: usize) -> Result<Cow<BitArray>> {
|
||||
self.ghb.getBlackRow(y)
|
||||
}
|
||||
|
||||
@@ -66,7 +67,7 @@ impl Binarizer for HybridBinarizer {
|
||||
* constructor instead, but there are some advantages to doing it lazily, such as making
|
||||
* profiling easier, and not doing heavy lifting when callers don't expect it.
|
||||
*/
|
||||
fn getBlackMatrix(&self) -> Result<&BitMatrix, Exceptions> {
|
||||
fn getBlackMatrix(&self) -> Result<&BitMatrix> {
|
||||
let matrix = self
|
||||
.black_matrix
|
||||
.get_or_try_init(|| Self::calculateBlackMatrix(&self.ghb))?;
|
||||
@@ -102,7 +103,7 @@ impl HybridBinarizer {
|
||||
}
|
||||
}
|
||||
|
||||
fn calculateBlackMatrix(ghb: &GlobalHistogramBinarizer) -> Result<BitMatrix, Exceptions> {
|
||||
fn calculateBlackMatrix(ghb: &GlobalHistogramBinarizer) -> Result<BitMatrix> {
|
||||
// let matrix;
|
||||
let source = ghb.getLuminanceSource();
|
||||
let width = source.getWidth();
|
||||
|
||||
@@ -19,6 +19,7 @@ use std::{fmt, rc::Rc};
|
||||
use encoding::EncodingRef;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{ECIEncoderSet, ECIInput};
|
||||
@@ -65,7 +66,7 @@ impl ECIInput for MinimalECIInput {
|
||||
* @throws IllegalArgumentException
|
||||
* if the value at the {@code index} argument is an ECI (@see #isECI)
|
||||
*/
|
||||
fn charAt(&self, index: usize) -> Result<char, Exceptions> {
|
||||
fn charAt(&self, index: usize) -> Result<char> {
|
||||
if index >= self.length() {
|
||||
return Err(Exceptions::IndexOutOfBoundsException(Some(
|
||||
index.to_string(),
|
||||
@@ -103,7 +104,7 @@ impl ECIInput for MinimalECIInput {
|
||||
* @throws IllegalArgumentException
|
||||
* if a value in the range {@code start}-{@code end} is an ECI (@see #isECI)
|
||||
*/
|
||||
fn subSequence(&self, start: usize, end: usize) -> Result<Vec<char>, Exceptions> {
|
||||
fn subSequence(&self, start: usize, end: usize) -> Result<Vec<char>> {
|
||||
if start > end || end > self.length() {
|
||||
return Err(Exceptions::IndexOutOfBoundsException(None));
|
||||
}
|
||||
@@ -131,7 +132,7 @@ impl ECIInput for MinimalECIInput {
|
||||
* if the {@code index} argument is negative or not less than
|
||||
* {@code length()}
|
||||
*/
|
||||
fn isECI(&self, index: u32) -> Result<bool, Exceptions> {
|
||||
fn isECI(&self, index: u32) -> Result<bool> {
|
||||
if index >= self.length() as u32 {
|
||||
return Err(Exceptions::IndexOutOfBoundsException(None));
|
||||
}
|
||||
@@ -156,7 +157,7 @@ impl ECIInput for MinimalECIInput {
|
||||
* @throws IllegalArgumentException
|
||||
* if the value at the {@code index} argument is not an ECI (@see #isECI)
|
||||
*/
|
||||
fn getECIValue(&self, index: usize) -> Result<i32, Exceptions> {
|
||||
fn getECIValue(&self, index: usize) -> Result<i32> {
|
||||
if index >= self.length() {
|
||||
return Err(Exceptions::IndexOutOfBoundsException(None));
|
||||
}
|
||||
@@ -168,7 +169,7 @@ impl ECIInput for MinimalECIInput {
|
||||
Ok((self.bytes[index] as u32 - 256) as i32)
|
||||
}
|
||||
|
||||
fn haveNCharacters(&self, index: usize, n: usize) -> Result<bool, Exceptions> {
|
||||
fn haveNCharacters(&self, index: usize, n: usize) -> Result<bool> {
|
||||
if index + n > self.bytes.len() {
|
||||
return Ok(false);
|
||||
}
|
||||
@@ -248,7 +249,7 @@ impl MinimalECIInput {
|
||||
* if the {@code index} argument is negative or not less than
|
||||
* {@code length()}
|
||||
*/
|
||||
pub fn isFNC1(&self, index: usize) -> Result<bool, Exceptions> {
|
||||
pub fn isFNC1(&self, index: usize) -> Result<bool> {
|
||||
if index >= self.length() {
|
||||
return Err(Exceptions::IndexOutOfBoundsException(None));
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ pub use string_utils::*;
|
||||
mod bit_array;
|
||||
pub use bit_array::*;
|
||||
|
||||
pub type Result<T, E = crate::Exceptions> = std::result::Result<T, E>;
|
||||
|
||||
/*
|
||||
* Copyright 2007 ZXing authors
|
||||
*
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::{borrow::Cow, rc::Rc};
|
||||
use image::{DynamicImage, ImageBuffer, Luma};
|
||||
use once_cell::sync::OnceCell;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::{Binarizer, Exceptions, LuminanceSource};
|
||||
|
||||
use super::{BitArray, BitMatrix};
|
||||
@@ -16,7 +17,7 @@ pub struct OtsuLevelBinarizer {
|
||||
}
|
||||
|
||||
impl OtsuLevelBinarizer {
|
||||
fn generate_threshold_matrix(source: &dyn LuminanceSource) -> Result<BitMatrix, Exceptions> {
|
||||
fn generate_threshold_matrix(source: &dyn LuminanceSource) -> Result<BitMatrix> {
|
||||
let image_buffer = {
|
||||
let Some(buff) : Option<ImageBuffer<Luma<u8>,Vec<u8>>> = ImageBuffer::from_vec(source.getWidth() as u32, source.getHeight() as u32, source.getMatrix()) else {
|
||||
return Err(Exceptions::IllegalArgumentException(None))
|
||||
@@ -49,10 +50,7 @@ impl Binarizer for OtsuLevelBinarizer {
|
||||
&self.source
|
||||
}
|
||||
|
||||
fn getBlackRow(
|
||||
&self,
|
||||
y: usize,
|
||||
) -> Result<std::borrow::Cow<super::BitArray>, crate::Exceptions> {
|
||||
fn getBlackRow(&self, y: usize) -> Result<std::borrow::Cow<super::BitArray>> {
|
||||
let row = self.black_row_cache[y].get_or_try_init(|| {
|
||||
let matrix = self.getBlackMatrix()?;
|
||||
Ok(matrix.getRow(y as u32))
|
||||
@@ -61,7 +59,7 @@ impl Binarizer for OtsuLevelBinarizer {
|
||||
Ok(Cow::Borrowed(row))
|
||||
}
|
||||
|
||||
fn getBlackMatrix(&self) -> Result<&super::BitMatrix, crate::Exceptions> {
|
||||
fn getBlackMatrix(&self) -> Result<&super::BitMatrix> {
|
||||
let matrix = self
|
||||
.black_matrix
|
||||
.get_or_try_init(|| Self::generate_threshold_matrix(self.source.as_ref()))?;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::fmt;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{GenericGFPoly, GenericGFRef};
|
||||
@@ -131,7 +132,7 @@ impl GenericGF {
|
||||
/**
|
||||
* @return base 2 log of a in GF(size)
|
||||
*/
|
||||
pub fn log(&self, a: i32) -> Result<i32, Exceptions> {
|
||||
pub fn log(&self, a: i32) -> Result<i32> {
|
||||
if a == 0 {
|
||||
return Err(Exceptions::IllegalArgumentException(None));
|
||||
}
|
||||
@@ -142,7 +143,7 @@ impl GenericGF {
|
||||
/**
|
||||
* @return multiplicative inverse of a
|
||||
*/
|
||||
pub fn inverse(&self, a: i32) -> Result<i32, Exceptions> {
|
||||
pub fn inverse(&self, a: i32) -> Result<i32> {
|
||||
if a == 0 {
|
||||
return Err(Exceptions::ArithmeticException(None));
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{GenericGF, GenericGFRef};
|
||||
@@ -47,7 +48,7 @@ impl GenericGFPoly {
|
||||
* or if leading coefficient is 0 and this is not a
|
||||
* constant polynomial (that is, it is not the monomial "0")
|
||||
*/
|
||||
pub fn new(field: GenericGFRef, coefficients: &[i32]) -> Result<Self, Exceptions> {
|
||||
pub fn new(field: GenericGFRef, coefficients: &[i32]) -> Result<Self> {
|
||||
if coefficients.is_empty() {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(String::from(
|
||||
"coefficients cannot be empty",
|
||||
@@ -138,7 +139,7 @@ impl GenericGFPoly {
|
||||
result
|
||||
}
|
||||
|
||||
pub fn addOrSubtract(&self, other: &GenericGFPoly) -> Result<GenericGFPoly, Exceptions> {
|
||||
pub fn addOrSubtract(&self, other: &GenericGFPoly) -> Result<GenericGFPoly> {
|
||||
if self.field != other.field {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"GenericGFPolys do not have same GenericGF field".to_owned(),
|
||||
@@ -174,7 +175,7 @@ impl GenericGFPoly {
|
||||
GenericGFPoly::new(self.field, &sumDiff)
|
||||
}
|
||||
|
||||
pub fn multiply(&self, other: &GenericGFPoly) -> Result<GenericGFPoly, Exceptions> {
|
||||
pub fn multiply(&self, other: &GenericGFPoly) -> Result<GenericGFPoly> {
|
||||
if self.field != other.field {
|
||||
//if (!field.equals(other.field)) {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
@@ -229,11 +230,7 @@ impl GenericGFPoly {
|
||||
GenericGFPoly::new(self.field, &[1]).unwrap()
|
||||
}
|
||||
|
||||
pub fn multiply_by_monomial(
|
||||
&self,
|
||||
degree: usize,
|
||||
coefficient: i32,
|
||||
) -> Result<GenericGFPoly, Exceptions> {
|
||||
pub fn multiply_by_monomial(&self, degree: usize, coefficient: i32) -> Result<GenericGFPoly> {
|
||||
if coefficient == 0 {
|
||||
return Ok(self.getZero());
|
||||
}
|
||||
@@ -247,10 +244,7 @@ impl GenericGFPoly {
|
||||
GenericGFPoly::new(self.field, &product)
|
||||
}
|
||||
|
||||
pub fn divide(
|
||||
&self,
|
||||
other: &GenericGFPoly,
|
||||
) -> Result<(GenericGFPoly, GenericGFPoly), Exceptions> {
|
||||
pub fn divide(&self, other: &GenericGFPoly) -> Result<(GenericGFPoly, GenericGFPoly)> {
|
||||
if self.field != other.field {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"GenericGFPolys do not have same GenericGF field".to_owned(),
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
//package com.google.zxing.common.reedsolomon;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{GenericGF, GenericGFPoly, GenericGFRef};
|
||||
@@ -60,7 +61,7 @@ impl ReedSolomonDecoder {
|
||||
* @param twoS number of error-correction codewords available
|
||||
* @throws ReedSolomonException if decoding fails for any reason
|
||||
*/
|
||||
pub fn decode(&self, received: &mut Vec<i32>, twoS: i32) -> Result<usize, Exceptions> {
|
||||
pub fn decode(&self, received: &mut Vec<i32>, twoS: i32) -> Result<usize> {
|
||||
let poly = GenericGFPoly::new(self.field, received)?;
|
||||
let mut syndromeCoefficients = vec![0; twoS as usize];
|
||||
let mut noError = true;
|
||||
@@ -113,7 +114,7 @@ impl ReedSolomonDecoder {
|
||||
a: &GenericGFPoly,
|
||||
b: &GenericGFPoly,
|
||||
R: usize,
|
||||
) -> Result<Vec<GenericGFPoly>, Exceptions> {
|
||||
) -> Result<Vec<GenericGFPoly>> {
|
||||
// Assume a's degree is >= b's
|
||||
let mut a = a.clone();
|
||||
let mut b = b.clone();
|
||||
@@ -184,7 +185,7 @@ impl ReedSolomonDecoder {
|
||||
Ok(vec![sigma, omega])
|
||||
}
|
||||
|
||||
fn findErrorLocations(&self, errorLocator: &GenericGFPoly) -> Result<Vec<usize>, Exceptions> {
|
||||
fn findErrorLocations(&self, errorLocator: &GenericGFPoly) -> Result<Vec<usize>> {
|
||||
// This is a direct application of Chien's search
|
||||
let numErrors = errorLocator.getDegree();
|
||||
if numErrors == 1 {
|
||||
@@ -216,7 +217,7 @@ impl ReedSolomonDecoder {
|
||||
&self,
|
||||
errorEvaluator: &GenericGFPoly,
|
||||
errorLocations: &Vec<usize>,
|
||||
) -> Result<Vec<i32>, Exceptions> {
|
||||
) -> Result<Vec<i32>> {
|
||||
// This is directly applying Forney's Formula
|
||||
let s = errorLocations.len();
|
||||
let mut result = vec![0; s];
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
//import java.util.ArrayList;
|
||||
//import java.util.List;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{GenericGFPoly, GenericGFRef};
|
||||
@@ -35,7 +36,7 @@ pub struct ReedSolomonEncoder {
|
||||
}
|
||||
|
||||
impl ReedSolomonEncoder {
|
||||
pub fn new(field: GenericGFRef) -> Result<Self, Exceptions> {
|
||||
pub fn new(field: GenericGFRef) -> Result<Self> {
|
||||
let n = field;
|
||||
Ok(Self {
|
||||
cachedGenerators: vec![GenericGFPoly::new(n, &[1])?],
|
||||
@@ -71,7 +72,7 @@ impl ReedSolomonEncoder {
|
||||
Some(rv)
|
||||
}
|
||||
|
||||
pub fn encode(&mut self, to_encode: &mut Vec<i32>, ec_bytes: usize) -> Result<(), Exceptions> {
|
||||
pub fn encode(&mut self, to_encode: &mut Vec<i32>, ec_bytes: usize) -> Result<()> {
|
||||
if ec_bytes == 0 {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"No error correction bytes".to_owned(),
|
||||
|
||||
Reference in New Issue
Block a user