mirror of
https://github.com/storytold/deckcraft.git
synced 2026-11-18 11:54:00 -06:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce4e60399d | ||
|
|
61610aa7dc |
@@ -1,31 +0,0 @@
|
||||
# This file is machine-generated by `gh actions-lock`.
|
||||
# Do not edit by hand; run `gh actions-lock` to update.
|
||||
# Docs: https://gh.io/actions-lockfile
|
||||
version: 'v0.0.3'
|
||||
workflows:
|
||||
'.github/workflows/ci.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/freebsd.yml': []
|
||||
'.github/workflows/house-rules.yml': []
|
||||
'.github/workflows/lockfile.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/nix.yml': []
|
||||
'.github/workflows/packaging-lint.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/release.yml': []
|
||||
'.github/workflows/sonarcloud.yml': []
|
||||
'.github/workflows/windows-arm64.yml': []
|
||||
dependencies:
|
||||
'actions/checkout@v7.0.1':
|
||||
ref: 'v7.0.1'
|
||||
commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1'
|
||||
owner_id: 44036562
|
||||
repo_id: 197814629
|
||||
'step-security/harden-runner@v2.22.4':
|
||||
ref: 'v2.22.4'
|
||||
commit: 'sha1-1d88384fd173af6ecb012dfd0264a306b711241a'
|
||||
owner_id: 88700172
|
||||
repo_id: 422287306
|
||||
@@ -27,11 +27,11 @@ jobs:
|
||||
CRAFT_FONTS_REF: 37827adc41fd165f9a9234ac40f94dbad353aa69
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- name: Check out craft-fonts
|
||||
uses: actions/checkout@v7.0.1
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
repository: storytold/craft-fonts
|
||||
ref: ${{ env.CRAFT_FONTS_REF }}
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
name: Lockfile
|
||||
|
||||
# Fails when .github/workflows/actions.lock is out of sync with the workflows, or when a locked pin
|
||||
# no longer verifies upstream, so a moved tag cannot slip a different commit into a run.
|
||||
# Docs: https://gh.io/actions-lockfile
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches: [main]
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
|
||||
jobs:
|
||||
lockfile:
|
||||
name: Lockfile is in sync
|
||||
runs-on: ubuntu-24.04
|
||||
timeout-minutes: 10
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- name: Install gh-actions-lock
|
||||
run: gh extension install github/gh-actions-lock --pin v0.4.1
|
||||
- name: Verify the lockfile
|
||||
run: gh actions-lock --verify
|
||||
@@ -28,10 +28,10 @@ jobs:
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- name: Install the pinned packaging toolchain
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
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: "16 1 * * *"
|
||||
push:
|
||||
branches: [release]
|
||||
workflow_dispatch:
|
||||
@@ -34,7 +30,6 @@ 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: deckcraft
|
||||
app-title: DeckCraft
|
||||
# Keep identical to ci.yml and freebsd.yml. The shared release workflow sets
|
||||
|
||||
@@ -18,7 +18,4 @@ jobs:
|
||||
sonarcloud:
|
||||
# Tracked at @main so org-wide CI improvements propagate without PR churn.
|
||||
uses: storytold/craft-common-actions/.github/workflows/sonarcloud.yml@main
|
||||
with:
|
||||
# Keep coverage tests on the same reviewed, immutable font set as the main gate.
|
||||
fonts-ref: 37827adc41fd165f9a9234ac40f94dbad353aa69
|
||||
secrets: inherit
|
||||
|
||||
@@ -239,7 +239,3 @@ the `ROADMAP.md` progress log, and update each touched doc's status line and rev
|
||||
- Workflows in `.github/workflows/` call the shared workflows in [`storytold/craft-common-actions`](https://github.com/storytold/craft-common-actions) at `@main`, with their inputs and secrets. No other jobs: if no shared workflow does what's needed, it doesn't go here.
|
||||
- Don't add or change CI in a feature or fix PR. If the existing workflows can't build or test a change, link an existing craft-common-actions issue or PR, or open a new issue, or a PR only for a small fix that is safe for every app and does not increase CI time.
|
||||
- Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it.
|
||||
|
||||
## Shared libraries
|
||||
|
||||
Before adding or changing file-format, codec, protocol, or reusable UI code, check `storytold/craft-libs` and its open PRs first; reuse or extend the shared library rather than duplicating it locally, keep app-specific policy and model mapping in the app, and preserve behavior, security/resource limits, attribution, and regression coverage during cutovers.
|
||||
|
||||
@@ -35,7 +35,6 @@ artwork. The ribbon and menu icons stay original drawings.
|
||||
| `crates/fonts/tests/fixtures/legacy-semibold.ttf` | DeckCraft contributors | a one-glyph Semibold face named like Segoe UI Semibold (its Windows family differs from the typographic one), built by `make_legacy_font.py` (fontTools as the builder only) | CC0-1.0 | Font lookup by Windows family name tests |
|
||||
| `crates/fonts/tests/fixtures/legacy-regular.ttf` | DeckCraft contributors | the same family's one-glyph Regular face, built by `make_legacy_font.py` (fontTools as the builder only) | CC0-1.0 | Font lookup by Windows family name tests |
|
||||
| `crates/render/tests/fixtures/make_colr_font.py` | DeckCraft contributors | this repository | MIT OR Apache-2.0 | Regenerates the COLR test font |
|
||||
| `crates/format/tests/golden/deck.deckcraft` | DeckCraft contributors | a synthetic deck (placeholder bytes, no images or text from elsewhere) written by `crates/format/src/golden_tests.rs` with `DECKCRAFT_BLESS_GOLDEN=1` | CC0-1.0 | Native format golden bytes |
|
||||
| Lucide icon paths in `crates/engine/src/icon_data.rs` (Insert › Icons) | Lucide Contributors; Feather icons by Cole Bemis | https://github.com/lucide-icons/lucide/tree/440a1e1dfb1e712505308da46bead3c28b1f1398/icons (release 1.56.0, 2026-10-13) | ISC; MIT for the Feather-derived icons named in that commit's LICENSE | Stroke icons inserted as shapes |
|
||||
|
||||
## Bundled through dependencies
|
||||
|
||||
Generated
+120
-206
@@ -222,7 +222,7 @@ dependencies = [
|
||||
"objc2-foundation 0.3.2",
|
||||
"parking_lot",
|
||||
"percent-encoding",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.61.2",
|
||||
"wl-clipboard-rs",
|
||||
"x11rb",
|
||||
]
|
||||
@@ -844,73 +844,15 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-audio"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
dependencies = [
|
||||
"craft-media",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-cfb"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10"
|
||||
|
||||
[[package]]
|
||||
name = "craft-codepage"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=254e9a409b410dc93c0e06ff66cd1e767893ac32#254e9a409b410dc93c0e06ff66cd1e767893ac32"
|
||||
|
||||
[[package]]
|
||||
name = "craft-codepage"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
|
||||
[[package]]
|
||||
name = "craft-deckcraft-format"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=254e9a409b410dc93c0e06ff66cd1e767893ac32#254e9a409b410dc93c0e06ff66cd1e767893ac32"
|
||||
dependencies = [
|
||||
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=254e9a409b410dc93c0e06ff66cd1e767893ac32)",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
"zip 9.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-drawingml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=44d83fa8f635333dcb3c231f87381387a1f5631b#44d83fa8f635333dcb3c231f87381387a1f5631b"
|
||||
dependencies = [
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=44d83fa8f635333dcb3c231f87381387a1f5631b)",
|
||||
"kurbo",
|
||||
"log",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-drawingml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=cff5af869dbf6926801c9ac63a927d155202ba3b#cff5af869dbf6926801c9ac63a927d155202ba3b"
|
||||
dependencies = [
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cff5af869dbf6926801c9ac63a927d155202ba3b)",
|
||||
"kurbo",
|
||||
"log",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-fonts-build"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
|
||||
[[package]]
|
||||
name = "craft-i18n"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
dependencies = [
|
||||
"fixed_decimal",
|
||||
"icu_decimal",
|
||||
@@ -933,65 +875,24 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-media"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-metafile"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"image",
|
||||
"kurbo",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-mime"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=254e9a409b410dc93c0e06ff66cd1e767893ac32#254e9a409b410dc93c0e06ff66cd1e767893ac32"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=254e9a409b410dc93c0e06ff66cd1e767893ac32)",
|
||||
"encoding_rs",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-opc"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10"
|
||||
dependencies = [
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10)",
|
||||
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10)",
|
||||
"log",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-pdf"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
dependencies = [
|
||||
"krilla",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-rtf"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
dependencies = [
|
||||
"craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725)",
|
||||
"encoding_rs",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ui"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
dependencies = [
|
||||
"craft-fonts-build",
|
||||
"craft-i18n",
|
||||
@@ -1011,7 +912,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-update"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dunce",
|
||||
@@ -1035,16 +936,6 @@ dependencies = [
|
||||
"zip 9.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-video"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
dependencies = [
|
||||
"craft-media",
|
||||
"rayon",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-web-api"
|
||||
version = "0.1.0"
|
||||
@@ -1065,55 +956,6 @@ dependencies = [
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=44d83fa8f635333dcb3c231f87381387a1f5631b#44d83fa8f635333dcb3c231f87381387a1f5631b"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=cff5af869dbf6926801c9ac63a927d155202ba3b#cff5af869dbf6926801c9ac63a927d155202ba3b"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=254e9a409b410dc93c0e06ff66cd1e767893ac32#254e9a409b410dc93c0e06ff66cd1e767893ac32"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"thiserror 2.0.21",
|
||||
"zip 9.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"thiserror 2.0.21",
|
||||
"zip 9.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.5.2"
|
||||
@@ -1187,7 +1029,7 @@ checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f"
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"arboard",
|
||||
"chrono",
|
||||
@@ -1209,16 +1051,32 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-anim"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"deckcraft-geom",
|
||||
"deckcraft-model",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-av1"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"deckcraft-bitstream",
|
||||
"rayon",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-bitstream"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"craft-media",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-cli"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"deckcraft-engine",
|
||||
"deckcraft-mcp",
|
||||
@@ -1228,17 +1086,16 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-color"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=44d83fa8f635333dcb3c231f87381387a1f5631b)",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-engine"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-mime",
|
||||
"deckcraft-anim",
|
||||
"deckcraft-color",
|
||||
"deckcraft-fonts",
|
||||
@@ -1262,7 +1119,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-fonts"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"epaint_default_fonts",
|
||||
"harfrust 0.13.3",
|
||||
@@ -1274,27 +1131,58 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-format"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"craft-deckcraft-format",
|
||||
"craft-rtf",
|
||||
"deckcraft-model",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
"zip 3.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-geom"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=44d83fa8f635333dcb3c231f87381387a1f5631b)",
|
||||
"kurbo",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-h264"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"deckcraft-bitstream",
|
||||
"rayon",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-hevc"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"deckcraft-bitstream",
|
||||
"rayon",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-isobmff"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"craft-media",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-matroska"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"craft-media",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-mcp"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"deckcraft-engine",
|
||||
"deckcraft-render",
|
||||
@@ -1304,20 +1192,34 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-media"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"craft-audio",
|
||||
"craft-media",
|
||||
"craft-video",
|
||||
"deckcraft-av1",
|
||||
"deckcraft-h264",
|
||||
"deckcraft-hevc",
|
||||
"deckcraft-isobmff",
|
||||
"deckcraft-matroska",
|
||||
"deckcraft-ogg",
|
||||
"deckcraft-opus",
|
||||
"deckcraft-vp9",
|
||||
"image",
|
||||
"log",
|
||||
"symphonia",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-metafile"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"image",
|
||||
"kurbo",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-model"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"deckcraft-color",
|
||||
"deckcraft-geom",
|
||||
@@ -1327,9 +1229,17 @@ dependencies = [
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-ogg"
|
||||
version = "0.18.0"
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-opus"
|
||||
version = "0.18.0"
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-pdf"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"craft-pdf",
|
||||
"deckcraft-fonts",
|
||||
@@ -1347,13 +1257,8 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-pptx"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"craft-cfb",
|
||||
"craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=44d83fa8f635333dcb3c231f87381387a1f5631b)",
|
||||
"craft-drawingml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cff5af869dbf6926801c9ac63a927d155202ba3b)",
|
||||
"craft-opc",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10)",
|
||||
"deckcraft-color",
|
||||
"deckcraft-engine",
|
||||
"deckcraft-geom",
|
||||
@@ -1368,13 +1273,13 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-render"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"craft-metafile",
|
||||
"deckcraft-anim",
|
||||
"deckcraft-color",
|
||||
"deckcraft-fonts",
|
||||
"deckcraft-geom",
|
||||
"deckcraft-metafile",
|
||||
"deckcraft-model",
|
||||
"deckcraft-text",
|
||||
"image",
|
||||
@@ -1387,7 +1292,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-text"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"deckcraft-color",
|
||||
@@ -1401,7 +1306,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-ui-egui"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"craft-ui",
|
||||
"craft-update",
|
||||
@@ -1425,9 +1330,18 @@ dependencies = [
|
||||
"web-time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-vp9"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"deckcraft-bitstream",
|
||||
"rayon",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deckcraft-web"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"craft-web-api",
|
||||
"craft-web-host",
|
||||
@@ -1822,7 +1736,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2878,7 +2792,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "krilla"
|
||||
version = "0.8.2"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"bumpalo",
|
||||
@@ -3184,7 +3098,7 @@ version = "0.50.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
|
||||
dependencies = [
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3657,7 +3571,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.45.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4212,7 +4126,7 @@ dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys 0.12.1",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4269,7 +4183,7 @@ dependencies = [
|
||||
"security-framework",
|
||||
"security-framework-sys",
|
||||
"webpki-root-certs",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4940,10 +4854,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
|
||||
dependencies = [
|
||||
"fastrand",
|
||||
"getrandom 0.3.4",
|
||||
"getrandom 0.4.3",
|
||||
"once_cell",
|
||||
"rustix 1.1.5",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5894,7 +5808,7 @@ version = "0.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||
dependencies = [
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -6412,7 +6326,7 @@ checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee"
|
||||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
+17
-27
@@ -4,26 +4,17 @@ members = ["crates/*", "apps/*", "xtask"]
|
||||
default-members = ["crates/*", "apps/deckcraft", "apps/deckcraft-cli"]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.19.0"
|
||||
version = "0.18.0"
|
||||
edition = "2024"
|
||||
license = "MIT OR Apache-2.0"
|
||||
rust-version = "1.95"
|
||||
repository = "https://github.com/storytold/deckcraft"
|
||||
|
||||
[workspace.dependencies]
|
||||
# OPC part-name primitives; relationship admission stays app-local.
|
||||
craft-opc = { git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10", version = "0.1.0" }
|
||||
# Compound File Binary directory reads (password-protected deck detection).
|
||||
craft-cfb = { git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10", version = "0.1.0" }
|
||||
# XML writer primitives; parser admission and OOXML policy stay app-local.
|
||||
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10", version = "0.1.0" }
|
||||
# DrawingML colours, themes and text (craft-libs#778, #792): the model DeckCraft reads and writes.
|
||||
# Isolated table XML writer; existing DrawingML model/readers keep their revision.
|
||||
craft-drawingml-table = { package = "craft-drawingml", git = "https://github.com/storytold/craft-libs", rev = "cff5af869dbf6926801c9ac63a927d155202ba3b" }
|
||||
craft-drawingml = { git = "https://github.com/storytold/craft-libs", rev = "44d83fa8f635333dcb3c231f87381387a1f5631b", version = "0.1.0" }
|
||||
# internal
|
||||
deckcraft-geom = { path = "crates/geom" }
|
||||
deckcraft-color = { path = "crates/color" }
|
||||
deckcraft-metafile = { path = "crates/metafile" }
|
||||
deckcraft-model = { path = "crates/model" }
|
||||
deckcraft-fonts = { path = "crates/fonts" }
|
||||
deckcraft-text = { path = "crates/text" }
|
||||
@@ -36,6 +27,16 @@ deckcraft-engine = { path = "crates/engine" }
|
||||
deckcraft-mcp = { path = "crates/mcp" }
|
||||
deckcraft-ui-egui = { path = "crates/ui-egui" }
|
||||
deckcraft-media = { path = "crates/media" }
|
||||
# codecs (ported from FilmCraft, same owner and licence)
|
||||
deckcraft-bitstream = { path = "crates/bitstream" }
|
||||
deckcraft-h264 = { path = "crates/h264" }
|
||||
deckcraft-hevc = { path = "crates/hevc" }
|
||||
deckcraft-vp9 = { path = "crates/vp9" }
|
||||
deckcraft-av1 = { path = "crates/av1" }
|
||||
deckcraft-isobmff = { path = "crates/isobmff" }
|
||||
deckcraft-matroska = { path = "crates/matroska" }
|
||||
deckcraft-ogg = { path = "crates/ogg" }
|
||||
deckcraft-opus = { path = "crates/opus" }
|
||||
|
||||
# foundation
|
||||
kurbo = { version = "0.13", features = ["serde"] }
|
||||
@@ -58,22 +59,11 @@ harfrust = "0.13"
|
||||
vello_cpu = "0.2"
|
||||
vello_common = { version = "0.2", default-features = false }
|
||||
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] }
|
||||
# Base64 for command payloads (engine); base64_compat keeps the legacy lenient decoder. Same rev as craft-deckcraft-format.
|
||||
craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "254e9a409b410dc93c0e06ff66cd1e767893ac32", version = "=0.1.0" }
|
||||
# craft-media, craft-audio and craft-video share types and stay on one rev.
|
||||
# The .deckcraft package layer (identity, manifest, member names, bounded reads); crates/format maps the presentation onto it.
|
||||
craft-deckcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "254e9a409b410dc93c0e06ff66cd1e767893ac32", version = "=0.1.0" }
|
||||
craft-rtf = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0" }
|
||||
craft-media = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false }
|
||||
craft-audio = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false, features = ["opus-decode"] }
|
||||
# H.264, HEVC, VP9 and AV1 decoders.
|
||||
craft-video = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false, features = ["h264-decode", "hevc-decode", "vp9-decode", "av1-decode", "threads"] }
|
||||
# WMF, EMF and EMF+ reader.
|
||||
craft-metafile = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0" }
|
||||
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false, features = ["write", "write-simple-text"] }
|
||||
craft-media = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", version = "=0.1.0", default-features = false }
|
||||
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", version = "=0.1.0", default-features = false, features = ["write", "write-simple-text"] }
|
||||
# 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 = "63196e66f3033a6623eb629af95a104d26986725", features = ["egui"] }
|
||||
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0" }
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
|
||||
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", version = "=0.1.0" }
|
||||
i_overlay = "9"
|
||||
quick-xml = "0.41"
|
||||
zip = { version = "3", default-features = false, features = ["deflate"] }
|
||||
@@ -119,7 +109,7 @@ codegen-units = 16
|
||||
debug = 0
|
||||
|
||||
[patch.crates-io]
|
||||
krilla = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.8.2" }
|
||||
krilla = { git = "https://github.com/storytold/craft-libs", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", version = "=0.8.2" }
|
||||
ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
|
||||
emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
|
||||
epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
|
||||
|
||||
@@ -76,9 +76,8 @@ Code dependencies
|
||||
- symphonia (audio decoding for MP3, AAC, ALAC, FLAC, Vorbis, WAV, AIFF, CAF) is used unmodified
|
||||
under the Mozilla Public License 2.0; its source is available at
|
||||
https://github.com/pdeljanov/Symphonia.
|
||||
- The H.264, HEVC, VP9 and AV1 crates under crates/ are ported from FilmCraft (same owner,
|
||||
MIT OR Apache-2.0). The Opus decoder (craft-audio) and the MP4, Matroska and Ogg demuxers
|
||||
(craft-media) come from craft-libs (same owner, MIT OR Apache-2.0).
|
||||
- The H.264, HEVC, VP9, AV1, Opus, MP4, Matroska and Ogg crates under crates/ are ported from
|
||||
FilmCraft (same owner, MIT OR Apache-2.0).
|
||||
|
||||
- Krilla 0.8.2 (https://github.com/LaurenzV/krilla) is used through craft-pdf under
|
||||
MIT OR Apache-2.0. craft-libs vendors that version with page/surface unwind recovery
|
||||
|
||||
+1
-6
@@ -2,7 +2,7 @@
|
||||
|
||||
**Stage: alpha** · next: beta, ~20 points and ~120–210 h away
|
||||
|
||||
> **Last reviewed:** 2026-11-04 · **Last updated:** 2026-11-14 · **Change:** trivial (a slide the show advances to again plays its animations again, #2090; slide-list entries with missing relationships are reported as lost slides and open as repaired copies; the Opus decoder and the Ogg, MP4 and Matroska demuxers come from craft-libs; parity scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
|
||||
> **Last reviewed:** 2026-11-04 · **Last updated:** 2026-11-04 · **Change:** trivial (Ogg Opus recovery includes trailing packets without granules in its reported duration; parity scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
|
||||
> **Last reviewed:** 2026-11-02 · **Last updated:** 2026-11-03 · **Change:** trivial (progress log: web `?host=parent` bridge opens and saves through a same-origin framing page; scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
|
||||
|
||||
> **Last reviewed:** 2026-11-02 · **Last updated:** 2026-11-03 · **Change:** trivial (community PR wave 10: PPTX and native saves keep table cell centred anchoring, text overflow, compatible line spacing, normalised character height, Thai distributed alignment, RTL table direction, alternate run language and reflection transforms; imported effects placed by parent time-node delays; Reuse Slides fits the destination size and brings custom table styles; deleted slides leave custom shows; merged shapes drop their animations; Compress Pictures measures grouped pictures at displayed size; parity scores unchanged) · **Target:** Microsoft PowerPoint for Mac 16.113 (Microsoft 365)
|
||||
@@ -154,9 +154,6 @@ app's JSON control channel (`deckcraft --control PORT`).
|
||||
|
||||
| Date | What landed |
|
||||
|---|---|
|
||||
| 2026-11-14 | Slide show: a slide the show advances to again (click, →, Space, automatic advance or a loop) starts over, so its animations play again, as in PowerPoint. Back and links still show it as it was left (#2090) |
|
||||
| 2026-11-11 | Media: the Opus decoder and the Ogg, MP4/MOV and Matroska demuxers are craft-audio and craft-media from craft-libs; DeckCraft's own copies and re-export crates are removed |
|
||||
| 2026-11-07 | PPTX slide-list entries with missing relationships are reported in lostSlides instead of silently omitted. The engine opens the partial deck as a repaired copy with no original save path. Slide lists over 10,000 entries are explicitly refused before a partial load; exactly 10,000 remain supported. Synthetic 13-slide regression; does not establish the cause of #1910's field report. |
|
||||
| 2026-11-04 | Ogg Opus recovery keeps trailing packets without granules in the total duration, matching their recovered positions; valid end trimming stays covered |
|
||||
| 2026-11-03 | Web: the `?host=parent` bridge (as in GridCraft, WordCraft and PdfCraft) lets a same-origin page that frames DeckCraft send a presentation and store its saves; Save writes an opened .pptx back as .pptx under its own name, as on the desktop |
|
||||
| 2026-11-02 | Slide show on Linux and the BSDs: Presenter View stays off when winit lists exactly one display, as in PowerPoint; if it lists none, the setting is kept. On one display the presenter window covered the show, and the hidden window stalled every frame: slide changes took up to ~1.7 s under Hyprland (#1156) |
|
||||
@@ -343,8 +340,6 @@ app's JSON control channel (`deckcraft --control PORT`).
|
||||
|
||||
| Date | Change | Summary |
|
||||
|---|---|---|
|
||||
| 2026-11-14 | trivial | Progress log: a slide the show advances to again plays its animations again (#2090); parity scores unchanged |
|
||||
| 2026-11-11 | trivial | Opus and container code moved to craft-libs crates; parity scores unchanged |
|
||||
| 2026-11-04 | trivial | Ogg Opus recovery duration includes its recovered tail; parity scores unchanged |
|
||||
| 2026-11-02 | trivial | Progress log: run alternate-language metadata round-trips through native and PPTX formats; product parity scores unchanged |
|
||||
| 2026-11-03 | trivial | Reuse Slides brings referenced custom table styles along; scores unchanged |
|
||||
|
||||
@@ -148,9 +148,6 @@ fn warn_if_damaged(path: &str, opened: &serde_json::Value) {
|
||||
if slides > 0 {
|
||||
eprintln!("deckcraft-cli: warning: {path}: {slides} slide{} couldn't be read and left out", if slides == 1 { "" } else { "s" });
|
||||
}
|
||||
for w in opened.get("importWarnings").and_then(|v| v.as_array()).into_iter().flatten().filter_map(|w| w.as_str()) {
|
||||
eprintln!("deckcraft-cli: warning: {path}: {w}");
|
||||
}
|
||||
if media > 0 {
|
||||
eprintln!("deckcraft-cli: warning: {path}: {media} picture or media file{} couldn't be read", if media == 1 { "" } else { "s" });
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::process::{Command, Output};
|
||||
|
||||
fn cli(args: &[&str]) -> Output {
|
||||
Command::new(crate::deckcraft_cli()).args(args).output().unwrap()
|
||||
Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).args(args).output().unwrap()
|
||||
}
|
||||
|
||||
fn stdout(o: &Output) -> String {
|
||||
@@ -197,7 +197,7 @@ fn every_subcommand_answers_help_and_does_nothing_else() {
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
for sub in SUBCOMMANDS {
|
||||
for args in [vec![sub, "--help"], vec![sub, "-h", "out.png"], vec!["help", sub]] {
|
||||
let o = Command::new(crate::deckcraft_cli()).current_dir(&dir).args(&args).output().unwrap();
|
||||
let o = Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).current_dir(&dir).args(&args).output().unwrap();
|
||||
assert!(stdout(&o).starts_with(&format!("usage: deckcraft-cli {sub}")), "{args:?}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ fn closed_stdout() -> Stdio {
|
||||
}
|
||||
|
||||
fn run(dir: &Path, args: &[&str], stdout: Stdio) -> Output {
|
||||
Command::new(crate::deckcraft_cli()).current_dir(dir).args(args).stdout(stdout).stderr(Stdio::piped()).output().unwrap()
|
||||
Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).current_dir(dir).args(args).stdout(stdout).stderr(Stdio::piped()).output().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -52,7 +52,7 @@ fn broken_pipe_does_not_skip_later_commands_or_save() {
|
||||
assert!(out.status.success(), "exit {:?}: {}", out.status, String::from_utf8_lossy(&out.stderr));
|
||||
let saved = dir.0.join("out.deckcraft");
|
||||
assert!(saved.is_file(), "requested save was skipped");
|
||||
let info = Command::new(crate::deckcraft_cli()).arg("info").arg(saved).output().unwrap();
|
||||
let info = Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).arg("info").arg(saved).output().unwrap();
|
||||
assert!(info.status.success(), "{}", String::from_utf8_lossy(&info.stderr));
|
||||
let data: serde_json::Value = serde_json::from_slice(&info.stdout).unwrap();
|
||||
let slides = data["slides"].as_array().unwrap();
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Output};
|
||||
|
||||
fn cli(args: &[&Path]) -> Output {
|
||||
Command::new(crate::deckcraft_cli()).args(args).output().unwrap()
|
||||
Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).args(args).output().unwrap()
|
||||
}
|
||||
|
||||
fn temp_dir(name: &str) -> PathBuf {
|
||||
@@ -18,7 +18,7 @@ fn temp_dir(name: &str) -> PathBuf {
|
||||
fn convert_rejects_output_formats_it_cannot_write() {
|
||||
let dir = temp_dir("convert-unsupported");
|
||||
let input = dir.join("in.pptx");
|
||||
let out = Command::new(crate::deckcraft_cli()).args(["run", "--sample", "--export"]).arg(&input).output().unwrap();
|
||||
let out = Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).args(["run", "--sample", "--export"]).arg(&input).output().unwrap();
|
||||
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
|
||||
|
||||
for name in ["out.odp", "out.bmp", "out.svg", "out.outline", "out"] {
|
||||
@@ -48,7 +48,7 @@ fn run_rejects_unsupported_save_and_export_paths_before_running() {
|
||||
let dir = temp_dir("run-unsupported");
|
||||
for flag in ["--save", "--export"] {
|
||||
let target = dir.join("deck.odp");
|
||||
let out = Command::new(crate::deckcraft_cli()).args(["run", "--sample", flag]).arg(&target).output().unwrap();
|
||||
let out = Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).args(["run", "--sample", flag]).arg(&target).output().unwrap();
|
||||
assert!(!out.status.success(), "{flag}: run should fail");
|
||||
assert!(String::from_utf8_lossy(&out.stderr).contains("unsupported output format .odp"), "{flag}");
|
||||
assert!(!target.exists(), "{flag}: nothing is written");
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::path::PathBuf;
|
||||
use std::process::{Command, Output};
|
||||
|
||||
fn cli(args: &[&str]) -> Output {
|
||||
Command::new(crate::deckcraft_cli()).args(args).output().unwrap()
|
||||
Command::new(env!("CARGO_BIN_EXE_deckcraft-cli")).args(args).output().unwrap()
|
||||
}
|
||||
|
||||
fn temp_dir(name: &str) -> PathBuf {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use std::ffi::OsString;
|
||||
|
||||
mod cli;
|
||||
#[cfg(unix)]
|
||||
mod closed_stdout;
|
||||
@@ -8,7 +6,3 @@ mod damaged;
|
||||
// Integration tests: unwrapping and panicking on failure is fine here, unlike in shipped code (AGENTS.md).
|
||||
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
mod mcp_roots;
|
||||
|
||||
fn deckcraft_cli() -> OsString {
|
||||
std::env::var_os("NEXTEST_BIN_EXE_deckcraft_cli").unwrap_or_else(|| env!("CARGO_BIN_EXE_deckcraft-cli").into())
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use std::process::{Command, Output, Stdio};
|
||||
use serde_json::Value;
|
||||
|
||||
fn bin() -> Command {
|
||||
Command::new(crate::deckcraft_cli())
|
||||
Command::new(env!("CARGO_BIN_EXE_deckcraft-cli"))
|
||||
}
|
||||
|
||||
fn run(args: &[&str]) -> Output {
|
||||
|
||||
@@ -73,7 +73,3 @@ sync_github_name = true
|
||||
display_name = "Arshiya"
|
||||
real_name = "Arshiya Rahgozar"
|
||||
sync_github_name = false
|
||||
|
||||
[people.adikshan11]
|
||||
real_name = "Adithya Shankaran"
|
||||
sync_github_name = true
|
||||
|
||||
@@ -9,8 +9,7 @@
|
||||
//! - a shape with a link and a triggered animation follows the link at once. The animation's clock
|
||||
//! runs only while its slide is shown, so it goes on where it stopped when the show returns;
|
||||
//! - with "advance on click" off a click never leaves the slide, but it still plays animation steps;
|
||||
//! - a slide the show returns to by a link or by going back looks as it was left; going forward
|
||||
//! to a slide starts it over (#2090).
|
||||
//! - a slide the show returns to (link or arrow keys) looks as it was left.
|
||||
|
||||
use deckcraft_model::text::Action;
|
||||
use deckcraft_model::{Presentation, ShapeId};
|
||||
|
||||
+3
-12
@@ -47,8 +47,7 @@ pub enum LinkJump {
|
||||
|
||||
/// Where a slide show is. Besides the current position it remembers what was played on every slide
|
||||
/// the show has visited (main steps, triggered sequences), so a slide reached again by a link or
|
||||
/// by going back looks the way it was left, as in PowerPoint. Going forward to a slide starts it
|
||||
/// over, so its animations play again (#2090).
|
||||
/// the arrow keys looks the way it was left, as in PowerPoint.
|
||||
#[derive(Clone, Debug, PartialEq, Default)]
|
||||
pub struct ShowState {
|
||||
/// Index into `presentation.slides`.
|
||||
@@ -227,14 +226,6 @@ impl ShowState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Advance to slide `i`: it starts over, as if the show had not been there, so its
|
||||
/// animations play again (#2090). Links and Back keep a slide as it was left.
|
||||
fn enter_forward(&mut self, pres: &Presentation, i: usize) {
|
||||
self.leave(pres);
|
||||
self.runs.remove(&i);
|
||||
self.enter(pres, i);
|
||||
}
|
||||
|
||||
fn next_index(&self, pres: &Presentation) -> Option<usize> {
|
||||
let order = self.visit(pres);
|
||||
match order.iter().position(|&i| i == self.slide) {
|
||||
@@ -280,12 +271,12 @@ impl ShowState {
|
||||
}
|
||||
match self.next_index(pres) {
|
||||
Some(i) => {
|
||||
self.enter_forward(pres, i);
|
||||
self.enter(pres, i);
|
||||
ShowAction::Slide(i)
|
||||
}
|
||||
None if looping(pres) => match self.visit(pres).first() {
|
||||
Some(&i) => {
|
||||
self.enter_forward(pres, i);
|
||||
self.enter(pres, i);
|
||||
ShowAction::Slide(i)
|
||||
}
|
||||
None => {
|
||||
|
||||
@@ -876,8 +876,6 @@ fn show_back_leaves_a_slide_whose_first_step_is_automatic() {
|
||||
// While the automatic step plays.
|
||||
assert_eq!(s.prev(&p), ShowAction::Slide(0), "{start:?}");
|
||||
assert_eq!(s.next(&p), ShowAction::Slide(1));
|
||||
// Going forward again plays the automatic step again (#2090).
|
||||
assert_eq!(s.step_done(&p), ShowAction::FinishStep(0), "{start:?}");
|
||||
// Once it has played, the click step is undone first, then Back leaves the slide.
|
||||
assert_eq!(s.next(&p), ShowAction::PlayStep(1));
|
||||
assert_eq!(s.step_done(&p), ShowAction::FinishStep(1));
|
||||
@@ -887,56 +885,6 @@ fn show_back_leaves_a_slide_whose_first_step_is_automatic() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_slide_entered_going_forward_again_plays_its_animations_again() {
|
||||
// #2090: the show remembered slide 2 as fully built, so its entrance effects did not play the
|
||||
// second time the show advanced to it.
|
||||
for start in [AnimStart::WithPrevious, AnimStart::AfterPrevious] {
|
||||
let mut p = pres(3);
|
||||
Arc::make_mut(&mut p.slides[1]).shapes = vec![shape(1, "A")];
|
||||
Arc::make_mut(&mut p.slides[1]).animations = vec![anim(1, AnimClass::Entrance, "fade", start)];
|
||||
let mut s = ShowState::new(&p);
|
||||
assert_eq!(s.next(&p), ShowAction::Slide(1));
|
||||
assert_eq!(s.step_done(&p), ShowAction::FinishStep(0));
|
||||
assert_eq!(s.prev(&p), ShowAction::Slide(0), "{start:?}");
|
||||
assert_eq!(s.next(&p), ShowAction::Slide(1));
|
||||
assert_eq!((s.step, s.playing), (0, true), "{start:?}: the slide starts again");
|
||||
let a = ShowState::timeline(&p, 1).state(ShapeId(1), None, s.step, 0.0);
|
||||
assert!(!a.visible || a.opacity < 0.01, "{start:?}: the shape has not come in yet");
|
||||
// Back from the next slide still shows it as it was left.
|
||||
assert_eq!(s.step_done(&p), ShowAction::FinishStep(0));
|
||||
assert_eq!(s.next(&p), ShowAction::Slide(2));
|
||||
assert_eq!(s.prev(&p), ShowAction::Slide(1));
|
||||
assert_eq!((s.step, s.playing), (1, false), "{start:?}");
|
||||
}
|
||||
// An On Click build left by a jump starts again when the show advances to the slide.
|
||||
let mut p = pres(2);
|
||||
Arc::make_mut(&mut p.slides[1]).shapes = vec![shape(1, "A")];
|
||||
Arc::make_mut(&mut p.slides[1]).animations = vec![anim(1, AnimClass::Entrance, "fade", AnimStart::OnClick)];
|
||||
let mut s = ShowState::new(&p);
|
||||
assert_eq!(s.next(&p), ShowAction::Slide(1));
|
||||
assert_eq!(s.next(&p), ShowAction::PlayStep(0));
|
||||
assert_eq!(s.step_done(&p), ShowAction::FinishStep(0));
|
||||
assert_eq!(s.goto(&p, 0), ShowAction::Slide(0));
|
||||
assert_eq!(s.next(&p), ShowAction::Slide(1));
|
||||
assert_eq!((s.step, s.playing), (0, false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_looping_show_plays_the_first_slide_again() {
|
||||
// #2090: a kiosk or loop-until-Esc show comes round to the first slide going forward.
|
||||
let mut p = pres(2);
|
||||
p.show.loop_until_esc = true;
|
||||
Arc::make_mut(&mut p.slides[0]).shapes = vec![shape(1, "A")];
|
||||
Arc::make_mut(&mut p.slides[0]).animations = vec![anim(1, AnimClass::Entrance, "fade", AnimStart::WithPrevious)];
|
||||
let mut s = ShowState::new(&p);
|
||||
assert!(s.playing);
|
||||
assert_eq!(s.step_done(&p), ShowAction::FinishStep(0));
|
||||
assert_eq!(s.next(&p), ShowAction::Slide(1));
|
||||
assert_eq!(s.next(&p), ShowAction::Slide(0));
|
||||
assert_eq!((s.step, s.playing), (0, true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn show_auto_advance() {
|
||||
let mut p = pres(2);
|
||||
|
||||
@@ -325,23 +325,6 @@ fn a_slide_comes_back_the_way_it_was_left() {
|
||||
assert_eq!(s.step, ShowState::steps(&pres, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_slide_entered_going_forward_again_starts_over() {
|
||||
// #2090: the show advances to slide 2 a second time; its triggered animation is undone too.
|
||||
let s1 = slide(1000, vec![], vec![], true);
|
||||
let s2 = slide(1001, vec![shape(4)], vec![exit_on_click(4, 4, 0)], true);
|
||||
let pres = Presentation { slides: vec![s1, s2], ..Default::default() };
|
||||
let mut r = Run::new(pres, vec![vec![], vec![hit(4, None)]]);
|
||||
assert_eq!(r.show.next(&r.pres), ShowAction::Slide(1));
|
||||
assert_eq!(r.click(), Click::Animated);
|
||||
assert!(!r.visible(ShapeId(4)));
|
||||
assert_eq!(r.show.prev(&r.pres), ShowAction::Slide(0));
|
||||
assert_eq!(r.show.prev(&r.pres), ShowAction::None);
|
||||
assert_eq!(r.show.next(&r.pres), ShowAction::Slide(1));
|
||||
assert!(r.visible(ShapeId(4)), "the shape is back");
|
||||
assert!(r.show.trigger_plays().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_playing_main_step_is_finished_when_the_show_leaves() {
|
||||
let a = Animation { shape: ShapeId(1), class: AnimClass::Entrance, effect: "fade".into(), duration_ms: 500, ..Default::default() };
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "deckcraft-av1"
|
||||
description = "Clean-room AV1 decoder (AV1 Bitstream & Decoding Process Specification v1.0.0 with Errata 1)"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[features]
|
||||
default = ["threads"]
|
||||
threads = ["dep:rayon"]
|
||||
|
||||
[dependencies]
|
||||
deckcraft-bitstream = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
rayon = { workspace = true, optional = true }
|
||||
@@ -0,0 +1,124 @@
|
||||
//! MSB-first bit reader for OBU headers and the uncompressed frame header (spec 4.10 descriptors).
|
||||
|
||||
use crate::{Error, Result};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct BitReader<'a> {
|
||||
data: &'a [u8],
|
||||
/// Position in bits.
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
impl<'a> BitReader<'a> {
|
||||
pub fn new(data: &'a [u8]) -> Self {
|
||||
BitReader { data, pos: 0 }
|
||||
}
|
||||
|
||||
/// `get_position()`: bits consumed.
|
||||
pub fn position(&self) -> usize {
|
||||
self.pos
|
||||
}
|
||||
|
||||
pub fn byte_pos(&self) -> usize {
|
||||
self.pos.div_ceil(8)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn bit(&mut self) -> Result<u32> {
|
||||
let byte = *self.data.get(self.pos >> 3).ok_or(Error::Truncated)?;
|
||||
let b = (byte >> (7 - (self.pos & 7))) & 1;
|
||||
self.pos += 1;
|
||||
Ok(b as u32)
|
||||
}
|
||||
|
||||
/// `f(n)`, n ≤ 32.
|
||||
pub fn f(&mut self, n: u32) -> Result<u32> {
|
||||
let mut x: u64 = 0;
|
||||
for _ in 0..n {
|
||||
x = (x << 1) | self.bit()? as u64;
|
||||
}
|
||||
Ok(x as u32)
|
||||
}
|
||||
|
||||
pub fn flag(&mut self) -> Result<bool> {
|
||||
Ok(self.bit()? == 1)
|
||||
}
|
||||
|
||||
/// `uvlc()`
|
||||
pub fn uvlc(&mut self) -> Result<u32> {
|
||||
let mut leading = 0u32;
|
||||
while self.bit()? == 0 {
|
||||
leading += 1;
|
||||
if leading > 40 {
|
||||
return Err(Error::Invalid("uvlc"));
|
||||
}
|
||||
}
|
||||
if leading >= 32 {
|
||||
return Ok(u32::MAX);
|
||||
}
|
||||
let v = self.f(leading)?;
|
||||
Ok(v + ((1u64 << leading) - 1) as u32)
|
||||
}
|
||||
|
||||
/// `le(n)`
|
||||
pub fn le(&mut self, n: u32) -> Result<u32> {
|
||||
let mut t = 0u32;
|
||||
for i in 0..n {
|
||||
t |= self.f(8)? << (8 * i);
|
||||
}
|
||||
Ok(t)
|
||||
}
|
||||
|
||||
/// `leb128()`
|
||||
pub fn leb128(&mut self) -> Result<u64> {
|
||||
let mut v = 0u64;
|
||||
for i in 0..8 {
|
||||
let b = self.f(8)? as u64;
|
||||
v |= (b & 0x7f) << (7 * i);
|
||||
if b & 0x80 == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// `su(n)`
|
||||
pub fn su(&mut self, n: u32) -> Result<i32> {
|
||||
let v = self.f(n)? as i64;
|
||||
let sign = 1i64 << (n - 1);
|
||||
Ok(if v & sign != 0 { (v - 2 * sign) as i32 } else { v as i32 })
|
||||
}
|
||||
|
||||
/// `ns(n)`
|
||||
pub fn ns(&mut self, n: u32) -> Result<u32> {
|
||||
if n <= 1 {
|
||||
return Ok(0);
|
||||
}
|
||||
let w = 32 - n.leading_zeros(); // FloorLog2(n) + 1
|
||||
let m = (1u32 << w) - n;
|
||||
let v = self.f(w - 1)?;
|
||||
if v < m {
|
||||
return Ok(v);
|
||||
}
|
||||
let extra = self.bit()?;
|
||||
Ok((v << 1) - m + extra)
|
||||
}
|
||||
|
||||
/// `byte_alignment()`
|
||||
pub fn byte_align(&mut self) {
|
||||
self.pos = self.pos.div_ceil(8) * 8;
|
||||
}
|
||||
}
|
||||
|
||||
/// Read a leb128 from a byte slice; returns (value, bytes used).
|
||||
pub(crate) fn leb128(data: &[u8]) -> Result<(u64, usize)> {
|
||||
let mut v = 0u64;
|
||||
for i in 0..8 {
|
||||
let b = *data.get(i).ok_or(Error::Truncated)? as u64;
|
||||
v |= (b & 0x7f) << (7 * i);
|
||||
if b & 0x80 == 0 {
|
||||
return Ok((v, i + 1));
|
||||
}
|
||||
}
|
||||
Ok((v, 8))
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
//! CDEF (7.15).
|
||||
|
||||
use crate::frame::{FrameBuf, Plane};
|
||||
use crate::spec_tables::{CDEF_DIRECTIONS, CDEF_PRI_TAPS, CDEF_SEC_TAPS, CDEF_UV_DIR, DIV_TABLE};
|
||||
use crate::state::FrameState;
|
||||
|
||||
/// Run CDEF on `fs.cur`; returns the CdefFrame, or None when no block is filtered (CdefFrame
|
||||
/// equals CurrFrame).
|
||||
pub(crate) fn apply(fs: &FrameState) -> Option<FrameBuf> {
|
||||
let fh = &fs.fh;
|
||||
if fh.coded_lossless || fh.allow_intrabc || !fs.seq.enable_cdef {
|
||||
return None;
|
||||
}
|
||||
let (mi_rows, mi_cols) = (fh.mi_rows as usize, fh.mi_cols as usize);
|
||||
let mut out = fs.cur.clone();
|
||||
let mut any = false;
|
||||
let mut r = 0;
|
||||
while r < mi_rows {
|
||||
let mut c = 0;
|
||||
while c < mi_cols {
|
||||
let idx = fs.cdef_idx(r & !15, c & !15);
|
||||
if idx >= 0 && cdef_block(fs, &mut out, r, c, idx as usize) {
|
||||
any = true;
|
||||
}
|
||||
c += 2;
|
||||
}
|
||||
r += 2;
|
||||
}
|
||||
any.then_some(out)
|
||||
}
|
||||
|
||||
fn cdef_block(fs: &FrameState, out: &mut FrameBuf, r: usize, c: usize, idx: usize) -> bool {
|
||||
let mi = &fs.mi;
|
||||
let sk = |rr: usize, cc: usize| -> bool { if rr < mi.rows && cc < mi.cols { mi.skip[mi.idx(rr, cc)] } else { true } };
|
||||
let skip = sk(r, c) && sk(r + 1, c) && sk(r, c + 1) && sk(r + 1, c + 1);
|
||||
if skip {
|
||||
return false;
|
||||
}
|
||||
let coeff_shift = fs.bit_depth - 8;
|
||||
let (y_dir, var) = direction(fs, r, c);
|
||||
let cp = &fs.fh.cdef;
|
||||
let mut pri = (cp.y_pri[idx] << coeff_shift) as i32;
|
||||
let sec = (cp.y_sec[idx] << coeff_shift) as i32;
|
||||
let dir = if pri == 0 { 0 } else { y_dir };
|
||||
let var_str = if (var >> 6) != 0 { (31 - ((var >> 6) as u32).leading_zeros()).min(12) as i32 } else { 0 };
|
||||
pri = if var != 0 { (pri * (4 + var_str) + 8) >> 4 } else { 0 };
|
||||
let damping = cp.damping as i32 + coeff_shift as i32;
|
||||
filter(fs, out, 0, (r, c), (pri, sec), damping, dir);
|
||||
if fs.num_planes == 1 {
|
||||
return true;
|
||||
}
|
||||
let pri = (cp.uv_pri[idx] << coeff_shift) as i32;
|
||||
let sec = (cp.uv_sec[idx] << coeff_shift) as i32;
|
||||
let dir = if pri == 0 { 0 } else { CDEF_UV_DIR[fs.ssx][fs.ssy][y_dir] as usize };
|
||||
let damping = cp.damping as i32 + coeff_shift as i32 - 1;
|
||||
filter(fs, out, 1, (r, c), (pri, sec), damping, dir);
|
||||
filter(fs, out, 2, (r, c), (pri, sec), damping, dir);
|
||||
true
|
||||
}
|
||||
|
||||
fn direction(fs: &FrameState, r: usize, c: usize) -> (usize, i32) {
|
||||
let mut cost = [0i32; 8];
|
||||
let mut partial = [[0i32; 15]; 8];
|
||||
let x0 = c << 2;
|
||||
let y0 = r << 2;
|
||||
let pl = &fs.cur.planes[0];
|
||||
let sh = fs.bit_depth - 8;
|
||||
for i in 0..8 {
|
||||
let row = pl.row(y0 + i);
|
||||
for j in 0..8 {
|
||||
let x = (row[x0 + j] as i32 >> sh) - 128;
|
||||
partial[0][i + j] += x;
|
||||
partial[1][i + j / 2] += x;
|
||||
partial[2][i] += x;
|
||||
partial[3][3 + i - j / 2] += x;
|
||||
partial[4][7 + i - j] += x;
|
||||
partial[5][3 - i / 2 + j] += x;
|
||||
partial[6][j] += x;
|
||||
partial[7][i / 2 + j] += x;
|
||||
}
|
||||
}
|
||||
for i in 0..8 {
|
||||
cost[2] += partial[2][i] * partial[2][i];
|
||||
cost[6] += partial[6][i] * partial[6][i];
|
||||
}
|
||||
cost[2] *= DIV_TABLE[8] as i32;
|
||||
cost[6] *= DIV_TABLE[8] as i32;
|
||||
for i in 0..7 {
|
||||
cost[0] += (partial[0][i] * partial[0][i] + partial[0][14 - i] * partial[0][14 - i]) * DIV_TABLE[i + 1] as i32;
|
||||
cost[4] += (partial[4][i] * partial[4][i] + partial[4][14 - i] * partial[4][14 - i]) * DIV_TABLE[i + 1] as i32;
|
||||
}
|
||||
cost[0] += partial[0][7] * partial[0][7] * DIV_TABLE[8] as i32;
|
||||
cost[4] += partial[4][7] * partial[4][7] * DIV_TABLE[8] as i32;
|
||||
let mut i = 1;
|
||||
while i < 8 {
|
||||
for j in 0..5 {
|
||||
cost[i] += partial[i][3 + j] * partial[i][3 + j];
|
||||
}
|
||||
cost[i] *= DIV_TABLE[8] as i32;
|
||||
for j in 0..3 {
|
||||
cost[i] += (partial[i][j] * partial[i][j] + partial[i][10 - j] * partial[i][10 - j]) * DIV_TABLE[2 * j + 2] as i32;
|
||||
}
|
||||
i += 2;
|
||||
}
|
||||
let mut best = 0;
|
||||
let mut y_dir = 0;
|
||||
for (i, &c) in cost.iter().enumerate() {
|
||||
if c > best {
|
||||
best = c;
|
||||
y_dir = i;
|
||||
}
|
||||
}
|
||||
(y_dir, (best - cost[(y_dir + 4) & 7]) >> 10)
|
||||
}
|
||||
|
||||
/// Marks a sample outside the frame (CdefAvailable = 0) in the padded block.
|
||||
const CDEF_NA: i32 = i32::MIN;
|
||||
/// Padded block: 8x8 plus 2 samples on every side, row stride 12.
|
||||
const PAD_STRIDE: usize = 12;
|
||||
|
||||
#[inline(always)]
|
||||
fn constrain(diff: i32, threshold: i32, adj: u32) -> i32 {
|
||||
let mag = (threshold - (diff.abs() >> adj)).clamp(0, diff.abs());
|
||||
if diff < 0 { -mag } else { mag }
|
||||
}
|
||||
|
||||
/// Tap offsets, weights and strengths of one block.
|
||||
struct Taps {
|
||||
pri: i32,
|
||||
sec: i32,
|
||||
pri_adj: u32,
|
||||
sec_adj: u32,
|
||||
pri_off: [isize; 2],
|
||||
sec_off: [[isize; 2]; 2],
|
||||
pri_taps: [i32; 2],
|
||||
sec_taps: [i32; 2],
|
||||
}
|
||||
|
||||
impl Taps {
|
||||
fn new(coeff_shift: u32, (pri, sec): (i32, i32), damping: i32, dir: usize) -> Taps {
|
||||
let pri_tap = ((pri >> coeff_shift) & 1) as usize;
|
||||
let off = |d: usize, k: usize| CDEF_DIRECTIONS[d][k][0] as isize * PAD_STRIDE as isize + CDEF_DIRECTIONS[d][k][1] as isize;
|
||||
let pri_off = [off(dir, 0), off(dir, 1)];
|
||||
let d_lo = (dir + 6) & 7;
|
||||
let d_hi = (dir + 2) & 7;
|
||||
let sec_off = [[off(d_lo, 0), off(d_hi, 0)], [off(d_lo, 1), off(d_hi, 1)]];
|
||||
let pri_taps = [CDEF_PRI_TAPS[pri_tap][0] as i32, CDEF_PRI_TAPS[pri_tap][1] as i32];
|
||||
let sec_taps = [CDEF_SEC_TAPS[pri_tap][0] as i32, CDEF_SEC_TAPS[pri_tap][1] as i32];
|
||||
let adj = |t: i32| if t == 0 { 0 } else { (damping - (31 - (t as u32).leading_zeros()) as i32).max(0) as u32 };
|
||||
Taps { pri, sec, pri_adj: adj(pri), sec_adj: adj(sec), pri_off, sec_off, pri_taps, sec_taps }
|
||||
}
|
||||
|
||||
/// The 12 (offset, weight, strength, adjustment) taps in filtering order.
|
||||
fn tap_list(&self) -> [(isize, i32, i32, u32); 12] {
|
||||
let mut taps = [(0isize, 0i32, 0i32, 0u32); 12];
|
||||
let mut n = 0;
|
||||
for k in 0..2 {
|
||||
for sign in [-1isize, 1] {
|
||||
taps[n] = (sign * self.pri_off[k], self.pri_taps[k], self.pri, self.pri_adj);
|
||||
taps[n + 1] = (sign * self.sec_off[k][0], self.sec_taps[k], self.sec, self.sec_adj);
|
||||
taps[n + 2] = (sign * self.sec_off[k][1], self.sec_taps[k], self.sec, self.sec_adj);
|
||||
n += 3;
|
||||
}
|
||||
}
|
||||
taps
|
||||
}
|
||||
}
|
||||
|
||||
/// Position and size of a block within its plane.
|
||||
#[derive(Clone, Copy)]
|
||||
struct BlockRect {
|
||||
x0: usize,
|
||||
y0: usize,
|
||||
w: usize,
|
||||
h: usize,
|
||||
}
|
||||
|
||||
/// The block with two samples of padding on every side, plus whether every sample is available
|
||||
/// (CdefAvailable: its 4x4 block is inside the frame).
|
||||
fn padded_block(fs: &FrameState, plane: usize, src: &Plane, rect: BlockRect) -> ([i32; PAD_STRIDE * PAD_STRIDE], bool) {
|
||||
let (sub_x, sub_y) = if plane > 0 { (fs.ssx, fs.ssy) } else { (0, 0) };
|
||||
let (mi_rows, mi_cols) = (fs.fh.mi_rows as isize, fs.fh.mi_cols as isize);
|
||||
let mut buf = [CDEF_NA; PAD_STRIDE * PAD_STRIDE];
|
||||
let mut all_avail = true;
|
||||
for i in 0..rect.h + 4 {
|
||||
let y = (rect.y0 + i) as isize - 2;
|
||||
let cand_r = (y << sub_y) >> 2;
|
||||
if y < 0 || cand_r >= mi_rows {
|
||||
all_avail = false;
|
||||
continue;
|
||||
}
|
||||
let row = src.row(y as usize);
|
||||
for j in 0..rect.w + 4 {
|
||||
let x = (rect.x0 + j) as isize - 2;
|
||||
let cand_c = (x << sub_x) >> 2;
|
||||
if x >= 0 && cand_c < mi_cols {
|
||||
buf[i * PAD_STRIDE + j] = row[x as usize] as i32;
|
||||
} else {
|
||||
all_avail = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
(buf, all_avail)
|
||||
}
|
||||
|
||||
/// Filters one sample whose taps may be unavailable (CDEF_NA).
|
||||
fn filter_sample(buf: &[i32; PAD_STRIDE * PAD_STRIDE], taps: &Taps, ci: isize, x: i32) -> u16 {
|
||||
let mut sum = 0i32;
|
||||
let mut max = x;
|
||||
let mut min = x;
|
||||
for k in 0..2 {
|
||||
for sign in [-1isize, 1] {
|
||||
let p = buf[(ci + sign * taps.pri_off[k]) as usize];
|
||||
if p != CDEF_NA {
|
||||
if taps.pri != 0 {
|
||||
sum += taps.pri_taps[k] * constrain(p - x, taps.pri, taps.pri_adj);
|
||||
}
|
||||
max = max.max(p);
|
||||
min = min.min(p);
|
||||
}
|
||||
for s in 0..2 {
|
||||
let q = buf[(ci + sign * taps.sec_off[k][s]) as usize];
|
||||
if q != CDEF_NA {
|
||||
if taps.sec != 0 {
|
||||
sum += taps.sec_taps[k] * constrain(q - x, taps.sec, taps.sec_adj);
|
||||
}
|
||||
max = max.max(q);
|
||||
min = min.min(q);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(x + ((8 + sum - (sum < 0) as i32) >> 4)).clamp(min, max) as u16
|
||||
}
|
||||
|
||||
fn filter(fs: &FrameState, out: &mut FrameBuf, plane: usize, (r, c): (usize, usize), strengths: (i32, i32), damping: i32, dir: usize) {
|
||||
let (pri, sec) = strengths;
|
||||
if pri == 0 && sec == 0 {
|
||||
// Every tap contributes 0 and the clamp to [min, max] keeps x: CdefFrame = CurrFrame.
|
||||
return;
|
||||
}
|
||||
let coeff_shift = fs.bit_depth - 8;
|
||||
let (sub_x, sub_y) = if plane > 0 { (fs.ssx, fs.ssy) } else { (0, 0) };
|
||||
let rect = BlockRect { x0: (c * 4) >> sub_x, y0: (r * 4) >> sub_y, w: 8 >> sub_x, h: 8 >> sub_y };
|
||||
let src = &fs.cur.planes[plane];
|
||||
let (buf, all_avail) = padded_block(fs, plane, src, rect);
|
||||
let taps = Taps::new(coeff_shift, strengths, damping, dir);
|
||||
let dst = &mut out.planes[plane];
|
||||
if all_avail {
|
||||
// Every tap is available: whole rows at a time (constrain() with threshold 0 is 0).
|
||||
let list = taps.tap_list();
|
||||
let rows = (rect.y0, rect.x0, rect.h);
|
||||
match rect.w {
|
||||
8 => filter_rows::<8>(&buf, &list, dst, rows),
|
||||
_ => filter_rows::<4>(&buf, &list, dst, rows),
|
||||
}
|
||||
return;
|
||||
}
|
||||
for i in 0..rect.h {
|
||||
// The centre sample is read even where CdefAvailable is 0 (8x8 blocks straddling an odd
|
||||
// MiRows / MiCols edge).
|
||||
let srow = &src.row(rect.y0 + i)[rect.x0..rect.x0 + rect.w];
|
||||
let drow = &mut dst.row_mut(rect.y0 + i)[rect.x0..rect.x0 + rect.w];
|
||||
for j in 0..rect.w {
|
||||
let ci = ((i + 2) * PAD_STRIDE + j + 2) as isize;
|
||||
drow[j] = filter_sample(&buf, &taps, ci, srow[j] as i32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// CDEF of a block whose taps are all available, `W` samples per row. Samples (at most 12
|
||||
/// bits), differences and the weighted tap sum all fit in i16, which doubles the SIMD width.
|
||||
#[inline(always)]
|
||||
fn filter_rows<const W: usize>(
|
||||
buf: &[i32; PAD_STRIDE * PAD_STRIDE],
|
||||
taps: &[(isize, i32, i32, u32); 12],
|
||||
dst: &mut Plane,
|
||||
(y0, x0, h): (usize, usize, usize),
|
||||
) {
|
||||
let mut b16 = [0i16; PAD_STRIDE * PAD_STRIDE];
|
||||
for (d, &s) in b16.iter_mut().zip(buf.iter()) {
|
||||
*d = s as i16;
|
||||
}
|
||||
for i in 0..h {
|
||||
let ci = ((i + 2) * PAD_STRIDE + 2) as isize;
|
||||
let Some(&x) = b16.get(ci as usize..).and_then(<[_]>::first_chunk::<W>) else {
|
||||
return;
|
||||
};
|
||||
let (mut sum, mut max, mut min) = ([0i16; W], x, x);
|
||||
for &(off, weight, thr, adj) in taps {
|
||||
let (weight, thr) = (weight as i16, thr as i16);
|
||||
let start = (ci + off) as usize;
|
||||
let Some(p) = b16.get(start..).and_then(<[_]>::first_chunk::<W>) else {
|
||||
return;
|
||||
};
|
||||
for j in 0..W {
|
||||
// constrain(): .max / .min instead of clamp (whose bound check panics), and
|
||||
// the sign applied branch-free, so the loop vectorises.
|
||||
let d = p[j] - x[j];
|
||||
let ad = d.abs();
|
||||
let mag = (thr - (ad >> adj)).max(0).min(ad);
|
||||
let sign = d >> 15;
|
||||
sum[j] += weight * ((mag ^ sign) - sign);
|
||||
max[j] = max[j].max(p[j]);
|
||||
min[j] = min[j].min(p[j]);
|
||||
}
|
||||
}
|
||||
let drow = &mut dst.row_mut(y0 + i)[x0..x0 + W];
|
||||
for j in 0..W {
|
||||
let s = sum[j];
|
||||
drow[j] = (x[j] + ((8 + s - (s < 0) as i16) >> 4)).max(min[j]).min(max[j]) as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,379 @@
|
||||
//! The adaptive CDF arrays (spec 6.8.2 init_non_coeff_cdfs / init_coeff_cdfs, load_cdfs).
|
||||
|
||||
use crate::spec_tables::{
|
||||
DEFAULT_ANGLE_DELTA_CDF, DEFAULT_CFL_ALPHA_CDF, DEFAULT_CFL_SIGN_CDF, DEFAULT_COEFF_BASE_CDF, DEFAULT_COEFF_BASE_EOB_CDF, DEFAULT_COEFF_BR_CDF,
|
||||
DEFAULT_COMP_BWD_REF_CDF, DEFAULT_COMP_GROUP_IDX_CDF, DEFAULT_COMP_MODE_CDF, DEFAULT_COMP_REF_CDF, DEFAULT_COMP_REF_TYPE_CDF,
|
||||
DEFAULT_COMPOUND_IDX_CDF, DEFAULT_COMPOUND_MODE_CDF, DEFAULT_COMPOUND_TYPE_CDF, DEFAULT_DC_SIGN_CDF, DEFAULT_DELTA_LF_CDF, DEFAULT_DELTA_Q_CDF,
|
||||
DEFAULT_DRL_MODE_CDF, DEFAULT_EOB_EXTRA_CDF, DEFAULT_EOB_PT_16_CDF, DEFAULT_EOB_PT_32_CDF, DEFAULT_EOB_PT_64_CDF, DEFAULT_EOB_PT_128_CDF,
|
||||
DEFAULT_EOB_PT_256_CDF, DEFAULT_EOB_PT_512_CDF, DEFAULT_EOB_PT_1024_CDF, DEFAULT_FILTER_INTRA_CDF, DEFAULT_FILTER_INTRA_MODE_CDF,
|
||||
DEFAULT_INTER_INTRA_CDF, DEFAULT_INTER_INTRA_MODE_CDF, DEFAULT_INTER_TX_TYPE_SET1_CDF, DEFAULT_INTER_TX_TYPE_SET2_CDF,
|
||||
DEFAULT_INTER_TX_TYPE_SET3_CDF, DEFAULT_INTERP_FILTER_CDF, DEFAULT_INTRA_TX_TYPE_SET1_CDF, DEFAULT_INTRA_TX_TYPE_SET2_CDF, DEFAULT_INTRABC_CDF,
|
||||
DEFAULT_IS_INTER_CDF, DEFAULT_MOTION_MODE_CDF, DEFAULT_MV_BIT_CDF, DEFAULT_MV_CLASS_CDF, DEFAULT_MV_CLASS0_BIT_CDF, DEFAULT_MV_CLASS0_FR_CDF,
|
||||
DEFAULT_MV_CLASS0_HP_CDF, DEFAULT_MV_FR_CDF, DEFAULT_MV_HP_CDF, DEFAULT_MV_JOINT_CDF, DEFAULT_MV_SIGN_CDF, DEFAULT_NEW_MV_CDF,
|
||||
DEFAULT_PALETTE_SIZE_2_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_2_Y_COLOR_CDF, DEFAULT_PALETTE_SIZE_3_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_3_Y_COLOR_CDF,
|
||||
DEFAULT_PALETTE_SIZE_4_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_4_Y_COLOR_CDF, DEFAULT_PALETTE_SIZE_5_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_5_Y_COLOR_CDF,
|
||||
DEFAULT_PALETTE_SIZE_6_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_6_Y_COLOR_CDF, DEFAULT_PALETTE_SIZE_7_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_7_Y_COLOR_CDF,
|
||||
DEFAULT_PALETTE_SIZE_8_UV_COLOR_CDF, DEFAULT_PALETTE_SIZE_8_Y_COLOR_CDF, DEFAULT_PALETTE_UV_MODE_CDF, DEFAULT_PALETTE_UV_SIZE_CDF,
|
||||
DEFAULT_PALETTE_Y_MODE_CDF, DEFAULT_PALETTE_Y_SIZE_CDF, DEFAULT_PARTITION_W8_CDF, DEFAULT_PARTITION_W16_CDF, DEFAULT_PARTITION_W32_CDF,
|
||||
DEFAULT_PARTITION_W64_CDF, DEFAULT_PARTITION_W128_CDF, DEFAULT_REF_MV_CDF, DEFAULT_RESTORATION_TYPE_CDF, DEFAULT_SEGMENT_ID_CDF,
|
||||
DEFAULT_SEGMENT_ID_PREDICTED_CDF, DEFAULT_SINGLE_REF_CDF, DEFAULT_SKIP_CDF, DEFAULT_SKIP_MODE_CDF, DEFAULT_TX_8X8_CDF, DEFAULT_TX_16X16_CDF,
|
||||
DEFAULT_TX_32X32_CDF, DEFAULT_TX_64X64_CDF, DEFAULT_TXB_SKIP_CDF, DEFAULT_TXFM_SPLIT_CDF, DEFAULT_UNI_COMP_REF_CDF, DEFAULT_USE_OBMC_CDF,
|
||||
DEFAULT_USE_SGRPROJ_CDF, DEFAULT_USE_WIENER_CDF, DEFAULT_UV_MODE_CFL_ALLOWED_CDF, DEFAULT_UV_MODE_CFL_NOT_ALLOWED_CDF, DEFAULT_WEDGE_INDEX_CDF,
|
||||
DEFAULT_WEDGE_INTER_INTRA_CDF, DEFAULT_Y_MODE_CDF, DEFAULT_ZERO_MV_CDF, FRAME_LF_COUNT, MV_CONTEXTS,
|
||||
};
|
||||
|
||||
/// Every CDF array listed by init_non_coeff_cdfs and init_coeff_cdfs. Each innermost array holds
|
||||
/// N cumulative values (the last is 32768) plus the adaptation counter.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Cdfs {
|
||||
pub y_mode: [[u16; 14]; 4],
|
||||
pub uv_mode_cfl_not_allowed: [[u16; 14]; 13],
|
||||
pub uv_mode_cfl_allowed: [[u16; 15]; 13],
|
||||
pub angle_delta: [[u16; 8]; 8],
|
||||
pub intrabc: [u16; 3],
|
||||
pub partition_w8: [[u16; 5]; 4],
|
||||
pub partition_w16: [[u16; 11]; 4],
|
||||
pub partition_w32: [[u16; 11]; 4],
|
||||
pub partition_w64: [[u16; 11]; 4],
|
||||
pub partition_w128: [[u16; 9]; 4],
|
||||
pub segment_id: [[u16; 9]; 3],
|
||||
pub segment_id_predicted: [[u16; 3]; 3],
|
||||
pub tx_8x8: [[u16; 3]; 3],
|
||||
pub tx_16x16: [[u16; 4]; 3],
|
||||
pub tx_32x32: [[u16; 4]; 3],
|
||||
pub tx_64x64: [[u16; 4]; 3],
|
||||
pub txfm_split: [[u16; 3]; 21],
|
||||
pub filter_intra_mode: [u16; 6],
|
||||
pub filter_intra: [[u16; 3]; 22],
|
||||
pub interp_filter: [[u16; 4]; 16],
|
||||
pub motion_mode: [[u16; 4]; 22],
|
||||
pub new_mv: [[u16; 3]; 6],
|
||||
pub zero_mv: [[u16; 3]; 2],
|
||||
pub ref_mv: [[u16; 3]; 6],
|
||||
pub compound_mode: [[u16; 9]; 8],
|
||||
pub drl_mode: [[u16; 3]; 3],
|
||||
pub is_inter: [[u16; 3]; 4],
|
||||
pub comp_mode: [[u16; 3]; 5],
|
||||
pub skip_mode: [[u16; 3]; 3],
|
||||
pub skip: [[u16; 3]; 3],
|
||||
pub comp_ref: [[[u16; 3]; 3]; 3],
|
||||
pub comp_bwd_ref: [[[u16; 3]; 2]; 3],
|
||||
pub single_ref: [[[u16; 3]; 6]; 3],
|
||||
pub mv_joint: [[u16; 5]; MV_CONTEXTS],
|
||||
pub mv_class: [[[u16; 12]; 2]; MV_CONTEXTS],
|
||||
pub mv_class0_bit: [[[u16; 3]; 2]; MV_CONTEXTS],
|
||||
pub mv_fr: [[[u16; 5]; 2]; MV_CONTEXTS],
|
||||
pub mv_class0_fr: [[[[u16; 5]; 2]; 2]; MV_CONTEXTS],
|
||||
pub mv_class0_hp: [[[u16; 3]; 2]; MV_CONTEXTS],
|
||||
pub mv_sign: [[[u16; 3]; 2]; MV_CONTEXTS],
|
||||
pub mv_bit: [[[[u16; 3]; 10]; 2]; MV_CONTEXTS],
|
||||
pub mv_hp: [[[u16; 3]; 2]; MV_CONTEXTS],
|
||||
pub palette_y_mode: [[[u16; 3]; 3]; 7],
|
||||
pub palette_uv_mode: [[u16; 3]; 2],
|
||||
pub palette_y_size: [[u16; 8]; 7],
|
||||
pub palette_uv_size: [[u16; 8]; 7],
|
||||
pub palette_2_y_color: [[u16; 3]; 5],
|
||||
pub palette_2_uv_color: [[u16; 3]; 5],
|
||||
pub palette_3_y_color: [[u16; 4]; 5],
|
||||
pub palette_3_uv_color: [[u16; 4]; 5],
|
||||
pub palette_4_y_color: [[u16; 5]; 5],
|
||||
pub palette_4_uv_color: [[u16; 5]; 5],
|
||||
pub palette_5_y_color: [[u16; 6]; 5],
|
||||
pub palette_5_uv_color: [[u16; 6]; 5],
|
||||
pub palette_6_y_color: [[u16; 7]; 5],
|
||||
pub palette_6_uv_color: [[u16; 7]; 5],
|
||||
pub palette_7_y_color: [[u16; 8]; 5],
|
||||
pub palette_7_uv_color: [[u16; 8]; 5],
|
||||
pub palette_8_y_color: [[u16; 9]; 5],
|
||||
pub palette_8_uv_color: [[u16; 9]; 5],
|
||||
pub delta_q: [u16; 5],
|
||||
pub delta_lf: [u16; 5],
|
||||
pub delta_lf_multi: [[u16; 5]; FRAME_LF_COUNT],
|
||||
pub intra_tx_type_set1: [[[u16; 8]; 13]; 2],
|
||||
pub intra_tx_type_set2: [[[u16; 6]; 13]; 3],
|
||||
pub inter_tx_type_set1: [[u16; 17]; 2],
|
||||
pub inter_tx_type_set2: [u16; 13],
|
||||
pub inter_tx_type_set3: [[u16; 3]; 4],
|
||||
pub use_obmc: [[u16; 3]; 22],
|
||||
pub inter_intra: [[u16; 3]; 3],
|
||||
pub comp_ref_type: [[u16; 3]; 5],
|
||||
pub cfl_sign: [u16; 9],
|
||||
pub uni_comp_ref: [[[u16; 3]; 3]; 3],
|
||||
pub wedge_inter_intra: [[u16; 3]; 22],
|
||||
pub comp_group_idx: [[u16; 3]; 6],
|
||||
pub compound_idx: [[u16; 3]; 6],
|
||||
pub compound_type: [[u16; 3]; 22],
|
||||
pub inter_intra_mode: [[u16; 5]; 3],
|
||||
pub wedge_index: [[u16; 17]; 22],
|
||||
pub cfl_alpha: [[u16; 17]; 6],
|
||||
pub use_wiener: [u16; 3],
|
||||
pub use_sgrproj: [u16; 3],
|
||||
pub restoration_type: [u16; 4],
|
||||
// coefficient CDFs
|
||||
pub txb_skip: [[[u16; 3]; 13]; 5],
|
||||
pub eob_pt_16: [[[u16; 6]; 2]; 2],
|
||||
pub eob_pt_32: [[[u16; 7]; 2]; 2],
|
||||
pub eob_pt_64: [[[u16; 8]; 2]; 2],
|
||||
pub eob_pt_128: [[[u16; 9]; 2]; 2],
|
||||
pub eob_pt_256: [[[u16; 10]; 2]; 2],
|
||||
pub eob_pt_512: [[u16; 11]; 2],
|
||||
pub eob_pt_1024: [[u16; 12]; 2],
|
||||
pub eob_extra: [[[[u16; 3]; 9]; 2]; 5],
|
||||
pub dc_sign: [[[u16; 3]; 3]; 2],
|
||||
pub coeff_base_eob: [[[[u16; 4]; 4]; 2]; 5],
|
||||
pub coeff_base: [[[[u16; 5]; 42]; 2]; 5],
|
||||
pub coeff_br: [[[[u16; 5]; 21]; 2]; 5],
|
||||
}
|
||||
|
||||
impl Cdfs {
|
||||
/// init_non_coeff_cdfs( ) followed by init_coeff_cdfs( ) for `base_q_idx`.
|
||||
pub fn new(base_q_idx: u32) -> Box<Cdfs> {
|
||||
let mut c = Box::new(Cdfs {
|
||||
y_mode: DEFAULT_Y_MODE_CDF,
|
||||
uv_mode_cfl_not_allowed: DEFAULT_UV_MODE_CFL_NOT_ALLOWED_CDF,
|
||||
uv_mode_cfl_allowed: DEFAULT_UV_MODE_CFL_ALLOWED_CDF,
|
||||
angle_delta: DEFAULT_ANGLE_DELTA_CDF,
|
||||
intrabc: DEFAULT_INTRABC_CDF,
|
||||
partition_w8: DEFAULT_PARTITION_W8_CDF,
|
||||
partition_w16: DEFAULT_PARTITION_W16_CDF,
|
||||
partition_w32: DEFAULT_PARTITION_W32_CDF,
|
||||
partition_w64: DEFAULT_PARTITION_W64_CDF,
|
||||
partition_w128: DEFAULT_PARTITION_W128_CDF,
|
||||
segment_id: DEFAULT_SEGMENT_ID_CDF,
|
||||
segment_id_predicted: DEFAULT_SEGMENT_ID_PREDICTED_CDF,
|
||||
tx_8x8: DEFAULT_TX_8X8_CDF,
|
||||
tx_16x16: DEFAULT_TX_16X16_CDF,
|
||||
tx_32x32: DEFAULT_TX_32X32_CDF,
|
||||
tx_64x64: DEFAULT_TX_64X64_CDF,
|
||||
txfm_split: DEFAULT_TXFM_SPLIT_CDF,
|
||||
filter_intra_mode: DEFAULT_FILTER_INTRA_MODE_CDF,
|
||||
filter_intra: DEFAULT_FILTER_INTRA_CDF,
|
||||
interp_filter: DEFAULT_INTERP_FILTER_CDF,
|
||||
motion_mode: DEFAULT_MOTION_MODE_CDF,
|
||||
new_mv: DEFAULT_NEW_MV_CDF,
|
||||
zero_mv: DEFAULT_ZERO_MV_CDF,
|
||||
ref_mv: DEFAULT_REF_MV_CDF,
|
||||
compound_mode: DEFAULT_COMPOUND_MODE_CDF,
|
||||
drl_mode: DEFAULT_DRL_MODE_CDF,
|
||||
is_inter: DEFAULT_IS_INTER_CDF,
|
||||
comp_mode: DEFAULT_COMP_MODE_CDF,
|
||||
skip_mode: DEFAULT_SKIP_MODE_CDF,
|
||||
skip: DEFAULT_SKIP_CDF,
|
||||
comp_ref: DEFAULT_COMP_REF_CDF,
|
||||
comp_bwd_ref: DEFAULT_COMP_BWD_REF_CDF,
|
||||
single_ref: DEFAULT_SINGLE_REF_CDF,
|
||||
mv_joint: [DEFAULT_MV_JOINT_CDF; MV_CONTEXTS],
|
||||
mv_class: [DEFAULT_MV_CLASS_CDF; MV_CONTEXTS],
|
||||
mv_class0_bit: [[DEFAULT_MV_CLASS0_BIT_CDF; 2]; MV_CONTEXTS],
|
||||
mv_fr: [DEFAULT_MV_FR_CDF; MV_CONTEXTS],
|
||||
mv_class0_fr: [DEFAULT_MV_CLASS0_FR_CDF; MV_CONTEXTS],
|
||||
mv_class0_hp: [[DEFAULT_MV_CLASS0_HP_CDF; 2]; MV_CONTEXTS],
|
||||
mv_sign: [[DEFAULT_MV_SIGN_CDF; 2]; MV_CONTEXTS],
|
||||
mv_bit: [[DEFAULT_MV_BIT_CDF; 2]; MV_CONTEXTS],
|
||||
mv_hp: [[DEFAULT_MV_HP_CDF; 2]; MV_CONTEXTS],
|
||||
palette_y_mode: DEFAULT_PALETTE_Y_MODE_CDF,
|
||||
palette_uv_mode: DEFAULT_PALETTE_UV_MODE_CDF,
|
||||
palette_y_size: DEFAULT_PALETTE_Y_SIZE_CDF,
|
||||
palette_uv_size: DEFAULT_PALETTE_UV_SIZE_CDF,
|
||||
palette_2_y_color: DEFAULT_PALETTE_SIZE_2_Y_COLOR_CDF,
|
||||
palette_2_uv_color: DEFAULT_PALETTE_SIZE_2_UV_COLOR_CDF,
|
||||
palette_3_y_color: DEFAULT_PALETTE_SIZE_3_Y_COLOR_CDF,
|
||||
palette_3_uv_color: DEFAULT_PALETTE_SIZE_3_UV_COLOR_CDF,
|
||||
palette_4_y_color: DEFAULT_PALETTE_SIZE_4_Y_COLOR_CDF,
|
||||
palette_4_uv_color: DEFAULT_PALETTE_SIZE_4_UV_COLOR_CDF,
|
||||
palette_5_y_color: DEFAULT_PALETTE_SIZE_5_Y_COLOR_CDF,
|
||||
palette_5_uv_color: DEFAULT_PALETTE_SIZE_5_UV_COLOR_CDF,
|
||||
palette_6_y_color: DEFAULT_PALETTE_SIZE_6_Y_COLOR_CDF,
|
||||
palette_6_uv_color: DEFAULT_PALETTE_SIZE_6_UV_COLOR_CDF,
|
||||
palette_7_y_color: DEFAULT_PALETTE_SIZE_7_Y_COLOR_CDF,
|
||||
palette_7_uv_color: DEFAULT_PALETTE_SIZE_7_UV_COLOR_CDF,
|
||||
palette_8_y_color: DEFAULT_PALETTE_SIZE_8_Y_COLOR_CDF,
|
||||
palette_8_uv_color: DEFAULT_PALETTE_SIZE_8_UV_COLOR_CDF,
|
||||
delta_q: DEFAULT_DELTA_Q_CDF,
|
||||
delta_lf: DEFAULT_DELTA_LF_CDF,
|
||||
delta_lf_multi: [DEFAULT_DELTA_LF_CDF; FRAME_LF_COUNT],
|
||||
intra_tx_type_set1: DEFAULT_INTRA_TX_TYPE_SET1_CDF,
|
||||
intra_tx_type_set2: DEFAULT_INTRA_TX_TYPE_SET2_CDF,
|
||||
inter_tx_type_set1: DEFAULT_INTER_TX_TYPE_SET1_CDF,
|
||||
inter_tx_type_set2: DEFAULT_INTER_TX_TYPE_SET2_CDF,
|
||||
inter_tx_type_set3: DEFAULT_INTER_TX_TYPE_SET3_CDF,
|
||||
use_obmc: DEFAULT_USE_OBMC_CDF,
|
||||
inter_intra: DEFAULT_INTER_INTRA_CDF,
|
||||
comp_ref_type: DEFAULT_COMP_REF_TYPE_CDF,
|
||||
cfl_sign: DEFAULT_CFL_SIGN_CDF,
|
||||
uni_comp_ref: DEFAULT_UNI_COMP_REF_CDF,
|
||||
wedge_inter_intra: DEFAULT_WEDGE_INTER_INTRA_CDF,
|
||||
comp_group_idx: DEFAULT_COMP_GROUP_IDX_CDF,
|
||||
compound_idx: DEFAULT_COMPOUND_IDX_CDF,
|
||||
compound_type: DEFAULT_COMPOUND_TYPE_CDF,
|
||||
inter_intra_mode: DEFAULT_INTER_INTRA_MODE_CDF,
|
||||
wedge_index: DEFAULT_WEDGE_INDEX_CDF,
|
||||
cfl_alpha: DEFAULT_CFL_ALPHA_CDF,
|
||||
use_wiener: DEFAULT_USE_WIENER_CDF,
|
||||
use_sgrproj: DEFAULT_USE_SGRPROJ_CDF,
|
||||
restoration_type: DEFAULT_RESTORATION_TYPE_CDF,
|
||||
txb_skip: DEFAULT_TXB_SKIP_CDF[0],
|
||||
eob_pt_16: DEFAULT_EOB_PT_16_CDF[0],
|
||||
eob_pt_32: DEFAULT_EOB_PT_32_CDF[0],
|
||||
eob_pt_64: DEFAULT_EOB_PT_64_CDF[0],
|
||||
eob_pt_128: DEFAULT_EOB_PT_128_CDF[0],
|
||||
eob_pt_256: DEFAULT_EOB_PT_256_CDF[0],
|
||||
eob_pt_512: DEFAULT_EOB_PT_512_CDF[0],
|
||||
eob_pt_1024: DEFAULT_EOB_PT_1024_CDF[0],
|
||||
eob_extra: DEFAULT_EOB_EXTRA_CDF[0],
|
||||
dc_sign: DEFAULT_DC_SIGN_CDF[0],
|
||||
coeff_base_eob: DEFAULT_COEFF_BASE_EOB_CDF[0],
|
||||
coeff_base: DEFAULT_COEFF_BASE_CDF[0],
|
||||
coeff_br: DEFAULT_COEFF_BR_CDF[0],
|
||||
});
|
||||
c.init_coeff(base_q_idx);
|
||||
c
|
||||
}
|
||||
|
||||
/// init_coeff_cdfs( )
|
||||
pub fn init_coeff(&mut self, base_q_idx: u32) {
|
||||
let idx = match base_q_idx {
|
||||
0..=20 => 0,
|
||||
21..=60 => 1,
|
||||
61..=120 => 2,
|
||||
_ => 3,
|
||||
};
|
||||
self.txb_skip = DEFAULT_TXB_SKIP_CDF[idx];
|
||||
self.eob_pt_16 = DEFAULT_EOB_PT_16_CDF[idx];
|
||||
self.eob_pt_32 = DEFAULT_EOB_PT_32_CDF[idx];
|
||||
self.eob_pt_64 = DEFAULT_EOB_PT_64_CDF[idx];
|
||||
self.eob_pt_128 = DEFAULT_EOB_PT_128_CDF[idx];
|
||||
self.eob_pt_256 = DEFAULT_EOB_PT_256_CDF[idx];
|
||||
self.eob_pt_512 = DEFAULT_EOB_PT_512_CDF[idx];
|
||||
self.eob_pt_1024 = DEFAULT_EOB_PT_1024_CDF[idx];
|
||||
self.eob_extra = DEFAULT_EOB_EXTRA_CDF[idx];
|
||||
self.dc_sign = DEFAULT_DC_SIGN_CDF[idx];
|
||||
self.coeff_base_eob = DEFAULT_COEFF_BASE_EOB_CDF[idx];
|
||||
self.coeff_base = DEFAULT_COEFF_BASE_CDF[idx];
|
||||
self.coeff_br = DEFAULT_COEFF_BR_CDF[idx];
|
||||
}
|
||||
|
||||
/// The counter reset of load_cdfs( ): the last entry of every CDF array is set to 0.
|
||||
pub fn clear_counts(&mut self) {
|
||||
macro_rules! clr {
|
||||
($($f:ident),*) => { $( self.$f.clear_count(); )* };
|
||||
}
|
||||
clr!(
|
||||
y_mode,
|
||||
uv_mode_cfl_not_allowed,
|
||||
uv_mode_cfl_allowed,
|
||||
angle_delta,
|
||||
intrabc,
|
||||
partition_w8,
|
||||
partition_w16,
|
||||
partition_w32,
|
||||
partition_w64,
|
||||
partition_w128,
|
||||
segment_id,
|
||||
segment_id_predicted,
|
||||
tx_8x8,
|
||||
tx_16x16,
|
||||
tx_32x32,
|
||||
tx_64x64,
|
||||
txfm_split,
|
||||
filter_intra_mode,
|
||||
filter_intra,
|
||||
interp_filter,
|
||||
motion_mode,
|
||||
new_mv,
|
||||
zero_mv,
|
||||
ref_mv,
|
||||
compound_mode,
|
||||
drl_mode,
|
||||
is_inter,
|
||||
comp_mode,
|
||||
skip_mode,
|
||||
skip,
|
||||
comp_ref,
|
||||
comp_bwd_ref,
|
||||
single_ref,
|
||||
mv_joint,
|
||||
mv_class,
|
||||
mv_class0_bit,
|
||||
mv_fr,
|
||||
mv_class0_fr,
|
||||
mv_class0_hp,
|
||||
mv_sign,
|
||||
mv_bit,
|
||||
mv_hp,
|
||||
palette_y_mode,
|
||||
palette_uv_mode,
|
||||
palette_y_size,
|
||||
palette_uv_size,
|
||||
palette_2_y_color,
|
||||
palette_2_uv_color,
|
||||
palette_3_y_color,
|
||||
palette_3_uv_color,
|
||||
palette_4_y_color,
|
||||
palette_4_uv_color,
|
||||
palette_5_y_color,
|
||||
palette_5_uv_color,
|
||||
palette_6_y_color,
|
||||
palette_6_uv_color,
|
||||
palette_7_y_color,
|
||||
palette_7_uv_color,
|
||||
palette_8_y_color,
|
||||
palette_8_uv_color,
|
||||
delta_q,
|
||||
delta_lf,
|
||||
delta_lf_multi,
|
||||
intra_tx_type_set1,
|
||||
intra_tx_type_set2,
|
||||
inter_tx_type_set1,
|
||||
inter_tx_type_set2,
|
||||
inter_tx_type_set3,
|
||||
use_obmc,
|
||||
inter_intra,
|
||||
comp_ref_type,
|
||||
cfl_sign,
|
||||
uni_comp_ref,
|
||||
wedge_inter_intra,
|
||||
comp_group_idx,
|
||||
compound_idx,
|
||||
compound_type,
|
||||
inter_intra_mode,
|
||||
wedge_index,
|
||||
cfl_alpha,
|
||||
use_wiener,
|
||||
use_sgrproj,
|
||||
restoration_type,
|
||||
txb_skip,
|
||||
eob_pt_16,
|
||||
eob_pt_32,
|
||||
eob_pt_64,
|
||||
eob_pt_128,
|
||||
eob_pt_256,
|
||||
eob_pt_512,
|
||||
eob_pt_1024,
|
||||
eob_extra,
|
||||
dc_sign,
|
||||
coeff_base_eob,
|
||||
coeff_base,
|
||||
coeff_br
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Zeroes the adaptation counter (last element) of every innermost CDF array.
|
||||
trait ClearCount {
|
||||
fn clear_count(&mut self);
|
||||
}
|
||||
|
||||
impl<const N: usize> ClearCount for [u16; N] {
|
||||
fn clear_count(&mut self) {
|
||||
self[N - 1] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ClearCount, const M: usize> ClearCount for [T; M] {
|
||||
fn clear_count(&mut self) {
|
||||
for e in self.iter_mut() {
|
||||
e.clear_count();
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,287 @@
|
||||
//! Frame sample buffers and per-4x4 mode info storage.
|
||||
//!
|
||||
//! Tiles decode in parallel into private buffers that cover only the tile's area: a [`Plane`]
|
||||
//! or [`MiInfo`] may hold a region of the frame (origin `ox`, `oy`), addressed with frame
|
||||
//! coordinates throughout.
|
||||
|
||||
/// Recycled plane allocations: every frame allocates (and every evicted reference frees)
|
||||
/// megabytes of planes, and fresh large allocations cost page faults and kernel calls. Buffers
|
||||
/// are zeroed again on reuse, so contents never leak between frames.
|
||||
static PLANE_POOL: std::sync::Mutex<Vec<Vec<u16>>> = std::sync::Mutex::new(Vec::new());
|
||||
const POOL_MIN_LEN: usize = 1 << 16;
|
||||
/// Samples kept at most (96 M samples = 192 MB, a few 4K frames).
|
||||
const POOL_MAX_SAMPLES: usize = 96 << 20;
|
||||
|
||||
/// A zeroed buffer of `len` samples (recycled memory for large ones).
|
||||
fn zeroed(len: usize) -> Vec<u16> {
|
||||
if len >= POOL_MIN_LEN {
|
||||
let found = {
|
||||
let mut pool = PLANE_POOL.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
pool.iter().position(|v| v.capacity() >= len && v.capacity() <= 2 * len).map(|i| pool.swap_remove(i))
|
||||
};
|
||||
if let Some(mut v) = found {
|
||||
v.clear();
|
||||
v.resize(len, 0);
|
||||
return v;
|
||||
}
|
||||
}
|
||||
vec![0; len]
|
||||
}
|
||||
|
||||
/// One plane of samples (u16 at every bit depth), or a rectangular region of one.
|
||||
#[derive(Default)]
|
||||
pub struct Plane {
|
||||
pub data: Vec<u16>,
|
||||
pub stride: usize,
|
||||
/// Allocated rows.
|
||||
pub rows: usize,
|
||||
/// Frame coordinates of `data[0]` (0 for whole planes).
|
||||
pub ox: usize,
|
||||
pub oy: usize,
|
||||
}
|
||||
|
||||
impl Clone for Plane {
|
||||
fn clone(&self) -> Plane {
|
||||
let mut data = zeroed(self.data.len());
|
||||
data.copy_from_slice(&self.data);
|
||||
Plane { data, stride: self.stride, rows: self.rows, ox: self.ox, oy: self.oy }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Plane {
|
||||
fn drop(&mut self) {
|
||||
if self.data.capacity() >= POOL_MIN_LEN {
|
||||
let v = std::mem::take(&mut self.data);
|
||||
let mut pool = PLANE_POOL.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let kept: usize = pool.iter().map(Vec::capacity).sum();
|
||||
if kept + v.capacity() <= POOL_MAX_SAMPLES {
|
||||
pool.push(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Plane {
|
||||
pub fn new(width: usize, height: usize) -> Plane {
|
||||
Plane { data: zeroed(width * height), stride: width, rows: height, ox: 0, oy: 0 }
|
||||
}
|
||||
/// A `width` x `height` region with its top-left sample at frame position (ox, oy).
|
||||
pub fn region(ox: usize, oy: usize, width: usize, height: usize) -> Plane {
|
||||
Plane { data: zeroed(width * height), stride: width, rows: height, ox, oy }
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn at(&self, x: usize, y: usize) -> u16 {
|
||||
self.data[(y - self.oy) * self.stride + x - self.ox]
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn set(&mut self, x: usize, y: usize, v: u16) {
|
||||
self.data[(y - self.oy) * self.stride + x - self.ox] = v;
|
||||
}
|
||||
/// Row `y`; index 0 is column `ox` (whole planes: column 0).
|
||||
#[inline(always)]
|
||||
pub fn row(&self, y: usize) -> &[u16] {
|
||||
let o = (y - self.oy) * self.stride;
|
||||
&self.data[o..o + self.stride]
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn row_mut(&mut self, y: usize) -> &mut [u16] {
|
||||
let o = (y - self.oy) * self.stride;
|
||||
&mut self.data[o..o + self.stride]
|
||||
}
|
||||
/// Row `y` from column `x` (frame coordinates) to the end of the stored row.
|
||||
#[inline(always)]
|
||||
pub fn row_from(&self, y: usize, x: usize) -> &[u16] {
|
||||
let o = (y - self.oy) * self.stride;
|
||||
&self.data[o + x - self.ox..o + self.stride]
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn row_from_mut(&mut self, y: usize, x: usize) -> &mut [u16] {
|
||||
let o = (y - self.oy) * self.stride;
|
||||
&mut self.data[o + x - self.ox..o + self.stride]
|
||||
}
|
||||
/// Copy this region into the whole plane `dst` (same frame coordinates).
|
||||
pub fn copy_into(&self, dst: &mut Plane) {
|
||||
for y in 0..self.rows {
|
||||
let s = &self.data[y * self.stride..(y + 1) * self.stride];
|
||||
dst.row_from_mut(self.oy + y, self.ox)[..self.stride].copy_from_slice(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A frame's three planes, sized to the macroblock-aligned coded area plus margins.
|
||||
#[derive(Clone, Default)]
|
||||
pub struct FrameBuf {
|
||||
pub planes: [Plane; 3],
|
||||
pub num_planes: usize,
|
||||
pub subsampling_x: usize,
|
||||
pub subsampling_y: usize,
|
||||
pub bit_depth: u8,
|
||||
/// Frame dimensions (luma) the samples are valid for.
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
}
|
||||
|
||||
impl FrameBuf {
|
||||
/// Allocate a frame for a coded area of `width` x `height` luma samples (rounded up to the
|
||||
/// 128x128 superblock grid plus 160 samples of slack for blocks extending past the edge).
|
||||
pub fn new(width: usize, height: usize, num_planes: usize, subsampling_x: usize, subsampling_y: usize, bit_depth: u8) -> FrameBuf {
|
||||
let aw = width.div_ceil(128) * 128 + 160;
|
||||
let ah = height.div_ceil(128) * 128 + 160;
|
||||
let mut planes: [Plane; 3] = Default::default();
|
||||
planes[0] = Plane::new(aw, ah);
|
||||
for p in planes.iter_mut().take(num_planes).skip(1) {
|
||||
*p = Plane::new(aw >> subsampling_x, ah >> subsampling_y);
|
||||
}
|
||||
FrameBuf { planes, num_planes, subsampling_x, subsampling_y, bit_depth, width, height }
|
||||
}
|
||||
|
||||
/// The region of a frame of this geometry covering luma samples [x0, x1) x [y0, y1)
|
||||
/// (chroma scaled down); the bounds are multiples of 8, or the allocated size.
|
||||
pub fn region_like(&self, x0: usize, y0: usize, x1: usize, y1: usize) -> FrameBuf {
|
||||
let mut planes: [Plane; 3] = Default::default();
|
||||
for (p, pl) in planes.iter_mut().enumerate().take(self.num_planes) {
|
||||
let (sx, sy) = if p == 0 { (0, 0) } else { (self.subsampling_x, self.subsampling_y) };
|
||||
let full = &self.planes[p];
|
||||
let (px0, py0) = (x0 >> sx, y0 >> sy);
|
||||
let px1 = (x1 >> sx).min(full.stride);
|
||||
let py1 = (y1 >> sy).min(full.rows);
|
||||
*pl = Plane::region(px0, py0, px1 - px0, py1 - py0);
|
||||
}
|
||||
FrameBuf {
|
||||
planes,
|
||||
num_planes: self.num_planes,
|
||||
subsampling_x: self.subsampling_x,
|
||||
subsampling_y: self.subsampling_y,
|
||||
bit_depth: self.bit_depth,
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn plane_width(&self, plane: usize) -> usize {
|
||||
if plane == 0 { self.width } else { (self.width + self.subsampling_x) >> self.subsampling_x }
|
||||
}
|
||||
|
||||
pub fn plane_height(&self, plane: usize) -> usize {
|
||||
if plane == 0 { self.height } else { (self.height + self.subsampling_y) >> self.subsampling_y }
|
||||
}
|
||||
}
|
||||
|
||||
/// A motion vector (row, col) in 1/8 sample units.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Mv {
|
||||
pub row: i16,
|
||||
pub col: i16,
|
||||
}
|
||||
|
||||
impl Mv {
|
||||
pub const ZERO: Mv = Mv { row: 0, col: 0 };
|
||||
pub fn new(row: i32, col: i32) -> Mv {
|
||||
Mv { row: row as i16, col: col as i16 }
|
||||
}
|
||||
}
|
||||
|
||||
/// Mode info stored for every 4x4 luma position (the arrays the spec indexes by
|
||||
/// `[ row ][ col ]` in mode-info units).
|
||||
#[derive(Clone, Default)]
|
||||
pub struct MiInfo {
|
||||
/// Frame size in mode info units.
|
||||
pub cols: usize,
|
||||
pub rows: usize,
|
||||
/// Stored region: origin and width (the whole frame unless a tile's private copy).
|
||||
pub ox: usize,
|
||||
pub oy: usize,
|
||||
pub stride: usize,
|
||||
pub region_rows: usize,
|
||||
pub y_mode: Vec<u8>,
|
||||
pub uv_mode: Vec<u8>,
|
||||
pub ref_frame: Vec<[i8; 2]>,
|
||||
pub mv: Vec<[Mv; 2]>,
|
||||
pub is_inter: Vec<bool>,
|
||||
pub skip_mode: Vec<bool>,
|
||||
pub skip: Vec<bool>,
|
||||
pub tx_size: Vec<u8>,
|
||||
pub inter_tx_size: Vec<u8>,
|
||||
pub mi_size: Vec<u8>,
|
||||
pub segment_id: Vec<u8>,
|
||||
pub palette_size: [Vec<u8>; 2],
|
||||
pub palette_colors: [Vec<[u16; 8]>; 2],
|
||||
pub delta_lf: Vec<[i8; 4]>,
|
||||
pub comp_group_idx: Vec<u8>,
|
||||
pub compound_idx: Vec<u8>,
|
||||
pub interp_filter: Vec<[u8; 2]>,
|
||||
pub motion_mode: Vec<u8>,
|
||||
/// TxTypes[ row ][ col ] (luma 4x4 units).
|
||||
pub tx_type: Vec<u8>,
|
||||
/// RefFrames[ row ][ col ] has been written for this frame.
|
||||
pub written: Vec<bool>,
|
||||
}
|
||||
|
||||
impl MiInfo {
|
||||
/// `palette`: whether palette mode can occur (allow_screen_content_tools); without it the
|
||||
/// palette arrays (40 bytes per unit) stay empty.
|
||||
pub fn new(cols: usize, rows: usize, palette: bool) -> MiInfo {
|
||||
MiInfo::region(cols, rows, 0, 0, cols, rows, palette)
|
||||
}
|
||||
|
||||
/// Storage for the w x h mode info units at (ox, oy) of a cols x rows frame.
|
||||
pub fn region(cols: usize, rows: usize, ox: usize, oy: usize, w: usize, h: usize, palette: bool) -> MiInfo {
|
||||
let n = w * h;
|
||||
let np = if palette { n } else { 0 };
|
||||
MiInfo {
|
||||
cols,
|
||||
rows,
|
||||
ox,
|
||||
oy,
|
||||
stride: w,
|
||||
region_rows: h,
|
||||
y_mode: vec![0; n],
|
||||
uv_mode: vec![0; n],
|
||||
ref_frame: vec![[0, -1]; n],
|
||||
mv: vec![[Mv::ZERO; 2]; n],
|
||||
is_inter: vec![false; n],
|
||||
skip_mode: vec![false; n],
|
||||
skip: vec![false; n],
|
||||
tx_size: vec![0; n],
|
||||
inter_tx_size: vec![0; n],
|
||||
mi_size: vec![0; n],
|
||||
segment_id: vec![0; n],
|
||||
palette_size: [vec![0; np], vec![0; np]],
|
||||
palette_colors: [vec![[0; 8]; np], vec![[0; 8]; np]],
|
||||
delta_lf: vec![[0; 4]; n],
|
||||
comp_group_idx: vec![0; n],
|
||||
compound_idx: vec![0; n],
|
||||
interp_filter: vec![[0; 2]; n],
|
||||
motion_mode: vec![0; n],
|
||||
tx_type: vec![0; n],
|
||||
written: vec![false; n],
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn idx(&self, row: usize, col: usize) -> usize {
|
||||
debug_assert!(col >= self.ox && col < self.ox + self.stride && row >= self.oy, "mode info ({row}, {col}) outside the stored region");
|
||||
(row - self.oy) * self.stride + col - self.ox
|
||||
}
|
||||
|
||||
/// Copy this region into the whole-frame `dst`.
|
||||
pub fn copy_into(&self, dst: &mut MiInfo) {
|
||||
let (w, h) = (self.stride, self.region_rows);
|
||||
macro_rules! copy {
|
||||
($($f:ident)*) => {$(
|
||||
for y in 0..h {
|
||||
let d = dst.idx(self.oy + y, self.ox);
|
||||
dst.$f[d..d + w].copy_from_slice(&self.$f[y * w..(y + 1) * w]);
|
||||
}
|
||||
)*};
|
||||
}
|
||||
copy!(y_mode uv_mode ref_frame mv is_inter skip_mode skip tx_size inter_tx_size mi_size segment_id delta_lf comp_group_idx compound_idx interp_filter motion_mode tx_type written);
|
||||
for k in (0..2).filter(|&k| !self.palette_size[k].is_empty()) {
|
||||
for y in 0..h {
|
||||
let d = dst.idx(self.oy + y, self.ox);
|
||||
dst.palette_size[k][d..d + w].copy_from_slice(&self.palette_size[k][y * w..(y + 1) * w]);
|
||||
dst.palette_colors[k][d..d + w].copy_from_slice(&self.palette_colors[k][y * w..(y + 1) * w]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,455 @@
|
||||
//! Film grain synthesis (7.18.3), applied to output pictures only (references stay clean).
|
||||
|
||||
use crate::Picture;
|
||||
use crate::header::{FilmGrainParams, SequenceHeader};
|
||||
use crate::spec_tables::{GAUSSIAN_SEQUENCE, MC_IDENTITY};
|
||||
|
||||
#[inline]
|
||||
/// One plane's scaling lookup (7.18.3.4) from its `n` points. The header rejects points whose
|
||||
/// values don't strictly increase; a pair that still doesn't (defence in depth) is skipped
|
||||
/// instead of dividing by zero. No points: all zero.
|
||||
fn scaling_lut(n: usize, xs: &[u8], ys: &[u8]) -> [i32; 256] {
|
||||
let mut l = [0i32; 256];
|
||||
let n = n.min(xs.len()).min(ys.len());
|
||||
let (Some(&x0), Some(&y0)) = (xs.first(), ys.first()) else { return l };
|
||||
if n == 0 {
|
||||
return l;
|
||||
}
|
||||
for v in l.iter_mut().take(x0 as usize) {
|
||||
*v = y0 as i32;
|
||||
}
|
||||
for i in 0..n - 1 {
|
||||
let (xa, xb, ya, yb) = (xs[i] as i32, xs[i + 1] as i32, ys[i] as i32, ys[i + 1] as i32);
|
||||
let dx = xb - xa;
|
||||
if dx <= 0 {
|
||||
continue;
|
||||
}
|
||||
let delta = (yb - ya) * ((65536 + (dx >> 1)) / dx);
|
||||
for x in 0..dx {
|
||||
if let Some(v) = l.get_mut((xa + x) as usize) {
|
||||
*v = ya + ((x * delta + 32768) >> 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
let (xl, yl) = (xs[n - 1], ys[n - 1]);
|
||||
for v in l.iter_mut().skip(xl as usize) {
|
||||
*v = yl as i32;
|
||||
}
|
||||
l
|
||||
}
|
||||
|
||||
fn round2(x: i32, n: u32) -> i32 {
|
||||
if n == 0 { x } else { (x + (1 << (n - 1))) >> n }
|
||||
}
|
||||
|
||||
struct Rng(u32);
|
||||
|
||||
impl Rng {
|
||||
/// get_random_number( bits ) (7.18.3.2)
|
||||
fn next(&mut self, bits: u32) -> i32 {
|
||||
let r = self.0;
|
||||
let bit = (r ^ (r >> 1) ^ (r >> 3) ^ (r >> 12)) & 1;
|
||||
let r = (r >> 1) | (bit << 15);
|
||||
self.0 = r;
|
||||
((r >> (16 - bits)) & ((1 << bits) - 1)) as i32
|
||||
}
|
||||
}
|
||||
|
||||
/// Picture-level parameters shared by the grain synthesis stages.
|
||||
struct GrainCtx<'a> {
|
||||
g: &'a FilmGrainParams,
|
||||
bd: u32,
|
||||
mono: bool,
|
||||
sub_x: usize,
|
||||
sub_y: usize,
|
||||
w: usize,
|
||||
h: usize,
|
||||
grain_min: i32,
|
||||
grain_max: i32,
|
||||
}
|
||||
|
||||
impl GrainCtx<'_> {
|
||||
fn num_planes(&self) -> usize {
|
||||
if self.mono { 1 } else { 3 }
|
||||
}
|
||||
|
||||
fn grain_shift(&self) -> u32 {
|
||||
12 - self.bd + self.g.grain_scale_shift
|
||||
}
|
||||
|
||||
fn ar_shift(&self) -> u32 {
|
||||
self.g.ar_coeff_shift_minus_6 + 6
|
||||
}
|
||||
|
||||
fn lag(&self) -> i32 {
|
||||
self.g.ar_coeff_lag as i32
|
||||
}
|
||||
|
||||
fn stripe_w(&self) -> usize {
|
||||
self.w + 64
|
||||
}
|
||||
}
|
||||
|
||||
type GrainTemplate = Vec<[i32; 82]>;
|
||||
|
||||
/// The luma, Cb and Cr grain templates (7.18.3.3).
|
||||
struct Templates {
|
||||
luma: GrainTemplate,
|
||||
cb: GrainTemplate,
|
||||
cr: GrainTemplate,
|
||||
}
|
||||
|
||||
impl Templates {
|
||||
fn plane(&self, plane: usize) -> &GrainTemplate {
|
||||
match plane {
|
||||
0 => &self.luma,
|
||||
1 => &self.cb,
|
||||
_ => &self.cr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn luma_ar_sum(g: &FilmGrainParams, luma: &[[i32; 82]], lag: i32, y: usize, x: usize) -> i32 {
|
||||
let mut s = 0;
|
||||
let mut pos = 0;
|
||||
'outer: for dr in -lag..=0 {
|
||||
for dc in -lag..=lag {
|
||||
if dr == 0 && dc == 0 {
|
||||
break 'outer;
|
||||
}
|
||||
let c = g.ar_coeffs_y_plus_128[pos] as i32 - 128;
|
||||
s += luma[(y as i32 + dr) as usize][(x as i32 + dc) as usize] * c;
|
||||
pos += 1;
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn luma_grain(c: &GrainCtx, rng: &mut Rng) -> GrainTemplate {
|
||||
let g = c.g;
|
||||
let shift = c.grain_shift();
|
||||
let mut luma = vec![[0i32; 82]; 73];
|
||||
for row in luma.iter_mut() {
|
||||
for v in row.iter_mut() {
|
||||
let gv = if g.num_y_points > 0 { GAUSSIAN_SEQUENCE[rng.next(11) as usize] as i32 } else { 0 };
|
||||
*v = round2(gv, shift);
|
||||
}
|
||||
}
|
||||
let ar_shift = c.ar_shift();
|
||||
let lag = c.lag();
|
||||
for y in 3..73 {
|
||||
for x in 3..82 - 3 {
|
||||
let s = luma_ar_sum(g, &luma, lag, y, x);
|
||||
luma[y][x] = (luma[y][x] + round2(s, ar_shift)).clamp(c.grain_min, c.grain_max);
|
||||
}
|
||||
}
|
||||
luma
|
||||
}
|
||||
|
||||
/// The luma contribution to the chroma autoregressive filter at chroma position (y, x).
|
||||
fn chroma_luma_term(c: &GrainCtx, luma: &[[i32; 82]], y: usize, x: usize) -> i32 {
|
||||
let mut l = 0;
|
||||
let lx = ((x - 3) << c.sub_x) + 3;
|
||||
let ly = ((y - 3) << c.sub_y) + 3;
|
||||
for i in 0..=c.sub_y {
|
||||
for j in 0..=c.sub_x {
|
||||
l += luma[ly + i][lx + j];
|
||||
}
|
||||
}
|
||||
round2(l, (c.sub_x + c.sub_y) as u32)
|
||||
}
|
||||
|
||||
fn chroma_ar_sums(c: &GrainCtx, t: &Templates, y: usize, x: usize) -> (i32, i32) {
|
||||
let g = c.g;
|
||||
let lag = c.lag();
|
||||
let mut s0 = 0;
|
||||
let mut s1 = 0;
|
||||
let mut pos = 0;
|
||||
'outer: for dr in -lag..=0 {
|
||||
for dc in -lag..=lag {
|
||||
let c0 = g.ar_coeffs_cb_plus_128[pos] as i32 - 128;
|
||||
let c1 = g.ar_coeffs_cr_plus_128[pos] as i32 - 128;
|
||||
if dr == 0 && dc == 0 {
|
||||
if g.num_y_points > 0 {
|
||||
let l = chroma_luma_term(c, &t.luma, y, x);
|
||||
s0 += l * c0;
|
||||
s1 += l * c1;
|
||||
}
|
||||
break 'outer;
|
||||
}
|
||||
let (yy, xx) = ((y as i32 + dr) as usize, (x as i32 + dc) as usize);
|
||||
s0 += t.cb[yy][xx] * c0;
|
||||
s1 += t.cr[yy][xx] * c1;
|
||||
pos += 1;
|
||||
}
|
||||
}
|
||||
(s0, s1)
|
||||
}
|
||||
|
||||
/// Chroma grain templates; zero for monochrome pictures.
|
||||
fn chroma_grain(c: &GrainCtx, rng: &mut Rng, luma: GrainTemplate) -> Templates {
|
||||
let g = c.g;
|
||||
let mut t = Templates { luma, cb: vec![[0i32; 82]; 73], cr: vec![[0i32; 82]; 73] };
|
||||
if c.mono {
|
||||
return t;
|
||||
}
|
||||
let shift = c.grain_shift();
|
||||
let ar_shift = c.ar_shift();
|
||||
let chroma_w = if c.sub_x != 0 { 44 } else { 82 };
|
||||
let chroma_h = if c.sub_y != 0 { 38 } else { 73 };
|
||||
for (arr, seed, on) in [(&mut t.cb, 0xb524u32, g.num_cb_points > 0), (&mut t.cr, 0x49d8, g.num_cr_points > 0)] {
|
||||
rng.0 = g.grain_seed ^ seed;
|
||||
for row in arr.iter_mut().take(chroma_h) {
|
||||
for v in row.iter_mut().take(chroma_w) {
|
||||
let gv = if on || g.chroma_scaling_from_luma { GAUSSIAN_SEQUENCE[rng.next(11) as usize] as i32 } else { 0 };
|
||||
*v = round2(gv, shift);
|
||||
}
|
||||
}
|
||||
}
|
||||
for y in 3..chroma_h {
|
||||
for x in 3..chroma_w - 3 {
|
||||
let (s0, s1) = chroma_ar_sums(c, &t, y, x);
|
||||
t.cb[y][x] = (t.cb[y][x] + round2(s0, ar_shift)).clamp(c.grain_min, c.grain_max);
|
||||
t.cr[y][x] = (t.cr[y][x] + round2(s1, ar_shift)).clamp(c.grain_min, c.grain_max);
|
||||
}
|
||||
}
|
||||
t
|
||||
}
|
||||
|
||||
/// Scaling lookup tables (7.18.3.4), one per plane.
|
||||
fn scaling_luts(g: &FilmGrainParams, num_planes: usize) -> [[i32; 256]; 3] {
|
||||
let mut lut = [[0i32; 256]; 3];
|
||||
for (plane, l) in lut.iter_mut().enumerate().take(num_planes) {
|
||||
let (n, xs, ys) = if plane == 0 || g.chroma_scaling_from_luma {
|
||||
(g.num_y_points, &g.point_y_value, &g.point_y_scaling)
|
||||
} else if plane == 1 {
|
||||
(g.num_cb_points, &g.point_cb_value, &g.point_cb_scaling)
|
||||
} else {
|
||||
(g.num_cr_points, &g.point_cr_value, &g.point_cr_scaling)
|
||||
};
|
||||
*l = scaling_lut(n, xs, ys);
|
||||
}
|
||||
lut
|
||||
}
|
||||
|
||||
fn scale_lookup(lut: &[[i32; 256]; 3], bd: u32, plane: usize, index: i32) -> i32 {
|
||||
let sh = bd - 8;
|
||||
let x = index >> sh;
|
||||
let rem = index - (x << sh);
|
||||
if bd == 8 || x == 255 {
|
||||
lut[plane][x as usize]
|
||||
} else {
|
||||
let start = lut[plane][x as usize];
|
||||
let end = lut[plane][x as usize + 1];
|
||||
start + round2((end - start) * rem, sh)
|
||||
}
|
||||
}
|
||||
|
||||
/// Fills one plane of a noise stripe from the grain template at stripe column `x`.
|
||||
fn fill_stripe_plane(c: &GrainCtx, src: &GrainTemplate, s: &mut [i32], plane: usize, offsets: (usize, usize), x: usize) {
|
||||
let (off_x, off_y) = offsets;
|
||||
let stripe_w = c.stripe_w();
|
||||
let overlap = c.g.overlap_flag;
|
||||
let psx = if plane > 0 { c.sub_x } else { 0 };
|
||||
let psy = if plane > 0 { c.sub_y } else { 0 };
|
||||
let pox = if psx != 0 { 6 + off_x } else { 9 + off_x * 2 };
|
||||
let poy = if psy != 0 { 6 + off_y } else { 9 + off_y * 2 };
|
||||
for i in 0..(34 >> psy) {
|
||||
for j in 0..(34 >> psx) {
|
||||
let mut gv = src[poy + i][pox + j];
|
||||
let (idx, blend) = if psx == 0 { (i * stripe_w + x * 2 + j, j < 2) } else { (i * stripe_w + x + j, j == 0) };
|
||||
if blend && overlap && x > 0 {
|
||||
let old = s[idx];
|
||||
gv = if psx != 0 {
|
||||
old * 23 + gv * 22
|
||||
} else if j == 0 {
|
||||
old * 27 + gv * 17
|
||||
} else {
|
||||
old * 17 + gv * 27
|
||||
};
|
||||
gv = round2(gv, 5).clamp(c.grain_min, c.grain_max);
|
||||
}
|
||||
s[idx] = gv;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Noise stripes (7.18.3.5).
|
||||
struct Stripes {
|
||||
data: Vec<[Vec<i32>; 3]>,
|
||||
stripe_w: usize,
|
||||
overlap: bool,
|
||||
sub_y: usize,
|
||||
grain_min: i32,
|
||||
grain_max: i32,
|
||||
}
|
||||
|
||||
impl Stripes {
|
||||
fn build(c: &GrainCtx, t: &Templates, rng: &mut Rng) -> Stripes {
|
||||
let g = c.g;
|
||||
let stripe_w = c.stripe_w();
|
||||
let n_stripes = (c.h.div_ceil(2)).div_ceil(16).max(1);
|
||||
let mut data: Vec<[Vec<i32>; 3]> = (0..n_stripes).map(|_| [vec![0; 34 * stripe_w], vec![0; 34 * stripe_w], vec![0; 34 * stripe_w]]).collect();
|
||||
for (luma_num, stripe) in data.iter_mut().enumerate() {
|
||||
let mut r = g.grain_seed;
|
||||
r ^= (((luma_num * 37 + 178) & 255) << 8) as u32;
|
||||
r ^= ((luma_num * 173 + 105) & 255) as u32;
|
||||
rng.0 = r;
|
||||
let mut x = 0;
|
||||
while x < c.w.div_ceil(2) {
|
||||
let rand = rng.next(8) as usize;
|
||||
let offsets = (rand >> 4, rand & 15);
|
||||
for (plane, s) in stripe.iter_mut().enumerate().take(c.num_planes()) {
|
||||
fill_stripe_plane(c, t.plane(plane), s, plane, offsets, x);
|
||||
}
|
||||
x += 16;
|
||||
}
|
||||
}
|
||||
Stripes { data, stripe_w, overlap: g.overlap_flag, sub_y: c.sub_y, grain_min: c.grain_min, grain_max: c.grain_max }
|
||||
}
|
||||
|
||||
fn noise_at(&self, plane: usize, y: usize, x: usize) -> i32 {
|
||||
let psy = if plane > 0 { self.sub_y } else { 0 };
|
||||
let luma_num = y >> (5 - psy);
|
||||
let i = y - (luma_num << (5 - psy));
|
||||
let mut gv = self.data[luma_num][plane][i * self.stripe_w + x];
|
||||
if psy == 0 {
|
||||
if i < 2 && luma_num > 0 && self.overlap {
|
||||
let old = self.data[luma_num - 1][plane][(i + 32) * self.stripe_w + x];
|
||||
gv = if i == 0 { old * 27 + gv * 17 } else { old * 17 + gv * 27 };
|
||||
gv = round2(gv, 5).clamp(self.grain_min, self.grain_max);
|
||||
}
|
||||
} else if i < 1 && luma_num > 0 && self.overlap {
|
||||
let old = self.data[luma_num - 1][plane][(i + 16) * self.stripe_w + x];
|
||||
gv = round2(old * 23 + gv * 22, 5).clamp(self.grain_min, self.grain_max);
|
||||
}
|
||||
gv
|
||||
}
|
||||
}
|
||||
|
||||
/// Output clipping range and scaling shift for the blend step (7.18.3.5).
|
||||
struct BlendRange {
|
||||
min_value: i32,
|
||||
max_luma: i32,
|
||||
max_chroma: i32,
|
||||
pix_max: i32,
|
||||
scaling_shift: u32,
|
||||
}
|
||||
|
||||
impl BlendRange {
|
||||
fn new(c: &GrainCtx, matrix_coefficients: usize) -> BlendRange {
|
||||
let bd = c.bd;
|
||||
let (min_value, max_luma, max_chroma) = if c.g.clip_to_restricted_range {
|
||||
let ml = 235 << (bd - 8);
|
||||
(16 << (bd - 8), ml, if matrix_coefficients == MC_IDENTITY { ml } else { 240 << (bd - 8) })
|
||||
} else {
|
||||
let m = (256 << (bd - 8)) - 1;
|
||||
(0, m, m)
|
||||
};
|
||||
BlendRange { min_value, max_luma, max_chroma, pix_max: (1i32 << bd) - 1, scaling_shift: c.g.grain_scaling_minus_8 + 8 }
|
||||
}
|
||||
}
|
||||
|
||||
struct BlendEnv<'a> {
|
||||
c: &'a GrainCtx<'a>,
|
||||
lut: &'a [[i32; 256]; 3],
|
||||
stripes: &'a Stripes,
|
||||
range: &'a BlendRange,
|
||||
}
|
||||
|
||||
impl BlendEnv<'_> {
|
||||
fn blend_chroma(&self, py: &[u16], pu: &mut [u16], pv: &mut [u16]) {
|
||||
let c = self.c;
|
||||
let g = c.g;
|
||||
let (w, h, sub_x, sub_y, bd) = (c.w, c.h, c.sub_x, c.sub_y, c.bd);
|
||||
let cw = (w + sub_x) >> sub_x;
|
||||
let ch = (h + sub_y) >> sub_y;
|
||||
for y in 0..ch {
|
||||
for x in 0..cw {
|
||||
let lx = x << sub_x;
|
||||
let ly = y << sub_y;
|
||||
let lnx = (lx + 1).min(w - 1);
|
||||
let avg = if sub_x != 0 { round2(py[ly * w + lx] as i32 + py[ly * w + lnx] as i32, 1) } else { py[ly * w + lx] as i32 };
|
||||
for (plane, out, on, luma_mult, mult, offset) in [
|
||||
(1usize, &mut *pu, g.num_cb_points > 0, g.cb_luma_mult, g.cb_mult, g.cb_offset),
|
||||
(2, &mut *pv, g.num_cr_points > 0, g.cr_luma_mult, g.cr_mult, g.cr_offset),
|
||||
] {
|
||||
if !(on || g.chroma_scaling_from_luma) {
|
||||
continue;
|
||||
}
|
||||
let orig = out[y * cw + x] as i32;
|
||||
let merged = if g.chroma_scaling_from_luma {
|
||||
avg
|
||||
} else {
|
||||
let combined = avg * (luma_mult as i32 - 128) + orig * (mult as i32 - 128);
|
||||
((combined >> 6) + ((offset as i32 - 256) << (bd - 8))).clamp(0, self.range.pix_max)
|
||||
};
|
||||
let noise = round2(scale_lookup(self.lut, bd, plane, merged) * self.stripes.noise_at(plane, y, x), self.range.scaling_shift);
|
||||
out[y * cw + x] = (orig + noise).clamp(self.range.min_value, self.range.max_chroma) as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn blend_luma(&self, py: &mut [u16]) {
|
||||
let (w, h) = (self.c.w, self.c.h);
|
||||
for y in 0..h {
|
||||
for x in 0..w {
|
||||
let orig = py[y * w + x] as i32;
|
||||
let noise = round2(scale_lookup(self.lut, self.c.bd, 0, orig) * self.stripes.noise_at(0, y, x), self.range.scaling_shift);
|
||||
py[y * w + x] = (orig + noise).clamp(self.range.min_value, self.range.max_luma) as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn apply(pic: &mut Picture, g: &FilmGrainParams, seq: &SequenceHeader) {
|
||||
let _ = seq;
|
||||
let bd = pic.bit_depth as u32;
|
||||
let grain_center = 128i32 << (bd - 8);
|
||||
let c = GrainCtx {
|
||||
g,
|
||||
bd,
|
||||
mono: pic.mono_chrome,
|
||||
sub_x: pic.subsampling_x as usize,
|
||||
sub_y: pic.subsampling_y as usize,
|
||||
w: pic.width as usize,
|
||||
h: pic.height as usize,
|
||||
grain_min: -grain_center,
|
||||
grain_max: (256i32 << (bd - 8)) - 1 - grain_center,
|
||||
};
|
||||
|
||||
// Generate grain (7.18.3.3).
|
||||
let mut rng = Rng(g.grain_seed);
|
||||
let luma = luma_grain(&c, &mut rng);
|
||||
let templates = chroma_grain(&c, &mut rng, luma);
|
||||
let lut = scaling_luts(g, c.num_planes());
|
||||
let stripes = Stripes::build(&c, &templates, &mut rng);
|
||||
|
||||
// Blend (7.18.3.5).
|
||||
let range = BlendRange::new(&c, pic.matrix_coefficients as usize);
|
||||
let env = BlendEnv { c: &c, lut: &lut, stripes: &stripes, range: &range };
|
||||
if !c.mono {
|
||||
let [py, pu, pv] = &mut pic.planes;
|
||||
env.blend_chroma(py, pu, pv);
|
||||
}
|
||||
if g.num_y_points > 0 {
|
||||
env.blend_luma(&mut pic.planes[0]);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn scaling_lut_interpolates_and_tolerates_duplicate_points() {
|
||||
let l = scaling_lut(2, &[0, 255], &[0, 255]);
|
||||
assert_eq!((l[0], l[128], l[255]), (0, 128, 255));
|
||||
// Duplicate and descending x values don't divide by zero.
|
||||
let l = scaling_lut(4, &[10, 10, 5, 200], &[20, 40, 60, 80]);
|
||||
assert_eq!(l[0], 20);
|
||||
assert_eq!(l[255], 80);
|
||||
assert_eq!(scaling_lut(0, &[], &[]), [0; 256]);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,839 @@
|
||||
//! Inter prediction (spec 7.11.3): motion vector scaling, block inter prediction, warped
|
||||
//! motion, masks (wedge, difference weight, inter-intra), distance weights and OBMC.
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use crate::frame::Plane;
|
||||
use crate::mvpred::round2_signed64;
|
||||
use crate::spec_tables::{
|
||||
BLOCK_8X8, BLOCK_SIZES, COMPOUND_AVERAGE, COMPOUND_DIFFWTD, COMPOUND_DISTANCE, COMPOUND_INTRA, COMPOUND_WEDGE, DIV_LUT, DIV_LUT_BITS,
|
||||
DIV_LUT_PREC_BITS, EIGHTTAP, EIGHTTAP_SHARP, EIGHTTAP_SMOOTH, FILTER_BITS, GLOBAL_GLOBALMV, GLOBALMV, II_H_PRED, II_SMOOTH_PRED, II_V_PRED,
|
||||
II_WEIGHTS_1D, INTRA_FRAME, LAST_FRAME, LOCALWARP, LS_MV_MAX, MASK_MASTER_SIZE, MAX_FRAME_DISTANCE, MAX_SB_SIZE, MI_HEIGHT_LOG2, MI_WIDTH_LOG2,
|
||||
NUM_4X4_BLOCKS_HIGH, NUM_4X4_BLOCKS_WIDE, OBMC, OBMC_MASK_2, OBMC_MASK_4, OBMC_MASK_8, OBMC_MASK_16, OBMC_MASK_32, QUANT_DIST_LOOKUP,
|
||||
QUANT_DIST_WEIGHT, REF_SCALE_SHIFT, SCALE_SUBPEL_BITS, SUBPEL_BITS, SUBPEL_FILTERS, SUBPEL_MASK, TRANSLATION, WARP_PARAM_REDUCE_BITS,
|
||||
WARPED_FILTERS, WARPEDDIFF_PREC_BITS, WARPEDMODEL_NONDIAGAFFINE_CLAMP, WARPEDMODEL_PREC_BITS, WARPEDMODEL_TRANS_CLAMP, WARPEDPIXEL_PREC_SHIFTS,
|
||||
WEDGE_BITS, WEDGE_CODEBOOK, WEDGE_HORIZONTAL, WEDGE_MASTER_OBLIQUE_EVEN, WEDGE_MASTER_OBLIQUE_ODD, WEDGE_MASTER_VERTICAL, WEDGE_OBLIQUE27,
|
||||
WEDGE_OBLIQUE63, WEDGE_OBLIQUE117, WEDGE_OBLIQUE153, WEDGE_TYPES, WEDGE_VERTICAL,
|
||||
};
|
||||
use crate::tile::TileDecoder;
|
||||
|
||||
const PRED_STRIDE: usize = 128;
|
||||
|
||||
#[inline(always)]
|
||||
fn round2(x: i32, n: u32) -> i32 {
|
||||
if n == 0 { x } else { (x + (1 << (n - 1))) >> n }
|
||||
}
|
||||
|
||||
/// Rounding variables (7.11.3.2): (InterRound0, InterRound1, InterPostRound).
|
||||
pub(crate) fn rounding(is_compound: bool, bd: u32) -> (u32, u32, u32) {
|
||||
let mut r0 = 3;
|
||||
let mut r1 = if is_compound { 7 } else { 11 };
|
||||
if bd == 12 {
|
||||
r0 += 2;
|
||||
if !is_compound {
|
||||
r1 -= 2;
|
||||
}
|
||||
}
|
||||
(r0, r1, 2 * FILTER_BITS as u32 - (r0 + r1))
|
||||
}
|
||||
|
||||
/// Resolve divisor process (7.11.3.7): (divShift, divFactor).
|
||||
fn resolve_divisor(d: i64) -> (i32, i64) {
|
||||
let a = d.unsigned_abs();
|
||||
let n = 63 - a.leading_zeros() as i32;
|
||||
let e = a as i64 - (1i64 << n);
|
||||
let f = if n > DIV_LUT_BITS as i32 { round2_signed64(e, (n - DIV_LUT_BITS as i32) as u32) } else { e << (DIV_LUT_BITS as i32 - n) };
|
||||
let shift = n + DIV_LUT_PREC_BITS as i32;
|
||||
let factor = DIV_LUT[f as usize] as i64;
|
||||
(shift, if d < 0 { -factor } else { factor })
|
||||
}
|
||||
|
||||
/// Setup shear process (7.11.3.6): (warpValid, alpha, beta, gamma, delta).
|
||||
pub(crate) fn setup_shear(p: &[i32; 6]) -> (bool, i32, i32, i32, i32) {
|
||||
let prec = 1i64 << WARPEDMODEL_PREC_BITS;
|
||||
let alpha0 = (p[2] as i64 - prec).clamp(-32768, 32767);
|
||||
let beta0 = (p[3] as i64).clamp(-32768, 32767);
|
||||
let (div_shift, div_factor) = resolve_divisor(p[2] as i64);
|
||||
let v = (p[4] as i64) << WARPEDMODEL_PREC_BITS;
|
||||
let gamma0 = round2_signed_wide(v as i128 * div_factor as i128, div_shift as u32).clamp(-32768, 32767);
|
||||
let w = p[3] as i64 * p[4] as i64;
|
||||
let delta0 = (p[5] as i64 - round2_signed_wide(w as i128 * div_factor as i128, div_shift as u32) - prec).clamp(-32768, 32767);
|
||||
let rb = WARP_PARAM_REDUCE_BITS as u32;
|
||||
let red = |x: i64| (round2_signed64(x, rb) << rb) as i32;
|
||||
let (alpha, beta, gamma, delta) = (red(alpha0), red(beta0), red(gamma0), red(delta0));
|
||||
let valid =
|
||||
!(4 * alpha.abs() + 7 * beta.abs() >= (1 << WARPEDMODEL_PREC_BITS) || 4 * gamma.abs() + 4 * delta.abs() >= (1 << WARPEDMODEL_PREC_BITS));
|
||||
(valid, alpha, beta, gamma, delta)
|
||||
}
|
||||
|
||||
fn round2_signed_wide(x: i128, n: u32) -> i64 {
|
||||
if n == 0 {
|
||||
return x as i64;
|
||||
}
|
||||
(if x >= 0 { (x + (1 << (n - 1))) >> n } else { -((-x + (1 << (n - 1))) >> n) }) as i64
|
||||
}
|
||||
|
||||
/// WedgeMasks[ bsize ][ flipSign ][ wedge ] (7.11.3.11), stored as w*h bytes each.
|
||||
fn wedge_masks() -> &'static Vec<Vec<[Vec<u8>; 2]>> {
|
||||
static M: OnceLock<Vec<Vec<[Vec<u8>; 2]>>> = OnceLock::new();
|
||||
M.get_or_init(|| {
|
||||
let n = MASK_MASTER_SIZE;
|
||||
let mut master = vec![[[0u8; 64]; 64]; 6];
|
||||
for j in 0..n {
|
||||
let mut shift = (n / 4) as i32;
|
||||
let mut i = 0;
|
||||
while i < n {
|
||||
master[WEDGE_OBLIQUE63][i][j] = WEDGE_MASTER_OBLIQUE_EVEN[(j as i32 - shift).clamp(0, n as i32 - 1) as usize];
|
||||
shift -= 1;
|
||||
master[WEDGE_OBLIQUE63][i + 1][j] = WEDGE_MASTER_OBLIQUE_ODD[(j as i32 - shift).clamp(0, n as i32 - 1) as usize];
|
||||
master[WEDGE_VERTICAL][i][j] = WEDGE_MASTER_VERTICAL[j];
|
||||
master[WEDGE_VERTICAL][i + 1][j] = WEDGE_MASTER_VERTICAL[j];
|
||||
i += 2;
|
||||
}
|
||||
}
|
||||
for i in 0..n {
|
||||
for j in 0..n {
|
||||
let msk = master[WEDGE_OBLIQUE63][i][j];
|
||||
master[WEDGE_OBLIQUE27][j][i] = msk;
|
||||
master[WEDGE_OBLIQUE117][i][n - 1 - j] = 64 - msk;
|
||||
master[WEDGE_OBLIQUE153][n - 1 - j][i] = 64 - msk;
|
||||
master[WEDGE_HORIZONTAL][j][i] = master[WEDGE_VERTICAL][i][j];
|
||||
}
|
||||
}
|
||||
let mut out = vec![Vec::new(); BLOCK_SIZES];
|
||||
for bsize in BLOCK_8X8..BLOCK_SIZES {
|
||||
if WEDGE_BITS[bsize] == 0 {
|
||||
continue;
|
||||
}
|
||||
let w = 4 * NUM_4X4_BLOCKS_WIDE[bsize] as usize;
|
||||
let h = 4 * NUM_4X4_BLOCKS_HIGH[bsize] as usize;
|
||||
let w4 = NUM_4X4_BLOCKS_WIDE[bsize];
|
||||
let h4 = NUM_4X4_BLOCKS_HIGH[bsize];
|
||||
let shape = if h4 > w4 {
|
||||
0
|
||||
} else if h4 < w4 {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
};
|
||||
for wedge in 0..WEDGE_TYPES {
|
||||
let cb = &WEDGE_CODEBOOK[shape][wedge];
|
||||
let dir = cb[0] as usize;
|
||||
let xoff = n / 2 - ((cb[1] as usize * w) >> 3);
|
||||
let yoff = n / 2 - ((cb[2] as usize * h) >> 3);
|
||||
let mut sum = 0usize;
|
||||
for i in 0..w {
|
||||
sum += master[dir][yoff][xoff + i] as usize;
|
||||
}
|
||||
for i in 1..h {
|
||||
sum += master[dir][yoff + i][xoff] as usize;
|
||||
}
|
||||
let avg = (sum + (w + h - 1) / 2) / (w + h - 1);
|
||||
let flip = (avg < 32) as usize;
|
||||
let mut a = vec![0u8; w * h];
|
||||
let mut b = vec![0u8; w * h];
|
||||
for i in 0..h {
|
||||
for j in 0..w {
|
||||
let m = master[dir][yoff + i][xoff + j];
|
||||
a[i * w + j] = m;
|
||||
b[i * w + j] = 64 - m;
|
||||
}
|
||||
}
|
||||
let pair = if flip == 0 { [a, b] } else { [b, a] };
|
||||
out[bsize].push(pair);
|
||||
}
|
||||
}
|
||||
out
|
||||
})
|
||||
}
|
||||
|
||||
impl TileDecoder<'_, '_> {
|
||||
/// Reference plane and its (lastX, lastY) for refIdx (-1: the current frame for intra BC).
|
||||
fn ref_plane_dims(&self, ref_idx: i32, plane: usize) -> (i32, i32) {
|
||||
let (sx, sy) = if plane > 0 { (self.fs.ssx as i32, self.fs.ssy as i32) } else { (0, 0) };
|
||||
let (uw, fh) = if ref_idx < 0 {
|
||||
((self.fs.fh.mi_cols * 4) as i32, (self.fs.fh.mi_rows * 4) as i32)
|
||||
} else {
|
||||
let ri = &self.fs.ref_info[ref_idx as usize];
|
||||
(ri.upscaled_width as i32, ri.frame_height as i32)
|
||||
};
|
||||
(((uw + sx) >> sx) - 1, ((fh + sy) >> sy) - 1)
|
||||
}
|
||||
|
||||
/// Motion vector scaling process (7.11.3.3): (startX, startY, stepX, stepY).
|
||||
fn mv_scaling(&self, plane: usize, ref_idx: i32, x: i32, y: i32, mv: [i32; 2]) -> (i32, i32, i32, i32) {
|
||||
let fh = &self.fs.fh;
|
||||
let (ref_uw, ref_h) = if ref_idx < 0 {
|
||||
(fh.upscaled_width as i64, fh.frame_height as i64)
|
||||
} else {
|
||||
let ri = &self.fs.ref_info[ref_idx as usize];
|
||||
(ri.upscaled_width as i64, ri.frame_height as i64)
|
||||
};
|
||||
let fw = fh.frame_width as i64;
|
||||
let fhh = fh.frame_height as i64;
|
||||
let rs = REF_SCALE_SHIFT as i64;
|
||||
let x_scale = ((ref_uw << rs) + fw / 2) / fw;
|
||||
let y_scale = ((ref_h << rs) + fhh / 2) / fhh;
|
||||
let (sx, sy) = if plane > 0 { (self.fs.ssx as i32, self.fs.ssy as i32) } else { (0, 0) };
|
||||
let half = 1i64 << (SUBPEL_BITS - 1);
|
||||
let orig_x = ((x as i64) << SUBPEL_BITS) + ((2 * mv[1]) >> sx) as i64 + half;
|
||||
let orig_y = ((y as i64) << SUBPEL_BITS) + ((2 * mv[0]) >> sy) as i64 + half;
|
||||
let base_x = orig_x * x_scale - (half << rs);
|
||||
let base_y = orig_y * y_scale - (half << rs);
|
||||
let off = (1i64 << (SCALE_SUBPEL_BITS - SUBPEL_BITS)) / 2;
|
||||
let sh = (REF_SCALE_SHIFT + SUBPEL_BITS - SCALE_SUBPEL_BITS) as u32;
|
||||
let start_x = round2_signed64(base_x, sh) + off;
|
||||
let start_y = round2_signed64(base_y, sh) + off;
|
||||
let step_x = round2_signed64(x_scale, (REF_SCALE_SHIFT - SCALE_SUBPEL_BITS) as u32);
|
||||
let step_y = round2_signed64(y_scale, (REF_SCALE_SHIFT - SCALE_SUBPEL_BITS) as u32);
|
||||
(start_x as i32, start_y as i32, step_x as i32, step_y as i32)
|
||||
}
|
||||
|
||||
fn ref_plane(&self, ref_idx: i32, plane: usize) -> &Plane {
|
||||
// A missing reference (rejected earlier for valid streams) predicts from the current frame.
|
||||
match usize::try_from(ref_idx).ok().and_then(|i| self.fs.refs.get(i)).and_then(|r| r.as_ref()) {
|
||||
Some(r) => &r.buf.planes[plane],
|
||||
None => &self.t.cur.planes[plane],
|
||||
}
|
||||
}
|
||||
|
||||
/// Block inter prediction process (7.11.3.4) into `pred` (stride PRED_STRIDE); `tmp` holds
|
||||
/// the intermediate (horizontally filtered) rows.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn block_inter_prediction(
|
||||
&self,
|
||||
plane: usize,
|
||||
ref_idx: i32,
|
||||
x: i32,
|
||||
y: i32,
|
||||
x_step: i32,
|
||||
y_step: i32,
|
||||
w: usize,
|
||||
h: usize,
|
||||
filters: [u8; 2],
|
||||
rnd: (u32, u32, u32),
|
||||
pred: &mut [i32],
|
||||
tmp: &mut [i32],
|
||||
) {
|
||||
let refp = self.ref_plane(ref_idx, plane);
|
||||
let (last_x, last_y) = self.ref_plane_dims(ref_idx, plane);
|
||||
let mut fh = filters[1] as usize;
|
||||
if w <= 4 {
|
||||
if fh == EIGHTTAP || fh == EIGHTTAP_SHARP {
|
||||
fh = 4;
|
||||
} else if fh == EIGHTTAP_SMOOTH {
|
||||
fh = 5;
|
||||
}
|
||||
}
|
||||
let mut fv = filters[0] as usize;
|
||||
if h <= 4 {
|
||||
if fv == EIGHTTAP || fv == EIGHTTAP_SHARP {
|
||||
fv = 4;
|
||||
} else if fv == EIGHTTAP_SMOOTH {
|
||||
fv = 5;
|
||||
}
|
||||
}
|
||||
let unit = 1 << SCALE_SUBPEL_BITS;
|
||||
if x_step == unit && y_step == unit {
|
||||
mc_unscaled(
|
||||
refp,
|
||||
last_x,
|
||||
last_y,
|
||||
x,
|
||||
y,
|
||||
w,
|
||||
h,
|
||||
&SUBPEL_FILTERS[fh][((x >> 6) & SUBPEL_MASK as i32) as usize],
|
||||
&SUBPEL_FILTERS[fv][((y >> 6) & SUBPEL_MASK as i32) as usize],
|
||||
rnd,
|
||||
pred,
|
||||
tmp,
|
||||
);
|
||||
return;
|
||||
}
|
||||
let inter_h = ((((h as i32 - 1) * y_step + (1 << SCALE_SUBPEL_BITS) - 1) >> SCALE_SUBPEL_BITS) + 8) as usize;
|
||||
for r in 0..inter_h {
|
||||
let ry = ((y >> 10) + r as i32 - 3).clamp(0, last_y) as usize;
|
||||
let row = refp.row(ry);
|
||||
for c in 0..w {
|
||||
let p = x + x_step * c as i32;
|
||||
let filt = &SUBPEL_FILTERS[fh][((p >> 6) & SUBPEL_MASK as i32) as usize];
|
||||
let mut s = 0i32;
|
||||
let bx = (p >> 10) - 3;
|
||||
for t in 0..8 {
|
||||
s += filt[t] as i32 * row[(bx + t as i32).clamp(0, last_x) as usize] as i32;
|
||||
}
|
||||
tmp[r * w + c] = round2(s, rnd.0);
|
||||
}
|
||||
}
|
||||
for r in 0..h {
|
||||
for c in 0..w {
|
||||
let p = (y & 1023) + y_step * r as i32;
|
||||
let filt = &SUBPEL_FILTERS[fv][((p >> 6) & SUBPEL_MASK as i32) as usize];
|
||||
let base = (p >> 10) as usize;
|
||||
let mut s = 0i32;
|
||||
for t in 0..8 {
|
||||
s += filt[t] as i32 * tmp[(base + t) * w + c];
|
||||
}
|
||||
pred[r * PRED_STRIDE + c] = round2(s, rnd.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Block warp process (7.11.3.5) for the 8x8 section (i8, j8).
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn block_warp(
|
||||
&self,
|
||||
params: &[i32; 6],
|
||||
plane: usize,
|
||||
ref_idx: i32,
|
||||
x: i32,
|
||||
y: i32,
|
||||
i8: usize,
|
||||
j8: usize,
|
||||
w: usize,
|
||||
h: usize,
|
||||
rnd: (u32, u32, u32),
|
||||
pred: &mut [i32],
|
||||
) {
|
||||
let refp = self.ref_plane(ref_idx, plane);
|
||||
let (last_x, last_y) = self.ref_plane_dims(ref_idx, plane);
|
||||
let (sx, sy) = if plane > 0 { (self.fs.ssx as i32, self.fs.ssy as i32) } else { (0, 0) };
|
||||
let src_x = (x + j8 as i32 * 8 + 4) << sx;
|
||||
let src_y = (y + i8 as i32 * 8 + 4) << sy;
|
||||
let dst_x = params[2] as i64 * src_x as i64 + params[3] as i64 * src_y as i64 + params[0] as i64;
|
||||
let dst_y = params[4] as i64 * src_x as i64 + params[5] as i64 * src_y as i64 + params[1] as i64;
|
||||
let (_, alpha, beta, gamma, delta) = setup_shear(params);
|
||||
let x4 = dst_x >> sx;
|
||||
let y4 = dst_y >> sy;
|
||||
let ix4 = (x4 >> WARPEDMODEL_PREC_BITS) as i32;
|
||||
let sx4 = (x4 & ((1 << WARPEDMODEL_PREC_BITS) - 1)) as i32;
|
||||
let iy4 = (y4 >> WARPEDMODEL_PREC_BITS) as i32;
|
||||
let sy4 = (y4 & ((1 << WARPEDMODEL_PREC_BITS) - 1)) as i32;
|
||||
let mut inter = [[0i32; 8]; 15];
|
||||
for i1 in -7..8i32 {
|
||||
let row = refp.row((iy4 + i1).clamp(0, last_y) as usize);
|
||||
for i2 in -4..4i32 {
|
||||
let sxv = sx4 + alpha * i2 + beta * i1;
|
||||
let offs = (round2(sxv, WARPEDDIFF_PREC_BITS as u32) + WARPEDPIXEL_PREC_SHIFTS as i32) as usize;
|
||||
let mut s = 0i32;
|
||||
for i3 in 0..8i32 {
|
||||
s += WARPED_FILTERS[offs][i3 as usize] as i32 * row[(ix4 + i2 - 3 + i3).clamp(0, last_x) as usize] as i32;
|
||||
}
|
||||
inter[(i1 + 7) as usize][(i2 + 4) as usize] = round2(s, rnd.0);
|
||||
}
|
||||
}
|
||||
let lim_i = 4.min(h as i32 - i8 as i32 * 8 - 4);
|
||||
let lim_j = 4.min(w as i32 - j8 as i32 * 8 - 4);
|
||||
for i1 in -4..lim_i {
|
||||
for i2 in -4..lim_j {
|
||||
let syv = sy4 + gamma * i2 + delta * i1;
|
||||
let offs = (round2(syv, WARPEDDIFF_PREC_BITS as u32) + WARPEDPIXEL_PREC_SHIFTS as i32) as usize;
|
||||
let mut s = 0i32;
|
||||
for i3 in 0..8 {
|
||||
s += WARPED_FILTERS[offs][i3] as i32 * inter[(i1 + i3 as i32 + 4) as usize][(i2 + 4) as usize];
|
||||
}
|
||||
pred[(i8 * 8 + (i1 + 4) as usize) * PRED_STRIDE + j8 * 8 + (i2 + 4) as usize] = round2(s, rnd.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Warp estimation process (7.11.3.8): sets LocalWarpParams / LocalValid.
|
||||
pub(crate) fn warp_estimation(&mut self) {
|
||||
let mut a = [[0i64; 2]; 2];
|
||||
let mut bx = [0i64; 2];
|
||||
let mut by = [0i64; 2];
|
||||
let mid_y = self.b.mi_row as i64 * 4 + self.b.bh4 as i64 * 2 - 1;
|
||||
let mid_x = self.b.mi_col as i64 * 4 + self.b.bw4 as i64 * 2 - 1;
|
||||
let suy = mid_y * 8;
|
||||
let sux = mid_x * 8;
|
||||
let duy = suy + self.b.mv_i[0][0] as i64;
|
||||
let dux = sux + self.b.mv_i[0][1] as i64;
|
||||
let ls = |a: i64, b: i64| ((a * b) >> 2) + (a + b);
|
||||
for i in 0..self.mvs.num_samples {
|
||||
let c = self.mvs.cand_list[i];
|
||||
let sy = c[0] as i64 - suy;
|
||||
let sx = c[1] as i64 - sux;
|
||||
let dy = c[2] as i64 - duy;
|
||||
let dx = c[3] as i64 - dux;
|
||||
if (sx - dx).abs() < LS_MV_MAX as i64 && (sy - dy).abs() < LS_MV_MAX as i64 {
|
||||
a[0][0] += ls(sx, sx) + 8;
|
||||
a[0][1] += ls(sx, sy) + 4;
|
||||
a[1][1] += ls(sy, sy) + 8;
|
||||
bx[0] += ls(sx, dx) + 8;
|
||||
bx[1] += ls(sy, dx) + 4;
|
||||
by[0] += ls(sx, dy) + 4;
|
||||
by[1] += ls(sy, dy) + 8;
|
||||
}
|
||||
}
|
||||
let det = a[0][0] * a[1][1] - a[0][1] * a[0][1];
|
||||
self.b.local_valid = det != 0;
|
||||
if det == 0 {
|
||||
return;
|
||||
}
|
||||
let (mut div_shift, mut div_factor) = resolve_divisor(det);
|
||||
div_shift -= WARPEDMODEL_PREC_BITS as i32;
|
||||
if div_shift < 0 {
|
||||
div_factor <<= -div_shift;
|
||||
div_shift = 0;
|
||||
}
|
||||
let prec = 1i64 << WARPEDMODEL_PREC_BITS;
|
||||
let cl = WARPEDMODEL_NONDIAGAFFINE_CLAMP as i64;
|
||||
let nondiag = |v: i64| round2_signed_wide(v as i128 * div_factor as i128, div_shift as u32).clamp(-cl + 1, cl - 1);
|
||||
let diag = |v: i64| round2_signed_wide(v as i128 * div_factor as i128, div_shift as u32).clamp(prec - cl + 1, prec + cl - 1);
|
||||
let mut p = [0i64; 6];
|
||||
p[2] = diag(a[1][1] * bx[0] - a[0][1] * bx[1]);
|
||||
p[3] = nondiag(-a[0][1] * bx[0] + a[0][0] * bx[1]);
|
||||
p[4] = nondiag(a[1][1] * by[0] - a[0][1] * by[1]);
|
||||
p[5] = diag(-a[0][1] * by[0] + a[0][0] * by[1]);
|
||||
let mvx = self.b.mv_i[0][1] as i64;
|
||||
let mvy = self.b.mv_i[0][0] as i64;
|
||||
let vx = mvx * (1 << (WARPEDMODEL_PREC_BITS - 3)) - (mid_x * (p[2] - prec) + mid_y * p[3]);
|
||||
let vy = mvy * (1 << (WARPEDMODEL_PREC_BITS - 3)) - (mid_x * p[4] + mid_y * (p[5] - prec));
|
||||
let tc = WARPEDMODEL_TRANS_CLAMP as i64;
|
||||
p[0] = vx.clamp(-tc, tc - 1);
|
||||
p[1] = vy.clamp(-tc, tc - 1);
|
||||
for i in 0..6 {
|
||||
self.b.local_warp_params[i] = p[i] as i32;
|
||||
}
|
||||
}
|
||||
|
||||
/// Inter prediction process (7.11.3.1) for a w x h region of `plane` at (x, y).
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn predict_inter(&mut self, plane: usize, x: usize, y: usize, w: usize, h: usize, cand_row: usize, cand_col: usize) {
|
||||
let bd = self.fs.bit_depth;
|
||||
let mi_i = self.t.mi.idx(cand_row, cand_col);
|
||||
let cand_refs = self.t.mi.ref_frame[mi_i];
|
||||
let cand_mvs = self.t.mi.mv[mi_i];
|
||||
let filters = self.t.mi.interp_filter[mi_i];
|
||||
let is_compound = cand_refs[1] > INTRA_FRAME as i8;
|
||||
let rnd = rounding(is_compound, bd);
|
||||
if plane == 0 && self.b.motion_mode as usize == LOCALWARP {
|
||||
self.warp_estimation();
|
||||
if self.b.local_valid {
|
||||
let p = self.b.local_warp_params;
|
||||
self.b.local_valid = setup_shear(&p).0;
|
||||
}
|
||||
}
|
||||
let mut preds = [std::mem::take(&mut self.pred_buf[0]), std::mem::take(&mut self.pred_buf[1])];
|
||||
let mut tmp = std::mem::take(&mut self.mc_tmp);
|
||||
let fh = &self.fs.fh;
|
||||
for ref_list in 0..1 + is_compound as usize {
|
||||
let ref_frame = cand_refs[ref_list];
|
||||
let ref_idx: i32 = if self.b.use_intrabc { -1 } else { fh.ref_frame_idx[ref_frame as usize - LAST_FRAME] as i32 };
|
||||
let use_warp = self.warp_mode(ref_frame, ref_idx, w, h);
|
||||
let mv = [cand_mvs[ref_list].row as i32, cand_mvs[ref_list].col as i32];
|
||||
if use_warp != 0 {
|
||||
let params = if use_warp == 1 { self.b.local_warp_params } else { fh.gm_params[ref_frame as usize] };
|
||||
for i8 in 0..=((h - 1) >> 3) {
|
||||
for j8 in 0..=((w - 1) >> 3) {
|
||||
self.block_warp(¶ms, plane, ref_idx, x as i32, y as i32, i8, j8, w, h, rnd, &mut preds[ref_list]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let (sx, sy, stx, sty) = self.mv_scaling(plane, ref_idx, x as i32, y as i32, mv);
|
||||
self.block_inter_prediction(plane, ref_idx, sx, sy, stx, sty, w, h, filters, rnd, &mut preds[ref_list], &mut tmp);
|
||||
}
|
||||
}
|
||||
let ct = self.b.compound_type;
|
||||
self.fill_blend_mask(plane, w, h, &preds, rnd.2);
|
||||
let (fwd, bck) = if ct == COMPOUND_DISTANCE { self.distance_weights(cand_refs) } else { (0, 0) };
|
||||
let max = (1i32 << bd) - 1;
|
||||
let is_inter_intra = self.b.is_inter && self.b.ref_frame[1] == INTRA_FRAME as i8;
|
||||
if !is_compound && !is_inter_intra {
|
||||
let pl = &mut self.t.cur.planes[plane];
|
||||
for i in 0..h {
|
||||
let row = &mut pl.row_from_mut(y + i, x)[..w];
|
||||
for j in 0..w {
|
||||
row[j] = preds[0][i * PRED_STRIDE + j].clamp(0, max) as u16;
|
||||
}
|
||||
}
|
||||
} else if ct == COMPOUND_AVERAGE {
|
||||
let pl = &mut self.t.cur.planes[plane];
|
||||
for i in 0..h {
|
||||
let row = &mut pl.row_from_mut(y + i, x)[..w];
|
||||
for j in 0..w {
|
||||
let k = i * PRED_STRIDE + j;
|
||||
row[j] = round2(preds[0][k] + preds[1][k], 1 + rnd.2).clamp(0, max) as u16;
|
||||
}
|
||||
}
|
||||
} else if ct == COMPOUND_DISTANCE {
|
||||
let pl = &mut self.t.cur.planes[plane];
|
||||
for i in 0..h {
|
||||
let row = &mut pl.row_from_mut(y + i, x)[..w];
|
||||
for j in 0..w {
|
||||
let k = i * PRED_STRIDE + j;
|
||||
row[j] = round2(fwd * preds[0][k] + bck * preds[1][k], 4 + rnd.2).clamp(0, max) as u16;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.mask_blend(&preds, plane, x, y, w, h, rnd.2, max);
|
||||
}
|
||||
self.pred_buf = preds;
|
||||
self.mc_tmp = tmp;
|
||||
if self.b.motion_mode as usize == OBMC {
|
||||
self.overlapped_motion_compensation(plane, w, h);
|
||||
}
|
||||
}
|
||||
|
||||
/// Warp selection for one reference: 0 none, 1 local warp, 2 global warp.
|
||||
fn warp_mode(&self, ref_frame: i8, ref_idx: i32, w: usize, h: usize) -> u8 {
|
||||
let fh = &self.fs.fh;
|
||||
let ym = self.b.y_mode;
|
||||
let global =
|
||||
(ym == GLOBALMV || ym == GLOBAL_GLOBALMV) && ref_frame > INTRA_FRAME as i8 && fh.gm_type[ref_frame as usize] as usize > TRANSLATION;
|
||||
let global_valid = global && setup_shear(&fh.gm_params[ref_frame as usize]).0;
|
||||
if w < 8 || h < 8 || fh.force_integer_mv {
|
||||
0
|
||||
} else if self.b.motion_mode as usize == LOCALWARP && self.b.local_valid {
|
||||
1
|
||||
} else if global && !self.is_scaled_ref(ref_idx) && global_valid {
|
||||
2
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the blend mask (wedge, inter-intra or difference-weighted) for the current compound type.
|
||||
fn fill_blend_mask(&mut self, plane: usize, w: usize, h: usize, preds: &[Vec<i32>; 2], round_post: u32) {
|
||||
let bd = self.fs.bit_depth;
|
||||
let ct = self.b.compound_type;
|
||||
if ct == COMPOUND_WEDGE && plane == 0 {
|
||||
let m = &wedge_masks()[self.b.mi_size][self.b.wedge_index][self.b.wedge_sign];
|
||||
for i in 0..h {
|
||||
for j in 0..w {
|
||||
self.mask[i * PRED_STRIDE + j] = m[i * w + j] as i32;
|
||||
}
|
||||
}
|
||||
} else if ct == COMPOUND_INTRA {
|
||||
let size_scale = MAX_SB_SIZE / h.max(w);
|
||||
let im = self.b.interintra_mode;
|
||||
for i in 0..h {
|
||||
for j in 0..w {
|
||||
self.mask[i * PRED_STRIDE + j] = if im == II_V_PRED {
|
||||
II_WEIGHTS_1D[i * size_scale] as i32
|
||||
} else if im == II_H_PRED {
|
||||
II_WEIGHTS_1D[j * size_scale] as i32
|
||||
} else if im == II_SMOOTH_PRED {
|
||||
II_WEIGHTS_1D[i.min(j) * size_scale] as i32
|
||||
} else {
|
||||
32
|
||||
};
|
||||
}
|
||||
}
|
||||
} else if ct == COMPOUND_DIFFWTD && plane == 0 {
|
||||
for i in 0..h {
|
||||
for j in 0..w {
|
||||
let k = i * PRED_STRIDE + j;
|
||||
let diff = (preds[0][k] - preds[1][k]).abs();
|
||||
let diff = round2(diff, (bd - 8) + round_post);
|
||||
let m = (38 + diff / 16).clamp(0, 64);
|
||||
self.mask[k] = if self.b.mask_type == 1 { 64 - m } else { m };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_scaled_ref(&self, ref_idx: i32) -> bool {
|
||||
if ref_idx < 0 {
|
||||
return false;
|
||||
}
|
||||
let fh = &self.fs.fh;
|
||||
let ri = &self.fs.ref_info[ref_idx as usize];
|
||||
let xs = ((ri.upscaled_width << REF_SCALE_SHIFT) + fh.frame_width / 2) / fh.frame_width;
|
||||
let ys = ((ri.frame_height << REF_SCALE_SHIFT) + fh.frame_height / 2) / fh.frame_height;
|
||||
xs != 1 << REF_SCALE_SHIFT || ys != 1 << REF_SCALE_SHIFT
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn mask_blend(&mut self, preds: &[Vec<i32>; 2], plane: usize, dx: usize, dy: usize, w: usize, h: usize, post: u32, max: i32) {
|
||||
let (sx, sy) = if plane > 0 { (self.fs.ssx, self.fs.ssy) } else { (0, 0) };
|
||||
let interintra = self.b.interintra;
|
||||
let wedge_ii = self.b.wedge_interintra;
|
||||
for yy in 0..h {
|
||||
for xx in 0..w {
|
||||
let mk = |r: usize, c: usize| self.mask[r * PRED_STRIDE + c];
|
||||
let m = if (sx == 0 && sy == 0) || (interintra && !wedge_ii) {
|
||||
mk(yy, xx)
|
||||
} else if sx == 1 && sy == 0 {
|
||||
round2(mk(yy, 2 * xx) + mk(yy, 2 * xx + 1), 1)
|
||||
} else {
|
||||
round2(mk(2 * yy, 2 * xx) + mk(2 * yy, 2 * xx + 1) + mk(2 * yy + 1, 2 * xx) + mk(2 * yy + 1, 2 * xx + 1), 2)
|
||||
};
|
||||
let k = yy * PRED_STRIDE + xx;
|
||||
let pl = &mut self.t.cur.planes[plane];
|
||||
if interintra {
|
||||
let p0 = round2(preds[0][k], post).clamp(0, max);
|
||||
let p1 = pl.at(dx + xx, dy + yy) as i32;
|
||||
pl.set(dx + xx, dy + yy, round2(m * p1 + (64 - m) * p0, 6) as u16);
|
||||
} else {
|
||||
let v = round2(m * preds[0][k] + (64 - m) * preds[1][k], 6 + post).clamp(0, max);
|
||||
pl.set(dx + xx, dy + yy, v as u16);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn distance_weights(&self, refs: [i8; 2]) -> (i32, i32) {
|
||||
let fh = &self.fs.fh;
|
||||
let mut dist = [0i32; 2];
|
||||
for (rl, d) in dist.iter_mut().enumerate() {
|
||||
let h = fh.order_hints[refs[rl] as usize];
|
||||
*d = crate::header::relative_dist(&self.fs.seq, h, fh.order_hint).abs().clamp(0, MAX_FRAME_DISTANCE as i32);
|
||||
}
|
||||
let d0 = dist[1];
|
||||
let d1 = dist[0];
|
||||
let order = (d0 <= d1) as usize;
|
||||
if d0 == 0 || d1 == 0 {
|
||||
return (QUANT_DIST_LOOKUP[3][order] as i32, QUANT_DIST_LOOKUP[3][1 - order] as i32);
|
||||
}
|
||||
let mut i = 0;
|
||||
while i < 3 {
|
||||
let c0 = QUANT_DIST_WEIGHT[i][order] as i32;
|
||||
let c1 = QUANT_DIST_WEIGHT[i][1 - order] as i32;
|
||||
if order == 1 {
|
||||
if d0 * c0 > d1 * c1 {
|
||||
break;
|
||||
}
|
||||
} else if d0 * c0 < d1 * c1 {
|
||||
break;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
(QUANT_DIST_LOOKUP[i][order] as i32, QUANT_DIST_LOOKUP[i][1 - order] as i32)
|
||||
}
|
||||
|
||||
/// Overlapped motion compensation (7.11.3.9).
|
||||
fn overlapped_motion_compensation(&mut self, plane: usize, w: usize, h: usize) {
|
||||
let (sx, sy) = if plane > 0 { (self.fs.ssx, self.fs.ssy) } else { (0, 0) };
|
||||
let (mi_rows, mi_cols) = (self.fs.fh.mi_rows as usize, self.fs.fh.mi_cols as usize);
|
||||
let bd = self.fs.bit_depth;
|
||||
let max = (1i32 << bd) - 1;
|
||||
let mut obmc = std::mem::take(&mut self.pred_buf[0]);
|
||||
let mut tmp = std::mem::take(&mut self.mc_tmp);
|
||||
let get_mask = |len: usize| -> &'static [u8] {
|
||||
match len {
|
||||
2 => &OBMC_MASK_2,
|
||||
4 => &OBMC_MASK_4,
|
||||
8 => &OBMC_MASK_8,
|
||||
16 => &OBMC_MASK_16,
|
||||
_ => &OBMC_MASK_32,
|
||||
}
|
||||
};
|
||||
let mut jobs: Vec<(usize, usize, usize, usize, usize, usize, usize)> = Vec::new(); // (pass, cand_row, cand_col, x4, y4, predW, predH)
|
||||
if self.b.avail_u && self.fs.plane_residual_size(self.b.mi_size, plane) >= BLOCK_8X8 {
|
||||
let w4 = self.b.bw4;
|
||||
let mut x4 = self.b.mi_col;
|
||||
let y4 = self.b.mi_row;
|
||||
let mut n = 0;
|
||||
let limit = 4.min(MI_WIDTH_LOG2[self.b.mi_size] as usize);
|
||||
while n < limit && x4 < mi_cols.min(self.b.mi_col + w4) {
|
||||
let cr = self.b.mi_row - 1;
|
||||
let cc = x4 | 1;
|
||||
let mi = &self.t.mi;
|
||||
let ci = mi.idx(cr, cc);
|
||||
let step4 = (NUM_4X4_BLOCKS_WIDE[mi.mi_size[ci] as usize] as usize).clamp(2, 16);
|
||||
if mi.ref_frame[ci][0] > INTRA_FRAME as i8 {
|
||||
n += 1;
|
||||
let pw = w.min((step4 * 4) >> sx);
|
||||
let ph = (h >> 1).min(32 >> sy);
|
||||
jobs.push((0, cr, cc, x4, y4, pw, ph));
|
||||
}
|
||||
x4 += step4;
|
||||
}
|
||||
}
|
||||
if self.b.avail_l {
|
||||
let h4 = self.b.bh4;
|
||||
let x4 = self.b.mi_col;
|
||||
let mut y4 = self.b.mi_row;
|
||||
let mut n = 0;
|
||||
let limit = 4.min(MI_HEIGHT_LOG2[self.b.mi_size] as usize);
|
||||
while n < limit && y4 < mi_rows.min(self.b.mi_row + h4) {
|
||||
let cc = self.b.mi_col - 1;
|
||||
let cr = y4 | 1;
|
||||
let mi = &self.t.mi;
|
||||
let ci = mi.idx(cr, cc);
|
||||
let step4 = (NUM_4X4_BLOCKS_HIGH[mi.mi_size[ci] as usize] as usize).clamp(2, 16);
|
||||
if mi.ref_frame[ci][0] > INTRA_FRAME as i8 {
|
||||
n += 1;
|
||||
let pw = (w >> 1).min(32 >> sx);
|
||||
let ph = h.min((step4 * 4) >> sy);
|
||||
jobs.push((1, cr, cc, x4, y4, pw, ph));
|
||||
}
|
||||
y4 += step4;
|
||||
}
|
||||
}
|
||||
// the above pass completes before the left pass reads samples; jobs keep that order
|
||||
for (pass, cr, cc, x4, y4, pw, ph) in jobs {
|
||||
let ci = self.t.mi.idx(cr, cc);
|
||||
let m = self.t.mi.mv[ci][0];
|
||||
let mv = [m.row as i32, m.col as i32];
|
||||
let ref_idx = self.fs.fh.ref_frame_idx[self.t.mi.ref_frame[ci][0] as usize - LAST_FRAME] as i32;
|
||||
let px = (x4 * 4) >> sx;
|
||||
let py = (y4 * 4) >> sy;
|
||||
let (stx, sty, sxs, sys) = self.mv_scaling(plane, ref_idx, px as i32, py as i32, mv);
|
||||
let filters = self.t.mi.interp_filter[ci];
|
||||
let rnd = rounding(false, bd);
|
||||
self.block_inter_prediction(plane, ref_idx, stx, sty, sxs, sys, pw, ph, filters, rnd, &mut obmc, &mut tmp);
|
||||
let mask = get_mask(if pass == 0 { ph } else { pw });
|
||||
let pl = &mut self.t.cur.planes[plane];
|
||||
for i in 0..ph {
|
||||
for j in 0..pw {
|
||||
let mm = if pass == 0 { mask[i] } else { mask[j] } as i32;
|
||||
let o = obmc[i * PRED_STRIDE + j].clamp(0, max);
|
||||
let c = pl.at(px + j, py + i) as i32;
|
||||
pl.set(px + j, py + i, round2(mm * c + (64 - mm) * o, 6) as u16);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.pred_buf[0] = obmc;
|
||||
self.mc_tmp = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
const IDENTITY_TAPS: [i16; 8] = [0, 0, 0, 128, 0, 0, 0, 0];
|
||||
|
||||
/// Horizontal pass of one row for a block `W` wide: taps outer, columns inner, fixed width so
|
||||
/// the row stays in registers.
|
||||
#[inline(always)]
|
||||
fn hpass<const W: usize>(src: &[u16], hf: &[i16; 8], out: &mut [i32], half: i32, sh: u32) {
|
||||
let mut acc = [half; W];
|
||||
for t in 0..8 {
|
||||
let f = hf[t] as i32;
|
||||
let Some(p) = src.get(t..).and_then(<[_]>::first_chunk::<W>) else {
|
||||
return;
|
||||
};
|
||||
for j in 0..W {
|
||||
acc[j] += f * p[j] as i32;
|
||||
}
|
||||
}
|
||||
let Some(o) = out.first_chunk_mut::<W>() else {
|
||||
return;
|
||||
};
|
||||
for j in 0..W {
|
||||
o[j] = acc[j] >> sh;
|
||||
}
|
||||
}
|
||||
|
||||
/// Vertical pass of one output row from 8 intermediate rows (`rows`, stride `W`).
|
||||
#[inline(always)]
|
||||
fn vpass<const W: usize>(rows: &[i32], vf: &[i16; 8], out: &mut [i32], half: i32, sh: u32) {
|
||||
let mut acc = [half; W];
|
||||
for t in 0..8 {
|
||||
let f = vf[t] as i32;
|
||||
let Some(p) = rows.get(t * W..).and_then(<[_]>::first_chunk::<W>) else {
|
||||
return;
|
||||
};
|
||||
for j in 0..W {
|
||||
acc[j] += f * p[j];
|
||||
}
|
||||
}
|
||||
let Some(o) = out.first_chunk_mut::<W>() else {
|
||||
return;
|
||||
};
|
||||
for j in 0..W {
|
||||
o[j] = acc[j] >> sh;
|
||||
}
|
||||
}
|
||||
|
||||
/// Block inter prediction without reference scaling: every column / row uses the same filter
|
||||
/// phase, so the filters run over whole rows (bit-identical to the per-sample process).
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn mc_unscaled(
|
||||
refp: &Plane,
|
||||
last_x: i32,
|
||||
last_y: i32,
|
||||
x: i32,
|
||||
y: i32,
|
||||
w: usize,
|
||||
h: usize,
|
||||
hf: &[i16; 8],
|
||||
vf: &[i16; 8],
|
||||
rnd: (u32, u32, u32),
|
||||
pred: &mut [i32],
|
||||
tmp: &mut [i32],
|
||||
) {
|
||||
let x0 = (x >> 10) - 3;
|
||||
let y0 = (y >> 10) - 3;
|
||||
let h_copy = *hf == IDENTITY_TAPS;
|
||||
let v_copy = *vf == IDENTITY_TAPS;
|
||||
// With the identity vertical phase only intermediate rows 3 .. 3 + h have a non-zero tap.
|
||||
let (r_lo, r_hi) = if v_copy { (3, 3 + h) } else { (0, h + 7) };
|
||||
let inside = x0 >= 0 && x0 + w as i32 + 7 <= last_x + 1;
|
||||
let mut edge = [0u16; 128 + 8];
|
||||
let mut acc = [0i32; 128];
|
||||
let (r0, r1) = (rnd.0, rnd.1);
|
||||
let (half0, half1) = (1i32 << (r0 - 1), 1i32 << (r1 - 1));
|
||||
for r in r_lo..r_hi {
|
||||
let ry = (y0 + r as i32).clamp(0, last_y) as usize;
|
||||
let row = refp.row(ry);
|
||||
let src: &[u16] = if inside {
|
||||
&row[x0 as usize..x0 as usize + w + 7]
|
||||
} else {
|
||||
for (k, e) in edge[..w + 7].iter_mut().enumerate() {
|
||||
*e = row[(x0 + k as i32).clamp(0, last_x) as usize];
|
||||
}
|
||||
&edge[..w + 7]
|
||||
};
|
||||
let out = &mut tmp[r * w..r * w + w];
|
||||
if h_copy {
|
||||
// Round2(128 * v, InterRound0) is exact.
|
||||
for (o, &v) in out.iter_mut().zip(&src[3..3 + w]) {
|
||||
*o = (v as i32) << (7 - r0);
|
||||
}
|
||||
} else {
|
||||
match w {
|
||||
2 => hpass::<2>(src, hf, out, half0, r0),
|
||||
4 => hpass::<4>(src, hf, out, half0, r0),
|
||||
8 => hpass::<8>(src, hf, out, half0, r0),
|
||||
16 => hpass::<16>(src, hf, out, half0, r0),
|
||||
32 => hpass::<32>(src, hf, out, half0, r0),
|
||||
64 => hpass::<64>(src, hf, out, half0, r0),
|
||||
_ => {
|
||||
let acc = &mut acc[..w];
|
||||
acc.fill(0);
|
||||
for t in 0..8 {
|
||||
let f = hf[t] as i32;
|
||||
for (a, &v) in acc.iter_mut().zip(&src[t..t + w]) {
|
||||
*a += f * v as i32;
|
||||
}
|
||||
}
|
||||
for (o, &a) in out.iter_mut().zip(acc.iter()) {
|
||||
*o = (a + half0) >> r0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for r in 0..h {
|
||||
let out = &mut pred[r * PRED_STRIDE..r * PRED_STRIDE + w];
|
||||
if v_copy {
|
||||
for (o, &v) in out.iter_mut().zip(&tmp[(r + 3) * w..(r + 4) * w]) {
|
||||
*o = (128 * v + half1) >> r1;
|
||||
}
|
||||
} else {
|
||||
let rows = &tmp[r * w..(r + 8) * w];
|
||||
match w {
|
||||
2 => vpass::<2>(rows, vf, out, half1, r1),
|
||||
4 => vpass::<4>(rows, vf, out, half1, r1),
|
||||
8 => vpass::<8>(rows, vf, out, half1, r1),
|
||||
16 => vpass::<16>(rows, vf, out, half1, r1),
|
||||
32 => vpass::<32>(rows, vf, out, half1, r1),
|
||||
64 => vpass::<64>(rows, vf, out, half1, r1),
|
||||
_ => {
|
||||
let acc = &mut acc[..w];
|
||||
acc.fill(0);
|
||||
for t in 0..8 {
|
||||
let f = vf[t] as i32;
|
||||
for (a, &v) in acc.iter_mut().zip(&tmp[(r + t) * w..(r + t + 1) * w]) {
|
||||
*a += f * v;
|
||||
}
|
||||
}
|
||||
for (o, &a) in out.iter_mut().zip(acc.iter()) {
|
||||
*o = (a + half1) >> r1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,777 @@
|
||||
//! Inter frame mode info syntax (5.11.7 / 5.11.18 – 5.11.33) with its CDF selection (8.3.2).
|
||||
|
||||
use crate::Result;
|
||||
use crate::header::{EIGHTTAP, SWITCHABLE};
|
||||
use crate::mvpred::MvP;
|
||||
use crate::spec_tables::{
|
||||
ALTREF_FRAME, ALTREF2_FRAME, BLOCK_8X8, BLOCK_32X32, BLOCK_64X64, BLOCK_128X128, BWDREF_FRAME, CLASS0_SIZE, COMP_NEWMV_CTXS, COMPOUND_AVERAGE,
|
||||
COMPOUND_DIFFWTD, COMPOUND_DISTANCE, COMPOUND_INTRA, COMPOUND_MODE_CTX_MAP, COMPOUND_REFERENCE, COMPOUND_WEDGE, GLOBAL_GLOBALMV, GLOBALMV,
|
||||
GOLDEN_FRAME, INTRA_FRAME, INTRABC_DELAY_PIXELS, LAST_FRAME, LAST2_FRAME, LAST3_FRAME, LOCALWARP, MV_CLASS_0, MV_INTRABC_CONTEXT,
|
||||
MV_JOINT_HNZVNZ, MV_JOINT_HNZVZ, MV_JOINT_HZVNZ, NEAR_NEARMV, NEAR_NEWMV, NEAREST_NEARESTMV, NEAREST_NEWMV, NEARESTMV, NEARMV, NEW_NEARESTMV,
|
||||
NEW_NEARMV, NEW_NEWMV, NEWMV, NUM_4X4_BLOCKS_HIGH, NUM_4X4_BLOCKS_WIDE, OBMC, REF_SCALE_SHIFT, SEG_LVL_GLOBALMV, SEG_LVL_REF_FRAME, SEG_LVL_SKIP,
|
||||
SIMPLE, SINGLE_REFERENCE, SIZE_GROUP, TRANSLATION, UNIDIR_COMP_REFERENCE, UV_CFL_PRED, WEDGE_BITS,
|
||||
};
|
||||
use crate::tile::TileDecoder;
|
||||
|
||||
fn block_width(bs: usize) -> usize {
|
||||
4 * NUM_4X4_BLOCKS_WIDE[bs] as usize
|
||||
}
|
||||
fn block_height(bs: usize) -> usize {
|
||||
4 * NUM_4X4_BLOCKS_HIGH[bs] as usize
|
||||
}
|
||||
|
||||
/// Neighbour reference frame summary (LeftRefFrame / AboveRefFrame and the derived flags).
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub(crate) struct Neighbours {
|
||||
pub left: [i8; 2],
|
||||
pub above: [i8; 2],
|
||||
pub left_intra: bool,
|
||||
pub above_intra: bool,
|
||||
pub left_single: bool,
|
||||
pub above_single: bool,
|
||||
}
|
||||
|
||||
impl TileDecoder<'_, '_> {
|
||||
pub(crate) fn inter_frame_mode_info_impl(&mut self) -> Result<()> {
|
||||
self.b.use_intrabc = false;
|
||||
let (r, c) = (self.b.mi_row, self.b.mi_col);
|
||||
let mi = &self.t.mi;
|
||||
let mut n = Neighbours {
|
||||
left: if self.b.avail_l { mi.ref_frame[mi.idx(r, c - 1)] } else { [INTRA_FRAME as i8, -1] },
|
||||
above: if self.b.avail_u { mi.ref_frame[mi.idx(r - 1, c)] } else { [INTRA_FRAME as i8, -1] },
|
||||
..Default::default()
|
||||
};
|
||||
if !self.b.avail_l {
|
||||
n.left[1] = -1;
|
||||
}
|
||||
if !self.b.avail_u {
|
||||
n.above[1] = -1;
|
||||
}
|
||||
n.left_intra = n.left[0] <= INTRA_FRAME as i8;
|
||||
n.above_intra = n.above[0] <= INTRA_FRAME as i8;
|
||||
n.left_single = n.left[1] <= INTRA_FRAME as i8;
|
||||
n.above_single = n.above[1] <= INTRA_FRAME as i8;
|
||||
self.nb = n;
|
||||
self.b.skip = false;
|
||||
self.inter_segment_id(true);
|
||||
self.read_skip_mode();
|
||||
if self.b.skip_mode {
|
||||
self.b.skip = true;
|
||||
} else {
|
||||
self.read_skip();
|
||||
}
|
||||
if !self.fs.fh.seg.seg_id_pre_skip {
|
||||
self.inter_segment_id(false);
|
||||
}
|
||||
self.b.lossless = self.fs.fh.lossless_array[self.b.segment_id];
|
||||
self.read_cdef();
|
||||
self.read_delta_qindex();
|
||||
self.read_delta_lf();
|
||||
self.read_deltas = false;
|
||||
self.read_is_inter();
|
||||
if self.b.is_inter { self.inter_block_mode_info() } else { self.intra_block_mode_info() }
|
||||
}
|
||||
|
||||
fn get_segment_id(&self) -> usize {
|
||||
let (mi_rows, mi_cols) = (self.fs.fh.mi_rows as usize, self.fs.fh.mi_cols as usize);
|
||||
let x_mis = (mi_cols - self.b.mi_col).min(self.b.bw4);
|
||||
let y_mis = (mi_rows - self.b.mi_row).min(self.b.bh4);
|
||||
let mut seg = 7u8;
|
||||
for y in 0..y_mis {
|
||||
for x in 0..x_mis {
|
||||
let i = (self.b.mi_row + y) * self.t.mi.cols + self.b.mi_col + x;
|
||||
seg = seg.min(self.fs.prev_segment_ids[i]);
|
||||
}
|
||||
}
|
||||
seg as usize
|
||||
}
|
||||
|
||||
fn set_seg_pred_context(&mut self, v: u8) {
|
||||
for i in 0..self.b.bw4 {
|
||||
if self.b.mi_col + i < self.t.above_seg_pred.len() {
|
||||
self.t.above_seg_pred[self.b.mi_col + i] = v;
|
||||
}
|
||||
}
|
||||
for i in 0..self.b.bh4 {
|
||||
if self.b.mi_row + i < self.t.left_seg_pred.len() {
|
||||
self.t.left_seg_pred[self.b.mi_row + i] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn inter_segment_id(&mut self, pre_skip: bool) {
|
||||
let seg = &self.fs.fh.seg;
|
||||
if !seg.enabled {
|
||||
self.b.segment_id = 0;
|
||||
return;
|
||||
}
|
||||
let predicted = self.get_segment_id();
|
||||
if seg.update_map {
|
||||
if pre_skip && !seg.seg_id_pre_skip {
|
||||
self.b.segment_id = 0;
|
||||
return;
|
||||
}
|
||||
if !pre_skip && self.b.skip {
|
||||
self.set_seg_pred_context(0);
|
||||
self.read_segment_id();
|
||||
return;
|
||||
}
|
||||
if seg.temporal_update {
|
||||
let ctx = self.t.left_seg_pred[self.b.mi_row] as usize + self.t.above_seg_pred[self.b.mi_col] as usize;
|
||||
let pred_flag = self.sd.read_symbol(&mut self.cdf.segment_id_predicted[ctx]);
|
||||
if pred_flag == 1 {
|
||||
self.b.segment_id = predicted;
|
||||
} else {
|
||||
self.read_segment_id();
|
||||
}
|
||||
self.set_seg_pred_context(pred_flag as u8);
|
||||
} else {
|
||||
self.read_segment_id();
|
||||
}
|
||||
} else {
|
||||
self.b.segment_id = predicted;
|
||||
}
|
||||
}
|
||||
|
||||
fn read_skip_mode(&mut self) {
|
||||
let ms = self.b.mi_size;
|
||||
if self.seg_active(SEG_LVL_SKIP)
|
||||
|| self.seg_active(SEG_LVL_REF_FRAME)
|
||||
|| self.seg_active(SEG_LVL_GLOBALMV)
|
||||
|| !self.fs.fh.skip_mode_present
|
||||
|| block_width(ms) < 8
|
||||
|| block_height(ms) < 8
|
||||
{
|
||||
self.b.skip_mode = false;
|
||||
} else {
|
||||
let (r, c) = (self.b.mi_row, self.b.mi_col);
|
||||
let mi = &self.t.mi;
|
||||
let mut ctx = 0;
|
||||
if self.b.avail_u {
|
||||
ctx += mi.skip_mode[mi.idx(r - 1, c)] as usize;
|
||||
}
|
||||
if self.b.avail_l {
|
||||
ctx += mi.skip_mode[mi.idx(r, c - 1)] as usize;
|
||||
}
|
||||
self.b.skip_mode = self.sd.read_symbol(&mut self.cdf.skip_mode[ctx]) == 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn seg_active(&self, feature: usize) -> bool {
|
||||
self.fs.fh.seg.enabled && self.fs.fh.seg.features.enabled[self.b.segment_id][feature]
|
||||
}
|
||||
|
||||
fn read_is_inter(&mut self) {
|
||||
if self.b.skip_mode {
|
||||
self.b.is_inter = true;
|
||||
} else if self.seg_active(SEG_LVL_REF_FRAME) {
|
||||
self.b.is_inter = self.fs.fh.seg.features.data[self.b.segment_id][SEG_LVL_REF_FRAME] != INTRA_FRAME as i32;
|
||||
} else if self.seg_active(SEG_LVL_GLOBALMV) {
|
||||
self.b.is_inter = true;
|
||||
} else {
|
||||
let n = self.nb;
|
||||
let ctx = if self.b.avail_u && self.b.avail_l {
|
||||
if n.left_intra && n.above_intra { 3 } else { (n.left_intra || n.above_intra) as usize }
|
||||
} else if self.b.avail_u || self.b.avail_l {
|
||||
2 * (if self.b.avail_u { n.above_intra } else { n.left_intra }) as usize
|
||||
} else {
|
||||
0
|
||||
};
|
||||
self.b.is_inter = self.sd.read_symbol(&mut self.cdf.is_inter[ctx]) == 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn intra_block_mode_info(&mut self) -> Result<()> {
|
||||
self.b.ref_frame = [INTRA_FRAME as i8, -1];
|
||||
let ctx = SIZE_GROUP[self.b.mi_size] as usize;
|
||||
self.b.y_mode = self.sd.read_symbol(&mut self.cdf.y_mode[ctx]);
|
||||
self.intra_angle_info_y();
|
||||
if self.b.has_chroma {
|
||||
self.read_uv_mode();
|
||||
if self.b.uv_mode == UV_CFL_PRED {
|
||||
self.read_cfl_alphas();
|
||||
}
|
||||
self.intra_angle_info_uv();
|
||||
}
|
||||
self.b.palette_size_y = 0;
|
||||
self.b.palette_size_uv = 0;
|
||||
let ms = self.b.mi_size;
|
||||
if ms >= BLOCK_8X8 && block_width(ms) <= 64 && block_height(ms) <= 64 && self.fs.fh.allow_screen_content_tools {
|
||||
self.palette_mode_info();
|
||||
}
|
||||
self.filter_intra_mode_info();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn count_refs(&self, frame_type: i8) -> usize {
|
||||
let n = self.nb;
|
||||
let mut c = 0;
|
||||
if self.b.avail_u {
|
||||
c += (n.above[0] == frame_type) as usize + (n.above[1] == frame_type) as usize;
|
||||
}
|
||||
if self.b.avail_l {
|
||||
c += (n.left[0] == frame_type) as usize + (n.left[1] == frame_type) as usize;
|
||||
}
|
||||
c
|
||||
}
|
||||
|
||||
fn ref_count_ctx(c0: usize, c1: usize) -> usize {
|
||||
match c0.cmp(&c1) {
|
||||
std::cmp::Ordering::Less => 0,
|
||||
std::cmp::Ordering::Equal => 1,
|
||||
std::cmp::Ordering::Greater => 2,
|
||||
}
|
||||
}
|
||||
|
||||
fn ctx_comp_ref(&self) -> usize {
|
||||
Self::ref_count_ctx(
|
||||
self.count_refs(LAST_FRAME as i8) + self.count_refs(LAST2_FRAME as i8),
|
||||
self.count_refs(LAST3_FRAME as i8) + self.count_refs(GOLDEN_FRAME as i8),
|
||||
)
|
||||
}
|
||||
fn ctx_comp_ref_p1(&self) -> usize {
|
||||
Self::ref_count_ctx(self.count_refs(LAST_FRAME as i8), self.count_refs(LAST2_FRAME as i8))
|
||||
}
|
||||
fn ctx_comp_ref_p2(&self) -> usize {
|
||||
Self::ref_count_ctx(self.count_refs(LAST3_FRAME as i8), self.count_refs(GOLDEN_FRAME as i8))
|
||||
}
|
||||
fn ctx_comp_bwdref(&self) -> usize {
|
||||
Self::ref_count_ctx(self.count_refs(BWDREF_FRAME as i8) + self.count_refs(ALTREF2_FRAME as i8), self.count_refs(ALTREF_FRAME as i8))
|
||||
}
|
||||
fn ctx_comp_bwdref_p1(&self) -> usize {
|
||||
Self::ref_count_ctx(self.count_refs(BWDREF_FRAME as i8), self.count_refs(ALTREF2_FRAME as i8))
|
||||
}
|
||||
fn ctx_single_ref_p1(&self) -> usize {
|
||||
let fwd = self.count_refs(LAST_FRAME as i8)
|
||||
+ self.count_refs(LAST2_FRAME as i8)
|
||||
+ self.count_refs(LAST3_FRAME as i8)
|
||||
+ self.count_refs(GOLDEN_FRAME as i8);
|
||||
let bwd = self.count_refs(BWDREF_FRAME as i8) + self.count_refs(ALTREF2_FRAME as i8) + self.count_refs(ALTREF_FRAME as i8);
|
||||
Self::ref_count_ctx(fwd, bwd)
|
||||
}
|
||||
|
||||
fn read_ref_frames(&mut self) {
|
||||
if self.b.skip_mode {
|
||||
let f = self.fs.fh.skip_mode_frame;
|
||||
self.b.ref_frame = [f[0] as i8, f[1] as i8];
|
||||
} else if self.seg_active(SEG_LVL_REF_FRAME) {
|
||||
self.b.ref_frame = [self.fs.fh.seg.features.data[self.b.segment_id][SEG_LVL_REF_FRAME] as i8, -1];
|
||||
} else if self.seg_active(SEG_LVL_SKIP) || self.seg_active(SEG_LVL_GLOBALMV) {
|
||||
self.b.ref_frame = [LAST_FRAME as i8, -1];
|
||||
} else {
|
||||
let comp_mode = if self.fs.fh.reference_select && self.b.bw4.min(self.b.bh4) >= 2 {
|
||||
let ctx = self.comp_mode_ctx();
|
||||
self.sd.read_symbol(&mut self.cdf.comp_mode[ctx])
|
||||
} else {
|
||||
SINGLE_REFERENCE
|
||||
};
|
||||
if comp_mode == COMPOUND_REFERENCE {
|
||||
let ctx = self.comp_ref_type_ctx();
|
||||
let comp_ref_type = self.sd.read_symbol(&mut self.cdf.comp_ref_type[ctx]);
|
||||
if comp_ref_type == UNIDIR_COMP_REFERENCE {
|
||||
let ctx = self.ctx_single_ref_p1();
|
||||
if self.sd.read_symbol(&mut self.cdf.uni_comp_ref[ctx][0]) == 1 {
|
||||
self.b.ref_frame = [BWDREF_FRAME as i8, ALTREF_FRAME as i8];
|
||||
} else {
|
||||
let ctx1 = Self::ref_count_ctx(
|
||||
self.count_refs(LAST2_FRAME as i8),
|
||||
self.count_refs(LAST3_FRAME as i8) + self.count_refs(GOLDEN_FRAME as i8),
|
||||
);
|
||||
if self.sd.read_symbol(&mut self.cdf.uni_comp_ref[ctx1][1]) == 1 {
|
||||
let ctx2 = self.ctx_comp_ref_p2();
|
||||
if self.sd.read_symbol(&mut self.cdf.uni_comp_ref[ctx2][2]) == 1 {
|
||||
self.b.ref_frame = [LAST_FRAME as i8, GOLDEN_FRAME as i8];
|
||||
} else {
|
||||
self.b.ref_frame = [LAST_FRAME as i8, LAST3_FRAME as i8];
|
||||
}
|
||||
} else {
|
||||
self.b.ref_frame = [LAST_FRAME as i8, LAST2_FRAME as i8];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let ctx = self.ctx_comp_ref();
|
||||
if self.sd.read_symbol(&mut self.cdf.comp_ref[ctx][0]) == 0 {
|
||||
let ctx = self.ctx_comp_ref_p1();
|
||||
let p1 = self.sd.read_symbol(&mut self.cdf.comp_ref[ctx][1]);
|
||||
self.b.ref_frame[0] = if p1 == 1 { LAST2_FRAME } else { LAST_FRAME } as i8;
|
||||
} else {
|
||||
let ctx = self.ctx_comp_ref_p2();
|
||||
let p2 = self.sd.read_symbol(&mut self.cdf.comp_ref[ctx][2]);
|
||||
self.b.ref_frame[0] = if p2 == 1 { GOLDEN_FRAME } else { LAST3_FRAME } as i8;
|
||||
}
|
||||
let ctx = self.ctx_comp_bwdref();
|
||||
if self.sd.read_symbol(&mut self.cdf.comp_bwd_ref[ctx][0]) == 0 {
|
||||
let ctx = self.ctx_comp_bwdref_p1();
|
||||
let p1 = self.sd.read_symbol(&mut self.cdf.comp_bwd_ref[ctx][1]);
|
||||
self.b.ref_frame[1] = if p1 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME } as i8;
|
||||
} else {
|
||||
self.b.ref_frame[1] = ALTREF_FRAME as i8;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let ctx = self.ctx_single_ref_p1();
|
||||
if self.sd.read_symbol(&mut self.cdf.single_ref[ctx][0]) == 1 {
|
||||
let ctx = self.ctx_comp_bwdref();
|
||||
if self.sd.read_symbol(&mut self.cdf.single_ref[ctx][1]) == 0 {
|
||||
let ctx = self.ctx_comp_bwdref_p1();
|
||||
let p6 = self.sd.read_symbol(&mut self.cdf.single_ref[ctx][5]);
|
||||
self.b.ref_frame[0] = if p6 == 1 { ALTREF2_FRAME } else { BWDREF_FRAME } as i8;
|
||||
} else {
|
||||
self.b.ref_frame[0] = ALTREF_FRAME as i8;
|
||||
}
|
||||
} else {
|
||||
let ctx = self.ctx_comp_ref();
|
||||
if self.sd.read_symbol(&mut self.cdf.single_ref[ctx][2]) == 1 {
|
||||
let ctx = self.ctx_comp_ref_p2();
|
||||
let p5 = self.sd.read_symbol(&mut self.cdf.single_ref[ctx][4]);
|
||||
self.b.ref_frame[0] = if p5 == 1 { GOLDEN_FRAME } else { LAST3_FRAME } as i8;
|
||||
} else {
|
||||
let ctx = self.ctx_comp_ref_p1();
|
||||
let p4 = self.sd.read_symbol(&mut self.cdf.single_ref[ctx][3]);
|
||||
self.b.ref_frame[0] = if p4 == 1 { LAST2_FRAME } else { LAST_FRAME } as i8;
|
||||
}
|
||||
}
|
||||
self.b.ref_frame[1] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_backward(r: i8) -> bool {
|
||||
r >= BWDREF_FRAME as i8 && r <= ALTREF_FRAME as i8
|
||||
}
|
||||
|
||||
fn comp_mode_ctx(&self) -> usize {
|
||||
let n = self.nb;
|
||||
let (au, al) = (self.b.avail_u, self.b.avail_l);
|
||||
if au && al {
|
||||
if n.above_single && n.left_single {
|
||||
(Self::check_backward(n.above[0]) ^ Self::check_backward(n.left[0])) as usize
|
||||
} else if n.above_single {
|
||||
2 + (Self::check_backward(n.above[0]) || n.above_intra) as usize
|
||||
} else if n.left_single {
|
||||
2 + (Self::check_backward(n.left[0]) || n.left_intra) as usize
|
||||
} else {
|
||||
4
|
||||
}
|
||||
} else if au {
|
||||
if n.above_single { Self::check_backward(n.above[0]) as usize } else { 3 }
|
||||
} else if al {
|
||||
if n.left_single { Self::check_backward(n.left[0]) as usize } else { 3 }
|
||||
} else {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
fn comp_ref_type_ctx(&self) -> usize {
|
||||
let n = self.nb;
|
||||
let (au, al) = (self.b.avail_u, self.b.avail_l);
|
||||
let samedir = |a: i8, b: i8| (a >= BWDREF_FRAME as i8) == (b >= BWDREF_FRAME as i8);
|
||||
let (above0, above1, left0, left1) = (n.above[0], n.above[1], n.left[0], n.left[1]);
|
||||
let above_comp_inter = au && !n.above_intra && !n.above_single;
|
||||
let left_comp_inter = al && !n.left_intra && !n.left_single;
|
||||
let above_uni = above_comp_inter && samedir(above0, above1);
|
||||
let left_uni = left_comp_inter && samedir(left0, left1);
|
||||
if au && !n.above_intra && al && !n.left_intra {
|
||||
let sd = samedir(above0, left0) as usize;
|
||||
if !above_comp_inter && !left_comp_inter {
|
||||
1 + 2 * sd
|
||||
} else if !above_comp_inter {
|
||||
if !left_uni { 1 } else { 3 + sd }
|
||||
} else if !left_comp_inter {
|
||||
if !above_uni { 1 } else { 3 + sd }
|
||||
} else if !above_uni && !left_uni {
|
||||
0
|
||||
} else if !above_uni || !left_uni {
|
||||
2
|
||||
} else {
|
||||
3 + ((above0 == BWDREF_FRAME as i8) == (left0 == BWDREF_FRAME as i8)) as usize
|
||||
}
|
||||
} else if au && al {
|
||||
if above_comp_inter {
|
||||
1 + 2 * above_uni as usize
|
||||
} else if left_comp_inter {
|
||||
1 + 2 * left_uni as usize
|
||||
} else {
|
||||
2
|
||||
}
|
||||
} else if above_comp_inter {
|
||||
4 * above_uni as usize
|
||||
} else if left_comp_inter {
|
||||
4 * left_uni as usize
|
||||
} else {
|
||||
2
|
||||
}
|
||||
}
|
||||
|
||||
fn inter_block_mode_info(&mut self) -> Result<()> {
|
||||
self.b.palette_size_y = 0;
|
||||
self.b.palette_size_uv = 0;
|
||||
self.read_ref_frames();
|
||||
let is_compound = self.b.ref_frame[1] > INTRA_FRAME as i8;
|
||||
self.find_mv_stack(is_compound);
|
||||
if self.b.skip_mode {
|
||||
self.b.y_mode = NEAREST_NEARESTMV;
|
||||
} else if self.seg_active(SEG_LVL_SKIP) || self.seg_active(SEG_LVL_GLOBALMV) {
|
||||
self.b.y_mode = GLOBALMV;
|
||||
} else if is_compound {
|
||||
let ctx = COMPOUND_MODE_CTX_MAP[self.mvs.ref_mv_context >> 1][self.mvs.new_mv_context.min(COMP_NEWMV_CTXS - 1)] as usize;
|
||||
self.b.y_mode = NEAREST_NEARESTMV + self.sd.read_symbol(&mut self.cdf.compound_mode[ctx]);
|
||||
} else {
|
||||
let new_mv = self.sd.read_symbol(&mut self.cdf.new_mv[self.mvs.new_mv_context]);
|
||||
if new_mv == 0 {
|
||||
self.b.y_mode = NEWMV;
|
||||
} else {
|
||||
let zero_mv = self.sd.read_symbol(&mut self.cdf.zero_mv[self.mvs.zero_mv_context]);
|
||||
if zero_mv == 0 {
|
||||
self.b.y_mode = GLOBALMV;
|
||||
} else {
|
||||
let ref_mv = self.sd.read_symbol(&mut self.cdf.ref_mv[self.mvs.ref_mv_context]);
|
||||
self.b.y_mode = if ref_mv == 0 { NEARESTMV } else { NEARMV };
|
||||
}
|
||||
}
|
||||
}
|
||||
self.b.ref_mv_idx = 0;
|
||||
let ym = self.b.y_mode;
|
||||
if ym == NEWMV || ym == NEW_NEWMV {
|
||||
for idx in 0..2 {
|
||||
if self.mvs.num_mv_found > idx + 1 {
|
||||
let ctx = self.mvs.drl_ctx_stack[idx];
|
||||
let drl = self.sd.read_symbol(&mut self.cdf.drl_mode[ctx]);
|
||||
if drl == 0 {
|
||||
self.b.ref_mv_idx = idx;
|
||||
break;
|
||||
}
|
||||
self.b.ref_mv_idx = idx + 1;
|
||||
}
|
||||
}
|
||||
} else if matches!(ym, NEARMV | NEAR_NEARMV | NEAR_NEWMV | NEW_NEARMV) {
|
||||
self.b.ref_mv_idx = 1;
|
||||
for idx in 1..3 {
|
||||
if self.mvs.num_mv_found > idx + 1 {
|
||||
let ctx = self.mvs.drl_ctx_stack[idx];
|
||||
let drl = self.sd.read_symbol(&mut self.cdf.drl_mode[ctx]);
|
||||
if drl == 0 {
|
||||
self.b.ref_mv_idx = idx;
|
||||
break;
|
||||
}
|
||||
self.b.ref_mv_idx = idx + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.assign_mv(is_compound)?;
|
||||
self.read_interintra_mode(is_compound);
|
||||
self.read_motion_mode(is_compound);
|
||||
self.read_compound_type(is_compound);
|
||||
if self.fs.fh.interpolation_filter == SWITCHABLE {
|
||||
let dual = self.fs.seq.enable_dual_filter;
|
||||
for dir in 0..if dual { 2 } else { 1 } {
|
||||
if self.needs_interp_filter() {
|
||||
let ctx = self.interp_filter_ctx(dir);
|
||||
self.b.interp_filter[dir] = self.sd.read_symbol(&mut self.cdf.interp_filter[ctx]) as u8;
|
||||
} else {
|
||||
self.b.interp_filter[dir] = EIGHTTAP;
|
||||
}
|
||||
}
|
||||
if !dual {
|
||||
self.b.interp_filter[1] = self.b.interp_filter[0];
|
||||
}
|
||||
} else {
|
||||
self.b.interp_filter = [self.fs.fh.interpolation_filter; 2];
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn needs_interp_filter(&self) -> bool {
|
||||
let ms = self.b.mi_size;
|
||||
let large = block_width(ms).min(block_height(ms)) >= 8;
|
||||
let gm = &self.fs.fh.gm_type;
|
||||
if self.b.skip_mode || self.b.motion_mode as usize == LOCALWARP {
|
||||
false
|
||||
} else if large && self.b.y_mode == GLOBALMV {
|
||||
gm[self.b.ref_frame[0] as usize] as usize == TRANSLATION
|
||||
} else if large && self.b.y_mode == GLOBAL_GLOBALMV {
|
||||
gm[self.b.ref_frame[0] as usize] as usize == TRANSLATION || gm[self.b.ref_frame[1] as usize] as usize == TRANSLATION
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn interp_filter_ctx(&self, dir: usize) -> usize {
|
||||
let mut ctx = ((dir & 1) * 2 + (self.b.ref_frame[1] > INTRA_FRAME as i8) as usize) * 4;
|
||||
let mut left_type = 3u8;
|
||||
let mut above_type = 3u8;
|
||||
let (r, c) = (self.b.mi_row, self.b.mi_col);
|
||||
let mi = &self.t.mi;
|
||||
let rf0 = self.b.ref_frame[0];
|
||||
if self.b.avail_l {
|
||||
let i = mi.idx(r, c - 1);
|
||||
if mi.ref_frame[i][0] == rf0 || mi.ref_frame[i][1] == rf0 {
|
||||
left_type = mi.interp_filter[i][dir];
|
||||
}
|
||||
}
|
||||
if self.b.avail_u {
|
||||
let i = mi.idx(r - 1, c);
|
||||
if mi.ref_frame[i][0] == rf0 || mi.ref_frame[i][1] == rf0 {
|
||||
above_type = mi.interp_filter[i][dir];
|
||||
}
|
||||
}
|
||||
ctx += if left_type == above_type {
|
||||
left_type as usize
|
||||
} else if left_type == 3 {
|
||||
above_type as usize
|
||||
} else if above_type == 3 {
|
||||
left_type as usize
|
||||
} else {
|
||||
3
|
||||
};
|
||||
ctx
|
||||
}
|
||||
|
||||
fn get_mode(&self, ref_list: usize) -> usize {
|
||||
let ym = self.b.y_mode;
|
||||
if ref_list == 0 {
|
||||
if ym < NEAREST_NEARESTMV {
|
||||
ym
|
||||
} else if matches!(ym, NEW_NEWMV | NEW_NEARESTMV | NEW_NEARMV) {
|
||||
NEWMV
|
||||
} else if matches!(ym, NEAREST_NEARESTMV | NEAREST_NEWMV) {
|
||||
NEARESTMV
|
||||
} else if matches!(ym, NEAR_NEARMV | NEAR_NEWMV) {
|
||||
NEARMV
|
||||
} else {
|
||||
GLOBALMV
|
||||
}
|
||||
} else if matches!(ym, NEW_NEWMV | NEAREST_NEWMV | NEAR_NEWMV) {
|
||||
NEWMV
|
||||
} else if matches!(ym, NEAREST_NEARESTMV | NEW_NEARESTMV) {
|
||||
NEARESTMV
|
||||
} else if matches!(ym, NEAR_NEARMV | NEW_NEARMV) {
|
||||
NEARMV
|
||||
} else {
|
||||
GLOBALMV
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn assign_mv(&mut self, is_compound: bool) -> Result<()> {
|
||||
for i in 0..1 + is_compound as usize {
|
||||
let comp_mode = if self.b.use_intrabc { NEWMV } else { self.get_mode(i) };
|
||||
let pred: MvP = if self.b.use_intrabc {
|
||||
let mut p = self.mvs.ref_stack_mv[0][0];
|
||||
if p == [0, 0] {
|
||||
p = self.mvs.ref_stack_mv[1][0];
|
||||
}
|
||||
if p == [0, 0] {
|
||||
let sb_size = if self.fs.seq.use_128x128_superblock { BLOCK_128X128 } else { BLOCK_64X64 };
|
||||
let sb4 = NUM_4X4_BLOCKS_HIGH[sb_size] as i32;
|
||||
if (self.b.mi_row as i32) - sb4 < self.mi_row_start as i32 {
|
||||
p = [0, -(sb4 * 4 + INTRABC_DELAY_PIXELS as i32) * 8];
|
||||
} else {
|
||||
p = [-(sb4 * 4 * 8), 0];
|
||||
}
|
||||
}
|
||||
p
|
||||
} else if comp_mode == GLOBALMV {
|
||||
self.mvs.global_mvs[i]
|
||||
} else {
|
||||
let mut pos = if comp_mode == NEARESTMV { 0 } else { self.b.ref_mv_idx };
|
||||
if comp_mode == NEWMV && self.mvs.num_mv_found <= 1 {
|
||||
pos = 0;
|
||||
}
|
||||
self.mvs.ref_stack_mv[pos][i]
|
||||
};
|
||||
self.b.pred_mv[i] = pred;
|
||||
if comp_mode == NEWMV {
|
||||
self.read_mv(i);
|
||||
} else {
|
||||
self.b.mv_i[i] = pred;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_mv(&mut self, r: usize) {
|
||||
let mut diff = [0i32; 2];
|
||||
let ctx = if self.b.use_intrabc { MV_INTRABC_CONTEXT } else { 0 };
|
||||
let joint = self.sd.read_symbol(&mut self.cdf.mv_joint[ctx]);
|
||||
if joint == MV_JOINT_HZVNZ || joint == MV_JOINT_HNZVNZ {
|
||||
diff[0] = self.read_mv_component(ctx, 0);
|
||||
}
|
||||
if joint == MV_JOINT_HNZVZ || joint == MV_JOINT_HNZVNZ {
|
||||
diff[1] = self.read_mv_component(ctx, 1);
|
||||
}
|
||||
self.b.mv_i[r] = [self.b.pred_mv[r][0] + diff[0], self.b.pred_mv[r][1] + diff[1]];
|
||||
}
|
||||
|
||||
fn read_mv_component(&mut self, ctx: usize, comp: usize) -> i32 {
|
||||
let fh = &self.fs.fh;
|
||||
let (fim, hp) = (fh.force_integer_mv, fh.allow_high_precision_mv);
|
||||
let sign = self.sd.read_symbol(&mut self.cdf.mv_sign[ctx][comp]);
|
||||
let class = self.sd.read_symbol(&mut self.cdf.mv_class[ctx][comp]);
|
||||
let mag = if class == MV_CLASS_0 {
|
||||
let b0 = self.sd.read_symbol(&mut self.cdf.mv_class0_bit[ctx][comp]) as i32;
|
||||
let fr = if fim { 3 } else { self.sd.read_symbol(&mut self.cdf.mv_class0_fr[ctx][comp][b0 as usize]) as i32 };
|
||||
let h = if hp { self.sd.read_symbol(&mut self.cdf.mv_class0_hp[ctx][comp]) as i32 } else { 1 };
|
||||
((b0 << 3) | (fr << 1) | h) + 1
|
||||
} else {
|
||||
let mut d = 0i32;
|
||||
for i in 0..class {
|
||||
d |= (self.sd.read_symbol(&mut self.cdf.mv_bit[ctx][comp][i]) as i32) << i;
|
||||
}
|
||||
let mut mag = (CLASS0_SIZE as i32) << (class + 2);
|
||||
let fr = if fim { 3 } else { self.sd.read_symbol(&mut self.cdf.mv_fr[ctx][comp]) as i32 };
|
||||
let h = if hp { self.sd.read_symbol(&mut self.cdf.mv_hp[ctx][comp]) as i32 } else { 1 };
|
||||
mag += ((d << 3) | (fr << 1) | h) + 1;
|
||||
mag
|
||||
};
|
||||
if sign == 1 { -mag } else { mag }
|
||||
}
|
||||
|
||||
fn read_interintra_mode(&mut self, is_compound: bool) {
|
||||
let ms = self.b.mi_size;
|
||||
self.b.interintra = false;
|
||||
if !self.b.skip_mode && self.fs.seq.enable_interintra_compound && !is_compound && (BLOCK_8X8..=BLOCK_32X32).contains(&ms) {
|
||||
let ctx = SIZE_GROUP[ms] as usize - 1;
|
||||
self.b.interintra = self.sd.read_symbol(&mut self.cdf.inter_intra[ctx]) == 1;
|
||||
if self.b.interintra {
|
||||
self.b.interintra_mode = self.sd.read_symbol(&mut self.cdf.inter_intra_mode[ctx]);
|
||||
self.b.ref_frame[1] = INTRA_FRAME as i8;
|
||||
self.b.angle_delta_y = 0;
|
||||
self.b.angle_delta_uv = 0;
|
||||
self.b.use_filter_intra = false;
|
||||
self.b.wedge_interintra = self.sd.read_symbol(&mut self.cdf.wedge_inter_intra[ms]) == 1;
|
||||
if self.b.wedge_interintra {
|
||||
self.b.wedge_index = self.sd.read_symbol(&mut self.cdf.wedge_index[ms]);
|
||||
self.b.wedge_sign = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_scaled(&self, rf: i8) -> bool {
|
||||
let fh = &self.fs.fh;
|
||||
let ri = &self.fs.ref_info[fh.ref_frame_idx[rf as usize - LAST_FRAME]];
|
||||
let fw = fh.frame_width;
|
||||
let fhh = fh.frame_height;
|
||||
let xs = ((ri.upscaled_width << REF_SCALE_SHIFT) + fw / 2) / fw;
|
||||
let ys = ((ri.frame_height << REF_SCALE_SHIFT) + fhh / 2) / fhh;
|
||||
let no = 1 << REF_SCALE_SHIFT;
|
||||
xs != no || ys != no
|
||||
}
|
||||
|
||||
fn read_motion_mode(&mut self, is_compound: bool) {
|
||||
self.b.motion_mode = SIMPLE as u8;
|
||||
let fh = &self.fs.fh;
|
||||
if self.b.skip_mode || !fh.is_motion_mode_switchable {
|
||||
return;
|
||||
}
|
||||
let ms = self.b.mi_size;
|
||||
if block_width(ms).min(block_height(ms)) < 8 {
|
||||
return;
|
||||
}
|
||||
if !fh.force_integer_mv
|
||||
&& (self.b.y_mode == GLOBALMV || self.b.y_mode == GLOBAL_GLOBALMV)
|
||||
&& fh.gm_type[self.b.ref_frame[0] as usize] as usize > TRANSLATION
|
||||
{
|
||||
return;
|
||||
}
|
||||
if is_compound || self.b.ref_frame[1] == INTRA_FRAME as i8 || !self.has_overlappable_candidates() {
|
||||
return;
|
||||
}
|
||||
self.find_warp_samples();
|
||||
let fh = &self.fs.fh;
|
||||
if fh.force_integer_mv || self.mvs.num_samples == 0 || !fh.allow_warped_motion || self.is_scaled(self.b.ref_frame[0]) {
|
||||
let use_obmc = self.sd.read_symbol(&mut self.cdf.use_obmc[ms]);
|
||||
self.b.motion_mode = if use_obmc == 1 { OBMC as u8 } else { SIMPLE as u8 };
|
||||
} else {
|
||||
self.b.motion_mode = self.sd.read_symbol(&mut self.cdf.motion_mode[ms]) as u8;
|
||||
}
|
||||
}
|
||||
|
||||
fn read_compound_type(&mut self, is_compound: bool) {
|
||||
self.b.comp_group_idx = 0;
|
||||
self.b.compound_idx = 1;
|
||||
if self.b.skip_mode {
|
||||
self.b.compound_type = COMPOUND_AVERAGE;
|
||||
return;
|
||||
}
|
||||
if is_compound {
|
||||
let ms = self.b.mi_size;
|
||||
let n = WEDGE_BITS[ms];
|
||||
if self.fs.seq.enable_masked_compound {
|
||||
let ctx = self.comp_group_idx_ctx();
|
||||
self.b.comp_group_idx = self.sd.read_symbol(&mut self.cdf.comp_group_idx[ctx]) as u8;
|
||||
}
|
||||
if self.b.comp_group_idx == 0 {
|
||||
if self.fs.seq.enable_jnt_comp {
|
||||
let ctx = self.compound_idx_ctx();
|
||||
self.b.compound_idx = self.sd.read_symbol(&mut self.cdf.compound_idx[ctx]) as u8;
|
||||
self.b.compound_type = if self.b.compound_idx == 1 { COMPOUND_AVERAGE } else { COMPOUND_DISTANCE };
|
||||
} else {
|
||||
self.b.compound_type = COMPOUND_AVERAGE;
|
||||
}
|
||||
} else if n == 0 {
|
||||
self.b.compound_type = COMPOUND_DIFFWTD;
|
||||
} else {
|
||||
self.b.compound_type = self.sd.read_symbol(&mut self.cdf.compound_type[ms]);
|
||||
}
|
||||
if self.b.compound_type == COMPOUND_WEDGE {
|
||||
self.b.wedge_index = self.sd.read_symbol(&mut self.cdf.wedge_index[ms]);
|
||||
self.b.wedge_sign = self.sd.read_literal(1) as usize;
|
||||
} else if self.b.compound_type == COMPOUND_DIFFWTD {
|
||||
self.b.mask_type = self.sd.read_literal(1) as usize;
|
||||
}
|
||||
} else if self.b.interintra {
|
||||
self.b.compound_type = if self.b.wedge_interintra { COMPOUND_WEDGE } else { COMPOUND_INTRA };
|
||||
} else {
|
||||
self.b.compound_type = COMPOUND_AVERAGE;
|
||||
}
|
||||
}
|
||||
|
||||
fn comp_group_idx_ctx(&self) -> usize {
|
||||
let n = self.nb;
|
||||
let mi = &self.t.mi;
|
||||
let (r, c) = (self.b.mi_row, self.b.mi_col);
|
||||
let mut ctx = 0;
|
||||
if self.b.avail_u {
|
||||
if !n.above_single {
|
||||
ctx += mi.comp_group_idx[mi.idx(r - 1, c)] as usize;
|
||||
} else if n.above[0] == ALTREF_FRAME as i8 {
|
||||
ctx += 3;
|
||||
}
|
||||
}
|
||||
if self.b.avail_l {
|
||||
if !n.left_single {
|
||||
ctx += mi.comp_group_idx[mi.idx(r, c - 1)] as usize;
|
||||
} else if n.left[0] == ALTREF_FRAME as i8 {
|
||||
ctx += 3;
|
||||
}
|
||||
}
|
||||
ctx.min(5)
|
||||
}
|
||||
|
||||
fn compound_idx_ctx(&self) -> usize {
|
||||
let fh = &self.fs.fh;
|
||||
let seq = &self.fs.seq;
|
||||
let fwd = crate::header::relative_dist(seq, fh.order_hints[self.b.ref_frame[0] as usize], fh.order_hint).abs();
|
||||
let bck = crate::header::relative_dist(seq, fh.order_hints[self.b.ref_frame[1] as usize], fh.order_hint).abs();
|
||||
let mut ctx = if fwd == bck { 3 } else { 0 };
|
||||
let n = self.nb;
|
||||
let mi = &self.t.mi;
|
||||
let (r, c) = (self.b.mi_row, self.b.mi_col);
|
||||
if self.b.avail_u {
|
||||
if !n.above_single {
|
||||
ctx += mi.compound_idx[mi.idx(r - 1, c)] as usize;
|
||||
} else if n.above[0] == ALTREF_FRAME as i8 {
|
||||
ctx += 1;
|
||||
}
|
||||
}
|
||||
if self.b.avail_l {
|
||||
if !n.left_single {
|
||||
ctx += mi.compound_idx[mi.idx(r, c - 1)] as usize;
|
||||
} else if n.left[0] == ALTREF_FRAME as i8 {
|
||||
ctx += 1;
|
||||
}
|
||||
}
|
||||
ctx
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,407 @@
|
||||
//! Intra prediction (spec 7.11.2), palette prediction (7.11.4) and chroma from luma (7.11.5).
|
||||
|
||||
use crate::frame::Plane;
|
||||
use crate::spec_tables::{
|
||||
ANGLE_STEP, D67_PRED, DC_PRED, DR_INTRA_DERIVATIVE, INTRA_EDGE_KERNEL, INTRA_EDGE_TAPS, INTRA_FILTER_SCALE_BITS, INTRA_FILTER_TAPS,
|
||||
MODE_TO_ANGLE, SM_WEIGHTS_TX_4X4, SM_WEIGHTS_TX_8X8, SM_WEIGHTS_TX_16X16, SM_WEIGHTS_TX_32X32, SM_WEIGHTS_TX_64X64, SMOOTH_H_PRED, SMOOTH_PRED,
|
||||
SMOOTH_V_PRED, V_PRED,
|
||||
};
|
||||
use crate::transform::round2;
|
||||
|
||||
/// Inputs of the intra prediction process that come from the block.
|
||||
pub(crate) struct IntraParams {
|
||||
pub plane: usize,
|
||||
pub x: usize,
|
||||
pub y: usize,
|
||||
pub have_left: bool,
|
||||
pub have_above: bool,
|
||||
pub have_above_right: bool,
|
||||
pub have_below_left: bool,
|
||||
pub mode: usize,
|
||||
pub log2w: u32,
|
||||
pub log2h: u32,
|
||||
/// Largest valid x / y in the plane: ((MiCols * MI_SIZE) >> subX) - 1.
|
||||
pub max_x: i32,
|
||||
pub max_y: i32,
|
||||
pub bit_depth: u32,
|
||||
pub angle_delta: i32,
|
||||
pub use_filter_intra: bool,
|
||||
pub filter_intra_mode: usize,
|
||||
pub enable_intra_edge_filter: bool,
|
||||
/// Result of the intra filter type process (7.11.2.8).
|
||||
pub filter_type: bool,
|
||||
}
|
||||
|
||||
const EDGE: usize = 32; // offset of index 0 in the edge buffers (they start at index -2 .. -1)
|
||||
|
||||
#[inline(always)]
|
||||
fn clip1(v: i32, bd: u32) -> u16 {
|
||||
v.clamp(0, (1 << bd) - 1) as u16
|
||||
}
|
||||
|
||||
pub(crate) fn is_directional_mode(mode: usize) -> bool {
|
||||
(V_PRED..=D67_PRED).contains(&mode)
|
||||
}
|
||||
|
||||
/// Predict the w x h block at (x, y) of `pl` (writes into the plane).
|
||||
pub(crate) fn predict_intra(pl: &mut Plane, p: &IntraParams) {
|
||||
let w = 1usize << p.log2w;
|
||||
let h = 1usize << p.log2h;
|
||||
let bd = p.bit_depth;
|
||||
let (x, y) = (p.x, p.y);
|
||||
// edge arrays with room for index -2 .. 2*(w+h)
|
||||
let mut above = [0i32; 2 * 128 + 2 * EDGE];
|
||||
let mut left = [0i32; 2 * 128 + 2 * EDGE];
|
||||
let n = w + h;
|
||||
if !p.have_above && p.have_left {
|
||||
let v = pl.at(x - 1, y) as i32;
|
||||
above[EDGE..EDGE + n].iter_mut().for_each(|a| *a = v);
|
||||
} else if !p.have_above && !p.have_left {
|
||||
let v = (1 << (bd - 1)) - 1;
|
||||
above[EDGE..EDGE + n].iter_mut().for_each(|a| *a = v);
|
||||
} else {
|
||||
let above_limit = (p.max_x).min(x as i32 + if p.have_above_right { 2 * w as i32 } else { w as i32 } - 1);
|
||||
let row = pl.row_from(y - 1, x);
|
||||
for i in 0..n {
|
||||
above[EDGE + i] = row[(above_limit.min(x as i32 + i as i32)) as usize - x] as i32;
|
||||
}
|
||||
}
|
||||
if !p.have_left && p.have_above {
|
||||
let v = pl.at(x, y - 1) as i32;
|
||||
left[EDGE..EDGE + n].iter_mut().for_each(|a| *a = v);
|
||||
} else if !p.have_left && !p.have_above {
|
||||
let v = (1 << (bd - 1)) + 1;
|
||||
left[EDGE..EDGE + n].iter_mut().for_each(|a| *a = v);
|
||||
} else {
|
||||
let left_limit = (p.max_y).min(y as i32 + if p.have_below_left { 2 * h as i32 } else { h as i32 } - 1);
|
||||
for i in 0..n {
|
||||
left[EDGE + i] = pl.at(x - 1, left_limit.min(y as i32 + i as i32) as usize) as i32;
|
||||
}
|
||||
}
|
||||
let corner = if p.have_above && p.have_left {
|
||||
pl.at(x - 1, y - 1) as i32
|
||||
} else if p.have_above {
|
||||
pl.at(x, y - 1) as i32
|
||||
} else if p.have_left {
|
||||
pl.at(x - 1, y) as i32
|
||||
} else {
|
||||
1 << (bd - 1)
|
||||
};
|
||||
above[EDGE - 1] = corner;
|
||||
left[EDGE - 1] = corner;
|
||||
|
||||
let mut pred = [0u16; 64 * 64];
|
||||
if p.plane == 0 && p.use_filter_intra {
|
||||
recursive_intra(&above, &left, w, h, p.filter_intra_mode, bd, &mut pred);
|
||||
} else if is_directional_mode(p.mode) {
|
||||
directional(&mut above, &mut left, p, w, h, &mut pred);
|
||||
} else if p.mode == SMOOTH_PRED || p.mode == SMOOTH_V_PRED || p.mode == SMOOTH_H_PRED {
|
||||
smooth(&above, &left, p.mode, p.log2w, p.log2h, w, h, &mut pred);
|
||||
} else if p.mode == DC_PRED {
|
||||
dc(&above, &left, p.have_left, p.have_above, p.log2w, p.log2h, w, h, bd, &mut pred);
|
||||
} else {
|
||||
// PAETH_PRED
|
||||
let tl = above[EDGE - 1];
|
||||
for i in 0..h {
|
||||
for j in 0..w {
|
||||
let a = above[EDGE + j];
|
||||
let l = left[EDGE + i];
|
||||
let base = a + l - tl;
|
||||
let p_left = (base - l).abs();
|
||||
let p_top = (base - a).abs();
|
||||
let p_tl = (base - tl).abs();
|
||||
pred[i * w + j] = if p_left <= p_top && p_left <= p_tl {
|
||||
l as u16
|
||||
} else if p_top <= p_tl {
|
||||
a as u16
|
||||
} else {
|
||||
tl as u16
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in 0..h {
|
||||
pl.row_from_mut(y + i, x)[..w].copy_from_slice(&pred[i * w..i * w + w]);
|
||||
}
|
||||
}
|
||||
|
||||
fn recursive_intra(above: &[i32], left: &[i32], w: usize, h: usize, mode: usize, bd: u32, pred: &mut [u16]) {
|
||||
let w4 = w >> 2;
|
||||
let h2 = h >> 1;
|
||||
for i2 in 0..h2 {
|
||||
for j4 in 0..w4 {
|
||||
let mut pv = [0i32; 7];
|
||||
for (i, v) in pv.iter_mut().enumerate() {
|
||||
*v = if i < 5 {
|
||||
if i2 == 0 {
|
||||
above[EDGE + (j4 << 2) + i - 1]
|
||||
} else if j4 == 0 && i == 0 {
|
||||
left[EDGE + (i2 << 1) - 1]
|
||||
} else {
|
||||
pred[((i2 << 1) - 1) * w + (j4 << 2) + i - 1] as i32
|
||||
}
|
||||
} else if j4 == 0 {
|
||||
left[EDGE + (i2 << 1) + i - 5]
|
||||
} else {
|
||||
pred[((i2 << 1) + i - 5) * w + (j4 << 2) - 1] as i32
|
||||
};
|
||||
}
|
||||
for i1 in 0..2 {
|
||||
for j1 in 0..4 {
|
||||
let mut pr = 0i32;
|
||||
for i in 0..7 {
|
||||
pr += INTRA_FILTER_TAPS[mode][(i1 << 2) + j1][i] as i32 * pv[i];
|
||||
}
|
||||
pred[((i2 << 1) + i1) * w + (j4 << 2) + j1] = clip1(round2_signed(pr, INTRA_FILTER_SCALE_BITS as u32), bd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn round2_signed(x: i32, n: u32) -> i32 {
|
||||
if x >= 0 { round2(x, n) } else { -round2(-x, n) }
|
||||
}
|
||||
|
||||
fn sm_weights(log2: u32) -> &'static [u8] {
|
||||
match log2 {
|
||||
2 => &SM_WEIGHTS_TX_4X4,
|
||||
3 => &SM_WEIGHTS_TX_8X8,
|
||||
4 => &SM_WEIGHTS_TX_16X16,
|
||||
5 => &SM_WEIGHTS_TX_32X32,
|
||||
_ => &SM_WEIGHTS_TX_64X64,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn smooth(above: &[i32], left: &[i32], mode: usize, log2w: u32, log2h: u32, w: usize, h: usize, pred: &mut [u16]) {
|
||||
let wx = sm_weights(log2w);
|
||||
let wy = sm_weights(log2h);
|
||||
for i in 0..h {
|
||||
for j in 0..w {
|
||||
let v = if mode == SMOOTH_PRED {
|
||||
let s = wy[i] as i32 * above[EDGE + j]
|
||||
+ (256 - wy[i] as i32) * left[EDGE + h - 1]
|
||||
+ wx[j] as i32 * left[EDGE + i]
|
||||
+ (256 - wx[j] as i32) * above[EDGE + w - 1];
|
||||
round2(s, 9)
|
||||
} else if mode == SMOOTH_V_PRED {
|
||||
round2(wy[i] as i32 * above[EDGE + j] + (256 - wy[i] as i32) * left[EDGE + h - 1], 8)
|
||||
} else {
|
||||
round2(wx[j] as i32 * left[EDGE + i] + (256 - wx[j] as i32) * above[EDGE + w - 1], 8)
|
||||
};
|
||||
pred[i * w + j] = v as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn dc(above: &[i32], left: &[i32], have_left: bool, have_above: bool, log2w: u32, log2h: u32, w: usize, h: usize, bd: u32, pred: &mut [u16]) {
|
||||
let v = if have_left && have_above {
|
||||
let mut sum: i32 = left[EDGE..EDGE + h].iter().sum::<i32>() + above[EDGE..EDGE + w].iter().sum::<i32>();
|
||||
sum += ((w + h) >> 1) as i32;
|
||||
sum / (w + h) as i32
|
||||
} else if have_left {
|
||||
let sum: i32 = left[EDGE..EDGE + h].iter().sum();
|
||||
(sum + (h >> 1) as i32) >> log2h
|
||||
} else if have_above {
|
||||
let sum: i32 = above[EDGE..EDGE + w].iter().sum();
|
||||
(sum + (w >> 1) as i32) >> log2w
|
||||
} else {
|
||||
1 << (bd - 1)
|
||||
};
|
||||
let v = clip1(v, bd);
|
||||
pred[..w * h].iter_mut().for_each(|p| *p = v);
|
||||
}
|
||||
|
||||
fn edge_filter_strength(w: usize, h: usize, filter_type: bool, delta: i32) -> u32 {
|
||||
let d = delta.abs();
|
||||
let blk_wh = w + h;
|
||||
let mut strength = 0;
|
||||
if !filter_type {
|
||||
if blk_wh <= 8 {
|
||||
if d >= 56 {
|
||||
strength = 1;
|
||||
}
|
||||
} else if blk_wh <= 16 {
|
||||
if d >= 40 {
|
||||
strength = 1;
|
||||
}
|
||||
} else if blk_wh <= 24 {
|
||||
if d >= 8 {
|
||||
strength = 1;
|
||||
}
|
||||
if d >= 16 {
|
||||
strength = 2;
|
||||
}
|
||||
if d >= 32 {
|
||||
strength = 3;
|
||||
}
|
||||
} else if blk_wh <= 32 {
|
||||
strength = 1;
|
||||
if d >= 4 {
|
||||
strength = 2;
|
||||
}
|
||||
if d >= 32 {
|
||||
strength = 3;
|
||||
}
|
||||
} else {
|
||||
strength = 3;
|
||||
}
|
||||
} else if blk_wh <= 8 {
|
||||
if d >= 40 {
|
||||
strength = 1;
|
||||
}
|
||||
if d >= 64 {
|
||||
strength = 2;
|
||||
}
|
||||
} else if blk_wh <= 16 {
|
||||
if d >= 20 {
|
||||
strength = 1;
|
||||
}
|
||||
if d >= 48 {
|
||||
strength = 2;
|
||||
}
|
||||
} else if blk_wh <= 24 {
|
||||
if d >= 4 {
|
||||
strength = 3;
|
||||
}
|
||||
} else {
|
||||
strength = 3;
|
||||
}
|
||||
strength
|
||||
}
|
||||
|
||||
fn use_upsample(w: usize, h: usize, filter_type: bool, delta: i32) -> bool {
|
||||
let d = delta.abs();
|
||||
let blk_wh = w + h;
|
||||
if d <= 0 || d >= 40 {
|
||||
false
|
||||
} else if !filter_type {
|
||||
blk_wh <= 16
|
||||
} else {
|
||||
blk_wh <= 8
|
||||
}
|
||||
}
|
||||
|
||||
/// Intra edge filter (7.11.2.12) on buf[-1 .. sz-2] (edge[i] = buf[i - 1]).
|
||||
fn edge_filter(buf: &mut [i32], sz: usize, strength: u32) {
|
||||
if strength == 0 {
|
||||
return;
|
||||
}
|
||||
let mut edge = [0i32; 2 * 128 + 2 * EDGE];
|
||||
for i in 0..sz {
|
||||
edge[i] = buf[EDGE + i - 1];
|
||||
}
|
||||
for i in 1..sz {
|
||||
let mut s = 0;
|
||||
for j in 0..INTRA_EDGE_TAPS {
|
||||
let k = (i as i32 - 2 + j as i32).clamp(0, sz as i32 - 1) as usize;
|
||||
s += INTRA_EDGE_KERNEL[strength as usize - 1][j] as i32 * edge[k];
|
||||
}
|
||||
buf[EDGE + i - 1] = (s + 8) >> 4;
|
||||
}
|
||||
}
|
||||
|
||||
/// Intra edge upsample (7.11.2.11).
|
||||
fn edge_upsample(buf: &mut [i32], num_px: usize, bd: u32) {
|
||||
let mut dup = [0i32; 2 * 128 + 8];
|
||||
dup[0] = buf[EDGE - 1];
|
||||
for i in -1..num_px as i32 {
|
||||
dup[(i + 2) as usize] = buf[(EDGE as i32 + i) as usize];
|
||||
}
|
||||
dup[num_px + 2] = buf[EDGE + num_px - 1];
|
||||
buf[EDGE - 2] = dup[0];
|
||||
for i in 0..num_px {
|
||||
let s = -dup[i] + 9 * dup[i + 1] + 9 * dup[i + 2] - dup[i + 3];
|
||||
let s = clip1(round2(s, 4), bd) as i32;
|
||||
buf[EDGE + 2 * i - 1] = s;
|
||||
buf[EDGE + 2 * i] = dup[i + 2];
|
||||
}
|
||||
}
|
||||
|
||||
fn directional(above: &mut [i32], left: &mut [i32], p: &IntraParams, w: usize, h: usize, pred: &mut [u16]) {
|
||||
let p_angle = MODE_TO_ANGLE[p.mode] as i32 + p.angle_delta * ANGLE_STEP as i32;
|
||||
let mut upsample_above = 0u32;
|
||||
let mut upsample_left = 0u32;
|
||||
if p.enable_intra_edge_filter {
|
||||
if p_angle != 90 && p_angle != 180 {
|
||||
if p_angle > 90 && p_angle < 180 && (w + h) >= 24 {
|
||||
let s = left[EDGE] * 5 + above[EDGE - 1] * 6 + above[EDGE] * 5;
|
||||
let c = round2(s, 4);
|
||||
left[EDGE - 1] = c;
|
||||
above[EDGE - 1] = c;
|
||||
}
|
||||
if p.have_above {
|
||||
let strength = edge_filter_strength(w, h, p.filter_type, p_angle - 90);
|
||||
let num_px = w.min((p.max_x - p.x as i32 + 1) as usize) + if p_angle < 90 { h } else { 0 } + 1;
|
||||
edge_filter(above, num_px, strength);
|
||||
}
|
||||
if p.have_left {
|
||||
let strength = edge_filter_strength(w, h, p.filter_type, p_angle - 180);
|
||||
let num_px = h.min((p.max_y - p.y as i32 + 1) as usize) + if p_angle > 180 { w } else { 0 } + 1;
|
||||
edge_filter(left, num_px, strength);
|
||||
}
|
||||
}
|
||||
if use_upsample(w, h, p.filter_type, p_angle - 90) {
|
||||
upsample_above = 1;
|
||||
let num_px = w + if p_angle < 90 { h } else { 0 };
|
||||
edge_upsample(above, num_px, p.bit_depth);
|
||||
}
|
||||
if use_upsample(w, h, p.filter_type, p_angle - 180) {
|
||||
upsample_left = 1;
|
||||
let num_px = h + if p_angle > 180 { w } else { 0 };
|
||||
edge_upsample(left, num_px, p.bit_depth);
|
||||
}
|
||||
}
|
||||
let dx = if p_angle < 90 {
|
||||
DR_INTRA_DERIVATIVE[p_angle as usize] as i32
|
||||
} else if p_angle > 90 && p_angle < 180 {
|
||||
DR_INTRA_DERIVATIVE[(180 - p_angle) as usize] as i32
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let dy = if p_angle > 90 && p_angle < 180 {
|
||||
DR_INTRA_DERIVATIVE[(p_angle - 90) as usize] as i32
|
||||
} else if p_angle > 180 {
|
||||
DR_INTRA_DERIVATIVE[(270 - p_angle) as usize] as i32
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let a = |i: i32| above[(EDGE as i32 + i) as usize];
|
||||
let l = |i: i32| left[(EDGE as i32 + i) as usize];
|
||||
for i in 0..h as i32 {
|
||||
for j in 0..w as i32 {
|
||||
let v = if p_angle < 90 {
|
||||
let idx = (i + 1) * dx;
|
||||
let base = (idx >> (6 - upsample_above)) + (j << upsample_above);
|
||||
let shift = ((idx << upsample_above) >> 1) & 0x1f;
|
||||
let max_base_x = (w as i32 + h as i32 - 1) << upsample_above;
|
||||
if base < max_base_x { round2(a(base) * (32 - shift) + a(base + 1) * shift, 5) } else { a(max_base_x) }
|
||||
} else if p_angle > 90 && p_angle < 180 {
|
||||
let idx = (j << 6) - (i + 1) * dx;
|
||||
let base = idx >> (6 - upsample_above);
|
||||
if base >= -(1 << upsample_above) {
|
||||
let shift = ((idx << upsample_above) >> 1) & 0x1f;
|
||||
round2(a(base) * (32 - shift) + a(base + 1) * shift, 5)
|
||||
} else {
|
||||
let idx = (i << 6) - (j + 1) * dy;
|
||||
let base = idx >> (6 - upsample_left);
|
||||
let shift = ((idx << upsample_left) >> 1) & 0x1f;
|
||||
round2(l(base) * (32 - shift) + l(base + 1) * shift, 5)
|
||||
}
|
||||
} else if p_angle > 180 {
|
||||
let idx = (j + 1) * dy;
|
||||
let base = (idx >> (6 - upsample_left)) + (i << upsample_left);
|
||||
let shift = ((idx << upsample_left) >> 1) & 0x1f;
|
||||
round2(l(base) * (32 - shift) + l(base + 1) * shift, 5)
|
||||
} else if p_angle == 90 {
|
||||
a(j)
|
||||
} else {
|
||||
l(i)
|
||||
};
|
||||
pred[(i as usize) * w + j as usize] = v as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
//! Clean-room AV1 decoder, implemented from the AV1 Bitstream & Decoding Process Specification
|
||||
//! v1.0.0 with Errata 1 (AOMediaCodec/av1-spec).
|
||||
//!
|
||||
//! See the crate README for the conformance status of each decoding stage.
|
||||
|
||||
// Items used only by stages still being written (inter prediction, post filters).
|
||||
// Spec-style indexed loops (FilmCraft allows this lint workspace-wide).
|
||||
#![allow(clippy::needless_range_loop)]
|
||||
#![allow(dead_code)]
|
||||
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))]
|
||||
|
||||
mod bits;
|
||||
mod cdef;
|
||||
mod cdf;
|
||||
mod decoder;
|
||||
mod frame;
|
||||
mod grain;
|
||||
pub mod header;
|
||||
mod inter;
|
||||
mod inter_info;
|
||||
mod intra;
|
||||
mod mvpred;
|
||||
mod par;
|
||||
mod postfilter;
|
||||
mod restoration;
|
||||
mod spec_tables;
|
||||
mod state;
|
||||
mod stats;
|
||||
mod symbol;
|
||||
mod tile;
|
||||
mod transform;
|
||||
|
||||
pub use decoder::Decoder;
|
||||
pub use header::{ColorConfig, FrameHeader, SequenceHeader};
|
||||
pub use stats::{DecodeStats, Stage};
|
||||
|
||||
/// Decoder errors.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("truncated AV1 data")]
|
||||
Truncated,
|
||||
#[error("invalid AV1 data: {0}")]
|
||||
Invalid(&'static str),
|
||||
#[error("unsupported AV1 feature: {0}")]
|
||||
Unsupported(&'static str),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// A decoded (shown) frame: planar samples without row padding, `bit_depth` significant bits in
|
||||
/// `u16`. Chroma planes are `((width + ssx) >> ssx) x ((height + ssy) >> ssy)`; they are empty
|
||||
/// for monochrome streams.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Picture {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub bit_depth: u8,
|
||||
pub subsampling_x: u8,
|
||||
pub subsampling_y: u8,
|
||||
pub mono_chrome: bool,
|
||||
pub planes: [Vec<u16>; 3],
|
||||
pub color_primaries: u8,
|
||||
pub transfer_characteristics: u8,
|
||||
pub matrix_coefficients: u8,
|
||||
pub full_range: bool,
|
||||
/// Timestamp of the temporal unit that showed the picture ([`Decoder::decode_pts`]).
|
||||
pub pts: i64,
|
||||
/// Decoded in draft mode without deblocking / CDEF / loop restoration
|
||||
/// ([`Decoder::set_draft`]): approximate, for reduced-resolution playback only.
|
||||
pub draft: bool,
|
||||
}
|
||||
|
||||
impl Picture {
|
||||
pub fn plane_width(&self, plane: usize) -> u32 {
|
||||
if plane == 0 { self.width } else { (self.width + self.subsampling_x as u32) >> self.subsampling_x }
|
||||
}
|
||||
pub fn plane_height(&self, plane: usize) -> u32 {
|
||||
if plane == 0 { self.height } else { (self.height + self.subsampling_y as u32) >> self.subsampling_y }
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a temporal unit starts a random access point: its first frame is a shown key frame
|
||||
/// (used for keyframe detection: containers may flag samples as sync that are not).
|
||||
pub fn is_key_frame_unit(data: &[u8]) -> bool {
|
||||
let mut pos = 0;
|
||||
let mut seq_seen = false;
|
||||
while pos < data.len() {
|
||||
let h = data[pos];
|
||||
let obu_type = (h >> 3) & 0xf;
|
||||
let ext = h & 4 != 0;
|
||||
let has_size = h & 2 != 0;
|
||||
let mut p = pos + 1 + ext as usize;
|
||||
let size = if has_size {
|
||||
match bits::leb128(data.get(p..).unwrap_or(&[])) {
|
||||
Ok((v, n)) => {
|
||||
p += n;
|
||||
v as usize
|
||||
}
|
||||
Err(_) => return false,
|
||||
}
|
||||
} else {
|
||||
data.len().saturating_sub(p)
|
||||
};
|
||||
if p > data.len() {
|
||||
return false;
|
||||
}
|
||||
match obu_type {
|
||||
1 => seq_seen = true,
|
||||
3 | 6 => {
|
||||
let b = data.get(p).copied().unwrap_or(0);
|
||||
// show_existing_frame = 0, frame_type = KEY_FRAME (0), show_frame = 1
|
||||
let show_existing = b >> 7;
|
||||
let frame_type = (b >> 5) & 3;
|
||||
let show_frame = (b >> 4) & 1;
|
||||
let _ = seq_seen;
|
||||
return show_existing == 0 && frame_type == 0 && show_frame == 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
pos = p.saturating_add(size);
|
||||
}
|
||||
false
|
||||
}
|
||||
@@ -0,0 +1,737 @@
|
||||
//! Motion vector prediction processes (spec 7.10): find_mv_stack, has_overlappable_candidates
|
||||
//! and find_warp_samples.
|
||||
|
||||
use crate::spec_tables::{
|
||||
GLOBAL_GLOBALMV, GLOBALMV, IDENTITY, INTRA_FRAME, LEAST_SQUARES_SAMPLES_MAX, MAX_REF_MV_STACK_SIZE, MV_BORDER, NEAR_NEWMV, NEAREST_NEWMV,
|
||||
NEW_NEARESTMV, NEW_NEARMV, NEW_NEWMV, NEWMV, NUM_4X4_BLOCKS_HIGH, NUM_4X4_BLOCKS_WIDE, REF_CAT_LEVEL, TRANSLATION, WARPEDMODEL_PREC_BITS,
|
||||
};
|
||||
use crate::tile::TileDecoder;
|
||||
|
||||
pub(crate) type MvP = [i32; 2];
|
||||
pub(crate) const INVALID_MV: i32 = -1 << 15;
|
||||
|
||||
/// Output of find_mv_stack plus its working state.
|
||||
#[derive(Clone, Default)]
|
||||
pub(crate) struct MvStack {
|
||||
pub ref_stack_mv: [[MvP; 2]; MAX_REF_MV_STACK_SIZE + 1],
|
||||
pub weight_stack: [u32; MAX_REF_MV_STACK_SIZE + 1],
|
||||
pub num_mv_found: usize,
|
||||
pub new_mv_count: usize,
|
||||
pub global_mvs: [MvP; 2],
|
||||
pub found_match: bool,
|
||||
pub close_matches: usize,
|
||||
pub total_matches: usize,
|
||||
pub new_mv_context: usize,
|
||||
pub ref_mv_context: usize,
|
||||
pub zero_mv_context: usize,
|
||||
pub drl_ctx_stack: [usize; MAX_REF_MV_STACK_SIZE + 1],
|
||||
ref_id_count: [usize; 2],
|
||||
ref_diff_count: [usize; 2],
|
||||
ref_id_mvs: [[MvP; 2]; 2],
|
||||
ref_diff_mvs: [[MvP; 2]; 2],
|
||||
// warp samples
|
||||
pub num_samples: usize,
|
||||
pub num_samples_scanned: usize,
|
||||
pub cand_list: [[i32; 4]; LEAST_SQUARES_SAMPLES_MAX],
|
||||
}
|
||||
|
||||
fn block_width(bs: usize) -> i32 {
|
||||
4 * NUM_4X4_BLOCKS_WIDE[bs] as i32
|
||||
}
|
||||
fn block_height(bs: usize) -> i32 {
|
||||
4 * NUM_4X4_BLOCKS_HIGH[bs] as i32
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn round2_signed64(x: i64, n: u32) -> i64 {
|
||||
if n == 0 {
|
||||
return x;
|
||||
}
|
||||
if x >= 0 { (x + (1 << (n - 1))) >> n } else { -((-x + (1 << (n - 1))) >> n) }
|
||||
}
|
||||
|
||||
pub(crate) fn has_newmv(mode: usize) -> bool {
|
||||
matches!(mode, NEWMV | NEW_NEWMV | NEAR_NEWMV | NEW_NEARMV | NEAREST_NEWMV | NEW_NEARESTMV)
|
||||
}
|
||||
|
||||
impl TileDecoder<'_, '_> {
|
||||
/// lower_mv_precision( candMv ) (7.10.2.10)
|
||||
pub(crate) fn lower_mv_precision(&self, mv: &mut MvP) {
|
||||
let fh = &self.fs.fh;
|
||||
if fh.allow_high_precision_mv {
|
||||
return;
|
||||
}
|
||||
for c in mv.iter_mut() {
|
||||
if fh.force_integer_mv {
|
||||
let a = c.abs();
|
||||
let a_int = (a + 3) >> 3;
|
||||
*c = if *c > 0 { a_int << 3 } else { -(a_int << 3) };
|
||||
} else if *c & 1 != 0 {
|
||||
*c += if *c > 0 { -1 } else { 1 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Setup global MV process (7.10.2.1).
|
||||
pub(crate) fn setup_global_mv(&self, ref_list: usize) -> MvP {
|
||||
let rf = self.b.ref_frame[ref_list];
|
||||
let fh = &self.fs.fh;
|
||||
let mut mv: MvP = [0, 0];
|
||||
if rf > INTRA_FRAME as i8 {
|
||||
let rf = rf as usize;
|
||||
let typ = fh.gm_type[rf] as usize;
|
||||
let gm = &fh.gm_params[rf];
|
||||
if typ == TRANSLATION {
|
||||
mv = [gm[0] >> (WARPEDMODEL_PREC_BITS - 3), gm[1] >> (WARPEDMODEL_PREC_BITS - 3)];
|
||||
} else if typ != IDENTITY {
|
||||
let bw = block_width(self.b.mi_size) as i64;
|
||||
let bh = block_height(self.b.mi_size) as i64;
|
||||
let x = self.b.mi_col as i64 * 4 + bw / 2 - 1;
|
||||
let y = self.b.mi_row as i64 * 4 + bh / 2 - 1;
|
||||
let p = 1i64 << WARPEDMODEL_PREC_BITS;
|
||||
let xc = (gm[2] as i64 - p) * x + gm[3] as i64 * y + gm[0] as i64;
|
||||
let yc = gm[4] as i64 * x + (gm[5] as i64 - p) * y + gm[1] as i64;
|
||||
if fh.allow_high_precision_mv {
|
||||
mv = [round2_signed64(yc, WARPEDMODEL_PREC_BITS as u32 - 3) as i32, round2_signed64(xc, WARPEDMODEL_PREC_BITS as u32 - 3) as i32];
|
||||
} else {
|
||||
mv = [
|
||||
round2_signed64(yc, WARPEDMODEL_PREC_BITS as u32 - 2) as i32 * 2,
|
||||
round2_signed64(xc, WARPEDMODEL_PREC_BITS as u32 - 2) as i32 * 2,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
self.lower_mv_precision(&mut mv);
|
||||
mv
|
||||
}
|
||||
|
||||
/// Find MV stack process (7.10.2).
|
||||
pub(crate) fn find_mv_stack(&mut self, is_compound: bool) {
|
||||
let bw4 = self.b.bw4 as isize;
|
||||
let bh4 = self.b.bh4 as isize;
|
||||
let mut s = MvStack { global_mvs: [self.setup_global_mv(0), [0, 0]], ..Default::default() };
|
||||
if is_compound {
|
||||
s.global_mvs[1] = self.setup_global_mv(1);
|
||||
}
|
||||
self.mvs = s;
|
||||
self.mvs.found_match = false;
|
||||
self.scan_row(-1, is_compound);
|
||||
let mut found_above = self.mvs.found_match;
|
||||
self.mvs.found_match = false;
|
||||
self.scan_col(-1, is_compound);
|
||||
let mut found_left = self.mvs.found_match;
|
||||
self.mvs.found_match = false;
|
||||
if bw4.max(bh4) <= 16 {
|
||||
self.scan_point(-1, bw4, is_compound);
|
||||
}
|
||||
if self.mvs.found_match {
|
||||
found_above = true;
|
||||
}
|
||||
self.mvs.close_matches = found_above as usize + found_left as usize;
|
||||
let num_nearest = self.mvs.num_mv_found;
|
||||
let num_new = self.mvs.new_mv_count;
|
||||
for idx in 0..num_nearest {
|
||||
self.mvs.weight_stack[idx] += REF_CAT_LEVEL as u32;
|
||||
}
|
||||
self.mvs.zero_mv_context = 0;
|
||||
if self.fs.fh.use_ref_frame_mvs {
|
||||
self.temporal_scan(is_compound);
|
||||
}
|
||||
self.scan_point(-1, -1, is_compound);
|
||||
if self.mvs.found_match {
|
||||
found_above = true;
|
||||
}
|
||||
self.mvs.found_match = false;
|
||||
self.scan_row(-3, is_compound);
|
||||
if self.mvs.found_match {
|
||||
found_above = true;
|
||||
}
|
||||
self.mvs.found_match = false;
|
||||
self.scan_col(-3, is_compound);
|
||||
if self.mvs.found_match {
|
||||
found_left = true;
|
||||
}
|
||||
self.mvs.found_match = false;
|
||||
if bh4 > 1 {
|
||||
self.scan_row(-5, is_compound);
|
||||
}
|
||||
if self.mvs.found_match {
|
||||
found_above = true;
|
||||
}
|
||||
self.mvs.found_match = false;
|
||||
if bw4 > 1 {
|
||||
self.scan_col(-5, is_compound);
|
||||
}
|
||||
if self.mvs.found_match {
|
||||
found_left = true;
|
||||
}
|
||||
self.mvs.total_matches = found_above as usize + found_left as usize;
|
||||
let n = self.mvs.num_mv_found;
|
||||
self.sort_stack(0, num_nearest, is_compound);
|
||||
self.sort_stack(num_nearest, n, is_compound);
|
||||
if self.mvs.num_mv_found < 2 {
|
||||
self.extra_search(is_compound);
|
||||
}
|
||||
self.context_and_clamping(is_compound, num_new);
|
||||
}
|
||||
|
||||
fn scan_row(&mut self, delta_row: isize, is_compound: bool) {
|
||||
let bw4 = self.b.bw4 as isize;
|
||||
let (mi_row, mi_col) = (self.b.mi_row as isize, self.b.mi_col as isize);
|
||||
let mi_cols = self.fs.fh.mi_cols as isize;
|
||||
let end4 = bw4.min(mi_cols - mi_col).min(16);
|
||||
let mut delta_col = 0;
|
||||
let use_step16 = bw4 >= 16;
|
||||
let mut delta_row = delta_row;
|
||||
if delta_row.abs() > 1 {
|
||||
delta_row += mi_row & 1;
|
||||
delta_col = 1 - (mi_col & 1);
|
||||
}
|
||||
let mut i = 0;
|
||||
while i < end4 {
|
||||
let mv_row = mi_row + delta_row;
|
||||
let mv_col = mi_col + delta_col + i;
|
||||
if !self.inside(mv_row, mv_col) {
|
||||
break;
|
||||
}
|
||||
let mi = &self.t.mi;
|
||||
let mut len = bw4.min(NUM_4X4_BLOCKS_WIDE[mi.mi_size[mi.idx(mv_row as usize, mv_col as usize)] as usize] as isize);
|
||||
if delta_row.abs() > 1 {
|
||||
len = len.max(2);
|
||||
}
|
||||
if use_step16 {
|
||||
len = len.max(4);
|
||||
}
|
||||
let weight = len as u32 * 2;
|
||||
self.add_ref_mv_candidate(mv_row as usize, mv_col as usize, is_compound, weight);
|
||||
i += len;
|
||||
}
|
||||
}
|
||||
|
||||
fn scan_col(&mut self, delta_col: isize, is_compound: bool) {
|
||||
let bh4 = self.b.bh4 as isize;
|
||||
let (mi_row, mi_col) = (self.b.mi_row as isize, self.b.mi_col as isize);
|
||||
let mi_rows = self.fs.fh.mi_rows as isize;
|
||||
let end4 = bh4.min(mi_rows - mi_row).min(16);
|
||||
let mut delta_row = 0;
|
||||
let use_step16 = bh4 >= 16;
|
||||
let mut delta_col = delta_col;
|
||||
if delta_col.abs() > 1 {
|
||||
delta_row = 1 - (mi_row & 1);
|
||||
delta_col += mi_col & 1;
|
||||
}
|
||||
let mut i = 0;
|
||||
while i < end4 {
|
||||
let mv_row = mi_row + delta_row + i;
|
||||
let mv_col = mi_col + delta_col;
|
||||
if !self.inside(mv_row, mv_col) {
|
||||
break;
|
||||
}
|
||||
let mi = &self.t.mi;
|
||||
let mut len = bh4.min(NUM_4X4_BLOCKS_HIGH[mi.mi_size[mi.idx(mv_row as usize, mv_col as usize)] as usize] as isize);
|
||||
if delta_col.abs() > 1 {
|
||||
len = len.max(2);
|
||||
}
|
||||
if use_step16 {
|
||||
len = len.max(4);
|
||||
}
|
||||
let weight = len as u32 * 2;
|
||||
self.add_ref_mv_candidate(mv_row as usize, mv_col as usize, is_compound, weight);
|
||||
i += len;
|
||||
}
|
||||
}
|
||||
|
||||
fn scan_point(&mut self, delta_row: isize, delta_col: isize, is_compound: bool) {
|
||||
let mv_row = self.b.mi_row as isize + delta_row;
|
||||
let mv_col = self.b.mi_col as isize + delta_col;
|
||||
if self.inside(mv_row, mv_col) && self.t.mi.written[self.t.mi.idx(mv_row as usize, mv_col as usize)] {
|
||||
self.add_ref_mv_candidate(mv_row as usize, mv_col as usize, is_compound, 4);
|
||||
}
|
||||
}
|
||||
|
||||
fn temporal_scan(&mut self, is_compound: bool) {
|
||||
let bw4 = self.b.bw4 as isize;
|
||||
let bh4 = self.b.bh4 as isize;
|
||||
let step_w4 = if bw4 >= 16 { 4 } else { 2 };
|
||||
let step_h4 = if bh4 >= 16 { 4 } else { 2 };
|
||||
let mut dr = 0;
|
||||
while dr < bh4.min(16) {
|
||||
let mut dc = 0;
|
||||
while dc < bw4.min(16) {
|
||||
self.add_tpl_ref_mv(dr, dc, is_compound);
|
||||
dc += step_w4;
|
||||
}
|
||||
dr += step_h4;
|
||||
}
|
||||
let allow_extension = (2..16).contains(&bh4) && (2..16).contains(&bw4);
|
||||
if allow_extension {
|
||||
let pos = [(bh4, -2), (bh4, bw4), (bh4 - 2, bw4)];
|
||||
for &(dr, dc) in &pos {
|
||||
let row = (self.b.mi_row as isize & 15) + dr;
|
||||
let col = (self.b.mi_col as isize & 15) + dc;
|
||||
if (0..16).contains(&row) && (0..16).contains(&col) {
|
||||
self.add_tpl_ref_mv(dr, dc, is_compound);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_tpl_ref_mv(&mut self, delta_row: isize, delta_col: isize, is_compound: bool) {
|
||||
let mv_row = (self.b.mi_row as isize + delta_row) | 1;
|
||||
let mv_col = (self.b.mi_col as isize + delta_col) | 1;
|
||||
if !self.inside(mv_row, mv_col) {
|
||||
return;
|
||||
}
|
||||
let x8 = (mv_col >> 1) as usize;
|
||||
let y8 = (mv_row >> 1) as usize;
|
||||
let w8 = self.fs.fh.mi_cols as usize >> 1;
|
||||
if delta_row == 0 && delta_col == 0 {
|
||||
self.mvs.zero_mv_context = 1;
|
||||
}
|
||||
let rf0 = self.b.ref_frame[0] as usize;
|
||||
if !is_compound {
|
||||
let mut cand = self.fs.motion_field[rf0][y8 * w8 + x8];
|
||||
if cand[0] == INVALID_MV {
|
||||
return;
|
||||
}
|
||||
self.lower_mv_precision(&mut cand);
|
||||
if delta_row == 0 && delta_col == 0 {
|
||||
let g = self.mvs.global_mvs[0];
|
||||
self.mvs.zero_mv_context = ((cand[0] - g[0]).abs() >= 16 || (cand[1] - g[1]).abs() >= 16) as usize;
|
||||
}
|
||||
let n = self.mvs.num_mv_found;
|
||||
let idx = (0..n).find(|&i| self.mvs.ref_stack_mv[i][0] == cand);
|
||||
if let Some(i) = idx {
|
||||
self.mvs.weight_stack[i] += 2;
|
||||
} else if n < MAX_REF_MV_STACK_SIZE {
|
||||
self.mvs.ref_stack_mv[n][0] = cand;
|
||||
self.mvs.weight_stack[n] = 2;
|
||||
self.mvs.num_mv_found += 1;
|
||||
}
|
||||
} else {
|
||||
let rf1 = self.b.ref_frame[1] as usize;
|
||||
let mut c0 = self.fs.motion_field[rf0][y8 * w8 + x8];
|
||||
if c0[0] == INVALID_MV {
|
||||
return;
|
||||
}
|
||||
let mut c1 = self.fs.motion_field[rf1][y8 * w8 + x8];
|
||||
if c1[0] == INVALID_MV {
|
||||
return;
|
||||
}
|
||||
self.lower_mv_precision(&mut c0);
|
||||
self.lower_mv_precision(&mut c1);
|
||||
if delta_row == 0 && delta_col == 0 {
|
||||
let g = self.mvs.global_mvs;
|
||||
self.mvs.zero_mv_context = ((c0[0] - g[0][0]).abs() >= 16
|
||||
|| (c0[1] - g[0][1]).abs() >= 16
|
||||
|| (c1[0] - g[1][0]).abs() >= 16
|
||||
|| (c1[1] - g[1][1]).abs() >= 16) as usize;
|
||||
}
|
||||
let n = self.mvs.num_mv_found;
|
||||
let idx = (0..n).find(|&i| self.mvs.ref_stack_mv[i][0] == c0 && self.mvs.ref_stack_mv[i][1] == c1);
|
||||
if let Some(i) = idx {
|
||||
self.mvs.weight_stack[i] += 2;
|
||||
} else if n < MAX_REF_MV_STACK_SIZE {
|
||||
self.mvs.ref_stack_mv[n] = [c0, c1];
|
||||
self.mvs.weight_stack[n] = 2;
|
||||
self.mvs.num_mv_found += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_ref_mv_candidate(&mut self, mv_row: usize, mv_col: usize, is_compound: bool, weight: u32) {
|
||||
let mi = &self.t.mi;
|
||||
let i = mi.idx(mv_row, mv_col);
|
||||
if !mi.is_inter[i] {
|
||||
return;
|
||||
}
|
||||
let rf = mi.ref_frame[i];
|
||||
if !is_compound {
|
||||
for cand_list in 0..2 {
|
||||
if rf[cand_list] == self.b.ref_frame[0] {
|
||||
self.search_stack(mv_row, mv_col, cand_list, weight);
|
||||
}
|
||||
}
|
||||
} else if rf[0] == self.b.ref_frame[0] && rf[1] == self.b.ref_frame[1] {
|
||||
self.compound_search_stack(mv_row, mv_col, weight);
|
||||
}
|
||||
}
|
||||
|
||||
fn search_stack(&mut self, mv_row: usize, mv_col: usize, cand_list: usize, weight: u32) {
|
||||
let mi = &self.t.mi;
|
||||
let i = mi.idx(mv_row, mv_col);
|
||||
let cand_mode = mi.y_mode[i] as usize;
|
||||
let cand_size = mi.mi_size[i] as usize;
|
||||
let large = block_width(cand_size).min(block_height(cand_size)) >= 8;
|
||||
let gm0 = self.fs.fh.gm_type[self.b.ref_frame[0] as usize] as usize;
|
||||
let mut cand = if (cand_mode == GLOBALMV || cand_mode == GLOBAL_GLOBALMV) && gm0 > TRANSLATION && large {
|
||||
self.mvs.global_mvs[0]
|
||||
} else {
|
||||
let m = mi.mv[i][cand_list];
|
||||
[m.row as i32, m.col as i32]
|
||||
};
|
||||
self.lower_mv_precision(&mut cand);
|
||||
if has_newmv(cand_mode) {
|
||||
self.mvs.new_mv_count += 1;
|
||||
}
|
||||
self.mvs.found_match = true;
|
||||
let n = self.mvs.num_mv_found;
|
||||
if let Some(idx) = (0..n).find(|&k| self.mvs.ref_stack_mv[k][0] == cand) {
|
||||
self.mvs.weight_stack[idx] += weight;
|
||||
} else if n < MAX_REF_MV_STACK_SIZE {
|
||||
self.mvs.ref_stack_mv[n][0] = cand;
|
||||
self.mvs.weight_stack[n] = weight;
|
||||
self.mvs.num_mv_found += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn compound_search_stack(&mut self, mv_row: usize, mv_col: usize, weight: u32) {
|
||||
let mi = &self.t.mi;
|
||||
let i = mi.idx(mv_row, mv_col);
|
||||
let mut cand = [[mi.mv[i][0].row as i32, mi.mv[i][0].col as i32], [mi.mv[i][1].row as i32, mi.mv[i][1].col as i32]];
|
||||
let cand_mode = mi.y_mode[i] as usize;
|
||||
if cand_mode == GLOBAL_GLOBALMV {
|
||||
for (rl, c) in cand.iter_mut().enumerate() {
|
||||
if self.fs.fh.gm_type[self.b.ref_frame[rl] as usize] as usize > TRANSLATION {
|
||||
*c = self.mvs.global_mvs[rl];
|
||||
}
|
||||
}
|
||||
}
|
||||
for c in cand.iter_mut() {
|
||||
self.lower_mv_precision(c);
|
||||
}
|
||||
self.mvs.found_match = true;
|
||||
let n = self.mvs.num_mv_found;
|
||||
if let Some(idx) = (0..n).find(|&k| self.mvs.ref_stack_mv[k][0] == cand[0] && self.mvs.ref_stack_mv[k][1] == cand[1]) {
|
||||
self.mvs.weight_stack[idx] += weight;
|
||||
} else if n < MAX_REF_MV_STACK_SIZE {
|
||||
self.mvs.ref_stack_mv[n] = cand;
|
||||
self.mvs.weight_stack[n] = weight;
|
||||
self.mvs.num_mv_found += 1;
|
||||
}
|
||||
if has_newmv(cand_mode) {
|
||||
self.mvs.new_mv_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn sort_stack(&mut self, start: usize, end: usize, is_compound: bool) {
|
||||
let mut end = end;
|
||||
let s = &mut self.mvs;
|
||||
while end > start {
|
||||
let mut new_end = start;
|
||||
for idx in start + 1..end {
|
||||
if s.weight_stack[idx - 1] < s.weight_stack[idx] {
|
||||
s.weight_stack.swap(idx - 1, idx);
|
||||
for list in 0..1 + is_compound as usize {
|
||||
let t = s.ref_stack_mv[idx - 1][list];
|
||||
s.ref_stack_mv[idx - 1][list] = s.ref_stack_mv[idx][list];
|
||||
s.ref_stack_mv[idx][list] = t;
|
||||
}
|
||||
new_end = idx;
|
||||
}
|
||||
}
|
||||
end = new_end;
|
||||
}
|
||||
}
|
||||
|
||||
fn extra_search(&mut self, is_compound: bool) {
|
||||
self.mvs.ref_id_count = [0; 2];
|
||||
self.mvs.ref_diff_count = [0; 2];
|
||||
let (mi_row, mi_col) = (self.b.mi_row as isize, self.b.mi_col as isize);
|
||||
let mut w4 = (self.b.bw4 as isize).min(16);
|
||||
let mut h4 = (self.b.bh4 as isize).min(16);
|
||||
w4 = w4.min(self.fs.fh.mi_cols as isize - mi_col);
|
||||
h4 = h4.min(self.fs.fh.mi_rows as isize - mi_row);
|
||||
let num4x4 = w4.min(h4);
|
||||
for pass in 0..2 {
|
||||
let mut idx = 0;
|
||||
while idx < num4x4 && self.mvs.num_mv_found < 2 {
|
||||
let (mv_row, mv_col) = if pass == 0 { (mi_row - 1, mi_col + idx) } else { (mi_row + idx, mi_col - 1) };
|
||||
if !self.inside(mv_row, mv_col) {
|
||||
break;
|
||||
}
|
||||
self.add_extra_mv_candidate(mv_row as usize, mv_col as usize, is_compound);
|
||||
let mi = &self.t.mi;
|
||||
let sz = mi.mi_size[mi.idx(mv_row as usize, mv_col as usize)] as usize;
|
||||
idx += if pass == 0 { NUM_4X4_BLOCKS_WIDE[sz] } else { NUM_4X4_BLOCKS_HIGH[sz] } as isize;
|
||||
}
|
||||
}
|
||||
if is_compound {
|
||||
let mut combined = [[[0i32; 2]; 2]; 2];
|
||||
for list in 0..2 {
|
||||
let mut comp_count = 0;
|
||||
for idx in 0..self.mvs.ref_id_count[list] {
|
||||
combined[comp_count][list] = self.mvs.ref_id_mvs[list][idx];
|
||||
comp_count += 1;
|
||||
}
|
||||
let mut idx = 0;
|
||||
while idx < self.mvs.ref_diff_count[list] && comp_count < 2 {
|
||||
combined[comp_count][list] = self.mvs.ref_diff_mvs[list][idx];
|
||||
comp_count += 1;
|
||||
idx += 1;
|
||||
}
|
||||
while comp_count < 2 {
|
||||
combined[comp_count][list] = self.mvs.global_mvs[list];
|
||||
comp_count += 1;
|
||||
}
|
||||
}
|
||||
let n = self.mvs.num_mv_found;
|
||||
if n == 1 {
|
||||
if combined[0][0] == self.mvs.ref_stack_mv[0][0] && combined[0][1] == self.mvs.ref_stack_mv[0][1] {
|
||||
self.mvs.ref_stack_mv[n] = combined[1];
|
||||
} else {
|
||||
self.mvs.ref_stack_mv[n] = combined[0];
|
||||
}
|
||||
self.mvs.weight_stack[n] = 2;
|
||||
self.mvs.num_mv_found += 1;
|
||||
} else {
|
||||
for c in combined {
|
||||
let n = self.mvs.num_mv_found;
|
||||
self.mvs.ref_stack_mv[n] = c;
|
||||
self.mvs.weight_stack[n] = 2;
|
||||
self.mvs.num_mv_found += 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for idx in self.mvs.num_mv_found..2 {
|
||||
self.mvs.ref_stack_mv[idx][0] = self.mvs.global_mvs[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_extra_mv_candidate(&mut self, mv_row: usize, mv_col: usize, is_compound: bool) {
|
||||
let mi = &self.t.mi;
|
||||
let i = mi.idx(mv_row, mv_col);
|
||||
let rfs = mi.ref_frame[i];
|
||||
let mvs = mi.mv[i];
|
||||
let bias = &self.fs.fh.ref_frame_sign_bias;
|
||||
if is_compound {
|
||||
for cand_list in 0..2 {
|
||||
let cand_ref = rfs[cand_list];
|
||||
if cand_ref > INTRA_FRAME as i8 {
|
||||
for list in 0..2 {
|
||||
let mut cand = [mvs[cand_list].row as i32, mvs[cand_list].col as i32];
|
||||
if cand_ref == self.b.ref_frame[list] && self.mvs.ref_id_count[list] < 2 {
|
||||
let k = self.mvs.ref_id_count[list];
|
||||
self.mvs.ref_id_mvs[list][k] = cand;
|
||||
self.mvs.ref_id_count[list] += 1;
|
||||
} else if self.mvs.ref_diff_count[list] < 2 {
|
||||
if bias[cand_ref as usize] != bias[self.b.ref_frame[list] as usize] {
|
||||
cand = [-cand[0], -cand[1]];
|
||||
}
|
||||
let k = self.mvs.ref_diff_count[list];
|
||||
self.mvs.ref_diff_mvs[list][k] = cand;
|
||||
self.mvs.ref_diff_count[list] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for cand_list in 0..2 {
|
||||
let cand_ref = rfs[cand_list];
|
||||
if cand_ref > INTRA_FRAME as i8 {
|
||||
let mut cand = [mvs[cand_list].row as i32, mvs[cand_list].col as i32];
|
||||
if bias[cand_ref as usize] != bias[self.b.ref_frame[0] as usize] {
|
||||
cand = [-cand[0], -cand[1]];
|
||||
}
|
||||
let n = self.mvs.num_mv_found;
|
||||
if !(0..n).any(|k| self.mvs.ref_stack_mv[k][0] == cand) {
|
||||
self.mvs.ref_stack_mv[n][0] = cand;
|
||||
self.mvs.weight_stack[n] = 2;
|
||||
self.mvs.num_mv_found += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn context_and_clamping(&mut self, is_compound: bool, num_new: usize) {
|
||||
let bw = block_width(self.b.mi_size);
|
||||
let bh = block_height(self.b.mi_size);
|
||||
let n = self.mvs.num_mv_found;
|
||||
for idx in 0..n {
|
||||
let mut z = 0;
|
||||
if idx + 1 < n {
|
||||
let w0 = self.mvs.weight_stack[idx];
|
||||
let w1 = self.mvs.weight_stack[idx + 1];
|
||||
if w0 >= REF_CAT_LEVEL as u32 {
|
||||
if w1 < REF_CAT_LEVEL as u32 {
|
||||
z = 1;
|
||||
}
|
||||
} else {
|
||||
z = 2;
|
||||
}
|
||||
}
|
||||
self.mvs.drl_ctx_stack[idx] = z;
|
||||
}
|
||||
for list in 0..1 + is_compound as usize {
|
||||
for idx in 0..n {
|
||||
let mut mv = self.mvs.ref_stack_mv[idx][list];
|
||||
mv[0] = self.clamp_mv_row(mv[0], MV_BORDER as i32 + bh * 8);
|
||||
mv[1] = self.clamp_mv_col(mv[1], MV_BORDER as i32 + bw * 8);
|
||||
self.mvs.ref_stack_mv[idx][list] = mv;
|
||||
}
|
||||
}
|
||||
let (close, total) = (self.mvs.close_matches, self.mvs.total_matches);
|
||||
if close == 0 {
|
||||
self.mvs.new_mv_context = total.min(1);
|
||||
self.mvs.ref_mv_context = total;
|
||||
} else if close == 1 {
|
||||
self.mvs.new_mv_context = 3 - num_new.min(1);
|
||||
self.mvs.ref_mv_context = 2 + total;
|
||||
} else {
|
||||
self.mvs.new_mv_context = 5 - num_new.min(1);
|
||||
self.mvs.ref_mv_context = 5;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn clamp_mv_row(&self, mvec: i32, border: i32) -> i32 {
|
||||
let bh4 = self.b.bh4 as i32;
|
||||
let to_top = -((self.b.mi_row as i32 * 4) * 8);
|
||||
let to_bottom = ((self.fs.fh.mi_rows as i32 - bh4 - self.b.mi_row as i32) * 4) * 8;
|
||||
mvec.clamp(to_top - border, to_bottom + border)
|
||||
}
|
||||
|
||||
pub(crate) fn clamp_mv_col(&self, mvec: i32, border: i32) -> i32 {
|
||||
let bw4 = self.b.bw4 as i32;
|
||||
let to_left = -((self.b.mi_col as i32 * 4) * 8);
|
||||
let to_right = ((self.fs.fh.mi_cols as i32 - bw4 - self.b.mi_col as i32) * 4) * 8;
|
||||
mvec.clamp(to_left - border, to_right + border)
|
||||
}
|
||||
|
||||
/// has_overlappable_candidates( ) (7.10.3)
|
||||
pub(crate) fn has_overlappable_candidates(&self) -> bool {
|
||||
let mi = &self.t.mi;
|
||||
let (mi_rows, mi_cols) = (self.fs.fh.mi_rows as usize, self.fs.fh.mi_cols as usize);
|
||||
if self.b.avail_u {
|
||||
let mut x4 = self.b.mi_col;
|
||||
while x4 < mi_cols.min(self.b.mi_col + self.b.bw4) {
|
||||
let c = (x4 | 1).min(mi_cols - 1);
|
||||
if mi.ref_frame[mi.idx(self.b.mi_row - 1, c)][0] > INTRA_FRAME as i8 {
|
||||
return true;
|
||||
}
|
||||
x4 += 2;
|
||||
}
|
||||
}
|
||||
if self.b.avail_l {
|
||||
let mut y4 = self.b.mi_row;
|
||||
while y4 < mi_rows.min(self.b.mi_row + self.b.bh4) {
|
||||
let r = (y4 | 1).min(mi_rows - 1);
|
||||
if mi.ref_frame[mi.idx(r, self.b.mi_col - 1)][0] > INTRA_FRAME as i8 {
|
||||
return true;
|
||||
}
|
||||
y4 += 2;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Find warp samples process (7.10.4).
|
||||
pub(crate) fn find_warp_samples(&mut self) {
|
||||
self.mvs.num_samples = 0;
|
||||
self.mvs.num_samples_scanned = 0;
|
||||
let w4 = self.b.bw4 as isize;
|
||||
let h4 = self.b.bh4 as isize;
|
||||
let (mi_row, mi_col) = (self.b.mi_row as isize, self.b.mi_col as isize);
|
||||
let (mi_rows, mi_cols) = (self.fs.fh.mi_rows as isize, self.fs.fh.mi_cols as isize);
|
||||
let mut do_top_left = true;
|
||||
let mut do_top_right = true;
|
||||
if self.b.avail_u {
|
||||
let mi = &self.t.mi;
|
||||
let src_w = NUM_4X4_BLOCKS_WIDE[mi.mi_size[mi.idx(mi_row as usize - 1, mi_col as usize)] as usize] as isize;
|
||||
if w4 <= src_w {
|
||||
let col_offset = -(mi_col & (src_w - 1));
|
||||
if col_offset < 0 {
|
||||
do_top_left = false;
|
||||
}
|
||||
if col_offset + src_w > w4 {
|
||||
do_top_right = false;
|
||||
}
|
||||
self.add_sample(-1, 0);
|
||||
} else {
|
||||
let mut i = 0;
|
||||
while i < w4.min(mi_cols - mi_col) {
|
||||
let mi = &self.t.mi;
|
||||
let src_w = NUM_4X4_BLOCKS_WIDE[mi.mi_size[mi.idx(mi_row as usize - 1, (mi_col + i) as usize)] as usize] as isize;
|
||||
let step = w4.min(src_w);
|
||||
self.add_sample(-1, i);
|
||||
i += step;
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.b.avail_l {
|
||||
let mi = &self.t.mi;
|
||||
let src_h = NUM_4X4_BLOCKS_HIGH[mi.mi_size[mi.idx(mi_row as usize, mi_col as usize - 1)] as usize] as isize;
|
||||
if h4 <= src_h {
|
||||
let row_offset = -(mi_row & (src_h - 1));
|
||||
if row_offset < 0 {
|
||||
do_top_left = false;
|
||||
}
|
||||
self.add_sample(0, -1);
|
||||
} else {
|
||||
let mut i = 0;
|
||||
while i < h4.min(mi_rows - mi_row) {
|
||||
let mi = &self.t.mi;
|
||||
let src_h = NUM_4X4_BLOCKS_HIGH[mi.mi_size[mi.idx((mi_row + i) as usize, mi_col as usize - 1)] as usize] as isize;
|
||||
let step = h4.min(src_h);
|
||||
self.add_sample(i, -1);
|
||||
i += step;
|
||||
}
|
||||
}
|
||||
}
|
||||
if do_top_left {
|
||||
self.add_sample(-1, -1);
|
||||
}
|
||||
if do_top_right && w4.max(h4) <= 16 {
|
||||
self.add_sample(-1, w4);
|
||||
}
|
||||
if self.mvs.num_samples == 0 && self.mvs.num_samples_scanned > 0 {
|
||||
self.mvs.num_samples = 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn add_sample(&mut self, delta_row: isize, delta_col: isize) {
|
||||
if self.mvs.num_samples_scanned >= LEAST_SQUARES_SAMPLES_MAX {
|
||||
return;
|
||||
}
|
||||
let mv_row = self.b.mi_row as isize + delta_row;
|
||||
let mv_col = self.b.mi_col as isize + delta_col;
|
||||
if !self.inside(mv_row, mv_col) {
|
||||
return;
|
||||
}
|
||||
let mi = &self.t.mi;
|
||||
let i = mi.idx(mv_row as usize, mv_col as usize);
|
||||
if !mi.written[i] {
|
||||
return;
|
||||
}
|
||||
if mi.ref_frame[i][0] != self.b.ref_frame[0] || mi.ref_frame[i][1] != -1 {
|
||||
return;
|
||||
}
|
||||
let cand_sz = mi.mi_size[i] as usize;
|
||||
let cand_w4 = NUM_4X4_BLOCKS_WIDE[cand_sz] as isize;
|
||||
let cand_h4 = NUM_4X4_BLOCKS_HIGH[cand_sz] as isize;
|
||||
let cand_row = mv_row & !(cand_h4 - 1);
|
||||
let cand_col = mv_col & !(cand_w4 - 1);
|
||||
let mid_y = cand_row * 4 + cand_h4 * 2 - 1;
|
||||
let mid_x = cand_col * 4 + cand_w4 * 2 - 1;
|
||||
let threshold = block_width(self.b.mi_size).max(block_height(self.b.mi_size)).clamp(16, 112);
|
||||
let cm = mi.mv[mi.idx(cand_row as usize, cand_col as usize)][0];
|
||||
let mv_diff_row = (cm.row as i32 - self.b.mv_i[0][0]).abs();
|
||||
let mv_diff_col = (cm.col as i32 - self.b.mv_i[0][1]).abs();
|
||||
let valid = mv_diff_row + mv_diff_col <= threshold;
|
||||
let cand = [mid_y as i32 * 8, mid_x as i32 * 8, mid_y as i32 * 8 + cm.row as i32, mid_x as i32 * 8 + cm.col as i32];
|
||||
self.mvs.num_samples_scanned += 1;
|
||||
if !valid && self.mvs.num_samples_scanned > 1 {
|
||||
return;
|
||||
}
|
||||
self.mvs.cand_list[self.mvs.num_samples] = cand;
|
||||
if valid {
|
||||
self.mvs.num_samples += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn inside(&self, r: isize, c: isize) -> bool {
|
||||
c >= self.mi_col_start as isize && c < self.mi_col_end as isize && r >= self.mi_row_start as isize && r < self.mi_row_end as isize
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
//! Optional data parallelism (rayon behind the `threads` feature; sequential otherwise and on
|
||||
//! wasm32).
|
||||
|
||||
#[cfg(feature = "threads")]
|
||||
use std::sync::Arc;
|
||||
|
||||
/// A handle to the decoder's worker pool (or none: run on the calling thread).
|
||||
#[derive(Clone, Default)]
|
||||
pub(crate) struct Pool {
|
||||
#[cfg(feature = "threads")]
|
||||
pool: Option<Arc<rayon::ThreadPool>>,
|
||||
}
|
||||
|
||||
impl Pool {
|
||||
/// A pool of `threads` workers (sequential when `threads` <= 1, without the `threads`
|
||||
/// feature, or on wasm32).
|
||||
pub fn new(threads: usize) -> Pool {
|
||||
#[cfg(feature = "threads")]
|
||||
{
|
||||
let pool = if threads > 1 && cfg!(not(target_arch = "wasm32")) {
|
||||
rayon::ThreadPoolBuilder::new().num_threads(threads).thread_name(|i| format!("av1-{i}")).build().ok().map(Arc::new)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Pool { pool }
|
||||
}
|
||||
#[cfg(not(feature = "threads"))]
|
||||
{
|
||||
let _ = threads;
|
||||
Pool {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_parallel(&self) -> bool {
|
||||
#[cfg(feature = "threads")]
|
||||
{
|
||||
self.pool.is_some()
|
||||
}
|
||||
#[cfg(not(feature = "threads"))]
|
||||
{
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// `f(i, &mut items[i])` for every item, in parallel when possible; results in order.
|
||||
pub fn map_mut<T: Send, R: Send>(&self, items: &mut [T], f: impl Fn(usize, &mut T) -> R + Sync + Send) -> Vec<R> {
|
||||
#[cfg(feature = "threads")]
|
||||
if let Some(pool) = &self.pool
|
||||
&& items.len() > 1
|
||||
{
|
||||
use rayon::prelude::*;
|
||||
return pool.install(|| items.par_iter_mut().enumerate().map(|(i, t)| f(i, t)).collect());
|
||||
}
|
||||
items.iter_mut().enumerate().map(|(i, t)| f(i, t)).collect()
|
||||
}
|
||||
|
||||
/// `f(i, chunk)` for the consecutive `chunk_len`-element chunks of `data`.
|
||||
pub fn chunks<T: Send>(&self, data: &mut [T], chunk_len: usize, f: impl Fn(usize, &mut [T]) + Sync + Send) {
|
||||
let chunk_len = chunk_len.max(1);
|
||||
#[cfg(feature = "threads")]
|
||||
if let Some(pool) = &self.pool
|
||||
&& data.len() > chunk_len
|
||||
{
|
||||
use rayon::prelude::*;
|
||||
pool.install(|| data.par_chunks_mut(chunk_len).enumerate().for_each(|(i, c)| f(i, c)));
|
||||
return;
|
||||
}
|
||||
for (i, c) in data.chunks_mut(chunk_len).enumerate() {
|
||||
f(i, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,269 @@
|
||||
//! In-loop post filters run by decode_frame_wrapup: loop filter (7.14), CDEF (7.15),
|
||||
//! super-resolution upscaling (7.16) and loop restoration (7.17).
|
||||
|
||||
use crate::spec_tables::{
|
||||
GLOBAL_GLOBALMV, GLOBALMV, INTRA_FRAME, MAX_LOOP_FILTER, NEARESTMV, NUM_4X4_BLOCKS_HIGH, NUM_4X4_BLOCKS_WIDE, SEG_LVL_ALT_LF_Y_V, TX_HEIGHT,
|
||||
TX_WIDTH,
|
||||
};
|
||||
use crate::state::FrameState;
|
||||
use crate::stats::{DecodeStats, Stage, Timer};
|
||||
|
||||
/// The post-filters run on the frame worker; `_pool` is for row-parallel filtering (not yet used).
|
||||
/// `draft`: only super-resolution (which sets the output size) runs.
|
||||
pub(crate) fn apply(fs: &mut FrameState, stats: &mut DecodeStats, _pool: &crate::par::Pool, draft: bool) {
|
||||
let mut t = Timer::start();
|
||||
let lvl = fs.fh.lf.level;
|
||||
if (lvl[0] != 0 || lvl[1] != 0) && !draft {
|
||||
loop_filter(fs);
|
||||
}
|
||||
stats.add(Stage::LoopFilter, t.lap());
|
||||
let cdef = if draft { None } else { crate::cdef::apply(fs) };
|
||||
stats.add(Stage::Cdef, t.lap());
|
||||
let (up_cur, up_cdef) = if fs.fh.use_superres {
|
||||
let c = crate::restoration::upscale(fs, &fs.cur);
|
||||
let d = cdef.as_ref().map(|f| crate::restoration::upscale(fs, f));
|
||||
(c, d)
|
||||
} else {
|
||||
(std::mem::take(&mut fs.cur), cdef)
|
||||
};
|
||||
stats.add(Stage::Superres, t.lap());
|
||||
fs.cur = if fs.fh.lr.uses_lr && !draft {
|
||||
crate::restoration::loop_restoration(fs, &up_cur, up_cdef.as_ref().unwrap_or(&up_cur))
|
||||
} else {
|
||||
up_cdef.unwrap_or(up_cur)
|
||||
};
|
||||
stats.add(Stage::Restoration, t.lap());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Loop filter (7.14)
|
||||
|
||||
fn loop_filter(fs: &mut FrameState) {
|
||||
let (mi_rows, mi_cols) = (fs.fh.mi_rows as usize, fs.fh.mi_cols as usize);
|
||||
for plane in 0..fs.num_planes {
|
||||
if plane == 0 || fs.fh.lf.level[1 + plane] != 0 {
|
||||
for pass in 0..2 {
|
||||
let row_step = if plane == 0 { 1 } else { 1 << fs.ssy };
|
||||
let col_step = if plane == 0 { 1 } else { 1 << fs.ssx };
|
||||
let mut row = 0;
|
||||
while row < mi_rows {
|
||||
let mut col = 0;
|
||||
while col < mi_cols {
|
||||
edge(fs, plane, pass, row, col);
|
||||
col += col_step;
|
||||
}
|
||||
row += row_step;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn edge(fs: &mut FrameState, plane: usize, pass: usize, row: usize, col: usize) {
|
||||
let (sub_x, sub_y) = if plane == 0 { (0, 0) } else { (fs.ssx, fs.ssy) };
|
||||
let (dx, dy) = if pass == 0 { (1i32, 0i32) } else { (0, 1) };
|
||||
let x = col * 4;
|
||||
let y = row * 4;
|
||||
let row = row | sub_y;
|
||||
let col = col | sub_x;
|
||||
let on_screen = !(x >= fs.fh.frame_width as usize || y >= fs.fh.frame_height as usize || (pass == 0 && x == 0) || (pass == 1 && y == 0));
|
||||
if !on_screen {
|
||||
return;
|
||||
}
|
||||
let xp = x >> sub_x;
|
||||
let yp = y >> sub_y;
|
||||
let prev_row = row - ((dy as usize) << sub_y);
|
||||
let prev_col = col - ((dx as usize) << sub_x);
|
||||
let tx_sz = fs.lf_tx_size(plane, row >> sub_y, col >> sub_x);
|
||||
let is_tx_edge = if pass == 0 { xp.is_multiple_of(TX_WIDTH[tx_sz] as usize) } else { yp.is_multiple_of(TX_HEIGHT[tx_sz] as usize) };
|
||||
if !is_tx_edge {
|
||||
return;
|
||||
}
|
||||
let mi = &fs.mi;
|
||||
let i = mi.idx(row, col);
|
||||
let mi_size = mi.mi_size[i] as usize;
|
||||
let plane_size = fs.plane_residual_size(mi_size, plane);
|
||||
let skip = mi.skip[i];
|
||||
let is_intra = mi.ref_frame[i][0] <= INTRA_FRAME as i8;
|
||||
let is_block_edge = if pass == 0 {
|
||||
xp.is_multiple_of(4 * NUM_4X4_BLOCKS_WIDE[plane_size] as usize)
|
||||
} else {
|
||||
yp.is_multiple_of(4 * NUM_4X4_BLOCKS_HIGH[plane_size] as usize)
|
||||
};
|
||||
if !(is_block_edge || !skip || is_intra) {
|
||||
return;
|
||||
}
|
||||
let prev_tx_sz = fs.lf_tx_size(plane, prev_row >> sub_y, prev_col >> sub_x);
|
||||
// filter size (7.14.3)
|
||||
let base_size = if pass == 0 { TX_WIDTH[prev_tx_sz].min(TX_WIDTH[tx_sz]) } else { TX_HEIGHT[prev_tx_sz].min(TX_HEIGHT[tx_sz]) } as usize;
|
||||
let filter_size = if plane == 0 { 16.min(base_size) } else { 8.min(base_size) };
|
||||
let (mut lvl, mut limit, mut blimit, mut thresh) = strength(fs, row, col, plane, pass);
|
||||
if lvl == 0 {
|
||||
(lvl, limit, blimit, thresh) = strength(fs, prev_row, prev_col, plane, pass);
|
||||
}
|
||||
if lvl == 0 {
|
||||
return;
|
||||
}
|
||||
let t = LfThresh::new(limit, blimit, thresh, fs.bit_depth);
|
||||
let pl = &mut fs.cur.planes[plane];
|
||||
let stride = pl.stride;
|
||||
let (step, along) = if pass == 0 { (1, stride) } else { (stride, 1) };
|
||||
let base = yp * stride + xp;
|
||||
let chroma = plane > 0;
|
||||
for k in 0..4 {
|
||||
sample_filter(&mut pl.data, base + k * along, step, chroma, filter_size, t);
|
||||
}
|
||||
}
|
||||
|
||||
/// Adaptive filter strength (7.14.4): (lvl, limit, blimit, thresh).
|
||||
fn strength(fs: &FrameState, row: usize, col: usize, plane: usize, pass: usize) -> (i32, i32, i32, i32) {
|
||||
let mi = &fs.mi;
|
||||
let i = mi.idx(row, col);
|
||||
let segment = mi.segment_id[i] as usize;
|
||||
let rf = mi.ref_frame[i][0];
|
||||
let mode = mi.y_mode[i] as usize;
|
||||
let mode_type = (mode >= NEARESTMV && mode != GLOBALMV && mode != GLOBAL_GLOBALMV) as usize;
|
||||
let delta_lf = if fs.fh.delta_lf_multi { mi.delta_lf[i][if plane == 0 { pass } else { plane + 1 }] } else { mi.delta_lf[i][0] } as i32;
|
||||
// 7.14.5
|
||||
let idx = if plane == 0 { pass } else { plane + 1 };
|
||||
let base = (delta_lf + fs.fh.lf.level[idx] as i32).clamp(0, MAX_LOOP_FILTER as i32);
|
||||
let mut lvl_seg = base;
|
||||
let feature = SEG_LVL_ALT_LF_Y_V + idx;
|
||||
if fs.fh.seg.enabled && fs.fh.seg.features.enabled[segment][feature] {
|
||||
lvl_seg = (fs.fh.seg.features.data[segment][feature] + lvl_seg).clamp(0, MAX_LOOP_FILTER as i32);
|
||||
}
|
||||
if fs.fh.lf.delta_enabled {
|
||||
let n_shift = lvl_seg >> 5;
|
||||
let d = &fs.fh.lf.deltas;
|
||||
if rf == INTRA_FRAME as i8 {
|
||||
lvl_seg += d.ref_deltas[INTRA_FRAME] << n_shift;
|
||||
} else if rf > INTRA_FRAME as i8 {
|
||||
lvl_seg += (d.ref_deltas[rf as usize] << n_shift) + (d.mode_deltas[mode_type] << n_shift);
|
||||
}
|
||||
lvl_seg = lvl_seg.clamp(0, MAX_LOOP_FILTER as i32);
|
||||
}
|
||||
let lvl = lvl_seg;
|
||||
let sharp = fs.fh.lf.sharpness as i32;
|
||||
let shift = if sharp > 4 {
|
||||
2
|
||||
} else if sharp > 0 {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let limit = if sharp > 0 { (lvl >> shift).clamp(1, 9 - sharp) } else { (lvl >> shift).max(1) };
|
||||
let blimit = 2 * (lvl + 2) + limit;
|
||||
let thresh = lvl >> 4;
|
||||
(lvl, limit, blimit, thresh)
|
||||
}
|
||||
|
||||
/// Per-edge thresholds of the sample filter, scaled to the bit depth.
|
||||
#[derive(Clone, Copy)]
|
||||
struct LfThresh {
|
||||
limit: i32,
|
||||
blimit: i32,
|
||||
thresh: i32,
|
||||
/// Flatness threshold 1 << (BitDepth - 8).
|
||||
flat: i32,
|
||||
/// 0x80 << (BitDepth - 8) and 1 << (BitDepth - 1).
|
||||
off: i32,
|
||||
half: i32,
|
||||
}
|
||||
|
||||
impl LfThresh {
|
||||
fn new(limit: i32, blimit: i32, thresh: i32, bd: u32) -> LfThresh {
|
||||
let sh = bd - 8;
|
||||
LfThresh { limit: limit << sh, blimit: blimit << sh, thresh: thresh << sh, flat: 1 << sh, off: 0x80 << sh, half: 1 << (bd - 1) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Sample filtering process (7.14.6) for one line across the edge: `base` indexes q0 and
|
||||
/// `step` is the distance between samples across the edge.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[inline(always)]
|
||||
fn sample_filter(d: &mut [u16], base: usize, step: usize, chroma: bool, filter_size: usize, t: LfThresh) {
|
||||
let at = |d: &[u16], k: isize| -> i32 { d[(base as isize + k * step as isize) as usize] as i32 };
|
||||
let (q0, q1, p0, p1) = (at(d, 0), at(d, 1), at(d, -1), at(d, -2));
|
||||
let lim = t.limit;
|
||||
// filter mask (7.14.6.2)
|
||||
if (p1 - p0).abs() > lim || (q1 - q0).abs() > lim || (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 > t.blimit {
|
||||
return;
|
||||
}
|
||||
let hev = (p1 - p0).abs() > t.thresh || (q1 - q0).abs() > t.thresh;
|
||||
let mut flat = false;
|
||||
if filter_size > 4 {
|
||||
let (q2, p2) = (at(d, 2), at(d, -3));
|
||||
if (p2 - p1).abs() > lim || (q2 - q1).abs() > lim {
|
||||
return;
|
||||
}
|
||||
let tf = t.flat;
|
||||
flat = (p1 - p0).abs() <= tf && (q1 - q0).abs() <= tf && (p2 - p0).abs() <= tf && (q2 - q0).abs() <= tf;
|
||||
if !chroma {
|
||||
// filterLen >= 8
|
||||
let (q3, p3) = (at(d, 3), at(d, -4));
|
||||
if (p3 - p2).abs() > lim || (q3 - q2).abs() > lim {
|
||||
return;
|
||||
}
|
||||
flat = flat && (p3 - p0).abs() <= tf && (q3 - q0).abs() <= tf;
|
||||
}
|
||||
}
|
||||
if !flat {
|
||||
// narrow filter (7.14.6.3)
|
||||
let c = |v: i32| v.clamp(-t.half, t.half - 1);
|
||||
let off = t.off;
|
||||
let (ps1, ps0, qs0, qs1) = (p1 - off, p0 - off, q0 - off, q1 - off);
|
||||
let mut filter = if hev { c(ps1 - qs1) } else { 0 };
|
||||
filter = c(filter + 3 * (qs0 - ps0));
|
||||
let filter1 = c(filter + 4) >> 3;
|
||||
let filter2 = c(filter + 3) >> 3;
|
||||
d[base] = (c(qs0 - filter1) + off) as u16;
|
||||
d[base - step] = (c(ps0 + filter2) + off) as u16;
|
||||
if !hev {
|
||||
let f = (filter1 + 1) >> 1;
|
||||
d[base + step] = (c(qs1 - f) + off) as u16;
|
||||
d[base - 2 * step] = (c(ps1 + f) + off) as u16;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if chroma {
|
||||
wide::<2, 1, 3>(d, base, step);
|
||||
} else if filter_size == 8 {
|
||||
wide::<3, 0, 3>(d, base, step);
|
||||
} else {
|
||||
let tf = t.flat;
|
||||
let q0 = at(d, 0);
|
||||
let p0 = at(d, -1);
|
||||
let flat2 = (4..7).all(|k| (at(d, k) - q0).abs() <= tf && (at(d, -k - 1) - p0).abs() <= tf);
|
||||
if flat2 { wide::<6, 1, 4>(d, base, step) } else { wide::<3, 0, 3>(d, base, step) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Wide filter process (7.14.6.4) with n = N, the 2-weight taps |j| <= N2 and log2Size = LOG2.
|
||||
#[inline(always)]
|
||||
fn wide<const N: usize, const N2: usize, const LOG2: u32>(d: &mut [u16], base: usize, step: usize) {
|
||||
// v[k] = sample at offset k - (N + 1) from q0, k in 0 .. 2N + 2
|
||||
let mut v = [0i32; 14];
|
||||
let first = base - (N + 1) * step;
|
||||
for (k, x) in v[..2 * N + 2].iter_mut().enumerate() {
|
||||
*x = d[first + k * step] as i32;
|
||||
}
|
||||
// e[m] = v[clamp(m - N, 0, 2N + 1)]: the clamped tap positions as one array, so
|
||||
// F[i] = sum_{jj = 0}^{2N} e[i + 1 + jj] * (2 if |jj - N| <= N2 else 1), a sliding window.
|
||||
let mut e = [0i32; 25];
|
||||
for (m, x) in e[..4 * N + 1].iter_mut().enumerate() {
|
||||
*x = v[m.saturating_sub(N).min(2 * N + 1)];
|
||||
}
|
||||
let mut f = [0i32; 12];
|
||||
let mut win: i32 = e[1..2 * N + 2].iter().sum();
|
||||
for i in 0..2 * N {
|
||||
if i > 0 {
|
||||
win += e[i + 1 + 2 * N] - e[i];
|
||||
}
|
||||
let centre: i32 = e[i + 1 + N - N2..=i + 1 + N + N2].iter().sum();
|
||||
f[i] = (win + centre + (1 << (LOG2 - 1))) >> LOG2;
|
||||
}
|
||||
let out = base - N * step;
|
||||
for i in 0..2 * N {
|
||||
d[out + i * step] = f[i] as u16;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,298 @@
|
||||
//! Super-resolution upscaling (7.16) and loop restoration (7.17).
|
||||
|
||||
use crate::frame::{FrameBuf, Plane};
|
||||
use crate::spec_tables::{
|
||||
FILTER_BITS, RESTORE_NONE, RESTORE_SGRPROJ, RESTORE_WIENER, SGR_PARAMS, SGRPROJ_MTABLE_BITS, SGRPROJ_PRJ_BITS, SGRPROJ_RECIP_BITS,
|
||||
SGRPROJ_RST_BITS, SGRPROJ_SGR_BITS, SUPERRES_EXTRA_BITS, SUPERRES_FILTER_OFFSET, SUPERRES_FILTER_TAPS, SUPERRES_SCALE_BITS, SUPERRES_SCALE_MASK,
|
||||
UPSCALE_FILTER,
|
||||
};
|
||||
use crate::state::FrameState;
|
||||
|
||||
/// Upscale a frame horizontally from FrameWidth to UpscaledWidth (7.16).
|
||||
pub(crate) fn upscale(fs: &FrameState, input: &FrameBuf) -> FrameBuf {
|
||||
let fh = &fs.fh;
|
||||
let up_w = fh.upscaled_width as usize;
|
||||
let mut out = FrameBuf::new(up_w, fh.frame_height as usize, fs.num_planes, fs.ssx, fs.ssy, fs.bit_depth as u8);
|
||||
let max = (1i32 << fs.bit_depth) - 1;
|
||||
for plane in 0..fs.num_planes {
|
||||
let (sub_x, sub_y) = if plane > 0 { (fs.ssx, fs.ssy) } else { (0, 0) };
|
||||
let down_w = ((fh.frame_width as usize) + sub_x) >> sub_x;
|
||||
let up_pw = (up_w + sub_x) >> sub_x;
|
||||
let plane_h = ((fh.frame_height as usize) + sub_y) >> sub_y;
|
||||
let sb = SUPERRES_SCALE_BITS as i64;
|
||||
let step_x = (((down_w as i64) << sb) + (up_pw as i64 / 2)) / up_pw as i64;
|
||||
let err = up_pw as i64 * step_x - ((down_w as i64) << sb);
|
||||
let mut initial = (-(((up_pw - down_w) as i64) << (sb - 1)) + up_pw as i64 / 2) / up_pw as i64 + (1 << (SUPERRES_EXTRA_BITS - 1)) - err / 2;
|
||||
initial &= SUPERRES_SCALE_MASK as i64;
|
||||
let mi_w = fh.mi_cols as usize >> sub_x;
|
||||
let max_x = (mi_w * 4 - 1) as i64;
|
||||
let src = &input.planes[plane];
|
||||
let dst = &mut out.planes[plane];
|
||||
for y in 0..plane_h {
|
||||
let row = src.row(y);
|
||||
for x in 0..up_pw {
|
||||
let src_x = -(1i64 << sb) + initial + x as i64 * step_x;
|
||||
let src_px = src_x >> sb;
|
||||
let subpel = ((src_x & SUPERRES_SCALE_MASK as i64) >> SUPERRES_EXTRA_BITS) as usize;
|
||||
let mut sum = 0i32;
|
||||
for k in 0..SUPERRES_FILTER_TAPS {
|
||||
let sx = (src_px + k as i64 - SUPERRES_FILTER_OFFSET as i64).clamp(0, max_x) as usize;
|
||||
sum += row[sx] as i32 * UPSCALE_FILTER[subpel][k] as i32;
|
||||
}
|
||||
dst.set(x, y, ((sum + 64) >> FILTER_BITS).clamp(0, max) as u16);
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
struct LrCtx<'a> {
|
||||
cur: &'a Plane,
|
||||
cdef: &'a Plane,
|
||||
plane_end_x: i32,
|
||||
plane_end_y: i32,
|
||||
stripe_start_y: i32,
|
||||
stripe_end_y: i32,
|
||||
}
|
||||
|
||||
impl LrCtx<'_> {
|
||||
/// Get source sample process (7.17.6).
|
||||
#[inline(always)]
|
||||
fn sample(&self, x: i32, y: i32) -> i32 {
|
||||
let x = x.min(self.plane_end_x).max(0) as usize;
|
||||
let y = y.min(self.plane_end_y).max(0);
|
||||
if y < self.stripe_start_y {
|
||||
let y = (self.stripe_start_y - 2).max(y) as usize;
|
||||
self.cur.at(x, y) as i32
|
||||
} else if y > self.stripe_end_y {
|
||||
let y = (self.stripe_end_y + 2).min(y) as usize;
|
||||
self.cur.at(x, y) as i32
|
||||
} else {
|
||||
self.cdef.at(x, y as usize) as i32
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Position and size of a (at most 4x4) restoration block within its plane.
|
||||
#[derive(Clone, Copy)]
|
||||
struct LrBlock {
|
||||
x: i32,
|
||||
y: i32,
|
||||
w: i32,
|
||||
h: i32,
|
||||
}
|
||||
|
||||
/// Loop restoration (7.17): returns LrFrame given UpscaledCurrFrame and UpscaledCdefFrame.
|
||||
pub(crate) fn loop_restoration(fs: &FrameState, cur: &FrameBuf, cdef: &FrameBuf) -> FrameBuf {
|
||||
let mut lr = cdef.clone();
|
||||
for plane in 0..fs.num_planes {
|
||||
if fs.fh.lr.frame_restoration_type[plane] == RESTORE_NONE as u8 {
|
||||
continue;
|
||||
}
|
||||
restore_plane(fs, cur, cdef, &mut lr.planes[plane], plane);
|
||||
}
|
||||
lr
|
||||
}
|
||||
|
||||
fn restore_plane(fs: &FrameState, cur: &FrameBuf, cdef: &FrameBuf, out: &mut Plane, plane: usize) {
|
||||
let fh = &fs.fh;
|
||||
let up_w = fh.upscaled_width as i32;
|
||||
let fr_h = fh.frame_height as i32;
|
||||
let bd = fs.bit_depth;
|
||||
let (sub_x, sub_y) = if plane > 0 { (fs.ssx as i32, fs.ssy as i32) } else { (0, 0) };
|
||||
let unit_size = fh.lr.loop_restoration_size[plane] as i32;
|
||||
let unit_rows = fs.lr_unit_rows[plane] as i32;
|
||||
let unit_cols = fs.lr_unit_cols[plane] as i32;
|
||||
let plane_end_x = ((up_w + sub_x) >> sub_x) - 1;
|
||||
let plane_end_y = ((fr_h + sub_y) >> sub_y) - 1;
|
||||
let mut row = 0;
|
||||
while row * 4 < fr_h {
|
||||
let luma_y = row * 4;
|
||||
let stripe_num = (luma_y + 8) / 64;
|
||||
let stripe_start_y = (-8 + stripe_num * 64) >> sub_y;
|
||||
let stripe_end_y = stripe_start_y + (64 >> sub_y) - 1;
|
||||
let ctx = LrCtx { cur: &cur.planes[plane], cdef: &cdef.planes[plane], plane_end_x, plane_end_y, stripe_start_y, stripe_end_y };
|
||||
let unit_row = (unit_rows - 1).min(((row * 4 + 8) >> sub_y) / unit_size);
|
||||
let mut col = 0;
|
||||
while col * 4 < up_w {
|
||||
let unit_col = (unit_cols - 1).min(((col * 4) >> sub_x) / unit_size);
|
||||
let x = (col * 4) >> sub_x;
|
||||
let y = (row * 4) >> sub_y;
|
||||
let w = (4 >> sub_x).min(plane_end_x - x + 1);
|
||||
let h = (4 >> sub_y).min(plane_end_y - y + 1);
|
||||
if w > 0 && h > 0 {
|
||||
let ui = (unit_row * unit_cols + unit_col) as usize;
|
||||
let blk = LrBlock { x, y, w, h };
|
||||
match fs.lr.lr_type[plane][ui] {
|
||||
t if t == RESTORE_WIENER as u8 => wiener(fs, &ctx, out, (plane, ui), blk, bd),
|
||||
t if t == RESTORE_SGRPROJ as u8 => self_guided(fs, &ctx, out, (plane, ui), blk, bd),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
col += 1;
|
||||
}
|
||||
row += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn wiener(fs: &FrameState, ctx: &LrCtx, out: &mut Plane, (plane, ui): (usize, usize), blk: LrBlock, bd: u32) {
|
||||
let LrBlock { x, y, w, h } = blk;
|
||||
let round0: u32 = if bd == 12 { 5 } else { 3 };
|
||||
let round1: u32 = if bd == 12 { 9 } else { 11 };
|
||||
let coef = &fs.lr.lr_wiener[plane][ui];
|
||||
let mk = |c: &[i8; 3]| -> [i32; 7] {
|
||||
let mut f = [0i32; 7];
|
||||
f[3] = 128;
|
||||
for i in 0..3 {
|
||||
f[i] = c[i] as i32;
|
||||
f[6 - i] = c[i] as i32;
|
||||
f[3] -= 2 * c[i] as i32;
|
||||
}
|
||||
f
|
||||
};
|
||||
let vfilter = mk(&coef[0]);
|
||||
let hfilter = mk(&coef[1]);
|
||||
let fb = FILTER_BITS as u32;
|
||||
let offset = 1i32 << (bd + fb - round0 - 1);
|
||||
let limit = (1i32 << (bd + 1 + fb - round0)) - 1;
|
||||
let mut inter = [[0i32; 4]; 10];
|
||||
for r in 0..(h + 6) as usize {
|
||||
for c in 0..w as usize {
|
||||
let mut s = 0;
|
||||
for t in 0..7 {
|
||||
s += hfilter[t] * ctx.sample(x + c as i32 + t as i32 - 3, y + r as i32 - 3);
|
||||
}
|
||||
let v = (s + (1 << (round0 - 1))) >> round0;
|
||||
inter[r][c] = v.clamp(-offset, limit - offset);
|
||||
}
|
||||
}
|
||||
let max = (1i32 << bd) - 1;
|
||||
for r in 0..h as usize {
|
||||
for c in 0..w as usize {
|
||||
let mut s = 0;
|
||||
for t in 0..7 {
|
||||
s += vfilter[t] * inter[r + t][c];
|
||||
}
|
||||
let v = (s + (1 << (round1 - 1))) >> round1;
|
||||
out.set(x as usize + c, y as usize + r, v.clamp(0, max) as u16);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn self_guided(fs: &FrameState, ctx: &LrCtx, out: &mut Plane, (plane, ui): (usize, usize), blk: LrBlock, bd: u32) {
|
||||
let LrBlock { x, y, w, h } = blk;
|
||||
let set = fs.lr.lr_sgr_set[plane][ui] as usize;
|
||||
let mut flt0 = [[0i32; 4]; 4];
|
||||
let mut flt1 = [[0i32; 4]; 4];
|
||||
box_filter(ctx, blk, (set, 0), bd, &mut flt0);
|
||||
box_filter(ctx, blk, (set, 1), bd, &mut flt1);
|
||||
let xqd = fs.lr.lr_sgr_xqd[plane][ui];
|
||||
let w0 = xqd[0] as i32;
|
||||
let w1 = xqd[1] as i32;
|
||||
let w2 = (1 << SGRPROJ_PRJ_BITS) - w0 - w1;
|
||||
let r0 = SGR_PARAMS[set][0];
|
||||
let r1 = SGR_PARAMS[set][2];
|
||||
let max = (1i32 << bd) - 1;
|
||||
let sh = (SGRPROJ_RST_BITS + SGRPROJ_PRJ_BITS) as u32;
|
||||
for i in 0..h as usize {
|
||||
for j in 0..w as usize {
|
||||
let u = (ctx.cdef.at(x as usize + j, y as usize + i) as i32) << SGRPROJ_RST_BITS;
|
||||
let mut v = w1 * u;
|
||||
v += if r0 != 0 { w0 * flt0[i][j] } else { w0 * u };
|
||||
v += if r1 != 0 { w2 * flt1[i][j] } else { w2 * u };
|
||||
let s = (v + (1 << (sh - 1))) >> sh;
|
||||
out.set(x as usize + j, y as usize + i, s.clamp(0, max) as u16);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn round2_64(x: i64, n: u32) -> i64 {
|
||||
if n == 0 { x } else { (x + (1i64 << (n - 1))) >> n }
|
||||
}
|
||||
|
||||
/// Box sums of the samples within `r` of (x, y): (sum of squares, sum).
|
||||
fn box_sums(ctx: &LrCtx, x: i32, y: i32, r: i32) -> (i64, i64) {
|
||||
let mut a = 0i64;
|
||||
let mut b = 0i64;
|
||||
for dy in -r..=r {
|
||||
for dx in -r..=r {
|
||||
let c = ctx.sample(x + dx, y + dy) as i64;
|
||||
a += c * c;
|
||||
b += c;
|
||||
}
|
||||
}
|
||||
(a, b)
|
||||
}
|
||||
|
||||
/// Self-guided filter constants of one pass.
|
||||
struct SgrConsts {
|
||||
n: i64,
|
||||
s: i64,
|
||||
one_over_n: i64,
|
||||
}
|
||||
|
||||
/// (A, B) coefficients of one sample from its box sums.
|
||||
fn sgr_coefficients(a: i64, b: i64, k: &SgrConsts, bd: u32) -> (i64, i64) {
|
||||
let a = round2_64(a, 2 * (bd - 8));
|
||||
let d = round2_64(b, bd - 8);
|
||||
let p = (a * k.n - d * d).max(0);
|
||||
let z = round2_64(p * k.s, SGRPROJ_MTABLE_BITS as u32);
|
||||
let a2 = if z >= 255 {
|
||||
256
|
||||
} else if z == 0 {
|
||||
1
|
||||
} else {
|
||||
((z << SGRPROJ_SGR_BITS) + z / 2) / (z + 1)
|
||||
};
|
||||
let b2 = ((1i64 << SGRPROJ_SGR_BITS) - a2) * b * k.one_over_n;
|
||||
(a2, round2_64(b2, SGRPROJ_RECIP_BITS as u32))
|
||||
}
|
||||
|
||||
fn box_weight(pass: usize, i: i32, dx: i32, dy: i32) -> i64 {
|
||||
if pass == 0 {
|
||||
if ((i + dy) & 1) != 0 { if dx == 0 { 6 } else { 5 } } else { 0 }
|
||||
} else if dx == 0 || dy == 0 {
|
||||
4
|
||||
} else {
|
||||
3
|
||||
}
|
||||
}
|
||||
|
||||
fn box_filter(ctx: &LrCtx, blk: LrBlock, (set, pass): (usize, usize), bd: u32, f: &mut [[i32; 4]; 4]) {
|
||||
let LrBlock { x, y, w, h } = blk;
|
||||
let r = SGR_PARAMS[set][pass * 2] as i32;
|
||||
if r == 0 {
|
||||
return;
|
||||
}
|
||||
let eps = SGR_PARAMS[set][pass * 2 + 1] as i64;
|
||||
let n = ((2 * r + 1) * (2 * r + 1)) as i64;
|
||||
let n2e = n * n * eps;
|
||||
let k = SgrConsts { n, s: ((1i64 << SGRPROJ_MTABLE_BITS) + n2e / 2) / n2e, one_over_n: ((1i64 << SGRPROJ_RECIP_BITS) + n / 2) / n };
|
||||
let mut a_arr = [[0i64; 6]; 6];
|
||||
let mut b_arr = [[0i64; 6]; 6];
|
||||
for i in -1..h + 1 {
|
||||
for j in -1..w + 1 {
|
||||
let (a, b) = box_sums(ctx, x + j, y + i, r);
|
||||
let (a2, b2) = sgr_coefficients(a, b, &k, bd);
|
||||
a_arr[(i + 1) as usize][(j + 1) as usize] = a2;
|
||||
b_arr[(i + 1) as usize][(j + 1) as usize] = b2;
|
||||
}
|
||||
}
|
||||
for i in 0..h {
|
||||
let shift = if pass == 0 && (i & 1) == 1 { 4 } else { 5 };
|
||||
for j in 0..w {
|
||||
let mut a = 0i64;
|
||||
let mut b = 0i64;
|
||||
for dy in -1..=1i32 {
|
||||
for dx in -1..=1i32 {
|
||||
let weight = box_weight(pass, i, dx, dy);
|
||||
a += weight * a_arr[(i + dy + 1) as usize][(j + dx + 1) as usize];
|
||||
b += weight * b_arr[(i + dy + 1) as usize][(j + dx + 1) as usize];
|
||||
}
|
||||
}
|
||||
let v = a * ctx.cdef.at((x + j) as usize, (y + i) as usize) as i64 + b;
|
||||
f[i as usize][j as usize] = round2_64(v, (SGRPROJ_SGR_BITS + shift - SGRPROJ_RST_BITS) as u32) as i32;
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,294 @@
|
||||
//! Per-frame decoding state shared by the tiles and the post-filters.
|
||||
//!
|
||||
//! [`FrameShared`] holds what tiles only read (headers, references, projected motion field);
|
||||
//! [`TileState`] what a tile writes (reconstruction, mode info, per-block filter parameters,
|
||||
//! entropy contexts). With one tile the frame's own buffers move into the tile state and back;
|
||||
//! with several, each tile decodes into private buffers covering its area (tiles never read
|
||||
//! outside themselves), which are then copied into the frame.
|
||||
|
||||
use crate::decoder::RefFrame;
|
||||
use crate::frame::{FrameBuf, MiInfo};
|
||||
use crate::header::{FrameHeader, RefInfo, SequenceHeader};
|
||||
use crate::spec_tables::{DEFAULT_INTRA_FRAME_Y_MODE_CDF, NUM_REF_FRAMES, RESTORE_NONE, SUBSAMPLED_SIZE};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Read-only (during tile decoding) frame state.
|
||||
pub(crate) struct FrameShared {
|
||||
pub seq: SequenceHeader,
|
||||
pub fh: FrameHeader,
|
||||
pub num_planes: usize,
|
||||
pub ssx: usize,
|
||||
pub ssy: usize,
|
||||
pub bit_depth: u32,
|
||||
pub lr_unit_rows: [usize; 3],
|
||||
pub lr_unit_cols: [usize; 3],
|
||||
pub cdef_cols: usize,
|
||||
/// Reference slots (FrameStore and saved state) used by this frame.
|
||||
pub refs: [Option<Arc<RefFrame>>; NUM_REF_FRAMES],
|
||||
pub ref_info: [RefInfo; NUM_REF_FRAMES],
|
||||
/// MotionFieldMvs[ ref ][ y8 * w8 + x8 ] (7.9); empty when unused.
|
||||
pub motion_field: [Vec<[i32; 2]>; 8],
|
||||
/// PrevSegmentIds (mi units).
|
||||
pub prev_segment_ids: Vec<u8>,
|
||||
}
|
||||
|
||||
impl FrameShared {
|
||||
pub fn plane_residual_size(&self, subsize: usize, plane: usize) -> usize {
|
||||
let sx = if plane > 0 { self.ssx } else { 0 };
|
||||
let sy = if plane > 0 { self.ssy } else { 0 };
|
||||
SUBSAMPLED_SIZE[subsize][sx][sy] as usize
|
||||
}
|
||||
}
|
||||
|
||||
/// A byte per 4x4 unit of a plane (LoopfilterTxSizes), possibly for a region only.
|
||||
#[derive(Clone, Default)]
|
||||
pub(crate) struct Grid8 {
|
||||
pub data: Vec<u8>,
|
||||
pub ox: usize,
|
||||
pub oy: usize,
|
||||
pub stride: usize,
|
||||
pub rows: usize,
|
||||
}
|
||||
|
||||
impl Grid8 {
|
||||
fn new(ox: usize, oy: usize, stride: usize, rows: usize) -> Grid8 {
|
||||
Grid8 { data: vec![0; stride * rows], ox, oy, stride, rows }
|
||||
}
|
||||
#[inline]
|
||||
pub fn get(&self, row: usize, col: usize) -> u8 {
|
||||
self.data[(row - self.oy) * self.stride + col - self.ox]
|
||||
}
|
||||
/// Stores outside the grid are dropped (blocks reaching past the allocated margin).
|
||||
#[inline]
|
||||
pub fn set(&mut self, row: usize, col: usize, v: u8) {
|
||||
if col >= self.ox && col < self.ox + self.stride && row >= self.oy && row < self.oy + self.rows {
|
||||
self.data[(row - self.oy) * self.stride + col - self.ox] = v;
|
||||
}
|
||||
}
|
||||
fn copy_into(&self, dst: &mut Grid8) {
|
||||
for y in 0..self.rows {
|
||||
let d = (self.oy + y - dst.oy) * dst.stride + self.ox - dst.ox;
|
||||
dst.data[d..d + self.stride].copy_from_slice(&self.data[y * self.stride..(y + 1) * self.stride]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Loop restoration parameters per unit.
|
||||
#[derive(Clone, Default)]
|
||||
pub(crate) struct LrUnits {
|
||||
pub lr_type: [Vec<u8>; 3],
|
||||
pub lr_wiener: [Vec<[[i8; 3]; 2]>; 3],
|
||||
pub lr_sgr_set: [Vec<u8>; 3],
|
||||
pub lr_sgr_xqd: [Vec<[i8; 2]>; 3],
|
||||
}
|
||||
|
||||
/// What tile decoding writes.
|
||||
pub(crate) struct TileState {
|
||||
pub cur: FrameBuf,
|
||||
pub mi: MiInfo,
|
||||
/// cdef_idx per 64x64 block (-1: not coded).
|
||||
pub cdef: Vec<i8>,
|
||||
/// LoopfilterTxSizes[ plane ][ row ][ col ] in 4x4 units of each plane.
|
||||
pub lf_tx_size: [Grid8; 3],
|
||||
pub lr: LrUnits,
|
||||
/// LR units this tile coded (per plane), for merging.
|
||||
pub lr_written: [Vec<usize>; 3],
|
||||
pub above_level: [Vec<u8>; 3],
|
||||
pub above_dc: [Vec<u8>; 3],
|
||||
pub above_seg_pred: Vec<u8>,
|
||||
pub left_level: [Vec<u8>; 3],
|
||||
pub left_dc: [Vec<u8>; 3],
|
||||
pub left_seg_pred: Vec<u8>,
|
||||
/// TileIntraFrameYModeCdf (reset per tile).
|
||||
pub intra_frame_y_mode_cdf: [[[u16; 14]; 5]; 5],
|
||||
cdef_cols: usize,
|
||||
}
|
||||
|
||||
impl TileState {
|
||||
fn with(cur: FrameBuf, mi: MiInfo, cdef: Vec<i8>, lf_tx_size: [Grid8; 3], lr: LrUnits, cdef_cols: usize) -> TileState {
|
||||
let (mi_cols, mi_rows) = (mi.cols, mi.rows);
|
||||
TileState {
|
||||
cur,
|
||||
mi,
|
||||
cdef,
|
||||
lf_tx_size,
|
||||
lr,
|
||||
lr_written: Default::default(),
|
||||
above_level: std::array::from_fn(|_| vec![0; mi_cols + 64]),
|
||||
above_dc: std::array::from_fn(|_| vec![0; mi_cols + 64]),
|
||||
above_seg_pred: vec![0; mi_cols + 64],
|
||||
left_level: std::array::from_fn(|_| vec![0; mi_rows + 64]),
|
||||
left_dc: std::array::from_fn(|_| vec![0; mi_rows + 64]),
|
||||
left_seg_pred: vec![0; mi_rows + 64],
|
||||
intra_frame_y_mode_cdf: DEFAULT_INTRA_FRAME_Y_MODE_CDF,
|
||||
cdef_cols,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn cdef_idx(&self, r: usize, c: usize) -> i8 {
|
||||
self.cdef[(r >> 4) * self.cdef_cols + (c >> 4)]
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_cdef_idx(&mut self, r: usize, c: usize, v: i8) {
|
||||
let i = (r >> 4) * self.cdef_cols + (c >> 4);
|
||||
if i < self.cdef.len() {
|
||||
self.cdef[i] = v;
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_lf_tx_size(&mut self, plane: usize, row: usize, col: usize, v: u8) {
|
||||
self.lf_tx_size[plane].set(row, col, v);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct FrameState {
|
||||
pub sh: FrameShared,
|
||||
pub cur: FrameBuf,
|
||||
pub mi: MiInfo,
|
||||
cdef: Vec<i8>,
|
||||
lf_tx_size: [Grid8; 3],
|
||||
pub lr: LrUnits,
|
||||
}
|
||||
|
||||
impl Deref for FrameState {
|
||||
type Target = FrameShared;
|
||||
fn deref(&self) -> &FrameShared {
|
||||
&self.sh
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for FrameState {
|
||||
fn deref_mut(&mut self) -> &mut FrameShared {
|
||||
&mut self.sh
|
||||
}
|
||||
}
|
||||
|
||||
fn count_units_in_frame(unit_size: usize, frame_size: usize) -> usize {
|
||||
((frame_size + (unit_size >> 1)) / unit_size).max(1)
|
||||
}
|
||||
|
||||
impl FrameState {
|
||||
pub fn new(seq: &SequenceHeader, fh: &FrameHeader) -> FrameState {
|
||||
let c = &seq.color;
|
||||
let (ssx, ssy) = (c.subsampling_x as usize, c.subsampling_y as usize);
|
||||
let mi_cols = fh.mi_cols as usize;
|
||||
let mi_rows = fh.mi_rows as usize;
|
||||
let cur = FrameBuf::new(fh.frame_width as usize, fh.frame_height as usize, c.num_planes, ssx, ssy, c.bit_depth);
|
||||
let cdef_cols = mi_cols.div_ceil(16) + 2;
|
||||
let cdef_rows = mi_rows.div_ceil(16) + 2;
|
||||
let mut lr_unit_rows = [0; 3];
|
||||
let mut lr_unit_cols = [0; 3];
|
||||
let mut lr = LrUnits::default();
|
||||
for plane in 0..c.num_planes {
|
||||
if fh.lr.frame_restoration_type[plane] == RESTORE_NONE as u8 {
|
||||
continue;
|
||||
}
|
||||
let sub_x = if plane == 0 { 0 } else { ssx };
|
||||
let sub_y = if plane == 0 { 0 } else { ssy };
|
||||
let unit = fh.lr.loop_restoration_size[plane] as usize;
|
||||
lr_unit_rows[plane] = count_units_in_frame(unit, (fh.frame_height as usize + sub_y) >> sub_y);
|
||||
lr_unit_cols[plane] = count_units_in_frame(unit, (fh.upscaled_width as usize + sub_x) >> sub_x);
|
||||
let n = lr_unit_rows[plane] * lr_unit_cols[plane];
|
||||
lr.lr_type[plane] = vec![RESTORE_NONE as u8; n];
|
||||
lr.lr_wiener[plane] = vec![[[0; 3]; 2]; n];
|
||||
lr.lr_sgr_set[plane] = vec![0; n];
|
||||
lr.lr_sgr_xqd[plane] = vec![[0; 2]; n];
|
||||
}
|
||||
FrameState {
|
||||
sh: FrameShared {
|
||||
seq: seq.clone(),
|
||||
fh: fh.clone(),
|
||||
num_planes: c.num_planes,
|
||||
ssx,
|
||||
ssy,
|
||||
bit_depth: c.bit_depth as u32,
|
||||
lr_unit_rows,
|
||||
lr_unit_cols,
|
||||
cdef_cols,
|
||||
refs: Default::default(),
|
||||
ref_info: Default::default(),
|
||||
motion_field: Default::default(),
|
||||
prev_segment_ids: vec![0; mi_cols * mi_rows],
|
||||
},
|
||||
cur,
|
||||
mi: MiInfo::new(mi_cols, mi_rows, fh.allow_screen_content_tools),
|
||||
cdef: vec![-1; cdef_cols * cdef_rows],
|
||||
lf_tx_size: std::array::from_fn(|_| Grid8::new(0, 0, mi_cols + 32, mi_rows + 32)),
|
||||
lr,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn cdef_idx(&self, r: usize, c: usize) -> i8 {
|
||||
self.cdef[(r >> 4) * self.cdef_cols + (c >> 4)]
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn lf_tx_size(&self, plane: usize, row: usize, col: usize) -> usize {
|
||||
self.lf_tx_size[plane].get(row, col) as usize
|
||||
}
|
||||
|
||||
/// Move the frame's buffers into a tile state covering the whole frame.
|
||||
pub fn take_tile_state(&mut self) -> TileState {
|
||||
TileState::with(
|
||||
std::mem::take(&mut self.cur),
|
||||
std::mem::take(&mut self.mi),
|
||||
std::mem::take(&mut self.cdef),
|
||||
std::mem::take(&mut self.lf_tx_size),
|
||||
std::mem::take(&mut self.lr),
|
||||
self.cdef_cols,
|
||||
)
|
||||
}
|
||||
|
||||
/// Return the buffers taken by [`FrameState::take_tile_state`].
|
||||
pub fn put_tile_state(&mut self, t: TileState) {
|
||||
self.cur = t.cur;
|
||||
self.mi = t.mi;
|
||||
self.cdef = t.cdef;
|
||||
self.lf_tx_size = t.lf_tx_size;
|
||||
self.lr = t.lr;
|
||||
}
|
||||
|
||||
/// Private buffers for the tile covering mode info rows [r0, r1) x columns [c0, c1); tiles
|
||||
/// on the right / bottom frame edge also cover the allocation margins.
|
||||
pub fn tile_state(&self, r0: usize, r1: usize, c0: usize, c1: usize) -> TileState {
|
||||
let (mi_cols, mi_rows) = (self.mi.cols, self.mi.rows);
|
||||
let x1 = if c1 >= mi_cols { usize::MAX } else { c1 * 4 };
|
||||
let y1 = if r1 >= mi_rows { usize::MAX } else { r1 * 4 };
|
||||
let cur = self.cur.region_like(c0 * 4, r0 * 4, x1, y1);
|
||||
let mi = MiInfo::region(mi_cols, mi_rows, c0, r0, c1 - c0, r1 - r0, !self.mi.palette_size[0].is_empty());
|
||||
let lf = std::array::from_fn(|p| {
|
||||
let (sx, sy) = if p == 0 { (0, 0) } else { (self.ssx, self.ssy) };
|
||||
let full = &self.lf_tx_size[p];
|
||||
let (gx0, gy0) = (c0 >> sx, r0 >> sy);
|
||||
let gx1 = if c1 >= mi_cols { full.stride } else { c1 >> sx };
|
||||
let gy1 = if r1 >= mi_rows { full.rows } else { r1 >> sy };
|
||||
Grid8::new(gx0, gy0, gx1 - gx0, gy1 - gy0)
|
||||
});
|
||||
TileState::with(cur, mi, vec![-1; self.cdef.len()], lf, self.lr.clone(), self.cdef_cols)
|
||||
}
|
||||
|
||||
/// Copy a tile's private buffers into the frame.
|
||||
pub fn merge_tile_state(&mut self, t: &TileState) {
|
||||
for p in 0..self.num_planes {
|
||||
t.cur.planes[p].copy_into(&mut self.cur.planes[p]);
|
||||
t.lf_tx_size[p].copy_into(&mut self.lf_tx_size[p]);
|
||||
for &u in &t.lr_written[p] {
|
||||
self.lr.lr_type[p][u] = t.lr.lr_type[p][u];
|
||||
self.lr.lr_wiener[p][u] = t.lr.lr_wiener[p][u];
|
||||
self.lr.lr_sgr_set[p][u] = t.lr.lr_sgr_set[p][u];
|
||||
self.lr.lr_sgr_xqd[p][u] = t.lr.lr_sgr_xqd[p][u];
|
||||
}
|
||||
}
|
||||
t.mi.copy_into(&mut self.mi);
|
||||
for (d, &s) in self.cdef.iter_mut().zip(&t.cdef) {
|
||||
if s != -1 {
|
||||
*d = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
//! Decode statistics: frame / tile counts and the busy time of every decoding stage.
|
||||
|
||||
/// A decoding stage (see [`DecodeStats::stage_secs`]).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Stage {
|
||||
/// Frame header follow-up: reference setup, motion field estimation, CDF initialisation.
|
||||
Setup,
|
||||
/// Tile decoding: mode info, coefficients, prediction and reconstruction.
|
||||
Tiles,
|
||||
/// Deblocking loop filter (7.14).
|
||||
LoopFilter,
|
||||
/// CDEF (7.15).
|
||||
Cdef,
|
||||
/// Super-resolution upscaling (7.16).
|
||||
Superres,
|
||||
/// Loop restoration (7.17).
|
||||
Restoration,
|
||||
/// Reference update, motion vector storage, output copy and film grain synthesis.
|
||||
Output,
|
||||
}
|
||||
|
||||
impl Stage {
|
||||
pub const COUNT: usize = 7;
|
||||
pub const ALL: [Stage; Stage::COUNT] =
|
||||
[Stage::Setup, Stage::Tiles, Stage::LoopFilter, Stage::Cdef, Stage::Superres, Stage::Restoration, Stage::Output];
|
||||
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
Stage::Setup => "setup",
|
||||
Stage::Tiles => "tiles",
|
||||
Stage::LoopFilter => "deblock",
|
||||
Stage::Cdef => "cdef",
|
||||
Stage::Superres => "superres",
|
||||
Stage::Restoration => "restoration",
|
||||
Stage::Output => "output",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Counters accumulated by a [`crate::Decoder`]. Stage times are busy time summed over every
|
||||
/// thread that worked on the stage (wall-clock per piece of work), so with threads they can add
|
||||
/// up to more than the elapsed time.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub struct DecodeStats {
|
||||
/// Frames decoded (excluding show_existing_frame repeats).
|
||||
pub frames: u64,
|
||||
/// Frames decoded in draft mode without in-loop filters.
|
||||
pub draft_frames: u64,
|
||||
/// Tiles decoded.
|
||||
pub tiles: u64,
|
||||
/// Busy seconds per [`Stage`] (index with `stage as usize`).
|
||||
pub stage_secs: [f64; Stage::COUNT],
|
||||
}
|
||||
|
||||
impl DecodeStats {
|
||||
pub fn secs(&self, stage: Stage) -> f64 {
|
||||
self.stage_secs[stage as usize]
|
||||
}
|
||||
|
||||
pub(crate) fn add(&mut self, stage: Stage, secs: f64) {
|
||||
self.stage_secs[stage as usize] += secs;
|
||||
}
|
||||
|
||||
pub(crate) fn merge(&mut self, o: &DecodeStats) {
|
||||
self.frames += o.frames;
|
||||
self.draft_frames += o.draft_frames;
|
||||
self.tiles += o.tiles;
|
||||
for (a, b) in self.stage_secs.iter_mut().zip(o.stage_secs.iter()) {
|
||||
*a += b;
|
||||
}
|
||||
}
|
||||
|
||||
/// Sum of all stage times.
|
||||
pub fn total_secs(&self) -> f64 {
|
||||
self.stage_secs.iter().sum()
|
||||
}
|
||||
}
|
||||
|
||||
/// A wall-clock stopwatch (a no-op on wasm32, which has no clock in std).
|
||||
pub(crate) struct Timer {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
start: std::time::Instant,
|
||||
}
|
||||
|
||||
impl Timer {
|
||||
#[inline]
|
||||
pub fn start() -> Timer {
|
||||
Timer {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
start: std::time::Instant::now(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Seconds since `start` (0 on wasm32).
|
||||
#[inline]
|
||||
pub fn secs(&self) -> f64 {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
self.start.elapsed().as_secs_f64()
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Seconds since `start`, restarting the stopwatch.
|
||||
#[inline]
|
||||
pub fn lap(&mut self) -> f64 {
|
||||
let s = self.secs();
|
||||
*self = Timer::start();
|
||||
s
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
//! Symbol (arithmetic) decoder, spec 8.2.
|
||||
//!
|
||||
//! The state follows the specification literally: `SymbolValue` / `SymbolRange` are 15/16-bit
|
||||
//! quantities renormalised by reading `bits` new bits at a time, and `SymbolMaxBits` counts the
|
||||
//! bits still available (going negative while zero padding is consumed).
|
||||
|
||||
const EC_PROB_SHIFT: u32 = 6;
|
||||
const EC_MIN_PROB: u32 = 4;
|
||||
|
||||
pub(crate) struct SymbolDecoder<'a> {
|
||||
data: &'a [u8],
|
||||
/// Next byte of `data` to load into the window.
|
||||
next: usize,
|
||||
/// SymbolValue followed by `k` look-ahead bits: the bitstream with every bit inverted
|
||||
/// (the spec's renormalisation XORs the new bits with ones), zero bits past the end of
|
||||
/// the data reading as ones. SymbolValue = win >> k.
|
||||
win: u64,
|
||||
k: u32,
|
||||
range: u32,
|
||||
pub disable_update: bool,
|
||||
}
|
||||
|
||||
impl<'a> SymbolDecoder<'a> {
|
||||
/// `init_symbol( sz )` over `data` (the tile's bytes).
|
||||
pub fn new(data: &'a [u8], disable_update: bool) -> Self {
|
||||
let mut d = SymbolDecoder { data, next: 0, win: 0, k: 0, range: 1 << 15, disable_update };
|
||||
// SymbolValue = ((1 << 15) - 1) ^ (first 15 bits): 15 inverted bits.
|
||||
let mut filled = 0;
|
||||
while filled < 15 + 32 {
|
||||
d.win = (d.win << 8) | d.inverted_byte() as u64;
|
||||
filled += 8;
|
||||
}
|
||||
d.k = filled - 15;
|
||||
d
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn inverted_byte(&mut self) -> u8 {
|
||||
let b = self.data.get(self.next).copied().unwrap_or(0);
|
||||
self.next += 1;
|
||||
!b
|
||||
}
|
||||
|
||||
/// `read_symbol( cdf )`: `cdf` has N + 1 entries (the last is the adaptation counter).
|
||||
#[inline]
|
||||
pub fn read_symbol(&mut self, cdf: &mut [u16]) -> usize {
|
||||
let n = cdf.len() - 1;
|
||||
let symbol = self.decode(cdf, n);
|
||||
if !self.disable_update {
|
||||
update_cdf(cdf, n, symbol);
|
||||
}
|
||||
symbol
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn decode(&mut self, cdf: &[u16], n: usize) -> usize {
|
||||
let value = (self.win >> self.k) as u32;
|
||||
let mut cur = self.range;
|
||||
let mut symbol = 0usize;
|
||||
let mut prev;
|
||||
loop {
|
||||
prev = cur;
|
||||
let f = (1u32 << 15) - cdf[symbol] as u32;
|
||||
cur = ((self.range >> 8) * (f >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT);
|
||||
cur += EC_MIN_PROB * (n - symbol - 1) as u32;
|
||||
if value >= cur {
|
||||
break;
|
||||
}
|
||||
symbol += 1;
|
||||
}
|
||||
self.range = prev - cur;
|
||||
self.win -= (cur as u64) << self.k;
|
||||
self.renormalize();
|
||||
symbol
|
||||
}
|
||||
|
||||
/// Renormalisation (8.2.6): SymbolValue takes `bits` more (inverted) bits, which are
|
||||
/// already in the window.
|
||||
#[inline(always)]
|
||||
fn renormalize(&mut self) {
|
||||
let bits = 15 - (31 - self.range.leading_zeros());
|
||||
self.range <<= bits;
|
||||
self.k -= bits;
|
||||
if self.k < 16 {
|
||||
// SymbolValue < 2^16, so 16 + k <= 64 bits stay in the window.
|
||||
while self.k <= 40 {
|
||||
self.win = (self.win << 8) | self.inverted_byte() as u64;
|
||||
self.k += 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `read_bool()`
|
||||
#[inline]
|
||||
pub fn read_bool(&mut self) -> u32 {
|
||||
let cdf = [1u16 << 14, 1 << 15, 0];
|
||||
self.decode(&cdf, 2) as u32
|
||||
}
|
||||
|
||||
/// `read_literal( n )` (L(n))
|
||||
pub fn read_literal(&mut self, n: u32) -> u32 {
|
||||
let mut x = 0;
|
||||
for _ in 0..n {
|
||||
x = (x << 1) | self.read_bool();
|
||||
}
|
||||
x
|
||||
}
|
||||
|
||||
/// NS(n)
|
||||
pub fn read_ns(&mut self, n: u32) -> u32 {
|
||||
if n <= 1 {
|
||||
return 0;
|
||||
}
|
||||
let w = 32 - n.leading_zeros();
|
||||
let m = (1u32 << w) - n;
|
||||
let v = self.read_literal(w - 1);
|
||||
if v < m {
|
||||
return v;
|
||||
}
|
||||
let extra = self.read_literal(1);
|
||||
(v << 1) - m + extra
|
||||
}
|
||||
}
|
||||
|
||||
/// CDF adaptation (8.2.6).
|
||||
#[inline(always)]
|
||||
pub(crate) fn update_cdf(cdf: &mut [u16], n: usize, symbol: usize) {
|
||||
let count = cdf[n];
|
||||
let rate = 3 + (count > 15) as u32 + (count > 31) as u32 + (31 - (n as u32).leading_zeros()).min(2);
|
||||
// Entries below `symbol` move towards 0, the others towards 1 << 15 (the specification's
|
||||
// comparison with tmp, resolved per entry: c >= 0 and c <= 1 << 15), without branching on
|
||||
// the decoded symbol.
|
||||
for (i, e) in cdf[..n - 1].iter_mut().enumerate() {
|
||||
let c = *e as u32;
|
||||
*e = if i < symbol { c - (c >> rate) } else { c + (((1 << 15) - c) >> rate) } as u16;
|
||||
}
|
||||
cdf[n] += (count < 32) as u16;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,780 @@
|
||||
//! Inverse transforms (spec 7.13), implemented step by step as specified.
|
||||
|
||||
use crate::spec_tables::{
|
||||
ADST_ADST, ADST_DCT, ADST_FLIPADST, COS128_LOOKUP, DCT_ADST, DCT_DCT, DCT_FLIPADST, FLIPADST_ADST, FLIPADST_DCT, FLIPADST_FLIPADST, H_ADST,
|
||||
H_DCT, H_FLIPADST, TRANSFORM_ROW_SHIFT, TX_HEIGHT_LOG2, TX_WIDTH_LOG2, V_ADST, V_DCT, V_FLIPADST,
|
||||
};
|
||||
|
||||
#[inline(always)]
|
||||
fn round2_64(x: i64, n: u32) -> i32 {
|
||||
if n == 0 { x as i32 } else { ((x + (1i64 << (n - 1))) >> n) as i32 }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn round2(x: i32, n: u32) -> i32 {
|
||||
if n == 0 { x } else { (x + (1 << (n - 1))) >> n }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn cos128(angle: i32) -> i64 {
|
||||
let a = (angle & 255) as usize;
|
||||
(match a {
|
||||
0..=64 => COS128_LOOKUP[a] as i32,
|
||||
65..=128 => -(COS128_LOOKUP[128 - a] as i32),
|
||||
129..=192 => -(COS128_LOOKUP[a - 128] as i32),
|
||||
_ => COS128_LOOKUP[256 - a] as i32,
|
||||
}) as i64
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn sin128(angle: i32) -> i64 {
|
||||
cos128(angle - 64)
|
||||
}
|
||||
|
||||
/// One value of the 1D transforms, or `L` independent columns / rows of them processed
|
||||
/// together (lane-wise, so the steps vectorise).
|
||||
pub(crate) trait Lane: Copy {
|
||||
const ZERO: Self;
|
||||
/// (Round2( a * cos - b * sin, 12 ), Round2( a * sin + b * cos, 12 ))
|
||||
fn rotate(a: Self, b: Self, cos: i32, sin: i32) -> (Self, Self);
|
||||
/// (Clamp( x + y ), Clamp( x - y )) to [lo, hi]
|
||||
fn hadamard(x: Self, y: Self, lo: i32, hi: i32) -> (Self, Self);
|
||||
fn neg(self) -> Self;
|
||||
/// Round2( v * m, 12 )
|
||||
fn mul_round12(self, m: i32) -> Self;
|
||||
fn mul(self, k: i32) -> Self;
|
||||
fn adst4(t: &mut [Self]);
|
||||
}
|
||||
|
||||
impl Lane for i32 {
|
||||
const ZERO: i32 = 0;
|
||||
#[inline(always)]
|
||||
fn rotate(a: i32, b: i32, cos: i32, sin: i32) -> (i32, i32) {
|
||||
let (a, b, c, s) = (a as i64, b as i64, cos as i64, sin as i64);
|
||||
(round2_64(a * c - b * s, 12), round2_64(a * s + b * c, 12))
|
||||
}
|
||||
#[inline(always)]
|
||||
fn hadamard(x: i32, y: i32, lo: i32, hi: i32) -> (i32, i32) {
|
||||
((x + y).clamp(lo, hi), (x - y).clamp(lo, hi))
|
||||
}
|
||||
#[inline(always)]
|
||||
fn neg(self) -> i32 {
|
||||
-self
|
||||
}
|
||||
#[inline(always)]
|
||||
fn mul_round12(self, m: i32) -> i32 {
|
||||
round2_64(self as i64 * m as i64, 12)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn mul(self, k: i32) -> i32 {
|
||||
self * k
|
||||
}
|
||||
fn adst4(t: &mut [i32]) {
|
||||
inverse_adst4_scalar(t);
|
||||
}
|
||||
}
|
||||
|
||||
/// Lanes in 32-bit arithmetic: exact for conforming streams up to 10 bits per sample (the
|
||||
/// spec requires every intermediate value to fit in 8 + BitDepth <= 18 bits, so products with
|
||||
/// the 12-bit cosines stay below 2^31). Wrapping operations keep corrupt streams from
|
||||
/// panicking.
|
||||
impl<const L: usize> Lane for [i32; L] {
|
||||
const ZERO: Self = [0; L];
|
||||
#[inline(always)]
|
||||
fn rotate(a: Self, b: Self, cos: i32, sin: i32) -> (Self, Self) {
|
||||
let mut x = [0; L];
|
||||
let mut y = [0; L];
|
||||
for l in 0..L {
|
||||
let p = a[l].wrapping_mul(cos).wrapping_sub(b[l].wrapping_mul(sin));
|
||||
let q = a[l].wrapping_mul(sin).wrapping_add(b[l].wrapping_mul(cos));
|
||||
x[l] = p.wrapping_add(1 << 11) >> 12;
|
||||
y[l] = q.wrapping_add(1 << 11) >> 12;
|
||||
}
|
||||
(x, y)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn hadamard(x: Self, y: Self, lo: i32, hi: i32) -> (Self, Self) {
|
||||
let mut a = [0; L];
|
||||
let mut b = [0; L];
|
||||
for l in 0..L {
|
||||
a[l] = x[l].wrapping_add(y[l]).max(lo).min(hi);
|
||||
b[l] = x[l].wrapping_sub(y[l]).max(lo).min(hi);
|
||||
}
|
||||
(a, b)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn neg(self) -> Self {
|
||||
self.map(i32::wrapping_neg)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn mul_round12(self, m: i32) -> Self {
|
||||
self.map(|v| v.wrapping_mul(m).wrapping_add(1 << 11) >> 12)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn mul(self, k: i32) -> Self {
|
||||
self.map(|v| v.wrapping_mul(k))
|
||||
}
|
||||
fn adst4(t: &mut [Self]) {
|
||||
for l in 0..L {
|
||||
let mut v = [t[0][l], t[1][l], t[2][l], t[3][l]];
|
||||
inverse_adst4_scalar(&mut v);
|
||||
for k in 0..4 {
|
||||
t[k][l] = v[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn brev(num_bits: u32, x: usize) -> usize {
|
||||
let mut t = 0;
|
||||
for i in 0..num_bits {
|
||||
let bit = (x >> i) & 1;
|
||||
t += bit << (num_bits - 1 - i);
|
||||
}
|
||||
t
|
||||
}
|
||||
|
||||
/// Butterfly rotation B( a, b, angle, flip ).
|
||||
#[inline(always)]
|
||||
fn bf<T: Lane>(t: &mut [T], a: usize, b: usize, angle: i32, flip: bool) {
|
||||
let (x, y) = T::rotate(t[a], t[b], cos128(angle) as i32, sin128(angle) as i32);
|
||||
t[a] = x;
|
||||
t[b] = y;
|
||||
if flip {
|
||||
t.swap(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
/// Hadamard rotation H( a, b, flip, r ).
|
||||
#[inline(always)]
|
||||
fn hd<T: Lane>(t: &mut [T], a: usize, b: usize, flip: bool, r: u32) {
|
||||
let (a, b) = if flip { (b, a) } else { (a, b) };
|
||||
let lo = -(1i32 << (r - 1));
|
||||
let hi = (1i32 << (r - 1)) - 1;
|
||||
let (x, y) = T::hadamard(t[a], t[b], lo, hi);
|
||||
t[a] = x;
|
||||
t[b] = y;
|
||||
}
|
||||
|
||||
/// Inverse DCT of length 2^n (7.13.2.3), in place.
|
||||
pub(crate) fn inverse_dct<T: Lane>(t: &mut [T], n: u32, r: u32) {
|
||||
// 7.13.2.2 permutation
|
||||
let len = 1usize << n;
|
||||
let mut copy = [T::ZERO; 64];
|
||||
copy[..len].copy_from_slice(&t[..len]);
|
||||
for i in 0..len {
|
||||
t[i] = copy[brev(n, i)];
|
||||
}
|
||||
inverse_dct_stage1(t, n, r);
|
||||
inverse_dct_stage2(t, n, r);
|
||||
inverse_dct_stage3(t, n, r);
|
||||
inverse_dct_stage4(t, n, r);
|
||||
inverse_dct_stage5(t, n, r);
|
||||
inverse_dct_stage6(t, n, r);
|
||||
}
|
||||
|
||||
fn inverse_dct_stage1<T: Lane>(t: &mut [T], n: u32, r: u32) {
|
||||
if n == 6 {
|
||||
for i in 0..16 {
|
||||
bf(t, 32 + i, 63 - i, 63 - 4 * brev(4, i) as i32, false);
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..8 {
|
||||
bf(t, 16 + i, 31 - i, 6 + ((brev(3, 7 - i) as i32) << 3), false);
|
||||
}
|
||||
}
|
||||
if n == 6 {
|
||||
for i in 0..16 {
|
||||
hd(t, 32 + i * 2, 33 + i * 2, i & 1 == 1, r);
|
||||
}
|
||||
}
|
||||
if n >= 4 {
|
||||
for i in 0..4 {
|
||||
bf(t, 8 + i, 15 - i, 12 + ((brev(2, 3 - i) as i32) << 4), false);
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..8 {
|
||||
hd(t, 16 + 2 * i, 17 + 2 * i, i & 1 == 1, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn inverse_dct_stage2<T: Lane>(t: &mut [T], n: u32, r: u32) {
|
||||
if n == 6 {
|
||||
for i in 0..4 {
|
||||
for j in 0..2 {
|
||||
bf(t, 62 - i * 4 - j, 33 + i * 4 + j, 60 - 16 * brev(2, i) as i32 + 64 * j as i32, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
if n >= 3 {
|
||||
for i in 0..2 {
|
||||
bf(t, 4 + i, 7 - i, 56 - 32 * i as i32, false);
|
||||
}
|
||||
}
|
||||
if n >= 4 {
|
||||
for i in 0..4 {
|
||||
hd(t, 8 + 2 * i, 9 + 2 * i, i & 1 == 1, r);
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..2 {
|
||||
for j in 0..2 {
|
||||
bf(t, 30 - 4 * i - j, 17 + 4 * i + j, 24 + ((j as i32) << 6) + (((1 - i) as i32) << 5), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
if n == 6 {
|
||||
for i in 0..8 {
|
||||
for j in 0..2 {
|
||||
hd(t, 32 + i * 4 + j, 35 + i * 4 - j, i & 1 == 1, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn inverse_dct_stage3<T: Lane>(t: &mut [T], n: u32, r: u32) {
|
||||
for i in 0..2 {
|
||||
bf(t, 2 * i, 2 * i + 1, 32 + 16 * i as i32, i == 0);
|
||||
}
|
||||
if n >= 3 {
|
||||
for i in 0..2 {
|
||||
hd(t, 4 + 2 * i, 5 + 2 * i, i == 1, r);
|
||||
}
|
||||
}
|
||||
if n >= 4 {
|
||||
for i in 0..2 {
|
||||
bf(t, 14 - i, 9 + i, 48 + 64 * i as i32, true);
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..4 {
|
||||
for j in 0..2 {
|
||||
hd(t, 16 + 4 * i + j, 19 + 4 * i - j, i & 1 == 1, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
if n == 6 {
|
||||
for i in 0..2 {
|
||||
for j in 0..4 {
|
||||
bf(t, 61 - i * 8 - j, 34 + i * 8 + j, 56 - i as i32 * 32 + (j as i32 >> 1) * 64, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn inverse_dct_stage4<T: Lane>(t: &mut [T], n: u32, r: u32) {
|
||||
for i in 0..2 {
|
||||
hd(t, i, 3 - i, false, r);
|
||||
}
|
||||
if n >= 3 {
|
||||
bf(t, 6, 5, 32, true);
|
||||
}
|
||||
if n >= 4 {
|
||||
for i in 0..2 {
|
||||
for j in 0..2 {
|
||||
hd(t, 8 + 4 * i + j, 11 + 4 * i - j, i == 1, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..4 {
|
||||
bf(t, 29 - i, 18 + i, 48 + (i as i32 >> 1) * 64, true);
|
||||
}
|
||||
}
|
||||
if n == 6 {
|
||||
for i in 0..4 {
|
||||
for j in 0..4 {
|
||||
hd(t, 32 + 8 * i + j, 39 + 8 * i - j, i & 1 == 1, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn inverse_dct_stage5<T: Lane>(t: &mut [T], n: u32, r: u32) {
|
||||
if n >= 3 {
|
||||
for i in 0..4 {
|
||||
hd(t, i, 7 - i, false, r);
|
||||
}
|
||||
}
|
||||
if n >= 4 {
|
||||
for i in 0..2 {
|
||||
bf(t, 13 - i, 10 + i, 32, true);
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..2 {
|
||||
for j in 0..4 {
|
||||
hd(t, 16 + i * 8 + j, 23 + i * 8 - j, i == 1, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
if n == 6 {
|
||||
for i in 0..8 {
|
||||
bf(t, 59 - i, 36 + i, if i < 4 { 48 } else { 112 }, true);
|
||||
}
|
||||
}
|
||||
if n >= 4 {
|
||||
for i in 0..8 {
|
||||
hd(t, i, 15 - i, false, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn inverse_dct_stage6<T: Lane>(t: &mut [T], n: u32, r: u32) {
|
||||
if n >= 5 {
|
||||
for i in 0..4 {
|
||||
bf(t, 27 - i, 20 + i, 32, true);
|
||||
}
|
||||
}
|
||||
if n == 6 {
|
||||
for i in 0..8 {
|
||||
hd(t, 32 + i, 47 - i, false, r);
|
||||
hd(t, 48 + i, 63 - i, true, r);
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..16 {
|
||||
hd(t, i, 31 - i, false, r);
|
||||
}
|
||||
}
|
||||
if n == 6 {
|
||||
for i in 0..8 {
|
||||
bf(t, 55 - i, 40 + i, 32, true);
|
||||
}
|
||||
}
|
||||
if n == 6 {
|
||||
for i in 0..32 {
|
||||
hd(t, i, 63 - i, false, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const SINPI_1_9: i64 = 1321;
|
||||
const SINPI_2_9: i64 = 2482;
|
||||
const SINPI_3_9: i64 = 3344;
|
||||
const SINPI_4_9: i64 = 3803;
|
||||
|
||||
fn inverse_adst4_scalar(t: &mut [i32]) {
|
||||
let mut s = [0i64; 7];
|
||||
let mut x = [0i64; 4];
|
||||
let t0 = t[0] as i64;
|
||||
let t1 = t[1] as i64;
|
||||
let t2 = t[2] as i64;
|
||||
let t3 = t[3] as i64;
|
||||
s[0] = SINPI_1_9 * t0;
|
||||
s[1] = SINPI_2_9 * t0;
|
||||
s[2] = SINPI_3_9 * t1;
|
||||
s[3] = SINPI_4_9 * t2;
|
||||
s[4] = SINPI_1_9 * t2;
|
||||
s[5] = SINPI_2_9 * t3;
|
||||
s[6] = SINPI_4_9 * t3;
|
||||
let a7 = t0 - t2;
|
||||
let b7 = a7 + t3;
|
||||
s[0] += s[3];
|
||||
s[1] -= s[4];
|
||||
s[3] = s[2];
|
||||
s[2] = SINPI_3_9 * b7;
|
||||
s[0] += s[5];
|
||||
s[1] -= s[6];
|
||||
x[0] = s[0] + s[3];
|
||||
x[1] = s[1] + s[3];
|
||||
x[2] = s[2];
|
||||
x[3] = s[0] + s[1];
|
||||
x[3] -= s[3];
|
||||
for i in 0..4 {
|
||||
t[i] = round2_64(x[i], 12);
|
||||
}
|
||||
}
|
||||
|
||||
fn adst_in_permute<T: Lane>(t: &mut [T], n: u32) {
|
||||
let n0 = 1usize << n;
|
||||
let mut copy = [T::ZERO; 16];
|
||||
copy[..n0].copy_from_slice(&t[..n0]);
|
||||
for i in 0..n0 {
|
||||
let idx = if i & 1 == 1 { i - 1 } else { n0 - i - 1 };
|
||||
t[i] = copy[idx];
|
||||
}
|
||||
}
|
||||
|
||||
fn adst_out_permute<T: Lane>(t: &mut [T], n: u32) {
|
||||
let n0 = 1usize << n;
|
||||
let mut copy = [T::ZERO; 16];
|
||||
copy[..n0].copy_from_slice(&t[..n0]);
|
||||
for i in 0..n0 {
|
||||
let a = (i >> 3) & 1;
|
||||
let b = ((i >> 2) & 1) ^ ((i >> 3) & 1);
|
||||
let c = ((i >> 1) & 1) ^ ((i >> 2) & 1);
|
||||
let d = (i & 1) ^ ((i >> 1) & 1);
|
||||
let idx = ((d << 3) | (c << 2) | (b << 1) | a) >> (4 - n);
|
||||
t[i] = if i & 1 == 1 { copy[idx].neg() } else { copy[idx] };
|
||||
}
|
||||
}
|
||||
|
||||
fn inverse_adst8<T: Lane>(t: &mut [T], r: u32) {
|
||||
adst_in_permute(t, 3);
|
||||
for i in 0..4 {
|
||||
bf(t, 2 * i, 2 * i + 1, 60 - 16 * i as i32, true);
|
||||
}
|
||||
for i in 0..4 {
|
||||
hd(t, i, 4 + i, false, r);
|
||||
}
|
||||
for i in 0..2 {
|
||||
bf(t, 4 + 3 * i, 5 + i, 48 - 32 * i as i32, true);
|
||||
}
|
||||
for i in 0..2 {
|
||||
for j in 0..2 {
|
||||
hd(t, 4 * j + i, 2 + 4 * j + i, false, r);
|
||||
}
|
||||
}
|
||||
for i in 0..2 {
|
||||
bf(t, 2 + 4 * i, 3 + 4 * i, 32, true);
|
||||
}
|
||||
adst_out_permute(t, 3);
|
||||
}
|
||||
|
||||
fn inverse_adst16<T: Lane>(t: &mut [T], r: u32) {
|
||||
adst_in_permute(t, 4);
|
||||
for i in 0..8 {
|
||||
bf(t, 2 * i, 2 * i + 1, 62 - 8 * i as i32, true);
|
||||
}
|
||||
for i in 0..8 {
|
||||
hd(t, i, 8 + i, false, r);
|
||||
}
|
||||
for i in 0..2 {
|
||||
bf(t, 8 + 2 * i, 9 + 2 * i, 56 - 32 * i as i32, true);
|
||||
bf(t, 13 + 2 * i, 12 + 2 * i, 8 + 32 * i as i32, true);
|
||||
}
|
||||
for i in 0..4 {
|
||||
for j in 0..2 {
|
||||
hd(t, 8 * j + i, 4 + 8 * j + i, false, r);
|
||||
}
|
||||
}
|
||||
for i in 0..2 {
|
||||
for j in 0..2 {
|
||||
bf(t, 4 + 8 * j + 3 * i, 5 + 8 * j + i, 48 - 32 * i as i32, true);
|
||||
}
|
||||
}
|
||||
for i in 0..2 {
|
||||
for j in 0..4 {
|
||||
hd(t, 4 * j + i, 2 + 4 * j + i, false, r);
|
||||
}
|
||||
}
|
||||
for i in 0..4 {
|
||||
bf(t, 2 + 4 * i, 3 + 4 * i, 32, true);
|
||||
}
|
||||
adst_out_permute(t, 4);
|
||||
}
|
||||
|
||||
fn inverse_adst<T: Lane>(t: &mut [T], n: u32, r: u32) {
|
||||
match n {
|
||||
2 => T::adst4(t),
|
||||
3 => inverse_adst8(t, r),
|
||||
_ => inverse_adst16(t, r),
|
||||
}
|
||||
}
|
||||
|
||||
fn inverse_identity<T: Lane>(t: &mut [T], n: u32) {
|
||||
match n {
|
||||
2 => {
|
||||
for v in t[..4].iter_mut() {
|
||||
*v = v.mul_round12(5793);
|
||||
}
|
||||
}
|
||||
3 => {
|
||||
for v in t[..8].iter_mut() {
|
||||
*v = v.mul(2);
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
for v in t[..16].iter_mut() {
|
||||
*v = v.mul_round12(11586);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
for v in t[..32].iter_mut() {
|
||||
*v = v.mul(4);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn inverse_wht(t: &mut [i32], shift: u32) {
|
||||
let mut a = t[0] >> shift;
|
||||
let mut c = t[1] >> shift;
|
||||
let mut d = t[2] >> shift;
|
||||
let mut b = t[3] >> shift;
|
||||
a += c;
|
||||
d -= b;
|
||||
let e = (a - d) >> 1;
|
||||
b = e - b;
|
||||
c = e - c;
|
||||
a -= b;
|
||||
d += c;
|
||||
t[0] = a;
|
||||
t[1] = b;
|
||||
t[2] = c;
|
||||
t[3] = d;
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Kind1d {
|
||||
Dct,
|
||||
Adst,
|
||||
Identity,
|
||||
}
|
||||
|
||||
fn row_kind(tx_type: usize) -> Kind1d {
|
||||
match tx_type {
|
||||
DCT_DCT | ADST_DCT | FLIPADST_DCT | H_DCT => Kind1d::Dct,
|
||||
DCT_ADST | ADST_ADST | DCT_FLIPADST | FLIPADST_FLIPADST | ADST_FLIPADST | FLIPADST_ADST | H_ADST | H_FLIPADST => Kind1d::Adst,
|
||||
_ => Kind1d::Identity,
|
||||
}
|
||||
}
|
||||
|
||||
fn col_kind(tx_type: usize) -> Kind1d {
|
||||
match tx_type {
|
||||
DCT_DCT | DCT_ADST | DCT_FLIPADST | V_DCT => Kind1d::Dct,
|
||||
ADST_DCT | ADST_ADST | FLIPADST_DCT | FLIPADST_FLIPADST | ADST_FLIPADST | FLIPADST_ADST | V_ADST | V_FLIPADST => Kind1d::Adst,
|
||||
_ => Kind1d::Identity,
|
||||
}
|
||||
}
|
||||
|
||||
/// 2D inverse transform (7.13.3). `dequant` is 64x64 (row stride 64, only the top-left 32x32
|
||||
/// may be non-zero); the residual is written to `residual` with row stride `w`.
|
||||
pub(crate) fn inverse_transform_2d(dequant: &[i32], residual: &mut [i32], tx_sz: usize, tx_type: usize, lossless: bool, bit_depth: u32) {
|
||||
let log2w = TX_WIDTH_LOG2[tx_sz] as u32;
|
||||
let log2h = TX_HEIGHT_LOG2[tx_sz] as u32;
|
||||
let w = 1usize << log2w;
|
||||
let h = 1usize << log2h;
|
||||
let row_shift = if lossless { 0 } else { TRANSFORM_ROW_SHIFT[tx_sz] as u32 };
|
||||
let col_shift = if lossless { 0 } else { 4 };
|
||||
let row_clamp = bit_depth + 8;
|
||||
let col_clamp = (bit_depth + 6).max(16);
|
||||
let rk = row_kind(tx_type);
|
||||
let ck = col_kind(tx_type);
|
||||
let rect2 = log2w.abs_diff(log2h) == 1;
|
||||
let p = Tx2d { w, h, log2w, log2h, row_shift, col_shift, row_clamp, col_clamp, rk, ck, rect2 };
|
||||
if !lossless && bit_depth <= 10 {
|
||||
if w >= 8 && h >= 8 {
|
||||
transform_lanes::<8>(dequant, residual, &p);
|
||||
} else {
|
||||
transform_lanes::<4>(dequant, residual, &p);
|
||||
}
|
||||
return;
|
||||
}
|
||||
scalar_row_pass(dequant, residual, &p, lossless);
|
||||
scalar_col_pass(residual, &p, lossless);
|
||||
}
|
||||
|
||||
fn scalar_row_pass(dequant: &[i32], residual: &mut [i32], p: &Tx2d, lossless: bool) {
|
||||
let (w, h, log2w, row_shift, row_clamp, col_clamp, rk, rect2) = (p.w, p.h, p.log2w, p.row_shift, p.row_clamp, p.col_clamp, p.rk, p.rect2);
|
||||
let mut t = [0i32; 64];
|
||||
let in_w = w.min(32);
|
||||
for i in 0..h {
|
||||
// Every 1D transform maps an all-zero row to zeros (each step is linear with
|
||||
// Round2( 0 ) = 0), so rows without coefficients (including rows 32..63) are zero.
|
||||
if i >= 32 || dequant[i * 64..i * 64 + in_w].iter().all(|&v| v == 0) {
|
||||
residual[i * w..i * w + w].fill(0);
|
||||
continue;
|
||||
}
|
||||
t[..in_w].copy_from_slice(&dequant[i * 64..i * 64 + in_w]);
|
||||
t[in_w..w].fill(0);
|
||||
if rect2 {
|
||||
for v in t[..w].iter_mut() {
|
||||
*v = round2_64(*v as i64 * 2896, 12);
|
||||
}
|
||||
}
|
||||
if lossless {
|
||||
inverse_wht(&mut t, 2);
|
||||
} else {
|
||||
match rk {
|
||||
Kind1d::Dct => inverse_dct(&mut t, log2w, row_clamp),
|
||||
Kind1d::Adst => inverse_adst(&mut t, log2w, row_clamp),
|
||||
Kind1d::Identity => inverse_identity(&mut t, log2w),
|
||||
}
|
||||
}
|
||||
let lo = -(1i32 << (col_clamp - 1));
|
||||
let hi = (1i32 << (col_clamp - 1)) - 1;
|
||||
for j in 0..w {
|
||||
residual[i * w + j] = round2(t[j], row_shift).clamp(lo, hi);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn scalar_col_pass(residual: &mut [i32], p: &Tx2d, lossless: bool) {
|
||||
let (w, h, log2h, col_shift, col_clamp, ck) = (p.w, p.h, p.log2h, p.col_shift, p.col_clamp, p.ck);
|
||||
let mut t = [0i32; 64];
|
||||
for j in 0..w {
|
||||
for i in 0..h {
|
||||
t[i] = residual[i * w + j];
|
||||
}
|
||||
if lossless {
|
||||
inverse_wht(&mut t, 0);
|
||||
} else {
|
||||
match ck {
|
||||
Kind1d::Dct => inverse_dct(&mut t, log2h, col_clamp),
|
||||
Kind1d::Adst => inverse_adst(&mut t, log2h, col_clamp),
|
||||
Kind1d::Identity => inverse_identity(&mut t, log2h),
|
||||
}
|
||||
}
|
||||
for i in 0..h {
|
||||
residual[i * w + j] = round2(t[i], col_shift);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Tx2d {
|
||||
w: usize,
|
||||
h: usize,
|
||||
log2w: u32,
|
||||
log2h: u32,
|
||||
row_shift: u32,
|
||||
col_shift: u32,
|
||||
row_clamp: u32,
|
||||
col_clamp: u32,
|
||||
rk: Kind1d,
|
||||
ck: Kind1d,
|
||||
rect2: bool,
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn transform_1d<T: Lane>(t: &mut [T], kind: Kind1d, n: u32, r: u32) {
|
||||
match kind {
|
||||
Kind1d::Dct => inverse_dct(t, n, r),
|
||||
Kind1d::Adst => inverse_adst(t, n, r),
|
||||
Kind1d::Identity => inverse_identity(t, n),
|
||||
}
|
||||
}
|
||||
|
||||
/// The 2D inverse transform with `L` rows (row pass) or columns (column pass) at a time;
|
||||
/// bit-identical to the one-at-a-time process for bit depths up to 10.
|
||||
fn transform_lanes<const L: usize>(dequant: &[i32], residual: &mut [i32], p: &Tx2d) {
|
||||
let mut t = [[0i32; L]; 64];
|
||||
lanes_row_pass(dequant, residual, p, &mut t);
|
||||
lanes_col_pass(residual, p, &mut t);
|
||||
}
|
||||
|
||||
fn lanes_row_pass<const L: usize>(dequant: &[i32], residual: &mut [i32], p: &Tx2d, t: &mut [[i32; L]; 64]) {
|
||||
let (w, h) = (p.w, p.h);
|
||||
let in_w = w.min(32);
|
||||
let lo = -(1i32 << (p.col_clamp - 1));
|
||||
let hi = (1i32 << (p.col_clamp - 1)) - 1;
|
||||
let mut i0 = 0;
|
||||
while i0 < h {
|
||||
// Rows without coefficients (and rows 32..63) transform to zeros.
|
||||
if i0 >= 32 || (i0..i0 + L).all(|i| dequant[i * 64..i * 64 + in_w].iter().all(|&v| v == 0)) {
|
||||
residual[i0 * w..(i0 + L) * w].fill(0);
|
||||
i0 += L;
|
||||
continue;
|
||||
}
|
||||
for (j, tj) in t[..w].iter_mut().enumerate() {
|
||||
for l in 0..L {
|
||||
tj[l] = if j < in_w { dequant[(i0 + l) * 64 + j] } else { 0 };
|
||||
}
|
||||
}
|
||||
if p.rect2 {
|
||||
for v in t[..w].iter_mut() {
|
||||
*v = v.mul_round12(2896);
|
||||
}
|
||||
}
|
||||
transform_1d(&mut t[..], p.rk, p.log2w, p.row_clamp);
|
||||
for l in 0..L {
|
||||
let out = &mut residual[(i0 + l) * w..(i0 + l + 1) * w];
|
||||
for j in 0..w {
|
||||
out[j] = round2(t[j][l], p.row_shift).clamp(lo, hi);
|
||||
}
|
||||
}
|
||||
i0 += L;
|
||||
}
|
||||
}
|
||||
|
||||
fn lanes_col_pass<const L: usize>(residual: &mut [i32], p: &Tx2d, t: &mut [[i32; L]; 64]) {
|
||||
let (w, h) = (p.w, p.h);
|
||||
let mut j0 = 0;
|
||||
while j0 < w {
|
||||
for i in 0..h {
|
||||
t[i].copy_from_slice(&residual[i * w + j0..i * w + j0 + L]);
|
||||
}
|
||||
transform_1d(&mut t[..], p.ck, p.log2h, p.col_clamp);
|
||||
for i in 0..h {
|
||||
let out = &mut residual[i * w + j0..i * w + j0 + L];
|
||||
for l in 0..L {
|
||||
out[l] = round2(t[i][l], p.col_shift);
|
||||
}
|
||||
}
|
||||
j0 += L;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The DCT step list must implement an orthogonal transform: a DC-only input gives a flat
|
||||
/// output equal to DC * cos(pi/4) (in the 2^12 fixed-point scale).
|
||||
#[test]
|
||||
fn dct_dc_is_flat() {
|
||||
for n in 2..=6 {
|
||||
let len = 1 << n;
|
||||
let mut t = [0i32; 64];
|
||||
t[0] = 4096;
|
||||
inverse_dct(&mut t, n, 24);
|
||||
for v in &t[..len] {
|
||||
assert_eq!(*v, 2896, "n={n}: {:?}", &t[..len]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Compare each inverse DCT basis vector with the real-valued DCT-III.
|
||||
#[test]
|
||||
fn dct_matches_float() {
|
||||
for n in 2..=6u32 {
|
||||
let len = 1usize << n;
|
||||
for k in 0..len {
|
||||
let mut t = [0i32; 64];
|
||||
t[k] = 1 << 12;
|
||||
inverse_dct(&mut t, n, 24);
|
||||
for x in 0..len {
|
||||
let c = if k == 0 { std::f64::consts::FRAC_1_SQRT_2 } else { 1.0 };
|
||||
let want = 4096.0 * c * ((2 * x + 1) as f64 * k as f64 * std::f64::consts::PI / (2 * len) as f64).cos();
|
||||
assert!((t[x] as f64 - want).abs() < 4.0 + 0.5 * n as f64, "n={n} k={k} x={x}: {} vs {want}", t[x]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// ADST8/16 basis vectors follow sin(pi (2i+1)(2k+1) / 4N) (scaled like the DCT).
|
||||
#[test]
|
||||
fn adst_basis_matches_sine() {
|
||||
for n in 3..=4u32 {
|
||||
let len = 1usize << n;
|
||||
for k in 0..len {
|
||||
let mut t = [0i32; 64];
|
||||
t[k] = 1 << 12;
|
||||
inverse_adst(&mut t, n, 24);
|
||||
for i in 0..len {
|
||||
let want = 4096.0 * ((std::f64::consts::PI * (2 * i + 1) as f64 * (2 * k + 1) as f64) / (4 * len) as f64).sin();
|
||||
assert!((t[i] as f64 - want).abs() < 8.0, "n={n} k={k} i={i}: {} vs {want:.1}", t[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adst_is_close_to_sine_transform() {
|
||||
for n in 3..=4u32 {
|
||||
let len = 1usize << n;
|
||||
for k in 0..len {
|
||||
let mut t = [0i32; 64];
|
||||
t[k] = 1 << 12;
|
||||
inverse_adst(&mut t, n, 24);
|
||||
let energy: f64 = t[..len].iter().map(|&v| (v as f64 / 4096.0).powi(2)).sum();
|
||||
// like the DCT, the AV1 ADST is scaled by sqrt(N / 2)
|
||||
assert!((energy - len as f64 / 2.0).abs() < 0.02, "n={n} k={k} energy {energy}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "deckcraft-bitstream"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-media = { workspace = true, features = ["bitstream"] }
|
||||
@@ -0,0 +1,82 @@
|
||||
//! Memory budget for decoded pictures, shared by the video decoders.
|
||||
//!
|
||||
//! A per-axis size cap does not bound memory: a decoder holds the current picture, every
|
||||
//! reference picture and the pictures still being decoded on worker threads. Each decoder checks
|
||||
//! the whole picture store against [`DECODE_MEMORY_BUDGET`] with checked arithmetic before it
|
||||
//! allocates for a new picture size, so a hostile header is an error, not an out-of-memory abort.
|
||||
|
||||
/// Bytes the decoded pictures of one decoder may take: 4 GiB on 64-bit targets, 1 GiB on 32-bit
|
||||
/// ones (wasm32 has a 4 GiB address space in all).
|
||||
pub const DECODE_MEMORY_BUDGET: u64 = if cfg!(target_pointer_width = "64") { 4 << 30 } else { 1 << 30 };
|
||||
|
||||
/// The shape of one decoded picture as a decoder allocates it.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct PictureShape {
|
||||
/// Allocated luma samples per row and rows (padding included).
|
||||
pub width: u64,
|
||||
pub height: u64,
|
||||
/// Chroma subsampling shifts (0 or 1); ignored when `chroma_planes` is 0.
|
||||
pub shift_x: u32,
|
||||
pub shift_y: u32,
|
||||
/// Chroma planes (0 for monochrome, 2 otherwise).
|
||||
pub chroma_planes: u64,
|
||||
/// Bytes per sample (1 for 8-bit buffers, 2 for 16-bit ones).
|
||||
pub bytes_per_sample: u64,
|
||||
}
|
||||
|
||||
impl PictureShape {
|
||||
/// Bytes of one picture, `None` if it does not fit in a `u64`.
|
||||
pub fn bytes(&self) -> Option<u64> {
|
||||
let luma = self.width.checked_mul(self.height)?;
|
||||
let cw = self.width.div_ceil(1 << self.shift_x.min(1));
|
||||
let ch = self.height.div_ceil(1 << self.shift_y.min(1));
|
||||
let chroma = cw.checked_mul(ch)?.checked_mul(self.chroma_planes)?;
|
||||
luma.checked_add(chroma)?.checked_mul(self.bytes_per_sample)
|
||||
}
|
||||
|
||||
/// Bytes of `pictures` such pictures, `None` on overflow.
|
||||
pub fn store_bytes(&self, pictures: u64) -> Option<u64> {
|
||||
self.bytes()?.checked_mul(pictures)
|
||||
}
|
||||
|
||||
/// Whether `pictures` such pictures fit in `budget` bytes.
|
||||
pub fn fits(&self, pictures: u64, budget: u64) -> bool {
|
||||
self.store_bytes(pictures).is_some_and(|b| b <= budget)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn shape(w: u64, h: u64) -> PictureShape {
|
||||
PictureShape { width: w, height: h, shift_x: 1, shift_y: 1, chroma_planes: 2, bytes_per_sample: 2 }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytes_count_luma_and_subsampled_chroma() {
|
||||
assert_eq!(shape(4, 4).bytes(), Some((16 + 2 * 4) * 2));
|
||||
// Odd sizes round chroma up.
|
||||
assert_eq!(shape(3, 3).bytes(), Some((9 + 2 * 4) * 2));
|
||||
let mono = PictureShape { chroma_planes: 0, bytes_per_sample: 1, ..shape(10, 10) };
|
||||
assert_eq!(mono.bytes(), Some(100));
|
||||
let full = PictureShape { shift_x: 0, shift_y: 0, ..shape(10, 10) };
|
||||
assert_eq!(full.bytes(), Some(600));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_budget_boundary() {
|
||||
let s = shape(4, 4); // 48 bytes
|
||||
assert!(s.fits(10, 480));
|
||||
assert!(!s.fits(10, 479));
|
||||
assert!(!s.fits(11, 480));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflow_is_never_within_budget() {
|
||||
let huge = shape(u64::MAX / 2, 3);
|
||||
assert_eq!(huge.bytes(), None);
|
||||
assert!(!huge.fits(1, u64::MAX));
|
||||
assert!(!shape(1 << 31, 1 << 31).fits(u64::MAX, u64::MAX));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
//! Bit-level reading and writing for DeckCraft's codec crates: a re-export of
|
||||
//! [`craft_media::bitstream`] from craft-libs, plus [`budget`].
|
||||
//!
|
||||
//! - [`BitReader`]: MSB-first reader with Exp-Golomb (`ue(v)`, `se(v)`), as used by H.264/HEVC/AV1 headers.
|
||||
//! - [`BitWriter`]: the matching writer (for encoders and header rewriting).
|
||||
//! - [`unescape_rbsp`] / [`escape_rbsp`]: remove/insert H.264/HEVC emulation-prevention bytes.
|
||||
//! - [`annexb_nals`] / [`length_prefixed_nals`]: split Annex-B byte streams and AVCC/HVCC samples into NAL units.
|
||||
//! - [`budget`]: the memory budget video decoders check their picture store against.
|
||||
|
||||
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))]
|
||||
|
||||
pub use craft_media::bitstream::*;
|
||||
|
||||
pub mod budget;
|
||||
@@ -9,4 +9,4 @@ description = "DeckCraft colour: RGB/HSL, theme colour slots, DrawingML-style co
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-drawingml = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
+753
-5
@@ -1,10 +1,758 @@
|
||||
//! DrawingML colours for DeckCraft: `Rgba`, scheme slots, colour schemes and colour transforms.
|
||||
//! They live in craft-libs (`craft_drawingml::color`, craft-libs#778 and #792), shared with the
|
||||
//! PPTX reader and writer; this crate re-exports them under the name the app uses.
|
||||
//! DeckCraft colour: sRGB colours, theme colour slots and DrawingML-style colour transforms
|
||||
//! (tint, shade, luminance modulation/offset, alpha…), so theme-relative colours resolve the way
|
||||
//! presentation files expect.
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
pub use craft_drawingml::color::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// An sRGB colour with straight alpha, components 0–255.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct Rgba {
|
||||
pub r: u8,
|
||||
pub g: u8,
|
||||
pub b: u8,
|
||||
pub a: u8,
|
||||
}
|
||||
|
||||
impl Default for Rgba {
|
||||
fn default() -> Self {
|
||||
Rgba::BLACK
|
||||
}
|
||||
}
|
||||
|
||||
impl Rgba {
|
||||
pub const BLACK: Rgba = Rgba::rgb(0, 0, 0);
|
||||
pub const WHITE: Rgba = Rgba::rgb(255, 255, 255);
|
||||
pub const TRANSPARENT: Rgba = Rgba { r: 0, g: 0, b: 0, a: 0 };
|
||||
|
||||
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
|
||||
Rgba { r, g, b, a: 255 }
|
||||
}
|
||||
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
|
||||
Rgba { r, g, b, a }
|
||||
}
|
||||
/// `RRGGBB` or `#RRGGBB` (also `RRGGBBAA`).
|
||||
pub fn from_hex(s: &str) -> Option<Self> {
|
||||
let s = s.trim();
|
||||
let s = s.strip_prefix('#').unwrap_or(s);
|
||||
if !s.bytes().all(|b| b.is_ascii_hexdigit()) {
|
||||
return None;
|
||||
}
|
||||
let byte = |i: usize| s.get(i..i + 2).and_then(|h| u8::from_str_radix(h, 16).ok());
|
||||
match s.len() {
|
||||
6 => Some(Rgba::rgb(byte(0)?, byte(2)?, byte(4)?)),
|
||||
8 => Some(Rgba::rgba(byte(0)?, byte(2)?, byte(4)?, byte(6)?)),
|
||||
3 => {
|
||||
let n = |i: usize| s.get(i..i + 1).and_then(|h| u8::from_str_radix(h, 16).ok()).map(|v| v * 17);
|
||||
Some(Rgba::rgb(n(0)?, n(1)?, n(2)?))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
/// `RRGGBB` uppercase (no alpha).
|
||||
pub fn hex(&self) -> String {
|
||||
format!("{:02X}{:02X}{:02X}", self.r, self.g, self.b)
|
||||
}
|
||||
pub fn with_alpha(self, a: u8) -> Self {
|
||||
Rgba { a, ..self }
|
||||
}
|
||||
pub fn to_f32(self) -> [f32; 4] {
|
||||
[self.r as f32 / 255.0, self.g as f32 / 255.0, self.b as f32 / 255.0, self.a as f32 / 255.0]
|
||||
}
|
||||
pub fn from_f64(r: f64, g: f64, b: f64, a: f64) -> Self {
|
||||
let c = |v: f64| if v.is_finite() { (v.clamp(0.0, 1.0) * 255.0).round() as u8 } else { 0 };
|
||||
Rgba { r: c(r), g: c(g), b: c(b), a: c(a) }
|
||||
}
|
||||
/// Relative luminance (WCAG), 0–1.
|
||||
pub fn luminance(self) -> f64 {
|
||||
let lin = |c: u8| {
|
||||
let v = c as f64 / 255.0;
|
||||
if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }
|
||||
};
|
||||
0.2126 * lin(self.r) + 0.7152 * lin(self.g) + 0.0722 * lin(self.b)
|
||||
}
|
||||
/// WCAG contrast ratio between two colours (1–21).
|
||||
pub fn contrast(self, other: Rgba) -> f64 {
|
||||
let (a, b) = (self.luminance(), other.luminance());
|
||||
let (hi, lo) = if a > b { (a, b) } else { (b, a) };
|
||||
(hi + 0.05) / (lo + 0.05)
|
||||
}
|
||||
pub fn to_hsl(self) -> (f64, f64, f64) {
|
||||
let r = self.r as f64 / 255.0;
|
||||
let g = self.g as f64 / 255.0;
|
||||
let b = self.b as f64 / 255.0;
|
||||
let max = r.max(g).max(b);
|
||||
let min = r.min(g).min(b);
|
||||
let l = (max + min) / 2.0;
|
||||
if (max - min).abs() < 1e-12 {
|
||||
return (0.0, 0.0, l);
|
||||
}
|
||||
let d = max - min;
|
||||
let s = if l > 0.5 { d / (2.0 - max - min) } else { d / (max + min) };
|
||||
let h = if (max - r).abs() < 1e-12 {
|
||||
(g - b) / d + if g < b { 6.0 } else { 0.0 }
|
||||
} else if (max - g).abs() < 1e-12 {
|
||||
(b - r) / d + 2.0
|
||||
} else {
|
||||
(r - g) / d + 4.0
|
||||
};
|
||||
(h * 60.0, s, l)
|
||||
}
|
||||
pub fn from_hsl(h: f64, s: f64, l: f64, a: u8) -> Self {
|
||||
let h = ((h % 360.0) + 360.0) % 360.0 / 360.0;
|
||||
let s = s.clamp(0.0, 1.0);
|
||||
let l = l.clamp(0.0, 1.0);
|
||||
if s == 0.0 {
|
||||
return Rgba::from_f64(l, l, l, a as f64 / 255.0);
|
||||
}
|
||||
let q = if l < 0.5 { l * (1.0 + s) } else { l + s - l * s };
|
||||
let p = 2.0 * l - q;
|
||||
let f = |t: f64| {
|
||||
let t = if t < 0.0 {
|
||||
t + 1.0
|
||||
} else if t > 1.0 {
|
||||
t - 1.0
|
||||
} else {
|
||||
t
|
||||
};
|
||||
if t < 1.0 / 6.0 {
|
||||
p + (q - p) * 6.0 * t
|
||||
} else if t < 0.5 {
|
||||
q
|
||||
} else if t < 2.0 / 3.0 {
|
||||
p + (q - p) * (2.0 / 3.0 - t) * 6.0
|
||||
} else {
|
||||
p
|
||||
}
|
||||
};
|
||||
Rgba::from_f64(f(h + 1.0 / 3.0), f(h), f(h - 1.0 / 3.0), a as f64 / 255.0)
|
||||
}
|
||||
/// Linear interpolation (straight alpha).
|
||||
pub fn lerp(self, o: Rgba, t: f64) -> Rgba {
|
||||
let t = if t.is_finite() { t.clamp(0.0, 1.0) } else { 0.0 };
|
||||
let m = |a: u8, b: u8| (a as f64 + (b as f64 - a as f64) * t).round().clamp(0.0, 255.0) as u8;
|
||||
Rgba { r: m(self.r, o.r), g: m(self.g, o.g), b: m(self.b, o.b), a: m(self.a, o.a) }
|
||||
}
|
||||
}
|
||||
|
||||
/// The twelve theme colour slots, plus the mapped aliases (`bg1`, `tx1`…) and `phClr` (the
|
||||
/// placeholder colour a style matrix entry is applied with).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum SchemeSlot {
|
||||
Dk1,
|
||||
Lt1,
|
||||
Dk2,
|
||||
Lt2,
|
||||
Accent1,
|
||||
Accent2,
|
||||
Accent3,
|
||||
Accent4,
|
||||
Accent5,
|
||||
Accent6,
|
||||
Hlink,
|
||||
FolHlink,
|
||||
Bg1,
|
||||
Tx1,
|
||||
Bg2,
|
||||
Tx2,
|
||||
PhClr,
|
||||
}
|
||||
|
||||
impl SchemeSlot {
|
||||
pub const THEME: [SchemeSlot; 12] = [
|
||||
SchemeSlot::Dk1,
|
||||
SchemeSlot::Lt1,
|
||||
SchemeSlot::Dk2,
|
||||
SchemeSlot::Lt2,
|
||||
SchemeSlot::Accent1,
|
||||
SchemeSlot::Accent2,
|
||||
SchemeSlot::Accent3,
|
||||
SchemeSlot::Accent4,
|
||||
SchemeSlot::Accent5,
|
||||
SchemeSlot::Accent6,
|
||||
SchemeSlot::Hlink,
|
||||
SchemeSlot::FolHlink,
|
||||
];
|
||||
pub fn xml_name(self) -> &'static str {
|
||||
match self {
|
||||
SchemeSlot::Dk1 => "dk1",
|
||||
SchemeSlot::Lt1 => "lt1",
|
||||
SchemeSlot::Dk2 => "dk2",
|
||||
SchemeSlot::Lt2 => "lt2",
|
||||
SchemeSlot::Accent1 => "accent1",
|
||||
SchemeSlot::Accent2 => "accent2",
|
||||
SchemeSlot::Accent3 => "accent3",
|
||||
SchemeSlot::Accent4 => "accent4",
|
||||
SchemeSlot::Accent5 => "accent5",
|
||||
SchemeSlot::Accent6 => "accent6",
|
||||
SchemeSlot::Hlink => "hlink",
|
||||
SchemeSlot::FolHlink => "folHlink",
|
||||
SchemeSlot::Bg1 => "bg1",
|
||||
SchemeSlot::Tx1 => "tx1",
|
||||
SchemeSlot::Bg2 => "bg2",
|
||||
SchemeSlot::Tx2 => "tx2",
|
||||
SchemeSlot::PhClr => "phClr",
|
||||
}
|
||||
}
|
||||
pub fn from_xml(s: &str) -> Option<Self> {
|
||||
Some(match s {
|
||||
"dk1" => SchemeSlot::Dk1,
|
||||
"lt1" => SchemeSlot::Lt1,
|
||||
"dk2" => SchemeSlot::Dk2,
|
||||
"lt2" => SchemeSlot::Lt2,
|
||||
"accent1" => SchemeSlot::Accent1,
|
||||
"accent2" => SchemeSlot::Accent2,
|
||||
"accent3" => SchemeSlot::Accent3,
|
||||
"accent4" => SchemeSlot::Accent4,
|
||||
"accent5" => SchemeSlot::Accent5,
|
||||
"accent6" => SchemeSlot::Accent6,
|
||||
"hlink" => SchemeSlot::Hlink,
|
||||
"folHlink" => SchemeSlot::FolHlink,
|
||||
"bg1" => SchemeSlot::Bg1,
|
||||
"tx1" => SchemeSlot::Tx1,
|
||||
"bg2" => SchemeSlot::Bg2,
|
||||
"tx2" => SchemeSlot::Tx2,
|
||||
"phClr" => SchemeSlot::PhClr,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
SchemeSlot::Dk1 | SchemeSlot::Tx1 => "Text/Background - Dark 1",
|
||||
SchemeSlot::Lt1 | SchemeSlot::Bg1 => "Text/Background - Light 1",
|
||||
SchemeSlot::Dk2 | SchemeSlot::Tx2 => "Text/Background - Dark 2",
|
||||
SchemeSlot::Lt2 | SchemeSlot::Bg2 => "Text/Background - Light 2",
|
||||
SchemeSlot::Accent1 => "Accent 1",
|
||||
SchemeSlot::Accent2 => "Accent 2",
|
||||
SchemeSlot::Accent3 => "Accent 3",
|
||||
SchemeSlot::Accent4 => "Accent 4",
|
||||
SchemeSlot::Accent5 => "Accent 5",
|
||||
SchemeSlot::Accent6 => "Accent 6",
|
||||
SchemeSlot::Hlink => "Hyperlink",
|
||||
SchemeSlot::FolHlink => "Followed Hyperlink",
|
||||
SchemeSlot::PhClr => "Placeholder colour",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A colour scheme: the twelve theme colours.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ColorScheme {
|
||||
pub name: String,
|
||||
pub colors: [Rgba; 12],
|
||||
}
|
||||
|
||||
impl ColorScheme {
|
||||
pub fn get(&self, slot: SchemeSlot) -> Rgba {
|
||||
let i = match slot {
|
||||
SchemeSlot::Dk1 | SchemeSlot::Tx1 => 0,
|
||||
SchemeSlot::Lt1 | SchemeSlot::Bg1 => 1,
|
||||
SchemeSlot::Dk2 | SchemeSlot::Tx2 => 2,
|
||||
SchemeSlot::Lt2 | SchemeSlot::Bg2 => 3,
|
||||
SchemeSlot::Accent1 => 4,
|
||||
SchemeSlot::Accent2 => 5,
|
||||
SchemeSlot::Accent3 => 6,
|
||||
SchemeSlot::Accent4 => 7,
|
||||
SchemeSlot::Accent5 => 8,
|
||||
SchemeSlot::Accent6 => 9,
|
||||
SchemeSlot::Hlink => 10,
|
||||
SchemeSlot::FolHlink => 11,
|
||||
SchemeSlot::PhClr => 4,
|
||||
};
|
||||
self.colors.get(i).copied().unwrap_or(Rgba::BLACK)
|
||||
}
|
||||
pub fn set(&mut self, slot: SchemeSlot, c: Rgba) {
|
||||
if let Some(i) = SchemeSlot::THEME.iter().position(|s| *s == slot)
|
||||
&& let Some(v) = self.colors.get_mut(i)
|
||||
{
|
||||
*v = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One colour transform, in file units (percentages ×1000, angles ×60000), applied in order.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", tag = "op", content = "val")]
|
||||
pub enum ColorTransform {
|
||||
Tint(i32),
|
||||
Shade(i32),
|
||||
LumMod(i32),
|
||||
LumOff(i32),
|
||||
SatMod(i32),
|
||||
SatOff(i32),
|
||||
HueMod(i32),
|
||||
HueOff(i32),
|
||||
Alpha(i32),
|
||||
AlphaMod(i32),
|
||||
AlphaOff(i32),
|
||||
Comp,
|
||||
Inv,
|
||||
Gray,
|
||||
RedMod(i32),
|
||||
GreenMod(i32),
|
||||
BlueMod(i32),
|
||||
}
|
||||
|
||||
impl ColorTransform {
|
||||
pub fn xml_name(&self) -> &'static str {
|
||||
match self {
|
||||
ColorTransform::Tint(_) => "tint",
|
||||
ColorTransform::Shade(_) => "shade",
|
||||
ColorTransform::LumMod(_) => "lumMod",
|
||||
ColorTransform::LumOff(_) => "lumOff",
|
||||
ColorTransform::SatMod(_) => "satMod",
|
||||
ColorTransform::SatOff(_) => "satOff",
|
||||
ColorTransform::HueMod(_) => "hueMod",
|
||||
ColorTransform::HueOff(_) => "hueOff",
|
||||
ColorTransform::Alpha(_) => "alpha",
|
||||
ColorTransform::AlphaMod(_) => "alphaMod",
|
||||
ColorTransform::AlphaOff(_) => "alphaOff",
|
||||
ColorTransform::Comp => "comp",
|
||||
ColorTransform::Inv => "inv",
|
||||
ColorTransform::Gray => "gray",
|
||||
ColorTransform::RedMod(_) => "redMod",
|
||||
ColorTransform::GreenMod(_) => "greenMod",
|
||||
ColorTransform::BlueMod(_) => "blueMod",
|
||||
}
|
||||
}
|
||||
pub fn value(&self) -> Option<i32> {
|
||||
match *self {
|
||||
ColorTransform::Tint(v)
|
||||
| ColorTransform::Shade(v)
|
||||
| ColorTransform::LumMod(v)
|
||||
| ColorTransform::LumOff(v)
|
||||
| ColorTransform::SatMod(v)
|
||||
| ColorTransform::SatOff(v)
|
||||
| ColorTransform::HueMod(v)
|
||||
| ColorTransform::HueOff(v)
|
||||
| ColorTransform::Alpha(v)
|
||||
| ColorTransform::AlphaMod(v)
|
||||
| ColorTransform::AlphaOff(v)
|
||||
| ColorTransform::RedMod(v)
|
||||
| ColorTransform::GreenMod(v)
|
||||
| ColorTransform::BlueMod(v) => Some(v),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub fn from_xml(name: &str, val: Option<i32>) -> Option<Self> {
|
||||
let v = val.unwrap_or(0);
|
||||
Some(match name {
|
||||
"tint" => ColorTransform::Tint(v),
|
||||
"shade" => ColorTransform::Shade(v),
|
||||
"lumMod" => ColorTransform::LumMod(v),
|
||||
"lumOff" => ColorTransform::LumOff(v),
|
||||
"satMod" => ColorTransform::SatMod(v),
|
||||
"satOff" => ColorTransform::SatOff(v),
|
||||
"hueMod" => ColorTransform::HueMod(v),
|
||||
"hueOff" => ColorTransform::HueOff(v),
|
||||
"alpha" => ColorTransform::Alpha(v),
|
||||
"alphaMod" => ColorTransform::AlphaMod(v),
|
||||
"alphaOff" => ColorTransform::AlphaOff(v),
|
||||
"comp" => ColorTransform::Comp,
|
||||
"inv" => ColorTransform::Inv,
|
||||
"gray" => ColorTransform::Gray,
|
||||
"redMod" => ColorTransform::RedMod(v),
|
||||
"greenMod" => ColorTransform::GreenMod(v),
|
||||
"blueMod" => ColorTransform::BlueMod(v),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply transforms to a base colour.
|
||||
pub fn apply(base: Rgba, transforms: &[ColorTransform]) -> Rgba {
|
||||
let mut c = base;
|
||||
for t in transforms {
|
||||
c = apply_one(c, *t);
|
||||
}
|
||||
c
|
||||
}
|
||||
|
||||
fn pct(v: i32) -> f64 {
|
||||
v as f64 / 100_000.0
|
||||
}
|
||||
|
||||
fn apply_one(c: Rgba, t: ColorTransform) -> Rgba {
|
||||
match t {
|
||||
// tint: towards white by (1 - value); shade: towards black by (1 - value). Office applies
|
||||
// these in linear light, which keeps mid-tones from washing out.
|
||||
ColorTransform::Tint(v) => {
|
||||
let k = pct(v).clamp(0.0, 1.0);
|
||||
map_linear(c, |x| x * k + (1.0 - k))
|
||||
}
|
||||
ColorTransform::Shade(v) => {
|
||||
let k = pct(v).clamp(0.0, 1.0);
|
||||
map_linear(c, |x| x * k)
|
||||
}
|
||||
ColorTransform::LumMod(v) => {
|
||||
let (h, s, l) = c.to_hsl();
|
||||
Rgba::from_hsl(h, s, l * pct(v), c.a)
|
||||
}
|
||||
ColorTransform::LumOff(v) => {
|
||||
let (h, s, l) = c.to_hsl();
|
||||
Rgba::from_hsl(h, s, l + pct(v), c.a)
|
||||
}
|
||||
ColorTransform::SatMod(v) => {
|
||||
let (h, s, l) = c.to_hsl();
|
||||
Rgba::from_hsl(h, s * pct(v), l, c.a)
|
||||
}
|
||||
ColorTransform::SatOff(v) => {
|
||||
let (h, s, l) = c.to_hsl();
|
||||
Rgba::from_hsl(h, s + pct(v), l, c.a)
|
||||
}
|
||||
ColorTransform::HueMod(v) => {
|
||||
let (h, s, l) = c.to_hsl();
|
||||
Rgba::from_hsl(h * pct(v), s, l, c.a)
|
||||
}
|
||||
ColorTransform::HueOff(v) => {
|
||||
let (h, s, l) = c.to_hsl();
|
||||
Rgba::from_hsl(h + v as f64 / 60_000.0, s, l, c.a)
|
||||
}
|
||||
ColorTransform::Alpha(v) => c.with_alpha((pct(v).clamp(0.0, 1.0) * 255.0).round() as u8),
|
||||
ColorTransform::AlphaMod(v) => c.with_alpha((c.a as f64 * pct(v)).round().clamp(0.0, 255.0) as u8),
|
||||
ColorTransform::AlphaOff(v) => c.with_alpha((c.a as f64 + pct(v) * 255.0).round().clamp(0.0, 255.0) as u8),
|
||||
ColorTransform::Comp => {
|
||||
let (h, s, l) = c.to_hsl();
|
||||
Rgba::from_hsl(h + 180.0, s, l, c.a)
|
||||
}
|
||||
ColorTransform::Inv => Rgba { r: 255 - c.r, g: 255 - c.g, b: 255 - c.b, a: c.a },
|
||||
ColorTransform::Gray => {
|
||||
let y = (0.299 * c.r as f64 + 0.587 * c.g as f64 + 0.114 * c.b as f64).round().clamp(0.0, 255.0) as u8;
|
||||
Rgba { r: y, g: y, b: y, a: c.a }
|
||||
}
|
||||
ColorTransform::RedMod(v) => Rgba { r: (c.r as f64 * pct(v)).round().clamp(0.0, 255.0) as u8, ..c },
|
||||
ColorTransform::GreenMod(v) => Rgba { g: (c.g as f64 * pct(v)).round().clamp(0.0, 255.0) as u8, ..c },
|
||||
ColorTransform::BlueMod(v) => Rgba { b: (c.b as f64 * pct(v)).round().clamp(0.0, 255.0) as u8, ..c },
|
||||
}
|
||||
}
|
||||
|
||||
fn to_lin(v: u8) -> f64 {
|
||||
let v = v as f64 / 255.0;
|
||||
if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }
|
||||
}
|
||||
fn from_lin(v: f64) -> f64 {
|
||||
let v = v.clamp(0.0, 1.0);
|
||||
if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 }
|
||||
}
|
||||
fn map_linear(c: Rgba, f: impl Fn(f64) -> f64) -> Rgba {
|
||||
Rgba::from_f64(from_lin(f(to_lin(c.r))), from_lin(f(to_lin(c.g))), from_lin(f(to_lin(c.b))), c.a as f64 / 255.0)
|
||||
}
|
||||
|
||||
/// The colour-picker grid shown under each theme colour: lighter 80/60/40%, darker 25/50%
|
||||
/// (for dark colours: lighter 90/75/50/25/10%).
|
||||
pub fn theme_variants(c: Rgba) -> [(Vec<ColorTransform>, &'static str); 5] {
|
||||
let (_, _, l) = c.to_hsl();
|
||||
if l < 0.2 {
|
||||
[
|
||||
(vec![ColorTransform::LumMod(10000), ColorTransform::LumOff(90000)], "Lighter 90%"),
|
||||
(vec![ColorTransform::LumMod(25000), ColorTransform::LumOff(75000)], "Lighter 75%"),
|
||||
(vec![ColorTransform::LumMod(50000), ColorTransform::LumOff(50000)], "Lighter 50%"),
|
||||
(vec![ColorTransform::LumMod(75000), ColorTransform::LumOff(25000)], "Lighter 25%"),
|
||||
(vec![ColorTransform::LumMod(90000), ColorTransform::LumOff(10000)], "Lighter 10%"),
|
||||
]
|
||||
} else if l > 0.8 {
|
||||
[
|
||||
(vec![ColorTransform::LumMod(95000)], "Darker 5%"),
|
||||
(vec![ColorTransform::LumMod(85000)], "Darker 15%"),
|
||||
(vec![ColorTransform::LumMod(75000)], "Darker 25%"),
|
||||
(vec![ColorTransform::LumMod(65000)], "Darker 35%"),
|
||||
(vec![ColorTransform::LumMod(50000)], "Darker 50%"),
|
||||
]
|
||||
} else {
|
||||
[
|
||||
(vec![ColorTransform::LumMod(20000), ColorTransform::LumOff(80000)], "Lighter 80%"),
|
||||
(vec![ColorTransform::LumMod(40000), ColorTransform::LumOff(60000)], "Lighter 60%"),
|
||||
(vec![ColorTransform::LumMod(60000), ColorTransform::LumOff(40000)], "Lighter 40%"),
|
||||
(vec![ColorTransform::LumMod(75000)], "Darker 25%"),
|
||||
(vec![ColorTransform::LumMod(50000)], "Darker 50%"),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
/// Standard colours row of the colour picker.
|
||||
pub const STANDARD_COLORS: [(Rgba, &str); 10] = [
|
||||
(Rgba::rgb(0xC0, 0x00, 0x00), "Dark Red"),
|
||||
(Rgba::rgb(0xFF, 0x00, 0x00), "Red"),
|
||||
(Rgba::rgb(0xFF, 0xC0, 0x00), "Orange"),
|
||||
(Rgba::rgb(0xFF, 0xFF, 0x00), "Yellow"),
|
||||
(Rgba::rgb(0x92, 0xD0, 0x50), "Light Green"),
|
||||
(Rgba::rgb(0x00, 0xB0, 0x50), "Green"),
|
||||
(Rgba::rgb(0x00, 0xB0, 0xF0), "Light Blue"),
|
||||
(Rgba::rgb(0x00, 0x70, 0xC0), "Blue"),
|
||||
(Rgba::rgb(0x00, 0x20, 0x60), "Dark Blue"),
|
||||
(Rgba::rgb(0x70, 0x30, 0xA0), "Purple"),
|
||||
];
|
||||
|
||||
/// A few CSS-like preset colour names (`prstClr`).
|
||||
pub fn preset(name: &str) -> Option<Rgba> {
|
||||
let c = match name {
|
||||
"black" => (0, 0, 0),
|
||||
"white" => (255, 255, 255),
|
||||
"red" => (255, 0, 0),
|
||||
"green" => (0, 128, 0),
|
||||
"lime" => (0, 255, 0),
|
||||
"blue" => (0, 0, 255),
|
||||
"yellow" => (255, 255, 0),
|
||||
"cyan" | "aqua" => (0, 255, 255),
|
||||
"magenta" | "fuchsia" => (255, 0, 255),
|
||||
"gray" | "grey" => (128, 128, 128),
|
||||
"silver" => (192, 192, 192),
|
||||
"maroon" => (128, 0, 0),
|
||||
"navy" => (0, 0, 128),
|
||||
"olive" => (128, 128, 0),
|
||||
"purple" => (128, 0, 128),
|
||||
"teal" => (0, 128, 128),
|
||||
"orange" => (255, 165, 0),
|
||||
"pink" => (255, 192, 203),
|
||||
"brown" => (165, 42, 42),
|
||||
"gold" => (255, 215, 0),
|
||||
"ltGray" | "lightGray" => (211, 211, 211),
|
||||
"dkGray" | "darkGray" => (169, 169, 169),
|
||||
_ => return None,
|
||||
};
|
||||
Some(Rgba::rgb(c.0, c.1, c.2))
|
||||
}
|
||||
|
||||
/// Every preset colour name a file may write (`ST_PresetColorVal`, with the later editions' aliases).
|
||||
pub const PRESET_NAMES: [&str; 190] = [
|
||||
"aliceBlue",
|
||||
"antiqueWhite",
|
||||
"aqua",
|
||||
"aquamarine",
|
||||
"azure",
|
||||
"beige",
|
||||
"bisque",
|
||||
"black",
|
||||
"blanchedAlmond",
|
||||
"blue",
|
||||
"blueViolet",
|
||||
"brown",
|
||||
"burlyWood",
|
||||
"cadetBlue",
|
||||
"chartreuse",
|
||||
"chocolate",
|
||||
"coral",
|
||||
"cornflowerBlue",
|
||||
"cornsilk",
|
||||
"crimson",
|
||||
"cyan",
|
||||
"darkBlue",
|
||||
"darkCyan",
|
||||
"darkGoldenrod",
|
||||
"darkGray",
|
||||
"darkGrey",
|
||||
"darkGreen",
|
||||
"darkKhaki",
|
||||
"darkMagenta",
|
||||
"darkOliveGreen",
|
||||
"darkOrange",
|
||||
"darkOrchid",
|
||||
"darkRed",
|
||||
"darkSalmon",
|
||||
"darkSeaGreen",
|
||||
"darkSlateBlue",
|
||||
"darkSlateGray",
|
||||
"darkSlateGrey",
|
||||
"darkTurquoise",
|
||||
"darkViolet",
|
||||
"dkBlue",
|
||||
"dkCyan",
|
||||
"dkGoldenrod",
|
||||
"dkGray",
|
||||
"dkGrey",
|
||||
"dkGreen",
|
||||
"dkKhaki",
|
||||
"dkMagenta",
|
||||
"dkOliveGreen",
|
||||
"dkOrange",
|
||||
"dkOrchid",
|
||||
"dkRed",
|
||||
"dkSalmon",
|
||||
"dkSeaGreen",
|
||||
"dkSlateBlue",
|
||||
"dkSlateGray",
|
||||
"dkSlateGrey",
|
||||
"dkTurquoise",
|
||||
"dkViolet",
|
||||
"deepPink",
|
||||
"deepSkyBlue",
|
||||
"dimGray",
|
||||
"dimGrey",
|
||||
"dodgerBlue",
|
||||
"firebrick",
|
||||
"floralWhite",
|
||||
"forestGreen",
|
||||
"fuchsia",
|
||||
"gainsboro",
|
||||
"ghostWhite",
|
||||
"gold",
|
||||
"goldenrod",
|
||||
"gray",
|
||||
"grey",
|
||||
"green",
|
||||
"greenYellow",
|
||||
"honeydew",
|
||||
"hotPink",
|
||||
"indianRed",
|
||||
"indigo",
|
||||
"ivory",
|
||||
"khaki",
|
||||
"lavender",
|
||||
"lavenderBlush",
|
||||
"lawnGreen",
|
||||
"lemonChiffon",
|
||||
"lightBlue",
|
||||
"lightCoral",
|
||||
"lightCyan",
|
||||
"lightGoldenrodYellow",
|
||||
"lightGray",
|
||||
"lightGrey",
|
||||
"lightGreen",
|
||||
"lightPink",
|
||||
"lightSalmon",
|
||||
"lightSeaGreen",
|
||||
"lightSkyBlue",
|
||||
"lightSlateGray",
|
||||
"lightSlateGrey",
|
||||
"lightSteelBlue",
|
||||
"lightYellow",
|
||||
"ltBlue",
|
||||
"ltCoral",
|
||||
"ltCyan",
|
||||
"ltGoldenrodYellow",
|
||||
"ltGray",
|
||||
"ltGrey",
|
||||
"ltGreen",
|
||||
"ltPink",
|
||||
"ltSalmon",
|
||||
"ltSeaGreen",
|
||||
"ltSkyBlue",
|
||||
"ltSlateGray",
|
||||
"ltSlateGrey",
|
||||
"ltSteelBlue",
|
||||
"ltYellow",
|
||||
"lime",
|
||||
"limeGreen",
|
||||
"linen",
|
||||
"magenta",
|
||||
"maroon",
|
||||
"medAquamarine",
|
||||
"medBlue",
|
||||
"medOrchid",
|
||||
"medPurple",
|
||||
"medSeaGreen",
|
||||
"medSlateBlue",
|
||||
"medSpringGreen",
|
||||
"medTurquoise",
|
||||
"medVioletRed",
|
||||
"mediumAquamarine",
|
||||
"mediumBlue",
|
||||
"mediumOrchid",
|
||||
"mediumPurple",
|
||||
"mediumSeaGreen",
|
||||
"mediumSlateBlue",
|
||||
"mediumSpringGreen",
|
||||
"mediumTurquoise",
|
||||
"mediumVioletRed",
|
||||
"midnightBlue",
|
||||
"mintCream",
|
||||
"mistyRose",
|
||||
"moccasin",
|
||||
"navajoWhite",
|
||||
"navy",
|
||||
"oldLace",
|
||||
"olive",
|
||||
"oliveDrab",
|
||||
"orange",
|
||||
"orangeRed",
|
||||
"orchid",
|
||||
"paleGoldenrod",
|
||||
"paleGreen",
|
||||
"paleTurquoise",
|
||||
"paleVioletRed",
|
||||
"papayaWhip",
|
||||
"peachPuff",
|
||||
"peru",
|
||||
"pink",
|
||||
"plum",
|
||||
"powderBlue",
|
||||
"purple",
|
||||
"red",
|
||||
"rosyBrown",
|
||||
"royalBlue",
|
||||
"saddleBrown",
|
||||
"salmon",
|
||||
"sandyBrown",
|
||||
"seaGreen",
|
||||
"seaShell",
|
||||
"sienna",
|
||||
"silver",
|
||||
"skyBlue",
|
||||
"slateBlue",
|
||||
"slateGray",
|
||||
"slateGrey",
|
||||
"snow",
|
||||
"springGreen",
|
||||
"steelBlue",
|
||||
"tan",
|
||||
"teal",
|
||||
"thistle",
|
||||
"tomato",
|
||||
"turquoise",
|
||||
"violet",
|
||||
"wheat",
|
||||
"white",
|
||||
"whiteSmoke",
|
||||
"yellow",
|
||||
"yellowGreen",
|
||||
];
|
||||
|
||||
/// Every system colour name a file may write (`ST_SystemColorVal`).
|
||||
pub const SYSTEM_NAMES: [&str; 30] = [
|
||||
"scrollBar",
|
||||
"background",
|
||||
"activeCaption",
|
||||
"inactiveCaption",
|
||||
"menu",
|
||||
"window",
|
||||
"windowFrame",
|
||||
"menuText",
|
||||
"windowText",
|
||||
"captionText",
|
||||
"activeBorder",
|
||||
"inactiveBorder",
|
||||
"appWorkspace",
|
||||
"highlight",
|
||||
"highlightText",
|
||||
"btnFace",
|
||||
"btnShadow",
|
||||
"grayText",
|
||||
"btnText",
|
||||
"inactiveCaptionText",
|
||||
"btnHighlight",
|
||||
"3dDkShadow",
|
||||
"3dLight",
|
||||
"infoText",
|
||||
"infoBk",
|
||||
"hotLight",
|
||||
"gradientActiveCaption",
|
||||
"gradientInactiveCaption",
|
||||
"menuHighlight",
|
||||
"menuBar",
|
||||
];
|
||||
|
||||
/// Whether `name` is a preset colour name a file may write.
|
||||
pub fn is_preset_name(name: &str) -> bool {
|
||||
PRESET_NAMES.contains(&name)
|
||||
}
|
||||
|
||||
/// Whether `name` is a system colour name a file may write.
|
||||
pub fn is_system_name(name: &str) -> bool {
|
||||
SYSTEM_NAMES.contains(&name)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
@@ -72,7 +820,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn scheme_slots() {
|
||||
let mut s = ColorScheme::new("t", [Rgba::BLACK; 12]);
|
||||
let mut s = ColorScheme { name: "t".into(), colors: [Rgba::BLACK; 12] };
|
||||
s.set(SchemeSlot::Accent3, Rgba::WHITE);
|
||||
assert_eq!(s.get(SchemeSlot::Accent3), Rgba::WHITE);
|
||||
assert_eq!(s.get(SchemeSlot::Tx1), Rgba::BLACK);
|
||||
|
||||
@@ -9,7 +9,6 @@ description = "DeckCraft engine: sessions, undo history, selection, the command
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-mime = { workspace = true }
|
||||
deckcraft-geom = { workspace = true }
|
||||
deckcraft-color = { workspace = true }
|
||||
deckcraft-model = { workspace = true }
|
||||
|
||||
+31
-122
@@ -433,15 +433,6 @@ pub(crate) fn shape_of(s: &Session, id: ShapeId) -> Result<deckcraft_model::Shap
|
||||
pub fn xfrm_of(doc: &deckcraft_model::Presentation, sel: &crate::Selection, shape: &deckcraft_model::Shape) -> Xfrm {
|
||||
let mut x = shape.xfrm.unwrap_or_else(|| ctx_xfrm(doc, sel, shape));
|
||||
if let deckcraft_model::ShapeKind::Table(t) = &shape.kind {
|
||||
// A loaded table may not be normalized yet: measure the bounded grid an edit will
|
||||
// leave, not every cell the file stored.
|
||||
let normalized;
|
||||
let t = if t.is_normalized() {
|
||||
t
|
||||
} else {
|
||||
normalized = t.normalized();
|
||||
&normalized
|
||||
};
|
||||
let drawn: f64 = table_drawn_heights(doc, sel, t).iter().sum();
|
||||
if drawn.is_finite() && drawn > x.h {
|
||||
x.h = drawn;
|
||||
@@ -616,133 +607,51 @@ fn param_optional(alt: &str) -> Option<bool> {
|
||||
|
||||
/// Base64 (standard alphabet) for binary payloads in JSON params and results.
|
||||
pub fn base64_encode(data: &[u8]) -> String {
|
||||
craft_mime::base64::STANDARD.encode(data)
|
||||
const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
|
||||
for chunk in data.chunks(3) {
|
||||
let b = [chunk.first().copied().unwrap_or(0), chunk.get(1).copied().unwrap_or(0), chunk.get(2).copied().unwrap_or(0)];
|
||||
let n = ((b[0] as u32) << 16) | ((b[1] as u32) << 8) | b[2] as u32;
|
||||
for (i, shift) in [18, 12, 6, 0].iter().enumerate() {
|
||||
if i <= chunk.len() {
|
||||
out.push(A.get(((n >> shift) & 63) as usize).copied().unwrap_or(b'A') as char);
|
||||
} else {
|
||||
out.push('=');
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub fn base64_decode(s: &str) -> Option<Vec<u8>> {
|
||||
let s = s.split_once("base64,").map(|(_, b)| b).unwrap_or(s);
|
||||
// Preserve the command transport's admission rules before the shared legacy decoder:
|
||||
// padding ends the payload and one trailing symbol cannot encode a byte.
|
||||
let (mut symbols_mod_four, mut padded) = (0u8, false);
|
||||
let mut out = Vec::with_capacity(s.len() / 4 * 3);
|
||||
let mut acc = 0u32;
|
||||
let mut bits = 0;
|
||||
for c in s.bytes() {
|
||||
match c {
|
||||
b'=' => padded = true,
|
||||
b'\n' | b'\r' | b' ' => {}
|
||||
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'+' | b'-' | b'/' | b'_' if !padded => {
|
||||
symbols_mod_four = (symbols_mod_four + 1) % 4;
|
||||
}
|
||||
let v = match c {
|
||||
b'A'..=b'Z' => c - b'A',
|
||||
b'a'..=b'z' => c - b'a' + 26,
|
||||
b'0'..=b'9' => c - b'0' + 52,
|
||||
b'+' | b'-' => 62,
|
||||
b'/' | b'_' => 63,
|
||||
b'=' | b'\n' | b'\r' | b' ' => continue,
|
||||
_ => return None,
|
||||
} as u32;
|
||||
acc = (acc << 6) | v;
|
||||
bits += 6;
|
||||
if bits >= 8 {
|
||||
bits -= 8;
|
||||
out.push(((acc >> bits) & 0xff) as u8);
|
||||
}
|
||||
}
|
||||
if symbols_mod_four == 1 {
|
||||
return None;
|
||||
}
|
||||
craft_mime::base64_compat::decode(s).ok()
|
||||
Some(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Frozen pre-cutover implementation (Brandon Thomas, with Mex's padding/tail fixes).
|
||||
fn previous_base64_decode(s: &str) -> Option<Vec<u8>> {
|
||||
let s = s.split_once("base64,").map(|(_, b)| b).unwrap_or(s);
|
||||
let mut out = Vec::with_capacity(s.len() / 4 * 3);
|
||||
let mut acc = 0u32;
|
||||
let mut bits = 0;
|
||||
let mut padded = false;
|
||||
for c in s.bytes() {
|
||||
let v = match c {
|
||||
b'A'..=b'Z' => c - b'A',
|
||||
b'a'..=b'z' => c - b'a' + 26,
|
||||
b'0'..=b'9' => c - b'0' + 52,
|
||||
b'+' | b'-' => 62,
|
||||
b'/' | b'_' => 63,
|
||||
b'=' => {
|
||||
padded = true;
|
||||
continue;
|
||||
}
|
||||
b'\n' | b'\r' | b' ' => continue,
|
||||
_ => return None,
|
||||
} as u32;
|
||||
// Padding ends the payload; no further data may be decoded.
|
||||
if padded {
|
||||
return None;
|
||||
}
|
||||
acc = (acc << 6) | v;
|
||||
bits += 6;
|
||||
if bits >= 8 {
|
||||
bits -= 8;
|
||||
out.push(((acc >> bits) & 0xff) as u8);
|
||||
}
|
||||
}
|
||||
// A trailing lone Base64 character cannot encode another byte.
|
||||
(bits != 6).then_some(out)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_decoder_preserves_command_transport_admission() {
|
||||
for text in ["", "=", "Zg== \r\n==", "Zg==A", "A", "AAAAA", "AB", "+_8=", "Z=m9v", "Zm9v\t", "Zm9v!", "data:text/plain;base64,Zm9v"] {
|
||||
assert_eq!(base64_decode(text), previous_base64_decode(text), "{text:?}");
|
||||
}
|
||||
let alphabet = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/-_= \r\n!\t";
|
||||
let mut seed = 0x1234_5678u32;
|
||||
for n in 0..50_000 {
|
||||
let mut text = String::new();
|
||||
for _ in 0..n % 65 {
|
||||
seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
|
||||
text.push(char::from(alphabet[(seed as usize) % alphabet.len()]));
|
||||
}
|
||||
assert_eq!(base64_decode(&text), previous_base64_decode(&text), "{text:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_encoder_preserves_rfc4648_and_all_byte_remainders() {
|
||||
for (input, expected) in
|
||||
[("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")]
|
||||
{
|
||||
assert_eq!(base64_encode(input.as_bytes()), expected);
|
||||
}
|
||||
// The decoder accepts both alphabets, so pin the standard one explicitly.
|
||||
assert_eq!(base64_encode(&[0xfb, 0xff]), "+/8=");
|
||||
let all: Vec<u8> = (0..=255).cycle().take(513).collect();
|
||||
for len in 0..=all.len() {
|
||||
let encoded = base64_encode(&all[..len]);
|
||||
assert_eq!(encoded.len(), len.div_ceil(3) * 4);
|
||||
assert_eq!(encoded.bytes().rev().take_while(|b| *b == b'=').count(), (3 - len % 3) % 3);
|
||||
assert_eq!(base64_decode(&encoded).as_deref(), Some(&all[..len]));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn base64_rejects_a_single_character_tail() {
|
||||
for (encoded, decoded) in [
|
||||
("", b"".as_slice()),
|
||||
("Zg==", b"f"),
|
||||
("Zg", b"f"),
|
||||
("Zm8=", b"fo"),
|
||||
("Zm8", b"fo"),
|
||||
(" Zm9v \r\n", b"foo"),
|
||||
("data:text/plain;base64,Zm9v", b"foo"),
|
||||
("+/8=", &[0xfb, 0xff]),
|
||||
("-_8=", &[0xfb, 0xff]),
|
||||
] {
|
||||
assert_eq!(base64_decode(encoded).as_deref(), Some(decoded), "{encoded:?}");
|
||||
}
|
||||
for encoded in ["A", "AAAAA", "Zm9vZ", " Zm9vZ \r\n", "data:text/plain;base64,Z"] {
|
||||
assert!(base64_decode(encoded).is_none(), "{encoded:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn base64_rejects_data_after_padding() {
|
||||
assert_eq!(base64_decode("Zg== \r\n=="), Some(b"f".to_vec()));
|
||||
for encoded in ["Zg==A", "Zg== Zm8=", "Zm8=A", "=Zg", "Zg==\r\nA", "data:text/plain;base64,Zg==A"] {
|
||||
assert!(base64_decode(encoded).is_none(), "{encoded:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_palette_skips_a_command_that_needs_a_value() {
|
||||
assert!(needs_arguments("{path}"));
|
||||
|
||||
@@ -365,38 +365,6 @@ fn ungroup(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
out.push(sh);
|
||||
}
|
||||
*list = out;
|
||||
// A group inside a group: its children take its place in the outer group, mapped into the
|
||||
// outer group's child space, where the inner group's own box is.
|
||||
for id in &ids {
|
||||
let Some(siblings) = deckcraft_model::sibling_list_mut(list, *id) else { continue };
|
||||
let Some(at) = siblings.iter().position(|x| x.id == *id) else { continue };
|
||||
let freed: Vec<Shape> = match siblings.get(at) {
|
||||
Some(Shape { kind: ShapeKind::Group { children, child }, xfrm: Some(gx), .. }) => children
|
||||
.iter()
|
||||
.map(|c| {
|
||||
let mut c = c.clone();
|
||||
if let Some(cx) = c.xfrm {
|
||||
c.xfrm = Some(child_to_parent(gx, child, &cx));
|
||||
}
|
||||
c
|
||||
})
|
||||
.collect(),
|
||||
_ => continue,
|
||||
};
|
||||
let freed_ids: Vec<ShapeId> = freed.iter().map(|c| c.id).collect();
|
||||
siblings.splice(at..=at, freed);
|
||||
// Ungrouped along with an outer group, it was listed among that group's children (once per expanded ancestor).
|
||||
match new_sel.iter().position(|x| x == id) {
|
||||
Some(i) => {
|
||||
new_sel.splice(i..=i, freed_ids);
|
||||
new_sel.retain(|x| x != id);
|
||||
}
|
||||
None => new_sel.extend(freed_ids),
|
||||
}
|
||||
}
|
||||
// A shape freed from a named group and then from a named group around it is listed once.
|
||||
let mut listed = std::collections::HashSet::new();
|
||||
new_sel.retain(|x| listed.insert(*x));
|
||||
sel.shapes = new_sel.clone();
|
||||
Ok(json!({"ids": new_sel}))
|
||||
})
|
||||
@@ -417,28 +385,17 @@ fn regroup(s: &mut Session, _p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
|
||||
/// `to` of arrange.align and arrange.distribute: `Some(true)` for the slide, `Some(false)` for the
|
||||
/// selection, `None` when it is left out. Any other value, a non-string included, is refused.
|
||||
fn align_to(cmd: &str, p: &Value) -> Result<Option<bool>> {
|
||||
match p.get("to").map(Value::as_str) {
|
||||
None => Ok(None),
|
||||
Some(Some("slide")) => Ok(Some(true)),
|
||||
Some(Some("selection")) => Ok(Some(false)),
|
||||
Some(_) => Err(bad(cmd, "`to` must be slide or selection")),
|
||||
}
|
||||
}
|
||||
|
||||
fn align(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let edge = match str_param(p, "edge") {
|
||||
Some(e @ ("left" | "center" | "right" | "top" | "middle" | "bottom")) => e.to_string(),
|
||||
_ => return Err(bad("arrange.align", "`edge` must be left, center, right, top, middle or bottom")),
|
||||
};
|
||||
let to = align_to("arrange.align", p)?;
|
||||
let edge = str_param(p, "edge").unwrap_or("left").to_string();
|
||||
let ids = targets(s, p)?;
|
||||
let st = s.doc()?;
|
||||
let size = st.doc.slide_size;
|
||||
let boxes: Vec<(ShapeId, Xfrm)> = ids.iter().filter_map(|id| st.shape(*id).map(|x| (*id, xfrm_of(&st.doc, &st.selection, x)))).collect();
|
||||
let to_slide = to.unwrap_or(boxes.len() < 2);
|
||||
let to_slide = match str_param(p, "to") {
|
||||
Some("slide") => true,
|
||||
Some(_) => false,
|
||||
None => boxes.len() < 2,
|
||||
};
|
||||
let reference = if to_slide {
|
||||
Rect::new(0.0, 0.0, size.width, size.height)
|
||||
} else {
|
||||
@@ -464,18 +421,13 @@ fn align(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
|
||||
fn distribute(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let horiz = match p.get("dir").map(Value::as_str) {
|
||||
None | Some(Some("horizontal")) => true,
|
||||
Some(Some("vertical")) => false,
|
||||
Some(_) => return Err(bad("arrange.distribute", "`dir` must be horizontal or vertical")),
|
||||
};
|
||||
let to = align_to("arrange.distribute", p)?;
|
||||
let horiz = !str_param(p, "dir").unwrap_or("horizontal").starts_with('v');
|
||||
let ids = targets(s, p)?;
|
||||
let st = s.doc()?;
|
||||
let size = st.doc.slide_size;
|
||||
let mut boxes: Vec<(ShapeId, Rect)> =
|
||||
ids.iter().filter_map(|id| st.shape(*id).map(|x| (*id, xfrm_of(&st.doc, &st.selection, x).bounds()))).collect();
|
||||
let to_slide = to == Some(true) || boxes.len() < 3;
|
||||
let to_slide = str_param(p, "to") == Some("slide") || boxes.len() < 3;
|
||||
if boxes.is_empty() {
|
||||
return ok();
|
||||
}
|
||||
|
||||
@@ -120,12 +120,6 @@ fn copy_inner(s: &mut Session, p: &Value) -> Result<String> {
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
// A shape in a group is copied with the box it is drawn in, so it pastes where it was seen.
|
||||
for sh in &mut shapes {
|
||||
if let Some(x) = sh.xfrm.and_then(|x| box_on_slide(&st.doc, &st.selection, st.shapes(), sh.id, &x)) {
|
||||
sh.xfrm = Some(x);
|
||||
}
|
||||
}
|
||||
// Copying is the last chance to read what a placeholder inherits: a cut removes it, and the
|
||||
// paste can be in another deck. Slide Master view pastes the originals instead.
|
||||
let placeholders: Vec<Shape> = shapes.iter().filter(|x| x.ph.is_some()).cloned().collect();
|
||||
@@ -153,39 +147,6 @@ pub(crate) fn detach_on_slide(doc: &deckcraft_model::Presentation, sel: &crate::
|
||||
}
|
||||
}
|
||||
|
||||
/// Where shape `id` is drawn on the slide when it is inside a group: `x`, its box in its group's
|
||||
/// space, mapped out through each group around it, as Ungroup places a freed shape. `None` for a
|
||||
/// shape on the top level or not found. A turned shape in a group stretched more one way than the
|
||||
/// other is drawn skewed, which a box cannot hold, so it gets the unskewed box Ungroup gives it.
|
||||
fn box_on_slide(
|
||||
doc: &deckcraft_model::Presentation,
|
||||
sel: &crate::Selection,
|
||||
shapes: &[Shape],
|
||||
id: ShapeId,
|
||||
x: &deckcraft_geom::Xfrm,
|
||||
) -> Option<deckcraft_geom::Xfrm> {
|
||||
fn rec(
|
||||
doc: &deckcraft_model::Presentation,
|
||||
sel: &crate::Selection,
|
||||
shapes: &[Shape],
|
||||
id: ShapeId,
|
||||
x: &deckcraft_geom::Xfrm,
|
||||
depth: usize,
|
||||
) -> Option<deckcraft_geom::Xfrm> {
|
||||
if depth > 64 {
|
||||
return None;
|
||||
}
|
||||
shapes.iter().find_map(|sh| {
|
||||
let deckcraft_model::ShapeKind::Group { children, child } = &sh.kind else {
|
||||
return None;
|
||||
};
|
||||
let inner = if children.iter().any(|c| c.id == id) { *x } else { rec(doc, sel, children, id, x, depth + 1)? };
|
||||
Some(super::arrange::child_to_parent(&xfrm_of(doc, sel, sh), child, &inner))
|
||||
})
|
||||
}
|
||||
rec(doc, sel, shapes, id, x, 0)
|
||||
}
|
||||
|
||||
fn copying_caret(s: &Session) -> bool {
|
||||
s.active().is_some_and(|d| d.selection.text.as_ref().is_some_and(|t| !t.is_range()))
|
||||
}
|
||||
@@ -598,16 +559,6 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (new_ids, made) = s.edit(|doc, sel| {
|
||||
let list = super::current_shapes(doc, sel).cloned().unwrap_or_default();
|
||||
let mut copies: Vec<Shape> = list.iter().filter(|x| ids.contains(&x.id)).cloned().collect();
|
||||
// A shape inside a group is copied into that group, with the map from the group's space onto
|
||||
// the slide so that its copy is offset on the slide as a top-level copy is.
|
||||
let top = copies.len();
|
||||
let mut to_slide = vec![None; top];
|
||||
for id in grouped_targets(&list, &ids) {
|
||||
if let Some((m, a)) = crate::connect::locate(doc, sel, &list, id) {
|
||||
copies.push(m.clone());
|
||||
to_slide.push(Some(a));
|
||||
}
|
||||
}
|
||||
let sources: Vec<ShapeId> = copies.iter().map(|c| c.id).collect();
|
||||
let boxes: Vec<_> = copies.iter().map(|c| xfrm_of(doc, sel, c)).collect();
|
||||
detach_on_slide(doc, sel, &mut copies);
|
||||
@@ -619,12 +570,10 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
})
|
||||
.and_then(|(_, _, pairs)| {
|
||||
let (src, copy) = pairs.first()?;
|
||||
// Measured on the slide, so the copy of a shape in a group repeats its drawn move.
|
||||
let (a, to_a) = crate::connect::locate(doc, sel, &list, *src)?;
|
||||
let (b, to_b) = crate::connect::locate(doc, sel, &list, *copy)?;
|
||||
let (a, b) = (xfrm_of(doc, sel, a), xfrm_of(doc, sel, b));
|
||||
let d = to_b * deckcraft_geom::Point::new(b.x, b.y) - to_a * deckcraft_geom::Point::new(a.x, a.y);
|
||||
(d.x.is_finite() && d.y.is_finite()).then_some((d.x, d.y))
|
||||
let a = xfrm_of(doc, sel, list.iter().find(|x| x.id == *src)?);
|
||||
let b = xfrm_of(doc, sel, list.iter().find(|x| x.id == *copy)?);
|
||||
let d = (b.x - a.x, b.y - a.y);
|
||||
(d.0.is_finite() && d.1.is_finite()).then_some(d)
|
||||
})
|
||||
.unwrap_or((off, off));
|
||||
reid("edit.duplicate", doc, &mut copies)?;
|
||||
@@ -639,8 +588,7 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
rename_automatic(c, &mut taken, count);
|
||||
count += 1;
|
||||
});
|
||||
for ((c, b), to) in copies.iter_mut().zip(boxes).zip(&to_slide) {
|
||||
let (dx, dy) = to.map_or((dx, dy), |a| offset_in(a, dx, dy));
|
||||
for (c, b) in copies.iter_mut().zip(boxes) {
|
||||
let mut x = b;
|
||||
x.x += dx;
|
||||
x.y += dy;
|
||||
@@ -651,12 +599,7 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
let new_ids: Vec<ShapeId> = copies.iter().map(|c| c.id).collect();
|
||||
let shapes = crate::shapes_mut(doc, sel).ok_or_else(|| bad("edit.duplicate", "no slide"))?;
|
||||
let mut copies = copies.into_iter();
|
||||
shapes.extend(copies.by_ref().take(top));
|
||||
// A member's copy goes last among its group's children, as a top-level copy goes last on the slide.
|
||||
for (c, src) in copies.zip(sources.iter().skip(top)) {
|
||||
deckcraft_model::sibling_list_mut(shapes, *src).ok_or_else(|| bad("edit.duplicate", "no such shape"))?.push(c);
|
||||
}
|
||||
shapes.extend(copies);
|
||||
sel.shapes = new_ids.clone();
|
||||
let made = (sel.slide, sel.target, sources.into_iter().zip(new_ids.iter().copied()).collect());
|
||||
Ok((new_ids, made))
|
||||
@@ -665,34 +608,6 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({"ids": new_ids}))
|
||||
}
|
||||
|
||||
/// The shapes in `ids` that are inside groups, in drawing order, without those in a group that is
|
||||
/// in `ids` too: they are copied along with it.
|
||||
fn grouped_targets(list: &[Shape], ids: &[ShapeId]) -> Vec<ShapeId> {
|
||||
fn rec(shapes: &[Shape], ids: &[ShapeId], nested: bool, out: &mut Vec<ShapeId>, depth: usize) {
|
||||
if depth > 64 {
|
||||
return;
|
||||
}
|
||||
for sh in shapes {
|
||||
if !ids.contains(&sh.id) {
|
||||
rec(sh.children(), ids, true, out, depth + 1);
|
||||
} else if nested {
|
||||
out.push(sh.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out = vec![];
|
||||
rec(list, ids, false, &mut out, 0);
|
||||
out
|
||||
}
|
||||
|
||||
/// The offset in the space `to_slide` maps onto the slide that moves a shape by (`dx`, `dy`) on
|
||||
/// the slide. A group with no width or height draws nothing, and the offset stays as it is.
|
||||
fn offset_in(to_slide: deckcraft_geom::Affine, dx: f64, dy: f64) -> (f64, f64) {
|
||||
let back = to_slide.inverse();
|
||||
let v = back * deckcraft_geom::Point::new(dx, dy) - back * deckcraft_geom::Point::ZERO;
|
||||
if v.x.is_finite() && v.y.is_finite() { (v.x, v.y) } else { (dx, dy) }
|
||||
}
|
||||
|
||||
fn delete(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if s.doc()?.selection.text.is_some() && ids_param(p, "ids").is_none() {
|
||||
return super::text::delete_dir(s, "forward");
|
||||
|
||||
@@ -139,13 +139,11 @@ pub struct Damage {
|
||||
pub media: Vec<String>,
|
||||
/// Slides a `.pptx` lists but that couldn't be read, so they are not in the presentation.
|
||||
pub slides: Vec<String>,
|
||||
/// Content a `.pptx` had that was cut to fit a limit; the file still opened.
|
||||
pub warnings: Vec<String>,
|
||||
}
|
||||
|
||||
impl Damage {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.media.is_empty() && self.slides.is_empty() && self.warnings.is_empty()
|
||||
self.media.is_empty() && self.slides.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,17 +151,17 @@ impl Damage {
|
||||
pub fn open_checked(name: &str, bytes: &[u8]) -> Result<(Presentation, Damage)> {
|
||||
if deckcraft_format::sniff(bytes) {
|
||||
let (p, media) = deckcraft_format::load_checked(bytes).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
return Ok((p, Damage { media, ..Default::default() }));
|
||||
return Ok((p, Damage { media, slides: vec![] }));
|
||||
}
|
||||
open_other(name, bytes).map(|(p, slides, warnings)| (p, Damage { slides, warnings, ..Default::default() }))
|
||||
open_other(name, bytes).map(|(p, slides)| (p, Damage { media: vec![], slides }))
|
||||
}
|
||||
|
||||
fn open_other(name: &str, bytes: &[u8]) -> Result<(Presentation, Vec<String>, Vec<String>)> {
|
||||
fn open_other(name: &str, bytes: &[u8]) -> Result<(Presentation, Vec<String>)> {
|
||||
if deckcraft_pptx::sniff(bytes) {
|
||||
let (mut p, lost_slides, warnings) = deckcraft_pptx::import_report(bytes).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
let (mut p, lost_slides) = deckcraft_pptx::import_checked(bytes).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
deckcraft_format::repair(&mut p);
|
||||
probe_clips(&mut p);
|
||||
return Ok((p, lost_slides, warnings));
|
||||
return Ok((p, lost_slides));
|
||||
}
|
||||
if deckcraft_pptx::is_encrypted(bytes) {
|
||||
return Err(EngineError::Other(format!("{name} is password-protected. DeckCraft can't open password-protected presentations yet.")));
|
||||
@@ -182,7 +180,7 @@ fn open_other(name: &str, bytes: &[u8]) -> Result<(Presentation, Vec<String>, Ve
|
||||
if p.slides.is_empty() {
|
||||
p = defaults::new_presentation(None);
|
||||
}
|
||||
return Ok((p, vec![], vec![]));
|
||||
return Ok((p, vec![]));
|
||||
}
|
||||
Err(EngineError::Other(format!("{name}: not a presentation DeckCraft can read")))
|
||||
}
|
||||
@@ -278,7 +276,7 @@ fn adopt_loaded(s: &mut Session, f: LoadedFile) -> Result<Value> {
|
||||
let st = s.doc()?;
|
||||
Ok(json!({
|
||||
"document": i, "slides": st.doc.slides.len(), "missingFonts": st.missing_fonts,
|
||||
"damagedMedia": damaged.media, "lostSlides": damaged.slides, "importWarnings": damaged.warnings,
|
||||
"damagedMedia": damaged.media, "lostSlides": damaged.slides,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -313,9 +311,7 @@ fn open_bytes(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (doc, damaged) = open_checked(name, &data)?;
|
||||
let i = add_opened(s, name, None, doc);
|
||||
report_damage(s, &damaged)?;
|
||||
Ok(
|
||||
json!({"document": i, "missingFonts": s.doc()?.missing_fonts, "damagedMedia": damaged.media, "lostSlides": damaged.slides, "importWarnings": damaged.warnings}),
|
||||
)
|
||||
Ok(json!({"document": i, "missingFonts": s.doc()?.missing_fonts, "damagedMedia": damaged.media, "lostSlides": damaged.slides}))
|
||||
}
|
||||
|
||||
/// Tell the user which pictures, media and slides a damaged file lost, and mark the copy repaired.
|
||||
@@ -343,10 +339,6 @@ fn report_damage(s: &mut Session, damaged: &Damage) -> Result<()> {
|
||||
if n == 1 { "was" } else { "were" },
|
||||
));
|
||||
}
|
||||
for w in &damaged.warnings {
|
||||
text.push_str(w);
|
||||
text.push_str(". ");
|
||||
}
|
||||
text.push_str("It was opened as a repaired copy; save it under a new name to keep the original file.");
|
||||
s.ui_requests.push(crate::UiRequest::Message { text });
|
||||
Ok(())
|
||||
|
||||
@@ -70,7 +70,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!(
|
||||
"format.textTransparency",
|
||||
"Text Transparency",
|
||||
["Shape Format", "Decorative Text Styles"],
|
||||
["Shape Format", "WordArt Styles"],
|
||||
None,
|
||||
"{value: 0..1} — 0 opaque, 1 invisible; keeps the text's colour (inherited colours included)",
|
||||
has_text_or_shapes,
|
||||
@@ -128,7 +128,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!(
|
||||
"format.textOutline",
|
||||
"Text Outline",
|
||||
["Shape Format", "Decorative Text Styles"],
|
||||
["Shape Format", "WordArt Styles"],
|
||||
None,
|
||||
"{color?, width?: pt} (no color = none)",
|
||||
has_text_or_shapes,
|
||||
@@ -137,7 +137,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!(
|
||||
"format.textWarp",
|
||||
"Text Transform",
|
||||
["Shape Format", "Decorative Text Styles"],
|
||||
["Shape Format", "WordArt Styles"],
|
||||
None,
|
||||
"{preset: a prstTxWarp name or none, adj?: [finite raw guide values, at most 8]} — none clears the transform; omitted adj uses the preset's defaults",
|
||||
has_text_or_shapes,
|
||||
|
||||
@@ -55,18 +55,12 @@ pub(super) fn size(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
format_run(s, &move |r| r.size = Some(v))
|
||||
}
|
||||
|
||||
/// PowerPoint's font size list, as the Font Size box lists it (grow/shrink steps through it).
|
||||
pub const SIZES: [f64; 24] =
|
||||
[8.0, 9.0, 10.0, 10.5, 11.0, 12.0, 14.0, 16.0, 18.0, 20.0, 24.0, 28.0, 32.0, 36.0, 40.0, 44.0, 48.0, 54.0, 60.0, 66.0, 72.0, 80.0, 88.0, 96.0];
|
||||
/// PowerPoint's font size list (grow/shrink steps through it).
|
||||
pub const SIZES: [f64; 20] = [8.0, 9.0, 10.0, 10.5, 11.0, 12.0, 14.0, 16.0, 18.0, 20.0, 24.0, 28.0, 32.0, 36.0, 40.0, 44.0, 48.0, 54.0, 60.0, 66.0];
|
||||
|
||||
/// Grow steps up the list, then 8 points at a time past its top; shrink steps back down the same
|
||||
/// way. A size past the top used to shrink straight to 66 (#2059).
|
||||
fn step(cur: f64, up: bool) -> f64 {
|
||||
let top = SIZES[SIZES.len() - 1];
|
||||
if up {
|
||||
SIZES.iter().copied().find(|v| *v > cur + 0.01).unwrap_or((cur + 8.0).min(4000.0))
|
||||
} else if cur - 0.01 > top {
|
||||
(cur - 8.0).max(top)
|
||||
} else {
|
||||
SIZES.iter().rev().copied().find(|v| *v < cur - 0.01).unwrap_or((cur - 1.0).max(1.0))
|
||||
}
|
||||
@@ -114,7 +108,6 @@ pub(super) fn color(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
edit_shapes(s, &json!({"ids": charts}), "format.color", |sh| {
|
||||
if let ShapeKind::Chart(ch) = &mut sh.kind {
|
||||
ch.text_color = Some(tc.clone());
|
||||
ch.legend_entries.iter_mut().for_each(|e| e.color = None);
|
||||
ch.raw = None;
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -91,7 +91,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
cmd!("insert.audio", "Audio from File…", ["Insert", "Media", "Audio"], None, "{path? | data: base64, name?, rect?}", has_slide, audio),
|
||||
cmd!("insert.video", "Video from File…", ["Insert", "Media", "Video"], None, "{path? | data: base64, name?, rect?}", has_slide, video),
|
||||
cmd!("insert.wordArt", "Decorative Text", ["Insert", "Text"], None, "{text?, style?: 0..}", has_slide, word_art),
|
||||
cmd!("insert.wordArt", "WordArt", ["Insert", "Text"], None, "{text?, style?: 0..}", has_slide, word_art),
|
||||
cmd!("insert.slideNumber", "Slide Number", ["Insert", "Text"], None, "{}", has_slide, slide_number),
|
||||
cmd!(
|
||||
"insert.dateTime",
|
||||
@@ -142,7 +142,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
cmd!(
|
||||
"insert.smartArt",
|
||||
"Diagram",
|
||||
"SmartArt",
|
||||
["Insert", "Illustrations"],
|
||||
None,
|
||||
"{kind: list|process|cycle|hierarchy|pyramid|matrix, items?: [text] (leading tabs = sub-points), nodes?: [{text, level}]} → {id}",
|
||||
|
||||
@@ -80,12 +80,6 @@ pub(super) fn photo_album_edit(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let opts = album_opts(p)?;
|
||||
let items = album_items(p)?;
|
||||
let (id, ids) = album_target(s, p)?;
|
||||
// The editor is seeded within the album limits; replacing the album from a trimmed seed would
|
||||
// delete what it left out, so refuse rather than lose it.
|
||||
let st = s.doc()?;
|
||||
if items_from_slides(&st.doc, &album_slides(st, &ids)).1 {
|
||||
return Err(bad("insert.photoAlbumEdit", "this album holds more than the editor can carry; editing it would delete the rest"));
|
||||
}
|
||||
commit_album(s, items, opts, Some((id, ids)))
|
||||
}
|
||||
|
||||
@@ -551,19 +545,6 @@ fn album_target(s: &Session, p: &Value) -> Result<(u32, Vec<u64>)> {
|
||||
Err(bad("insert.photoAlbumEdit", "this slide is not part of a photo album"))
|
||||
}
|
||||
|
||||
/// Whether a slide holds anything besides album items: shapes, notes, comments, a background, a
|
||||
/// transition, animations, a colour map or tags.
|
||||
fn has_slide_content(s: &deckcraft_model::Slide) -> bool {
|
||||
!s.shapes.is_empty()
|
||||
|| !s.notes.text().trim().is_empty()
|
||||
|| !s.comments.is_empty()
|
||||
|| s.background.is_some()
|
||||
|| s.transition.is_some()
|
||||
|| !s.animations.is_empty()
|
||||
|| s.color_map.is_some()
|
||||
|| !s.tags.is_empty()
|
||||
}
|
||||
|
||||
fn pic_count(slide: &deckcraft_model::Slide) -> usize {
|
||||
slide.shapes.iter().filter(|sh| matches!(sh.kind, ShapeKind::Picture { .. })).count()
|
||||
}
|
||||
@@ -611,16 +592,9 @@ fn infer_album(slides: &[&deckcraft_model::Slide], theme_now: &str) -> (String,
|
||||
(layout, frame.into(), captions, any && gray, theme_now.to_string())
|
||||
}
|
||||
|
||||
/// The album's items for the dialog, within the limits `insert.photoAlbum` itself takes (items,
|
||||
/// bytes per picture and in all), checked before a picture is encoded: an album slide set can
|
||||
/// name one large picture many times.
|
||||
///
|
||||
/// The flag is true when something was left out for a limit.
|
||||
fn items_from_slides(doc: &deckcraft_model::Presentation, slides: &[&deckcraft_model::Slide]) -> (Vec<Value>, bool) {
|
||||
fn items_from_slides(doc: &deckcraft_model::Presentation, slides: &[&deckcraft_model::Slide]) -> Vec<Value> {
|
||||
let mut items = Vec::new();
|
||||
let mut total = 0usize;
|
||||
let mut omitted = false;
|
||||
'slides: for slide in slides {
|
||||
for slide in slides {
|
||||
let pics: Vec<&deckcraft_model::Shape> = slide.shapes.iter().filter(|sh| matches!(sh.kind, ShapeKind::Picture { .. })).collect();
|
||||
if pics.is_empty() {
|
||||
let text = slide
|
||||
@@ -631,27 +605,13 @@ fn items_from_slides(doc: &deckcraft_model::Presentation, slides: &[&deckcraft_m
|
||||
.find(|t| !t.trim().is_empty())
|
||||
.unwrap_or_default();
|
||||
if !text.trim().is_empty() {
|
||||
if items.len() >= ALBUM_MAX_ITEMS {
|
||||
omitted = true;
|
||||
break 'slides;
|
||||
}
|
||||
items.push(json!({"text": text.trim()}));
|
||||
} else if items.len() >= ALBUM_MAX_ITEMS && has_slide_content(slide) {
|
||||
// Past the item limit, a slide with other content would be deleted by a replace.
|
||||
omitted = true;
|
||||
break 'slides;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
for sh in pics {
|
||||
let ShapeKind::Picture { fill } = &sh.kind else { continue };
|
||||
let media = doc.media(fill.media);
|
||||
let size = media.map_or(0, |m| m.data.len());
|
||||
if items.len() >= ALBUM_MAX_ITEMS || size > ALBUM_MAX_BYTES || total.saturating_add(size) > ALBUM_MAX_TOTAL {
|
||||
omitted = true;
|
||||
break 'slides;
|
||||
}
|
||||
total += size;
|
||||
let name = if !sh.descr.is_empty() {
|
||||
sh.descr.clone()
|
||||
} else {
|
||||
@@ -666,19 +626,14 @@ fn items_from_slides(doc: &deckcraft_model::Presentation, slides: &[&deckcraft_m
|
||||
}));
|
||||
}
|
||||
}
|
||||
(items, omitted)
|
||||
}
|
||||
|
||||
/// The slides of album `ids` as the editor would seed them.
|
||||
fn album_slides<'a>(st: &'a crate::DocState, ids: &[u64]) -> Vec<&'a deckcraft_model::Slide> {
|
||||
ids.iter().filter_map(|sid| u32::try_from(*sid).ok()).filter_map(|n| st.doc.slide(deckcraft_model::SlideId(n))).collect()
|
||||
items
|
||||
}
|
||||
|
||||
fn album_seed(s: &Session) -> Result<Value> {
|
||||
let (id, ids) = album_target(s, &Value::Null)?;
|
||||
let st = s.doc()?;
|
||||
let slides = album_slides(st, &ids);
|
||||
let (items, _) = items_from_slides(&st.doc, &slides);
|
||||
let slides: Vec<&deckcraft_model::Slide> =
|
||||
ids.iter().filter_map(|sid| u32::try_from(*sid).ok()).filter_map(|n| st.doc.slide(deckcraft_model::SlideId(n))).collect();
|
||||
let theme_now = st.doc.masters.first().map(|m| m.theme.name.clone()).filter(|n| !n.is_empty()).unwrap_or_else(|| "DeckCraft".into());
|
||||
let inferred = infer_album(&slides, &theme_now);
|
||||
let mark = slides.first().and_then(|sl| sl.raw_ext.as_deref().and_then(parse_album_mark));
|
||||
@@ -700,7 +655,7 @@ fn album_seed(s: &Session) -> Result<Value> {
|
||||
"captions": captions,
|
||||
"grayscale": grayscale,
|
||||
"theme": theme,
|
||||
"items": items,
|
||||
"items": items_from_slides(&st.doc, &slides),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -767,96 +722,3 @@ fn insert_album(s: &mut Session, items: Vec<AlbumItem>, opts: &AlbumOpts, at: Op
|
||||
s.execute("slide.go", &json!({"index": first}))?;
|
||||
Ok(json!({"slides": slides, "first": first, "pictures": pictures, "album": album_id}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod seed_tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use deckcraft_model::style::PictureFill;
|
||||
use deckcraft_model::{Shape, ShapeId, defaults};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn reopening_an_album_seeds_at_most_the_items_an_album_takes() {
|
||||
// One picture named by 500 shapes: the dialog gets 48 items, not 500 encoded copies.
|
||||
let mut p = defaults::new_presentation(None);
|
||||
let img = p.add_media("a.png", "image/png", vec![1; 64 * 1024]).unwrap();
|
||||
let pic = |k: u32| Shape {
|
||||
id: ShapeId(10_000 + k),
|
||||
kind: ShapeKind::Picture { fill: PictureFill { media: img, ..Default::default() } },
|
||||
..Default::default()
|
||||
};
|
||||
Arc::make_mut(&mut p.slides[0]).shapes = (0..500).map(pic).collect();
|
||||
let slides: Vec<&deckcraft_model::Slide> = p.slides.iter().map(|s| s.as_ref()).collect();
|
||||
let (items, omitted) = items_from_slides(&p, &slides);
|
||||
assert_eq!(items.len(), ALBUM_MAX_ITEMS);
|
||||
assert!(omitted);
|
||||
let few: Vec<&deckcraft_model::Slide> = Vec::new();
|
||||
assert!(!items_from_slides(&p, &few).1);
|
||||
}
|
||||
|
||||
/// An album too big for the editor's seed cannot be replaced from that seed: the replace would
|
||||
/// delete what the seed left out.
|
||||
#[test]
|
||||
fn editing_an_album_the_editor_cannot_hold_is_refused() {
|
||||
let mut s = crate::Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let texts: Vec<Value> = (0..ALBUM_MAX_ITEMS).map(|i| json!({"text": format!("t{i}")})).collect();
|
||||
s.execute("insert.photoAlbum", &json!({"pictures": texts, "layout": "1"})).unwrap();
|
||||
let st = s.doc_mut().unwrap();
|
||||
let doc = Arc::make_mut(&mut st.doc);
|
||||
let mut extra = doc.slides.last().unwrap().as_ref().clone();
|
||||
extra.id = deckcraft_model::SlideId(doc.alloc_id().unwrap());
|
||||
doc.slides.push(Arc::new(extra));
|
||||
let n = doc.slides.len();
|
||||
let e = s.execute("insert.photoAlbumEdit", &json!({"pictures": [{"text": "x"}], "layout": "1"})).unwrap_err().to_string();
|
||||
assert!(e.contains("delete the rest"), "{e}");
|
||||
assert_eq!(s.doc().unwrap().doc.slides.len(), n);
|
||||
}
|
||||
|
||||
/// Past the item limit, a slide holding only a drawing counts as content the replace would delete.
|
||||
#[test]
|
||||
fn editing_an_album_with_a_drawing_slide_past_the_limit_is_refused() {
|
||||
let mut s = crate::Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let texts: Vec<Value> = (0..ALBUM_MAX_ITEMS).map(|i| json!({"text": format!("t{i}")})).collect();
|
||||
s.execute("insert.photoAlbum", &json!({"pictures": texts, "layout": "1"})).unwrap();
|
||||
let st = s.doc_mut().unwrap();
|
||||
let doc = Arc::make_mut(&mut st.doc);
|
||||
let mut extra = doc.slides.last().unwrap().as_ref().clone();
|
||||
extra.id = deckcraft_model::SlideId(doc.alloc_id().unwrap());
|
||||
extra.shapes = vec![Shape { id: ShapeId(90_000), ..Default::default() }];
|
||||
doc.slides.push(Arc::new(extra));
|
||||
let e = s.execute("insert.photoAlbumEdit", &json!({"pictures": [{"text": "x"}], "layout": "1"})).unwrap_err().to_string();
|
||||
assert!(e.contains("delete the rest"), "{e}");
|
||||
}
|
||||
|
||||
/// Notes or comments on a shapeless slide past the limit are content a replace would delete.
|
||||
#[test]
|
||||
fn editing_an_album_with_a_notes_only_slide_past_the_limit_is_refused() {
|
||||
let mut s = crate::Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let texts: Vec<Value> = (0..ALBUM_MAX_ITEMS).map(|i| json!({"text": format!("t{i}")})).collect();
|
||||
s.execute("insert.photoAlbum", &json!({"pictures": texts, "layout": "1"})).unwrap();
|
||||
let st = s.doc_mut().unwrap();
|
||||
let doc = Arc::make_mut(&mut st.doc);
|
||||
let mut extra = doc.slides.last().unwrap().as_ref().clone();
|
||||
extra.id = deckcraft_model::SlideId(doc.alloc_id().unwrap());
|
||||
extra.shapes.clear();
|
||||
extra.notes = deckcraft_model::text::TextBody::from_text("speaker notes");
|
||||
doc.slides.push(Arc::new(extra));
|
||||
let e = s.execute("insert.photoAlbumEdit", &json!({"pictures": [{"text": "x"}], "layout": "1"})).unwrap_err().to_string();
|
||||
assert!(e.contains("delete the rest"), "{e}");
|
||||
}
|
||||
|
||||
/// A shapeless slide with a background, transition, colour map or tags counts as content; a plain empty one does not.
|
||||
#[test]
|
||||
fn slide_level_settings_count_as_slide_content() {
|
||||
use deckcraft_model::Slide;
|
||||
assert!(!has_slide_content(&Slide::default()));
|
||||
assert!(has_slide_content(&Slide { tags: vec![("k".into(), "v".into())], ..Default::default() }));
|
||||
assert!(has_slide_content(&Slide { color_map: Some(vec![("bg1".into(), "lt1".into())]), ..Default::default() }));
|
||||
assert!(has_slide_content(&Slide { transition: Some(Default::default()), ..Default::default() }));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,7 +190,6 @@ pub(super) fn chart_options(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
with_chart(s, p, "chart.options", |c| {
|
||||
if let Some(t) = &text_color {
|
||||
c.text_color = t.clone();
|
||||
c.legend_entries.iter_mut().for_each(|e| e.color = None);
|
||||
}
|
||||
apply_bin_options(c, bin_count, bin_size);
|
||||
apply_subtotals_and_bars(c, p, subtotals.as_deref());
|
||||
|
||||
@@ -24,24 +24,12 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Merge Shapes",
|
||||
["Shape Format", "Insert Shapes", "Merge Shapes"],
|
||||
None,
|
||||
"{op: union|combine|fragment|intersect|subtract, ids?: shapes in selection order (the first one's formatting is kept)} → {ids} (a shape inside a group is refused; ungroup it first)",
|
||||
can_merge,
|
||||
"{op: union|combine|fragment|intersect|subtract, ids?: shapes in selection order (the first one's formatting is kept)} → {ids}",
|
||||
has_two,
|
||||
merge
|
||||
)]
|
||||
}
|
||||
|
||||
/// Why Merge Shapes is unavailable while a shape inside a group is selected.
|
||||
pub(crate) const IN_GROUP: &str = "a shape in a group can't be merged; ungroup it first";
|
||||
|
||||
/// Two or more shapes selected, none of them inside a group: the result replaces its sources in
|
||||
/// the slide's top-level list, so a member would be drawn outside its group's scale and offset.
|
||||
fn can_merge(s: &Session) -> std::result::Result<(), String> {
|
||||
has_two(s)?;
|
||||
let in_group =
|
||||
s.active().is_some_and(|st| st.selection.shapes.iter().any(|id| st.shape(*id).is_some() && !st.shapes().iter().any(|t| t.id == *id)));
|
||||
if in_group { Err(IN_GROUP.into()) } else { Ok(()) }
|
||||
}
|
||||
|
||||
/// The filled outline of `s` in slide points (curves flattened). A text box with no fill and no
|
||||
/// outline is its letters, as in PowerPoint (merging text with a picture fills the letters).
|
||||
fn area_of(doc: &deckcraft_model::Presentation, sel: &crate::Selection, s: &Shape) -> Area {
|
||||
@@ -209,10 +197,6 @@ fn merge(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let ids = targets(s, p)?;
|
||||
let st = s.doc()?;
|
||||
let shapes: Vec<Shape> = ids.iter().filter_map(|id| st.shape(*id).cloned()).collect();
|
||||
if let Some(member) = shapes.iter().find(|x| !st.shapes().iter().any(|t| t.id == x.id)) {
|
||||
let name = if member.name.is_empty() { format!("shape {}", member.id.0) } else { format!("`{}`", member.name) };
|
||||
return Err(bad("shape.merge", format!("{name} is in a group; ungroup it first to merge it")));
|
||||
}
|
||||
let mergeable = |x: &Shape| matches!(x.kind, ShapeKind::Shape | ShapeKind::Picture { .. }) && !x.is_line();
|
||||
if shapes.len() < 2 || !shapes.iter().all(mergeable) {
|
||||
return Err(bad("shape.merge", "select two or more shapes (not lines, groups, tables or charts)"));
|
||||
@@ -395,40 +379,4 @@ mod tests {
|
||||
assert_eq!(animations[1].trigger.map(|id| id.0), Some(first));
|
||||
}
|
||||
}
|
||||
|
||||
/// Merge Shapes refuses a shape inside a group, naming it, and is unavailable while one is
|
||||
/// selected. Before, the result went to the slide's top level with the member's box from the
|
||||
/// group's own space, so the shapes of a moved or resized group were redrawn somewhere else.
|
||||
#[test]
|
||||
fn a_shape_in_a_group_is_refused() {
|
||||
fn rect(s: &mut Session, x: f64) -> u64 {
|
||||
s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, 100, 100, 100]})).unwrap()["id"].as_u64().unwrap()
|
||||
}
|
||||
let mut s = Session::with_new();
|
||||
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
|
||||
let (a, b, c) = (rect(&mut s, 100.0), rect(&mut s, 150.0), rect(&mut s, 500.0));
|
||||
let g = s.execute("arrange.group", &json!({"ids": [a, b]})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("shape.move", &json!({"ids": [g], "dx": 300})).unwrap();
|
||||
let before = s.doc().unwrap().current_slide().unwrap().shapes.clone();
|
||||
let name = s.doc().unwrap().shape(ShapeId(a as u32)).unwrap().name.clone();
|
||||
for ids in [json!([a, b]), json!([c, a])] {
|
||||
let err = s.execute("shape.merge", &json!({"op": "union", "ids": ids})).unwrap_err().to_string();
|
||||
assert!(err.contains(&format!("`{name}` is in a group; ungroup it first")), "{ids}: {err}");
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().current_slide().unwrap().shapes, before, "a refused merge leaves the slide as it was");
|
||||
|
||||
let spec = crate::cmd::find_command("shape.merge").unwrap();
|
||||
s.execute("edit.select", &json!({"ids": [a, c]})).unwrap();
|
||||
assert_eq!((spec.enabled)(&s), Err("a shape in a group can't be merged; ungroup it first".to_string()));
|
||||
assert!(s.execute("shape.merge", &json!({"op": "union"})).is_err());
|
||||
// Named top-level shapes don't depend on the selection.
|
||||
let d = rect(&mut s, 550.0);
|
||||
s.execute("edit.select", &json!({"ids": [a, b]})).unwrap();
|
||||
assert_eq!(s.execute("shape.merge", &json!({"op": "union", "ids": [c, d]})).unwrap()["ids"].as_array().map(Vec::len), Some(1));
|
||||
|
||||
s.execute("arrange.ungroup", &json!({"ids": [g]})).unwrap();
|
||||
s.execute("edit.select", &json!({"ids": [a, b]})).unwrap();
|
||||
assert_eq!((spec.enabled)(&s), Ok(()));
|
||||
assert_eq!(s.execute("shape.merge", &json!({"op": "union"})).unwrap()["ids"].as_array().map(Vec::len), Some(1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ pub(super) fn adjust(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let v = f64_param(p, "value").ok_or_else(|| bad("shape.adjust", "missing `value`"))?;
|
||||
edit_geometry(s, p, "shape.adjust", |sh| {
|
||||
if let Geom::Preset { name, adj } = &mut sh.geom {
|
||||
let defaults = deckcraft_geom::preset::adjust_defaults(name);
|
||||
let defaults = deckcraft_geom::preset::info(name).map(|x| x.defaults).unwrap_or(&[]);
|
||||
while adj.len() <= i {
|
||||
let k = adj.len();
|
||||
adj.push(defaults.get(k).copied().unwrap_or(0.0));
|
||||
|
||||
@@ -196,7 +196,7 @@ pub(super) fn flip(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mut graphic = false;
|
||||
deckcraft_model::walk(std::slice::from_ref(&*sh), &mut |x, _| graphic |= x.diagram.is_some());
|
||||
if graphic {
|
||||
return Err(bad("shape.flip", "Flip is not available for a Diagram graphic; use `smartArt.layout` to change how it is laid out"));
|
||||
return Err(bad("shape.flip", "Flip is not available for a SmartArt graphic; use `smartArt.layout` to change how it is laid out"));
|
||||
}
|
||||
if let Some(x) = sh.xfrm.as_mut() {
|
||||
if v {
|
||||
|
||||
@@ -162,7 +162,7 @@ pub fn preview(kind: &str, style: &str, colors: &str, nodes: Option<&[DiagramNod
|
||||
}
|
||||
Shape {
|
||||
id: ShapeId(9_999),
|
||||
name: "Diagram preview".into(),
|
||||
name: "SmartArt preview".into(),
|
||||
xfrm: Some(area),
|
||||
kind: ShapeKind::Group { children, child: area },
|
||||
..Default::default()
|
||||
@@ -178,16 +178,16 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!(
|
||||
query "smartArt.nodes",
|
||||
"Text Pane",
|
||||
["Diagram Design", "Create Graphic"],
|
||||
["SmartArt Design", "Create Graphic"],
|
||||
None,
|
||||
"{id?} → {id, kind, nodes: [{text, level}]} (id: the Diagram group or one of its shapes; default the selection)",
|
||||
"{id?} → {id, kind, nodes: [{text, level}]} (id: the SmartArt group or one of its shapes; default the selection)",
|
||||
has_selection,
|
||||
nodes
|
||||
),
|
||||
cmd!(
|
||||
"smartArt.setNodes",
|
||||
"Edit Diagram Text",
|
||||
["Diagram Design", "Create Graphic"],
|
||||
"Edit SmartArt Text",
|
||||
["SmartArt Design", "Create Graphic"],
|
||||
None,
|
||||
"{id?, nodes?: [{text, level}], lines?: [string] (leading tabs set the level), index?, text?, title?} (index + text edits one node; title: its Name and Title box)",
|
||||
has_selection,
|
||||
@@ -196,7 +196,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!(
|
||||
"smartArt.addNode",
|
||||
"Add Shape",
|
||||
["Diagram Design", "Create Graphic"],
|
||||
["SmartArt Design", "Create Graphic"],
|
||||
None,
|
||||
"{id?, index?, position?: after|before|below|above|assistant, text?} → {index} (default: after the last node)",
|
||||
has_selection,
|
||||
@@ -205,18 +205,18 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!(
|
||||
"smartArt.removeNode",
|
||||
"Remove Shape",
|
||||
["Diagram Design", "Create Graphic"],
|
||||
["SmartArt Design", "Create Graphic"],
|
||||
None,
|
||||
"{id?, index} (its sub-points move up one level)",
|
||||
has_selection,
|
||||
remove_node
|
||||
),
|
||||
cmd!("smartArt.promote", "Promote", ["Diagram Design", "Create Graphic"], None, "{id?, index}", has_selection, promote),
|
||||
cmd!("smartArt.demote", "Demote", ["Diagram Design", "Create Graphic"], None, "{id?, index}", has_selection, demote),
|
||||
cmd!("smartArt.promote", "Promote", ["SmartArt Design", "Create Graphic"], None, "{id?, index}", has_selection, promote),
|
||||
cmd!("smartArt.demote", "Demote", ["SmartArt Design", "Create Graphic"], None, "{id?, index}", has_selection, demote),
|
||||
cmd!(
|
||||
"smartArt.layout",
|
||||
"Change Layout",
|
||||
["Diagram Design", "Layouts"],
|
||||
["SmartArt Design", "Layouts"],
|
||||
None,
|
||||
"{id?, kind: list|blockList|hBulletList|process|chevron|verticalProcess|stepUp|cycle|blockCycle|radial|hierarchy|pyramid|invertedPyramid|matrix|titledMatrix|pictureList|pictureCaption|pictureProcess|pictureHList|timeline|venn|target|textCycle|continuousCycle|horizontalHierarchy|gridMatrix|gear|funnel|segmentedCycle|multidirectionalCycle|orgChart|halfCircleOrgChart|cycleMatrix|segmentedPyramid|pie|nameTitleOrgChart|verticalBoxList|trapezoidList|tableList|accentProcess|continuousBlockProcess|bendingProcess|linearVenn|stackedVenn|nestedTarget}",
|
||||
has_selection,
|
||||
@@ -224,17 +224,17 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
cmd!(
|
||||
"smartArt.fromText",
|
||||
"Convert to Diagram",
|
||||
"Convert to SmartArt",
|
||||
["Home", "Paragraph"],
|
||||
None,
|
||||
"{id?, kind?} → {id} (a text box or placeholder becomes a Diagram graphic in its place: one node per paragraph, indent levels kept)",
|
||||
"{id?, kind?} → {id} (a text box or placeholder becomes a SmartArt graphic in its place: one node per paragraph, indent levels kept)",
|
||||
has_selection,
|
||||
from_text
|
||||
),
|
||||
cmd!(
|
||||
"smartArt.toText",
|
||||
"Convert to Text",
|
||||
["Diagram Design", "Reset"],
|
||||
["SmartArt Design", "Reset"],
|
||||
None,
|
||||
"{id?} → {id} (the graphic becomes a text box of its outline, bulleted by level)",
|
||||
has_selection,
|
||||
@@ -243,7 +243,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!(
|
||||
"smartArt.toShapes",
|
||||
"Convert to Shapes",
|
||||
["Diagram Design", "Reset"],
|
||||
["SmartArt Design", "Reset"],
|
||||
None,
|
||||
"{id?} (the graphic becomes a plain group of its shapes; the outline is dropped)",
|
||||
has_selection,
|
||||
@@ -251,8 +251,8 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
cmd!(
|
||||
"smartArt.picture",
|
||||
"Insert Picture in Diagram",
|
||||
["Diagram Design", "Create Graphic"],
|
||||
"Insert Picture in SmartArt",
|
||||
["SmartArt Design", "Create Graphic"],
|
||||
None,
|
||||
"{id?, index, data?: base64 | path?: string | reset?: bool} (the node's picture placeholder in picture layouts)",
|
||||
has_selection,
|
||||
@@ -260,8 +260,8 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
cmd!(
|
||||
"smartArt.style",
|
||||
"Diagram Styles",
|
||||
["Diagram Design", "Diagram Styles"],
|
||||
"SmartArt Styles",
|
||||
["SmartArt Design", "SmartArt Styles"],
|
||||
None,
|
||||
"{id?, style: flat|subtle|intense|outline|polished|inset|cartoon|powder|brick}",
|
||||
has_selection,
|
||||
@@ -270,7 +270,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!(
|
||||
"smartArt.colors",
|
||||
"Change Colors",
|
||||
["Diagram Design", "Diagram Styles"],
|
||||
["SmartArt Design", "SmartArt Styles"],
|
||||
None,
|
||||
"{id?, colors: colorful|range23|range34|range45|range56|accent1|…|accent6|accent1Grad|…|accent6Grad}",
|
||||
has_selection,
|
||||
|
||||
@@ -38,7 +38,7 @@ fn target_group(s: &Session, p: &Value, cmd: &str) -> Result<ShapeId> {
|
||||
let st = s.doc()?;
|
||||
let shapes = current_shapes(&st.doc, &st.selection).ok_or_else(|| bad(cmd, "no slide"))?;
|
||||
let ids = targets(s, p)?;
|
||||
ids.iter().find_map(|id| diagram_group(shapes, *id)).map(|g| g.id).ok_or_else(|| bad(cmd, "select a Diagram graphic"))
|
||||
ids.iter().find_map(|id| diagram_group(shapes, *id)).map(|g| g.id).ok_or_else(|| bad(cmd, "select a SmartArt graphic"))
|
||||
}
|
||||
|
||||
/// Children with fresh ids for diagram `d` inside a group whose child space is `area`.
|
||||
@@ -55,7 +55,7 @@ fn edit_diagram<T>(s: &mut Session, p: &Value, cmd: &str, f: impl FnOnce(&mut Di
|
||||
let gid = target_group(s, p, cmd)?;
|
||||
s.edit(|doc, sel| {
|
||||
let g = current_shapes(doc, sel).and_then(|v| deckcraft_model::find_shape(v, gid)).cloned().ok_or_else(|| bad(cmd, "no such shape"))?;
|
||||
let (ShapeKind::Group { children, child }, Some(mut d)) = (g.kind, g.diagram) else { return Err(bad(cmd, "not a Diagram graphic")) };
|
||||
let (ShapeKind::Group { children, child }, Some(mut d)) = (g.kind, g.diagram) else { return Err(bad(cmd, "not a SmartArt graphic")) };
|
||||
sync_text(&mut d, &children);
|
||||
let out = f(&mut d)?;
|
||||
normalize(&mut d.nodes);
|
||||
@@ -90,7 +90,7 @@ pub fn insert(s: &mut Session, kind: &str, mut nodes: Vec<DiagramNode>) -> Resul
|
||||
let id = ShapeId(doc.alloc_id().ok_or_else(|| bad("insert.smartArt", NO_ID_SPACE))?);
|
||||
let g = Shape {
|
||||
id,
|
||||
name: format!("Diagram ({kind})"),
|
||||
name: format!("SmartArt ({kind})"),
|
||||
xfrm: Some(area),
|
||||
kind: ShapeKind::Group { children, child: area },
|
||||
diagram: Some(d),
|
||||
@@ -201,7 +201,7 @@ pub(super) fn from_text(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let gid = ShapeId(doc.alloc_id().ok_or_else(|| bad(CMD, NO_ID_SPACE))?);
|
||||
let g = Shape {
|
||||
id: gid,
|
||||
name: format!("Diagram ({kind})"),
|
||||
name: format!("SmartArt ({kind})"),
|
||||
xfrm: Some(area),
|
||||
kind: ShapeKind::Group { children, child: area },
|
||||
diagram: Some(d),
|
||||
@@ -222,12 +222,12 @@ pub(super) fn from_text(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
pub(super) fn to_text(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const CMD: &str = "smartArt.toText";
|
||||
let gid = target_group(s, p, CMD)?;
|
||||
let (_, d) = selected_for(s, gid).ok_or_else(|| bad(CMD, "select a Diagram graphic"))?;
|
||||
let (_, d) = selected_for(s, gid).ok_or_else(|| bad(CMD, "select a SmartArt graphic"))?;
|
||||
// The sizes the graphic's text shows at: its titles' and its bullets' (shrunk to fit included).
|
||||
let (title_pt, bullet_pt) = {
|
||||
let st = s.doc()?;
|
||||
let shapes = current_shapes(&st.doc, &st.selection).ok_or_else(|| bad(CMD, "no slide"))?;
|
||||
let g = diagram_group(shapes, gid).ok_or_else(|| bad(CMD, "select a Diagram graphic"))?;
|
||||
let g = diagram_group(shapes, gid).ok_or_else(|| bad(CMD, "select a SmartArt graphic"))?;
|
||||
text_sizes(g.children())
|
||||
};
|
||||
let new = s.edit(|doc, sel| {
|
||||
|
||||
@@ -166,21 +166,21 @@ pub(super) fn text_shape(preset: &str, x: Xfrm, lines: &[&DiagramNode], title: b
|
||||
if !title {
|
||||
body.body.anchor = Some(deckcraft_model::text::Anchor::Top);
|
||||
}
|
||||
Shape { name: "Diagram shape".into(), xfrm: Some(x), geom: Geom::preset(preset), text: Some(body), ..Default::default() }
|
||||
Shape { name: "SmartArt shape".into(), xfrm: Some(x), geom: Geom::preset(preset), text: Some(body), ..Default::default() }
|
||||
}
|
||||
|
||||
/// A plain shape with no text (arrows between steps).
|
||||
pub(super) fn deco(preset: &str, x: Xfrm, rot: f64) -> Shape {
|
||||
let mut x = x;
|
||||
x.rot = rot;
|
||||
Shape { name: "Diagram arrow".into(), xfrm: Some(x), geom: Geom::preset(preset), ..Default::default() }
|
||||
Shape { name: "SmartArt arrow".into(), xfrm: Some(x), geom: Geom::preset(preset), ..Default::default() }
|
||||
}
|
||||
|
||||
pub(super) fn line_shape(a: (f64, f64), b: (f64, f64)) -> Shape {
|
||||
let mut x = Xfrm::new(a.0.min(b.0), a.1.min(b.1), (b.0 - a.0).abs(), (b.1 - a.1).abs());
|
||||
x.flip_h = (b.0 < a.0) != (b.1 < a.1);
|
||||
Shape {
|
||||
name: "Diagram connector".into(),
|
||||
name: "SmartArt connector".into(),
|
||||
xfrm: Some(x),
|
||||
geom: Geom::preset("line"),
|
||||
fill: Some(Fill::None),
|
||||
@@ -257,7 +257,7 @@ fn fit_words(sh: &mut Shape) {
|
||||
|
||||
/// A picture placeholder for node `node`: its picture when it has one, else a pale box.
|
||||
pub(super) fn picture_shape(d: &Diagram, preset: &str, x: Xfrm, i: usize, node: usize) -> Shape {
|
||||
let mut sh = Shape { name: "Diagram picture".into(), xfrm: Some(x), geom: Geom::preset(preset), ..Default::default() };
|
||||
let mut sh = Shape { name: "SmartArt picture".into(), xfrm: Some(x), geom: Geom::preset(preset), ..Default::default() };
|
||||
paint(d, &mut sh, i, true);
|
||||
if let Some(m) = d.nodes.get(node).and_then(|n| n.picture) {
|
||||
sh.fill = Some(Fill::Picture(deckcraft_model::style::PictureFill { media: m, ..Default::default() }));
|
||||
|
||||
@@ -286,12 +286,6 @@ fn with_table(s: &mut Session, p: &Value, cmd: &str, f: impl Fn(&mut Table) -> R
|
||||
};
|
||||
let r = edit_shapes(s, &json!({"id": id}), cmd, |sh| match &mut sh.kind {
|
||||
ShapeKind::Table(t) => {
|
||||
// Edits walk the grid: bound it first (a loaded table need not be normalized yet).
|
||||
// An over-cap table is refused, not trimmed: a trim would delete rows while reporting success.
|
||||
if t.over_cap() {
|
||||
return Err(bad(cmd, format!("the table is larger than {} cells; edit refused", deckcraft_model::table::MAX_CELLS)));
|
||||
}
|
||||
t.normalize();
|
||||
f(t)?;
|
||||
t.normalize();
|
||||
let (w, h) = (t.width(), t.height());
|
||||
@@ -325,17 +319,10 @@ fn rows_cols(s: &mut Session, p: &Value, op: &str, name: &'static str) -> Result
|
||||
let op = op.to_string();
|
||||
with_table(s, p, name, move |t| {
|
||||
match op.as_str() {
|
||||
"rowAbove" | "rowBelow" | "colLeft" | "colRight" => {
|
||||
let ok = match op.as_str() {
|
||||
"rowAbove" => t.insert_row(r),
|
||||
"rowBelow" => t.insert_row(r.saturating_add(1)),
|
||||
"colLeft" => t.insert_col(c),
|
||||
_ => t.insert_col(c.saturating_add(1)),
|
||||
};
|
||||
if !ok {
|
||||
return Err(bad(name, format!("the table is at its {}-cell limit", deckcraft_model::table::MAX_CELLS)));
|
||||
}
|
||||
}
|
||||
"rowAbove" => t.insert_row(r),
|
||||
"rowBelow" => t.insert_row(r.saturating_add(1)),
|
||||
"colLeft" => t.insert_col(c),
|
||||
"colRight" => t.insert_col(c.saturating_add(1)),
|
||||
"delRow" => {
|
||||
if !t.delete_row(r) {
|
||||
return Err(bad(name, "can't delete the last row"));
|
||||
|
||||
@@ -56,7 +56,7 @@ pub(crate) fn delete_dir(s: &mut Session, dir: &str) -> Result<Value> {
|
||||
ed::delete(body, a, b)
|
||||
} else {
|
||||
let other = match dir.as_str() {
|
||||
"forward" => ed::cluster_end(body, a),
|
||||
"forward" => ed::char_move(body, a, true),
|
||||
_ => {
|
||||
// Backspace at the start of an indented/bulleted paragraph outdents first.
|
||||
if a.1 == 0
|
||||
|
||||
@@ -115,7 +115,7 @@ fn split_rows(t: &mut Table, y: f64, x0: f64, x1: f64) -> bool {
|
||||
let b = match ys.iter().position(|e| (e - y).abs() <= SNAP) {
|
||||
Some(b) => b,
|
||||
None => {
|
||||
if t.rows.len() >= MAX_GRID || t.cols.len().saturating_mul(t.rows.len().saturating_add(1)) > deckcraft_model::table::MAX_CELLS {
|
||||
if t.rows.len() >= MAX_GRID {
|
||||
return false;
|
||||
}
|
||||
let Some(k) = ys.windows(2).position(|w| w[0] < y && y < w[1]) else { return false };
|
||||
@@ -273,17 +273,4 @@ mod tests {
|
||||
}
|
||||
assert!(t.rows.len() <= MAX_GRID && n > 100, "{} rows", t.rows.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pencil_refuses_a_row_past_the_cell_cap() {
|
||||
let cols = 1000;
|
||||
let rows = deckcraft_model::table::MAX_CELLS / cols;
|
||||
let mut t = Table {
|
||||
cols: vec![10.0; cols],
|
||||
rows: vec![deckcraft_model::table::Row { height: 50.0, cells: vec![Cell::new(""); cols] }; rows],
|
||||
..Default::default()
|
||||
};
|
||||
assert!(!draw_line(&mut t, (0.0, 25.0), (10_000.0, 25.0)));
|
||||
assert_eq!((t.n_rows(), t.n_cols()), (rows, cols));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//! folder, the rest of the path being plain names (`..` below a missing folder is refused, and so
|
||||
//! is a link that leads nowhere).
|
||||
//! - The result must lie inside the root, compared by whole components (`/work2` isn't inside
|
||||
//! `/work`), exactly: on Windows what exists is resolved to its real case first.
|
||||
//! `/work`), without regard to case on Windows.
|
||||
//! - Read and write access are separate: a root given alone grants only its own kind of access.
|
||||
//!
|
||||
//! Roots are in force on a thread inside [`confine`], and in the whole process after
|
||||
@@ -85,20 +85,14 @@ impl AutomationRoots {
|
||||
|
||||
/// May the file or folder at `path` be read (or written)? `Err` says why not.
|
||||
pub fn check(&self, path: &str, access: Access) -> Result<(), String> {
|
||||
self.check_path(Path::new(path), access)
|
||||
}
|
||||
|
||||
/// [`Self::check`] for a path that may not be UTF-8: the checked path is the one opened.
|
||||
pub fn check_path(&self, path: &Path, access: Access) -> Result<(), String> {
|
||||
let path_text = path.display();
|
||||
let roots = match access {
|
||||
Access::Read => &self.read,
|
||||
Access::Write => &self.write,
|
||||
};
|
||||
if roots.is_empty() {
|
||||
return match access {
|
||||
Access::Read => Err(format!("automation filesystem access is not granted: read authority is absent: {path_text}")),
|
||||
Access::Write => Err(format!("automation filesystem access is not granted: write authority is absent: {path_text}")),
|
||||
Access::Read => Err(format!("automation filesystem access is not granted: read authority is absent: {path}")),
|
||||
Access::Write => Err(format!("automation filesystem access is not granted: write authority is absent: {path}")),
|
||||
};
|
||||
}
|
||||
let resolved = resolve(path)?;
|
||||
@@ -107,8 +101,8 @@ impl AutomationRoots {
|
||||
}
|
||||
let roots = roots.iter().map(|root| root.shown.display().to_string()).collect::<Vec<_>>().join(", ");
|
||||
match access {
|
||||
Access::Read => Err(format!("automation path rejected: outside the read roots {roots}: {path_text}")),
|
||||
Access::Write => Err(format!("automation path rejected: outside the write roots {roots}: {path_text}")),
|
||||
Access::Read => Err(format!("automation path rejected: outside the read roots {roots}: {path}")),
|
||||
Access::Write => Err(format!("automation path rejected: outside the write roots {roots}: {path}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -197,23 +191,17 @@ pub fn check_write(path: &str) -> Result<(), String> {
|
||||
current().map_or(Ok(()), |r| r.check(path, Access::Write))
|
||||
}
|
||||
|
||||
/// May `path` be written here? Always, without roots in force. For a path that may not be UTF-8.
|
||||
pub fn check_write_path(path: &Path) -> Result<(), String> {
|
||||
current().map_or(Ok(()), |r| r.check_path(path, Access::Write))
|
||||
}
|
||||
|
||||
/// `path` made absolute with its links followed; for a path that doesn't exist (yet), its nearest
|
||||
/// existing folder's links followed and the rest appended.
|
||||
fn resolve(path: &Path) -> Result<PathBuf, String> {
|
||||
if path.as_os_str().is_empty() {
|
||||
fn resolve(path: &str) -> Result<PathBuf, String> {
|
||||
if path.is_empty() {
|
||||
return Err("automation path rejected: path is empty".into());
|
||||
}
|
||||
let shown = path.display();
|
||||
// Checked as given: making `…\NUL` absolute gives the device path `\\.\NUL` on Windows.
|
||||
if cfg!(windows) && path.components().any(|c| matches!(c, Component::Normal(n) if is_device_name(&n.to_string_lossy()))) {
|
||||
return Err(format!("automation path rejected: reserved device names are not allowed: {shown}"));
|
||||
if cfg!(windows) && Path::new(path).components().any(|c| matches!(c, Component::Normal(n) if is_device_name(&n.to_string_lossy()))) {
|
||||
return Err(format!("automation path rejected: reserved device names are not allowed: {path}"));
|
||||
}
|
||||
let absolute = std::path::absolute(path).map_err(|e| format!("automation path rejected: cannot resolve {shown}: {e}"))?;
|
||||
let absolute = std::path::absolute(path).map_err(|e| format!("automation path rejected: cannot resolve {path}: {e}"))?;
|
||||
// The names below the nearest existing folder, last first.
|
||||
let mut missing: Vec<OsString> = vec![];
|
||||
let mut at = absolute.as_path();
|
||||
@@ -224,11 +212,11 @@ fn resolve(path: &Path) -> Result<PathBuf, String> {
|
||||
return Ok(found);
|
||||
}
|
||||
Err(e) => match std::fs::symlink_metadata(at) {
|
||||
Ok(m) if m.file_type().is_symlink() => return Err(format!("automation path rejected: a link that leads nowhere: {shown}")),
|
||||
Ok(m) if m.file_type().is_symlink() => return Err(format!("automation path rejected: a link that leads nowhere: {path}")),
|
||||
// There, yet its real path can't be had (no permission): nothing to compare.
|
||||
Ok(_) => return Err(format!("automation path rejected: cannot resolve {shown}: {e}")),
|
||||
Ok(_) => return Err(format!("automation path rejected: cannot resolve {path}: {e}")),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
|
||||
Err(e) => return Err(format!("automation path rejected: cannot resolve {shown}: {e}")),
|
||||
Err(e) => return Err(format!("automation path rejected: cannot resolve {path}: {e}")),
|
||||
},
|
||||
}
|
||||
let parent = at.parent();
|
||||
@@ -238,30 +226,41 @@ fn resolve(path: &Path) -> Result<PathBuf, String> {
|
||||
at = parent;
|
||||
}
|
||||
(Some(Component::ParentDir), _) => {
|
||||
return Err(format!("automation path rejected: parent traversal below a folder that doesn't exist: {shown}"));
|
||||
return Err(format!("automation path rejected: parent traversal below a folder that doesn't exist: {path}"));
|
||||
}
|
||||
// Down to the root (or drive) and it doesn't exist either.
|
||||
_ => {
|
||||
let e = std::io::Error::from(std::io::ErrorKind::NotFound);
|
||||
return Err(format!("automation path rejected: cannot resolve {shown}: {e}"));
|
||||
return Err(format!("automation path rejected: cannot resolve {path}: {e}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Is `path` (resolved) `root` or inside it, component by component? Exactly, on Windows too: what
|
||||
/// exists has its real case from [`resolve`], and a folder that doesn't exist yet may be a different
|
||||
/// one from the root where a parent folder is case sensitive.
|
||||
/// Is `path` (resolved) `root` or inside it, component by component (without regard to case on
|
||||
/// Windows, whose file systems ignore it)?
|
||||
fn inside(path: &Path, root: &Path) -> bool {
|
||||
let mut path = path.components();
|
||||
root.components().all(|r| path.next() == Some(r))
|
||||
root.components().all(|r| path.next().is_some_and(|p| same_component(p, r)))
|
||||
}
|
||||
|
||||
/// A Windows device name (`CON`, `NUL`, `COM1`, `COM¹`… also with an extension or a colon), which names no file.
|
||||
fn same_component(a: Component, b: Component) -> bool {
|
||||
if a == b {
|
||||
return true;
|
||||
}
|
||||
if !cfg!(windows) {
|
||||
return false;
|
||||
}
|
||||
match (a.as_os_str().to_str(), b.as_os_str().to_str()) {
|
||||
(Some(a), Some(b)) => a.to_lowercase() == b.to_lowercase(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// A Windows device name (`CON`, `NUL`, `COM1`… also with an extension), which names no file.
|
||||
fn is_device_name(component: &str) -> bool {
|
||||
let stem = component.split(['.', ':']).next().unwrap_or(component).trim_end_matches(' ').to_ascii_uppercase();
|
||||
let numbered =
|
||||
|prefix: &str| stem.strip_prefix(prefix).is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "¹" | "²" | "³"));
|
||||
let stem = component.split('.').next().unwrap_or(component).trim_end_matches(' ').to_ascii_uppercase();
|
||||
let numbered = |prefix: &str| stem.strip_prefix(prefix).is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"));
|
||||
matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL" | "CLOCK$" | "CONIN$" | "CONOUT$") || numbered("COM") || numbered("LPT")
|
||||
}
|
||||
|
||||
@@ -366,7 +365,6 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// A root and a path reached through links are compared after the links are followed.
|
||||
#[test]
|
||||
fn links_are_followed_before_comparing() {
|
||||
let (base, roots) = folders("links");
|
||||
@@ -403,67 +401,20 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// A name that differs in case is another name until the file system says otherwise.
|
||||
#[test]
|
||||
fn names_are_compared_exactly() {
|
||||
let root = Path::new("/work/In");
|
||||
assert!(inside(Path::new("/work/In/new/a.deckcraft"), root));
|
||||
assert!(!inside(Path::new("/work/IN/a.deckcraft"), root));
|
||||
assert!(!inside(Path::new("/WORK/In/a.deckcraft"), root));
|
||||
assert!(!inside(Path::new("/work/in\u{212A}/a.deckcraft"), Path::new("/work/ink")), "the Kelvin sign folds to k");
|
||||
if cfg!(windows) {
|
||||
let root = Path::new(r"C:\work\In");
|
||||
assert!(inside(Path::new(r"c:\work\In\a.deckcraft"), root), "a drive letter is one drive in either case");
|
||||
assert!(!inside(Path::new(r"C:\work\IN\a.deckcraft"), root));
|
||||
}
|
||||
}
|
||||
|
||||
/// In a case-sensitive folder, `IN` beside the root `in` is another folder, there or not yet.
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn a_case_sensitive_folder_keeps_a_differently_cased_name_out() {
|
||||
let base = std::env::temp_dir().join(format!("dc-file-access-case-sensitive-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
std::fs::create_dir_all(&base).unwrap();
|
||||
let set = std::process::Command::new("fsutil").args(["file", "setCaseSensitiveInfo"]).arg(&base).arg("enable").output();
|
||||
if !set.as_ref().is_ok_and(|o| o.status.success()) {
|
||||
eprintln!("skipped: a case-sensitive folder can't be made here: {set:?}");
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
return;
|
||||
}
|
||||
std::fs::create_dir(base.join("in")).unwrap();
|
||||
let root = [base.join("in")];
|
||||
let roots = AutomationRoots::new(&root, &root).unwrap().unwrap();
|
||||
assert_eq!(roots.check(&s(base.join("in/new.deckcraft")), Access::Write), Ok(()));
|
||||
let e = roots.check(&s(base.join("IN/new.deckcraft")), Access::Write).unwrap_err();
|
||||
assert!(e.contains("outside the write root"), "{e}");
|
||||
std::fs::create_dir(base.join("IN")).unwrap();
|
||||
std::fs::write(base.join("IN/secret.deckcraft"), "x").unwrap();
|
||||
let e = roots.check(&s(base.join("IN/secret.deckcraft")), Access::Read).unwrap_err();
|
||||
assert!(e.contains("outside the read root"), "{e}");
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
/// On Windows a path finds its real case on disk, and device names are refused.
|
||||
#[test]
|
||||
fn windows_finds_the_real_case_and_refuses_devices() {
|
||||
fn windows_ignores_case_and_refuses_devices() {
|
||||
let (base, roots) = folders("case");
|
||||
let upper = s(base.join("IN/a.deckcraft"));
|
||||
assert_eq!(roots.check(&upper, Access::Read), Ok(()), "{upper}");
|
||||
assert_eq!(roots.check(&s(base.join("In/Sub/New.deckcraft")), Access::Read), Ok(()));
|
||||
assert_eq!(roots.check(&s(base.join("OUT/New.deckcraft")), Access::Write), Ok(()));
|
||||
assert_eq!(roots.check(&s(base.join("OUT")), Access::Write), Ok(()));
|
||||
let e = roots.check(&s(base.join("out/NUL")), Access::Write).unwrap_err();
|
||||
assert!(e.contains("reserved device names"), "{e}");
|
||||
assert!(roots.check(&s(base.join("out/com1.txt")), Access::Write).is_err());
|
||||
let e = roots.check(&s(base.join("out/COM¹.txt")), Access::Write).unwrap_err();
|
||||
assert!(e.contains("reserved device names"), "{e}");
|
||||
assert_eq!(roots.check(&s(base.join("out/console.txt")), Access::Write), Ok(()));
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// Scopes nest, and a scope applies only on the thread that set it.
|
||||
#[test]
|
||||
fn scopes_nest_and_stay_on_their_thread() {
|
||||
let (base, roots) = folders("scopes");
|
||||
@@ -485,13 +436,12 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// What is a device name, with superscript digits and a name before a colon, and what isn't.
|
||||
#[test]
|
||||
fn device_names() {
|
||||
for d in ["CON", "nul", "Com1", "LPT9.txt", "aux.deckcraft", "NUL ", "COM¹", "Lpt²", "lpt³.txt", "COM1:", "nul:x", "COM¹:"] {
|
||||
for d in ["CON", "nul", "Com1", "LPT9.txt", "aux.deckcraft", "NUL "] {
|
||||
assert!(is_device_name(d), "{d}");
|
||||
}
|
||||
for f in ["console", "com10", "COM0", "lpt", "a.deckcraft", "nul-x", "COM⁴", "com¹0", "lpt¹x", "console:x"] {
|
||||
for f in ["console", "com10", "COM0", "lpt", "a.deckcraft", "nul-x"] {
|
||||
assert!(!is_device_name(f), "{f}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -646,9 +646,7 @@ impl Session {
|
||||
self.settle_composition();
|
||||
}
|
||||
if let Err(e) = (spec.enabled)(self) {
|
||||
// A command told which shapes to act on doesn't depend on what is selected.
|
||||
let named = (e == cmd::NOTHING_SELECTED || e == cmd::SELECT_TWO || e == cmd::merge::IN_GROUP)
|
||||
&& (params.get("ids").is_some() || params.get("id").is_some());
|
||||
let named = (e == cmd::NOTHING_SELECTED || e == cmd::SELECT_TWO) && (params.get("ids").is_some() || params.get("id").is_some());
|
||||
if !named {
|
||||
return Err(EngineError::Disabled(id.into(), e));
|
||||
}
|
||||
|
||||
@@ -699,8 +699,6 @@ pub fn add_user_word(word: &str) -> std::io::Result<()> {
|
||||
return Ok(());
|
||||
}
|
||||
let path = user_dictionary_path().ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "no folder for the user dictionary"))?;
|
||||
// A confined automation client may write only inside its write roots, this file included.
|
||||
crate::file_access::check_write_path(&path).map_err(|e| std::io::Error::new(std::io::ErrorKind::PermissionDenied, e))?;
|
||||
if let Some(d) = path.parent() {
|
||||
std::fs::create_dir_all(d)?;
|
||||
}
|
||||
@@ -1022,20 +1020,6 @@ SFX D Y 1\nSFX D 0 ed/S .\n";
|
||||
assert_eq!(w, vec!["check", "done"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_confined_client_cannot_write_the_user_dictionary_outside_its_roots() {
|
||||
let root = std::env::temp_dir().join(format!("deckcraft-dict-root-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&root).unwrap();
|
||||
let roots = crate::file_access::AutomationRoots::new(std::slice::from_ref(&root), std::slice::from_ref(&root)).unwrap();
|
||||
// The user dictionary is a file of its own outside `root`, not whatever HOME points at.
|
||||
let outside = std::env::temp_dir().join(format!("deckcraft-dict-outside-{}", std::process::id()));
|
||||
set_user_dictionary_path(outside.join("user.dic"));
|
||||
let r = crate::file_access::confine(roots.as_ref(), || add_user_word("zqxconfinedword"));
|
||||
assert_eq!(r.map_err(|e| e.kind()), Err(std::io::ErrorKind::PermissionDenied));
|
||||
assert!(!outside.exists(), "the refused write created nothing");
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_long_paragraph_with_a_huge_language_is_checked_in_linear_work() {
|
||||
use deckcraft_model::{Paragraph, Run, RunProps};
|
||||
|
||||
+5
-451
@@ -98,33 +98,6 @@ fn text_editing_flow() {
|
||||
assert_eq!(s.doc().unwrap().selection.shapes.len(), 1);
|
||||
}
|
||||
|
||||
/// Forward Delete removes a whole grapheme cluster, not just its first scalar.
|
||||
#[test]
|
||||
fn forward_delete_removes_whole_grapheme_cluster() {
|
||||
for (input, rest) in [("\u{1F44D}\u{1F3FD}y", "y"), ("e\u{301}x", "x")] {
|
||||
let mut s = session();
|
||||
let title = s.doc().unwrap().current_slide().unwrap().shapes[0].id.0;
|
||||
s.execute("text.edit", &json!({"id": title})).unwrap();
|
||||
s.execute("text.selectAll", &json!({})).unwrap();
|
||||
s.execute("text.insert", &json!({"text": input})).unwrap();
|
||||
s.execute("text.move", &json!({"to": "start"})).unwrap();
|
||||
s.execute("text.delete", &json!({"dir": "forward"})).unwrap();
|
||||
let t = s.execute("text.get", &json!({"id": title})).unwrap();
|
||||
assert_eq!(t["text"], rest);
|
||||
}
|
||||
// Inside a cluster, Delete stops at that cluster's end and leaves the next flag intact.
|
||||
let mut s = session();
|
||||
let title = s.doc().unwrap().current_slide().unwrap().shapes[0].id.0;
|
||||
s.execute("text.edit", &json!({"id": title})).unwrap();
|
||||
s.execute("text.selectAll", &json!({})).unwrap();
|
||||
s.execute("text.insert", &json!({"text": "\u{1F1FA}\u{1F1F8}\u{1F1E8}\u{1F1E6}"})).unwrap();
|
||||
s.execute("text.move", &json!({"to": "start"})).unwrap();
|
||||
s.execute("text.move", &json!({"to": "right"})).unwrap();
|
||||
s.execute("text.delete", &json!({"dir": "forward"})).unwrap();
|
||||
let t = s.execute("text.get", &json!({"id": title})).unwrap();
|
||||
assert_eq!(t["text"], "\u{1F1FA}\u{1F1E8}\u{1F1E6}");
|
||||
}
|
||||
|
||||
/// `text.set` treats CRLF (or a lone CR) as one paragraph break and keeps no `\r` in the text.
|
||||
#[test]
|
||||
fn text_set_crlf_makes_clean_paragraphs() {
|
||||
@@ -241,71 +214,6 @@ fn clipboard_copy_paste_shapes_and_slides() {
|
||||
assert!(s.doc().unwrap().doc.validate().is_empty());
|
||||
}
|
||||
|
||||
/// A shape copied or cut from inside a group pastes where it was drawn: on another slide in the
|
||||
/// same place, on its own slide offset as any paste there is. Its box is mapped out through each
|
||||
/// group around it, however the groups were moved, stretched, turned or flipped. Before, the
|
||||
/// clipboard kept the box inside the group, so the shape pasted where it had been before the group
|
||||
/// was moved or stretched.
|
||||
#[test]
|
||||
fn a_shape_copied_from_a_group_pastes_where_it_was_drawn() {
|
||||
fn rect(s: &mut Session, x: f64, y: f64) -> u64 {
|
||||
s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, y, 100, 100]})).unwrap()["id"].as_u64().unwrap()
|
||||
}
|
||||
let sid = |id: u64| ShapeId(u32::try_from(id).unwrap());
|
||||
let sites = |s: &Session, id: ShapeId| {
|
||||
let st = s.doc().unwrap();
|
||||
connect::sites_of(&st.doc, &st.selection, st.shapes(), id).unwrap()
|
||||
};
|
||||
type Edits<'a> = &'a [(&'a str, Value)];
|
||||
let edit = |s: &mut Session, id: u64, edits: Edits<'_>| {
|
||||
for (cmd, p) in edits {
|
||||
let mut p = p.clone();
|
||||
p["ids"] = json!([id]);
|
||||
s.execute(cmd, &p).unwrap();
|
||||
}
|
||||
};
|
||||
// Edits to the shape's group, then to a group around that one (none: no outer group).
|
||||
let cases: [(Edits<'_>, Edits<'_>); 4] = [
|
||||
(&[("shape.move", json!({"dx": 200}))], &[]),
|
||||
(&[("shape.resize", json!({"w": 600}))], &[]),
|
||||
(&[("shape.rotate", json!({"deg": 30})), ("shape.flip", json!({"axis": "vertical"})), ("shape.move", json!({"dx": 50, "dy": 20}))], &[]),
|
||||
(&[("shape.move", json!({"dx": 200}))], &[("shape.resize", json!({"w": 450})), ("shape.move", json!({"dy": 60}))]),
|
||||
];
|
||||
let off = session().prefs.duplicate_offset;
|
||||
for (n, (inner_edits, outer_edits)) in cases.into_iter().enumerate() {
|
||||
for (cmd, same_slide) in [("edit.copy", false), ("edit.cut", false), ("edit.copy", true)] {
|
||||
let mut s = session();
|
||||
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
|
||||
let (a, b) = (rect(&mut s, 100.0, 100.0), rect(&mut s, 300.0, 100.0));
|
||||
let g = s.execute("arrange.group", &json!({"ids": [a, b]})).unwrap()["id"].as_u64().unwrap();
|
||||
edit(&mut s, g, inner_edits);
|
||||
if !outer_edits.is_empty() {
|
||||
let c = rect(&mut s, 100.0, 300.0);
|
||||
let outer = s.execute("arrange.group", &json!({"ids": [g, c]})).unwrap()["id"].as_u64().unwrap();
|
||||
edit(&mut s, outer, outer_edits);
|
||||
}
|
||||
let was = sites(&s, sid(a));
|
||||
s.doc_mut().unwrap().selection.shapes = vec![sid(a)];
|
||||
s.execute(cmd, &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().shape(sid(a)).is_some(), cmd == "edit.copy", "case {n}: {cmd}");
|
||||
if !same_slide {
|
||||
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
|
||||
}
|
||||
let r = s.execute("edit.paste", &json!({})).unwrap();
|
||||
let pasted = sid(r["ids"][0].as_u64().unwrap());
|
||||
assert!(s.doc().unwrap().shapes().iter().any(|x| x.id == pasted), "case {n}: {cmd} pastes on the top level");
|
||||
let d = if same_slide { off } else { 0.0 };
|
||||
let now = sites(&s, pasted);
|
||||
assert_eq!(was.len(), now.len(), "case {n}: {cmd}");
|
||||
assert!(
|
||||
was.iter().zip(&now).all(|(p, q)| (q.x - p.x - d).hypot(q.y - p.y - d) < 1e-6),
|
||||
"case {n}: {cmd} on the same slide {same_slide}: drawn at {was:?}, pasted at {now:?}"
|
||||
);
|
||||
assert_eq!(s.doc().unwrap().doc.validate(), Vec::<String>::new());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The resolved look of every paragraph and run of shape `id` on the current slide: size, font,
|
||||
/// colour, bold, bullet, indent, alignment, and the box's insets and anchor.
|
||||
fn resolved_look(s: &Session, id: ShapeId) -> String {
|
||||
@@ -977,154 +885,6 @@ fn group_ungroup_roundtrip_positions() {
|
||||
assert!((v["box"]["x"].as_f64().unwrap() - 310.0).abs() < 1e-6, "{v}");
|
||||
}
|
||||
|
||||
/// Ungrouping a group inside another group frees its shapes into the outer group, in its place
|
||||
/// in the outer group's order, drawn where they were, and selects them. Before, arrange.ungroup
|
||||
/// looked only at the slide's top level: it answered `{ids: []}` and changed nothing.
|
||||
#[test]
|
||||
fn ungrouping_a_nested_group_frees_its_shapes_into_the_outer_group() {
|
||||
fn rect(s: &mut Session, x: f64, y: f64) -> u64 {
|
||||
s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, y, 100, 100]})).unwrap()["id"].as_u64().unwrap()
|
||||
}
|
||||
let sid = |id: u64| ShapeId(u32::try_from(id).unwrap());
|
||||
let sites = |s: &Session, id: u64| {
|
||||
let st = s.doc().unwrap();
|
||||
connect::sites_of(&st.doc, &st.selection, st.shapes(), sid(id)).unwrap()
|
||||
};
|
||||
// The outer group moved and stretched; then an inner group turned and flipped, inside an outer
|
||||
// group that is turned and moved.
|
||||
type Edits<'a> = &'a [(&'a str, Value)];
|
||||
let cases: [(Edits<'_>, Edits<'_>); 2] = [
|
||||
(&[], &[("shape.move", json!({"dx": 200})), ("shape.resize", json!({"w": 450}))]),
|
||||
(
|
||||
&[("shape.rotate", json!({"deg": 30})), ("shape.flip", json!({"axis": "horizontal"}))],
|
||||
&[("shape.rotate", json!({"deg": 45})), ("shape.move", json!({"dx": 120, "dy": 40}))],
|
||||
),
|
||||
];
|
||||
let edit = |s: &mut Session, id: u64, edits: Edits<'_>| {
|
||||
for (cmd, p) in edits {
|
||||
let mut p = p.clone();
|
||||
p["ids"] = json!([id]);
|
||||
s.execute(cmd, &p).unwrap();
|
||||
}
|
||||
};
|
||||
for (n, (inner_edits, outer_edits)) in cases.into_iter().enumerate() {
|
||||
let mut s = session();
|
||||
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
|
||||
let (a, b, c) = (rect(&mut s, 100.0, 100.0), rect(&mut s, 300.0, 100.0), rect(&mut s, 100.0, 300.0));
|
||||
let inner = s.execute("arrange.group", &json!({"ids": [a, b]})).unwrap()["id"].as_u64().unwrap();
|
||||
edit(&mut s, inner, inner_edits);
|
||||
let outer = s.execute("arrange.group", &json!({"ids": [inner, c]})).unwrap()["id"].as_u64().unwrap();
|
||||
edit(&mut s, outer, outer_edits);
|
||||
let before = [sites(&s, a), sites(&s, b), sites(&s, c)];
|
||||
let r = s.execute("arrange.ungroup", &json!({"ids": [inner]})).unwrap();
|
||||
assert_eq!(r["ids"], json!([a, b]), "case {n}");
|
||||
assert_eq!(s.doc().unwrap().selection.shapes, [sid(a), sid(b)]);
|
||||
let st = s.doc().unwrap();
|
||||
let order: Vec<ShapeId> = deckcraft_model::find_shape(st.shapes(), sid(outer)).unwrap().children().iter().map(|x| x.id).collect();
|
||||
assert_eq!(order, [sid(a), sid(b), sid(c)], "case {n}");
|
||||
for (id, was) in [a, b, c].into_iter().zip(before) {
|
||||
let now = sites(&s, id);
|
||||
assert_eq!(was.len(), now.len(), "case {n}: shape {id}");
|
||||
assert!(was.iter().zip(&now).all(|(p, q)| (*p - *q).hypot() < 1e-6), "case {n}: shape {id} moved from {was:?} to {now:?}");
|
||||
}
|
||||
}
|
||||
// Two named groups, one inside the other, inside a third: the shapes of both go to the third,
|
||||
// each listed once.
|
||||
let mut s = session();
|
||||
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
|
||||
let (a, b, c, d) = (rect(&mut s, 100.0, 100.0), rect(&mut s, 300.0, 100.0), rect(&mut s, 100.0, 300.0), rect(&mut s, 300.0, 300.0));
|
||||
let inner = s.execute("arrange.group", &json!({"ids": [a, b]})).unwrap()["id"].as_u64().unwrap();
|
||||
let middle = s.execute("arrange.group", &json!({"ids": [inner, c]})).unwrap()["id"].as_u64().unwrap();
|
||||
let outer = s.execute("arrange.group", &json!({"ids": [middle, d]})).unwrap()["id"].as_u64().unwrap();
|
||||
s.execute("shape.resize", &json!({"ids": [outer], "w": 600})).unwrap();
|
||||
let before = [a, b, c, d].map(|id| sites(&s, id));
|
||||
let r = s.execute("arrange.ungroup", &json!({"ids": [inner, middle]})).unwrap();
|
||||
assert_eq!(r["ids"], json!([a, b, c]));
|
||||
assert_eq!(s.doc().unwrap().selection.shapes, [sid(a), sid(b), sid(c)]);
|
||||
let st = s.doc().unwrap();
|
||||
let order: Vec<ShapeId> = deckcraft_model::find_shape(st.shapes(), sid(outer)).unwrap().children().iter().map(|x| x.id).collect();
|
||||
assert_eq!(order, [sid(a), sid(b), sid(c), sid(d)]);
|
||||
for (id, was) in [a, b, c, d].into_iter().zip(before) {
|
||||
let now = sites(&s, id);
|
||||
assert_eq!(was.len(), now.len(), "shape {id}");
|
||||
assert!(was.iter().zip(&now).all(|(p, q)| (*p - *q).hypot() < 1e-6), "shape {id} moved from {was:?} to {now:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Ungrouping nested groups in one call returns each freed shape once, whatever order the groups
|
||||
/// are named in. Before, a group freed into a group that the same call freed next was listed twice
|
||||
/// and replaced once, so the call returned the id of a group that no longer existed.
|
||||
#[test]
|
||||
fn ungrouping_nested_groups_in_any_order_returns_only_freed_shapes() {
|
||||
fn rect(s: &mut Session, x: f64, y: f64) -> u64 {
|
||||
s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, y, 100, 100]})).unwrap()["id"].as_u64().unwrap()
|
||||
}
|
||||
fn group(s: &mut Session, ids: [u64; 2]) -> u64 {
|
||||
s.execute("arrange.group", &json!({"ids": ids})).unwrap()["id"].as_u64().unwrap()
|
||||
}
|
||||
let sid = |id: u64| ShapeId(u32::try_from(id).unwrap());
|
||||
let mut s = session();
|
||||
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
|
||||
let [a, b, c, d, e] = [(100.0, 100.0), (300.0, 100.0), (100.0, 300.0), (300.0, 300.0), (500.0, 300.0)].map(|(x, y)| rect(&mut s, x, y));
|
||||
let inner = group(&mut s, [a, b]);
|
||||
let middle = group(&mut s, [inner, c]);
|
||||
let upper = group(&mut s, [middle, d]);
|
||||
let outer = group(&mut s, [upper, e]);
|
||||
let r = s.execute("arrange.ungroup", &json!({"ids": [middle, upper, inner]})).unwrap();
|
||||
assert_eq!(r["ids"], json!([a, b, c, d]));
|
||||
assert_eq!(s.doc().unwrap().selection.shapes, [sid(a), sid(b), sid(c), sid(d)]);
|
||||
let st = s.doc().unwrap();
|
||||
let order: Vec<ShapeId> = deckcraft_model::find_shape(st.shapes(), sid(outer)).unwrap().children().iter().map(|x| x.id).collect();
|
||||
assert_eq!(order, [sid(a), sid(b), sid(c), sid(d), sid(e)]);
|
||||
}
|
||||
|
||||
/// Ungrouping a top-level group and a group inside it in one call frees the shapes of both onto
|
||||
/// the slide, in the top-level group's place and drawn where they were, whichever is named first.
|
||||
#[test]
|
||||
fn ungrouping_a_top_level_group_with_a_group_inside_it_frees_both() {
|
||||
fn rect(s: &mut Session, x: f64, y: f64) -> u64 {
|
||||
s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, y, 100, 100]})).unwrap()["id"].as_u64().unwrap()
|
||||
}
|
||||
fn group(s: &mut Session, ids: [u64; 2]) -> u64 {
|
||||
s.execute("arrange.group", &json!({"ids": ids})).unwrap()["id"].as_u64().unwrap()
|
||||
}
|
||||
let sid = |id: u64| ShapeId(u32::try_from(id).unwrap());
|
||||
let sites = |s: &Session, id: u64| {
|
||||
let st = s.doc().unwrap();
|
||||
connect::sites_of(&st.doc, &st.selection, st.shapes(), sid(id)).unwrap()
|
||||
};
|
||||
let roots = |s: &Session| -> Vec<ShapeId> { s.doc().unwrap().shapes().iter().map(|x| x.id).collect() };
|
||||
for outer_first in [true, false] {
|
||||
let mut s = session();
|
||||
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
|
||||
let [a, b, c, z] = [(100.0, 100.0), (300.0, 100.0), (100.0, 300.0), (500.0, 500.0)].map(|(x, y)| rect(&mut s, x, y));
|
||||
let inner = group(&mut s, [a, b]);
|
||||
let outer = group(&mut s, [inner, c]);
|
||||
s.execute("shape.resize", &json!({"ids": [outer], "w": 600})).unwrap();
|
||||
let before = [a, b, c].map(|id| sites(&s, id));
|
||||
let freed = [sid(a), sid(b), sid(c)];
|
||||
let mut expected = vec![];
|
||||
for x in roots(&s) {
|
||||
if x == sid(outer) {
|
||||
expected.extend(freed);
|
||||
} else {
|
||||
expected.push(x);
|
||||
}
|
||||
}
|
||||
assert!(expected.contains(&sid(z)));
|
||||
let ids = if outer_first { [outer, inner] } else { [inner, outer] };
|
||||
let r = s.execute("arrange.ungroup", &json!({"ids": ids})).unwrap();
|
||||
assert_eq!(r["ids"], json!([a, b, c]), "{ids:?}");
|
||||
assert_eq!(s.doc().unwrap().selection.shapes, freed, "{ids:?}");
|
||||
assert_eq!(roots(&s), expected, "{ids:?}");
|
||||
for (id, was) in [a, b, c].into_iter().zip(before) {
|
||||
let now = sites(&s, id);
|
||||
assert_eq!(was.len(), now.len(), "{ids:?}: shape {id}");
|
||||
assert!(was.iter().zip(&now).all(|(p, q)| (*p - *q).hypot() < 1e-6), "{ids:?}: shape {id} moved from {was:?} to {now:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Grouping a group again and again stops at `MAX_GROUP_DEPTH`: past about 39 levels a saved
|
||||
/// .deckcraft file no longer opens, and past 64 commands such as format.languageAll miss the text.
|
||||
#[test]
|
||||
@@ -1184,82 +944,6 @@ fn distribute_wide_middle_stays_in_selection_bounds() {
|
||||
assert!(xs.iter().all(|x| (*x - [0.0, 10.0, 110.0][xs.iter().position(|y| y == x).unwrap()]).abs() < 1e-6), "{xs:?}");
|
||||
}
|
||||
|
||||
/// An `edge`, `dir` or `to` that the commands do not name, or no `edge` at all, is refused with
|
||||
/// the accepted values. Before, such a call reported its shapes as edited and moved nothing,
|
||||
/// aligned to the selection, or fell back to the left edge or to horizontal.
|
||||
#[test]
|
||||
fn align_and_distribute_refuse_values_they_do_not_name() {
|
||||
let mut s = session();
|
||||
let mut ids = vec![];
|
||||
for (x, y) in [(10.0, 10.0), (200.0, 50.0), (500.0, 200.0)] {
|
||||
ids.push(s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, y, 40, 40]})).unwrap()["id"].as_u64().unwrap());
|
||||
}
|
||||
s.execute("edit.select", &json!({"ids": ids})).unwrap();
|
||||
let before = s.doc().unwrap().revision;
|
||||
let edges = "`edge` must be left, center, right, top, middle or bottom";
|
||||
let dirs = "`dir` must be horizontal or vertical";
|
||||
let tos = "`to` must be slide or selection";
|
||||
for (cmd, params, want) in [
|
||||
("arrange.align", json!({"edge": "centre"}), edges),
|
||||
("arrange.align", json!({"edge": "Left"}), edges),
|
||||
("arrange.align", json!({"edge": "hcenter"}), edges),
|
||||
("arrange.align", json!({}), edges),
|
||||
("arrange.align", json!({"ids": [ids[0]], "edge": "left", "to": "Slide"}), tos),
|
||||
("arrange.distribute", json!({"dir": "Vertical"}), dirs),
|
||||
("arrange.distribute", json!({"dir": "columns"}), dirs),
|
||||
("arrange.distribute", json!({"dir": "v"}), dirs),
|
||||
("arrange.distribute", json!({"dir": "vertical", "to": "page"}), tos),
|
||||
] {
|
||||
let err = s.execute(cmd, ¶ms).unwrap_err().to_string();
|
||||
assert!(err.contains(want), "{cmd} {params}: {err}");
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().revision, before, "a refused call leaves the slide as it was");
|
||||
let pos = |s: &mut Session, id: u64, k: &str| s.execute("shape.inspect", &json!({"id": id})).unwrap()["box"][k].as_f64().unwrap();
|
||||
s.execute("arrange.align", &json!({"ids": [ids[1]], "edge": "left", "to": "slide"})).unwrap();
|
||||
assert!(pos(&mut s, ids[1], "x").abs() < 1e-6);
|
||||
// Within the selection's span, 10..240: 120 pt of shapes leaves two 55 pt gaps.
|
||||
s.execute("arrange.distribute", &json!({"ids": ids, "dir": "vertical", "to": "selection"})).unwrap();
|
||||
assert!((pos(&mut s, ids[1], "y") - 105.0).abs() < 1e-6);
|
||||
}
|
||||
|
||||
/// A `dir` or `to` that is present but is not a string (null, a number, a bool, an object or an
|
||||
/// array) is refused with the accepted values, and the shapes and the revision stay as they were.
|
||||
/// Before, such a value counted as left out: `dir` meant horizontal and `to` each command's
|
||||
/// default, so the call moved the selected shapes.
|
||||
#[test]
|
||||
fn align_and_distribute_refuse_dir_and_to_that_are_not_strings() {
|
||||
let dirs = "`dir` must be horizontal or vertical";
|
||||
let tos = "`to` must be slide or selection";
|
||||
let mut failures = vec![];
|
||||
for v in [json!(null), json!(123), json!(false), json!({}), json!([])] {
|
||||
for (cmd, params, want) in [
|
||||
("arrange.distribute", json!({"dir": v.clone()}), dirs),
|
||||
("arrange.distribute", json!({"dir": "horizontal", "to": v.clone()}), tos),
|
||||
("arrange.align", json!({"edge": "left", "to": v.clone()}), tos),
|
||||
] {
|
||||
let mut s = session();
|
||||
let mut ids = vec![];
|
||||
for (x, y) in [(10.0, 10.0), (200.0, 50.0), (500.0, 200.0)] {
|
||||
ids.push(s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, y, 40, 40]})).unwrap()["id"].as_u64().unwrap());
|
||||
}
|
||||
s.execute("edit.select", &json!({"ids": ids})).unwrap();
|
||||
let boxes = |s: &mut Session| -> Vec<Value> {
|
||||
ids.iter().map(|id| s.execute("shape.inspect", &json!({"id": id})).unwrap()["box"].clone()).collect()
|
||||
};
|
||||
let (rev, before) = (s.doc().unwrap().revision, boxes(&mut s));
|
||||
match s.execute(cmd, ¶ms) {
|
||||
Ok(_) => failures.push(format!("{cmd} {params}: accepted")),
|
||||
Err(e) if !e.to_string().contains(want) => failures.push(format!("{cmd} {params}: {e}")),
|
||||
Err(_) => {}
|
||||
}
|
||||
if s.doc().unwrap().revision != rev || boxes(&mut s) != before {
|
||||
failures.push(format!("{cmd} {params}: moved the shapes"));
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(failures.is_empty(), "{failures:#?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pointer_draw_move_resize() {
|
||||
let mut s = session();
|
||||
@@ -4476,10 +4160,10 @@ fn smartart_round_trips_through_pptx_as_a_diagram() {
|
||||
}
|
||||
let back = deckcraft_pptx::import(&bytes).unwrap();
|
||||
assert!(back.validate().is_empty());
|
||||
let g = back.slides[1].shapes.iter().find(|x| x.diagram.is_some()).expect("a Diagram group");
|
||||
let g = back.slides[1].shapes.iter().find(|x| x.diagram.is_some()).expect("a SmartArt group");
|
||||
let d = g.diagram.as_ref().unwrap();
|
||||
assert_eq!(d.kind, "hierarchy");
|
||||
assert_eq!((d.style.as_str(), d.colors.as_str()), ("intense", "accent3"), "Diagram style and colours come back");
|
||||
assert_eq!((d.style.as_str(), d.colors.as_str()), ("intense", "accent3"), "SmartArt style and colours come back");
|
||||
assert!(d.imported);
|
||||
let outline: Vec<(&str, u8)> = d.nodes.iter().map(|n| (n.text.as_str(), n.level)).collect();
|
||||
assert_eq!(outline, [("CEO", 0), ("CTO", 1), ("Dev", 2), ("CFO", 1)]);
|
||||
@@ -4507,7 +4191,7 @@ fn smartart_pictures_3d_styles_and_colour_variants() {
|
||||
let pics = |s: &Session| -> Vec<deckcraft_model::Shape> {
|
||||
let st = s.doc().unwrap();
|
||||
let g = st.doc.slides[1].shapes.iter().find(|x| x.diagram.is_some()).unwrap().clone();
|
||||
g.children().iter().filter(|c| c.name == "Diagram picture").cloned().collect()
|
||||
g.children().iter().filter(|c| c.name == "SmartArt picture").cloned().collect()
|
||||
};
|
||||
assert_eq!(pics(&s).len(), 2, "one picture placeholder per top-level node");
|
||||
assert!(pics(&s).iter().all(|p| !matches!(p.fill, Some(deckcraft_model::Fill::Picture(_)))));
|
||||
@@ -6910,66 +6594,6 @@ fn duplicating_a_moved_copy_repeats_the_move() {
|
||||
assert_eq!(s.doc().unwrap().doc.validate(), Vec::<String>::new());
|
||||
}
|
||||
|
||||
/// Duplicating a shape inside a group puts the copy in that group, last among its children, and
|
||||
/// selects it. The copy is offset on the slide as a top-level copy is, however the group is moved,
|
||||
/// stretched or turned, and duplicating a moved copy repeats the move (#966). A shape whose group is
|
||||
/// duplicated as well goes along with the group's copy. Before, Duplicate looked only at the slide's
|
||||
/// top level: it answered `{ids: []}` and changed nothing.
|
||||
#[test]
|
||||
fn duplicating_a_shape_in_a_group_copies_it_into_the_group() {
|
||||
fn rect(s: &mut Session, x: f64) -> u64 {
|
||||
s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, 100, 100, 100]})).unwrap()["id"].as_u64().unwrap()
|
||||
}
|
||||
let sid = |id: u64| ShapeId(u32::try_from(id).unwrap());
|
||||
// How far shape `to` is drawn from shape `from` on the slide, the same for each of its sites.
|
||||
let offset = |s: &Session, from: u64, to: u64| {
|
||||
let st = s.doc().unwrap();
|
||||
let sites = |id| connect::sites_of(&st.doc, &st.selection, st.shapes(), sid(id)).unwrap();
|
||||
let (p, q) = (sites(from), sites(to));
|
||||
assert_eq!(p.len(), q.len(), "{p:?} {q:?}");
|
||||
let d = q[0] - p[0];
|
||||
assert!(p.iter().zip(&q).all(|(p, q)| (*q - *p - d).hypot() < 1e-6), "{p:?} {q:?}");
|
||||
(d.x, d.y)
|
||||
};
|
||||
let grouped = |edit: &str, p: Value| {
|
||||
let mut s = session();
|
||||
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
|
||||
let (a, b) = (rect(&mut s, 100.0), rect(&mut s, 300.0));
|
||||
let g = s.execute("arrange.group", &json!({"ids": [a, b]})).unwrap()["id"].as_u64().unwrap();
|
||||
let mut p = p;
|
||||
p["ids"] = json!([g]);
|
||||
s.execute(edit, &p).unwrap();
|
||||
(s, a, b, g)
|
||||
};
|
||||
for (edit, p) in [("shape.move", json!({"dx": 200})), ("shape.resize", json!({"w": 600})), ("shape.rotate", json!({"deg": 90}))] {
|
||||
let (mut s, a, b, g) = grouped(edit, p);
|
||||
let off = s.prefs.duplicate_offset;
|
||||
let r = s.execute("edit.duplicate", &json!({"ids": [a]})).unwrap();
|
||||
assert_eq!(r["ids"].as_array().map(Vec::len), Some(1), "{edit}: {r}");
|
||||
let c = r["ids"][0].as_u64().unwrap();
|
||||
assert_eq!(s.doc().unwrap().selection.shapes, [sid(c)]);
|
||||
let st = s.doc().unwrap();
|
||||
let order: Vec<ShapeId> = deckcraft_model::find_shape(st.shapes(), sid(g)).unwrap().children().iter().map(|x| x.id).collect();
|
||||
assert_eq!(order, [sid(a), sid(b), sid(c)], "{edit}");
|
||||
let (dx, dy) = offset(&s, a, c);
|
||||
assert!((dx - off).abs() < 1e-6 && (dy - off).abs() < 1e-6, "{edit}: the copy is drawn ({dx}, {dy}) from the original");
|
||||
// Moved, then duplicated again: the next copy is drawn as far from it again.
|
||||
s.execute("shape.move", &json!({"ids": [c], "dx": 30})).unwrap();
|
||||
let (mx, my) = offset(&s, a, c);
|
||||
let d = s.execute("edit.duplicate", &json!({"ids": [c]})).unwrap()["ids"][0].as_u64().unwrap();
|
||||
let (nx, ny) = offset(&s, c, d);
|
||||
assert!((nx - mx).abs() < 1e-6 && (ny - my).abs() < 1e-6, "{edit}: moved ({mx}, {my}), then ({nx}, {ny})");
|
||||
assert_eq!(s.doc().unwrap().doc.validate(), Vec::<String>::new());
|
||||
}
|
||||
let (mut s, a, _, g) = grouped("shape.move", json!({"dx": 200}));
|
||||
let r = s.execute("edit.duplicate", &json!({"ids": [a, g]})).unwrap();
|
||||
assert_eq!(r["ids"].as_array().map(Vec::len), Some(1), "{r}");
|
||||
let st = s.doc().unwrap();
|
||||
let count = |id: u64| deckcraft_model::find_shape(st.shapes(), sid(id)).unwrap().children().len();
|
||||
assert_eq!((count(g), count(r["ids"][0].as_u64().unwrap())), (2, 2));
|
||||
assert_eq!(st.shapes().len(), 2);
|
||||
}
|
||||
|
||||
fn chart_of(s: &Session, id: u64) -> deckcraft_model::Chart {
|
||||
match &s.doc().unwrap().shape(deckcraft_model::ShapeId(id as u32)).unwrap().kind {
|
||||
deckcraft_model::ShapeKind::Chart(c) => (**c).clone(),
|
||||
@@ -7297,76 +6921,6 @@ fn master_body_placeholder_format_keeps_per_level_formatting() {
|
||||
assert_eq!(shown(&s), vec![Some(28.0), Some(24.0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_edits_past_the_cell_cap_are_refused_not_trimmed() {
|
||||
use deckcraft_model::table::{MAX_CELLS, Row};
|
||||
let mut s = session();
|
||||
let id = table_at(&mut s, &[100.0, 100.0], json!([["a", "b"], ["c", "d"]]));
|
||||
let cols = 200;
|
||||
let rows = MAX_CELLS / cols;
|
||||
let big = deckcraft_model::Table {
|
||||
cols: vec![1.0; cols],
|
||||
rows: vec![Row { height: 1.0, cells: vec![Default::default(); cols] }; rows],
|
||||
..Default::default()
|
||||
};
|
||||
for sl in std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).slides.iter_mut() {
|
||||
for sh in std::sync::Arc::make_mut(sl).shapes.iter_mut() {
|
||||
if sh.id == id {
|
||||
sh.kind = deckcraft_model::ShapeKind::Table(big.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
for cmd in ["table.insertRowAbove", "table.insertRowBelow"] {
|
||||
let e = s.execute(cmd, &json!({"id": id.0, "row": 0})).unwrap_err().to_string();
|
||||
assert!(e.contains("cell limit"), "{cmd}: {e}");
|
||||
}
|
||||
let e = s.execute("table.insertColumnLeft", &json!({"id": id.0, "col": 0})).unwrap_err().to_string();
|
||||
assert!(e.contains("cell limit"), "{e}");
|
||||
assert_eq!(table_of(&s, id), big, "a refused insert leaves every row in place");
|
||||
// A table already over the cap is refused too, not trimmed behind a success.
|
||||
let huge = deckcraft_model::Table { rows: vec![Row { height: 1.0, cells: vec![Default::default(); cols] }; rows + 1], ..big.clone() };
|
||||
for sl in std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).slides.iter_mut() {
|
||||
for sh in std::sync::Arc::make_mut(sl).shapes.iter_mut() {
|
||||
if sh.id == id {
|
||||
sh.kind = deckcraft_model::ShapeKind::Table(huge.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(s.execute("table.deleteRow", &json!({"id": id.0, "row": 0})).is_err());
|
||||
assert_eq!(table_of(&s, id).n_rows(), rows + 1);
|
||||
}
|
||||
|
||||
/// #2059: Decrease Font Size (Ctrl+[, Cmd+Shift+,) steps down one size at a time from any size:
|
||||
/// PowerPoint's list up to 96, then 8 points at a time above it, as Increase Font Size steps up.
|
||||
/// A size above 66 used to shrink straight to 66.
|
||||
#[test]
|
||||
fn shrinking_a_large_font_steps_down_one_size_at_a_time() {
|
||||
let mut s = session();
|
||||
let r = s.execute("shape.insert", &json!({"preset": "rect", "rect": [80.0, 80.0, 420.0, 160.0], "text": "Big"})).unwrap();
|
||||
let id = deckcraft_model::ShapeId(u32::try_from(r["id"].as_u64().unwrap()).unwrap());
|
||||
let size = |s: &Session| s.doc().unwrap().shape(id).unwrap().text.as_ref().unwrap().paragraphs[0].runs[0].props.size;
|
||||
s.execute("format.size", &json!({"size": 120})).unwrap();
|
||||
let mut down = vec![];
|
||||
for _ in 0..9 {
|
||||
s.execute("format.shrink", &json!({})).unwrap();
|
||||
down.push(size(&s).unwrap());
|
||||
}
|
||||
assert_eq!(down, [112.0, 104.0, 96.0, 88.0, 80.0, 72.0, 66.0, 60.0, 54.0]);
|
||||
let mut up = vec![];
|
||||
for _ in 0..7 {
|
||||
s.execute("format.grow", &json!({})).unwrap();
|
||||
up.push(size(&s).unwrap());
|
||||
}
|
||||
assert_eq!(up, [60.0, 66.0, 72.0, 80.0, 88.0, 96.0, 104.0]);
|
||||
// A size between list steps goes to the next one down, and one past the top steps 8 at most.
|
||||
s.execute("format.size", &json!({"size": 99})).unwrap();
|
||||
s.execute("format.shrink", &json!({})).unwrap();
|
||||
assert_eq!(size(&s), Some(96.0));
|
||||
s.execute("format.size", &json!({"size": 150})).unwrap();
|
||||
s.execute("format.shrink", &json!({})).unwrap();
|
||||
assert_eq!(size(&s), Some(142.0));
|
||||
}
|
||||
|
||||
/// A shape named many times in one edit is edited once and not copied per mention.
|
||||
#[test]
|
||||
fn repeated_target_ids_edit_the_shape_once() {
|
||||
@@ -7500,13 +7054,13 @@ fn flip_is_refused_on_a_smartart_graphic_and_leaves_it_a_graphic() {
|
||||
let gid = s.execute("smartArt.fromText", &json!({"kind": "blockList"})).unwrap()["id"].as_u64().unwrap();
|
||||
for cmd in ["arrange.flipHorizontal", "arrange.flipVertical", "shape.flip"] {
|
||||
let e = s.execute(cmd, &json!({"ids": [gid]})).unwrap_err().to_string();
|
||||
assert!(e.contains("Diagram") && e.contains("smartArt.layout"), "{cmd}: {e}");
|
||||
assert!(e.contains("SmartArt") && e.contains("smartArt.layout"), "{cmd}: {e}");
|
||||
}
|
||||
// Grouped with another shape it cannot be flipped either: ungrouping would hand it the flip.
|
||||
let other = s.execute("insert.textBox", &json!({"text": "x"})).unwrap()["id"].as_u64().unwrap();
|
||||
let group = s.execute("arrange.group", &json!({"ids": [gid, other]})).unwrap()["id"].as_u64().unwrap();
|
||||
let e = s.execute("shape.flip", &json!({"ids": [group]})).unwrap_err().to_string();
|
||||
assert!(e.contains("Diagram"), "{e}");
|
||||
assert!(e.contains("SmartArt"), "{e}");
|
||||
s.execute("arrange.ungroup", &json!({"ids": [group]})).unwrap();
|
||||
// Still a SmartArt graphic: smartArt commands keep working.
|
||||
s.execute("smartArt.layout", &json!({"id": gid, "kind": "cycle"})).unwrap();
|
||||
|
||||
@@ -293,7 +293,7 @@ impl Session {
|
||||
if let Some(sh) = deckcraft_model::find_shape_mut(list, id)
|
||||
&& let deckcraft_model::Geom::Preset { name, adj } = &mut sh.geom
|
||||
{
|
||||
let defaults = deckcraft_geom::preset::adjust_defaults(name);
|
||||
let defaults = deckcraft_geom::preset::info(name).map(|x| x.defaults).unwrap_or(&[]);
|
||||
for (i, v) in &vals {
|
||||
while adj.len() <= *i {
|
||||
let k = adj.len();
|
||||
|
||||
@@ -353,7 +353,7 @@ impl Session {
|
||||
bp.warp.map(|n| (n, bp.warp_adj))
|
||||
});
|
||||
if let Some((name, base)) = found {
|
||||
self.begin("Adjust Decorative Text");
|
||||
self.begin("Adjust WordArt");
|
||||
self.tool.drag = Some(Drag::Warp { id, index, name, base, orig });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,14 +169,8 @@ impl FontDb {
|
||||
if bundled.contains(fam) {
|
||||
continue;
|
||||
}
|
||||
if self.is_loaded(fam) || (self.system_fallback_enabled() && self.has_family(fam)) {
|
||||
// With system fallback off, only a face that is already loaded: `face` could load more catalog files.
|
||||
let f = if self.system_fallback_enabled() {
|
||||
self.face(fam, style)
|
||||
} else {
|
||||
let Some(f) = self.find(fam, style) else { continue };
|
||||
f
|
||||
};
|
||||
if self.is_loaded(fam) || self.has_family(fam) {
|
||||
let f = self.face(fam, style);
|
||||
if f.family.eq_ignore_ascii_case(fam) && covers_cluster(&f, cluster) {
|
||||
return f;
|
||||
}
|
||||
@@ -317,21 +311,4 @@ mod tests {
|
||||
assert!(c.windows(2).all(|w| w[0] <= w[1]));
|
||||
}
|
||||
}
|
||||
|
||||
/// With system fallback off, the automatic cascades (script families and symbol faces) load no
|
||||
/// installed family: the result comes from what is already loaded.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn cascades_load_no_installed_family_when_system_fallback_is_off() {
|
||||
let db = FontDb::with_font_dirs(Vec::new());
|
||||
db.set_system_fallback(false);
|
||||
let base = db.face(crate::DEFAULT_FAMILY, "Regular");
|
||||
let loaded = |db: &FontDb| [b"Arab", b"Hebr", b"Thai", b"Deva"].into_iter().flat_map(|s| os_families(*s)).filter(|f| db.is_loaded(f)).count();
|
||||
let before = loaded(&db);
|
||||
for c in ['\u{0628}', '\u{05D0}', '\u{0E01}', '\u{0905}', '\u{2211}', '\u{2603}'] {
|
||||
let f = db.cascade(&[c], std::slice::from_ref(&base), "Regular");
|
||||
assert!(!f.family.is_empty());
|
||||
}
|
||||
assert_eq!(loaded(&db), before, "no installed family was loaded");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -976,17 +976,6 @@ impl FontDb {
|
||||
let _ = on;
|
||||
}
|
||||
|
||||
/// Whether automatic system-font lookups (fallback by character, symbol and script families)
|
||||
/// may touch installed fonts. Lookups of a family the document or host names are not gated.
|
||||
pub(crate) fn system_fallback_enabled(&self) -> bool {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
self.sys.lock().unwrap_or_else(std::sync::PoisonError::into_inner).enabled
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
false
|
||||
}
|
||||
|
||||
/// Process-wide database: the bundled fonts, then the installed system fonts, cataloged the
|
||||
/// first time a lookup by family name needs them (or ahead of time by
|
||||
/// [`FontDb::scan_in_background`]).
|
||||
@@ -1298,7 +1287,7 @@ impl FontDb {
|
||||
Some(f)
|
||||
}
|
||||
|
||||
pub(crate) fn find(&self, family: &str, style: &str) -> Option<Arc<FontFace>> {
|
||||
fn find(&self, family: &str, style: &str) -> Option<Arc<FontFace>> {
|
||||
if style.contains('{') {
|
||||
{
|
||||
let faces = self.read_faces();
|
||||
@@ -1484,8 +1473,7 @@ impl FontDb {
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
let cataloged: Vec<String> =
|
||||
if self.system_fallback_enabled() { self.read_catalog().iter().map(|e| e.family.clone()).collect() } else { Vec::new() };
|
||||
let cataloged: Vec<String> = self.read_catalog().iter().map(|e| e.family.clone()).collect();
|
||||
for fam in SYSTEM_FALLBACKS.iter().filter(|f| f.contains("Symbol")) {
|
||||
if !self.is_loaded(fam) && cataloged.iter().any(|f| f.eq_ignore_ascii_case(fam)) {
|
||||
self.load_cataloged(fam);
|
||||
|
||||
@@ -425,11 +425,3 @@ fn the_font_scan_skips_fifos_folders_and_link_loops() {
|
||||
assert!(fifo || n >= 1);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disabling_system_fallback_gates_automatic_family_loads() {
|
||||
let db = FontDb::with_font_dirs(Vec::new());
|
||||
assert!(cfg!(target_arch = "wasm32") || db.system_fallback_enabled());
|
||||
db.set_system_fallback(false);
|
||||
assert!(!db.system_fallback_enabled());
|
||||
}
|
||||
|
||||
@@ -9,10 +9,8 @@ description = "DeckCraft native file format (.deckcraft: zip of a JSON manifest
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-deckcraft-format = { workspace = true }
|
||||
craft-rtf = { workspace = true }
|
||||
deckcraft-model = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
zip = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
//! Keep opaque chart payloads out of the JSON manifest and within the media load budget.
|
||||
use std::collections::HashSet;
|
||||
use std::io::Cursor;
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft_deckcraft_format::{MAX_ENTRY, Package, chart_part_entry, is_chart_part_entry};
|
||||
|
||||
use deckcraft_model::chart::ChartPart;
|
||||
use deckcraft_model::{Master, Presentation, Shape, ShapeKind};
|
||||
|
||||
use crate::{FormatError, Result};
|
||||
use crate::{FormatError, MAX_ENTRY, Result, read_entry_max};
|
||||
|
||||
const PREFIX: &str = "chart-parts/";
|
||||
type Payload = (String, String, Vec<u8>);
|
||||
|
||||
pub(super) fn separate(p: &mut Presentation) -> Result<Vec<Payload>> {
|
||||
@@ -16,7 +16,7 @@ pub(super) fn separate(p: &mut Presentation) -> Result<Vec<Payload>> {
|
||||
visit_parts(p, |part| {
|
||||
// The native namespace is independent of the original OPC names, which may repeat
|
||||
// when charts from several decks are combined. No user-supplied path is extracted.
|
||||
let entry = chart_part_entry(payloads.len());
|
||||
let entry = format!("{PREFIX}{}", payloads.len());
|
||||
part.native_entry = Some(entry.clone());
|
||||
payloads.push((entry, part.content_type.clone(), std::mem::take(&mut part.bytes)));
|
||||
Ok(())
|
||||
@@ -26,7 +26,7 @@ pub(super) fn separate(p: &mut Presentation) -> Result<Vec<Payload>> {
|
||||
|
||||
pub(super) fn restore(
|
||||
p: &mut Presentation,
|
||||
z: &mut Package<'_>,
|
||||
z: &mut zip::ZipArchive<Cursor<&[u8]>>,
|
||||
left: &mut u64,
|
||||
mut items: usize,
|
||||
max_items: usize,
|
||||
@@ -40,21 +40,21 @@ pub(super) fn restore(
|
||||
items += 1;
|
||||
if let Some(entry) = part.native_entry.take() {
|
||||
// Accept only names produced by separate(), and inflate each member once.
|
||||
let canonical = is_chart_part_entry(&entry);
|
||||
let canonical = entry.strip_prefix(PREFIX).and_then(|s| s.parse::<usize>().ok()).is_some_and(|n| entry == format!("{PREFIX}{n}"));
|
||||
if !canonical || !seen.insert(entry.clone()) || !part.bytes.is_empty() {
|
||||
return Err(FormatError::Damaged("invalid or ambiguous chart part reference".into()));
|
||||
}
|
||||
if !z.contains(&entry) {
|
||||
if z.index_for_name(&entry).is_none() {
|
||||
// Missing members consume no inflation work and can be reported for recovery.
|
||||
damaged.push(format!("{entry}: missing chart part"));
|
||||
return Ok(());
|
||||
}
|
||||
if z.declared_size(&entry).is_some_and(|size| size > *left) {
|
||||
if z.by_name(&entry).is_ok_and(|f| f.size() > *left) {
|
||||
return Err(FormatError::Damaged("embedded media and chart parts are larger than DeckCraft can open".into()));
|
||||
}
|
||||
// A CRC/decode error may already have inflated bytes. Stop on that first error
|
||||
// rather than letting later corrupt members reuse the unchanged byte budget.
|
||||
let bytes = z.read_max(&entry, (*left).min(MAX_ENTRY))?;
|
||||
let bytes = read_entry_max(z, &entry, (*left).min(MAX_ENTRY))?;
|
||||
*left -= bytes.len() as u64;
|
||||
part.bytes = bytes;
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
//! A serde serializer adapter that writes non-finite floats as 0.
|
||||
//!
|
||||
//! serde_json writes NaN and ±infinity as `null`, which doesn't read back as a number: a single
|
||||
//! NaN anywhere in the model (a rotation, a crop, a stroke width) made a saved file fail to open
|
||||
//! as "damaged". [`Finite`] wraps any `Serialize` value and replaces those floats on the way out;
|
||||
//! everything else passes through unchanged.
|
||||
|
||||
use serde::ser::{self, Serialize, Serializer};
|
||||
|
||||
/// `T`, serialized with non-finite floats written as 0.
|
||||
pub struct Finite<'a, T: ?Sized>(pub &'a T);
|
||||
|
||||
impl<T: ?Sized + Serialize> Serialize for Finite<'_, T> {
|
||||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize(Wrap(s))
|
||||
}
|
||||
}
|
||||
|
||||
struct Wrap<S>(S);
|
||||
|
||||
macro_rules! pass {
|
||||
($($m:ident($t:ty)),* $(,)?) => {
|
||||
$(fn $m(self, v: $t) -> Result<S::Ok, S::Error> { self.0.$m(v) })*
|
||||
};
|
||||
}
|
||||
|
||||
impl<S: Serializer> Serializer for Wrap<S> {
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
type SerializeSeq = Wrap<S::SerializeSeq>;
|
||||
type SerializeTuple = Wrap<S::SerializeTuple>;
|
||||
type SerializeTupleStruct = Wrap<S::SerializeTupleStruct>;
|
||||
type SerializeTupleVariant = Wrap<S::SerializeTupleVariant>;
|
||||
type SerializeMap = Wrap<S::SerializeMap>;
|
||||
type SerializeStruct = Wrap<S::SerializeStruct>;
|
||||
type SerializeStructVariant = Wrap<S::SerializeStructVariant>;
|
||||
|
||||
pass!(
|
||||
serialize_bool(bool),
|
||||
serialize_i8(i8),
|
||||
serialize_i16(i16),
|
||||
serialize_i32(i32),
|
||||
serialize_i64(i64),
|
||||
serialize_i128(i128),
|
||||
serialize_u8(u8),
|
||||
serialize_u16(u16),
|
||||
serialize_u32(u32),
|
||||
serialize_u64(u64),
|
||||
serialize_u128(u128),
|
||||
serialize_char(char),
|
||||
serialize_str(&str),
|
||||
serialize_bytes(&[u8]),
|
||||
);
|
||||
|
||||
fn serialize_f32(self, v: f32) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_f32(if v.is_finite() { v } else { 0.0 })
|
||||
}
|
||||
fn serialize_f64(self, v: f64) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_f64(if v.is_finite() { v } else { 0.0 })
|
||||
}
|
||||
fn serialize_none(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_none()
|
||||
}
|
||||
fn serialize_some<T: ?Sized + Serialize>(self, v: &T) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_some(&Finite(v))
|
||||
}
|
||||
fn serialize_unit(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_unit()
|
||||
}
|
||||
fn serialize_unit_struct(self, name: &'static str) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_unit_struct(name)
|
||||
}
|
||||
fn serialize_unit_variant(self, name: &'static str, i: u32, variant: &'static str) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_unit_variant(name, i, variant)
|
||||
}
|
||||
fn serialize_newtype_struct<T: ?Sized + Serialize>(self, name: &'static str, v: &T) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_newtype_struct(name, &Finite(v))
|
||||
}
|
||||
fn serialize_newtype_variant<T: ?Sized + Serialize>(self, name: &'static str, i: u32, variant: &'static str, v: &T) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_newtype_variant(name, i, variant, &Finite(v))
|
||||
}
|
||||
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, S::Error> {
|
||||
self.0.serialize_seq(len).map(Wrap)
|
||||
}
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, S::Error> {
|
||||
self.0.serialize_tuple(len).map(Wrap)
|
||||
}
|
||||
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeTupleStruct, S::Error> {
|
||||
self.0.serialize_tuple_struct(name, len).map(Wrap)
|
||||
}
|
||||
fn serialize_tuple_variant(self, name: &'static str, i: u32, variant: &'static str, len: usize) -> Result<Self::SerializeTupleVariant, S::Error> {
|
||||
self.0.serialize_tuple_variant(name, i, variant, len).map(Wrap)
|
||||
}
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, S::Error> {
|
||||
self.0.serialize_map(len).map(Wrap)
|
||||
}
|
||||
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct, S::Error> {
|
||||
self.0.serialize_struct(name, len).map(Wrap)
|
||||
}
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
i: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStructVariant, S::Error> {
|
||||
self.0.serialize_struct_variant(name, i, variant, len).map(Wrap)
|
||||
}
|
||||
fn is_human_readable(&self) -> bool {
|
||||
self.0.is_human_readable()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! compound {
|
||||
($tr:ident, $m:ident) => {
|
||||
impl<S: ser::$tr> ser::$tr for Wrap<S> {
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn $m<T: ?Sized + Serialize>(&mut self, v: &T) -> Result<(), S::Error> {
|
||||
self.0.$m(&Finite(v))
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
compound!(SerializeSeq, serialize_element);
|
||||
compound!(SerializeTuple, serialize_element);
|
||||
compound!(SerializeTupleStruct, serialize_field);
|
||||
compound!(SerializeTupleVariant, serialize_field);
|
||||
|
||||
impl<S: ser::SerializeMap> ser::SerializeMap for Wrap<S> {
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn serialize_key<T: ?Sized + Serialize>(&mut self, k: &T) -> Result<(), S::Error> {
|
||||
self.0.serialize_key(&Finite(k))
|
||||
}
|
||||
fn serialize_value<T: ?Sized + Serialize>(&mut self, v: &T) -> Result<(), S::Error> {
|
||||
self.0.serialize_value(&Finite(v))
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! fields {
|
||||
($tr:ident) => {
|
||||
impl<S: ser::$tr> ser::$tr for Wrap<S> {
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn serialize_field<T: ?Sized + Serialize>(&mut self, key: &'static str, v: &T) -> Result<(), S::Error> {
|
||||
self.0.serialize_field(key, &Finite(v))
|
||||
}
|
||||
fn skip_field(&mut self, key: &'static str) -> Result<(), S::Error> {
|
||||
self.0.skip_field(key)
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
fields!(SerializeStruct);
|
||||
fields!(SerializeStructVariant);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn non_finite_floats_are_written_as_zero_and_nothing_else_changes() {
|
||||
#[derive(serde::Serialize)]
|
||||
struct T {
|
||||
a: f64,
|
||||
b: Option<f64>,
|
||||
c: Vec<(f32, f64)>,
|
||||
d: Option<String>,
|
||||
e: std::collections::BTreeMap<String, f64>,
|
||||
}
|
||||
let t = T { a: f64::NAN, b: Some(f64::INFINITY), c: vec![(f32::NEG_INFINITY, 1.5)], d: None, e: [("k".to_string(), f64::NAN)].into() };
|
||||
let s = serde_json::to_string(&Finite(&t)).unwrap_or_default();
|
||||
assert_eq!(s, r#"{"a":0.0,"b":0.0,"c":[[0.0,1.5]],"d":null,"e":{"k":0.0}}"#);
|
||||
}
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
//! Byte-for-byte checks on the `.deckcraft` package. The files in `tests/golden` were written by
|
||||
//! the format code as it was before the package layer moved to `craft-deckcraft-format`
|
||||
//! (storytold/craft-libs). A save of the same deck must give the same bytes, and a load must give
|
||||
//! the same presentation and the same damage report.
|
||||
//! `DECKCRAFT_BLESS_GOLDEN=1` rewrites them. Only do that for a deliberate format change.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
use deckcraft_model::chart::{Chart, ChartPart, ChartType};
|
||||
use deckcraft_model::text::TextBody;
|
||||
use deckcraft_model::{MediaId, MediaItem, Presentation, Shape, ShapeId, ShapeKind, Xfrm, defaults};
|
||||
|
||||
use super::{load_checked, save_as};
|
||||
|
||||
/// A fixed generator, so the golden bytes don't change with the app's version.
|
||||
const GENERATOR: &str = "DeckCraft golden";
|
||||
|
||||
fn golden(name: &str) -> PathBuf {
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("golden").join(name)
|
||||
}
|
||||
|
||||
fn media(id: u32, name: &str, content_type: &str, data: &[u8], link: Option<&str>) -> MediaItem {
|
||||
MediaItem { id: MediaId(id), name: name.into(), content_type: content_type.into(), data: Arc::new(data.to_vec()), link: link.map(Into::into) }
|
||||
}
|
||||
|
||||
/// A deck that reaches every kind of package member: stored and deflated media, linked media
|
||||
/// without bytes (not written), stored and deflated chart parts, and a non-finite number.
|
||||
fn deck() -> Presentation {
|
||||
let mut p = defaults::new_presentation(None);
|
||||
let slide = Arc::make_mut(&mut p.slides[0]);
|
||||
if let Some(title) = slide.shapes.first_mut() {
|
||||
title.text = Some(TextBody::from_text("Golden «deck» ✓"));
|
||||
}
|
||||
let mut chart = Chart::sample(ChartType::Bar);
|
||||
for (i, (content_type, bytes)) in
|
||||
[("application/zip", vec![80, 75, 3, 4, 1, 2, 3]), ("application/vnd.ms-office.chartstyle+xml", b"<cs:chartStyle id=\"201\"/>".repeat(8))]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
chart.kept.parts.push(ChartPart { name: format!("ppt/charts/part{i}"), content_type: content_type.into(), bytes, native_entry: None });
|
||||
}
|
||||
let chart_id = ShapeId(p.alloc_id().unwrap_or(9000));
|
||||
let odd_id = ShapeId(p.alloc_id().unwrap_or(9001));
|
||||
let slide = Arc::make_mut(&mut p.slides[0]);
|
||||
slide.shapes.push(Shape {
|
||||
id: chart_id,
|
||||
xfrm: Some(Xfrm::new(10.0, 20.0, 300.0, 200.0)),
|
||||
kind: ShapeKind::Chart(Box::new(chart)),
|
||||
..Default::default()
|
||||
});
|
||||
// Written as 0 by the NaN-safe writer, then clamped on load.
|
||||
slide.shapes.push(Shape { id: odd_id, xfrm: Some(Xfrm::new(f64::NAN, 5.0, f64::INFINITY, 7.5)), ..Default::default() });
|
||||
p.media = vec![
|
||||
media(1, "image1.png", "image/png", &[0x89, b'P', b'N', b'G', 13, 10, 26, 10, 0, 0, 0, 0], None),
|
||||
media(
|
||||
2,
|
||||
"drawing.svg",
|
||||
"image/svg+xml",
|
||||
&b"<svg xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"4\" height=\"4\"/></svg>".repeat(4),
|
||||
None,
|
||||
),
|
||||
media(3, "clip.mp4", "video/mp4", &[0, 0, 0, 24, b'f', b't', b'y', b'p'], Some("https://example.invalid/clip.mp4")),
|
||||
media(4, "linked.png", "image/png", &[], Some("file:///nowhere/linked.png")),
|
||||
];
|
||||
p
|
||||
}
|
||||
|
||||
/// The loaded deck and its damage report, as JSON. Media bytes are not in the model's JSON, so
|
||||
/// each item's bytes follow it in hex. `with_model: false` leaves the presentation out.
|
||||
fn loaded_json(bytes: &[u8], with_model: bool) -> String {
|
||||
let (p, damaged) = load_checked(bytes).unwrap_or_else(|e| panic!("load: {e}"));
|
||||
let hex = |b: &[u8]| b.iter().map(|x| format!("{x:02x}")).collect::<String>();
|
||||
let media: Vec<(u32, String)> = p.media.iter().map(|m| (m.id.0, hex(&m.data))).collect();
|
||||
let mut value = serde_json::json!({ "damaged": damaged, "media": media });
|
||||
if with_model {
|
||||
value["presentation"] = serde_json::to_value(&p).unwrap_or_default();
|
||||
}
|
||||
serde_json::to_string(&value).unwrap_or_default() + "\n"
|
||||
}
|
||||
|
||||
fn check(name: &str, actual: &[u8]) {
|
||||
let path = golden(name);
|
||||
if std::env::var_os("DECKCRAFT_BLESS_GOLDEN").is_some() {
|
||||
std::fs::write(&path, actual).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
|
||||
return;
|
||||
}
|
||||
let expected = std::fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
|
||||
assert!(expected == actual, "{name} changed ({} bytes, golden has {})", actual.len(), expected.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_save_writes_the_golden_package_bytes() {
|
||||
let bytes = save_as(&deck(), GENERATOR).unwrap_or_else(|e| panic!("save: {e}"));
|
||||
check("deck.deckcraft", &bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_load_of_the_golden_package_gives_the_golden_presentation() {
|
||||
let bytes = std::fs::read(golden("deck.deckcraft")).unwrap_or_else(|e| panic!("{e}"));
|
||||
check("deck.loaded.json", loaded_json(&bytes, true).as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_load_of_a_damaged_package_gives_the_golden_report() {
|
||||
// The PNG member and the second chart part removed: both are reported, nothing is refused.
|
||||
let bytes = std::fs::read(golden("deck.deckcraft")).unwrap_or_else(|e| panic!("{e}"));
|
||||
let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes.as_slice())).unwrap_or_else(|e| panic!("{e}"));
|
||||
let mut out = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
for i in 0..z.len() {
|
||||
let Ok(f) = z.by_index_raw(i) else { continue };
|
||||
if f.name() != "media/1" && f.name() != "chart-parts/1" {
|
||||
out.raw_copy_file(f).unwrap_or_else(|e| panic!("{e}"));
|
||||
}
|
||||
}
|
||||
let damaged = out.finish().unwrap_or_else(|e| panic!("{e}")).into_inner();
|
||||
check("damaged.loaded.json", loaded_json(&damaged, false).as_bytes());
|
||||
}
|
||||
+232
-35
@@ -5,63 +5,158 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
mod chart_parts;
|
||||
mod finite;
|
||||
pub mod rtf;
|
||||
|
||||
#[cfg(test)]
|
||||
mod chart_parts_tests;
|
||||
#[cfg(test)]
|
||||
mod golden_tests;
|
||||
|
||||
use std::io::{Cursor, Read, Write};
|
||||
use std::sync::Arc;
|
||||
|
||||
use deckcraft_model::text::{Paragraph, TextBody};
|
||||
use deckcraft_model::{LayoutType, Presentation, ShapeKind, defaults};
|
||||
|
||||
// The package itself (identity, manifest, member names, bounded reads) is shared.
|
||||
pub use craft_deckcraft_format::{Budget, EXTENSION, FormatError, LEGACY_MIMETYPE, MIMETYPE, Result, VERSION, sniff};
|
||||
use craft_deckcraft_format::{Member, Package, media_entry};
|
||||
pub const MIMETYPE: &str = "application/vnd.storyteller.deckcraft+zip";
|
||||
/// Files written before the SlideCraft → DeckCraft rename (`.slidecraft`) are still read.
|
||||
pub const LEGACY_MIMETYPE: &str = concat!("application/vnd.storyteller.", "slide", "craft+zip");
|
||||
pub const EXTENSION: &str = "deckcraft";
|
||||
/// Format version written into the manifest.
|
||||
pub const VERSION: u32 = 1;
|
||||
/// Largest entry we will inflate (guards against zip bombs).
|
||||
const MAX_ENTRY: u64 = 2 << 30;
|
||||
/// All embedded media and chart sidecars together, inflated (#393).
|
||||
const MAX_TOTAL_MEDIA: u64 = 4 << 30;
|
||||
/// Media items and chart sidecar parts one file may list.
|
||||
const MAX_MEDIA_ITEMS: usize = 100_000;
|
||||
/// Largest `presentation.json` we will inflate. The manifest holds no media bytes, so it is far
|
||||
/// smaller than a media entry may be; inflating and parsing one is what dominates memory.
|
||||
const MAX_MANIFEST: u64 = 256 << 20;
|
||||
|
||||
/// What one load may inflate.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Budget {
|
||||
total: u64,
|
||||
items: usize,
|
||||
manifest: u64,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl Budget {
|
||||
/// A media budget with the default manifest limit.
|
||||
fn media(total: u64, items: usize) -> Self {
|
||||
Budget { total, items, manifest: MAX_MANIFEST }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum FormatError {
|
||||
#[error("not a DeckCraft file: {0}")]
|
||||
NotOurs(String),
|
||||
#[error("damaged file: {0}")]
|
||||
Damaged(String),
|
||||
#[error("write failed: {0}")]
|
||||
Write(String),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, FormatError>;
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
struct Manifest {
|
||||
format: String,
|
||||
version: u32,
|
||||
generator: String,
|
||||
presentation: Presentation,
|
||||
}
|
||||
|
||||
/// Serialize a presentation.
|
||||
pub fn save(p: &Presentation) -> Result<Vec<u8>> {
|
||||
save_as(p, &format!("DeckCraft {}", env!("CARGO_PKG_VERSION")))
|
||||
let mut buf = Cursor::new(Vec::new());
|
||||
{
|
||||
let mut z = zip::ZipWriter::new(&mut buf);
|
||||
let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
|
||||
let deflate = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
|
||||
let w = |e: zip::result::ZipError| FormatError::Write(e.to_string());
|
||||
z.start_file("mimetype", stored).map_err(w)?;
|
||||
z.write_all(MIMETYPE.as_bytes()).map_err(|e| FormatError::Write(e.to_string()))?;
|
||||
let mut presentation = p.clone();
|
||||
let parts = chart_parts::separate(&mut presentation)?;
|
||||
let m =
|
||||
Manifest { format: "deckcraft".into(), version: VERSION, generator: format!("DeckCraft {}", env!("CARGO_PKG_VERSION")), presentation };
|
||||
let json = serde_json::to_vec(&finite::Finite(&m)).map_err(|e| FormatError::Write(e.to_string()))?;
|
||||
z.start_file("presentation.json", deflate).map_err(w)?;
|
||||
z.write_all(&json).map_err(|e| FormatError::Write(e.to_string()))?;
|
||||
for item in &p.media {
|
||||
if item.link.is_some() && item.data.is_empty() {
|
||||
continue;
|
||||
}
|
||||
// Already-compressed media is stored, not deflated again.
|
||||
let opts = if is_compressed(&item.content_type) { stored } else { deflate };
|
||||
z.start_file(format!("media/{}", item.id.0), opts).map_err(w)?;
|
||||
z.write_all(&item.data).map_err(|e| FormatError::Write(e.to_string()))?;
|
||||
}
|
||||
for (name, content_type, bytes) in parts {
|
||||
let opts = if is_compressed(&content_type) { stored } else { deflate };
|
||||
z.start_file(name, opts).map_err(w)?;
|
||||
z.write_all(&bytes).map_err(|e| FormatError::Write(e.to_string()))?;
|
||||
}
|
||||
z.finish().map_err(w)?;
|
||||
}
|
||||
Ok(buf.into_inner())
|
||||
}
|
||||
|
||||
/// [`save`] naming `generator` in the manifest.
|
||||
fn save_as(p: &Presentation, generator: &str) -> Result<Vec<u8>> {
|
||||
let mut presentation = p.clone();
|
||||
let parts = chart_parts::separate(&mut presentation)?;
|
||||
// Linked media saved without its bytes has no member.
|
||||
let media: Vec<(String, &deckcraft_model::MediaItem)> =
|
||||
p.media.iter().filter(|item| !(item.link.is_some() && item.data.is_empty())).map(|item| (media_entry(item.id.0), item)).collect();
|
||||
let members: Vec<Member<'_>> = media
|
||||
.iter()
|
||||
.map(|(name, item)| Member { name, content_type: &item.content_type, bytes: &item.data })
|
||||
.chain(parts.iter().map(|(name, content_type, bytes)| Member { name, content_type, bytes }))
|
||||
.collect();
|
||||
craft_deckcraft_format::write(&presentation, generator, &members)
|
||||
fn is_compressed(ct: &str) -> bool {
|
||||
ct.starts_with("image/jpeg")
|
||||
|| ct.starts_with("image/png")
|
||||
|| ct.starts_with("video/")
|
||||
|| ct.starts_with("audio/mpeg")
|
||||
|| ct.starts_with("audio/mp4")
|
||||
|| ct.contains("zip")
|
||||
}
|
||||
|
||||
/// Does this look like a `.deckcraft` file?
|
||||
pub fn sniff(bytes: &[u8]) -> bool {
|
||||
bytes.starts_with(b"PK") && [MIMETYPE, LEGACY_MIMETYPE].iter().any(|m| bytes.windows(m.len()).take(200).any(|w| w == m.as_bytes()))
|
||||
}
|
||||
|
||||
/// A member of a package a test built or rewrote with `zip`, within `max` bytes.
|
||||
#[cfg(test)]
|
||||
fn read_entry_max(z: &mut zip::ZipArchive<std::io::Cursor<&[u8]>>, name: &str, max: u64) -> Result<Vec<u8>> {
|
||||
use std::io::Read;
|
||||
fn read_entry(z: &mut zip::ZipArchive<Cursor<&[u8]>>, name: &str) -> Result<Vec<u8>> {
|
||||
read_entry_max(z, name, MAX_ENTRY)
|
||||
}
|
||||
|
||||
/// Read one member, refusing it before inflating if it declares more than `max` bytes, and
|
||||
/// stopping at `max` if it inflates further than it declared.
|
||||
fn read_entry_max(z: &mut zip::ZipArchive<Cursor<&[u8]>>, name: &str, max: u64) -> Result<Vec<u8>> {
|
||||
read_entry_counted(z, name, max, &mut 0)
|
||||
}
|
||||
|
||||
/// [`read_entry_max`] within `left` (and [`MAX_ENTRY`]), charging `left` for every byte inflated,
|
||||
/// including those inflated before a decode or CRC error, so a member that fails late still
|
||||
/// spends the shared budget it used.
|
||||
fn read_entry_charged(z: &mut zip::ZipArchive<Cursor<&[u8]>>, name: &str, left: &mut u64) -> Result<Vec<u8>> {
|
||||
let mut spent = 0;
|
||||
let out = read_entry_counted(z, name, (*left).min(MAX_ENTRY), &mut spent);
|
||||
*left = left.saturating_sub(spent);
|
||||
out
|
||||
}
|
||||
|
||||
/// [`read_entry_max`], adding the bytes inflated to `spent` whether or not the read succeeds.
|
||||
fn read_entry_counted(z: &mut zip::ZipArchive<Cursor<&[u8]>>, name: &str, max: u64, spent: &mut u64) -> Result<Vec<u8>> {
|
||||
let f = z.by_name(name).map_err(|e| FormatError::Damaged(format!("{name}: {e}")))?;
|
||||
if f.size() > max {
|
||||
return Err(FormatError::Damaged(format!("{name} is too large")));
|
||||
}
|
||||
let mut out = vec![];
|
||||
f.take(max.saturating_add(1)).read_to_end(&mut out).map_err(|e| FormatError::Damaged(format!("{name}: {e}")))?;
|
||||
let mut out = Vec::with_capacity(f.size().min(64 << 20) as usize);
|
||||
let mut f = f.take(max.saturating_add(1));
|
||||
let read = f.read_to_end(&mut out);
|
||||
*spent = spent.saturating_add(out.len() as u64);
|
||||
read.map_err(|e| FormatError::Damaged(format!("{name}: {e}")))?;
|
||||
if out.len() as u64 > max {
|
||||
return Err(FormatError::Damaged(format!("{name} is too large")));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn read_entry(z: &mut zip::ZipArchive<std::io::Cursor<&[u8]>>, name: &str) -> Result<Vec<u8>> {
|
||||
read_entry_max(z, name, craft_deckcraft_format::MAX_ENTRY)
|
||||
}
|
||||
|
||||
/// Read a presentation. Missing media/chart parts are left empty; use [`load_checked`] to
|
||||
/// learn which. A damaged chart member refuses the file before further chart inflation.
|
||||
pub fn load(bytes: &[u8]) -> Result<Presentation> {
|
||||
@@ -72,11 +167,19 @@ pub fn load(bytes: &[u8]) -> Result<Presentation> {
|
||||
/// A chart member that is present but fails decoding, CRC or its entry limit refuses the file.
|
||||
/// Linked media saved without its bytes isn't damage.
|
||||
pub fn load_checked(bytes: &[u8]) -> Result<(Presentation, Vec<String>)> {
|
||||
load_within(bytes, Budget::default())
|
||||
load_within(bytes, Budget { total: MAX_TOTAL_MEDIA, items: MAX_MEDIA_ITEMS, manifest: MAX_MANIFEST })
|
||||
}
|
||||
|
||||
fn load_within(bytes: &[u8], budget: Budget) -> Result<(Presentation, Vec<String>)> {
|
||||
let (m, mut z) = Package::open_manifest::<Presentation>(bytes, budget.manifest)?;
|
||||
let mut z = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| FormatError::NotOurs(e.to_string()))?;
|
||||
if z.index_for_name("presentation.json").is_none() {
|
||||
return Err(FormatError::NotOurs("no presentation.json".into()));
|
||||
}
|
||||
let json = read_entry_max(&mut z, "presentation.json", budget.manifest)?;
|
||||
let m = parse_manifest(&json)?;
|
||||
if m.format != "deckcraft" {
|
||||
return Err(FormatError::NotOurs(m.format));
|
||||
}
|
||||
let mut p = m.presentation;
|
||||
let mut damaged = vec![];
|
||||
let items = p.media.len();
|
||||
@@ -89,13 +192,13 @@ fn load_within(bytes: &[u8], budget: Budget) -> Result<(Presentation, Vec<String
|
||||
p.media.retain(|m| seen.insert(m.id));
|
||||
let mut left = budget.total;
|
||||
for item in &mut p.media {
|
||||
let name = media_entry(item.id.0);
|
||||
let linked_only = item.link.is_some() && !z.contains(&name);
|
||||
if z.declared_size(&name).is_some_and(|size| size > left) {
|
||||
let name = format!("media/{}", item.id.0);
|
||||
let linked_only = item.link.is_some() && z.index_for_name(&name).is_none();
|
||||
if z.by_name(&name).is_ok_and(|f| f.size() > left) {
|
||||
return Err(FormatError::Damaged("embedded media is larger than DeckCraft can open".into()));
|
||||
}
|
||||
// A failed read is charged for what it inflated, so later members can't reuse that budget.
|
||||
match z.read_charged(&name, &mut left) {
|
||||
match read_entry_charged(&mut z, &name, &mut left) {
|
||||
Ok(data) => item.data = Arc::new(data),
|
||||
Err(_) if linked_only => {}
|
||||
Err(e) => damaged.push(e.to_string()),
|
||||
@@ -243,6 +346,100 @@ fn add_outline_slides(p: &mut Presentation, slides: OutlineSlides) -> usize {
|
||||
made
|
||||
}
|
||||
|
||||
/// Most `null`s standing in for numbers that [`parse_manifest`] patches in one file, and the
|
||||
/// candidates it tries for each.
|
||||
const MAX_NULL_FIXES: usize = 256;
|
||||
const MAX_TRIES_PER_FIX: usize = 32;
|
||||
/// Bytes [`parse_manifest`] may re-parse in all while patching, so a hostile file full of `null`s
|
||||
/// can't keep it busy: past this the file is reported damaged.
|
||||
const MAX_REPARSE_BYTES: usize = 256 * 1024 * 1024;
|
||||
|
||||
/// Where a parse failed, comparable: later is progress.
|
||||
fn failure(src: &[u8]) -> std::result::Result<Manifest, (String, (usize, usize))> {
|
||||
serde_json::from_slice::<Manifest>(src).map_err(|e| (e.to_string(), (e.line(), e.column())))
|
||||
}
|
||||
|
||||
/// Parse `presentation.json`. serde_json writes a NaN or infinite number as `null`, which then
|
||||
/// fails to read back as a number ("invalid type: null, expected f64"), so DeckCraft could save
|
||||
/// a file it couldn't open. Each such `null` is read as 0 instead ([`repair`] then brings the
|
||||
/// value into range); anything else that doesn't parse is damage.
|
||||
///
|
||||
/// serde reports the failing `null` exactly, except inside tagged enums (buffered), where it
|
||||
/// reports the end of the enclosing object: there the `null`s before that point are tried
|
||||
/// nearest first, and a patch is kept only if the parse then gets further. (Files saved since
|
||||
/// [`finite::Finite`] never contain such `null`s; this reads older ones.)
|
||||
fn parse_manifest(json: &[u8]) -> Result<Manifest> {
|
||||
// Parse the inflated text as it is: only a file that fails on a `null` number is copied for
|
||||
// patching (the copy is bounded by `MAX_MANIFEST`).
|
||||
let mut last = match failure(json) {
|
||||
Ok(m) => return Ok(m),
|
||||
Err(f) => f,
|
||||
};
|
||||
if !is_null_number(&last.0) {
|
||||
return Err(FormatError::Damaged(last.0));
|
||||
}
|
||||
let mut src = json.to_vec();
|
||||
let mut budget = MAX_REPARSE_BYTES;
|
||||
'fixes: for _ in 0..MAX_NULL_FIXES {
|
||||
let (msg, pos) = &last;
|
||||
if !is_null_number(msg) {
|
||||
break;
|
||||
}
|
||||
let end = offset(&src, pos.0, pos.1).unwrap_or(src.len());
|
||||
let mut progressed = false;
|
||||
let candidates: Vec<usize> = value_nulls_before(&src, end + 4).take(MAX_TRIES_PER_FIX).collect();
|
||||
for at in candidates {
|
||||
let (Some(head), Some(tail)) = (src.get(..at), src.get(at.saturating_add(4)..)) else { continue };
|
||||
let Some(left) = budget.checked_sub(src.len()) else { break 'fixes };
|
||||
budget = left;
|
||||
let mut next = Vec::with_capacity(src.len());
|
||||
next.extend_from_slice(head);
|
||||
next.push(b'0');
|
||||
next.extend_from_slice(tail);
|
||||
match failure(&next) {
|
||||
Ok(m) => return Ok(m),
|
||||
// The patch shortened the text by 3 bytes before the error: compare positions
|
||||
// allowing for that.
|
||||
Err(f) if f.1 > (pos.0, pos.1.saturating_sub(3)) => {
|
||||
src = next;
|
||||
last = f;
|
||||
progressed = true;
|
||||
break;
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
if !progressed {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(FormatError::Damaged(last.0))
|
||||
}
|
||||
|
||||
/// Is this parse error a `null` where a number belongs (a NaN or infinity serde_json wrote)?
|
||||
fn is_null_number(msg: &str) -> bool {
|
||||
msg.starts_with("invalid type: null, expected f") || msg.starts_with("invalid type: null, expected a number")
|
||||
}
|
||||
|
||||
/// Byte offset of a 1-based `line`:`column`.
|
||||
fn offset(src: &[u8], line: usize, column: usize) -> Option<usize> {
|
||||
let mut start = 0;
|
||||
for _ in 1..line {
|
||||
start += src.get(start..)?.iter().position(|b| *b == b'\n')? + 1;
|
||||
}
|
||||
Some((start + column).min(src.len()))
|
||||
}
|
||||
|
||||
/// Starts of `null` tokens in value position (after `:`, `[` or `,`) before `end`, nearest first.
|
||||
fn value_nulls_before(src: &[u8], end: usize) -> impl Iterator<Item = usize> + '_ {
|
||||
let end = end.min(src.len());
|
||||
(0..end.saturating_sub(3)).rev().filter(move |&i| {
|
||||
src.get(i..i + 4) == Some(b"null")
|
||||
&& src[..i].iter().rev().find(|b| !b.is_ascii_whitespace()).is_some_and(|b| matches!(b, b':' | b'[' | b','))
|
||||
&& src.get(i + 4).is_none_or(|b| !b.is_ascii_alphanumeric())
|
||||
})
|
||||
}
|
||||
|
||||
/// Largest coordinate or size kept, in points (about 350 m; PowerPoint's own limit is far smaller).
|
||||
const MAX_COORD: f64 = 1.0e6;
|
||||
|
||||
|
||||
+29
-102
@@ -10,24 +10,24 @@ use deckcraft_model::text::{Paragraph, RunKind};
|
||||
|
||||
/// RTF-escape `s` (non-ASCII as `\uN?`, UTF-16, signed as RTF wants).
|
||||
fn escape(out: &mut String, s: &str) {
|
||||
// Keep outline text literal: the shared writer also supports page/column breaks and
|
||||
// shorthand controls that this app's deliberately small outline reader does not consume.
|
||||
// Delegate ordinary spans, including UTF-16 escaping, without allocating a second string.
|
||||
let mut start = 0;
|
||||
for (i, c) in s.char_indices() {
|
||||
let replacement = match c {
|
||||
'\u{000C}' | '\u{000E}' => "",
|
||||
'\u{00A0}' => "\\u160?",
|
||||
'\u{00AD}' => "\\u173?",
|
||||
'\u{2011}' => "\\u8209?",
|
||||
'\u{2028}' => "\\u8232?",
|
||||
_ => continue,
|
||||
};
|
||||
craft_rtf::push_esc(out, &s[start..i]);
|
||||
out.push_str(replacement);
|
||||
start = i + c.len_utf8();
|
||||
for c in s.chars() {
|
||||
match c {
|
||||
'\\' | '{' | '}' => {
|
||||
out.push('\\');
|
||||
out.push(c);
|
||||
}
|
||||
'\t' => out.push_str("\\tab "),
|
||||
'\u{b}' | '\n' => out.push_str("\\line "),
|
||||
c if c.is_ascii() && !c.is_ascii_control() => out.push(c),
|
||||
c if c.is_ascii() => {}
|
||||
c => {
|
||||
let mut buf = [0u16; 2];
|
||||
for u in c.encode_utf16(&mut buf) {
|
||||
out.push_str(&format!("\\u{}?", *u as i16));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
craft_rtf::push_esc(out, &s[start..]);
|
||||
}
|
||||
|
||||
fn runs(out: &mut String, p: &Paragraph) {
|
||||
@@ -276,9 +276,6 @@ impl Outline<'_> {
|
||||
while k > 0 && self.i < self.b.len() {
|
||||
if self.b[self.i] == b'\\' && self.b.get(self.i + 1) == Some(&b'\'') {
|
||||
self.i = advance(self.rtf, self.i, 4);
|
||||
} else if self.b[self.i] == b'\\' && self.b.get(self.i + 1).is_some_and(|b| !b.is_ascii_alphabetic()) {
|
||||
// An escaped control symbol counts as one Unicode fallback character.
|
||||
self.i = advance(self.rtf, self.i + 1, 1);
|
||||
} else if matches!(self.b[self.i], b'{' | b'}' | b'\\') {
|
||||
break;
|
||||
} else {
|
||||
@@ -297,7 +294,7 @@ impl Outline<'_> {
|
||||
&& let Some(h) = hex
|
||||
&& h != 0
|
||||
{
|
||||
self.line.push(craft_rtf::codepage::cp1252(h));
|
||||
self.line.push(cp1252(h));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -384,75 +381,22 @@ fn finish_surrogates(s: String) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
fn cp1252(b: u8) -> char {
|
||||
const HIGH: [char; 32] = [
|
||||
'€', '\u{81}', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\u{8d}', 'Ž', '\u{8f}', '\u{90}', '‘', '’', '“', '”', '•', '–', '—',
|
||||
'˜', '™', 'š', '›', 'œ', '\u{9d}', 'ž', 'Ÿ',
|
||||
];
|
||||
match b {
|
||||
0x80..=0x9f => HIGH[(b - 0x80) as usize],
|
||||
_ => b as char,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(clippy::unwrap_used)]
|
||||
use super::*;
|
||||
|
||||
// Frozen pre-cutover implementations are test oracles, not production parsers.
|
||||
fn legacy_escape(out: &mut String, s: &str) {
|
||||
for c in s.chars() {
|
||||
match c {
|
||||
'\\' | '{' | '}' => {
|
||||
out.push('\\');
|
||||
out.push(c);
|
||||
}
|
||||
'\t' => out.push_str("\\tab "),
|
||||
'\u{b}' | '\n' => out.push_str("\\line "),
|
||||
c if c.is_ascii() && !c.is_ascii_control() => out.push(c),
|
||||
c if c.is_ascii() => {}
|
||||
c => {
|
||||
let mut buf = [0u16; 2];
|
||||
for u in c.encode_utf16(&mut buf) {
|
||||
out.push_str(&format!("\\u{}?", *u as i16));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn legacy_cp1252(b: u8) -> char {
|
||||
const HIGH: [char; 32] = [
|
||||
'€', '\u{81}', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\u{8d}', 'Ž', '\u{8f}', '\u{90}', '‘', '’', '“', '”', '•', '–',
|
||||
'—', '˜', '™', 'š', '›', 'œ', '\u{9d}', 'ž', 'Ÿ',
|
||||
];
|
||||
match b {
|
||||
0x80..=0x9f => HIGH[(b - 0x80) as usize],
|
||||
_ => b as char,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_escape_preserves_every_unicode_scalar() {
|
||||
for value in 0..=0x10ffff {
|
||||
let Some(c) = char::from_u32(value) else { continue };
|
||||
let mut buf = [0; 4];
|
||||
let text = c.encode_utf8(&mut buf);
|
||||
let mut expected = String::new();
|
||||
let mut actual = String::new();
|
||||
legacy_escape(&mut expected, text);
|
||||
escape(&mut actual, text);
|
||||
assert_eq!(actual, expected, "U+{value:04X}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_escape_preserves_mixed_spans_and_append() {
|
||||
let text = "a\t中\u{a0}b\u{ad}c\u{2011}d\u{2028}e\u{c}f\u{e}g\n😀{}\\";
|
||||
let mut expected = String::from("prefix:");
|
||||
let mut actual = expected.clone();
|
||||
legacy_escape(&mut expected, text);
|
||||
escape(&mut actual, text);
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_cp1252_preserves_all_bytes() {
|
||||
for b in 0..=u8::MAX {
|
||||
assert_eq!(craft_rtf::codepage::cp1252(b), legacy_cp1252(b));
|
||||
}
|
||||
}
|
||||
|
||||
/// A sign in a `\'hh` escape is not a hex digit and must be ignored.
|
||||
#[test]
|
||||
fn hex_escape_rejects_sign() {
|
||||
@@ -478,23 +422,6 @@ mod tests {
|
||||
assert_eq!(rtf_to_outline(r"{\rtf1 x\u-70000?y\par}"), "x\u{ee90}y\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unicode_fallback_control_symbols_are_skipped() {
|
||||
for (rtf, expected) in [
|
||||
(r"{\rtf1\uc1 \u92\\X\par}", "\\X\n"),
|
||||
(r"{\rtf1\uc1 \u123\{X\par}", "{X\n"),
|
||||
(r"{\rtf1\uc1 \u125\}X\par}", "}X\n"),
|
||||
(r"{\rtf1\uc2 \u233\{\}X\par}", "éX\n"),
|
||||
(r"{\rtf1\uc0 \u92\\X\par}", "\\\\X\n"),
|
||||
(r"{\rtf1 \\ \{ \}X\par}", "\\ { }X\n"),
|
||||
(r"{\rtf1\uc1 \u233{X}\par}", "éX\n"),
|
||||
(r"{\rtf1\uc1 \u233\éX\par}", "éX\n"),
|
||||
(r"{\rtf1\uc1 \u233\", "é\n"),
|
||||
] {
|
||||
assert_eq!(rtf_to_outline(rtf), expected, "{rtf:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multibyte_control_symbol_keeps_character_and_following_text() {
|
||||
assert_eq!(rtf_to_outline("{\\rtf1 A\\éB\\par}"), "AéB\n");
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
use super::*;
|
||||
use craft_deckcraft_format::{Manifest, parse_manifest};
|
||||
use std::io::{Cursor, Read, Write};
|
||||
|
||||
#[test]
|
||||
fn chart_sidecars_are_optional_for_older_native_files() {
|
||||
@@ -366,7 +364,7 @@ fn reflection_transform_survives_two_native_saves_and_absent_fields_stay_absent(
|
||||
assert_eq!(reopened.slides[0].shapes.last().and_then(|s| s.effects.as_ref()).and_then(|e| e.reflection.as_ref()), Some(&absent));
|
||||
}
|
||||
|
||||
/// Files written before [`craft_deckcraft_format::Finite`] (plain serde_json: NaN as `null`) open too,
|
||||
/// Files written before [`finite::Finite`] (plain serde_json: NaN as `null`) open too,
|
||||
/// including a `null` inside a tagged enum, where serde reports only the enclosing object.
|
||||
#[test]
|
||||
fn older_files_with_null_numbers_open() {
|
||||
@@ -389,7 +387,7 @@ fn older_files_with_null_numbers_open() {
|
||||
let m = Manifest { format: "deckcraft".into(), version: VERSION, generator: "old".into(), presentation: p };
|
||||
let json = serde_json::to_vec(&m).unwrap();
|
||||
assert!(json.windows(4).any(|w| w == b"null"));
|
||||
let back = parse_manifest::<Presentation>(&json).unwrap_or_else(|e| panic!("case {case}: {e}"));
|
||||
let back = parse_manifest(&json).unwrap_or_else(|e| panic!("case {case}: {e}"));
|
||||
assert_eq!(back.presentation.slides[0].shapes.len(), m.presentation.slides[0].shapes.len(), "case {case}");
|
||||
}
|
||||
}
|
||||
@@ -421,14 +419,14 @@ fn a_bare_normautofit_body_round_trips() {
|
||||
#[test]
|
||||
fn other_damage_is_still_reported() {
|
||||
let ok = serde_json::to_vec(&serde_json::json!({"format": "deckcraft", "version": 1, "generator": "t", "presentation": {}})).unwrap();
|
||||
assert!(parse_manifest::<Presentation>(&ok).is_ok());
|
||||
assert!(parse_manifest(&ok).is_ok());
|
||||
let bad = br#"{"format": null, "version": 1, "generator": "t", "presentation": {}}"#;
|
||||
assert!(parse_manifest::<Presentation>(bad).is_err());
|
||||
assert!(parse_manifest(bad).is_err());
|
||||
let truncated = &ok[..ok.len() - 3];
|
||||
assert!(parse_manifest::<Presentation>(truncated).is_err());
|
||||
assert!(parse_manifest(truncated).is_err());
|
||||
// A null number on a later line (pretty-printed by another tool) is found too.
|
||||
let pretty = b"{\n \"format\": \"deckcraft\",\n \"version\": 1,\n \"generator\": \"t\",\n \"presentation\": {\"slides\": [{\"shapes\": [{\"xfrm\": {\"x\": null, \"y\": 1, \"w\": 2, \"h\": 3}}]}]}\n}";
|
||||
let m = parse_manifest::<Presentation>(pretty).unwrap_or_else(|e| panic!("{e}"));
|
||||
let m = parse_manifest(pretty).unwrap_or_else(|e| panic!("{e}"));
|
||||
assert_eq!(m.presentation.slides[0].shapes[0].xfrm.unwrap().x, 0.0);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
# Golden files are compared byte for byte: no line-ending conversion on checkout.
|
||||
* -text
|
||||
@@ -1 +0,0 @@
|
||||
{"damaged":["damaged file: media/1: specified file not found in archive","chart-parts/1: missing chart part"],"media":[[1,""],[2,"3c73766720786d6c6e733d22687474703a2f2f7777772e77332e6f72672f323030302f737667223e3c726563742077696474683d223422206865696768743d2234222f3e3c2f7376673e3c73766720786d6c6e733d22687474703a2f2f7777772e77332e6f72672f323030302f737667223e3c726563742077696474683d223422206865696768743d2234222f3e3c2f7376673e3c73766720786d6c6e733d22687474703a2f2f7777772e77332e6f72672f323030302f737667223e3c726563742077696474683d223422206865696768743d2234222f3e3c2f7376673e3c73766720786d6c6e733d22687474703a2f2f7777772e77332e6f72672f323030302f737667223e3c726563742077696474683d223422206865696768743d2234222f3e3c2f7376673e"],[3,"0000001866747970"],[4,""]]}
|
||||
Binary file not shown.
File diff suppressed because one or more lines are too long
@@ -9,6 +9,5 @@ description = "DeckCraft geometry: units (EMU/pt), transforms, preset shape gene
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-drawingml = { workspace = true }
|
||||
kurbo = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
+95
-3
@@ -7,14 +7,39 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
pub mod morph;
|
||||
pub mod preset;
|
||||
pub mod squircle;
|
||||
pub mod warp;
|
||||
|
||||
pub use kurbo::{Affine, BezPath, PathEl, Point, Rect, Shape as KurboShape, Size, Vec2};
|
||||
// Placement, units and preset and warp geometry are DrawingML's, shared in craft-libs
|
||||
// (`craft_drawingml`, craft-libs#778 and #792).
|
||||
pub use craft_drawingml::{EMU_PER_CM, EMU_PER_IN, EMU_PER_PT, Xfrm, emu_to_pt, group_child_affine, preset, pt_to_emu, warp};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// EMU per point.
|
||||
pub const EMU_PER_PT: f64 = 12_700.0;
|
||||
/// EMU per inch.
|
||||
pub const EMU_PER_IN: f64 = 914_400.0;
|
||||
/// EMU per centimetre.
|
||||
pub const EMU_PER_CM: f64 = 360_000.0;
|
||||
|
||||
pub fn emu_to_pt(emu: i64) -> f64 {
|
||||
emu as f64 / EMU_PER_PT
|
||||
}
|
||||
|
||||
/// Points to EMU, rounded. Non-finite input becomes 0; huge values saturate.
|
||||
pub fn pt_to_emu(pt: f64) -> i64 {
|
||||
if !pt.is_finite() {
|
||||
return 0;
|
||||
}
|
||||
let v = (pt * EMU_PER_PT).round();
|
||||
if v >= i64::MAX as f64 {
|
||||
i64::MAX
|
||||
} else if v <= i64::MIN as f64 {
|
||||
i64::MIN
|
||||
} else {
|
||||
v as i64
|
||||
}
|
||||
}
|
||||
|
||||
/// Display units for measurements (inches on US English systems, centimetres elsewhere).
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -76,6 +101,73 @@ fn split_number(t: &str) -> Option<(f64, &str)> {
|
||||
Some((n, t.get(end..).unwrap_or("")))
|
||||
}
|
||||
|
||||
/// A shape's placement on its slide (or in its group's child space): offset, extent, rotation
|
||||
/// (degrees clockwise) and flips. Rotation and flips are about the centre.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct Xfrm {
|
||||
pub x: f64,
|
||||
pub y: f64,
|
||||
pub w: f64,
|
||||
pub h: f64,
|
||||
#[serde(skip_serializing_if = "is_zero")]
|
||||
pub rot: f64,
|
||||
#[serde(skip_serializing_if = "std::ops::Not::not")]
|
||||
pub flip_h: bool,
|
||||
#[serde(skip_serializing_if = "std::ops::Not::not")]
|
||||
pub flip_v: bool,
|
||||
}
|
||||
|
||||
fn is_zero(v: &f64) -> bool {
|
||||
*v == 0.0
|
||||
}
|
||||
|
||||
impl Xfrm {
|
||||
pub fn new(x: f64, y: f64, w: f64, h: f64) -> Self {
|
||||
Xfrm { x, y, w, h, ..Default::default() }
|
||||
}
|
||||
pub fn rect(&self) -> Rect {
|
||||
Rect::new(self.x, self.y, self.x + self.w, self.y + self.h)
|
||||
}
|
||||
pub fn center(&self) -> Point {
|
||||
Point::new(self.x + self.w / 2.0, self.y + self.h / 2.0)
|
||||
}
|
||||
/// Local shape space (0..w, 0..h) → parent space, applying flips and rotation about the centre.
|
||||
pub fn affine(&self) -> Affine {
|
||||
let c = Vec2::new(self.w / 2.0, self.h / 2.0);
|
||||
let sx = if self.flip_h { -1.0 } else { 1.0 };
|
||||
let sy = if self.flip_v { -1.0 } else { 1.0 };
|
||||
Affine::translate(Vec2::new(self.x, self.y) + c)
|
||||
* Affine::rotate(self.rot.to_radians())
|
||||
* Affine::scale_non_uniform(sx, sy)
|
||||
* Affine::translate(-c)
|
||||
}
|
||||
/// Axis-aligned bounds of the rotated box in parent space.
|
||||
pub fn bounds(&self) -> Rect {
|
||||
let a = self.affine();
|
||||
let pts = [Point::new(0.0, 0.0), Point::new(self.w, 0.0), Point::new(self.w, self.h), Point::new(0.0, self.h)].map(|p| a * p);
|
||||
let mut r = Rect::from_points(pts[0], pts[1]);
|
||||
r = r.union_pt(pts[2]).union_pt(pts[3]);
|
||||
r
|
||||
}
|
||||
/// True when no dimension is NaN/inf.
|
||||
pub fn is_finite(&self) -> bool {
|
||||
[self.x, self.y, self.w, self.h, self.rot].iter().all(|v| v.is_finite())
|
||||
}
|
||||
/// Rotation normalised to 0..360.
|
||||
pub fn rot_norm(&self) -> f64 {
|
||||
let r = self.rot.rem_euclid(360.0);
|
||||
if r >= 360.0 { 0.0 } else { r }
|
||||
}
|
||||
}
|
||||
|
||||
/// Maps a group's child coordinate space (`ch_off`, `ch_ext`) onto the group's own box.
|
||||
pub fn group_child_affine(group: &Xfrm, ch: Rect) -> Affine {
|
||||
let sx = if ch.width().abs() > 1e-9 { group.w / ch.width() } else { 1.0 };
|
||||
let sy = if ch.height().abs() > 1e-9 { group.h / ch.height() } else { 1.0 };
|
||||
group.affine() * Affine::scale_non_uniform(sx, sy) * Affine::translate(-ch.origin().to_vec2())
|
||||
}
|
||||
|
||||
/// Distance from `p` to the segment `a`–`b`.
|
||||
pub fn dist_to_segment(p: Point, a: Point, b: Point) -> f64 {
|
||||
let ab = b - a;
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
//! Preset shape geometry.
|
||||
//!
|
||||
//! Each preset is identified by its Office Open XML name (`rect`, `roundRect`, `rightArrow`…) so
|
||||
//! files round-trip, but every outline here is our own construction in Rust: we re-derive the shapes
|
||||
//! from what they look like and from the meaning of their adjust values, not from any reference
|
||||
//! geometry table. Adjust values use the file convention: ratios in 1/100 000 of the shorter side (or
|
||||
//! of the width/height), angles in 1/60 000 degree.
|
||||
|
||||
use kurbo::{Arc, BezPath, Point, Rect, Vec2};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
mod build;
|
||||
mod builder;
|
||||
mod catalog;
|
||||
mod shapes;
|
||||
|
||||
pub use build::build;
|
||||
use builder::{Corner, Dir, Pb, corner_rect, fill, fill_eo, fill_only, mid_sites, orient, shade, simple, stroke_only, vis};
|
||||
pub use catalog::{CATALOG, Category, FillMode, Geometry, Handle, PresetInfo, SubPath, info, is_line_like, mitred_by_default};
|
||||
|
||||
mod extra;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,344 @@
|
||||
use super::catalog::{FillMode, Geometry, Handle, SubPath};
|
||||
use super::{Arc, BezPath, Point, Rect, Vec2};
|
||||
|
||||
// ---------- path builder ----------
|
||||
|
||||
pub(super) struct Pb {
|
||||
pub(super) p: BezPath,
|
||||
pub(super) cur: Point,
|
||||
}
|
||||
|
||||
impl Pb {
|
||||
pub(super) fn new() -> Self {
|
||||
Pb { p: BezPath::new(), cur: Point::ZERO }
|
||||
}
|
||||
pub(super) fn m(&mut self, x: f64, y: f64) -> &mut Self {
|
||||
self.cur = Point::new(x, y);
|
||||
self.p.move_to(self.cur);
|
||||
self
|
||||
}
|
||||
pub(super) fn l(&mut self, x: f64, y: f64) -> &mut Self {
|
||||
self.cur = Point::new(x, y);
|
||||
self.p.line_to(self.cur);
|
||||
self
|
||||
}
|
||||
pub(super) fn c(&mut self, x1: f64, y1: f64, x2: f64, y2: f64, x: f64, y: f64) -> &mut Self {
|
||||
self.cur = Point::new(x, y);
|
||||
self.p.curve_to(Point::new(x1, y1), Point::new(x2, y2), self.cur);
|
||||
self
|
||||
}
|
||||
pub(super) fn q(&mut self, x1: f64, y1: f64, x: f64, y: f64) -> &mut Self {
|
||||
self.cur = Point::new(x, y);
|
||||
self.p.quad_to(Point::new(x1, y1), self.cur);
|
||||
self
|
||||
}
|
||||
/// Elliptic arc continuing from the current point, which lies on the ellipse at parametric
|
||||
/// angle `st` (degrees); sweeps `sw` degrees (positive = clockwise on screen).
|
||||
pub(super) fn arc(&mut self, wr: f64, hr: f64, st: f64, sw: f64) -> &mut Self {
|
||||
let (s, e) = (st.to_radians(), (st + sw).to_radians());
|
||||
let c = Point::new(self.cur.x - wr * s.cos(), self.cur.y - hr * s.sin());
|
||||
let a = Arc::new(c, Vec2::new(wr.abs(), hr.abs()), s, e - s, 0.0);
|
||||
a.to_cubic_beziers(0.05, |p1, p2, p| {
|
||||
self.p.curve_to(p1, p2, p);
|
||||
});
|
||||
self.cur = Point::new(c.x + wr * e.cos(), c.y + hr * e.sin());
|
||||
self
|
||||
}
|
||||
/// A DrawingML `arcTo`: angles are where the ray from the ellipse's centre at that angle
|
||||
/// meets it (not the ellipse's parameter), so elliptical arcs start and end where the
|
||||
/// standard's shapes expect.
|
||||
pub(super) fn arc_spec(&mut self, wr: f64, hr: f64, st: f64, sw: f64) -> &mut Self {
|
||||
let par = |a: f64| {
|
||||
let a = a.to_radians();
|
||||
(wr * a.sin()).atan2(hr * a.cos())
|
||||
};
|
||||
let t0 = par(st);
|
||||
let mut d = par(st + sw) - t0;
|
||||
let tau = std::f64::consts::TAU;
|
||||
if sw.abs() >= 360.0 {
|
||||
d = tau.copysign(sw);
|
||||
} else {
|
||||
while sw > 0.0 && d < 0.0 {
|
||||
d += tau;
|
||||
}
|
||||
while sw < 0.0 && d > 0.0 {
|
||||
d -= tau;
|
||||
}
|
||||
}
|
||||
self.arc(wr, hr, t0.to_degrees(), d.to_degrees())
|
||||
}
|
||||
pub(super) fn z(&mut self) -> &mut Self {
|
||||
self.p.close_path();
|
||||
self
|
||||
}
|
||||
pub(super) fn poly(&mut self, pts: &[(f64, f64)]) -> &mut Self {
|
||||
for (i, (x, y)) in pts.iter().enumerate() {
|
||||
if i == 0 {
|
||||
self.m(*x, *y);
|
||||
} else {
|
||||
self.l(*x, *y);
|
||||
}
|
||||
}
|
||||
self.z()
|
||||
}
|
||||
pub(super) fn ellipse(&mut self, cx: f64, cy: f64, rx: f64, ry: f64) -> &mut Self {
|
||||
self.m(cx + rx, cy);
|
||||
self.arc(rx, ry, 0.0, 360.0);
|
||||
self.z()
|
||||
}
|
||||
pub(super) fn take(&mut self) -> BezPath {
|
||||
std::mem::take(&mut self.p)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn fill(p: BezPath) -> SubPath {
|
||||
SubPath { path: p, fill: FillMode::Norm, stroke: true, even_odd: false }
|
||||
}
|
||||
|
||||
pub(super) fn fill_eo(p: BezPath) -> SubPath {
|
||||
SubPath { path: p, fill: FillMode::Norm, stroke: true, even_odd: true }
|
||||
}
|
||||
|
||||
pub(super) fn shade(p: BezPath, m: FillMode) -> SubPath {
|
||||
SubPath { path: p, fill: m, stroke: true, even_odd: false }
|
||||
}
|
||||
|
||||
pub(super) fn stroke_only(p: BezPath) -> SubPath {
|
||||
SubPath { path: p, fill: FillMode::None, stroke: true, even_odd: false }
|
||||
}
|
||||
|
||||
pub(super) fn fill_only(p: BezPath) -> SubPath {
|
||||
SubPath { path: p, fill: FillMode::Norm, stroke: false, even_odd: false }
|
||||
}
|
||||
|
||||
/// Parametric angle on an rx×ry ellipse for a visual angle `deg` (file angles are visual).
|
||||
pub(super) fn vis(deg: f64, rx: f64, ry: f64) -> f64 {
|
||||
let t = deg.to_radians();
|
||||
(rx * t.sin()).atan2(ry * t.cos()).to_degrees()
|
||||
}
|
||||
|
||||
pub(super) fn polygon_regular(n: usize, w: f64, h: f64, phase: f64) -> Vec<(f64, f64)> {
|
||||
let pts: Vec<(f64, f64)> = (0..n)
|
||||
.map(|i| {
|
||||
let a = (phase + 360.0 * i as f64 / n as f64).to_radians();
|
||||
(a.cos(), a.sin())
|
||||
})
|
||||
.collect();
|
||||
normalize(&pts, w, h)
|
||||
}
|
||||
|
||||
/// Scale `pts` so their bounding box fills 0..w × 0..h.
|
||||
fn normalize(pts: &[(f64, f64)], w: f64, h: f64) -> Vec<(f64, f64)> {
|
||||
let (mut x0, mut y0, mut x1, mut y1) = (f64::MAX, f64::MAX, f64::MIN, f64::MIN);
|
||||
for (x, y) in pts {
|
||||
x0 = x0.min(*x);
|
||||
y0 = y0.min(*y);
|
||||
x1 = x1.max(*x);
|
||||
y1 = y1.max(*y);
|
||||
}
|
||||
let sx = if x1 - x0 > 1e-12 { w / (x1 - x0) } else { 0.0 };
|
||||
let sy = if y1 - y0 > 1e-12 { h / (y1 - y0) } else { 0.0 };
|
||||
pts.iter().map(|(x, y)| ((x - x0) * sx, (y - y0) * sy)).collect()
|
||||
}
|
||||
|
||||
/// An n-pointed star as ECMA-376 draws it: points on an ellipse around (hc, vc · vf) with radii
|
||||
/// half the box stretched by the preset's `hf` / `vf` (so a five-pointed star fills its box and
|
||||
/// looks regular in a square), the inner corners `inner` of the way out.
|
||||
pub(super) fn star(n: usize, w: f64, h: f64, inner: f64) -> Vec<(f64, f64)> {
|
||||
let (hf, vf) = match n {
|
||||
5 => (1.05146, 1.10557),
|
||||
6 => (1.15470, 1.0),
|
||||
7 => (1.02572, 1.05210),
|
||||
10 => (1.05146, 1.0),
|
||||
_ => (1.0, 1.0),
|
||||
};
|
||||
let (rx, ry, cy) = (w / 2.0 * hf, h / 2.0 * vf, h / 2.0 * vf);
|
||||
let mut pts = Vec::with_capacity(n * 2);
|
||||
for i in 0..n * 2 {
|
||||
let a = (-90.0 + 180.0 * i as f64 / n as f64).to_radians();
|
||||
let r = if i % 2 == 0 { 1.0 } else { inner };
|
||||
pts.push((w / 2.0 + r * rx * a.cos(), cy + r * ry * a.sin()));
|
||||
}
|
||||
pts
|
||||
}
|
||||
|
||||
/// The text rectangle ECMA-376's presetShapeDefinitions give a star (`inner` is the adjust value
|
||||
/// over 50000), where known: star4's is the square inside its inner points, star5's spans the
|
||||
/// inner pentagon from its upper side points to its bottom inner point.
|
||||
pub(super) fn star_text_rect(n: usize, w: f64, h: f64, inner: f64) -> Option<Rect> {
|
||||
let (hc, vc) = (w / 2.0, h / 2.0);
|
||||
match n {
|
||||
4 => {
|
||||
let (dx, dy) = (hc * inner * 45f64.to_radians().cos(), vc * inner * 45f64.to_radians().sin());
|
||||
Some(Rect::new(hc - dx, vc - dy, hc + dx, vc + dy))
|
||||
}
|
||||
5 => {
|
||||
let (swd2, shd2, svc) = (hc * 1.05146, vc * 1.10557, vc * 1.10557);
|
||||
let (iwd2, ihd2) = (swd2 * inner, shd2 * inner);
|
||||
let sdx1 = iwd2 * 18f64.to_radians().cos();
|
||||
let sdy1 = ihd2 * 54f64.to_radians().sin();
|
||||
Some(Rect::new(hc - sdx1, svc - sdy1, hc + sdx1, svc + ihd2))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) enum Corner {
|
||||
Square,
|
||||
Round(f64),
|
||||
Snip(f64),
|
||||
}
|
||||
|
||||
/// A rectangle with each corner (TL, TR, BR, BL) square, rounded or snipped.
|
||||
pub(super) fn corner_rect(w: f64, h: f64, c: [Corner; 4]) -> BezPath {
|
||||
let mut b = Pb::new();
|
||||
let size = |k: Corner| match k {
|
||||
Corner::Square => 0.0,
|
||||
Corner::Round(r) | Corner::Snip(r) => r.max(0.0),
|
||||
};
|
||||
let s0 = size(c[0]);
|
||||
b.m(s0, 0.0);
|
||||
// top edge → TR
|
||||
let s1 = size(c[1]);
|
||||
b.l(w - s1, 0.0);
|
||||
match c[1] {
|
||||
Corner::Round(r) if r > 0.0 => {
|
||||
b.arc(r, r, 270.0, 90.0);
|
||||
}
|
||||
Corner::Snip(_) => {
|
||||
b.l(w, s1);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let s2 = size(c[2]);
|
||||
b.l(w, h - s2);
|
||||
match c[2] {
|
||||
Corner::Round(r) if r > 0.0 => {
|
||||
b.arc(r, r, 0.0, 90.0);
|
||||
}
|
||||
Corner::Snip(_) => {
|
||||
b.l(w - s2, h);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let s3 = size(c[3]);
|
||||
b.l(s3, h);
|
||||
match c[3] {
|
||||
Corner::Round(r) if r > 0.0 => {
|
||||
b.arc(r, r, 90.0, 90.0);
|
||||
}
|
||||
Corner::Snip(_) => {
|
||||
b.l(0.0, h - s3);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
b.l(0.0, s0);
|
||||
match c[0] {
|
||||
Corner::Round(r) if r > 0.0 => {
|
||||
b.arc(r, r, 180.0, 90.0);
|
||||
}
|
||||
Corner::Snip(_) => {
|
||||
b.l(s0, 0.0);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
b.z();
|
||||
b.take()
|
||||
}
|
||||
|
||||
pub(super) fn mid_sites(w: f64, h: f64) -> Vec<Point> {
|
||||
vec![Point::new(w / 2.0, 0.0), Point::new(0.0, h / 2.0), Point::new(w / 2.0, h), Point::new(w, h / 2.0)]
|
||||
}
|
||||
|
||||
/// Orientation for single-axis arrows built along +x.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) enum Dir {
|
||||
Right,
|
||||
Left,
|
||||
Up,
|
||||
Down,
|
||||
}
|
||||
|
||||
/// Map canonical (u along the arrow's length L, v across thickness T) to shape space.
|
||||
pub(super) fn orient(d: Dir, w: f64, h: f64, pts: &[(f64, f64)]) -> Vec<(f64, f64)> {
|
||||
pts.iter()
|
||||
.map(|&(u, v)| match d {
|
||||
Dir::Right => (u, v),
|
||||
Dir::Left => (w - u, v),
|
||||
Dir::Up => (v, h - u),
|
||||
Dir::Down => (v, u),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(super) fn len_thick(d: Dir, w: f64, h: f64) -> (f64, f64) {
|
||||
match d {
|
||||
Dir::Right | Dir::Left => (w, h),
|
||||
Dir::Up | Dir::Down => (h, w),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn arrow(d: Dir, w: f64, h: f64, a1: f64, a2: f64) -> Geometry {
|
||||
let (l, t) = len_thick(d, w, h);
|
||||
let ss = l.min(t);
|
||||
let a1 = a1.clamp(0.0, 100000.0) / 100000.0;
|
||||
let dx = (ss * a2.clamp(0.0, 100000.0 * l / ss.max(1e-9)) / 100000.0).min(l);
|
||||
let y1 = t / 2.0 - t * a1 / 2.0;
|
||||
let y2 = t / 2.0 + t * a1 / 2.0;
|
||||
let pts = [(0.0, y1), (l - dx, y1), (l - dx, 0.0), (l, t / 2.0), (l - dx, t), (l - dx, y2), (0.0, y2)];
|
||||
let pts = orient(d, w, h, &pts);
|
||||
let tr = orient(d, w, h, &[(0.0, y1), (l - dx + dx * (y1 / (t / 2.0).max(1e-9)), y2)]);
|
||||
let mut text = Rect::from_points(tr[0], tr[1]);
|
||||
text = text.abs();
|
||||
let mut g = Geometry { paths: vec![fill(Pb::new().poly(&pts).take())], text_rect: text, handles: vec![], sites: mid_sites(w, h) };
|
||||
// handle 0: shaft thickness on the head base; handle 1: head length.
|
||||
let o = orient(d, w, h, &[(l - dx, t / 2.0), (l, t / 2.0)]);
|
||||
let (o0, o1) = (Point::new(o[0].0, o[0].1), Point::new(o[1].0, o[1].1));
|
||||
let across = match d {
|
||||
Dir::Right | Dir::Left => Vec2::new(0.0, -t / 2.0 / 100000.0),
|
||||
Dir::Up | Dir::Down => Vec2::new(-t / 2.0 / 100000.0, 0.0),
|
||||
};
|
||||
let along = (o0 - o1) * (ss / 100000.0 / dx.max(1e-9));
|
||||
g.handles.push(Handle::new(0, o0, across, a1 * 100000.0, 0.0, 100000.0));
|
||||
if dx > 1e-9 {
|
||||
g.handles.push(Handle::new(1, o1, along, a2, 0.0, 100000.0 * l / ss.max(1e-9)));
|
||||
}
|
||||
g
|
||||
}
|
||||
|
||||
pub(super) fn double_arrow(vertical: bool, w: f64, h: f64, a1: f64, a2: f64) -> Geometry {
|
||||
let (l, t) = if vertical { (h, w) } else { (w, h) };
|
||||
let ss = l.min(t);
|
||||
let a1 = a1.clamp(0.0, 100000.0) / 100000.0;
|
||||
let dx = (ss * a2.max(0.0) / 100000.0).min(l / 2.0);
|
||||
let y1 = t / 2.0 - t * a1 / 2.0;
|
||||
let y2 = t / 2.0 + t * a1 / 2.0;
|
||||
let pts = [(0.0, t / 2.0), (dx, 0.0), (dx, y1), (l - dx, y1), (l - dx, 0.0), (l, t / 2.0), (l - dx, t), (l - dx, y2), (dx, y2), (dx, t)];
|
||||
let pts = orient(if vertical { Dir::Down } else { Dir::Right }, w, h, &pts);
|
||||
let r = if vertical { Rect::new(y1, dx, y2, h - dx) } else { Rect::new(dx, y1, w - dx, y2) };
|
||||
Geometry { paths: vec![fill(Pb::new().poly(&pts).take())], text_rect: r, handles: vec![], sites: mid_sites(w, h) }
|
||||
}
|
||||
|
||||
/// Points given in 21600ths of the box (as ECMA-376 defines many fixed shapes), scaled to it.
|
||||
pub(super) fn spec_pts(pts: &[(i32, i32)], w: f64, h: f64) -> Vec<(f64, f64)> {
|
||||
pts.iter().map(|(x, y)| (w * *x as f64 / 21600.0, h * *y as f64 / 21600.0)).collect()
|
||||
}
|
||||
|
||||
pub(super) fn simple(path: BezPath, text: Rect, w: f64, h: f64) -> Geometry {
|
||||
Geometry { paths: vec![fill(path)], text_rect: text, handles: vec![], sites: mid_sites(w, h) }
|
||||
}
|
||||
|
||||
pub(super) fn ellipse_sites(w: f64, h: f64) -> Vec<Point> {
|
||||
(0..8)
|
||||
.map(|i| {
|
||||
let a = (-90.0 - 45.0 * i as f64).to_radians();
|
||||
Point::new(w / 2.0 + w / 2.0 * a.cos(), h / 2.0 + h / 2.0 * a.sin())
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(super) fn tip_handle(w: f64, h: f64, tip: Point) -> Handle {
|
||||
// A free 2-D handle is represented as index 0 with zero direction; callers move both values.
|
||||
Handle { index: 0, pos: tip, origin: Point::new(w / 2.0, h / 2.0), dir: Vec2::new(w / 1e5, h / 1e5), min: -1e6, max: 1e6, angle: false }
|
||||
}
|
||||
@@ -0,0 +1,391 @@
|
||||
use super::builder::vis;
|
||||
use super::{BezPath, Deserialize, Point, Rect, Serialize, Vec2};
|
||||
|
||||
/// How a sub-path is filled. `Lighten`/`Darken` variants shade the shape's fill (3-D looking faces).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum FillMode {
|
||||
Norm,
|
||||
None,
|
||||
Lighten,
|
||||
LightenLess,
|
||||
Darken,
|
||||
DarkenLess,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SubPath {
|
||||
pub path: BezPath,
|
||||
pub fill: FillMode,
|
||||
pub stroke: bool,
|
||||
/// Even-odd fill (holes in donuts and frames).
|
||||
pub even_odd: bool,
|
||||
}
|
||||
|
||||
/// A draggable adjust handle (the yellow dot). `pos = origin + dir * value`; dragging projects
|
||||
/// the pointer onto `dir`. An `angle` handle instead measures the pointer's angle about `origin`
|
||||
/// (1/60 000 degree, y down, 0° at +x).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Handle {
|
||||
pub index: usize,
|
||||
pub pos: Point,
|
||||
pub origin: Point,
|
||||
pub dir: Vec2,
|
||||
pub min: f64,
|
||||
pub max: f64,
|
||||
pub angle: bool,
|
||||
}
|
||||
|
||||
impl Handle {
|
||||
pub(super) fn new(index: usize, origin: Point, dir: Vec2, value: f64, min: f64, max: f64) -> Self {
|
||||
Handle { index, pos: origin + dir * value, origin, dir, min, max, angle: false }
|
||||
}
|
||||
/// An angle handle on the `rx`×`ry` ellipse around `center` at visual angle `value`.
|
||||
pub(super) fn angle(index: usize, center: Point, rx: f64, ry: f64, value: f64) -> Self {
|
||||
let t = vis(value / 60000.0, rx, ry).to_radians();
|
||||
let pos = Point::new(center.x + rx * t.cos(), center.y + ry * t.sin());
|
||||
Handle { index, pos, origin: center, dir: Vec2::ZERO, min: 0.0, max: 21600000.0, angle: true }
|
||||
}
|
||||
/// The adjust value for a pointer at `p` (shape-local space).
|
||||
pub fn value_at(&self, p: Point) -> f64 {
|
||||
if self.angle {
|
||||
let d = p - self.origin;
|
||||
if !(d.x.is_finite() && d.y.is_finite()) || d.hypot() < 1e-9 {
|
||||
return self.min;
|
||||
}
|
||||
let v = (d.y.atan2(d.x).to_degrees().rem_euclid(360.0) * 60000.0).round();
|
||||
return if v >= self.max { self.min } else { v };
|
||||
}
|
||||
let l2 = self.dir.hypot2();
|
||||
if l2 < 1e-18 {
|
||||
return self.min;
|
||||
}
|
||||
let v = (p - self.origin).dot(self.dir) / l2;
|
||||
if !v.is_finite() {
|
||||
return self.min;
|
||||
}
|
||||
v.clamp(self.min.min(self.max), self.max.max(self.min)).round()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Geometry {
|
||||
pub paths: Vec<SubPath>,
|
||||
/// Where text goes, in shape-local space.
|
||||
pub text_rect: Rect,
|
||||
pub handles: Vec<Handle>,
|
||||
/// Connector glue points, shape-local.
|
||||
pub sites: Vec<Point>,
|
||||
}
|
||||
|
||||
impl Geometry {
|
||||
/// The union outline used for hit testing and selection (all filled or stroked sub-paths).
|
||||
pub fn outline(&self) -> BezPath {
|
||||
let mut p = BezPath::new();
|
||||
for s in &self.paths {
|
||||
p.extend(s.path.iter());
|
||||
}
|
||||
p
|
||||
}
|
||||
/// Whether nothing is filled (lines, connectors, arcs, brackets).
|
||||
pub fn is_open(&self) -> bool {
|
||||
self.paths.iter().all(|s| s.fill == FillMode::None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Gallery category, as in the Shapes menu.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum Category {
|
||||
Lines,
|
||||
Rectangles,
|
||||
BasicShapes,
|
||||
BlockArrows,
|
||||
EquationShapes,
|
||||
Flowchart,
|
||||
StarsAndBanners,
|
||||
Callouts,
|
||||
ActionButtons,
|
||||
}
|
||||
|
||||
impl Category {
|
||||
pub const ALL: [Category; 9] = [
|
||||
Category::Lines,
|
||||
Category::Rectangles,
|
||||
Category::BasicShapes,
|
||||
Category::BlockArrows,
|
||||
Category::EquationShapes,
|
||||
Category::Flowchart,
|
||||
Category::StarsAndBanners,
|
||||
Category::Callouts,
|
||||
Category::ActionButtons,
|
||||
];
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
Category::Lines => "Lines",
|
||||
Category::Rectangles => "Rectangles",
|
||||
Category::BasicShapes => "Basic Shapes",
|
||||
Category::BlockArrows => "Block Arrows",
|
||||
Category::EquationShapes => "Equation Shapes",
|
||||
Category::Flowchart => "Flowchart",
|
||||
Category::StarsAndBanners => "Stars and Banners",
|
||||
Category::Callouts => "Callouts",
|
||||
Category::ActionButtons => "Action Buttons",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PresetInfo {
|
||||
pub name: &'static str,
|
||||
pub label: &'static str,
|
||||
pub category: Category,
|
||||
/// Default adjust values.
|
||||
pub defaults: &'static [f64],
|
||||
}
|
||||
|
||||
macro_rules! p {
|
||||
($n:literal, $l:literal, $c:ident, [$($d:expr),*]) => {
|
||||
PresetInfo { name: $n, label: $l, category: Category::$c, defaults: &[$($d as f64),*] }
|
||||
};
|
||||
}
|
||||
|
||||
/// Every preset we generate, in gallery order.
|
||||
pub static CATALOG: &[PresetInfo] = &[
|
||||
p!("line", "Line", Lines, []),
|
||||
p!("straightConnector1", "Arrow", Lines, []),
|
||||
p!("bentConnector3", "Connector: Elbow", Lines, [50000]),
|
||||
p!("curvedConnector3", "Connector: Curved", Lines, [50000]),
|
||||
p!("bentConnector2", "Connector: Elbow (one bend)", Lines, []),
|
||||
p!("lineInv", "Line: Rising", Lines, []),
|
||||
p!("rect", "Rectangle", Rectangles, []),
|
||||
p!("roundRect", "Rectangle: Rounded Corners", Rectangles, [16667]),
|
||||
p!("snip1Rect", "Rectangle: Single Corner Snipped", Rectangles, [16667]),
|
||||
p!("snip2SameRect", "Rectangle: Top Corners Snipped", Rectangles, [16667, 0]),
|
||||
p!("snip2DiagRect", "Rectangle: Diagonal Corners Snipped", Rectangles, [0, 16667]),
|
||||
p!("snipRoundRect", "Rectangle: Top Corners One Rounded and One Snipped", Rectangles, [16667, 16667]),
|
||||
p!("round1Rect", "Rectangle: Single Corner Rounded", Rectangles, [16667]),
|
||||
p!("round2SameRect", "Rectangle: Top Corners Rounded", Rectangles, [16667, 0]),
|
||||
p!("round2DiagRect", "Rectangle: Diagonal Corners Rounded", Rectangles, [16667, 0]),
|
||||
p!("textBox", "Text Box", BasicShapes, []),
|
||||
p!("ellipse", "Oval", BasicShapes, []),
|
||||
p!("triangle", "Isosceles Triangle", BasicShapes, [50000]),
|
||||
p!("rtTriangle", "Right Triangle", BasicShapes, []),
|
||||
p!("parallelogram", "Parallelogram", BasicShapes, [25000]),
|
||||
p!("trapezoid", "Trapezoid", BasicShapes, [25000]),
|
||||
p!("nonIsoscelesTrapezoid", "Trapezoid: Uneven Sides", BasicShapes, [25000, 25000]),
|
||||
p!("diamond", "Diamond", BasicShapes, []),
|
||||
p!("pentagon", "Regular Pentagon", BasicShapes, []),
|
||||
p!("hexagon", "Hexagon", BasicShapes, [25000]),
|
||||
p!("heptagon", "Heptagon", BasicShapes, []),
|
||||
p!("octagon", "Octagon", BasicShapes, [29289]),
|
||||
p!("decagon", "Decagon", BasicShapes, []),
|
||||
p!("dodecagon", "Dodecagon", BasicShapes, []),
|
||||
p!("pie", "Partial Circle", BasicShapes, [0, 16200000]),
|
||||
p!("pieWedge", "Quarter Wedge", BasicShapes, []),
|
||||
p!("chord", "Chord", BasicShapes, [2700000, 16200000]),
|
||||
p!("teardrop", "Teardrop", BasicShapes, [100000]),
|
||||
p!("frame", "Frame", BasicShapes, [12500]),
|
||||
p!("cornerTabs", "Corner Tabs: Triangles", BasicShapes, []),
|
||||
p!("squareTabs", "Corner Tabs: Squares", BasicShapes, []),
|
||||
p!("plaqueTabs", "Corner Tabs: Quarter Circles", BasicShapes, []),
|
||||
p!("halfFrame", "Half Frame", BasicShapes, [33333, 33333]),
|
||||
p!("corner", "L-Shape", BasicShapes, [50000, 50000]),
|
||||
p!("diagStripe", "Diagonal Stripe", BasicShapes, [50000]),
|
||||
p!("plus", "Cross", BasicShapes, [25000]),
|
||||
p!("plaque", "Plaque", BasicShapes, [16667]),
|
||||
p!("can", "Cylinder", BasicShapes, [25000]),
|
||||
p!("cube", "Cube", BasicShapes, [25000]),
|
||||
p!("bevel", "Bevel", BasicShapes, [12500]),
|
||||
p!("donut", "Donut", BasicShapes, [25000]),
|
||||
p!("noSmoking", "\"Not Allowed\" Symbol", BasicShapes, [18750]),
|
||||
p!("blockArc", "Block Arc", BasicShapes, [10800000, 0, 25000]),
|
||||
p!("foldedCorner", "Folded Corner", BasicShapes, [16667]),
|
||||
p!("smileyFace", "Smiley Face", BasicShapes, [4653]),
|
||||
p!("heart", "Heart", BasicShapes, []),
|
||||
p!("lightningBolt", "Lightning Bolt", BasicShapes, []),
|
||||
p!("sun", "Sun", BasicShapes, [25000]),
|
||||
p!("moon", "Moon", BasicShapes, [50000]),
|
||||
p!("cloud", "Cloud", BasicShapes, []),
|
||||
p!("gear6", "Gear: 6 Teeth", BasicShapes, [15000, 3526]),
|
||||
p!("gear9", "Gear: 9 Teeth", BasicShapes, [10000, 1763]),
|
||||
p!("funnel", "Funnel", BasicShapes, []),
|
||||
p!("arc", "Arc", BasicShapes, [16200000, 0]),
|
||||
p!("bracketPair", "Double Bracket", BasicShapes, [16667]),
|
||||
p!("bracePair", "Double Brace", BasicShapes, [8333]),
|
||||
p!("leftBracket", "Left Bracket", BasicShapes, [8333]),
|
||||
p!("rightBracket", "Right Bracket", BasicShapes, [8333]),
|
||||
p!("leftBrace", "Left Brace", BasicShapes, [8333, 50000]),
|
||||
p!("rightBrace", "Right Brace", BasicShapes, [8333, 50000]),
|
||||
p!("chartX", "Chart Marker: Cross Lines", BasicShapes, []),
|
||||
p!("chartStar", "Chart Marker: Star Lines", BasicShapes, []),
|
||||
p!("chartPlus", "Chart Marker: Plus Lines", BasicShapes, []),
|
||||
p!("rightArrow", "Arrow: Right", BlockArrows, [50000, 50000]),
|
||||
p!("leftArrow", "Arrow: Left", BlockArrows, [50000, 50000]),
|
||||
p!("upArrow", "Arrow: Up", BlockArrows, [50000, 50000]),
|
||||
p!("downArrow", "Arrow: Down", BlockArrows, [50000, 50000]),
|
||||
p!("leftRightArrow", "Arrow: Left-Right", BlockArrows, [50000, 50000]),
|
||||
p!("upDownArrow", "Arrow: Up-Down", BlockArrows, [50000, 50000]),
|
||||
p!("quadArrow", "Arrow: Quad", BlockArrows, [22500, 22500, 22500]),
|
||||
p!("leftRightUpArrow", "Arrow: Left-Right-Up", BlockArrows, [25000, 25000, 25000]),
|
||||
p!("bentArrow", "Arrow: Bent", BlockArrows, [25000, 25000, 25000, 43750]),
|
||||
p!("uturnArrow", "Arrow: U-Turn", BlockArrows, [25000, 25000, 25000, 43750, 75000]),
|
||||
p!("curvedRightArrow", "Arrow: Curved Right", BlockArrows, [25000, 50000, 25000]),
|
||||
p!("curvedLeftArrow", "Arrow: Curved Left", BlockArrows, [25000, 50000, 25000]),
|
||||
p!("curvedUpArrow", "Arrow: Curved Up", BlockArrows, [25000, 50000, 25000]),
|
||||
p!("curvedDownArrow", "Arrow: Curved Down", BlockArrows, [25000, 50000, 25000]),
|
||||
p!("leftUpArrow", "Arrow: Left-Up", BlockArrows, [25000, 25000, 25000]),
|
||||
p!("bentUpArrow", "Arrow: Bent-Up", BlockArrows, [25000, 25000, 25000]),
|
||||
p!("stripedRightArrow", "Arrow: Striped Right", BlockArrows, [50000, 50000]),
|
||||
p!("notchedRightArrow", "Arrow: Notched Right", BlockArrows, [50000, 50000]),
|
||||
p!("homePlate", "Arrow: Pentagon", BlockArrows, [50000]),
|
||||
p!("chevron", "Arrow: Chevron", BlockArrows, [50000]),
|
||||
p!("circularArrow", "Arrow: Circular", BlockArrows, [12500, 1142319, 20457681, 10800000, 12500]),
|
||||
p!("leftCircularArrow", "Arrow: Circular Counter-clockwise", BlockArrows, [12500, -1142319, 1142319, 10800000, 12500]),
|
||||
p!("leftRightCircularArrow", "Arrow: Circular Two-headed", BlockArrows, [12500, 1142319, 20457681, 11942319, 12500]),
|
||||
p!("swooshArrow", "Arrow: Swoosh", BlockArrows, [25000, 16667]),
|
||||
p!("rightArrowCallout", "Callout: Right Arrow", BlockArrows, [25000, 25000, 25000, 64977]),
|
||||
p!("downArrowCallout", "Callout: Down Arrow", BlockArrows, [25000, 25000, 25000, 64977]),
|
||||
p!("leftArrowCallout", "Callout: Left Arrow", BlockArrows, [25000, 25000, 25000, 64977]),
|
||||
p!("upArrowCallout", "Callout: Up Arrow", BlockArrows, [25000, 25000, 25000, 64977]),
|
||||
p!("leftRightArrowCallout", "Callout: Left-Right Arrow", BlockArrows, [25000, 25000, 25000, 48123]),
|
||||
p!("upDownArrowCallout", "Callout: Up-Down Arrow", BlockArrows, [25000, 25000, 25000, 48123]),
|
||||
p!("quadArrowCallout", "Callout: Quad Arrow", BlockArrows, [18515, 18515, 18515, 48123]),
|
||||
p!("mathPlus", "Plus Sign", EquationShapes, [23520]),
|
||||
p!("mathMinus", "Minus Sign", EquationShapes, [23520]),
|
||||
p!("mathMultiply", "Multiplication Sign", EquationShapes, [23520]),
|
||||
p!("mathDivide", "Division Sign", EquationShapes, [23520, 5880, 11760]),
|
||||
p!("mathEqual", "Equals", EquationShapes, [23520, 11760]),
|
||||
p!("mathNotEqual", "Not Equal", EquationShapes, [23520, 6600000, 11760]),
|
||||
p!("flowChartProcess", "Flowchart: Process", Flowchart, []),
|
||||
p!("flowChartAlternateProcess", "Flowchart: Alternate Process", Flowchart, []),
|
||||
p!("flowChartDecision", "Flowchart: Decision", Flowchart, []),
|
||||
p!("flowChartInputOutput", "Flowchart: Data", Flowchart, []),
|
||||
p!("flowChartPredefinedProcess", "Flowchart: Predefined Process", Flowchart, []),
|
||||
p!("flowChartInternalStorage", "Flowchart: Internal Storage", Flowchart, []),
|
||||
p!("flowChartDocument", "Flowchart: Document", Flowchart, []),
|
||||
p!("flowChartMultidocument", "Flowchart: Multidocument", Flowchart, []),
|
||||
p!("flowChartTerminator", "Flowchart: Terminator", Flowchart, []),
|
||||
p!("flowChartPreparation", "Flowchart: Preparation", Flowchart, []),
|
||||
p!("flowChartManualInput", "Flowchart: Manual Input", Flowchart, []),
|
||||
p!("flowChartManualOperation", "Flowchart: Manual Operation", Flowchart, []),
|
||||
p!("flowChartConnector", "Flowchart: Connector", Flowchart, []),
|
||||
p!("flowChartOffpageConnector", "Flowchart: Off-page Connector", Flowchart, []),
|
||||
p!("flowChartPunchedCard", "Flowchart: Card", Flowchart, []),
|
||||
p!("flowChartPunchedTape", "Flowchart: Punched Tape", Flowchart, []),
|
||||
p!("flowChartSummingJunction", "Flowchart: Summing Junction", Flowchart, []),
|
||||
p!("flowChartOr", "Flowchart: Or", Flowchart, []),
|
||||
p!("flowChartCollate", "Flowchart: Collate", Flowchart, []),
|
||||
p!("flowChartSort", "Flowchart: Sort", Flowchart, []),
|
||||
p!("flowChartExtract", "Flowchart: Extract", Flowchart, []),
|
||||
p!("flowChartMerge", "Flowchart: Merge", Flowchart, []),
|
||||
p!("flowChartOnlineStorage", "Flowchart: Stored Data", Flowchart, []),
|
||||
p!("flowChartDelay", "Flowchart: Delay", Flowchart, []),
|
||||
p!("flowChartMagneticTape", "Flowchart: Sequential Access Storage", Flowchart, []),
|
||||
p!("flowChartMagneticDisk", "Flowchart: Magnetic Disk", Flowchart, []),
|
||||
p!("flowChartMagneticDrum", "Flowchart: Direct Access Storage", Flowchart, []),
|
||||
p!("flowChartDisplay", "Flowchart: Display", Flowchart, []),
|
||||
p!("flowChartOfflineStorage", "Flowchart: Offline Storage", Flowchart, []),
|
||||
p!("irregularSeal1", "Explosion: 8 Points", StarsAndBanners, []),
|
||||
p!("irregularSeal2", "Explosion: 14 Points", StarsAndBanners, []),
|
||||
p!("star4", "Star: 4 Points", StarsAndBanners, [12500]),
|
||||
p!("star5", "Star: 5 Points", StarsAndBanners, [19098]),
|
||||
p!("star6", "Star: 6 Points", StarsAndBanners, [28868]),
|
||||
p!("star7", "Star: 7 Points", StarsAndBanners, [34601]),
|
||||
p!("star8", "Star: 8 Points", StarsAndBanners, [37500]),
|
||||
p!("star10", "Star: 10 Points", StarsAndBanners, [42533]),
|
||||
p!("star12", "Star: 12 Points", StarsAndBanners, [37500]),
|
||||
p!("star16", "Star: 16 Points", StarsAndBanners, [37500]),
|
||||
p!("star24", "Star: 24 Points", StarsAndBanners, [37500]),
|
||||
p!("star32", "Star: 32 Points", StarsAndBanners, [37500]),
|
||||
p!("ribbon2", "Ribbon: Tilted Up", StarsAndBanners, [16667, 50000]),
|
||||
p!("ribbon", "Ribbon: Tilted Down", StarsAndBanners, [16667, 50000]),
|
||||
p!("ellipseRibbon2", "Ribbon: Curved and Tilted Up", StarsAndBanners, [25000, 50000, 12500]),
|
||||
p!("ellipseRibbon", "Ribbon: Curved and Tilted Down", StarsAndBanners, [25000, 50000, 12500]),
|
||||
p!("leftRightRibbon", "Ribbon: Left-Right Fold", StarsAndBanners, [50000, 50000, 16667]),
|
||||
p!("verticalScroll", "Scroll: Vertical", StarsAndBanners, [12500]),
|
||||
p!("horizontalScroll", "Scroll: Horizontal", StarsAndBanners, [12500]),
|
||||
p!("wave", "Wave", StarsAndBanners, [12500, 0]),
|
||||
p!("doubleWave", "Double Wave", StarsAndBanners, [6250, 0]),
|
||||
p!("wedgeRectCallout", "Speech Bubble: Rectangle", Callouts, [-20833, 62500]),
|
||||
p!("wedgeRoundRectCallout", "Speech Bubble: Rectangle with Corners Rounded", Callouts, [-20833, 62500, 16667]),
|
||||
p!("wedgeEllipseCallout", "Speech Bubble: Oval", Callouts, [-20833, 62500]),
|
||||
p!("cloudCallout", "Thought Bubble: Cloud", Callouts, [-20833, 62500]),
|
||||
p!("borderCallout1", "Callout: Line", Callouts, [18750, -8333, 112500, -38333]),
|
||||
p!("borderCallout2", "Callout: Bent Line", Callouts, [18750, -8333, 18750, -16667, 112500, -46667]),
|
||||
p!("borderCallout3", "Callout: Double Bent Line", Callouts, [18750, -8333, 18750, -16667, 100000, -16667, 112963, -8333]),
|
||||
p!("accentCallout1", "Callout: Line with Accent Bar", Callouts, [18750, -8333, 112500, -38333]),
|
||||
p!("accentCallout2", "Callout: Bent Line with Accent Bar", Callouts, [18750, -8333, 18750, -16667, 112500, -46667]),
|
||||
p!("accentCallout3", "Callout: Double Bent Line with Accent Bar", Callouts, [18750, -8333, 18750, -16667, 100000, -16667, 112963, -8333]),
|
||||
p!("callout1", "Callout: Line with No Border", Callouts, [18750, -8333, 112500, -38333]),
|
||||
p!("callout2", "Callout: Bent Line with No Border", Callouts, [18750, -8333, 18750, -16667, 112500, -46667]),
|
||||
p!("callout3", "Callout: Double Bent Line with No Border", Callouts, [18750, -8333, 18750, -16667, 100000, -16667, 112963, -8333]),
|
||||
p!("accentBorderCallout1", "Callout: Line with Border and Accent Bar", Callouts, [18750, -8333, 112500, -38333]),
|
||||
p!("accentBorderCallout2", "Callout: Bent Line with Border and Accent Bar", Callouts, [18750, -8333, 18750, -16667, 112500, -46667]),
|
||||
p!(
|
||||
"accentBorderCallout3",
|
||||
"Callout: Double Bent Line with Border and Accent Bar",
|
||||
Callouts,
|
||||
[18750, -8333, 18750, -16667, 100000, -16667, 112963, -8333]
|
||||
),
|
||||
p!("actionButtonBackPrevious", "Action Button: Go Back or Previous", ActionButtons, []),
|
||||
p!("actionButtonForwardNext", "Action Button: Go Forward or Next", ActionButtons, []),
|
||||
p!("actionButtonBeginning", "Action Button: Go to Beginning", ActionButtons, []),
|
||||
p!("actionButtonEnd", "Action Button: Go to End", ActionButtons, []),
|
||||
p!("actionButtonHome", "Action Button: Go Home", ActionButtons, []),
|
||||
p!("actionButtonInformation", "Action Button: Get Information", ActionButtons, []),
|
||||
p!("actionButtonReturn", "Action Button: Go Back", ActionButtons, []),
|
||||
p!("actionButtonMovie", "Action Button: Video", ActionButtons, []),
|
||||
p!("actionButtonDocument", "Action Button: Document", ActionButtons, []),
|
||||
p!("actionButtonSound", "Action Button: Sound", ActionButtons, []),
|
||||
p!("actionButtonHelp", "Action Button: Help", ActionButtons, []),
|
||||
p!("actionButtonBlank", "Action Button: Custom", ActionButtons, []),
|
||||
];
|
||||
|
||||
pub fn info(name: &str) -> Option<&'static PresetInfo> {
|
||||
CATALOG.iter().find(|p| p.name == name)
|
||||
}
|
||||
|
||||
/// Whether PowerPoint joins this preset's outline with sharp (miter) corners when the line names
|
||||
/// no join (it rounds them otherwise). Observed per preset on its export: the plus, the right,
|
||||
/// up and down block arrows (not the left one), chevrons, stars from five points up, the
|
||||
/// equation shapes, curved ribbons and the action buttons are mitred.
|
||||
pub fn mitred_by_default(name: &str) -> bool {
|
||||
matches!(
|
||||
name,
|
||||
"plus"
|
||||
| "rightArrow"
|
||||
| "upArrow"
|
||||
| "downArrow"
|
||||
| "upDownArrow"
|
||||
| "leftUpArrow"
|
||||
| "bentUpArrow"
|
||||
| "stripedRightArrow"
|
||||
| "notchedRightArrow"
|
||||
| "chevron"
|
||||
| "leftArrowCallout"
|
||||
| "leftRightArrowCallout"
|
||||
| "upDownArrowCallout"
|
||||
| "quadArrowCallout"
|
||||
| "mathPlus"
|
||||
| "mathMinus"
|
||||
| "mathMultiply"
|
||||
| "mathEqual"
|
||||
| "mathNotEqual"
|
||||
| "star5"
|
||||
| "star6"
|
||||
| "star8"
|
||||
| "star10"
|
||||
| "star12"
|
||||
| "star16"
|
||||
| "star24"
|
||||
| "star32"
|
||||
| "ellipseRibbon"
|
||||
| "ellipseRibbon2"
|
||||
) || name.starts_with("actionButton")
|
||||
}
|
||||
|
||||
pub fn is_line_like(name: &str) -> bool {
|
||||
matches!(name, "line" | "straightConnector1" | "bentConnector2" | "bentConnector3" | "curvedConnector3" | "arc")
|
||||
|| name.starts_with("bentConnector")
|
||||
|| name.starts_with("curvedConnector")
|
||||
}
|
||||
@@ -0,0 +1,799 @@
|
||||
//! More preset outlines: line callouts, arrow callouts, curved and circular arrows, curved
|
||||
//! ribbons, gears, tabs and chart markers. Like the rest of `preset`, every outline is our own
|
||||
//! construction, fitted to how the shapes look and to what their adjust values mean.
|
||||
|
||||
use super::{
|
||||
Corner, Dir, FillMode, Geometry, Handle, Pb, Point, Rect, Vec2, corner_rect, fill, fill_eo, fill_only, mid_sites, orient, shade, simple,
|
||||
stroke_only, vis,
|
||||
};
|
||||
|
||||
/// Builds the presets this module knows; `None` for any other name.
|
||||
pub(super) fn build(name: &str, w: f64, h: f64, a: &dyn Fn(usize) -> f64) -> Option<Geometry> {
|
||||
let ss = w.min(h);
|
||||
let full = Rect::new(0.0, 0.0, w, h);
|
||||
let mut b = Pb::new();
|
||||
let g = match name {
|
||||
"lineInv" => {
|
||||
let l = b.m(0.0, h).l(w, 0.0).take();
|
||||
Geometry { paths: vec![stroke_only(l)], text_rect: full, handles: vec![], sites: vec![Point::new(0.0, h), Point::new(w, 0.0)] }
|
||||
}
|
||||
"nonIsoscelesTrapezoid" => {
|
||||
let max = 50000.0 * w / ss.max(1e-9);
|
||||
let (v1, v2) = (a(0).clamp(0.0, max), a(1).clamp(0.0, max));
|
||||
let (d1, d2) = (ss * v1 / 1e5, ss * v2 / 1e5);
|
||||
let mut g = simple(b.poly(&[(0.0, h), (d1, 0.0), (w - d2, 0.0), (w, h)]).take(), Rect::new(d1 * 0.66, h * 0.2, w - d2 * 0.66, h), w, h);
|
||||
g.handles.push(Handle::new(0, Point::ZERO, Vec2::new(ss / 1e5, 0.0), v1, 0.0, max));
|
||||
g.handles.push(Handle::new(1, Point::new(w, 0.0), Vec2::new(-ss / 1e5, 0.0), v2, 0.0, max));
|
||||
g
|
||||
}
|
||||
"pieWedge" => {
|
||||
b.m(0.0, h).arc(w, h, 180.0, 90.0).l(w, h).z();
|
||||
simple(b.take(), Rect::new(w * 0.29289, h * 0.29289, w, h), w, h)
|
||||
}
|
||||
"flowChartOfflineStorage" => {
|
||||
let tri = b.poly(&[(0.0, 0.0), (w, 0.0), (w / 2.0, h)]).take();
|
||||
let bar = Pb::new().m(w * 0.4, h * 0.8).l(w * 0.6, h * 0.8).take();
|
||||
Geometry {
|
||||
paths: vec![fill(tri), stroke_only(bar)],
|
||||
text_rect: Rect::new(w / 4.0, 0.0, w * 0.75, h * 0.8),
|
||||
handles: vec![],
|
||||
sites: vec![Point::new(w / 2.0, 0.0), Point::new(w / 4.0, h / 2.0), Point::new(w / 2.0, h), Point::new(w * 0.75, h / 2.0)],
|
||||
}
|
||||
}
|
||||
"chartX" | "chartStar" | "chartPlus" => {
|
||||
let body = corner_rect(w, h, [Corner::Square; 4]);
|
||||
let mut l = Pb::new();
|
||||
if name != "chartPlus" {
|
||||
l.m(0.0, 0.0).l(w, h).m(0.0, h).l(w, 0.0);
|
||||
}
|
||||
if name != "chartX" {
|
||||
l.m(w / 2.0, 0.0).l(w / 2.0, h);
|
||||
}
|
||||
if name == "chartPlus" {
|
||||
l.m(0.0, h / 2.0).l(w, h / 2.0);
|
||||
}
|
||||
Geometry { paths: vec![fill_only(body), stroke_only(l.take())], text_rect: full, handles: vec![], sites: mid_sites(w, h) }
|
||||
}
|
||||
"cornerTabs" | "squareTabs" | "plaqueTabs" => {
|
||||
// Each tab's size is a twentieth of the diagonal.
|
||||
let d = (w.hypot(h) / 20.0).min(w / 2.0).min(h / 2.0);
|
||||
match name {
|
||||
"cornerTabs" => {
|
||||
b.poly(&[(0.0, 0.0), (d, 0.0), (0.0, d)]);
|
||||
b.poly(&[(w - d, 0.0), (w, 0.0), (w, d)]);
|
||||
b.poly(&[(w, h - d), (w, h), (w - d, h)]);
|
||||
b.poly(&[(0.0, h - d), (d, h), (0.0, h)]);
|
||||
}
|
||||
"squareTabs" => {
|
||||
b.poly(&[(0.0, 0.0), (d, 0.0), (d, d), (0.0, d)]);
|
||||
b.poly(&[(w - d, 0.0), (w, 0.0), (w, d), (w - d, d)]);
|
||||
b.poly(&[(w - d, h - d), (w, h - d), (w, h), (w - d, h)]);
|
||||
b.poly(&[(0.0, h - d), (d, h - d), (d, h), (0.0, h)]);
|
||||
}
|
||||
_ => {
|
||||
if d > 0.0 {
|
||||
b.m(0.0, 0.0).l(d, 0.0).arc(d, d, 0.0, 90.0).z();
|
||||
b.m(w, 0.0).l(w, d).arc(d, d, 90.0, 90.0).z();
|
||||
b.m(w, h).l(w - d, h).arc(d, d, 180.0, 90.0).z();
|
||||
b.m(0.0, h).l(0.0, h - d).arc(d, d, 270.0, 90.0).z();
|
||||
} else {
|
||||
b.m(0.0, 0.0).z();
|
||||
}
|
||||
}
|
||||
}
|
||||
Geometry { paths: vec![fill(b.take())], text_rect: full.inset(-d), handles: vec![], sites: mid_sites(w, h) }
|
||||
}
|
||||
"callout1"
|
||||
| "callout2"
|
||||
| "callout3"
|
||||
| "accentCallout1"
|
||||
| "accentCallout2"
|
||||
| "accentCallout3"
|
||||
| "borderCallout2"
|
||||
| "borderCallout3"
|
||||
| "accentBorderCallout1"
|
||||
| "accentBorderCallout2"
|
||||
| "accentBorderCallout3" => {
|
||||
let n = match name.chars().last() {
|
||||
Some('2') => 2,
|
||||
Some('3') => 3,
|
||||
_ => 1,
|
||||
};
|
||||
line_callout(w, h, a, n + 1, name.starts_with("accent"), name.contains("order"))
|
||||
}
|
||||
"leftRightArrowCallout" | "upDownArrowCallout" => {
|
||||
let vertical = name == "upDownArrowCallout";
|
||||
let (l, t) = if vertical { (h, w) } else { (w, h) };
|
||||
let ss = l.min(t);
|
||||
let head = (ss * a(1).max(0.0) / 1e5).min(t / 2.0);
|
||||
let shaft = (ss * a(0).max(0.0) / 1e5).min(head * 2.0);
|
||||
let hl = (ss * a(2).max(0.0) / 1e5).min(l / 2.0);
|
||||
let boxl = (l * a(3).max(0.0) / 1e5).min(l - 2.0 * hl);
|
||||
let (c, b0, b1) = (t / 2.0, (l - boxl) / 2.0, (l + boxl) / 2.0);
|
||||
let pts = [
|
||||
(0.0, c),
|
||||
(hl, c - head),
|
||||
(hl, c - shaft / 2.0),
|
||||
(b0, c - shaft / 2.0),
|
||||
(b0, 0.0),
|
||||
(b1, 0.0),
|
||||
(b1, c - shaft / 2.0),
|
||||
(l - hl, c - shaft / 2.0),
|
||||
(l - hl, c - head),
|
||||
(l, c),
|
||||
(l - hl, c + head),
|
||||
(l - hl, c + shaft / 2.0),
|
||||
(b1, c + shaft / 2.0),
|
||||
(b1, t),
|
||||
(b0, t),
|
||||
(b0, c + shaft / 2.0),
|
||||
(hl, c + shaft / 2.0),
|
||||
(hl, c + head),
|
||||
];
|
||||
let d = if vertical { Dir::Down } else { Dir::Right };
|
||||
let pts = orient(d, w, h, &pts);
|
||||
let tr = orient(d, w, h, &[(b0, 0.0), (b1, t)]);
|
||||
simple(b.poly(&pts).take(), Rect::from_points(Point::new(tr[0].0, tr[0].1), Point::new(tr[1].0, tr[1].1)).abs(), w, h)
|
||||
}
|
||||
"quadArrowCallout" => {
|
||||
let head = (ss * a(1).max(0.0) / 1e5).min(ss / 2.0);
|
||||
let shaft = (ss * a(0).max(0.0) / 1e5).min(head * 2.0);
|
||||
let hl = (ss * a(2).max(0.0) / 1e5).min(ss / 2.0);
|
||||
let k = a(3).clamp(0.0, 100000.0) / 1e5;
|
||||
let (bw, bh) = ((w * k).min(w - 2.0 * hl).max(shaft), (h * k).min(h - 2.0 * hl).max(shaft));
|
||||
let (cx, cy) = (w / 2.0, h / 2.0);
|
||||
let (bx0, bx1, by0, by1) = (cx - bw / 2.0, cx + bw / 2.0, cy - bh / 2.0, cy + bh / 2.0);
|
||||
let (s2, hd) = (shaft / 2.0, head);
|
||||
let pts = [
|
||||
(0.0, cy),
|
||||
(hl, cy - hd),
|
||||
(hl, cy - s2),
|
||||
(bx0, cy - s2),
|
||||
(bx0, by0),
|
||||
(cx - s2, by0),
|
||||
(cx - s2, hl),
|
||||
(cx - hd, hl),
|
||||
(cx, 0.0),
|
||||
(cx + hd, hl),
|
||||
(cx + s2, hl),
|
||||
(cx + s2, by0),
|
||||
(bx1, by0),
|
||||
(bx1, cy - s2),
|
||||
(w - hl, cy - s2),
|
||||
(w - hl, cy - hd),
|
||||
(w, cy),
|
||||
(w - hl, cy + hd),
|
||||
(w - hl, cy + s2),
|
||||
(bx1, cy + s2),
|
||||
(bx1, by1),
|
||||
(cx + s2, by1),
|
||||
(cx + s2, h - hl),
|
||||
(cx + hd, h - hl),
|
||||
(cx, h),
|
||||
(cx - hd, h - hl),
|
||||
(cx - s2, h - hl),
|
||||
(cx - s2, by1),
|
||||
(bx0, by1),
|
||||
(bx0, cy + s2),
|
||||
(hl, cy + s2),
|
||||
(hl, cy + hd),
|
||||
];
|
||||
simple(b.poly(&pts).take(), Rect::new(bx0, by0, bx1, by1), w, h)
|
||||
}
|
||||
"curvedRightArrow" | "curvedLeftArrow" | "curvedUpArrow" | "curvedDownArrow" => curved_arrow(name, w, h, a(0), a(1), a(2)),
|
||||
"swooshArrow" => swoosh(w, h, a(0), a(1)),
|
||||
"ellipseRibbon" | "ellipseRibbon2" => ellipse_ribbon(name == "ellipseRibbon2", w, h, a(0), a(1), a(2)),
|
||||
"leftRightRibbon" => left_right_ribbon(w, h, a(0), a(1), a(2)),
|
||||
"gear6" | "gear9" => gear(if name == "gear6" { 6 } else { 9 }, w, h, a(0), a(1)),
|
||||
"funnel" => funnel(w, h),
|
||||
"circularArrow" | "leftCircularArrow" | "leftRightCircularArrow" => circular_arrow(name, w, h, [a(0), a(1), a(2), a(3), a(4)]),
|
||||
_ => return None,
|
||||
};
|
||||
Some(g)
|
||||
}
|
||||
|
||||
/// An ellipse-centred frame for the circular arrows: the band's middle runs on the ellipse
|
||||
/// `(rx, ry)`, its edges `th/2` either side.
|
||||
struct Ring {
|
||||
c: Point,
|
||||
rx: f64,
|
||||
ry: f64,
|
||||
th: f64,
|
||||
hw: f64,
|
||||
}
|
||||
|
||||
impl Ring {
|
||||
/// The point at visual angle `deg` on the ellipse grown by `d`.
|
||||
fn at(&self, deg: f64, d: f64) -> Point {
|
||||
let (ax, ay) = ((self.rx + d).max(0.0), (self.ry + d).max(0.0));
|
||||
let t = vis(deg, ax, ay).to_radians();
|
||||
Point::new(self.c.x + ax * t.cos(), self.c.y + ay * t.sin())
|
||||
}
|
||||
/// Parametric angle (degrees) of `p` on the ellipse grown by `d`.
|
||||
fn param(&self, p: Point, d: f64) -> f64 {
|
||||
let (ax, ay) = ((self.rx + d).max(1e-9), (self.ry + d).max(1e-9));
|
||||
((p.y - self.c.y) / ay).atan2((p.x - self.c.x) / ax).to_degrees()
|
||||
}
|
||||
/// Where the line `b + s·u` meets the ellipse grown by `d`, taking the crossing nearest `b`
|
||||
/// on the side `sign` (+1 outwards, −1 inwards).
|
||||
fn cross(&self, b: Point, u: Vec2, d: f64, sign: f64) -> Point {
|
||||
let (ax, ay) = ((self.rx + d).max(1e-9), (self.ry + d).max(1e-9));
|
||||
let (px, py) = ((b.x - self.c.x) / ax, (b.y - self.c.y) / ay);
|
||||
let (ux, uy) = (u.x / ax, u.y / ay);
|
||||
let qa = ux * ux + uy * uy;
|
||||
let qb = 2.0 * (px * ux + py * uy);
|
||||
let qc = px * px + py * py - 1.0;
|
||||
let disc = qb * qb - 4.0 * qa * qc;
|
||||
if qa < 1e-18 || disc < 0.0 {
|
||||
// The line misses (a long head on a thin ring): the edge then carries on from the
|
||||
// base centre itself, as the reference does.
|
||||
return b;
|
||||
}
|
||||
let r = disc.sqrt();
|
||||
let (s0, s1) = ((-qb - r) / (2.0 * qa), (-qb + r) / (2.0 * qa));
|
||||
let s = if sign > 0.0 {
|
||||
if s0 >= 0.0 { s0 } else { s1 }
|
||||
} else if s1 <= 0.0 {
|
||||
s1
|
||||
} else {
|
||||
s0
|
||||
};
|
||||
b + u * s
|
||||
}
|
||||
/// An arrow head whose base centre sits on the middle ellipse at `base` degrees and whose tip
|
||||
/// sits there at `base + sweep`. The base runs along the line from the centre to the tip.
|
||||
/// Returns (band outer edge, outer wing, tip, inner wing, band inner edge).
|
||||
fn head(&self, base: f64, sweep: f64) -> [Point; 5] {
|
||||
let b = self.at(base, 0.0);
|
||||
let tip = self.at(base + sweep, 0.0);
|
||||
let d = tip - self.c;
|
||||
let u = if d.hypot() > 1e-12 { d / d.hypot() } else { Vec2::new(1.0, 0.0) };
|
||||
let half = self.th / 2.0;
|
||||
[self.cross(b, u, half, 1.0), b + u * self.hw, tip, b - u * self.hw, self.cross(b, u, -half, -1.0)]
|
||||
}
|
||||
}
|
||||
|
||||
/// Sweep (degrees) from `a` to `b` turning one way: positive when `dir > 0`.
|
||||
fn turn(a: f64, b: f64, dir: f64) -> f64 {
|
||||
if dir > 0.0 { (b - a).rem_euclid(360.0) } else { -(a - b).rem_euclid(360.0) }
|
||||
}
|
||||
|
||||
/// The circular arrows: a band along an ellipse with one head (`circularArrow` clockwise,
|
||||
/// `leftCircularArrow` anticlockwise) or a head at each end (`leftRightCircularArrow`).
|
||||
/// Adjusts: band thickness, head sweep (degrees), end angle, start angle, head half-width.
|
||||
fn circular_arrow(name: &str, w: f64, h: f64, a: [f64; 5]) -> Geometry {
|
||||
let ss = w.min(h);
|
||||
if ss <= 0.0 {
|
||||
return simple(corner_rect(w, h, [Corner::Square; 4]), Rect::new(0.0, 0.0, w, h), w, h);
|
||||
}
|
||||
let a5 = a[4].clamp(0.0, 25000.0);
|
||||
let hw = ss * a5 / 1e5;
|
||||
let th = ss * a[0].clamp(0.0, 2.0 * a5) / 1e5;
|
||||
let ring = Ring { c: Point::new(w / 2.0, h / 2.0), rx: (w / 2.0 - hw).max(th / 2.0), ry: (h / 2.0 - hw).max(th / 2.0), th, hw };
|
||||
let deg = |v: f64| (v / 60000.0) % 360.0;
|
||||
let (sweep, end, start) = (deg(a[1]), deg(a[2]), deg(a[3]));
|
||||
let half = th / 2.0;
|
||||
let mut b = Pb::new();
|
||||
// Arcs of the ellipse grown by `d`, drawn on from the current point (which is on it except in
|
||||
// the degenerate case above, where the arc shifts with it like the reference's).
|
||||
let edge_arc = |b: &mut Pb, from: Point, to: Point, d: f64, dir: f64| {
|
||||
let (ax, ay) = ((ring.rx + d).max(0.0), (ring.ry + d).max(0.0));
|
||||
let (s, e) = (ring.param(from, d), ring.param(to, d));
|
||||
b.arc(ax, ay, s, turn(s, e, dir));
|
||||
};
|
||||
let outer_arc = |b: &mut Pb, from: Point, to: Point, dir: f64| edge_arc(b, from, to, half, dir);
|
||||
let inner_arc = |b: &mut Pb, from: Point, to: Point, dir: f64| edge_arc(b, from, to, -half, dir);
|
||||
if name == "leftRightCircularArrow" {
|
||||
let hs = ring.head(start, -sweep);
|
||||
let he = ring.head(end, sweep);
|
||||
b.m(hs[0].x, hs[0].y);
|
||||
outer_arc(&mut b, hs[0], he[0], 1.0);
|
||||
b.l(he[1].x, he[1].y).l(he[2].x, he[2].y).l(he[3].x, he[3].y).l(he[4].x, he[4].y);
|
||||
inner_arc(&mut b, he[4], hs[4], -1.0);
|
||||
b.l(hs[3].x, hs[3].y).l(hs[2].x, hs[2].y).l(hs[1].x, hs[1].y).z();
|
||||
} else {
|
||||
let dir = if name == "circularArrow" { 1.0 } else { -1.0 };
|
||||
let he = ring.head(end, sweep);
|
||||
let (o0, i0) = (ring.at(start, half), ring.at(start, -half));
|
||||
b.m(o0.x, o0.y);
|
||||
outer_arc(&mut b, o0, he[0], dir);
|
||||
b.l(he[1].x, he[1].y).l(he[2].x, he[2].y).l(he[3].x, he[3].y).l(he[4].x, he[4].y);
|
||||
inner_arc(&mut b, he[4], i0, -dir);
|
||||
b.z();
|
||||
}
|
||||
simple(b.take(), Rect::new(0.0, 0.0, w, h), w, h)
|
||||
}
|
||||
|
||||
/// A funnel: an elliptical rim (with an open mouth) across the top quarter, and straight sides
|
||||
/// down to a small rounded spout. The side lines leave the rim and meet the spout at fixed
|
||||
/// angles, fitted to the reference look.
|
||||
fn funnel(w: f64, h: f64) -> Geometry {
|
||||
const RIM: f64 = 173.0; // where the left side leaves the rim, degrees on the rim ellipse
|
||||
const SPOUT: f64 = 163.4; // where it meets the spout, degrees on the spout ellipse
|
||||
let ss = w.min(h);
|
||||
let (cx, rim_y) = (w / 2.0, h / 4.0);
|
||||
let (sx, sy) = (w / 8.0, h / 15.0);
|
||||
let spout_y = h - sy;
|
||||
let at = |rx: f64, ry: f64, cy: f64, deg: f64| {
|
||||
let t = deg.to_radians();
|
||||
Point::new(cx + rx * t.cos(), cy + ry * t.sin())
|
||||
};
|
||||
let p0 = at(cx, rim_y, rim_y, RIM);
|
||||
let p1 = at(sx, sy, spout_y, 180.0 - SPOUT);
|
||||
let mut b = Pb::new();
|
||||
b.m(p0.x, p0.y).arc(cx, rim_y, RIM, 540.0 - 2.0 * RIM);
|
||||
b.l(p1.x, p1.y).arc(sx, sy, 180.0 - SPOUT, 2.0 * SPOUT - 180.0).z();
|
||||
let d = ss / 20.0;
|
||||
b.ellipse(cx, rim_y, (cx - d).max(0.0), (rim_y - d).max(0.0));
|
||||
Geometry { paths: vec![fill_eo(b.take())], text_rect: Rect::new(w / 4.0, h / 2.0, w * 0.75, h - sy), handles: vec![], sites: mid_sites(w, h) }
|
||||
}
|
||||
|
||||
/// A gear: an elliptical body with `n` teeth, the first pointing straight up. Each tooth leaves
|
||||
/// the body at two points an equal angle either side of its centre, and its tip corners sit a
|
||||
/// tooth height further out along the body's normal and a flank lean further in.
|
||||
fn gear(n: usize, w: f64, h: f64, a0: f64, a1: f64) -> Geometry {
|
||||
let ss = w.min(h);
|
||||
let th = ss * a0.clamp(0.0, 20000.0) / 1e5; // tooth height
|
||||
let lean_max = if n == 6 { 5380.0 } else { 2730.0 };
|
||||
let l = ss * a1.clamp(0.0, a0.clamp(0.0, 20000.0).min(lean_max)) / 1e5; // flank lean
|
||||
let (cx, cy) = (w / 2.0, h / 2.0);
|
||||
let (rx, ry) = ((cx - th).max(0.0), (cy - th).max(0.0));
|
||||
let r = (ss / 2.0 - th).max(1e-9);
|
||||
let half = ((th + l) / 2.0 / r).atan();
|
||||
let step = std::f64::consts::TAU / n as f64;
|
||||
let on_body = |t: f64| Point::new(cx + rx * t.cos(), cy + ry * t.sin());
|
||||
let mut b = Pb::new();
|
||||
for k in 0..n {
|
||||
let t = -std::f64::consts::FRAC_PI_2 + step * k as f64;
|
||||
// The tooth stands along the body's normal at its centre.
|
||||
let nrm = Vec2::new(t.cos() * ry, t.sin() * rx);
|
||||
let u = if nrm.hypot() > 1e-12 { nrm / nrm.hypot() } else { Vec2::new(t.cos(), t.sin()) };
|
||||
let v = Vec2::new(-u.y, u.x);
|
||||
let (r0, r1) = (on_body(t - half), on_body(t + half));
|
||||
let (c0, c1) = (r0 + u * th + v * l, r1 + u * th - v * l);
|
||||
if k == 0 {
|
||||
b.m(r0.x, r0.y);
|
||||
} else {
|
||||
b.arc(rx, ry, (t - step + half).to_degrees(), (step - 2.0 * half).to_degrees());
|
||||
}
|
||||
b.l(c0.x, c0.y).l(c1.x, c1.y).l(r1.x, r1.y);
|
||||
}
|
||||
let t_last = -std::f64::consts::FRAC_PI_2 + step * (n - 1) as f64;
|
||||
b.arc(rx, ry, (t_last + half).to_degrees(), (step - 2.0 * half).to_degrees()).z();
|
||||
let k = std::f64::consts::FRAC_1_SQRT_2;
|
||||
let text = Rect::new(cx - rx * k, cy - ry * k, cx + rx * k, cy + ry * k);
|
||||
simple(b.take(), text, w, h)
|
||||
}
|
||||
|
||||
/// Two arrows on a band that steps down in the middle: the left arrow sits high, the right one low,
|
||||
/// and the band rolls over between them in a shaded half-ellipse curl.
|
||||
fn left_right_ribbon(w: f64, h: f64, a0: f64, a1: f64, a2: f64) -> Geometry {
|
||||
let ss = w.min(h);
|
||||
let sh = h * a2.clamp(0.0, 33333.0) / 1e5; // step between the two halves
|
||||
let bt = (h * a0.max(0.0) / 1e5).min(h - sh); // band thickness
|
||||
let cx = w / 2.0;
|
||||
let (rx, ry) = (w / 32.0, (sh / 4.0).min(w / 24.0));
|
||||
let hb = (ss * a1.max(0.0) / 1e5).min(cx - rx); // head length
|
||||
let yc = (h - sh) / 2.0;
|
||||
let (lt, lb) = (yc - bt / 2.0, yc + bt / 2.0);
|
||||
let (rt, rb) = (lt + sh, lb + sh);
|
||||
// The left half: filled as a closed outline, stroked only where it can show.
|
||||
let mut edge = Pb::new();
|
||||
edge.m(cx - rx, lb).l(hb, lb).l(hb, h - sh).l(0.0, yc).l(hb, 0.0).l(hb, lt).l(cx, lt);
|
||||
edge.arc(rx, ry, 270.0, 180.0).arc(rx, ry, 270.0, -90.0);
|
||||
let edge = edge.take();
|
||||
let mut left = Pb::new();
|
||||
left.m(0.0, yc).l(hb, 0.0).l(hb, lt).l(cx, lt).arc(rx, ry, 270.0, 180.0).arc(rx, ry, 270.0, -180.0);
|
||||
left.l(cx, lb.max(lt + 4.0 * ry)).l(hb, lb).l(hb, h - sh).z();
|
||||
let left = left.take();
|
||||
let mut curl = Pb::new();
|
||||
curl.m(cx + rx, lt + ry).l(cx + rx, rt).l(cx, lt + 4.0 * ry).arc(rx, ry, 90.0, 180.0).arc(rx, ry, 90.0, -90.0).z();
|
||||
let mut right = Pb::new();
|
||||
right.m(w, yc + sh).l(w - hb, h).l(w - hb, rb).l(cx, rb).arc(rx, ry, 90.0, 90.0).l(cx - rx, rt - ry).arc(rx, ry, 180.0, -90.0);
|
||||
right.l(w - hb, rt).l(w - hb, sh).z();
|
||||
Geometry {
|
||||
paths: vec![fill_only(left), stroke_only(edge), shade(curl.take(), FillMode::DarkenLess), fill(right.take())],
|
||||
text_rect: Rect::new(hb.min(cx - rx), rt, (w - hb).max(cx + rx), lb.max(rt)),
|
||||
handles: vec![],
|
||||
sites: vec![Point::new(0.0, yc), Point::new(cx, rt), Point::new(w, yc + sh), Point::new(cx, lb)],
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a piece of the parabola `y = y0 + d·(1 − ((x − w/2)/(w/2))²)` from the current point (at
|
||||
/// `xa`) to `xb`, as one quadratic curve.
|
||||
fn sag(b: &mut Pb, w: f64, d: f64, y0: f64, xa: f64, xb: f64) {
|
||||
let hw = (w / 2.0).max(1e-9);
|
||||
let f = |x: f64| y0 + d * (1.0 - ((x - hw) / hw).powi(2));
|
||||
let slope = -2.0 * d * (xa - hw) / (hw * hw);
|
||||
b.q((xa + xb) / 2.0, f(xa) + slope * (xb - xa) / 2.0, xb, f(xb));
|
||||
}
|
||||
|
||||
/// A ribbon whose edges sag along one parabola: a front band in the middle and two notched ends
|
||||
/// behind it, joined by shaded folds. Built sagging downwards; `up` mirrors it vertically.
|
||||
fn ellipse_ribbon(up: bool, w: f64, h: f64, a0: f64, a1: f64, a2: f64) -> Geometry {
|
||||
let th = h * a0.clamp(0.0, 100000.0) / 1e5; // band drop at the centre
|
||||
// Sag of every edge: never more than the drop, never so little that the band would rise
|
||||
// above the ends by more than half their height.
|
||||
let d = (h * a2.max(0.0) / 1e5).max(th - (h - th) / 2.0).clamp(0.0, th);
|
||||
let bw = w * a1.clamp(25000.0, 75000.0) / 1e5;
|
||||
let (x0, x1) = ((w - bw) / 2.0, (w + bw) / 2.0);
|
||||
let ear = w / 8.0;
|
||||
let (xf, xg) = (x0 + ear, x1 - ear);
|
||||
let e = h - th; // band height
|
||||
let off = th - d; // band top below the ends' top
|
||||
let t = |x: f64| {
|
||||
let hw = (w / 2.0).max(1e-9);
|
||||
d * (1.0 - ((x - hw) / hw).powi(2))
|
||||
};
|
||||
let mut ends = Pb::new();
|
||||
ends.m(0.0, 0.0);
|
||||
sag(&mut ends, w, d, 0.0, 0.0, xf);
|
||||
ends.l(xf, t(xf) + off).l(x0, t(x0) + off).l(x0, t(x0) + e);
|
||||
sag(&mut ends, w, d, e, x0, 0.0);
|
||||
ends.l(ear, t(ear) + e / 2.0).z();
|
||||
ends.m(w, 0.0).l(w - ear, t(w - ear) + e / 2.0).l(w, e);
|
||||
sag(&mut ends, w, d, e, w, x1);
|
||||
ends.l(x1, t(x1) + off).l(xg, t(xg) + off).l(xg, t(xg));
|
||||
sag(&mut ends, w, d, 0.0, xg, w);
|
||||
ends.z();
|
||||
let mut folds = Pb::new();
|
||||
folds.poly(&[(x0, t(x0) + off), (xf, t(xf)), (xf, t(xf) + off)]);
|
||||
folds.poly(&[(x1, t(x1) + off), (xg, t(xg)), (xg, t(xg) + off)]);
|
||||
let mut band = Pb::new();
|
||||
band.m(x0, t(x0) + off);
|
||||
sag(&mut band, w, d, off, x0, x1);
|
||||
band.l(x1, t(x1) + off + e);
|
||||
sag(&mut band, w, d, off + e, x1, x0);
|
||||
band.z();
|
||||
let mut paths = vec![fill(ends.take()), shade(folds.take(), FillMode::DarkenLess), fill(band.take())];
|
||||
let mut text = Rect::new(x0, th, x1, t(x0) + off + e);
|
||||
if up {
|
||||
let m = kurbo::Affine::new([1.0, 0.0, 0.0, -1.0, 0.0, h]);
|
||||
for p in &mut paths {
|
||||
p.path.apply_affine(m);
|
||||
}
|
||||
text = m.transform_rect_bbox(text);
|
||||
}
|
||||
Geometry { paths, text_rect: text, handles: vec![], sites: mid_sites(w, h) }
|
||||
}
|
||||
|
||||
/// A sweeping arrow rising from the bottom-left corner. The band's two edges are quadratic curves
|
||||
/// from the corner; the head's base leans slightly forward and reaches one eighth of the shorter
|
||||
/// side above and below the band. The tilt and the tip's lift were fitted to the reference look.
|
||||
fn swoosh(w: f64, h: f64, a0: f64, a1: f64) -> Geometry {
|
||||
const TILT: f64 = 0.1126; // tan of the head base's lean from vertical
|
||||
const LIFT: f64 = 0.051; // tip height above the band's centre, per unit of height
|
||||
let ss = w.min(h);
|
||||
let e = ss / 8.0;
|
||||
let th = (h * a0.max(0.0) / 1e5).min((h - 2.0 * e).max(0.0));
|
||||
// The head is at least long enough to keep its leaning base inside the frame.
|
||||
let hl = (ss * a1.max(0.0) / 1e5).max((e + th) * TILT).min((w - e * TILT).max(0.0));
|
||||
let qx = w - hl;
|
||||
let mut b = Pb::new();
|
||||
b.m(0.0, h)
|
||||
.q(w / 6.0, h / 3.0, qx, e)
|
||||
.l(qx - e * TILT, 0.0)
|
||||
.l(w, (e + th / 2.0 - LIFT * h).clamp(0.0, 2.0 * e + th))
|
||||
.l((qx + (e + th) * TILT).min(w), 2.0 * e + th)
|
||||
.l((qx + th * TILT).min(w), e + th)
|
||||
.q(w / 4.0, (th + h / 12.0 + e).min(h), 0.0, h)
|
||||
.z();
|
||||
simple(b.take(), Rect::new(0.0, 0.0, w, h), w, h)
|
||||
}
|
||||
|
||||
/// A band that sweeps around half an ellipse and ends in an arrow head. Built pointing right
|
||||
/// (`curvedRightArrow`) in a `cw`×`ch` frame, then turned for the other directions.
|
||||
///
|
||||
/// Both halves of the band hang off one ellipse touching the far side: its centre sits on the
|
||||
/// head side's edge, its horizontal radius is the full length, and the band has a constant
|
||||
/// vertical thickness. The back half (shaded) starts flat against the head side; the front half
|
||||
/// runs to the head, centred on the band where it ends.
|
||||
fn curved_arrow(name: &str, w: f64, h: f64, a0: f64, a1: f64, a2: f64) -> Geometry {
|
||||
let along_x = matches!(name, "curvedRightArrow" | "curvedLeftArrow");
|
||||
let (cw, ch) = if along_x { (w, h) } else { (h, w) };
|
||||
let ss = cw.min(ch);
|
||||
let head = (ss * a1.max(0.0) / 1e5).min(ch / 2.0);
|
||||
let th = (ss * a0.max(0.0) / 1e5).min(head);
|
||||
let hl = (ss * a2.max(0.0) / 1e5).min(cw);
|
||||
let hh = head / 2.0;
|
||||
// The band's centre line, carried on to the head side, ends at the tip, which sits half a
|
||||
// head width above the bottom.
|
||||
let r = ((ch - hh - th / 2.0) / 2.0).max(0.0);
|
||||
// Parametric angle where the front band reaches the head base.
|
||||
let te = if cw > 0.0 { (-(hl / cw)).clamp(-1.0, 1.0).acos().to_degrees() } else { 90.0 };
|
||||
let mut back = Pb::new();
|
||||
back.m(cw, 0.0).arc(cw, r, 270.0, -90.0).l(0.0, r + th).arc(cw, r, 180.0, 90.0).z();
|
||||
let mut front = Pb::new();
|
||||
front.m(0.0, r).arc(cw, r, 180.0, te - 180.0);
|
||||
let yb = front.cur.y;
|
||||
let yc = yb + th / 2.0;
|
||||
front.l(cw - hl, yc - hh).l(cw, 2.0 * r + th / 2.0).l(cw - hl, yc + hh).l(cw - hl, yb + th).arc(cw, r, te, 180.0 - te).z();
|
||||
let m = match name {
|
||||
"curvedLeftArrow" => kurbo::Affine::new([-1.0, 0.0, 0.0, 1.0, w, 0.0]),
|
||||
"curvedUpArrow" => kurbo::Affine::new([0.0, -1.0, 1.0, 0.0, 0.0, h]),
|
||||
"curvedDownArrow" => kurbo::Affine::new([0.0, 1.0, 1.0, 0.0, 0.0, 0.0]),
|
||||
_ => kurbo::Affine::IDENTITY,
|
||||
};
|
||||
let (mut back, mut front) = (back.take(), front.take());
|
||||
back.apply_affine(m);
|
||||
front.apply_affine(m);
|
||||
Geometry {
|
||||
paths: vec![shade(back, FillMode::DarkenLess), fill(front)],
|
||||
text_rect: Rect::new(0.0, 0.0, w, h),
|
||||
handles: vec![],
|
||||
sites: mid_sites(w, h),
|
||||
}
|
||||
}
|
||||
|
||||
/// A rectangle with a callout leader through `pts` points (`(y, x)` adjust pairs in 1/100 000 of
|
||||
/// the height and width), optionally with an accent bar at the first point's x and a border.
|
||||
fn line_callout(w: f64, h: f64, a: &dyn Fn(usize) -> f64, pts: usize, accent: bool, border: bool) -> Geometry {
|
||||
let body = corner_rect(w, h, [Corner::Square; 4]);
|
||||
// Leaders may reach well outside the box, but not without bound.
|
||||
let pt = |i: usize| (w * a(2 * i + 1).clamp(-1e7, 1e7) / 1e5, h * a(2 * i).clamp(-1e7, 1e7) / 1e5);
|
||||
let mut l = Pb::new();
|
||||
let (x0, y0) = pt(0);
|
||||
l.m(x0, y0);
|
||||
for i in 1..pts {
|
||||
let (x, y) = pt(i);
|
||||
l.l(x, y);
|
||||
}
|
||||
let mut paths = vec![if border { fill(body) } else { fill_only(body) }];
|
||||
if accent {
|
||||
paths.push(stroke_only(Pb::new().m(x0, 0.0).l(x0, h).take()));
|
||||
}
|
||||
paths.push(stroke_only(l.take()));
|
||||
Geometry { paths, text_rect: Rect::new(0.0, 0.0, w, h), handles: vec![], sites: mid_sites(w, h) }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::{CATALOG, FillMode, build, info};
|
||||
use kurbo::{BezPath, PathEl, Point, Shape};
|
||||
|
||||
const NEW: [&str; 38] = [
|
||||
"lineInv",
|
||||
"nonIsoscelesTrapezoid",
|
||||
"pieWedge",
|
||||
"leftRightArrowCallout",
|
||||
"upDownArrowCallout",
|
||||
"quadArrowCallout",
|
||||
"circularArrow",
|
||||
"leftCircularArrow",
|
||||
"leftRightCircularArrow",
|
||||
"curvedRightArrow",
|
||||
"curvedLeftArrow",
|
||||
"curvedUpArrow",
|
||||
"curvedDownArrow",
|
||||
"swooshArrow",
|
||||
"callout1",
|
||||
"callout2",
|
||||
"callout3",
|
||||
"accentCallout1",
|
||||
"accentCallout2",
|
||||
"accentCallout3",
|
||||
"borderCallout2",
|
||||
"borderCallout3",
|
||||
"accentBorderCallout1",
|
||||
"accentBorderCallout2",
|
||||
"accentBorderCallout3",
|
||||
"ellipseRibbon",
|
||||
"ellipseRibbon2",
|
||||
"leftRightRibbon",
|
||||
"flowChartOfflineStorage",
|
||||
"gear6",
|
||||
"gear9",
|
||||
"funnel",
|
||||
"cornerTabs",
|
||||
"squareTabs",
|
||||
"plaqueTabs",
|
||||
"chartX",
|
||||
"chartStar",
|
||||
"chartPlus",
|
||||
];
|
||||
|
||||
fn outline(name: &str, w: f64, h: f64, adj: &[f64]) -> BezPath {
|
||||
build(name, w, h, adj).map(|g| g.outline()).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Distance from `p` to the nearest point of the (flattened) outline.
|
||||
fn dist(path: &BezPath, p: Point) -> f64 {
|
||||
let mut best = f64::MAX;
|
||||
let mut prev = Point::ZERO;
|
||||
let mut start = Point::ZERO;
|
||||
kurbo::flatten(path.iter(), 0.01, |el| {
|
||||
let (a, b) = match el {
|
||||
PathEl::MoveTo(q) => {
|
||||
prev = q;
|
||||
start = q;
|
||||
return;
|
||||
}
|
||||
PathEl::LineTo(q) => (prev, q),
|
||||
PathEl::ClosePath => (prev, start),
|
||||
_ => return,
|
||||
};
|
||||
let d = b - a;
|
||||
let t = if d.hypot2() > 0.0 { ((p - a).dot(d) / d.hypot2()).clamp(0.0, 1.0) } else { 0.0 };
|
||||
best = best.min((a + d * t - p).hypot());
|
||||
prev = b;
|
||||
});
|
||||
best
|
||||
}
|
||||
|
||||
/// Asserts every point lies within 1pt of the outline.
|
||||
fn on(name: &str, w: f64, h: f64, adj: &[f64], pts: &[(f64, f64)]) {
|
||||
let path = outline(name, w, h, adj);
|
||||
for &(x, y) in pts {
|
||||
let d = dist(&path, Point::new(x, y));
|
||||
assert!(d < 1.0, "{name} {w}x{h} {adj:?}: ({x}, {y}) is {d:.2} from the outline");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_presets_are_in_the_catalog() {
|
||||
for n in NEW {
|
||||
let i = info(n).unwrap_or_else(|| panic!("{n} missing from the catalog"));
|
||||
assert!(!i.label.is_empty());
|
||||
}
|
||||
assert!(CATALOG.len() >= 183);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_presets_build_within_bounds_at_many_sizes_and_adjusts() {
|
||||
let sizes = [(480.0, 300.0), (300.0, 300.0), (200.0, 460.0), (100.0, 460.0), (1.0, 1.0), (0.0, 0.0), (0.0, 50.0), (60.0, 0.0)];
|
||||
let adjusts: [&[f64]; 8] = [
|
||||
&[],
|
||||
&[0.0; 8],
|
||||
&[100000.0; 8],
|
||||
&[-50000.0; 8],
|
||||
&[1e12, -1e12, 1e12, -1e12, 1e12, -1e12, 1e12, -1e12],
|
||||
&[f64::MAX, f64::MIN, f64::MAX, f64::MIN, f64::MAX, f64::MIN, f64::MAX, f64::MIN],
|
||||
&[f64::NAN, f64::INFINITY, f64::NEG_INFINITY, f64::NAN, 5.0, 6.0, 7.0, 8.0],
|
||||
&[30000.0, 60000.0, 20000.0, 9000000.0, 20000.0, 40000.0, 120000.0, -30000.0],
|
||||
];
|
||||
for n in NEW {
|
||||
// Line callouts' leaders may reach anywhere, by design.
|
||||
let callout = n.contains("allout") && !n.contains("Arrow");
|
||||
for (w, h) in sizes {
|
||||
for adj in adjusts {
|
||||
let g = build(n, w, h, adj).unwrap_or_else(|| panic!("{n} did not build"));
|
||||
assert!(!g.paths.is_empty(), "{n} has no paths");
|
||||
for s in &g.paths {
|
||||
let bb = s.path.bounding_box();
|
||||
assert!(bb.x0.is_finite() && bb.y0.is_finite() && bb.x1.is_finite() && bb.y1.is_finite(), "{n} {w}x{h} {adj:?}: {bb:?}");
|
||||
if !callout {
|
||||
// Circular arrows with a head longer than their ring can hold carry the
|
||||
// inner edge on from the head, as the reference does, so may spill.
|
||||
let spill = if n.contains("ircular") { 0.5 } else { 0.01 };
|
||||
let tol = 0.5 + w.max(h) * spill;
|
||||
assert!(
|
||||
bb.x0 >= -tol && bb.y0 >= -tol && bb.x1 <= w + tol && bb.y1 <= h + tol,
|
||||
"{n} {w}x{h} {adj:?} out of bounds: {bb:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
let t = g.text_rect;
|
||||
assert!(t.x0.is_finite() && t.y0.is_finite() && t.x1.is_finite() && t.y1.is_finite(), "{n} text rect {t:?}");
|
||||
}
|
||||
}
|
||||
let g = build(n, 480.0, 300.0, &[]).unwrap_or_else(|| panic!("{n}"));
|
||||
assert!(g.outline().elements().len() >= 2, "{n} default outline is empty");
|
||||
}
|
||||
}
|
||||
|
||||
// The spot checks below are outline points measured on reference renders.
|
||||
|
||||
#[test]
|
||||
fn simple_new_presets_match_reference_points() {
|
||||
on("lineInv", 480.0, 300.0, &[], &[(0.0, 300.0), (480.0, 0.0)]);
|
||||
on("nonIsoscelesTrapezoid", 480.0, 300.0, &[], &[(75.0, 0.0), (405.0, 0.0), (0.0, 300.0)]);
|
||||
on("nonIsoscelesTrapezoid", 200.0, 460.0, &[80000.0, 20000.0], &[(100.0, 0.0), (160.0, 0.0)]);
|
||||
on("pieWedge", 480.0, 300.0, &[], &[(64.3, 150.0), (0.0, 300.0), (480.0, 0.0)]);
|
||||
on("flowChartOfflineStorage", 480.0, 300.0, &[], &[(240.0, 300.0), (192.0, 240.0), (288.0, 240.0)]);
|
||||
on("squareTabs", 480.0, 300.0, &[], &[(28.3, 28.3), (451.7, 271.7)]);
|
||||
on("cornerTabs", 480.0, 300.0, &[], &[(23.3, 5.0)]);
|
||||
on("plaqueTabs", 480.0, 300.0, &[], &[(27.85, 5.0)]);
|
||||
on("chartStar", 480.0, 300.0, &[], &[(240.0, 0.0), (240.0, 300.0), (48.0, 30.0)]);
|
||||
on("chartPlus", 480.0, 300.0, &[], &[(0.0, 150.0), (240.0, 0.0)]);
|
||||
// Chart markers and plain callouts leave the box unstroked.
|
||||
for n in ["chartX", "callout1", "accentCallout2"] {
|
||||
let g = build(n, 100.0, 100.0, &[]).unwrap_or_else(|| panic!("{n}"));
|
||||
assert!(!g.paths[0].stroke, "{n}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn line_callouts_follow_their_adjusts() {
|
||||
on("callout3", 480.0, 300.0, &[], &[(-40.0, 56.25), (-80.0, 56.25), (-80.0, 300.0), (-40.0, 338.9)]);
|
||||
let adj = [50000.0, 110000.0, 20000.0, 120000.0, -20000.0, 80000.0, 30000.0, 40000.0];
|
||||
let g = build("accentBorderCallout3", 480.0, 300.0, &adj).unwrap_or_else(|| panic!("accentBorderCallout3"));
|
||||
assert_eq!(g.paths.len(), 3);
|
||||
assert!(g.paths[0].stroke);
|
||||
let accent = g.paths[1].path.bounding_box();
|
||||
assert!((accent.x0 - 528.0).abs() < 1e-6 && accent.y0 == 0.0 && accent.y1 == 300.0, "{accent:?}");
|
||||
on("accentBorderCallout3", 480.0, 300.0, &adj, &[(576.0, 60.0), (384.0, -60.0), (192.0, 90.0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arrow_callouts_match_reference_points() {
|
||||
on("leftRightArrowCallout", 480.0, 300.0, &[], &[(0.0, 150.0), (75.0, 75.0), (124.5, 0.0), (355.5, 300.0), (480.0, 150.0)]);
|
||||
on("leftRightArrowCallout", 200.0, 460.0, &[], &[(50.0, 180.0), (52.0, 0.0), (148.0, 460.0)]);
|
||||
on("upDownArrowCallout", 480.0, 300.0, &[], &[(240.0, 0.0), (0.0, 77.8), (480.0, 222.2), (240.0, 300.0)]);
|
||||
on("quadArrowCallout", 480.0, 300.0, &[], &[(0.0, 150.0), (124.5, 77.8), (240.0, 0.0), (480.0, 150.0), (240.0, 300.0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn curved_arrows_match_reference_points() {
|
||||
on(
|
||||
"curvedRightArrow",
|
||||
480.0,
|
||||
300.0,
|
||||
&[],
|
||||
&[(480.0, 0.0), (480.0, 75.0), (480.0, 225.0), (405.0, 149.0), (405.0, 299.0), (100.0, 151.5), (300.0, 6.7)],
|
||||
);
|
||||
on("curvedRightArrow", 200.0, 460.0, &[30000.0, 60000.0, 40000.0], &[(200.0, 400.0), (120.0, 324.5), (120.0, 443.5), (200.0, 60.0)]);
|
||||
// The band never outgrows the head; the head never outgrows half the frame.
|
||||
on("curvedRightArrow", 200.0, 460.0, &[60000.0, 30000.0, 80000.0], &[(200.0, 60.0)]);
|
||||
on("curvedLeftArrow", 480.0, 300.0, &[], &[(0.0, 225.0), (0.0, 0.0)]);
|
||||
on("curvedUpArrow", 480.0, 300.0, &[], &[(405.0, 0.0), (0.0, 0.0), (75.0, 0.0)]);
|
||||
on("curvedDownArrow", 480.0, 300.0, &[], &[(405.0, 300.0), (0.0, 300.0)]);
|
||||
let g = build("curvedRightArrow", 480.0, 300.0, &[]).unwrap_or_else(|| panic!("curvedRightArrow"));
|
||||
assert_eq!(g.paths[0].fill, FillMode::DarkenLess);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swoosh_matches_reference_points() {
|
||||
on("swooshArrow", 480.0, 300.0, &[], &[(0.0, 300.0), (425.8, 0.0), (480.0, 59.7), (442.8, 150.0), (202.0, 105.5), (202.0, 159.0)]);
|
||||
on("swooshArrow", 480.0, 300.0, &[40000.0, 16667.0], &[(480.0, 82.2), (447.8, 195.0)]);
|
||||
on("swooshArrow", 200.0, 460.0, &[], &[(163.9, 0.0), (200.0, 59.4)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ribbons_match_reference_points() {
|
||||
on(
|
||||
"ellipseRibbon",
|
||||
480.0,
|
||||
300.0,
|
||||
&[],
|
||||
&[(0.0, 0.0), (120.0, 28.1), (60.0, 128.9), (240.0, 300.0), (120.0, 66.1), (180.0, 72.2), (180.0, 35.2)],
|
||||
);
|
||||
on("ellipseRibbon", 480.0, 300.0, &[30000.0, 60000.0, 20000.0], &[(120.0, 45.0), (240.0, 300.0), (96.0, 68.4)]);
|
||||
on("ellipseRibbon", 480.0, 300.0, &[80000.0, 20000.0, 60000.0], &[(240.0, 240.0), (1.0, 61.75)]);
|
||||
on("ellipseRibbon2", 480.0, 300.0, &[], &[(0.0, 300.0), (240.0, 0.0)]);
|
||||
on(
|
||||
"leftRightRibbon",
|
||||
480.0,
|
||||
300.0,
|
||||
&[],
|
||||
&[(0.0, 125.0), (150.0, 0.0), (150.0, 50.0), (255.0, 62.5), (225.0, 87.5), (480.0, 175.0), (330.0, 300.0)],
|
||||
);
|
||||
on("leftRightRibbon", 480.0, 300.0, &[90000.0, 10000.0, 60000.0], &[(0.0, 100.0), (480.0, 200.0), (240.0, 300.0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gears_and_funnel_match_reference_points() {
|
||||
// Square gear6: body radius 105, top tooth tip 3.5 inside the frame, tip half-width ~16.3.
|
||||
on("gear6", 300.0, 300.0, &[], &[(150.0, 3.5), (133.7, 3.5), (166.3, 3.5), (45.0, 150.0)]);
|
||||
on("gear9", 300.0, 300.0, &[], &[(150.0, 1.0), (30.0, 150.0)]);
|
||||
on("gear6", 480.0, 300.0, &[], &[(240.0, 3.5), (201.0, 3.5)]);
|
||||
on("funnel", 480.0, 300.0, &[], &[(240.0, 0.0), (0.0, 75.0), (240.0, 15.0), (15.0, 75.0), (240.0, 300.0), (60.5, 150.0)]);
|
||||
let g = build("funnel", 480.0, 300.0, &[]).unwrap_or_else(|| panic!("funnel"));
|
||||
assert!(g.paths[0].even_odd);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn circular_arrows_match_reference_points() {
|
||||
on("circularArrow", 300.0, 300.0, &[], &[(19.0, 150.0), (56.5, 150.0), (150.0, 19.0), (262.4, 150.0), (218.6, 113.4), (293.6, 113.4)]);
|
||||
on(
|
||||
"circularArrow",
|
||||
300.0,
|
||||
300.0,
|
||||
&[20000.0, 1800000.0, 18000000.0, 5400000.0, 20000.0],
|
||||
&[(150.0, 270.0), (150.0, 210.0), (247.0, 42.1), (143.0, 102.1), (227.9, 105.0)],
|
||||
);
|
||||
on("leftCircularArrow", 300.0, 300.0, &[], &[(37.6, 150.0), (262.4, 150.0), (150.0, 281.0)]);
|
||||
on("leftRightCircularArrow", 300.0, 300.0, &[], &[(262.4, 150.0), (37.6, 150.0), (150.0, 19.0)]);
|
||||
// The band never grows past twice the head's half-width.
|
||||
on("circularArrow", 300.0, 300.0, &[25000.0, 1142319.0, 20457681.0, 10800000.0, 5000.0], &[(0.0, 150.0), (30.0, 150.0)]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,375 @@
|
||||
use super::builder::{Corner, Pb, corner_rect, fill, mid_sites, shade, simple, stroke_only, tip_handle};
|
||||
use super::catalog::{FillMode, Geometry};
|
||||
use super::{Point, Rect};
|
||||
|
||||
pub(super) fn wedge_rect(w: f64, h: f64, a0: f64, a1: f64, r: f64) -> Geometry {
|
||||
let tip = Point::new(w / 2.0 + w * a0 / 1e5, h / 2.0 + h * a1 / 1e5);
|
||||
let inside = tip.x > 0.0 && tip.x < w && tip.y > 0.0 && tip.y < h;
|
||||
let (dx, dy) = ((tip.x - w / 2.0) / w.max(1e-9), (tip.y - h / 2.0) / h.max(1e-9));
|
||||
let mut b = Pb::new();
|
||||
let r = r.min(w / 2.0).min(h / 2.0);
|
||||
// Walk clockwise from the top-left; insert the wedge on the edge facing the tip.
|
||||
let horiz = dx.abs() > dy.abs();
|
||||
// The wedge's base: 2..5 twelfths of the edge on the tip's side of the centre when it is
|
||||
// before it, 7..10 twelfths when after (presetShapeDefinitions' xg1/xg2, yg1/yg2).
|
||||
let (bx0, bx1) = if tip.x < w / 2.0 { (w * 2.0 / 12.0, w * 5.0 / 12.0) } else { (w * 7.0 / 12.0, w * 10.0 / 12.0) };
|
||||
let (by0, by1) = if tip.y < h / 2.0 { (h * 2.0 / 12.0, h * 5.0 / 12.0) } else { (h * 7.0 / 12.0, h * 10.0 / 12.0) };
|
||||
b.m(r, 0.0);
|
||||
if !inside && !horiz && dy < 0.0 {
|
||||
b.l(bx0, 0.0).l(tip.x, tip.y).l(bx1, 0.0);
|
||||
}
|
||||
b.l(w - r, 0.0);
|
||||
if r > 0.0 {
|
||||
b.arc(r, r, 270.0, 90.0);
|
||||
}
|
||||
if !inside && horiz && dx > 0.0 {
|
||||
b.l(w, by0).l(tip.x, tip.y).l(w, by1);
|
||||
}
|
||||
b.l(w, h - r);
|
||||
if r > 0.0 {
|
||||
b.arc(r, r, 0.0, 90.0);
|
||||
}
|
||||
if !inside && !horiz && dy > 0.0 {
|
||||
b.l(bx1, h).l(tip.x, tip.y).l(bx0, h);
|
||||
}
|
||||
b.l(r, h);
|
||||
if r > 0.0 {
|
||||
b.arc(r, r, 90.0, 90.0);
|
||||
}
|
||||
if !inside && horiz && dx < 0.0 {
|
||||
b.l(0.0, by1).l(tip.x, tip.y).l(0.0, by0);
|
||||
}
|
||||
b.l(0.0, r);
|
||||
if r > 0.0 {
|
||||
b.arc(r, r, 180.0, 90.0);
|
||||
}
|
||||
b.z();
|
||||
let mut g = simple(b.take(), Rect::new(0.0, 0.0, w, h).inset(-r * 0.29289), w, h);
|
||||
g.handles.push(tip_handle(w, h, tip));
|
||||
g
|
||||
}
|
||||
|
||||
/// ECMA-376 cloud outline arcs (radii in 43200ths of the box, angles in 60000ths of a degree,
|
||||
/// as DrawingML `arcTo` takes them), from the start point (3900, 14370).
|
||||
const CLOUD_ARCS: [(f64, f64, f64, f64); 11] = [
|
||||
(6753.0, 9190.0, -11429249.0, 7426832.0),
|
||||
(5333.0, 7267.0, -8646143.0, 5396714.0),
|
||||
(4365.0, 5945.0, -8748475.0, 5983381.0),
|
||||
(4857.0, 6595.0, -7859164.0, 7034504.0),
|
||||
(5333.0, 7273.0, -4722533.0, 6541615.0),
|
||||
(6775.0, 9220.0, -2776035.0, 7816140.0),
|
||||
(5785.0, 7867.0, 37501.0, 6842000.0),
|
||||
(6752.0, 9215.0, 1347096.0, 6910353.0),
|
||||
(7720.0, 10543.0, 3974558.0, 4542661.0),
|
||||
(4360.0, 5918.0, -16496525.0, 8804134.0),
|
||||
(4345.0, 5945.0, -14809710.0, 9151131.0),
|
||||
];
|
||||
|
||||
/// The cloud's creases: short arcs inside the outline, each from its own start point.
|
||||
const CLOUD_CREASES: [(f64, f64, f64, f64, f64, f64); 11] = [
|
||||
(4693.0, 26177.0, 4345.0, 5945.0, 5204520.0, 1585770.0),
|
||||
(6928.0, 34899.0, 4360.0, 5918.0, 4416628.0, 686848.0),
|
||||
(16478.0, 39090.0, 6752.0, 9215.0, 8257449.0, 844866.0),
|
||||
(28827.0, 34751.0, 6752.0, 9215.0, 387196.0, 959901.0),
|
||||
(34129.0, 22954.0, 5785.0, 7867.0, -4217541.0, 4255042.0),
|
||||
(41798.0, 15354.0, 5333.0, 7273.0, 1819082.0, 1665090.0),
|
||||
(38324.0, 5426.0, 4857.0, 6595.0, -824660.0, 891534.0),
|
||||
(29078.0, 3952.0, 4857.0, 6595.0, -8950887.0, 1091722.0),
|
||||
(22141.0, 4720.0, 4365.0, 5945.0, -9809656.0, 1061181.0),
|
||||
(14000.0, 5192.0, 6753.0, 9190.0, -4002417.0, 739161.0),
|
||||
(4127.0, 15789.0, 6753.0, 9190.0, 9459261.0, 711490.0),
|
||||
];
|
||||
|
||||
pub(super) fn cloud(name: &str, w: f64, h: f64, a0: f64, a1: f64) -> Geometry {
|
||||
// Drawn in the path's own 43200-unit square, then stretched to the box (as DrawingML lays out
|
||||
// a path with its own w and h), so the arcs keep their shape's proportions at any aspect.
|
||||
let to_box = kurbo::Affine::scale_non_uniform(w / 43200.0, h / 43200.0);
|
||||
let deg = |v: f64| v / 60000.0;
|
||||
let mut b = Pb::new();
|
||||
b.m(3900.0, 14370.0);
|
||||
for (wr, hr, st, sw) in CLOUD_ARCS {
|
||||
b.arc_spec(wr, hr, deg(st), deg(sw));
|
||||
}
|
||||
b.z();
|
||||
let mut creases = Pb::new();
|
||||
for (x, y, wr, hr, st, sw) in CLOUD_CREASES {
|
||||
creases.m(x, y).arc_spec(wr, hr, deg(st), deg(sw));
|
||||
}
|
||||
let mut outline = b.take();
|
||||
outline.apply_affine(to_box);
|
||||
let mut paths = vec![fill(outline)];
|
||||
if name == "cloud" {
|
||||
let mut c = creases.take();
|
||||
c.apply_affine(to_box);
|
||||
paths.push(stroke_only(c));
|
||||
}
|
||||
let text_rect = Rect::new(w * 2977.0 / 21600.0, h * 3262.0 / 21600.0, w * 17087.0 / 21600.0, h * 17337.0 / 21600.0);
|
||||
let mut handles = vec![];
|
||||
if name == "cloudCallout" {
|
||||
let tip = Point::new(w / 2.0 + w * a0 / 1e5, h / 2.0 + h * a1 / 1e5);
|
||||
let c = Point::new(w / 2.0, h / 2.0);
|
||||
let mut bubbles = Pb::new();
|
||||
for (k, r) in [(0.55, 0.06), (0.75, 0.045), (0.95, 0.03)] {
|
||||
let p = c + (tip - c) * k;
|
||||
if !(p.x > w * 0.1 && p.x < w * 0.9 && p.y > h * 0.1 && p.y < h * 0.9) || k > 0.9 {
|
||||
bubbles.ellipse(p.x, p.y, w * r, h * r);
|
||||
}
|
||||
}
|
||||
paths.push(fill(bubbles.take()));
|
||||
handles.push(tip_handle(w, h, tip));
|
||||
}
|
||||
Geometry { paths, text_rect, handles, sites: mid_sites(w, h) }
|
||||
}
|
||||
|
||||
/// The equation shapes, by ECMA-376's formulas (checked against PowerPoint's outlines at several
|
||||
/// sizes and adjust values). Their bars span 73.49% of the width; the plus and multiply bars are
|
||||
/// `adj1` of the shorter side thick, the others' `adj1` of the height.
|
||||
pub(super) fn math(name: &str, w: f64, h: f64, a0: f64, a1: f64, a2: f64) -> Geometry {
|
||||
let pin = |lo: f64, v: f64, hi: f64| v.max(lo).min(hi.max(lo));
|
||||
let ss = w.min(h);
|
||||
let (hc, vc) = (w / 2.0, h / 2.0);
|
||||
let dx1 = w * 73490.0 / 200000.0;
|
||||
let (x1, x2) = (hc - dx1, hc + dx1);
|
||||
let mut b = Pb::new();
|
||||
let text = match name {
|
||||
"mathPlus" => {
|
||||
let a = pin(0.0, a0, 73490.0);
|
||||
let dy1 = h * 73490.0 / 200000.0;
|
||||
let dx2 = ss * a / 200000.0;
|
||||
let (y1, y2, y3, y4) = (vc - dy1, vc - dx2, vc + dx2, vc + dy1);
|
||||
let (xa, xb) = (hc - dx2, hc + dx2);
|
||||
b.poly(&[(x1, y2), (xa, y2), (xa, y1), (xb, y1), (xb, y2), (x2, y2), (x2, y3), (xb, y3), (xb, y4), (xa, y4), (xa, y3), (x1, y3)]);
|
||||
Rect::new(x1, y2, x2, y3)
|
||||
}
|
||||
"mathMinus" => {
|
||||
let dy1 = h * pin(0.0, a0, 100000.0) / 200000.0;
|
||||
b.poly(&[(x1, vc - dy1), (x2, vc - dy1), (x2, vc + dy1), (x1, vc + dy1)]);
|
||||
Rect::new(x1, vc - dy1, x2, vc + dy1)
|
||||
}
|
||||
"mathEqual" => {
|
||||
let a = pin(0.0, a0, 36745.0);
|
||||
let g = pin(0.0, a1, 100000.0 - 2.0 * a);
|
||||
let (dy1, dy2) = (h * a / 100000.0, h * g / 200000.0);
|
||||
let (y2, y3) = (vc - dy2, vc + dy2);
|
||||
let (y1, y4) = (y2 - dy1, y3 + dy1);
|
||||
b.poly(&[(x1, y1), (x2, y1), (x2, y2), (x1, y2)]);
|
||||
b.poly(&[(x1, y3), (x2, y3), (x2, y4), (x1, y4)]);
|
||||
Rect::new(x1, y1, x2, y4)
|
||||
}
|
||||
"mathNotEqual" => {
|
||||
let a = pin(0.0, a0, 50000.0);
|
||||
let cr_ang = pin(4200000.0, a1, 6600000.0);
|
||||
let g = pin(0.0, a2, 100000.0 - 2.0 * a);
|
||||
let (dy1, dy2) = (h * a / 100000.0, h * g / 200000.0);
|
||||
let hd2 = (h / 2.0).max(1e-9);
|
||||
let (y2, y3) = (vc - dy2, vc + dy2);
|
||||
let (y1, y4) = (y2 - dy1, y3 + dy1);
|
||||
let cadj2 = cr_ang - 5400000.0;
|
||||
let xadj2 = hd2 * (cadj2 / 60000.0).to_radians().tan();
|
||||
let len = xadj2.hypot(hd2);
|
||||
let bhw = len * dy1 / hd2;
|
||||
let x7 = hc + xadj2 - bhw / 2.0;
|
||||
let at = |y: f64| x7 - xadj2 * y / hd2;
|
||||
let (x6, x5, x4, x3) = (at(y1), at(y2), at(y3), at(y4));
|
||||
let rx7 = x7 + bhw;
|
||||
let dx7 = dy1 * hd2 / len;
|
||||
let dy3 = dy1 * xadj2 / len;
|
||||
let (rx, lx, ry, ly) = if cadj2 > 0.0 { (x7 + dx7, x7, dy3, 0.0) } else { (rx7, rx7 - dx7, 0.0, -dy3) };
|
||||
b.poly(&[
|
||||
(x1, y1),
|
||||
(x6, y1),
|
||||
(lx, ly),
|
||||
(rx, ry),
|
||||
(x6 + bhw, y1),
|
||||
(x2, y1),
|
||||
(x2, y2),
|
||||
(x5 + bhw, y2),
|
||||
(x4 + bhw, y3),
|
||||
(x2, y3),
|
||||
(x2, y4),
|
||||
(x3 + bhw, y4),
|
||||
(w - lx, h - ly),
|
||||
(w - rx, h - ry),
|
||||
(x3, y4),
|
||||
(x1, y4),
|
||||
(x1, y3),
|
||||
(x4, y3),
|
||||
(x5, y2),
|
||||
(x1, y2),
|
||||
]);
|
||||
Rect::new(x1, y1, x2, y4)
|
||||
}
|
||||
"mathDivide" => {
|
||||
let a = pin(1000.0, a0, 36745.0);
|
||||
let max3 = ((73490.0 - a) / 4.0).min(36745.0 * w / h.max(1e-9));
|
||||
let r3 = pin(1000.0, a2, max3);
|
||||
let g = pin(0.0, a1, 73490.0 - 4.0 * r3 - a);
|
||||
let dy1 = h * a / 200000.0;
|
||||
let yg = h * g / 100000.0;
|
||||
let rad = h * r3 / 100000.0;
|
||||
let (y3, y4) = (vc - dy1, vc + dy1);
|
||||
let y2 = y3 - (yg + rad);
|
||||
let y1 = y2 - rad;
|
||||
b.ellipse(hc, y1 + rad, rad, rad);
|
||||
b.ellipse(hc, h - y1 - rad, rad, rad);
|
||||
b.poly(&[(x1, y3), (x2, y3), (x2, y4), (x1, y4)]);
|
||||
Rect::new(x1, y3, x2, y4)
|
||||
}
|
||||
_ => {
|
||||
// mathMultiply: an X whose arms run along the box's diagonals.
|
||||
let th = ss * pin(0.0, a0, 51965.0) / 100000.0;
|
||||
let ang = h.atan2(w);
|
||||
let (sa, ca, ta) = (ang.sin(), ang.cos(), ang.tan().max(1e-9));
|
||||
let lm = w.hypot(h) * (1.0 - 51965.0 / 100000.0);
|
||||
let (xm, ym) = (ca * lm / 2.0, sa * lm / 2.0);
|
||||
let (dxam, dyam) = (sa * th / 2.0, ca * th / 2.0);
|
||||
let (xa, ya, xb, yb) = (xm - dxam, ym + dyam, xm + dxam, ym - dyam);
|
||||
let yc = (hc - xb) * ta + yb;
|
||||
let (xd, xe) = (w - xb, w - xa);
|
||||
let xfe = (vc - ya) / ta;
|
||||
let (xf, xl) = (xe - xfe, xa + xfe);
|
||||
let (yg, yh, yi) = (h - ya, h - yb, h - yc);
|
||||
b.poly(&[(xa, ya), (xb, yb), (hc, yc), (xd, yb), (xe, ya), (xf, vc), (xe, yg), (xd, yh), (hc, yi), (xb, yh), (xa, yg), (xl, vc)]);
|
||||
Rect::new(xa, yb, xe, yh)
|
||||
}
|
||||
};
|
||||
Geometry { paths: vec![fill(b.take())], text_rect: text, handles: vec![], sites: mid_sites(w, h) }
|
||||
}
|
||||
|
||||
pub(super) fn ribbon(up: bool, w: f64, h: f64, a0: f64, a1: f64) -> Geometry {
|
||||
let fold = h * a0.clamp(0.0, 33333.0) / 1e5;
|
||||
let cw = w * a1.clamp(25000.0, 75000.0) / 1e5; // centre band width
|
||||
let x0 = (w - cw) / 2.0;
|
||||
let x1 = w - x0;
|
||||
let ear = w / 8.0;
|
||||
let (top, bot) = (0.0, h);
|
||||
let mut b = Pb::new();
|
||||
let mut shade_p = Pb::new();
|
||||
if up {
|
||||
// centre band sits high; ends hang low.
|
||||
b.poly(&[(x0, top), (x1, top), (x1, bot - fold), (x0, bot - fold)]);
|
||||
let mut ends = Pb::new();
|
||||
ends.poly(&[(0.0, fold), (x0 + ear * 0.5, fold), (x0 + ear * 0.5, bot), (0.0, bot), (ear, (fold + bot) / 2.0)]);
|
||||
ends.poly(&[(w, fold), (x1 - ear * 0.5, fold), (x1 - ear * 0.5, bot), (w, bot), (w - ear, (fold + bot) / 2.0)]);
|
||||
shade_p.poly(&[(x0, bot - fold), (x0 + ear * 0.5, bot - fold), (x0 + ear * 0.5, bot)]);
|
||||
shade_p.poly(&[(x1, bot - fold), (x1 - ear * 0.5, bot - fold), (x1 - ear * 0.5, bot)]);
|
||||
let text = Rect::new(x0, top, x1, bot - fold);
|
||||
return Geometry {
|
||||
paths: vec![fill(ends.take()), shade(shade_p.take(), FillMode::DarkenLess), fill(b.take())],
|
||||
text_rect: text,
|
||||
handles: vec![],
|
||||
sites: mid_sites(w, h),
|
||||
};
|
||||
}
|
||||
b.poly(&[(x0, top + fold), (x1, top + fold), (x1, bot), (x0, bot)]);
|
||||
let mut ends = Pb::new();
|
||||
ends.poly(&[(0.0, top), (x0 + ear * 0.5, top), (x0 + ear * 0.5, bot - fold), (0.0, bot - fold), (ear, (bot - fold) / 2.0)]);
|
||||
ends.poly(&[(w, top), (x1 - ear * 0.5, top), (x1 - ear * 0.5, bot - fold), (w, bot - fold), (w - ear, (bot - fold) / 2.0)]);
|
||||
shade_p.poly(&[(x0, top + fold), (x0 + ear * 0.5, top + fold), (x0 + ear * 0.5, top)]);
|
||||
shade_p.poly(&[(x1, top + fold), (x1 - ear * 0.5, top + fold), (x1 - ear * 0.5, top)]);
|
||||
let text = Rect::new(x0, top + fold, x1, bot);
|
||||
Geometry {
|
||||
paths: vec![fill(ends.take()), shade(shade_p.take(), FillMode::DarkenLess), fill(b.take())],
|
||||
text_rect: text,
|
||||
handles: vec![],
|
||||
sites: mid_sites(w, h),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn scroll(vertical: bool, w: f64, h: f64, a0: f64) -> Geometry {
|
||||
let ss = w.min(h);
|
||||
let r = ss * a0.clamp(0.0, 25000.0) / 1e5 / 2.0;
|
||||
let mut b = Pb::new();
|
||||
let mut curl = Pb::new();
|
||||
if vertical {
|
||||
b.poly(&[(r, r * 2.0), (w - r, r * 2.0), (w - r, h - r), (r, h - r)]);
|
||||
b.ellipse(w / 2.0, r, w / 2.0 - r * 0.0, r);
|
||||
curl.ellipse(r, h - r, r, r);
|
||||
curl.ellipse(w - r * 2.0, r, r * 0.5, r * 0.5);
|
||||
let text = Rect::new(r, r * 2.0, w - r, h - r);
|
||||
return Geometry {
|
||||
paths: vec![fill(b.take()), shade(curl.take(), FillMode::DarkenLess)],
|
||||
text_rect: text,
|
||||
handles: vec![],
|
||||
sites: mid_sites(w, h),
|
||||
};
|
||||
}
|
||||
b.poly(&[(r * 2.0, r), (w - r, r), (w - r, h - r), (r * 2.0, h - r)]);
|
||||
b.ellipse(r, h / 2.0, r, h / 2.0);
|
||||
curl.ellipse(w - r, r, r, r);
|
||||
curl.ellipse(r, h - r * 2.0, r * 0.5, r * 0.5);
|
||||
let text = Rect::new(r * 2.0, r, w - r, h - r);
|
||||
Geometry { paths: vec![fill(b.take()), shade(curl.take(), FillMode::DarkenLess)], text_rect: text, handles: vec![], sites: mid_sites(w, h) }
|
||||
}
|
||||
|
||||
pub(super) fn action_button(name: &str, w: f64, h: f64) -> Geometry {
|
||||
let body = corner_rect(w, h, [Corner::Square; 4]);
|
||||
let ss = w.min(h);
|
||||
let (cx, cy) = (w / 2.0, h / 2.0);
|
||||
let s = ss * 0.3;
|
||||
let mut g = Pb::new();
|
||||
match name {
|
||||
"actionButtonBackPrevious" => {
|
||||
g.poly(&[(cx + s, cy - s), (cx + s, cy + s), (cx - s, cy)]);
|
||||
}
|
||||
"actionButtonForwardNext" => {
|
||||
g.poly(&[(cx - s, cy - s), (cx - s, cy + s), (cx + s, cy)]);
|
||||
}
|
||||
"actionButtonBeginning" => {
|
||||
g.poly(&[(cx + s, cy - s), (cx + s, cy + s), (cx - s * 0.6, cy)]);
|
||||
g.poly(&[(cx - s, cy - s), (cx - s * 0.7, cy - s), (cx - s * 0.7, cy + s), (cx - s, cy + s)]);
|
||||
}
|
||||
"actionButtonEnd" => {
|
||||
g.poly(&[(cx - s, cy - s), (cx - s, cy + s), (cx + s * 0.6, cy)]);
|
||||
g.poly(&[(cx + s * 0.7, cy - s), (cx + s, cy - s), (cx + s, cy + s), (cx + s * 0.7, cy + s)]);
|
||||
}
|
||||
"actionButtonHome" => {
|
||||
g.poly(&[
|
||||
(cx, cy - s),
|
||||
(cx + s, cy),
|
||||
(cx + s * 0.7, cy),
|
||||
(cx + s * 0.7, cy + s),
|
||||
(cx - s * 0.7, cy + s),
|
||||
(cx - s * 0.7, cy),
|
||||
(cx - s, cy),
|
||||
]);
|
||||
}
|
||||
"actionButtonInformation" | "actionButtonHelp" => {
|
||||
g.ellipse(cx, cy, s, s);
|
||||
}
|
||||
"actionButtonReturn" => {
|
||||
g.m(cx + s, cy - s * 0.6).l(cx + s, cy + s * 0.2).q(cx + s, cy + s * 0.8, cx + s * 0.4, cy + s * 0.8).l(cx - s * 0.5, cy + s * 0.8);
|
||||
g.l(cx - s * 0.5, cy + s * 0.4)
|
||||
.l(cx - s, cy + s * 0.9)
|
||||
.l(cx - s * 0.5, cy + s * 1.4)
|
||||
.l(cx - s * 0.5, cy + s)
|
||||
.l(cx + s * 0.6, cy + s)
|
||||
.l(cx + s * 0.6, cy - s * 0.6)
|
||||
.z();
|
||||
}
|
||||
"actionButtonMovie" => {
|
||||
g.poly(&[(cx - s, cy - s * 0.6), (cx + s * 0.4, cy - s * 0.6), (cx + s * 0.4, cy + s * 0.6), (cx - s, cy + s * 0.6)]);
|
||||
g.poly(&[(cx + s * 0.4, cy), (cx + s, cy - s * 0.5), (cx + s, cy + s * 0.5)]);
|
||||
}
|
||||
"actionButtonDocument" => {
|
||||
g.poly(&[(cx - s * 0.7, cy - s), (cx + s * 0.3, cy - s), (cx + s * 0.7, cy - s * 0.6), (cx + s * 0.7, cy + s), (cx - s * 0.7, cy + s)]);
|
||||
}
|
||||
"actionButtonSound" => {
|
||||
g.poly(&[
|
||||
(cx - s, cy - s * 0.35),
|
||||
(cx - s * 0.5, cy - s * 0.35),
|
||||
(cx, cy - s),
|
||||
(cx, cy + s),
|
||||
(cx - s * 0.5, cy + s * 0.35),
|
||||
(cx - s, cy + s * 0.35),
|
||||
]);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Geometry {
|
||||
paths: vec![fill(body), shade(g.take(), FillMode::DarkenLess)],
|
||||
text_rect: Rect::new(0.0, 0.0, w, h),
|
||||
handles: vec![],
|
||||
sites: mid_sites(w, h),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,420 @@
|
||||
use super::*;
|
||||
use kurbo::Shape;
|
||||
|
||||
#[test]
|
||||
fn every_catalog_preset_builds_within_bounds() {
|
||||
for p in CATALOG {
|
||||
for (w, h) in [(200.0, 100.0), (100.0, 200.0), (50.0, 50.0), (0.0, 0.0), (1.0, 300.0)] {
|
||||
let g = build(p.name, w, h, &[]).unwrap_or_else(|| panic!("{} did not build", p.name));
|
||||
assert!(!g.paths.is_empty(), "{} has no paths", p.name);
|
||||
for s in &g.paths {
|
||||
let bb = s.path.bounding_box();
|
||||
if w > 0.0 && h > 0.0 && !p.name.contains("Connector") && !p.name.contains("allout") {
|
||||
let tol = w.max(h) * 0.35 + 1.0;
|
||||
assert!(bb.x0 >= -tol && bb.y0 >= -tol && bb.x1 <= w + tol && bb.y1 <= h + tol, "{} {w}x{h} out of bounds: {bb:?}", p.name);
|
||||
}
|
||||
assert!(bb.x0.is_finite() && bb.y1.is_finite(), "{} produced non-finite geometry", p.name);
|
||||
}
|
||||
assert!(g.text_rect.x0.is_finite() && g.text_rect.y1.is_finite(), "{}", p.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_adjust_values_never_panic() {
|
||||
for p in CATALOG {
|
||||
for v in [f64::NAN, f64::INFINITY, -1e12, 1e12, -1.0, 0.0] {
|
||||
let adj = vec![v; 6];
|
||||
let g = build(p.name, 120.0, 80.0, &adj);
|
||||
assert!(g.is_some(), "{}", p.name);
|
||||
}
|
||||
}
|
||||
assert!(build("rect", f64::NAN, 10.0, &[]).is_none());
|
||||
assert!(build("noSuchShape", 10.0, 10.0, &[]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn catalog_names_are_unique() {
|
||||
let mut names: Vec<&str> = CATALOG.iter().map(|p| p.name).collect();
|
||||
names.sort_unstable();
|
||||
let n = names.len();
|
||||
names.dedup();
|
||||
assert_eq!(n, names.len());
|
||||
assert!(n >= 140, "catalog has {n} presets");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_rect_handle_maps_back() {
|
||||
let g = build("roundRect", 200.0, 100.0, &[16667.0]).unwrap();
|
||||
let h = g.handles.first().unwrap();
|
||||
assert!((h.value_at(h.pos) - 16667.0).abs() <= 1.0);
|
||||
assert_eq!(h.value_at(Point::new(-50.0, 0.0)), 0.0);
|
||||
assert_eq!(h.value_at(Point::new(500.0, 0.0)), 50000.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn partial_circle_angle_handles_map_back() {
|
||||
// Start 0° sits at the right of the ellipse, end 270° at its top.
|
||||
for name in ["pie", "chord", "arc"] {
|
||||
let g = build(name, 200.0, 100.0, &[0.0, 16200000.0]).unwrap();
|
||||
assert_eq!(g.handles.iter().map(|h| h.index).collect::<Vec<_>>(), vec![0, 1], "{name}");
|
||||
let (s, e) = (&g.handles[0], &g.handles[1]);
|
||||
assert!((s.pos - Point::new(200.0, 50.0)).hypot() < 1e-6, "{name} {:?}", s.pos);
|
||||
assert!((e.pos - Point::new(100.0, 0.0)).hypot() < 1e-6, "{name} {:?}", e.pos);
|
||||
assert_eq!(s.value_at(s.pos), 0.0, "{name}");
|
||||
assert_eq!(e.value_at(e.pos), 16200000.0, "{name}");
|
||||
// Dragging to the bottom of the ellipse gives 90°.
|
||||
assert_eq!(s.value_at(Point::new(100.0, 100.0)), 5400000.0, "{name}");
|
||||
// Off-axis angles on a non-square ellipse: the visual 45° ray meets the 200×100
|
||||
// ellipse at offset (√2000, √2000) from the centre.
|
||||
let k = 2000f64.sqrt();
|
||||
let g = build(name, 200.0, 100.0, &[2700000.0, 8100000.0]).unwrap();
|
||||
let (s, e) = (&g.handles[0], &g.handles[1]);
|
||||
assert!((s.pos - Point::new(100.0 + k, 50.0 + k)).hypot() < 1e-6, "{name} {:?}", s.pos);
|
||||
assert!((e.pos - Point::new(100.0 - k, 50.0 + k)).hypot() < 1e-6, "{name} {:?}", e.pos);
|
||||
for (deg, h) in [(45.0, s), (135.0, e)] {
|
||||
let on = ((h.pos.x - 100.0) / 100.0).powi(2) + ((h.pos.y - 50.0) / 50.0).powi(2);
|
||||
assert!((on - 1.0).abs() < 1e-9, "{name} {deg}° off ellipse: {:?}", h.pos);
|
||||
assert!((h.value_at(h.pos) - deg * 60000.0).abs() <= 1.0, "{name} {deg}°: {}", h.value_at(h.pos));
|
||||
}
|
||||
}
|
||||
for deg in [45.0, 135.0, 200.0, 315.0] {
|
||||
let g = build("blockArc", 200.0, 100.0, &[deg * 60000.0, 0.0, 25000.0]).unwrap();
|
||||
let start = g.handles.iter().find(|h| h.index == 0).unwrap();
|
||||
// Thickness 25 → the handle runs on the 87.5×37.5 mid-ring ellipse.
|
||||
let on = ((start.pos.x - 100.0) / 87.5).powi(2) + ((start.pos.y - 50.0) / 37.5).powi(2);
|
||||
assert!((on - 1.0).abs() < 1e-9, "blockArc {deg}° off ring: {:?}", start.pos);
|
||||
let d = start.pos - Point::new(100.0, 50.0);
|
||||
assert!((d.y.atan2(d.x).to_degrees().rem_euclid(360.0) - deg).abs() < 1e-6, "blockArc {deg}°: {:?}", start.pos);
|
||||
assert!((start.value_at(start.pos) - deg * 60000.0).abs() <= 1.0, "blockArc {deg}°");
|
||||
}
|
||||
let g = build("blockArc", 200.0, 100.0, &[10800000.0, 0.0, 25000.0]).unwrap();
|
||||
let start = g.handles.iter().find(|h| h.index == 0).unwrap();
|
||||
assert_eq!(start.value_at(start.pos), 10800000.0);
|
||||
let thick = g.handles.iter().find(|h| h.index == 2).unwrap();
|
||||
assert!((thick.value_at(thick.pos) - 25000.0).abs() <= 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn presets_with_adjust_values_expose_handles() {
|
||||
// Presets whose adjust values have no handle of their own yet.
|
||||
const NO_HANDLES: &[&str] = &[
|
||||
"curvedConnector3",
|
||||
"snip2SameRect",
|
||||
"snip2DiagRect",
|
||||
"snipRoundRect",
|
||||
"round1Rect",
|
||||
"round2SameRect",
|
||||
"round2DiagRect",
|
||||
"teardrop",
|
||||
"halfFrame",
|
||||
"corner",
|
||||
"diagStripe",
|
||||
"foldedCorner",
|
||||
"smileyFace",
|
||||
"moon",
|
||||
"gear6",
|
||||
"gear9",
|
||||
"bracketPair",
|
||||
"bracePair",
|
||||
"leftBracket",
|
||||
"rightBracket",
|
||||
"leftBrace",
|
||||
"rightBrace",
|
||||
"leftRightArrow",
|
||||
"upDownArrow",
|
||||
"quadArrow",
|
||||
"leftRightUpArrow",
|
||||
"bentArrow",
|
||||
"uturnArrow",
|
||||
"curvedRightArrow",
|
||||
"curvedLeftArrow",
|
||||
"curvedUpArrow",
|
||||
"curvedDownArrow",
|
||||
"leftUpArrow",
|
||||
"bentUpArrow",
|
||||
"circularArrow",
|
||||
"leftCircularArrow",
|
||||
"leftRightCircularArrow",
|
||||
"swooshArrow",
|
||||
"rightArrowCallout",
|
||||
"downArrowCallout",
|
||||
"leftArrowCallout",
|
||||
"upArrowCallout",
|
||||
"leftRightArrowCallout",
|
||||
"upDownArrowCallout",
|
||||
"quadArrowCallout",
|
||||
"mathPlus",
|
||||
"mathMinus",
|
||||
"mathMultiply",
|
||||
"mathDivide",
|
||||
"mathEqual",
|
||||
"mathNotEqual",
|
||||
"ribbon2",
|
||||
"ribbon",
|
||||
"ellipseRibbon2",
|
||||
"ellipseRibbon",
|
||||
"leftRightRibbon",
|
||||
"verticalScroll",
|
||||
"horizontalScroll",
|
||||
"wave",
|
||||
"doubleWave",
|
||||
"borderCallout1",
|
||||
"borderCallout2",
|
||||
"borderCallout3",
|
||||
"accentCallout1",
|
||||
"accentCallout2",
|
||||
"accentCallout3",
|
||||
"callout1",
|
||||
"callout2",
|
||||
"callout3",
|
||||
"accentBorderCallout1",
|
||||
"accentBorderCallout2",
|
||||
"accentBorderCallout3",
|
||||
];
|
||||
for p in CATALOG {
|
||||
if p.defaults.is_empty() || NO_HANDLES.contains(&p.name) {
|
||||
continue;
|
||||
}
|
||||
let g = build(p.name, 200.0, 100.0, &[]).unwrap();
|
||||
assert!(!g.handles.is_empty(), "{} has adjust values but no handles", p.name);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rect_area_and_ellipse_area() {
|
||||
let g = build("rect", 100.0, 50.0, &[]).unwrap();
|
||||
assert!((g.paths[0].path.area().abs() - 5000.0).abs() < 1e-6);
|
||||
let e = build("ellipse", 100.0, 50.0, &[]).unwrap();
|
||||
let expect = std::f64::consts::PI * 50.0 * 25.0;
|
||||
assert!((e.paths[0].path.area().abs() - expect).abs() / expect < 0.01);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lines_are_open() {
|
||||
assert!(build("line", 10.0, 10.0, &[]).unwrap().is_open());
|
||||
assert!(!build("rect", 10.0, 10.0, &[]).unwrap().is_open());
|
||||
}
|
||||
|
||||
fn on_path(g: &Geometry, p: Point, tol: f64) -> bool {
|
||||
use kurbo::ParamCurveNearest;
|
||||
g.paths.iter().any(|sp| sp.path.segments().any(|seg| seg.nearest(p, 1e-6).distance_sq <= tol * tol))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixed_shapes_follow_the_standards_coordinates() {
|
||||
// Points of ECMA-376's definitions (in 21600ths), checked on a 216 × 108 box.
|
||||
let (w, h) = (216.0, 108.0);
|
||||
let at = |x: f64, y: f64| Point::new(w * x / 21600.0, h * y / 21600.0);
|
||||
let g = build("lightningBolt", w, h, &[]).unwrap();
|
||||
for (x, y) in [(8472.0, 0.0), (16577.0, 12007.0), (21600.0, 21600.0), (0.0, 3890.0)] {
|
||||
assert!(on_path(&g, at(x, y), 1e-6), "lightningBolt ({x}, {y})");
|
||||
}
|
||||
let g = build("irregularSeal1", w, h, &[]).unwrap();
|
||||
assert!(on_path(&g, at(14522.0, 0.0), 1e-6) && on_path(&g, at(8485.0, 21600.0), 1e-6));
|
||||
let g = build("irregularSeal2", w, h, &[]).unwrap();
|
||||
assert!(on_path(&g, at(14790.0, 0.0), 1e-6) && on_path(&g, at(4917.0, 21600.0), 1e-6));
|
||||
// The heart's point is the middle of the bottom edge, its notch a quarter down.
|
||||
let g = build("heart", w, h, &[]).unwrap();
|
||||
assert!(on_path(&g, Point::new(w / 2.0, h), 1e-6) && on_path(&g, Point::new(w / 2.0, h / 4.0), 1e-6));
|
||||
let g = build("flowChartDocument", w, h, &[]).unwrap();
|
||||
assert!(on_path(&g, at(21600.0, 17322.0), 1e-6) && on_path(&g, at(0.0, 20172.0), 1e-6));
|
||||
// Punched tape: the top edge dips to a fifth of the height a quarter of the way along.
|
||||
let g = build("flowChartPunchedTape", w, h, &[]).unwrap();
|
||||
assert!(on_path(&g, Point::new(w / 4.0, h / 5.0), 1e-3) && on_path(&g, Point::new(w * 0.75, 0.0), 1e-3));
|
||||
let g = build("flowChartMultidocument", w, h, &[]).unwrap();
|
||||
assert!(on_path(&g, at(2972.0, 0.0), 1e-6) && on_path(&g, at(18595.0, 18022.0), 1e-6));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn notched_arrow_notch_parallels_the_head() {
|
||||
// presetShapeDefinitions: the notch's point is at x1 = dy1 · dx2 / hd2, so its edges run
|
||||
// parallel to the head's (25 on a 200 × 100 arrow at the default adjusts).
|
||||
for (w, h, adj, x1) in [(200.0, 100.0, [50000.0, 50000.0], 25.0), (300.0, 120.0, [80000.0, 30000.0], 28.8)] {
|
||||
let g = build("notchedRightArrow", w, h, &adj).unwrap();
|
||||
let notch = g.paths[0].path.elements().iter().find_map(|e| match e {
|
||||
kurbo::PathEl::MoveTo(p) | kurbo::PathEl::LineTo(p) if (p.y - h / 2.0).abs() < 1e-9 && p.x < w / 2.0 => Some(*p),
|
||||
_ => None,
|
||||
});
|
||||
assert!(notch.is_some_and(|p| (p.x - x1).abs() < 1e-6), "{w}x{h} {adj:?}: notch at {notch:?}, want x = {x1}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn equation_shapes_match_powerpoints_outlines() {
|
||||
// Corners read off PowerPoint's own outlines of the presets at their default adjusts.
|
||||
let g = build("mathMultiply", 216.0, 216.0, &[]).unwrap();
|
||||
for (x, y) in [(0.32333, 0.15702), (0.5, 0.33369), (0.84298, 0.32333), (0.66631, 0.5), (0.33369, 0.5)] {
|
||||
assert!(on_path(&g, Point::new(216.0 * x, 216.0 * y), 0.01), "multiply ({x}, {y})");
|
||||
}
|
||||
let g = build("mathEqual", 300.0, 120.0, &[]).unwrap();
|
||||
for (x, y) in [(0.13255, 0.5588), (0.86745, 0.794), (0.13255, 0.206), (0.86745, 0.4412)] {
|
||||
assert!(on_path(&g, Point::new(300.0 * x, 120.0 * y), 0.01), "equal ({x}, {y})");
|
||||
}
|
||||
let g = build("mathPlus", 216.0, 216.0, &[]).unwrap();
|
||||
assert!(on_path(&g, Point::new(216.0 * 0.3824, 216.0 * 0.13255), 0.01) && on_path(&g, Point::new(216.0 * 0.86745, 216.0 * 0.6176), 0.01));
|
||||
// A tall "not equal": its slash, leaning at the default 110° from top to bottom, reaches
|
||||
// past the box's sides exactly as far as PowerPoint's (-22.242 to 122.242).
|
||||
let g = build("mathNotEqual", 100.0, 260.0, &[]).unwrap();
|
||||
use kurbo::Shape as _;
|
||||
let bb = g.paths[0].path.bounding_box();
|
||||
assert!((bb.x0 + 22.242).abs() < 0.01 && (bb.x1 - 122.242).abs() < 0.01 && bb.y0.abs() < 1e-6 && (bb.y1 - 260.0).abs() < 1e-6, "{bb:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sun_and_waves_match_powerpoints_outlines() {
|
||||
// Points read off PowerPoint's outlines of the presets (216 pt square, default adjusts).
|
||||
let k = 216.0;
|
||||
let g = build("sun", k, k, &[]).unwrap();
|
||||
for (x, y) in [(0.85352, 0.14644), (0.76161, 0.33987), (0.66013, 0.23839), (0.42825, 0.79823), (0.20177, 0.57175), (0.75, 0.5), (0.5, 0.25)] {
|
||||
assert!(on_path(&g, Point::new(k * x, k * y), 0.02), "sun ({x}, {y})");
|
||||
}
|
||||
let g = build("wave", k, k, &[]).unwrap();
|
||||
for (x, y) in [(0.21875, 0.00484), (1.0, 0.125), (1.0, 0.875), (0.0, 0.875)] {
|
||||
assert!(on_path(&g, Point::new(k * x, k * y), 0.05), "wave ({x}, {y})");
|
||||
}
|
||||
// Shifted: a wave of 300 × 120 with adj (20000, 8000) starts 0.16 of the width in.
|
||||
let g = build("wave", 300.0, 120.0, &[20000.0, 8000.0]).unwrap();
|
||||
for (x, y) in [(0.18375, 0.00774), (0.84, 0.2), (1.0, 0.8), (0.16, 0.8)] {
|
||||
assert!(on_path(&g, Point::new(300.0 * x, 120.0 * y), 0.05), "shifted wave ({x}, {y})");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn star_text_rectangles_follow_the_standard() {
|
||||
// star5 at its default 19098 in a 144 pt square: the inner pentagon, about 0.38 of the
|
||||
// width (PowerPoint wraps "Shape 3" at 18 pt there), from y ≈ 55 to ≈ 110.
|
||||
let g = build("star5", 144.0, 144.0, &[]).unwrap();
|
||||
let t = g.text_rect;
|
||||
assert!((t.width() - 55.0).abs() < 1.0, "{t:?}");
|
||||
assert!((t.y0 - 55.0).abs() < 1.0 && (t.y1 - 110.0).abs() < 1.0, "{t:?}");
|
||||
// star4 at 12500: the square through its inner points' 45° corners.
|
||||
let g = build("star4", 144.0, 144.0, &[]).unwrap();
|
||||
assert!((g.text_rect.width() - 72.0 * 0.25 * 2f64.sqrt()).abs() < 0.5, "{:?}", g.text_rect);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stars_are_stretched_like_the_standards() {
|
||||
// star5 in a 216 pt square: top point at the top middle, side points at 18 degrees on an
|
||||
// ellipse 5.146% wider and 10.557% taller, centred 10.557% below the middle; its bottom
|
||||
// points reach the box's bottom edge.
|
||||
let k = 216.0;
|
||||
let g = build("star5", k, k, &[]).unwrap();
|
||||
let (rx, ry, cy) = (k / 2.0 * 1.05146, k / 2.0 * 1.10557, k / 2.0 * 1.10557);
|
||||
let a = 18f64.to_radians();
|
||||
assert!(on_path(&g, Point::new(k / 2.0, 0.0), 1e-6));
|
||||
assert!(on_path(&g, Point::new(k / 2.0 + rx * a.cos(), cy - ry * a.sin()), 1e-6));
|
||||
use kurbo::Shape as _;
|
||||
let bb = g.paths[0].path.bounding_box();
|
||||
assert!((bb.x0).abs() < 0.5 && (bb.x1 - k).abs() < 0.5 && (bb.y1 - k).abs() < 0.5, "{bb:?}");
|
||||
// star6 is 15.47% wider than its box's half-width allows (its points touch the sides).
|
||||
let g = build("star6", k, k, &[]).unwrap();
|
||||
let bb = g.paths[0].path.bounding_box();
|
||||
assert!((bb.x0).abs() < 0.5 && (bb.x1 - k).abs() < 0.5, "{bb:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn moon_disk_and_drum_match_powerpoints_outlines() {
|
||||
let k = 216.0;
|
||||
// Moon, default: the inner edge runs through (0.5, 0.5) and (0.625, 0.2266) of the box.
|
||||
let g = build("moon", k, k, &[]).unwrap();
|
||||
for (x, y) in [(0.5, 0.5), (0.626, 0.2266), (0.0, 0.5), (1.0, 0.0)] {
|
||||
assert!(on_path(&g, Point::new(k * x, k * y), 0.5), "moon ({x}, {y})");
|
||||
}
|
||||
// 300 × 120 at 30%: its inner edge reaches 30% of the width in.
|
||||
let g = build("moon", 300.0, 120.0, &[30000.0]).unwrap();
|
||||
assert!(on_path(&g, Point::new(90.0, 60.0), 0.5));
|
||||
// Magnetic disk ends are ellipses h/6 high, drum ends w/6 across.
|
||||
let g = build("flowChartMagneticDisk", k, k, &[]).unwrap();
|
||||
assert!(on_path(&g, Point::new(k, k / 6.0), 0.01) && on_path(&g, Point::new(k / 2.0, k / 3.0), 0.5));
|
||||
let g = build("flowChartMagneticDrum", k, k, &[]).unwrap();
|
||||
assert!(on_path(&g, Point::new(k / 6.0, 0.0), 0.01) && on_path(&g, Point::new(0.0, k / 2.0), 0.01));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_cloud_fills_its_box_at_any_aspect_and_has_creases() {
|
||||
use kurbo::Shape as _;
|
||||
for (w, h) in [(200.0, 200.0), (300.0, 100.0), (60.0, 180.0)] {
|
||||
let g = build("cloud", w, h, &[]).unwrap();
|
||||
let bb = g.paths[0].path.bounding_box();
|
||||
assert!(bb.x0.abs() < w * 0.02 && (bb.x1 - w).abs() < w * 0.02, "{w}×{h}: {bb:?}");
|
||||
assert!(bb.y0.abs() < h * 0.02 && (bb.y1 - h).abs() < h * 0.02, "{w}×{h}: {bb:?}");
|
||||
assert_eq!(g.paths.len(), 2, "outline and creases");
|
||||
assert!(g.paths[1].stroke && g.paths[1].fill == FillMode::None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spec_arcs_take_angles_as_directions_from_the_centre() {
|
||||
// A DrawingML arc of a 2:1 ellipse from 45° to 90°: 45° is the direction (1, 1) from the
|
||||
// centre, which on that ellipse is the point (r/√5)·(2, 1)… scaled so it lies on it.
|
||||
let mut b = Pb::new();
|
||||
b.m(0.0, 0.0).arc_spec(200.0, 100.0, 45.0, 45.0);
|
||||
let p = b.take();
|
||||
let end = p.elements().last().and_then(|e| match e {
|
||||
kurbo::PathEl::CurveTo(_, _, q) => Some(*q),
|
||||
_ => None,
|
||||
});
|
||||
// Centre: the start sits at direction 45°, i.e. at (k, k) from it with (k/200)² + (k/100)² = 1.
|
||||
let k = 1.0 / (1.0f64 / 40000.0 + 1.0 / 10000.0).sqrt();
|
||||
let c = Point::new(-k, -k);
|
||||
let q = end.unwrap();
|
||||
assert!((q.x - c.x).abs() < 1e-6 && (q.y - (c.y + 100.0)).abs() < 1e-6, "{q:?} vs centre {c:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn brackets_and_braces_fill_out_to_their_straight_side() {
|
||||
// PowerPoint fills a bracket or brace (when it has a fill) between its curve and the
|
||||
// straight side it opens to; only the curve is outlined.
|
||||
for name in ["leftBracket", "rightBracket", "leftBrace", "rightBrace"] {
|
||||
let g = build(name, 40.0, 100.0, &[]).unwrap();
|
||||
let filled: Vec<_> = g.paths.iter().filter(|p| p.fill != FillMode::None).collect();
|
||||
assert_eq!(filled.len(), 1, "{name}");
|
||||
assert!(!filled[0].stroke, "{name}: the filled area is not outlined");
|
||||
let area = filled[0].path.area().abs();
|
||||
assert!(area > 40.0 * 100.0 * 0.4 && area < 40.0 * 100.0, "{name}: {area}");
|
||||
assert!(g.paths.iter().any(|p| p.stroke && p.fill == FillMode::None), "{name}: the curve is outlined");
|
||||
}
|
||||
}
|
||||
|
||||
/// The end points of a path's move/line elements, in order.
|
||||
fn vertices(p: &BezPath) -> Vec<Point> {
|
||||
p.elements()
|
||||
.iter()
|
||||
.filter_map(|e| match e {
|
||||
kurbo::PathEl::MoveTo(q) | kurbo::PathEl::LineTo(q) => Some(*q),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wedge_callout_mirrors_with_its_tip() {
|
||||
// A tip below and right of the centre draws the mirror image of the same tip on the left;
|
||||
// the same for a tip right of the centre, above or below it. presetShapeDefinitions puts
|
||||
// the wedge's base at 2..5 twelfths of the edge on the near side of the centre and at
|
||||
// 7..10 twelfths on the far side.
|
||||
let (w, h) = (240.0, 120.0);
|
||||
// Sorted and deduplicated: the outline closes on the corner it starts from, which the
|
||||
// mirror moves to the opposite corner.
|
||||
let sorted = |mut v: Vec<Point>| {
|
||||
v.sort_by(|a, b| a.x.total_cmp(&b.x).then(a.y.total_cmp(&b.y)));
|
||||
v.dedup_by(|a, b| (*a - *b).hypot() < 1e-9);
|
||||
v
|
||||
};
|
||||
for (right, left, flip_x) in [([10000.0, 80000.0], [-10000.0, 80000.0], true), ([80000.0, 10000.0], [80000.0, -10000.0], false)] {
|
||||
let a = vertices(&build("wedgeRectCallout", w, h, &right).unwrap().paths[0].path);
|
||||
let b = vertices(&build("wedgeRectCallout", w, h, &left).unwrap().paths[0].path);
|
||||
let b: Vec<Point> = b.iter().map(|p| if flip_x { Point::new(w - p.x, p.y) } else { Point::new(p.x, h - p.y) }).collect();
|
||||
let (a, b) = (sorted(a), sorted(b));
|
||||
assert_eq!(a.len(), b.len(), "{right:?}: {a:?} vs mirrored {left:?}: {b:?}");
|
||||
for (p, q) in a.iter().zip(&b) {
|
||||
assert!((*p - *q).hypot() < 1e-6, "{right:?}: {a:?}\n is not the mirror of {left:?}: {b:?}");
|
||||
}
|
||||
}
|
||||
// The bottom wedge of a tip right of the centre leaves the edge at 7/12 and 10/12 of the width.
|
||||
let a = vertices(&build("wedgeRectCallout", w, h, &[10000.0, 80000.0]).unwrap().paths[0].path);
|
||||
for x in [w * 7.0 / 12.0, w * 10.0 / 12.0] {
|
||||
assert!(a.iter().any(|p| (p.x - x).abs() < 1e-6 && (p.y - h).abs() < 1e-6), "no wedge corner at ({x}, {h}): {a:?}");
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user