2009-03-18 01:41:57 +01:00
|
|
|
/* nohalo level 1 interpolator
|
2009-03-16 15:13:38 +01:00
|
|
|
*
|
|
|
|
* Hacked for vips by J. Cupitt, 20/1/09
|
2009-03-18 01:41:57 +01:00
|
|
|
* Tweaks by N. Robidoux and J. Cupitt 3/09
|
2009-03-16 15:13:38 +01:00
|
|
|
*
|
|
|
|
* 16/3/09
|
|
|
|
* - rename as nohalo1
|
|
|
|
* - move "restrict" support to configure
|
2009-01-20 22:32:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This file is part of VIPS.
|
2009-03-04 23:20:58 +01:00
|
|
|
|
2009-03-04 23:09:54 +01:00
|
|
|
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.
|
2009-01-20 22:32:05 +01:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2009-03-04 23:09:54 +01:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
2009-01-20 22:32:05 +01:00
|
|
|
|
2009-03-04 23:09:54 +01:00
|
|
|
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
|
2009-01-20 22:32:05 +01:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2009-03-04 23:20:58 +01:00
|
|
|
* 2009 (c) Nicolas Robidoux
|
2009-01-20 22:32:05 +01:00
|
|
|
*
|
2009-03-04 23:09:54 +01:00
|
|
|
* Nicolas thanks Geert Jordaens, John Cupitt, Minglun Gong, Øyvind
|
|
|
|
* Kolås and Sven Neumann for useful comments and code.
|
2009-03-04 23:20:58 +01:00
|
|
|
*
|
2009-03-04 23:09:54 +01:00
|
|
|
* Nicolas Robidoux's research on nohalo funded in part by an NSERC
|
|
|
|
* (National Science and Engineering Research Council of Canada)
|
|
|
|
* Discovery Grant.
|
2009-01-20 22:32:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ================
|
|
|
|
* NOHALO RESAMPLER
|
|
|
|
* ================
|
|
|
|
*
|
|
|
|
* "Nohalo" is a family of parameterized resamplers with a mission:
|
|
|
|
* smoothly straightening oblique lines without undesirable
|
2009-03-18 01:41:57 +01:00
|
|
|
* side-effects. In particular, without much blurring and with
|
|
|
|
* absolutely no added haloing.
|
2009-01-20 22:32:05 +01:00
|
|
|
*
|
|
|
|
* The key parameter, which may be described as a "quality" parameter,
|
|
|
|
* is an integer which specifies the number of "levels" of binary
|
2009-01-28 11:01:18 +01:00
|
|
|
* subdivision which are performed. level = 0 can be thought of as
|
|
|
|
* being plain vanilla bilinear resampling; level = 1 is then the
|
|
|
|
* first "non-classical" method of the familiy.
|
2009-01-20 22:32:05 +01:00
|
|
|
*
|
2009-01-28 11:01:18 +01:00
|
|
|
* Although it increases computational cost, additional levels
|
|
|
|
* increase the quality of the resampled pixel value unless the
|
|
|
|
* resampled location happens to be exactly where a subdivided grid
|
|
|
|
* point (for this level) is located, in which case further levels do
|
|
|
|
* not change the answer, and consequently do not increase its
|
|
|
|
* quality.
|
2009-01-20 22:32:05 +01:00
|
|
|
*
|
2009-03-04 23:09:54 +01:00
|
|
|
* ===================================================
|
|
|
|
* THIS CODE ONLY IMPLEMENTS THE LOWEST QUALITY NOHALO
|
|
|
|
* ===================================================
|
2009-03-04 23:20:58 +01:00
|
|
|
*
|
2009-01-20 22:32:05 +01:00
|
|
|
* This code implement nohalo for (quality) level = 1. Nohalo for
|
|
|
|
* higher quality levels will be implemented later.
|
|
|
|
*
|
|
|
|
* Key properties:
|
|
|
|
*
|
|
|
|
* =======================
|
|
|
|
* Nohalo is interpolatory
|
|
|
|
* =======================
|
|
|
|
*
|
|
|
|
* That is, nohalo preserves point values: If asked for the value at
|
|
|
|
* the center of an input pixel, the sampler returns the corresponding
|
|
|
|
* value, unchanged. In addition, because nohalo is continuous, if
|
|
|
|
* asked for a value at a location "very close" to the center of an
|
|
|
|
* input pixel, then the sampler returns a value "very close" to
|
|
|
|
* it. (Nohalo is not smoothing like, say, B-Spline
|
|
|
|
* pseudo-interpolation.)
|
|
|
|
*
|
|
|
|
* ========================================================
|
|
|
|
* Nohalo is co-monotone (this is why it's called "nohalo")
|
|
|
|
* ========================================================
|
|
|
|
*
|
2009-03-18 01:41:57 +01:00
|
|
|
* What monotonicity more or less means here is that the resampled
|
|
|
|
* value is in the range of the four closest input values. This
|
|
|
|
* property is why there is no added haloing. It also implies that
|
|
|
|
* clamping is unnecessary (provided abyss values are within the range
|
|
|
|
* of acceptable values, which is always the case). (Note: plain
|
|
|
|
* vanilla bilinear is also co-monotone.)
|
2009-01-20 22:32:05 +01:00
|
|
|
*
|
|
|
|
* Note: If the abyss policy is an extrapolating one---for example,
|
|
|
|
* linear or bilinear extrapolation---clamping is still unnecessary
|
|
|
|
* unless one attempts to resample outside of the convex hull of the
|
|
|
|
* input pixel positions. Consequence: the "corner" image size
|
|
|
|
* convention does not require clamping when using linear
|
|
|
|
* extrapolation abyss policy when performing image resizing, but the
|
|
|
|
* "center" one does, when upscaling, at locations very close to the
|
|
|
|
* boundary. If computing values at locations outside of the convex
|
|
|
|
* hull of the pixel locations of the input image, nearest neighbour
|
|
|
|
* abyss policy is most likely better anyway, because linear
|
|
|
|
* extrapolation produces "streaks" if positions far outside the
|
|
|
|
* original image boundary are resampled.
|
|
|
|
*
|
|
|
|
* ========================
|
|
|
|
* Nohalo is a local method
|
|
|
|
* ========================
|
|
|
|
*
|
|
|
|
* The value of the reconstructed intensity surface at any point
|
|
|
|
* depends on the values of (at most) 12 nearby input values, located
|
2009-03-04 23:09:54 +01:00
|
|
|
* in a "cross" centered at the closest four input pixel centers.
|
2009-01-20 22:32:05 +01:00
|
|
|
*
|
|
|
|
* ===========================================================
|
|
|
|
* When level = infinity, nohalo's intensity surface is smooth
|
|
|
|
* ===========================================================
|
|
|
|
*
|
|
|
|
* It is conjectured that the intensity surface is infinitely
|
|
|
|
* differentiable. Consequently, "Mach banding" (primarily caused by
|
|
|
|
* sharp "ridges" in the reconstructed intensity surface and
|
|
|
|
* particularly noticeable, for example, when using bilinear
|
|
|
|
* resampling) is (essentially) absent, even at high magnifications,
|
|
|
|
* WHEN THE LEVEL IS HIGH (more or less when 2^(level+1) is at least
|
|
|
|
* the largest local magnification factor, which means that the level
|
|
|
|
* 1 nohalo does not show much Mach banding up to a magnification of
|
|
|
|
* about 4).
|
|
|
|
*
|
|
|
|
* ===============================
|
|
|
|
* Nohalo is second order accurate
|
|
|
|
* ===============================
|
|
|
|
*
|
|
|
|
* (Except possibly near the boundary: it is easy to make this
|
|
|
|
* property carry over everywhere but this requires a tuned abyss
|
2009-03-04 23:09:54 +01:00
|
|
|
* policy---linear extrapolation, say---or building the boundary
|
|
|
|
* conditions inside the sampler.) Nohalo is exact on linear
|
|
|
|
* intensity profiles, meaning that if the input pixel values (in the
|
|
|
|
* stencil) are obtained from a function of the form f(x,y) = a + b*x
|
|
|
|
* + c*y (a, b, c constants), then the computed pixel value is exactly
|
|
|
|
* the value of f(x,y) at the asked-for sampling location. The
|
|
|
|
* boundary condition which is emulated by VIPS throught the "extend"
|
|
|
|
* extension of the input image---this corresponds to the nearest
|
|
|
|
* neighbour abyss policy---does NOT make this resampler exact on
|
|
|
|
* linears at the boundary. It does, however, guarantee that no
|
|
|
|
* clamping is required even when resampled values are computed at
|
|
|
|
* positions outside of the extent of the input image (when
|
|
|
|
* extrapolation is required).
|
2009-01-20 22:32:05 +01:00
|
|
|
*
|
|
|
|
* ===================
|
|
|
|
* Nohalo is nonlinear
|
|
|
|
* ===================
|
|
|
|
*
|
|
|
|
* In particular, resampling a sum of images may not be the same as
|
2009-03-04 23:09:54 +01:00
|
|
|
* summing the resamples. (This occurs even without taking into account
|
2009-01-20 22:32:05 +01:00
|
|
|
* over and underflow issues: images can only take values within a
|
|
|
|
* banded range, and consequently no sampler is truly linear.)
|
|
|
|
*
|
|
|
|
* ====================
|
|
|
|
* Weaknesses of nohalo
|
|
|
|
* ====================
|
|
|
|
*
|
|
|
|
* In some cases, the first level nonlinear computation is wasted:
|
|
|
|
*
|
|
|
|
* If a region is bichromatic, the nonlinear component of the level 1
|
|
|
|
* nohalo is zero in the interior of the region, and consequently
|
|
|
|
* nohalo boils down to bilinear. For such images, either stick to
|
|
|
|
* bilinear, or use a higher level (quality) setting. (There is no
|
|
|
|
* real harm in using nohalo when it boils down to bilinear if one
|
|
|
|
* does not mind wasting cycles.)
|
|
|
|
*
|
|
|
|
* Low quality levels do NOT produce a continuously differentiable
|
|
|
|
* intensity surface:
|
|
|
|
*
|
|
|
|
* With a "finite" level is used (that is, in practice), the nohalo
|
|
|
|
* intensity surface is only continuous: there are gradient
|
|
|
|
* discontinuities because the "final interpolation step" is performed
|
|
|
|
* with bilinear. (Exception: if the "corner" image size convention is
|
|
|
|
* used and the magnification factor is 2, that is, if the resampled
|
|
|
|
* points sit exactly on the binary subdivided grid, then nohalo level
|
|
|
|
* 1 gives the same result as as level=infinity, and consequently the
|
|
|
|
* intensity surface can be treated as if smooth.)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FAST_PSEUDO_FLOOR is a floor and floorf replacement which has been
|
|
|
|
* found to be faster on several linux boxes than the library
|
|
|
|
* version. It returns the floor of its argument unless the argument
|
|
|
|
* is a negative integer, in which case it returns one less than the
|
|
|
|
* floor. For example:
|
|
|
|
*
|
|
|
|
* FAST_PSEUDO_FLOOR(0.5) = 0
|
|
|
|
*
|
2009-03-15 20:55:59 +01:00
|
|
|
* FAST_PSEUDO_FLOOR(0.) = 0
|
2009-01-20 22:32:05 +01:00
|
|
|
*
|
|
|
|
* FAST_PSEUDO_FLOOR(-.5) = -1
|
|
|
|
*
|
|
|
|
* as expected, but
|
|
|
|
*
|
2009-03-15 20:55:59 +01:00
|
|
|
* FAST_PSEUDO_FLOOR(-1.) = -2
|
2009-01-20 22:32:05 +01:00
|
|
|
*
|
|
|
|
* The locations of the discontinuities of FAST_PSEUDO_FLOOR are the
|
|
|
|
* same as floor and floorf; it is just that at negative integers the
|
|
|
|
* function is discontinuous on the right instead of the left.
|
|
|
|
*/
|
|
|
|
#define FAST_PSEUDO_FLOOR(x) ( (int)(x) - ( (x) < 0. ) )
|
2009-03-15 03:30:57 +01:00
|
|
|
|
2009-01-20 22:32:05 +01:00
|
|
|
/*
|
2009-03-15 03:30:57 +01:00
|
|
|
* FAST_MINMOD is an implementation of the minmod function which only
|
2009-03-16 10:33:10 +01:00
|
|
|
* needs two conditional moves. (Nicolas: I think that this may be
|
|
|
|
* the very first two branch minmod.) The product of the two arguments
|
|
|
|
* and a useful difference involving them are also precomputed to keep
|
|
|
|
* them out of branching way.
|
2009-01-20 22:32:05 +01:00
|
|
|
*/
|
2009-03-16 10:23:03 +01:00
|
|
|
#define FAST_MINMOD(a,b,ab,abminusaa) \
|
|
|
|
( (ab)>=0. ? ( (abminusaa)>=0. ? (a) : (b) ) : 0. )
|
2009-01-26 23:45:25 +01:00
|
|
|
|
2009-03-16 15:13:38 +01:00
|
|
|
#define VIPS_TYPE_INTERPOLATE_NOHALO1 \
|
|
|
|
(vips_interpolate_nohalo1_get_type())
|
|
|
|
#define VIPS_INTERPOLATE_NOHALO1( obj ) \
|
2009-01-21 00:16:30 +01:00
|
|
|
(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
|
2009-03-16 15:13:38 +01:00
|
|
|
VIPS_TYPE_INTERPOLATE_NOHALO1, VipsInterpolateNohalo1 ))
|
|
|
|
#define VIPS_INTERPOLATE_NOHALO1_CLASS( klass ) \
|
2009-01-21 00:16:30 +01:00
|
|
|
(G_TYPE_CHECK_CLASS_CAST( (klass), \
|
2009-03-16 15:13:38 +01:00
|
|
|
VIPS_TYPE_INTERPOLATE_NOHALO1, VipsInterpolateNohalo1Class))
|
|
|
|
#define VIPS_IS_INTERPOLATE_NOHALO1( obj ) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_INTERPOLATE_NOHALO1 ))
|
|
|
|
#define VIPS_IS_INTERPOLATE_NOHALO1_CLASS( klass ) \
|
|
|
|
(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_INTERPOLATE_NOHALO1 ))
|
|
|
|
#define VIPS_INTERPOLATE_NOHALO1_GET_CLASS( obj ) \
|
2009-01-21 00:16:30 +01:00
|
|
|
(G_TYPE_INSTANCE_GET_CLASS( (obj), \
|
2009-03-16 15:13:38 +01:00
|
|
|
VIPS_TYPE_INTERPOLATE_NOHALO1, VipsInterpolateNohalo1Class ))
|
2009-01-21 00:16:30 +01:00
|
|
|
|
2009-03-16 15:13:38 +01:00
|
|
|
typedef struct _VipsInterpolateNohalo1 {
|
2009-01-21 00:16:30 +01:00
|
|
|
VipsInterpolate parent_object;
|
|
|
|
|
2009-03-16 15:13:38 +01:00
|
|
|
} VipsInterpolateNohalo1;
|
2009-01-21 00:16:30 +01:00
|
|
|
|
2009-03-16 15:13:38 +01:00
|
|
|
typedef struct _VipsInterpolateNohalo1Class {
|
2009-01-21 00:16:30 +01:00
|
|
|
VipsInterpolateClass parent_class;
|
|
|
|
|
2009-03-16 15:13:38 +01:00
|
|
|
} VipsInterpolateNohalo1Class;
|
2009-01-21 00:16:30 +01:00
|
|
|
|
2009-01-27 16:50:19 +01:00
|
|
|
static void inline
|
2009-03-16 15:13:38 +01:00
|
|
|
nohalo1( const double uno_two,
|
|
|
|
const double uno_thr,
|
|
|
|
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_two,
|
|
|
|
const double qua_thr,
|
|
|
|
double* restrict r1,
|
|
|
|
double* restrict r2,
|
|
|
|
double* restrict r3 )
|
2009-01-20 22:32:05 +01:00
|
|
|
{
|
2009-03-04 23:20:58 +01:00
|
|
|
/*
|
2009-03-04 23:09:54 +01:00
|
|
|
* This function calculates the missing three double density pixel
|
|
|
|
* values. The caller does bilinear interpolation on them and
|
|
|
|
* dos_two.
|
|
|
|
*/
|
2009-01-23 11:56:12 +01:00
|
|
|
/*
|
2009-03-04 23:09:54 +01:00
|
|
|
* THE STENCIL OF INPUT VALUES:
|
2009-01-23 11:56:12 +01:00
|
|
|
*
|
2009-03-04 23:09:54 +01:00
|
|
|
* Nohalo's stencil is the same as, say, Catmull-Rom, with the
|
|
|
|
* exception that the four corner values are not used:
|
2009-01-23 11:56:12 +01:00
|
|
|
*
|
2009-03-18 01:41:57 +01:00
|
|
|
* (ix,iy-1) (ix+1,iy-1)
|
2009-03-04 23:09:54 +01:00
|
|
|
* = uno_two = uno_thr
|
2009-01-23 11:56:12 +01:00
|
|
|
*
|
2009-03-18 01:41:57 +01:00
|
|
|
* (ix-1,iy) (ix,iy) (ix+1,iy) (ix+2,iy)
|
2009-03-04 23:09:54 +01:00
|
|
|
* = dos_one = dos_two = dos_thr = dos_fou
|
2009-01-23 11:56:12 +01:00
|
|
|
*
|
2009-03-18 01:41:57 +01:00
|
|
|
* (ix-1,iy+1) (ix,iy+1) (ix+1,iy+1) (ix+2,iy+1)
|
2009-03-04 23:09:54 +01:00
|
|
|
* = tre_one = tre_two = tre_thr = tre_fou
|
2009-01-23 11:56:12 +01:00
|
|
|
*
|
2009-03-18 01:41:57 +01:00
|
|
|
* (ix,iy+2) (ix+1,iy+2)
|
2009-03-04 23:09:54 +01:00
|
|
|
* = qua_two = qua_thr
|
2009-01-23 11:56:12 +01:00
|
|
|
*
|
2009-03-18 01:41:57 +01:00
|
|
|
* Here, ix is the floor of the requested left-to-right location, iy
|
|
|
|
* is the floor of the requested up-to-down location.
|
|
|
|
*
|
|
|
|
* Pointer arithmetic is used to implicitly reflect the input
|
|
|
|
* stencil so that the requested pixel location is closer to
|
|
|
|
* dos_two, The above consequently corresponds to the case in which
|
|
|
|
* absolute_x is closer to ix than ix+1, and absolute_y is closer to
|
|
|
|
* iy than iy+1. For example, if relative_x_is_rite = 1 but
|
|
|
|
* relative_y_is_down = 0 (see below), then dos_two corresponds to
|
|
|
|
* (ix+1,iy), dos_thr corresponds to (ix,iy) etc. Consequently, the
|
|
|
|
* three missing double density values (corresponding to r1, r2 and
|
|
|
|
* r3) are halfway between dos_two and dos_thr, halfway between
|
2009-03-04 23:09:54 +01:00
|
|
|
* dos_two and tre_two, and at the average of the four central
|
|
|
|
* positions.
|
2009-03-18 01:41:57 +01:00
|
|
|
*
|
|
|
|
* The following code assumes that the stencil reflection has
|
|
|
|
* already been performed.
|
2009-01-23 11:56:12 +01:00
|
|
|
*/
|
2009-03-18 01:41:57 +01:00
|
|
|
|
2009-01-23 11:56:12 +01:00
|
|
|
/*
|
|
|
|
* Computation of the nonlinear slopes: If two consecutive pixel
|
|
|
|
* value differences have the same sign, the smallest one (in
|
|
|
|
* absolute value) is taken to be the corresponding slope; if the
|
|
|
|
* two consecutive pixel value differences don't have the same sign,
|
2009-03-18 01:41:57 +01:00
|
|
|
* the corresponding slope is set to 0. In other words, apply minmod
|
|
|
|
* to comsecutive differences.
|
2009-01-23 11:56:12 +01:00
|
|
|
*/
|
2009-03-04 23:09:54 +01:00
|
|
|
/*
|
|
|
|
* Dos(s) horizontal differences:
|
|
|
|
*/
|
|
|
|
const double prem_dos = dos_two - dos_one;
|
|
|
|
const double deux_dos = dos_thr - dos_two;
|
|
|
|
const double troi_dos = dos_fou - dos_thr;
|
2009-01-23 11:56:12 +01:00
|
|
|
/*
|
|
|
|
* Tre(s) horizontal differences:
|
|
|
|
*/
|
2009-03-04 23:09:54 +01:00
|
|
|
const double prem_tre = tre_two - tre_one;
|
2009-01-23 11:56:12 +01:00
|
|
|
const double deux_tre = tre_thr - tre_two;
|
|
|
|
const double troi_tre = tre_fou - tre_thr;
|
|
|
|
/*
|
2009-03-04 23:09:54 +01:00
|
|
|
* Two vertical differences:
|
2009-01-23 11:56:12 +01:00
|
|
|
*/
|
2009-03-04 23:09:54 +01:00
|
|
|
const double prem_two = dos_two - uno_two;
|
|
|
|
const double deux_two = tre_two - dos_two;
|
|
|
|
const double troi_two = qua_two - tre_two;
|
2009-01-23 11:56:12 +01:00
|
|
|
/*
|
|
|
|
* Thr(ee) vertical differences:
|
|
|
|
*/
|
2009-03-04 23:09:54 +01:00
|
|
|
const double prem_thr = dos_thr - uno_thr;
|
2009-01-23 11:56:12 +01:00
|
|
|
const double deux_thr = tre_thr - dos_thr;
|
|
|
|
const double troi_thr = qua_thr - tre_thr;
|
2009-03-04 23:09:54 +01:00
|
|
|
|
2009-03-15 14:21:59 +01:00
|
|
|
/*
|
2009-03-18 01:41:57 +01:00
|
|
|
* Products and differences useful for minmod:
|
2009-01-23 11:56:12 +01:00
|
|
|
*/
|
2009-03-16 10:17:46 +01:00
|
|
|
const double deux_prem_dos = deux_dos * prem_dos;
|
|
|
|
const double deux_deux_dos = deux_dos * deux_dos;
|
|
|
|
const double deux_troi_dos = deux_dos * troi_dos;
|
2009-01-21 00:16:30 +01:00
|
|
|
|
2009-03-16 10:17:46 +01:00
|
|
|
const double deux_prem_two = deux_two * prem_two;
|
|
|
|
const double deux_deux_two = deux_two * deux_two;
|
|
|
|
const double deux_troi_two = deux_two * troi_two;
|
2009-03-04 23:09:54 +01:00
|
|
|
|
2009-03-18 01:41:57 +01:00
|
|
|
const double deux_prem_minus_deux_deux_dos = deux_prem_dos - deux_deux_dos;
|
|
|
|
const double deux_troi_minus_deux_deux_dos = deux_troi_dos - deux_deux_dos;
|
|
|
|
|
|
|
|
const double deux_prem_minus_deux_deux_two = deux_prem_two - deux_deux_two;
|
|
|
|
const double deux_troi_minus_deux_deux_two = deux_troi_two - deux_deux_two;
|
|
|
|
|
2009-03-16 10:17:46 +01:00
|
|
|
const double deux_prem_tre = deux_tre * prem_tre;
|
|
|
|
const double deux_deux_tre = deux_tre * deux_tre;
|
|
|
|
const double deux_troi_tre = deux_tre * troi_tre;
|
2009-03-04 23:09:54 +01:00
|
|
|
|
2009-03-16 10:17:46 +01:00
|
|
|
const double deux_prem_thr = deux_thr * prem_thr;
|
|
|
|
const double deux_deux_thr = deux_thr * deux_thr;
|
|
|
|
const double deux_troi_thr = deux_thr * troi_thr;
|
2009-01-26 23:45:25 +01:00
|
|
|
|
2009-03-16 10:17:46 +01:00
|
|
|
const double deux_prem_minus_deux_deux_tre = deux_prem_tre - deux_deux_tre;
|
|
|
|
const double deux_troi_minus_deux_deux_tre = deux_troi_tre - deux_deux_tre;
|
|
|
|
|
|
|
|
const double deux_prem_minus_deux_deux_thr = deux_prem_thr - deux_deux_thr;
|
|
|
|
const double deux_troi_minus_deux_deux_thr = deux_troi_thr - deux_deux_thr;
|
|
|
|
|
2009-03-18 01:41:57 +01:00
|
|
|
/*
|
|
|
|
* Useful sums:
|
|
|
|
*/
|
|
|
|
const double dos_two_plus_dos_thr = dos_two + dos_thr;
|
|
|
|
const double dos_two_plus_tre_two = dos_two + tre_two;
|
|
|
|
const double deux_thr_plus_deux_dos = deux_thr + deux_dos;
|
|
|
|
|
2009-01-26 23:45:25 +01:00
|
|
|
/*
|
2009-03-04 23:09:54 +01:00
|
|
|
* Compute the needed "right" (at the boundary between one input
|
2009-01-26 23:45:25 +01:00
|
|
|
* pixel areas) double resolution pixel value:
|
2009-01-23 11:56:12 +01:00
|
|
|
*/
|
2009-03-15 14:21:59 +01:00
|
|
|
const double four_times_dos_twothr =
|
2009-03-16 10:17:46 +01:00
|
|
|
FAST_MINMOD( deux_dos, prem_dos, deux_prem_dos,
|
|
|
|
deux_prem_minus_deux_deux_dos )
|
2009-03-15 14:21:59 +01:00
|
|
|
-
|
2009-03-16 10:17:46 +01:00
|
|
|
FAST_MINMOD( deux_dos, troi_dos, deux_troi_dos,
|
|
|
|
deux_troi_minus_deux_deux_dos )
|
2009-03-15 22:04:22 +01:00
|
|
|
+
|
|
|
|
2. * dos_two_plus_dos_thr;
|
2009-01-26 23:45:25 +01:00
|
|
|
|
2009-01-23 11:56:12 +01:00
|
|
|
/*
|
2009-01-26 23:45:25 +01:00
|
|
|
* Compute the needed "down" double resolution pixel value:
|
2009-01-23 11:56:12 +01:00
|
|
|
*/
|
2009-03-15 14:21:59 +01:00
|
|
|
const double four_times_dostre_two =
|
2009-03-16 10:17:46 +01:00
|
|
|
FAST_MINMOD( deux_two, prem_two, deux_prem_two,
|
|
|
|
deux_prem_minus_deux_deux_two )
|
2009-03-15 14:21:59 +01:00
|
|
|
-
|
2009-03-16 10:17:46 +01:00
|
|
|
FAST_MINMOD( deux_two, troi_two, deux_troi_two,
|
|
|
|
deux_troi_minus_deux_deux_two )
|
2009-03-15 22:04:22 +01:00
|
|
|
+
|
|
|
|
2. * dos_two_plus_tre_two;
|
2009-01-21 00:16:30 +01:00
|
|
|
|
2009-01-23 11:56:12 +01:00
|
|
|
/*
|
2009-03-04 23:09:54 +01:00
|
|
|
* Compute the "diagonal" (at the boundary between thrr input
|
2009-01-26 23:45:25 +01:00
|
|
|
* pixel areas) double resolution pixel value:
|
2009-01-23 11:56:12 +01:00
|
|
|
*/
|
2009-03-15 22:04:22 +01:00
|
|
|
const double piece_of_eight_times_dostre_twothr =
|
2009-03-15 14:21:59 +01:00
|
|
|
four_times_dos_twothr
|
2009-01-23 11:56:12 +01:00
|
|
|
+
|
2009-03-15 14:21:59 +01:00
|
|
|
four_times_dostre_two
|
|
|
|
+
|
2009-03-15 22:04:22 +01:00
|
|
|
2. * deux_thr_plus_deux_dos;
|
|
|
|
|
|
|
|
const double eight_times_dostre_twothr =
|
2009-03-16 10:17:46 +01:00
|
|
|
FAST_MINMOD( deux_tre, prem_tre, deux_prem_tre,
|
|
|
|
deux_prem_minus_deux_deux_tre )
|
2009-03-15 14:21:59 +01:00
|
|
|
-
|
2009-03-16 10:17:46 +01:00
|
|
|
FAST_MINMOD( deux_tre, troi_tre, deux_troi_tre,
|
|
|
|
deux_troi_minus_deux_deux_tre )
|
2009-03-15 14:21:59 +01:00
|
|
|
+
|
2009-03-16 10:17:46 +01:00
|
|
|
FAST_MINMOD( deux_thr, prem_thr, deux_prem_thr,
|
|
|
|
deux_prem_minus_deux_deux_thr )
|
2009-03-15 14:21:59 +01:00
|
|
|
-
|
2009-03-16 10:17:46 +01:00
|
|
|
FAST_MINMOD( deux_thr, troi_thr, deux_troi_thr,
|
2009-03-18 01:48:55 +01:00
|
|
|
deux_troi_minus_deux_deux_thr )
|
|
|
|
+
|
|
|
|
piece_of_eight_times_dostre_twothr;
|
2009-01-21 00:16:30 +01:00
|
|
|
|
2009-03-04 23:09:54 +01:00
|
|
|
/*
|
2009-03-15 03:30:57 +01:00
|
|
|
* Return the first newly computed double density values:
|
2009-03-04 23:09:54 +01:00
|
|
|
*/
|
2009-03-15 14:21:59 +01:00
|
|
|
*r1 = four_times_dos_twothr;
|
|
|
|
*r2 = four_times_dostre_two;
|
|
|
|
*r3 = eight_times_dostre_twothr;
|
2009-01-20 22:32:05 +01:00
|
|
|
}
|
|
|
|
|
2009-03-16 15:13:38 +01:00
|
|
|
/* Call nohalo1 with an interpolator as a parameter.
|
2009-03-04 23:20:58 +01:00
|
|
|
* It'd be nice to do this with templates somehow :-( but I can't see a
|
2009-01-27 18:09:40 +01:00
|
|
|
* clean way to do it.
|
2009-01-27 16:03:08 +01:00
|
|
|
*/
|
2009-03-16 15:13:38 +01:00
|
|
|
#define NOHALO1_INTER( inter ) \
|
2009-02-06 04:33:16 +01:00
|
|
|
template <typename T> static void inline \
|
2009-03-16 15:13:38 +01:00
|
|
|
nohalo1_ ## inter( PEL* restrict pout, \
|
|
|
|
const PEL* restrict pin, \
|
|
|
|
const int bands, \
|
|
|
|
const int lskip, \
|
|
|
|
const double relative_x, \
|
|
|
|
const double relative_y ) \
|
2009-02-06 04:33:16 +01:00
|
|
|
{ \
|
2009-03-16 15:13:38 +01:00
|
|
|
T* restrict out = (T *) pout; \
|
2009-02-06 04:33:16 +01:00
|
|
|
\
|
2009-03-04 23:09:54 +01:00
|
|
|
const int relative_y_is_down = ( relative_y >= 0. ); \
|
2009-03-18 01:41:57 +01:00
|
|
|
const int relative_x_is_rite = ( relative_x >= 0. ); \
|
2009-03-04 23:09:54 +01:00
|
|
|
\
|
|
|
|
const int sign_of_relative_y = 2 * relative_y_is_down - 1; \
|
2009-03-18 01:41:57 +01:00
|
|
|
const int sign_of_relative_x = 2 * relative_x_is_rite - 1; \
|
2009-02-06 04:33:16 +01:00
|
|
|
\
|
|
|
|
const int corner_reflection_shift = \
|
2009-03-18 01:41:57 +01:00
|
|
|
relative_y_is_down * lskip + relative_x_is_rite * bands; \
|
2009-02-06 04:33:16 +01:00
|
|
|
\
|
2009-03-04 23:09:54 +01:00
|
|
|
const int shift_1_row = sign_of_relative_y * lskip; \
|
2009-03-18 01:41:57 +01:00
|
|
|
const int shift_1_pixel = sign_of_relative_x * bands; \
|
2009-02-06 04:33:16 +01:00
|
|
|
\
|
2009-03-18 01:41:57 +01:00
|
|
|
const T* restrict in = ( (T *) pin ) + corner_reflection_shift; \
|
2009-02-06 04:33:16 +01:00
|
|
|
\
|
2009-03-04 23:09:54 +01:00
|
|
|
const int uno_two_shift = shift_1_row; \
|
|
|
|
const int uno_thr_shift = shift_1_row - shift_1_pixel; \
|
2009-02-06 04:33:16 +01:00
|
|
|
\
|
2009-03-04 23:09:54 +01:00
|
|
|
const int dos_one_shift = shift_1_pixel; \
|
|
|
|
const int dos_two_shift = 0; \
|
|
|
|
const int dos_thr_shift = -shift_1_pixel; \
|
2009-03-18 01:41:57 +01:00
|
|
|
\
|
|
|
|
const double w = ( 2 * sign_of_relative_x ) * relative_x; \
|
|
|
|
const double z = ( 2 * sign_of_relative_y ) * relative_y; \
|
|
|
|
\
|
|
|
|
const int dos_fou_shift = 2 * dos_thr_shift; \
|
|
|
|
\
|
|
|
|
const double x = 1. - w; \
|
2009-02-06 04:33:16 +01:00
|
|
|
\
|
2009-03-04 23:09:54 +01:00
|
|
|
const int tre_one_shift = dos_one_shift - shift_1_row; \
|
|
|
|
const int tre_two_shift = -shift_1_row; \
|
|
|
|
const int tre_thr_shift = dos_thr_shift - shift_1_row; \
|
|
|
|
const int tre_fou_shift = dos_fou_shift - shift_1_row; \
|
2009-02-06 04:33:16 +01:00
|
|
|
\
|
2009-03-18 01:41:57 +01:00
|
|
|
const double w_times_z = w * z; \
|
|
|
|
const double x_times_z = x * z; \
|
|
|
|
\
|
2009-03-04 23:09:54 +01:00
|
|
|
const int qua_two_shift = tre_two_shift - shift_1_row; \
|
|
|
|
const int qua_thr_shift = tre_thr_shift - shift_1_row; \
|
|
|
|
\
|
2009-03-15 14:21:59 +01:00
|
|
|
const double w_times_y_over_4 = .25 * ( w - w_times_z ); \
|
|
|
|
const double x_times_z_over_4 = .25 * x_times_z; \
|
|
|
|
const double x_times_y_over_8 = .125 * ( x - x_times_z ); \
|
2009-02-06 04:33:16 +01:00
|
|
|
\
|
2009-03-16 05:47:13 +01:00
|
|
|
int band = bands; \
|
|
|
|
\
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
double four_times_dos_twothr; \
|
|
|
|
double four_times_dostre_two; \
|
|
|
|
double eight_times_dostre_twothr; \
|
|
|
|
\
|
|
|
|
const double dos_two = in[dos_two_shift]; \
|
|
|
|
\
|
2009-03-16 15:13:38 +01:00
|
|
|
nohalo1( in[uno_two_shift], in[uno_thr_shift], \
|
|
|
|
in[dos_one_shift], dos_two, \
|
|
|
|
in[dos_thr_shift], in[dos_fou_shift], \
|
|
|
|
in[tre_one_shift], in[tre_two_shift], \
|
|
|
|
in[tre_thr_shift], in[tre_fou_shift], \
|
|
|
|
in[qua_two_shift], in[qua_thr_shift], \
|
|
|
|
&four_times_dos_twothr, \
|
|
|
|
&four_times_dostre_two, \
|
|
|
|
&eight_times_dostre_twothr ); \
|
2009-03-16 05:47:13 +01:00
|
|
|
\
|
|
|
|
const T result = bilinear_ ## inter<T>( w_times_z, \
|
|
|
|
x_times_z_over_4, \
|
|
|
|
w_times_y_over_4, \
|
|
|
|
x_times_y_over_8, \
|
|
|
|
dos_two, \
|
|
|
|
four_times_dos_twothr, \
|
|
|
|
four_times_dostre_two, \
|
|
|
|
eight_times_dostre_twothr ); \
|
|
|
|
\
|
|
|
|
in++; \
|
|
|
|
*out++ = result; \
|
|
|
|
} while (--band); \
|
2009-03-04 23:20:58 +01:00
|
|
|
}
|
2009-01-27 18:09:40 +01:00
|
|
|
|
2009-03-16 15:13:38 +01:00
|
|
|
NOHALO1_INTER( float )
|
|
|
|
NOHALO1_INTER( signed )
|
|
|
|
NOHALO1_INTER( unsigned )
|
2009-01-20 22:32:05 +01:00
|
|
|
|
2009-01-23 13:03:11 +01:00
|
|
|
/* We need C linkage for this.
|
|
|
|
*/
|
|
|
|
extern "C" {
|
2009-03-16 15:13:38 +01:00
|
|
|
G_DEFINE_TYPE( VipsInterpolateNohalo1, vips_interpolate_nohalo1,
|
2009-01-23 13:03:11 +01:00
|
|
|
VIPS_TYPE_INTERPOLATE );
|
|
|
|
}
|
|
|
|
|
2009-01-23 11:56:12 +01:00
|
|
|
static void
|
2009-03-16 15:13:38 +01:00
|
|
|
vips_interpolate_nohalo1_interpolate( VipsInterpolate* restrict interpolate,
|
|
|
|
PEL* restrict out,
|
|
|
|
REGION* restrict in,
|
|
|
|
double absolute_x,
|
|
|
|
double absolute_y )
|
2009-01-21 00:16:30 +01:00
|
|
|
{
|
2009-03-15 03:30:57 +01:00
|
|
|
/*
|
2009-03-18 01:41:57 +01:00
|
|
|
* VIPS versions of Nicolas's pixel addressing values.
|
2009-03-15 03:30:57 +01:00
|
|
|
*/
|
2009-03-18 01:41:57 +01:00
|
|
|
const int actual_bands = in->im->Bands;
|
2009-03-15 14:27:16 +01:00
|
|
|
const int lskip = IM_REGION_LSKIP( in ) / IM_IMAGE_SIZEOF_ELEMENT( in->im );
|
2009-03-15 14:21:59 +01:00
|
|
|
|
2009-03-18 01:41:57 +01:00
|
|
|
const double absolute_y_minus_half = absolute_y - .5;
|
|
|
|
const double absolute_x_minus_half = absolute_x - .5;
|
2009-01-20 22:32:05 +01:00
|
|
|
/*
|
|
|
|
* floor's surrogate FAST_PSEUDO_FLOOR is used to make sure that the
|
|
|
|
* transition through 0 is smooth. If it is known that absolute_x
|
2009-03-04 23:09:54 +01:00
|
|
|
* and absolute_y will never be less than 0, plain cast---that is,
|
|
|
|
* const int ix = absolute_x---should be used instead. Actually,
|
|
|
|
* any function which agrees with floor for non-integer values, and
|
2009-01-20 22:32:05 +01:00
|
|
|
* picks one of the two possibilities for integer values, can be
|
2009-03-04 23:09:54 +01:00
|
|
|
* used. FAST_PSEUDO_FLOOR fits the bill.
|
2009-03-15 03:30:57 +01:00
|
|
|
*
|
|
|
|
* Then, x is the x-coordinate of the sampling point relative to the
|
|
|
|
* position of the center of the convex hull of the 2x2 block of
|
|
|
|
* closest pixels. Similarly for y. Range of values: [-.5,.5).
|
2009-01-20 22:32:05 +01:00
|
|
|
*/
|
2009-03-16 05:47:13 +01:00
|
|
|
const int iy = FAST_PSEUDO_FLOOR (absolute_y);
|
|
|
|
const double relative_y = absolute_y_minus_half - iy;
|
|
|
|
const int ix = FAST_PSEUDO_FLOOR (absolute_x);
|
|
|
|
const double relative_x = absolute_x_minus_half - ix;
|
2009-01-20 22:32:05 +01:00
|
|
|
|
2009-03-04 23:09:54 +01:00
|
|
|
/*
|
2009-03-18 01:41:57 +01:00
|
|
|
* Move the pointer to (the first band of) the top/left pixel of the
|
|
|
|
* 2x2 group of pixel centers which contains the sampling location
|
|
|
|
* in its convex hull:
|
2009-03-04 23:09:54 +01:00
|
|
|
*/
|
2009-03-16 15:13:38 +01:00
|
|
|
const PEL* restrict p = (PEL *) IM_REGION_ADDR( in, ix, iy );
|
2009-02-06 04:25:57 +01:00
|
|
|
|
2009-03-18 01:41:57 +01:00
|
|
|
/*
|
|
|
|
* Double bands for complex images:
|
|
|
|
*/
|
|
|
|
const int bands =
|
|
|
|
( im_iscomplex( in->im ) ? 2 * actual_bands : actual_bands );
|
|
|
|
|
2009-03-04 23:09:54 +01:00
|
|
|
#define CALL( T, inter ) \
|
2009-03-16 15:13:38 +01:00
|
|
|
nohalo1_ ## inter<T>( out, \
|
|
|
|
p, \
|
|
|
|
bands, \
|
|
|
|
lskip, \
|
|
|
|
relative_x, \
|
|
|
|
relative_y );
|
|
|
|
|
|
|
|
switch( in->im->BandFmt ) {
|
|
|
|
case IM_BANDFMT_UCHAR:
|
|
|
|
CALL( unsigned char, unsigned );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IM_BANDFMT_CHAR:
|
|
|
|
CALL( signed char, signed );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IM_BANDFMT_USHORT:
|
|
|
|
CALL( unsigned short, unsigned );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IM_BANDFMT_SHORT:
|
|
|
|
CALL( signed short, signed );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IM_BANDFMT_UINT:
|
|
|
|
CALL( unsigned int, unsigned );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IM_BANDFMT_INT:
|
|
|
|
CALL( signed int, signed );
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Complex images handled by doubling of bands, see above.
|
|
|
|
*/
|
|
|
|
case IM_BANDFMT_FLOAT:
|
|
|
|
case IM_BANDFMT_COMPLEX:
|
|
|
|
CALL( float, float );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IM_BANDFMT_DOUBLE:
|
|
|
|
case IM_BANDFMT_DPCOMPLEX:
|
|
|
|
CALL( double, float );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert( 0 );
|
|
|
|
break;
|
|
|
|
}
|
2009-01-21 00:16:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-03-16 15:13:38 +01:00
|
|
|
vips_interpolate_nohalo1_class_init( VipsInterpolateNohalo1Class *klass )
|
2009-01-21 00:16:30 +01:00
|
|
|
{
|
2009-03-16 15:13:38 +01:00
|
|
|
VipsObjectClass *object_class = VIPS_OBJECT_CLASS( klass );
|
|
|
|
VipsInterpolateClass *interpolate_class =
|
|
|
|
VIPS_INTERPOLATE_CLASS( klass );
|
2009-01-21 00:16:30 +01:00
|
|
|
|
2009-03-16 15:13:38 +01:00
|
|
|
object_class->nickname = "nohalo1";
|
|
|
|
object_class->description = _( "Edge-enhancing bilinear" );
|
2009-01-21 00:16:30 +01:00
|
|
|
|
2009-03-16 15:13:38 +01:00
|
|
|
interpolate_class->interpolate = vips_interpolate_nohalo1_interpolate;
|
|
|
|
interpolate_class->window_size = 4;
|
2009-01-20 22:32:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-03-16 15:13:38 +01:00
|
|
|
vips_interpolate_nohalo1_init( VipsInterpolateNohalo1 *nohalo )
|
2009-01-20 22:32:05 +01:00
|
|
|
{
|
|
|
|
}
|