Merge branch 'master' into add-lanczos3

This commit is contained in:
John Cupitt 2016-03-13 11:23:03 +00:00
commit dc7902b12f
23 changed files with 510 additions and 1653 deletions

View File

@ -18,11 +18,13 @@
- better behaviour for vips_cast() shift of non-int types (thanks apacheark)
- python .bandrank() now works like .bandjoin()
- vipsthumbnail --interpolator and --sharpen are deprecated
- switches to disable PPM, Rad and Analyze support
27/1/16 started 8.2.3
- fix a crash with SPARC byte-order labq vips images
- fix parsing of filenames containing brackets, thanks shilpi230
- fix hist_entropy (lovell)
- small fixes to radiance load
12/1/16 started 8.2.2
- changes to ease compiling C++ binding with MSVC [Lovell Fuller]

9
TODO
View File

@ -4,15 +4,6 @@
- try orc version of reducev?
- try
$ vipsthumbnail ghibli_orig.jpg -s 600x1085
$ vipsheader tn_ghibli_orig.jpg
tn_ghibli_orig.jpg: 599x1083 uchar, 3 bands, srgb, jpegload
- try SEQ_UNBUFFERED on jpg source, get out of order error?

View File

@ -618,6 +618,33 @@ if test x"$with_matio" != "xno"; then
])
fi
# not external libraries, but have options to disable them, helps to
# reduce attack surface
AC_ARG_WITH([ppm],
AS_HELP_STRING([--without-ppm], [build without ppm (default: with)]))
if test x"$with_ppm" != "xno"; then
AC_DEFINE(HAVE_PPM,1,[define to build ppm support.])
with_ppm=yes
fi
AC_ARG_WITH([analyze],
AS_HELP_STRING([--without-analyze], [build without analyze (default: with)]))
if test x"$with_analyze" != "xno"; then
AC_DEFINE(HAVE_ANALYZE,1,[define to build analyze support.])
with_analyze=yes
fi
AC_ARG_WITH([radiance],
AS_HELP_STRING([--without-radiance], [build without radiance (default: with)]))
if test x"$with_radiance" != "xno"; then
AC_DEFINE(HAVE_RADIANCE,1,[define to build radiance support.])
with_radiance=yes
fi
# cfitsio
AC_ARG_WITH([cfitsio],
AS_HELP_STRING([--without-cfitsio], [build without cfitsio (default: test)]))
@ -911,6 +938,9 @@ gobject introspection: $found_introspection
build vips7 Python binding: $with_python
install vips8 Python overrides: $enable_pyvips8
(requires pygobject-3.12.0 or later)
build radiance support: $with_radiance
build analyze support: $with_analyze
build PPM support: $with_ppm
* optional dependencies
use fftw3 for FFT: $with_fftw

View File

@ -42,9 +42,6 @@
#include <vips/vips.h>
#include "../foreign/dbh.h"
#include "../foreign/analyze2vips.h"
static VipsFormatFlags
analyze_flags( const char *filename )
{
@ -61,7 +58,15 @@ isanalyze( const char *filename )
int
im_analyze2vips( const char *filename, IMAGE *out )
{
return( vips__analyze_read( filename, out ) );
VipsImage *t;
if( vips_analyzeload( filename, &t, NULL ) )
return( -1 );
if( vips_image_write( t, out ) ) {
g_object_unref( t );
return( -1 );
}
g_object_unref( t );
return( 0 );
}

View File

@ -50,6 +50,8 @@
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#ifdef HAVE_ANALYZE
#include <ctype.h>
#include <stdio.h>
#include <string.h>
@ -594,3 +596,4 @@ vips__analyze_read( const char *filename, VipsImage *out )
return( 0 );
}
#endif /*HAVE_ANALYZE*/

View File

