Home | History | Annotate | Download | only in colorchecker
      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 "MeteringTest"
     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 "meteringtest.h"
     27 
     28 const float kOverExposure = 230.f;
     29 const float kEqThreshold = 0.05f;
     30 // Processes the checker colors stored by comparing the pixel values from the
     31 // two scenarios in a test.
     32 void MeteringTest::processData() {
     33     ALOGV("Start Processing Metering Test Data!");
     34 
     35     int numTests = mCheckerColors.size() / 2;
     36     mNumPatches = 0;
     37 
     38     if (numTests > 0) {
     39         mNumPatches = mCheckerColors[0].size();
     40     }
     41 
     42     for (int i = 0; i < numTests; ++i) {
     43         mComparisonResults.push_back(
     44                 isEquivalentTo((&mCheckerColors[i * 2]),
     45                                (&mCheckerColors[i * 2 + 1])));
     46         mComparisonResults.push_back(
     47                 isDarkerThan((&mCheckerColors[i * 2]),
     48                              (&mCheckerColors[i * 2 + 1])));
     49     }
     50 }
     51 
     52 void MeteringTest::clearData() {
     53     mComparisonResults.clear();
     54     mCheckerColors.clear();
     55 }
     56 
     57 // Compares two given arrays of pixel values and decide whether the first one is
     58 // significantly darker than the second one.
     59 bool MeteringTest::isDarkerThan(
     60         const std::vector<Vec3f>* checkerColors1,
     61         const std::vector<Vec3f>* checkerColors2) const {
     62     float meanRatio = 0.f;
     63     int meanNumCount = 0;
     64 
     65     for (int i = 0; i < mNumPatches; ++i) {
     66         float luminance1 = (*checkerColors1)[i].convertToLuminance();
     67         float luminance2 = (*checkerColors2)[i].convertToLuminance();
     68 
     69         // Out of the saturation rage, define 5% as a margin for being
     70         // significantly brighter.
     71         if ((luminance2 < kOverExposure) && (luminance1 != 0.f)) {
     72             meanRatio += luminance2 / luminance1;
     73             ++meanNumCount;
     74         }
     75     }
     76     meanRatio = meanRatio / meanNumCount;
     77 
     78     return (meanRatio > 1 + kEqThreshold);
     79 }
     80 
     81 // Compares the two givn arrays of pixel values and decide whether they are
     82 // equivalent within an acceptable range.
     83 bool MeteringTest::isEquivalentTo(
     84         const std::vector<Vec3f>* checkerColors1,
     85         const std::vector<Vec3f>* checkerColors2) const {
     86     float meanRatio = 0.f;
     87     int meanNumCount = 0;
     88 
     89     for (int i = 0; i < mNumPatches; ++i) {
     90         float luminance1 = (*checkerColors1)[i].convertToLuminance();
     91         float luminance2 = (*checkerColors2)[i].convertToLuminance();
     92         ALOGV("Luma_1 and Luma_2 is %f, %f", luminance1, luminance2);
     93 
     94         if ((luminance1 < kOverExposure) && (luminance2 < kOverExposure)) {
     95               meanRatio += luminance2 / luminance1;
     96               ++meanNumCount;
     97         }
     98     }
     99     meanRatio = meanRatio / meanNumCount;
    100 
    101     return ((meanRatio >= 1 - kEqThreshold) && (meanRatio <= 1 + kEqThreshold));
    102 }
    103