File: getpoint.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 (240 lines) | stat: -rw-r--r-- 5,993 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
/* read a single getpoint
 *
 * Copyright: J. Cupitt
 * Written: 15/06/1992
 * 22/7/93 JC
 *	- im_incheck() added
 * 16/8/94 JC
 *	- im_incheck() changed to im_makerw()
 * 5/12/06
 * 	- im_invalidate() after paint
 * 6/3/10
 * 	- don't im_invalidate() after paint, this now needs to be at a higher
 * 	  level
 * 29/9/10
 * 	- gtk-doc
 * 	- use Draw base class
 * 	- read_getpoint partial-ised
 * 10/2/14
 * 	- redo as a class
 * 16/12/14
 * 	- free the input region much earlier
 * 14/10/16
 * 	- crop to a memory image rather than using a region ... this means we
 * 	  use workers to calculate pixels and therefore use per-thread caching
 * 	  in the revised buffer system
 * 26/8/24
 *	- add "unpack_complex"
 */

/*

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

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

#include "statistic.h"

typedef struct _VipsGetpoint {
	VipsOperation parent_instance;

	VipsImage *in;
	int x;
	int y;
	gboolean unpack_complex;

	VipsArrayDouble *out_array;
} VipsGetpoint;

typedef VipsOperationClass VipsGetpointClass;

G_DEFINE_TYPE(VipsGetpoint, vips_getpoint, VIPS_TYPE_OPERATION);

static int
vips_getpoint_build(VipsObject *object)
{
	VipsGetpoint *getpoint = (VipsGetpoint *) object;
	VipsImage **t = (VipsImage **) vips_object_local_array(object, 4);

	double *vector;
	VipsArrayDouble *out_array;

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

	/* Unpack to double. Complex unpacks to 2 * bands.
	 */
	gboolean iscomplex = getpoint->unpack_complex &&
		vips_band_format_iscomplex(getpoint->in->BandFmt);
	VipsBandFormat target_bands = iscomplex ?
		getpoint->in->Bands * 2 : getpoint->in->Bands;
	VipsBandFormat target_format = iscomplex ?
		VIPS_FORMAT_DPCOMPLEX : VIPS_FORMAT_DOUBLE;

	/* Crop, decode and unpack to double.
	 */
	if (vips_crop(getpoint->in, &t[0], getpoint->x, getpoint->y, 1, 1, NULL) ||
		vips_image_decode(t[0], &t[1]) ||
		vips_cast(t[1], &t[2], target_format, NULL))
		return -1;

	/* To a mem buffer, then copy to out.
	 */
	vips_image_set_int(t[2], "hide-progress", 1);
	if (!(t[3] = vips_image_new_memory()) ||
		vips_image_write(t[2], t[3]))
		return -1;

	if (!(vector = VIPS_ARRAY(getpoint->in, target_bands, double)))
		return -1;
	memcpy(vector, t[3]->data, VIPS_IMAGE_SIZEOF_PEL(t[3]));

	out_array = vips_array_double_new(vector, target_bands);
	g_object_set(object,
		"out_array", out_array,
		NULL);
	vips_area_unref(VIPS_AREA(out_array));

	return 0;
}

static void
vips_getpoint_class_init(VipsGetpointClass *class)
{
	GObjectClass *gobject_class = (GObjectClass *) 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 = "getpoint";
	object_class->description = _("read a point from an image");
	object_class->build = vips_getpoint_build;

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

	VIPS_ARG_BOXED(class, "out_array", 2,
		_("Output array"),
		_("Array of output values"),
		VIPS_ARGUMENT_REQUIRED_OUTPUT,
		G_STRUCT_OFFSET(VipsGetpoint, out_array),
		VIPS_TYPE_ARRAY_DOUBLE);

	VIPS_ARG_INT(class, "x", 5,
		_("x"),
		_("Point to read"),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET(VipsGetpoint, x),
		0, VIPS_MAX_COORD, 0);

	VIPS_ARG_INT(class, "y", 6,
		_("y"),
		_("Point to read"),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET(VipsGetpoint, y),
		0, VIPS_MAX_COORD, 0);

	VIPS_ARG_BOOL(class, "unpack_complex", 7,
		_("unpack_complex"),
		_("Complex pixels should be unpacked"),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET(VipsGetpoint, unpack_complex),
		FALSE);
}

static void
vips_getpoint_init(VipsGetpoint *getpoint)
{
}

/**
 * vips_getpoint: (method)
 * @in: image to read from
 * @vector: (out)(array length=n): output pixel value here
 * @n: length of output vector
 * @x: position to read
 * @y: position to read
 * @...: %NULL-terminated list of optional named arguments
 *
 * Reads a single pixel on an image.
 *
 * The pixel values are returned in @vector, the length of the
 * array in @n. You must free the array with g_free() when you are done with
 * it.
 *
 * The result array has an element for each band. If @unpack_complex is set,
 * pixels in complex images are returned as double-length arrays.
 *
 * See also: vips_draw_point().
 *
 * Returns: 0 on success, or -1 on error.
 */
int
vips_getpoint(VipsImage *in, double **vector, int *n, int x, int y, ...)
{
	va_list ap;
	VipsArrayDouble *out_array;
	VipsArea *area;
	int result;

	va_start(ap, y);
	result = vips_call_split("getpoint", ap, in, &out_array, x, y);
	va_end(ap);

	if (result)
		return -1;

	area = VIPS_AREA(out_array);
	*vector = VIPS_ARRAY(NULL, area->n, double);
	if (!*vector) {
		vips_area_unref(area);
		return -1;
	}
	memcpy(*vector, area->data, area->n * area->sizeof_type);
	*n = area->n;
	vips_area_unref(area);

	return 0;
}