Coverage Report

Created: 2026-12-01 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvips/libvips/resample/lbb.cpp
Line
Count
Source
1
/* lbb (locally bounded bicubic) resampler
2
 *
3
 * N. Robidoux, C. Racette and J. Cupitt, 23-28/03/2010
4
 *
5
 * N. Robidoux, 16-19/05/2010
6
 *
7
 * N. Robidoux, 22/11/2011
8
 */
9
10
/*
11
12
  This file is part of VIPS.
13
14
  VIPS is free software; you can redistribute it and/or modify it
15
  under the terms of the GNU Lesser General Public License as
16
  published by the Free Software Foundation; either version 2 of the
17
  License, or (at your option) any later version.
18
19
  This program is distributed in the hope that it will be useful,
20
  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22
  Lesser General Public License for more details.
23
24
  You should have received a copy of the GNU Lesser General Public
25
  License along with this program; if not, write to the Free Software
26
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27
  02110-1301 USA
28
29
 */
30
31
/*
32
33
  These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
34
35
 */
36
37
/*
38
 * 2010 (c) Nicolas Robidoux, Chantal Racette, John Cupitt.
39
 *
40
 * N. Robidoux thanks Adam Turcotte, Geert Jordaens, Ralf Meyer,
41
 * Øyvind Kolås, Minglun Gong, Eric Daoust and Sven Neumann for useful
42
 * comments and code.
43
 *
44
 * C. Racette's image resampling research and programming funded in
45
 * part by an NSERC (National Science and Engineering Research Council
46
 * of Canada) Alexander Graham Bell Canada Graduate Scholarship, by an
47
 * NSERC Discovery Grant awarded to Julien Dompierre (grant number
48
 * 20-61098) and by N. Robidoux's Laurentian University professional
49
 * allowance.
50
 */
51
52
/*
53
 * LBB has two versions:
54
 *
55
 *   A "soft" version, which shows a little less staircasing and a
56
 *   little more haloing, and which is a little more expensive to
57
 *   compute. We recommend this as the default.
58
 *
59
 *   A "sharp" version, which shows a little more staircasing and a
60
 *   little less haloing, which is a little cheaper (it uses 6 less
61
 *   comparisons and 12 less "? :").
62
 *
63
 * The only difference between the two is that the "soft" versions
64
 * uses local minima and maxima computed over 3x3 square blocks, and
65
 * the "sharp" version uses local minima and maxima computed over 3x3
66
 * crosses.
67
 *
68
 * If you want to use the "sharp" version, comment out the following
69
 * three pre-processor code lines:
70
 */
71
/*
72
#ifndef __LBB_CHEAP_H__
73
#define __LBB_CHEAP_H__
74
#endif
75
*/
76
77
/*
78
 * LBB (Locally Bounded Bicubic) is a high quality nonlinear variant
79
 * of Catmull-Rom. Images resampled with LBB have much smaller halos
80
 * than images resampled with windowed sincs or other interpolatory
81
 * cubic spline filters. Specifically, LBB halos are narrower and the
82
 * over/undershoot amplitude is smaller. This is accomplished without
83
 * significantly affecting the smoothness of the result (compared to
84
 * Catmull-Rom).
85
 *
86
 * Another important property is that the resampled values are
87
 * contained within the range of nearby input values. Consequently, no
88
 * final clamping is needed to stay "in range" (e.g., 0-255 for
89
 * standard 8-bit images).
90
 *
91
 * LBB was developed by N. Robidoux and C. Racette at the Department
92
 * of Mathematics and Computer Science of Laurentian University in the
93
 * course of C. Racette's Masters thesis in Computational
94
 * Sciences. Preliminary work directly leading to the LBB method and
95
 * code was performed by C. Racette and N. Robidoux in the course of
96
 * her honours thesis, and by N. Robidoux, A. Turcotte and E. Daoust
97
 * during Google Summer of Code 2009 (through two awards made to GIMP
98
 * to improve GEGL).
99
 *
100
 * LBB is a novel method with the following properties:
101
 *
102
 * --LBB is a Hermite bicubic method: The bicubic surface is defined,
103
 *   one convex hull of four nearby input points at a time, using four
104
 *   point values, four x-derivatives, four y-derivatives, and four
105
 *   cross-derivatives.
106
 *
107
 * --The stencil for values in a square patch is the usual 4x4.
108
 *
109
 * --LBB is interpolatory.
110
 *
111
 * --It is C^1 with continuous cross derivatives.
112
 *
113
 * --When the limiters are inactive, LBB gives the same result as
114
 *   Catmull-Rom.
115
 *
116
 * --When used on binary images, LBB gives results similar to bicubic
117
 *   Hermite with all first derivatives---but not necessarily the
118
 *   cross derivatives (this last assertion needs to be double
119
 *   checked)--at input pixel locations set to zero.
120
 *
121
 * --The LBB reconstruction is locally bounded: Over each square
122
 *   patch, the surface is contained between the minimum and the
123
 *   maximum of the 16 nearest input pixel values.
124
 *
125
 * --Consequently, the LBB reconstruction is globally bounded between
126
 *   the very smallest input pixel value and the very largest input
127
 *   pixel value. It is not necessary to clamp results.
128
 *
129
 * The LBB method is based on the method of Ken Brodlie, Petros
130
 * Mashwama and Sohail Butt for constraining Hermite interpolants
131
 * between globally defined planes:
132
 *
133
 *   Visualization of surface data to preserve positivity and other
134
 *   simple constraints. Computer & Graphics, Vol. 19, Number 4, pages
135
 *   585-594, 1995. DOI: 10.1016/0097-8493(95)00036-C.
136
 *
137
 * Instead of forcing the reconstructed surface to lie between two
138
 * GLOBALLY defined planes, LBB constrains one patch at a time to lie
139
 * between LOCALLY defined planes. This is accomplished by
140
 * constraining the derivatives (x, y and cross) at each input pixel
141
 * location so that if the constraint was applied everywhere the
142
 * surface would fit between the min and max of the values at the 9
143
 * closest pixel locations. Because this is done with each of the four
144
 * pixel locations which define the bicubic patch, this forces the
145
 * reconstructed surface to lie between the min and max of the values
146
 * at the 16 closest values pixel locations. (Each corner defines its
147
 * own 3x3 subgroup of the 4x4 stencil. Consequently, the surface is
148
 * necessarily above the minimum of the four minima, which happens to
149
 * be the minimum over the 4x4. Similarly with the maxima.)
150
 *
151
 * The above paragraph described the "soft" version of LBB. The
152
 * "sharp" version is similar.
153
 */
