<commit_before>/* lbb (locally bounded bicubic) resampler
 */

/*

    This file is part of VIPS.

    VIPS is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this program; if not, write to the Free
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA

 */

/*

    These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

/*
 * 2010 (c) <NAME>, <NAME>, <NAME>.
 *
 * <NAME> thanks <NAME>, <NAME>, <NAME>,
 * <NAME>, <NAME>, <NAME>, and <NAME> for
 * useful comments and code.
 */

/*
 * LBB (Locally Bounded Bicubic) is a high quality nonlinear variant
 * of Catmull-Rom. Compared to Catmull-Rom, it produces resampled
 * images with much reduced halos, both in terms of physical extent
 * and over/undershoot amplitude. This is accomplished without a
 * significant change in the smoothness of the result.
 *
 * Another important property is that the resampled values are
 * contained within the range of nearby input values. Consequently, no
 * clamping is needed to stay "in range."
 *
 * LBB was developed by <NAME> and <NAME> of the
 * Department of Mathematics and Computer Science of Laurentian
 * University.
 */

/*
 * LBB is a novel method with the following properties:
 *
 * --LBB is a Hermite bicubic method: The bicubic surface is defined,
 *   one convex hull of four nearby input points at a time, using four
 *   point values, four x-derivatives, four y-derivatives, and four
 *   cross-derivatives.
 *
 * --The stencil for values in a square patch is the usual 4x4.
 *
 * --LBB is interpolatory.
 *
 * --It is C^1 with continuous cross derivatives.
 *
 * --When the limiters are inactive, LBB gives the same results as
 *   Catmull-Rom.
 *
 * --When used on binary images, LBB gives results similar to bicubic
 *   Hermite with all first derivatives---but not necessarily the
 *   cross derivatives--at the input pixel locations set to zero.
 *
 * --The LBB reconstruction is locally bounded: Over each square
 *   patch, the surface is contained between the minimum and the
 *   maximum values among the 16 nearest input pixel values (those in
 *   the stencil).
 *
 * --Consequently, the LBB reconstruction is globally bounded between
 *   the very smallest input pixel value and the very largest input
 *   pixel value. (It is not necessary to clamp results.)
 *
 * The LBB method is based on the method of <NAME>, Petros
 * Mashwama and <NAME> for constraining Hermite interpolants
 * between globally defined planes:
 *
 *   Visualization of surface data to preserve positivity and other
 *   simple constraints. Computer & Graphics, Vol. 19, Number 4, pages
 *   585-594, 1995. DOI: 10.1016/0097-8493(95)00036-C.
 *
 * Instead of forcing the reconstructed surface to lie between two
 * GLOBALLY defined planes, LBB constrains one patch at a time to lie
 * between LOCALLY defined planes. This is accomplished by
 * constraining the derivatives (x, y and cross) at each input pixel
 * location so that if the constraint was applied everywhere the
 * surface would fit between the min and max of the values at the 9
 * closest pixel locations. Because this is done with each of the four
 * pixel locations which define the bicubic patch, this forces the
 * reconstructed surface to lie between the min and max of the values
 * at the 16 closest values pixel locations. (Each corner defines its
 * own 3x3 subgroup of the 4x4 stencil. Consequently, the surface is
 * necessarily above the minimum of the four minima, which happens to
 * be the minimum over the 4x4. Similarly with the maxima.)
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>

#include <stdio.h>
#include <stdlib.h>

#include <vips/vips.h>
#include <vips/internal.h>

#include "templates.h"

#define VIPS_TYPE_INTERPOLATE_LBB \
	(vips_interpolate_lbb_get_type())
#define VIPS_INTERPOLATE_LBB( obj ) \
	(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
	VIPS_TYPE_INTERPOLATE_LBB, VipsInterpolateLbb ))
#define VIPS_INTERPOLATE_LBB_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_CAST( (klass), \
	VIPS_TYPE_INTERPOLATE_LBB, VipsInterpolateLbbClass))
#define VIPS_IS_INTERPOLATE_LBB( obj ) \
	(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_INTERPOLATE_LBB ))
#define VIPS_IS_INTERPOLATE_LBB_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_INTERPOLATE_LBB ))
#define VIPS_INTERPOLATE_LBB_GET_CLASS( obj ) \
	(G_TYPE_INSTANCE_GET_CLASS( (obj), \
	VIPS_TYPE_INTERPOLATE_LBB, VipsInterpolateLbbClass ))

typedef struct _VipsInterpolateLbb {
	VipsInterpolate parent_object;

} VipsInterpolateLbb;

typedef struct _VipsInterpolateLbbClass {
	VipsInterpolateClass parent_class;

} VipsInterpolateLbbClass;

#define LBB_ABS(x)  ( ((x)>=0.) ? (x) : -(x) )
#define LBB_SIGN(x) ( ((x)>=0.) ? 1.0 : -1.0 )
/*
 * MIN and MAX macros set up so that I can put the likely winner in
 * the first argument (forward branch likely blah blah blah):
 */
