Skip to content

Commit c09d144

Browse files
authored
reduce{h,v}: simplify coefficients handling (libvips#3553)
- De-duplicate `n_points` calculations. - Extract the various filters in inline functions. - Merge the `calculate_coefficients*` functions into a single one. - Simplify `reduce_sum()`.
1 parent 2f1e550 commit c09d144

File tree

4 files changed

+134
-164
lines changed

4 files changed

+134
-164
lines changed

libvips/resample/presample.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ GType vips_resample_get_type(void);
7070
#define MAX_POINT (2000)
7171

7272
int vips_reduce_get_points(VipsKernel kernel, double shrink);
73-
void vips_reduce_make_mask(double *c,
74-
VipsKernel kernel, double shrink, double x);
7573

7674
void vips_reduceh_uchar_hwy(VipsPel *pout, VipsPel *pin,
7775
int n, int width, int bands,

libvips/resample/reduceh.cpp

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ vips_reduce_get_points(VipsKernel kernel, double shrink)
120120
return 2 * rint(2 * shrink) + 1;
121121

122122
case VIPS_KERNEL_LANCZOS2:
123-
/* Needs to be in sync with calculate_coefficients_lanczos().
124-
*/
125123
return 2 * rint(2 * shrink) + 1;
126124

127125
case VIPS_KERNEL_LANCZOS3:
@@ -133,45 +131,6 @@ vips_reduce_get_points(VipsKernel kernel, double shrink)
133131
}
134132
}
135133

136-
/* Calculate a mask element.
137-
*/
138-
void
139-
vips_reduce_make_mask(double *c, VipsKernel kernel, double shrink, double x)
140-
{
141-
switch (kernel) {
142-
case VIPS_KERNEL_NEAREST:
143-
c[0] = 1.0;
144-
break;
145-
146-
case VIPS_KERNEL_LINEAR:
147-
calculate_coefficients_triangle(c, shrink, x);
148-
break;
149-
150-
case VIPS_KERNEL_CUBIC:
151-
/* Catmull-Rom.
152-
*/
153-
calculate_coefficients_cubic(c, shrink, x, 0.0, 0.5);
154-
break;
155-
156-
case VIPS_KERNEL_MITCHELL:
157-
calculate_coefficients_cubic(c, shrink, x,
158-
1.0 / 3.0, 1.0 / 3.0);
159-
break;
160-
161-
case VIPS_KERNEL_LANCZOS2:
162-
calculate_coefficients_lanczos(c, 2, shrink, x);
163-
break;
164-
165-
case VIPS_KERNEL_LANCZOS3:
166-
calculate_coefficients_lanczos(c, 3, shrink, x);
167-
break;
168-
169-
default:
170-
g_assert_not_reached();
171-
break;
172-
}
173-
}
174-
175134
template <typename T, int max_value>
176135
static void inline reduceh_unsigned_int_tab(VipsReduceh *reduceh,
177136
VipsPel *pout, const VipsPel *pin,
@@ -275,7 +234,8 @@ static void inline reduceh_notab(VipsReduceh *reduceh,
275234

276235
double cx[MAX_POINT];
277236

278-
vips_reduce_make_mask(cx, reduceh->kernel, reduceh->hshrink, x);
237+
vips_reduce_make_mask(cx, reduceh->kernel, reduceh->n_point,
238+
reduceh->hshrink, x);
279239

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

558-
vips_reduce_make_mask(reduceh->matrixf[x],
559-
reduceh->kernel, reduceh->hshrink,
518+
vips_reduce_make_mask(reduceh->matrixf[x], reduceh->kernel,
519+
reduceh->n_point, reduceh->hshrink,
560520
(float) x / VIPS_TRANSFORM_SCALE);
561521

562522
for (int i = 0; i < reduceh->n_point; i++)

libvips/resample/reducev.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,8 @@ static void inline reducev_notab(VipsReducev *reducev,
511511

512512
double cy[MAX_POINT];
513513

514-
vips_reduce_make_mask(cy, reducev->kernel, reducev->vshrink, y);
514+
vips_reduce_make_mask(cy, reducev->kernel, reducev->n_point,
515+
reducev->vshrink, y);
515516

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

959-
vips_reduce_make_mask(reducev->matrixf[y],
960-
reducev->kernel, reducev->vshrink,
960+
vips_reduce_make_mask(reducev->matrixf[y], reducev->kernel,
961+
reducev->n_point, reducev->vshrink,
961962
(float) y / VIPS_TRANSFORM_SCALE);
962963

963964
for (int i = 0; i < reducev->n_point; i++)

0 commit comments

Comments
 (0)