Add a note about Subtitle Edit to the manual.
[dcpomatic.git] / doc / manual / colour.tex
1 \documentclass{article}
2 \usepackage{url,amsmath,mathtools}
3 \usepackage[landscape]{geometry}
4 \title{Colour conversions for DCP creation}
5 \author{Carl Hetherington, Dennis Couzin}
6 \date{}
7 \begin{document}
8 \maketitle
9
10 \section{Overview}
11
12 The process of conversion from RGB to XYZ is:
13
14 \begin{enumerate}
15 \item Convert to linear RGB (i.e.\ apply a gamma curve, or at least an approximation to one).
16 \item Convert to XYZ (preserving the white point).
17 \item Adjust the white point.
18 \item Normalize values.
19 \item Convert to non-linear XYZ (i.e.\ apply a gamma curve)
20 \end{enumerate}
21
22
23 \section{Convert to linear RGB}
24
25 This is done using either a `pure' gamma function, so that for some colour value $C_i$ the output colour $C_o$ is given by
26
27 \begin{align}
28 C_o &= C_i ^ \gamma
29 \end{align}
30
31 or a modified function of the form
32
33 \begin{align}
34 C_o &= \left\{
35 \begin{array}{ll}
36 \frac{C_i}{K} & C_i \leq C_t \\
37 \left( \frac{C_i + A}{1 + A} \right)^\zeta & C_i > C_t
38 \end{array}
39 \right.
40 \end{align}
41
42 where $K$, $A$, $C_t$ and $\zeta$ are constants.  This modified function approximates a `pure' gamma function but changes the output for small inputs.
43
44
45 \section{Convert to XYZ}
46
47 This is done by multiplying the colours by a $3\times{}3$ matrix.
48 This matrix depends on the chromaticities of the RGB primaries and the
49 white point.  Note that these are the same for BT709 and sRGB, so the
50 corresponding matrices are the same.
51
52 The chromaticities for sRGB and BT709 are shown in
53 Table~\ref{tab:chromaticities}.  The white point is ($x = 0.3127$, $y
54 = 0.329$); this is called D65.
55
56 \begin{table}[ht]
57 \begin{center}
58 \begin{tabular}{|l|l|l|}
59 \hline
60 & $x$ & $y$ \\
61 \hline
62 Red & 0.64 & 0.33 \\
63 \hline
64 Green & 0.3 & 0.6 \\
65 \hline
66 Blue & 0.15 & 0.06 \\
67 \hline
68 \end{tabular}
69 \end{center}
70 \caption{RGB chromaticities for sRGB and BT709}
71 \label{tab:chromaticities}
72 \end{table}
73
74 Let
75
76 \begin{align}
77 D &= \left|\begin{matrix} R_x - W_x & W_x - B_x \\ R_y - W_y & W_y - B_y \end{matrix} \right| \\
78 E &= \left|\begin{matrix} W_x - G_x & R_x - W_x \\ W_y - G_y & R_y - W_y \end{matrix} \right| \\
79 F &= \left|\begin{matrix} W_x - G_x & W_x - B_x \\ W_y - G_y & W_y - B_y \end{matrix} \right| \\
80 P &= R_y + G_y \frac{D}{F} + B_y \frac{E}{F}
81 \end{align}
82
83 where:
84
85 \begin{itemize}
86 \item $R_x$, $R_y$: red point; $R_z = 1 - R_x - R_y$
87 \item $G_x$, $G_y$: green point; $G_z = 1 - G_x - G_y$
88 \item $B_x$, $B_y$: blue point; $B_z = 1 - B_x - B_y$
89 \item $W_x$, $W_y$: white point
90 \end{itemize}
91
92 Then the conversion matrix $\mathbf{C}$ is as follows:
93
94 \begin{align}
95 \mathbf{C} &= \frac{1}{P} \left[\begin{matrix}
96 R_x & G_x\dfrac{D}{F} & B_x\dfrac{E}{F} \\[2ex]
97 R_y & G_y\dfrac{D}{F} & B_y\dfrac{E}{F} \\[2ex]
98 R_z & G_z\dfrac{D}{F} & B_z\dfrac{E}{F} \\[2ex]
99 \end{matrix}\right] = \left[\begin{matrix}
100 0.4123908 & 0.3575843 & 0.1804808 \\
101 0.2126390 & 0.7151687 & 0.0721923 \\
102 0.0193308 & 0.1191948 & 0.9505322 \\
103 \end{matrix}\right]
104 \end{align}
105
106 Then to convert RGB to XYZ we do
107
108 \begin{align}
109 \left[\begin{matrix} X \\ Y \\ Z \\ \end{matrix}\right] &= \mathbf{C} \left[\begin{matrix} R \\ G \\ B \\ \end{matrix}\right]
110 \intertext{i.e.}
111 \left[\begin{matrix} X \\ Y \\ Z \\ \end{matrix}\right] &= \left[\begin{matrix}
112 0.4123908 & 0.3575843 & 0.1804808 \\
113 0.2126390 & 0.7151687 & 0.0721923 \\
114 0.0193308 & 0.1191948 & 0.9505322 \\
115 \end{matrix}\right] \left[\begin{matrix} R \\ G \\ B \\ \end{matrix}\right]
116 \end{align}
117
118 Note: there is also a CIE definition of the D65 white point as ($x =
119 0.31272$, $y = 0.32903$), which we do not use.
120
121
122 \section{Adjust the white point}
123
124 If required we can adjust the white point of the colours from one
125 white point $\mathbf{S_1}$ to another $\mathbf{S_2}$.  This is done by multiplication by
126 a Bradford matrix, $\mathbf{B}$.  To calculate it, we start with the
127 Bradford chromatic adaption transform matrix $\mathbf{M}$, taken from
128 S\"usstrunk et al., \textit{Chromatic adaption performance of
129   different RGB sensors}, IS\&T/SPIE Electronic Imaging, SPIE
130 Vol.\ 4300 (2001).
131
132 \begin{align}
133 \mathbf{M} &= \left[\begin{matrix}
134 0.8951 & 0.2664 & -0.1614 \\
135 -0.7502 & 1.7135 & 0.0367 \\
136 0.0389 & -0.0685 & 1.0296 \\
137 \end{matrix}\right]
138 \end{align}
139
140 The inverse of $\mathbf{M}$ is
141 \begin{align}
142 \mathbf{M}^{-1} &= \left[\begin{matrix}
143 0.9869929055 & -0.1470542564 & 0.1599626517 \\
144 0.4323052697 & 0.5183602715 & 0.0492912282 \\
145 -0.0085286646 & 0.0400428217 & 0.9684866958 \\
146 \end{matrix}\right]
147 \end{align}
148
149 Next, compute $\mathbf{G}$ and $\mathbf{H}$ as follows
150
151 \begin{align}
152 \mathbf{G} &= \mathbf{M} \left[\begin{matrix} \dfrac{S_{1x}}{S_{1y}} \\[2ex] 1 \\ \dfrac{1 - S_{1x} - S_{1y}}{S_{1y}} \\[2ex] \end{matrix}\right] \\
153 \mathbf{H} &= \mathbf{M} \left[\begin{matrix} \dfrac{S_{2x}}{S_{2y}} \\[2ex] 1 \\ \dfrac{1 - S_{2x} - S_{2y}}{S_{2y}} \\[2ex] \end{matrix}\right]
154 \end{align}
155
156 Then the Bradford matrix $\mathbf{B}$ is given by
157
158 \begin{align}
159 \mathbf{B} &= \mathbf{M}^{-1}
160 \left(\left[\begin{matrix} \dfrac{H_1}{G_1} & 0 & 0 \\ 0 & \dfrac{H_2}{G_2} & 0 \\ 0 & 0 & \dfrac{H_3}{G_3} \\ \end{matrix}\right] \mathbf{M}\right) \\
161 \end{align}
162
163
164 \section{Normalize values}
165
166 Here we just multiply all colour values by the constant $N$ where
167
168 \begin{align}
169 N &= \frac{48}{52.37}
170 \end{align}
171
172
173 \section{Convert to non-linear XYZ}
174
175 This is a gamma correction of $1/2.6$, so that for some colour value $C_i$ the output colour $C_o$ is given by
176
177 \begin{align}
178 C_o &= C_i ^ {1/2.6}
179 \end{align}
180
181
182 \section{Converting from a colour matrix to chromaticities}
183
184 To get back from a colour matrix $\mathbf{C}$ to the chromaticities:
185
186 \begin{align}
187 R_x &= \frac{C_{11}}{C_{11} + C_{21} + C_{31}} \\
188 R_y &= \frac{C_{21}}{C_{11} + C_{21} + C_{31}} \\
189 G_x &= \frac{C_{12}}{C_{12} + C_{22} + C_{32}} \\
190 G_y &= \frac{C_{22}}{C_{12} + C_{22} + C_{32}} \\
191 B_x &= \frac{C_{13}}{C_{13} + C_{23} + C_{33}} \\
192 B_y &= \frac{C_{23}}{C_{13} + C_{23} + C_{33}} \\
193 W_x &= \frac{C_{11} + C_{12} + C_{13}}{C_{11} + C_{12} + C_{13} + C_{21} + C_{22} + C_{23} + C_{31} + C_{32} + C_{33}} \\
194 W_y &= \frac{C_{21} + C_{22} + C_{23}}{C_{11} + C_{12} + C_{13} + C_{21} + C_{22} + C_{23} + C_{31} + C_{32} + C_{33}}
195 \end{align}
196
197 \end{document}