From a570dc384279bf9b38ff0cc9d4635c2081aa3147 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 6 Mar 2021 11:21:41 +0530 Subject: add image manip apis --- src/lib.rs | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 7429101..f949d06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![allow(unreachable_patterns)] #![allow(non_snake_case)] +#![allow(dead_code)] use std::io; @@ -24,6 +25,7 @@ impl OBIVersion { } #[non_exhaustive] +#[derive(Debug)] pub struct FileHeader { pub file_size: u32, pub data_offset: u32, @@ -45,6 +47,7 @@ impl FileHeader { } #[non_exhaustive] +#[derive(Debug)] pub struct ImageInfoHeader { pub width: u32, pub height: u32, @@ -82,6 +85,7 @@ impl CompressionType { } } +#[derive(Debug)] pub struct Image { pub file_header: FileHeader, pub image_info_header: ImageInfoHeader, @@ -90,11 +94,15 @@ pub struct Image { impl Image { pub fn new(width: u32, height: u32) -> Self { + Self::new_with(width, height, false) + } + + pub fn new_with(width: u32, height: u32, default_val: bool) -> Self { // round to the nearest multiple of 8 // convert to number of bytes by dividing by 8 let mut data_size = width * height + 7; data_size = data_size - (data_size % 8); - let data = vec![false; data_size as usize]; + let data = vec![default_val; data_size as usize]; Self { file_header: FileHeader::new(OBIVersion::One, data_size / 8), @@ -111,20 +119,32 @@ impl Image { self.image_info_header.height } - fn to_index(&self, x: u32, y: u32) -> usize { - (y * self.width() + x) as usize - } - - pub fn set_pixel(&mut self, x: u32, y: u32, val: bool) -> OBIResult<()> { - if x >= self.width() || y > self.height() { + #[doc(hidden)] + pub fn index(&self, x: u32, y: u32) -> OBIResult { + if x >= self.width() || y >= self.height() { Err(OBIError::Image) } else { - let index = self.to_index(x, y); - self.data[index] = val; - Ok(()) + return Ok((y * self.width() + x) as usize); } } + pub fn set(&mut self, x: u32, y: u32, val: bool) -> OBIResult<()> { + let index = self.index(x, y)?; + self.data[index] = val; + Ok(()) + } + + pub fn flip(&mut self, x: u32, y: u32) -> OBIResult<()> { + let index = self.index(x, y)?; + self.data[index] = !self.data[index]; + Ok(()) + } + + pub fn get(&self, x: u32, y: u32) -> OBIResult { + let index = self.index(x, y)?; + Ok(self.data[index]) + } + pub fn encode(&self) -> OBIResult> { encode::encode_image(self) } -- cgit v1.2.3