in-line reduceh as well

This commit is contained in:
John Cupitt 2016-01-29 15:01:54 +00:00
parent dd7b30bd00
commit fbe010b80a
8 changed files with 532 additions and 724 deletions

14
TODO
View File

@ -35,8 +35,20 @@
user 0m6.296s
sys 0m0.304s
with in-line reduceh as well
$ time vipsthumbnail wtc.tif -o x.tif -s 7000
real 0m1.838s
user 0m6.132s
sys 0m0.264s
more small tweaks
$ time vipsthumbnail wtc.tif -o x.tif -s 7000
real 0m1.843s
user 0m6.040s
sys 0m0.252s
- get some brightly coloured spots with nohalo / vsqbs on wobble.ws ... very
odd, the interpolation shouldn't change between bands

View File

@ -10,12 +10,11 @@ libresample_la_SOURCES = \
shrinkh.c \
shrinkv.c \
reduce.c \
reduceh.c \
reduceh.cpp \
reducev.cpp \
interpolate.c \
transform.c \
bicubic.cpp \
cubich.cpp \
lbb.cpp \
nohalo.cpp \
vsqbs.cpp \

View File

@ -1,372 +0,0 @@
/* horizontal cubic (catmull-rom) interpolator
*
* 26/1/16
* - from bicubic.cpp
*/
/*
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., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/* Cubic (Catmull-Rom) interpolator derived from Nicolas Robidoux's
* original YAFR resampler with permission and thanks.
*/
/*
#define DEBUG
*/
#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"
#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif /*WITH_DMALLOC*/
#define VIPS_TYPE_INTERPOLATE_CUBICH \
(vips_interpolate_cubich_get_type())
#define VIPS_INTERPOLATE_CUBICH( obj ) \
(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
VIPS_TYPE_INTERPOLATE_CUBICH, VipsInterpolateCubich ))
#define VIPS_INTERPOLATE_CUBICH_CLASS( klass ) \
(G_TYPE_CHECK_CLASS_CAST( (klass), \
VIPS_TYPE_INTERPOLATE_CUBICH, VipsInterpolateCubichClass))
#define VIPS_IS_INTERPOLATE_CUBICH( obj ) \
(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_INTERPOLATE_CUBICH ))
#define VIPS_IS_INTERPOLATE_CUBICH_CLASS( klass ) \
(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_INTERPOLATE_CUBICH ))
#define VIPS_INTERPOLATE_CUBICH_GET_CLASS( obj ) \
(G_TYPE_INSTANCE_GET_CLASS( (obj), \
VIPS_TYPE_INTERPOLATE_CUBICH, VipsInterpolateCubichClass ))
typedef VipsInterpolate VipsInterpolateCubich;
typedef VipsInterpolateClass VipsInterpolateCubichClass;
/* Precalculated interpolation matrices. int (used for pel
* sizes up to short), and double (for all others). We go to
* scale + 1 so we can round-to-nearest safely.
*/
static int vips_cubich_matrixi[VIPS_TRANSFORM_SCALE + 1][4];
static double vips_cubich_matrixf[VIPS_TRANSFORM_SCALE + 1][4];
/* We need C linkage for this.
*/
extern "C" {
G_DEFINE_TYPE( VipsInterpolateCubich, vips_interpolate_cubich,
VIPS_TYPE_INTERPOLATE );
}
/* Pointers to write to / read from, number of bands,
* how many bytes to add to move down a line.
*/
/* T is the type of pixels we are reading and writing.
*/
/* Fixed-point version, for 8 and 16-bit types.
*/
template <typename T, int max_value>
static void inline
cubich_unsigned_int_tab( void *pout, const VipsPel *pin,
const int bands, const int *cx )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int b1 = bands;
const int b2 = b1 + b1;
const int b3 = b1 + b2;
for( int z = 0; z < bands; z++ ) {
const T one = in[0];
const T two = in[b1];
const T thr = in[b2];
const T fou = in[b3];
int cubich = cubic_unsigned_int<T>( one, two, thr, fou, cx );
cubich = VIPS_CLIP( 0, cubich, max_value );
out[z] = cubich;
in += 1;
}
}
template <typename T, int min_value, int max_value>
static void inline
cubich_signed_int_tab( void *pout, const VipsPel *pin,
const int bands, const int *cx )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int b1 = bands;
const int b2 = b1 + b1;
const int b3 = b1 + b2;
for( int z = 0; z < bands; z++ ) {
const T one = in[0];
const T two = in[b1];
const T thr = in[b2];
const T fou = in[b3];
int cubich = cubic_signed_int<T>( one, two, thr, fou, cx );
cubich = VIPS_CLIP( min_value, cubich, max_value );
out[z] = cubich;
in += 1;
}
}
/* Floating-point version, for int/float types.
*/
template <typename T>
static void inline
cubich_float_tab( void *pout, const VipsPel *pin,
const int bands, const double *cx )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int b1 = bands;
const int b2 = b1 + b1;
const int b3 = b1 + b2;
for( int z = 0; z < bands; z++ ) {
const T one = in[0];
const T two = in[b1];
const T thr = in[b2];
const T fou = in[b3];
out[z] = cubic_float<T>( one, two, thr, fou, cx );
in += 1;
}
}
/* Ultra-high-quality version for double images.
*/
template <typename T>
static void inline
cubich_notab( void *pout, const VipsPel *pin,
const int bands, double x )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int b1 = bands;
const int b2 = b1 + b1;
const int b3 = b1 + b2;
double cx[4];
calculate_coefficients_catmull( x, cx );
for( int z = 0; z < bands; z++ ) {
const T one = in[0];
const T two = in[b1];
const T thr = in[b2];
const T fou = in[b3];
out[z] = cubic_float<T>( one, two, thr, fou, cx );
in += 1;
}
}
static void
vips_interpolate_cubich_interpolate( VipsInterpolate *interpolate,
void *out, VipsRegion *in, double x, double y )
{
/* Find the mask index. We round-to-nearest, so we need to generate
* indexes in 0 to VIPS_TRANSFORM_SCALE, 2^n + 1 values. We multiply
* by 2 more than we need to, add one, mask, then shift down again to
* get the extra range.
*/
const int sx = x * VIPS_TRANSFORM_SCALE * 2;
const int six = sx & (VIPS_TRANSFORM_SCALE * 2 - 1);
const int tx = (six + 1) >> 1;
/* We know (x, y) are always positive, so we can just (int) them.
*/
const int ix = (int) x;
const int iy = (int) y;
/* Back one to get the left of the 4x1.
*/
const VipsPel *p = VIPS_REGION_ADDR( in, ix - 1, iy );
/* Look up the tables we need.
*/
const int *cxi = vips_cubich_matrixi[tx];
const double *cxf = vips_cubich_matrixf[tx];
/* Pel size and line size.
*/
const int bands = in->im->Bands;
g_assert( ix - 1 >= in->valid.left );
g_assert( iy >= in->valid.top );
g_assert( ix + 2 < VIPS_RECT_RIGHT( &in->valid ) );
g_assert( iy < VIPS_RECT_BOTTOM( &in->valid ) );
g_assert( iy == y );
/* Confirm that absolute_x >= 1, because of window_offset.
*/
g_assert( x >= 1.0 );
#ifdef DEBUG
printf( "vips_interpolate_cubich_interpolate: %g %g\n", x, y );
printf( "\tleft=%d, top=%d, width=%d, height=%d\n",
ix - 1, iy, 4, 1 );
printf( "\tmaskx=%d\n", tx );
#endif /*DEBUG*/
switch( in->im->BandFmt ) {
case VIPS_FORMAT_UCHAR:
cubich_unsigned_int_tab<unsigned char, UCHAR_MAX>(
out, p, bands, cxi );
//cubich_float_tab<unsigned char>(
//out, p, bands, cxf );
//cubich_notab<unsigned char>(
//out, p, bands, x - ix );
/*
Handy for benchmarking
cubich_float_tab<unsigned char>(
out, p, bands, cxf );
cubich_notab<unsigned char>(
out, p, bands, x - ix );
*/
break;
case VIPS_FORMAT_CHAR:
cubich_signed_int_tab<signed char, SCHAR_MIN, SCHAR_MAX>(
out, p, bands, cxi );
break;
case VIPS_FORMAT_USHORT:
cubich_unsigned_int_tab<unsigned short, USHRT_MAX>(
out, p, bands, cxi );
break;
case VIPS_FORMAT_SHORT:
cubich_signed_int_tab<signed short, SHRT_MIN, SHRT_MAX>(
out, p, bands, cxi );
break;
case VIPS_FORMAT_UINT:
cubich_float_tab<unsigned int>( out, p, bands, cxf );
break;
case VIPS_FORMAT_INT:
cubich_float_tab<signed int>( out, p, bands, cxf );
break;
case VIPS_FORMAT_FLOAT:
cubich_float_tab<float>( out, p, bands, cxf );
break;
case VIPS_FORMAT_DOUBLE:
cubich_notab<double>( out, p, bands, x - ix );
break;
case VIPS_FORMAT_COMPLEX:
cubich_float_tab<float>( out, p, bands * 2, cxf );
break;
case VIPS_FORMAT_DPCOMPLEX:
cubich_notab<double>( out, p, bands * 2, x - ix );
break;
default:
break;
}
}
static void
vips_interpolate_cubich_class_init( VipsInterpolateCubichClass *iclass )
{
VipsObjectClass *object_class = VIPS_OBJECT_CLASS( iclass );
VipsInterpolateClass *interpolate_class =
VIPS_INTERPOLATE_CLASS( iclass );
object_class->nickname = "cubich";
object_class->description =
_( "horizontal cubic interpolation (Catmull-Rom)" );
interpolate_class->interpolate = vips_interpolate_cubich_interpolate;
interpolate_class->window_size = 4;
/* Build the tables of pre-computed coefficients.
*/
for( int x = 0; x < VIPS_TRANSFORM_SCALE + 1; x++ ) {
calculate_coefficients_catmull(
(float) x / VIPS_TRANSFORM_SCALE,
vips_cubich_matrixf[x] );
for( int i = 0; i < 4; i++ )
vips_cubich_matrixi[x][i] =
vips_cubich_matrixf[x][i] *
VIPS_INTERPOLATE_SCALE;
}
}
static void
vips_interpolate_cubich_init( VipsInterpolateCubich *cubich )
{
#ifdef DEBUG
printf( "vips_interpolate_cubich_init: " );
vips_object_print( VIPS_OBJECT( cubich ) );
#endif /*DEBUG*/
}

