add cubich interp., start reduceh

This commit is contained in:
John Cupitt 2016-01-27 11:12:36 +00:00
parent 65930cf866
commit 8b09c86492
8 changed files with 799 additions and 466 deletions

View File

@ -43,8 +43,11 @@ int vips_shrink( VipsImage *in, VipsImage **out,
__attribute__((sentinel));
int vips_shrinkh( VipsImage *in, VipsImage **out, int xshrink, ... );
int vips_shrinkv( VipsImage *in, VipsImage **out, int yshrink, ... );
int vips_shrink2( VipsImage *in, VipsImage **out,
int vips_reduce( VipsImage *in, VipsImage **out,
double xshrink, double yshrink, ... );
int vips_reduceh( VipsImage *in, VipsImage **out, int xshrink, ... );
int vips_reducev( VipsImage *in, VipsImage **out, int yshrink, ... );
int vips_similarity( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));

View File

@ -9,10 +9,10 @@ libresample_la_SOURCES = \
shrink.c \
shrinkh.c \
shrinkv.c \
shrink2.c \
interpolate.c \
transform.c \
bicubic.cpp \
bicubich.cpp \
lbb.cpp \
nohalo.cpp \
vsqbs.cpp \

View File

@ -0,0 +1,380 @@
/* 1D horizontal bicubich (catmull-rom) interpolator
*
* 26/1/16
* - from bicubich.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
*/
/* Bicubich (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_BICUBICH \
(vips_interpolate_bicubich_get_type())
#define VIPS_INTERPOLATE_BICUBICH( obj ) \
(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
VIPS_TYPE_INTERPOLATE_BICUBICH, VipsInterpolateBicubich ))
#define VIPS_INTERPOLATE_BICUBICH_CLASS( klass ) \
(G_TYPE_CHECK_CLASS_CAST( (klass), \
VIPS_TYPE_INTERPOLATE_BICUBICH, VipsInterpolateBicubichClass))
#define VIPS_IS_INTERPOLATE_BICUBICH( obj ) \
(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_INTERPOLATE_BICUBICH ))
#define VIPS_IS_INTERPOLATE_BICUBICH_CLASS( klass ) \
(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_INTERPOLATE_BICUBICH ))
#define VIPS_INTERPOLATE_BICUBICH_GET_CLASS( obj ) \
(G_TYPE_INSTANCE_GET_CLASS( (obj), \
VIPS_TYPE_INTERPOLATE_BICUBICH, VipsInterpolateBicubichClass ))
typedef VipsInterpolate VipsInterpolateBicubich;
typedef VipsInterpolateClass VipsInterpolateBicubichClass;
/* 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_bicubich_matrixi[VIPS_TRANSFORM_SCALE + 1][4];
static double vips_bicubich_matrixf[VIPS_TRANSFORM_SCALE + 1][4];
/* We need C linkage for this.
*/
extern "C" {
G_DEFINE_TYPE( VipsInterpolateBicubich, vips_interpolate_bicubich,
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
bicubich_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 bicubich = bicubic1d_unsigned_int<T>(
one, two, thr, fou, cx );
bicubich = VIPS_CLIP( 0, bicubich, max_value );
out[z] = bicubich;
in += 1;
}
}
template <typename T, int min_value, int max_value>
static void inline
bicubich_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 bicubich = bicubic1d_signed_int<T>(
one, two, thr, fou, cx );
bicubich = VIPS_CLIP( min_value, bicubich, max_value );
out[z] = bicubich;
in += 1;
}
}
/* Floating-point version, for int/float types.
*/
template <typename T>
static void inline
bicubich_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];
const T bicubich = bicubic1d_float<T>(
one, two, thr, fou, cx );
out[z] = bicubich;
in += 1;
}
}
/* Ultra-high-quality version for double images.
*/
template <typename T>
static void inline
bicubich_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];
const T bicubich = bicubic1d_float<T>(
one, two, thr, fou, cx );
out[z] = bicubich;
in += 1;
}
}
static void
vips_interpolate_bicubich_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_bicubich_matrixi[tx];
const double *cxf = vips_bicubich_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_bicubich_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:
bicubich_unsigned_int_tab<unsigned char, UCHAR_MAX>(
out, p, bands, cxi );
/*
Handy for benchmarking
bicubich_float_tab<unsigned char>(
out, p, bands, cxf );
bicubich_notab<unsigned char>(
out, p, bands, x - ix );
*/
break;
case VIPS_FORMAT_CHAR:
bicubich_signed_int_tab<signed char, SCHAR_MIN, SCHAR_MAX>(
out, p, bands, cxi );
break;
case VIPS_FORMAT_USHORT:
bicubich_unsigned_int_tab<unsigned short, USHRT_MAX>(
out, p, bands, cxi );
break;
case VIPS_FORMAT_SHORT:
bicubich_signed_int_tab<signed short, SHRT_MIN, SHRT_MAX>(
out, p, bands, cxi );
break;
case VIPS_FORMAT_UINT:
bicubich_float_tab<unsigned int>(
out, p, bands, cxf );
break;
case VIPS_FORMAT_INT:
bicubich_float_tab<signed int>(
out, p, bands, cxf );
break;
case VIPS_FORMAT_FLOAT:
bicubich_float_tab<float>(
out, p, bands, cxf );
break;
case VIPS_FORMAT_DOUBLE:
bicubich_notab<double>(
out, p, bands, x - ix );
break;
case VIPS_FORMAT_COMPLEX:
bicubich_float_tab<float>(
out, p, bands * 2, cxf );
break;
case VIPS_FORMAT_DPCOMPLEX:
bicubich_notab<double>(
out, p, bands * 2, x - ix );
break;
default:
break;
}
}
static void
vips_interpolate_bicubich_class_init( VipsInterpolateBicubichClass *iclass )
{
VipsObjectClass *object_class = VIPS_OBJECT_CLASS( iclass );
VipsInterpolateClass *interpolate_class =
VIPS_INTERPOLATE_CLASS( iclass );
object_class->nickname = "bicubich";
object_class->description =
_( "horizontal bicubic interpolation (Catmull-Rom)" );
interpolate_class->interpolate = vips_interpolate_bicubich_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_bicubich_matrixf[x] );
for( int i = 0; i < 4; i++ )
vips_bicubich_matrixi[x][i] =
vips_bicubich_matrixf[x][i] *
VIPS_INTERPOLATE_SCALE;
}
}
static void
vips_interpolate_bicubich_init( VipsInterpolateBicubich *bicubich )
{
#ifdef DEBUG
printf( "vips_interpolate_bicubich_init: " );
vips_object_print( VIPS_OBJECT( bicubich ) );
#endif /*DEBUG*/
}

