image.pro\recipe\plan.mbt

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