2009-09-19 13:13:28 +02:00
|
|
|
/* round.c --- various rounding operations
|
|
|
|
*
|
|
|
|
* 20/6/02 JC
|
|
|
|
* - adapted from im_abs()
|
|
|
|
* 29/8/09
|
|
|
|
* - gtkdoc
|
|
|
|
* - tiny cleanups
|
|
|
|
* 19/9/09
|
|
|
|
* - im_ceil.c adapted to make round.c
|
2011-11-10 14:53:58 +01:00
|
|
|
* 10/11/11
|
|
|
|
* - redone as a class
|
2009-09-19 13:13:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
2011-11-10 14:53:58 +01:00
|
|
|
Copyright (C) 1991-2005 The National Gallery
|
|
|
|
|
2012-09-17 12:52:32 +02:00
|
|
|
This library 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.1 of the License, or (at your option) any later version.
|
2009-09-19 13:13:28 +02:00
|
|
|
|
2012-09-17 12:52:32 +02:00
|
|
|
This library is distributed in the hope that it will be useful,
|
2009-09-19 13:13:28 +02:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-09-17 12:52:32 +02:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
2009-09-19 13:13:28 +02:00
|
|
|
|
2012-09-17 12:52:32 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
2013-03-07 06:40:19 +01:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
02110-1301 USA
|
2009-09-19 13:13:28 +02:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-11-10 14:53:58 +01:00
|
|
|
/*
|
|
|
|
#define DEBUG
|
|
|
|
*/
|
|
|
|
|
2009-09-19 13:13:28 +02:00
|
|
|
#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>
|
|
|
|
|
2011-11-10 14:53:58 +01:00
|
|
|
#include "unary.h"
|
|
|
|
|
|
|
|
typedef struct _VipsRound {
|
|
|
|
VipsUnary parent_instance;
|
|
|
|
|
|
|
|
VipsOperationRound round;
|
|
|
|
|
|
|
|
} VipsRound;
|
|
|
|
|
|
|
|
typedef VipsUnaryClass VipsRoundClass;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE( VipsRound, vips_round, VIPS_TYPE_UNARY );
|
|
|
|
|
|
|
|
static int
|
|
|
|
vips_round_build( VipsObject *object )
|
|
|
|
{
|
|
|
|
VipsUnary *unary = (VipsUnary *) object;
|
|
|
|
|
2011-11-22 13:00:32 +01:00
|
|
|
/* Is this one of the int types? Degenerate to vips_copy() if it
|
2011-11-10 14:53:58 +01:00
|
|
|
* is.
|
|
|
|
*/
|
|
|
|
if( unary->in &&
|
2011-11-22 13:00:32 +01:00
|
|
|
vips_bandfmt_isint( unary->in->BandFmt ) )
|
|
|
|
return( vips_unary_copy( unary ) );
|
2011-11-10 14:53:58 +01:00
|
|
|
|
|
|
|
if( VIPS_OBJECT_CLASS( vips_round_parent_class )->build( object ) )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOOP( TYPE, OP ) { \
|
|
|
|
TYPE *p = (TYPE *) in[0]; \
|
2009-09-19 13:13:28 +02:00
|
|
|
TYPE *q = (TYPE *) out; \
|
|
|
|
\
|
2011-11-10 14:53:58 +01:00
|
|
|
for( x = 0; x < sz; x++ ) \
|
|
|
|
q[x] = OP( p[x] ); \
|
2009-09-19 13:13:28 +02:00
|
|
|
}
|
|
|
|
|
2011-11-10 14:53:58 +01:00
|
|
|
#define SWITCH( OP ) { \
|
|
|
|
switch( vips_image_get_format( im ) ) { \
|
|
|
|
case VIPS_FORMAT_COMPLEX: \
|
|
|
|
case VIPS_FORMAT_FLOAT: LOOP( float, OP ); break; \
|
2009-09-19 13:13:28 +02:00
|
|
|
\
|
2011-11-10 14:53:58 +01:00
|
|
|
case VIPS_FORMAT_DPCOMPLEX: \
|
|
|
|
case VIPS_FORMAT_DOUBLE:LOOP( double, OP ); break;\
|
|
|
|
\
|
|
|
|
default: \
|
2009-09-19 13:13:28 +02:00
|
|
|
g_assert( 0 ); \
|
2011-11-10 14:53:58 +01:00
|
|
|
} \
|
2009-09-19 13:13:28 +02:00
|
|
|
}
|
|
|
|
|
2011-11-10 14:53:58 +01:00
|
|
|
static void
|
2011-12-31 19:22:42 +01:00
|
|
|
vips_round_buffer( VipsArithmetic *arithmetic,
|
|
|
|
VipsPel *out, VipsPel **in, int width )
|
2011-11-10 14:53:58 +01:00
|
|
|
{
|
|
|
|
VipsRound *round = (VipsRound *) arithmetic;
|
|
|
|
VipsImage *im = arithmetic->ready[0];
|
2009-09-19 13:13:28 +02:00
|
|
|
|
2011-11-10 14:53:58 +01:00
|
|
|
/* Complex just doubles the size.
|
2009-09-19 13:13:28 +02:00
|
|
|
*/
|
2011-11-10 14:53:58 +01:00
|
|
|
const int sz = width * im->Bands *
|
|
|
|
(vips_bandfmt_iscomplex( im->BandFmt ) ? 2 : 1);
|
2009-09-19 13:13:28 +02:00
|
|
|
|
2011-11-10 14:53:58 +01:00
|
|
|
int x;
|
2009-09-19 13:13:28 +02:00
|
|
|
|
2011-11-10 14:53:58 +01:00
|
|
|
switch( round->round ) {
|
2011-11-18 15:34:33 +01:00
|
|
|
case VIPS_OPERATION_ROUND_RINT: SWITCH( VIPS_RINT ); break;
|
|
|
|
case VIPS_OPERATION_ROUND_CEIL: SWITCH( ceil ); break;
|
|
|
|
case VIPS_OPERATION_ROUND_FLOOR: SWITCH( floor ); break;
|
2009-09-19 13:13:28 +02:00
|
|
|
|
2011-11-10 14:53:58 +01:00
|
|
|
default:
|
|
|
|
g_assert( 0 );
|
|
|
|
}
|
2009-09-19 13:13:28 +02:00
|
|
|
}
|
|
|
|
|
2011-11-10 14:53:58 +01:00
|
|
|
/* Save a bit of typing.
|
2009-09-19 13:13:28 +02:00
|
|
|
*/
|
2011-11-10 14:53:58 +01:00
|
|
|
#define UC VIPS_FORMAT_UCHAR
|
|
|
|
#define C VIPS_FORMAT_CHAR
|
|
|
|
#define US VIPS_FORMAT_USHORT
|
|
|
|
#define S VIPS_FORMAT_SHORT
|
|
|
|
#define UI VIPS_FORMAT_UINT
|
|
|
|
#define I VIPS_FORMAT_INT
|
|
|
|
#define F VIPS_FORMAT_FLOAT
|
|
|
|
#define X VIPS_FORMAT_COMPLEX
|
|
|
|
#define D VIPS_FORMAT_DOUBLE
|
|
|
|
#define DX VIPS_FORMAT_DPCOMPLEX
|
|
|
|
|
|
|
|
static const VipsBandFormat vips_bandfmt_round[10] = {
|
|
|
|
/* UC C US S UI I F X D DX */
|
|
|
|
UC, C, US, S, UI, I, F, X, D, DX
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
vips_round_class_init( VipsRoundClass *class )
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
|
|
|
|
VipsObjectClass *object_class = (VipsObjectClass *) class;
|
|
|
|
VipsArithmeticClass *aclass = VIPS_ARITHMETIC_CLASS( class );
|
|
|
|
|
|
|
|
gobject_class->set_property = vips_object_set_property;
|
|
|
|
gobject_class->get_property = vips_object_get_property;
|
|
|
|
|
|
|
|
object_class->nickname = "round";
|
|
|
|
object_class->description = _( "perform a round function on an image" );
|
|
|
|
object_class->build = vips_round_build;
|
|
|
|
|
|
|
|
vips_arithmetic_set_format_table( aclass, vips_bandfmt_round );
|
|
|
|
|
|
|
|
aclass->process_line = vips_round_buffer;
|
|
|
|
|
|
|
|
VIPS_ARG_ENUM( class, "round", 200,
|
|
|
|
_( "Round operation" ),
|
|
|
|
_( "rounding operation to perform" ),
|
|
|
|
VIPS_ARGUMENT_REQUIRED_INPUT,
|
|
|
|
G_STRUCT_OFFSET( VipsRound, round ),
|
2011-11-18 15:34:33 +01:00
|
|
|
VIPS_TYPE_OPERATION_ROUND, VIPS_OPERATION_ROUND_RINT );
|
2009-09-19 13:13:28 +02:00
|
|
|
}
|
|
|
|
|
2011-11-10 14:53:58 +01:00
|
|
|
static void
|
|
|
|
vips_round_init( VipsRound *round )
|
|
|
|
{
|
2009-09-19 13:13:28 +02:00
|
|
|
}
|
|
|
|
|
2011-11-18 15:34:33 +01:00
|
|
|
static int
|
|
|
|
vips_roundv( VipsImage *in, VipsImage **out,
|
|
|
|
VipsOperationRound round, va_list ap )
|
|
|
|
{
|
|
|
|
return( vips_call_split( "round", ap, in, out, round ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vips_round:
|
|
|
|
* @in: input #VipsImage
|
|
|
|
* @out: output #VipsImage
|
|
|
|
* @round: #VipsOperationRound rounding operation to perform
|
|
|
|
* @...: %NULL-terminated list of optional named arguments
|
|
|
|
*
|
|
|
|
* Round to an integral value.
|
|
|
|
*
|
|
|
|
* Copy for integer types, round float and
|
|
|
|
* complex types.
|
|
|
|
*
|
|
|
|
* The format of @out is always the same as @in, so you may wish to cast to an
|
|
|
|
* integer format afterwards.
|
|
|
|
*
|
|
|
|
* See also: vips_cast()
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 on error
|
|
|
|
*/
|
2011-11-10 14:53:58 +01:00
|
|
|
int
|
|
|
|
vips_round( VipsImage *in, VipsImage **out, VipsOperationRound round, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int result;
|
2009-09-19 13:13:28 +02:00
|
|
|
|
2011-11-10 14:53:58 +01:00
|
|
|
va_start( ap, round );
|
2011-11-18 15:34:33 +01:00
|
|
|
result = vips_roundv( in, out, round, ap );
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
return( result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vips_floor:
|
|
|
|
* @in: input #VipsImage
|
|
|
|
* @out: output #VipsImage
|
|
|
|
* @...: %NULL-terminated list of optional named arguments
|
|
|
|
*
|
|
|
|
* Round to an integral value with #VIPS_OPERATION_ROUND_FLOOR. See
|
|
|
|
* vips_round().
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 on error
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vips_floor( VipsImage *in, VipsImage **out, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
va_start( ap, out );
|
|
|
|
result = vips_roundv( in, out, VIPS_OPERATION_ROUND_FLOOR, ap );
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
return( result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vips_ceil:
|
|
|
|
* @in: input #VipsImage
|
|
|
|
* @out: output #VipsImage
|
|
|
|
* @...: %NULL-terminated list of optional named arguments
|
|
|
|
*
|
|
|
|
* Round to an integral value with #VIPS_OPERATION_ROUND_CEIL. See
|
|
|
|
* vips_round().
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 on error
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vips_ceil( VipsImage *in, VipsImage **out, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
va_start( ap, out );
|
|
|
|
result = vips_roundv( in, out, VIPS_OPERATION_ROUND_CEIL, ap );
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
return( result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vips_rint:
|
|
|
|
* @in: input #VipsImage
|
|
|
|
* @out: output #VipsImage
|
|
|
|
* @...: %NULL-terminated list of optional named arguments
|
|
|
|
*
|
|
|
|
* Round to an integral value with #VIPS_OPERATION_ROUND_RINT. See
|
|
|
|
* vips_round().
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, -1 on error
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vips_rint( VipsImage *in, VipsImage **out, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
va_start( ap, out );
|
|
|
|
result = vips_roundv( in, out, VIPS_OPERATION_ROUND_RINT, ap );
|
2011-11-10 14:53:58 +01:00
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
return( result );
|
2009-09-19 13:13:28 +02:00
|
|
|
}
|