# SPEC
## Library API (crate `ocr`)
### `OcrEngine`
Builder-style configuration and OCR entry points.
| `new` | `() -> Self` | Default engine (`language`: `"eng"`, preprocessing off) |
| `language` | `(impl Into<String>) -> Self` | Sets `language` field (reserved / future use) |
| `preprocessing` | `(bool) -> Self` | Enable grayscale + threshold preprocessing before binarize |
| `recognize_file` | `(&Path) -> Result<OcrResult>` | Load image from path and run OCR |
| `recognize_bytes`| `(&[u8]) -> Result<OcrResult>` | Decode image from memory and run OCR |
| `recognize_image`| `(&DynamicImage) -> Result<OcrResult>` | Run OCR on an already-loaded image |
### `OcrResult`
| `text` | `String` | Full text; lines joined with `\n` |
| `words` | `Vec<OcrWord>` | Word-level segments (space-split within a line) |
| `confidence` | `f32` | Average character confidence (0–100) |
### `OcrWord`
| `text` | `String` | Recognized word |
| `confidence` | `f32` | Average confidence for characters in the word |
| `bounding_box` | `BoundingBox` | Union bbox of character boxes in the word |
### `BoundingBox`
| `x` | `u32` | Left edge (pixels) |
| `y` | `u32` | Top edge (pixels) |
| `width` | `u32` | Width (pixels) |
| `height` | `u32` | Height (pixels) |
### `Error`
| `Io` | I/O errors (including filesystem) |
| `Image` | Image decode errors (`image` crate) |
| `NoText` | No text detected (reserved / optional use) |
### Preprocessing (`preprocess` module)
| `grayscale` | Luminance from dynamic image |
| `threshold` | Binary image at fixed level |
| `binarize` | Otsu threshold → `Vec<Vec<bool>>` grid (used by engine) |
| `preprocess` | Grayscale then threshold at 128 (optional path) |
| `clean_noise` | Remove small foreground components |
---
## CLI (binary `ocr`)
```
ocr [OPTIONS] <IMAGE_PATH>
Arguments:
<IMAGE_PATH> Path to the image file
Options:
-f, --format <FORMAT> Output format: text, json [default: text]
--preprocess Enable preprocessing before OCR
-h, --help Print help
-V, --version Print version
```
**Output**
- `text` — plain text to stdout
- `json` — pretty-printed `OcrResult`
**Note:** There is **no** `--tesseract` (or any external OCR) option; recognition is entirely in-process.
---
## MCP server (binary `ocr-mcp`)
Transport: stdio (MCP JSON-RPC on stdin/stdout).
### Tools
#### `ocr_image`
OCR on an image file (absolute path recommended for tools).
| `image_path` | string | Path to the image file |
**Returns:** Extracted text; may append a short confidence / word-count footer.
#### `ocr_base64`
OCR on base64-encoded image bytes (raw base64 or data URI).
| `image_base64` | string | Encoded image |
**Returns:** Same shape as `ocr_image`. Errors are returned as plain-text lines starting with `Error:`.
---
## System dependencies
**None** for OCR. Only a normal Rust toolchain is required to build. Runtime does not invoke Tesseract or other OCR installations.
## Rust dependencies (see `Cargo.toml`)
| `image` | Load/decode images, export formats |
| `thiserror` | Error enum |
| `serde` / `serde_json` | JSON serialization (CLI / MCP) |
| `clap` | CLI parsing |
| `anyhow` | CLI error convenience |
| `tokio` | Async runtime (MCP server) |
| `rmcp` | MCP server implementation |
| `schemars` | JSON Schema for MCP tool params |
| `base64` | MCP base64 decoding |
| `tracing` / `tracing-subscriber` | Logging (MCP) |