File: sdf.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 (405 lines) | stat: -rw-r--r-- 9,579 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
/* make various signed distance fields
 *
 * 16/6/24
 * 	- from xyz.c
 */

/*

	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 VIPS_DEBUG
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/debug.h>

#include "pcreate.h"

typedef struct _VipsSdf VipsSdf;

typedef float (*PointFn)(VipsSdf *, int x, int y);

struct _VipsSdf {
	VipsCreate parent_instance;

	int width;
	int height;
	VipsSdfShape shape;

	double *a;					// two vec2
	double *b;
	double r;
	double *corners;			// corner radii

	float cx;					// centre
	float cy;
	float dx;					// difference
	float dy;
	float sx;					// half size
	float sy;

	VipsArea *corners_area;
	VipsArea *a_area;
	VipsArea *b_area;

	PointFn point;
};

typedef VipsCreateClass VipsSdfClass;

G_DEFINE_TYPE(VipsSdf, vips_sdf, VIPS_TYPE_CREATE);

/* SDF functions derived from
 *
 * https://iquilezles.org/articles/distfunctions2d/
 */

static float
vips_sdf_circle(VipsSdf *sdf, int x, int y)
{
	return hypot(x - sdf->a[0], y - sdf->a[1]) - sdf->r;
}

static float
vips_sdf_box(VipsSdf *sdf, int x, int y)
{
	float px = x - sdf->cx;
	float py = y - sdf->cy;

	float dx = fabs(px) - sdf->sx;
	float dy = fabs(py) - sdf->sy;

	return hypot(VIPS_MAX(dx, 0), VIPS_MAX(dy, 0)) +
		VIPS_MIN(VIPS_MAX(dx, dy), 0);
}

static float
vips_sdf_rounded_box(VipsSdf *sdf, int x, int y)
{
	float px = x - sdf->cx;
	float py = y - sdf->cy;

	// radius of nearest corner
	float r_top = px > 0 ? sdf->corners[0] : sdf->corners[2];
	float r_bottom = px > 0 ? sdf->corners[1] : sdf->corners[3];
	float r = py > 0 ? r_top : r_bottom;

	float qx = fabs(px) - sdf->sx + r;
	float qy = fabs(py) - sdf->sy + r;

	return hypot(VIPS_MAX(qx, 0), VIPS_MAX(qy, 0)) +
		VIPS_MIN(VIPS_MAX(qx, qy), 0) - r;
}

static float
vips_sdf_line(VipsSdf *sdf, int px, int py)
{
	float pax = px - sdf->a[0];
	float pay = py - sdf->a[1];

	float dot_paba = pax * sdf->dx + pay * sdf->dy;
	float dot_baba = sdf->dx * sdf->dx + sdf->dy * sdf->dy;
	float h = VIPS_CLIP(0, dot_paba / dot_baba, 1);

	float dx = pax - h * sdf->dx;
	float dy = pay - h * sdf->dy;

	return hypot(dx, dy);
}

static int
vips_sdf_gen(VipsRegion *out_region,
	void *seq, void *a, void *b, gboolean *stop)
{
	VipsSdf *sdf = (VipsSdf *) a;
	VipsRect *r = &out_region->valid;

	for (int y = 0; y < r->height; y++) {
		int ay = y + r->top;
		float *q = (float *) VIPS_REGION_ADDR(out_region, r->left, ay);

		for (int x = 0; x < r->width; x++)
			q[x] = sdf->point(sdf, x + r->left, ay);
	}

	return 0;
}

