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 std::collections::HashMap;
use crate::common::StringUtils;
use crate::common::string_utils;
use super::CharacterSet;
@@ -40,7 +40,7 @@ fn test_random() {
// }
assert_eq!(
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) {
let guessedCharset = StringUtils::guessCharset(bytes, &HashMap::new()).unwrap();
let guessedEncoding = StringUtils::guessEncoding(bytes, &HashMap::new()).unwrap();
let guessedCharset = string_utils::guessCharset(bytes, &HashMap::new()).unwrap();
let guessedEncoding = string_utils::guessEncoding(bytes, &HashMap::new()).unwrap();
assert_eq!(charset, guessedCharset);
assert_eq!(encoding, guessedEncoding);
}

View File

@@ -39,8 +39,7 @@ impl<LS: LuminanceSource> AdaptiveThresholdBinarizer<LS> {
buff
};
let filtered_iamge =
imageproc::contrast::adaptive_threshold(&image_buffer, self.radius);
let filtered_iamge = imageproc::contrast::adaptive_threshold(&image_buffer, self.radius);
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 {
bits: Vec<BaseType>,
size: usize,
read_offset: usize,
}
impl BitArray {
pub fn new() -> Self {
Self {
bits: Vec::new(),
size: 0,
}
// Self {
// bits: Vec::new(),
// size: 0,
// read_offset: 0,
// }
Self::default()
}
pub fn with_size(size: usize) -> Self {
Self {
bits: makeArray(size),
size,
read_offset: 0,
}
}
/// For testing only
#[cfg(test)]
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 {
@@ -429,3 +437,39 @@ impl From<&BitArray> for Vec<bool> {
fn makeArray(size: usize) -> Vec<BaseType> {
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,
height,
row_size: ((width as usize + BASE_BITS - 1) / BASE_BITS),
bits: vec![
0;
((width as usize + BASE_BITS - 1) / BASE_BITS) * height as usize
],
bits: vec![0; ((width as usize + BASE_BITS - 1) / BASE_BITS) * height as usize],
})
// this.width = width;
// this.height = height;

View File

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

View File

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

View File

@@ -26,7 +26,7 @@ use std::{
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
@@ -220,7 +220,7 @@ impl ECIStringBuilder {
}
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) {
encoded_string.push_str(&found_encoded_str);
not_encoded_yet = false;

View File

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

View File

@@ -24,19 +24,6 @@ use super::CharacterSet;
* @author Sean Owen
* @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 SHIFT_JIS_CHARSET: &dyn Encoding =
@@ -48,209 +35,206 @@ const ASSUME_SHIFT_JIS: bool = false;
// static SHIFT_JIS: &'static str = "SJIS";
// 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 =
// SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) ||
// EUC_JP.equals(PLATFORM_DEFAULT_ENCODING);
impl StringUtils {
/**
* @param bytes bytes encoding a string, whose encoding should be guessed
* @param hints decode hints if applicable
* @return name of guessed encoding; at the moment will only guess one of:
* "SJIS", "UTF8", "ISO8859_1", or the platform default encoding if none
* of these can possibly be correct
*/
pub fn guessEncoding(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<&'static str> {
let c = StringUtils::guessCharset(bytes, hints)?;
if c == CharacterSet::Shift_JIS {
Some("SJIS")
} else if c == CharacterSet::UTF8 {
Some("UTF8")
} else if c == CharacterSet::ISO8859_1 {
Some("ISO8859_1")
/**
* @param bytes bytes encoding a string, whose encoding should be guessed
* @param hints decode hints if applicable
* @return name of guessed encoding; at the moment will only guess one of:
* "SJIS", "UTF8", "ISO8859_1", or the platform default encoding if none
* of these can possibly be correct
*/
pub fn guessEncoding(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<&'static str> {
let c = guessCharset(bytes, hints)?;
if c == CharacterSet::Shift_JIS {
Some("SJIS")
} else if c == CharacterSet::UTF8 {
Some("UTF8")
} else if c == CharacterSet::ISO8859_1 {
Some("ISO8859_1")
} else {
Some(c.get_charset_name())
}
}
/**
* @param bytes bytes encoding a string, whose encoding should be guessed
* @param hints decode hints if applicable
* @return Charset of guessed encoding; at the moment will only guess one of:
* {@link #SHIFT_JIS_CHARSET}, {@link StandardCharsets#UTF_8},
* {@link StandardCharsets#ISO_8859_1}, {@link StandardCharsets#UTF_16},
* or the platform default encoding if
* none of these can possibly be correct
*/
pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<CharacterSet> {
if let Some(DecodeHintValue::CharacterSet(cs_name)) = hints.get(&DecodeHintType::CHARACTER_SET)
{
return CharacterSet::get_character_set_by_name(cs_name);
}
// First try UTF-16, assuming anything with its BOM is UTF-16
if bytes.len() > 2 && ((bytes[0..=1] == [0xFE, 0xFF]) || (bytes[0..=1] == [0xFF, 0xFE])) {
if bytes[0..=1] == [0xFE, 0xFF] {
return Some(CharacterSet::UTF16BE);
} else {
Some(c.get_charset_name())
return Some(CharacterSet::UTF16LE);
}
}
/**
* @param bytes bytes encoding a string, whose encoding should be guessed
* @param hints decode hints if applicable
* @return Charset of guessed encoding; at the moment will only guess one of:
* {@link #SHIFT_JIS_CHARSET}, {@link StandardCharsets#UTF_8},
* {@link StandardCharsets#ISO_8859_1}, {@link StandardCharsets#UTF_16},
* or the platform default encoding if
* none of these can possibly be correct
*/
pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<CharacterSet> {
if let Some(DecodeHintValue::CharacterSet(cs_name)) =
hints.get(&DecodeHintType::CHARACTER_SET)
{
return CharacterSet::get_character_set_by_name(cs_name);
// For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS,
// which should be by far the most common encodings.
let length = bytes.len();
let mut can_be_iso88591 = true;
let mut can_be_shift_jis = true;
let mut can_be_utf8 = true;
let mut utf8_bytes_left = 0;
let mut utf2_bytes_chars = 0;
let mut utf3_bytes_chars = 0;
let mut utf4_bytes_chars = 0;
let mut sjis_bytes_left = 0;
let mut sjis_katakana_chars = 0;
let mut sjis_cur_katakana_word_length = 0;
let mut sjis_cur_double_bytes_word_length = 0;
let mut sjis_max_katakana_word_length = 0;
let mut sjis_max_double_bytes_word_length = 0;
let mut iso_high_other = 0;
let utf8bom = bytes.len() > 3 && bytes[0..=2] == [0xEF, 0xBB, 0xBF];
// for i in 0..length {
for value in bytes.iter().take(length).copied() {
// for (int i = 0;
// i < length && (canBeISO88591 || canBeShiftJIS || canBeUTF8);
// i++) {
if !(can_be_iso88591 || can_be_shift_jis || can_be_utf8) {
break;
}
// First try UTF-16, assuming anything with its BOM is UTF-16
// let value = bytes[i];
if bytes.len() > 2 && ((bytes[0..=1] == [0xFE, 0xFF]) || (bytes[0..=1] == [0xFF, 0xFE])) {
if bytes[0..=1] == [0xFE, 0xFF] {
return Some(CharacterSet::UTF16BE);
} else {
return Some(CharacterSet::UTF16LE);
}
}
// For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS,
// which should be by far the most common encodings.
let length = bytes.len();
let mut can_be_iso88591 = true;
let mut can_be_shift_jis = true;
let mut can_be_utf8 = true;
let mut utf8_bytes_left = 0;
let mut utf2_bytes_chars = 0;
let mut utf3_bytes_chars = 0;
let mut utf4_bytes_chars = 0;
let mut sjis_bytes_left = 0;
let mut sjis_katakana_chars = 0;
let mut sjis_cur_katakana_word_length = 0;
let mut sjis_cur_double_bytes_word_length = 0;
let mut sjis_max_katakana_word_length = 0;
let mut sjis_max_double_bytes_word_length = 0;
let mut iso_high_other = 0;
let utf8bom = bytes.len() > 3 && bytes[0..=2] == [0xEF, 0xBB, 0xBF];
// for i in 0..length {
for value in bytes.iter().take(length).copied() {
// for (int i = 0;
// i < length && (canBeISO88591 || canBeShiftJIS || canBeUTF8);
// i++) {
if !(can_be_iso88591 || can_be_shift_jis || can_be_utf8) {
break;
}
// let value = bytes[i];
// UTF-8 stuff
if can_be_utf8 {
if utf8_bytes_left > 0 {
if (value & 0x80) == 0 {
can_be_utf8 = false;
} else {
utf8_bytes_left -= 1;
}
} else if (value & 0x80) != 0 {
if (value & 0x40) == 0 {
can_be_utf8 = false;
// UTF-8 stuff
if can_be_utf8 {
if utf8_bytes_left > 0 {
if (value & 0x80) == 0 {
can_be_utf8 = false;
} else {
utf8_bytes_left -= 1;
}
} else if (value & 0x80) != 0 {
if (value & 0x40) == 0 {
can_be_utf8 = false;
} else {
utf8_bytes_left += 1;
if (value & 0x20) == 0 {
utf2_bytes_chars += 1;
} else {
utf8_bytes_left += 1;
if (value & 0x20) == 0 {
utf2_bytes_chars += 1;
if (value & 0x10) == 0 {
utf3_bytes_chars += 1;
} else {
utf8_bytes_left += 1;
if (value & 0x10) == 0 {
utf3_bytes_chars += 1;
if (value & 0x08) == 0 {
utf4_bytes_chars += 1;
} else {
utf8_bytes_left += 1;
if (value & 0x08) == 0 {
utf4_bytes_chars += 1;
} else {
can_be_utf8 = false;
}
can_be_utf8 = false;
}
}
}
}
}
// ISO-8859-1 stuff
if can_be_iso88591 {
if value > 0x7F && value < 0xA0 {
can_be_iso88591 = false;
} else if value > 0x9F && (value < 0xC0 || value == 0xD7 || value == 0xF7) {
iso_high_other += 1;
}
}
// Shift_JIS stuff
if can_be_shift_jis {
if sjis_bytes_left > 0 {
if value < 0x40 || value == 0x7F || value > 0xFC {
can_be_shift_jis = false;
} else {
sjis_bytes_left -= 1;
}
} else if value == 0x80 || value == 0xA0 || value > 0xEF {
can_be_shift_jis = false;
} else if value > 0xA0 && value < 0xE0 {
sjis_katakana_chars += 1;
sjis_cur_double_bytes_word_length = 0;
sjis_cur_katakana_word_length += 1;
if sjis_cur_katakana_word_length > sjis_max_katakana_word_length {
sjis_max_katakana_word_length = sjis_cur_katakana_word_length;
}
} else if value > 0x7F {
sjis_bytes_left += 1;
//sjisDoubleBytesChars++;
sjis_cur_katakana_word_length = 0;
sjis_cur_double_bytes_word_length += 1;
if sjis_cur_double_bytes_word_length > sjis_max_double_bytes_word_length {
sjis_max_double_bytes_word_length = sjis_cur_double_bytes_word_length;
}
} else {
//sjisLowChars++;
sjis_cur_katakana_word_length = 0;
sjis_cur_double_bytes_word_length = 0;
}
}
}
if can_be_utf8 && utf8_bytes_left > 0 {
can_be_utf8 = false;
}
if can_be_shift_jis && sjis_bytes_left > 0 {
can_be_shift_jis = false;
}
// Easy -- if there is BOM or at least 1 valid not-single byte character (and no evidence it can't be UTF-8), done
if can_be_utf8 && (utf8bom || utf2_bytes_chars + utf3_bytes_chars + utf4_bytes_chars > 0) {
return Some(CharacterSet::UTF8);
}
// Easy -- if assuming Shift_JIS or >= 3 valid consecutive not-ascii characters (and no evidence it can't be), done
if can_be_shift_jis
&& (ASSUME_SHIFT_JIS
|| sjis_max_katakana_word_length >= 3
|| sjis_max_double_bytes_word_length >= 3)
{
return Some(CharacterSet::Shift_JIS); //encoding::label::encoding_from_whatwg_label("SJIS");
}
// Distinguishing Shift_JIS and ISO-8859-1 can be a little tough for short words. The crude heuristic is:
// - If we saw
// - only two consecutive katakana chars in the whole text, or
// - at least 10% of bytes that could be "upper" not-alphanumeric Latin1,
// - then we conclude Shift_JIS, else ISO-8859-1
if can_be_iso88591 && can_be_shift_jis {
return if (sjis_max_katakana_word_length == 2 && sjis_katakana_chars == 2)
|| iso_high_other * 10 >= length
{
Some(CharacterSet::Shift_JIS)
} else {
Some(CharacterSet::ISO8859_1)
};
}
// Otherwise, try in order ISO-8859-1, Shift JIS, UTF-8 and fall back to default platform encoding
// ISO-8859-1 stuff
if can_be_iso88591 {
return Some(CharacterSet::ISO8859_1);
if value > 0x7F && value < 0xA0 {
can_be_iso88591 = false;
} else if value > 0x9F && (value < 0xC0 || value == 0xD7 || value == 0xF7) {
iso_high_other += 1;
}
}
// Shift_JIS stuff
if can_be_shift_jis {
return Some(CharacterSet::Shift_JIS);
if sjis_bytes_left > 0 {
if value < 0x40 || value == 0x7F || value > 0xFC {
can_be_shift_jis = false;
} else {
sjis_bytes_left -= 1;
}
} else if value == 0x80 || value == 0xA0 || value > 0xEF {
can_be_shift_jis = false;
} else if value > 0xA0 && value < 0xE0 {
sjis_katakana_chars += 1;
sjis_cur_double_bytes_word_length = 0;
sjis_cur_katakana_word_length += 1;
if sjis_cur_katakana_word_length > sjis_max_katakana_word_length {
sjis_max_katakana_word_length = sjis_cur_katakana_word_length;
}
} else if value > 0x7F {
sjis_bytes_left += 1;
//sjisDoubleBytesChars++;
sjis_cur_katakana_word_length = 0;
sjis_cur_double_bytes_word_length += 1;
if sjis_cur_double_bytes_word_length > sjis_max_double_bytes_word_length {
sjis_max_double_bytes_word_length = sjis_cur_double_bytes_word_length;
}
} else {
//sjisLowChars++;
sjis_cur_katakana_word_length = 0;
sjis_cur_double_bytes_word_length = 0;
}
}
if can_be_utf8 {
return Some(CharacterSet::UTF8);
}
// Otherwise, we take a wild guess with platform encoding
Some(CharacterSet::UTF8)
}
if can_be_utf8 && utf8_bytes_left > 0 {
can_be_utf8 = false;
}
if can_be_shift_jis && sjis_bytes_left > 0 {
can_be_shift_jis = false;
}
// Easy -- if there is BOM or at least 1 valid not-single byte character (and no evidence it can't be UTF-8), done
if can_be_utf8 && (utf8bom || utf2_bytes_chars + utf3_bytes_chars + utf4_bytes_chars > 0) {
return Some(CharacterSet::UTF8);
}
// Easy -- if assuming Shift_JIS or >= 3 valid consecutive not-ascii characters (and no evidence it can't be), done
if can_be_shift_jis
&& (ASSUME_SHIFT_JIS
|| sjis_max_katakana_word_length >= 3
|| sjis_max_double_bytes_word_length >= 3)
{
return Some(CharacterSet::Shift_JIS); //encoding::label::encoding_from_whatwg_label("SJIS");
}
// Distinguishing Shift_JIS and ISO-8859-1 can be a little tough for short words. The crude heuristic is:
// - If we saw
// - only two consecutive katakana chars in the whole text, or
// - at least 10% of bytes that could be "upper" not-alphanumeric Latin1,
// - then we conclude Shift_JIS, else ISO-8859-1
if can_be_iso88591 && can_be_shift_jis {
return if (sjis_max_katakana_word_length == 2 && sjis_katakana_chars == 2)
|| iso_high_other * 10 >= length
{
Some(CharacterSet::Shift_JIS)
} else {
Some(CharacterSet::ISO8859_1)
};
}
// Otherwise, try in order ISO-8859-1, Shift JIS, UTF-8 and fall back to default platform encoding
if can_be_iso88591 {
return Some(CharacterSet::ISO8859_1);
}
if can_be_shift_jis {
return Some(CharacterSet::Shift_JIS);
}
if can_be_utf8 {
return Some(CharacterSet::UTF8);
}
// Otherwise, we take a wild guess with platform encoding
Some(CharacterSet::UTF8)
}

View File

@@ -17,7 +17,7 @@
use std::collections::{HashMap, HashSet};
use crate::common::Result;
#[cfg(feature="experimental_features")]
#[cfg(feature = "experimental_features")]
use crate::oned::cpp::ODReader;
use crate::qrcode::cpp_port::QrReader;
use crate::{
@@ -196,7 +196,7 @@ impl MultiFormatReader {
BarcodeFormat::MAXICODE => {
MaxiCodeReader::default().decode_with_hints(image, &self.hints)
}
#[cfg(feature="experimental_features")]
#[cfg(feature = "experimental_features")]
BarcodeFormat::DXFilmEdge => {
ODReader::new(&self.hints).decode_with_hints(image, &self.hints)
}
@@ -236,7 +236,7 @@ impl MultiFormatReader {
if let Ok(res) = MaxiCodeReader::default().decode_with_hints(image, &self.hints) {
return Ok(res);
}
#[cfg(feature="experimental_features")]
#[cfg(feature = "experimental_features")]
if let Ok(res) = ODReader::new(&self.hints).decode_with_hints(image, &self.hints) {
return Ok(res);
}

View File

@@ -16,7 +16,7 @@
use crate::{
common::{
BitSource, CharacterSet, DecoderRXingResult, ECIStringBuilder, Eci, Result, StringUtils,
string_utils, BitSource, CharacterSet, DecoderRXingResult, ECIStringBuilder, Eci, Result,
},
DecodingHintDictionary, Exceptions,
};
@@ -305,7 +305,7 @@ fn decodeByteSegment(
// give a hint.
{
#[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")]