1 /* cast an image to a numerical format
2  *
3  * Author: Nicos Dessipris
4  * Written on: 07/03/1991
5  * Modified on:
6  * 04/05/1992 JC
7  *	- works for char, uchar too
8  *	- floating point code removed from integer clip operations
9  *	- uses nint() instead of own rounding code
10  *	- calculated the number of >255 clips for float/double input
11  *	  incorrectly
12  *	- rejects complex input correctly now
13  * 27/4/93 JC
14  *	- adapted to work with partial images
15  *	- nint() removed, now just +0.5
16  *	- im_warning code removed
17  * 30/6/93 JC
18  *	- adapted for partial v2
19  * 31/8/93 JC
20  *	- now detects and prints over/underflows
21  * 27/10/93 JC
22  *	- unsigned integer clips now faster!
23  *	- falls back to im_copy() correctly
24  * 5/5/94 JC
25  *	- switched to rint()
26  * 18/8/94 JC
27  *	- now uses evalend callback
28  * 9/5/95 JC
29  *	- now does complex too
30  * 11/7/95 JC
31  *	- now uses IM_RINT() macro
32  * 10/3/01 JC
33  *	- slightly faster and simpler
34  *	- generalised to im_clip2fmt(), all other clippers now just call
35  *	  this
36  * 21/4/04 JC
37  *	- now does floor(), not rint() ... you'll need to round yourself
38  *	  before calling this if you want round-to-nearest
39  * 7/11/07
40  * 	- use new evalstart/evalend system
41  * 26/8/08
42  * 	- oops, complex->complex conversion was broken
43  * 27/1/10
44  * 	- modernised
45  * 	- gtk-doc
46  * 27/10/11
47  * 	- redone as a class
48  * 10/4/12
49  * 	- cast to uint now removes <0 values
50  * 11/2/15
51  * 	- add @shift option
52  * 1/3/16
53  * 	- better behaviour for shift of non-int types (thanks apacheark)
54  * 14/11/18
55  * 	- revise for better uint/int clipping [erdmann]
56  * 	- remove old overflow/underflow detect
57  * 8/12/20
58  * 	- fix range clip in int32 -> unsigned casts [ewelot]
59  */
60 
61 /*
62 
63     This file is part of VIPS.
64 
65     VIPS is free software; you can redistribute it and/or modify
66     it under the terms of the GNU Lesser General Public License as published by
67     the Free Software Foundation; either version 2 of the License, or
68     (at your option) any later version.
69 
70     This program is distributed in the hope that it will be useful,
71     but WITHOUT ANY WARRANTY; without even the implied warranty of
72     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
73     GNU Lesser General Public License for more details.
74 
75     You should have received a copy of the GNU Lesser General Public License
76     along with this program; if not, write to the Free Software
77     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
78     02110-1301  USA
79 
80  */
81 
82 /*
83 
84     These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
85 
86  */
87 
88 /*
89 #define VIPS_DEBUG
90  */
91 
92 #ifdef HAVE_CONFIG_H
93 #include <config.h>
94 #endif /*HAVE_CONFIG_H*/
95 #include <vips/intl.h>
96 
97 #include <stdio.h>
98 #include <string.h>
99 #include <stdlib.h>
100 #include <math.h>
101 
102 #include <vips/vips.h>
103 #include <vips/internal.h>
104 #include <vips/debug.h>
105 
106 #include "pconversion.h"
107 
108 typedef struct _VipsCast {
109 	VipsConversion parent_instance;
110 
111 	VipsImage *in;
112 	VipsBandFormat format;
113 	gboolean shift;
114 
115 } VipsCast;
116 
117 typedef VipsConversionClass VipsCastClass;
118 
119 G_DEFINE_TYPE( VipsCast, vips_cast, VIPS_TYPE_CONVERSION );
120 
121 /* Cast down from an int.
122  */
123 #define CAST_UCHAR( X ) VIPS_CLIP( 0, (X), UCHAR_MAX )
124 #define CAST_CHAR( X ) VIPS_CLIP( SCHAR_MIN, (X), SCHAR_MAX )
125 #define CAST_USHORT( X ) VIPS_CLIP( 0, (X), USHRT_MAX )
126 #define CAST_SHORT( X ) VIPS_CLIP( SHRT_MIN, (X), SHRT_MAX )
127 
128 /* These cast down from gint64 to uint32 or int32.
129  */
130 #define CAST_UINT( X ) VIPS_CLIP( 0, (X), UINT_MAX )
131 #define CAST_INT( X ) VIPS_CLIP( INT_MIN, (X), INT_MAX )
132 
133 /* Rightshift an integer type, ie. sizeof(ITYPE) > sizeof(OTYPE).
134  */
135 #define SHIFT_RIGHT( ITYPE, OTYPE ) { \
136 	ITYPE * restrict p = (ITYPE *) in; \
137 	OTYPE * restrict q = (OTYPE *) out; \
138 	int n = ((int) sizeof( ITYPE ) << 3) - ((int) sizeof( OTYPE ) << 3); \
139 	\
140 	g_assert( sizeof( ITYPE ) > sizeof( OTYPE ) ); \
141 	\
142 	for( x = 0; x < sz; x++ ) \
143 		q[x] = p[x] >> n; \
144 }
145 
146 /* Leftshift an integer type, ie. sizeof(ITYPE) < sizeof(OTYPE). We need to
147  * copy the bottom bit up into the fresh new bits.
148  */
149 #define SHIFT_LEFT( ITYPE, OTYPE ) { \
150 	ITYPE * restrict p = (ITYPE *) in; \
151 	OTYPE * restrict q = (OTYPE *) out; \
152 	int n = ((int) sizeof( OTYPE ) << 3) - ((int) sizeof( ITYPE ) << 3); \
153 	\
154 	g_assert( sizeof( ITYPE ) < sizeof( OTYPE ) ); \
155 	\
156 	for( x = 0; x < sz; x++ ) \
157 		q[x] = (p[x] << n) | (((p[x] & 1) << n) - (p[x] & 1)); \
158 }
159 
160 #define SHIFT_LEFT_SIGNED( ITYPE, OTYPE ) { \
161 	ITYPE * restrict p = (ITYPE *) in; \
162 	OTYPE * restrict q = (OTYPE *) out; \
163 	int n = ((int) sizeof( OTYPE ) << 3) - ((int) sizeof( ITYPE ) << 3); \
164 	\
165 	g_assert( sizeof( ITYPE ) < sizeof( OTYPE ) ); \
166 	\
167 	for( x = 0; x < sz; x++ ) \
168 		q[x] = VIPS_LSHIFT_INT( p[x], n ) | \
169 			(((p[x] & 1) << n) - (p[x] & 1)); \
170 }
171 
172 /* Cast int types to an int type. We need to pass in the type of the
173  * intermediate value, either int or int64, or we'll have problems with uint
174  * sources turning -ve.
175  */
176 #define CAST_INT_INT( ITYPE, OTYPE, TEMP, CAST ) { \
177 	ITYPE * restrict p = (ITYPE *) in; \
178 	OTYPE * restrict q = (OTYPE *) out; \
179 	\
180 	for( x = 0; x < sz; x++ ) { \
181 		TEMP t = (TEMP) p[x]; \
182 		\
183 		q[x] = CAST( t ); \
184 	} \
185 }
186 
187 /* Int to int handling.
188  */
189 #define INT_INT( ITYPE, OTYPE, TEMP, CAST ) { \
190 	if( cast->shift && \
191 		sizeof( ITYPE ) > sizeof( OTYPE ) ) { \
192 		SHIFT_RIGHT( ITYPE, OTYPE ); \
193 	} \
194 	else if( cast->shift ) { \
195 		SHIFT_LEFT( ITYPE, OTYPE ); \
196 	} \
197 	else { \
198 		CAST_INT_INT( ITYPE, OTYPE, TEMP, CAST ); \
199 	} \
200 }
201 
202 /* Int to int handling for signed int types.
203  */
204 #define INT_INT_SIGNED( ITYPE, OTYPE, TEMP, CAST ) { \
205 	if( cast->shift && \
206 		sizeof( ITYPE ) > sizeof( OTYPE ) ) { \
207 		SHIFT_RIGHT( ITYPE, OTYPE ); \
208 	} \
209 	else if( cast->shift ) { \
210 		SHIFT_LEFT_SIGNED( ITYPE, OTYPE ); \
211 	} \
212 	else { \
213 		CAST_INT_INT( ITYPE, OTYPE, TEMP, CAST ); \
214 	} \
215 }
216 
217 /* Cast float types to an int type.
218  *
219  * We need to do the range clip as double or we'll get errors for int max,
220  * since that can't be represented as a 32-bit float.
221  */
222 #define CAST_FLOAT_INT( ITYPE, OTYPE, TEMP, CAST ) { \
223 	ITYPE * restrict p = (ITYPE *) in; \
224 	OTYPE * restrict q = (OTYPE *) out; \
225 	\
226 	for( x = 0; x < sz; x++ ) \
227 		q[x] = CAST( (double) p[x] ); \
228 }
229 
230 /* Cast complex types to an int type. Just take the real part.
231  *
232  * We need to do the range clip as double or we'll get errors for int max,
233  * since that can't be represented as a 32-bit float.
234  */
235 #define CAST_COMPLEX_INT( ITYPE, OTYPE, TEMP, CAST ) { \
236 	ITYPE * restrict p = (ITYPE *) in; \
237 	OTYPE * restrict q = (OTYPE *) out; \
238 	\
239 	for( x = 0; x < sz; x++ ) { \
240 		q[x] = CAST( (double) p[0] ); \
241 		p += 2; \
242 	} \
243 }
244 
245 /* Cast non-complex types to a float type.
246  */
247 #define CAST_REAL_FLOAT( ITYPE, OTYPE ) { \
248 	ITYPE * restrict p = (ITYPE *) in; \
249 	OTYPE * restrict q = (OTYPE *) out; \
250 	\
251 	for( x = 0; x < sz; x++ ) \
252 		q[x] = p[x]; \
253 }
254 
255 /* Cast complex types to a float type ... just take real.
256  */
257 #define CAST_COMPLEX_FLOAT( ITYPE, OTYPE ) { \
258 	ITYPE * restrict p = (ITYPE *) in; \
259 	OTYPE * restrict q = (OTYPE *) out; \
260 	\
261 	for( x = 0; x < sz; x++ ) { \
262 		q[x] = p[0]; \
263 		p += 2; \
264 	} \
265 }
266 
267 /* Cast any non-complex to a complex type ... set imaginary to zero.
268  */
269 #define CAST_REAL_COMPLEX( ITYPE, OTYPE ) { \
270 	ITYPE * restrict p = (ITYPE *) in; \
271 	OTYPE * restrict q = (OTYPE *) out; \
272 	\
273 	for( x = 0; x < sz; x++ ) { \
274 		q[0] = p[x]; \
275 		q[1] = 0.0; \
276 		q += 2; \
277 	} \
278 }
279 
280 /* Cast any complex to a complex type.
281  */
282 #define CAST_COMPLEX_COMPLEX( ITYPE, OTYPE ) { \
283 	ITYPE * restrict p = (ITYPE *) in; \
284 	OTYPE * restrict q = (OTYPE *) out; \
285 	\
286 	for( x = 0; x < sz; x++ ) { \
287 		q[0] = p[0]; \
288 		q[1] = p[1]; \
289 		p += 2; \
290 		q += 2; \
291 	} \
292 }
293 
294 #define BAND_SWITCH_INNER( ITYPE, INT, FLOAT, COMPLEX ) { \
295 	switch( conversion->out->BandFmt ) { \
296 	case VIPS_FORMAT_UCHAR: \
297 		INT( ITYPE, unsigned char, int, CAST_UCHAR ); \
298 		break; \
299 	\
300 	case VIPS_FORMAT_CHAR: \
301 		INT( ITYPE, signed char, int, CAST_CHAR ); \
302 		break; \
303 	\
304 	case VIPS_FORMAT_USHORT: \
305 		INT( ITYPE, unsigned short, int, CAST_USHORT ); \
306 		break; \
307 	\
308 	case VIPS_FORMAT_SHORT: \
309 		INT( ITYPE, signed short, int, CAST_SHORT ); \
310 		break; \
311 	\
312 	case VIPS_FORMAT_UINT: \
313 		INT( ITYPE, unsigned int, gint64, CAST_UINT ); \
314 		break; \
315 	\
316 	case VIPS_FORMAT_INT: \
317 		INT( ITYPE, signed int, gint64, CAST_INT ); \
318 		break; \
319 	\
320 	case VIPS_FORMAT_FLOAT: \
321 		FLOAT( ITYPE, float ); \
322 		break; \
323 	\
324 	case VIPS_FORMAT_DOUBLE: \
325 		FLOAT( ITYPE, double ); \
326 		break; \
327 	\
328 	case VIPS_FORMAT_COMPLEX: \
329 		COMPLEX( ITYPE, float ); \
330 		break; \
331 	\
332 	case VIPS_FORMAT_DPCOMPLEX: \
333 		COMPLEX( ITYPE, double ); \
334 		break; \
335 	\
336 	default: \
337 		g_assert_not_reached(); \
338 	} \
339 }
340 
341 static int
vips_cast_gen(VipsRegion * or,void * vseq,void * a,void * b,gboolean * stop)342 vips_cast_gen( VipsRegion *or, void *vseq, void *a, void *b, gboolean *stop )
343 {
344 	VipsRegion *ir = (VipsRegion *) vseq;
345 	VipsCast *cast = (VipsCast *) b;
346 	VipsConversion *conversion = (VipsConversion *) b;
347 	VipsRect *r = &or->valid;
348 	int sz = VIPS_REGION_N_ELEMENTS( or );
349 
350 	int x, y;
351 
352 	if( vips_region_prepare( ir, r ) )
353 		return( -1 );
354 
355 	VIPS_GATE_START( "vips_cast_gen: work" );
356 
357 	for( y = 0; y < r->height; y++ ) {
358 		VipsPel *in = VIPS_REGION_ADDR( ir, r->left, r->top + y );
359 		VipsPel *out = VIPS_REGION_ADDR( or, r->left, r->top + y );
360 
361 		switch( ir->im->BandFmt ) {
362 		case VIPS_FORMAT_UCHAR:
363 			BAND_SWITCH_INNER( unsigned char,
364 				INT_INT,
365 				CAST_REAL_FLOAT,
366 				CAST_REAL_COMPLEX );
367 			break;
368 
369 		case VIPS_FORMAT_CHAR:
370 			BAND_SWITCH_INNER( signed char,
371 				INT_INT_SIGNED,
372 				CAST_REAL_FLOAT,
373 				CAST_REAL_COMPLEX );
374 			break;
375 
376 		case VIPS_FORMAT_USHORT:
377 			BAND_SWITCH_INNER( unsigned short,
378 				INT_INT,
379 				CAST_REAL_FLOAT,
380 				CAST_REAL_COMPLEX );
381 			break;
382 
383 		case VIPS_FORMAT_SHORT:
384 			BAND_SWITCH_INNER( signed short,
385 				INT_INT_SIGNED,
386 				CAST_REAL_FLOAT,
387 				CAST_REAL_COMPLEX );
388 			break;
389 
390 		case VIPS_FORMAT_UINT:
391 			BAND_SWITCH_INNER( unsigned int,
392 				INT_INT,
393 				CAST_REAL_FLOAT,
394 				CAST_REAL_COMPLEX );
395 			break;
396 
397 		case VIPS_FORMAT_INT:
398 			BAND_SWITCH_INNER( signed int,
399 				INT_INT_SIGNED,
400 				CAST_REAL_FLOAT,
401 				CAST_REAL_COMPLEX );
402 			break;
403 
404 		case VIPS_FORMAT_FLOAT:
405 			BAND_SWITCH_INNER( float,
406 				CAST_FLOAT_INT,
407 				CAST_REAL_FLOAT,
408 				CAST_REAL_COMPLEX );
409 			break;
410 
411 		case VIPS_FORMAT_DOUBLE:
412 			BAND_SWITCH_INNER( double,
413 				CAST_FLOAT_INT,
414 				CAST_REAL_FLOAT,
415 				CAST_REAL_COMPLEX );
416 			break;
417 
418 		case VIPS_FORMAT_COMPLEX:
419 			BAND_SWITCH_INNER( float,
420 				CAST_COMPLEX_INT,
421 				CAST_COMPLEX_FLOAT,
422 				CAST_COMPLEX_COMPLEX );
423 			break;
424 
425 		case VIPS_FORMAT_DPCOMPLEX:
426 			BAND_SWITCH_INNER( double,
427 				CAST_COMPLEX_INT,
428 				CAST_COMPLEX_FLOAT,
429 				CAST_COMPLEX_COMPLEX );
430 			break;
431 
432 		default:
433 			g_assert_not_reached();
434 		}
435 	}
436 
437 	VIPS_GATE_STOP( "vips_cast_gen: work" );
438 
439 	return( 0 );
440 }
441 
442 static int
vips_cast_build(VipsObject * object)443 vips_cast_build( VipsObject *object )
444 {
445 	VipsConversion *conversion = VIPS_CONVERSION( object );
446 	VipsCast *cast = (VipsCast *) object;
447 	VipsImage **t = (VipsImage **)
448 		vips_object_local_array( object, 2 );
449 
450 	VipsImage *in;
451 
452 	if( VIPS_OBJECT_CLASS( vips_cast_parent_class )->build( object ) )
453 		return( -1 );
454 
455 	in = cast->in;
456 
457 	/* Trivial case: fall back to copy().
458 	 */
459 	if( in->BandFmt == cast->format )
460 		return( vips_image_write( in, conversion->out ) );
461 
462 	if( vips_image_decode( in, &t[0] ) )
463 		return( -1 );
464 	in = t[0];
465 
466 	/* If @shift is on but we're not in an int format and we're going to
467 	 * an int format, we need to cast to int first. For example, what
468 	 * about a float image tagged as rgb16 being cast to uint8? We need
469 	 * to cast to ushort before we do the final cast to uint8.
470 	 */
471 	if( cast->shift &&
472 		!vips_band_format_isint( in->BandFmt ) &&
473 		vips_band_format_isint( cast->format ) ) {
474 		if( vips_cast( in, &t[1],
475 			vips_image_guess_format( in ), NULL ) )
476 			return( -1 );
477 		in = t[1];
478 	}
479 
480 	if( vips_image_pipelinev( conversion->out,
481 		VIPS_DEMAND_STYLE_THINSTRIP, in, NULL ) )
482 		return( -1 );
483 
484 	conversion->out->BandFmt = cast->format;
485 
486 	if( vips_image_generate( conversion->out,
487 		vips_start_one, vips_cast_gen, vips_stop_one,
488 		in, cast ) )
489 		return( -1 );
490 
491 	return( 0 );
492 }
493 
494 static void
vips_cast_class_init(VipsCastClass * class)495 vips_cast_class_init( VipsCastClass *class )
496 {
497 	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
498 	VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
499 	VipsOperationClass *operation_class = VIPS_OPERATION_CLASS( class );
500 
501 	VIPS_DEBUG_MSG( "vips_cast_class_init\n" );
502 
503 	gobject_class->set_property = vips_object_set_property;
504 	gobject_class->get_property = vips_object_get_property;
505 
506 	vobject_class->nickname = "cast";
507 	vobject_class->description = _( "cast an image" );
508 	vobject_class->build = vips_cast_build;
509 
510 	operation_class->flags = VIPS_OPERATION_SEQUENTIAL;
511 
512 	VIPS_ARG_IMAGE( class, "in", 1,
513 		_( "Input" ),
514 		_( "Input image" ),
515 		VIPS_ARGUMENT_REQUIRED_INPUT,
516 		G_STRUCT_OFFSET( VipsCast, in ) );
517 
518 	VIPS_ARG_ENUM( class, "format", 6,
519 		_( "Format" ),
520 		_( "Format to cast to" ),
521 		VIPS_ARGUMENT_REQUIRED_INPUT,
522 		G_STRUCT_OFFSET( VipsCast, format ),
523 		VIPS_TYPE_BAND_FORMAT, VIPS_FORMAT_UCHAR );
524 
525 	VIPS_ARG_BOOL( class, "shift", 7,
526 		_( "Shift" ),
527 		_( "Shift integer values up and down" ),
528 		VIPS_ARGUMENT_OPTIONAL_INPUT,
529 		G_STRUCT_OFFSET( VipsCast, shift ),
530 		FALSE );
531 }
532 
533 static void
vips_cast_init(VipsCast * cast)534 vips_cast_init( VipsCast *cast )
535 {
536 }
537 
538 static int
vips_castv(VipsImage * in,VipsImage ** out,VipsBandFormat format,va_list ap)539 vips_castv( VipsImage *in, VipsImage **out, VipsBandFormat format, va_list ap )
540 {
541 	return( vips_call_split( "cast", ap, in, out, format ) );
542 }
543 
544 /**
545  * vips_cast: (method)
546  * @in: input image
547  * @out: (out): output image
548  * @format: format to convert to
549  * @...: %NULL-terminated list of optional named arguments
550  *
551  * Optional arguments:
552  *
553  * * @shift: %gboolean, integer values are shifted
554  *
555  * Convert @in to @format. You can convert between any pair of formats.
556  * Floats are truncated (not rounded). Out of range values are clipped.
557  *
558  * Casting from complex to real returns the real part.
559  *
560  * If @shift is %TRUE, integer values are shifted up and down. For example,
561  * casting from unsigned 8 bit to unsigned 16 bit would
562  * shift every value left by 8 bits. The bottom bit is copied into the new
563  * bits, so 255 would become 65535.
564  *
565  * See also: vips_scale(), vips_complexform(), vips_real(), vips_imag(),
566  * vips_cast_uchar(), vips_msb().
567  *
568  * Returns: 0 on success, -1 on error
569  */
570 int
vips_cast(VipsImage * in,VipsImage ** out,VipsBandFormat format,...)571 vips_cast( VipsImage *in, VipsImage **out, VipsBandFormat format, ... )
572 {
573 	va_list ap;
574 	int result;
575 
576 	va_start( ap, format );
577 	result = vips_castv( in, out, format, ap );
578 	va_end( ap );
579 
580 	return( result );
581 }
582 
583 /**
584  * vips_cast_uchar: (method)
585  * @in: input image
586  * @out: (out): output image
587  * @...: %NULL-terminated list of optional named arguments
588  *
589  * Convert @in to #VIPS_FORMAT_UCHAR. See vips_cast().
590  *
591  * Returns: 0 on success, -1 on error
592  */
593 int
vips_cast_uchar(VipsImage * in,VipsImage ** out,...)594 vips_cast_uchar( VipsImage *in, VipsImage **out, ... )
595 {
596 	va_list ap;
597 	int result;
598 
599 	va_start( ap, out );
600 	result = vips_castv( in, out, VIPS_FORMAT_UCHAR, ap );
601 	va_end( ap );
602 
603 	return( result );
604 }
605 
606 /**
607  * vips_cast_char: (method)
608  * @in: input image
609  * @out: (out): output image
610  * @...: %NULL-terminated list of optional named arguments
611  *
612  * Convert @in to #VIPS_FORMAT_CHAR. See vips_cast().
613  *
614  * Returns: 0 on success, -1 on error
615  */
616 int
vips_cast_char(VipsImage * in,VipsImage ** out,...)617 vips_cast_char( VipsImage *in, VipsImage **out, ... )
618 {
619 	va_list ap;
620 	int result;
621 
622 	va_start( ap, out );
623 	result = vips_castv( in, out, VIPS_FORMAT_CHAR, ap );
624 	va_end( ap );
625 
626 	return( result );
627 }
628 
629 /**
630  * vips_cast_ushort: (method)
631  * @in: input image
632  * @out: (out): output image
633  * @...: %NULL-terminated list of optional named arguments
634  *
635  * Convert @in to #VIPS_FORMAT_USHORT. See vips_cast().
636  *
637  * Returns: 0 on success, -1 on error
638  */
639 int
vips_cast_ushort(VipsImage * in,VipsImage ** out,...)640 vips_cast_ushort( VipsImage *in, VipsImage **out, ... )
641 {
642 	va_list ap;
643 	int result;
644 
645 	va_start( ap, out );
646 	result = vips_castv( in, out, VIPS_FORMAT_USHORT, ap );
647 	va_end( ap );
648 
649 	return( result );
650 }
651 
652 /**
653  * vips_cast_short: (method)
654  * @in: input image
655  * @out: (out): output image
656  * @...: %NULL-terminated list of optional named arguments
657  *
658  * Convert @in to #VIPS_FORMAT_SHORT. See vips_cast().
659  *
660  * Returns: 0 on success, -1 on error
661  */
662 int
vips_cast_short(VipsImage * in,VipsImage ** out,...)663 vips_cast_short( VipsImage *in, VipsImage **out, ... )
664 {
665 	va_list ap;
666 	int result;
667 
668 	va_start( ap, out );
669 	result = vips_castv( in, out, VIPS_FORMAT_SHORT, ap );
670 	va_end( ap );
671 
672 	return( result );
673 }
674 
675 /**
676  * vips_cast_uint: (method)
677  * @in: input image
678  * @out: (out): output image
679  * @...: %NULL-terminated list of optional named arguments
680  *
681  * Convert @in to #VIPS_FORMAT_UINT. See vips_cast().
682  *
683  * Returns: 0 on success, -1 on error
684  */
685 int
vips_cast_uint(VipsImage * in,VipsImage ** out,...)686 vips_cast_uint( VipsImage *in, VipsImage **out, ... )
687 {
688 	va_list ap;
689 	int result;
690 
691 	va_start( ap, out );
692 	result = vips_castv( in, out, VIPS_FORMAT_UINT, ap );
693 	va_end( ap );
694 
695 	return( result );
696 }
697 
698 /**
699  * vips_cast_int: (method)
700  * @in: input image
701  * @out: (out): output image
702  * @...: %NULL-terminated list of optional named arguments
703  *
704  * Convert @in to #VIPS_FORMAT_INT. See vips_cast().
705  *
706  * Returns: 0 on success, -1 on error
707  */
708 int
vips_cast_int(VipsImage * in,VipsImage ** out,...)709 vips_cast_int( VipsImage *in, VipsImage **out, ... )
710 {
711 	va_list ap;
712 	int result;
713 
714 	va_start( ap, out );
715 	result = vips_castv( in, out, VIPS_FORMAT_INT, ap );
716 	va_end( ap );
717 
718 	return( result );
719 }
720 
721 /**
722  * vips_cast_float: (method)
723  * @in: input image
724  * @out: (out): output image
725  * @...: %NULL-terminated list of optional named arguments
726  *
727  * Convert @in to #VIPS_FORMAT_FLOAT. See vips_cast().
728  *
729  * Returns: 0 on success, -1 on error
730  */
731 int
vips_cast_float(VipsImage * in,VipsImage ** out,...)732 vips_cast_float( VipsImage *in, VipsImage **out, ... )
733 {
734 	va_list ap;
735 	int result;
736 
737 	va_start( ap, out );
738 	result = vips_castv( in, out, VIPS_FORMAT_FLOAT, ap );
739 	va_end( ap );
740 
741 	return( result );
742 }
743 
744 /**
745  * vips_cast_double: (method)
746  * @in: input image
747  * @out: (out): output image
748  * @...: %NULL-terminated list of optional named arguments
749  *
750  * Convert @in to #VIPS_FORMAT_DOUBLE. See vips_cast().
751  *
752  * Returns: 0 on success, -1 on error
753  */
754 int
vips_cast_double(VipsImage * in,VipsImage ** out,...)755 vips_cast_double( VipsImage *in, VipsImage **out, ... )
756 {
757 	va_list ap;
758 	int result;
759 
760 	va_start( ap, out );
761 	result = vips_castv( in, out, VIPS_FORMAT_DOUBLE, ap );
762 	va_end( ap );
763 
764 	return( result );
765 }
766 
767 /**
768  * vips_cast_complex: (method)
769  * @in: input image
770  * @out: (out): output image
771  * @...: %NULL-terminated list of optional named arguments
772  *
773  * Convert @in to #VIPS_FORMAT_COMPLEX. See vips_cast().
774  *
775  * Returns: 0 on success, -1 on error
776  */
777 int
vips_cast_complex(VipsImage * in,VipsImage ** out,...)778 vips_cast_complex( VipsImage *in, VipsImage **out, ... )
779 {
780 	va_list ap;
781 	int result;
782 
783 	va_start( ap, out );
784 	result = vips_castv( in, out, VIPS_FORMAT_COMPLEX, ap );
785 	va_end( ap );
786 
787 	return( result );
788 }
789 
790 /**
791  * vips_cast_dpcomplex: (method)
792  * @in: input image
793  * @out: (out): output image
794  * @...: %NULL-terminated list of optional named arguments
795  *
796  * Convert @in to #VIPS_FORMAT_DPCOMPLEX. See vips_cast().
797  *
798  * Returns: 0 on success, -1 on error
799  */
800 int
vips_cast_dpcomplex(VipsImage * in,VipsImage ** out,...)801 vips_cast_dpcomplex( VipsImage *in, VipsImage **out, ... )
802 {
803 	va_list ap;
804 	int result;
805 
806 	va_start( ap, out );
807 	result = vips_castv( in, out, VIPS_FORMAT_DPCOMPLEX, ap );
808 	va_end( ap );
809 
810 	return( result );
811 }
812 
813