From fe7bd9bdcc384dca56ee9c37f54982ae91311c68 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Wed, 13 Aug 2014 13:57:52 +0100 Subject: [PATCH] add vips_resize() a wrapper over affine that just scales ... much faster from the command-line, since it can set SEQ mode --- ChangeLog | 1 + libvips/create/gaussmat.c | 4 +- libvips/include/vips/resample.h | 3 + libvips/resample/Makefile.am | 1 + libvips/resample/affine.c | 2 +- libvips/resample/resample.c | 2 + libvips/resample/resize.c | 196 ++++++++++++++++++++++++++++++++ libvips/resample/similarity.c | 4 +- 8 files changed, 208 insertions(+), 5 deletions(-) create mode 100644 libvips/resample/resize.c diff --git a/ChangeLog b/ChangeLog index a7cf43c5..0744dbc9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 25/7/14 started 7.41.0 - start working on --disable-deprecated +- add vips_resize() 12/8/14 started 7.40.5 - more doc fixes diff --git a/libvips/create/gaussmat.c b/libvips/create/gaussmat.c index e1f624cf..145f5821 100644 --- a/libvips/create/gaussmat.c +++ b/libvips/create/gaussmat.c @@ -157,8 +157,8 @@ vips_gaussmat_class_init( VipsGaussmatClass *class ) vobject_class->build = vips_gaussmat_build; VIPS_ARG_DOUBLE( class, "sigma", 2, - _( "Radius" ), - _( "Radius of Gaussian" ), + _( "Sigma" ), + _( "Sigma of Gaussian" ), VIPS_ARGUMENT_REQUIRED_INPUT, G_STRUCT_OFFSET( VipsGaussmat, sigma ), 0.000001, 10000.0, 1.0 ); diff --git a/libvips/include/vips/resample.h b/libvips/include/vips/resample.h index e692952c..bee29ea9 100644 --- a/libvips/include/vips/resample.h +++ b/libvips/include/vips/resample.h @@ -43,6 +43,9 @@ int vips_shrink( VipsImage *in, VipsImage **out, __attribute__((sentinel)); int vips_similarity( VipsImage *in, VipsImage **out, ... ) __attribute__((sentinel)); +int vips_resize( VipsImage *in, VipsImage **out, + double h_scale, double v_scale, ... ) + __attribute__((sentinel)); int vips_affine( VipsImage *in, VipsImage **out, double a, double b, double c, double d, ... ) __attribute__((sentinel)); diff --git a/libvips/resample/Makefile.am b/libvips/resample/Makefile.am index c0e61caf..24228c1e 100644 --- a/libvips/resample/Makefile.am +++ b/libvips/resample/Makefile.am @@ -9,6 +9,7 @@ libresample_la_SOURCES = \ quadratic.c \ resample.c \ similarity.c \ + resize.c \ presample.h \ shrink.c \ interpolate.c \ diff --git a/libvips/resample/affine.c b/libvips/resample/affine.c index d01e3cd5..6123ad8f 100644 --- a/libvips/resample/affine.c +++ b/libvips/resample/affine.c @@ -1,4 +1,4 @@ -/* im_affine() ... affine transform with a supplied interpolator. +/* affine transform with a supplied interpolator. * * * Copyright N. Dessipris diff --git a/libvips/resample/resample.c b/libvips/resample/resample.c index f6718feb..9c59bdcc 100644 --- a/libvips/resample/resample.c +++ b/libvips/resample/resample.c @@ -115,10 +115,12 @@ vips_resample_operation_init( void ) extern GType vips_quadratic_get_type( void ); extern GType vips_affine_get_type( void ); extern GType vips_similarity_get_type( void ); + extern GType vips_resize_get_type( void ); vips_shrink_get_type(); vips_quadratic_get_type(); vips_affine_get_type(); vips_similarity_get_type(); + vips_resize_get_type(); } diff --git a/libvips/resample/resize.c b/libvips/resample/resize.c new file mode 100644 index 00000000..9b6b757d --- /dev/null +++ b/libvips/resample/resize.c @@ -0,0 +1,196 @@ +/* resize an image ... a simple wrapper over affine + * + * 13/8/14 + * - from affine.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_VERBOSE +#define DEBUG + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "presample.h" + +typedef struct _VipsResize { + VipsResample parent_instance; + + double h_scale; + double v_scale; + VipsInterpolate *interpolate; + double idx; + double idy; + +} VipsResize; + +typedef VipsResampleClass VipsResizeClass; + +G_DEFINE_TYPE( VipsResize, vips_resize, VIPS_TYPE_RESAMPLE ); + +static int +vips_resize_build( VipsObject *object ) +{ + VipsResample *resample = VIPS_RESAMPLE( object ); + VipsResize *resize = (VipsResize *) object; + + VipsImage **t = (VipsImage **) + vips_object_local_array( object, 4 ); + + double a, b, c, d; + + if( VIPS_OBJECT_CLASS( vips_resize_parent_class )->build( object ) ) + return( -1 ); + + a = resize->h_scale; + b = 0.0; + c = 0.0; + d = resize->v_scale; + + if( vips_affine( resample->in, &t[0], a, b, c, d, + "interpolate", resize->interpolate, + "idx", resize->idx, + "idy", resize->idy, + NULL ) || + vips_image_write( t[0], resample->out ) ) + return( -1 ); + + return( 0 ); +} + +static void +vips_resize_class_init( VipsResizeClass *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_resize_class_init\n" ); + + gobject_class->set_property = vips_object_set_property; + gobject_class->get_property = vips_object_get_property; + + vobject_class->nickname = "resize"; + vobject_class->description = _( "resize an image" ); + vobject_class->build = vips_resize_build; + + operation_class->flags = VIPS_OPERATION_SEQUENTIAL; + + VIPS_ARG_DOUBLE( class, "h_scale", 113, + _( "Horizontal scale factor" ), + _( "Scale image by this factor in the horizontal axis" ), + VIPS_ARGUMENT_REQUIRED_INPUT, + G_STRUCT_OFFSET( VipsResize, h_scale ), + 0, 10000000, 0 ); + + VIPS_ARG_DOUBLE( class, "v_scale", 114, + _( "Vertical scale factor" ), + _( "Scale image by this factor in the vertical axis" ), + VIPS_ARGUMENT_REQUIRED_INPUT, + G_STRUCT_OFFSET( VipsResize, v_scale ), + 0, 10000000, 0 ); + + VIPS_ARG_INTERPOLATE( class, "interpolate", 2, + _( "Interpolate" ), + _( "Interpolate pixels with this" ), + VIPS_ARGUMENT_OPTIONAL_INPUT, + G_STRUCT_OFFSET( VipsResize, interpolate ) ); + + VIPS_ARG_DOUBLE( class, "idx", 115, + _( "Input offset" ), + _( "Horizontal input displacement" ), + VIPS_ARGUMENT_OPTIONAL_INPUT, + G_STRUCT_OFFSET( VipsResize, idx ), + -10000000, 10000000, 0 ); + + VIPS_ARG_DOUBLE( class, "idy", 116, + _( "Input offset" ), + _( "Vertical input displacement" ), + VIPS_ARGUMENT_OPTIONAL_INPUT, + G_STRUCT_OFFSET( VipsResize, idy ), + -10000000, 10000000, 0 ); +} + +static void +vips_resize_init( VipsResize *resize ) +{ +} + +/** + * vips_resize: + * @in: input image + * @out: output image + * @h_scale: horizontal scale factor + * @v_scale: vertical scale factor + * + * Optional arguments: + * + * @interpolate: interpolate pixels with this + * @idx: input horizontal offset + * @idy: input vertical offset + * + * @interpolate defaults to bilinear. + * + * @idx, @idy default to zero. + * + * See also: vips_shrink(), vips_affine(), #VipsInterpolate. + * + * Returns: 0 on success, -1 on error + */ +int +vips_resize( VipsImage *in, VipsImage **out, + double h_scale, double v_scale, ... ) +{ + va_list ap; + int result; + + va_start( ap, v_scale ); + result = vips_call_split( "affine", ap, in, out, h_scale, v_scale ); + va_end( ap ); + + return( result ); +} + + + diff --git a/libvips/resample/similarity.c b/libvips/resample/similarity.c index ffdb8ff2..d9f2c48e 100644 --- a/libvips/resample/similarity.c +++ b/libvips/resample/similarity.c @@ -1,8 +1,8 @@ -/* simple wrapper over vips_similarity() to make scale / rotate easy from the +/* simple wrapper over vips_affine() to make scale / rotate easy from the * command-line * * 3/10/13 - * - from similarity.c + * - from affine.c * 25/10/13 * - oops, reverse rotation direction to match the convention used in the * rest of vips