View File

@ -600,7 +600,6 @@ void
vips__interpolate_init( void )
{
extern GType vips_interpolate_bicubic_get_type( void );
extern GType vips_interpolate_cubich_get_type( void );
extern GType vips_interpolate_lbb_get_type( void );
extern GType vips_interpolate_nohalo_get_type( void );
extern GType vips_interpolate_vsqbs_get_type( void );
@ -609,7 +608,6 @@ vips__interpolate_init( void )
vips_interpolate_bilinear_get_type();
vips_interpolate_bicubic_get_type();
vips_interpolate_cubich_get_type();
vips_interpolate_lbb_get_type();
vips_interpolate_nohalo_get_type();
vips_interpolate_vsqbs_get_type();

View File

@ -55,7 +55,6 @@ typedef struct _VipsReduce {
double xshrink; /* Shrink factors */
double yshrink;
VipsInterpolate *interpolateh;
} VipsReduce;
@ -75,39 +74,32 @@ vips_reduce_build( VipsObject *object )
return( -1 );
/*
if( vips_reduceh( resample->in, &t[0], reduce->xshrink,
"interpolate", reduce->interpolateh, NULL ) ||
if( vips_reduceh( resample->in, &t[0], reduce->xshrink, NULL ) ||
vips_linecache( t[0], &t[1],
"tile_height", 4,
NULL ) ||
vips_reducev( t[1], &t[2], reduce->yshrink,
NULL ) ||
vips_reducev( t[1], &t[2], reduce->yshrink, NULL ) ||
vips_image_write( t[2], resample->out ) )
return( -1 );
*/
/*
if( vips_reducev( resample->in, &t[0], reduce->yshrink,
NULL ) ||
if( vips_reducev( resample->in, &t[0], reduce->yshrink, NULL ) ||
vips_linecache( t[0], &t[1],
"tile_height", 4,
NULL ) ||
vips_reduceh( t[1], &t[2], reduce->xshrink,
"interpolate", reduce->interpolateh,
NULL ) ||
vips_reduceh( t[1], &t[2], reduce->xshrink, NULL ) ||
vips_image_write( t[2], resample->out ) )
return( -1 );
*/
if( vips_reducev( resample->in, &t[0], reduce->yshrink, NULL ) ||
vips_reduceh( t[0], &t[1], reduce->xshrink,
"interpolate", reduce->interpolateh, NULL ) ||
vips_reduceh( t[0], &t[1], reduce->xshrink, NULL ) ||
vips_image_write( t[1], resample->out ) )
return( -1 );
/*
if( vips_reduceh( resample->in, &t[0], reduce->xshrink,
"interpolate", reduce->interpolateh, NULL ) ||
if( vips_reduceh( resample->in, &t[0], reduce->xshrink, NULL ) ||
vips_reducev( t[0], &t[1], reduce->yshrink, NULL ) ||
vips_image_write( t[1], resample->out ) )
return( -1 );
@ -148,12 +140,6 @@ vips_reduce_class_init( VipsReduceClass *class )
G_STRUCT_OFFSET( VipsReduce, yshrink ),
1.0, 1000000.0, 1.0 );
VIPS_ARG_INTERPOLATE( class, "interpolateh", 10,
_( "Interpolateh" ),
_( "Interpolate horizontal pixels with this" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsReduce, interpolateh ) );
}
static void
@ -169,12 +155,8 @@ vips_reduce_init( VipsReduce *reduce )
* @shrinke: vertical shrink
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @interpolateh: interpolate horizontally with this, default cubich
*
* Reduce @in by a pair of factors with a pair of 1D interpolators. This iwll
* not work well for shrink factors greater than two.
* Reduce @in by a pair of factors with a pair of 1D cubic interpolators. This
* will not work well for shrink factors greater than two.
*
* This is a very low-level operation: see vips_resize() for a more
* convenient way to resize images.

View File

@ -1,292 +0,0 @@
/* horizontal reduce by a float factor
*
* 30/10/15
* - from reduceh.c
*/
/*
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., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vips/vips.h>
#include <vips/debug.h>
#include <vips/internal.h>
#include "presample.h"
typedef struct _VipsReduceh {
VipsResample parent_instance;
double xshrink; /* Reduce factor */
VipsInterpolate *interpolate;
} VipsReduceh;
typedef VipsResampleClass VipsReducehClass;
G_DEFINE_TYPE( VipsReduceh, vips_reduceh, VIPS_TYPE_RESAMPLE );
static int
vips_reduceh_gen( VipsRegion *or, void *seq,
void *a, void *b, gboolean *stop )
{
VipsImage *in = (VipsImage *) a;
VipsReduceh *reduceh = (VipsReduceh *) b;
int window_size =
vips_interpolate_get_window_size( reduceh->interpolate );
int window_offset =
vips_interpolate_get_window_offset( reduceh->interpolate );
const VipsInterpolateMethod interpolate =
vips_interpolate_get_method( reduceh->interpolate );
int ps = VIPS_IMAGE_SIZEOF_PEL( in );
VipsRegion *ir = (VipsRegion *) seq;
VipsRect *r = &or->valid;
VipsRect s;
int y;
#ifdef DEBUG
printf( "vips_reduceh_gen: generating %d x %d at %d x %d\n",
r->width, r->height, r->left, r->top );
#endif /*DEBUG*/
s.left = r->left * reduceh->xshrink - window_offset;
s.top = r->top;
s.width = r->width * reduceh->xshrink + window_size - 1;
s.height = r->height;
if( vips_region_prepare( ir, &s ) )
return( -1 );
VIPS_GATE_START( "vips_reduceh_gen: work" );
for( y = 0; y < r->height; y ++ ) {
VipsPel *q = VIPS_REGION_ADDR( or, r->left, r->top + y );
double X = window_offset + r->left * reduceh->xshrink;
double Y = r->top + y;
int x;
for( x = 0; x < r->width; x++ ) {
interpolate( reduceh->interpolate, q, ir, X, Y );
X += reduceh->xshrink;
q += ps;
}
}
VIPS_GATE_STOP( "vips_reduceh_gen: work" );
return( 0 );
}
static int
vips_reduceh_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsResample *resample = VIPS_RESAMPLE( object );
VipsReduceh *reduceh = (VipsReduceh *) object;
VipsImage **t = (VipsImage **)
vips_object_local_array( object, 1 );
VipsImage *in;
int window_size;
int window_offset;
if( VIPS_OBJECT_CLASS( vips_reduceh_parent_class )->build( object ) )
return( -1 );
in = resample->in;
/* We can't use vips_object_argument_isset(), since it may have been
* set to NULL, see vips_similarity().
*/
if( !reduceh->interpolate ) {
VipsInterpolate *interpolate;
interpolate = vips_interpolate_new( "cubich" );
g_object_set( object,
"interpolate", interpolate,
NULL );
g_object_unref( interpolate );
/* coverity gets confused by this, it thinks
* reduceh->interpolate may still be null. Assign ourselves,
* even though we don't need to.
*/
reduceh->interpolate = interpolate;
}
window_size = vips_interpolate_get_window_size( reduceh->interpolate );
window_offset =
vips_interpolate_get_window_offset( reduceh->interpolate );
if( reduceh->xshrink < 1 ) {
vips_error( class->nickname,
"%s", _( "reduce factors should be >= 1" ) );
return( -1 );
}
if( reduceh->xshrink > 2 )
vips_warn( class->nickname,
"%s", _( "reduce factor greater than 2" ) );
if( reduceh->xshrink == 1 )
return( vips_image_write( in, resample->out ) );
/* Unpack for processing.
*/
if( vips_image_decode( in, &t[0] ) )
return( -1 );
in = t[0];
/* Add new pixels around the input so we can interpolate at the edges.
*/
if( vips_embed( in, &t[1],
window_offset, 0,
in->Xsize + window_size - 1, in->Ysize,
"extend", VIPS_EXTEND_COPY,
NULL ) )
return( -1 );
in = t[1];
if( vips_image_pipelinev( resample->out,
VIPS_DEMAND_STYLE_THINSTRIP, in, NULL ) )
return( -1 );
/* Size output. Note: we round the output width down!
*
* Don't change xres/yres, leave that to the application layer. For
* example, vipsthumbnail knows the true reduce factor (including the
* fractional part), we just see the integer part here.
*/
resample->out->Xsize = (in->Xsize - window_size + 1) / reduceh->xshrink;
if( resample->out->Xsize <= 0 ) {
vips_error( class->nickname,
"%s", _( "image has shrunk to nothing" ) );
return( -1 );
}
#ifdef DEBUG
printf( "vips_reduceh_build: reducing %d x %d image to %d x %d\n",
in->Xsize, in->Ysize,
resample->out->Xsize, resample->out->Ysize );
#endif /*DEBUG*/
if( vips_image_generate( resample->out,
vips_start_one, vips_reduceh_gen, vips_stop_one,
in, reduceh ) )
return( -1 );
return( 0 );
}
static void
vips_reduceh_class_init( VipsReducehClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
VipsOperationClass *operation_class = VIPS_OPERATION_CLASS( class );
VIPS_DEBUG_MSG( "vips_reduceh_class_init\n" );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "reduceh";
vobject_class->description = _( "shrink an image horizontally" );
vobject_class->build = vips_reduceh_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;
VIPS_ARG_DOUBLE( class, "xshrink", 3,
_( "Xshrink" ),
_( "Horizontal shrink factor" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsReduceh, xshrink ),
1, 1000000, 1 );
VIPS_ARG_INTERPOLATE( class, "interpolate", 4,
_( "Interpolate" ),
_( "Interpolate pixels with this" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsReduceh, interpolate ) );
}
static void
vips_reduceh_init( VipsReduceh *reduceh )
{
}
/**
* vips_reduceh:
* @in: input image
* @out: output image
* @xshrink: horizontal reduce
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @interpolate: interpolate pixels with this, default cubich
*
* Reduce @in horizontally by a float factor. The pixels in @out are
* interpolated with a 1D cubic mask. This operation will not work well for
* a reduction of more than a factor of two.
*
* This is a very low-level operation: see vips_resize() for a more
* convenient way to resize images.
*
* This operation does not change xres or yres. The image resolution needs to
* be updated by the application.
*
* See also: vips_shrink(), vips_resize(), vips_affine().
*
* Returns: 0 on success, -1 on error
*/
int
vips_reduceh( VipsImage *in, VipsImage **out, double xshrink, ... )
{
va_list ap;
int result;
va_start( ap, xshrink );
result = vips_call_split( "reduceh", ap, in, out, xshrink );
va_end( ap );
return( result );
}

