image.process\color\tonemap_16f.mbt
88.2% · 120 / 136 points · 349 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 色调映射 16-bit/float 变体 Tone mapping 16-bit/float variants - 3
/// - 4
/// 16位 Reinhard/Gamma 色调映射 tonemap_reinhard_16 / tonemap_gamma_16 - 5
/// 16位/浮点色调映射变体 16-bit/float tone mapping variants - 6
// 色调映射 16-bit/float 变体 - 7
// Gamma + Reinhard,返回 Image16/ImageF - 8
- 9
///| - 10
fn read_float_le_tm(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 write_float_le_tm(data : Array[Byte], offset : Int32, v : Float) -> Unit { - 21●
let bits = v.reinterpret_as_int() - 22●
data[offset] = (bits & 0xFF).to_byte() - 23●
data[offset + 1] = ((bits >> 8) & 0xFF).to_byte() - 24●
data[offset + 2] = ((bits >> 16) & 0xFF).to_byte() - 25●
data[offset + 3] = ((bits >> 24) & 0xFF).to_byte() - 26
} - 27
- 28
///| - 29
fn write_u16_le_tm(data : Array[Byte], offset : Int32, v : Int32) -> Unit { - 30●
data[offset] = (v & 0xFF).to_byte() - 31●
data[offset + 1] = ((v >> 8) & 0xFF).to_byte() - 32
} - 33
- 34
///| - 35
fn log_f_tm(x : Float) -> Float { - 36●
Float::from_double(@math.log2(x.to_double()) * 0.6931471805599453) - 37
} - 38
- 39
// ========== Gamma 色调映射 float ========== - 40
- 41
///| - 42
/// Gamma 色调映射(float 输出)。 - 43
/// 先归一化到 [0,1],再应用 gamma 校正:L_out = L^(1/gamma)。 - 44
/// gamma > 1 提亮暗部,gamma < 1 压暗。 - 45
/// @see gamma_tonemap 基础版本,本函数为 ImageF 变体 - 46
pub fn gamma_tonemap_f( - 47
imgf : @types.ImageF, - 48
gamma : Float, - 49
) -> @types.ImageF raise @types.LoadError { - 50
// 输入验证 - 51●
if imgf.width <= 0 || imgf.height <= 0 { - 52●
raise @types.LoadError::DecodeFailed( - 53
"gamma_tonemap_f: 图像尺寸必须大于0", - 54
) - 55
} - 56
if imgf.channels < 1 || imgf.channels > 4 { - 57●
raise @types.LoadError::DecodeFailed( - 58
"gamma_tonemap_f: 通道数必须在1-4之间", - 59
) - 60
} - 61
let expected_size = imgf.width * imgf.height * imgf.channels * 4 - 62●
if imgf.data.length() != expected_size { - 63●
raise @types.LoadError::DecodeFailed( - 64
"gamma_tonemap_f: 数据大小不匹配,期望 " + - 65●
expected_size.to_string() + - 66
",实际 " + - 67●
imgf.data.length().to_string(), - 68
) - 69
} - 70
if gamma <= 0.0F { - 71●
raise @types.LoadError::DecodeFailed("gamma_tonemap_f: gamma必须大于0") - 72
} - 73
let w = imgf.width - 74
let h = imgf.height - 75
let ch = imgf.channels - 76
let n = w * h - 77◐
let color_ch = if ch >= 3 { 3 } else { ch } - 78
// 找最大值用于归一化 - 79
let mut max_val = 0.0F - 80
for i in 0..<n { - 81●
for c in 0..<color_ch { - 82●
let v = read_float_le_tm(imgf.data, (i * ch + c) * 4) - 83
if v > max_val { - 84●
max_val = v - 85
} - 86
} - 87
} - 88
if max_val <= 0.0F { - 89●
return @types.ImageF::{ - 90
width: w, - 91
height: h, - 92
channels: ch, - 93●
data: Bytes::from_array(Array::make(n * ch * 4, b'\x00')), - 94
} - 95
} - 96
let inv_gamma = 1.0F / gamma - 97●
let out : Array[Byte] = Array::make(n * ch * 4, b'\x00') - 98
for i in 0..<n { - 99●
for c in 0..<color_ch { - 100●
let v = read_float_le_tm(imgf.data, (i * ch + c) * 4) - 101
let normalized = v / max_val - 102
let mapped = if normalized > 0.0F { - 103●
@math.powf(normalized, inv_gamma) - 104
} else { - 105○
0.0F - 106
} - 107●
write_float_le_tm(out, (i * ch + c) * 4, mapped) - 108
} - 109
if ch == 4 { - 110○
write_float_le_tm(out, (i * ch + 3) * 4, 1.0F) - 111
} - 112
} - 113
@types.ImageF::{ - 114
width: w, - 115
height: h, - 116
channels: ch, - 117●
data: Bytes::from_array(out), - 118
} - 119
} - 120
- 121
// ========== Reinhard 色调映射 float ========== - 122
- 123
///| - 124
/// Reinhard 全局色调映射(float 输出)。 - 125
/// key 控制整体亮度(默认 0.18),值越大越亮。 - 126
/// 公式:L_out = L_scaled / (1 + L_scaled),其中 L_scaled = key * L / L_avg。 - 127
/// @see reinhard_tonemap 基础版本,本函数为 ImageF 变体 - 128
pub fn reinhard_tonemap_f( - 129
imgf : @types.ImageF, - 130
key~ : Float = 0.18F, - 131
) -> @types.ImageF raise @types.LoadError { - 132
// 输入验证 - 133●
if imgf.width <= 0 || imgf.height <= 0 { - 134●
raise @types.LoadError::DecodeFailed( - 135
"reinhard_tonemap_f: 图像尺寸必须大于0", - 136
) - 137
} - 138
if imgf.channels < 1 || imgf.channels > 4 { - 139●
raise @types.LoadError::DecodeFailed( - 140
"reinhard_tonemap_f: 通道数必须在1-4之间", - 141
) - 142
} - 143
let expected_size = imgf.width * imgf.height * imgf.channels * 4 - 144●
if imgf.data.length() != expected_size { - 145●
raise @types.LoadError::DecodeFailed( - 146
"reinhard_tonemap_f: 数据大小不匹配,期望 " + - 147●
expected_size.to_string() + - 148
",实际 " + - 149●
imgf.data.length().to_string(), - 150
) - 151
} - 152
if key <= 0.0F { - 153●
raise @types.LoadError::DecodeFailed("reinhard_tonemap_f: key必须大于0") - 154
} - 155
let w = imgf.width - 156
let h = imgf.height - 157
let ch = imgf.channels - 158
let n = w * h - 159◐
let color_ch = if ch >= 3 { 3 } else { ch } - 160
// 计算对数平均亮度 - 161
let mut log_sum = 0.0F - 162
for i in 0..<n { - 163●
let r = read_float_le_tm(imgf.data, i * ch * 4) - 164●
let g = read_float_le_tm(imgf.data, i * ch * 4 + 4) - 165●
let b = read_float_le_tm(imgf.data, i * ch * 4 + 8) - 166
let lum = 0.2126F * r + 0.7152F * g + 0.0722F * b - 167◐
let lum_safe = if lum > 0.0F { lum } else { 0.0001F } - 168●
log_sum += log_f_tm(lum_safe) - 169
} - 170●
let log_avg = @math.expf(log_sum / Float::from_int(n)) - 171
let scale = key / log_avg - 172
// 应用 Reinhard 映射 - 173●
let out : Array[Byte] = Array::make(n * ch * 4, b'\x00') - 174
for i in 0..<n { - 175●
for c in 0..<color_ch { - 176●
let v = read_float_le_tm(imgf.data, (i * ch + c) * 4) - 177
let scaled = v * scale - 178
let mapped = scaled / (1.0F + scaled) - 179●
write_float_le_tm(out, (i * ch + c) * 4, mapped) - 180
} - 181
if ch == 4 { - 182○
write_float_le_tm(out, (i * ch + 3) * 4, 1.0F) - 183
} - 184
} - 185
@types.ImageF::{ - 186
width: w, - 187
height: h, - 188
channels: ch, - 189●
data: Bytes::from_array(out), - 190
} - 191
} - 192
- 193
// ========== Gamma 色调映射 16-bit ========== - 194
- 195
///| - 196
/// Gamma 色调映射(16-bit 输出)。 - 197
/// 输入 ImageF,输出 Image16(0-65535)。 - 198
/// @see gamma_tonemap 基础版本,本函数为 Image16 变体 - 199
pub fn gamma_tonemap_16( - 200
imgf : @types.ImageF, - 201
gamma : Float, - 202
) -> @types.Image16 raise @types.LoadError { - 203
// 输入验证 - 204●
if imgf.width <= 0 || imgf.height <= 0 { - 205●
raise @types.LoadError::DecodeFailed( - 206
"gamma_tonemap_16: 图像尺寸必须大于0", - 207
) - 208
} - 209
if imgf.channels < 1 || imgf.channels > 4 { - 210●
raise @types.LoadError::DecodeFailed( - 211
"gamma_tonemap_16: 通道数必须在1-4之间", - 212
) - 213
} - 214
let expected_size = imgf.width * imgf.height * imgf.channels * 4 - 215●
if imgf.data.length() != expected_size { - 216●
raise @types.LoadError::DecodeFailed( - 217
"gamma_tonemap_16: 数据大小不匹配,期望 " + - 218●
expected_size.to_string() + - 219
",实际 " + - 220●
imgf.data.length().to_string(), - 221
) - 222
} - 223
if gamma <= 0.0F { - 224●
raise @types.LoadError::DecodeFailed("gamma_tonemap_16: gamma必须大于0") - 225
} - 226
let w = imgf.width - 227
let h = imgf.height - 228
let ch = imgf.channels - 229
let n = w * h - 230◐
let color_ch = if ch >= 3 { 3 } else { ch } - 231
// 找最大值用于归一化 - 232
let mut max_val = 0.0F - 233
for i in 0..<n { - 234●
for c in 0..<color_ch { - 235●
let v = read_float_le_tm(imgf.data, (i * ch + c) * 4) - 236
if v > max_val { - 237●
max_val = v - 238
} - 239
} - 240
} - 241
if max_val <= 0.0F { - 242●
return @types.Image16::{ - 243
width: w, - 244
height: h, - 245
channels: ch, - 246●
data: Bytes::from_array(Array::make(n * ch * 2, b'\x00')), - 247
} - 248
} - 249
let inv_gamma = 1.0F / gamma - 250●
let out : Array[Byte] = Array::make(n * ch * 2, b'\x00') - 251
for i in 0..<n { - 252●
for c in 0..<color_ch { - 253●
let v = read_float_le_tm(imgf.data, (i * ch + c) * 4) - 254
let normalized = v / max_val - 255
let mapped = if normalized > 0.0F { - 256●
@math.powf(normalized, inv_gamma) - 257
} else { - 258○
0.0F - 259
} - 260●
let val = (mapped * 65535.0F + 0.5F).to_int() - 261◐
let clamped = if val < 0 { 0 } else if val > 65535 { 65535 } else { val } - 262●
write_u16_le_tm(out, (i * ch + c) * 2, clamped) - 263
} - 264
if ch == 4 { - 265○
write_u16_le_tm(out, (i * ch + 3) * 2, 65535) - 266
} - 267
} - 268
@types.Image16::{ - 269
width: w, - 270
height: h, - 271
channels: ch, - 272●
data: Bytes::from_array(out), - 273
} - 274
} - 275
- 276
// ========== Reinhard 色调映射 16-bit ========== - 277
- 278
///| - 279
/// Reinhard 全局色调映射(16-bit 输出)。 - 280
/// 输入 ImageF,输出 Image16(0-65535)。 - 281
/// @see reinhard_tonemap 基础版本,本函数为 Image16 变体 - 282
pub fn reinhard_tonemap_16( - 283
imgf : @types.ImageF, - 284
key~ : Float = 0.18F, - 285
) -> @types.Image16 raise @types.LoadError { - 286
// 输入验证 - 287●
if imgf.width <= 0 || imgf.height <= 0 { - 288●
raise @types.LoadError::DecodeFailed( - 289
"reinhard_tonemap_16: 图像尺寸必须大于0", - 290
) - 291
} - 292
if imgf.channels < 1 || imgf.channels > 4 { - 293●
raise @types.LoadError::DecodeFailed( - 294
"reinhard_tonemap_16: 通道数必须在1-4之间", - 295
) - 296
} - 297
let expected_size = imgf.width * imgf.height * imgf.channels * 4 - 298●
if imgf.data.length() != expected_size { - 299●
raise @types.LoadError::DecodeFailed( - 300
"reinhard_tonemap_16: 数据大小不匹配,期望 " + - 301●
expected_size.to_string() + - 302
",实际 " + - 303●
imgf.data.length().to_string(), - 304
) - 305
} - 306
if key <= 0.0F { - 307●
raise @types.LoadError::DecodeFailed( - 308
"reinhard_tonemap_16: key必须大于0", - 309
) - 310
} - 311
let w = imgf.width - 312
let h = imgf.height - 313
let ch = imgf.channels - 314
let n = w * h - 315◐
let color_ch = if ch >= 3 { 3 } else { ch } - 316
// 计算对数平均亮度 - 317
let mut log_sum = 0.0F - 318
for i in 0..<n { - 319●
let r = read_float_le_tm(imgf.data, i * ch * 4) - 320●
let g = read_float_le_tm(imgf.data, i * ch * 4 + 4) - 321●
let b = read_float_le_tm(imgf.data, i * ch * 4 + 8) - 322
let lum = 0.2126F * r + 0.7152F * g + 0.0722F * b - 323◐
let lum_safe = if lum > 0.0F { lum } else { 0.0001F } - 324●
log_sum += log_f_tm(lum_safe) - 325
} - 326●
let log_avg = @math.expf(log_sum / Float::from_int(n)) - 327
let scale = key / log_avg - 328
// 应用 Reinhard 映射 - 329●
let out : Array[Byte] = Array::make(n * ch * 2, b'\x00') - 330
for i in 0..<n { - 331●
for c in 0..<color_ch { - 332●
let v = read_float_le_tm(imgf.data, (i * ch + c) * 4) - 333
let scaled = v * scale - 334
let mapped = scaled / (1.0F + scaled) - 335●
let val = (mapped * 65535.0F + 0.5F).to_int() - 336◐
let clamped = if val < 0 { 0 } else if val > 65535 { 65535 } else { val } - 337●
write_u16_le_tm(out, (i * ch + c) * 2, clamped) - 338
} - 339
if ch == 4 { - 340○
write_u16_le_tm(out, (i * ch + 3) * 2, 65535) - 341
} - 342
} - 343
@types.Image16::{ - 344
width: w, - 345
height: h, - 346
channels: ch, - 347●
data: Bytes::from_array(out), - 348
} - 349
}