Tone mapping and shadow recovery using GIMP's 'Colors/Exposure'
This tutorial shows you how to use high bit depth GIMP's floating point "Colors/Exposure" operation to add one or more stops of positive exposure compensation to an image's shadows and midtones, without blowing out the highlights.
Written March 2016. Updated May 2016.
High bit depth GIMP's floating point "Colors/Exposure": much better than Curves for lightening the shadows and midtones of an image without blowing the highlights
A very common editing problem is how to lighten the shadows and midtones of an image without blowing out the highlights, which problem is very often encountered when dealing with photographs of scenes lit by direct sunlight. Precanned algorithms for accomplishing this task are often referred to as "shadow recovery" algorithms. But really these algorithms are special-purpose tone-mapping algorithms, which sometimes work pretty well, and sometimes not so well, depending on the algorithm, the image, and your artistic intentions for the image.
This step-by-step tutorial shows you how to use GIMP's unbounded floating point "Colors/Exposure" operation to recover shadow information — that is, add one or more stops of positive exposure compensation to an image's shadows and midtones — without blowing out or unduly compressing the image highlights. The procedure is completely "hand-tunable" using masks and layers, and is as close as you can get to non-destructive image editing using high bit depth GIMP 2.9/2.10.
High bit depth GIMP is my primary image editor, and I've used the procedure described below for several years as my "go to" way to modify image tonality. The same general procedure can be used to darken as well as lighten portions of an image, again controlling the effect using a layer mask. This isn't exactly nondestructive editing because at some point you need to make a "New from Visible" layer. But unlike using Curves, using high bit depth GIMP's floating point "Colors/Exposure" doesn't clip RGB channel values and allows you to fine-tune the results by modifying and remodifying the layer mask until you are completely happy with the resulting tonality.
A worked example showing how to recover shadow information using high bit depth GIMP's floating point "Colors/Exposure"
The worked example given below is broken down into five steps, starting with downloading the image. Steps 3, 4, and 5 describe the actual procedure:
- Download tree.png, which is a 16-bit integer sRGB image. High bit depth GIMP 2.9/2.10 really is an "sRGB only" image editor, so it's best if you don't even try to edit in other RGB working spaces.
- Open tree.png with GIMP and assign the GIMP built-in sRGB profile (the image colors won't change a bit). Then convert the image to 32-bit floating point linear precision: Go to "Image/Precision", select "32-bit floating point", and when the Dither dialog pops up, select "Linear light" (this ensures that the Normal blend mode produces radiometrically correct results).
- Make a copy of the "tree.png" layer, and label it "+1 stop exposure comp". Then use "Colors/Exposure" to add one stop of positive exposure compensation — Figure 3 shows the result.
The image in Figure 3 clearly has "blown" highlights in the sky. But the highlights aren't really blown (that is, clipped to 1.0 in one or more channels). Instead the highlight information is still there, but some of the RGB channel values fall outside the RGB display channel value range of 0.0f to 1.0f.
- Add an inverse grayscale layer mask: Right-click on the layer and select "Layer/Mask/Add Layer Mask", and when the "Add a mask to the Layer" dialog pops up, choose "Grayscale copy of layer" and check the "Invert mask" box.
As shown in Figure 4, at this point the highlights will be brought back into the display range, meaning all RGB channel values are between 0.0f and 1.0f. But the image will probably look a little odd (sort of cloudy and flat), and depending on the image, the brightest highlights might actually have dark splotches — don't worry! this is temporary.
- Click on the layer mask to select it for editing, and then do "Colors/Auto/Stretch Contrast".
"Auto/Stretch Contrast" on the mask is necessary because just like the image layer has out of gamut RGB channel values, the inverted grayscale mask contains out of gamut grayscale values. "Auto/Stretch Contrast" brings all the mask grayscale values back into the display range, allowing the mask to proportionately compensate for the layer's otherwise out-of-gamut RGB channel values, masking more in the layer highlights and less/not at all in the image's shadows and midtones."Keep Colors" should be checked (though it doesn't really matter on grayscale images such as layer masks). Figure 5 shows the result.
That's the whole procedure for using "Colors/Exposure" to add a stop of positive exposure compensation to the shadows without blowing out the highlights. Now you can either fine-tune the mask, or else just make a "New from Visible" layer and continue editing your nicely brightened image. Depending on the image and also on your artistic intentions for the image, the mask might not need fine-tuning. But very often you'll want to modify the resulting tonal distribution by doing a "Colors/Exposure" gamma correction, or perhaps a Curves operation on the mask, or else by painting directly on the mask. And sometimes you'll want to blur the mask to restore micro contrast.
- An essential component of the procedure for using "Colors/Exposure" to add positive exposure compensation to images with dark shadows and midtones needs to be explicitly mentioned: Not only is the high bit depth GIMP's "Colors/Exposure" operation unbounded at floating point precision — layer masks are also unbounded.
If the inverted grayscale masks were summarily clipped (as is the case when editing at integer precision), then the procedure described in this tutorial wouldn't work.
- Depending on your particular artistic intentions for an image, some images are more likely than others to benefit from being tone mapped using floating point "Colors/Exposure". Your mileage may vary, but typically the procedure described on this page works best for photographs of scenes with a pronounced tonal difference between the highlights and shadows, as per typical sunny day "sky-ground" photographs.
- For adding just one stop of positive exposure compensation, the procedure described on this page works really well. Depending on the image you might want to blur the mask using an edge-respecting blur algorithm, and/or tweak the mask using "Colors/Exposure", Curves, etc. But only modify the mask after using Auto Stretch Contrast on the mask. Otherwise results will be unpredictable: Gamma adjustments produce odd results when operating on out of gamut values, and Curves will summarily clip out of gamut values.
- For adding more than one stop of exposure compensation, sometimes it works out when you just add more than one stop of exposure compensation on a single layer. Often you get better results using more than one positive-exposure-compensation layer. Either way the layer mask(s) will need careful tweaking that's very image-specific and also specific to your intended result. Figure 6 shows an example of using two exposure compensation layers to add two and a half stops of exposure compensation to the shadows and midtones of an image:
Tone-mapping by hand gives you complete control over the resulting image. Mantuik and other "automagic" tone-mapping algorithms often produce unnatural-looking results, and tend to be CPU-intensive and unpredictable.
The screenshot to the right shows the layer stack that I used to tone-map the photograph of the apple orchard truck.
- Before using "Colors/Exposure" to add positive exposure compensation, the base layer should already be stretched to its maximum dynamic range. The easiest way to stretch the base layer to its maximum dynamic range is to do "Colors/Auto/Stretch Contrast" and make sure that "Keep colors" is checked.
If you've never used an unbounded floating point image editor before, "Colors/Auto/Stretch Contrast" can produce an unexpected result: The image might actually end up with a severely reduced dynamic range, having either lighter shadows or darker highlights or both:
As captured by the raw file, the image in Figure 7 of power lines marching into the distance is a typical result of taking a photograph at noon on a bright sunny day: The sky and clouds look pretty good right out of the camera, but the ground is far too dark. So the image can benefit from some tone mapping to raise the shadows and midtones. The first step is to do "Colors/Auto/Stretch Contrast" to bring any channel values that are less than 0.0f or greater than 1.0f back within the display range of 0.0 to 1.0 floating point.
Performing "Auto/Stretch Contrast" to bring the channel values back inside the display range doesn't exactly look like an editing step in the right direction for tone-mapping this particular image! but really it is. Using "Colors/Exposure" to add positive exposure compensation to the shadows and midtones won't work if the image has channel values that fall outside the display range.
- Dispensing with "useless" shadow and highlight information: Sometimes interpolated raw files of photographs of high dynamic range scenes end up with a sprinkling of highlight and shadow pixels that contains essentially no useful information. The easiest thing to do with such pixels is to use "Colors/Exposure" to set the desired black and white points, and then clip the resulting out of gamut channel information.
- Useless highlight information:
For the "Power lines" picture shown in Figure 8 above, after doing "Color/Auto/Stretch Contrast", a measly 48 pixels occupied nearly half the tonal range (see the histogram to the right). A little investigation with GIMP's Threshold tool revealed that all 48 pixels are the peak values of specular highlights on the ceramic insulators on the power line pole in the foreground.
In cases where nearly half the histogram is occupied by a sprinkling of specular highlights, clipping the pixels is often the best and easiest solution. For the "Power lines" image, the 48 pixels in question carried essentially zero information. So I used "Colors/Exposure" to raise the white point, and then used "Tools/GEGL Operation/Clip RGB" to actually clip the channel information in the highlights (this time making sure the "Clip high pixel values" box was checked).
- Useless shadow information:
Some raw processors can output images with negative channel values. And previous edits using high bit depth GIMP might have produced negative channel values. If doing an "Auto/Stretch Contrast" on your base image layer makes the image a whole lot lighter in the shadows, the problem is negative RGB channel values. One solution is to use "Colors/Exposure" to move the black point to where you want it to be, and then clip the negative channel values. Here are two ways to clip negative channel values:
- Use "Tools/GEGL Operation/Clip RGB", making sure to uncheck the "Clip high pixel values" box.
- Or else create a solid black layer above your base image layer, set the blend mode to "Lighten only", and make a "New from Visible" layer.
- Useless highlight information:
- Blurring the mask to restore micro contrast: Putting an inverse mask on a layer that's used to add positive exposure compensation necessarily slightly flattens micro contrast. Depending on your artistic intentions for the image, you might want to blur the mask to restore micro contrast. The trick is how to blur the mask without introducing "halos" around the edges of objects in the image. Small radius gaussian blurs produce small but distressingly obvious halos around dark edges. A large radius gaussian blur sometimes works but just as often produces a large obvious halo separating the brighter and darker portions of the image. As illustrated in Figure 8, for many images a better solution is to blur the mask use an edge-respecting filter such as GIMP's Selective Gaussian Blur or G'MIC bilateral smooth filter.
Adding exposure compensation combined with an inverse grayscale layer mask does flatten micro contrast, which might or might not be desireable depending on your artistic intentions for the image. To restore micro contrast, try using an edge-respecting blur such as G'MIC's bilateral smoothing filter.
Photographs taken in bright direct sunlight typically are of high dynamic range scenes, and the resulting camera file usually requires careful tone mapping to produce a satisfactory final image. High bit depth GIMP's floating point floating point "Colors/Exposure" provides a very useful tool for dealing with this type of image, and of course is equally useful for any image where the goal is to raise the shadows and midtones without blowing out the highlights.
This is a GIMP-specific tutorial. However, the same technique can be employed using the PhotoFlow raw processor and possibly other image editors that allow for 32-bit floating point processing using unbounded RGB channel values. The neat thing about using this technique in PhotoFlow is that PhotoFlow uses nodes, which allows for completely non-destructive editing of the inverted grayscale mask that's used to recover the highlight detail after applying positive exposure compensation to raise the tonality of the shadows and midtones (even if you close and reopen the image, if you save the image's PFI file).