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
|
/* im_maxpos_avg.c
*
* Copyright: 2006, The Nottingham Trent University
* Copyright: 2006, Tom Vajzovic
*
* Author: Tom Vajzovic
*
* Written on: 2006-09-25
* 15/10/07 JC
* - changed spelling of occurrences
* - check for !occurrences before using val
* - renamed avg as sum, a bit clearer
* 2/9/09
* - gtkdoc comment
* 8/9/08
* - rewrite from im_maxpos()
* - now handles many bands, complex, faster
* 27/7/14
* - fix a race ... did not merge states if max was equal
* 26/3/15
* - avoid NaN, thanks Paul
*/
/*
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 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/vips7compat.h>
#include <vips/internal.h>
/* A position and maximum.
*/
typedef struct _Maxposavg {
int xpos;
int ypos;
double max;
/* occurences == 0 means we found no points, or we are uninitialised.
*/
int occurences;
} Maxposavg;
/* New sequence value.
*/
static void *
maxposavg_start( IMAGE *in, void *a, void *b )
{
Maxposavg *global_maxposavg = (Maxposavg *) b;
Maxposavg *maxposavg;
if( !(maxposavg = IM_NEW( NULL, Maxposavg )) )
return( NULL );
*maxposavg = *global_maxposavg;
return( (void *) maxposavg );
}
/* Merge the sequence value back into the per-call state.
*/
static int
maxposavg_stop( void *seq, void *a, void *b )
{
Maxposavg *global_maxposavg = (Maxposavg *) b;
Maxposavg *maxposavg = (Maxposavg *) seq;
/* Merge.
*/
if( maxposavg->occurences == 0 ) {
}
else if( maxposavg->max > global_maxposavg->max )
*global_maxposavg = *maxposavg;
else if( maxposavg->max == global_maxposavg->max ) {
global_maxposavg->xpos += maxposavg->xpos;
global_maxposavg->ypos += maxposavg->ypos;
global_maxposavg->occurences += maxposavg->occurences;
}
im_free( seq );
return( 0 );
}
/* int loop.
*/
#define ILOOP( TYPE ) { \
TYPE *p = (TYPE *) in; \
TYPE m; \
\
m = max; \
\
for( x = 0; x < sz; x++ ) { \
TYPE v = p[x]; \
\
if( occurences == 0 || v > m ) { \
m = v; \
xpos = r->left + x / reg->im->Bands; \
ypos = r->top + y; \
occurences = 1; \
} \
else if( v == m ) { \
xpos += r->left + x / reg->im->Bands; \
ypos += r->top + y; \
occurences += 1; \
} \
} \
\
max = m; \
}
/* float/double loop ... avoid NaN.
*/
#define FLOOP( TYPE ) { \
TYPE *p = (TYPE *) in; \
TYPE m; \
\
m = max; \
\
for( x = 0; x < sz; x++ ) { \
TYPE v = p[x]; \
\
if( isnan( v ) ) { \
} \
else if( occurences == 0 || v > m ) { \
m = v; \
xpos = r->left + x / reg->im->Bands; \
ypos = r->top + y; \
occurences = 1; \
} \
else if( v == m ) { \
xpos += r->left + x / reg->im->Bands; \
ypos += r->top + y; \
occurences += 1; \
} \
} \
\
max = m; \
}
/* complex/dpcomplex loop ... avoid NaN.
*/
#define CLOOP( TYPE ) { \
TYPE *p = (TYPE *) in; \
\
for( x = 0; x < sz; x++ ) { \
double mod, re, im; \
\
re = p[0]; \
im = p[1]; \
p += 2; \
mod = re * re + im * im; \
\
if( isnan( mod ) ) { \
} \
else if( occurences == 0 || mod > max ) { \
max = mod; \
xpos = r->left + x / reg->im->Bands; \
ypos = r->top + y; \
occurences = 1; \
} \
else if( mod == max ) { \
xpos += r->left + x / reg->im->Bands; \
ypos += r->top + y; \
occurences += 1; \
} \
} \
}
/* Loop over region, adding to seq.
*/
static int
maxposavg_scan( REGION *reg, void *seq, void *a, void *b, gboolean *stop )
{
const Rect *r = ®->valid;
const int sz = IM_REGION_N_ELEMENTS( reg );
Maxposavg *maxposavg = (Maxposavg *) seq;
int x, y;
double max;
int xpos, ypos, occurences;
xpos = maxposavg->xpos;
ypos = maxposavg->ypos;
max = maxposavg->max;
occurences = maxposavg->occurences;
for( y = 0; y < r->height; y++ ) {
VipsPel *in = VIPS_REGION_ADDR( reg, r->left, r->top + y );
switch( reg->im->BandFmt ) {
case IM_BANDFMT_UCHAR: ILOOP( unsigned char ); break;
case IM_BANDFMT_CHAR: ILOOP( signed char ); break;
case IM_BANDFMT_USHORT: ILOOP( unsigned short ); break;
case IM_BANDFMT_SHORT: ILOOP( signed short ); break;
case IM_BANDFMT_UINT: ILOOP( unsigned int ); break;
case IM_BANDFMT_INT: ILOOP( signed int ); break;
case IM_BANDFMT_FLOAT: FLOOP( float ); break;
case IM_BANDFMT_DOUBLE: FLOOP( double ); break;
case IM_BANDFMT_COMPLEX: CLOOP( float ); break;
case IM_BANDFMT_DPCOMPLEX: CLOOP( double ); break;
default:
g_assert( 0 );
}
}
maxposavg->xpos = xpos;
maxposavg->ypos = ypos;
maxposavg->max = max;
maxposavg->occurences = occurences;
return( 0 );
}
/**
* im_maxpos_avg:
* @im: image to scan
* @xpos: returned X position
* @ypos: returned Y position
* @out: returned value
*
* Function to find the maximum of an image. Returns coords and value at
* double precision. In the event of a draw, returns average of all
* drawing coords.
*
* See also: im_maxpos(), im_min(), im_stats().
*
* Returns: 0 on success, -1 on error
*/
int
im_maxpos_avg( IMAGE *in, double *xpos, double *ypos, double *out )
{
Maxposavg *global_maxposavg;
if( im_pincheck( in ) ||
im_check_uncoded( "im_maxpos_avg", in ) )
return( -1 );
if( !(global_maxposavg = IM_NEW( in, Maxposavg )) )
return( -1 );
global_maxposavg->occurences = 0;
if( vips_sink( in, maxposavg_start, maxposavg_scan, maxposavg_stop,
in, global_maxposavg ) )
return( -1 );
if( global_maxposavg->occurences == 0 ) {
*xpos = nan("");
*ypos = nan("");
*out = nan("");
}
else {
/* Back to modulus.
*/
if( vips_band_format_iscomplex( in->BandFmt ) )
global_maxposavg->max = sqrt( global_maxposavg->max );
if( xpos )
*xpos = (double) global_maxposavg->xpos /
global_maxposavg->occurences;
if( ypos )
*ypos = (double) global_maxposavg->ypos /
global_maxposavg->occurences;
if( out )
*out = global_maxposavg->max;
}
return( 0 );
}
|