File: gaussblur.c

package info (click to toggle)
vips 8.17.3-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 52,228 kB
  • sloc: ansic: 169,684; cpp: 12,156; python: 4,887; sh: 733; perl: 40; makefile: 25; javascript: 6
file content (206 lines) | stat: -rw-r--r-- 5,017 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
/* Gaussian blur.
 *
 * 15/11/13
 * 	- from vips_sharpen()
 * 19/11/14
 * 	- change parameters to be more imagemagick-like
 * 21/9/20
 * 	- allow sigma zero, meaning no blur
 * 	- sigma < 0.2 is just copy
 */

/*

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

#include <vips/vips.h>

typedef struct _VipsGaussblur {
	VipsOperation parent_instance;

	VipsImage *in;
	VipsImage *out;

	gdouble sigma;
	gdouble min_ampl;
	VipsPrecision precision;

} VipsGaussblur;

typedef VipsOperationClass VipsGaussblurClass;

G_DEFINE_TYPE(VipsGaussblur, vips_gaussblur, VIPS_TYPE_OPERATION);

static int
vips_gaussblur_build(VipsObject *object)
{
	VipsGaussblur *gaussblur = (VipsGaussblur *) object;
	VipsImage **t = (VipsImage **) vips_object_local_array(object, 2);

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

	/* vips_gaussmat() will make a 1x1 pixel mask for anything smaller than
	 * this.
	 */
	if (gaussblur->sigma < 0.2) {
		if (vips_copy(gaussblur->in, &t[1], NULL))
			return -1;
	}
	else {
		if (vips_gaussmat(&t[0],
				gaussblur->sigma, gaussblur->min_ampl,
				"separable", TRUE,
				"precision", gaussblur->precision,
				NULL))
			return -1;

#ifdef DEBUG
		printf("gaussblur: blurring with:\n");
		vips_matrixprint(t[0], NULL);
#endif /*DEBUG*/

		g_info("gaussblur mask width %d", t[0]->Xsize);

		if (vips_convsep(gaussblur->in, &t[1], t[0],
				"precision", gaussblur->precision,
				NULL))
			return -1;
	}

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

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

	return 0;
}

static void
vips_gaussblur_class_init(VipsGaussblurClass *class)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS(class);
	VipsObjectClass *object_class = (VipsObjectClass *) class;
	VipsOperationClass *operation_class = VIPS_OPERATION_CLASS(class);

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

	object_class->nickname = "gaussblur";
	object_class->description = _("gaussian blur");
	object_class->build = vips_gaussblur_build;

	operation_class->flags = VIPS_OPERATION_SEQUENTIAL;

	VIPS_ARG_IMAGE(class, "in", 1,
		_("Input"),
		_("Input image"),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET(VipsGaussblur, in));

	VIPS_ARG_IMAGE(class, "out", 2,
		_("Output"),
		_("Output image"),
		VIPS_ARGUMENT_REQUIRED_OUTPUT,
		G_STRUCT_OFFSET(VipsGaussblur, out));

	VIPS_ARG_DOUBLE(class, "sigma", 3,
		_("Sigma"),
		_("Sigma of Gaussian"),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET(VipsGaussblur, sigma),
		0.0, 1000, 1.5);

	VIPS_ARG_DOUBLE(class, "min_ampl", 3,
		_("Minimum amplitude"),
		_("Minimum amplitude of Gaussian"),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET(VipsGaussblur, min_ampl),
		0.001, 1.0, 0.2);

	VIPS_ARG_ENUM(class, "precision", 4,
		_("Precision"),
		_("Convolve with this precision"),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET(VipsGaussblur, precision),
		VIPS_TYPE_PRECISION, VIPS_PRECISION_INTEGER);
}

static void
vips_gaussblur_init(VipsGaussblur *gaussblur)
{
	gaussblur->sigma = 1.5;
	gaussblur->min_ampl = 0.2;
	gaussblur->precision = VIPS_PRECISION_INTEGER;
}

/**
 * vips_gaussblur: (method)
 * @in: input image
 * @out: (out): output image
 * @sigma: how large a mask to use
 * @...: `NULL`-terminated list of optional named arguments
 *
 * This operator runs [ctor@Image.gaussmat] and [method@Image.convsep] for
 * you on an image.
 *
 * Set @min_ampl smaller to generate a larger, more accurate mask. Set @sigma
 * larger to make the blur more blurry.
 *
 * ::: tip "Optional arguments"
 *     * @precision: [enum@Precision], precision for blur, default int
 *     * @min_ampl: `gdouble`, minimum amplitude, default 0.2
 *
 * ::: seealso
 *     [ctor@Image.gaussmat], [method@Image.convsep].
 *
 * Returns: 0 on success, -1 on error.
 */
int
vips_gaussblur(VipsImage *in, VipsImage **out, double sigma, ...)
{
	va_list ap;
	int result;

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

	return result;
}