File: bandbool.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 (352 lines) | stat: -rw-r--r-- 8,396 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
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
/* bandbool.c --- bool op across image bands
 *
 * 7/12/12
 * 	- from boolean.c
 */

/*

    Copyright (C) 1991-2005 The National Gallery

    This library 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.1 of the License, or (at your option) any later version.

    This library 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 library; 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 <vips/intl.h>

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

#include <vips/vips.h>

#include "bandary.h"

typedef struct _VipsBandbool {
	VipsBandary parent_instance;

	VipsImage *in;

	VipsOperationBoolean operation;

} VipsBandbool;

typedef VipsBandaryClass VipsBandboolClass;

G_DEFINE_TYPE( VipsBandbool, vips_bandbool, VIPS_TYPE_BANDARY );

static int
vips_bandbool_build( VipsObject *object )
{
	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
	VipsBandary *bandary = (VipsBandary *) object;
	VipsBandbool *bandbool = (VipsBandbool *) object;

	/* << and >> don't work over bands.
	 */
	if( bandbool->operation == VIPS_OPERATION_BOOLEAN_LSHIFT ||
		bandbool->operation == VIPS_OPERATION_BOOLEAN_RSHIFT ) {
		vips_error( class->nickname, 
			_( "operator %s not supported across image bands" ), 
			vips_enum_nick( VIPS_TYPE_OPERATION_BOOLEAN, 
				bandbool->operation ) );
		return( -1 );
	}

	if( bandbool->in ) {
		if( vips_check_noncomplex( class->nickname, bandbool->in ) )
			return( -1 );

		bandary->n = 1;
		bandary->in = &bandbool->in;

		if( bandbool->in->Bands == 1 ) 
			return( vips_bandary_copy( bandary ) );
	}

	bandary->out_bands = 1;

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

	return( 0 );
}

#define SWITCH( I, F, OP ) \
	switch( vips_image_get_format( im ) ) { \
	case VIPS_FORMAT_UCHAR:		I( unsigned char, OP ); break; \
	case VIPS_FORMAT_CHAR:		I( signed char, OP ); break; \
	case VIPS_FORMAT_USHORT: 	I( unsigned short, OP ); break; \
	case VIPS_FORMAT_SHORT: 	I( signed short, OP ); break; \
	case VIPS_FORMAT_UINT: 		I( unsigned int, OP ); break; \
	case VIPS_FORMAT_INT: 		I( signed int, OP ); break; \
	case VIPS_FORMAT_FLOAT: 	F( float, OP ); break; \
	case VIPS_FORMAT_DOUBLE: 	F( double, OP ); break;\
 	\
	default: \
		g_assert_not_reached(); \
	} 

#define LOOPB( TYPE, OP ) { \
	TYPE *p = (TYPE *) in[0]; \
	TYPE *q = (TYPE *) out; \
 	\
	for( x = 0; x < width; x++ ) { \
		TYPE acc; \
		\
		acc = p[0]; \
		for( b = 1; b < bands; b++ ) \
			acc = acc OP p[b]; \
		\
		q[x] = acc; \
		p += bands; \
	} \
}

#define FLOOPB( TYPE, OP ) { \
	TYPE *p = (TYPE *) in[0]; \
	int *q = (int *) out; \
 	\
	for( x = 0; x < width; x++ ) { \
		int acc; \
		\
		acc = (int) p[0]; \
		for( b = 1; b < bands; b++ ) \
			acc = acc OP ((int) p[b]); \
		\
		q[x] = acc; \
		p += bands; \
	} \
}

static void
vips_bandbool_buffer( VipsBandary *bandary, 
	VipsPel *out, VipsPel **in, int width )
{
	VipsBandbool *bandbool = (VipsBandbool *) bandary;
	VipsImage *im = bandary->ready[0];
	int bands = im->Bands;

	int x, b;

	switch( bandbool->operation ) {
	case VIPS_OPERATION_BOOLEAN_AND: 	
		SWITCH( LOOPB, FLOOPB, & ); 
		break;

	case VIPS_OPERATION_BOOLEAN_OR: 	
		SWITCH( LOOPB, FLOOPB, | ); 
		break;

	case VIPS_OPERATION_BOOLEAN_EOR: 	
		SWITCH( LOOPB, FLOOPB, ^ ); 
		break;

	default:
		g_assert_not_reached();
	}
}

/* Save a bit of typing.
 */
