File: extract.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 (477 lines) | stat: -rw-r--r-- 11,514 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* extract an area and/or a set of bands
 *
 * Copyright: 1990, J. Cupitt
 *
 * Author: J. Cupitt
 * Written on: 12/02/1990
 * Modified on: 4/6/92, J.Cupitt
 *	- speed up! why wasn't this done before? Why am I stupid?
 *	- layout, messages fixed
 * now extracts IM_CODING_LABQ to IM_CODING_LABQ file: K.Martinez 1/7/93
 * 2/7/93 JC
 *	- adapted for partial v2
 *	- ANSIfied
 * 7/7/93 JC
 *	- behaviour for IM_CODING_LABQ fixed
 *	- better messages
 * 7/10/94 JC
 *	- new IM_NEW()
 * 22/2/95 JC
 *	- new use of im_region_region()
 * 6/7/98 JC
 *	- im_extract_area() and im_extract_band() added
 * 11/7/01 JC
 *	- im_extract_band() now numbers from zero
 * 7/11/01 JC
 *	- oh what pain, im_extract now numbers bands from zero as well
 * 6/9/02 JC
 *	- zero xoff/yoff for extracted area
 * 14/4/04 JC
 *	- nope, -ve the origin
 * 17/7/04
 *	- added im_extract_bands(), remove many bands from image
 * 24/3/09
 * 	- added IM_CODING_RAD support
 * 29/1/10
 * 	- cleanups
 * 	- gtkdoc
 * 26/10/11
 * 	- redone as a class
 */

/*

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

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

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

#include "pconversion.h"

#include "bandary.h"

typedef struct _VipsExtractArea {
	VipsConversion parent_instance;

	/* The input image.
	 */
	VipsImage *in;

	int left;
	int top;
	int width;
	int height;

} VipsExtractArea;

typedef VipsConversionClass VipsExtractAreaClass;

G_DEFINE_TYPE( VipsExtractArea, vips_extract_area, VIPS_TYPE_CONVERSION );

/* Extract an area. Can just use pointers.
 */
static int
vips_extract_area_gen( VipsRegion *or, void *seq, void *a, void *b, 
	gboolean *stop )
{
	VipsRegion *ir = (VipsRegion *) seq;
	VipsExtractArea *extract = (VipsExtractArea *) b;
	VipsRect iarea;

	/* Ask for input we need. Translate from demand in or's space to
	 * demand in ir's space.
	 */
	iarea = or->valid;
	iarea.left += extract->left;
	iarea.top += extract->top;
	if( vips_region_prepare( ir, &iarea ) )
		return( -1 );

	/* Attach or to ir.
	 */
	if( vips_region_region( or, ir, &or->valid, iarea.left, iarea.top ) )
		return( -1 );
	
	return( 0 );
}

static int
vips_extract_area_build( VipsObject *object )
{
	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
	VipsConversion *conversion = VIPS_CONVERSION( object );
	VipsExtractArea *extract = (VipsExtractArea *) object;

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

	if( extract->left + extract->width > extract->in->Xsize ||
		extract->top + extract->height > extract->in->Ysize ||
		extract->left < 0 || extract->top < 0 ||
		extract->width <= 0 || extract->height <= 0 ) {
		vips_error( class->nickname, "%s", _( "bad extract area" ) );
		return( -1 );
	}

	if( vips_image_pio_input( extract->in ) || 
		vips_check_coding_known( class->nickname, extract->in ) )  
		return( -1 );

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

        conversion->out->Xsize = extract->width;
        conversion->out->Ysize = extract->height;
        conversion->out->Xoffset = -extract->left;
        conversion->out->Yoffset = -extract->top;

	if( vips_image_generate( conversion->out,
		vips_start_one, vips_extract_area_gen, vips_stop_one, 
		extract->in, extract ) )
		return( -1 );

	return( 0 );
}

/* xy range we sanity check on ... just to stop crazy numbers from divide by 0 
 * etc. causing g_assert() failures later.
 */
#define RANGE (100000000)

static void
vips_extract_area_class_init( VipsExtractAreaClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
	VipsOperationClass *operation_class = VIPS_OPERATION_CLASS( class );

	VIPS_DEBUG_MSG( "vips_extract_area_class_init\n" );

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

	vobject_class->nickname = "extract_area";
	vobject_class->description = _( "extract an area from an image" );
	vobject_class->build = vips_extract_area_build;

	operation_class->flags = VIPS_OPERATION_SEQUENTIAL_UNBUFFERED;

	VIPS_ARG_IMAGE( class, "input", 0, 
		_( "Input" ), 
		_( "Input image" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsExtractArea, in ) );

	VIPS_ARG_INT( class, "left", 2, 
		_( "Left" ), 
		_( "Left edge of extract area" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsExtractArea, left ),
		-RANGE, RANGE, 0 );

	VIPS_ARG_INT( class, "top", 3, 
		_( "Top" ), 
		_( "Top edge of extract area" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsExtractArea, top ),
		-RANGE, RANGE, 0 );

	VIPS_ARG_INT( class, "width", 4, 
		_( "Width" ), 
		_( "Width of extract area" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsExtractArea, width ),
		1, RANGE, 1 );

	VIPS_ARG_INT( class, "height", 5, 
		_( "Height" ), 
		_( "Height of extract area" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsExtractArea, height ),
		1, RANGE, 1 );

}

static void
vips_extract_area_init( VipsExtractArea *extract )
{
}

