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