Bilateral tone mapping¶
| Fact | Value |
|---|---|
| Also called | Durand-Dorsey tone mapping, Fast bilateral tone mapping, Base and detail tone mapping |
| Status | Implemented and tested |
| Family | Enhancement, illumination and color |
| Engine | Browser |
| Type | Nonlinear, color, base and detail decomposition with base compression |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/enhancement/tonemap.ts, bilateralTonemapFilter |
| Operator code | web/src/core/filters/enhancement/tonemap.ts, bilateralTonemap |
Overview¶
Durand and Dorsey split the log brightness of an image into two layers. The base layer is a bilateral filter of it: the broad areas of light, with sharp edges between them. The detail layer is what is left. Only the base is compressed, so a dark room and a bright window come closer together while the texture in both keeps its contrast.
How it works¶
- Compute the intensity of each pixel and keep its color ratios.
- Take the log of the intensity and smooth it with a bilateral filter. This is the base layer.
- Subtract the base from the log intensity. This is the detail layer.
- Scale the base down to the target contrast, add the detail back, and restore the colors.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): The intensity (20 R + 40 G + B) / 61
- \(B\): The base layer, the bilateral filter of log10 I
- \(D\): The detail layer
- \(C\): The target base contrast
- \(O\): The log10 of the output intensity
I is the intensity (20 R + 40 G + B) / 61. The bilateral filter of the log intensity is the base layer B, the large shapes of the light. What is left, D, is the detail. The base is scaled by c, so that its range becomes log10 C, and shifted so that its largest value becomes 0. The detail is not scaled, so it keeps its contrast. Each channel becomes its ratio to I times 10^O, clipped to 1. Durand and Dorsey keep sigma_s at 2 percent of the image size and found sigma_r = 0.4 and a base contrast of 5 to work well. The bilateral filter is their piecewise-linear version. It filters at NB evenly spaced levels of log I, with NB = (max - min) / sigma_r rounded up. Each pixel mixes the two levels nearest to its value. PixelNexus's own choices are linear light for 8-bit images, intensities of at least 1e-4, the longer side as the image size, and bilinear upsampling.
In the log domain, a ratio of brightness is a difference. Scaling the base by c < 1 shrinks all large ratios, such as between light and shadow, by the same power. The detail D is added back unscaled, so small ratios, the texture, stay as they were. The bilateral filter makes this work at edges. It averages only pixels of a similar log intensity. So the step from shadow to light stays in the base and is compressed, instead of going into the detail and making a halo.
A brute-force bilateral filter with sigma_s at 2 percent of the image is slow. Durand and Dorsey's piecewise-linear version picks levels of log intensity sigma_r apart. For each level it computes a normalized Gaussian blur weighted by how close each pixel is to that level. Every pixel then mixes the results of the two levels next to its own value. The blurs can run on a smaller copy, every z-th pixel, while the final mixing uses every pixel. In PixelNexus the Gaussian is the exact one on the mirrored image, through the FFT of each row and column.
Kernel or operator¶
The Inspector shows the four stages, from intensity to color, as formulas with the current settings.
Algorithm¶
- Decode to linear light, compute the intensity and the color ratios.
- Take log10 of the intensity.
- Filter it with the piecewise-linear bilateral filter to get the base layer, on every z-th pixel, with a Gaussian through the FFT of each row and column.
- Subtract the base from the log intensity to get the detail layer.
- Scale and shift the base, add the detail, and raise 10 to the result.
- Multiply the color ratios by the new intensity, clip to 1 and encode back to sRGB.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Base contrast contrast |
5 | 1.5 to 100 | The ratio between the brightest and the darkest base value after compression. |
Spatial sigma spatial |
2 | 0.2 to 20 % | The spatial scale sigma_s of the bilateral filter, in percent of the longer side of the image. |
Range sigma range |
0.4 | 0.05 to 2 | The range scale sigma_r of the bilateral filter, in log10 units of intensity. |
Downsampling downsample |
4 | 1 to 16 | The bilateral filter runs on every z-th pixel in each direction, as in the paper. 1 runs it on every pixel. |
Linear light linearLight |
on | On or off | Decode the sRGB values to linear light first, so the intensity is proportional to light. |
Implementation¶
bilateralTonemapFilter in web/src/core/filters/enhancement/tonemap.ts calls bilateralTonemap, which uses piecewiseBilateral for the base layer. The Gaussian is mirrorGaussian from the Retinex code. It is a global color filter, so it always sees the whole image.
Worked example¶
| Image | Lit half luma | Shaded half luma |
|---|---|---|
| Scene | 175.6 | 32 |
| Base contrast 3 | 211.1 | 177.4 |
| Base contrast 5 | 209.1 | 155.1 |
| Base contrast 10 | 206.6 | 128.2 |
The scene of the figure, 240 by 120 pixels: patches in full light on the left, and the same patches in a shadow that lets 3 percent of the light through on the right. Luma is 0.299 R + 0.587 G + 0.114 B of the encoded values, averaged over each half without the 10 columns next to the shadow edge. The other settings are the defaults.
The shaded half has a luma of 32 before the filter. With the default base contrast of 5 it rises to 155, while the lit half stays near 210. A lower base contrast brings the two halves closer.
Complexity¶
- Time: \(O(N log N + NB N / z^2)\)
- Memory: \(O(N)\)
N is the number of pixels, NB the number of levels and z the downsampling. The cost does not depend on sigma_s.
Artifacts and limitations¶
Limitations
- Small halos can appear at antialiased or very high-contrast edges. The paper's fix for uncertain pixels is not included.
- An 8-bit photo has little detail left in its deepest shadows, so lifting them also lifts noise.
Artifacts
- Noise in lifted shadows.
- Thin bright or dark rims at some strong edges.
Tests¶
The fixture cases come from a NumPy reference of the same steps, with downsampling 1, 2 and 3, in linear light and on encoded values. Before the cases are written, the piecewise filter is checked against two limits: with a huge sigma_r it is the Gaussian blur, and two levels far apart stay apart. Unit tests check the same limits and that two flat areas 100 to 1 apart come out exactly at the target contrast of 5, with the brighter one white.
web/src/core/filters/filters.test.tsweb/src/core/filters/run.test.tsweb/src/core/filters/enhancement/tonemap.test.ts
References¶
- Durand, F. and Dorsey, J. Fast bilateral filtering for the display of high-dynamic-range images. ACM Transactions on Graphics 21(3), 257 to 266, 2002. DOI 10.1145/566654.566574.
- Durand, F. Fast Bilateral Filtering for the Display of High-Dynamic-Range Images, the paper's page with the tone mapping recipe. https://people.csail.mit.edu/fredo/PUBLI/Siggraph2002/