| 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 |
* Definition of a lookup table based 1D floating-point to floating-point function abstraction using linear interpolation. |
| 11 |
*/ |
| 12 |
|
| 13 |
/*****************************************************************************/ |
| 14 |
|
| 15 |
#ifndef __dng_1d_table__ |
| 16 |
#define __dng_1d_table__ |
| 17 |
|
| 18 |
/*****************************************************************************/ |
| 19 |
|
| 20 |
#include "dng_assertions.h" |
| 21 |
#include "dng_auto_ptr.h" |
| 22 |
#include "dng_classes.h" |
| 23 |
#include "dng_types.h" |
| 24 |
#include "dng_uncopyable.h" |
| 25 |
|
| 26 |
/*****************************************************************************/ |
| 27 |
|
| 28 |
/// @brief A 1D floating-point lookup table using linear interpolation. |
| 29 |
|
| 30 |
class dng_1d_table: private dng_uncopyable |
| 31 |
{ |
| 32 |
|
| 33 |
public: |
| 34 |
|
| 35 |
/// Constant denoting minimum size of table. |
| 36 |
|
| 37 |
static const uint32 kMinTableSize = 512; |
| 38 |
|
| 39 |
private: |
| 40 |
|
| 41 |
/// Constant denoting default size of table. |
| 42 |
|
| 43 |
static const uint32 kDefaultTableSize = 4096; |
| 44 |
|
| 45 |
protected: |
| 46 |
|
| 47 |
AutoPtr<dng_memory_block> fBuffer; |
| 48 |
|
| 49 |
real32 *fTable; |
| 50 |
|
| 51 |
const uint32 fTableCount; |
| 52 |
|
| 53 |
private: |
| 54 |
|
| 55 |
const real32 fTableCount32; |
| 56 |
|
| 57 |
public: |
| 58 |
|
| 59 |
/// Table constructor. count must be a power of two |
| 60 |
/// and at least kMinTableSize. |
| 61 |
|
| 62 |
explicit dng_1d_table (uint32 count = kDefaultTableSize); |
| 63 |
|
| 64 |
virtual ~dng_1d_table (); |
| 65 |
|
| 66 |
/// Number of table entries. |
| 67 |
|
| 68 |
uint32 Count () const |
| 69 |
{ |
| 70 |
return fTableCount; |
| 71 |
} |
| 72 |
|
| 73 |
/// Set up table, initialize entries using functiion. |
| 74 |
/// This method can throw an exception, e.g. if there is not enough memory. |
| 75 |
/// \param allocator Memory allocator from which table memory is allocated. |
| 76 |
/// \param function Table is initialized with values of function.Evaluate(0.0) to function.Evaluate(1.0). |
| 77 |
/// \param subSample If true, only sample the function a limited number of times and interpolate. |
| 78 |
|
| 79 |
void Initialize (dng_memory_allocator &allocator, |
| 80 |
const dng_1d_function &function, |
| 81 |
bool subSample = false); |
| 82 |
|
| 83 |
/// Lookup and interpolate mapping for an input. |
| 84 |
/// \param x value from 0.0 to 1.0 used as input for mapping |
| 85 |
/// \retval Approximation of function.Evaluate(x) |
| 86 |
|
| 87 |
real32 Interpolate (real32 x) const |
| 88 |
{ |
| 89 |
|
| 90 |
real32 y = x * fTableCount32; |
| 91 |
|
| 92 |
int32 index = (int32) y; |
| 93 |
|
| 94 |
if (index < 0 || index > (int32) fTableCount) |
| 95 |
{ |
| 96 |
|
| 97 |
ThrowBadFormat ("Index out of range."); |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
// Enable vectorization by using DNG_ASSERT instead of DNG_REQUIRE |
| 102 |
|
| 103 |
DNG_ASSERT (!(index < 0 || index > (int32) fTableCount), "dng_1d_table::Interpolate parameter out of range"); |
| 104 |
|
| 105 |
real32 z = (real32) index; |
| 106 |
|
| 107 |
real32 fract = y - z; |
| 108 |
|
| 109 |
return fTable [index ] * (1.0f - fract) + |
| 110 |
fTable [index + 1] * ( fract); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
/// Unsafe version of above for use in code that knows the input |
| 115 |
/// is limited to 0.0f to 1.0f. |
| 116 |
|
| 117 |
DNG_ALWAYS_INLINE real32 UnsafeInterpolate (real32 x) const |
| 118 |
{ |
| 119 |
|
| 120 |
real32 y = x * fTableCount32; |
| 121 |
|
| 122 |
int32 index = (int32) y; |
| 123 |
|
| 124 |
real32 z = (real32) index; |
| 125 |
|
| 126 |
real32 fract = y - z; |
| 127 |
|
| 128 |
return fTable [index ] * (1.0f - fract) + |
| 129 |
fTable [index + 1] * ( fract); |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
/// Direct access function for table data. |
| 134 |
|
| 135 |
const real32 * Table () const |
| 136 |
{ |
| 137 |
return fTable; |
| 138 |
} |
| 139 |
|
| 140 |
/// Expand the table to a 16-bit to 16-bit table. |
| 141 |
|
| 142 |
void Expand16 (uint16 *table16) const; |
| 143 |
|
| 144 |
private: |
| 145 |
|
| 146 |
void SubDivide (const dng_1d_function &function, |
| 147 |
uint32 lower, |
| 148 |
uint32 upper, |
| 149 |
real32 maxDelta); |
| 150 |
|
| 151 |
}; |
| 152 |
|
| 153 |
/*****************************************************************************/ |
| 154 |
|
| 155 |
#endif |
| 156 |
|
| 157 |
/*****************************************************************************/ |