image.pro\recipe\plan.mbt
96.5% · 109 / 113 points · 207 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// Recipe 的计划层(S25):只解析容器头,不解像素,给出"会走哪几步、每步出多大"。 - 3
/// - 4
/// 与 `run_recipe` 的分工:`run_recipe` 真解码真编码,回执里的 `steps` 是**事后记录** - 5
/// (带真实字节数);`plan_recipe` 只读文件头(`@lib.get_image_info`),回执里是**事前计划** - 6
/// (尺寸是推算值、字节数未知),毫秒级返回,供调用方先看代价再决定跑不跑。 - 7
/// - 8
/// 刻意不做的两件事: - 9
/// 1. 不估输出字节数。编码体积取决于内容,`quality=N` 对一张纯色图和一张照片不是一个量级, - 10
/// 给个拍脑袋的数字比不给更坏——需要真字节就走 `--preview` 或正常执行。 - 11
/// 2. 不判 `max_bytes` 是否可满足。字节轴只能执行期反解,计划阶段如实标 - 12
/// `budget_axis:"bytes"` + `solve:"runtime"`,不假装能给结论。 - 13
- 14
///| - 15
/// 计划里的一步:名称与真实执行时的 `StepRec.name` 同源,尺寸是推算值。 - 16
pub(all) struct PlanStep { - 17
name : String - 18
width : Int32 - 19
height : Int32 - 20
note : String - 21
} - 22
- 23
///| - 24
/// 一份 Recipe 计划。`peak_frame_bytes` 是解码后单帧的原始字节数(w×h×channels), - 25
/// 不是编码体积——它回答的是"这一趟要吃掉多少内存"。 - 26
pub(all) struct RecipePlan { - 27
recipe : String - 28
src_format : String - 29
src_width : Int32 - 30
src_height : Int32 - 31
src_channels : Int32 - 32
out_width : Int32 - 33
out_height : Int32 - 34
steps : Array[PlanStep] - 35
decode_pixels : Int64 - 36
peak_frame_bytes : Int64 - 37
budget_axis : String - 38
budget_bytes : Int32 - 39
} - 40
- 41
///| - 42
fn pl_step(name : String, w : Int32, h : Int32, note : String) -> PlanStep { - 43●
{ name, width: w, height: h, note, } - 44
} - 45
- 46
///| - 47
fn pl_fit(w : Int32, h : Int32, longest : Int32) -> (Int32, Int32) { - 48●
if longest <= 0 { - 49●
return (w, h) - 50
} - 51●
let long = if w >= h { w } else { h } - 52
if longest >= long { - 53●
return (w, h) - 54
} - 55●
@transform.thumbnail_dims(w, h, longest) - 56
} - 57
- 58
///| - 59
/// 按 `name` 推演步骤。`thumb-batch` 按单张 `thumb` 的计划走(批量只是外层循环)。 - 60
pub fn plan_recipe( - 61
name : String, - 62
data : Bytes, - 63
opts : RecipeOpts, - 64
) -> RecipePlan raise @types.ImageError { - 65●
let info = @lib.get_image_info(data) catch { - 66○
e => raise @types.image_error_from_load(e) - 67
} - 68
let w = info.width - 69
let h = info.height - 70◐
let ch = if info.channels <= 0 { 3 } else { info.channels } - 71●
let px = w.to_int64() * h.to_int64() - 72●
let frame = px * ch.to_int64() - 73●
let axis = if opts.max_bytes > 0 { "bytes" } else { "" } - 74
let steps : Array[PlanStep] = [] - 75
let mut ow = w - 76
let mut oh = h - 77●
steps.push(pl_step("decode", w, h, info.format_name)) - 78●
match name { - 79●
"sanitize" => { - 80
let policy = opts.policy - 81
if policy != "metadata" && policy != "standard" && policy != "strict" { - 82●
raise @types.ImageError::Process( - 83
@types.ErrorCode::InvalidArgument, - 84
"policy 只能是 metadata / standard / strict(收到 \{policy})", - 85
) - 86
} - 87●
let nreg = opts.regions.length() - 88●
steps.push( - 89●
pl_step("policy", -1, -1, policy + " regions=" + nreg.to_string()), - 90
) - 91●
steps.push( - 92●
pl_step("encode", ow, oh, "quality=" + opts.quality.to_string()), - 93
) - 94
} - 95●
"thumb" | "thumb-batch" => { - 96●
let (tw, th) = pl_fit(w, h, opts.longest) - 97
if tw != w || th != h { - 98●
steps.push( - 99●
pl_step("resize", tw, th, "longest=" + opts.longest.to_string()), - 100
) - 101
} - 102
ow = tw - 103
oh = th - 104●
let mut note = "quality=" + opts.quality.to_string() - 105
if opts.max_bytes > 0 { - 106●
note = note + - 107
" budget=" + - 108●
opts.max_bytes.to_string() + - 109
" (可能追加 fits-budget 收缩)" - 110
} - 111●
steps.push(pl_step("encode", ow, oh, note)) - 112
} - 113●
"avatar" => { - 114◐
let side = if w < h { w } else { h } - 115
if side != w || side != h { - 116●
steps.push(pl_step("crop", side, side, "center")) - 117
} - 118●
let target = if opts.longest <= 0 { 256 } else { opts.longest } - 119
if target != side { - 120●
steps.push(pl_step("resize", target, target, "")) - 121
} - 122
ow = target - 123
oh = target - 124●
steps.push( - 125●
pl_step("encode", ow, oh, "quality=" + opts.quality.to_string()), - 126
) - 127
} - 128●
"code-intake" => { - 129
// 扫码不改像素,也就不吃 longest:出图恒为原尺寸。 - 130●
steps.push(pl_step("scan", w, h, "barcode/qrcode")) - 131●
steps.push( - 132●
pl_step("encode", ow, oh, "quality=" + opts.quality.to_string()), - 133
) - 134
} - 135●
"doc-scan" => { - 136●
steps.push(pl_step("grayscale", w, h, "")) - 137●
steps.push(pl_step("flatten", w, h, "clahe tile=8 clip=2.0")) - 138●
steps.push(pl_step("binarize", w, h, "gaussian block=15 c=10")) - 139●
steps.push(pl_step("text-lines", w, h, "行投影计数")) - 140●
let (tw, th) = pl_fit(w, h, opts.longest) - 141
if tw != w || th != h { - 142●
steps.push(pl_step("resize", tw, th, "binary 后缩,保二值边缘")) - 143
ow = tw - 144
oh = th - 145
} - 146●
steps.push(pl_step("encode", ow, oh, "1 通道")) - 147
} - 148●
"ecom-white" => { - 149●
steps.push(pl_step("bg-estimate", w, h, "外框 5% 环均值")) - 150●
steps.push(pl_step("mask", w, h, "tol=" + opts.block.to_string())) - 151●
steps.push(pl_step("largest", w, h, "8 连通最大块")) - 152●
steps.push(pl_step("shrink", w, h, "close + erode 1px")) - 153●
steps.push(pl_step("compose", w, h, "硬判白底合成")) - 154●
let side_max = if w >= h { w } else { h } - 155●
steps.push( - 156●
pl_step( - 157
"pad-square", side_max, side_max, "边长 ≤ max(w,h),实际取决于前景框", - 158
), - 159
) - 160
ow = side_max - 161
oh = side_max - 162●
steps.push(pl_step("encode", ow, oh, "3 通道")) - 163
} - 164●
"redact-id" => { - 165●
steps.push(pl_step("scan", w, h, "barcode/qrcode")) - 166●
steps.push(pl_step("redact-auto", -1, -1, "命中即整带扩高填黑")) - 167●
if opts.regions.length() > 0 { - 168●
steps.push( - 169●
pl_step( - 170
"redact-manual", - 171
-1, - 172
-1, - 173●
"regions=" + opts.regions.length().to_string(), - 174
), - 175
) - 176
} - 177●
let (tw, th) = pl_fit(w, h, opts.longest) - 178
if tw != w || th != h { - 179●
steps.push(pl_step("resize", tw, th, "脱敏后缩")) - 180
ow = tw - 181
oh = th - 182
} - 183●
steps.push( - 184●
pl_step("encode", ow, oh, "quality=" + opts.quality.to_string()), - 185
) - 186
} - 187
_ => - 188●
raise @types.ImageError::Process( - 189
@types.ErrorCode::InvalidArgument, - 190
"未知 recipe:\{name}", - 191
) - 192
} - 193
{ - 194
recipe: name, - 195
src_format: info.format_name, - 196
src_width: w, - 197
src_height: h, - 198
src_channels: ch, - 199
out_width: ow, - 200
out_height: oh, - 201
steps, - 202
decode_pixels: px, - 203
peak_frame_bytes: frame, - 204
budget_axis: axis, - 205
budget_bytes: opts.max_bytes, - 206
} - 207
}