File: correlation.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 (171 lines) | stat: -rw-r--r-- 4,719 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
/* base class for correlation
 *
 * 7/11/13
 * 	- from convolution.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

 */

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

#include <stdio.h>
#include <math.h>

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

#include "pconvolution.h"
#include "correlation.h"

G_DEFINE_ABSTRACT_TYPE(VipsCorrelation, vips_correlation,
	VIPS_TYPE_OPERATION);

static int
vips_correlation_gen(VipsRegion *out_region,
	void *seq, void *a, void *b, gboolean *stop)
{
	VipsRegion *ir = (VipsRegion *) seq;
	VipsCorrelation *correlation = (VipsCorrelation *) b;
	VipsCorrelationClass *cclass =
		VIPS_CORRELATION_GET_CLASS(correlation);
	VipsRect *r = &out_region->valid;

	VipsRect irect;

	/* What part of ir do we need?
	 */
	irect.left = r->left;
	irect.top = r->top;
	irect.width = r->width + correlation->ref_ready->Xsize - 1;
	irect.height = r->height + correlation->ref_ready->Ysize - 1;

	if (vips_region_prepare(ir, &irect))
		return -1;

	cclass->correlation(correlation, ir, out_region);

	return 0;
}

static int
vips_correlation_build(VipsObject *object)
{
	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS(object);
	VipsCorrelationClass *cclass = VIPS_CORRELATION_CLASS(class);
	VipsCorrelation *correlation = (VipsCorrelation *) object;
	VipsImage **t = (VipsImage **) vips_object_local_array(object, 6);

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

	/* Stretch input out.
	 */
	if (vips_embed(correlation->in, &t[0],
			correlation->ref->Xsize / 2, correlation->ref->Ysize / 2,
			correlation->in->Xsize + correlation->ref->Xsize - 1,
			correlation->in->Ysize + correlation->ref->Ysize - 1,
			"extend", VIPS_EXTEND_COPY,
			NULL))
		return -1;
	if (vips__formatalike(t[0], correlation->ref, &t[1], &t[2]) ||
		vips__bandalike(class->nickname, t[1], t[2], &t[3], &t[4]) ||
		!(t[5] = vips_image_copy_memory(t[4])))
		return -1;

	correlation->in_ready = t[3];
	correlation->ref_ready = t[5];

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

	/* FATSTRIP is good for us as THINSTRIP will cause
	 * too many recalculations on overlaps.
	 */
	if (vips_image_pipelinev(correlation->out,
			VIPS_DEMAND_STYLE_FATSTRIP,
			correlation->in_ready, correlation->ref_ready, NULL))
		return -1;
	correlation->out->Xsize = correlation->in->Xsize;
	correlation->out->Ysize = correlation->in->Ysize;
	correlation->out->BandFmt =
		cclass->format_table[correlation->in_ready->BandFmt];
	if (cclass->pre_generate &&
		cclass->pre_generate(correlation))
		return -1;
	if (vips_image_generate(correlation->out,
			vips_start_one, vips_correlation_gen, vips_stop_one,
			correlation->in_ready, correlation))
		return -1;

	vips_reorder_margin_hint(correlation->out,
		correlation->ref->Xsize * correlation->ref->Ysize);

	return 0;
}

static void
vips_correlation_class_init(VipsCorrelationClass *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 = "correlation";
	object_class->description = _("correlation operation");
	object_class->build = vips_correlation_build;

	operation_class->flags = VIPS_OPERATION_SEQUENTIAL;

	VIPS_ARG_IMAGE(class, "in", 0,
		_("Input"),
		_("Input image argument"),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET(VipsCorrelation, in));

	VIPS_ARG_IMAGE(class, "ref", 10,
		_("Mask"),
		_("Input reference image"),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET(VipsCorrelation, ref));

	VIPS_ARG_IMAGE(class, "out", 20,
		_("Output"),
		_("Output image"),
		VIPS_ARGUMENT_REQUIRED_OUTPUT,
		G_STRUCT_OFFSET(VipsCorrelation, out));
}

static void
vips_correlation_init(VipsCorrelation *correlation)
{
}