Conversion
hdrconv.convert
HDR conversion algorithms.
This module provides functions for converting between HDR formats and applying color space and transfer function transformations:
- Gainmap conversion:
gainmap_to_hdr,hdr_to_gainmap - Apple HEIC conversion:
apple_heic_to_hdr - Color space conversion:
convert_color_space - Transfer functions:
apply_pq,inverse_pq
apple_heic_to_hdr(data)
Convert Apple HEIC gain map data to linear HDR.
Applies Apple's proprietary gain map formula to reconstruct the HDR image from the SDR base and single-channel gain map.
The reconstruction formula is
hdr_rgb = sdr_rgb * (1.0 + (headroom - 1.0) * gainmap)
Where all values are in linear light space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
AppleHeicData
|
AppleHeicData dict containing:
- |
required |
Returns:
| Type | Description |
|---|---|
HDRImage
|
HDRImage dict with the following keys: |
HDRImage
|
|
HDRImage
|
|
HDRImage
|
|
HDRImage
|
|
Note
The gain map is upscaled from 1/4 resolution using bilinear interpolation. Both base image (sRGB transfer) and gain map (Rec. 709 transfer) are linearized before applying the formula.
See Also
read_apple_heic: Read AppleHeicData from HEIC file.convert_color_space: Convert output to BT.2020 if needed.
Source code in src/hdrconv/convert/apple.py
convert_color_space(image, source_space, target_space, clip=False)
Convert image between color spaces.
Transforms RGB values from one color space to another using chromatic adaptation and matrix transformations. Input must be in linear light.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
ndarray
|
Linear RGB image data, float32, shape (H, W, 3). Values should be in linear light (not gamma-encoded). |
required |
source_space
|
str
|
Source color space identifier. Options: 'bt709' (Rec. 709), 'p3' (Display P3), 'bt2020' (Rec. 2020). |
required |
target_space
|
str
|
Target color space identifier. Options: 'bt709', 'p3', 'bt2020'. |
required |
clip
|
bool
|
Whether to clip output to [0, inf). Default: False. Enable when negative values from gamut mapping are undesirable. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Converted image in target color space, same shape as input. |
ndarray
|
Values remain in linear light. |
Note
If source_space equals target_space, returns input unchanged. Uses colour-science library for accurate color transformations.
See Also
apply_pq: Apply PQ transfer function after color space conversion.gainmap_to_hdr: Includes color space conversion in HDR reconstruction.
Source code in src/hdrconv/convert/colorspace.py
gainmap_to_hdr(data, baseline_color_space='p3', alt_color_space='p3', target_color_space='bt2020')
Convert ISO 21496-1 Gainmap to linear HDR image.
Applies the gainmap to the baseline image to reconstruct the alternate (HDR) representation using the ISO 21496-1 formula:
- G' = (G^(1/gamma)) * (max - min) + min
- L = 2^G'
- HDR = L * (baseline + baseline_offset) - alternate_offset
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
GainmapImage
|
GainmapImage dict containing baseline, gainmap, and metadata. |
required |
baseline_color_space
|
str
|
Color space of baseline image. Options: 'bt709', 'p3', 'bt2020'. Default: 'p3'. |
'p3'
|
alt_color_space
|
str
|
Color space of alternate/HDR image. Options: 'bt709', 'p3', 'bt2020'. Default: 'p3'. |
'p3'
|
target_color_space
|
str
|
Target output color space. Options: 'bt709', 'p3', 'bt2020'. Default: 'bt2020'. |
'bt2020'
|
Returns:
| Type | Description |
|---|---|
HDRImage
|
HDRImage dict with the following keys: |
HDRImage
|
|
HDRImage
|
|
HDRImage
|
|
HDRImage
|
|
Note
The baseline image is assumed to be sRGB-encoded. The function automatically applies EOTF conversion to linear light before applying the gainmap formula.
See Also
hdr_to_gainmap: Inverse operation, create gainmap from HDR.convert_color_space: For additional color space transformations.
Source code in src/hdrconv/convert/gainmap.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
hdr_to_gainmap(hdr, baseline=None, color_space='bt709', icc_profile=None, gamma=1.0)
Convert linear HDR image to ISO 21496-1 Gainmap format.
Creates a gainmap by computing the log2 ratio between HDR and SDR images. If baseline is not provided, generates one by clipping HDR to [0, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdr
|
HDRImage
|
HDRImage dict with linear HDR data in any supported color space. |
required |
baseline
|
Optional[ndarray]
|
Optional pre-computed baseline (SDR) image. If None, generated by clipping HDR to [0, 1]. Expected format: float32, shape (H, W, 3), range [0, 1]. |
None
|
color_space
|
str
|
Target color space for output. Options: 'bt709', 'p3', 'bt2020'. Default: 'bt709'. |
'bt709'
|
icc_profile
|
Optional[bytes]
|
Optional ICC profile bytes to embed in output. Should match the specified color_space. |
None
|
gamma
|
float
|
Gainmap gamma parameter for encoding. Higher values compress highlights. Default: 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
GainmapImage
|
GainmapImage dict containing: |
GainmapImage
|
|
GainmapImage
|
|
GainmapImage
|
|
GainmapImage
|
|
GainmapImage
|
|
Note
Uses fixed offsets of 1/64 for both baseline and alternate to avoid division by zero in dark regions.
See Also
gainmap_to_hdr: Inverse operation, reconstruct HDR from gainmap.write_21496: Write GainmapImage to ISO 21496-1 JPEG.
Source code in src/hdrconv/convert/gainmap.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
apply_pq(linear_rgb)
Apply PQ (Perceptual Quantizer) transfer function.
Encodes linear light RGB values to PQ (SMPTE ST 2084) transfer function as specified in ITU-R BT.2100 for HDR content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
linear_rgb
|
ndarray
|
Linear RGB data, float32, shape (H, W, 3). Values should be normalized where 1.0 = 203 nits (reference white). Values above 1.0 represent HDR highlights up to ~49x (10000 nits). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
PQ-encoded data, float32, shape (H, W, 3), range [0, 1]. |
ndarray
|
Output is clipped to valid PQ range. |
Note
Uses 203 nits as reference white (PQ specification). Linear value of 1.0 maps to ~58% in PQ code values.
See Also
inverse_pq: Decode PQ back to linear light.write_22028_pq: Write PQ-encoded data to AVIF file.
Source code in src/hdrconv/convert/transfer.py
inverse_pq(pq_encoded)
Decode PQ-encoded values to linear light RGB.
Applies the inverse PQ (SMPTE ST 2084) EOTF to convert PQ-encoded values back to linear light as specified in ITU-R BT.2100.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pq_encoded
|
ndarray
|
PQ-encoded data, float32, shape (H, W, 3), range [0, 1]. Values represent 0-10000 nits in PQ perceptual scale. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Linear RGB data, float32, shape (H, W, 3). |
ndarray
|
Normalized where 1.0 = 203 nits (reference white). |
ndarray
|
HDR highlights may exceed 1.0 up to ~49x. |
Note
Uses 203 nits as reference white (PQ specification). PQ code value of ~0.58 maps to linear 1.0.
See Also
apply_pq: Encode linear light to PQ.read_22028_pq: Read PQ-encoded AVIF file.