image.process\color\tonemap.mbt
79.1% · 53 / 67 points · 172 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 色调映射 Tone mapping - 3
/// - 4
/// Reinhard/Gamma 色调映射 tonemap_reinhard / tonemap_gamma - 5
/// Reinhard + Gamma 色调映射 Reinhard + Gamma tone mapping - 6
// 色调映射(纯 MoonBit):Reinhard + Gamma - 7
// 将 HDR float 图像 (ImageF) 映射到 LDR 8-bit 图像 (Image) - 8
- 9
///| - 10
fn read_float_le(data : Bytes, offset : Int32) -> Float { - 11●
let b0 = data[offset].to_int() - 12●
let b1 = data[offset + 1].to_int() - 13●
let b2 = data[offset + 2].to_int() - 14●
let b3 = data[offset + 3].to_int() - 15
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 16●
Float::reinterpret_from_int(bits) - 17
} - 18
- 19
///| - 20
fn log_f(x : Float) -> Float { - 21●
Float::from_double(@math.log2(x.to_double()) * 0.6931471805599453) - 22
} - 23
- 24
///| - 25
/// Reinhard 全局色调映射。 - 26
/// key 控制整体亮度(默认 0.18),值越大越亮。 - 27
/// 公式:L_out = L_scaled / (1 + L_scaled),其中 L_scaled = key * L / L_avg。 - 28
pub fn reinhard_tonemap( - 29
imgf : @types.ImageF, - 30
key~ : Float = 0.18F, - 31
) -> @types.Image raise @types.LoadError { - 32
// 输入验证 - 33●
if imgf.width <= 0 || imgf.height <= 0 { - 34○
raise @types.LoadError::DecodeFailed( - 35
"reinhard_tonemap: 图像尺寸必须大于0", - 36
) - 37
} - 38
if imgf.channels < 1 || imgf.channels > 4 { - 39○
raise @types.LoadError::DecodeFailed( - 40
"reinhard_tonemap: 通道数必须在1-4之间", - 41
) - 42
} - 43
let expected_size = imgf.width * imgf.height * imgf.channels * 4 - 44●
if imgf.data.length() != expected_size { - 45○
raise @types.LoadError::DecodeFailed( - 46
"reinhard_tonemap: 数据大小不匹配,期望 " + - 47○
expected_size.to_string() + - 48
",实际 " + - 49○
imgf.data.length().to_string(), - 50
) - 51
} - 52
if key <= 0.0F { - 53●
raise @types.LoadError::DecodeFailed("reinhard_tonemap: key必须大于0") - 54
} - 55
let w = imgf.width - 56
let h = imgf.height - 57
let ch = imgf.channels - 58
let n = w * h - 59◐
let color_ch = if ch >= 3 { 3 } else { ch } - 60
// 计算对数平均亮度 - 61
let mut log_sum = 0.0F - 62
for i in 0..<n { - 63●
let r = read_float_le(imgf.data, i * ch * 4) - 64●
let g = read_float_le(imgf.data, i * ch * 4 + 4) - 65●
let b = read_float_le(imgf.data, i * ch * 4 + 8) - 66
let lum = 0.2126F * r + 0.7152F * g + 0.0722F * b - 67●
let lum_safe = if lum > 0.0F { lum } else { 0.0001F } - 68●
log_sum += log_f(lum_safe) - 69
} - 70●
let log_avg = @math.expf(log_sum / Float::from_int(n)) - 71
let scale = key / log_avg - 72
// 应用 Reinhard 映射 - 73●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 74
for i in 0..<n { - 75●
for c in 0..<color_ch { - 76●
let v = read_float_le(imgf.data, (i * ch + c) * 4) - 77
let scaled = v * scale - 78
let mapped = scaled / (1.0F + scaled) - 79●
let val = (mapped * 255.0F + 0.5F).to_int().clamp(min=0, max=255) - 80●
out[i * ch + c] = val.to_byte() - 81
} - 82
if ch == 4 { - 83●
out[i * ch + 3] = b'\xFF' - 84
} - 85
} - 86
@types.Image::{ - 87
width: w, - 88
height: h, - 89
channels: ch, - 90●
data: Bytes::from_array(out), - 91
} - 92
} - 93
- 94
///| - 95
/// Gamma 色调映射。 - 96
/// 先归一化到 [0,1],再应用 gamma 校正:L_out = L^(1/gamma)。 - 97
/// gamma > 1 提亮暗部,gamma < 1 压暗。 - 98
pub fn gamma_tonemap( - 99
imgf : @types.ImageF, - 100
gamma : Float, - 101
) -> @types.Image raise @types.LoadError { - 102
// 输入验证 - 103●
if imgf.width <= 0 || imgf.height <= 0 { - 104○
raise @types.LoadError::DecodeFailed( - 105
"gamma_tonemap: 图像尺寸必须大于0", - 106
) - 107
} - 108
if imgf.channels < 1 || imgf.channels > 4 { - 109○
raise @types.LoadError::DecodeFailed( - 110
"gamma_tonemap: 通道数必须在1-4之间", - 111
) - 112
} - 113
let expected_size = imgf.width * imgf.height * imgf.channels * 4 - 114●
if imgf.data.length() != expected_size { - 115○
raise @types.LoadError::DecodeFailed( - 116
"gamma_tonemap: 数据大小不匹配,期望 " + - 117○
expected_size.to_string() + - 118
",实际 " + - 119○
imgf.data.length().to_string(), - 120
) - 121
} - 122
if gamma <= 0.0F { - 123●
raise @types.LoadError::DecodeFailed("gamma_tonemap: gamma必须大于0") - 124
} - 125
let w = imgf.width - 126
let h = imgf.height - 127
let ch = imgf.channels - 128
let n = w * h - 129◐
let color_ch = if ch >= 3 { 3 } else { ch } - 130
// 找最大值用于归一化 - 131
let mut max_val = 0.0F - 132
for i in 0..<n { - 133●
for c in 0..<color_ch { - 134●
let v = read_float_le(imgf.data, (i * ch + c) * 4) - 135
if v > max_val { - 136●
max_val = v - 137
} - 138
} - 139
} - 140
if max_val <= 0.0F { - 141●
return @types.Image::{ - 142
width: w, - 143
height: h, - 144
channels: ch, - 145●
data: Bytes::from_array(Array::make(n * ch, b'\x00')), - 146
} - 147
} - 148
let inv_gamma = 1.0F / gamma - 149●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 150
for i in 0..<n { - 151●
for c in 0..<color_ch { - 152●
let v = read_float_le(imgf.data, (i * ch + c) * 4) - 153
let normalized = v / max_val - 154
let mapped = if normalized > 0.0F { - 155●
@math.powf(normalized, inv_gamma) - 156
} else { - 157●
0.0F - 158
} - 159●
let val = (mapped * 255.0F + 0.5F).to_int().clamp(min=0, max=255) - 160●
out[i * ch + c] = val.to_byte() - 161
} - 162
if ch == 4 { - 163●
out[i * ch + 3] = b'\xFF' - 164
} - 165
} - 166
@types.Image::{ - 167
width: w, - 168
height: h, - 169
channels: ch, - 170●
data: Bytes::from_array(out), - 171
} - 172
}