154
155
#ifdef HAVE_CONFIG_H
156
#include <config.h>
157
#endif /*HAVE_CONFIG_H*/
158
#include <glib/gi18n-lib.h>
159
160
#include <cstdio>
161
#include <cstdlib>
162
163
#include <vips/vips.h>
164
#include <vips/internal.h>
165
166
#include "templates.h"
167
168
#define VIPS_TYPE_INTERPOLATE_LBB \
169
  (vips_interpolate_lbb_get_type())
170
#define VIPS_INTERPOLATE_LBB(obj) \
171
  (G_TYPE_CHECK_INSTANCE_CAST((obj), \
172
    VIPS_TYPE_INTERPOLATE_LBB, VipsInterpolateLbb))
173
#define VIPS_INTERPOLATE_LBB_CLASS(klass) \
174
  (G_TYPE_CHECK_CLASS_CAST((klass), \
175
    VIPS_TYPE_INTERPOLATE_LBB, VipsInterpolateLbbClass))
176
#define VIPS_IS_INTERPOLATE_LBB(obj) \
177
  (G_TYPE_CHECK_INSTANCE_TYPE((obj), VIPS_TYPE_INTERPOLATE_LBB))
178
#define VIPS_IS_INTERPOLATE_LBB_CLASS(klass) \
179
  (G_TYPE_CHECK_CLASS_TYPE((klass), VIPS_TYPE_INTERPOLATE_LBB))
180
#define VIPS_INTERPOLATE_LBB_GET_CLASS(obj) \
181
  (G_TYPE_INSTANCE_GET_CLASS((obj), \
182
    VIPS_TYPE_INTERPOLATE_LBB, VipsInterpolateLbbClass))
183
184
typedef struct _VipsInterpolateLbb {
185
  VipsInterpolate parent_object;
186
187
} VipsInterpolateLbb;
188
189
typedef struct _VipsInterpolateLbbClass {
190
  VipsInterpolateClass parent_class;
191
192
} VipsInterpolateLbbClass;
193
194
/*
195
 * Absolute value and sign macros:
196
 */
197
0
#define LBB_ABS(x) (((x) >= 0.) ? (x) : -(x))
198
0
#define LBB_SIGN(x) (((x) >= 0.) ? 1.0 : -1.0)
199
/*
200
 * MIN and MAX macros set up so that I can put the likely winner in
201
 * the first argument (forward branch likely blah blah blah):
202
 */
203
0
#define LBB_MIN(x, y) (((x) <= (y)) ? (x) : (y))
204
0
#define LBB_MAX(x, y) (((x) >= (y)) ? (x) : (y))
205
206
static inline double
207
lbbicubic(const double c00,
208
  const double c10,
209
  const double c01,
210
  const double c11,
211
  const double c00dx,
212
  const double c10dx,
213
  const double c01dx,
214
  const double c11dx,
215
  const double c00dy,
216
  const double c10dy,
217
  const double c01dy,
218
  const double c11dy,
219
  const double c00dxdy,
220
  const double c10dxdy,
221
  const double c01dxdy,
222
  const double c11dxdy,
223
  const double uno_one,
224
  const double uno_two,
225
  const double uno_thr,
226
  const double uno_fou,
227
  const double dos_one,
228
  const double dos_two,
229
  const double dos_thr,
230
  const double dos_fou,
231
  const double tre_one,
232
  const double tre_two,
233
  const double tre_thr,
234
  const double tre_fou,
235
  const double qua_one,
236
  const double qua_two,
237
  const double qua_thr,
238
  const double qua_fou)
