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  ** Unless required by applicable law or agreed to in writing, software
     10  * distributed under the License is distributed on an "AS IS" BASIS,
     11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  * See the License for the specific language governing permissions and
     13  * limitations under the License.
     14  */
     15 
     16 #ifndef VEC3_H
     17 #define VEC3_H
     18 
     19 #include <assert.h>
     20 #include <cmath>
     21 
     22 // Implementes a class to represent the pixel value in a 3-dimensional color
     23 // space. The colors are all represented by int as it is mostly used as RGB.
     24 template <class T>
     25 class Vec3{
     26   public:
     27     Vec3(T inputRed, T inputGreen, T inputBlue) {
     28         mRed = inputRed;
     29         mGreen = inputGreen;
     30         mBlue = inputBlue;
     31     }
     32 
     33     Vec3() {}
     34 
     35     template <class U>
     36     inline Vec3<T> operator+ (const Vec3<U> &param) const {
     37         Vec3<T> temp(mRed + param.r(), mGreen + param.g(), mBlue + param.b());
     38         return temp;
     39     }
     40 
     41     inline Vec3<T> operator- (const Vec3<T> &param) const {
     42         Vec3<T> temp(mRed - param.r(), mGreen - param.g(), mBlue - param.b());
     43         return temp;
     44     }
     45 
     46     inline Vec3<T> operator* (const int param) const {
     47         Vec3<T> temp(mRed * param, mGreen * param, mBlue * param);
     48         return temp;
     49     }
     50 
     51     template <class U>
     52     inline Vec3<float> operator* (const Vec3<U> &param) const {
     53         Vec3<float> temp(mRed * static_cast<U>(param.r()),
     54                          mGreen * static_cast<U>(param.g()),
     55                          mBlue * static_cast<U>(param.b()));
     56         return temp;
     57     }
     58 
     59 
     60     inline Vec3<float> operator/ (const int param) const {
     61         Vec3<float> temp;
     62         assert(param != 0);
     63         temp.set(static_cast<float>(mRed) / static_cast<float>(param),
     64                  static_cast<float>(mGreen) / static_cast<float>(param),
     65                  static_cast<float>(mBlue) / static_cast<float>(param));
     66         return temp;
     67     }
     68 
     69     template <class U>
     70     inline Vec3<float> operator/ (const Vec3<U> &param) const {
     71         Vec3<float> temp;
     72         assert((param.r() != 0.f) && (param.g() != 0.f) && (param.b() != 0.f));
     73         temp.set(static_cast<float>(mRed) / static_cast<float>(param.r()),
     74                  static_cast<float>(mGreen) / static_cast<float>(param.g()),
     75                  static_cast<float>(mBlue) / static_cast<float>(param.b()));
     76         return temp;
     77     }
     78 
     79     template <class U>
     80     float squareDistance(const Vec3<U> &param) const {
     81         float difference = 0.f;
     82         difference = static_cast<float>(mRed - param.r()) *
     83                 static_cast<float>(mRed - param.r()) +
     84                 static_cast<float>(mGreen - param.g()) *
     85                 static_cast<float>(mGreen - param.g()) +
     86                 static_cast<float>(mBlue - param.b()) *
     87                 static_cast<float>(mBlue - param.b());
     88         return difference;
     89     }
     90 
     91     // Assumes conversion from sRGB to luminance.
     92     float convertToLuminance() const {
     93         return (0.299 * static_cast<float>(mRed) +
     94                 0.587 * static_cast<float>(mGreen) +
     95                 0.114 * static_cast<float>(mBlue));
     96     }
     97 
     98     inline T r() const { return mRed; }
     99     inline T g() const { return mGreen; }
    100     inline T b() const { return mBlue; }
    101 
    102     inline void set(const T inputRed, const T inputGreen, const T inputBlue){
    103         mRed = inputRed;
    104         mGreen = inputGreen;
    105         mBlue = inputBlue;
    106     }
    107 
    108   private:
    109     T mRed;
    110     T mGreen;
    111     T mBlue;
    112 };
    113 
    114 typedef Vec3<int> Vec3i;
    115 typedef Vec3<float> Vec3f;
    116 #endif
    117