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
|
/* find image maximum
*
* Copyright: 1990, J. Cupitt
*
* Author: J. Cupitt
* Written on: 02/05/1990
* Modified on : 18/03/1991, N. Dessipris
* 23/11/92: J.Cupitt - correct result for more than 1 band now.
* 23/7/93 JC
* - im_incheck() call added
* 20/6/95 JC
* - now returns double for value, like im_max()
* 4/9/09
* - gtkdoc comment
* 8/9/09
* - rewrite based on im_max() to get partial
* - move im_max() in here as a convenience function
* 6/11/11
* - rewrite as a class
* - abandon scan if we find maximum possible value
* 24/2/12
* - avoid NaN in float/double/complex images
* - allow +/- INFINITY as a result
*/
/*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include "statistic.h"
typedef struct _VipsMax {
VipsStatistic parent_instance;
gboolean set; /* FALSE means no value yet */
/* The current maximum. When scanning complex images, we keep the
* square of the modulus here and do a single sqrt() right at the end.
*/
double max;
/* And its position.
*/
int x, y;
} VipsMax;
typedef VipsStatisticClass VipsMaxClass;
G_DEFINE_TYPE( VipsMax, vips_max, VIPS_TYPE_STATISTIC );
static int
vips_max_build( VipsObject *object )
{
VipsStatistic *statistic = VIPS_STATISTIC( object );
VipsMax *max = (VipsMax *) object;
if( VIPS_OBJECT_CLASS( vips_max_parent_class )->build( object ) )
return( -1 );
/* Don't set if there's no value (eg. if every pixel is NaN). This
* will trigger an error later.
*/
if( max->set ) {
double m;
/* For speed we accumulate max^2 for complex.
*/
m = max->max;
if( vips_bandfmt_iscomplex(
vips_image_get_format( statistic->in ) ) )
m = sqrt( m );
/* We have to set the props via g_object_set() to stop vips
* complaining they are unset.
*/
g_object_set( max,
"out", m,
"x", max->x,
"y", max->y,
NULL );
}
return( 0 );
}
/* New sequence value. Make a private VipsMax for this thread.
*/
static void *
vips_max_start( VipsStatistic *statistic )
{
VipsMax *max;
max = g_new( VipsMax, 1 );
max->set = FALSE;
return( (void *) max );
}
/* Merge the sequence value back into the per-call state.
*/
static int
vips_max_stop( VipsStatistic *statistic, void *seq )
{
VipsMax *global = (VipsMax *) statistic;
VipsMax *local = (VipsMax *) seq;
if( local->set &&
(!global->set || local->max > global->max) ) {
global->max = local->max;
global->x = local->x;
global->y = local->y;
global->set = TRUE;
}
g_free( local );
return( 0 );
}
/* real max with an upper bound.
*/
#define LOOPU( TYPE, UPPER ) { \
TYPE *p = (TYPE *) in; \
TYPE m; \
\
if( max->set ) \
m = max->max; \
else { \
m = p[0]; \
max->x = x; \
max->y = y; \
} \
\
for( i = 0; i < sz; i++ ) { \
if( p[i] > m ) { \
m = p[i]; \
max->x = x + i / bands; \
max->y = y; \
if( m >= UPPER ) { \
statistic->stop = TRUE; \
break; \
} \
} \
} \
\
max->max = m; \
max->set = TRUE; \
}
/* float/double max ... no limits, and we have to avoid NaN.
*
* NaN compares false to every float value, so if we were to take the first
* point in this buffer as our start max (as we do above) and it was NaN, we'd
* never replace it with a true value.
*/
#define LOOPF( TYPE ) { \
TYPE *p = (TYPE *) in; \
TYPE m; \
gboolean set; \
\
set = max->set; \
m = max->max; \
\
for( i = 0; i < sz; i++ ) { \
if( set ) { \
if( p[i] > m ) { \
m = p[i]; \
max->x = x + i / bands; \
max->y = y; \
} \
} \
else if( !isnan( p[i] ) ) { \
m = p[i]; \
max->x = x + i / bands; \
max->y = y; \
set = TRUE; \
} \
} \
\
if( set ) { \
max->max = m; \
max->set = TRUE; \
} \
}
/* As LOOPF, but complex. Track max(mod) to avoid sqrt().
*/
#define LOOPC( TYPE ) { \
TYPE *p = (TYPE *) in; \
TYPE m; \
gboolean set; \
\
set = max->set; \
m = max->max; \
\
for( i = 0; i < sz; i++ ) { \
TYPE mod; \
\
mod = p[0] * p[0] + p[1] * p[1]; \
p += 2; \
\
if( set ) { \
if( mod > m ) { \
m = mod; \
max->x = x + i / bands; \
max->y = y; \
} \
} \
else if( !isnan( mod ) ) { \
m = mod; \
max->x = x + i / bands; \
max->y = y; \
set = TRUE; \
} \
} \
\
if( set ) { \
max->max = m; \
max->set = TRUE; \
} \
}
/* Loop over region, adding to seq.
*/
static int
vips_max_scan( VipsStatistic *statistic, void *seq,
int x, int y, void *in, int n )
{
VipsMax *max = (VipsMax *) seq;
const int bands = vips_image_get_bands( statistic->in );
const int sz = n * bands;
int i;
switch( vips_image_get_format( statistic->in ) ) {
case VIPS_FORMAT_UCHAR:
LOOPU( unsigned char, UCHAR_MAX ); break;
case VIPS_FORMAT_CHAR:
LOOPU( signed char, SCHAR_MAX ); break;
case VIPS_FORMAT_USHORT:
LOOPU( unsigned short, USHRT_MAX ); break;
case VIPS_FORMAT_SHORT:
LOOPU( signed short, SHRT_MAX ); break;
case VIPS_FORMAT_UINT:
LOOPU( unsigned int, UINT_MAX ); break;
case VIPS_FORMAT_INT:
LOOPU( signed int, INT_MAX ); break;
case VIPS_FORMAT_FLOAT:
LOOPF( float ); break;
case VIPS_FORMAT_DOUBLE:
LOOPF( double ); break;
case VIPS_FORMAT_COMPLEX:
LOOPC( float ); break;
case VIPS_FORMAT_DPCOMPLEX:
LOOPC( double ); break;
default:
g_assert( 0 );
}
return( 0 );
}
static void
vips_max_class_init( VipsMaxClass *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 = "max";
object_class->description = _( "find image maximum" );
object_class->build = vips_max_build;
sclass->start = vips_max_start;
sclass->scan = vips_max_scan;
sclass->stop = vips_max_stop;
VIPS_ARG_DOUBLE( class, "out", 1,
_( "Output" ),
_( "Output value" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsMax, max ),
-INFINITY, INFINITY, 0.0 );
VIPS_ARG_INT( class, "x", 2,
_( "x" ),
_( "Horizontal position of maximum" ),
VIPS_ARGUMENT_OPTIONAL_OUTPUT,
G_STRUCT_OFFSET( VipsMax, x ),
0, 1000000, 0 );
VIPS_ARG_INT( class, "y", 2,
_( "y" ),
_( "Vertical position of maximum" ),
VIPS_ARGUMENT_OPTIONAL_OUTPUT,
G_STRUCT_OFFSET( VipsMax, y ),
0, 1000000, 0 );
}
static void
vips_max_init( VipsMax *max )
{
}
/**
* vips_max:
* @in: input #VipsImage
* @out: output pixel maximum
* @...: %NULL-terminated list of optional named arguments
*
* Optional arguments:
*
* @x: horizontal position of maximum
* @y: vertical position of maximum
*
* This operation finds the maximum value in an image.
*
* If the image contains several maximum values, only the first one found is
* returned.
*
* It operates on all
* bands of the input image: use vips_stats() if you need to find an
* maximum for each band.
*
* For complex images, this operation finds the maximum modulus.
*
* See also: vips_min(), vips_stats().
*
* Returns: 0 on success, -1 on error
*/
int
vips_max( VipsImage *in, double *out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "max", ap, in, out );
va_end( ap );
return( result );
}
|