moved remainder over to a class

This commit is contained in:
John Cupitt 2011-11-12 13:29:32 +00:00
parent 827e5311c1
commit 3c608233e5
12 changed files with 652 additions and 520 deletions

View File

@ -10,7 +10,7 @@
im_abs(), im_sign(), im_max(), im_maxpos(), im_deviate(), im_divide(),
im_multiply(), im_stats(), im_measure(), im_recomb(), im_floor(), im_ceil(),
im_rint(), im_equal*(), im_notequal*(), im_less*(), im_lesseq*(), im_more*(),
im_moreeq*()
im_moreeq*(), im_remainder*()
redone as classes
- added argument priorites to help control arg ordering
- generate has a 'stop' param to signal successful early termination

6
TODO
View File

@ -2,7 +2,11 @@
- remainder, power move to unary const
- move power, boolean to unary const
- boolean.c

View File

@ -14,7 +14,7 @@ libarithmetic_la_SOURCES = \
measure.c \
multiply.c \
im_point_bilinear.c \
im_remainder.c \
remainder.c \
sign.c \
stats.c \
statistic.c \

View File

@ -520,6 +520,8 @@ vips_arithmetic_operation_init( void )
extern GType vips_round_get_type( void );
extern GType vips_relational_get_type( void );
extern GType vips_relational_const_get_type( void );
extern GType vips_remainder_get_type( void );
extern GType vips_remainder_const_get_type( void );
vips_add_get_type();
vips_subtract_get_type();
@ -540,4 +542,6 @@ vips_arithmetic_operation_init( void )
vips_round_get_type();
vips_relational_get_type();
vips_relational_const_get_type();
vips_remainder_get_type();
vips_remainder_const_get_type();
}

View File

