add vips_sum()

sum an array of images
This commit is contained in:
John Cupitt 2014-03-18 19:39:47 +00:00
parent 58239a4583
commit 4c31c77d80
8 changed files with 814 additions and 0 deletions

View File

@ -13,6 +13,7 @@
- removed embedded thumbnail reader from vipsthumbnail: embedded thumbnails
are too unlike the main image
- fix to vipsthumbnail --crop, thanks Alessandro
- add vips_sum()
6/3/14 started 7.38.6
- grey ramp minimum was wrong

View File

@ -1,6 +1,7 @@
noinst_LTLIBRARIES = libarithmetic.la
libarithmetic_la_SOURCES = \
sum.c \
abs.c \
complex.c \
deviate.c \
@ -29,6 +30,8 @@ libarithmetic_la_SOURCES = \
binary.h \
unary.c \
unary.h \
nary.c \
nary.h \
unaryconst.c \
unaryconst.h \
relational.c \

View File

@ -690,6 +690,7 @@ void
vips_arithmetic_operation_init( void )
{
extern GType vips_add_get_type( void );
extern GType vips_sum_get_type( void );
extern GType vips_subtract_get_type( void );
extern GType vips_multiply_get_type( void );
extern GType vips_divide_get_type( void );
@ -725,6 +726,7 @@ vips_arithmetic_operation_init( void )
extern GType vips_complexform_get_type( void );
vips_add_get_type();
vips_sum_get_type();
vips_subtract_get_type();
vips_multiply_get_type();
vips_divide_get_type();

364
libvips/arithmetic/hough.c Normal file
View File

@ -0,0 +1,364 @@
/* hough transform
*
* 7/3/14
* - from hough.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
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <string.h>
#include <vips/vips.h>
#include "statistic.h"
typedef struct _VipsHough {
VipsStatistic parent_instance;
/* Lock writes to the output image with this. We can't have a separate
* output for each thread, memory use would be crazy.
*/
GMutex *lock;
/* Accumulate the transform in this large memory image.
*/
VipsImage *out;
} VipsHough;
typedef VipsStatisticClass VipsHoughClass;
G_DEFINE_TYPE( VipsHough, vips_hough, VIPS_TYPE_STATISTIC );
static void
vips_hough_dispose( GObject *gobject )
{
VipsHough *hough = (VipsHough *) gobject;
VIPS_FREEF( vips_g_mutex_free, hough->lock );
G_OBJECT_CLASS( vips_hough_parent_class )->dispose( gobject );
}
static int
vips_hough_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsStatistic *statistic = VIPS_STATISTIC( object );
VipsHough *hough = (VipsHough *) object;
unsigned int *obuffer;
unsigned int *q;
int i, j;
g_object_set( object,
"out", vips_image_new_buffer(),
NULL );
if( VIPS_OBJECT_CLASS( vips_hough_parent_class )->build( object ) )
return( -1 );
/* Make the output image.
*/
if( vips_image_pipelinev( hough->out,
VIPS_DEMAND_STYLE_ANY, statistic->ready, NULL ) )
return( -1 );
vips_image_init_fields( hough->out,
hough->hist->mx + 1, 1, hough->hist->bands,
VIPS_FORMAT_UINT,
VIPS_CODING_NONE, VIPS_INTERPRETATION_HISTOGRAM, 1.0, 1.0 );
if( vips_image_write_prepare( hough->out ) )
return( -1 );
return( 0 );
}
/* Build a sub-hist, based on the main hist.
*/
static void *
vips_hough_start( VipsStatistic *statistic )
{
VipsHough *hough = (VipsHough *) statistic;
/* Make the main hist, if necessary.
*/
if( !hough->hist )
hough->hist = histogram_new( hough,
hough->which == -1 ?
statistic->ready->Bands : 1,
hough->which,
statistic->ready->BandFmt == VIPS_FORMAT_UCHAR ?
256 : 65536 );
return( (void *) histogram_new( hough,
hough->hist->bands,
hough->hist->which,
hough->hist->size ) );
}
/* Join a sub-hist onto the main hist.
*/
static int
vips_hough_stop( VipsStatistic *statistic, void *seq )
{
Histogram *sub_hist = (Histogram *) seq;
VipsHough *hough = (VipsHough *) statistic;
Histogram *hist = hough->hist;
int i, j;
g_assert( sub_hist->bands == hist->bands &&
sub_hist->size == hist->size );
/* Add on sub-data.
*/
hist->mx = VIPS_MAX( hist->mx, sub_hist->mx );
for( i = 0; i < hist->bands; i++ )
for( j = 0; j < hist->size; j++ )
hist->bins[i][j] += sub_hist->bins[i][j];
/* Blank out sub-hist to make sure we can't add it again.
*/
sub_hist->mx = 0;
for( i = 0; i < sub_hist->bands; i++ )
sub_hist->bins[i] = NULL;
return( 0 );
}
/* Hist of all bands of uchar.
*/
static int
vips_hough_uchar_scan( VipsStatistic *statistic,
void *seq, int x, int y, void *in, int n )
{
Histogram *hist = (Histogram *) seq;
int nb = statistic->ready->Bands;
VipsPel *p = (VipsPel *) in;
int i, j, z;
/* Tried swapping these loops, no meaningful speedup.
*/
for( i = 0, j = 0; j < n; j++ )
for( z = 0; z < nb; z++, i++ )
hist->bins[z][p[i]] += 1;
/* Note the maximum.
*/
hist->mx = 255;
return( 0 );
}
/* Histogram of a selected band of a uchar image.
*/
static int
vips_hough_uchar_extract_scan( VipsStatistic *statistic,
void *seq, int x, int y, void *in, int n )
{
Histogram *hist = (Histogram *) seq;
int nb = statistic->ready->Bands;
int max = n * nb;
unsigned int *bins = hist->bins[0];
VipsPel *p = (VipsPel *) in;
int i;
for( i = hist->which; i < max; i += nb )
bins[p[i]] += 1;
/* Note the maximum.
*/
hist->mx = 255;
return( 0 );
}
/* Histogram of all bands of a ushort image.
*/
static int
vips_hough_ushort_scan( VipsStatistic *statistic,
void *seq, int x, int y, void *in, int n )
{
Histogram *hist = (Histogram *) seq;
int mx = hist->mx;
int nb = statistic->ready->Bands;
unsigned short *p = (unsigned short *) in;
int i, j, z;
for( i = 0, j = 0; j < n; j++ )
for( z = 0; z < nb; z++, i++ ) {
int v = p[i];
/* Adjust maximum.
*/
if( v > mx )
mx = v;
hist->bins[z][v] += 1;
}
/* Note the maximum.
*/
hist->mx = mx;
return( 0 );
}
/* Histogram of one band of a ushort image.
*/
static int
vips_hough_ushort_extract_scan( VipsStatistic *statistic,
void *seq, int x, int y, void *in, int n )
{
Histogram *hist = (Histogram *) seq;
int mx = hist->mx;
unsigned int *bins = hist->bins[0];
unsigned short *p = (unsigned short *) in;
int nb = statistic->ready->Bands;
int max = nb * n;
int i;
for( i = hist->which; i < max; i += nb ) {
int v = p[i];
/* Adjust maximum.
*/
if( v > mx )
mx = v;
bins[v] += 1;
}
/* Note the maximum.
*/
hist->mx = mx;
return( 0 );
}
static int
vips_hough_scan( VipsStatistic *statistic, void *seq,
int x, int y, void *in, int n )
{
VipsHough *hough = (VipsHough *) statistic;
VipsStatisticScanFn scan;
if( hough->which < 0 ) {
if( statistic->in->BandFmt == VIPS_FORMAT_UCHAR )
scan = vips_hough_uchar_scan;
else
scan = vips_hough_ushort_scan;
}
else {
if( statistic->in->BandFmt == VIPS_FORMAT_UCHAR )
scan = vips_hough_uchar_extract_scan;
else
scan = vips_hough_ushort_extract_scan;
}
return( scan( statistic, seq, x, y, in, n ) );
}
/* Save a bit of typing.
*/
#define UC VIPS_FORMAT_UCHAR
#define US VIPS_FORMAT_USHORT
/* Type mapping: go to uchar or ushort.
*/
static const VipsBandFormat vips_histgr_format_table[10] = {
/* UC C US S UI I F X D DX */
UC, UC, US, US, US, US, US, US, US, US
};
static void
vips_hough_class_init( VipsHoughClass *class )
{
GObjectClass *gobject_class = (GObjectClass *) class;
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsStatisticClass *sclass = VIPS_STATISTIC_CLASS( class );
gobject_class->dispose = vips_hough_dispose;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "hough";
object_class->description = _( "find image histogram" );
object_class->build = vips_hough_build;
sclass->start = vips_hough_start;
sclass->scan = vips_hough_scan;
sclass->stop = vips_hough_stop;
sclass->format_table = vips_histgr_format_table;
VIPS_ARG_IMAGE( class, "out", 100,
_( "Output" ),
_( "Output histogram" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsHough, out ) );
}
static void
vips_hough_init( VipsHough *hough )
{
hough->lock = vips_g_mutex_new();
}
/**
* vips_hough:
* @in: input image
* @out: output image
* @...: %NULL-terminated list of optional named arguments
*
* See also:
*
* Returns: 0 on success, -1 on error
*/
int
vips_hough( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "hough", ap, in, out );
va_end( ap );
return( result );
}

