wip: cleanup

This commit is contained in:
Henry Schimke
2024-02-08 17:01:05 -06:00
parent 888efeb93f
commit 44d7853099
11 changed files with 236 additions and 215 deletions

View File

@@ -26,7 +26,7 @@
use rand::Rng; use rand::Rng;
use std::collections::HashMap; use std::collections::HashMap;
use crate::common::StringUtils; use crate::common::string_utils;
use super::CharacterSet; use super::CharacterSet;
@@ -40,7 +40,7 @@ fn test_random() {
// } // }
assert_eq!( assert_eq!(
CharacterSet::UTF8, CharacterSet::UTF8,
StringUtils::guessCharset(&bytes, &HashMap::new()).unwrap() string_utils::guessCharset(&bytes, &HashMap::new()).unwrap()
); );
} }
@@ -97,8 +97,8 @@ fn test_utf16_le() {
} }
fn do_test(bytes: &[u8], charset: CharacterSet, encoding: &str) { fn do_test(bytes: &[u8], charset: CharacterSet, encoding: &str) {
let guessedCharset = StringUtils::guessCharset(bytes, &HashMap::new()).unwrap(); let guessedCharset = string_utils::guessCharset(bytes, &HashMap::new()).unwrap();
let guessedEncoding = StringUtils::guessEncoding(bytes, &HashMap::new()).unwrap(); let guessedEncoding = string_utils::guessEncoding(bytes, &HashMap::new()).unwrap();
assert_eq!(charset, guessedCharset); assert_eq!(charset, guessedCharset);
assert_eq!(encoding, guessedEncoding); assert_eq!(encoding, guessedEncoding);
} }

View File

@@ -39,8 +39,7 @@ impl<LS: LuminanceSource> AdaptiveThresholdBinarizer<LS> {
buff buff
}; };
let filtered_iamge = let filtered_iamge = imageproc::contrast::adaptive_threshold(&image_buffer, self.radius);
imageproc::contrast::adaptive_threshold(&image_buffer, self.radius);
let dynamic_filtered = DynamicImage::from(filtered_iamge); let dynamic_filtered = DynamicImage::from(filtered_iamge);

View File

