wip: incomplete port checking

This commit is contained in:
Henry Schimke
2024-01-26 11:11:50 -06:00
parent dabb8a9d08
commit d222a98aad
13 changed files with 236 additions and 99 deletions

View File

@@ -18,6 +18,7 @@
// import java.util.Arrays;
use std::ops::Index;
use std::{cmp, fmt};
use crate::common::Result;
@@ -420,6 +421,19 @@ impl From<BitArray> for Vec<u8> {
impl From<BitArray> for Vec<bool> {
fn from(value: BitArray) -> Self {
// let mut array = vec![false; value.size];
// for (pixel, element) in array.iter_mut().enumerate().take(value.size) {
// *element = value.get(pixel);
// }
// array
Self::from(&value)
}
}
impl From<&BitArray> for Vec<bool> {
fn from(value: &BitArray) -> Self {
let mut array = vec![false; value.size];
for (pixel, element) in array.iter_mut().enumerate().take(value.size) {
@@ -429,3 +443,11 @@ impl From<BitArray> for Vec<bool> {
array
}
}
impl Index<usize> for BitArray {
type Output = bool;
fn index(&self, index: usize) -> &Self::Output {
&self.get(index)
}
}

View File

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

View File

@@ -51,6 +51,10 @@ impl PatternRow {
pub fn sum(&self) -> PatternType {
self.0.iter().sum()
}
pub fn rev(&mut self) {
self.0.reverse()
}
}
impl IntoIterator for PatternRow {

View File

@@ -51,6 +51,7 @@ pub struct GlobalHistogramBinarizer<LS: LuminanceSource> {
source: LS,
black_matrix: OnceCell<BitMatrix>,
black_row_cache: Vec<OnceCell<BitArray>>,
black_column_cache: Vec<OnceCell<BitArray>>,
}
impl<LS: LuminanceSource> Binarizer for GlobalHistogramBinarizer<LS> {
@@ -106,6 +107,10 @@ impl<LS: LuminanceSource> Binarizer for GlobalHistogramBinarizer<LS> {
Ok(Cow::Borrowed(row))
}
fn get_black_line(&self, l: usize, lt: super::LineOrientation) -> Result<Cow<BitArray>> {
unimplemented!()
}
// Does not sharpen the data, as this call is intended to only be used by 2D Readers.
fn get_black_matrix(&self) -> Result<&BitMatrix> {
let matrix = self
@@ -137,6 +142,7 @@ impl<LS: LuminanceSource> GlobalHistogramBinarizer<LS> {
height: source.get_height(),
black_matrix: OnceCell::new(),
black_row_cache: vec![OnceCell::default(); source.get_height()],
black_column_cache: vec![OnceCell::default(); source.get_width()],
source,
}
}

View File

@@ -64,6 +64,10 @@ impl<LS: LuminanceSource> Binarizer for HybridBinarizer<LS> {
self.ghb.get_black_row(y)
}
fn get_black_line(&self, l: usize, lt: super::LineOrientation) -> Result<Cow<BitArray>> {
self.ghb.get_black_line(l, lt)
}
/**
* Calculates the final BitMatrix once for all requests. This could be called once from the
* constructor instead, but there are some advantages to doing it lazily, such as making

View File

@@ -0,0 +1,5 @@
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LineOrientation {
Row,
Column,
}

View File

@@ -114,6 +114,9 @@ pub use quad::*;
pub mod cpp_essentials;
mod line_orientation;
pub use line_orientation::LineOrientation;
#[cfg(feature = "otsu_level")]
mod otsu_level_binarizer;
#[cfg(feature = "otsu_level")]

View File

@@ -1,4 +1,4 @@
use crate::{point_f, Point};
use crate::{point_f, Exceptions, Point};
#[derive(Clone, Copy, Debug)]
pub struct Quadrilateral(pub [Point; 4]);
@@ -272,3 +272,19 @@ impl From<[Point; 4]> for Quadrilateral {
Self(value)
}
}
impl TryFrom<&Vec<Point>> for Quadrilateral {
type Error = Exceptions;
fn try_from(value: &Vec<Point>) -> Result<Self, Self::Error> {
if value.len() == 4 {
Ok(
Self(
[value[0], value[1], value[2], value[3]]
)
)
}else{
Err(Exceptions::INDEX_OUT_OF_BOUNDS)
}
}
}