View File

@ -600,6 +600,7 @@ void
vips__interpolate_init( void )
{
extern GType vips_interpolate_bicubic_get_type( void );
extern GType vips_interpolate_bicubich_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 );
@ -608,6 +609,7 @@ vips__interpolate_init( void )
vips_interpolate_bilinear_get_type();
vips_interpolate_bicubic_get_type();
vips_interpolate_bicubich_get_type();
vips_interpolate_lbb_get_type();
vips_interpolate_nohalo_get_type();
vips_interpolate_vsqbs_get_type();

372
libvips/resample/reduceh.c Normal file
View File

@ -0,0 +1,372 @@
/* 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 xreduce; /* Reduce factor */
VipsInterpolate *interpolate;
} VipsReduceh;
typedef VipsResampleClass VipsReducehClass;
G_DEFINE_TYPE( VipsReduceh, vips_reduceh, VIPS_TYPE_RESAMPLE );
#define INNER( BANDS ) \
sum += p[x1]; \
x1 += BANDS;
/* Integer reduce.
*/
#define IREDUCE( TYPE, BANDS ) { \
TYPE * restrict p = (TYPE *) in; \
TYPE * restrict q = (TYPE *) out; \
\
for( x = 0; x < width; x++ ) { \
for( b = 0; b < BANDS; b++ ) { \
int sum; \
\
sum = 0; \
x1 = b; \
VIPS_UNROLL( reduce->xreduce, INNER( BANDS ) ); \
q[b] = (sum + reduce->xreduce / 2) / \
reduce->xreduce; \
} \
p += ne; \
q += BANDS; \
} \
}
/* Float reduce.
*/
#define FREDUCE( TYPE ) { \
TYPE * restrict p = (TYPE *) in; \
TYPE * restrict q = (TYPE *) out; \
\
for( x = 0; x < width; x++ ) { \
for( b = 0; b < bands; b++ ) { \
double sum; \
\
sum = 0.0; \
x1 = b; \
VIPS_UNROLL( reduce->xreduce, INNER( bands ) ); \
q[b] = sum / reduce->xreduce; \
} \
p += ne; \
q += bands; \
} \
}
/* Generate an line of @or. @ir is large enough.
*/
static void
vips_reduceh_gen2( VipsReduceh *reduce, VipsRegion *or, VipsRegion *ir,
int left, int top, int width )
{
VipsResample *resample = VIPS_RESAMPLE( reduce );
const int bands = resample->in->Bands *
(vips_band_format_iscomplex( resample->in->BandFmt ) ?
2 : 1);
const int ne = reduce->xreduce * bands;
VipsPel *out = VIPS_REGION_ADDR( or, left, top );
VipsPel *in = VIPS_REGION_ADDR( ir, left * reduce->xreduce, top );
int x;
int x1, b;
switch( resample->in->BandFmt ) {
IREDUCE( unsigned char, bands ); break;
case VIPS_FORMAT_CHAR:
IREDUCE( char, bands ); break;
case VIPS_FORMAT_USHORT:
IREDUCE( unsigned short, bands ); break;
case VIPS_FORMAT_SHORT:
IREDUCE( short, bands ); break;
case VIPS_FORMAT_UINT:
IREDUCE( unsigned int, bands ); break;
case VIPS_FORMAT_INT:
IREDUCE( int, bands ); break;
case VIPS_FORMAT_FLOAT:
FREDUCE( float ); break;
case VIPS_FORMAT_DOUBLE:
FREDUCE( double ); break;
case VIPS_FORMAT_COMPLEX:
FREDUCE( float ); break;
case VIPS_FORMAT_DPCOMPLEX:
FREDUCE( double ); break;
default:
g_assert_not_reached();
}
}
static int
vips_reduceh_gen( VipsRegion *or, void *seq,
void *a, void *b, gboolean *stop )
{
VipsReduceh *reduce = (VipsReduceh *) b;
VipsRegion *ir = (VipsRegion *) seq;
VipsRect *r = &or->valid;
int y;
/* How do we chunk up the image? We don't want to prepare the whole of
* the input region corresponding to *r since it could be huge.
*
* Request input a line at a time.
*/
#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*/
for( y = 0; y < r->height; y ++ ) {
VipsRect s;
s.left = r->left * reduce->xreduce;
s.top = r->top + y;
s.width = r->width * reduce->xreduce;
s.height = 1;
#ifdef DEBUG
printf( "reduceh_gen: requesting line %d\n", s.top );
#endif /*DEBUG*/
if( vips_region_prepare( ir, &s ) )
return( -1 );
VIPS_GATE_START( "vips_reduceh_gen: work" );
vips_reduceh_gen2( reduce, or, ir,
r->left, r->top + y, r->width );
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 *reduce = (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( reduce->xreduce < 1 ) {
vips_error( class->nickname,
"%s", _( "reduce factors should be >= 1" ) );
return( -1 );
}
if( reduce->xreduce > 2 )
vips_warn( class->nickname,
"%s", _( "reduce factor greater than 2" ) );
if( reduce->xreduce == 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];
/* THINSTRIP will work, anything else will break seq mode. If you
* combine reduce with conv you'll need to use a line cache to maintain
* sequentiality.
*/
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 / reduce->xreduce;
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, reduce ) )
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 = _( "reduce an image horizontally" );
vobject_class->build = vips_reduceh_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;
VIPS_ARG_DOUBLE( class, "xreduce", 3,
_( "Xreduce" ),
_( "Horizontal reduce factor" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsReduceh, xreduce ),
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 *reduce )
{
}
/**
* vips_reduceh:
* @in: input image
* @out: output image
* @xreduce: 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 bicubic 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 xreduce, ... )
{
va_list ap;
int result;
va_start( ap, xreduce );
result = vips_call_split( "reduceh", ap, in, out, xreduce );
va_end( ap );
return( result );
}

View File

@ -111,11 +111,13 @@ vips_resample_init( VipsResample *resample )
void
vips_resample_operation_init( void )
{
extern GType vips_shrink_get_type( void );
extern GType vips_mapim_get_type( void );
extern GType vips_shrink_get_type( void );
extern GType vips_shrinkh_get_type( void );
extern GType vips_shrinkv_get_type( void );
extern GType vips_shrink2_get_type( void );
extern GType vips_reduce_get_type( void );
extern GType vips_reduceh_get_type( void );
extern GType vips_reducev_get_type( void );
extern GType vips_quadratic_get_type( void );
extern GType vips_affine_get_type( void );
extern GType vips_similarity_get_type( void );
@ -125,7 +127,7 @@ vips_resample_operation_init( void )
vips_shrink_get_type();
vips_shrinkh_get_type();
vips_shrinkv_get_type();
vips_shrink2_get_type();
vips_reduceh_get_type();
vips_quadratic_get_type();
vips_affine_get_type();
vips_similarity_get_type();

View File

@ -1,461 +0,0 @@
/* shrink with a box filter
*
* Copyright: 1990, N. Dessipris.
*
* Authors: Nicos Dessipris and Kirk Martinez
* Written on: 29/04/1991
* Modified on: 2/11/92, 22/2/93 Kirk Martinez - Xres Yres & cleanup
incredibly inefficient for box filters as LUTs are used instead of +
Needs converting to a smoother filter: eg Gaussian! KM
* 15/7/93 JC
* - rewritten for partial v2
* - ANSIfied
* - now shrinks any non-complex type
* - no longer cloned from im_convsub()
* - could be much better! see km comments above
* 3/8/93 JC
* - rounding bug fixed
* 11/1/94 JC
* - problems with .000001 and round up/down ignored! Try shrink 3738
* pixel image by 9.345000000001
* 7/10/94 JC
* - IM_NEW and IM_ARRAY added
* - more typedef
* 3/7/95 JC
* - IM_CODING_LABQ handling added here
* 20/12/08
* - fall back to im_copy() for 1/1 shrink
* 2/2/11
* - gtk-doc
* 10/2/12
* - shrink in chunks to reduce peak memuse for large shrinks
* - simpler
* 12/6/12
* - redone as a class
* - warn about non-int shrinks
* - some tuning .. tried an int coordinate path, not worthwhile
* 16/11/12
* - don't change xres/yres, see comment below
* 8/4/13
* - oops demand_hint was incorrect, thanks Jan
* 6/6/13
* - don't chunk horizontally, fixes seq problems with large shrink
* factors
*/
/*
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 _VipsShrink2 {
VipsResample parent_instance;
double xshrink; /* Shrink factors */
double yshrink;
int mw; /* Size of area we average */
int mh;
int np; /* Number of pels we average */
} VipsShrink2;
typedef VipsResampleClass VipsShrink2Class;
G_DEFINE_TYPE( VipsShrink2, vips_shrink2, VIPS_TYPE_RESAMPLE );
/* Our per-sequence parameter struct. Somewhere to sum band elements.
*/
typedef struct {
VipsRegion *ir;
VipsPel *sum;
} VipsShrink2Sequence;
/* Free a sequence value.
*/
static int
vips_shrink2_stop( void *vseq, void *a, void *b )
{
VipsShrink2Sequence *seq = (VipsShrink2Sequence *) vseq;
VIPS_FREEF( g_object_unref, seq->ir );
return( 0 );
}
/* Make a sequence value.
*/
static void *
vips_shrink2_start( VipsImage *out, void *a, void *b )
{
VipsImage *in = (VipsImage *) a;
VipsShrink2 *shrink = (VipsShrink2 *) b;
VipsShrink2Sequence *seq;
if( !(seq = VIPS_NEW( out, VipsShrink2Sequence )) )
return( NULL );
seq->ir = vips_region_new( in );
if( !(seq->sum = (VipsPel *) VIPS_ARRAY( out, in->Bands, double )) ) {
vips_shrink2_stop( seq, in, shrink );
return( NULL );
}
return( (void *) seq );
}
/* Integer shrink.
*/
#define ISHRINK( TYPE ) { \
int *sum = (int *) seq->sum; \
TYPE *p = (TYPE *) in; \
TYPE *q = (TYPE *) out; \
\
for( b = 0; b < bands; b++ ) \
sum[b] = 0; \
\
for( y1 = 0; y1 < shrink->mh; y1++ ) { \
for( i = 0, x1 = 0; x1 < shrink->mw; x1++ ) \
for( b = 0; b < bands; b++, i++ ) \
sum[b] += p[i]; \
\
p += ls; \
} \
\
for( b = 0; b < bands; b++ ) \
q[b] = (sum[b] + shrink->np / 2) / shrink->np; \
}
/* Float shrink.
*/
#define FSHRINK( TYPE ) { \
double *sum = (double *) seq->sum; \
TYPE *p = (TYPE *) in; \
TYPE *q = (TYPE *) out; \
\
for( b = 0; b < bands; b++ ) \
sum[b] = 0.0; \
\
for( y1 = 0; y1 < shrink->mh; y1++ ) { \
for( i = 0, x1 = 0; x1 < shrink->mw; x1++ ) \
for( b = 0; b < bands; b++, i++ ) \
sum[b] += p[i]; \
\
p += ls; \
} \
\
for( b = 0; b < bands; b++ ) \
q[b] = sum[b] / shrink->np; \
}
/* Generate an area of @or. @ir is large enough.
*/
static void
vips_shrink2_gen2( VipsShrink2 *shrink, VipsShrink2Sequence *seq,
VipsRegion *or, VipsRegion *ir,
int left, int top, int width, int height )
{
VipsResample *resample = VIPS_RESAMPLE( shrink );
const int bands = resample->in->Bands;
const int sizeof_pixel = VIPS_IMAGE_SIZEOF_PEL( resample->in );
const int ls = VIPS_REGION_LSKIP( ir ) /
VIPS_IMAGE_SIZEOF_ELEMENT( resample->in );
int x, y, i;
int x1, y1, b;
for( y = 0; y < height; y++ ) {
VipsPel *out = VIPS_REGION_ADDR( or, left, top + y );
for( x = 0; x < width; x++ ) {
int ix = (left + x) * shrink->xshrink;
int iy = (top + y) * shrink->yshrink;
VipsPel *in = VIPS_REGION_ADDR( ir, ix, iy );
switch( resample->in->BandFmt ) {
case VIPS_FORMAT_UCHAR:
ISHRINK( unsigned char ); break;
case VIPS_FORMAT_CHAR:
ISHRINK( char ); break;
case VIPS_FORMAT_USHORT:
ISHRINK( unsigned short ); break;
case VIPS_FORMAT_SHORT:
ISHRINK( short ); break;
case VIPS_FORMAT_UINT:
ISHRINK( unsigned int ); break;
case VIPS_FORMAT_INT:
ISHRINK( int ); break;
case VIPS_FORMAT_FLOAT:
FSHRINK( float ); break;
case VIPS_FORMAT_DOUBLE:
FSHRINK( double ); break;
default:
g_assert_not_reached();
}
out += sizeof_pixel;
}
}
}
static int
vips_shrink2_gen( VipsRegion *or, void *vseq, void *a, void *b, gboolean *stop )
{
VipsShrink2Sequence *seq = (VipsShrink2Sequence *) vseq;
VipsShrink2 *shrink = (VipsShrink2 *) b;
VipsRegion *ir = seq->ir;
VipsRect *r = &or->valid;
/* How do we chunk up the image? We don't want to prepare the whole of
* the input region corresponding to *r since it could be huge.
*
* Each pixel of *r will depend on roughly mw x mh
* pixels, so we walk *r in chunks which map to the tile size.
*
* Make sure we can't ask for a zero step.
*
* We don't chunk horizontally. We want "vips shrink x.jpg b.jpg 100
* 100" to run sequentially. If we chunk horizontally, we will fetch
* 100x100 lines from the top of the image, then 100x100 100 lines
* down, etc. for each thread, then when they've finished, fetch
* 100x100, 100 pixels across from the top of the image. This will
* break sequentiality.
*/
int ystep = shrink->mh > VIPS__TILE_HEIGHT ?
1 : VIPS__TILE_HEIGHT / shrink->mh;
int y;
#ifdef DEBUG
printf( "vips_shrink2_gen: generating %d x %d at %d x %d\n",
r->width, r->height, r->left, r->top );
#endif /*DEBUG*/
for( y = 0; y < r->height; y += ystep ) {
/* Clip the this rect against the demand size.
*/
int height = VIPS_MIN( ystep, r->height - y );
VipsRect s;
s.left = r->left * shrink->xshrink;
s.top = (r->top + y) * shrink->yshrink;
s.width = VIPS_CEIL( r->width * shrink->xshrink );
s.height = VIPS_CEIL( height * shrink->yshrink );
#ifdef DEBUG
printf( "shrink_gen: requesting %d x %d at %d x %d\n",
s.width, s.height, s.left, s.top );
#endif /*DEBUG*/
if( vips_region_prepare( ir, &s ) )
return( -1 );
VIPS_GATE_START( "vips_shrink2_gen: work" );
vips_shrink2_gen2( shrink, seq,
or, ir,
r->left, r->top + y, r->width, height );
VIPS_GATE_STOP( "vips_shrink2_gen: work" );
}
return( 0 );
}
static int
vips_shrink2_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsResample *resample = VIPS_RESAMPLE( object );
VipsShrink2 *shrink = (VipsShrink2 *) object;
VipsImage **t = (VipsImage **)
vips_object_local_array( object, 1 );
VipsImage *in;
if( VIPS_OBJECT_CLASS( vips_shrink2_parent_class )->build( object ) )
return( -1 );
shrink->mw = VIPS_CEIL( shrink->xshrink );
shrink->mh = VIPS_CEIL( shrink->yshrink );
shrink->np = shrink->mw * shrink->mh;
in = resample->in;
if( vips_check_noncomplex( class->nickname, in ) )
return( -1 );
if( shrink->xshrink < 1.0 ||
shrink->yshrink < 1.0 ) {
vips_error( class->nickname,
"%s", _( "shrink factors should be >= 1" ) );
return( -1 );
}
if( (int) shrink->xshrink != shrink->xshrink ||
(int) shrink->yshrink != shrink->yshrink )
vips_warn( class->nickname,
"%s", _( "not integer shrink factors, "
"expect poor results" ) );
if( shrink->xshrink == 1.0 &&
shrink->yshrink == 1.0 )
return( vips_image_write( in, resample->out ) );
/* Unpack for processing.
*/
if( vips_image_decode( in, &t[0] ) )
return( -1 );
in = t[0];
/* THINSTRIP will work, anything else will break seq mode. If you
* combine shrink with conv you'll need to use a line cache to maintain
* sequentiality.
*/
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 shrink factor (including the
* fractional part), we just see the integer part here.
*/
resample->out->Xsize = in->Xsize / shrink->xshrink;
resample->out->Ysize = in->Ysize / shrink->yshrink;
if( resample->out->Xsize <= 0 ||
resample->out->Ysize <= 0 ) {
vips_error( class->nickname,
"%s", _( "image has shrunk to nothing" ) );
return( -1 );
}
#ifdef DEBUG
printf( "vips_shrink2_build: shrinking %d x %d image to %d x %d\n",
in->Xsize, in->Ysize,
resample->out->Xsize, resample->out->Ysize );
printf( "vips_shrink2_build: %d x %d block average\n",
shrink->mw, shrink->mh );
#endif /*DEBUG*/
if( vips_image_generate( resample->out,
vips_shrink2_start, vips_shrink2_gen, vips_shrink2_stop,
in, shrink ) )
return( -1 );
return( 0 );
}
static void
vips_shrink2_class_init( VipsShrink2Class *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_shrink2_class_init\n" );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "shrink2";
vobject_class->description = _( "shrink an image" );
vobject_class->build = vips_shrink2_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL;
VIPS_ARG_DOUBLE( class, "xshrink", 8,
_( "Xshrink" ),
_( "Horizontal shrink factor" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsShrink2, xshrink ),
1.0, 1000000, 1 );
VIPS_ARG_DOUBLE( class, "yshrink", 9,
_( "Yshrink" ),
_( "Vertical shrink factor" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsShrink2, yshrink ),
1.0, 1000000, 1 );
}
static void
vips_shrink2_init( VipsShrink2 *shrink )
{
}
/**
* vips_shrink2:
* @in: input image
* @out: output image
* @xshrink: horizontal shrink
* @yshrink: vertical shrink
* @...: %NULL-terminated list of optional named arguments
*
* Shrink @in by a pair of factors with a simple box filter.
*
* This is an old version of vips_shrink() kept around for testing. Use
* vips_shrink() in preference.
*
* See also: vips_resize(), vips_affine().
*
* Returns: 0 on success, -1 on error
*/
int
vips_shrink2( VipsImage *in, VipsImage **out,
double xshrink, double yshrink, ... )
{
va_list ap;
int result;
va_start( ap, yshrink );
result = vips_call_split( "shrink2", ap, in, out, xshrink, yshrink );
va_end( ap );
return( result );
}

