Skip to content

Commit

Permalink
reduce{h,v}: simplify coefficients handling
Browse files Browse the repository at this point in the history
- De-duplicate `n_points` calculations.
- Extract the various filters in inline functions.
- Merge the `calculate_coefficients*` functions into a single one.
- Simplify `reduce_sum()`.
  • Loading branch information
kleisauke committed Sep 26, 2023
1 parent b32cb5e commit 2ebefa4
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 164 deletions.
2 changes: 0 additions & 2 deletions libvips/resample/presample.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ GType vips_resample_get_type(void);
#define MAX_POINT (2000)

int vips_reduce_get_points(VipsKernel kernel, double shrink);
void vips_reduce_make_mask(double *c,
VipsKernel kernel, double shrink, double x);

void vips_reduceh_uchar_hwy(VipsPel *pout, VipsPel *pin,
int n, int width, int bands,
Expand Down
48 changes: 4 additions & 44 deletions libvips/resample/reduceh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ vips_reduce_get_points(VipsKernel kernel, double shrink)
return 2 * rint(2 * shrink) + 1;

case VIPS_KERNEL_LANCZOS2:
/* Needs to be in sync with calculate_coefficients_lanczos().
*/
return 2 * rint(2 * shrink) + 1;

case VIPS_KERNEL_LANCZOS3:
Expand All @@ -133,45 +131,6 @@ vips_reduce_get_points(VipsKernel kernel, double shrink)
}
}

/* Calculate a mask element.
*/
void
vips_reduce_make_mask(double *c, VipsKernel kernel, double shrink, double x)
{
switch (kernel) {
case VIPS_KERNEL_NEAREST:
c[0] = 1.0;
break;

case VIPS_KERNEL_LINEAR:
calculate_coefficients_triangle(c, shrink, x);
break;

case VIPS_KERNEL_CUBIC:
/* Catmull-Rom.
*/
calculate_coefficients_cubic(c, shrink, x, 0.0, 0.5);
break;

case VIPS_KERNEL_MITCHELL:
calculate_coefficients_cubic(c, shrink, x,
1.0 / 3.0, 1.0 / 3.0);
break;

case VIPS_KERNEL_LANCZOS2:
calculate_coefficients_lanczos(c, 2, shrink, x);
break;

case VIPS_KERNEL_LANCZOS3:
calculate_coefficients_lanczos(c, 3, shrink, x);
break;

default:
g_assert_not_reached();
break;
}
}

template <typename T, int max_value>
static void inline reduceh_unsigned_int_tab(VipsReduceh *reduceh,
VipsPel *pout, const VipsPel *pin,
Expand Down Expand Up @@ -275,7 +234,8 @@ static void inline reduceh_notab(VipsReduceh *reduceh,

double cx[MAX_POINT];

vips_reduce_make_mask(cx, reduceh->kernel, reduceh->hshrink, x);
vips_reduce_make_mask(cx, reduceh->kernel, reduceh->n_point,
reduceh->hshrink, x);

for (int z = 0; z < bands; z++) {
double sum;
Expand Down Expand Up @@ -555,8 +515,8 @@ vips_reduceh_build(VipsObject *object)
!reduceh->matrixs[x])
return -1;

vips_reduce_make_mask(reduceh->matrixf[x],
reduceh->kernel, reduceh->hshrink,
vips_reduce_make_mask(reduceh->matrixf[x], reduceh->kernel,
reduceh->n_point, reduceh->hshrink,
(float) x / VIPS_TRANSFORM_SCALE);

for (int i = 0; i < reduceh->n_point; i++)
Expand Down
7 changes: 4 additions & 3 deletions libvips/resample/reducev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ static void inline reducev_notab(VipsReducev *reducev,

double cy[MAX_POINT];

vips_reduce_make_mask(cy, reducev->kernel, reducev->vshrink, y);
vips_reduce_make_mask(cy, reducev->kernel, reducev->n_point,
reducev->vshrink, y);

for (int z = 0; z < ne; z++) {
double sum;
Expand Down Expand Up @@ -956,8 +957,8 @@ vips_reducev_build(VipsObject *object)
!reducev->matrixs[y])
return -1;

vips_reduce_make_mask(reducev->matrixf[y],
reducev->kernel, reducev->vshrink,
vips_reduce_make_mask(reducev->matrixf[y], reducev->kernel,
reducev->n_point, reducev->vshrink,
(float) y / VIPS_TRANSFORM_SCALE);

for (int i = 0; i < reducev->n_point; i++)
Expand Down
Loading

0 comments on commit 2ebefa4

Please sign in to comment.