PICCANTE  0.4
The hottest HDR imaging library!
matrix_from_primaries.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_COLORS_MATRIX_FORM_PRIMARIES_HPP
19 #define PIC_COLORS_MATRIX_FORM_PRIMARIES_HPP
20 
21 #include "../base.hpp"
22 
23 #ifndef PIC_DISABLE_EIGEN
24  #ifndef PIC_EIGEN_NOT_BUNDLED
25  #include "../externals/Eigen/Dense"
26  #include "../externals/Eigen/QR"
27  #else
28  #include <Eigen/Dense>
29  #include <Eigen/QR>
30  #endif
31 #endif
32 
33 namespace pic {
34 
44 float *createMatrixFromPrimaries(float *red_XYZ,
45  float *green_XYZ,
46  float *blue_XYZ,
47  float *white_point_XYZ,
48  float *ret = NULL
49  )
50 {
51  if(red_XYZ == NULL || green_XYZ == NULL || blue_XYZ == NULL) {
52  return ret;
53  }
54 
55  if(ret == NULL) {
56  ret = new float[9];
57  }
58 
59 #ifndef PIC_DISABLE_EIGEN
60 
61  int w = 0;
62  if(white_point_XYZ != NULL) {
63  w = 3;
64  }
65 
66  //set up a liner system A x = b
67  int nRow = 9 + w;
68  Eigen::MatrixXf A(nRow, 9);
69  Eigen::VectorXf b(nRow);
70 
71  //A matrix
72  A.setZero();
73 
74  //red
75  for(int j = 0; j < 3; j++) {
76  for(int i = 0 ; i < 3; i++) {
77  A(j, j * 3 + i) = red_XYZ[i];
78  }
79  }
80 
81  //green`
82  for(int j = 0; j < 3; j++) {
83  for(int i = 0 ; i < 3; i++) {
84  A(j + 3, j * 3 + i) = green_XYZ[i];
85  }
86  }
87 
88  //blue`
89  for(int j = 0; j < 3; j++) {
90  for(int i = 0 ; i < 3; i++) {
91  A(j + 6, j * 3 + i) = blue_XYZ[i];
92  }
93  }
94 
95  //white
96  if(w == 3) {
97  for(int j = 0; j < 3; j++) {
98  for(int i = 0 ; i < 3; i++) {
99  A(j + 9, j * 3 + i) = white_point_XYZ[i];
100  }
101  }
102  }
103 
104  //b vector
105  b(0) = 1.0f;
106  b(1) = 0.0f;
107  b(2) = 0.0f;
108 
109  b(3) = 0.0f;
110  b(4) = 1.0f;
111  b(5) = 0.0f;
112 
113  b(6) = 0.0f;
114  b(7) = 0.0f;
115  b(8) = 1.0f;
116 
117  if(w == 3) {
118  b(9) = 1.0f;
119  b(10) = 1.0f;
120  b(11) = 1.0f;
121  }
122 
123  //solve Ax=b
124  Eigen::VectorXf x = A.colPivHouseholderQr().solve(b);
125 
126  for(int i = 0; i < 9; i++) {
127  ret[i] = x(i);
128  }
129 #endif
130  return ret;
131 }
132 
133 } // end namespace pic
134 
135 #endif /* PIC_COLORS_MATRIX_FORM_PRIMARIES_HPP */
136 
Definition: bilateral_separation.hpp:25
float * createMatrixFromPrimaries(float *red_XYZ, float *green_XYZ, float *blue_XYZ, float *white_point_XYZ, float *ret=NULL)
createMatrixFromPrimaries computes a matrix for converting XYZ values into the defined color space (i...
Definition: matrix_from_primaries.hpp:44