image.pro\recipe\thumb_batch.mbt
88.2% · 15 / 17 points · 105 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # thumb-batch:多图批量缩略 - 3
/// - 4
/// 与 `thumb` 的区别只在入口形态:`thumb` 一次一张、产物就是那张图;这里一次给 N 张, - 5
/// 逐张走同一条缩略逻辑(`recipe_thumb`),产物是 N 份字节 + 一份批量回执。 - 6
/// - 7
/// 内存铁律:**不持有解码帧**。每张解完、缩完、编完,`Image` 立刻出作用域交给 GC, - 8
/// `BatchItem` 里只留字节与尺寸元数据;`peak_frame_pixels` 记的是"单张最大帧", - 9
/// 它不是累计值——所以 N 张的峰值与 1 张同阶,这是批量入口存在的意义。 - 10
- 11
///| - 12
/// 一张的产物。刻意不含 `Image`:留着它,批量峰值就变成 N 张帧之和。 - 13
pub(all) struct BatchItem { - 14
name : String - 15
bytes : Bytes - 16
format : @types.ImageFormat - 17
width : Int32 - 18
height : Int32 - 19
src_width : Int32 - 20
src_height : Int32 - 21
quality : Int32 - 22
steps : Array[StepRec] - 23
} - 24
- 25
///| - 26
/// 批量回执。 - 27
pub(all) struct BatchOut { - 28
items : Array[BatchItem] - 29
total_in_bytes : Int32 - 30
total_out_bytes : Int32 - 31
peak_frame_pixels : Int32 - 32
} - 33
- 34
///| - 35
/// 单张形态:注册表里的 `thumb-batch` 就是它,`run_recipe` 与批量入口共用同一段逻辑。 - 36
fn recipe_thumb_batch( - 37
data : Bytes, - 38
opts : RecipeOpts, - 39
) -> RecipeResult raise @types.ImageError { - 40●
let r = recipe_thumb(data, opts) - 41
{ - 42
recipe: "thumb-batch", - 43
image: r.image, - 44
bytes: r.bytes, - 45
format: r.format, - 46
steps: r.steps, - 47
quality: r.quality, - 48
redacted: 0, - 49
blurred: false, - 50
codes: [], - 51
} - 52
} - 53
- 54
///| - 55
/// 按 `RecipeOpts` 逐张缩略;任一张失败即整批失败(返回错误,不做部分成功)。 - 56
/// - 57
/// `opts` 对每张都生效:`longest` 是目标长边、`max_bytes` 是每张的体积上限、 - 58
/// `format` 不给则按解码结果推。`name` 只做回执标识(CLI 传文件路径)。 - 59
pub fn run_thumb_batch( - 60
inputs : Array[(String, Bytes)], - 61
opts : RecipeOpts, - 62
) -> BatchOut raise @types.ImageError { - 63●
let items : Array[BatchItem] = [] - 64
let mut total_in = 0 - 65
let mut total_out = 0 - 66
let mut peak = 0 - 67
for it in inputs { - 68●
let name = it.0 - 69
let data = it.1 - 70●
total_in = total_in + data.length().to_int() - 71●
let r = recipe_thumb_batch(data, opts) - 72
let px = r.image.width * r.image.height - 73
if px > peak { - 74●
peak = px - 75
} - 76●
total_out = total_out + r.bytes.length().to_int() - 77●
let src_w = if r.steps.length() > 0 { - 78●
r.steps[0].width - 79
} else { - 80○
r.image.width - 81
} - 82●
let src_h = if r.steps.length() > 0 { - 83●
r.steps[0].height - 84
} else { - 85○
r.image.height - 86
} - 87●
items.push({ - 88
name, - 89
bytes: r.bytes, - 90
format: r.format, - 91
width: r.image.width, - 92
height: r.image.height, - 93
src_width: src_w, - 94
src_height: src_h, - 95
quality: r.quality, - 96
steps: r.steps, - 97
}) - 98
} - 99
{ - 100
items, - 101
total_in_bytes: total_in, - 102
total_out_bytes: total_out, - 103
peak_frame_pixels: peak, - 104
} - 105
}