Code covered by the BSD License  

Highlights from
Film-like tone reproduction operator

image thumbnail
from Film-like tone reproduction operator by Jeff Mather
A film-like tone mapping function for HDR images

simulateFilm(rgbRadiance, nStops)
function rgbSimulated = simulateFilm(rgbRadiance, nStops)
%simulateFilm   Perform film-like tone mapping.
%   LDR = simulateFilm(HDR, middleEV) converts the floating-point high
%   dynamic range image HDR to a UINT8 low dynamic range image LDR using a
%   method that recreates the sensitivity of film.  The middleEV value sets
%   the middle exposure value (EV) for the rendering.  Larger EV numbers
%   will generate brighter images; smaller values of middleEV will result
%   in darker images.
%
%   The film sensitivity is set to 6 EV, which is comparable to many
%   late-model transparency (positive) films.
%
%   Example
%   -------
%
%   Render the same HDR image using different "exposure settings."
%   Essentially, simulate exposing the same scene from 0 to 12 EV in 1/2
%   stop steps.
%
%      % Read the HDR image and create a buffer for the rendered images.
%      hdr = hdrread('office.hdr');
%      s = size(hdr);
%      ldr = ones(s(1), s(2), 3, 25, 'uint8');
%
%      % Perform the tone mapping.
%      for p = 0.5:0.5:12.5
%          ldr(:,:,:,p*2) = simulateFilm(hdr, p);
%      end
%      figure; montage(ldr)

% Author: Jeff Mather
% Copyright 2006-2007 The MathWorks, Inc.
% $Revision$  $Date$


% Set the film sensitivity and the mid-tone log-radiance.
sensitivity = 6;
midPoint = 3;

minLogExposure = midPoint - sensitivity/2;
maxLogExposure = midPoint + sensitivity/2;

% Film works in stops.  Convert radiance to base 2 to compute perception.
% Values that are outside the film sensitivity are lost (min or max).
rgbRadiance = rgbRadiance * 2^(nStops);

rgbSimulated = rgbRadiance;
rgbSimulated(rgbRadiance ~= 0) = log2(rgbRadiance(rgbRadiance ~= 0));
rgbSimulated(rgbSimulated < minLogExposure) = minLogExposure;
rgbSimulated(rgbSimulated > maxLogExposure) = maxLogExposure;

% Convert to RGB.
rgbSimulated = uint8(255 * (rgbSimulated - minLogExposure) ./ ...
                     sensitivity);

Contact us at files@mathworks.com