image.pro\recipe\thumb_batch.mbt

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