redo im_histnD() as a class

This commit is contained in:
John Cupitt 2013-08-17 16:59:28 +01:00
parent af2ac55f1c
commit e2215bd04c
12 changed files with 381 additions and 274 deletions

View File

@ -3,8 +3,8 @@
- rename image arrays as image matrices ... INTERPRETATION_ARRAY ->
INTERPRETATION_MATRIX etc.
- rewrite im_buildlut(), im_identity*(), im_maplut(), im_falsecolour(),
im_gammacorrect(), im_histgr(), im_histcum(), im_histnorm(), im_heq()
as classes
im_gammacorrect(), im_histgr(), im_histcum(), im_histnorm(), im_heq(),
im_histnD() as classes
- added vips_error_freeze() / vips_error_thaw()
- used freeze() / thaw() to stop file format sniffers logging spurious errors
- vipsthumbnail uses embedded jpg thumbnails if it can

3
TODO
View File

@ -1,3 +1,6 @@
- im_histspec() is a binary hist operator ... need many inputs for
VipsHistogram and VipsHistBuffer
- use g_log() instead of vips_info()
- what's the difference between buildlut and invertlut? can we make invertlut

View File

@ -16,6 +16,7 @@ libarithmetic_la_SOURCES = \
min.c \
max.c \
hist_find.c \
hist_find_ndim.c \
subtract.c \
math.c \
arithmetic.c \

View File

@ -691,6 +691,7 @@ vips_arithmetic_operation_init( void )
extern GType vips_sign_get_type( void );
extern GType vips_stats_get_type( void );
extern GType vips_hist_find_get_type( void );
extern GType vips_hist_find_ndim_get_type( void );
extern GType vips_measure_get_type( void );
extern GType vips_round_get_type( void );
extern GType vips_relational_get_type( void );
@ -721,6 +722,7 @@ vips_arithmetic_operation_init( void )
vips_sign_get_type();
vips_stats_get_type();
vips_hist_find_get_type();
vips_hist_find_ndim_get_type();
vips_measure_get_type();
vips_round_get_type();
vips_relational_get_type();

View File