100
libvips/arithmetic/nary.c Normal file
View File

@ -0,0 +1,100 @@
/* base class for all nary operations
*
* 18/3/14
* - from binary.c
*/
/*
Copyright (C) 1991-2005 The National Gallery
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.
This library 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 library; 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 "nary.h"
G_DEFINE_ABSTRACT_TYPE( VipsNary, vips_nary, VIPS_TYPE_ARITHMETIC );
static int
vips_nary_build( VipsObject *object )
{
VipsArithmetic *arithmetic = VIPS_ARITHMETIC( object );
VipsNary *nary = VIPS_NARY( object );
if( nary->in ) {
arithmetic->in = nary->in->data;
arithmetic->n = nary->in->n;
}
if( VIPS_OBJECT_CLASS( vips_nary_parent_class )->build( object ) )
return( -1 );
return( 0 );
}
static void
vips_nary_class_init( VipsNaryClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "nary";
vobject_class->description = _( "nary operations" );
vobject_class->build = vips_nary_build;
/* Create properties.
*/
VIPS_ARG_BOXED( class, "in", 0,
_( "Input" ),
_( "Array of input images" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsNary, in ),
VIPS_TYPE_ARRAY_IMAGE );
}
static void
vips_nary_init( VipsNary *nary )
{
/* Init our instance fields.
*/
}

