1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #define LOG_NDEBUG 0
17
18 #define LOG_TAG "WhiteBalanceTest"
19 #include <utils/Log.h>
20 #include <utils/Timers.h>
21 #include <cmath>
22 #include <string>
23
24 #include "vec2.h"
25 #include "vec3.h"
26 #include "whitebalancetest.h"
27
28 // White point in XYZ color space under 5200k illumination.
29 const Vec3f kDaylightWhitePoint(0.9781f, 1.f, 0.9021f);
30
31 // Process the data of checker colors collected under different white balance.
32 // Assuming the Daylight CCT is set to 5200k, compute the CCT of other white
33 // balance modes.
processData()34 void WhiteBalanceTest::processData() {
35 ALOGV("Start Processing White Balance Test Data!");
36
37 int numPatches = mCheckerColors.size();
38 ALOGV("Processing %d tests with %d patches", 2, numPatches);
39
40 std::vector<Vec3f> xyzColors(numPatches);
41 for (int j = 0; j < numPatches; ++j) {
42 Vec3f xyzCheckerColor = initializeFromRGB(mCheckerColors[j]);
43 xyzColors[j] = xyzCheckerColor;
44 ALOGV("XYZ coordinate is %f, %f, %f", xyzCheckerColor.r(),
45 xyzCheckerColor.g(), xyzCheckerColor.b());
46 }
47
48 Vec3f meanScale(0.f, 0.f, 0.f);
49
50 if (mMode == "daylight") {
51 mXyzColorsDaylight = xyzColors;
52 // For testing the auto white balance mode. Compute a CCT that would
53 // map the gray checkers to a white point.
54 for (int j = 1; j < numPatches; ++j) {
55 meanScale = meanScale +
56 (mXyzColorsDaylight[j] / kDaylightWhitePoint);
57 }
58 } else {
59 for (int j = 1; j < numPatches; ++j) {
60 meanScale = meanScale + (mXyzColorsDaylight[j] / xyzColors[j]);
61 }
62 }
63
64 meanScale = meanScale / (numPatches - 1);
65 ALOGV("Scale: %f, %f, %f", meanScale.r(), meanScale.g(), meanScale.b());
66
67 Vec3f whitePoint;
68 whitePoint = meanScale * kDaylightWhitePoint;
69
70 ALOGV("White point is %f, %f, %f", whitePoint.r(),
71 whitePoint.g(), whitePoint.b());
72
73 mCorrelatedColorTemp = findCorrelatedColorTemp(whitePoint);
74 ALOGV("CCT is %d", mCorrelatedColorTemp);
75 }
76
77 // Given a white point, find the correlated color temperature.
78 // Formula taken from the paper "Calculating Correlated Color Temperatures
79 // Across the Entire Gamut of Daylight and Skylight Chromaticities" by Hernandez
80 // Andres et al. in 1999. The numbers are fitting parameters.
findCorrelatedColorTemp(const Vec3f & whitePoint)81 int WhiteBalanceTest::findCorrelatedColorTemp(const Vec3f &whitePoint) {
82 Vec2f chromaOfWhitePoint(
83 whitePoint.r() / (whitePoint.r() + whitePoint.g() + whitePoint.b()),
84 whitePoint.g() / (whitePoint.r() + whitePoint.g() + whitePoint.b()));
85
86 float n = (chromaOfWhitePoint.x() - 0.3366f)
87 / (chromaOfWhitePoint.y() - 0.1735f);
88 float y = -949.86315f + 6253.80338f * exp(-n / 0.92159f)
89 + 28.70599f * exp(-n / 0.20039f) + 0.00004f * exp(-n / 0.07125f);
90
91 return static_cast<int>(y);
92 }
93
94 // Converts a RGB pixel value to XYZ color space.
initializeFromRGB(const Vec3f & rgb)95 Vec3f WhiteBalanceTest::initializeFromRGB(const Vec3f &rgb) {
96 float linearRed = convertToLinear(rgb.r());
97 float linearGreen = convertToLinear(rgb.g());
98 float linearBlue = convertToLinear(rgb.b());
99
100 float x = 0.4124f * linearRed + 0.3576f * linearGreen +
101 0.1805f * linearBlue;
102 float y = 0.2126f * linearRed + 0.7152f * linearGreen +
103 0.0722f * linearBlue;
104 float z = 0.0193f * linearRed + 0.1192f * linearGreen +
105 0.9505f * linearBlue;
106
107 return Vec3f(x, y, z);
108 }
109
convertToLinear(float color)110 float WhiteBalanceTest::convertToLinear(float color) {
111 float norm = color/ 255.0f;
112 float linearColor;
113
114 // Convert from sRGB space to linear RGB value
115 if (norm > 0.04045f) {
116 linearColor = pow(((norm + 0.055f) / 1.055f), 2.4f);
117 } else {
118 linearColor = norm / 12.92f;
119 }
120
121 return linearColor;
122 }
123