2014-11-18 19:37:19 +01:00
|
|
|
/* resize an image ... up and down resampling.
|
2014-08-13 14:57:52 +02:00
|
|
|
*
|
|
|
|
* 13/8/14
|
|
|
|
* - from affine.c
|
2014-11-18 22:39:29 +01:00
|
|
|
* 18/11/14
|
|
|
|
* - add the fancier algorithm from vipsthumbnail
|
2015-11-11 11:23:50 +01:00
|
|
|
* 11/11/15
|
|
|
|
* - smarter cache sizing
|
2016-03-01 12:19:30 +01:00
|
|
|
* 29/2/16
|
|
|
|
* - shrink more affine less, now we have better anti-alias settings
|
2016-03-10 18:33:34 +01:00
|
|
|
* 10/3/16
|
|
|
|
* - revise again, using new vips_reduce() code
|
2016-05-01 13:03:13 +02:00
|
|
|
* 1/5/16
|
|
|
|
* - allow >1 on one axis, <1 on the other
|
2016-05-01 13:33:49 +02:00
|
|
|
* - expose @kernel setting
|
2014-08-13 14:57:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
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 <config.h>
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
#include <vips/intl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include <vips/vips.h>
|
|
|
|
#include <vips/debug.h>
|
|
|
|
#include <vips/internal.h>
|
|
|
|
#include <vips/transform.h>
|
|
|
|
|
|
|
|
#include "presample.h"
|
|
|
|
|
|
|
|
typedef struct _VipsResize {
|
|
|
|
VipsResample parent_instance;
|
|
|
|
|
2014-11-18 19:37:19 +01:00
|
|
|
double scale;
|
2015-11-19 18:09:18 +01:00
|
|
|
double vscale;
|
2016-05-01 13:33:49 +02:00
|
|
|
VipsKernel kernel;
|
2016-03-10 18:33:34 +01:00
|
|
|
|
|
|
|
/* Deprecated.
|
|
|
|
*/
|
2014-08-13 14:57:52 +02:00
|
|
|
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 )
|
|
|
|
{
|
2015-11-19 16:22:23 +01:00
|
|
|
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
|
2014-08-13 14:57:52 +02:00
|
|
|
VipsResample *resample = VIPS_RESAMPLE( object );
|
|
|
|
VipsResize *resize = (VipsResize *) object;
|
|
|
|
|
2016-03-10 18:33:34 +01:00
|
|
|
VipsImage **t = (VipsImage **) vips_object_local_array( object, 7 );
|
2014-08-13 14:57:52 +02:00
|
|
|
|
2014-11-18 19:37:19 +01:00
|
|
|
VipsImage *in;
|
2016-03-10 18:33:34 +01:00
|
|
|
int target_width;
|
|
|
|
int target_height;
|
2015-11-19 18:09:18 +01:00
|
|
|
int int_hshrink;
|
|
|
|
int int_vshrink;
|
|
|
|
double hresidual;
|
|
|
|
double vresidual;
|
2014-08-13 14:57:52 +02:00
|
|
|
|
|
|
|
if( VIPS_OBJECT_CLASS( vips_resize_parent_class )->build( object ) )
|
|
|
|
return( -1 );
|
|
|
|
|
2014-11-18 19:37:19 +01:00
|
|
|
in = resample->in;
|
|
|
|
|
2016-03-10 18:33:34 +01:00
|
|
|
/* The image size we are aiming for.
|
|
|
|
*/
|
|
|
|
target_width = in->Xsize * resize->scale;
|
|
|
|
if( vips_object_argument_isset( object, "vscale" ) )
|
|
|
|
target_height = in->Ysize * resize->vscale;
|
|
|
|
else
|
|
|
|
target_height = in->Ysize * resize->scale;
|
2014-11-18 19:37:19 +01:00
|
|
|
|
|
|
|
/* If the factor is > 1.0, we need to zoom rather than shrink.
|
|
|
|
* Just set the int part to 1 in this case.
|
|
|
|
*/
|
2016-03-14 15:58:02 +01:00
|
|
|
|
|
|
|
/* We want the int part of the shrink to leave a bit to do with
|
|
|
|
* blur/reduce/sharpen, or we'll see strange changes in aliasing on int
|
|
|
|
* shrink boundaries as we resize.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if( resize->scale > 1.0 )
|
|
|
|
int_hshrink = 1;
|
|
|
|
else
|
2016-03-15 15:59:59 +01:00
|
|
|
int_hshrink = VIPS_FLOOR( 1.0 / (resize->scale * 2) );
|
2016-03-14 15:58:02 +01:00
|
|
|
if( vips_object_argument_isset( object, "vscale" ) ) {
|
|
|
|
if( resize->vscale > 1.0 )
|
|
|
|
int_vshrink = 1;
|
|
|
|
else
|
2016-03-15 15:59:59 +01:00
|
|
|
int_vshrink = VIPS_FLOOR( 1.0 / (resize->vscale * 2) );
|
2016-03-14 15:58:02 +01:00
|
|
|
}
|
2016-03-10 18:33:34 +01:00
|
|
|
else
|
|
|
|
int_vshrink = int_hshrink;
|
|
|
|
|
2016-05-01 13:03:13 +02:00
|
|
|
if( int_vshrink > 1 ) {
|
|
|
|
vips_info( class->nickname, "shrinkv by %d", int_vshrink );
|
|
|
|
if( vips_shrinkv( in, &t[0], int_vshrink, NULL ) )
|
2014-11-18 22:39:29 +01:00
|
|
|
return( -1 );
|
2016-03-10 18:33:34 +01:00
|
|
|
in = t[0];
|
2014-11-18 22:39:29 +01:00
|
|
|
}
|
|
|
|
|
2016-05-01 13:03:13 +02:00
|
|
|
if( int_hshrink > 1 ) {
|
|
|
|
vips_info( class->nickname, "shrinkh by %d", int_hshrink );
|
|
|
|
if( vips_shrinkh( in, &t[1], int_hshrink, NULL ) )
|
|
|
|
return( -1 );
|
|
|
|
in = t[1];
|
|
|
|
}
|
|
|
|
|
2016-03-10 18:33:34 +01:00
|
|
|
/* Do we need a further size adjustment? It's the difference
|
|
|
|
* between our target size and the size we have after vips_shrink().
|
2016-03-10 20:53:05 +01:00
|
|
|
*
|
2016-03-12 14:14:34 +01:00
|
|
|
* This can break the aspect ratio slightly :/ but hopefully no one
|
|
|
|
* will notice.
|
2016-03-10 18:33:34 +01:00
|
|
|
*/
|
2016-03-13 18:44:24 +01:00
|
|
|
hresidual = (double) target_width / in->Xsize;
|
|
|
|
vresidual = (double) target_height / in->Ysize;
|
2016-03-10 18:33:34 +01:00
|
|
|
|
2016-03-22 15:35:44 +01:00
|
|
|
/* We will get overcomputation on vips_shrink() from the vips_reduce()
|
|
|
|
* coming later, so read into a cache where tiles are scanlines, and
|
|
|
|
* make sure we keep enough scanlines.
|
|
|
|
*
|
|
|
|
* We use a threaded tilecache to avoid a deadlock: suppose thread1,
|
|
|
|
* evaluating the top block of the output, is delayed, and thread2,
|
|
|
|
* evaluating the second block, gets here first (this can happen on
|
|
|
|
* a heavily-loaded system).
|
|
|
|
*
|
|
|
|
* With an unthreaded tilecache, thread2 will get
|
|
|
|
* the cache lock and start evaling the second block of the shrink.
|
|
|
|
* When it reaches the png reader it will stall until the first block
|
|
|
|
* has been used ... but it never will, since thread1 will block on
|
|
|
|
* this cache lock.
|
|
|
|
*
|
|
|
|
* Cache sizing: we double-buffer writes, so threads can be up to one
|
|
|
|
* line of tiles behind. For example, one thread could be allocated
|
|
|
|
* tile (0,0) and then stall, the whole write system won't stall until
|
|
|
|
* it tries to allocate tile (0, 2).
|
|
|
|
*
|
|
|
|
* We reduce down after this, which can be a scale of up to @residual,
|
|
|
|
* perhaps 0.5 or down as low as 0.3. So the number of scanlines we
|
|
|
|
* need to keep for the worst case is 2 * @tile_height / @residual,
|
|
|
|
* plus a little extra.
|
|
|
|
*/
|
|
|
|
if( int_vshrink > 1 ) {
|
|
|
|
int tile_width;
|
|
|
|
int tile_height;
|
|
|
|
int n_lines;
|
|
|
|
int need_lines;
|
|
|
|
|
|
|
|
vips_get_tile_size( in,
|
|
|
|
&tile_width, &tile_height, &n_lines );
|
|
|
|
need_lines = 1.2 * n_lines / vresidual;
|
|
|
|
if( vips_tilecache( in, &t[6],
|
|
|
|
"tile_width", in->Xsize,
|
|
|
|
"tile_height", 10,
|
|
|
|
"max_tiles", 1 + need_lines / 10,
|
|
|
|
"access", VIPS_ACCESS_SEQUENTIAL,
|
|
|
|
"threaded", TRUE,
|
|
|
|
NULL ) )
|
|
|
|
return( -1 );
|
|
|
|
in = t[6];
|
|
|
|
}
|
|
|
|
|
2016-05-01 13:03:13 +02:00
|
|
|
/* Any residual downsizing.
|
2014-11-18 19:37:19 +01:00
|
|
|
*/
|
2016-05-01 13:03:13 +02:00
|
|
|
if( vresidual < 1.0 ) {
|
|
|
|
vips_info( class->nickname, "residual reducev by %g",
|
|
|
|
vresidual );
|
2016-05-01 13:33:49 +02:00
|
|
|
if( vips_reducev( in, &t[2], 1.0 / vresidual,
|
|
|
|
"kernel", resize->kernel,
|
|
|
|
NULL ) )
|
2016-01-29 16:23:00 +01:00
|
|
|
return( -1 );
|
2016-03-10 18:33:34 +01:00
|
|
|
in = t[2];
|
2016-01-29 16:23:00 +01:00
|
|
|
}
|
2016-03-15 15:59:59 +01:00
|
|
|
|
2016-05-01 13:03:13 +02:00
|
|
|
if( hresidual < 1.0 ) {
|
|
|
|
vips_info( class->nickname, "residual reduceh by %g",
|
|
|
|
hresidual );
|
2016-05-01 13:33:49 +02:00
|
|
|
if( vips_reduceh( in, &t[3], 1.0 / hresidual,
|
|
|
|
"kernel", resize->kernel,
|
|
|
|
NULL ) )
|
2016-05-01 13:03:13 +02:00
|
|
|
return( -1 );
|
|
|
|
in = t[3];
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:59:59 +01:00
|
|
|
/* Any upsizing.
|
|
|
|
*/
|
2016-05-01 13:03:13 +02:00
|
|
|
if( hresidual > 1.0 ) {
|
|
|
|
vips_info( class->nickname, "residual scaleh %g",
|
|
|
|
hresidual );
|
|
|
|
if( vips_affine( in, &t[4], hresidual, 0.0, 0.0, 1.0,
|
2016-03-10 18:33:34 +01:00
|
|
|
"interpolate", vips_interpolate_nearest_static(),
|
2016-01-29 16:23:00 +01:00
|
|
|
NULL ) )
|
|
|
|
return( -1 );
|
2016-05-01 13:03:13 +02:00
|
|
|
in = t[4];
|
|
|
|
}
|
|
|
|
|
|
|
|
if( vresidual > 1.0 ) {
|
|
|
|
vips_info( class->nickname, "residual scalev %g", vresidual );
|
|
|
|
if( vips_affine( in, &t[5], 1.0, 0.0, 0.0, vresidual,
|
|
|
|
"interpolate", vips_interpolate_nearest_static(),
|
|
|
|
NULL ) )
|
|
|
|
return( -1 );
|
|
|
|
in = t[5];
|
2014-11-18 22:39:29 +01:00
|
|
|
}
|
|
|
|
|
2014-11-18 19:37:19 +01:00
|
|
|
if( vips_image_write( in, resample->out ) )
|
2014-08-13 14:57:52 +02:00
|
|
|
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;
|
|
|
|
|
2014-11-18 19:37:19 +01:00
|
|
|
VIPS_ARG_DOUBLE( class, "scale", 113,
|
|
|
|
_( "Scale factor" ),
|
|
|
|
_( "Scale image by this factor" ),
|
2014-08-13 14:57:52 +02:00
|
|
|
VIPS_ARGUMENT_REQUIRED_INPUT,
|
2014-11-18 19:37:19 +01:00
|
|
|
G_STRUCT_OFFSET( VipsResize, scale ),
|
2014-08-13 14:57:52 +02:00
|
|
|
0, 10000000, 0 );
|
|
|
|
|
2015-11-19 18:09:18 +01:00
|
|
|
VIPS_ARG_DOUBLE( class, "vscale", 113,
|
|
|
|
_( "Vertical scale factor" ),
|
|
|
|
_( "Vertical scale image by this factor" ),
|
|
|
|
VIPS_ARGUMENT_OPTIONAL_INPUT,
|
|
|
|
G_STRUCT_OFFSET( VipsResize, vscale ),
|
|
|
|
0, 10000000, 0 );
|
|
|
|
|
2016-05-01 13:33:49 +02:00
|
|
|
VIPS_ARG_ENUM( class, "kernel", 3,
|
|
|
|
_( "Kernel" ),
|
|
|
|
_( "Resampling kernel" ),
|
|
|
|
VIPS_ARGUMENT_OPTIONAL_INPUT,
|
|
|
|
G_STRUCT_OFFSET( VipsResize, kernel ),
|
|
|
|
VIPS_TYPE_KERNEL, VIPS_KERNEL_LANCZOS3 );
|
|
|
|
|
2016-03-10 18:33:34 +01:00
|
|
|
/* We used to let people set the input offset so you could pick centre
|
|
|
|
* or corner interpolation, but it's not clear this was useful.
|
|
|
|
*/
|
2014-08-13 14:57:52 +02:00
|
|
|
VIPS_ARG_DOUBLE( class, "idx", 115,
|
|
|
|
_( "Input offset" ),
|
|
|
|
_( "Horizontal input displacement" ),
|
2016-03-10 18:33:34 +01:00
|
|
|
VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED,
|
2014-08-13 14:57:52 +02:00
|
|
|
G_STRUCT_OFFSET( VipsResize, idx ),
|
|
|
|
-10000000, 10000000, 0 );
|
|
|
|
|
|
|
|
VIPS_ARG_DOUBLE( class, "idy", 116,
|
|
|
|
_( "Input offset" ),
|
|
|
|
_( "Vertical input displacement" ),
|
2016-03-10 18:33:34 +01:00
|
|
|
VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED,
|
2014-08-13 14:57:52 +02:00
|
|
|
G_STRUCT_OFFSET( VipsResize, idy ),
|
|
|
|
-10000000, 10000000, 0 );
|
2016-03-10 18:33:34 +01:00
|
|
|
|
|
|
|
/* We used to let people set the interpolator, but it's not clear this
|
|
|
|
* was useful. Anyway, vips_reduce() no longer has an interpolator
|
|
|
|
* param.
|
|
|
|
*/
|
|
|
|
VIPS_ARG_INTERPOLATE( class, "interpolate", 2,
|
|
|
|
_( "Interpolate" ),
|
|
|
|
_( "Interpolate pixels with this" ),
|
|
|
|
VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED,
|
|
|
|
G_STRUCT_OFFSET( VipsResize, interpolate ) );
|
|
|
|
|
2014-08-13 14:57:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vips_resize_init( VipsResize *resize )
|
|
|
|
{
|
2016-05-01 13:33:49 +02:00
|
|
|
resize->kernel = VIPS_KERNEL_LANCZOS3;
|
2014-08-13 14:57:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vips_resize:
|
|
|
|
* @in: input image
|
|
|
|
* @out: output image
|
2014-11-18 19:37:19 +01:00
|
|
|
* @scale: scale factor
|
2014-09-06 00:18:16 +02:00
|
|
|
* @...: %NULL-terminated list of optional named arguments
|
2014-08-13 14:57:52 +02:00
|
|
|
*
|
|
|
|
* Optional arguments:
|
|
|
|
*
|
2016-05-02 11:12:37 +02:00
|
|
|
* * @vscale: vertical scale factor
|
|
|
|
* * @kernel: #VipsKernel to reduce with
|
2014-08-13 14:57:52 +02:00
|
|
|
*
|
2016-03-10 18:33:34 +01:00
|
|
|
* Resize an image. When upsizing (@scale > 1), the image is simply block
|
|
|
|
* upsized. When downsizing, the
|
2016-05-01 13:33:49 +02:00
|
|
|
* image is block-shrunk with vips_shrink(),
|
|
|
|
* then the image is shrunk again to the
|
|
|
|
* target size with vips_reduce(). The operation will leave at least the final
|
|
|
|
* x2 to be done with vips_reduce().
|
2014-11-18 22:39:29 +01:00
|
|
|
*
|
2015-11-19 18:09:18 +01:00
|
|
|
* vips_resize() normally maintains the image apect ratio. If you set
|
|
|
|
* @vscale, that factor is used for the vertical scale and @scale for the
|
|
|
|
* horizontal.
|
|
|
|
*
|
2016-05-01 13:33:49 +02:00
|
|
|
* vips_resize() normally uses #VIPS_KERNEL_LANCZOS3 for thre final shrink, you
|
|
|
|
* can change this with @kernel.
|
|
|
|
*
|
2015-02-25 09:47:45 +01:00
|
|
|
* This operation does not change xres or yres. The image resolution needs to
|
|
|
|
* be updated by the application.
|
|
|
|
*
|
2016-05-01 13:33:49 +02:00
|
|
|
* See also: vips_shrink(), vips_reduce().
|
2014-08-13 14:57:52 +02:00
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 on error
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vips_resize( VipsImage *in, VipsImage **out,
|
2014-11-18 19:37:19 +01:00
|
|
|
double scale, ... )
|
2014-08-13 14:57:52 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int result;
|
|
|
|
|
2014-11-18 19:37:19 +01:00
|
|
|
va_start( ap, scale );
|
2015-01-21 10:05:36 +01:00
|
|
|
result = vips_call_split( "resize", ap, in, out, scale );
|
2014-08-13 14:57:52 +02:00
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
return( result );
|
|
|
|
}
|