File: merge.c

package info (click to toggle)
vips 8.4.5-1%2Bdeb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 25,724 kB
  • sloc: ansic: 102,605; cpp: 57,527; sh: 4,957; xml: 3,317; python: 3,105; makefile: 1,089; perl: 40
file content (217 lines) | stat: -rw-r--r-- 5,771 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
207
208
209
210
211
212
213
214
215
216
217
/* merge two images left/right or up/down
 *
 * 22/5/14
 * 	- from vips_merge()
 */

/*

    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

 */

/* This is a simple wrapper over the old vips7 functions. At some point we
 * should rewrite this as a pure vips8 class and redo the vips7 functions as
 * wrappers over this.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>

#include <stdio.h>

#include <vips/vips.h>

typedef struct {
	VipsOperation parent_instance;

	VipsImage *ref;
	VipsImage *sec;
	VipsImage *out;
	VipsDirection direction;
	int dx;
	int dy;
	int mblend;

} VipsMerge;

typedef VipsOperationClass VipsMergeClass;

G_DEFINE_TYPE( VipsMerge, vips_merge, VIPS_TYPE_OPERATION );

static int
vips_merge_build( VipsObject *object )
{
	VipsMerge *merge = (VipsMerge *) object;

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

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

	switch( merge->direction ) { 
	case VIPS_DIRECTION_HORIZONTAL:
		if( im_lrmerge( merge->ref, merge->sec, merge->out, 
			merge->dx, merge->dy, merge->mblend ) )
			return( -1 ); 
		break;

	case VIPS_DIRECTION_VERTICAL:
		if( im_tbmerge( merge->ref, merge->sec, merge->out, 
			merge->dx, merge->dy, merge->mblend ) )
			return( -1 ); 
		break;

	default:
		g_assert_not_reached();
	}

	return( 0 );
}

static void
vips_merge_class_init( VipsMergeClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( 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 = "merge";
	object_class->description = _( "merge two images" );
	object_class->build = vips_merge_build;

	VIPS_ARG_IMAGE( class, "ref", 1, 
		_( "Reference" ), 
		_( "Reference image" ),
		VIPS_ARGUMENT_REQUIRED_INPUT, 
		G_STRUCT_OFFSET( VipsMerge, ref ) );

	VIPS_ARG_IMAGE( class, "sec", 2, 
		_( "Secondary" ), 
		_( "Secondary image" ),
		VIPS_ARGUMENT_REQUIRED_INPUT, 
		G_STRUCT_OFFSET( VipsMerge, sec ) );

	VIPS_ARG_IMAGE( class, "out", 3, 
		_( "Output" ), 
		_( "Output image" ),
		VIPS_ARGUMENT_REQUIRED_OUTPUT, 
		G_STRUCT_OFFSET( VipsMerge, out ) );

	VIPS_ARG_ENUM( class, "direction", 4, 
		_( "Direction" ), 
		_( "Horizontal or vertcial merge" ),
		VIPS_ARGUMENT_REQUIRED_INPUT, 
		G_STRUCT_OFFSET( VipsMerge, direction ), 
		VIPS_TYPE_DIRECTION, VIPS_DIRECTION_HORIZONTAL ); 

	VIPS_ARG_INT( class, "dx", 5, 
		_( "dx" ), 
		_( "Horizontal displacement from sec to ref" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsMerge, dx ),
		-100000000, 1000000000, 1 );

	VIPS_ARG_INT( class, "dy", 6, 
		_( "dy" ), 
		_( "Vertical displacement from sec to ref" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsMerge, dy ),
		-100000000, 1000000000, 1 );

	VIPS_ARG_INT( class, "mblend", 7, 
		_( "Max blend" ), 
		_( "Maximum blend size" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsMerge, mblend ),
		0, 10000, 10 );

}

static void
vips_merge_init( VipsMerge *merge )
{
	merge->mblend = 10;
}

/**
 * vips_merge:
 * @ref: reference image
 * @sec: secondary image
 * @out: output image
 * @direction: horizontal or vertical merge
 * @dx: displacement of ref from sec
 * @dy: displacement of ref from sec
 * @...: %NULL-terminated list of optional named arguments
 * 
 * Optional arguments:
 *
 * * @mblend: %gint, maximum blend size 
 *
 * This operation joins two images left-right (with @ref on the left) or
 * up-down (with @ref above) with a smooth seam.
 *
 * If the number of bands differs, one of the images 
 * must have one band. In this case, an n-band image is formed from the 
 * one-band image by joining n copies of the one-band image together, and then
 * the two n-band images are operated upon.
 *
 * The two input images are cast up to the smallest common type (see table 
 * Smallest common format in 
 * <link linkend="libvips-arithmetic">arithmetic</link>).
 *
 * @dx and @dy give the displacement of @sec relative to @ref, in other words,
 * the vector to get from the origin of @sec to the origin of @ref, in other
 * words, @dx will generally be a negative number. 
 *
 * @mblend limits  the  maximum width of the
 * blend area.  A value of "-1" means "unlimited". The two images are blended 
 * with a raised cosine. 
 *
 * Pixels with all bands equal to zero are "transparent", that
 * is, zero pixels in the overlap area do not  contribute  to  the  merge.
 * This makes it possible to join non-rectangular images.
 *
 * See also: vips_mosaic(), vips_insert().
 *
 * Returns: 0 on success, -1 on error
 */
int 
vips_merge( VipsImage *ref, VipsImage *sec, VipsImage **out, 
	VipsDirection direction, int dx, int dy, ... )
{
	va_list ap;
	int result;

	va_start( ap, dy );
	result = vips_call_split( "merge", ap, 
		ref, sec, out, direction, dx, dy );
	va_end( ap );

	return( result );
}