redo im_histgr() as a class
This commit is contained in:
parent
924d4a6d3b
commit
ae6d917811
@ -3,7 +3,7 @@
|
||||
- rename image arrays as image matrices ... INTERPRETATION_ARRAY ->
|
||||
INTERPRETATION_MATRIX etc.
|
||||
- rewrite im_buildlut(), im_identity*(), im_maplut(), im_falsecolour(),
|
||||
im_gammacorrect() as classes
|
||||
im_gammacorrect(), im_histgr() 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
|
||||
|
@ -9,12 +9,13 @@ libarithmetic_la_SOURCES = \
|
||||
multiply.c \
|
||||
remainder.c \
|
||||
sign.c \
|
||||
stats.c \
|
||||
statistic.c \
|
||||
statistic.h \
|
||||
stats.c \
|
||||
avg.c \
|
||||
min.c \
|
||||
max.c \
|
||||
hist_find.c \
|
||||
subtract.c \
|
||||
math.c \
|
||||
arithmetic.c \
|
||||
|
@ -690,6 +690,7 @@ vips_arithmetic_operation_init( void )
|
||||
extern GType vips_abs_get_type( 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_measure_get_type( void );
|
||||
extern GType vips_round_get_type( void );
|
||||
extern GType vips_relational_get_type( void );
|
||||
@ -719,6 +720,7 @@ vips_arithmetic_operation_init( void )
|
||||
vips_abs_get_type();
|
||||
vips_sign_get_type();
|
||||
vips_stats_get_type();
|
||||
vips_hist_find_get_type();
|
||||
vips_measure_get_type();
|
||||
vips_round_get_type();
|
||||
vips_relational_get_type();
|
||||
|
@ -22,6 +22,8 @@
|
||||
* - small cleanups
|
||||
* 25/1/12
|
||||
* - cast @in to u8/u16.
|
||||
* 12/8/13
|
||||
* - redo as a class
|
||||
*/
|
||||
|
||||
/*
|
||||
@ -56,8 +58,12 @@
|
||||
#endif /*HAVE_CONFIG_H*/
|
||||
#include <vips/intl.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <vips/vips.h>
|
||||
|
||||
#include "statistic.h"
|
||||
|
||||
/* Accumulate a histogram in one of these.
|
||||
*/
|
||||
typedef struct {
|
||||
@ -119,7 +125,7 @@ vips_hist_find_build( VipsObject *object )
|
||||
{
|
||||
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
|
||||
VipsStatistic *statistic = VIPS_STATISTIC( object );
|
||||
VipsStats *hist_find = (VipsStats *) object;
|
||||
VipsHistFind *hist_find = (VipsHistFind *) object;
|
||||
|
||||
unsigned int *obuffer;
|
||||
unsigned int *q;
|
||||
@ -129,6 +135,11 @@ vips_hist_find_build( VipsObject *object )
|
||||
"out", vips_image_new(),
|
||||
NULL );
|
||||
|
||||
if( statistic->in &&
|
||||
vips_check_bandno( class->nickname,
|
||||
statistic->in, hist_find->band ) )
|
||||
return( -1 );
|
||||
|
||||
/* main hist made on first thread start.
|
||||
*/
|
||||
|
||||
@ -142,7 +153,7 @@ vips_hist_find_build( VipsObject *object )
|
||||
vips_image_init_fields( hist_find->out,
|
||||
hist_find->hist->mx + 1, 1, hist_find->hist->bands,
|
||||
VIPS_FORMAT_UINT,
|
||||
VIPS_CODING_NONE, VIPS_TYPE_HISTOGRAM, 1.0, 1.0 );
|
||||
VIPS_CODING_NONE, VIPS_INTERPRETATION_HISTOGRAM, 1.0, 1.0 );
|
||||
|
||||
/* Interleave for output.
|
||||
*/
|
||||
@ -151,7 +162,7 @@ vips_hist_find_build( VipsObject *object )
|
||||
return( -1 );
|
||||
for( q = obuffer, j = 0; j < hist_find->out->Xsize; j++ )
|
||||
for( i = 0; i < hist_find->out->Bands; i++ )
|
||||
*q++ = mhist->bins[i][j];
|
||||
*q++ = hist_find->hist->bins[i][j];
|
||||
|
||||
if( vips_image_write_line( hist_find->out, 0, (VipsPel *) obuffer ) )
|
||||
return( -1 );
|
||||
@ -170,9 +181,9 @@ vips_hist_find_start( VipsStatistic *statistic )
|
||||
*/
|
||||
if( !hist_find->hist )
|
||||
hist_find->hist = histogram_new( hist_find,
|
||||
hist_find->bandno == -1 ?
|
||||
hist_find->band == -1 ?
|
||||
statistic->ready->Bands : 1,
|
||||
hist_find->bandno,
|
||||
hist_find->band,
|
||||
statistic->ready->BandFmt == VIPS_FORMAT_UCHAR ?
|
||||
256 : 65536 );
|
||||
|
||||
@ -218,19 +229,18 @@ static int
|
||||
vips_hist_find_uchar_scan( VipsStatistic *statistic,
|
||||
void *seq, int x, int y, void *in, int n )
|
||||
{
|
||||
VipsHistFind *hist_find = (VipsHistFind *) statistic;
|
||||
Histogram *hist = (Histogram *) seq;
|
||||
int nb = hist_find->in->Bands;
|
||||
int nb = statistic->ready->Bands;
|
||||
VipsPel *p = (VipsPel *) in;
|
||||
|
||||
int x, z, i;
|
||||
int i, j, z;
|
||||
|
||||
/* FIXME
|
||||
*
|
||||
* Try swapping these loops, we could remove an indexing operation.
|
||||
*/
|
||||
|
||||
for( i = 0, x = 0; x < n; x++ )
|
||||
for( i = 0, j = 0; j < n; j++ )
|
||||
for( z = 0; z < nb; z++, i++ )
|
||||
hist->bins[z][p[i]] += 1;
|
||||
|
||||
@ -247,17 +257,16 @@ static int
|
||||
vips_hist_find_uchar_extract_scan( VipsStatistic *statistic,
|
||||
void *seq, int x, int y, void *in, int n )
|
||||
{
|
||||
VipsHistFind *hist_find = (VipsHistFind *) statistic;
|
||||
Histogram *hist = (Histogram *) seq;
|
||||
int nb = hist_find->in->Bands;
|
||||
int nb = statistic->ready->Bands;
|
||||
int max = n * nb;
|
||||
unsigned int *bins = hist->bins[0];
|
||||
VipsPel *p = (VipsPel *) in;
|
||||
|
||||
int x;
|
||||
int i;
|
||||
|
||||
for( x = hist->which; x < max; x += nb )
|
||||
bins[p[x]] += 1;
|
||||
for( i = hist->which; i < max; i += nb )
|
||||
bins[p[i]] += 1;
|
||||
|
||||
/* Note the maximum.
|
||||
*/
|
||||
@ -273,16 +282,13 @@ vips_hist_find_ushort_scan( VipsStatistic *statistic,
|
||||
void *seq, int x, int y, void *in, int n )
|
||||
{
|
||||
Histogram *hist = (Histogram *) seq;
|
||||
VipsRect *r = ®->valid;
|
||||
VipsImage *im = reg->im;
|
||||
int mx = hist->mx;
|
||||
int nb = im->Bands;
|
||||
int nb = statistic->ready->Bands;
|
||||
unsigned short *p = (unsigned short *) in;
|
||||
|
||||
int x, y, z;
|
||||
int i;
|
||||
int i, j, z;
|
||||
|
||||
for( i = 0, x = 0; x < n; x++ )
|
||||
for( i = 0, j = 0; j < n; j++ )
|
||||
for( z = 0; z < nb; z++, i++ ) {
|
||||
int v = p[i];
|
||||
|
||||
@ -308,18 +314,16 @@ vips_hist_find_ushort_extract_scan( VipsStatistic *statistic,
|
||||
void *seq, int x, int y, void *in, int n )
|
||||
{
|
||||
Histogram *hist = (Histogram *) seq;
|
||||
VipsRect *r = ®->valid;
|
||||
VipsImage *im = reg->im;
|
||||
int mx = hist->mx;
|
||||
unsigned int *bins = hist->bins[0];
|
||||
unsigned short *p = (unsigned short *) in;
|
||||
int nb = im->Bands;
|
||||
int nb = statistic->ready->Bands;
|
||||
int max = nb * n;
|
||||
|
||||
int x;
|
||||
int i;
|
||||
|
||||
for( x = hist->which; x < max; x += nb ) {
|
||||
int v = p[x];
|
||||
for( i = hist->which; i < max; i += nb ) {
|
||||
int v = p[i];
|
||||
|
||||
/* Adjust maximum.
|
||||
*/
|
||||
@ -373,7 +377,7 @@ static const VipsBandFormat bandfmt_histgr[10] = {
|
||||
};
|
||||
|
||||
static void
|
||||
vips_hist_find_class_init( VipsStatsClass *class )
|
||||
vips_hist_find_class_init( VipsHistFindClass *class )
|
||||
{
|
||||
GObjectClass *gobject_class = (GObjectClass *) class;
|
||||
VipsObjectClass *object_class = (VipsObjectClass *) class;
|
||||
@ -395,27 +399,36 @@ vips_hist_find_class_init( VipsStatsClass *class )
|
||||
_( "Output" ),
|
||||
_( "Output histogram" ),
|
||||
VIPS_ARGUMENT_REQUIRED_OUTPUT,
|
||||
G_STRUCT_OFFSET( VipsStats, out ) );
|
||||
G_STRUCT_OFFSET( VipsHistFind, out ) );
|
||||
|
||||
VIPS_ARG_INT( class, "band", 110,
|
||||
_( "Band" ),
|
||||
_( "Find histogram of band" ),
|
||||
VIPS_ARGUMENT_OPTIONAL_INPUT,
|
||||
G_STRUCT_OFFSET( VipsHistFind, band ),
|
||||
-1, 100000, -1 );
|
||||
|
||||
}
|
||||
|
||||
static void
|
||||
vips_hist_find_init( VipsStats *stats )
|
||||
vips_hist_find_init( VipsHistFind *hist_find )
|
||||
{
|
||||
hist_find->band = -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* vips_hist_find:
|
||||
* @in: input image
|
||||
* @out: output image
|
||||
* @bandno: band to equalise
|
||||
* @band: band to equalise
|
||||
*
|
||||
* Find the histogram of @in. Find the histogram for band @bandno (producing a
|
||||
* Find the histogram of @in. Find the histogram for band @band (producing a
|
||||
* one-band histogram), or for all bands (producing an n-band histogram) if
|
||||
* @bandno is -1.
|
||||
* @band is -1.
|
||||
*
|
||||
* @in is cast to u8 or u16. @out is always u32.
|
||||
*
|
||||
* See also: im_hist_indexed(), im_histeq().
|
||||
* See also: vips_hist_find_indexed().
|
||||
*
|
||||
* Returns: 0 on success, -1 on error
|
||||
*/
|
||||
@ -431,94 +444,3 @@ vips_hist_find( VipsImage *in, VipsImage **out, ... )
|
||||
|
||||
return( result );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int
|
||||
im_histgr( VipsImage *in, VipsImage *out, int bandno )
|
||||
{
|
||||
VipsImage *t;
|
||||
int size; /* Length of hist */
|
||||
int bands; /* Number of bands in output */
|
||||
Histogram *mhist;
|
||||
VipsGenerateFn scanfn;
|
||||
int i, j;
|
||||
unsigned int *obuffer, *q;
|
||||
|
||||
/* Check images. PIO from in, WIO to out.
|
||||
*/
|
||||
if( im_check_uncoded( "im_histgr", in ) ||
|
||||
im_check_bandno( "im_histgr", in, bandno ) ||
|
||||
im_pincheck( in ) ||
|
||||
im_outcheck( out ) )
|
||||
return( -1 );
|
||||
|
||||
/* Cast in to u8/u16.
|
||||
*/
|
||||
if( !(t = im_open_local( out, "im_histgr", "p" )) ||
|
||||
im_clip2fmt( in, t, bandfmt_histgr[in->BandFmt] ) )
|
||||
return( -1 );
|
||||
in = t;
|
||||
|
||||
/* Find the range of pixel values we must handle.
|
||||
*/
|
||||
size = in->BandFmt == VIPS_FORMAT_UCHAR ? 256 : 65536;
|
||||
|
||||
/* How many output bands?
|
||||
*/
|
||||
if( bandno == -1 )
|
||||
bands = in->Bands;
|
||||
else
|
||||
bands = 1;
|
||||
|
||||
/* Build main hist we accumulate data in.
|
||||
*/
|
||||
if( !(mhist = build_hist( out, bands, bandno, size )) )
|
||||
return( -1 );
|
||||
|
||||
/* Select scan function.
|
||||
*/
|
||||
if( in->BandFmt == VIPS_FORMAT_UCHAR && bandno == -1 )
|
||||
scanfn = vips_hist_find_uchar_hist;
|
||||
else if( in->BandFmt == VIPS_FORMAT_UCHAR )
|
||||
scanfn = vips_hist_find_uchar_hist_extract;
|
||||
else if( in->BandFmt == VIPS_FORMAT_USHORT && bandno == -1 )
|
||||
scanfn = vips_hist_find_ushort_hist;
|
||||
else
|
||||
scanfn = vips_hist_find_ushort_hist_extract;
|
||||
|
||||
/* Accumulate data.
|
||||
*/
|
||||
if( vips_sink( in,
|
||||
vips_hist_find_start, scanfn, vips_hist_find_stop,
|
||||
mhist, NULL ) )
|
||||
return( -1 );
|
||||
|
||||
/* Make the output image.
|
||||
*/
|
||||
if( vips_image_copy_fields( hist_find->out, statistic->ready ) )
|
||||
return( -1 );
|
||||
im_initdesc( out,
|
||||
mhist->mx + 1, 1, bands, VIPS_FORMAT_UINT,
|
||||
VIPS_CODING_NONE, VIPS_TYPE_HISTOGRAM, 1.0, 1.0, 0, 0 );
|
||||
if( im_setupout( out ) )
|
||||
return( -1 );
|
||||
|
||||
/* Interleave for output.
|
||||
*/
|
||||
if( !(obuffer = VIPS_ARRAY( out,
|
||||
VIPS_IMAGE_N_ELEMENTS( out ), unsigned int )) )
|
||||
return( -1 );
|
||||
for( q = obuffer, j = 0; j < out->Xsize; j++ )
|
||||
for( i = 0; i < out->Bands; i++ )
|
||||
*q++ = mhist->bins[i][j];
|
||||
|
||||
/* Write interleaved buffer into hist.
|
||||
*/
|
||||
if( im_writeline( 0, out, (VipsPel *) obuffer ) )
|
||||
return( -1 );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
@ -3337,6 +3337,25 @@ im_hist( IMAGE *in, IMAGE *out, int bandno )
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int
|
||||
im_histgr( IMAGE *in, IMAGE *out, int bandno )
|
||||
{
|
||||
VipsImage *x;
|
||||
|
||||
if( vips_hist_find( in, &x,
|
||||
"band", bandno,
|
||||
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 )
|
||||
{
|
||||
|
@ -7,7 +7,6 @@ libhistogram_la_SOURCES = \
|
||||
hist_dispatch.c \
|
||||
im_heq.c \
|
||||
im_histeq.c \
|
||||
im_histgr.c \
|
||||
im_histnD.c \
|
||||
im_histplot.c \
|
||||
im_histindexed.c \
|
||||
|
@ -1,400 +0,0 @@
|
||||
/* find histograms
|
||||
*
|
||||
* Copyright: 1990, 1991, N. Dessipris.
|
||||
*
|
||||
* Author: Nicos Dessipris.
|
||||
* Written on: 09/07/1990
|
||||
* Modified on : 11/03/1991
|
||||
* 19/7/93 JC
|
||||
* - test for Coding type added
|
||||
* 26/10/94 JC
|
||||
* - rewritten for ANSI
|
||||
* - now does USHORT too
|
||||
* - 5 x faster!
|
||||
* 2/6/95 JC
|
||||
* - rewritten for partials
|
||||
* 3/3/01 JC
|
||||
* - tiny speed ups
|
||||
* 21/1/07
|
||||
* - number bands from zero
|
||||
* 24/3/10
|
||||
* - gtkdoc
|
||||
* - small cleanups
|
||||
* 25/1/12
|
||||
* - cast @in to u8/u16.
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
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 {
|
||||
int bands; /* Number of bands in output */
|
||||
int which; /* If one band in out, which band of input */
|
||||
int size; /* Length of bins */
|
||||
int mx; /* Maximum value we have seen */
|
||||
unsigned int **bins; /* All the bins! */
|
||||
} Histogram;
|
||||
|
||||
/* Build a Histogram.
|
||||
*/
|
||||
static Histogram *
|
||||
build_hist( IMAGE *out, int bands, int which, int size )
|
||||
{
|
||||
int i;
|
||||
Histogram *hist = IM_NEW( out, Histogram );
|
||||
|
||||
if( !hist || !(hist->bins = IM_ARRAY( out, bands, unsigned int * )) )
|
||||
return( NULL );
|
||||
for( i = 0; i < bands; i++ ) {
|
||||
if( !(hist->bins[i] = IM_ARRAY( out, size, unsigned int )) )
|
||||
return( NULL );
|
||||
memset( hist->bins[i], 0, size * sizeof( unsigned int ) );
|
||||
}
|
||||
|
||||
hist->bands = bands;
|
||||
hist->which = which;
|
||||
hist->size = size;
|
||||
hist->mx = 0;
|
||||
|
||||
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( out, mhist->bands, mhist->which, mhist->size ) );
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
||||
g_assert( shist->bands == mhist->bands && shist->size == mhist->size );
|
||||
|
||||
/* Add on sub-data.
|
||||
*/
|
||||
mhist->mx = IM_MAX( mhist->mx, shist->mx );
|
||||
for( i = 0; i < mhist->bands; i++ )
|
||||
for( j = 0; j < mhist->size; j++ )
|
||||
mhist->bins[i][j] += shist->bins[i][j];
|
||||
|
||||
/* Blank out sub-hist to make sure we can't add it again.
|
||||
*/
|
||||
shist->mx = 0;
|
||||
for( i = 0; i < shist->bands; i++ )
|
||||
shist->bins[i] = NULL;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/* Histogram of all bands of a uchar image.
|
||||
*/
|
||||
static int
|
||||
find_uchar_hist( REGION *reg, void *seq, void *a, void *b, gboolean *stop )
|
||||
{
|
||||
Histogram *hist = (Histogram *) seq;
|
||||
Rect *r = ®->valid;
|
||||
IMAGE *im = reg->im;
|
||||
int le = r->left;
|
||||
int to = r->top;
|
||||
int bo = IM_RECT_BOTTOM(r);
|
||||
int nb = im->Bands;
|
||||
int x, y, z;
|
||||
|
||||
/* Accumulate!
|
||||
*/
|
||||
for( y = to; y < bo; y++ ) {
|
||||
VipsPel *p = IM_REGION_ADDR( reg, le, y );
|
||||
int i;
|
||||
|
||||
for( i = 0, x = 0; x < r->width; x++ )
|
||||
for( z = 0; z < nb; z++, i++ )
|
||||
hist->bins[z][p[i]]++;
|
||||
}
|
||||
|
||||
/* Note the maximum.
|
||||
*/
|
||||
hist->mx = 255;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/* Histogram of a selected band of a uchar image.
|
||||
*/
|
||||
static int
|
||||
find_uchar_hist_extract( REGION *reg,
|
||||
void *seq, void *a, void *b, gboolean *stop )
|
||||
{
|
||||
Histogram *hist = (Histogram *) seq;
|
||||
Rect *r = ®->valid;
|
||||
IMAGE *im = reg->im;
|
||||
int le = r->left;
|
||||
int to = r->top;
|
||||
int bo = IM_RECT_BOTTOM(r);
|
||||
unsigned int *bins = hist->bins[0];
|
||||
int nb = im->Bands;
|
||||
int max = r->width * nb;
|
||||
int x, y;
|
||||
|
||||
/* Accumulate!
|
||||
*/
|
||||
for( y = to; y < bo; y++ ) {
|
||||
VipsPel *p = IM_REGION_ADDR( reg, le, y );
|
||||
|
||||
for( x = hist->which; x < max; x += nb )
|
||||
bins[p[x]]++;
|
||||
}
|
||||
|
||||
/* Note the maximum.
|
||||
*/
|
||||
hist->mx = 255;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/* Histogram of all bands of a ushort image.
|
||||
*/
|
||||
static int
|
||||
find_ushort_hist( REGION *reg, void *seq, void *a, void *b, gboolean *stop )
|
||||
{
|
||||
Histogram *hist = (Histogram *) seq;
|
||||
Rect *r = ®->valid;
|
||||
IMAGE *im = reg->im;
|
||||
int le = r->left;
|
||||
int to = r->top;
|
||||
int bo = IM_RECT_BOTTOM(r);
|
||||
int mx = hist->mx;
|
||||
int nb = im->Bands;
|
||||
int x, y, z;
|
||||
|
||||
/* Accumulate!
|
||||
*/
|
||||
for( y = to; y < bo; y++ ) {
|
||||
unsigned short *p = (unsigned short *)
|
||||
IM_REGION_ADDR( reg, le, y );
|
||||
int i;
|
||||
|
||||
for( i = 0, x = 0; x < r->width; x++ )
|
||||
for( z = 0; z < nb; z++, i++ ) {
|
||||
int v = p[i];
|
||||
|
||||
/* Adjust maximum.
|
||||
*/
|
||||
if( v > mx )
|
||||
mx = v;
|
||||
|
||||
hist->bins[z][v]++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Note the maximum.
|
||||
*/
|
||||
hist->mx = mx;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/* Histogram of all bands of a ushort image.
|
||||
*/
|
||||
static int
|
||||
find_ushort_hist_extract( REGION *reg,
|
||||
void *seq, void *a, void *b, gboolean *stop )
|
||||
{
|
||||
Histogram *hist = (Histogram *) seq;
|
||||
Rect *r = ®->valid;
|
||||
IMAGE *im = reg->im;
|
||||
int le = r->left;
|
||||
int to = r->top;
|
||||
int bo = IM_RECT_BOTTOM(r);
|
||||
int mx = hist->mx;
|
||||
unsigned int *bins = hist->bins[0];
|
||||
int nb = im->Bands;
|
||||
int max = nb * r->width;
|
||||
int x, y;
|
||||
|
||||
/* Accumulate!
|
||||
*/
|
||||
for( y = to; y < bo; y++ ) {
|
||||
unsigned short *p = (unsigned short *)
|
||||
IM_REGION_ADDR( reg, le, y ) + hist->which;
|
||||
|
||||
for( x = hist->which; x < max; x += nb ) {
|
||||
int v = p[x];
|
||||
|
||||
/* Adjust maximum.
|
||||
*/
|
||||
if( v > mx )
|
||||
mx = v;
|
||||
|
||||
bins[v]++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Note the maximum.
|
||||
*/
|
||||
hist->mx = mx;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/* Save a bit of typing.
|
||||
*/
|
||||
#define UC IM_BANDFMT_UCHAR
|
||||
#define US IM_BANDFMT_USHORT
|
||||
#define UI IM_BANDFMT_UINT
|
||||
|
||||
/* Type mapping: go to uchar or ushort.
|
||||
*/
|
||||
static int bandfmt_histgr[10] = {
|
||||
/* UC C US S UI I F X D DX */
|
||||
UC, UC, US, US, US, US, US, US, US, US
|
||||
};
|
||||
|
||||
/**
|
||||
* im_histgr:
|
||||
* @in: input image
|
||||
* @out: output image
|
||||
* @bandno: band to equalise
|
||||
*
|
||||
* Find the histogram of @in. Find the histogram for band @bandno (producing a
|
||||
* one-band histogram), or for all bands (producing an n-band histogram) if
|
||||
* @bandno is -1.
|
||||
*
|
||||
* @in is cast to u8 or u16. @out is always u32.
|
||||
*
|
||||
* See also: im_hist_indexed(), im_histeq().
|
||||
*
|
||||
* Returns: 0 on success, -1 on error
|
||||
*/
|
||||
int
|
||||
im_histgr( IMAGE *in, IMAGE *out, int bandno )
|
||||
{
|
||||
IMAGE *t;
|
||||
int size; /* Length of hist */
|
||||
int bands; /* Number of bands in output */
|
||||
Histogram *mhist;
|
||||
VipsGenerateFn scanfn;
|
||||
int i, j;
|
||||
unsigned int *obuffer, *q;
|
||||
|
||||
/* Check images. PIO from in, WIO to out.
|
||||
*/
|
||||
if( im_check_uncoded( "im_histgr", in ) ||
|
||||
im_check_bandno( "im_histgr", in, bandno ) ||
|
||||
im_pincheck( in ) ||
|
||||
im_outcheck( out ) )
|
||||
return( -1 );
|
||||
|
||||
/* Cast in to u8/u16.
|
||||
*/
|
||||
if( !(t = im_open_local( out, "im_histgr", "p" )) ||
|
||||
im_clip2fmt( in, t, bandfmt_histgr[in->BandFmt] ) )
|
||||
return( -1 );
|
||||
in = t;
|
||||
|
||||
/* Find the range of pixel values we must handle.
|
||||
*/
|
||||
size = in->BandFmt == IM_BANDFMT_UCHAR ? 256 : 65536;
|
||||
|
||||
/* How many output bands?
|
||||
*/
|
||||
if( bandno == -1 )
|
||||
bands = in->Bands;
|
||||
else
|
||||
bands = 1;
|
||||
|
||||
/* Build main hist we accumulate data in.
|
||||
*/
|
||||
if( !(mhist = build_hist( out, bands, bandno, size )) )
|
||||
return( -1 );
|
||||
|
||||
/* Select scan function.
|
||||
*/
|
||||
if( in->BandFmt == IM_BANDFMT_UCHAR && bandno == -1 )
|
||||
scanfn = find_uchar_hist;
|
||||
else if( in->BandFmt == IM_BANDFMT_UCHAR )
|
||||
scanfn = find_uchar_hist_extract;
|
||||
else if( in->BandFmt == IM_BANDFMT_USHORT && bandno == -1 )
|
||||
scanfn = find_ushort_hist;
|
||||
else
|
||||
scanfn = find_ushort_hist_extract;
|
||||
|
||||
/* Accumulate data.
|
||||
*/
|
||||
if( vips_sink( in,
|
||||
build_subhist, scanfn, merge_subhist, mhist, NULL ) )
|
||||
return( -1 );
|
||||
|
||||
/* Make the output image.
|
||||
*/
|
||||
if( im_cp_desc( out, in ) )
|
||||
return( -1 );
|
||||
im_initdesc( out,
|
||||
mhist->mx + 1, 1, bands, 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 for output.
|
||||
*/
|
||||
if( !(obuffer = IM_ARRAY( out,
|
||||
IM_IMAGE_N_ELEMENTS( out ), unsigned int )) )
|
||||
return( -1 );
|
||||
for( q = obuffer, j = 0; j < out->Xsize; j++ )
|
||||
for( i = 0; i < out->Bands; i++ )
|
||||
*q++ = mhist->bins[i][j];
|
||||
|
||||
/* Write interleaved buffer into hist.
|
||||
*/
|
||||
if( im_writeline( 0, out, (VipsPel *) obuffer ) )
|
||||
return( -1 );
|
||||
|
||||
return( 0 );
|
||||
}
|
@ -377,6 +377,8 @@ int vips_stats( VipsImage *in, VipsImage **out, ... )
|
||||
__attribute__((sentinel));
|
||||
int vips_measure( VipsImage *in, VipsImage **out, int h, int v, ... )
|
||||
__attribute__((sentinel));
|
||||
int vips_hist_find( VipsImage *in, VipsImage **out, ... )
|
||||
__attribute__((sentinel));
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -42,9 +42,6 @@ int vips_maplut( VipsImage *in, VipsImage **out, VipsImage *lut, ... )
|
||||
__attribute__((sentinel));
|
||||
|
||||
|
||||
|
||||
|
||||
int im_histgr( VipsImage *in, VipsImage *out, int bandno );
|
||||
int im_histnD( VipsImage *in, VipsImage *out, int bins );
|
||||
int im_hist_indexed( VipsImage *index, VipsImage *value, VipsImage *out );
|
||||
|
||||
|
@ -851,6 +851,7 @@ int im_quadratic( IMAGE *in, IMAGE *out, IMAGE *coeff );
|
||||
|
||||
int im_maplut( VipsImage *in, VipsImage *out, VipsImage *lut );
|
||||
int im_hist( VipsImage *in, VipsImage *out, int bandno );
|
||||
int im_histgr( VipsImage *in, VipsImage *out, int bandno );
|
||||
|
||||
/* ruby-vips uses this
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user