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 466
|
/* stats.c ... many image stats in a single pass
*
(C) Kirk Martinez 1993
23/4/93 J.Cupitt
- adapted to partial images
- special struct abandoned; now returns DOUBLEMASK.
1/7/93 JC
- adapted for partial v2
- ANSIfied
27/7/93 JC
- init of mask changed to use actual values, not IM_MAXDOUBLE and
(-IM_MAXDOUBLE). These caused problems when assigned to floats.
funny business with offset==42, yuk!
31/8/93 JC
- forgot to init global max/min properly! sorry.
21/6/95 JC
- still did not init max and min correctly --- now fixed for good
* 13/1/05
* - use 64 bit arithmetic
* 1/9/09
* - argh nope min/max was broken again for >1CPU in short pipelines on
* some architectures
* 7/9/09
* - rework based on new im__wrapscan() / im_max() ideas for a proper fix
* - gtkdoc comment
* 7/11/11
* - redone as a class
* - track maxpos / minpos too
*/
/*
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
*/
/*
#define VIPS_DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include "statistic.h"
typedef struct _VipsStats {
VipsStatistic parent_instance;
VipsImage *out;
gboolean set; /* FALSE means no value yet */
} VipsStats;
typedef VipsStatisticClass VipsStatsClass;
G_DEFINE_TYPE( VipsStats, vips_stats, VIPS_TYPE_STATISTIC );
/* Names for our columns.
*/
enum {
COL_MIN = 0,
COL_MAX = 1,
COL_SUM = 2,
COL_SUM2 = 3,
COL_AVG = 4,
COL_SD = 5,
COL_XMIN = 6,
COL_YMIN = 7,
COL_XMAX = 8,
COL_YMAX = 9,
COL_LAST = 10
};
static int
vips_stats_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsStatistic *statistic = VIPS_STATISTIC( object );
VipsStats *stats = (VipsStats *) object;
gint64 vals, pels;
double *row0, *row;
int b, y, i;
if( vips_object_argument_isset( object, "in" ) ) {
int bands = vips_image_get_bands( statistic->in );
if( vips_check_noncomplex( class->nickname, statistic->in ) )
return( -1 );
g_object_set( object,
"out", vips_image_new_matrix( COL_LAST, bands + 1 ),
NULL );
}
if( VIPS_OBJECT_CLASS( vips_stats_parent_class )->build( object ) )
return( -1 );
pels = (gint64) vips_image_get_width( statistic->in ) *
vips_image_get_height( statistic->in );
vals = pels * vips_image_get_bands( statistic->in );
row0 = VIPS_MATRIX( stats->out, 0, 0 );
row = VIPS_MATRIX( stats->out, 0, 1 );
for( i = 0; i < COL_LAST; i++ )
row0[i] = row[i];
for( b = 1; b < vips_image_get_bands( statistic->in ); b++ ) {
row = VIPS_MATRIX( stats->out, 0, b + 1 );
if( row[COL_MIN] < row0[COL_MIN] ) {
row0[COL_MIN] = row[COL_MIN];
row0[COL_XMIN] = row[COL_XMIN];
row0[COL_YMIN] = row[COL_YMIN];
}
if( row[COL_MAX] > row0[COL_MAX] ) {
row0[COL_MAX] = row[COL_MAX];
row0[COL_XMAX] = row[COL_XMAX];
row0[COL_YMAX] = row[COL_YMAX];
}
row0[COL_SUM] += row[COL_SUM];
row0[COL_SUM2] += row[COL_SUM2];
}
for( y = 1; y < vips_image_get_height( stats->out ); y++ ) {
double *row = VIPS_MATRIX( stats->out, 0, y );
row[COL_AVG] = row[COL_SUM] / pels;
row[COL_SD] = sqrt( VIPS_FABS( row[COL_SUM2] -
(row[COL_SUM] * row[COL_SUM] / pels) ) / (pels - 1) );
}
row0[COL_AVG] = row0[COL_SUM] / vals;
row0[COL_SD] = sqrt( VIPS_FABS( row0[COL_SUM2] -
(row0[COL_SUM] * row0[COL_SUM] / vals) ) / (vals - 1) );
return( 0 );
}
/* Stop function. Add these little stats to the main set of stats.
*/
static int
vips_stats_stop( VipsStatistic *statistic, void *seq )
{
int bands = vips_image_get_bands( statistic->in );
VipsStats *global = (VipsStats *) statistic;
VipsStats *local = (VipsStats *) seq;
int b;
if( local->set && !global->set ) {
for( b = 0; b < bands; b++ ) {
double *p = VIPS_MATRIX( local->out, 0, b + 1 );
double *q = VIPS_MATRIX( global->out, 0, b + 1 );
int i;
for( i = 0; i < COL_LAST; i++ )
q[i] = p[i];
}
global->set = TRUE;
}
else if( local->set && global->set ) {
for( b = 0; b < bands; b++ ) {
double *p = VIPS_MATRIX( local->out, 0, b + 1 );
double *q = VIPS_MATRIX( global->out, 0, b + 1 );
if( p[COL_MIN] < q[COL_MIN] ) {
q[COL_MIN] = p[COL_MIN];
q[COL_XMIN] = p[COL_XMIN];
q[COL_YMIN] = p[COL_YMIN];
}
if( p[COL_MAX] > q[COL_MAX] ) {
q[COL_MAX] = p[COL_MAX];
q[COL_XMAX] = p[COL_XMAX];
q[COL_YMAX] = p[COL_YMAX];
}
q[COL_SUM] += p[COL_SUM];
q[COL_SUM2] += p[COL_SUM2];
}
}
VIPS_FREEF( g_object_unref, local->out );
VIPS_FREEF( g_free, seq );
return( 0 );
}
/* Start function: make a dummy local stats for the private use of this thread.
*/
static void *
vips_stats_start( VipsStatistic *statistic )
{
int bands = vips_image_get_bands( statistic->in );
VipsStats *stats;
stats = g_new( VipsStats, 1 );
if( !(stats->out = vips_image_new_matrix( COL_LAST, bands + 1 )) ) {
g_free( stats );
return( NULL );
}
stats->set = FALSE;
return( (void *) stats );
}
/* We scan lines bands times to avoid repeating band loops.
* Use temp variables of same type for min/max for faster comparisons.
*/
#define LOOP( TYPE ) { \
for( b = 0; b < bands; b++ ) { \
TYPE *p = ((TYPE *) in) + b; \
double *q = VIPS_MATRIX( local->out, 0, b + 1 ); \
TYPE small, big; \
double sum, sum2; \
int xmin, ymin; \
int xmax, ymax; \
\
if( local->set ) { \
small = q[COL_MIN]; \
big = q[COL_MAX]; \
sum = q[COL_SUM]; \
sum2 = q[COL_SUM2]; \
xmin = q[COL_XMIN]; \
ymin = q[COL_YMIN]; \
xmax = q[COL_XMAX]; \
ymax = q[COL_YMAX]; \
} \
else { \
small = p[0]; \
big = p[0]; \
sum = 0; \
sum2 = 0; \
xmin = x; \
ymin = y; \
xmax = x; \
ymax = y; \
} \
\
for( i = 0; i < n; i++ ) { \
TYPE value = *p; \
\
sum += value; \
sum2 += (double) value * (double) value; \
if( value > big ) { \
big = value; \
xmax = x + i; \
ymax = y; \
} \
else if( value < small ) { \
small = value; \
xmin = x + i; \
ymin = y; \
} \
\
p += bands; \
} \
\
q[COL_MIN] = small; \
q[COL_MAX] = big; \
q[COL_SUM] = sum; \
q[COL_SUM2] = sum2; \
q[COL_XMIN] = xmin; \
q[COL_YMIN] = ymin; \
q[COL_XMAX] = xmax; \
q[COL_YMAX] = ymax; \
} \
\
local->set = TRUE; \
}
/* As above, but for float/double types where we have to avoid NaN.
*/
#define LOOPF( TYPE ) { \
for( b = 0; b < bands; b++ ) { \
TYPE *p = ((TYPE *) in) + b; \
double *q = VIPS_MATRIX( local->out, 0, b + 1 ); \
TYPE small, big; \
double sum, sum2; \
int xmin, ymin; \
int xmax, ymax; \
\
if( local->set ) { \
small = q[COL_MIN]; \
big = q[COL_MAX]; \
sum = q[COL_SUM]; \
sum2 = q[COL_SUM2]; \
xmin = q[COL_XMIN]; \
ymin = q[COL_YMIN]; \
xmax = q[COL_XMAX]; \
ymax = q[COL_YMAX]; \
} \
else { \
small = p[0]; \
big = p[0]; \
sum = 0; \
sum2 = 0; \
xmin = x; \
ymin = y; \
xmax = x; \
ymax = y; \
} \
\
for( i = 0; i < n; i++ ) { \
TYPE value = *p; \
\
sum += value; \
sum2 += (double) value * (double) value; \
if( value > big ) { \
big = value; \
xmax = x + i; \
ymax = y; \
} \
else if( value < small ) { \
small = value; \
xmin = x + i; \
ymin = y; \
} \
\
p += bands; \
} \
\
q[COL_MIN] = small; \
q[COL_MAX] = big; \
q[COL_SUM] = sum; \
q[COL_SUM2] = sum2; \
q[COL_XMIN] = xmin; \
q[COL_YMIN] = ymin; \
q[COL_XMAX] = xmax; \
q[COL_YMAX] = ymax; \
} \
\
local->set = TRUE; \
}
/* Loop over region, accumulating a sum in *tmp.
*/
static int
vips_stats_scan( VipsStatistic *statistic, void *seq,
int x, int y, void *in, int n )
{
const int bands = vips_image_get_bands( statistic->in );
VipsStats *local = (VipsStats *) seq;
int b, i;
switch( vips_image_get_format( statistic->in ) ) {
case VIPS_FORMAT_UCHAR: LOOP( unsigned char ); break;
case VIPS_FORMAT_CHAR: LOOP( signed char ); break;
case VIPS_FORMAT_USHORT: LOOP( unsigned short ); break;
case VIPS_FORMAT_SHORT: LOOP( signed short ); break;
case VIPS_FORMAT_UINT: LOOP( unsigned int ); break;
case VIPS_FORMAT_INT: LOOP( signed int ); break;
case VIPS_FORMAT_FLOAT: LOOP( float ); break;
case VIPS_FORMAT_DOUBLE: LOOP( double ); break;
default:
g_assert_not_reached();
}
return( 0 );
}
static void
vips_stats_class_init( VipsStatsClass *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 = "stats";
object_class->description = _( "find many image stats" );
object_class->build = vips_stats_build;
sclass->start = vips_stats_start;
sclass->scan = vips_stats_scan;
sclass->stop = vips_stats_stop;
VIPS_ARG_IMAGE( class, "out", 100,
_( "Output" ),
_( "Output array of statistics" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsStats, out ) );
}
static void
vips_stats_init( VipsStats *stats )
{
}
/**
* vips_stats: (method)
* @in: image to scan
* @out: (out): image of statistics
* @...: %NULL-terminated list of optional named arguments
*
* Find many image statistics in a single pass through the data. @out is a
* one-band #VIPS_FORMAT_DOUBLE image of at least 10 columns by n + 1
* (where n is number of bands in image @in)
* rows. Columns are statistics, and are, in order: minimum, maximum, sum,
* sum of squares, mean, standard deviation, x coordinate of minimum, y
* coordinate of minimum, x coordinate of maximum, y coordinate of maximum.
* Later versions of vips_stats() may add more columns.
*
* Row 0 has statistics for all
* bands together, row 1 has stats for band 1, and so on.
*
* If there is more than one maxima or minima, one of them will be chosen at
* random.
*
* See also: vips_avg(), vips_min().
*
* Returns: 0 on success, -1 on error
*/
int
vips_stats( VipsImage *in, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "stats", ap, in, out );
va_end( ap );
return( result );
}
|