73
libvips/arithmetic/nary.h Normal file
View File

@ -0,0 +1,73 @@
/* base class for all nary arithmetic operations
*/
/*
Copyright (C) 1991-2005 The National Gallery
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.
This library 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 library; 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
*/
#ifndef VIPS_NARY_H
#define VIPS_NARY_H
#include <vips/vips.h>
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus*/
#include "parithmetic.h"
#define VIPS_TYPE_NARY (vips_nary_get_type())
#define VIPS_NARY( obj ) \
(G_TYPE_CHECK_INSTANCE_CAST( (obj), VIPS_TYPE_NARY, VipsNary ))
#define VIPS_NARY_CLASS( klass ) \
(G_TYPE_CHECK_CLASS_CAST( (klass), VIPS_TYPE_NARY, VipsNaryClass))
#define VIPS_IS_NARY( obj ) \
(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_NARY ))
#define VIPS_IS_NARY_CLASS( klass ) \
(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_NARY ))
#define VIPS_NARY_GET_CLASS( obj ) \
(G_TYPE_INSTANCE_GET_CLASS( (obj), VIPS_TYPE_NARY, VipsNaryClass ))
typedef struct _VipsNary {
VipsArithmetic parent_instance;
/* The input images.
*/
VipsArea *in;
} VipsNary;
typedef VipsArithmeticClass VipsNaryClass;
GType vips_nary_get_type( void );
#ifdef __cplusplus
}
#endif /*__cplusplus*/
#endif /*VIPS_NARY_H*/

269
libvips/arithmetic/sum.c Normal file
View File