@ -1,472 +0,0 @@
/* im_remainder.c
*
* 2/8/99 JC
* - im_divide adapted to make im_remainder
* 8/5/02 JC
* - im_remainderconst added
* - im_remainderconst_vec added
* 27/9/04
* - updated for 1 band $op n band image -> n band image case
* 26/2/07
* - oop, broken for _vec case :-(
* 14/5/08
* - better /0 test
* 27/8/08
* - revise upcasting system
* - add gtkdoc comments
* 23/6/10
* - constant ops clip to target range
*/
/*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
#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/internal.h>
/* Integer remainder-after-division.
*/
#define IREMAINDER( TYPE ) { \
TYPE *p1 = (TYPE *) in[0]; \
TYPE *p2 = (TYPE *) in[1]; \
TYPE *q = (TYPE *) out; \
\
for( x = 0; x < ne; x++ ) \
if( p2[x] ) \
q[x] = p1[x] % p2[x]; \
else \
q[x] = -1; \
}
/* Float remainder-after-division.
*/
#define FREMAINDER( TYPE ) { \
TYPE *p1 = (TYPE *) in[0]; \
TYPE *p2 = (TYPE *) in[1]; \
TYPE *q = (TYPE *) out; \
\
for( x = 0; x < ne; x++ ) { \
double a = p1[x]; \
double b = p2[x]; \
\
if( b ) \
q[x] = a - b * floor (a / b); \
else \
q[x] = -1; \
} \
}
static void
remainder_buffer( PEL **in, PEL *out, int width, IMAGE *im )
{
const int ne = width * im->Bands;
int x;
switch( im->BandFmt ) {
case IM_BANDFMT_CHAR: IREMAINDER( signed char ); break;
case IM_BANDFMT_UCHAR: IREMAINDER( unsigned char ); break;
case IM_BANDFMT_SHORT: IREMAINDER( signed short ); break;
case IM_BANDFMT_USHORT: IREMAINDER( unsigned short ); break;
case IM_BANDFMT_INT: IREMAINDER( signed int ); break;
case IM_BANDFMT_UINT: IREMAINDER( unsigned int ); break;
case IM_BANDFMT_FLOAT: FREMAINDER( float ); break;
case IM_BANDFMT_DOUBLE: FREMAINDER( double ); break;
default:
g_assert( 0 );
}
}
/* Save a bit of typing.
*/
#define UC IM_BANDFMT_UCHAR
#define C IM_BANDFMT_CHAR
#define US IM_BANDFMT_USHORT
#define S IM_BANDFMT_SHORT
#define UI IM_BANDFMT_UINT
#define I IM_BANDFMT_INT
#define F IM_BANDFMT_FLOAT
#define X IM_BANDFMT_COMPLEX
#define D IM_BANDFMT_DOUBLE
#define DX IM_BANDFMT_DPCOMPLEX
/* Type promotion for remainder. Keep in sync with remainder_buffer() above.
*/
static int bandfmt_remainder[10] = {
/* UC C US S UI I F X D DX */
UC, C, US, S, UI, I, F, X, D, DX
};
/**
* im_remainder:
* @in1: input #IMAGE 1
* @in2: input #IMAGE 2
* @out: output #IMAGE
*
* This operation calculates @in1 % @in2 (remainder after division) and writes
* the result to @out. The images may have any
* non-complex format. For float formats, im_remainder() calculates @in1 -
* @in2 * floor (@in1 / @in2).
*
* If the images differ in size, the smaller image is enlarged to match the
* larger by adding zero pixels along the bottom and right.
*
* If the number of bands differs, one of the images
* must have one band. In this case, an n-band image is formed from the
* one-band image by joining n copies of the one-band image together, and then
* the two n-band images are operated upon.
*
* The two input images are cast up to the smallest common type (see table
* Smallest common format in
* <link linkend="VIPS-arithmetic">arithmetic</link>), and that format is the
* result type.
*
* See also: im_remainderconst(), im_divide().
*
* Returns: 0 on success, -1 on error
*/
int
im_remainder( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
if( im_check_noncomplex( "im_remainder", in1 ) ||
im_check_noncomplex( "im_remainder", in2 ) )
return( -1 );
return( im__arith_binary( "im_remainder",
in1, in2, out,
bandfmt_remainder,
(im_wrapmany_fn) remainder_buffer, NULL ) );
}
/* Cast a vector of double to a vector of TYPE, clipping to a range.
*/
#define CAST_CLIP( TYPE, N, X ) { \
TYPE *tq = (TYPE *) q; \
\
for( i = 0; i < n; i++ ) \
tq[i] = (TYPE) IM_CLIP( N, p[i], X ); \
}
/* Cast a vector of double to a vector of TYPE.
*/
#define CAST( TYPE ) { \
TYPE *tq = (TYPE *) q; \
\
for( i = 0; i < n; i++ ) \
tq[i] = (TYPE) p[i]; \
}
/* Cast a vector of double to a complex vector of TYPE.
*/
#define CASTC( TYPE ) { \
TYPE *tq = (TYPE *) q; \
\
for( i = 0; i < n; i++ ) { \
tq[0] = (TYPE) p[i]; \
tq[1] = 0; \
tq += 2; \
} \
}
/* Cast a vector of double to a passed format.
*/
static PEL *
make_pixel( IMAGE *out, VipsBandFmt fmt, int n, double *p )
{
PEL *q;
int i;
if( !(q = IM_ARRAY( out, n * (im_bits_of_fmt( fmt ) >> 3), PEL )) )
return( NULL );
switch( fmt ) {
case IM_BANDFMT_CHAR:
CAST_CLIP( signed char, SCHAR_MIN, SCHAR_MAX );
break;
case IM_BANDFMT_UCHAR:
CAST_CLIP( unsigned char, 0, UCHAR_MAX );
break;
case IM_BANDFMT_SHORT:
CAST_CLIP( signed short, SCHAR_MIN, SCHAR_MAX );
break;
case IM_BANDFMT_USHORT:
CAST_CLIP( unsigned short, 0, USHRT_MAX );
break;
case IM_BANDFMT_INT:
CAST_CLIP( signed int, INT_MIN, INT_MAX );
break;
case IM_BANDFMT_UINT:
CAST_CLIP( unsigned int, 0, UINT_MAX );
break;
case IM_BANDFMT_FLOAT:
CAST( float );
break;
case IM_BANDFMT_DOUBLE:
CAST( double );
break;
case IM_BANDFMT_COMPLEX:
CASTC( float );
break;
case IM_BANDFMT_DPCOMPLEX:
CASTC( double );
break;
default:
g_assert( 0 );
}
return( q );
}
int
im__arith_binary_const( const char *domain,
IMAGE *in, IMAGE *out,
int n, double *c, VipsBandFmt vfmt,
int format_table[10],
im_wrapone_fn fn1, im_wrapone_fn fnn )
{
PEL *vector;
if( im_piocheck( in, out ) ||
im_check_vector( domain, n, in ) ||
im_check_uncoded( domain, in ) )
return( -1 );
if( im_cp_desc( out, in ) )
return( -1 );
out->BandFmt = format_table[in->BandFmt];
/* Some operations need the vector in the input type (eg.
* im_equal_vec() where the output type is always uchar and is useless
* for comparisons), some need it in the output type (eg.
* im_andimage_vec() where we want to get the double to an int so we
* can do bitwise-and without having to cast for each pixel), some
* need a fixed type (eg. im_powtra_vec(), where we want to keep it as
* double).
*
* Therefore pass in the desired vector type as a param.
*/
if( !(vector = make_pixel( out, vfmt, n, c )) )
return( -1 );
/* Band-up the input image if we have a >1 vector and
* a 1-band image.
*/
if( n > 1 && out->Bands == 1 ) {
IMAGE *t;
if( !(t = im_open_local( out, domain, "p" )) ||
im__bandup( domain, in, t, n ) )
return( -1 );
in = t;
}
if( n == 1 ) {
if( im_wrapone( in, out, fn1, vector, in ) )
return( -1 );
}
else {
if( im_wrapone( in, out, fnn, vector, in ) )
return( -1 );
}
return( 0 );
}
/* Integer remainder-after-divide, single constant.
*/
#define IREMAINDERCONST1( TYPE ) { \
TYPE *p = (TYPE *) in; \
TYPE *q = (TYPE *) out; \
TYPE c = *((TYPE *) vector); \
\
for( x = 0; x < ne; x++ ) \
q[x] = p[x] % c; \
}
/* Float remainder-after-divide, single constant.
*/
#define FREMAINDERCONST1( TYPE ) { \
TYPE *p = (TYPE *) in; \
TYPE *q = (TYPE *) out; \
TYPE c = *((TYPE *) vector); \
\
for( x = 0; x < ne; x++ ) { \
double a = p[x]; \
\
if( c ) \
q[x] = a - c * floor (a / c); \
else \
q[x] = -1; \
} \
}
static void
remainderconst1_buffer( PEL *in, PEL *out, int width, PEL *vector, IMAGE *im )
{
const int ne = width * im->Bands;
int x;
switch( im->BandFmt ) {
case IM_BANDFMT_CHAR: IREMAINDERCONST1( signed char ); break;
case IM_BANDFMT_UCHAR: IREMAINDERCONST1( unsigned char ); break;
case IM_BANDFMT_SHORT: IREMAINDERCONST1( signed short ); break;
case IM_BANDFMT_USHORT: IREMAINDERCONST1( unsigned short ); break;
case IM_BANDFMT_INT: IREMAINDERCONST1( signed int ); break;
case IM_BANDFMT_UINT: IREMAINDERCONST1( unsigned int ); break;
case IM_BANDFMT_FLOAT: FREMAINDERCONST1( float ); break;
case IM_BANDFMT_DOUBLE: FREMAINDERCONST1( double ); break;
default:
g_assert( 0 );
}
}
/* Integer remainder-after-divide, per-band constant.
*/
#define IREMAINDERCONSTN( TYPE ) { \
TYPE *p = (TYPE *) in; \
TYPE *q = (TYPE *) out; \
TYPE *c = (TYPE *) vector; \
\
for( i = 0, x = 0; x < width; x++ ) \
for( k = 0; k < b; k++, i++ ) \
q[i] = p[i] % c[k]; \
}
/* Float remainder-after-divide, per-band constant.
*/
#define FREMAINDERCONSTN( TYPE ) { \
TYPE *p = (TYPE *) in; \
TYPE *q = (TYPE *) out; \
TYPE *c = (TYPE *) vector; \
\
for( i = 0, x = 0; x < width; x++ ) \
for( k = 0; k < b; k++, i++ ) { \
double a = p[i]; \
double b = c[k]; \
\
if( b ) \
q[i] = a - b * floor (a / b); \
else \
q[i] = -1; \
} \
}
static void
remainderconstn_buffer( PEL *in, PEL *out, int width, PEL *vector, IMAGE *im )
{
int b = im->Bands;
int i, x, k;
switch( im->BandFmt ) {
case IM_BANDFMT_CHAR: IREMAINDERCONSTN( signed char ); break;
case IM_BANDFMT_UCHAR: IREMAINDERCONSTN( unsigned char ); break;
case IM_BANDFMT_SHORT: IREMAINDERCONSTN( signed short ); break;
case IM_BANDFMT_USHORT: IREMAINDERCONSTN( unsigned short ); break;
case IM_BANDFMT_INT: IREMAINDERCONSTN( signed int ); break;
case IM_BANDFMT_UINT: IREMAINDERCONSTN( unsigned int ); break;
case IM_BANDFMT_FLOAT: FREMAINDERCONSTN( float ); break;
case IM_BANDFMT_DOUBLE: FREMAINDERCONSTN( double ); break;
default:
g_assert( 0 );
}
}
/**
* im_remainder_vec:
* @in: input #IMAGE
* @out: output #IMAGE
* @n: number of elements in array
* @c: array of constants
*
* This operation calculates @in % @c (remainder after division by constant)
* and writes the result to @out.
* The image may have any
* non-complex format. For float formats, im_remainder() calculates @in -
* @c * floor (@in / @c).
*
* If the number of image bands end array elements differs, one of them
* must have one band. Either the image is up-banded by joining n copies of
* the one-band image together, or the array is upbanded by copying the single
* element n times.
*
* See also: im_remainder(), im_remainderconst(), im_divide().
*
* Returns: 0 on success, -1 on error
*/
int
im_remainder_vec( IMAGE *in, IMAGE *out, int n, double *c )
{
if( im_check_noncomplex( "im_remainder", in ) )
return( -1 );
return( im__arith_binary_const( "im_remainder",
in, out, n, c, in->BandFmt,
bandfmt_remainder,
(im_wrapone_fn) remainderconst1_buffer,
(im_wrapone_fn) remainderconstn_buffer ) );
}
/**
* im_remainderconst:
* @in: input #IMAGE
* @out: output #IMAGE
* @c: constant
*
* This operation calculates @in % @c (remainder after division by constant)
* and writes the result to @out. The image must be one of the integer types.
*
* See also: im_remainder_vec(), im_divide().
*
* Returns: 0 on success, -1 on error
*/
int
im_remainderconst( IMAGE *in, IMAGE *out, double c )
{
return( im_remainder_vec( in, out, 1, &c ) );
}

