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_temperature.h"
10 
11 #include "dng_xy_coord.h"
12 
13 /*****************************************************************************/
14 
15 // Scale factor between distances in uv space to a more user friendly "tint"
16 // parameter.
17 
18 static const real64 kTintScale = -3000.0;
19 
20 /*****************************************************************************/
21 
22 // Table from Wyszecki & Stiles, "Color Science", second edition, page 228.
23 
24 struct ruvt
25 	{
26 	real64 r;
27 	real64 u;
28 	real64 v;
29 	real64 t;
30 	};
31 
32 static const ruvt kTempTable [] =
33 	{
34 	{	0, 0.18006, 0.26352, -0.24341 },
35 	{  10, 0.18066, 0.26589, -0.25479 },
36 	{  20, 0.18133, 0.26846, -0.26876 },
37 	{  30, 0.18208, 0.27119, -0.28539 },
38 	{  40, 0.18293, 0.27407, -0.30470 },
39 	{  50, 0.18388, 0.27709, -0.32675 },
40 	{  60, 0.18494, 0.28021, -0.35156 },
41 	{  70, 0.18611, 0.28342, -0.37915 },
42 	{  80, 0.18740, 0.28668, -0.40955 },
43 	{  90, 0.18880, 0.28997, -0.44278 },
44 	{ 100, 0.19032, 0.29326, -0.47888 },
45 	{ 125, 0.19462, 0.30141, -0.58204 },
46 	{ 150, 0.19962, 0.30921, -0.70471 },
47 	{ 175, 0.20525, 0.31647, -0.84901 },
48 	{ 200, 0.21142, 0.32312, -1.0182 },
49 	{ 225, 0.21807, 0.32909, -1.2168 },
50 	{ 250, 0.22511, 0.33439, -1.4512 },
51 	{ 275, 0.23247, 0.33904, -1.7298 },
52 	{ 300, 0.24010, 0.34308, -2.0637 },
53 	{ 325, 0.24702, 0.34655, -2.4681 },
54 	{ 350, 0.25591, 0.34951, -2.9641 },
55 	{ 375, 0.26400, 0.35200, -3.5814 },
56 	{ 400, 0.27218, 0.35407, -4.3633 },
57 	{ 425, 0.28039, 0.35577, -5.3762 },
58 	{ 450, 0.28863, 0.35714, -6.7262 },
59 	{ 475, 0.29685, 0.35823, -8.5955 },
60 	{ 500, 0.30505, 0.35907, -11.324 },
61 	{ 525, 0.31320, 0.35968, -15.628 },
62 	{ 550, 0.32129, 0.36011, -23.325 },
63 	{ 575, 0.32931, 0.36038, -40.770 },
64 	{ 600, 0.33724, 0.36051, -116.45 }
65 	};
66 
67 /*****************************************************************************/
68 
Set_xy_coord(const dng_xy_coord & xy)69 void dng_temperature::Set_xy_coord (const dng_xy_coord &xy)
70 	{
71 
72 	// Convert to uv space.
73 
74 	real64 u = 2.0 * xy.x / (1.5 - xy.x + 6.0 * xy.y);
75 	real64 v = 3.0 * xy.y / (1.5 - xy.x + 6.0 * xy.y);
76 
77 	// Search for line pair coordinate is between.
78 
79 	real64 last_dt = 0.0;
80 
81 	real64 last_dv = 0.0;
82 	real64 last_du = 0.0;
83 
84 	for (uint32 index = 1; index <= 30; index++)
85 		{
86 
87 		// Convert slope to delta-u and delta-v, with length 1.
88 
89 		real64 du = 1.0;
90 		real64 dv = kTempTable [index] . t;
91 
92 		real64 len = sqrt (1.0 + dv * dv);
93 
94 		du /= len;
95 		dv /= len;
96 
97 		// Find delta from black body point to test coordinate.
98 
99 		real64 uu = u - kTempTable [index] . u;
100 		real64 vv = v - kTempTable [index] . v;
101 
102 		// Find distance above or below line.
103 
104 		real64 dt = - uu * dv + vv * du;
105 
106 		// If below line, we have found line pair.
107 
108 		if (dt <= 0.0 || index == 30)
109 			{
110 
111 			// Find fractional weight of two lines.
112 
113 			if (dt > 0.0)
114 				dt = 0.0;
115 
116 			dt = -dt;
117 
118 			real64 f;
119 
120 			if (index == 1)
121 				{
122 				f = 0.0;
123 				}
124 			else
125 				{
126 				f = dt / (last_dt + dt);
127 				}
128 
129 			// Interpolate the temperature.
130 
131 			fTemperature = 1.0E6 / (kTempTable [index - 1] . r * f +
132 									kTempTable [index	 ] . r * (1.0 - f));
133 
134 			// Find delta from black body point to test coordinate.
135 
136 			uu = u - (kTempTable [index - 1] . u * f +
137 					  kTempTable [index	   ] . u * (1.0 - f));
138 
139 			vv = v - (kTempTable [index - 1] . v * f +
140 					  kTempTable [index	   ] . v * (1.0 - f));
141 
142 			// Interpolate vectors along slope.
143 
144 			du = du * (1.0 - f) + last_du * f;
145 			dv = dv * (1.0 - f) + last_dv * f;
146 
147 			len = sqrt (du * du + dv * dv);
148 
149 			du /= len;
150 			dv /= len;
151 
152 			// Find distance along slope.
153 
154 			fTint = (uu * du + vv * dv) * kTintScale;
155 
156 			break;
157 
158 			}
159 
160 		// Try next line pair.
161 
162 		last_dt = dt;
163 
164 		last_du = du;
165 		last_dv = dv;
166 
167 		}
168 
169 	}
170 
171 /*****************************************************************************/
172 
Get_xy_coord() const173 dng_xy_coord dng_temperature::Get_xy_coord () const
174 	{
175 
176 	dng_xy_coord result;
177 
178 	// Find inverse temperature to use as index.
179 
180 	real64 r = 1.0E6 / fTemperature;
181 
182 	// Convert tint to offset is uv space.
183 
184 	real64 offset = fTint * (1.0 / kTintScale);
185 
186 	// Search for line pair containing coordinate.
187 
188 	for (uint32 index = 0; index <= 29; index++)
189 		{
190 
191 		if (r < kTempTable [index + 1] . r || index == 29)
192 			{
193 
194 			// Find relative weight of first line.
195 
196 			real64 f = (kTempTable [index + 1] . r - r) /
197 					   (kTempTable [index + 1] . r - kTempTable [index] . r);
198 
199 			// Interpolate the black body coordinates.
200 
201 			real64 u = kTempTable [index	] . u * f +
202 					   kTempTable [index + 1] . u * (1.0 - f);
203 
204 			real64 v = kTempTable [index	] . v * f +
205 					   kTempTable [index + 1] . v * (1.0 - f);
206 
207 			// Find vectors along slope for each line.
208 
209 			real64 uu1 = 1.0;
210 			real64 vv1 = kTempTable [index] . t;
211 
212 			real64 uu2 = 1.0;
213 			real64 vv2 = kTempTable [index + 1] . t;
214 
215 			real64 len1 = sqrt (1.0 + vv1 * vv1);
216 			real64 len2 = sqrt (1.0 + vv2 * vv2);
217 
218 			uu1 /= len1;
219 			vv1 /= len1;
220 
221 			uu2 /= len2;
222 			vv2 /= len2;
223 
224 			// Find vector from black body point.
225 
226 			real64 uu3 = uu1 * f + uu2 * (1.0 - f);
227 			real64 vv3 = vv1 * f + vv2 * (1.0 - f);
228 
229 			real64 len3 = sqrt (uu3 * uu3 + vv3 * vv3);
230 
231 			uu3 /= len3;
232 			vv3 /= len3;
233 
234 			// Adjust coordinate along this vector.
235 
236 			u += uu3 * offset;
237 			v += vv3 * offset;
238 
239 			// Convert to xy coordinates.
240 
241 			result.x = 1.5 * u / (u - 4.0 * v + 2.0);
242 			result.y =		 v / (u - 4.0 * v + 2.0);
243 
244 			break;
245 
246 			}
247 
248 		}
249 
250 	return result;
251 
252 	}
253 
254 /*****************************************************************************/
255