Merge remote-tracking branch 'origin/main' into pr-11825

# Conflicts:
#	contributors/people.toml
#	crates/ui-egui/src/widgets.rs
#	crates/ui-egui/src/widgets/fields.rs
#	crates/ui-egui/src/widgets/units.rs
This commit is contained in:
David Mann committed 2026-11-13 23:48:59 +01:00
commit 778545d231
882 files changed
+39422 -34411

No files matched your search

+1 -1
View File
@@ -16,7 +16,7 @@ nextest-version = "0.9.150"
gpu = { max-threads = 4 }
[[profile.default.overrides]]
filter = 'package(photocraft-gpu) | (package(photocraft-ui-egui) & (kind(test) | test(/canvas|gpu|mip|type_tool|background_redraw::|floating_group_titles|swap_arrow::tests::rendered_glyph/)))'
filter = 'package(photocraft-gpu) | (package(photocraft-ui-egui) & (kind(test) | test(/canvas|gpu|mip|type_tool|background_redraw::|floating_group_titles|swap_arrow::tests::rendered_glyph/))) | (package(photocraft-automation) & test(/^gpu_backend::/))'
test-group = 'gpu'
# CI (`--profile ci`, .github/workflows/ci.yml). Collect all failures in one integration run
+107 -78
View File
@@ -68,6 +68,12 @@ concurrency:
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
# Git dependencies come from github.com/storytold, and Cargo.lock is complete and up to date.
git-sources:
if: github.event.action != 'labeled' || github.event.label.name == 'full-ci'
# Tracks @main so suite-wide CI fixes reach every app without a PR in each repository.
uses: storytold/craft-common-actions/.github/workflows/git-sources.yml@main
# Whether the Windows legs and the web build run. main, manual runs and PRs labelled `full-ci`
# get both. Other PRs get Windows when they touch code whose behaviour differs by platform: the
# app shell, UI, text and fonts, GPU, pen input, native saves, build scripts or the lockfile.
@@ -82,41 +88,14 @@ jobs:
windows: ${{ steps.pick.outputs.windows }}
web: ${{ steps.pick.outputs.web }}
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
persist-credentials: false
- name: Git dependencies come from storytold
run: |
set -euo pipefail
test -f Cargo.lock || { echo "Cargo.lock is missing"; exit 1; }
# Source replacement, patches and path overrides in cargo config never show up
# in Cargo.lock. Parsed rather than grepped: TOML has many spellings of one key.
# Cargo still reads the legacy .cargo/config.
python3 - <<'PY'
import os, sys, tomllib
for path in (".cargo/config", ".cargo/config.toml"):
if not os.path.exists(path):
continue
config = tomllib.load(open(path, "rb"))
replaced = [name for name, source in config.get("source", {}).items()
if isinstance(source, dict) and "replace-with" in source]
if replaced or "patch" in config or "paths" in config:
print(f"{path} replaces, patches or overrides a source")
sys.exit(1)
PY
# Fails when Cargo.lock is out of date with the manifests.
cargo metadata --locked --format-version 1 > /dev/null
python3 - <<'PY'
import sys, tomllib
lock = tomllib.load(open("Cargo.lock", "rb"))
bad = sorted({p["source"] for p in lock.get("package", [])
if p.get("source", "").startswith("git+")
and not p["source"].startswith("git+https://github.com/storytold/")})
if bad:
print("Git dependencies outside github.com/storytold:", *bad, sep="\n")
sys.exit(1)
PY
- id: pick
env:
EVENT: ${{ github.event_name }}
@@ -170,8 +149,8 @@ jobs:
# Windows tests run as two legs in parallel: the unit tests and the integration tests. As one job
# they took ~28 min on the Windows runner; the legs take ~13 and ~15. Splitting the integration
# tests over a third runner (nextest --partition) saved ~2 min of wall time but cost ~7
# runner-minutes and one more Windows slot per PR. They stay outside the shared gate for sccache,
# which needs its own action step.
# runner-minutes and one more Windows slot per PR. Moving these legs into the shared gate is
# tracked by storytold/craft-common-actions#212.
windows:
needs: platforms
if: needs.platforms.outputs.windows == 'true'
@@ -183,6 +162,10 @@ jobs:
matrix:
part: [unit, integration]
env:
# Released sccache expands Cargo's response files and exceeds Windows' rustc spawn limit
# (#13543). Let Cargo handle long commands until a release includes mozilla/sccache#3026.
# The dependency rust-cache below stays enabled; only compiler caching is disabled.
RUSTC_WRAPPER: ""
# No debuginfo on Windows: writing PDBs for ~200 test binaries cost 2.2 of ~21 build minutes.
# A panic still names its file and line; only backtrace frames lose theirs (Linux keeps
# line-tables-only, so a backtrace with lines is one OS away).
@@ -197,6 +180,10 @@ jobs:
CRAFT_FONTS_DIR: ${{ github.workspace }}/craft-fonts
CRAFT_FONTS_REQUIRED: "1"
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
@@ -214,37 +201,20 @@ jobs:
- name: Check out craft-i18n
uses: storytold/craft-common-actions/actions/checkout-craft-i18n@main
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
with:
toolchain: stable
components: clippy
# Only main saves caches; PRs restore main's, so the repo's 10 GB cache budget keeps main's.
# A failing test still saves: the cache holds compiled dependencies, not test results.
# Both legs share one key (same dependencies); the integration leg saves it, because it also
# runs Clippy and so leaves Clippy's dependency metadata in it.
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
with:
save-if: ${{ github.ref == 'refs/heads/main' && matrix.part == 'integration' }}
cache-on-failure: true
# sccache caches the workspace crates' compiled libraries, which rust-cache leaves out:
# warm, each Windows leg built 4.5 min faster. Like rust-cache, only main writes (PRs read
# main's entries); a full set is ~90 MB, so it barely touches the 10 GB budget (#2688).
# Test executables can't be cached, so a PR still links its tests.
- name: Install sccache
uses: Mozilla-Actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11
with:
version: v0.18.0
- name: Use sccache
env:
MAIN: ${{ github.ref == 'refs/heads/main' }}
run: |
echo "SCCACHE_GHA_ENABLED=true" >> "$GITHUB_ENV"
echo "RUSTC_WRAPPER=sccache" >> "$GITHUB_ENV"
if [ "$MAIN" != true ]; then echo "SCCACHE_GHA_RW_MODE=READ_ONLY" >> "$GITHUB_ENV"; fi
shell: bash
- name: Clippy
if: matrix.part == 'integration'
run: cargo clippy --workspace --all-targets --locked --features heif -- -D warnings
@@ -253,7 +223,7 @@ jobs:
if: matrix.part == 'integration'
run: cargo xtask layers
- uses: taiki-e/install-action@3a3daafd3b7f23b8f65d9f5ba2db203fb59b79e2 # v2.90.2
- uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32
with:
tool: cargo-nextest@0.9.150
@@ -290,6 +260,10 @@ jobs:
CARGO_INCREMENTAL: 0
CARGO_NET_RETRY: 10
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
@@ -306,16 +280,18 @@ jobs:
libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev
libgtk-3-dev
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
with:
toolchain: stable
# gate.yml keys its cache `<cache-version>-rust-<hash of Cargo.toml>-gate`.
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
with:
prefix-key: v1-rust
shared-key: ${{ hashFiles('Cargo.toml') }}-gate
save-if: false
- uses: taiki-e/install-action@3a3daafd3b7f23b8f65d9f5ba2db203fb59b79e2 # v2.90.2
- uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32
with:
tool: cargo-nextest@0.9.150
@@ -339,11 +315,17 @@ jobs:
CARGO_PROFILE_RELEASE_LTO: "false"
CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "16"
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
with:
toolchain: stable
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
with:
key: corpus
save-if: ${{ github.ref == 'refs/heads/main' }}
@@ -369,6 +351,10 @@ jobs:
# Match release.yml so PR results reflect what would actually ship.
TRUNK_VERSION: 0.21.14
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
@@ -378,19 +364,20 @@ jobs:
- name: Check out craft-i18n
uses: storytold/craft-common-actions/actions/checkout-craft-i18n@main
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
with:
toolchain: stable
targets: wasm32-unknown-unknown
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
with:
key: ci-web
save-if: ${{ github.ref == 'refs/heads/main' }}
cache-on-failure: true
# Checksum-verified, as in release.yml's web build.
- name: Install trunk
run: |
curl --proto '=https' -fsSL "https://github.com/trunk-rs/trunk/releases/download/v${TRUNK_VERSION}/trunk-x86_64-unknown-linux-gnu.tar.gz" \
| tar -xz -C "$HOME/.cargo/bin" trunk
trunk --version
uses: storytold/craft-common-actions/actions/install-trunk@main
with:
trunk-version: ${{ env.TRUNK_VERSION }}
# package.sh runs Trunk's release builds, validates relative URLs, cuts the wasm into
# parts, checks the 24 MiB per-file limit and exercises the packaging step release.yml uses.
- name: Build and verify browser release
@@ -446,11 +433,17 @@ jobs:
- { crate: affinity, name: import_preview, args: "--features io" }
- { crate: format, name: load }
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # nightly
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
with:
toolchain: stable
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
with:
workspaces: crates/${{ matrix.target.crate }}/fuzz
- name: Install cargo-fuzz
@@ -474,6 +467,10 @@ jobs:
env:
RAYON_NUM_THREADS: "4"
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
@@ -483,8 +480,10 @@ jobs:
packages: >-
libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev
libgtk-3-dev
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
with:
toolchain: stable
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
with:
key: color-range-perf
- name: Compare exact outputs and release timings on 24 MP
@@ -504,6 +503,10 @@ jobs:
env:
RAYON_NUM_THREADS: "4"
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
@@ -513,8 +516,10 @@ jobs:
packages: >-
libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev
libgtk-3-dev
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
with:
toolchain: stable
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
with:
key: selection-morphology-perf
- name: Compare exact outputs and release timings on 36 MP
@@ -539,11 +544,17 @@ jobs:
runs-on: macos-15
timeout-minutes: 60
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
with:
toolchain: stable
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
with:
key: perf-release
- name: Quick performance suite
@@ -563,11 +574,17 @@ jobs:
env:
RAYON_NUM_THREADS: "4"
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
with:
toolchain: stable
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
with:
key: selection-combining-perf
- name: Compare exact outputs and release timings on 36 MP
@@ -587,6 +604,10 @@ jobs:
env:
RAYON_NUM_THREADS: "4"
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@v7
with:
persist-credentials: false
@@ -596,8 +617,10 @@ jobs:
persist-credentials: false
ref: ${{ inputs.brush_tip_base }}
path: before
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
with:
toolchain: stable
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
with:
workspaces: |
before -> target
@@ -633,6 +656,10 @@ jobs:
CARGO_PROFILE_RELEASE_LTO: "false"
CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "16"
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
@@ -647,7 +674,9 @@ jobs:
git worktree add --detach "$RUNNER_TEMP/photocraft-baseline" "$base"
mkdir -p "$RUNNER_TEMP/photocraft-baseline/crates/engine/examples"
cp crates/engine/examples/bench_dry_mixer.rs "$RUNNER_TEMP/photocraft-baseline/crates/engine/examples/bench_dry_mixer.rs"
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
with:
toolchain: stable
- name: Install Linux build dependencies
uses: storytold/craft-common-actions/actions/install-apt-packages@main
with:
+12 -9
View File
@@ -27,13 +27,16 @@ concurrency:
jobs:
build:
timeout-minutes: 15
name: mdBook build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- name: Install mdBook
run: cargo install mdbook --version 0.4.52 --locked
- name: Build documentation
run: mdbook build book
# Tracks @main so suite-wide CI fixes reach every app without a PR in each repository.
uses: storytold/craft-common-actions/.github/workflows/gate.yml@main
with:
timeout-minutes: 15
# The book needs mdBook only: no craft-fonts, apt packages, wasm target or nextest.
craft-fonts-ref: ""
targets: ""
free-disk-space: false
# Prebuilt mdBook, the version cargo install built before.
tools: mdbook@0.4.52
cache-version: v1-docs
command: mdbook build book
+8
View File
@@ -29,6 +29,10 @@ jobs:
name: Verified AppImage inputs
runs-on: ubuntu-latest
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- run: bash packaging/linux/verified-download-test.sh
@@ -37,6 +41,10 @@ jobs:
name: Windows installation policy (no app build)
runs-on: windows-latest
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
+8 -2
View File
@@ -43,13 +43,19 @@ jobs:
runs-on: macos-15
timeout-minutes: 120
steps:
- name: Harden runner
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
with:
egress-policy: audit
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
with:
toolchain: stable
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
with:
key: perf-release
+5
View File
@@ -1,6 +1,10 @@
name: Release
on:
# A nightly dry run on main: every release build except macOS, nothing signed or released
# (craft-common-actions release.yml `dry-run`). Finds release-only breakage before release day.
schedule:
- cron: "28 2 * * *"
push:
branches: [release]
workflow_dispatch:
@@ -30,6 +34,7 @@ jobs:
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
uses: storytold/craft-common-actions/.github/workflows/release.yml@main
with:
dry-run: ${{ github.event_name == 'schedule' }}
app-name: photocraft
app-title: PhotoCraft
craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83
+3 -1
View File
@@ -118,7 +118,7 @@ People trust PhotoCraft with their work, and a crash loses it. A malformed file,
3. **Clean-room.** We studied Photoshop and other proprietary editors for *behaviour and look only*. Never copy their code, shaders, profiles or assets. Implement from public specs (Adobe PSD spec, ICC, ISO 32000 blend modes, papers) and observation. Third-party assets must be permissively licensed, keep their license file next to them, and get a row in `ATTRIBUTION.md` (path, title, author, source, license) in the same change; so do original assets. The ArtCraft logos in `docs/brand/` are not open source (`docs/brand/LICENSE-brand.txt`). **Never commit font files:** fonts live in [storytold/craft-fonts](https://github.com/storytold/craft-fonts). PhotoCraft uses it through the optional build input `CRAFT_FONTS_DIR=<craft-fonts checkout>` (read by `crates/text/build.rs`, exposed as `photocraft_text::CRAFT_FONTS`; empty without it, and everything must still work). Build with it: `CRAFT_FONTS_DIR="$PWD/../craft-fonts" cargo build`; details in `docs/development.md` › Fonts.
4. **Tests are the gate.** Every change comes with tests. Format crates use round-trip, synthetic-generator, oracle and fuzz tests. Keep the PSD corpus results and the parity floor (`crates/ui-egui/src/parity.rs`) from regressing.
5. **The UI is thin and data-driven.** UI state lives in `ui-egui/src/state.rs` (serde), so the control channel can read and drive it. Colours and radii come from `theme::Tokens`, never hard-coded.
6. **Verify UI changes visually.** Render offscreen with `cargo run -p photocraft-ui-egui --example snapshot` (no window, no focus stealing), or launch with `--control` and take `ui.screenshot`. Look at the PNG. Demo images must be public-domain art, never personal photos. When fetching assets, never put a person's name, email or other personal details in requests (User-Agent, headers, URLs); use a generic `Photocraft-dev` User-Agent.
6. **Verify UI changes visually.** Render offscreen with `cargo run -p photocraft-ui-egui --example snapshot` (no window, no focus stealing), or launch with `--control` and take `ui.screenshot`. Look at the PNG. **Screenshots that show a PR's change (before/after evidence) go in the PR description, never in the repo:** `docs/images/` holds only images the README or a doc displays (a doc's own before/after comparison included), so a PR adds an image there only together with the doc that shows it. Demo images must be public-domain art, never personal photos. When fetching assets, never put a person's name, email or other personal details in requests (User-Agent, headers, URLs); use a generic `Photocraft-dev` User-Agent.
7. **Never break wasm.** L0–L6 must `cargo check --target wasm32-unknown-unknown` (run `cargo xtask wasm`). File-system code is `cfg(not(target_arch = "wasm32"))` or goes through the platform services.
8. **Performance is a feature.** Benchmark heavy operations on a 24–36 MP image in release. Work per tile in parallel (rayon), skip empty tiles, never scan a full surface per frame (cache per revision), and record before/after timings in the dev log.
@@ -161,6 +161,8 @@ cargo xtask test-corpus # if you touched psd, io, codecs, compose, gpu, text
While iterating, run less and keep the full commands above as the final gate: `cargo test -p <crate> --lib --tests` skips the examples and doctests plain `cargo test` builds every time (ui-egui has 11 examples), and `cargo test -p <crate> --test <file>` runs one integration-test file. `cargo nextest run` runs tests in parallel across binaries; see `docs/development.md` › Faster test loops.
**MCP in tests and agent sessions: `photocraft-cli mcp --headless`.** Plain `photocraft-cli mcp` works in the open PhotoCraft window while the ArtCraft Launcher's agents switch is on, and then may start PhotoCraft on the developer's machine when it is closed. Scripts, tests and agents that want a session without a window pass `--headless` (or use `photocraft-cli serve`); a test that runs the CLI also gives it its own settings, launcher and flags folders (`PHOTOCRAFT_CONFIG_DIR`, `CRAFT_MCP_DIR`, `CRAFT_FLAGS_FILE`, as `crate::command()` in `apps/photocraft-cli/tests/it/main.rs` does). To drive the running app on purpose, use `--control` (`docs/development.md`).
**Test corpora.** Real-file corpora live in `corpus/` (gitignored, never committed), fetched at pinned commits and sha256-verified by `cargo xtask corpus --all`: our Photoshop- and GIMP-authored oracles from https://github.com/storytold/photocraft-corpus plus psd-tools, ag-psd and PngSuite from their upstreams. Pins: `xtask/src/corpus_pins.rs`. The corpus tests are opt-in (cargo feature `corpus`): plain `cargo test` skips them, and with the feature on a missing corpus fails ("run `cargo xtask corpus --all`"). `cargo xtask test-corpus` fetches and runs them all. CI always runs them (the `corpus` job, cached by pin). Never commit corpus files; new oracles go to photocraft-corpus (its `AGENTS.md`), then a pin bump here. Details: `docs/development.md` › Test corpora.
Commands must **never panic** on bad input (Rule 9): every `run` closure and the code it calls returns `Err`, not a panic, for any params or document state. New commands come with a graceful-failure test (empty/out-of-range/wrong-type params → `Err`, not a crash).
+5 -7
View File
@@ -10,13 +10,13 @@ next to them.
| Path | Title | Author | Source | License |
|---|---|---|---|---|
| `crates/codecs/src/codecs/xpm/x11_colors.rs` | X11 `rgb.txt` color-name lookup table (normalized X.Org RGB values) | X.Org Foundation and X11 contributors | <https://cgit.freedesktop.org/xorg/app/rgb/tree/rgb.txt> | MIT (X.Org app/rgb) |
| Embedded at build time from storytold/craft-i18n `locales/<code>/common.tsv` and `locales/<code>/photocraft.tsv` (revision in [`craft-i18n.rev`](craft-i18n.rev)) | UI translations (all languages) | craft-i18n and PhotoCraft contributors; per-language credits and provenance in [craft-i18n's ATTRIBUTION.md](https://github.com/storytold/craft-i18n/blob/main/ATTRIBUTION.md) | <https://github.com/storytold/craft-i18n> | MIT OR Apache-2.0; Copyright (c) 2026 ArtCraft Team and the craft-i18n contributors; notice in [NOTICE](NOTICE) |
| `epaint_default_fonts::EGUI_ICONS` (build input; deflated into `OUT_DIR/egui_fonts/egui-icons.ttf.deflate`, registered by `crates/ui-egui/src/text/egui_fonts.rs`) | egui-icons, five-glyph subset of emoji-icon-font | John Slegers | [egui fork at 6d518fc](https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts), [original](https://github.com/jslegers/emoji-icon-font); egui subset, unmodified here | MIT; Copyright (c) 2014 John Slegers; full upstream notice in [NOTICE](NOTICE) |
| `assets/fonts/Inter-Regular.ttf`, `Inter-Medium.ttf`, `Inter-SemiBold.ttf` | Inter 4.001 (UI font) | The Inter Project Authors (Rasmus Andersson) | <https://github.com/rsms/inter> | SIL OFL 1.1, [`assets/fonts/OFL-Inter.txt`](assets/fonts/OFL-Inter.txt) |
| `assets/fonts/JetBrainsMono-Regular.ttf` | JetBrains Mono 2.305 (numeric font) | The JetBrains Mono Project Authors | <https://github.com/JetBrains/JetBrainsMono> | SIL OFL 1.1, [`assets/fonts/OFL-JetBrainsMono.txt`](assets/fonts/OFL-JetBrainsMono.txt) |
| `assets/icons/*.svg` except `slice-knife.svg`, `slice-select.svg`, `font-similar.svg`, `font-variable.svg`, `sampler.svg`, `artboard.svg`, `perspective-crop.svg`, `frame-placeholder.svg`, `ellipse-dashed.svg`, `gradient.svg`, `type-mask.svg`, `type-mask-vertical.svg`, `text-warp.svg`, `clip-below.svg`, `clip-release.svg`, `layer-mask.svg`, `adjustment-layer.svg`, `brush-replace.svg`, `history-brush.svg`, `quick-selection.svg`, `eraser-background.svg`, `eraser-magic.svg`, `lasso-magnetic.svg`, `direct-select.svg`, `convert-point.svg`, `pen-add-anchor.svg`, `pen-delete-anchor.svg`, `pen-freeform.svg`, `pen-curvature.svg`, `bandage-sparkle.svg`, `painting-symmetry.svg`, `symmetry-*.svg`, `path-op-*.svg` and `marquee-{row,column}.svg`, `align-edge-*.svg` and `distribute-*.svg`, `crop-overlay-*.svg`, `vertical-align-*.svg`, `spirit-level.svg` (116 files; `star-filled.svg` is `star.svg` with a solid fill) | Lucide icons | Lucide Icons and Contributors | <https://github.com/lucide-icons/lucide> (`icons/<name>.svg`; `align-*` are now named `text-align-*` upstream) | ISC, [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) |
| `assets/icons/{check,chevron-down,chevron-left,chevron-right,chevron-up,chevrons-left,chevrons-right,circle,clock,compass,info,link,link-2,lock,minus,monitor,moon,move,navigation,plus,search,square,trash,triangle,type,x,zoom-in}.svg` | Lucide icons derived from Feather (subset of the row above) | Cole Bemis (Feather), Lucide Contributors | <https://github.com/feathericons/feather> via Lucide | MIT (Feather) and ISC (Lucide), [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) |
| `assets/icons/*.svg` except `vector-shape.svg`, `slice-knife.svg`, `slice-select.svg`, `font-similar.svg`, `font-variable.svg`, `sampler.svg`, `artboard.svg`, `perspective-crop.svg`, `frame-placeholder.svg`, `ellipse-dashed.svg`, `gradient.svg`, `type-mask.svg`, `type-mask-vertical.svg`, `text-warp.svg`, `clip-below.svg`, `clip-release.svg`, `layer-mask.svg`, `adjustment-layer.svg`, `brush-replace.svg`, `history-brush.svg`, `quick-selection.svg`, `eraser-background.svg`, `eraser-magic.svg`, `lasso-magnetic.svg`, `direct-select.svg`, `convert-point.svg`, `pen-add-anchor.svg`, `pen-delete-anchor.svg`, `pen-freeform.svg`, `pen-curvature.svg`, `bandage-sparkle.svg`, `painting-symmetry.svg`, `symmetry-*.svg`, `path-op-*.svg` and `marquee-{row,column}.svg`, `align-edge-*.svg` and `distribute-*.svg`, `crop-overlay-*.svg`, `vertical-align-*.svg`, `spirit-level.svg` (117 files; `star-filled.svg` is `star.svg` with a solid fill) | Lucide icons | Lucide Icons and Contributors | <https://github.com/lucide-icons/lucide> (`icons/<name>.svg`; `align-*` are now named `text-align-*` upstream) | ISC, [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) |
| `assets/icons/{check,chevron-down,chevron-left,chevron-right,chevron-up,chevrons-left,chevrons-right,circle,clock,compass,info,link,link-2,lock,minus,monitor,moon,move,navigation,plus,search,square,trash,triangle,type,x,zoom-in,zoom-out}.svg` | Lucide icons derived from Feather (subset of the row above) | Cole Bemis (Feather), Lucide Contributors | <https://github.com/feathericons/feather> via Lucide | MIT (Feather) and ISC (Lucide), [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) |
| `assets/icons/vector-shape.svg` | Vector path shape-layer glyph with four anchor points | @superplay2021 | Original work for PhotoCraft | MIT OR Apache-2.0 |
| `assets/icons/slice-knife.svg` | Slice tool glyph | PhotoCraft contributors | Original work | MIT OR Apache-2.0 |
| `assets/icons/slice-select.svg` | Slice Select tool glyph | PhotoCraft contributors | Original work | MIT OR Apache-2.0 |
| `assets/icons/crop-overlay-*.svg` | Crop overlay glyphs: Rule of Thirds, Grid, Diagonal, Triangle, Golden Ratio and Golden Spiral inside the crop-mark frame | PhotoCraft contributors | Original work | MIT OR Apache-2.0 |
@@ -80,14 +80,12 @@ The other built-in ICC profiles and the generated LUT looks are produced by code
|---|---|---|---|---|
| `docs/brand/` (all files) | ArtCraft name, wordmark and mark | ArtCraft Team | getartcraft.com | Not open source; trademarks of the ArtCraft Team, [`docs/brand/LICENSE-brand.txt`](docs/brand/LICENSE-brand.txt) |
| `docs/images/photocraft-*.jpg` | PhotoCraft screenshots | PhotoCraft contributors (UI) | Rendered offscreen with the `snapshot` example | MIT OR Apache-2.0 (UI); the artwork in each is public domain, listed below |
| `docs/images/preferences-apply-*.png` | Preferences before and after adding Apply (no artwork) | PhotoCraft contributors | PhotoCraft control-channel capture and offscreen `snapshot` example | MIT OR Apache-2.0 |
| `docs/images/guide-position-*-tab.png` | New Guide before and after focusing Position with Tab (no artwork) | PhotoCraft contributors | Offscreen `snapshot` example with `ui.key Tab` | MIT OR Apache-2.0 |
| `docs/images/embedded-vector-editor-en.png` | Embedded AI/PDF/EPS conversion warning and editor chooser (no artwork) | Don Omondi | Offscreen `smart_svg_ui::tests::capture_embedded_vector_conversion_prompt` with authored bytes and mock services | MIT OR Apache-2.0 |
| `docs/images/embedded-vector-tabs-linux.png` | Shared VectorCraft window after a portal editor save and ordinary document launch | Don Omondi | Local Linux GUI acceptance with authored orange rectangle/blue circle SVGs; no private artwork | MIT OR Apache-2.0 |
| `docs/images/gradient-overlay-method-*.png` | Gradient Overlay Method selector, before and after (synthetic gradient) | @oRE-o | Rendered offscreen with the `snapshot` example; no external artwork | MIT OR Apache-2.0 |
| `docs/images/preset-invalid-choice-before.png` | Gradient preset import before validation, with the collision dialog closed | @yoriswind | Offscreen PhotoCraft capture with the synthetic `Fire.grd` gradient fixture | MIT OR Apache-2.0 |
| `docs/images/preset-invalid-choice-after.png` | Gradient preset import after validation, with the collision dialog still open | @yoriswind | Offscreen PhotoCraft capture with the synthetic `Fire.grd` gradient fixture | MIT OR Apache-2.0 |
| `docs/images/cpu-pixel-alignment/*.png` | CPU canvas pixel alignment, before and after #8321 | PhotoCraft contributors | Original synthetic noise rendered with the offscreen `snapshot` example; see the adjacent README | MIT OR Apache-2.0, adjacent `LICENSE-MIT` and `LICENSE-APACHE` |
| `docs/images/clone-source/*.png` | Clone source opacity/flow, cross-document and source-edge previews, and offset readouts | PhotoCraft contributors | Original synthetic red shapes rendered offscreen with the `snapshot` example; adjacent capture scripts and README | MIT OR Apache-2.0 |
| `docs/images/multi-channel-editing/{curves,curves-choices,levels,drawing,mask-view-before,mask-view-after}.png` | Selected colour channel editing (synthetic gradient) | @hidoba | Original synthetic gradient rendered with the offscreen `snapshot` example; adjacent JSON scripts | MIT OR Apache-2.0, adjacent `LICENSE-MIT` and `LICENSE-APACHE` |
| `docs/images/multi-channel-editing/great-wave.jpg`, `rgb.png`, `cmyk.png`, `lab.png`, `gallery-channel-preview-{before,after}.png` | *The Great Wave off Kanagawa*, c. 1830–1832, and PhotoCraft channel-selection captures | Katsushika Hokusai (artwork); PhotoCraft contributors (UI) | [Wikimedia Commons](https://commons.wikimedia.org/wiki/File:Tsunami_by_hokusai_19th_century.jpg), Metropolitan Museum reproduction; adjacent JSON capture scripts | Public domain artwork (PD-Art), adjacent `LICENSE-PUBLIC-DOMAIN.txt`; UI/scripts MIT OR Apache-2.0 |
@@ -133,7 +131,7 @@ Files copied into `corpus/` by hand (tiff, exr, raw) must be MIT, BSD or CC0.
| Path | Title | Author | Source | License |
|---|---|---|---|---|
| `crates/psd/tests/fixtures/atn/*.atn` | Independently encoded ATN version-16 and version-12 parser fixtures | PhotoCraft contributors (version-12 files: @yoriswind) | Original synthetic test data; wire layout documented in `crates/psd/src/atn.rs`; provenance in the adjacent README | MIT OR Apache-2.0 |
| `crates/engine/tests/fixtures/atn/v12-commands.atn` | Independently encoded ATN version-12 parser fixture (Gaussian Blur, Levels, Select All, Make Layer; 404 bytes, sha256 `451958b686da9aab45e65ae902c86333b7149d72021c21904e3aae8e39eb77cb`), copied from the PSD crate's fixtures, which now live in craft-libs' `craft-psd` | PhotoCraft contributors (@yoriswind) | Original synthetic test data | MIT OR Apache-2.0 |
## Embedded-vector editor source
Generated
+639 -401
View File
File diff suppressed because it is too large. Load diff
+31 -14
View File
@@ -4,13 +4,19 @@ members = ["crates/*", "apps/*", "xtask"]
default-members = ["crates/*", "apps/photocraft", "apps/photocraft-cli", "xtask"]
[workspace.package]
version = "0.20.1"
version = "0.23.1"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.95"
[workspace.dependencies]
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["serde"] }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["serde"] }
# Feature flags shared with the launcher and the other Craft apps (craft-libs#1118, #1119).
craft-flags = { git = "https://github.com/storytold/craft-libs", rev = "86d6e8c5b05a4dadc35ff1c8e5d2f992c687ee97" }
# The ArtCraft Launcher's agents switch and app list, shared with the launcher, the MCP gateway and
# every Craft app (storytold/craft-ai-integrations#195). Pinned to the head of
# storytold/craft-ai-integrations#197 until it merges.
craft-ai-agents = { git = "https://github.com/storytold/craft-ai-integrations", rev = "35e0352666a117407d62230a6c6c2366f3e21a3b" }
# internal
photocraft-geom = { path = "crates/geom" }
photocraft-color = { path = "crates/color" }
@@ -18,7 +24,8 @@ photocraft-cms = { path = "crates/cms" }
photocraft-raster = { path = "crates/raster" }
photocraft-doc = { path = "crates/doc" }
photocraft-ops = { path = "crates/ops" }
photocraft-psd = { path = "crates/psd" }
# PSD/PSB and the Photoshop resource files (ABR, ASL, ACO, ATN, ...): the shared reader and writer (craft-libs#875).
craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
photocraft-codecs = { path = "crates/codecs" }
photocraft-raw = { path = "crates/raw" }
photocraft-affinity = { path = "crates/affinity" }
@@ -39,19 +46,29 @@ photocraft-tablet = { path = "crates/tablet" }
photocraft-ipp = { path = "crates/ipp" }
# external
craft-print = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" }
# DDS, DICOM, FITS, IFF/ILBM, PCX, PNM and XPM: the shared decoders and encoders (craft-libs#880).
# DPX and Cineon frames (craft-libs#887); the mapping onto images stays in crates/codecs.
craft-dpx = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
# The Targa writer (craft-libs#892).
craft-tga = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
# GIF blocks without decoding: the first frame's colour table, the frame walk (craft-libs#939).
craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
# OpenEXR channel naming and Cryptomatte IDs, manifests and streams (craft-libs#929).
craft-exr = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
craft-raster-codecs = { git = "https://github.com/storytold/craft-libs", rev = "09dcbc225b95232998498a40ca56e24716571d53" }
craft-print = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
# HEIC/HEIF: the shared decoder (replaces crates/heif; craft-libs#784).
craft-heif = { git = "https://github.com/storytold/craft-libs", rev = "081078bb1b44aeedfa5bef3d4f13d978dc77510a" }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["raster"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" }
craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["raster"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
craft-pcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "b8f7d93b00dfb8f172938f3d2d796ae62dcfe123" }
# Interface translations: lookup, fallback, CLDR plurals and language names over the craft-i18n
# catalogs that `crates/ui-egui/build.rs` embeds from `CRAFT_I18N_DIR` (storytold/craft-i18n, at
# the commit in `craft-i18n.rev`). Already in the tree through craft-ui.
craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" }
craft-i18n = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
# craft-update: app-specific pretend versions apply to startup and manual checks.
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["egui"] }
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
thiserror = "2"
serde = { version = "1", features = ["derive", "rc"] }
serde_json = "1"
@@ -151,7 +168,7 @@ opt-level = "s"
# (runs when a pipeline is created). "z" keeps the wasm well under the size gate.
[profile.wasm-release.package.photocraft-doc]
opt-level = "z"
[profile.wasm-release.package.photocraft-psd]
[profile.wasm-release.package.craft-psd]
opt-level = "z"
[profile.wasm-release.package.photocraft-format]
opt-level = "z"
@@ -211,7 +228,7 @@ egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "6d518fc7
egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" }
# craft-pdf's vendored Hayro trees: consumers patch the same craft-libs revision (crates/pdf README).
hayro = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" }
hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" }
hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" }
hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" }
hayro = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
hayro-interpret = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b" }
+6 -2
View File
@@ -221,7 +221,7 @@ Every screenshot here is the real app at work on public-domain art, rendered off
PhotoCraft's PSD support is a standalone crate written from Adobe's public specification and tested against a corpus of real-world files.
- **Faithful round trips:** opening and re-saving a document renders the same for 307 of the 309 files in the psd-tools test set and 169 of 170 in our mixed ag-psd/psd-tools set (`crates/io/tests/it/corpus.rs`; fetch the psd-tools set with `cargo xtask corpus --psd-tools`), and anything we don't model yet (raw blocks, descriptors, extras) is carried over instead of being dropped. A re-saved file is not byte-identical to its source: PhotoCraft rewrites image resources, layer records and the composite. Only the standalone `photocraft-psd` crate, parsing and writing a file without the document model, reproduces every parseable corpus file byte for byte (`crates/psd/tests/it/corpus.rs`).
- **Faithful round trips:** opening and re-saving a document renders the same for 307 of the 309 files in the psd-tools test set and 169 of 170 in our mixed ag-psd/psd-tools set (`crates/io/tests/it/corpus.rs`; fetch the psd-tools set with `cargo xtask corpus --psd-tools`), and anything we don't model yet (raw blocks, descriptors, extras) is carried over instead of being dropped. A re-saved file is not byte-identical to its source: PhotoCraft rewrites image resources, layer records and the composite. Only the standalone `craft-psd` crate, parsing and writing a file without the document model, reproduces every parseable corpus file byte for byte (`crates/psd/tests/it/corpus.rs`).
- **Pixels that match:** a composite oracle compares our render with Photoshop's own merged image, covering gradient interpolation (Classic, Perceptual and Linear), layer effects, shape strokes, clipping and fill opacity.
- **Large documents:** PSB, 16 and 32-bit files, and CMYK and Lab documents open natively.
@@ -242,10 +242,14 @@ photocraft-cli batch --actions grade.json --in ./raw --out ./graded
# Every subcommand explains itself
photocraft-cli batch --help
# Let an agent drive it over MCP (headless, or bridged to the running app)
# Let an agent drive it over MCP: the open window while agents are allowed, otherwise headless
photocraft-cli mcp
# Always without a window (scripts, tests)
photocraft-cli mcp --headless
```
To let Claude or another AI app drive the open PhotoCraft window without a terminal, turn on **ArtCraft Launcher › Settings › AI agents** and click **Add** next to the AI app (one entry for every Craft app). See [`docs/mcp.md`](docs/mcp.md). These settings are behind the Craft suite's `AI-MCP` feature flag: set `AI-MCP = true` in `ArtCraft/flags.toml`, or start PhotoCraft with `CRAFT_FLAGS=AI-MCP`.
The desktop app also offers an authenticated, loopback-only control channel (`photocraft --control`) for inspecting UI state, driving tools with pointer events, and taking offscreen screenshots. Every image in this README was rendered that way. See [`docs/control-protocol.md`](docs/control-protocol.md).
## Under the hood
+1 -1
View File
@@ -46,7 +46,7 @@ tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
serde = { workspace = true }
tempfile = "3"
# Reading back a saved PSD's merged-image flag (#8064).
photocraft-psd = { workspace = true }
craft-psd = { workspace = true }
# A real ICC profile for the --metadata test (#9466).
photocraft-cms = { workspace = true }
# Synthetic camera raws (#7789).
+51 -5
View File
@@ -270,13 +270,24 @@ pub(crate) struct CommandsArgs {
pub(crate) filter: Option<String>,
}
/// Run the MCP server on stdio (headless engine, or bridge to a running `photocraft --control <port>`)
/// Run the MCP server on stdio: in the open PhotoCraft window when the person lets agents use it
/// (ArtCraft Launcher › Settings › AI agents), otherwise headless
///
/// Headless: each automation root grants that direction of file access; an omitted root denies it.
/// Prefer absolute root paths in MCP client configurations. Bridge: set the roots on the desktop
/// app; root flags are refused. The token flags require --bridge (or PHOTOCRAFT_CONTROL_PORT).
/// The open window is found again on every call, with no port or token to configure. Without
/// the person's consent, or with --headless, it runs headless: each automation root grants that
/// direction of file access; an omitted root denies it. Prefer absolute root paths in MCP client
/// configurations. Bridge (--bridge or PHOTOCRAFT_CONTROL_PORT): a running `photocraft --control
/// <port>`; set the roots on the desktop app, root flags are refused, and the token flags require
/// it.
#[derive(Args)]
pub(crate) struct McpArgs {
/// Always work headless, without the open window
#[arg(long, conflicts_with = "when_closed")]
pub(crate) headless: bool,
/// While agents may use PhotoCraft but it is closed: start it, serve a headless session, or
/// fail (default: the app's When PhotoCraft Is Closed setting)
#[arg(long, value_name = "launch|headless|error", value_parser = when_closed)]
pub(crate) when_closed: Option<photocraft_engine::prefs::WhenAppClosed>,
/// Bridge to the PhotoCraft app listening here instead of a headless engine; a bare port means
/// 127.0.0.1:<PORT>
#[arg(long, value_name = "127.0.0.1:PORT", env = "PHOTOCRAFT_CONTROL_PORT", value_parser = bridge)]
@@ -337,6 +348,8 @@ enum ValueError {
/// Why a `--bridge` value, or a PHOTOCRAFT_CONTROL_PORT value for `mcp`, is refused.
#[error("expected a port, 0-65535 (--bridge also takes 127.0.0.1:<PORT>)")]
Bridge,
#[error("expected launch, headless or error")]
WhenClosed,
}
/// Why `parse` refuses `mcp` or `serve` options clap accepted: clap prints it as a usage error
@@ -360,13 +373,19 @@ enum ServerOptionError {
/// A token flag without the bridge or port that reads it would be ignored (#8035).
#[error("{flag} requires {transport}")]
NeedsTransport { flag: &'static str, transport: &'static str },
/// `--headless` never reaches an app.
#[error("--headless works without the app; {0} is for --bridge")]
HeadlessBridge(&'static str),
/// `--when-closed` is about the open app, which a bridge names itself.
#[error("--when-closed is for the open window; --bridge names the app itself")]
BridgeWhenClosed,
}
impl ServerOptionError {
fn into_clap(self, cmd: &mut clap::Command) -> clap::Error {
let kind = match self {
Self::BridgeVar(_) | Self::GpuVar(_) => ErrorKind::ValueValidation,
Self::BridgeRoots | Self::BridgeGpu => ErrorKind::ArgumentConflict,
Self::BridgeRoots | Self::BridgeGpu | Self::HeadlessBridge(_) | Self::BridgeWhenClosed => ErrorKind::ArgumentConflict,
Self::NeedsTransport { .. } => ErrorKind::MissingRequiredArgument,
};
cmd.error(kind, self)
@@ -394,6 +413,10 @@ fn tiff_compression(s: &str) -> Result<TiffCompression, ValueError> {
}
}
fn when_closed(s: &str) -> Result<photocraft_engine::prefs::WhenAppClosed, ValueError> {
photocraft_engine::prefs::WhenAppClosed::parse(s).ok_or(ValueError::WhenClosed)
}
fn on_off(s: &str) -> Result<bool, ValueError> {
match s {
"on" => Ok(true),
@@ -474,6 +497,11 @@ fn steps(m: &ArgMatches) -> Vec<Step> {
/// stray.psd --help`): clap reports the mistake first.
pub(crate) fn parse(args: &[OsString]) -> Result<Cli, clap::Error> {
let mut cmd = Cli::command().mut_subcommands(|sub| sub.mut_args(blank_env_is_unset));
// `mcp --headless` reaches no app: a PHOTOCRAFT_CONTROL_PORT exported for another tool is not
// read, so a value it couldn't use anyway doesn't stop it.
if args.first().is_some_and(|a| a == "mcp") && args.iter().take_while(|a| *a != "--").any(|a| a == "--headless") {
cmd = cmd.mut_subcommand("mcp", |sub| sub.mut_arg("bridge", |arg| arg.env(None::<&str>)));
}
let argv = std::iter::once(OsString::from("photocraft-cli")).chain(args.iter().cloned());
let matches = match cmd.try_get_matches_from_mut(argv) {
Err(e) if args.iter().take_while(|a| *a != "--").any(|a| *a == "--help" || *a == "-h") => {
@@ -490,6 +518,21 @@ pub(crate) fn parse(args: &[OsString]) -> Result<Cli, clap::Error> {
check_gpu_env(&mut cmd, m)?;
}
(Command::Mcp(a), Some((name, m))) => {
if a.headless {
// `--headless` reaches no app. Given on the command line, the bridge's flags
// conflict; exported for another tool, they are left alone.
for (id, flag) in [("bridge", "--bridge"), ("control_token", "--control-token"), ("control_token_file", "--control-token-file")] {
if m.value_source(id) == Some(ValueSource::CommandLine) {
if let Some(sub) = cmd.find_subcommand_mut(name) {
return Err(ServerOptionError::HeadlessBridge(flag).into_clap(sub));
}
return Err(ServerOptionError::HeadlessBridge(flag).into_clap(&mut cmd));
}
}
a.bridge = None;
check_gpu_env(&mut cmd, m)?;
return Ok(cli);
}
a.bridge_from_env = from_env(m, "bridge");
if a.bridge_from_env
&& let Some(Bridge::Addr(v)) = &a.bridge
@@ -516,6 +559,9 @@ pub(crate) fn parse(args: &[OsString]) -> Result<Cli, clap::Error> {
return Err(ServerOptionError::BridgeGpu.into_clap(sub));
}
check_server(sub, m, a.bridge.is_some(), "--bridge")?;
if a.bridge.is_some() && a.when_closed.is_some() {
return Err(ServerOptionError::BridgeWhenClosed.into_clap(sub));
}
}
if a.bridge.is_none() {
check_gpu_env(&mut cmd, m)?;
+31
View File
@@ -68,8 +68,12 @@ pub(crate) fn serve(a: &ServeArgs, err: &mut dyn Write) -> Result {
}
}
/// `mcp`: with the suite's `AI-MCP` flag, the open window when agents may use it, otherwise
/// headless (`mcp_live`); without the flag headless, as before it; `--headless` always headless;
/// `--bridge` (or PHOTOCRAFT_CONTROL_PORT) a `photocraft --control` app.
pub(crate) fn mcp(a: &McpArgs, err: &mut dyn Write) -> Result {
let server = match &a.bridge {
None if !a.headless && photocraft_automation::agent_link::mcp_flag() => PhotocraftMcp::live(live_config(a)?),
Some(addr) => {
if a.bridge_from_env {
// Name the mode switch, as `serve` does: the way back to headless is to unset it.
@@ -94,3 +98,30 @@ pub(crate) fn mcp(a: &McpArgs, err: &mut dyn Write) -> Result {
let rt = tokio::runtime::Builder::new_multi_thread().enable_all().build()?;
Ok(rt.block_on(server.serve_stdio())?)
}
/// `mcp`: the app's settings folder, the closed-app override, the app launcher, and the headless
/// session's roots and rendering for when it works without the window.
fn live_config(a: &McpArgs) -> Result<photocraft_automation::live::LiveConfig> {
let other = |m: &str| crate::CliError::Other(m.to_string());
let dir = photocraft_automation::app_dirs::from_env()
.dir
.ok_or_else(|| other("can't find PhotoCraft's settings folder (no home folder; set PHOTOCRAFT_CONFIG_DIR)"))?;
let mut config = photocraft_automation::live::LiveConfig::new(dir);
config.when_closed = a.when_closed;
// As a headless `mcp` always did: folders that can't be opened stop the start, before the
// server answers anything, rather than at the first call that would fall back to them.
a.automation.workspace()?;
config.read_roots.clone_from(&a.automation.automation_read_root);
config.write_root = a.automation.automation_write_root.clone();
config.launch = std::env::current_exe().ok().and_then(|exe| photocraft_automation::live::launcher(&exe));
let policy = crate::gpu::GpuPolicy::from_args(&a.gpu).map_err(crate::CliError::Other)?;
config.prepare_headless = Some(Box::new(move |h| {
policy.apply_to_headless(h);
// stdout carries the protocol; the warnings a headless start has go to stderr.
let mut err = std::io::stderr();
for d in h.drain_diagnostics() {
let _ = writeln!(err, "warning: {d}");
}
}));
Ok(config)
}
+95 -3
View File
@@ -8,7 +8,7 @@ use serde_json::{Value, json};
/// The binary, without the variables `mcp` and `serve` fall back to: a test that wants one sets it.
fn bin() -> Command {
let mut cmd = Command::new(crate::binary());
let mut cmd = crate::command();
for var in ENV_VARS {
cmd.env_remove(var);
}
@@ -879,7 +879,7 @@ fn no_maximize_compatibility_writes_the_psd_placeholder() {
{
let out = d.join(format!("a.{ext}"));
ok(bin().arg("run").arg(&a).args(["--cmd", "layer.new.layer"]).arg("--out").arg(&out).args(args));
let psd = photocraft_psd::PsdFile::from_bytes(&std::fs::read(&out).unwrap()).unwrap();
let psd = craft_psd::PsdFile::from_bytes(&std::fs::read(&out).unwrap()).unwrap();
assert_eq!(psd.has_real_merged_data(), Some(real), "{args:?} {ext}");
}
std::fs::remove_dir_all(d).unwrap();
@@ -1442,7 +1442,7 @@ fn batch_plays_a_photoshop_actions_file() {
fn extensionless_version_12_actions_file_is_read_as_atn() {
let d = tmp("atn-v12");
let path = d.join("Legacy Actions");
std::fs::write(&path, include_bytes!("../../../../crates/psd/tests/fixtures/atn/v12-commands.atn")).unwrap();
std::fs::write(&path, include_bytes!("../../../../crates/engine/tests/fixtures/atn/v12-commands.atn")).unwrap();
let (steps, set, warnings) = photocraft_cli::load_actions_file(&path, None).unwrap();
assert_eq!(set.len(), 1);
assert_eq!(steps, vec![("actions.play".to_string(), json!({"action": set[0].name}))]);
@@ -2029,3 +2029,95 @@ fn info_detail_adds_text_runs_and_antialiasing() {
assert!(text["runs"].as_array().is_some_and(|r| !r.is_empty()), "{text}");
let _ = std::fs::remove_dir_all(&dir);
}
/// Flags that don't go together are usage errors before anything starts. Adding PhotoCraft to an
/// MCP client is the ArtCraft Launcher's job, so there is no `mcp install`, and plain `mcp` does
/// what `--live` did, so there is no `--live` either.
#[test]
fn mcp_rejects_mismatched_flags() {
for (args, needle) in [
(&["mcp", "install"][..], "install"),
(&["mcp", "--live"], "--live"),
(&["mcp", "--client", "json"], "--client"),
(&["mcp", "--headless", "--bridge", "127.0.0.1:1"], "--bridge"),
(&["mcp", "--headless", "--control-token-file", "t"], "--control-token-file"),
(&["mcp", "--headless", "--when-closed", "launch"], "--when-closed"),
(&["mcp", "--when-closed", "sometimes"], "sometimes"),
(&["mcp", "--bridge", "127.0.0.1:1", "--when-closed", "error"], "--when-closed"),
] {
let o = bin().args(args).stdin(Stdio::null()).output().unwrap();
let err = String::from_utf8_lossy(&o.stderr);
assert_eq!(o.status.code(), Some(2), "{args:?}: {err}");
assert!(err.contains(needle), "{args:?}: {err}");
}
}
/// One MCP exchange with `photocraft-cli mcp <args>`: initialize, then each tool call; returns the
/// tool results.
fn mcp_session(args: &[&str], env: &[(&str, &std::ffi::OsStr)], calls: &[(&str, Value)]) -> Vec<Value> {
let mut cmd = bin();
cmd.args(args).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null());
for (k, v) in env {
cmd.env(k, v);
}
let mut child = cmd.spawn().unwrap();
let mut stdin = child.stdin.take().unwrap();
let mut stdout = BufReader::new(child.stdout.take().unwrap());
let mut send = |v: Value| {
writeln!(stdin, "{v}").unwrap();
stdin.flush().unwrap();
};
let mut recv = |id: u64| -> Value {
loop {
let mut line = String::new();
assert!(stdout.read_line(&mut line).unwrap() > 0, "server closed");
let v: Value = serde_json::from_str(&line).unwrap();
if v["id"] == id {
return v;
}
}
};
send(json!({"jsonrpc":"2.0","id":1,"method":"initialize","params":{
"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"0"}}}));
recv(1);
send(json!({"jsonrpc":"2.0","method":"notifications/initialized"}));
let mut out = Vec::new();
for (i, (name, arguments)) in calls.iter().enumerate() {
let id = 2 + i as u64;
send(json!({"jsonrpc":"2.0","id":id,"method":"tools/call","params":{"name":name,"arguments":arguments}}));
out.push(recv(id)["result"].clone());
}
drop(stdin);
let _ = child.kill();
let _ = child.wait();
out
}
/// Plain `mcp` without the person's consent works headless, as it always did: commands run, UI
/// tools say they need the open app. `--headless` too, and it doesn't read a PHOTOCRAFT_CONTROL_PORT
/// exported for another tool (here not even a port).
#[test]
fn mcp_without_consent_works_headless() {
let dir = tmp("mcp-plain");
let empty = tmp("mcp-plain-launcher");
// `crate::command()` keeps the machine's launcher and flags out already; this test names its
// own settings and an empty launcher folder as well, to say what it relies on.
let env = [("PHOTOCRAFT_CONFIG_DIR", dir.as_os_str()), ("CRAFT_MCP_DIR", empty.as_os_str())];
let calls = [("command_list", json!({})), ("ui_inspect", json!({}))];
let r = mcp_session(&["mcp", "--when-closed", "error"], &env, &calls);
assert_ne!(r[0]["isError"], true, "headless commands run: {}", r[0]);
assert_eq!(r[1]["isError"], true, "{}", r[1]);
assert!(r[1]["content"][0]["text"].as_str().unwrap().contains("bridge"), "{}", r[1]);
let mut env = env.to_vec();
env.push(("PHOTOCRAFT_CONTROL_PORT", std::ffi::OsStr::new("not-a-port")));
let r = mcp_session(&["mcp", "--headless"], &env, &calls[..1]);
assert_ne!(r[0]["isError"], true, "{}", r[0]);
// Without the `AI-MCP` flag a Control Port saved before the agents switch (which the app
// reads as "agents may connect") still leaves plain `mcp` headless: it neither looks for
// the app nor applies When PhotoCraft Is Closed ("error" would fail this call).
std::fs::write(dir.join("preferences.json"), r#"{"integrations": {"controlPort": 7878}}"#).unwrap();
let r = mcp_session(&["mcp", "--when-closed", "error"], &env[..2], &calls[..1]);
assert_ne!(r[0]["isError"], true, "without the flag plain mcp stays headless: {}", r[0]);
let _ = std::fs::remove_dir_all(&dir);
let _ = std::fs::remove_dir_all(&empty);
}
+1 -1
View File
@@ -6,7 +6,7 @@ use std::process::Command;
use serde_json::Value;
fn bin() -> Command {
let mut cmd = Command::new(crate::binary());
let mut cmd = crate::command();
cmd.env_remove("PHOTOCRAFT_CONTROL_PORT");
cmd.env_remove("PHOTOCRAFT_CONTROL_TOKEN");
cmd.env_remove("PHOTOCRAFT_CONTROL_TOKEN_FILE");
+16
View File
@@ -13,3 +13,19 @@ mod psd_write_oracle;
fn binary() -> std::ffi::OsString {
std::env::var_os("NEXTEST_BIN_EXE_photocraft_cli").unwrap_or_else(|| env!("CARGO_BIN_EXE_photocraft-cli").into())
}
/// The CLI, kept away from this machine's own PhotoCraft settings, ArtCraft Launcher and feature
/// flags: plain `mcp` works in the open app when the launcher's switch lets agents use it, and
/// would otherwise find (or start) the developer's own PhotoCraft. A test that needs a settings
/// folder of its own sets `PHOTOCRAFT_CONFIG_DIR` again; the last value wins.
fn command() -> std::process::Command {
let base = std::env::temp_dir().join(format!("pc-cli-it-isolated-{}", std::process::id()));
let settings = base.join("settings");
let _ = std::fs::create_dir_all(&settings);
let mut cmd = std::process::Command::new(binary());
cmd.env("PHOTOCRAFT_CONFIG_DIR", &settings)
.env("CRAFT_MCP_DIR", base.join("launcher"))
.env("CRAFT_FLAGS_FILE", base.join("flags.toml"))
.env_remove("CRAFT_FLAGS");
cmd
}
@@ -2,13 +2,13 @@
//! and a value the user set.
use std::io::{BufRead, BufReader, Write};
use std::process::{Command, Stdio};
use std::process::Stdio;
use photocraft_engine::malloc_tuning::{TRIM_THRESHOLD, TRIM_THRESHOLD_VAR};
/// The environment of a running `serve` once it answered a request, as `NAME=value` entries.
fn serve_environ(set: Option<&str>) -> Vec<String> {
let mut cmd = Command::new(crate::binary());
let mut cmd = crate::command();
for var in [TRIM_THRESHOLD_VAR, "MALLOC_MMAP_THRESHOLD_", "MALLOC_TOP_PAD_", "MALLOC_MMAP_MAX_", "MALLOC_ARENA_MAX", "GLIBC_TUNABLES"] {
cmd.env_remove(var);
}
+1 -1
View File
@@ -10,7 +10,7 @@ use photocraft_codecs::{ChannelLayout, DecodeOptions, EncodeOptions, Format, Ima
use serde_json::Value;
fn bin() -> Command {
Command::new(crate::binary())
crate::command()
}
fn tmp(name: &str) -> PathBuf {
+1 -1
View File
@@ -40,4 +40,4 @@ web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlE
[dev-dependencies]
serde_json = { workspace = true }
photocraft-paint = { workspace = true }
photocraft-psd = { workspace = true }
craft-psd = { workspace = true }
+2 -2
View File
@@ -181,7 +181,7 @@ impl PresetBackend for Bridge {
#[cfg(test)]
mod tests {
use super::*;
use photocraft_psd::abr::{AbrSample, LegacyBrush, LegacyTip, write_v12};
use craft_psd::abr::{AbrSample, LegacyBrush, LegacyTip, write_v12};
use serde_json::json;
fn import(s: &mut Session) {
@@ -367,7 +367,7 @@ mod tests {
}
#[test]
fn indexed_brush_store_coexists_with_imported_gradient_preferences() {
use photocraft_psd::grd::{GrdColor, GrdGradient, GrdStop, write};
use craft_psd::grd::{GrdColor, GrdGradient, GrdStop, write};
let b = Bridge::default();
let (mut s, _) = session(b.clone());
import(&mut s);
+1
View File
@@ -27,6 +27,7 @@ photocraft-io = { workspace = true }
photocraft-format = { workspace = true }
photocraft-automation = { workspace = true }
photocraft-ui-egui = { workspace = true }
craft-flags = { workspace = true }
# Pen tablet pressure/tilt on macOS and X11 (the workspace's one isolated unsafe helper crate).
photocraft-tablet = { workspace = true }
eframe = { workspace = true, features = ["wgpu", "accesskit", "wayland", "x11", "persistence", "clipboard"] }
+281
View File
@@ -0,0 +1,281 @@
//! Agents in the open desktop app: while the person lets them in (ArtCraft Launcher › Settings ›
//! AI agents; without the `AI-MCP` flag, a Control Port saved before), the app serves the control
//! protocol by itself (no `--control`) and publishes the connection file `photocraft-cli mcp`
//! finds it by ([`photocraft_automation::agent_link`]). Turning it on or off, or changing the
//! port or folders in Preferences › AI Integrations, applies at once. The token stays in the settings
//! folder across launches (`agent-control.token`, as `--control-token-file` would keep it; delete
//! it for a new one). Agent requests get the policy and the folders chosen there, exactly as with
//! `--control` and the automation roots.
//!
//! Launch flags win: started with `--control`, the app doesn't install this service and the
//! dialog says the launch options manage the connection.
use std::path::{Path, PathBuf};
use std::sync::mpsc::Receiver;
use photocraft_automation::agent_link::{self, Connection};
use photocraft_automation::security;
use photocraft_engine::prefs::Integrations;
use photocraft_ui_egui::{AgentLink, AgentLinkFn, ControlRequest};
use crate::control_server;
/// The connection while it runs; dropping it stops the server and removes the connection file.
struct Running {
server: control_server::Listening,
dir: PathBuf,
/// The `controlPort` preference it was started for (0 = any).
port_pref: u32,
}
impl Drop for Running {
fn drop(&mut self) {
agent_link::remove_connection(&self.dir, std::process::id());
log::info!("agent connection on 127.0.0.1:{} stopped", self.server.port);
}
}
/// Start serving agents on `port` (0 = any free port): the token from the settings folder
/// (created `0600` when missing), then the connection file. Returns the connection and the
/// channel its requests arrive on. A token file that can't be written leaves the connection off
/// (no server, so no token to leak) and says why.
fn start(dir: &Path, port: u16, ctx: &egui::Context, port_pref: u32) -> Result<(Running, Receiver<ControlRequest>), String> {
let token_file = dir.join(agent_link::TOKEN_FILE);
let token = security::server_token(None, Some(&token_file)).map_err(|e| format!("Agents can't connect: the token file could not be written: {e}"))?;
let (requests, server) = control_server::listen(port, token, ctx.clone()).map_err(|e| format!("Couldn't listen on 127.0.0.1:{port}: {e}"))?;
let running = Running { server, dir: dir.to_path_buf(), port_pref };
agent_link::write_connection(dir, &Connection::new(running.server.port, token_file)).map_err(|e| format!("Couldn't publish the agent connection: {e}"))?;
log::info!("agents can connect on 127.0.0.1:{}", running.server.port);
Ok((running, requests))
}
/// The agent connection service (`settings_dir`: where the connection file and token go).
pub fn service(settings_dir: Option<PathBuf>) -> AgentLinkFn {
let mut running: Option<Running> = None;
Box::new(move |integrations: &Integrations, ctx: &egui::Context| apply(&mut running, settings_dir.as_deref(), integrations, ctx))
}
/// Bring the connection in line with `integrations`: start, restart on another port, stop, or only
/// change the folders agents may use.
fn apply(running: &mut Option<Running>, dir: Option<&Path>, integrations: &Integrations, ctx: &egui::Context) -> AgentLink {
let mut out = AgentLink::default();
if !integrations.connect_agents {
out.stopped = running.take().is_some();
return out;
}
// Allow Agent Control off refuses every control server, this one as `--control`.
if !integrations.allow_agent_control {
out.stopped = running.take().is_some();
out.status.error = Some("Agents can't connect: Allow Agent Control is off in the preferences file.".into());
return out;
}
let Some(dir) = dir else {
out.stopped = running.take().is_some();
out.status.error = Some("Agents can't connect: PhotoCraft has no settings folder (no home folder).".into());
return out;
};
if running.as_ref().is_none_or(|r| r.port_pref != integrations.control_port) {
out.stopped = running.take().is_some();
// The preference's range is 0..=65535; anything else picks a free port.
let port = u16::try_from(integrations.control_port).unwrap_or(0);
match start(dir, port, ctx, integrations.control_port) {
Ok((r, requests)) => {
out.requests = Some(requests);
*running = Some(r);
}
Err(e) => {
log::warn!("{e}");
out.status.error = Some(e);
return out;
}
}
}
out.status.port = running.as_ref().map(|r| r.server.port);
// Agent requests always go through the automation policy; without folders they get no file
// access at all (as `--control` without automation roots). A folder that can't be opened, or
// isn't an absolute path, is left out (that direction fails closed) and named, as at launch.
let no_flags = photocraft_automation::args::AutomationArgs { automation_read_root: Vec::new(), automation_write_root: None };
let workspace = match crate::automation_workspace(&no_flags, integrations) {
Ok((workspace, warning)) => {
if let Some(w) = warning {
out.status.error = Some(format!("Agents can connect, but without file access there: {w}"));
}
workspace
}
Err(e) => {
out.status.error = Some(format!("Agents can connect, but without file access: {e}"));
photocraft_automation::AuthorizedWorkspace::default()
}
};
out.automation = crate::services::automation(Some(workspace));
out
}
/// How often [`launcher_watch`] looks at the ArtCraft Launcher's files.
const LAUNCHER_POLL: std::time::Duration = std::time::Duration::from_secs(2);
/// Whether the ArtCraft Launcher turned agents on for every Craft app
/// ([`agent_link::launcher_allows_agents`]), watched on its own thread so the UI can ask every
/// frame without touching the disk. A change wakes the UI, which then starts or stops the
/// connection. Without a thread (one couldn't be started) the answer stays what it was at launch.
pub fn launcher_watch(ctx: &egui::Context) -> Box<dyn Fn() -> bool> {
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
let on = Arc::new(AtomicBool::new(agent_link::launcher_allows_agents()));
let (seen, ctx) = (on.clone(), ctx.clone());
let started = std::thread::Builder::new().name("launcher-agents".into()).spawn(move || {
loop {
std::thread::sleep(LAUNCHER_POLL);
let now = agent_link::launcher_allows_agents();
if seen.swap(now, Ordering::Relaxed) != now {
log::info!("ArtCraft Launcher: agents {}", if now { "on" } else { "off" });
ctx.request_repaint();
}
}
});
if let Err(e) = started {
log::warn!("couldn't watch the ArtCraft Launcher's agents switch: {e}");
}
Box::new(move || on.load(Ordering::Relaxed))
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
fn temp() -> PathBuf {
static N: AtomicUsize = AtomicUsize::new(0);
let d = std::env::temp_dir().join(format!("photocraft-agent-service-{}-{}", std::process::id(), N.fetch_add(1, Ordering::Relaxed)));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
d
}
fn on(port: u32) -> Integrations {
Integrations { connect_agents: true, control_port: port, ..Default::default() }
}
#[test]
fn turning_agents_on_publishes_a_reachable_connection_and_off_removes_it() {
let dir = temp();
let ctx = egui::Context::default();
let mut link = service(Some(dir.clone()));
// Off (the default): nothing listens, nothing is published.
let off = link(&Integrations::default(), &ctx);
assert!(off.requests.is_none() && !off.stopped && off.status.port.is_none());
assert!(off.automation.command.is_none(), "no agent policy without a connection");
assert_eq!(agent_link::read_connection(&dir).unwrap(), None);
let started = link(&on(0), &ctx);
let port = started.status.port.unwrap();
assert!(started.requests.is_some());
assert!(started.automation.command.is_some() && started.automation.authorize.is_some(), "agent requests go through the policy");
assert!(started.automation.read.is_some(), "the workspace answers (and refuses: no folder chosen)");
let c = agent_link::read_connection(&dir).unwrap().unwrap();
assert_eq!((c.port, c.pid), (port, std::process::id()));
assert!(agent_link::probe(port, agent_link::PROBE_TIMEOUT));
let token = security::client_token(None, Some(&c.token_file)).unwrap();
// Folder changes keep the server and its token.
let folders = Integrations { read_root: dir.to_string_lossy().into_owned(), ..on(0) };
let changed = link(&folders, &ctx);
assert!(changed.requests.is_none() && !changed.stopped);
assert_eq!(changed.status.port, Some(port));
assert!(changed.status.error.is_none(), "{:?}", changed.status.error);
assert_eq!(security::client_token(None, Some(&c.token_file)).unwrap(), token);
// A missing folder still connects, without file access, and says why.
let missing = Integrations { write_root: dir.join("nope").to_string_lossy().into_owned(), ..on(0) };
let partial = link(&missing, &ctx);
assert_eq!(partial.status.port, Some(port));
assert!(partial.status.error.as_deref().is_some_and(|e| e.contains("without file access")), "{:?}", partial.status.error);
assert!(partial.automation.command.is_some());
let stopped = link(&Integrations::default(), &ctx);
assert!(stopped.stopped && stopped.status.port.is_none());
assert_eq!(agent_link::read_connection(&dir).unwrap(), None, "the connection file goes with the server");
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
while agent_link::probe(port, agent_link::PROBE_TIMEOUT) {
assert!(std::time::Instant::now() < deadline, "port {port} still open");
std::thread::sleep(std::time::Duration::from_millis(20));
}
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_new_port_restarts_with_the_same_token_and_a_taken_port_is_reported() {
let dir = temp();
let ctx = egui::Context::default();
let mut link = service(Some(dir.clone()));
let first = link(&on(0), &ctx);
let first_port = first.status.port.unwrap();
let token_file = dir.join(agent_link::TOKEN_FILE);
let first_token = std::fs::read_to_string(&token_file).unwrap();
// A taken port: the error names it and nothing is published for it.
let taken = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let taken_port = taken.local_addr().unwrap().port();
let failed = link(&on(u32::from(taken_port)), &ctx);
assert!(failed.stopped && failed.requests.is_none() && failed.status.port.is_none());
assert!(failed.status.error.as_deref().is_some_and(|e| e.contains(&taken_port.to_string())), "{:?}", failed.status.error);
assert_eq!(agent_link::read_connection(&dir).unwrap(), None);
drop(taken);
// Back to any port: the token in the settings folder is kept, so a client holding it
// (a `--bridge` setup reading the token file) needn't be set up again.
let again = link(&on(0), &ctx);
assert!(again.requests.is_some());
assert!(again.status.port.is_some_and(|p| p != 0));
assert_eq!(std::fs::read_to_string(&token_file).unwrap(), first_token);
let _ = first_port;
drop(link);
assert_eq!(agent_link::read_connection(&dir).unwrap(), None, "quitting removes the connection file");
let _ = std::fs::remove_dir_all(&dir);
}
/// A token file that can't be written (here the "settings folder" is a file) leaves the
/// connection off with the reason shown, and its port free.
#[test]
fn a_token_file_that_cannot_be_written_leaves_agents_off() {
let dir = temp();
let settings = dir.join("not-a-folder");
std::fs::write(&settings, b"").unwrap();
let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
let mut link = service(Some(settings));
let r = link(&on(u32::from(port)), &egui::Context::default());
assert!(r.requests.is_none() && r.status.port.is_none());
assert!(r.status.error.as_deref().is_some_and(|e| e.contains("token file")), "{:?}", r.status.error);
std::net::TcpListener::bind(("127.0.0.1", port)).expect("the port was let go");
let _ = std::fs::remove_dir_all(&dir);
}
/// A relative folder would depend on the folder the app was started from: left out and named.
#[test]
fn a_relative_folder_is_left_out_with_a_reason() {
let dir = temp();
let mut link = service(Some(dir.clone()));
let r = link(&Integrations { read_root: ".".into(), ..on(0) }, &egui::Context::default());
assert!(r.status.port.is_some());
assert!(r.status.error.as_deref().is_some_and(|e| e.contains("not an absolute path")), "{:?}", r.status.error);
drop(link);
let _ = std::fs::remove_dir_all(&dir);
}
/// Allow Agent Control off (in the file; the dialog doesn't show it) refuses this server too.
#[test]
fn allow_agent_control_off_keeps_agents_out() {
let dir = temp();
let mut link = service(Some(dir.clone()));
let r = link(&Integrations { allow_agent_control: false, ..on(0) }, &egui::Context::default());
assert!(r.requests.is_none() && r.status.port.is_none() && r.automation.command.is_none());
assert!(r.status.error.as_deref().is_some_and(|e| e.contains("Allow Agent Control")), "{:?}", r.status.error);
assert_eq!(agent_link::read_connection(&dir).unwrap(), None);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn no_settings_folder_is_an_error_not_a_panic() {
let mut link = service(None);
let r = link(&on(0), &egui::Context::default());
assert!(r.requests.is_none() && r.status.error.is_some());
}
}
+8 -90
View File
@@ -10,6 +10,9 @@
//! 3. The platform convention: macOS `~/Library/Application Support/Photocraft`, Windows
//! `%APPDATA%\Photocraft`, Linux `$XDG_CONFIG_HOME/photocraft` or `~/.config/photocraft`.
//!
//! The rules live in [`photocraft_automation::app_dirs`], so `photocraft-cli` resolves the same
//! folder; this module resolves it once for the app and logs the outcome.
//!
//! One exception: the platform convention keeps crash-recovery autosaves out of what is synced
//! with the settings: the Windows roaming profile and the Linux config folder ([`recovery_dir`]).
@@ -17,31 +20,11 @@ use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
/// Files beside the executable that switch on portable mode (either one; contents are ignored).
pub const PORTABLE_MARKERS: [&str; 2] = ["portable.txt", "PhotoCraft.portable"];
/// The data folder created beside the executable in portable mode.
pub const PORTABLE_DATA_DIR: &str = "PhotoCraftData";
/// How [`DataDir::dir`] was chosen.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Mode {
/// `PHOTOCRAFT_CONFIG_DIR`.
Override,
/// `<exe dir>/PhotoCraftData`.
Portable,
/// The platform's per-user config directory.
Platform,
}
/// The resolved data directory.
#[derive(Clone, Debug)]
pub struct DataDir {
/// `None` only when no platform directory is known either (no `HOME`/`APPDATA`).
pub dir: Option<PathBuf>,
pub mode: Mode,
/// Set when a portable marker was found but its data folder isn't writable.
pub warning: Option<String>,
}
pub use photocraft_automation::app_dirs::{DataDir, Mode, resolve};
// The tests below exercise the shared resolver too; some of them run on Unix only.
#[cfg(test)]
#[allow(unused_imports)]
use photocraft_automation::app_dirs::{PORTABLE_DATA_DIR, PORTABLE_MARKERS, WRITE_PROBE_ATTEMPTS, ensure_writable, platform_dir, portable_marker};
/// The data directory for this process, resolved once (the portable check probes the disk).
pub fn current() -> &'static DataDir {
@@ -453,70 +436,5 @@ fn copy_file(from: &Path, to: &Path) -> std::io::Result<()> {
dst.sync_all()
}
/// Resolve the data directory from the environment (`env`) and the executable's folder.
pub fn resolve(env: impl Fn(&str) -> Option<OsString>, exe_dir: Option<&Path>) -> DataDir {
if let Some(d) = env("PHOTOCRAFT_CONFIG_DIR").filter(|d| !d.is_empty()) {
return DataDir { dir: Some(PathBuf::from(d)), mode: Mode::Override, warning: None };
}
let platform = platform_dir(&env);
let Some((exe_dir, marker)) = exe_dir.and_then(|d| portable_marker(d).map(|m| (d, m))) else {
return DataDir { dir: platform, mode: Mode::Platform, warning: None };
};
let data = exe_dir.join(PORTABLE_DATA_DIR);
match ensure_writable(&data) {
Ok(()) => DataDir { dir: Some(data), mode: Mode::Portable, warning: None },
Err(e) => {
let fallback = platform.as_ref().map_or_else(|| "nowhere (no user folder found)".to_string(), |p| p.display().to_string());
let warning =
format!("portable mode ({} found) but {} isn't writable ({e}); settings are kept in {fallback} instead", marker.display(), data.display());
DataDir { dir: platform, mode: Mode::Platform, warning: Some(warning) }
}
}
}
/// The platform convention, from `env` (so tests can fake `HOME`/`APPDATA`/`XDG_CONFIG_HOME`).
pub fn platform_dir(env: &impl Fn(&str) -> Option<OsString>) -> Option<PathBuf> {
let home = env("HOME").filter(|h| !h.is_empty()).map(PathBuf::from);
if cfg!(target_os = "macos") {
return home.map(|h| h.join("Library/Application Support/Photocraft"));
}
if cfg!(windows) {
return env("APPDATA").filter(|a| !a.is_empty()).map(|a| PathBuf::from(a).join("Photocraft"));
}
env("XDG_CONFIG_HOME").filter(|x| !x.is_empty()).map(PathBuf::from).or_else(|| home.map(|h| h.join(".config"))).map(|c| c.join("photocraft"))
}
/// The first portable marker file in `exe_dir`, if any.
pub fn portable_marker(exe_dir: &Path) -> Option<PathBuf> {
PORTABLE_MARKERS.iter().map(|m| exe_dir.join(m)).find(|p| p.is_file())
}
/// Probe names tried before giving up on a portable data folder.
const WRITE_PROBE_ATTEMPTS: u32 = 64;
/// Create `dir` and prove a file can be written in it (a folder can exist yet be read-only).
///
/// The probe is created exclusively (`create_new`), so an entry already at its name (a link
/// someone else placed there, say) is never opened, written through or removed: the next name
/// is tried instead. Only a probe this call created is removed.
fn ensure_writable(dir: &Path) -> std::io::Result<()> {
std::fs::create_dir_all(dir)?;
let pid = std::process::id();
for attempt in 0..WRITE_PROBE_ATTEMPTS {
let probe = dir.join(format!(".write-test-{pid}-{attempt}"));
match std::fs::OpenOptions::new().write(true).create_new(true).open(&probe) {
Ok(file) => {
drop(file);
// A leftover probe is harmless; don't fail portable mode over it.
let _ = std::fs::remove_file(&probe);
return Ok(());
}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(e) => return Err(e),
}
}
Err(std::io::Error::new(std::io::ErrorKind::AlreadyExists, "every write-test name is already taken"))
}
#[cfg(test)]
mod tests;
+93 -19
View File
@@ -1,9 +1,13 @@
//! Localhost JSON-lines control server (one request per line, one reply per line).
//! Loopback only. This is the transport the MCP server will wrap.
//!
//! `--control` starts it for the whole run ([`start`]); Preferences › AI Integrations starts and
//! stops it while the app runs ([`listen`], see `agent_link`).
use std::io::{BufReader, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{Receiver, Sender, channel};
use std::time::{Duration, Instant};
@@ -16,32 +20,74 @@ use photocraft_ui_egui::ControlRequest;
use photocraft_ui_egui::control::REPLY_DEADLINE;
use serde_json::{Value, json};
/// Start serving a listener already bound during startup, before a window is opened.
/// Start serving a listener already bound during startup, before a window is opened. It serves
/// for the whole run.
pub fn start(listener: TcpListener, token: String, ctx: egui::Context) -> Receiver<ControlRequest> {
let (tx, rx) = channel::<ControlRequest>();
let port = listener.local_addr().map_or(0, |a| a.port());
eprintln!("photocraft: control server listening on {}", listening_addr(&listener, port));
std::thread::spawn(move || {
let limiter = ConnectionLimiter::new(MAX_CONNECTIONS);
let token = Arc::new(token);
for mut stream in listener.incoming().flatten() {
let Some(permit) = limiter.try_acquire() else {
let _ = configure_stream(&stream);
let _ = writeln!(stream, "{}", json!({"id": null, "ok": false, "error": "connection limit reached"}));
continue;
};
let tx = tx.clone();
let ctx = ctx.clone();
let token = Arc::clone(&token);
std::thread::spawn(move || {
let _permit = permit;
serve(stream, &token, tx, ctx);
});
}
});
std::thread::spawn(move || accept(listener, token, tx, ctx, None));
rx
}
/// A control server started by [`listen`]. Dropping it stops accepting connections; connections
/// already open end at their next request, once the app drops the request channel.
pub struct Listening {
/// The bound port (the one the system picked for port 0).
pub port: u16,
stop: Arc<AtomicBool>,
accept: Option<std::thread::JoinHandle<()>>,
}
impl Drop for Listening {
fn drop(&mut self) {
self.stop.store(true, Ordering::Release);
// Wake the accept loop so it sees the flag and closes the listener, and wait for that, so
// the port is free when this returns (a restart on the same port binds it again at once).
// Without a wake-up the loop would block in `accept`: then don't wait.
let woke = TcpStream::connect_timeout(&std::net::SocketAddr::from(([127, 0, 0, 1], self.port)), Duration::from_millis(500)).is_ok();
if woke && let Some(accept) = self.accept.take() {
let _ = accept.join();
}
}
}
/// Serve the control protocol on `127.0.0.1:port` (0 = any free port) until the returned
/// [`Listening`] is dropped.
pub fn listen(port: u16, token: String, ctx: egui::Context) -> std::io::Result<(Receiver<ControlRequest>, Listening)> {
let (tx, rx) = channel::<ControlRequest>();
let listener = TcpListener::bind(("127.0.0.1", port))?;
let port = listener.local_addr()?.port();
let stop = Arc::new(AtomicBool::new(false));
let stopped = stop.clone();
let accept = std::thread::Builder::new().name("control server".into()).spawn(move || accept(listener, token, tx, ctx, Some(stopped)))?;
Ok((rx, Listening { port, stop, accept: Some(accept) }))
}
/// Accept connections on `listener` and serve each on its own thread, until `stop` is set (never,
/// without one).
fn accept(listener: TcpListener, token: String, tx: Sender<ControlRequest>, ctx: egui::Context, stop: Option<Arc<AtomicBool>>) {
let limiter = ConnectionLimiter::new(MAX_CONNECTIONS);
let token = Arc::new(token);
for mut stream in listener.incoming().flatten() {
if stop.as_ref().is_some_and(|s| s.load(Ordering::Acquire)) {
break;
}
let Some(permit) = limiter.try_acquire() else {
let _ = configure_stream(&stream);
let _ = writeln!(stream, "{}", json!({"id": null, "ok": false, "error": "connection limit reached"}));
continue;
};
let tx = tx.clone();
let ctx = ctx.clone();
let token = Arc::clone(&token);
std::thread::spawn(move || {
let _permit = permit;
serve(stream, &token, tx, ctx);
});
}
}
/// The address `listener` is bound to: with `--control 0` the OS picks the port, and a client
/// needs that one, not the requested 0 (#6427). Falls back to the requested port if the OS
/// can't report it.
@@ -138,6 +184,34 @@ mod tests {
use std::io::BufRead;
/// #6427: with port 0 the log names the port the OS bound, which a client can connect to.
#[test]
fn a_listening_server_picks_a_port_and_stops_when_dropped() {
const TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
let (rx, server) = listen(0, TOKEN.into(), egui::Context::default()).unwrap();
let port = server.port;
assert_ne!(port, 0);
let handler = std::thread::spawn(move || {
let req = rx.recv_timeout(Duration::from_secs(5)).unwrap();
assert_eq!(req.method, "test.ping");
req.reply.send(json!({"ok": true, "result": "pong"})).unwrap();
});
let mut stream = TcpStream::connect(("127.0.0.1", port)).unwrap();
stream.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
let mut reader = BufReader::new(stream.try_clone().unwrap());
let mut line = String::new();
writeln!(stream, "{}", json!({"id": 1, "method": "auth", "params": {"token": TOKEN}})).unwrap();
reader.read_line(&mut line).unwrap();
assert_eq!(serde_json::from_str::<Value>(&line).unwrap()["ok"], true);
writeln!(stream, "{}", json!({"id": 2, "method": "test.ping"})).unwrap();
line.clear();
reader.read_line(&mut line).unwrap();
assert_eq!(serde_json::from_str::<Value>(&line).unwrap()["result"], "pong");
handler.join().unwrap();
drop(server);
// The listener is closed when the drop returns: the same port binds again at once.
TcpListener::bind(("127.0.0.1", port)).expect("the port is free after the drop");
}
#[test]
fn port_zero_logs_the_bound_port() {
let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();
+2 -5
View File
@@ -627,17 +627,14 @@ mod native {
/// True for a symlink, or on Windows any reparse point, whose target could be swapped.
/// Expects `symlink_metadata`, which describes the link itself rather than its target.
fn is_redirect(metadata: &Metadata) -> bool {
if metadata.file_type().is_symlink() {
return true;
}
#[cfg(windows)]
{
use std::os::windows::fs::MetadataExt;
metadata.file_attributes() & 0x0000_0400 != 0 // FILE_ATTRIBUTE_REPARSE_POINT
metadata.file_type().is_symlink() || metadata.file_attributes() & 0x0000_0400 != 0 // FILE_ATTRIBUTE_REPARSE_POINT
}
#[cfg(not(windows))]
{
false
metadata.file_type().is_symlink()
}
}
+15 -2
View File
@@ -244,6 +244,17 @@ pub struct LogFile {
/// One formatted log line: `<timestamp> <LEVEL> [<thread>] <target>: <message>\n`.
pub fn format_line(ts: &str, level: log::Level, thread: &str, target: &str, message: &str) -> String {
let message = message.strip_suffix('\n').unwrap_or(message);
// A message cannot forge a record or inject terminal controls: continuation lines are
// indented (a record starts with a timestamp) and other control characters are escaped.
let message: String = message
.chars()
.flat_map(|c| match c {
'\n' => "\n ".chars().collect::<Vec<_>>(),
'\r' => vec!['\\', 'r'],
c if c.is_control() && c != '\t' => c.escape_default().collect(),
c => vec![c],
})
.collect();
format!("{ts} {level:<5} [{thread}] {target}: {message}\n")
}
@@ -560,8 +571,10 @@ mod tests {
format_line("2026-10-08T07:59:17.728Z", log::Level::Warn, "main", "photocraft::gpu_startup", "GPU startup marker: denied"),
"2026-10-08T07:59:17.728Z WARN [main] photocraft::gpu_startup: GPU startup marker: denied\n"
);
// A multi-line message (a panic with a backtrace) keeps its lines; the record still ends in one newline.
assert!(format_line("t", log::Level::Error, "w", "x", "a\nb\n").ends_with("x: a\nb\n"));
// A multi-line message (a panic with a backtrace) keeps its lines, indented; the record still ends in one newline.
assert!(format_line("t", log::Level::Error, "w", "x", "a\nb\n").ends_with("x: a\n b\n"));
let forged = format_line("t", log::Level::Error, "w", "x", "a\r\n2026 INFO forged\x1b[2J");
assert!(forged.lines().all(|l| !l.starts_with("2026")) && !forged.contains('\x1b') && !forged.contains('\r'));
}
#[test]
+232 -53
View File
@@ -29,6 +29,7 @@
#![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")]
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
mod agent_link;
mod app_dirs;
mod app_icon;
mod appearance;
@@ -94,51 +95,77 @@ fn custom_titlebar(prefs_file: Option<&std::path::Path>) -> bool {
CUSTOM_TITLEBAR && !prefs.interface.system_title_bar
}
/// Preferences › Integrations, read before the window opens the way [`custom_titlebar`] is:
/// whether agents may drive this launch at all, and the port to listen on when no `--control`
/// was given (0 = only with `--control`). A missing or unreadable file keeps the defaults.
/// Preferences › AI Integrations, read before the window opens the way [`custom_titlebar`] is:
/// whether agents may drive this launch at all, the port to listen on when no `--control`
/// was given (0 = only with `--control`) and the automation folders used when no
/// `--automation-read-root` / `--automation-write-root` was given. A missing or unreadable file
/// keeps the defaults.
///
/// The section is read on its own, and when it doesn't read as a whole each value on its own,
/// as the app's lenient load reads them (#6611): a wrong-typed value elsewhere in the file, or
/// in the other field, must not turn Allow Agent Control back on while the dialog shows it off.
fn integrations(prefs_file: Option<&std::path::Path>) -> photocraft_engine::prefs::Integrations {
use serde_json::Value;
let mut out = photocraft_engine::prefs::Preferences::default().integrations;
let file: Option<Value> = prefs_file.and_then(|p| std::fs::read_to_string(p).ok()).and_then(|t| serde_json::from_str(&t).ok());
let Some(section) = file.as_ref().and_then(|v| v.get("integrations")) else {
return out;
};
if let Ok(whole) = serde_json::from_value(section.clone()) {
return whole;
let file: Option<serde_json::Value> = prefs_file.and_then(|p| std::fs::read_to_string(p).ok()).and_then(|t| serde_json::from_str(&t).ok());
file.as_ref()
.and_then(|v| v.get("integrations"))
.cloned()
.map(photocraft_engine::prefs::read_integrations_section)
.unwrap_or_else(|| photocraft_engine::prefs::Preferences::default().integrations)
}
/// The automation roots: `--automation-read-root` / `--automation-write-root` (or their
/// variables) win; a direction without one takes Preferences › AI Integrations' folder. A flag's
/// root that can't be opened stops the launch (exit status 2) as before. A preference's root that
/// can't be opened, or isn't an absolute path (it would depend on the folder the app was started
/// from), is left out, so that direction fails closed, and the warning says why: a folder moved
/// since it was chosen must not stop the app from starting. Each folder is opened once, so one
/// that goes away during the launch can't turn into an error, and the same folder for reading
/// and writing shares one capability, as it does when given by flags.
fn automation_workspace(
flags: &photocraft_automation::args::AutomationArgs,
prefs: &photocraft_engine::prefs::Integrations,
) -> Result<(photocraft_automation::workspace::AuthorizedWorkspace, Option<String>), photocraft_automation::AutomationError> {
use photocraft_automation::agent_link::folder;
let mut workspace = flags.workspace()?;
let mut warnings = Vec::new();
for read in [true, false] {
let given = if read { !flags.automation_read_root.is_empty() } else { flags.automation_write_root.is_some() };
let Some(root) = folder(if read { &prefs.read_root } else { &prefs.write_root }).filter(|_| !given) else {
continue;
};
let opened = if !root.is_absolute() {
Err(format!("`{}` is not an absolute path", root.display()))
} else if read {
workspace.with_read_root(&root).map_err(|e| e.to_string())
} else {
workspace.with_write_root(&root).map_err(|e| e.to_string())
};
match opened {
Ok(w) => workspace = w,
Err(e) => {
let which = if read { "Automation Read Folder" } else { "Automation Write Folder" };
warnings.push(format!("Preferences › AI Integrations › {which} is not used: {e}"));
}
}
}
if let Some(allow) = section.get("allowAgentControl").and_then(Value::as_bool) {
out.allow_agent_control = allow;
}
if let Some(port) = section.get("controlPort").and_then(Value::as_u64).and_then(|p| u32::try_from(p).ok()) {
out.control_port = port;
}
out
Ok((workspace, (!warnings.is_empty()).then(|| warnings.join("\n"))))
}
/// The control port this launch listens on, and the warning to print when a request was refused.
/// `--control` (or `PHOTOCRAFT_CONTROL_PORT`) wins over the preference; Allow Agent Control off
/// refuses both, and says so rather than ignoring the flag silently. A preferred port outside
/// 1..=65535 is refused the same way.
/// Only `--control` (or `PHOTOCRAFT_CONTROL_PORT`) opens one at launch: Preferences › AI
/// Integrations' connection starts and stops while the app runs (`agent_link`). Allow Agent
/// Control off refuses the flag too, and says so rather than ignoring it silently.
fn control_port_for(cli: Option<u16>, prefs: &photocraft_engine::prefs::Integrations) -> (Option<u16>, Option<String>) {
if !prefs.allow_agent_control {
let warning = cli.map(|p| format!("photocraft: Preferences › Integrations › Allow Agent Control is off: not listening on port {p}\n"));
let warning = cli.map(|p| {
format!(
"photocraft: Preferences › AI Integrations › Allow Agent Control is off: not listening on port {p}
"
)
});
return (None, warning);
}
if let Some(port) = cli {
return (Some(port), None);
}
match prefs.control_port {
0 => (None, None),
p => match u16::try_from(p).ok().filter(|p| *p != 0) {
Some(port) => (Some(port), None),
None => (None, Some(format!("photocraft: Preferences › Integrations › Control Port {p} is not a usable port: not listening\n"))),
},
}
(cli, None)
}
/// The main window: 1440 × 900 (shrunk to fit the monitor, and maximized on the first frame
@@ -282,17 +309,54 @@ fn linux_display_session() -> linux_libs::DisplaySession {
session
}
/// The control server's port, token and workspace, when it is on (exits 2 on a bad configuration).
/// The control server: its listener, token and workspace.
type Control = (std::net::TcpListener, String, photocraft_automation::workspace::AuthorizedWorkspace);
/// What [`control_setup`] set up: the control server when it is on, and a notice for the window
/// (a preference's automation folder that wasn't used).
struct ControlSetup {
control: Option<Control>,
warning: Option<ControlNotice>,
}
/// A notice for the window about the control server: a Dock launch has no console.
struct ControlNotice {
title: &'static str,
lines: Vec<String>,
}
/// The `--control` server's port, token and workspace, when it is on (exits 2 on a bad
/// configuration). Preferences › AI Integrations' folders stand in for automation roots no flag
/// names. The server the agents switch starts is `agent_link`'s, not this.
fn control_setup(
cli_port: Option<u16>,
auth: photocraft_automation::args::ControlArgs,
automation: &photocraft_automation::args::AutomationArgs,
) -> Option<(std::net::TcpListener, String, photocraft_automation::workspace::AuthorizedWorkspace)> {
let (port, warning) = control_port_for(cli_port, &integrations(services::prefs_file().as_deref()));
) -> ControlSetup {
control_setup_in(services::config_dir(), &integrations(services::prefs_file().as_deref()), cli_port, auth, automation)
}
/// [`control_setup`] with the settings folder and Preferences › AI Integrations given.
fn control_setup_in(
settings: Option<std::path::PathBuf>,
prefs: &photocraft_engine::prefs::Integrations,
cli_port: Option<u16>,
auth: photocraft_automation::args::ControlArgs,
automation: &photocraft_automation::args::AutomationArgs,
) -> ControlSetup {
use photocraft_automation::agent_link;
let (port, warning) = control_port_for(cli_port, prefs);
if let Some(warning) = &warning {
eprint!("{warning}");
}
let port = port?;
// A file a crashed (or, on macOS, quit) launch left behind names a port nothing serves. Before
// binding, so this launch's own listener doesn't keep it.
if let Some(dir) = &settings {
agent_link::remove_stale_connection(dir);
}
let Some(port) = port else {
return ControlSetup { control: None, warning: None };
};
let listener = match std::net::TcpListener::bind(("127.0.0.1", port)) {
Ok(listener) => listener,
Err(error) => {
@@ -308,21 +372,26 @@ fn control_setup(
std::process::exit(2);
}
};
if let Some(path) = token_file {
if let Some(path) = &token_file {
eprintln!("photocraft: control token file: {}", path.display());
} else if supplied.is_none() {
eprintln!("photocraft: control token: {token}");
} else {
eprintln!("photocraft: using supplied control token");
}
let workspace = match automation.workspace() {
let (workspace, warning) = match automation_workspace(automation, prefs) {
Ok(workspace) => workspace,
Err(error) => {
eprintln!("photocraft: cannot configure automation workspace: {error}");
std::process::exit(2);
}
};
Some((listener, token, workspace))
if let Some(warning) = &warning {
eprintln!("photocraft: {warning}");
log::warn!("{warning}");
}
let warning = warning.map(|w| ControlNotice { title: "Automation folder not used", lines: w.lines().map(str::to_owned).collect() });
ControlSetup { control: Some((listener, token, workspace)), warning }
}
/// Record this start's backend in the GPU startup marker, if the marker is locked.
@@ -398,7 +467,7 @@ fn main() -> eframe::Result {
let _ = (new_instance, &launch_options);
// Bind and authenticate the authorized control listener before display/GPU initialization.
let control = control_setup(control_port, auth, &automation);
let ControlSetup { control, warning: control_warning } = control_setup(control_port, auth, &automation);
#[cfg(target_os = "linux")]
let native_session = linux_libs::session_from_env(|name| std::env::var(name).ok());
@@ -618,9 +687,24 @@ fn main() -> eframe::Result {
log::warn!("couldn't remember GPU backend {}: {e}", b.name());
}
});
// Agents in the open window, unless the launch flags (`--control`) manage the
// connection. Behind the suite's AI-MCP feature flag, the ArtCraft Launcher's switch
// (Settings › AI agents) is the one consent; without it, as before, a Control Port.
let flags = craft_flags::load();
for problem in &flags.problems {
log::warn!("feature flags: {problem}");
}
app.agent_link.mcp_flag = flags.enabled(&craft_flags::AI_MCP);
if app.agent_link.mcp_flag {
app.services.launcher_agents = Some(agent_link::launcher_watch(&cc.egui_ctx));
}
if let Some((listener, token, _)) = control {
app.agent_link.launch_flags = true;
app.agent_link.port = listener.local_addr().ok().map(|a| a.port());
let rx = control_server::start(listener, token, cc.egui_ctx.clone());
app = app.with_control(rx);
} else {
app.services.agent_link = Some(agent_link::service(services::config_dir()));
}
#[cfg(target_os = "macos")]
{
@@ -695,6 +779,11 @@ fn main() -> eframe::Result {
if let Some(w) = &app_dirs::current().warning {
photocraft_ui_egui::notices::post(&mut app, "Portable mode is off", vec![w.clone()], false, None);
}
// A preference's automation folder that couldn't be opened, or a token file it couldn't
// write: a Dock launch has no console.
if let Some(ControlNotice { title, lines }) = control_warning {
photocraft_ui_egui::notices::post(&mut app, title, lines, false, None);
}
Ok(Box::new(app))
}),
);
@@ -977,16 +1066,17 @@ mod tests {
#[test]
fn integrations_preferences_decide_the_control_port() {
use photocraft_engine::prefs::Integrations;
let off = Integrations { allow_agent_control: false, control_port: 0 };
let on = Integrations { allow_agent_control: true, control_port: 0 };
let preferred = Integrations { allow_agent_control: true, control_port: 7878 };
let off = Integrations { allow_agent_control: false, control_port: 0, ..Default::default() };
let on = Integrations { allow_agent_control: true, control_port: 0, ..Default::default() };
let preferred = Integrations { allow_agent_control: true, control_port: 7878, ..Default::default() };
// Nothing asked for, nothing listening.
assert_eq!(super::control_port_for(None, &on), (None, None));
// --control wins over the preference.
assert_eq!(super::control_port_for(Some(9000), &preferred), (Some(9000), None));
// The preference opens the port when --control is absent.
assert_eq!(super::control_port_for(None, &preferred), (Some(7878), None));
// The preference alone opens nothing at launch: the agent connection starts it while
// the app runs (`agent_link`).
assert_eq!(super::control_port_for(None, &preferred), (None, None));
// Allow Agent Control off refuses both, and says so when --control asked for one
// rather than ignoring the flag.
@@ -994,14 +1084,8 @@ mod tests {
let (port, warning) = super::control_port_for(Some(9000), &off);
assert_eq!(port, None);
assert!(warning.is_some_and(|w| w.contains("Allow Agent Control") && w.contains("9000")), "the refusal names the port");
let off_with_port = Integrations { allow_agent_control: false, control_port: 7878 };
let off_with_port = Integrations { allow_agent_control: false, control_port: 7878, ..Default::default() };
assert_eq!(super::control_port_for(None, &off_with_port), (None, None), "off beats a preferred port");
// A port a hand-edited file could hold, outside 1..=65535, is refused and named.
let huge = Integrations { allow_agent_control: true, control_port: 70_000 };
let (port, warning) = super::control_port_for(None, &huge);
assert_eq!(port, None);
assert!(warning.is_some_and(|w| w.contains("70000")), "the refusal names the value");
}
#[test]
@@ -1049,13 +1133,108 @@ mod tests {
assert_eq!(r.control_port, 0, "the bad port alone takes its default");
// A bad Allow Agent Control alone takes its default.
let r = read(r#"{"integrations":{"allowAgentControl":"no","controlPort":7878}}"#);
assert_eq!(r, photocraft_engine::prefs::Integrations { allow_agent_control: true, control_port: 7878 });
// (A saved port from before the agents switch turns it on, in both reads.)
assert_eq!(r, photocraft_engine::prefs::Integrations { allow_agent_control: true, control_port: 7878, connect_agents: true, ..Default::default() });
read(r#"{"integrations":{"allowAgentControl":false,"controlPort":-1}}"#);
read(r#"{"integrations":{"allowAgentControl":false,"controlPort":4294967296}}"#);
read(r#"{"integrations":"off"}"#);
// serde reads a struct from a list too; the app does.
assert!(!read(r#"{"integrations":[false]}"#).allow_agent_control, "a list");
assert_eq!(read(r#"{"integrations":[false,7878]}"#).control_port, 7878);
// The automation folders: a bad one takes its default alone, too.
let r = read(r#"{"integrations":{"allowAgentControl":false,"readRoot":5,"writeRoot":"/w"}}"#);
assert!(!r.allow_agent_control, "a bad folder");
assert_eq!((r.read_root.as_str(), r.write_root.as_str()), ("", "/w"));
let _ = std::fs::remove_dir_all(&dir);
}
fn temp_dir(name: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!("pc-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn flags(read: Option<&std::path::Path>, write: Option<&std::path::Path>) -> photocraft_automation::args::AutomationArgs {
photocraft_automation::args::AutomationArgs {
automation_read_root: read.map(Into::into).into_iter().collect(),
automation_write_root: write.map(Into::into),
}
}
fn roots(read: &std::path::Path, write: &std::path::Path) -> photocraft_engine::prefs::Integrations {
photocraft_engine::prefs::Integrations {
read_root: read.to_string_lossy().into_owned(),
write_root: write.to_string_lossy().into_owned(),
..Default::default()
}
}
/// Preferences › AI Integrations' folders are the automation roots when no flag names one, and
/// a flag wins over its preference, direction by direction.
#[test]
fn automation_folders_come_from_the_preferences_unless_a_flag_names_one() {
let dir = temp_dir("automation-roots");
let (pref, flag) = (dir.join("pref"), dir.join("flag"));
std::fs::create_dir_all(&pref).unwrap();
std::fs::create_dir_all(&flag).unwrap();
// No flags: both directions from the preferences.
let (ws, warning) = super::automation_workspace(&flags(None, None), &roots(&pref, &pref)).unwrap();
assert_eq!(warning, None);
ws.write("a.txt", b"pref").unwrap();
assert_eq!(std::fs::read(pref.join("a.txt")).unwrap(), b"pref");
assert_eq!(ws.read("a.txt").unwrap(), b"pref");
// One folder for both shares one capability, as the flags do: an implicit save is allowed.
assert_eq!(ws.read_with_save_path("a.txt").unwrap().1.as_deref(), Some("a.txt"));
// A write flag wins over the write folder; reads still come from the preference.
let (ws, _) = super::automation_workspace(&flags(None, Some(&flag)), &roots(&pref, &pref)).unwrap();
ws.write("b.txt", b"flag").unwrap();
assert!(flag.join("b.txt").is_file() && !pref.join("b.txt").exists(), "the flag's folder takes the write");
assert_eq!(ws.read("a.txt").unwrap(), b"pref");
assert_eq!(ws.read_with_save_path("a.txt").unwrap().1, None, "read and write folders differ: no implicit save");
// A read folder spelled like the write flag shares the flag's capability.
let (ws, _) = super::automation_workspace(&flags(None, Some(&flag)), &roots(&flag, &pref)).unwrap();
assert_eq!(ws.read_with_save_path("b.txt").unwrap().1.as_deref(), Some("b.txt"));
// Empty preferences grant nothing, as no flags do.
let (ws, warning) = super::automation_workspace(&flags(None, None), &photocraft_engine::prefs::Integrations::default()).unwrap();
assert_eq!(warning, None);
assert!(ws.read("a.txt").is_err() && ws.write("c.txt", b"x").is_err());
let _ = std::fs::remove_dir_all(&dir);
}
/// A preference's folder that can't be opened is left out (that direction fails closed) and
/// named in the warning; the app still starts. A flag's still stops the launch.
#[test]
fn a_missing_preference_folder_is_left_out_and_a_missing_flag_root_is_an_error() {
let dir = temp_dir("automation-roots-missing");
let missing = dir.join("gone");
let (ws, warning) = super::automation_workspace(&flags(None, None), &roots(&dir, &missing)).unwrap();
let warning = warning.expect("the missing write folder is named");
assert!(warning.contains("Automation Write Folder") && !warning.contains("Read Folder"), "{warning}");
std::fs::write(dir.join("in.txt"), b"x").unwrap();
assert_eq!(ws.read("in.txt").unwrap(), b"x", "the read folder still applies");
assert!(ws.write("out.txt", b"x").is_err(), "no write access without its folder");
assert!(super::automation_workspace(&flags(Some(&missing), None), &roots(&dir, &dir)).is_err(), "a flag's root must open");
let _ = std::fs::remove_dir_all(&dir);
}
/// A relative folder preference would depend on the folder the app was started from: left
/// out with a warning, like a missing one.
#[test]
fn a_relative_preference_folder_is_left_out() {
let dir = temp_dir("automation-roots-relative");
let relative = photocraft_engine::prefs::Integrations { read_root: ".".into(), ..roots(&dir, &dir) };
let (ws, warning) = super::automation_workspace(&flags(None, None), &relative).unwrap();
let warning = warning.expect("the relative read folder is named");
assert!(warning.contains("Automation Read Folder") && warning.contains("not an absolute path"), "{warning}");
assert!(ws.read("anything").is_err(), "no read access from a relative folder");
ws.write("out.txt", b"x").unwrap();
assert!(dir.join("out.txt").is_file(), "the absolute write folder still applies");
let _ = std::fs::remove_dir_all(&dir);
}
+23 -16
View File
@@ -721,38 +721,45 @@ fn shell_word(s: &str) -> String {
if s.bytes().all(|b| b.is_ascii_alphanumeric() || b"/._-+:,@".contains(&b)) { s.to_string() } else { format!("'{}'", s.replace('\'', r"'\''")) }
}
pub fn native(automation: Option<photocraft_automation::AuthorizedWorkspace>) -> Services {
let clip: Rc<RefCell<Option<arboard::Clipboard>>> = Rc::default();
let clip_unreadable: Rc<Cell<bool>> = Rc::default();
let automation_read = automation.clone().map(|workspace| {
/// What control and MCP requests may read, write and run, through `workspace` (`None`: no
/// automation, as without `--control` or an agent connection).
pub fn automation(workspace: Option<photocraft_automation::AuthorizedWorkspace>) -> photocraft_ui_egui::AutomationServices {
let read = workspace.clone().map(|workspace| {
Box::new(move |path: &str| {
let (bytes, save_path) = workspace.read_with_save_path(path).map_err(|error| error.to_string())?;
let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path).to_string();
Ok(photocraft_ui_egui::AutomationReadResult { name, bytes, save_path })
}) as photocraft_ui_egui::AutomationReadFn
});
let automation_write = automation.clone().map(|workspace| {
let write = workspace.clone().map(|workspace| {
Box::new(move |path: &str, bytes: &[u8]| workspace.write(path, bytes).map_err(|error| error.to_string())) as photocraft_ui_egui::AutomationWriteFn
});
let automation_open_pcraft = automation.clone().map(|workspace| {
let open_pcraft = workspace.clone().map(|workspace| {
Box::new(move |path: &str| {
let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path).to_string();
let opened = crate::crash_guard::guard("Open", || workspace.open_pcraft(path).map_err(|e| e.to_string()))?;
Ok(opened.map(|doc| (name, doc)))
}) as photocraft_ui_egui::AutomationOpenPcraftFn
});
let automation_save = automation.clone().map(|workspace| {
let save = workspace.clone().map(|workspace| {
std::sync::Arc::new(move |doc: &Document, path: &str, settings: &photocraft_ui_egui::ExportSettings, ctl: &photocraft_ui_egui::jobs_ui::SaveCtl| {
save_through(doc, path, settings, ctl, |p, encoded, _| workspace.write_stream(p, &mut |w| encoded.write_to(w)).map_err(|e| e.to_string()))
}) as photocraft_ui_egui::SaveFileFn
});
let step: fn(&str, &serde_json::Value) -> photocraft_engine::Result<()> = photocraft_automation::workspace::authorize_desktop_engine_step;
let automation_authorize = automation.is_some().then_some(step);
let automation_command = automation.map(|_| {
let step: photocraft_ui_egui::AutomationAuthorizeFn = photocraft_automation::workspace::authorize_desktop_engine_step;
let authorize = workspace.is_some().then_some(step);
let command = workspace.map(|_| {
Box::new(|id: &str, params: &serde_json::Value| {
photocraft_automation::workspace::authorize_desktop_engine_command(id, params).map_err(|error| error.to_string())
}) as photocraft_ui_egui::AutomationCommandFn
});
photocraft_ui_egui::AutomationServices { read, write, open_pcraft, save, command, authorize }
}
pub fn native(automation: Option<photocraft_automation::AuthorizedWorkspace>) -> Services {
let clip: Rc<RefCell<Option<arboard::Clipboard>>> = Rc::default();
let clip_unreadable: Rc<Cell<bool>> = Rc::default();
let automation = self::automation(automation);
Services {
import: Some(Box::new(|name: &str, bytes: &[u8], opts: &photocraft_io::ImportOptions| {
crate::crash_guard::guard("Open", || {
@@ -772,12 +779,12 @@ pub fn native(automation: Option<photocraft_automation::AuthorizedWorkspace>) ->
Some(photocraft_ui_egui::FileStamp { len: meta.len(), modified: meta.modified().ok() })
})),
same_file: Some(Box::new(|a: &str, b: &str| same_entry(Path::new(a), Path::new(b), same_file))),
automation_read,
automation_write,
automation_open_pcraft,
automation_save,
automation_command,
automation_authorize,
automation_read: automation.read,
automation_write: automation.write,
automation_open_pcraft: automation.open_pcraft,
automation_save: automation.save,
automation_command: automation.command,
automation_authorize: automation.authorize,
encode_png: Some(Box::new(|w, h, rgba| {
let img = Image::from_u8(w, h, ChannelLayout::Rgba, rgba.to_vec()).map_err(|e| e.to_string())?;
photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &EncodeOptions::default()).map_err(|e| e.to_string())
+7
View File
@@ -0,0 +1,7 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75">
<path d="M8 6H16 M18 8V16 M16 18H8 M6 16V8"/>
<rect x="4" y="4" width="4" height="4"/>
<rect x="16" y="4" width="4" height="4"/>
<rect x="4" y="16" width="4" height="4"/>
<rect x="16" y="16" width="4" height="4"/>
</svg>

After

Width:  |  Height:  |  Size: 368 B

+15
View File
@@ -0,0 +1,15 @@
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<circle cx="11" cy="11" r="8" />
<line x1="21" x2="16.65" y1="21" y2="16.65" />
<line x1="8" x2="14" y1="11" y2="11" />
</svg>

After

Width:  |  Height:  |  Size: 334 B

+13 -3
View File
@@ -1,17 +1,21 @@
# MCP
`photocraft-automation` implements an MCP server over stdio with two backends:
`photocraft-automation` implements an MCP server over stdio with three backends:
- **Headless:** an in-process `Headless` session owns the engine and performs file I/O.
- **Bridge:** `BridgeClient` forwards tools to a desktop application started with `photocraft --control <port>`.
- **The open window:** plain `photocraft-cli mcp` finds the desktop application through the connection file it publishes while the person lets agents use it (ArtCraft Launcher › Settings › AI agents), before every tool call; while the app is closed it starts it, serves a headless session, or reports an error, as chosen in Preferences › AI Integrations. Without that consent, or with `--headless`, it is the headless session above.
The simplest setup needs no terminal: turn on ArtCraft Launcher › Settings › AI agents and click *Add* next to the AI app; that one entry reaches every Craft app. See [`docs/mcp.md`](../../../docs/mcp.md#connecting-the-desktop-app-no-terminal).
For a source checkout, first [install the CLI on PATH and register the server](../../../docs/mcp.md#install-and-register).
The installed binary survives `cargo clean`; a symlink into `target/release` does not.
Start the headless server with:
Start the headless server with (without `--headless`, `mcp` works in the open PhotoCraft window
while the ArtCraft Launcher's agents switch is on):
```sh
photocraft-cli mcp --automation-read-root /work/project --automation-write-root /work/project
photocraft-cli mcp --headless --automation-read-root /work/project --automation-write-root /work/project
```
Bridge to a running desktop application with the same private token file used by the app:
@@ -56,6 +60,12 @@ explicitly granted at launch; omitting a root omits that authority. Bridge mode
loopback control connection with a bearer token before invoking any control method and uses the
roots configured on the desktop process.
The launcher's switch (off by default) opens the same loopback, token-authenticated control
listener as `--control`, with a token the app keeps in its settings folder (`agent-control.token`,
user-only on Unix; delete it for a new one). The folders chosen in Preferences › AI Integrations
are the desktop process's roots, and the roots of a `mcp` headless session only when agents may
use the app and *When PhotoCraft is closed* is set to work without a window.
The current server does not issue scoped sessions or distinguish trusted tools from high-impact tools. Authentication proves possession of the token; it does not restrict an authenticated client to document-only access. Do not expose either transport through an untrusted broker, shell, proxy, or remote service without adding an external policy boundary.
Filesystem capabilities are implemented. General tool and method capabilities remain
+1 -1
View File
@@ -2,7 +2,7 @@
External bytes enter PhotoCraft through four main paths:
- `photocraft-psd` parses and writes PSD and PSB structures.
- `craft-psd` parses and writes PSD and PSB structures.
- `photocraft-codecs` decodes and encodes flat raster formats.
- `photocraft-format` loads and saves the native `.pcraft` bundle.
- `photocraft-affinity` reads native Affinity documents (`.af`, `.afdesign`, `.afphoto`, `.afpub`): layers, artboards, vectors, text, images and masks; it does not write them.
+1 -1
View File
@@ -1,6 +1,6 @@
# PSD and PSB
`photocraft-psd` is a standalone clean-room reader and writer based on Adobe's public PSD specification. It supports PSD version 1 and PSB version 2, preserves unknown blocks for round-trip fidelity, and decodes channel data lazily.
`craft-psd` is a standalone clean-room reader and writer based on Adobe's public PSD specification. It supports PSD version 1 and PSB version 2, preserves unknown blocks for round-trip fidelity, and decodes channel data lazily.
The parser accepts all four PSD compression methods handled by the crate and represents format-level structures before `photocraft-io` maps them to `photocraft-doc`. The current public API and fidelity behavior are documented in [`crates/psd/README.md`](https://github.com/storytold/photocraft/blob/main/crates/psd/README.md).
+2 -2
View File
@@ -4,7 +4,7 @@
## Layered TIFF
A TIFF saved by Photoshop with its layers carries them in two private tags: 37724 (`ImageSourceData`, the same layer section a PSD has, preceded by the string `Adobe Photoshop Document Data Block`) and 34377 (the PSD image resources). `photocraft-codecs` passes both through as opaque bytes on `Metadata`; `photocraft-io` rebuilds a PSD model from them plus the TIFF's pixels and hands it to the PSD importer, so groups, masks, layer styles, type, shapes, smart objects and adjustment layers open from a TIFF exactly as from a PSD, and are written back the same way. In Intel-order (`II`) files Photoshop stores the whole block structure byte-swapped (reversed keys such as `MIB8` and `ryaL`, little-endian integers and doubles) while pixel samples stay big-endian; `photocraft-psd`'s `tiff` module converts between the two orders field by field, and drops, with a warning, any block whose layout it does not know rather than pass byte-swapped data to other readers. Limits: up to sixteen extra samples (spot colours and alpha channels, plus a Quick Mask) are written in the IFD and round-trip, with Photoshop image resources for the channel name, ink colour and solidity; Lab documents are saved flat, with their extra samples. Save As asks "Including layers will increase file size" (with "Discard Layers and Save a Copy") while *Ask Before Saving Layered TIFF Files* is on in Preferences › File Handling. Scripted and agent saves write a flat TIFF unless they ask for layers: `--tiff-layers` for `photocraft-cli convert`, `run` and `batch`, and `"tiffLayers": true` in the save, batch and export commands and MCP `doc_save`.
A TIFF saved by Photoshop with its layers carries them in two private tags: 37724 (`ImageSourceData`, the same layer section a PSD has, preceded by the string `Adobe Photoshop Document Data Block`) and 34377 (the PSD image resources). `photocraft-codecs` passes both through as opaque bytes on `Metadata`; `photocraft-io` rebuilds a PSD model from them plus the TIFF's pixels and hands it to the PSD importer, so groups, masks, layer styles, type, shapes, smart objects and adjustment layers open from a TIFF exactly as from a PSD, and are written back the same way. In Intel-order (`II`) files Photoshop stores the whole block structure byte-swapped (reversed keys such as `MIB8` and `ryaL`, little-endian integers and doubles) while pixel samples stay big-endian; `craft-psd`'s `tiff` module converts between the two orders field by field, and drops, with a warning, any block whose layout it does not know rather than pass byte-swapped data to other readers. Limits: up to sixteen extra samples (spot colours and alpha channels, plus a Quick Mask) are written in the IFD and round-trip, with Photoshop image resources for the channel name, ink colour and solidity; Lab documents are saved flat, with their extra samples. Save As asks "Including layers will increase file size" (with "Discard Layers and Save a Copy") while *Ask Before Saving Layered TIFF Files* is on in Preferences › File Handling. Scripted and agent saves write a flat TIFF unless they ask for layers: `--tiff-layers` for `photocraft-cli convert`, `run` and `batch`, and `"tiffLayers": true` in the save, batch and export commands and MCP `doc_save`.
## JPEG XL
@@ -31,7 +31,7 @@ Every route that writes a document to a flat format (Save As, the CLI, MCP `doc_
- Formats without transparency (JPEG, Radiance HDR) get the image composited over white, as Export As and Quick Export do; in CMYK, white is no ink.
- Formats that can't embed an ICC profile (GIF, BMP, TGA, QOI, ICO, Netpbm/PFM) get RGB documents in another profile (linear light, Display P3, …) converted to sRGB through the colour engine (perceptual intent), since an untagged file is read as sRGB. OpenEXR and Radiance HDR get linear sRGB instead (linear gray for a grayscale OpenEXR), which is how they are tagged when opened.
- TGA, as Photoshop's Targa: an RGB Targa is 24 or 32 bits per pixel. Save As and Save a Copy ask (Targa Options, starting at 32 with an alpha channel, else 24); Export As has a Resolution choice (Auto, 24 or 32 bits/pixel); scripts pass `tgaBits` (`--tga-bits` on the CLI). At 32 bits the document's alpha channel is the file's alpha when it has exactly one (spot channels don't count; with several, none is used), and the colours are written exactly, never premultiplied or cleared where the alpha is 0, so a game texture's mask and its colours both survive. Without a usable alpha channel the layers' transparency is the alpha (opaque without any). 24 bits write no alpha, compositing transparency over white. Without a choice: 32 with an alpha channel or transparency, else 24. A 32-bit Targa opens as an opaque Background with its alpha as "Alpha 1", so it round-trips. Grayscale Targas are 8-bit.
- JPEG, as Photoshop's JPEG Options: File › Save, Save As and Save a Copy ask for the Quality on Photoshop's 0–12 scale (with its Low, Medium, High and Maximum presets) and a Format Option: Baseline ("Standard"), Baseline Optimized (Huffman tables fitted to the image) or Progressive. The quality starts at the opened JPEG's own quality, else at the Export preference's JPEG quality; left there, an opened JPEG is written at exactly its own quality. Scripts pass `"quality": 0..12` and `"jpegFormat": "baseline" | "optimized" | "progressive"` (on the CLI, `--quality` on the encoder's 1–100 scale and `--jpeg-format`) and aren't asked; recorded actions replay the answer. The scale maps onto the encoder's 1–100 as `round(q / 12 × 99 + 1)`. RGB JPEGs are written 4:2:0, where Baseline Optimized keeps the standard tables for now (Gray and CMYK, written 4:4:4, get fitted ones). Matte, the progressive scan count (Scans 3–5) and the file-size preview aren't offered.
- JPEG, as Photoshop's JPEG Options: File › Save, Save As and Save a Copy ask for the Quality on Photoshop's 0–12 scale (with its Low, Medium, High and Maximum presets) and a Format Option: Baseline ("Standard"), Baseline Optimized (Huffman tables fitted to the image) or Progressive. The quality starts at the opened JPEG's own quality, else at the Export preference's JPEG quality; left there, an opened JPEG is written at exactly its own quality. Scripts pass `"quality": 0..12` and `"jpegFormat": "baseline" | "optimized" | "progressive"` (on the CLI, `--quality` on the encoder's 1–100 scale and `--jpeg-format`) and aren't asked; recorded actions replay the answer. The scale maps onto the encoder's 1–100 as `round(q / 12 × 99 + 1)`. RGB JPEGs are written 4:2:0, where Baseline Optimized keeps the standard tables for now (Gray and CMYK, written 4:4:4, get fitted ones). Below the Format Options the dialog shows how big the file will be ("152.3K", "1.5M"), so the quality can be chosen with the size in view; it is worked out as the options change, in the background in the desktop app and within the frame in the web build, for documents up to 40 megapixels (4 on the web), and reads "—" for larger ones. Matte and the progressive scan count (Scans 3–5) aren't offered.
- DDS, for game and 3D textures: Save As and Save a Copy ask (DDS Options) for the compression (None, uncompressed and lossless; BC1/DXT1, whose alpha is on or off; BC2/DXT3; BC3/DXT5) and whether to generate mipmaps (the full chain down to 1×1, each level a box filter of the one above). Scripts pass `ddsCompression` (`none`, `bc1`, `bc2`, `bc3`) and `ddsMipmaps` (`--dds-compression`, `--dds-mipmaps` on the CLI). Opening one reads its top mip level.
Flat TIFF and JPEG exports of composited CMYK documents convert the RGB composite back through
+8
View File
@@ -29,6 +29,14 @@ Automation is a privilege boundary because requests can cause filesystem access,
Screenshots are decoded under separate dimension, pixel, and allocation ceilings before
downscaling; the 5 MiB PNG limit applies to the returned image.
- MCP normally uses stdio and can optionally bridge to loopback control TCP.
- ArtCraft Launcher › Settings › AI agents (off by default; one switch for every Craft app) opens that loopback
listener without launch flags. Its token is kept in a user-only file in the settings folder
(`agent-control.token`) and stays the same across restarts and port changes: restarting does
not revoke it. To revoke an exposed token, delete that file; the next start creates a new one.
The published `agent-connection.json` holds the port and the token file's path, never the
token. `photocraft-cli mcp` authenticates with the challenge handshake, so the token
itself is never sent: the listener must first prove it holds the token, and a stale file whose
port another program took fails that check before the client reveals anything.
- The bridge and UI request paths use timeouts.
- Engine commands are expected to reject invalid parameters without panicking.
- Remote filesystem methods use separately granted read and write directory capabilities.
+1 -1
View File
@@ -6,7 +6,7 @@ Parser safety is broader than “does not crash.” A parser must also bound mem
### PSD/PSB
`photocraft-psd` enforces maximum document dimensions and channel counts, bounds decoded channel data, limits ActionDescriptor nesting and pattern edges, and uses checked arithmetic for key decoded-size calculations. Tests cover malformed headers, truncation, property-generated inputs, and dedicated fuzz targets.
`craft-psd` enforces maximum document dimensions and channel counts, bounds decoded channel data, limits ActionDescriptor nesting and pattern edges, and uses checked arithmetic for key decoded-size calculations. Tests cover malformed headers, truncation, property-generated inputs, and dedicated fuzz targets.
### Raster codecs
+12
View File
@@ -20,6 +20,9 @@ sync_github_name = true
[people.brianblakely]
sync_github_name = true
[people.mlo0352]
sync_github_name = true
[people.abdul-karim-mia]
real_name = "Abdul karim mia"
sync_github_name = false
@@ -32,6 +35,9 @@ sync_github_name = false
display_name = "Skyfuller"
sync_github_name = true
[people.JohnXu28]
display_name = "JohnXu"
[people.AlanRockefeller]
sync_github_name = true
@@ -326,6 +332,9 @@ sync_github_name = true
[people.Luis-0508]
sync_github_name = true
[people.ashokram-si]
real_name = "Ashok Ram"
[people.Kaedo17]
sync_github_name = true
@@ -341,5 +350,8 @@ sync_github_name = false
display_name = "Tsieg"
sync_github_name = true
[people.slafebre]
real_name = "Sven Lafebre"
[people.kirubhakaran12]
sync_github_name = true
+1 -1
View File
@@ -71,7 +71,7 @@ are rejected. The I/O adapter decodes it with a 128 MiB allocation limit.
Affinity has no published file-format specification, so this reader is built only from a public
description of the format and from public files, never from Affinity itself. This is how
PhotoCraft already treats formats that are only partly documented: `photocraft-psd` fills the gaps
PhotoCraft already treats formats that are only partly documented: `craft-psd` fills the gaps
in Adobe's specification from the MIT-licensed psd-tools and ag-psd, and `photocraft-raw` recovered
Nikon's compression tables by black-box analysis of CC0 sample files (see their READMEs).
+19 -2
View File
@@ -447,7 +447,7 @@ pub(crate) fn effect_reach(e: &GalleryEffect) -> i32 {
F::Plaster => gs(0.5 + g("smoothness") * 0.8) + gs(1.5) + 2,
F::Stamp => gs(0.3 + g("smoothness") * 0.3),
F::TornEdges => gs(1.0),
F::WaterPaper => 2 * (g("fiberLength") / 6.0) as i32 + 2,
F::WaterPaper => 2 * (fiber_length(e) / 6.0) as i32 + 2,
F::GlowingEdges => gs(0.3 + g("smoothness") * 0.25) + gs(g("edgeWidth") * 0.4) + 2,
F::Craquelure => 0,
F::Patchwork => patch_size(e).ceil() as i32 + 2,
@@ -518,6 +518,12 @@ fn plastic_sigma(e: &GalleryEffect) -> f32 {
fn watercolor_radius(e: &GalleryEffect) -> usize {
1 + ((15.0 - e.get("brushDetail")) / 2.5) as usize
}
/// Water Paper's fiber length within its parameter range (3..=50): raw gallery values bypass the
/// setter's clamp, and the reach estimate and the blur radius must agree on one bounded value.
fn fiber_length(e: &GalleryEffect) -> f32 {
e.get("fiberLength").clamp(3.0, 50.0)
}
fn basrelief_sigma(e: &GalleryEffect) -> f32 {
0.3 + e.get("smoothness") * 0.5 + (15.0 - e.get("detail")) * 0.12
}
@@ -1178,7 +1184,7 @@ fn apply_sketch(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) -> bool {
}
}
F::WaterPaper => {
let fl = g("fiberLength");
let fl = fiber_length(e);
let (br, co) = (g("brightness") / 100.0, g("contrast") / 100.0);
let r = (fl / 6.0) as usize;
line_blur3(&mut win.c, w, h, (1, 0), r);
@@ -1360,6 +1366,17 @@ mod tests {
s
}
#[test]
fn gallery_window_budget_counts_working_temporaries() {
// A 6300 px square RGBA window is 159 M samples for the source read alone, 476 M with the old
// 3x factor, and over the 512 Mi-sample budget once the working window and temporaries count.
// The check runs before any window is read, so nothing that large is allocated.
let s = pattern(SampleType::F32);
let big = Rect::new(0, 0, 6300, 6300);
let fp = FilterParams::FilterGallery { effects: vec![GalleryEffect::new(crate::GalleryFilter::ALL[0])] };
assert!(crate::apply_tiled_with(&s, &fp, big, big, None, 6300, None, &photocraft_raster::Interrupt::NONE).is_none());
}
#[test]
fn every_effect_changes_pixels_and_is_tile_independent() {
let b = Rect::new(0, 0, 56, 40);
+6 -1
View File
@@ -168,13 +168,18 @@ fn boxes_for_gauss(sigma: f32, n: usize) -> Vec<usize> {
(0..n).map(|i| if i < m { wl as usize } else { wu as usize }).collect()
}
/// Largest box-blur radius run (far beyond any real use).
const MAX_BOX_RADIUS: f32 = 16384.0;
/// The box widths (odd) a blur runs as running-sum passes along each axis, or `None` when it uses a
/// direct kernel (small radii, where the kernel is quicker).
pub(crate) fn box_widths(params: &FilterParams) -> Option<Vec<usize>> {
match *params {
FilterParams::GaussianBlur { radius } if radius > 4.0 => Some(boxes_for_gauss(radius, 3)),
FilterParams::BoxBlur { radius } => {
let r = radius.max(0.0).round() as usize;
// Capped: the line scratch grows with the radius, and `2 * r + 1` must not overflow
// (Photoshop's largest Box Blur radius is 999).
let r = radius.clamp(0.0, MAX_BOX_RADIUS).round() as usize;
(r > 4).then(|| vec![2 * r + 1])
}
_ => None,
+14 -6
View File
@@ -1029,23 +1029,25 @@ fn dehaze(px: &mut [[f32; 4]], w: usize, h: usize, d: &Dehaze) {
}
fn detail(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw, ps: f32) {
// The controls' documented 0..=100 range: raw values drive the filter radii below.
let [nl, nld, nc, ncd] = [p.noise_luminance, p.noise_luminance_detail, p.noise_color, p.noise_color_detail].map(|v| v.clamp(0.0, 100.0));
// Work in Y + chroma (BT.709 luma, B−Y, R−Y).
let mut y: Vec<f32> = px.iter().map(|q| luma([q[0], q[1], q[2]])).collect();
let mut cb: Vec<f32> = px.iter().zip(&y).map(|(q, l)| q[2] - l).collect();
let mut cr: Vec<f32> = px.iter().zip(&y).map(|(q, l)| q[0] - l).collect();
if p.noise_luminance > 0.0 {
let n = p.noise_luminance / 100.0;
if nl > 0.0 {
let n = nl / 100.0;
let r = ((1.0 + 3.0 * n) * ps).round().max(1.0) as usize;
let smooth = guided(&y, &y, w, h, r, (n * 0.06).powi(2) + 1e-6);
let keep = p.noise_luminance_detail / 100.0 * 0.5;
let keep = nld / 100.0 * 0.5;
for i in 0..w * h {
y[i] = smooth[i] + (y[i] - smooth[i]) * keep;
}
}
if p.noise_color > 0.0 {
let sigma = (p.noise_color / 100.0 * 6.0 * ps).max(0.3);
if nc > 0.0 {
let sigma = (nc / 100.0 * 6.0 * ps).max(0.3);
let (bcb, bcr) = (gauss(&cb, w, h, sigma), gauss(&cr, w, h, sigma));
let keep = p.noise_color_detail / 100.0 * 0.3;
let keep = ncd / 100.0 * 0.3;
for i in 0..w * h {
cb[i] = bcb[i] + (cb[i] - bcb[i]) * keep;
cr[i] = bcr[i] + (cr[i] - bcr[i]) * keep;
@@ -1306,6 +1308,12 @@ mod tests {
let mut nr = noisy.clone();
develop(&mut nr, w, h, &CameraRaw { noise_luminance: 80.0, ..Default::default() }, false);
assert!(var(&nr) < var(&noisy) * 0.5);
// Out-of-range noise settings are held to their 0..=100 range, not turned into huge radii.
let mut wild = noisy.clone();
let hostile =
CameraRaw { noise_luminance: 1.0e30, noise_color: 1.0e30, noise_luminance_detail: 1.0e30, noise_color_detail: 1.0e30, ..Default::default() };
develop(&mut wild, w, h, &hostile, false);
assert!(wild.iter().all(|q| q[0].is_finite()));
let mut sh = noisy.clone();
develop(&mut sh, w, h, &CameraRaw { sharpen_amount: 100.0, ..Default::default() }, false);
assert!(var(&sh) > var(&noisy));
+34 -2
View File
@@ -30,9 +30,14 @@ impl CameraInfo {
}
}
/// Text bytes decoded per EXIF read (real Make/Model/Lens/Date strings are a few dozen bytes).
const MAX_TEXT_BYTES: usize = 1 << 20;
struct Tiff<'a> {
b: &'a [u8],
le: bool,
/// Text bytes this read may still decode: duplicate tags can share one large blank value.
text_left: std::cell::Cell<usize>,
}
impl Tiff<'_> {
@@ -93,7 +98,9 @@ impl Tiff<'_> {
return None;
}
let at = self.data_at(typ, count, field)?;
let s = self.b.get(at..at.checked_add(usize::try_from(count).ok()?)?)?;
let n = usize::try_from(count).ok()?;
self.text_left.set(self.text_left.get().checked_sub(n)?);
let s = self.b.get(at..at.checked_add(n)?)?;
let s = String::from_utf8_lossy(s).trim_end_matches('\0').trim().to_string();
(!s.is_empty()).then_some(s)
}
@@ -111,7 +118,7 @@ pub fn read(exif: &[u8]) -> CameraInfo {
Some([b'M', b'M', 0, 42]) => false,
_ => return info,
};
let t = Tiff { b, le };
let t = Tiff { b, le, text_left: std::cell::Cell::new(MAX_TEXT_BYTES) };
let Some(ifd0) = t.u32(4) else { return info };
let mut queue = vec![ifd0 as usize];
let mut seen = Vec::new();
@@ -259,6 +266,31 @@ mod tests {
/// Hostile payloads: huge counts, offsets near `u32::MAX`, self-referencing IFDs and random
/// bytes read as `None` fields instead of panicking or overflowing (usize is 32 bits on wasm).
#[test]
fn blank_duplicate_text_tags_share_one_decode_budget() {
// Blank values never fill `make`, so every duplicate would decode the same 1 MiB again;
// the per-read budget stops after the first, so the later real value is not reached.
let blank = MAX_TEXT_BYTES;
let entry = |count: u32, off: u32| {
let mut e = Vec::new();
e.extend_from_slice(&0x010Fu16.to_le_bytes());
e.extend_from_slice(&2u16.to_le_bytes());
e.extend_from_slice(&count.to_le_bytes());
e.extend_from_slice(&off.to_le_bytes());
e
};
let mut b = b"II*\0".to_vec();
b.extend_from_slice(&8u32.to_le_bytes());
b.extend_from_slice(&3u16.to_le_bytes());
b.extend(entry(blank as u32, 50));
b.extend(entry(blank as u32, 50));
b.extend(entry(2, u32::from_le_bytes(*b"M\0\0\0")));
b.extend_from_slice(&0u32.to_le_bytes());
assert_eq!(b.len(), 50);
b.extend(std::iter::repeat_n(b' ', blank));
assert_eq!(read(&b).make, None);
}
#[test]
fn duplicate_text_tags_keep_the_first_value_without_copying_large_duplicates() {
let value = vec![b'X'; 2 * 1024 * 1024];
+8 -3
View File
@@ -115,7 +115,7 @@ pub fn complete_with(
p: &CompleteParams,
ctl: &photocraft_raster::Interrupt,
) -> Result<Option<Vec<f32>>, photocraft_raster::Cancelled> {
assert_eq!(hole.len(), w * h);
assert_eq!(w.checked_mul(h), Some(hole.len()));
let px: Vec<Px> = hole.iter().map(|h| if *h { Px::Hole } else { Px::Source }).collect();
complete_masked(w, h, ch, img, &px, p, ctl)
}
@@ -159,8 +159,13 @@ fn complete_cow<'a>(
p: &CompleteParams,
ctl: &photocraft_raster::Interrupt,
) -> Result<Result<Vec<f32>, Cow<'a, [f32]>>, photocraft_raster::Cancelled> {
assert_eq!(img.len(), w * h * ch);
assert_eq!(px.len(), w * h);
// Checked: wrapped products must not let tiny buffers pass for a huge `h`.
assert_eq!(w.checked_mul(h), Some(px.len()));
assert_eq!(w.checked_mul(h).and_then(|n| n.checked_mul(ch)), Some(img.len()));
if w == 0 || h == 0 {
// Nothing to fill; no per-row tables for a zero-width image of any height.
return Ok(Ok(img.into_owned()));
}
let Some(hb0) = hole_bounds(w, h, px) else { return Ok(Ok(img.into_owned())) };
if ch == 0 {
return Ok(Ok(Vec::new()));
+2 -1
View File
@@ -261,7 +261,8 @@ pub fn best_offset(w: usize, h: usize, ch: usize, img: &[f32], hole: &[bool], ri
pub fn synthesize(w: usize, h: usize, ch: usize, img: &[f32], hole: &[bool], block: usize, seed: u64) -> Vec<f32> {
let mut out = img.to_vec();
let Some((bx0, by0, bx1, by1)) = hole_bbox(w, h, hole) else { return out };
let b = block.max(4) as i32;
// A block larger than the image fits no candidate either way: the clamp keeps the i32 math in range.
let b = block.clamp(4, w.max(h).max(4)) as i32;
let ov = (b / 3).max(1);
let hs = integral(w, h, hole);
// Candidate block origins: whole block (with overlap) inside the grid and outside the hole.
+18
View File
@@ -23,6 +23,24 @@ fn zero_width_images_complete_without_sizing_by_height() {
assert_eq!(complete(1 << 40, 0, 3, &[], &[], &p), Some(Vec::new()));
}
/// Dimensions whose product wraps must be refused, not matched against tiny buffers.
#[test]
#[should_panic]
fn wrapping_dimension_products_are_refused() {
let _ = complete(1 << 40, 1 << 40, 1, &[], &[], &CompleteParams::default());
}
/// A block size beyond `i32` falls back to the membrane fill instead of looping on a zero step.
#[test]
fn a_huge_synthesis_block_does_not_hang() {
let (w, h) = (8, 8);
let img = vec![0.5f32; w * h];
let mut hole = vec![false; w * h];
hole[27] = true;
let out = synthesize(w, h, 1, &img, &hole, usize::MAX, 1);
assert_eq!(out.len(), w * h);
}
/// Smooth gradient plus deterministic fine noise.
fn texture(w: usize, h: usize) -> Vec<f32> {
let mut rng = Rng::new(7);
+18 -1
View File
@@ -1317,6 +1317,15 @@ fn coverage_operation(params: &FilterParams) -> Option<coverage::Operation<'_>>
}
}
/// Working-copy samples per pixel a Filter Gallery stack holds beside the source read: none for an
/// empty stack, which returns the cropped source.
fn gallery_working_samples(params: &FilterParams) -> usize {
match params {
FilterParams::FilterGallery { effects } if !effects.is_empty() => 12,
_ => 0,
}
}
#[allow(clippy::too_many_arguments)]
/// Dispatch checked whole-domain filters or bounded native tiles, publishing a result only after successful processing
/// and cancellation checks.
@@ -1390,7 +1399,12 @@ fn apply_tiled_with_impl(
// caller-supplied, so a short parameter set can ask for a huge source window per tile.
if let Halo::Radius(reach) = halo {
let (w, h) = (area.width().min(tile as u32) as usize, area.height().min(tile as u32) as usize);
let read_samples = w.checked_add(2 * reach as usize)?.checked_mul(h.checked_add(2 * reach as usize)?)?.checked_mul(fmt.channels())?;
// A Filter Gallery applies every effect to one working window. Beside the source read (`channels`
// samples per pixel) one effect holds the three-channel window plus its worst temporaries: two more
// three-channel copies and a plane or gradient set, 12 samples per pixel however many effects there
// are. An empty stack makes no working window (it returns the cropped source), so it is not charged.
let per_pixel = fmt.channels().checked_add(gallery_working_samples(params))?;
let read_samples = w.checked_add(2 * reach as usize)?.checked_mul(h.checked_add(2 * reach as usize)?)?.checked_mul(per_pixel)?;
let tile_count = u64::from(area.width()).div_ceil(tile as u64).checked_mul(u64::from(area.height()).div_ceil(tile as u64))?;
// Reject hostile source-window sizes before Image::read allocates them.
// Ordinary filter parameters and automatic tiles fit these bounds.
@@ -1771,3 +1785,6 @@ mod selected_alpha_tests {
}
}
}
#[cfg(test)]
mod paint_bucket_transparent_seed_tests;
+250 -7
View File
@@ -63,7 +63,11 @@ pub fn sample_stops(stops: &[(f32, [f32; 4])], t: f32) -> [f32; 4] {
}
}
/// Composites `color(i)` over `area` of the surface with per-pixel coverage.
/// Composites `color(i)` over `area` of the surface with per-pixel coverage. Where the coverage is
/// zero, `rim(x, y)` (when given) is the strength of a bucket's transparent-seed fringe there: the
/// pixel takes the fill at coverage `rim × (1 − α)`, its own transparency (#11354 review; see
/// [`bucket_fill_inks`]). Lock Transparent Pixels and Clear ignore it.
#[allow(clippy::too_many_arguments)]
fn composite_area(
s: &mut Surface,
area: Rect,
@@ -72,6 +76,7 @@ fn composite_area(
color: impl Fn(i32, i32) -> [f32; 4] + Sync,
lock_transparency: bool,
inks: Option<[f32; 4]>,
rim: Option<&(dyn Fn(i32, i32) -> f32 + Sync)>,
) {
// Composite tile by tile (in parallel on native); tiles with zero coverage are left untouched,
// so their copy-on-write storage (and GPU residency) survives.
@@ -86,8 +91,12 @@ fn composite_area(
let mut enc = [0.0f32; 8];
for y in r.y0..r.y1 {
for x in r.x0..r.x1 {
let k = coverage(x, y);
if k <= 0.0 {
let mut k = coverage(x, y);
let on_rim = match rim {
Some(rim) if k <= 0.0 && !lock_transparency && matches!(mode, FillMode::Blend(_)) => rim(x, y),
_ => 0.0,
};
if k <= 0.0 && on_rim <= 0.0 {
continue;
}
if !any {
@@ -97,6 +106,13 @@ fn composite_area(
let i = ((y - r.y0) as usize * w + (x - r.x0) as usize) * n;
let px = &mut region[i..i + n];
let d = photocraft_raster::to_rgba(&fmt, px);
if k <= 0.0 {
// A rim pixel: only its transparency takes the fill, so opaque art stays as it is.
k = on_rim * (1.0 - d[3].clamp(0.0, 1.0));
if k.is_nan() || k <= 0.0 {
continue;
}
}
let o = match mode {
FillMode::Blend(m) if lock_transparency => blend::composite_atop(m, d, color(x, y), k),
FillMode::Blend(m) => blend::composite(m, d, color(x, y), k),
@@ -166,6 +182,7 @@ pub fn paint_gradient(
},
lock_transparency,
None,
None,
);
}
@@ -227,9 +244,21 @@ pub fn bucket_fill_inks(
return false;
}
let img = crate::selection::rgba8_image(s, area);
let Some(region) = crate::selection::wand_region(&img, area, seed, tolerance, contiguous, anti_alias) else { return false };
let Some(region) = crate::selection::wand_region_bucket(&img, area, seed, tolerance, contiguous, anti_alias) else { return false };
// Clicked on transparency with Anti-alias on: the wand's fringe gives the partly transparent
// pixels around the flood no coverage (their alpha is measured against the seed's 0), but the
// reference application fills each pixel 8-adjacent to the flood at coverage `1 − α`, so the
// soft rim a cleared selection leaves rises partly (α 187 → 205, 56 → 211, 3 → 252; the
// reference gives 204, 211, 253) and a black edge pixel of alpha 40 filled green becomes
// (0, 248, 0, 221) (#11311, #12528, #11354 review). Pixels further out stay as they are; with
// Anti-alias off nothing outside the flood changes.
let at_seed = usize::try_from(seed.1 - area.y0)
.ok()
.and_then(|y| y.checked_mul(area.width() as usize))
.and_then(|row| row.checked_add(usize::try_from(seed.0 - area.x0).ok()?));
let empty_seed = anti_alias && at_seed.and_then(|i| img.get(i)).is_some_and(|p| p[3] == 0);
drop(img);
bucket_fill_region_inks(s, &region, opacity, selection, lock_transparency, src, inks, mode);
fill_region(s, &region, opacity, selection, lock_transparency, src, inks, mode, empty_seed);
true
}
@@ -258,9 +287,55 @@ pub fn bucket_fill_region_inks(
inks: Option<[f32; 4]>,
mode: FillMode,
) {
// Composite only over the filled region's box.
fill_region(s, region, opacity, selection, lock_transparency, src, inks, mode, false);
}
/// [`bucket_fill_region_inks`]; with `rim`, the pixels 8-adjacent to the region's fully covered
/// pixels that the region itself leaves uncovered take the fill at coverage `1 − α` (see
/// [`composite_area`]). Fully covered pixels and pixels further out are filled as without it.
#[allow(clippy::too_many_arguments)]
fn fill_region(
s: &mut Surface,
region: &crate::selection::Region,
opacity: f32,
selection: Option<&Surface>,
lock_transparency: bool,
src: impl Fn(i32, i32) -> [f32; 4] + Sync,
inks: Option<[f32; 4]>,
mode: FillMode,
rim: bool,
) {
// Composite only over the filled region's box (with Anti-alias on it already holds the
// one-pixel ring around the flood).
let b = region.bbox;
composite_area(s, b, mode, |x, y| region.at(x, y) * opacity * selection.map_or(1.0, |m| m.sample_channel(x, y, 0)), src, lock_transparency, inks);
let strength = |x: i32, y: i32| opacity * selection.map_or(1.0, |m| m.sample_channel(x, y, 0));
// The ring is only ever read for an unlocked Blend fill (see composite_area), and it is
// empty when the region covers every pixel, so do not build it in those cases: on a large
// canvas it is a bool per pixel plus a full scan of the mask.
let use_rim = rim && !lock_transparency && matches!(mode, FillMode::Blend(_)) && region.mask.contains(&0);
let ring = if use_rim { rim_ring(region) } else { Vec::new() };
let on_ring = |x: i32, y: i32| {
let at = usize::try_from(y - b.y0).ok().zip(usize::try_from(x - b.x0).ok()).filter(|_| b.contains(x, y));
let hit = at.and_then(|(yy, xx)| yy.checked_mul(b.width() as usize)?.checked_add(xx)).and_then(|i| ring.get(i));
if hit == Some(&true) { strength(x, y) } else { 0.0 }
};
let rim: Option<&(dyn Fn(i32, i32) -> f32 + Sync)> = if use_rim { Some(&on_ring) } else { None };
composite_area(s, b, mode, |x, y| region.at(x, y) * strength(x, y), src, lock_transparency, inks, rim);
}
/// Over `region.bbox` (row-major): the pixels the region leaves uncovered that touch one of its
/// fully covered pixels, diagonals included.
fn rim_ring(region: &crate::selection::Region) -> Vec<bool> {
let (w, h) = (region.bbox.width() as usize, region.bbox.height() as usize);
let full = |x: usize, y: usize| y.checked_mul(w).and_then(|r| r.checked_add(x)).and_then(|i| region.mask.get(i)) == Some(&255);
let n = w.saturating_mul(h).min(region.mask.len());
(0..n)
.map(|i| {
let (x, y) = (i % w, i / w);
region.mask.get(i) == Some(&0)
&& (y.saturating_sub(1)..=(y + 1).min(h - 1)).any(|ny| (x.saturating_sub(1)..=(x + 1).min(w - 1)).any(|nx| (nx, ny) != (x, y) && full(nx, ny)))
})
.collect()
}
#[cfg(test)]
@@ -349,6 +424,174 @@ mod tests {
assert_eq!(s.rgba(-4, 4), [0.0; 4], "outside the region");
}
/// #11311: flooding transparency with anti-aliasing fills the empty part of the region's edge
/// pixels at the fill's opacity, so the edge is no semi-transparent ring; opaque pixels the
/// anti-aliasing reaches blend exactly as before.
#[test]
fn bucket_on_transparency_fills_the_edge_pixels_in_full() {
let a = Rect::new(0, 0, 8, 1);
let white = |_, _| [1.0, 1.0, 1.0, 1.0];
for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] {
for (opaque, opacity) in [([1.0, 1.0, 1.0, 1.0], 1.0), ([1.0, 1.0, 1.0, 1.0], 0.5), ([1.0, 0.0, 0.0, 1.0], 1.0)] {
let mut s = Surface::new(fmt);
s.fill_rect(Rect::new(4, 0, 8, 1), &opaque);
// The same region filled without the rule: what the bucket used to do.
let mut before = s.clone();
let img = crate::selection::rgba8_image(&before, a);
let region = crate::selection::wand_region(&img, a, (1, 0), 0.0, true, true).unwrap();
bucket_fill_region_inks(&mut before, &region, opacity, None, false, white, None, FillMode::Blend(BlendMode::Normal));
assert!(bucket_fill_inks(&mut s, a, (1, 0), 0.0, true, true, opacity, None, false, white, None, FillMode::Blend(BlendMode::Normal)));
for x in 0..4 {
let p = s.rgba(x, 0);
assert!(p[..3].iter().all(|c| (c - 1.0).abs() < 1e-3) && (p[3] - opacity).abs() < 3e-3, "{fmt:?} {opaque:?} at {x}: {p:?}");
}
for x in 4..8 {
assert_eq!(s.rgba(x, 0), before.rgba(x, 0), "{fmt:?} {opaque:?} at {x}: opaque pixels blend as before");
}
assert!(s.rgba(4, 0)[3] > 0.999);
}
}
}
/// #11354 review: on a CMYK target, the edge pixels a transparent-seed bucket makes
/// opaque take the picked inks like the fully covered interior — their colour is the
/// fill alone. A partially transparent destination the fill touches keeps the blended
/// colour it already had, so its inks stay the conversion of that blend.
#[test]
fn bucket_on_transparency_writes_the_picked_cmyk_inks_at_the_edge() {
let a = Rect::new(0, 0, 8, 1);
let inks = [1.0f32, 0.0, 0.0, 0.0];
let rgb = photocraft_color::convert::cmyk_to_rgb(inks).map(|c| c.clamp(0.0, 1.0));
let src = |_, _| [rgb[0], rgb[1], rgb[2], 1.0];
let raw = |s: &Surface, x: i32| -> Vec<f32> { s.read_region(Rect::new(x, 0, x + 1, 1)).into_iter().take(4).collect() };
let is_inks = |got: &[f32]| got.iter().zip(inks).all(|(a, b)| (a - b).abs() < 0.02);
// Transparent run against opaque white: x=3 is the anti-aliased edge (coverage < 1).
let mut s = Surface::new(PixelFormat::CMYKA8);
s.fill_rect(Rect::new(4, 0, 8, 1), &[0.0, 0.0, 0.0, 0.0, 1.0]);
assert!(bucket_fill_inks(&mut s, a, (1, 0), 0.0, true, true, 1.0, None, false, src, Some(inks), FillMode::Blend(BlendMode::Normal)));
assert!(is_inks(&raw(&s, 1)), "fully covered: {:?}", raw(&s, 1));
assert!(is_inks(&raw(&s, 3)), "the made-opaque edge takes the picked inks: {:?}", raw(&s, 3));
// The edge pixel holds white at half alpha instead: the fill raises its alpha,
// but the colour is a blend, so the picked inks do not replace it.
let mut s = Surface::new(PixelFormat::CMYKA8);
s.fill_rect(Rect::new(3, 0, 4, 1), &[0.0, 0.0, 0.0, 0.0, 0.5]);
s.fill_rect(Rect::new(4, 0, 8, 1), &[0.0, 0.0, 0.0, 0.0, 1.0]);
assert!(bucket_fill_inks(&mut s, a, (1, 0), 0.0, true, true, 1.0, None, false, src, Some(inks), FillMode::Blend(BlendMode::Normal)));
let edge = s.rgba(3, 0);
assert!(edge[3] > 0.5, "the fill raised the partial pixel's alpha: {edge:?}");
assert!(!is_inks(&raw(&s, 3)), "a blended destination keeps its own encoding: {:?}", raw(&s, 3));
}
/// #11354 review: the soft rim a Clear leaves rises only partly. The reference application
/// takes rim alphas 187, 56 and 3 to 204, 211 and 253; the rule here, `α + (1 − α)²`, gives
/// 205, 211 and 252, each within one level of it (fit in PR #11354's review round). The rim pixel
/// sits on the bucket's anti-aliased fringe; the hole beside it fills in full and the opaque
/// art behind it is untouched by the rule.
#[test]
fn bucket_on_transparency_raises_the_cleared_rim_partly() {
let a = Rect::new(0, 0, 8, 1);
let white = |_, _| [1.0, 1.0, 1.0, 1.0];
let level = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as i32;
// (rim alpha after the Clear, the reference application's result, ours)
for (rim, reference, ours) in [(187, 204, 205), (56, 211, 211), (3, 253, 252)] {
for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] {
let mut s = Surface::new(fmt);
// The cleared hole is [0, 0, 0, 0] (Clear zeroes its RGB), the rim straight white.
s.fill_rect(Rect::new(4, 0, 5, 1), &[1.0, 1.0, 1.0, rim as f32 / 255.0]);
s.fill_rect(Rect::new(5, 0, 8, 1), &[1.0, 1.0, 1.0, 1.0]);
let before = s.clone();
assert!(bucket_fill_inks(&mut s, a, (1, 0), 32.0, true, true, 1.0, None, false, white, None, FillMode::Blend(BlendMode::Normal)));
let p = s.rgba(4, 0);
assert_eq!(level(p[3]), ours, "{fmt:?}: rim {rim} -> {p:?}");
assert!((ours - reference).abs() <= 1, "rim {rim}: ours {ours}, reference {reference}");
assert!(p[..3].iter().all(|c| (c - 1.0).abs() < 1e-3), "{fmt:?}: the rim stays white: {p:?}");
for x in 0..4 {
assert_eq!(level(s.rgba(x, 0)[3]), 255, "{fmt:?} rim {rim}: the hole and its edge fill in full at {x}");
}
for x in 5..8 {
assert_eq!(s.rgba(x, 0), before.rgba(x, 0), "{fmt:?} rim {rim}: opaque art at {x}");
}
}
}
}
/// The rule is for a click on transparency only: from an opaque seed the fill is exactly the
/// wand region's own fill, fringe included.
#[test]
fn bucket_on_opaque_pixels_keeps_neighbouring_transparency() {
let a = Rect::new(0, 0, 8, 1);
let mut s = Surface::new(PixelFormat::RGBA8);
s.fill_rect(Rect::new(0, 0, 4, 1), &[1.0, 1.0, 1.0, 0.5]);
s.fill_rect(Rect::new(4, 0, 8, 1), &[1.0, 1.0, 1.0, 1.0]);
let mut plain = s.clone();
let img = crate::selection::rgba8_image(&plain, a);
let region = crate::selection::wand_region(&img, a, (6, 0), 0.0, true, true).unwrap();
bucket_fill_region_inks(&mut plain, &region, 1.0, None, false, |_, _| [1.0; 4], None, FillMode::Blend(BlendMode::Normal));
assert!(bucket_fill_inks(&mut s, a, (6, 0), 0.0, true, true, 1.0, None, false, |_, _| [1.0; 4], None, FillMode::Blend(BlendMode::Normal)));
for x in 0..8 {
assert_eq!(s.rgba(x, 0), plain.rgba(x, 0), "at {x}");
}
assert!((s.rgba(1, 0)[3] - 0.5).abs() < 0.01, "outside the region");
}
/// #12528 (reference application, transparent seed, Tolerance 32, Anti-alias on): a black
/// edge pixel bordering the filled area takes the green fill at coverage `1 − α`, so its
/// colour as well as its alpha follow the reference within two levels; with Anti-alias off it
/// stays as it was.
#[test]
fn bucket_on_transparency_blends_into_the_edge_at_its_transparency() {
let a = Rect::new(0, 0, 8, 1);
let green = |_, _| [0.0, 1.0, 0.0, 1.0];
let level = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as i32;
// (edge alpha, the reference application's result)
for (alpha, reference) in [(40, [0, 248, 0, 221]), (112, [0, 190, 0, 192]), (197, [0, 70, 0, 210]), (253, [0, 2, 0, 253])] {
for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] {
for anti_alias in [true, false] {
let mut s = Surface::new(fmt);
s.fill_rect(Rect::new(4, 0, 5, 1), &[0.0, 0.0, 0.0, alpha as f32 / 255.0]);
s.fill_rect(Rect::new(5, 0, 8, 1), &[0.0, 0.0, 0.0, 1.0]);
let before = s.clone();
assert!(bucket_fill_inks(&mut s, a, (1, 0), 32.0, true, anti_alias, 1.0, None, false, green, None, FillMode::Blend(BlendMode::Normal)));
let got = s.rgba(4, 0).map(level);
if anti_alias {
assert!(got.iter().zip(reference).all(|(g, r)| (g - r).abs() <= 2), "{fmt:?} alpha {alpha}: {got:?}, reference {reference:?}");
} else {
assert_eq!(s.rgba(4, 0), before.rgba(4, 0), "{fmt:?} alpha {alpha}: Anti-alias off leaves the edge");
}
for x in 5..8 {
assert_eq!(s.rgba(x, 0), before.rgba(x, 0), "{fmt:?} alpha {alpha}: opaque art at {x}");
}
}
}
}
}
/// #11354 review (measured in the reference application with a 4 px feathered rim): only the
/// one-pixel ring 8-adjacent to the flood rises, diagonals included; pixels further out keep
/// their alpha exactly.
#[test]
fn bucket_on_transparency_raises_only_the_ring_next_to_the_flood() {
let a = Rect::new(0, 0, 7, 7);
let mut s = Surface::new(PixelFormat::RGBA8);
s.fill_rect(a, &[1.0, 1.0, 1.0, 100.0 / 255.0]);
s.fill_rect(Rect::new(3, 3, 4, 4), &[0.0; 4]);
let before = s.clone();
assert!(bucket_fill_inks(&mut s, a, (3, 3), 32.0, true, true, 1.0, None, false, |_, _| [1.0; 4], None, FillMode::Blend(BlendMode::Normal)));
for y in 0..7 {
for x in 0..7 {
let (p, was) = (s.rgba(x, y), before.rgba(x, y));
match ((x - 3).abs(), (y - 3).abs()) {
(0, 0) => assert!(p[3] > 0.999, "the flood at ({x},{y}): {p:?}"),
(dx, dy) if dx <= 1 && dy <= 1 => assert!(p[3] > was[3] + 0.1, "the ring at ({x},{y}) rises: {p:?}"),
_ => assert_eq!(p, was, "beyond the ring at ({x},{y})"),
}
}
}
}
/// The Paint Bucket's Mode: Multiply darkens what's there, Clear erases it (half at 50 %).
#[test]
fn bucket_mode_blends_or_clears() {
@@ -0,0 +1,185 @@
//! Paint Bucket match rule for transparent and semi-transparent seeds (#12529), Anti-alias off.
//!
//! The issue's document is 800 x 600 with a red square, a grey ramp and an anti-aliased black
//! disc on a transparent layer. These tests shrink the geometry (see `NOTES.md`) but keep every
//! measured relationship: the ramp is its own 256 x 1 strip so seeds 127 / 128 and the tolerances
//! 0 / 1 / 32 / 100 give the issue's exact grey ranges.
use photocraft_color::{PixelFormat, blend::BlendMode};
use photocraft_geom::Rect;
use photocraft_raster::Surface;
use crate::paint::{FillMode, bucket_fill_inks};
use crate::selection::{rgba8_image, wand_region};
const W: i32 = 100;
const H: i32 = 80;
const AREA: Rect = Rect { x0: 0, y0: 0, x1: W, y1: H };
const GREEN: [u8; 4] = [0, 255, 0, 255];
/// Row of the black disc's edge; x 10..14 hold alpha 40, 112, 197, 253 like (690..700, 400).
const EDGE_Y: i32 = 70;
/// Alphas of the edge row from x = 10: the 40 / 112 / 197 / 253 ladder, then solid 253 inside.
const LADDER: [u8; 4] = [40, 112, 197, 253];
fn idx(x: i32, y: i32) -> usize {
(y * W + x) as usize
}
/// Red square (40..60 x 10..30), grey ramp strip (value = x, x 0..64, y 40..43), and a black
/// edge row at `EDGE_Y` (x 10..14 the alpha ladder, x 14..30 alpha 253); the rest transparent.
fn document() -> Vec<[u8; 4]> {
let mut img = vec![[0u8; 4]; (W * H) as usize];
for y in 10..30 {
for x in 40..60 {
img[idx(x, y)] = [220, 30, 30, 255];
}
}
for y in 40..43 {
for x in 0..64 {
img[idx(x, y)] = [x as u8, x as u8, x as u8, 255];
}
}
for (i, a) in LADDER.iter().enumerate() {
img[idx(10 + i as i32, EDGE_Y)] = [0, 0, 0, *a];
}
for x in 14..30 {
img[idx(x, EDGE_Y)] = [0, 0, 0, 253];
}
img
}
fn surface(img: &[[u8; 4]], area: Rect) -> Surface {
let bytes: Vec<u8> = img.iter().flatten().copied().collect();
Surface::from_interleaved(PixelFormat::RGBA8, area, &bytes)
}
/// Bucket-fills green at `seed` and returns the layer afterwards.
fn fill(img: &[[u8; 4]], area: Rect, seed: (i32, i32), tol: f32, contiguous: bool) -> Vec<[u8; 4]> {
let mut s = surface(img, area);
let green = |_, _| [0.0, 1.0, 0.0, 1.0];
assert!(bucket_fill_inks(&mut s, area, seed, tol, contiguous, false, 1.0, None, false, green, None, FillMode::Blend(BlendMode::Normal)));
rgba8_image(&s, area)
}
/// Pixels the fill changed, as indices.
fn changed(before: &[[u8; 4]], after: &[[u8; 4]]) -> Vec<usize> {
(0..before.len()).filter(|&i| before[i] != after[i]).collect()
}
fn assert_only_alpha_zero_filled(before: &[[u8; 4]], after: &[[u8; 4]], label: &str) {
for (i, (b, a)) in before.iter().zip(after).enumerate() {
if b[3] == 0 {
assert_eq!(*a, GREEN, "{label}: transparent pixel {i} should fill");
} else {
assert_eq!(a, b, "{label}: pixel {i} with alpha {} must not change", b[3]);
}
}
}
#[test]
fn transparent_seed_fills_only_alpha_zero_at_any_tolerance() {
let img = document();
for tol in [0.0, 32.0, 255.0] {
let after = fill(&img, AREA, (49, 49), tol, true);
// The transparent background is one connected region here, so contiguous reaches all of it.
assert_only_alpha_zero_filled(&img, &after, &format!("tolerance {tol}"));
for (i, a) in LADDER.iter().enumerate() {
assert_eq!(after[idx(10 + i as i32, EDGE_Y)], [0, 0, 0, *a], "edge alpha {a} survives at tolerance {tol}");
}
assert_eq!(after[idx(9, EDGE_Y)], GREEN, "transparent pixel next to the edge fills");
assert_eq!(after[idx(50, 20)], [220, 30, 30, 255], "red square untouched at tolerance {tol}");
assert_eq!(after[idx(30, 41)], [30, 30, 30, 255], "ramp untouched at tolerance {tol}");
}
}
#[test]
fn transparent_seed_not_contiguous_fills_every_alpha_zero_pixel_only() {
let mut img = document();
// An enclosed transparent hole that contiguous fill cannot reach.
for y in 12..16 {
for x in 44..48 {
img[idx(x, y)] = [0, 0, 0, 0];
}
}
for tol in [0.0, 255.0] {
let after = fill(&img, AREA, (49, 49), tol, false);
assert_only_alpha_zero_filled(&img, &after, &format!("global, tolerance {tol}"));
assert_eq!(after[idx(45, 13)], GREEN, "the enclosed transparent hole fills");
assert_eq!(after[idx(13, EDGE_Y)], [0, 0, 0, 253]);
}
}
#[test]
fn semi_transparent_seed_takes_same_colour_pixels_of_any_nonzero_alpha() {
let img = document();
let after = fill(&img, AREA, (10, EDGE_Y), 32.0, true);
// The 40 / 112 / 197 / 253 ladder and the solid 253 run all fill: no alpha band applies.
for x in 10..30 {
assert_eq!(after[idx(x, EDGE_Y)], GREEN, "black alpha pixel at x {x} fills");
}
assert_eq!(after[idx(9, EDGE_Y)], [0, 0, 0, 0], "the transparent neighbour is never taken");
let n = changed(&img, &after).len();
assert_eq!(n, 20, "only the black edge row changes");
}
#[test]
fn semi_transparent_seed_respects_colour_tolerance() {
let mut img = document();
img[idx(12, EDGE_Y)] = [100, 100, 100, 112];
let after = fill(&img, AREA, (10, EDGE_Y), 32.0, true);
assert_eq!(after[idx(11, EDGE_Y)], GREEN);
assert_eq!(after[idx(12, EDGE_Y)], [100, 100, 100, 112], "a colour outside Tolerance stays");
assert_eq!(after[idx(13, EDGE_Y)], [0, 0, 0, 253], "and it cuts the contiguous run");
}
fn ramp_strip() -> Vec<[u8; 4]> {
(0..=255u8).map(|v| [v, v, v, 255]).collect()
}
fn filled_range(after: &[[u8; 4]]) -> Option<(usize, usize)> {
let hits: Vec<usize> = (0..after.len()).filter(|&i| after[i] == GREEN).collect();
Some((*hits.first()?, *hits.last()?))
}
#[test]
fn opaque_seed_keeps_todays_ranges() {
let img = ramp_strip();
let area = Rect::new(0, 0, 256, 1);
for (seed, tol, contiguous, lo, hi) in
[(127, 0.0, true, 127, 127), (127, 1.0, true, 126, 128), (127, 32.0, true, 95, 159), (127, 100.0, true, 27, 227), (128, 32.0, false, 96, 160)]
{
let after = fill(&img, area, (seed, 0), tol, contiguous);
assert_eq!(filled_range(&after), Some((lo, hi)), "seed {seed} tolerance {tol} contiguous {contiguous}");
let count = after.iter().filter(|p| **p == GREEN).count();
assert_eq!(count, hi - lo + 1, "the filled range is solid");
}
}
/// The wand rule did not move: a transparent seed at Tolerance 255 still matches every pixel
/// (alpha within 255 of 0), which is what the Magic Wand and Magic Eraser keep.
#[test]
fn wand_region_still_uses_the_alpha_band() {
let img = document();
let r = wand_region(&img, AREA, (49, 49), 255.0, true, false).expect("seed is inside");
assert_eq!(r.at(50, 20), 1.0, "red square is inside the wand region");
assert_eq!(r.at(30, 41), 1.0, "ramp is inside the wand region");
assert_eq!(r.at(13, EDGE_Y), 1.0, "disc edge is inside the wand region");
let r32 = wand_region(&img, AREA, (49, 49), 32.0, true, false).expect("seed is inside");
assert_eq!(r32.at(10, EDGE_Y), 0.0, "alpha 40 is outside a Tolerance 32 alpha band");
assert_eq!(r32.at(9, EDGE_Y), 1.0, "alpha 0 is inside it");
}
/// The measured semi-transparent case is one colour at several alphas; a pixel of another
/// colour keeps the alpha band the shared rule always had (the issue leaves unmeasured
/// cases at today's behaviour). Seed [0, 0, 0, 40] at x = 1: x = 0 is the same-colour
/// control [0, 0, 0, 253]; x = 2 is [20, 20, 20, 197] — its RGB distance of 20 passes
/// Tolerance 32, but its alpha sits 157 from the seed's, so it is not filled.
#[test]
fn semi_transparent_seed_keeps_the_alpha_band_for_other_colours() {
let img: Vec<[u8; 4]> = vec![[0, 0, 0, 253], [0, 0, 0, 40], [20, 20, 20, 197]];
let area = Rect::new(0, 0, 3, 1);
let after = fill(&img, area, (1, 0), 32.0, true);
let ch = changed(&img, &after);
assert!(ch.contains(&0), "same-colour pixel of alpha 253 fills");
assert!(!ch.contains(&2), "different-colour pixel of alpha 197 keeps the alpha band");
}
+10
View File
@@ -896,6 +896,16 @@ mod tests {
assert!(pv.vignette[0].abs() < 0.1, "{pv:?}");
}
#[test]
fn an_oversized_level_count_is_clamped() {
let (w, h) = (4, 4);
let img = RoiImage { x0: 0, y0: 0, w, h, ch: 1, px: vec![0.5; w * h], alpha: vec![1.0; w * h] };
let (out, cov) = multiband(w, h, &[img], &[vec![1.0; w * h]], usize::MAX).unwrap();
assert_eq!(out.len(), w * h);
assert!(out.iter().all(|v| (v - 0.5).abs() < 1e-5), "{out:?}");
assert!(cov.iter().all(|c| *c == 1.0), "{cov:?}");
}
#[test]
fn seams_avoid_disagreement_and_multiband_is_seamless() {
// Two images over a 120×40 strip, overlapping in 40..80; the second has a bright
+2 -1
View File
@@ -555,7 +555,8 @@ pub fn try_multiband_batched(
max_band_budget: Option<usize>,
) -> Result<(Vec<f32>, Vec<f32>), AllocationError> {
let ch = imgs.first().map_or(1, |i| i.ch);
let levels = levels.max(1);
// Levels past 1x1 add nothing; 16 (real use is at most 7) keeps the shifts below in range.
let levels = levels.clamp(1, 16);
let cost = |im: &RoiImage| band_cost(im, w, h, ch, levels);
if let Some(limit) = max_band_budget {
for im in imgs {
+2 -2
View File
@@ -167,8 +167,8 @@ impl BandChol {
let n = a.diag.len();
let mut p = 0;
for i in 0..n {
for k in a.rows[i]..a.rows[i + 1] {
p = p.max(i.abs_diff(a.cols[k]));
for &j in &a.cols[a.rows[i]..a.rows[i + 1]] {
p = p.max(i.abs_diff(j));
}
}
if n.saturating_mul(p + 1) > 40_000_000 {
+4
View File
@@ -233,6 +233,10 @@ fn weights(o: i32, scale: f64, kernel: Kernel, edge: Option<(i32, i32)>, content
let sum: f64 = w.iter().sum();
(lo, w.into_iter().map(|v| v / sum).collect())
}
// Past a 4096x reduction Lanczos would build a weight per covered source sample (tens of
// thousands per output sample); the box average that folds the beyond-edge weights is
// indistinguishable at that scale.
Kernel::Lanczos if scale < 1.0 / 4096.0 => weights(o, scale, Kernel::Bilinear, edge, content),
Kernel::Lanczos => {
let stretch = if scale < 1.0 { 1.0 / scale } else { 1.0 };
let u = (o as f64 + 0.5) / scale - 0.5;
+12
View File
@@ -401,6 +401,18 @@ fn nearest_neighbor_reduction_does_not_average() {
}
}
/// A reduction beyond 4096x by Lanczos must not build a weight per covered source sample.
#[test]
fn a_tiny_lanczos_scale_keeps_its_weights_small() {
let scale = 1.0e-7;
for o in [0, 1] {
let (_, w) = weights(o, scale, Kernel::Lanczos, None, (0, 63));
assert!(w.len() < 1024, "{} weights", w.len());
let sum: f64 = w.iter().sum();
assert!((sum - 1.0).abs() < 1e-6, "{sum}");
}
}
/// #9212: a bicubic shrink of both axes stores the image between its passes with one axis at the
/// old size, larger than the result; single-pass filters and growing axes store nothing larger.
#[test]
+28 -47
View File
@@ -194,10 +194,22 @@ pub fn local_blur(data: &[f32], w: usize, h: usize, ch: usize, alpha: Option<usi
out
}
/// Unsharp-mask sharpen of a window (same layout as [`local_blur`]): `v + amount·(v − blur(v))`.
/// Gaussian sigma of the Protect Detail sharpen in [`local_sharpen`], in pixels. It reads
/// `ceil(3·sigma)` pixels around each output pixel.
pub const PROTECT_DETAIL_SIGMA: f32 = 2.5;
/// Largest change one Protect Detail pass makes to a pixel (6 levels of 255), approached softly.
const PROTECT_DETAIL_LIMIT: f32 = 6.0 / 255.0;
/// Unsharp-mask sharpen of a window (same layout as [`local_blur`]): `v + amount·d` with
/// `d = v − blur(v)`, against a Gaussian of sigma 1.
///
/// With `protect_detail`, each result is clamped to the 3×3 neighbourhood's range, which stops
/// halos and noise from being amplified into new extremes. Alpha is left unchanged.
/// With `protect_detail` (the Sharpen tool's default) it sharpens gently, as the reference
/// application does (#12938): the Gaussian is wider ([`PROTECT_DETAIL_SIGMA`]), so an edge gets a
/// low halo a few pixels wide, and each difference is soft-limited to `L` = 6 levels:
/// `v + amount·d·L/(L + |d|)`. Fine detail is sharpened about as much as without it, while a
/// strong edge or a lone bright pixel moves by less than `L` per pass instead of overshooting.
/// Uniform areas stay exactly as they are. Alpha is left unchanged.
#[allow(clippy::too_many_arguments)]
pub fn local_sharpen(
data: &[f32],
@@ -214,7 +226,7 @@ pub fn local_sharpen(
if ow == 0 || oh == 0 || ch == 0 {
return Vec::new();
}
let blur = local_blur(data, w, h, ch, alpha, win, 1.0);
let blur = local_blur(data, w, h, ch, alpha, win, if protect_detail { PROTECT_DETAIL_SIGMA } else { 1.0 });
let mut out = vec![0.0f32; blur.len()];
let stride = ow * ch;
@@ -223,53 +235,18 @@ pub fn local_sharpen(
let y = y0 + oy;
let blur_row = &blur[oy * stride..(oy + 1) * stride];
for ox in 0..ow {
let x = x0 + ox;
let o = ox * ch;
let i = (y * w + x) * ch;
// Precompute 3×3 neighbourhood offsets once per pixel if protecting detail.
let offsets: [usize; 8] = if protect_detail {
if x > 0 && x + 1 < w && y > 0 && y + 1 < h {
let w_ch = w * ch;
[i - w_ch - ch, i - w_ch, i - w_ch + ch, i - ch, i + ch, i + w_ch - ch, i + w_ch, i + w_ch + ch]
} else {
let get_n_idx = |dx: i32, dy: i32| -> usize {
let nx = (x as i32 + dx).clamp(0, w as i32 - 1) as usize;
let ny = (y as i32 + dy).clamp(0, h as i32 - 1) as usize;
(ny * w + nx) * ch
};
[
get_n_idx(-1, -1),
get_n_idx(0, -1),
get_n_idx(1, -1),
get_n_idx(-1, 0),
get_n_idx(1, 0),
get_n_idx(-1, 1),
get_n_idx(0, 1),
get_n_idx(1, 1),
]
}
} else {
[0; 8]
};
let i = (y * w + x0 + ox) * ch;
for c in 0..ch {
let v = data[i + c];
if Some(c) == alpha {
dst[o + c] = v;
continue;
}
let mut s = v + amount * (v - blur_row[o + c]);
if protect_detail {
let (mut lo, mut hi) = (v, v);
for &n_idx in &offsets {
let nv = data[n_idx + c];
lo = lo.min(nv);
hi = hi.max(nv);
}
s = s.clamp(lo, hi);
}
dst[o + c] = s;
let d = v - blur_row[o + c];
// `PROTECT_DETAIL_LIMIT + |d|` is never zero, so this can't divide by zero.
let d = if protect_detail { d * PROTECT_DETAIL_LIMIT / (PROTECT_DETAIL_LIMIT + d.abs()) } else { d };
dst[o + c] = v + amount * d;
}
}
};
@@ -287,6 +264,10 @@ pub fn local_sharpen(
out
}
#[cfg(test)]
#[path = "retouch_sharpen_protect_detail_tests.rs"]
mod sharpen_protect_detail_tests;
#[cfg(test)]
mod tests {
use super::*;
@@ -353,10 +334,10 @@ mod tests {
let s = local_sharpen(&smooth, w, h, ch, None, win, 1.0, false);
let so: Vec<f32> = (4..12).flat_map(|y| (4..12).map(move |x| (x, y))).map(|(x, y)| smooth[y * w + x]).collect();
assert!(var(&s) > var(&so));
// Protect detail never exceeds the local range.
// Protect Detail sharpens too, but moves no pixel by more than its limit per unit of amount.
let sp = local_sharpen(&smooth, w, h, ch, None, win, 4.0, true);
let hi = smooth.iter().cloned().fold(0.0, f32::max);
assert!(sp.iter().all(|v| *v <= hi + 1e-6));
assert!(var(&sp) > var(&so));
assert!(sp.iter().zip(&so).all(|(a, b)| (a - b).abs() < 4.0 * PROTECT_DETAIL_LIMIT));
}
#[test]
@@ -0,0 +1,125 @@
//! Protect Detail in [`local_sharpen`] sharpens gently instead of changing nothing (#12938).
//!
//! The issue's test document, in RGBA like the Sharpen tool's dabs: a grey 96 | grey 160 step and
//! a lone grey-160 pixel in grey 96. A hard 40 px tip covers each test image, so one click is one
//! pass that moves every pixel by `strength · (sharpened − original)`, rounded to 8 bits. The
//! expected values are the reference application's, with the issue's tolerances.
use super::*;
const W: usize = 60;
const H: usize = 21;
/// x of the first grey-160 column of the step: x 400 in the issue's document.
const EDGE: usize = 30;
fn grey(levels: impl Fn(usize, usize) -> f32, w: usize, h: usize) -> Vec<f32> {
(0..w * h)
.flat_map(|i| {
let g = levels(i % w, i / w) / 255.0;
[g, g, g, 1.0]
})
.collect()
}
fn step() -> Vec<f32> {
grey(|x, _| if x < EDGE { 96.0 } else { 160.0 }, W, H)
}
/// One pass of the tool at `strength` (0–1) over the whole image.
fn pass(data: &[f32], w: usize, h: usize, strength: f32, protect: bool) -> Vec<f32> {
let out = local_sharpen(data, w, h, 4, Some(3), (0, 0, w, h), 1.0, protect);
data.iter().zip(&out).map(|(v, s)| v + (s - v) * strength).collect()
}
/// Red channel of each pixel in 8-bit levels.
fn levels(data: &[f32]) -> Vec<i32> {
data.as_chunks::<4>().0.iter().map(|p| (p[0].clamp(0.0, 1.0) * 255.0).round() as i32).collect()
}
/// Row `y` of the step, as x 400 + `x - EDGE` in the issue's document.
fn at(row: &[i32], x_doc: usize) -> i32 {
row[x_doc + EDGE - 400]
}
#[test]
fn one_click_on_the_step_gives_a_low_halo_that_follows_strength() {
let row = |strength: f32| levels(&pass(&step(), W, H, strength, true))[H / 2 * W..(H / 2 + 1) * W].to_vec();
let full = row(1.0);
for (x, want) in [(396, 94), (397, 92), (398, 91), (399, 90), (400, 164), (401, 163), (402, 161)] {
assert!((at(&full, x) - want).abs() <= 3, "100 %: x {x} is {}, want {want} ± 3 ({full:?})", at(&full, x));
}
let half = row(0.5);
assert!((at(&half, 399) - 93).abs() <= 2 && (at(&half, 400) - 162).abs() <= 2, "50 %: x 399/400 = {}/{}", at(&half, 399), at(&half, 400));
let quarter = row(0.25);
assert!(at(&quarter, 399) < 96 && at(&quarter, 400) > 160, "25 % still sharpens: {}/{}", at(&quarter, 399), at(&quarter, 400));
assert_eq!(row(0.01), levels(&step())[..W].to_vec(), "1 % changes nothing");
}
#[test]
fn one_click_on_a_lone_pixel_lifts_it_gently() {
let (w, h, c) = (41, 41, 20);
let data = grey(|x, y| if (x, y) == (c, c) { 160.0 } else { 96.0 }, w, h);
let on = levels(&pass(&data, w, h, 1.0, true));
let off = levels(&pass(&data, w, h, 1.0, false));
let centre = on[c * w + c];
assert!((centre - 168).abs() <= 4, "centre {centre}, want 168 ± 4");
for (dx, dy) in [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)] {
let n = on[(c as i32 + dy) as usize * w + (c as i32 + dx) as usize];
assert!((93..=95).contains(&n), "neighbour ({dx}, {dy}) is {n}, want 93–95");
}
let (lift_on, lift_off) = (centre - 160, off[c * w + c] - 160);
assert!(lift_on > 0 && 2 * lift_on < lift_off, "Protect Detail lifts {lift_on}, off lifts {lift_off}");
}
#[test]
fn uniform_areas_and_alpha_stay_exactly_as_they_were() {
for g in [0.0, 96.0 / 255.0, 0.5, 1.0, 2.5] {
let data: Vec<f32> = (0..12 * 9).flat_map(|i| [g, g * 0.5, 0.25, [0.0, 0.3, 1.0][i % 3]]).collect();
let out = local_sharpen(&data, 12, 9, 4, Some(3), (0, 0, 12, 9), 1.0, true);
for (o, d) in out.as_chunks::<4>().0.iter().zip(data.as_chunks::<4>().0) {
assert_eq!(o[3].to_bits(), d[3].to_bits(), "alpha unchanged");
}
let opaque: Vec<f32> = (0..12 * 9).flat_map(|_| [g, g * 0.5, 0.25, 1.0]).collect();
let out = local_sharpen(&opaque, 12, 9, 4, Some(3), (2, 2, 10, 7), 1.0, true);
// Exact up to the Gaussian weights' rounding, far below one 16-bit level.
let same = |a: f32, b: f32| (a - b).abs() < 1e-6;
assert!(out.as_chunks::<4>().0.iter().all(|p| same(p[0], g) && same(p[1], g * 0.5) && same(p[2], 0.25)), "grey {g} unchanged: {out:?}");
}
}
#[test]
fn three_drags_widen_the_halo_without_ringing_between_black_and_white() {
// A drag of a 40 px tip at 25 % spacing passes about four dabs over each point; each drag is a
// stroke of its own, rounded to 8 bits when it ends.
let (w, h, edge) = (80, 5, 40);
let mut data = grey(|x, _| if x < edge { 96.0 } else { 160.0 }, w, h);
let mut halos = Vec::new();
for _ in 0..3 {
for _ in 0..4 {
data = pass(&data, w, h, 1.0, true);
}
data = data.iter().map(|v| (v.clamp(0.0, 1.0) * 255.0).round() / 255.0).collect();
let row = levels(&data)[h / 2 * w..(h / 2 + 1) * w].to_vec();
halos.push(row.iter().enumerate().filter(|&(x, &v)| (v - if x < edge { 96 } else { 160 }).abs() >= 2).count());
// Darker towards the edge on the dark side, lighter towards it on the light side.
for x in edge - 5..edge - 1 {
assert!(row[x] >= row[x + 1], "dark side rises at x {x}: {row:?}");
}
for x in edge..edge + 4 {
assert!(row[x] >= row[x + 1], "light side rises at x {x}: {row:?}");
}
assert!(row[..edge].iter().all(|&v| v < 255) && row[edge..].iter().all(|&v| v > 0), "no black–white ringing: {row:?}");
}
assert!(halos[0] < halos[2], "the halo widens with every drag: {halos:?}");
}
#[test]
fn protect_detail_off_keeps_the_sigma_1_unsharp_mask() {
let data = step();
let blur = local_blur(&data, W, H, 4, Some(3), (0, 0, W, H), 1.0);
let out = local_sharpen(&data, W, H, 4, Some(3), (0, 0, W, H), 1.0, false);
for (i, ((v, b), o)) in data.iter().zip(&blur).zip(&out).enumerate() {
let want = if i % 4 == 3 { *v } else { v + (v - b) };
assert_eq!(o.to_bits(), want.to_bits(), "sample {i}");
}
}
+53 -1
View File
@@ -354,6 +354,51 @@ pub fn wand_region_by(
anti_alias: bool,
within: Option<&[f32]>,
distance: ColorDistance,
) -> Option<Region> {
region_by_rule(img, area, seed, tolerance, contiguous, anti_alias, within, distance, MatchRule::Wand)
}
/// Which pixels count as "similar" to the clicked one.
#[derive(Clone, Copy)]
enum MatchRule {
/// The Magic Wand / Magic Eraser rule: colour and alpha each within Tolerance, except that
/// two fully transparent pixels always match.
Wand,
/// The Paint Bucket rule, see [`wand_region_bucket`].
Bucket,
}
/// [`wand_region`] with the Paint Bucket's match rule (measured on the reference, #12529).
///
/// It differs from the wand rule only when the clicked pixel is not opaque. The wand lets any
/// pixel whose alpha is within Tolerance of the seed's match, so a bucket click on transparent
/// pixels leaked into the anti-aliased edge of a shape and, at Tolerance 255, filled the whole
/// layer (#6183). The bucket rule is:
/// - seed alpha 0: only pixels with alpha 0 match; Tolerance plays no role;
/// - seed alpha 255: unchanged (colour and alpha each within Tolerance);
/// - seed alpha in between: a pixel of the seed's own colour matches when its alpha is
/// above 0 — there is no alpha band for that case (alpha 40, 197 and 253 of one colour
/// were measured to fill together). A pixel of any other colour must also sit within
/// Tolerance of the seed's alpha, as before: that case was not measured, and unmeasured
/// cases keep today's behaviour. Alpha-0 pixels are never taken.
///
/// Only that one semi-transparent case was measured; unmeasured cases keep the previous
/// behaviour. [`wand_region`] and [`wand_region_within`] are unchanged.
pub(crate) fn wand_region_bucket(img: &[[u8; 4]], area: Rect, seed: (i32, i32), tolerance: f32, contiguous: bool, anti_alias: bool) -> Option<Region> {
region_by_rule(img, area, seed, tolerance, contiguous, anti_alias, None, ColorDistance::LargestChannel, MatchRule::Bucket)
}
#[allow(clippy::too_many_arguments)]
fn region_by_rule(
img: &[[u8; 4]],
area: Rect,
seed: (i32, i32),
tolerance: f32,
contiguous: bool,
anti_alias: bool,
within: Option<&[f32]>,
distance: ColorDistance,
rule: MatchRule,
) -> Option<Region> {
if !area.contains(seed.0, seed.1) {
return None;
@@ -371,7 +416,14 @@ pub fn wand_region_by(
let tol = (tolerance + 1e-3).floor().max(0.0) as i32;
// Copying a selection can leave RGB under zero alpha. Those invisible colours must not
// split a transparent region into pieces, as if the copied source were still visible.
let similar = |p: [u8; 4]| (target[3] == 0 && p[3] == 0) || (distance.rgb(p, target) <= tol && (p[3] as i32 - target[3] as i32).abs() <= tol);
let similar = |p: [u8; 4]| match rule {
MatchRule::Wand => (target[3] == 0 && p[3] == 0) || (distance.rgb(p, target) <= tol && (p[3] as i32 - target[3] as i32).abs() <= tol),
MatchRule::Bucket => match target[3] {
0 => p[3] == 0,
255 => distance.rgb(p, target) <= tol && (p[3] as i32 - 255).abs() <= tol,
_ => p[3] != 0 && distance.rgb(p, target) <= tol && (p[..3] == target[..3] || (p[3] as i32 - target[3] as i32).abs() <= tol),
},
};
let mut marks = vec![0u8; w * h];
let (mut bx0, mut by0, mut bx1, mut by1) = (usize::MAX, usize::MAX, 0usize, 0usize);
if contiguous {
+12 -2
View File
@@ -210,6 +210,15 @@ pub fn convex_quad(q: &[[f64; 2]; 4]) -> bool {
area2.is_finite() && area2 >= 2.0
}
/// Is `q` (TL, TR, BR, BL) a usable Perspective Crop quad in either winding: a [`convex_quad`],
/// or one wound counter-clockwise on screen (a box flipped on one axis, which crops mirrored,
/// #12489) with the same corner angle and area limits? Self-crossing, degenerate and non-finite
/// quads are not. The Perspective Warp steps keep the clockwise-only [`convex_quad`].
pub fn convex_quad_any_winding(q: &[[f64; 2]; 4]) -> bool {
let [tl, tr, br, bl] = *q;
convex_quad(q) || convex_quad(&[tl, bl, br, tr])
}
/// The output size a Perspective Crop of `q` (TL, TR, BR, BL) takes when none is given: the mean
/// length of the top and bottom edges by the mean length of the left and right edges.
pub fn quad_size(q: &[[f64; 2]; 4]) -> (f64, f64) {
@@ -287,9 +296,10 @@ fn perspective_estimate(q: &[[f64; 2]; 4], [cx, cy]: [f64; 2]) -> Option<(f64, f
/// The homography taking the quad `q` (TL, TR, BR, BL) onto the rectangle (0, 0)–(`w`, `h`), corner
/// to corner: the inverse of the unit-square-to-quad map (Heckbert, "Fundamentals of Texture
/// Mapping and Image Warping", 1989, §2.2.3) scaled to the rectangle. `None` when `q` isn't a
/// [`convex_quad`] or the size isn't positive.
/// [`convex_quad_any_winding`] or the size isn't positive. A quad wound counter-clockwise maps
/// mirrored, each corner onto its own rectangle corner.
pub fn quad_to_rect(q: &[[f64; 2]; 4], w: f64, h: f64) -> Option<Homography> {
if !(convex_quad(q) && w.is_finite() && h.is_finite() && w > 0.0 && h > 0.0) {
if !(convex_quad_any_winding(q) && w.is_finite() && h.is_finite() && w > 0.0 && h > 0.0) {
return None;
}
Homography::rect_to_quad([0.0, 0.0, w, h], *q)?.inverse()
+26 -2
View File
@@ -61,7 +61,7 @@ fn the_interruptible_warp_reports_progress_and_stops_when_cancelled() {
}
/// Perspective Crop's map lands each quad corner exactly on its rectangle corner; degenerate,
/// self-crossing, counter-clockwise and non-finite quads have none.
/// self-crossing and non-finite quads have none.
#[test]
fn quad_to_rect_maps_corners_and_refuses_bad_quads() {
let q = [[30.0, 12.0], [140.0, 25.0], [128.0, 110.0], [14.0, 95.0]];
@@ -75,7 +75,7 @@ fn quad_to_rect_maps_corners_and_refuses_bad_quads() {
for bad in [
[[0.0, 0.0], [50.0, 0.0], [100.0, 0.0], [0.0, 50.0]],
[[0.0, 0.0], [50.0, 50.0], [50.0, 0.0], [0.0, 50.0]],
[[0.0, 0.0], [0.0, 50.0], [50.0, 50.0], [50.0, 0.0]],
[[0.0, 0.0], [50.0, 50.0], [0.0, 50.0], [50.0, 0.0]],
[[0.0, 0.0], [f64::NAN, 0.0], [50.0, 50.0], [0.0, 50.0]],
[[0.0, 0.0]; 4],
] {
@@ -84,6 +84,30 @@ fn quad_to_rect_maps_corners_and_refuses_bad_quads() {
assert!(quad_to_rect(&q, 0.0, 10.0).is_none());
}
/// #12489: a quad wound counter-clockwise (a box flipped on one axis) maps mirrored, each corner
/// onto its own rectangle corner. Perspective Warp's clockwise-only check still refuses it.
#[test]
fn quad_to_rect_maps_a_counter_clockwise_quad_mirrored() {
let q = [[140.0, 25.0], [30.0, 12.0], [14.0, 95.0], [128.0, 110.0]];
assert!(!convex_quad(&q) && convex_quad_any_winding(&q));
let h = quad_to_rect(&q, 100.0, 80.0).unwrap();
for (c, want) in q.iter().zip([[0.0, 0.0], [100.0, 0.0], [100.0, 80.0], [0.0, 80.0]]) {
let (x, y) = h.apply(c[0], c[1]);
assert!((x - want[0]).abs() < 1e-9 && (y - want[1]).abs() < 1e-9, "{c:?} -> ({x}, {y})");
}
// Self-crossing in either winding, three corners on a line, and a sliver stay refused.
for bad in [
[[0.0, 0.0], [50.0, 0.0], [0.0, 50.0], [50.0, 50.0]],
[[50.0, 0.0], [0.0, 0.0], [50.0, 50.0], [0.0, 50.0]],
[[50.0, 0.0], [25.0, 0.0], [0.0, 0.0], [50.0, 50.0]],
[[50.0, 0.0], [0.0, 0.0], [0.0, 0.01], [50.0, 0.01]],
[[50.0, 0.0], [0.0, f64::INFINITY], [0.0, 50.0], [50.0, 50.0]],
] {
assert!(!convex_quad_any_winding(&bad), "{bad:?}");
assert!(quad_to_rect(&bad, 10.0, 10.0).is_none(), "{bad:?}");
}
}
fn rgba() -> Surface {
Surface::new(PixelFormat::RGBA8)
}
+16
View File
@@ -46,6 +46,8 @@ impl Fold<'_> {
/// Most subsamples per axis when a warp shrinks the source; also the longest source step per
/// destination pixel the filter follows (bounds the footprint each tile reads).
const MAX_SS: f64 = 8.0;
/// Bounding-box pixels scanned per destination pixel of a tile, across all its triangles.
const MAX_SCAN_PER_PIXEL: usize = 256;
/// Places a smart object's source image (`src`, its pixels in `src_rect`) in the document:
/// through `warp` (source space) and then the affine `t` (source → document) in one resampling
@@ -462,6 +464,11 @@ fn triangles(
let by0 = ps.iter().map(|p| p[1]).fold(f64::MAX, f64::min).floor() as i32;
let bx1 = ps.iter().map(|p| p[0]).fold(f64::MIN, f64::max).ceil() as i32;
let by1 = ps.iter().map(|p| p[1]).fold(f64::MIN, f64::max).ceil() as i32;
// Degenerate triangles draw nothing (`work` skips them the same way): do not bin them.
let det = (ps[1][0] - ps[0][0]) * (ps[2][1] - ps[0][1]) - (ps[2][0] - ps[0][0]) * (ps[1][1] - ps[0][1]);
if det.is_nan() || det.abs() < 1e-12 {
continue;
}
let (cx0, cy0) = ((bx0.max(dst.x0) - tx0).div_euclid(ts), (by0.max(dst.y0) - ty0).div_euclid(ts));
let (cx1, cy1) = ((bx1.min(dst.x1 - 1) - tx0).div_euclid(ts), (by1.min(dst.y1 - 1) - ty0).div_euclid(ts));
for cy in cy0.max(0)..=cy1 {
@@ -515,6 +522,9 @@ fn triangles(
let mut under_at: Vec<u32> = Vec::new();
let mut jac: Vec<([f64; 4], usize, usize)> = Vec::with_capacity(order.len());
'pass: loop {
// Folded warps pile thousands of triangles over the same pixels: the bounding boxes
// scanned per tile are capped (a sound warp scans a small multiple of the tile's area).
let mut scan_left = w * h * MAX_SCAN_PER_PIXEL + (1 << 16);
for (k, tri) in order.iter().enumerate() {
let [(p0, s0), (p1, s1), (p2, s2)] = tri.map(|v| verts[v]);
let det = (p1[0] - p0[0]) * (p2[1] - p0[1]) - (p2[0] - p0[0]) * (p1[1] - p0[1]);
@@ -538,6 +548,12 @@ fn triangles(
let bx1 = (p0[0].max(p1[0]).max(p2[0]) + 0.5).ceil().min(f64::from(t.x1)) as i32;
let by1 = (p0[1].max(p1[1]).max(p2[1]) + 0.5).ceil().min(f64::from(t.y1)) as i32;
let eps = -1e-9;
let scan = (bx1 - bx0).max(0) as usize * (by1 - by0).max(0) as usize;
// A triangle that no longer fits is skipped, not the end of the tile: later (smaller) ones still draw.
if scan > scan_left {
continue;
}
scan_left -= scan;
for y in by0..by1 {
let py = f64::from(y) + 0.5;
for x in bx0..bx1 {
+8 -15
View File
@@ -1,5 +1,6 @@
//! Frozen scalar sharpen from main, used as an exact-output oracle.
use photocraft_algo::retouch::{local_blur, local_sharpen};
//! Frozen scalar sharpen from main, used as an exact-output oracle. Its Protect Detail branch
//! follows #12938: a wider unsharp mask whose differences are soft-limited to 6 levels.
use photocraft_algo::retouch::{PROTECT_DETAIL_SIGMA, local_blur, local_sharpen};
#[test]
fn matches_scalar_sharpen_bit_for_bit_across_layouts_edges_and_hdr() {
@@ -51,8 +52,9 @@ fn scalar_local_sharpen(
if ow == 0 || oh == 0 || ch == 0 {
return Vec::new();
}
let blur = local_blur(data, w, h, ch, alpha, win, 1.0);
let blur = local_blur(data, w, h, ch, alpha, win, if protect_detail { PROTECT_DETAIL_SIGMA } else { 1.0 });
let mut out = vec![0.0f32; blur.len()];
let limit = 6.0f32 / 255.0;
for y in y0..y1 {
for x in x0..x1 {
let o = ((y - y0) * ow + (x - x0)) * ch;
@@ -63,18 +65,9 @@ fn scalar_local_sharpen(
out[o + c] = v;
continue;
}
let mut s = v + amount * (v - blur[o + c]);
if protect_detail {
let (mut lo, mut hi) = (v, v);
for (dx, dy) in [(-1i32, -1i32), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)] {
let (nx, ny) = ((x as i32 + dx).clamp(0, w as i32 - 1) as usize, (y as i32 + dy).clamp(0, h as i32 - 1) as usize);
let nv = data[(ny * w + nx) * ch + c];
lo = lo.min(nv);
hi = hi.max(nv);
}
s = s.clamp(lo, hi);
}
out[o + c] = s;
let d = v - blur[o + c];
let d = if protect_detail { d * limit / (limit + d.abs()) } else { d };
out[o + c] = v + amount * d;
}
}
}
+4 -1
View File
@@ -39,6 +39,9 @@ hmac = "0.12"
sha2 = "0.10"
cap-std = "4"
clap = { workspace = true }
# The suite's AI-MCP flag and the ArtCraft Launcher's agents switch (the one consent for agents).
craft-flags = { workspace = true }
craft-ai-agents = { workspace = true }
# Optional native GPU rendering backend
photocraft-gpu = { workspace = true, optional = true }
@@ -53,7 +56,7 @@ photocraft-raw = { workspace = true, features = ["testgen"] }
# Tiny generated PSDs and the scoped estimate charge for the high-memory approval tests
# (no multi-gigabyte fixture or global override).
photocraft-io = { workspace = true, features = ["testgen"] }
photocraft-psd = { workspace = true, features = ["testgen"] }
craft-psd = { workspace = true, features = ["testgen"] }
rmcp = { version = "3.5", default-features = false, features = ["client", "server", "macros", "transport-io"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros", "io-std", "sync", "net", "io-util", "time"] }
+387
View File
@@ -0,0 +1,387 @@
//! Connecting AI agents to the open desktop app: whether the person lets them (one switch, in
//! the ArtCraft Launcher, [`agents_allowed`]), and how a local client finds the app's control
//! server without being told. Adding PhotoCraft to MCP clients is the launcher's job (one entry
//! for every Craft app), so nothing here writes a client's configuration.
//!
//! While agents may connect (and no `--control` was given), the app serves its control protocol
//! on loopback and publishes where in its settings folder ([`CONNECTION_FILE`]): the port, its
//! process id and the bearer-token file ([`TOKEN_FILE`] unless a token file was given at launch;
//! created `0600` on Unix, see [`crate::security::server_token`]). A local client reads that
//! file, then the token from the file it names, so nothing has to pass a port or a token to an
//! app started from the Dock, the Start menu or a file manager.
//!
//! The file holds no secret. A crashed (or, on macOS, quit) app can leave it behind; nothing then
//! listens on its port ([`probe`]), and the app's next launch replaces it or removes it
//! ([`remove_stale_connection`]). Until then another program could take that port, so a client
//! checks that [`Connection::pid`] is still running before it sends the token. That narrows the
//! gap without closing it (a pid can be reused); `docs/control-protocol.md` says when a client
//! should not send the token on the strength of this file.
//!
//! The app starts and stops that server at once when the switch changes. `photocraft-cli mcp`
//! ([`crate::live`]) reads the connection file on every tool call, so MCP client configurations
//! never hold a port or a token, and keep working when the app restarts on another port.
use std::net::{SocketAddr, TcpStream};
use std::path::{Path, PathBuf};
use std::time::Duration;
use photocraft_engine::prefs::Integrations;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::AutomationError;
/// The connection file in the settings folder, present while the app accepts agents.
pub const CONNECTION_FILE: &str = "agent-connection.json";
/// The bearer-token file the app keeps in its settings folder for agent connections.
pub const TOKEN_FILE: &str = "agent-control.token";
/// The server name MCP clients list PhotoCraft under.
pub const SERVER_NAME: &str = "photocraft";
/// How long [`probe`] waits for the app to accept a connection.
pub const PROBE_TIMEOUT: Duration = Duration::from_millis(500);
/// Version of the [`Connection`] file format.
const CONNECTION_VERSION: u32 = 1;
/// Largest connection file read; anything bigger is not ours.
const MAX_CONNECTION_BYTES: u64 = 1 << 20;
/// What the app publishes in [`CONNECTION_FILE`]. Holds no secret: the token stays in its own
/// user-only file.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Connection {
pub version: u32,
/// The app's process id: only that process removes the file.
pub pid: u32,
/// Loopback port of the control server.
pub port: u16,
pub token_file: PathBuf,
/// The app's version, for diagnostics.
pub app: String,
}
impl Connection {
pub fn new(port: u16, token_file: PathBuf) -> Self {
Connection { version: CONNECTION_VERSION, pid: std::process::id(), port, token_file, app: photocraft_engine::build_info::long_version() }
}
/// `127.0.0.1:<port>`.
pub fn addr(&self) -> String {
format!("127.0.0.1:{}", self.port)
}
}
fn io_error(path: &Path, e: impl std::fmt::Display) -> AutomationError {
AutomationError::Io(format!("{}: {e}", path.display()))
}
/// Publish `c` in `dir` (written atomically, so a reader never sees half a file).
pub fn write_connection(dir: &Path, c: &Connection) -> Result<(), AutomationError> {
std::fs::create_dir_all(dir).map_err(|e| io_error(dir, e))?;
let path = dir.join(CONNECTION_FILE);
let text = serde_json::to_string_pretty(c).map_err(|e| AutomationError::Other(e.to_string()))?;
photocraft_format::atomic_write(&path, text.as_bytes()).map_err(|e| io_error(&path, e))
}
/// The connection published in `dir`, if any. A file from another version or one that doesn't
/// parse (invalid UTF-8 included) counts as none: the app rewrites it when it next starts
/// listening. Anything but a regular file (a FIFO would block the read) is an error, and left
/// alone.
pub fn read_connection(dir: &Path) -> Result<Option<Connection>, AutomationError> {
let path = dir.join(CONNECTION_FILE);
let meta = match std::fs::metadata(&path) {
Ok(m) => m,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(io_error(&path, e)),
};
if !meta.is_file() {
return Err(io_error(&path, "not a regular file"));
}
if meta.len() > MAX_CONNECTION_BYTES {
return Err(io_error(&path, format!("larger than {MAX_CONNECTION_BYTES} bytes")));
}
let bytes = match std::fs::read(&path) {
Ok(b) => b,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(io_error(&path, e)),
};
Ok(serde_json::from_slice::<Connection>(&bytes).ok().filter(|c| c.version == CONNECTION_VERSION && c.port != 0))
}
/// Remove the connection file in `dir` if process `pid` wrote it (another instance may have
/// published its own since). Errors are ignored: a stale file only fails [`probe`].
pub fn remove_connection(dir: &Path, pid: u32) {
if let Ok(Some(c)) = read_connection(dir)
&& c.pid == pid
{
let _ = std::fs::remove_file(dir.join(CONNECTION_FILE));
}
}
/// Remove the connection file in `dir` when nothing listens on its port any more (an app that
/// crashed or quit without removing it), or when it doesn't read as a connection. A live
/// instance's file is kept. Errors are ignored.
pub fn remove_stale_connection(dir: &Path) {
let path = dir.join(CONNECTION_FILE);
let stale = match read_connection(dir) {
Ok(Some(c)) => !probe(c.port, PROBE_TIMEOUT),
Ok(None) => path.is_file(),
Err(_) => false,
};
if stale {
let _ = std::fs::remove_file(path);
}
}
/// Does something accept connections on loopback `port`? (A crashed app leaves its connection
/// file behind; nothing listens on its port any more.)
pub fn probe(port: u16, timeout: Duration) -> bool {
port != 0 && TcpStream::connect_timeout(&SocketAddr::from(([127, 0, 0, 1], port)), timeout).is_ok()
}
/// A folder preference as a path (`None` when empty or blank).
pub fn folder(pref: &str) -> Option<PathBuf> {
let pref = pref.trim();
(!pref.is_empty()).then(|| PathBuf::from(pref))
}
/// Largest client configuration file read; anything bigger is not ours.
const MAX_CONFIG_BYTES: u64 = 16 << 20;
/// Read a small text file, refusing anything larger than [`MAX_CONFIG_BYTES`] or anything but a
/// regular file (a FIFO would block the read).
fn read_small(path: &Path) -> Result<Option<String>, AutomationError> {
let meta = match std::fs::metadata(path) {
Ok(m) => m,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(io_error(path, e)),
};
if !meta.is_file() {
return Err(io_error(path, "not a regular file"));
}
if meta.len() > MAX_CONFIG_BYTES {
return Err(io_error(path, format!("larger than {MAX_CONFIG_BYTES} bytes")));
}
match std::fs::read_to_string(path) {
Ok(s) => Ok(Some(s)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(io_error(path, e)),
}
}
/// The MCP preferences saved in `dir/preferences.json` (defaults when the file or
/// the section is missing or unreadable: the CLI must not fail over the app's settings).
pub fn read_integrations(dir: &Path) -> Integrations {
read_small(&dir.join("preferences.json"))
.ok()
.flatten()
.and_then(|text| serde_json::from_str::<Value>(&text).ok())
.and_then(|v| v.get("integrations").cloned())
.map(photocraft_engine::prefs::read_integrations_section)
.unwrap_or_default()
}
// ---- The agents switch -------------------------------------------------------------------------
/// The id the ArtCraft Launcher's app list names PhotoCraft by.
pub const LAUNCHER_APP_ID: &str = "photocraft";
/// The suite's `AI-MCP` feature flag (`ArtCraft/flags.toml` or `CRAFT_FLAGS`).
pub fn mcp_flag() -> bool {
craft_flags::load().enabled(&craft_flags::AI_MCP)
}
/// The ArtCraft Launcher's switch (Settings › AI agents › *Let AI agents use Craft apps*) lets
/// agents use PhotoCraft: the `AI-MCP` flag is on and the launcher's app list names PhotoCraft,
/// read through `craft-ai-agents` like the launcher, the MCP gateway and every Craft app do. Reads
/// two small files; ask from a timer, not every frame.
pub fn launcher_allows_agents() -> bool {
mcp_flag() && craft_ai_agents::allowed(LAUNCHER_APP_ID)
}
/// Whether agents may use the open window. With the `AI-MCP` flag only the launcher's switch
/// decides: there is one switch for every Craft app, and none in PhotoCraft. Without the flag it
/// is as on `main`: a Control Port saved in the preferences (`connect_agents`, set from it when
/// the preferences load). Allow Agent Control off still refuses agents either way.
pub fn agents_allowed(integrations: &Integrations, mcp_flag: bool, launcher: bool) -> bool {
if mcp_flag { launcher } else { integrations.connect_agents }
}
/// [`agents_allowed`] for this run: the flag and the launcher's switch as they are now.
pub fn agents_allowed_now(integrations: &Integrations) -> bool {
let flag = mcp_flag();
agents_allowed(integrations, flag, flag && craft_ai_agents::allowed(LAUNCHER_APP_ID))
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
fn temp(tag: &str) -> PathBuf {
static N: AtomicUsize = AtomicUsize::new(0);
let d = std::env::temp_dir().join(format!("photocraft-agent-link-{tag}-{}-{}", std::process::id(), N.fetch_add(1, Ordering::Relaxed)));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
d
}
#[test]
fn with_the_flag_only_the_launchers_switch_lets_agents_in() {
let legacy = Integrations { connect_agents: true, ..Default::default() };
let none = Integrations::default();
// Without the AI-MCP flag: as on main, a Control Port saved before.
assert!(agents_allowed(&legacy, false, false));
assert!(!agents_allowed(&none, false, true), "the launcher's list means nothing without the flag");
// With the flag: the launcher's switch, and nothing else.
assert!(agents_allowed(&none, true, true));
assert!(!agents_allowed(&legacy, true, false), "no second switch in PhotoCraft");
}
#[test]
fn connection_round_trips_and_only_its_writer_removes_it() {
let dir = temp("conn");
assert_eq!(read_connection(&dir).unwrap(), None);
let c = Connection::new(4242, dir.join(TOKEN_FILE));
write_connection(&dir, &c).unwrap();
assert_eq!(read_connection(&dir).unwrap(), Some(c.clone()));
assert_eq!(c.addr(), "127.0.0.1:4242");
remove_connection(&dir, c.pid.wrapping_add(1));
assert!(dir.join(CONNECTION_FILE).exists(), "another instance's file stays");
remove_connection(&dir, c.pid);
assert!(!dir.join(CONNECTION_FILE).exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_damaged_or_foreign_connection_file_counts_as_none() {
let dir = temp("bad-conn");
for text in
["", "{", "[]", r#"{"version":99,"pid":1,"port":1,"tokenFile":"t","app":"x"}"#, r#"{"version":1,"pid":1,"port":0,"tokenFile":"t","app":"x"}"#]
{
std::fs::write(dir.join(CONNECTION_FILE), text).unwrap();
assert_eq!(read_connection(&dir).unwrap(), None, "{text}");
}
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn probe_sees_a_listener_and_not_a_closed_port() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
assert!(probe(port, PROBE_TIMEOUT));
drop(listener);
assert!(!probe(port, PROBE_TIMEOUT));
assert!(!probe(0, PROBE_TIMEOUT));
}
#[test]
fn integrations_come_from_the_saved_preferences_or_default() {
let dir = temp("prefs");
assert_eq!(read_integrations(&dir), Integrations::default());
std::fs::write(dir.join("preferences.json"), "not json").unwrap();
assert_eq!(read_integrations(&dir), Integrations::default());
// A section of the wrong type doesn't fail the CLI either.
std::fs::write(dir.join("preferences.json"), r#"{"integrations": {"connectAgents": "yes"}}"#).unwrap();
assert_eq!(read_integrations(&dir), Integrations::default());
std::fs::write(
dir.join("preferences.json"),
r#"{"general": {}, "integrations": {"connectAgents": true, "whenAppClosed": "headless", "readRoot": "/in"}}"#,
)
.unwrap();
let i = read_integrations(&dir);
assert!(i.connect_agents);
assert_eq!(i.when_app_closed, photocraft_engine::prefs::WhenAppClosed::Headless);
assert_eq!(folder(&i.read_root), Some(PathBuf::from("/in")));
assert_eq!(folder(&i.write_root), None);
assert_eq!(folder(" "), None);
// A Control Port saved before the agents switch reads as on, as the app reads it.
std::fs::write(dir.join("preferences.json"), r#"{"integrations": {"controlPort": 7878}}"#).unwrap();
assert!(read_integrations(&dir).connect_agents);
std::fs::write(dir.join("preferences.json"), r#"{"integrations": {"controlPort": 7878, "connectAgents": false}}"#).unwrap();
assert!(!read_integrations(&dir).connect_agents);
// One bad value resets only itself, as in the app: the saved closed-app choice stays.
std::fs::write(dir.join("preferences.json"), r#"{"integrations": {"whenAppClosed": "error", "readRoot": 5}}"#).unwrap();
assert_eq!(read_integrations(&dir).when_app_closed, photocraft_engine::prefs::WhenAppClosed::Error);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_published_connection_reads_back_in_the_documented_shape() {
let dir = temp("conn");
let c = Connection::new(7878, dir.join(TOKEN_FILE));
write_connection(&dir, &c).unwrap();
assert_eq!(read_connection(&dir).unwrap(), Some(c.clone()));
let v: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(dir.join(CONNECTION_FILE)).unwrap()).unwrap();
let mut keys: Vec<_> = v.as_object().unwrap().keys().cloned().collect();
keys.sort();
assert_eq!(keys, ["app", "pid", "port", "tokenFile", "version"]);
assert_eq!((v["version"].as_u64(), v["port"].as_u64()), (Some(1), Some(7878)));
assert_eq!(c.addr(), "127.0.0.1:7878");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn other_versions_garbage_and_port_zero_read_as_none() {
let dir = temp("conn");
assert_eq!(read_connection(&dir).unwrap(), None, "no file");
for text in [
&b"{not json"[..],
br#"{"version":2,"pid":1,"port":7878,"tokenFile":"t","app":"x"}"#,
br#"{"version":1,"pid":1,"port":0,"tokenFile":"t","app":"x"}"#,
b"\xff\xfe not utf-8",
] {
std::fs::write(dir.join(CONNECTION_FILE), text).unwrap();
assert_eq!(read_connection(&dir).unwrap(), None, "{}", String::from_utf8_lossy(text));
}
std::fs::remove_file(dir.join(CONNECTION_FILE)).unwrap();
// Not a regular file: an error (and `remove_stale_connection` leaves it), never a read
// that could block on a FIFO.
std::fs::create_dir(dir.join(CONNECTION_FILE)).unwrap();
assert!(read_connection(&dir).is_err());
remove_stale_connection(&dir);
assert!(dir.join(CONNECTION_FILE).is_dir());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn only_the_publishing_process_removes_its_file() {
let dir = temp("conn");
let c = Connection { pid: 1, ..Connection::new(7878, dir.join(TOKEN_FILE)) };
write_connection(&dir, &c).unwrap();
remove_connection(&dir, 2);
assert!(dir.join(CONNECTION_FILE).is_file(), "another process's file stays");
remove_connection(&dir, 1);
assert!(!dir.join(CONNECTION_FILE).exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_stale_file_is_removed_and_a_live_one_kept() {
let dir = temp("conn");
let live = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let live_port = live.local_addr().unwrap().port();
write_connection(&dir, &Connection { pid: 1, ..Connection::new(live_port, dir.join(TOKEN_FILE)) }).unwrap();
remove_stale_connection(&dir);
assert!(dir.join(CONNECTION_FILE).is_file(), "something listens on {live_port}");
drop(live);
// A port nothing listens on: bind one, then close it.
let closed = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
write_connection(&dir, &Connection { pid: 1, ..Connection::new(closed, dir.join(TOKEN_FILE)) }).unwrap();
remove_stale_connection(&dir);
assert!(!dir.join(CONNECTION_FILE).exists(), "nothing listens on {closed}");
for text in [&b"{not json"[..], b"\xff\xfe not utf-8"] {
std::fs::write(dir.join(CONNECTION_FILE), text).unwrap();
remove_stale_connection(&dir);
assert!(!dir.join(CONNECTION_FILE).exists(), "an unreadable file is not a connection");
}
remove_stale_connection(&dir);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn folder_preferences_are_trimmed_and_empty_means_none() {
assert_eq!(folder(""), None);
assert_eq!(folder(" "), None);
assert_eq!(folder(" /work/project "), Some(PathBuf::from("/work/project")));
}
}
+114
View File
@@ -0,0 +1,114 @@
//! Where PhotoCraft keeps its per-user data, resolved the same way by the desktop app and
//! `photocraft-cli` (which finds the app's agent connection file there, see [`crate::agent_link`]).
//!
//! In order:
//! 1. `PHOTOCRAFT_CONFIG_DIR`, when set (tests, agents, custom setups).
//! 2. **Portable mode** (#228): a marker file ([`PORTABLE_MARKERS`]) beside the executable puts
//! everything in `<exe dir>/PhotoCraftData`, so a portable copy on a USB stick leaves nothing in
//! `%APPDATA%`. The Windows portable zip ships with `portable.txt`; the MSI doesn't. If that
//! folder can't be written (read-only medium, Program Files), the platform folder is used and
//! [`DataDir::warning`] says why.
//! 3. The platform convention: macOS `~/Library/Application Support/Photocraft`, Windows
//! `%APPDATA%\Photocraft`, Linux `$XDG_CONFIG_HOME/photocraft` or `~/.config/photocraft`.
//!
//! The desktop app's `app_dirs` resolves it once per process and adds the crash-recovery folder.
use std::ffi::OsString;
use std::path::{Path, PathBuf};
/// Files beside the executable that switch on portable mode (either one; contents are ignored).
pub const PORTABLE_MARKERS: [&str; 2] = ["portable.txt", "PhotoCraft.portable"];
/// The data folder created beside the executable in portable mode.
pub const PORTABLE_DATA_DIR: &str = "PhotoCraftData";
/// How [`DataDir::dir`] was chosen.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Mode {
/// `PHOTOCRAFT_CONFIG_DIR`.
Override,
/// `<exe dir>/PhotoCraftData`.
Portable,
/// The platform's per-user config directory.
Platform,
}
/// The resolved data directory.
#[derive(Clone, Debug)]
pub struct DataDir {
/// `None` only when no platform directory is known either (no `HOME`/`APPDATA`).
pub dir: Option<PathBuf>,
pub mode: Mode,
/// Set when a portable marker was found but its data folder isn't writable.
pub warning: Option<String>,
}
/// Resolve the data directory from the environment (`env`) and the executable's folder.
pub fn resolve(env: impl Fn(&str) -> Option<OsString>, exe_dir: Option<&Path>) -> DataDir {
if let Some(d) = env("PHOTOCRAFT_CONFIG_DIR").filter(|d| !d.is_empty()) {
return DataDir { dir: Some(PathBuf::from(d)), mode: Mode::Override, warning: None };
}
let platform = platform_dir(&env);
let Some((exe_dir, marker)) = exe_dir.and_then(|d| portable_marker(d).map(|m| (d, m))) else {
return DataDir { dir: platform, mode: Mode::Platform, warning: None };
};
let data = exe_dir.join(PORTABLE_DATA_DIR);
match ensure_writable(&data) {
Ok(()) => DataDir { dir: Some(data), mode: Mode::Portable, warning: None },
Err(e) => {
let fallback = platform.as_ref().map_or_else(|| "nowhere (no user folder found)".to_string(), |p| p.display().to_string());
let warning =
format!("portable mode ({} found) but {} isn't writable ({e}); settings are kept in {fallback} instead", marker.display(), data.display());
DataDir { dir: platform, mode: Mode::Platform, warning: Some(warning) }
}
}
}
/// The platform convention, from `env` (so tests can fake `HOME`/`APPDATA`/`XDG_CONFIG_HOME`).
pub fn platform_dir(env: &impl Fn(&str) -> Option<OsString>) -> Option<PathBuf> {
let home = env("HOME").filter(|h| !h.is_empty()).map(PathBuf::from);
if cfg!(target_os = "macos") {
return home.map(|h| h.join("Library/Application Support/Photocraft"));
}
if cfg!(windows) {
return env("APPDATA").filter(|a| !a.is_empty()).map(|a| PathBuf::from(a).join("Photocraft"));
}
env("XDG_CONFIG_HOME").filter(|x| !x.is_empty()).map(PathBuf::from).or_else(|| home.map(|h| h.join(".config"))).map(|c| c.join("photocraft"))
}
/// The first portable marker file in `exe_dir`, if any.
pub fn portable_marker(exe_dir: &Path) -> Option<PathBuf> {
PORTABLE_MARKERS.iter().map(|m| exe_dir.join(m)).find(|p| p.is_file())
}
/// Probe names tried before giving up on a portable data folder.
pub const WRITE_PROBE_ATTEMPTS: u32 = 64;
/// Create `dir` and prove a file can be written in it (a folder can exist yet be read-only).
///
/// The probe is created exclusively (`create_new`), so an entry already at its name (a link
/// someone else placed there, say) is never opened, written through or removed: the next name
/// is tried instead. Only a probe this call created is removed.
pub fn ensure_writable(dir: &Path) -> std::io::Result<()> {
std::fs::create_dir_all(dir)?;
let pid = std::process::id();
for attempt in 0..WRITE_PROBE_ATTEMPTS {
let probe = dir.join(format!(".write-test-{pid}-{attempt}"));
match std::fs::OpenOptions::new().write(true).create_new(true).open(&probe) {
Ok(file) => {
drop(file);
// A leftover probe is harmless; don't fail portable mode over it.
let _ = std::fs::remove_file(&probe);
return Ok(());
}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(e) => return Err(e),
}
}
Err(std::io::Error::new(std::io::ErrorKind::AlreadyExists, "every write-test name is already taken"))
}
/// The data directory for this process's environment and executable (not cached).
pub fn from_env() -> DataDir {
let exe = std::env::current_exe().ok();
resolve(|k| std::env::var_os(k), exe.as_deref().and_then(Path::parent))
}
+3 -1
View File
@@ -203,7 +203,9 @@ pub fn save_bytes_with_renderer(
}
return Ok((bytes, warnings));
}
let result = photocraft_io::export_with_renderer(doc, &ext, opts, &photocraft_raster::Interrupt::NONE, renderer)?;
// No ambient authority: a linked path inside the document must not be read and embedded.
let opts = ExportOptions { read_linked_files: false, ..opts.clone() };
let result = photocraft_io::export_with_renderer(doc, &ext, &opts, &photocraft_raster::Interrupt::NONE, renderer)?;
Ok((result.bytes, result.warnings))
}
+5
View File
@@ -11,16 +11,21 @@
//! * [`rpc`]: a headless JSON-lines server (stdio or loopback TCP) with the
//! control protocol's envelope, used by `photocraft-cli serve`.
//! * [`files`]: open/save any supported format, `.pcraft` natively.
//! * [`agent_link`] and [`live`]: connecting agents to the desktop app without a terminal
//! (the ArtCraft Launcher's agents switch and plain `photocraft-cli mcp`).
//!
//! L6, no UI-toolkit dependencies.
#![forbid(unsafe_code)]
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
pub mod agent_link;
pub mod app_dirs;
pub mod args;
pub mod bridge;
pub mod budgets;
pub mod files;
pub mod headless;
pub mod live;
pub mod rpc;
pub mod security;
pub mod server;
+295
View File
@@ -0,0 +1,295 @@
//! `photocraft-cli mcp`: tools go to the open desktop app when the person lets agents use it,
//! found through its connection file ([`crate::agent_link`]), and otherwise to a session without
//! a window, as `mcp` always did (`--headless` forces that). The file is looked up again on every
//! tool call, so the app may start, quit or move to another port while the MCP client stays
//! connected, and the MCP client's own start never opens a window.
//!
//! While agents may use the app but it is closed, When PhotoCraft Is Closed (or `--when-closed`)
//! decides: start the app and wait for it, serve a headless session with the folders chosen in
//! the app, or report an error that says what to do. A session that went headless stays headless
//! for the rest of the MCP session, so documents don't move under the agent.
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::{Duration, Instant};
use photocraft_engine::prefs::WhenAppClosed;
use crate::agent_link::{self, Connection, PROBE_TIMEOUT};
use crate::bridge::BridgeClient;
use crate::headless::Headless;
use crate::{AuthorizedWorkspace, security};
/// Starts the desktop app (see [`launcher`]).
pub type LaunchFn = Box<dyn Fn() -> Result<(), String> + Send + Sync>;
/// Prepares a headless session before its first call (see [`LiveConfig::prepare_headless`]).
pub type PrepareFn = Box<dyn Fn(&mut Headless) + Send + Sync>;
/// How long a started app gets to accept agents. A first start can take a while (GPU driver
/// shader caches, font scan), but MCP clients give a tool call about a minute.
pub const LAUNCH_TIMEOUT: Duration = Duration::from_secs(45);
const POLL: Duration = Duration::from_millis(250);
pub struct LiveConfig {
/// The app's settings folder: connection file, token and preferences.
pub config_dir: PathBuf,
/// Overrides the app's preference (`--when-closed`).
pub when_closed: Option<WhenAppClosed>,
/// The folders a headless session may use (`--automation-read-root`, repeatable, and
/// `--automation-write-root`); while agents may use the app, they override its folders.
pub read_roots: Vec<PathBuf>,
pub write_root: Option<PathBuf>,
/// Starts the app; `None` when this install can't (Flatpak sandbox, no app beside the CLI).
pub launch: Option<LaunchFn>,
pub launch_timeout: Duration,
/// Whether the person lets agents use the open app, given its saved preferences
/// ([`agent_link::agents_allowed_now`]: the ArtCraft Launcher's switch); tests replace it.
pub allowed: fn(&photocraft_engine::prefs::Integrations) -> bool,
/// Prepares a headless session before its first call (the CLI's `--gpu` / `--no-gpu`).
pub prepare_headless: Option<PrepareFn>,
}
impl LiveConfig {
pub fn new(config_dir: PathBuf) -> Self {
LiveConfig {
config_dir,
when_closed: None,
read_roots: Vec::new(),
write_root: None,
launch: None,
launch_timeout: LAUNCH_TIMEOUT,
allowed: agent_link::agents_allowed_now,
prepare_headless: None,
}
}
}
/// Where a tool call goes once [`Live::resolve`] has run.
#[derive(Clone)]
pub(crate) enum Target {
Bridge(Arc<BridgeClient>),
Headless(Arc<Mutex<Headless>>),
}
/// The port and token a bridge client was made for: when the app restarts, either changes.
type BridgeKey = (u16, String, u32);
pub struct Live {
cfg: LiveConfig,
/// Serialises lookups, so concurrent calls start the app once.
lookup: tokio::sync::Mutex<()>,
/// The current target, and for the bridge the port and token it was made for.
current: Mutex<Option<(Target, Option<BridgeKey>)>>,
}
impl Live {
pub fn new(cfg: LiveConfig) -> Self {
Live { cfg, lookup: tokio::sync::Mutex::new(()), current: Mutex::new(None) }
}
pub(crate) fn current(&self) -> Option<Target> {
self.current.lock().unwrap_or_else(PoisonError::into_inner).as_ref().map(|(t, _)| t.clone())
}
fn set(&self, target: Target, key: Option<BridgeKey>) {
*self.current.lock().unwrap_or_else(PoisonError::into_inner) = Some((target, key));
}
/// Find the app (or apply the closed-app choice) before a tool call. The error is the tool's
/// result: it tells the agent, and through it the user, what to do.
pub(crate) async fn resolve(&self) -> Result<(), String> {
let _one_at_a_time = self.lookup.lock().await;
if matches!(self.current(), Some(Target::Headless(_))) {
return Ok(());
}
let dir = self.cfg.config_dir.clone();
let consent = self.cfg.allowed;
let (found, integrations, allowed) = blocking(move || {
let integrations = agent_link::read_integrations(&dir);
let allowed = consent(&integrations);
(find_app(&dir), integrations, allowed)
})
.await?;
if let Some((c, token)) = found? {
self.use_app(&c, token)?;
return Ok(());
}
if !allowed {
// As `mcp` always worked: a session without a window, with only the folders given to
// this command. The app's own folders are for agents the person let in.
return self.headless(&self.cfg.read_roots, self.cfg.write_root.clone(), "the automation roots can't be used");
}
match self.cfg.when_closed.unwrap_or(integrations.when_app_closed) {
WhenAppClosed::Error => Err("PhotoCraft isn't running: open it, then try again.".into()),
WhenAppClosed::Headless => {
let reads = if self.cfg.read_roots.is_empty() {
agent_link::folder(&integrations.read_root).into_iter().collect()
} else {
self.cfg.read_roots.clone()
};
let write = self.cfg.write_root.clone().or_else(|| agent_link::folder(&integrations.write_root));
self.headless(&reads, write, "PhotoCraft isn't running and its agent folders can't be used")
}
WhenAppClosed::Launch => self.launch_and_wait().await,
}
}
/// Serve a headless session with these folders from now on.
fn headless(&self, reads: &[PathBuf], write: Option<PathBuf>, why: &str) -> Result<(), String> {
let workspace = AuthorizedWorkspace::with_read_roots(reads.iter().map(PathBuf::as_path), write.as_deref()).map_err(|e| format!("{why}: {e}"))?;
let mut session = Headless::with_workspace(workspace);
if let Some(prepare) = &self.cfg.prepare_headless {
prepare(&mut session);
}
self.set(Target::Headless(Arc::new(Mutex::new(session))), None);
Ok(())
}
/// Start the app (agents may use it) and wait until it accepts them.
async fn launch_and_wait(&self) -> Result<(), String> {
let Some(launch) = &self.cfg.launch else {
return Err("PhotoCraft isn't running, and this install can't start it from here: open PhotoCraft, then try again.".into());
};
launch().map_err(|e| format!("couldn't start PhotoCraft: {e}"))?;
let deadline = Instant::now() + self.cfg.launch_timeout;
loop {
tokio::time::sleep(POLL).await;
let dir = self.cfg.config_dir.clone();
if let Some((c, token)) = blocking(move || find_app(&dir)).await?? {
return self.use_app(&c, token);
}
if Instant::now() >= deadline {
return Err(format!(
"PhotoCraft didn't accept agents within {} s of starting. If it's still opening, try again in a moment; otherwise check that AI agents are on in ArtCraft Launcher › Settings › AI agents.",
self.cfg.launch_timeout.as_secs()
));
}
}
}
fn use_app(&self, c: &Connection, token: String) -> Result<(), String> {
// The pid too: a restarted app keeps its port and token, but the old socket is dead.
let key: BridgeKey = (c.port, token, c.pid);
let same = self.current.lock().unwrap_or_else(PoisonError::into_inner).as_ref().is_some_and(|(_, k)| k.as_ref() == Some(&key));
if !same {
let client = BridgeClient::new(c.addr(), key.1.clone()).map_err(|e| e.to_string())?;
self.set(Target::Bridge(Arc::new(client)), Some(key));
}
Ok(())
}
}
/// Run blocking file and socket work off the async runtime.
async fn blocking<T: Send + 'static>(f: impl FnOnce() -> T + Send + 'static) -> Result<T, String> {
tokio::task::spawn_blocking(f).await.map_err(|e| format!("task failed: {e}"))
}
/// The running app's connection and token: `None` when no app accepts agents (no connection
/// file, or one left by an app that crashed).
fn find_app(dir: &Path) -> Result<Option<(Connection, String)>, String> {
let Some(c) = agent_link::read_connection(dir).map_err(|e| e.to_string())? else { return Ok(None) };
if !agent_link::probe(c.port, PROBE_TIMEOUT) {
return Ok(None);
}
let token = security::client_token(None, Some(&c.token_file)).map_err(|e| format!("PhotoCraft's agent token: {e}"))?;
Ok(Some((c, token)))
}
/// How to start the desktop app that ships with the CLI at `cli` (detached, with no standard
/// streams: the CLI's carry the MCP protocol, and the app must outlive the MCP client).
pub fn launcher(cli: &Path) -> Option<LaunchFn> {
use std::process::{Command, Stdio};
if std::env::var_os("FLATPAK_ID").is_some() {
return None;
}
let command: Command = if let Some(image) = std::env::var_os("APPIMAGE").filter(|v| !v.is_empty()) {
// The AppImage's mount point goes away with the CLI; the image itself stays.
Command::new(image)
} else {
let dir = cli.parent()?;
let bundle = dir.parent().filter(|_| dir.ends_with("Contents/MacOS")).and_then(Path::parent);
match bundle {
// A macOS app bundle: LaunchServices starts (or brings forward) the app.
Some(app) if cfg!(target_os = "macos") => {
let mut c = Command::new("/usr/bin/open");
c.arg("-a").arg(app);
c
}
_ => {
let app = dir.join(if cfg!(windows) { "photocraft.exe" } else { "photocraft" });
if !app.is_file() {
return None;
}
if cfg!(windows) {
let (program, args) = windows_launch(&app, std::env::var_os("SystemRoot").as_deref());
let mut c = Command::new(program);
c.args(args);
c
} else {
Command::new(app)
}
}
}
};
let command = Mutex::new(command);
Some(Box::new(move || {
let mut c = command.lock().unwrap_or_else(PoisonError::into_inner);
c.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null());
detach(&mut c);
c.spawn().map(|_| ()).map_err(|e| e.to_string())
}))
}
/// How Windows starts the app at `app`: through Explorer, as a double-click would. The app is then
/// no child of the CLI, so it outlives the MCP client ending the CLI's process tree (Claude Code
/// does on Windows), and it runs outside the container of a Microsoft Store client (Claude
/// Desktop): there, new files under `%APPDATA%` land in the client's package, where the app
/// started from the Start menu would never see the settings saved meanwhile. Explorer reads a
/// comma as a separator, so such a path is started directly. (`%WINDIR%` can be empty under a
/// Store client, hence `%SystemRoot%`.)
fn windows_launch(app: &Path, system_root: Option<&std::ffi::OsStr>) -> (PathBuf, Vec<std::ffi::OsString>) {
if app.to_string_lossy().contains(',') {
return (app.to_path_buf(), Vec::new());
}
let root = system_root.filter(|r| !r.is_empty()).map_or_else(|| PathBuf::from(r"C:\Windows"), PathBuf::from);
(root.join("explorer.exe"), vec![app.as_os_str().to_os_string()])
}
/// Keep the app running when the MCP client stops the CLI (and its process group).
fn detach(c: &mut std::process::Command) {
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
c.process_group(0);
}
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
const DETACHED_PROCESS: u32 = 0x0000_0008;
const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
c.creation_flags(DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP);
}
#[cfg(not(any(unix, windows)))]
let _ = c;
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::OsStr;
#[test]
fn windows_starts_the_app_through_explorer() {
let app = Path::new(r"C:\Program Files\PhotoCraft\photocraft.exe");
let (program, args) = windows_launch(app, Some(OsStr::new(r"D:\Win")));
assert_eq!(program, Path::new(r"D:\Win").join("explorer.exe"));
assert_eq!(args, [app.as_os_str()]);
// No usable %SystemRoot%: the default Windows folder.
let default = Path::new(r"C:\Windows").join("explorer.exe");
assert_eq!(windows_launch(app, Some(OsStr::new(""))).0, default);
assert_eq!(windows_launch(app, None).0, default);
// Explorer would split this path at the comma.
let comma = Path::new(r"C:\Apps, tools\photocraft.exe");
assert_eq!(windows_launch(comma, None), (comma.to_path_buf(), Vec::new()));
}
}
+2 -2
View File
@@ -312,7 +312,7 @@ fn doc_save_takes_maximize_compatibility() {
let mut h = Headless::trusted_local();
h.handle("doc.new", json!({"width": 64, "height": 32})).unwrap();
h.handle("engine.execute", json!({"command": "layer.new.layer"})).unwrap();
let real_merged = |p: &std::path::Path| photocraft_psd::PsdFile::from_bytes(&std::fs::read(p).unwrap()).unwrap().has_real_merged_data();
let real_merged = |p: &std::path::Path| craft_psd::PsdFile::from_bytes(&std::fs::read(p).unwrap()).unwrap().has_real_merged_data();
for (name, maximize, real) in [("default.psd", json!(null), true), ("off.psd", json!(false), false), ("on.psd", json!(true), true)] {
let path = dir.join(name);
h.handle("doc.save", json!({"path": path.to_string_lossy(), "maximizeCompatibility": maximize})).unwrap();
@@ -1155,7 +1155,7 @@ fn doc_place_forwards_high_memory_approval() {
let gb = 1u64 << 30;
let root = std::env::temp_dir().join(format!("pc-doc-place-approval-{}", std::process::id()));
std::fs::create_dir_all(&root).unwrap();
let psd = photocraft_psd::testgen::small(photocraft_psd::Version::Psd, photocraft_psd::Compression::Zip).to_bytes().unwrap();
let psd = craft_psd::testgen::small(craft_psd::Version::Psd, craft_psd::Compression::Zip).to_bytes().unwrap();
std::fs::write(root.join("high.psd"), &psd).unwrap();
// Known 5 GiB RAM: S = 2.5 GiB, H = 3.75 GiB. A 3 GiB estimate charge lands in (S, H].
let snap = photocraft_io::memory::MemorySnapshot { available_ram: Some(5 * gb), swap_free: None, commit_headroom: None };
+32
View File
@@ -282,6 +282,20 @@ fn create_token_file(path: &Path, token: &str) -> Result<(), AutomationError> {
writeln!(file, "{token}").map_err(|e| AutomationError::Io(format!("{}: {e}", path.display())))
}
/// Write a fresh token to `path`, replacing any earlier one (user-only on Unix), and return it.
/// The desktop app rotates the agent-connection token this way each time it starts listening, so
/// a token sent to a port another program took over after a crash stops working.
pub fn replace_token_file(path: &Path) -> Result<String, AutomationError> {
let token = generate_token()?;
match std::fs::remove_file(path) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => return Err(AutomationError::Io(format!("{}: {e}", path.display()))),
}
create_token_file(path, &token)?;
Ok(token)
}
/// Resolve a server token. With no supplied token or file, a fresh token is returned.
/// A missing token file is created atomically; an existing one is read and validated.
pub fn server_token(supplied: Option<&str>, token_file: Option<&Path>) -> Result<String, AutomationError> {
@@ -486,6 +500,24 @@ mod tests {
}
}
#[test]
fn replaced_token_files_hold_a_new_valid_token() {
let dir = std::env::temp_dir().join(format!("photocraft-replace-token-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let path = dir.join("agent.token");
let first = replace_token_file(&path).unwrap();
assert_eq!(client_token(None, Some(&path)).unwrap(), first);
let second = replace_token_file(&path).unwrap();
assert_ne!(first, second);
assert_eq!(client_token(None, Some(&path)).unwrap(), second);
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600);
}
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn generated_tokens_are_valid_and_distinct() {
let a = generate_token().unwrap();
+74 -13
View File
@@ -18,6 +18,7 @@ use serde_json::{Value, json};
use crate::bridge::BridgeClient;
use crate::budgets::{BatchReplyBudget, MAX_PREVIEW_SIDE, check_png, json_bytes};
use crate::headless::Headless;
use crate::live::{Live, LiveConfig, Target};
use crate::security::MAX_BATCH_STEPS;
use crate::{AuthorizedWorkspace, AutomationError, files};
@@ -27,6 +28,9 @@ pub enum Backend {
Headless(Arc<Mutex<Headless>>),
/// A running desktop app reached over the control protocol.
Bridge(Arc<BridgeClient>),
/// The desktop app found through its connection file before each call, or the closed-app
/// choice (plain `photocraft-cli mcp`, see [`crate::live`]).
Live(Arc<Live>),
}
#[derive(Clone)]
@@ -619,6 +623,12 @@ impl PhotocraftMcp {
Ok(Self::with_backend(Backend::Bridge(Arc::new(BridgeClient::new(addr, token)?))))
}
/// Serve the desktop app found through its connection file, or headless without consent
/// (plain `photocraft-cli mcp`).
pub fn live(config: LiveConfig) -> Self {
Self::with_backend(Backend::Live(Arc::new(Live::new(config))))
}
pub fn with_backend(backend: Backend) -> Self {
let mut tool_router = Self::tool_router();
for route in tool_router.map.values_mut() {
@@ -639,10 +649,7 @@ impl PhotocraftMcp {
&self,
f: impl FnOnce(&mut Headless) -> Result<T, AutomationError> + Send + 'static,
) -> Option<Result<T, AutomationError>> {
let Backend::Headless(h) = &*self.backend else {
return None;
};
let h = h.clone();
let h = self.headless_session()?;
Some(
tokio::task::spawn_blocking(move || {
// A panicking command poisons the lock, but edits run on a copy
@@ -657,13 +664,47 @@ impl PhotocraftMcp {
)
}
fn bridge_client(&self) -> Option<&BridgeClient> {
/// The headless session tools run on, if this backend (or the live choice) is one.
fn headless_session(&self) -> Option<Arc<Mutex<Headless>>> {
match &*self.backend {
Backend::Bridge(b) => Some(b),
Backend::Headless(h) => Some(h.clone()),
Backend::Live(live) => match live.current()? {
Target::Headless(h) => Some(h),
Target::Bridge(_) => None,
},
Backend::Bridge(_) => None,
}
}
fn bridge_client(&self) -> Option<Arc<BridgeClient>> {
match &*self.backend {
Backend::Bridge(b) => Some(b.clone()),
Backend::Live(live) => match live.current()? {
Target::Bridge(b) => Some(b),
Target::Headless(_) => None,
},
Backend::Headless(_) => None,
}
}
/// Where calls go now: the backend's session or app, or the live mode's last resolve.
fn target(&self) -> Option<Target> {
match &*self.backend {
Backend::Headless(h) => Some(Target::Headless(h.clone())),
Backend::Bridge(b) => Some(Target::Bridge(b.clone())),
Backend::Live(live) => live.current(),
}
}
/// Live mode: find the app (or apply the closed-app choice) before a call; the error is the
/// call's result. Other backends are always ready.
async fn ready(&self) -> Result<(), String> {
match &*self.backend {
Backend::Live(live) => live.resolve().await,
Backend::Headless(_) | Backend::Bridge(_) => Ok(()),
}
}
async fn screenshot(&self, b: &BridgeClient, max_side: Option<u32>) -> Result<CallToolResult, McpError> {
if max_side.is_some_and(|side| side > MAX_PREVIEW_SIDE) {
return Ok(fail(format!("automation preview side exceeds {MAX_PREVIEW_SIDE} pixels")));
@@ -960,7 +1001,7 @@ impl PhotocraftMcp {
if p.index.is_some() {
return Ok(fail("`index` is only supported in headless mode; bridge previews capture the app window"));
}
self.screenshot(b, Some(max)).await
self.screenshot(&b, Some(max)).await
}
#[tool(description = "Render a bounded flattened PNG preview (bridge mode: app window screenshot).")]
@@ -1056,7 +1097,7 @@ impl PhotocraftMcp {
#[tool(description = "Bridge mode: screenshot of the live app window as PNG.")]
async fn ui_screenshot(&self, Parameters(p): Parameters<ScreenshotParams>) -> Result<CallToolResult, McpError> {
match self.bridge_client() {
Some(b) => self.screenshot(b, p.max_side).await,
Some(b) => self.screenshot(&b, p.max_side).await,
None => bridge_only("ui_screenshot"),
}
}
@@ -1107,6 +1148,16 @@ impl PhotocraftMcp {
}
}
/// Whether two targets are the same session or the same app connection.
fn same_target(a: Option<&Target>, b: Option<&Target>) -> bool {
match (a, b) {
(Some(Target::Headless(a)), Some(Target::Headless(b))) => Arc::ptr_eq(a, b),
(Some(Target::Bridge(a)), Some(Target::Bridge(b))) => Arc::ptr_eq(a, b),
(None, None) => true,
_ => false,
}
}
fn bridge_only_headless(name: &str) -> Result<CallToolResult, McpError> {
Ok(fail(format!("`{name}` is only available in headless mode (in the GUI use the window/tab UI or control_call)")))
}
@@ -1119,11 +1170,13 @@ impl PhotocraftMcp {
/// Run one command in either backend; `at` is its place in a batch, for progress.
async fn step(&self, id: String, params: Value, wait: bool, waiter: &mut Waiter, at: (usize, usize)) -> Result<Value, AutomationError> {
let none = || Err(AutomationError::Other("internal error: no headless session or app bridge".into()));
match &*self.backend {
Backend::Headless(_) if wait => self.headless_wait(id, params, waiter, at).await.unwrap_or_else(none),
Backend::Headless(_) => self.headless_op(move |h| h.command_start(&id, params, false)).await.unwrap_or_else(none),
Backend::Bridge(b) if wait => self.bridge_wait(b, id, params, waiter, at).await,
Backend::Bridge(b) => b.call("engine.execute", json!({"command": id, "params": params, "wait": false})).await,
// Live mode goes wherever its last resolve pointed: the app, or a headless session.
match (self.headless_session(), self.bridge_client()) {
(Some(_), _) if wait => self.headless_wait(id, params, waiter, at).await.unwrap_or_else(none),
(Some(_), _) => self.headless_op(move |h| h.command_start(&id, params, false)).await.unwrap_or_else(none),
(None, Some(b)) if wait => self.bridge_wait(&b, id, params, waiter, at).await,
(None, Some(b)) => b.call("engine.execute", json!({"command": id, "params": params, "wait": false})).await,
(None, None) => none(),
}
}
@@ -1137,11 +1190,19 @@ impl PhotocraftMcp {
let mut failed = 0;
// The reply travels as escaped JSON text, so steps are charged their escaped size.
let mut reply_budget = BatchReplyBudget::escaped();
// Live mode may find a restarted app (or go headless) while a batch runs: its steps must
// not end up split across two documents in two places.
let pinned = self.target();
for (i, s) in p.steps.into_iter().enumerate() {
// A cancelled request runs no further steps; the client no longer reads the reply.
if waiter.is_cancelled() {
break;
}
if !same_target(pinned.as_ref(), self.target().as_ref()) {
failed += 1;
results.push(json!({"ok": false, "error": "PhotoCraft restarted (or the session changed) during the batch; this step and the ones after it did not run"}));
break;
}
let response = self.step(s.id, s.params.unwrap_or_else(|| json!({})), s.wait.unwrap_or(true), &mut waiter, (i, total)).await;
let was_error = response.is_err();
let result = match response {
+8 -2
View File
@@ -1,5 +1,5 @@
//! Catalog metadata, strict tool envelopes and live resources.
use super::{Arc, Backend, BatchParams, DocIndex, ListParams, McpError, Parameters, PhotocraftMcp, RunParams, ServerHandler, Value, json};
use super::{Arc, BatchParams, DocIndex, ListParams, McpError, Parameters, PhotocraftMcp, RunParams, ServerHandler, Value, fail, json};
use rmcp::RoleServer;
use rmcp::model::{
CallToolRequestParams, CallToolResponse, Implementation, ListResourcesResult, ListToolsResult, PaginatedRequestParams, ReadResourceRequestParams,
@@ -55,7 +55,10 @@ impl ServerHandler for PhotocraftMcp {
let args = Value::Object(request.arguments.clone().unwrap_or_default());
let _: BatchParams = serde_json::from_value(args).map_err(|e| McpError::invalid_params(e.to_string(), None))?;
}
if request.name == "command_run" && matches!(&*self.backend, Backend::Headless(_)) {
if let Err(error) = self.ready().await {
return Ok(fail(error).into());
}
if request.name == "command_run" && self.headless_session().is_some() {
let args = Value::Object(request.arguments.clone().unwrap_or_default());
let params: RunParams = serde_json::from_value(args).map_err(|e| McpError::invalid_params(e.to_string(), None))?;
if params.id == "file.export.renderVideo" {
@@ -74,6 +77,9 @@ impl ServerHandler for PhotocraftMcp {
}
async fn read_resource(&self, request: ReadResourceRequestParams, _: RequestContext<RoleServer>) -> Result<ReadResourceResponse, McpError> {
if let Err(error) = self.ready().await {
return Err(McpError::internal_error(error, None));
}
let result = match request.uri.as_str() {
DOCUMENT => self.doc_inspect(Parameters(DocIndex::default())).await?,
COMMANDS => self.command_list(Parameters(ListParams::default())).await?,
+12 -12
View File
@@ -1,8 +1,9 @@
//! Progress and cancellation for calls that wait. Render Video runs on the existing blocking
//! worker; other long commands run as engine jobs that the server polls ([`Waiter`]): the headless
//! session's own, or the app's over the bridge.
use super::{Arc, AutomationError, Backend, BridgeClient, CallToolResult, PhotocraftMcp, PoisonError, Value, fail, json, no_backend, ok_json};
use super::{Arc, AutomationError, BridgeClient, CallToolResult, PhotocraftMcp, PoisonError, Value, fail, json, no_backend, ok_json};
use crate::headless::JobPoll;
use crate::live::Target;
use photocraft_engine::jobs::{JobId, Started};
use rmcp::RoleServer;
use rmcp::model::ProgressNotificationParam;
@@ -92,7 +93,9 @@ impl Waiter {
/// Cancels a waited job when its waiter goes away without collecting it: rmcp may drop the
/// request future on cancellation, and nothing else would stop the job.
struct Abandon {
backend: Arc<Backend>,
/// The session or app that started the job: in live mode the current target can change
/// before the guard drops, and the job id means nothing elsewhere.
target: Target,
job: Option<JobId>,
}
@@ -100,8 +103,8 @@ impl Drop for Abandon {
fn drop(&mut self) {
let Some(job) = self.job.take() else { return };
let runtime = tokio::runtime::Handle::try_current();
match &*self.backend {
Backend::Headless(session) => {
match &self.target {
Target::Headless(session) => {
let session = session.clone();
let abandon = move || drop(session.lock().unwrap_or_else(PoisonError::into_inner).job_abandon(job));
match runtime {
@@ -111,7 +114,7 @@ impl Drop for Abandon {
}
}
// Without a runtime there is no way to reach the app; the job then runs to the end.
Backend::Bridge(client) => {
Target::Bridge(client) => {
if let Ok(runtime) = runtime {
let client = client.clone();
drop(runtime.spawn(async move { client.call("jobs.cancel", json!({"job": job.0})).await }));
@@ -127,10 +130,7 @@ impl PhotocraftMcp {
/// `waiter` as `step.0 + fraction` of `step.1`, and cancelling the request cancels the job,
/// leaving the document unchanged. `None` in bridge mode.
pub(super) async fn headless_wait(&self, id: String, params: Value, waiter: &mut Waiter, step: (usize, usize)) -> Option<Result<Value, AutomationError>> {
if !matches!(&*self.backend, Backend::Headless(_)) {
return None;
}
let backend = self.backend.clone();
let target = Target::Headless(self.headless_session()?);
let cancel = waiter.context.as_ref().map(|context| context.ct.clone());
// The guard is armed under the lock that starts the job: when the request is dropped while
// the start still waits for the lock, the closure's output is dropped unread and cancels it.
@@ -143,7 +143,7 @@ impl PhotocraftMcp {
Started::Job(job) if cancel.as_ref().is_some_and(|token| token.is_cancelled()) => {
return h.job_abandon(job).map(Err);
}
Started::Job(job) => Ok(Abandon { backend, job: Some(job) }),
Started::Job(job) => Ok(Abandon { target, job: Some(job) }),
Started::Done(value) => Err(value),
})
})
@@ -187,7 +187,7 @@ impl PhotocraftMcp {
/// (#1007); a failed poll reports the job instead of guessing its outcome.
pub(super) async fn bridge_wait(
&self,
client: &BridgeClient,
client: &Arc<BridgeClient>,
id: String,
params: Value,
waiter: &mut Waiter,
@@ -202,7 +202,7 @@ impl PhotocraftMcp {
let Some(job) = started.get("job").and_then(Value::as_u64).filter(|_| pending) else {
return Ok(started);
};
let mut guard = Abandon { backend: self.backend.clone(), job: Some(JobId(job)) };
let mut guard = Abandon { target: Target::Bridge(client.clone()), job: Some(JobId(job)) };
let mut delay = Duration::from_millis(10);
loop {
// Race the poll against a cancel: an app slow to answer must not hold it until the call times out.
+19 -1
View File
@@ -56,6 +56,20 @@ impl AuthorizedWorkspace {
Ok(Self { read, write })
}
/// This workspace with `path` as its only read root (the write authority stays). A path
/// spelled like the write root shares its capability, as [`Self::with_read_roots`] does, so
/// an implicit save can still write where a file was read.
pub fn with_read_root(&self, path: &Path) -> Result<Self, AutomationError> {
let opened: Vec<RootCapability> = self.write.iter().cloned().collect();
Ok(Self { read: vec![open_root(path, "read", &opened)?], write: self.write.clone() })
}
/// This workspace with `path` as its write root (the read authority stays), sharing the
/// capability of a read root spelled the same.
pub fn with_write_root(&self, path: &Path) -> Result<Self, AutomationError> {
Ok(Self { read: self.read.clone(), write: Some(open_root(path, "write", &self.read)?) })
}
/// Read the first matching regular file, trying roots in their configured order.
/// Only an open that reports NotFound tries the next root. Once opened, errors
/// (including a non-regular file or a failed bounded read) never select another file.
@@ -738,8 +752,12 @@ fn preference_uses_ambient_filesystem(path: &str) -> bool {
let mut segments = path.split('.').filter(|segment| !segment.is_empty());
let Some(section) = segments.next() else { return true };
// File Handling's LUT folder (#6558) is listed from disk; the rest of that section is not.
// Integrations' automation roots are what this policy grants (applied at once): an agent must
// not widen its own file access.
let key = segments.next();
matches!(section, "colorSettings" | "scriptEvents" | "historyLog" | "plugIns" | "scratchDisks")
|| (section == "fileHandling" && segments.next().is_none_or(|key| key == "lutFolder"))
|| (section == "fileHandling" && key.is_none_or(|key| key == "lutFolder"))
|| (section == "integrations" && key.is_none_or(|key| matches!(key, "readRoot" | "writeRoot")))
}
fn command_uses_ambient_path(id: &str, params: &Value) -> bool {
+11 -1
View File
@@ -802,6 +802,10 @@ fn automation_rejects_ambient_path_commands_and_preferences() {
"fileHandling.lutFolder",
"fileHandling",
"scratchDisks.disks",
"integrations",
"integrations.readRoot",
"integrations.writeRoot",
".integrations.writeRoot",
".",
"..historyLog.filePath",
] {
@@ -812,6 +816,12 @@ fn automation_rejects_ambient_path_commands_and_preferences() {
);
assert!(authorize_engine_command("prefs.set", &serde_json::json!({"path": "interface.language", "value": "fr"})).is_ok());
assert!(authorize_engine_command("prefs.set", &serde_json::json!({"path": "fileHandling.autosave", "value": false})).is_ok());
assert!(
authorize_engine_command("prefs.set", &serde_json::json!({"values": {"integrations.controlPort": 7878, "integrations.writeRoot": "/outside"}}))
.is_err(),
"a batch may not grant automation roots either"
);
assert!(authorize_engine_command("prefs.set", &serde_json::json!({"path": "integrations.controlPort", "value": 7878})).is_ok());
}
#[test]
@@ -1508,7 +1518,7 @@ fn csh_import_requires_inline_data_in_automation() {
/// A one-shape CSH set (version 2) named `name`, with a single open one-knot subpath.
fn one_shape_csh(name: &str) -> Vec<u8> {
use photocraft_psd::path::{PathData, PsdKnot, PsdSubpath};
use craft_psd::path::{PathData, PsdKnot, PsdSubpath};
let mut bytes = b"cush".to_vec();
bytes.extend_from_slice(&2u32.to_be_bytes());
bytes.extend_from_slice(&1u32.to_be_bytes());
@@ -0,0 +1,29 @@
//! `files::save_bytes` is the capability-scoped encoder: a linked path inside the document must
//! not be read through ambient filesystem authority and embedded in the returned bytes.
use photocraft_automation::files;
use photocraft_doc::{Document, Layer, LayerContent, SmartContentsId, SmartObject, SmartSource};
#[test]
fn a_linked_smart_object_is_not_read_into_the_bytes() {
let mut doc = Document::new("linked", photocraft_doc::Size::new(8, 6), photocraft_doc::ColorMode::Rgb, photocraft_doc::SampleType::U8);
let linked = std::env::current_exe().unwrap();
let smart = SmartObject {
contents_id: SmartContentsId::fresh(),
source: SmartSource::Linked { path: linked.to_string_lossy().into_owned() },
transform: photocraft_geom::Affine::IDENTITY,
smart_filters: vec![],
cache: Some(photocraft_raster::Surface::new(doc.pixel_format())),
psd_raw: None,
filters_enabled: true,
filter_mask: None,
warp: None,
stack_mode: None,
perspective: None,
vector_preview: None,
};
doc.layers.push(Layer::new("Linked", LayerContent::Smart(smart)));
let (bytes, warnings) = files::save_bytes(&doc, "out.psd", None, &Default::default()).unwrap();
assert!(warnings.iter().any(|w| w.contains("not read for this export")), "{warnings:?}");
assert!(bytes.len() < 1 << 20, "the test executable was not embedded");
}
+1
View File
@@ -9,6 +9,7 @@ mod gpu_backend;
mod inactive_save;
mod krita;
mod layered_save_dirty;
mod linked_save_bytes;
mod mcp;
mod mcp_wire;
mod missing_font_warnings;
+2 -2
View File
@@ -532,7 +532,7 @@ async fn headless_doc_save_takes_maximize_compatibility() {
let r = call(&client, tool, params.clone()).await;
assert_ne!(r.is_error, Some(true), "{params}: {}", text(&r));
let bytes = std::fs::read(dir.join(params["path"].as_str().unwrap())).unwrap();
assert_eq!(photocraft_psd::PsdFile::from_bytes(&bytes).unwrap().has_real_merged_data(), Some(real), "{params}");
assert_eq!(craft_psd::PsdFile::from_bytes(&bytes).unwrap().has_real_merged_data(), Some(real), "{params}");
}
let r = call(&client, "doc_export", json!({"path": "bad.psd", "maximizeCompatibility": "no"})).await;
assert_eq!(r.is_error, Some(true), "{}", text(&r));
@@ -555,7 +555,7 @@ async fn headless_doc_export_layers_takes_maximize_compatibility() {
let file = result["files"][0].as_str().unwrap();
assert!(file.starts_with(sub), "{file}");
let bytes = std::fs::read(dir.join(file)).unwrap();
assert_eq!(photocraft_psd::PsdFile::from_bytes(&bytes).unwrap().has_real_merged_data(), Some(real), "{params}");
assert_eq!(craft_psd::PsdFile::from_bytes(&bytes).unwrap().has_real_merged_data(), Some(real), "{params}");
}
let r = call(&client, "doc_export_layers", json!({"dir": "bad", "format": "psd", "maximizeCompatibility": "no"})).await;
assert_eq!(r.is_error, Some(true), "{}", text(&r));
+378
View File
@@ -0,0 +1,378 @@
//! Plain `photocraft-cli mcp` with agents allowed (the ArtCraft Launcher's switch): the MCP server finds the desktop
//! app through its connection file before every call, follows it to a new port and token, and
//! applies the closed-app choice (start it, headless, or an error) when no app answers.
use std::io::{BufRead, BufReader, Write};
use std::net::TcpListener;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use photocraft_automation::PhotocraftMcp;
use photocraft_automation::agent_link::{self, Connection};
use photocraft_automation::live::LiveConfig;
use photocraft_engine::prefs::WhenAppClosed;
use rmcp::model::{CallToolRequestParams, CallToolResult, ClientConfig};
use rmcp::service::RunningService;
use rmcp::{ClientHandler, RoleClient, ServiceExt};
use serde_json::{Value, json};
#[derive(Clone, Default)]
struct Client;
impl ClientHandler for Client {
fn get_info(&self) -> ClientConfig {
ClientConfig::default()
}
}
async fn connect(server: PhotocraftMcp) -> RunningService<RoleClient, Client> {
let (s, c) = tokio::io::duplex(1 << 20);
tokio::spawn(async move {
if let Ok(running) = server.serve(s).await {
let _ = running.waiting().await;
}
});
Client.serve(c).await.expect("client init")
}
async fn call(client: &RunningService<RoleClient, Client>, name: &str, args: Value) -> CallToolResult {
let mut p = CallToolRequestParams::new(name.to_owned());
if let Value::Object(m) = args {
p = p.with_arguments(m);
}
client.call_tool(p).await.expect("call_tool transport")
}
fn text(r: &CallToolResult) -> String {
r.content.iter().filter_map(|c| c.as_text()).map(|t| t.text.clone()).collect::<Vec<_>>().join("\n")
}
fn settings_dir(tag: &str) -> PathBuf {
static N: AtomicUsize = AtomicUsize::new(0);
let d = std::env::temp_dir().join(format!("pc-mcp-live-{tag}-{}-{}", std::process::id(), N.fetch_add(1, Ordering::Relaxed)));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
d
}
fn prefs(dir: &Path, integrations: Value) {
std::fs::write(dir.join("preferences.json"), json!({"integrations": integrations}).to_string()).unwrap();
}
/// A desktop app as `mcp` sees it: a control server on a free port with a fresh token, its
/// connection file published in `dir`. `ui.inspect` answers with `name`. [`quit`] stops it.
struct FakeApp {
port: u16,
stop: Arc<std::sync::atomic::AtomicBool>,
}
fn fake_app(dir: &Path, name: &'static str) -> FakeApp {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
let token_file = dir.join(agent_link::TOKEN_FILE);
let token = photocraft_automation::security::replace_token_file(&token_file).unwrap();
agent_link::write_connection(dir, &Connection::new(port, token_file)).unwrap();
let stop = Arc::new(std::sync::atomic::AtomicBool::new(false));
let stopped = stop.clone();
std::thread::spawn(move || {
for stream in listener.incoming().flatten() {
if stopped.load(Ordering::SeqCst) {
break;
}
let token = token.clone();
std::thread::spawn(move || {
let mut out = stream.try_clone().unwrap();
// The app's own handshake (challenge or token), as `control_server` runs it.
let mut handshake = photocraft_automation::security::ServerHandshake::new();
let mut authenticated = false;
for line in BufReader::new(stream).lines() {
let Ok(line) = line else { break };
if !authenticated {
let (reply, step) = handshake.reply(&line, &token);
authenticated = step == photocraft_automation::security::AuthStep::Authenticated;
if writeln!(out, "{reply}").is_err() || step == photocraft_automation::security::AuthStep::Rejected {
break;
}
continue;
}
let req: Value = serde_json::from_str(&line).unwrap();
let id = req["id"].clone();
let reply = match req["method"].as_str().unwrap_or("") {
"ui.inspect" => json!({"id": id, "ok": true, "result": {"app": name}}),
m => json!({"id": id, "ok": false, "error": format!("unexpected {m}")}),
};
if writeln!(out, "{reply}").is_err() {
break;
}
}
});
}
});
FakeApp { port, stop }
}
/// The fake app quits: its port stops accepting (connections already open end with the test).
fn quit(app: FakeApp) {
app.stop.store(true, Ordering::SeqCst);
let _ = std::net::TcpStream::connect(("127.0.0.1", app.port));
while agent_link::probe(app.port, Duration::from_millis(200)) {
std::thread::sleep(Duration::from_millis(10));
}
}
fn live(dir: &Path, when_closed: Option<WhenAppClosed>) -> LiveConfig {
let mut c = LiveConfig::new(dir.to_path_buf());
c.when_closed = when_closed;
c.launch_timeout = Duration::from_secs(10);
// The machine's own ArtCraft Launcher and feature flags stay out of the tests: agents are
// allowed exactly when the test's preferences say so (as without the AI-MCP flag).
c.allowed = |i| i.connect_agents;
c
}
#[tokio::test(flavor = "multi_thread")]
async fn a_closed_app_is_a_tool_error_that_says_what_to_do() {
let dir = settings_dir("closed");
prefs(&dir, json!({"connectAgents": true}));
let client = connect(PhotocraftMcp::live(live(&dir, Some(WhenAppClosed::Error)))).await;
// The tool list doesn't need the app.
assert!(client.list_all_tools().await.unwrap().iter().any(|t| t.name == "ui_inspect"));
let r = call(&client, "command_list", json!({})).await;
assert_eq!(r.is_error, Some(true));
assert!(text(&r).contains("isn't running"), "{}", text(&r));
// A connection file left by a crashed app (nothing listens) counts as closed.
let gone = TcpListener::bind("127.0.0.1:0").unwrap();
let port = gone.local_addr().unwrap().port();
drop(gone);
agent_link::write_connection(&dir, &Connection::new(port, dir.join(agent_link::TOKEN_FILE))).unwrap();
let r = call(&client, "ui_inspect", json!({})).await;
assert!(text(&r).contains("isn't running"), "{}", text(&r));
client.cancel().await.unwrap();
let _ = std::fs::remove_dir_all(&dir);
}
#[tokio::test(flavor = "multi_thread")]
async fn follows_the_app_across_restarts_on_new_ports_and_tokens() {
let dir = settings_dir("follow");
prefs(&dir, json!({"connectAgents": true, "whenAppClosed": "error"}));
let client = connect(PhotocraftMcp::live(live(&dir, None))).await;
let first = fake_app(&dir, "first");
let r = call(&client, "ui_inspect", json!({})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
assert!(text(&r).contains("first"));
// Restart: another port, a new token.
quit(first);
let second = fake_app(&dir, "second");
let r = call(&client, "ui_inspect", json!({})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
assert!(text(&r).contains("second"), "{}", text(&r));
quit(second);
std::fs::remove_file(dir.join(agent_link::CONNECTION_FILE)).unwrap();
let r = call(&client, "ui_inspect", json!({})).await;
assert_eq!(r.is_error, Some(true));
client.cancel().await.unwrap();
let _ = std::fs::remove_dir_all(&dir);
}
#[tokio::test(flavor = "multi_thread")]
async fn launch_starts_the_app_once_and_only_when_agents_may_connect() {
let dir = settings_dir("launch");
let launches = Arc::new(AtomicUsize::new(0));
let apps = Arc::new(std::sync::Mutex::new(Vec::new()));
let config = |dir: &Path| {
let mut c = live(dir, Some(WhenAppClosed::Launch));
let (launches, apps, dir) = (launches.clone(), apps.clone(), dir.to_path_buf());
c.launch = Some(Box::new(move || {
launches.fetch_add(1, Ordering::SeqCst);
let (apps, dir) = (apps.clone(), dir.clone());
// The app takes a moment to start listening.
std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(300));
apps.lock().unwrap().push(fake_app(&dir, "launched"));
});
Ok(())
}));
c
};
// Agents not allowed: the app would never listen, so it isn't started; the session works
// without a window, as `mcp` always did.
let client = connect(PhotocraftMcp::live(config(&dir))).await;
let r = call(&client, "ui_inspect", json!({})).await;
assert!(text(&r).contains("bridge"), "UI tools need the open app: {}", text(&r));
let r = call(&client, "command_list", json!({})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
assert_eq!(launches.load(Ordering::SeqCst), 0);
client.cancel().await.unwrap();
// The ArtCraft Launcher's switch is the consent: the app is started on it alone.
let mut c = config(&dir);
c.allowed = |_| true;
let client = connect(PhotocraftMcp::live(c)).await;
let r = call(&client, "ui_inspect", json!({})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
assert_eq!(launches.load(Ordering::SeqCst), 1);
client.cancel().await.unwrap();
for app in apps.lock().unwrap().drain(..) {
quit(app);
}
launches.store(0, Ordering::SeqCst);
prefs(&dir, json!({"connectAgents": true}));
let client = connect(PhotocraftMcp::live(config(&dir))).await;
// Two calls at once start one app.
let (a, b) = tokio::join!(call(&client, "ui_inspect", json!({})), call(&client, "ui_inspect", json!({})));
for r in [a, b] {
assert_eq!(r.is_error, Some(false), "{}", text(&r));
assert!(text(&r).contains("launched"));
}
assert_eq!(launches.load(Ordering::SeqCst), 1);
client.cancel().await.unwrap();
for app in apps.lock().unwrap().drain(..) {
quit(app);
}
let _ = std::fs::remove_dir_all(&dir);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_launch_that_never_listens_times_out_with_advice() {
let dir = settings_dir("timeout");
prefs(&dir, json!({"connectAgents": true}));
let mut c = live(&dir, Some(WhenAppClosed::Launch));
c.launch_timeout = Duration::from_millis(600);
c.launch = Some(Box::new(|| Ok(())));
let client = connect(PhotocraftMcp::live(c)).await;
let r = call(&client, "ui_inspect", json!({})).await;
assert_eq!(r.is_error, Some(true));
assert!(text(&r).contains("didn't accept agents"), "{}", text(&r));
// A launcher that fails says so.
let mut c = live(&dir, Some(WhenAppClosed::Launch));
c.launch = Some(Box::new(|| Err("no such file".into())));
let client2 = connect(PhotocraftMcp::live(c)).await;
let r = call(&client2, "ui_inspect", json!({})).await;
assert!(text(&r).contains("couldn't start PhotoCraft: no such file"), "{}", text(&r));
// No launcher (Flatpak, the app not installed beside the CLI): ask the user.
let client3 = connect(PhotocraftMcp::live(live(&dir, Some(WhenAppClosed::Launch)))).await;
let r = call(&client3, "ui_inspect", json!({})).await;
assert!(text(&r).contains("open PhotoCraft"), "{}", text(&r));
for c in [client, client2, client3] {
c.cancel().await.unwrap();
}
let _ = std::fs::remove_dir_all(&dir);
}
#[tokio::test(flavor = "multi_thread")]
async fn headless_uses_the_app_folders_and_stays_headless() {
let dir = settings_dir("headless");
let work = dir.join("work");
std::fs::create_dir_all(&work).unwrap();
prefs(&dir, json!({"connectAgents": true, "whenAppClosed": "headless", "writeRoot": work.to_string_lossy()}));
let client = connect(PhotocraftMcp::live(live(&dir, None))).await;
let r = call(&client, "doc_new", json!({"width": 8, "height": 8})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
let r = call(&client, "doc_save", json!({"path": "out.pcraft"})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
assert!(work.join("out.pcraft").exists(), "saved into the app's write folder");
// No read folder was chosen: reads are refused.
let r = call(&client, "doc_open", json!({"path": "out.pcraft"})).await;
assert_eq!(r.is_error, Some(true));
// The app starting later doesn't move the documents away from the agent.
let app = fake_app(&dir, "late");
let r = call(&client, "ui_inspect", json!({})).await;
assert!(text(&r).contains("bridge"), "UI tools stay unavailable in the headless session: {}", text(&r));
let r = call(&client, "session_list", json!({})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
client.cancel().await.unwrap();
quit(app);
// A folder that doesn't exist is a clear error, not a crash.
prefs(&dir, json!({"connectAgents": true, "whenAppClosed": "headless", "readRoot": dir.join("missing").to_string_lossy()}));
std::fs::remove_file(dir.join(agent_link::CONNECTION_FILE)).unwrap();
let client = connect(PhotocraftMcp::live(live(&dir, None))).await;
let r = call(&client, "command_list", json!({})).await;
assert_eq!(r.is_error, Some(true));
assert!(text(&r).contains("agent folders"), "{}", text(&r));
client.cancel().await.unwrap();
let _ = std::fs::remove_dir_all(&dir);
}
/// Agents allowed, app closed, headless: a root flag given to `mcp` replaces the app's folder for
/// its own direction only; the other direction still falls back to the app's folder.
#[tokio::test(flavor = "multi_thread")]
async fn headless_root_flags_override_each_direction_on_their_own() {
let dir = settings_dir("headless-override");
let (app_read, app_write, given) = (dir.join("app-read"), dir.join("app-write"), dir.join("given"));
for d in [&app_read, &app_write, &given] {
std::fs::create_dir_all(d).unwrap();
}
prefs(&dir, json!({"connectAgents": true, "whenAppClosed": "headless", "readRoot": app_read.to_string_lossy(), "writeRoot": app_write.to_string_lossy()}));
// Only the write root is given: saves go there, reads still come from the app's read folder.
let mut c = live(&dir, None);
c.write_root = Some(given.clone());
let client = connect(PhotocraftMcp::live(c)).await;
let r = call(&client, "doc_new", json!({"width": 8, "height": 8})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
let r = call(&client, "doc_save", json!({"path": "out.pcraft"})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
assert!(given.join("out.pcraft").exists(), "saved into the given write root");
assert!(!app_write.join("out.pcraft").exists(), "not into the app's write folder");
std::fs::copy(given.join("out.pcraft"), app_read.join("in.pcraft")).unwrap();
let r = call(&client, "doc_open", json!({"path": app_read.join("in.pcraft").to_string_lossy()})).await;
assert_eq!(r.is_error, Some(false), "the app's read folder still applies: {}", text(&r));
client.cancel().await.unwrap();
// Only a read root is given: it replaces the app's read folder; saves still go to the app's write folder.
let mut c = live(&dir, None);
c.read_roots = vec![given.clone()];
let client = connect(PhotocraftMcp::live(c)).await;
let r = call(&client, "doc_open", json!({"path": app_read.join("in.pcraft").to_string_lossy()})).await;
assert_eq!(r.is_error, Some(true), "the app's read folder is replaced by the given one");
let r = call(&client, "doc_open", json!({"path": given.join("out.pcraft").to_string_lossy()})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
let r = call(&client, "doc_save", json!({"path": "again.pcraft"})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
assert!(app_write.join("again.pcraft").exists(), "saved into the app's write folder");
client.cancel().await.unwrap();
let _ = std::fs::remove_dir_all(&dir);
}
/// Agents not allowed: `mcp` serves a session without a window as it always did, with only the
/// folders given to the command (the app's own folders are for agents the person let in), never
/// starts the app, and prepares the session as the command asked (`--gpu`).
#[tokio::test(flavor = "multi_thread")]
async fn without_consent_mcp_is_headless_with_its_own_folders() {
static PREPARED: AtomicUsize = AtomicUsize::new(0);
let dir = settings_dir("no-consent");
let (app_folder, given) = (dir.join("app-folder"), dir.join("given"));
std::fs::create_dir_all(&app_folder).unwrap();
std::fs::create_dir_all(&given).unwrap();
prefs(&dir, json!({"whenAppClosed": "launch", "writeRoot": app_folder.to_string_lossy()}));
let launches = Arc::new(AtomicUsize::new(0));
let config = |write_root: Option<PathBuf>| {
let mut c = live(&dir, None);
c.write_root = write_root;
let launches = launches.clone();
c.launch = Some(Box::new(move || {
launches.fetch_add(1, Ordering::SeqCst);
Ok(())
}));
c.prepare_headless = Some(Box::new(|_| {
PREPARED.fetch_add(1, Ordering::SeqCst);
}));
c
};
let client = connect(PhotocraftMcp::live(config(None))).await;
let r = call(&client, "doc_new", json!({"width": 8, "height": 8})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
let r = call(&client, "doc_save", json!({"path": "out.pcraft"})).await;
assert_eq!(r.is_error, Some(true), "the app's folder isn't granted without consent");
assert!(!app_folder.join("out.pcraft").exists());
client.cancel().await.unwrap();
let client = connect(PhotocraftMcp::live(config(Some(given.clone())))).await;
call(&client, "doc_new", json!({"width": 8, "height": 8})).await;
let r = call(&client, "doc_save", json!({"path": "out.pcraft"})).await;
assert_eq!(r.is_error, Some(false), "{}", text(&r));
assert!(given.join("out.pcraft").exists(), "the command's own root is used");
client.cancel().await.unwrap();
assert_eq!(launches.load(Ordering::SeqCst), 0, "never started without consent");
assert_eq!(PREPARED.load(Ordering::SeqCst), 2, "each headless session is prepared once");
let _ = std::fs::remove_dir_all(&dir);
}
+87 -2
View File
@@ -2,13 +2,18 @@
use crate::clut::Clut;
use crate::pipeline::{Pipeline, Stage};
use crate::profile::Profile;
use crate::profile::{ColorSpace, Profile};
use crate::transform::link_stages;
use crate::{CmsError, Intent};
/// Default ΔE76 threshold above which a colour counts as out of gamut.
pub const DEFAULT_THRESHOLD: f32 = 4.0;
#[cfg(not(target_arch = "wasm32"))]
const PAR_MIN_PIXELS: usize = 16 * 1024;
#[cfg(not(target_arch = "wasm32"))]
const PAR_CHUNK_PIXELS: usize = 16 * 1024;
/// Precomputed out-of-gamut test for colours of `src` against `proof`.
///
/// A colour is out of gamut when sending it to the proof device (relative colorimetric,
@@ -17,6 +22,7 @@ pub const DEFAULT_THRESHOLD: f32 = 4.0;
#[derive(Debug)]
pub struct GamutCheck {
inputs: usize,
source_space: ColorSpace,
grid: Option<Clut>,
exact: (Pipeline, Pipeline),
pub threshold: f32,
@@ -44,7 +50,7 @@ impl GamutCheck {
4 => Some(Clut::sample(vec![17; 4], 1, de)),
_ => None,
};
Ok(GamutCheck { inputs: n, grid, exact: (direct, via), threshold })
Ok(GamutCheck { inputs: n, source_space: src.color_space, grid, exact: (direct, via), threshold })
}
pub fn inputs(&self) -> usize {
@@ -75,4 +81,83 @@ impl GamutCheck {
pub fn out_of_gamut(&self, px: &[f32]) -> bool {
self.delta_e(px) > self.threshold
}
/// Out-of-gamut mask for RGBA pixels: `255` where the pixel has alpha > 0 and its RGB
/// channels are out of gamut, `0` elsewhere. Returns the number of flagged pixels. Only
/// `min(px.len(), mask.len())` pixels are processed. Runs on the rayon pool (not on wasm);
/// the result is identical to calling [`Self::out_of_gamut`] per pixel.
///
/// # Errors
/// [`CmsError::Invalid`] when the check was not built from an RGB source profile: an RGBA
/// pixel carries only RGB colour channels, so it cannot be tested against a CMYK, gray or
/// Lab source. Nothing is written to `mask` in that case.
pub fn mask_rgba(&self, px: &[[f32; 4]], mask: &mut [u8]) -> Result<usize, CmsError> {
if self.source_space != ColorSpace::Rgb || self.inputs != 3 {
return Err(CmsError::Invalid(format!("gamut mask needs an RGB source profile, got {:?} with {} channels", self.source_space, self.inputs)));
}
let n = px.len().min(mask.len());
let (px, mask) = (&px[..n], &mut mask[..n]);
let run = |p: &[[f32; 4]], m: &mut [u8]| {
let mut count = 0;
for (p, m) in p.iter().zip(m.iter_mut()) {
let hit = p[3] > 0.0 && self.out_of_gamut(&p[..3]);
*m = if hit { 255 } else { 0 };
count += usize::from(hit);
}
count
};
#[cfg(not(target_arch = "wasm32"))]
if n >= PAR_MIN_PIXELS {
use rayon::prelude::*;
return Ok(px.par_chunks(PAR_CHUNK_PIXELS).zip(mask.par_chunks_mut(PAR_CHUNK_PIXELS)).map(|(p, m)| run(p, m)).sum());
}
Ok(run(px, mask))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Builtin;
fn image(n: usize) -> Vec<[f32; 4]> {
let mut s = 0x9E37_79B9u32;
let mut rnd = move || {
s ^= s << 13;
s ^= s >> 17;
s ^= s << 5;
(s >> 8) as f32 / (1u32 << 24) as f32
};
// Includes out-of-range values and zero alpha.
(0..n).map(|i| [rnd() * 1.2 - 0.1, rnd(), rnd(), if i % 7 == 0 { 0.0 } else { rnd() }]).collect()
}
#[test]
fn mask_rgba_matches_per_pixel_check() {
let src = Builtin::Srgb.profile();
// Sizes straddle the parallel threshold and the chunk size, odd ones included.
for (proof, threshold) in [(Builtin::CoatedCmyk, 4.0), (Builtin::CoatedCmyk, 1.5), (Builtin::GrayGamma22, 4.0), (Builtin::DisplayP3, 0.5)] {
let check = GamutCheck::new(src, proof.profile(), threshold).unwrap();
for n in [0, 1, 1023, 16 * 1024 - 1, 16 * 1024, 16 * 1024 + 1, 50_001] {
let px = image(n);
let want: Vec<u8> = px.iter().map(|p| if p[3] > 0.0 && check.out_of_gamut(&p[..3]) { 255 } else { 0 }).collect();
let mut got = vec![7u8; n];
let count = check.mask_rgba(&px, &mut got).unwrap();
assert_eq!(got, want, "{proof:?} threshold {threshold} n {n}");
assert_eq!(count, want.iter().filter(|&&m| m == 255).count());
}
}
}
#[test]
fn mask_rgba_rejects_non_rgb_source() {
// A CMYK (4-channel), gray (1-channel) and Lab (3-channel, not RGB) source cannot be tested from RGBA pixels.
for src in [Builtin::CoatedCmyk, Builtin::GrayGamma22, Builtin::LabD50] {
let check = GamutCheck::new(src.profile(), Builtin::Srgb.profile(), 4.0).unwrap();
let px = image(64);
let mut mask = vec![9u8; 64];
assert!(matches!(check.mask_rgba(&px, &mut mask), Err(CmsError::Invalid(_))), "{src:?}");
assert!(mask.iter().all(|&m| m == 9), "mask must stay untouched");
}
}
}
+37
View File
@@ -185,6 +185,7 @@ impl Profile {
/// Parses ICC profile bytes.
pub fn parse(bytes: &[u8]) -> Result<Profile, CmsError> {
CURVE_SAMPLES_LEFT.with(|left| left.set(MAX_CURVE_SAMPLES));
CLUT_VALUES_LEFT.with(|left| left.set(MAX_PROFILE_CLUT_VALUES));
let mut p = parse_profile(bytes)?;
p.bytes = Some(Arc::new(bytes.to_vec()));
Ok(p)
@@ -595,6 +596,24 @@ thread_local! {
static CURVE_SAMPLES_LEFT: std::cell::Cell<usize> = const { std::cell::Cell::new(MAX_CURVE_SAMPLES) };
}
/// CLUT values one profile may decode in all (128 MB of `f32`): the twelve LUT tags can alias
/// one table, and each reference decodes its own copy.
const MAX_PROFILE_CLUT_VALUES: usize = 32 << 20;
thread_local! {
/// What is left of [`MAX_PROFILE_CLUT_VALUES`] for the profile being parsed on this thread.
static CLUT_VALUES_LEFT: std::cell::Cell<usize> = const { std::cell::Cell::new(MAX_PROFILE_CLUT_VALUES) };
}
/// Charges `n` decoded CLUT values to the profile being parsed.
fn charge_clut_values(n: usize) -> Result<(), CmsError> {
CLUT_VALUES_LEFT.with(|left| {
let rest = left.get().checked_sub(n).ok_or_else(|| CmsError::Invalid("the profile's lookup tables hold too many values".into()))?;
left.set(rest);
Ok(())
})
}
/// Charges `n` decoded curve samples to the profile being parsed.
fn charge_curve_samples(n: usize) -> Result<(), CmsError> {
CURVE_SAMPLES_LEFT.with(|left| {
@@ -698,6 +717,7 @@ fn parse_lut(d: &[u8], a2b: bool) -> Result<Lut, CmsError> {
return Err(CmsError::Invalid("CLUT too large".into()));
}
let total = nodes as usize * o;
charge_clut_values(total)?;
let s = r.slice(off, total * bps)?;
let data: Vec<f32> = if wide {
s.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]]) as f32 / 65535.0).collect()
@@ -803,6 +823,7 @@ fn lut_ab_clut(r: &Reader, off: usize, cin: usize, cout: usize) -> Result<Stage,
return Err(CmsError::Invalid("CLUT too large".into()));
}
let total = nodes as usize * cout;
charge_clut_values(total)?;
let samples = extent(off, 20, 1)?;
let data: Vec<f32> = match prec {
1 => r.slice(samples, total)?.iter().map(|b| *b as f32 / 255.0).collect(),
@@ -984,6 +1005,22 @@ mod tests {
mod curve_budget_tests {
use super::*;
#[test]
fn aliased_cluts_are_charged_for_each_decoded_table() {
// A 255³ × 1 CLUT (within one table's limit) referenced by LUT tags three times.
let mut element = vec![255u8, 255, 255];
element.resize(16, 0);
element.extend([1, 0, 0, 0]);
element.extend(std::iter::repeat_n(0u8, 255 * 255 * 255));
let r = Reader { b: &element };
CLUT_VALUES_LEFT.with(|left| left.set(MAX_PROFILE_CLUT_VALUES));
assert!(lut_ab_clut(&r, 0, 3, 1).is_ok());
assert!(lut_ab_clut(&r, 0, 3, 1).is_ok());
assert!(lut_ab_clut(&r, 0, 3, 1).is_err(), "the third decoded copy passes the profile's allowance");
CLUT_VALUES_LEFT.with(|left| left.set(MAX_PROFILE_CLUT_VALUES));
assert!(lut_ab_clut(&r, 0, 3, 1).is_ok());
}
#[test]
fn aliased_curves_are_charged_for_each_decoded_table() {
// One 1M-sample `curv` element referenced seventeen times decodes 17M samples.
+2 -2
View File
@@ -119,7 +119,7 @@ impl CmykModel {
(v + 4.0 * self.tvi[i] * v * (1.0 - v)).clamp(0.0, 1.0)
});
let mut s = [0.0; 3];
for m in 0..16 {
for (m, prim) in self.prim_n.iter().enumerate() {
let mut w = 1.0;
for (i, ai) in a.iter().enumerate() {
w *= if m & (1 << i) != 0 { *ai } else { 1.0 - ai };
@@ -127,7 +127,7 @@ impl CmykModel {
if w == 0.0 {
continue;
}
for (acc, p) in s.iter_mut().zip(&self.prim_n[m]) {
for (acc, p) in s.iter_mut().zip(prim) {
*acc += w * p;
}
}
+5
View File
@@ -33,6 +33,11 @@ zune-core = "0.5"
jpeg-encoder = "0.6"
# 12-bit and arithmetic-coded JPEG, which zune-jpeg does not decode.
craft-jpeg.workspace = true
craft-raster-codecs.workspace = true
craft-dpx.workspace = true
craft-tga.workspace = true
craft-gif.workspace = true
craft-exr.workspace = true
# Pure-Rust modular JPEG XL lossless encoder.
zune-jpegxl = { version = "=0.5.2", default-features = false, features = ["std"] }
craft-heif = { workspace = true, optional = true }
+2 -9
View File
@@ -1,20 +1,13 @@
pub(crate) mod dds;
pub(crate) mod dds_bc;
pub(crate) mod deep_exr;
pub(crate) mod dicom;
pub(crate) mod dpx;
pub(crate) mod exr;
pub(crate) mod exr_cryptomatte;
pub(crate) mod fits;
pub(crate) mod gif_frames;
pub(crate) mod gif_palette;
pub(crate) mod heif;
pub(crate) mod iff;
pub(crate) mod jpeg;
pub(crate) mod jxl;
pub(crate) mod pcx;
pub(crate) mod png;
pub(crate) mod pnm;
mod raster;
pub(crate) mod tga;
pub(crate) mod tiff;
pub(crate) mod tiff_exif;
@@ -22,4 +15,4 @@ pub(crate) mod tiff_ifd;
pub(crate) mod via_image;
pub(crate) mod vp8;
pub(crate) mod webp;
pub(crate) mod xpm;
pub(crate) use raster::{dds, dicom, fits, iff, pcx, pnm, xpm};
-556
View File
@@ -1,556 +0,0 @@
//! DirectDraw Surface (DDS) textures, after Microsoft's public DDS documentation ("DDS",
//! "DDS_HEADER", "DDS_PIXELFORMAT", "DDS_HEADER_DXT10").
//!
//! Reading: the top mip level of a 2D texture, either uncompressed (8 to 32 bits per pixel
//! described by channel masks, RGB or luminance) or block-compressed BC1/BC2/BC3
//! (DXT1/DXT3/DXT5), with or without the DX10 header. Cubemaps, texture arrays and volume
//! textures are refused rather than flattened to one face or slice.
//!
//! Writing: a 2D texture, uncompressed (BGRA or BGRX, luminance with or without alpha) or
//! BC1/BC2/BC3, optionally with its full mip chain. The legacy header describes all of these,
//! so no DX10 header is written; formats only DXGI can name (BC4–BC7, sRGB) aren't written.
use std::borrow::Cow;
use super::dds_bc::{self, Block, Mask};
use crate::SampleType;
use crate::error::CodecError;
use crate::fidelity::Plan;
use crate::format::Format;
use crate::image::{ChannelLayout, Image};
use crate::options::{DdsCompression, EncodeOptions, Limits};
/// `DDS ` plus the 124-byte DDS_HEADER.
const HEADER: usize = 128;
/// DDS_HEADER_DXT10, present when the FourCC is `DX10`.
const DX10_HEADER: usize = 20;
const DDSD_DEPTH: u32 = 0x80_0000;
const DDPF_ALPHAPIXELS: u32 = 0x1;
const DDPF_FOURCC: u32 = 0x4;
const DDPF_RGB: u32 = 0x40;
const DDPF_LUMINANCE: u32 = 0x2_0000;
const DDSCAPS2_CUBEMAP: u32 = 0x200;
const DDSCAPS2_CUBEMAP_FACES: u32 = 0xfc00;
const DDSCAPS2_VOLUME: u32 = 0x20_0000;
const DIMENSION_TEXTURE2D: u32 = 3;
const MISC_TEXTURECUBE: u32 = 0x4;
fn bad(reason: impl std::fmt::Display) -> CodecError {
CodecError::malformed(Format::Dds, reason)
}
fn unsupported(reason: impl Into<String>) -> CodecError {
CodecError::unsupported(Format::Dds, reason)
}
fn le32(b: &[u8], at: usize) -> Result<u32, CodecError> {
let bytes = b.get(at..at + 4).and_then(|s| <[u8; 4]>::try_from(s).ok()).ok_or_else(|| bad("truncated header"))?;
Ok(u32::from_le_bytes(bytes))
}
/// Header-declared extra subresources cannot survive a single-image rewrite, even without presence flags.
pub(crate) fn has_additional_images(bytes: &[u8]) -> Result<bool, CodecError> {
let depth = le32(bytes, 24)?;
let mips = le32(bytes, 28)?;
let faces = (le32(bytes, 112)? & DDSCAPS2_CUBEMAP_FACES).count_ones();
let dx10 = le32(bytes, 80)? & DDPF_FOURCC != 0 && bytes.get(84..88) == Some(b"DX10");
let (cube, array) = if dx10 { (le32(bytes, 136)? & MISC_TEXTURECUBE != 0, le32(bytes, 140)?) } else { (false, 0) };
Ok(depth > 1 || mips > 1 || faces > 1 || cube || array > 1)
}
/// A FourCC as text when it is printable, else as the number legacy writers store there.
fn fourcc_name(fourcc: u32) -> String {
let bytes = fourcc.to_le_bytes();
if bytes.iter().all(|c| c.is_ascii_graphic() || *c == b' ') { bytes.iter().map(|&c| char::from(c)).collect() } else { fourcc.to_string() }
}
/// One channel of an uncompressed pixel: where it sits and its largest value.
#[derive(Clone, Copy)]
struct Field {
shift: u32,
max: u32,
}
impl Field {
/// `None` for an empty mask; an error for one with gaps or wider than the pixel.
fn new(mask: u32, bits: u32) -> Result<Option<Field>, CodecError> {
if mask == 0 {
return Ok(None);
}
let shift = mask.trailing_zeros();
let max = mask >> shift;
if max & max.wrapping_add(1) != 0 || (bits < 32 && mask >> bits != 0) {
return Err(bad(format!("channel mask {mask:#x} is not a contiguous field of a {bits}-bit pixel")));
}
Ok(Some(Field { shift, max }))
}
fn get(self, pixel: u32) -> u8 {
let v = u64::from((pixel >> self.shift) & self.max);
let max = u64::from(self.max);
((v * 255 + max / 2) / max) as u8
}
}
enum Encoding {
/// Uncompressed: bytes per pixel, then red (or luminance), green, blue and alpha.
Masks {
bytes: usize,
gray: bool,
fields: [Option<Field>; 4],
},
Bc1,
Bc2,
Bc3,
}
impl Encoding {
fn masks(bits: u32, masks: [u32; 4], gray: bool) -> Result<Encoding, CodecError> {
if !matches!(bits, 8 | 16 | 24 | 32) {
return Err(bad(format!("{bits} bits per pixel is not 8, 16, 24 or 32")));
}
let mut fields = [None; 4];
for (field, mask) in fields.iter_mut().zip(masks) {
*field = Field::new(mask, bits)?;
}
let colour = if gray { fields[0].is_some() } else { fields[..3].iter().any(Option::is_some) };
if !colour {
return Err(bad("the pixel format has no colour channel masks"));
}
Ok(Encoding::Masks { bytes: bits as usize / 8, gray, fields })
}
fn layout(&self) -> ChannelLayout {
match self {
Encoding::Masks { gray: true, fields, .. } if fields[3].is_some() => ChannelLayout::GrayA,
Encoding::Masks { gray: true, .. } => ChannelLayout::Gray,
Encoding::Masks { fields, .. } if fields[3].is_none() => ChannelLayout::Rgb,
_ => ChannelLayout::Rgba,
}
}
/// Stored row stride and byte size of the top mip level (block rows for BC).
fn level_layout(&self, width: u32, height: u32, pitch: Option<u32>) -> Result<(usize, usize), CodecError> {
let overflow = || bad("pixel data size overflow");
let (w, h) = (usize::try_from(width).map_err(|_| overflow())?, usize::try_from(height).map_err(|_| overflow())?);
let (row_bytes, rows) = match self {
Encoding::Masks { bytes, .. } => {
let packed = w.checked_mul(*bytes).ok_or_else(overflow)?;
let row_bytes = match pitch {
Some(pitch) => usize::try_from(pitch).map_err(|_| overflow())?,
None => packed,
};
if row_bytes < packed {
return Err(bad(format!("row pitch {row_bytes} is smaller than the {packed}-byte pixel row")));
}
(row_bytes, h)
}
Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => {
let block = if matches!(self, Encoding::Bc1) { 8 } else { 16 };
(w.div_ceil(4).checked_mul(block).ok_or_else(overflow)?, h.div_ceil(4))
}
};
let size = row_bytes.checked_mul(rows).ok_or_else(overflow)?;
Ok((row_bytes, size))
}
}
/// The legacy pixel format: a FourCC, or channel masks.
fn legacy_encoding(b: &[u8]) -> Result<Encoding, CodecError> {
let (flags, fourcc, bits) = (le32(b, 80)?, le32(b, 84)?, le32(b, 88)?);
let alpha = if flags & DDPF_ALPHAPIXELS != 0 { le32(b, 104)? } else { 0 };
if flags & DDPF_FOURCC != 0 {
return match &fourcc.to_le_bytes() {
b"DXT1" => Ok(Encoding::Bc1),
b"DXT3" => Ok(Encoding::Bc2),
b"DXT5" => Ok(Encoding::Bc3),
b"DXT2" | b"DXT4" => Err(unsupported("premultiplied-alpha DXT2/DXT4 textures are not supported")),
_ => Err(unsupported(format!("compression `{}` is not supported (DXT1, DXT3 and DXT5 are)", fourcc_name(fourcc)))),
};
}
if flags & DDPF_RGB != 0 {
return Encoding::masks(bits, [le32(b, 92)?, le32(b, 96)?, le32(b, 100)?, alpha], false);
}
if flags & DDPF_LUMINANCE != 0 {
return Encoding::masks(bits, [le32(b, 92)?, 0, 0, alpha], true);
}
Err(unsupported(format!("pixel format flags {flags:#x} are not RGB, luminance or a supported compression")))
}
/// The DX10 header's DXGI format, refusing anything but a single 2D texture.
fn dx10_encoding(b: &[u8]) -> Result<Encoding, CodecError> {
let (dxgi, dimension, misc, array) = (le32(b, HEADER)?, le32(b, HEADER + 4)?, le32(b, HEADER + 8)?, le32(b, HEADER + 12)?);
if misc & MISC_TEXTURECUBE != 0 {
return Err(unsupported("cubemaps are not supported"));
}
if dimension != DIMENSION_TEXTURE2D {
return Err(unsupported(format!("only 2D textures are supported (resource dimension {dimension})")));
}
if array > 1 {
return Err(unsupported(format!("texture arrays are not supported ({array} textures)")));
}
const RGBA: [u32; 4] = [0xff, 0xff00, 0xff_0000, 0xff00_0000];
const BGRA: [u32; 4] = [0xff_0000, 0xff00, 0xff, 0xff00_0000];
const BGRX: [u32; 4] = [0xff_0000, 0xff00, 0xff, 0];
match dxgi {
27..=29 => Encoding::masks(32, RGBA, false),
87 | 90 | 91 => Encoding::masks(32, BGRA, false),
88 | 92 | 93 => Encoding::masks(32, BGRX, false),
70..=72 => Ok(Encoding::Bc1),
73..=75 => Ok(Encoding::Bc2),
76..=78 => Ok(Encoding::Bc3),
_ => Err(unsupported(format!("DXGI format {dxgi} is not supported (BC1, BC2, BC3 and 8-bit RGBA or BGRA are)"))),
}
}
pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
if !b.starts_with(b"DDS ") {
return Err(bad("missing the `DDS ` signature"));
}
if le32(b, 4)? != 124 {
return Err(bad("header size is not 124"));
}
let (flags, height, width, depth, caps2) = (le32(b, 8)?, le32(b, 12)?, le32(b, 16)?, le32(b, 24)?, le32(b, 112)?);
if caps2 & DDSCAPS2_CUBEMAP != 0 {
return Err(unsupported("cubemaps are not supported"));
}
if caps2 & DDSCAPS2_VOLUME != 0 || (flags & DDSD_DEPTH != 0 && depth > 1) {
return Err(unsupported("volume textures are not supported"));
}
let dx10 = le32(b, 80)? & DDPF_FOURCC != 0 && &le32(b, 84)?.to_le_bytes() == b"DX10";
let (encoding, start) = if dx10 { (dx10_encoding(b)?, HEADER + DX10_HEADER) } else { (legacy_encoding(b)?, HEADER) };
let layout = encoding.layout();
limits.check(width, height, layout, SampleType::U8)?;
// The same header field is a total level size for BC, but a row stride when DDSD_PITCH is set.
let pitch = if flags & DDSD_PITCH != 0 { Some(le32(b, 20)?) } else { None };
let (row_pitch, size) = encoding.level_layout(width, height, pitch)?;
let data = b.get(start..).and_then(|d| d.get(..size)).ok_or_else(|| bad(format!("truncated pixel data: {size} bytes needed")))?;
let (w, h) = (width as usize, height as usize);
let channels = layout.channels();
let row_size = w.checked_mul(channels).ok_or_else(|| bad("pixel size overflow"))?;
let total = row_size.checked_mul(h).ok_or_else(|| bad("pixel size overflow"))?;
let mut pixels = vec![0u8; total];
match encoding {
Encoding::Masks { bytes, gray, fields } => {
for (src_row, dst_row) in data.chunks_exact(row_pitch).zip(pixels.chunks_exact_mut(row_size)) {
// Stop at the output width, leaving any stored row padding unread.
for (src, dst) in src_row.chunks_exact(bytes).zip(dst_row.chunks_exact_mut(channels)) {
let pixel = src.iter().rev().fold(0u32, |v, &byte| v << 8 | u32::from(byte));
let channel = |i: usize| fields[i].map_or(0, |f| f.get(pixel));
let values = if gray { [channel(0), channel(3), 0, 0] } else { [channel(0), channel(1), channel(2), channel(3)] };
dst.copy_from_slice(&values[..channels]);
}
}
}
Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => {
let block_size = if matches!(encoding, Encoding::Bc1) { 8 } else { 16 };
let blocks_x = w.div_ceil(4);
for (i, block) in data.chunks_exact(block_size).enumerate() {
let texels = match encoding {
Encoding::Bc1 => bc1(block, true),
Encoding::Bc2 => with_alpha(bc1(block.get(8..).unwrap_or_default(), false), bc2_alpha(block)),
_ => with_alpha(bc1(block.get(8..).unwrap_or_default(), false), bc3_alpha(block)),
};
let (bx, by) = ((i % blocks_x) * 4, (i / blocks_x) * 4);
for (t, texel) in texels.iter().enumerate() {
let (x, y) = (bx + t % 4, by + t / 4);
if x < w && y < h {
let at = (y * w + x) * 4;
if let Some(dst) = pixels.get_mut(at..at + 4) {
dst.copy_from_slice(texel);
}
}
}
}
}
}
Image::from_u8(width, height, layout, pixels)
}
/// A BC1 colour block. BC2 and BC3 colour blocks always have four colours; only BC1's
/// `c0 <= c1` order selects three colours and transparent black.
fn bc1(block: &[u8], one_bit_alpha: bool) -> [[u8; 4]; 16] {
let byte = |i: usize| block.get(i).copied().unwrap_or(0);
let (c0, c1) = (u16::from_le_bytes([byte(0), byte(1)]), u16::from_le_bytes([byte(2), byte(3)]));
let palette = dds_bc::colour_palette(c0, c1, c0 > c1 || !one_bit_alpha);
let indices = u32::from_le_bytes([byte(4), byte(5), byte(6), byte(7)]);
std::array::from_fn(|t| palette[(indices >> (2 * t)) as usize & 3])
}
/// BC2: four explicit bits of alpha per texel.
fn bc2_alpha(block: &[u8]) -> [u8; 16] {
let bits = u64::from_le_bytes(std::array::from_fn(|i| block.get(i).copied().unwrap_or(0)));
std::array::from_fn(|t| ((bits >> (4 * t)) & 0xf) as u8 * 17)
}
/// BC3: two alpha endpoints and a 3-bit index per texel into eight values, or into six plus
/// 0 and 255 when `a0 <= a1`.
fn bc3_alpha(block: &[u8]) -> [u8; 16] {
let byte = |i: usize| block.get(i).copied().unwrap_or(0);
let palette = dds_bc::alpha_palette(byte(0), byte(1));
let bits = u64::from_le_bytes([byte(2), byte(3), byte(4), byte(5), byte(6), byte(7), 0, 0]);
std::array::from_fn(|t| palette[((bits >> (3 * t)) & 7) as usize])
}
fn with_alpha(mut texels: [[u8; 4]; 16], alpha: [u8; 16]) -> [[u8; 4]; 16] {
for (texel, a) in texels.iter_mut().zip(alpha) {
texel[3] = a;
}
texels
}
const DDSD_CAPS: u32 = 0x1;
const DDSD_HEIGHT: u32 = 0x2;
const DDSD_WIDTH: u32 = 0x4;
const DDSD_PITCH: u32 = 0x8;
const DDSD_PIXELFORMAT: u32 = 0x1000;
const DDSD_MIPMAPCOUNT: u32 = 0x2_0000;
const DDSD_LINEARSIZE: u32 = 0x8_0000;
const DDSCAPS_COMPLEX: u32 = 0x8;
const DDSCAPS_TEXTURE: u32 = 0x1000;
const DDSCAPS_MIPMAP: u32 = 0x40_0000;
fn encode_err(reason: impl std::fmt::Display) -> CodecError {
CodecError::encode(Format::Dds, reason)
}
/// How a written texture stores its pixels.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Store {
/// 8-bit luminance.
Luminance,
/// 8-bit luminance, then 8-bit alpha.
LuminanceAlpha,
/// Blue, green, red and an unused byte (written as 255, so a reader that takes it for
/// alpha still sees an opaque texture).
Bgrx,
Bgra,
Bc1,
Bc2,
Bc3,
}
impl Store {
/// The storage for an image of `layout`, as [`crate::fidelity::plan`] chose it.
fn new(compression: DdsCompression, layout: ChannelLayout) -> Result<Store, CodecError> {
Ok(match (compression, layout) {
(DdsCompression::None, ChannelLayout::Gray) => Store::Luminance,
(DdsCompression::None, ChannelLayout::GrayA) => Store::LuminanceAlpha,
(DdsCompression::None, ChannelLayout::Rgb) => Store::Bgrx,
(DdsCompression::None, ChannelLayout::Rgba) => Store::Bgra,
(DdsCompression::Bc1, ChannelLayout::Rgba) => Store::Bc1,
(DdsCompression::Bc2, ChannelLayout::Rgba) => Store::Bc2,
(DdsCompression::Bc3, ChannelLayout::Rgba) => Store::Bc3,
(_, layout) => return Err(encode_err(format!("{layout:?} pixels can't be written as `{}`", compression.name()))),
})
}
/// Bytes per pixel, or per 4×4 block for a block compression.
fn unit(self) -> usize {
match self {
Store::Luminance => 1,
Store::LuminanceAlpha => 2,
Store::Bgrx | Store::Bgra => 4,
Store::Bc1 => 8,
Store::Bc2 | Store::Bc3 => 16,
}
}
fn is_block(self) -> bool {
matches!(self, Store::Bc1 | Store::Bc2 | Store::Bc3)
}
/// Bytes of one row of pixels (or of 4×4 blocks) of a level `width` wide.
fn row_bytes(self, width: usize) -> Option<usize> {
if self.is_block() { width.div_ceil(4).checked_mul(self.unit()) } else { width.checked_mul(self.unit()) }
}
fn level_bytes(self, width: usize, height: usize) -> Option<usize> {
let rows = if self.is_block() { height.div_ceil(4) } else { height };
self.row_bytes(width)?.checked_mul(rows)
}
/// DDS_PIXELFORMAT: flags, FourCC, bits per pixel and the red, green, blue and alpha masks.
fn pixel_format(self) -> (u32, [u8; 4], u32, [u32; 4]) {
match self {
Store::Luminance => (DDPF_LUMINANCE, [0; 4], 8, [0xff, 0, 0, 0]),
Store::LuminanceAlpha => (DDPF_LUMINANCE | DDPF_ALPHAPIXELS, [0; 4], 16, [0xff, 0, 0, 0xff00]),
Store::Bgrx => (DDPF_RGB, [0; 4], 32, [0xff_0000, 0xff00, 0xff, 0]),
Store::Bgra => (DDPF_RGB | DDPF_ALPHAPIXELS, [0; 4], 32, [0xff_0000, 0xff00, 0xff, 0xff00_0000]),
Store::Bc1 => (DDPF_FOURCC, *b"DXT1", 0, [0; 4]),
Store::Bc2 => (DDPF_FOURCC, *b"DXT3", 0, [0; 4]),
Store::Bc3 => (DDPF_FOURCC, *b"DXT5", 0, [0; 4]),
}
}
}
/// Levels in a full mip chain: the larger side halves down to 1.
fn mip_levels(width: u32, height: u32) -> u32 {
32 - width.max(height).max(1).leading_zeros()
}
fn half((w, h): (usize, usize)) -> (usize, usize) {
((w / 2).max(1), (h / 2).max(1))
}
pub(crate) fn encode(image: &Image, plan: Plan, opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
let img = image.converted(plan.layout, SampleType::U8);
let pixels = img.as_u8().ok_or_else(|| encode_err("the image was not converted to 8 bits per channel"))?;
let store = Store::new(opts.dds.compression, img.layout())?;
let (width, height) = img.dimensions();
let levels = if opts.dds.mipmaps { mip_levels(width, height) } else { 1 };
let top = (width as usize, height as usize);
// Every level's size, checked before anything is allocated.
let mut sizes = Vec::new();
let mut size = top;
for _ in 0..levels {
sizes.push(store.level_bytes(size.0, size.1).ok_or_else(|| encode_err("texture size overflow"))?);
size = half(size);
}
let total = sizes.iter().try_fold(HEADER, |total, &s| total.checked_add(s)).ok_or_else(|| encode_err("texture size overflow"))?;
let header = header(width, height, store, levels, sizes.first().copied().unwrap_or(0))?;
let mut out = crate::options::alloc_zeroed::<u8>(total)?;
out.get_mut(..HEADER).ok_or_else(|| encode_err("output buffer too small"))?.copy_from_slice(&header);
let channels = img.layout().channels();
let mut level = Cow::Borrowed(pixels);
let (mut size, mut at) = (top, HEADER);
for (i, &bytes) in sizes.iter().enumerate() {
let dst = out.get_mut(at..at + bytes).ok_or_else(|| encode_err("output buffer too small"))?;
write_level(store, &level, size, dst)?;
at += bytes;
if i + 1 < sizes.len() {
let next = half(size);
level = Cow::Owned(downsample(&level, size, next, channels)?);
size = next;
}
}
Ok(out)
}
/// The `DDS ` signature and DDS_HEADER. `top` is the top level's size in bytes.
fn header(width: u32, height: u32, store: Store, levels: u32, top: usize) -> Result<[u8; HEADER], CodecError> {
let mut b = [0u8; HEADER];
let mut put = |at: usize, v: u32| {
if let Some(d) = b.get_mut(at..at + 4) {
d.copy_from_slice(&v.to_le_bytes());
}
};
// Uncompressed: the pitch of one row. Compressed: the whole top level (its "linear size").
let (size_flag, pitch_or_size) = if store.is_block() { (DDSD_LINEARSIZE, Some(top)) } else { (DDSD_PITCH, store.row_bytes(width as usize)) };
let pitch_or_size =
pitch_or_size.and_then(|v| u32::try_from(v).ok()).ok_or_else(|| encode_err(format!("a {width}x{height} level is too large for a DDS header")))?;
let mipmapped = levels > 1;
put(4, 124);
put(8, DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | size_flag | if mipmapped { DDSD_MIPMAPCOUNT } else { 0 });
put(12, height);
put(16, width);
put(20, pitch_or_size);
put(28, levels);
let (flags, fourcc, bits, masks) = store.pixel_format();
put(76, 32);
put(80, flags);
put(84, u32::from_le_bytes(fourcc));
put(88, bits);
for (i, mask) in masks.into_iter().enumerate() {
put(92 + 4 * i, mask);
}
put(108, DDSCAPS_TEXTURE | if mipmapped { DDSCAPS_COMPLEX | DDSCAPS_MIPMAP } else { 0 });
b[..4].copy_from_slice(b"DDS ");
Ok(b)
}
/// Runs `f` on each `len`-byte row of `out` with its index, in parallel on native targets.
fn rows(out: &mut [u8], len: usize, f: impl Fn(usize, &mut [u8]) + Send + Sync) {
#[cfg(not(target_arch = "wasm32"))]
{
use rayon::prelude::*;
out.par_chunks_mut(len.max(1)).enumerate().for_each(|(i, row)| f(i, row));
}
#[cfg(target_arch = "wasm32")]
for (i, row) in out.chunks_mut(len.max(1)).enumerate() {
f(i, row);
}
}
/// Writes one level of `width` × `height` pixels (in the image's layout) into `dst`.
fn write_level(store: Store, pixels: &[u8], (width, height): (usize, usize), dst: &mut [u8]) -> Result<(), CodecError> {
let row_bytes = store.row_bytes(width).ok_or_else(|| encode_err("texture size overflow"))?;
match store {
Store::Luminance | Store::LuminanceAlpha => {
// The image's gray (and alpha) samples, in the same order.
let src = pixels.get(..dst.len()).ok_or_else(|| encode_err("the level is smaller than its size"))?;
dst.copy_from_slice(src);
}
Store::Bgrx | Store::Bgra => {
let channels = if store == Store::Bgra { 4 } else { 3 };
rows(dst, row_bytes, |y, row| {
let src = pixels.get(y * width * channels..).unwrap_or_default();
for (d, s) in row.as_chunks_mut::<4>().0.iter_mut().zip(src.chunks_exact(channels)) {
let byte = |i: usize| s.get(i).copied().unwrap_or(255);
*d = [byte(2), byte(1), byte(0), byte(3)];
}
});
}
Store::Bc1 | Store::Bc2 | Store::Bc3 => {
let (texels, _) = pixels.as_chunks::<4>();
rows(dst, row_bytes, |by, row| {
for (bx, d) in row.chunks_exact_mut(store.unit()).enumerate() {
let (block, inside) = gather(texels, (width, height), bx, by);
let encoded: &[u8] = match store {
Store::Bc1 => &dds_bc::encode_bc1(&block, inside),
Store::Bc2 => &dds_bc::encode_bc2(&block, inside),
_ => &dds_bc::encode_bc3(&block, inside),
};
for (d, s) in d.iter_mut().zip(encoded) {
*d = *s;
}
}
});
}
}
Ok(())
}
/// Block (`bx`, `by`)'s texels, and which of them lie inside the image.
fn gather(texels: &[[u8; 4]], (width, height): (usize, usize), bx: usize, by: usize) -> (Block, Mask) {
let mut block = [[0u8; 4]; 16];
let mut inside: Mask = 0;
for (t, texel) in block.iter_mut().enumerate() {
let (x, y) = (bx * 4 + t % 4, by * 4 + t / 4);
if x < width
&& y < height
&& let Some(p) = texels.get(y * width + x)
{
*texel = *p;
inside |= 1 << t;
}
}
(block, inside)
}
/// The next mip level: each pixel the average of the 2×2 pixels it covers, or of 2×3, 3×2 or
/// 3×3 along the last column and row of an odd-sized level, so no pixel is dropped.
fn downsample(src: &[u8], (w, h): (usize, usize), (nw, nh): (usize, usize), channels: usize) -> Result<Vec<u8>, CodecError> {
let len = nw.checked_mul(nh).and_then(|n| n.checked_mul(channels)).ok_or_else(|| encode_err("mip level size overflow"))?;
let mut dst = crate::options::alloc_zeroed::<u8>(len)?;
let span = |i: usize, n: usize, size: usize| 2 * i..=if i + 1 == n { size.saturating_sub(1) } else { 2 * i + 1 };
rows(&mut dst, nw * channels, |y, row| {
let ys = span(y, nh, h);
for (x, px) in row.chunks_exact_mut(channels.max(1)).enumerate() {
let xs = span(x, nw, w);
for (c, v) in px.iter_mut().enumerate() {
let (mut sum, mut n) = (0u32, 0u32);
for sy in ys.clone() {
for sx in xs.clone() {
if let Some(&s) = src.get((sy * w + sx) * channels + c) {
sum += u32::from(s);
n += 1;
}
}
}
*v = ((sum + n / 2) / n.max(1)) as u8;
}
}
});
Ok(dst)
}
-368
View File
@@ -1,368 +0,0 @@
//! BC1, BC2 and BC3 (DXT1, DXT3, DXT5) blocks, after Microsoft's public block compression
//! documentation ("Block Compression (Direct3D 10)" and "Texture Block Compression in Direct3D
//! 11"): the palettes the DDS reader decodes with, and the block encoder the DDS writer uses.
//!
//! The encoder fits a block's colours with two RGB565 endpoints along their principal axis,
//! refines them by least squares, and scores every candidate against the exact palette the
//! reader rebuilds, so the indices it picks are the best ones for what a decoder shows.
/// The 16 texels of a 4×4 block, row by row, as RGBA.
pub(crate) type Block = [[u8; 4]; 16];
/// Bit `t` set for each texel `t` of a block that counts: edge blocks of a texture whose size is
/// not a multiple of four have texels outside the image.
pub(crate) type Mask = u16;
pub(crate) fn rgb565(c: u16) -> [u8; 3] {
let scale = |v: u16, max: u16| ((u32::from(v) * 255 + u32::from(max) / 2) / u32::from(max)) as u8;
[scale(c >> 11, 31), scale((c >> 5) & 63, 63), scale(c & 31, 31)]
}
/// The colours a colour block's indices select. Four-colour mode (BC1 with `c0 > c1`, and every
/// BC2 and BC3 block) adds two colours at thirds; three-colour mode adds the midpoint and
/// transparent black.
pub(crate) fn colour_palette(c0: u16, c1: u16, four: bool) -> [[u8; 4]; 4] {
let (a, b) = (rgb565(c0), rgb565(c1));
let mix = |wa: u32, wb: u32| {
let d = wa + wb;
let at = |i: usize| ((wa * u32::from(a[i]) + wb * u32::from(b[i]) + d / 2) / d) as u8;
[at(0), at(1), at(2), 255]
};
if four {
[[a[0], a[1], a[2], 255], [b[0], b[1], b[2], 255], mix(2, 1), mix(1, 2)]
} else {
[[a[0], a[1], a[2], 255], [b[0], b[1], b[2], 255], mix(1, 1), [0; 4]]
}
}
/// A BC3 alpha block's eight values: six interpolated at sevenths when `a0 > a1`, else four
/// interpolated at fifths followed by 0 and 255.
pub(crate) fn alpha_palette(a0: u8, a1: u8) -> [u8; 8] {
let (a0, a1) = (u32::from(a0), u32::from(a1));
let mut palette = [a0, a1, 0, 0, 0, 0, 0, 255];
if a0 > a1 {
for (k, v) in palette.iter_mut().enumerate().skip(2) {
let k = k as u32;
*v = ((8 - k) * a0 + (k - 1) * a1 + 3) / 7;
}
} else {
for (k, v) in palette.iter_mut().enumerate().take(6).skip(2) {
let k = k as u32;
*v = ((6 - k) * a0 + (k - 1) * a1 + 2) / 5;
}
}
palette.map(|v| v as u8)
}
/// A BC1 block. Texels under half opacity become BC1's transparent black, which needs the
/// three-colour mode; an opaque block takes whichever mode fits its colours better.
pub(crate) fn encode_bc1(block: &Block, inside: Mask) -> [u8; 8] {
let transparent = select(block, inside, |t| t[3] < 128);
colour_block(block, inside & !transparent, transparent, true)
}
/// A BC2 block: four bits of alpha per texel, then a four-colour block.
pub(crate) fn encode_bc2(block: &Block, inside: Mask) -> [u8; 16] {
let bits = block.iter().enumerate().fold(0u64, |bits, (t, texel)| bits | ((u64::from(texel[3]) * 15 + 127) / 255) << (4 * t));
join(bits.to_le_bytes(), colour_block(block, inside, 0, false))
}
/// A BC3 block: an interpolated alpha block, then a four-colour block.
pub(crate) fn encode_bc3(block: &Block, inside: Mask) -> [u8; 16] {
join(alpha_block(block, inside), colour_block(block, inside, 0, false))
}
fn join(a: [u8; 8], b: [u8; 8]) -> [u8; 16] {
std::array::from_fn(|i| if i < 8 { a.get(i) } else { b.get(i - 8) }.copied().unwrap_or(0))
}
/// The texels of `inside` for which `f` holds.
fn select(block: &Block, inside: Mask, f: impl Fn(&[u8; 4]) -> bool) -> Mask {
block.iter().enumerate().filter(|(t, texel)| inside >> t & 1 != 0 && f(texel)).fold(0, |m, (t, _)| m | 1 << t)
}
fn rgb_of(texel: &[u8; 4]) -> [f32; 3] {
[f32::from(texel[0]), f32::from(texel[1]), f32::from(texel[2])]
}
/// RGB565 of a colour, rounding each channel to the nearest level.
fn to565(c: [f32; 3]) -> u16 {
let q = |v: f32, max: f32| (v.clamp(0.0, 255.0) * max / 255.0).round() as u16;
q(c[0], 31.0) << 11 | q(c[1], 63.0) << 5 | q(c[2], 31.0)
}
fn distance(a: &[u8; 4], b: &[u8; 4]) -> u32 {
(0..3).map(|i| u32::from(a[i].abs_diff(b[i])).pow(2)).sum()
}
/// A colour block (endpoints, then 2-bit indices) for the `opaque` texels' colours, with the
/// `transparent` ones on BC1's transparent index. `bc1` allows the three-colour mode; BC2 and
/// BC3 always decode four colours.
fn colour_block(block: &Block, opaque: Mask, transparent: Mask, bc1: bool) -> [u8; 8] {
let colours: Vec<[f32; 3]> = block.iter().enumerate().filter(|(t, _)| opaque >> t & 1 != 0).map(|(_, texel)| rgb_of(texel)).collect();
if colours.is_empty() {
// Nothing visible: every texel on the transparent index (or index 0 of black).
let indices = if transparent != 0 { u32::MAX } else { 0 };
return pack(0, 0, indices);
}
let (lo, hi) = principal_endpoints(&colours);
let mut candidates = vec![(to565(lo), to565(hi))];
let modes: &[bool] = if transparent != 0 {
&[false]
} else if bc1 {
&[true, false]
} else {
&[true]
};
let mut best: Option<(u32, [u8; 8])> = None;
// Each refinement starts from the best block so far: its indices give the least-squares
// endpoints, which become one more candidate.
for round in 0..3 {
for &(e0, e1) in &candidates {
for &four in modes {
let (bytes, error) = fit(block, opaque, transparent, e0, e1, four);
if best.is_none_or(|(e, _)| error < e) {
best = Some((error, bytes));
}
}
}
let Some((error, bytes)) = best else { break };
if error == 0 || round == 2 {
break;
}
candidates = least_squares(block, opaque, &bytes).into_iter().collect();
if candidates.is_empty() {
break;
}
}
best.map_or([0; 8], |(_, bytes)| bytes)
}
/// Orders endpoints `e0`, `e1` for the mode (`c0 > c1` for four colours, `c0 <= c1` for three)
/// and picks each texel's nearest palette colour. Returns the block and its squared error.
fn fit(block: &Block, opaque: Mask, transparent: Mask, e0: u16, e1: u16, four: bool) -> ([u8; 8], u32) {
let (c0, c1) = if four == (e0 >= e1) { (e0, e1) } else { (e1, e0) };
// Equal endpoints can't be in four-colour order; the palette is then one colour either way.
let palette = colour_palette(c0, c1, four && c0 > c1);
let choices = if four && c0 > c1 { 4 } else { 3 };
let mut indices = 0u32;
let mut error = 0u32;
for (t, texel) in block.iter().enumerate() {
let index = if transparent >> t & 1 != 0 {
3
} else if opaque >> t & 1 != 0 {
let (i, d) = palette.iter().take(choices).enumerate().map(|(i, p)| (i, distance(texel, p))).min_by_key(|&(_, d)| d).unwrap_or((0, 0));
error += d;
i as u32
} else {
0
};
indices |= index << (2 * t);
}
(pack(c0, c1, indices), error)
}
fn pack(c0: u16, c1: u16, indices: u32) -> [u8; 8] {
let (c0, c1, i) = (c0.to_le_bytes(), c1.to_le_bytes(), indices.to_le_bytes());
[c0[0], c0[1], c1[0], c1[1], i[0], i[1], i[2], i[3]]
}
/// The ends of the colours' spread along their principal axis (the covariance's dominant
/// eigenvector, by power iteration), clamped to the RGB cube.
fn principal_endpoints(colours: &[[f32; 3]]) -> ([f32; 3], [f32; 3]) {
let n = colours.len().max(1) as f32;
let mean = [0, 1, 2].map(|i| colours.iter().map(|c| c[i]).sum::<f32>() / n);
let mut cov = [[0f32; 3]; 3];
for c in colours {
let d = [c[0] - mean[0], c[1] - mean[1], c[2] - mean[2]];
for (i, row) in cov.iter_mut().enumerate() {
for (j, v) in row.iter_mut().enumerate() {
*v += d[i] * d[j];
}
}
}
let times = |a: [f32; 3]| [0, 1, 2].map(|i| cov[i][0] * a[0] + cov[i][1] * a[1] + cov[i][2] * a[2]);
let iterate = |start: usize| {
let mut axis = [0f32; 3];
axis[start] = 1.0;
for _ in 0..8 {
let next = times(axis);
let len = next.iter().map(|v| v * v).sum::<f32>().sqrt();
if !len.is_normal() {
break;
}
axis = next.map(|v| v / len);
}
axis
};
// From each channel's axis, keeping the one the colours spread along most: a channel that
// varies on its own is already an eigenvector, so a single start there would never leave it
// even when the others vary more together.
let spread = |a: [f32; 3]| times(a).iter().zip(a).map(|(x, y)| x * y).sum::<f32>();
let axis = [0, 1, 2].map(iterate).into_iter().max_by(|&a, &b| spread(a).total_cmp(&spread(b))).unwrap_or([1.0, 0.0, 0.0]);
let along = |c: &[f32; 3]| (c[0] - mean[0]) * axis[0] + (c[1] - mean[1]) * axis[1] + (c[2] - mean[2]) * axis[2];
let (lo, hi) = colours.iter().map(along).fold((f32::MAX, f32::MIN), |(lo, hi), t| (lo.min(t), hi.max(t)));
let at = |t: f32| [0, 1, 2].map(|i| (mean[i] + t * axis[i]).clamp(0.0, 255.0));
if lo > hi { (mean, mean) } else { (at(lo), at(hi)) }
}
/// The endpoints that minimise the squared error for the indices `bytes` already chose: each
/// texel's index fixes its weights between the endpoints, giving 2×2 normal equations per
/// channel. `None` when the indices don't constrain both endpoints.
fn least_squares(block: &Block, opaque: Mask, bytes: &[u8; 8]) -> Option<(u16, u16)> {
let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]]));
let indices = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]);
let four = c0 > c1;
// How much of c0 each index holds.
let weight = |i: u32| match (four, i) {
(_, 0) => Some(1.0f32),
(_, 1) => Some(0.0),
(true, 2) => Some(2.0 / 3.0),
(true, _) => Some(1.0 / 3.0),
(false, 2) => Some(0.5),
(false, _) => None,
};
let (mut aa, mut ab, mut bb) = (0f32, 0f32, 0f32);
let (mut ax, mut bx) = ([0f32; 3], [0f32; 3]);
for (t, texel) in block.iter().enumerate() {
if opaque >> t & 1 == 0 {
continue;
}
let Some(a) = weight(indices >> (2 * t) & 3) else { continue };
let b = 1.0 - a;
let c = rgb_of(texel);
aa += a * a;
ab += a * b;
bb += b * b;
for ((x0, x1), v) in ax.iter_mut().zip(bx.iter_mut()).zip(c) {
*x0 += a * v;
*x1 += b * v;
}
}
let det = aa * bb - ab * ab;
if det.abs() < 1e-3 {
return None;
}
let e0 = [0, 1, 2].map(|i| (ax[i] * bb - bx[i] * ab) / det);
let e1 = [0, 1, 2].map(|i| (bx[i] * aa - ax[i] * ab) / det);
Some((to565(e0), to565(e1)))
}
/// A BC3 alpha block: whichever of the two modes (eight values between the extremes, or six
/// between the extremes other than 0 and 255, plus exact 0 and 255) fits better.
fn alpha_block(block: &Block, inside: Mask) -> [u8; 8] {
let alphas = || block.iter().enumerate().filter(move |(t, _)| inside >> t & 1 != 0).map(|(_, texel)| texel[3]);
let range = |values: &mut dyn Iterator<Item = u8>| values.fold(None, |r: Option<(u8, u8)>, a| Some(r.map_or((a, a), |(lo, hi)| (lo.min(a), hi.max(a)))));
let (lo, hi) = range(&mut alphas()).unwrap_or((0, 0));
// Eight values need a0 > a1; equal ends fall to the six-value mode, which starts with a0 too.
let eight = alpha_fit(block, inside, hi, lo);
let (mid_lo, mid_hi) = range(&mut alphas().filter(|a| !matches!(a, 0 | 255))).unwrap_or((0, 0));
let six = alpha_fit(block, inside, mid_lo, mid_hi);
if six.1 < eight.1 { six.0 } else { eight.0 }
}
fn alpha_fit(block: &Block, inside: Mask, a0: u8, a1: u8) -> ([u8; 8], u32) {
let palette = alpha_palette(a0, a1);
let mut bits = 0u64;
let mut error = 0u32;
for (t, texel) in block.iter().enumerate() {
if inside >> t & 1 == 0 {
continue;
}
let (i, d) = palette.iter().enumerate().map(|(i, &p)| (i, u32::from(p.abs_diff(texel[3])).pow(2))).min_by_key(|&(_, d)| d).unwrap_or((0, 0));
error += d;
bits |= (i as u64) << (3 * t);
}
let b = bits.to_le_bytes();
([a0, a1, b[0], b[1], b[2], b[3], b[4], b[5]], error)
}
#[cfg(test)]
mod tests {
use super::*;
fn decode_colour(bytes: &[u8; 8], bc1: bool) -> Block {
let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]]));
let palette = colour_palette(c0, c1, c0 > c1 || !bc1);
let indices = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]);
std::array::from_fn(|t| palette[(indices >> (2 * t)) as usize & 3])
}
#[test]
fn solid_blocks_land_on_the_nearest_565_colour() {
for colour in [[0, 0, 0, 255], [255, 255, 255, 255], [12, 200, 77, 255]] {
let block = [colour; 16];
let out = decode_colour(&encode_bc1(&block, u16::MAX), true);
for texel in out {
assert!(texel.iter().zip(colour).all(|(a, b)| a.abs_diff(b) <= 4), "{texel:?} for {colour:?}");
}
}
}
#[test]
fn two_colour_blocks_are_exact_when_the_colours_are_565() {
let (a, b) = ([255, 0, 0, 255], [0, 0, 255, 255]);
let block: Block = std::array::from_fn(|t| if t % 3 == 0 { a } else { b });
for bc1 in [true, false] {
let bytes = if bc1 { encode_bc1(&block, u16::MAX) } else { colour_block(&block, u16::MAX, 0, false) };
assert_eq!(decode_colour(&bytes, bc1), block);
}
}
#[test]
fn bc1_punches_through_texels_under_half_alpha() {
let block: Block = std::array::from_fn(|t| if t < 8 { [200, 100, 50, 255] } else { [9, 9, 9, 127] });
let bytes = encode_bc1(&block, u16::MAX);
let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]]));
assert!(c0 <= c1, "three-colour mode");
let out = decode_colour(&bytes, true);
assert!(out[..8].iter().all(|t| t[3] == 255) && out[8..].iter().all(|t| *t == [0; 4]), "{out:?}");
}
#[test]
fn four_colour_blocks_keep_c0_above_c1() {
// A ramp needs all four colours; BC2 and BC3 blocks must not rely on the order, but
// older decoders read c0 <= c1 as three colours, so the encoder never writes that.
let block: Block = std::array::from_fn(|t| [(t * 16) as u8, 0, 0, 255]);
let bytes = colour_block(&block, u16::MAX, 0, false);
assert!(u16::from_le_bytes([bytes[0], bytes[1]]) > u16::from_le_bytes([bytes[2], bytes[3]]));
}
#[test]
fn alpha_blocks_pick_the_mode_that_keeps_0_and_255() {
// 0, 255 and a cluster in between: the six-value mode stores 0 and 255 exactly.
let block: Block = std::array::from_fn(|t| [0, 0, 0, [0, 255, 100, 110][t % 4]]);
let bytes = alpha_block(&block, u16::MAX);
assert!(bytes[0] <= bytes[1], "six-value mode");
let palette = alpha_palette(bytes[0], bytes[1]);
let bits = u64::from_le_bytes([bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], 0, 0]);
for (t, texel) in block.iter().enumerate() {
let got = palette[(bits >> (3 * t)) as usize & 7];
assert!(got.abs_diff(texel[3]) <= 2, "texel {t}: {got} for {}", texel[3]);
}
}
#[test]
fn the_principal_axis_is_found_when_one_channel_varies_on_its_own() {
// Red alternates on its own (variance 64); green and blue alternate together (variance 49
// each, 98 along their shared axis), uncorrelated with red. Starting on red alone would
// keep red's axis and flatten green and blue; the block must do better than its bounding box.
let block: Block = std::array::from_fn(|t| {
let gb = if (t / 2) % 2 == 0 { 121 } else { 135 };
[if t % 2 == 0 { 120 } else { 136 }, gb, gb, 255]
});
let error = |bytes: &[u8; 8]| decode_colour(bytes, false).iter().zip(&block).map(|(a, b)| distance(a, b)).sum::<u32>();
let (_, bounding_box) = fit(&block, u16::MAX, 0, to565([120.0, 121.0, 121.0]), to565([136.0, 135.0, 135.0]), true);
let ours = error(&colour_block(&block, u16::MAX, 0, false));
assert!(ours < bounding_box, "{ours} vs the bounding box's {bounding_box}");
}
#[test]
fn texels_outside_the_image_are_ignored() {
// Only the top-left texel is inside: its colour alone decides the endpoints.
let mut block: Block = [[0, 255, 0, 255]; 16];
block[0] = [255, 0, 255, 255];
let out = decode_colour(&encode_bc1(&block, 1), true);
assert_eq!(out[0], [255, 0, 255, 255]);
}
}
-517
View File
@@ -1,517 +0,0 @@
//! DICOM (medical imaging) reader, uncompressed transfer syntaxes only.
//!
//! Supports what a viewer needs from a single-frame image: MONOCHROME1/2 (gray) and RGB or
//! YBR_FULL, 8- and 16-bit samples, interleaved or planar, both byte orders and explicit or
//! implicit VR. Undefined-length sequences outside the pixel data are skipped (clinical files
//! contain them); compressed transfer syntaxes, encapsulated pixel data and other photometric
//! interpretations are refused with a clear error. Read-only in this crate.
use std::collections::HashMap;
use crate::error::CodecError;
use crate::format::Format;
use crate::image::{ChannelLayout, Image, SampleType};
use crate::options::Limits;
const F: Format = Format::Dicom;
const PREAMBLE: usize = 132; // 128 zero bytes + "DICM"
const UNDEFINED: u32 = 0xFFFF_FFFF;
fn bad(msg: impl std::fmt::Display) -> CodecError {
CodecError::malformed(F, msg)
}
/// A DICOM file begins with a 128-byte preamble and the `DICM` magic.
pub(crate) fn is_dicom(b: &[u8]) -> bool {
b.get(128..132) == Some(b"DICM")
}
#[derive(Clone, Copy)]
struct Endian {
big: bool,
}
/// The transfer syntax: byte order and whether value representations are explicit.
#[derive(Clone, Copy)]
struct Syntax {
endian: Endian,
explicit: bool,
}
/// The value representations whose length field is 2 reserved bytes + `u32` in explicit VR.
fn long_vr(vr: &[u8]) -> bool {
matches!(vr, b"OB" | b"OW" | b"OF" | b"SQ" | b"UT" | b"UN" | b"OD" | b"OL" | b"OV" | b"UC" | b"UR")
}
/// A cursor over the byte stream.
struct Cur<'a> {
b: &'a [u8],
pos: usize,
big: bool,
}
impl<'a> Cur<'a> {
fn take(&mut self, n: usize) -> Option<&'a [u8]> {
let s = self.b.get(self.pos..self.pos.checked_add(n)?)?;
self.pos += n;
Some(s)
}
fn u16(&mut self) -> Option<u16> {
let c = self.take(2)?;
Some(if self.big { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) })
}
fn u32(&mut self) -> Option<u32> {
let c = self.take(4)?;
Some(if self.big { u32::from_be_bytes([c[0], c[1], c[2], c[3]]) } else { u32::from_le_bytes([c[0], c[1], c[2], c[3]]) })
}
/// Skip `len` bytes, refusing to run past the buffer.
fn skip(&mut self, len: usize) -> Result<(), CodecError> {
self.pos = self.pos.checked_add(len).filter(|p| *p <= self.b.len()).ok_or_else(|| bad("a value runs past the end of the file"))?;
Ok(())
}
}
/// Read one element's tag and value length, leaving the cursor at the value. Item and delimiter
/// tags (group `FFFE`) carry a `u32` length with no VR even under explicit VR.
fn read_header(c: &mut Cur, syn: &Syntax) -> Result<(u16, u16, [u8; 2], u32), CodecError> {
let group = c.u16().ok_or_else(|| bad("truncated element tag"))?;
let elem = c.u16().ok_or_else(|| bad("truncated element tag"))?;
let (vr, len) = if group == 0xFFFE {
([0, 0], c.u32().ok_or_else(|| bad("truncated item length"))?)
} else if syn.explicit {
let vr = c.take(2).ok_or_else(|| bad("truncated VR"))?;
let v = [vr[0], vr[1]];
let len = if long_vr(&v) {
c.take(2).ok_or_else(|| bad("truncated VR"))?; // reserved
c.u32().ok_or_else(|| bad("truncated length"))?
} else {
u32::from(c.u16().ok_or_else(|| bad("truncated length"))?)
};
(v, len)
} else {
([0, 0], c.u32().ok_or_else(|| bad("truncated length"))?)
};
Ok((group, elem, vr, len))
}
/// Skip an undefined-length sequence, ending just after its `FFFE,E0DD` delimiter. Nesting is
/// tracked through undefined-length items and nested sequences; everything else is skipped by
/// its length. Returns the position after the delimiter.
fn skip_undefined(c: &mut Cur, syn: &Syntax) -> Result<(), CodecError> {
let mut depth = 1u32;
while depth > 0 {
let (group, elem, _, len) = read_header(c, syn)?;
// Item (E00D) and sequence (E0DD) delimiters close one undefined length; they carry no
// content of their own.
if group == 0xFFFE && matches!(elem, 0xE00D | 0xE0DD) {
depth -= 1;
continue;
}
if len == UNDEFINED {
depth += 1;
} else {
c.skip(len as usize)?;
}
}
Ok(())
}
/// Walk the elements from `start`, calling `f` with each `(group, element, value)`. An
/// undefined-length sequence outside the pixel data is skipped; encapsulated (undefined-length)
/// pixel data is refused. Returns the number of bytes walked.
fn elements(b: &[u8], start: usize, syn: &Syntax, mut f: impl FnMut(u16, u16, &Elem) -> Result<(), CodecError>) -> Result<usize, CodecError> {
let mut c = Cur { b, pos: start, big: syn.endian.big };
loop {
if c.pos >= b.len() {
return Ok(c.pos - start);
}
let (group, elem, vr, len) = read_header(&mut c, syn)?;
if len == UNDEFINED {
if (group, elem) == (0x7FE0, 0x0010) {
return Err(CodecError::unsupported(F, "encapsulated (compressed) pixel data"));
}
skip_undefined(&mut c, syn)?;
continue;
}
let len = len as usize;
let start = c.pos;
if b.len() < start.saturating_add(len) {
return Err(bad("an element value runs past the end of the file"));
}
f(group, elem, &Elem { vr, start, len })?;
c.pos = start + len;
}
}
/// A parsed element: its VR and the byte range of its value.
struct Elem {
vr: [u8; 2],
start: usize,
len: usize,
}
fn u16_val(b: &[u8], e: &Elem, big: bool) -> Option<u16> {
let c = b.get(e.start..e.start.checked_add(2)?)?;
Some(if big { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) })
}
/// The transfer syntax (byte order and explicit/implicit VR) and the data-set start. The file
/// meta group is always explicit VR little-endian; the data set follows its `(0002,0010)`
/// TransferSyntaxUID.
fn transfer_syntax(b: &[u8]) -> Result<(Syntax, usize), CodecError> {
let meta = Syntax { endian: Endian { big: false }, explicit: true };
let mut ts: Option<String> = None;
let mut data_start = b.len();
let mut c = Cur { b, pos: PREAMBLE, big: false };
while c.pos + 8 <= b.len() {
let save = c.pos;
let (group, elem, _, len) = read_header(&mut c, &meta)?;
if group != 0x0002 {
// A non-meta group begins the data set.
data_start = save;
break;
}
if len == UNDEFINED {
return Err(CodecError::unsupported(F, "undefined-length meta element"));
}
let start = c.pos;
if b.len() < start.saturating_add(len as usize) {
return Err(bad("a meta element runs past the end of the file"));
}
if elem == 0x0010 {
ts = Some(String::from_utf8_lossy(&b[start..start + len as usize]).trim_end_matches(['\0', ' ']).to_string());
}
c.pos = start + len as usize;
}
let syn = match ts.as_deref() {
Some("1.2.840.10008.1.2") => Syntax { endian: Endian { big: false }, explicit: false },
Some("1.2.840.10008.1.2.1") => Syntax { endian: Endian { big: false }, explicit: true },
Some("1.2.840.10008.1.2.2") => Syntax { endian: Endian { big: true }, explicit: true },
Some(other) => return Err(CodecError::unsupported(F, format!("transfer syntax {other} (only uncompressed is read)"))),
// No meta: the historical default is implicit VR little-endian.
None => Syntax { endian: Endian { big: false }, explicit: false },
};
Ok((syn, data_start))
}
pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
if !is_dicom(b) {
return Err(bad("not a DICOM file (no DICM magic)"));
}
let (syn, start) = transfer_syntax(b)?;
let mut tags: HashMap<(u16, u16), Elem> = HashMap::new();
elements(b, start, &syn, |g, e, el| {
// Later duplicates must not silently override the first (DICOM forbids duplicates).
tags.entry((g, e)).or_insert_with(|| Elem { vr: el.vr, start: el.start, len: el.len });
Ok(())
})?;
let big = syn.endian.big;
let get_u16 = |tag: (u16, u16)| tags.get(&tag).and_then(|e| u16_val(b, e, big));
let rows = u32::from(get_u16((0x0028, 0x0010)).ok_or_else(|| bad("missing Rows (0028,0010)"))?);
let cols = u32::from(get_u16((0x0028, 0x0011)).ok_or_else(|| bad("missing Columns (0028,0011)"))?);
let samples = u32::from(get_u16((0x0028, 0x0002)).unwrap_or(1));
let bits = get_u16((0x0028, 0x0100)).ok_or_else(|| bad("missing BitsAllocated (0028,0100)"))?;
let bits_stored = get_u16((0x0028, 0x0101)).unwrap_or(bits);
let high_bit = get_u16((0x0028, 0x0102)).unwrap_or(bits_stored.saturating_sub(1));
if u32::from(high_bit) >= u32::from(bits) {
return Err(bad("HighBit is outside the allocated bits"));
}
let signed = get_u16((0x0028, 0x0103)).unwrap_or(0) == 1;
let planar = get_u16((0x0028, 0x0006)).unwrap_or(0);
let photometric = tags
.get(&(0x0028, 0x0004))
.and_then(|e| b.get(e.start..e.start.checked_add(e.len)?))
.map(|v| String::from_utf8_lossy(v).trim_end_matches(['\0', ' ']).to_string())
.ok_or_else(|| bad("missing PhotometricInterpretation (0028,0004)"))?;
let pixel = tags.get(&(0x7FE0, 0x0010)).ok_or_else(|| CodecError::unsupported(F, "no pixel data (7FE0,0010)"))?;
if !matches!(bits, 8 | 16) || !matches!(bits_stored, 1..=16) {
return Err(CodecError::unsupported(F, format!("{bits}-bit allocated / {bits_stored}-bit stored samples")));
}
let mono = matches!(photometric.as_str(), "MONOCHROME1" | "MONOCHROME2");
let rgb = photometric == "RGB";
let ybr = photometric == "YBR_FULL";
if !(mono || rgb || ybr) {
return Err(CodecError::unsupported(F, format!("photometric interpretation {photometric}")));
}
// Mono stores one sample; RGB and YBR_FULL store three. Any other count would index past the
// pixel data, so it is refused rather than misread.
let want_samples = if mono { 1 } else { 3 };
if samples != want_samples {
return Err(CodecError::unsupported(F, format!("{samples} samples per pixel with {photometric}")));
}
if ybr && bits != 8 {
return Err(CodecError::unsupported(F, format!("{bits}-bit {photometric} (8-bit only)")));
}
let layout = if mono { ChannelLayout::Gray } else { ChannelLayout::Rgb };
let out_sample = if bits == 8 { SampleType::U8 } else { SampleType::U16 };
limits.check(cols, rows, layout, out_sample)?;
let pixels = (cols as usize).checked_mul(rows as usize).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?;
let n = pixels.checked_mul(samples as usize).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?;
let bytes_per = if bits == 16 { 2usize } else { 1 };
let need = n.checked_mul(bytes_per).ok_or_else(|| CodecError::LimitExceeded("pixel data too large".into()))?;
let data = b
.get(pixel.start..pixel.start.checked_add(need).ok_or_else(|| CodecError::LimitExceeded("pixel data too large".into()))?)
.ok_or_else(|| bad("pixel data is truncated"))?;
let out_channels = layout.channels();
let out_len = pixels.checked_mul(out_channels).and_then(|k| k.checked_mul(bytes_per)).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?;
let mut out = Vec::new();
out.try_reserve_exact(out_len).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?;
// Raw sample of channel `ch` at pixel `p`, honouring the planar configuration.
let sample_at = |p: usize, ch: usize| -> Option<u32> {
let idx = if planar == 1 { ch.checked_mul(pixels)?.checked_add(p)? } else { p.checked_mul(samples as usize)?.checked_add(ch)? };
let off = idx.checked_mul(bytes_per)?;
if bits == 16 {
let c = data.get(off..off.checked_add(2)?)?;
Some(if big { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) }.into())
} else {
Some(u32::from(*data.get(off)?))
}
};
let stored_max = f64::from((1u32 << bits_stored) - 1);
// Extract the `bits_stored` samples (the least significant one at `high_bit`) and scale to
// [0, 1]; signed values are shifted up by half a range first. `high_bit` is 1-based here.
let shift = (u32::from(high_bit) + 1).saturating_sub(u32::from(bits_stored));
let mask = (1u32 << bits_stored) - 1;
let half = 1u32 << (bits_stored - 1);
let norm = move |raw: u32| -> f64 {
let v = (raw >> shift) & mask;
// Signed: flip the sign bit to map [-2^(b-1), 2^(b-1)-1] onto [0, 2^b-1].
let v = if signed { v ^ half } else { v };
(f64::from(v) / stored_max).clamp(0.0, 1.0)
};
let q = |x: f64| -> u16 { (x * 65535.0).round() as u16 };
let push = |out: &mut Vec<u8>, x: f64| {
if bits == 8 {
out.push((x * 255.0).round() as u8);
} else {
out.extend_from_slice(&q(x).to_ne_bytes());
}
};
for p in 0..pixels {
let s = |ch: usize| sample_at(p, ch).ok_or_else(|| bad("pixel data is shorter than the image"));
if mono {
let x = norm(s(0)?);
push(&mut out, if photometric == "MONOCHROME1" { 1.0 - x } else { x });
} else if rgb {
for ch in 0..3 {
push(&mut out, norm(s(ch)?));
}
} else {
// YBR_FULL (8-bit): BT.601 full-range to RGB.
let y = f64::from(s(0)?);
let cb = f64::from(s(1)?) - 128.0;
let cr = f64::from(s(2)?) - 128.0;
let r = (y + 1.402 * cr).clamp(0.0, 255.0);
let g = (y - 0.344_136 * cb - 0.714_136 * cr).clamp(0.0, 255.0);
let bl = (y + 1.772 * cb).clamp(0.0, 255.0);
for v in [r, g, bl] {
push(&mut out, v / 255.0);
}
}
}
Image::from_raw(cols, rows, layout, out_sample, out)
}
#[cfg(test)]
mod tests {
use super::*;
/// Build the file meta group (explicit VR little-endian), including the given extra
/// `(element, ascii)` pairs before the TransferSyntaxUID.
fn meta(b: &mut Vec<u8>, ts: &str, extra: &[(u16, &str)]) {
let ui = |e: u16, v: &str, b: &mut Vec<u8>| {
let mut s = v.as_bytes().to_vec();
if s.len() % 2 == 1 {
s.push(0);
}
b.extend_from_slice(&0x0002u16.to_le_bytes());
b.extend_from_slice(&e.to_le_bytes());
b.extend_from_slice(b"UI");
b.extend_from_slice(&(s.len() as u16).to_le_bytes());
b.extend_from_slice(&s);
};
for (e, v) in extra {
ui(*e, v, b);
}
ui(0x0010, ts, b);
}
fn us(b: &mut Vec<u8>, g: u16, e: u16, v: u16, explicit: bool) {
b.extend_from_slice(&g.to_le_bytes());
b.extend_from_slice(&e.to_le_bytes());
if explicit {
b.extend_from_slice(b"US");
b.extend_from_slice(&2u16.to_le_bytes());
} else {
b.extend_from_slice(&2u32.to_le_bytes());
}
b.extend_from_slice(&v.to_le_bytes());
}
fn cs(b: &mut Vec<u8>, g: u16, e: u16, v: &str, explicit: bool) {
let mut s = v.as_bytes().to_vec();
if s.len() % 2 == 1 {
s.push(0);
}
b.extend_from_slice(&g.to_le_bytes());
b.extend_from_slice(&e.to_le_bytes());
if explicit {
b.extend_from_slice(b"CS");
b.extend_from_slice(&(s.len() as u16).to_le_bytes());
} else {
b.extend_from_slice(&(s.len() as u32).to_le_bytes());
}
b.extend_from_slice(&s);
}
/// A DICOM with the given transfer syntax UID and data-set encoding.
#[allow(clippy::too_many_arguments)]
fn synth(ts: &str, explicit: bool, big: bool, planar: u16, rows: u16, cols: u16, samples: u16, bits: u16, photometric: &str, pixels: &[u8]) -> Vec<u8> {
let mut b = vec![0u8; 128];
b.extend_from_slice(b"DICM");
// One extra meta element before the syntax, so a reader that only looks at the last
// group-0002 element takes the wrong one.
meta(&mut b, ts, &[(0x0002, "1.2.840.10008.5.1.4.1.1.7")]);
let _ = big;
us(&mut b, 0x0028, 0x0010, rows, explicit);
us(&mut b, 0x0028, 0x0011, cols, explicit);
us(&mut b, 0x0028, 0x0002, samples, explicit);
us(&mut b, 0x0028, 0x0006, planar, explicit);
cs(&mut b, 0x0028, 0x0004, photometric, explicit);
us(&mut b, 0x0028, 0x0100, bits, explicit);
us(&mut b, 0x0028, 0x0101, bits, explicit);
us(&mut b, 0x0028, 0x0102, bits - 1, explicit);
us(&mut b, 0x0028, 0x0103, 0, explicit);
b.extend_from_slice(&0x7FE0u16.to_le_bytes());
b.extend_from_slice(&0x0010u16.to_le_bytes());
if explicit {
b.extend_from_slice(b"OB");
b.extend_from_slice(&0u16.to_le_bytes());
}
b.extend_from_slice(&(pixels.len() as u32).to_le_bytes());
b.extend_from_slice(pixels);
b
}
#[test]
fn decodes_8bit_monochrome() {
let b = synth("1.2.840.10008.1.2.1", true, false, 0, 2, 2, 1, 8, "MONOCHROME2", &[0, 51, 102, 255]);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (2, 2, ChannelLayout::Gray, SampleType::U8));
assert_eq!(img.data(), &[0, 51, 102, 255]);
}
#[test]
fn decodes_16bit_monochrome_and_scales() {
let px: Vec<u8> = [0u16, 32768, 65535].iter().flat_map(|v| v.to_le_bytes()).collect();
let b = synth("1.2.840.10008.1.2.1", true, false, 0, 3, 1, 1, 16, "MONOCHROME2", &px);
let img = decode(&b, &Limits::default()).unwrap();
let v = img.to_u16_samples().unwrap();
assert_eq!(v[0], 0);
assert!((i32::from(v[1]) - 32768).abs() <= 1);
assert_eq!(v[2], 65535);
}
#[test]
fn monochrome1_is_inverted() {
let b = synth("1.2.840.10008.1.2.1", true, false, 0, 2, 1, 1, 8, "MONOCHROME1", &[0, 255]);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.data(), &[255, 0]);
}
#[test]
fn decodes_rgb_interleaved_and_planar() {
// 2 pixels: (10,20,30) and (40,50,60).
let interleaved = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 2, 3, 8, "RGB", &[10, 20, 30, 40, 50, 60]);
let img = decode(&interleaved, &Limits::default()).unwrap();
assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgb, &[10u8, 20, 30, 40, 50, 60][..]));
// Planar 0: R plane, then G, then B.
let planar = synth("1.2.840.10008.1.2.1", true, false, 1, 1, 2, 3, 8, "RGB", &[10, 40, 20, 50, 30, 60]);
let img = decode(&planar, &Limits::default()).unwrap();
assert_eq!(img.data(), &[10, 20, 30, 40, 50, 60]);
}
#[test]
fn decodes_ybr_full_to_rgb() {
// White (Y=128, Cb=Cr=128) stays neutral grey.
let b = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 1, 3, 8, "YBR_FULL", &[128, 128, 128]);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.layout(), ChannelLayout::Rgb);
let d = img.data();
assert!(d[0].abs_diff(d[1]) <= 1 && d[1].abs_diff(d[2]) <= 1, "{d:?}");
}
#[test]
fn decodes_implicit_vr() {
let b = synth("1.2.840.10008.1.2", false, false, 0, 2, 2, 1, 8, "MONOCHROME2", &[0, 51, 102, 255]);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.layout(), ChannelLayout::Gray);
assert_eq!(img.data(), &[0, 51, 102, 255]);
}
#[test]
fn skips_an_undefined_length_sequence_outside_the_pixel_data() {
// Insert an undefined-length SQ (0008,2218) with one item, then a defined-length one,
// before the pixel data: the reader must skip both and still decode the image.
let mut b = vec![0u8; 128];
b.extend_from_slice(b"DICM");
meta(&mut b, "1.2.840.10008.1.2.1", &[]);
for (g, e, v) in [
(0x0028, 0x0010, 1u16),
(0x0028, 0x0011, 1),
(0x0028, 0x0002, 1),
(0x0028, 0x0100, 8),
(0x0028, 0x0101, 8),
(0x0028, 0x0102, 7),
(0x0028, 0x0103, 0),
] {
us(&mut b, g, e, v, true);
}
cs(&mut b, 0x0028, 0x0004, "MONOCHROME2", true);
// Undefined-length sequence (0008,2211), one undefined-length item containing a nested
// defined-length sequence, then the delimiters.
b.extend_from_slice(&[0x08, 0x00, 0x11, 0x22, b'S', b'Q', 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF]);
b.extend_from_slice(&[0xFE, 0xFF, 0x00, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF]); // item, undefined
b.extend_from_slice(&[0x08, 0x00, 0x55, 0x22, b'S', b'Q', 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 1, 2, 3, 4]); // nested, defined
b.extend_from_slice(&[0xFE, 0xFF, 0x0D, 0xE0, 0x00, 0x00, 0x00, 0x00]); // item delimitation
b.extend_from_slice(&[0xFE, 0xFF, 0xDD, 0xE0, 0x00, 0x00, 0x00, 0x00]); // sequence delimitation
// Pixel data (defined length).
b.extend_from_slice(&0x7FE0u16.to_le_bytes());
b.extend_from_slice(&0x0010u16.to_le_bytes());
b.extend_from_slice(b"OB");
b.extend_from_slice(&0u16.to_le_bytes());
b.extend_from_slice(&1u32.to_le_bytes());
b.push(200);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.data(), &[200]);
}
#[test]
fn truncated_and_bad_is_error_not_panic() {
let b = synth("1.2.840.10008.1.2.1", true, false, 0, 2, 2, 1, 8, "MONOCHROME2", &[0, 1, 2, 3]);
for cut in [0, 4, 128, 132, 140, b.len() - 1] {
assert!(decode(&b[..cut.min(b.len())], &Limits::default()).is_err(), "cut {cut}");
}
assert!(decode(b"not dicom", &Limits::default()).is_err());
}
#[test]
fn high_bit_outside_the_allocated_bits_is_an_error_not_a_panic() {
let mut b = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 1, 1, 8, "MONOCHROME2", &[7]);
let tag = [0x28, 0x00, 0x02, 0x01, b'U', b'S', 0x02, 0x00];
let at = b.windows(tag.len()).position(|w| w == tag).unwrap() + tag.len();
b[at..at + 2].copy_from_slice(&200u16.to_le_bytes());
assert!(decode(&b, &Limits::default()).is_err());
}
#[test]
fn rejects_compressed_transfer_syntax() {
let mut b = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 1, 1, 8, "MONOCHROME2", &[0]);
let uncompressed = b"1.2.840.10008.1.2.1\0";
let compressed = b"1.2.840.10008.1.2.4\0"; // same length, a compressed family
let at = b.windows(uncompressed.len()).position(|w| w == uncompressed).expect("transfer syntax in the fixture");
b[at..at + compressed.len()].copy_from_slice(compressed);
assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. })));
}
}
+132 -770
View File
@@ -1,15 +1,8 @@
//! DPX (SMPTE 268M Digital Picture Exchange), implemented here from the published layout: the
//! 1664-byte generic header (file and image information, image source), the 384-byte industry
//! header (film and television), then the image data of each element.
//! DPX (SMPTE 268M Digital Picture Exchange) and Kodak Cineon (`.cin`): the format code is
//! craft-libs' `craft-dpx`. This maps its frames and header fields onto this crate's image:
//!
//! Reading:
//!
//! * Both byte orders: `SDPX` (big-endian) and `XPDS` (little-endian).
//! * The first image element, descriptor 6 (luma), 50 (RGB) or 51 (RGBA), unsigned, without
//! run-length encoding, at 8, 10, 12 or 16 bits. 10-bit samples are packed into 32-bit words
//! (packing 0), or three to a word with the padding bits at the bottom (1, method A) or the
//! top (2, method B); 12-bit samples are packed (0) or one to a 16-bit word in its top bits
//! (1, method A). Lines start on a 32-bit word boundary.
//! * The first image element, luma, RGB or RGBA (ABGR comes back as RGBA). Y'CbCr elements are
//! not converted here and stay unsupported.
//! * 8- and 16-bit code values become U8 and U16 samples unchanged; 10- and 12-bit ones become U16
//! as `round(c × 65535 / (2^b − 1))`, so they come back exactly when written at their depth.
//! * No colour transform: the transfer characteristic and colorimetric specification are reported
@@ -18,11 +11,11 @@
//! * Header fields a pipeline relies on travel as text metadata under the [`KEYS`] names, and a
//! DPX export writes them back.
//!
//! Writing: one element, big-endian, unencoded, descriptor 6, 50 or 51; 8 and 16 bits with
//! packing 0, 10 and 12 bits with packing 1 (method A). The bit depth follows the image
//! (8 → 8, 16 → 16) unless [`EncodeOptions::dpx_bits`] picks one.
//!
//! Kodak Cineon (`.cin`), the format DPX grew from, is read and written here too (see its section).
//! Writing: one element, big-endian, unencoded, luma, RGB or RGBA; 8 and 16 bits with packing 0,
//! 10 and 12 bits with packing 1 (method A). The bit depth follows the image (8 → 8, 16 → 16)
//! unless [`EncodeOptions::dpx_bits`] picks one. Cineon is written at 10 bits.
use craft_dpx::{Frame, Info, Kind, Layout};
use crate::Format;
use crate::error::CodecError;
@@ -31,29 +24,8 @@ use crate::image::{ChannelLayout, DecodeWarning, Image, SampleType};
use crate::options::{DpxBits, EncodeOptions, Limits};
const F: Format = Format::Dpx;
/// Generic plus industry header: where our image data starts.
const HEADER_LEN: usize = 2048;
const GENERIC_LEN: u32 = 1664;
const INDUSTRY_LEN: u32 = 384;
/// "Undefined" for 32-bit fields (also the bit pattern of an undefined float).
const UNDEF32: u32 = u32::MAX;
const UNDEF8: u8 = u8::MAX;
/// Offset of the first image element and the size of one.
const ELEMENT0: usize = 780;
const ELEMENT_LEN: usize = 72;
const MAX_ELEMENTS: u16 = 8;
// Header field offsets (generic file header, image information, film and TV headers).
const CREATION_TIME: (usize, usize) = (136, 24);
const CREATOR: (usize, usize) = (160, 100);
const PROJECT: (usize, usize) = (260, 200);
const COPYRIGHT: (usize, usize) = (460, 200);
const ORIENTATION: usize = 768;
const FRAME_POSITION: usize = 1712;
const FRAME_RATE: usize = 1724;
const TIME_CODE: usize = 1920;
/// Text metadata keys of the header fields kept through a DPX → document → DPX round trip.
/// Header fields carried as text metadata.
pub(crate) const KEY_CREATOR: &str = "DPX:Creator";
pub(crate) const KEY_PROJECT: &str = "DPX:Project";
pub(crate) const KEY_COPYRIGHT: &str = "DPX:Copyright";
@@ -66,473 +38,126 @@ pub(crate) const KEY_TIME_CODE: &str = "DPX:TimeCode";
pub(crate) const KEYS: [&str; 9] =
[KEY_CREATOR, KEY_PROJECT, KEY_COPYRIGHT, KEY_CREATION_TIME, KEY_TRANSFER, KEY_COLORIMETRIC, KEY_FRAME_POSITION, KEY_FRAME_RATE, KEY_TIME_CODE];
fn err(msg: impl std::fmt::Display) -> CodecError {
CodecError::malformed(F, msg)
}
fn unsupported(msg: impl Into<String>) -> CodecError {
CodecError::unsupported(F, msg)
}
/// The file's byte order.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Order {
Big,
Little,
}
impl Order {
fn bytes<const N: usize>(b: &[u8], at: usize) -> Option<[u8; N]> {
b.get(at..at.checked_add(N)?)?.try_into().ok()
}
fn u16(self, b: &[u8], at: usize) -> Option<u16> {
let s = Self::bytes::<2>(b, at)?;
Some(if self == Order::Big { u16::from_be_bytes(s) } else { u16::from_le_bytes(s) })
}
fn u32(self, b: &[u8], at: usize) -> Option<u32> {
let s = Self::bytes::<4>(b, at)?;
Some(if self == Order::Big { u32::from_be_bytes(s) } else { u32::from_le_bytes(s) })
}
/// A 32-bit field that may be undefined (all ones).
fn defined_u32(self, b: &[u8], at: usize) -> Option<u32> {
self.u32(b, at).filter(|&v| v != UNDEF32)
}
fn f32(self, b: &[u8], at: usize) -> Option<f32> {
self.defined_u32(b, at).map(f32::from_bits).filter(|v| v.is_finite())
fn err(format: Format, e: craft_dpx::Error) -> CodecError {
match e {
craft_dpx::Error::NotDpx => CodecError::malformed(format, "missing the DPX or Cineon magic number"),
craft_dpx::Error::Malformed(m) => CodecError::malformed(format, m),
craft_dpx::Error::Unsupported(m) => CodecError::unsupported(format, m),
craft_dpx::Error::LimitExceeded(m) => CodecError::LimitExceeded(m.into()),
craft_dpx::Error::Encode(m) => CodecError::encode(format, m),
}
}
/// The byte order from the magic number, `None` when it is not DPX.
fn order(b: &[u8]) -> Option<Order> {
match b.get(..4)? {
b"SDPX" => Some(Order::Big),
b"XPDS" => Some(Order::Little),
_ => None,
fn dpx_limits(l: &Limits) -> craft_dpx::Limits {
let mut out = craft_dpx::Limits::default();
out.max_pixels = l.max_pixels;
out
}
/// The header, for a file of `kind`; anything else is malformed.
fn info(bytes: &[u8], kind: Kind) -> Result<Info, CodecError> {
let format = if kind == Kind::Dpx { F } else { Format::Cineon };
if craft_dpx::sniff(bytes) != Some(kind) {
return Err(CodecError::malformed(
format,
if kind == Kind::Dpx { "missing SDPX/XPDS magic number" } else { "missing the Cineon magic number (802A5FD7)" },
));
}
}
/// An ASCII header string: up to the first NUL, printable characters only, trimmed. `None` when
/// empty or undefined (all ones).
fn string(b: &[u8], (at, len): (usize, usize)) -> Option<String> {
let raw = b.get(at..at.checked_add(len)?)?;
let end = raw.iter().position(|&c| c == 0).unwrap_or(raw.len());
let s: String = raw.get(..end)?.iter().filter(|c| (32..=126).contains(*c)).map(|&c| c as char).collect();
Some(s.trim().to_string()).filter(|s| !s.is_empty())
}
/// The descriptive fields of image element 0 that decoding needs.
#[derive(Debug, Clone, Copy)]
struct Element {
signed: bool,
descriptor: u8,
transfer: u8,
colorimetric: u8,
bits: u8,
packing: u16,
encoding: u16,
data_offset: usize,
eol_padding: usize,
}
struct Header {
order: Order,
width: u32,
height: u32,
elements: u16,
element: Element,
// The size is checked by the caller, against its own limits.
let mut unlimited = craft_dpx::Limits::default();
unlimited.max_pixels = u64::MAX;
craft_dpx::probe(bytes, &unlimited).map_err(|e| err(format, e))
}
/// The decoder opens only element zero; protect the other declared elements from flat saves.
pub(crate) fn has_additional_images(bytes: &[u8]) -> Result<bool, CodecError> {
Ok(parse(bytes)?.elements > 1)
Ok(info(bytes, Kind::Dpx)?.elements > 1)
}
fn parse(b: &[u8]) -> Result<Header, CodecError> {
let o = order(b).ok_or_else(|| err("missing SDPX/XPDS magic number"))?;
let missing = |what: &str| err(format!("the header ends before {what}"));
let image_offset = o.u32(b, 4).ok_or_else(|| missing("the image data offset"))?;
o.u16(b, ORIENTATION).ok_or_else(|| missing("the orientation"))?;
let elements = o.u16(b, ORIENTATION + 2).ok_or_else(|| missing("the element count"))?;
if !(1..=MAX_ELEMENTS).contains(&elements) {
return Err(err(format!("invalid number of image elements {elements}")));
}
let width = o.u32(b, 772).ok_or_else(|| missing("the line length"))?;
let height = o.u32(b, 776).ok_or_else(|| missing("the line count"))?;
let e = ELEMENT0;
let field = |at: usize| o.u32(b, e + at).ok_or_else(|| missing("image element 1"));
let byte = |at: usize| b.get(e + at).copied().ok_or_else(|| missing("image element 1"));
let half = |at: usize| o.u16(b, e + at).ok_or_else(|| missing("image element 1"));
let data_offset = match field(28)? {
0 | UNDEF32 => image_offset,
v => v,
};
let eol_padding = match field(32)? {
UNDEF32 => 0,
v => v,
};
let element = Element {
signed: field(0)? == 1,
descriptor: byte(20)?,
transfer: byte(21)?,
colorimetric: byte(22)?,
bits: byte(23)?,
packing: half(24)?,
encoding: half(26)?,
data_offset: usize::try_from(data_offset).map_err(|_| err("image data offset out of range"))?,
eol_padding: usize::try_from(eol_padding).map_err(|_| err("line padding out of range"))?,
};
Ok(Header { order: o, width, height, elements, element })
}
/// The EXIF orientation (1–8) that turns the stored lines upright: the DPX orientation codes 0–7
/// in SMPTE 268M order (left-to-right top-to-bottom, mirrored, flipped, rotated, transposed…).
/// Anything else, undefined included, is taken as the default (1).
/// The EXIF orientation (1–8) that turns the stored lines upright; 1 for an unreadable header.
pub(crate) fn orientation(b: &[u8]) -> u16 {
let Some(o) = order(b).and_then(|o| o.u16(b, ORIENTATION)) else { return 1 };
match o {
1 => 2,
2 => 4,
3 => 3,
4 => 5,
5 => 6,
6 => 8,
7 => 7,
_ => 1,
}
info(b, Kind::Dpx).map_or(1, |i| i.exif_orientation())
}
/// Bytes in one stored line of `samples` samples, padded to a 32-bit word.
fn line_bytes(samples: usize, bits: u8, packing: u16) -> Option<usize> {
let words = match (bits, packing) {
// Three samples to a word, or one to a 16-bit half word.
(10, 1 | 2) => samples.div_ceil(3),
(12, 1 | 2) => samples.div_ceil(2),
_ => samples.checked_mul(usize::from(bits))?.div_ceil(32),
/// The Cineon magic number (`802A5FD7` hex).
pub(crate) fn is_cineon(b: &[u8]) -> bool {
craft_dpx::sniff(b) == Some(Kind::Cineon)
}
/// The EXIF orientation (1–8) for a Cineon header's orientation code.
pub(crate) fn cineon_orientation(b: &[u8]) -> u16 {
info(b, Kind::Cineon).map_or(1, |i| i.exif_orientation())
}
/// A decoded frame as an image: luma, RGB or RGBA, 8 bits as U8, deeper codes widened to U16.
fn image(format: Format, frame: Frame) -> Result<(Info, Image), CodecError> {
let i = &frame.info;
let layout = match i.layout {
Layout::Luma => ChannelLayout::Gray,
Layout::Rgb => ChannelLayout::Rgb,
Layout::Rgba => ChannelLayout::Rgba,
l => return Err(CodecError::unsupported(format, format!("{l:?} (Y'CbCr) elements are not supported (only luma, RGB and RGBA)"))),
};
words.checked_mul(4)
let img = match i.bits {
8 => Image::from_u8(i.width, i.height, layout, frame.codes.iter().map(|&c| c as u8).collect())?,
_ => Image::from_u16(i.width, i.height, layout, &frame.samples_u16())?,
};
Ok((frame.info, img))
}
/// Code values of one stored line.
fn unpack_line(line: &[u8], o: Order, bits: u8, packing: u16, out: &mut [u16]) -> Result<(), CodecError> {
let short = || err("truncated image data");
match (bits, packing) {
(8, _) => {
let src = line.get(..out.len()).ok_or_else(short)?;
for (v, &c) in out.iter_mut().zip(src) {
*v = u16::from(c);
}
}
(16, _) => {
for (i, v) in out.iter_mut().enumerate() {
*v = o.u16(line, i * 2).ok_or_else(short)?;
}
}
(10, 1 | 2) => {
let base = if packing == 1 { 22 } else { 20 };
for (i, v) in out.iter_mut().enumerate() {
let word = o.u32(line, i / 3 * 4).ok_or_else(short)?;
*v = ((word >> (base - 10 * (i % 3) as u32)) & 0x3FF) as u16;
}
}
(12, 1 | 2) => {
let shift = if packing == 1 { 4 } else { 0 };
for (i, v) in out.iter_mut().enumerate() {
*v = (o.u16(line, i * 2).ok_or_else(short)? >> shift) & 0xFFF;
}
}
// Packing 0: one bit stream through the line's 32-bit words, high bits first.
_ => {
let mask = (1u64 << bits) - 1;
let (mut acc, mut have, mut word) = (0u64, 0u32, 0usize);
for v in out.iter_mut() {
while have < u32::from(bits) {
acc = (acc << 32) | u64::from(o.u32(line, word * 4).ok_or_else(short)?);
word += 1;
have += 32;
}
have -= u32::from(bits);
*v = ((acc >> have) & mask) as u16;
}
}
}
Ok(())
}
/// A 10- or 12-bit code value as a 16-bit sample: `round(c × 65535 / max)`.
fn widen(c: u16, max: u64) -> u16 {
((u64::from(c) * 2 * 65535 + max) / (2 * max)).min(65535) as u16
}
/// The inverse of [`widen`]: `round(v × max / 65535)`.
fn narrow(v: u16, max: u64) -> u16 {
((u64::from(v) * 2 * max + 65535) / (2 * 65535)) as u16
/// Decodes a frame of `kind` within `limits`, checked as this crate checks every decoder.
fn decode_kind(bytes: &[u8], limits: &Limits, kind: Kind) -> Result<(Info, Image), CodecError> {
let format = if kind == Kind::Dpx { F } else { Format::Cineon };
let i = info(bytes, kind)?;
let layout = match i.layout {
Layout::Luma => ChannelLayout::Gray,
Layout::Rgb => ChannelLayout::Rgb,
_ => ChannelLayout::Rgba,
};
limits.check(i.width, i.height, layout, if i.bits == 8 { SampleType::U8 } else { SampleType::U16 })?;
image(format, craft_dpx::decode(bytes, &dpx_limits(limits)).map_err(|e| err(format, e))?)
}
pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
let h = parse(bytes)?;
let e = h.element;
let layout = match e.descriptor {
6 => ChannelLayout::Gray,
50 => ChannelLayout::Rgb,
51 => ChannelLayout::Rgba,
d => return Err(unsupported(format!("descriptor {d} is not supported (only 6 luma, 50 RGB and 51 RGBA)"))),
};
if e.signed {
return Err(unsupported("data sign 1 (signed samples) is not supported"));
let (info, mut img) = decode_kind(bytes, limits, Kind::Dpx)?;
let i = &info;
if i.elements > 1 {
img.warnings.push(DecodeWarning::MoreElements { total: u32::from(i.elements) });
}
if e.encoding != 0 {
return Err(unsupported(format!("encoding {} (run-length encoded data) is not supported", e.encoding)));
if i.transfer.is_some() || i.colorimetric.is_some() {
img.warnings.push(DecodeWarning::DpxEncoding { transfer: i.transfer.unwrap_or(u8::MAX), colorimetric: i.colorimetric.unwrap_or(u8::MAX) });
}
let packing_ok = match e.bits {
8 | 16 => e.packing == 0,
10 => e.packing <= 2,
12 => e.packing <= 1,
b => return Err(unsupported(format!("bit size {b} is not supported (only 8, 10, 12 and 16)"))),
};
if !packing_ok {
return Err(unsupported(format!("packing {} is not supported at {} bits", e.packing, e.bits)));
}
let sample = if e.bits == 8 { SampleType::U8 } else { SampleType::U16 };
limits.check(h.width, h.height, layout, sample)?;
let nc = layout.channels();
let per_line = (h.width as usize).checked_mul(nc).ok_or_else(|| CodecError::LimitExceeded("line too long".into()))?;
let height = h.height as usize;
let data = bytes.get(e.data_offset..).ok_or_else(|| err("the image data offset is past the end of the file"))?;
let too_large = || CodecError::LimitExceeded("image too large".into());
let mut line_len = line_bytes(per_line, e.bits, e.packing).ok_or_else(too_large)?;
let mut stride = line_len.checked_add(e.eol_padding).ok_or_else(too_large)?;
// Lines run `stride` apart; the last one needs no padding after it.
let span = |stride: usize, line_len: usize| stride.checked_mul(height - 1)?.checked_add(line_len);
// Some writers pack 8- and 16-bit lines without padding them to a word; take that layout
// when the file is too short for padded lines but holds the unpadded ones.
let tight = per_line * usize::from(e.bits / 8);
if matches!(e.bits, 8 | 16) && e.eol_padding == 0 && line_len != tight && span(stride, line_len).is_none_or(|need| data.len() < need) {
(line_len, stride) = (tight, tight);
}
if span(stride, line_len).ok_or_else(too_large)? > data.len() {
return Err(err("truncated image data"));
}
let mut codes = vec![0u16; per_line.checked_mul(height).ok_or_else(too_large)?];
for (y, row) in codes.chunks_exact_mut(per_line.max(1)).enumerate() {
let start = y * stride;
let line = data.get(start..).ok_or_else(|| err("truncated image data"))?;
unpack_line(line, h.order, e.bits, e.packing, row)?;
}
let mut img = match e.bits {
8 => Image::from_u8(h.width, h.height, layout, codes.iter().map(|&c| c as u8).collect())?,
16 => Image::from_u16(h.width, h.height, layout, &codes)?,
b => {
let max = (1u64 << b) - 1;
let wide: Vec<u16> = codes.iter().map(|&c| widen(c, max)).collect();
Image::from_u16(h.width, h.height, layout, &wide)?
}
};
if h.elements > 1 {
img.warnings.push(DecodeWarning::MoreElements { total: u32::from(h.elements) });
}
if e.transfer != UNDEF8 || e.colorimetric != UNDEF8 {
img.warnings.push(DecodeWarning::DpxEncoding { transfer: e.transfer, colorimetric: e.colorimetric });
}
img.meta.text = header_text(bytes, h.order, e);
img.meta.text = header_text(i);
Ok(img)
}
// ---------------------------------------------------------------- Cineon (Kodak .cin)
//
// Cineon is the 10-bit film-scan format DPX grew from (same Kodak header family, big-endian).
// The generic header is 1024 bytes: the file information fields, then at offset 192 the image
// orientation (1 byte), the channel count (1 byte) and up to eight 28-byte channel descriptors
// (the first carries the bits per pixel and the line/image dimensions). Image data is three
// 10-bit samples packed into each 32-bit word, lines on a word boundary — the same layout as
// DPX's 10-bit packing method A, so the unpacking and packing are shared.
/// The Cineon magic number (`802A5FD7` hex), big-endian.
pub(crate) fn is_cineon(b: &[u8]) -> bool {
b.get(..4) == Some(&[0x80, 0x2A, 0x5F, 0xD7])
}
/// The EXIF orientation (1–8) for a Cineon header's orientation code (the same 0–7 order as DPX).
pub(crate) fn cineon_orientation(b: &[u8]) -> u16 {
let Some(code) = b.get(192).copied() else { return 1 };
match code {
1 => 2,
2 => 4,
3 => 3,
4 => 5,
5 => 6,
6 => 8,
7 => 7,
_ => 1,
}
}
/// Decode a Cineon (`.cin`) frame: 10-bit logarithmic film scans, big-endian, one (gray) or
/// three (RGB) channels. The 10-bit code values become U16 as `round(c × 65535 / 1023)`; the
/// file's black/white reference points are not applied (a log conversion is a display choice).
/// Decode a Cineon (`.cin`) frame: 10-bit logarithmic film scans, one (gray) or three (RGB)
/// channels. The file's black/white reference points are not applied (a log conversion is a
/// display choice).
pub(crate) fn decode_cineon(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
if !is_cineon(bytes) {
return Err(err("missing the Cineon magic number (802A5FD7)"));
}
let o = Order::Big;
let missing = |what: &str| err(format!("the header ends before {what}"));
let image_offset = usize::try_from(o.u32(bytes, 4).ok_or_else(|| missing("the image data offset"))?).map_err(|_| err("image data offset out of range"))?;
let channels = *bytes.get(193).ok_or_else(|| missing("the channel count"))?;
let bits = *bytes.get(198).ok_or_else(|| missing("the bits per pixel"))?;
if bits != 10 {
return Err(unsupported(format!("{bits}-bit samples (Cineon frames are 10-bit)")));
}
let layout = match channels {
1 => ChannelLayout::Gray,
3 => ChannelLayout::Rgb,
n => return Err(unsupported(format!("{n} channels (Cineon frames have 1 or 3)"))),
};
let width = o.u32(bytes, 200).ok_or_else(|| missing("the pixels per line"))?;
let height = o.u32(bytes, 204).ok_or_else(|| missing("the lines per image"))?;
limits.check(width, height, layout, SampleType::U16)?;
let (w, h) = (width as usize, height as usize);
let per_line = w.checked_mul(layout.channels()).ok_or_else(|| CodecError::LimitExceeded("line too long".into()))?;
// Three 10-bit samples per 32-bit word, lines on a word boundary (DPX packing 1).
let line_len = line_bytes(per_line, 10, 1).ok_or_else(|| CodecError::LimitExceeded("line too long".into()))?;
let data = bytes.get(image_offset..).ok_or_else(|| err("the image data offset is past the end of the file"))?;
let need = line_len.checked_mul(h).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?;
if data.len() < need {
return Err(err("truncated image data"));
}
let mut codes = vec![0u16; per_line.checked_mul(h).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?];
for (y, row) in codes.chunks_exact_mut(per_line).enumerate() {
let start = y * line_len;
let line = data.get(start..start + line_len).ok_or_else(|| err("truncated image data"))?;
unpack_line(line, o, 10, 1, row)?;
}
let wide: Vec<u16> = codes.iter().map(|&c| widen(c, 1023)).collect();
Image::from_u16(width, height, layout, &wide)
}
/// Cineon's generic plus film header: where our image data starts.
const CINEON_HEADER_LEN: usize = 2048;
const CINEON_GENERIC_LEN: u32 = 1024;
/// Offset of the first of the eight 28-byte channel descriptors.
const CINEON_CHANNEL0: usize = 196;
/// Encode a Cineon 4.5 frame: the generic header, an undefined film header, then the samples as
/// 10-bit code values `round(v × 1023 / 65535)`, three to a big-endian word, left justified
/// (packing 5), lines on a word boundary. The plan has already made the image U16 gray or RGB.
pub(crate) fn encode_cineon(src: &Image, plan: Plan) -> Result<Vec<u8>, CodecError> {
let enc = |msg: String| CodecError::encode(Format::Cineon, msg);
let img = src.converted(plan.layout, plan.sample);
let (w, h) = img.dimensions();
// Channel designators: 0 for black and white, 1–3 for red, green and blue.
let designators: &[u8] = match img.layout() {
ChannelLayout::Gray => &[0],
ChannelLayout::Rgb => &[1, 2, 3],
l => return Err(enc(format!("Cineon cannot store {l:?}"))),
};
let samples = img.to_u16_samples().ok_or_else(|| enc(format!("cannot write {:?} samples at 10 bits", img.sample_type())))?;
let codes: Vec<u16> = samples.into_iter().map(|v| narrow(v, 1023)).collect();
let per_line = (w as usize).checked_mul(designators.len()).ok_or_else(|| enc("image too large".into()))?;
if codes.len() != per_line.checked_mul(h as usize).ok_or_else(|| enc("image too large".into()))? {
return Err(enc("the image has the wrong number of samples".into()));
}
let data_len = line_bytes(per_line, 10, 1).and_then(|l| l.checked_mul(h as usize)).ok_or_else(|| enc("image too large".into()))?;
let total =
CINEON_HEADER_LEN.checked_add(data_len).and_then(|t| u32::try_from(t).ok()).ok_or_else(|| enc("image too large for a Cineon file (4 GiB)".into()))?;
let mut wr = Writer { h: vec![0; CINEON_HEADER_LEN] };
// File information.
wr.u32(0, 0x802A_5FD7);
wr.u32(4, CINEON_HEADER_LEN as u32);
wr.u32(8, CINEON_GENERIC_LEN);
wr.u32(12, CINEON_HEADER_LEN as u32 - CINEON_GENERIC_LEN);
wr.u32(16, 0);
wr.u32(20, total);
wr.string((24, 8), "V4.5");
// Image information: default orientation, then each channel's descriptor; the unused ones,
// the white point and the primaries are undefined.
wr.u8(192, 0);
wr.u8(193, designators.len() as u8);
wr.undefined(CINEON_CHANNEL0, 420);
for (i, &designator) in designators.iter().enumerate() {
let c = CINEON_CHANNEL0 + 28 * i;
wr.u8(c, 0);
wr.u8(c + 1, designator);
wr.u8(c + 2, 10);
wr.u8(c + 3, 0);
wr.u32(c + 4, w);
wr.u32(c + 8, h);
// Code values 0–1023 stand for printing densities 0–2.048, the Cineon convention. All
// four are 32-bit floats here (DPX stores the data values as integers).
wr.u32(c + 12, 0f32.to_bits());
wr.u32(c + 16, 0f32.to_bits());
wr.u32(c + 20, 1023f32.to_bits());
wr.u32(c + 24, 2.048f32.to_bits());
}
// Pixel-interleaved, unsigned, positive, no line or channel padding.
wr.u8(681, 5);
// Image origination: pixel pitches and gamma unknown.
wr.undefined(972, 984);
// Film header: manufacturer, type, perforation offset, prefix, count, frame position and rate
// unknown; its strings stay empty.
wr.undefined(1024, 1036);
wr.undefined(1068, 1076);
let mut out = wr.h;
out.try_reserve_exact(data_len).map_err(|_| enc("out of memory".into()))?;
for row in codes.chunks_exact(per_line.max(1)) {
pack_line(&mut out, row, 10);
}
Ok(out)
decode_kind(bytes, limits, Kind::Cineon).map(|(_, img)| img)
}
/// The kept header fields as text metadata, in [`KEYS`] order; undefined ones are left out.
fn header_text(b: &[u8], o: Order, e: Element) -> Vec<(String, String)> {
fn header_text(i: &Info) -> Vec<(String, String)> {
let mut text = Vec::new();
let mut put = |k: &str, v: Option<String>| {
if let Some(v) = v {
text.push((k.to_string(), v));
}
};
put(KEY_CREATOR, string(b, CREATOR));
put(KEY_PROJECT, string(b, PROJECT));
put(KEY_COPYRIGHT, string(b, COPYRIGHT));
put(KEY_CREATION_TIME, string(b, CREATION_TIME));
put(KEY_TRANSFER, (e.transfer != UNDEF8).then(|| e.transfer.to_string()));
put(KEY_COLORIMETRIC, (e.colorimetric != UNDEF8).then(|| e.colorimetric.to_string()));
// The industry header is there when the generic header says so and the file holds it.
let industry = o.u32(b, 28).is_some_and(|l| l != 0 && l != UNDEF32) && b.len() >= HEADER_LEN;
if industry {
put(KEY_FRAME_POSITION, o.defined_u32(b, FRAME_POSITION).map(|v| v.to_string()));
put(KEY_FRAME_RATE, o.f32(b, FRAME_RATE).map(|v| v.to_string()));
put(KEY_TIME_CODE, o.defined_u32(b, TIME_CODE).map(format_time_code));
}
put(KEY_CREATOR, i.creator.clone());
put(KEY_PROJECT, i.project.clone());
put(KEY_COPYRIGHT, i.copyright.clone());
put(KEY_CREATION_TIME, i.creation_time.clone());
put(KEY_TRANSFER, i.transfer.map(|v| v.to_string()));
put(KEY_COLORIMETRIC, i.colorimetric.map(|v| v.to_string()));
put(KEY_FRAME_POSITION, i.frame_position.map(|v| v.to_string()));
put(KEY_FRAME_RATE, i.frame_rate.map(|v| v.to_string()));
put(KEY_TIME_CODE, i.time_code.map(craft_dpx::format_time_code));
text
}
/// A SMPTE time code (BCD `HHMMSSFF`) as `HH:MM:SS:FF`, or `0x…` when it is not valid BCD.
fn format_time_code(v: u32) -> String {
let d: [u32; 8] = std::array::from_fn(|i| (v >> ((7 - i) * 4)) & 0xF);
if d.iter().all(|&d| d <= 9) { format!("{}{}:{}{}:{}{}:{}{}", d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7]) } else { format!("{v:#010x}") }
}
/// The inverse of [`format_time_code`]; `None` for anything else.
fn parse_time_code(s: &str) -> Option<u32> {
if let Some(hex) = s.strip_prefix("0x") {
return u32::from_str_radix(hex, 16).ok();
}
let parts: Vec<&str> = s.split(':').collect();
if parts.len() != 4 || parts.iter().any(|p| p.len() != 2 || !p.bytes().all(|c| c.is_ascii_digit())) {
return None;
}
parts.iter().flat_map(|p| p.bytes()).try_fold(0u32, |acc, c| Some((acc << 4) | u32::from(c - b'0')))
}
/// Bits written for `sample` when the options pick none: 8 for U8, 16 otherwise.
pub(crate) fn bits_for(opt: Option<DpxBits>, sample: SampleType) -> u8 {
match opt {
@@ -558,8 +183,7 @@ pub(crate) fn keeps_u16_samples(img: &Image, bits: u8) -> bool {
if img.sample_type() != SampleType::U16 || !(1..16).contains(&bits) {
return false;
}
let max = (1u64 << bits) - 1;
img.data().as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes(*c)).all(|v| widen(narrow(v, max), max) == v)
img.data().as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes(*c)).all(|v| craft_dpx::widen(craft_dpx::narrow(v, bits), bits) == v)
}
/// The plan's sample type for the chosen bits: U8 for 8 bits, U16 for 10, 12 and 16.
@@ -572,315 +196,53 @@ pub(crate) fn plan_sample(opt: Option<DpxBits>, src: SampleType) -> SampleType {
}
}
struct Writer {
h: Vec<u8>,
}
impl Writer {
fn u8(&mut self, at: usize, v: u8) {
if let Some(b) = self.h.get_mut(at) {
*b = v;
}
}
fn u16(&mut self, at: usize, v: u16) {
if let Some(s) = self.h.get_mut(at..at + 2) {
s.copy_from_slice(&v.to_be_bytes());
}
}
fn u32(&mut self, at: usize, v: u32) {
if let Some(s) = self.h.get_mut(at..at + 4) {
s.copy_from_slice(&v.to_be_bytes());
}
}
/// Marks a run of numeric fields undefined (all ones).
fn undefined(&mut self, from: usize, to: usize) {
if let Some(s) = self.h.get_mut(from..to) {
s.fill(0xFF);
}
}
/// An ASCII string field, cut to leave room for a terminating NUL.
fn string(&mut self, (at, len): (usize, usize), v: &str) {
let ascii: Vec<u8> = v.chars().map(|c| if (' '..='~').contains(&c) { c as u8 } else { b'?' }).take(len.saturating_sub(1)).collect();
if let Some(s) = self.h.get_mut(at..at + ascii.len()) {
s.copy_from_slice(&ascii);
}
}
}
fn text<'a>(img: &'a Image, key: &str) -> Option<&'a str> {
img.meta.text.iter().find(|(k, _)| k == key).map(|(_, v)| v.trim()).filter(|v| !v.is_empty())
}
/// Appends one line's code values, padded to a 32-bit word.
fn pack_line(out: &mut Vec<u8>, codes: &[u16], bits: u8) {
let start = out.len();
match bits {
8 => out.extend(codes.iter().map(|&c| c as u8)),
10 => {
for chunk in codes.chunks(3) {
let word = chunk.iter().enumerate().fold(0u32, |w, (i, &c)| w | (u32::from(c & 0x3FF) << (22 - 10 * i as u32)));
out.extend_from_slice(&word.to_be_bytes());
}
}
12 => out.extend(codes.iter().flat_map(|&c| ((c & 0xFFF) << 4).to_be_bytes())),
_ => out.extend(codes.iter().flat_map(|&c| c.to_be_bytes())),
}
let written = out.len() - start;
out.resize(start + written.div_ceil(4) * 4, 0);
/// `img`'s samples as `bits`-bit code values, with the layout they are written in.
fn codes(format: Format, img: &Image, bits: u8) -> Result<(Layout, Vec<u16>), CodecError> {
let layout = match img.layout() {
ChannelLayout::Gray => Layout::Luma,
ChannelLayout::Rgb => Layout::Rgb,
ChannelLayout::Rgba if format == F => Layout::Rgba,
l => return Err(CodecError::encode(format, format!("{format:?} cannot store {l:?}"))),
};
let codes: Vec<u16> = match (img.sample_type(), bits) {
(SampleType::U8, 8) => img.data().iter().map(|&v| u16::from(v)).collect(),
(SampleType::U16, 16) => img.to_u16_samples().unwrap_or_default(),
(SampleType::U16, 10 | 12) => img.to_u16_samples().unwrap_or_default().into_iter().map(|v| craft_dpx::narrow(v, bits)).collect(),
(s, b) => return Err(CodecError::encode(format, format!("cannot write {s:?} samples at {b} bits"))),
};
Ok((layout, codes))
}
/// Encode a Cineon 4.5 frame at 10 bits. The plan has already made the image U16 gray or RGB.
pub(crate) fn encode_cineon(src: &Image, plan: Plan) -> Result<Vec<u8>, CodecError> {
let img = src.converted(plan.layout, plan.sample);
let (layout, codes) = codes(Format::Cineon, &img, 10)?;
let (width, height) = img.dimensions();
craft_dpx::encode_cineon(&craft_dpx::Picture { width, height, layout, bits: 10, codes: &codes }).map_err(|e| err(Format::Cineon, e))
}
pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
let img = src.converted(plan.layout, plan.sample);
let (w, h) = img.dimensions();
let (descriptor, layout) = match img.layout() {
ChannelLayout::Gray => (6u8, ChannelLayout::Gray),
ChannelLayout::Rgb => (50, ChannelLayout::Rgb),
ChannelLayout::Rgba => (51, ChannelLayout::Rgba),
l => return Err(CodecError::encode(F, format!("DPX cannot store {l:?}"))),
};
let bits = bits_for(opts.dpx_bits, img.sample_type());
let codes: Vec<u16> = match (img.sample_type(), bits) {
(SampleType::U8, 8) => img.data().iter().map(|&v| u16::from(v)).collect(),
(SampleType::U16, 16) => img.to_u16_samples().unwrap_or_default(),
(SampleType::U16, 10 | 12) => {
let max = (1u64 << bits) - 1;
img.to_u16_samples().unwrap_or_default().into_iter().map(|v| narrow(v, max)).collect()
}
(s, b) => return Err(CodecError::encode(F, format!("cannot write {s:?} samples at {b} bits"))),
};
let per_line = w as usize * layout.channels();
if codes.len() != per_line * h as usize {
return Err(CodecError::encode(F, "the image has the wrong number of samples"));
}
let data_len = line_bytes(per_line, bits, if matches!(bits, 10 | 12) { 1 } else { 0 })
.and_then(|l| l.checked_mul(h as usize))
.ok_or_else(|| CodecError::encode(F, "image too large"))?;
let total = u32::try_from(HEADER_LEN + data_len).map_err(|_| CodecError::encode(F, "image too large for a DPX file (4 GiB)"))?;
let mut wr = Writer { h: vec![0; HEADER_LEN] };
// Generic file header.
wr.h[..4].copy_from_slice(b"SDPX");
wr.u32(4, HEADER_LEN as u32);
wr.string((8, 8), "V2.0");
wr.u32(16, total);
wr.u32(20, 1);
wr.u32(24, GENERIC_LEN);
wr.u32(28, INDUSTRY_LEN);
wr.u32(32, 0);
let (layout, codes) = codes(F, &img, bits)?;
let keep = |key| if opts.embed_metadata { text(&img, key) } else { None };
wr.string(CREATION_TIME, keep(KEY_CREATION_TIME).unwrap_or(""));
wr.string(CREATOR, keep(KEY_CREATOR).unwrap_or("PhotoCraft"));
wr.string(PROJECT, keep(KEY_PROJECT).unwrap_or(""));
wr.string(COPYRIGHT, keep(KEY_COPYRIGHT).unwrap_or(""));
wr.u32(660, UNDEF32);
// Image information: default orientation, one element.
wr.u16(ORIENTATION, 0);
wr.u16(ORIENTATION + 2, 1);
wr.u32(772, w);
wr.u32(776, h);
let e = ELEMENT0;
wr.u32(e, 0);
wr.u32(e + 4, 0);
wr.u32(e + 8, UNDEF32);
wr.u32(e + 12, ((1u32 << bits) - 1).min(65535));
wr.u32(e + 16, UNDEF32);
wr.u8(e + 20, descriptor);
// No claim about the encoding unless the document came with one.
let code = |key| keep(key).and_then(|v| v.parse::<u8>().ok()).unwrap_or(UNDEF8);
wr.u8(e + 21, code(KEY_TRANSFER));
wr.u8(e + 22, code(KEY_COLORIMETRIC));
wr.u8(e + 23, bits);
wr.u16(e + 24, if matches!(bits, 10 | 12) { 1 } else { 0 });
wr.u16(e + 26, 0);
wr.u32(e + 28, HEADER_LEN as u32);
wr.u32(e + 32, 0);
wr.u32(e + 36, 0);
// The seven unused elements, the image source header's numbers and the industry header's
// numbers are undefined; their strings stay empty.
wr.undefined(e + ELEMENT_LEN, e + ELEMENT_LEN * usize::from(MAX_ELEMENTS));
wr.undefined(1408, 1432);
wr.undefined(1620, 1644);
wr.undefined(FRAME_POSITION, 1732);
wr.undefined(TIME_CODE, 1972);
if let Some(v) = keep(KEY_FRAME_POSITION).and_then(|v| v.parse::<u32>().ok()) {
wr.u32(FRAME_POSITION, v);
}
if let Some(v) = keep(KEY_FRAME_RATE).and_then(|v| v.parse::<f32>().ok()).filter(|v| v.is_finite()) {
wr.u32(FRAME_RATE, v.to_bits());
}
if let Some(v) = keep(KEY_TIME_CODE).and_then(parse_time_code) {
wr.u32(TIME_CODE, v);
}
let mut out = wr.h;
out.try_reserve_exact(data_len).map_err(|_| CodecError::encode(F, "out of memory"))?;
for row in codes.chunks_exact(per_line.max(1)) {
pack_line(&mut out, row, bits);
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
/// A minimal header for `w`×`h` with element 0 set; image data at 2048.
fn header(o: Order, w: u32, h: u32, descriptor: u8, bits: u8, packing: u16) -> Vec<u8> {
let mut b = vec![0u8; HEADER_LEN];
let put32 = |b: &mut Vec<u8>, at: usize, v: u32| b[at..at + 4].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() });
let put16 = |b: &mut Vec<u8>, at: usize, v: u16| b[at..at + 2].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() });
b[..4].copy_from_slice(if o == Order::Big { b"SDPX" } else { b"XPDS" });
put32(&mut b, 4, HEADER_LEN as u32);
put32(&mut b, 28, INDUSTRY_LEN);
put16(&mut b, ORIENTATION, 0);
put16(&mut b, ORIENTATION + 2, 1);
put32(&mut b, 772, w);
put32(&mut b, 776, h);
b[ELEMENT0 + 20] = descriptor;
b[ELEMENT0 + 21] = UNDEF8;
b[ELEMENT0 + 22] = UNDEF8;
b[ELEMENT0 + 23] = bits;
put16(&mut b, ELEMENT0 + 24, packing);
put32(&mut b, ELEMENT0 + 28, HEADER_LEN as u32);
b[FRAME_POSITION..1732].fill(0xFF);
b[TIME_CODE..1972].fill(0xFF);
b
}
fn words(o: Order, ws: &[u32]) -> Vec<u8> {
ws.iter().flat_map(|w| if o == Order::Big { w.to_be_bytes() } else { w.to_le_bytes() }).collect()
}
#[test]
fn ten_bit_method_a_and_b_unpack_three_to_a_word() {
for o in [Order::Big, Order::Little] {
for (packing, base) in [(1u16, 22u32), (2, 20)] {
let mut b = header(o, 1, 1, 50, 10, packing);
b.extend(words(o, &[(1023 << base) | (512 << (base - 10)) | (1 << (base - 20))]));
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.to_u16_samples().unwrap(), vec![65535, widen(512, 1023), widen(1, 1023)], "{o:?} packing {packing}");
}
}
}
#[test]
fn ten_bit_packing_0_is_one_bit_stream_per_line() {
// Four luma samples: 40 bits over two words, the line padded to the second word.
let samples = [1u64, 2, 1022, 1023];
let stream = samples.iter().fold(0u64, |acc, &s| (acc << 10) | s) << 24;
for o in [Order::Big, Order::Little] {
let mut b = header(o, 4, 2, 6, 10, 0);
let line = words(o, &[(stream >> 32) as u32, stream as u32]);
b.extend(&line);
b.extend(&line);
let img = decode(&b, &Limits::default()).unwrap();
let want: Vec<u16> = samples.iter().map(|&s| widen(s as u16, 1023)).collect();
assert_eq!(img.to_u16_samples().unwrap(), [want.clone(), want].concat(), "{o:?}");
}
}
#[test]
fn twelve_bit_method_a_keeps_the_top_bits() {
let mut b = header(Order::Big, 2, 1, 6, 12, 1);
b.extend_from_slice(&[0xFF, 0xF0, 0x00, 0x10]);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.to_u16_samples().unwrap(), vec![65535, widen(1, 4095)]);
}
#[test]
fn widen_and_narrow_round_trip_every_code() {
for bits in [10u8, 12] {
let max = (1u64 << bits) - 1;
for c in 0..=max as u16 {
assert_eq!(narrow(widen(c, max), max), c, "{bits}-bit {c}");
}
}
assert_eq!(widen(512, 1023), ((512.0f64 * 65535.0 / 1023.0).round()) as u16);
}
#[test]
fn orientation_codes_map_to_exif() {
let mut b = header(Order::Big, 1, 1, 6, 8, 0);
for (dpx, exif) in [(0u16, 1u16), (1, 2), (2, 4), (3, 3), (4, 5), (5, 6), (6, 8), (7, 7), (0xFFFF, 1)] {
b[ORIENTATION..ORIENTATION + 2].copy_from_slice(&dpx.to_be_bytes());
assert_eq!(orientation(&b), exif, "DPX {dpx}");
}
}
#[test]
fn time_codes_round_trip() {
assert_eq!(format_time_code(0x0102_0304), "01:02:03:04");
assert_eq!(parse_time_code("01:02:03:04"), Some(0x0102_0304));
assert_eq!(parse_time_code(&format_time_code(0xABCD_0000)), Some(0xABCD_0000));
assert_eq!(parse_time_code("1:2:3:4"), None);
}
#[test]
fn eight_bit_lines_without_word_padding_still_open() {
// 1 × 2 RGB: 3 bytes a line, 6 in all; padded lines would need 8.
let mut b = header(Order::Big, 1, 2, 50, 8, 0);
b.extend_from_slice(&[1, 2, 3, 4, 5, 6]);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.data(), &[1, 2, 3, 4, 5, 6]);
}
/// A minimal Cineon: a 1024-byte generic header, then `w × h` 10-bit samples packed three
/// to a 32-bit word (RGB in word order, or gray three pixels per word).
fn cineon(w: u32, h: u32, channels: u8, px: &[[u16; 3]]) -> Vec<u8> {
let mut b = vec![0u8; 1024];
b[0..4].copy_from_slice(&[0x80, 0x2A, 0x5F, 0xD7]); // magic (802A5FD7)
b[4..8].copy_from_slice(&1024u32.to_be_bytes()); // image data offset
b[192] = 0; // orientation: left-to-right, top-to-bottom
b[193] = channels;
b[198] = 10; // bits per pixel (channel 1)
b[200..204].copy_from_slice(&w.to_be_bytes()); // pixels per line
b[204..208].copy_from_slice(&h.to_be_bytes()); // lines per image
let word = |a: u16, c: u16, e: u16| ((u32::from(a) << 22) | (u32::from(c) << 12) | (u32::from(e) << 2)).to_be_bytes();
if channels == 3 {
for p in px {
b.extend_from_slice(&word(p[0], p[1], p[2]));
}
} else {
let g = |i: usize| px.get(i).map_or(0, |p| p[0]);
for i in (0..px.len()).step_by(3) {
b.extend_from_slice(&word(g(i), g(i + 1), g(i + 2)));
}
}
b
}
#[test]
fn cineon_reads_ten_bit_rgb() {
let px = [[0, 512, 1023], [1023, 0, 256]];
let b = cineon(2, 1, 3, &px);
assert!(is_cineon(&b));
assert_eq!(order(&b), None, "Cineon is not DPX");
let img = decode_cineon(&b, &Limits::default()).unwrap();
assert_eq!((img.width(), img.height(), img.layout()), (2, 1, ChannelLayout::Rgb));
assert_eq!(
img.to_u16_samples().unwrap(),
vec![widen(0, 1023), widen(512, 1023), widen(1023, 1023), widen(1023, 1023), widen(0, 1023), widen(256, 1023)]
);
}
#[test]
fn cineon_reads_grayscale_and_rejects_other_depths() {
let px = [[10, 0, 0], [20, 0, 0], [30, 0, 0], [40, 0, 0]];
let b = cineon(4, 1, 1, &px);
let img = decode_cineon(&b, &Limits::default()).unwrap();
assert_eq!(img.layout(), ChannelLayout::Gray);
assert_eq!(img.to_u16_samples().unwrap(), vec![widen(10, 1023), widen(20, 1023), widen(30, 1023), widen(40, 1023)]);
// A depth other than 10 is unsupported, and truncation errors rather than panicking.
let mut bad = cineon(1, 1, 3, &[[0, 0, 0]]);
bad[198] = 8;
assert!(matches!(decode_cineon(&bad, &Limits::default()), Err(CodecError::Unsupported { .. })));
let full = cineon(4, 4, 3, &[[0; 3]; 16]);
for cut in [0, 4, 200, 1024, full.len() - 1] {
assert!(decode_cineon(&full[..cut.min(full.len())], &Limits::default()).is_err(), "cut {cut}");
}
}
let code = |key| keep(key).and_then(|v| v.parse::<u8>().ok());
let meta = craft_dpx::Metadata {
creator: Some(keep(KEY_CREATOR).unwrap_or("PhotoCraft")),
project: keep(KEY_PROJECT),
copyright: keep(KEY_COPYRIGHT),
creation_time: keep(KEY_CREATION_TIME),
transfer: code(KEY_TRANSFER),
colorimetric: code(KEY_COLORIMETRIC),
frame_position: keep(KEY_FRAME_POSITION).and_then(|v| v.parse::<u32>().ok()),
frame_rate: keep(KEY_FRAME_RATE).and_then(|v| v.parse::<f32>().ok()),
time_code: keep(KEY_TIME_CODE).and_then(craft_dpx::parse_time_code),
};
let (width, height) = img.dimensions();
craft_dpx::encode(&craft_dpx::Picture { width, height, layout, bits, codes: &codes }, &meta).map_err(|e| err(F, e))
}
+6 -1
View File
@@ -296,9 +296,14 @@ pub(crate) fn decode_channels(bytes: &[u8], index: usize, names: &[&str], limits
let (w, h) = (layer.size.0, layer.size.1);
let channels = &layer.channel_data.list;
let npx = w * h;
// More names than channels can only repeat one: refuse before the per-name lookups multiply.
if names.len() > channels.len() {
return Err(err("more channel names than the part has channels"));
}
let have: Vec<String> = channels.iter().map(|c| c.name.to_string()).collect();
let mut picked: Vec<&AnyChannel<FlatSamples>> = Vec::with_capacity(names.len());
for name in names {
let c = channels.iter().find(|c| c.name.to_string() == *name).ok_or_else(|| err(format!("the part has no channel {name}")))?;
let c = have.iter().position(|n| n == name).map(|i| &channels[i]).ok_or_else(|| err(format!("the part has no channel {name}")))?;
if c.sample_data.len() != npx {
return Err(err("channel sample count mismatch (subsampled channels are unsupported)"));
}
+76 -128
View File
@@ -20,84 +20,23 @@ use crate::options::Limits;
const F: Format = Format::OpenExr;
/// A Cryptomatte stream channel (`crypto_asset00.red`) splits into its stream name
/// (`crypto_asset00`), the stream index, whether it is a coverage channel (green/alpha)
/// instead of an ID (red/blue), and its rank within the set (0 for red/green, 1 for
/// blue/alpha).
pub(crate) struct StreamChannel {
pub stream: String,
pub index: u32,
pub coverage: bool,
pub rank: usize,
}
/// `<name>NN` where NN is one or more digits (the reference readers match `\d+`), with a
/// `.red`/`.r` or `.blue`/`.b` (ID) or `.green`/`.g` or `.alpha`/`.a` (coverage) suffix,
/// in any case (Blender writes `.R`/`.G`/`.B`/`.A`).
pub(crate) fn split_stream(channel: &str) -> Option<StreamChannel> {
let (stream, suffix) = channel.rsplit_once('.')?;
let (coverage, rank) = match suffix.to_ascii_lowercase().as_str() {
"red" | "r" => (false, 0),
"green" | "g" => (true, 0),
"blue" | "b" => (false, 1),
"alpha" | "a" => (true, 1),
_ => return None,
};
let digits = stream.chars().rev().take_while(|c| c.is_ascii_digit()).count();
if digits == 0 || digits == stream.len() {
return None;
}
let split = stream.len() - digits;
let index = stream[split..].parse().ok()?;
Some(StreamChannel { stream: stream.to_string(), index, coverage, rank })
}
/// MurmurHash3, x86-32 bit variant, seed 0 (public-domain algorithm, Austin Appleby).
fn murmur3_32(data: &[u8]) -> u32 {
const C1: u32 = 0xcc9e_2d51;
const C2: u32 = 0x1b87_3593;
let mut h = 0u32;
let (chunks, rest) = data.as_chunks::<4>();
for c in chunks {
let mut k = u32::from_le_bytes(*c);
k = k.wrapping_mul(C1).rotate_left(15).wrapping_mul(C2);
h ^= k;
h = h.rotate_left(13).wrapping_mul(5).wrapping_add(0xe654_6b64);
}
if !rest.is_empty() {
let mut k = [0u8; 4];
k[..rest.len()].copy_from_slice(rest);
let mut k = u32::from_le_bytes(k);
k = k.wrapping_mul(C1).rotate_left(15).wrapping_mul(C2);
h ^= k;
}
h ^= data.len() as u32;
h ^= h >> 16;
h = h.wrapping_mul(0x85eb_ca6b);
h ^= h >> 13;
h = h.wrapping_mul(0xc2b2_ae35);
h ^ (h >> 16)
}
pub(crate) use craft_exr::cryptomatte::split_stream;
/// The Cryptomatte ID of a name: the MurmurHash3 bits with the exponent fixed away from
/// 0 and 255, as an f32 value.
pub fn cryptomatte_id(name: &str) -> f32 {
let bits = murmur3_32(name.as_bytes());
let exp = (bits >> 23) & 0xff;
let bits = if exp == 0 || exp == 255 { bits ^ (1 << 23) } else { bits };
f32::from_bits(bits)
craft_exr::cryptomatte::id(name)
}
/// The 7-hex-digit metadata key of a layer name (the ID bits without their last digit).
pub fn cryptomatte_key(name: &str) -> String {
format!("{:08x}", cryptomatte_id(name).to_bits())[..7].to_string()
craft_exr::cryptomatte::key(name)
}
/// The preview colour of an ID (the reference formula): green and blue channels shifted
/// out of the ID's bit pattern, red unused.
pub fn cryptomatte_preview_color(id: f32) -> [f32; 3] {
let bits = id.to_bits();
[0.0, f32::from_bits(bits << 8), f32::from_bits(bits << 16)]
craft_exr::cryptomatte::preview_color(id)
}
/// The `cryptomatte/<key>/<field>` text attribute of a header, if present.
@@ -109,40 +48,39 @@ fn attribute(other: &std::collections::HashMap<exr::meta::attribute::Text, Attri
}
}
/// Parse a manifest JSON (`{"object name": "<8-hex ID bits>"}`) into (ID value, name)
/// pairs. Entries that are not strings of up to eight hex digits are skipped, like the
/// reference readers tolerate a partially broken manifest; a broken document yields none.
/// A manifest JSON (`{"object name": "<8-hex ID bits>"}`) as (ID value, name) pairs, by name.
/// Entries that are not hex strings are skipped; a broken document yields none.
fn parse_manifest(json: &str) -> Vec<(f32, String)> {
let Ok(value) = serde_json::from_str::<serde_json::Value>(json) else { return Vec::new() };
let Some(map) = value.as_object() else { return Vec::new() };
let mut out = Vec::with_capacity(map.len().min(1 << 20));
for (name, hex) in map {
let Some(hex) = hex.as_str() else { continue };
let Some(bits) = u32::from_str_radix(hex.trim_start_matches("0x"), 16).ok() else { continue };
out.push((f32::from_bits(bits), name.clone()));
}
out
craft_exr::cryptomatte::parse_manifest(json).into_iter().map(|(name, bits)| (f32::from_bits(bits), name)).collect()
}
/// The stream channel names of a layer (`crypto_asset00`, `crypto_asset01`, …, exactly as
/// the file spells them), matched leniently: the channel prefix and the `name` attribute
/// differ in case or `_`/`-` between renderers (`CryptoAsset` vs `crypto_asset`).
fn layer_streams(header: &exr::meta::header::Header, name: &str) -> Vec<String> {
let normalize = |s: &str| s.to_ascii_lowercase().replace(['_', '-'], "");
let want = normalize(name);
let mut found: Vec<(u32, String)> = header
.channels
.list
.iter()
.filter_map(|c| {
let s = split_stream(&c.name.to_string())?;
(!s.coverage).then_some((s.index, s.stream))
/// How a layer name compares with a stream's channel prefix: the channel prefix and the `name`
/// attribute differ in case or `_`/`-` between renderers (`CryptoAsset` vs `crypto_asset`).
fn normalize_layer(s: &str) -> String {
s.to_ascii_lowercase().replace(['_', '-'], "")
}
/// The stream channel names of every layer of a header (`crypto_asset00`, `crypto_asset01`, …,
/// exactly as the file spells them), keyed by [`normalize_layer`] of their prefix. Built in one
/// pass over the channels, so looking up each `cryptomatte/<key>/name` costs no channel scan.
fn layer_streams(header: &exr::meta::header::Header) -> std::collections::HashMap<String, Vec<String>> {
let mut found: std::collections::HashMap<String, Vec<(u32, String)>> = std::collections::HashMap::new();
for c in &header.channels.list {
let Some(s) = split_stream(&c.name.to_string()) else { continue };
if s.coverage {
continue;
}
let layer = normalize_layer(s.stream.trim_end_matches(|c: char| c.is_ascii_digit()));
found.entry(layer).or_default().push((s.index, s.stream));
}
found
.into_iter()
.map(|(layer, mut streams)| {
streams.sort();
streams.dedup();
(layer, streams.into_iter().map(|(_, stream)| stream).collect())
})
.filter(|(_, stream)| normalize(stream.trim_end_matches(|c: char| c.is_ascii_digit())) == want)
.collect();
found.sort();
found.dedup();
found.into_iter().map(|(_, stream)| stream).collect()
.collect()
}
/// Lists every Cryptomatte layer of the file from the headers alone: what a Cryptomatte
@@ -157,50 +95,55 @@ pub(crate) fn layers(bytes: &[u8], limits: &Limits) -> Result<Vec<CryptomatteLay
let size = header.layer_size;
let (w, h) = (u32::try_from(size.0).unwrap_or(u32::MAX), u32::try_from(size.1).unwrap_or(u32::MAX));
limits.check_bytes(w, h, 8)?;
// Cache stream lists by resolved layer name: layer_streams() rescans every channel,
// and distinct file-controlled keys can all name the same layer, which would turn
// channel discovery into O(keys * channels) work and allocate a fresh list each time.
let mut streams_for: std::collections::HashMap<String, Vec<String>> = std::collections::HashMap::new();
for key in cryptomatte_keys(&header.own_attributes.other) {
let Some(name) = attribute(&header.own_attributes.other, &key, "name") else { continue };
let channels = streams_for.entry(name.clone()).or_insert_with(|| layer_streams(header, name.as_str())).clone();
if channels.is_empty() {
continue;
}
let manifest = attribute(&header.own_attributes.other, &key, "manifest")
.filter(|m| m.len() <= MANIFEST_CAP)
.map(|m| parse_manifest(m.as_str()))
.unwrap_or_default();
// Every channel's stream, indexed once by layer. Each layer's list is moved into the
// first key that names it: file-controlled keys can all name the same layer (in any
// case or `_`/`-` spelling), and copying its list for each of them would make the
// result grow with keys * channels. A second key naming a listed layer is skipped;
// `decode` resolves a name to its first layer anyway.
let mut streams_for = layer_streams(header);
// A key whose manifest is missing or unparseable only stands in for its layer when no later
// key naming it has a usable one.
let mut fallback: std::collections::HashMap<String, (String, String)> = std::collections::HashMap::new();
let other = &header.own_attributes.other;
let mut push = |name: String, key: String, manifest: Vec<_>, channels: Vec<String>| {
out.push(CryptomatteLayer {
part,
name,
key: key.clone(),
conversion: attribute(&header.own_attributes.other, &key, "conversion"),
conversion: attribute(other, &key, "conversion"),
manifest,
sidecar_manifest: attribute(&header.own_attributes.other, &key, "manif_file"),
sidecar_manifest: attribute(other, &key, "manif_file"),
key,
channels,
});
};
for key in cryptomatte_keys(other) {
let Some(name) = attribute(other, &key, "name") else { continue };
let norm = normalize_layer(&name);
if streams_for.get(&norm).is_none_or(|c| c.is_empty()) {
continue;
}
let manifest = attribute(other, &key, "manifest").filter(|m| m.len() <= MANIFEST_CAP).map(|m| parse_manifest(m.as_str())).unwrap_or_default();
if manifest.is_empty() {
fallback.entry(norm).or_insert((name, key));
} else if let Some(channels) = streams_for.remove(&norm) {
push(name, key, manifest, channels);
}
}
for (norm, (name, key)) in fallback {
if let Some(channels) = streams_for.remove(&norm) {
push(name, key, Vec::new(), channels);
}
}
}
out.sort_by(|a, b| (&a.part, &a.name).cmp(&(&b.part, &b.name)));
Ok(out)
}
/// Every `cryptomatte/<key>/` key of a header, in file order, without duplicates.
/// Every `cryptomatte/<key>/` key of a header, sorted, without duplicates.
fn cryptomatte_keys(other: &std::collections::HashMap<exr::meta::attribute::Text, exr::meta::attribute::AttributeValue>) -> Vec<String> {
let mut keys = Vec::new();
// Track seen keys in a set: the attribute names are file-controlled, so membership with
// keys.iter().any() is O(n^2) in the number of distinct `cryptomatte/<key>/<field>`
// groups. The Vec still records file order; the set only answers "seen before".
let mut seen = std::collections::HashSet::new();
for name in other.keys() {
let full = name.to_string();
let Some(rest) = full.strip_prefix("cryptomatte/") else { continue };
let Some((key, _field)) = rest.split_once('/') else { continue };
if !key.is_empty() && seen.insert(key.to_string()) {
keys.push(key.to_string());
}
}
let names: Vec<String> = other.keys().map(|name| name.to_string()).collect();
let mut keys = craft_exr::cryptomatte::keys(names.iter().map(String::as_str));
keys.sort(); // HashMap order varies per call; layer names must stay stable
keys
}
@@ -210,9 +153,11 @@ fn cryptomatte_keys(other: &std::collections::HashMap<exr::meta::attribute::Text
/// other flat part decoders.
pub(crate) fn decode(bytes: &[u8], layer_name: &str, limits: &Limits) -> Result<CryptomatteBuffer, CodecError> {
let found = layers(bytes, limits)?;
// An exact name wins, so layers whose names only differ after normalising can each be asked for.
let layer = found
.iter()
.find(|l| l.name.eq_ignore_ascii_case(layer_name))
.find(|l| l.name == layer_name)
.or_else(|| found.iter().find(|l| normalize_layer(&l.name) == normalize_layer(layer_name)))
.ok_or_else(|| CodecError::unsupported(F, format!("no Cryptomatte layer named {layer_name}")))?;
// Also caps all parts' declared sizes together, like every other flat decode.
let meta = super::exr::read_meta(bytes, limits)?;
@@ -240,9 +185,12 @@ pub(crate) fn decode(bytes: &[u8], layer_name: &str, limits: &Limits) -> Result<
// Two (ID, coverage) ranks per set: rank `slot * 2 + stream.rank`.
let mut ids: Vec<Vec<f32>> = vec![Vec::new(); ranks];
let mut coverages: Vec<Vec<f32>> = vec![Vec::new(); ranks];
// Stream name -> its slot, built once: a linear search per decoded channel is quadratic in
// the file's stream count.
let slots: std::collections::HashMap<&str, usize> = layer.channels.iter().enumerate().rev().map(|(i, s)| (s.as_str(), i)).collect();
for c in &ex_layer.channel_data.list {
let Some(stream) = split_stream(&c.name.to_string()) else { continue };
let Some(slot) = layer.channels.iter().position(|s| *s == stream.stream) else { continue };
let Some(&slot) = slots.get(stream.stream.as_str()) else { continue };
let values: Vec<f32> = c.sample_data.values_as_f32().collect();
if values.len() != npx {
return Err(CodecError::malformed(F, "a Cryptomatte channel is smaller than its layer"));
-855
View File
@@ -1,855 +0,0 @@
//! FITS (Flexible Image Transport System, FITS Standard 4.0), implemented here: the image in the
//! primary HDU, or the first `IMAGE` extension when the primary HDU holds no data.
//!
//! Reading:
//!
//! * `BITPIX` 8, 16, 32, 64, -32 and -64, with `BSCALE`/`BZERO` and integer `BLANK`.
//! * `NAXIS3 = 3` opens as RGB (the planes are R, G, B); any other plane count opens the first
//! plane as gray with [`DecodeWarning::MorePages`].
//! * Rows are stored bottom-to-top, as the standard's convention has it (Siril, DS9, Astropy),
//! unless `ROWORDER = 'TOP-DOWN'`.
//! * Physical values that fit (unsigned 8-bit, or integers in 0..=65535 such as a camera's
//! `BITPIX = 16`, `BZERO = 32768`) are kept exactly as U8/U16. Everything else becomes F32:
//! values up to 2 are taken as already normalised (Siril, PixInsight), larger ones are scaled by
//! 65535 when they fit in it (DeepSkyStacker, most stackers) and by the maximum otherwise. NaN
//! and blank pixels read as 0.
//! * The other header cards are kept as text: `KEY` → value (strings unquoted), `COMMENT` and
//! `HISTORY` by their text, `HIERARCH` keywords by their long name. Long strings continued
//! on `CONTINUE` cards are joined.
//!
//! Writing: gray or RGB, U8 (`BITPIX = 8`), U16 (`BITPIX = 16`, `BZERO = 32768`) or F32
//! (`BITPIX = -32`, 0..1), bottom-to-top with `ROWORDER = 'BOTTOM-UP'`, plus the text cards
//! (strings longer than one card on `CONTINUE` cards).
//! Tile-compressed files (fpack `ZIMAGE` tables) are recognised and refused.
use std::collections::HashMap;
use crate::Format;
use crate::error::CodecError;
use crate::fidelity::Plan;
use crate::image::{ChannelLayout, DecodeWarning, Image, SampleType};
use crate::options::{EncodeOptions, Limits};
const F: Format = Format::Fits;
const BLOCK: usize = 2880;
const CARD: usize = 80;
/// Most HDUs walked looking for an image; FITS files from cameras and stackers hold a handful.
const MAX_HDUS: usize = 1000;
/// Keywords that describe the data layout. They are written from the image, never copied from
/// (or kept as) text metadata: after decoding they no longer describe the pixels.
const STRUCTURAL: &[&str] = &[
"SIMPLE", "BITPIX", "NAXIS", "EXTEND", "BZERO", "BSCALE", "BLANK", "END", "XTENSION", "PCOUNT", "GCOUNT", "ROWORDER", "CHECKSUM", "DATASUM", "DATAMIN",
"DATAMAX", "ZIMAGE",
];
fn err(msg: impl std::fmt::Display) -> CodecError {
CodecError::malformed(F, msg)
}
#[derive(Debug, Clone, PartialEq)]
enum Value {
/// A quoted string, unescaped, trailing blanks removed.
Str(String),
/// Anything else (number, logical, complex) as written.
Raw(String),
}
#[derive(Debug, Clone)]
struct Card {
key: String,
/// `None` for commentary cards (`COMMENT`, `HISTORY`, blank keyword).
value: Option<Value>,
/// The text of a commentary card.
text: String,
/// A `HIERARCH` keyword (`key` holds its long name).
hierarch: bool,
}
fn parse_value(field: &str) -> Value {
let t = field.trim_start();
if let Some(rest) = t.strip_prefix('\'') {
let mut s = String::new();
let mut chars = rest.chars().peekable();
while let Some(c) = chars.next() {
if c == '\'' {
if chars.peek() == Some(&'\'') {
chars.next();
s.push('\'');
} else {
break;
}
} else {
s.push(c);
}
}
Value::Str(s.trim_end().to_string())
} else {
Value::Raw(t.split('/').next().unwrap_or("").trim().to_string())
}
}
fn parse_card(raw: &[u8]) -> Card {
// Header bytes are ASCII 32–126 by the standard; anything else is replaced, never trusted.
let s: String = raw.iter().map(|&b| if (32..=126).contains(&b) { b as char } else { '?' }).collect();
let key_field = s.get(..8).unwrap_or(&s).trim_end().to_string();
if key_field == "HIERARCH" {
let rest = s.get(9..).unwrap_or("");
if let Some((k, v)) = rest.split_once('=') {
return Card { key: k.trim().to_string(), value: Some(parse_value(v)), text: String::new(), hierarch: true };
}
}
if s.get(8..10) == Some("= ") {
let v = parse_value(s.get(10..).unwrap_or(""));
return Card { key: key_field, value: Some(v), text: String::new(), hierarch: false };
}
Card { key: key_field, value: None, text: s.get(8..).unwrap_or("").trim_end().to_string(), hierarch: false }
}
struct Header {
cards: Vec<Card>,
/// First-occurrence index of each non-HIERARCH keyword, so `get` is O(1). The card
/// count and the number of looked-up keywords (NAXIS1..NAXISn) are both file-controlled,
/// so a linear scan per lookup lets a crafted header multiply commentary cards by axis
/// count into quadratic work.
index: HashMap<String, usize>,
/// Offset of the data unit.
data_start: usize,
}
impl Header {
fn get(&self, key: &str) -> Option<&Value> {
let c = &self.cards[*self.index.get(key)?];
c.value.as_ref()
}
fn int(&self, key: &str) -> Option<i64> {
match self.get(key)? {
Value::Raw(r) => r.parse::<i64>().ok().or_else(|| self.float(key).filter(|f| f.fract() == 0.0 && f.abs() < 9.0e15).map(|f| f as i64)),
Value::Str(_) => None,
}
}
fn float(&self, key: &str) -> Option<f64> {
match self.get(key)? {
// Fortran-style `D` exponents are allowed in FITS.
Value::Raw(r) => r.replace(['D', 'd'], "E").parse::<f64>().ok().filter(|f| f.is_finite()),
Value::Str(_) => None,
}
}
fn logical(&self, key: &str) -> bool {
matches!(self.get(key), Some(Value::Raw(r)) if r == "T")
}
fn string(&self, key: &str) -> Option<&str> {
match self.get(key)? {
Value::Str(s) => Some(s.trim()),
Value::Raw(_) => None,
}
}
}
fn parse_header(b: &[u8], start: usize) -> Result<Header, CodecError> {
let mut cards = Vec::new();
let mut pos = start;
loop {
let raw = b.get(pos..pos.checked_add(CARD).ok_or_else(|| err("header too long"))?).ok_or_else(|| err("header ends before END"))?;
pos += CARD;
if raw.starts_with(b"END") && raw.get(3..).is_some_and(|r| r.iter().all(|&c| c == b' ')) {
break;
}
let card = parse_card(raw);
// Long strings (FITS 4.0 section 4.2.1.2): a string ending in '&' goes on in the next
// `CONTINUE` record. An orphaned `CONTINUE` stays a card of its own (commentary).
if let Some(Card { value: Some(Value::Str(s)), .. }) = cards.last_mut()
&& s.ends_with('&')
&& card.key == "CONTINUE"
&& card.text.starts_with(" ")
&& let Value::Str(more) = parse_value(&card.text)
{
s.pop();
s.push_str(&more);
continue;
}
cards.push(card);
}
let data_start = pos.div_ceil(BLOCK).checked_mul(BLOCK).ok_or_else(|| err("header too long"))?;
// Index each non-HIERARCH keyword to its first occurrence, matching the previous
// `find(|c| !c.hierarch && c.key == key)` lookup while making `get` O(1).
let mut index = HashMap::with_capacity(cards.len());
for (i, c) in cards.iter().enumerate() {
if !c.hierarch {
index.entry(c.key.clone()).or_insert(i);
}
}
Ok(Header { cards, index, data_start })
}
/// The axis lengths (`NAXIS1`, `NAXIS2`, …).
fn axes(h: &Header) -> Result<Vec<u64>, CodecError> {
let n = h.int("NAXIS").ok_or_else(|| err("missing NAXIS"))?;
if !(0..=999).contains(&n) {
return Err(err(format!("invalid NAXIS {n}")));
}
(1..=n).map(|i| h.int(&format!("NAXIS{i}")).and_then(|v| u64::try_from(v).ok()).ok_or_else(|| err(format!("missing or negative NAXIS{i}")))).collect()
}
/// Bytes in the data unit (before padding to a block).
fn data_len(h: &Header, bitpix: i64) -> Result<u64, CodecError> {
let ax = axes(h)?;
if ax.is_empty() {
return Ok(0);
}
let pcount = u64::try_from(h.int("PCOUNT").unwrap_or(0)).map_err(|_| err("negative PCOUNT"))?;
let gcount = u64::try_from(h.int("GCOUNT").unwrap_or(1)).map_err(|_| err("negative GCOUNT"))?;
let elems = ax.iter().try_fold(1u64, |a, &n| a.checked_mul(n)).ok_or_else(|| err("data size overflows"))?;
elems
.checked_add(pcount)
.and_then(|v| v.checked_mul(gcount))
.and_then(|v| v.checked_mul(bitpix.unsigned_abs() / 8))
.ok_or_else(|| err("data size overflows"))
}
/// Inspect every HDU without decoding pixels: one gray plane or three RGB planes are retained,
/// while other cubes, image extensions, tables and other data-bearing HDUs cannot survive a flat save.
/// Incomplete or over-limit walks fail closed rather than treating an unchecked suffix as safe.
pub(crate) fn has_additional_images(bytes: &[u8]) -> Result<bool, CodecError> {
let mut start = 0;
let mut image_seen = false;
for index in 0..MAX_HDUS {
let h = parse_header(bytes, start)?;
if index == 0 && h.get("SIMPLE").is_none() {
return Err(err("missing SIMPLE"));
}
let ax = axes(&h)?;
let size = data_len(&h, h.int("BITPIX").unwrap_or(8))?;
let image_hdu = index == 0 || h.string("XTENSION") == Some("IMAGE");
if h.logical("ZIMAGE") {
return Ok(true);
}
if !image_hdu && size > 0 {
return Ok(true);
}
// An image HDU with a zero axis has no data (FITS 4.0, NAXISn) and holds no image, like
// the empty primary: `find_image` skips it too.
if image_hdu && size > 0 && ax.len() >= 2 && ax[..2].iter().all(|n| *n > 0) {
let planes = ax.iter().skip(2).try_fold(1u64, |n, axis| n.checked_mul(*axis)).ok_or_else(|| err("plane count overflows"))?;
let retained = planes == 1 || (ax.get(2) == Some(&3) && planes == 3);
if image_seen || !retained || h.int("GCOUNT").unwrap_or(1) > 1 || h.int("PCOUNT").unwrap_or(0) > 0 {
return Ok(true);
}
image_seen = true;
} else if size > 0 {
return Ok(true);
}
let padded = size.div_ceil(BLOCK as u64).checked_mul(BLOCK as u64).ok_or_else(|| err("data size overflows"))?;
let next = usize::try_from(padded).ok().and_then(|size| h.data_start.checked_add(size)).ok_or_else(|| err("data size overflows"))?;
if next > bytes.len() {
return Err(err("truncated HDU data"));
}
if next == bytes.len() {
return Ok(false);
}
start = next;
}
Err(err("too many HDUs to inspect every source image"))
}
/// Whether an HDU with these axes stores pixels: at least two axes and none of them zero (FITS 4.0,
/// NAXISn: a zero axis means no data follow the header).
fn holds_pixels(ax: &[u64]) -> bool {
ax.len() >= 2 && ax.iter().all(|n| *n > 0)
}
/// Finds the image HDU: the primary one when it holds pixels, else the first `IMAGE` extension
/// that does. Returns its header and the primary header (for its descriptive cards).
fn find_image(b: &[u8]) -> Result<(Header, Option<Header>), CodecError> {
let primary = parse_header(b, 0)?;
if primary.get("SIMPLE").is_none() {
return Err(err("missing SIMPLE"));
}
if holds_pixels(&axes(&primary)?) {
return Ok((primary, None));
}
let mut compressed = false;
let mut cur_start = primary.data_start;
let mut cur_len = data_len(&primary, primary.int("BITPIX").unwrap_or(8))?;
for _ in 0..MAX_HDUS {
let next = usize::try_from(cur_len.div_ceil(BLOCK as u64).saturating_mul(BLOCK as u64))
.ok()
.and_then(|l| cur_start.checked_add(l))
.ok_or_else(|| err("data size overflows"))?;
if next >= b.len() {
break;
}
let h = parse_header(b, next)?;
let kind = h.string("XTENSION").unwrap_or("").to_string();
if kind == "IMAGE" && holds_pixels(&axes(&h)?) {
return Ok((h, Some(primary)));
}
if kind == "BINTABLE" && h.logical("ZIMAGE") {
compressed = true;
}
cur_len = data_len(&h, h.int("BITPIX").unwrap_or(8))?;
cur_start = h.data_start;
}
if compressed {
return Err(CodecError::unsupported(F, "tile-compressed FITS (fpack) is not supported; decompress it with funpack first"));
}
Err(err("the file holds no image"))
}
pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
let (h, primary) = find_image(bytes)?;
let bitpix = h.int("BITPIX").ok_or_else(|| err("missing BITPIX"))?;
if !matches!(bitpix, 8 | 16 | 32 | 64 | -32 | -64) {
return Err(err(format!("invalid BITPIX {bitpix}")));
}
let ax = axes(&h)?;
let (Some(&w), Some(&hgt)) = (ax.first(), ax.get(1)) else { return Err(err("the image has fewer than two axes")) };
let w = u32::try_from(w).map_err(|_| CodecError::LimitExceeded(format!("width {w}")))?;
let hgt = u32::try_from(hgt).map_err(|_| CodecError::LimitExceeded(format!("height {hgt}")))?;
let planes = ax.iter().skip(2).try_fold(1u64, |a, &n| a.checked_mul(n)).ok_or_else(|| err("too many planes"))?;
let (layout, nplanes) = if ax.get(2) == Some(&3) && planes == 3 { (ChannelLayout::Rgb, 3usize) } else { (ChannelLayout::Gray, 1) };
let bscale = h.float("BSCALE").unwrap_or(1.0);
let bzero = h.float("BZERO").unwrap_or(0.0);
let blank = if bitpix > 0 { h.int("BLANK") } else { None };
let integral = bscale == 1.0 && bzero.fract() == 0.0;
let candidate = match bitpix {
8 if integral && bzero == 0.0 => SampleType::U8,
8 | 16 if integral => SampleType::U16,
_ => SampleType::F32,
};
limits.check(w, hgt, layout, candidate)?;
let bps = (bitpix.unsigned_abs() / 8) as usize;
let plane_len = (w as usize).checked_mul(hgt as usize).ok_or_else(|| CodecError::LimitExceeded(format!("{w}x{hgt} does not fit in memory")))?;
let n = plane_len.checked_mul(nplanes).ok_or_else(|| CodecError::LimitExceeded("image does not fit in memory".into()))?;
let need = n.checked_mul(bps).and_then(|l| l.checked_add(h.data_start)).ok_or_else(|| err("data size overflows"))?;
let data = bytes.get(h.data_start..need).ok_or_else(|| err("truncated data"))?;
// Physical value of sample `i` (planar order); `None` for a blank or NaN pixel.
let physical = |i: usize| -> Option<f64> {
let s = data.get(i * bps..(i + 1) * bps)?;
let raw = match bitpix {
8 => s.first().map(|&v| v as i64)?,
16 => i16::from_be_bytes([s[0], s[1]]) as i64,
32 => i32::from_be_bytes([s[0], s[1], s[2], s[3]]) as i64,
64 => i64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]]),
-32 => return Some(f32::from_be_bytes([s[0], s[1], s[2], s[3]]) as f64 * bscale + bzero).filter(|v| v.is_finite()),
_ => return Some(f64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]]) * bscale + bzero).filter(|v| v.is_finite()),
};
if blank == Some(raw) {
return None;
}
Some(raw as f64 * bscale + bzero)
};
let top_down = h.string("ROWORDER").is_some_and(|r| r.eq_ignore_ascii_case("TOP-DOWN"));
let (wu, hu) = (w as usize, hgt as usize);
// Planar, bottom-up (by default) → interleaved, top-down.
let dest = |i: usize| -> usize {
let (p, rem) = (i / plane_len, i % plane_len);
let (y, x) = (rem / wu, rem % wu);
let y = if top_down { y } else { hu - 1 - y };
(y * wu + x) * nplanes + p
};
let mut img = match candidate {
SampleType::U8 => {
let mut out = vec![0u8; n];
for i in 0..n {
out[dest(i)] = physical(i).unwrap_or(0.0) as u8;
}
Image::from_u8(w, hgt, layout, out)?
}
_ => {
let mut out16 = if candidate == SampleType::U16 { Some(vec![0u16; n]) } else { None };
if let Some(out) = out16.as_mut() {
for i in 0..n {
let v = physical(i).unwrap_or(0.0);
if !(0.0..=65535.0).contains(&v) {
// Signed data with negative values: fall back to float.
out16 = None;
break;
}
out[dest(i)] = v as u16;
}
}
match out16 {
Some(out) => Image::from_u16(w, hgt, layout, &out)?,
None => {
let max = (0..n).filter_map(physical).fold(0.0f64, f64::max);
let white = if max <= 2.0 {
1.0
} else if max <= 65535.0 {
65535.0
} else {
max
};
let mut out = vec![0f32; n];
for i in 0..n {
out[dest(i)] = (physical(i).unwrap_or(0.0) / white) as f32;
}
Image::from_f32(w, hgt, layout, &out)?
}
}
}
};
if planes > nplanes as u64 {
img.warnings.push(DecodeWarning::MorePages { total: u32::try_from(planes).ok() });
}
let mut text = Vec::new();
for c in primary.iter().flat_map(|p| p.cards.iter()).chain(h.cards.iter()) {
if !c.hierarch && (STRUCTURAL.contains(&c.key.as_str()) || is_naxis(&c.key)) {
continue;
}
let entry = match &c.value {
Some(Value::Str(s) | Value::Raw(s)) => (c.key.clone(), s.clone()),
None if c.key.is_empty() && c.text.trim().is_empty() => continue,
None => (if c.key.is_empty() { "COMMENT".to_string() } else { c.key.clone() }, c.text.trim().to_string()),
};
text.push(entry);
}
img.meta.text = text;
Ok(img)
}
fn is_naxis(key: &str) -> bool {
key.strip_prefix("NAXIS").is_some_and(|d| d.chars().all(|c| c.is_ascii_digit()))
}
/// A standard keyword: 1–8 characters from `A-Z 0-9 _ -`.
fn standard_key(key: &str) -> bool {
(1..=8).contains(&key.len()) && key.bytes().all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'_' || b == b'-')
}
fn ascii(s: &str) -> String {
s.chars().map(|c| if (' '..='~').contains(&c) { c } else { '?' }).collect()
}
/// A FITS literal (number or logical) that can be written unquoted.
fn literal(v: &str) -> bool {
v == "T" || v == "F" || (v.replace(['D', 'd'], "E").parse::<f64>().is_ok_and(|f| f.is_finite()) && v.bytes().all(|b| b"0123456789+-.EeDd".contains(&b)))
}
fn quoted(v: &str, room: usize) -> String {
let mut s = String::from("'");
for c in ascii(v).chars() {
let add = if c == '\'' { 2 } else { 1 };
// Keep room for the closing quote.
if s.len() + add + 1 > room {
break;
}
s.push(c);
if c == '\'' {
s.push('\'');
}
}
// Strings are at least eight characters inside the quotes.
while s.len() < 9 {
s.push(' ');
}
s.push('\'');
s
}
struct Writer {
out: Vec<u8>,
}
impl Writer {
fn card(&mut self, text: &str) {
let mut c: Vec<u8> = ascii(text).into_bytes();
c.truncate(CARD);
c.resize(CARD, b' ');
self.out.extend_from_slice(&c);
}
fn value(&mut self, key: &str, v: &str) {
if literal(v) {
self.card(&format!("{key:<8}= {v:>20}"));
return;
}
// A string too long for one card goes on in `CONTINUE` cards, every piece but the last
// ending in '&' (FITS 4.0 section 4.2.1.2). A doubled quote is never split, and trailing
// blanks (not significant) are dropped so none are kept before an '&'.
let escaped = ascii(v).trim_end().replace('\'', "''");
let mut rest = escaped.as_str();
let mut head = format!("{key:<8}= ");
while rest.len() > CARD - 12 {
let cut = CARD - 13 - rest.get(..CARD - 13).unwrap_or("").bytes().rev().take_while(|&b| b == b'\'').count() % 2;
let (piece, tail) = rest.split_at_checked(cut).unwrap_or((rest, ""));
self.card(&format!("{head}'{piece}&'"));
head = "CONTINUE ".to_string();
rest = tail;
}
self.card(&format!("{head}'{rest:<8}'"));
}
fn commentary(&mut self, key: &str, text: &str) {
let chars: Vec<char> = ascii(text).chars().collect();
if chars.is_empty() {
self.card(key);
}
for chunk in chars.chunks(CARD - 8) {
self.card(&format!("{key:<8}{}", chunk.iter().collect::<String>()));
}
}
fn text(&mut self, key: &str, v: &str) {
let upper = key.to_ascii_uppercase();
if matches!(upper.as_str(), "COMMENT" | "HISTORY") {
self.commentary(&upper, v);
} else if standard_key(key) {
// `CONTINUE` only ever carries a string's continuation, written by `value`.
if !STRUCTURAL.contains(&key) && !is_naxis(key) && key != "CONTINUE" {
self.value(key, v);
}
} else {
// Long or mixed-case names (`Description`, `Software`): the HIERARCH convention,
// when the card fits, else a comment.
let name = ascii(key);
let head = format!("HIERARCH {name} = ");
let fits_key = !name.contains('=') && !name.trim().is_empty();
let body = if literal(v) { v.to_string() } else { quoted(v, CARD.saturating_sub(head.len())) };
if fits_key && head.len() + body.len() <= CARD && (literal(v) || body.ends_with('\'')) && quoted_whole(v, &body) {
self.card(&format!("{head}{body}"));
} else {
self.commentary("COMMENT", &format!("{key} = {v}"));
}
}
}
}
/// `body` holds all of `v` (it was not cut short to fit the card).
fn quoted_whole(v: &str, body: &str) -> bool {
literal(v) || body.trim_matches('\'').trim_end().replace("''", "'") == ascii(v).trim_end()
}
pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
let img = src.converted(plan.layout, plan.sample);
let (w, h) = img.dimensions();
let layout = img.layout();
let nc = match layout {
ChannelLayout::Gray => 1,
ChannelLayout::Rgb => 3,
l => return Err(CodecError::encode(F, format!("FITS cannot store {l:?}"))),
};
let bitpix = match img.sample_type() {
SampleType::U8 => 8,
SampleType::U16 => 16,
SampleType::F32 => -32,
s => return Err(CodecError::encode(F, format!("unsupported sample {s:?}"))),
};
let mut wr = Writer { out: Vec::new() };
wr.value("SIMPLE", "T");
wr.value("BITPIX", &bitpix.to_string());
wr.value("NAXIS", if nc == 3 { "3" } else { "2" });
wr.value("NAXIS1", &w.to_string());
wr.value("NAXIS2", &h.to_string());
if nc == 3 {
wr.value("NAXIS3", "3");
}
if bitpix == 16 {
wr.value("BZERO", "32768");
wr.value("BSCALE", "1");
}
wr.value("ROWORDER", "BOTTOM-UP");
if opts.embed_metadata {
for (k, v) in &img.meta.text {
wr.text(k, v);
}
}
wr.card("END");
let mut out = wr.out;
out.resize(out.len().div_ceil(BLOCK) * BLOCK, b' ');
// Planar, bottom row first.
let (wu, hu) = (w as usize, h as usize);
let idx = |p: usize, y: usize, x: usize| ((hu - 1 - y) * wu + x) * nc + p;
match bitpix {
8 => {
let d = img.data();
for p in 0..nc {
for y in 0..hu {
for x in 0..wu {
out.push(d[idx(p, y, x)]);
}
}
}
}
16 => {
let d = img.to_u16_samples().unwrap_or_default();
for p in 0..nc {
for y in 0..hu {
for x in 0..wu {
out.extend_from_slice(&((d[idx(p, y, x)] as i32 - 32768) as i16).to_be_bytes());
}
}
}
}
_ => {
let d = img.to_f32_samples().unwrap_or_default();
for p in 0..nc {
for y in 0..hu {
for x in 0..wu {
// 0..1 as documented: a value above 2 would make readers (ours too)
// take the data as ADU and scale it by 65535. NaN is written as 0.
let v = d[idx(p, y, x)];
let v = if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) };
out.extend_from_slice(&v.to_be_bytes());
}
}
}
}
}
out.resize(out.len().div_ceil(BLOCK) * BLOCK, 0);
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
fn header(cards: &[&str]) -> Vec<u8> {
let mut w = Writer { out: Vec::new() };
for c in cards {
w.card(c);
}
w.card("END");
let mut out = w.out;
out.resize(out.len().div_ceil(BLOCK) * BLOCK, b' ');
out
}
#[test]
fn continue_strings_decode() {
let mut b = header(&[
"SIMPLE = T",
"BITPIX = 8",
"NAXIS = 2",
"NAXIS1 = 1",
"NAXIS2 = 1",
"OBJECT = 'aaa&'",
"CONTINUE 'bbb&' / a comment",
"CONTINUE '&'",
"CONTINUE ''",
"OBSERVER= 'R&'",
"COMMENT hi",
"CONTINUE 'orphan'",
]);
b.push(0);
let img = decode(&b, &Limits::default()).unwrap();
let text = [("OBJECT", "aaabbb"), ("OBSERVER", "R&"), ("COMMENT", "hi"), ("CONTINUE", "'orphan'")];
assert_eq!(img.meta.text, text.map(|(k, v)| (k.to_string(), v.to_string())));
}
#[test]
fn continue_strings_round_trip() {
for value in ["a".repeat(100), format!("{}' {}", "a".repeat(65), "b".repeat(100)), "'".repeat(100), "a".repeat(68)] {
let mut img = Image::from_u8(1, 1, ChannelLayout::Gray, vec![0]).unwrap();
img.meta.text = vec![("OBJECT".into(), value.clone()), ("CONTINUE".into(), "orphan".into())];
let b = crate::encode(&img, F, &EncodeOptions::default()).unwrap();
assert!(!b.chunks(CARD).any(|c| c.starts_with(b"CONTINUE=")));
let decoded = decode(&b, &Limits::default()).unwrap();
assert_eq!(decoded.meta.text, vec![("OBJECT".into(), value)]);
}
}
#[test]
fn camera_u16_with_bzero_reads_exactly_and_bottom_up() {
let mut b = header(&[
"SIMPLE = T",
"BITPIX = 16",
"NAXIS = 2",
"NAXIS1 = 2",
"NAXIS2 = 2",
"BZERO = 32768",
"OBJECT = 'M 31 '",
"EXPTIME = 300.0 / seconds",
]);
for v in [0u16, 1000, 40000, 65535] {
b.extend_from_slice(&((v as i32 - 32768) as i16).to_be_bytes());
}
b.resize(b.len().div_ceil(BLOCK) * BLOCK, 0);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.sample_type(), SampleType::U16);
// The first stored row is the bottom one.
assert_eq!(img.to_u16_samples().unwrap(), vec![40000, 65535, 0, 1000]);
assert!(img.meta.text.contains(&("OBJECT".into(), "M 31".into())));
assert!(img.meta.text.contains(&("EXPTIME".into(), "300.0".into())));
assert!(!img.meta.text.iter().any(|(k, _)| k == "BZERO" || k == "NAXIS1"));
}
#[test]
fn roworder_top_down_is_honoured() {
let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 2", "ROWORDER= 'TOP-DOWN'"]);
b.extend_from_slice(&[10, 20]);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.data(), &[10, 20]);
}
#[test]
fn stacked_float_in_adu_is_scaled_by_65535() {
let mut b = header(&["SIMPLE = T", "BITPIX = -32", "NAXIS = 2", "NAXIS1 = 2", "NAXIS2 = 1"]);
for v in [65535.0f32, f32::NAN] {
b.extend_from_slice(&v.to_be_bytes());
}
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.to_f32_samples().unwrap(), vec![1.0, 0.0]);
}
#[test]
fn hdr_float_saves_clamped_and_reopens_as_saved() {
// One value above 2 used to make the reader scale everything by 65535 (all black).
let img = Image::from_f32(2, 1, ChannelLayout::Gray, &[0.5, 3.0]).unwrap();
let opts = EncodeOptions::default();
let bytes = encode(&img, crate::fidelity::plan(&img, F, &opts), &opts).unwrap();
let back = decode(&bytes, &Limits::default()).unwrap().to_f32_samples().unwrap();
assert!((back[0] - 0.5).abs() < 1e-6, "{back:?}");
assert!((back[1] - 1.0).abs() < 1e-6, "{back:?}");
}
#[test]
fn signed_negative_data_falls_back_to_float() {
let mut b = header(&["SIMPLE = T", "BITPIX = 16", "NAXIS = 2", "NAXIS1 = 2", "NAXIS2 = 1"]);
for v in [-100i16, 1000] {
b.extend_from_slice(&v.to_be_bytes());
}
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.sample_type(), SampleType::F32);
}
#[test]
fn image_extension_after_empty_primary() {
let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0", "EXTEND = T", "TELESCOP= 'Seestar S50'"]);
b.extend(header(&["XTENSION= 'IMAGE '", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 1", "PCOUNT = 0", "GCOUNT = 1"]));
b.push(42);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.data(), &[42]);
assert!(img.meta.text.contains(&("TELESCOP".into(), "Seestar S50".into())));
}
/// A zero axis (`NAXIS3 = 0`) means the HDU has no data: it is neither the opened image nor
/// an additional one that Batch Save and Close would discard.
#[test]
fn zero_axis_image_hdus_are_empty() {
let empty = |primary: bool| {
let first = if primary { "SIMPLE = T" } else { "XTENSION= 'IMAGE '" };
header(&[first, "BITPIX = 8", "NAXIS = 3", "NAXIS1 = 1", "NAXIS2 = 1", "NAXIS3 = 0", "PCOUNT = 0", "GCOUNT = 1"])
};
let image = || {
let mut b = header(&["XTENSION= 'IMAGE '", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 1", "PCOUNT = 0", "GCOUNT = 1"]);
b.push(42);
b.resize(b.len().div_ceil(BLOCK) * BLOCK, 0);
b
};
let mut after_empty_primary = empty(true);
after_empty_primary.extend(image());
let mut after_empty_extension = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0", "EXTEND = T"]);
after_empty_extension.extend(empty(false));
after_empty_extension.extend(image());
let mut before_empty_extension = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0", "EXTEND = T"]);
before_empty_extension.extend(image());
before_empty_extension.extend(empty(false));
for b in [&after_empty_primary, &after_empty_extension, &before_empty_extension] {
assert!(!crate::has_unretained_source_data(b).unwrap());
assert_eq!(decode(b, &Limits::default()).unwrap().data(), &[42]);
}
// Only empty HDUs: nothing to open, nothing to lose.
assert!(!crate::has_unretained_source_data(&empty(true)).unwrap());
assert!(decode(&empty(true), &Limits::default()).is_err());
// A second non-empty image is still additional data.
let mut two = after_empty_primary.clone();
two.extend(image());
assert!(crate::has_unretained_source_data(&two).unwrap());
}
#[test]
fn fpack_is_refused_as_unsupported() {
let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0"]);
b.extend(header(&["XTENSION= 'BINTABLE'", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 0", "NAXIS2 = 0", "ZIMAGE = T"]));
b.extend(header(&["XTENSION= 'BINTABLE'", "BITPIX = 8", "NAXIS = 1", "NAXIS1 = 0"]));
assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. })));
}
/// Hostile headers: absurd or negative axes, bad BITPIX, overflowing sizes, missing END,
/// a long chain of empty HDUs and declared data past the end all error, never panic or
/// allocate the declared size.
#[test]
fn hostile_headers_error_without_panicking() {
let base = ["SIMPLE = T", "BITPIX = 16"];
let cases: &[&[&str]] = &[
&["NAXIS = 2", "NAXIS1 = 4294967296", "NAXIS2 = 1"],
&["NAXIS = 2", "NAXIS1 = -5", "NAXIS2 = 1"],
&["NAXIS = 2", "NAXIS1 = 2000000", "NAXIS2 = 2000000"],
&["NAXIS = 3", "NAXIS1 = 65536", "NAXIS2 = 65536", "NAXIS3 = 9223372036854775807"],
&["NAXIS = 1000"],
&["NAXIS = -1"],
&["NAXIS = 2", "NAXIS1 = 0", "NAXIS2 = 0"],
&["NAXIS = 2", "NAXIS1 = 1.5D300", "NAXIS2 = 1"],
&["NAXIS = 2"],
&["NAXIS = 2", "NAXIS1 = 100", "NAXIS2 = 100"],
];
for extra in cases {
let cards: Vec<&str> = base.iter().chain(extra.iter()).copied().collect();
let b = header(&cards);
assert!(decode(&b, &Limits::default()).is_err(), "{extra:?}");
}
for bitpix in ["0", "7", "-16", "128", "'X'"] {
let card = format!("BITPIX = {bitpix}");
let b = header(&["SIMPLE = T", &card, "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 1"]);
assert!(decode(&b, &Limits::default()).is_err(), "BITPIX {bitpix}");
}
// No END card: the header runs off the end of the file.
let mut w = Writer { out: Vec::new() };
for c in ["SIMPLE = T", "BITPIX = 8", "NAXIS = 2"] {
w.card(c);
}
assert!(decode(&w.out, &Limits::default()).is_err());
// An empty primary followed by many empty extensions: bounded walk, then "no image".
let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0"]);
for _ in 0..(MAX_HDUS + 5) {
b.extend(header(&["XTENSION= 'IMAGE '", "BITPIX = 8", "NAXIS = 0"]));
}
assert!(decode(&b, &Limits::default()).is_err());
// An extension whose declared data size overflows.
let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0"]);
b.extend(header(&[
"XTENSION= 'BINTABLE'",
"BITPIX = 64",
"NAXIS = 2",
"NAXIS1 = 9223372036854775807",
"NAXIS2 = 4",
"PCOUNT = 0",
"GCOUNT = 1",
]));
assert!(decode(&b, &Limits::default()).is_err());
}
#[test]
fn extra_planes_warn() {
let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 3", "NAXIS1 = 1", "NAXIS2 = 1", "NAXIS3 = 4"]);
b.extend_from_slice(&[1, 2, 3, 4]);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.layout(), ChannelLayout::Gray);
assert_eq!(img.warnings, vec![DecodeWarning::MorePages { total: Some(4) }]);
}
#[test]
fn strings_with_quotes_and_hierarch_round_trip() {
let mut w = Writer { out: Vec::new() };
w.text("OBSERVER", "O'Brien");
w.text("Software", "photocraft 1.0");
w.text("HISTORY", "stacked");
let cards: Vec<Card> = w.out.chunks(CARD).map(parse_card).collect();
assert_eq!(cards[0].value, Some(Value::Str("O'Brien".into())));
assert!(cards[1].hierarch && cards[1].key == "Software");
assert_eq!(cards[1].value, Some(Value::Str("photocraft 1.0".into())));
assert_eq!((cards[2].key.as_str(), cards[2].text.as_str()), ("HISTORY", "stacked"));
}
}
-126
View File
@@ -1,126 +0,0 @@
//! The colour table of a GIF, read from its blocks without decoding pixels (GIF89a
//! specification: Logical Screen Descriptor, Global and Local Color Tables, Graphic Control
//! Extension).
//!
//! A GIF is always palette-based, so it opens in Indexed Color with the table of the frame that
//! [`crate::decode`] keeps, the first: its Local Color Table when it has one, else the Global
//! one, and the transparent entry that frame's Graphic Control Extension names.
/// Colours of the table the first frame uses, and its transparent entry. `None` for anything
/// that isn't a well-formed GIF up to its first image, or a first frame without a table.
pub(crate) fn palette(b: &[u8]) -> Option<(Vec<[u8; 3]>, Option<u8>)> {
if !(b.starts_with(b"GIF87a") || b.starts_with(b"GIF89a")) {
return None;
}
// Logical Screen Descriptor: width, height, packed fields, background, aspect.
let packed = *b.get(10)?;
let mut at = 13;
let table = |at: usize, packed: u8| -> Option<Vec<[u8; 3]>> {
let n = 2usize << (packed & 0x07);
let bytes = b.get(at..at.checked_add(n.checked_mul(3)?)?)?;
Some(bytes.as_chunks::<3>().0.to_vec())
};
let global = if packed & 0x80 != 0 {
let t = table(at, packed)?;
at += t.len() * 3;
Some(t)
} else {
None
};
let mut transparent = None;
loop {
match *b.get(at)? {
// Extension: label, then data sub-blocks up to a zero-length one.
0x21 => {
let label = *b.get(at + 1)?;
at += 2;
// Graphic Control Extension: block size 4, packed (bit 0: transparent colour
// given), delay (2 bytes), transparent colour index.
if label == 0xF9 && *b.get(at)? == 4 {
let flags = *b.get(at + 1)?;
transparent = (flags & 0x01 != 0).then_some(*b.get(at + 4)?);
}
loop {
let size = usize::from(*b.get(at)?);
at = at.checked_add(1 + size)?;
if size == 0 {
break;
}
}
}
// Image Descriptor: position and size (8 bytes), packed fields, then the Local Color
// Table if bit 7 is set. This is the first frame: its table is the one used.
0x2C => {
let packed = *b.get(at + 9)?;
let colors = if packed & 0x80 != 0 { table(at + 10, packed)? } else { global? };
let transparent = transparent.filter(|i| usize::from(*i) < colors.len());
return Some((colors, transparent));
}
// Trailer, or anything else: no image.
_ => return None,
}
}
}
#[cfg(test)]
mod tests {
use super::palette;
/// A GIF up to its first image: `global` table entries, optional GCE transparency, optional
/// local table.
fn gif(global: &[[u8; 3]], transparent: Option<u8>, local: Option<&[[u8; 3]]>) -> Vec<u8> {
let size_bits = |n: usize| (n.max(2).next_power_of_two().trailing_zeros() - 1) as u8;
let mut b = b"GIF89a".to_vec();
b.extend([4, 0, 4, 0]);
b.push(if global.is_empty() { 0 } else { 0x80 | size_bits(global.len()) });
b.extend([0, 0]);
let pad = |b: &mut Vec<u8>, t: &[[u8; 3]]| {
let n = 2usize << size_bits(t.len());
for k in 0..n {
b.extend(t.get(k).copied().unwrap_or([0, 0, 0]));
}
};
if !global.is_empty() {
pad(&mut b, global);
}
// A comment extension first, to skip.
b.extend([0x21, 0xFE, 3, b'h', b'i', b'!', 0]);
if let Some(i) = transparent {
b.extend([0x21, 0xF9, 4, 0x01, 0, 0, i, 0]);
}
b.extend([0x2C, 0, 0, 0, 0, 4, 0, 4, 0]);
match local {
Some(t) => {
b.push(0x80 | size_bits(t.len()));
pad(&mut b, t);
}
None => b.push(0),
}
b.extend([2, 2, 0x4C, 0x01, 0, 0x3B]);
b
}
#[test]
fn the_first_frames_table_and_transparent_entry() {
let g = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 255]];
assert_eq!(palette(&gif(&g, None, None)), Some((g.to_vec(), None)));
assert_eq!(palette(&gif(&g, Some(2), None)), Some((g.to_vec(), Some(2))));
// A local table wins over the global one.
let l = [[1, 2, 3], [4, 5, 6]];
assert_eq!(palette(&gif(&g, Some(1), Some(&l))), Some((l.to_vec(), Some(1))));
// A transparent index past the table is no transparent entry.
assert_eq!(palette(&gif(&l, Some(9), None)), Some((l.to_vec(), None)));
}
#[test]
fn no_table_or_not_a_gif_is_none() {
assert_eq!(palette(&gif(&[], None, None)), None, "no table at all");
assert_eq!(palette(b"\x89PNG\r\n\x1a\n"), None);
assert_eq!(palette(b""), None);
let whole = gif(&[[9, 9, 9], [8, 8, 8]], Some(0), None);
for cut in 0..whole.len() - 6 {
// Cut short anywhere before the image descriptor's fields: never a panic.
let _ = palette(&whole[..cut]);
}
}
}
-544
View File
@@ -1,544 +0,0 @@
//! Amiga IFF/ILBM reader and writer (EA "Interchange File Format", "InterLeaved BitMap" and the
//! chunky "PBM " variant). Written from the published IFF/ILBM layout: a `FORM` chunk of four-byte
//! chunks (`BMHD` bitmap header, `CMAP` palette, `CAMG` Amiga viewport mode, `BODY` pixel data).
//!
//! Read: 1–8 bitplanes with a palette (or a gray ramp when there is none) with masking 0/1/2, and
//! 24-bitplane "deep" ILBMs (8 planes each of red, green and blue, least significant bit first,
//! no palette) with masking 0/1, both stored (0) and ByteRun1/PackBits (1) compressed. Anything
//! else is a clear `Unsupported`/`Malformed` error, never a panic.
//!
//! Write: a 24-bitplane ILBM, ByteRun1 compressed one plane row at a time, with a mask plane
//! (masking 1) when the image has alpha. The mask plane is one bit per pixel: alpha of 128 or
//! more is opaque.
use crate::error::CodecError;
use crate::fidelity::Plan;
use crate::format::Format;
use crate::image::{ChannelLayout, Image, SampleType};
use crate::options::Limits;
const F: Format = Format::Iff;
fn bad(msg: impl std::fmt::Display) -> CodecError {
CodecError::malformed(F, msg)
}
fn unsupported(msg: impl Into<String>) -> CodecError {
CodecError::unsupported(F, msg)
}
fn be32(b: &[u8], at: usize) -> Option<u32> {
let s = b.get(at..at.checked_add(4)?)?.try_into().ok()?;
Some(u32::from_be_bytes(s))
}
fn be16(b: &[u8], at: usize) -> Option<u16> {
let s = b.get(at..at.checked_add(2)?)?.try_into().ok()?;
Some(u16::from_be_bytes(s))
}
/// `FORM` + a bitmap type (`ILBM` interleaved, `PBM ` chunky) at offset 8.
pub(crate) fn looks_like_iff(b: &[u8]) -> bool {
b.get(0..4) == Some(b"FORM") && matches!(b.get(8..12), Some(b"ILBM" | b"PBM "))
}
/// Bitplanes of a "deep" ILBM: red, green and blue, 8 each, least significant bit first.
const DEEP_PLANES: u8 = 24;
/// The bitmap header fields decoding needs.
struct Header {
width: u32,
height: u32,
planes: u8,
masking: u8,
compression: u8,
transparent: u16,
/// `PBM ` stores one palette index per byte, padded to even row lengths.
chunky: bool,
}
/// One four-byte chunk's id and byte range.
struct Chunk<'a> {
id: [u8; 4],
data: &'a [u8],
}
/// Walk the `FORM`'s chunks from offset 12 to `end` (the form length), stopping at the first
/// malformed length so a hostile file can't run past the buffer.
fn chunks(b: &[u8]) -> impl Iterator<Item = Chunk<'_>> {
let end = be32(b, 4).map_or(b.len(), |n| (n as usize).saturating_add(8).min(b.len()));
let mut pos = 12usize;
std::iter::from_fn(move || {
if pos + 8 > end {
return None;
}
let id = b.get(pos..pos + 4)?.try_into().ok()?;
let len = be32(b, pos + 4)? as usize;
let start = pos + 8;
let data = b.get(start..start.checked_add(len)?)?;
// Chunks are padded to an even length.
pos = start.checked_add(len)?.checked_add(len & 1)?;
Some(Chunk { id, data })
})
}
fn parse_header(b: &[u8], chunky: bool) -> Result<Header, CodecError> {
let bmhd = chunks(b).find(|c| &c.id == b"BMHD").ok_or_else(|| bad("no BMHD chunk"))?;
let d = bmhd.data;
if d.len() < 20 {
return Err(bad("BMHD chunk is too short"));
}
let width = u32::from(be16(d, 0).ok_or_else(|| bad("BMHD width"))?);
let height = u32::from(be16(d, 2).ok_or_else(|| bad("BMHD height"))?);
let planes = *d.get(8).ok_or_else(|| bad("BMHD planes"))?;
let masking = *d.get(9).ok_or_else(|| bad("BMHD masking"))?;
let compression = *d.get(10).ok_or_else(|| bad("BMHD compression"))?;
if width == 0 || height == 0 {
return Err(bad("zero-sized bitmap"));
}
if !(1..=8).contains(&planes) && planes != DEEP_PLANES {
return Err(unsupported(format!("{planes} bitplanes (1–8 with a palette, or 24)")));
}
if masking > 2 {
return Err(unsupported(format!("masking {masking}")));
}
if planes == DEEP_PLANES && masking == 2 {
return Err(unsupported("a transparent color in a 24-bitplane ILBM"));
}
if compression > 1 {
return Err(unsupported(format!("compression {compression} (only stored and ByteRun1)")));
}
if chunky && (planes != 8 || masking == 1) {
return Err(unsupported("PBM requires 8-bit indices without a mask plane"));
}
let transparent = be16(d, 12).ok_or_else(|| bad("BMHD transparent colour"))?;
Ok(Header { width, height, planes, masking, compression, transparent, chunky })
}
/// ByteRun1 / PackBits decode of one plane row into `out`; returns the bytes consumed. A `0x80`
/// control byte is a no-op, as the layout specifies.
fn unpack_byte_run1(src: &[u8], out: &mut [u8]) -> Result<usize, CodecError> {
let (mut i, mut o) = (0usize, 0usize);
while o < out.len() {
let n = *src.get(i).ok_or_else(|| bad("truncated ByteRun1 data"))? as i8;
i += 1;
if n >= 0 {
let count = n as usize + 1;
let lit = src.get(i..i.checked_add(count).ok_or_else(|| bad("ByteRun1 overflow"))?).ok_or_else(|| bad("truncated ByteRun1 run"))?;
if count > out.len() - o {
return Err(bad("ByteRun1 run crosses the plane row"));
}
out[o..o + count].copy_from_slice(lit);
i += count;
o += count;
} else if n != -128 {
let count = (1 - n as i32) as usize;
let value = *src.get(i).ok_or_else(|| bad("truncated ByteRun1 repeat"))?;
i += 1;
if count > out.len() - o {
return Err(bad("ByteRun1 repeat crosses the plane row"));
}
out[o..o + count].fill(value);
o += count;
}
}
Ok(i)
}
pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
if !looks_like_iff(b) {
return Err(bad("not an IFF/ILBM file (no FORM ... ILBM)"));
}
let h = parse_header(b, b.get(8..12) == Some(b"PBM "))?;
if let Some(camg) = chunks(b).find(|c| &c.id == b"CAMG") {
let mode = be32(camg.data, 0).ok_or_else(|| bad("short CAMG chunk"))?;
if mode & (0x80 | 0x800) != 0 {
return Err(unsupported("EHB and HAM viewport modes"));
}
}
let layout = if h.masking == 0 { ChannelLayout::Rgb } else { ChannelLayout::Rgba };
limits.check(h.width, h.height, layout, SampleType::U8)?;
// Palette: three bytes per colour; missing entries read as black.
let cmap = chunks(b).find(|c| &c.id == b"CMAP").map(|c| c.data).filter(|p| !p.is_empty());
let colors = 1usize << h.planes;
let palette = |idx: usize| -> [u8; 3] {
if let Some(p) = cmap {
p.get(idx * 3..idx * 3 + 3).map(|c| [c[0], c[1], c[2]]).unwrap_or([0; 3])
} else {
let v = ((idx as u32 * 255) / (colors as u32 - 1).max(1)) as u8;
[v, v, v]
}
};
let body = chunks(b).find(|c| &c.id == b"BODY").map(|c| c.data).ok_or_else(|| bad("no BODY chunk"))?;
let (w, hh) = (h.width as usize, h.height as usize);
// Each plane row is stored as a whole number of 16-bit words.
let plane_row = if h.chunky { w.div_ceil(2) * 2 } else { w.div_ceil(16) * 2 };
// A masking type 1 bitmap carries one extra (mask) plane per row.
let rows_planes: usize = if h.chunky { 1 } else { usize::from(h.planes) + usize::from(h.masking == 1) };
// Decode every stored plane row up front, then recombine.
let total_rows = hh.checked_mul(rows_planes).ok_or_else(|| CodecError::LimitExceeded("bitmap too large".into()))?;
let total_bytes = total_rows.checked_mul(plane_row).ok_or_else(|| CodecError::LimitExceeded("bitmap too large".into()))?;
if total_bytes as u64 > limits.max_alloc {
return Err(CodecError::LimitExceeded("bitmap storage exceeds max_alloc".into()));
}
Limits::check_plausible(total_bytes as u64, body.len(), if h.compression == 0 { 1 } else { 128 })?;
let mut stored = crate::options::alloc_zeroed::<u8>(total_bytes)?;
let mut cursor = 0usize;
for row in stored.chunks_mut(plane_row.max(1)).take(total_rows) {
match h.compression {
0 => {
let src = body.get(cursor..cursor.checked_add(plane_row).ok_or_else(|| bad("BODY overflow"))?).ok_or_else(|| bad("truncated BODY data"))?;
let n = row.len().min(src.len());
row[..n].copy_from_slice(&src[..n]);
cursor += plane_row;
}
_ => {
let tail = body.get(cursor..).ok_or_else(|| bad("truncated BODY data"))?;
cursor += unpack_byte_run1(tail, row)?;
}
}
}
let mut out = Vec::new();
out.try_reserve_exact(w.saturating_mul(hh).saturating_mul(layout.channels())).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?;
let planes = usize::from(h.planes);
for y in 0..hh {
for x in 0..w {
let index = if h.chunky {
usize::from(*stored.get(y * plane_row + x).ok_or_else(|| bad("PBM pixel outside BODY"))?)
} else {
(0..planes).filter(|&p| bit_at(&stored, (y * rows_planes + p) * plane_row, x)).fold(0usize, |acc, p| acc | (1 << p))
};
let c = if h.planes == DEEP_PLANES { [index as u8, (index >> 8) as u8, (index >> 16) as u8] } else { palette(index) };
out.extend_from_slice(&c);
if h.masking != 0 {
let opaque = if h.masking == 1 { bit_at(&stored, (y * rows_planes + planes) * plane_row, x) } else { index != usize::from(h.transparent) };
out.push(if opaque { 255 } else { 0 });
}
}
}
Image::from_u8(h.width, h.height, layout, out)
}
/// The bit for pixel `x` of a stored plane row at `base` (bits run MSB-first).
fn bit_at(stored: &[u8], base: usize, x: usize) -> bool {
stored.get(base + x / 8).is_some_and(|byte| byte & (0x80 >> (x % 8)) != 0)
}
/// ByteRun1 / PackBits encode of one plane row, appended to `out`: three or more equal bytes
/// become a repeat (at most 128), everything else literal runs (at most 128 bytes).
fn pack_byte_run1(row: &[u8], out: &mut Vec<u8>) {
let mut i = 0usize;
while let Some(&value) = row.get(i) {
let repeat = row[i..].iter().take(128).take_while(|&&b| b == value).count();
if repeat >= 3 {
out.push((1 - repeat as i32) as i8 as u8);
out.push(value);
i += repeat;
continue;
}
let start = i;
while i < row.len() && i - start < 128 {
if row.get(i..i + 3).is_some_and(|r| r[0] == r[1] && r[1] == r[2]) {
break;
}
i += 1;
}
out.push((i - start - 1) as u8);
out.extend_from_slice(&row[start..i]);
}
}
fn push_chunk(out: &mut Vec<u8>, id: &[u8; 4], data: &[u8]) -> Result<(), CodecError> {
let len = u32::try_from(data.len()).map_err(|_| CodecError::encode(F, "chunk too large for an IFF file (4 GiB)"))?;
out.extend_from_slice(id);
out.extend_from_slice(&len.to_be_bytes());
out.extend_from_slice(data);
// Chunks are padded to an even length.
if data.len() % 2 == 1 {
out.push(0);
}
Ok(())
}
/// Writes a 24-bitplane `FORM ILBM`: `BMHD` then a ByteRun1-compressed `BODY`, each row stored as
/// the red, green and blue planes (least significant bit first) and, for RGBA, a mask plane.
pub(crate) fn encode(src: &Image, plan: Plan) -> Result<Vec<u8>, CodecError> {
let enc = |msg: String| CodecError::encode(F, msg);
let img = src.converted(plan.layout, plan.sample);
let masked = match img.layout() {
ChannelLayout::Rgb => false,
ChannelLayout::Rgba => true,
l => return Err(enc(format!("IFF/ILBM cannot store {l:?}"))),
};
if img.sample_type() != SampleType::U8 {
return Err(enc(format!("IFF/ILBM cannot store {:?} samples", img.sample_type())));
}
let (w, h) = img.dimensions();
let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(h)) else {
return Err(enc("IFF/ILBM stores at most 65535 × 65535 pixels".into()));
};
let (w, ch) = (w as usize, img.layout().channels());
let data = img.data();
let row_len = w.checked_mul(ch).ok_or_else(|| enc("image too large".into()))?;
if data.len() != row_len.checked_mul(h as usize).ok_or_else(|| enc("image too large".into()))? {
return Err(enc("the image has the wrong number of samples".into()));
}
let planes = usize::from(DEEP_PLANES) + usize::from(masked);
let mut plane_row = vec![0u8; w.div_ceil(16) * 2];
let mut body = Vec::new();
for px in data.chunks_exact(row_len.max(1)) {
for plane in 0..planes {
plane_row.fill(0);
for (x, p) in px.chunks_exact(ch).enumerate() {
let on = if plane == usize::from(DEEP_PLANES) { p[3] >= 128 } else { (p[plane / 8] >> (plane % 8)) & 1 == 1 };
if on {
plane_row[x / 8] |= 0x80 >> (x % 8);
}
}
pack_byte_run1(&plane_row, &mut body);
}
}
let mut bmhd = Vec::with_capacity(20);
bmhd.extend_from_slice(&w16.to_be_bytes());
bmhd.extend_from_slice(&h16.to_be_bytes());
bmhd.extend_from_slice(&[0, 0, 0, 0]); // x/y origin
bmhd.push(DEEP_PLANES);
bmhd.push(u8::from(masked)); // masking: 1 = a mask plane follows the color planes
bmhd.push(1); // ByteRun1
bmhd.push(0); // pad
bmhd.extend_from_slice(&[0, 0]); // transparent color (unused)
bmhd.extend_from_slice(&[1, 1]); // square pixels
bmhd.extend_from_slice(&w16.to_be_bytes()); // page size
bmhd.extend_from_slice(&h16.to_be_bytes());
let mut out = b"FORM".to_vec();
out.extend_from_slice(&[0; 4]); // patched below
out.extend_from_slice(b"ILBM");
push_chunk(&mut out, b"BMHD", &bmhd)?;
push_chunk(&mut out, b"BODY", &body)?;
let form_len = u32::try_from(out.len() - 8).map_err(|_| enc("image too large for an IFF file (4 GiB)".into()))?;
out[4..8].copy_from_slice(&form_len.to_be_bytes());
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
/// A `FORM ILBM` with a `BMHD`, `CMAP` and `BODY`. `planes` plane rows per row,
/// `compression` 0 (stored) or 1 (ByteRun1), MSB-first.
fn ilbm(w: u16, h: u16, planes: u8, palette: &[[u8; 3]], compression: u8, body: &[u8]) -> Vec<u8> {
let mut b = b"FORM".to_vec();
b.extend_from_slice(&0u32.to_be_bytes()); // patched below
b.extend_from_slice(b"ILBM");
// BMHD (20 bytes)
b.extend_from_slice(b"BMHD");
b.extend_from_slice(&20u32.to_be_bytes());
b.extend_from_slice(&w.to_be_bytes());
b.extend_from_slice(&h.to_be_bytes());
b.extend_from_slice(&[0, 0, 0, 0]); // x/y origin
b.push(planes);
b.push(0); // masking
b.push(compression);
b.push(0); // pad
b.extend_from_slice(&[0, 0]); // transparent colour
b.extend_from_slice(&[1, 1]); // aspect
b.extend_from_slice(&[0, 0, 0, 0]); // page size
// CMAP
b.extend_from_slice(b"CMAP");
b.extend_from_slice(&((palette.len() * 3) as u32).to_be_bytes());
for c in palette {
b.extend_from_slice(c);
}
if palette.len() % 2 == 1 {
b.push(0);
}
// BODY
b.extend_from_slice(b"BODY");
b.extend_from_slice(&(body.len() as u32).to_be_bytes());
b.extend_from_slice(body);
if body.len() % 2 == 1 {
b.push(0);
}
let size = (b.len() - 8) as u32;
b[4..8].copy_from_slice(&size.to_be_bytes());
b
}
#[test]
fn reads_a_stored_two_plane_bitmap() {
// 2×1, 2 planes: pixel 0 index 0 (plane bits 0,0), pixel 1 index 3 (bits 1,1).
// Plane 0 = 0b0100_0000, plane 1 = 0b0100_0000, each padded to 2 bytes.
let body = [0b0100_0000, 0, 0b0100_0000, 0];
let pal = [[10, 20, 30], [40, 50, 60], [70, 80, 90], [100, 110, 120]];
let b = ilbm(2, 1, 2, &pal, 0, &body);
assert!(looks_like_iff(&b));
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!((img.width(), img.height(), img.layout()), (2, 1, ChannelLayout::Rgb));
assert_eq!(img.data(), &[10, 20, 30, 100, 110, 120]);
}
#[test]
fn reads_byte_run1_compressed_rows() {
// Same image, ByteRun1: literal run of two bytes (0x01) then the two plane bytes each row.
let body = [0x01, 0b0100_0000, 0, 0x01, 0b0100_0000, 0];
let pal = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [250, 251, 252]];
let b = ilbm(2, 1, 2, &pal, 1, &body);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.data(), &[1, 2, 3, 250, 251, 252]);
}
#[test]
fn no_palette_falls_back_to_a_grey_ramp() {
// 2×1, 1 plane, no CMAP: indices 0 and 1 → black and white.
let body = [0b0100_0000, 0];
let b = ilbm(2, 1, 1, &[], 0, &body);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.data(), &[0, 0, 0, 255, 255, 255]);
}
#[test]
fn pbm_reads_chunky_indices_with_odd_width_padding() {
let pal = [[10, 20, 30], [40, 50, 60], [70, 80, 90]];
for (compression, body) in [(0, vec![2, 0, 1, 255, 1, 2, 0, 255]), (1, vec![3, 2, 0, 1, 255, 3, 1, 2, 0, 255])] {
let mut b = ilbm(3, 2, 8, &pal, compression, &body);
b[8..12].copy_from_slice(b"PBM ");
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.data(), &[70, 80, 90, 10, 20, 30, 40, 50, 60, 40, 50, 60, 70, 80, 90, 10, 20, 30]);
}
}
#[test]
fn transparency_uses_the_mask_plane_or_transparent_index() {
for masking in [1, 2] {
let mut b = ilbm(2, 1, 1, &[[10, 20, 30], [40, 50, 60]], 0, if masking == 1 { &[0x40, 0, 0x40, 0] } else { &[0x40, 0] });
b[29] = masking;
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!(img.layout(), ChannelLayout::Rgba);
assert_eq!(img.data(), &[10, 20, 30, 0, 40, 50, 60, 255]);
}
let mut b = ilbm(2, 1, 8, &[[10, 20, 30], [40, 50, 60]], 0, &[0, 1]);
b[8..12].copy_from_slice(b"PBM ");
b[29] = 2;
b[32..34].copy_from_slice(&1u16.to_be_bytes());
assert_eq!(decode(&b, &Limits::default()).unwrap().data(), &[10, 20, 30, 255, 40, 50, 60, 0]);
}
#[test]
fn rejects_ham_and_ehb_instead_of_returning_wrong_colours() {
for mode in [0x80u32, 0x800] {
let mut b = ilbm(1, 1, 1, &[], 0, &[0, 0]);
b.extend_from_slice(b"CAMG");
b.extend_from_slice(&4u32.to_be_bytes());
b.extend_from_slice(&mode.to_be_bytes());
let len = (b.len() - 8) as u32;
b[4..8].copy_from_slice(&len.to_be_bytes());
assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. })));
}
}
#[test]
fn missing_palette_entries_are_black() {
let b = ilbm(1, 1, 2, &[[10, 20, 30]], 0, &[0x80, 0, 0x80, 0]);
assert_eq!(decode(&b, &Limits::default()).unwrap().data(), &[0, 0, 0]);
}
fn plan(layout: ChannelLayout) -> Plan {
Plan { layout, sample: SampleType::U8 }
}
#[test]
fn reads_a_stored_24_plane_bitmap() {
// 2×1: pixel 0 = (1, 0, 128), pixel 1 = (0, 255, 0). Each plane row is 2 bytes; red bit 0
// has pixel 0 set, green bits 0–7 have pixel 1 set, blue bit 7 has pixel 0 set.
let mut body = Vec::new();
for plane in 0..24 {
let byte = match plane {
0 | 23 => 0x80,
8..=15 => 0x40,
_ => 0,
};
body.extend_from_slice(&[byte, 0]);
}
let b = ilbm(2, 1, 24, &[], 0, &body);
let img = decode(&b, &Limits::default()).unwrap();
assert_eq!((img.width(), img.height(), img.layout()), (2, 1, ChannelLayout::Rgb));
assert_eq!(img.data(), &[1, 0, 128, 0, 255, 0]);
}
#[test]
fn writes_24_plane_byte_run1_that_reads_back_exactly() {
// 37 pixels wide: three plane-row words, the last one partly padding.
let (w, h) = (37u32, 3u32);
let data: Vec<u8> = (0..w * h * 3).map(|i| (i * 7 % 256) as u8).collect();
let img = Image::from_u8(w, h, ChannelLayout::Rgb, data.clone()).unwrap();
let bytes = encode(&img, plan(ChannelLayout::Rgb)).unwrap();
assert!(looks_like_iff(&bytes));
assert_eq!(be32(&bytes, 4).unwrap() as usize, bytes.len() - 8, "FORM length");
let h = parse_header(&bytes, false).unwrap();
assert_eq!((h.width, h.height, h.planes, h.masking, h.compression), (37, 3, 24, 0, 1));
assert!(chunks(&bytes).all(|c| &c.id != b"CMAP"), "a deep ILBM has no palette");
let back = decode(&bytes, &Limits::default()).unwrap();
assert_eq!(back.layout(), ChannelLayout::Rgb);
assert_eq!(back.data(), &data[..]);
}
#[test]
fn alpha_is_written_as_a_mask_plane() {
let data = [10, 20, 30, 0, 40, 50, 60, 127, 70, 80, 90, 128, 1, 2, 3, 255];
let img = Image::from_u8(4, 1, ChannelLayout::Rgba, data.to_vec()).unwrap();
let bytes = encode(&img, plan(ChannelLayout::Rgba)).unwrap();
assert_eq!(parse_header(&bytes, false).unwrap().masking, 1);
let back = decode(&bytes, &Limits::default()).unwrap();
assert_eq!(back.layout(), ChannelLayout::Rgba);
assert_eq!(back.data(), &[10, 20, 30, 0, 40, 50, 60, 0, 70, 80, 90, 255, 1, 2, 3, 255]);
}
#[test]
fn byte_run1_round_trips_runs_literals_and_long_rows() {
let mut rows: Vec<Vec<u8>> = vec![vec![], vec![5], vec![5, 5], vec![5, 5, 5], vec![1, 2, 2, 2, 3], vec![9; 300]];
rows.push((0..300).map(|i| (i % 251) as u8).collect());
rows.push([vec![1, 2], vec![7; 129], vec![3]].concat());
for row in rows {
let mut packed = Vec::new();
pack_byte_run1(&row, &mut packed);
let mut back = vec![0; row.len()];
assert_eq!(unpack_byte_run1(&packed, &mut back).unwrap(), packed.len(), "{row:?}");
assert_eq!(back, row);
}
// A long repeat packs into two-byte runs.
let mut packed = Vec::new();
pack_byte_run1(&[0; 256], &mut packed);
assert_eq!(packed, [0x81, 0, 0x81, 0]);
}
#[test]
fn rejects_what_ilbm_cannot_store() {
let gray = Image::from_u8(1, 1, ChannelLayout::Gray, vec![0]).unwrap();
assert!(encode(&gray, plan(ChannelLayout::Gray)).is_err());
let wide = Image::from_u8(65536, 1, ChannelLayout::Rgb, vec![0; 65536 * 3]).unwrap();
assert!(encode(&wide, plan(ChannelLayout::Rgb)).is_err());
// A transparent color index has no meaning without a palette.
let mut b = ilbm(1, 1, 24, &[], 0, &[0; 48]);
b[29] = 2;
assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. })));
}
#[test]
fn hostile_input_errors_not_panics() {
// Truncations of a valid file never panic.
let body = [0b0100_0000, 0, 0b0100_0000, 0];
let full = ilbm(2, 1, 2, &[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], 0, &body);
for cut in [0, 4, 8, 12, 40, full.len() - 1] {
assert!(decode(&full[..cut.min(full.len())], &Limits::default()).is_err(), "cut {cut}");
}
// Too many planes is unsupported.
let bad = ilbm(1, 1, 9, &[], 0, &[0, 0]);
assert!(matches!(decode(&bad, &Limits::default()), Err(CodecError::Unsupported { .. })));
}
}
-139
View File
@@ -1,139 +0,0 @@
//! ZSoft PCX (Paintbrush) reader: strictly bounded scanline RLE, 16/256-colour
//! palettes and 8-bit three-plane RGB. Read-only: modern saves should use PNG.
use crate::SampleType;
use crate::error::CodecError;
use crate::format::Format;
use crate::image::{ChannelLayout, Image};
use crate::options::Limits;
const HEADER: usize = 128;
fn bad(reason: impl std::fmt::Display) -> CodecError {
CodecError::malformed(Format::Pcx, reason)
}
fn le16(b: &[u8], i: usize) -> u16 {
u16::from_le_bytes([b[i], b[i + 1]])
}
/// PCX lacks a strong magic number. Require plausible complete header fields before
/// identifying it, rather than classifying every file starting with 0x0a as PCX.
pub(crate) fn looks_like_pcx(b: &[u8]) -> bool {
if b.len() < HEADER || b[0] != 0x0a || !matches!(b[1], 0 | 2 | 3 | 5) || b[2] != 1 {
return false;
}
let bits = b[3];
let planes = b[65];
let stride = u32::from(le16(b, 66));
let (x0, y0, x1, y1) = (le16(b, 4), le16(b, 6), le16(b, 8), le16(b, 10));
if x1 < x0 || y1 < y0 || !matches!(bits, 1 | 2 | 4 | 8) || !(1..=4).contains(&planes) {
return false;
}
let width = u32::from(x1 - x0) + 1;
let min_stride = (width * u32::from(bits)).div_ceil(8);
stride >= min_stride && stride > 0 && (bits != 8 || matches!(planes, 1 | 3)) && (bits == 8 || u32::from(bits) * u32::from(planes) <= 4)
}
/// Read one compressed scanline, including all planes. RLE packets never cross a scanline.
fn read_row(b: &[u8], pos: &mut usize, out: &mut [u8]) -> Result<(), CodecError> {
let mut n = 0;
while n < out.len() {
let code = *b.get(*pos).ok_or_else(|| bad("truncated pixel RLE"))?;
*pos += 1;
let (count, value) = if code >= 0xc0 {
let count = usize::from(code & 0x3f);
let value = *b.get(*pos).ok_or_else(|| bad("truncated RLE packet"))?;
*pos += 1;
(count, value)
} else {
(1, code)
};
if count == 0 || count > out.len() - n {
return Err(bad("RLE packet crosses a scanline or has zero length"));
}
out[n..n + count].fill(value);
n += count;
}
Ok(())
}
pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
if !looks_like_pcx(b) {
return Err(bad("invalid PCX header or unsupported pixel layout"));
}
let (w, h) = (u32::from(le16(b, 8) - le16(b, 4)) + 1, u32::from(le16(b, 10) - le16(b, 6)) + 1);
limits.check(w, h, ChannelLayout::Rgb, SampleType::U8)?;
let (bits, planes, stride) = (usize::from(b[3]), usize::from(b[65]), usize::from(le16(b, 66)));
let row_len = stride.checked_mul(planes).ok_or_else(|| bad("row size overflow"))?;
let total = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(3)).ok_or_else(|| bad("pixel size overflow"))?;
// The last 769 bytes are the optional VGA palette for 8-bit indexed images.
let vga = bits == 8 && planes == 1 && b.len() >= HEADER + 769 && b[b.len() - 769] == 0x0c;
if bits == 8 && planes == 1 && !vga && le16(b, 68) != 2 {
return Err(bad("8-bit indexed PCX has no 256-colour palette"));
}
let image_end = if vga { b.len() - 769 } else { b.len() };
// A 63-byte RLE packet takes 2 bytes; a trailing remainder of 1 byte below 0xC0
// can be encoded as a 1-byte literal, while any larger remainder takes at least 2 bytes.
// Because RLE packets may span planes within a scanline (`row_len`), compute the
// minimum encoded bytes across the full scanline (`row_len`) and multiply by `h`.
// Reject inputs that cannot possibly contain all scanlines before allocating
// the decoded image buffer.
let min_packets = (row_len / 63)
.checked_mul(2)
.and_then(|n| {
n.checked_add(match row_len % 63 {
0 => 0,
1 => 1,
_ => 2,
})
})
.ok_or_else(|| bad("compressed row size overflow"))?;
let min_encoded = min_packets.checked_mul(h as usize).ok_or_else(|| bad("compressed image size overflow"))?;
let encoded_len = image_end.saturating_sub(HEADER);
if encoded_len < min_encoded {
return Err(bad("PCX pixel data is too short for the declared image"));
}
let mut row = vec![0u8; row_len];
let mut pixels = Vec::new();
pixels.try_reserve_exact(total).map_err(|_| bad("PCX pixel buffer is too large"))?;
pixels.resize(total, 0);
let mut p = HEADER;
let width = w as usize;
for y in 0..h as usize {
read_row(&b[..image_end], &mut p, &mut row)?;
for x in 0..width {
let rgb = if bits == 8 && planes == 3 {
[row[x], row[stride + x], row[2 * stride + x]]
} else {
let index = if bits == 8 {
usize::from(row[x])
} else {
let bit = x * bits;
let shift = 8 - bits - (bit % 8);
let mask = (1usize << bits) - 1;
(0..planes).fold(0usize, |v, plane| {
let sample = (usize::from(row[plane * stride + bit / 8]) >> shift) & mask;
v | sample << (plane * bits)
})
};
if vga {
let o = image_end + 1 + index * 3;
[b[o], b[o + 1], b[o + 2]]
} else if bits == 8 {
let g = index as u8;
[g, g, g] // palette-info 2: gray scale
} else {
let o = 16 + index * 3;
[b[o], b[o + 1], b[o + 2]]
}
};
pixels[(y * width + x) * 3..(y * width + x + 1) * 3].copy_from_slice(&rgb);
}
}
let mut image = Image::from_u8(w, h, ChannelLayout::Rgb, pixels)?;
let (xdpi, ydpi) = (le16(b, 12), le16(b, 14));
if xdpi > 0 && ydpi > 0 {
image.meta.dpi = Some((f32::from(xdpi), f32::from(ydpi)));
}
Ok(image)
}
+1 -1
View File
@@ -1598,7 +1598,7 @@ pub fn encode_indexed(
dpi: Option<(f32, f32)>,
stereo: Option<PngStereo>,
) -> Result<Vec<u8>, CodecError> {
if width == 0 || height == 0 || indices.len() != width as usize * height as usize {
if width == 0 || height == 0 || (width as usize).checked_mul(height as usize) != Some(indices.len()) {
return Err(CodecError::InvalidImage("index data does not match the size".into()));
}
if palette.is_empty() || palette.len() > 256 || indices.iter().any(|&i| usize::from(i) >= palette.len()) {
-355
View File
@@ -1,355 +0,0 @@
//! Netpbm family, implemented here: P1–P6 (PBM/PGM/PPM, ASCII and binary),
//! P7 (PAM, including the common `CMYK`/`CMYK_ALPHA` tuple types) and PFM
//! (`PF` colour / `Pf` gray float).
//!
//! Writing picks the subtype from the image: float → PFM (gray/RGB), gray
//! → P5, RGB → P6, anything with alpha or CMYK → P7.
use crate::Format;
use crate::error::CodecError;
use crate::fidelity::Plan;
use crate::image::{ChannelLayout, Image, SampleType};
use crate::options::{EncodeOptions, Limits};
const F: Format = Format::Pnm;
fn err(msg: impl std::fmt::Display) -> CodecError {
CodecError::malformed(F, msg)
}
struct Reader<'a> {
b: &'a [u8],
pos: usize,
}
impl<'a> Reader<'a> {
fn skip_ws_and_comments(&mut self) {
while self.pos < self.b.len() {
let c = self.b[self.pos];
if c == b'#' {
while self.pos < self.b.len() && self.b[self.pos] != b'\n' && self.b[self.pos] != b'\r' {
self.pos += 1;
}
} else if c.is_ascii_whitespace() {
self.pos += 1;
} else {
break;
}
}
}
fn token(&mut self) -> Result<&'a [u8], CodecError> {
self.skip_ws_and_comments();
let start = self.pos;
while self.pos < self.b.len() && !self.b[self.pos].is_ascii_whitespace() && self.b[self.pos] != b'#' {
self.pos += 1;
}
if start == self.pos {
return Err(err("unexpected end of header"));
}
Ok(&self.b[start..self.pos])
}
fn uint(&mut self) -> Result<u32, CodecError> {
let t = self.token()?;
std::str::from_utf8(t).ok().and_then(|s| s.parse::<u32>().ok()).ok_or_else(|| err("invalid integer in header"))
}
/// Consume the single whitespace byte that separates header and raster.
fn single_ws(&mut self) -> Result<(), CodecError> {
match self.b.get(self.pos) {
Some(c) if c.is_ascii_whitespace() => {
self.pos += 1;
Ok(())
}
_ => Err(err("missing whitespace after header")),
}
}
/// PAM ends its header with an LF-terminated line, unlike P4–P6's single separator.
fn pam_endhdr(&mut self) -> Result<(), CodecError> {
while let Some(&c) = self.b.get(self.pos) {
self.pos += 1;
if c == b'\n' {
// Stop at the first LF: whitespace and '#' after it are raster bytes.
return Ok(());
}
if !c.is_ascii_whitespace() {
return Err(err("unexpected data after PAM ENDHDR"));
}
}
Err(err("unterminated PAM ENDHDR line"))
}
fn rest(&self) -> &'a [u8] {
&self.b[self.pos.min(self.b.len())..]
}
}
fn sample_for_maxval(maxval: u32) -> Result<SampleType, CodecError> {
match maxval {
1..=255 => Ok(SampleType::U8),
256..=65535 => Ok(SampleType::U16),
_ => Err(err(format!("invalid maxval {maxval}"))),
}
}
/// The decoded raster, built straight in the output representation. It is the only buffer sized
/// by the header, so `Limits::check` on the output budgets all of it: no wider scratch buffer
/// (such as one `u32` per sample) sits beside it (#2938).
struct Samples {
maxval: u32,
sample: SampleType,
bytes: Vec<u8>,
}
impl Samples {
/// Reserve room for `cap` samples (callers bound `cap` by the file length where the header
/// alone could overstate it).
fn with_capacity(maxval: u32, cap: usize) -> Result<Self, CodecError> {
let sample = sample_for_maxval(maxval)?;
let len = cap.checked_mul(sample.bytes()).ok_or_else(|| CodecError::LimitExceeded(format!("{cap} samples do not fit in memory")))?;
let mut bytes = Vec::new();
bytes.try_reserve_exact(len).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {len} bytes for the image")))?;
Ok(Samples { maxval, sample, bytes })
}
/// Append one raw value in `[0, maxval]`, rescaled to the full range of the output sample.
fn push(&mut self, v: u32) -> Result<(), CodecError> {
let m = self.maxval;
if v > m {
return Err(err("sample exceeds maxval"));
}
match (self.sample, m) {
(SampleType::U8, 255) => self.bytes.push(v as u8),
(SampleType::U8, m) => self.bytes.push(((v * 255 + m / 2) / m) as u8),
(_, 65535) => self.bytes.extend_from_slice(&(v as u16).to_ne_bytes()),
(_, m) => self.bytes.extend_from_slice(&(((v as u64 * 65535 + m as u64 / 2) / m as u64) as u16).to_ne_bytes()),
}
Ok(())
}
fn into_image(self, w: u32, h: u32, layout: ChannelLayout) -> Result<Image, CodecError> {
Image::from_raw(w, h, layout, self.sample, self.bytes)
}
}
/// `w × h × channels` samples. `Limits::none()` lets any header through, so the product is
/// checked here rather than trusted to the limits.
fn sample_count(w: u32, h: u32, channels: usize) -> Result<usize, CodecError> {
(w as usize)
.checked_mul(h as usize)
.and_then(|p| p.checked_mul(channels))
.ok_or_else(|| CodecError::LimitExceeded(format!("{w}x{h}x{channels} samples do not fit in memory")))
}
fn read_binary(data: &[u8], n: usize, maxval: u32) -> Result<Samples, CodecError> {
let mut out = Samples::with_capacity(maxval, n)?;
if maxval < 256 {
let Some(raw) = data.get(..n) else {
return Err(err("truncated raster"));
};
for &v in raw {
out.push(v as u32)?;
}
} else {
let Some(raw) = n.checked_mul(2).and_then(|len| data.get(..len)) else {
return Err(err("truncated raster"));
};
for c in raw.as_chunks::<2>().0 {
out.push(u16::from_be_bytes(*c) as u32)?;
}
}
Ok(out)
}
pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
if bytes.len() < 2 || bytes[0] != b'P' {
return Err(err("not a Netpbm file"));
}
let kind = bytes[1];
let mut r = Reader { b: bytes, pos: 2 };
match kind {
b'1' | b'4' => {
let (w, h) = (r.uint()?, r.uint()?);
limits.check(w, h, ChannelLayout::Gray, SampleType::U8)?;
let n = sample_count(w, h, 1)?;
let mut px;
if kind == b'4' {
r.single_ws()?;
let row = (w as usize).div_ceil(8);
let data = r.rest();
if row.checked_mul(h as usize).is_none_or(|len| data.len() < len) {
return Err(err("truncated raster"));
}
px = Vec::with_capacity(n);
for y in 0..h as usize {
for x in 0..w as usize {
let bit = (data[y * row + x / 8] >> (7 - x % 8)) & 1;
px.push(if bit == 1 { 0 } else { 255 });
}
}
} else {
// Each ASCII pixel takes at least a byte, so the file bounds the reservation (#1110).
px = Vec::with_capacity(n.min(r.rest().len()));
while px.len() < n {
r.skip_ws_and_comments();
match r.b.get(r.pos) {
Some(b'0') => px.push(255),
Some(b'1') => px.push(0),
Some(_) => return Err(err("invalid PBM digit")),
None => return Err(err("truncated raster")),
}
r.pos += 1;
}
}
Image::from_u8(w, h, ChannelLayout::Gray, px)
}
b'2' | b'3' | b'5' | b'6' => {
let (w, h, maxval) = (r.uint()?, r.uint()?, r.uint()?);
let layout = if matches!(kind, b'2' | b'5') { ChannelLayout::Gray } else { ChannelLayout::Rgb };
limits.check(w, h, layout, sample_for_maxval(maxval)?)?;
let n = sample_count(w, h, layout.channels())?;
let samples = if matches!(kind, b'5' | b'6') {
r.single_ws()?;
read_binary(r.rest(), n, maxval)?
} else {
// Each ASCII sample is a digit and a separator (the last may lack one), so the
// file, not the header, bounds the reservation (#1110).
let mut v = Samples::with_capacity(maxval, n.min(r.rest().len() / 2 + 1))?;
for _ in 0..n {
v.push(r.uint()?)?;
}
v
};
samples.into_image(w, h, layout)
}
b'7' => decode_pam(&mut r, limits),
b'F' | b'f' => {
let layout = if kind == b'F' { ChannelLayout::Rgb } else { ChannelLayout::Gray };
let (w, h) = (r.uint()?, r.uint()?);
let scale: f32 = std::str::from_utf8(r.token()?)
.ok()
.and_then(|s| s.parse().ok())
.filter(|s: &f32| s.is_finite() && *s != 0.0)
.ok_or_else(|| err("invalid PFM scale"))?;
r.single_ws()?;
limits.check(w, h, layout, SampleType::F32)?;
let nc = layout.channels();
let row = w as usize * nc;
let data = r.rest();
if sample_count(w, h, nc)?.checked_mul(4).is_none_or(|len| data.len() < len) {
return Err(err("truncated raster"));
}
let le = scale < 0.0;
// Write native-endian bytes straight into the output buffer: a separate `Vec<f32>`
// would double the budgeted allocation (#2938).
let row_bytes = row * 4;
let mut out = vec![0u8; row_bytes * h as usize];
for (y, src) in data.chunks_exact(row_bytes).take(h as usize).enumerate() {
// PFM rows are stored bottom-to-top.
let dst_start = (h as usize - 1 - y) * row_bytes;
let Some(dst) = out.get_mut(dst_start..dst_start + row_bytes) else {
return Err(err("truncated raster"));
};
for (d, s) in dst.as_chunks_mut::<4>().0.iter_mut().zip(src.as_chunks::<4>().0) {
let v = if le { f32::from_le_bytes(*s) } else { f32::from_be_bytes(*s) };
*d = v.to_ne_bytes();
}
}
Image::from_raw(w, h, layout, SampleType::F32, out)
}
_ => Err(err("unknown Netpbm subtype")),
}
}
fn decode_pam(r: &mut Reader<'_>, limits: &Limits) -> Result<Image, CodecError> {
let (mut w, mut h, mut depth, mut maxval) = (None, None, None, None);
let mut tupltype = String::new();
loop {
let tok = r.token()?;
match tok {
b"WIDTH" => w = Some(r.uint()?),
b"HEIGHT" => h = Some(r.uint()?),
b"DEPTH" => depth = Some(r.uint()?),
b"MAXVAL" => maxval = Some(r.uint()?),
b"TUPLTYPE" => {
let t = r.token()?;
if !tupltype.is_empty() {
tupltype.push(' ');
}
tupltype.push_str(std::str::from_utf8(t).map_err(|_| err("bad TUPLTYPE"))?);
}
b"ENDHDR" => break,
_ => return Err(err("unknown PAM header field")),
}
}
r.pam_endhdr()?;
let (w, h, depth, maxval) = match (w, h, depth, maxval) {
(Some(a), Some(b), Some(c), Some(d)) => (a, b, c, d),
_ => return Err(err("incomplete PAM header")),
};
let layout = match (tupltype.as_str(), depth) {
("BLACKANDWHITE" | "GRAYSCALE", 1) | ("", 1) => ChannelLayout::Gray,
("BLACKANDWHITE_ALPHA" | "GRAYSCALE_ALPHA", 2) | ("", 2) => ChannelLayout::GrayA,
("RGB", 3) | ("", 3) => ChannelLayout::Rgb,
("RGB_ALPHA", 4) | ("", 4) => ChannelLayout::Rgba,
("CMYK", 4) => ChannelLayout::Cmyk,
("CMYK_ALPHA", 5) => ChannelLayout::CmykA,
(t, d) => {
return Err(CodecError::unsupported(F, format!("PAM tuple type {t:?} with depth {d}")));
}
};
limits.check(w, h, layout, sample_for_maxval(maxval)?)?;
let n = sample_count(w, h, layout.channels())?;
read_binary(r.rest(), n, maxval)?.into_image(w, h, layout)
}
pub(crate) fn encode(src: &Image, plan: Plan, _opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
let img = src.converted(plan.layout, plan.sample);
let (w, h) = img.dimensions();
let layout = img.layout();
let mut out = Vec::new();
match img.sample_type() {
SampleType::F32 => {
let tag = match layout {
ChannelLayout::Gray => "Pf",
ChannelLayout::Rgb => "PF",
l => return Err(CodecError::encode(F, format!("PFM cannot store {l:?}"))),
};
out.extend_from_slice(format!("{tag}\n{w} {h}\n-1.0\n").as_bytes());
let s = img.to_f32_samples().unwrap_or_default();
let row = w as usize * layout.channels();
for y in (0..h as usize).rev() {
for v in &s[y * row..(y + 1) * row] {
out.extend_from_slice(&v.to_le_bytes());
}
}
}
sample @ (SampleType::U8 | SampleType::U16) => {
let maxval = if sample == SampleType::U8 { 255 } else { 65535 };
match layout {
ChannelLayout::Gray => out.extend_from_slice(format!("P5\n{w} {h}\n{maxval}\n").as_bytes()),
ChannelLayout::Rgb => out.extend_from_slice(format!("P6\n{w} {h}\n{maxval}\n").as_bytes()),
l => {
let t = match l {
ChannelLayout::GrayA => "GRAYSCALE_ALPHA",
ChannelLayout::Rgba => "RGB_ALPHA",
ChannelLayout::Cmyk => "CMYK",
_ => "CMYK_ALPHA",
};
out.extend_from_slice(format!("P7\nWIDTH {w}\nHEIGHT {h}\nDEPTH {}\nMAXVAL {maxval}\nTUPLTYPE {t}\nENDHDR\n", l.channels()).as_bytes());
}
}
if sample == SampleType::U8 {
out.extend_from_slice(img.data());
} else {
for v in img.to_u16_samples().unwrap_or_default() {
out.extend_from_slice(&v.to_be_bytes());
}
}
}
s => return Err(CodecError::encode(F, format!("unsupported sample {s:?}"))),
}
Ok(out)
}
+169
View File
@@ -0,0 +1,169 @@
//! DDS, DICOM, FITS, IFF/ILBM, PCX, PNM and XPM: the format code is craft-libs'
//! `craft-raster-codecs`. This maps its image, limits and errors onto this crate's, and the
//! encoders take this crate's [`Plan`] (the layout and depth conversion stays here).
use craft_raster_codecs as rc;
use crate::error::CodecError;
use crate::fidelity::Plan;
use crate::format::Format;
use crate::image::{ChannelLayout, DecodeWarning, Image, SampleType};
use crate::options::Limits;
fn limits(l: &Limits) -> rc::Limits {
rc::Limits { max_width: l.max_width, max_height: l.max_height, max_pixels: l.max_pixels, max_alloc: l.max_alloc }
}
fn err(format: Format, e: rc::CodecError) -> CodecError {
match e {
rc::CodecError::Unsupported { reason, .. } => CodecError::Unsupported { format, reason },
rc::CodecError::Malformed { message, .. } => CodecError::Malformed { format, message },
rc::CodecError::LimitExceeded(m) => CodecError::LimitExceeded(m),
rc::CodecError::Encode { message, .. } => CodecError::Encode { format, message },
rc::CodecError::InvalidImage(m) => CodecError::InvalidImage(m),
}
}
/// A decoded image as this crate's.
fn from_rc(format: Format, img: rc::Image) -> Result<Image, CodecError> {
let layout = match img.layout() {
rc::ChannelLayout::Gray => ChannelLayout::Gray,
rc::ChannelLayout::GrayA => ChannelLayout::GrayA,
rc::ChannelLayout::Rgb => ChannelLayout::Rgb,
rc::ChannelLayout::Rgba => ChannelLayout::Rgba,
rc::ChannelLayout::Cmyk => ChannelLayout::Cmyk,
rc::ChannelLayout::CmykA => ChannelLayout::CmykA,
};
let sample = match img.sample_type() {
rc::SampleType::U8 => SampleType::U8,
rc::SampleType::U16 => SampleType::U16,
rc::SampleType::F32 => SampleType::F32,
};
let (w, h) = img.dimensions();
let (dpi, text) = (img.dpi, img.text.clone());
let warnings: Vec<DecodeWarning> = img.warnings.iter().map(|rc::DecodeWarning::MorePages { total }| DecodeWarning::MorePages { total: *total }).collect();
let mut out = Image::from_raw(w, h, layout, sample, img.into_data()).map_err(|e| CodecError::malformed(format, e))?;
out.meta.dpi = dpi;
out.meta.text = text;
out.warnings = warnings;
Ok(out)
}
/// `image` converted as `plan` says, as the shared crate's image (with its resolution and text).
fn to_rc(format: Format, image: &Image, plan: Plan) -> Result<rc::Image, CodecError> {
let img = image.converted(plan.layout, plan.sample);
let layout = match img.layout() {
ChannelLayout::Gray => rc::ChannelLayout::Gray,
ChannelLayout::GrayA => rc::ChannelLayout::GrayA,
ChannelLayout::Rgb => rc::ChannelLayout::Rgb,
ChannelLayout::Rgba => rc::ChannelLayout::Rgba,
ChannelLayout::Cmyk => rc::ChannelLayout::Cmyk,
ChannelLayout::CmykA => rc::ChannelLayout::CmykA,
l => return Err(CodecError::encode(format, format!("cannot store {l:?}"))),
};
let sample = match img.sample_type() {
SampleType::U8 => rc::SampleType::U8,
SampleType::U16 => rc::SampleType::U16,
SampleType::F32 => rc::SampleType::F32,
s => return Err(CodecError::encode(format, format!("unsupported sample {s:?}"))),
};
let (w, h) = img.dimensions();
let mut out = rc::Image::from_raw(w, h, layout, sample, img.data().to_vec()).map_err(|e| err(format, e))?;
out.dpi = img.meta.dpi;
out.text = img.meta.text.clone();
Ok(out)
}
pub(crate) mod dds {
use super::{CodecError, Format, Image, Limits, Plan, SampleType, err, from_rc, limits, rc, to_rc};
use crate::options::EncodeOptions;
const F: Format = Format::Dds;
pub(crate) fn has_additional_images(bytes: &[u8]) -> Result<bool, CodecError> {
rc::dds::has_additional_images(bytes).map_err(|e| err(F, e))
}
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
from_rc(F, rc::dds::decode(b, &limits(l)).map_err(|e| err(F, e))?)
}
pub(crate) fn encode(image: &Image, plan: Plan, opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
let img = to_rc(F, image, Plan { sample: SampleType::U8, ..plan })?;
rc::dds::encode(&img, opts.dds).map_err(|e| err(F, e))
}
}
pub(crate) mod dicom {
use super::{CodecError, Format, Image, Limits, err, from_rc, limits, rc};
const F: Format = Format::Dicom;
pub(crate) fn is_dicom(b: &[u8]) -> bool {
rc::dicom::is_dicom(b)
}
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
from_rc(F, rc::dicom::decode(b, &limits(l)).map_err(|e| err(F, e))?)
}
}
pub(crate) mod fits {
use super::{CodecError, Format, Image, Limits, Plan, err, from_rc, limits, rc, to_rc};
use crate::options::EncodeOptions;
const F: Format = Format::Fits;
pub(crate) fn has_additional_images(bytes: &[u8]) -> Result<bool, CodecError> {
rc::fits::has_additional_images(bytes).map_err(|e| err(F, e))
}
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
from_rc(F, rc::fits::decode(b, &limits(l)).map_err(|e| err(F, e))?)
}
pub(crate) fn encode(image: &Image, plan: Plan, _opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
rc::fits::encode(&to_rc(F, image, plan)?).map_err(|e| err(F, e))
}
}
pub(crate) mod iff {
use super::{CodecError, Format, Image, Limits, Plan, err, from_rc, limits, rc, to_rc};
const F: Format = Format::Iff;
pub(crate) fn looks_like_iff(b: &[u8]) -> bool {
rc::iff::looks_like_iff(b)
}
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
from_rc(F, rc::iff::decode(b, &limits(l)).map_err(|e| err(F, e))?)
}
pub(crate) fn encode(image: &Image, plan: Plan) -> Result<Vec<u8>, CodecError> {
rc::iff::encode(&to_rc(F, image, plan)?).map_err(|e| err(F, e))
}
}
pub(crate) mod pcx {
use super::{CodecError, Format, Image, Limits, err, from_rc, limits, rc};
const F: Format = Format::Pcx;
pub(crate) fn looks_like_pcx(b: &[u8]) -> bool {
rc::pcx::looks_like_pcx(b)
}
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
from_rc(F, rc::pcx::decode(b, &limits(l)).map_err(|e| err(F, e))?)
}
}
pub(crate) mod pnm {
use super::{CodecError, Format, Image, Limits, Plan, err, from_rc, limits, rc, to_rc};
use crate::options::EncodeOptions;
const F: Format = Format::Pnm;
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
from_rc(F, rc::pnm::decode(b, &limits(l)).map_err(|e| err(F, e))?)
}
pub(crate) fn encode(image: &Image, plan: Plan, _opts: &EncodeOptions) -> Result<Vec<u8>, CodecError> {
rc::pnm::encode(&to_rc(F, image, plan)?).map_err(|e| err(F, e))
}
}
pub(crate) mod xpm {
use super::{CodecError, Format, Image, Limits, err, from_rc, limits, rc};
const F: Format = Format::Xpm;
pub(crate) fn decode(b: &[u8], l: &Limits) -> Result<Image, CodecError> {
from_rc(F, rc::xpm::decode(b, &limits(l)).map_err(|e| err(F, e))?)
}
}
+13 -162
View File
@@ -1,175 +1,26 @@
//! Targa writer. The `image` crate still reads Targa ([`super::via_image`]); writing is ours
//! because its run-length packets ran on from the end of one row into the next. TGA 2.0 says a
//! packet must not cross a scanline, and readers that decode a row at a time (Pillow, texture
//! converters, game engines) misread such files (#6382).
//!
//! Like Photoshop's Targa: RLE (types 10 and 11), rows stored bottom-up (the bottom-left origin
//! every reader assumes, also those that ignore the origin bits), alpha bits in the descriptor,
//! and the TGA 2.0 footer.
//! Targa writer: craft-libs' `craft-tga` (run-length packets that end with their scanline, rows
//! bottom-up, alpha bits in the descriptor, the TGA 2.0 footer, #6382). The `image` crate still
//! reads Targa ([`super::via_image`]).
use crate::Format;
use crate::error::CodecError;
use crate::image::{ChannelLayout, Image, SampleType};
/// Most pixels one packet holds (a 7-bit count, plus one).
const MAX_PACKET: usize = 128;
/// The TGA 2.0 footer: no extension or developer area, then the signature.
const FOOTER: &[u8; 26] = b"\0\0\0\0\0\0\0\0TRUEVISION-XFILE.\0";
/// Encodes an 8-bit Gray, Gray+alpha, RGB or RGBA image as a run-length encoded Targa.
pub(crate) fn encode(img: &Image) -> Result<Vec<u8>, CodecError> {
let (w, h) = img.dimensions();
if img.sample_type() != SampleType::U8 {
return Err(CodecError::encode(Format::Tga, format!("{:?} samples (Targa stores 8 bits)", img.sample_type())));
}
let (bpp, image_type, alpha_bits) = match img.layout() {
ChannelLayout::Gray => (1, 11, 0),
ChannelLayout::GrayA => (2, 11, 8),
ChannelLayout::Rgb => (3, 10, 0),
ChannelLayout::Rgba => (4, 10, 8),
let layout = match img.layout() {
ChannelLayout::Gray => craft_tga::Layout::Gray,
ChannelLayout::GrayA => craft_tga::Layout::GrayAlpha,
ChannelLayout::Rgb => craft_tga::Layout::Rgb,
ChannelLayout::Rgba => craft_tga::Layout::Rgba,
l => return Err(CodecError::encode(Format::Tga, format!("no Targa layout for {l:?}"))),
};
let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(h)) else {
return Err(CodecError::encode(Format::Tga, format!("dimensions exceed 65535x65535: {w}x{h}")));
};
let row = w as usize * bpp;
let data = img.data();
if data.len() != row * h as usize {
return Err(CodecError::InvalidImage("buffer size mismatch".into()));
}
let mut out = Vec::with_capacity(18 + data.len() + data.len() / MAX_PACKET + FOOTER.len());
out.extend_from_slice(&[0, 0, image_type, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
out.extend_from_slice(&w16.to_le_bytes());
out.extend_from_slice(&h16.to_le_bytes());
// Bits per pixel; the descriptor holds the alpha bits, origin bits 0 (bottom-left).
out.extend_from_slice(&[(bpp * 8) as u8, alpha_bits]);
let mut px = vec![0u8; row];
for y in (0..h as usize).rev() {
px.copy_from_slice(&data[y * row..(y + 1) * row]);
// Targa stores colour as BGR.
if bpp >= 3 {
for p in px.chunks_exact_mut(bpp) {
p.swap(0, 2);
}
}
rle_row(&px, bpp, &mut out);
}
out.extend_from_slice(FOOTER);
Ok(out)
}
/// One scanline as run-length packets that end with it: a run of two or more equal pixels is a
/// run packet, anything else goes out in raw packets.
fn rle_row(row: &[u8], bpp: usize, out: &mut Vec<u8>) {
let px: Vec<&[u8]> = row.chunks_exact(bpp).collect();
let n = px.len();
let mut i = 0;
while i < n {
let mut run = 1;
while i + run < n && run < MAX_PACKET && px[i + run] == px[i] {
run += 1;
}
if run >= 2 {
out.push(0x80 | (run - 1) as u8);
out.extend_from_slice(px[i]);
i += run;
continue;
}
// Raw until two equal pixels start a run, the packet is full or the row ends. The first
// pixel always goes in: it differs from the one after it, or it is the row's last.
let start = i;
while i < n && i - start < MAX_PACKET && !(i > start && i + 1 < n && px[i] == px[i + 1]) {
i += 1;
}
out.push((i - start - 1) as u8);
for p in &px[start..i] {
out.extend_from_slice(p);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn run_packets_at_the_128_pixel_boundary() {
for bpp in 1..=4 {
for (pixels, headers) in [(1, vec![0]), (2, vec![0x81]), (127, vec![0xfe]), (128, vec![0xff]), (129, vec![0xff, 0])] {
let pixel: Vec<u8> = (1..=bpp as u8).collect();
let mut encoded = Vec::new();
rle_row(&pixel.repeat(pixels), bpp, &mut encoded);
let expected: Vec<u8> = headers.into_iter().flat_map(|header| std::iter::once(header).chain(pixel.iter().copied())).collect();
assert_eq!(encoded, expected, "{pixels} pixels, {bpp} bytes per pixel");
}
}
}
#[test]
fn raw_packets_at_the_128_pixel_boundary() {
for bpp in 1..=4 {
for pixels in [127usize, 128, 129] {
let row: Vec<u8> = (0..pixels).flat_map(|x| vec![x as u8; bpp]).collect();
let mut encoded = Vec::new();
rle_row(&row, bpp, &mut encoded);
let mut expected = vec![(pixels.min(128) - 1) as u8];
expected.extend_from_slice(&row[..pixels.min(128) * bpp]);
if pixels > 128 {
expected.push(0);
expected.extend_from_slice(&row[128 * bpp..]);
}
assert_eq!(encoded, expected, "{pixels} pixels, {bpp} bytes per pixel");
}
}
}
#[test]
fn raw_packets_stop_before_a_run_of_complete_pixels() {
// Equal first channels alone do not make an RGBA run; alpha participates too.
let a = [10, 20, 30, 40];
let b = [10, 20, 30, 41];
let c = [50, 60, 70, 80];
let row = [a, b, c, c, a].concat();
let mut encoded = Vec::new();
rle_row(&row, 4, &mut encoded);
assert_eq!(encoded, [vec![1], a.to_vec(), b.to_vec(), vec![0x81], c.to_vec(), vec![0], a.to_vec()].concat());
}
#[test]
fn rejects_sample_types_and_layouts_targa_cannot_store() {
for sample in [SampleType::U16, SampleType::F16, SampleType::F32] {
let img = Image::new(1, 1, ChannelLayout::Rgb, sample).unwrap();
assert!(matches!(encode(&img), Err(CodecError::Encode { format: Format::Tga, .. })), "{sample:?}");
}
for layout in [ChannelLayout::Cmyk, ChannelLayout::CmykA, ChannelLayout::Lab, ChannelLayout::LabA] {
let img = Image::new(1, 1, layout, SampleType::U8).unwrap();
assert!(matches!(encode(&img), Err(CodecError::Encode { format: Format::Tga, .. })), "{layout:?}");
}
}
#[test]
fn dimensions_fit_the_unsigned_16_bit_header() {
for (width, height) in [(65535, 1), (1, 65535)] {
let img = Image::new(width, height, ChannelLayout::Gray, SampleType::U8).unwrap();
let file = encode(&img).unwrap();
assert_eq!(&file[12..14], &(width as u16).to_le_bytes());
assert_eq!(&file[14..16], &(height as u16).to_le_bytes());
}
for (width, height) in [(65536, 1), (1, 65536)] {
let img = Image::new(width, height, ChannelLayout::Gray, SampleType::U8).unwrap();
assert!(matches!(encode(&img), Err(CodecError::Encode { format: Format::Tga, .. })), "{width}x{height}");
}
}
#[test]
fn invalid_buffers_are_rejected_before_targa_encoding() {
// Image keeps its buffer private, so malformed buffers must fail at construction.
for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] {
let len = 2 * 2 * layout.channels();
for bytes in [len - 1, len + 1] {
assert!(matches!(Image::from_u8(2, 2, layout, vec![0; bytes]), Err(CodecError::InvalidImage(_))), "{layout:?}: {bytes} bytes");
}
}
}
let (w, h) = img.dimensions();
craft_tga::encode(w, h, layout, img.data()).map_err(|e| match e {
craft_tga::Error::Encode(m) if m == "buffer size mismatch" => CodecError::InvalidImage(m),
e => CodecError::encode(Format::Tga, e),
})
}
+11 -57
View File
@@ -231,75 +231,29 @@ fn bmp_is_rle(b: &[u8]) -> bool {
u32_at(14).is_some_and(|n| n >= 40) && matches!(u32_at(30), Some(1 | 2))
}
/// The blocks of a GIF (craft-gif), every frame recorded. The frame list is bounded by the file:
/// each image descriptor takes at least 11 of its bytes.
fn gif_scan(b: &[u8]) -> Option<craft_gif::Info> {
craft_gif::scan(b, usize::MAX)
}
/// The width and height of a GIF's first image descriptor, if the blocks reach one.
fn gif_first_frame(b: &[u8]) -> Option<(u32, u32)> {
let mut at = None;
gif_walk(b, |i| {
at = Some(i);
false
});
let u16_at = |at: usize| b.get(at..at + 2).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes);
// Descriptor: separator, left, top, width, height (little-endian u16s), packed fields.
let i = at?;
Some((u32::from(u16_at(i + 5)?), u32::from(u16_at(i + 7)?)))
craft_gif::scan(b, 1)?.frames.first().map(|f| (u32::from(f.width), u32::from(f.height)))
}
/// Counts a GIF's frames (image descriptors) by walking its blocks, without decompressing any.
/// The total is unknown when the blocks end before the trailer or break off.
pub(crate) fn gif_more_frames(b: &[u8]) -> Option<DecodeWarning> {
let (frames, total) = gif_walk(b, |_| true);
(frames > 1).then_some(DecodeWarning::MoreFrames { total })
let info = gif_scan(b)?;
let total = info.complete.then(|| u32::try_from(info.frames.len()).unwrap_or(u32::MAX));
(info.frames.len() > 1).then_some(DecodeWarning::MoreFrames { total })
}
/// Whether a GIF holds more than one frame, or None when the walk stops before the trailer
/// after at most one frame (so frames past the break could not be counted).
pub(crate) fn gif_has_more_frames(b: &[u8]) -> Option<bool> {
let (frames, total) = gif_walk(b, |_| true);
if frames > 1 { Some(true) } else { total.map(|_| false) }
}
/// Walks a GIF's blocks without decompressing any, handing `frame` the offset of each image
/// descriptor until it returns false. Gives the descriptors seen, and the frame count if the
/// blocks reached the trailer.
fn gif_walk(b: &[u8], mut frame: impl FnMut(usize) -> bool) -> (u32, Option<u32>) {
// Size of the colour table a packed-fields byte announces.
let table = |flags: u8| if flags & 0x80 != 0 { 3usize << ((flags & 7) + 1) } else { 0 };
// Skips the data sub-blocks starting at `i` (each length-prefixed, ended by a zero length).
let skip = |mut i: usize| -> Option<usize> {
loop {
let n = usize::from(*b.get(i)?);
i = i.checked_add(1 + n)?;
if n == 0 {
return Some(i);
}
}
};
// Header (6 bytes), logical screen descriptor (7) and the global colour table.
let Some(&flags) = b.get(10) else {
return (0, None);
};
let mut i = 13 + table(flags);
let mut frames = 0u32;
let total = loop {
let next = match b.get(i) {
Some(0x3B) => break Some(frames),
Some(0x21) => skip(i + 2),
Some(0x2C) => {
frames = frames.saturating_add(1);
if !frame(i) {
break None;
}
// Descriptor (10 bytes, packed fields last), local colour table, LZW code size.
b.get(i + 9).and_then(|&flags| skip(i + 11 + table(flags)))
}
_ => None,
};
match next {
Some(n) => i = n,
None => break None,
}
};
(frames, total)
gif_scan(b)?.has_more_frames()
}
pub(crate) fn from_dynamic(f: Format, d: DynamicImage) -> Result<Image, CodecError> {
+7 -2
View File
@@ -448,7 +448,9 @@ impl<'a> Encoder<'a> {
let whole_cost = best_sse + 3 * lambda;
// B_PRED: sub-blocks in raster order, each predicted from the reconstruction so far.
let saved_y = self.rec_y.data.clone();
// B_PRED writes only this macroblock's 16 rows: save those, not the whole plane.
let rows = |p: &Plane| -> [[u8; 16]; 16] { std::array::from_fn(|r| p.data[(y0 + r) * p.width + x0..][..16].try_into().unwrap_or([0; 16])) };
let saved_y = rows(&self.rec_y);
let mut b_modes = [B_DC_PRED; 16];
let mut b_blocks: Vec<Block> = Vec::with_capacity(16);
let mut b_sse = 0u64;
@@ -490,7 +492,10 @@ impl<'a> Encoder<'a> {
(B_PRED, None, b_blocks)
} else {
// Whole-block mode: undo the B_PRED reconstruction, code 16 blocks + Y2.
self.rec_y.data = saved_y;
let w = self.rec_y.width;
for (r, row) in saved_y.iter().enumerate() {
self.rec_y.data[(y0 + r) * w + x0..][..16].copy_from_slice(row);
}
let mut dcs = [0i32; 16];
let mut ac_blocks: Vec<[i32; 16]> = Vec::with_capacity(16);
let mut deq_blocks: Vec<[i32; 16]> = Vec::with_capacity(16);
-226
View File
@@ -1,226 +0,0 @@
//! X PixMap reader for XPM2 plain-text icons and XPM3 C-source icon arrays.
//! Supports transparency (None), RGB hex at 4/8/16 bits per channel and
//! common X11 colour names. Does not execute C code.
use std::collections::HashMap;
mod x11_colors;
use crate::SampleType;
use crate::error::CodecError;
use crate::format::Format;
use crate::image::{ChannelLayout, Image};
use crate::options::Limits;
const MAX_SOURCE: usize = 256 << 20;
const MAX_COLOURS: usize = 65_536;
const MAX_CPP: usize = 8;
fn bad(msg: impl std::fmt::Display) -> CodecError {
CodecError::malformed(Format::Xpm, msg)
}
/// Extract C string literals without treating quoted text or // in a string as a comment.
/// XPM uses one literal for the header, each palette entry and each row.
fn c_strings(bytes: &[u8]) -> Result<Vec<Vec<u8>>, CodecError> {
let mut lines = Vec::new();
let mut i = 0usize;
while i < bytes.len() {
if bytes[i..].starts_with(b"/*") {
let tail = &bytes[i + 2..];
let Some(end) = tail.windows(2).position(|p| p == b"*/") else { return Err(bad("unterminated C comment")) };
i += end + 4;
} else if bytes[i..].starts_with(b"//") {
i += bytes[i..].iter().position(|&c| c == b'\n').unwrap_or(bytes.len() - i);
} else if bytes[i] == b'"' {
i += 1;
let mut s = Vec::new();
let mut closed = false;
while i < bytes.len() {
let c = bytes[i];
i += 1;
match c {
b'"' => {
closed = true;
break;
}
b'\\' => {
let next = *bytes.get(i).ok_or_else(|| bad("truncated C escape"))?;
i += 1;
s.push(match next {
b'n' => b'\n',
b'r' => b'\r',
b't' => b'\t',
b'0' => 0,
_ => next, // quoted backslash or quote
});
}
_ => s.push(c),
}
}
if !closed {
return Err(bad("unterminated XPM string"));
}
lines.push(s);
} else {
i += 1;
}
}
Ok(lines)
}
/// Extract XPM2's plain-text lines without trimming pixel rows: leading and
/// trailing spaces can be palette keys, and physical CRLF is not image data.
fn xpm2_lines(bytes: &[u8]) -> Result<Vec<Vec<u8>>, CodecError> {
let mut input = bytes.split(|&b| b == b'\n');
let header = input.next().ok_or_else(|| bad("missing XPM2 marker"))?;
if header.strip_suffix(b"\r").unwrap_or(header) != b"! XPM2" {
return Err(bad("not an XPM2 image"));
}
let mut lines = Vec::new();
for raw in input {
let line = raw.strip_suffix(b"\r").unwrap_or(raw);
// The header may be preceded by comments and blank lines. After the
// header, a leading '!' is allowed as a palette key or pixel value.
if lines.is_empty() && (line.is_empty() || line.starts_with(b"!")) {
continue;
}
lines.push(line.to_vec());
}
Ok(lines)
}
fn channel(hex: &str) -> Option<u8> {
let v = u32::from_str_radix(hex, 16).ok()?;
let max = (1u32 << (hex.len() * 4)) - 1;
Some(((v * 255 + max / 2) / max) as u8)
}
fn named(name: &str) -> Option<[u8; 3]> {
let s = name.to_ascii_lowercase().replace([' ', '-', '_'], "");
// XPM imports X11 names, whose RGB values differ from CSS for names
// such as gray, maroon and purple. Keep these exact rather than guessing.
if let Ok(index) = x11_colors::X11_COLORS.binary_search_by_key(&s.as_str(), |(name, _)| *name) {
return Some(x11_colors::X11_COLORS[index].1);
}
// Retain useful web color aliases not present in the X11 database.
Some(match s.as_str() {
"silver" => [192, 192, 192],
"olive" => [128, 128, 0],
"teal" => [0, 128, 128],
"fuchsia" => [255, 0, 255],
"navyblue" => [0, 0, 128],
_ => return None,
})
}
fn color(name: &str) -> Option<[u8; 4]> {
if name.eq_ignore_ascii_case("none") {
return Some([0, 0, 0, 0]);
}
let rgb = if let Some(hex) = name.strip_prefix('#') {
// UTF-8 byte lengths can match a supported hex width while slicing would panic.
// `from_str_radix` would also take a leading `+`: accept hex digits only.
if !hex.bytes().all(|b| b.is_ascii_hexdigit()) {
return None;
}
let digits = match hex.len() {
3 => 1,
6 => 2,
12 => 4,
_ => return None,
};
let rgb = (0..3).map(|i| channel(&hex[i * digits..(i + 1) * digits])).collect::<Option<Vec<_>>>()?;
[rgb[0], rgb[1], rgb[2]]
} else {
named(name)?
};
Some([rgb[0], rgb[1], rgb[2], 255])
}
pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
if bytes.len() > MAX_SOURCE {
return Err(CodecError::LimitExceeded("XPM source exceeds 256 MiB".into()));
}
let lines = if bytes.starts_with(b"/* XPM */") {
c_strings(bytes)?
} else if bytes.starts_with(b"! XPM2\n") || bytes.starts_with(b"! XPM2\r\n") {
xpm2_lines(bytes)?
} else {
return Err(bad("not an XPM2/XPM3 image"));
};
decode_lines(&lines, limits)
}
fn decode_lines(lines: &[Vec<u8>], limits: &Limits) -> Result<Image, CodecError> {
let header = lines.first().ok_or_else(|| bad("missing image header"))?;
let parts = std::str::from_utf8(header).map_err(|_| bad("non-UTF-8 XPM header"))?;
let mut parts = parts.split_ascii_whitespace();
let number = |key: &str, s: Option<&str>| -> Result<usize, CodecError> {
s.ok_or_else(|| bad(format!("missing {key}")))?.parse::<usize>().map_err(|_| bad(format!("invalid {key}")))
};
let width = number("width", parts.next())?;
let height = number("height", parts.next())?;
let colours = number("palette size", parts.next())?;
let cpp = number("chars per pixel", parts.next())?;
if width == 0 || height == 0 || colours == 0 || colours > MAX_COLOURS || !(1..=MAX_CPP).contains(&cpp) {
return Err(bad("invalid XPM dimensions, palette size or chars per pixel"));
}
let (w, h) = (u32::try_from(width).map_err(|_| bad("width overflow"))?, u32::try_from(height).map_err(|_| bad("height overflow"))?);
limits.check(w, h, ChannelLayout::Rgba, SampleType::U8)?;
let required = 1usize.checked_add(colours).and_then(|n| n.checked_add(height)).ok_or_else(|| bad("row count overflow"))?;
if lines.len() < required {
return Err(bad("truncated palette or image rows"));
}
let mut palette = HashMap::<Vec<u8>, [u8; 4]>::with_capacity(colours);
for line in &lines[1..1 + colours] {
if line.len() < cpp {
return Err(bad("truncated colour key"));
}
let fields = std::str::from_utf8(&line[cpp..]).map_err(|_| bad("invalid colour name"))?;
// XPM priority is colour, gray, four-level gray, then monochrome,
// regardless of the order of keys in the palette entry.
let terms: Vec<&str> = fields.split_ascii_whitespace().collect();
let mut picked: Option<(u8, String)> = None;
let mut i = 0;
while i < terms.len() {
let priority = match terms[i] {
"c" => 4,
"g" => 3,
"g4" => 2,
"m" => 1,
_ => 0, // Symbolic names ('s') are not colour values.
};
let start = i + 1;
i = start;
while i < terms.len() && !matches!(terms[i], "c" | "g" | "g4" | "m" | "s") {
i += 1;
}
if priority > 0 && i > start && picked.as_ref().is_none_or(|(current, _)| priority > *current) {
picked = Some((priority, terms[start..i].join(" ")));
}
}
let value = picked.as_ref().and_then(|(_, name)| color(name)).ok_or_else(|| bad("unsupported or missing XPM colour"))?;
if palette.insert(line[..cpp].to_vec(), value).is_some() {
return Err(bad("duplicate XPM palette key"));
}
}
let row_len = width.checked_mul(cpp).ok_or_else(|| bad("row byte count overflow"))?;
// Validate all source rows before reserving the decoded RGBA image. A malformed
// short row must not be able to make a large header-sized allocation first.
if lines[1 + colours..required].iter().any(|line| line.len() != row_len) {
return Err(bad("XPM row has the wrong width"));
}
let total = width.checked_mul(height).and_then(|n| n.checked_mul(4)).ok_or_else(|| bad("pixel size overflow"))?;
let mut pixels = Vec::new();
pixels.try_reserve_exact(total).map_err(|_| bad("XPM pixel buffer is too large"))?;
pixels.resize(total, 0);
for (y, line) in lines[1 + colours..required].iter().enumerate() {
for x in 0..width {
let key = &line[x * cpp..(x + 1) * cpp];
let rgba = palette.get(key).ok_or_else(|| bad("unknown palette key"))?;
pixels[(y * width + x) * 4..(y * width + x + 1) * 4].copy_from_slice(rgba);
}
}
Image::from_u8(w, h, ChannelLayout::Rgba, pixels)
}
Loaded 100 of 882 files, more files were not shown because too many files have changed in this diff. Show more