Skip to content

Commit

Permalink
wip: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimke committed Feb 8, 2024
1 parent 888efeb commit 44d7853
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 208 deletions.
8 changes: 4 additions & 4 deletions src/common/StringUtilsTestCase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use rand::Rng;
use std::collections::HashMap;

use crate::common::StringUtils;
use crate::common::string_utils;

use super::CharacterSet;

Expand All @@ -40,7 +40,7 @@ fn test_random() {
// }
assert_eq!(
CharacterSet::UTF8,
StringUtils::guessCharset(&bytes, &HashMap::new()).unwrap()
string_utils::guessCharset(&bytes, &HashMap::new()).unwrap()
);
}

Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions src/common/adaptive_threshold_binarizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
54 changes: 49 additions & 5 deletions src/common/bit_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
}
}
5 changes: 1 addition & 4 deletions src/common/bit_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/common/cpp_essentials/base_extentions/bitmatrix.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use crate::common::BitMatrix;
use crate::common::Result;
use crate::point_f;
Expand Down
1 change: 0 additions & 1 deletion src/common/cpp_essentials/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::iter::Sum;


use crate::common::Result;
use crate::qrcode::cpp_port::detector::AppendBit;
use crate::{Exceptions, Point};
Expand Down
4 changes: 2 additions & 2 deletions src/common/eci_string_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
Loading

0 comments on commit 44d7853

Please sign in to comment.