Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_1d_function.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_1d_function.h"
10
11 #include "dng_assertions.h"
12 #include "dng_stream.h"
13 #include "dng_utils.h"
14
15 /*****************************************************************************/
16
17 dng_1d_function::~dng_1d_function ()
18 {
19
20 }
21
22 /*****************************************************************************/
23
24 bool dng_1d_function::IsIdentity () const
25 {
26
27 return false;
28
29 }
30
31 /*****************************************************************************/
32
33 real64 dng_1d_function::EvaluateInverse (real64 y) const
34 {
35
36 const uint32 kMaxIterations = 30;
37 const real64 kNearZero = 1.0e-10;
38
39 real64 x0 = 0.0;
40 real64 y0 = Evaluate (x0);
41
42 real64 x1 = 1.0;
43 real64 y1 = Evaluate (x1);
44
45 for (uint32 iteration = 0; iteration < kMaxIterations; iteration++)
46 {
47
48 if (Abs_real64 (y1 - y0) < kNearZero)
49 {
50 break;
51 }
52
53 real64 x2 = Pin_real64 (0.0,
54 x1 + (y - y1) * (x1 - x0) / (y1 - y0),
55 1.0);
56
57 real64 y2 = Evaluate (x2);
58
59 x0 = x1;
60 y0 = y1;
61
62 x1 = x2;
63 y1 = y2;
64
65 }
66
67 return x1;
68
69 }
70
71 /*****************************************************************************/
72
73 bool dng_1d_identity::IsIdentity () const
74 {
75
76 return true;
77
78 }
79
80 /*****************************************************************************/
81
82 real64 dng_1d_identity::Evaluate (real64 x) const
83 {
84
85 return x;
86
87 }
88
89 /*****************************************************************************/
90
91 real64 dng_1d_identity::EvaluateInverse (real64 x) const
92 {
93
94 return x;
95
96 }
97
98 /*****************************************************************************/
99
100 const dng_1d_function & dng_1d_identity::Get ()
101 {
102
103 static dng_1d_identity static_function;
104
105 return static_function;
106
107 }
108
109 /*****************************************************************************/
110
111 dng_1d_concatenate::dng_1d_concatenate (const dng_1d_function &function1,
112 const dng_1d_function &function2)
113
114 : fFunction1 (function1)
115 , fFunction2 (function2)
116
117 {
118
119 }
120
121 /*****************************************************************************/
122
123 bool dng_1d_concatenate::IsIdentity () const
124 {
125
126 return fFunction1.IsIdentity () &&
127 fFunction2.IsIdentity ();
128
129 }
130
131 /*****************************************************************************/
132
133 real64 dng_1d_concatenate::Evaluate (real64 x) const
134 {
135
136 real64 y = Pin_real64 (0.0, fFunction1.Evaluate (x), 1.0);
137
138 return fFunction2.Evaluate (y);
139
140 }
141
142 /*****************************************************************************/
143
144 real64 dng_1d_concatenate::EvaluateInverse (real64 x) const
145 {
146
147 real64 y = fFunction2.EvaluateInverse (x);
148
149 return fFunction1.EvaluateInverse (y);
150
151 }
152
153 /*****************************************************************************/
154
155 dng_1d_inverse::dng_1d_inverse (const dng_1d_function &f)
156
157 : fFunction (f)
158
159 {
160
161 }
162
163 /*****************************************************************************/
164
165 bool dng_1d_inverse::IsIdentity () const
166 {
167
168 return fFunction.IsIdentity ();
169
170 }
171
172 /*****************************************************************************/
173
174 real64 dng_1d_inverse::Evaluate (real64 x) const
175 {
176
177 return fFunction.EvaluateInverse (x);
178
179 }
180
181 /*****************************************************************************/
182
183 real64 dng_1d_inverse::EvaluateInverse (real64 y) const
184 {
185
186 return fFunction.Evaluate (y);
187
188 }
189
190 /*****************************************************************************/
191
192 dng_piecewise_linear::dng_piecewise_linear ()
193
194 : X ()
195 , Y ()
196
197 {
198
199 }
200
201 /*****************************************************************************/
202
203 dng_piecewise_linear::~dng_piecewise_linear ()
204 {
205
206 }
207
208 /*****************************************************************************/
209
210 void dng_piecewise_linear::Reset ()
211 {
212
213 X.clear ();
214 Y.clear ();
215
216 }
217
218 /*****************************************************************************/
219
220 void dng_piecewise_linear::Add (real64 x, real64 y)
221 {
222
223 X.push_back (x);
224 Y.push_back (y);
225
226 }
227
228 /*****************************************************************************/
229
230 bool dng_piecewise_linear::IsIdentity () const
231 {
232
233 return (X.size () == 2 &&
234 X.size () == Y.size () &&
235 X [0] == 0.0 && Y [0] == 0.0 &&
236 X [1] == 1.0 && Y [1] == 1.0);
237
238 }
239
240 /*****************************************************************************/
241
242 real64 dng_piecewise_linear::Evaluate (real64 x) const
243 {
244
245 DNG_ASSERT (X.size () >= 2, "Too few points.");
246
247 DNG_ASSERT (X.size () == Y.size (), "Input/output vector size mismatch.");
248
249 // Check for extremes.
250
251 if (x <= X.front ())
252 {
253 return Y.front ();
254 }
255
256 else if (x >= X.back ())
257 {
258 return Y.back ();
259 }
260
261 // Binary search for the X index.
262
263 int32 lower = 1;
264 int32 upper = ((int32) (X.size ())) - 1;
265
266 while (upper > lower)
267 {
268
269 int32 mid = (lower + upper) >> 1;
270
271 if (x == X [mid]) return Y [mid];
272
273 if (x > X [mid]) lower = mid + 1;
274
275 else upper = mid;
276
277 }
278
279 DNG_ASSERT (upper == lower, "Binary search error in point list.");
280
281 int32 index0 = lower - 1;
282 int32 index1 = lower;
283
284 real64 X0 = X [index0];
285 real64 X1 = X [index1];
286
287 real64 Y0 = Y [index0];
288 real64 Y1 = Y [index1];
289
290 if (X0 == X1) return 0.5 * (Y0 + Y1);
291
292 real64 t = (x - X0) / (X1 - X0);
293
294 return Y0 + t * (Y1 - Y0);
295
296 }
297
298 /*****************************************************************************/
299
300 real64 dng_piecewise_linear::EvaluateInverse (real64 y) const
301 {
302
303 DNG_ASSERT (X.size () >= 2, "Too few points.");
304
305 DNG_ASSERT (X.size () == Y.size (), "Input/output vector size mismatch.");
306
307 // Binary search for the Y index.
308
309 int32 lower = 1;
310 int32 upper = ((int32) (Y.size ())) - 1;
311
312 while (upper > lower)
313 {
314
315 int32 mid = (lower + upper) >> 1;
316
317 if (y == Y [mid]) return X [mid];
318
319 if (y > Y [mid]) lower = mid + 1;
320
321 else upper = mid;
322
323 }
324
325 DNG_ASSERT (upper == lower, "Binary search error in point list.");
326
327 int32 index0 = lower - 1;
328 int32 index1 = lower;
329
330 real64 X0 = X [index0];
331 real64 X1 = X [index1];
332
333 real64 Y0 = Y [index0];
334 real64 Y1 = Y [index1];
335
336 if (Y0 == Y1) return 0.5 * (X0 + X1);
337
338 real64 t = (y - Y0) / (Y1 - Y0);
339
340 return X0 + t * (X1 - X0);
341
342 }
343
344 /*****************************************************************************/
345
346 void dng_piecewise_linear::PutFingerprintData (dng_stream &stream) const
347 {
348
349 const char *name = "dng_piecewise_linear";
350
351 stream.Put (name, (uint32) strlen (name));
352
353 if (IsValid ())
354 {
355
356 for (size_t i = 0; i < X.size (); i++)
357 {
358
359 stream.Put_real64 (X [i]);
360 stream.Put_real64 (Y [i]);
361
362 }
363
364 }
365
366 }
367
368 /*****************************************************************************/
369
370 bool dng_piecewise_linear::operator== (const dng_piecewise_linear &piecewise) const
371 {
372
373 return X == piecewise.X &&
374 Y == piecewise.Y;
375
376 }
377
378 /*****************************************************************************/