static int
vips_sdf_build(VipsObject *object)
{
	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS(object);
	VipsCreate *create = VIPS_CREATE(object);
	VipsSdf *sdf = (VipsSdf *) object;

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

	switch (sdf->shape) {
	case VIPS_SDF_SHAPE_CIRCLE:
		if (!vips_object_argument_isset(object, "a") ||
			!vips_object_argument_isset(object, "r")) {
			vips_error(class->nickname, "%s",
				_("circle needs a, r to be set"));
			return -1;
		}
		if (sdf->a_area->n != 2) {
			vips_error(class->nickname, "%s",
				_("rounded-box needs 2 values for a"));
			return -1;
		}

		sdf->a = (double *) sdf->a_area->data;
		sdf->point = vips_sdf_circle;

		break;

	case VIPS_SDF_SHAPE_BOX:
		if (!vips_object_argument_isset(object, "a") ||
			!vips_object_argument_isset(object, "b")) {
			vips_error(class->nickname, "%s",
				_("box needs a, b to be set"));
			return -1;
		}
		if (sdf->a_area->n != 2 ||
			sdf->b_area->n != 2) {
			vips_error(class->nickname, "%s",
				_("box needs 2 values for a, b"));
			return -1;
		}

		sdf->a = (double *) sdf->a_area->data;
		sdf->b = (double *) sdf->b_area->data;
		sdf->point = vips_sdf_box;

		break;

	case VIPS_SDF_SHAPE_ROUNDED_BOX:
		if (!vips_object_argument_isset(object, "a") ||
			!vips_object_argument_isset(object, "b")) {
			vips_error(class->nickname, "%s",
				_("rounded-box needs a, b to be set"));
			return -1;
		}
		if (sdf->a_area->n != 2 ||
			sdf->b_area->n != 2) {
			vips_error(class->nickname, "%s",
				_("rounded-box needs 2 values for a, b"));
			return -1;
		}
		if (sdf->corners_area->n != 4) {
			vips_error(class->nickname, "%s",
				_("rounded-box needs 4 values for corners"));
			return -1;
		}

		sdf->a = (double *) sdf->a_area->data;
		sdf->b = (double *) sdf->b_area->data;
		sdf->corners = (double *) sdf->corners_area->data;
		sdf->point = vips_sdf_rounded_box;

		break;

	case VIPS_SDF_SHAPE_LINE:
		if (!vips_object_argument_isset(object, "a") ||
			!vips_object_argument_isset(object, "b")) {
			vips_error(class->nickname, "%s",
				_("line needs sx, sy to be set"));
			return -1;
		}
		if (sdf->a_area->n != 2 ||
			sdf->b_area->n != 2) {
			vips_error(class->nickname, "%s",
				_("line needs 2 values for a, b"));
			return -1;
		}

		sdf->a = (double *) sdf->a_area->data;
		sdf->b = (double *) sdf->b_area->data;
		sdf->point = vips_sdf_line;

		break;

	default:
		vips_error(class->nickname, _("unknown SDF %d"), sdf->shape);
		return -1;
	}

	if (sdf->a &&
		sdf->b) {
		// centre
		sdf->cx = (sdf->a[0] + sdf->b[0]) / 2.0;
		sdf->cy = (sdf->a[1] + sdf->b[1]) / 2.0;

		// difference
		sdf->dx = sdf->b[0] - sdf->a[0];
		sdf->dy = sdf->b[1] - sdf->a[1];

		// half size
		sdf->sx = sdf->dx / 2.0;
		sdf->sy = sdf->dy / 2.0;
	}

	vips_image_init_fields(create->out,
		sdf->width, sdf->height, 1,
		VIPS_FORMAT_FLOAT, VIPS_CODING_NONE, VIPS_INTERPRETATION_B_W, 1.0, 1.0);
	if (vips_image_pipelinev(create->out, VIPS_DEMAND_STYLE_ANY, NULL) ||
		vips_image_generate(create->out, NULL, vips_sdf_gen, NULL, sdf, NULL))
		return -1;

	return 0;
}

static void
vips_sdf_class_init(VipsSdfClass *class)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS(class);
	VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS(class);

	VIPS_DEBUG_MSG("vips_sdf_class_init\n");

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

	vobject_class->nickname = "sdf";
	vobject_class->description = _("create an SDF image");
	vobject_class->build = vips_sdf_build;

	VIPS_ARG_INT(class, "width", 2,
		_("Width"),
		_("Image width in pixels"),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET(VipsSdf, width),
		1, VIPS_MAX_COORD, 1);

	VIPS_ARG_INT(class, "height", 3,
		_("Height"),
		_("Image height in pixels"),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET(VipsSdf, height),
		1, VIPS_MAX_COORD, 1);

	VIPS_ARG_ENUM(class, "shape", 8,
		_("Shape"),
		_("SDF shape to create"),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET(VipsSdf, shape),
		VIPS_TYPE_SDF_SHAPE, VIPS_SDF_SHAPE_CIRCLE);

	VIPS_ARG_DOUBLE(class, "r", 9,
		_("r"),
		_("Radius"),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET(VipsSdf, r),
		0.0, VIPS_MAX_COORD, 50);

	VIPS_ARG_BOXED(class, "a", 13,
		_("a"),
		_("Point a"),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET(VipsSdf, a_area),
		VIPS_TYPE_ARRAY_DOUBLE);

	VIPS_ARG_BOXED(class, "b", 14,
		_("b"),
		_("Point b"),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET(VipsSdf, b_area),
		VIPS_TYPE_ARRAY_DOUBLE);

	VIPS_ARG_BOXED(class, "corners", 15,
		_("corners"),
		_("Corner radii"),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET(VipsSdf, corners_area),
		VIPS_TYPE_ARRAY_DOUBLE);

}

static void
vips_sdf_init(VipsSdf *sdf)
{
	sdf->corners_area = vips_area_new_array(G_TYPE_DOUBLE, sizeof(double), 4);
}

/**
 * vips_sdf:
 * @out: (out): output image
 * @width: horizontal size
 * @height: vertical size
 * @shape: SDF to create
 * @...: %NULL-terminated list of optional named arguments
 *
 * Optional arguments:
 *
 * * @a: #VipsArrayDouble, first point
 * * @b: #VipsArrayDouble, second point
 * * @r: %gfloat, radius
 * * @corners: #VipsArrayDouble, corner radii
 *
 * Create a signed distance field (SDF) image of the given shape. Different
 * shapes use different combinations of the optional arguments, see below.
 *
 * @shape #VIPS_SDF_SHAPE_CIRCLE: create a circle centred on @a, radius @r.
 *
 * @shape #VIPS_SDF_SHAPE_BOX: create a box with top-left corner @a and
 * bottom-right corner @b.
 *
 * @shape #VIPS_SDF_SHAPE_ROUNDED_BOX: create a box with top-left corner @a
 * and bottom-right corner @b, whose four corners are
 * rounded by the four-element float array @corners. @corners will default to
 * 0.0.
 *
 * @shape #VIPS_SDF_SHAPE_LINE: draw a line from @a to @b.
 *
 * See also: vips_grey(), vips_grid(), vips_xyz().
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_sdf(VipsImage **out, int width, int height, VipsSdfShape shape, ...)
{
	va_list ap;
	int result;

	va_start(ap, shape);
	result = vips_call_split("sdf", ap, out, width, height, shape);
	va_end(ap);

	return result;
}