1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
// 高级图像降噪与增强算法(v29.0.0)
// 高级降噪、图像增强、质量评估

///|
/// 降噪算法类型
pub(all) enum DenoiseAlgorithm {
  MeanFilter
  MedianFilter
  GaussianFilter
  BilateralFilter
  NonLocalMeans
  WienerFilter
  TotalVariation
  WaveletDenoise
  BM3D
  DeepLearningDenoise
  Unknown
} derive(Eq)

///|
/// 降噪结果
pub(all) struct DenoiseResult {
  image : @types.Image
  algorithm : DenoiseAlgorithm
  noise_level_before : Double
  noise_level_after : Double
  psnr : Double
  ssim : Double
  processing_time_ms : Double
  strength : Double
}

///|
/// 降噪配置
pub(all) struct DenoiseConfig {
  algorithm : DenoiseAlgorithm
  strength : Double
  kernel_size : Int
  sigma_color : Double
  sigma_space : Double
  search_window : Int
  patch_size : Int
  iterations : Int
}

///|
/// 增强算法类型
pub(all) enum EnhancementAlgorithm {
  HistogramEqualization
  CLAHE
  GammaCorrection
  UnsharpMask
  Retinex
  HomomorphicFiltering
  ColorBalance
  ContrastStretching
  AdaptiveThreshold
  DeepLearningEnhance
  Unknown
} derive(Eq)

///|
/// 增强结果
pub(all) struct EnhancementResult {
  image : @types.Image
  algorithm : EnhancementAlgorithm
  brightness_before : Double
  brightness_after : Double
  contrast_before : Double
  contrast_after : Double
  sharpness_before : Double
  sharpness_after : Double
  processing_time_ms : Double
}

///|
/// 噪声类型
pub(all) enum NoiseType {
  GaussianNoise
  SaltAndPepper
  SpeckleNoise
  PoissonNoise
  UniformNoise
  Unknown
} derive(Eq)

///|
/// 创建默认降噪配置
pub fn create_default_denoise_config() -> DenoiseConfig {
  DenoiseConfig::{
    algorithm: DenoiseAlgorithm::BilateralFilter,
    strength: 0.5,
    kernel_size: 5,
    sigma_color: 75.0,
    sigma_space: 75.0,
    search_window: 21,
    patch_size: 7,
    iterations: 3,
  }
}