239
0
{
240
  /*
241
   * STENCIL (FOOTPRINT) OF INPUT VALUES:
242
   *
243
   * The stencil of LBB is the same as for any standard Hermite
244
   * bicubic (e.g., Catmull-Rom):
245
   *
246
   *  (ix-1,iy-1)  (ix,iy-1)    (ix+1,iy-1)  (ix+2,iy-1)
247
   *  = uno_one    = uno_two    = uno_thr    = uno_fou
248
   *
249
   *  (ix-1,iy)    (ix,iy)      (ix+1,iy)    (ix+2,iy)
250
   *  = dos_one    = dos_two    = dos_thr    = dos_fou
251
   *                        X
252
   *  (ix-1,iy+1)  (ix,iy+1)    (ix+1,iy+1)  (ix+2,iy+1)
253
   *  = tre_one    = tre_two    = tre_thr    = tre_fou
254
   *
255
   *  (ix-1,iy+2)  (ix,iy+2)    (ix+1,iy+2)  (ix+2,iy+2)
256
   *  = qua_one    = qua_two    = qua_thr    = qua_fou
257
   *
258
   * where ix is the (pseudo-)floor of the requested left-to-right
259
   * location ("X"), and iy is the floor of the requested up-to-down
260
   * location.
261
   */
262
263
#if defined(__LBB_CHEAP_H__)
264
  /*
265
   * Computation of the four min and four max over 3x3 input data
266
   * sub-crosses of the 4x4 input stencil, performed with only 22
267
   * comparisons and 28 "? :". If you can figure out how to do this
268
   * more efficiently, let us know.
269
   *
270
   * This is the cheaper (but arguably less desirable in terms of
271
   * quality) version of the computation.
272
   */
273
  const double m1 = (dos_two <= dos_thr) ? dos_two : dos_thr;
274
  const double M1 = (dos_two <= dos_thr) ? dos_thr : dos_two;
275
  const double m2 = (tre_two <= tre_thr) ? tre_two : tre_thr;
276
  const double M2 = (tre_two <= tre_thr) ? tre_thr : tre_two;
277
  const double m3 = (uno_two <= dos_one) ? uno_two : dos_one;
278
  const double M3 = (uno_two <= dos_one) ? dos_one : uno_two;
279
  const double m4 = (uno_thr <= dos_fou) ? uno_thr : dos_fou;
280
  const double M4 = (uno_thr <= dos_fou) ? dos_fou : uno_thr;
281
  const double m5 = (tre_one <= qua_two) ? tre_one : qua_two;
282
  const double M5 = (tre_one <= qua_two) ? qua_two : tre_one;
283
  const double m6 = (tre_fou <= qua_thr) ? tre_fou : qua_thr;
284
  const double M6 = (tre_fou <= qua_thr) ? qua_thr : tre_fou;
285
  const double m7 = LBB_MIN(m1, tre_two);
286
  const double M7 = LBB_MAX(M1, tre_two);
287
  const double m8 = LBB_MIN(m1, tre_thr);
288
  const double M8 = LBB_MAX(M1, tre_thr);
289
  const double m9 = LBB_MIN(m2, dos_two);
290
  const double M9 = LBB_MAX(M2, dos_two);
291
  const double m10 = LBB_MIN(m2, dos_thr);
292
  const double M10 = LBB_MAX(M2, dos_thr);
293
  const double min00 = LBB_MIN(m7, m3);
294
  const double max00 = LBB_MAX(M7, M3);
295
  const double min10 = LBB_MIN(m8, m4);
296
  const double max10 = LBB_MAX(M8, M4);
297
  const double min01 = LBB_MIN(m9, m5);
298
  const double max01 = LBB_MAX(M9, M5);
299
  const double min11 = LBB_MIN(m10, m6);
300
  const double max11 = LBB_MAX(M10, M6);
301
#else
302
  /*
303
   * Computation of the four min and four max over 3x3 input data
304
   * sub-blocks of the 4x4 input stencil, performed with only 28
305
   * comparisons and 34 "? :". If you can figure how to do this more
306
   * efficiently, let us know.
307
   */
308
0
  const double m1 = (dos_two <= dos_thr) ? dos_two : dos_thr;
309
0
  const double M1 = (dos_two <= dos_thr) ? dos_thr : dos_two;
310
0
  const double m2 = (tre_two <= tre_thr) ? tre_two : tre_thr;
311
0
  const double M2 = (tre_two <= tre_thr) ? tre_thr : tre_two;
312
0
  const double m6 = (dos_one <= tre_one) ? dos_one : tre_one;
313
0
  const double M6 = (dos_one <= tre_one) ? tre_one : dos_one;
314
0
  const double m7 = (dos_fou <= tre_fou) ? dos_fou : tre_fou;
315
0
  const double M7 = (dos_fou <= tre_fou) ? tre_fou : dos_fou;
316
0
  const double m3 = (uno_two <= uno_thr) ? uno_two : uno_thr;
317
0
  const double M3 = (uno_two <= uno_thr) ? uno_thr : uno_two;
318
0
  const double m4 = (qua_two <= qua_thr) ? qua_two : qua_thr;
319
0
  const double M4 = (qua_two <= qua_thr) ? qua_thr : qua_two;
320
0
  const double m5 = LBB_MIN(m1, m2);
321
0
  const double M5 = LBB_MAX(M1, M2);
322
0
  const double m10 = LBB_MIN(m6, uno_one);
323
0
  const double M10 = LBB_MAX(M6, uno_one);
324
0
  const double m11 = LBB_MIN(m6, qua_one);
325
0
  const double M11 = LBB_MAX(M6, qua_one);
326
0
  const double m12 = LBB_MIN(m7, uno_fou);
327
0
  const double M12 = LBB_MAX(M7, uno_fou);
328
0
  const double m13 = LBB_MIN(m7, qua_fou);
329
0
  const double M13 = LBB_MAX(M7, qua_fou);
330
0
  const double m8 = LBB_MIN(m5, m3);
331
0
  const double M8 = LBB_MAX(M5, M3);
332
0
  const double m9 = LBB_MIN(m5, m4);
333
0
  const double M9 = LBB_MAX(M5, M4);
334
0
  const double min00 = LBB_MIN(m8, m10);
335
0
  const double max00 = LBB_MAX(M8, M10);
336
0
  const double min10 = LBB_MIN(m8, m12);
337
0
  const double max10 = LBB_MAX(M8, M12);
338
0
  const double min01 = LBB_MIN(m9, m11);
339
0
  const double max01 = LBB_MAX(M9, M11);
340
0
  const double min11 = LBB_MIN(m9, m13);
341
0
  const double max11 = LBB_MAX(M9, M13);
342
0
#endif
343
344
  /*
345
   * The remainder of the "per channel" computation involves the
346
   * computation of:
347
   *
348
   * --8 conditional moves,
349
   *
350
   * --8 signs (in which the sign of zero is unimportant),
351
   *
352
   * --12 minima of two values,
353
   *
354
   * --8 maxima of two values,
355
   *
356
   * --8 absolute values,
357
   *
358
   * for a grand total of 29 minima, 25 maxima, 8 conditional moves, 8
359
   * signs, and 8 absolute values. If everything is done with
360
   * conditional moves, "only" 28+8+8+12+8+8=72 flags are involved
361
   * (because initial min and max can be computed with one flag).
362
   *
363
   * The "per channel" part of the computation also involves 107
364
   * arithmetic operations (54 *, 21 +, 42 -).
365
   */
366
367
  /*
368
   * Distances to the local min and max:
369
   */
370
0
  const double u00 = dos_two - min00;
371
0
  const double v00 = max00 - dos_two;
372
0
  const double u10 = dos_thr - min10;
373
0
  const double v10 = max10 - dos_thr;
374
0
  const double u01 = tre_two - min01;
375
0
  const double v01 = max01 - tre_two;
376
0
  const double u11 = tre_thr - min11;
377
0
  const double v11 = max11 - tre_thr;
378
379
  /*
380
   * Initial values of the derivatives computed with centered
381
   * differences. Factors of 1/2 are left out because they are folded
382
   * in later:
383
   */
384
0
  const double dble_dzdx00i = dos_thr - dos_one;
385
0
  const double dble_dzdy11i = qua_thr - dos_thr;
386
0
  const double dble_dzdx10i = dos_fou - dos_two;
387
0
  const double dble_dzdy01i = qua_two - dos_two;
388
0
  const double dble_dzdx01i = tre_thr - tre_one;
389
0
  const double dble_dzdy10i = tre_thr - uno_thr;
390
0
  const double dble_dzdx11i = tre_fou - tre_two;
391
0
  const double dble_dzdy00i = tre_two - uno_two;
392
393
  /*
394
   * Signs of the derivatives. The upcoming clamping does not change
395
   * them (except if the clamping sends a negative derivative to 0, in
396
   * which case the sign does not matter anyway).
397
   */
398
0
  const double sign_dzdx00 = LBB_SIGN(dble_dzdx00i);
399
0
  const double sign_dzdx10 = LBB_SIGN(dble_dzdx10i);
400
0
  const double sign_dzdx01 = LBB_SIGN(dble_dzdx01i);
401
0
  const double sign_dzdx11 = LBB_SIGN(dble_dzdx11i);
402
403
0
  const double sign_dzdy00 = LBB_SIGN(dble_dzdy00i);
404
0
  const double sign_dzdy10 = LBB_SIGN(dble_dzdy10i);
405
0
  const double sign_dzdy01 = LBB_SIGN(dble_dzdy01i);
406
0
  const double sign_dzdy11 = LBB_SIGN(dble_dzdy11i);
407
408
  /*
409
   * Initial values of the cross-derivatives. Factors of 1/4 are left
410
   * out because folded in later:
411
   */
412
0
  const double quad_d2zdxdy00i = uno_one - uno_thr + dble_dzdx01i;
413
0
  const double quad_d2zdxdy10i = uno_two - uno_fou + dble_dzdx11i;
414
0
  const double quad_d2zdxdy01i = qua_thr - qua_one - dble_dzdx00i;
415
0
  const double quad_d2zdxdy11i = qua_fou - qua_two - dble_dzdx10i;
416
417
  /*
418
   * Slope limiters. The key multiplier is 3 but we fold a factor of
419
   * 2, hence 6:
420
   */
421
0
  const double dble_slopelimit_00 = 6.0 * LBB_MIN(u00, v00);
422
0
  const double dble_slopelimit_10 = 6.0 * LBB_MIN(u10, v10);
423
0
  const double dble_slopelimit_01 = 6.0 * LBB_MIN(u01, v01);
424
0
  const double dble_slopelimit_11 = 6.0 * LBB_MIN(u11, v11);
425
426
  /*
427
   * Clamped first derivatives:
428
   */
429
0
  const double dble_dzdx00 =
430
0
    (sign_dzdx00 * dble_dzdx00i <= dble_slopelimit_00)
431
0
    ? dble_dzdx00i
432
0
    : sign_dzdx00 * dble_slopelimit_00;
433
0
  const double dble_dzdy00 =
434
0
    (sign_dzdy00 * dble_dzdy00i <= dble_slopelimit_00)
435
0
    ? dble_dzdy00i
436
0
    : sign_dzdy00 * dble_slopelimit_00;
437
0
  const double dble_dzdx10 =
438
0
    (sign_dzdx10 * dble_dzdx10i <= dble_slopelimit_10)
439
0
    ? dble_dzdx10i
440
0
    : sign_dzdx10 * dble_slopelimit_10;
441
0
  const double dble_dzdy10 =
442
0
    (sign_dzdy10 * dble_dzdy10i <= dble_slopelimit_10)
443
0
    ? dble_dzdy10i
444
0
    : sign_dzdy10 * dble_slopelimit_10;
445
0
  const double dble_dzdx01 =
446
0
    (sign_dzdx01 * dble_dzdx01i <= dble_slopelimit_01)
447
0
    ? dble_dzdx01i
448
0
    : sign_dzdx01 * dble_slopelimit_01;
449
0
  const double dble_dzdy01 =
450
0
    (sign_dzdy01 * dble_dzdy01i <= dble_slopelimit_01)
451
0
    ? dble_dzdy01i
452
0
    : sign_dzdy01 * dble_slopelimit_01;
453
0
  const double dble_dzdx11 =
454
0
    (sign_dzdx11 * dble_dzdx11i <= dble_slopelimit_11)
455
0
    ? dble_dzdx11i
456
0
    : sign_dzdx11 * dble_slopelimit_11;
457
0
  const double dble_dzdy11 =
458
0
    (sign_dzdy11 * dble_dzdy11i <= dble_slopelimit_11)
459
0
    ? dble_dzdy11i
460
0
    : sign_dzdy11 * dble_slopelimit_11;
461
462
  /*
463
   * Sums and differences of first derivatives:
464
   */
465
0
  const double twelve_sum00 = 6.0 * (dble_dzdx00 + dble_dzdy00);
466
0
  const double twelve_dif00 = 6.0 * (dble_dzdx00 - dble_dzdy00);
467
0
  const double twelve_sum10 = 6.0 * (dble_dzdx10 + dble_dzdy10);
468
0
  const double twelve_dif10 = 6.0 * (dble_dzdx10 - dble_dzdy10);
469
0
  const double twelve_sum01 = 6.0 * (dble_dzdx01 + dble_dzdy01);
470
0
  const double twelve_dif01 = 6.0 * (dble_dzdx01 - dble_dzdy01);
471
0
  const double twelve_sum11 = 6.0 * (dble_dzdx11 + dble_dzdy11);
472
0
  const double twelve_dif11 = 6.0 * (dble_dzdx11 - dble_dzdy11);
473
474
  /*
475
   * Absolute values of the sums:
476
   */
477
0
  const double twelve_abs_sum00 = LBB_ABS(twelve_sum00);
478
0
  const double twelve_abs_sum10 = LBB_ABS(twelve_sum10);
479
0
  const double twelve_abs_sum01 = LBB_ABS(twelve_sum01);
480
0
  const double twelve_abs_sum11 = LBB_ABS(twelve_sum11);
481
482
  /*
483
   * Scaled distances to the min:
484
   */
485
0
  const double u00_times_36 = 36.0 * u00;
486
0
  const double u10_times_36 = 36.0 * u10;
487
0
  const double u01_times_36 = 36.0 * u01;
488
0
  const double u11_times_36 = 36.0 * u11;
489
490
  /*
491
   * First cross-derivative limiter:
492
   */
493
0
  const double first_limit00 = twelve_abs_sum00 - u00_times_36;
494
0
  const double first_limit10 = twelve_abs_sum10 - u10_times_36;
495
0
  const double first_limit01 = twelve_abs_sum01 - u01_times_36;
496
0
  const double first_limit11 = twelve_abs_sum11 - u11_times_36;
497
498
0
  const double quad_d2zdxdy00ii = LBB_MAX(quad_d2zdxdy00i, first_limit00);
499
0
  const double quad_d2zdxdy10ii = LBB_MAX(quad_d2zdxdy10i, first_limit10);
500
0
  const double quad_d2zdxdy01ii = LBB_MAX(quad_d2zdxdy01i, first_limit01);
501
0
  const double quad_d2zdxdy11ii = LBB_MAX(quad_d2zdxdy11i, first_limit11);
502
503
  /*
504
   * Scaled distances to the max:
505
   */
506
0
  const double v00_times_36 = 36.0 * v00;
507
0
  const double v10_times_36 = 36.0 * v10;
508
0
  const double v01_times_36 = 36.0 * v01;
509
0
  const double v11_times_36 = 36.0 * v11;
510
511
  /*
512
   * Second cross-derivative limiter:
513
   */
514
0
  const double second_limit00 = v00_times_36 - twelve_abs_sum00;
515
0
  const double second_limit10 = v10_times_36 - twelve_abs_sum10;
516
0
  const double second_limit01 = v01_times_36 - twelve_abs_sum01;
517
0
  const double second_limit11 = v11_times_36 - twelve_abs_sum11;
518
519
0
  const double quad_d2zdxdy00iii = LBB_MIN(quad_d2zdxdy00ii, second_limit00);
520
0
  const double quad_d2zdxdy10iii = LBB_MIN(quad_d2zdxdy10ii, second_limit10);
521
0
  const double quad_d2zdxdy01iii = LBB_MIN(quad_d2zdxdy01ii, second_limit01);
522
0
  const double quad_d2zdxdy11iii = LBB_MIN(quad_d2zdxdy11ii, second_limit11);
523
524
  /*
525
   * Absolute values of the differences:
526
   */
527
0
  const double twelve_abs_dif00 = LBB_ABS(twelve_dif00);
528
0
  const double twelve_abs_dif10 = LBB_ABS(twelve_dif10);
529
0
  const double twelve_abs_dif01 = LBB_ABS(twelve_dif01);
530
0
  const double twelve_abs_dif11 = LBB_ABS(twelve_dif11);
531
532
  /*
533
   * Third cross-derivative limiter:
534
   */
535
0
  const double third_limit00 = twelve_abs_dif00 - v00_times_36;
536
0
  const double third_limit10 = twelve_abs_dif10 - v10_times_36;
537
0
  const double third_limit01 = twelve_abs_dif01 - v01_times_36;
538
0
  const double third_limit11 = twelve_abs_dif11 - v11_times_36;
539
540
0
  const double quad_d2zdxdy00iiii = LBB_MAX(quad_d2zdxdy00iii, third_limit00);
541
0
  const double quad_d2zdxdy10iiii = LBB_MAX(quad_d2zdxdy10iii, third_limit10);
542
0
  const double quad_d2zdxdy01iiii = LBB_MAX(quad_d2zdxdy01iii, third_limit01);
543
0
  const double quad_d2zdxdy11iiii = LBB_MAX(quad_d2zdxdy11iii, third_limit11);
544
545
  /*
546
   * Fourth cross-derivative limiter:
547
   */
548
0
  const double fourth_limit00 = u00_times_36 - twelve_abs_dif00;
549
0
  const double fourth_limit10 = u10_times_36 - twelve_abs_dif10;
550
0
  const double fourth_limit01 = u01_times_36 - twelve_abs_dif01;
551
0
  const double fourth_limit11 = u11_times_36 - twelve_abs_dif11;
552
553
0
  const double quad_d2zdxdy00 = LBB_MIN(quad_d2zdxdy00iiii, fourth_limit00);
554
0
  const double quad_d2zdxdy10 = LBB_MIN(quad_d2zdxdy10iiii, fourth_limit10);
555
0
  const double quad_d2zdxdy01 = LBB_MIN(quad_d2zdxdy01iiii, fourth_limit01);
556
0
  const double quad_d2zdxdy11 = LBB_MIN(quad_d2zdxdy11iiii, fourth_limit11);
557
558
  /*
559
   * Part of the result which does not need derivatives:
560
   */
561
0
  const double newval1 = c00 * dos_two + c10 * dos_thr +
562
0
    c01 * tre_two + c11 * tre_thr;
563
564
  /*
565
   * Twice the part of the result which only needs first derivatives.
566
   */
567
0
  const double newval2 = c00dx * dble_dzdx00 + c10dx * dble_dzdx10 +
568
0
    c01dx * dble_dzdx01 + c11dx * dble_dzdx11 +
569
0
    c00dy * dble_dzdy00 + c10dy * dble_dzdy10 +
570
0
    c01dy * dble_dzdy01 + c11dy * dble_dzdy11;
571
572
  /*
573
   * Four times the part of the result which only uses cross
574
   * derivatives:
575
   */
576
0
  const double newval3 = c00dxdy * quad_d2zdxdy00 + c10dxdy * quad_d2zdxdy10 +
577
0
    c01dxdy * quad_d2zdxdy01 + c11dxdy * quad_d2zdxdy11;
578
579
0
  const double newval = newval1 + .5 * newval2 + .25 * newval3;
580
581
0
  return newval;
582
0
}
583
584
/*
585
 * Call lbb with a type conversion operator as a parameter.
586
 *
587
 * It would be nice to do this with templates but we can't figure out
588
 * how to do it cleanly. Suggestions welcome!
589
 */
