MEPP2 Project
rgb_to_lab.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2020 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published
6 // by the Free Software Foundation; either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 
12 #pragma once
13 
14 inline double F(double input) // function f(...), which is used for defining L, a and b
15  // changes within [4/29,1]
16 {
17  if (input > 0.008856)
18  return std::cbrt(input); // maximum 1 --- prefer cbrt to pow for cubic root
19  else
20  return ((double(841.0) / 108.0) * input +
21  double(4.0) / 29.0); // 841/108 = 29*29/36*16
22 }
23 
24 inline void RGBtoXYZ(double R, double G, double B, double &X, double &Y, double &Z)
25 {
26  // RGB Working Space: sRGB
27  // R 0 -- 1, G 0 -- 1, B 0 -- 1
28 
29  //Gamma correction
30  R = ((R > 0.0404482362771076) ? std::pow((R + 0.055) / 1.055, 2.4) : (R / 12.92)) ;
31  G = ((G > 0.0404482362771076) ? std::pow((G + 0.055) / 1.055, 2.4) : (G / 12.92));
32  B = ((B > 0.0404482362771076) ? std::pow((B + 0.055) / 1.055, 2.4) : (B / 12.92));
33 
34  //Transform
35  /*X = 0.4124 *R + 0.3576*G + 0.1805*B;
36  Y = 0.2126 *R + 0.7152*G + 0.0722*B;
37  Z = 0.0193 *R + 0.1192*G + 0.9505*B;*/
38 
39  // Coefficients more precise
40  X = 0.412396 *R + 0.357583*G + 0.180493*B;
41  Y = 0.212586*R + 0.71517*G + 0.0722005*B;
42  Z = 0.0192972*R + 0.119184 *G + 0.950497*B;
43 
44 }
45 
46 // XYZ to CIELab
47 inline void XYZtoLab(double X, double Y, double Z, double *lab)
48 {
49 
50  //D65 white
51  //const double Xo = 0.95047;
52  //const double Yo = 1.00000;
53  //const double Zo = 1.08883;
54 
55  // matlab White point
56  const double Xo = 0.950456;
57  const double Yo = 1.000000;
58  const double Zo = 1.088754;
59 
60  lab[0] = 116.0 * F(Y / Yo) - 16.0; // maximum L = 100
61  lab[1] = 500.0 * (F(X / Xo) - F(Y / Yo)); // maximum
62  lab[2] = 200.0 * (F(Y / Yo) - F(Z / Zo));
63 
64 }
65 
66 // RGB to CIELab
67 inline void rgb_to_lab(double R, double G, double B, double *lab)
68 {
69  double X, Y, Z;
70  RGBtoXYZ(R, G, B, X, Y, Z);
71  XYZtoLab(X, Y, Z, lab);
72 }
F
double F(double input)
Definition: rgb_to_lab.h:14
XYZtoLab
void XYZtoLab(double X, double Y, double Z, double *lab)
Definition: rgb_to_lab.h:47
FEVV::DataStructures::AIF::AIFMesh
This class represents an AIF structure. AIF structure can deal with both manifold and non-manifold su...
Definition: AIFMesh.hpp:47
rgb_to_lab
void rgb_to_lab(double R, double G, double B, double *lab)
Definition: rgb_to_lab.h:67
RGBtoXYZ
void RGBtoXYZ(double R, double G, double B, double &X, double &Y, double &Z)
Definition: rgb_to_lab.h:24