View File

@ -288,7 +288,6 @@ vips_linear_class_init( VipsLinearClass *class )
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsLinear, b ),
VIPS_TYPE_ARRAY_DOUBLE );
}
static void
@ -296,10 +295,10 @@ vips_linear_init( VipsLinear *linear )
{
}
int
vips_linear( VipsImage *in, VipsImage **out, double *a, double *b, int n, ... )
static int
vips_linearv( VipsImage *in, VipsImage **out,
double *a, double *b, int n, va_list ap )
{
va_list ap;
VipsArea *area_a;
VipsArea *area_b;
double *array;
@ -316,9 +315,7 @@ vips_linear( VipsImage *in, VipsImage **out, double *a, double *b, int n, ... )
for( i = 0; i < n; i++ )
array[i] = b[i];
va_start( ap, n );
result = vips_call_split( "linear", ap, in, out, area_a, area_b );
va_end( ap );
vips_area_unref( area_a );
vips_area_unref( area_b );
@ -327,28 +324,27 @@ vips_linear( VipsImage *in, VipsImage **out, double *a, double *b, int n, ... )
}
int
vips_linear1( VipsImage *in, VipsImage **out, double a, double b, ... )
vips_linear( VipsImage *in, VipsImage **out, double *a, double *b, int n, ... )
{
va_list ap;
VipsArea *area_a;
VipsArea *area_b;
double *array;
int result;
area_a = vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), 1 );
array = (double *) area_a->data;
array[0] = a;
area_b = vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), 1 );
array = (double *) area_b->data;
array[0] = b;
va_start( ap, b );
result = vips_call_split( "linear", ap, in, out, area_a, area_b );
va_start( ap, n );
result = vips_linearv( in, out, a, b, n, ap );
va_end( ap );
return( result );
}
int
vips_linear1( VipsImage *in, VipsImage **out, double a, double b, ... )
{
va_list ap;
int result;
va_start( ap, b );
result = vips_linearv( in, out, &a, &b, 1, ap );
va_end( ap );
vips_area_unref( area_a );
vips_area_unref( area_b );
return( result );
}