@ -48,6 +48,8 @@
#include <vips/buf.h>
#include <vips/internal.h>
#ifdef HAVE_ANALYZE
#include "analyze2vips.h"
typedef struct _VipsForeignLoadAnalyze {
@ -142,6 +144,8 @@ vips_foreign_load_analyze_init( VipsForeignLoadAnalyze *analyze )
{
}
#endif /*HAVE_ANALYZE*/
/**
* vips_analyzeload:
* @filename: file to load

View File

@ -1665,22 +1665,31 @@ vips_foreign_operation_init( void )
extern GType vips_foreign_load_gif_file_get_type( void );
extern GType vips_foreign_load_gif_buffer_get_type( void );
vips_foreign_load_rad_get_type();
vips_foreign_save_rad_get_type();
vips_foreign_load_ppm_get_type();
vips_foreign_save_ppm_get_type();
vips_foreign_load_csv_get_type();
vips_foreign_save_csv_get_type();
vips_foreign_load_matrix_get_type();
vips_foreign_save_matrix_get_type();
vips_foreign_print_matrix_get_type();
vips_foreign_load_analyze_get_type();
vips_foreign_load_raw_get_type();
vips_foreign_save_raw_get_type();
vips_foreign_save_raw_fd_get_type();
vips_foreign_load_vips_get_type();
vips_foreign_save_vips_get_type();
#ifdef HAVE_ANALYZE
vips_foreign_load_analyze_get_type();
#endif /*HAVE_ANALYZE*/
#ifdef HAVE_PPM
vips_foreign_load_ppm_get_type();
vips_foreign_save_ppm_get_type();
#endif /*HAVE_PPM*/
#ifdef HAVE_RADIANCE
vips_foreign_load_rad_get_type();
vips_foreign_save_rad_get_type();
#endif /*HAVE_RADIANCE*/
#ifdef HAVE_POPPLER
vips_foreign_load_pdf_get_type();
vips_foreign_load_pdf_file_get_type();

View File

@ -68,6 +68,8 @@
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#ifdef HAVE_PPM
#include <ctype.h>
#include <stdio.h>
#include <string.h>
@ -825,3 +827,5 @@ vips__ppm_save( VipsImage *in, const char *filename,
return( 0 );
}
#endif /*HAVE_PPM*/

View File

@ -48,6 +48,8 @@
#include <vips/buf.h>
#include <vips/internal.h>
#ifdef HAVE_PPM
#include "ppm.h"
typedef struct _VipsForeignLoadPpm {
@ -142,6 +144,8 @@ vips_foreign_load_ppm_init( VipsForeignLoadPpm *ppm )
{
}
#endif /*HAVE_PPM*/
/**
* vips_ppmload:
* @filename: file to load

View File

@ -47,6 +47,8 @@
#include <vips/vips.h>
#ifdef HAVE_PPM
#include "ppm.h"
typedef struct _VipsForeignSavePpm {
@ -144,6 +146,8 @@ vips_foreign_save_ppm_init( VipsForeignSavePpm *ppm )
{
}
#endif /*HAVE_PPM*/
/**
* vips_ppmsave:
* @in: image to save

View File

@ -127,6 +127,8 @@
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#ifdef HAVE_RADIANCE
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
@ -653,16 +655,23 @@ buffer_need( Buffer *buffer, int require )
int remaining;
g_assert( require < BUFFER_MARGIN );
g_assert( buffer->length >= 0 );
g_assert( buffer->position >= 0 );
g_assert( buffer->position <= buffer->length );
remaining = buffer->length - buffer->position;
if( remaining < require ) {
size_t len;
memcpy( buffer->text,
/* Areas can overlap.
*/
memmove( buffer->text,
buffer->text + buffer->position, remaining );
buffer->position = 0;
buffer->length = remaining;
g_assert( buffer->length < BUFFER_MARGIN );
len = fread( buffer->text + buffer->length,
1, BUFFER_SIZE, buffer->fp );
buffer->length += len;
@ -687,6 +696,10 @@ scanline_read_old( Buffer *buffer, COLR *scanline, int width )
{
int rshift;
g_assert( buffer->length >= 0 );
g_assert( buffer->position >= 0 );
g_assert( buffer->position <= buffer->length );
rshift = 0;
while( width > 0 ) {
@ -728,6 +741,10 @@ scanline_read( Buffer *buffer, COLR *scanline, int width )
{
int i, j;
g_assert( buffer->length >= 0 );
g_assert( buffer->position >= 0 );
g_assert( buffer->position <= buffer->length );
/* Detect old-style scanlines.
*/
if( width < MINELEN ||
@ -996,8 +1013,10 @@ static const char *colcor_name[3] = {
static int
rad2vips_get_header( Read *read, VipsImage *out )
{
int i, j;
VipsInterpretation interpretation;
int width;
int height;
int i, j;
if( getheader( read->fin, (gethfunc *) rad2vips_process_line, read ) ||
!fgetsresolu( &read->rs, read->fin ) ) {
@ -1013,9 +1032,17 @@ rad2vips_get_header( Read *read, VipsImage *out )
else
interpretation = VIPS_INTERPRETATION_MULTIBAND;
vips_image_init_fields( out,
scanlen( &read->rs ), numscans( &read->rs ),
4,
width = scanlen( &read->rs );
height = numscans( &read->rs );
if( width <= 0 ||
width > VIPS_MAX_COORD ||
height <= 0 ||
height > VIPS_MAX_COORD ) {
vips_error( "rad2vips", "%s", _( "image size out of bounds" ) );
return( -1 );
}
vips_image_init_fields( out, width, height, 4,
VIPS_FORMAT_UCHAR, VIPS_CODING_RAD,
interpretation,
1, read->aspect );
@ -1080,6 +1107,7 @@ rad2vips_generate( VipsRegion *or,
if( scanline_read( read->buffer, buf, or->im->Xsize ) ) {
vips_error( "rad2vips",
_( "read error line %d" ), r->top + y );
VIPS_GATE_STOP( "rad2vips_generate: work" );
return( -1 );
}
}
@ -1277,3 +1305,5 @@ vips__rad_save( VipsImage *in, const char *filename )
}
const char *vips__rad_suffs[] = { ".hdr", NULL };
#endif /*HAVE_RADIANCE*/

View File

@ -48,6 +48,8 @@
#include <vips/buf.h>
#include <vips/internal.h>
#ifdef HAVE_RADIANCE
#include "radiance.h"
typedef struct _VipsForeignLoadRad {
@ -145,6 +147,8 @@ vips_foreign_load_rad_init( VipsForeignLoadRad *rad )
{
}
#endif /*HAVE_RADIANCE*/
/**
* vips_radload:
* @filename: file to load

View File

@ -47,6 +47,8 @@
#include <vips/vips.h>
#ifdef HAVE_RADIANCE
#include "radiance.h"
typedef struct _VipsForeignSaveRad {
@ -129,6 +131,8 @@ vips_foreign_save_rad_init( VipsForeignSaveRad *rad )
{
}
#endif /*HAVE_RADIANCE*/
/**
* vips_radsave:
* @in: image to save

View File

@ -58,11 +58,6 @@ int vips_reduce( VipsImage *in, VipsImage **out,
int vips_reduceh( VipsImage *in, VipsImage **out, double xshrink, ... );
int vips_reducev( VipsImage *in, VipsImage **out, double yshrink, ... );
int vips_reducel3( VipsImage *in, VipsImage **out,
double xshrink, double yshrink, ... );
int vips_reducehl3( VipsImage *in, VipsImage **out, double xshrink, ... );
int vips_reducevl3( VipsImage *in, VipsImage **out, double yshrink, ... );
int vips_similarity( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int vips_affine( VipsImage *in, VipsImage **out,

View File

@ -10,11 +10,8 @@ libresample_la_SOURCES = \
shrinkh.c \
shrinkv.c \
reduce.c \
reducel3.c \
reduceh.cpp \
reducehl3.cpp \
reducev.cpp \
reducevl3.cpp \
interpolate.c \
transform.c \
bicubic.cpp \

View File

@ -56,6 +56,10 @@ typedef struct _VipsReduce {
double xshrink; /* Shrink factors */
double yshrink;
/* The thing we use to make the kernel.
*/
VipsKernel kernel;
} VipsReduce;
typedef VipsResampleClass VipsReduceClass;
@ -73,8 +77,12 @@ vips_reduce_build( VipsObject *object )
if( VIPS_OBJECT_CLASS( vips_reduce_parent_class )->build( object ) )
return( -1 );
if( vips_reducev( resample->in, &t[0], reduce->yshrink, NULL ) ||
vips_reduceh( t[0], &t[1], reduce->xshrink, NULL ) ||
if( vips_reducev( resample->in, &t[0], reduce->yshrink,
"kernel", reduce->kernel,
NULL ) ||
vips_reduceh( t[0], &t[1], reduce->xshrink,
"kernel", reduce->kernel,
NULL ) ||
vips_image_write( t[1], resample->out ) )
return( -1 );
@ -113,11 +121,19 @@ vips_reduce_class_init( VipsReduceClass *class )
G_STRUCT_OFFSET( VipsReduce, yshrink ),
1.0, 1000000.0, 1.0 );
VIPS_ARG_ENUM( class, "kernel", 3,
_( "Kernel" ),
_( "Resampling kernel" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsReduce, kernel ),
VIPS_TYPE_KERNEL, VIPS_KERNEL_LANCZOS3 );
}
static void
vips_reduce_init( VipsReduce *reduce )
{
reduce->kernel = VIPS_KERNEL_LANCZOS3;
}
/**
@ -128,7 +144,11 @@ vips_reduce_init( VipsReduce *reduce )
* @shrinke: vertical shrink
* @...: %NULL-terminated list of optional named arguments
*
* Reduce @in by a pair of factors with a pair of 1D cubic interpolators. This
* Optional arguments:
*
* @kernel: #VipsKernel to use to interpolate (default: lanczos3)
*
* Reduce @in by a pair of factors with a pair of 1D 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

View File

@ -1,7 +1,9 @@
/* horizontal reduce by a float factor
/* horizontal reduce by a float factor with lanczos3
*
* 29/1/16
* - from shrinkh.c
* 10/3/16
* - add other kernels
*/
/*
@ -51,54 +53,163 @@
#include "presample.h"
#include "templates.h"
/**
* VipsKernel:
* @VIPS_KERNEL_NEAREST: nearest-neighbour
* @VIPS_KERNEL_LINEAR: linear interpolation
* @VIPS_KERNEL_CUBIC: cubic interpolation
* @VIPS_KERNEL_LANCZOS2: lanczos2 interpolation
* @VIPS_KERNEL_LANCZOS3: lanczos3 interpolation
*
* 1D resampling kernels.
*/
typedef struct _VipsReduceh {
VipsResample parent_instance;
double xshrink; /* Reduce factor */
/* The thing we use to make the kernel.
*/
VipsKernel kernel;
/* Number of points in kernel.
*/
int n_points;
/* 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.
*/
int matrixi[VIPS_TRANSFORM_SCALE + 1][MAX_POINTS];
double matrixf[VIPS_TRANSFORM_SCALE + 1][MAX_POINTS];
} 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 );
}
/* Get n points.
*/
int
vips_reduce_get_points( VipsKernel kernel )
{
switch( kernel ) {
case VIPS_KERNEL_NEAREST:
return( 1 );
case VIPS_KERNEL_LINEAR:
return( 2 );
case VIPS_KERNEL_CUBIC:
return( 4 );
case VIPS_KERNEL_LANCZOS2:
return( 4 );
case VIPS_KERNEL_LANCZOS3:
return( 6 );
default:
g_assert_not_reached();
return( 0 );
}
}
/* Calculate a mask.
*/
void
vips_reduce_make_mask( VipsKernel kernel, double x, double *c )
{
switch( kernel ) {
case VIPS_KERNEL_NEAREST:
c[0] = 1.0;
break;
case VIPS_KERNEL_LINEAR:
c[0] = 1.0 - x;
c[1] = x;
break;
case VIPS_KERNEL_CUBIC:
calculate_coefficients_catmull( x, c );
break;
case VIPS_KERNEL_LANCZOS2:
calculate_coefficients_lanczos( 2, x, c );
break;
case VIPS_KERNEL_LANCZOS3:
calculate_coefficients_lanczos( 3, x, c );
break;
default:
g_assert_not_reached();
break;
}
}
template <typename T, int max_value>
static void inline
reduceh_unsigned_int_tab( VipsPel *pout, const VipsPel *pin,
const int bands, const int *cx )
reduceh_unsigned_int_tab( VipsReduceh *reduceh,
VipsPel *pout, const VipsPel *pin,
const int bands, const int * restrict cx )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reduceh->n_points;
for( int z = 0; z < bands; z++ ) {
int sum;
sum = reduce_sum<T, int>( in, bands, cx, n );
sum = unsigned_fixed_round( sum );
sum = VIPS_CLIP( 0, sum, max_value );
out[z] = sum;
in += 1;
}
}
/* A 4-point interpolation on uint8 is the most common case ... unroll that.
*
* The inner loop here won't vectorise, but our inner loop doesn't run for
* long enough for vectorisation to be useful :-( gcc says it needs about an
* 11-point kernel for the vector version to be worthwhile.
*/
static void inline
reduceh_unsigned_uint8_6tab( VipsPel *out, const VipsPel *in,
const int bands, const int *cx )
{
const int b1 = bands;
const int b2 = b1 + b1;
const int b3 = b1 + b2;
const int b4 = b2 + b2;
const int b5 = b1 + b4;
const int c0 = cx[0];
const int c1 = cx[1];
const int c2 = cx[2];
const int c3 = cx[3];
const int c4 = cx[4];
const int c5 = cx[5];
for( int z = 0; z < bands; z++ ) {
int cubich = unsigned_fixed_round(
c0 * in[0] +
c1 * in[b1] +
c2 * in[b2] +
c3 * in[b3] );
c3 * in[b3] +
c4 * in[b4] +
c5 * in[b5] );
cubich = VIPS_CLIP( 0, cubich, max_value );
cubich = VIPS_CLIP( 0, cubich, 255 );
out[z] = cubich;
@ -108,31 +219,22 @@ reduceh_unsigned_int_tab( VipsPel *pout, const VipsPel *pin,
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 )
reduceh_signed_int_tab( VipsReduceh *reduceh,
VipsPel *pout, const VipsPel *pin,
const int bands, const int * restrict 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];
const int n = reduceh->n_points;
for( int z = 0; z < bands; z++ ) {
int cubich = signed_fixed_round(
c0 * in[0] +
c1 * in[b1] +
c2 * in[b2] +
c3 * in[b3] );
int sum;
cubich = VIPS_CLIP( min_value, cubich, max_value );
sum = reduce_sum<T, int>( in, bands, cx, n );
sum = signed_fixed_round( sum );
sum = VIPS_CLIP( min_value, sum, max_value );
out[z] = cubich;
out[z] = sum;
in += 1;
}
@ -142,62 +244,38 @@ reduceh_signed_int_tab( VipsPel *pout, const VipsPel *pin,
*/
template <typename T>
static void inline
reduceh_float_tab( VipsPel *pout, const VipsPel *pin,
reduceh_float_tab( VipsReduceh *reduceh,
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];
const int n = reduceh->n_points;
for( int z = 0; z < bands; z++ ) {
out[z] =
c0 * in[0] +
c1 * in[b1] +
c2 * in[b2] +
c3 * in[b3];
out[z] = reduce_sum<T, double>( in, bands, cx, n );
in += 1;
}
}
/* 32-bit output needs a double intermediate.
/* 32-bit int output needs a double intermediate.
*/
template <typename T, int max_value>
static void inline
reduceh_unsigned_int32_tab( VipsPel *pout, const VipsPel *pin,
const int bands, const double *cx )
reduceh_unsigned_int32_tab( VipsReduceh *reduceh,
VipsPel *pout, const VipsPel *pin,
const int bands, const double * restrict 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];
const int n = reduceh->n_points;
for( int z = 0; z < bands; z++ ) {
double cubich =
c0 * in[0] +
c1 * in[b1] +
c2 * in[b2] +
c3 * in[b3];
double sum;
cubich = VIPS_CLIP( 0, cubich, max_value );
out[z] = cubich;
sum = reduce_sum<T, double>( in, bands, cx, n );
out[z] = VIPS_CLIP( 0, sum, max_value );
in += 1;
}
@ -205,31 +283,20 @@ reduceh_unsigned_int32_tab( VipsPel *pout, const VipsPel *pin,
template <typename T, int min_value, int max_value>
static void inline
reduceh_signed_int32_tab( VipsPel *pout, const VipsPel *pin,
const int bands, const double *cx )
reduceh_signed_int32_tab( VipsReduceh *reduceh,
VipsPel *pout, const VipsPel *pin,
const int bands, const double * restrict 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];
const int n = reduceh->n_points;
for( int z = 0; z < bands; z++ ) {
double cubich =
c0 * in[0] +
c1 * in[b1] +
c2 * in[b2] +
c3 * in[b3];
double sum;
cubich = VIPS_CLIP( min_value, cubich, max_value );
out[z] = cubich;
sum = reduce_sum<T, double>( in, bands, cx, n );
sum = VIPS_CLIP( min_value, sum, max_value );
out[z] = sum;
in += 1;
}
@ -239,27 +306,20 @@ reduceh_signed_int32_tab( VipsPel *pout, const VipsPel *pin,
*/
template <typename T>
static void inline
reduceh_notab( VipsPel *pout, const VipsPel *pin,
reduceh_notab( VipsReduceh *reduceh,
VipsPel *pout, const VipsPel *pin,
const int bands, double x )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reduceh->n_points;
const int b1 = bands;
const int b2 = b1 + b1;
const int b3 = b1 + b2;
double cx[MAX_POINTS];
double cx[4];
calculate_coefficients_catmull( x, cx );
vips_reduce_make_mask( reduceh->kernel, 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 );
out[z] = reduce_sum<T, double>( in, bands, cx, n );
in += 1;
}
@ -289,7 +349,7 @@ vips_reduceh_gen( VipsRegion *out_region, void *seq,
s.left = r->left * reduceh->xshrink;
s.top = r->top;
s.width = r->width * reduceh->xshrink + 4;
s.width = r->width * reduceh->xshrink + reduceh->n_points;
s.height = r->height;
if( vips_region_prepare( ir, &s ) )
return( -1 );
@ -309,54 +369,66 @@ vips_reduceh_gen( VipsRegion *out_region, void *seq,
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];
const int *cxi = reduceh->matrixi[tx];
const double *cxf = reduceh->matrixf[tx];
switch( in->BandFmt ) {
case VIPS_FORMAT_UCHAR:
reduceh_unsigned_int_tab
<unsigned char, UCHAR_MAX>(
q, p, bands, cxi );
if( reduceh->n_points == 6 )
reduceh_unsigned_uint8_6tab(
q, p, bands, cxi );
else
reduceh_unsigned_int_tab
<unsigned char, UCHAR_MAX>(
reduceh,
q, p, bands, cxi );
break;
case VIPS_FORMAT_CHAR:
reduceh_signed_int_tab
<signed char, SCHAR_MIN, SCHAR_MAX>(
reduceh,
q, p, bands, cxi );
break;
case VIPS_FORMAT_USHORT:
reduceh_unsigned_int_tab
<unsigned short, USHRT_MAX>(
reduceh,
q, p, bands, cxi );
break;
case VIPS_FORMAT_SHORT:
reduceh_signed_int_tab
<signed short, SHRT_MIN, SHRT_MAX>(
reduceh,
q, p, bands, cxi );
break;
case VIPS_FORMAT_UINT:
reduceh_unsigned_int32_tab
<unsigned int, INT_MAX>(
reduceh,
q, p, bands, cxf );
break;
case VIPS_FORMAT_INT:
reduceh_signed_int32_tab
<signed int, INT_MIN, INT_MAX>(
reduceh,
q, p, bands, cxf );
break;
case VIPS_FORMAT_FLOAT:
case VIPS_FORMAT_COMPLEX:
reduceh_float_tab<float>( q, p, bands, cxf );
reduceh_float_tab<float>( reduceh,
q, p, bands, cxf );
break;
case VIPS_FORMAT_DOUBLE:
case VIPS_FORMAT_DPCOMPLEX:
reduceh_notab<double>( q, p, bands, X - ix );
reduceh_notab<double>( reduceh,
q, p, bands, X - ix );
break;
default:
@ -402,6 +474,19 @@ vips_reduceh_build( VipsObject *object )
if( reduceh->xshrink == 1 )
return( vips_image_write( in, resample->out ) );
/* Build the tables of pre-computed coefficients.
*/
reduceh->n_points = vips_reduce_get_points( reduceh->kernel );
for( int x = 0; x < VIPS_TRANSFORM_SCALE + 1; x++ ) {
vips_reduce_make_mask( reduceh->kernel,
(float) x / VIPS_TRANSFORM_SCALE,
reduceh->matrixf[x] );
for( int i = 0; i < reduceh->n_points; i++ )
reduceh->matrixi[x][i] = reduceh->matrixf[x][i] *
VIPS_INTERPOLATE_SCALE;
}
/* Unpack for processing.
*/
if( vips_image_decode( in, &t[0] ) )
@ -411,8 +496,8 @@ vips_reduceh_build( VipsObject *object )
/* 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,
reduceh->n_points / 2, 0,
in->Xsize + reduceh->n_points - 1, in->Ysize,
"extend", VIPS_EXTEND_COPY,
NULL ) )
return( -1 );
@ -428,7 +513,8 @@ vips_reduceh_build( VipsObject *object )
* 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;
resample->out->Xsize = (in->Xsize - reduceh->n_points + 1) /
reduceh->xshrink;
if( resample->out->Xsize <= 0 ) {
vips_error( object_class->nickname,
"%s", _( "image has shrunk to nothing" ) );
@ -466,27 +552,6 @@ vips_reduceh_class_init( VipsReducehClass *reduceh_class )
vobject_class->description = _( "shrink an image horizontally" );
vobject_class->build = vips_reduceh_build;
/*
{
double x;
for( x = 0.0; x <= 1.0; x += 0.05 ) {
double c[7];
int i;
calculate_coefficients_lanczos3( x, c );
printf( "%g, ", x );
for( i = 0; i < 7; i++ ) {
printf( "%5g", c[i] );
if( i < 6 )
printf( ", " );
}
printf( "\n" );
}
}
*/
operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;
VIPS_ARG_DOUBLE( reduceh_class, "xshrink", 3,
@ -496,24 +561,19 @@ vips_reduceh_class_init( VipsReducehClass *reduceh_class )
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;
}
VIPS_ARG_ENUM( reduceh_class, "kernel", 3,
_( "Kernel" ),
_( "Resampling kernel" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsReduceh, kernel ),
VIPS_TYPE_KERNEL, VIPS_KERNEL_LANCZOS3 );
}
static void
vips_reduceh_init( VipsReduceh *reduceh )
{
reduceh->kernel = VIPS_KERNEL_LANCZOS3;
}
/**
@ -523,8 +583,12 @@ vips_reduceh_init( VipsReduceh *reduceh )
* @xshrink: horizontal reduce
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @kernel: #VipsKernel to use to interpolate (default: lanczos3)
*
* 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
* interpolated with a 1D 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

View File

@ -1,615 +0,0 @@
/* horizontal reduce by a float factor with lanczos3
*
* 29/1/16
* - from shrinkh.c
* 10/3/16
* - add other kernels
*/
/*
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"
/**
* VipsKernel:
* @VIPS_KERNEL_NEAREST: nearest-neighbour
* @VIPS_KERNEL_LINEAR: linear interpolation
* @VIPS_KERNEL_CUBIC: cubic interpolation
* @VIPS_KERNEL_LANCZOS2: lanczos2 interpolation
* @VIPS_KERNEL_LANCZOS3: lanczos3 interpolation
*
* 1D resampling kernels.
*/
typedef struct _VipsReducehl3 {
VipsResample parent_instance;
double xshrink; /* Reduce factor */
/* The thing we use to make the kernel.
*/
VipsKernel kernel;
/* Number of points in kernel.
*/
int n_points;
/* 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.
*/
int matrixi[VIPS_TRANSFORM_SCALE + 1][MAX_POINTS];
double matrixf[VIPS_TRANSFORM_SCALE + 1][MAX_POINTS];
} VipsReducehl3;
typedef VipsResampleClass VipsReducehl3Class;
/* We need C linkage for this.
*/
extern "C" {
G_DEFINE_TYPE( VipsReducehl3, vips_reducehl3, VIPS_TYPE_RESAMPLE );
}
/* Get n points.
*/
int
vips_reduce_get_points( VipsKernel kernel )
{
switch( kernel ) {
case VIPS_KERNEL_NEAREST:
return( 1 );
case VIPS_KERNEL_LINEAR:
return( 2 );
case VIPS_KERNEL_CUBIC:
return( 4 );
case VIPS_KERNEL_LANCZOS2:
return( 4 );
case VIPS_KERNEL_LANCZOS3:
return( 6 );
default:
g_assert_not_reached();
return( 0 );
}
}
/* Calculate a mask.
*/
void
vips_reduce_make_mask( VipsKernel kernel, double x, double *c )
{
switch( kernel ) {
case VIPS_KERNEL_NEAREST:
c[0] = 1.0;
break;
case VIPS_KERNEL_LINEAR:
c[0] = 1.0 - x;
c[1] = x;
break;
case VIPS_KERNEL_CUBIC:
calculate_coefficients_catmull( x, c );
break;
case VIPS_KERNEL_LANCZOS2:
calculate_coefficients_lanczos( 2, x, c );
break;
case VIPS_KERNEL_LANCZOS3:
calculate_coefficients_lanczos( 3, x, c );
break;
default:
g_assert_not_reached();
break;
}
}
template <typename T, int max_value>
static void inline
reducehl3_unsigned_int_tab( VipsReducehl3 *reducehl3,
VipsPel *pout, const VipsPel *pin,
const int bands, const int * restrict cx )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducehl3->n_points;
for( int z = 0; z < bands; z++ ) {
int sum;
sum = reduce_sum<T, int>( in, bands, cx, n );
sum = unsigned_fixed_round( sum );
sum = VIPS_CLIP( 0, sum, max_value );
out[z] = sum;
in += 1;
}
}
/* A 4-point interpolation on uint8 is the most common case ... unroll that.
*
* The inner loop here won't vectorise, but our inner loop doesn't run for
* long enough for vectorisation to be useful :-( gcc says it needs about an
* 11-point kernel for the vector version to be worthwhile.
*/
static void inline
reducehl3_unsigned_uint8_6tab( VipsPel *out, const VipsPel *in,
const int bands, const int *cx )
{
const int b1 = bands;
const int b2 = b1 + b1;
const int b3 = b1 + b2;
const int b4 = b2 + b2;
const int b5 = b1 + b4;
const int c0 = cx[0];
const int c1 = cx[1];
const int c2 = cx[2];
const int c3 = cx[3];
const int c4 = cx[4];
const int c5 = cx[5];
for( int z = 0; z < bands; z++ ) {
int cubich = unsigned_fixed_round(
c0 * in[0] +
c1 * in[b1] +
c2 * in[b2] +
c3 * in[b3] +
c4 * in[b4] +
c5 * in[b5] );
cubich = VIPS_CLIP( 0, cubich, 255 );
out[z] = cubich;
in += 1;
}
}
template <typename T, int min_value, int max_value>
static void inline
reducehl3_signed_int_tab( VipsReducehl3 *reducehl3,
VipsPel *pout, const VipsPel *pin,
const int bands, const int * restrict cx )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducehl3->n_points;
for( int z = 0; z < bands; z++ ) {
int sum;
sum = reduce_sum<T, int>( in, bands, cx, n );
sum = signed_fixed_round( sum );
sum = VIPS_CLIP( min_value, sum, max_value );
out[z] = sum;
in += 1;
}
}
/* Floating-point version.
*/
template <typename T>
static void inline
reducehl3_float_tab( VipsReducehl3 *reducehl3,
VipsPel *pout, const VipsPel *pin,
const int bands, const double *cx )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducehl3->n_points;
for( int z = 0; z < bands; z++ ) {
out[z] = reduce_sum<T, double>( in, bands, cx, n );
in += 1;
}
}
/* 32-bit int output needs a double intermediate.
*/
template <typename T, int max_value>
static void inline
reducehl3_unsigned_int32_tab( VipsReducehl3 *reducehl3,
VipsPel *pout, const VipsPel *pin,
const int bands, const double * restrict cx )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducehl3->n_points;
for( int z = 0; z < bands; z++ ) {
double sum;
sum = reduce_sum<T, double>( in, bands, cx, n );
out[z] = VIPS_CLIP( 0, sum, max_value );
in += 1;
}
}
template <typename T, int min_value, int max_value>
static void inline
reducehl3_signed_int32_tab( VipsReducehl3 *reducehl3,
VipsPel *pout, const VipsPel *pin,
const int bands, const double * restrict cx )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducehl3->n_points;
for( int z = 0; z < bands; z++ ) {
double sum;
sum = reduce_sum<T, double>( in, bands, cx, n );
sum = VIPS_CLIP( min_value, sum, max_value );
out[z] = sum;
in += 1;
}
}
/* Ultra-high-quality version for double images.
*/
template <typename T>
static void inline
reducehl3_notab( VipsReducehl3 *reducehl3,
VipsPel *pout, const VipsPel *pin,
const int bands, double x )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducehl3->n_points;
double cx[MAX_POINTS];
vips_reduce_make_mask( reducehl3->kernel, x, cx );
for( int z = 0; z < bands; z++ ) {
out[z] = reduce_sum<T, double>( in, bands, cx, n );
in += 1;
}
}
static int
vips_reducehl3_gen( VipsRegion *out_region, void *seq,
void *a, void *b, gboolean *stop )
{
VipsImage *in = (VipsImage *) a;
VipsReducehl3 *reducehl3 = (VipsReducehl3 *) 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_reducehl3_gen: generating %d x %d at %d x %d\n",
r->width, r->height, r->left, r->top );
#endif /*DEBUG*/
s.left = r->left * reducehl3->xshrink;
s.top = r->top;
s.width = r->width * reducehl3->xshrink + reducehl3->n_points;
s.height = r->height;
if( vips_region_prepare( ir, &s ) )
return( -1 );
VIPS_GATE_START( "vips_reducehl3_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 * reducehl3->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 = reducehl3->matrixi[tx];
const double *cxf = reducehl3->matrixf[tx];
switch( in->BandFmt ) {
case VIPS_FORMAT_UCHAR:
if( reducehl3->n_points == 6 )
reducehl3_unsigned_uint8_6tab(
q, p, bands, cxi );
else
reducehl3_unsigned_int_tab
<unsigned char, UCHAR_MAX>(
reducehl3,
q, p, bands, cxi );
break;
case VIPS_FORMAT_CHAR:
reducehl3_signed_int_tab
<signed char, SCHAR_MIN, SCHAR_MAX>(
reducehl3,
q, p, bands, cxi );
break;
case VIPS_FORMAT_USHORT:
reducehl3_unsigned_int_tab
<unsigned short, USHRT_MAX>(
reducehl3,
q, p, bands, cxi );
break;
case VIPS_FORMAT_SHORT:
reducehl3_signed_int_tab
<signed short, SHRT_MIN, SHRT_MAX>(
reducehl3,
q, p, bands, cxi );
break;
case VIPS_FORMAT_UINT:
reducehl3_unsigned_int32_tab
<unsigned int, INT_MAX>(
reducehl3,
q, p, bands, cxf );
break;
case VIPS_FORMAT_INT:
reducehl3_signed_int32_tab
<signed int, INT_MIN, INT_MAX>(
reducehl3,
q, p, bands, cxf );
break;
case VIPS_FORMAT_FLOAT:
case VIPS_FORMAT_COMPLEX:
reducehl3_float_tab<float>( reducehl3,
q, p, bands, cxf );
break;
case VIPS_FORMAT_DOUBLE:
case VIPS_FORMAT_DPCOMPLEX:
reducehl3_notab<double>( reducehl3,
q, p, bands, X - ix );
break;
default:
g_assert_not_reached();
break;
}
X += reducehl3->xshrink;
q += ps;
}
}
VIPS_GATE_STOP( "vips_reducehl3_gen: work" );
return( 0 );
}
static int
vips_reducehl3_build( VipsObject *object )
{
VipsObjectClass *object_class = VIPS_OBJECT_GET_CLASS( object );
VipsResample *resample = VIPS_RESAMPLE( object );
VipsReducehl3 *reducehl3 = (VipsReducehl3 *) object;
VipsImage **t = (VipsImage **)
vips_object_local_array( object, 2 );
VipsImage *in;
if( VIPS_OBJECT_CLASS( vips_reducehl3_parent_class )->build( object ) )
return( -1 );
in = resample->in;
if( reducehl3->xshrink < 1 ) {
vips_error( object_class->nickname,
"%s", _( "reduce factors should be >= 1" ) );
return( -1 );
}
if( reducehl3->xshrink > 3 )
vips_warn( object_class->nickname,
"%s", _( "reduce factor greater than 3" ) );
if( reducehl3->xshrink == 1 )
return( vips_image_write( in, resample->out ) );
/* Build the tables of pre-computed coefficients.
*/
reducehl3->n_points = vips_reduce_get_points( reducehl3->kernel );
for( int x = 0; x < VIPS_TRANSFORM_SCALE + 1; x++ ) {
vips_reduce_make_mask( reducehl3->kernel,
(float) x / VIPS_TRANSFORM_SCALE,
reducehl3->matrixf[x] );
for( int i = 0; i < reducehl3->n_points; i++ )
reducehl3->matrixi[x][i] = reducehl3->matrixf[x][i] *
VIPS_INTERPOLATE_SCALE;
}
/* 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],
reducehl3->n_points / 2, 0,
in->Xsize + reducehl3->n_points - 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 - reducehl3->n_points + 1) /
reducehl3->xshrink;
if( resample->out->Xsize <= 0 ) {
vips_error( object_class->nickname,
"%s", _( "image has shrunk to nothing" ) );
return( -1 );
}
#ifdef DEBUG
printf( "vips_reducehl3_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_reducehl3_gen, vips_stop_one,
in, reducehl3 ) )
return( -1 );
return( 0 );
}
static void
vips_reducehl3_class_init( VipsReducehl3Class *reducehl3_class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( reducehl3_class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( reducehl3_class );
VipsOperationClass *operation_class =
VIPS_OPERATION_CLASS( reducehl3_class );
VIPS_DEBUG_MSG( "vips_reducehl3_class_init\n" );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "reducehl3";
vobject_class->description = _( "shrink an image horizontally" );
vobject_class->build = vips_reducehl3_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;
VIPS_ARG_DOUBLE( reducehl3_class, "xshrink", 3,
_( "Xshrink" ),
_( "Horizontal shrink factor" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsReducehl3, xshrink ),
1, 1000000, 1 );
VIPS_ARG_ENUM( reducehl3_class, "kernel", 3,
_( "Kernel" ),
_( "Resampling kernel" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsReducehl3, kernel ),
VIPS_TYPE_KERNEL, VIPS_KERNEL_LANCZOS3 );
}
static void
vips_reducehl3_init( VipsReducehl3 *reducehl3 )
{
reducehl3->kernel = VIPS_KERNEL_LANCZOS3;
}
/**
* vips_reducehl3:
* @in: input image
* @out: output image
* @xshrink: horizontal reduce
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @kernel: #VipsKernel to use to interpolate (default: lanczos3)
*
* Reduce @in horizontally by a float factor. The pixels in @out are
* interpolated with a 1D 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_reducehl3( VipsImage *in, VipsImage **out, double xshrink, ... )
{
va_list ap;
int result;
va_start( ap, xshrink );
result = vips_call_split( "reducehl3", ap, in, out, xshrink );
va_end( ap );
return( result );
}

View File

@ -1,176 +0,0 @@
/* 2D reducel3 ... call reducel3h and reducel3v
*
* 27/1/16
* - from shrink.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 _VipsReducel3 {
VipsResample parent_instance;
double xshrink; /* Shrink factors */
double yshrink;
/* The thing we use to make the kernel.
*/
VipsKernel kernel;
} VipsReducel3;
typedef VipsResampleClass VipsReducel3Class;
G_DEFINE_TYPE( VipsReducel3, vips_reducel3, VIPS_TYPE_RESAMPLE );
static int
vips_reducel3_build( VipsObject *object )
{
VipsResample *resample = VIPS_RESAMPLE( object );
VipsReducel3 *reducel3 = (VipsReducel3 *) object;
VipsImage **t = (VipsImage **)
vips_object_local_array( object, 3 );
if( VIPS_OBJECT_CLASS( vips_reducel3_parent_class )->build( object ) )
return( -1 );
if( vips_reducevl3( resample->in, &t[0], reducel3->yshrink,
"kernel", reducel3->kernel,
NULL ) ||
vips_reducehl3( t[0], &t[1], reducel3->xshrink,
"kernel", reducel3->kernel,
NULL ) ||
vips_image_write( t[1], resample->out ) )
return( -1 );
return( 0 );
}
static void
vips_reducel3_class_init( VipsReducel3Class *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_reducel3_class_init\n" );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "reducel3";
vobject_class->description = _( "reducel3 an image" );
vobject_class->build = vips_reducel3_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL;
VIPS_ARG_DOUBLE( class, "xshrink", 8,
_( "Xshrink" ),
_( "Horizontal shrink factor" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsReducel3, xshrink ),
1.0, 1000000.0, 1.0 );
VIPS_ARG_DOUBLE( class, "yshrink", 9,
_( "Yshrink" ),
_( "Vertical shrink factor" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsReducel3, yshrink ),
1.0, 1000000.0, 1.0 );
VIPS_ARG_ENUM( class, "kernel", 3,
_( "Kernel" ),
_( "Resampling kernel" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsReducel3, kernel ),
VIPS_TYPE_KERNEL, VIPS_KERNEL_LANCZOS3 );
}
static void
vips_reducel3_init( VipsReducel3 *reducel3 )
{
reducel3->kernel = VIPS_KERNEL_LANCZOS3;
}
/**
* vips_reducel3:
* @in: input image
* @out: output image
* @xshrink: horizontal shrink
* @shrinke: vertical shrink
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @kernel: #VipsKernel to use to interpolate (default: lanczos3)
*
* Reducel3 @in by a pair of factors with a pair of 1D 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.
*
* This operation does not change xres or yres. The image resolution needs to
* be updated by the application.
*
* See also: vips_resize(), vips_affine().
*
* Returns: 0 on success, -1 on error
*/
int
vips_reducel3( VipsImage *in, VipsImage **out,
double xshrink, double yshrink, ... )
{
va_list ap;
int result;
va_start( ap, yshrink );
result = vips_call_split( "reducel3", ap, in, out, xshrink, yshrink );
va_end( ap );
return( result );
}

View File

@ -1,7 +1,9 @@
/* vertical reduce by a float factor
/* horizontal reduce by a float factor with lanczos3
*
* 29/1/16
* - from shrinkv.c
* 10/3/16
* - add other kernels
*/
/*
@ -51,57 +53,96 @@
#include "presample.h"
#include "templates.h"
/* The max size of the vector we use.
*/
#define MAX_POINTS (6)
typedef struct _VipsReducev {
VipsResample parent_instance;
double yshrink; /* Shrink factor */
/* The thing we use to make the kernel.
*/
VipsKernel kernel;
/* Number of points in kernel.
*/
int n_points;
/* 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.
*/
int matrixi[VIPS_TRANSFORM_SCALE + 1][MAX_POINTS];
double matrixf[VIPS_TRANSFORM_SCALE + 1][MAX_POINTS];
} 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];
/* We need C linkage for this.
*/
extern "C" {
G_DEFINE_TYPE( VipsReducev, vips_reducev, VIPS_TYPE_RESAMPLE );
}
/* You'd think this would vectorise, but gcc hates mixed types in nested loops
* :-(
*/
template <typename T, int max_value>
static void inline
reducev_unsigned_int_tab( VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip,
const int *cy )
reducev_unsigned_int_tab( VipsReducev *reducev,
VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip, const int * restrict cy )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducev->n_points;
const int l1 = lskip / sizeof( T );
for( int z = 0; z < ne; z++ ) {
int sum;
sum = reduce_sum<T, int>( in + z, l1, cy, n );
sum = unsigned_fixed_round( sum );
sum = VIPS_CLIP( 0, sum, max_value );
out[z] = sum;
}
}
/* An unrolled version of ^^ for the most common case.
*/
static void inline
reducev_unsigned_uint8_6tab( VipsPel *out, const VipsPel *in,
const int ne, const int lskip, const int *cy )
{
const int l1 = lskip;
const int l2 = l1 + l1;
const int l3 = l1 + l2;
const int = l1 + l2;
const int l4 = l2 + l2;
const int l5 = l4 + l1;
const int c0 = cy[0];
const int c1 = cy[1];
const int c2 = cy[2];
const int c3 = cy[3];
const int c4 = cy[4];
const int c5 = cy[5];
for( int z = 0; z < ne; z++ ) {
int cubicv = unsigned_fixed_round(
int sum = unsigned_fixed_round(
c0 * in[0] +
c1 * in[l1] +
c2 * in[l2] +
c3 * in[l3] );
c3 * in[] +
c4 * in[l4] +
c5 * in[l5] );
cubicv = VIPS_CLIP( 0, cubicv, max_value );
sum = VIPS_CLIP( 0, sum, 255 );
out[z] = cubicv;
out[z] = sum;
in += 1;
}
@ -109,135 +150,81 @@ reducev_unsigned_int_tab( VipsPel *pout, const VipsPel *pin,
template <typename T, int min_value, int max_value>
static void inline
reducev_signed_int_tab( VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip,
const int *cy )
reducev_signed_int_tab( VipsReducev *reducev,
VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip, const int * restrict cy )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducev->n_points;
const int l1 = lskip / sizeof( T );
const int l2 = l1 + l1;
const int l3 = l1 + l2;
const int c0 = cy[0];
const int c1 = cy[1];
const int c2 = cy[2];
const int c3 = cy[3];
for( int z = 0; z < ne; z++ ) {
int cubicv = signed_fixed_round(
c0 * in[0] +
c1 * in[l1] +
c2 * in[l2] +
c3 * in[l3] );
int sum;
cubicv = VIPS_CLIP( min_value, cubicv, max_value );
sum = reduce_sum<T, int>( in + z, l1, cy, n );
sum = signed_fixed_round( sum );
sum = VIPS_CLIP( min_value, sum, max_value );
out[z] = cubicv;
in += 1;
out[z] = sum;
}
}
/* Floating-point version.
*/
template <typename T>
static void inline
reducev_float_tab( VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip,
const double *cy )
reducev_float_tab( VipsReducev *reducev,
VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip, const double * restrict cy )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducev->n_points;
const int l1 = lskip / sizeof( T );
const int l2 = l1 + l1;
const int l3 = l1 + l2;
const double c0 = cy[0];
const double c1 = cy[1];
const double c2 = cy[2];
const double c3 = cy[3];
for( int z = 0; z < ne; z++ ) {
out[z] =
c0 * in[0] +
c1 * in[l1] +
c2 * in[l2] +
c3 * in[l3];
in += 1;
}
for( int z = 0; z < ne; z++ )
out[z] = reduce_sum<T, double>( in + z, l1, cy, n );
}
/* 32-bit int version needs a double intermediate.
/* 32-bit int output needs a double intermediate.
*/
template <typename T, int max_value>
static void inline
reducev_unsigned_int32_tab( VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip,
const double *cy )
reducev_unsigned_int32_tab( VipsReducev *reducev,
VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip, const double * restrict cy )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducev->n_points;
const int l1 = lskip / sizeof( T );
const int l2 = l1 + l1;
const int l3 = l1 + l2;
const double c0 = cy[0];
const double c1 = cy[1];
const double c2 = cy[2];
const double c3 = cy[3];
for( int z = 0; z < ne; z++ ) {
double cubicv =
c0 * in[0] +
c1 * in[l1] +
c2 * in[l2] +
c3 * in[l3];
double sum;
cubicv = VIPS_CLIP( 0, cubicv, max_value );
out[z] = cubicv;
in += 1;
sum = reduce_sum<T, double>( in + z, l1, cy, n );
out[z] = VIPS_CLIP( 0, sum, max_value );
}
}
template <typename T, int min_value, int max_value>
static void inline
reducev_signed_int32_tab( VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip,
const double *cy )
reducev_signed_int32_tab( VipsReducev *reducev,
VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip, const double * restrict cy )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducev->n_points;
const int l1 = lskip / sizeof( T );
const int l2 = l1 + l1;
const int l3 = l1 + l2;
const double c0 = cy[0];
const double c1 = cy[1];
const double c2 = cy[2];
const double c3 = cy[3];
for( int z = 0; z < ne; z++ ) {
double cubicv =
c0 * in[0] +
c1 * in[l1] +
c2 * in[l2] +
c3 * in[l3];
double sum;
cubicv = VIPS_CLIP( min_value, cubicv, max_value );
out[z] = cubicv;
in += 1;
sum = reduce_sum<T, double>( in + z, l1, cy, n );
out[z] = VIPS_CLIP( min_value, sum, max_value );
}
}
@ -245,35 +232,21 @@ reducev_signed_int32_tab( VipsPel *pout, const VipsPel *pin,
*/
template <typename T>
static void inline
reducev_notab( VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip,
double y )
reducev_notab( VipsReducev *reducev,
VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip, double y )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducev->n_points;
const int l1 = lskip / sizeof( T );
const int l2 = l1 + l1;
const int l3 = l1 + l2;
double cy[4];
double cy[MAX_POINTS];
calculate_coefficients_catmull( y, cy );
vips_reduce_make_mask( reducev->kernel, y, cy );
const double c0 = cy[0];
const double c1 = cy[1];
const double c2 = cy[2];
const double c3 = cy[3];
for( int z = 0; z < ne; z++ ) {
out[z] =
c0 * in[0] +
c1 * in[l1] +
c2 * in[l2] +
c3 * in[l3];
in += 1;
}
for( int z = 0; z < ne; z++ )
out[z] = reduce_sum<T, double>( in + z, l1, cy, n );
}
static int
@ -301,68 +274,81 @@ vips_reducev_gen( VipsRegion *out_region, void *seq,
s.left = r->left;
s.top = r->top * reducev->yshrink;
s.width = r->width;
s.height = r->height * reducev->yshrink + 4;
s.height = r->height * reducev->yshrink + reducev->n_points;
if( vips_region_prepare( ir, &s ) )
return( -1 );
VIPS_GATE_START( "vips_reducev_gen: work" );
for( int y = 0; y < r->height; y ++ ) {
VipsPel *q = VIPS_REGION_ADDR( out_region, r->left, r->top + 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 );
const int sy = Y * VIPS_TRANSFORM_SCALE * 2;
const int siy = sy & (VIPS_TRANSFORM_SCALE * 2 - 1);
const int ty = (siy + 1) >> 1;
const int *cyi = vips_reducev_matrixi[ty];
const double *cyf = vips_reducev_matrixf[ty];
const int *cyi = reducev->matrixi[ty];
const double *cyf = reducev->matrixf[ty];
const int lskip = VIPS_REGION_LSKIP( ir );
switch( in->BandFmt ) {
case VIPS_FORMAT_UCHAR:
reducev_unsigned_int_tab
<unsigned char, UCHAR_MAX>(
q, p, ne, lskip, cyi );
if( reducev->n_points == 6 )
reducev_unsigned_uint8_6tab(
q, p, ne, lskip, cyi );
else
reducev_unsigned_int_tab
<unsigned char, UCHAR_MAX>(
reducev,
q, p, ne, lskip, cyi );
break;
case VIPS_FORMAT_CHAR:
reducev_signed_int_tab
<signed char, SCHAR_MIN, SCHAR_MAX>(
reducev,
q, p, ne, lskip, cyi );
break;
case VIPS_FORMAT_USHORT:
reducev_unsigned_int_tab
<unsigned short, USHRT_MAX>(
reducev,
q, p, ne, lskip, cyi );
break;
case VIPS_FORMAT_SHORT:
reducev_signed_int_tab
<signed short, SHRT_MIN, SHRT_MAX>(
reducev,
q, p, ne, lskip, cyi );
break;
case VIPS_FORMAT_UINT:
reducev_unsigned_int32_tab
<unsigned int, INT_MAX>(
reducev,
q, p, ne, lskip, cyf );
break;
case VIPS_FORMAT_INT:
reducev_signed_int32_tab
<signed int, INT_MIN, INT_MAX>(
reducev,
q, p, ne, lskip, cyf );
break;
case VIPS_FORMAT_FLOAT:
case VIPS_FORMAT_COMPLEX:
reducev_float_tab<float>( q, p, ne, lskip, cyf );
reducev_float_tab<float>( reducev,
q, p, ne, lskip, cyf );
break;
case VIPS_FORMAT_DPCOMPLEX:
case VIPS_FORMAT_DOUBLE:
reducev_notab<double>( q, p, ne, lskip, Y - (int) Y );
reducev_notab<double>( reducev,
q, p, ne, lskip, Y - (int) Y );
break;
default:
@ -403,6 +389,19 @@ vips_reducev_build( VipsObject *object )
if( reducev->yshrink == 1 )
return( vips_image_write( in, resample->out ) );
/* Build the tables of pre-computed coefficients.
*/
reducev->n_points = vips_reduce_get_points( reducev->kernel );
for( int y = 0; y < VIPS_TRANSFORM_SCALE + 1; y++ ) {
vips_reduce_make_mask( reducev->kernel,
(float) y / VIPS_TRANSFORM_SCALE,
reducev->matrixf[y] );
for( int i = 0; i < reducev->n_points; i++ )
reducev->matrixi[y][i] = reducev->matrixf[y][i] *
VIPS_INTERPOLATE_SCALE;
}
/* Unpack for processing.
*/
if( vips_image_decode( in, &t[0] ) )
@ -412,8 +411,8 @@ vips_reducev_build( VipsObject *object )
/* Add new pixels around the input so we can interpolate at the edges.
*/
if( vips_embed( in, &t[1],
0, 1,
in->Xsize, in->Ysize + 3,
0, reducev->n_points / 2,
in->Xsize, in->Ysize + reducev->n_points - 1,
"extend", VIPS_EXTEND_COPY,
NULL ) )
return( -1 );
@ -429,7 +428,7 @@ vips_reducev_build( VipsObject *object )
* example, vipsthumbnail knows the true reduce factor (including the
* fractional part), we just see the integer part here.
*/
resample->out->Ysize = (in->Ysize - 3) / reducev->yshrink;
resample->out->Ysize = (in->Ysize - reducev->n_points + 1) / reducev->yshrink;
if( resample->out->Ysize <= 0 ) {
vips_error( object_class->nickname,
"%s", _( "image has shrunk to nothing" ) );
@ -476,24 +475,19 @@ vips_reducev_class_init( VipsReducevClass *reducev_class )
G_STRUCT_OFFSET( VipsReducev, yshrink ),
1, 1000000, 1 );
/* Build the tables of pre-computed coefficients.
*/
for( int y = 0; y < VIPS_TRANSFORM_SCALE + 1; y++ ) {
calculate_coefficients_catmull(
(float) y / VIPS_TRANSFORM_SCALE,
vips_reducev_matrixf[y] );
for( int i = 0; i < 4; i++ )
vips_reducev_matrixi[y][i] =
vips_reducev_matrixf[y][i] *
VIPS_INTERPOLATE_SCALE;
}
VIPS_ARG_ENUM( reducev_class, "kernel", 3,
_( "Kernel" ),
_( "Resampling kernel" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsReducev, kernel ),
VIPS_TYPE_KERNEL, VIPS_KERNEL_LANCZOS3 );
}
static void
vips_reducev_init( VipsReducev *reducev )
{
reducev->kernel = VIPS_KERNEL_LANCZOS3;
}
/**
@ -503,8 +497,12 @@ vips_reducev_init( VipsReducev *reducev )
* @yshrink: horizontal reduce
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @kernel: #VipsKernel to use to interpolate (default: lanczos3)
*
* Reduce @in vertically by a float factor. The pixels in @out are
* interpolated with a 1D cubic mask. This operation will not work well for
* interpolated with a 1D 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

View File

@ -1,529 +0,0 @@
/* horizontal reduce by a float factor with lanczos3
*
* 29/1/16
* - from shrinkv.c
* 10/3/16
* - add other kernels
*/
/*
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"
/* The max size of the vector we use.
*/
#define MAX_POINTS (6)
typedef struct _VipsReducevl3 {
VipsResample parent_instance;
double yshrink; /* Shrink factor */
/* The thing we use to make the kernel.
*/
VipsKernel kernel;
/* Number of points in kernel.
*/
int n_points;
/* 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.
*/
int matrixi[VIPS_TRANSFORM_SCALE + 1][MAX_POINTS];
double matrixf[VIPS_TRANSFORM_SCALE + 1][MAX_POINTS];
} VipsReducevl3;
typedef VipsResampleClass VipsReducevl3Class;
/* We need C linkage for this.
*/
extern "C" {
G_DEFINE_TYPE( VipsReducevl3, vips_reducevl3, VIPS_TYPE_RESAMPLE );
}
/* You'd think this would vectorise, but gcc hates mixed types in nested loops
* :-(
*/
template <typename T, int max_value>
static void inline
reducevl3_unsigned_int_tab( VipsReducevl3 *reducevl3,
VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip, const int * restrict cy )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducevl3->n_points;
const int l1 = lskip / sizeof( T );
for( int z = 0; z < ne; z++ ) {
int sum;
sum = reduce_sum<T, int>( in + z, l1, cy, n );
sum = unsigned_fixed_round( sum );
sum = VIPS_CLIP( 0, sum, max_value );
out[z] = sum;
}
}
/* An unrolled version of ^^ for the most common case.
*/
static void inline
reducevl3_unsigned_uint8_6tab( VipsPel *out, const VipsPel *in,
const int ne, const int lskip, const int *cy )
{
const int l1 = lskip;
const int l2 = l1 + l1;
const int l3 = l1 + l2;
const int l4 = l2 + l2;
const int l5 = l4 + l1;
const int c0 = cy[0];
const int c1 = cy[1];
const int c2 = cy[2];
const int c3 = cy[3];
const int c4 = cy[4];
const int c5 = cy[5];
for( int z = 0; z < ne; z++ ) {
int sum = unsigned_fixed_round(
c0 * in[0] +
c1 * in[l1] +
c2 * in[l2] +
c3 * in[l3] +
c4 * in[l4] +
c5 * in[l5] );
sum = VIPS_CLIP( 0, sum, 255 );
out[z] = sum;
in += 1;
}
}
template <typename T, int min_value, int max_value>
static void inline
reducevl3_signed_int_tab( VipsReducevl3 *reducevl3,
VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip, const int * restrict cy )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducevl3->n_points;
const int l1 = lskip / sizeof( T );
for( int z = 0; z < ne; z++ ) {
int sum;
sum = reduce_sum<T, int>( in + z, l1, cy, n );
sum = signed_fixed_round( sum );
sum = VIPS_CLIP( min_value, sum, max_value );
out[z] = sum;
}
}
/* Floating-point version.
*/
template <typename T>
static void inline
reducevl3_float_tab( VipsReducevl3 *reducevl3,
VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip, const double * restrict cy )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducevl3->n_points;
const int l1 = lskip / sizeof( T );
for( int z = 0; z < ne; z++ )
out[z] = reduce_sum<T, double>( in + z, l1, cy, n );
}
/* 32-bit int output needs a double intermediate.
*/
template <typename T, int max_value>
static void inline
reducevl3_unsigned_int32_tab( VipsReducevl3 *reducevl3,
VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip, const double * restrict cy )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducevl3->n_points;
const int l1 = lskip / sizeof( T );
for( int z = 0; z < ne; z++ ) {
double sum;
sum = reduce_sum<T, double>( in + z, l1, cy, n );
out[z] = VIPS_CLIP( 0, sum, max_value );
}
}
template <typename T, int min_value, int max_value>
static void inline
reducevl3_signed_int32_tab( VipsReducevl3 *reducevl3,
VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip, const double * restrict cy )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducevl3->n_points;
const int l1 = lskip / sizeof( T );
for( int z = 0; z < ne; z++ ) {
double sum;
sum = reduce_sum<T, double>( in + z, l1, cy, n );
out[z] = VIPS_CLIP( min_value, sum, max_value );
}
}
/* Ultra-high-quality version for double images.
*/
template <typename T>
static void inline
reducevl3_notab( VipsReducevl3 *reducevl3,
VipsPel *pout, const VipsPel *pin,
const int ne, const int lskip, double y )
{
T* restrict out = (T *) pout;
const T* restrict in = (T *) pin;
const int n = reducevl3->n_points;
const int l1 = lskip / sizeof( T );
double cy[MAX_POINTS];
vips_reduce_make_mask( reducevl3->kernel, y, cy );
for( int z = 0; z < ne; z++ )
out[z] = reduce_sum<T, double>( in + z, l1, cy, n );
}
static int
vips_reducevl3_gen( VipsRegion *out_region, void *seq,
void *a, void *b, gboolean *stop )
{
VipsImage *in = (VipsImage *) a;
VipsReducevl3 *reducevl3 = (VipsReducevl3 *) b;
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);
int ne = r->width * bands;
VipsRect s;
#ifdef DEBUG
printf( "vips_reducevl3_gen: generating %d x %d at %d x %d\n",
r->width, r->height, r->left, r->top );
#endif /*DEBUG*/
s.left = r->left;
s.top = r->top * reducevl3->yshrink;
s.width = r->width;
s.height = r->height * reducevl3->yshrink + reducevl3->n_points;
if( vips_region_prepare( ir, &s ) )
return( -1 );
VIPS_GATE_START( "vips_reducevl3_gen: work" );
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) * reducevl3->yshrink;
VipsPel *p = VIPS_REGION_ADDR( ir, r->left, (int) Y );
const int sy = Y * VIPS_TRANSFORM_SCALE * 2;
const int siy = sy & (VIPS_TRANSFORM_SCALE * 2 - 1);
const int ty = (siy + 1) >> 1;
const int *cyi = reducevl3->matrixi[ty];
const double *cyf = reducevl3->matrixf[ty];
const int lskip = VIPS_REGION_LSKIP( ir );
switch( in->BandFmt ) {
case VIPS_FORMAT_UCHAR:
if( reducevl3->n_points == 6 )
reducevl3_unsigned_uint8_6tab(
q, p, ne, lskip, cyi );
else
reducevl3_unsigned_int_tab
<unsigned char, UCHAR_MAX>(
reducevl3,
q, p, ne, lskip, cyi );
break;
case VIPS_FORMAT_CHAR:
reducevl3_signed_int_tab
<signed char, SCHAR_MIN, SCHAR_MAX>(
reducevl3,
q, p, ne, lskip, cyi );
break;
case VIPS_FORMAT_USHORT:
reducevl3_unsigned_int_tab
<unsigned short, USHRT_MAX>(
reducevl3,
q, p, ne, lskip, cyi );
break;
case VIPS_FORMAT_SHORT:
reducevl3_signed_int_tab
<signed short, SHRT_MIN, SHRT_MAX>(
reducevl3,
q, p, ne, lskip, cyi );
break;
case VIPS_FORMAT_UINT:
reducevl3_unsigned_int32_tab
<unsigned int, INT_MAX>(
reducevl3,
q, p, ne, lskip, cyf );
break;
case VIPS_FORMAT_INT:
reducevl3_signed_int32_tab
<signed int, INT_MIN, INT_MAX>(
reducevl3,
q, p, ne, lskip, cyf );
break;
case VIPS_FORMAT_FLOAT:
case VIPS_FORMAT_COMPLEX:
reducevl3_float_tab<float>( reducevl3,
q, p, ne, lskip, cyf );
break;
case VIPS_FORMAT_DPCOMPLEX:
case VIPS_FORMAT_DOUBLE:
reducevl3_notab<double>( reducevl3,
q, p, ne, lskip, Y - (int) Y );
break;
default:
g_assert_not_reached();
break;
}
}
VIPS_GATE_STOP( "vips_reducevl3_gen: work" );
return( 0 );
}
static int
vips_reducevl3_build( VipsObject *object )
{
VipsObjectClass *object_class = VIPS_OBJECT_GET_CLASS( object );
VipsResample *resample = VIPS_RESAMPLE( object );
VipsReducevl3 *reducevl3 = (VipsReducevl3 *) object;
VipsImage **t = (VipsImage **) vips_object_local_array( object, 2 );
VipsImage *in;
if( VIPS_OBJECT_CLASS( vips_reducevl3_parent_class )->build( object ) )
return( -1 );
in = resample->in;
if( reducevl3->yshrink < 1 ) {
vips_error( object_class->nickname,
"%s", _( "reduce factors should be >= 1" ) );
return( -1 );
}
if( reducevl3->yshrink > 3 )
vips_warn( object_class->nickname,
"%s", _( "reduce factor greater than 3" ) );
if( reducevl3->yshrink == 1 )
return( vips_image_write( in, resample->out ) );
/* Build the tables of pre-computed coefficients.
*/
reducevl3->n_points = vips_reduce_get_points( reducevl3->kernel );
for( int y = 0; y < VIPS_TRANSFORM_SCALE + 1; y++ ) {
vips_reduce_make_mask( reducevl3->kernel,
(float) y / VIPS_TRANSFORM_SCALE,
reducevl3->matrixf[y] );
for( int i = 0; i < reducevl3->n_points; i++ )
reducevl3->matrixi[y][i] = reducevl3->matrixf[y][i] *
VIPS_INTERPOLATE_SCALE;
}
/* 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],
0, reducevl3->n_points / 2,
in->Xsize, in->Ysize + reducevl3->n_points - 1,
"extend", VIPS_EXTEND_COPY,
NULL ) )
return( -1 );
in = t[1];
if( vips_image_pipelinev( resample->out,
VIPS_DEMAND_STYLE_SMALLTILE, 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->Ysize = (in->Ysize - reducevl3->n_points + 1) / reducevl3->yshrink;
if( resample->out->Ysize <= 0 ) {
vips_error( object_class->nickname,
"%s", _( "image has shrunk to nothing" ) );
return( -1 );
}
#ifdef DEBUG
printf( "vips_reducevl3_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_reducevl3_gen, vips_stop_one,
in, reducevl3 ) )
return( -1 );
return( 0 );
}
static void
vips_reducevl3_class_init( VipsReducevl3Class *reducevl3_class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( reducevl3_class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( reducevl3_class );
VipsOperationClass *operation_class =
VIPS_OPERATION_CLASS( reducevl3_class );
VIPS_DEBUG_MSG( "vips_reducevl3_class_init\n" );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "reducevl3";
vobject_class->description = _( "shrink an image vertically" );
vobject_class->build = vips_reducevl3_build;
operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;
VIPS_ARG_DOUBLE( reducevl3_class, "yshrink", 3,
_( "Xshrink" ),
_( "Vertical shrink factor" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsReducevl3, yshrink ),
1, 1000000, 1 );
VIPS_ARG_ENUM( reducevl3_class, "kernel", 3,
_( "Kernel" ),
_( "Resampling kernel" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsReducevl3, kernel ),
VIPS_TYPE_KERNEL, VIPS_KERNEL_LANCZOS3 );
}
static void
vips_reducevl3_init( VipsReducevl3 *reducevl3 )
{
reducevl3->kernel = VIPS_KERNEL_LANCZOS3;
}
/**
* vips_reducevl3:
* @in: input image
* @out: output image
* @yshrink: horizontal reduce
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @kernel: #VipsKernel to use to interpolate (default: lanczos3)
*
* Reduce @in vertically by a float factor. The pixels in @out are
* interpolated with a 1D 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_reducevl3( VipsImage *in, VipsImage **out, double yshrink, ... )
{
va_list ap;
int result;
va_start( ap, yshrink );
result = vips_call_split( "reducevl3", ap, in, out, yshrink );
va_end( ap );
return( result );
}

View File

@ -138,11 +138,8 @@ vips_resample_operation_init( void )
extern GType vips_shrinkh_get_type( void );
extern GType vips_shrinkv_get_type( void );
extern GType vips_reduce_get_type( void );
extern GType vips_reducel3_get_type( void );
extern GType vips_reduceh_get_type( void );
extern GType vips_reducehl3_get_type( void );
extern GType vips_reducev_get_type( void );
extern GType vips_reducevl3_get_type( void );
extern GType vips_quadratic_get_type( void );
extern GType vips_affine_get_type( void );
extern GType vips_similarity_get_type( void );
@ -153,11 +150,8 @@ vips_resample_operation_init( void )
vips_shrinkh_get_type();
vips_shrinkv_get_type();
vips_reduceh_get_type();
vips_reducehl3_get_type();
vips_reducev_get_type();
vips_reducevl3_get_type();
vips_reduce_get_type();
vips_reducel3_get_type();
vips_quadratic_get_type();
vips_affine_get_type();
vips_similarity_get_type();

View File

@ -267,6 +267,11 @@ class TestForeign(unittest.TestCase):
self.save_load("%s.webp", self.colour)
def test_analyzeload(self):
x = Vips.type_find("VipsForeign", "analyzeload")
if not x.is_instantiatable():
print("no analyze support in this vips, skipping test")
return
def analyze_valid(self, im):
a = im(10, 10)
self.assertAlmostEqual(a[0], 3335)
@ -416,10 +421,20 @@ class TestForeign(unittest.TestCase):
self.save_load("%s.mat", self.mono)
def test_ppm(self):
x = Vips.type_find("VipsForeign", "ppmload")
if not x.is_instantiatable():
print("no PPM support in this vips, skipping test")
return
self.save_load("%s.ppm", self.mono)
self.save_load("%s.ppm", self.colour)
def test_rad(self):
x = Vips.type_find("VipsForeign", "radload")
if not x.is_instantiatable():
print("no Radiance support in this vips, skipping test")
return
self.save_load("%s.hdr", self.colour)
def test_dzsave(self):