Skip to content

Commit

Permalink
rename from_size
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarriba committed Feb 15, 2024
1 parent 4d255ea commit 106fe84
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
let gw = T::from(0.587).unwrap();
let bw = T::from(0.114).unwrap();

let mut output = Image::<T, 1>::from_shape(image.image_size())?;
let mut output = Image::<T, 1>::from_size(image.image_size())?;

ndarray::Zip::from(output.data.rows_mut())
.and(image.data.rows())
Expand Down
43 changes: 7 additions & 36 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,49 +45,30 @@ pub struct Image<T, const CHANNELS: usize> {

// provisionally, we will use the following types:
impl<T, const CHANNELS: usize> Image<T, CHANNELS> {
pub fn new(shape: ImageSize, data: Vec<T>) -> Result<Self> {
pub fn new(size: ImageSize, data: Vec<T>) -> Result<Self> {
// check if the data length matches the image size
if data.len() != shape.width * shape.height * CHANNELS {
if data.len() != size.width * size.height * CHANNELS {
return Err(anyhow::anyhow!(
"Data length ({}) does not match the image size ({})",
data.len(),
shape.width * shape.height * CHANNELS
size.width * size.height * CHANNELS
));
//return Err(Error::new(
// std::io::ErrorKind::InvalidData,
// format!(
// "Data length ({}) does not match the image size ({})",
// data.len(),
// shape.width * shape.height * CHANNELS
// ),
//));
}

// allocate the image data
let data =
ndarray::Array::<T, _>::from_shape_vec((shape.height, shape.width, CHANNELS), data)
ndarray::Array::<T, _>::from_shape_vec((size.height, size.width, CHANNELS), data)
.expect("Failed to create image data");

Ok(Image { data })
}

pub fn from_shape(shape: ImageSize) -> Result<Self>
pub fn from_size(size: ImageSize) -> Result<Self>
where
T: Clone + Default,
{
let data = vec![T::default(); shape.width * shape.height * CHANNELS];
let image = Image::new(shape, data)?;

Ok(image)
}

pub fn zeros_like(&self) -> Result<Self>
where
T: Clone + Default,
{
let shape = self.image_size();
let data = vec![T::default(); shape.width * shape.height * CHANNELS];
let image = Image::new(shape, data)?;
let data = vec![T::default(); size.width * size.height * CHANNELS];
let image = Image::new(size, data)?;

Ok(image)
}
Expand Down Expand Up @@ -159,16 +140,6 @@ impl<T, const CHANNELS: usize> Image<T, CHANNELS> {
CHANNELS
}

//pub fn from_shape_vec(shape: [usize; 2], data: Vec<T>) -> Image<T> {
// let image = match ndarray::Array::<T, _>::from_shape_vec(shape, data) {
// Ok(image) => image,
// Err(err) => {
// panic!("Error converting image: {}", err);
// }
// };
// Image { data: image }
//}

//pub fn from_file(image_path: &Path) -> Image<u8, 3> {
// match image_path.extension().and_then(|ext| ext.to_str()) {
// Some("jpeg") | Some("jpg") => io::functions::read_image_jpeg(image_path),
Expand Down
2 changes: 1 addition & 1 deletion src/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn resize<const CHANNELS: usize>(
optional_args: ResizeOptions,
) -> Result<Image<f32, CHANNELS>> {
// create the output image
let mut output = Image::from_shape(new_size.clone())?;
let mut output = Image::from_size(new_size.clone())?;

// create a grid of x and y coordinates for the output image
// and interpolate the values from the input image.
Expand Down
2 changes: 1 addition & 1 deletion src/threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn threshold_binary<T, const CHANNELS: usize>(
where
T: Copy + Clone + Default + Send + Sync + num_traits::NumCast + std::fmt::Debug + PartialOrd,
{
let mut output = Image::<T, CHANNELS>::from_shape(image.image_size())?;
let mut output = Image::<T, CHANNELS>::from_size(image.image_size())?;

ndarray::Zip::from(&mut output.data)
.and(&image.data)
Expand Down

0 comments on commit 106fe84

Please sign in to comment.