swap to struct from tuple

This commit is contained in:
Henry
2023-01-04 19:00:29 -06:00
parent 9a5d6a704b
commit e0ab98d9a5

View File

@@ -1,34 +1,40 @@
use crate::LuminanceSource; use crate::LuminanceSource;
pub struct Luma8LuminanceSource((u32, u32), (u32, u32), Vec<u8>, bool, (u32, u32)); pub struct Luma8LuminanceSource {
dimensions: (u32, u32),
origin: (u32, u32),
data: Vec<u8>,
inverted: bool,
original_dimension: (u32, u32),
}
impl LuminanceSource for Luma8LuminanceSource { impl LuminanceSource for Luma8LuminanceSource {
fn getRow(&self, y: usize) -> Vec<u8> { fn getRow(&self, y: usize) -> Vec<u8> {
self.2 self.data
.chunks_exact(y * self.0 .0 as usize) .chunks_exact(y * self.dimensions.0 as usize)
.skip(y) .skip(y)
.take(1) .take(1)
.flatten() .flatten()
.map(|byte| Self::invert_if_should(*byte, self.3)) .map(|byte| Self::invert_if_should(*byte, self.inverted))
.collect() .collect()
} }
fn getMatrix(&self) -> Vec<u8> { fn getMatrix(&self) -> Vec<u8> {
self.2 self.data
.iter() .iter()
.map(|byte| Self::invert_if_should(*byte, self.3)) .map(|byte| Self::invert_if_should(*byte, self.inverted))
.collect() .collect()
} }
fn getWidth(&self) -> usize { fn getWidth(&self) -> usize {
self.0 .0 as usize self.dimensions.0 as usize
} }
fn getHeight(&self) -> usize { fn getHeight(&self) -> usize {
self.0 .1 as usize self.dimensions.1 as usize
} }
fn invert(&mut self) { fn invert(&mut self) {
self.3 = !self.3; self.inverted = !self.inverted;
} }
fn isCropSupported(&self) -> bool { fn isCropSupported(&self) -> bool {
@@ -42,18 +48,24 @@ impl LuminanceSource for Luma8LuminanceSource {
width: usize, width: usize,
height: usize, height: usize,
) -> Result<Box<dyn LuminanceSource>, crate::Exceptions> { ) -> Result<Box<dyn LuminanceSource>, crate::Exceptions> {
Ok(Box::new(Self( Ok(Box::new(Self {
(width as u32, height as u32), dimensions: (width as u32, height as u32),
(left as u32, top as u32), origin: (left as u32, top as u32),
self.2.clone(), data: self.data.clone(),
self.3, inverted: self.inverted,
self.4, original_dimension: self.original_dimension,
))) }))
} }
} }
impl Luma8LuminanceSource { impl Luma8LuminanceSource {
pub fn new(source: Vec<u8>, width: u32, height: u32) -> Self { pub fn new(source: Vec<u8>, width: u32, height: u32) -> Self {
Self((width, height), (0, 0), source, false, (width, height)) Self {
dimensions: (width, height),
origin: (0, 0),
data: source,
inverted: false,
original_dimension: (width, height),
}
} }
#[inline(always)] #[inline(always)]