Skip to content

Commit

Permalink
small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarriba committed Jan 31, 2024
1 parent 1cd281e commit d8eea97
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
13 changes: 9 additions & 4 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ use ndarray::{Array3, Zip};
// or automatically:
// let gray = Image<u8, 1>::try_from(rgb);
pub fn gray_from_rgb(image: &Image) -> Image {
assert_eq!(image.num_channels(), 3);

// TODO: implement this using a map or cast
// let image_f32 = image.cast::<f32>();
let mut output = Array3::<u8>::zeros(image.data.dim());
//let mut output = Array3::<u8>::zeros(image.data.dim());
let mut output = Array3::<u8>::zeros((
image.image_size().height as usize,

Check warning on line 16 in src/color.rs

View workflow job for this annotation

GitHub Actions / Clippy Output

casting to the same type is unnecessary (`usize` -> `usize`)

warning: casting to the same type is unnecessary (`usize` -> `usize`) --> src/color.rs:16:9 | 16 | image.image_size().height as usize, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `image.image_size().height` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast = note: `#[warn(clippy::unnecessary_cast)]` on by default
image.image_size().width as usize,

Check warning on line 17 in src/color.rs

View workflow job for this annotation

GitHub Actions / Clippy Output

casting to the same type is unnecessary (`usize` -> `usize`)

warning: casting to the same type is unnecessary (`usize` -> `usize`) --> src/color.rs:17:9 | 17 | image.image_size().width as usize, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `image.image_size().width` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
1,
));

Zip::from(output.rows_mut())
.and(image.data.rows())
Expand All @@ -21,8 +28,6 @@ pub fn gray_from_rgb(image: &Image) -> Image {
let gray = (76. * r + 150. * g + 29. * b) / 255.;

out[0] = gray as u8;
out[1] = gray as u8;
out[2] = gray as u8;
});

Image { data: output }
Expand All @@ -37,7 +42,7 @@ mod tests {
let image_path = std::path::Path::new("tests/data/dog.jpeg");
let image = F::read_image_jpeg(image_path);
let gray = super::gray_from_rgb(&image);
assert_eq!(gray.num_channels(), 3);
assert_eq!(gray.num_channels(), 1);
assert_eq!(gray.image_size().width, 258);
assert_eq!(gray.image_size().height, 195);
}
Expand Down
13 changes: 13 additions & 0 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl<T> Image<T> {
Image { data: image }
}

// TODO: implement without `num_traits`
pub fn cast<U>(&self) -> Image<U>
where
T: num_traits::cast::AsPrimitive<U>,
Expand Down Expand Up @@ -96,6 +97,9 @@ impl<T> Image<T> {
_ => io::functions::read_image_any(image_path),
}
}

// TODO: implement from bytes
// pub fn from_bytes(bytes: &[u8]) -> Image {
}

#[cfg(test)]
Expand Down Expand Up @@ -158,4 +162,13 @@ mod tests {
let image_i32: Image<i32> = image_u8.cast();
assert_eq!(image_i32.data.get((1, 0, 2)).unwrap(), &5i32);
}

#[test]
fn image_rgbd() {
use crate::image::Image;
let image = Image::from_shape_vec([2, 2, 4], vec![0f32; 2 * 2 * 4]);
assert_eq!(image.image_size().width, 2);
assert_eq!(image.image_size().height, 2);
assert_eq!(image.num_channels(), 4);
}
}
41 changes: 30 additions & 11 deletions src/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn resize(image: &Image, new_size: ImageSize, optional_args: ResizeOptions)
let image_size = image.image_size();

// create the output image
let mut output = Array3::<u8>::zeros((new_size.height, new_size.width, 3));
let mut output = Array3::<u8>::zeros((new_size.height, new_size.width, image.num_channels()));

// create a grid of x and y coordinates for the output image
// and interpolate the values from the input image.
Expand Down Expand Up @@ -120,15 +120,17 @@ pub fn resize(image: &Image, new_size: ImageSize, optional_args: ResizeOptions)
assert_eq!(uv.len(), 2);
let (u, v) = (uv[0], uv[1]);

// TODO: this assumes 3 channels
for k in [0, 1, 2].iter() {
let pixel = match optional_args.interpolation {
InterpolationMode::Bilinear => bilinear_interpolation(&image.data, u, v, *k),
InterpolationMode::NearestNeighbor => {
nearest_neighbor_interpolation(&image.data, u, v, *k)
}
};
out[*k] = pixel as u8;
// compute the pixel values for each channel
let pixels = (0..image.num_channels()).map(|k| match optional_args.interpolation {
InterpolationMode::Bilinear => bilinear_interpolation(&image.data, u, v, k),
InterpolationMode::NearestNeighbor => {
nearest_neighbor_interpolation(&image.data, u, v, k)
}
});

// write the pixel values to the output image
for (k, pixel) in pixels.enumerate() {
out[k] = pixel as u8;
}
});

Expand All @@ -139,7 +141,7 @@ pub fn resize(image: &Image, new_size: ImageSize, optional_args: ResizeOptions)
mod tests {

#[test]
fn resize_smoke() {
fn resize_smoke_ch3() {
use crate::image::{Image, ImageSize};
let image = Image::from_shape_vec([4, 5, 3], vec![0; 4 * 5 * 3]);
let image_resized = super::resize(
Expand All @@ -154,4 +156,21 @@ mod tests {
assert_eq!(image_resized.image_size().width, 2);
assert_eq!(image_resized.image_size().height, 3);
}

#[test]
fn resize_smoke_ch1() {
use crate::image::{Image, ImageSize};
let image = Image::from_shape_vec([4, 5, 1], vec![0; 4 * 5 * 1]);
let image_resized = super::resize(
&image,
ImageSize {
width: 2,
height: 3,
},
super::ResizeOptions::default(),
);
assert_eq!(image_resized.num_channels(), 1);
assert_eq!(image_resized.image_size().width, 2);
assert_eq!(image_resized.image_size().height, 3);
}
}

0 comments on commit d8eea97

Please sign in to comment.