summaryrefslogtreecommitdiff
path: root/gpr/source/lib/dng_sdk/dng_1d_table.h
blob: e24972a63f5007f09e7ed67d4dac3994870ac9bf (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
/*****************************************************************************/
// 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.h#1 $ */ 
/* $DateTime: 2012/05/30 13:28:51 $ */
/* $Change: 832332 $ */
/* $Author: tknoll $ */

/** \file
 * Definition of a lookup table based 1D floating-point to floating-point function abstraction using linear interpolation.
 */

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

#ifndef __dng_1d_table__
#define __dng_1d_table__

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

#include "dng_assertions.h"
#include "dng_auto_ptr.h"
#include "dng_classes.h"
#include "dng_types.h"

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

/// \brief A 1D floating-point lookup table using linear interpolation.

class dng_1d_table
	{
	
	public:
	
		/// Constants denoting size of table.

		enum
			{
			kTableBits = 12,				//< Table is always a power of 2 in size. This is log2(kTableSize).
			kTableSize = (1 << kTableBits)	//< Number of entries in table.
			};
			
	protected:
	
		AutoPtr<dng_memory_block> fBuffer;
		
		real32 *fTable;
	
	public:
	
		dng_1d_table ();
			
		virtual ~dng_1d_table ();

		/// Set up table, initialize entries using functiion.
		/// This method can throw an exception, e.g. if there is not enough memory.
		/// \param allocator Memory allocator from which table memory is allocated.
		/// \param function Table is initialized with values of finction.Evalluate(0.0) to function.Evaluate(1.0).
		/// \param subSample If true, only sample the function a limited number of times and interpolate.

		void Initialize (dng_memory_allocator &allocator,
						 const dng_1d_function &function,
						 bool subSample = false);

		/// Lookup and interpolate mapping for an input.
		/// \param x value from 0.0 to 1.0 used as input for mapping
		/// \retval Approximation of function.Evaluate(x)

		real32 Interpolate (real32 x) const
			{
			
			real32 y = x * (real32) kTableSize;
			
			int32 index = (int32) y;
			
			DNG_ASSERT (index >= 0 && index <= kTableSize,
						"dng_1d_table::Interpolate parameter out of range");
					
			real32 z = (real32) index;
						
			real32 fract = y - z;
			
			return fTable [index    ] * (1.0f - fract) +
				   fTable [index + 1] * (       fract);
			
			}
			
		/// Direct access function for table data.
			
		const real32 * Table () const
			{
			return fTable;
			}
			
		/// Expand the table to a 16-bit to 16-bit table.
		
		void Expand16 (uint16 *table16) const;
			
	private:
	
		void SubDivide (const dng_1d_function &function,
						uint32 lower,
						uint32 upper,
						real32 maxDelta);
	
		// Hidden copy constructor and assignment operator.
	
		dng_1d_table (const dng_1d_table &table);
		
		dng_1d_table & operator= (const dng_1d_table &table);
	
	};

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

#endif
	
/*****************************************************************************/