File: edge.c

package info (click to toggle)
vips 8.16.1-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 51,940 kB
  • sloc: ansic: 169,179; cpp: 11,890; python: 4,620; xml: 4,353; sh: 732; perl: 40; makefile: 19
file content (415 lines) | stat: -rw-r--r-- 9,047 bytes parent folder | download | duplicates (2)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* Edge detector
 *
 * 12/4/23
 * 	- from vips_sobel()
 */

/*

	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 _VipsEdge {
	VipsOperation parent_instance;

	VipsImage *in;
	VipsImage *out;
	VipsImage *mask;

	/* Need an image vector for start_many.
	 */
	VipsImage *args[3];
} VipsEdge;

typedef VipsOperationClass VipsEdgeClass;

G_DEFINE_ABSTRACT_TYPE(VipsEdge, vips_edge, VIPS_TYPE_OPERATION);

static void
vips_edge_dispose(GObject *gobject)
{
	VipsEdge *edge = (VipsEdge *) gobject;

	VIPS_UNREF(edge->mask);

	G_OBJECT_CLASS(vips_edge_parent_class)->dispose(gobject);
}

static int
vips_edge_uchar_gen(VipsRegion *out_region,
	void *vseq, void *a, void *b, gboolean *stop)
{
	VipsRegion **in = (VipsRegion **) vseq;
	VipsRect *r = &out_region->valid;
	int sz = r->width * in[0]->im->Bands;

	int x, y;

	if (vips_reorder_prepare_many(out_region->im, in, r))
		return -1;

	for (y = 0; y < r->height; y++) {
		VipsPel *p1 = (VipsPel *restrict)
			VIPS_REGION_ADDR(in[0], r->left, r->top + y);
		VipsPel *p2 = (VipsPel *restrict)
			VIPS_REGION_ADDR(in[1], r->left, r->top + y);
		VipsPel *q = (VipsPel *restrict)
			VIPS_REGION_ADDR(out_region, r->left, r->top + y);

		for (x = 0; x < sz; x++) {
			int v1 = 2 * (p1[x] - 128);
			int v2 = 2 * (p2[x] - 128);
			/* Avoid the sqrt() for uchar.
			 */
			int v = VIPS_ABS(v1) + VIPS_ABS(v2);

			q[x] = v > 255 ? 255 : v;
		}
	}

	return 0;
}

/* Fast uchar path.
 */
static int
vips_edge_build_uchar(VipsEdge *edge)
{
	VipsImage **t = (VipsImage **)
		vips_object_local_array(VIPS_OBJECT(edge), 20);

	g_info("vips_edge: uchar path");

	/* For uchar, use 128 as the zero and divide the result by 2 to
	 * prevent overflow.
	 */
	if (vips_copy(edge->mask, &t[1], NULL))
		return -1;
	vips_image_set_double(t[1], "offset", 128.0);
	vips_image_set_double(t[1], "scale", 2.0);
	if (vips_conv(edge->in, &t[3], t[1],
			"precision", VIPS_PRECISION_INTEGER,
			NULL))
		return -1;

	if (vips_rot90(t[1], &t[5], NULL) ||
		vips_conv(edge->in, &t[7], t[5],
			"precision", VIPS_PRECISION_INTEGER,
			NULL))
		return -1;

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

	edge->args[0] = t[3];
	edge->args[1] = t[7];
	edge->args[2] = NULL;
	if (vips_image_pipeline_array(edge->out,
			VIPS_DEMAND_STYLE_FATSTRIP, edge->args))
		return -1;

	if (vips_image_generate(edge->out,
			vips_start_many, vips_edge_uchar_gen, vips_stop_many,
			edge->args, NULL))
		return -1;

	return 0;
}

/* Accurate but slow path.
 */
static int
vips_edge_build_float(VipsEdge *edge)
{
	VipsImage **t = (VipsImage **)
		vips_object_local_array(VIPS_OBJECT(edge), 20);

	g_info("vips_edge: float path");

	if (vips_rot90(edge->mask, &t[0], NULL) ||
		vips_conv(edge->in, &t[1], edge->mask, NULL) ||
		vips_conv(edge->in, &t[2], t[0], NULL))
		return -1;

	if (vips_multiply(t[1], t[1], &t[3], NULL) ||
		vips_multiply(t[2], t[2], &t[4], NULL) ||
		vips_add(t[3], t[4], &t[5], NULL) ||
		vips_pow_const1(t[5], &t[6], 0.5, NULL) ||
		vips_cast_uchar(t[6], &t[7], NULL))
		return -1;

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

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

	return 0;
}

static int
vips_edge_build(VipsObject *object)
{
	VipsEdge *edge = (VipsEdge *) object;

	if (edge->in->BandFmt == VIPS_FORMAT_UCHAR) {
		if (vips_edge_build_uchar(edge))
			return -1;
	}
	else {
		if (vips_edge_build_float(edge))
			return -1;
	}

	return 0;
}

