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 	public:
54 
55 		/// Table constructor. count must be a power of two
56 		/// and at least kMinTableSize.
57 
58 		explicit dng_1d_table (uint32 count = kDefaultTableSize);
59 
60 		virtual ~dng_1d_table ();
61 
62 		/// Number of table entries.
63 
Count()64 		uint32 Count () const
65 			{
66 			return fTableCount;
67 			}
68 
69 		/// Set up table, initialize entries using functiion.
70 		/// This method can throw an exception, e.g. if there is not enough memory.
71 		/// \param allocator Memory allocator from which table memory is allocated.
72 		/// \param function Table is initialized with values of finction.Evalluate(0.0) to function.Evaluate(1.0).
73 		/// \param subSample If true, only sample the function a limited number of times and interpolate.
74 
75 		void Initialize (dng_memory_allocator &allocator,
76 						 const dng_1d_function &function,
77 						 bool subSample = false);
78 
79 		/// Lookup and interpolate mapping for an input.
80 		/// \param x value from 0.0 to 1.0 used as input for mapping
81 		/// \retval Approximation of function.Evaluate(x)
82 
Interpolate(real32 x)83 		real32 Interpolate (real32 x) const
84 			{
85 
86 			real32 y = x * (real32) fTableCount;
87 
88 			int32 index = (int32) y;
89 
90 			// Enable vectorization by using DNG_ASSERT instead of DNG_REQUIRE
91 			DNG_ASSERT(!(index < 0 || index >(int32) fTableCount), "dng_1d_table::Interpolate parameter out of range");
92 
93 			real32 z = (real32) index;
94 
95 			real32 fract = y - z;
96 
97 			return fTable [index	] * (1.0f - fract) +
98 				   fTable [index + 1] * (		fract);
99 
100 			}
101 
102 		/// Direct access function for table data.
103 
Table()104 		const real32 * Table () const
105 			{
106 			return fTable;
107 			}
108 
109 		/// Expand the table to a 16-bit to 16-bit table.
110 
111 		void Expand16 (uint16 *table16) const;
112 
113 	private:
114 
115 		void SubDivide (const dng_1d_function &function,
116 						uint32 lower,
117 						uint32 upper,
118 						real32 maxDelta);
119 
120 	};
121 
122 /*****************************************************************************/
123 
124 #endif
125 
126 /*****************************************************************************/
127