Skip to content

Commit

Permalink
Apply clippy fix (#61)
Browse files Browse the repository at this point in the history
* Apply clippy fix

* Apply more fixes and remove warnings
  • Loading branch information
xd009642 authored Jul 12, 2023
1 parent bf7c834 commit 6ac5e26
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 72 deletions.
6 changes: 3 additions & 3 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use ndarray_vision::core::*;
use ndarray_vision::format::netpbm::*;
use ndarray_vision::format::*;
use ndarray_vision::processing::*;
use std::env::current_exe;

use std::path::{Path, PathBuf};

fn main() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let mut cameraman = root.clone().join("images/cameraman.ppm");
let cameraman = root.clone().join("images/cameraman.ppm");
println!("{:?}", cameraman);

let decoder = PpmDecoder::default();
Expand All @@ -21,7 +21,7 @@ fn main() {

let mut image: Image<f64, _> = image.into_type();

let _ = image
image
.conv2d_inplace(boxkern.view())
.expect("Poorly sized kernel");
// There's no u8: From<f64> so I've done this to hack things
Expand Down
2 changes: 1 addition & 1 deletion examples/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main() {
// save
let path = Path::new("transformed_cameraman.png");
let file = File::create(path).expect("Couldn't create output file");
let ref mut w = BufWriter::new(file);
let w = &mut BufWriter::new(file);

let mut encoder = png::Encoder::new(w, transformed.cols() as u32, transformed.rows() as u32);
encoder.set_color(png::ColorType::RGB);
Expand Down
16 changes: 8 additions & 8 deletions src/core/colour_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ fn rescale_pixel<T>(x: f64) -> T
where
T: FromPrimitive + Num + NumCast + PixelBound + Display,
{
let tmax = T::max_pixel().to_f64().unwrap_or_else(|| 0.0f64);
let tmin = T::min_pixel().to_f64().unwrap_or_else(|| 0.0f64);
let tmax = T::max_pixel().to_f64().unwrap_or(0.0f64);
let tmin = T::min_pixel().to_f64().unwrap_or(0.0f64);

let x = x * (tmax - tmin) + tmin;

Expand Down Expand Up @@ -159,17 +159,17 @@ where
let x = c * (1.0f64 - ((h_deg / 60.0f64) % 2.0f64 - 1.0f64).abs());
let m = v_norm - c;

let rgb = if 0.0f64 <= h_deg && h_deg < 60.0f64 {
let rgb = if (0.0f64..60.0f64).contains(&h_deg) {
(c, x, 0.0f64)
} else if 60.0f64 <= h_deg && h_deg < 120.0f64 {
} else if (60.0f64..120.0f64).contains(&h_deg) {
(x, c, 0.0f64)
} else if 120.0f64 <= h_deg && h_deg < 180.0f64 {
} else if (120.0f64..180.0f64).contains(&h_deg) {
(0.0f64, c, x)
} else if 180.0f64 <= h_deg && h_deg < 240.0f64 {
} else if (180.0f64..240.0f64).contains(&h_deg) {
(0.0f64, x, c)
} else if 240.0f64 <= h_deg && h_deg < 300.0f64 {
} else if (240.0f64..300.0f64).contains(&h_deg) {
(x, 0.0f64, c)
} else if 300.0f64 <= h_deg && h_deg < 360.0f64 {
} else if (300.0f64..360.0f64).contains(&h_deg) {
(c, 0.0f64, x)
} else {
(0.0f64, 0.0f64, 0.0f64)
Expand Down
6 changes: 3 additions & 3 deletions src/core/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ where
let scaled = normalise_pixel_value(*x)
* (T2::max_pixel() - T2::min_pixel())
.to_f64()
.unwrap_or_else(|| 0.0f64);
.unwrap_or(0.0f64);
T2::from_f64(scaled).unwrap_or_else(T2::zero) + T2::min_pixel()
};
let data = self.data.map(rescale);
Expand Down Expand Up @@ -204,8 +204,8 @@ where
let numerator = (t + T::min_pixel()).to_f64();
let denominator = (T::max_pixel() - T::min_pixel()).to_f64();

let numerator = numerator.unwrap_or_else(|| 0.0f64);
let denominator = denominator.unwrap_or_else(|| 1.0f64);
let numerator = numerator.unwrap_or(0.0f64);
let denominator = denominator.unwrap_or(1.0f64);

numerator / denominator
}
Expand Down
2 changes: 1 addition & 1 deletion src/format/netpbm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl PpmEncoder {
U: Data<Elem = T>,
T: Copy + Clone + Num + NumAssignOps + NumCast + PartialOrd + Display + PixelBound,
{
let max_val = Self::get_max_value(image).unwrap_or_else(|| 255);
let max_val = Self::get_max_value(image).unwrap_or(255);

let mut result = self
.generate_header(image.rows(), image.cols(), max_val)
Expand Down
15 changes: 11 additions & 4 deletions src/processing/canny.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ where
let mut dir = rotations[[i as usize, j, 0]]
.to_degrees()
.to_f64()
.unwrap_or_else(|| 0.0);
.unwrap_or(0.0);

let j = j as isize;
if dir >= 180.0 {
Expand Down Expand Up @@ -197,6 +197,15 @@ where
result
}

impl<T> Default for CannyBuilder<T>
where
T: Copy + Clone + FromPrimitive + Real + Num,
{
fn default() -> Self {
Self::new()
}
}

impl<T> CannyBuilder<T>
where
T: Copy + Clone + FromPrimitive + Real + Num,
Expand Down Expand Up @@ -267,9 +276,7 @@ where
None => T::from_f64(0.7).unwrap(),
};
if t2 < t1 {
let temp = t1;
t1 = t2;
t2 = temp;
std::mem::swap(&mut t1, &mut t2);
}
CannyParameters { blur, t1, t2 }
}
Expand Down
4 changes: 2 additions & 2 deletions src/processing/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ mod tests {
}

#[test]
#[cfg_attr(rustfmt, rustfmt_skip)]
#[rustfmt::skip]
fn basic_conv() {
let input_pixels = vec![
1, 1, 1, 0, 0,
Expand Down Expand Up @@ -300,7 +300,7 @@ mod tests {
}

#[test]
#[cfg_attr(rustfmt, rustfmt_skip)]
#[rustfmt::skip]
fn basic_conv_inplace() {
let input_pixels = vec![
1, 1, 1, 0, 0,
Expand Down
6 changes: 3 additions & 3 deletions src/processing/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ where
Zip::indexed(self.windows(region)).for_each(|(i, j, k), window| {
let mut flat_window = Array::from_iter(window.iter()).mapv(|x| *x);
if let Ok(v) = flat_window.quantile_mut(n64(0.5f64), &Linear {}) {
result
.get_mut([i + r_offset, j + c_offset, k])
.map(|r| *r = v);
if let Some(r) = result.get_mut([i + r_offset, j + c_offset, k]) {
*r = v;
}
}
});
result
Expand Down
6 changes: 3 additions & 3 deletions src/processing/kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ where
let res = match p {
LaplaceType::Standard => {
let m_1 = -T::one();
let p_4 = T::from_u8(4).ok_or_else(|| Error::NumericError)?;
let p_4 = T::from_u8(4).ok_or(Error::NumericError)?;
let z = T::zero();

arr2(&[[z, m_1, z], [m_1, p_4, m_1], [z, m_1, z]])
}
LaplaceType::Diagonal => {
let m_1 = -T::one();
let p_8 = T::from_u8(8).ok_or_else(|| Error::NumericError)?;
let p_8 = T::from_u8(8).ok_or(Error::NumericError)?;

arr2(&[[m_1, m_1, m_1], [m_1, p_8, m_1], [m_1, m_1, m_1]])
}
Expand Down Expand Up @@ -219,7 +219,7 @@ where

/// Build a fixed size kernel with the given parameters
fn build_with_params(p: Self::Params) -> Result<Array3<T>, Error> {
let two = T::from_i8(2).ok_or_else(|| Error::NumericError)?;
let two = T::from_i8(2).ok_or(Error::NumericError)?;
// Gets the gradient along the horizontal axis
#[rustfmt::skip]
let horz_sobel = arr2(&[
Expand Down
6 changes: 3 additions & 3 deletions src/processing/threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ where
sum_intensity += (index as f64) * (*count).to_f64().unwrap();
}
for (index, count) in counts.indexed_iter() {
weight_b = weight_b + count.to_f64().unwrap();
sum_b = sum_b + (index as f64) * count.to_f64().unwrap();
weight_b += count.to_f64().unwrap();
sum_b += (index as f64) * count.to_f64().unwrap();
let weight_f = total - weight_b;
if (weight_b > 0.0) && (weight_f > 0.0) {
let mean_f = (sum_intensity - sum_b) / weight_f;
Expand All @@ -162,7 +162,7 @@ where
}
}
}
threshold = level as f64 / scale_factor;
threshold = level / scale_factor;
}
Ok(threshold)
}
Expand Down
13 changes: 6 additions & 7 deletions src/transform/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ use ndarray_linalg::Inverse;

/// converts a matrix into an equivalent `AffineTransform`
pub fn transform_from_2dmatrix(in_array: Array2<f64>) -> AffineTransform {
let transform = match in_array.inv() {
match in_array.inv() {
Ok(inv) => AffineTransform {
matrix2d_transform: in_array.clone(),
matrix2d_transform: in_array,
matrix2d_transform_inverse: inv,
inverse_exists: true,
},
Err(e) => AffineTransform {
matrix2d_transform: in_array.clone(),
Err(_e) => AffineTransform {
matrix2d_transform: in_array,
matrix2d_transform_inverse: Array2::zeros((2, 2)),
inverse_exists: false,
},
};
return transform;
}
}

/// a linear transform of an image represented by either size 2x2
Expand Down Expand Up @@ -60,7 +59,7 @@ impl Transform for AffineTransform {
}

fn inverse_exists(&self) -> bool {
return self.inverse_exists;
self.inverse_exists
}
}

Expand Down
41 changes: 7 additions & 34 deletions src/transform/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::core::{ColourModel, Image, ImageBase};
use ndarray::{array, prelude::*, s, Data};
use ndarray_linalg::*;
use ndarray::{prelude::*, s, Data};
use num_traits::{Num, NumAssignOps};
use std::cmp::{max, min};
use std::fmt::Display;

pub mod affine;
Expand All @@ -18,9 +16,9 @@ impl std::error::Error for TransformError {}
impl Display for TransformError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TransformError::InvalidTransform => return write!(f, "invalid transform"),
TransformError::InvalidTransform => write!(f, "invalid transform"),
TransformError::NonInvertibleTransform => {
return write!(
write!(
f,
"Non Invertible Transform, Forward transform not yet implemented "
)
Expand All @@ -46,17 +44,16 @@ pub struct ComposedTransform<T: Transform> {

impl<T: Transform> Transform for ComposedTransform<T> {
fn apply(&self, p: (f64, f64)) -> (f64, f64) {
return self.transform2.apply(self.transform1.apply(p));
self.transform2.apply(self.transform1.apply(p))
}

fn apply_inverse(&self, p: (f64, f64)) -> (f64, f64) {
return self
.transform1
.apply_inverse(self.transform2.apply_inverse(p));
self.transform1
.apply_inverse(self.transform2.apply_inverse(p))
}

fn inverse_exists(&self) -> bool {
return self.transform1.inverse_exists() && self.transform2.inverse_exists();
self.transform1.inverse_exists() && self.transform2.inverse_exists()
}
}

Expand Down Expand Up @@ -85,30 +82,6 @@ struct Rect {
h: usize,
}

fn bounding_box<T: Transform>(dims: (f64, f64), transform: T) -> Rect {
let tl = transform.apply((0.0, 0.0));
let tr = transform.apply((0.0, dims.1));
let br = transform.apply(dims);
let bl = transform.apply((dims.0, 0.0));

let tl = (tl.0.round() as isize, tl.1.round() as isize);
let tr = (tr.0.round() as isize, tr.1.round() as isize);
let br = (br.0.round() as isize, br.1.round() as isize);
let bl = (bl.0.round() as isize, bl.1.round() as isize);

let leftmost = min(min(tl.0, tr.0), min(br.0, bl.0));
let topmost = min(min(tl.1, tr.1), min(br.1, bl.1));
let rightmost = max(max(tl.0, tr.0), max(br.0, bl.0));
let bottommost = max(max(tl.1, tr.1), max(br.1, bl.1));

Rect {
x: leftmost,
y: topmost,
w: (rightmost - leftmost) as usize,
h: (bottommost - topmost) as usize,
}
}

impl<T, U, V> TransformExt<V> for ArrayBase<U, Ix3>
where
T: Copy + Clone + Num + NumAssignOps,
Expand Down

0 comments on commit 6ac5e26

Please sign in to comment.