image.pro\recipe\redact_id.mbt

93.1% · 81 / 87 points · 283 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/// # redact-id:证件 / 票据脱敏
  3. 3///
  4. 4/// 与 `sanitize` 的分工:`sanitize` 是"给框就打码"(块均值,适合对外发图前手标),
  5. 5/// `redact-id` 是**先自动找敏感区、再实心覆盖**:
  6. 6///
  7. 7/// - 自动命中:复用 `scan_codes` 的条码/二维码结果(证件号、票据码就在这些框里),
  8. 8/// 每个命中框外扩 `pad` 像素;
  9. 9/// - 手工补充:`opts.regions` 里的框同样处理(人脸、签名等条码找不到的东西);
  10. 10/// - 覆盖方式**不可逆**:整块填纯黑,不做块均值/模糊——均值仍可能被反解,实心填才叫脱敏;
  11. 11/// - 编码即剥离元数据(我们本来就不写 EXIF)。
  12. 12///
  13. 13/// 验收口径:`diff_pixels(orig, out)` 在脱敏区内必须全部不同,`solid_pixels(out, 0)`
  14. 14/// 精确等于各框面积(并集不重叠时)。
  15. 15
  16. 16///|
  17. 17/// 判断某一行在 `[x0, x1)` 带内是不是"条纹行":相邻像素亮度跳变(>40)至少 6 次。
  18. 18/// 条码行必满足(几十条黑白交替),文字行与空白行达不到。
  19. 19fn ri_row_is_striped(
  20. 20 img : @types.Image,
  21. 21 x0 : Int32,
  22. 22 x1 : Int32,
  23. 23 y : Int32,
  24. 24) -> Bool {
  25. 25● let w = img.width
  26. 26 let ch = img.channels
  27. 27 let mut jumps = 0
  28. 28 let mut prev = -1
  29. 29 let mut x = x0
  30. 30 while x < x1 {
  31. 31● let base = (y * w + x) * ch
  32. 32 let v = if ch == 1 {
  33. 33● img.data[base].to_int()
  34. 34 } else {
  35. 35● let r = img.data[base].to_int()
  36. 36● let g = img.data[base + 1].to_int()
  37. 37● let b = img.data[base + 2].to_int()
  38. 38 (r * 77 + g * 150 + b * 29) / 256
  39. 39 }
  40. 40● if prev >= 0 && (v - prev).abs() > 40 {
  41. 41● jumps = jumps + 1
  42. 42 }
  43. 43 prev = v
  44. 44 x = x + 1
  45. 45 }
  46. 46 jumps >= 6
  47. 47}
  48. 48
  49. 49///|
  50. 50/// 把"扫描线那一行"扩成整条码带:从命中行向上/下连续找条纹行,返回 `[top, bot)`。
  51. 51///
  52. 52/// 解码器只在一条扫描线上定位,回执里的 `height` 常是 1;不扩高就只涂一条线,
  53. 53///
  54. 54/// 脱敏等于没脱。这是 `redact-id` 必须自己补的一步。
  55. 55fn ri_code_band(
  56. 56 img : @types.Image,
  57. 57 x0 : Int32,
  58. 58 x1 : Int32,
  59. 59 y_hint : Int32,
  60. 60) -> (Int32, Int32) {
  61. 61● let w = img.width
  62. 62 let h = img.height
  63. 63◐ let lx = if x0 < 0 { 0 } else { x0 }
  64. 64◐ let rx = if x1 > w { w } else { x1 }
  65. 65◐ let mut top = if y_hint < 0 { 0 } else { y_hint }
  66. 66● while top > 0 && ri_row_is_striped(img, lx, rx, top - 1) {
  67. 67● top = top - 1
  68. 68 }
  69. 69 let mut bot = top + 1
  70. 70● while bot < h && ri_row_is_striped(img, lx, rx, bot) {
  71. 71● bot = bot + 1
  72. 72 }
  73. 73 (top, bot)
  74. 74}
  75. 75
  76. 76///|
  77. 77/// 实心覆盖:把 `r` 内所有像素填成 `(rv,gv,bv)`,越界部分夹到边界(不 panic)。
  78. 78/// 4 通道保留 alpha 原值——脱敏不该顺手改透明度。
  79. 79pub fn solid_fill(
  80. 80 img : @types.Image,
  81. 81 r : @types.Region,
  82. 82 rv : Byte,
  83. 83 gv : Byte,
  84. 84 bv : Byte,
  85. 85) -> @types.Image {
  86. 86● let w = img.width
  87. 87 let h = img.height
  88. 88 let ch = img.channels
  89. 89● let x0 = if r.x < 0 { 0 } else { r.x }
  90. 90● let y0 = if r.y < 0 { 0 } else { r.y }
  91. 91● let x1 = if r.x + r.w > w { w } else { r.x + r.w }
  92. 92● let y1 = if r.y + r.h > h { h } else { r.y + r.h }
  93. 93● let n = img.data.length()
  94. 94● let out : Array[Byte] = Array::make(n, (0 : Byte))
  95. 95 for i in 0..<n {
  96. 96● out[i] = img.data[i]
  97. 97 }
  98. 98 if x1 <= x0 || y1 <= y0 {
  99. 99● return { width: w, height: h, channels: ch, data: Bytes::from_array(out), }
  100. 100 }
  101. 101 for y in y0..<y1 {
  102. 102● for x in x0..<x1 {
  103. 103● let base = (y * w + x) * ch
  104. 104 if ch == 1 {
  105. 105● out[base] = rv
  106. 106 } else {
  107. 107● out[base] = rv
  108. 108 out[base + 1] = gv
  109. 109 out[base + 2] = bv
  110. 110 }
  111. 111 }
  112. 112 }
  113. 113● { width: w, height: h, channels: ch, data: Bytes::from_array(out), }
  114. 114}
  115. 115
  116. 116///|
  117. 117/// 两张同尺寸同通道图里"像素值不同"的像素数(任一通道不同即计 1)。
  118. 118pub fn diff_pixels(a : @types.Image, b : @types.Image) -> Int32 {
  119. 119● if a.width != b.width || a.height != b.height || a.channels != b.channels {
  120. 120● return -1
  121. 121 }
  122. 122 let ch = a.channels
  123. 123 let n = a.width * a.height
  124. 124 let mut count = 0
  125. 125 for i in 0..<n {
  126. 126● let base = i * ch
  127. 127 let mut same = true
  128. 128 for c in 0..<ch {
  129. 129● if a.data[base + c] != b.data[base + c] {
  130. 130● same = false
  131. 131 break
  132. 132 }
  133. 133 }
  134. 134 if !same {
  135. 135● count = count + 1
  136. 136 }
  137. 137 }
  138. 138 count
  139. 139}
  140. 140
  141. 141///|
  142. 142/// 统计纯 `(rv,gv,bv)` 的像素数(验收"实心黑铺满矩形")。
  143. 143pub fn solid_pixels(
  144. 144 img : @types.Image,
  145. 145 rv : Byte,
  146. 146 gv : Byte,
  147. 147 bv : Byte,
  148. 148) -> Int32 {
  149. 149● let ch = img.channels
  150. 150 let n = img.width * img.height
  151. 151 let mut count = 0
  152. 152 for i in 0..<n {
  153. 153● let base = i * ch
  154. 154 let hit = if ch == 1 {
  155. 155● img.data[base] == rv
  156. 156 } else {
  157. 157● img.data[base] == rv &&
  158. 158 img.data[base + 1] == gv &&
  159. 159 img.data[base + 2] == bv
  160. 160 }
  161. 161 if hit {
  162. 162● count = count + 1
  163. 163 }
  164. 164 }
  165. 165 count
  166. 166}
  167. 167
  168. 168///|
  169. 169/// redact-id Recipe:读 →(自动命中条码区 + 手工框)实心覆盖 → 编码。
  170. 170///
  171. 171/// 无命中且无手工框时不失败:返回原图并记 `nothing-to-redact`——证件扫描件可能真没码,
  172. 172/// 让调用方自己判断要不要补框。
  173. 173fn recipe_redact_id(
  174. 174 data : Bytes,
  175. 175 opts : RecipeOpts,
  176. 176) -> RecipeResult raise @types.ImageError {
  177. 177● let steps : Array[StepRec] = []
  178. 178● let img0 = step_decode(data, steps)
  179. 179 let mut img = img0
  180. 180◐ let pad = if opts.block > 0 { opts.block } else { 4 }
  181. 181● let hits = scan_codes(img)
  182. 182● steps.push({
  183. 183 name: "scan",
  184. 184 note: "hits=\{hits.length()}",
  185. 185 width: img.width,
  186. 186 height: img.height,
  187. 187 bytes: -1,
  188. 188● ms: seg_ms(),
  189. 189 })
  190. 190 let mut auto = 0
  191. 191 for hit in hits {
  192. 192● let (top, bot) = ri_code_band(
  193. 193 img,
  194. 194 hit.x,
  195. 195 hit.x + hit.width,
  196. 196 hit.y + hit.height / 2,
  197. 197 )
  198. 198● let r = @types.Region::new(
  199. 199 hit.x - pad,
  200. 200 top - pad,
  201. 201 hit.width + pad * 2,
  202. 202 bot - top + pad * 2,
  203. 203 )
  204. 204● img = solid_fill(img, r, b'\x00', b'\x00', b'\x00')
  205. 205 auto = auto + 1
  206. 206 }
  207. 207 if auto > 0 {
  208. 208● steps.push({
  209. 209 name: "redact-auto",
  210. 210 note: "blocks=\{auto} pad=\{pad}",
  211. 211 width: img.width,
  212. 212 height: img.height,
  213. 213 bytes: -1,
  214. 214● ms: seg_ms(),
  215. 215 })
  216. 216 }
  217. 217 let mut manual = 0
  218. 218 for r in opts.regions {
  219. 219● img = solid_fill(img, r, b'\x00', b'\x00', b'\x00')
  220. 220 manual = manual + 1
  221. 221 }
  222. 222 if manual > 0 {
  223. 223● steps.push({
  224. 224 name: "redact-manual",
  225. 225 note: "blocks=\{manual}",
  226. 226 width: img.width,
  227. 227 height: img.height,
  228. 228 bytes: -1,
  229. 229● ms: seg_ms(),
  230. 230 })
  231. 231 }
  232. 232 if auto == 0 && manual == 0 {
  233. 233● steps.push({
  234. 234 name: "redact",
  235. 235 note: "nothing-to-redact",
  236. 236 width: img.width,
  237. 237 height: img.height,
  238. 238 bytes: -1,
  239. 239● ms: seg_ms(),
  240. 240 })
  241. 241 }
  242. 242● let (tw, th) = fit_longest(img, opts.longest)
  243. 243 let mut cur = img
  244. 244 if tw != cur.width || th != cur.height {
  245. 245● cur = @transform.resize_to_contain(
  246. 246 cur,
  247. 247 tw,
  248. 248 th,
  249. 249● Array::make(cur.channels, (255 : Byte)),
  250. 250 ) catch {
  251. 251○ e => raise @types.image_error_from_load(e)
  252. 252 }
  253. 253● steps.push({
  254. 254 name: "resize",
  255. 255 note: "",
  256. 256 width: cur.width,
  257. 257 height: cur.height,
  258. 258 bytes: -1,
  259. 259● ms: seg_ms(),
  260. 260 })
  261. 261 }
  262. 262● let fmt0 = pick_format(cur, opts.format)
  263. 263● let (bytes, q0) = encode_fit(cur, fmt0, opts.quality, opts.max_bytes)
  264. 264● steps.push({
  265. 265 name: "encode",
  266. 266 note: "quality=\{q0}",
  267. 267 width: cur.width,
  268. 268 height: cur.height,
  269. 269● bytes: bytes.length().to_int(),
  270. 270● ms: seg_ms(),
  271. 271 })
  272. 272 {
  273. 273 recipe: "redact-id",
  274. 274 image: cur,
  275. 275 bytes,
  276. 276 format: fmt0,
  277. 277 steps,
  278. 278 quality: q0,
  279. 279 redacted: auto + manual,
  280. 280 blurred: false,
  281. 281 codes: hits,
  282. 282 }
  283. 283}