View File

@ -0,0 +1,461 @@
/* horizontal reduce by a float factor
*
* 29/1/16
* - from shrinkh.c
*/
/*
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., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vips/vips.h>
#include <vips/debug.h>
#include <vips/internal.h>
#include "presample.h"
#include "templates.h"
typedef struct _VipsReduceh {
VipsResample parent_instance;
double xshrink; /* Reduce factor */
} VipsReduceh;
typedef VipsResampleClass VipsReducehClass;
/* Precalculated interpolation matrices. int (used for pel
* sizes up to short), and double (for all others). We go to
* scale + 1 so we can round-to-nearest safely.
*/
static int vips_reduceh_matrixi[VIPS_TRANSFORM_SCALE + 1][4];
static double vips_reduceh_matrixf[VIPS_TRANSFORM_SCALE + 1][4];
/* We need C linkage for this.
*/
extern "C" {
G_DEFINE_TYPE( VipsReduceh, vips_reduceh, VIPS_TYPE_RESAMPLE );
}
template <typename T, int max_value>
static void inline
reduceh_unsigned_int_tab( VipsPel *pout, const VipsPel *pin,
const int bands, const int *cx )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int b1 = bands;
const int b2 = b1 + b1;
const int b3 = b1 + b2;
const int c0 = cx[0];
const int c1 = cx[1];
const int c2 = cx[2];
const int c3 = cx[3];
for( int z = 0; z < bands; z++ ) {
int cubich = unsigned_fixed_round(
c0 * in[0] +
c1 * in[b1] +
c2 * in[b2] +
c3 * in[b3] );
cubich = VIPS_CLIP( 0, cubich, max_value );
out[z] = cubich;
in += 1;
}
}
template <typename T, int min_value, int max_value>
static void inline
reduceh_signed_int_tab( VipsPel *pout, const VipsPel *pin,
const int bands, const int *cx )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int b1 = bands;
const int b2 = b1 + b1;
const int b3 = b1 + b2;
const int c0 = cx[0];
const int c1 = cx[1];
const int c2 = cx[2];
const int c3 = cx[3];
for( int z = 0; z < bands; z++ ) {
int cubich = signed_fixed_round(
c0 * in[0] +
c1 * in[b1] +
c2 * in[b2] +
c3 * in[b3] );
cubich = VIPS_CLIP( min_value, cubich, max_value );
out[z] = cubich;
in += 1;
}
}
/* Floating-point version, for int/float types.
*/
template <typename T>
static void inline
reduceh_float_tab( VipsPel *pout, const VipsPel *pin,
const int bands, const double *cx )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int b1 = bands;
const int b2 = b1 + b1;
const int b3 = b1 + b2;
const double c0 = cx[0];
const double c1 = cx[1];
const double c2 = cx[2];
const double c3 = cx[3];
for( int z = 0; z < bands; z++ ) {
out[z] =
c0 * in[0] +
c1 * in[1] +
c2 * in[2] +
c3 * in[3];
in += 1;
}
}
/* Ultra-high-quality version for double images.
*/
template <typename T>
static void inline
reduceh_notab( VipsPel *pout, const VipsPel *pin,
const int bands, double x )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int b1 = bands;
const int b2 = b1 + b1;
const int b3 = b1 + b2;
double cx[4];
calculate_coefficients_catmull( x, cx );
for( int z = 0; z < bands; z++ ) {
const T one = in[0];
const T two = in[b1];
const T thr = in[b2];
const T fou = in[b3];
out[z] = cubic_float<T>( one, two, thr, fou, cx );
in += 1;
}
}
static int
vips_reduceh_gen( VipsRegion *out_region, void *seq,
void *a, void *b, gboolean *stop )
{
VipsImage *in = (VipsImage *) a;
VipsReduceh *reduceh = (VipsReduceh *) b;
const int ps = VIPS_IMAGE_SIZEOF_PEL( in );
VipsRegion *ir = (VipsRegion *) seq;
VipsRect *r = &out_region->valid;
/* Double bands for complex.
*/
const int bands = in->Bands *
(vips_band_format_iscomplex( in->BandFmt ) ? 2 : 1);
VipsRect s;
#ifdef DEBUG
printf( "vips_reduceh_gen: generating %d x %d at %d x %d\n",
r->width, r->height, r->left, r->top );
#endif /*DEBUG*/
s.left = r->left * reduceh->xshrink;
s.top = r->top;
s.width = r->width * reduceh->xshrink + 3;
s.height = r->height;
if( vips_region_prepare( ir, &s ) )
return( -1 );
VIPS_GATE_START( "vips_reduceh_gen: work" );
for( int y = 0; y < r->height; y ++ ) {
VipsPel *q;
double X;
q = VIPS_REGION_ADDR( out_region, r->left, r->top + y );
X = r->left * reduceh->xshrink;
for( int x = 0; x < r->width; x++ ) {
int ix = (int) X;
VipsPel *p = VIPS_REGION_ADDR( ir, ix, r->top + y );
const int sx = X * VIPS_TRANSFORM_SCALE * 2;
const int six = sx & (VIPS_TRANSFORM_SCALE * 2 - 1);
const int tx = (six + 1) >> 1;
const int *cxi = vips_reduceh_matrixi[tx];
const double *cxf = vips_reduceh_matrixf[tx];
switch( in->BandFmt ) {
case VIPS_FORMAT_UCHAR:
reduceh_unsigned_int_tab
<unsigned char, UCHAR_MAX>(
q, p, bands, cxi );
break;
case VIPS_FORMAT_CHAR:
reduceh_signed_int_tab
<signed char, SCHAR_MIN, SCHAR_MAX>(
q, p, bands, cxi );
break;
case VIPS_FORMAT_USHORT:
reduceh_unsigned_int_tab
<unsigned short, USHRT_MAX>(
q, p, bands, cxi );
break;
case VIPS_FORMAT_SHORT:
reduceh_signed_int_tab
<signed short, SHRT_MIN, SHRT_MAX>(
q, p, bands, cxi );
break;
case VIPS_FORMAT_UINT:
reduceh_float_tab<unsigned int>(
q, p, bands, cxf );
break;
case VIPS_FORMAT_INT:
reduceh_float_tab<signed int>(
q, p, bands, cxf );
break;
case VIPS_FORMAT_FLOAT:
case VIPS_FORMAT_COMPLEX:
reduceh_float_tab<float>( q, p, bands, cxf );
break;
case VIPS_FORMAT_DOUBLE:
case VIPS_FORMAT_DPCOMPLEX:
reduceh_notab<double>( q, p, bands, X - ix );
break;
default:
g_assert_not_reached();
break;
}
X += reduceh->xshrink;
q += ps;
}
}
VIPS_GATE_STOP( "vips_reduceh_gen: work" );
return( 0 );
}
static int
vips_reduceh_build( VipsObject *object )
{
VipsObjectClass *object_class = VIPS_OBJECT_GET_CLASS( object );
VipsResample *resample = VIPS_RESAMPLE( object );
VipsReduceh *reduceh = (VipsReduceh *) object;
VipsImage **t = (VipsImage **)
vips_object_local_array( object, 2 );
VipsImage *in;
if( VIPS_OBJECT_CLASS( vips_reduceh_parent_class )->build( object ) )
return( -1 );
in = resample->in;
if( reduceh->xshrink < 1 ) {
vips_error( object_class->nickname,
"%s", _( "reduce factors should be >= 1" ) );
return( -1 );
}
if( reduceh->xshrink > 2 )
vips_warn( object_class->nickname,
"%s", _( "reduce factor greater than 2" ) );
if( reduceh->xshrink == 1 )
return( vips_image_write( in, resample->out ) );
/* Unpack for processing.
*/
if( vips_image_decode( in, &t[0] ) )
return( -1 );
in = t[0];
/* Add new pixels around the input so we can interpolate at the edges.
*/
if( vips_embed( in, &t[1],
1, 0,
in->Xsize + 3, in->Ysize,
"extend", VIPS_EXTEND_COPY,
NULL ) )
return( -1 );
in = t[1];
if( vips_image_pipelinev( resample->out,
VIPS_DEMAND_STYLE_THINSTRIP, in, NULL ) )
return( -1 );
/* Size output. Note: we round the output width down!
*
* Don't change xres/yres, leave that to the application layer. For
* example, vipsthumbnail knows the true reduce factor (including the
* fractional part), we just see the integer part here.
*/
resample->out->Xsize = (in->Xsize - 3) / reduceh->xshrink;
if( resample->out->Xsize <= 0 ) {
vips_error( object_class->nickname,
"%s", _( "image has shrunk to nothing" ) );
return( -1 );
}
#ifdef DEBUG
printf( "vips_reduceh_build: reducing %d x %d image to %d x %d\n",
in->Xsize, in->Ysize,
resample->out->Xsize, resample->out->Ysize );
#endif /*DEBUG*/
if( vips_image_generate( resample->out,
vips_start_one, vips_reduceh_gen, vips_stop_one,
in, reduceh ) )
return( -1 );
return( 0 );
}
static void
vips_reduceh_class_init( VipsReducehClass *reduceh_class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( reduceh_class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( reduceh_class );
VipsOperationClass *operation_class =
VIPS_OPERATION_CLASS( reduceh_class );
VIPS_DEBUG_MSG( "vips_reduceh_class_init\n" );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "reduceh";
vobject_class->description = _( "shrink an image horizontally" );
vobject_class->build = vips_reduceh_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;
VIPS_ARG_DOUBLE( reduceh_class, "xshrink", 3,
_( "Xshrink" ),
_( "Horizontal shrink factor" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsReduceh, xshrink ),
1, 1000000, 1 );
/* Build the tables of pre-computed coefficients.
*/
for( int x = 0; x < VIPS_TRANSFORM_SCALE + 1; x++ ) {
calculate_coefficients_catmull(
(float) x / VIPS_TRANSFORM_SCALE,
vips_reduceh_matrixf[x] );
for( int i = 0; i < 4; i++ )
vips_reduceh_matrixi[x][i] =
vips_reduceh_matrixf[x][i] *
VIPS_INTERPOLATE_SCALE;
}
}
static void
vips_reduceh_init( VipsReduceh *reduceh )
{
}
/**
* vips_reduceh:
* @in: input image
* @out: output image
* @xshrink: horizontal reduce
* @...: %NULL-terminated list of optional named arguments
*
* Reduce @in horizontally by a float factor. The pixels in @out are
* interpolated with a 1D cubic mask. This operation will not work well for
* a reduction of more than a factor of two.
*
* This is a very low-level operation: see vips_resize() for a more
* convenient way to resize images.
*
* This operation does not change xres or yres. The image resolution needs to
* be updated by the application.
*
* See also: vips_shrink(), vips_resize(), vips_affine().
*
* Returns: 0 on success, -1 on error
*/
int
vips_reduceh( VipsImage *in, VipsImage **out, double xshrink, ... )
{
va_list ap;
int result;
va_start( ap, xshrink );
result = vips_call_split( "reduceh", ap, in, out, xshrink );
va_end( ap );
return( result );
}

View File

@ -1,7 +1,7 @@
/* horizontal reduce by a float factor
/* vertical reduce by a float factor
*
* 30/10/15
* - from reducev.c
* 29/1/16
* - from shrinkv.c
*/
/*
@ -60,6 +60,11 @@ typedef struct _VipsReducev {
typedef VipsResampleClass VipsReducevClass;
/* Precalculated interpolation matrices. int (used for pel
* sizes up to short), and double (for all others). We go to
* scale + 1 so we can round-to-nearest safely.
*/
static int vips_reducev_matrixi[VIPS_TRANSFORM_SCALE + 1][4];
static double vips_reducev_matrixf[VIPS_TRANSFORM_SCALE + 1][4];
@ -82,13 +87,17 @@ reducev_unsigned_int_tab( VipsPel *pout, const VipsPel *pin,
const int l2 = l1 + l1;
const int l3 = l1 + l2;
for( int z = 0; z < ne; z++ ) {
const T uno = in[0];
const T dos = in[l1];
const T tre = in[l2];
const T qua = in[l3];
const int c0 = cy[0];
const int c1 = cy[1];
const int c2 = cy[2];
const int c3 = cy[3];
int cubicv = cubic_unsigned_int<T>( uno, dos, tre, qua, cy );
for( int z = 0; z < ne; z++ ) {
int cubicv = unsigned_fixed_round(
c0 * in[0] +
c1 * in[l1] +
c2 * in[l2] +
c3 * in[l3] );
cubicv = VIPS_CLIP( 0, cubicv, max_value );
@ -111,13 +120,17 @@ reducev_signed_int_tab( VipsPel *pout, const VipsPel *pin,
const int l2 = l1 + l1;
const int l3 = l1 + l2;
for( int z = 0; z < ne; z++ ) {
const T uno = in[0];
const T dos = in[l1];
const T tre = in[l2];
const T qua = in[l3];
const int c0 = cy[0];
const int c1 = cy[1];
const int c2 = cy[2];
const int c3 = cy[3];
int cubicv = cubic_signed_int<T>( uno, dos, tre, qua, cy );
for( int z = 0; z < ne; z++ ) {
int cubicv = signed_fixed_round(
c0 * in[0] +
c1 * in[l1] +
c2 * in[l2] +
c3 * in[l3] );
cubicv = VIPS_CLIP( min_value, cubicv, max_value );
@ -142,13 +155,17 @@ reducev_float_tab( VipsPel *pout, const VipsPel *pin,
const int l2 = l1 + l1;
const int l3 = l1 + l2;
for( int z = 0; z < ne; z++ ) {
const T uno = in[0];
const T dos = in[l1];
const T tre = in[l2];
const T qua = in[l3];
const double c0 = cy[0];
const double c1 = cy[1];
const double c2 = cy[2];
const double c3 = cy[3];
out[z] = cubic_float<T>( uno, dos, tre, qua, cy );
for( int z = 0; z < ne; z++ ) {
out[z] =
c0 * in[0] +
c1 * in[l1] +
c2 * in[l2] +
c3 * in[l3];
in += 1;
}
@ -173,13 +190,17 @@ reducev_notab( VipsPel *pout, const VipsPel *pin,
calculate_coefficients_catmull( y, cy );
for( int z = 0; z < ne; z++ ) {
const T uno = in[0];
const T dos = in[l1];
const T tre = in[l2];
const T qua = in[l3];
const double c0 = cy[0];
const double c1 = cy[1];
const double c2 = cy[2];
const double c3 = cy[3];
out[z] = cubic_float<T>( uno, dos, tre, qua, cy );
for( int z = 0; z < ne; z++ ) {
out[z] =
c0 * in[0] +
c1 * in[l1] +
c2 * in[l2] +
c3 * in[l3];
in += 1;
}
@ -201,7 +222,6 @@ vips_reducev_gen( VipsRegion *out_region, void *seq,
int ne = r->width * bands;
VipsRect s;
int y;
#ifdef DEBUG
printf( "vips_reducev_gen: generating %d x %d at %d x %d\n",
@ -217,7 +237,7 @@ vips_reducev_gen( VipsRegion *out_region, void *seq,
VIPS_GATE_START( "vips_reducev_gen: work" );
for( y = 0; y < r->height; y ++ ) {
for( int y = 0; y < r->height; y ++ ) {
VipsPel *q = VIPS_REGION_ADDR( out_region, r->left, r->top + y );
const double Y = (r->top + y) * reducev->yshrink;
VipsPel *p = VIPS_REGION_ADDR( ir, r->left, (int) Y );