File: avg.c

package info (click to toggle)
vips 8.14.1-3%2Bdeb12u2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 35,912 kB
  • sloc: ansic: 165,449; cpp: 10,987; python: 4,462; xml: 4,212; sh: 471; perl: 40; makefile: 23
file content (252 lines) | stat: -rw-r--r-- 5,791 bytes parent folder | download
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
/* avg ... average value of image
 *
 * Copyright: 1990, J. Cupitt
 *
 * Author: J. Cupitt
 * Written on: 02/08/1990
 * Modified on: 
 * 5/5/93 JC
 *	- now does partial images
 *	- less likely to overflow
 * 1/7/93 JC
 *	- adapted for partial v2
 *	- ANSI C
 * 21/2/95 JC
 *	- modernised again
 * 11/5/95 JC
 * 	- oops! return( NULL ) in im_avg(), instead of return( -1 )
 * 20/6/95 JC
 *	- now returns double
 * 13/1/05
 *	- use 64 bit arithmetic 
 * 8/12/06
 * 	- add liboil support
 * 18/8/09
 * 	- gtkdoc, minor reformatting
 * 7/9/09
 * 	- rewrite for im__wrapiter()
 * 	- add complex case (needed for im_max())
 * 8/9/09
 * 	- wrapscan stuff moved here
 * 31/7/10
 * 	- remove liboil
 * 24/8/11
 * 	- rewrite as a class
 * 12/9/14
 * 	- oops, fix complex avg
 */

/*

    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 <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <vips/vips.h>
#include <vips/internal.h>

#include "statistic.h"

typedef struct _VipsAvg {
	VipsStatistic parent_instance;

	double sum;
	double out;
} VipsAvg;

typedef VipsStatisticClass VipsAvgClass;

G_DEFINE_TYPE( VipsAvg, vips_avg, VIPS_TYPE_STATISTIC );

static int
vips_avg_build( VipsObject *object )
{
	VipsStatistic *statistic = VIPS_STATISTIC( object ); 
	VipsAvg *avg = (VipsAvg *) object;

	gint64 vals;
	double average;

	if( VIPS_OBJECT_CLASS( vips_avg_parent_class )->build( object ) )
		return( -1 );

	vals = (gint64) 
		vips_image_get_width( statistic->in ) * 
		vips_image_get_height( statistic->in ) * 
		vips_image_get_bands( statistic->in );
	average = avg->sum / vals;
	g_object_set( object, "out", average, NULL );

	return( 0 );
}

/* Start function: allocate space for a double in which we can accumulate the
 * sum for this thread.
 */
static void *
vips_avg_start( VipsStatistic *statistic )
{
	return( (void *) g_new0( double, 1 ) );
}

/* Stop function. Add this little sum to the main sum.
 */
static int
vips_avg_stop( VipsStatistic *statistic, void *seq )
{
	VipsAvg *avg = (VipsAvg *) statistic;
	double *sum = (double *) seq;

	avg->sum += *sum;

	g_free( seq );

	return( 0 );
}

/* Sum pels in this section.
 */
#define LOOP( TYPE ) { \
	TYPE *p = (TYPE *) in; \
	\
	for( i = 0; i < sz; i++ ) \
		m += p[i]; \
}

#define CLOOP( TYPE ) { \
	TYPE *p = (TYPE *) in; \
	\
	for( i = 0; i < sz; i++ ) { \
		double mod = sqrt( p[0] * p[0] + p[1] * p[1] ); \
		\
		m += mod; \
		p += 2; \
	} \
} 

/* Loop over region, accumulating a sum in *tmp.
 */
static int
vips_avg_scan( VipsStatistic *statistic, void *seq, 
	int x, int y, void *in, int n )
{
	const int sz = n * vips_image_get_bands( statistic->in );

	double *sum = (double *) seq;

	int i;
	double m;

	m = *sum;

	/* Now generate code for all types. 
	 */
	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; 
	case VIPS_FORMAT_COMPLEX:	CLOOP( float ); break; 
	case VIPS_FORMAT_DPCOMPLEX:	CLOOP( double ); break; 

	default: 
		g_assert_not_reached();
	}

	*sum = m;

	return( 0 );
}

static void
vips_avg_class_init( VipsAvgClass *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 = "avg";
	object_class->description = _( "find image average" );
	object_class->build = vips_avg_build;

	sclass->start = vips_avg_start;
	sclass->scan = vips_avg_scan;
	sclass->stop = vips_avg_stop;

	VIPS_ARG_DOUBLE( class, "out", 2, 
		_( "Output" ), 
		_( "Output value" ),
		VIPS_ARGUMENT_REQUIRED_OUTPUT,
		G_STRUCT_OFFSET( VipsAvg, out ),
		-INFINITY, INFINITY, 0.0 );
}

static void
vips_avg_init( VipsAvg *avg )
{
}


/**
 * vips_avg: (method)
 * @in: input #VipsImage
 * @out: (out): output pixel average
 * @...: %NULL-terminated list of optional named arguments
 *
 * This operation finds the average value in an image. It operates on all 
 * bands of the input image: use vips_stats() if you need to calculate an 
 * average for each band. For complex images, return the average modulus.
 *
 * See also: vips_stats(), vips_bandmean(), vips_deviate(), vips_rank()
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_avg( VipsImage *in, double *out, ... )
{
	va_list ap;
	int result;

	va_start( ap, out );
	result = vips_call_split( "avg", ap, in, out );
	va_end( ap );

	return( result );
}