# adobe/trustmark architecture

> Architecture diagram of the GitHub repository adobe/trustmark, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/adobe/trustmark
- Repository: https://github.com/adobe/trustmark

## Overview

##### Project type and purpose
- TrustMark is an open-source, multi-language toolkit for embedding, decoding, and removing robust watermarks in images, based on the “TrustMark” universal watermarking paper.
- It provides three parallel implementations: a Python library (PyTorch-based), a JavaScript decoder (ONNX + WebAssembly), and a Rust library + CLI. It also includes a Python example for C2PA manifest integration.

##### File structure
Top-level layout:
- c2pa/ – Python example showing how to inject a watermark presence into a C2PA manifest.
- images/ – Sample images used by the Python test script.
- js/ – Browser/Node decoder using ONNX, with WASM dependencies.
- python/ – Core Python package: modules under trustmark/, configuration, tests, packaging.
- rust/ – Cargo workspace with two crates (trustmark-cli and xtask), plus pure-Rust library code under src/.

Key patterns:
- Multi-module, language-specific wrappers around a shared algorithm.
- Pretrained model downloads at runtime – configured in Python trustmark.py.
- CLI vs. library dichotomy in Rust (trustmark-cli crate vs. core lib).
- JS uses a static ONNX runtime build in wasm and a thin data-layer / watermark logic wrapper.

##### README for architectural clues
- Python: uses PyTorch to train/infer UNet-based watermark encoder/decoder + BCH/ECC error-correction.
- JS: uses ONNX models compiled to WASM modules + lightweight JS glue.
- Rust: performant native routines for image processing, ECC (bch.rs), embedding logic, plus a CLI frontend.
- C2PA example ties into the CAI manifest JSON.

