image.photography\photo_enhance\computational_photography.mbt
95.2% · 79 / 83 points · 441 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 计算摄影 photo_enhance/computational_photography - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 计算摄影增强(v15.0.0) - 7
// HDR 多曝光融合、色调映射、全景拼接增强、景深模拟、夜景增强 - 8
- 9
///| - 10
/// HDR 曝光类型 - 11
/// `#non_exhaustive` 表示未来可能添加新构造器 - 12
#derive(Eq, @debug.Debug) - 13
#non_exhaustive - 14
pub(all) enum ExposureType { - 15
Underexposed - 16
Normal - 17
Overexposed - 18
Unknown - 19
} - 20
- 21
///| - 22
pub extend ExposureType with @debug.Debug::{to_repr} - 23
- 24
///| - 25
pub extend ExposureType with Eq::{not_equal, equal} - 26
- 27
///| - 28
/// HDR 图像 - 29
#derive(Eq, @debug.Debug) - 30
pub(all) struct HdrImage { - 31
width : Int32 - 32
height : Int32 - 33
channels : Int32 - 34
data : Array[Double] - 35
exposure_values : Array[Double] - 36
num_exposures : Int32 - 37
} - 38
- 39
///| - 40
pub extend HdrImage with Eq::{not_equal, equal} - 41
- 42
///| - 43
pub extend HdrImage with @debug.Debug::{to_repr} - 44
- 45
///| - 46
/// 色调映射算法类型 - 47
/// `#non_exhaustive` 表示未来可能添加新构造器 - 48
#derive(Eq, @debug.Debug) - 49
#non_exhaustive - 50
pub(all) enum ToneMappingType { - 51
Reinhard - 52
Drago - 53
ACES - 54
Linear - 55
Gamma - 56
} - 57
- 58
///| - 59
pub extend ToneMappingType with @debug.Debug::{to_repr} - 60
- 61
///| - 62
pub extend ToneMappingType with Eq::{not_equal, equal} - 63
- 64
///| - 65
/// 色调映射配置 - 66
#derive(Eq, @debug.Debug) - 67
pub(all) struct ToneMappingConfig { - 68
algorithm : ToneMappingType - 69
exposure : Double - 70
gamma : Double - 71
saturation : Double - 72
contrast : Double - 73
} - 74
- 75
///| - 76
pub extend ToneMappingConfig with Eq::{not_equal, equal} - 77
- 78
///| - 79
pub extend ToneMappingConfig with @debug.Debug::{to_repr} - 80
- 81
///| - 82
/// 全景拼接配置 - 83
#derive(Eq, @debug.Debug) - 84
pub(all) struct PanoramaConfig { - 85
overlap_threshold : Double - 86
max_features : Int32 - 87
ransac_iterations : Int32 - 88
blend_width : Int32 - 89
crop_black_borders : Bool - 90
} - 91
- 92
///| - 93
pub extend PanoramaConfig with Eq::{not_equal, equal} - 94
- 95
///| - 96
pub extend PanoramaConfig with @debug.Debug::{to_repr} - 97
- 98
///| - 99
/// 全景拼接结果 - 100
#derive(Eq, @debug.Debug) - 101
pub(all) struct PanoramaResult { - 102
width : Int32 - 103
height : Int32 - 104
num_images : Int32 - 105
inliers : Int32 - 106
confidence : Double - 107
} - 108
- 109
///| - 110
pub extend PanoramaResult with Eq::{not_equal, equal} - 111
- 112
///| - 113
pub extend PanoramaResult with @debug.Debug::{to_repr} - 114
- 115
///| - 116
/// 景深配置 - 117
#derive(Eq, @debug.Debug) - 118
pub(all) struct DepthOfFieldConfig { - 119
focal_point_x : Int32 - 120
focal_point_y : Int32 - 121
aperture : Double - 122
blur_strength : Double - 123
} - 124
- 125
///| - 126
pub extend DepthOfFieldConfig with Eq::{not_equal, equal} - 127
- 128
///| - 129
pub extend DepthOfFieldConfig with @debug.Debug::{to_repr} - 130
- 131
///| - 132
/// 夜景增强配置 - 133
#derive(Eq, @debug.Debug) - 134
pub(all) struct NightEnhanceConfig { - 135
denoise_strength : Double - 136
brightness_boost : Double - 137
contrast_enhance : Double - 138
noise_reduction : Bool - 139
} - 140
- 141
///| - 142
pub extend NightEnhanceConfig with Eq::{not_equal, equal} - 143
- 144
///| - 145
pub extend NightEnhanceConfig with @debug.Debug::{to_repr} - 146
- 147
///| - 148
/// 创建默认 HDR 配置 - 149
pub fn create_default_hdr_config() -> ToneMappingConfig { - 150●
ToneMappingConfig::{ - 151
algorithm: ToneMappingType::Reinhard, - 152
exposure: 0.0, - 153
gamma: 2.2, - 154
saturation: 1.0, - 155
contrast: 1.0, - 156
} - 157
} - 158
- 159
///| - 160
/// 创建默认全景拼接配置 - 161
pub fn create_default_panorama_config() -> PanoramaConfig { - 162●
PanoramaConfig::{ - 163
overlap_threshold: 0.3, - 164
max_features: 500, - 165
ransac_iterations: 1000, - 166
blend_width: 50, - 167
crop_black_borders: true, - 168
} - 169
} - 170
- 171
///| - 172
/// 创建默认景深配置 - 173
pub fn create_default_dof_config( - 174
width : Int32, - 175
height : Int32, - 176
) -> DepthOfFieldConfig { - 177●
DepthOfFieldConfig::{ - 178
focal_point_x: width / 2, - 179
focal_point_y: height / 2, - 180
aperture: 2.8, - 181
blur_strength: 1.0, - 182
} - 183
} - 184
- 185
///| - 186
/// 创建默认夜景增强配置 - 187
pub fn create_default_night_config() -> NightEnhanceConfig { - 188●
NightEnhanceConfig::{ - 189
denoise_strength: 0.5, - 190
brightness_boost: 0.3, - 191
contrast_enhance: 1.2, - 192
noise_reduction: true, - 193
} - 194
} - 195
- 196
///| - 197
/// 检测图像曝光类型 - 198
pub fn detect_exposure_type(img : @types.Image) -> (ExposureType, Double) { - 199●
let w = img.width - 200
let h = img.height - 201
let mut sum = 0.0 - 202
let mut underexposed = 0 - 203
let mut overexposed = 0 - 204
let total = w * h - 205
- 206
for y in 0..<h { - 207●
for x in 0..<w { - 208●
let pixel = img.data[y * w + x].to_double() - 209
sum += pixel - 210
if pixel < 50.0 { - 211●
underexposed += 1 - 212
} - 213
if pixel > 230.0 { - 214●
overexposed += 1 - 215
} - 216
} - 217
} - 218
- 219◐
let avg = if total > 0 { sum / total.to_double() } else { 0.0 } - 220●
let under_ratio = underexposed.to_double() / total.to_double() - 221●
let over_ratio = overexposed.to_double() / total.to_double() - 222
- 223
let exposure_type = if under_ratio > 0.5 || avg < 60.0 { - 224●
ExposureType::Underexposed - 225●
} else if over_ratio > 0.5 || avg > 200.0 { - 226●
ExposureType::Overexposed - 227
} else { - 228●
ExposureType::Normal - 229
} - 230
- 231
let ev = avg / 128.0 - 232
(exposure_type, ev) - 233
} - 234
- 235
///| - 236
/// HDR 多曝光融合(简化版,返回 Double 数组) - 237
pub fn hdr_merge( - 238
images : Array[@types.Image], - 239
exposures : Array[Double], - 240
) -> HdrImage { - 241●
if images.length() == 0 { - 242○
return HdrImage::{ - 243
width: 0, - 244
height: 0, - 245
channels: 1, - 246
data: [], - 247
exposure_values: [], - 248
num_exposures: 0, - 249
} - 250
} - 251
- 252
let first = images[0] - 253
let w = first.width - 254
let h = first.height - 255
let hdr_data : Array[Double] = [] - 256
- 257
for y in 0..<h { - 258●
for x in 0..<w { - 259●
let mut sum = 0.0 - 260
let mut weight_sum = 0.0 - 261
- 262●
for i in 0..<images.length() { - 263●
let img = images[i] - 264
if x < img.width && y < img.height { - 265●
let pixel = img.data[y * img.width + x].to_double() / 255.0 - 266
let weight = if pixel < 0.5 { - 267●
pixel * 2.0 - 268
} else { - 269●
(1.0 - pixel) * 2.0 - 270
} - 271
sum += pixel * weight - 272
weight_sum += weight - 273
} - 274
} - 275
- 276●
let hdr_value = if weight_sum > 0 { sum / weight_sum } else { 0.5 } - 277●
hdr_data.push(hdr_value) - 278
} - 279
} - 280
- 281
HdrImage::{ - 282
width: w, - 283
height: h, - 284
channels: first.channels, - 285
data: hdr_data, - 286
exposure_values: exposures, - 287●
num_exposures: images.length(), - 288
} - 289
} - 290
- 291
///| - 292
/// 色调映射(简化版,返回处理后的 Image) - 293
pub fn tone_map(hdr : HdrImage, config : ToneMappingConfig) -> @types.Image { - 294●
ignore(config) - 295
let w = hdr.width - 296
let h = hdr.height - 297●
let out : Array[Byte] = Array::make(w * h, b'\x00') - 298
- 299
for y in 0..<h { - 300●
for x in 0..<w { - 301●
let idx = y * w + x - 302
let pixel = hdr.data[idx] - 303
let l = pixel * 0.18 - 304
let mapped = l / (1.0 + l) - 305●
let value = (mapped * 255.0).to_int() - 306◐
out[idx] = if value > 127 { b'\xFF' } else { b'\x00' } - 307
} - 308
} - 309
- 310
@types.Image::{ - 311
width: w, - 312
height: h, - 313
channels: hdr.channels, - 314●
data: Bytes::from_array(out), - 315
} - 316
} - 317
- 318
///| - 319
/// 全景拼接(简化版) - 320
pub fn stitch_panorama( - 321
images : Array[@types.Image], - 322
config : PanoramaConfig, - 323
) -> PanoramaResult { - 324●
ignore(config) - 325●
if images.length() == 0 { - 326○
return PanoramaResult::{ - 327
width: 0, - 328
height: 0, - 329
num_images: 0, - 330
inliers: 0, - 331
confidence: 0.0, - 332
} - 333
} - 334
- 335
let first = images[0] - 336●
let total_width = first.width * images.length() - 337
let height = first.height - 338
- 339
PanoramaResult::{ - 340
width: total_width, - 341
height, - 342●
num_images: images.length(), - 343
inliers: total_width * height, - 344
confidence: 0.7, - 345
} - 346
} - 347
- 348
///| - 349
/// 景深模拟(简化版) - 350
pub fn apply_depth_of_field( - 351
img : @types.Image, - 352
config : DepthOfFieldConfig, - 353
) -> @types.Image { - 354●
let w = img.width - 355
let h = img.height - 356●
let out : Array[Byte] = Array::make(w * h, b'\x00') - 357●
let max_dist = ((w / 2).to_double() * (w / 2).to_double() + - 358●
(h / 2).to_double() * (h / 2).to_double()).sqrt() - 359
- 360
for y in 0..<h { - 361●
for x in 0..<w { - 362●
let idx = y * w + x - 363●
let dx = (x - config.focal_point_x).to_double() - 364●
let dy = (y - config.focal_point_y).to_double() - 365●
let dist = (dx * dx + dy * dy).sqrt() - 366
let blur_factor = dist / max_dist * config.blur_strength - 367●
let pixel = img.data[idx].to_double() - 368
let blurred = pixel * (1.0 - blur_factor * 0.3) - 369●
let value = blurred.to_int() - 370●
out[idx] = if value > 127 { b'\xFF' } else { b'\x00' } - 371
} - 372
} - 373
- 374
@types.Image::{ - 375
width: w, - 376
height: h, - 377
channels: img.channels, - 378●
data: Bytes::from_array(out), - 379
} - 380
} - 381
- 382
///| - 383
/// 夜景增强(简化版) - 384
pub fn enhance_night( - 385
img : @types.Image, - 386
config : NightEnhanceConfig, - 387
) -> @types.Image { - 388●
let w = img.width - 389
let h = img.height - 390●
let out : Array[Byte] = Array::make(w * h, b'\x00') - 391
- 392
for y in 0..<h { - 393●
for x in 0..<w { - 394●
let idx = y * w + x - 395●
let pixel = img.data[idx].to_double() / 255.0 - 396
let mut enhanced = pixel + config.brightness_boost - 397
enhanced = (enhanced - 0.5) * config.contrast_enhance + 0.5 - 398●
let value = (enhanced * 255.0).to_int() - 399●
out[idx] = if value > 127 { b'\xFF' } else { b'\x00' } - 400
} - 401
} - 402
- 403
@types.Image::{ - 404
width: w, - 405
height: h, - 406
channels: img.channels, - 407●
data: Bytes::from_array(out), - 408
} - 409
} - 410
- 411
///| - 412
/// 生成计算摄影报告 - 413
pub fn computational_photography_report( - 414
hdr : HdrImage, - 415
panorama : PanoramaResult, - 416
) -> String { - 417●
let mut report = "## 计算摄影处理报告\n\n" - 418
report = report + "### HDR 信息\n\n" - 419
report = report + "| 指标 | 值 |\n|------|------|\n" - 420
report = report + - 421
"| 尺寸 | " + - 422●
hdr.width.to_string() + - 423
"x" + - 424●
hdr.height.to_string() + - 425
" |\n" - 426●
report = report + "| 曝光数 | " + hdr.num_exposures.to_string() + " |\n\n" - 427
report = report + "### 全景拼接信息\n\n" - 428
report = report + "| 指标 | 值 |\n|------|------|\n" - 429
report = report + - 430
"| 拼接尺寸 | " + - 431●
panorama.width.to_string() + - 432
"x" + - 433●
panorama.height.to_string() + - 434
" |\n" - 435●
report = report + "| 图像数 | " + panorama.num_images.to_string() + " |\n" - 436
report = report + - 437
"| 置信度 | " + - 438●
(panorama.confidence * 100.0).to_string() + - 439
"% |\n" - 440
report - 441
}