Skip to content

Add Magic Kernel support #4237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 3, 2024
Merged
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 ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
8.17.0

- add Magic Kernel support [akimon658]

8.16.1

- support multipage JXL
Expand Down
2 changes: 2 additions & 0 deletions libvips/include/vips/resample.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ typedef enum {
VIPS_KERNEL_MITCHELL,
VIPS_KERNEL_LANCZOS2,
VIPS_KERNEL_LANCZOS3,
VIPS_KERNEL_MKS2013,
VIPS_KERNEL_MKS2021,
VIPS_KERNEL_LAST
} VipsKernel;

Expand Down
2 changes: 2 additions & 0 deletions libvips/resample/reduce.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
* @VIPS_KERNEL_MITCHELL: Convolve with a Mitchell kernel.
* @VIPS_KERNEL_LANCZOS2: Convolve with a two-lobe Lanczos kernel.
* @VIPS_KERNEL_LANCZOS3: Convolve with a three-lobe Lanczos kernel.
* @VIPS_KERNEL_MKS2013: Convolve with Magic Kernel Sharp 2013.
* @VIPS_KERNEL_MKS2021: Convolve with Magic Kernel Sharp 2021.
*
* The resampling kernels vips supports. See vips_reduce(), for example.
*/
Expand Down
6 changes: 6 additions & 0 deletions libvips/resample/reduceh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ vips_reduce_get_points(VipsKernel kernel, double shrink)
case VIPS_KERNEL_LANCZOS3:
return 2 * rint(3 * shrink) + 1;

case VIPS_KERNEL_MKS2013:
return 2 * rint(3 * shrink) + 1;

case VIPS_KERNEL_MKS2021:
return 2 * rint(5 * shrink) + 1;

default:
g_assert_not_reached();
return 0;
Expand Down
52 changes: 52 additions & 0 deletions libvips/resample/templates.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,48 @@ double inline filter<VIPS_KERNEL_LANCZOS3>(double x)
return 0.0;
}

template <>
double inline filter<VIPS_KERNEL_MKS2013>(double x)
{
if (x < 0.0)
x = -x;

if (x >= 2.5)
return 0.0;

if (x >= 1.5)
return (x - 5.0 / 2.0) * (x - 5.0 / 2.0) / -8.0;

if (x >= 0.5)
return (4.0 * x * x - 11.0 * x + 7.0) / 4.0;

return 17.0 / 16.0 - 7.0 * x * x / 4.0;
}

template <>
double inline filter<VIPS_KERNEL_MKS2021>(double x)
{
if (x < 0.0)
x = -x;

if (x >= 4.5)
return 0.0;

if (x >= 3.5)
return (4.0 * x * x - 36.0 * x + 81.0) / -1152.0;

if (x >= 2.5)
return (4.0 * x * x - 27.0 * x + 45.0) / 144.0;

if (x >= 1.5)
return (24.0 * x * x - 113.0 * x + 130.0) / -144.0;

if (x >= 0.5)
return (140.0 * x * x - 379.0 * x + 239.0) / 144.0;

return 577.0 / 576.0 - 239.0 * x * x / 144.0;
}

/* Given an x in [0,1] (we can have x == 1 when building tables),
* calculate c0 .. c(@n_points), the coefficients. This is called
* from the interpolator as well as from the table builder.
Expand Down Expand Up @@ -469,6 +511,16 @@ vips_reduce_make_mask(T *c, VipsKernel kernel, const int n_points,
filter<VIPS_KERNEL_LANCZOS3>, shrink, x);
break;

case VIPS_KERNEL_MKS2013:
calculate_coefficients(c, n_points,
filter<VIPS_KERNEL_MKS2013>, shrink, x);
break;

case VIPS_KERNEL_MKS2021:
calculate_coefficients(c, n_points,
filter<VIPS_KERNEL_MKS2021>, shrink, x);
break;

default:
g_assert_not_reached();
break;
Expand Down
6 changes: 4 additions & 2 deletions test/test-suite/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def test_reduce(self):
for fac in [1, 1.1, 1.5, 1.999]:
for fmt in all_formats:
for kernel in ["nearest", "linear",
"cubic", "lanczos2", "lanczos3"]:
"cubic", "lanczos2",
"lanczos3", "mks2013", "mks2021"]:
x = im.cast(fmt)
r = x.reduce(fac, fac, kernel=kernel)
d = abs(r.avg() - im.avg())
Expand All @@ -93,7 +94,8 @@ def test_reduce(self):
for const in [0, 1, 2, 254, 255]:
im = (pyvips.Image.black(10, 10) + const).cast("uchar")
for kernel in ["nearest", "linear",
"cubic", "lanczos2", "lanczos3"]:
"cubic", "lanczos2",
"lanczos3", "mks2013", "mks2021"]:
# print "testing kernel =", kernel
# print "testing const =", const
shr = im.reduce(2, 2, kernel=kernel)
Expand Down
Loading