README.md
1# HDR tools
2
3This directory contains a small set of command-line tools for HDR conversions,
4including to SDR.
5
6## Tone mapping
7
8`tools/tone_map` implements tone mapping as described in annex 5 of
9[Report ITU-R BT.2408-4](https://www.itu.int/pub/R-REP-BT.2408-4-2021), more
10specifically the YRGB variant. Since the result may contain out-of-gamut colors,
11it additionally does very basic gamut mapping, maintaining hue and luminance at
12the expense of saturation (so bright colorful highlights may be brought closer
13to white).
14
15### Examples
16
17```shell
18# Tone maps a PQ image for a 300 cd/m² display, and writes the result as an SDR
19# (but still wide-gamut) image to be shown on such a display.
20$ tools/tone_map -t 300 ClassE_507.png ClassE_507_tone_mapped_300.png
21
22# The result can also be written as a PQ image itself:
23$ tools/tone_map -t 300 --pq ClassE_507.png ClassE_507_tone_mapped_300_pq.png
24
25# It is possible to specify the maximum luminance found in the image using
26# `--max_nits`. For OpenEXR input, it will override the `whiteLuminance` tag
27# which indicates the luminance of (1, 1, 1). For PQ, it will not affect the
28# luminance calculated from the signal, but it will tell the tone mapping how
29# much headroom to leave for highlights. Leaving more headroom than necessary
30# can help with the problem of desaturated highlights mentioned above.
31$ tools/tone_map -m 4000 -t 300 ClassE_507.png ClassE_507_tone_mapped_300.png
32```
33
34## PQ to HLG conversion
35
36`tools/pq_to_hlg` performs conversion of a PQ image to HLG as described in
37section 6 of the aforementioned BT.2408-4. That is, the PQ image is first
38limited to 1000 cd/m² using the tone mapping mentioned above, and the result is
39treated as if it were the output of a reference 1000 cd/m² HLG display: such a
40display would have a system gamma of 1.2, and therefore, we can apply the
41HLG inverse OOTF with a gamma of 1.2 to get “back” to the linear scene-referred
42signal that would have produced that output on that reference display (and then
43encode it using the OETF).
44
45As with the tone mapping tool, the `--max_nits` option can be used to guide the
461000 cd/m² limiting.
47
48### Example
49
50```shell
51$ tools/pq_to_hlg ClassE_507.png ClassE_507_hlg.png
52```
53
54## HLG rendering
55
56HLG is designed to look acceptable without specific processing on displays that
57expect a “traditional” SDR signal. Nevertheless, it is possible to optimize the
58appearance for specific viewing conditions by applying the HLG inverse OETF and
59then the OOTF with an appropriate system gamma. Here, the system gamma is
60computed using the extended model mentioned at the bottom of page 29 of
61[Report ITU-R BT.2390-9](https://www.itu.int/pub/R-REP-BT.2390-9-2021). That
62formula should work well over a wide range of display peak luminances.
63
64It is possible to specify not just the peak luminance of the target display
65(using `--target_nits`) but also the ambient luminance of the viewing
66environment using `--surround_nits`.
67
68As with the tone mapping tool, the result can be written as a PQ image. In that
69case, it would make sense, in further usage of `tools/tone_map` or
70`tools/pq_to_hlg`, to set `--max_nits` to the value that was passed as
71`--target_nits` to this tool. This also applies to the tone mapping tool.
72
73### Examples
74
75```shell
76# Renders an HLG image for a 300 cd/m² display in a 10 cd/m² room.
77$ tools/render_hlg -t 300 -s 10 ClassE_507_hlg.png ClassE_507_hlg_300.png
78
79# Renders it for a reference 1000 cd/m² display and writes the result as a PQ
80# image.
81$ tools/render_hlg -t 1000 --pq ClassE_507_hlg.png ClassE_507_hlg_pq.png
82
83# Informing pq_to_hlg about that maximum luminance then ensures proper
84# roundtripping as it will not needlessly tone map the highlights.
85$ tools/pq_to_hlg -m 1000 ClassE_507_hlg_pq.png ClassE_507_hlg_pq_hlg.png
86```
87