File: math.c

package info (click to toggle)
vips 8.14.1-3%2Bdeb12u2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 35,912 kB
  • sloc: ansic: 165,449; cpp: 10,987; python: 4,462; xml: 4,212; sh: 471; perl: 40; makefile: 23
file content (647 lines) | stat: -rw-r--r-- 14,952 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/* VipsMath --- call various -lm functions (trig, log etc.) on images
 *
 * Copyright: 1990, N. Dessipris, based on im_powtra()
 * Author: Nicos Dessipris
 * Written on: 02/05/1990
 * Modified on: 
 * 5/5/93 JC
 *	- adapted from im_lintra to work with partial images
 *	- incorrect implementation of complex logs removed
 * 1/7/93 JC
 *	- adapted for partial v2
 *	- ANSIfied
 * 24/2/95 JC
 *	- im_logtra() adapted to make im_sintra()
 *	- adapted for im_wrapone()
 * 26/1/96 JC
 *	- im_asintra() added
 * 30/8/09
 * 	- gtkdoc
 * 	- tiny cleanups
 * 	- use im__math()
 * 19/9/09
 * 	- im_sintra() adapted to make math.c
 * 4/11/11
 * 	- redone as a class
 * 11/8/15
 * 	- log/log10 zero-avoid
 */

/*

    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 <glib/gi18n-lib.h>

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

#include <vips/vips.h>

#include "unary.h"

typedef struct _VipsMath {
	VipsUnary parent_instance;

	VipsOperationMath math;

} VipsMath;

typedef VipsUnaryClass VipsMathClass;

G_DEFINE_TYPE( VipsMath, vips_math, VIPS_TYPE_UNARY );

static int
vips_math_build( VipsObject *object )
{
	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
	VipsUnary *unary = (VipsUnary *) object;

	if( unary->in &&
		vips_check_noncomplex( class->nickname, unary->in ) )
		return( -1 );

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

	return( 0 );
}

#define LOOP( IN, OUT, OP ) { \
	IN * restrict p = (IN *) in[0]; \
	OUT * restrict q = (OUT *) out; \
	\
	for( x = 0; x < sz; x++ ) \
		q[x] = OP( p[x] ); \
}

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

/* If there's asinh, assume we have the other two as well.
 */
#if HAVE_ASINH
  #define ASINH( X ) (asinh( X ))
  #define ACOSH( X ) (acosh( X ))
  #define ATANH( X ) (atanh( X ))
#else
  #define ASINH( X ) (LOGZ( (X) + sqrt( (X) * (X) + 1.0 ) ))
  #define ACOSH( X ) (LOGZ( (X) + sqrt( (X) * (X) - 1.0 ) ))
  #define ATANH( X ) (0.5 * LOGZ( (1.0 + (X)) / (1.0 - (X)) ))
#endif

/* sin/cos/tan in degrees.
 */
#define DSIN( X ) (sin( VIPS_RAD( X ) ))
#define DCOS( X ) (cos( VIPS_RAD( X ) ))
#define DTAN( X ) (tan( VIPS_RAD( X ) ))
#define ADSIN( X ) (VIPS_DEG( asin( X ) ))
#define ADCOS( X ) (VIPS_DEG( acos( X ) ))
#define ADTAN( X ) (VIPS_DEG( atan( X ) ))

/* exp10() is a gnu extension, use pow().
 */
#define EXP10( X ) (pow( 10.0, (X) ))

/* Zero-avoiding log, cf. zero-avoiding behaviour of /.
 */
#define LOGZ( X ) ((X) == 0.0 ? 0.0 : log( X ))
#define LOGZ10( X ) ((X) == 0.0 ? 0.0 : log10( X ))

