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