View File

@ -59,6 +59,150 @@
#include <vips/vips.h>
#include <vips/internal.h>
/* Cast a vector of double to a vector of TYPE, clipping to a range.
*/
#define CAST_CLIP( TYPE, N, X ) { \
TYPE *tq = (TYPE *) q; \
\
for( i = 0; i < n; i++ ) \
tq[i] = (TYPE) IM_CLIP( N, p[i], X ); \
}
/* Cast a vector of double to a vector of TYPE.
*/
#define CAST( TYPE ) { \
TYPE *tq = (TYPE *) q; \
\
for( i = 0; i < n; i++ ) \
tq[i] = (TYPE) p[i]; \
}
/* Cast a vector of double to a complex vector of TYPE.
*/
#define CASTC( TYPE ) { \
TYPE *tq = (TYPE *) q; \
\
for( i = 0; i < n; i++ ) { \
tq[0] = (TYPE) p[i]; \
tq[1] = 0; \
tq += 2; \
} \
}
/* Cast a vector of double to a passed format.
*/
static PEL *
make_pixel( IMAGE *out, VipsBandFmt fmt, int n, double *p )
{
PEL *q;
int i;
if( !(q = IM_ARRAY( out, n * (im_bits_of_fmt( fmt ) >> 3), PEL )) )
return( NULL );
switch( fmt ) {
case IM_BANDFMT_CHAR:
CAST_CLIP( signed char, SCHAR_MIN, SCHAR_MAX );
break;
case IM_BANDFMT_UCHAR:
CAST_CLIP( unsigned char, 0, UCHAR_MAX );
break;
case IM_BANDFMT_SHORT:
CAST_CLIP( signed short, SCHAR_MIN, SCHAR_MAX );
break;
case IM_BANDFMT_USHORT:
CAST_CLIP( unsigned short, 0, USHRT_MAX );
break;
case IM_BANDFMT_INT:
CAST_CLIP( signed int, INT_MIN, INT_MAX );
break;
case IM_BANDFMT_UINT:
CAST_CLIP( unsigned int, 0, UINT_MAX );
break;
case IM_BANDFMT_FLOAT:
CAST( float );
break;
case IM_BANDFMT_DOUBLE:
CAST( double );
break;
case IM_BANDFMT_COMPLEX:
CASTC( float );
break;
case IM_BANDFMT_DPCOMPLEX:
CASTC( double );
break;
default:
g_assert( 0 );
}
return( q );
}
int
im__arith_binary_const( const char *domain,
IMAGE *in, IMAGE *out,
int n, double *c, VipsBandFmt vfmt,
int format_table[10],
im_wrapone_fn fn1, im_wrapone_fn fnn )
{
PEL *vector;
if( im_piocheck( in, out ) ||
im_check_vector( domain, n, in ) ||
im_check_uncoded( domain, in ) )
return( -1 );
if( im_cp_desc( out, in ) )
return( -1 );
out->BandFmt = format_table[in->BandFmt];
/* Some operations need the vector in the input type (eg.
* im_equal_vec() where the output type is always uchar and is useless
* for comparisons), some need it in the output type (eg.
* im_andimage_vec() where we want to get the double to an int so we
* can do bitwise-and without having to cast for each pixel), some
* need a fixed type (eg. im_powtra_vec(), where we want to keep it as
* double).
*
* Therefore pass in the desired vector type as a param.
*/
if( !(vector = make_pixel( out, vfmt, n, c )) )
return( -1 );
/* Band-up the input image if we have a >1 vector and
* a 1-band image.
*/
if( n > 1 && out->Bands == 1 ) {
IMAGE *t;
if( !(t = im_open_local( out, domain, "p" )) ||
im__bandup( domain, in, t, n ) )
return( -1 );
in = t;
}
if( n == 1 ) {
if( im_wrapone( in, out, fn1, vector, in ) )
return( -1 );
}
else {
if( im_wrapone( in, out, fnn, vector, in ) )
return( -1 );
}
return( 0 );
}
/* Operator with a single constant.
*/
#define CONST1( IN, OUT, FUN ) { \