@ -420,6 +420,9 @@ vips_hist_find_init( VipsHistFind *hist_find )
* vips_hist_find:
* @in: input image
* @out: output image
*
* Optional arguments:
*
* @band: band to equalise
*
* Find the histogram of @in. Find the histogram for band @band (producing a

View File

@ -0,0 +1,348 @@
/* n-dimensional histogram
*
* Written on: 8/7/03
* 10/11/04
* - oops, was not checking the bandfmt coming in
* 24/3/10
* - gtkdoc
* - small celanups
* 17/8/13
* - redo as a class
*/
/*
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vips/vips.h>
#include "statistic.h"
struct _VipsHistFindNDim;
/* Accumulate a histogram in one of these.
*/
typedef struct {
struct _VipsHistFindNDim *ndim;
int bins;
int max_val;
unsigned int ***data;
} Histogram;
typedef struct _VipsHistFindNDim {
VipsStatistic parent_instance;
/* Number of bins on each axis.
*/
int bins;
/* Main image histogram. Subhists accumulate to this.
*/
Histogram *hist;
/* Write hist to this output image.
*/
VipsImage *out;
} VipsHistFindNDim;
typedef VipsStatisticClass VipsHistFindNDimClass;
G_DEFINE_TYPE( VipsHistFindNDim, vips_hist_find_ndim, VIPS_TYPE_STATISTIC );
/* Build a Histogram.
*/
static Histogram *
histogram_new( VipsHistFindNDim *ndim )
{
VipsImage *in = VIPS_STATISTIC( ndim )->ready;
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( ndim );
int bins = ndim->bins;
/* How many dimensions do we need to allocate?
*/
int ilimit = in->Bands > 2 ? bins : 1;
int jlimit = in->Bands > 1 ? bins : 1;
int i, j;
Histogram *hist;
if( !(hist = VIPS_NEW( ndim, Histogram )) )
return( NULL );
hist->ndim = ndim;
hist->bins = bins;
hist->max_val = in->BandFmt == VIPS_FORMAT_UCHAR ? 256 : 65536;
if( bins < 1 ||
bins > hist->max_val ) {
vips_error( class->nickname,
_( "bins out of range [1,%d]" ), hist->max_val );
return( NULL );
}
if( !(hist->data = VIPS_ARRAY( ndim, bins, unsigned int ** )) )
return( NULL );
memset( hist->data, 0, bins * sizeof( unsigned int ** ) );
for( i = 0; i < ilimit; i++ ) {
if( !(hist->data[i] =
VIPS_ARRAY( ndim, bins, unsigned int * )) )
return( NULL );
memset( hist->data[i], 0, bins * sizeof( unsigned int * ) );
for( j = 0; j < jlimit; j++ ) {
if( !(hist->data[i][j] =
VIPS_ARRAY( ndim, bins, unsigned int )) )
return( NULL );
memset( hist->data[i][j],
0, bins * sizeof( unsigned int ) );
}
}
return( hist );
}
static int
vips_hist_find_ndim_build( VipsObject *object )
{
VipsStatistic *statistic = VIPS_STATISTIC( object );
VipsHistFindNDim *ndim = (VipsHistFindNDim *) object;
unsigned int *obuffer;
int y, i, x, z;
g_object_set( object,
"out", vips_image_new(),
NULL );
/* main hist made on first thread start.
*/
if( VIPS_OBJECT_CLASS( vips_hist_find_ndim_parent_class )->
build( object ) )
return( -1 );
if( vips_image_copy_fields( ndim->out, statistic->ready ) )
return( -1 );
vips_image_init_fields( ndim->out,
ndim->bins,
statistic->ready->Bands > 1 ? ndim->bins : 1,
statistic->ready->Bands > 2 ? ndim->bins : 1,
VIPS_FORMAT_UINT,
VIPS_CODING_NONE, VIPS_INTERPRETATION_HISTOGRAM, 1.0, 1.0 );
if( !(obuffer = VIPS_ARRAY( ndim,
VIPS_IMAGE_N_ELEMENTS( ndim->out ), unsigned int )) )
return( -1 );
for( y = 0; y < ndim->out->Ysize; y++ ) {
for( i = 0, x = 0; x < ndim->out->Xsize; x++ )
for( z = 0; z < ndim->out->Bands; z++, i++ )
obuffer[i] = ndim->hist->data[z][y][x];
if( vips_image_write_line( ndim->out, y, (VipsPel *) obuffer ) )
return( -1 );
}
return( 0 );
}
static void *
vips_hist_find_ndim_start( VipsStatistic *statistic )
{
VipsHistFindNDim *ndim = (VipsHistFindNDim *) statistic;
/* Make the main hist, if necessary.
*/
if( !ndim->hist )
ndim->hist = histogram_new( ndim );
return( (void *) histogram_new( ndim ) );
}
/* Join a sub-hist onto the main hist.
*/
static int
vips_hist_find_ndim_stop( VipsStatistic *statistic, void *seq )
{
Histogram *sub_hist = (Histogram *) seq;
VipsHistFindNDim *ndim = (VipsHistFindNDim *) statistic;
Histogram *hist = ndim->hist;
int i, j, k;
for( i = 0; i < hist->bins; i++ )
for( j = 0; j < hist->bins; j++ )
for( k = 0; k < hist->bins; k++ )
if( hist->data[i] && hist->data[i][j] ) {
hist->data[i][j][k] +=
sub_hist->data[i][j][k];
/* Zap sub-hist to make sure we
* can't add it again.
*/
sub_hist->data[i][j][k] = 0;
}
return( 0 );
}
#define LOOP( TYPE ) { \
TYPE *p = (TYPE *) in; \
\
for( i = 0, j = 0; j < n; j++ ) { \
for( k = 0; k < nb; k++, i++ ) \
index[k] = p[i] / scale; \
\
hist->data[index[2]][index[1]][index[0]] += 1; \
} \
}
static int
vips_hist_find_ndim_scan( VipsStatistic *statistic, void *seq,
int x, int y, void *in, int n )
{
Histogram *hist = (Histogram *) seq;
VipsImage *im = statistic->ready;
int nb = im->Bands;
double scale = (double) (hist->max_val + 1) / hist->bins;
int i, j, k;
int index[3];
/* Fill these with dimensions, backwards.
*/
index[0] = index[1] = index[2] = 0;
switch( im->BandFmt ) {
case VIPS_FORMAT_UCHAR:
LOOP( unsigned char );
break;
case VIPS_FORMAT_USHORT:
LOOP( unsigned short );
break;
default:
g_assert( 0 );
}
return( 0 );
}
/* Save a bit of typing.
*/
#define UC VIPS_FORMAT_UCHAR
#define US VIPS_FORMAT_USHORT
#define UI VIPS_FORMAT_UINT
/* Type mapping: go to uchar or ushort.
*/
static const VipsBandFormat vips_hist_find_ndim_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_hist_find_ndim_class_init( VipsHistFindNDimClass *class )
{
GObjectClass *gobject_class = (GObjectClass *) class;
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsStatisticClass *sclass = VIPS_STATISTIC_CLASS( class );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "hist_find_ndim";
object_class->description = _( "find image histogram" );
object_class->build = vips_hist_find_ndim_build;
sclass->start = vips_hist_find_ndim_start;
sclass->scan = vips_hist_find_ndim_scan;
sclass->stop = vips_hist_find_ndim_stop;
sclass->format_table = vips_hist_find_ndim_format_table;
VIPS_ARG_IMAGE( class, "out", 100,
_( "Output" ),
_( "Output histogram" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsHistFindNDim, out ) );
VIPS_ARG_INT( class, "bins", 110,
_( "Bins" ),
_( "Number of bins in each dimension" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsHistFindNDim, bins ),
1, 65536, 10 );
}
static void
vips_hist_find_ndim_init( VipsHistFindNDim *ndim )
{
ndim->bins = 10;
}
/**
* vips_hist_find_ndim:
* @in: input image
* @out: output image
*
* Optional arguments:
*
* @bins: number of bins to make on each axis
*
* Make a one, two or three dimensional histogram of a 1, 2 or
* 3 band image. Divide each axis into @bins bins .. ie.
* output is 1 x bins, bins x bins, or bins x bins x bins bands.
* @bins defaults to 10.
*
* Images are cast to uchar or ushort before histogramming.
*
* See also: vips_hist_find(), vips_hist_find_indexed().
*
* Returns: 0 on success, -1 on error
*/
int
vips_hist_find_ndim( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "hist_find_ndim", ap, in, out );
va_end( ap );
return( result );
}

