After much work, pretty much competing with my PhD for available time slots, I now have a matrix than can convert from raw RGB to jpg RGB. At least for the 8 bits per pixel raw files. However, I believe they could apply also for the 12 bits per pixels, with proper scaling.
So based on this matrix, you could derive all the rest of the matrices by simple computations. But there is a critical point, and this gave me quite a few head-aches - the gamma correction must be done on a color channel basis, otherwise the results are not OK. Another point, the matrix is no producing 100% identical images in all conditions, but the results are good, at least for me are sometimes better than what the camera produces (for instance a more blueish hue for the sky instead of the more purple hue typical for Fuji cameras). I believe this is because of automatic white balancing done inside the camera, for which we don't have info in the raw files. Anyway, the matrix was extracted from a picture done in broad daylight, about noon, and covers many colors.
A few words about matrix extraction. Initially I tried to follow the suggestion given by Alex in his links, however that did not work. One of the reasons was that it assumed a gamma value, with no connection to the actual image sensor. So this means that the matrix extraction and gamma determination must be done both in order to have good results - this was done by estimation over the entire image, using gradient descent - the process of converting to a new color space and applying gamma correction could be assimilated with passing the image through an artificial neural network with 2 layers. The first layer has a non-linear activation function (log function) and applies the color space conversion (the weights of the neuron inputs are the matrix coefficients) plus a gamma linearization. The second level is also non-linear and applies the gamma correction as weights at the inputs. The activation function is an exponential. In a few math lines this could be like this:
N1 = ln(R*a + G * b + B * c)
N2 = e^(gamma * N1) = (e^N1)^gamma = (R*a + G * b + B * c)^gamma
With this in mind, it's possible to apply the gradient descent algorithm to compute both the gamma value and the matrix values. But like I said, each color channel has it's own gamma value and until I realized this I was very frustrated because I could get no good results!
So - here are my values:
gamma vector:
g_R = 0.7645
g_G = 1.2790
g_B = 0.6802
matrix =
1.4929 -0.3490 0.1428
-0.4644 1.6422 -0.2113
0.0398 -0.5037 1.8335
The operation needed with this matrix to convert from RAW to JPG is like this:
JPG = (matrix x RAW)^gamma
JPG and RAW are column vectors like this:
JPG = [jpg_R
jpg_G
jpg_B]
I hope this results will give a new impulse to the thread and that we will see new results with this!!
I forgot to tell that all computations are done with both the RAW image and the JPG image normalized to the maximum value that can be represented given the number of bits per pixel. In my case this was 256, but if you're going to use the 12 bits per pixel image, than you must normalize the raw image to 2^12 = 4096 and than use the matrix. After that, you can de-normalize to what ever bit depth you're using (8 / 12/ 16). Of course - it's also possible to de-normalize the matrix.