imgcli\cli_vision.mbt
73.1% · 38 / 52 points · 69 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// `vision` — 文档侧预处理入口(同尺寸,输出不吃缩放)。 - 3
/// - 4
/// 五个 op 全走 `image.vision/ocr` 与 `image.process/color` 的既有实现,CLI 只做 - 5
/// 「多通道先转灰度 → 按 op 分发 → 落盘」三件事。加这条是为了让 vision 侧的 - 6
/// 处理管道也能被 `imgtool quality` 同一口径点评(S29 golden 用它)。 - 7
fn cmd_vision(flags : Flags) -> Out raise @types.ImageError { - 8●
let src = flags.positional[0] - 9
let dst = flags.positional[1] - 10●
let input = src_bytes(src) - 11●
let img = @lib.decode_with_limits(input, limits_of(flags)) - 12●
let op = flags.str("op", "otsu") - 13
let gray = if img.channels >= 3 { - 14●
@color.to_grayscale(img) catch { - 15○
e => raise @types.image_error_from_load(e) - 16
} - 17
} else { - 18●
img - 19
} - 20●
let block = flags.int("block", 15) - 21●
let kappa = flags.dbl("kappa", 0.5) - 22●
let out = match op { - 23●
"gray" => gray - 24
"otsu" => - 25●
@ocr.otsu_threshold(gray) catch { - 26○
e => raise @types.image_error_from_load(e) - 27
} - 28
"sauvola" => - 29○
@ocr.sauvola_threshold(gray, window_size=block, k=kappa) catch { - 30○
e => raise @types.image_error_from_load(e) - 31
} - 32
"shadow" => - 33○
@ocr.shadow_removal(gray, kernel_size=block) catch { - 34○
e => raise @types.image_error_from_load(e) - 35
} - 36
"normalize" => - 37○
@ocr.background_normalization(gray, kernel_size=block) catch { - 38○
e => raise @types.image_error_from_load(e) - 39
} - 40
_ => - 41●
raise @types.ImageError::Process( - 42
@types.ErrorCode::InvalidArgument, - 43
"未知 --op: \{op}(可用 gray/otsu/sauvola/shadow/normalize)", - 44
) - 45
} - 46●
let fmt = out_format(flags, dst, writable_format_of(input)) - 47●
let quality = quality_of(flags) - 48●
let data = encode_image(out, fmt, quality) - 49●
let dry = flags.has("dry-run") - 50●
match budget_gate(src, dst, fmt, data, flags.int("max-bytes", 0), dry) { - 51○
Some(b) => return b - 52●
None => () - 53
} - 54●
let to_stdout = emit_bytes(dst, data, dry) - 55●
result({ - 56●
"src": jstr(src), - 57●
"dst": jstr(dst), - 58●
"op": jstr(op), - 59●
"block": jnum(block), - 60●
"channels": jnum(out.channels), - 61●
"width": jnum(out.width), - 62●
"height": jnum(out.height), - 63●
"format": jstr(format_name(fmt)), - 64●
"bytes": jnum(data.length().to_int()), - 65●
"dry_run": jbool(dry), - 66●
"wrote": jbool(!dry), - 67●
"to_stdout": jbool(to_stdout), - 68
}) - 69
}