static void
vips_math_buffer( VipsArithmetic *arithmetic, 
	VipsPel *out, VipsPel **in, int width )
{
	VipsMath *math = (VipsMath *) arithmetic;
	VipsImage *im = arithmetic->ready[0];
	const int sz = width * vips_image_get_bands( im );

	int x;

	switch( math->math ) {
	case VIPS_OPERATION_MATH_SIN: 	SWITCH( DSIN ); break;
	case VIPS_OPERATION_MATH_COS: 	SWITCH( DCOS ); break;
	case VIPS_OPERATION_MATH_TAN: 	SWITCH( DTAN ); break;
	case VIPS_OPERATION_MATH_ASIN: 	SWITCH( ADSIN ); break;
	case VIPS_OPERATION_MATH_ACOS: 	SWITCH( ADCOS ); break;
	case VIPS_OPERATION_MATH_ATAN: 	SWITCH( ADTAN ); break;
	case VIPS_OPERATION_MATH_SINH: 	SWITCH( sinh ); break;
	case VIPS_OPERATION_MATH_COSH: 	SWITCH( cosh ); break;
	case VIPS_OPERATION_MATH_TANH: 	SWITCH( tanh ); break;
	case VIPS_OPERATION_MATH_ASINH:	SWITCH( ASINH ); break;
	case VIPS_OPERATION_MATH_ACOSH:	SWITCH( ACOSH ); break;
	case VIPS_OPERATION_MATH_ATANH:	SWITCH( ATANH ); break;
	case VIPS_OPERATION_MATH_LOG: 	SWITCH( LOGZ ); break;
	case VIPS_OPERATION_MATH_LOG10:	SWITCH( LOGZ10 ); break;
	case VIPS_OPERATION_MATH_EXP: 	SWITCH( exp ); break;
	case VIPS_OPERATION_MATH_EXP10:	SWITCH( EXP10 ); 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

static const VipsBandFormat vips_math_format_table[10] = {
	/* Band format:  UC C  US S  UI I  F  X  D  DX */
	/* Promotion: */ F, F, F, F, F, F, F, X, D, DX
};

static void
vips_math_class_init( VipsMathClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *object_class = (VipsObjectClass *) class;
	VipsArithmeticClass *aclass = VIPS_ARITHMETIC_CLASS( class );

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

	object_class->nickname = "math";
	object_class->description = _( "apply a math operation to an image" );
	object_class->build = vips_math_build;

	aclass->process_line = vips_math_buffer;

	vips_arithmetic_set_format_table( aclass, vips_math_format_table ); 

	VIPS_ARG_ENUM( class, "math", 200, 
		_( "Operation" ), 
		_( "Math to perform" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsMath, math ),
		VIPS_TYPE_OPERATION_MATH, VIPS_OPERATION_MATH_SIN ); 
}

static void
vips_math_init( VipsMath *math )
{
}

static int
vips_mathv( VipsImage *in, VipsImage **out, VipsOperationMath math, va_list ap )
{
	return( vips_call_split( "math", ap, in, out, math ) );
}

/**
 * vips_math: (method)
 * @in: input #VipsImage
 * @out: (out): output #VipsImage
 * @math: math operation to perform
 * @...: %NULL-terminated list of optional named arguments
 *
 * Perform various functions in -lm, the maths library, on images. 
 *
 * Angles are expressed in degrees. The output type is float unless the 
 * input is double, in which case the output is double.  
 *
 * Non-complex images only.
 *
 * See also: vips_math2().
 *
 * Returns: 0 on success, -1 on error
 */
int
vips_math( VipsImage *in, VipsImage **out, VipsOperationMath math, ... )
{
	va_list ap;
	int result;

	va_start( ap, math );
	result = vips_mathv( in, out, math, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_SIN, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_COS, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_TAN, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_ASIN, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_ACOS, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_ATAN, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_SINH, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_COSH, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_TANH, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_ASINH, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_ACOSH, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_ATANH, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_LOG, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_LOG10, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_EXP, ap );
	va_end( ap );

	return( result );
}

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

	va_start( ap, out );
	result = vips_mathv( in, out, VIPS_OPERATION_MATH_EXP10, ap );
	va_end( ap );

	return( result );
}