@ -0,0 +1,269 @@
/* sum an array of images
*
* 18/3/14
* - from add.c
*/
/*
Copyright (C) 1991-2005 The National Gallery
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.
This library 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 library; 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 "nary.h"
typedef VipsNary VipsSum;
typedef VipsNaryClass VipsSumClass;
G_DEFINE_TYPE( VipsSum, vips_sum, VIPS_TYPE_NARY );
#define LOOP( IN, OUT ) { \
IN ** restrict p = (IN **) in; \
OUT * restrict q = (OUT *) out; \
\
for( x = 0; x < sz; x++ ) { \
OUT sum; \
\
sum = p[0][x]; \
for( i = 1; i < n; i++ ) \
sum += p[i][x]; \
q[x] = sum; \
} \
}
static void
sum_buffer( VipsArithmetic *arithmetic, VipsPel *out, VipsPel **in, int width )
{
VipsImage *im = arithmetic->ready[0];
int n = arithmetic->n;
/* Complex just doubles the size.
*/
const int sz = width * vips_image_get_bands( im ) *
(vips_band_format_iscomplex( vips_image_get_format( im ) ) ?
2 : 1);
int x;
int i;
/* Sum all input types. Keep types here in sync with
* vips_sum_format_table[] below.
*/
switch( vips_image_get_format( im ) ) {
case VIPS_FORMAT_UCHAR:
LOOP( unsigned char, unsigned int ); break;
case VIPS_FORMAT_CHAR:
LOOP( signed char, signed int ); break;
case VIPS_FORMAT_USHORT:
LOOP( unsigned short, unsigned int ); break;
case VIPS_FORMAT_SHORT:
LOOP( signed short, signed int ); break;
case VIPS_FORMAT_UINT:
LOOP( unsigned int, unsigned int ); break;
case VIPS_FORMAT_INT:
LOOP( signed int, signed int ); break;
case VIPS_FORMAT_FLOAT:
case VIPS_FORMAT_COMPLEX:
LOOP( float, float ); break;
case VIPS_FORMAT_DOUBLE:
case VIPS_FORMAT_DPCOMPLEX:
LOOP( double, double ); break;
default:
g_assert( 0 );
}
}
#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 addition. Sign and value preserving. Make sure these
* match the case statement in sum_buffer() above.
*/
static const VipsBandFormat vips_sum_format_table[10] = {
/* UC C US S UI I F X D DX */
UI, I, UI, I, UI, I, F, X, D, DX
};
static void
vips_sum_class_init( VipsSumClass *class )
{
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsArithmeticClass *aclass = VIPS_ARITHMETIC_CLASS( class );
object_class->nickname = "sum";
object_class->description = _( "sum an array of images" );
aclass->process_line = sum_buffer;
vips_arithmetic_set_format_table( aclass, vips_sum_format_table );
}
static void
vips_sum_init( VipsSum *sum )
{
}
static int
vips_sumv( VipsImage **in, VipsImage **out, int n, va_list ap )
{
VipsArea *area;
VipsImage **array;
int i;
int result;
area = vips_area_new_array_object( n );
array = (VipsImage **) area->data;
for( i = 0; i < n; i++ ) {
array[i] = in[i];
g_object_ref( array[i] );
}
result = vips_call_split( "sum", ap, area, out );
vips_area_unref( area );
return( result );
}
/**
* vips_sum:
* @in: array of input images
* @out: output image
* @n: number of input images
* @...: %NULL-terminated list of optional named arguments
*
* This operation sums @in1 + @in2 and writes the result to @out.
*
* If the images differ in size, the smaller images are enlarged to match the
* largest by adding zero pixels along the bottom and right.
*
* If the number of bands differs, all but one of the images
* must have one band. In this case, n-band images are formed from the
* one-band images by joining n copies of the one-band images together, and then
* the n-band images are operated upon.
*
* The input images are cast up to the smallest common format (see table
* Smallest common format in
* <link linkend="VIPS-arithmetic">arithmetic</link>), then the
* following table is used to determine the output type:
*
* <table>
* <title>VipsSum type promotion</title>
* <tgroup cols='2' align='left' colsep='1' rowsep='1'>
* <thead>
* <row>
* <entry>input type</entry>
* <entry>output type</entry>
* </row>
* </thead>
* <tbody>
* <row>
* <entry>uchar</entry>
* <entry>uint</entry>
* </row>
* <row>
* <entry>char</entry>
* <entry>int</entry>
* </row>
* <row>
* <entry>ushort</entry>
* <entry>uint</entry>
* </row>
* <row>
* <entry>short</entry>
* <entry>int</entry>
* </row>
* <row>
* <entry>uint</entry>
* <entry>uint</entry>
* </row>
* <row>
* <entry>int</entry>
* <entry>int</entry>
* </row>
* <row>
* <entry>float</entry>
* <entry>float</entry>
* </row>
* <row>
* <entry>double</entry>
* <entry>double</entry>
* </row>
* <row>
* <entry>complex</entry>
* <entry>complex</entry>
* </row>
* <row>
* <entry>double complex</entry>
* <entry>double complex</entry>
* </row>
* </tbody>
* </tgroup>
* </table>
*
* In other words, the output type is just large enough to hold the whole
* range of possible values.
*
* See also: vips_add().
*
* Returns: 0 on success, -1 on error
*/
int
vips_sum( VipsImage **in, VipsImage **out, int n, ... )
{
va_list ap;
int result;
va_start( ap, n );
result = vips_sumv( in, out, n, ap );
va_end( ap );
return( result );
}

View File

@ -174,6 +174,8 @@ typedef enum {
int vips_add( VipsImage *left, VipsImage *right, VipsImage **out, ... )
__attribute__((sentinel));
int vips_sum( VipsImage **in, VipsImage **out, int n, ... )
__attribute__((sentinel));
int vips_subtract( VipsImage *in1, VipsImage *in2, VipsImage **out, ... )
__attribute__((sentinel));
int vips_multiply( VipsImage *left, VipsImage *right, VipsImage **out, ... )