##### Detail
- Files or modules next to components (e.g., python/trustmark/trustmark.py, js/tm_watermark.js, rust/src/bits/bch.rs).
- Do not over-abstract: list each implementation pipeline separately, even if they share logic.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Python Pipeline
    subgraph "Python"
        direction TB
        PY_CLI["Python CLI / test.py"]:::frontend
        PY_WRAPPER["Python Library Wrapper"]:::binding
        PY_CLI -->|"tm.encode()/tm.decode()"| PY_WRAPPER
    end

    %% JavaScript Pipeline
    subgraph "JavaScript"
        direction TB
        JS_FE["Browser/Node Sample (js/index.html)"]:::frontend
        JS_WRAPPER["JS Decoder & Datalayer"]:::binding
        JS_FE -->|"ort.InferenceSession.run()"| JS_WRAPPER
    end

    %% Rust Pipeline
    subgraph "Rust"
        direction TB
        RS_CLI["Rust CLI (trustmark-cli)"]:::frontend
        RS_LIB["Rust Library Wrapper"]:::binding
        RS_CLI -->|"encode()/decode()"| RS_LIB
    end

    %% Core Engine
    subgraph "Core Watermark Engine"
        direction TB
        ENCODER["Encoder (UNet + ECC)"]:::core
        DECODER["Decoder"]:::core
        REMOVER["Remover"]:::core
        ENCODER --> DECODER --> REMOVER
    end

    %% Data Flow
    PY_WRAPPER -->|"calls"| ENCODER
    JS_WRAPPER -->|"calls"| DECODER
    RS_LIB -->|"calls"| ENCODER

    storage["Local Filesystem (images/)"]:::external
    ENCODER -->|"watermarked image"| storage
    DECODER -->|"decoded payload"| storage

    %% External Resources
    subgraph "External Resources"
        direction TB
        MODEL_SERVER["Remote Model Server"]:::external
        LOCAL_CACHE["Model Cache"]:::external
        MODEL_SERVER -->|"download ckpt/.onnx"| LOCAL_CACHE
        PY_WRAPPER --> LOCAL_CACHE
        JS_WRAPPER --> LOCAL_CACHE
    end

    %% C2PA Integration
    subgraph "C2PA Integration"
        direction TB
        C2PA_SCRIPT["C2PA Example Script"]:::frontend
        MANIFEST["manifest.json"]:::external
        KEY_FILES[".pem & .key"]:::external
        C2PA_SCRIPT -->|"reads/writes"| MANIFEST
        C2PA_SCRIPT --> KEY_FILES
        C2PA_SCRIPT --> ENCODER
    end

    %% Click Events
    click PY_CLI "https://github.com/adobe/trustmark/blob/main/python/test.py"
    click PY_WRAPPER "https://github.com/adobe/trustmark/blob/main/python/setup.py"
    click PY_WRAPPER "https://github.com/adobe/trustmark/blob/main/python/pyproject.toml"
    click PY_WRAPPER "https://github.com/adobe/trustmark/blob/main/python/requirements.txt"
    click PY_WRAPPER "https://github.com/adobe/trustmark/blob/main/python/trustmark/__init__.py"

    click ENCODER "https://github.com/adobe/trustmark/blob/main/python/trustmark/unet.py"
    click ENCODER "https://github.com/adobe/trustmark/blob/main/python/trustmark/bchecc.py"
    click ENCODER "https://github.com/adobe/trustmark/blob/main/python/trustmark/datalayer.py"
    click ENCODER "https://github.com/adobe/trustmark/blob/main/python/trustmark/model.py"
    click ENCODER "https://github.com/adobe/trustmark/blob/main/python/trustmark/trustmark.py"

    click JS_WRAPPER "https://github.com/adobe/trustmark/blob/main/js/tm_watermark.js"
    click JS_WRAPPER "https://github.com/adobe/trustmark/blob/main/js/tm_datalayer.js"
    click JS_FE "https://github.com/adobe/trustmark/blob/main/js/index.html"
    click LOCAL_CACHE "https://github.com/adobe/trustmark/blob/main/js/deps/ort-wasm-simd-threaded.wasm"
    click LOCAL_CACHE "https://github.com/adobe/trustmark/blob/main/js/deps/ort-wasm-simd-threaded.jsep.wasm"
    click LOCAL_CACHE "https://github.com/adobe/trustmark/blob/main/js/deps/ort-wasm-simd-threaded.mjs"
    click LOCAL_CACHE "https://github.com/adobe/trustmark/blob/main/js/deps/ort-wasm-simd-threaded.jsep.mjs"

    click RS_CLI "https://github.com/adobe/trustmark/blob/main/rust/crates/trustmark-cli/src/main.rs"
    click RS_LIB "https://github.com/adobe/trustmark/blob/main/rust/src/lib.rs"
    click RS_LIB "https://github.com/adobe/trustmark/blob/main/rust/src/image_processing.rs"
    click RS_LIB "https://github.com/adobe/trustmark/blob/main/rust/src/model.rs"
    click RS_LIB "https://github.com/adobe/trustmark/blob/main/rust/src/bits.rs"
    click RS_LIB "https://github.com/adobe/trustmark/blob/main/rust/src/bits/bch.rs"
    click EXTERNAL "https://github.com/adobe/trustmark/blob/main/rust/Cargo.toml"
    click EXTERNAL "https://github.com/adobe/trustmark/blob/main/rust/.cargo/config.toml"
    click EXTERNAL "https://github.com/adobe/trustmark/blob/main/rust/crates/xtask/src/main.rs"

    click C2PA_SCRIPT "https://github.com/adobe/trustmark/blob/main/c2pa/c2pa_watermark_example.py"
    click MANIFEST "https://github.com/adobe/trustmark/blob/main/c2pa/manifest.json"
    click KEY_FILES "https://github.com/adobe/trustmark/blob/main/c2pa/keys/es256_certs.pem"
    click KEY_FILES "https://github.com/adobe/trustmark/blob/main/c2pa/keys/es256_private.key"
    click storage "https://github.com/adobe/trustmark/tree/main/images/"

    %% Styles
    classDef core fill:#88ccff,stroke:#333,stroke-width:1px
    classDef binding fill:#88ff88,stroke:#333,stroke-width:1px
    classDef frontend fill:#ffcc88,stroke:#333,stroke-width:1px
    classDef external fill:#cccccc,stroke:#333,stroke-width:1px
```