@@ -37,27 +37,35 @@ const BASE_BITS: usize = super::BIT_FIELD_BASE_BITS;
pub struct BitArray { pub struct BitArray {
bits: Vec<BaseType>, bits: Vec<BaseType>,
size: usize, size: usize,
read_offset: usize,
} }
impl BitArray { impl BitArray {
pub fn new() -> Self { pub fn new() -> Self {
Self { // Self {
bits: Vec::new(), // bits: Vec::new(),
size: 0, // size: 0,
} // read_offset: 0,
// }
Self::default()
} }
pub fn with_size(size: usize) -> Self { pub fn with_size(size: usize) -> Self {
Self { Self {
bits: makeArray(size), bits: makeArray(size),
size, size,
read_offset: 0,
} }
} }
/// For testing only /// For testing only
#[cfg(test)] #[cfg(test)]
pub fn with_initial_values(bits: Vec<BaseType>, size: usize) -> Self { pub fn with_initial_values(bits: Vec<BaseType>, size: usize) -> Self {
Self { bits, size } Self {
bits,
size,
read_offset: 0,
}
} }
pub fn get_size(&self) -> usize { pub fn get_size(&self) -> usize {
@@ -429,3 +437,39 @@ impl From<&BitArray> for Vec<bool> {
fn makeArray(size: usize) -> Vec<BaseType> { fn makeArray(size: usize) -> Vec<BaseType> {
vec![0; (size + BASE_BITS - 1) / BASE_BITS] vec![0; (size + BASE_BITS - 1) / BASE_BITS]
} }
impl std::io::Read for BitArray {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let size = self.size;
let desired = buf.len();
let current_offset = self.read_offset;
let available = size - current_offset;
let to_read = if desired <= available {
desired
} else {
available
};
self.toBytes(current_offset, buf, 0, to_read);
self.read_offset = current_offset + to_read;
Ok(to_read)
}
}
impl std::io::Write for BitArray {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
for byte in buf {
self.appendBits(*byte as BaseType, 8)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?
}
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}

View File

@@ -77,10 +77,7 @@ impl BitMatrix {
width, width,
height, height,
row_size: ((width as usize + BASE_BITS - 1) / BASE_BITS), row_size: ((width as usize + BASE_BITS - 1) / BASE_BITS),
bits: vec![ bits: vec![0; ((width as usize + BASE_BITS - 1) / BASE_BITS) * height as usize],
0;
((width as usize + BASE_BITS - 1) / BASE_BITS) * height as usize
],
}) })
// this.width = width; // this.width = width;
// this.height = height; // this.height = height;

View File

@@ -1,4 +1,3 @@
use crate::common::BitMatrix; use crate::common::BitMatrix;
use crate::common::Result; use crate::common::Result;
use crate::point_f; use crate::point_f;

View File

@@ -1,6 +1,5 @@
use std::iter::Sum; use std::iter::Sum;
use crate::common::Result; use crate::common::Result;
use crate::qrcode::cpp_port::detector::AppendBit; use crate::qrcode::cpp_port::detector::AppendBit;
use crate::{Exceptions, Point}; use crate::{Exceptions, Point};

View File

@@ -26,7 +26,7 @@ use std::{
fmt::{self}, fmt::{self},
}; };
use super::{CharacterSet, Eci, StringUtils}; use super::{string_utils, CharacterSet, Eci};
/** /**
* Class that converts a sequence of ECIs and bytes into a string * Class that converts a sequence of ECIs and bytes into a string
@@ -220,7 +220,7 @@ impl ECIStringBuilder {
} }
else */ else */
if let Some(found_encoding) = StringUtils::guessCharset(bytes, &HashMap::default()) { if let Some(found_encoding) = string_utils::guessCharset(bytes, &HashMap::default()) {
if let Ok(found_encoded_str) = found_encoding.decode(bytes) { if let Ok(found_encoded_str) = found_encoding.decode(bytes) {
encoded_string.push_str(&found_encoded_str); encoded_string.push_str(&found_encoded_str);
not_encoded_yet = false; not_encoded_yet = false;

View File

@@ -18,8 +18,7 @@ mod BitSourceTestCase;
#[cfg(test)] #[cfg(test)]
mod PerspectiveTransformTestCase; mod PerspectiveTransformTestCase;
mod string_utils; pub mod string_utils;
pub use string_utils::*;
mod bit_array; mod bit_array;
pub use bit_array::*; pub use bit_array::*;

View File

@@ -24,19 +24,6 @@ use super::CharacterSet;
* @author Sean Owen * @author Sean Owen
* @author Alex Dupre * @author Alex Dupre
*/ */
pub struct StringUtils {
// private static final Charset PLATFORM_DEFAULT_ENCODING = Charset.defaultCharset();
// public static final Charset SHIFT_JIS_CHARSET = Charset.forName("SJIS");
// public static final Charset GB2312_CHARSET = Charset.forName("GB2312");
// private static final Charset EUC_JP = Charset.forName("EUC_JP");
// private static final boolean ASSUME_SHIFT_JIS =
// SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) ||
// EUC_JP.equals(PLATFORM_DEFAULT_ENCODING);
// // Retained for ABI compatibility with earlier versions
// public static final String SHIFT_JIS = "SJIS";
// public static final String GB2312 = "GB2312";
}
// const PLATFORM_DEFAULT_ENCODING: &dyn Encoding = encoding::all::UTF_8; // const PLATFORM_DEFAULT_ENCODING: &dyn Encoding = encoding::all::UTF_8;
// const SHIFT_JIS_CHARSET: &dyn Encoding = // const SHIFT_JIS_CHARSET: &dyn Encoding =
@@ -48,13 +35,12 @@ const ASSUME_SHIFT_JIS: bool = false;
// static SHIFT_JIS: &'static str = "SJIS"; // static SHIFT_JIS: &'static str = "SJIS";
// static GB2312: &'static str = "GB2312"; // static GB2312: &'static str = "GB2312";
pub static SHIFT_JIS_CHARSET: CharacterSet = CharacterSet::Shift_JIS; pub const SHIFT_JIS_CHARSET: CharacterSet = CharacterSet::Shift_JIS;
// private static final boolean ASSUME_SHIFT_JIS = // private static final boolean ASSUME_SHIFT_JIS =
// SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) || // SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) ||
// EUC_JP.equals(PLATFORM_DEFAULT_ENCODING); // EUC_JP.equals(PLATFORM_DEFAULT_ENCODING);
impl StringUtils {
/** /**
* @param bytes bytes encoding a string, whose encoding should be guessed * @param bytes bytes encoding a string, whose encoding should be guessed
* @param hints decode hints if applicable * @param hints decode hints if applicable
@@ -63,7 +49,7 @@ impl StringUtils {
* of these can possibly be correct * of these can possibly be correct
*/ */
pub fn guessEncoding(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<&'static str> { pub fn guessEncoding(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<&'static str> {
let c = StringUtils::guessCharset(bytes, hints)?; let c = guessCharset(bytes, hints)?;
if c == CharacterSet::Shift_JIS { if c == CharacterSet::Shift_JIS {
Some("SJIS") Some("SJIS")
} else if c == CharacterSet::UTF8 { } else if c == CharacterSet::UTF8 {
@@ -85,8 +71,7 @@ impl StringUtils {
* none of these can possibly be correct * none of these can possibly be correct
*/ */
pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<CharacterSet> { pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<CharacterSet> {
if let Some(DecodeHintValue::CharacterSet(cs_name)) = if let Some(DecodeHintValue::CharacterSet(cs_name)) = hints.get(&DecodeHintType::CHARACTER_SET)
hints.get(&DecodeHintType::CHARACTER_SET)
{ {
return CharacterSet::get_character_set_by_name(cs_name); return CharacterSet::get_character_set_by_name(cs_name);
} }
@@ -253,4 +238,3 @@ impl StringUtils {
// Otherwise, we take a wild guess with platform encoding // Otherwise, we take a wild guess with platform encoding
Some(CharacterSet::UTF8) Some(CharacterSet::UTF8)
} }
}

View File

@@ -16,7 +16,7 @@
use crate::{ use crate::{
common::{ common::{
BitSource, CharacterSet, DecoderRXingResult, ECIStringBuilder, Eci, Result, StringUtils, string_utils, BitSource, CharacterSet, DecoderRXingResult, ECIStringBuilder, Eci, Result,
}, },
DecodingHintDictionary, Exceptions, DecodingHintDictionary, Exceptions,
}; };
@@ -305,7 +305,7 @@ fn decodeByteSegment(
// give a hint. // give a hint.
{ {
#[cfg(not(feature = "allow_forced_iso_ied_18004_compliance"))] #[cfg(not(feature = "allow_forced_iso_ied_18004_compliance"))]
StringUtils::guessCharset(&readBytes, hints).ok_or(Exceptions::ILLEGAL_STATE)? string_utils::guessCharset(&readBytes, hints).ok_or(Exceptions::ILLEGAL_STATE)?
} }
#[cfg(feature = "allow_forced_iso_ied_18004_compliance")] #[cfg(feature = "allow_forced_iso_ied_18004_compliance")]