static void
vips_edge_class_init(VipsEdgeClass *class)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS(class);
	VipsObjectClass *object_class = (VipsObjectClass *) class;

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

	object_class->nickname = "edge";
	object_class->description = _("Edge detector");
	object_class->build = vips_edge_build;

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

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

static void
vips_edge_init(VipsEdge *edge)
{
}

typedef VipsEdge VipsSobel;
typedef VipsEdgeClass VipsSobelClass;

G_DEFINE_TYPE(VipsSobel, vips_sobel, vips_edge_get_type());

static int
vips_sobel_build(VipsObject *object)
{
	VipsEdge *edge = (VipsEdge *) object;

	edge->mask = vips_image_new_matrixv(3, 3,
		1.0, 2.0, 1.0,
		0.0, 0.0, 0.0,
		-1.0, -2.0, -1.0);

	return VIPS_OBJECT_CLASS(vips_sobel_parent_class)->build(object);
}

static void
vips_sobel_class_init(VipsSobelClass *class)
{
	VipsObjectClass *object_class = (VipsObjectClass *) class;

	object_class->nickname = "sobel";
	object_class->description = _("Sobel edge detector");
	object_class->build = vips_sobel_build;
}

static void
vips_sobel_init(VipsEdge *sobel)
{
}

typedef VipsEdge VipsScharr;
typedef VipsEdgeClass VipsScharrClass;

G_DEFINE_TYPE(VipsScharr, vips_scharr, vips_edge_get_type());

static int
vips_scharr_build(VipsObject *object)
{
	VipsEdge *edge = (VipsEdge *) object;

	edge->mask = vips_image_new_matrixv(3, 3,
		-3.0, 0.0, 3.0,
		-10.0, 0.0, 10.0,
		-3.0, 0.0, 3.0);

	return VIPS_OBJECT_CLASS(vips_scharr_parent_class)->build(object);
}

static void
vips_scharr_class_init(VipsSobelClass *class)
{
	VipsObjectClass *object_class = (VipsObjectClass *) class;

	object_class->nickname = "scharr";
	object_class->description = _("Scharr edge detector");
	object_class->build = vips_scharr_build;
}

static void
vips_scharr_init(VipsScharr *scharr)
{
}

typedef VipsEdge VipsPrewitt;
typedef VipsEdgeClass VipsPrewittClass;

G_DEFINE_TYPE(VipsPrewitt, vips_prewitt, vips_edge_get_type());

static int
vips_prewitt_build(VipsObject *object)
{
	VipsEdge *edge = (VipsEdge *) object;

	edge->mask = vips_image_new_matrixv(3, 3,
		-1.0, 0.0, 1.0,
		-1.0, 0.0, 1.0,
		-1.0, 0.0, 1.0);

	return VIPS_OBJECT_CLASS(vips_prewitt_parent_class)->build(object);
}

static void
vips_prewitt_class_init(VipsSobelClass *class)
{
	VipsObjectClass *object_class = (VipsObjectClass *) class;

	object_class->nickname = "prewitt";
	object_class->description = _("Prewitt edge detector");
	object_class->build = vips_prewitt_build;
}

static void
vips_prewitt_init(VipsPrewitt *prewitt)
{
}

/**
 * vips_sobel: (method)
 * @in: input image
 * @out: (out): output image
 * @...: %NULL-terminated list of optional named arguments
 *
 * Sobel edge detector.
 *
 * uchar images are computed using a fast, low-precision path. Cast to float
 * for a high-precision implementation.
 *
 * See also: vips_canny(), vips_sobel(), vips_prewitt(), vips_scharr().
 *
 * Returns: 0 on success, -1 on error.
 */
int
vips_sobel(VipsImage *in, VipsImage **out, ...)
{
	va_list ap;
	int result;

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

	return result;
}

/**
 * vips_scharr: (method)
 * @in: input image
 * @out: (out): output image
 * @...: %NULL-terminated list of optional named arguments
 *
 * Scharr edge detector.
 *
 * uchar images are computed using a fast, low-precision path. Cast to float
 * for a high-precision implementation.
 *
 * See also: vips_canny(), vips_sobel(), vips_prewitt(), vips_scharr().
 *
 * Returns: 0 on success, -1 on error.
 */
int
vips_scharr(VipsImage *in, VipsImage **out, ...)
{
	va_list ap;
	int result;

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

	return result;
}

/**
 * vips_prewitt: (method)
 * @in: input image
 * @out: (out): output image
 * @...: %NULL-terminated list of optional named arguments
 *
 * Prewitt edge detector.
 *
 * uchar images are computed using a fast, low-precision path. Cast to float
 * for a high-precision implementation.
 *
 * See also: vips_canny(), vips_sobel(), vips_prewitt(), vips_scharr().
 *
 * Returns: 0 on success, -1 on error.
 */
int
vips_prewitt(VipsImage *in, VipsImage **out, ...)
{
	va_list ap;
	int result;

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

	return result;
}