1 /* avg ... average value of image
2  *
3  * Copyright: 1990, J. Cupitt
4  *
5  * Author: J. Cupitt
6  * Written on: 02/08/1990
7  * Modified on:
8  * 5/5/93 JC
9  *	- now does partial images
10  *	- less likely to overflow
11  * 1/7/93 JC
12  *	- adapted for partial v2
13  *	- ANSI C
14  * 21/2/95 JC
15  *	- modernised again
16  * 11/5/95 JC
17  * 	- oops! return( NULL ) in im_avg(), instead of return( -1 )
18  * 20/6/95 JC
19  *	- now returns double
20  * 13/1/05
21  *	- use 64 bit arithmetic
22  * 8/12/06
23  * 	- add liboil support
24  * 18/8/09
25  * 	- gtkdoc, minor reformatting
26  * 7/9/09
27  * 	- rewrite for im__wrapiter()
28  * 	- add complex case (needed for im_max())
29  * 8/9/09
30  * 	- wrapscan stuff moved here
31  * 31/7/10
32  * 	- remove liboil
33  * 24/8/11
34  * 	- rewrite as a class
35  * 12/9/14
36  * 	- oops, fix complex avg
37  */
38 
39 /*
40 
41     This file is part of VIPS.
42 
43     VIPS is free software; you can redistribute it and/or modify
44     it under the terms of the GNU Lesser General Public License as published by
45     the Free Software Foundation; either version 2 of the License, or
46     (at your option) any later version.
47 
48     This program is distributed in the hope that it will be useful,
49     but WITHOUT ANY WARRANTY; without even the implied warranty of
50     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
51     GNU Lesser General Public License for more details.
52 
53     You should have received a copy of the GNU Lesser General Public License
54     along with this program; if not, write to the Free Software
55     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
56     02110-1301  USA
57 
58  */
59 
60 /*
61 
62     These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
63 
64  */
65 
66 #ifdef HAVE_CONFIG_H
67 #include <config.h>
68 #endif /*HAVE_CONFIG_H*/
69 #include <vips/intl.h>
70 
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <math.h>
74 
75 #include <vips/vips.h>
76 #include <vips/internal.h>
77 
78 #include "statistic.h"
79 
80 typedef struct _VipsAvg {
81 	VipsStatistic parent_instance;
82 
83 	double sum;
84 	double out;
85 } VipsAvg;
86 
87 typedef VipsStatisticClass VipsAvgClass;
88 
89 G_DEFINE_TYPE( VipsAvg, vips_avg, VIPS_TYPE_STATISTIC );
90 
91 static int
vips_avg_build(VipsObject * object)92 vips_avg_build( VipsObject *object )
93 {
94 	VipsStatistic *statistic = VIPS_STATISTIC( object );
95 	VipsAvg *avg = (VipsAvg *) object;
96 
97 	gint64 vals;
98 	double average;
99 
100 	if( VIPS_OBJECT_CLASS( vips_avg_parent_class )->build( object ) )
101 		return( -1 );
102 
103 	vals = (gint64)
104 		vips_image_get_width( statistic->in ) *
105 		vips_image_get_height( statistic->in ) *
106 		vips_image_get_bands( statistic->in );
107 	average = avg->sum / vals;
108 	g_object_set( object, "out", average, NULL );
109 
110 	return( 0 );
111 }
112 
113 /* Start function: allocate space for a double in which we can accumulate the
114  * sum for this thread.
115  */
116 static void *
vips_avg_start(VipsStatistic * statistic)117 vips_avg_start( VipsStatistic *statistic )
118 {
119 	return( (void *) g_new0( double, 1 ) );
120 }
121 
122 /* Stop function. Add this little sum to the main sum.
123  */
124 static int
vips_avg_stop(VipsStatistic * statistic,void * seq)125 vips_avg_stop( VipsStatistic *statistic, void *seq )
126 {
127 	VipsAvg *avg = (VipsAvg *) statistic;
128 	double *sum = (double *) seq;
129 
130 	avg->sum += *sum;
131 
132 	g_free( seq );
133 
134 	return( 0 );
135 }
136 
137 /* Sum pels in this section.
138  */
139 #define LOOP( TYPE ) { \
140 	TYPE *p = (TYPE *) in; \
141 	\
142 	for( i = 0; i < sz; i++ ) \
143 		m += p[i]; \
144 }
145 
146 #define CLOOP( TYPE ) { \
147 	TYPE *p = (TYPE *) in; \
148 	\
149 	for( i = 0; i < sz; i++ ) { \
150 		double mod = sqrt( p[0] * p[0] + p[1] * p[1] ); \
151 		\
152 		m += mod; \
153 		p += 2; \
154 	} \
155 }
156 
157 /* Loop over region, accumulating a sum in *tmp.
158  */
159 static int
vips_avg_scan(VipsStatistic * statistic,void * seq,int x,int y,void * in,int n)160 vips_avg_scan( VipsStatistic *statistic, void *seq,
161 	int x, int y, void *in, int n )
162 {
163 	const int sz = n * vips_image_get_bands( statistic->in );
164 
165 	double *sum = (double *) seq;
166 
167 	int i;
168 	double m;
169 
170 	m = *sum;
171 
172 	/* Now generate code for all types.
173 	 */
174 	switch( vips_image_get_format( statistic->in ) ) {
175 	case VIPS_FORMAT_UCHAR:		LOOP( unsigned char ); break;
176 	case VIPS_FORMAT_CHAR:		LOOP( signed char ); break;
177 	case VIPS_FORMAT_USHORT:	LOOP( unsigned short ); break;
178 	case VIPS_FORMAT_SHORT:		LOOP( signed short ); break;
179 	case VIPS_FORMAT_UINT:		LOOP( unsigned int ); break;
180 	case VIPS_FORMAT_INT:		LOOP( signed int ); break;
181 	case VIPS_FORMAT_FLOAT:		LOOP( float ); break;
182 	case VIPS_FORMAT_DOUBLE:	LOOP( double ); break;
183 	case VIPS_FORMAT_COMPLEX:	CLOOP( float ); break;
184 	case VIPS_FORMAT_DPCOMPLEX:	CLOOP( double ); break;
185 
186 	default:
187 		g_assert_not_reached();
188 	}
189 
190 	*sum = m;
191 
192 	return( 0 );
193 }
194 
195 static void
vips_avg_class_init(VipsAvgClass * class)196 vips_avg_class_init( VipsAvgClass *class )
197 {
198 	GObjectClass *gobject_class = (GObjectClass *) class;
199 	VipsObjectClass *object_class = (VipsObjectClass *) class;
200 	VipsStatisticClass *sclass = VIPS_STATISTIC_CLASS( class );
201 
202 	gobject_class->set_property = vips_object_set_property;
203 	gobject_class->get_property = vips_object_get_property;
204 
205 	object_class->nickname = "avg";
206 	object_class->description = _( "find image average" );
207 	object_class->build = vips_avg_build;
208 
209 	sclass->start = vips_avg_start;
210 	sclass->scan = vips_avg_scan;
211 	sclass->stop = vips_avg_stop;
212 
213 	VIPS_ARG_DOUBLE( class, "out", 2,
214 		_( "Output" ),
215 		_( "Output value" ),
216 		VIPS_ARGUMENT_REQUIRED_OUTPUT,
217 		G_STRUCT_OFFSET( VipsAvg, out ),
218 		-INFINITY, INFINITY, 0.0 );
219 }
220 
221 static void
vips_avg_init(VipsAvg * avg)222 vips_avg_init( VipsAvg *avg )
223 {
224 }
225 
226 
227 /**
228  * vips_avg: (method)
229  * @in: input #VipsImage
230  * @out: (out): output pixel average
231  * @...: %NULL-terminated list of optional named arguments
232  *
233  * This operation finds the average value in an image. It operates on all
234  * bands of the input image: use vips_stats() if you need to calculate an
235  * average for each band. For complex images, return the average modulus.
236  *
237  * See also: vips_stats(), vips_bandmean(), vips_deviate(), vips_rank()
238  *
239  * Returns: 0 on success, -1 on error
240  */
241 int
vips_avg(VipsImage * in,double * out,...)242 vips_avg( VipsImage *in, double *out, ... )
243 {
244 	va_list ap;
245 	int result;
246 
247 	va_start( ap, out );
248 	result = vips_call_split( "avg", ap, in, out );
249 	va_end( ap );
250 
251 	return( result );
252 }
253