Skip to content

Commit 5396d7d

Browse files
committed
Added zero padding
1 parent 766346c commit 5396d7d

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/core/padding.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::core::{ColourModel, Image};
22
use ndarray::{prelude::*, s};
3+
use num_traits::identities::Zero;
34
use std::marker::PhantomData;
45

56
/// Defines a method for padding the data of an image applied directly to the
@@ -24,9 +25,13 @@ pub struct ConstantPadding<T>(T)
2425
where
2526
T: Copy;
2627

28+
/// Pad the image with zeros. Uses ConstantPadding internally
29+
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
30+
pub struct ZeroPadding;
31+
2732
impl<T> PaddingStrategy<T> for NoPadding
2833
where
29-
T: Copy + Sized,
34+
T: Copy,
3035
{
3136
fn pad(&self, image: ArrayView3<T>, _padding: (usize, usize)) -> Array3<T> {
3237
image.to_owned()
@@ -35,7 +40,7 @@ where
3540

3641
impl<T> PaddingStrategy<T> for ConstantPadding<T>
3742
where
38-
T: Copy + Sized,
43+
T: Copy,
3944
{
4045
fn pad(&self, image: ArrayView3<T>, padding: (usize, usize)) -> Array3<T> {
4146
let shape = (
@@ -57,11 +62,19 @@ where
5762
}
5863
}
5964

60-
/// Padding extension for images
61-
pub trait PaddingExt
65+
66+
impl<T> PaddingStrategy<T> for ZeroPadding
6267
where
63-
Self: Sized,
68+
T: Copy + Zero,
6469
{
70+
fn pad(&self, image: ArrayView3<T>, padding: (usize, usize)) -> Array3<T> {
71+
let padder = ConstantPadding(T::zero());
72+
padder.pad(image, padding)
73+
}
74+
}
75+
76+
/// Padding extension for images
77+
pub trait PaddingExt {
6578
/// Data type for container
6679
type Data;
6780
/// Pad the object with the given padding and strategy
@@ -70,7 +83,7 @@ where
7083

7184
impl<T> PaddingExt for Array3<T>
7285
where
73-
T: Copy + Sized,
86+
T: Copy,
7487
{
7588
type Data = T;
7689

@@ -81,7 +94,7 @@ where
8194

8295
impl<T, C> PaddingExt for Image<T, C>
8396
where
84-
T: Copy + Sized,
97+
T: Copy,
8598
C: ColourModel,
8699
{
87100
type Data = T;
@@ -126,6 +139,8 @@ mod tests {
126139
assert_eq!(p, exp);
127140

128141
let p = i.pad((2, 0), &ConstantPadding(0));
142+
let z = i.pad((2, 0), &ZeroPadding{});
143+
assert_eq!(p, z);
129144

130145
let exp = Image::<u8, Gray>::from_shape_data(
131146
7,

0 commit comments

Comments
 (0)