View File

@ -73,7 +73,8 @@
/**
* VipsRelational:
* @in: input #VipsImage
* @left: left-hand input #VipsImage
* @right: right-hand input #VipsImage
* @out: output #VipsImage
* @relational: relational operation to perform
*
@ -423,11 +424,10 @@ vips_relational_const_init( VipsRelationalConst *relational_const )
{
}
int
vips_relational_const( VipsImage *in, VipsImage **out,
VipsOperationRelational relational, double *c, int n, ... )
static int
vips_relational_constv( VipsImage *in, VipsImage **out,
VipsOperationRelational relational, double *c, int n, va_list ap )
{
va_list ap;
VipsArea *area_c;
double *array;
int result;
@ -438,35 +438,38 @@ vips_relational_const( VipsImage *in, VipsImage **out,
for( i = 0; i < n; i++ )
array[i] = c[i];
va_start( ap, n );
result = vips_call_split( "relational_const", ap,
in, out, relational, area_c );
va_end( ap );
vips_area_unref( area_c );
return( result );
}
int
vips_relational_const( VipsImage *in, VipsImage **out,
VipsOperationRelational relational, double *c, int n, ... )
{
va_list ap;
int result;
va_start( ap, n );
result = vips_relational_constv( in, out, relational, c, n, ap );
va_end( ap );
return( result );
}
int
vips_relational_const1( VipsImage *in, VipsImage **out,
VipsOperationRelational relational, double c, ... )
{
va_list ap;
VipsArea *area_c;
double *array;
int result;
area_c = vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), 1 );
array = (double *) area_c->data;
array[0] = c;
va_start( ap, c );
result = vips_call_split( "relational_const", ap,
in, out, relational, area_c );
result = vips_relational_constv( in, out, relational, &c, 1, ap );
va_end( ap );
vips_area_unref( area_c );
return( result );
}

View File

@ -0,0 +1,407 @@
/* remainder.c
*
* 2/8/99 JC
* - im_divide adapted to make im_remainder
* 8/5/02 JC
* - im_remainderconst added
* - im_remainderconst_vec added
* 27/9/04
* - updated for 1 band $op n band image -> n band image case
* 26/2/07
* - oop, broken for _vec case :-(
* 14/5/08
* - better /0 test
* 27/8/08
* - revise upcasting system
* - add gtkdoc comments
* 23/6/10
* - constant ops clip to target range
* 12/11/11
* - redone as a class
*/
/*
Copyright (C) 1991-2005 The National Gallery
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "binary.h"
#include "unaryconst.h"
/**
* VipsRemainder:
* @left: left-hand input #VipsImage
* @right: right-hand input #VipsImage
* @out: output #VipsImage
*
* This operation calculates @left % @right (remainder after integer division)
* and writes the result to @out. The images may have any
* non-complex format. For float formats, #VipsRemainder calculates @in1 -
* @in2 * floor (@in1 / @in2).
*
* If the images differ in size, the smaller image is enlarged to match the
* larger by adding zero pixels along the bottom and right.
*
* If the number of bands differs, one of the images
* must have one band. In this case, an n-band image is formed from the
* one-band image by joining n copies of the one-band image together, and then
* the two n-band images are operated upon.
*
* The two input images are cast up to the smallest common type (see table
* Smallest common format in
* <link linkend="VIPS-arithmetic">arithmetic</link>), and that format is the
* result type.
*
* See also: #VipsRemainderConst, #VipsDivide.
*/
typedef VipsBinary VipsRemainder;
typedef VipsBinaryClass VipsRemainderClass;
G_DEFINE_TYPE( VipsRemainder, vips_remainder, VIPS_TYPE_BINARY );
static int
vips_remainder_build( VipsObject *object )
{
VipsBinary *binary = (VipsBinary *) object;
if( binary->left &&
vips_check_noncomplex( "VipsRemainder", binary->left ) )
return( -1 );
if( binary->right &&
vips_check_noncomplex( "VipsRemainder", binary->right ) )
return( -1 );
if( VIPS_OBJECT_CLASS( vips_remainder_parent_class )->build( object ) )
return( -1 );
return( 0 );
}
/* Integer remainder-after-division.
*/
#define IREMAINDER( TYPE ) { \
TYPE *p1 = (TYPE *) in[0]; \
TYPE *p2 = (TYPE *) in[1]; \
TYPE *q = (TYPE *) out; \
\
for( x = 0; x < sz; x++ ) \
if( p2[x] ) \
q[x] = p1[x] % p2[x]; \
else \
q[x] = -1; \
}
/* Float remainder-after-division.
*/
#define FREMAINDER( TYPE ) { \
TYPE *p1 = (TYPE *) in[0]; \
TYPE *p2 = (TYPE *) in[1]; \
TYPE *q = (TYPE *) out; \
\
for( x = 0; x < sz; x++ ) { \
double a = p1[x]; \
double b = p2[x]; \
\
if( b ) \
q[x] = a - b * floor (a / b); \
else \
q[x] = -1; \
} \
}
static void
vips_remainder_buffer( VipsArithmetic *arithmetic,
PEL *out, PEL **in, int width )
{
VipsImage *im = arithmetic->ready[0];
const int sz = width * vips_image_get_bands( im );
int x;
switch( vips_image_get_format( im ) ) {
case VIPS_FORMAT_CHAR: IREMAINDER( signed char ); break;
case VIPS_FORMAT_UCHAR: IREMAINDER( unsigned char ); break;
case VIPS_FORMAT_SHORT: IREMAINDER( signed short ); break;
case VIPS_FORMAT_USHORT:IREMAINDER( unsigned short ); break;
case VIPS_FORMAT_INT: IREMAINDER( signed int ); break;
case VIPS_FORMAT_UINT: IREMAINDER( unsigned int ); break;
case VIPS_FORMAT_FLOAT: FREMAINDER( float ); break;
case VIPS_FORMAT_DOUBLE:FREMAINDER( double ); break;
default:
g_assert( 0 );
}
}
/* Save a bit of typing.
*/
#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
/* Type promotion for remainder. Keep in sync with remainder_buffer() above.
*/
static int vips_bandfmt_remainder[10] = {
/* UC C US S UI I F X D DX */
UC, C, US, S, UI, I, F, X, D, DX
};
static void
vips_remainder_class_init( VipsRemainderClass *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 = "remainder";
object_class->description =
_( "remainder after integer division for a pair of images" );
object_class->build = vips_remainder_build;
vips_arithmetic_set_format_table( aclass, vips_bandfmt_remainder );
aclass->process_line = vips_remainder_buffer;
}
static void
vips_remainder_init( VipsRemainder *remainder )
{
}
int
vips_remainder( VipsImage *left, VipsImage *right, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "remainder", ap, left, right, out );
va_end( ap );
return( result );
}
/**
* VipsRemainderConst:
* @in: input image
* @out: output image
* @a: array of constants
*
* This operation calculates @in % @c (remainder after division by constant)
* and writes the result to @out.
* The image may have any
* non-complex format. For float formats, im_remainder() calculates @in -
* @c * floor (@in / @c).
*
* If the array of constants has just one element, that constant is used for
* all image bands. If the array has more than one element and they have
* the same number of elements as there are bands in the image, then
* one array element is used for each band. If the arrays have more than one
* element and the image only has a single band, the result is a many-band
* image where each band corresponds to one array element.
*
* See also: #VipsRemainder, #VipsDivide.
*/
typedef VipsUnaryConst VipsRemainderConst;
typedef VipsUnaryConstClass VipsRemainderConstClass;
G_DEFINE_TYPE( VipsRemainderConst,
vips_remainder_const, VIPS_TYPE_UNARY_CONST );
static int
vips_remainder_const_build( VipsObject *object )
{
VipsUnary *unary = (VipsUnary *) object;
VipsUnaryConst *uconst = (VipsUnaryConst *) object;
if( unary->in &&
vips_check_noncomplex( "VipsRemainder", unary->in ) )
return( -1 );
if( unary->in )
uconst->const_format = unary->in->BandFmt;
if( VIPS_OBJECT_CLASS( vips_remainder_const_parent_class )->
build( object ) )
return( -1 );
return( 0 );
}
/* Integer remainder-after-divide, per-band constant.
*/
#define IREMAINDERCONST( TYPE ) { \
TYPE *p = (TYPE *) in[0]; \
TYPE *q = (TYPE *) out; \
TYPE *c = (TYPE *) uconst->c_ready; \
\
for( i = 0, x = 0; x < width; x++ ) \
for( b = 0; b < bands; b++, i++ ) \
q[i] = p[i] % c[b]; \
}
/* Float remainder-after-divide, per-band constant.
*/
#define FREMAINDERCONST( TYPE ) { \
TYPE *p = (TYPE *) in[0]; \
TYPE *q = (TYPE *) out; \
TYPE *c = (TYPE *) uconst->c_ready; \
\
for( i = 0, x = 0; x < width; x++ ) \
for( b = 0; b < bands; b++, i++ ) { \
double left = p[i]; \
double right = c[b]; \
\
if( right ) \
q[i] = left - right * floor( left / right ); \
else \
q[i] = -1; \
} \
}
static void
vips_remainder_const_buffer( VipsArithmetic *arithmetic,
PEL *out, PEL **in, int width )
{
VipsUnaryConst *uconst = (VipsUnaryConst *) arithmetic;
VipsImage *im = arithmetic->ready[0];
int bands = im->Bands;
int i, x, b;
switch( vips_image_get_format( im ) ) {
case VIPS_FORMAT_CHAR: IREMAINDERCONST( signed char ); break;
case VIPS_FORMAT_UCHAR: IREMAINDERCONST( unsigned char ); break;
case VIPS_FORMAT_SHORT: IREMAINDERCONST( signed short ); break;
case VIPS_FORMAT_USHORT:IREMAINDERCONST( unsigned short ); break;
case VIPS_FORMAT_INT: IREMAINDERCONST( signed int ); break;
case VIPS_FORMAT_UINT: IREMAINDERCONST( unsigned int ); break;
case VIPS_FORMAT_FLOAT: FREMAINDERCONST( float ); break;
case VIPS_FORMAT_DOUBLE:FREMAINDERCONST( double ); break;
default:
g_assert( 0 );
}
}
static void
vips_remainder_const_class_init( VipsRemainderConstClass *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 = "remainder_const";
object_class->description =
_( "remainder after integer division for an image "
"and a constant" );
object_class->build = vips_remainder_const_build;
vips_arithmetic_set_format_table( aclass, vips_bandfmt_remainder );
aclass->process_line = vips_remainder_const_buffer;
}
static void
vips_remainder_const_init( VipsRemainderConst *remainder_const )
{
}
static int
vips_remainder_constv( VipsImage *in, VipsImage **out,
double *c, int n, va_list ap )
{
VipsArea *area_c;
double *array;
int result;
int i;
area_c = vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), n );
array = (double *) area_c->data;
for( i = 0; i < n; i++ )
array[i] = c[i];
result = vips_call_split( "remainder_const", ap, in, out, area_c );
vips_area_unref( area_c );
return( result );
}
int
vips_remainder_const( VipsImage *in, VipsImage **out, double *c, int n, ... )
{
va_list ap;
int result;
va_start( ap, n );
result = vips_remainder_constv( in, out, c, n, ap );
va_end( ap );
return( result );
}
int
vips_remainder_const1( VipsImage *in, VipsImage **out, double c, ... )
{
va_list ap;
int result;
va_start( ap, c );
result = vips_remainder_constv( in, out, &c, 1, ap );
va_end( ap );
return( result );
}