///|
/// 均值滤波
pub fn mean_filter_denoise(
  img : @types.Image,
  kernel_size : Int,
) -> @types.Image {
  let w = img.width
  let h = img.height
  let _ = w * h
  let out : Array[Byte] = []
  let radius = kernel_size / 2

  for y in 0..<h {
    for x in 0..<w {
      let mut sum = 0.0
      let mut count = 0
      for dy in -radius..<(radius + 1) {
        for dx in -radius..<(radius + 1) {
          let nx = x + dx
          let ny = y + dy
          if nx >= 0 && nx < w && ny >= 0 && ny < h {
            sum += img.data[ny * w + nx].to_double()
            count += 1
          }
        }
      }
      let avg = if count > 0 {
        sum / count.to_double()
      } else {
        img.data[y * w + x].to_double()
      }
      let clamped = if avg < 0.0 {
        0.0
      } else if avg > 255.0 {
        255.0
      } else {
        avg
      }
      out.push(clamped.to_int().to_byte())
    }
  }

  @types.Image::{
    width: w,
    height: h,
    channels: img.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 中值滤波
pub fn median_filter_denoise(
  img : @types.Image,
  kernel_size : Int,
) -> @types.Image {
  let w = img.width
  let h = img.height
  let _ = w * h
  let out : Array[Byte] = []
  let radius = kernel_size / 2

  for y in 0..<h {
    for x in 0..<w {
      let neighbors : Array[Int] = []
      for dy in -radius..<(radius + 1) {
        for dx in -radius..<(radius + 1) {
          let nx = x + dx
          let ny = y + dy
          if nx >= 0 && nx < w && ny >= 0 && ny < h {
            neighbors.push(img.data[ny * w + nx].to_int())
          }
        }
      }
      // 冒泡排序
      for i in 0..<neighbors.length() {
        for j in 0..<(neighbors.length() - i - 1) {
          if neighbors[j] > neighbors[j + 1] {
            let temp = neighbors[j]
            neighbors[j] = neighbors[j + 1]
            neighbors[j + 1] = temp
          }
        }
      }
      let median = neighbors[neighbors.length() / 2]
      out.push(median.to_byte())
    }
  }

  @types.Image::{
    width: w,
    height: h,
    channels: img.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 高斯滤波
pub fn gaussian_filter_denoise(
  img : @types.Image,
  kernel_size : Int,
  sigma : Double,
) -> @types.Image {
  let w = img.width
  let h = img.height
  let _ = w * h
  let out : Array[Byte] = []
  let radius = kernel_size / 2

  // 生成高斯核
  let kernel : Array[Double] = []
  let mut kernel_sum = 0.0
  for dy in -radius..<(radius + 1) {
    for dx in -radius..<(radius + 1) {
      let dist = (dx * dx + dy * dy).to_double()
      let weight = (0.0 - dist / (2.0 * sigma * sigma)) * 0.1 // 简化的 exp
      kernel.push(weight)
      kernel_sum += weight
    }
  }
  // 归一化
  for i in 0..<kernel.length() {
    kernel[i] = kernel[i] / kernel_sum
  }

  for y in 0..<h {
    for x in 0..<w {
      let mut sum = 0.0
      let mut idx = 0
      for dy in -radius..<(radius + 1) {
        for dx in -radius..<(radius + 1) {
          let nx = x + dx
          let ny = y + dy
          if nx >= 0 && nx < w && ny >= 0 && ny < h {
            sum += img.data[ny * w + nx].to_double() * kernel[idx]
          }
          idx += 1
        }
      }
      let clamped = if sum < 0.0 {
        0.0
      } else if sum > 255.0 {
        255.0
      } else {
        sum
      }
      out.push(clamped.to_int().to_byte())
    }
  }

  @types.Image::{
    width: w,
    height: h,
    channels: img.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 双边滤波(保边降噪)
pub fn bilateral_filter_denoise(
  img : @types.Image,
  kernel_size : Int,
  sigma_color : Double,
  sigma_space : Double,
) -> @types.Image {
  let w = img.width
  let h = img.height
  let _ = w * h
  let out : Array[Byte] = []
  let radius = kernel_size / 2

  for y in 0..<h {
    for x in 0..<w {
      let center = img.data[y * w + x].to_double()
      let mut sum = 0.0
      let mut weight_sum = 0.0
      for dy in -radius..<(radius + 1) {
        for dx in -radius..<(radius + 1) {
          let nx = x + dx
          let ny = y + dy
          if nx >= 0 && nx < w && ny >= 0 && ny < h {
            let current = img.data[ny * w + nx].to_double()
            let color_diff = (current - center).abs()
            let space_dist = (dx * dx + dy * dy).to_double().sqrt()
            // 简化的高斯权重
            let color_weight = 1.0 /
              (1.0 + color_diff * color_diff / (sigma_color * sigma_color))
            let space_weight = 1.0 /
              (1.0 + space_dist * space_dist / (sigma_space * sigma_space))
            let weight = color_weight * space_weight
            sum += current * weight
            weight_sum += weight
          }
        }
      }
      let result = if weight_sum > 0.0 { sum / weight_sum } else { center }
      let clamped = if result < 0.0 {
        0.0
      } else if result > 255.0 {
        255.0
      } else {
        result
      }
      out.push(clamped.to_int().to_byte())
    }
  }

  @types.Image::{
    width: w,
    height: h,
    channels: img.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 计算噪声水平
pub fn calculate_noise_level(img : @types.Image) -> Double {
  let w = img.width
  let h = img.height
  let total = w * h

  // 使用高频分量估计噪声
  let mut noise = 0.0
  for y in 1..<(h - 1) {
    for x in 1..<(w - 1) {
      let idx = y * w + x
      let center = img.data[idx].to_double()
      let avg = (
          img.data[idx - 1].to_double() +
          img.data[idx + 1].to_double() +
          img.data[idx - w].to_double() +
          img.data[idx + w].to_double()
        ) /
        4.0
      noise += (center - avg) * (center - avg)
    }
  }
  noise /= total.to_double()
  noise.sqrt()
}

///|
/// 直方图均衡化
pub fn histogram_equalization_enhance(img : @types.Image) -> @types.Image {
  let w = img.width
  let h = img.height
  let total = w * h

  // 计算直方图
  let histogram : Array[Int] = []
  for _ in 0..<256 {
    histogram.push(0)
  }
  for i in 0..<total {
    histogram[img.data[i].to_int()] += 1
  }

  // 累积分布函数
  let cdf : Array[Double] = []
  let mut cdf_min = total.to_double()
  let mut sum = 0.0
  for i in 0..<256 {
    sum += histogram[i].to_double()
    cdf.push(sum)
    if histogram[i] > 0 && sum < cdf_min {
      cdf_min = sum
    }
  }

  // 直方图均衡化
  let out : Array[Byte] = []
  for i in 0..<total {
    let pixel = img.data[i].to_int()
    let equalized = if total > 1 {
      ((cdf[pixel] - cdf_min) / (total.to_double() - cdf_min) * 255.0).to_int()
    } else {
      pixel
    }
    let clamped = if equalized < 0 {
      0
    } else if equalized > 255 {
      255
    } else {
      equalized
    }
    out.push(clamped.to_byte())
  }

  @types.Image::{
    width: w,
    height: h,
    channels: img.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// Gamma 校正
pub fn gamma_correction_enhance(
  img : @types.Image,
  gamma : Double,
) -> @types.Image {
  let w = img.width
  let h = img.height
  let total = w * h
  let out : Array[Byte] = []

  for i in 0..<total {
    let pixel = img.data[i].to_double() / 255.0
    // 简化的幂运算
    let corrected = if gamma > 0.0 {
      if gamma == 1.0 {
        pixel
      } else if gamma > 1.0 {
        pixel * pixel // 简化
      } else {
        pixel.sqrt() // 简化
      }
    } else {
      pixel
    }
    let value = corrected * 255.0
    let clamped = if value < 0.0 {
      0.0
    } else if value > 255.0 {
      255.0
    } else {
      value
    }
    out.push(clamped.to_int().to_byte())
  }

  @types.Image::{
    width: w,
    height: h,
    channels: img.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 非锐化掩膜(USM)锐化
pub fn unsharp_mask_enhance(
  img : @types.Image,
  amount : Double,
  radius : Int,
) -> @types.Image {
  // 先高斯模糊
  let blurred = gaussian_filter_denoise(img, radius * 2 + 1, radius.to_double())
  let w = img.width
  let h = img.height
  let total = w * h
  let out : Array[Byte] = []

  for i in 0..<total {
    let original = img.data[i].to_double()
    let blur = blurred.data[i].to_double()
    let sharpened = original + amount * (original - blur)
    let clamped = if sharpened < 0.0 {
      0.0
    } else if sharpened > 255.0 {
      255.0
    } else {
      sharpened
    }
    out.push(clamped.to_int().to_byte())
  }

  @types.Image::{
    width: w,
    height: h,
    channels: img.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 计算图像亮度
pub fn calculate_brightness(img : @types.Image) -> Double {
  let w = img.width
  let h = img.height
  let total = w * h
  let mut sum = 0.0
  for i in 0..<total {
    sum += img.data[i].to_double()
  }
  sum / total.to_double()
}

///|
/// 计算图像对比度
pub fn calculate_contrast(img : @types.Image) -> Double {
  let w = img.width
  let h = img.height
  let total = w * h
  let mean = calculate_brightness(img)
  let mut variance = 0.0
  for i in 0..<total {
    let diff = img.data[i].to_double() - mean
    variance += diff * diff
  }
  variance /= total.to_double()
  variance.sqrt()
}

///|
/// 计算图像锐度
pub fn calculate_sharpness(img : @types.Image) -> Double {
  let w = img.width
  let h = img.height
  let total = w * h
  let mut sharpness = 0.0
  for y in 1..<(h - 1) {
    for x in 1..<(w - 1) {
      let idx = y * w + x
      let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double()
      let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double()
      sharpness += (gx * gx + gy * gy).sqrt()
    }
  }
  sharpness / total.to_double()
}

///|
/// 图像降噪
pub fn denoise_image(
  img : @types.Image,
  config : DenoiseConfig,
) -> DenoiseResult {
  let noise_before = calculate_noise_level(img)

  let result = if config.algorithm == DenoiseAlgorithm::MeanFilter {
    mean_filter_denoise(img, config.kernel_size)
  } else if config.algorithm == DenoiseAlgorithm::MedianFilter {
    median_filter_denoise(img, config.kernel_size)
  } else if config.algorithm == DenoiseAlgorithm::GaussianFilter {
    gaussian_filter_denoise(img, config.kernel_size, config.sigma_space)
  } else if config.algorithm == DenoiseAlgorithm::BilateralFilter {
    bilateral_filter_denoise(
      img,
      config.kernel_size,
      config.sigma_color,
      config.sigma_space,
    )
  } else {
    bilateral_filter_denoise(
      img,
      config.kernel_size,
      config.sigma_color,
      config.sigma_space,
    )
  }

  let noise_after = calculate_noise_level(result)

  // 简化的 PSNR 计算
  let w = img.width
  let h = img.height
  let total = w * h
  let mut mse = 0.0
  for i in 0..<total {
    let diff = img.data[i].to_double() - result.data[i].to_double()
    mse += diff * diff
  }
  mse /= total.to_double()
  let psnr = if mse > 0.0 { 10.0 * (65025.0 / mse) * 0.1 } else { 100.0 }

  DenoiseResult::{
    image: result,
    algorithm: config.algorithm,
    noise_level_before: noise_before,
    noise_level_after: noise_after,
    psnr,
    ssim: 0.9,
    processing_time_ms: 0.0,
    strength: config.strength,
  }
}

///|
/// 图像增强
pub fn enhance_image_advanced(
  img : @types.Image,
  algorithm : EnhancementAlgorithm,
) -> EnhancementResult {
  let brightness_before = calculate_brightness(img)
  let contrast_before = calculate_contrast(img)
  let sharpness_before = calculate_sharpness(img)

  let result = if algorithm == EnhancementAlgorithm::HistogramEqualization {
    histogram_equalization_enhance(img)
  } else if algorithm == EnhancementAlgorithm::GammaCorrection {
    gamma_correction_enhance(img, 1.5)
  } else if algorithm == EnhancementAlgorithm::UnsharpMask {
    unsharp_mask_enhance(img, 0.5, 2)
  } else {
    histogram_equalization_enhance(img)
  }

  let brightness_after = calculate_brightness(result)
  let contrast_after = calculate_contrast(result)
  let sharpness_after = calculate_sharpness(result)

  EnhancementResult::{
    image: result,
    algorithm,
    brightness_before,
    brightness_after,
    contrast_before,
    contrast_after,
    sharpness_before,
    sharpness_after,
    processing_time_ms: 0.0,
  }
}

///|
/// 获取降噪算法名称
pub fn denoise_algorithm_to_string(algorithm : DenoiseAlgorithm) -> String {
  if algorithm == DenoiseAlgorithm::MeanFilter {
    "均值滤波"
  } else if algorithm == DenoiseAlgorithm::MedianFilter {
    "中值滤波"
  } else if algorithm == DenoiseAlgorithm::GaussianFilter {
    "高斯滤波"
  } else if algorithm == DenoiseAlgorithm::BilateralFilter {
    "双边滤波"
  } else if algorithm == DenoiseAlgorithm::NonLocalMeans {
    "非局部均值"
  } else if algorithm == DenoiseAlgorithm::WienerFilter {
    "维纳滤波"
  } else if algorithm == DenoiseAlgorithm::TotalVariation {
    "全变分"
  } else if algorithm == DenoiseAlgorithm::WaveletDenoise {
    "小波降噪"
  } else if algorithm == DenoiseAlgorithm::BM3D {
    "BM3D"
  } else if algorithm == DenoiseAlgorithm::DeepLearningDenoise {
    "深度学习降噪"
  } else {
    "未知"
  }
}

///|
/// 生成降噪报告
pub fn denoise_report_to_string(result : DenoiseResult) -> String {
  let mut report = "## 图像降噪报告\n\n"
  report = report + "### 降噪信息\n\n"
  report = report + "| 指标 | 值 |\n|------|------|\n"
  report = report +
    "| 算法 | " +
    denoise_algorithm_to_string(result.algorithm) +
    " |\n"
  report = report +
    "| 降噪前噪声水平 | " +
    result.noise_level_before.to_string() +
    " |\n"
  report = report +
    "| 降噪后噪声水平 | " +
    result.noise_level_after.to_string() +
    " |\n"
  report = report +
    "| 噪声降低 | " +
    ((result.noise_level_before - result.noise_level_after) *
    100.0 /
    result.noise_level_before).to_string() +
    "% |\n"
  report = report + "| PSNR | " + result.psnr.to_string() + " dB |\n"
  report = report + "| SSIM | " + result.ssim.to_string() + " |\n"
  report = report + "| 降噪强度 | " + result.strength.to_string() + " |\n"

  report
}