590
#define LBB_CONVERSION(conversion) \
591
  template <typename T> \
592
  static void inline lbb_##conversion(void *restrict pout, \
593
    const VipsPel *restrict pin, \
594
    const int bands, \
595
    const int lskip, \
596
    const double relative_x, \
597
    const double relative_y) \
598
0
  { \
599
0
    T *restrict out = (T *) pout; \
600
0
\
601
0
    const T *restrict in = (T *) pin; \
602
0
\
603
0
    const int one_shift = -bands; \
604
0
    const int thr_shift = bands; \
605
0
    const int fou_shift = 2 * bands; \
606
0
\
607
0
    const int uno_two_shift = -lskip; \
608
0
\
609
0
    const int tre_two_shift = lskip; \
610
0
    const int qua_two_shift = 2 * lskip; \
611
0
\
612
0
    const int uno_one_shift = uno_two_shift + one_shift; \
613
0
    const int dos_one_shift = one_shift; \
614
0
    const int tre_one_shift = tre_two_shift + one_shift; \
615
0
    const int qua_one_shift = qua_two_shift + one_shift; \
616
0
\
617
0
    const int uno_thr_shift = uno_two_shift + thr_shift; \
618
0
    const int dos_thr_shift = thr_shift; \
619
0
    const int tre_thr_shift = tre_two_shift + thr_shift; \
620
0
    const int qua_thr_shift = qua_two_shift + thr_shift; \
621
0
\
622
0
    const int uno_fou_shift = uno_two_shift + fou_shift; \
623
0
    const int dos_fou_shift = fou_shift; \
624
0
    const int tre_fou_shift = tre_two_shift + fou_shift; \
625
0
    const int qua_fou_shift = qua_two_shift + fou_shift; \
626
0
\
627
0
    const double xp1over2 = relative_x; \
628
0
    const double xm1over2 = xp1over2 - 1.0; \
629
0
    const double onepx = 0.5 + xp1over2; \
630
0
    const double onemx = 1.5 - xp1over2; \
631
0
    const double xp1over2sq = xp1over2 * xp1over2; \
632
0
\
633
0
    const double yp1over2 = relative_y; \
634
0
    const double ym1over2 = yp1over2 - 1.0; \
635
0
    const double onepy = 0.5 + yp1over2; \
636
0
    const double onemy = 1.5 - yp1over2; \
637
0
    const double yp1over2sq = yp1over2 * yp1over2; \
638
0
\
639
0
    const double xm1over2sq = xm1over2 * xm1over2; \
640
0
    const double ym1over2sq = ym1over2 * ym1over2; \
641
0
\
642
0
    const double twice1px = onepx + onepx; \
643
0
    const double twice1py = onepy + onepy; \
644
0
    const double twice1mx = onemx + onemx; \
645
0
    const double twice1my = onemy + onemy; \
646
0
\
647
0
    const double xm1over2sq_times_ym1over2sq = xm1over2sq * ym1over2sq; \
648
0
    const double xp1over2sq_times_ym1over2sq = xp1over2sq * ym1over2sq; \
649
0
    const double xp1over2sq_times_yp1over2sq = xp1over2sq * yp1over2sq; \
650
0
    const double xm1over2sq_times_yp1over2sq = xm1over2sq * yp1over2sq; \
651
0
\
652
0
    const double four_times_1px_times_1py = twice1px * twice1py; \
653
0
    const double four_times_1mx_times_1py = twice1mx * twice1py; \
654
0
    const double twice_xp1over2_times_1py = xp1over2 * twice1py; \
655
0
    const double twice_xm1over2_times_1py = xm1over2 * twice1py; \
656
0
\
657
0
    const double twice_xm1over2_times_1my = xm1over2 * twice1my; \
658
0
    const double twice_xp1over2_times_1my = xp1over2 * twice1my; \
659
0
    const double four_times_1mx_times_1my = twice1mx * twice1my; \
660
0
    const double four_times_1px_times_1my = twice1px * twice1my; \
661
0
\
662
0
    const double twice_1px_times_ym1over2 = twice1px * ym1over2; \
663
0
    const double twice_1mx_times_ym1over2 = twice1mx * ym1over2; \
664
0
    const double xp1over2_times_ym1over2 = xp1over2 * ym1over2; \
665
0
    const double xm1over2_times_ym1over2 = xm1over2 * ym1over2; \
666
0
\
667
0
    const double xm1over2_times_yp1over2 = xm1over2 * yp1over2; \
668
0
    const double xp1over2_times_yp1over2 = xp1over2 * yp1over2; \
669
0
    const double twice_1mx_times_yp1over2 = twice1mx * yp1over2; \
670
0
    const double twice_1px_times_yp1over2 = twice1px * yp1over2; \
671
0
\
672
0
    const double c00 = \
673
0
      four_times_1px_times_1py * xm1over2sq_times_ym1over2sq; \
674
0
    const double c00dx = \
675
0
      twice_xp1over2_times_1py * xm1over2sq_times_ym1over2sq; \
676
0
    const double c00dy = \
677
0
      twice_1px_times_yp1over2 * xm1over2sq_times_ym1over2sq; \
678
0
    const double c00dxdy = \
679
0
      xp1over2_times_yp1over2 * xm1over2sq_times_ym1over2sq; \
680
0
\
681
0
    const double c10 = \
682
0
      four_times_1mx_times_1py * xp1over2sq_times_ym1over2sq; \
683
0
    const double c10dx = \
684
0
      twice_xm1over2_times_1py * xp1over2sq_times_ym1over2sq; \
685
0
    const double c10dy = \
686
0
      twice_1mx_times_yp1over2 * xp1over2sq_times_ym1over2sq; \
687
0
    const double c10dxdy = \
688
0
      xm1over2_times_yp1over2 * xp1over2sq_times_ym1over2sq; \
689
0
\
690
0
    const double c01 = \
691
0
      four_times_1px_times_1my * xm1over2sq_times_yp1over2sq; \
692
0
    const double c01dx = \
693
0
      twice_xp1over2_times_1my * xm1over2sq_times_yp1over2sq; \
694
0
    const double c01dy = \
695
0
      twice_1px_times_ym1over2 * xm1over2sq_times_yp1over2sq; \
696
0
    const double c01dxdy = \
697
0
      xp1over2_times_ym1over2 * xm1over2sq_times_yp1over2sq; \
698
0
\
699
0
    const double c11 = \
700
0
      four_times_1mx_times_1my * xp1over2sq_times_yp1over2sq; \
701
0
    const double c11dx = \
702
0
      twice_xm1over2_times_1my * xp1over2sq_times_yp1over2sq; \
703
0
    const double c11dy = \
704
0
      twice_1mx_times_ym1over2 * xp1over2sq_times_yp1over2sq; \
705
0
    const double c11dxdy = \
706
0
      xm1over2_times_ym1over2 * xp1over2sq_times_yp1over2sq; \
707
0
\
708
0
    int band = bands; \
709
0
\
710
0
    do { \
711
0
      const double double_result = \
712
0
        lbbicubic(c00, \
713
0
          c10, \
714
0
          c01, \
715
0
          c11, \
716
0
          c00dx, \
717
0
          c10dx, \
718
0
          c01dx, \
719
0
          c11dx, \
720
0
          c00dy, \
721
0
          c10dy, \
722
0
          c01dy, \
723
0
          c11dy, \
724
0
          c00dxdy, \
725
0
          c10dxdy, \
726
0
          c01dxdy, \
727
0
          c11dxdy, \
728
0
          in[uno_one_shift], \
729
0
          in[uno_two_shift], \
730
0
          in[uno_thr_shift], \
731
0
          in[uno_fou_shift], \
732
0
          in[dos_one_shift], \
733
0
          in[0], \
734
0
          in[dos_thr_shift], \
735
0
          in[dos_fou_shift], \
736
0
          in[tre_one_shift], \
737
0
          in[tre_two_shift], \
738
0
          in[tre_thr_shift], \
739
0
          in[tre_fou_shift], \
740
0
          in[qua_one_shift], \
741
0
          in[qua_two_shift], \
742
0
          in[qua_thr_shift], \
743
0
          in[qua_fou_shift]); \
744
0
\
745
0
      const T result = to_##conversion<T>(double_result); \
746
0
      in++; \
747
0
      *out++ = result; \
748
0
    } while (--band); \
749
0
  }
