1 /* LIBGIMP - The GIMP Library
2  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3  *
4  * gimpmath.h
5  *
6  * This library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library.  If not, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef __GIMP_MATH_H__
22 #define __GIMP_MATH_H__
23 
24 
25 #include <math.h>
26 
27 #ifdef HAVE_IEEEFP_H
28 #include <ieeefp.h>
29 #endif
30 
31 #ifdef G_OS_WIN32
32 #include <float.h>
33 #endif
34 
35 #define __GIMP_MATH_H_INSIDE__
36 
37 #include <libgimpmath/gimpmathtypes.h>
38 
39 #include <libgimpmath/gimpmatrix.h>
40 #include <libgimpmath/gimpmd5.h>
41 #include <libgimpmath/gimpvector.h>
42 
43 #undef __GIMP_MATH_H_INSIDE__
44 
45 
46 G_BEGIN_DECLS
47 
48 
49 /**
50  * SECTION: gimpmath
51  * @title: GimpMath
52  * @short_description: Mathematical definitions and macros.
53  *
54  * Mathematical definitions and macros for use both by the GIMP
55  * application and plug-ins. These macros should be used rather than
56  * the ones from &lt;math.h&gt; for enhanced portability.
57  **/
58 
59 
60 /**
61  * RINT:
62  * @x: the value to be rounded
63  *
64  * This macro rounds its argument @x to an integer value in floating
65  * point format. Use RINT() instead of rint().
66  **/
67 #if defined (HAVE_RINT) && 0
68 /* note:  rint() depends on the current floating-point rounding mode.  when the
69  * rounding mode is FE_TONEAREST, it, in parctice, breaks ties to even.  this
70  * is different from 'floor (x + 0.5)', which breaks ties up.  in other words
71  * 'rint (2.5) == 2.0', while 'floor (2.5 + 0.5) == 3.0'.  this is asking for
72  * trouble, so let's just use the latter.
73  */
74 #define RINT(x) rint(x)
75 #else
76 #define RINT(x) floor ((x) + 0.5)
77 #endif
78 
79 /**
80  * ROUND:
81  * @x: the value to be rounded.
82  *
83  * This macro rounds its positive argument @x to the nearest integer.
84  **/
85 #define ROUND(x) ((int) ((x) + 0.5))
86 
87 /**
88  * SIGNED_ROUND:
89  * @x: the value to be rounded.
90  *
91  * This macro rounds its argument @x to the nearest integer.
92  **/
93 #define SIGNED_ROUND(x) ((int) RINT (x))
94 
95 /**
96  * SQR:
97  * @x: the value to be squared.
98  *
99  * This macro squares its argument @x.
100  **/
101 #define SQR(x) ((x) * (x))
102 
103 /**
104  * MAX255:
105  * @a: the value to be limited.
106  *
107  * This macro limits it argument @a, an (0-511) int, to 255.
108  **/
109 #define MAX255(a)  ((a) | (((a) & 256) - (((a) & 256) >> 8)))
110 
111 /**
112  * CLAMP0255:
113  * @a: the value to be clamped.
114  *
115  * This macro clamps its argument @a, an int32-range int, between 0
116  * and 255 inclusive.
117  **/
118 #define CLAMP0255(a)  CLAMP(a,0,255)
119 
120 /**
121  * SAFE_CLAMP:
122  * @x:    the value to be limited.
123  * @low:  the lower limit.
124  * @high: the upper limit.
125  *
126  * Ensures that @x is between the limits set by @low and @high,
127  * even if @x is NaN. If @low is greater than @high, or if either
128  * of them is NaN, the result is undefined.
129  *
130  * Since: 2.10
131  **/
132 #define SAFE_CLAMP(x, low, high)  ((x) > (low) ? (x) < (high) ? (x) : (high) : (low))
133 
134 /**
135  * gimp_deg_to_rad:
136  * @angle: the angle to be converted.
137  *
138  * This macro converts its argument @angle from degree to radian.
139  **/
140 #define gimp_deg_to_rad(angle) ((angle) * (2.0 * G_PI) / 360.0)
141 
142 /**
143  * gimp_rad_to_deg:
144  * @angle: the angle to be converted.
145  *
146  * This macro converts its argument @angle from radian to degree.
147  **/
148 #define gimp_rad_to_deg(angle) ((angle) * 360.0 / (2.0 * G_PI))
149 
150 
151 G_END_DECLS
152 
153 #endif /* __GIMP_MATH_H__ */
154