1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
|
/* 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.
* 12/8/13
* - redo as a class
* 28/2/16 lovell
* - unroll common cases
* 1/2/21 erdmann
* - use double for very large histograms
*/
/*
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 <glib/gi18n-lib.h>
#include <string.h>
#include <vips/vips.h>
#include "statistic.h"
/* Accumulate a histogram in one of these.
*/
typedef struct {
int n_bands; /* Number of bands in output */
int band; /* If one band in out, which band of input */
int size; /* Number of bins for each band */
int mx; /* Maximum value we have seen */
VipsPel **bins; /* double or uint bins */
} Histogram;
typedef struct _VipsHistFind {
VipsStatistic parent_instance;
/* -1 for all bands, or the band we scan.
*/
int band;
/* Main image histogram. Subhists accumulate to this.
*/
Histogram *hist;
/* Write hist to this output image.
*/
VipsImage *out;
/* TRUE for "large" histograms ... causes double output rather than
* uint.
*/
gboolean large;
} VipsHistFind;
typedef VipsStatisticClass VipsHistFindClass;
G_DEFINE_TYPE( VipsHistFind, vips_hist_find, VIPS_TYPE_STATISTIC );
/* Build a Histogram.
*/
static Histogram *
histogram_new( VipsHistFind *hist_find, int n_bands, int band, int size )
{
/* We won't use all of this for uint accumulators.
*/
int n_bytes = size * sizeof( double );
Histogram *hist;
int i;
if( !(hist = VIPS_NEW( hist_find, Histogram )) ||
!(hist->bins = VIPS_ARRAY( hist_find, n_bands, VipsPel * )) )
return( NULL );
for( i = 0; i < n_bands; i++ ) {
if( !(hist->bins[i] = VIPS_ARRAY( hist_find,
n_bytes, VipsPel )) )
return( NULL );
memset( hist->bins[i], 0, n_bytes );
}
hist->n_bands = n_bands;
hist->band = band;
hist->size = size;
hist->mx = 0;
return( hist );
}
static int
vips_hist_find_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsStatistic *statistic = VIPS_STATISTIC( object );
VipsHistFind *hist_find = (VipsHistFind *) object;
VipsImage *in = statistic->in;
VipsPel *obuffer;
g_object_set( object,
"out", vips_image_new(),
NULL );
if( in &&
vips_check_bandno( class->nickname, in, hist_find->band ) )
return( -1 );
/* Is this a large histogram? We want to avoid overflow of the uint
* accumulators.
*/
if( in &&
(guint64) in->Xsize * (guint64) in->Ysize >=
((guint64) 1 << 32) )
hist_find->large = TRUE;
/* main hist made on in vips_hist_find_start().
*/
if( VIPS_OBJECT_CLASS( vips_hist_find_parent_class )->build( object ) )
return( -1 );
/* Make the output image.
*/
if( vips_image_pipelinev( hist_find->out,
VIPS_DEMAND_STYLE_ANY, statistic->ready, NULL ) )
return( -1 );
vips_image_init_fields( hist_find->out,
hist_find->hist->mx + 1, 1, hist_find->hist->n_bands,
hist_find->large ? VIPS_FORMAT_DOUBLE : VIPS_FORMAT_UINT,
VIPS_CODING_NONE, VIPS_INTERPRETATION_HISTOGRAM, 1.0, 1.0 );
/* Interleave for output.
*/
if( !(obuffer = VIPS_ARRAY( object,
VIPS_IMAGE_SIZEOF_LINE( hist_find->out ), VipsPel )) )
return( -1 );
#define INTERLEAVE( TYPE ) G_STMT_START { \
TYPE **bins = (TYPE **) hist_find->hist->bins; \
\
TYPE *q; \
int i, j; \
\
for( q = (TYPE *) obuffer, j = 0; j < hist_find->out->Xsize; j++ ) \
for( i = 0; i < hist_find->out->Bands; i++ ) \
*q++ = bins[i][j]; \
} G_STMT_END
if( hist_find->large )
INTERLEAVE( double );
else
INTERLEAVE( unsigned int );
if( vips_image_write_line( hist_find->out, 0, obuffer ) )
return( -1 );
return( 0 );
}
/* Build a sub-hist, based on the main hist.
*/
static void *
vips_hist_find_start( VipsStatistic *statistic )
{
VipsHistFind *hist_find = (VipsHistFind *) statistic;
/* Make the main hist, if necessary.
*/
if( !hist_find->hist )
hist_find->hist = histogram_new( hist_find,
hist_find->band == -1 ?
statistic->ready->Bands : 1,
hist_find->band,
statistic->ready->BandFmt == VIPS_FORMAT_UCHAR ?
256 : 65536 );
return( (void *) histogram_new( hist_find,
hist_find->hist->n_bands,
hist_find->hist->band,
hist_find->hist->size ) );
}
/* Join a sub-hist onto the main hist.
*/
static int
vips_hist_find_stop( VipsStatistic *statistic, void *seq )
{
Histogram *sub_hist = (Histogram *) seq;
VipsHistFind *hist_find = (VipsHistFind *) statistic;
Histogram *hist = hist_find->hist;
int i, j;
g_assert( sub_hist->n_bands == hist->n_bands &&
sub_hist->size == hist->size );
/* Add on sub-data.
*/
hist->mx = VIPS_MAX( hist->mx, sub_hist->mx );
#define SUM( TYPE ) G_STMT_START { \
TYPE **main_bins = (TYPE **) hist->bins; \
TYPE **sub_bins = (TYPE **) sub_hist->bins; \
\
for( i = 0; i < hist->n_bands; i++ ) \
for( j = 0; j < hist->size; j++ ) \
main_bins[i][j] += sub_bins[i][j]; \
} G_STMT_END
if( hist_find->large )
SUM( double );
else
SUM( unsigned int );
/* Blank out sub-hist to make sure we can't add it again.
*/
sub_hist->mx = 0;
for( i = 0; i < sub_hist->n_bands; i++ )
sub_hist->bins[i] = NULL;
return( 0 );
}
#define SCANOP G_STMT_START { \
for( z = 0; z < nb; z++ ) { \
int v = p[z]; \
\
if( v > mx ) \
mx = v; \
\
bins[z][v] += 1; \
} \
\
p += nb; \
} G_STMT_END
/* No need to track max for uchar images (it's always 255).
*/
#define UCSCANOP G_STMT_START { \
for( z = 0; z < nb; z++ ) { \
int v = p[z]; \
\
bins[z][v] += 1; \
} \
\
p += nb; \
} G_STMT_END
/* Hist of all bands. This one is just about worth unrolling.
*/
#define SCAN( IMAGE_TYPE, HIST_TYPE, OP ) G_STMT_START { \
HIST_TYPE **bins = (HIST_TYPE **) hist->bins; \
IMAGE_TYPE *p = (IMAGE_TYPE *) in; \
\
int z; \
\
VIPS_UNROLL( n, OP ); \
} G_STMT_END
/* Hist of selected band.
*/
#define SCAN1( IMAGE_TYPE, HIST_TYPE ) G_STMT_START { \
HIST_TYPE *bins = (HIST_TYPE *) hist->bins[0]; \
IMAGE_TYPE *p = (IMAGE_TYPE *) in; \
int max = nb * n; \
\
for( i = hist->band; i < max; i += nb ) { \
int v = p[i]; \
\
if( v > mx ) \
mx = v; \
\
bins[v] += 1; \
} \
} G_STMT_END
static int
vips_hist_find_scan( VipsStatistic *statistic, void *seq,
int x, int y, void *in, int n )
{
VipsHistFind *hist_find = (VipsHistFind *) statistic;
Histogram *hist = (Histogram *) seq;
int nb = statistic->ready->Bands;
int mx = hist->mx;
int i;
if( hist_find->band < 0 )
switch( statistic->ready->BandFmt ) {
case VIPS_FORMAT_UCHAR:
if( hist_find->large )
SCAN( unsigned char, double, UCSCANOP );
else
SCAN( unsigned char, unsigned int, UCSCANOP );
mx = 255;
break;
case VIPS_FORMAT_USHORT:
if( hist_find->large )
SCAN( unsigned short, double, SCANOP );
else
SCAN( unsigned short, unsigned int, SCANOP );
break;
default:
g_assert_not_reached();
}
else
switch( statistic->ready->BandFmt ) {
case VIPS_FORMAT_UCHAR:
if( hist_find->large )
SCAN1( unsigned char, double );
else
SCAN1( unsigned char, unsigned int );
break;
case VIPS_FORMAT_USHORT:
if( hist_find->large )
SCAN1( unsigned short, double );
else
SCAN1( unsigned short, unsigned int );
break;
default:
g_assert_not_reached();
}
hist->mx = mx;
return( 0 );
}
/* 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_hist_find_format_table[10] = {
/* Band format: UC C US S UI I F X D DX */
/* Promotion: */ UC, UC, US, US, US, US, US, US, US, US
};
static void
vips_hist_find_class_init( VipsHistFindClass *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";
object_class->description = _( "find image histogram" );
object_class->build = vips_hist_find_build;
sclass->start = vips_hist_find_start;
sclass->scan = vips_hist_find_scan;
sclass->stop = vips_hist_find_stop;
sclass->format_table = vips_hist_find_format_table;
VIPS_ARG_IMAGE( class, "out", 100,
_( "Output" ),
_( "Output histogram" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT,
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( VipsHistFind *hist_find )
{
hist_find->band = -1;
}
/**
* vips_hist_find: (method)
* @in: input image
* @out: (out): output image
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* * @band: band to equalise
*
* 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
* @band is -1.
*
* char and uchar images are cast to uchar before histogramming, all other
* image types are cast to ushort.
*
* See also: vips_hist_find_ndim(), vips_hist_find_indexed().
*
* Returns: 0 on success, -1 on error
*/
int
vips_hist_find( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "hist_find", ap, in, out );
va_end( ap );
return( result );
}
|