mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
Implement clippy suggestions
This commit is contained in:
@@ -243,7 +243,7 @@ fn reverse_algorithm_test() {
|
||||
let newBitsNew = newBitArray.getBitArray();
|
||||
assert!(arrays_are_equal(
|
||||
&newBitsOriginal,
|
||||
&newBitsNew,
|
||||
newBitsNew,
|
||||
size / 32 + 1
|
||||
));
|
||||
}
|
||||
@@ -276,14 +276,14 @@ fn reverse_original(oldBits: &[u32], size: usize) -> Vec<u32> {
|
||||
for i in 0..size {
|
||||
// for (int i = 0; i < size; i++) {
|
||||
if bit_set(oldBits, size - i - 1) {
|
||||
newBits[i / 32 as usize] |= 1 << (i & 0x1F);
|
||||
newBits[i / 32_usize] |= 1 << (i & 0x1F);
|
||||
}
|
||||
}
|
||||
return newBits;
|
||||
newBits
|
||||
}
|
||||
|
||||
fn bit_set(bits: &[u32], i: usize) -> bool {
|
||||
return (bits[i / 32] & (1 << (i & 0x1F))) != 0;
|
||||
(bits[i / 32] & (1 << (i & 0x1F))) != 0
|
||||
}
|
||||
|
||||
fn arrays_are_equal(left: &[u32], right: &[u32], size: usize) -> bool {
|
||||
@@ -293,7 +293,7 @@ fn arrays_are_equal(left: &[u32], right: &[u32], size: usize) -> bool {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
true
|
||||
}
|
||||
|
||||
// }
|
||||
|
||||
@@ -61,7 +61,10 @@ fn test_set_region() {
|
||||
// for (int y = 0; y < 5; y++) {
|
||||
for x in 0..5 {
|
||||
// for (int x = 0; x < 5; x++) {
|
||||
assert_eq!(y >= 1 && y <= 3 && x >= 1 && x <= 3, matrix.get(x, y));
|
||||
assert_eq!(
|
||||
(1..=3).contains(&y) && (1..=3).contains(&x),
|
||||
matrix.get(x, y)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -133,7 +136,10 @@ fn test_rectangular_set_region() {
|
||||
// for (int y = 0; y < 240; y++) {
|
||||
for x in 0..320 {
|
||||
// for (int x = 0; x < 320; x++) {
|
||||
assert_eq!(y >= 22 && y < 34 && x >= 105 && x < 185, matrix.get(x, y));
|
||||
assert_eq!(
|
||||
(22..34).contains(&y) && (105..185).contains(&x),
|
||||
matrix.get(x, y)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -302,7 +308,7 @@ fn test_xor_case() {
|
||||
centerMatrix.setRegion(1, 1, 1, 1).expect("must set");
|
||||
let mut invertedCenterMatrix = fullMatrix.clone();
|
||||
invertedCenterMatrix.unset(1, 1);
|
||||
let badMatrix = BitMatrix::new(4, 4).unwrap();
|
||||
let mut badMatrix = BitMatrix::new(4, 4).unwrap();
|
||||
|
||||
test_XOR(&emptyMatrix, &emptyMatrix, &emptyMatrix);
|
||||
test_XOR(&emptyMatrix, ¢erMatrix, ¢erMatrix);
|
||||
@@ -328,7 +334,7 @@ fn test_xor_case() {
|
||||
// // good
|
||||
// }
|
||||
|
||||
assert!(badMatrix.clone().xor(&emptyMatrix).is_err());
|
||||
assert!(badMatrix.xor(&emptyMatrix).is_err());
|
||||
// try {
|
||||
// badMatrix.clone().xor(emptyMatrix);
|
||||
// fail();
|
||||
@@ -344,7 +350,7 @@ pub fn matrix_to_string(result: &BitMatrix) -> String {
|
||||
// for (int i = 0; i < result.getWidth(); i++) {
|
||||
builder.push(if result.get(i, 0) { '1' } else { '0' });
|
||||
}
|
||||
return builder;
|
||||
builder
|
||||
}
|
||||
|
||||
fn test_XOR(dataMatrix: &BitMatrix, flipMatrix: &BitMatrix, expectedMatrix: &BitMatrix) {
|
||||
@@ -378,7 +384,7 @@ fn get_expected(width: u32, height: u32) -> BitMatrix {
|
||||
);
|
||||
i += 2;
|
||||
}
|
||||
return result;
|
||||
result
|
||||
}
|
||||
|
||||
fn get_input(width: u32, height: u32) -> BitMatrix {
|
||||
@@ -389,7 +395,7 @@ fn get_input(width: u32, height: u32) -> BitMatrix {
|
||||
result.set(BIT_MATRIX_POINTS[i], BIT_MATRIX_POINTS[i + 1]);
|
||||
i += 2;
|
||||
}
|
||||
return result;
|
||||
result
|
||||
}
|
||||
|
||||
// }
|
||||
|
||||
@@ -33,9 +33,10 @@ use crate::common::StringUtils;
|
||||
fn test_random() {
|
||||
let mut r = rand::thread_rng();
|
||||
let mut bytes: Vec<u8> = vec![0; 1000];
|
||||
for i in 0..bytes.len() {
|
||||
bytes[i] = r.gen();
|
||||
}
|
||||
bytes.fill_with(|| r.gen());
|
||||
// for byte in &mut bytes {
|
||||
// *byte = r.gen();
|
||||
// }
|
||||
assert_eq!(
|
||||
encoding::all::UTF_8.name(),
|
||||
StringUtils::guessCharset(&bytes, &HashMap::new()).name()
|
||||
@@ -102,7 +103,7 @@ fn test_utf16_le() {
|
||||
);
|
||||
}
|
||||
|
||||
fn do_test(bytes: &Vec<u8>, charset: EncodingRef, encoding: &str) {
|
||||
fn do_test(bytes: &[u8], charset: EncodingRef, encoding: &str) {
|
||||
let guessedCharset = StringUtils::guessCharset(bytes, &HashMap::new());
|
||||
let guessedEncoding = StringUtils::guessEncoding(bytes, &HashMap::new());
|
||||
assert_eq!(charset.name(), guessedCharset.name());
|
||||
|
||||
@@ -47,16 +47,13 @@ impl BitArray {
|
||||
pub fn with_size(size: usize) -> Self {
|
||||
Self {
|
||||
bits: BitArray::makeArray(size),
|
||||
size: size,
|
||||
size,
|
||||
}
|
||||
}
|
||||
|
||||
// For testing only
|
||||
pub fn with_initial_values(bits: Vec<u32>, size: usize) -> Self {
|
||||
Self {
|
||||
bits: bits,
|
||||
size: size,
|
||||
}
|
||||
Self { bits, size }
|
||||
}
|
||||
|
||||
pub fn getSize(&self) -> usize {
|
||||
@@ -64,7 +61,7 @@ impl BitArray {
|
||||
}
|
||||
|
||||
pub fn getSizeInBytes(&self) -> usize {
|
||||
return (self.size + 7) / 8;
|
||||
(self.size + 7) / 8
|
||||
}
|
||||
|
||||
fn ensure_capacity(&mut self, newSize: usize) {
|
||||
@@ -148,7 +145,7 @@ impl BitArray {
|
||||
currentBits = !self.bits[bitsOffset] as i64;
|
||||
}
|
||||
let result = (bitsOffset * 32) + currentBits.trailing_zeros() as usize;
|
||||
return cmp::min(result, self.size);
|
||||
cmp::min(result, self.size)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -171,9 +168,7 @@ impl BitArray {
|
||||
pub fn setRange(&mut self, start: usize, end: usize) -> Result<(), Exceptions> {
|
||||
let mut end = end;
|
||||
if end < start || end > self.size {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"end < start || start < 0 || end > self.size".to_owned(),
|
||||
));
|
||||
return Err(Exceptions::IllegalArgumentException(None));
|
||||
}
|
||||
if end == start {
|
||||
return Ok(());
|
||||
@@ -216,9 +211,7 @@ impl BitArray {
|
||||
pub fn isRange(&self, start: usize, end: usize, value: bool) -> Result<bool, Exceptions> {
|
||||
let mut end = end;
|
||||
if end < start || end > self.size {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
"end < start || start < 0 || end > self.size".to_owned(),
|
||||
));
|
||||
return Err(Exceptions::IllegalArgumentException(None));
|
||||
}
|
||||
if end == start {
|
||||
return Ok(true); // empty range matches
|
||||
@@ -239,7 +232,7 @@ impl BitArray {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
return Ok(true);
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn appendBit(&mut self, bit: bool) {
|
||||
@@ -260,9 +253,9 @@ impl BitArray {
|
||||
*/
|
||||
pub fn appendBits(&mut self, value: u32, num_bits: usize) -> Result<(), Exceptions> {
|
||||
if num_bits > 32 {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Num bits must be between 0 and 32".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
if num_bits == 0 {
|
||||
@@ -293,9 +286,9 @@ impl BitArray {
|
||||
|
||||
pub fn xor(&mut self, other: &BitArray) -> Result<(), Exceptions> {
|
||||
if self.size != other.size {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Sizes don't match".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
for i in 0..self.bits.len() {
|
||||
//for (int i = 0; i < bits.length; i++) {
|
||||
@@ -335,7 +328,7 @@ impl BitArray {
|
||||
* significant bit is bit 0.
|
||||
*/
|
||||
pub fn getBitArray(&self) -> &Vec<u32> {
|
||||
return &self.bits;
|
||||
&self.bits
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -367,7 +360,7 @@ impl BitArray {
|
||||
}
|
||||
|
||||
fn makeArray(size: usize) -> Vec<u32> {
|
||||
return vec![0; (size + 31) / 32];
|
||||
vec![0; (size + 31) / 32]
|
||||
}
|
||||
|
||||
// @Override
|
||||
@@ -396,10 +389,16 @@ impl fmt::Display for BitArray {
|
||||
for i in 0..self.size {
|
||||
//for (int i = 0; i < size; i++) {
|
||||
if (i & 0x07) == 0 {
|
||||
_str.push_str(" ");
|
||||
_str.push(' ');
|
||||
}
|
||||
_str.push_str(if self.get(i) { "X" } else { "." });
|
||||
}
|
||||
write!(f, "{}", _str)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for BitArray {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,9 +65,9 @@ impl BitMatrix {
|
||||
*/
|
||||
pub fn new(width: u32, height: u32) -> Result<Self, Exceptions> {
|
||||
if width < 1 || height < 1 {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Both dimensions must be greater than 0".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
Ok(Self {
|
||||
width,
|
||||
@@ -101,17 +101,19 @@ impl BitMatrix {
|
||||
let height: u32 = image.len().try_into().unwrap();
|
||||
let width: u32 = image[0].len().try_into().unwrap();
|
||||
let mut bits = BitMatrix::new(width, height).unwrap();
|
||||
for i in 0..height as usize {
|
||||
for (i, imageI) in image.iter().enumerate().take(height as usize) {
|
||||
// for i in 0..height as usize {
|
||||
//for (int i = 0; i < height; i++) {
|
||||
let imageI = &image[i];
|
||||
for j in 0..width as usize {
|
||||
// let imageI = &image[i];
|
||||
for (j, imageI_j) in imageI.iter().enumerate().take(width as usize) {
|
||||
// for j in 0..width as usize {
|
||||
//for (int j = 0; j < width; j++) {
|
||||
if imageI[j] {
|
||||
if *imageI_j {
|
||||
bits.set(j as u32, i as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
return bits;
|
||||
bits
|
||||
}
|
||||
|
||||
pub fn parse_strings(
|
||||
@@ -141,9 +143,9 @@ impl BitMatrix {
|
||||
first_run = false;
|
||||
rowLength = bitsPos - rowStartPos;
|
||||
} else if bitsPos - rowStartPos != rowLength {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"row lengths do not match".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
rowStartPos = bitsPos;
|
||||
nRows += 1;
|
||||
@@ -158,10 +160,10 @@ impl BitMatrix {
|
||||
bits[bitsPos] = false;
|
||||
bitsPos += 1;
|
||||
} else {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"illegal character encountered: {}",
|
||||
string_representation[pos..].to_owned()
|
||||
)));
|
||||
))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,24 +174,25 @@ impl BitMatrix {
|
||||
// first_run = false;
|
||||
rowLength = bitsPos - rowStartPos;
|
||||
} else if bitsPos - rowStartPos != rowLength {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"row lengths do not match".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
nRows += 1;
|
||||
}
|
||||
|
||||
let mut matrix = BitMatrix::new(rowLength.try_into().unwrap(), nRows)?;
|
||||
for i in 0..bitsPos {
|
||||
for (i, bit) in bits.iter().enumerate().take(bitsPos) {
|
||||
// for i in 0..bitsPos {
|
||||
//for (int i = 0; i < bitsPos; i++) {
|
||||
if bits[i] {
|
||||
if *bit {
|
||||
matrix.set(
|
||||
(i % rowLength).try_into().unwrap(),
|
||||
(i / rowLength).try_into().unwrap(),
|
||||
);
|
||||
}
|
||||
}
|
||||
return Ok(matrix);
|
||||
Ok(matrix)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -201,15 +204,15 @@ impl BitMatrix {
|
||||
*/
|
||||
pub fn get(&self, x: u32, y: u32) -> bool {
|
||||
let offset = y as usize * self.row_size + (x as usize / 32);
|
||||
return ((self.bits[offset] >> (x & 0x1f)) & 1) != 0;
|
||||
((self.bits[offset] >> (x & 0x1f)) & 1) != 0
|
||||
}
|
||||
|
||||
pub fn try_get(&self, x: u32, y: u32) -> Result<bool, Exceptions> {
|
||||
let offset = y as usize * self.row_size + (x as usize / 32);
|
||||
if offset > self.bits.len() {
|
||||
return Err(Exceptions::IndexOutOfBoundsException("".to_owned()));
|
||||
return Err(Exceptions::IndexOutOfBoundsException(None));
|
||||
}
|
||||
return Ok(((self.bits[offset] >> (x & 0x1f)) & 1) != 0);
|
||||
Ok(((self.bits[offset] >> (x & 0x1f)) & 1) != 0)
|
||||
}
|
||||
|
||||
pub fn check_in_bounds(&self, x: u32, y: u32) -> bool {
|
||||
@@ -263,9 +266,9 @@ impl BitMatrix {
|
||||
pub fn xor(&mut self, mask: &BitMatrix) -> Result<(), Exceptions> {
|
||||
if self.width != mask.width || self.height != mask.height || self.row_size != mask.row_size
|
||||
{
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"input matrix dimensions do not match".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
// let mut rowArray = BitArray::with_size(self.width as usize);
|
||||
for y in 0..self.height {
|
||||
@@ -273,9 +276,10 @@ impl BitMatrix {
|
||||
let offset = y as usize * self.row_size;
|
||||
let rowArray = mask.getRow(y);
|
||||
let row = rowArray.getBitArray();
|
||||
for x in 0..self.row_size {
|
||||
for (x, row_x) in row.iter().enumerate().take(self.row_size) {
|
||||
// for x in 0..self.row_size {
|
||||
//for (int x = 0; x < rowSize; x++) {
|
||||
self.bits[offset + x] ^= row[x];
|
||||
self.bits[offset + x] ^= *row_x;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -314,16 +318,16 @@ impl BitMatrix {
|
||||
// ));
|
||||
// }
|
||||
if height < 1 || width < 1 {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Height and width must be at least 1".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
let right = left + width;
|
||||
let bottom = top + height;
|
||||
if bottom > self.height || right > self.width {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"The region must fit inside the matrix".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
for y in top..bottom {
|
||||
//for (int y = top; y < bottom; y++) {
|
||||
@@ -361,7 +365,7 @@ impl BitMatrix {
|
||||
//for (int x = 0; x < rowSize; x++) {
|
||||
rw.setBulk(x * 32, self.bits[offset + x]);
|
||||
}
|
||||
return rw;
|
||||
rw
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -395,9 +399,9 @@ impl BitMatrix {
|
||||
self.rotate180();
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(Exceptions::IllegalArgumentException(
|
||||
_ => Err(Exceptions::IllegalArgumentException(Some(
|
||||
"degrees must be a multiple of 0, 90, 180, or 270".to_owned(),
|
||||
)),
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -497,7 +501,7 @@ impl BitMatrix {
|
||||
return None;
|
||||
}
|
||||
|
||||
return Some(vec![left, top, right - left + 1, bottom - top + 1]);
|
||||
Some(vec![left, top, right - left + 1, bottom - top + 1])
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -522,7 +526,7 @@ impl BitMatrix {
|
||||
bit += 1;
|
||||
}
|
||||
x += bit;
|
||||
return Some(vec![x as u32, y as u32]);
|
||||
Some(vec![x as u32, y as u32])
|
||||
}
|
||||
|
||||
pub fn getBottomRightOnBit(&self) -> Option<Vec<u32>> {
|
||||
@@ -544,28 +548,28 @@ impl BitMatrix {
|
||||
}
|
||||
x += bit;
|
||||
|
||||
return Some(vec![x as u32, y as u32]);
|
||||
Some(vec![x as u32, y as u32])
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The width of the matrix
|
||||
*/
|
||||
pub fn getWidth(&self) -> u32 {
|
||||
return self.width;
|
||||
self.width
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The height of the matrix
|
||||
*/
|
||||
pub fn getHeight(&self) -> u32 {
|
||||
return self.height;
|
||||
self.height
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The row size of the matrix
|
||||
*/
|
||||
pub fn getRowSize(&self) -> usize {
|
||||
return self.row_size;
|
||||
self.row_size
|
||||
}
|
||||
|
||||
// @Override
|
||||
@@ -594,7 +598,7 @@ impl BitMatrix {
|
||||
* @return string representation of entire matrix utilizing given strings
|
||||
*/
|
||||
pub fn toString(&self, setString: &str, unsetString: &str) -> String {
|
||||
return self.buildToString(setString, unsetString, "\n");
|
||||
self.buildToString(setString, unsetString, "\n")
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -624,7 +628,7 @@ impl BitMatrix {
|
||||
}
|
||||
result.push_str(lineSeparator);
|
||||
}
|
||||
return result;
|
||||
result
|
||||
}
|
||||
|
||||
// @Override
|
||||
|
||||
@@ -52,14 +52,14 @@ impl BitSource {
|
||||
* @return index of next bit in current byte which would be read by the next call to {@link #readBits(int)}.
|
||||
*/
|
||||
pub fn getBitOffset(&self) -> usize {
|
||||
return self.bit_offset;
|
||||
self.bit_offset
|
||||
}
|
||||
|
||||
/**
|
||||
* @return index of next byte in input byte array which would be read by the next call to {@link #readBits(int)}.
|
||||
*/
|
||||
pub fn getByteOffset(&self) -> usize {
|
||||
return self.byte_offset;
|
||||
self.byte_offset
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,8 +69,10 @@ impl BitSource {
|
||||
* @throws IllegalArgumentException if numBits isn't in [1,32] or more than is available
|
||||
*/
|
||||
pub fn readBits(&mut self, numBits: usize) -> Result<u32, Exceptions> {
|
||||
if numBits < 1 || numBits > 32 || numBits > self.available() {
|
||||
return Err(Exceptions::IllegalArgumentException(numBits.to_string()));
|
||||
if !(1..=32).contains(&numBits) || numBits > self.available() {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
numBits.to_string(),
|
||||
)));
|
||||
}
|
||||
|
||||
let mut result: u32 = 0;
|
||||
@@ -96,7 +98,7 @@ impl BitSource {
|
||||
// Next read whole bytes
|
||||
if num_bits > 0 {
|
||||
while num_bits >= 8 {
|
||||
result = (result << 8) | (self.bytes[self.byte_offset] & 0xFF) as u32;
|
||||
result = (result << 8) | self.bytes[self.byte_offset] as u32;
|
||||
// result = ((result as u16) << 8) as u8 | (self.bytes[self.byte_offset]);
|
||||
self.byte_offset += 1;
|
||||
num_bits -= 8;
|
||||
@@ -112,13 +114,13 @@ impl BitSource {
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(result);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/**
|
||||
* @return number of bits that can be read successfully
|
||||
*/
|
||||
pub fn available(&self) -> usize {
|
||||
return 8 * (self.bytes.len() - self.byte_offset) - self.bit_offset;
|
||||
8 * (self.bytes.len() - self.byte_offset) - self.bit_offset
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,3 +65,9 @@ impl BitSourceBuilder {
|
||||
&self.output
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for BitSourceBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,7 +244,9 @@ impl CharacterSetECI {
|
||||
28 => Ok(CharacterSetECI::Big5),
|
||||
29 => Ok(CharacterSetECI::GB18030),
|
||||
30 => Ok(CharacterSetECI::EUC_KR),
|
||||
_ => Err(Exceptions::NotFoundException("Bad ECI Value".to_owned())),
|
||||
_ => Err(Exceptions::NotFoundException(Some(
|
||||
"Bad ECI Value".to_owned(),
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -66,9 +66,7 @@ impl GridSampler for DefaultGridSampler {
|
||||
transform: &PerspectiveTransform,
|
||||
) -> Result<BitMatrix, Exceptions> {
|
||||
if dimensionX == 0 || dimensionY == 0 {
|
||||
return Err(Exceptions::NotFoundException(
|
||||
"getNotFoundInstance".to_owned(),
|
||||
));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
let mut bits = BitMatrix::new(dimensionX, dimensionY)?;
|
||||
let mut points = vec![0_f32; 2 * dimensionX as usize];
|
||||
@@ -94,9 +92,9 @@ impl GridSampler for DefaultGridSampler {
|
||||
if points[x].floor() as u32 >= image.getWidth()
|
||||
|| points[x + 1].floor() as u32 >= image.getHeight()
|
||||
{
|
||||
return Err(Exceptions::NotFoundException(
|
||||
return Err(Exceptions::NotFoundException(Some(
|
||||
"index out of bounds, see documentation in file for explanation".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
if image.get(points[x].floor() as u32, points[x + 1].floor() as u32) {
|
||||
// Black(-ish) pixel
|
||||
@@ -115,6 +113,6 @@ impl GridSampler for DefaultGridSampler {
|
||||
// throw NotFoundException.getNotFoundInstance();
|
||||
// }
|
||||
}
|
||||
return Ok(bits);
|
||||
Ok(bits)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ use std::{f32, i32};
|
||||
* @return nearest {@code int}
|
||||
*/
|
||||
pub fn round(d: f32) -> i32 {
|
||||
return (d + (if d < 0.0f32 { -0.5f32 } else { 0.5f32 })) as i32;
|
||||
(d + (if d < 0.0f32 { -0.5f32 } else { 0.5f32 })) as i32
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,7 +44,7 @@ pub fn round(d: f32) -> i32 {
|
||||
pub fn distance_float(aX: f32, aY: f32, bX: f32, bY: f32) -> f32 {
|
||||
let xDiff: f64 = (aX - bX).into();
|
||||
let yDiff: f64 = (aY - bY).into();
|
||||
return (xDiff * xDiff + yDiff * yDiff).sqrt() as f32;
|
||||
(xDiff * xDiff + yDiff * yDiff).sqrt() as f32
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,7 +57,7 @@ pub fn distance_float(aX: f32, aY: f32, bX: f32, bY: f32) -> f32 {
|
||||
pub fn distance_int(aX: i32, aY: i32, bX: i32, bY: i32) -> f32 {
|
||||
let xDiff: f64 = (aX - bX).into();
|
||||
let yDiff: f64 = (aY - bY).into();
|
||||
return (xDiff * xDiff + yDiff * yDiff).sqrt() as f32;
|
||||
(xDiff * xDiff + yDiff * yDiff).sqrt() as f32
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,7 +69,7 @@ pub fn sum(array: &[i32]) -> i32 {
|
||||
for a in array {
|
||||
count += a;
|
||||
}
|
||||
return count;
|
||||
count
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -120,7 +120,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn testSum() {
|
||||
assert_eq!(0, MathUtils::sum(&vec![]));
|
||||
assert_eq!(0, MathUtils::sum(&[]));
|
||||
assert_eq!(1, MathUtils::sum(&[1]));
|
||||
assert_eq!(4, MathUtils::sum(&[1, 3]));
|
||||
assert_eq!(0, MathUtils::sum(&[-1, 1]));
|
||||
|
||||
@@ -55,8 +55,8 @@ impl MonochromeRectangleDetector {
|
||||
let width = self.image.getWidth() as i32;
|
||||
let halfHeight = height / 2;
|
||||
let halfWidth = width / 2;
|
||||
let deltaY = 1.max(height as i32 / (MAX_MODULES * 8));
|
||||
let deltaX = 1.max(width as i32 / (MAX_MODULES * 8));
|
||||
let deltaY = 1.max(height / (MAX_MODULES * 8));
|
||||
let deltaX = 1.max(width / (MAX_MODULES * 8));
|
||||
|
||||
let mut top = 0;
|
||||
let mut bottom = height;
|
||||
@@ -124,7 +124,7 @@ impl MonochromeRectangleDetector {
|
||||
halfWidth / 4,
|
||||
)?;
|
||||
|
||||
return Ok(vec![pointA, pointB, pointC, pointD]);
|
||||
Ok(vec![pointA, pointB, pointC, pointD])
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -161,14 +161,13 @@ impl MonochromeRectangleDetector {
|
||||
let mut y: i32 = centerY;
|
||||
let mut x: i32 = centerX;
|
||||
while y < bottom && y >= top && x < right && x >= left {
|
||||
let range: Option<Vec<i32>>;
|
||||
if deltaX == 0 {
|
||||
let range: Option<Vec<i32>> = if deltaX == 0 {
|
||||
// horizontal slices, up and down
|
||||
range = self.blackWhiteRange(y, maxWhiteRun, left, right, true);
|
||||
self.blackWhiteRange(y, maxWhiteRun, left, right, true)
|
||||
} else {
|
||||
// vertical slices, left and right
|
||||
range = self.blackWhiteRange(x, maxWhiteRun, top, bottom, false);
|
||||
}
|
||||
self.blackWhiteRange(x, maxWhiteRun, top, bottom, false)
|
||||
};
|
||||
if range.is_none() {
|
||||
if let Some(lastRange) = lastRange_z {
|
||||
// lastRange was found
|
||||
@@ -178,7 +177,7 @@ impl MonochromeRectangleDetector {
|
||||
if lastRange[1] > centerX {
|
||||
// straddle, choose one or the other based on direction
|
||||
return Ok(RXingResultPoint::new(
|
||||
lastRange[if deltaY > 0 { 0 } else { 1 }] as f32,
|
||||
lastRange[usize::from(deltaY <= 0)] as f32,
|
||||
lastY as f32,
|
||||
));
|
||||
}
|
||||
@@ -192,7 +191,7 @@ impl MonochromeRectangleDetector {
|
||||
if lastRange[1] > centerY {
|
||||
return Ok(RXingResultPoint::new(
|
||||
lastX as f32,
|
||||
lastRange[if deltaX < 0 { 0 } else { 1 }] as f32,
|
||||
lastRange[usize::from(deltaX >= 0)] as f32,
|
||||
));
|
||||
}
|
||||
return Ok(RXingResultPoint::new(lastX as f32, lastRange[0] as f32));
|
||||
@@ -202,13 +201,13 @@ impl MonochromeRectangleDetector {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
lastRange_z = range;
|
||||
y += deltaY;
|
||||
x += deltaX
|
||||
}
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -243,10 +242,10 @@ impl MonochromeRectangleDetector {
|
||||
} else {
|
||||
self.image.get(fixedDimension as u32, start as u32)
|
||||
} {
|
||||
start = start - 1;
|
||||
start -= 1;
|
||||
} else {
|
||||
let whiteRunStart = start;
|
||||
start = start - 1;
|
||||
start -= 1;
|
||||
while start >= minDim
|
||||
&& !(if horizontal {
|
||||
self.image.get(start as u32, fixedDimension as u32)
|
||||
@@ -254,7 +253,7 @@ impl MonochromeRectangleDetector {
|
||||
self.image.get(fixedDimension as u32, start as u32)
|
||||
})
|
||||
{
|
||||
start = start - 1;
|
||||
start -= 1;
|
||||
}
|
||||
let whiteRunSize = whiteRunStart - start;
|
||||
if start < minDim || whiteRunSize > maxWhiteRun {
|
||||
@@ -263,7 +262,7 @@ impl MonochromeRectangleDetector {
|
||||
}
|
||||
}
|
||||
}
|
||||
start = start + 1;
|
||||
start += 1;
|
||||
|
||||
// Then try right/down
|
||||
let mut end = center;
|
||||
@@ -273,10 +272,10 @@ impl MonochromeRectangleDetector {
|
||||
} else {
|
||||
self.image.get(fixedDimension as u32, end as u32)
|
||||
} {
|
||||
end = end + 1;
|
||||
end += 1;
|
||||
} else {
|
||||
let whiteRunStart = end;
|
||||
end = end + 1;
|
||||
end += 1;
|
||||
while end < maxDim
|
||||
&& !(if horizontal {
|
||||
self.image.get(end as u32, fixedDimension as u32)
|
||||
@@ -284,7 +283,7 @@ impl MonochromeRectangleDetector {
|
||||
self.image.get(fixedDimension as u32, end as u32)
|
||||
})
|
||||
{
|
||||
end = end + 1;
|
||||
end += 1;
|
||||
}
|
||||
let whiteRunSize = end - whiteRunStart;
|
||||
if end >= maxDim || whiteRunSize > maxWhiteRun {
|
||||
@@ -293,12 +292,12 @@ impl MonochromeRectangleDetector {
|
||||
}
|
||||
}
|
||||
}
|
||||
end = end - 1;
|
||||
end -= 1;
|
||||
|
||||
return if end > start {
|
||||
if end > start {
|
||||
Some(vec![start, end])
|
||||
} else {
|
||||
None
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,17 +72,17 @@ impl WhiteRectangleDetector {
|
||||
|| downInit >= image.getHeight() as i32
|
||||
|| rightInit >= image.getWidth() as i32
|
||||
{
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
image: image.clone(),
|
||||
height: image.getHeight() as i32,
|
||||
width: image.getWidth() as i32,
|
||||
leftInit: leftInit,
|
||||
rightInit: rightInit,
|
||||
downInit: downInit,
|
||||
upInit: upInit,
|
||||
leftInit,
|
||||
rightInit,
|
||||
downInit,
|
||||
upInit,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -218,7 +218,7 @@ impl WhiteRectangleDetector {
|
||||
}
|
||||
|
||||
if z.is_none() {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let mut t: Option<RXingResultPoint> = None;
|
||||
@@ -236,7 +236,7 @@ impl WhiteRectangleDetector {
|
||||
}
|
||||
|
||||
if t.is_none() {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let mut x: Option<RXingResultPoint> = None;
|
||||
@@ -254,7 +254,7 @@ impl WhiteRectangleDetector {
|
||||
}
|
||||
|
||||
if x.is_none() {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let mut y: Option<RXingResultPoint> = None;
|
||||
@@ -272,12 +272,12 @@ impl WhiteRectangleDetector {
|
||||
}
|
||||
|
||||
if y.is_none() {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
return Ok(self.center_edges(&y.unwrap(), &z.unwrap(), &x.unwrap(), &t.unwrap()));
|
||||
Ok(self.center_edges(&y.unwrap(), &z.unwrap(), &x.unwrap(), &t.unwrap()))
|
||||
} else {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,7 +299,7 @@ impl WhiteRectangleDetector {
|
||||
return Some(RXingResultPoint::new(x as f32, y as f32));
|
||||
}
|
||||
}
|
||||
return None;
|
||||
None
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -339,19 +339,19 @@ impl WhiteRectangleDetector {
|
||||
let tj = t.getY();
|
||||
|
||||
if yi < self.width as f32 / 2.0f32 {
|
||||
return vec![
|
||||
vec![
|
||||
RXingResultPoint::new(ti - CORR as f32, tj + CORR as f32),
|
||||
RXingResultPoint::new(zi + CORR as f32, zj + CORR as f32),
|
||||
RXingResultPoint::new(xi - CORR as f32, xj - CORR as f32),
|
||||
RXingResultPoint::new(yi + CORR as f32, yj - CORR as f32),
|
||||
];
|
||||
]
|
||||
} else {
|
||||
return vec![
|
||||
vec![
|
||||
RXingResultPoint::new(ti + CORR as f32, tj + CORR as f32),
|
||||
RXingResultPoint::new(zi + CORR as f32, zj - CORR as f32),
|
||||
RXingResultPoint::new(xi - CORR as f32, xj + CORR as f32),
|
||||
RXingResultPoint::new(yi - CORR as f32, yj - CORR as f32),
|
||||
];
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,6 +379,6 @@ impl WhiteRectangleDetector {
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ impl ECIEncoderSet {
|
||||
// for (CharsetEncoder encoder : ENCODERS) {
|
||||
if encoder
|
||||
.encode(
|
||||
&stringToEncode.get(i).unwrap(),
|
||||
stringToEncode.get(i).unwrap(),
|
||||
encoding::EncoderTrap::Strict,
|
||||
)
|
||||
.is_ok()
|
||||
@@ -185,9 +185,10 @@ impl ECIEncoderSet {
|
||||
//Compute priorityEncoderIndex by looking up priorityCharset in encoders
|
||||
// if priorityCharset != null {
|
||||
if priorityCharset.is_some() {
|
||||
for i in 0..encoders.len() {
|
||||
// for i in 0..encoders.len() {
|
||||
for (i, encoder) in encoders.iter().enumerate() {
|
||||
// for (int i = 0; i < encoders.length; i++) {
|
||||
if priorityCharset.as_ref().unwrap().name() == encoders[i].name() {
|
||||
if priorityCharset.as_ref().unwrap().name() == encoder.name() {
|
||||
priorityEncoderIndexValue = Some(i);
|
||||
break;
|
||||
}
|
||||
@@ -197,13 +198,17 @@ impl ECIEncoderSet {
|
||||
//invariants
|
||||
assert_eq!(encoders[0].name(), encoding::all::ISO_8859_1.name());
|
||||
Self {
|
||||
encoders: encoders,
|
||||
encoders,
|
||||
priorityEncoderIndex: priorityEncoderIndexValue,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
return self.encoders.len();
|
||||
self.encoders.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.encoders.is_empty()
|
||||
}
|
||||
|
||||
pub fn getCharsetName(&self, index: usize) -> &'static str {
|
||||
@@ -213,7 +218,7 @@ impl ECIEncoderSet {
|
||||
|
||||
pub fn getCharset(&self, index: usize) -> EncodingRef {
|
||||
assert!(index < self.len());
|
||||
return self.encoders[index];
|
||||
self.encoders[index]
|
||||
}
|
||||
|
||||
pub fn getECIValue(&self, encoderIndex: usize) -> u32 {
|
||||
@@ -240,9 +245,9 @@ impl ECIEncoderSet {
|
||||
pub fn encode_char(&self, c: &str, encoderIndex: usize) -> Vec<u8> {
|
||||
assert!(encoderIndex < self.len());
|
||||
let encoder = self.encoders[encoderIndex];
|
||||
let enc_data = encoder.encode(&c.to_string(), encoding::EncoderTrap::Strict);
|
||||
let enc_data = encoder.encode(c, encoding::EncoderTrap::Strict);
|
||||
assert!(enc_data.is_ok());
|
||||
return enc_data.unwrap();
|
||||
enc_data.unwrap()
|
||||
}
|
||||
|
||||
pub fn encode_string(&self, s: &str, encoderIndex: usize) -> Vec<u8> {
|
||||
|
||||
@@ -163,8 +163,8 @@ impl ECIStringBuilder {
|
||||
/**
|
||||
* @return true iff nothing has been appended
|
||||
*/
|
||||
pub fn is_empty(&self) -> bool {
|
||||
return self.current_bytes.is_empty() && self.result.is_empty();
|
||||
pub fn is_empty(&mut self) -> bool {
|
||||
self.current_bytes.is_empty() && self.result.is_empty()
|
||||
}
|
||||
|
||||
pub fn build_result(mut self) -> Self {
|
||||
@@ -180,3 +180,9 @@ impl fmt::Display for ECIStringBuilder {
|
||||
write!(f, "{}", self.result)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ECIStringBuilder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,9 +72,10 @@ impl Binarizer for GlobalHistogramBinarizer {
|
||||
|
||||
if width < 3 {
|
||||
// Special case for very small images
|
||||
for x in 0..width {
|
||||
for (x, lum) in localLuminances.iter().enumerate().take(width) {
|
||||
// for x in 0..width {
|
||||
// for (int x = 0; x < width; x++) {
|
||||
if (localLuminances[x] as u32) < blackPoint {
|
||||
if (*lum as u32) < blackPoint {
|
||||
row.set(x);
|
||||
}
|
||||
}
|
||||
@@ -109,7 +110,7 @@ impl Binarizer for GlobalHistogramBinarizer {
|
||||
&self,
|
||||
source: Box<dyn crate::LuminanceSource>,
|
||||
) -> Rc<RefCell<dyn Binarizer>> {
|
||||
return Rc::new(RefCell::new(GlobalHistogramBinarizer::new(source)));
|
||||
Rc::new(RefCell::new(GlobalHistogramBinarizer::new(source)))
|
||||
}
|
||||
|
||||
fn getWidth(&self) -> usize {
|
||||
@@ -134,7 +135,7 @@ impl GlobalHistogramBinarizer {
|
||||
height: source.getHeight(),
|
||||
black_matrix: None,
|
||||
black_row_cache: vec![None; source.getHeight()],
|
||||
source: source,
|
||||
source,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,7 +173,7 @@ impl GlobalHistogramBinarizer {
|
||||
let offset = y * width;
|
||||
for x in 0..width {
|
||||
// for (int x = 0; x < width; x++) {
|
||||
let pixel = localLuminances[offset + x] & 0xff;
|
||||
let pixel = localLuminances[offset + x];
|
||||
if (pixel as u32) < blackPoint {
|
||||
matrix.set(x as u32, y as u32);
|
||||
}
|
||||
@@ -198,25 +199,27 @@ impl GlobalHistogramBinarizer {
|
||||
let mut maxBucketCount = 0;
|
||||
let mut firstPeak = 0;
|
||||
let mut firstPeakSize = 0;
|
||||
for x in 0..numBuckets {
|
||||
for (x, bucket) in buckets.iter().enumerate().take(numBuckets) {
|
||||
// for x in 0..numBuckets {
|
||||
// for (int x = 0; x < numBuckets; x++) {
|
||||
if buckets[x] > firstPeakSize {
|
||||
if *bucket > firstPeakSize {
|
||||
firstPeak = x;
|
||||
firstPeakSize = buckets[x];
|
||||
firstPeakSize = *bucket;
|
||||
}
|
||||
if buckets[x] > maxBucketCount {
|
||||
maxBucketCount = buckets[x];
|
||||
if *bucket > maxBucketCount {
|
||||
maxBucketCount = *bucket;
|
||||
}
|
||||
}
|
||||
|
||||
// Find the second-tallest peak which is somewhat far from the tallest peak.
|
||||
let mut secondPeak = 0;
|
||||
let mut secondPeakScore = 0;
|
||||
for x in 0..numBuckets {
|
||||
for (x, bucket) in buckets.iter().enumerate().take(numBuckets) {
|
||||
// for x in 0..numBuckets {
|
||||
// for (int x = 0; x < numBuckets; x++) {
|
||||
let distanceToBiggest = (x as i32 - firstPeak as i32).abs() as u32;
|
||||
let distanceToBiggest = (x as i32 - firstPeak as i32).unsigned_abs();
|
||||
// Encourage more distant second peaks by multiplying by square of distance.
|
||||
let score = buckets[x] * distanceToBiggest as u32 * distanceToBiggest as u32;
|
||||
let score = *bucket * distanceToBiggest * distanceToBiggest;
|
||||
if score > secondPeakScore {
|
||||
secondPeak = x;
|
||||
secondPeakScore = score;
|
||||
@@ -231,9 +234,9 @@ impl GlobalHistogramBinarizer {
|
||||
// If there is too little contrast in the image to pick a meaningful black point, throw rather
|
||||
// than waste time trying to decode the image, and risk false positives.
|
||||
if secondPeak - firstPeak <= numBuckets / 16 {
|
||||
return Err(Exceptions::NotFoundException(
|
||||
return Err(Exceptions::NotFoundException(Some(
|
||||
"secondPeak - firstPeak <= numBuckets / 16 ".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
// Find a valley between them that is low and closer to the white peak.
|
||||
|
||||
@@ -144,9 +144,7 @@ pub trait GridSampler {
|
||||
let x = points[offset] as i32;
|
||||
let y = points[offset + 1] as i32;
|
||||
if x < -1 || x > width.try_into().unwrap() || y < -1 || y > height.try_into().unwrap() {
|
||||
return Err(Exceptions::NotFoundException(
|
||||
"getNotFoundInstance".to_owned(),
|
||||
));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
nudged = false;
|
||||
if x == -1 {
|
||||
@@ -173,9 +171,7 @@ pub trait GridSampler {
|
||||
let x = points[offset as usize] as i32;
|
||||
let y = points[offset as usize + 1] as i32;
|
||||
if x < -1 || x > width.try_into().unwrap() || y < -1 || y > height.try_into().unwrap() {
|
||||
return Err(Exceptions::NotFoundException(
|
||||
"getNotFoundInstance".to_owned(),
|
||||
));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
nudged = false;
|
||||
if x == -1 {
|
||||
|
||||
@@ -99,16 +99,16 @@ impl HybridBinarizer {
|
||||
let ghb = GlobalHistogramBinarizer::new(source);
|
||||
Self {
|
||||
black_matrix: None,
|
||||
ghb: ghb,
|
||||
ghb,
|
||||
}
|
||||
}
|
||||
|
||||
fn calculateBlackMatrix(ghb: &mut GlobalHistogramBinarizer) -> Result<BitMatrix, Exceptions> {
|
||||
let matrix;
|
||||
// let matrix;
|
||||
let source = ghb.getLuminanceSource();
|
||||
let width = source.getWidth();
|
||||
let height = source.getHeight();
|
||||
if width >= HybridBinarizer::MINIMUM_DIMENSION
|
||||
let matrix = if width >= HybridBinarizer::MINIMUM_DIMENSION
|
||||
&& height >= HybridBinarizer::MINIMUM_DIMENSION
|
||||
{
|
||||
let luminances = source.getMatrix();
|
||||
@@ -138,12 +138,12 @@ impl HybridBinarizer {
|
||||
&black_points,
|
||||
&mut new_matrix,
|
||||
);
|
||||
matrix = Ok(new_matrix);
|
||||
Ok(new_matrix)
|
||||
} else {
|
||||
// If the image is too small, fall back to the global histogram approach.
|
||||
let m = ghb.getBlackMatrix()?;
|
||||
matrix = Ok(m.clone());
|
||||
}
|
||||
Ok(m.clone())
|
||||
};
|
||||
// dbg!(matrix.to_string());
|
||||
matrix
|
||||
}
|
||||
@@ -159,7 +159,7 @@ impl HybridBinarizer {
|
||||
sub_height: u32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
black_points: &Vec<Vec<u32>>,
|
||||
black_points: &[Vec<u32>],
|
||||
matrix: &mut BitMatrix,
|
||||
) {
|
||||
let maxYOffset = height - HybridBinarizer::BLOCK_SIZE as u32;
|
||||
|
||||
@@ -52,7 +52,7 @@ impl ECIInput for MinimalECIInput {
|
||||
* @return the number of {@code char}s in this sequence
|
||||
*/
|
||||
fn length(&self) -> usize {
|
||||
return self.bytes.len();
|
||||
self.bytes.len()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,13 +73,15 @@ impl ECIInput for MinimalECIInput {
|
||||
*/
|
||||
fn charAt(&self, index: usize) -> Result<char, Exceptions> {
|
||||
if index >= self.length() {
|
||||
return Err(Exceptions::IndexOutOfBoundsException(index.to_string()));
|
||||
return Err(Exceptions::IndexOutOfBoundsException(Some(
|
||||
index.to_string(),
|
||||
)));
|
||||
}
|
||||
if self.isECI(index as u32)? {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"value at {} is not a character but an ECI",
|
||||
index
|
||||
)));
|
||||
))));
|
||||
}
|
||||
if self.isFNC1(index)? {
|
||||
Ok(self.fnc1 as u8 as char)
|
||||
@@ -110,16 +112,18 @@ impl ECIInput for MinimalECIInput {
|
||||
*/
|
||||
fn subSequence(&self, start: usize, end: usize) -> Result<Vec<char>, Exceptions> {
|
||||
if start > end || end > self.length() {
|
||||
return Err(Exceptions::IndexOutOfBoundsException(start.to_string()));
|
||||
return Err(Exceptions::IndexOutOfBoundsException(Some(
|
||||
start.to_string(),
|
||||
)));
|
||||
}
|
||||
let mut result = String::new();
|
||||
for i in start..end {
|
||||
// for (int i = start; i < end; i++) {
|
||||
if self.isECI(i as u32)? {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"value at {} is not a character but an ECI",
|
||||
i
|
||||
)));
|
||||
))));
|
||||
}
|
||||
result.push_str(&self.charAt(i)?.to_string());
|
||||
}
|
||||
@@ -139,7 +143,9 @@ impl ECIInput for MinimalECIInput {
|
||||
*/
|
||||
fn isECI(&self, index: u32) -> Result<bool, Exceptions> {
|
||||
if index >= self.length() as u32 {
|
||||
return Err(Exceptions::IndexOutOfBoundsException(index.to_string()));
|
||||
return Err(Exceptions::IndexOutOfBoundsException(Some(
|
||||
index.to_string(),
|
||||
)));
|
||||
}
|
||||
Ok(self.bytes[index as usize] > 255) // && self.bytes[index as usize] <= u16::MAX)
|
||||
}
|
||||
@@ -164,19 +170,21 @@ impl ECIInput for MinimalECIInput {
|
||||
*/
|
||||
fn getECIValue(&self, index: usize) -> Result<i32, Exceptions> {
|
||||
if index >= self.length() {
|
||||
return Err(Exceptions::IndexOutOfBoundsException(index.to_string()));
|
||||
return Err(Exceptions::IndexOutOfBoundsException(Some(
|
||||
index.to_string(),
|
||||
)));
|
||||
}
|
||||
if !self.isECI(index as u32)? {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"value at {} is not an ECI but a character",
|
||||
index
|
||||
)));
|
||||
))));
|
||||
}
|
||||
Ok((self.bytes[index] as u32 - 256) as i32)
|
||||
}
|
||||
|
||||
fn haveNCharacters(&self, index: usize, n: usize) -> bool {
|
||||
if index + n - 1 >= self.bytes.len() {
|
||||
if index + n > self.bytes.len() {
|
||||
return false;
|
||||
}
|
||||
for i in 0..n {
|
||||
@@ -185,7 +193,7 @@ impl ECIInput for MinimalECIInput {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
true
|
||||
}
|
||||
}
|
||||
impl MinimalECIInput {
|
||||
@@ -210,13 +218,14 @@ impl MinimalECIInput {
|
||||
let bytes = if encoderSet.len() == 1 {
|
||||
//optimization for the case when all can be encoded without ECI in ISO-8859-1
|
||||
let mut bytes_hld = vec![0; stringToEncode.len()];
|
||||
for i in 0..stringToEncode.len() {
|
||||
for (i, byt) in bytes_hld.iter_mut().enumerate().take(stringToEncode.len()) {
|
||||
// for i in 0..stringToEncode.len() {
|
||||
// for (int i = 0; i < bytes.length; i++) {
|
||||
let c = stringToEncode.get(i).unwrap();
|
||||
bytes_hld[i] = if fnc1.is_some() && c == fnc1.as_ref().unwrap() {
|
||||
*byt = if fnc1.is_some() && c == fnc1.as_ref().unwrap() {
|
||||
1000
|
||||
} else {
|
||||
c.chars().nth(0).unwrap() as u16
|
||||
c.chars().next().unwrap() as u16
|
||||
};
|
||||
}
|
||||
bytes_hld
|
||||
@@ -225,10 +234,10 @@ impl MinimalECIInput {
|
||||
};
|
||||
|
||||
Self {
|
||||
bytes: bytes,
|
||||
bytes,
|
||||
fnc1: if let Some(fnc1_exists) = fnc1 {
|
||||
//}.as_ref().unwrap().chars().nth(0).unwrap() as u16,
|
||||
fnc1_exists.chars().nth(0).unwrap() as u16
|
||||
fnc1_exists.chars().next().unwrap() as u16
|
||||
} else {
|
||||
1000
|
||||
},
|
||||
@@ -252,12 +261,14 @@ impl MinimalECIInput {
|
||||
*/
|
||||
pub fn isFNC1(&self, index: usize) -> Result<bool, Exceptions> {
|
||||
if index >= self.length() {
|
||||
return Err(Exceptions::IndexOutOfBoundsException(index.to_string()));
|
||||
return Err(Exceptions::IndexOutOfBoundsException(Some(
|
||||
index.to_string(),
|
||||
)));
|
||||
}
|
||||
Ok(self.bytes[index] == 1000)
|
||||
}
|
||||
|
||||
fn addEdge(edges: &mut Vec<Vec<Option<Rc<InputEdge>>>>, to: usize, edge: Rc<InputEdge>) {
|
||||
fn addEdge(edges: &mut [Vec<Option<Rc<InputEdge>>>], to: usize, edge: Rc<InputEdge>) {
|
||||
if edges[to][edge.encoderIndex].is_none()
|
||||
|| edges[to][edge.encoderIndex]
|
||||
.clone()
|
||||
@@ -272,7 +283,7 @@ impl MinimalECIInput {
|
||||
fn addEdges(
|
||||
stringToEncode: &str,
|
||||
encoderSet: &ECIEncoderSet,
|
||||
edges: &mut Vec<Vec<Option<Rc<InputEdge>>>>,
|
||||
edges: &mut [Vec<Option<Rc<InputEdge>>>],
|
||||
from: usize,
|
||||
previous: Option<Rc<InputEdge>>,
|
||||
fnc1: Option<&str>,
|
||||
@@ -285,7 +296,7 @@ impl MinimalECIInput {
|
||||
//if let Some(fnc1) = fnc1 {
|
||||
if encoderSet.getPriorityEncoderIndex().is_some()
|
||||
&& ((fnc1.is_some()
|
||||
&& ch.chars().nth(0).unwrap() == fnc1.as_ref().unwrap().chars().nth(0).unwrap())
|
||||
&& ch.chars().next().unwrap() == fnc1.as_ref().unwrap().chars().next().unwrap())
|
||||
|| encoderSet.canEncode(ch, encoderSet.getPriorityEncoderIndex().unwrap()))
|
||||
{
|
||||
start = encoderSet.getPriorityEncoderIndex().unwrap();
|
||||
@@ -296,7 +307,7 @@ impl MinimalECIInput {
|
||||
for i in start..end {
|
||||
// for (int i = start; i < end; i++) {
|
||||
if (fnc1.is_some()
|
||||
&& ch.chars().nth(0).unwrap() == fnc1.as_ref().unwrap().chars().nth(0).unwrap())
|
||||
&& ch.chars().next().unwrap() == fnc1.as_ref().unwrap().chars().next().unwrap())
|
||||
|| encoderSet.canEncode(ch, i)
|
||||
{
|
||||
Self::addEdge(
|
||||
@@ -380,19 +391,17 @@ impl MinimalECIInput {
|
||||
// 0..0,
|
||||
// [256 as u16 + encoderSet.getECIValue(c.encoderIndex) as u16],
|
||||
// );
|
||||
intsAL.insert(
|
||||
0,
|
||||
256 as u16 + encoderSet.getECIValue(c.encoderIndex) as u16,
|
||||
);
|
||||
intsAL.insert(0, 256_u16 + encoderSet.getECIValue(c.encoderIndex) as u16);
|
||||
}
|
||||
current = c.previous.clone();
|
||||
}
|
||||
let mut ints = vec![0; intsAL.len()];
|
||||
for i in 0..ints.len() {
|
||||
// for (int i = 0; i < ints.length; i++) {
|
||||
ints[i] = *intsAL.get(i).unwrap() as u16;
|
||||
}
|
||||
return ints;
|
||||
// for i in 0..ints.len() {
|
||||
// // for (int i = 0; i < ints.length; i++) {
|
||||
// ints[i] = *intsAL.get(i).unwrap();
|
||||
// }
|
||||
ints[..].copy_from_slice(&intsAL[..]);
|
||||
ints
|
||||
}
|
||||
}
|
||||
|
||||
@@ -432,7 +441,7 @@ impl InputEdge {
|
||||
String::from(c)
|
||||
},
|
||||
encoderIndex,
|
||||
previous: Some(prev.clone()),
|
||||
previous: Some(prev),
|
||||
cachedTotalSize: size,
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -81,7 +81,7 @@ impl PerspectiveTransform {
|
||||
let q_to_s = PerspectiveTransform::quadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3);
|
||||
let s_to_q =
|
||||
PerspectiveTransform::squareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p);
|
||||
return s_to_q.times(&q_to_s);
|
||||
s_to_q.times(&q_to_s)
|
||||
}
|
||||
|
||||
pub fn transform_points_single(&self, points: &mut [f32]) {
|
||||
@@ -133,7 +133,7 @@ impl PerspectiveTransform {
|
||||
let dy3 = y0 - y1 + y2 - y3;
|
||||
if dx3 == 0.0f32 && dy3 == 0.0f32 {
|
||||
// Affine
|
||||
return PerspectiveTransform::new(
|
||||
PerspectiveTransform::new(
|
||||
x1 - x0,
|
||||
x2 - x1,
|
||||
x0,
|
||||
@@ -143,7 +143,7 @@ impl PerspectiveTransform {
|
||||
0.0f32,
|
||||
0.0f32,
|
||||
1.0f32,
|
||||
);
|
||||
)
|
||||
} else {
|
||||
let dx1 = x1 - x2;
|
||||
let dx2 = x3 - x2;
|
||||
@@ -152,7 +152,7 @@ impl PerspectiveTransform {
|
||||
let denominator = dx1 * dy2 - dx2 * dy1;
|
||||
let a13 = (dx3 * dy2 - dx2 * dy3) / denominator;
|
||||
let a23 = (dx1 * dy3 - dx3 * dy1) / denominator;
|
||||
return PerspectiveTransform::new(
|
||||
PerspectiveTransform::new(
|
||||
x1 - x0 + a13 * x1,
|
||||
x3 - x0 + a23 * x3,
|
||||
x0,
|
||||
@@ -162,7 +162,7 @@ impl PerspectiveTransform {
|
||||
a13,
|
||||
a23,
|
||||
1.0f32,
|
||||
);
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,13 +177,12 @@ impl PerspectiveTransform {
|
||||
y3: f32,
|
||||
) -> Self {
|
||||
// Here, the adjoint serves as the inverse
|
||||
return PerspectiveTransform::squareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3)
|
||||
.buildAdjoint();
|
||||
PerspectiveTransform::squareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3).buildAdjoint()
|
||||
}
|
||||
|
||||
fn buildAdjoint(&self) -> Self {
|
||||
// Adjoint is the transpose of the cofactor matrix:
|
||||
return PerspectiveTransform::new(
|
||||
PerspectiveTransform::new(
|
||||
self.a22 * self.a33 - self.a23 * self.a32,
|
||||
self.a23 * self.a31 - self.a21 * self.a33,
|
||||
self.a21 * self.a32 - self.a22 * self.a31,
|
||||
@@ -193,11 +192,11 @@ impl PerspectiveTransform {
|
||||
self.a12 * self.a23 - self.a13 * self.a22,
|
||||
self.a13 * self.a21 - self.a11 * self.a23,
|
||||
self.a11 * self.a22 - self.a12 * self.a21,
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
fn times(&self, other: &Self) -> Self {
|
||||
return PerspectiveTransform::new(
|
||||
PerspectiveTransform::new(
|
||||
self.a11 * other.a11 + self.a21 * other.a12 + self.a31 * other.a13,
|
||||
self.a11 * other.a21 + self.a21 * other.a22 + self.a31 * other.a23,
|
||||
self.a11 * other.a31 + self.a21 * other.a32 + self.a31 * other.a33,
|
||||
@@ -207,6 +206,6 @@ impl PerspectiveTransform {
|
||||
self.a13 * other.a11 + self.a23 * other.a12 + self.a33 * other.a13,
|
||||
self.a13 * other.a21 + self.a23 * other.a22 + self.a33 * other.a23,
|
||||
self.a13 * other.a31 + self.a23 * other.a32 + self.a33 * other.a33,
|
||||
);
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,9 +38,9 @@ const DECODER_TEST_ITERATIONS: i32 = 10;
|
||||
fn test_data_matrix() {
|
||||
let dm256 = super::get_predefined_genericgf(super::PredefinedGenericGF::DataMatrixField256);
|
||||
// real life test cases
|
||||
test_encode_decode(&dm256, &vec![142, 164, 186], &vec![114, 25, 5, 88, 102]);
|
||||
test_encode_decode(dm256, &vec![142, 164, 186], &vec![114, 25, 5, 88, 102]);
|
||||
test_encode_decode(
|
||||
&dm256,
|
||||
dm256,
|
||||
&vec![
|
||||
0x69, 0x75, 0x75, 0x71, 0x3B, 0x30, 0x30, 0x64, 0x70, 0x65, 0x66, 0x2F, 0x68, 0x70,
|
||||
0x70, 0x68, 0x6D, 0x66, 0x2F, 0x64, 0x70, 0x6E, 0x30, 0x71, 0x30, 0x7B, 0x79, 0x6A,
|
||||
@@ -62,7 +62,7 @@ fn test_qr_code() {
|
||||
let qrcf256 = super::get_predefined_genericgf(super::PredefinedGenericGF::QrCodeField256);
|
||||
// Test case from example given in ISO 18004, Annex I
|
||||
test_encode_decode(
|
||||
&qrcf256,
|
||||
qrcf256,
|
||||
&vec![
|
||||
0x10, 0x20, 0x0C, 0x56, 0x61, 0x80, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11,
|
||||
0xEC, 0x11,
|
||||
@@ -70,7 +70,7 @@ fn test_qr_code() {
|
||||
&vec![0xA5, 0x24, 0xD4, 0xC1, 0xED, 0x36, 0xC7, 0x87, 0x2C, 0x55],
|
||||
);
|
||||
test_encode_decode(
|
||||
&qrcf256,
|
||||
qrcf256,
|
||||
&vec![
|
||||
0x72, 0x67, 0x2F, 0x77, 0x69, 0x6B, 0x69, 0x2F, 0x4D, 0x61, 0x69, 0x6E, 0x5F, 0x50,
|
||||
0x61, 0x67, 0x65, 0x3B, 0x3B, 0x00, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11, 0xEC, 0x11,
|
||||
@@ -92,28 +92,28 @@ fn test_qr_code() {
|
||||
fn test_aztec() {
|
||||
// real life test cases
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecParam),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecParam),
|
||||
&vec![0x5, 0x6],
|
||||
&vec![0x3, 0x2, 0xB, 0xB, 0x7],
|
||||
);
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecParam),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecParam),
|
||||
&vec![0x0, 0x0, 0x0, 0x9],
|
||||
&vec![0xA, 0xD, 0x8, 0x6, 0x5, 0x6],
|
||||
);
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecParam),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecParam),
|
||||
&vec![0x2, 0x8, 0x8, 0x7],
|
||||
&vec![0xE, 0xC, 0xA, 0x9, 0x6, 0x8],
|
||||
);
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData6),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData6),
|
||||
&vec![0x9, 0x32, 0x1, 0x29, 0x2F, 0x2, 0x27, 0x25, 0x1, 0x1B],
|
||||
&vec![0x2C, 0x2, 0xD, 0xD, 0xA, 0x16, 0x28, 0x9, 0x22, 0xA, 0x14],
|
||||
);
|
||||
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData8),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData8),
|
||||
&vec![
|
||||
0xE0, 0x86, 0x42, 0x98, 0xE8, 0x4A, 0x96, 0xC6, 0xB9, 0xF0, 0x8C, 0xA7, 0x4A, 0xDA,
|
||||
0xF8, 0xCE, 0xB7, 0xDE, 0x88, 0x64, 0x29, 0x8E, 0x84, 0xA9, 0x6C, 0x6B, 0x9F, 0x08,
|
||||
@@ -129,7 +129,7 @@ fn test_aztec() {
|
||||
],
|
||||
);
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData10),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData10),
|
||||
&vec![
|
||||
0x15C, 0x1E1, 0x2D5, 0x02E, 0x048, 0x1E2, 0x037, 0x0CD, 0x02E, 0x056, 0x26A, 0x281,
|
||||
0x1C2, 0x1A6, 0x296, 0x045, 0x041, 0x0AA, 0x095, 0x2CE, 0x003, 0x38F, 0x2CD, 0x1A2,
|
||||
@@ -176,7 +176,7 @@ fn test_aztec() {
|
||||
],
|
||||
);
|
||||
test_encode_decode(
|
||||
&super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData12),
|
||||
super::get_predefined_genericgf(super::PredefinedGenericGF::AztecData12),
|
||||
&vec![
|
||||
0x571, 0xE1B, 0x542, 0xE12, 0x1E2, 0x0DC, 0xCD0, 0xB85, 0x69A, 0xA81, 0x709, 0xA6A,
|
||||
0x584, 0x510, 0x4AA, 0x256, 0xCE0, 0x0F8, 0xFB3, 0x5A2, 0x0D9, 0xAD1, 0x389, 0x09C,
|
||||
@@ -432,7 +432,7 @@ fn test_encode_decode_random(field: GenericGFRef, dataSize: usize, ecSize: usize
|
||||
ecWords[0..ecSize].clone_from_slice(&message[dataSize..dataSize + ecSize]);
|
||||
//System.arraycopy(message, dataSize, ecWords, 0, ecSize);
|
||||
// check to see if Decoder can fix up to ecWords/2 random errors
|
||||
test_decoder(&field, &dataWords, &ecWords);
|
||||
test_decoder(field, &dataWords, &ecWords);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -536,7 +536,7 @@ fn test_decoder(field: GenericGFRef, dataWords: &Vec<i32>, ecWords: &Vec<i32>) {
|
||||
ecWords.len(),
|
||||
i
|
||||
),
|
||||
&dataWords,
|
||||
dataWords,
|
||||
&message,
|
||||
);
|
||||
}
|
||||
@@ -545,7 +545,7 @@ fn test_decoder(field: GenericGFRef, dataWords: &Vec<i32>, ecWords: &Vec<i32>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_data_equals(message: String, expected: &Vec<i32>, received: &Vec<i32>) {
|
||||
fn assert_data_equals(message: String, expected: &Vec<i32>, received: &[i32]) {
|
||||
for i in 0..expected.len() {
|
||||
//for (int i = 0; i < expected.length; i++) {
|
||||
if expected[i] != received[i] {
|
||||
@@ -553,7 +553,7 @@ fn assert_data_equals(message: String, expected: &Vec<i32>, received: &Vec<i32>)
|
||||
"{}. Mismatch at {}. Expected {}, got {}",
|
||||
message,
|
||||
i,
|
||||
array_to_string(&expected),
|
||||
array_to_string(expected),
|
||||
array_to_string(&received[0..expected.len()])
|
||||
);
|
||||
//fail();
|
||||
@@ -563,12 +563,12 @@ fn assert_data_equals(message: String, expected: &Vec<i32>, received: &Vec<i32>)
|
||||
|
||||
fn array_to_string(data: &[i32]) -> String {
|
||||
let mut sb = String::from("{");
|
||||
for i in 0..data.len() {
|
||||
for dtm in data.iter() {
|
||||
//for (int i = 0; i < data.length; i++) {
|
||||
//sb.append(String.format(i > 0 ? ",%X" : "%X", data[i]));
|
||||
sb.push_str(&format!("{}", data[i]));
|
||||
sb.push_str(&format!("{}", *dtm));
|
||||
}
|
||||
sb.push_str("}");
|
||||
sb.push('}');
|
||||
|
||||
sb
|
||||
}
|
||||
|
||||
@@ -42,10 +42,11 @@ impl GenericGF {
|
||||
let mut expTable = vec![0; size];
|
||||
let mut logTable = vec![0; size];
|
||||
let mut x = 1;
|
||||
for i in 0..size {
|
||||
for expTableEntry in expTable.iter_mut().take(size) {
|
||||
// for i in 0..size {
|
||||
//for (int i = 0; i < size; i++) {
|
||||
//expTable.push(x);
|
||||
expTable[i] = x;
|
||||
*expTableEntry = x;
|
||||
x *= 2; // we're assuming the generator alpha is 2
|
||||
if x >= size as i32 {
|
||||
x ^= primitive;
|
||||
@@ -53,10 +54,11 @@ impl GenericGF {
|
||||
x &= sz_m_1;
|
||||
}
|
||||
}
|
||||
for i in 0..size - 1 {
|
||||
for (i, loc) in expTable.iter().enumerate().take(size - 1) {
|
||||
// for i in 0..size - 1 {
|
||||
//for (int i = 0; i < size - 1; i++) {
|
||||
let loc: usize = expTable[i] as usize;
|
||||
logTable[loc] = i as i32;
|
||||
// let loc: usize = expTable[i] as usize;
|
||||
logTable[*loc as usize] = i as i32;
|
||||
}
|
||||
logTable[0] = 0;
|
||||
|
||||
@@ -106,7 +108,7 @@ impl GenericGF {
|
||||
}
|
||||
let mut coefficients = vec![0; degree + 1];
|
||||
coefficients[0] = coefficient;
|
||||
return GenericGFPoly::new(source, &coefficients).unwrap();
|
||||
GenericGFPoly::new(source, &coefficients).unwrap()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,7 +117,7 @@ impl GenericGF {
|
||||
* @return sum/difference of a and b
|
||||
*/
|
||||
pub fn addOrSubtract(a: i32, b: i32) -> i32 {
|
||||
return a ^ b;
|
||||
a ^ b
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,7 +125,7 @@ impl GenericGF {
|
||||
*/
|
||||
pub fn exp(&self, a: i32) -> i32 {
|
||||
// let pos: usize = a.try_into().unwrap();
|
||||
return self.expTable[a as usize];
|
||||
self.expTable[a as usize]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,10 +133,10 @@ impl GenericGF {
|
||||
*/
|
||||
pub fn log(&self, a: i32) -> Result<i32, Exceptions> {
|
||||
if a == 0 {
|
||||
return Err(Exceptions::IllegalArgumentException("".to_owned()));
|
||||
return Err(Exceptions::IllegalArgumentException(None));
|
||||
}
|
||||
// let pos: usize = a.try_into().unwrap();
|
||||
return Ok(self.logTable[a as usize]);
|
||||
Ok(self.logTable[a as usize])
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,11 +144,11 @@ impl GenericGF {
|
||||
*/
|
||||
pub fn inverse(&self, a: i32) -> Result<i32, Exceptions> {
|
||||
if a == 0 {
|
||||
return Err(Exceptions::ArithmeticException("".to_owned()));
|
||||
return Err(Exceptions::ArithmeticException(None));
|
||||
}
|
||||
let log_t_loc: usize = a as usize;
|
||||
let loc: usize = ((self.size as i32) - self.logTable[log_t_loc] - 1) as usize;
|
||||
return Ok(self.expTable[loc]);
|
||||
Ok(self.expTable[loc])
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,15 +161,15 @@ impl GenericGF {
|
||||
let a_loc: usize = a as usize; //.try_into().unwrap();
|
||||
let b_loc: usize = b as usize; //.try_into().unwrap();
|
||||
let comb_loc: usize = (self.logTable[a_loc] + self.logTable[b_loc]) as usize;
|
||||
return self.expTable[comb_loc % (self.size - 1)];
|
||||
self.expTable[comb_loc % (self.size - 1)]
|
||||
}
|
||||
|
||||
pub fn getSize(&self) -> usize {
|
||||
return self.size;
|
||||
self.size
|
||||
}
|
||||
|
||||
pub fn getGeneratorBase(&self) -> i32 {
|
||||
return self.generatorBase;
|
||||
self.generatorBase
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,13 +48,13 @@ impl GenericGFPoly {
|
||||
* constant polynomial (that is, it is not the monomial "0")
|
||||
*/
|
||||
pub fn new(field: GenericGFRef, coefficients: &Vec<i32>) -> Result<Self, Exceptions> {
|
||||
if coefficients.len() == 0 {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
if coefficients.is_empty() {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"coefficients.len()".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
Ok(Self {
|
||||
field: field,
|
||||
field,
|
||||
coefficients: {
|
||||
let coefficients_length = coefficients.len();
|
||||
if coefficients_length > 1 && coefficients[0] == 0 {
|
||||
@@ -85,28 +85,28 @@ impl GenericGFPoly {
|
||||
}
|
||||
|
||||
pub fn getCoefficients(&self) -> &Vec<i32> {
|
||||
return &self.coefficients;
|
||||
&self.coefficients
|
||||
}
|
||||
|
||||
/**
|
||||
* @return degree of this polynomial
|
||||
*/
|
||||
pub fn getDegree(&self) -> usize {
|
||||
return self.coefficients.len() - 1;
|
||||
self.coefficients.len() - 1
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true iff this polynomial is the monomial "0"
|
||||
*/
|
||||
pub fn isZero(&self) -> bool {
|
||||
return self.coefficients[0] == 0;
|
||||
self.coefficients[0] == 0
|
||||
}
|
||||
|
||||
/**
|
||||
* @return coefficient of x^degree term in this polynomial
|
||||
*/
|
||||
pub fn getCoefficient(&self, degree: usize) -> i32 {
|
||||
return self.coefficients[self.coefficients.len() - 1 - degree];
|
||||
self.coefficients[self.coefficients.len() - 1 - degree]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,18 +131,18 @@ impl GenericGFPoly {
|
||||
for i in 1..size {
|
||||
//for (int i = 1; i < size; i++) {
|
||||
result = GenericGF::addOrSubtract(
|
||||
self.field.multiply(a as i32, result as i32),
|
||||
self.field.multiply(a as i32, result),
|
||||
self.coefficients[i],
|
||||
);
|
||||
}
|
||||
return result;
|
||||
result
|
||||
}
|
||||
|
||||
pub fn addOrSubtract(&self, other: &GenericGFPoly) -> Result<GenericGFPoly, Exceptions> {
|
||||
if self.field != other.field {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"GenericGFPolys do not have same GenericGF field".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
if self.isZero() {
|
||||
return Ok(other.clone());
|
||||
@@ -171,15 +171,15 @@ impl GenericGFPoly {
|
||||
);
|
||||
}
|
||||
|
||||
return Ok(GenericGFPoly::new(self.field, &sumDiff)?);
|
||||
GenericGFPoly::new(self.field, &sumDiff)
|
||||
}
|
||||
|
||||
pub fn multiply(&self, other: &GenericGFPoly) -> Result<GenericGFPoly, Exceptions> {
|
||||
if self.field != other.field {
|
||||
//if (!field.equals(other.field)) {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"GenericGFPolys do not have same GenericGF field".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
if self.isZero() || other.isZero() {
|
||||
return Ok(self.getZero());
|
||||
@@ -200,7 +200,7 @@ impl GenericGFPoly {
|
||||
);
|
||||
}
|
||||
}
|
||||
return Ok(GenericGFPoly::new(self.field, &product)?);
|
||||
GenericGFPoly::new(self.field, &product)
|
||||
}
|
||||
|
||||
pub fn multiply_with_scalar(&self, scalar: i32) -> GenericGFPoly {
|
||||
@@ -213,11 +213,12 @@ impl GenericGFPoly {
|
||||
let size = self.coefficients.len();
|
||||
|
||||
let mut product = vec![0; size];
|
||||
for i in 0..size {
|
||||
for (i, prod) in product.iter_mut().enumerate().take(size) {
|
||||
// for i in 0..size {
|
||||
//for (int i = 0; i < size; i++) {
|
||||
product[i] = self.field.multiply(self.coefficients[i], scalar);
|
||||
*prod = self.field.multiply(self.coefficients[i], scalar);
|
||||
}
|
||||
return GenericGFPoly::new(self.field, &product).unwrap();
|
||||
GenericGFPoly::new(self.field, &product).unwrap()
|
||||
}
|
||||
|
||||
pub fn getZero(&self) -> Self {
|
||||
@@ -238,11 +239,12 @@ impl GenericGFPoly {
|
||||
}
|
||||
let size = self.coefficients.len();
|
||||
let mut product = vec![0; size + degree];
|
||||
for i in 0..size {
|
||||
for (i, prod) in product.iter_mut().enumerate().take(size) {
|
||||
// for i in 0..size {
|
||||
//for (int i = 0; i < size; i++) {
|
||||
product[i] = self.field.multiply(self.coefficients[i], coefficient);
|
||||
*prod = self.field.multiply(self.coefficients[i], coefficient);
|
||||
}
|
||||
return Ok(GenericGFPoly::new(self.field, &product)?);
|
||||
GenericGFPoly::new(self.field, &product)
|
||||
}
|
||||
|
||||
pub fn divide(
|
||||
@@ -250,14 +252,14 @@ impl GenericGFPoly {
|
||||
other: &GenericGFPoly,
|
||||
) -> Result<(GenericGFPoly, GenericGFPoly), Exceptions> {
|
||||
if self.field != other.field {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"GenericGFPolys do not have same GenericGF field".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
if other.isZero() {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Divide by 0".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
let mut quotient = self.getZero();
|
||||
@@ -267,9 +269,9 @@ impl GenericGFPoly {
|
||||
let inverse_denominator_leading_term = match self.field.inverse(denominator_leading_term) {
|
||||
Ok(val) => val,
|
||||
Err(_issue) => {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"arithmetic issue".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
@@ -285,7 +287,7 @@ impl GenericGFPoly {
|
||||
remainder = remainder.addOrSubtract(&term)?;
|
||||
}
|
||||
|
||||
return Ok((quotient, remainder));
|
||||
Ok((quotient, remainder))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,22 +303,20 @@ impl fmt::Display for GenericGFPoly {
|
||||
if coefficient != 0 {
|
||||
if coefficient < 0 {
|
||||
if degree == self.getDegree() {
|
||||
result.push_str("-");
|
||||
result.push('-');
|
||||
} else {
|
||||
result.push_str(" - ");
|
||||
}
|
||||
coefficient = -coefficient;
|
||||
} else {
|
||||
if result.len() > 0 {
|
||||
result.push_str(" + ");
|
||||
}
|
||||
} else if !result.is_empty() {
|
||||
result.push_str(" + ");
|
||||
}
|
||||
if degree == 0 || coefficient != 1 {
|
||||
if let Ok(alpha_power) = self.field.log(coefficient) {
|
||||
if alpha_power == 0 {
|
||||
result.push_str("1");
|
||||
result.push('1');
|
||||
} else if alpha_power == 1 {
|
||||
result.push_str("a");
|
||||
result.push('a');
|
||||
} else {
|
||||
result.push_str("a^");
|
||||
result.push_str(&format!("{}", alpha_power));
|
||||
@@ -325,7 +325,7 @@ impl fmt::Display for GenericGFPoly {
|
||||
}
|
||||
if degree != 0 {
|
||||
if degree == 1 {
|
||||
result.push_str("x");
|
||||
result.push('x');
|
||||
} else {
|
||||
result.push_str("x^");
|
||||
result.push_str(&format!("{}", degree));
|
||||
|
||||
@@ -48,7 +48,7 @@ pub struct ReedSolomonDecoder {
|
||||
|
||||
impl ReedSolomonDecoder {
|
||||
pub fn new(field: GenericGFRef) -> Self {
|
||||
Self { field: field }
|
||||
Self { field }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,11 +78,7 @@ impl ReedSolomonDecoder {
|
||||
}
|
||||
let syndrome = match GenericGFPoly::new(self.field, &syndromeCoefficients) {
|
||||
Ok(res) => res,
|
||||
Err(_fail) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
"IllegalArgumentException".to_owned(),
|
||||
))
|
||||
}
|
||||
Err(_fail) => return Err(Exceptions::ReedSolomonException(None)),
|
||||
};
|
||||
let sigmaOmega = self.runEuclideanAlgorithm(
|
||||
&GenericGF::buildMonomial(self.field, twoS as usize, 1),
|
||||
@@ -91,21 +87,21 @@ impl ReedSolomonDecoder {
|
||||
)?;
|
||||
let sigma = &sigmaOmega[0];
|
||||
let omega = &sigmaOmega[1];
|
||||
let errorLocations = self.findErrorLocations(&sigma)?;
|
||||
let errorMagnitudes = self.findErrorMagnitudes(&omega, &errorLocations);
|
||||
let errorLocations = self.findErrorLocations(sigma)?;
|
||||
let errorMagnitudes = self.findErrorMagnitudes(omega, &errorLocations);
|
||||
for i in 0..errorLocations.len() {
|
||||
//for (int i = 0; i < errorLocations.length; i++) {
|
||||
let log_value = self.field.log(errorLocations[i] as i32)?;
|
||||
if log_value > received.len() as i32 - 1 {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"Bad error location".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
let position: isize = received.len() as isize - 1 - log_value as isize;
|
||||
if position < 0 {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"Bad error location".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
received[position as usize] =
|
||||
GenericGF::addOrSubtract(received[position as usize], errorMagnitudes[i]);
|
||||
@@ -143,9 +139,9 @@ impl ReedSolomonDecoder {
|
||||
// Divide rLastLast by rLast, with quotient in q and remainder in r
|
||||
if rLast.isZero() {
|
||||
// Oops, Euclidean algorithm already terminated?
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"r_{i-1} was zero".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
r = rLastLast;
|
||||
let mut q = r.getZero();
|
||||
@@ -153,9 +149,9 @@ impl ReedSolomonDecoder {
|
||||
let dltInverse = match self.field.inverse(denominatorLeadingTerm) {
|
||||
Ok(inv) => inv,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"ArithmetricException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
while r.getDegree() >= rLast.getDegree() && !r.isZero() {
|
||||
@@ -167,24 +163,24 @@ impl ReedSolomonDecoder {
|
||||
{
|
||||
Ok(res) => res,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"IllegalArgumentException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
r = match r.addOrSubtract(&match rLast.multiply_by_monomial(degreeDiff, scale) {
|
||||
Ok(res) => res,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"IllegalArgumentException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
}) {
|
||||
Ok(res) => res,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"IllegalArgumentException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -192,47 +188,47 @@ impl ReedSolomonDecoder {
|
||||
t = match (match q.multiply(&tLast) {
|
||||
Ok(res) => res,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"IllegalArgumentException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
})
|
||||
.addOrSubtract(&tLastLast)
|
||||
{
|
||||
Ok(res) => res,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"IllegalArgumentException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
if r.getDegree() >= rLast.getDegree() {
|
||||
return Err(Exceptions::ReedSolomonException(format!(
|
||||
return Err(Exceptions::ReedSolomonException(Some(format!(
|
||||
"Division algorithm failed to reduce polynomial? r: {}, rLast: {}",
|
||||
r, rLast
|
||||
)));
|
||||
))));
|
||||
}
|
||||
}
|
||||
|
||||
let sigmaTildeAtZero = t.getCoefficient(0);
|
||||
if sigmaTildeAtZero == 0 {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"sigmaTilde(0) was zero".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
let inverse = match self.field.inverse(sigmaTildeAtZero) {
|
||||
Ok(res) => res,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"ArithmetricException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
let sigma = t.multiply_with_scalar(inverse);
|
||||
let omega = r.multiply_with_scalar(inverse);
|
||||
return Ok(vec![sigma, omega]);
|
||||
Ok(vec![sigma, omega])
|
||||
}
|
||||
|
||||
fn findErrorLocations(&self, errorLocator: &GenericGFPoly) -> Result<Vec<usize>, Exceptions> {
|
||||
@@ -254,20 +250,20 @@ impl ReedSolomonDecoder {
|
||||
result[e] = match self.field.inverse(i as i32) {
|
||||
Ok(res) => res as usize,
|
||||
Err(_err) => {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"ArithmetricException".to_owned(),
|
||||
))
|
||||
)))
|
||||
}
|
||||
};
|
||||
e += 1;
|
||||
}
|
||||
}
|
||||
if e != numErrors {
|
||||
return Err(Exceptions::ReedSolomonException(
|
||||
return Err(Exceptions::ReedSolomonException(Some(
|
||||
"Error locator degree does not match number of roots".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
return Ok(result);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn findErrorMagnitudes(
|
||||
@@ -282,14 +278,15 @@ impl ReedSolomonDecoder {
|
||||
//for (int i = 0; i < s; i++) {
|
||||
let xiInverse = self.field.inverse(errorLocations[i] as i32).unwrap();
|
||||
let mut denominator = 1;
|
||||
for j in 0..s {
|
||||
for (j, loc) in errorLocations.iter().enumerate().take(s) {
|
||||
// for j in 0..s {
|
||||
//for (int j = 0; j < s; j++) {
|
||||
if i != j {
|
||||
//denominator = field.multiply(denominator,
|
||||
// GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse)));
|
||||
// Above should work but fails on some Apple and Linux JDKs due to a Hotspot bug.
|
||||
// Below is a funny-looking workaround from Steven Parkes
|
||||
let term = self.field.multiply(errorLocations[j] as i32, xiInverse);
|
||||
let term = self.field.multiply(*loc as i32, xiInverse);
|
||||
let termPlus1 = if (term & 0x1) == 0 {
|
||||
term | 1
|
||||
} else {
|
||||
@@ -306,6 +303,6 @@ impl ReedSolomonDecoder {
|
||||
result[i] = self.field.multiply(result[i], xiInverse);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,10 +45,7 @@ impl ReedSolomonEncoder {
|
||||
|
||||
fn buildGenerator(&mut self, degree: usize) -> &GenericGFPoly {
|
||||
if degree >= self.cachedGenerators.len() {
|
||||
let mut lastGenerator = self
|
||||
.cachedGenerators
|
||||
.get(self.cachedGenerators.len() - 1)
|
||||
.unwrap();
|
||||
let mut lastGenerator = self.cachedGenerators.last().unwrap();
|
||||
let cg_len = self.cachedGenerators.len();
|
||||
let mut nextGenerator;
|
||||
for d in cg_len..=degree {
|
||||
@@ -71,20 +68,20 @@ impl ReedSolomonEncoder {
|
||||
}
|
||||
}
|
||||
let rv = self.cachedGenerators.get(degree).unwrap();
|
||||
return rv;
|
||||
rv
|
||||
}
|
||||
|
||||
pub fn encode(&mut self, to_encode: &mut Vec<i32>, ec_bytes: usize) -> Result<(), Exceptions> {
|
||||
if ec_bytes == 0 {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"No error correction bytes".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
let data_bytes = to_encode.len() - ec_bytes;
|
||||
if data_bytes == 0 {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"No data bytes provided".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
let fld = self.field;
|
||||
let generator = self.buildGenerator(ec_bytes);
|
||||
@@ -93,7 +90,7 @@ impl ReedSolomonEncoder {
|
||||
//System.arraycopy(toEncode, 0, infoCoefficients, 0, dataBytes);
|
||||
let mut info = GenericGFPoly::new(fld, &info_coefficients)?;
|
||||
info = info.multiply_by_monomial(ec_bytes, 1)?;
|
||||
let remainder = &info.divide(&generator)?.1;
|
||||
let remainder = &info.divide(generator)?.1;
|
||||
let coefficients = remainder.getCoefficients();
|
||||
let num_zero_coefficients = ec_bytes - coefficients.len();
|
||||
for i in 0..num_zero_coefficients {
|
||||
|
||||
@@ -98,14 +98,13 @@ impl StringUtils {
|
||||
* none of these can possibly be correct
|
||||
*/
|
||||
pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> EncodingRef {
|
||||
match hints.get(&DecodeHintType::CHARACTER_SET) {
|
||||
Some(hint) => {
|
||||
if let DecodeHintValue::CharacterSet(cs_name) = hint {
|
||||
return encoding::label::encoding_from_whatwg_label(cs_name).unwrap();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
if let Some(DecodeHintValue::CharacterSet(cs_name)) =
|
||||
hints.get(&DecodeHintType::CHARACTER_SET)
|
||||
{
|
||||
// if let DecodeHintValue::CharacterSet(cs_name) = hint {
|
||||
return encoding::label::encoding_from_whatwg_label(cs_name).unwrap();
|
||||
// }
|
||||
}
|
||||
// if hints.contains_key(&DecodeHintType::CHARACTER_SET) {
|
||||
// return Charset.forName(hints.get(DecodeHintType.CHARACTER_SET).toString());
|
||||
// }
|
||||
@@ -141,7 +140,8 @@ impl StringUtils {
|
||||
|
||||
let utf8bom = bytes.len() > 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF;
|
||||
|
||||
for i in 0..length {
|
||||
// for i in 0..length {
|
||||
for value in bytes.iter().take(length).copied() {
|
||||
// for (int i = 0;
|
||||
// i < length && (canBeISO88591 || canBeShiftJIS || canBeUTF8);
|
||||
// i++) {
|
||||
@@ -149,7 +149,7 @@ impl StringUtils {
|
||||
break;
|
||||
}
|
||||
|
||||
let value = bytes[i];
|
||||
// let value = bytes[i];
|
||||
|
||||
// UTF-8 stuff
|
||||
if can_be_utf8 {
|
||||
@@ -270,6 +270,6 @@ impl StringUtils {
|
||||
return encoding::all::UTF_8;
|
||||
}
|
||||
// Otherwise, we take a wild guess with platform encoding
|
||||
return encoding::all::UTF_8;
|
||||
encoding::all::UTF_8
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user