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