image.photography\photo_enhance\hdr.mbt
99.3% · 146 / 147 points · 433 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # HDR 高动态范围 photo_enhance/hdr - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// HDR 高动态范围(v11.7.0) - 7
// 多曝光融合 + 色调映射(Reinhard / Gamma / Sigmoid) - 8
- 9
///| - 10
/// HDR 配置 - 11
#derive(Eq, @debug.Debug) - 12
pub(all) struct HdrConfig { - 13
fusion_method : String // "average" / "weighted" / "mertens" - 14
tone_mapping : String // "reinhard" / "gamma" / "sigmoid" - 15
gamma : Double // Gamma 校正值 - 16
reinhard_key : Double // Reinhard 色调映射关键值 - 17
sigmoid_strength : Double // Sigmoid 强度 - 18
} - 19
- 20
///| - 21
pub extend HdrConfig with Eq::{not_equal, equal} - 22
- 23
///| - 24
pub extend HdrConfig with @debug.Debug::{to_repr} - 25
- 26
///| - 27
/// 创建默认 HDR 配置 - 28
pub fn hdr_default_config() -> HdrConfig { - 29●
HdrConfig::{ - 30
fusion_method: "weighted", - 31
tone_mapping: "reinhard", - 32
gamma: 1.0 / 2.2, - 33
reinhard_key: 0.18, - 34
sigmoid_strength: 5.0, - 35
} - 36
} - 37
- 38
///| - 39
/// 多曝光融合(平均法) - 40
/// 将多张不同曝光的图像取平均 - 41
pub fn hdr_fusion_average( - 42
images : Array[@types.Image], - 43
) -> @types.Image raise @types.LoadError { - 44
// 输入验证 - 45●
if images.length() == 0 { - 46●
raise @types.LoadError::DecodeFailed( - 47
"hdr_fusion_average: 图像数组不能为空", - 48
) - 49
} - 50
let w = images[0].width - 51
let h = images[0].height - 52
let channels = images[0].channels - 53
- 54
// 验证第一张图像 - 55
if w <= 0 || h <= 0 { - 56●
raise @types.LoadError::DecodeFailed( - 57
"hdr_fusion_average: 图像尺寸必须大于0", - 58
) - 59
} - 60
if channels < 1 || channels > 4 { - 61●
raise @types.LoadError::DecodeFailed( - 62
"hdr_fusion_average: 通道数必须在1-4之间", - 63
) - 64
} - 65
- 66
// 验证所有图像尺寸一致且数据大小正确 - 67
let expected_size = w * h * channels - 68●
for img_idx in 0..<images.length() { - 69●
let img = images[img_idx] - 70
if img.width != w || img.height != h || img.channels != channels { - 71●
raise @types.LoadError::DecodeFailed( - 72
"hdr_fusion_average: 第 " + - 73●
img_idx.to_string() + - 74
" 张图像尺寸/通道不一致", - 75
) - 76
} - 77●
if img.data.length() != expected_size { - 78●
raise @types.LoadError::DecodeFailed( - 79
"hdr_fusion_average: 第 " + - 80●
img_idx.to_string() + - 81
" 张图像数据大小不匹配", - 82
) - 83
} - 84
} - 85●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 86●
let n = images.length().to_double() - 87
for i in 0..<(w * h) { - 88●
for c in 0..<channels { - 89●
let mut sum = 0.0 - 90
for img in images { - 91●
sum += img.data[i * channels + c].to_double() - 92
} - 93
let avg = sum / n - 94●
out_data[i * channels + c] = avg.min(255.0).max(0.0).to_int().to_byte() - 95
} - 96
} - 97
@types.Image::{ - 98
width: w, - 99
height: h, - 100
channels, - 101●
data: Bytes::from_array(out_data), - 102
} - 103
} - 104
- 105
///| - 106
/// 多曝光融合(加权法) - 107
/// 根据图像亮度计算权重,亮部和暗部权重低,中间调权重高 - 108
pub fn hdr_fusion_weighted( - 109
images : Array[@types.Image], - 110
) -> @types.Image raise @types.LoadError { - 111
// 输入验证 - 112●
if images.length() == 0 { - 113●
raise @types.LoadError::DecodeFailed( - 114
"hdr_fusion_weighted: 图像数组不能为空", - 115
) - 116
} - 117
let w = images[0].width - 118
let h = images[0].height - 119
let channels = images[0].channels - 120
- 121
// 验证第一张图像 - 122
if w <= 0 || h <= 0 { - 123●
raise @types.LoadError::DecodeFailed( - 124
"hdr_fusion_weighted: 图像尺寸必须大于0", - 125
) - 126
} - 127
if channels < 1 || channels > 4 { - 128●
raise @types.LoadError::DecodeFailed( - 129
"hdr_fusion_weighted: 通道数必须在1-4之间", - 130
) - 131
} - 132
- 133
// 验证所有图像尺寸一致且数据大小正确 - 134
let expected_size = w * h * channels - 135●
for img_idx in 0..<images.length() { - 136●
let img = images[img_idx] - 137
if img.width != w || img.height != h || img.channels != channels { - 138●
raise @types.LoadError::DecodeFailed( - 139
"hdr_fusion_weighted: 第 " + - 140●
img_idx.to_string() + - 141
" 张图像尺寸/通道不一致", - 142
) - 143
} - 144●
if img.data.length() != expected_size { - 145●
raise @types.LoadError::DecodeFailed( - 146
"hdr_fusion_weighted: 第 " + - 147●
img_idx.to_string() + - 148
" 张图像数据大小不匹配", - 149
) - 150
} - 151
} - 152●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 153
// 计算每张图像的权重(基于亮度,中间调权重高) - 154
let weights : Array[Array[Double]] = [] - 155
for img in images { - 156●
let img_weight : Array[Double] = [] - 157
for i in 0..<(w * h) { - 158●
let idx = i * channels - 159●
let r = img.data[idx].to_double() - 160●
let g = if channels >= 2 { img.data[idx + 1].to_double() } else { r } - 161●
let b = if channels >= 3 { img.data[idx + 2].to_double() } else { r } - 162
let luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255.0 - 163
// 高斯权重:在 0.5 处最大,向两端递减 - 164●
let weight = @math.exp(-(luminance - 0.5) * (luminance - 0.5) / 0.08) - 165●
img_weight.push(weight) - 166
} - 167●
weights.push(img_weight) - 168
} - 169
// 加权融合 - 170
for i in 0..<(w * h) { - 171●
let mut weight_sum = 0.0 - 172●
for img_idx in 0..<images.length() { - 173●
weight_sum += weights[img_idx][i] - 174
} - 175
for c in 0..<channels { - 176●
if weight_sum > 0.0 { - 177●
let mut sum = 0.0 - 178●
for img_idx in 0..<images.length() { - 179●
let weight = weights[img_idx][i] / weight_sum - 180●
sum += images[img_idx].data[i * channels + c].to_double() * weight - 181
} - 182●
out_data[i * channels + c] = sum.min(255.0).max(0.0).to_int().to_byte() - 183
} else { - 184○
out_data[i * channels + c] = images[0].data[i * channels + c] - 185
} - 186
} - 187
} - 188
@types.Image::{ - 189
width: w, - 190
height: h, - 191
channels, - 192●
data: Bytes::from_array(out_data), - 193
} - 194
} - 195
- 196
///| - 197
/// Reinhard 色调映射 - 198
/// L_ mapped = L / (1 + L) (全局色调映射) - 199
pub fn tone_mapping_reinhard( - 200
img : @types.Image, - 201
key~ : Double = 0.18, - 202
) -> @types.Image raise @types.LoadError { - 203
// 输入验证 - 204●
if img.width <= 0 || img.height <= 0 { - 205●
raise @types.LoadError::DecodeFailed( - 206
"tone_mapping_reinhard: 图像尺寸必须大于0", - 207
) - 208
} - 209
if img.channels < 1 || img.channels > 4 { - 210●
raise @types.LoadError::DecodeFailed( - 211
"tone_mapping_reinhard: 通道数必须在1-4之间", - 212
) - 213
} - 214
let expected_size = img.width * img.height * img.channels - 215●
if img.data.length() != expected_size { - 216●
raise @types.LoadError::DecodeFailed( - 217
"tone_mapping_reinhard: 数据大小不匹配,期望 " + - 218●
expected_size.to_string() + - 219
",实际 " + - 220●
img.data.length().to_string(), - 221
) - 222
} - 223
if key <= 0.0 { - 224●
raise @types.LoadError::DecodeFailed( - 225
"tone_mapping_reinhard: key必须大于0", - 226
) - 227
} - 228
- 229
let k = key - 230
let w = img.width - 231
let h = img.height - 232
let channels = img.channels - 233
let data = img.data - 234
// 计算对数平均亮度 - 235
let mut log_sum = 0.0 - 236
let delta = 0.0001 - 237
for i in 0..<(w * h) { - 238●
let idx = i * channels - 239●
let r = data[idx].to_double() / 255.0 - 240●
let g = if channels >= 2 { data[idx + 1].to_double() / 255.0 } else { r } - 241●
let b = if channels >= 3 { data[idx + 2].to_double() / 255.0 } else { r } - 242
let luminance = 0.299 * r + 0.587 * g + 0.114 * b - 243●
log_sum += @math.ln(luminance + delta) - 244
} - 245●
let log_avg = @math.exp(log_sum / (w * h).to_double()) - 246
// 应用 Reinhard 色调映射 - 247●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 248
for i in 0..<(w * h) { - 249●
let idx = i * channels - 250
for c in 0..<channels { - 251●
let val = data[idx + c].to_double() / 255.0 - 252
// 缩放 - 253
let scaled = val * k / (log_avg + delta) - 254
// Reinhard 映射 - 255
let mapped = scaled / (1.0 + scaled) - 256
out_data[idx + c] = (mapped * 255.0) - 257●
.min(255.0) - 258●
.max(0.0) - 259●
.to_int() - 260●
.to_byte() - 261
} - 262
} - 263
@types.Image::{ - 264
width: w, - 265
height: h, - 266
channels, - 267●
data: Bytes::from_array(out_data), - 268
} - 269
} - 270
- 271
///| - 272
/// Gamma 校正色调映射 - 273
/// 伽马校正 out = in ^ gamma - 274
pub fn tone_mapping_gamma( - 275
img : @types.Image, - 276
gamma~ : Double = 1.0 / 2.2, - 277
) -> @types.Image raise @types.LoadError { - 278
// 输入验证 - 279●
if img.width <= 0 || img.height <= 0 { - 280●
raise @types.LoadError::DecodeFailed( - 281
"tone_mapping_gamma: 图像尺寸必须大于0", - 282
) - 283
} - 284
if img.channels < 1 || img.channels > 4 { - 285●
raise @types.LoadError::DecodeFailed( - 286
"tone_mapping_gamma: 通道数必须在1-4之间", - 287
) - 288
} - 289
let expected_size = img.width * img.height * img.channels - 290●
if img.data.length() != expected_size { - 291●
raise @types.LoadError::DecodeFailed( - 292
"tone_mapping_gamma: 数据大小不匹配,期望 " + - 293●
expected_size.to_string() + - 294
",实际 " + - 295●
img.data.length().to_string(), - 296
) - 297
} - 298
if gamma <= 0.0 { - 299●
raise @types.LoadError::DecodeFailed( - 300
"tone_mapping_gamma: gamma必须大于0", - 301
) - 302
} - 303
- 304
let g = gamma - 305
let w = img.width - 306
let h = img.height - 307
let channels = img.channels - 308
let data = img.data - 309●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 310
for i in 0..<(w * h) { - 311●
let idx = i * channels - 312
for c in 0..<channels { - 313●
let val = data[idx + c].to_double() / 255.0 - 314●
let mapped = @math.pow(val, g) - 315
out_data[idx + c] = (mapped * 255.0) - 316●
.min(255.0) - 317●
.max(0.0) - 318●
.to_int() - 319●
.to_byte() - 320
} - 321
} - 322
@types.Image::{ - 323
width: w, - 324
height: h, - 325
channels, - 326●
data: Bytes::from_array(out_data), - 327
} - 328
} - 329
- 330
///| - 331
/// Sigmoid 色调映射 - 332
/// Sigmoid 调整 out = 1 / (1 + exp(-strength * (in - 0.5))) - 333
pub fn tone_mapping_sigmoid( - 334
img : @types.Image, - 335
strength~ : Double = 5.0, - 336
) -> @types.Image raise @types.LoadError { - 337
// 输入验证 - 338●
if img.width <= 0 || img.height <= 0 { - 339●
raise @types.LoadError::DecodeFailed( - 340
"tone_mapping_sigmoid: 图像尺寸必须大于0", - 341
) - 342
} - 343
if img.channels < 1 || img.channels > 4 { - 344●
raise @types.LoadError::DecodeFailed( - 345
"tone_mapping_sigmoid: 通道数必须在1-4之间", - 346
) - 347
} - 348
let expected_size = img.width * img.height * img.channels - 349●
if img.data.length() != expected_size { - 350●
raise @types.LoadError::DecodeFailed( - 351
"tone_mapping_sigmoid: 数据大小不匹配,期望 " + - 352●
expected_size.to_string() + - 353
",实际 " + - 354●
img.data.length().to_string(), - 355
) - 356
} - 357
if strength <= 0.0 { - 358●
raise @types.LoadError::DecodeFailed( - 359
"tone_mapping_sigmoid: strength必须大于0", - 360
) - 361
} - 362
- 363
let s = strength - 364
let w = img.width - 365
let h = img.height - 366
let channels = img.channels - 367
let data = img.data - 368●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 369
for i in 0..<(w * h) { - 370●
let idx = i * channels - 371
for c in 0..<channels { - 372●
let val = data[idx + c].to_double() / 255.0 - 373●
let mapped = 1.0 / (1.0 + @math.exp(-s * (val - 0.5))) - 374
out_data[idx + c] = (mapped * 255.0) - 375●
.min(255.0) - 376●
.max(0.0) - 377●
.to_int() - 378●
.to_byte() - 379
} - 380
} - 381
@types.Image::{ - 382
width: w, - 383
height: h, - 384
channels, - 385●
data: Bytes::from_array(out_data), - 386
} - 387
} - 388
- 389
///| - 390
/// 完整 HDR 流水线(多曝光融合 + 色调映射) - 391
pub fn hdr_process( - 392
images : Array[@types.Image], - 393
config? : HdrConfig, - 394
) -> @types.Image raise @types.LoadError { - 395
// 输入验证 - 396●
if images.length() == 0 { - 397●
raise @types.LoadError::DecodeFailed( - 398
"hdr_process: 图像数组不能为空", - 399
) - 400
} - 401
- 402●
let cfg = match config { - 403●
Some(c) => c - 404●
None => hdr_default_config() - 405
} - 406
- 407
// 配置验证 - 408
if cfg.gamma <= 0.0 { - 409●
raise @types.LoadError::DecodeFailed("hdr_process: gamma必须大于0") - 410
} - 411
if cfg.reinhard_key <= 0.0 { - 412●
raise @types.LoadError::DecodeFailed( - 413
"hdr_process: reinhard_key必须大于0", - 414
) - 415
} - 416
if cfg.sigmoid_strength <= 0.0 { - 417●
raise @types.LoadError::DecodeFailed( - 418
"hdr_process: sigmoid_strength必须大于0", - 419
) - 420
} - 421
// 1. 多曝光融合 - 422●
let fused = match cfg.fusion_method { - 423●
"average" => hdr_fusion_average(images) - 424●
_ => hdr_fusion_weighted(images) - 425
} - 426
// 2. 色调映射 - 427●
let tonemapped = match cfg.tone_mapping { - 428●
"gamma" => tone_mapping_gamma(fused, gamma=cfg.gamma) - 429●
"sigmoid" => tone_mapping_sigmoid(fused, strength=cfg.sigmoid_strength) - 430●
_ => tone_mapping_reinhard(fused, key=cfg.reinhard_key) - 431
} - 432
tonemapped - 433
}