| 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 |
/** \file |
| 10 |
* Classes for a 1D floating-point to floating-point function abstraction. |
| 11 |
*/ |
| 12 |
|
| 13 |
/*****************************************************************************/ |
| 14 |
|
| 15 |
#ifndef __dng_1d_function__ |
| 16 |
#define __dng_1d_function__ |
| 17 |
|
| 18 |
/*****************************************************************************/ |
| 19 |
|
| 20 |
#include "dng_classes.h" |
| 21 |
#include "dng_types.h" |
| 22 |
|
| 23 |
#include <vector> |
| 24 |
|
| 25 |
/*****************************************************************************/ |
| 26 |
|
| 27 |
/// @brief A 1D floating-point function. |
| 28 |
/// |
| 29 |
/// The domain (input) is always from 0.0 to 1.0, while the range (output) can |
| 30 |
/// be an arbitrary interval. |
| 31 |
|
| 32 |
class dng_1d_function |
| 33 |
{ |
| 34 |
|
| 35 |
public: |
| 36 |
|
| 37 |
virtual ~dng_1d_function (); |
| 38 |
|
| 39 |
/// Returns true if this function is the map x -> y such that x == y for all x. |
| 40 |
/// That is if Evaluate(x) == x for all x. |
| 41 |
|
| 42 |
virtual bool IsIdentity () const; |
| 43 |
|
| 44 |
/// Return the mapping for value x. |
| 45 |
/// This method must be implemented by a derived class of dng_1d_function and |
| 46 |
/// the derived class determines the lookup method and function used. |
| 47 |
/// \param x A value between 0.0 and 1.0 (inclusive). |
| 48 |
/// \retval Mapped value for x |
| 49 |
|
| 50 |
virtual real64 Evaluate (real64 x) const = 0; |
| 51 |
|
| 52 |
/// Return the reverse mapped value for y. |
| 53 |
/// This method can be implemented by derived classes. The default implementation |
| 54 |
/// uses Newton's method to solve for x such that Evaluate(x) == y. |
| 55 |
/// \param y A value to reverse map. Should be within the range of the function |
| 56 |
/// implemented by this dng_1d_function. |
| 57 |
/// \retval A value x such that Evaluate(x) == y (to very close approximation). |
| 58 |
|
| 59 |
virtual real64 EvaluateInverse (real64 y) const; |
| 60 |
|
| 61 |
}; |
| 62 |
|
| 63 |
/*****************************************************************************/ |
| 64 |
|
| 65 |
/// An identity (x -> y such that x == y for all x) mapping function. |
| 66 |
|
| 67 |
class dng_1d_identity: public dng_1d_function |
| 68 |
{ |
| 69 |
|
| 70 |
public: |
| 71 |
/// Always returns true for this class. |
| 72 |
|
| 73 |
virtual bool IsIdentity () const; |
| 74 |
|
| 75 |
/// Always returns x for this class. |
| 76 |
|
| 77 |
virtual real64 Evaluate (real64 x) const; |
| 78 |
|
| 79 |
/// Always returns y for this class. |
| 80 |
|
| 81 |
virtual real64 EvaluateInverse (real64 y) const; |
| 82 |
|
| 83 |
/// This class is a singleton, and is entirely threadsafe. Use this |
| 84 |
/// method to get an instance of the class. |
| 85 |
|
| 86 |
static const dng_1d_function & Get (); |
| 87 |
|
| 88 |
}; |
| 89 |
|
| 90 |
/*****************************************************************************/ |
| 91 |
|
| 92 |
/// A dng_1d_function that represents the composition (curry) of two other |
| 93 |
/// dng_1d_functions. |
| 94 |
|
| 95 |
class dng_1d_concatenate: public dng_1d_function |
| 96 |
{ |
| 97 |
|
| 98 |
protected: |
| 99 |
|
| 100 |
const dng_1d_function &fFunction1; |
| 101 |
|
| 102 |
const dng_1d_function &fFunction2; |
| 103 |
|
| 104 |
public: |
| 105 |
|
| 106 |
/// Create a dng_1d_function which computes |
| 107 |
/// |
| 108 |
/// y = function2.Evaluate (function1.Evaluate (x)) |
| 109 |
/// |
| 110 |
/// The range of function1.Evaluate must be a subset of 0.0 to 1.0 inclusive, |
| 111 |
/// otherwise the result of function1(x) will be pinned (clipped) to 0.0 |
| 112 |
/// if < 0.0 and to 1.0 if > 1.0. |
| 113 |
/// |
| 114 |
/// \param function1 Inner function of composition. |
| 115 |
/// \param function2 Outer function of composition. |
| 116 |
|
| 117 |
dng_1d_concatenate (const dng_1d_function &function1, |
| 118 |
const dng_1d_function &function2); |
| 119 |
|
| 120 |
/// Only true if both function1 and function2 have IsIdentity equal to true. |
| 121 |
|
| 122 |
virtual bool IsIdentity () const; |
| 123 |
|
| 124 |
/// Return the composed mapping for value x. |
| 125 |
/// \param x A value between 0.0 and 1.0 (inclusive). |
| 126 |
/// \retval function2.Evaluate(function1.Evaluate(x)). |
| 127 |
|
| 128 |
virtual real64 Evaluate (real64 x) const; |
| 129 |
|
| 130 |
/// Return the reverse mapped value for y. |
| 131 |
/// Be careful using this method with compositions where the inner function |
| 132 |
/// does not have a range 0.0 to 1.0 . (Or better yet, do not use such functions.) |
| 133 |
/// \param y A value to reverse map. Should be within the range of function2.Evaluate. |
| 134 |
/// \retval A value x such that function2.Evaluate(function1.Evaluate(x)) == y |
| 135 |
/// (to very close approximation). |
| 136 |
|
| 137 |
virtual real64 EvaluateInverse (real64 y) const; |
| 138 |
|
| 139 |
}; |
| 140 |
|
| 141 |
/*****************************************************************************/ |
| 142 |
|
| 143 |
/// A dng_1d_function that represents the inverse of another dng_1d_function. |
| 144 |
|
| 145 |
class dng_1d_inverse: public dng_1d_function |
| 146 |
{ |
| 147 |
|
| 148 |
protected: |
| 149 |
|
| 150 |
const dng_1d_function &fFunction; |
| 151 |
|
| 152 |
public: |
| 153 |
|
| 154 |
dng_1d_inverse (const dng_1d_function &f); |
| 155 |
|
| 156 |
virtual bool IsIdentity () const; |
| 157 |
|
| 158 |
virtual real64 Evaluate (real64 x) const; |
| 159 |
|
| 160 |
virtual real64 EvaluateInverse (real64 y) const; |
| 161 |
|
| 162 |
}; |
| 163 |
|
| 164 |
/*****************************************************************************/ |
| 165 |
|
| 166 |
// Piecewise-linear 1D function. |
| 167 |
|
| 168 |
class dng_piecewise_linear: public dng_1d_function |
| 169 |
{ |
| 170 |
|
| 171 |
public: |
| 172 |
|
| 173 |
std::vector<real64> X; |
| 174 |
std::vector<real64> Y; |
| 175 |
|
| 176 |
public: |
| 177 |
|
| 178 |
dng_piecewise_linear (); |
| 179 |
|
| 180 |
virtual ~dng_piecewise_linear (); |
| 181 |
|
| 182 |
void Reset (); |
| 183 |
|
| 184 |
void Add (real64 x, real64 y); |
| 185 |
|
| 186 |
bool IsValid () const |
| 187 |
{ |
| 188 |
return (X.size () >= 2) && (X.size () == Y.size ()); |
| 189 |
} |
| 190 |
|
| 191 |
bool NotValid () const |
| 192 |
{ |
| 193 |
return !IsValid (); |
| 194 |
} |
| 195 |
|
| 196 |
virtual bool IsIdentity () const; |
| 197 |
|
| 198 |
virtual real64 Evaluate (real64 x) const; |
| 199 |
|
| 200 |
virtual real64 EvaluateInverse (real64 y) const; |
| 201 |
|
| 202 |
void PutFingerprintData (dng_stream &stream) const; |
| 203 |
|
| 204 |
bool operator== (const dng_piecewise_linear &piecewise) const; |
| 205 |
|
| 206 |
bool operator!= (const dng_piecewise_linear &piecewise) const |
| 207 |
{ |
| 208 |
return !(*this == piecewise); |
| 209 |
} |
| 210 |
|
| 211 |
}; |
| 212 |
|
| 213 |
/*****************************************************************************/ |
| 214 |
|
| 215 |
#endif |
| 216 |
|
| 217 |
/*****************************************************************************/ |