#define LBB_MIN(x,y) ( ((x)<=(y)) ? (x) : (y) )
#define LBB_MAX(x,y) ( ((x)>=(y)) ? (x) : (y) )

static inline double
lbbicubic( const double c00,
           const double c10,
           const double c01,
           const double c11,
           const double c00dx,
           const double c10dx,
           const double c01dx,
           const double c11dx,
           const double c00dy,
           const double c10dy,
           const double c01dy,
           const double c11dy,
           const double c00dxdy,
           const double c10dxdy,
           const double c01dxdy,
           const double c11dxdy,
           const double uno_one,
           const double uno_two,
           const double uno_thr,
           const double uno_fou,
           const double dos_one,
           const double dos_two,
           const double dos_thr,
           const double dos_fou,
           const double tre_one,
           const double tre_two,
           const double tre_thr,
           const double tre_fou,
           const double qua_one,
           const double qua_two,
           const double qua_thr,
           const double qua_fou )
{
  /*
   * STENCIL (FOOTPRINT) OF INPUT VALUES:
   *
   * The stencil of LBB is the same as for any standard Hermite
   * bicubic (e.g., Catmull-Rom):
   *
   *  (ix-1,iy-1)  (ix,iy-1)    (ix+1,iy-1)  (ix+2,iy-1)
   *  = uno_one    = uno_two    = uno_thr    = uno_fou
   *
   *  (ix-1,iy)    (ix,iy)      (ix+1,iy)    (ix+2,iy)
   *  = dos_one    = dos_two    = dos_thr    = dos_fou
   *                        X
   *  (ix-1,iy+1)  (ix,iy+1)    (ix+1,iy+1)  (ix+2,iy+1)
   *  = tre_one    = tre_two    = tre_thr    = tre_fou
   *
   *  (ix-1,iy+2)  (ix,iy+2)    (ix+1,iy+2)  (ix+2,iy+2)
   *  = qua_one    = qua_two    = qua_thr    = qua_fou
   *
   * where ix is the (pseudo-)floor of the requested left-to-right
   * location ("X"), and iy is the floor of the requested up-to-down
   * location.
   */

  /*
   * Computation of the four min and four max over 3x3 input data
   * sub-blocks of the 4x4 input stencil (involves 28 flag
   * computations):
   */
  const double m1    = (dos_two <= dos_thr) ? dos_two : dos_thr;
  const double M1    = (dos_two <= dos_thr) ? dos_thr : dos_two;
  const double m2    = (tre_two <= tre_thr) ? tre_two : tre_thr;
  const double M2    = (tre_two <= tre_thr) ? tre_thr : tre_two;
  const double m3    = (uno_two <= uno_thr) ? uno_two : uno_thr;
  const double M3    = (uno_two <= uno_thr) ? uno_thr : uno_two;
  const double m4    = (qua_two <= qua_thr) ? qua_two : qua_thr;
  const double M4    = (qua_two <= qua_thr) ? qua_thr : qua_two;
  const double m5    = LBB_MIN( m1, m2 );
  const double M5    = LBB_MAX( M1, M2 );
  const double m6    = (dos_one <= tre_one) ? dos_one : tre_one;
  const double M6    = (dos_one <= tre_one) ? tre_one : dos_one;
  const double m7    = (dos_fou <= tre_fou) ? dos_fou : tre_fou;
  const double M7    = (dos_fou <= tre_fou) ? tre_fou : dos_fou;
  const double m8    = LBB_MIN( m5, m3 );
  const double M8    = LBB_MAX( M5, M3 );
  const double m9    = LBB_MIN( m5, m4 );
  const double M9    = LBB_MAX( M5, M4 );
  const double m10   = LBB_MIN( m6, uno_one );
  const double M10   = LBB_MAX( M6, uno_one );
  const double m11   = LBB_MIN( m7, uno_fou );
  const double M11   = LBB_MAX( M7, uno_fou );
  const double m12   = LBB_MIN( m6, qua_one );
  const double M12   = LBB_MAX( M6, qua_one );
  const double m13   = LBB_MIN( m7, qua_fou );
  const double M13   = LBB_MAX( M7, qua_fou );
  const double min00 = LBB_MIN( m8, m10 );
  const double max00 = LBB_MAX( M8, M10 );
  const double min10 = LBB_MIN( m8, m11 );
  const double max10 = LBB_MAX( M8, M11 );
  const double min01 = LBB_MIN( m9, m12 );
  const double max01 = LBB_MAX( M9, M12 );
  const double min11 = LBB_MIN( m9, m13 );
  const double max11 = LBB_MAX( M9, M13 );

  /*
   * Distances to the local min and max:
   */
  const double u00 = dos_two - min00;
  const double v00 = max00 - dos_two;<commit_msg>lbb resampler comments<commit_after>/* lbb (locally bounded bicubic) resampler
 */

/*

    This file is part of VIPS.

    VIPS is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this program; if not, write to the Free
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA

 */

/*

    These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

/*
 * 2010 (c) <NAME>, <NAME>, <NAME>.
 *
 * <NAME> thanks <NAME>, <NAME>, <NAME>,
 * <NAME>, <NAME>, <NAME>, and <NAME> for
 * useful comments and code.
 */

/*
 * LBB (Locally Bounded Bicubic) is a high quality nonlinear variant
 * of Catmull-Rom. Compared to Catmull-Rom, it produces resampled
 * images with much reduced halos, both in terms of physical extent
 * and over/undershoot amplitude. This is accomplished without a
 * significant change in the smoothness of the result.
 *
 * Another important property is that the resampled values are
 * contained within the range of nearby input values. Consequently, no
 * clamping is needed to stay "in range."
 *
 * LBB was developed by <NAME> and <NAME> of the
 * Department of Mathematics and Computer Science of Laurentian
 * University.
 */

/*
 * LBB is a novel method with the following properties:
 *
 * --LBB is a Hermite bicubic method: The bicubic surface is defined,
 *   one convex hull of four nearby input points at a time, using four
 *   point values, four x-derivatives, four y-derivatives, and four
 *   cross-derivatives.
 *
 * --The stencil for values in a square patch is the usual 4x4.
 *
 * --LBB is interpolatory.
 *
 * --It is C^1 with continuous cross derivatives.
 *
 * --When the limiters are inactive, LBB gives the same results as
 *   Catmull-Rom.
 *
 * --When used on binary images, LBB gives results similar to bicubic
 *   Hermite with all first derivatives---but not necessarily the
 *   cross derivatives--at the input pixel locations set to zero.
 *
 * --The LBB reconstruction is locally bounded: Over each square
 *   patch, the surface is contained between the minimum and the
 *   maximum values among the 16 nearest input pixel values (those in
 *   the stencil).
 *
 * --Consequently, the LBB reconstruction is globally bounded between
 *   the very smallest input pixel value and the very largest input
 *   pixel value. (It is not necessary to clamp results.)
 *
 * The LBB method is based on the method of <NAME>, Petros
 * Mashwama and <NAME> for constraining Hermite interpolants
 * between globally defined planes:
 *
 *   Visualization of surface data to preserve positivity and other
 *   simple constraints. Computer & Graphics, Vol. 19, Number 4, pages
 *   585-594, 1995. DOI: 10.1016/0097-8493(95)00036-C.
 *
 * Instead of forcing the reconstructed surface to lie between two
 * GLOBALLY defined planes, LBB constrains one patch at a time to lie
 * between LOCALLY defined planes. This is accomplished by
 * constraining the derivatives (x, y and cross) at each input pixel
 * location so that if the constraint was applied everywhere the
 * surface would fit between the min and max of the values at the 9
 * closest pixel locations. Because this is done with each of the four
 * pixel locations which define the bicubic patch, this forces the
 * reconstructed surface to lie between the min and max of the values
 * at the 16 closest values pixel locations. (Each corner defines its
 * own 3x3 subgroup of the 4x4 stencil. Consequently, the surface is
 * necessarily above the minimum of the four minima, which happens to
 * be the minimum over the 4x4. Similarly with the maxima.)
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>

#include <stdio.h>
#include <stdlib.h>

#include <vips/vips.h>
#include <vips/internal.h>

#include "templates.h"

#define VIPS_TYPE_INTERPOLATE_LBB \
	(vips_interpolate_lbb_get_type())
#define VIPS_INTERPOLATE_LBB( obj ) \
	(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
	VIPS_TYPE_INTERPOLATE_LBB, VipsInterpolateLbb ))
#define VIPS_INTERPOLATE_LBB_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_CAST( (klass), \
	VIPS_TYPE_INTERPOLATE_LBB, VipsInterpolateLbbClass))
#define VIPS_IS_INTERPOLATE_LBB( obj ) \
	(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_INTERPOLATE_LBB ))
#define VIPS_IS_INTERPOLATE_LBB_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_INTERPOLATE_LBB ))
#define VIPS_INTERPOLATE_LBB_GET_CLASS( obj ) \
	(G_TYPE_INSTANCE_GET_CLASS( (obj), \
	VIPS_TYPE_INTERPOLATE_LBB, VipsInterpolateLbbClass ))

typedef struct _VipsInterpolateLbb {
	VipsInterpolate parent_object;

} VipsInterpolateLbb;

typedef struct _VipsInterpolateLbbClass {
	VipsInterpolateClass parent_class;

} VipsInterpolateLbbClass;

#define LBB_ABS(x)  ( ((x)>=0.) ? (x) : -(x) )
#define LBB_SIGN(x) ( ((x)>=0.) ? 1.0 : -1.0 )
/*
 * MIN and MAX macros set up so that I can put the likely winner in
 * the first argument (forward branch likely blah blah blah):
 */
#define LBB_MIN(x,y) ( ((x)<=(y)) ? (x) : (y) )
#define LBB_MAX(x,y) ( ((x)>=(y)) ? (x) : (y) )

static inline double
lbbicubic( const double c00,
           const double c10,
           const double c01,
           const double c11,
           const double c00dx,
           const double c10dx,
           const double c01dx,
           const double c11dx,
           const double c00dy,
           const double c10dy,
           const double c01dy,
           const double c11dy,
           const double c00dxdy,
           const double c10dxdy,
           const double c01dxdy,
           const double c11dxdy,
           const double uno_one,
           const double uno_two,
           const double uno_thr,
           const double uno_fou,
           const double dos_one,
           const double dos_two,
           const double dos_thr,
           const double dos_fou,
           const double tre_one,
           const double tre_two,
           const double tre_thr,
           const double tre_fou,
           const double qua_one,
           const double qua_two,
           const double qua_thr,
           const double qua_fou )
{
  /*
   * STENCIL (FOOTPRINT) OF INPUT VALUES:
   *
   * The stencil of LBB is the same as for any standard Hermite
   * bicubic (e.g., Catmull-Rom):
   *
   *  (ix-1,iy-1)  (ix,iy-1)    (ix+1,iy-1)  (ix+2,iy-1)
   *  = uno_one    = uno_two    = uno_thr    = uno_fou
   *
   *  (ix-1,iy)    (ix,iy)      (ix+1,iy)    (ix+2,iy)
   *  = dos_one    = dos_two    = dos_thr    = dos_fou
   *                        X
   *  (ix-1,iy+1)  (ix,iy+1)    (ix+1,iy+1)  (ix+2,iy+1)
   *  = tre_one    = tre_two    = tre_thr    = tre_fou
   *
   *  (ix-1,iy+2)  (ix,iy+2)    (ix+1,iy+2)  (ix+2,iy+2)
   *  = qua_one    = qua_two    = qua_thr    = qua_fou
   *
   * where ix is the (pseudo-)floor of the requested left-to-right
   * location ("X"), and iy is the floor of the requested up-to-down
   * location.
   */

  /*
   * Computation of the four min and four max over 3x3 input data
   * sub-blocks of the 4x4 input stencil (involves 28 flag
   * computations if done with conditional moves):
   */
  const double m1    = (dos_two <= dos_thr) ? dos_two : dos_thr;
  const double M1    = (dos_two <= dos_thr) ? dos_thr : dos_two;
  const double m2    = (tre_two <= tre_thr) ? tre_two : tre_thr;
  const double M2    = (tre_two <= tre_thr) ? tre_thr : tre_two;
  const double m3    = (uno_two <= uno_thr) ? uno_two : uno_thr;
  const double M3    = (uno_two <= uno_thr) ? uno_thr : uno_two;
  const double m4    = (qua_two <= qua_thr) ? qua_two : qua_thr;
  const double M4    = (qua_two <= qua_thr) ? qua_thr : qua_two;
  const double m5    = LBB_MIN( m1, m2 );
  const double M5    = LBB_MAX( M1, M2 );
  const double m6    = (dos_one <= tre_one) ? dos_one : tre_one;
  const double M6    = (dos_one <= tre_one) ? tre_one : dos_one;
  const double m7    = (dos_fou <= tre_fou) ? dos_fou : tre_fou;
  const double M7    = (dos_fou <= tre_fou) ? tre_fou : dos_fou;
  const double m8    = LBB_MIN( m5, m3 );
  const double M8    = LBB_MAX( M5, M3 );
  const double m9    = LBB_MIN( m5, m4 );
  const double M9    = LBB_MAX( M5, M4 );
  const double m10   = LBB_MIN( m6, uno_one );
  const double M10   = LBB_MAX( M6, uno_one );
  const double m11   = LBB_MIN( m7, uno_fou );
  const double M11   = LBB_MAX( M7, uno_fou );
  const double m12   = LBB_MIN( m6, qua_one );
  const double M12   = LBB_MAX( M6, qua_one );
  const double m13   = LBB_MIN( m7, qua_fou );
  const double M13   = LBB_MAX( M7, qua_fou );
  const double min00 = LBB_MIN( m8, m10 );
  const double max00 = LBB_MAX( M8, M10 );
  const double min10 = LBB_MIN( m8, m11 );
  const double max10 = LBB_MAX( M8, M11 );
  const double min01 = LBB_MIN( m9, m12 );
  const double max01 = LBB_MAX( M9, M12 );
  const double min11 = LBB_MIN( m9, m13 );
  const double max11 = LBB_MAX( M9, M13 );
  /*
   * The remainder of the computation involves the computation of:
   *
   * --8 conditional moves,
   *
   * --8 signs (in which the sign of zero is unimportant),
   *
   * --12 minima of two values,
   *
   * --8 maxima of two values,
   *
   * --8 absolute values,
   *
   * for a grand total of 29 minima, 25 maxima, 8 conditional moves, 8
   * signs, and 8 absolute values. (If everything is done with
   * conditional moves, "only" 28+8+8+12+8+8=72 flags are involved.)
   *
   * This part of the computation also involves 109 arithmetic (*,+,-)
   * arithmetic operations (to be double checked).
   */

  /*
   * Distances to the local min and max:
   */
  const double u00 = dos_two - min00;
  const double v00 = max00 - dos_two;
{'fraction_non_alphanumeric': 0.0918429929469488, 'fraction_numerical': 0.025554533374220586, 'mean_word_length': 2.922814755412991, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6505613', 'n_tokens_mistral': 6970, 'n_tokens_neox': 6614, 'n_words': 2350}