summaryrefslogtreecommitdiff
path: root/gpr/source/lib/dng_sdk/dng_1d_table.cpp
blob: 267078e8af094cbcd0fa696f312b9c30072d45a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*****************************************************************************/
// Copyright 2006-2008 Adobe Systems Incorporated
// All Rights Reserved.
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in
// accordance with the terms of the Adobe license agreement accompanying it.
/*****************************************************************************/

/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_1d_table.cpp#1 $ */ 
/* $DateTime: 2012/05/30 13:28:51 $ */
/* $Change: 832332 $ */
/* $Author: tknoll $ */

/*****************************************************************************/

#include "dng_1d_table.h"

#include "dng_1d_function.h"
#include "dng_memory.h"
#include "dng_utils.h"

/*****************************************************************************/

dng_1d_table::dng_1d_table ()

	:	fBuffer ()
	,	fTable  (NULL)
	
	{
	
	}

/*****************************************************************************/

dng_1d_table::~dng_1d_table ()
	{
	
	}
	
/*****************************************************************************/

void dng_1d_table::SubDivide (const dng_1d_function &function,
							  uint32 lower,
							  uint32 upper,
							  real32 maxDelta)
	{
	
	uint32 range = upper - lower;
		
	bool subDivide = (range > (kTableSize >> 8));
	
	if (!subDivide)
		{
		
		real32 delta = Abs_real32 (fTable [upper] - 
								   fTable [lower]);
								   
		if (delta > maxDelta)
			{
			
			subDivide = true;
			
			}
		
		}
		
	if (subDivide)
		{
		
		uint32 middle = (lower + upper) >> 1;
		
		fTable [middle] = (real32) function.Evaluate (middle * (1.0 / (real64) kTableSize));
		
		if (range > 2)
			{
			
			SubDivide (function, lower, middle, maxDelta);
			
			SubDivide (function, middle, upper, maxDelta);
			
			}
	
		}
		
	else
		{
		
		real64 y0 = fTable [lower];
		real64 y1 = fTable [upper];
		
		real64 delta = (y1 - y0) / (real64) range;
		
		for (uint32 j = lower + 1; j < upper; j++)
			{
			
			y0 += delta;
				
			fTable [j] = (real32) y0;
						
			}
		
		}
		
	}
	
/*****************************************************************************/

void dng_1d_table::Initialize (dng_memory_allocator &allocator,
							   const dng_1d_function &function,
							   bool subSample)
	{
	
	fBuffer.Reset (allocator.Allocate ((kTableSize + 2) * sizeof (real32)));
	
	fTable = fBuffer->Buffer_real32 ();
	
	if (subSample)
		{
		
		fTable [0         ] = (real32) function.Evaluate (0.0);
		fTable [kTableSize] = (real32) function.Evaluate (1.0);
		
		real32 maxDelta = Max_real32 (Abs_real32 (fTable [kTableSize] -
												  fTable [0         ]), 1.0f) *
						  (1.0f / 256.0f);
							   
		SubDivide (function,
				   0,
				   kTableSize,
				   maxDelta);
		
		}
		
	else
		{
			
		for (uint32 j = 0; j <= kTableSize; j++)
			{
			
			real64 x = j * (1.0 / (real64) kTableSize);
			
			real64 y = function.Evaluate (x);
			
			fTable [j] = (real32) y;
			
			}
			
		}
		
	fTable [kTableSize + 1] = fTable [kTableSize];
	
	}

/*****************************************************************************/

void dng_1d_table::Expand16 (uint16 *table16) const
	{
	
	real64 step = (real64) kTableSize / 65535.0;
	
	real64 y0 = fTable [0];
	real64 y1 = fTable [1];
	
	real64 base  = y0 * 65535.0 + 0.5;
	real64 slope = (y1 - y0) * 65535.0;
	
	uint32 index = 1;
	real64 fract = 0.0;
	
	for (uint32 j = 0; j < 0x10000; j++)
		{
		
		table16 [j] = (uint16) (base + slope * fract);
		
		fract += step;
		
		if (fract > 1.0)
			{
			
			index += 1;
			fract -= 1.0;
			
			y0 = y1;
			y1 = fTable [index];
			
			base  = y0 * 65535.0 + 0.5;
			slope = (y1 - y0) * 65535.0;
			
			}
		
		}
	
	}

/*****************************************************************************/