image.vision\imgir\ir_executor.mbt

84.4% · 65 / 77 points · 164 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/// # IR 执行器 imgir/ir_executor
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// ImgIR 执行器:在 IR 值上按顺序执行管道操作。
  7. 7// IR executor: executes pipeline operations on IR values in order.
  8. 8//
  9. 9// 执行规则:
  10. 10// Image 操作要求输入为 IRValue::VImage
  11. 11// Field 操作要求输入为 IRValue::VScalarField(ToScalarField 除外,它要求 VImage)
  12. 12// ST 操作要求输入为 IRValue::VTImage
  13. 13// 类型不匹配时抛 LoadError::DecodeFailed
  14. 14
  15. 15///|
  16. 16/// 执行单个操作:根据操作类型变换 IR 值。
  17. 17/// 执行单个操作:按操作类型变换 IR 值 Execute a single operation: transform IR value according to operation type.
  18. 18pub fn ir_execute_op(
  19. 19 op : IROp,
  20. 20 input : IRValue,
  21. 21) -> IRValue raise @types.LoadError {
  22. 22● match op {
  23. 23 // === 图像变换 ===
  24. 24○ Resize(w, h) => {
  25. 25○ let img = input.as_image()
  26. 26○ let resized = @pure_util.resize_pure(img, w, h)
  27. 27 VImage(resized)
  28. 28 }
  29. 29● Rotate90 => {
  30. 30● let img = input.as_image()
  31. 31● VImage(@transform.rotate_90(img))
  32. 32 }
  33. 33● FlipH => {
  34. 34● let img = input.as_image()
  35. 35● VImage(@transform.flip_horizontal(img))
  36. 36 }
  37. 37○ FlipV => {
  38. 38○ let img = input.as_image()
  39. 39○ VImage(@transform.flip_vertical(img))
  40. 40 }
  41. 41○ Crop(x, y, w, h) => {
  42. 42○ let img = input.as_image()
  43. 43○ VImage(@transform.crop(img, x, y, w, h))
  44. 44 }
  45. 45● ToGray => {
  46. 46● let img = input.as_image()
  47. 47● VImage(@color.to_grayscale(img))
  48. 48 }
  49. 49 // === 色彩/滤波 ===
  50. 50● Brightness(delta) => {
  51. 51● let img = input.as_image()
  52. 52● VImage(@color.adjust_brightness(img, delta))
  53. 53 }
  54. 54● GaussianBlur(radius, sigma) => {
  55. 55● let img = input.as_image()
  56. 56● VImage(@filter.gaussian_blur(img, radius, sigma))
  57. 57 }
  58. 58○ BoxBlur(radius) => {
  59. 59○ let img = input.as_image()
  60. 60○ VImage(@filter.box_blur(img, radius))
  61. 61 }
  62. 62● Sharpen(amount) => {
  63. 63● let img = input.as_image()
  64. 64● VImage(@filter.sharpen(img, amount))
  65. 65 }
  66. 66 // === 场操作 ===
  67. 67● ToScalarField => {
  68. 68● let img = input.as_image()
  69. 69● VScalarField(@fieldimg.image_to_scalar_field(img))
  70. 70 }
  71. 71● FieldGradient => {
  72. 72● let f = input.as_scalar_field()
  73. 73● VVectorField(@fieldimg.field_gradient(f))
  74. 74 }
  75. 75● FieldLaplacian => {
  76. 76● let f = input.as_scalar_field()
  77. 77● VScalarField(@fieldimg.field_laplacian(f))
  78. 78 }
  79. 79● FieldIntegrate => {
  80. 80● let f = input.as_scalar_field()
  81. 81● VFloat(@fieldimg.field_integrate(f))
  82. 82 }
  83. 83● FieldClamp(lo, hi) => {
  84. 84● let f = input.as_scalar_field()
  85. 85● VScalarField(@fieldimg.field_clamp(f, lo, hi))
  86. 86 }
  87. 87● FieldMask(threshold) => {
  88. 88● let f = input.as_scalar_field()
  89. 89● VScalarField(@fieldimg.field_mask(f, threshold))
  90. 90 }
  91. 91● ScalarFieldToImage => {
  92. 92● let f = input.as_scalar_field()
  93. 93● VImage(@fieldimg.scalar_field_to_image(f))
  94. 94 }
  95. 95 // === 时空操作 ===
  96. 96● STSnapshot(time) => {
  97. 97● let ti = input.as_timage()
  98. 98● VImage(@spatiotemporal.st_snapshot(ti, time))
  99. 99 }
  100. 100● STInterpolate(time) => {
  101. 101● let ti = input.as_timage()
  102. 102● VImage(@spatiotemporal.st_interpolate_at(ti, time))
  103. 103 }
  104. 104 }
  105. 105}
  106. 106
  107. 107///|
  108. 108/// 执行管道:按顺序在输入值上执行所有操作。
  109. 109/// 执行管线 Execute pipeline
  110. 110pub fn ir_execute(
  111. 111 pipeline : IRPipeline,
  112. 112 input : IRValue,
  113. 113) -> IRValue raise @types.LoadError {
  114. 114● let mut current = input
  115. 115● for i in 0..<pipeline.ops.length() {
  116. 116● current = ir_execute_op(pipeline.ops[i], current)
  117. 117 }
  118. 118 current
  119. 119}
  120. 120
  121. 121///|
  122. 122/// 类型检查:验证管道在给定输入类型下是否类型安全。
  123. 123/// 类型检查:验证管线对给定输入类型安全 Type check: verify pipeline is type-safe for the given input type.
  124. 124/// 返回最终输出类型标签,或在不匹配处抛异常。
  125. 125pub fn ir_type_check(
  126. 126 pipeline : IRPipeline,
  127. 127 input_tag : String,
  128. 128) -> String raise @types.LoadError {
  129. 129● let mut current_tag = input_tag
  130. 130● for i in 0..<pipeline.ops.length() {
  131. 131● let op = pipeline.ops[i]
  132. 132● let expected = op.input_tag()
  133. 133 if current_tag != expected {
  134. 134● raise @types.LoadError::DecodeFailed(
  135. 135 "ir_type_check: 操作 " +
  136. 136● op.tag() +
  137. 137 " 期望输入 " +
  138. 138 expected +
  139. 139 ",实际 " +
  140. 140 current_tag,
  141. 141 )
  142. 142 }
  143. 143● current_tag = op.output_tag()
  144. 144 }
  145. 145 current_tag
  146. 146}
  147. 147
  148. 148///|
  149. 149/// 管道描述:生成人类可读的操作序列描述。
  150. 150/// 管线描述:生成可读操作序列 Pipeline description: generate human-readable operation sequence.
  151. 151pub fn ir_describe(pipeline : IRPipeline) -> String {
  152. 152● if pipeline.ops.length() == 0 {
  153. 153● return "empty pipeline"
  154. 154 }
  155. 155 let mut desc = ""
  156. 156● for i in 0..<pipeline.ops.length() {
  157. 157● let op = pipeline.ops[i]
  158. 158 if i > 0 {
  159. 159● desc = desc + " → "
  160. 160 }
  161. 161● desc = desc + op.tag()
  162. 162 }
  163. 163 desc
  164. 164}