Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_rational.cpp

raw
1 /*****************************************************************************/
2 // Copyright 2006-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 #include "dng_rational.h"
10
11 #include "dng_utils.h"
12
13 /*****************************************************************************/
14
15 real64 dng_srational::As_real64 () const
16 {
17
18 if (d)
19 return (real64) n / (real64) d;
20
21 else
22 return 0.0;
23
24 }
25
26 /*****************************************************************************/
27
28 void dng_srational::Set_real64 (real64 x, int32 dd)
29 {
30
31 if (x == 0.0)
32 {
33
34 *this = dng_srational (0, 1);
35
36 }
37
38 if (dd == 0)
39 {
40
41 real64 y = Abs_real64 (x);
42
43 if (y >= 32768.0)
44 {
45 dd = 1;
46 }
47
48 else if (y >= 1.0)
49 {
50 dd = 32768;
51 }
52
53 else
54 {
55 dd = 32768 * 32768;
56 }
57
58 }
59
60 *this = dng_srational (Round_int32 (x * dd), dd);
61
62 }
63
64 /*****************************************************************************/
65
66 void dng_srational::ReduceByFactor (int32 factor)
67 {
68
69 while (n % factor == 0 &&
70 d % factor == 0 &&
71 d >= factor)
72 {
73 n /= factor;
74 d /= factor;
75 }
76
77 }
78
79 /*****************************************************************************/
80
81 real64 dng_urational::As_real64 () const
82 {
83
84 if (d)
85 return (real64) n / (real64) d;
86
87 else
88 return 0.0;
89
90 }
91
92 /*****************************************************************************/
93
94 void dng_urational::Set_real64 (real64 x, uint32 dd)
95 {
96
97 if (x <= 0.0)
98 {
99
100 *this = dng_urational (0, 1);
101
102 }
103
104 if (dd == 0)
105 {
106
107 if (x >= 32768.0)
108 {
109 dd = 1;
110 }
111
112 else if (x >= 1.0)
113 {
114 dd = 32768;
115 }
116
117 else
118 {
119 dd = 32768 * 32768;
120 }
121
122 }
123
124 *this = dng_urational (Round_uint32 (x * dd), dd);
125
126 }
127
128 /*****************************************************************************/
129
130 void dng_urational::ReduceByFactor (uint32 factor)
131 {
132
133 while (n % factor == 0 &&
134 d % factor == 0 &&
135 d >= factor)
136 {
137 n /= factor;
138 d /= factor;
139 }
140
141 }
142
143 /*****************************************************************************/
144
145 void dng_urational::ScaleBy (real64 scale)
146 {
147
148 if (scale <= 0.0)
149 {
150 *this = dng_urational (0, 1);
151 return;
152 }
153
154 if (IsValid ())
155 {
156
157 if (d > 1)
158 {
159 ReduceByFactor (d);
160 }
161
162 ReduceByFactor (2);
163 ReduceByFactor (3);
164 ReduceByFactor (5);
165
166 while (true)
167 {
168
169 if (scale == 1.0)
170 {
171 break;
172 }
173
174 else if (scale > 1.0)
175 {
176
177 uint32 x = Round_uint32 (scale);
178
179 if (scale == (real64) x)
180 {
181
182 if (d % x == 0)
183 {
184 d /= x;
185 break;
186 }
187
188 else if (n * (uint64) x <= 0xFFFFFFFF)
189 {
190 n *= x;
191 break;
192 }
193
194 }
195
196 }
197
198 else
199 {
200
201 uint32 x = Round_uint32 (1.0 / scale);
202
203 if (scale == (1.0 / (real64) x))
204 {
205
206 if (n % x == 0)
207 {
208 n /= x;
209 break;
210 }
211
212 else if (d * (uint64) x <= 0xFFFFFFFF)
213 {
214 d *= x;
215 break;
216 }
217
218 }
219
220 }
221
222 Set_real64 (As_real64 () * scale);
223
224 break;
225
226 }
227
228 ReduceByFactor (2);
229
230 }
231
232 }
233
234 /*****************************************************************************/