View File

@ -195,6 +195,18 @@ bicubic_unsigned_int(
cy[3] * r3 ) );
}
template <typename T> static int inline
bicubic1d_unsigned_int(
const T one, const T two, const T thr, const T fou,
const int* restrict cx )
{
return( unsigned_fixed_round(
cx[0] * one +
cx[1] * two +
cx[2] * thr +
cx[3] * fou ) );
}
static int inline
signed_fixed_round( int v )
{
@ -245,6 +257,18 @@ bicubic_signed_int(
cy[3] * r3 ) );
}
template <typename T> static int inline
bicubic1d_signed_int(
const T one, const T two, const T thr, const T fou,
const int* restrict cx )
{
return( signed_fixed_round(
cx[0] * one +
cx[1] * two +
cx[2] * thr +
cx[3] * fou ) );
}
/* Floating-point bicubic, used for int/float/double types.
*/
template <typename T> static T inline
@ -277,6 +301,17 @@ bicubic_float(
cx[3] * qua_fou) );
}
template <typename T> static T inline
bicubic1d_float(
const T one, const T two, const T thr, const T fou,
const double* restrict cx )
{
return( cx[0] * one +
cx[1] * two +
cx[2] * thr +
cx[3] * fou );
}
/* Given an offset in [0,1] (we can have x == 1 when building tables),
* calculate c0, c1, c2, c3, the catmull-rom coefficients. This is called
* from the interpolator as well as from the table builder.