A comprehensive guide to tone mapping - the process of converting high dynamic range (HDR) images to displayable low dynamic range (LDR) formats while preserving visual detail and maintaining natural appearance across different lighting conditions.
Tone mapping is a crucial technique in digital imaging that compresses the wide range of luminance values found in real-world scenes or HDR images into the limited dynamic range that can be displayed on standard monitors or printed media. This process aims to preserve the visual impression and detail of the original scene while making it viewable on conventional display devices.
Professional photo editing and enhancement tools powered by AI for photographers and content creators.
Remove backgrounds instantly • Resize without quality loss • Extract color palettes • Professional results
The human visual system can perceive a dynamic range of approximately 10,000:1 in a single scene, while typical displays can only reproduce about 100:1 to 1,000:1. Tone mapping bridges this gap:
// Comprehensive tone mapping processor with multiple algorithms
class ToneMappingProcessor {
constructor() {
this.algorithms = {
reinhard: 'Global Reinhard operator',
reinhardLocal: 'Local adaptation Reinhard',
aces: 'ACES filmic tone mapping',
uncharted2: 'Uncharted 2 filmic curve',
drago: 'Drago logarithmic mapping'
};
}
// Global Reinhard tone mapping operator
reinhardGlobal(hdrData, options = {}) {
const { keyValue = 0.18, whitePoint = null } = options;
const { luminanceData, width, height } = this.extractLuminance(hdrData);
// Calculate log-average luminance
const logAvgLuminance = this.calculateLogAverageLuminance(luminanceData);
const actualWhitePoint = whitePoint || this.calculateWhitePoint(luminanceData);
const result = new Float32Array(luminanceData.length);
for (let i = 0; i < luminanceData.length; i++) {
const worldLuminance = luminanceData[i];
const scaledLuminance = (keyValue / logAvgLuminance) * worldLuminance;
// Apply Reinhard operator
const numerator = scaledLuminance * (1 + scaledLuminance / (actualWhitePoint * actualWhitePoint));
const denominator = 1 + scaledLuminance;
result[i] = numerator / denominator;
}
return this.reconstructRGB(hdrData, result, luminanceData);
}
// ACES filmic tone mapping
acesFilmic(hdrData, options = {}) {
const { exposure = 1.0 } = options;
const { data, width, height } = hdrData;
const result = new Float32Array(data.length);
// ACES constants
const a = 2.51, b = 0.03, c = 2.43, d = 0.59, e = 0.14;
for (let i = 0; i < data.length; i += 4) {
const r = data[i] * exposure;
const g = data[i + 1] * exposure;
const b_val = data[i + 2] * exposure;
// ACES tone mapping formula
result[i] = this.acesToneMapChannel(r, a, b, c, d, e);
result[i + 1] = this.acesToneMapChannel(g, a, b, c, d, e);
result[i + 2] = this.acesToneMapChannel(b_val, a, b, c, d, e);
result[i + 3] = data[i + 3];
}
return { data: result, width, height, algorithm: 'aces' };
}
acesToneMapChannel(x, a, b, c, d, e) {
return Math.max(0, Math.min(1, (x * (a * x + b)) / (x * (c * x + d) + e)));
}
// Uncharted 2 filmic tone mapping
uncharted2Filmic(hdrData, options = {}) {
const { exposureBias = 2.0, whiteScale = 11.2 } = options;
const { data, width, height } = hdrData;
const result = new Float32Array(data.length);
// Uncharted 2 constants
const A = 0.15, B = 0.50, C = 0.10, D = 0.20, E = 0.02, F = 0.30;
const whiteOutputLevel = this.uncharted2Function(whiteScale, A, B, C, D, E, F);
for (let i = 0; i < data.length; i += 4) {
const r = data[i] * exposureBias;
const g = data[i + 1] * exposureBias;
const b = data[i + 2] * exposureBias;
result[i] = this.uncharted2Function(r, A, B, C, D, E, F) / whiteOutputLevel;
result[i + 1] = this.uncharted2Function(g, A, B, C, D, E, F) / whiteOutputLevel;
result[i + 2] = this.uncharted2Function(b, A, B, C, D, E, F) / whiteOutputLevel;
result[i + 3] = data[i + 3];
}
return { data: result, width, height, algorithm: 'uncharted2' };
}
uncharted2Function(x, A, B, C, D, E, F) {
return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F;
}
// Extract luminance channel from HDR data
extractLuminance(hdrData) {
const { data, width, height } = hdrData;
const luminanceData = new Float32Array(width * height);
for (let i = 0; i < data.length; i += 4) {
const r = data[i], g = data[i + 1], b = data[i + 2];
// ITU-R BT.709 luminance weights
luminanceData[i / 4] = 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
return { luminanceData, width, height };
}
// Calculate log-average luminance
calculateLogAverageLuminance(luminanceData) {
const delta = 0.0001;
let logSum = 0, validPixels = 0;
for (let i = 0; i < luminanceData.length; i++) {
const luminance = luminanceData[i];
if (luminance > 0) {
logSum += Math.log(luminance + delta);
validPixels++;
}
}
return Math.exp(logSum / validPixels);
}
// Calculate white point (95th percentile)
calculateWhitePoint(luminanceData) {
const sorted = [...luminanceData].sort((a, b) => a - b);
return sorted[Math.floor(sorted.length * 0.95)];
}
// Reconstruct RGB from tone-mapped luminance
reconstructRGB(originalHdrData, toneMappedLuminance, originalLuminance) {
const { data, width, height } = originalHdrData;
const result = new Float32Array(data.length);
for (let i = 0; i < data.length; i += 4) {
const pixelIndex = i / 4;
const originalLum = originalLuminance[pixelIndex];
const newLum = toneMappedLuminance[pixelIndex];
if (originalLum > 0) {
const scale = newLum / originalLum;
result[i] = Math.max(0, Math.min(1, data[i] * scale));
result[i + 1] = Math.max(0, Math.min(1, data[i + 1] * scale));
result[i + 2] = Math.max(0, Math.min(1, data[i + 2] * scale));
} else {
result[i] = result[i + 1] = result[i + 2] = 0;
}
result[i + 3] = data[i + 3];
}
return { data: result, width, height };
}
// Apply exposure adjustment
applyExposureAdjustment(imageData, exposureStops) {
const { data, width, height } = imageData;
const result = new Float32Array(data.length);
const multiplier = Math.pow(2, exposureStops);
for (let i = 0; i < data.length; i += 4) {
result[i] = data[i] * multiplier;
result[i + 1] = data[i + 1] * multiplier;
result[i + 2] = data[i + 2] * multiplier;
result[i + 3] = data[i + 3];
}
return { data: result, width, height };
}
// Analyze tone mapping quality
analyzeToneMappingQuality(originalHdr, toneMapped) {
const originalLum = this.extractLuminance(originalHdr).luminanceData;
const mappedLum = this.extractLuminance(toneMapped).luminanceData;
let contrastPreservation = 0;
let naturalness = 0;
// Analyze contrast preservation
for (let i = 1; i < originalLum.length - 1; i++) {
const originalContrast = Math.abs(originalLum[i] - originalLum[i - 1]);
const mappedContrast = Math.abs(mappedLum[i] - mappedLum[i - 1]);
if (originalContrast > 0) {
contrastPreservation += Math.min(1, mappedContrast / originalContrast);
}
}
contrastPreservation /= (originalLum.length - 1);
// Analyze naturalness (avoid clipping)
let naturalPixels = 0;
for (let i = 0; i < mappedLum.length; i++) {
if (mappedLum[i] > 0.05 && mappedLum[i] < 0.95) naturalPixels++;
}
naturalness = naturalPixels / mappedLum.length;
const overallQuality = (contrastPreservation + naturalness) / 2;
return {
overallQuality,
contrastPreservation,
naturalness,
grade: overallQuality > 0.8 ? 'excellent' :
overallQuality > 0.6 ? 'good' :
overallQuality > 0.4 ? 'fair' : 'poor'
};
}
}// Complete HDR processing workflow
class HDRProcessor {
constructor() {
this.toneMappingProcessor = new ToneMappingProcessor();
}
// Process HDR image with comprehensive settings
processHDRImage(hdrImageData, settings = {}) {
const {
algorithm = 'aces',
exposure = 0,
highlights = 0,
shadows = 0,
contrast = 0,
saturation = 1.0
} = settings;
// Apply exposure adjustment
const exposureAdjusted = this.toneMappingProcessor.applyExposureAdjustment(
hdrImageData, exposure
);
// Apply tone mapping
let toneMapped;
switch (algorithm) {
case 'reinhard':
toneMapped = this.toneMappingProcessor.reinhardGlobal(exposureAdjusted);
break;
case 'aces':
toneMapped = this.toneMappingProcessor.acesFilmic(exposureAdjusted);
break;
case 'uncharted2':
toneMapped = this.toneMappingProcessor.uncharted2Filmic(exposureAdjusted);
break;
default:
toneMapped = this.toneMappingProcessor.acesFilmic(exposureAdjusted);
}
// Apply additional adjustments
const adjusted = this.applyPostToneMappingAdjustments(
toneMapped, { highlights, shadows, contrast, saturation }
);
// Convert to display format
const finalImage = this.convertToDisplayFormat(adjusted);
return {
processedImage: finalImage,
quality: this.toneMappingProcessor.analyzeToneMappingQuality(
hdrImageData, finalImage
)
};
}
applyPostToneMappingAdjustments(imageData, adjustments) {
const { highlights, shadows, contrast, saturation } = adjustments;
const { data, width, height } = imageData;
const result = new Float32Array(data.length);
for (let i = 0; i < data.length; i += 4) {
let r = data[i], g = data[i + 1], b = data[i + 2];
// Calculate luminance
const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
// Apply shadow/highlight adjustments
const shadowWeight = Math.exp(-luminance * 4);
const highlightWeight = Math.exp(-(1 - luminance) * 4);
const shadowFactor = 1 + shadows * shadowWeight;
const highlightFactor = 1 + highlights * highlightWeight;
r *= shadowFactor * highlightFactor;
g *= shadowFactor * highlightFactor;
b *= shadowFactor * highlightFactor;
// Apply contrast
const contrastFactor = Math.tan((contrast + 1) * Math.PI / 4);
r = ((r - 0.5) * contrastFactor) + 0.5;
g = ((g - 0.5) * contrastFactor) + 0.5;
b = ((b - 0.5) * contrastFactor) + 0.5;
// Apply saturation
const newLuminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
r = newLuminance + (r - newLuminance) * saturation;
g = newLuminance + (g - newLuminance) * saturation;
b = newLuminance + (b - newLuminance) * saturation;
result[i] = Math.max(0, Math.min(1, r));
result[i + 1] = Math.max(0, Math.min(1, g));
result[i + 2] = Math.max(0, Math.min(1, b));
result[i + 3] = data[i + 3];
}
return { data: result, width, height };
}
convertToDisplayFormat(imageData) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
for (let i = 0; i < data.length; i++) {
// Apply gamma correction for sRGB
const linearValue = data[i];
const srgbValue = linearValue <= 0.0031308 ?
linearValue * 12.92 :
1.055 * Math.pow(linearValue, 1 / 2.4) - 0.055;
result[i] = Math.round(Math.max(0, Math.min(255, srgbValue * 255)));
}
return new ImageData(result, width, height);
}
}Explore these comprehensive pillar articles related to Tone Mapping
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
Comprehensive guide to complementary colors - opposing hues on the color wheel that create maximum visual contrast, dynamic tension, and powerful design impact through scientific color relationships and perceptual phenomena.
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
Comprehensive guide to triadic color schemes - three colors evenly spaced around the color wheel that create balanced, vibrant, and dynamic visual compositions with mathematical precision and artistic appeal.
Discover more about Tone Mapping and related color theory concepts
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
Comprehensive guide to complementary colors - opposing hues on the color wheel that create maximum visual contrast, dynamic tension, and powerful design impact through scientific color relationships and perceptual phenomena.
A comprehensive guide to monochromatic color schemes - sophisticated single-hue harmonies that create unified visual experiences through systematic variations in saturation, lightness, and tonal relationships, essential for elegant design compositions, brand consistency, and advanced color theory applications requiring cohesive aesthetic unity.
A comprehensive guide to split-complementary color schemes - sophisticated three-color harmonies that combine a base color with two colors adjacent to its complement, creating balanced visual contrast with reduced tension compared to direct complementary pairs, essential for refined design compositions, brand identity systems, and advanced color theory applications.
A comprehensive guide to tetradic color schemes - sophisticated four-color harmonies that use two complementary pairs arranged in rectangular or square formations on the color wheel, providing maximum color variety while maintaining structured relationships essential for dynamic design compositions, brand systems, and advanced color theory applications.
Discover more color terminology and concepts in our comprehensive glossary.
Browse Full Glossary