File: recomb.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 (255 lines) | stat: -rw-r--r-- 6,420 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
/* recomb.c ... pass an image though a matrix *
 * 21/6/95 JC
 *	- mildly modernised
 * 14/3/96 JC
 *	- better error checks, partial
 * 4/11/09
 * 	- gtkdoc
 * 9/11/11
 * 	- redo as a class
 */

/*

    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 <math.h>

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

#include "pconversion.h"

typedef struct _VipsRecomb {
	VipsConversion parent_instance;

	VipsImage *in;
	VipsImage *m;

	/* Our input matrix as a one-band double. 
	 */
	VipsImage *coeff;

} VipsRecomb;

typedef VipsConversionClass VipsRecombClass;

G_DEFINE_TYPE( VipsRecomb, vips_recomb, VIPS_TYPE_CONVERSION );

/* Inner loop.
 */
#define LOOP( IN, OUT ) { \
	IN * restrict p = (IN *) in; \
	OUT * restrict q = (OUT *) out; \
	\
	for( x = 0; x < or->valid.width; x++ ) { \
		double * restrict m = VIPS_MATRIX( recomb->coeff, 0, 0 ); \
		\
		for( v = 0; v < mheight; v++ ) { \
			double t; \
			\
			t = 0.0; \
			\
			for( u = 0; u < mwidth; u++ ) \
				t += m[u] * p[u]; \
			\
			q[v] = (OUT) t; \
			m += mwidth; \
		} \
		\
		p += mwidth; \
		q += mheight; \
	} \
}

static int
vips_recomb_gen( VipsRegion *or, 
	void *seq, void *a, void *b, gboolean *stop )
{
	VipsRegion *ir = (VipsRegion *) seq;
	VipsRecomb *recomb = (VipsRecomb *) b;
	VipsImage *im = recomb->in;
	int mwidth = recomb->m->Xsize;
	int mheight = recomb->m->Ysize;

	int y, x, u, v;

	if( vips_region_prepare( ir, &or->valid ) ) 
		return( -1 );

	for( y = 0; y < or->valid.height; y++ ) {
		VipsPel *in = VIPS_REGION_ADDR( ir, 
			or->valid.left, or->valid.top + y );
		VipsPel *out = VIPS_REGION_ADDR( or, 
			or->valid.left, or->valid.top + y );

		switch( vips_image_get_format( im ) ) {
		case VIPS_FORMAT_UCHAR: LOOP( unsigned char, float ); break;
		case VIPS_FORMAT_CHAR: 	LOOP( signed char, float ); break; 
		case VIPS_FORMAT_USHORT:LOOP( unsigned short, float ); break; 
		case VIPS_FORMAT_SHORT: LOOP( signed short, float ); break; 
		case VIPS_FORMAT_UINT: 	LOOP( unsigned int, float ); break; 
		case VIPS_FORMAT_INT: 	LOOP( signed int, float );  break; 
		case VIPS_FORMAT_FLOAT: LOOP( float, float ); break; 
		case VIPS_FORMAT_DOUBLE:LOOP( double, double ); break; 

		default:
			g_assert_not_reached();
		}
	}

	return( 0 );
}

static int
vips_recomb_build( VipsObject *object )
{
	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
	VipsConversion *conversion = (VipsConversion *) object;
	VipsRecomb *recomb = (VipsRecomb *) object;
	VipsImage **t = (VipsImage **) vips_object_local_array( object, 2 );

	VipsImage *in;

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

	in = recomb->in; 

	if( vips_image_decode( in, &t[0] ) )
		return( -1 );
	in = t[0]; 

	if( vips_check_noncomplex( class->nickname, in ) )
		return( -1 );
	if( vips_image_pio_input( recomb->m ) || 
		vips_check_uncoded( class->nickname, recomb->m ) ||
		vips_check_noncomplex( class->nickname, recomb->m ) ||
		vips_check_mono( class->nickname, recomb->m ) )
		return( -1 );
	if( in->Bands != recomb->m->Xsize ) {
		vips_error( class->nickname, 
			"%s", _( "bands in must equal matrix width" ) );
		return( -1 );
	}

	if( vips_check_matrix( class->nickname, recomb->m, &t[1] ) )
		return( -1 ); 
	recomb->coeff = t[1]; 

	if( vips_image_pipelinev( conversion->out, 
		VIPS_DEMAND_STYLE_THINSTRIP, in, NULL ) )
		return( -1 );

	conversion->out->Bands = recomb->m->Ysize;
	if( vips_band_format_isint( in->BandFmt ) ) 
		conversion->out->BandFmt = VIPS_FORMAT_FLOAT;

	if( vips_image_generate( conversion->out,
		vips_start_one, vips_recomb_gen, vips_stop_one, 
		in, recomb ) )
		return( -1 );

	return( 0 );
}

static void
vips_recomb_class_init( VipsRecombClass *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 = "recomb";
	object_class->description = _( "linear recombination with matrix" );
	object_class->build = vips_recomb_build;

	operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;

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

	VIPS_ARG_IMAGE( class, "m", 102, 
		_( "M" ), 
		_( "matrix of coefficients" ),
		VIPS_ARGUMENT_REQUIRED_INPUT, 
		G_STRUCT_OFFSET( VipsRecomb, m ) );
}

static void
vips_recomb_init( VipsRecomb *recomb )
{
}

/** 
 * vips_recomb:
 * @in: input image
 * @out: output image
 * @m: recombination matrix
 * @...: %NULL-terminated list of optional named arguments
 *
 * This operation recombines an image's bands. Each pixel in @in is treated as 
 * an n-element vector, where n is the number of bands in @in, and multipled by
 * the n x m matrix @m to produce the m-band image @out.
 *
 * @out is always float, unless @in is double, in which case @out is double
 * too. No complex images allowed.
 *
 * It's useful for various sorts of colour space conversions.
 *
 * See also: vips_bandmean().
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_recomb( VipsImage *in, VipsImage **out, VipsImage *m, ... )
{
	va_list ap;
	int result;

	va_start( ap, m );
	result = vips_call_split( "recomb", ap, in, out, m );
	va_end( ap );

	return( result );
}