ai-meta-rs 0.1.1

Rust bindings for the ai_meta C library — detect, extract, strip, and write AI-generation metadata in PNG/JPEG/WebP
docs.rs failed to build ai-meta-rs-0.1.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.

ai-meta (Rust)

CI License: MIT Crates.io docs.rs Rust Release

ai-meta-rs is a safe Rust crate for detecting, extracting, stripping, and writing AI-generation metadata declared in image files.

It wraps the ai_meta C library and links prebuilt release binaries of that library (pinned by LIBAI_META_VERSION).

Sibling projects: ai-meta (C), ai-meta-php, ai-meta-node, ai-meta-wasm (browser / WebAssembly).

Why

Generators and platforms increasingly embed provenance and generation parameters (Stable Diffusion parameters, EXIF/XMP, C2PA Content Credentials, IPTC). This crate gives Rust applications a small, dependency-light way to inspect and scrub that metadata while leaving pixel data and color profiles intact when requested.

Features

Capability Notes
Formats PNG, JPEG, WebP
Schemes PNG text (tEXt / iTXt / zTXt), EXIF, XMP, IPTC (JPEG), C2PA detect/strip/hints
Scan Fast format + scheme list + likely_ai
Extract BTreeMap<String, String> of fields (e.g. parameters, prompt, Seed, …)
Strip / write Returns a new Vec<u8>; input is never mutated
Errors Error with messages from ai_meta_strerror()
Safety RAII frees ai_meta_info / strip-write buffers; no aliasing of freed C memory

C2PA signatures are not cryptographically verified (same as the C library).

Quick start

Install

[dependencies]
ai-meta-rs = "0.1"
use ai_meta::{extract, scan, strip, write, Flags};

fn main() -> ai_meta::Result<()> {
    let data = std::fs::read("image.png")?;

    let info = scan(&data)?;
    println!("{} likely_ai={} {:?}", info.format, info.likely_ai, info.schemes);

    if info.likely_ai {
        let fields = extract(&data)?;
        // fields["parameters"], fields["prompt"], …
        let _ = fields;
    }

    let cleaned = strip(&data, Flags::STRIP_ALL_AI | Flags::KEEP_COLOR_PROFILE)?;
    let stamped = write(&data, "parameters", "a cat\nSteps: 10, Seed: 1")?;
    let _ = (cleaned, stamped);
    Ok(())
}

First build downloads the pinned ai_meta prebuilt into OUT_DIR when needed (requires network, curl or wget). Runtime needs zlib only — libai_meta.a is linked statically.

WebAssembly

For wasm32-unknown-unknown, build.rs compiles libai_meta and zlib from source with a wasm-capable clang (wasi-sdk). Set WASI_SDK_PATH or AI_META_WASM_CC. Optional AI_META_SRC points at an ai-meta checkout; otherwise the pinned tag is downloaded.

Browser / npm bindings live in the sibling crate ai-meta-wasm (wasm-packpkg/).

From source

git clone https://github.com/localtools/ai-meta-rs.git
cd ai-meta-rs

cargo test
cargo run --example basic -- tests/fixtures/sd_parameters.png

Optional local cache of the C library (skips download when present):

./scripts/fetch-libai-meta.sh   # → deps/ai_meta

Requirements

  • Rust 1.74+
  • A C linker (cc, clang, or gcc)
  • zlib (zlib1g-dev on Debian/Ubuntu; usually present on macOS)
  • Network on first build or a prebuilt prefix

How libai_meta is found

build.rs resolves the library in this order:

  1. AI_META_PREFIX (directory with include/ + lib/libai_meta.a)
  2. ./deps/ai_meta (from scripts/fetch-libai-meta.sh)
  3. pkg-config ai_meta
  4. Download the pinned GitHub Release into OUT_DIR
export AI_META_PREFIX=/path/to/ai_meta-0.1.0-linux-x86_64
cargo build

API

Function Role
scan(&[u8]) -> Result<ScanResult> Format, schemes, likely_ai
extract(&[u8]) -> Result<BTreeMap<String, String>> Field map
strip(&[u8], Flags) -> Result<Vec<u8>> New image bytes
write(&[u8], key, value) -> Result<Vec<u8>> Inject/replace metadata
detect_format(&[u8]) -> Format Format only
version() -> String Linked libai_meta version

Flags

Flag Role
Flags::NONE No strip flags
Flags::STRIP_PNG_TEXT / STRIP_EXIF / STRIP_XMP / STRIP_IPTC / STRIP_C2PA Per-scheme strip
Flags::STRIP_ALL_AI All AI-related strip bits
Flags::KEEP_COLOR_PROFILE Preserve ICC / color profile when possible
Flags::KEEP_NON_AI_TEXT Keep benign PNG text when stripping

Conventions

Topic Rule
Errors Result<T, Error>; message from ai_meta_strerror()
Memory Library buffers freed inside the crate before return
Strip / write Always return a new Vec<u8>
Color profiles Preserved when KEEP_COLOR_PROFILE is set (recommended)

Full docs: docs.rs/ai-meta-rs. Example: examples/basic.rs.

Design

  • Thin safe wrapper over the buffer-based C API (src/ffi.rs + src/lib.rs)
  • Compiled dependency — links release artifacts of ai_meta, not a source copy
  • Runtime deps — zlib (+ the Rust crate deps); static libai_meta.a preferred at link time
  • Portability — Linux and macOS; CI covers both

Out of scope

  • Pixel / ML-based “is this AI?” detection
  • Full C2PA claim verification and signing
  • Lossy re-encode of image payloads (metadata containers only)
  • Bundling or re-implementing the C library inside this repo

Testing

cargo test
cargo clippy --all-targets -- -D warnings
cargo fmt --check

Coverage includes scan, extract, write/strip round-trip, and error paths against small PNG fixtures under tests/fixtures/.

CI: .github/workflows/ci.yml.

Project layout

Cargo.toml                Package metadata (name: ai-meta-rs, lib: ai_meta)
build.rs                  Locate / download libai_meta + link + zlib
src/lib.rs                Safe public API
src/ffi.rs                Raw `ai_meta.h` bindings
examples/basic.rs         Minimal CLI demo
scripts/fetch-libai-meta.sh
tests/fixtures/           PNG fixtures
LIBAI_META_VERSION        Pinned ai_meta release
LICENSE NOTICE            MIT + third-party notices

Releases

Tag a release to run tests, publish to crates.io, and create a GitHub Release:

git tag v0.1.1
git push origin v0.1.1

Requires repository secret CARGO_REGISTRY_TOKEN (crates.io API token). Workflow: .github/workflows/release.yml.

Native library (dependency)

Item Location
Releases https://github.com/localtools/ai-meta/releases
Pin file LIBAI_META_VERSION
Fetch helper scripts/fetch-libai-meta.sh

Contributing

Contributions are welcome. See CONTRIBUTING.md and our Code of Conduct.

For security issues, see SECURITY.md — please do not open public issues for vulnerabilities.

Changelog

See CHANGELOG.md.

License

This project is licensed under the MIT License — see LICENSE and NOTICE.

Copyright (c) 2026 Local Tools and ai-meta-rs contributors

You may use, modify, and distribute this software under the terms of that license.

The native ai_meta library is also MIT-licensed; prebuilt binaries are obtained from its GitHub Releases.