# Local manga panel colorization

An experimental, on-demand reader tool for Android, iOS, and desktop. Long-press a
page, choose **Colorize locally**, then colorize the whole page or select a panel.
The result is a separate PNG preview with Save and Share actions. Source pages,
downloads, and the library database are not replaced.

## Model setup

1. In the colorizer, choose **Model source**. This opens the pinned export page:
   [Faridzar/manga-colorization-v2-onnx](https://huggingface.co/Faridzar/manga-colorization-v2-onnx/tree/5515e06d31b08ffd107af686cba5e98e95e8d4cf).
2. Review its terms and download `manga-colorize-fp16.onnx` (61,650,260 bytes).
3. Return to Yokan and choose **Import model**. Select the downloaded file.
4. Drag a rectangle over a panel, use **Adjust panel bounds** for keyboard or
   accessibility controls, or choose **Whole page**. Then **Colorize selection**.
5. Compare with the original page, save a PNG, or share it using the platform UI.

Only this exact export is accepted. Its SHA-256 is:

```text
39660d0047ea6f1a0ddee6aa89054997f95ea566f4d56ff762f66dbcf1a1a7ef
```

Import validates size and digest before atomically replacing the local copy.
Inference verifies the stored copy whenever it loads it into the native runtime. Model files
are held under the application cache's `colorizer/` directory; clearing the app
cache may require reimporting. **Remove local model** releases its disk space.
Yokan does not download weights automatically or upload pages for inference.

## Model provenance

The export is attributed to
[qweasdd/manga-colorization-v2](https://github.com/qweasdd/manga-colorization-v2).
The export's model card labels it MIT; the original repository did not include a
license file at review time. Yokan ships inference code and links to the export,
not model weights. The publisher's labeling is not independent verification of
rights to redistribute the original weights or training material.

## Platform integration

- Android: ONNX Runtime Android 1.24.2; BitmapFactory subsampling and PNG encoding.
- Desktop: ONNX Runtime Java 1.24.2; ImageIO subsampling and PNG encoding. Intel
  macOS uses 1.22.0 because newer Java artifacts omit its native JNI binaries.
  Packaged platforms are Windows x64, macOS ARM64/x64, and Linux ARM64/x64.
- iOS: ONNX Runtime 1.24.2 through Microsoft's official Swift package, linked to
  the Xcode app. `PanelColorizerBridge.mm` provides ImageIO/CoreGraphics codecs
  and synchronous Objective-C runtime inference to Kotlin. No Python is needed.
- Web: the action explains that local colorization requires a native app; no
  model or ONNX runtime is loaded in the browser.

Minified Android and desktop builds keep ONNX Runtime's JNI classes because
`composeApp/release-rules.pro` keeps every class outside the Material icon
packages, as the
[ONNX Runtime Android guidance](https://onnxruntime.ai/docs/build/android.html)
requires.

The iOS package version and revision are locked in `Package.resolved`. Open the
existing `iosApp` Xcode scheme; package resolution supplies the runtime. The
Gradle-generated framework expects the bridge in the host app, like the existing
embedded extension bridge.

## Resource and quality limits

- Only one inference/import/removal operation can hold the process-wide gate.
- One CPU inference thread; no GPU setup or background chapter processing.
- A 64 × 64, five-channel model input with aspect-preserving white padding; hints
  and hint masks are zero. Color estimation is deliberately coarse.
- Output uses original luminance and linework with interpolated model chroma.
  White speech bubbles and black ink remain neutral.
- Source payloads are limited to 25 MB; rasterization is subsampled to a maximum
  dimension of 1600 pixels before allocating the output image.
- Runtime tensors are scoped to each job. On Android and desktop the native session
  stays loaded between panels and closes after a minute without use or when the
  model is removed; iOS still opens one per job. Preview images bypass
  Coil's memory/disk caches. Only the current result is retained by the reader.
- Cancelling dismisses the UI and discards the result. An in-flight synchronous
  native call finishes before releasing resources and allowing the next job.
  It is not forcibly interrupted or destroyed while running.

The exploratory CPU test on Apple Silicon peaked around **430 MB for the entire
standalone Python test process**, compared with over 1 GB at a 256-pixel input.
This is evidence for choosing a conservative default, not a device-memory cap or
an Android/iOS memory profile. The model remains too expensive for some low-memory
phones. Colors are approximate, can bleed between regions, and need not match
character designs or remain consistent between panels. Selecting a single panel
usually gives better results than coloring a full page at this input resolution.

PNG and JPEG are the portable baseline. Decoder support for other formats varies
by platform. Output is a preview/export feature, not a persistent reader filter,
automatic panel detector, reference-guided colorizer, or chapter batch job.

## Validation

Targeted tests cover crop geometry, white padding and hints, preservation of ink
and white areas, corrupt model rejection, atomic import cleanup, decoder bounds,
and serialization across a cancelled native call. Optional real-model tests run
repeated inference and write PNG previews under `composeApp/build/reports/colorizer/`.

```sh
YOKAN_COLORIZER_TEST_MODEL=/path/to/manga-colorize-fp16.onnx \
YOKAN_COLORIZER_TEST_IMAGE=/path/to/page.jpg \
./gradlew :composeApp:desktopTest --no-daemon --no-parallel --max-workers=1 \
  -Pkotlin.compiler.execution.strategy=in-process \
  --tests '*PanelColorizerTest' --tests '*PanelColorizerDesktopTest' \
  --tests '*PanelColorizerRenderTest' --tests '*ReaderViewModelStreamTest'
```

The real-model test is explicitly skipped when the two environment variables are
absent. Model weights and test artwork are not checked into the repository.
Desktop real-model inference, the targeted tests, and Android, iOS device/simulator,
and Wasm Kotlin compilation passed. The Objective-C bridge also passed syntax
checks for iOS device and simulator SDKs. This does not verify a signed iOS app
or inference on physical Android/iOS devices. Device profiling and real mobile
reading-plus-colorization testing are still needed before production use.