Unexecuted instantiation: lbb.cpp:void lbb_nosign<unsigned char>(void*, unsigned char const*, int, int, double, double)
Unexecuted instantiation: lbb.cpp:void lbb_withsign<signed char>(void*, unsigned char const*, int, int, double, double)
Unexecuted instantiation: lbb.cpp:void lbb_nosign<unsigned short>(void*, unsigned char const*, int, int, double, double)
Unexecuted instantiation: lbb.cpp:void lbb_withsign<short>(void*, unsigned char const*, int, int, double, double)
Unexecuted instantiation: lbb.cpp:void lbb_nosign<unsigned int>(void*, unsigned char const*, int, int, double, double)
Unexecuted instantiation: lbb.cpp:void lbb_withsign<int>(void*, unsigned char const*, int, int, double, double)
Unexecuted instantiation: lbb.cpp:void lbb_fptypes<float>(void*, unsigned char const*, int, int, double, double)
Unexecuted instantiation: lbb.cpp:void lbb_fptypes<double>(void*, unsigned char const*, int, int, double, double)
750
751
LBB_CONVERSION(fptypes)
752
LBB_CONVERSION(withsign)
753
LBB_CONVERSION(nosign)
754
755
#define CALL(T, conversion) \
756
0
  lbb_##conversion<T>(out, \