#define UC VIPS_FORMAT_UCHAR
#define C VIPS_FORMAT_CHAR
#define US VIPS_FORMAT_USHORT
#define S VIPS_FORMAT_SHORT
#define UI VIPS_FORMAT_UINT
#define I VIPS_FORMAT_INT
#define F VIPS_FORMAT_FLOAT
#define X VIPS_FORMAT_COMPLEX
#define D VIPS_FORMAT_DOUBLE
#define DX VIPS_FORMAT_DPCOMPLEX

/* Format conversions for boolean. 
 */
static const VipsBandFormat vips_bandbool_format_table[10] = {
/* UC  C   US  S   UI  I   F   X   D   DX */
   UC, C,  US, S,  UI, I,  I,  I,  I,  I,
};

static void
vips_bandbool_class_init( VipsBandboolClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *object_class = (VipsObjectClass *) class;
	VipsBandaryClass *bandary_class = VIPS_BANDARY_CLASS( class );

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

	object_class->nickname = "bandbool";
	object_class->description = _( "boolean operation across image bands" );
	object_class->build = vips_bandbool_build;

	bandary_class->process_line = vips_bandbool_buffer;
	bandary_class->format_table = vips_bandbool_format_table;

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

	VIPS_ARG_ENUM( class, "boolean", 200, 
		_( "Operation" ), 
		_( "boolean to perform" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsBandbool, operation ),
		VIPS_TYPE_OPERATION_BOOLEAN, 
			VIPS_OPERATION_BOOLEAN_AND ); 
}

static void
vips_bandbool_init( VipsBandbool *bandbool )
{
	bandbool->operation = VIPS_OPERATION_BOOLEAN_AND;
}

static int
vips_bandboolv( VipsImage *in, VipsImage **out, 
	VipsOperationBoolean operation, va_list ap )
{
	return( vips_call_split( "bandbool", ap, in, out, operation ) );
}

/**
 * vips_bandbool:
 * @in: left-hand input #VipsImage
 * @out: output #VipsImage
 * @boolean: boolean operation to perform
 * @...: %NULL-terminated list of optional named arguments
 *
 * Perform various boolean operations across the bands of an image. For
 * example, a three-band uchar image operated on with
 * #VIPS_OPERATION_BOOLEAN_AND will produce a one-band uchar image where each
 * pixel is the bitwise and of the band elements of the corresponding pixel in
 * the input image. 
 *
 * The output image is the same format as the input image for integer
 * types. Float types are cast to int before processing. Complex types are not
 * supported.
 *
 * The output image always has one band. 
 *
 * This operation is useful in conjuction with vips_relational(). You can use
 * it to see if all image bands match exactly. 
 *
 * See also: vips_boolean_const().
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_bandbool( VipsImage *in, VipsImage **out, 
	VipsOperationBoolean boolean, ... )
{
	va_list ap;
	int result;

	va_start( ap, boolean );
	result = vips_bandboolv( in, out, boolean, ap );
	va_end( ap );

	return( result );
}

/**
 * vips_bandand:
 * @in: left-hand input #VipsImage
 * @out: output #VipsImage
 * @...: %NULL-terminated list of optional named arguments
 *
 * Perform #VIPS_OPERATION_BOOLEAN_AND on an image. See
 * vips_bandbool().
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_bandand( VipsImage *in, VipsImage **out, ... )
{
	va_list ap;
	int result;

	va_start( ap, out );
	result = vips_bandboolv( in, out, VIPS_OPERATION_BOOLEAN_AND, ap );
	va_end( ap );

	return( result );
}

/**
 * vips_bandor:
 * @in: left-hand input #VipsImage
 * @out: output #VipsImage
 * @...: %NULL-terminated list of optional named arguments
 *
 * Perform #VIPS_OPERATION_BOOLEAN_OR on an image. See
 * vips_bandbool().
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_bandor( VipsImage *in, VipsImage **out, ... )
{
	va_list ap;
	int result;

	va_start( ap, out );
	result = vips_bandboolv( in, out, VIPS_OPERATION_BOOLEAN_OR, ap );
	va_end( ap );

	return( result );
}

/**
 * vips_bandeor:
 * @in: left-hand input #VipsImage
 * @out: output #VipsImage
 * @...: %NULL-terminated list of optional named arguments
 *
 * Perform #VIPS_OPERATION_BOOLEAN_EOR on an image. See
 * vips_bandbool().
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_bandeor( VipsImage *in, VipsImage **out, ... )
{
	va_list ap;
	int result;

	va_start( ap, out );
	result = vips_bandboolv( in, out, VIPS_OPERATION_BOOLEAN_EOR, ap );
	va_end( ap );

	return( result );
}