image.photography\photo_enhance\photo_enhancement.mbt
93.4% · 155 / 166 points · 409 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/photo_enhancement - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 计算摄影增强(v12.15.0) - 7
// 低光增强、去运动模糊、色彩增强、自适应直方图均衡化 - 8
- 9
///| - 10
/// 低光增强配置 - 11
#derive(Eq, @debug.Debug) - 12
pub(all) struct LowLightConfig { - 13
method_type : String // "gamma" / "log" / "retinex" - 14
gamma : Double // Gamma 校正参数 - 15
strength : Double // 增强强度 - 16
} - 17
- 18
///| - 19
pub extend LowLightConfig with Eq::{not_equal, equal} - 20
- 21
///| - 22
pub extend LowLightConfig with @debug.Debug::{to_repr} - 23
- 24
///| - 25
/// 创建默认低光增强配置 - 26
pub fn low_light_default_config() -> LowLightConfig { - 27●
LowLightConfig::{ method_type: "gamma", gamma: 0.5, strength: 1.0, } - 28
} - 29
- 30
///| - 31
/// Gamma 校正低光增强 - 32
fn gamma_enhance(img : @types.Image, gamma : Double) -> @types.Image { - 33●
let w = img.width - 34
let h = img.height - 35
let channels = img.channels - 36●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 37
// 预计算 Gamma 查找表 - 38
let lut : Array[Byte] = [] - 39
for i in 0..<256 { - 40●
let normalized = i.to_double() / 255.0 - 41●
let enhanced = @math.pow(normalized, gamma) - 42●
let val = (enhanced * 255.0).to_int().min(255).max(0) - 43●
lut.push(val.to_byte()) - 44
} - 45
for i in 0..<(w * h * channels) { - 46●
out_data[i] = lut[img.data[i].to_int()] - 47
} - 48
@types.Image::{ - 49
width: w, - 50
height: h, - 51
channels, - 52●
data: Bytes::from_array(out_data), - 53
} - 54
} - 55
- 56
///| - 57
/// 对数变换低光增强 - 58
fn log_enhance(img : @types.Image, strength : Double) -> @types.Image { - 59●
let w = img.width - 60
let h = img.height - 61
let channels = img.channels - 62●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 63●
let c = 255.0 / @math.ln(1.0 + 255.0) - 64
for i in 0..<(w * h * channels) { - 65●
let val = img.data[i].to_double() - 66●
let enhanced = c * @math.ln(1.0 + val) * strength - 67●
out_data[i] = enhanced.min(255.0).max(0.0).to_int().to_byte() - 68
} - 69
@types.Image::{ - 70
width: w, - 71
height: h, - 72
channels, - 73●
data: Bytes::from_array(out_data), - 74
} - 75
} - 76
- 77
///| - 78
/// 简化版 Retinex 低光增强(单尺度 Retinex) - 79
fn retinex_enhance(img : @types.Image, sigma~ : Double = 50.0) -> @types.Image { - 80●
let _ = sigma - 81
let w = img.width - 82
let h = img.height - 83
let channels = img.channels - 84
// 简化版:使用高斯模糊作为光照估计 - 85
// 这里用简单的均值模糊代替(避免依赖外部高斯模糊) - 86
let kernel_size = 15 - 87
let half = kernel_size / 2 - 88●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 89
for y in 0..<h { - 90●
for x in 0..<w { - 91●
let idx = (y * w + x) * channels - 92
for c in 0..<channels { - 93
// 计算局部均值(光照估计) - 94●
let mut sum = 0.0 - 95
let mut count = 0 - 96
for ky in -half..<=half { - 97●
for kx in -half..<=half { - 98●
let ny = y + ky - 99
let nx = x + kx - 100
if ny >= 0 && ny < h && nx >= 0 && nx < w { - 101●
let nidx = (ny * w + nx) * channels - 102●
sum += img.data[nidx + c].to_double() - 103
count += 1 - 104
} - 105
} - 106
} - 107◐
let illumination = if count > 0 { sum / count.to_double() } else { 0.0 } - 108
// Retinex: R = log(I) - log(L) = log(I/L) - 109●
let original = img.data[idx + c].to_double() + 1.0 - 110
let light = illumination + 1.0 - 111
let ratio = original / light - 112●
let reflectance = @math.ln(ratio) - 113
// 映射回 0-255 - 114
let enhanced = (reflectance + 3.0) / 6.0 * 255.0 - 115●
out_data[idx + c] = enhanced.min(255.0).max(0.0).to_int().to_byte() - 116
} - 117
} - 118
} - 119
@types.Image::{ - 120
width: w, - 121
height: h, - 122
channels, - 123●
data: Bytes::from_array(out_data), - 124
} - 125
} - 126
- 127
///| - 128
/// 低光增强(统一接口) - 129
pub fn low_light_enhance( - 130
img : @types.Image, - 131
config? : LowLightConfig, - 132
) -> @types.Image { - 133●
let cfg = match config { - 134●
Some(v) => v - 135●
None => low_light_default_config() - 136
} - 137●
match cfg.method_type { - 138●
"gamma" => gamma_enhance(img, cfg.gamma) - 139●
"log" => log_enhance(img, cfg.strength) - 140●
"retinex" => retinex_enhance(img, sigma=50.0) - 141○
_ => gamma_enhance(img, cfg.gamma) - 142
} - 143
} - 144
- 145
///| - 146
/// 去运动模糊(简化版维纳滤波) - 147
pub fn deblur_motion( - 148
img : @types.Image, - 149
kernel_size~ : Int32 = 15, - 150
angle~ : Double = 0.0, - 151
) -> @types.Image { - 152●
let ks = kernel_size - 153
let _ = angle - 154
let w = img.width - 155
let h = img.height - 156
let channels = img.channels - 157
// 简化版:使用非锐化掩模(Unsharp Mask)作为去模糊近似 - 158
let amount = 1.5 - 159
let threshold = 0 - 160●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 161
let blur_radius = ks / 2 - 162
for y in 0..<h { - 163●
for x in 0..<w { - 164●
let idx = (y * w + x) * channels - 165
for c in 0..<channels { - 166
// 计算局部均值(模糊版本) - 167●
let mut sum = 0.0 - 168
let mut count = 0 - 169
for ky in -blur_radius..<=blur_radius { - 170●
for kx in -blur_radius..<=blur_radius { - 171●
let ny = y + ky - 172
let nx = x + kx - 173
if ny >= 0 && ny < h && nx >= 0 && nx < w { - 174●
let nidx = (ny * w + nx) * channels - 175●
sum += img.data[nidx + c].to_double() - 176
count += 1 - 177
} - 178
} - 179
} - 180◐
let blurred = if count > 0 { sum / count.to_double() } else { 0.0 } - 181●
let original = img.data[idx + c].to_double() - 182
// 非锐化掩模:sharpened = original + amount * (original - blurred) - 183
let diff = original - blurred - 184●
let sharpened = if diff.abs() > threshold.to_double() { - 185●
original + amount * diff - 186
} else { - 187●
original - 188
} - 189●
out_data[idx + c] = sharpened.min(255.0).max(0.0).to_int().to_byte() - 190
} - 191
} - 192
} - 193
@types.Image::{ - 194
width: w, - 195
height: h, - 196
channels, - 197●
data: Bytes::from_array(out_data), - 198
} - 199
} - 200
- 201
///| - 202
/// 色彩增强(饱和度和对比度增强) - 203
pub fn color_enhance( - 204
img : @types.Image, - 205
saturation~ : Double = 1.3, - 206
contrast~ : Double = 1.1, - 207
) -> @types.Image { - 208●
let sat = saturation - 209
let con = contrast - 210
let w = img.width - 211
let h = img.height - 212
let channels = img.channels - 213
if channels < 3 { - 214○
return img - 215
} - 216●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 217
for y in 0..<h { - 218●
for x in 0..<w { - 219●
let idx = (y * w + x) * channels - 220●
let r = img.data[idx].to_double() - 221●
let g = img.data[idx + 1].to_double() - 222●
let b = img.data[idx + 2].to_double() - 223
// 计算灰度值 - 224
let gray = 0.299 * r + 0.587 * g + 0.114 * b - 225
// 饱和度增强 - 226
let new_r = gray + (r - gray) * sat - 227
let new_g = gray + (g - gray) * sat - 228
let new_b = gray + (b - gray) * sat - 229
// 对比度增强 - 230
let final_r = (new_r - 128.0) * con + 128.0 - 231
let final_g = (new_g - 128.0) * con + 128.0 - 232
let final_b = (new_b - 128.0) * con + 128.0 - 233●
out_data[idx] = final_r.min(255.0).max(0.0).to_int().to_byte() - 234●
out_data[idx + 1] = final_g.min(255.0).max(0.0).to_int().to_byte() - 235●
out_data[idx + 2] = final_b.min(255.0).max(0.0).to_int().to_byte() - 236
// 复制其他通道 - 237
for c in 3..<channels { - 238○
out_data[idx + c] = img.data[idx + c] - 239
} - 240
} - 241
} - 242
@types.Image::{ - 243
width: w, - 244
height: h, - 245
channels, - 246●
data: Bytes::from_array(out_data), - 247
} - 248
} - 249
- 250
///| - 251
/// 自适应直方图均衡化(CLAHE 简化版) - 252
pub fn clahe_enhance( - 253
img : @types.Image, - 254
clip_limit~ : Double = 2.0, - 255
tile_size~ : Int32 = 8, - 256
) -> @types.Image { - 257●
let clip = clip_limit - 258
let _ = tile_size - 259
let w = img.width - 260
let h = img.height - 261
let channels = img.channels - 262
// 简化版:全局直方图均衡化 - 263●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 264
for c in 0..<channels { - 265
// 计算直方图 - 266●
let histogram : Array[Int32] = [] - 267
for _ in 0..<256 { - 268●
histogram.push(0) - 269
} - 270
for y in 0..<h { - 271●
for x in 0..<w { - 272●
let idx = (y * w + x) * channels - 273●
histogram[img.data[idx + c].to_int()] += 1 - 274
} - 275
} - 276
// 裁剪直方图 - 277●
let total_pixels = (w * h).to_double() - 278●
let max_count = (clip * total_pixels / 256.0).to_int() - 279
let mut excess = 0 - 280
for i in 0..<256 { - 281●
if histogram[i] > max_count { - 282●
excess += histogram[i] - max_count - 283
histogram[i] = max_count - 284
} - 285
} - 286
// 重新分配超额像素 - 287
let redistribute = excess / 256 - 288
for i in 0..<256 { - 289●
histogram[i] += redistribute - 290
} - 291
// 计算累积分布函数(CDF) - 292
let cdf : Array[Int32] = [] - 293
let mut cumsum = 0 - 294
for i in 0..<256 { - 295●
cumsum += histogram[i] - 296●
cdf.push(cumsum) - 297
} - 298
// 查找表 - 299
let lut : Array[Byte] = [] - 300
let cdf_min = cdf[0] - 301●
let denominator = (w * h - cdf_min).max(1) - 302
for i in 0..<256 { - 303●
let val = ((cdf[i] - cdf_min).to_double() / - 304●
denominator.to_double() * - 305
255.0) - 306●
.to_int() - 307●
.min(255) - 308●
.max(0) - 309●
lut.push(val.to_byte()) - 310
} - 311
// 应用查找表 - 312
for y in 0..<h { - 313●
for x in 0..<w { - 314●
let idx = (y * w + x) * channels - 315●
out_data[idx + c] = lut[img.data[idx + c].to_int()] - 316
} - 317
} - 318
} - 319
@types.Image::{ - 320
width: w, - 321
height: h, - 322
channels, - 323●
data: Bytes::from_array(out_data), - 324
} - 325
} - 326
- 327
///| - 328
/// 白平衡校正(灰度世界算法) - 329
pub fn white_balance(img : @types.Image) -> @types.Image { - 330●
let w = img.width - 331
let h = img.height - 332
let channels = img.channels - 333
if channels < 3 { - 334○
return img - 335
} - 336
// 计算各通道平均值 - 337
let mut sum_r = 0.0 - 338
let mut sum_g = 0.0 - 339
let mut sum_b = 0.0 - 340●
let total = (w * h).to_double() - 341
for y in 0..<h { - 342●
for x in 0..<w { - 343●
let idx = (y * w + x) * channels - 344●
sum_r += img.data[idx].to_double() - 345●
sum_g += img.data[idx + 1].to_double() - 346●
sum_b += img.data[idx + 2].to_double() - 347
} - 348
} - 349
let avg_r = sum_r / total - 350
let avg_g = sum_g / total - 351
let avg_b = sum_b / total - 352
let avg_gray = (avg_r + avg_g + avg_b) / 3.0 - 353
// 计算增益 - 354●
let gain_r = avg_gray / avg_r.max(1.0) - 355●
let gain_g = avg_gray / avg_g.max(1.0) - 356●
let gain_b = avg_gray / avg_b.max(1.0) - 357
// 应用增益 - 358●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 359
for y in 0..<h { - 360●
for x in 0..<w { - 361●
let idx = (y * w + x) * channels - 362●
out_data[idx] = (img.data[idx].to_double() * gain_r) - 363●
.min(255.0) - 364●
.max(0.0) - 365●
.to_int() - 366●
.to_byte() - 367●
out_data[idx + 1] = (img.data[idx + 1].to_double() * gain_g) - 368●
.min(255.0) - 369●
.max(0.0) - 370●
.to_int() - 371●
.to_byte() - 372●
out_data[idx + 2] = (img.data[idx + 2].to_double() * gain_b) - 373●
.min(255.0) - 374●
.max(0.0) - 375●
.to_int() - 376●
.to_byte() - 377
for c in 3..<channels { - 378○
out_data[idx + c] = img.data[idx + c] - 379
} - 380
} - 381
} - 382
@types.Image::{ - 383
width: w, - 384
height: h, - 385
channels, - 386●
data: Bytes::from_array(out_data), - 387
} - 388
} - 389
- 390
///| - 391
/// 批量计算摄影增强 - 392
pub fn batch_photo_enhance( - 393
images : Array[@types.Image], - 394
method_type : String, - 395
) -> Array[@types.Image] { - 396●
let result : Array[@types.Image] = [] - 397
for img in images { - 398●
let enhanced = match method_type { - 399●
"low_light" => low_light_enhance(img) - 400○
"deblur" => deblur_motion(img, kernel_size=15, angle=0.0) - 401○
"color" => color_enhance(img, saturation=1.3, contrast=1.1) - 402●
"clahe" => clahe_enhance(img, clip_limit=2.0, tile_size=8) - 403○
"white_balance" => white_balance(img) - 404○
_ => img - 405
} - 406●
result.push(enhanced) - 407
} - 408
result - 409
}