Commit | Line | Data |
---|---|---|
3719602c PC |
1 | /* Copyright (C) 2010-2020 The RetroArch team |
2 | * | |
3 | * --------------------------------------------------------------------------------------- | |
4 | * The following license statement only applies to this file (retro_math.h). | |
5 | * --------------------------------------------------------------------------------------- | |
6 | * | |
7 | * Permission is hereby granted, free of charge, | |
8 | * to any person obtaining a copy of this software and associated documentation files (the "Software"), | |
9 | * to deal in the Software without restriction, including without limitation the rights to | |
10 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
11 | * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
16 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
19 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
21 | */ | |
22 | ||
23 | #ifndef _LIBRETRO_COMMON_MATH_H | |
24 | #define _LIBRETRO_COMMON_MATH_H | |
25 | ||
26 | #include <stdint.h> | |
27 | ||
28 | #if defined(_WIN32) && !defined(_XBOX) | |
29 | #define WIN32_LEAN_AND_MEAN | |
30 | #include <windows.h> | |
31 | #elif defined(_WIN32) && defined(_XBOX) | |
32 | #include <Xtl.h> | |
33 | #endif | |
34 | ||
35 | #include <limits.h> | |
36 | ||
37 | #ifdef _MSC_VER | |
38 | #include <compat/msvc.h> | |
39 | #endif | |
40 | #include <retro_inline.h> | |
41 | ||
42 | #ifndef M_PI | |
43 | #if !defined(USE_MATH_DEFINES) | |
44 | #define M_PI 3.14159265358979323846264338327 | |
45 | #endif | |
46 | #endif | |
47 | ||
48 | #ifndef MAX | |
49 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) | |
50 | #endif | |
51 | ||
52 | #ifndef MIN | |
53 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
54 | #endif | |
55 | ||
56 | /** | |
57 | * next_pow2: | |
58 | * @v : initial value | |
59 | * | |
60 | * Get next power of 2 value based on initial value. | |
61 | * | |
62 | * Returns: next power of 2 value (derived from @v). | |
63 | **/ | |
64 | static INLINE uint32_t next_pow2(uint32_t v) | |
65 | { | |
66 | v--; | |
67 | v |= v >> 1; | |
68 | v |= v >> 2; | |
69 | v |= v >> 4; | |
70 | v |= v >> 8; | |
71 | v |= v >> 16; | |
72 | v++; | |
73 | return v; | |
74 | } | |
75 | ||
76 | /** | |
77 | * prev_pow2: | |
78 | * @v : initial value | |
79 | * | |
80 | * Get previous power of 2 value based on initial value. | |
81 | * | |
82 | * Returns: previous power of 2 value (derived from @v). | |
83 | **/ | |
84 | static INLINE uint32_t prev_pow2(uint32_t v) | |
85 | { | |
86 | v |= v >> 1; | |
87 | v |= v >> 2; | |
88 | v |= v >> 4; | |
89 | v |= v >> 8; | |
90 | v |= v >> 16; | |
91 | return v - (v >> 1); | |
92 | } | |
93 | ||
94 | /** | |
95 | * clamp: | |
96 | * @v : initial value | |
97 | * | |
98 | * Get the clamped value based on initial value. | |
99 | * | |
100 | * Returns: clamped value (derived from @v). | |
101 | **/ | |
102 | static INLINE float clamp_value(float v, float min, float max) | |
103 | { | |
104 | return v <= min ? min : v >= max ? max : v; | |
105 | } | |
106 | ||
107 | /** | |
108 | * saturate_value: | |
109 | * @v : initial value | |
110 | * | |
111 | * Get the clamped 0.0-1.0 value based on initial value. | |
112 | * | |
113 | * Returns: clamped 0.0-1.0 value (derived from @v). | |
114 | **/ | |
115 | static INLINE float saturate_value(float v) | |
116 | { | |
117 | return clamp_value(v, 0.0f, 1.0f); | |
118 | } | |
119 | ||
120 | /** | |
121 | * dot_product: | |
122 | * @a : left hand vector value | |
123 | * @b : right hand vector value | |
124 | * | |
125 | * Get the dot product of the two passed in vectors. | |
126 | * | |
127 | * Returns: dot product value (derived from @a and @b). | |
128 | **/ | |
129 | static INLINE float dot_product(const float* a, const float* b) | |
130 | { | |
131 | return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]); | |
132 | } | |
133 | ||
134 | /** | |
135 | * convert_rgb_to_yxy: | |
136 | * @rgb : in RGB colour space value | |
137 | * @Yxy : out Yxy colour space value | |
138 | * | |
139 | * Convert from RGB colour space to Yxy colour space. | |
140 | * | |
141 | * Returns: Yxy colour space value (derived from @rgb). | |
142 | **/ | |
143 | static INLINE void convert_rgb_to_yxy(const float* rgb, float* Yxy) | |
144 | { | |
145 | float inv; | |
146 | float xyz[3]; | |
147 | float one[3] = {1.0, 1.0, 1.0}; | |
148 | float rgb_xyz[3][3] = { | |
149 | {0.4124564, 0.3575761, 0.1804375}, | |
150 | {0.2126729, 0.7151522, 0.0721750}, | |
151 | {0.0193339, 0.1191920, 0.9503041} | |
152 | }; | |
153 | ||
154 | xyz[0] = dot_product(rgb_xyz[0], rgb); | |
155 | xyz[1] = dot_product(rgb_xyz[1], rgb); | |
156 | xyz[2] = dot_product(rgb_xyz[2], rgb); | |
157 | ||
158 | inv = 1.0f / dot_product(xyz, one); | |
159 | Yxy[0] = xyz[1]; | |
160 | Yxy[1] = xyz[0] * inv; | |
161 | Yxy[2] = xyz[1] * inv; | |
162 | } | |
163 | ||
164 | /** | |
165 | * convert_yxy_to_rgb: | |
166 | * @rgb : in Yxy colour space value | |
167 | * @Yxy : out rgb colour space value | |
168 | * | |
169 | * Convert from Yxy colour space to rgb colour space. | |
170 | * | |
171 | * Returns: rgb colour space value (derived from @Yxy). | |
172 | **/ | |
173 | static INLINE void convert_yxy_to_rgb(const float* Yxy, float* rgb) | |
174 | { | |
175 | float xyz[3]; | |
176 | float xyz_rgb[3][3] = { | |
177 | {3.2404542, -1.5371385, -0.4985314}, | |
178 | {-0.9692660, 1.8760108, 0.0415560}, | |
179 | {0.0556434, -0.2040259, 1.0572252} | |
180 | }; | |
181 | xyz[0] = Yxy[0] * Yxy[1] / Yxy[2]; | |
182 | xyz[1] = Yxy[0]; | |
183 | xyz[2] = Yxy[0] * (1.0 - Yxy[1] - Yxy[2]) / Yxy[2]; | |
184 | ||
185 | rgb[0] = dot_product(xyz_rgb[0], xyz); | |
186 | rgb[1] = dot_product(xyz_rgb[1], xyz); | |
187 | rgb[2] = dot_product(xyz_rgb[2], xyz); | |
188 | } | |
189 | ||
190 | #endif |