Hello!

I tried to write my own program for finding the color matrix (something like the ArtDen's one) but I failed.....

I do not seem to grasp the magic behind DCRaw.

I thought that the color matrix is some kind of transformation from XYZ into RGB (something like

http://www.brucelindbloom.com/Eqn_XYZ_to_RGB.html). So the algorithm I used was:

1) get the RAW

2) use dcraw -D -T -4 raw.dng

3) debayer raw.tiff to obtain debayered.tiff

4) I assumed that "debayered.tiff" contains pixels in XYZ format with (X being stored in R, Y in G and Z in B of the tiff). Am I right?

4) find the matrix M which would cause "debayered.tiff" to match raw.jpg (the JPEG generated by camera) as closely as possible. The formula to use was:

for each pixel (X,Y,Z) in debayered.tiff, to obtain a pixel in RGB format use

[R G B] = [X Y Z] *M,

where M is a 3x3 matrix of

m11, m12 m13

m21, m22, m23

m31, m32, m33

of unknown values that need to be found. I performed the search by minimizing the sum of squared errors ([R_jpg, G_jpg, B_jpg] - [X Y Z]*M

To test if the matrix is correct I used:

dngsetmatrix raw.dng M m11 m12 m13 m21 m22 m23 m31 m32 m33

dcraw -T -r 1 1 1 1 raw.dng

and then visually comparing the result with raw.jpg.

The results varied from "barely acceptable" to "hopeless".

I also tried to find the white balance (the -r r1 g1 b1 g1 argument to DCRaw) by hand so that the whites on ArtDen's test file were uniform (i.e. R=G=B on the white spot in the left-down corner) as closely as possible. I then redid my algorithm - the results were equally hopeless....

I come to a conclusion that DCRaw does some additional tricks, as:

1a) if you multiply each row of matrix M by any non-zero number such that the matrix is still displayed properly in dngsetmatrix, the result does not change

2a) if you multiply each column of M, the result changes

3a) If M really is the transformation from XYZ to RGB, then (all conslusions UNVERIFIED and COULD BE COMPLETELY WRONG!):

3.1a) by setting it to identity matrix you should obtain the TIFF with XYZ instead of RGB. Am I right?

3.2a) If you set M to

1 0 0

0 0 0

0 0 0

you should get TIFF with all reds, and G=B=0 - instead the DCRaw crashes.

3.3a) Maybe you need to have M invertible? If so, set:

1 0 0

0 0.001 0

0 0 0.001

and G and B should be very small - NOT TRUE - After testing it, I think that I got every possible value of RGB....

3.4a) Maybe the formula for transformation is wrong? Maybe it is the INVERSE of M that is needed?

I tried the inverse of 3.3a) and the result was different but still no reds...

Could someone (ArtDen?

) provide some comments on my approach? I KNOW I am doing SOMETHING very wrong, the problem is to find that SOMETHING