Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/bindings/chainner_ext.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class ResizeFilter(Enum):
Lanczos = 4
Lagrange = 7
Gauss = 5
MKS2013 = 12
MKS2021 = 13

def resize(
img: np.ndarray,
Expand Down
4 changes: 4 additions & 0 deletions crates/bindings/src/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub enum ResizeFilter {
Lanczos = 4,
Lagrange = 7,
Gauss = 5,
MKS2013 = 12,
MKS2021 = 13,
}

impl From<ResizeFilter> for Filter {
Expand All @@ -41,6 +43,8 @@ impl From<ResizeFilter> for Filter {
ResizeFilter::Lanczos => Filter::Lanczos3,
ResizeFilter::Lagrange => Filter::Lagrange,
ResizeFilter::Gauss => Filter::Gauss,
ResizeFilter::MKS2013 => Filter::MKS2013,
ResizeFilter::MKS2021 => Filter::MKS2021,
}
}
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 45 additions & 1 deletion crates/image-ops/src/scale/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub enum Filter {
Lanczos3,
Lagrange,
Gauss,
MKS2013,
MKS2021,
}

#[inline]
Expand Down Expand Up @@ -65,6 +67,40 @@ fn lagrange(x: f32, support: f32) -> f32 {
value
}

// Taken from
// https://github.com/ImageMagick/ImageMagick/blob/b5f748c38ad5bb6b58f46411b1bcb78392b5f575/MagickCore/resize.c#L428
fn mks2013(x: f32) -> f32 {
let x = x.abs();
if x < 0.5 {
0.625 + 1.75 * (0.5 - x) * (0.5 + x)
} else if x < 1.5 {
(1.0 - x) * (1.75 - x)
} else if x < 2.5 {
-0.125 * (2.5 - x) * (2.5 - x)
} else {
0.0
}
}

// Taken from
// https://github.com/ImageMagick/ImageMagick/blob/b5f748c38ad5bb6b58f46411b1bcb78392b5f575/MagickCore/resize.c#L448
fn mks2021(x: f32) -> f32 {
let x = x.abs();
if x < 0.5 {
577.0 / 576.0 - 239.0 / 144.0 * x * x
} else if x < 1.5 {
35.0 / 36.0 * (x - 1.0) * (x - 239.0 / 140.0)
} else if x < 2.5 {
1.0 / 6.0 * (x - 2.0) * (65.0 / 24.0 - x)
} else if x < 3.5 {
1.0 / 36.0 * (x - 3.0) * (x - 3.75)
} else if x < 4.5 {
-1.0 / 288.0 * (x - 4.5) * (x - 4.5)
} else {
0.0
}
}

impl From<Filter> for resize::Type {
fn from(filter: Filter) -> Self {
match filter {
Expand Down Expand Up @@ -108,6 +144,14 @@ impl From<Filter> for resize::Type {
resize::Type::Custom(filter)
}
Filter::Gauss => resize::Type::Gaussian,
}
Filter::MKS2013 => {
let filter = resize::Filter::new(Box::new(|x| mks2013(x)), 2.5);
resize::Type::Custom(filter)
}
Filter::MKS2021 => {
let filter = resize::Filter::new(Box::new(|x| mks2021(x)), 4.5);
resize::Type::Custom(filter)
}
}
}
}
30 changes: 30 additions & 0 deletions crates/image-ops/src/scale/scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,34 @@ mod tests {
let nn = super::scale(original.view(), new_size, filter).unwrap();
nn.snapshot("resize_lagrange_200");
}

#[test]
fn scale_mks2013() {
let filter = super::Filter::MKS2013;

let original = small_portrait();
let new_size = original.size().scale(4.);
let nn = super::scale(original.view(), new_size, filter).unwrap();
nn.snapshot("resize_mks2013_4x");

let original = read_portrait();
let new_size = Size::new(200, 200);
let nn = super::scale(original.view(), new_size, filter).unwrap();
nn.snapshot("resize_mks2013_200");
}

#[test]
fn scale_mks2021() {
let filter = super::Filter::MKS2021;

let original = small_portrait();
let new_size = original.size().scale(4.);
let nn = super::scale(original.view(), new_size, filter).unwrap();
nn.snapshot("resize_mks2021_4x");

let original = read_portrait();
let new_size = Size::new(200, 200);
let nn = super::scale(original.view(), new_size, filter).unwrap();
nn.snapshot("resize_mks2021_200");
}
}