/**
 * vips_extract_area:
 * @in: input image
 * @out: output image
 * @left: left edge of area to extract
 * @top: top edge of area to extract
 * @width: width of area to extract
 * @height: height of area to extract
 * @...: %NULL-terminated list of optional named arguments
 *
 * Extract an area from an image. The area must fit within @in.
 *
 * See also: vips_extract_bands().
 * 
 * Returns: 0 on success, -1 on error.
 */
int
vips_extract_area( VipsImage *in, VipsImage **out, 
	int left, int top, int width, int height, ... )
{
	va_list ap;
	int result;

	va_start( ap, height );
	result = vips_call_split( "extract_area", ap, in, out, 
		left, top, width, height );
	va_end( ap );

	return( result );
}

/* A synonym for extract_area.
 */

GType
vips_crop_get_type( void )
{
	static GType type = 0;

	if( !type ) {
		static const GTypeInfo info = {
			sizeof( VipsExtractAreaClass ),
			NULL,           /* base_init */
			NULL,           /* base_finalize */
			(GClassInitFunc) vips_extract_area_class_init,
			NULL,           /* class_finalize */
			NULL,           /* class_data */
			sizeof( VipsExtractArea ),
			32,             /* n_preallocs */
			(GInstanceInitFunc) vips_extract_area_init,
		};

		type = g_type_register_static( VIPS_TYPE_CONVERSION, 
			"crop", &info, 0 );
	}

	return( type );
}

/**
 * vips_crop:
 * @in: input image
 * @out: output image
 * @left: left edge of area to extract
 * @top: top edge of area to extract
 * @width: width of area to extract
 * @height: height of area to extract
 * @...: %NULL-terminated list of optional named arguments
 *
 * A synonym for vips_extract_area(). 
 *
 * See also: vips_extract_bands().
 * 
 * Returns: 0 on success, -1 on error.
 */
int
vips_crop( VipsImage *in, VipsImage **out, 
	int left, int top, int width, int height, ... )
{
	va_list ap;
	int result;

	va_start( ap, height );
	result = vips_call_split( "crop", ap, in, out, 
		left, top, width, height );
	va_end( ap );

	return( result );
}

typedef struct _VipsExtractBand {
	VipsBandary parent_instance;

	/* The input image.
	 */
	VipsImage *in;

	int band;
	int n;
} VipsExtractBand;

typedef VipsBandaryClass VipsExtractBandClass;

G_DEFINE_TYPE( VipsExtractBand, vips_extract_band, VIPS_TYPE_BANDARY );

static void
vips_extract_band_buffer( VipsBandary *bandary, 
	VipsPel *out, VipsPel **in, int width )
{
	VipsConversion *conversion = (VipsConversion *) bandary;
	VipsExtractBand *extract = (VipsExtractBand *) bandary;
	VipsImage *im = bandary->ready[0];
	int es = VIPS_IMAGE_SIZEOF_ELEMENT( im );
	int ips = VIPS_IMAGE_SIZEOF_PEL( im );
	const int ops = VIPS_IMAGE_SIZEOF_PEL( conversion->out );

	VipsPel *p, *q;
	int x, z;

	p = in[0] + extract->band * es;
	q = out;
	for( x = 0; x < width; x++ ) {
		for( z = 0; z < ops; z++ )
			q[z] = p[z];

		p += ips;
		q += ops;
	}
}

static int
vips_extract_band_build( VipsObject *object )
{
	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
	VipsBandary *bandary = (VipsBandary *) object;
	VipsExtractBand *extract = (VipsExtractBand *) object;

	if( extract->in ) {
		int bands; 

		vips_image_decode_predict( extract->in, &bands, NULL );

		bandary->n = 1;
		bandary->in = &extract->in;
		bandary->out_bands = extract->n;

		if( extract->band + extract->n > bands ) {
			vips_error( class->nickname, 
				"%s", _( "bad extract band" ) );
			return( -1 );
		}

		if( extract->band == 0 && 
			extract->n == bands ) 
			return( vips_bandary_copy( bandary ) );
	}

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

	return( 0 );
}

static void
vips_extract_band_class_init( VipsExtractBandClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
	VipsBandaryClass *bandary_class = VIPS_BANDARY_CLASS( class );

	VIPS_DEBUG_MSG( "vips_extract_band_class_init\n" );

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

	vobject_class->nickname = "extract_band";
	vobject_class->description = _( "extract band from an image" );
	vobject_class->build = vips_extract_band_build;

	bandary_class->process_line = vips_extract_band_buffer;

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

	VIPS_ARG_INT( class, "band", 3, 
		_( "Band" ), 
		_( "Band to extract" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsExtractBand, band ),
		0, RANGE, 0 );

	VIPS_ARG_INT( class, "n", 4, 
		_( "n" ), 
		_( "Number of bands to extract" ),
		VIPS_ARGUMENT_OPTIONAL_INPUT,
		G_STRUCT_OFFSET( VipsExtractBand, n ),
		1, RANGE, 1 );

}

static void
vips_extract_band_init( VipsExtractBand *extract )
{
	extract->n = 1;
}

/**
 * vips_extract_band:
 * @in: input image
 * @out: output image
 * @band: band to extract
 * @...: %NULL-terminated list of optional named arguments
 *
 * Optional arguments:
 *
 * * @n: number of bands to extract
 *
 * Extract a band or bands from an image. Extracting out of range is an error.
 *
 * See also: vips_extract_area().
 * 
 * Returns: 0 on success, -1 on error.
 */
int
vips_extract_band( VipsImage *in, VipsImage **out, int band, ... )
{
	va_list ap;
	int result;

	va_start( ap, band );
	result = vips_call_split( "extract_band", ap, in, out, band );
	va_end( ap );

	return( result );
}