im_histspec() becomes a class

This commit is contained in:
John Cupitt 2013-09-04 09:19:34 +01:00
parent f1f6ab29ae
commit f35e27135a
7 changed files with 218 additions and 216 deletions

View File

@ -4,7 +4,7 @@
INTERPRETATION_MATRIX etc.
- rewrite im_buildlut(), im_identity*(), im_maplut(), im_falsecolour(),
im_gammacorrect(), im_histgr(), im_histcum(), im_histnorm(), im_heq(),
im_histnD(), im_histindexed() as classes
im_histnD(), im_histindexed(), im_histspec() as classes
- thin vips8 wrapper for im_histplot()
- added vips_error_freeze() / vips_error_thaw()
- used freeze() / thaw() to stop file format sniffers logging spurious errors

View File

@ -10,6 +10,7 @@ libhistogram_la_SOURCES = \
hist_norm.c \
hist_equal.c \
hist_plot.c \
hist_match.c \
\
hist_dispatch.c \
im_histspec.c \

View File

@ -83,7 +83,7 @@ G_DEFINE_TYPE( VipsHistCum, vips_hist_cum, VIPS_TYPE_HIST_UNARY );
}
static void
vips_hist_cum_buffer( VipsHistogram *histogram,
vips_hist_cum_process( VipsHistogram *histogram,
VipsPel *out, VipsPel **in, int width )
{
const int bands = vips_image_get_bands( histogram->ready[0] );
@ -132,7 +132,7 @@ vips_hist_cum_buffer( VipsHistogram *histogram,
#define D VIPS_FORMAT_DOUBLE
#define DX VIPS_FORMAT_DPCOMPLEX
static const VipsBandFormat vips_bandfmt_hist_cum[10] = {
static const VipsBandFormat vips_hist_cum_format_table[10] = {
/* UC C US S UI I F X D DX */
UI, I, UI, I, UI, I, F, F, D, D
};
@ -146,8 +146,8 @@ vips_hist_cum_class_init( VipsHistCumClass *class )
object_class->nickname = "hist_cum";
object_class->description = _( "form cumulative histogram" );
hclass->format_table = vips_bandfmt_hist_cum;
hclass->process = vips_hist_cum_buffer;
hclass->format_table = vips_hist_cum_format_table;
hclass->process = vips_hist_cum_process;
}
static void

View File

@ -0,0 +1,209 @@
/* Match two normalised, cumulative histograms.
*
* Copyright: 1991, N. Dessipris.
*
* Author: Nicos Dessipris
* Written on: 19/07/1990
* Modified on: 26/03/1991
*
* 1/3/01 JC
* - bleurg! rewritten, now does 16 bits as well, bugs removed, faster,
* smaller
* 24/3/10
* - gtkdoc
* - small cleanups
* 12/8/13
* - redone im_histspec() as a class, vips_hist_match()
*/
/*
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 <assert.h>
#include <vips/vips.h>
#include "phistogram.h"
/*
#define DEBUG
*/
typedef struct _VipsHistMatch {
VipsHistogram parent_instance;
VipsImage *in;
VipsImage *ref;
} VipsHistMatch;
typedef VipsHistogramClass VipsHistMatchClass;
G_DEFINE_TYPE( VipsHistMatch, vips_hist_match, VIPS_TYPE_HISTOGRAM );
static void
vips_hist_match_process( VipsHistogram *histogram,
VipsPel *out, VipsPel **in, int width )
{
VipsHistMatch *match = (VipsHistMatch *) histogram;
const int bands = match->in->Bands;
const int max = width * bands;
unsigned int *inbuf = (unsigned int *) in[0];
unsigned int *refbuf = (unsigned int *) in[1];
unsigned int *outbuf = (unsigned int *) out;
int i, j;
for( j = 0; j < bands; j++ ) {
/* Track up refbuf[] with this.
*/
int ri = j;
int limit = max - bands;
for( i = j; i < max; i += bands ) {
unsigned int inv = inbuf[i];
for( ; ri < limit; ri += bands )
if( inv <= refbuf[ri] )
break;
if( ri < limit ) {
/* Simple rounding.
*/
double mid = refbuf[ri] +
refbuf[ri + bands] / 2.0;
if( inv < mid )
outbuf[i] = ri / bands;
else
outbuf[i] = ri / bands + 1;
}
else
outbuf[i] = refbuf[ri];
}
}
}
static int
vips_hist_match_build( VipsObject *object )
{
VipsHistogram *histogram = VIPS_HISTOGRAM( object );
VipsHistMatch *match = (VipsHistMatch *) object;
histogram->n = 2;
histogram->in = (VipsImage **) vips_object_local_array( object, 2 );
histogram->in[0] = match->in;
histogram->in[1] = match->ref;
if( histogram->in[0] )
g_object_ref( histogram->in[0] );
if( histogram->in[1] )
g_object_ref( histogram->in[1] );
if( VIPS_OBJECT_CLASS( vips_hist_match_parent_class )->build( object ) )
return( -1 );
return( 0 );
}
#define UI VIPS_FORMAT_UINT
static const VipsBandFormat vips_hist_match_format_table[10] = {
/* UC C US S UI I F X D DX */
UI, UI, UI, UI, UI, UI, UI, UI, UI, UI
};
static void
vips_hist_match_class_init( VipsHistMatchClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
VipsHistogramClass *hclass = VIPS_HISTOGRAM_CLASS( class );
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
vobject_class->nickname = "hist_match";
vobject_class->description = _( "match two histograms" );
vobject_class->build = vips_hist_match_build;
hclass->format_table = vips_hist_match_format_table;
hclass->process = vips_hist_match_process;
VIPS_ARG_IMAGE( class, "in", 1,
_( "Input" ),
_( "Input image" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsHistMatch, in ) );
VIPS_ARG_IMAGE( class, "ref", 2,
_( "Reference" ),
_( "Reference image" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsHistMatch, ref ) );
}
static void
vips_hist_match_init( VipsHistMatch *match )
{
}
/**
* vips_hist_match:
* @in: input histogram
* @ref: reference histogram
* @out: output histogram
*
* Creates a lut which, when applied to the image from which histogram @in was
* formed, will produce an image whose PDF matches that of the image from
* which @ref was formed.
*
* See also: im_hsp(), im_histgr(), im_maplut().
*
* Returns: 0 on success, -1 on error
*/
int
vips_hist_match( VipsImage *in, VipsImage *ref, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "hist_match", ap, in, ref, out );
va_end( ap );
return( result );
}

View File

@ -1,211 +0,0 @@
/* match PDFs
*
* Copyright: 1991, N. Dessipris.
*
* Author: Nicos Dessipris
* Written on: 19/07/1990
* Modified on: 26/03/1991
*
* 1/3/01 JC
* - bleurg! rewritten, now does 16 bits as well, bugs removed, faster,
* smaller
* 24/3/10
* - gtkdoc
* - small cleanups
*/
/*
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 <assert.h>
#include <vips/vips.h>
/*
#define DEBUG
*/
/*
#define PIM_RINT
*/
/* Match two normalised cumulative histograms.
*/
static int
match( IMAGE *in, IMAGE *ref, IMAGE *out )
{
const guint64 inpx = VIPS_IMAGE_N_PELS( in );
const guint64 refpx = VIPS_IMAGE_N_PELS( ref );
const int bands = in->Bands;
unsigned int *inbuf; /* in and ref, padded to same size */
unsigned int *refbuf;
unsigned int *outbuf; /* Always output as uint */
guint64 px; /* Number of pixels */
guint64 max; /* px * bands */
guint64 i, j;
if( im_iocheck( in, out ) ||
im_iocheck( ref, out ) ||
im_check_uncoded( "im_histspec", in ) ||
im_check_format( "im_histspec", in, IM_BANDFMT_UINT ) ||
im_check_coding_same( "im_histspec", in, ref ) ||
im_check_format_same( "im_histspec", in, ref ) ||
im_check_bands_same( "im_histspec", in, ref ) ||
im_check_hist( "im_histspec", in ) ||
im_check_hist( "im_histspec", ref ) )
return( -1 );
/* How big?
*/
if( inpx <= 256 && refpx <= 256 )
px = 256;
else if( inpx <= 65536 && refpx <= 65536 )
px = 65536;
else
px = IM_MAX( inpx, refpx );
max = px * bands;
/* Unpack to equal-sized buffers.
*/
inbuf = IM_ARRAY( out, max, unsigned int );
refbuf = IM_ARRAY( out, max, unsigned int );
outbuf = IM_ARRAY( out, max, unsigned int );
if( !inbuf || !refbuf || !outbuf )
return( -1 );
for( i = 0; i < inpx * bands; i++ )
inbuf[i] = ((unsigned int *)in->data)[i];
for( ; i < max; i++ )
inbuf[i] = 0;
for( i = 0; i < refpx * bands; i++ )
refbuf[i] = ((unsigned int *)ref->data)[i];
for( ; i < max; i++ )
refbuf[i] = 0;
for( j = 0; j < bands; j++ ) {
/* Track up refbuf[] with this.
*/
int ri = j;
int limit = max - bands;
for( i = j; i < max; i += bands ) {
unsigned int inv = inbuf[i];
for( ; ri < limit; ri += bands )
if( inv <= refbuf[ri] )
break;
if( ri < limit ) {
/* Simple rounding.
*/
double mid = refbuf[ri] +
refbuf[ri + bands] / 2.0;
if( inv < mid )
outbuf[i] = ri/bands;
else
outbuf[i] = ri/bands + 1;
}
else
outbuf[i] = refbuf[ri];
}
}
if( im_cp_descv( out, in, ref, NULL ) )
return( -1 );
out->Xsize = px;
out->Ysize = 1;
out->Type = IM_TYPE_HISTOGRAM;
if( im_setupout( out ) )
return( -1 );
if( im_writeline( 0, out, (VipsPel *) outbuf ) )
return( -1 );
return( 0 );
}
/**
* im_histspec:
* @in: input histogram
* @ref: reference histogram
* @out: output histogram
*
* Creates a lut which, when applied to the image from which histogram @in was
* formed, will produce an image whose PDF matches that of the image from
* which @ref was formed.
*
* See also: im_hsp(), im_histgr(), im_maplut().
*
* Returns: 0 on success, -1 on error
*/
int
im_histspec( IMAGE *in, IMAGE *ref, IMAGE *out )
{
IMAGE *t[5];
guint64 px;
int fmt;
if( im_check_uint( "im_histspec", in ) ||
im_check_uint( "im_histspec", ref ) )
return( -1 );
/* Match hists.
*/
if( im_open_local_array( out, t, 5, "im_histspec", "p" ) ||
im_histeq( in, t[0] ) ||
im_clip2fmt( t[0], t[1], IM_BANDFMT_UINT ) ||
im_histeq( ref, t[2] ) ||
im_clip2fmt( t[2], t[3], IM_BANDFMT_UINT ) ||
match( t[1], t[3], t[4] ) )
return( -1 );
/* Clip type down.
*/
px = VIPS_IMAGE_N_PELS( t[4] );
if( px <= 256 )
fmt = IM_BANDFMT_UCHAR;
else if( px <= 65536 )
fmt = IM_BANDFMT_USHORT;
else
fmt = IM_BANDFMT_UINT;
if( im_clip2fmt( t[4], out, fmt ) )
return( -1 );
return( 0 );
}

View File

@ -169,6 +169,7 @@ vips_histogram_build( VipsObject *object )
if( hclass->format_table )
histogram->out->BandFmt =
hclass->format_table[histogram->ready[0]->BandFmt];
histogram->out->Type = VIPS_INTERPRETATION_HISTOGRAM;
if( !(outbuf = vips_malloc( object,
VIPS_IMAGE_SIZEOF_LINE( histogram->out ))) )

View File

@ -48,6 +48,8 @@ int vips_hist_equal( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int vips_hist_plot( VipsImage *in, VipsImage **out, ... )
__attribute__((sentinel));
int vips_hist_match( VipsImage *in, VipsImage *ref, VipsImage **out, ... )
__attribute__((sentinel));
int im_invertlut( DOUBLEMASK *input, VipsImage *output, int lut_size );