File: convsep.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 (197 lines) | stat: -rw-r--r-- 5,140 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
/* convolve twice, rotating the mask
 *
 * 23/10/13	
 * 	- from vips_convsep()
 * 8/5/17
 *      - default to float ... int will often lose precision and should not be
 *        the default
 */

/*

    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 <vips/vips.h>

#include "pconvolution.h"

typedef struct {
	VipsConvolution parent_instance;

	VipsPrecision precision; 
	int layers; 
	int cluster; 
} VipsConvsep;

typedef VipsConvolutionClass VipsConvsepClass;

G_DEFINE_TYPE( VipsConvsep, vips_convsep, VIPS_TYPE_CONVOLUTION );

static int
vips_convsep_build( VipsObject *object )
{
	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
	VipsConvolution *convolution = (VipsConvolution *) object;
	VipsConvsep *convsep = (VipsConvsep *) object;
	VipsImage **t = (VipsImage **) 
		vips_object_local_array( object, 4 );

	VipsImage *in;

	g_object_set( convsep, "out", vips_image_new(), NULL ); 

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

	if( vips_check_separable( class->nickname, convolution->M ) ) 
                return( -1 );

	in = convolution->in;

	if( convsep->precision == VIPS_PRECISION_APPROXIMATE ) {
		if( vips_convasep( in, &t[0], convolution->M,
			"layers", convsep->layers,
			NULL ) )
			return( -1 ); 
		in = t[0];
	}
	else { 
		/* Take a copy, since we must set the offset.
		 */
		if( vips_rot( convolution->M, &t[0], VIPS_ANGLE_D90, NULL ) ||
			vips_copy( t[0], &t[3], NULL ) )
			return( -1 ); 
		vips_image_set_double( t[3], "offset", 0 );

		if( vips_conv( in, &t[1], convolution->M, 
				"precision", convsep->precision,
				"layers", convsep->layers,
				"cluster", convsep->cluster,
				NULL ) ||
			vips_conv( t[1], &t[2], t[3], 
				"precision", convsep->precision,
				"layers", convsep->layers,
				"cluster", convsep->cluster,
				NULL ) )
			return( -1 ); 
		in = t[2];
	}

	if( vips_image_write( in, convolution->out ) )
		return( -1 ); 

	return( 0 );
}

static void
vips_convsep_class_init( VipsConvsepClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *object_class = (VipsObjectClass *) class;

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

	object_class->nickname = "convsep";
	object_class->description = _( "seperable convolution operation" );
	object_class->build = vips_convsep_build;

	VIPS_ARG_ENUM( class, "precision", 203, 
		_( "Precision" ), 
		_( "Convolve with this precision" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT, 
		G_STRUCT_OFFSET( VipsConvsep, precision ), 
		VIPS_TYPE_PRECISION, VIPS_PRECISION_FLOAT ); 

	VIPS_ARG_INT( class, "layers", 204, 
		_( "Layers" ), 
		_( "Use this many layers in approximation" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT, 
		G_STRUCT_OFFSET( VipsConvsep, layers ), 
		1, 1000, 5 ); 

	VIPS_ARG_INT( class, "cluster", 205, 
		_( "Cluster" ), 
		_( "Cluster lines closer than this in approximation" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT, 
		G_STRUCT_OFFSET( VipsConvsep, cluster ), 
		1, 100, 1 ); 

}

static void
vips_convsep_init( VipsConvsep *convsep )
{
	convsep->precision = VIPS_PRECISION_FLOAT;
	convsep->layers = 5;
	convsep->cluster = 1;
}

/**
 * vips_convsep: (method)
 * @in: input image
 * @out: (out): output image
 * @mask: convolution mask
 * @...: %NULL-terminated list of optional named arguments
 *
 * Optional arguments:
 *
 * * @precision: calculation accuracy
 * * @layers: number of layers for approximation
 * * @cluster: cluster lines closer than this distance
 *
 * Perform a separable convolution of @in with @mask.
 * See vips_conv() for a detailed description.
 *
 * The mask must be 1xn or nx1 elements. 
 *
 * The image is convolved twice: once with @mask and then again with @mask 
 * rotated by 90 degrees. This is much faster for certain types of mask
 * (gaussian blur, for example) than doing a full 2D convolution.
 *
 * See also: vips_conv(), vips_gaussmat().
 *
 * Returns: 0 on success, -1 on error
 */
int 
vips_convsep( VipsImage *in, VipsImage **out, VipsImage *mask, ... )
{
	va_list ap;
	int result;

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

	return( result );
}