Debian Codegraph @

converter/other/cameratopam/dng.c

raw
1 #include <string.h>
2 #include "global_variables.h"
3
4 #include "dng.h"
5
6 void
7 dng_coeff (double cc[4][4],
8 double cm[4][3],
9 double xyz[3]) {
10 static const double rgb_xyz[3][3] = { /* RGB from XYZ */
11 { 3.240479, -1.537150, -0.498535 },
12 { -0.969256, 1.875992, 0.041556 },
13 { 0.055648, -0.204043, 1.057311 } };
14 #if 0
15 static const double xyz_rgb[3][3] = { /* XYZ from RGB */
16 { 0.412453, 0.357580, 0.180423 },
17 { 0.212671, 0.715160, 0.072169 },
18 { 0.019334, 0.119193, 0.950227 } };
19 #endif
20 double cam_xyz[4][3], xyz_cam[3][4], invert[3][6];
21 unsigned int i;
22
23 for (i = 0; i < colors; ++i) {
24 unsigned int j;
25 for (j = 0; j < 3; ++j) {
26 unsigned int k;
27 for (k = 0, cam_xyz[i][j] = 0.0; k < colors; ++k) {
28 cam_xyz[i][j] += cc[i][k] * cm[k][j] * xyz[j];
29 }
30 }
31 }
32 for (i = 0; i < colors; ++i) {
33 unsigned int j;
34 double camXyzSum;
35
36 for (j = 0, camXyzSum = 0.0; j < 3; ++j)
37 camXyzSum += cam_xyz[i][j];
38
39 for (j = 0; j < 3; ++j)
40 cam_xyz[i][j] /= camXyzSum;
41
42 pre_mul[i] = 1 / camXyzSum;
43 }
44 for (i = 0; i < 3; ++i) {
45 unsigned int j;
46 for (j = 0; j < 6; ++j)
47 invert[i][j] = j == i+3;
48 for (j = 0; j < 3; ++j) {
49 unsigned int k;
50 for (k = 0; k < colors; ++k)
51 invert[i][j] += cam_xyz[k][i] * cam_xyz[k][j];
52 }
53 }
54 for (i = 0; i < 3; ++i) {
55 double const num = invert[i][i];
56 unsigned int j;
57 unsigned int k;
58 for (j = 0; j < 6; ++j) /* Normalize row i */
59 invert[i][j] /= num;
60 for (k = 0; k < 3; ++k) { /* Subtract it from other rows */
61 if (k != i) {
62 double const num = invert[k][i];
63 unsigned int j;
64 for (j = 0; j < 6; ++j)
65 invert[k][j] -= invert[i][j] * num;
66 }
67 }
68 }
69
70 memset(xyz_cam, 0, sizeof xyz_cam);
71
72 for (i = 0; i < 3; ++i) {
73 unsigned int j;
74 for (j = 0; j < colors; ++j) {
75 unsigned int k;
76 for (k = 0; k < 3; ++k)
77 xyz_cam[i][j] += invert[i][k+3] * cam_xyz[j][k];
78 }
79 }
80 memset (coeff, 0, sizeof coeff);
81
82 for (i = 0; i < 3; ++i) {
83 unsigned int j;
84 for (j = 0; j < colors; ++j) {
85 unsigned int k;
86 for (k = 0; k < 3; ++k)
87 coeff[i][j] += rgb_xyz[i][k] * xyz_cam[k][j];
88 }
89 }
90 {
91 double greenSum;
92 unsigned int j;
93 unsigned int i;
94
95 for (j = 0, greenSum = 0.0; j < colors; ++j)
96 greenSum += coeff[1][j];
97
98 for (i = 0; i < 3; ++i) {
99 unsigned int j;
100 for (j = 0; j < colors; ++j)
101 coeff[i][j] /= greenSum;
102 }
103 }
104 use_coeff = 1;
105 }
106
107
108