View File

@ -3420,6 +3420,25 @@ im_histgr( IMAGE *in, IMAGE *out, int bandno )
return( 0 );
}
int
im_histnD( VipsImage *in, VipsImage *out, int bins )
{
VipsImage *x;
if( vips_hist_find_ndim( in, &x,
"bins", bins,
NULL ) )
return( -1 );
if( im_copy( x, out ) ) {
g_object_unref( x );
return( -1 );
}
g_object_unref( x );
return( 0 );
}
int
im_falsecolour( IMAGE *in, IMAGE *out )
{

View File

@ -11,7 +11,6 @@ libhistogram_la_SOURCES = \
hist_equal.c \
\
hist_dispatch.c \
im_histnD.c \
im_histplot.c \
im_histindexed.c \
im_histspec.c \

View File

@ -1,270 +0,0 @@
/* n-dimensional histogram
*
* Written on: 8/7/03
* 10/11/04
* - oops, was not checking the bandfmt coming in
* 24/3/10
* - gtkdoc
* - small celanups
*/
/*
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vips/vips.h>
/* Accumulate a histogram in one of these.
*/
typedef struct {
IMAGE *in;
IMAGE *out;
int bins;
unsigned int ***data; /* Gather stats here */
} Histogram;
/* Build a Histogram.
*/
static Histogram *
build_hist( IMAGE *in, IMAGE *out, int bins )
{
/* How many dimensions we we need to allocate?
*/
int ilimit = in->Bands > 2 ? bins : 1;
int jlimit = in->Bands > 1 ? bins : 1;
int i, j;
Histogram *hist;
if( !(hist = IM_NEW( out, Histogram )) )
return( NULL );
hist->in = in;
hist->out = out;
hist->bins = bins;
if( !(hist->data = IM_ARRAY( out, bins, unsigned int ** )) )
return( NULL );
memset( hist->data, 0, bins * sizeof( unsigned int ** ) );
for( i = 0; i < ilimit; i++ ) {
if( !(hist->data[i] = IM_ARRAY( out, bins, unsigned int * )) )
return( NULL );
memset( hist->data[i], 0, bins * sizeof( unsigned int * ) );
for( j = 0; j < jlimit; j++ ) {
if( !(hist->data[i][j] = IM_ARRAY( out,
bins, unsigned int )) )
return( NULL );
memset( hist->data[i][j],
0, bins * sizeof( unsigned int ) );
}
}
return( hist );
}
/* Build a sub-hist, based on the main hist.
*/
static void *
build_subhist( IMAGE *out, void *a, void *b )
{
Histogram *mhist = (Histogram *) a;
return( (void *)
build_hist( mhist->in, mhist->out, mhist->bins ) );
}
/* Join a sub-hist onto the main hist.
*/
static int
merge_subhist( void *seq, void *a, void *b )
{
Histogram *shist = (Histogram *) seq;
Histogram *mhist = (Histogram *) a;
int i, j, k;
/* Sanity!
*/
if( shist->in != mhist->in || shist->out != mhist->out )
error_exit( "sanity failure in merge_subhist" );
/* Add on sub-data.
*/
for( i = 0; i < mhist->bins; i++ )
for( j = 0; j < mhist->bins; j++ )
for( k = 0; k < mhist->bins; k++ )
if( mhist->data[i] && mhist->data[i][j] ) {
mhist->data[i][j][k] +=
shist->data[i][j][k];
/* Zap sub-hist to make sure we
* can't add it again.
*/
shist->data[i][j][k] = 0;
}
return( 0 );
}
#define LOOP( TYPE ) { \
TYPE *p = (TYPE *) line;\
\
for( i = 0, x = 0; x < r->width; x++ ) { \
for( z = 0; z < nb; z++, i++ ) \
index[z] = p[i] / scale; \
\
hist->data[index[2]][index[1]][index[0]]++; \
} \
}
static int
find_hist( REGION *reg, void *seq, void *a, void *b, gboolean *stop )
{
Histogram *hist = (Histogram *) seq;
Rect *r = &reg->valid;
IMAGE *im = reg->im;
int le = r->left;
int to = r->top;
int bo = IM_RECT_BOTTOM(r);
int nb = im->Bands;
int max_val = im->BandFmt == IM_BANDFMT_UCHAR ? 256 : 65536;
int scale = max_val / hist->bins;
int x, y, z, i;
int index[3];
/* Fill these with dimensions, backwards.
*/
index[0] = index[1] = index[2] = 0;
/* Accumulate!
*/
for( y = to; y < bo; y++ ) {
VipsPel *line = IM_REGION_ADDR( reg, le, y );
switch( im->BandFmt ) {
case IM_BANDFMT_UCHAR:
LOOP( unsigned char );
break;
case IM_BANDFMT_USHORT:
LOOP( unsigned char );
break;
default:
error_exit( "panic #34847563245" );
}
}
return( 0 );
}
/**
* im_histnD:
* @in: input image
* @out: output image
* @bins: number of bins to make on each axis
*
* Make a one, two or three dimensional histogram of a 1, 2 or
* 3 band image. Divide each axis into a certain number of bins .. ie.
* output is 1 x bins, bins x bins, or bins x bins x bins bands.
* uchar and ushort only.
*
* See also: im_histgr(), im_histindexed().
*
* Returns: 0 on success, -1 on error
*/
int
im_histnD( IMAGE *in, IMAGE *out, int bins )
{
int max_val;
Histogram *mhist;
int x, y, z, i;
unsigned int *obuffer;
/* Check images. PIO from in, WIO to out.
*/
if( im_check_uncoded( "im_histnD", in ) ||
im_check_u8or16( "im_histnD", in ) ||
im_pincheck( in ) ||
im_outcheck( out ) )
return( -1 );
max_val = in->BandFmt == IM_BANDFMT_UCHAR ? 256 : 65536;
if( bins < 1 || bins > max_val ) {
im_error( "im_histnD",
_( " bins out of range [1,%d]" ), max_val );
return( -1 );
}
/* Build main hist we accumulate to.
*/
if( !(mhist = build_hist( in, out, bins )) )
return( -1 );
/* Accumulate data.
*/
if( vips_sink( in,
build_subhist, find_hist, merge_subhist, mhist, NULL ) )
return( -1 );
/* Make the output image.
*/
if( im_cp_desc( out, in ) )
return( -1 );
im_initdesc( out,
bins, in->Bands > 1 ? bins : 1, in->Bands > 2 ? bins : 1,
IM_BBITS_INT, IM_BANDFMT_UINT,
IM_CODING_NONE, IM_TYPE_HISTOGRAM, 1.0, 1.0, 0, 0 );
if( im_setupout( out ) )
return( -1 );
/* Interleave to output buffer.
*/
if( !(obuffer = IM_ARRAY( out,
IM_IMAGE_N_ELEMENTS( out ), unsigned int )) )
return( -1 );
for( y = 0; y < out->Ysize; y++ ) {
for( i = 0, x = 0; x < out->Xsize; x++ )
for( z = 0; z < out->Bands; z++, i++ )
obuffer[i] = mhist->data[z][y][x];
if( im_writeline( y, out, (VipsPel *) obuffer ) )
return( -1 );
}
return( 0 );
}

View File

@ -379,6 +379,8 @@ int vips_measure( VipsImage *in, VipsImage **out, int h, int v, ... )
__attribute__((sentinel));
int vips_hist_find( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int vips_hist_find_ndim( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
#ifdef __cplusplus
}

View File

@ -48,7 +48,6 @@ int vips_hist_equal( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int im_histnD( VipsImage *in, VipsImage *out, int bins );
int im_hist_indexed( VipsImage *index, VipsImage *value, VipsImage *out );
int im_invertlut( DOUBLEMASK *input, VipsImage *output, int lut_size );

View File

@ -856,6 +856,7 @@ int im_histcum( VipsImage *in, VipsImage *out );
int im_histnorm( VipsImage *in, VipsImage *out );
int im_histeq( VipsImage *in, VipsImage *out );
int im_heq( VipsImage *in, VipsImage *out, int bandno );
int im_histnD( VipsImage *in, VipsImage *out, int bins );
/* ruby-vips uses this
*/