File indexing completed on 2025-01-19 03:55:18

0001 /*****************************************************************************/
0002 // Copyright 2006-2019 Adobe Systems Incorporated
0003 // All Rights Reserved.
0004 //
0005 // NOTICE:  Adobe permits you to use, modify, and distribute this file in
0006 // accordance with the terms of the Adobe license agreement accompanying it.
0007 /*****************************************************************************/
0008 
0009 #include "dng_temperature.h"
0010 
0011 #include "dng_xy_coord.h"
0012 
0013 /*****************************************************************************/
0014 
0015 // Scale factor between distances in uv space to a more user friendly "tint"
0016 // parameter.
0017 
0018 static const real64 kTintScale = -3000.0;
0019 
0020 /*****************************************************************************/
0021 
0022 // Table from Wyszecki & Stiles, "Color Science", second edition, page 228.
0023 
0024 struct ruvt
0025     {
0026     real64 r;
0027     real64 u;
0028     real64 v;
0029     real64 t;
0030     };
0031 
0032 static const ruvt kTempTable [] =
0033     {
0034     {   0, 0.18006, 0.26352, -0.24341 },
0035     {  10, 0.18066, 0.26589, -0.25479 },
0036     {  20, 0.18133, 0.26846, -0.26876 },
0037     {  30, 0.18208, 0.27119, -0.28539 },
0038     {  40, 0.18293, 0.27407, -0.30470 },
0039     {  50, 0.18388, 0.27709, -0.32675 },
0040     {  60, 0.18494, 0.28021, -0.35156 },
0041     {  70, 0.18611, 0.28342, -0.37915 },
0042     {  80, 0.18740, 0.28668, -0.40955 },
0043     {  90, 0.18880, 0.28997, -0.44278 },
0044     { 100, 0.19032, 0.29326, -0.47888 },
0045     { 125, 0.19462, 0.30141, -0.58204 },
0046     { 150, 0.19962, 0.30921, -0.70471 },
0047     { 175, 0.20525, 0.31647, -0.84901 },
0048     { 200, 0.21142, 0.32312, -1.0182 },
0049     { 225, 0.21807, 0.32909, -1.2168 },
0050     { 250, 0.22511, 0.33439, -1.4512 },
0051     { 275, 0.23247, 0.33904, -1.7298 },
0052     { 300, 0.24010, 0.34308, -2.0637 },
0053     { 325, 0.24702, 0.34655, -2.4681 },
0054     { 350, 0.25591, 0.34951, -2.9641 },
0055     { 375, 0.26400, 0.35200, -3.5814 },
0056     { 400, 0.27218, 0.35407, -4.3633 },
0057     { 425, 0.28039, 0.35577, -5.3762 },
0058     { 450, 0.28863, 0.35714, -6.7262 },
0059     { 475, 0.29685, 0.35823, -8.5955 },
0060     { 500, 0.30505, 0.35907, -11.324 },
0061     { 525, 0.31320, 0.35968, -15.628 },
0062     { 550, 0.32129, 0.36011, -23.325 },
0063     { 575, 0.32931, 0.36038, -40.770 },
0064     { 600, 0.33724, 0.36051, -116.45 }
0065     };
0066 
0067 /*****************************************************************************/
0068 
0069 void dng_temperature::Set_xy_coord (const dng_xy_coord &xy)
0070     {
0071 
0072     // Convert to uv space.
0073 
0074     real64 u = 2.0 * xy.x / (1.5 - xy.x + 6.0 * xy.y);
0075     real64 v = 3.0 * xy.y / (1.5 - xy.x + 6.0 * xy.y);
0076 
0077     // Search for line pair coordinate is between.
0078 
0079     real64 last_dt = 0.0;
0080 
0081     real64 last_dv = 0.0;
0082     real64 last_du = 0.0;
0083 
0084     for (uint32 index = 1; index <= 30; index++)
0085         {
0086 
0087         // Convert slope to delta-u and delta-v, with length 1.
0088 
0089         real64 du = 1.0;
0090         real64 dv = kTempTable [index] . t;
0091 
0092         real64 len = sqrt (1.0 + dv * dv);
0093 
0094         du /= len;
0095         dv /= len;
0096 
0097         // Find delta from black body point to test coordinate.
0098 
0099         real64 uu = u - kTempTable [index] . u;
0100         real64 vv = v - kTempTable [index] . v;
0101 
0102         // Find distance above or below line.
0103 
0104         real64 dt = - uu * dv + vv * du;
0105 
0106         // If below line, we have found line pair.
0107 
0108         if (dt <= 0.0 || index == 30)
0109             {
0110 
0111             // Find fractional weight of two lines.
0112 
0113             if (dt > 0.0)
0114                 dt = 0.0;
0115 
0116             dt = -dt;
0117 
0118             real64 f;
0119 
0120             if (index == 1)
0121                 {
0122                 f = 0.0;
0123                 }
0124             else
0125                 {
0126                 f = dt / (last_dt + dt);
0127                 }
0128 
0129             // Interpolate the temperature.
0130 
0131             fTemperature = 1.0E6 / (kTempTable [index - 1] . r * f +
0132                                     kTempTable [index    ] . r * (1.0 - f));
0133 
0134             // Find delta from black body point to test coordinate.
0135 
0136             uu = u - (kTempTable [index - 1] . u * f +
0137                       kTempTable [index    ] . u * (1.0 - f));
0138 
0139             vv = v - (kTempTable [index - 1] . v * f +
0140                       kTempTable [index    ] . v * (1.0 - f));
0141 
0142             // Interpolate vectors along slope.
0143 
0144             du = du * (1.0 - f) + last_du * f;
0145             dv = dv * (1.0 - f) + last_dv * f;
0146 
0147             len = sqrt (du * du + dv * dv);
0148 
0149             du /= len;
0150             dv /= len;
0151 
0152             // Find distance along slope.
0153 
0154             fTint = (uu * du + vv * dv) * kTintScale;
0155 
0156             break;
0157 
0158             }
0159 
0160         // Try next line pair.
0161 
0162         last_dt = dt;
0163 
0164         last_du = du;
0165         last_dv = dv;
0166 
0167         }
0168 
0169     }
0170 
0171 /*****************************************************************************/
0172 
0173 dng_xy_coord dng_temperature::Get_xy_coord () const
0174     {
0175 
0176     dng_xy_coord result;
0177 
0178     // Find inverse temperature to use as index.
0179 
0180     real64 r = 1.0E6 / fTemperature;
0181 
0182     // Convert tint to offset is uv space.
0183 
0184     real64 offset = fTint * (1.0 / kTintScale);
0185 
0186     // Search for line pair containing coordinate.
0187 
0188     for (uint32 index = 0; index <= 29; index++)
0189         {
0190 
0191         if (r < kTempTable [index + 1] . r || index == 29)
0192             {
0193 
0194             // Find relative weight of first line.
0195 
0196             real64 f = (kTempTable [index + 1] . r - r) /
0197                        (kTempTable [index + 1] . r - kTempTable [index] . r);
0198 
0199             // Interpolate the black body coordinates.
0200 
0201             real64 u = kTempTable [index    ] . u * f +
0202                        kTempTable [index + 1] . u * (1.0 - f);
0203 
0204             real64 v = kTempTable [index    ] . v * f +
0205                        kTempTable [index + 1] . v * (1.0 - f);
0206 
0207             // Find vectors along slope for each line.
0208 
0209             real64 uu1 = 1.0;
0210             real64 vv1 = kTempTable [index] . t;
0211 
0212             real64 uu2 = 1.0;
0213             real64 vv2 = kTempTable [index + 1] . t;
0214 
0215             real64 len1 = sqrt (1.0 + vv1 * vv1);
0216             real64 len2 = sqrt (1.0 + vv2 * vv2);
0217 
0218             uu1 /= len1;
0219             vv1 /= len1;
0220 
0221             uu2 /= len2;
0222             vv2 /= len2;
0223 
0224             // Find vector from black body point.
0225 
0226             real64 uu3 = uu1 * f + uu2 * (1.0 - f);
0227             real64 vv3 = vv1 * f + vv2 * (1.0 - f);
0228 
0229             real64 len3 = sqrt (uu3 * uu3 + vv3 * vv3);
0230 
0231             uu3 /= len3;
0232             vv3 /= len3;
0233 
0234             // Adjust coordinate along this vector.
0235 
0236             u += uu3 * offset;
0237             v += vv3 * offset;
0238 
0239             // Convert to xy coordinates.
0240 
0241             result.x = 1.5 * u / (u - 4.0 * v + 2.0);
0242             result.y =       v / (u - 4.0 * v + 2.0);
0243 
0244             break;
0245 
0246             }
0247 
0248         }
0249 
0250     return result;
0251 
0252     }
0253 
0254 /*****************************************************************************/