| 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_table.h" |
| 10 |
|
| 11 |
#include "dng_1d_function.h" |
| 12 |
#include "dng_assertions.h" |
| 13 |
#include "dng_memory.h" |
| 14 |
#include "dng_safe_arithmetic.h" |
| 15 |
#include "dng_utils.h" |
| 16 |
|
| 17 |
/*****************************************************************************/ |
| 18 |
|
| 19 |
dng_1d_table::dng_1d_table (uint32 count) |
| 20 |
|
| 21 |
: fBuffer () |
| 22 |
, fTable (NULL) |
| 23 |
, fTableCount (count) |
| 24 |
, fTableCount32 ((real32) count) |
| 25 |
|
| 26 |
{ |
| 27 |
|
| 28 |
DNG_REQUIRE (count >= kMinTableSize, |
| 29 |
"count must be at least kMinTableSize"); |
| 30 |
|
| 31 |
DNG_REQUIRE ((count & (count - 1)) == 0, |
| 32 |
"count must be power of 2"); |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
/*****************************************************************************/ |
| 37 |
|
| 38 |
dng_1d_table::~dng_1d_table () |
| 39 |
{ |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
/*****************************************************************************/ |
| 44 |
|
| 45 |
void dng_1d_table::SubDivide (const dng_1d_function &function, |
| 46 |
uint32 lower, |
| 47 |
uint32 upper, |
| 48 |
real32 maxDelta) |
| 49 |
{ |
| 50 |
|
| 51 |
uint32 range = upper - lower; |
| 52 |
|
| 53 |
bool subDivide = (range > (fTableCount >> 8)); |
| 54 |
|
| 55 |
if (!subDivide) |
| 56 |
{ |
| 57 |
|
| 58 |
real32 delta = Abs_real32 (fTable [upper] - |
| 59 |
fTable [lower]); |
| 60 |
|
| 61 |
if (delta > maxDelta) |
| 62 |
{ |
| 63 |
|
| 64 |
subDivide = true; |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
if (subDivide) |
| 71 |
{ |
| 72 |
|
| 73 |
uint32 middle = (lower + upper) >> 1; |
| 74 |
|
| 75 |
fTable [middle] = (real32) function.Evaluate (middle * (1.0 / (real64) fTableCount)); |
| 76 |
|
| 77 |
if (range > 2) |
| 78 |
{ |
| 79 |
|
| 80 |
SubDivide (function, lower, middle, maxDelta); |
| 81 |
|
| 82 |
SubDivide (function, middle, upper, maxDelta); |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
else |
| 89 |
{ |
| 90 |
|
| 91 |
real64 y0 = fTable [lower]; |
| 92 |
real64 y1 = fTable [upper]; |
| 93 |
|
| 94 |
real64 delta = (y1 - y0) / (real64) range; |
| 95 |
|
| 96 |
for (uint32 j = lower + 1; j < upper; j++) |
| 97 |
{ |
| 98 |
|
| 99 |
y0 += delta; |
| 100 |
|
| 101 |
fTable [j] = (real32) y0; |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
/*****************************************************************************/ |
| 110 |
|
| 111 |
void dng_1d_table::Initialize (dng_memory_allocator &allocator, |
| 112 |
const dng_1d_function &function, |
| 113 |
bool subSample) |
| 114 |
{ |
| 115 |
|
| 116 |
fBuffer.Reset (allocator.Allocate ((fTableCount + 2) * sizeof (real32))); |
| 117 |
|
| 118 |
fTable = fBuffer->Buffer_real32 (); |
| 119 |
|
| 120 |
if (subSample) |
| 121 |
{ |
| 122 |
|
| 123 |
fTable [0 ] = (real32) function.Evaluate (0.0); |
| 124 |
fTable [fTableCount] = (real32) function.Evaluate (1.0); |
| 125 |
|
| 126 |
real32 maxDelta = Max_real32 (Abs_real32 (fTable [fTableCount] - |
| 127 |
fTable [0 ]), 1.0f) * |
| 128 |
(1.0f / 256.0f); |
| 129 |
|
| 130 |
SubDivide (function, |
| 131 |
0, |
| 132 |
fTableCount, |
| 133 |
maxDelta); |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
else |
| 138 |
{ |
| 139 |
|
| 140 |
for (uint32 j = 0; j <= fTableCount; j++) |
| 141 |
{ |
| 142 |
|
| 143 |
real64 x = j * (1.0 / (real64) fTableCount); |
| 144 |
|
| 145 |
real64 y = function.Evaluate (x); |
| 146 |
|
| 147 |
fTable [j] = ConvertDoubleToFloat (y); |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
fTable [fTableCount + 1] = fTable [fTableCount]; |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
/*****************************************************************************/ |
| 158 |
|
| 159 |
void dng_1d_table::Expand16 (uint16 *table16) const |
| 160 |
{ |
| 161 |
|
| 162 |
real64 step = (real64) fTableCount / 65535.0; |
| 163 |
|
| 164 |
real64 y0 = fTable [0]; |
| 165 |
real64 y1 = fTable [1]; |
| 166 |
|
| 167 |
real64 base = y0 * 65535.0 + 0.5; |
| 168 |
real64 slope = (y1 - y0) * 65535.0; |
| 169 |
|
| 170 |
uint32 index = 1; |
| 171 |
real64 fract = 0.0; |
| 172 |
|
| 173 |
for (uint32 j = 0; j < 0x10000; j++) |
| 174 |
{ |
| 175 |
|
| 176 |
table16 [j] = (uint16) (base + slope * fract); |
| 177 |
|
| 178 |
fract += step; |
| 179 |
|
| 180 |
if (fract > 1.0) |
| 181 |
{ |
| 182 |
|
| 183 |
index += 1; |
| 184 |
fract -= 1.0; |
| 185 |
|
| 186 |
y0 = y1; |
| 187 |
y1 = fTable [index]; |
| 188 |
|
| 189 |
base = y0 * 65535.0 + 0.5; |
| 190 |
slope = (y1 - y0) * 65535.0; |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
/*****************************************************************************/ |