757
0
    p, \
758
0
    bands, \
759
0
    lskip, \
760
0
    relative_x, \
761
0
    relative_y);
762
763
/*
764
 * We need C linkage:
765
 */
766
extern "C" {
767
2
G_DEFINE_TYPE(VipsInterpolateLbb, vips_interpolate_lbb,
768
2
  VIPS_TYPE_INTERPOLATE);
769
2
}
770
2
771
2
static void
772
2
vips_interpolate_lbb_interpolate(VipsInterpolate *restrict interpolate,
773
2
  void *restrict out,
774
2
  VipsRegion *restrict in,
775
2
  double absolute_x,
776
2
  double absolute_y)
777
2
{
778
  /* absolute_x and absolute_y are always >= 1.0 (see double-check assert
779
   * below), so we don't need floor().
780
   *
781
   * It's 1 not 0 since have a window_offset of 1.
782
   */
783
0
  const int ix = (int) absolute_x;
784
0
  const int iy = (int) absolute_y;
785
786
  /*
787
   * Move the pointer to (the first band of) the top/left pixel of the
788
   * 2x2 group of pixel centers which contains the sampling location
789
   * in its convex hull:
790
   */
791
0
  const VipsPel *restrict p = VIPS_REGION_ADDR(in, ix, iy);
792
793
0
  const double relative_x = absolute_x - ix;
794
0
  const double relative_y = absolute_y - iy;
795
796
  /*
797
   * VIPS versions of Nicolas's pixel addressing values.
798
   */
799
0
  const int lskip = VIPS_REGION_LSKIP(in) /
800
0
    VIPS_IMAGE_SIZEOF_ELEMENT(in->im);
801
  /*
802
   * Double the bands for complex images to account for the real and
803
   * imaginary parts being computed independently:
804
   */
805
0
  const int actual_bands = in->im->Bands;
806
0
  const int bands = vips_band_format_iscomplex(in->im->BandFmt)
807
0
    ? 2 * actual_bands
808
0
    : actual_bands;
809
810
0
  g_assert(ix - 1 >= in->valid.left);
811
0
  g_assert(iy - 1 >= in->valid.top);
812
0
  g_assert(ix + 2 < VIPS_RECT_RIGHT(&in->valid));
813
0
  g_assert(iy + 2 < VIPS_RECT_BOTTOM(&in->valid));
814
815
  /* Confirm that absolute_x and absolute_y are >= 1, see above.
816
   */
817
0
  g_assert(absolute_x >= 1.0);
818
0
  g_assert(absolute_y >= 1.0);
819
820
0
  switch (in->im->BandFmt) {
821
0
  case VIPS_FORMAT_UCHAR:
822
0
    CALL(unsigned char, nosign);
823
0
    break;
824
825
0
  case VIPS_FORMAT_CHAR:
826
0
    CALL(signed char, withsign);
827
0
    break;
828
829
0
  case VIPS_FORMAT_USHORT:
830
0
    CALL(unsigned short, nosign);
831
0
    break;
832
833
0
  case VIPS_FORMAT_SHORT:
834
0
    CALL(signed short, withsign);
835
0
    break;
836
837
0
  case VIPS_FORMAT_UINT:
838
0
    CALL(unsigned int, nosign);
839
0
    break;
840
841
0
  case VIPS_FORMAT_INT:
842
0
    CALL(signed int, withsign);
843
0
    break;
844
845
  /*
846
   * Complex images are handled by doubling of bands.
847
   */
848
0
  case VIPS_FORMAT_FLOAT:
849
0
  case VIPS_FORMAT_COMPLEX:
850
0
    CALL(float, fptypes);
851
0
    break;
852
853
0
  case VIPS_FORMAT_DOUBLE:
854
0
  case VIPS_FORMAT_DPCOMPLEX:
855
0
    CALL(double, fptypes);
856
0
    break;
857
858
0
  default:
859
0
    g_assert(0);
860
0
    break;
861
0
  }
862
0
}
863
864
static void
865
vips_interpolate_lbb_class_init(VipsInterpolateLbbClass *klass)
866
1
{
867
1
  VipsObjectClass *object_class = VIPS_OBJECT_CLASS(klass);
868
1
  VipsInterpolateClass *interpolate_class =
869
1
    VIPS_INTERPOLATE_CLASS(klass);
870
871
1
  object_class->nickname = "lbb";
872
1
  object_class->description = _("reduced halo bicubic");
873
874
1
  interpolate_class->interpolate = vips_interpolate_lbb_interpolate;
875
1
  interpolate_class->window_size = 4;
876
1
}
877
878
static void
879
vips_interpolate_lbb_init(VipsInterpolateLbb *lbb)
880
0
{
881
0
}