1 /* round.c --- various rounding operations
2 *
3 * 20/6/02 JC
4 * - adapted from im_abs()
5 * 29/8/09
6 * - gtkdoc
7 * - tiny cleanups
8 * 19/9/09
9 * - im_ceil.c adapted to make round.c
10 * 10/11/11
11 * - redone as a class
12 */
13
14 /*
15
16 Copyright (C) 1991-2005 The National Gallery
17
18 This library is free software; you can redistribute it and/or
19 modify it under the terms of the GNU Lesser General Public
20 License as published by the Free Software Foundation; either
21 version 2.1 of the License, or (at your option) any later version.
22
23 This library is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 Lesser General Public License for more details.
27
28 You should have received a copy of the GNU Lesser General Public
29 License along with this library; if not, write to the Free Software
30 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 02110-1301 USA
32
33 */
34
35 /*
36
37 These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
38
39 */
40
41 /*
42 #define DEBUG
43 */
44
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif /*HAVE_CONFIG_H*/
48 #include <vips/intl.h>
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <math.h>
53
54 #include <vips/vips.h>
55
56 #include "unary.h"
57
58 typedef struct _VipsRound {
59 VipsUnary parent_instance;
60
61 VipsOperationRound round;
62
63 } VipsRound;
64
65 typedef VipsUnaryClass VipsRoundClass;
66
67 G_DEFINE_TYPE( VipsRound, vips_round, VIPS_TYPE_UNARY );
68
69 static int
vips_round_build(VipsObject * object)70 vips_round_build( VipsObject *object )
71 {
72 VipsUnary *unary = (VipsUnary *) object;
73
74 /* Is this one of the int types? Degenerate to vips_copy() if it
75 * is.
76 */
77 if( unary->in &&
78 vips_band_format_isint( unary->in->BandFmt ) )
79 return( vips_unary_copy( unary ) );
80
81 if( VIPS_OBJECT_CLASS( vips_round_parent_class )->build( object ) )
82 return( -1 );
83
84 return( 0 );
85 }
86
87 #define LOOP( TYPE, OP ) { \
88 TYPE * restrict p = (TYPE *) in[0]; \
89 TYPE * restrict q = (TYPE *) out; \
90 \
91 for( x = 0; x < sz; x++ ) \
92 q[x] = OP( p[x] ); \
93 }
94
95 #define SWITCH( OP ) { \
96 switch( vips_image_get_format( im ) ) { \
97 case VIPS_FORMAT_COMPLEX: \
98 case VIPS_FORMAT_FLOAT: LOOP( float, OP ); break; \
99 \
100 case VIPS_FORMAT_DPCOMPLEX: \
101 case VIPS_FORMAT_DOUBLE:LOOP( double, OP ); break;\
102 \
103 default: \
104 g_assert_not_reached(); \
105 } \
106 }
107
108 static void
vips_round_buffer(VipsArithmetic * arithmetic,VipsPel * out,VipsPel ** in,int width)109 vips_round_buffer( VipsArithmetic *arithmetic,
110 VipsPel *out, VipsPel **in, int width )
111 {
112 VipsRound *round = (VipsRound *) arithmetic;
113 VipsImage *im = arithmetic->ready[0];
114
115 /* Complex just doubles the size.
116 */
117 const int sz = width * im->Bands *
118 (vips_band_format_iscomplex( im->BandFmt ) ? 2 : 1);
119
120 int x;
121
122 switch( round->round ) {
123 case VIPS_OPERATION_ROUND_RINT: SWITCH( VIPS_RINT ); break;
124 case VIPS_OPERATION_ROUND_CEIL: SWITCH( VIPS_CEIL ); break;
125 case VIPS_OPERATION_ROUND_FLOOR: SWITCH( VIPS_FLOOR ); break;
126
127 default:
128 g_assert_not_reached();
129 }
130 }
131
132 /* Save a bit of typing.
133 */
134 #define UC VIPS_FORMAT_UCHAR
135 #define C VIPS_FORMAT_CHAR
136 #define US VIPS_FORMAT_USHORT
137 #define S VIPS_FORMAT_SHORT
138 #define UI VIPS_FORMAT_UINT
139 #define I VIPS_FORMAT_INT
140 #define F VIPS_FORMAT_FLOAT
141 #define X VIPS_FORMAT_COMPLEX
142 #define D VIPS_FORMAT_DOUBLE
143 #define DX VIPS_FORMAT_DPCOMPLEX
144
145 static const VipsBandFormat vips_round_format_table[10] = {
146 /* UC C US S UI I F X D DX */
147 UC, C, US, S, UI, I, F, X, D, DX
148 };
149
150 static void
vips_round_class_init(VipsRoundClass * class)151 vips_round_class_init( VipsRoundClass *class )
152 {
153 GObjectClass *gobject_class = G_OBJECT_CLASS( class );
154 VipsObjectClass *object_class = (VipsObjectClass *) class;
155 VipsArithmeticClass *aclass = VIPS_ARITHMETIC_CLASS( class );
156
157 gobject_class->set_property = vips_object_set_property;
158 gobject_class->get_property = vips_object_get_property;
159
160 object_class->nickname = "round";
161 object_class->description = _( "perform a round function on an image" );
162 object_class->build = vips_round_build;
163
164 aclass->process_line = vips_round_buffer;
165
166 vips_arithmetic_set_format_table( aclass, vips_round_format_table );
167
168 VIPS_ARG_ENUM( class, "round", 200,
169 _( "Round operation" ),
170 _( "rounding operation to perform" ),
171 VIPS_ARGUMENT_REQUIRED_INPUT,
172 G_STRUCT_OFFSET( VipsRound, round ),
173 VIPS_TYPE_OPERATION_ROUND, VIPS_OPERATION_ROUND_RINT );
174 }
175
176 static void
vips_round_init(VipsRound * round)177 vips_round_init( VipsRound *round )
178 {
179 }
180
181 static int
vips_roundv(VipsImage * in,VipsImage ** out,VipsOperationRound round,va_list ap)182 vips_roundv( VipsImage *in, VipsImage **out,
183 VipsOperationRound round, va_list ap )
184 {
185 return( vips_call_split( "round", ap, in, out, round ) );
186 }
187
188 /**
189 * vips_round: (method)
190 * @in: input #VipsImage
191 * @out: (out): output #VipsImage
192 * @round: #VipsOperationRound rounding operation to perform
193 * @...: %NULL-terminated list of optional named arguments
194 *
195 * Round to an integral value.
196 *
197 * Copy for integer types, round float and
198 * complex types.
199 *
200 * The format of @out is always the same as @in, so you may wish to cast to an
201 * integer format afterwards.
202 *
203 * See also: vips_cast()
204 *
205 * Returns: 0 on success, -1 on error
206 */
207 int
vips_round(VipsImage * in,VipsImage ** out,VipsOperationRound round,...)208 vips_round( VipsImage *in, VipsImage **out, VipsOperationRound round, ... )
209 {
210 va_list ap;
211 int result;
212
213 va_start( ap, round );
214 result = vips_roundv( in, out, round, ap );
215 va_end( ap );
216
217 return( result );
218 }
219
220 /**
221 * vips_floor: (method)
222 * @in: input #VipsImage
223 * @out: (out): output #VipsImage
224 * @...: %NULL-terminated list of optional named arguments
225 *
226 * Round to an integral value with #VIPS_OPERATION_ROUND_FLOOR. See
227 * vips_round().
228 *
229 * Returns: 0 on success, -1 on error
230 */
231 int
vips_floor(VipsImage * in,VipsImage ** out,...)232 vips_floor( VipsImage *in, VipsImage **out, ... )
233 {
234 va_list ap;
235 int result;
236
237 va_start( ap, out );
238 result = vips_roundv( in, out, VIPS_OPERATION_ROUND_FLOOR, ap );
239 va_end( ap );
240
241 return( result );
242 }
243
244 /**
245 * vips_ceil: (method)
246 * @in: input #VipsImage
247 * @out: (out): output #VipsImage
248 * @...: %NULL-terminated list of optional named arguments
249 *
250 * Round to an integral value with #VIPS_OPERATION_ROUND_CEIL. See
251 * vips_round().
252 *
253 * Returns: 0 on success, -1 on error
254 */
255 int
vips_ceil(VipsImage * in,VipsImage ** out,...)256 vips_ceil( VipsImage *in, VipsImage **out, ... )
257 {
258 va_list ap;
259 int result;
260
261 va_start( ap, out );
262 result = vips_roundv( in, out, VIPS_OPERATION_ROUND_CEIL, ap );
263 va_end( ap );
264
265 return( result );
266 }
267
268 /**
269 * vips_rint: (method)
270 * @in: input #VipsImage
271 * @out: (out): output #VipsImage
272 * @...: %NULL-terminated list of optional named arguments
273 *
274 * Round to an integral value with #VIPS_OPERATION_ROUND_RINT. See
275 * vips_round().
276 *
277 * Returns: 0 on success, -1 on error
278 */
279 int
vips_rint(VipsImage * in,VipsImage ** out,...)280 vips_rint( VipsImage *in, VipsImage **out, ... )
281 {
282 va_list ap;
283 int result;
284
285 va_start( ap, out );
286 result = vips_roundv( in, out, VIPS_OPERATION_ROUND_RINT, ap );
287 va_end( ap );
288
289 return( result );
290 }
291