View File

@ -1837,3 +1837,42 @@ im_moreeqconst( IMAGE *in, IMAGE *out, double c )
return( im_moreeq_vec( in, out, 1, &c ) );
}
int
im_remainder( IMAGE *in1, IMAGE *in2, IMAGE *out )
{
VipsImage *t;
if( vips_remainder( in1, in2, &t,
NULL ) )
return( -1 );
if( vips_image_write( t, out ) ) {
g_object_unref( t );
return( -1 );
}
g_object_unref( t );
return( 0 );
}
int
im_remainder_vec( IMAGE *in, IMAGE *out, int n, double *c )
{
VipsImage *t;
if( vips_remainder_const( in, &t, c, n,
NULL ) )
return( -1 );
if( vips_image_write( t, out ) ) {
g_object_unref( t );
return( -1 );
}
g_object_unref( t );
return( 0 );
}
int
im_remainderconst( IMAGE *in, IMAGE *out, double c )
{
return( im_remainder_vec( in, out, 1, &c ) );
}

View File

@ -149,6 +149,14 @@ int vips_relational_const( VipsImage *in, VipsImage **out,
int vips_relational_const1( VipsImage *in, VipsImage **out,
VipsOperationRelational relational, double c, ... )
__attribute__((sentinel));
int vips_remainder( VipsImage *left, VipsImage *right, VipsImage **out, ... )
__attribute__((sentinel));
int vips_remainder_const( VipsImage *in, VipsImage **out,
double *c, int n, ... )
__attribute__((sentinel));
int vips_remainder_const1( VipsImage *in, VipsImage **out,
double c, ... )
__attribute__((sentinel));
@ -158,11 +166,6 @@ int im_maxpos_vec( VipsImage *im, int *xpos, int *ypos, double *maxima, int n );
int im_minpos_vec( VipsImage *im, int *xpos, int *ypos, double *minima, int n );
int im_bandmean( VipsImage *in, VipsImage *out );
int im_remainder( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_remainder_vec( VipsImage *in, VipsImage *out, int n, double *c );
int im_remainderconst( VipsImage *in, VipsImage *out, double c );
int im_linreg( VipsImage **ins, VipsImage *out, double *xs );
int im_point( VipsImage *im, VipsInterpolate *interpolate,
double x, double y, int band, double *out );

View File

@ -564,6 +564,10 @@ int im_lesseq( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_more( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_moreeq( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_remainder( VipsImage *in1, VipsImage *in2, VipsImage *out );
int im_remainder_vec( VipsImage *in, VipsImage *out, int n, double *c );
int im_remainderconst( VipsImage *in, VipsImage *out, double c );
int im_equal_vec( VipsImage *in, VipsImage *out, int n, double *c );
int im_notequal_vec( VipsImage *in, VipsImage *out, int n, double *c );
int im_less_vec( VipsImage *in, VipsImage *out, int n, double *c );