image.domain\self_evolve\equiv_check.mbt

83.3% · 25 / 30 points · 67 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 等价检查 self_evolve/equiv_check
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 算子等价性检查 — 恒等算子 + 互逆算子对
  7. 7// Op equivalence: identity ops + inverse pairs
  8. 8
  9. 9///|
  10. 10/// 判断算子是否为恒等(对图像无效果)。
  11. 11/// 检查 Check if an op is identity (no effect on image).
  12. 12pub fn is_identity_op(op : @imgir.IROp) -> Bool {
  13. 13● match op {
  14. 14● @imgir.Brightness(0) => true
  15. 15● @imgir.GaussianBlur(0, _) => true
  16. 16● @imgir.BoxBlur(0) => true
  17. 17● @imgir.Sharpen(0.0F) => true
  18. 18● @imgir.Resize(w, h) => w == -1 && h == -1 // 标记恒等(非实际语义)
  19. 19● _ => false
  20. 20 }
  21. 21}
  22. 22
  23. 23///|
  24. 24/// 判断两个算子是否互逆(连续执行 = 恒等)。
  25. 25/// 检查 Check if two ops are inverse (composition = identity).
  26. 26pub fn check_inverse_pair(op1 : @imgir.IROp, op2 : @imgir.IROp) -> Bool {
  27. 27● match (op1, op2) {
  28. 28● (@imgir.FlipH, @imgir.FlipH) => true
  29. 29● (@imgir.FlipV, @imgir.FlipV) => true
  30. 30● (@imgir.Rotate90, @imgir.Rotate90) => false // 需要四次才恒等
  31. 31○ (@imgir.ToGray, @imgir.ToGray) => false // 幂等但非互逆
  32. 32● _ => false
  33. 33 }
  34. 34}
  35. 35
  36. 36///|
  37. 37/// 判断两个算子是否可合并(同类相邻)。
  38. 38/// 检查 Check if two ops can be merged (same type, adjacent).
  39. 39pub fn can_merge(op1 : @imgir.IROp, op2 : @imgir.IROp) -> Bool {
  40. 40● match (op1, op2) {
  41. 41● (@imgir.Brightness(_), @imgir.Brightness(_)) => true
  42. 42● (@imgir.GaussianBlur(r1, _), @imgir.GaussianBlur(r2, _)) => r1 == r2 // 同半径可合并
  43. 43● (@imgir.BoxBlur(_), @imgir.BoxBlur(_)) => true
  44. 44● _ => false
  45. 45 }
  46. 46}
  47. 47
  48. 48///|
  49. 49/// 合并两个同类算子。
  50. 50/// 合并 Merge two same-type ops into one.
  51. 51pub fn merge_ops(op1 : @imgir.IROp, op2 : @imgir.IROp) -> @imgir.IROp {
  52. 52● match (op1, op2) {
  53. 53● (@imgir.Brightness(a), @imgir.Brightness(b)) => @imgir.Brightness(a + b)
  54. 54 (@imgir.GaussianBlur(r, s1), @imgir.GaussianBlur(_, s2)) =>
  55. 55● @imgir.GaussianBlur(r, s1 + s2) // sigma 简单相加(近似)
  56. 56 (@imgir.BoxBlur(r1), @imgir.BoxBlur(r2)) =>
  57. 57○ @imgir.BoxBlur(if r1 > r2 { r1 } else { r2 }) // 取较大半径
  58. 58○ _ => op1 // 无法合并,返回第一个
  59. 59 }
  60. 60}
  61. 61
  62. 62///|
  63. 63/// 判断 Rotate90 的连续次数是否为恒等。
  64. 64/// 检查 Check if n consecutive Rotate90 = identity (n % 4 == 0).
  65. 65pub fn rotate90_power_is_identity(n : Int32) -> Bool {
  66. 66● n % 4 == 0
  67. 67}