File: similarity.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 (329 lines) | stat: -rw-r--r-- 8,855 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
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
/* simple wrapper over vips_affine() to make scale / rotate easy from the
 * command-line
 *
 * 3/10/13
 * 	- from affine.c
 * 25/10/13
 * 	- oops, reverse rotation direction to match the convention used in the
 * 	  rest of vips
 * 13/8/14
 * 	- oops, missing scale from b, thanks Topochicho
 * 7/2/16
 * 	- use vips_reduce(), if we can
 * 17/11/17
 * `	- add optional "background" param
 * `	- don't use vips_reduce() since it has no "background" param
 * 10/3/18
 * 	- add vips_rotate() class for convenience
 */

/*

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

#include <vips/vips.h>

#include "presample.h"

typedef struct _VipsSimilarityBase {
	VipsResample parent_instance;

	double scale;
	double angle;
	VipsInterpolate *interpolate;
	VipsArrayDouble *background;
	double odx;
	double ody;
	double idx;
	double idy;

} VipsSimilarityBase;

typedef VipsResampleClass VipsSimilarityBaseClass;

G_DEFINE_ABSTRACT_TYPE( VipsSimilarityBase, vips_similarity_base, 
	VIPS_TYPE_RESAMPLE );

static int
vips_similarity_base_build( VipsObject *object )
{
	VipsResample *resample = VIPS_RESAMPLE( object );
	VipsSimilarityBase *base = (VipsSimilarityBase *) object;
	VipsImage **t = (VipsImage **) 
		vips_object_local_array( object, 4 );
	double a = base->scale * cos( VIPS_RAD( base->angle ) ); 
	double b = base->scale * -sin( VIPS_RAD( base->angle ) );
	double c = -b;
	double d = a;

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

	if( vips_affine( resample->in, &t[0], a, b, c, d, 
		"interpolate", base->interpolate,
		"odx", base->odx,
		"ody", base->ody,
		"idx", base->idx,
		"idy", base->idy,
		"background", base->background,
		NULL ) )
		return( -1 );

	if( vips_image_write( t[0], resample->out ) )
		return( -1 ); 

	return( 0 );
}

static void
vips_similarity_base_class_init( VipsSimilarityBaseClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );

	gobject_class->set_property = vips_object_set_property;
	gobject_class->get_property = vips_object_get_property;

	vobject_class->nickname = "similarity_base";
	vobject_class->description = _( "base similarity transform" );
	vobject_class->build = vips_similarity_base_build;

	VIPS_ARG_INTERPOLATE( class, "interpolate", 5, 
		_( "Interpolate" ), 
		_( "Interpolate pixels with this" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT, 
		G_STRUCT_OFFSET( VipsSimilarityBase, interpolate ) );

	VIPS_ARG_BOXED( class, "background", 6, 
		_( "Background" ), 
		_( "Background value" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsSimilarityBase, background ),
		VIPS_TYPE_ARRAY_DOUBLE );

	VIPS_ARG_DOUBLE( class, "odx", 112, 
		_( "Output offset" ), 
		_( "Horizontal output displacement" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsSimilarityBase, odx ),
		-10000000, 10000000, 0 );

	VIPS_ARG_DOUBLE( class, "ody", 113, 
		_( "Output offset" ), 
		_( "Vertical output displacement" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsSimilarityBase, ody ),
		-10000000, 10000000, 0 );

	VIPS_ARG_DOUBLE( class, "idx", 114, 
		_( "Input offset" ), 
		_( "Horizontal input displacement" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsSimilarityBase, idx ),
		-10000000, 10000000, 0 );

	VIPS_ARG_DOUBLE( class, "idy", 115, 
		_( "Input offset" ), 
		_( "Vertical input displacement" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsSimilarityBase, idy ),
		-10000000, 10000000, 0 );

}

static void
vips_similarity_base_init( VipsSimilarityBase *base )
{
	base->scale = 1; 
	base->angle = 0; 
	base->interpolate = NULL; 
	base->odx = 0; 
	base->ody = 0; 
	base->idx = 0; 
	base->idy = 0; 
	base->background = vips_array_double_newv( 1, 0.0 );
}

typedef VipsSimilarityBase VipsSimilarity;
typedef VipsSimilarityBaseClass VipsSimilarityClass;

G_DEFINE_TYPE( VipsSimilarity, vips_similarity, 
	vips_similarity_base_get_type() );

static void
vips_similarity_class_init( VipsSimilarityClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );

	gobject_class->set_property = vips_object_set_property;
	gobject_class->get_property = vips_object_get_property;

	vobject_class->nickname = "similarity";
	vobject_class->description = _( "similarity transform of an image" );

	VIPS_ARG_DOUBLE( class, "scale", 3, 
		_( "Scale" ), 
		_( "Scale by this factor" ), 
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsSimilarity, scale ),
		0, 10000000, 1 );

	VIPS_ARG_DOUBLE( class, "angle", 4, 
		_( "Angle" ), 
		_( "Rotate anticlockwise by this many degrees" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsSimilarity, angle ),
		-10000000, 10000000, 0 );

}

static void
vips_similarity_init( VipsSimilarity *similarity )
{
}

/**
 * vips_similarity: (method)
 * @in: input image
 * @out: (out): output image
 * @...: %NULL-terminated list of optional named arguments
 *
 * Optional arguments:
 *
 * * @scale: %gdouble, scale by this factor
 * * @angle: %gdouble, rotate by this many degrees clockwise
 * * @interpolate: #VipsInterpolate, interpolate pixels with this
 * * @background: #VipsArrayDouble colour for new pixels 
 * * @idx: %gdouble, input horizontal offset
 * * @idy: %gdouble, input vertical offset
 * * @odx: %gdouble, output horizontal offset
 * * @ody: %gdouble, output vertical offset
 * * @ody: %gdouble, output vertical offset
 *
 * This operator calls vips_affine() for you, calculating the matrix for the
 * affine transform from @scale and @angle. Other parameters are passed on to
 * vips_affine() unaltered. 
 *
 * See also: vips_affine(), #VipsInterpolate.
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_similarity( VipsImage *in, VipsImage **out, ... )
{
	va_list ap;
	int result;

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

	return( result );
}

typedef VipsSimilarityBase VipsRotate;
typedef VipsSimilarityBaseClass VipsRotateClass;

G_DEFINE_TYPE( VipsRotate, vips_rotate, vips_similarity_base_get_type() );

static void
vips_rotate_class_init( VipsRotateClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );

	gobject_class->set_property = vips_object_set_property;
	gobject_class->get_property = vips_object_get_property;

	vobject_class->nickname = "rotate";
	vobject_class->description = 
		_( "rotate an image by a number of degrees" );

	VIPS_ARG_DOUBLE( class, "angle", 4, 
		_( "Angle" ), 
		_( "Rotate anticlockwise by this many degrees" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsSimilarity, angle ),
		-10000000, 10000000, 0 );

}

static void
vips_rotate_init( VipsRotate *rotate )
{
}

/**
 * vips_rotate: (method)
 * @in: input image
 * @out: (out): output image
 * @angle: %gdouble, rotate by this many degrees clockwise
 * @...: %NULL-terminated list of optional named arguments
 *
 * Optional arguments:
 *
 * * @interpolate: #VipsInterpolate, interpolate pixels with this
 * * @background: #VipsArrayDouble colour for new pixels 
 * * @idx: %gdouble, input horizontal offset
 * * @idy: %gdouble, input vertical offset
 * * @odx: %gdouble, output horizontal offset
 * * @ody: %gdouble, output vertical offset
 * * @ody: %gdouble, output vertical offset
 *
 * This operator calls vips_affine() for you, calculating the matrix for the
 * affine transform from @scale and @angle. Other parameters are passed on to
 * vips_affine() unaltered. 
 *
 * See also: vips_affine(), #VipsInterpolate.
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_rotate( VipsImage *in, VipsImage **out, double angle, ... )
{
	va_list ap;
	int result;

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

	return( result );
}