Merge tag 'tegra-for-4.8-i2c' of git://git.kernel.org/pub/scm/linux/kernel/git/tegra...
[cascardo/linux.git] / Documentation / media / uapi / v4l / colorspaces.rst
1 .. -*- coding: utf-8; mode: rst -*-
2
3 .. _colorspaces:
4
5 ***********
6 Colorspaces
7 ***********
8
9 'Color' is a very complex concept and depends on physics, chemistry and
10 biology. Just because you have three numbers that describe the 'red',
11 'green' and 'blue' components of the color of a pixel does not mean that
12 you can accurately display that color. A colorspace defines what it
13 actually *means* to have an RGB value of e.g. (255, 0, 0). That is,
14 which color should be reproduced on the screen in a perfectly calibrated
15 environment.
16
17 In order to do that we first need to have a good definition of color,
18 i.e. some way to uniquely and unambiguously define a color so that
19 someone else can reproduce it. Human color vision is trichromatic since
20 the human eye has color receptors that are sensitive to three different
21 wavelengths of light. Hence the need to use three numbers to describe
22 color. Be glad you are not a mantis shrimp as those are sensitive to 12
23 different wavelengths, so instead of RGB we would be using the
24 ABCDEFGHIJKL colorspace...
25
26 Color exists only in the eye and brain and is the result of how strongly
27 color receptors are stimulated. This is based on the Spectral Power
28 Distribution (SPD) which is a graph showing the intensity (radiant
29 power) of the light at wavelengths covering the visible spectrum as it
30 enters the eye. The science of colorimetry is about the relationship
31 between the SPD and color as perceived by the human brain.
32
33 Since the human eye has only three color receptors it is perfectly
34 possible that different SPDs will result in the same stimulation of
35 those receptors and are perceived as the same color, even though the SPD
36 of the light is different.
37
38 In the 1920s experiments were devised to determine the relationship
39 between SPDs and the perceived color and that resulted in the CIE 1931
40 standard that defines spectral weighting functions that model the
41 perception of color. Specifically that standard defines functions that
42 can take an SPD and calculate the stimulus for each color receptor.
43 After some further mathematical transforms these stimuli are known as
44 the *CIE XYZ tristimulus* values and these X, Y and Z values describe a
45 color as perceived by a human unambiguously. These X, Y and Z values are
46 all in the range [0…1].
47
48 The Y value in the CIE XYZ colorspace corresponds to luminance. Often
49 the CIE XYZ colorspace is transformed to the normalized CIE xyY
50 colorspace:
51
52 x = X / (X + Y + Z)
53
54 y = Y / (X + Y + Z)
55
56 The x and y values are the chromaticity coordinates and can be used to
57 define a color without the luminance component Y. It is very confusing
58 to have such similar names for these colorspaces. Just be aware that if
59 colors are specified with lower case 'x' and 'y', then the CIE xyY
60 colorspace is used. Upper case 'X' and 'Y' refer to the CIE XYZ
61 colorspace. Also, y has nothing to do with luminance. Together x and y
62 specify a color, and Y the luminance. That is really all you need to
63 remember from a practical point of view. At the end of this section you
64 will find reading resources that go into much more detail if you are
65 interested.
66
67 A monitor or TV will reproduce colors by emitting light at three
68 different wavelengths, the combination of which will stimulate the color
69 receptors in the eye and thus cause the perception of color.
70 Historically these wavelengths were defined by the red, green and blue
71 phosphors used in the displays. These *color primaries* are part of what
72 defines a colorspace.
73
74 Different display devices will have different primaries and some
75 primaries are more suitable for some display technologies than others.
76 This has resulted in a variety of colorspaces that are used for
77 different display technologies or uses. To define a colorspace you need
78 to define the three color primaries (these are typically defined as x, y
79 chromaticity coordinates from the CIE xyY colorspace) but also the white
80 reference: that is the color obtained when all three primaries are at
81 maximum power. This determines the relative power or energy of the
82 primaries. This is usually chosen to be close to daylight which has been
83 defined as the CIE D65 Illuminant.
84
85 To recapitulate: the CIE XYZ colorspace uniquely identifies colors.
86 Other colorspaces are defined by three chromaticity coordinates defined
87 in the CIE xyY colorspace. Based on those a 3x3 matrix can be
88 constructed that transforms CIE XYZ colors to colors in the new
89 colorspace.
90
91 Both the CIE XYZ and the RGB colorspace that are derived from the
92 specific chromaticity primaries are linear colorspaces. But neither the
93 eye, nor display technology is linear. Doubling the values of all
94 components in the linear colorspace will not be perceived as twice the
95 intensity of the color. So each colorspace also defines a transfer
96 function that takes a linear color component value and transforms it to
97 the non-linear component value, which is a closer match to the
98 non-linear performance of both the eye and displays. Linear component
99 values are denoted RGB, non-linear are denoted as R'G'B'. In general
100 colors used in graphics are all R'G'B', except in openGL which uses
101 linear RGB. Special care should be taken when dealing with openGL to
102 provide linear RGB colors or to use the built-in openGL support to apply
103 the inverse transfer function.
104
105 The final piece that defines a colorspace is a function that transforms
106 non-linear R'G'B' to non-linear Y'CbCr. This function is determined by
107 the so-called luma coefficients. There may be multiple possible Y'CbCr
108 encodings allowed for the same colorspace. Many encodings of color
109 prefer to use luma (Y') and chroma (CbCr) instead of R'G'B'. Since the
110 human eye is more sensitive to differences in luminance than in color
111 this encoding allows one to reduce the amount of color information
112 compared to the luma data. Note that the luma (Y') is unrelated to the Y
113 in the CIE XYZ colorspace. Also note that Y'CbCr is often called YCbCr
114 or YUV even though these are strictly speaking wrong.
115
116 Sometimes people confuse Y'CbCr as being a colorspace. This is not
117 correct, it is just an encoding of an R'G'B' color into luma and chroma
118 values. The underlying colorspace that is associated with the R'G'B'
119 color is also associated with the Y'CbCr color.
120
121 The final step is how the RGB, R'G'B' or Y'CbCr values are quantized.
122 The CIE XYZ colorspace where X, Y and Z are in the range [0…1] describes
123 all colors that humans can perceive, but the transform to another
124 colorspace will produce colors that are outside the [0…1] range. Once
125 clamped to the [0…1] range those colors can no longer be reproduced in
126 that colorspace. This clamping is what reduces the extent or gamut of
127 the colorspace. How the range of [0…1] is translated to integer values
128 in the range of [0…255] (or higher, depending on the color depth) is
129 called the quantization. This is *not* part of the colorspace
130 definition. In practice RGB or R'G'B' values are full range, i.e. they
131 use the full [0…255] range. Y'CbCr values on the other hand are limited
132 range with Y' using [16…235] and Cb and Cr using [16…240].
133
134 Unfortunately, in some cases limited range RGB is also used where the
135 components use the range [16…235]. And full range Y'CbCr also exists
136 using the [0…255] range.
137
138 In order to correctly interpret a color you need to know the
139 quantization range, whether it is R'G'B' or Y'CbCr, the used Y'CbCr
140 encoding and the colorspace. From that information you can calculate the
141 corresponding CIE XYZ color and map that again to whatever colorspace
142 your display device uses.
143
144 The colorspace definition itself consists of the three chromaticity
145 primaries, the white reference chromaticity, a transfer function and the
146 luma coefficients needed to transform R'G'B' to Y'CbCr. While some
147 colorspace standards correctly define all four, quite often the
148 colorspace standard only defines some, and you have to rely on other
149 standards for the missing pieces. The fact that colorspaces are often a
150 mix of different standards also led to very confusing naming conventions
151 where the name of a standard was used to name a colorspace when in fact
152 that standard was part of various other colorspaces as well.
153
154 If you want to read more about colors and colorspaces, then the
155 following resources are useful: :ref:`poynton` is a good practical
156 book for video engineers, :ref:`colimg` has a much broader scope and
157 describes many more aspects of color (physics, chemistry, biology,
158 etc.). The
159 `http://www.brucelindbloom.com <http://www.brucelindbloom.com>`__
160 website is an excellent resource, especially with respect to the
161 mathematics behind colorspace conversions. The wikipedia
162 `CIE 1931 colorspace <http://en.wikipedia.org/wiki/CIE_1931_color_space#CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space>`__
163 article is also very useful.