mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-20 17:33:20 +01:00
Merge current main into library folder safety work
This commit is contained in:
commit
5fa4d0eba6
246 files changed
+9521
-6376
No files matched your search
@@ -23,6 +23,10 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 5
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- name: Git dependencies come from storytold
|
||||
run: |
|
||||
|
||||
@@ -28,6 +28,10 @@ jobs:
|
||||
runs-on: windows-latest
|
||||
timeout-minutes: 10
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- name: Install WiX
|
||||
shell: pwsh
|
||||
@@ -43,6 +47,10 @@ jobs:
|
||||
name: Windows installation policy (no app build)
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
# A nightly dry run on main: every release build except macOS, nothing signed or released
|
||||
# (craft-common-actions release.yml `dry-run`). Finds release-only breakage before release day.
|
||||
schedule:
|
||||
- cron: "4 2 * * *"
|
||||
push:
|
||||
branches: [release]
|
||||
workflow_dispatch:
|
||||
@@ -12,21 +16,31 @@ on:
|
||||
type: string
|
||||
default: ""
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
packages: write
|
||||
# Nothing by default; each job names what it needs. Only `release` writes (the draft GitHub Release
|
||||
# and its packages).
|
||||
permissions: {}
|
||||
|
||||
concurrency:
|
||||
group: release-${{ github.ref }}
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
# Regenerates contributors/contributors.json for the release commit; every build bundles it.
|
||||
credits:
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: read
|
||||
uses: storytold/craft-common-actions/.github/workflows/refresh-contributors.yml@main
|
||||
git-sources:
|
||||
name: Git dependencies come from storytold
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- name: Git dependencies come from storytold
|
||||
run: |
|
||||
@@ -61,15 +75,20 @@ jobs:
|
||||
PY
|
||||
|
||||
release:
|
||||
needs: git-sources
|
||||
needs: [git-sources, credits]
|
||||
permissions:
|
||||
contents: write
|
||||
packages: write
|
||||
# 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: lightcraft
|
||||
app-title: LightCraft
|
||||
craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83
|
||||
web-craft-fonts: true
|
||||
version: ${{ inputs.version }}
|
||||
contributors-artifact: ${{ needs.credits.outputs.artifact-name }}
|
||||
secrets:
|
||||
APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
|
||||
APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }}
|
||||
|
||||
Generated
+69
-57
@@ -1199,6 +1199,23 @@ name = "craft-fonts-build"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
|
||||
|
||||
[[package]]
|
||||
name = "craft-gainmap"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=430a908f68819bf23d14fe73ab9b18d8f4fd4e5d#430a908f68819bf23d14fe73ab9b18d8f4fd4e5d"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-heif"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=430a908f68819bf23d14fe73ab9b18d8f4fd4e5d#430a908f68819bf23d14fe73ab9b18d8f4fd4e5d"
|
||||
dependencies = [
|
||||
"heic-rs",
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-i18n"
|
||||
version = "0.1.0"
|
||||
@@ -1222,6 +1239,15 @@ dependencies = [
|
||||
"writeable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-tiff"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=430a908f68819bf23d14fe73ab9b18d8f4fd4e5d#430a908f68819bf23d14fe73ab9b18d8f4fd4e5d"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ui"
|
||||
version = "0.1.0"
|
||||
@@ -1246,7 +1272,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-update"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a231e517c3b371e1bf0ee8af71ed807424a9a05d#a231e517c3b371e1bf0ee8af71ed807424a9a05d"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dunce",
|
||||
@@ -1256,6 +1282,7 @@ dependencies = [
|
||||
"image",
|
||||
"log",
|
||||
"plist",
|
||||
"rustix 1.1.5",
|
||||
"semver",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -3587,7 +3614,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-update",
|
||||
@@ -3616,7 +3643,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-catalog"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"crc32fast",
|
||||
@@ -3637,7 +3664,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-cli"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"dhat",
|
||||
"lightcraft-codecs",
|
||||
@@ -3654,10 +3681,12 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-codecs"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"avif-serialize",
|
||||
"bytemuck",
|
||||
"craft-gainmap",
|
||||
"craft-heif",
|
||||
"dhat",
|
||||
"image",
|
||||
"image-webp",
|
||||
@@ -3667,7 +3696,6 @@ dependencies = [
|
||||
"jxl-oxide",
|
||||
"lightcraft-color",
|
||||
"lightcraft-geom",
|
||||
"lightcraft-heif",
|
||||
"lightcraft-raster",
|
||||
"lightcraft-testkit",
|
||||
"moxcms 0.9.1",
|
||||
@@ -3685,7 +3713,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-color"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-geom",
|
||||
"proptest",
|
||||
@@ -3694,7 +3722,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-denoise"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"faer-core",
|
||||
"lightcraft-denoise-core",
|
||||
@@ -3711,7 +3739,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-denoise-core"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"dhat",
|
||||
"lightcraft-raster",
|
||||
@@ -3727,7 +3755,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-develop"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-color",
|
||||
"lightcraft-geom",
|
||||
@@ -3739,11 +3767,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-engine"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"ab_glyph",
|
||||
"chrono",
|
||||
"craft-docpath",
|
||||
"craft-heif",
|
||||
"craft-tiff",
|
||||
"crc32fast",
|
||||
"fs4",
|
||||
"harfrust 0.13.3",
|
||||
"jxl-oxide",
|
||||
@@ -3759,7 +3790,6 @@ dependencies = [
|
||||
"lightcraft-geocode",
|
||||
"lightcraft-geom",
|
||||
"lightcraft-gpu",
|
||||
"lightcraft-heif",
|
||||
"lightcraft-lensdb",
|
||||
"lightcraft-merge",
|
||||
"lightcraft-meta",
|
||||
@@ -3771,7 +3801,6 @@ dependencies = [
|
||||
"lightcraft-segment",
|
||||
"lightcraft-slideshow",
|
||||
"lightcraft-testkit",
|
||||
"lightcraft-tiff",
|
||||
"log",
|
||||
"miniz_oxide 0.9.1",
|
||||
"rayon",
|
||||
@@ -3785,7 +3814,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-faces"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-testkit",
|
||||
"matrixmultiply",
|
||||
@@ -3800,7 +3829,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-fetch"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"log",
|
||||
"rustls",
|
||||
@@ -3811,14 +3840,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-film"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"libm",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-geocode"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"miniz_oxide 0.9.1",
|
||||
"serde",
|
||||
@@ -3826,7 +3855,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-geom"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
@@ -3834,7 +3863,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-gpu"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"lightcraft-color",
|
||||
@@ -3852,24 +3881,16 @@ dependencies = [
|
||||
"wgpu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-heif"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"heic-rs",
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-inpaint"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-lensdb"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-develop",
|
||||
"lightcraft-geom",
|
||||
@@ -3878,7 +3899,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-mcp"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-codecs",
|
||||
"lightcraft-engine",
|
||||
@@ -3889,7 +3910,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-merge"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-codecs",
|
||||
"lightcraft-color",
|
||||
@@ -3905,12 +3926,12 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-meta"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-heif",
|
||||
"craft-tiff",
|
||||
"lightcraft-geom",
|
||||
"lightcraft-heif",
|
||||
"lightcraft-tiff",
|
||||
"proptest",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
@@ -3919,7 +3940,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-pipeline"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"dhat",
|
||||
"image",
|
||||
@@ -3938,7 +3959,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-preview"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"jpeg-encoder",
|
||||
"lightcraft-raster",
|
||||
@@ -3950,7 +3971,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-raster"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"lightcraft-color",
|
||||
@@ -3962,10 +3983,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-raw"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-crw",
|
||||
"craft-tiff",
|
||||
"dhat",
|
||||
"jpeg-encoder",
|
||||
"jxl-oxide",
|
||||
@@ -3974,7 +3996,6 @@ dependencies = [
|
||||
"lightcraft-meta",
|
||||
"lightcraft-raster",
|
||||
"lightcraft-testkit",
|
||||
"lightcraft-tiff",
|
||||
"miniz_oxide 0.9.1",
|
||||
"proptest",
|
||||
"rayon",
|
||||
@@ -3988,7 +4009,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-scenes"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"image",
|
||||
"lightcraft-color",
|
||||
@@ -3999,7 +4020,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-segment"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"candle-core",
|
||||
"candle-nn",
|
||||
@@ -4013,7 +4034,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-slideshow"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-raster",
|
||||
"serde",
|
||||
@@ -4022,24 +4043,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-sysmem"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-testkit"
|
||||
version = "0.18.0"
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-tiff"
|
||||
version = "0.18.0"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
version = "0.21.1"
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-ui-egui"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"craft-ui",
|
||||
"craft-update",
|
||||
@@ -4072,7 +4084,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-web"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"eframe",
|
||||
@@ -4095,7 +4107,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-wincolor"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-testkit",
|
||||
"windows-sys 0.61.2",
|
||||
@@ -7914,7 +7926,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7"
|
||||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"brotli",
|
||||
"flate2",
|
||||
|
||||
+8
-4
@@ -3,7 +3,7 @@ resolver = "3"
|
||||
members = ["crates/*", "apps/*", "xtask"]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
edition = "2024"
|
||||
license = "MIT OR Apache-2.0"
|
||||
rust-version = "1.95"
|
||||
@@ -15,8 +15,10 @@ craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-l
|
||||
# Canon CRW decoder shared with the other Craft apps
|
||||
craft-crw = { git = "https://github.com/storytold/craft-libs", rev = "f3b62ec61f45534392376492fc5ad33047375205" }
|
||||
# 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 = "a231e517c3b371e1bf0ee8af71ed807424a9a05d", features = ["egui"] }
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
|
||||
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", features = ["egui"] }
|
||||
# HDR gain maps: ISO 21496-1 and hdrgm metadata, compute and apply (moved from crates/codecs; craft-libs#904).
|
||||
craft-gainmap = { git = "https://github.com/storytold/craft-libs", rev = "430a908f68819bf23d14fe73ab9b18d8f4fd4e5d" }
|
||||
# internal
|
||||
lightcraft-denoise-core = { path = "crates/denoise-core" }
|
||||
lightcraft-denoise = { path = "crates/denoise", default-features = false }
|
||||
@@ -26,7 +28,8 @@ lightcraft-sysmem = { path = "crates/sysmem" }
|
||||
lightcraft-wincolor = { path = "crates/wincolor" }
|
||||
lightcraft-color = { path = "crates/color" }
|
||||
lightcraft-raster = { path = "crates/raster" }
|
||||
lightcraft-tiff = { path = "crates/tiff" }
|
||||
# TIFF/IFD: the shared reader and writer (moved from crates/tiff; craft-libs#783).
|
||||
craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "430a908f68819bf23d14fe73ab9b18d8f4fd4e5d" }
|
||||
lightcraft-raw = { path = "crates/raw" }
|
||||
lightcraft-codecs = { path = "crates/codecs" }
|
||||
lightcraft-meta = { path = "crates/meta" }
|
||||
@@ -44,7 +47,8 @@ lightcraft-engine = { path = "crates/engine" }
|
||||
lightcraft-ui-egui = { path = "crates/ui-egui" }
|
||||
lightcraft-mcp = { path = "crates/mcp" }
|
||||
lightcraft-fetch = { path = "crates/fetch" }
|
||||
lightcraft-heif = { path = "crates/heif" }
|
||||
# HEIC/HEIF: the shared decoder (moved from crates/heif; craft-libs#784).
|
||||
craft-heif = { git = "https://github.com/storytold/craft-libs", rev = "430a908f68819bf23d14fe73ab9b18d8f4fd4e5d" }
|
||||
lightcraft-segment = { path = "crates/segment" }
|
||||
lightcraft-geocode = { path = "crates/geocode" }
|
||||
lightcraft-slideshow = { path = "crates/slideshow" }
|
||||
|
||||
@@ -37,6 +37,6 @@ rawnind-model = ["denoise", "lightcraft-engine/rawnind-model"]
|
||||
# (open it in DHAT's viewer: allocation sites by bytes at the peak). Slows allocation-heavy work.
|
||||
dhat-heap = ["dep:dhat"]
|
||||
|
||||
# Open HEIC/HEIF photos (the optional lightcraft-heif crate). Not default so distributors choose
|
||||
# Open HEIC/HEIF photos (the optional craft-heif crate). Not default so distributors choose
|
||||
# it (HEVC patents); build with `--features heif` to include it.
|
||||
heif = ["lightcraft-codecs/heif"]
|
||||
@@ -77,7 +77,7 @@ web-sys = { workspace = true, features = [
|
||||
] }
|
||||
|
||||
[features]
|
||||
# Open HEIC/HEIF photos (the optional lightcraft-heif crate). Not default so distributors choose
|
||||
# Open HEIC/HEIF photos (the optional craft-heif crate). Not default so distributors choose
|
||||
# it (HEVC patents); build with `--features heif` to include it. On wasm32 panics abort, so
|
||||
# heic-rs's `catch_unwind` guard does not apply there.
|
||||
heif = ["lightcraft-codecs/heif"]
|
||||
|
||||
@@ -210,6 +210,8 @@ impl WorkerCore {
|
||||
None => src,
|
||||
};
|
||||
let info = src.info_or(job.info());
|
||||
// an over-budget brush mask is an error for the caller, not a preview missing its later strokes
|
||||
lightcraft_engine::pipeline::check_brush_budget(&src.image, &info, &job.settings, &job.request())?;
|
||||
let mut rendered = match &job.stages {
|
||||
Some(view) => {
|
||||
let st = self.stages.entry(view.clone()).or_default().clone();
|
||||
@@ -442,6 +444,21 @@ mod tests {
|
||||
assert!(max.is_some_and(|m| m <= 3), "max channel difference {max:?}");
|
||||
}
|
||||
|
||||
// an over-budget brush mask is an error for the caller, as on native
|
||||
#[test]
|
||||
fn an_over_budget_brush_mask_fails_the_render() {
|
||||
use lightcraft_engine::develop::{BrushStroke, Mask, MaskComponent, MaskShape};
|
||||
let mut s = Session::with_demo();
|
||||
let id = s.visible_cloned()[0];
|
||||
let mut wire = WireJob::from_job(&s.thumb_job(id, 128).unwrap(), false, "Thumb");
|
||||
let points = serde_json::from_value(serde_json::json!([{"x": -4000.0, "y": 0.5}, {"x": 4000.0, "y": 0.5}])).unwrap();
|
||||
let far = BrushStroke { points, size: 0.05, ..Default::default() };
|
||||
let component = MaskComponent { name: None, op: Default::default(), invert: false, shape: MaskShape::Brush { strokes: vec![far; 10] } };
|
||||
Arc::make_mut(&mut wire.settings).masks.push(Mask { visible: true, components: vec![component], ..Default::default() });
|
||||
let err = WorkerCore::default().render(&wire, None).err().unwrap_or_default();
|
||||
assert!(err.contains("dabs"), "{err}");
|
||||
}
|
||||
|
||||
// a job from before windows existed has none
|
||||
#[test]
|
||||
fn a_job_without_a_window_field_has_no_window() {
|
||||
|
||||
@@ -13,7 +13,7 @@ workspace = true
|
||||
default = ["denoise"]
|
||||
denoise = ["lightcraft-engine/denoise"]
|
||||
rawnind-model = ["denoise", "lightcraft-engine/rawnind-model"]
|
||||
# Open HEIC/HEIF photos (the optional lightcraft-heif crate). Not default so distributors choose
|
||||
# Open HEIC/HEIF photos (the optional craft-heif crate). Not default so distributors choose
|
||||
# it (HEVC patents); build with `--features heif` to include it.
|
||||
heif = ["lightcraft-codecs/heif"]
|
||||
|
||||
|
||||
@@ -201,6 +201,10 @@ impl eframe::App for App {
|
||||
// app after this and the session's last writes (face data) still go out under the lock.
|
||||
#[cfg(target_os = "macos")]
|
||||
self.0.session.release_library_lock();
|
||||
// macOS only: the process ends below without dropping the app, so a ready update's helper
|
||||
// starts here. Elsewhere dropping the app starts it.
|
||||
#[cfg(target_os = "macos")]
|
||||
self.0.start_pending_update();
|
||||
// macOS (#1331): end the process here, before eframe tears the window down, as Quit from
|
||||
// the menu bar already does (PR #444). AccessKit's macOS adapter subclasses the window's
|
||||
// content view and restores its class on teardown; AppKit's Touch Bar support observes
|
||||
|
||||
@@ -49,7 +49,7 @@ pub(crate) fn services(cc: &eframe::CreationContext<'_>, log_file: Option<&std::
|
||||
pick_lightroom_catalog: Some(Box::new(|| {
|
||||
file_dialog()
|
||||
.set_title(lightcraft_ui_egui::i18n::tr("Import Lightroom Catalog"))
|
||||
.add_filter_nocase(lightcraft_ui_egui::i18n::tr("Lightroom Classic Catalog"), &["lrcat"])
|
||||
.add_filter_nocase(lightcraft_ui_egui::i18n::tr("Lightroom Classic Catalog"), &["lrcat", "zip"])
|
||||
.pick_file()
|
||||
.map(|p| p.to_string_lossy().to_string())
|
||||
})),
|
||||
|
||||
@@ -64,6 +64,7 @@
|
||||
//! | 10 | masks stay on the photo under lens corrections, Upright and Transform | `Mask.on_photo` (develop settings) |
|
||||
//! | 11 | lens database profiles named in the Optics panel | `EmbeddedLens.source` |
|
||||
//! | 12 | history steps and versions share segmentation grids (#2154) | `Catalog.grids`, `SegMask.grid` |
|
||||
//! | 13 | Lens Blur | `DevelopSettings.lens_blur`, `Mask.adjust.lens_blur` |
|
||||
//!
|
||||
//! Rules:
|
||||
//! - **Bump [`VERSION`]** (and add a row above) in the change that adds an [`Op`] variant or a
|
||||
@@ -88,7 +89,7 @@ pub const LOG: &str = "catalog.log";
|
||||
const FORMAT: &str = "lightcraft-catalog";
|
||||
/// The catalog format this build writes (and the newest it reads). See the module docs →
|
||||
/// *Format versions*; bump it whenever an [`Op`] variant or a serialized field is added.
|
||||
pub const VERSION: u32 = 12;
|
||||
pub const VERSION: u32 = 13;
|
||||
|
||||
/// When [`Journal::wants_snapshot`] says it's time to compact the log.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
|
||||
@@ -238,9 +238,9 @@ fn op_variants_are_versioned() {
|
||||
Op::SetHistoryCursor { .. } => 8,
|
||||
}
|
||||
}
|
||||
/// v12: `Catalog.grids` and `SegMask.grid`, shared by a snapshot's masks (#2154) (v11: `EmbeddedLens.source`; v10: `Mask.on_photo`; v9:
|
||||
/// `Photo.preview_checked` and `Op::SetContent.preview_checked`).
|
||||
const NEWEST_FIELDS: u32 = 12;
|
||||
/// v13: Lens Blur in develop settings and mask adjustments (v12: `Catalog.grids` and `SegMask.grid`; v11: `EmbeddedLens.source`;
|
||||
/// v10: `Mask.on_photo`; v9: `Photo.preview_checked` and `Op::SetContent.preview_checked`).
|
||||
const NEWEST_FIELDS: u32 = 13;
|
||||
let newest = since(&Op::SetHistoryCursor { id: PhotoId(1), cursor: None });
|
||||
assert_eq!(newest.max(NEWEST_FIELDS), VERSION, "the newest op's or fields' version must be the current format version");
|
||||
}
|
||||
@@ -12,7 +12,8 @@ workspace = true
|
||||
lightcraft-geom = { workspace = true }
|
||||
lightcraft-color = { workspace = true }
|
||||
lightcraft-raster = { workspace = true }
|
||||
lightcraft-heif = { workspace = true, optional = true }
|
||||
craft-gainmap = { workspace = true }
|
||||
craft-heif = { workspace = true, optional = true }
|
||||
serde = { workspace = true }
|
||||
bytemuck = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
@@ -40,10 +41,10 @@ default = ["parallel", "jxl", "avif"]
|
||||
parallel = ["dep:rayon", "lightcraft-raster/parallel", "jpeg-decoder/rayon"]
|
||||
## JPEG XL decode (jxl-oxide) and, on native targets, encode (jixel); both pure Rust.
|
||||
jxl = ["dep:jxl-oxide", "dep:jixel"]
|
||||
# HEIC/HEIF decode via the optional lightcraft-heif crate (heic-rs, pure Rust). Off by default so
|
||||
# HEIC/HEIF decode via the optional craft-heif crate (heic-rs, pure Rust). Off by default so
|
||||
# distributors choose it at build time (HEVC patents are a distributor's call, same policy as
|
||||
# PhotoCraft); `cargo xtask ci` clippies and tests it (`--features heif`), releases don't ship it yet.
|
||||
heif = ["dep:lightcraft-heif"]
|
||||
heif = ["dep:craft-heif"]
|
||||
## AVIF encoding via ravif/rav1e (pure Rust, BSD). Native targets only; a no-op on wasm32.
|
||||
avif = ["dep:ravif", "dep:rav1e", "dep:avif-serialize"]
|
||||
|
||||
@@ -54,7 +55,7 @@ lightcraft-testkit = { workspace = true }
|
||||
jxl-oxide = { workspace = true }
|
||||
proptest = { workspace = true }
|
||||
# Lossless synthetic HEIC files for the `heif` feature's tests.
|
||||
lightcraft-heif = { workspace = true, features = ["testdata"] }
|
||||
craft-heif = { workspace = true, features = ["testdata"] }
|
||||
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
|
||||
dhat = { workspace = true }
|
||||
@@ -48,13 +48,13 @@ CRW, MRW, X3F) are detected so the engine can route them to `lightcraft-raw`; `d
|
||||
|---|---|---|---|
|
||||
| JPEG | yes (zune-jpeg; jpeg-decoder for DCT scaling, CMYK/YCCK, 12-bit, non-interleaved scans) | yes (jpeg-encoder, baseline, 4:4:4/4:2:2/4:2:0) | EXIF (APP1), XMP (APP1, ≤ 64 KiB), ICC (APP2; only complete, consistently counted, uniquely numbered chunk sequences are assembled; malformed profiles use the existing unsupported-profile fallback), Adobe APP14, MPF previews |
|
||||
| PNG | yes, 1–16-bit, palette, tRNS, interlaced | 8/16-bit, gray/GA/RGB/RGBA | iCCP, eXIf, iTXt XMP; sRGB/cICP/cHRM/gAMA honoured when no ICC |
|
||||
| TIFF | 8/16-bit int (32/64-bit int kept to 16 bits), 16/32/64-bit float, gray/RGB/CMYK/palette, alpha (assoc./unassoc.), planar, LZW/Deflate/PackBits/JPEG | 8/16-bit, 32-bit float; none/LZW/Deflate/PackBits | ICC (34675), XMP (700), Orientation; first IFD only; EXIF not written (use `lightcraft-tiff`) |
|
||||
| TIFF | 8/16-bit int (32/64-bit int kept to 16 bits), 16/32/64-bit float, gray/RGB/CMYK/palette, alpha (assoc./unassoc.), planar, LZW/Deflate/PackBits/JPEG | 8/16-bit, 32-bit float; none/LZW/Deflate/PackBits | ICC (34675), XMP (700), Orientation; first IFD only; EXIF not written (use `craft-tiff`) |
|
||||
| WebP | lossy + lossless, alpha, first frame of animations | lossless only | ICCP/EXIF/XMP chunks |
|
||||
| GIF / BMP | yes (first GIF frame) | — | via `image` |
|
||||
| PSD / PSB | merged composite: 8/16/32-bit gray, RGB, CMYK, indexed, duotone (as gray); raw/RLE | — | ICC (1039), EXIF (1058), XMP (1060); ZIP-compressed composite, Lab and 1-bit unsupported |
|
||||
| JPEG XL | yes (jxl-oxide, feature `jxl`, default on) | yes (jixel, `encode_jxl`, feature `jxl`, native only): lossy VarDCT / lossless Modular, 8/16-bit, effort (fast / normal), progressive; HDR as 16-bit Rec. 2020 PQ | decode: enum colour → rendered straight to linear Rec.2020 (XYB via linear sRGB primaries); ICC → our ICC path; orientation applied by the decoder (reported as 1). Encode: lossy files declare an enum encoding (sRGB, Display P3, Rec. 2020 BT.709 curve, Rec. 2020 PQ), lossless ones embed the ICC profile; EXIF / XMP in `Exif` / `xml ` boxes |
|
||||
| AVIF | **no** | yes (ravif/rav1e, native only, feature `avif`) | 8/10-bit sRGB, EXIF; no ICC in the muxer. HDR: `encode_avif_hdr` writes 10-bit BT.2020 PQ (SDR white at 203 cd/m², CICP `nclx` + `clli`) or HLG (`hlg_signal`, SDR white at 0.75) |
|
||||
| HEIC/HEIF | yes with feature `heif` (lightcraft-heif: heic-rs + libheif-matching colour; 4:2:0/mono, 8–10-bit, grids, irot/imir/clap, ICC/nclx/VUI, EXIF/XMP, thumbnail item) | — | 4:2:2/4:4:4 (Canon/Sony HIF) unsupported |
|
||||
| HEIC/HEIF | yes with feature `heif` (craft-heif: heic-rs + libheif-matching colour; 4:2:0/mono, 8–10-bit, grids, irot/imir/clap, ICC/nclx/VUI, EXIF/XMP, thumbnail item) | — | 4:2:2/4:4:4 (Canon/Sony HIF) unsupported |
|
||||
| Gain map JPEG (HDR) | gain map found and decoded (`gainmap::read_jpeg`): ISO 21496-1 metadata, else Adobe `hdrgm` XMP | yes (`gainmap::encode_jpeg`) | CIPA DC-007 MPF index (gain map typed `0x050000`), ISO 21496-1 APP2 on both images, `hdrgm` + Container XMP for Android/Chrome, Apple `HDRGainMap`/`HDRToneMap` XMP; gain maps are never used as thumbnails |
|
||||
|
||||
## Colour
|
||||
|
||||
@@ -39,12 +39,16 @@ impl<'a> EncodeImage<'a> {
|
||||
if self.width == 0 || self.height == 0 {
|
||||
return Err(Error::Encode("zero dimension".into()));
|
||||
}
|
||||
let need = self.width as u64 * self.height as u64 * self.channels as u64;
|
||||
// u128: 2^32 x 2^32 x 4 does not fit u64, and every later `width * height * channels` is bounded by this
|
||||
let need = self.width as u128 * self.height as u128 * self.channels as u128;
|
||||
if need > usize::MAX as u128 {
|
||||
return Err(Error::Encode("image too large".into()));
|
||||
}
|
||||
let have = match self.samples {
|
||||
Samples::U8(s) => s.len(),
|
||||
Samples::U16(s) => s.len(),
|
||||
Samples::F32(s) => s.len(),
|
||||
} as u64;
|
||||
} as u128;
|
||||
if have < need {
|
||||
return Err(Error::Encode(format!("sample buffer too short: {have} < {need}")));
|
||||
}
|
||||
@@ -225,7 +229,7 @@ pub enum TiffCompression {
|
||||
}
|
||||
|
||||
/// Encode TIFF: 8/16-bit integer or 32-bit float, gray/RGB with optional (unassociated) alpha.
|
||||
/// ICC and XMP are embedded; EXIF is not (write it through the TIFF writer in `lightcraft-tiff`).
|
||||
/// ICC and XMP are embedded; EXIF is not (write it through the TIFF writer in `craft-tiff`).
|
||||
pub fn encode_tiff(img: &EncodeImage, compression: TiffCompression, meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||||
use tiff::encoder::{Compression, DeflateLevel, TiffEncoder};
|
||||
use tiff::tags::Predictor;
|
||||
@@ -671,6 +675,13 @@ fn avif_10bit(enc: &ravif::Encoder, img: &EncodeImage, s: &[u16]) -> Result<Vec<
|
||||
mod hlg_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn dimensions_whose_sample_count_wraps_are_refused() {
|
||||
// 2^31 x 2^31 x 4 is 2^64 samples: zero when wrapped, which an empty buffer would satisfy
|
||||
let img = EncodeImage::new(1 << 31, 1 << 31, 4, Samples::U8(&[]));
|
||||
assert!(img.validate().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hlg_reference_white_and_peak() {
|
||||
// BT.2408: SDR (HDR reference) white sits at 75 % HLG signal
|
||||
|
||||
+25
-630
@@ -1,547 +1,21 @@
|
||||
//! Gain map HDR images (ISO 21496-1): an SDR base image plus a gain map that a decoder uses to
|
||||
//! rebuild the HDR rendition, so the file looks right everywhere and lights up on HDR displays.
|
||||
//! Gain map JPEGs (ISO 21496-1): an SDR base image plus a gain map that a decoder uses to rebuild
|
||||
//! the HDR rendition, so the file looks right everywhere and lights up on HDR displays.
|
||||
//!
|
||||
//! The metadata formats and the arithmetic ([`compute`], [`apply`], [`GainMapMeta`]) are
|
||||
//! `craft-gainmap`'s, re-exported here. This module lays the two images out in a JPEG:
|
||||
//!
|
||||
//! - [`compute`] measures a gain map from an SDR and an HDR rendition of the same picture (linear
|
||||
//! light, same primaries). Measure it against the base *as written* (decoded from its 8-bit
|
||||
//! codes), so quantisation in the base is handed back by the gain map.
|
||||
//! - [`apply`] rebuilds the HDR rendition from a base and its gain map.
|
||||
//! - [`encode_jpeg`] writes a gain map JPEG: the base with an MPF index (CIPA DC-007), the ISO
|
||||
//! 21496-1 marker and Adobe/Google container XMP, followed by the gain map JPEG carrying the ISO
|
||||
//! 21496-1 metadata, the Adobe `hdrgm` XMP (Android, Chrome) and Apple's description of the same
|
||||
//! map (what survives an iMessage send).
|
||||
//! - [`read_jpeg`] finds and decodes the gain map of a gain map JPEG (ours, cameras, Lightroom),
|
||||
//! from its ISO 21496-1 metadata or, failing that, its `hdrgm` XMP.
|
||||
//!
|
||||
//! All gains are log2. A decoder recovers `log2(hdr + offset_hdr) − log2(sdr + offset_sdr)` as
|
||||
//! `min + (max − min) · (code / 255)^(1 / gamma)`, scaled by a weight in 0..1 that depends on how
|
||||
//! much headroom the display has between `base_headroom` and `alternate_headroom`.
|
||||
|
||||
pub use craft_gainmap::{GainImage, GainMapMeta, GainMapOptions, ISO_URN, apply, compute};
|
||||
|
||||
use crate::encode::{EncodeImage, EncodeMeta, Samples, jpeg_app_segments, xmp_segment};
|
||||
use crate::{ChromaSubsampling, Error, Format, Result, jpeg, jpeg_par};
|
||||
|
||||
/// The ISO 21496-1 namespace that opens its APP2 payloads (NUL terminated).
|
||||
pub const ISO_URN: &[u8] = b"urn:iso:std:iso:ts:21496:-1\0";
|
||||
|
||||
/// Fixed denominator for the metadata's rationals: one value per setting, byte-identical output.
|
||||
const DENOMINATOR: u32 = 1_000_000;
|
||||
|
||||
/// Gain map parameters (ISO 21496-1 / Adobe `hdrgm`). Per-channel arrays repeat the first value
|
||||
/// when the map is single-channel.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct GainMapMeta {
|
||||
/// Three gain channels (R, G, B) instead of one luminance gain.
|
||||
pub multichannel: bool,
|
||||
/// The gain applies in the base image's colour space.
|
||||
pub use_base_colour_space: bool,
|
||||
/// log2 headroom at which the base is shown as is (0: an SDR base).
|
||||
pub base_headroom: f32,
|
||||
/// log2 headroom at which the gain map applies in full.
|
||||
pub alternate_headroom: f32,
|
||||
pub min: [f32; 3],
|
||||
pub max: [f32; 3],
|
||||
pub gamma: [f32; 3],
|
||||
pub base_offset: [f32; 3],
|
||||
pub alternate_offset: [f32; 3],
|
||||
}
|
||||
|
||||
impl Default for GainMapMeta {
|
||||
fn default() -> Self {
|
||||
GainMapMeta {
|
||||
multichannel: false,
|
||||
use_base_colour_space: true,
|
||||
base_headroom: 0.0,
|
||||
alternate_headroom: 1.0,
|
||||
min: [0.0; 3],
|
||||
max: [1.0; 3],
|
||||
gamma: [1.0; 3],
|
||||
base_offset: [1.0 / 64.0; 3],
|
||||
alternate_offset: [1.0 / 64.0; 3],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GainMapMeta {
|
||||
fn channels(&self) -> usize {
|
||||
if self.multichannel { 3 } else { 1 }
|
||||
}
|
||||
|
||||
/// Usable by [`apply`]: finite, positive gammas, max ≥ min, headrooms in order.
|
||||
pub fn is_valid(&self) -> bool {
|
||||
let fin = |a: &[f32; 3]| a.iter().all(|v| v.is_finite());
|
||||
fin(&self.min)
|
||||
&& fin(&self.max)
|
||||
&& fin(&self.gamma)
|
||||
&& fin(&self.base_offset)
|
||||
&& fin(&self.alternate_offset)
|
||||
&& self.gamma.iter().all(|g| *g > 0.0)
|
||||
&& self.min.iter().zip(&self.max).all(|(a, b)| b >= a)
|
||||
&& self.base_headroom.is_finite()
|
||||
&& self.alternate_headroom.is_finite()
|
||||
&& self.alternate_headroom > self.base_headroom
|
||||
}
|
||||
|
||||
/// The ISO 21496-1 APP2 payload of the gain map image, URN included.
|
||||
pub fn to_iso(&self) -> Vec<u8> {
|
||||
let mut p = ISO_URN.to_vec();
|
||||
p.extend_from_slice(&0u16.to_be_bytes()); // minimum_version: readers gate on this
|
||||
p.extend_from_slice(&0u16.to_be_bytes()); // writer_version
|
||||
let mut flags = 0u8;
|
||||
if self.multichannel {
|
||||
flags |= FLAG_MULTICHANNEL;
|
||||
}
|
||||
if self.use_base_colour_space {
|
||||
flags |= FLAG_BASE_COLOUR_SPACE;
|
||||
}
|
||||
p.push(flags);
|
||||
let unsigned = |p: &mut Vec<u8>, v: f32| {
|
||||
let n = (v as f64 * DENOMINATOR as f64).round().clamp(0.0, u32::MAX as f64) as u32;
|
||||
p.extend_from_slice(&n.to_be_bytes());
|
||||
p.extend_from_slice(&DENOMINATOR.to_be_bytes());
|
||||
};
|
||||
let signed = |p: &mut Vec<u8>, v: f32| {
|
||||
let n = (v as f64 * DENOMINATOR as f64).round().clamp(i32::MIN as f64, i32::MAX as f64) as i32;
|
||||
p.extend_from_slice(&n.to_be_bytes());
|
||||
p.extend_from_slice(&DENOMINATOR.to_be_bytes());
|
||||
};
|
||||
unsigned(&mut p, self.base_headroom.max(0.0));
|
||||
unsigned(&mut p, self.alternate_headroom.max(0.0));
|
||||
for c in 0..self.channels() {
|
||||
signed(&mut p, self.min[c]);
|
||||
signed(&mut p, self.max[c]);
|
||||
unsigned(&mut p, self.gamma[c]);
|
||||
signed(&mut p, self.base_offset[c]);
|
||||
signed(&mut p, self.alternate_offset[c]);
|
||||
}
|
||||
p
|
||||
}
|
||||
|
||||
/// Parse an ISO 21496-1 payload (the bytes after [`ISO_URN`]). `None` for the versions-only
|
||||
/// marker a primary image carries, unknown versions, a base that is the HDR rendition, or
|
||||
/// values no decoder could use.
|
||||
pub fn from_iso(p: &[u8]) -> Option<GainMapMeta> {
|
||||
let mut r = Reader { b: p, at: 0 };
|
||||
let min_version = r.u16()?;
|
||||
let _writer_version = r.u16()?;
|
||||
if min_version != 0 {
|
||||
return None;
|
||||
}
|
||||
let flags = r.u8()?;
|
||||
if flags & FLAG_BACKWARD != 0 {
|
||||
return None;
|
||||
}
|
||||
let multichannel = flags & FLAG_MULTICHANNEL != 0;
|
||||
let common = flags & FLAG_COMMON_DENOMINATOR != 0;
|
||||
let common_d = if common { r.u32()? } else { 0 };
|
||||
let unsigned = |r: &mut Reader| -> Option<f32> {
|
||||
let n = r.u32()?;
|
||||
let d = if common { common_d } else { r.u32()? };
|
||||
(d != 0).then(|| (n as f64 / d as f64) as f32)
|
||||
};
|
||||
let signed = |r: &mut Reader| -> Option<f32> {
|
||||
let n = r.u32()? as i32;
|
||||
let d = if common { common_d } else { r.u32()? };
|
||||
(d != 0).then(|| (n as f64 / d as f64) as f32)
|
||||
};
|
||||
let mut m = GainMapMeta {
|
||||
multichannel,
|
||||
use_base_colour_space: flags & FLAG_BASE_COLOUR_SPACE != 0,
|
||||
base_headroom: unsigned(&mut r)?,
|
||||
alternate_headroom: unsigned(&mut r)?,
|
||||
..GainMapMeta::default()
|
||||
};
|
||||
for c in 0..m.channels() {
|
||||
m.min[c] = signed(&mut r)?;
|
||||
m.max[c] = signed(&mut r)?;
|
||||
m.gamma[c] = unsigned(&mut r)?;
|
||||
m.base_offset[c] = signed(&mut r)?;
|
||||
m.alternate_offset[c] = signed(&mut r)?;
|
||||
}
|
||||
if !multichannel {
|
||||
m.fill_from_first();
|
||||
}
|
||||
m.is_valid().then_some(m)
|
||||
}
|
||||
|
||||
fn fill_from_first(&mut self) {
|
||||
for a in [&mut self.min, &mut self.max, &mut self.gamma, &mut self.base_offset, &mut self.alternate_offset] {
|
||||
a[1] = a[0];
|
||||
a[2] = a[0];
|
||||
}
|
||||
}
|
||||
|
||||
/// The gain parameters from Adobe `hdrgm` XMP (the gain map image's packet), for files
|
||||
/// without ISO 21496-1 metadata (the first Ultra HDR files). Attributes or `rdf:Seq` lists.
|
||||
pub fn from_hdrgm_xmp(xmp: &str) -> Option<GainMapMeta> {
|
||||
if !xmp.contains("hdrgm:") {
|
||||
return None;
|
||||
}
|
||||
let base_rendition_hdr = xmp_values(xmp, "BaseRenditionIsHDR").is_some_and(|v| v.first().is_some_and(|s| s.eq_ignore_ascii_case("true")));
|
||||
if base_rendition_hdr {
|
||||
return None;
|
||||
}
|
||||
let get = |name: &str, default: f32| -> Option<([f32; 3], bool)> {
|
||||
let Some(v) = xmp_values(xmp, name) else { return Some(([default; 3], false)) };
|
||||
let f: Vec<f32> = v.iter().map(|s| s.trim().parse::<f32>().ok()).collect::<Option<_>>()?;
|
||||
match f.as_slice() {
|
||||
[a] => Some(([*a; 3], false)),
|
||||
[a, b, c] => Some(([*a, *b, *c], true)),
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
let (max, m1) = get("GainMapMax", f32::NAN)?;
|
||||
if max.iter().any(|v| v.is_nan()) {
|
||||
return None; // the one required parameter
|
||||
}
|
||||
let (min, m2) = get("GainMapMin", 0.0)?;
|
||||
let (gamma, m3) = get("Gamma", 1.0)?;
|
||||
let (base_offset, m4) = get("OffsetSDR", 1.0 / 64.0)?;
|
||||
let (alternate_offset, m5) = get("OffsetHDR", 1.0 / 64.0)?;
|
||||
let (cap_min, _) = get("HDRCapacityMin", 0.0)?;
|
||||
let max_gain = max.iter().copied().fold(f32::MIN, f32::max);
|
||||
let (cap_max, _) = get("HDRCapacityMax", max_gain)?;
|
||||
let m = GainMapMeta {
|
||||
multichannel: m1 || m2 || m3 || m4 || m5,
|
||||
use_base_colour_space: true,
|
||||
base_headroom: cap_min[0].max(0.0),
|
||||
alternate_headroom: cap_max[0],
|
||||
min,
|
||||
max,
|
||||
gamma,
|
||||
base_offset,
|
||||
alternate_offset,
|
||||
};
|
||||
m.is_valid().then_some(m)
|
||||
}
|
||||
|
||||
/// The gain map image's XMP: the Adobe `hdrgm` parameters (read by Android and Chrome, which
|
||||
/// ignore the ISO payload) and Apple's description of the same map (what iMessage keeps).
|
||||
pub fn to_gain_map_xmp(&self) -> String {
|
||||
let names = ["GainMapMin", "GainMapMax", "Gamma", "OffsetSDR", "OffsetHDR"];
|
||||
let values = [&self.min, &self.max, &self.gamma, &self.base_offset, &self.alternate_offset];
|
||||
let uniform = |v: &[f32; 3]| !self.multichannel || (v[0] == v[1] && v[1] == v[2]);
|
||||
let mut s = String::from(XMP_OPEN);
|
||||
s.push_str(r#"<rdf:Description rdf:about="" xmlns:hdrgm="http://ns.adobe.com/hdr-gain-map/1.0/" hdrgm:Version="1.0""#);
|
||||
s.push_str(&format!(r#" hdrgm:HDRCapacityMin="{}""#, num(self.base_headroom)));
|
||||
s.push_str(&format!(r#" hdrgm:HDRCapacityMax="{}""#, num(self.alternate_headroom)));
|
||||
s.push_str(r#" hdrgm:BaseRenditionIsHDR="False""#);
|
||||
for (n, v) in names.iter().zip(values) {
|
||||
if uniform(v) {
|
||||
s.push_str(&format!(r#" hdrgm:{n}="{}""#, num(v[0])));
|
||||
}
|
||||
}
|
||||
s.push('>');
|
||||
// per-channel values are an rdf:Seq of three, not a comma list: readers parse each item
|
||||
for (n, v) in names.iter().zip(values) {
|
||||
if !uniform(v) {
|
||||
s.push_str(&format!("<hdrgm:{n}><rdf:Seq>"));
|
||||
for x in v {
|
||||
s.push_str(&format!("<rdf:li>{}</rdf:li>", num(*x)));
|
||||
}
|
||||
s.push_str(&format!("</rdf:Seq></hdrgm:{n}>"));
|
||||
}
|
||||
}
|
||||
s.push_str("</rdf:Description>");
|
||||
s.push_str(&self.apple_description());
|
||||
s.push_str(XMP_CLOSE);
|
||||
s
|
||||
}
|
||||
|
||||
/// Apple's own description of the map (`apdi`, `HDRGainMap`, `HDRToneMap` namespaces).
|
||||
fn apple_description(&self) -> String {
|
||||
// 'L008' (8-bit luminance) or '444f'; the stored map must really have that many channels
|
||||
let format = if self.multichannel { 0x3434_3466u32 } else { 0x4C30_3038 };
|
||||
let mut s = String::from(
|
||||
r#"<rdf:Description rdf:about="" xmlns:apdi="http://ns.apple.com/pixeldatainfo/1.0/" xmlns:HDRGainMap="http://ns.apple.com/HDRGainMap/1.0/" xmlns:HDRToneMap="http://ns.apple.com/HDRToneMap/1.0/">"#,
|
||||
);
|
||||
s.push_str("<apdi:AuxiliaryImageType>urn:com:apple:photo:2020:aux:hdrgainmap</apdi:AuxiliaryImageType>");
|
||||
s.push_str(&format!("<apdi:NativeFormat>{format}</apdi:NativeFormat><apdi:StoredFormat>{format}</apdi:StoredFormat>"));
|
||||
s.push_str("<HDRGainMap:HDRGainMapVersion>131072</HDRGainMap:HDRGainMapVersion>");
|
||||
// linear, not log2: the headroom multiplier itself
|
||||
s.push_str(&format!("<HDRGainMap:HDRGainMapHeadroom>{}</HDRGainMap:HDRGainMapHeadroom>", num(self.alternate_headroom.exp2())));
|
||||
s.push_str(&format!("<HDRToneMap:AlternateHeadroom>{}</HDRToneMap:AlternateHeadroom>", num(self.alternate_headroom)));
|
||||
s.push_str("<HDRToneMap:ChannelMetadata><rdf:Seq>");
|
||||
for c in 0..self.channels() {
|
||||
s.push_str(&format!(
|
||||
r#"<rdf:li rdf:parseType="Resource"><HDRToneMap:GainMapMin>{}</HDRToneMap:GainMapMin><HDRToneMap:GainMapMax>{}</HDRToneMap:GainMapMax><HDRToneMap:Gamma>{}</HDRToneMap:Gamma><HDRToneMap:BaseOffset>{}</HDRToneMap:BaseOffset><HDRToneMap:AlternateOffset>{}</HDRToneMap:AlternateOffset></rdf:li>"#,
|
||||
num(self.min[c]),
|
||||
num(self.max[c]),
|
||||
num(self.gamma[c]),
|
||||
num(self.base_offset[c]),
|
||||
num(self.alternate_offset[c])
|
||||
));
|
||||
}
|
||||
s.push_str("</rdf:Seq></HDRToneMap:ChannelMetadata>");
|
||||
s.push_str(&format!("<HDRToneMap:BaseHeadroom>{}</HDRToneMap:BaseHeadroom>", num(self.base_headroom)));
|
||||
s.push_str("<HDRToneMap:BaseColorIsWorkingColor>True</HDRToneMap:BaseColorIsWorkingColor><HDRToneMap:Version>1</HDRToneMap:Version>");
|
||||
s.push_str("</rdf:Description>");
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
const FLAG_MULTICHANNEL: u8 = 0x80;
|
||||
const FLAG_BASE_COLOUR_SPACE: u8 = 0x40;
|
||||
const FLAG_COMMON_DENOMINATOR: u8 = 0x08;
|
||||
const FLAG_BACKWARD: u8 = 0x04;
|
||||
|
||||
const XMP_OPEN: &str = "<?xpacket begin=\"\u{feff}\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?><x:xmpmeta xmlns:x=\"adobe:ns:meta/\"><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">";
|
||||
const XMP_CLOSE: &str = "</rdf:RDF></x:xmpmeta><?xpacket end=\"w\"?>";
|
||||
|
||||
/// A short decimal (up to 6 significant digits, no exponent for ordinary values).
|
||||
fn num(v: f32) -> String {
|
||||
if !v.is_finite() {
|
||||
return "0".into();
|
||||
}
|
||||
let s = format!("{:.6}", v);
|
||||
let s = s.trim_end_matches('0').trim_end_matches('.');
|
||||
if s == "-0" || s.is_empty() { "0".into() } else { s.to_string() }
|
||||
}
|
||||
|
||||
/// The values of `hdrgm:<name>`: an attribute (`name="v"`) or an element holding an `rdf:Seq`.
|
||||
fn xmp_values(xmp: &str, name: &str) -> Option<Vec<String>> {
|
||||
let attr = format!("hdrgm:{name}=");
|
||||
if let Some(i) = xmp.find(&attr) {
|
||||
let rest = xmp.get(i + attr.len()..)?;
|
||||
let q = rest.chars().next()?;
|
||||
if q == '"' || q == '\'' {
|
||||
let body = rest.get(1..)?;
|
||||
let end = body.find(q)?;
|
||||
return Some(vec![body.get(..end)?.to_string()]);
|
||||
}
|
||||
}
|
||||
let open = format!("<hdrgm:{name}>");
|
||||
let close = format!("</hdrgm:{name}>");
|
||||
let i = xmp.find(&open)?;
|
||||
let body = xmp.get(i + open.len()..)?;
|
||||
let body = body.get(..body.find(&close)?)?;
|
||||
let mut out = Vec::new();
|
||||
let mut rest = body;
|
||||
while let Some(a) = rest.find("<rdf:li>") {
|
||||
let after = rest.get(a + 8..)?;
|
||||
let b = after.find("</rdf:li>")?;
|
||||
out.push(after.get(..b)?.to_string());
|
||||
rest = after.get(b..)?;
|
||||
if out.len() > 3 {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
if out.is_empty() {
|
||||
// a plain element value
|
||||
out.push(body.trim().to_string());
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
struct Reader<'a> {
|
||||
b: &'a [u8],
|
||||
at: usize,
|
||||
}
|
||||
|
||||
impl Reader<'_> {
|
||||
fn take<const N: usize>(&mut self) -> Option<[u8; N]> {
|
||||
let s = self.b.get(self.at..self.at.checked_add(N)?)?;
|
||||
self.at += N;
|
||||
s.try_into().ok()
|
||||
}
|
||||
fn u8(&mut self) -> Option<u8> {
|
||||
self.take::<1>().map(|a| a[0])
|
||||
}
|
||||
fn u16(&mut self) -> Option<u16> {
|
||||
self.take().map(u16::from_be_bytes)
|
||||
}
|
||||
fn u32(&mut self) -> Option<u32> {
|
||||
self.take().map(u32::from_be_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
/// A gain map image: `channels` (1 or 3) interleaved 8-bit codes.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct GainImage {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub channels: usize,
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl GainImage {
|
||||
/// Bilinear code of channel `c` at gain map coordinates (`x`, `y`) (pixel centres at +0.5).
|
||||
fn sample(&self, x: f32, y: f32, c: usize) -> f32 {
|
||||
let (w, h) = (self.width, self.height);
|
||||
let fx = (x - 0.5).clamp(0.0, (w - 1) as f32);
|
||||
let fy = (y - 0.5).clamp(0.0, (h - 1) as f32);
|
||||
let (x0, y0) = (fx as usize, fy as usize);
|
||||
let (x1, y1) = ((x0 + 1).min(w - 1), (y0 + 1).min(h - 1));
|
||||
let (tx, ty) = (fx - x0 as f32, fy - y0 as f32);
|
||||
let at = |x: usize, y: usize| self.data.get((y * w + x) * self.channels + c).copied().unwrap_or(0) as f32;
|
||||
let a = at(x0, y0) + (at(x1, y0) - at(x0, y0)) * tx;
|
||||
let b = at(x0, y1) + (at(x1, y1) - at(x0, y1)) * tx;
|
||||
a + (b - a) * ty
|
||||
}
|
||||
}
|
||||
|
||||
/// How [`compute`] builds the map.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct GainMapOptions {
|
||||
/// Downscale factor of the map (1, 2 or 4).
|
||||
pub scale: usize,
|
||||
/// Three gains (R, G, B) instead of one luminance gain. Apple's readers want a single
|
||||
/// channel, so this is off by default.
|
||||
pub multichannel: bool,
|
||||
pub gamma: f32,
|
||||
/// Offsets added to SDR and HDR before the ratio (keep blacks finite and noise quiet).
|
||||
pub offset: f32,
|
||||
}
|
||||
|
||||
impl Default for GainMapOptions {
|
||||
fn default() -> Self {
|
||||
GainMapOptions { scale: 2, multichannel: false, gamma: 1.0, offset: 1.0 / 64.0 }
|
||||
}
|
||||
}
|
||||
|
||||
/// Measure the gain map that turns `sdr` into `hdr` (both `width × height` linear RGB in the same
|
||||
/// primaries; `luma` = that space's luminance weights).
|
||||
pub fn compute(
|
||||
sdr: &[[f32; 3]],
|
||||
hdr: &[[f32; 3]],
|
||||
width: usize,
|
||||
height: usize,
|
||||
luma: [f32; 3],
|
||||
o: &GainMapOptions,
|
||||
) -> Result<(GainImage, GainMapMeta)> {
|
||||
let n = width.checked_mul(height).ok_or_else(|| Error::Encode("gain map: image too large".into()))?;
|
||||
if n == 0 || sdr.len() < n || hdr.len() < n {
|
||||
return Err(Error::Encode("gain map: SDR and HDR renditions must match the image size".into()));
|
||||
}
|
||||
if !(matches!(o.scale, 1 | 2 | 4) && o.gamma > 0.0 && o.gamma.is_finite() && o.offset > 0.0 && o.offset.is_finite()) {
|
||||
return Err(Error::Encode("gain map: scale must be 1, 2 or 4, gamma and offset positive".into()));
|
||||
}
|
||||
let s = o.scale;
|
||||
let (gw, gh) = (width.div_ceil(s), height.div_ceil(s));
|
||||
let ch = if o.multichannel { 3 } else { 1 };
|
||||
let y = |p: &[f32; 3]| luma[0] * p[0] + luma[1] * p[1] + luma[2] * p[2];
|
||||
let ratio = |a: f32, b: f32| ((b.max(0.0) + o.offset) / (a.max(0.0) + o.offset)).log2();
|
||||
// block means of the log gains
|
||||
let mut gains = vec![0f32; gw * gh * ch];
|
||||
let mut lo = [f32::MAX; 3];
|
||||
let mut hi = [f32::MIN; 3];
|
||||
for gy in 0..gh {
|
||||
for gx in 0..gw {
|
||||
let mut acc = [0f32; 3];
|
||||
let mut count = 0f32;
|
||||
for yy in gy * s..((gy + 1) * s).min(height) {
|
||||
for xx in gx * s..((gx + 1) * s).min(width) {
|
||||
let i = yy * width + xx;
|
||||
let (Some(a), Some(b)) = (sdr.get(i), hdr.get(i)) else { continue };
|
||||
if o.multichannel {
|
||||
for c in 0..3 {
|
||||
acc[c] += ratio(a[c], b[c]);
|
||||
}
|
||||
} else {
|
||||
acc[0] += ratio(y(a), y(b));
|
||||
}
|
||||
count += 1.0;
|
||||
}
|
||||
}
|
||||
for c in 0..ch {
|
||||
let g = if count > 0.0 { acc[c] / count } else { 0.0 };
|
||||
let g = if g.is_finite() { g } else { 0.0 };
|
||||
gains[(gy * gw + gx) * ch + c] = g;
|
||||
lo[c] = lo[c].min(g);
|
||||
hi[c] = hi[c].max(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut m = GainMapMeta {
|
||||
multichannel: o.multichannel,
|
||||
gamma: [o.gamma; 3],
|
||||
base_offset: [o.offset; 3],
|
||||
alternate_offset: [o.offset; 3],
|
||||
..GainMapMeta::default()
|
||||
};
|
||||
for c in 0..ch {
|
||||
// the range always includes 0 (no change) so an SDR-only area encodes exactly
|
||||
m.min[c] = lo[c].min(0.0);
|
||||
m.max[c] = hi[c].max(m.min[c] + 1e-4);
|
||||
}
|
||||
if !o.multichannel {
|
||||
m.fill_from_first();
|
||||
}
|
||||
m.base_headroom = 0.0;
|
||||
m.alternate_headroom = m.max.iter().copied().fold(0.0f32, f32::max).max(1e-3);
|
||||
let data = gains
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, g)| {
|
||||
let c = i % ch;
|
||||
let norm = ((g - m.min[c]) / (m.max[c] - m.min[c])).clamp(0.0, 1.0);
|
||||
(norm.powf(o.gamma) * 255.0).round() as u8
|
||||
})
|
||||
.collect();
|
||||
Ok((GainImage { width: gw, height: gh, channels: ch, data }, m))
|
||||
}
|
||||
|
||||
/// Rebuild the HDR rendition in place: `base` (`width × height` linear RGB) is replaced by the
|
||||
/// rendition at `weight` (0 = the base, 1 = the full alternate rendition; a display with headroom
|
||||
/// `H` uses `(H − base_headroom) / (alternate_headroom − base_headroom)`, clamped).
|
||||
pub fn apply(base: &mut [[f32; 3]], width: usize, height: usize, gain: &GainImage, m: &GainMapMeta, weight: f32) -> Result<()> {
|
||||
let n = width.checked_mul(height).ok_or_else(|| Error::Encode("gain map: image too large".into()))?;
|
||||
if base.len() < n
|
||||
|| gain.width == 0
|
||||
|| gain.height == 0
|
||||
|| !matches!(gain.channels, 1 | 3)
|
||||
|| gain.data.len() < gain.width * gain.height * gain.channels
|
||||
{
|
||||
return Err(Error::Encode("gain map: base and gain map sizes don't match".into()));
|
||||
}
|
||||
if !m.is_valid() {
|
||||
return Err(Error::Encode("gain map: unusable metadata".into()));
|
||||
}
|
||||
let weight = if weight.is_finite() { weight.clamp(0.0, 1.0) } else { 1.0 };
|
||||
let (sx, sy) = (gain.width as f32 / width as f32, gain.height as f32 / height as f32);
|
||||
let inv_gamma = m.gamma.map(|g| 1.0 / g);
|
||||
for (i, p) in base.iter_mut().enumerate().take(n) {
|
||||
let (x, y) = ((i % width) as f32 + 0.5, (i / width) as f32 + 0.5);
|
||||
let (gx, gy) = (x * sx, y * sy);
|
||||
let mut g = [0f32; 3];
|
||||
for (c, gc) in g.iter_mut().enumerate() {
|
||||
let src = if gain.channels == 3 { c } else { 0 };
|
||||
let norm = (gain.sample(gx, gy, src) / 255.0).clamp(0.0, 1.0).powf(inv_gamma[c]);
|
||||
*gc = m.min[c] + (m.max[c] - m.min[c]) * norm;
|
||||
}
|
||||
for c in 0..3 {
|
||||
let v = (p[c] + m.base_offset[c]) * (g[c] * weight).exp2() - m.alternate_offset[c];
|
||||
p[c] = v.max(0.0);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The primary image's container XMP: `hdrgm:Version` (marks the file as a gain map image for
|
||||
/// Adobe/Google readers) and the Container directory naming the gain map and its length.
|
||||
fn primary_description(gain_len: usize) -> String {
|
||||
format!(
|
||||
concat!(
|
||||
r#"<rdf:Description rdf:about="" xmlns:Container="http://ns.google.com/photos/1.0/container/" xmlns:Item="http://ns.google.com/photos/1.0/container/item/" xmlns:hdrgm="http://ns.adobe.com/hdr-gain-map/1.0/" hdrgm:Version="1.0">"#,
|
||||
r#"<Container:Directory><rdf:Seq>"#,
|
||||
r#"<rdf:li rdf:parseType="Resource"><Container:Item Item:Semantic="Primary" Item:Mime="image/jpeg"/></rdf:li>"#,
|
||||
r#"<rdf:li rdf:parseType="Resource"><Container:Item Item:Semantic="GainMap" Item:Mime="image/jpeg" Item:Length="{}"/></rdf:li>"#,
|
||||
r#"</rdf:Seq></Container:Directory></rdf:Description>"#
|
||||
),
|
||||
gain_len
|
||||
)
|
||||
}
|
||||
|
||||
/// `xmp` with `description` added as another `rdf:Description` (a fresh packet when there is
|
||||
/// none, or when it has no `rdf:RDF` to add to).
|
||||
fn merge_xmp(xmp: Option<&str>, description: &str) -> String {
|
||||
if let Some(x) = xmp
|
||||
&& let Some(i) = x.rfind("</rdf:RDF>")
|
||||
&& let (Some(a), Some(b)) = (x.get(..i), x.get(i..))
|
||||
{
|
||||
return format!("{a}{description}{b}");
|
||||
}
|
||||
format!("{XMP_OPEN}{description}{XMP_CLOSE}")
|
||||
}
|
||||
|
||||
/// Offsets within the MPF payload (from "MPF\0"): the endian field, then the index IFD (3 tags).
|
||||
const MPF_ENTRIES_FROM_ENDIAN: usize = 8 + 2 + 3 * 12 + 4;
|
||||
|
||||
@@ -585,6 +59,10 @@ pub fn encode_jpeg(base: &EncodeImage, gain: &GainImage, m: &GainMapMeta, qualit
|
||||
if !matches!(gain.channels, 1 | 3) || gain.width == 0 || gain.height == 0 || gain.data.len() < gain.width * gain.height * gain.channels {
|
||||
return Err(Error::Encode("gain map image is malformed".into()));
|
||||
}
|
||||
// (jpeg_par::encode gives an empty JPEG past this, which would be appended as a size-0 gain map)
|
||||
if gain.width > u16::MAX as usize || gain.height > u16::MAX as usize {
|
||||
return Err(Error::Encode("JPEG dimensions are limited to 65535".into()));
|
||||
}
|
||||
if !m.is_valid() || m.multichannel != (gain.channels == 3) {
|
||||
return Err(Error::Encode("gain map metadata doesn't match the map".into()));
|
||||
}
|
||||
@@ -594,11 +72,9 @@ pub fn encode_jpeg(base: &EncodeImage, gain: &GainImage, m: &GainMapMeta, qualit
|
||||
let gain_segs = vec![(0xE1, xmp_segment(&m.to_gain_map_xmp())?), (0xE2, m.to_iso())];
|
||||
let gain_jpeg = jpeg_par::encode(&gain.data, gain.width, gain.height, gain.channels, quality, ChromaSubsampling::S444, &gain_segs);
|
||||
|
||||
let xmp = merge_xmp(meta.xmp, &primary_description(gain_jpeg.len()));
|
||||
let xmp = craft_gainmap::primary_xmp(meta.xmp, gain_jpeg.len());
|
||||
let mut segs = jpeg_app_segments(&EncodeMeta { xmp: Some(&xmp), ..*meta })?;
|
||||
let mut iso_marker = ISO_URN.to_vec();
|
||||
iso_marker.extend_from_slice(&[0, 0, 0, 0]); // versions only: "this image has a gain map"
|
||||
segs.push((0xE2, iso_marker));
|
||||
segs.push((0xE2, craft_gainmap::primary_iso_marker()));
|
||||
segs.push((0xE2, mpf_placeholder()));
|
||||
let mut primary = jpeg_par::encode(data, w, h, base.channels as usize, quality, sub, &segs);
|
||||
|
||||
@@ -668,77 +144,6 @@ pub fn is_gain_map_jpeg(bytes: &[u8]) -> bool {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn meta3() -> GainMapMeta {
|
||||
GainMapMeta {
|
||||
multichannel: true,
|
||||
use_base_colour_space: true,
|
||||
base_headroom: 0.0,
|
||||
alternate_headroom: 2.5,
|
||||
min: [-0.25, -0.5, 0.0],
|
||||
max: [2.5, 2.25, 2.0],
|
||||
gamma: [1.0, 1.25, 0.75],
|
||||
base_offset: [0.015625; 3],
|
||||
alternate_offset: [0.015625, 0.03125, 0.015625],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iso_metadata_round_trips() {
|
||||
for m in [GainMapMeta { alternate_headroom: 3.0, max: [3.0; 3], ..GainMapMeta::default() }, meta3()] {
|
||||
let p = m.to_iso();
|
||||
assert!(p.starts_with(ISO_URN));
|
||||
let back = GainMapMeta::from_iso(&p[ISO_URN.len()..]).unwrap();
|
||||
for (a, b) in [(back.min, m.min), (back.max, m.max), (back.gamma, m.gamma), (back.alternate_offset, m.alternate_offset)] {
|
||||
for c in 0..3 {
|
||||
assert!((a[c] - b[c]).abs() < 1e-6, "{a:?} {b:?}");
|
||||
}
|
||||
}
|
||||
assert_eq!(back.multichannel, m.multichannel);
|
||||
assert!((back.alternate_headroom - m.alternate_headroom).abs() < 1e-6);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iso_common_denominator_and_rejections() {
|
||||
// common-denominator form, single channel: d = 4, headrooms 0 and 2, min 0, max 2, gamma 1, offsets 0
|
||||
let mut p = vec![0, 0, 0, 0, FLAG_COMMON_DENOMINATOR];
|
||||
for v in [4u32, 0, 8, 0, 8, 4, 0, 0] {
|
||||
p.extend_from_slice(&v.to_be_bytes());
|
||||
}
|
||||
let m = GainMapMeta::from_iso(&p).unwrap();
|
||||
assert_eq!((m.alternate_headroom, m.max[2], m.gamma[1]), (2.0, 2.0, 1.0));
|
||||
// versions only (a primary image's marker), unknown version, truncated, backward
|
||||
assert!(GainMapMeta::from_iso(&[0, 0, 0, 0]).is_none());
|
||||
assert!(GainMapMeta::from_iso(&[0, 1, 0, 0, 0]).is_none());
|
||||
assert!(GainMapMeta::from_iso(&p[..p.len() - 1]).is_none());
|
||||
let mut back = p.clone();
|
||||
back[4] |= FLAG_BACKWARD;
|
||||
assert!(GainMapMeta::from_iso(&back).is_none());
|
||||
// a zero denominator is rejected, not divided by
|
||||
let mut z = p.clone();
|
||||
z[5..9].copy_from_slice(&0u32.to_be_bytes());
|
||||
assert!(GainMapMeta::from_iso(&z).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hdrgm_xmp_round_trips_including_per_channel_sequences() {
|
||||
for m in [GainMapMeta { alternate_headroom: 2.0, max: [2.0; 3], ..GainMapMeta::default() }, meta3()] {
|
||||
let x = m.to_gain_map_xmp();
|
||||
assert!(x.contains("apdi:AuxiliaryImageType"));
|
||||
if m.multichannel {
|
||||
assert!(x.contains("<hdrgm:GainMapMax><rdf:Seq><rdf:li>2.5</rdf:li>"), "{x}");
|
||||
}
|
||||
let back = GainMapMeta::from_hdrgm_xmp(&x).unwrap();
|
||||
assert_eq!(back.multichannel, m.multichannel);
|
||||
for c in 0..3 {
|
||||
assert!((back.max[c] - m.max[c]).abs() < 1e-5 && (back.gamma[c] - m.gamma[c]).abs() < 1e-5);
|
||||
}
|
||||
assert!((back.alternate_headroom - m.alternate_headroom).abs() < 1e-5);
|
||||
}
|
||||
assert!(GainMapMeta::from_hdrgm_xmp("<x hdrgm:Version=\"1.0\"/>").is_none(), "no GainMapMax");
|
||||
assert!(GainMapMeta::from_hdrgm_xmp("hdrgm:GainMapMax=\"nope\"").is_none());
|
||||
}
|
||||
|
||||
fn renditions(w: usize, h: usize) -> (Vec<[f32; 3]>, Vec<[f32; 3]>) {
|
||||
// a ramp: SDR rolls off at 1, HDR keeps going to 6
|
||||
let hdr: Vec<[f32; 3]> = (0..w * h)
|
||||
@@ -762,28 +167,18 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compute_then_apply_recovers_the_hdr_rendition() {
|
||||
let (w, h) = (64, 8);
|
||||
let (sdr, hdr) = renditions(w, h);
|
||||
for multichannel in [false, true] {
|
||||
let o = GainMapOptions { scale: 1, multichannel, ..GainMapOptions::default() };
|
||||
let (g, m) = compute(&sdr, &hdr, w, h, [0.2126, 0.7152, 0.0722], &o).unwrap();
|
||||
assert!(m.is_valid() && m.alternate_headroom > 1.0);
|
||||
let mut out = sdr.clone();
|
||||
apply(&mut out, w, h, &g, &m, 1.0).unwrap();
|
||||
for (a, b) in out.iter().zip(&hdr) {
|
||||
for c in 0..3 {
|
||||
// 8-bit codes over the gain range: within ~2 % in linear light
|
||||
assert!((a[c] - b[c]).abs() <= 0.03 * b[c] + 0.01, "{multichannel}: {a:?} vs {b:?}");
|
||||
}
|
||||
}
|
||||
// weight 0 is the base
|
||||
let mut base = sdr.clone();
|
||||
apply(&mut base, w, h, &g, &m, 0.0).unwrap();
|
||||
for (a, b) in base.iter().zip(&sdr) {
|
||||
assert!((a[0] - b[0]).abs() < 1e-5);
|
||||
}
|
||||
}
|
||||
fn a_gain_map_wider_than_a_jpeg_allows_is_refused() {
|
||||
let base = [0u8; 3];
|
||||
let gain = GainImage { width: 65_536, height: 1, channels: 1, data: vec![0; 65_536] };
|
||||
let r = encode_jpeg(
|
||||
&EncodeImage::new(1, 1, 3, Samples::U8(&base)),
|
||||
&gain,
|
||||
&GainMapMeta::default(),
|
||||
90,
|
||||
ChromaSubsampling::S444,
|
||||
&EncodeMeta::default(),
|
||||
);
|
||||
assert!(matches!(r, Err(Error::Encode(m)) if m.contains("65535")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! HEIF / HEIC (the iPhone and Mac photo format), read-only.
|
||||
//!
|
||||
//! Decoding lives in the optional `lightcraft-heif` crate (heic-rs, a pure-Rust HEVC
|
||||
//! Decoding lives in the optional `craft-heif` crate (heic-rs, a pure-Rust HEVC
|
||||
//! still-picture decoder, plus a libheif-matching colour conversion), enabled by this crate's
|
||||
//! `heif` feature — off by default because HEVC is patent-encumbered and whether a build carries
|
||||
//! an HEVC decoder is the distributor's call (the same policy as the sibling PhotoCraft). Without
|
||||
@@ -35,10 +35,10 @@ pub(crate) fn header(_bytes: &[u8]) -> Result<(u32, u32, u16)> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "heif")]
|
||||
fn err(e: lightcraft_heif::Error) -> Error {
|
||||
fn err(e: craft_heif::Error) -> Error {
|
||||
match e {
|
||||
lightcraft_heif::Error::Unsupported(why) => Error::Unsupported(F, why),
|
||||
lightcraft_heif::Error::Limit(m) | lightcraft_heif::Error::Malformed(m) => Error::Malformed(F, m),
|
||||
craft_heif::Error::Unsupported(why) => Error::Unsupported(F, why),
|
||||
craft_heif::Error::Limit(m) | craft_heif::Error::Malformed(m) => Error::Malformed(F, m),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,24 +46,24 @@ fn err(e: lightcraft_heif::Error) -> Error {
|
||||
/// alone: no picture is decoded.
|
||||
#[cfg(feature = "heif")]
|
||||
pub(crate) fn header(bytes: &[u8]) -> Result<(u32, u32, u16)> {
|
||||
let info = lightcraft_heif::probe(bytes).map_err(err)?;
|
||||
let info = craft_heif::probe(bytes).map_err(err)?;
|
||||
Ok((info.width, info.height, 1))
|
||||
}
|
||||
|
||||
#[cfg(feature = "heif")]
|
||||
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
|
||||
// The container alone: the declared size is checked before any pixel is decoded.
|
||||
let info = lightcraft_heif::probe(bytes).map_err(err)?;
|
||||
let info = craft_heif::probe(bytes).map_err(err)?;
|
||||
if (info.width as u64).saturating_mul(info.height as u64) > opts.max_pixels {
|
||||
return Err(Error::TooLarge(info.width as u64, info.height as u64));
|
||||
}
|
||||
let options = lightcraft_heif::Options { max_pixels: opts.max_pixels, apply_transforms: true };
|
||||
let options = craft_heif::Options { max_pixels: opts.max_pixels, apply_transforms: true, ..craft_heif::Options::default() };
|
||||
// A small decode (a thumbnail or a cataloguing probe) uses the file's own thumbnail image when
|
||||
// it is at least as large as asked: iPhones store one of 320 × 240, and decoding it is a
|
||||
// hundredth of the work of the 12 MP photo.
|
||||
let thumb = match opts.max_size {
|
||||
Some((mw, mh)) if mw > 0 && mh > 0 && mw.max(mh) < info.width.max(info.height) => {
|
||||
lightcraft_heif::decode_thumbnail(bytes, mw.max(mh), &options).ok().flatten().filter(|t| {
|
||||
craft_heif::decode_thumbnail(bytes, mw.max(mh), &options).ok().flatten().filter(|t| {
|
||||
// Only a thumbnail of the same picture: same orientation and aspect within 1 %.
|
||||
let aspect = |w: u32, h: u32| w as f64 / h.max(1) as f64;
|
||||
(aspect(t.width, t.height) / aspect(info.width, info.height) - 1.0).abs() < 0.01
|
||||
@@ -73,7 +73,7 @@ pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
|
||||
};
|
||||
let mut decoded = match thumb {
|
||||
Some(t) => t,
|
||||
None => lightcraft_heif::decode(bytes, &options).map_err(err)?,
|
||||
None => craft_heif::decode(bytes, &options).map_err(err)?,
|
||||
};
|
||||
let raw = Raw {
|
||||
width: decoded.width as usize,
|
||||
|
||||
@@ -401,6 +401,12 @@ fn encode_with(
|
||||
app_segments: &[(u8, Vec<u8>)],
|
||||
optimize: bool,
|
||||
) -> Vec<u8> {
|
||||
// The public entry takes caller-sized dimensions: refuse (an empty output) what would size an allocation from
|
||||
// them or index past `data`, rather than panic or allocate for them.
|
||||
let fits = width.checked_mul(height).and_then(|n| n.checked_mul(channels)).is_some_and(|n| n <= data.len());
|
||||
if !fits || width == 0 || height == 0 || width > 65_535 || height > 65_535 || !matches!(channels, 1 | 3 | 4) {
|
||||
return Vec::new();
|
||||
}
|
||||
let gray = channels == 1;
|
||||
let (hs, vs) = match (gray, sub) {
|
||||
(true, _) | (false, ChromaSubsampling::S444) => (1usize, 1usize),
|
||||
@@ -586,6 +592,13 @@ fn encode_with(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn hostile_dimensions_give_no_output_instead_of_a_panic_or_allocation() {
|
||||
for (w, h, c) in [(1usize << 40, 1usize << 40, 3usize), (0, 8, 3), (70_000, 2, 3), (8, 8, 7), (8, 8, 3)] {
|
||||
assert!(encode(&[0u8; 16], w, h, c, 90, ChromaSubsampling::S444, &[]).is_empty(), "{w} x {h} x {c}");
|
||||
}
|
||||
}
|
||||
|
||||
fn test_rgb(w: usize, h: usize) -> Vec<u8> {
|
||||
let mut v = Vec::with_capacity(w * h * 3);
|
||||
for y in 0..h {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//! HEIC/HEIF through the codec API (feature `heif`), on lossless synthetic files from
|
||||
//! `lightcraft_heif::testdata`: an iPhone-like grid with a Display P3 profile, rotation, EXIF, a
|
||||
//! `craft_heif::testdata`: an iPhone-like grid with a Display P3 profile, rotation, EXIF, a
|
||||
//! thumbnail and an HDR gain map; `nclx`-only colour; 10-bit; and hostile input.
|
||||
|
||||
use craft_heif::testdata::{Prop, Spec, build};
|
||||
use lightcraft_codecs::*;
|
||||
use lightcraft_heif::testdata::{Prop, Spec, build};
|
||||
|
||||
fn grey(x: u32, y: u32) -> [u16; 3] {
|
||||
[((x * 7 + y * 13) % 220 + 20) as u16, 128, 128]
|
||||
|
||||
@@ -28,6 +28,7 @@ pub enum Section {
|
||||
Calibration,
|
||||
PointColor,
|
||||
RedEye,
|
||||
LensBlur,
|
||||
}
|
||||
|
||||
impl Section {
|
||||
@@ -49,6 +50,7 @@ impl Section {
|
||||
Section::Calibration => "Calibration",
|
||||
Section::PointColor => "Point Color",
|
||||
Section::RedEye => "Red Eye",
|
||||
Section::LensBlur => "Lens Blur",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -286,6 +288,9 @@ controls! {
|
||||
"geometry.scale" => geometry.scale, "Scale", Geometry, 50, 150, 100, 1, 0, Plain;
|
||||
"geometry.offsetX" => geometry.offset_x, "Offset X", Geometry, -100, 100, 0, 0.1, 1, Centered;
|
||||
"geometry.offsetY" => geometry.offset_y, "Offset Y", Geometry, -100, 100, 0, 0.1, 1, Centered;
|
||||
"lensBlur.amount" => lens_blur.amount, "Blur Amount", LensBlur, 0, 100, 0, 1, 0, Plain;
|
||||
"lensBlur.boost" => lens_blur.boost, "Boost", LensBlur, 0, 100, 0, 1, 0, Plain;
|
||||
"lensBlur.transition" => lens_blur.transition, "Transition", LensBlur, 0, 100, 50, 1, 0, Plain;
|
||||
"calibration.shadowsTint" => calibration.shadows_tint, "Shadows Tint", Calibration, -100, 100, 0, 1, 0, Tint;
|
||||
"calibration.redHue" => calibration.red_hue, "Red Hue", Calibration, -100, 100, 0, 1, 0, Gradient { from: "#e0306a", to: "#e08a30" };
|
||||
"calibration.redSat" => calibration.red_sat, "Red Saturation", Calibration, -100, 100, 0, 1, 0, Gradient { from: "#7a7a7a", to: "#e03a3a" };
|
||||
@@ -450,9 +455,16 @@ fn indexed_field<'a>(s: &'a mut DevelopSettings, id: &str) -> Option<&'a mut f64
|
||||
}
|
||||
|
||||
fn indexed_get(s: &DevelopSettings, id: &str) -> Option<f64> {
|
||||
// (a clone keeps one accessor for get and set; these lists are tiny)
|
||||
let mut c = DevelopSettings { point_colors: s.point_colors.clone(), red_eye: s.red_eye.clone(), ..DevelopSettings::default() };
|
||||
indexed_field(&mut c, id).map(|v| *v)
|
||||
// one accessor for get and set: a copy of the one element read (never of the lists, which a
|
||||
// read of every indexed control would copy once per control)
|
||||
let (fam, i, field) = split_indexed(id)?;
|
||||
let mut c = DevelopSettings::default();
|
||||
match fam {
|
||||
"pointColor" => c.point_colors.push(*s.point_colors.get(i)?),
|
||||
"redEye" => c.red_eye.push(*s.red_eye.get(i)?),
|
||||
_ => return None,
|
||||
}
|
||||
indexed_field(&mut c, &format!("{fam}.0.{field}")).map(|v| *v)
|
||||
}
|
||||
|
||||
fn indexed_set(s: &mut DevelopSettings, id: &str, v: f64) -> bool {
|
||||
|
||||
@@ -200,6 +200,14 @@ impl DevelopSettings {
|
||||
}
|
||||
}
|
||||
|
||||
/// The middle of the crop, in normalized photo coordinates (the photo's middle when the crop
|
||||
/// isn't a usable rectangle).
|
||||
pub fn crop_center(&self) -> lightcraft_geom::Point {
|
||||
let r = self.crop.geometry.rect;
|
||||
let (x, y) = ((r.x0 + r.x1) / 2.0, (r.y0 + r.y1) / 2.0);
|
||||
if x.is_finite() && y.is_finite() { lightcraft_geom::Point::new(x, y) } else { lightcraft_geom::Point::new(0.5, 0.5) }
|
||||
}
|
||||
|
||||
/// Next free mask id: one past the highest, or, when that is `u32::MAX` (ids come from files,
|
||||
/// pastes and agents), the lowest one no mask uses.
|
||||
pub fn next_mask_id(&self) -> u32 {
|
||||
|
||||
@@ -313,13 +313,28 @@ impl Preset {
|
||||
/// The preset's masks, added to the photo's own (ones it already has are skipped), their effect
|
||||
/// scaled by the amount.
|
||||
fn add_masks(out: &mut DevelopSettings, masks: &[Value], amount: f64) {
|
||||
// the masks by name, kept up to date: a preset of many masks compares each with its
|
||||
// namesakes only, not with every mask added before it
|
||||
let mut by_name: std::collections::HashMap<String, Vec<usize>> = std::collections::HashMap::new();
|
||||
for (i, q) in out.masks.iter().enumerate() {
|
||||
by_name.entry(q.name.clone()).or_default().push(i);
|
||||
}
|
||||
let mut next_id = out.next_mask_id();
|
||||
let mut used: std::collections::HashSet<u32> = out.masks.iter().map(|q| q.id).collect();
|
||||
for m in masks {
|
||||
let Ok(mut m) = serde_json::from_value::<crate::Mask>(m.clone()) else { continue };
|
||||
if out.masks.iter().any(|q| q.name == m.name && q.components == m.components) {
|
||||
if by_name.get(&m.name).is_some_and(|ix| ix.iter().any(|&i| out.masks.get(i).is_some_and(|q| q.components == m.components))) {
|
||||
continue;
|
||||
}
|
||||
m.id = out.next_mask_id();
|
||||
// ids run on from the first free one, past any a mask already has (a scan for the
|
||||
// next free id per mask would be another pass over them)
|
||||
while used.contains(&next_id) {
|
||||
next_id = next_id.checked_add(1).unwrap_or(1);
|
||||
}
|
||||
m.id = next_id;
|
||||
used.insert(next_id);
|
||||
m.adjust.amount = (m.adjust.amount * amount).clamp(0.0, 200.0);
|
||||
by_name.entry(m.name.clone()).or_default().push(out.masks.len());
|
||||
out.masks.push(m);
|
||||
}
|
||||
}
|
||||
@@ -604,4 +619,22 @@ mod tests {
|
||||
ids.dedup();
|
||||
assert_eq!(ids.len(), 3, "{:?}", out.masks);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preset_masks_skip_ids_an_existing_mask_has_after_the_first_free_one() {
|
||||
// the first free id is 1, and 2 is taken: the second added mask must not get 2
|
||||
let theirs = |id| crate::Mask { id, name: format!("T{id}"), ..Default::default() };
|
||||
let s = DevelopSettings { masks: vec![theirs(u32::MAX), theirs(2)], ..Default::default() };
|
||||
let p = Preset {
|
||||
id: "p".into(),
|
||||
name: "P".into(),
|
||||
group: "User".into(),
|
||||
settings: json!({"masks": [{"name": "A"}, {"name": "B"}]}),
|
||||
favorite: false,
|
||||
builtin: false,
|
||||
};
|
||||
let mut ids: Vec<u32> = p.apply(&s, 1.0).masks.iter().map(|m| m.id).collect();
|
||||
ids.sort_unstable();
|
||||
assert_eq!(ids, [1, 2, 3, u32::MAX]);
|
||||
}
|
||||
}
|
||||
@@ -96,12 +96,18 @@ pub enum Process {
|
||||
/// V5, with colour noise reduction on the linear colour differences R−Y and B−Y, leaving Y alone, with
|
||||
/// edge-aware weights from a denoised guide and a brake for isolated colour specks (issue #815).
|
||||
V6,
|
||||
/// V6, with film grain from a stack of crystal layers (after Aurélien Pierre's model): resolution
|
||||
/// independent, fading out at black and white; V1 to V6 keep their value noise.
|
||||
V7,
|
||||
/// V7, with the Highlights/Shadows base taken of log luminance without its fine detail (clamped
|
||||
/// between a fine and a 3× wider median), so gaps in dark lettering aren't lifted into bright blocks.
|
||||
V8,
|
||||
}
|
||||
|
||||
impl Process {
|
||||
/// Every process, oldest first.
|
||||
pub const ALL: [Process; 6] = [Process::V1, Process::V2, Process::V3, Process::V4, Process::V5, Process::V6];
|
||||
pub const LATEST: Process = Process::V6;
|
||||
pub const ALL: [Process; 8] = [Process::V1, Process::V2, Process::V3, Process::V4, Process::V5, Process::V6, Process::V7, Process::V8];
|
||||
pub const LATEST: Process = Process::V8;
|
||||
|
||||
pub const fn version(self) -> ProcessVersion {
|
||||
match self {
|
||||
@@ -111,6 +117,8 @@ impl Process {
|
||||
Process::V4 => ProcessVersion(4),
|
||||
Process::V5 => ProcessVersion(5),
|
||||
Process::V6 => ProcessVersion(6),
|
||||
Process::V7 => ProcessVersion(7),
|
||||
Process::V8 => ProcessVersion(8),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -890,6 +898,25 @@ pub struct LocalAdjustments {
|
||||
pub noise: f64,
|
||||
pub moire: f64,
|
||||
pub defringe: f64,
|
||||
/// Lens Blur inside the mask, 0..100 (`lightcraft_pipeline::lens_blur`): disc (bokeh) blur
|
||||
/// whose radius follows the mask's strength. Omitted while 0, so masks without it keep their
|
||||
/// JSON (and hash).
|
||||
#[serde(skip_serializing_if = "is_zero")]
|
||||
pub lens_blur: f64,
|
||||
/// The mask's Lens Blur block switched off (its values kept).
|
||||
#[serde(skip_serializing_if = "std::ops::Not::not")]
|
||||
pub lens_blur_off: bool,
|
||||
/// Blur what the mask leaves out instead of what it selects (the mask's other adjustments
|
||||
/// still go where it selects).
|
||||
#[serde(skip_serializing_if = "std::ops::Not::not")]
|
||||
pub lens_blur_outside: bool,
|
||||
/// Brighter bokeh highlights in the mask's blur, 0..100.
|
||||
#[serde(skip_serializing_if = "is_zero")]
|
||||
pub lens_blur_boost: f64,
|
||||
/// How gradually the mask's blur grows away from what stays sharp, 0..100 (0: it follows the
|
||||
/// mask's own edge).
|
||||
#[serde(skip_serializing_if = "is_zero")]
|
||||
pub lens_blur_transition: f64,
|
||||
/// Colour overlay (hue degrees, saturation 0..100); sat 0 = none.
|
||||
pub color_hue: f64,
|
||||
pub color_sat: f64,
|
||||
@@ -917,6 +944,11 @@ impl Default for LocalAdjustments {
|
||||
noise: 0.0,
|
||||
moire: 0.0,
|
||||
defringe: 0.0,
|
||||
lens_blur: 0.0,
|
||||
lens_blur_off: false,
|
||||
lens_blur_outside: false,
|
||||
lens_blur_boost: 0.0,
|
||||
lens_blur_transition: 0.0,
|
||||
color_hue: 0.0,
|
||||
color_sat: 0.0,
|
||||
amount: 100.0,
|
||||
@@ -1258,13 +1290,157 @@ impl Default for RedEye {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
/// Lens Blur: a shallow depth of field added after the fact (`lightcraft_pipeline::lens_blur`).
|
||||
/// What stays sharp is the photo's subject, or the selection of the mask [`LensBlur::focus_mask`]
|
||||
/// names; everything else gets disc-shaped (bokeh) blur up to [`LensBlur::amount`].
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct LensBlur {
|
||||
pub enabled: bool,
|
||||
/// Blur of what is out of focus, 0..100 (100: a disc of 3.5 % of the long edge).
|
||||
pub amount: f64,
|
||||
/// Reserved for a depth map (not rendered yet).
|
||||
pub focal_distance: f64,
|
||||
/// Reserved for a depth map (not rendered yet).
|
||||
pub focal_range: f64,
|
||||
/// How much brighter the out-of-focus highlights turn into bokeh discs, 0..100. Omitted while
|
||||
/// 0, so settings without it keep their JSON (and hash).
|
||||
#[serde(skip_serializing_if = "is_zero")]
|
||||
pub boost: f64,
|
||||
/// The id of the mask whose selection stays in focus (any mask, shown or hidden); `None`, or a
|
||||
/// mask that no longer exists: the photo's subject.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub focus_mask: Option<u32>,
|
||||
/// Brush Refinement, Focus brush: strokes painted in focus (their strength: how far; erase
|
||||
/// strokes take it back). Omitted while empty.
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
pub focus_strokes: Vec<BrushStroke>,
|
||||
/// Brush Refinement, Blur brush: strokes painted out of focus, over the focus and the Focus
|
||||
/// brush. Omitted while empty.
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
pub blur_strokes: Vec<BrushStroke>,
|
||||
/// How gradually the blur grows away from what is in focus, 0..100: 0 a sharp cut at the
|
||||
/// focus's edge, higher a longer ramp (a stand-in for depth until there is a depth map).
|
||||
/// Omitted at its default, so settings without it keep their JSON (and hash).
|
||||
#[serde(skip_serializing_if = "LensBlur::is_default_transition")]
|
||||
pub transition: f64,
|
||||
/// What stays in focus without a focus mask: the subject estimate, or a fixed shape in the
|
||||
/// middle of the framing. Omitted while automatic.
|
||||
#[serde(skip_serializing_if = "FocusShape::is_auto")]
|
||||
pub shape: FocusShape,
|
||||
/// Where a fixed shape sits and how large it is, once moved or resized on the photo; `None`:
|
||||
/// the shape's preset, in the middle of the crop.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub area: Option<FocusArea>,
|
||||
}
|
||||
|
||||
/// A fixed focus shape on the photo: an upright ellipse.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct FocusArea {
|
||||
/// Normalized (uncropped, oriented) photo coordinates, as mask shapes.
|
||||
pub center: Point,
|
||||
/// Half-width and half-height, in units of the photo's long edge.
|
||||
pub rx: f64,
|
||||
pub ry: f64,
|
||||
}
|
||||
|
||||
impl FocusArea {
|
||||
/// Smallest and largest radius (long-edge units).
|
||||
pub const MIN_RADIUS: f64 = 0.02;
|
||||
pub const MAX_RADIUS: f64 = 1.0;
|
||||
|
||||
/// Usable values: finite, the radii within range, the centre no farther than a photo's width
|
||||
/// outside it. A hostile value falls back to the middle and the default size.
|
||||
pub fn sanitized(self) -> FocusArea {
|
||||
let r = |v: f64, d: f64| if v.is_finite() { v.clamp(Self::MIN_RADIUS, Self::MAX_RADIUS) } else { d };
|
||||
let c = |v: f64| if v.is_finite() { v.clamp(-1.0, 2.0) } else { 0.5 };
|
||||
FocusArea { center: Point::new(c(self.center.x), c(self.center.y)), rx: r(self.rx, 0.22), ry: r(self.ry, 0.22) }
|
||||
}
|
||||
}
|
||||
|
||||
impl LensBlur {
|
||||
/// The fixed focus shape in effect, if any: the moved / resized [`LensBlur::area`], else the
|
||||
/// shape's preset centred on `crop_center`. `None` for [`FocusShape::Auto`].
|
||||
pub fn focus_area(&self, crop_center: Point) -> Option<FocusArea> {
|
||||
let (rx, ry) = self.shape.radii()?;
|
||||
Some(self.area.unwrap_or(FocusArea { center: crop_center, rx, ry }).sanitized())
|
||||
}
|
||||
}
|
||||
|
||||
/// The focus area [`LensBlur`] uses when no mask says what is in focus.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum FocusShape {
|
||||
/// A circle.
|
||||
Round,
|
||||
/// An oval lying down (landscape).
|
||||
Wide,
|
||||
/// An oval standing up (portrait).
|
||||
Tall,
|
||||
/// The photo's subject, estimated (person mattes, else a saliency heuristic). Also what a
|
||||
/// shape this build doesn't know reads as (so it is last: serde's `other`).
|
||||
#[default]
|
||||
#[serde(other)]
|
||||
Auto,
|
||||
}
|
||||
|
||||
impl FocusShape {
|
||||
pub const ALL: [FocusShape; 4] = [FocusShape::Auto, FocusShape::Round, FocusShape::Wide, FocusShape::Tall];
|
||||
|
||||
/// Whether this is the default (Auto: no fixed shape), for skipping it in the JSON.
|
||||
fn is_auto(&self) -> bool {
|
||||
*self == FocusShape::Auto
|
||||
}
|
||||
|
||||
/// The fixed shape's half-width and half-height, in units of the photo's long edge; `None`
|
||||
/// for [`FocusShape::Auto`].
|
||||
pub fn radii(self) -> Option<(f64, f64)> {
|
||||
match self {
|
||||
FocusShape::Auto => None,
|
||||
FocusShape::Round => Some((0.22, 0.22)),
|
||||
FocusShape::Wide => Some((0.32, 0.17)),
|
||||
FocusShape::Tall => Some((0.17, 0.32)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The name shown on the shape's button (translated by the UI).
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
FocusShape::Auto => "Auto",
|
||||
FocusShape::Round => "Round",
|
||||
FocusShape::Wide => "Horizontal",
|
||||
FocusShape::Tall => "Vertical",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LensBlur {
|
||||
/// [`LensBlur::transition`]'s default.
|
||||
pub const DEFAULT_TRANSITION: f64 = 50.0;
|
||||
|
||||
/// Whether `v` is [`Self::DEFAULT_TRANSITION`], for skipping it in the JSON.
|
||||
fn is_default_transition(v: &f64) -> bool {
|
||||
*v == Self::DEFAULT_TRANSITION
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for LensBlur {
|
||||
/// Off, with the default Transition and everything else empty.
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enabled: false,
|
||||
amount: 0.0,
|
||||
focal_distance: 0.0,
|
||||
focal_range: 0.0,
|
||||
boost: 0.0,
|
||||
focus_mask: None,
|
||||
focus_strokes: Vec::new(),
|
||||
blur_strokes: Vec::new(),
|
||||
transition: Self::DEFAULT_TRANSITION,
|
||||
shape: FocusShape::Auto,
|
||||
area: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
|
||||
@@ -89,13 +89,13 @@ fn v1_is_left_out_when_saving_and_other_processes_are_kept() {
|
||||
#[test]
|
||||
fn unknown_process_numbers_load_and_render_with_the_newest_known() {
|
||||
// written by a newer LightCraft: kept as it is, rendered with the latest process this build has
|
||||
let s = DevelopSettings::from_json(&json!({"process": 7, "light": {"exposure": 0.5}})).unwrap();
|
||||
assert_eq!(s.process, ProcessVersion(7));
|
||||
let s = DevelopSettings::from_json(&json!({"process": LATEST_PLUS_ONE, "light": {"exposure": 0.5}})).unwrap();
|
||||
assert_eq!(s.process, ProcessVersion(LATEST_PLUS_ONE));
|
||||
assert_eq!(s.process.process(), Process::LATEST);
|
||||
assert!(!s.process.is_known());
|
||||
assert!(!s.process.is_outdated(), "never downgraded by Update to Current Process");
|
||||
assert_eq!(s.to_json()["process"], json!(7));
|
||||
assert_eq!(s.merged(&json!({"light": {"exposure": 1.0}})).unwrap().process, ProcessVersion(7));
|
||||
assert_eq!(s.to_json()["process"], json!(LATEST_PLUS_ONE));
|
||||
assert_eq!(s.merged(&json!({"light": {"exposure": 1.0}})).unwrap().process, ProcessVersion(LATEST_PLUS_ONE));
|
||||
// below V1 (no LightCraft writes it): rendered as V1, and updatable
|
||||
let zero = DevelopSettings::from_json(&json!({"process": 0})).unwrap().process;
|
||||
assert_eq!((zero.process(), zero.is_known(), zero.is_outdated()), (Process::V1, false, true));
|
||||
|
||||
@@ -29,6 +29,8 @@ lightcraft-meta = { workspace = true }
|
||||
lightcraft-lensdb = { workspace = true }
|
||||
lightcraft-faces = { workspace = true }
|
||||
miniz_oxide = { workspace = true }
|
||||
# CRC-32 of a catalog unpacked from a Lightroom backup zip (lightroom_backup.rs).
|
||||
crc32fast = { workspace = true }
|
||||
lightcraft-merge = { workspace = true }
|
||||
lightcraft-geocode = { workspace = true }
|
||||
lightcraft-slideshow = { workspace = true }
|
||||
@@ -64,6 +66,6 @@ lightcraft-testkit = { workspace = true }
|
||||
# HDR JPEG XL tests read the file's colour encoding
|
||||
jxl-oxide = { workspace = true }
|
||||
# Lossless synthetic HEIC files (the import test; decoded when the codecs' `heif` feature is on).
|
||||
lightcraft-heif = { workspace = true, features = ["testdata"] }
|
||||
craft-heif = { workspace = true, features = ["testdata"] }
|
||||
libc = { workspace = true }
|
||||
lightcraft-tiff = { workspace = true }
|
||||
craft-tiff = { workspace = true }
|
||||
@@ -311,6 +311,13 @@ fn import_presets(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Err(bad(C, "missing `path` / `paths`"));
|
||||
}
|
||||
let (mut imported, mut failed) = (Vec::new(), Vec::new());
|
||||
// the user presets by name, kept up to date: a file of many presets replaces or adds each
|
||||
// without a scan of all the others
|
||||
// (the first of two same-named presets, as `upsert` and the lookups find it)
|
||||
let mut by_name: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
|
||||
for (i, x) in s.curve_presets.iter().enumerate() {
|
||||
by_name.entry(x.name.to_ascii_lowercase()).or_insert(i);
|
||||
}
|
||||
for f in &paths {
|
||||
match std::fs::read(f).map_err(|e| e.to_string()).and_then(|b| parse_file(&b)) {
|
||||
Ok(list) => {
|
||||
@@ -321,7 +328,13 @@ fn import_presets(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
preset.name = format!("{} (imported)", preset.name);
|
||||
}
|
||||
imported.push(preset.name.clone());
|
||||
upsert(s, preset);
|
||||
match by_name.get(&preset.name.to_ascii_lowercase()).and_then(|&i| s.curve_presets.get_mut(i)) {
|
||||
Some(x) => *x = preset,
|
||||
None => {
|
||||
by_name.insert(preset.name.to_ascii_lowercase(), s.curve_presets.len());
|
||||
s.curve_presets.push(preset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => failed.push(json!([f, e])),
|
||||
|
||||
@@ -8,6 +8,9 @@ use serde_json::{Value, json};
|
||||
use super::{CommandSpec, always, bad, bool_or, cmd, f64_or, f64_req, has_active, has_clipboard, has_selection, index_req, ok, str_param};
|
||||
use crate::{Result, Session, media::SourceLevel};
|
||||
|
||||
/// Strokes one Lens Blur refinement brush keeps (a document can't grow without bound).
|
||||
const MAX_LENS_BLUR_STROKES: usize = 2000;
|
||||
|
||||
fn active(s: &Session, c: &str) -> Result<PhotoId> {
|
||||
s.active().ok_or_else(|| bad(c, "no active photo"))
|
||||
}
|
||||
@@ -482,6 +485,150 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
Ok(())
|
||||
})
|
||||
}),
|
||||
cmd!(
|
||||
"lensBlur.brushStroke",
|
||||
"Lens Blur Brush Stroke",
|
||||
[],
|
||||
None,
|
||||
"{mode: \"focus\"|\"blur\", points: [[x,y],…] normalized, size?: fraction of long edge (0.03), feather?: 0..100, flow?: 1..100, amount?: 1..100 (how far the stroke refines), erase?: bool, autoMask?: bool, pressure?: [p,…] 0..1 per point} — Lens Blur Brush Refinement: Focus strokes stay sharp, Blur strokes are blurred",
|
||||
has_active,
|
||||
|s, p| {
|
||||
const C: &str = "lensBlur.brushStroke";
|
||||
let focus = match str_param(p, "mode") {
|
||||
Some("focus") => true,
|
||||
Some("blur") => false,
|
||||
_ => return Err(bad(C, "`mode` must be \"focus\" or \"blur\"")),
|
||||
};
|
||||
let points: Vec<Point> = p
|
||||
.get("points")
|
||||
.and_then(Value::as_array)
|
||||
.ok_or_else(|| bad(C, "missing points"))?
|
||||
.iter()
|
||||
.filter_map(|q| Some(Point::new(q.get(0)?.as_f64()?, q.get(1)?.as_f64()?)))
|
||||
.filter(|q| q.x.is_finite() && q.y.is_finite())
|
||||
.collect();
|
||||
if points.is_empty() {
|
||||
return Err(bad(C, "a stroke needs at least one point"));
|
||||
}
|
||||
let d = lightcraft_develop::BrushStroke::default();
|
||||
let num = |key: &str, def: f64, lo: f64, hi: f64| {
|
||||
let v = f64_or(p, key, def);
|
||||
if v.is_finite() { v.clamp(lo, hi) } else { def }
|
||||
};
|
||||
let stroke = lightcraft_develop::BrushStroke {
|
||||
pressure: super::masks::pressure_list(p.get("pressure"), points.len())?,
|
||||
points,
|
||||
size: num("size", d.size, 0.001, 0.5),
|
||||
feather: num("feather", d.feather, 0.0, 100.0),
|
||||
flow: num("flow", d.flow, 1.0, 100.0),
|
||||
density: num("amount", d.density, 1.0, 100.0),
|
||||
erase: bool_or(p, "erase", false),
|
||||
auto_mask: bool_or(p, "autoMask", false),
|
||||
};
|
||||
edit(s, C, if focus { "Lens Blur Focus Brush" } else { "Lens Blur Blur Brush" }, |d| {
|
||||
let strokes = if focus { &mut d.lens_blur.focus_strokes } else { &mut d.lens_blur.blur_strokes };
|
||||
if strokes.len() >= MAX_LENS_BLUR_STROKES {
|
||||
return Err(bad(C, format!("at most {MAX_LENS_BLUR_STROKES} strokes per brush: Reset the brush first")));
|
||||
}
|
||||
strokes.push(stroke);
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
),
|
||||
cmd!(
|
||||
"lensBlur.findSubject",
|
||||
"Find Subject",
|
||||
[],
|
||||
None,
|
||||
"{} — selects the photo's subject with an Object mask (SAM 3) clicked where the camera focused, when the file records it, else at the likeliest subject; Lens Blur is switched on and keeps that mask in focus → {mask, point: [x,y], from: \"camera\"|\"photo\", pending?}",
|
||||
has_active,
|
||||
|s, _| {
|
||||
const C: &str = "lensBlur.findSubject";
|
||||
let id = active(s, C)?;
|
||||
let d = s.develop_of(id).unwrap_or_default();
|
||||
let camera = s.catalog.photo(id).and_then(|p| p.meta.focus.clone());
|
||||
let (point, from) = match camera {
|
||||
// stored on the upright photo: the user's Rotate / Flip on top, as masks are
|
||||
Some(f) => {
|
||||
let r = d.orientation.map_norm_rect(f.rect);
|
||||
(Point::new((r.x0 + r.x1) / 2.0, (r.y0 + r.y1) / 2.0), "camera")
|
||||
}
|
||||
None => {
|
||||
let src = s.source_now(id, SourceLevel::Thumb).map_err(|e| bad(C, e))?;
|
||||
let info = s.source_info(id);
|
||||
let p = lightcraft_pipeline::lens_blur::subject_point(&src, &d, info.mattes.as_deref())
|
||||
.ok_or_else(|| bad(C, "the photo has no picture to look for a subject in"))?;
|
||||
(p, "photo")
|
||||
}
|
||||
};
|
||||
if !(point.x.is_finite() && point.y.is_finite()) {
|
||||
return Err(bad(C, "the camera's focus point is not usable"));
|
||||
}
|
||||
let point = Point::new(point.x.clamp(0.0, 1.0), point.y.clamp(0.0, 1.0));
|
||||
// an Object waiting for the subject (the photo is analyzed meanwhile); the
|
||||
// segmenter fills it with the people / animals the focus falls on, or the object
|
||||
// at the point
|
||||
let mut out = s.execute("mask.add", &json!({"kind": "object", "name": "Lens Blur Subject"}))?;
|
||||
let mask = out
|
||||
.get("activeMask")
|
||||
.and_then(Value::as_u64)
|
||||
.and_then(|v| u32::try_from(v).ok())
|
||||
.ok_or_else(|| bad(C, "the subject mask was not created"))?;
|
||||
let focus = (from == "camera").then_some(point);
|
||||
if let Err(e) = s.segment_subject_later(id, mask, 0, point, focus) {
|
||||
// nothing half-made: the empty mask goes again
|
||||
let _ = s.execute("edit.undo", &json!({}));
|
||||
return Err(crate::EngineError::Other(e));
|
||||
}
|
||||
out["pending"] = json!(s.segmenter.background);
|
||||
edit(s, C, "Lens Blur Subject", |d| {
|
||||
// a mask that only says what is in focus: no adjustments of its own
|
||||
if let Some(m) = d.masks.iter_mut().find(|m| m.id == mask) {
|
||||
m.adjust = lightcraft_develop::LocalAdjustments::default();
|
||||
}
|
||||
let b = &mut d.lens_blur;
|
||||
b.enabled = true;
|
||||
if b.amount <= 0.0 || !b.amount.is_finite() {
|
||||
b.amount = 50.0;
|
||||
}
|
||||
b.focus_mask = Some(mask);
|
||||
d.set_section_enabled("lensBlur", true);
|
||||
Ok(())
|
||||
})?;
|
||||
if let Some(o) = out.as_object_mut() {
|
||||
o.insert("mask".into(), json!(mask));
|
||||
o.insert("point".into(), json!([point.x, point.y]));
|
||||
o.insert("from".into(), json!(from));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
),
|
||||
cmd!(
|
||||
"lensBlur.brushReset",
|
||||
"Reset Lens Blur Brush",
|
||||
[],
|
||||
None,
|
||||
"{mode?: \"focus\"|\"blur\"} — removes that brush's strokes (both brushes without `mode`)",
|
||||
has_active,
|
||||
|s, p| {
|
||||
const C: &str = "lensBlur.brushReset";
|
||||
let (focus, blur) = match str_param(p, "mode") {
|
||||
None => (true, true),
|
||||
Some("focus") => (true, false),
|
||||
Some("blur") => (false, true),
|
||||
Some(_) => return Err(bad(C, "`mode` must be \"focus\" or \"blur\"")),
|
||||
};
|
||||
edit(s, C, "Reset Lens Blur Brush", |d| {
|
||||
if focus {
|
||||
d.lens_blur.focus_strokes.clear();
|
||||
}
|
||||
if blur {
|
||||
d.lens_blur.blur_strokes.clear();
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
),
|
||||
cmd!("develop.sectionEnabled", "Toggle Section", [], None, "{section: string, enabled: bool}", has_active, |s, p| {
|
||||
let sec = str_param(p, "section").ok_or_else(|| bad("develop.sectionEnabled", "missing section"))?.to_string();
|
||||
let on = bool_or(p, "enabled", true);
|
||||
|
||||
@@ -1302,10 +1302,13 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
}),
|
||||
// ---- import
|
||||
cmd!(query "library.inspectLightroom", "Inspect Lightroom Catalog", [], None,
|
||||
"{path: .lrcat} → {photos, collections, missing, warnings}; read-only, no Lightroom required",
|
||||
always, |_, p| {
|
||||
"{path: .lrcat, or a Lightroom catalog backup .zip} → {photos, collections, missing, warnings}; read-only, no Lightroom required",
|
||||
always, |s, p| {
|
||||
let path = str_param(p,"path").ok_or_else(|| bad("library.inspectLightroom","missing path"))?;
|
||||
let data = crate::lightroom_catalog::read(std::path::Path::new(path)).map_err(|e| bad("library.inspectLightroom",e))?;
|
||||
let (cancel, total, done) = Default::default();
|
||||
let temp = crate::lightroom_catalog::backup_temp_root(s);
|
||||
let data = crate::lightroom_catalog::read_source(std::path::Path::new(path), temp.as_deref(), &cancel, &total, &done)
|
||||
.map_err(|e| bad("library.inspectLightroom",e))?;
|
||||
Ok(json!({"photos":data.photos.len(),"collections":data.collections.iter().filter(|r|r.get("systemOnly").and_then(Value::as_f64).unwrap_or(0.0)==0.0).count(),"missing":data.photos.iter().filter(|p| !p.original_is_file()).map(|p|&p.path).collect::<Vec<_>>(),"warnings":data.warnings,"skipped":data.skipped}))
|
||||
}),
|
||||
cmd!(
|
||||
@@ -1313,7 +1316,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Import Lightroom Catalog",
|
||||
[],
|
||||
None,
|
||||
"{path: .lrcat, updateExisting?: false}; direct import of paths, ratings, flags, metadata, keywords, collections, virtual copies & mapped edits. Archives source settings/history first. Existing edited photos are preserved unless updateExisting=true; source catalog is read-only.",
|
||||
"{path: .lrcat, or a Lightroom catalog backup .zip (unpacked to a temporary folder in the library, removed after reading; the zip is not changed), updateExisting?: false}; direct import of paths, ratings, flags, metadata, keywords, collections, virtual copies & mapped edits. Archives source settings/history first. Existing edited photos are preserved unless updateExisting=true; source catalog is read-only.",
|
||||
always,
|
||||
|s, p| {
|
||||
let path = str_param(p, "path").ok_or_else(|| bad("library.importLightroom", "missing path"))?;
|
||||
|
||||
@@ -103,6 +103,10 @@ fn import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
let (files, mut failed) = cube_files(&paths);
|
||||
let mut imported = Vec::new();
|
||||
// the ids taken and the next suffix per slug: a bundle of same-titled LUTs doesn't probe the
|
||||
// whole list from 2 for each one
|
||||
let mut taken: std::collections::HashSet<String> = s.lut_profiles.iter().map(|x| x.id.clone()).collect();
|
||||
let mut next_suffix: std::collections::HashMap<String, u32> = std::collections::HashMap::new();
|
||||
for (name, text, group) in files {
|
||||
let lut = match lightcraft_pipeline::lut::Lut3d::parse_cube(&text) {
|
||||
Ok(l) => l,
|
||||
@@ -115,11 +119,12 @@ fn import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let group = super::super::preset_import::group_from_dir(&group.unwrap_or_default()).unwrap_or_else(|| "Imported".into());
|
||||
let slug = crate::presets::slug(&format!("{group}-{display}"));
|
||||
let mut id = format!("lut:{slug}");
|
||||
let mut n = 2;
|
||||
while s.lut_profiles.iter().any(|x| x.id == id) {
|
||||
let n = next_suffix.entry(slug.clone()).or_insert(2);
|
||||
while taken.contains(&id) {
|
||||
id = format!("lut:{slug}-{n}");
|
||||
n += 1;
|
||||
*n += 1;
|
||||
}
|
||||
taken.insert(id.clone());
|
||||
let file = match &dir {
|
||||
Some(d) => {
|
||||
let f = d.join(format!("{}.cube", id.trim_start_matches("lut:")));
|
||||
|
||||
@@ -64,6 +64,9 @@ fn set_names_ops(s: &crate::Session, names: &[String; 5]) -> Vec<Op> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Most bytes of a track log file.
|
||||
const MAX_GPX_BYTES: u64 = 256 << 20;
|
||||
|
||||
/// Geotag photos from a GPX track log by capture time (Lightroom Classic's Map ▸ Tracklog ▸
|
||||
/// Auto-Tag Photos). GPX times are UTC; capture times are the camera's local clock, so a photo's
|
||||
/// recorded zone (Exif `OffsetTimeOriginal`) or the `offset` parameter converts them.
|
||||
@@ -72,7 +75,9 @@ fn auto_tag_tracklog(s: &mut crate::Session, p: &Value) -> Result<Value> {
|
||||
const C: &str = "photo.autoTagTracklog";
|
||||
let text = match (str_param(p, "gpx"), str_param(p, "path")) {
|
||||
(Some(t), _) => t.to_string(),
|
||||
(None, Some(path)) => std::fs::read_to_string(path).map_err(|e| bad(C, format!("can't read {path}: {e}")))?,
|
||||
(None, Some(path)) => crate::read_file::read_regular(path, MAX_GPX_BYTES)
|
||||
.map(|b| String::from_utf8_lossy(&b).into_owned())
|
||||
.map_err(|e| bad(C, format!("can't read {path}: {e}")))?,
|
||||
(None, None) => return Err(bad(C, "missing `path` (a .gpx file) or `gpx` (its text)")),
|
||||
};
|
||||
let log = parse_gpx(&text).map_err(|e| bad(C, e.to_string()))?;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
use lightcraft_develop::LocalAdjustments;
|
||||
use serde_json::{Map, Value, json};
|
||||
|
||||
use super::mask_effects::{MaskEffect, upsert};
|
||||
use super::mask_effects::MaskEffect;
|
||||
use super::{bad, str_param};
|
||||
use crate::preset_import::{Lua, group_from_dir, parse_lua, read_zip};
|
||||
use crate::{Result, Session};
|
||||
@@ -397,8 +397,25 @@ pub fn add_found(s: &mut Session, found: Vec<Found>, group: Option<&str>, dry: b
|
||||
return Ok((rows, div_rows));
|
||||
}
|
||||
s.mask_effects.retain(|e| !(e.is_empty() && dividers.iter().any(|(d, _)| d.effect.name.eq_ignore_ascii_case(&e.name))));
|
||||
// as `upsert`, with the names indexed once: a file of many effects doesn't scan the list per effect
|
||||
// (the first of two same-named effects, as `upsert` and readers find it)
|
||||
let mut by_name: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
|
||||
for (i, x) in s.mask_effects.iter().enumerate() {
|
||||
by_name.entry(x.name.trim().to_ascii_lowercase()).or_insert(i);
|
||||
}
|
||||
for f in effects {
|
||||
upsert(s, f.effect);
|
||||
let e = f.effect;
|
||||
let key = e.name.trim().to_ascii_lowercase();
|
||||
match by_name.get(&key).and_then(|&i| s.mask_effects.get_mut(i)) {
|
||||
Some(x) => {
|
||||
let favorite = x.favorite || e.favorite;
|
||||
*x = MaskEffect { favorite, ..e };
|
||||
}
|
||||
None => {
|
||||
by_name.insert(key, s.mask_effects.len());
|
||||
s.mask_effects.push(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
s.save_prefs()?;
|
||||
Ok((rows, div_rows))
|
||||
@@ -489,12 +506,11 @@ pub fn export_effects(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if chosen.is_empty() {
|
||||
return Err(bad(C, "no effect presets to export"));
|
||||
}
|
||||
if let Some(dir) = path.parent().filter(|d| !d.as_os_str().is_empty()) {
|
||||
std::fs::create_dir_all(dir).map_err(|e| crate::EngineError::Other(format!("{}: {e}", dir.display())))?;
|
||||
}
|
||||
let target = path.to_string_lossy();
|
||||
s.check_write_target(&target).map_err(|e| bad(C, e))?;
|
||||
let text = serde_json::to_string_pretty(&json!({"format": FILE_FORMAT, "version": 2, "effects": chosen}))
|
||||
.map_err(|e| crate::EngineError::Other(e.to_string()))?;
|
||||
std::fs::write(&path, text).map_err(|e| crate::EngineError::Other(format!("{}: {e}", path.display())))?;
|
||||
crate::export::write_file_durable(&target, text.as_bytes()).map_err(crate::EngineError::Other)?;
|
||||
Ok(json!({"path": path.display().to_string(), "count": chosen.len()}))
|
||||
}
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ pub const FIELDS: &[&str] = &[
|
||||
"noise",
|
||||
"moire",
|
||||
"defringe",
|
||||
"lens_blur",
|
||||
"color_hue",
|
||||
"color_sat",
|
||||
];
|
||||
@@ -118,10 +119,11 @@ pub fn get(a: &LocalAdjustments, k: &str) -> f64 {
|
||||
serde_json::to_value(a).ok().and_then(|v| v.get(k).and_then(Value::as_f64)).unwrap_or(0.0)
|
||||
}
|
||||
|
||||
/// `a` with field `k` set to `v` (unchanged for an unknown name).
|
||||
/// `a` with field `k` set to `v` (unchanged for an unknown name). A [`FIELDS`] slider counts as
|
||||
/// known also while its JSON leaves it out at 0 (`lens_blur`).
|
||||
pub fn set(a: &LocalAdjustments, k: &str, v: f64) -> LocalAdjustments {
|
||||
let Ok(Value::Object(mut o)) = serde_json::to_value(a) else { return *a };
|
||||
if !o.contains_key(k) || !v.is_finite() {
|
||||
if !(o.contains_key(k) || FIELDS.contains(&k)) || !v.is_finite() {
|
||||
return *a;
|
||||
}
|
||||
o.insert(k.into(), json!(v));
|
||||
@@ -707,6 +709,22 @@ mod tests {
|
||||
assert!(!e.matches(&LocalAdjustments { amount: 100.0, ..out }), "Amount counts when carried");
|
||||
}
|
||||
|
||||
/// A partial effect sets Lens Blur on a mask without any yet (its JSON leaves `lens_blur` out
|
||||
/// at 0); a name no slider has still changes nothing.
|
||||
#[test]
|
||||
fn partial_effects_set_lens_blur_from_zero() {
|
||||
let e = MaskEffect {
|
||||
name: "Bokeh".into(),
|
||||
adjust: LocalAdjustments { lens_blur: 60.0, ..Default::default() },
|
||||
fields: vec!["lens_blur".into()],
|
||||
..Default::default()
|
||||
};
|
||||
let out = e.apply(&LocalAdjustments::default());
|
||||
assert_eq!(out.lens_blur, 60.0);
|
||||
assert!(e.matches(&out));
|
||||
assert_eq!(set(&LocalAdjustments::default(), "bogus", 5.0), LocalAdjustments::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hue_without_saturation_still_matches() {
|
||||
let e = MaskEffect {
|
||||
|
||||
@@ -98,6 +98,10 @@ fn resolve_ai(s: &mut Session, shape: &mut MaskShape, c: &str) -> Result<Later>
|
||||
match shape {
|
||||
MaskShape::Object { seg: Some(_), .. } | MaskShape::Prompt { seg: Some(_), .. } => {}
|
||||
MaskShape::Object { hint, exclude, .. } if !hint.is_empty() && background => {
|
||||
// refused here, not when the worker later fails at the model's limit after "pending"
|
||||
if hint.len() + exclude.len() > crate::segment::MAX_CLICKS {
|
||||
return Err(ai_err(format!("an Object selection takes at most {} clicks", crate::segment::MAX_CLICKS)));
|
||||
}
|
||||
s.segmenter.require_model()?;
|
||||
return Ok(Later::Clicks(std::mem::take(hint), std::mem::take(exclude)));
|
||||
}
|
||||
@@ -158,7 +162,11 @@ fn masks_edit(s: &mut Session, c: &str, label: &str, f: impl FnOnce(&mut Vec<Mas
|
||||
let id = s.active().ok_or_else(|| bad(c, "no active photo"))?;
|
||||
let mut d = (*s.develop_of(id).unwrap_or_default()).clone();
|
||||
let mut active = s.active_mask;
|
||||
// the Lens Blur focus follows its mask: a deleted one stops being the focus, so a new mask
|
||||
// that gets its id (the next id is the highest + 1) doesn't turn into it
|
||||
let focus = d.lens_blur.focus_mask.filter(|f| d.masks.iter().any(|m| m.id == *f));
|
||||
f(&mut d.masks, &mut active)?;
|
||||
d.lens_blur.focus_mask = focus.filter(|f| d.masks.iter().any(|m| m.id == *f));
|
||||
s.set_develop(id, d, label)?;
|
||||
s.active_mask = active;
|
||||
Ok(json!({"activeMask": active}))
|
||||
@@ -648,7 +656,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Set Mask Adjustments",
|
||||
[],
|
||||
None,
|
||||
"{id?, values: {exposure, contrast, highlights, shadows, whites, blacks, temp, tint, texture, clarity, dehaze, hue, saturation, sharpness, noise, moire, defringe, color_hue, color_sat, amount}}",
|
||||
"{id?, values: {exposure, contrast, highlights, shadows, whites, blacks, temp, tint, texture, clarity, dehaze, hue, saturation, sharpness, noise, moire, defringe, lens_blur, lens_blur_off, lens_blur_outside, lens_blur_boost, lens_blur_transition, color_hue, color_sat, amount}}",
|
||||
has_active,
|
||||
|s, p| {
|
||||
let vals = p.get("values").cloned().ok_or_else(|| bad("mask.adjust", "missing values"))?;
|
||||
@@ -1147,6 +1155,9 @@ pub(crate) fn clamp_local(mut a: LocalAdjustments) -> LocalAdjustments {
|
||||
] {
|
||||
c(v, -100.0, 100.0);
|
||||
}
|
||||
c(&mut a.lens_blur, 0.0, 100.0);
|
||||
c(&mut a.lens_blur_boost, 0.0, 100.0);
|
||||
c(&mut a.lens_blur_transition, 0.0, 100.0);
|
||||
c(&mut a.color_hue, 0.0, 360.0);
|
||||
c(&mut a.color_sat, 0.0, 100.0);
|
||||
c(&mut a.amount, 0.0, 200.0);
|
||||
@@ -1155,7 +1166,7 @@ pub(crate) fn clamp_local(mut a: LocalAdjustments) -> LocalAdjustments {
|
||||
|
||||
/// `mask.brushStroke`'s `pressure`: one number 0..1 per point (clamped), at most one per point; absent or `null`
|
||||
/// is no pressure data.
|
||||
fn pressure_list(v: Option<&Value>, points: usize) -> Result<Vec<f32>> {
|
||||
pub(super) fn pressure_list(v: Option<&Value>, points: usize) -> Result<Vec<f32>> {
|
||||
let Some(v) = v.filter(|v| !v.is_null()) else { return Ok(Vec::new()) };
|
||||
let list = v.as_array().ok_or_else(|| bad("mask.brushStroke", "pressure must be a list of numbers 0..1"))?;
|
||||
if list.len() > points {
|
||||
|
||||
@@ -23,6 +23,8 @@ fn import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// .lceffect, also inside zips — become mask effects
|
||||
let mut effects = Vec::new();
|
||||
let mut effect_failed = Vec::new();
|
||||
// every file's presets, added in one go below (adding one at a time indexed the library each time)
|
||||
let mut pending: Vec<(lightcraft_develop::Preset, Value)> = Vec::new();
|
||||
for top in &paths {
|
||||
let from_dir = std::path::Path::new(top).is_dir();
|
||||
for f in expand_preset_paths(std::slice::from_ref(top)) {
|
||||
@@ -65,13 +67,14 @@ fn import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
it.unmapped.extend(crate::profile_refs::report(&look, &r));
|
||||
profile = json!({"name": look["name"], "id": it.preset.settings["profile"].get("id")});
|
||||
}
|
||||
let (name, grp) = (it.preset.name.clone(), it.preset.group.clone());
|
||||
let id = if dry { Some(it.preset.id.clone()) } else { s.add_presets(vec![it.preset]).pop() };
|
||||
match id {
|
||||
Some(id) => {
|
||||
imported.push(json!({"id": id, "name": name, "group": grp, "file": f, "unmapped": it.unmapped, "profile": profile}))
|
||||
}
|
||||
None => skipped += 1,
|
||||
let mut row =
|
||||
json!({"name": it.preset.name, "group": it.preset.group, "file": f, "unmapped": it.unmapped, "profile": profile});
|
||||
if dry {
|
||||
// a dry run keeps nothing: the row is final, the parsed preset is dropped
|
||||
row["id"] = json!(it.preset.id);
|
||||
imported.push(row);
|
||||
} else {
|
||||
pending.push((it.preset, row));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,6 +82,18 @@ fn import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
}
|
||||
// split, not cloned: a large import holds one copy of the parsed presets
|
||||
let (presets, rows): (Vec<_>, Vec<_>) = pending.into_iter().unzip();
|
||||
let ids = if dry { Vec::new() } else { s.add_presets_each(presets) };
|
||||
for (mut row, id) in rows.into_iter().zip(ids) {
|
||||
match id {
|
||||
Some(id) => {
|
||||
row["id"] = json!(id);
|
||||
imported.push(row);
|
||||
}
|
||||
None => skipped += 1,
|
||||
}
|
||||
}
|
||||
failed.extend(effect_failed.into_iter().map(|(f, e)| json!([f, e])));
|
||||
let (effects, dividers) = mef::add_found(s, effects, group.as_deref(), dry)?;
|
||||
Ok(json!({"imported": imported, "skipped": skipped, "failed": failed, "effects": effects, "effectDividers": dividers}))
|
||||
|
||||
@@ -363,7 +363,7 @@ fn template_import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if meta.len() > MAX_FILE {
|
||||
return Err(bad(ID, format!("{}: {} bytes is too large for a print template (at most {} KB)", path.display(), meta.len(), MAX_FILE / 1024)));
|
||||
}
|
||||
let bytes = std::fs::read(path).map_err(|e| bad(ID, format!("{}: {e}", path.display())))?;
|
||||
let bytes = crate::read_file::read_regular(path, MAX_FILE).map_err(|e| bad(ID, format!("{}: {e}", path.display())))?;
|
||||
let mut t = PrintTemplate::from_file(&bytes).map_err(|e| bad(ID, format!("{}: {e}", path.display())))?;
|
||||
if s.print_templates.len() >= MAX_TEMPLATES {
|
||||
return Err(bad(ID, format!("at most {MAX_TEMPLATES} templates; delete one first")));
|
||||
|
||||
@@ -274,15 +274,20 @@ pub fn masks(values: &Values, aspect: f64) -> (Vec<Value>, Vec<String>) {
|
||||
skipped.extend(unmapped_local(c));
|
||||
// brush dabs of one correction are one brush component
|
||||
let mut merged: Vec<Value> = Vec::new();
|
||||
// where the brush of each op sits in `merged` (no scan of it per dab)
|
||||
let mut brush_at: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
|
||||
for p in parts {
|
||||
let is_brush = p["shape"]["kind"] == "brush";
|
||||
if is_brush
|
||||
&& let Some(prev) = merged.iter_mut().find(|q| q["shape"]["kind"] == "brush" && q["op"] == p["op"])
|
||||
&& let Some(prev) = brush_at.get(&p["op"].to_string()).and_then(|&i| merged.get_mut(i))
|
||||
&& let (Some(a), Some(b)) = (prev["shape"]["strokes"].as_array_mut(), p["shape"]["strokes"].as_array())
|
||||
{
|
||||
a.extend(b.iter().cloned());
|
||||
continue;
|
||||
}
|
||||
if is_brush {
|
||||
brush_at.insert(p["op"].to_string(), merged.len());
|
||||
}
|
||||
merged.push(p);
|
||||
}
|
||||
let n = out.len() + 1;
|
||||
|
||||
@@ -29,11 +29,7 @@ impl Display {
|
||||
/// Load the ICC profile at `path`.
|
||||
pub fn load(path: &str) -> Result<Display, String> {
|
||||
let path = path.trim();
|
||||
let meta = std::fs::metadata(path).map_err(|e| format!("{path}: {e}"))?;
|
||||
if meta.len() > MAX_PROFILE_BYTES as u64 {
|
||||
return Err(format!("{path}: too large for an ICC profile"));
|
||||
}
|
||||
let bytes = std::fs::read(path).map_err(|e| format!("{path}: {e}"))?;
|
||||
let bytes = crate::read_file::read_regular(path, MAX_PROFILE_BYTES as u64).map_err(|e| format!("{path}: {e} (an ICC profile)"))?;
|
||||
Display::from_bytes(path, &bytes)
|
||||
}
|
||||
|
||||
|
||||
@@ -600,11 +600,15 @@ impl ExportOptions {
|
||||
let base = if self.naming.trim().is_empty() { "{name}" } else { self.naming.as_str() };
|
||||
let n = seq + self.start_number.max(1) as usize - 1;
|
||||
let name = crate::rename::expand_tokens(base, p, n, 3);
|
||||
let mut name: String = name.chars().map(|c| if matches!(c, '/' | '\\' | ':' | '\0') { '_' } else { c }).collect();
|
||||
// the catalog's own file name is cleaned the same way: a name from a supplied catalog may hold separators
|
||||
let clean = |s: &str| -> String { s.chars().map(|c| if matches!(c, '/' | '\\' | ':' | '\0') { '_' } else { c }).collect() };
|
||||
let mut name = clean(&name);
|
||||
let (stem, ext) = p.file_name.rsplit_once('.').unwrap_or((&p.file_name, ""));
|
||||
let ext = clean(ext);
|
||||
let ext = ext.as_str();
|
||||
if name.trim().is_empty() {
|
||||
// every token came out empty (no title, no capture time…): keep the original name
|
||||
name = stem.to_string();
|
||||
name = clean(stem);
|
||||
}
|
||||
if self.format == ExportFormat::Original {
|
||||
let base = name.trim_end_matches('.');
|
||||
|
||||
@@ -1525,3 +1525,50 @@ fn export_weights_follow_the_source_and_output_sizes() {
|
||||
assert_eq!(render_weight(0, 0, SourceLevel::Full, 0), 0);
|
||||
assert!(render_weight(usize::MAX, 1, SourceLevel::Full, usize::MAX) > 0, "no overflow");
|
||||
}
|
||||
|
||||
/// A catalog's file name never adds a folder level through the empty-naming fallback or the original's extension.
|
||||
#[test]
|
||||
fn file_name_fallback_and_extension_are_one_path_component() {
|
||||
let empty = |format| ExportOptions { format, naming: "{title}".into(), ..Default::default() };
|
||||
for name in ["../../x.jpg", "..\\y.jpg", "a/b/c.jpg"] {
|
||||
let f = empty(ExportFormat::Jpeg).file_name_for(&named(name), 1);
|
||||
assert!(!f.contains(['/', '\\']), "{f}");
|
||||
}
|
||||
let f = empty(ExportFormat::Original).file_name_for(&named("x.a/../b"), 1);
|
||||
assert!(!f.contains(['/', '\\']), "{f}");
|
||||
assert_eq!(empty(ExportFormat::Jpeg).file_name_for(&named("IMG_1.jpg"), 1), "IMG_1.jpg");
|
||||
}
|
||||
|
||||
/// A graphic is read within 8192 x 8192 pixels: a PNG header declaring more is refused before any pixel is allocated.
|
||||
#[test]
|
||||
fn a_graphic_past_the_pixel_cap_is_refused_before_it_is_decoded() {
|
||||
fn crc32(data: &[u8]) -> u32 {
|
||||
let mut crc = !0u32;
|
||||
for &b in data {
|
||||
crc ^= u32::from(b);
|
||||
for _ in 0..8 {
|
||||
crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 };
|
||||
}
|
||||
}
|
||||
!crc
|
||||
}
|
||||
let mut ihdr = b"IHDR".to_vec();
|
||||
ihdr.extend(10_000u32.to_be_bytes());
|
||||
ihdr.extend(10_000u32.to_be_bytes());
|
||||
ihdr.extend([8, 0, 0, 0, 0]);
|
||||
let mut png = b"\x89PNG\r\n\x1a\n".to_vec();
|
||||
png.extend(13u32.to_be_bytes());
|
||||
png.extend(&ihdr);
|
||||
png.extend(crc32(&ihdr).to_be_bytes());
|
||||
// the start of the image data, so the header is read to its end (no pixels follow)
|
||||
png.extend(0u32.to_be_bytes());
|
||||
png.extend(b"IDAT");
|
||||
png.extend(crc32(b"IDAT").to_be_bytes());
|
||||
let dir = std::env::temp_dir().join(format!("lc-graphic-cap-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("huge.png");
|
||||
std::fs::write(&path, &png).unwrap();
|
||||
let e = read_graphic(&path.to_string_lossy()).unwrap_err();
|
||||
assert!(e.contains("too large") || e.contains("10000"), "{e}");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
@@ -236,12 +236,20 @@ pub(crate) fn watermark_logo(path: &str) -> Option<std::sync::Arc<Rgba8>> {
|
||||
Some(img)
|
||||
}
|
||||
|
||||
/// Most pixels of a watermark or layout graphic (8192 x 8192).
|
||||
const MAX_GRAPHIC_PIXELS: u64 = 1 << 26;
|
||||
|
||||
/// Most bytes of a watermark or layout graphic file.
|
||||
const MAX_GRAPHIC_BYTES: u64 = 256 << 20;
|
||||
|
||||
/// A graphic file (a PNG with transparency, typically) as 8-bit sRGB with its alpha: the
|
||||
/// watermark logo, the loupe's layout image. Applies EXIF orientation before either consumer sizes it.
|
||||
/// The error says why it couldn't be read or decoded.
|
||||
pub fn read_graphic(path: &str) -> Result<Rgba8, String> {
|
||||
let bytes = std::fs::read(path).map_err(|e| format!("can't read {path}: {e}"))?;
|
||||
let decoded = lightcraft_codecs::decode(&bytes, Default::default()).map_err(|e| format!("can't decode {path}: {e}"))?;
|
||||
let bytes = crate::read_file::read_regular(path, MAX_GRAPHIC_BYTES).map_err(|e| format!("can't read {path}: {e}"))?;
|
||||
// a graphic is a logo or a layout image, not a photo: its decode is held to 8192 x 8192 pixels, not the general gigapixel
|
||||
let opts = lightcraft_codecs::DecodeOptions { max_pixels: MAX_GRAPHIC_PIXELS, ..Default::default() };
|
||||
let decoded = lightcraft_codecs::decode(&bytes, opts).map_err(|e| format!("can't decode {path}: {e}"))?;
|
||||
Ok(decoded.to_srgb8().into_oriented(lightcraft_geom::Orientation::from_exif(decoded.orientation)))
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ pub fn fs_pair_loader() -> PairLoader {
|
||||
let weight = len * if max_edge <= crate::media::SourceLevel::Thumb.max_edge() { 4 } else { 10 };
|
||||
let gate = crate::memory::work_gate();
|
||||
let _permit = if crate::memory::is_background() { gate.acquire(weight) } else { gate.acquire_urgent(weight) };
|
||||
let bytes = std::fs::read(path).map_err(|e| format!("{path}: {e}"))?;
|
||||
let bytes = crate::read_file::read_regular(path, crate::read_file::MAX_MEDIA_BYTES).map_err(|e| format!("{path}: {e}"))?;
|
||||
let r = load_vec_pair(bytes, max_edge, spec, with);
|
||||
crate::memory::release();
|
||||
r
|
||||
@@ -84,15 +84,13 @@ pub fn fs_hooks() -> (FileLoader, FileProbe) {
|
||||
let loader: FileLoader =
|
||||
Arc::new(|path: &str, max_edge: usize, with: RawDecode<'_>| fs_load(path, max_edge, |bytes| load_vec(bytes, max_edge, with)));
|
||||
let probe: FileProbe = Arc::new(|path: &str| {
|
||||
use std::io::Read;
|
||||
// one open, then everything from the open file: its size and modification time are not
|
||||
// round trips of their own (on a network share each of them is one)
|
||||
let mut file = std::fs::File::open(path).map_err(|e| format!("{path}: {e}"))?;
|
||||
let file = crate::read_file::open_for_read(path).map_err(|e| format!("{path}: {e}"))?;
|
||||
let meta = file.metadata().ok();
|
||||
let len = meta.as_ref().map_or(0, |m| usize::try_from(m.len()).unwrap_or(usize::MAX));
|
||||
let _permit = crate::memory::work_gate().acquire(len);
|
||||
let mut bytes = Vec::new();
|
||||
file.read_to_end(&mut bytes).map_err(|e| format!("{path}: {e}"))?;
|
||||
let bytes = crate::read_file::read_open(file, crate::read_file::MAX_MEDIA_BYTES).map_err(|e| format!("{path}: {e}"))?;
|
||||
let mut info = probe_bytes(path, &bytes)?;
|
||||
info.file_modified = meta.as_ref().and_then(modified_iso);
|
||||
Ok(info)
|
||||
@@ -111,7 +109,7 @@ fn fs_load<T>(path: &str, max_edge: usize, load: impl FnOnce(Vec<u8>) -> Result<
|
||||
let weight = len.saturating_mul(if max_edge <= crate::media::SourceLevel::Thumb.max_edge() { 3 } else { 6 });
|
||||
let gate = crate::memory::work_gate();
|
||||
let _permit = if crate::memory::is_background() { gate.acquire(weight) } else { gate.acquire_urgent(weight) };
|
||||
let bytes = std::fs::read(path).map_err(|e| format!("{path}: {e}"))?;
|
||||
let bytes = crate::read_file::read_regular(path, crate::read_file::MAX_MEDIA_BYTES).map_err(|e| format!("{path}: {e}"))?;
|
||||
let r = load(bytes);
|
||||
// the file, the samples and the intermediate images are gone: give their pages back
|
||||
crate::memory::release();
|
||||
|
||||
@@ -281,7 +281,7 @@ fn tiff_with_preview_strip(strip: &[u8], tail: usize, what: Fixture) -> Vec<u8>
|
||||
/// [`tiff_with_preview_strip`] that also keeps the `smaller` JPEGs in sub-IFDs of IFD0, as an ARW keeps its
|
||||
/// 1616 px preview beside the full-size one.
|
||||
fn tiff_with_preview_strips(strip: &[u8], smaller: &[&[u8]], tail: usize, what: Fixture) -> Vec<u8> {
|
||||
use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
let mut preview = IfdBuilder::new();
|
||||
if what.dng {
|
||||
preview.set(t::DNG_VERSION, Value::Byte(vec![1, 4, 0, 0]));
|
||||
@@ -492,8 +492,14 @@ fn a_probe_of_a_file_that_cannot_be_read_names_the_path_and_the_cause() {
|
||||
for path in [dir.join("no-such-file.jpg"), dir.clone()] {
|
||||
let path = path.to_string_lossy().to_string();
|
||||
let e = probe(&path).unwrap_err();
|
||||
assert_eq!(e, format!("{path}: {}", std::fs::read(&path).unwrap_err()));
|
||||
// a folder is refused as "not a regular file" before any read, so only the path and a cause are pinned
|
||||
assert!(e.starts_with(&format!("{path}: ")), "{e}");
|
||||
}
|
||||
// the system's own words for it, whatever the platform says
|
||||
let missing = dir.join("no-such-file.jpg");
|
||||
let want = std::fs::read(&missing).unwrap_err().to_string();
|
||||
let got = probe(&missing.to_string_lossy()).unwrap_err();
|
||||
assert_eq!(got.strip_prefix(&format!("{}: ", missing.to_string_lossy())), Some(want.as_str()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
@@ -656,7 +662,7 @@ fn corpus_a7cr_warp_keeps_coordinates_after_orientation_and_resizing() {
|
||||
let Ok(mut bytes) = std::fs::read(root.join("raw").join(name)) else { continue };
|
||||
let off = lightcraft_raw::probe_info(&bytes).unwrap();
|
||||
assert!(embedded_lens(&off).is_none());
|
||||
let tiff = lightcraft_tiff::Tiff::parse(&bytes).unwrap();
|
||||
let tiff = craft_tiff::Tiff::parse(&bytes).unwrap();
|
||||
let at = tiff.find(0x7036).unwrap().offset as usize;
|
||||
let mut enabled = Vec::new();
|
||||
tiff.order.put_u16(&mut enabled, 17);
|
||||
@@ -1214,8 +1220,8 @@ fn corpus_clipped_sky_beside_a_tree_has_no_coloured_halo() {
|
||||
/// copy's samples are much brighter than the image's here, so a picture says which one it came from.
|
||||
#[test]
|
||||
fn exports_never_develop_a_dngs_reduced_copy() {
|
||||
use lightcraft_tiff::tags::{self as t, photometric};
|
||||
use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value};
|
||||
use craft_tiff::tags::{self as t, photometric};
|
||||
use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value};
|
||||
use serde_json::json;
|
||||
let linear = |subfile: u32, w: u32, h: u32, sample: u16| {
|
||||
let mut ifd = IfdBuilder::new();
|
||||
@@ -1292,10 +1298,10 @@ fn exports_never_develop_a_dngs_reduced_copy() {
|
||||
/// heuristic would never look) is used; a matte with an unknown name is ignored.
|
||||
#[test]
|
||||
fn dng_sky_matte_drives_the_sky_mask() {
|
||||
use craft_tiff::tags::{self as t, photometric};
|
||||
use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value};
|
||||
use lightcraft_develop::{DevelopSettings, LocalAdjustments, Mask, MaskComponent, MaskOp, MaskShape};
|
||||
use lightcraft_pipeline::{RenderRequest, render};
|
||||
use lightcraft_tiff::tags::{self as t, photometric};
|
||||
use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value};
|
||||
let (w, h) = (16u32, 8u32);
|
||||
let image = |ifd: &mut IfdBuilder, w: u32, h: u32, cpp: u16, bits: u16, data: Vec<u8>| {
|
||||
ifd.set(t::IMAGE_WIDTH, Value::Long(vec![w]));
|
||||
@@ -1530,7 +1536,7 @@ fn unsupported_raw_imports_as_preview_only_and_survives_reopen() {
|
||||
/// A raw whose TIFF shell holds only a small RGB image (IFD0) next to a private block, as Exif
|
||||
/// declares a picture 25x its size.
|
||||
fn tiff_shell(w: u32, h: u32, shell: bool) -> Vec<u8> {
|
||||
use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
let mut ifd0 = IfdBuilder::new();
|
||||
ifd0.set(t::IMAGE_WIDTH, Value::Long(vec![w]));
|
||||
ifd0.set(t::IMAGE_LENGTH, Value::Long(vec![h]));
|
||||
@@ -1571,7 +1577,7 @@ fn thumbnail_only_tiff_raw_is_preview_only_not_a_plain_image() {
|
||||
|
||||
// TIFFs preserving camera Make tags (SONY, NIKON, etc.) are ordinary images, not raws (#281)
|
||||
for make in ["SONY", "NIKON CORPORATION", "PENTAX", "RICOH", "SAMSUNG"] {
|
||||
use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
let mut ifd0 = IfdBuilder::new();
|
||||
ifd0.set(t::IMAGE_WIDTH, Value::Long(vec![24]));
|
||||
ifd0.set(t::IMAGE_LENGTH, Value::Long(vec![16]));
|
||||
@@ -1615,7 +1621,7 @@ fn undecodable_container_with_a_preview_imports_as_preview_only() {
|
||||
/// holds the preview) imports as a raw shown from its preview, not as a greyscale image of the mosaic.
|
||||
#[test]
|
||||
fn uncompressed_leaf_mos_imports_as_a_preview_only_raw() {
|
||||
use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
let px: Vec<u8> = (0..40 * 30).flat_map(|i| [(i % 251) as u8, 128, 200]).collect();
|
||||
let jpeg = encode_jpeg(&EncodeImage::new(40, 30, 3, Samples::U8(&px)), 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
|
||||
let mos = |leaf_data: bool| {
|
||||
@@ -1646,7 +1652,7 @@ fn uncompressed_leaf_mos_imports_as_a_preview_only_raw() {
|
||||
/// headers; loading it says what is wrong instead of naming a bare TIFF compression number.
|
||||
#[test]
|
||||
fn gopro_vc5_dng_imports_and_fails_to_load_with_a_clear_reason() {
|
||||
use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
let mut ifd = IfdBuilder::new();
|
||||
ifd.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0]));
|
||||
ifd.set(t::IMAGE_WIDTH, Value::Long(vec![32]));
|
||||
@@ -1675,7 +1681,7 @@ fn decode_failures_of_recognised_raws_may_fall_back_to_the_preview() {
|
||||
assert_eq!(preview_reason(b"x", RawError::Unsupported("x".into())), Ok("x".to_string()));
|
||||
assert!(preview_reason(b"x", RawError::Corrupt("bad strip".into())).is_ok_and(|w| w.contains("bad strip")));
|
||||
assert!(preview_reason(b"x", RawError::Limit("too big")).is_ok());
|
||||
assert!(preview_reason(b"x", RawError::Tiff(lightcraft_tiff::TiffError::MissingTag(256))).is_ok());
|
||||
assert!(preview_reason(b"x", RawError::Tiff(craft_tiff::TiffError::MissingTag(256))).is_ok());
|
||||
assert!(preview_reason(b"x", RawError::NotRaw).is_err());
|
||||
assert!(preview_reason(cr3, RawError::Corrupt("bad".into())).is_ok_and(|w| w.starts_with("CR3 ")));
|
||||
}
|
||||
@@ -1740,8 +1746,8 @@ fn a_quick_thumbnail_without_an_embedded_preview_renders_the_raw() {
|
||||
/// A Sony ARW of `model`: a 64 × 48 uncompressed 14-bit CFA strip in a SubIFD (as `crates/raw/tests/it/vendor_robust.rs`
|
||||
/// lays it out) whose IFD carries the full-frame 16–35 mm distortion table (tag 0x7037).
|
||||
fn sony_arw(model: &str) -> Vec<u8> {
|
||||
use lightcraft_tiff::tags::{self as t, photometric};
|
||||
use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value};
|
||||
use craft_tiff::tags::{self as t, photometric};
|
||||
use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value};
|
||||
// 3:2, the aspect Sony's lens opcodes are limited to
|
||||
let (w, h) = (72u32, 48u32);
|
||||
let mut raw = IfdBuilder::new();
|
||||
|
||||
@@ -51,6 +51,7 @@ pub mod lens_profiles;
|
||||
pub mod lensfun_download;
|
||||
pub mod library;
|
||||
mod lightroom_archive;
|
||||
pub mod lightroom_backup;
|
||||
pub mod lightroom_catalog;
|
||||
pub mod lightroom_folder;
|
||||
pub mod lightroom_job;
|
||||
@@ -72,6 +73,7 @@ pub mod print_color;
|
||||
pub mod print_pdf;
|
||||
pub mod profile_refs;
|
||||
pub mod proof;
|
||||
pub(crate) mod read_file;
|
||||
pub mod rename;
|
||||
pub mod segment;
|
||||
pub mod sidecar;
|
||||
|
||||
@@ -519,6 +519,8 @@ mod tests {
|
||||
fn data(source: String) -> CatalogImport {
|
||||
CatalogImport {
|
||||
source,
|
||||
catalog_id: None,
|
||||
catalog_path: None,
|
||||
photos: Vec::new(),
|
||||
collections: Vec::new(),
|
||||
members: Vec::new(),
|
||||
@@ -543,6 +545,8 @@ mod tests {
|
||||
};
|
||||
CatalogImport {
|
||||
source: "source.lrcat".into(),
|
||||
catalog_id: None,
|
||||
catalog_path: None,
|
||||
photos: vec![photo],
|
||||
collections: vec![BTreeMap::from([("name".into(), serde_json::json!("Keepers"))])],
|
||||
members: vec![BTreeMap::from([("image".into(), serde_json::json!(7))])],
|
||||
|
||||
@@ -0,0 +1,558 @@
|
||||
//! Lightroom Classic catalog backups (issue #2592): `Backups/YYYY-MM-DD HHMM/<name>.zip`, which
|
||||
//! holds the catalog `<name>.lrcat` (and, since Classic 12, a small `<name>.lrcat-data/` folder that
|
||||
//! the import does not need). Someone who has left Lightroom may have nothing else: the import takes
|
||||
//! the zip as it is.
|
||||
//!
|
||||
//! The catalog entry is inflated by streaming into a private folder ([`Extracted`]), read from
|
||||
//! there like any `.lrcat`, and the folder is removed when the [`Extracted`] guard is dropped (after
|
||||
//! the read, on an error and on cancel alike). The zip itself is only read. The folder is made under
|
||||
//! the open library's `Interop/` when there is one (the library's disk, not a RAM-backed `/tmp`),
|
||||
//! else under [`std::env::temp_dir`]; a folder left by a crash (`lc-lrbackup-*`, older than a day)
|
||||
//! is removed by the next extraction there.
|
||||
//!
|
||||
//! Written from the ZIP application note (PKWARE APPNOTE.TXT 6.3: end of central directory,
|
||||
//! ZIP64 end record and locator, central directory headers, local headers, the ZIP64 extra field
|
||||
//! 0x0001) and RFC 1951 (deflate, inflated by `miniz_oxide`); no third-party zip code was used.
|
||||
//! Every size and offset comes from the file and is checked before use.
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Seek, SeekFrom, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
/// The largest catalog a backup may unpack to: the SQLite reader's cap on a database file.
|
||||
pub const MAX_CATALOG_BYTES: u64 = 16 << 30;
|
||||
/// The most central directory entries looked at (a Lightroom backup has one to four).
|
||||
const MAX_ENTRIES: u64 = 4096;
|
||||
/// The largest central directory read (names of `MAX_ENTRIES` entries fit easily).
|
||||
const MAX_DIRECTORY_BYTES: u64 = 4 << 20;
|
||||
/// Prefix of the private extraction folders, so leftovers can be found.
|
||||
const PREFIX: &str = "lc-lrbackup-";
|
||||
/// Leftover folders older than this are removed (one still being read is never this old).
|
||||
const STALE_SECS: u64 = 24 * 60 * 60;
|
||||
/// Bytes read from the zip and written to the catalog per step.
|
||||
const CHUNK: usize = 1 << 20;
|
||||
|
||||
/// Whether `path` names a Lightroom catalog backup: a `.zip` file (any case).
|
||||
pub fn is_backup(path: &Path) -> bool {
|
||||
path.extension().is_some_and(|e| e.eq_ignore_ascii_case("zip"))
|
||||
}
|
||||
|
||||
/// Where the catalog in a backup lived, for resolving the paths it records relative to itself, and
|
||||
/// whether that is known: `<catalog folder>/Backups/<date>/<name>.zip` (Lightroom's default
|
||||
/// layout) → `<catalog folder>/<name>.lrcat`, known; a zip anywhere else (a backup folder of the
|
||||
/// person's choosing, a zip moved away) → the `.lrcat` next to it, a guess that is only used to
|
||||
/// resolve paths, never to recognise the catalog.
|
||||
pub fn original_catalog(zip: &Path, entry: &str) -> (PathBuf, bool) {
|
||||
let name = Path::new(entry).file_name().map_or_else(|| PathBuf::from("catalog.lrcat"), PathBuf::from);
|
||||
let dated = zip.parent();
|
||||
let backups = dated.and_then(Path::parent);
|
||||
match backups {
|
||||
Some(b) if b.file_name().is_some_and(|n| n.eq_ignore_ascii_case("backups")) => (b.parent().unwrap_or(b).join(name), true),
|
||||
_ => (dated.map_or_else(|| name.clone(), |d| d.join(&name)), false),
|
||||
}
|
||||
}
|
||||
|
||||
/// A catalog unpacked from a backup; its private folder is removed when this is dropped.
|
||||
#[derive(Debug)]
|
||||
pub struct Extracted {
|
||||
dir: PathBuf,
|
||||
/// The unpacked `.lrcat`.
|
||||
pub catalog: PathBuf,
|
||||
/// The catalog's name inside the zip.
|
||||
pub entry: String,
|
||||
}
|
||||
|
||||
impl Drop for Extracted {
|
||||
/// Removes the private folder and the catalog in it; a failure is logged, not raised (drop can't fail).
|
||||
fn drop(&mut self) {
|
||||
if let Err(e) = std::fs::remove_dir_all(&self.dir)
|
||||
&& e.kind() != std::io::ErrorKind::NotFound
|
||||
{
|
||||
log::warn!("Lightroom backup: could not remove {}: {e}", self.dir.display());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One file in the zip's central directory.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
struct Entry {
|
||||
name: String,
|
||||
method: u16,
|
||||
crc: u32,
|
||||
compressed: u64,
|
||||
size: u64,
|
||||
local: u64,
|
||||
}
|
||||
|
||||
/// The little-endian `u16` at `at` in `b`, or `None` past its end.
|
||||
fn u16_at(b: &[u8], at: usize) -> Option<u16> {
|
||||
let s = b.get(at..at.checked_add(2)?)?;
|
||||
Some(u16::from_le_bytes([s[0], s[1]]))
|
||||
}
|
||||
|
||||
/// The little-endian `u32` at `at` in `b`, or `None` past its end.
|
||||
fn u32_at(b: &[u8], at: usize) -> Option<u32> {
|
||||
let s = b.get(at..at.checked_add(4)?)?;
|
||||
Some(u32::from_le_bytes([s[0], s[1], s[2], s[3]]))
|
||||
}
|
||||
|
||||
/// The little-endian `u64` at `at` in `b`, or `None` past its end.
|
||||
fn u64_at(b: &[u8], at: usize) -> Option<u64> {
|
||||
let s = b.get(at..at.checked_add(8)?)?;
|
||||
let mut a = [0u8; 8];
|
||||
a.copy_from_slice(s);
|
||||
Some(u64::from_le_bytes(a))
|
||||
}
|
||||
|
||||
/// `len` bytes of `f` from offset `at`; a length that doesn't fit memory or a short read is an error.
|
||||
fn read_at(f: &mut File, at: u64, len: u64) -> Result<Vec<u8>, String> {
|
||||
let len = usize::try_from(len).map_err(|_| "zip record too large".to_string())?;
|
||||
f.seek(SeekFrom::Start(at)).map_err(|e| e.to_string())?;
|
||||
let mut buf = vec![0u8; len];
|
||||
f.read_exact(&mut buf).map_err(|e| format!("truncated zip: {e}"))?;
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
/// The central directory's entries (offset and count from the end record, or its ZIP64 record).
|
||||
fn entries(f: &mut File) -> Result<Vec<Entry>, String> {
|
||||
let len = f.metadata().map_err(|e| e.to_string())?.len();
|
||||
// end of central directory: 22 bytes plus a comment of up to 64 KiB
|
||||
let tail_len = len.min(22 + 65_535);
|
||||
let tail_at = len - tail_len;
|
||||
let tail = read_at(f, tail_at, tail_len)?;
|
||||
let eocd = (0..tail.len().saturating_sub(21)).rev().find(|&i| tail.get(i..).is_some_and(|b| b.starts_with(b"PK\x05\x06")));
|
||||
let Some(eocd) = eocd else { return Err("not a zip file (no end of central directory)".into()) };
|
||||
let bad = || "damaged zip (end of central directory)".to_string();
|
||||
let mut count = u64::from(u16_at(&tail, eocd + 10).ok_or_else(bad)?);
|
||||
let mut dir_len = u64::from(u32_at(&tail, eocd + 12).ok_or_else(bad)?);
|
||||
let mut dir_at = u64::from(u32_at(&tail, eocd + 16).ok_or_else(bad)?);
|
||||
// ZIP64: a locator just before the end record points at the ZIP64 end record
|
||||
if (count == 0xFFFF || dir_len == 0xFFFF_FFFF || dir_at == 0xFFFF_FFFF)
|
||||
&& let Some(loc) = eocd.checked_sub(20)
|
||||
&& tail.get(loc..).is_some_and(|b| b.starts_with(b"PK\x06\x07"))
|
||||
{
|
||||
let rec_at = u64_at(&tail, loc + 8).ok_or_else(bad)?;
|
||||
let rec = read_at(f, rec_at, 56)?;
|
||||
if !rec.starts_with(b"PK\x06\x06") {
|
||||
return Err("damaged zip (ZIP64 end record)".into());
|
||||
}
|
||||
count = u64_at(&rec, 32).ok_or_else(bad)?;
|
||||
dir_len = u64_at(&rec, 40).ok_or_else(bad)?;
|
||||
dir_at = u64_at(&rec, 48).ok_or_else(bad)?;
|
||||
}
|
||||
if count > MAX_ENTRIES || dir_len > MAX_DIRECTORY_BYTES || dir_at.checked_add(dir_len).is_none_or(|end| end > len) {
|
||||
return Err("damaged zip (central directory out of range)".into());
|
||||
}
|
||||
let dir = read_at(f, dir_at, dir_len)?;
|
||||
let mut out = Vec::new();
|
||||
let mut at = 0usize;
|
||||
let bad = || "damaged zip (central directory)".to_string();
|
||||
for _ in 0..count {
|
||||
if !dir.get(at..).is_some_and(|b| b.starts_with(b"PK\x01\x02")) {
|
||||
return Err(bad());
|
||||
}
|
||||
let method = u16_at(&dir, at + 10).ok_or_else(bad)?;
|
||||
let crc = u32_at(&dir, at + 16).ok_or_else(bad)?;
|
||||
let mut compressed = u64::from(u32_at(&dir, at + 20).ok_or_else(bad)?);
|
||||
let mut size = u64::from(u32_at(&dir, at + 24).ok_or_else(bad)?);
|
||||
let name_len = usize::from(u16_at(&dir, at + 28).ok_or_else(bad)?);
|
||||
let extra_len = usize::from(u16_at(&dir, at + 30).ok_or_else(bad)?);
|
||||
let comment_len = usize::from(u16_at(&dir, at + 32).ok_or_else(bad)?);
|
||||
let mut local = u64::from(u32_at(&dir, at + 42).ok_or_else(bad)?);
|
||||
let name_at = at + 46;
|
||||
let name_end = name_at.checked_add(name_len).ok_or_else(bad)?;
|
||||
let name = String::from_utf8_lossy(dir.get(name_at..name_end).ok_or_else(bad)?).replace('\\', "/");
|
||||
let extra = dir.get(name_end..name_end.checked_add(extra_len).ok_or_else(bad)?).ok_or_else(bad)?;
|
||||
// ZIP64 extra field: the 64-bit values, in this order, of those set to 0xFFFFFFFF
|
||||
let mut e = 0usize;
|
||||
while e + 4 <= extra.len() {
|
||||
let (id, len) = (u16_at(extra, e).ok_or_else(bad)?, usize::from(u16_at(extra, e + 2).ok_or_else(bad)?));
|
||||
let body = extra.get(e + 4..e + 4 + len).ok_or_else(bad)?;
|
||||
if id == 0x0001 {
|
||||
let mut v = 0usize;
|
||||
for field in [&mut size, &mut compressed, &mut local] {
|
||||
if *field == 0xFFFF_FFFF {
|
||||
*field = u64_at(body, v).ok_or_else(bad)?;
|
||||
v += 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
e += 4 + len;
|
||||
}
|
||||
at = name_end.checked_add(extra_len).and_then(|x| x.checked_add(comment_len)).ok_or_else(bad)?;
|
||||
out.push(Entry { name, method, crc, compressed, size, local });
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The backup's catalog entry: the one `*.lrcat` file in the zip, outside macOS resource forks.
|
||||
fn catalog_entry(entries: &[Entry]) -> Result<&Entry, String> {
|
||||
let mut found = entries.iter().filter(|e| {
|
||||
let lower = e.name.to_lowercase();
|
||||
lower.ends_with(".lrcat") && !lower.split('/').any(|p| p == "__macosx" || p.starts_with("._"))
|
||||
});
|
||||
match (found.next(), found.next()) {
|
||||
(Some(e), None) => Ok(e),
|
||||
(None, _) => Err("this zip holds no Lightroom catalog (.lrcat)".into()),
|
||||
(Some(_), Some(_)) => Err("this zip holds more than one Lightroom catalog (.lrcat); unzip it and import one".into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the zip at `path` holds exactly one Lightroom catalog, by its central directory only
|
||||
/// (nothing is unpacked). Any read error means no.
|
||||
pub fn holds_catalog(path: &Path) -> bool {
|
||||
File::open(path).ok().and_then(|mut f| entries(&mut f).ok()).is_some_and(|all| catalog_entry(&all).is_ok())
|
||||
}
|
||||
|
||||
/// Remove `lc-lrbackup-*` folders in `root` left by an earlier run that ended without cleaning up.
|
||||
fn sweep(root: &Path) {
|
||||
let Ok(dir) = std::fs::read_dir(root) else { return };
|
||||
for entry in dir.take(10_000).flatten() {
|
||||
if !entry.file_name().to_str().is_some_and(|n| n.starts_with(PREFIX)) {
|
||||
continue;
|
||||
}
|
||||
// only a real folder of ours: `DirEntry::metadata` doesn't follow a link (Unix), and a link named
|
||||
// like one is skipped explicitly rather than relying on `remove_dir_all` removing just the link
|
||||
let Ok(meta) = entry.metadata() else { continue };
|
||||
let stale = meta.modified().ok().and_then(|t| t.elapsed().ok()).is_some_and(|age| age.as_secs() > STALE_SECS);
|
||||
if meta.is_dir()
|
||||
&& !meta.file_type().is_symlink()
|
||||
&& stale
|
||||
&& let Err(e) = std::fs::remove_dir_all(entry.path())
|
||||
{
|
||||
log::warn!("Lightroom backup: could not remove the leftover {}: {e}", entry.path().display());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Refuse a known shortage; an unavailable space reading is left to normal write-error handling.
|
||||
fn check_space(root: &Path, needed: u64, available: Option<u64>) -> Result<(), String> {
|
||||
if let Some(available) = available
|
||||
&& needed > available
|
||||
{
|
||||
return Err(format!("not enough free space in {} to unpack this catalog ({} bytes needed, {} available)", root.display(), needed, available));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Unpack the catalog of the backup `zip` into a new private folder under `root`, checking its
|
||||
/// size and CRC-32. Stops with an error when `cancel` is set; the folder is removed then too.
|
||||
pub fn extract(zip: &Path, root: &Path, cancel: &AtomicBool) -> Result<Extracted, String> {
|
||||
let mut f = File::open(zip).map_err(|e| format!("{}: {e}", zip.display()))?;
|
||||
let all = entries(&mut f)?;
|
||||
let entry = catalog_entry(&all)?.clone();
|
||||
if entry.size > MAX_CATALOG_BYTES {
|
||||
return Err(format!("the catalog in this backup unpacks to {} GB, more than LightCraft reads", entry.size >> 30));
|
||||
}
|
||||
std::fs::create_dir_all(root).map_err(|e| format!("{}: {e}", root.display()))?;
|
||||
check_space(root, entry.size, crate::disk_space::read(root).map(|space| space.available))?;
|
||||
sweep(root);
|
||||
let stamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos());
|
||||
let dir = root.join(format!("{PREFIX}{}-{stamp}", std::process::id()));
|
||||
// owner-only from the start on Unix (a catalog holds the person's paths and metadata)
|
||||
#[cfg(unix)]
|
||||
let builder = {
|
||||
let mut builder = std::fs::DirBuilder::new();
|
||||
std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700);
|
||||
builder
|
||||
};
|
||||
#[cfg(not(unix))]
|
||||
let builder = std::fs::DirBuilder::new();
|
||||
builder.create(&dir).map_err(|e| format!("{}: {e}", dir.display()))?;
|
||||
// a fixed name, not the zip's: an entry named `NUL.lrcat` or `a:b.lrcat` would be a device or an
|
||||
// alternate data stream on Windows. The guard exists before the first byte is written: every
|
||||
// early return below cleans up.
|
||||
let out = Extracted { catalog: dir.join("catalog.lrcat"), dir, entry: entry.name.clone() };
|
||||
let local = read_at(&mut f, entry.local, 30)?;
|
||||
if !local.starts_with(b"PK\x03\x04") {
|
||||
return Err("damaged zip (local header)".into());
|
||||
}
|
||||
let bad = || "damaged zip (local header)".to_string();
|
||||
let skip = u64::from(u16_at(&local, 26).ok_or_else(bad)?) + u64::from(u16_at(&local, 28).ok_or_else(bad)?);
|
||||
let data_at = entry.local.checked_add(30 + skip).ok_or_else(bad)?;
|
||||
f.seek(SeekFrom::Start(data_at)).map_err(|e| e.to_string())?;
|
||||
let mut input = f.take(entry.compressed);
|
||||
// created new: a file or a link already in its place is an error, never written through
|
||||
let file = std::fs::OpenOptions::new().write(true).create_new(true).open(&out.catalog).map_err(|e| format!("{}: {e}", out.catalog.display()))?;
|
||||
let mut writer = std::io::BufWriter::with_capacity(CHUNK, file);
|
||||
let mut crc = crc32fast::Hasher::new();
|
||||
let mut written = 0u64;
|
||||
let mut write = |bytes: &[u8], written: &mut u64| -> Result<(), String> {
|
||||
*written = written.saturating_add(bytes.len() as u64);
|
||||
if *written > entry.size {
|
||||
return Err("damaged zip (the catalog is longer than the zip says)".into());
|
||||
}
|
||||
crc.update(bytes);
|
||||
writer.write_all(bytes).map_err(|e| format!("writing {}: {e}", out.catalog.display()))
|
||||
};
|
||||
let mut buf = vec![0u8; CHUNK];
|
||||
match entry.method {
|
||||
0 => loop {
|
||||
if cancel.load(Ordering::Relaxed) {
|
||||
return Err("cancelled".into());
|
||||
}
|
||||
let n = input.read(&mut buf).map_err(|e| e.to_string())?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
write(buf.get(..n).unwrap_or_default(), &mut written)?;
|
||||
},
|
||||
8 => {
|
||||
use miniz_oxide::inflate::stream::{InflateState, inflate};
|
||||
use miniz_oxide::{DataFormat, MZFlush, MZStatus};
|
||||
let mut state = InflateState::new_boxed(DataFormat::Raw);
|
||||
let mut out_buf = vec![0u8; CHUNK];
|
||||
let (mut start, mut end) = (0usize, 0usize);
|
||||
loop {
|
||||
if cancel.load(Ordering::Relaxed) {
|
||||
return Err("cancelled".into());
|
||||
}
|
||||
if start == end {
|
||||
end = input.read(&mut buf).map_err(|e| e.to_string())?;
|
||||
start = 0;
|
||||
}
|
||||
let flush = if end == 0 { MZFlush::Finish } else { MZFlush::None };
|
||||
let r = inflate(&mut state, buf.get(start..end).unwrap_or_default(), &mut out_buf, flush);
|
||||
start = start.saturating_add(r.bytes_consumed).min(end);
|
||||
write(out_buf.get(..r.bytes_written).unwrap_or_default(), &mut written)?;
|
||||
match r.status {
|
||||
Ok(MZStatus::StreamEnd) => break,
|
||||
Ok(_) => {}
|
||||
// no progress with all input given: the stream is cut short
|
||||
Err(miniz_oxide::MZError::Buf) if end == 0 && r.bytes_written == 0 => return Err("damaged zip (the catalog is cut short)".into()),
|
||||
Err(miniz_oxide::MZError::Buf) => {}
|
||||
Err(e) => return Err(format!("damaged zip (deflate: {e:?})")),
|
||||
}
|
||||
if r.bytes_consumed == 0 && r.bytes_written == 0 && start < end {
|
||||
return Err("damaged zip (the catalog's data does not inflate)".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
m => return Err(format!("this zip uses compression method {m}, which LightCraft does not read; unzip it and import the .lrcat")),
|
||||
}
|
||||
writer.flush().map_err(|e| format!("writing {}: {e}", out.catalog.display()))?;
|
||||
drop(writer);
|
||||
if written != entry.size {
|
||||
return Err("damaged zip (the catalog is shorter than the zip says)".into());
|
||||
}
|
||||
if crc.finalize() != entry.crc {
|
||||
return Err("damaged zip (the catalog's checksum does not match)".into());
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
#[test]
|
||||
fn free_space_check_refuses_known_shortage() {
|
||||
let root = std::path::Path::new("library");
|
||||
assert!(super::check_space(root, 11, Some(10)).unwrap_err().contains("not enough free space"));
|
||||
assert!(super::check_space(root, 10, Some(10)).is_ok());
|
||||
assert!(super::check_space(root, 11, None).is_ok());
|
||||
}
|
||||
use super::*;
|
||||
|
||||
/// A zip of `files` (name, bytes), each stored or deflated; `zip64` writes ZIP64 records and
|
||||
/// extra fields (with the 32-bit fields set to 0xFFFFFFFF) as large archives do.
|
||||
pub(crate) fn zip(files: &[(&str, &[u8])], deflate: bool, zip64: bool) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
let mut central = Vec::new();
|
||||
for (name, data) in files {
|
||||
let body = if deflate { miniz_oxide::deflate::compress_to_vec(data, 6) } else { data.to_vec() };
|
||||
let crc = crc32fast::hash(data);
|
||||
let local = out.len() as u64;
|
||||
let method: u16 = if deflate { 8 } else { 0 };
|
||||
let big = |v: u64| if zip64 { 0xFFFF_FFFFu32 } else { v as u32 };
|
||||
let mut extra = Vec::new();
|
||||
if zip64 {
|
||||
extra.extend_from_slice(&1u16.to_le_bytes());
|
||||
extra.extend_from_slice(&24u16.to_le_bytes());
|
||||
extra.extend_from_slice(&(data.len() as u64).to_le_bytes());
|
||||
extra.extend_from_slice(&(body.len() as u64).to_le_bytes());
|
||||
extra.extend_from_slice(&local.to_le_bytes());
|
||||
}
|
||||
out.extend_from_slice(b"PK\x03\x04");
|
||||
out.extend_from_slice(&[20, 0, 0, 0]);
|
||||
out.extend_from_slice(&method.to_le_bytes());
|
||||
out.extend_from_slice(&[0; 4]);
|
||||
out.extend_from_slice(&crc.to_le_bytes());
|
||||
out.extend_from_slice(&big(body.len() as u64).to_le_bytes());
|
||||
out.extend_from_slice(&big(data.len() as u64).to_le_bytes());
|
||||
out.extend_from_slice(&(name.len() as u16).to_le_bytes());
|
||||
out.extend_from_slice(&0u16.to_le_bytes());
|
||||
out.extend_from_slice(name.as_bytes());
|
||||
out.extend_from_slice(&body);
|
||||
central.extend_from_slice(b"PK\x01\x02");
|
||||
central.extend_from_slice(&[45, 0, 45, 0, 0, 0]);
|
||||
central.extend_from_slice(&method.to_le_bytes());
|
||||
central.extend_from_slice(&[0; 4]);
|
||||
central.extend_from_slice(&crc.to_le_bytes());
|
||||
central.extend_from_slice(&big(body.len() as u64).to_le_bytes());
|
||||
central.extend_from_slice(&big(data.len() as u64).to_le_bytes());
|
||||
central.extend_from_slice(&(name.len() as u16).to_le_bytes());
|
||||
central.extend_from_slice(&(extra.len() as u16).to_le_bytes());
|
||||
// comment length, disk number, internal attributes, external attributes
|
||||
central.extend_from_slice(&[0; 6]);
|
||||
central.extend_from_slice(&0u32.to_le_bytes());
|
||||
central.extend_from_slice(&big(local).to_le_bytes());
|
||||
central.extend_from_slice(name.as_bytes());
|
||||
central.extend_from_slice(&extra);
|
||||
}
|
||||
let dir_at = out.len() as u64;
|
||||
out.extend_from_slice(¢ral);
|
||||
let count = files.len() as u64;
|
||||
if zip64 {
|
||||
let rec_at = out.len() as u64;
|
||||
out.extend_from_slice(b"PK\x06\x06");
|
||||
out.extend_from_slice(&44u64.to_le_bytes());
|
||||
out.extend_from_slice(&[45, 0, 45, 0]);
|
||||
out.extend_from_slice(&[0; 8]);
|
||||
out.extend_from_slice(&count.to_le_bytes());
|
||||
out.extend_from_slice(&count.to_le_bytes());
|
||||
out.extend_from_slice(&(central.len() as u64).to_le_bytes());
|
||||
out.extend_from_slice(&dir_at.to_le_bytes());
|
||||
out.extend_from_slice(b"PK\x06\x07");
|
||||
out.extend_from_slice(&0u32.to_le_bytes());
|
||||
out.extend_from_slice(&rec_at.to_le_bytes());
|
||||
out.extend_from_slice(&1u32.to_le_bytes());
|
||||
}
|
||||
out.extend_from_slice(b"PK\x05\x06");
|
||||
out.extend_from_slice(&[0; 4]);
|
||||
let c16 = if zip64 { 0xFFFF } else { count as u16 };
|
||||
out.extend_from_slice(&c16.to_le_bytes());
|
||||
out.extend_from_slice(&c16.to_le_bytes());
|
||||
out.extend_from_slice(&(if zip64 { 0xFFFF_FFFF } else { central.len() as u32 }).to_le_bytes());
|
||||
out.extend_from_slice(&(if zip64 { 0xFFFF_FFFF } else { dir_at as u32 }).to_le_bytes());
|
||||
out.extend_from_slice(&0u16.to_le_bytes());
|
||||
out
|
||||
}
|
||||
|
||||
/// A fresh, empty folder for one test.
|
||||
fn temp(tag: &str) -> PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("lc-lrbackup-test-{tag}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
/// The private extraction folders still in `root`.
|
||||
fn leftovers(root: &Path) -> Vec<String> {
|
||||
let Ok(dir) = std::fs::read_dir(root) else { return Vec::new() };
|
||||
dir.flatten().map(|e| e.file_name().to_string_lossy().into_owned()).filter(|n| n.starts_with(PREFIX)).collect()
|
||||
}
|
||||
|
||||
/// Stored, deflated and ZIP64 backups give back the catalog byte for byte, under the fixed name, and leave nothing.
|
||||
#[test]
|
||||
fn stored_deflated_and_zip64_backups_unpack_their_catalog_and_clean_up() {
|
||||
let catalog: Vec<u8> = (0..300_000u32).flat_map(|i| (i % 251).to_le_bytes()).collect();
|
||||
for (deflate, zip64) in [(false, false), (true, false), (true, true), (false, true)] {
|
||||
let dir = temp(&format!("ok-{deflate}-{zip64}"));
|
||||
let path = dir.join("Cat.zip");
|
||||
let files: [(&str, &[u8]); 3] = [("Cat.lrcat", &catalog), ("Cat.lrcat-data/CURRENT", b"MANIFEST-1\n"), ("__MACOSX/._Cat.lrcat", b"x")];
|
||||
std::fs::write(&path, zip(&files, deflate, zip64)).unwrap();
|
||||
let root = dir.join("tmp");
|
||||
let got = extract(&path, &root, &AtomicBool::new(false)).unwrap();
|
||||
assert_eq!(std::fs::read(&got.catalog).unwrap(), catalog, "deflate {deflate} zip64 {zip64}");
|
||||
assert_eq!(got.entry, "Cat.lrcat");
|
||||
assert_eq!(got.catalog.file_name().unwrap(), "catalog.lrcat", "a fixed name, never the zip's");
|
||||
drop(got);
|
||||
assert!(leftovers(&root).is_empty(), "the private folder is removed");
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// Not a zip, empty, no or two catalogs, cut short, bad CRC, or cancelled: an error, and no folder left.
|
||||
#[test]
|
||||
fn damaged_or_wrong_zips_are_errors_and_leave_nothing_behind() {
|
||||
let dir = temp("bad");
|
||||
let root = dir.join("tmp");
|
||||
let catalog = vec![7u8; 100_000];
|
||||
let good = zip(&[("Cat.lrcat", &catalog)], true, false);
|
||||
let cases: Vec<(&str, Vec<u8>)> = vec![
|
||||
("not a zip", b"SQLite format 3\0".to_vec()),
|
||||
("empty", Vec::new()),
|
||||
("no catalog", zip(&[("notes.txt", b"hi")], false, false)),
|
||||
("two catalogs", zip(&[("A.lrcat", b"a"), ("B.lrcat", b"b")], false, false)),
|
||||
("cut short", good.get(..good.len() / 2).unwrap().to_vec()),
|
||||
("bad crc", {
|
||||
let mut z = good.clone();
|
||||
// flip one byte of the deflated data (after the 30-byte local header and the name)
|
||||
z[30 + 9 + 10] ^= 0xFF;
|
||||
z
|
||||
}),
|
||||
];
|
||||
for (what, bytes) in cases {
|
||||
let path = dir.join("x.zip");
|
||||
std::fs::write(&path, &bytes).unwrap();
|
||||
assert!(extract(&path, &root, &AtomicBool::new(false)).is_err(), "{what} must fail");
|
||||
if root.exists() {
|
||||
assert!(leftovers(&root).is_empty(), "{what} left a folder");
|
||||
}
|
||||
}
|
||||
std::fs::write(dir.join("x.zip"), &good).unwrap();
|
||||
assert!(extract(&dir.join("x.zip"), &root, &AtomicBool::new(true)).is_err(), "cancel stops");
|
||||
assert!(leftovers(&root).is_empty(), "cancel cleans up");
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
/// A ZIP64 size of 1 TB is refused before anything is written.
|
||||
#[test]
|
||||
fn a_zip_that_lies_about_the_catalog_size_is_refused() {
|
||||
let dir = temp("size");
|
||||
let mut z = zip(&[("Cat.lrcat", &[1u8; 1000])], true, true);
|
||||
// the ZIP64 extra field's uncompressed size: 1 TB
|
||||
let at = z.windows(4).rposition(|w| w == [1, 0, 24, 0]).unwrap() + 4;
|
||||
z[at..at + 8].copy_from_slice(&(1u64 << 40).to_le_bytes());
|
||||
std::fs::write(dir.join("x.zip"), &z).unwrap();
|
||||
let err = extract(&dir.join("x.zip"), &dir.join("tmp"), &AtomicBool::new(false)).unwrap_err();
|
||||
assert!(err.contains("more than LightCraft reads"), "{err}");
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
/// A day-old leftover folder is removed; a recent one (another import still reading) is kept.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn stale_leftovers_are_swept_and_recent_ones_kept() {
|
||||
let root = temp("sweep");
|
||||
let old = root.join(format!("{PREFIX}1-1"));
|
||||
let recent = root.join(format!("{PREFIX}2-2"));
|
||||
std::fs::create_dir_all(&old).unwrap();
|
||||
std::fs::create_dir_all(&recent).unwrap();
|
||||
let past = std::time::SystemTime::now() - std::time::Duration::from_secs(STALE_SECS + 60);
|
||||
File::open(&old).unwrap().set_modified(past).unwrap();
|
||||
sweep(&root);
|
||||
assert!(!old.exists() && recent.exists());
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
}
|
||||
|
||||
/// Paths relative to the catalog resolve from where Lightroom kept it, not from the zip.
|
||||
#[test]
|
||||
fn the_original_catalog_is_two_folders_above_a_dated_backup() {
|
||||
let zip = Path::new("/p/Lightroom/Backups/2023-11-07 0900/Cat-v13.zip");
|
||||
assert_eq!(original_catalog(zip, "Cat-v13.lrcat"), (PathBuf::from("/p/Lightroom/Cat-v13.lrcat"), true));
|
||||
// a backup folder of the person's choosing: a guess for paths, not an identity
|
||||
assert_eq!(original_catalog(Path::new("/elsewhere/Cat.zip"), "sub/Cat.lrcat"), (PathBuf::from("/elsewhere/Cat.lrcat"), false));
|
||||
let chosen = Path::new("/Volumes/Ext/LR Backups/2026-10-22 1200/Cat.zip");
|
||||
assert!(!original_catalog(chosen, "Cat.lrcat").1);
|
||||
assert!(is_backup(Path::new("a/B.ZIP")) && !is_backup(Path::new("a/B.lrcat")));
|
||||
}
|
||||
|
||||
/// Folder detection counts a zip only when its directory lists exactly one catalog.
|
||||
#[test]
|
||||
fn only_a_zip_with_one_catalog_holds_one() {
|
||||
let dir = temp("holds");
|
||||
let cases: [(&str, Vec<u8>, bool); 4] = [
|
||||
("one.zip", zip(&[("Cat.lrcat", b"x"), ("Cat.lrcat-data/CURRENT", b"y")], true, false), true),
|
||||
("none.zip", zip(&[("photo.jpg", b"x")], false, false), false),
|
||||
("two.zip", zip(&[("A.lrcat", b"a"), ("B.lrcat", b"b")], false, false), false),
|
||||
("junk.zip", b"PK".to_vec(), false),
|
||||
];
|
||||
for (name, bytes, want) in cases {
|
||||
std::fs::write(dir.join(name), bytes).unwrap();
|
||||
assert_eq!(holds_catalog(&dir.join(name)), want, "{name}");
|
||||
}
|
||||
assert!(!holds_catalog(&dir.join("absent.zip")));
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -78,6 +78,15 @@ impl Reader {
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// The catalog's `Adobe_storeProviderID` (a UUID), kept by Lightroom in its backups; `None`
|
||||
/// when the table or the value is missing or doesn't look like an id.
|
||||
fn catalog_id(&self) -> Option<String> {
|
||||
let rows = self.table("Adobe_variablesTable", false).ok()?;
|
||||
let row = rows.iter().find(|r| text(r, "name") == "Adobe_storeProviderID")?;
|
||||
let id = text(row, "value").trim();
|
||||
(!id.is_empty() && id.len() <= 128 && id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')).then(|| id.to_string())
|
||||
}
|
||||
|
||||
fn optional_table(&self, name: &str, warnings: &mut Vec<String>) -> Result<Vec<Row>, String> {
|
||||
match self.table(name, false) {
|
||||
Ok(rows) => Ok(rows),
|
||||
@@ -176,6 +185,16 @@ impl CatalogPhoto {
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct CatalogImport {
|
||||
pub source: String,
|
||||
/// The catalog's own id (`Adobe_storeProviderID` in `Adobe_variablesTable`), the same in the
|
||||
/// catalog and in every backup of it, so a backup's collections match those of an earlier import
|
||||
/// of the catalog (issue #2592). `None` when the catalog has none.
|
||||
#[serde(default)]
|
||||
pub catalog_id: Option<String>,
|
||||
/// Where the catalog was when it isn't `source`: for a backup zip, the `.lrcat` it was backed up
|
||||
/// from. Collections imported by earlier releases are keyed on that path, so a backup still finds
|
||||
/// them. `None` when the catalog was read where it is.
|
||||
#[serde(default)]
|
||||
pub catalog_path: Option<String>,
|
||||
pub photos: Vec<CatalogPhoto>,
|
||||
pub collections: Vec<Row>,
|
||||
pub members: Vec<Row>,
|
||||
@@ -215,6 +234,11 @@ pub enum SkipReason {
|
||||
pub(crate) struct ImportIndex {
|
||||
pub(crate) photos: BTreeMap<String, u64>,
|
||||
pub(crate) collections: BTreeMap<String, u64>,
|
||||
/// The catalog id (`Adobe_storeProviderID`) each collection key was recorded for, when the
|
||||
/// import had one: a path key of one catalog never matches another catalog that later sits at
|
||||
/// the same path. Keys without an owner (earlier releases, catalogs without an id) match any.
|
||||
#[serde(default)]
|
||||
pub(crate) collection_owners: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
pub(crate) fn load_index(path: Option<&Path>) -> crate::Result<ImportIndex> {
|
||||
@@ -246,7 +270,12 @@ pub(crate) struct AppliedImport {
|
||||
}
|
||||
|
||||
fn identity(data: &CatalogImport, photo: &CatalogPhoto) -> String {
|
||||
if photo.uuid.is_empty() { format!("{}:{}", key(&data.source), photo.source_id) } else { photo.uuid.clone() }
|
||||
// (a backup's photos are recognised by the catalog it was made from, as the catalog's own are)
|
||||
if photo.uuid.is_empty() {
|
||||
format!("{}:{}", key(data.catalog_path.as_deref().unwrap_or(&data.source)), photo.source_id)
|
||||
} else {
|
||||
photo.uuid.clone()
|
||||
}
|
||||
}
|
||||
|
||||
fn validate(data: &CatalogImport) -> Result<(), String> {
|
||||
@@ -440,10 +469,47 @@ pub fn read(path: &Path) -> Result<CatalogImport, String> {
|
||||
/// stopping when `cancel` is set. Images whose catalog records are incomplete are left out and
|
||||
/// listed in [`CatalogImport::skipped`] instead of failing the read.
|
||||
pub fn read_with_progress(path: &Path, cancel: &AtomicBool, total: &AtomicUsize, done: &AtomicUsize) -> Result<CatalogImport, String> {
|
||||
read_source(path, None, cancel, total, done)
|
||||
}
|
||||
|
||||
/// Where a Lightroom backup's catalog is unpacked while it is read: the open library's `Interop/`
|
||||
/// folder (on the library's disk), or `None` for the system's temporary folder.
|
||||
pub fn backup_temp_root(s: &crate::Session) -> Option<PathBuf> {
|
||||
s.library.as_ref().filter(|l| l.on_disk).map(|l| l.dir.join("Interop"))
|
||||
}
|
||||
|
||||
/// [`read_with_progress`] of a catalog or of a Lightroom catalog backup (`.zip`, issue #2592): a
|
||||
/// backup's catalog is unpacked into a private folder under `temp_root` (else the system's temporary
|
||||
/// folder), read, and removed again ([`crate::lightroom_backup`]). Paths the catalog records
|
||||
/// relative to itself are taken from where the catalog was before it was backed up.
|
||||
pub fn read_source(
|
||||
path: &Path,
|
||||
temp_root: Option<&Path>,
|
||||
cancel: &AtomicBool,
|
||||
total: &AtomicUsize,
|
||||
done: &AtomicUsize,
|
||||
) -> Result<CatalogImport, String> {
|
||||
// 13 catalog tables plus one validation stage; ImportJob adds one unit per original path.
|
||||
total.store(14, Ordering::Relaxed);
|
||||
done.store(0, Ordering::Relaxed);
|
||||
ensure_read_active(cancel)?;
|
||||
if crate::lightroom_backup::is_backup(path) {
|
||||
let root = temp_root.map_or_else(std::env::temp_dir, Path::to_path_buf);
|
||||
let extracted = crate::lightroom_backup::extract(path, &root, cancel)?;
|
||||
let (document, known) = crate::lightroom_backup::original_catalog(path, &extracted.entry);
|
||||
let mut data = read_file(&extracted.catalog, &document, path, cancel, done)?;
|
||||
if !known {
|
||||
// only a guess: it resolves relative paths, but never names the catalog
|
||||
data.catalog_path = None;
|
||||
}
|
||||
return Ok(data);
|
||||
}
|
||||
read_file(path, path, path, cancel, done)
|
||||
}
|
||||
|
||||
/// Read the catalog file `path`, resolving the paths it records with `document` as the catalog's
|
||||
/// place and reporting `source` as where it came from.
|
||||
fn read_file(path: &Path, document: &Path, source: &Path, cancel: &AtomicBool, done: &AtomicUsize) -> Result<CatalogImport, String> {
|
||||
let before = std::fs::metadata(path).map_err(|e| e.to_string())?;
|
||||
let wal_path = PathBuf::from(format!("{}-wal", path.display()));
|
||||
let wal_before = std::fs::metadata(&wal_path).ok().map(|m| (m.len(), m.modified().ok()));
|
||||
@@ -529,7 +595,7 @@ pub fn read_with_progress(path: &Path, cancel: &AtomicBool, total: &AtomicUsize,
|
||||
root_ids.sort_unstable();
|
||||
for (id, root) in root_ids.iter().filter_map(|id| Some((*id, roots.get(id)?))) {
|
||||
let absolute = text(root, "absolutePath");
|
||||
let resolved = match relocated_root(absolute, text(root, "relativePathFromCatalog"), path) {
|
||||
let resolved = match relocated_root(absolute, text(root, "relativePathFromCatalog"), document) {
|
||||
Some(found) => {
|
||||
let found = found.to_string_lossy().to_string();
|
||||
warnings.push(format!("root folder {}: {absolute} not found; using {found} (relative to the catalog)", text(root, "name")));
|
||||
@@ -537,7 +603,7 @@ pub fn read_with_progress(path: &Path, cancel: &AtomicBool, total: &AtomicUsize,
|
||||
}
|
||||
None => absolute.to_string(),
|
||||
};
|
||||
if let Err(crate::doc_path::NotFollowed::Refused(why)) = crate::doc_path::resolve(&resolved, Some(path)) {
|
||||
if let Err(crate::doc_path::NotFollowed::Refused(why)) = crate::doc_path::resolve(&resolved, Some(document)) {
|
||||
warnings
|
||||
.push(format!("root folder {}: {resolved} is not opened: {why}; its photos are listed as missing and not added", text(root, "name")));
|
||||
refused_roots.insert(id);
|
||||
@@ -547,7 +613,9 @@ pub fn read_with_progress(path: &Path, cancel: &AtomicBool, total: &AtomicUsize,
|
||||
let mut history = group(optional_table("Adobe_libraryImageDevelopHistoryStep", &mut warnings)?);
|
||||
let mut snapshots = group(optional_table("Adobe_libraryImageDevelopSnapshot", &mut warnings)?);
|
||||
let mut out = CatalogImport {
|
||||
source: path.to_string_lossy().into(),
|
||||
source: source.to_string_lossy().into(),
|
||||
catalog_id: reader.catalog_id(),
|
||||
catalog_path: (document != source).then(|| document.to_string_lossy().into()),
|
||||
photos: Vec::new(),
|
||||
collections: optional_table("AgLibraryCollection", &mut warnings)?,
|
||||
members: optional_table("AgLibraryCollectionImage", &mut warnings)?,
|
||||
@@ -584,7 +652,7 @@ pub fn read_with_progress(path: &Path, cancel: &AtomicBool, total: &AtomicUsize,
|
||||
orphans.insert(id);
|
||||
continue;
|
||||
}
|
||||
let (photo_path, original) = original_path(root_path, folder_path, name, path);
|
||||
let (photo_path, original) = original_path(root_path, folder_path, name, document);
|
||||
if let Original::Refused(why) = &original
|
||||
&& !refused_roots.contains(&root_id)
|
||||
{
|
||||
@@ -1006,14 +1074,34 @@ fn stage_collections(
|
||||
if number(collection, "systemOnly") != 0 {
|
||||
continue;
|
||||
}
|
||||
let collection_key = format!("{}:{}", key(&data.source), number(collection, "id_local"));
|
||||
let indexed = index
|
||||
.collections
|
||||
.get(&collection_key)
|
||||
.copied()
|
||||
// keyed on the catalog's own id (the same in its backups); imports made before that was
|
||||
// read are keyed on the catalog's path, and are still found by it, also from a backup (the
|
||||
// path of the catalog it was backed up from)
|
||||
let path_key = format!("{}:{}", key(&data.source), number(collection, "id_local"));
|
||||
let original_key = data.catalog_path.as_ref().map(|p| format!("{}:{}", key(p), number(collection, "id_local")));
|
||||
let collection_key = data.catalog_id.as_ref().map_or_else(|| path_key.clone(), |c| format!("lrcat:{c}:{}", number(collection, "id_local")));
|
||||
// A path can be reused by a different catalog. Without an ID we cannot prove that an
|
||||
// owned path key belongs to this input, so refuse instead of silently mixing albums.
|
||||
if data.catalog_id.is_none()
|
||||
&& [Some(&path_key), original_key.as_ref()].into_iter().flatten().any(|k| index.collection_owners.contains_key(k))
|
||||
{
|
||||
return Err(error(
|
||||
"the Lightroom catalog has no stable identity and its path was previously associated with a catalog; cannot safely match collections",
|
||||
));
|
||||
}
|
||||
// a key recorded for another catalog (by its id) is not this one, whatever its path
|
||||
let mine = |k: &String| match (data.catalog_id.as_ref(), index.collection_owners.get(k)) {
|
||||
(Some(c), Some(owner)) => owner == c,
|
||||
_ => true,
|
||||
};
|
||||
let id = [Some(&collection_key), Some(&path_key), original_key.as_ref()]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|k| mine(k))
|
||||
.filter_map(|k| index.collections.get(k).copied())
|
||||
.map(lightcraft_catalog::AlbumId)
|
||||
.filter(|id| s.catalog.album(*id).is_some() && !assigned.contains(id));
|
||||
let id = match indexed {
|
||||
.find(|id| s.catalog.album(*id).is_some() && !assigned.contains(id));
|
||||
let id = match id {
|
||||
Some(id) => id,
|
||||
None => {
|
||||
let id = s.catalog.alloc_album_id();
|
||||
@@ -1027,7 +1115,16 @@ fn stage_collections(
|
||||
}
|
||||
};
|
||||
assigned.insert(id);
|
||||
index.collections.insert(collection_key, id.0);
|
||||
// recorded under its id and the live catalog's path (a backup's: the catalog it was made
|
||||
// from), so a later import of the same catalog finds it in either order; a zip's own path
|
||||
// is not kept when the catalog's is known (every dated backup has a new one)
|
||||
let path_kept = if data.catalog_path.is_some() { None } else { Some(path_key) };
|
||||
for k in [Some(collection_key), path_kept, original_key].into_iter().flatten() {
|
||||
if let Some(c) = &data.catalog_id {
|
||||
index.collection_owners.insert(k.clone(), c.clone());
|
||||
}
|
||||
index.collections.insert(k, id.0);
|
||||
}
|
||||
albums.insert(number(collection, "id_local"), id);
|
||||
}
|
||||
let parents: HashMap<_, _> = rows.iter().map(|r| (number(r, "id_local"), number(r, "parent"))).collect();
|
||||
|
||||
@@ -24,6 +24,8 @@ fn sample() -> CatalogImport {
|
||||
photo.image.insert("captureTime".into(), json!("2024-01-16T12:34:56"));
|
||||
CatalogImport {
|
||||
source: "test.lrcat".into(),
|
||||
catalog_id: None,
|
||||
catalog_path: None,
|
||||
photos: vec![copy, photo],
|
||||
collections: vec![
|
||||
row(json!({"id_local":9,"name":"Travel","creationId":"collection.group"})),
|
||||
@@ -1088,6 +1090,178 @@ fn wal_checksums_cover_commits_without_materializing_historical_databases() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Issue #2592: a catalog in Lightroom's backup layout (`Backups/<date>/<name>.zip`), with one
|
||||
/// collection and the catalog's own id.
|
||||
fn backup_fixture(root: &Path, catalog_id: &str) -> (PathBuf, PathBuf) {
|
||||
use SqlValue::{Integer as I, Null as N, Text as T};
|
||||
let mut tables: Vec<(&str, &str, Vec<Vec<SqlValue>>)> = one_row(photo_tables(("/catalog/", "")));
|
||||
tables.push((
|
||||
"AgLibraryCollection",
|
||||
"id_local INTEGER PRIMARY KEY, creationId, name, parent, systemOnly",
|
||||
vec![vec![N, T("com.adobe.ag.library.collection".into()), T("Keepers".into()), N, I(0)]],
|
||||
));
|
||||
tables.push(("AgLibraryCollectionImage", "id_local INTEGER PRIMARY KEY, collection, image", vec![vec![N, I(1), I(1)]]));
|
||||
tables.push((
|
||||
"Adobe_variablesTable",
|
||||
"id_local INTEGER PRIMARY KEY, id_global, name, type, value",
|
||||
vec![vec![N, T("v1".into()), T("Adobe_storeProviderID".into()), N, T(catalog_id.into())]],
|
||||
));
|
||||
let bytes = native_catalog_rows(&tables, None);
|
||||
let folder = root.join("Lightroom");
|
||||
let dated = folder.join("Backups").join("2023-11-07 0900");
|
||||
std::fs::create_dir_all(&dated).unwrap();
|
||||
let live = folder.join("Cat.lrcat");
|
||||
std::fs::write(&live, &bytes).unwrap();
|
||||
let zip = dated.join("Cat.zip");
|
||||
let files: [(&str, &[u8]); 2] = [("Cat.lrcat", &bytes), ("Cat.lrcat-data/CURRENT", b"MANIFEST-000005\n")];
|
||||
std::fs::write(&zip, crate::lightroom_backup::tests::zip(&files, true, false)).unwrap();
|
||||
(live, zip)
|
||||
}
|
||||
|
||||
/// How many albums the library has.
|
||||
fn albums(s: &crate::Session) -> usize {
|
||||
s.catalog.albums().count()
|
||||
}
|
||||
|
||||
/// The zip imports the same photos and collections as its catalog, with the zip as source, no temp files and the zip unchanged.
|
||||
#[test]
|
||||
fn a_backup_zip_imports_like_its_catalog_and_leaves_no_temporary_files() {
|
||||
let root = std::env::temp_dir().join(format!("lightcraft-lrbackup-zip-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
let (live, zip) = backup_fixture(&root, "968AF29B-EEE2-481E-8AF9-55DD82965F49");
|
||||
let zip_before = std::fs::read(&zip).unwrap();
|
||||
let from_zip = read(&zip).unwrap();
|
||||
let from_live = read(&live).unwrap();
|
||||
assert_eq!(from_zip.source, zip.to_string_lossy(), "the zip is the import's source");
|
||||
assert_eq!(from_zip.catalog_id.as_deref(), Some("968AF29B-EEE2-481E-8AF9-55DD82965F49"));
|
||||
assert_eq!((from_zip.photos.len(), from_zip.collections.len()), (from_live.photos.len(), from_live.collections.len()));
|
||||
assert_eq!(from_zip.photos[0].path, from_live.photos[0].path);
|
||||
// the live catalog is gone (Lightroom uninstalled): the backup alone imports into a library
|
||||
std::fs::remove_file(&live).unwrap();
|
||||
let lib = root.join("Library");
|
||||
let mut s = crate::Session::new();
|
||||
s.open_library(&lib, false).unwrap();
|
||||
let report = import(&mut s, &zip, false).unwrap();
|
||||
assert_eq!((report["photos"].as_u64(), report["collections"].as_u64()), (Some(1), Some(1)));
|
||||
assert_eq!(report["source"], json!(zip.to_string_lossy()));
|
||||
assert_eq!(albums(&s), 1);
|
||||
let interop: Vec<_> = std::fs::read_dir(lib.join("Interop")).unwrap().flatten().map(|e| e.file_name().to_string_lossy().into_owned()).collect();
|
||||
assert!(!interop.iter().any(|n| n.starts_with("lc-lrbackup-")), "the unpacked catalog is removed: {interop:?}");
|
||||
assert_eq!(std::fs::read(&zip).unwrap(), zip_before, "the backup is only read");
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
/// Issue #2606: the backup of an imported catalog matches its collections by the catalog id.
|
||||
#[test]
|
||||
fn importing_a_backup_after_its_catalog_adds_no_album_twice() {
|
||||
let root = std::env::temp_dir().join(format!("lightcraft-lrbackup-twice-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
let (live, zip) = backup_fixture(&root, "968AF29B-EEE2-481E-8AF9-55DD82965F49");
|
||||
let mut s = crate::Session::new();
|
||||
s.open_library(root.join("Library"), false).unwrap();
|
||||
import(&mut s, &live, false).unwrap();
|
||||
assert_eq!(albums(&s), 1);
|
||||
let again = import(&mut s, &zip, false).unwrap();
|
||||
assert_eq!(again["imported"], 0, "the photos are matched by their Lightroom id");
|
||||
assert_eq!(albums(&s), 1, "the collections are matched by the catalog's id, not its path");
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
/// A backup of a catalog that an earlier release imported (collections keyed on the live
|
||||
/// `.lrcat` path, no catalog id) finds those albums through the path it was backed up from.
|
||||
#[test]
|
||||
fn a_backup_finds_collections_an_earlier_release_keyed_on_the_live_catalog_path() {
|
||||
let root = std::env::temp_dir().join(format!("lightcraft-lrbackup-legacy-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
let (live, zip) = backup_fixture(&root, "968AF29B-EEE2-481E-8AF9-55DD82965F49");
|
||||
let mut s = crate::Session::new();
|
||||
s.open_library(root.join("Library"), false).unwrap();
|
||||
// an earlier release: the live catalog imported without its id (path keys only)
|
||||
let mut old = read(&live).unwrap();
|
||||
old.catalog_id = None;
|
||||
apply(&mut s, old, false).unwrap();
|
||||
assert_eq!(albums(&s), 1);
|
||||
let backup = read(&zip).unwrap();
|
||||
assert_eq!(backup.catalog_path.as_deref(), Some(live.to_string_lossy().as_ref()), "where the zip's catalog was");
|
||||
apply(&mut s, backup, false).unwrap();
|
||||
assert_eq!(albums(&s), 1, "the backup finds the albums keyed on the live catalog's path");
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
/// Issue #2606, the other order: a backup without a catalog id imported first, then the live
|
||||
/// catalog (no id either) finds the same albums through the live path the backup recorded.
|
||||
#[test]
|
||||
fn the_live_catalog_after_a_backup_without_an_id_adds_no_album_twice() {
|
||||
let root = std::env::temp_dir().join(format!("lightcraft-lrbackup-noid-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
let (live, zip) = backup_fixture(&root, "968AF29B-EEE2-481E-8AF9-55DD82965F49");
|
||||
let mut s = crate::Session::new();
|
||||
s.open_library(root.join("Library"), false).unwrap();
|
||||
let mut backup = read(&zip).unwrap();
|
||||
backup.catalog_id = None;
|
||||
apply(&mut s, backup, false).unwrap();
|
||||
assert_eq!(albums(&s), 1);
|
||||
let mut current = read(&live).unwrap();
|
||||
current.catalog_id = None;
|
||||
apply(&mut s, current, false).unwrap();
|
||||
assert_eq!(albums(&s), 1, "the live catalog finds the albums the backup imported");
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
/// Issue #2606, the reviewer's second order: a backup with an id, then the live catalog read
|
||||
/// without one, is rejected rather than risk matching a different catalog at the same path.
|
||||
#[test]
|
||||
fn the_live_catalog_without_an_id_after_a_backup_with_one_adds_no_album_twice() {
|
||||
let root = std::env::temp_dir().join(format!("lightcraft-lrbackup-idthennone-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
let (live, zip) = backup_fixture(&root, "968AF29B-EEE2-481E-8AF9-55DD82965F49");
|
||||
let mut s = crate::Session::new();
|
||||
s.open_library(root.join("Library"), false).unwrap();
|
||||
apply(&mut s, read(&zip).unwrap(), false).unwrap();
|
||||
let mut current = read(&live).unwrap();
|
||||
current.catalog_id = None;
|
||||
let err = apply(&mut s, current, false).unwrap_err();
|
||||
assert!(err.to_string().contains("no stable identity"));
|
||||
assert_eq!(albums(&s), 1, "an ambiguous path must not change albums");
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
/// Two different catalogs (different ids) that sat at the same path, one after the other (a
|
||||
/// new computer's default location, a catalog recreated), keep their own albums even with the
|
||||
/// same collection `id_local`.
|
||||
#[test]
|
||||
fn two_catalogs_at_the_same_path_keep_their_own_albums() {
|
||||
let root = std::env::temp_dir().join(format!("lightcraft-lrcat-samepath-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
let (live, _) = backup_fixture(&root, "AAAAAAAA-0000-0000-0000-000000000001");
|
||||
let mut s = crate::Session::new();
|
||||
s.open_library(root.join("Library"), false).unwrap();
|
||||
apply(&mut s, read(&live).unwrap(), false).unwrap();
|
||||
// another catalog, now at the same path
|
||||
let (live2, _) = backup_fixture(&root, "BBBBBBBB-0000-0000-0000-000000000002");
|
||||
assert_eq!(live, live2);
|
||||
apply(&mut s, read(&live2).unwrap(), false).unwrap();
|
||||
assert_eq!(albums(&s), 2, "the second catalog's collection is its own album");
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
/// Libraries imported by earlier releases (path keys) still find their albums.
|
||||
#[test]
|
||||
fn collections_imported_before_the_catalog_id_was_read_are_still_matched_by_path() {
|
||||
let dir = std::env::temp_dir().join(format!("lightcraft-lrcat-pathkey-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let mut s = crate::Session::new();
|
||||
s.open_library(&dir, false).unwrap();
|
||||
// an import made by an earlier release: no catalog id, keys on the path
|
||||
apply(&mut s, sample(), false).unwrap();
|
||||
let before = albums(&s);
|
||||
let mut data = sample();
|
||||
data.catalog_id = Some("A-B".into());
|
||||
apply(&mut s, data, false).unwrap();
|
||||
assert_eq!(albums(&s), before, "the old path keys still find the albums");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persistent_reimport_keeps_copy_and_collection_ids_and_capture_time() {
|
||||
let dir = std::env::temp_dir().join(format!("lightcraft-lrcat-reimport-{}", std::process::id()));
|
||||
@@ -1175,7 +1349,9 @@ fn native_catalog_with_last_table_page(tables: &[(&str, &str, Vec<SqlValue>)], l
|
||||
/// table's root page.
|
||||
fn native_catalog_rows(tables: &[(&str, &str, Vec<Vec<SqlValue>>)], last_page: Option<i64>) -> Vec<u8> {
|
||||
use SqlValue::{Integer as I, Text as T};
|
||||
let mut bytes = vec![0u8; 5 * 2048];
|
||||
// the schema page, then one page per table (at least four: the photo tables)
|
||||
let pages = (tables.len() + 1).max(5);
|
||||
let mut bytes = vec![0u8; pages * 2048];
|
||||
bytes[..16].copy_from_slice(b"SQLite format 3\0");
|
||||
bytes[16..18].copy_from_slice(&2048u16.to_be_bytes());
|
||||
bytes[18] = 1;
|
||||
@@ -1183,7 +1359,7 @@ fn native_catalog_rows(tables: &[(&str, &str, Vec<Vec<SqlValue>>)], last_page: O
|
||||
bytes[21] = 64;
|
||||
bytes[22] = 32;
|
||||
bytes[23] = 32;
|
||||
bytes[28..32].copy_from_slice(&5u32.to_be_bytes());
|
||||
bytes[28..32].copy_from_slice(&(pages as u32).to_be_bytes());
|
||||
bytes[44..48].copy_from_slice(&4u32.to_be_bytes());
|
||||
bytes[56..60].copy_from_slice(&1u32.to_be_bytes());
|
||||
let mut schema = Vec::new();
|
||||
|
||||
@@ -6,14 +6,20 @@
|
||||
//! an empty LightCraft library inside Lightroom's folder; [`Session::open_library`] refuses with
|
||||
//! [`EngineError::LightroomCatalog`] instead, and the app offers File ▸ Import Lightroom Catalog….
|
||||
//!
|
||||
//! What counts (only names are looked at; no catalog is opened or read):
|
||||
//! What counts (only names are looked at, and a backup zip's list of files; no catalog is opened
|
||||
//! or read):
|
||||
//! - a LightCraft library (`catalog.snap` or `catalog.log` in the folder) is never a Lightroom
|
||||
//! catalog, whatever else the folder holds: it opens as before;
|
||||
//! - a path named `*.lrcat` or one of its companions (`*.lrcat-wal`, `*.lrcat-shm`,
|
||||
//! `*.lrcat-journal`, `*.lrcat.lock`);
|
||||
//! - a folder named `*.lrdata` or `*.lrcat-data` (Lightroom's previews and data);
|
||||
//! - a folder holding a `*.lrcat` at its top level (the catalog folder itself), or `Backups/`
|
||||
//! next to one.
|
||||
//! next to one;
|
||||
//! - Lightroom's backups (issue #2592), even with the catalog itself gone: a `Backups/` folder or
|
||||
//! one of its dated folders, `Backups/YYYY-MM-DD HHMM/`, holding a `*.zip` with one `.lrcat` in
|
||||
//! it ([`crate::lightroom_backup::holds_catalog`]). A dated folder names
|
||||
//! its zip; `Backups/` names one only when it holds a single backup, since the person has to choose
|
||||
//! among several (the import then asks for the file).
|
||||
//!
|
||||
//! The folder scan is bounded ([`SCAN_LIMIT`] entries, top level only) and every I/O error just
|
||||
//! means "not found": this never fails and never panics.
|
||||
@@ -133,6 +139,41 @@ pub fn lightroom_catalog_at(path: &Path) -> Option<LightroomPlace> {
|
||||
{
|
||||
return Some(LightroomPlace { catalog: Some(catalog) });
|
||||
}
|
||||
// `Backups/` alone (the catalog deleted): its backup when there is only one; with several,
|
||||
// which one to import is the person's choice
|
||||
if lower == "backups"
|
||||
&& let Some(backups) = backup_zips(path)
|
||||
{
|
||||
let catalog = if backups.len() == 1 { backups.into_iter().next() } else { None };
|
||||
return Some(LightroomPlace { catalog });
|
||||
}
|
||||
// a dated backup folder, `Backups/2023-11-07 0900/`
|
||||
if parent.and_then(lower_name).as_deref() == Some("backups")
|
||||
&& let Some(zip) = find_backup_zip(path)
|
||||
{
|
||||
return Some(LightroomPlace { catalog: Some(zip) });
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// The first `*.zip` file (by name) at the top level of `dir` that holds a Lightroom catalog,
|
||||
/// looking at no more than [`SCAN_LIMIT`] entries.
|
||||
fn find_backup_zip(dir: &Path) -> Option<PathBuf> {
|
||||
let entries = std::fs::read_dir(dir).ok()?;
|
||||
entries
|
||||
.take(SCAN_LIMIT)
|
||||
.flatten()
|
||||
.map(|e| e.path())
|
||||
.filter(|p| crate::lightroom_backup::is_backup(p) && p.is_file())
|
||||
.filter(|p| crate::lightroom_backup::holds_catalog(p))
|
||||
.min()
|
||||
}
|
||||
|
||||
/// The backup zips in the dated folders of `backups`, one per folder; `None` when there are none.
|
||||
fn backup_zips(backups: &Path) -> Option<Vec<PathBuf>> {
|
||||
let entries = std::fs::read_dir(backups).ok()?;
|
||||
let zips: Vec<PathBuf> =
|
||||
entries.take(SCAN_LIMIT).flatten().map(|e| e.path()).filter(|p| p.is_dir()).filter_map(|d| find_backup_zip(&d)).collect();
|
||||
(!zips.is_empty()).then_some(zips)
|
||||
}
|
||||
@@ -241,8 +241,14 @@ impl LightroomJob {
|
||||
return Err(cancelled());
|
||||
}
|
||||
// (reading stops with a message when `cancel` is set: the flag says it was cancelled)
|
||||
let mut data = lightroom_catalog::read_with_progress(&self.path, cancel, &self.progress.catalog_total, &self.progress.catalog_done)
|
||||
.map_err(|e| if cancel.load(Ordering::Relaxed) { cancelled() } else { error(e) })?;
|
||||
let mut data = lightroom_catalog::read_source(
|
||||
&self.path,
|
||||
self.archive_dir.as_deref(),
|
||||
cancel,
|
||||
&self.progress.catalog_total,
|
||||
&self.progress.catalog_done,
|
||||
)
|
||||
.map_err(|e| if cancel.load(Ordering::Relaxed) { cancelled() } else { error(e) })?;
|
||||
lightroom_catalog::validate_for_job(&data).map_err(error)?;
|
||||
if cancel.load(Ordering::Relaxed) {
|
||||
return Err(cancelled());
|
||||
@@ -350,6 +356,8 @@ mod tests {
|
||||
fn empty_data() -> CatalogImport {
|
||||
CatalogImport {
|
||||
source: "fixture.lrcat".into(),
|
||||
catalog_id: None,
|
||||
catalog_path: None,
|
||||
photos: Vec::new(),
|
||||
collections: Vec::new(),
|
||||
members: Vec::new(),
|
||||
@@ -384,6 +392,8 @@ mod tests {
|
||||
PreparedLightroom {
|
||||
data: CatalogImport {
|
||||
source: "fixture.lrcat".into(),
|
||||
catalog_id: None,
|
||||
catalog_path: None,
|
||||
photos: vec![lightroom_catalog::CatalogPhoto {
|
||||
source_id: 1,
|
||||
uuid: "fixture-photo".into(),
|
||||
|
||||
@@ -356,6 +356,21 @@ impl RenderJob {
|
||||
// converted for the display below
|
||||
let clip_view = self.request.overlay.is_clipping();
|
||||
let request = if clip_view { RenderRequest { display: None, ..self.request } } else { self.request };
|
||||
if let Err(e) = lightcraft_pipeline::check_brush_budget(src, &info, &self.settings, &request) {
|
||||
return RenderResult {
|
||||
request_id: self.request_id,
|
||||
source_key: self.source_key,
|
||||
photo: self.photo,
|
||||
level: self.level,
|
||||
key: self.key,
|
||||
rendered: Err(e),
|
||||
gpu_fallback: None,
|
||||
loaded: None,
|
||||
quick: None,
|
||||
display: None,
|
||||
frame: None,
|
||||
};
|
||||
}
|
||||
let pixels_needed =
|
||||
self.cache.is_some() || self.display.is_some() || (self.view_cache.is_some() && self.request.quality == Quality::Full);
|
||||
let frame = (self.on_screen && !pixels_needed && gpu)
|
||||
|
||||
@@ -122,9 +122,15 @@ impl OriginalGuard {
|
||||
self.others = std::sync::OnceLock::new();
|
||||
let mut put = |file: String, p: PathBuf, what: String| self.by_name.entry(file.to_lowercase()).or_default().push((p, what));
|
||||
put(name.clone(), path.to_path_buf(), format!("the original of {label}"));
|
||||
// every sidecar name the reader accepts (`.xmp`, `.XMP`, `.Xmp`, a recased photo extension), not only lowercase ones
|
||||
let stem = path.file_stem().map(|s| s.to_string_lossy().to_string()).unwrap_or_default();
|
||||
put(format!("{stem}.xmp"), path.with_extension("xmp"), format!("the XMP sidecar of {label}"));
|
||||
put(format!("{name}.xmp"), PathBuf::from(format!("{}.xmp", path.to_string_lossy())), format!("the XMP sidecar of {label}"));
|
||||
for c in crate::sidecar::sidecar_candidates(&path.to_string_lossy(), crate::sidecar::SidecarNaming::Stem) {
|
||||
if let Some(file) = c.file_name().map(|n| n.to_string_lossy().to_string()) {
|
||||
put(file, c, format!("the XMP sidecar of {label}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `Err` (a message for the user) when writing `target` would replace a protected file, or
|
||||
@@ -224,8 +230,15 @@ impl crate::Session {
|
||||
/// without building the guard over the whole library.
|
||||
pub fn check_write_target(&self, path: &str) -> Result<(), String> {
|
||||
let path = Path::new(path);
|
||||
if std::fs::symlink_metadata(path).is_err() && !is_xmp(path) {
|
||||
return Ok(()); // nothing there to replace, and no sidecar to create
|
||||
if std::fs::symlink_metadata(path).is_err() {
|
||||
// `missing/../IMG_1.jpg` names an existing file once the writer creates `missing`, so a
|
||||
// `..` that cannot be resolved now cannot be checked: refuse it.
|
||||
if path.components().any(|c| c == std::path::Component::ParentDir) && std::fs::canonicalize(folder_of(path)).is_err() {
|
||||
return Err(format!("{}: the folder part has `..` components that cannot be resolved; use a path without them", path.display()));
|
||||
}
|
||||
if !is_xmp(path) {
|
||||
return Ok(()); // nothing there to replace, and no sidecar to create
|
||||
}
|
||||
}
|
||||
self.original_guard().check(path)
|
||||
}
|
||||
@@ -324,6 +337,24 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sidecars_the_reader_accepts_in_other_cases_are_protected() {
|
||||
let d = std::env::temp_dir().join(format!("lc-originals-case-sidecar-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
std::fs::create_dir_all(&d).unwrap();
|
||||
let orig = d.join("IMG_1.jpg");
|
||||
std::fs::write(&orig, b"original").unwrap();
|
||||
let mut c = Catalog::default();
|
||||
let p = Photo::new(PhotoId(1), Source::File { path: orig.to_string_lossy().to_string() }, "IMG_1.jpg", "JPEG", 10, 10, "2026-01-01");
|
||||
c.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
|
||||
let g = OriginalGuard::new(&c);
|
||||
for name in ["IMG_1.XMP", "IMG_1.Xmp", "IMG_1.JPG.xmp", "IMG_1.jpg.XMP"] {
|
||||
std::fs::write(d.join(name), b"<x/>").unwrap();
|
||||
assert!(g.check(&d.join(name)).unwrap_err().contains("sidecar"), "{name}");
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_sidecar_that_isnt_there_yet_is_protected_too() {
|
||||
let d = std::env::temp_dir().join(format!("lc-originals-new-sidecar-{}", std::process::id()));
|
||||
|
||||
@@ -42,7 +42,13 @@ pub fn read_zip(bytes: &[u8]) -> Result<Vec<(String, Vec<u8>)>, String> {
|
||||
read_zip_limited(bytes, MAX_ZIP_ENTRY, MAX_ZIP_TOTAL)
|
||||
}
|
||||
|
||||
fn read_zip_limited(bytes: &[u8], max_entry: usize, max_total: usize) -> Result<Vec<(String, Vec<u8>)>, String> {
|
||||
pub(crate) fn read_zip_limited(bytes: &[u8], max_entry: usize, max_total: usize) -> Result<Vec<(String, Vec<u8>)>, String> {
|
||||
read_zip_spending(bytes, max_entry, max_total, &mut 0)
|
||||
}
|
||||
|
||||
/// [`read_zip_limited`] that adds to `spent` the bytes of each entry as it is unpacked, so they
|
||||
/// are counted when a later entry fails; an entry that fails for size spends its whole limit.
|
||||
pub(crate) fn read_zip_spending(bytes: &[u8], max_entry: usize, max_total: usize, spent: &mut usize) -> Result<Vec<(String, Vec<u8>)>, String> {
|
||||
let u16_at = |o: usize| bytes.get(o..o.checked_add(2)?).map(|b| u16::from_le_bytes([b[0], b[1]]) as usize);
|
||||
let u32_at = |o: usize| bytes.get(o..o.checked_add(4)?).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize);
|
||||
// offsets from the file, added up without overflowing (a 32-bit build reads them too)
|
||||
@@ -53,7 +59,7 @@ fn read_zip_limited(bytes: &[u8], max_entry: usize, max_total: usize) -> Result<
|
||||
let count = u16_at(eocd + 10).ok_or("bad zip")?;
|
||||
let mut at = u32_at(eocd + 16).ok_or("bad zip")?;
|
||||
let mut out = Vec::new();
|
||||
let mut total = 0usize;
|
||||
let total = spent;
|
||||
for _ in 0..count {
|
||||
if !bytes.get(at..).is_some_and(|b| b.starts_with(b"PK\x01\x02")) {
|
||||
return Err("bad zip central directory".into());
|
||||
@@ -76,18 +82,40 @@ fn read_zip_limited(bytes: &[u8], max_entry: usize, max_total: usize) -> Result<
|
||||
}
|
||||
let data_at = add(add(add(local, 30)?, u16_at(add(local, 26)?).ok_or("bad zip")?)?, u16_at(add(local, 28)?).ok_or("bad zip")?)?;
|
||||
let data = bytes.get(data_at..add(data_at, csize)?).ok_or_else(|| format!("{name}: truncated"))?;
|
||||
let left = max_total.saturating_sub(total);
|
||||
let left = max_total.saturating_sub(*total);
|
||||
let limit = max_entry.min(left);
|
||||
let too_big = || format!("{name}: unpacks to more than {} MB, too much for a preset", limit >> 20);
|
||||
let content = match method {
|
||||
0 if data.len() > limit => return Err(too_big()),
|
||||
0 if data.len() > limit => {
|
||||
*total = total.saturating_add(limit);
|
||||
return Err(too_big());
|
||||
}
|
||||
0 => data.to_vec(),
|
||||
8 => miniz_oxide::inflate::decompress_to_vec_with_limit(data, usize_.max(1).saturating_mul(2).saturating_add(1024).min(limit)).map_err(
|
||||
|e| if matches!(e.status, miniz_oxide::inflate::TINFLStatus::HasMoreOutput) { too_big() } else { format!("{name}: {e:?}") },
|
||||
)?,
|
||||
8 => {
|
||||
// the entry's own stated size, with slack, caps the inflate below `limit`
|
||||
let cap = usize_.max(1).saturating_mul(2).saturating_add(1024);
|
||||
match miniz_oxide::inflate::decompress_to_vec_with_limit(data, cap.min(limit)) {
|
||||
Ok(v) => v,
|
||||
Err(e) if matches!(e.status, miniz_oxide::inflate::TINFLStatus::HasMoreOutput) && cap < limit => {
|
||||
// not over the budget: only what was unpacked is spent
|
||||
*total = total.saturating_add(e.output.len());
|
||||
return Err(format!("{name}: unpacks to more than its stated size"));
|
||||
}
|
||||
Err(e) if matches!(e.status, miniz_oxide::inflate::TINFLStatus::HasMoreOutput) => {
|
||||
// past the budget (or the entry limit): its whole limit is spent
|
||||
*total = total.saturating_add(limit);
|
||||
return Err(too_big());
|
||||
}
|
||||
Err(e) => {
|
||||
// what a corrupt stream unpacked before failing was still unpacked: charge it
|
||||
*total = total.saturating_add(e.output.len());
|
||||
return Err(format!("{name}: {:?}", e.status));
|
||||
}
|
||||
}
|
||||
}
|
||||
m => return Err(format!("{name}: unsupported compression method {m}")),
|
||||
};
|
||||
total = total.saturating_add(content.len());
|
||||
*total = total.saturating_add(content.len());
|
||||
out.push((name, content));
|
||||
}
|
||||
Ok(out)
|
||||
@@ -139,7 +167,11 @@ impl LuaParser<'_> {
|
||||
if self.s[self.i..].starts_with(b"--") {
|
||||
self.i += 2;
|
||||
if let Some(level) = self.long_bracket_level() {
|
||||
let _ = self.long_string(level);
|
||||
// an unterminated long comment runs to the end (as in Lua): going on past its
|
||||
// opening would search the rest of the file again for every such opening
|
||||
if self.long_string(level).is_err() {
|
||||
self.i = self.s.len();
|
||||
}
|
||||
} else {
|
||||
while self.i < self.s.len() && self.s[self.i] != b'\n' {
|
||||
self.i += 1;
|
||||
@@ -546,14 +578,23 @@ pub fn read_presets(name: &str, bytes: &[u8], group: Option<String>) -> Result<V
|
||||
"zip" => {
|
||||
let mut out = Vec::new();
|
||||
let mut errors = Vec::new();
|
||||
for (inner, data) in read_zip(bytes)? {
|
||||
let entries = read_zip(bytes)?;
|
||||
// what the archive's entries left of the unpack budget: the Luminar packs inside it
|
||||
// (archives themselves) unpack within it, not each with a budget of its own
|
||||
let mut budget = MAX_ZIP_TOTAL.saturating_sub(entries.iter().map(|(_, d)| d.len()).sum());
|
||||
for (inner, data) in entries {
|
||||
let iext = Path::new(&inner).extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).unwrap_or_default();
|
||||
if !(PRESET_EXTS.contains(&iext.as_str()) || iext == crate::presets::LCPRESET_EXT) || iext == "zip" {
|
||||
continue;
|
||||
}
|
||||
let dir_group =
|
||||
inner.rsplit_once('/').and_then(|(d, _)| group_from_dir(d)).or_else(|| group_from_dir(&stem)).or_else(|| group.clone());
|
||||
match read_presets(&inner, &data, dir_group) {
|
||||
let read = if iext == "mplumpack" {
|
||||
crate::preset_luminar::read_mplumpack_within(&inner, &data, &mut budget)
|
||||
} else {
|
||||
read_presets(&inner, &data, dir_group)
|
||||
};
|
||||
match read {
|
||||
Ok(v) => out.extend(v),
|
||||
Err(e) => errors.push(format!("{inner}: {e}")),
|
||||
}
|
||||
@@ -771,6 +812,68 @@ text]], z = ZSTR "loc" }"#,
|
||||
assert!(photo(r#"crs:WhiteBalance="As Shot""#).unmapped.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn packs_inside_a_zip_share_its_unpack_budget() {
|
||||
// two packs unpacking to 2 MB each on a 3 MB budget: the first spends it, the second is refused
|
||||
let pad = vec![b' '; 2 << 20];
|
||||
let pack = zip(&[("a.lmp", b"<plist/>"), ("pad.txt", &pad)], true);
|
||||
let mut budget = 3 << 20;
|
||||
let first = crate::preset_luminar::read_mplumpack_within("one.mplumpack", &pack, &mut budget).unwrap_err();
|
||||
assert!(!first.contains("too much"), "{first}");
|
||||
assert_eq!(budget, (1 << 20) - 8);
|
||||
let second = crate::preset_luminar::read_mplumpack_within("two.mplumpack", &pack, &mut budget).unwrap_err();
|
||||
assert!(second.contains("too much"), "{second}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pack_that_fails_part_way_still_spends_what_it_unpacked() {
|
||||
// 2 MB unpacks, then an entry that does not fit the 3 MB budget: the next pack gets nothing
|
||||
let pad = vec![b' '; 2 << 20];
|
||||
let pack = zip(&[("a.txt", &pad), ("b.txt", &pad)], true);
|
||||
let mut budget = 3 << 20;
|
||||
let first = crate::preset_luminar::read_mplumpack_within("one.mplumpack", &pack, &mut budget).unwrap_err();
|
||||
assert!(first.contains("too much"), "{first}");
|
||||
assert_eq!(budget, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_corrupt_deflate_entry_spends_what_it_unpacked() {
|
||||
// 2 MB declared, the stream cut in half: it unpacks a part, then fails; the budget keeps the part
|
||||
let pad = vec![b' '; 2 << 20];
|
||||
let deflated = miniz_oxide::deflate::compress_to_vec(&pad, 6);
|
||||
let mut pack = zip(&[("a.txt", &deflated[..deflated.len() / 2])], false);
|
||||
let central = 30 + "a.txt".len() + deflated.len() / 2;
|
||||
pack[8] = 8; // local header: method deflate
|
||||
pack[central + 10] = 8; // central header: method deflate
|
||||
pack[central + 24..central + 28].copy_from_slice(&(pad.len() as u32).to_le_bytes());
|
||||
let mut budget = 3 << 20;
|
||||
let err = crate::preset_luminar::read_mplumpack_within("one.mplumpack", &pack, &mut budget).unwrap_err();
|
||||
assert!(!err.contains("too much"), "{err}");
|
||||
assert!(budget < 3 << 20, "{budget}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unterminated_long_comment_ends_the_template() {
|
||||
// 20 000 openings of a long comment that never closes: each used to search the rest of
|
||||
// the file for its close (seconds to minutes); now the first ends the file
|
||||
let text = format!("s = {{ a = 1 }} {}", "--[==[ ".repeat(20_000));
|
||||
let t = std::time::Instant::now();
|
||||
let _ = parse_lua(&text);
|
||||
assert!(t.elapsed().as_secs() < 5, "took {:?}", t.elapsed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_entry_past_its_stated_size_spends_only_what_it_unpacked() {
|
||||
// the stated size (1 byte) caps the inflate at 1026 bytes, far below the 1 MB limit
|
||||
let mut lying = zip(&[("a.xmp", &vec![b' '; 10_000])], true);
|
||||
let cd = lying.windows(4).position(|w| w == b"PK\x01\x02").unwrap();
|
||||
lying[cd + 24..cd + 28].copy_from_slice(&1u32.to_le_bytes());
|
||||
let mut spent = 0;
|
||||
let e = read_zip_spending(&lying, 1 << 20, 1 << 20, &mut spent).unwrap_err();
|
||||
assert!(e.contains("stated size"), "{e}");
|
||||
assert!(spent > 0 && spent <= 2048, "{spent}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zip_bombs_are_refused() {
|
||||
// an entry unpacking past the limit (3 MB of zeros pack into a few kB): refused, named
|
||||
|
||||
@@ -19,7 +19,7 @@ use std::collections::BTreeMap;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::crs::Props;
|
||||
use crate::preset_import::{Imported, build, group_from_dir, read_zip};
|
||||
use crate::preset_import::{Imported, MAX_ZIP_ENTRY, MAX_ZIP_TOTAL, build, group_from_dir, read_zip_spending};
|
||||
|
||||
// ----------------------------------------------------------------------------- property lists
|
||||
|
||||
@@ -72,13 +72,26 @@ struct Parser<'a> {
|
||||
/// Deepest `<dict>` / `<array>` nesting read (real looks nest a handful of levels).
|
||||
const MAX_DEPTH: usize = 64;
|
||||
|
||||
/// Index of the `;` that ends a reference starting at `rest` (its `&`). The scan stops at the first
|
||||
/// byte that cannot appear in a name or a numeric reference, so zero-padded references such as
|
||||
/// `a` are found and each run of name bytes is read once, not once per `&`.
|
||||
fn entity_end(rest: &str) -> Option<usize> {
|
||||
let n = rest.bytes().skip(1).take_while(|b| b.is_ascii_alphanumeric() || *b == b'#').count();
|
||||
(rest.as_bytes().get(n + 1) == Some(&b';')).then_some(n + 1)
|
||||
}
|
||||
|
||||
fn unescape(t: &str) -> String {
|
||||
let mut out = String::with_capacity(t.len());
|
||||
let mut rest = t;
|
||||
while let Some(p) = rest.find('&') {
|
||||
out.push_str(&rest[..p]);
|
||||
rest = &rest[p..];
|
||||
let Some(end) = rest.find(';') else { break };
|
||||
// no `;` within a reference's reach: the `&` is kept and the search goes on after it
|
||||
let Some(end) = entity_end(rest) else {
|
||||
out.push('&');
|
||||
rest = &rest[1..];
|
||||
continue;
|
||||
};
|
||||
let ent = &rest[1..end];
|
||||
let c = match ent {
|
||||
"amp" => Some('&'),
|
||||
@@ -335,11 +348,13 @@ struct Acc {
|
||||
sums: BTreeMap<String, (f64, (f64, f64))>,
|
||||
curves: BTreeMap<String, Vec<(f64, f64)>>,
|
||||
unmapped: Vec<String>,
|
||||
/// What `unmapped` holds, for the look-up (a look names each unsupported setting once).
|
||||
seen: std::collections::HashSet<String>,
|
||||
}
|
||||
|
||||
impl Acc {
|
||||
fn skip(&mut self, what: String) {
|
||||
if !self.unmapped.contains(&what) {
|
||||
if self.seen.insert(what.clone()) {
|
||||
self.unmapped.push(what);
|
||||
}
|
||||
}
|
||||
@@ -455,7 +470,18 @@ pub fn read_lmp(path: &str, bytes: &[u8], group: Option<String>) -> Result<Impor
|
||||
/// Read a Luminar looks collection (`.mplumpack`, a zip): every look, grouped by the pack's
|
||||
/// `GroupName` (else the pack's file name).
|
||||
pub fn read_mplumpack(path: &str, bytes: &[u8]) -> Result<Vec<Imported>, String> {
|
||||
let entries = read_zip(bytes)?;
|
||||
let mut budget = MAX_ZIP_TOTAL;
|
||||
read_mplumpack_within(path, bytes, &mut budget)
|
||||
}
|
||||
|
||||
/// [`read_mplumpack`] unpacking at most `budget` bytes, which it spends: packs inside one zip
|
||||
/// share the zip's budget.
|
||||
pub(crate) fn read_mplumpack_within(path: &str, bytes: &[u8], budget: &mut usize) -> Result<Vec<Imported>, String> {
|
||||
// spent as each entry unpacks: a pack that fails part way still used its budget
|
||||
let mut spent = 0;
|
||||
let entries = read_zip_spending(bytes, MAX_ZIP_ENTRY, *budget, &mut spent);
|
||||
*budget = budget.saturating_sub(spent);
|
||||
let entries = entries?;
|
||||
let info = entries
|
||||
.iter()
|
||||
.find(|(n, _)| n.rsplit('/').next().is_some_and(|b| b.eq_ignore_ascii_case("PresetsInfo.plist")))
|
||||
@@ -486,6 +512,19 @@ mod tests {
|
||||
use crate::preset_import::read_presets;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn many_ampersands_before_one_semicolon_are_read_once() {
|
||||
let hostile = format!("{};&", "&".repeat(200_000));
|
||||
assert_eq!(unescape(&hostile), format!("{};&", "&".repeat(200_000)));
|
||||
assert_eq!(unescape("<a>  &unknownreference;"), "<a> \u{10FFFF} &unknownreference;");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_padded_numeric_references_decode() {
|
||||
assert_eq!(unescape("Canon A A"), "Canon A A");
|
||||
assert_eq!(unescape("&abc&#xyz;&;"), "&abc&#xyz;&;");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unescape_rejects_signed_numeric_references() {
|
||||
assert_eq!(unescape("&#+65;&#x+41;"), "&#+65;&#x+41;");
|
||||
|
||||
@@ -198,40 +198,62 @@ impl Session {
|
||||
/// Add imported presets. Identical presets (same name, group and settings) already present are
|
||||
/// skipped; id clashes get a fresh id. Returns the ids added.
|
||||
pub fn add_presets(&mut self, presets: Vec<Preset>) -> Vec<String> {
|
||||
self.add_presets_each(presets).into_iter().flatten().collect()
|
||||
}
|
||||
|
||||
/// [`Self::add_presets`] reporting per preset, in order: its id, or `None` when it was skipped
|
||||
/// as already present. One call indexes the library once, so an importer adds a whole file's
|
||||
/// presets through it rather than one at a time.
|
||||
pub fn add_presets_each(&mut self, presets: Vec<Preset>) -> Vec<Option<String>> {
|
||||
use std::collections::{HashMap, HashSet};
|
||||
// indexed: a collection of 10 000+ presets was quadratic (minutes) to import
|
||||
let mut ids: HashSet<String> = self.presets.iter().map(|q| q.id.clone()).collect();
|
||||
let mut by_name: HashMap<(String, String), Vec<usize>> = HashMap::new();
|
||||
for (i, q) in self.presets.iter().enumerate() {
|
||||
by_name.entry((q.name.clone(), q.group.clone())).or_default().push(i);
|
||||
}
|
||||
let mut added = Vec::new();
|
||||
// (name, group, settings): a preset is looked up rather than compared with every namesake.
|
||||
// Every number is keyed as an f64 with -0.0 folded into 0.0: `0` and `0.0` are unequal
|
||||
// `Value`s with equal hashes, so a crafted file of them would collide in one bucket.
|
||||
// shortcut: integers past 2^53 lose precision in the key, so two such presets are one
|
||||
let key_of = |p: &Preset| (p.name.clone(), p.group.clone(), canonical_numbers(&p.settings));
|
||||
let mut present: HashSet<(String, String, serde_json::Value)> = self.presets.iter().map(key_of).collect();
|
||||
// the next suffix to try per generated id, so a batch of clashes doesn't probe from 2 each time
|
||||
let mut next_suffix: HashMap<String, u32> = HashMap::new();
|
||||
let mut added = Vec::with_capacity(presets.len());
|
||||
for mut p in presets {
|
||||
let key = (p.name.clone(), p.group.clone());
|
||||
let same = by_name.get(&key).is_some_and(|ix| ix.iter().any(|&i| self.presets.get(i).is_some_and(|q| q.settings == p.settings)));
|
||||
if same {
|
||||
let key = key_of(&p);
|
||||
if present.contains(&key) {
|
||||
added.push(None);
|
||||
continue;
|
||||
}
|
||||
if p.id.trim().is_empty() || p.id.starts_with("lc.") || ids.contains(&p.id) {
|
||||
let base = format!("user.{}", slug(&format!("{}-{}", p.group, p.name)));
|
||||
let mut id = base.clone();
|
||||
let mut n = 2;
|
||||
let n = next_suffix.entry(base.clone()).or_insert(2);
|
||||
while ids.contains(&id) {
|
||||
id = format!("{base}-{n}");
|
||||
n += 1;
|
||||
*n += 1;
|
||||
}
|
||||
p.id = id;
|
||||
}
|
||||
p.builtin = false;
|
||||
ids.insert(p.id.clone());
|
||||
by_name.entry(key).or_default().push(self.presets.len());
|
||||
added.push(p.id.clone());
|
||||
present.insert(key);
|
||||
added.push(Some(p.id.clone()));
|
||||
self.presets.push(p);
|
||||
}
|
||||
added
|
||||
}
|
||||
}
|
||||
|
||||
/// `v` with every number replaced by its f64 (`-0.0` as `0.0`), so equal numbers hash and compare alike.
|
||||
fn canonical_numbers(v: &serde_json::Value) -> serde_json::Value {
|
||||
use serde_json::Value;
|
||||
match v {
|
||||
Value::Number(n) => n.as_f64().and_then(|f| serde_json::Number::from_f64(f + 0.0)).map_or_else(|| v.clone(), Value::Number),
|
||||
Value::Array(a) => Value::Array(a.iter().map(canonical_numbers).collect()),
|
||||
Value::Object(o) => Value::Object(o.iter().map(|(k, x)| (k.clone(), canonical_numbers(x))).collect()),
|
||||
_ => v.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn p(group: &str, id: &str, name: &str, settings: serde_json::Value) -> Preset {
|
||||
Preset { id: format!("lc.{id}"), name: name.into(), group: group.into(), settings, favorite: false, builtin: true }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
//! Reads of files named by a user, a sidecar or a library: the OPENED file must be a regular file within a byte
|
||||
//! cap, and no more than the cap is ever read. (A size taken from the pathname, then a second open and a read to EOF,
|
||||
//! lets a link, a FIFO, a device or a file that grows in between turn into an unbounded read.)
|
||||
|
||||
use std::io::{self, Read};
|
||||
use std::path::Path;
|
||||
|
||||
/// Most bytes of a photo, video or other media file read into memory.
|
||||
pub(crate) const MAX_MEDIA_BYTES: u64 = 8 << 30;
|
||||
|
||||
/// `O_NONBLOCK` of the platforms that have a FIFO to wait on (std has no name for it, and libc is a
|
||||
/// dev-dependency only); 0 elsewhere, where a FIFO open still waits for a writer.
|
||||
#[cfg(unix)]
|
||||
const O_NONBLOCK: i32 = if cfg!(all(
|
||||
any(target_os = "linux", target_os = "android"),
|
||||
not(any(
|
||||
target_arch = "mips",
|
||||
target_arch = "mips32r6",
|
||||
target_arch = "mips64",
|
||||
target_arch = "mips64r6",
|
||||
target_arch = "sparc",
|
||||
target_arch = "sparc64"
|
||||
))
|
||||
)) {
|
||||
0x800
|
||||
} else if cfg!(any(
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "freebsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "dragonfly"
|
||||
)) {
|
||||
0x4
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
/// Opens `path` for reading without waiting for a FIFO's writer (a FIFO is refused by [`read_open`], not waited on). A
|
||||
/// regular file is unaffected by the flag.
|
||||
pub(crate) fn open_for_read(path: impl AsRef<Path>) -> io::Result<std::fs::File> {
|
||||
let mut options = std::fs::OpenOptions::new();
|
||||
options.read(true);
|
||||
#[cfg(unix)]
|
||||
std::os::unix::fs::OpenOptionsExt::custom_flags(&mut options, O_NONBLOCK);
|
||||
options.open(path)
|
||||
}
|
||||
|
||||
/// The whole of the regular file at `path`, at most `max` bytes.
|
||||
pub(crate) fn read_regular(path: impl AsRef<Path>, max: u64) -> io::Result<Vec<u8>> {
|
||||
read_open(open_for_read(path)?, max)
|
||||
}
|
||||
|
||||
/// The whole of an already opened regular file, at most `max` bytes.
|
||||
pub(crate) fn read_open(file: std::fs::File, max: u64) -> io::Result<Vec<u8>> {
|
||||
let meta = file.metadata()?;
|
||||
if !meta.is_file() {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidInput, "not a regular file"));
|
||||
}
|
||||
if meta.len() > max {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, format!("{} bytes is more than the {max} allowed", meta.len())));
|
||||
}
|
||||
let mut bytes = Vec::with_capacity(usize::try_from(meta.len()).unwrap_or(0));
|
||||
file.take(max.saturating_add(1)).read_to_end(&mut bytes)?;
|
||||
if bytes.len() as u64 > max {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, format!("more than the {max} bytes allowed")));
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_regular_file_within_the_cap_is_read_and_others_are_refused() {
|
||||
let dir = std::env::temp_dir().join(format!("read-file-{}", std::process::id()));
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
let f = dir.join("a");
|
||||
std::fs::write(&f, b"abcd").unwrap();
|
||||
assert_eq!(read_regular(&f, 4).unwrap(), b"abcd");
|
||||
assert!(read_regular(&f, 3).is_err());
|
||||
assert!(read_regular(&dir, 100).is_err());
|
||||
#[cfg(unix)]
|
||||
{
|
||||
std::os::unix::fs::symlink("/dev/zero", dir.join("z")).unwrap();
|
||||
assert!(read_regular(dir.join("z"), 1 << 20).is_err());
|
||||
}
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
/// A FIFO with no writer is refused, not waited on forever in `open`.
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn a_fifo_without_a_writer_is_refused_without_blocking() {
|
||||
let dir = std::env::temp_dir().join(format!("read-file-fifo-{}", std::process::id()));
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
let fifo = dir.join("pipe");
|
||||
// (no unsafe code in this crate: the coreutils tool makes the FIFO)
|
||||
if !std::process::Command::new("mkfifo").arg(&fifo).status().is_ok_and(|s| s.success()) {
|
||||
eprintln!("skipped: no mkfifo");
|
||||
return;
|
||||
}
|
||||
let err = read_regular(&fifo, 1 << 20).unwrap_err();
|
||||
assert_eq!(err.kind(), io::ErrorKind::InvalidInput, "{err}");
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -622,3 +622,14 @@ fn photos_sharing_a_target_name_do_not_rescan_the_suffixes_already_claimed() {
|
||||
assert_eq!(names.len(), 300);
|
||||
assert!(checks.get() <= 3 * 300, "{} disk checks", checks.get());
|
||||
}
|
||||
|
||||
/// A token repeated many times with a long value is cut at the expansion cap, not multiplied into gigabytes.
|
||||
#[test]
|
||||
fn token_expansion_stops_at_the_output_cap() {
|
||||
let mut p = sample_photo();
|
||||
p.meta.title = "é".repeat(100_000);
|
||||
let out = expand_tokens(&"{title}".repeat(10_000), &p, 1, 1);
|
||||
assert!(out.len() <= tokens::MAX_EXPANDED_BYTES && out.len() > tokens::MAX_EXPANDED_BYTES - 2, "{}", out.len());
|
||||
p.meta.title = "Harbour".into();
|
||||
assert_eq!(expand_tokens("a-{title}-b", &p, 1, 1), "a-Harbour-b", "ordinary templates are unchanged");
|
||||
}
|
||||
@@ -205,6 +205,20 @@ pub fn expand(template: &str, p: &Photo, seq: usize) -> String {
|
||||
if ext.is_empty() { stem_out } else { format!("{stem_out}.{ext}") }
|
||||
}
|
||||
|
||||
/// Most bytes one expansion yields (far above any file name or overlay line). A token repeated many times
|
||||
/// with a long metadata value would otherwise multiply into gigabytes; the result is cut at this size.
|
||||
pub const MAX_EXPANDED_BYTES: usize = 1 << 20;
|
||||
|
||||
/// `s` appended to `out`, cut at a character boundary where `out` would pass [`MAX_EXPANDED_BYTES`].
|
||||
fn push_capped(out: &mut String, s: &str) {
|
||||
let room = MAX_EXPANDED_BYTES.saturating_sub(out.len());
|
||||
let mut n = s.len().min(room);
|
||||
while !s.is_char_boundary(n) {
|
||||
n -= 1;
|
||||
}
|
||||
out.push_str(&s[..n]);
|
||||
}
|
||||
|
||||
/// The template's tokens replaced for `p` (no extension added, nothing sanitized). A bare `{seq}`
|
||||
/// is zero-padded to `seq_width` digits.
|
||||
pub fn expand_tokens(template: &str, p: &Photo, seq: usize, seq_width: usize) -> String {
|
||||
@@ -225,9 +239,12 @@ pub fn expand_tokens_with(
|
||||
let mut out = String::new();
|
||||
let mut rest = template;
|
||||
while let Some(i) = rest.find('{') {
|
||||
out.push_str(&rest[..i]);
|
||||
if out.len() >= MAX_EXPANDED_BYTES {
|
||||
return out;
|
||||
}
|
||||
push_capped(&mut out, &rest[..i]);
|
||||
let Some(j) = rest[i..].find('}') else {
|
||||
out.push_str(&rest[i..]);
|
||||
push_capped(&mut out, &rest[i..]);
|
||||
rest = "";
|
||||
break;
|
||||
};
|
||||
@@ -263,9 +280,9 @@ pub fn expand_tokens_with(
|
||||
_ => format!("{{{tok}}}"),
|
||||
},
|
||||
};
|
||||
out.push_str(&v);
|
||||
push_capped(&mut out, &v);
|
||||
rest = &rest[i + j + 1..];
|
||||
}
|
||||
out.push_str(rest);
|
||||
push_capped(&mut out, rest);
|
||||
out
|
||||
}
|
||||
@@ -64,6 +64,9 @@ pub(crate) enum Tag {
|
||||
NewPrompt { photo: PhotoId, mask: Option<u32>, op: String, name: Option<String>, text: String },
|
||||
/// A zoomed-in pass for a component, computed from `shape` (without its detail).
|
||||
Detail { photo: PhotoId, mask: u32, comp: usize, shape: MaskShape },
|
||||
/// Find Subject for component `comp` of mask `mask` (an Object waiting for it): the people /
|
||||
/// animals found become a "person" Describe selection; none, the object at `point`.
|
||||
Subject { photo: PhotoId, mask: u32, comp: usize, point: Point },
|
||||
}
|
||||
|
||||
/// The model input: 8-bit RGB.
|
||||
@@ -498,6 +501,9 @@ impl Session {
|
||||
if !clicks.iter().any(|c| c.include) {
|
||||
return Err("click the object to include it first".into());
|
||||
}
|
||||
if clicks.len() > MAX_CLICKS {
|
||||
return Err(format!("an Object selection takes at most {MAX_CLICKS} clicks"));
|
||||
}
|
||||
#[cfg(feature = "sam")]
|
||||
{
|
||||
self.segment_wait(id, worker::Kind::Clicks(Self::model_clicks(clicks)), None)?.ok_or_else(|| "nothing was selected".to_string())
|
||||
@@ -570,6 +576,35 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
/// Find Subject for component `comp` of mask `mask` on photo `id` (an Object without clicks):
|
||||
/// the photo's main people / animals ([`worker::Kind::Subject`]), those the camera's `focus`
|
||||
/// falls on when the file records it; when the subject is neither, the object at `point`. In
|
||||
/// background mode [`Session::segment_poll`] applies the result; otherwise it is applied
|
||||
/// before this returns.
|
||||
pub(crate) fn segment_subject_later(&mut self, id: PhotoId, mask: u32, comp: usize, point: Point, focus: Option<Point>) -> Result<(), String> {
|
||||
self.segmenter.model_dir()?;
|
||||
#[cfg(feature = "sam")]
|
||||
{
|
||||
let kind = worker::Kind::Subject { focus: focus.map(|p| (p.x as f32, p.y as f32)) };
|
||||
let tag = Tag::Subject { photo: id, mask, comp, point };
|
||||
if self.segmenter.background {
|
||||
return self.segment_queue(id, kind, tag, None);
|
||||
}
|
||||
let seg = self.segment_wait(id, kind, None)?;
|
||||
let mut polled = Polled::default();
|
||||
self.apply_outcome(worker::Outcome { tag, result: Ok(seg), superseded: false, notice: None }, &mut polled);
|
||||
match polled.messages.into_iter().next() {
|
||||
Some(e) => Err(e),
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "sam"))]
|
||||
{
|
||||
let _ = (id, mask, comp, point, focus);
|
||||
Err("AI masks are not available in this build".into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Start a zoomed-in pass for component `comp` of mask `mask` on photo `id` (an Object or
|
||||
/// Describe selection): the photo around the selection is rendered at a higher resolution
|
||||
/// and segmented again. In background mode [`Session::segment_poll`] applies the result;
|
||||
@@ -706,6 +741,51 @@ impl Session {
|
||||
Err(e) => polled.messages.push(e.to_string()),
|
||||
}
|
||||
}
|
||||
Tag::Subject { photo, mask, comp, point } => {
|
||||
let found = match o.result {
|
||||
Ok(found) => found,
|
||||
Err(e) => return polled.messages.push(e),
|
||||
};
|
||||
let Some(d) = self.develop_of(photo) else { return };
|
||||
let mut d = (*d).clone();
|
||||
let Some(c) = d.masks.iter_mut().find(|m| m.id == mask).and_then(|m| m.components.get_mut(comp)) else { return };
|
||||
// still the Object waiting for it (not clicked or replaced meanwhile)
|
||||
let MaskShape::Object { hint, seg: None, edge, .. } = &c.shape else { return };
|
||||
if !hint.is_empty() {
|
||||
return;
|
||||
}
|
||||
let edge = *edge;
|
||||
let seg = match found {
|
||||
Some(seg) => seg,
|
||||
// neither a person nor an animal: the object at the point
|
||||
None if self.segmenter.background => {
|
||||
if let Err(e) = self.segment_clicks_later(photo, mask, comp, vec![point], Vec::new()) {
|
||||
polled.messages.push(e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
None => {
|
||||
match self.segment_clicks(photo, &clicks_of(&[point], &[])) {
|
||||
Ok(seg) => {
|
||||
c.shape = MaskShape::Object { hint: vec![point], exclude: Vec::new(), seg: Some(seg), detail: Vec::new(), edge }
|
||||
}
|
||||
Err(e) => return polled.messages.push(e),
|
||||
}
|
||||
if let Err(e) = self.set_develop(photo, d, "Find Subject") {
|
||||
polled.messages.push(e.to_string());
|
||||
} else {
|
||||
polled.changed = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
let text = lightcraft_segment::SUBJECT_CONCEPTS.join(", ");
|
||||
c.shape = MaskShape::Prompt { text, seg: Some(seg), detail: Vec::new(), edge };
|
||||
match self.set_develop(photo, d, "Find Subject") {
|
||||
Ok(()) => polled.changed = true,
|
||||
Err(e) => polled.messages.push(e.to_string()),
|
||||
}
|
||||
}
|
||||
Tag::Detail { photo, mask, comp, shape } => {
|
||||
let patch = match o.result {
|
||||
Ok(Some(p)) => p,
|
||||
@@ -747,6 +827,47 @@ mod tests {
|
||||
assert!(s.segment_poll().messages.is_empty(), "once");
|
||||
}
|
||||
|
||||
/// Find Subject's result: people / animals found become a Describe selection of the waiting
|
||||
/// Object (one undo step), not after it was clicked meanwhile; nothing found queues the object
|
||||
/// at the point (here: no model, so a message, and the mask still waits).
|
||||
#[cfg(feature = "sam")]
|
||||
#[test]
|
||||
fn find_subject_results_fill_the_waiting_mask() {
|
||||
use lightcraft_develop::{Mask, MaskComponent, MaskOp};
|
||||
let mut s = Session::with_demo();
|
||||
s.segmenter.dir = Some(std::env::temp_dir().join(format!("lc-no-sam3-subject-{}", std::process::id())));
|
||||
let id = s.active().unwrap();
|
||||
let waiting = || MaskShape::Object { hint: vec![], exclude: vec![], seg: None, detail: vec![], edge: 0.0 };
|
||||
let mut d = (*s.develop_of(id).unwrap()).clone();
|
||||
d.masks.push(Mask {
|
||||
id: 3,
|
||||
components: vec![MaskComponent { name: None, op: MaskOp::Add, invert: false, shape: waiting() }],
|
||||
..Default::default()
|
||||
});
|
||||
s.set_develop(id, d, "test").unwrap();
|
||||
let point = Point::new(0.4, 0.5);
|
||||
let send = |s: &mut Session, result| {
|
||||
let tag = Tag::Subject { photo: id, mask: 3, comp: 0, point };
|
||||
s.segmenter.results.0.send(worker::Outcome { tag, result, superseded: false, notice: None }).unwrap();
|
||||
s.segment_poll()
|
||||
};
|
||||
let shape = |s: &Session| s.develop_of(id).unwrap().masks[0].components[0].shape.clone();
|
||||
// nothing found, no model to click with: said so, and the mask still waits
|
||||
let polled = send(&mut s, Ok(None));
|
||||
assert!(!polled.messages.is_empty() && shape(&s) == waiting(), "{:?}", polled.messages);
|
||||
// people found: a Describe selection of them
|
||||
let polled = send(&mut s, Ok(Some(seg_box(20, 5, 5, 15, 15))));
|
||||
assert!(polled.changed && polled.messages.is_empty());
|
||||
let MaskShape::Prompt { text, seg: Some(_), .. } = shape(&s) else { panic!("{:?}", shape(&s)) };
|
||||
assert_eq!(text, "person, animal");
|
||||
// a result for a mask that changed meanwhile is dropped
|
||||
let before = shape(&s);
|
||||
assert!(!send(&mut s, Ok(Some(seg_box(20, 0, 0, 4, 4)))).changed);
|
||||
assert_eq!(shape(&s), before);
|
||||
// an error reaches the user
|
||||
assert_eq!(send(&mut s, Err("boom".into())).messages, vec!["boom".to_string()]);
|
||||
}
|
||||
|
||||
fn seg_box(side: usize, x0: usize, y0: usize, x1: usize, y1: usize) -> SegMask {
|
||||
let l: Vec<f32> =
|
||||
(0..side * side).map(|i| if (x0..x1).contains(&(i % side)) && (y0..y1).contains(&(i / side)) { 8.0 } else { -8.0 }).collect();
|
||||
|
||||
@@ -42,6 +42,12 @@ pub enum Kind {
|
||||
Prepare,
|
||||
Clicks(Vec<lightcraft_segment::Click>),
|
||||
Text(String),
|
||||
/// The photo's subject among its people and animals (`Sam3::segment_subject`); `focus`: where
|
||||
/// the camera focused (normalized), when the file records it. No result: the subject is
|
||||
/// something else.
|
||||
Subject {
|
||||
focus: Option<(f32, f32)>,
|
||||
},
|
||||
/// A zoomed-in pass over `region` (normalized) of the photo, rendered by the job's `render`.
|
||||
Detail {
|
||||
prompt: Prompt,
|
||||
@@ -420,6 +426,7 @@ fn remote_job(state: &mut State, shared: &Shared, job: &mut Job, endpoint: &str)
|
||||
let operation = match std::mem::replace(&mut job.kind, Kind::Prepare) {
|
||||
Kind::Prepare => Operation::Prepare,
|
||||
Kind::Text(text) => Operation::Text(text),
|
||||
Kind::Subject { focus } => Operation::Subject(focus.map(|(x, y)| [x, y])),
|
||||
Kind::Clicks(clicks) => Operation::Clicks(clicks.iter().map(|c| [c.x, c.y, if c.positive { 1.0 } else { 0.0 }]).collect()),
|
||||
Kind::Detail { prompt, region: r } => {
|
||||
region = Some(r);
|
||||
@@ -480,6 +487,11 @@ fn run_job(state: &mut State, shared: &Shared, job: &mut Job, placement: Placeme
|
||||
let (m, enc) = encoded(state, shared, job, placement)?;
|
||||
Ok(text_logits(m, enc, &text)?.map(|l| SegMask::from_logits(side, &l)))
|
||||
}
|
||||
Kind::Subject { focus } => {
|
||||
let (m, enc) = encoded(state, shared, job, placement)?;
|
||||
// (the reference score threshold, as Describe uses)
|
||||
Ok(m.segment_subject(enc, focus, 0.5)?.map(|p| SegMask::from_logits(side, &p.logits)))
|
||||
}
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::panic)]
|
||||
Kind::Panic => panic!("a test panic"),
|
||||
|
||||
@@ -743,7 +743,7 @@ fn backup(path: &Path, stamp: &str) -> std::io::Result<PathBuf> {
|
||||
if !std::fs::symlink_metadata(path)?.is_file() {
|
||||
return Err(std::io::Error::other(format!("{} is not a regular file", path.display())));
|
||||
}
|
||||
let bytes = std::fs::read(path)?;
|
||||
let bytes = crate::read_file::read_regular(path, SIDECAR_READ_MAX)?;
|
||||
let stamp: String = stamp.chars().filter(char::is_ascii_alphanumeric).collect();
|
||||
let name = path.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
|
||||
for k in 0..1000 {
|
||||
@@ -774,7 +774,7 @@ pub struct SidecarSaved {
|
||||
/// The new contents for the sidecar at `path`: `fresh` merged into the existing file, if any.
|
||||
/// Returns (contents, merged, backup).
|
||||
fn sidecar_contents(path: &Path, fresh: String, stamp: &str) -> std::io::Result<(String, bool, Option<PathBuf>)> {
|
||||
let bytes = match std::fs::read(path) {
|
||||
let bytes = match crate::read_file::read_regular(path, SIDECAR_READ_MAX) {
|
||||
Ok(b) => b,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok((fresh, false, None)),
|
||||
// can't see what is there: never overwrite it blindly
|
||||
@@ -784,17 +784,28 @@ fn sidecar_contents(path: &Path, fresh: String, stamp: &str) -> std::io::Result<
|
||||
return Ok((fresh, false, None));
|
||||
}
|
||||
let rules = lightcraft_meta::MergeRules { owned: OWNED, owned_if_present: OWNED_IF_STATED };
|
||||
match std::str::from_utf8(&bytes)
|
||||
.map_err(|e| e.to_string())
|
||||
.and_then(|old| lightcraft_meta::merge_xmp(old, &fresh, rules).map_err(|e| e.to_string()))
|
||||
{
|
||||
Ok(merged) => Ok((merged, true, None)),
|
||||
Err(why) => {
|
||||
let b = backup(path, stamp)?;
|
||||
log::warn!("{}: not readable as XMP ({why}); kept a copy as {} before replacing it", path.display(), b.display());
|
||||
Ok((fresh, false, Some(b)))
|
||||
}
|
||||
}
|
||||
let why = match std::str::from_utf8(&bytes) {
|
||||
Ok(old) => match lightcraft_meta::merge_xmp(old, &fresh, rules) {
|
||||
Ok(merged) => return Ok((merged, true, None)),
|
||||
// a readable sidecar whose merge fails (e.g. the merged packet exceeds the XMP limits), or one
|
||||
// that is valid but over the parser's complexity limits: fail the save and keep the file
|
||||
// as it is, rather than replace metadata that could be read
|
||||
// (text with no `rdf:RDF` element parses as empty metadata but holds nothing to keep: it is replaced
|
||||
// after a backup, as any sidecar that is not XMP)
|
||||
Err(why)
|
||||
if why == lightcraft_meta::XmpError::TooComplex
|
||||
|| (lightcraft_meta::parse_xmp(old.strip_prefix('\u{feff}').unwrap_or(old)).is_ok()
|
||||
&& !matches!(&why, lightcraft_meta::XmpError::Xml(m) if m.contains("no rdf:RDF element"))) =>
|
||||
{
|
||||
return Err(std::io::Error::other(format!("the sidecar is readable but could not be merged ({why}); it was left unchanged")));
|
||||
}
|
||||
Err(why) => why.to_string(),
|
||||
},
|
||||
Err(why) => why.to_string(),
|
||||
};
|
||||
let b = backup(path, stamp)?;
|
||||
log::warn!("{}: not readable as XMP ({why}); kept a copy as {} before replacing it", path.display(), b.display());
|
||||
Ok((fresh, false, Some(b)))
|
||||
}
|
||||
|
||||
/// Key of the stem sidecar a file maps to (case-insensitive).
|
||||
@@ -840,10 +851,10 @@ fn file_path(p: &Photo) -> Option<&str> {
|
||||
pub fn read_packet(original: &str, kind: MediaKind, lookup: SidecarLookup) -> Option<(String, PathBuf)> {
|
||||
if let Some(sc) = lookup.find(original) {
|
||||
// A discovered sidecar keeps precedence even when reading it fails.
|
||||
return std::fs::read_to_string(&sc).ok().map(|s| (s, sc));
|
||||
return crate::read_file::read_regular(&sc, SIDECAR_READ_MAX).ok().and_then(|b| String::from_utf8(b).ok()).map(|s| (s, sc));
|
||||
}
|
||||
if kind == MediaKind::Raw {
|
||||
let bytes = std::fs::read(original).ok()?;
|
||||
let bytes = crate::read_file::read_regular(original, crate::read_file::MAX_MEDIA_BYTES).ok()?;
|
||||
let x = lightcraft_meta::embedded(&bytes).xmp?;
|
||||
return Some((x, PathBuf::from(original)));
|
||||
}
|
||||
@@ -855,6 +866,9 @@ pub fn read_packet(original: &str, kind: MediaKind, lookup: SidecarLookup) -> Op
|
||||
None
|
||||
}
|
||||
|
||||
/// Most bytes of a sidecar read whole (the opened file must be a regular file).
|
||||
const SIDECAR_READ_MAX: u64 = 256 << 20;
|
||||
|
||||
const JPEG_XMP_HEADER_SPAN: u64 = 16 * 1024 * 1024;
|
||||
const JPEG_XMP_HEADER_STEPS: usize = 4096;
|
||||
|
||||
@@ -1592,6 +1606,62 @@ mod tests {
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// A readable sidecar whose merged packet fails read-back (too many namespace declarations in scope) is kept: the
|
||||
/// save fails instead of moving it to a backup and writing fresh content only.
|
||||
#[test]
|
||||
fn a_readable_sidecar_that_cannot_be_merged_is_kept() {
|
||||
let dir = std::env::temp_dir().join(format!("lightcraft-sidecar-keep-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let decls: String = (0..500).map(|i| format!(" xmlns:n{i}=\"urn:n:{i}\"")).collect();
|
||||
let old = format!(
|
||||
"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"{decls}><rdf:Description rdf:about=\"\"/></rdf:RDF></x:xmpmeta>"
|
||||
);
|
||||
assert!(lightcraft_meta::parse_xmp(&old).is_ok(), "the existing sidecar is readable");
|
||||
let path = dir.join("a.xmp");
|
||||
std::fs::write(&path, &old).unwrap();
|
||||
let fresh = lightcraft_meta::write_xmp_lc(&lightcraft_meta::Metadata { rating: Some(3), ..Default::default() }, &[("settings", "{}")]);
|
||||
assert!(super::sidecar_contents(&path, fresh, "t").is_err());
|
||||
assert_eq!(std::fs::read_to_string(&path).unwrap(), old);
|
||||
assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 1, "no backup was made");
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// Text that is not XMP at all (it parses as empty metadata, but has no `rdf:RDF` element) is backed up and replaced.
|
||||
#[test]
|
||||
fn a_sidecar_with_no_rdf_element_is_backed_up_and_replaced() {
|
||||
let dir = std::env::temp_dir().join(format!("lightcraft-sidecar-plain-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("a.xmp");
|
||||
std::fs::write(&path, "not xml at all").unwrap();
|
||||
let fresh = lightcraft_meta::write_xmp_lc(&lightcraft_meta::Metadata { rating: Some(3), ..Default::default() }, &[("settings", "{}")]);
|
||||
let (data, merged, backup) = super::sidecar_contents(&path, fresh.clone(), "t").unwrap();
|
||||
assert!(!merged && data == fresh);
|
||||
assert!(backup.is_some_and(|b| std::fs::read_to_string(b).unwrap() == "not xml at all"));
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// A sidecar over the parser's namespace-declaration limit cannot be read or merged: the save fails and the file stays.
|
||||
#[test]
|
||||
fn a_sidecar_over_the_complexity_limit_is_kept() {
|
||||
let dir = std::env::temp_dir().join(format!("lightcraft-sidecar-complex-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let decls: String = (0..600).map(|i| format!(" xmlns:n{i}=\"urn:n:{i}\"")).collect();
|
||||
let old = format!(
|
||||
"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"{decls}><rdf:Description rdf:about=\"\"/></rdf:RDF></x:xmpmeta>"
|
||||
);
|
||||
assert_eq!(lightcraft_meta::parse_xmp(&old).unwrap_err(), lightcraft_meta::XmpError::TooComplex);
|
||||
let path = dir.join("a.xmp");
|
||||
std::fs::write(&path, &old).unwrap();
|
||||
let fresh = lightcraft_meta::write_xmp_lc(&lightcraft_meta::Metadata { rating: Some(3), ..Default::default() }, &[("settings", "{}")]);
|
||||
assert!(super::sidecar_contents(&path, fresh, "t").is_err());
|
||||
assert_eq!(std::fs::read_to_string(&path).unwrap(), old);
|
||||
assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 1, "no backup was made");
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// A sidecar written by Lightroom Classic (names flat, paths in `lr:hierarchicalSubject`)
|
||||
/// gives the photo its keywords as paths.
|
||||
#[test]
|
||||
|
||||
@@ -389,7 +389,7 @@ fn read_user(file: &str, path: &Path) -> Result<Template, String> {
|
||||
if meta.len() > MAX_FILE {
|
||||
return Err(format!("{} bytes is too large for a slideshow template (at most {} KB)", meta.len(), MAX_FILE / 1024));
|
||||
}
|
||||
let bytes = std::fs::read(path).map_err(|e| e.to_string())?;
|
||||
let bytes = crate::read_file::read_regular(path, MAX_FILE).map_err(|e| e.to_string())?;
|
||||
let p = parse(&bytes)?;
|
||||
Ok(Template {
|
||||
id: format!("{USER}{stem}"),
|
||||
@@ -411,7 +411,7 @@ pub fn read_file(path: &Path) -> Result<Parsed, String> {
|
||||
if meta.len() > MAX_FILE {
|
||||
return Err(format!("{}: {} bytes is too large for a slideshow template (at most {} KB)", path.display(), meta.len(), MAX_FILE / 1024));
|
||||
}
|
||||
let bytes = std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))?;
|
||||
let bytes = crate::read_file::read_regular(path, MAX_FILE).map_err(|e| format!("{}: {e}", path.display()))?;
|
||||
let mut p = parse(&bytes).map_err(|e| format!("{}: {e}", path.display()))?;
|
||||
if p.name.is_none() {
|
||||
let stem = path.file_stem().map(|s| s.to_string_lossy().to_string()).unwrap_or_default();
|
||||
|
||||
@@ -387,6 +387,84 @@ fn permanently_deleting_explicit_targets_keeps_the_surviving_selection() {
|
||||
assert_eq!(s.catalog.to_snapshot(), snapshot);
|
||||
}
|
||||
|
||||
/// `lensBlur.brushStroke` adds a stroke to the Focus or the Blur brush (clamped, undoable); hostile
|
||||
/// input is an error; `lensBlur.brushReset` clears one brush or both.
|
||||
#[test]
|
||||
fn lens_blur_brush_strokes_and_reset() {
|
||||
let mut s = demo();
|
||||
let id = s.active().unwrap();
|
||||
s.execute(
|
||||
"lensBlur.brushStroke",
|
||||
&json!({"mode": "focus", "points": [[0.2, 0.3], [0.25, 0.3]], "size": 9.0, "amount": 40, "feather": f64::NAN, "autoMask": true}),
|
||||
)
|
||||
.unwrap();
|
||||
s.execute("lensBlur.brushStroke", &json!({"mode": "blur", "points": [[0.7, 0.6]]})).unwrap();
|
||||
let d = s.develop_of(id).unwrap();
|
||||
let (f, b) = (&d.lens_blur.focus_strokes, &d.lens_blur.blur_strokes);
|
||||
assert_eq!((f.len(), b.len()), (1, 1));
|
||||
assert_eq!(f[0].points.len(), 2);
|
||||
assert!(f[0].size == 0.5 && f[0].density == 40.0 && f[0].feather == lightcraft_develop::BrushStroke::default().feather && f[0].auto_mask);
|
||||
for bad in [
|
||||
json!({"mode": "sideways", "points": [[0.5, 0.5]]}),
|
||||
json!({"mode": "focus", "points": []}),
|
||||
json!({"mode": "focus"}),
|
||||
json!({"mode": "blur", "points": [[0.5, 0.5]], "pressure": [0.5, 0.5]}),
|
||||
] {
|
||||
assert!(s.execute("lensBlur.brushStroke", &bad).is_err(), "{bad}");
|
||||
}
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(s.develop_of(id).unwrap().lens_blur.blur_strokes.is_empty(), "a stroke is one undo step");
|
||||
s.execute("lensBlur.brushStroke", &json!({"mode": "blur", "points": [[0.7, 0.6]]})).unwrap();
|
||||
s.execute("lensBlur.brushReset", &json!({"mode": "blur"})).unwrap();
|
||||
let d = s.develop_of(id).unwrap();
|
||||
assert!(d.lens_blur.blur_strokes.is_empty() && d.lens_blur.focus_strokes.len() == 1);
|
||||
assert!(s.execute("lensBlur.brushReset", &json!({"mode": "nope"})).is_err());
|
||||
s.execute("lensBlur.brushReset", &json!({})).unwrap();
|
||||
assert!(s.develop_of(id).unwrap().lens_blur.focus_strokes.is_empty());
|
||||
}
|
||||
|
||||
/// `lensBlur.findSubject` without the SAM 3 model (or in a build without AI masks) is an error that
|
||||
/// leaves the photo as it was: no empty mask, the blur not switched on.
|
||||
#[test]
|
||||
fn find_subject_without_the_model_changes_nothing() {
|
||||
let mut s = demo();
|
||||
let id = s.active().unwrap();
|
||||
// (never the machine's own model: a folder that has none)
|
||||
s.segmenter.dir = Some(std::env::temp_dir().join(format!("lc-no-sam3-{}", std::process::id())));
|
||||
let before = s.develop_of(id).unwrap();
|
||||
let e = s.execute("lensBlur.findSubject", &json!({})).unwrap_err();
|
||||
assert!(e.to_string().contains("SAM 3") || e.to_string().contains("AI masks"), "{e}");
|
||||
let after = s.develop_of(id).unwrap();
|
||||
assert!(after.masks.is_empty() && !after.lens_blur.enabled);
|
||||
assert_eq!(*before, *after);
|
||||
}
|
||||
|
||||
/// Deleting the mask that is the Lens Blur focus clears the focus, so a new mask that gets the same
|
||||
/// id doesn't become the focus; undo brings both back.
|
||||
#[test]
|
||||
fn deleting_the_focus_mask_clears_the_lens_blur_focus() {
|
||||
let mut s = demo();
|
||||
let id = s.active().unwrap();
|
||||
let radial = json!({"kind": "radial", "center": [0.5, 0.5], "rx": 0.2, "ry": 0.2});
|
||||
s.execute("mask.add", &radial).unwrap();
|
||||
let mid = s.develop_of(id).unwrap().masks[0].id;
|
||||
s.execute("develop.merge", &json!({"settings": {"lens_blur": {"enabled": true, "amount": 50, "focus_mask": mid}}})).unwrap();
|
||||
s.execute("mask.delete", &json!({"id": mid})).unwrap();
|
||||
assert_eq!(s.develop_of(id).unwrap().lens_blur.focus_mask, None);
|
||||
// a new mask takes the freed id, and stays an ordinary mask
|
||||
s.execute("mask.add", &radial).unwrap();
|
||||
let d = s.develop_of(id).unwrap();
|
||||
assert_eq!(d.masks[0].id, mid);
|
||||
assert_eq!(d.lens_blur.focus_mask, None);
|
||||
// undoing the add and the delete brings the focus back with its mask
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.develop_of(id).unwrap().lens_blur.focus_mask, Some(mid));
|
||||
// other mask edits keep it
|
||||
s.execute("mask.adjust", &json!({"values": {"exposure": 0.5}})).unwrap();
|
||||
assert_eq!(s.develop_of(id).unwrap().lens_blur.focus_mask, Some(mid));
|
||||
}
|
||||
|
||||
/// `mask.brushStroke {connect: true}` (a ⇧-click) starts from the last point of the mask's last stroke: one straight
|
||||
/// stroke when the brush is unchanged, pieces whose size, feather and flow step from the last stroke's to the new
|
||||
/// values when it changed, and a plain dab when there is nothing to connect to.
|
||||
|
||||
@@ -374,6 +374,8 @@ fn library_with_jpeg(tag: &str) -> (Session, lightcraft_catalog::PhotoId, std::p
|
||||
|
||||
fn preset_export_preserves_originals(command: &str, selection: serde_json::Value, tag: &str) {
|
||||
let (mut s, _, dir, src, original) = library_with_jpeg(tag);
|
||||
// something for mask.exportEffects to write (the other commands ignore it)
|
||||
s.mask_effects.push(crate::cmd::mask_effects::MaskEffect { name: "Glow".into(), ..Default::default() });
|
||||
let sidecar = src.with_extension("xmp");
|
||||
let appended_sidecar = dir.join("IMG_1.jpg.xmp");
|
||||
std::fs::write(&sidecar, b"existing sidecar").unwrap();
|
||||
@@ -399,6 +401,14 @@ fn preset_export_preserves_originals(command: &str, selection: serde_json::Value
|
||||
assert_eq!(std::fs::read(&sidecar).unwrap(), b"existing sidecar");
|
||||
assert_eq!(std::fs::read(&appended_sidecar).unwrap(), b"appended sidecar");
|
||||
}
|
||||
// `..` through a folder that does not exist yet could reach the original once it is created
|
||||
let mut params = selection.clone();
|
||||
params["path"] = json!(dir.join("missing/../IMG_1.jpg").to_string_lossy());
|
||||
let error = s.execute(command, ¶ms).unwrap_err().to_string();
|
||||
// Windows resolves `..` lexically and finds the original; Unix cannot resolve it and refuses
|
||||
assert!(error.contains("cannot be resolved") || error.contains("never writes over an original"), "{error}");
|
||||
assert!(!dir.join("missing").exists());
|
||||
assert_eq!(std::fs::read(&src).unwrap(), original);
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
@@ -460,9 +470,12 @@ fn keyword_export_preserves_the_previous_file_on_write_failure() {
|
||||
fn preset_exports_preserve_previous_files_on_write_failure() {
|
||||
let dir = temp_dir("preset-export-failure");
|
||||
let mut s = Session::new();
|
||||
for (command, selection, file) in
|
||||
[("preset.export", json!({"ids": ["lc.warm-glow"]}), "look.lcpreset"), ("curve.exportPresets", json!({"names": ["Linear"]}), "curve.lccurve")]
|
||||
{
|
||||
s.mask_effects.push(crate::cmd::mask_effects::MaskEffect { name: "Glow".into(), ..Default::default() });
|
||||
for (command, selection, file) in [
|
||||
("preset.export", json!({"ids": ["lc.warm-glow"]}), "look.lcpreset"),
|
||||
("curve.exportPresets", json!({"names": ["Linear"]}), "curve.lccurve"),
|
||||
("mask.exportEffects", json!({"names": ["Glow"]}), "effects.lceffect"),
|
||||
] {
|
||||
let path = dir.join(file);
|
||||
let mut params = selection;
|
||||
params["path"] = json!(path.to_string_lossy());
|
||||
@@ -475,10 +488,15 @@ fn preset_exports_preserve_previous_files_on_write_failure() {
|
||||
assert_eq!(std::fs::read(&path).unwrap(), previous, "{command} preserves the prior export");
|
||||
assert_eq!(s.execute(command, ¶ms).unwrap()["count"], 1, "replacement still succeeds");
|
||||
}
|
||||
assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 2, "no temporary files remain");
|
||||
assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 3, "no temporary files remain");
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mask_effect_export_never_overwrites_originals_or_sidecars() {
|
||||
preset_export_preserves_originals("mask.exportEffects", json!({"names": ["Glow"]}), "effect-export-guard");
|
||||
}
|
||||
|
||||
fn disk_batch(
|
||||
s: &mut Session,
|
||||
id: lightcraft_catalog::PhotoId,
|
||||
|
||||
@@ -46,7 +46,7 @@ fn jpeg(w: u32, h: u32, px: impl Fn(u32, u32) -> [u8; 3]) -> Vec<u8> {
|
||||
/// photo by its largest preview (600 × 400) and the develop path renders from it, after reading
|
||||
/// the whole file.
|
||||
fn raw_with_two_previews(tail: usize) -> Vec<u8> {
|
||||
use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
let preview = |w: u32, h: u32, bytes: Vec<u8>, first: bool| {
|
||||
let mut ifd = IfdBuilder::new();
|
||||
if first {
|
||||
|
||||
@@ -34,7 +34,7 @@ fn write_png(path: &Path, seed: u8) {
|
||||
}
|
||||
|
||||
fn exif_clock_block(original: Option<&str>, digitized: Option<&str>) -> Vec<u8> {
|
||||
use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value};
|
||||
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value};
|
||||
|
||||
let mut exif = IfdBuilder::new();
|
||||
if let Some(value) = original {
|
||||
@@ -2820,7 +2820,7 @@ fn a_file_gone_since_the_import_review_is_not_imported() {
|
||||
/// a JPEG. In a build without the codecs' `heif` feature it is reported as failed, saying why.
|
||||
#[test]
|
||||
fn heic_imports_as_a_normal_photo_or_says_why_not() {
|
||||
use lightcraft_heif::testdata::{Prop, Spec, build};
|
||||
use craft_heif::testdata::{Prop, Spec, build};
|
||||
let src = temp_dir("heic");
|
||||
let photo = |x: u32, y: u32| [((x * 7 + y * 13) % 200 + 30) as u16, 110, 150];
|
||||
let p3 = lightcraft_codecs::icc::write_named(lightcraft_codecs::NamedSpace::DisplayP3);
|
||||
@@ -2857,8 +2857,8 @@ fn heic_imports_as_a_normal_photo_or_says_why_not() {
|
||||
/// writes as UTC: an iPhone photo landed five hours late and after 19:00 on the wrong day.
|
||||
#[test]
|
||||
fn a_heic_is_dated_by_its_exif_and_not_by_the_file() {
|
||||
use lightcraft_heif::testdata::{Spec, build};
|
||||
use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t};
|
||||
use craft_heif::testdata::{Spec, build};
|
||||
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t};
|
||||
let src = temp_dir("heic-capture-time");
|
||||
let photo = |x: u32, y: u32| [((x * 7 + y * 13) % 200 + 30) as u16, 110, 150];
|
||||
let mut exif = IfdBuilder::new();
|
||||
|
||||
@@ -98,6 +98,37 @@ fn an_lrdata_folder_whose_catalog_was_renamed_finds_the_one_next_to_it() {
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
/// Issue #2592: someone who deleted Lightroom and its catalog keeps only `Backups/`; that folder and
|
||||
/// each dated folder in it are refused. A dated folder names its zip; `Backups/` names one only when
|
||||
/// it is the only backup (with several, the import asks which).
|
||||
#[test]
|
||||
fn lightroom_backups_without_their_catalog_are_refused_and_name_the_zip() {
|
||||
let root = temp_dir("backups");
|
||||
let backups = root.join("Lightroom").join("Backups");
|
||||
let backup = crate::lightroom_backup::tests::zip(&[("Cat.lrcat", b"SQLite format 3\0")], true, false);
|
||||
for (dated, zip) in [("2019-03-26 0751", "Cat-2.lrcat.zip"), ("2023-11-07 0900", "Cat-v13.zip")] {
|
||||
std::fs::create_dir_all(backups.join(dated)).unwrap();
|
||||
std::fs::write(backups.join(dated).join(zip), &backup).unwrap();
|
||||
}
|
||||
// a zip without a catalog in a dated folder is not a backup
|
||||
std::fs::create_dir_all(backups.join("2024-01-01 1200")).unwrap();
|
||||
std::fs::write(backups.join("2024-01-01 1200").join("photos.zip"), crate::lightroom_backup::tests::zip(&[("a.jpg", b"x")], false, false))
|
||||
.unwrap();
|
||||
assert_eq!(lightroom_catalog_at(&backups.join("2024-01-01 1200")), None, "no catalog in the zip");
|
||||
let newest = backups.join("2023-11-07 0900").join("Cat-v13.zip");
|
||||
assert_eq!(lightroom_catalog_at(&backups), Some(LightroomPlace { catalog: None }), "two backups: no arbitrary pick");
|
||||
refused(&root, &backups.join("2023-11-07 0900"), &newest);
|
||||
refused(&root, &backups.join("2019-03-26 0751"), &backups.join("2019-03-26 0751").join("Cat-2.lrcat.zip"));
|
||||
std::fs::remove_dir_all(backups.join("2019-03-26 0751")).unwrap();
|
||||
refused(&root, &backups, &newest);
|
||||
// a folder of zips that isn't in a `Backups/` folder is not Lightroom's
|
||||
let other = root.join("Downloads");
|
||||
std::fs::create_dir_all(&other).unwrap();
|
||||
std::fs::write(other.join("photos.zip"), &backup).unwrap();
|
||||
assert_eq!(lightroom_catalog_at(&other), None);
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_folder_still_becomes_a_library() {
|
||||
let root = temp_dir("empty");
|
||||
|
||||
@@ -94,6 +94,50 @@ fn importing_thousands_of_presets_is_not_quadratic() {
|
||||
assert!(t.elapsed().as_secs() < 20, "took {:?}", t.elapsed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_preset_whose_settings_differ_only_in_the_sign_of_zero_is_a_repeat() {
|
||||
let mut s = Session::new();
|
||||
let preset = |x: f64| Preset {
|
||||
id: "user.z".into(),
|
||||
name: "Zero".into(),
|
||||
group: "Coll".into(),
|
||||
settings: json!({"light": {"exposure": x}}),
|
||||
favorite: false,
|
||||
builtin: false,
|
||||
};
|
||||
assert!(s.add_presets_each(vec![preset(0.0)])[0].is_some());
|
||||
assert_eq!(s.add_presets_each(vec![preset(-0.0)]), vec![None]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thousands_of_presets_sharing_a_name_and_id_get_unique_ids_in_one_pass() {
|
||||
let mut s = Session::new();
|
||||
let t = Instant::now();
|
||||
// the same name, group and id throughout, each with its own settings: every one clashes
|
||||
let same: Vec<Preset> = (0..20_000)
|
||||
.map(|i| Preset {
|
||||
id: "user.x".into(),
|
||||
name: "Look".into(),
|
||||
group: "Coll".into(),
|
||||
settings: json!({"light": {"exposure": i}}),
|
||||
favorite: false,
|
||||
builtin: false,
|
||||
})
|
||||
.collect();
|
||||
let each = s.add_presets_each(same);
|
||||
assert_eq!(each.len(), 20_000);
|
||||
assert!(each.iter().all(Option::is_some));
|
||||
// (the session may hold built-in presets too: only the added ones are counted)
|
||||
let ids: std::collections::HashSet<_> = s.presets.iter().filter(|p| p.group == "Coll").map(|p| p.id.as_str()).collect();
|
||||
assert_eq!(ids.len(), 20_000);
|
||||
// a repeat is skipped, per preset
|
||||
let last = s.presets.last().cloned().unwrap();
|
||||
let again = s.add_presets_each(vec![last.clone(), Preset { settings: json!({"light": {"exposure": -1}}), ..last }]);
|
||||
assert_eq!(again[0], None);
|
||||
assert!(again[1].is_some());
|
||||
assert!(t.elapsed().as_secs() < 20, "took {:?}", t.elapsed());
|
||||
}
|
||||
|
||||
/// Timing with a real preset collection: `LIGHTCRAFT_BENCH_PRESETS=path/to/presets.json cargo test
|
||||
/// -p lightcraft-engine --release presets_scale_bench -- --ignored --nocapture`. The file is copied
|
||||
/// into a temporary library; the original is only read.
|
||||
@@ -124,3 +168,22 @@ fn presets_scale_bench() {
|
||||
eprintln!("preset.favorite (saves presets.json): {:?}", t.elapsed());
|
||||
let _ = std::fs::remove_dir_all(&lib);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integer_and_float_spellings_of_one_number_are_one_preset() {
|
||||
// 15 slots of `0` / `0.0` / `-0.0`: all spellings are the same preset, so none collide in the index
|
||||
let mut s = Session::new();
|
||||
let preset = |i: usize| {
|
||||
let spell = |bit: usize| match (i >> bit) % 3 {
|
||||
0 => json!(0),
|
||||
1 => json!(0.0),
|
||||
_ => json!(-0.0),
|
||||
};
|
||||
let slots: Vec<_> = (0..15).map(spell).collect();
|
||||
Preset { id: "user.n".into(), name: "N".into(), group: "G".into(), settings: json!({"v": slots}), favorite: false, builtin: false }
|
||||
};
|
||||
let t = Instant::now();
|
||||
let added = s.add_presets_each((0..4000).map(preset).collect());
|
||||
assert_eq!(added.iter().filter(|a| a.is_some()).count(), 1);
|
||||
assert!(t.elapsed().as_secs() < 20);
|
||||
}
|
||||
@@ -240,6 +240,25 @@ fn object_clicks_are_capped() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// In background mode a new Object mask with too many clicks is refused up front, not queued.
|
||||
#[test]
|
||||
fn background_object_clicks_over_the_cap_are_refused_before_queueing() {
|
||||
if !Segmenter::AVAILABLE {
|
||||
return;
|
||||
}
|
||||
let mut s = Session::with_demo();
|
||||
s.segmenter.background = true;
|
||||
let dir = damaged_model("later-clicks");
|
||||
s.segmenter.dir = Some(dir.clone());
|
||||
let id = s.active().unwrap();
|
||||
let points: Vec<_> = (0..=crate::segment::MAX_CLICKS).map(|_| json!([0.5, 0.5])).collect();
|
||||
let e = s.execute("mask.add", &json!({"kind": "object", "points": points})).unwrap_err().to_string();
|
||||
assert!(e.contains("at most"), "{e}");
|
||||
assert!(s.develop_of(id).unwrap().masks.is_empty());
|
||||
assert!(!s.segmenter.busy());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[cfg(feature = "sam")]
|
||||
#[test]
|
||||
fn a_running_model_download_shows_in_activity_and_cancels() {
|
||||
|
||||
@@ -638,7 +638,7 @@ fn an_orientation_only_the_xmp_states_leaves_stored_frame_regions_as_written() {
|
||||
let img = lightcraft_raster::Rgba8::from_fn(32, 24, |x, y| [(x * 7) as u8, (y * 9) as u8, 90, 255]);
|
||||
// an Exif that states the size as stored (32 × 24, as cameras do) and no orientation
|
||||
let exif = {
|
||||
use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t};
|
||||
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t};
|
||||
let mut sub = IfdBuilder::new();
|
||||
sub.set(t::PIXEL_X_DIMENSION, Value::Long(vec![32]));
|
||||
sub.set(t::PIXEL_Y_DIMENSION, Value::Long(vec![24]));
|
||||
@@ -681,7 +681,7 @@ fn a_raws_decoder_orientation_turns_stored_frame_regions_on_import_and_reread()
|
||||
let px: Vec<u16> = (0..16 * 6).map(|i| (i * 41 % 4096) as u16).collect();
|
||||
let strip: Vec<u8> = px.chunks(2).flat_map(|p| [(p[0] >> 4) as u8, ((p[0] & 15) << 4) as u8 | (p[1] >> 8) as u8, p[1] as u8]).collect();
|
||||
{
|
||||
use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value, tags as t};
|
||||
let mut raw = IfdBuilder::new();
|
||||
raw.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0]));
|
||||
raw.set(t::IMAGE_WIDTH, Value::Long(vec![16]));
|
||||
|
||||
@@ -242,6 +242,7 @@ fn validate_files(files: &[FileSpec<'_>]) -> Result<u64, DownloadError> {
|
||||
if files.len() > 256 {
|
||||
return Err(DownloadError::Invalid("too many files".into()));
|
||||
}
|
||||
// (names that differ only in case are one file on a case-insensitive file system)
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut total = 0u64;
|
||||
for f in files {
|
||||
@@ -259,7 +260,7 @@ fn validate_files(files: &[FileSpec<'_>]) -> Result<u64, DownloadError> {
|
||||
|| f.name.starts_with('.')
|
||||
|| f.name.contains(['/', '\\', ':'])
|
||||
|| f.name.chars().any(char::is_control)
|
||||
|| !seen.insert(f.name)
|
||||
|| !seen.insert(f.name.to_lowercase())
|
||||
{
|
||||
return Err(DownloadError::Invalid("a filename must be a unique, ordinary filename".into()));
|
||||
}
|
||||
|
||||
@@ -425,3 +425,10 @@ fn hostile_file_specs_are_rejected_before_touching_disk() {
|
||||
assert!(!dir.exists());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn names_differing_only_in_case_are_one_file() {
|
||||
let spec = |name| FileSpec { name, size: Some(1), sha256: None, max: 1 };
|
||||
assert!(super::validate_files(&[spec("Model.bin")]).is_ok());
|
||||
assert!(super::validate_files(&[spec("Model.bin"), spec("model.BIN")]).is_err());
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
//! Grain slider drags on the GPU: the time per frame of the draft renders the loupe makes while
|
||||
//! Amount or Size is dragged, on a ~2.5 MP preview (1936 × 1290, the procedural demo photo).
|
||||
//! `cargo run --release -p lightcraft-gpu --example grain_drag_bench` (`FRAMES=n`, default 40).
|
||||
//!
|
||||
//! The same source, settings and request on every build, so it can be copied unchanged into
|
||||
//! another branch to compare grain models (see `docs/grain.md` → Performance). Each drag first
|
||||
//! renders four frames that aren't counted (stage cache, GPU pipelines), then moves the slider one
|
||||
//! step a frame; prints the median, fastest and slowest frame.
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
|
||||
use lightcraft_develop::DevelopSettings;
|
||||
use lightcraft_pipeline::{Quality, RenderRequest, SourceInfo, StageCache};
|
||||
|
||||
fn main() {
|
||||
let frames: usize = std::env::var("FRAMES").ok().and_then(|v| v.parse().ok()).unwrap_or(40).max(1);
|
||||
let (w, h) = (1936, 1290);
|
||||
let src = Arc::new(lightcraft_scenes::demo_library()[3].render(w, h));
|
||||
let info = SourceInfo { raw: true, ..Default::default() };
|
||||
let draft = RenderRequest { quality: Quality::Draft, ..RenderRequest::fit(w, h) };
|
||||
let Some(adapter) = lightcraft_gpu::adapter_name() else {
|
||||
eprintln!("no GPU adapter: nothing to measure");
|
||||
return;
|
||||
};
|
||||
println!("gpu: {adapter}; {w}×{h} draft renders, {frames} frames per drag");
|
||||
let mut s = DevelopSettings::default();
|
||||
s.light.exposure = 0.3;
|
||||
let mut drag = |label: &str, step: &dyn Fn(&mut DevelopSettings, usize)| {
|
||||
let cache = StageCache::default();
|
||||
let mut ms = Vec::with_capacity(frames);
|
||||
for i in 0..frames + 4 {
|
||||
step(&mut s, i);
|
||||
let t = Instant::now();
|
||||
assert!(lightcraft_gpu::render(&src, &info, &s, &draft, Some(&cache)).is_some(), "GPU render failed");
|
||||
if i >= 4 {
|
||||
ms.push(t.elapsed().as_secs_f64() * 1e3);
|
||||
}
|
||||
}
|
||||
ms.sort_by(f64::total_cmp);
|
||||
println!("{label:<30} median {:5.1} ms min {:5.1} max {:5.1}", ms[ms.len() / 2], ms[0], ms[ms.len() - 1]);
|
||||
};
|
||||
drag("exposure drag, no grain", &|s, i| {
|
||||
s.grain.amount = 0.0;
|
||||
s.light.exposure = 0.3 + i as f64 * 0.01;
|
||||
});
|
||||
drag("Amount drag (20 → 100)", &|s, i| {
|
||||
s.grain.size = 25.0;
|
||||
s.grain.amount = 20.0 + (i * 2 % 80) as f64;
|
||||
});
|
||||
drag("Size drag (amount 50, 0 → 100)", &|s, i| {
|
||||
s.grain.amount = 50.0;
|
||||
s.grain.size = (i * 2 % 100) as f64;
|
||||
});
|
||||
}
|
||||
+84
-2
@@ -51,6 +51,7 @@ const MODULES: &[Module] = &[
|
||||
"guided_pre",
|
||||
"guided_ab",
|
||||
"guided_apply",
|
||||
"clamp_between_k",
|
||||
"wb_k",
|
||||
"nr_lum",
|
||||
"chroma_k",
|
||||
@@ -197,7 +198,11 @@ fn build_jobs(
|
||||
layout: Some(&p.pipeline_layout),
|
||||
module: &p.module,
|
||||
entry_point: Some(entry),
|
||||
compilation_options: Default::default(),
|
||||
// Only the median kernels (`blur.wgsl`, `med_tile`) use workgroup memory, and they
|
||||
// load every element they read before the barrier, so wgpu's default zeroing is not
|
||||
// needed. On DX12 it is written out as one assignment per element, which made FXC take
|
||||
// about 87 s over these kernels at every start of a release build.
|
||||
compilation_options: wgpu::PipelineCompilationOptions { zero_initialize_workgroup_memory: false, ..Default::default() },
|
||||
cache: None,
|
||||
});
|
||||
let kernel = Kernel { pipeline, layout: p.layout.clone(), nbuf: p.nbuf };
|
||||
@@ -382,6 +387,11 @@ pub struct Gpu {
|
||||
free: std::sync::Mutex<Vec<Tracked>>,
|
||||
/// Largest storage buffer the device accepts (bytes).
|
||||
pub max_buffer: u64,
|
||||
/// The crystal grain tables of the last grain pattern, resident: a render (each Amount step
|
||||
/// of a drag) copies them on the GPU instead of uploading their 12 MB again ([`Gpu::grain_tables`]).
|
||||
/// Shared with each render that recorded a copy from them until it submits: replacing them
|
||||
/// while another thread's copy is unsubmitted must not hand the buffer back to the pool.
|
||||
grain: std::sync::Mutex<Option<(std::sync::Arc<lightcraft_pipeline::crystal::CrystalTile>, std::sync::Arc<Buf>)>>,
|
||||
/// The device is the window's (eframe's): renders can hand it textures to draw directly.
|
||||
pub window: bool,
|
||||
}
|
||||
@@ -430,6 +440,11 @@ fn constants() -> String {
|
||||
for (i, h) in GRAIN_HASH.iter().enumerate() {
|
||||
s += &format!("const GRAIN_H{i}: u32 = {h}u;\n");
|
||||
}
|
||||
use lightcraft_pipeline::crystal::{TILE_PER_LONG, TORI};
|
||||
s += &format!(
|
||||
"const CRYSTAL_T0: u32 = {}u;\nconst CRYSTAL_T1: u32 = {}u;\nconst CRYSTAL_TPL: f32 = {:?};\n",
|
||||
TORI[0], TORI[1], TILE_PER_LONG as f32
|
||||
);
|
||||
s += &format!("const DEEP_WRITTEN: u32 = {}u;\n", crate::params::DEEP_WRITTEN);
|
||||
for (name, idx) in crate::params::finish_fields() {
|
||||
s += &format!("const F_{name}: u32 = {idx}u;\n");
|
||||
@@ -543,7 +558,7 @@ impl Gpu {
|
||||
max_buffer >> 20,
|
||||
if window { ", the window's device" } else { "" }
|
||||
);
|
||||
Ok(Gpu { device, queue, info, kernels, dummy, free: Default::default(), max_buffer, window })
|
||||
Ok(Gpu { device, queue, info, kernels, dummy, free: Default::default(), max_buffer, window, grain: Default::default() })
|
||||
}
|
||||
|
||||
/// A buffer of `len` 32-bit values with undefined contents (a recycled one when possible).
|
||||
@@ -718,6 +733,44 @@ impl Gpu {
|
||||
);
|
||||
}
|
||||
|
||||
/// Record into `enc` a copy of `tile`'s summed-area tables (both tori, back to back) into
|
||||
/// `dst` at value offset `at`, uploading them first unless they are the resident ones.
|
||||
/// Returns the buffer copied from, which the caller keeps until it has submitted `enc` (another
|
||||
/// render may replace the resident tables meanwhile). None when `dst` can't hold them (an
|
||||
/// over-limit placeholder: the render has failed already).
|
||||
pub fn grain_tables(
|
||||
&self,
|
||||
enc: &mut wgpu::CommandEncoder,
|
||||
tile: &std::sync::Arc<lightcraft_pipeline::crystal::CrystalTile>,
|
||||
dst: &Buf,
|
||||
at: usize,
|
||||
) -> Option<std::sync::Arc<Buf>> {
|
||||
let len: usize = tile.sats.iter().map(Vec::len).sum();
|
||||
let (start, bytes) = ((at as u64) * 4, (len as u64) * 4);
|
||||
if len == 0 || dst.raw().size() < start.saturating_add(bytes) {
|
||||
return None;
|
||||
}
|
||||
let mut resident = self.grain.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if !resident.as_ref().is_some_and(|(t, b)| std::sync::Arc::ptr_eq(t, tile) && b.raw().size() >= bytes) {
|
||||
// the old tables go back to the pool first (once no unsubmitted copy holds them): one
|
||||
// pattern's tables are resident at a time
|
||||
*resident = None;
|
||||
let buf = self.buffer(len);
|
||||
if buf.raw().size() < bytes {
|
||||
return None;
|
||||
}
|
||||
let mut off = 0u64;
|
||||
for sat in &tile.sats {
|
||||
self.queue.write_buffer(buf.raw(), off, bytemuck::cast_slice(sat));
|
||||
off += (sat.len() as u64) * 4;
|
||||
}
|
||||
*resident = Some((tile.clone(), std::sync::Arc::new(buf)));
|
||||
}
|
||||
let (_, src) = resident.as_ref()?;
|
||||
enc.copy_buffer_to_buffer(src.raw(), 0, dst.raw(), start, bytes);
|
||||
Some(src.clone())
|
||||
}
|
||||
|
||||
/// Stands in for a buffer that could not be created (the render has failed already).
|
||||
fn placeholder(&self) -> wgpu::Buffer {
|
||||
self.device.create_buffer(&wgpu::BufferDescriptor {
|
||||
@@ -1321,6 +1374,35 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Resident grain tables replaced by another pattern stay out of the pool while a render
|
||||
/// that recorded a copy from them hasn't submitted it: reusing them then would hand that
|
||||
/// render another tile's tables.
|
||||
#[test]
|
||||
fn replaced_grain_tables_stay_alive_until_their_copy_is_submitted() {
|
||||
use lightcraft_pipeline::crystal::{CrystalLook, CrystalTile};
|
||||
let Some(g) = crate::device() else {
|
||||
lightcraft_testkit::skip!(Gpu, "no GPU device");
|
||||
return;
|
||||
};
|
||||
let look = CrystalLook::new(25.0, 50.0);
|
||||
let (Some(a), Some(b)) = (CrystalTile::new(look, 0x7ab1_0001), CrystalTile::new(look, 0x7ab1_0002)) else { panic!("tiles") };
|
||||
let (a, b) = (std::sync::Arc::new(a), std::sync::Arc::new(b));
|
||||
let len: usize = a.sats.iter().map(Vec::len).sum();
|
||||
let (dst_a, dst_b) = (g.buffer(len), g.buffer(len));
|
||||
let mut first = g.encoder();
|
||||
let held = g.grain_tables(&mut first, &a, &dst_a, 0).expect("tables copied");
|
||||
// another render replaces the resident tables before `first` is submitted
|
||||
let mut second = g.encoder();
|
||||
assert!(g.grain_tables(&mut second, &b, &dst_b, 0).is_some());
|
||||
let next = g.buffer(len);
|
||||
assert!(next.raw() != held.raw(), "the replaced tables went back to the pool before their copy was submitted");
|
||||
g.submit([first.finish()]);
|
||||
// each render read its own tile's tables
|
||||
let tables = |t: &lightcraft_pipeline::crystal::CrystalTile| t.sats.concat();
|
||||
assert!(g.finish_and_read::<f32>(second, &dst_a, len) == tables(&a), "the first render copied other tables");
|
||||
assert!(g.finish_and_read::<f32>(g.encoder(), &dst_b, len) == tables(&b), "the second render copied other tables");
|
||||
}
|
||||
|
||||
/// A GPU retired by another failure (a validation error) keeps a live device: its buffers are
|
||||
/// still released, which takes a poll.
|
||||
#[test]
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
use lightcraft_develop::VignetteStyle;
|
||||
use lightcraft_pipeline::colorops::POINT_WORDS;
|
||||
use lightcraft_pipeline::finish::{FinishParams, MASK_TERMS, srgb_lut};
|
||||
use lightcraft_pipeline::finish::{FinishParams, GrainModel, MASK_TERMS, srgb_lut};
|
||||
|
||||
use crate::ctx::{FailKind, fail};
|
||||
|
||||
@@ -71,6 +71,12 @@ const FIELDS: &[(&str, usize)] = &[
|
||||
// the window's offset in the whole output and the whole output's size (pixels)
|
||||
("VIG_VIEW", 4),
|
||||
("GRAIN", 1),
|
||||
// 0 = value noise (processes V1 to V6: GRAIN_AMT, GRAIN_SC, GRAIN_ROUGH), 1 = crystal layers (V7)
|
||||
("GRAIN_MODEL", 1),
|
||||
// crystal grain: amplitude, footprint (tile px per output px), the tori's tables in `aux`
|
||||
("GRAIN_K", 1),
|
||||
("GRAIN_FOOT", 1),
|
||||
("GRAIN_SAT", 2),
|
||||
("GRAIN_AMT", 1),
|
||||
("GRAIN_SC", 1),
|
||||
("GRAIN_ROUGH", 1),
|
||||
@@ -178,7 +184,8 @@ pub struct Present {
|
||||
}
|
||||
|
||||
/// The `finish` kernel's parameter block and auxiliary table (tone LUT | chroma curve | sRGB LUT | curve LUTs |
|
||||
/// mask terms | light operator tables) for `fp` with `masks` (their local adjustments' terms).
|
||||
/// mask terms | light operator tables) for `fp` with `masks` (their local adjustments' terms). The crystal
|
||||
/// grain's tables go after `aux` in the kernel's buffer ([`crate::render`] copies them there).
|
||||
///
|
||||
/// `deep`: the kernel writes float samples, as the pipeline's `finish_deep` does.
|
||||
///
|
||||
@@ -220,6 +227,16 @@ pub fn finish_block(fp: &FinishParams, masks: &[[f32; MASK_TERMS]], present: &Pr
|
||||
if let Some((l, _)) = &fp.lut {
|
||||
aux.extend(l.data.iter().flatten());
|
||||
}
|
||||
// the crystal grain's tables follow `aux` on the GPU without being part of it: they stay
|
||||
// resident between renders ([`crate::Gpu::grain_tables`]) and the render copies them in
|
||||
let mut sat_off = [0u32; 2];
|
||||
if let Some(GrainModel::Crystal(g)) = &fp.grain {
|
||||
let mut off = aux.len();
|
||||
for (slot, sat) in sat_off.iter_mut().zip(&g.tile.sats) {
|
||||
*slot = off as u32;
|
||||
off += sat.len();
|
||||
}
|
||||
}
|
||||
|
||||
let mut p = Block::new();
|
||||
p.u("LIGHT_OPS", light.len() as u32);
|
||||
@@ -330,12 +347,27 @@ pub fn finish_block(fp: &FinishParams, masks: &[[f32; MASK_TERMS]], present: &Pr
|
||||
p.b("PROOF_DEST_WARN", pp.dest_warning);
|
||||
p.b("PROOF_DISP_WARN", pp.display_warning);
|
||||
}
|
||||
if let Some((amt, cell, rough, seed)) = fp.grain {
|
||||
if let Some(model) = &fp.grain {
|
||||
p.b("GRAIN", true);
|
||||
p.f("GRAIN_AMT", amt);
|
||||
p.f("GRAIN_SC", fp.px_per_long as f32 / cell);
|
||||
p.f("GRAIN_ROUGH", rough);
|
||||
p.u("GRAIN_SEED", seed);
|
||||
match model {
|
||||
GrainModel::Value(g) => {
|
||||
p.u("GRAIN_MODEL", 0);
|
||||
p.f("GRAIN_AMT", g.amt);
|
||||
p.f("GRAIN_SC", fp.px_per_long as f32 / g.cell);
|
||||
p.f("GRAIN_ROUGH", g.rough);
|
||||
p.u("GRAIN_SEED", g.seed);
|
||||
}
|
||||
GrainModel::Crystal(g) => {
|
||||
p.u("GRAIN_MODEL", 1);
|
||||
p.f("GRAIN_K", g.k);
|
||||
p.f("GRAIN_FOOT", g.foot);
|
||||
if let Some(i) = index("GRAIN_SAT") {
|
||||
for (slot, off) in p.0.iter_mut().skip(i).zip(sat_off) {
|
||||
*slot = off;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let long = fp.ow.max(fp.oh);
|
||||
let [a, b, c, d, e, f] = fp.out_to_norm.0;
|
||||
let (kx, ky) = (fp.ow / long, fp.oh / long);
|
||||
|
||||
+130
-10
@@ -136,6 +136,10 @@ pub(crate) struct Cx<'a> {
|
||||
enc: Option<wgpu::CommandEncoder>,
|
||||
/// Kernel invocations recorded since the last submit.
|
||||
pending: u64,
|
||||
/// Buffers shared with other renders that recorded commands read from (the resident grain
|
||||
/// tables): kept until those commands are submitted. Declared after `enc`, so an unsubmitted
|
||||
/// encoder is dropped before them.
|
||||
held: Vec<std::sync::Arc<Buf>>,
|
||||
}
|
||||
|
||||
/// Kernel invocations recorded before a render submits them (under one full-size pass over a
|
||||
@@ -147,7 +151,7 @@ const BAND_PIXELS: usize = 4 << 20;
|
||||
|
||||
impl<'a> Cx<'a> {
|
||||
pub fn new(gpu: &'a Gpu) -> Cx<'a> {
|
||||
Cx { gpu, enc: Some(gpu.encoder()), pending: 0 }
|
||||
Cx { gpu, enc: Some(gpu.encoder()), pending: 0, held: Vec::new() }
|
||||
}
|
||||
|
||||
/// Record kernel `name` (see [`Gpu::run`]).
|
||||
@@ -161,11 +165,27 @@ impl<'a> Cx<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The finish kernel's `aux` buffer: `aux`, then the crystal grain's tables when it has them
|
||||
/// (copied on the GPU from the resident ones, see [`Gpu::grain_tables`]).
|
||||
pub fn finish_aux(&mut self, aux: &[f32], grain: Option<&lightcraft_pipeline::finish::GrainModel>) -> Buf {
|
||||
let Some(lightcraft_pipeline::finish::GrainModel::Crystal(g)) = grain else { return self.gpu.upload(aux) };
|
||||
let tables: usize = g.tile.sats.iter().map(Vec::len).sum();
|
||||
let out = self.gpu.buffer(aux.len().saturating_add(tables));
|
||||
if !aux.is_empty() && out.raw().size() >= (aux.len() as u64) * 4 {
|
||||
self.gpu.queue.write_buffer(out.raw(), 0, bytemuck::cast_slice(aux));
|
||||
}
|
||||
let enc = self.enc.get_or_insert_with(|| self.gpu.encoder());
|
||||
self.held.extend(self.gpu.grain_tables(enc, &g.tile, &out, aux.len()));
|
||||
out
|
||||
}
|
||||
|
||||
/// Submit what is recorded and read back `len` values of `b`.
|
||||
pub fn read<T: bytemuck::Pod>(&mut self, b: &Buf, len: usize) -> Vec<T> {
|
||||
self.pending = 0;
|
||||
let enc = self.enc.take().unwrap_or_else(|| self.gpu.encoder());
|
||||
self.gpu.finish_and_read(enc, b, len)
|
||||
let out = self.gpu.finish_and_read(enc, b, len);
|
||||
self.held.clear();
|
||||
out
|
||||
}
|
||||
|
||||
/// Submit what is recorded so far without waiting. Renders submit stage by stage and every
|
||||
@@ -177,6 +197,7 @@ impl<'a> Cx<'a> {
|
||||
if let Some(enc) = self.enc.take() {
|
||||
self.gpu.submit([enc.finish()]);
|
||||
}
|
||||
self.held.clear();
|
||||
}
|
||||
|
||||
/// Submit what is recorded and wait for it (profiling only: stage timings).
|
||||
@@ -185,6 +206,7 @@ impl<'a> Cx<'a> {
|
||||
if let Some(enc) = self.enc.take() {
|
||||
self.gpu.submit([enc.finish()]);
|
||||
}
|
||||
self.held.clear();
|
||||
let _ = self.gpu.poll(wgpu::PollType::wait_indefinitely());
|
||||
}
|
||||
|
||||
@@ -349,6 +371,9 @@ fn guided_coeffs(cx: &mut Cx<'_>, p: &Buf, w: usize, h: usize, sigma: f32, eps:
|
||||
gaussian(cx, &ab, w, h, 2, sigma)
|
||||
}
|
||||
|
||||
/// Pixels per thread along the pass of `median_h` / `median_v` (`MED_K` in `blur.wgsl`).
|
||||
const MEDIAN_K: usize = 4;
|
||||
|
||||
/// Separable median of a plane (`local::median_hv`).
|
||||
fn median_hv(cx: &mut Cx<'_>, p: &Buf, w: usize, h: usize, r: usize) -> Buf {
|
||||
if r == 0 || w * h == 0 {
|
||||
@@ -356,21 +381,38 @@ fn median_hv(cx: &mut Cx<'_>, p: &Buf, w: usize, h: usize, r: usize) -> Buf {
|
||||
}
|
||||
let pr = [w as u32, h as u32, 1, r.min(local::BASE_DETAIL_MAX) as u32];
|
||||
let tmp = cx.gpu.buffer(w * h);
|
||||
cx.run("median_h", &pr, &[Some(p), Some(&tmp)], groups2(w, h, [16, 16]));
|
||||
cx.run("median_h", &pr, &[Some(p), Some(&tmp)], groups2(w.div_ceil(MEDIAN_K), h, [16, 16]));
|
||||
let out = cx.gpu.buffer(w * h);
|
||||
cx.run("median_v", &pr, &[Some(&tmp), Some(&out)], groups2(w, h, [16, 16]));
|
||||
cx.run("median_v", &pr, &[Some(&tmp), Some(&out)], groups2(w, h.div_ceil(MEDIAN_K), [16, 16]));
|
||||
out
|
||||
}
|
||||
|
||||
/// The highlights/shadows base (`local::tone_base`; `detail` 0: V1, no median).
|
||||
fn tone_base(cx: &mut Cx<'_>, l: &Buf, w: usize, h: usize, sigma: f32, detail: usize) -> Buf {
|
||||
/// The highlights/shadows base (`local::tone_base`; `detail` 0: V1, no detail removal).
|
||||
fn tone_base(cx: &mut Cx<'_>, l: &Buf, w: usize, h: usize, sigma: f32, detail: usize, keeps_gaps: bool) -> Buf {
|
||||
if detail == 0 {
|
||||
return guided_fast(cx, l, w, h, sigma, local::BASE_EPS);
|
||||
}
|
||||
let m = median_hv(cx, l, w, h, detail);
|
||||
let m = if keeps_gaps { detail_free(cx, l, w, h, detail) } else { median_hv(cx, l, w, h, detail) };
|
||||
guided_fast(cx, &m, w, h, sigma, local::BASE_EPS)
|
||||
}
|
||||
|
||||
/// Log luminance without detail finer than `r` px (`local::detail_free`): clamped between its
|
||||
/// median at `r` and the median of a `local::BASE_COARSE_STEP`× downscaled copy, enlarged.
|
||||
fn detail_free(cx: &mut Cx<'_>, l: &Buf, w: usize, h: usize, r: usize) -> Buf {
|
||||
if r == 0 || w * h == 0 {
|
||||
return cx.copy(l);
|
||||
}
|
||||
let fine = median_hv(cx, l, w, h, r);
|
||||
let s = local::BASE_COARSE_STEP;
|
||||
let (lw, lh) = (w.div_ceil(s).max(1), h.div_ceil(s).max(1));
|
||||
let lo = resize(cx, l, (w, h), (lw, lh), 1, Filter::Box);
|
||||
let lo_median = median_hv(cx, &lo, lw, lh, r);
|
||||
let coarse = resize(cx, &lo_median, (lw, lh), (w, h), 1, Filter::Bilinear);
|
||||
let out = cx.gpu.buffer(w * h);
|
||||
map(cx, "clamp_between_k", w * h, &[], [Some(l), Some(&fine), Some(&coarse)], &out);
|
||||
out
|
||||
}
|
||||
|
||||
/// Fast guided filter (`local::guided_fast`): coefficients on a subsampled grid.
|
||||
fn guided_fast(cx: &mut Cx<'_>, p: &Buf, w: usize, h: usize, sigma: f32, eps: f32) -> Buf {
|
||||
let s = local::guided_fast_step(sigma);
|
||||
@@ -749,6 +791,7 @@ pub fn render(
|
||||
px_per_long: plan.px_per_long,
|
||||
air_pre: prep.air,
|
||||
space: req.space,
|
||||
draft: req.quality == lightcraft_pipeline::Quality::Draft,
|
||||
});
|
||||
// soft proofing as the CPU's `finish` sets it up (an output-device table never gets here: see `render`)
|
||||
fp.proof = req.proof.map(|pr| pr.params(req.space));
|
||||
@@ -780,7 +823,7 @@ pub fn render(
|
||||
if crate::ctx::failed() {
|
||||
return None; // a malformed parameter block failed the render: the CPU fallback redoes it
|
||||
}
|
||||
let aux = gpu.upload(&aux);
|
||||
let aux = cx.finish_aux(&aux, fp.grain.as_ref());
|
||||
// cleared, so that work which never ran cannot pass for an image (see the check below)
|
||||
let out = cx.zeroed(if deep { 3 * n } else { n });
|
||||
if fault == Some(crate::Fault::Validation) {
|
||||
@@ -1011,7 +1054,14 @@ fn linear(cx: &mut Cx<'_>, sampled: &Buf, info: &SourceInfo, plan: &Plan<'_>, ho
|
||||
eyed
|
||||
}
|
||||
};
|
||||
denoise(cx, img, plan, colour_noise)
|
||||
let img = denoise(cx, img, plan, colour_noise);
|
||||
if !lightcraft_pipeline::lens_blur::renders(s) {
|
||||
return img;
|
||||
}
|
||||
// Lens Blur (global or a mask's): on the CPU, like the CPU renderer's (`lens_blur::apply`)
|
||||
let mut host_img = cx.read_rgb(&img, w, h);
|
||||
lightcraft_pipeline::lens_blur::apply_with(&mut host_img, s, &plan.frame, plan.px_per_long, plan.mattes.as_deref(), plan.lens_subject.as_deref());
|
||||
cx.gpu.upload(rgb_words(&host_img))
|
||||
}
|
||||
|
||||
/// Defringe (`optics::defringe`) of the white-balanced image.
|
||||
@@ -1395,7 +1445,7 @@ fn prepare(cx: &mut Cx<'_>, lin: &Buf, plan: &Plan<'_>, req: &RenderRequest, pla
|
||||
let base = match sig.base {
|
||||
Some(sg) => {
|
||||
let key = local::base_key(&plan.settings, sg, sig.base_detail);
|
||||
plane_at(&mut planes.base, key, || tone_base(cx, &log_l, w, h, sg, sig.base_detail))
|
||||
plane_at(&mut planes.base, key, || tone_base(cx, &log_l, w, h, sg, sig.base_detail, local::base_keeps_gaps(&plan.settings)))
|
||||
}
|
||||
None => log_l.clone(),
|
||||
};
|
||||
@@ -1818,6 +1868,76 @@ mod tests {
|
||||
time("guided sigma=1.75", &mut |cx| drop(guided(cx, &src, w, h, 1.75, 0.01)));
|
||||
}
|
||||
|
||||
/// The tiled median kernels select exactly what the CPU's sorted window does: edge tiles,
|
||||
/// planes smaller than a tile or a window, every radius and many ties.
|
||||
#[test]
|
||||
fn median_matches_cpu_bit_for_bit() {
|
||||
// (creating the device writes the crash sentinel another test swaps out)
|
||||
let _serial = crate::DEVICE_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let Some(gpu) = crate::device() else { return lightcraft_testkit::skip!(Gpu, "no GPU adapter") };
|
||||
let _scope = crate::ctx::RenderScope::new(gpu);
|
||||
let mut seed = 0x9e37_79b9_u32;
|
||||
let mut next = move || {
|
||||
seed ^= seed << 13;
|
||||
seed ^= seed >> 17;
|
||||
seed ^= seed << 5;
|
||||
seed
|
||||
};
|
||||
for (w, h) in [(1, 1), (2, 3), (5, 70), (70, 5), (63, 65), (130, 67), (200, 129)] {
|
||||
// a coarse level set gives ties; a smooth ramp and fine noise give distinct values
|
||||
let mut p = Plane::new(w, h);
|
||||
for (i, o) in p.data.iter_mut().enumerate() {
|
||||
let (x, y) = (i % w, i / w);
|
||||
*o = match (next() % 3, next()) {
|
||||
(0, v) => (v % 7) as f32 * 0.5 - 2.0,
|
||||
(1, v) => (x + 2 * y) as f32 * 0.01 + (v % 100) as f32 * 1e-4,
|
||||
(_, v) => (v % 10_000) as f32 * 1e-3 - 5.0,
|
||||
};
|
||||
}
|
||||
let src = gpu.upload(&p.data);
|
||||
for r in 1..=local::BASE_DETAIL_MAX {
|
||||
let mut cx = Cx::new(gpu);
|
||||
let out = median_hv(&mut cx, &src, w, h, r);
|
||||
let got: Vec<f32> = cx.read(&out, w * h);
|
||||
let want = local::median_hv(&p, r);
|
||||
let bad = got.iter().zip(&want.data).position(|(g, c)| g.to_bits() != c.to_bits());
|
||||
assert!(bad.is_none(), "{w}×{h} r={r}: pixel {bad:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Highlights/shadows base timings on the #3460 frame size:
|
||||
/// `cargo test --release -p lightcraft-gpu bench_tone_base -- --ignored --nocapture`.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn bench_tone_base() {
|
||||
let Some(gpu) = crate::device() else { return eprintln!("no gpu: {:?}", crate::unavailable_reason()) };
|
||||
let _scope = crate::ctx::RenderScope::new(gpu);
|
||||
let (w, h) = (6960, 4640);
|
||||
let r = local::base_detail_radius(w as f64);
|
||||
eprintln!("{} ({:?}) r={r}", gpu.info.name, gpu.info.backend);
|
||||
let l: Vec<f32> = (0..w * h).map(|i: usize| ((i.wrapping_mul(2_654_435_761)) % 1000) as f32 / 250.0 - 6.0).collect();
|
||||
let src = gpu.upload(&l);
|
||||
let time = |name: &str, f: &mut dyn FnMut(&mut Cx<'_>)| {
|
||||
let mut ts = Vec::new();
|
||||
for _ in 0..9 {
|
||||
let mut cx = Cx::new(gpu);
|
||||
cx.sync();
|
||||
let t = std::time::Instant::now();
|
||||
f(&mut cx);
|
||||
cx.sync();
|
||||
ts.push(t.elapsed().as_secs_f64() * 1e3);
|
||||
}
|
||||
ts.sort_by(f64::total_cmp);
|
||||
eprintln!("{name:<40} min {:.1} ms median {:.1} ms", ts[0], ts[4]);
|
||||
};
|
||||
let sigma = 0.015 * w as f32;
|
||||
time("median_hv", &mut |cx| drop(median_hv(cx, &src, w, h, r)));
|
||||
time("detail_free", &mut |cx| drop(detail_free(cx, &src, w, h, r)));
|
||||
time("guided_fast", &mut |cx| drop(guided_fast(cx, &src, w, h, sigma, local::BASE_EPS)));
|
||||
time("tone_base", &mut |cx| drop(tone_base(cx, &src, w, h, sigma, r, true)));
|
||||
}
|
||||
|
||||
/// The device scores Remove's candidate sources like the CPU and picks the same one.
|
||||
#[test]
|
||||
fn auto_source_picks_the_cpu_candidate() {
|
||||
|
||||
@@ -113,56 +113,114 @@ fn minmax(@builtin(global_invocation_id) g: vec3<u32>) {
|
||||
}
|
||||
|
||||
// Separable median of a single-channel plane (`lightcraft_pipeline::local::median_hv`): 2r+1
|
||||
// taps (r ≤ 24, clamped edges) along a row (median_h) or a column (median_v), one thread per
|
||||
// pixel. The median is a selection, so the result matches the CPU's bit for bit.
|
||||
// taps (1 ≤ r ≤ 24, clamped edges) along a row (median_h) or a column (median_v). The median is a
|
||||
// selection, so the result matches the CPU's bit for bit.
|
||||
// P: w, h, nc (unused), r.
|
||||
fn median_of(n: u32, r: u32, v: ptr<function, array<f32, 49>>) -> f32 {
|
||||
// partial selection sort up to the middle
|
||||
for (var i = 0u; i <= r; i++) {
|
||||
var m = i;
|
||||
for (var j = i + 1u; j < n; j++) {
|
||||
if ((*v)[j] < (*v)[m]) {
|
||||
m = j;
|
||||
}
|
||||
//
|
||||
// Each thread finds the medians of 4 neighbouring pixels along the pass by rank: a tap is the
|
||||
// median of a window when fewer than r+1 of its taps are smaller and at least r+1 are smaller or
|
||||
// equal. The 4 windows share all but their first and last 3 taps, so one count over the shared
|
||||
// taps serves all four. A 16×16 workgroup (64 pixels along the pass, 16 lines across) first loads
|
||||
// its taps into `med_tile`. On a 6960×4640 plane at r = 10 this takes 47 ms on an M2 Max, against
|
||||
// 191 ms for a partial selection sort per pixel over a private array of taps, which drivers keep in
|
||||
// scratch memory because it is indexed dynamically.
|
||||
const MED_K: u32 = 4u;
|
||||
const MED_LINE: u32 = 113u; // 16·MED_K + 2·24, odd
|
||||
// Not zero-initialised (`build_jobs` in ctx.rs turns that off): write every element read.
|
||||
var<workgroup> med_tile: array<f32, 1808>; // 16 lines
|
||||
|
||||
// The medians of the 2r+1-tap windows starting at `med_tile[base + k]`, k < 4 (r ≥ 1). Taps 3 to
|
||||
// n-1 are in all four windows; the three at each end are in some.
|
||||
fn median4(base: u32, r: u32) -> vec4<f32> {
|
||||
let n = 2u * r + 1u;
|
||||
let e0 = vec3<f32>(med_tile[base], med_tile[base + 1u], med_tile[base + 2u]);
|
||||
let e1 = vec3<f32>(med_tile[base + n], med_tile[base + n + 1u], med_tile[base + n + 2u]);
|
||||
var out = vec4<f32>(0.0);
|
||||
var found = vec4<bool>(false);
|
||||
for (var i = 0u; i < n + 3u; i++) {
|
||||
let c = med_tile[base + i];
|
||||
var less = 0u;
|
||||
var le = 0u;
|
||||
for (var j = 3u; j < n; j++) {
|
||||
let v = med_tile[base + j];
|
||||
less += select(0u, 1u, v < c);
|
||||
le += select(0u, 1u, v <= c);
|
||||
}
|
||||
let l0 = select(vec3<u32>(0u), vec3<u32>(1u), e0 < vec3<f32>(c));
|
||||
let l1 = select(vec3<u32>(0u), vec3<u32>(1u), e1 < vec3<f32>(c));
|
||||
let q0 = select(vec3<u32>(0u), vec3<u32>(1u), e0 <= vec3<f32>(c));
|
||||
let q1 = select(vec3<u32>(0u), vec3<u32>(1u), e1 <= vec3<f32>(c));
|
||||
// window k: taps k..k+n-1 = taps k..2 of the start, the middle, taps n..n+k-1 of the end
|
||||
let lw = vec4<u32>(l0.x + l0.y + l0.z, l0.y + l0.z + l1.x, l0.z + l1.x + l1.y, l1.x + l1.y + l1.z) + vec4<u32>(less);
|
||||
let qw = vec4<u32>(q0.x + q0.y + q0.z, q0.y + q0.z + q1.x, q0.z + q1.x + q1.y, q1.x + q1.y + q1.z) + vec4<u32>(le);
|
||||
let has = vec4<bool>((i < n), (i >= 1u) && (i < n + 1u), (i >= 2u) && (i < n + 2u), (i >= 3u));
|
||||
let hit = has & !found & (lw <= vec4<u32>(r)) & (qw > vec4<u32>(r));
|
||||
out = select(out, vec4<f32>(c), hit);
|
||||
found = found | hit;
|
||||
if (all(found)) {
|
||||
break;
|
||||
}
|
||||
let t = (*v)[i];
|
||||
(*v)[i] = (*v)[m];
|
||||
(*v)[m] = t;
|
||||
}
|
||||
return (*v)[r];
|
||||
return out;
|
||||
}
|
||||
|
||||
// Thread (l.x, l.y) of a workgroup takes line l.x and pixels 4·l.y to 4·l.y + 3 along it, so the
|
||||
// threads of a SIMD group read 16 different lines at once; with an odd line length those fall in
|
||||
// different banks of workgroup memory.
|
||||
@compute @workgroup_size(16, 16)
|
||||
fn median_h(@builtin(global_invocation_id) g: vec3<u32>) {
|
||||
fn median_h(@builtin(local_invocation_id) l: vec3<u32>, @builtin(workgroup_id) wg: vec3<u32>) {
|
||||
let w = pu(0u);
|
||||
let h = pu(1u);
|
||||
let r = min(pu(3u), 24u);
|
||||
if (g.x >= w || g.y >= h) {
|
||||
let span = 16u * MED_K + 2u * r;
|
||||
let x0 = i32(wg.x * 16u * MED_K) - i32(r);
|
||||
// load: consecutive threads read consecutive pixels of a row
|
||||
for (var e = l.y * 16u + l.x; e < 16u * span; e += 256u) {
|
||||
let line = e / span;
|
||||
let k = e % span;
|
||||
let y = min(wg.y * 16u + line, h - 1u);
|
||||
let x = clamp(x0 + i32(k), 0, i32(w) - 1);
|
||||
med_tile[line * MED_LINE + k] = src[y * w + u32(x)];
|
||||
}
|
||||
workgroupBarrier();
|
||||
let y = wg.y * 16u + l.x;
|
||||
let x = (wg.x * 16u + l.y) * MED_K;
|
||||
if (x >= w || y >= h) {
|
||||
return;
|
||||
}
|
||||
let n = 2u * r + 1u;
|
||||
var v: array<f32, 49>;
|
||||
let row = g.y * w;
|
||||
for (var k = 0u; k < n; k++) {
|
||||
let x = clamp(i32(g.x + k) - i32(r), 0, i32(w) - 1);
|
||||
v[k] = src[row + u32(x)];
|
||||
let m = median4(l.x * MED_LINE + l.y * MED_K, r);
|
||||
for (var k = 0u; k < MED_K; k++) {
|
||||
if (x + k < w) {
|
||||
dst[y * w + x + k] = m[k];
|
||||
}
|
||||
}
|
||||
dst[row + g.x] = median_of(n, r, &v);
|
||||
}
|
||||
|
||||
@compute @workgroup_size(16, 16)
|
||||
fn median_v(@builtin(global_invocation_id) g: vec3<u32>) {
|
||||
fn median_v(@builtin(local_invocation_id) l: vec3<u32>, @builtin(workgroup_id) wg: vec3<u32>) {
|
||||
let w = pu(0u);
|
||||
let h = pu(1u);
|
||||
let r = min(pu(3u), 24u);
|
||||
if (g.x >= w || g.y >= h) {
|
||||
let span = 16u * MED_K + 2u * r;
|
||||
let y0 = i32(wg.y * 16u * MED_K) - i32(r);
|
||||
// load: consecutive threads read consecutive pixels of a row (16 columns)
|
||||
for (var e = l.y * 16u + l.x; e < 16u * span; e += 256u) {
|
||||
let line = e % 16u;
|
||||
let k = e / 16u;
|
||||
let x = min(wg.x * 16u + line, w - 1u);
|
||||
let y = clamp(y0 + i32(k), 0, i32(h) - 1);
|
||||
med_tile[line * MED_LINE + k] = src[u32(y) * w + x];
|
||||
}
|
||||
workgroupBarrier();
|
||||
let x = wg.x * 16u + l.x;
|
||||
let y = (wg.y * 16u + l.y) * MED_K;
|
||||
if (x >= w || y >= h) {
|
||||
return;
|
||||
}
|
||||
let n = 2u * r + 1u;
|
||||
var v: array<f32, 49>;
|
||||
for (var k = 0u; k < n; k++) {
|
||||
let y = clamp(i32(g.y + k) - i32(r), 0, i32(h) - 1);
|
||||
v[k] = src[u32(y) * w + g.x];
|
||||
let m = median4(l.x * MED_LINE + l.y * MED_K, r);
|
||||
for (var k = 0u; k < MED_K; k++) {
|
||||
if (y + k < h) {
|
||||
dst[(y + k) * w + x] = m[k];
|
||||
}
|
||||
}
|
||||
dst[g.y * w + g.x] = median_of(n, r, &v);
|
||||
}
|
||||
@@ -291,6 +291,65 @@ fn ghash(i: i32, j: i32, seed: u32) -> f32 {
|
||||
return f32(v & 0xffffu) / 32768.0 - 1.0;
|
||||
}
|
||||
|
||||
// Crystal grain (process V4): `lightcraft_pipeline::crystal` (keep in step with it). The tori's
|
||||
// summed-area tables follow the other tables in `aux` (offsets F_GRAIN_SAT).
|
||||
fn cg_bilinear(o: u32, t: u32, x: f32, y: f32) -> f32 {
|
||||
let s = t + 1u;
|
||||
let i = min(u32(max(x, 0.0)), t - 1u);
|
||||
let j = min(u32(max(y, 0.0)), t - 1u);
|
||||
let fx = x - f32(i);
|
||||
let fy = y - f32(j);
|
||||
let b = o + j * s + i;
|
||||
let a = aux[b] + (aux[b + 1u] - aux[b]) * fx;
|
||||
let c = aux[b + s] + (aux[b + s + 1u] - aux[b + s]) * fx;
|
||||
return a + (c - a) * fy;
|
||||
}
|
||||
|
||||
fn cg_integral(o: u32, t: u32, x: f32, y: f32) -> f32 {
|
||||
let tf = f32(t);
|
||||
let wx = x >= tf;
|
||||
let wy = y >= tf;
|
||||
let rx = select(x, x - tf, wx);
|
||||
let ry = select(y, y - tf, wy);
|
||||
var v = cg_bilinear(o, t, rx, ry);
|
||||
if (wx) {
|
||||
v += cg_bilinear(o, t, tf, ry);
|
||||
}
|
||||
if (wy) {
|
||||
v += cg_bilinear(o, t, rx, tf);
|
||||
}
|
||||
if (wx && wy) {
|
||||
v += cg_bilinear(o, t, tf, tf);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
fn cg_box(o: u32, t: u32, x0_: f32, y0_: f32, w: f32) -> f32 {
|
||||
let tf = f32(t);
|
||||
let x0 = x0_ - floor(x0_ / tf) * tf;
|
||||
let y0 = y0_ - floor(y0_ / tf) * tf;
|
||||
return cg_integral(o, t, x0 + w, y0 + w) - cg_integral(o, t, x0, y0 + w) - cg_integral(o, t, x0 + w, y0) + cg_integral(o, t, x0, y0);
|
||||
}
|
||||
|
||||
fn cg_lift(v: f32) -> f32 {
|
||||
let c = clamp(v, 0.0, 1.0);
|
||||
return select((c + 0.055) / 2.4, c, c <= 0.04045);
|
||||
}
|
||||
|
||||
fn cg_srgb_to_linear(v: f32) -> f32 {
|
||||
return select(pow((v + 0.055) / 1.055, 2.4), v / 12.92, v <= 0.04045);
|
||||
}
|
||||
|
||||
fn crystal_grain(e: vec3<f32>, gx: f32, gy: f32) -> vec3<f32> {
|
||||
let w = pf(F_GRAIN_FOOT);
|
||||
let x0 = gx * CRYSTAL_TPL - 0.5 * w;
|
||||
let y0 = gy * CRYSTAL_TPL - 0.5 * w;
|
||||
let g = (cg_box(pu(F_GRAIN_SAT), CRYSTAL_T0, x0, y0, w) + cg_box(pu(F_GRAIN_SAT + 1u), CRYSTAL_T1, x0, y0, w)) / (w * w);
|
||||
let lum = clamp(0.2126 * e.x + 0.7152 * e.y + 0.0722 * e.z, 0.0, 1.0);
|
||||
let k = pf(F_GRAIN_K) * g * (1.0 - cg_srgb_to_linear(lum));
|
||||
return e + k * vec3<f32>(cg_lift(e.x), cg_lift(e.y), cg_lift(e.z));
|
||||
}
|
||||
|
||||
fn grain_noise(x: f32, y: f32, seed: u32) -> f32 {
|
||||
let x0 = floor(x);
|
||||
let y0 = floor(y);
|
||||
@@ -694,17 +753,21 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
||||
let py = f32(y) + 0.5;
|
||||
let gx = pf(F_GRAIN_AFF) * px + pf(F_GRAIN_AFF + 2u) * py + pf(F_GRAIN_AFF + 4u);
|
||||
let gy = pf(F_GRAIN_AFF + 1u) * px + pf(F_GRAIN_AFF + 3u) * py + pf(F_GRAIN_AFF + 5u);
|
||||
let sc = pf(F_GRAIN_SC);
|
||||
let rough = pf(F_GRAIN_ROUGH);
|
||||
let seed = pu(F_GRAIN_SEED);
|
||||
var g = grain_noise(gx * sc, gy * sc, seed);
|
||||
g = g * (1.0 - rough * 0.5) + grain_noise(gx * sc * 2.3, gy * sc * 2.3, seed ^ 0x55u) * rough * 0.7;
|
||||
var lum = 0.2126 * e.x + 0.7152 * e.y + 0.0722 * e.z;
|
||||
if (hdr) {
|
||||
lum = clamp(lum, 0.0, 1.0);
|
||||
if (pu(F_GRAIN_MODEL) == 1u) {
|
||||
e = crystal_grain(e, gx, gy);
|
||||
} else {
|
||||
let sc = pf(F_GRAIN_SC);
|
||||
let rough = pf(F_GRAIN_ROUGH);
|
||||
let seed = pu(F_GRAIN_SEED);
|
||||
var g = grain_noise(gx * sc, gy * sc, seed);
|
||||
g = g * (1.0 - rough * 0.5) + grain_noise(gx * sc * 2.3, gy * sc * 2.3, seed ^ 0x55u) * rough * 0.7;
|
||||
var lum = 0.2126 * e.x + 0.7152 * e.y + 0.0722 * e.z;
|
||||
if (hdr) {
|
||||
lum = clamp(lum, 0.0, 1.0);
|
||||
}
|
||||
let k = pf(F_GRAIN_AMT) * g * (0.35 + 2.6 * lum * (1.0 - lum));
|
||||
e = e + k;
|
||||
}
|
||||
let k = pf(F_GRAIN_AMT) * g * (0.35 + 2.6 * lum * (1.0 - lum));
|
||||
e = e + k;
|
||||
}
|
||||
if (pu(F_LUT) != 0u) {
|
||||
let lc = clamp(e, vec3<f32>(0.0), vec3<f32>(1.0));
|
||||
|
||||
@@ -70,6 +70,25 @@ fn guided_apply(@builtin(global_invocation_id) g: vec3<u32>, @builtin(num_workgr
|
||||
dst[i] = b[2u * i] * a[i] + b[2u * i + 1u];
|
||||
}
|
||||
|
||||
// `clamp_between` (`local::detail_free`): a clamped between b and c, in either order.
|
||||
@compute @workgroup_size(256)
|
||||
fn clamp_between_k(@builtin(global_invocation_id) g: vec3<u32>, @builtin(num_workgroups) nw: vec3<u32>) {
|
||||
let i = lin_index(g, nw);
|
||||
if (i >= pu(0u)) {
|
||||
return;
|
||||
}
|
||||
let lo = min(b[i], c[i]);
|
||||
let hi = max(b[i], c[i]);
|
||||
let v = a[i];
|
||||
if (v < lo) {
|
||||
dst[i] = lo;
|
||||
} else if (v > hi) {
|
||||
dst[i] = hi;
|
||||
} else {
|
||||
dst[i] = v;
|
||||
}
|
||||
}
|
||||
|
||||
// Cross guided filter (`masks::guided_cross`), a: guide, b: input. P[1] = 0: (guide, input);
|
||||
// 1: (guide·input, guide²).
|
||||
@compute @workgroup_size(256)
|
||||
|
||||
@@ -210,6 +210,33 @@ fn banded_full_size_render_matches() {
|
||||
check("banded 3000×2000", &src, &SourceInfo { raw: true, ..Default::default() }, &s, &RenderRequest::fit(w, h));
|
||||
}
|
||||
|
||||
/// The crystal grain's tables stay on the GPU between renders: an Amount change reuses them,
|
||||
/// another pattern (Size, Roughness, seed) replaces them, and going back draws the first again.
|
||||
#[test]
|
||||
fn grain_patterns_in_turn_match() {
|
||||
if !gpu() {
|
||||
return;
|
||||
}
|
||||
let src = scene(1, 480, 320);
|
||||
let info = SourceInfo { raw: true, ..Default::default() };
|
||||
let steps: [(&str, f64, f64, f64, u32); 6] = [
|
||||
("grain 50/25/50", 50.0, 25.0, 50.0, 1),
|
||||
("grain 90/25/50 (same pattern)", 90.0, 25.0, 50.0, 1),
|
||||
("grain 50/70/50 (new size)", 50.0, 70.0, 50.0, 1),
|
||||
("grain 50/70/0 (new roughness)", 50.0, 70.0, 0.0, 1),
|
||||
("grain 50/70/0 seed 7", 50.0, 70.0, 0.0, 7),
|
||||
("grain 50/25/50 again", 50.0, 25.0, 50.0, 1),
|
||||
];
|
||||
for (name, amount, size, roughness, seed) in steps {
|
||||
let mut s = DevelopSettings::default();
|
||||
s.grain.amount = amount;
|
||||
s.grain.size = size;
|
||||
s.grain.roughness = roughness;
|
||||
s.grain.seed = seed;
|
||||
check(name, &src, &info, &s, &RenderRequest::fit(480, 320));
|
||||
}
|
||||
}
|
||||
|
||||
fn cases() -> Vec<(&'static str, Edit)> {
|
||||
vec![
|
||||
("default", |_| {}),
|
||||
@@ -287,6 +314,25 @@ fn cases() -> Vec<(&'static str, Edit)> {
|
||||
s.grain.size = 40.0;
|
||||
s.grain.roughness = 60.0;
|
||||
}),
|
||||
// the frozen value-noise grain of the earlier processes (the default is V7's crystals)
|
||||
("grain, process V6", |s| {
|
||||
s.process = Process::V6.version();
|
||||
s.grain.amount = 50.0;
|
||||
s.grain.size = 40.0;
|
||||
s.grain.roughness = 60.0;
|
||||
}),
|
||||
("grain, process V1", |s| {
|
||||
s.process = Process::V1.version();
|
||||
s.grain.amount = 50.0;
|
||||
s.grain.size = 40.0;
|
||||
s.grain.roughness = 60.0;
|
||||
}),
|
||||
("grain, process V2", |s| {
|
||||
s.process = Process::V2.version();
|
||||
s.grain.amount = 50.0;
|
||||
s.grain.size = 40.0;
|
||||
s.grain.roughness = 60.0;
|
||||
}),
|
||||
("dehaze -", |s| s.effects.dehaze = -50.0),
|
||||
("dehaze +", |s| s.effects.dehaze = 60.0),
|
||||
("clarity/texture -", |s| {
|
||||
@@ -761,7 +807,7 @@ fn light_v3_operators_match() {
|
||||
}
|
||||
|
||||
/// Issue #632: the Highlights / Shadows base of each process (V1: the edge-aware filter of log
|
||||
/// luminance; V2: of its fine median, a radius that grows with the render) matches the CPU's, and
|
||||
/// luminance; V2 to V7: of its fine median, V8 on: of its detail-free plane, a radius that grows with the render) matches the CPU's, and
|
||||
/// a warm cache never hands one process the other's base.
|
||||
#[test]
|
||||
fn highlights_shadows_base_matches_on_every_process() {
|
||||
@@ -792,7 +838,7 @@ fn highlights_shadows_base_matches_on_every_process() {
|
||||
}];
|
||||
check(&format!("{p:?} local highlights {w}"), &src, &raw, &s, &req);
|
||||
}
|
||||
for p in [Process::V1, Process::V2, Process::V4, Process::V1, Process::V2, Process::V4] {
|
||||
for p in [Process::V1, Process::V2, Process::V4, Process::V8, Process::V1, Process::V2, Process::V4, Process::V8] {
|
||||
let mut s = DevelopSettings { process: p.version(), ..DevelopSettings::default() };
|
||||
s.light.highlights = -100.0;
|
||||
let warm = lightcraft_gpu::render(&src, &raw, &s, &req, Some(&cache)).expect("gpu");
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
[package]
|
||||
name = "lightcraft-heif"
|
||||
description = "Optional HEIF/HEIC still-image decoder for LightCraft (heic-rs for the container and HEVC, libheif-matching colour conversion, panic-guarded)"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
# Pure Rust, no unsafe. Pinned exactly: a young, single-owner crate that parses untrusted files;
|
||||
# moving the pin is a reviewed change (re-run the heif tests and the codecs corpus tests).
|
||||
# Mirrors PhotoCraft's photocraft-heif (same team, same decision): heic-rs was chosen over the
|
||||
# other permissive candidates (heif-oxide's HEVC decoder, rust_h265, follows FFmpeg's (LGPL)
|
||||
# code structure; gamut-heic has no HEVC decoder of its own).
|
||||
heic-rs = { version = "=0.1.4", default-features = false, features = ["std"] }
|
||||
|
||||
# The tiles of a HEIC photo decode in parallel on native; wasm stays single-threaded.
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
heic-rs = { version = "=0.1.4", default-features = false, features = ["std", "parallel"] }
|
||||
rayon = { workspace = true }
|
||||
|
||||
[features]
|
||||
# `lightcraft_heif::testdata`: lossless synthetic HEIF files, for the tests of the crates above.
|
||||
testdata = []
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -1,47 +0,0 @@
|
||||
# lightcraft-heif
|
||||
|
||||
The optional HEIF / HEIC decoder of LightCraft: iPhone and Mac photos. [`heic-rs`](https://github.com/tbraun96/heic-rs)
|
||||
(pure Rust, no `unsafe`, MIT OR Apache-2.0, written from the H.265 / HEIF specifications) reads the
|
||||
container and reconstructs the HEVC pictures; this crate turns them into 16-bit RGB itself so the
|
||||
result matches libheif to within one code value:
|
||||
|
||||
- the colour description is the item's `colr nclx` (or its first tile's), else the HEVC VUI
|
||||
(iPhones: full range), else the H.265 defaults; an unspecified matrix is BT.601. heic-rs alone
|
||||
assumes BT.709 limited range, about 8 code values off on every iPhone photo;
|
||||
- chroma is upsampled by nearest neighbour, or centred bilinear when a `clap` starts on an odd
|
||||
pixel, as libheif does;
|
||||
- `imir` axis 0 flips top and bottom, as libheif writes and reads it (heic-rs names it the other way);
|
||||
- a grid's ICC profile may sit on its tiles.
|
||||
|
||||
Single pictures and grid-tiled photos, 4:2:0 and monochrome, 8- to 10-bit, alpha auxiliary images
|
||||
(other auxiliary images — HDR gain maps, depth — are ignored), the container's rotation/mirror/crop,
|
||||
ICC, EXIF and XMP, and the file's thumbnail item. 4:2:2 and 4:4:4 (Canon/Sony "HIF") are refused
|
||||
with `Error::Unsupported`: heic-rs 0.1.1 loses sync on them. Read-only.
|
||||
|
||||
```rust
|
||||
let info = lightcraft_heif::probe(&bytes)?; // size, depth, alpha: check limits first
|
||||
let img = lightcraft_heif::decode(&bytes, &lightcraft_heif::Options::default())?;
|
||||
// img.width, img.height, img.has_alpha, img.bit_depth, img.data (RGB/RGBA u16), img.colour, img.icc, img.exif, img.xmp
|
||||
let thumb = lightcraft_heif::decode_thumbnail(&bytes, 256, &Default::default())?; // Option<Decoded>
|
||||
```
|
||||
|
||||
Errors are `Error::Unsupported` (image sequences, overlays, 4:2:2/4:4:4, …), `Error::Limit` or
|
||||
`Error::Malformed`. It never panics: every call into heic-rs runs under `catch_unwind`, and a panic
|
||||
inside it (PhotoCraft's fuzzing found two in 0.1.1) becomes `Error::Malformed`.
|
||||
|
||||
Feature `testdata` exposes `lightcraft_heif::testdata`: lossless synthetic HEIF files (PCM-coded
|
||||
32 × 32 HEVC pictures, grids, every property above) for the tests of the crates above.
|
||||
|
||||
## Why it is a separate, optional crate
|
||||
|
||||
- **A young decoder.** heic-rs is new and has a single maintainer. It is pinned exactly
|
||||
(`=0.1.1`); moving the pin is a reviewed change (re-run the crate tests and
|
||||
`cargo test -p lightcraft-codecs --features heif`). The other permissive candidates were
|
||||
rejected: heif-oxide's HEVC decoder (rust_h265) follows FFmpeg's (LGPL) code structure, and
|
||||
gamut-heic has no HEVC decoder of its own.
|
||||
- **HEVC patents are a distributor's call.** `lightcraft-codecs` uses this crate only behind its
|
||||
non-default `heif` feature; the apps forward it and the release packages enable it. Without it,
|
||||
HEIC files are still recognised and opening one is a clear "HEIC/HEIF support isn't included in
|
||||
this build" error — the same policy as PhotoCraft.
|
||||
|
||||
The crate depends on no other LightCraft crate; `codecs` → `heif` is the only edge between them.
|
||||
@@ -1,122 +0,0 @@
|
||||
//! Caps on the container's metadata boxes, counted before heic-rs touches the file.
|
||||
//!
|
||||
//! heic-rs 0.1.4 (pinned `=0.1.4`) has no aggregate work budget inside its container parsing:
|
||||
//! every `ipma` association entry rescans the vector of already-seen item ids (quadratic in
|
||||
//! the number of entries), and an `iloc` extent using construction method 2 reconstructs its
|
||||
//! source item afresh for every extent that references it, with no shared cache. Legitimate
|
||||
//! phone-photo metadata never comes near these caps — real corpora keep both well under a few
|
||||
//! kilobytes — so anything above them is hostile or corrupt and is refused before the
|
||||
//! countable CPU work happens.
|
||||
//!
|
||||
//! [`depth_boxes`] stops silently at structural blips: heic-rs's own parser reports those
|
||||
//! better than this walk could, and an `ipma` box past the cap is caught wherever it parses.
|
||||
|
||||
use heic_rs::boxes::BoxIter;
|
||||
|
||||
use crate::Error;
|
||||
|
||||
/// The largest `ipma` box (item ↔ property associations) payload accepted, 64 KiB. An entry
|
||||
/// costs at least 3 bytes, so this bounds heic-rs's per-entry rescan of earlier item ids to
|
||||
/// ~21 000 entries ≈ 220 million comparisons: long enough to be annoying, never minutes.
|
||||
const MAX_IPMA_BYTES: usize = 64 << 10;
|
||||
|
||||
/// The largest `iloc` box (item locations) payload accepted, 64 KiB. An extent costs at least
|
||||
/// 12 bytes, so this bounds both the parse itself and how many extents a
|
||||
/// construction-method-2 reconstruction chain can visit.
|
||||
const MAX_ILOC_BYTES: usize = 64 << 10;
|
||||
|
||||
/// Falls through unless the container declares more `ipma` or `iloc` payload than a
|
||||
/// legitimate phone photo ever does.
|
||||
pub fn cap(bytes: &[u8]) -> Result<(), Error> {
|
||||
let mut c = Census::default();
|
||||
depth_boxes(&mut c, BoxIter::new(bytes), 0);
|
||||
if c.ipma > MAX_IPMA_BYTES {
|
||||
return Err(Error::Limit(format!("the image carries {} bytes of property associations, over the {MAX_IPMA_BYTES}-byte cap", c.ipma)));
|
||||
}
|
||||
if c.iloc > MAX_ILOC_BYTES {
|
||||
return Err(Error::Limit(format!("the image carries {} bytes of item locations, over the {MAX_ILOC_BYTES}-byte cap", c.iloc)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The metadata-box payload sizes seen so far.
|
||||
#[derive(Default)]
|
||||
struct Census {
|
||||
ipma: usize,
|
||||
iloc: usize,
|
||||
}
|
||||
|
||||
/// Walks boxes, adding the payload sizes of those named in [`Census`].
|
||||
///
|
||||
/// Only `meta` and `iprp` (the two nesting boxes the metadata is built from) are descended
|
||||
/// into, no deeper than that depth, so a nested-box trick can't hide a box from the count.
|
||||
fn depth_boxes(c: &mut Census, boxes: BoxIter<'_>, depth: u32) {
|
||||
if depth > 3 {
|
||||
return;
|
||||
}
|
||||
for b in boxes {
|
||||
// A structural error stops the walk; `heic_rs::probe` reports it properly afterwards.
|
||||
let Ok(b) = b else { return };
|
||||
if b.is(b"ipma") {
|
||||
c.ipma = c.ipma.saturating_add(b.payload.len());
|
||||
} else if b.is(b"iloc") {
|
||||
c.iloc = c.iloc.saturating_add(b.payload.len());
|
||||
} else if b.is(b"meta") {
|
||||
// `meta` is a FullBox: version and flags sit between its header and its children.
|
||||
if b.payload.len() >= 4 {
|
||||
depth_boxes(c, BoxIter::new(&b.payload[4..]), depth + 1);
|
||||
}
|
||||
} else if b.is(b"iprp") {
|
||||
depth_boxes(c, b.children(), depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Assembles ftyp + meta(version/flags + iprp(ipco, ipma)) by hand; only the structure
|
||||
/// the census walks matters, no item is ever defined.
|
||||
fn file_with(ipma_payload: &[u8]) -> Vec<u8> {
|
||||
let mut out = b"\0\0\0\x18ftypheic\0\0\0\0mif1heic".to_vec();
|
||||
let mut iprp = Vec::new();
|
||||
iprp.extend(box_of(b"ipco", b""));
|
||||
iprp.extend(box_of(b"ipma", ipma_payload));
|
||||
let mut meta = vec![0u8, 0, 0, 0]; // version 0, flags 0
|
||||
meta.extend(box_of(b"iprp", &iprp));
|
||||
out.extend(box_of(b"meta", &meta));
|
||||
out
|
||||
}
|
||||
|
||||
fn box_of(kind: &[u8; 4], body: &[u8]) -> Vec<u8> {
|
||||
let mut out = Vec::with_capacity(8 + body.len());
|
||||
let size = u32::try_from(8 + body.len()).expect("test box fits in u32");
|
||||
out.extend_from_slice(&size.to_be_bytes());
|
||||
out.extend_from_slice(kind);
|
||||
out.extend_from_slice(body);
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_oversized_ipma_is_refused() {
|
||||
// 40 000 entries of 6 bytes: 240 KiB of ipma, far over the cap, and quadratic if
|
||||
// parsed (each entry rescans the earlier item ids in heic-rs 0.1.4).
|
||||
let mut ipma = Vec::new();
|
||||
for _ in 0..40_000u32 {
|
||||
ipma.extend_from_slice(&[0u8, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
let bytes = file_with(&ipma);
|
||||
let r = crate::probe(&bytes).map(|_| ());
|
||||
assert!(matches!(r, Err(Error::Limit(ref m)) if m.contains("associations")), "{r:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_undersized_ipma_is_untouched_by_the_caps() {
|
||||
// The same shape with a normal ipma (two entries): the caps pass, and whatever error
|
||||
// follows must not come from them.
|
||||
let bytes = file_with(&[0, 0, 4, 1, 1, 2, 0, 0]);
|
||||
let r = crate::probe(&bytes);
|
||||
assert!(!matches!(r, Err(Error::Limit(ref m)) if m.contains("cap")), "{r:?}");
|
||||
}
|
||||
}
|
||||
@@ -1,528 +0,0 @@
|
||||
//! `lightcraft-heif`: the optional HEIF / HEIC (iPhone and Mac photo) decoder.
|
||||
//!
|
||||
//! `heic-rs`, a pure-Rust HEIF container and HEVC still-picture decoder, reads the container and
|
||||
//! reconstructs the coded pictures (bit-exact against libde265 on iPhone photos); this crate turns
|
||||
//! them into RGB itself ([`ycc`]) so the colours match libheif: it honours the HEVC VUI's range
|
||||
//! and matrix ([`vui`]) where heic-rs assumes BT.709 limited range, and upsamples chroma like
|
||||
//! libheif. Single pictures and grid-tiled photos, 8- to 16-bit, monochrome, alpha auxiliary
|
||||
//! images, ICC, EXIF and XMP. The API is plain data ([`Info`], [`Decoded`], [`Error`]) so the
|
||||
//! crate knows nothing about the rest of LightCraft; `lightcraft-codecs` adapts it behind its
|
||||
//! `heif` feature. Shares heic-rs and its regression fixtures with PhotoCraft's `photocraft-heif`.
|
||||
//!
|
||||
//! HEIF records orientation in the container (`irot`/`imir`, plus a `clap` crop), not in EXIF.
|
||||
//! [`Options::apply_transforms`] applies them; with it off, the pixels come back as coded.
|
||||
//! Auxiliary images other than alpha (HDR gain maps, depth maps) are ignored.
|
||||
//!
|
||||
//! Never panics: heic-rs is young, so every call into it runs under `catch_unwind` and a panic
|
||||
//! inside it becomes [`Error::Malformed`].
|
||||
|
||||
#![forbid(unsafe_code)]
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
|
||||
mod census;
|
||||
mod orient;
|
||||
/// Lossless synthetic HEIF files for tests, here and in the crates above (feature `testdata`).
|
||||
#[cfg(any(test, feature = "testdata"))]
|
||||
#[doc(hidden)]
|
||||
pub mod testdata;
|
||||
mod vui;
|
||||
mod ycc;
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use heic_rs::context::Context;
|
||||
use heic_rs::hevc::Frame;
|
||||
use heic_rs::props::ItemProps;
|
||||
use heic_rs::props::colr::Range;
|
||||
|
||||
/// What a HEIF file declares, read from the container alone (no pixel is decoded).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Info {
|
||||
/// Size after the container transforms (rotation, mirror, crop): what a viewer shows.
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
/// Size as coded, before the transforms.
|
||||
pub coded_width: u32,
|
||||
pub coded_height: u32,
|
||||
/// The primary image has an alpha auxiliary image.
|
||||
pub has_alpha: bool,
|
||||
/// Bits per sample as coded (8 for most photos, 10 for HDR-capable ones).
|
||||
pub bit_depth: u8,
|
||||
}
|
||||
|
||||
/// Decode settings.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Options {
|
||||
/// Refuse images with more pixels than this (checked before decoding).
|
||||
pub max_pixels: u64,
|
||||
/// Apply the container's rotation, mirror and crop (`irot`/`imir`/`clap`).
|
||||
pub apply_transforms: bool,
|
||||
}
|
||||
|
||||
impl Default for Options {
|
||||
fn default() -> Self {
|
||||
Options { max_pixels: 1 << 28, apply_transforms: true }
|
||||
}
|
||||
}
|
||||
|
||||
/// How the file describes its colour: the code points of ITU-T H.273, from the `colr` `nclx`
|
||||
/// box, else the HEVC VUI, else the standard defaults (2 = unspecified, limited range).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Colour {
|
||||
pub primaries: u16,
|
||||
pub transfer: u16,
|
||||
pub matrix: u16,
|
||||
pub full_range: bool,
|
||||
}
|
||||
|
||||
/// A decoded image: interleaved RGB or RGBA, 16 bits per sample whatever the coded depth,
|
||||
/// row-major, no padding.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Decoded {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
/// Four channels (RGBA) instead of three (RGB).
|
||||
pub has_alpha: bool,
|
||||
/// Bits per sample as coded.
|
||||
pub bit_depth: u8,
|
||||
pub data: Vec<u16>,
|
||||
pub colour: Colour,
|
||||
/// The primary image's ICC profile.
|
||||
pub icc: Option<Vec<u8>>,
|
||||
/// EXIF as a TIFF structure (without HEIF's offset header). Its Orientation tag only mirrors
|
||||
/// what the container says; it is returned verbatim.
|
||||
pub exif: Option<Vec<u8>>,
|
||||
/// The XMP packet.
|
||||
pub xmp: Option<String>,
|
||||
}
|
||||
|
||||
/// Why a file could not be decoded.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Error {
|
||||
/// A valid file using something this decoder does not handle (image sequences, overlays…).
|
||||
Unsupported(&'static str),
|
||||
/// The image is larger than [`Options::max_pixels`] or holds an oversized box.
|
||||
Limit(String),
|
||||
/// Broken or truncated data (or a decoder bug, reported the same way).
|
||||
Malformed(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Error::Unsupported(m) => write!(f, "unsupported HEIF: {m}"),
|
||||
Error::Limit(m) => write!(f, "limit exceeded: {m}"),
|
||||
Error::Malformed(m) => write!(f, "malformed HEIF: {m}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
const SEQUENCE: &str = "this file holds an image sequence; only HEIF still images can be opened";
|
||||
|
||||
/// heic-rs 0.1.1 reconstructs 4:2:0 and monochrome pictures exactly but fails on the 4:2:2 and
|
||||
/// 4:4:4 ones some cameras write (Canon and Sony "HIF"); those failures are reported as this.
|
||||
const CHROMA_422_444: &str = "4:2:2 and 4:4:4 HEIF images (as some Canon and Sony cameras write) can't be decoded yet";
|
||||
|
||||
pub(crate) fn err(e: heic_rs::Error) -> Error {
|
||||
match e {
|
||||
heic_rs::Error::Unsupported(what) => Error::Unsupported(what),
|
||||
// The primary item is neither an HEVC picture nor a grid of them.
|
||||
heic_rs::Error::MissingBox("hvcC") => Error::Unsupported("the image is an overlay, an identity derivation or not HEVC-coded"),
|
||||
heic_rs::Error::PixelLimit { .. } | heic_rs::Error::BoxTooLarge { .. } => Error::Limit(e.to_string()),
|
||||
e => Error::Malformed(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs `f`, turning a heic-rs panic into an error. Fuzzing in the sibling app found two
|
||||
/// out-of-range slices in heic-rs 0.1.1 on malformed files: in its box parser (`boxes.rs:130`)
|
||||
/// and during reconstruction (`hevc/decode/recon.rs:70`).
|
||||
fn guarded<T>(f: impl FnOnce() -> Result<T, Error>) -> Result<T, Error> {
|
||||
std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).unwrap_or_else(|_| Err(Error::Malformed("the HEIF decoder failed on this file".into())))
|
||||
}
|
||||
|
||||
/// Reads the container's declared size, depth and alpha without decoding pixels, so callers can
|
||||
/// check their limits first. Also refuses a file cut short: every coded picture of the primary
|
||||
/// image must lie within `bytes`.
|
||||
pub fn probe(bytes: &[u8]) -> Result<Info, Error> {
|
||||
guarded(|| probe_unguarded(bytes))
|
||||
}
|
||||
|
||||
fn probe_unguarded(bytes: &[u8]) -> Result<Info, Error> {
|
||||
census::cap(bytes)?;
|
||||
// No `meta` box: no still image, only a `moov` image sequence (a video track).
|
||||
let open_error = |e| match e {
|
||||
heic_rs::Error::MissingBox("meta") => Error::Unsupported(SEQUENCE),
|
||||
e => err(e),
|
||||
};
|
||||
let ctx = Context::open(bytes).map_err(open_error)?;
|
||||
let id = ctx.meta.primary;
|
||||
// heic-rs 0.1.4 caps the size of what it assembles, not the work: an item built from other
|
||||
// items (`iloc` construction method 2) is rebuilt once per extent, so a chain of such items
|
||||
// multiplies at every level while each result stays small. Count those visits from the
|
||||
// `iloc` table before reconstructing anything: the primary item first (`heic_rs::probe` and
|
||||
// `Context::grid` both rebuild its descriptor), then its alpha plane, then every tile
|
||||
// reference, repeats included.
|
||||
let mut visits = 0_usize;
|
||||
item_visits(&ctx, id, 0, &mut visits)?;
|
||||
let info = heic_rs::probe(bytes).map_err(open_error)?;
|
||||
if let Some(alpha) = ctx.alpha_item(id).map_err(err)? {
|
||||
item_visits(&ctx, alpha, 0, &mut visits)?;
|
||||
}
|
||||
let mut coded = match ctx.grid(id).map_err(err)? {
|
||||
Some((_, tiles)) => tiles,
|
||||
None => vec![id],
|
||||
};
|
||||
for &item in coded.iter().filter(|&&item| item != id) {
|
||||
item_visits(&ctx, item, 0, &mut visits)?;
|
||||
}
|
||||
// A `dimg` listing may name the same tile more than once, and reconstructable items are
|
||||
// moreover rebuilt for every extent that references them in heic-rs 0.1.4. For the size
|
||||
// check each unique item counts once: the total reconstruction (all of it assembled from
|
||||
// `bytes` by heic-rs) is held below one copy of the file, above which a repeat or chain
|
||||
// wants more bytes than exist. Order doesn't matter here: this only checks that every
|
||||
// tile's bytes lie in the file.
|
||||
coded.sort_unstable();
|
||||
coded.dedup();
|
||||
let mut reconstructed = 0_usize;
|
||||
for item in coded {
|
||||
reconstructed = reconstructed.saturating_add(ctx.item_data(item).map_err(err)?.len());
|
||||
if reconstructed > bytes.len() {
|
||||
return Err(Error::Limit(format!(
|
||||
"the container holds {reconstructed} bytes of item data, over the {} bytes of the file itself",
|
||||
bytes.len()
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(Info {
|
||||
width: info.width,
|
||||
height: info.height,
|
||||
coded_width: info.coded_width,
|
||||
coded_height: info.coded_height,
|
||||
has_alpha: info.has_alpha,
|
||||
bit_depth: info.bit_depth,
|
||||
})
|
||||
}
|
||||
|
||||
/// Most extent visits heic-rs may spend reconstructing the primary image's coded items; real
|
||||
/// files need one per tile (plus a few for the rare derived item).
|
||||
const MAX_ITEM_VISITS: usize = 4096;
|
||||
/// Deepest chain of items built from items we follow (heic-rs refuses beyond eight itself).
|
||||
const MAX_ITEM_CHAIN: u32 = 8;
|
||||
|
||||
/// Adds to `visits` the extents heic-rs would read to reconstruct `id`, following items built
|
||||
/// from other items, and fails once that passes [`MAX_ITEM_VISITS`] or the chain is too deep.
|
||||
fn item_visits(ctx: &Context<'_>, id: u32, depth: u32, visits: &mut usize) -> Result<(), Error> {
|
||||
use heic_rs::meta::iloc::Construction;
|
||||
if depth > MAX_ITEM_CHAIN {
|
||||
return Err(Error::Limit(format!("items built from items nest deeper than {MAX_ITEM_CHAIN} levels")));
|
||||
}
|
||||
let Some(loc) = ctx.meta.location(id) else { return Ok(()) };
|
||||
*visits = visits.saturating_add(loc.extents.len().max(1));
|
||||
if *visits > MAX_ITEM_VISITS {
|
||||
return Err(Error::Limit(format!("reconstructing the image takes over {MAX_ITEM_VISITS} item extent reads")));
|
||||
}
|
||||
if loc.construction == Construction::Item {
|
||||
for e in &loc.extents {
|
||||
if let Ok(source) = u32::try_from(e.index)
|
||||
&& source != id
|
||||
{
|
||||
item_visits(ctx, source, depth + 1, visits)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Decodes the primary image, with its metadata.
|
||||
pub fn decode(bytes: &[u8], options: &Options) -> Result<Decoded, Error> {
|
||||
guarded(|| decode_unguarded(bytes, options))
|
||||
}
|
||||
|
||||
/// Decodes the smallest thumbnail image the file carries whose long edge is at least
|
||||
/// `min_edge`, if any (iPhones store one of about 320 × 240). Transforms are applied as for the
|
||||
/// primary image; the metadata (and, when the thumbnail has no profile of its own, the ICC
|
||||
/// profile) are the primary image's.
|
||||
pub fn decode_thumbnail(bytes: &[u8], min_edge: u32, options: &Options) -> Result<Option<Decoded>, Error> {
|
||||
guarded(|| {
|
||||
let ctx = Context::open(bytes).map_err(err)?;
|
||||
let primary = ctx.meta.primary;
|
||||
let mut best: Option<(u32, u32)> = None;
|
||||
// A thumbnail whose own reconstruction would cost too much is not a candidate; the visits
|
||||
// of all the candidates share one budget.
|
||||
let mut candidate_visits = 0_usize;
|
||||
for r in ctx.meta.refs.iter().filter(|r| r.kind == heic_rs::meta::iref::RefKind::Thmb && r.to.contains(&primary)) {
|
||||
if item_visits(&ctx, r.from, 0, &mut candidate_visits).is_err() {
|
||||
continue;
|
||||
}
|
||||
let Ok(p) = ctx.props(r.from) else { continue };
|
||||
let Ok((w, h)) = ctx.coded_size(r.from, &p) else { continue };
|
||||
let edge = w.max(h);
|
||||
if edge >= min_edge && best.is_none_or(|(_, e)| edge < e) {
|
||||
best = Some((r.from, edge));
|
||||
}
|
||||
}
|
||||
let Some((id, _)) = best else { return Ok(None) };
|
||||
// The same pre-scan as `probe`: the thumbnail's own descriptor and every tile reference
|
||||
// are counted before heic-rs rebuilds any of them.
|
||||
let mut visits = 0_usize;
|
||||
item_visits(&ctx, id, 0, &mut visits)?;
|
||||
if let Some((_, tiles)) = ctx.grid(id).map_err(err)? {
|
||||
for &tile in &tiles {
|
||||
item_visits(&ctx, tile, 0, &mut visits)?;
|
||||
}
|
||||
}
|
||||
let mut decoded = decode_item(&ctx, id, options, false)?;
|
||||
if decoded.icc.is_none() {
|
||||
decoded.icc = item_icc(&ctx, primary);
|
||||
}
|
||||
(decoded.exif, decoded.xmp) = metadata(&ctx);
|
||||
Ok(Some(decoded))
|
||||
})
|
||||
}
|
||||
|
||||
/// An item's ICC profile; a grid whose own properties carry none takes its first tile's (as on
|
||||
/// some iPhones).
|
||||
fn item_icc(ctx: &Context<'_>, id: u32) -> Option<Vec<u8>> {
|
||||
let p = ctx.props(id).ok()?;
|
||||
let icc = p.icc.or_else(|| {
|
||||
let (_, tiles) = ctx.grid(id).ok()??;
|
||||
ctx.props(*tiles.first()?).ok()?.icc
|
||||
});
|
||||
icc.map(<[u8]>::to_vec)
|
||||
}
|
||||
|
||||
fn decode_unguarded(bytes: &[u8], options: &Options) -> Result<Decoded, Error> {
|
||||
// Refuses sequences with a clear message before anything else is read.
|
||||
probe_unguarded(bytes)?;
|
||||
let ctx = Context::open(bytes).map_err(err)?;
|
||||
let mut decoded = decode_item(&ctx, ctx.meta.primary, options, true)?;
|
||||
let (exif, xmp) = metadata(&ctx);
|
||||
decoded.exif = exif;
|
||||
decoded.xmp = xmp;
|
||||
Ok(decoded)
|
||||
}
|
||||
|
||||
/// Decodes one image item (a coded picture or a grid of them) to RGB(A), alpha included when
|
||||
/// `with_alpha` and the item has an alpha auxiliary image.
|
||||
fn decode_item(ctx: &Context<'_>, id: u32, options: &Options, with_alpha: bool) -> Result<Decoded, Error> {
|
||||
let p = ctx.props(id).map_err(err)?;
|
||||
// heic-rs 0.1.4 keeps every associated transform and its [`orient::apply`] allocates the
|
||||
// full picture again for each: a file could chain the repetition and multiply decode CPU
|
||||
// without changing any checked size. Real files carry at most one `clap`, `irot` and
|
||||
// `imir`, so refuse anything approaching [`MAX_TRANSFORMS`] (CodeRabbit #44).
|
||||
if p.transforms.len() > MAX_TRANSFORMS {
|
||||
return Err(Error::Limit(format!("the image carries {} transform properties, over the {MAX_TRANSFORMS}-property cap", p.transforms.len())));
|
||||
}
|
||||
let limit = options.max_pixels;
|
||||
// The declared sizes are refused before anything is decoded or allocated.
|
||||
let (cw, ch) = ctx.coded_size(id, &p).map_err(err)?;
|
||||
heic_rs::image::check_pixels(cw, ch, limit).map_err(err)?;
|
||||
let (tw, th) = heic_rs::transform::transformed_size(cw, ch, &p.transforms).map_err(err)?;
|
||||
heic_rs::image::check_pixels(tw, th, limit).map_err(err)?;
|
||||
|
||||
let coded = Coded::decode(ctx, id, &p, limit)?;
|
||||
let planes = coded.planes()?;
|
||||
let colour = colour(&p, &coded.first_props);
|
||||
let signal = ycc::Signal { matrix: colour.matrix, full_range: colour.full_range };
|
||||
let alpha_id = if with_alpha { ctx.alpha_item(id).map_err(err)? } else { None };
|
||||
let (w, h) = planes.size();
|
||||
// A grid's canvas dimensions come from its payload, not the checked `ispe` (CodeRabbit
|
||||
// #48): check the real one, before [`ycc::to_rgb16`] allocates for it.
|
||||
match (u32::try_from(w), u32::try_from(h)) {
|
||||
(Ok(wu), Ok(hu)) => {
|
||||
heic_rs::image::check_pixels(wu, hu, limit).map_err(err)?;
|
||||
}
|
||||
_ => return Err(Error::Limit(format!("the image canvas is {w}x{h}, beyond the {limit}-pixel limit"))),
|
||||
}
|
||||
let mode = if options.apply_transforms && orient::odd_crop(w, h, &p.transforms)? { ycc::Upsampling::Bilinear } else { ycc::Upsampling::Nearest };
|
||||
let mut data = ycc::to_rgb16(&planes, signal, alpha_id.is_some(), mode)
|
||||
.ok_or_else(|| Error::Malformed("the decoded pictures don't cover the image".into()))?;
|
||||
if let Some(aux) = alpha_id {
|
||||
let ap = ctx.props(aux).map_err(err)?;
|
||||
let alpha = Coded::decode(ctx, aux, &ap, limit)?;
|
||||
let alpha_planes = alpha.planes()?;
|
||||
if alpha_planes.size() != (w, h) {
|
||||
return Err(Error::Unsupported("an alpha image of a different size than the picture"));
|
||||
}
|
||||
ycc::put_alpha(&alpha_planes, &mut data).ok_or_else(|| Error::Malformed("the alpha image is incomplete".into()))?;
|
||||
}
|
||||
let bit_depth = planes.bit_depth();
|
||||
let has_alpha = alpha_id.is_some();
|
||||
let mut img = orient::Pixels { width: w, height: h, ch: if has_alpha { 4 } else { 3 }, data };
|
||||
if options.apply_transforms {
|
||||
img = orient::apply(img, &p.transforms)?;
|
||||
}
|
||||
Ok(Decoded {
|
||||
width: u32::try_from(img.width).map_err(|_| Error::Limit("image too wide".into()))?,
|
||||
height: u32::try_from(img.height).map_err(|_| Error::Limit("image too tall".into()))?,
|
||||
has_alpha,
|
||||
bit_depth,
|
||||
data: img.data,
|
||||
colour,
|
||||
// A grid whose own properties carry no profile takes its tiles' (as on some iPhones).
|
||||
icc: p.icc.or(coded.first_props.icc).map(<[u8]>::to_vec),
|
||||
exif: None,
|
||||
xmp: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// How many times `max_pixels` the coded pictures of one item may hold together: a grid's tiles
|
||||
/// cover its canvas rounded up to whole tiles, so legitimate grids sit near 1.
|
||||
const GRID_BUDGET: u64 = 2;
|
||||
|
||||
/// The most transform properties (`clap`/`irot`/`imir`) one item may carry. Real files have at
|
||||
/// most one of each, so anything approaching this is a reordered or hostile association list.
|
||||
const MAX_TRANSFORMS: usize = 12;
|
||||
|
||||
/// The decoded pictures of an item: one, or a grid's tiles in `dimg` order.
|
||||
struct Coded<'a> {
|
||||
frames: Vec<Frame>,
|
||||
grid: Option<heic_rs::grid::Grid>,
|
||||
/// The properties of the first coded picture (the item itself, or the first tile): where the
|
||||
/// HEVC configuration and its VUI live.
|
||||
first_props: ItemProps<'a>,
|
||||
/// A failed 4:2:2 / 4:4:4 picture, for the error message.
|
||||
chroma_format: u8,
|
||||
}
|
||||
|
||||
impl<'a> Coded<'a> {
|
||||
fn decode(ctx: &Context<'a>, id: u32, p: &ItemProps<'a>, limit: u64) -> Result<Coded<'a>, Error> {
|
||||
let (grid, ids) = match ctx.grid(id).map_err(err)? {
|
||||
Some((g, tiles)) => (Some(g), tiles),
|
||||
None => (None, vec![id]),
|
||||
};
|
||||
let first = *ids.first().ok_or_else(|| Error::Malformed("grid lists no tiles".into()))?;
|
||||
let first_props = if first == id { p.clone() } else { ctx.props(first).map_err(err)? };
|
||||
let chroma_format = first_props.hvcc.as_ref().map_or(1, |c| c.chroma_format);
|
||||
// #1518: the decoder allocates each picture at its SPS's uncropped size, which a
|
||||
// conformance window (or a slice picking another SPS) can make far larger than the
|
||||
// declared one, and a grid holds all its tiles at once. Both are refused before decoding.
|
||||
let mut total = 0u64;
|
||||
for t in &ids {
|
||||
let tp = ctx.props(*t).map_err(err)?;
|
||||
let hvcc = tp.hvcc.as_ref().ok_or(heic_rs::Error::MissingBox("hvcC")).map_err(err)?;
|
||||
let sizes = vui::coded_sizes(&hvcc.parameter_sets()).ok_or_else(|| Error::Malformed("unreadable sequence parameter set".into()))?;
|
||||
let mut largest = 0u64;
|
||||
for (w, h) in sizes {
|
||||
heic_rs::image::check_pixels(w, h, limit).map_err(err)?;
|
||||
largest = largest.max(u64::from(w) * u64::from(h));
|
||||
}
|
||||
total = total.saturating_add(largest);
|
||||
}
|
||||
if total > limit.saturating_mul(GRID_BUDGET) {
|
||||
return Err(Error::Limit(format!("the coded pictures hold {total} pixels, over the {limit}-pixel limit")));
|
||||
}
|
||||
let decode_one = |t: &u32| -> Result<Frame, Error> {
|
||||
let tp = ctx.props(*t).map_err(err)?;
|
||||
let hvcc = tp.hvcc.as_ref().ok_or(heic_rs::Error::MissingBox("hvcC")).map_err(err)?;
|
||||
let data = ctx.item_data(*t).map_err(err)?;
|
||||
let nals = hvcc.split_nals(&data).map_err(err)?;
|
||||
let params = hvcc.parameter_sets();
|
||||
if params.is_empty() {
|
||||
return Err(Error::Malformed("hvcC carries no parameter sets".into()));
|
||||
}
|
||||
let frame = heic_rs::hevc::decode_still(¶ms, &nals).map_err(err)?;
|
||||
frame.validate().map_err(err)?;
|
||||
heic_rs::image::check_pixels(frame.width, frame.height, limit).map_err(err)?;
|
||||
Ok(frame)
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let frames: Result<Vec<Frame>, Error> = {
|
||||
use rayon::prelude::*;
|
||||
ids.par_iter().map(decode_one).collect()
|
||||
};
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let frames: Result<Vec<Frame>, Error> = ids.iter().map(decode_one).collect();
|
||||
let frames = frames.map_err(|e| match e {
|
||||
Error::Malformed(_) if chroma_format >= 2 => Error::Unsupported(CHROMA_422_444),
|
||||
e => e,
|
||||
})?;
|
||||
Ok(Coded { frames, grid, first_props, chroma_format })
|
||||
}
|
||||
|
||||
fn planes(&self) -> Result<ycc::Planes<'_>, Error> {
|
||||
match (&self.grid, self.frames.as_slice()) {
|
||||
(Some(g), tiles) => heic_rs::grid::Mosaic::new(g, tiles).map(ycc::Planes::Grid).map_err(err),
|
||||
(None, [frame]) => Ok(ycc::Planes::Picture(frame)),
|
||||
(None, _) => Err(Error::Malformed(format!("expected one coded picture (chroma format {})", self.chroma_format))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The colour description in force: `nclx` on the item (or, for a grid without one, on its
|
||||
/// first tile), else the VUI of its (first) coded picture, else the H.265 defaults.
|
||||
fn colour(p: &ItemProps<'_>, coded: &ItemProps<'_>) -> Colour {
|
||||
if let Some(n) = p.nclx.or(coded.nclx) {
|
||||
return Colour { primaries: n.primaries, transfer: n.transfer, matrix: n.matrix_code, full_range: n.range == Range::Full };
|
||||
}
|
||||
let s = coded.hvcc.as_ref().and_then(|c| vui::signal(&c.parameter_sets())).unwrap_or_default();
|
||||
Colour { primaries: s.primaries.into(), transfer: s.transfer.into(), matrix: s.matrix.into(), full_range: s.full_range }
|
||||
}
|
||||
|
||||
/// The primary image's EXIF (TIFF structure) and XMP packet. Metadata is a courtesy: a broken
|
||||
/// metadata item never fails a decode whose pixels came out fine.
|
||||
fn metadata(ctx: &Context<'_>) -> (Option<Vec<u8>>, Option<String>) {
|
||||
let id = ctx.meta.primary;
|
||||
// Metadata is best effort, so an item that would cost too much to rebuild is skipped, not an error.
|
||||
let mut visits = 0_usize;
|
||||
let cheap = |item: Option<u32>, visits: &mut usize| item.is_none_or(|item| item_visits(ctx, item, 0, visits).is_ok());
|
||||
let exif = if cheap(ctx.exif_item(id), &mut visits) { ctx.exif(id).ok().flatten().map(<[u8]>::to_vec) } else { None };
|
||||
let xmp_item = ctx.xmp_item(id).filter(|&x| cheap(Some(x), &mut visits));
|
||||
let xmp = xmp_item.and_then(|x| ctx.item_data(x).ok()).and_then(|d| match d {
|
||||
Cow::Borrowed(b) => String::from_utf8(b.to_vec()).ok(),
|
||||
Cow::Owned(b) => String::from_utf8(b).ok(),
|
||||
});
|
||||
(exif, xmp.map(|x| x.trim_end_matches('\0').to_string()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests_synthetic;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Found by PhotoCraft's `decode_heif` fuzz target: a malformed box makes heic-rs 0.1.1 slice
|
||||
/// out of range (`boxes.rs:130`). It must be an error, not a crash.
|
||||
const HEIC_RS_BOX_PANIC: [u8; 72] = [
|
||||
0x00, 0x00, 0x00, 0x24, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x69, 0x66, 0x31, 0x4d, 0x69, 0x50,
|
||||
0x72, 0x6d, 0x69, 0x61, 0x66, 0x4d, 0x69, 0x48, 0x42, 0x72, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x65, 0x74, 0x61, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x75, 0x75, 0x69, 0x64, 0x00, 0x00, 0x00, 0x00, 0x03, 0x08, 0x08, 0x08, 0x00, 0x03, 0x01, 0x03, 0x70,
|
||||
0x00, 0xa8, 0x00,
|
||||
];
|
||||
|
||||
#[test]
|
||||
fn a_heic_rs_panic_is_a_malformed_error() {
|
||||
for apply_transforms in [true, false] {
|
||||
let r = decode(&HEIC_RS_BOX_PANIC, &Options { apply_transforms, ..Default::default() });
|
||||
assert!(matches!(r, Err(Error::Malformed(_))), "{r:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_sequence_is_unsupported_with_a_clear_message() {
|
||||
// An ftyp and nothing else: a HEIF image sequence would have a moov here instead of a meta.
|
||||
let bytes = b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc";
|
||||
for r in [probe(bytes).map(|_| ()), decode(bytes, &Options::default()).map(|_| ())] {
|
||||
assert!(matches!(&r, Err(Error::Unsupported(m)) if m.contains("sequence")), "{r:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn garbage_is_an_error() {
|
||||
for bytes in [&b""[..], b"\0\0\0\x18ftypheic\0\0\0\0mif1heic", &[0xFF; 64]] {
|
||||
assert!(probe(bytes).is_err());
|
||||
assert!(decode(bytes, &Options::default()).is_err());
|
||||
assert!(decode_thumbnail(bytes, 1, &Options::default()).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_display_their_kind() {
|
||||
assert!(Error::Unsupported("x").to_string().contains("unsupported"));
|
||||
assert!(Error::Limit("x".into()).to_string().contains("limit"));
|
||||
assert!(Error::Malformed("x".into()).to_string().contains("malformed"));
|
||||
}
|
||||
}
|
||||
@@ -1,178 +0,0 @@
|
||||
//! The container's transformative properties (`clap` crop, `irot` rotation, `imir` mirror),
|
||||
//! applied to interleaved 16-bit pixels in the order the file associates them.
|
||||
//!
|
||||
//! `imir` follows libheif, which writes and reads `axis = 0` as a flip about the horizontal axis
|
||||
//! (top and bottom exchanged) and `axis = 1` as a left-right flip. heic-rs 0.1.1 names the coded
|
||||
//! values the other way round (`Mirror::LeftRight` is `axis = 0`), so they are swapped here.
|
||||
|
||||
use heic_rs::props::Transform;
|
||||
use heic_rs::props::simple::{Clap, Mirror, Rotation};
|
||||
|
||||
use crate::Error;
|
||||
|
||||
/// Interleaved samples, `ch` per pixel, row-major.
|
||||
pub struct Pixels {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub ch: usize,
|
||||
pub data: Vec<u16>,
|
||||
}
|
||||
|
||||
impl Pixels {
|
||||
fn remap(&self, width: usize, height: usize, src: impl Fn(usize, usize) -> (usize, usize)) -> Pixels {
|
||||
let ch = self.ch;
|
||||
let mut data = vec![0u16; width * height * ch];
|
||||
for (y, row) in data.chunks_exact_mut(width * ch).enumerate() {
|
||||
for (x, px) in row.chunks_exact_mut(ch).enumerate() {
|
||||
let (sx, sy) = src(x, y);
|
||||
let i = (sy * self.width + sx) * ch;
|
||||
if let Some(s) = self.data.get(i..i + ch) {
|
||||
px.copy_from_slice(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
Pixels { width, height, ch, data }
|
||||
}
|
||||
}
|
||||
|
||||
/// The rectangle `(left, top, width, height)` a `clap` selects from a `w` × `h` image. Its centre
|
||||
/// is `((w - 1) / 2 + horizOff, (h - 1) / 2 + vertOff)`; a corner that falls between two pixels
|
||||
/// is rounded as libheif does: left down, top up.
|
||||
pub fn crop_rect(w: usize, h: usize, c: Clap) -> Result<(usize, usize, usize, usize), Error> {
|
||||
let bad = || Error::Malformed("clap selects an impossible rectangle".into());
|
||||
let size = |(n, d): (u32, u32)| (d != 0).then(|| n / d).ok_or_else(bad);
|
||||
let (cw, ch) = (size(c.width)? as usize, size(c.height)? as usize);
|
||||
if cw == 0 || ch == 0 || cw > w || ch > h {
|
||||
return Err(bad());
|
||||
}
|
||||
// Twice the corner, so the half pixels of odd sizes stay exact.
|
||||
let twice = |full: usize, cropped: usize, (n, d): (i32, u32)| -> Result<i64, Error> {
|
||||
if d == 0 {
|
||||
return Err(bad());
|
||||
}
|
||||
Ok((full - cropped) as i64 + (2 * i64::from(n)).div_euclid(i64::from(d)))
|
||||
};
|
||||
let left = twice(w, cw, c.horiz_off)?.div_euclid(2);
|
||||
let top = (twice(h, ch, c.vert_off)? + 1).div_euclid(2);
|
||||
let left = usize::try_from(left).map_err(|_| bad())?;
|
||||
let top = usize::try_from(top).map_err(|_| bad())?;
|
||||
if left + cw > w || top + ch > h {
|
||||
return Err(bad());
|
||||
}
|
||||
Ok((left, top, cw, ch))
|
||||
}
|
||||
|
||||
/// Whether a `clap` among `transforms` (applied to a `w` × `h` image) starts at an odd column
|
||||
/// or row. libheif crops such images after upsampling their chroma bilinearly instead of with
|
||||
/// its default nearest neighbour; [`crate::ycc`] follows it so the pixels match.
|
||||
pub fn odd_crop(mut w: usize, mut h: usize, transforms: &[Transform]) -> Result<bool, Error> {
|
||||
for t in transforms {
|
||||
match *t {
|
||||
Transform::Crop(c) => {
|
||||
let (left, top, cw, ch) = crop_rect(w, h, c)?;
|
||||
if left % 2 == 1 || top % 2 == 1 {
|
||||
return Ok(true);
|
||||
}
|
||||
(w, h) = (cw, ch);
|
||||
}
|
||||
Transform::Rotate(Rotation::Ccw90 | Rotation::Ccw270) => (w, h) = (h, w),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
pub fn apply(mut img: Pixels, transforms: &[Transform]) -> Result<Pixels, Error> {
|
||||
for t in transforms {
|
||||
let (w, h) = (img.width, img.height);
|
||||
img = match *t {
|
||||
Transform::Crop(c) => {
|
||||
let (left, top, cw, ch) = crop_rect(w, h, c)?;
|
||||
img.remap(cw, ch, |x, y| (x + left, y + top))
|
||||
}
|
||||
// Counter-clockwise: the right column becomes the top row.
|
||||
Transform::Rotate(Rotation::Ccw90) => img.remap(h, w, |x, y| (w - 1 - y, x)),
|
||||
Transform::Rotate(Rotation::Ccw180) => img.remap(w, h, |x, y| (w - 1 - x, h - 1 - y)),
|
||||
Transform::Rotate(Rotation::Ccw270) => img.remap(h, w, |x, y| (y, h - 1 - x)),
|
||||
Transform::Rotate(Rotation::None) => img,
|
||||
// `axis = 0` (heic-rs's `LeftRight`): top and bottom exchanged, as libheif reads it.
|
||||
Transform::Mirror(Mirror::LeftRight) => img.remap(w, h, |x, y| (x, h - 1 - y)),
|
||||
// `axis = 1`: left and right exchanged.
|
||||
Transform::Mirror(Mirror::TopBottom) => img.remap(w, h, |x, y| (w - 1 - x, y)),
|
||||
};
|
||||
}
|
||||
Ok(img)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// 3×2, one channel: 1 2 3 / 4 5 6.
|
||||
fn img() -> Pixels {
|
||||
Pixels { width: 3, height: 2, ch: 1, data: vec![1, 2, 3, 4, 5, 6] }
|
||||
}
|
||||
|
||||
fn run(t: Transform) -> (usize, usize, Vec<u16>) {
|
||||
let p = apply(img(), &[t]).unwrap();
|
||||
(p.width, p.height, p.data)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotations_are_counter_clockwise() {
|
||||
assert_eq!(run(Transform::Rotate(Rotation::Ccw90)), (2, 3, vec![3, 6, 2, 5, 1, 4]));
|
||||
assert_eq!(run(Transform::Rotate(Rotation::Ccw180)), (3, 2, vec![6, 5, 4, 3, 2, 1]));
|
||||
assert_eq!(run(Transform::Rotate(Rotation::Ccw270)), (2, 3, vec![4, 1, 5, 2, 6, 3]));
|
||||
assert_eq!(run(Transform::Rotate(Rotation::None)), (3, 2, vec![1, 2, 3, 4, 5, 6]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirrors_follow_libheif() {
|
||||
// axis 0 (heic-rs `LeftRight`) exchanges top and bottom; axis 1 left and right.
|
||||
assert_eq!(run(Transform::Mirror(Mirror::LeftRight)), (3, 2, vec![4, 5, 6, 1, 2, 3]));
|
||||
assert_eq!(run(Transform::Mirror(Mirror::TopBottom)), (3, 2, vec![3, 2, 1, 6, 5, 4]));
|
||||
}
|
||||
|
||||
fn clap(w: u32, h: u32, ho: i32, vo: i32) -> Clap {
|
||||
Clap { width: (w, 1), height: (h, 1), horiz_off: (ho, 1), vert_off: (vo, 1) }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clap_corners_round_like_libheif() {
|
||||
// Measured against libheif 1.23 on a 512 × 512 picture.
|
||||
assert_eq!(crop_rect(512, 512, clap(301, 201, 0, 0)).unwrap(), (105, 156, 301, 201));
|
||||
assert_eq!(crop_rect(512, 512, clap(300, 200, 0, 0)).unwrap(), (106, 156, 300, 200));
|
||||
assert_eq!(crop_rect(512, 512, clap(301, 201, 37, -21)).unwrap(), (142, 135, 301, 201));
|
||||
assert_eq!(crop_rect(512, 512, clap(301, 201, -3, 5)).unwrap(), (102, 161, 301, 201));
|
||||
assert_eq!(crop_rect(512, 512, clap(300, 201, 0, 0)).unwrap(), (106, 156, 300, 201));
|
||||
assert_eq!(crop_rect(1500, 1000, clap(1001, 701, 37, -21)).unwrap(), (286, 129, 1001, 701));
|
||||
let p = apply(img(), &[Transform::Crop(clap(2, 1, 1, 0))]).unwrap();
|
||||
// The half-pixel top rounds down a row.
|
||||
assert_eq!((p.width, p.height, p.data), (2, 1, vec![5, 6]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn impossible_claps_are_errors() {
|
||||
for c in [
|
||||
clap(0, 1, 0, 0),
|
||||
clap(4, 1, 0, 0),
|
||||
clap(3, 2, 1, 0),
|
||||
clap(3, 2, 0, -1),
|
||||
Clap { width: (1, 0), ..clap(1, 1, 0, 0) },
|
||||
Clap { horiz_off: (0, 0), ..clap(1, 1, 0, 0) },
|
||||
] {
|
||||
assert!(crop_rect(3, 2, c).is_err(), "{c:?}");
|
||||
}
|
||||
assert!(apply(img(), &[Transform::Crop(clap(9, 9, 0, 0))]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn odd_crops_are_detected_through_rotations() {
|
||||
assert!(!odd_crop(512, 512, &[Transform::Crop(clap(300, 200, 0, 0))]).unwrap());
|
||||
assert!(odd_crop(512, 512, &[Transform::Crop(clap(301, 200, 0, 0))]).unwrap());
|
||||
// After a quarter turn the crop applies to a 200 × 300 image.
|
||||
let t = [Transform::Crop(clap(300, 200, 0, 0)), Transform::Rotate(Rotation::Ccw90), Transform::Crop(clap(197, 300, 0, 0))];
|
||||
assert!(odd_crop(512, 512, &t).unwrap());
|
||||
assert!(!odd_crop(512, 512, &[Transform::Rotate(Rotation::Ccw90)]).unwrap());
|
||||
}
|
||||
}
|
||||
@@ -1,542 +0,0 @@
|
||||
//! Synthetic HEIF files for tests (feature `testdata`): lossless, so every decoded sample is known.
|
||||
//!
|
||||
//! Each coded picture is one 32 × 32 coding tree unit stored as PCM samples (H.265 7.3.8.7):
|
||||
//! the bitstream carries the YCbCr values verbatim, so the decoder's HEVC stage reproduces them
|
||||
//! exactly and a test checks everything after it (grid composition, colour, transforms,
|
||||
//! metadata) against values it chose. The few CABAC bins around the PCM data are written with a
|
||||
//! minimal arithmetic encoder (9.3.4.4). Larger images are grids of such pictures, which is how
|
||||
//! iPhones store photos too (with 512 × 512 tiles).
|
||||
//!
|
||||
//! Not a general HEIF or HEVC writer: 4:2:0 or monochrome, 8 or 10 bits, a CTU per picture.
|
||||
|
||||
/// Side of every coded picture.
|
||||
pub const TILE: u32 = 32;
|
||||
|
||||
/// YCbCr samples at a luma position (chroma is taken at even positions, 4:2:0).
|
||||
pub type Sampler<'a> = &'a dyn Fn(u32, u32) -> [u16; 3];
|
||||
|
||||
/// A property of the primary image.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Prop {
|
||||
/// `colr` of type `prof`.
|
||||
Icc(Vec<u8>),
|
||||
/// `colr` of type `nclx`: primaries, transfer, matrix, full range.
|
||||
Nclx(u16, u16, u16, bool),
|
||||
/// `irot`: quarter turns counter-clockwise.
|
||||
Irot(u8),
|
||||
/// `imir` with this axis.
|
||||
Imir(u8),
|
||||
/// `clap`: width, height, horizontal and vertical offset (whole pixels).
|
||||
Clap(u32, u32, i32, i32),
|
||||
}
|
||||
|
||||
/// What [`build`] writes.
|
||||
pub struct Spec<'a> {
|
||||
/// Displayed (grid output) size, before transforms.
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub bit_depth: u8,
|
||||
pub sample: Sampler<'a>,
|
||||
/// Properties of the primary image, in association order.
|
||||
pub props: Vec<Prop>,
|
||||
/// EXIF as a TIFF structure (written with HEIF's 4-byte offset header).
|
||||
pub exif: Option<Vec<u8>>,
|
||||
/// An alpha auxiliary image (monochrome, full range).
|
||||
pub alpha: Option<&'a dyn Fn(u32, u32) -> u16>,
|
||||
/// A thumbnail item of half the size with these samples (in its own coordinates).
|
||||
pub thumbnail: Option<Sampler<'a>>,
|
||||
/// Auxiliary images of these types the decoder must ignore (HDR gain map, depth…).
|
||||
pub ignored_aux: Vec<&'static str>,
|
||||
}
|
||||
|
||||
impl<'a> Spec<'a> {
|
||||
pub fn new(width: u32, height: u32, sample: Sampler<'a>) -> Spec<'a> {
|
||||
Spec { width, height, bit_depth: 8, sample, props: Vec::new(), exif: None, alpha: None, thumbnail: None, ignored_aux: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Bits {
|
||||
bytes: Vec<u8>,
|
||||
n: usize,
|
||||
}
|
||||
|
||||
impl Bits {
|
||||
fn put(&mut self, v: u32, len: u32) {
|
||||
for i in (0..len).rev() {
|
||||
if self.n.is_multiple_of(8) {
|
||||
self.bytes.push(0);
|
||||
}
|
||||
if (v >> i) & 1 == 1
|
||||
&& let Some(b) = self.bytes.last_mut()
|
||||
{
|
||||
*b |= 0x80 >> (self.n % 8);
|
||||
}
|
||||
self.n += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn ue(&mut self, v: u32) {
|
||||
let x = u64::from(v) + 1;
|
||||
let len = 64 - x.leading_zeros();
|
||||
self.put(0, len - 1);
|
||||
self.put((x & 0xffff_ffff) as u32, len);
|
||||
}
|
||||
|
||||
fn align_zero(&mut self) {
|
||||
while !self.n.is_multiple_of(8) {
|
||||
self.put(0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn trailing(&mut self) {
|
||||
self.put(1, 1);
|
||||
self.align_zero();
|
||||
}
|
||||
}
|
||||
|
||||
/// The CABAC arithmetic encoder of H.265 9.3.4.4, for the bins these pictures need.
|
||||
struct Cabac {
|
||||
low: u32,
|
||||
range: u32,
|
||||
first: bool,
|
||||
outstanding: u32,
|
||||
}
|
||||
|
||||
impl Cabac {
|
||||
fn new() -> Cabac {
|
||||
Cabac { low: 0, range: 510, first: true, outstanding: 0 }
|
||||
}
|
||||
|
||||
fn put_bit(&mut self, out: &mut Bits, b: u32) {
|
||||
if self.first {
|
||||
self.first = false;
|
||||
} else {
|
||||
out.put(b, 1);
|
||||
}
|
||||
while self.outstanding > 0 {
|
||||
out.put(1 - b, 1);
|
||||
self.outstanding -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn renorm(&mut self, out: &mut Bits) {
|
||||
while self.range < 256 {
|
||||
if self.low < 256 {
|
||||
self.put_bit(out, 0);
|
||||
} else if self.low >= 512 {
|
||||
self.low -= 512;
|
||||
self.put_bit(out, 1);
|
||||
} else {
|
||||
self.low -= 256;
|
||||
self.outstanding += 1;
|
||||
}
|
||||
self.range <<= 1;
|
||||
self.low <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// `part_mode` = PART_2Nx2N: the most probable symbol of a fresh context (initValue 184 at
|
||||
/// QP 26 gives pStateIdx 0, valMps 1), so the LPS range is `rangeTabLps[0][3]` = 240.
|
||||
fn part_mode_2nx2n(&mut self, out: &mut Bits) {
|
||||
let lps = [128, 176, 208, 240][((self.range >> 6) & 3) as usize];
|
||||
self.range -= lps;
|
||||
self.renorm(out);
|
||||
}
|
||||
|
||||
/// A terminating bin equal to 1 (`pcm_flag`, `end_of_slice_segment_flag`), with the flush.
|
||||
fn terminate_one(&mut self, out: &mut Bits) {
|
||||
self.range -= 2;
|
||||
self.low += self.range;
|
||||
self.range = 2;
|
||||
self.renorm(out);
|
||||
self.put_bit(out, (self.low >> 9) & 1);
|
||||
out.put(((self.low >> 7) & 3) | 1, 2);
|
||||
}
|
||||
}
|
||||
|
||||
fn nal(kind: u8, rbsp: &[u8]) -> Vec<u8> {
|
||||
let mut out = vec![kind << 1, 1];
|
||||
let mut zeros = 0;
|
||||
for &b in rbsp {
|
||||
if zeros >= 2 && b <= 3 {
|
||||
out.push(3);
|
||||
zeros = 0;
|
||||
}
|
||||
zeros = if b == 0 { zeros + 1 } else { 0 };
|
||||
out.push(b);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn profile_idc(bit_depth: u8, mono: bool) -> u32 {
|
||||
match (bit_depth, mono) {
|
||||
(_, true) => 4, // range extensions
|
||||
(8, false) => 1,
|
||||
_ => 2,
|
||||
}
|
||||
}
|
||||
|
||||
fn ptl(w: &mut Bits, idc: u32) {
|
||||
w.put(0, 2);
|
||||
w.put(0, 1);
|
||||
w.put(idc, 5);
|
||||
w.put(1 << (31 - idc), 32); // compatible with itself
|
||||
w.put(0b1001, 4); // progressive, frame only
|
||||
w.put(0, 22);
|
||||
w.put(0, 21);
|
||||
w.put(0, 1);
|
||||
w.put(93, 8); // level 3.1
|
||||
}
|
||||
|
||||
fn parameter_sets(bit_depth: u8, mono: bool) -> [Vec<u8>; 3] {
|
||||
let idc = profile_idc(bit_depth, mono);
|
||||
let bd = u32::from(bit_depth);
|
||||
let mut v = Bits::default();
|
||||
v.put(0, 4);
|
||||
v.put(3, 2);
|
||||
v.put(0, 6);
|
||||
v.put(0, 3);
|
||||
v.put(1, 1);
|
||||
v.put(0xffff, 16);
|
||||
ptl(&mut v, idc);
|
||||
v.put(0, 1); // vps_sub_layer_ordering_info_present_flag
|
||||
v.ue(0);
|
||||
v.ue(0);
|
||||
v.ue(0);
|
||||
v.put(0, 6); // vps_max_layer_id
|
||||
v.ue(0); // vps_num_layer_sets_minus1
|
||||
v.put(0, 1); // vps_timing_info_present_flag
|
||||
v.put(0, 1); // vps_extension_flag
|
||||
v.trailing();
|
||||
|
||||
let mut s = Bits::default();
|
||||
s.put(0, 4);
|
||||
s.put(0, 3);
|
||||
s.put(1, 1);
|
||||
ptl(&mut s, idc);
|
||||
s.ue(0);
|
||||
s.ue(if mono { 0 } else { 1 });
|
||||
s.ue(TILE);
|
||||
s.ue(TILE);
|
||||
s.put(0, 1); // conformance_window_flag
|
||||
s.ue(bd - 8);
|
||||
s.ue(bd - 8);
|
||||
s.ue(4);
|
||||
s.put(1, 1);
|
||||
s.ue(0);
|
||||
s.ue(0);
|
||||
s.ue(0);
|
||||
s.ue(2); // log2_min_luma_coding_block_size_minus3: 32
|
||||
s.ue(0); // log2_diff_max_min_luma_coding_block_size: CTB 32
|
||||
s.ue(0); // log2_min_luma_transform_block_size_minus2: 4
|
||||
s.ue(3); // log2_diff_max_min_luma_transform_block_size: 32
|
||||
s.ue(0);
|
||||
s.ue(0);
|
||||
s.put(0, 1); // scaling_list_enabled_flag
|
||||
s.put(0, 1); // amp_enabled_flag
|
||||
s.put(0, 1); // sample_adaptive_offset_enabled_flag
|
||||
s.put(1, 1); // pcm_enabled_flag
|
||||
s.put(bd - 1, 4); // pcm_sample_bit_depth_luma_minus1
|
||||
s.put(bd - 1, 4); // pcm_sample_bit_depth_chroma_minus1
|
||||
s.ue(2); // log2_min_pcm_luma_coding_block_size_minus3: 32
|
||||
s.ue(0); // log2_diff_max_min_pcm_luma_coding_block_size
|
||||
s.put(1, 1); // pcm_loop_filter_disabled_flag
|
||||
s.ue(0); // num_short_term_ref_pic_sets
|
||||
s.put(0, 1); // long_term_ref_pics_present_flag
|
||||
s.put(0, 1); // sps_temporal_mvp_enabled_flag
|
||||
s.put(0, 1); // strong_intra_smoothing_enabled_flag
|
||||
s.put(0, 1); // vui_parameters_present_flag
|
||||
s.put(0, 1); // sps_extension_present_flag
|
||||
s.trailing();
|
||||
|
||||
let mut p = Bits::default();
|
||||
p.ue(0);
|
||||
p.ue(0);
|
||||
p.put(0, 6); // dependent slices, output flag, 3 extra header bits, sign hiding
|
||||
p.put(0, 1); // cabac_init_present_flag
|
||||
p.ue(0);
|
||||
p.ue(0);
|
||||
p.ue(0); // init_qp_minus26 (se(0) is the same bits)
|
||||
p.put(0, 3); // constrained intra, transform skip, cu_qp_delta
|
||||
p.ue(0);
|
||||
p.ue(0);
|
||||
p.put(0, 1);
|
||||
p.put(0, 2);
|
||||
p.put(0, 1); // transquant_bypass_enabled_flag
|
||||
p.put(0, 1); // tiles_enabled_flag
|
||||
p.put(0, 1); // entropy_coding_sync_enabled_flag
|
||||
p.put(0, 1); // pps_loop_filter_across_slices_enabled_flag
|
||||
p.put(1, 1); // deblocking_filter_control_present_flag
|
||||
p.put(0, 1); // deblocking_filter_override_enabled_flag
|
||||
p.put(1, 1); // pps_deblocking_filter_disabled_flag
|
||||
p.put(0, 1);
|
||||
p.put(0, 1);
|
||||
p.ue(0);
|
||||
p.put(0, 1);
|
||||
p.put(0, 1);
|
||||
p.trailing();
|
||||
[nal(32, &v.bytes), nal(33, &s.bytes), nal(34, &p.bytes)]
|
||||
}
|
||||
|
||||
/// One coded 32 × 32 picture: the `hvcC` payload and the item data (length-prefixed NAL).
|
||||
struct Coded {
|
||||
hvcc: Vec<u8>,
|
||||
data: Vec<u8>,
|
||||
}
|
||||
|
||||
fn picture(bit_depth: u8, mono: bool, sample: &dyn Fn(u32, u32) -> [u16; 3]) -> Coded {
|
||||
let bd = u32::from(bit_depth);
|
||||
let mut w = Bits::default();
|
||||
w.put(1, 1); // first_slice_segment_in_pic_flag
|
||||
w.put(0, 1); // no_output_of_prior_pics_flag
|
||||
w.ue(0);
|
||||
w.ue(2); // I slice
|
||||
w.ue(0); // slice_qp_delta, se(0)
|
||||
w.trailing(); // byte_alignment()
|
||||
let mut c = Cabac::new();
|
||||
c.part_mode_2nx2n(&mut w);
|
||||
c.terminate_one(&mut w); // pcm_flag
|
||||
w.align_zero(); // pcm_alignment_zero_bit
|
||||
for y in 0..TILE {
|
||||
for x in 0..TILE {
|
||||
w.put(u32::from(sample(x, y)[0]), bd);
|
||||
}
|
||||
}
|
||||
if !mono {
|
||||
for k in 1..3 {
|
||||
for y in 0..TILE / 2 {
|
||||
for x in 0..TILE / 2 {
|
||||
w.put(u32::from(sample(2 * x, 2 * y)[k]), bd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut c = Cabac::new();
|
||||
c.terminate_one(&mut w); // end_of_slice_segment_flag (its flush ends in the stop bit)
|
||||
w.align_zero();
|
||||
let slice = nal(19, &w.bytes);
|
||||
|
||||
let sets = parameter_sets(bit_depth, mono);
|
||||
let mut h = vec![1u8, profile_idc(bit_depth, mono) as u8];
|
||||
h.extend_from_slice(&(1u32 << (31 - profile_idc(bit_depth, mono))).to_be_bytes());
|
||||
h.extend_from_slice(&[
|
||||
0x90,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
93,
|
||||
0xf0,
|
||||
0,
|
||||
0xfc,
|
||||
0xfc | u8::from(!mono),
|
||||
0xf8 | (bit_depth - 8),
|
||||
0xf8 | (bit_depth - 8),
|
||||
0,
|
||||
0,
|
||||
0x0f,
|
||||
3,
|
||||
]);
|
||||
for (kind, set) in [32u8, 33, 34].into_iter().zip(&sets) {
|
||||
h.push(0x80 | kind);
|
||||
h.extend_from_slice(&1u16.to_be_bytes());
|
||||
h.extend_from_slice(&(set.len() as u16).to_be_bytes());
|
||||
h.extend_from_slice(set);
|
||||
}
|
||||
let mut data = (slice.len() as u32).to_be_bytes().to_vec();
|
||||
data.extend_from_slice(&slice);
|
||||
Coded { hvcc: h, data }
|
||||
}
|
||||
|
||||
fn bx(kind: &[u8; 4], payload: &[u8]) -> Vec<u8> {
|
||||
let mut v = ((payload.len() + 8) as u32).to_be_bytes().to_vec();
|
||||
v.extend_from_slice(kind);
|
||||
v.extend_from_slice(payload);
|
||||
v
|
||||
}
|
||||
|
||||
fn full_box(kind: &[u8; 4], version: u8, payload: &[u8]) -> Vec<u8> {
|
||||
let mut p = vec![version, 0, 0, 0];
|
||||
p.extend_from_slice(payload);
|
||||
bx(kind, &p)
|
||||
}
|
||||
|
||||
struct Item {
|
||||
id: u16,
|
||||
kind: &'static [u8; 4],
|
||||
data: Vec<u8>,
|
||||
/// Indices (1-based) into the property list, with the essential flag.
|
||||
props: Vec<(u8, bool)>,
|
||||
/// Not an image of its own (a grid tile).
|
||||
hidden: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct File {
|
||||
items: Vec<Item>,
|
||||
props: Vec<Vec<u8>>,
|
||||
refs: Vec<(&'static [u8; 4], u16, Vec<u16>)>,
|
||||
}
|
||||
|
||||
impl File {
|
||||
fn prop(&mut self, b: Vec<u8>) -> u8 {
|
||||
self.props.push(b);
|
||||
self.props.len() as u8
|
||||
}
|
||||
|
||||
fn add(&mut self, kind: &'static [u8; 4], data: Vec<u8>, props: Vec<(u8, bool)>) -> u16 {
|
||||
let id = self.items.len() as u16 + 1;
|
||||
self.items.push(Item { id, kind, data, props, hidden: false });
|
||||
id
|
||||
}
|
||||
|
||||
/// A coded image of `w` × `h` (a grid when larger than a tile); returns its item id.
|
||||
fn image(&mut self, w: u32, h: u32, bit_depth: u8, mono: bool, sample: &dyn Fn(u32, u32) -> [u16; 3]) -> u16 {
|
||||
let (cols, rows) = (w.div_ceil(TILE), h.div_ceil(TILE));
|
||||
let ispe = |w: u32, h: u32| {
|
||||
let mut p = w.to_be_bytes().to_vec();
|
||||
p.extend_from_slice(&h.to_be_bytes());
|
||||
full_box(b"ispe", 0, &p)
|
||||
};
|
||||
let mut hvcc_prop = None;
|
||||
let tile_ispe = self.prop(ispe(TILE, TILE));
|
||||
let mut tiles = Vec::new();
|
||||
for r in 0..rows {
|
||||
for c in 0..cols {
|
||||
let coded = picture(bit_depth, mono, &|x, y| sample((c * TILE + x).min(w - 1), (r * TILE + y).min(h - 1)));
|
||||
let hv = *hvcc_prop.get_or_insert_with(|| self.prop(bx(b"hvcC", &coded.hvcc)));
|
||||
tiles.push(self.add(b"hvc1", coded.data, vec![(hv, true), (tile_ispe, false)]));
|
||||
}
|
||||
}
|
||||
if let ([only], true) = (tiles.as_slice(), w == TILE && h == TILE) {
|
||||
return *only;
|
||||
}
|
||||
for it in self.items.iter_mut().filter(|i| tiles.contains(&i.id)) {
|
||||
it.hidden = true;
|
||||
}
|
||||
let mut g = vec![0u8, 0, (rows - 1) as u8, (cols - 1) as u8];
|
||||
g.extend_from_slice(&(w as u16).to_be_bytes());
|
||||
g.extend_from_slice(&(h as u16).to_be_bytes());
|
||||
let grid_ispe = self.prop(ispe(w, h));
|
||||
let id = self.add(b"grid", g, vec![(grid_ispe, false)]);
|
||||
self.refs.push((b"dimg", id, tiles));
|
||||
id
|
||||
}
|
||||
|
||||
fn write(self, primary: u16) -> Vec<u8> {
|
||||
let ftyp = bx(b"ftyp", b"heic\0\0\0\0mif1heic");
|
||||
let mut hdlr = vec![0u8; 4];
|
||||
hdlr.extend_from_slice(b"pict");
|
||||
hdlr.extend_from_slice(&[0; 13]);
|
||||
let hdlr = full_box(b"hdlr", 0, &hdlr);
|
||||
let pitm = full_box(b"pitm", 0, &primary.to_be_bytes());
|
||||
let mut iinf = (self.items.len() as u16).to_be_bytes().to_vec();
|
||||
for it in &self.items {
|
||||
let mut e = vec![2, 0, 0, u8::from(it.hidden)]; // version 2, flags
|
||||
e.extend_from_slice(&it.id.to_be_bytes());
|
||||
e.extend_from_slice(&[0, 0]);
|
||||
e.extend_from_slice(it.kind);
|
||||
e.push(0);
|
||||
iinf.extend_from_slice(&bx(b"infe", &e));
|
||||
}
|
||||
let iinf = full_box(b"iinf", 0, &iinf);
|
||||
let mut iref = Vec::new();
|
||||
for (kind, from, to) in &self.refs {
|
||||
let mut r = from.to_be_bytes().to_vec();
|
||||
r.extend_from_slice(&(to.len() as u16).to_be_bytes());
|
||||
for t in to {
|
||||
r.extend_from_slice(&t.to_be_bytes());
|
||||
}
|
||||
iref.extend_from_slice(&bx(kind, &r));
|
||||
}
|
||||
let iref = full_box(b"iref", 0, &iref);
|
||||
let ipco = bx(b"ipco", &self.props.concat());
|
||||
let mut ipma = (self.items.iter().filter(|i| !i.props.is_empty()).count() as u32).to_be_bytes().to_vec();
|
||||
for it in self.items.iter().filter(|i| !i.props.is_empty()) {
|
||||
ipma.extend_from_slice(&it.id.to_be_bytes());
|
||||
ipma.push(it.props.len() as u8);
|
||||
ipma.extend(it.props.iter().map(|&(i, essential)| i | if essential { 0x80 } else { 0 }));
|
||||
}
|
||||
let iprp = bx(b"iprp", &[ipco, full_box(b"ipma", 0, &ipma)].concat());
|
||||
let iloc = |base: u32| {
|
||||
let mut l = vec![0x44, 0x00];
|
||||
l.extend_from_slice(&(self.items.len() as u16).to_be_bytes());
|
||||
let mut offset = base;
|
||||
for it in &self.items {
|
||||
l.extend_from_slice(&it.id.to_be_bytes());
|
||||
l.extend_from_slice(&[0, 0, 0, 0, 0, 1]); // construction method 0, data ref 0, one extent
|
||||
l.extend_from_slice(&offset.to_be_bytes());
|
||||
l.extend_from_slice(&(it.data.len() as u32).to_be_bytes());
|
||||
offset += it.data.len() as u32;
|
||||
}
|
||||
full_box(b"iloc", 1, &l)
|
||||
};
|
||||
let meta = |base| full_box(b"meta", 0, &[hdlr.clone(), pitm.clone(), iloc(base), iinf.clone(), iref.clone(), iprp.clone()].concat());
|
||||
let start = (ftyp.len() + meta(0).len() + 8) as u32;
|
||||
let mdat = bx(b"mdat", &self.items.iter().flat_map(|i| i.data.iter().copied()).collect::<Vec<u8>>());
|
||||
[ftyp, meta(start), mdat].concat()
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes the HEIF file `spec` describes.
|
||||
pub fn build(spec: &Spec<'_>) -> Vec<u8> {
|
||||
let mut f = File::default();
|
||||
let primary = f.image(spec.width, spec.height, spec.bit_depth, false, spec.sample);
|
||||
let mut assoc = Vec::new();
|
||||
for p in &spec.props {
|
||||
let b = match p {
|
||||
Prop::Icc(icc) => bx(b"colr", &[b"prof".as_slice(), icc].concat()),
|
||||
Prop::Nclx(pr, tc, mc, full) => {
|
||||
let mut c = b"nclx".to_vec();
|
||||
for v in [pr, tc, mc] {
|
||||
c.extend_from_slice(&v.to_be_bytes());
|
||||
}
|
||||
c.push(if *full { 0x80 } else { 0 });
|
||||
bx(b"colr", &c)
|
||||
}
|
||||
Prop::Irot(a) => bx(b"irot", &[a & 3]),
|
||||
Prop::Imir(a) => bx(b"imir", &[a & 1]),
|
||||
Prop::Clap(w, h, ho, vo) => {
|
||||
let mut c = Vec::new();
|
||||
for v in [*w as i32, 1, *h as i32, 1, *ho, 1, *vo, 1] {
|
||||
c.extend_from_slice(&v.to_be_bytes());
|
||||
}
|
||||
bx(b"clap", &c)
|
||||
}
|
||||
};
|
||||
assoc.push((f.prop(b), true, matches!(p, Prop::Clap(..))));
|
||||
}
|
||||
if let Some(it) = f.items.iter_mut().find(|i| i.id == primary) {
|
||||
it.props.extend(assoc.iter().map(|&(i, e, _)| (i, e)));
|
||||
}
|
||||
if let Some(exif) = &spec.exif {
|
||||
let id = f.add(b"Exif", [&[0u8, 0, 0, 0][..], exif].concat(), Vec::new());
|
||||
f.refs.push((b"cdsc", id, vec![primary]));
|
||||
}
|
||||
let bd = spec.bit_depth;
|
||||
let aux = |f: &mut File, urn: &str, sample: &dyn Fn(u32, u32) -> [u16; 3]| {
|
||||
let id = f.image(spec.width, spec.height, bd, true, sample);
|
||||
let auxc = f.prop(full_box(b"auxC", 0, &[urn.as_bytes(), &[0]].concat()));
|
||||
if let Some(it) = f.items.iter_mut().find(|i| i.id == id) {
|
||||
it.props.push((auxc, true));
|
||||
}
|
||||
f.refs.push((b"auxl", id, vec![primary]));
|
||||
};
|
||||
if let Some(alpha) = spec.alpha {
|
||||
aux(&mut f, "urn:mpeg:hevc:2015:auxid:1", &|x, y| [alpha(x, y), 0, 0]);
|
||||
}
|
||||
for urn in &spec.ignored_aux {
|
||||
aux(&mut f, urn, &|x, y| [((x + y) % 256) as u16, 0, 0]);
|
||||
}
|
||||
if let Some(sample) = spec.thumbnail {
|
||||
// Half the size, with the primary image's colour and orientation (not its crop).
|
||||
let id = f.image(spec.width / 2, spec.height / 2, spec.bit_depth, false, sample);
|
||||
if let Some(it) = f.items.iter_mut().find(|i| i.id == id) {
|
||||
it.props.extend(assoc.iter().filter(|a| !a.2).map(|&(i, e, _)| (i, e)));
|
||||
}
|
||||
f.refs.push((b"thmb", id, vec![primary]));
|
||||
}
|
||||
f.write(primary)
|
||||
}
|
||||
@@ -1,200 +0,0 @@
|
||||
//! Decoding lossless synthetic HEIF files ([`crate::testdata`]): every expected sample is known.
|
||||
|
||||
use crate::testdata::{Prop, Spec, build};
|
||||
use crate::{Error, Options, decode, decode_thumbnail, probe};
|
||||
|
||||
/// A luma value unique enough to identify its position; chroma neutral.
|
||||
fn luma(x: u32, y: u32) -> u16 {
|
||||
((x * 7 + y * 13) % 220 + 20) as u16
|
||||
}
|
||||
|
||||
fn grey(x: u32, y: u32) -> [u16; 3] {
|
||||
[luma(x, y), 128, 128]
|
||||
}
|
||||
|
||||
const FULL_601: Prop = Prop::Nclx(1, 13, 6, true);
|
||||
|
||||
/// Decoded red channel at `(x, y)`, back in 8-bit code values.
|
||||
fn at(d: &crate::Decoded, x: u32, y: u32) -> u16 {
|
||||
let ch = if d.has_alpha { 4 } else { 3 };
|
||||
d.data[((y * d.width + x) as usize) * ch] / 257
|
||||
}
|
||||
|
||||
fn decoded(spec: &Spec) -> crate::Decoded {
|
||||
decode(&build(spec), &Options::default()).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_grid_reassembles_exactly() {
|
||||
// 4 × 3 tiles of 32, cropped by the grid to 100 × 70.
|
||||
let d = decoded(&Spec { props: vec![FULL_601], ..Spec::new(100, 70, &grey) });
|
||||
assert_eq!((d.width, d.height, d.bit_depth, d.has_alpha), (100, 70, 8, false));
|
||||
for y in 0..70 {
|
||||
for x in 0..100 {
|
||||
let px = &d.data[((y * 100 + x) * 3) as usize..][..3];
|
||||
assert_eq!(px, [luma(x, y) * 257; 3], "({x}, {y})");
|
||||
}
|
||||
}
|
||||
let info = probe(&build(&Spec::new(100, 70, &grey))).unwrap();
|
||||
assert_eq!((info.width, info.height, info.coded_width, info.coded_height, info.bit_depth), (100, 70, 100, 70, 8));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn colour_follows_the_matrix_and_range() {
|
||||
// BT.601 full range, a saturated orange: within one code value of the exact conversion.
|
||||
let (y, cb, cr) = (150.0f32, 80.0, 190.0);
|
||||
let s = |_: u32, _: u32| [y as u16, cb as u16, cr as u16];
|
||||
let d = decoded(&Spec { props: vec![FULL_601], ..Spec::new(32, 32, &s) });
|
||||
let (u, v) = (cb - 128.0, cr - 128.0);
|
||||
let expected = [y + 1.402 * v, y - 0.344_136 * u - 0.714_136 * v, y + 1.772 * u].map(|c| c.clamp(0.0, 255.0));
|
||||
for (got, want) in d.data[..3].iter().zip(expected) {
|
||||
assert!((f32::from(*got) / 257.0 - want).abs() <= 1.0, "{:?} vs {expected:?}", &d.data[..3]);
|
||||
}
|
||||
assert_eq!(d.colour, crate::Colour { primaries: 1, transfer: 13, matrix: 6, full_range: true });
|
||||
|
||||
// No nclx and no VUI: the H.265 defaults, limited range (16 → black, 235 → white).
|
||||
let ends = |x: u32, _: u32| [if x < 16 { 16 } else { 235 }, 128, 128];
|
||||
let d = decoded(&Spec::new(32, 32, &ends));
|
||||
assert!(!d.colour.full_range);
|
||||
assert_eq!((d.data[0], d.data[20 * 3]), (0, 65535));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ten_bit_samples_keep_their_precision() {
|
||||
let s = |x: u32, y: u32| [(x * 31 + y) as u16 % 1024, 512, 512];
|
||||
let d = decoded(&Spec { bit_depth: 10, props: vec![FULL_601], ..Spec::new(64, 32, &s) });
|
||||
assert_eq!(d.bit_depth, 10);
|
||||
for x in [0, 1, 5, 33, 63] {
|
||||
let want = ((x * 31) % 1024) as f64 / 1023.0 * 65535.0;
|
||||
assert!((f64::from(d.data[(x * 3) as usize]) - want).abs() <= 1.0, "x {x}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn container_transforms_are_applied_like_libheif() {
|
||||
let (w, h) = (70u32, 40u32);
|
||||
let spec = |props: Vec<Prop>| Spec { props: [vec![FULL_601], props].concat(), ..Spec::new(w, h, &grey) };
|
||||
// irot: counter-clockwise quarter turns.
|
||||
let d = decoded(&spec(vec![Prop::Irot(1)]));
|
||||
assert_eq!((d.width, d.height), (h, w));
|
||||
assert_eq!(at(&d, 0, 0), luma(w - 1, 0)); // the right column becomes the top row
|
||||
assert_eq!(at(&d, h - 1, w - 1), luma(0, h - 1));
|
||||
let d = decoded(&spec(vec![Prop::Irot(2)]));
|
||||
assert_eq!(at(&d, 0, 0), luma(w - 1, h - 1));
|
||||
let d = decoded(&spec(vec![Prop::Irot(3)]));
|
||||
assert_eq!(at(&d, 0, 0), luma(0, h - 1));
|
||||
// imir: axis 0 exchanges top and bottom, axis 1 left and right (libheif's reading).
|
||||
let d = decoded(&spec(vec![Prop::Imir(0)]));
|
||||
assert_eq!((at(&d, 0, 0), at(&d, 5, 0)), (luma(0, h - 1), luma(5, h - 1)));
|
||||
let d = decoded(&spec(vec![Prop::Imir(1)]));
|
||||
assert_eq!((at(&d, 0, 0), at(&d, 0, 5)), (luma(w - 1, 0), luma(w - 1, 5)));
|
||||
// clap: centred crop moved by its offsets; a half-pixel corner goes left and down.
|
||||
let d = decoded(&spec(vec![Prop::Clap(31, 21, 3, -2)]));
|
||||
assert_eq!((d.width, d.height), (31, 21));
|
||||
// left = (70 - 31 + 6) / 2 = 22.5 → 22, top = (40 - 21 - 4) / 2 = 7.5 → 8
|
||||
assert_eq!(at(&d, 0, 0), luma(22, 8));
|
||||
// In association order: crop, then turn, then mirror.
|
||||
let d = decoded(&spec(vec![Prop::Clap(30, 20, 0, 0), Prop::Irot(1), Prop::Imir(1)]));
|
||||
assert_eq!((d.width, d.height), (20, 30));
|
||||
// The crop is (20, 10) to (49, 29); the turn brings its bottom-right corner to the top-right,
|
||||
// the left-right mirror to the top-left.
|
||||
assert_eq!(at(&d, 0, 0), luma(49, 29));
|
||||
let info = probe(&build(&spec(vec![Prop::Irot(1)]))).unwrap();
|
||||
assert_eq!((info.width, info.height, info.coded_width, info.coded_height), (h, w, w, h));
|
||||
// Transforms off: as coded.
|
||||
let d = decode(&build(&spec(vec![Prop::Irot(1)])), &Options { apply_transforms: false, ..Default::default() }).unwrap();
|
||||
assert_eq!((d.width, d.height, at(&d, 3, 2)), (w, h, luma(3, 2)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metadata_aux_images_and_thumbnails() {
|
||||
let icc = b"not parsed here: carried verbatim".to_vec();
|
||||
let exif = b"MM\0\x2a\0\0\0\x08\0\0".to_vec();
|
||||
let spec = Spec {
|
||||
props: vec![Prop::Icc(icc.clone()), FULL_601],
|
||||
exif: Some(exif.clone()),
|
||||
thumbnail: Some(&|_, _| [77, 128, 128]),
|
||||
ignored_aux: vec!["urn:com:apple:photo:2020:aux:hdrgainmap", "urn:mpeg:hevc:2015:auxid:2"],
|
||||
..Spec::new(64, 64, &grey)
|
||||
};
|
||||
let bytes = build(&spec);
|
||||
let d = decode(&bytes, &Options::default()).unwrap();
|
||||
assert_eq!((d.width, d.height, d.has_alpha), (64, 64, false));
|
||||
assert_eq!(d.icc.as_deref(), Some(icc.as_slice()));
|
||||
assert_eq!(d.exif.as_deref(), Some(exif.as_slice()));
|
||||
let t = decode_thumbnail(&bytes, 32, &Options::default()).unwrap().unwrap();
|
||||
assert_eq!((t.width, t.height, t.data[0]), (32, 32, 77 * 257));
|
||||
assert_eq!(t.exif.as_deref(), Some(exif.as_slice()));
|
||||
assert_eq!(t.icc.as_deref(), Some(icc.as_slice()));
|
||||
assert!(decode_thumbnail(&bytes, 33, &Options::default()).unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_comes_from_its_auxiliary_image() {
|
||||
let alpha = |x: u32, _: u32| (x * 4) as u16;
|
||||
let d = decoded(&Spec { props: vec![FULL_601], alpha: Some(&alpha), ..Spec::new(64, 32, &grey) });
|
||||
assert!(d.has_alpha);
|
||||
assert_eq!(d.data.len(), 64 * 32 * 4);
|
||||
assert_eq!((d.data[3], d.data[10 * 4 + 3]), (0, 40 * 257));
|
||||
assert_eq!(d.data[..3], [luma(0, 0) * 257; 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn limits_are_checked_before_decoding() {
|
||||
let bytes = build(&Spec::new(96, 64, &grey));
|
||||
let r = decode(&bytes, &Options { max_pixels: 96 * 64 - 1, ..Default::default() });
|
||||
assert!(matches!(r, Err(Error::Limit(_))), "{r:?}");
|
||||
}
|
||||
|
||||
/// #1518: a grid's tiles together are held to the pixel budget before any is decoded. A
|
||||
/// 33 × 33 canvas fits 1089 pixels, but its four 32 × 32 tiles hold 4096: over twice the limit.
|
||||
#[test]
|
||||
fn grid_tiles_are_held_to_the_pixel_budget_together() {
|
||||
let bytes = build(&Spec::new(33, 33, &grey));
|
||||
let r = decode(&bytes, &Options { max_pixels: 33 * 33, ..Default::default() });
|
||||
assert!(matches!(r, Err(Error::Limit(_))), "{r:?}");
|
||||
let d = decode(&bytes, &Options { max_pixels: 2 * 32 * 32, ..Default::default() }).unwrap();
|
||||
assert_eq!((d.width, d.height), (33, 33));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_and_corrupted_files_are_errors_not_panics() {
|
||||
let bytes = build(&Spec { props: vec![FULL_601, Prop::Irot(1)], thumbnail: Some(&grey), ..Spec::new(64, 64, &grey) });
|
||||
let full = decode(&bytes, &Options::default()).unwrap();
|
||||
for cut in (0..bytes.len()).step_by(7) {
|
||||
let b = &bytes[..cut];
|
||||
// Cut inside the photo: an error. Cut in the trailing thumbnail: the whole photo.
|
||||
if let (Ok(_), Ok(d)) = (probe(b), decode(b, &Options::default())) {
|
||||
assert_eq!(d.data, full.data, "cut at {cut} of {}", bytes.len());
|
||||
}
|
||||
let _ = decode_thumbnail(b, 1, &Options::default());
|
||||
}
|
||||
// Flip bits all over the file: any outcome but a panic.
|
||||
let mut state = 0x2545_f491_u32;
|
||||
for _ in 0..400 {
|
||||
let mut b = bytes.clone();
|
||||
for _ in 0..3 {
|
||||
state ^= state << 13;
|
||||
state ^= state >> 17;
|
||||
state ^= state << 5;
|
||||
let i = state as usize % b.len();
|
||||
b[i] ^= 1 << (state % 8);
|
||||
}
|
||||
let _ = probe(&b);
|
||||
let _ = decode(&b, &Options::default());
|
||||
let _ = decode_thumbnail(&b, 1, &Options::default());
|
||||
}
|
||||
}
|
||||
|
||||
/// CodeRabbit #44: heic-rs keeps every associated transform and each one re-allocates the
|
||||
/// whole picture, so 13 of them are over the [`crate::MAX_TRANSFORMS`] cap and refused.
|
||||
#[test]
|
||||
fn too_many_transform_properties_are_refused() {
|
||||
let bytes = build(&Spec { props: std::iter::repeat_n(Prop::Irot(0), 13).collect(), ..Spec::new(32, 32, &grey) });
|
||||
let r = decode(&bytes, &Options::default());
|
||||
assert!(matches!(r, Err(Error::Limit(ref m)) if m.contains("transform")), "{r:?}");
|
||||
// At the cap they're not: 12 identity rotations decode normally.
|
||||
let bytes = build(&Spec { props: std::iter::repeat_n(Prop::Irot(0), 12).collect(), ..Spec::new(32, 32, &grey) });
|
||||
let d = decode(&bytes, &Options::default()).unwrap();
|
||||
assert_eq!((d.width, d.height), (32, 32));
|
||||
}
|
||||
@@ -1,459 +0,0 @@
|
||||
//! The colour description in an HEVC sequence parameter set's VUI (Rec. ITU-T H.265, 7.3.2.2 and
|
||||
//! Annex E): the fallback a HEIF reader uses when the item carries no `colr` box of type `nclx`.
|
||||
//! iPhone photos are the common case: they label their colour with an ICC profile only and say in
|
||||
//! the VUI that their YCbCr is full range.
|
||||
//!
|
||||
//! Only the syntax up to `matrix_coeffs` is read. Anything this parser can't follow returns `None`
|
||||
//! and the caller falls back to the H.265 defaults; it never fails a decode.
|
||||
|
||||
/// What the VUI's `video_signal_type` says (H.265 E.3.1). Absent fields keep the defaults the
|
||||
/// standard infers: limited range, every code point 2 ("unspecified").
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Signal {
|
||||
pub full_range: bool,
|
||||
pub primaries: u8,
|
||||
pub transfer: u8,
|
||||
pub matrix: u8,
|
||||
}
|
||||
|
||||
impl Default for Signal {
|
||||
fn default() -> Self {
|
||||
Signal { full_range: false, primaries: 2, transfer: 2, matrix: 2 }
|
||||
}
|
||||
}
|
||||
|
||||
/// NAL unit type of a sequence parameter set.
|
||||
const SPS_NUT: u8 = 33;
|
||||
|
||||
/// Reads the signal description from the SPS among `parameter_sets` (NAL units with their
|
||||
/// two-byte header, as an `hvcC` record stores them). `None`: no SPS, or one this reader can't
|
||||
/// follow; `Some(Signal::default())`: an SPS without VUI colour fields.
|
||||
pub fn signal(parameter_sets: &[&[u8]]) -> Option<Signal> {
|
||||
let sps = parameter_sets.iter().find(|n| n.first().is_some_and(|b| (b >> 1) & 0x3f == SPS_NUT))?;
|
||||
parse_sps(&unescape(sps.get(2..)?))
|
||||
}
|
||||
|
||||
/// The uncropped picture size (`pic_width/height_in_luma_samples`) of every SPS among
|
||||
/// `parameter_sets`: what the decoder allocates before it applies the conformance window, so the
|
||||
/// pixel budget is checked against it (heic-rs reports only the first SPS, cropped). `None`: an
|
||||
/// SPS this reader can't follow.
|
||||
pub fn coded_sizes(parameter_sets: &[&[u8]]) -> Option<Vec<(u32, u32)>> {
|
||||
parameter_sets
|
||||
.iter()
|
||||
.filter(|n| n.first().is_some_and(|b| (b >> 1) & 0x3f == SPS_NUT))
|
||||
.map(|sps| {
|
||||
let rbsp = unescape(sps.get(2..)?);
|
||||
let r = &mut Bits { data: &rbsp, pos: 0 };
|
||||
sps_size(r).map(|(w, h, _)| (w, h))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Reads an SPS up to and including the picture size, leaving `r` after it: width, height and
|
||||
/// `sps_max_sub_layers_minus1`.
|
||||
fn sps_size(r: &mut Bits) -> Option<(u32, u32, u32)> {
|
||||
r.skip(4)?; // sps_video_parameter_set_id
|
||||
let max_sub_layers_minus1 = r.u(3)?;
|
||||
if max_sub_layers_minus1 > 6 {
|
||||
return None;
|
||||
}
|
||||
r.skip(1)?; // sps_temporal_id_nesting_flag
|
||||
profile_tier_level(r, max_sub_layers_minus1)?;
|
||||
r.ue()?; // sps_seq_parameter_set_id
|
||||
if r.ue()? == 3 {
|
||||
r.skip(1)?; // separate_colour_plane_flag
|
||||
}
|
||||
let width = r.ue()?; // pic_width_in_luma_samples
|
||||
let height = r.ue()?; // pic_height_in_luma_samples
|
||||
Some((width, height, max_sub_layers_minus1))
|
||||
}
|
||||
|
||||
/// Removes emulation prevention bytes (`00 00 03` → `00 00`).
|
||||
fn unescape(nal: &[u8]) -> Vec<u8> {
|
||||
let mut out = Vec::with_capacity(nal.len());
|
||||
let mut zeros = 0usize;
|
||||
for &b in nal {
|
||||
if zeros >= 2 && b == 3 {
|
||||
zeros = 0;
|
||||
continue;
|
||||
}
|
||||
zeros = if b == 0 { zeros + 1 } else { 0 };
|
||||
out.push(b);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
struct Bits<'a> {
|
||||
data: &'a [u8],
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
impl Bits<'_> {
|
||||
fn u(&mut self, n: u32) -> Option<u32> {
|
||||
let mut v = 0u32;
|
||||
for _ in 0..n {
|
||||
let byte = *self.data.get(self.pos / 8)?;
|
||||
v = (v << 1) | u32::from((byte >> (7 - self.pos % 8)) & 1);
|
||||
self.pos += 1;
|
||||
}
|
||||
Some(v)
|
||||
}
|
||||
|
||||
fn flag(&mut self) -> Option<bool> {
|
||||
self.u(1).map(|b| b == 1)
|
||||
}
|
||||
|
||||
fn skip(&mut self, n: usize) -> Option<()> {
|
||||
self.pos = self.pos.checked_add(n)?;
|
||||
(self.pos <= self.data.len().saturating_mul(8)).then_some(())
|
||||
}
|
||||
|
||||
/// `ue(v)`; values past 32 bits are refused (no valid field here needs them).
|
||||
fn ue(&mut self) -> Option<u32> {
|
||||
let mut zeros = 0u32;
|
||||
while !self.flag()? {
|
||||
zeros += 1;
|
||||
if zeros > 31 {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let rest = self.u(zeros)?;
|
||||
((1u64 << zeros) - 1 + u64::from(rest)).try_into().ok()
|
||||
}
|
||||
|
||||
fn se(&mut self) -> Option<()> {
|
||||
self.ue().map(|_| ())
|
||||
}
|
||||
}
|
||||
|
||||
/// `profile_tier_level(1, max_sub_layers_minus1)` (7.3.3): only skipped.
|
||||
fn profile_tier_level(r: &mut Bits, max_sub_layers_minus1: u32) -> Option<()> {
|
||||
r.skip(88)?; // general profile space .. general_inbld/reserved flag
|
||||
r.skip(8)?; // general_level_idc
|
||||
let mut present = [(false, false); 8];
|
||||
for p in present.iter_mut().take(max_sub_layers_minus1 as usize) {
|
||||
*p = (r.flag()?, r.flag()?);
|
||||
}
|
||||
if max_sub_layers_minus1 > 0 {
|
||||
r.skip(2 * (8 - max_sub_layers_minus1 as usize))?; // reserved_zero_2bits
|
||||
}
|
||||
for &(profile, level) in present.iter().take(max_sub_layers_minus1 as usize) {
|
||||
if profile {
|
||||
r.skip(88)?;
|
||||
}
|
||||
if level {
|
||||
r.skip(8)?;
|
||||
}
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// `scaling_list_data()` (7.3.4): only skipped.
|
||||
fn scaling_list_data(r: &mut Bits) -> Option<()> {
|
||||
for size_id in 0..4u32 {
|
||||
let step = if size_id == 3 { 3 } else { 1 };
|
||||
for _ in (0..6).step_by(step) {
|
||||
if !r.flag()? {
|
||||
r.ue()?; // scaling_list_pred_matrix_id_delta
|
||||
} else {
|
||||
let coefs = 64.min(1 << (4 + (size_id << 1)));
|
||||
if size_id > 1 {
|
||||
r.se()?; // scaling_list_dc_coef_minus8
|
||||
}
|
||||
for _ in 0..coefs {
|
||||
r.se()?; // scaling_list_delta_coef
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// `st_ref_pic_set(idx)` (7.3.7) as it appears in an SPS; returns `NumDeltaPocs[idx]`.
|
||||
fn st_ref_pic_set(r: &mut Bits, idx: usize, num_delta_pocs: &[u32]) -> Option<u32> {
|
||||
if idx != 0 && r.flag()? {
|
||||
// inter_ref_pic_set_prediction_flag: predicted from the previous set (in an SPS,
|
||||
// delta_idx_minus1 is absent and RefRpsIdx = idx - 1).
|
||||
r.skip(1)?; // delta_rps_sign
|
||||
r.ue()?; // abs_delta_rps_minus1
|
||||
let reference = *num_delta_pocs.get(idx - 1)?;
|
||||
let mut count = 0u32;
|
||||
for _ in 0..=reference {
|
||||
let used = r.flag()?;
|
||||
let kept = used || r.flag()?; // use_delta_flag when not used_by_curr_pic_flag
|
||||
count += u32::from(kept);
|
||||
}
|
||||
return Some(count);
|
||||
}
|
||||
let negative = r.ue()?;
|
||||
let positive = r.ue()?;
|
||||
let total = negative.checked_add(positive)?;
|
||||
if total > 32 {
|
||||
return None; // at most 16 + 16 for any level
|
||||
}
|
||||
for _ in 0..total {
|
||||
r.ue()?; // delta_poc_s*_minus1
|
||||
r.skip(1)?; // used_by_curr_pic_s*_flag
|
||||
}
|
||||
Some(total)
|
||||
}
|
||||
|
||||
fn parse_sps(rbsp: &[u8]) -> Option<Signal> {
|
||||
let r = &mut Bits { data: rbsp, pos: 0 };
|
||||
let (_, _, max_sub_layers_minus1) = sps_size(r)?;
|
||||
parse_sps_parameters(r, max_sub_layers_minus1)?;
|
||||
parse_vui(r)
|
||||
}
|
||||
|
||||
fn parse_sps_parameters(r: &mut Bits<'_>, max_sub_layers_minus1: u32) -> Option<()> {
|
||||
if r.flag()? {
|
||||
for _ in 0..4 {
|
||||
r.ue()?; // conf_win_*_offset
|
||||
}
|
||||
}
|
||||
r.ue()?; // bit_depth_luma_minus8
|
||||
r.ue()?; // bit_depth_chroma_minus8
|
||||
let log2_max_poc_lsb = r.ue()?.checked_add(4)?;
|
||||
if log2_max_poc_lsb > 16 {
|
||||
return None;
|
||||
}
|
||||
let first = if r.flag()? { 0 } else { max_sub_layers_minus1 };
|
||||
for _ in first..=max_sub_layers_minus1 {
|
||||
r.ue()?; // sps_max_dec_pic_buffering_minus1
|
||||
r.ue()?; // sps_max_num_reorder_pics
|
||||
r.ue()?; // sps_max_latency_increase_plus1
|
||||
}
|
||||
for _ in 0..6 {
|
||||
r.ue()?; // coding/transform block sizes, transform hierarchy depths
|
||||
}
|
||||
if r.flag()? && r.flag()? {
|
||||
// scaling_list_enabled_flag, sps_scaling_list_data_present_flag
|
||||
scaling_list_data(r)?;
|
||||
}
|
||||
r.skip(2)?; // amp_enabled_flag, sample_adaptive_offset_enabled_flag
|
||||
if r.flag()? {
|
||||
// pcm_enabled_flag
|
||||
r.skip(8)?; // pcm sample bit depths
|
||||
r.ue()?;
|
||||
r.ue()?;
|
||||
r.skip(1)?; // pcm_loop_filter_disabled_flag
|
||||
}
|
||||
let sets = r.ue()? as usize;
|
||||
if sets > 64 {
|
||||
return None;
|
||||
}
|
||||
let mut num_delta_pocs = Vec::with_capacity(sets);
|
||||
for idx in 0..sets {
|
||||
let n = st_ref_pic_set(r, idx, &num_delta_pocs)?;
|
||||
num_delta_pocs.push(n);
|
||||
}
|
||||
if r.flag()? {
|
||||
// long_term_ref_pics_present_flag
|
||||
let n = r.ue()?;
|
||||
if n > 32 {
|
||||
return None;
|
||||
}
|
||||
for _ in 0..n {
|
||||
r.skip(log2_max_poc_lsb as usize + 1)?; // lt_ref_pic_poc_lsb_sps, used_by_curr_pic_lt_sps_flag
|
||||
}
|
||||
}
|
||||
r.skip(2)?; // sps_temporal_mvp_enabled_flag, strong_intra_smoothing_enabled_flag
|
||||
Some(())
|
||||
}
|
||||
|
||||
fn parse_vui(r: &mut Bits<'_>) -> Option<Signal> {
|
||||
let mut signal = Signal::default();
|
||||
if !r.flag()? {
|
||||
return Some(signal); // no VUI
|
||||
}
|
||||
if r.flag()? && r.u(8)? == 255 {
|
||||
// aspect_ratio_info_present_flag, aspect_ratio_idc == EXTENDED_SAR
|
||||
r.skip(32)?;
|
||||
}
|
||||
if r.flag()? {
|
||||
r.skip(1)?; // overscan_appropriate_flag
|
||||
}
|
||||
if r.flag()? {
|
||||
// video_signal_type_present_flag
|
||||
r.skip(3)?; // video_format
|
||||
signal.full_range = r.flag()?;
|
||||
if r.flag()? {
|
||||
signal.primaries = r.u(8)? as u8;
|
||||
signal.transfer = r.u(8)? as u8;
|
||||
signal.matrix = r.u(8)? as u8;
|
||||
}
|
||||
}
|
||||
Some(signal)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A bit writer for building parameter sets in tests.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Writer {
|
||||
pub bits: Vec<bool>,
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
pub fn u(&mut self, v: u32, n: u32) {
|
||||
for i in (0..n).rev() {
|
||||
self.bits.push(i < 32 && (v >> i) & 1 == 1);
|
||||
}
|
||||
}
|
||||
pub fn ue(&mut self, v: u32) {
|
||||
let x = u64::from(v) + 1;
|
||||
let len = 64 - x.leading_zeros();
|
||||
self.u(0, len - 1);
|
||||
for i in (0..len).rev() {
|
||||
self.bits.push((x >> i) & 1 == 1);
|
||||
}
|
||||
}
|
||||
pub fn bytes(mut self) -> Vec<u8> {
|
||||
self.bits.push(true); // rbsp_stop_one_bit
|
||||
while !self.bits.len().is_multiple_of(8) {
|
||||
self.bits.push(false);
|
||||
}
|
||||
self.bits.chunks(8).map(|c| c.iter().fold(0u8, |a, &b| (a << 1) | u8::from(b))).collect()
|
||||
}
|
||||
}
|
||||
|
||||
fn sps(vui: impl FnOnce(&mut Writer), st_sets: impl FnOnce(&mut Writer)) -> Vec<u8> {
|
||||
let mut w = Writer::default();
|
||||
w.u(0, 4);
|
||||
w.u(1, 3); // one sub-layer above the base
|
||||
w.u(1, 1);
|
||||
w.u(0, 88);
|
||||
w.u(93, 8);
|
||||
w.u(0b11, 2); // sub-layer profile and level present
|
||||
w.u(0, 2 * 7);
|
||||
w.u(0, 88);
|
||||
w.u(0, 8);
|
||||
w.ue(0);
|
||||
w.ue(1); // 4:2:0
|
||||
w.ue(4032);
|
||||
w.ue(3024);
|
||||
w.u(1, 1); // conformance window
|
||||
for _ in 0..4 {
|
||||
w.ue(3);
|
||||
}
|
||||
w.ue(0);
|
||||
w.ue(0);
|
||||
w.ue(4);
|
||||
w.u(1, 1);
|
||||
for _ in 0..2 {
|
||||
w.ue(1);
|
||||
w.ue(0);
|
||||
w.ue(0);
|
||||
}
|
||||
for v in [0, 3, 0, 3, 0, 0] {
|
||||
w.ue(v);
|
||||
}
|
||||
w.u(1, 1); // scaling lists, with data
|
||||
w.u(1, 1);
|
||||
for size_id in 0..4u32 {
|
||||
for _ in (0..6).step_by(if size_id == 3 { 3 } else { 1 }) {
|
||||
w.u(1, 1);
|
||||
if size_id > 1 {
|
||||
w.ue(0);
|
||||
}
|
||||
for _ in 0..64.min(1 << (4 + (size_id << 1))) {
|
||||
w.ue(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
w.u(0, 2);
|
||||
w.u(1, 1); // pcm
|
||||
w.u(0x77, 8);
|
||||
w.ue(0);
|
||||
w.ue(1);
|
||||
w.u(1, 1);
|
||||
st_sets(&mut w);
|
||||
w.u(1, 1); // long-term pictures
|
||||
w.ue(1);
|
||||
w.u(0, 8 + 1);
|
||||
w.u(0, 2);
|
||||
vui(&mut w);
|
||||
let mut nal = vec![SPS_NUT << 1, 1];
|
||||
// escape like a real NAL so `signal` has to unescape
|
||||
let mut zeros = 0;
|
||||
for b in w.bytes() {
|
||||
if zeros >= 2 && b <= 3 {
|
||||
nal.push(3);
|
||||
zeros = 0;
|
||||
}
|
||||
zeros = if b == 0 { zeros + 1 } else { 0 };
|
||||
nal.push(b);
|
||||
}
|
||||
nal
|
||||
}
|
||||
|
||||
fn two_rps(w: &mut Writer) {
|
||||
w.ue(2);
|
||||
w.ue(1); // set 0: one negative picture
|
||||
w.ue(0);
|
||||
w.ue(0);
|
||||
w.u(1, 1);
|
||||
w.u(1, 1); // set 1: predicted from set 0
|
||||
w.u(0, 1);
|
||||
w.ue(0);
|
||||
w.u(0, 1); // not used ...
|
||||
w.u(1, 1); // ... but kept
|
||||
w.u(1, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_full_range_and_matrix_from_the_vui() {
|
||||
let nal = sps(
|
||||
|w| {
|
||||
w.u(1, 1); // vui present
|
||||
w.u(1, 1);
|
||||
w.u(255, 8);
|
||||
w.u(0x0001_0001, 32);
|
||||
w.u(1, 1);
|
||||
w.u(0, 1);
|
||||
w.u(1, 1); // video_signal_type_present_flag
|
||||
w.u(5, 3);
|
||||
w.u(1, 1); // full range
|
||||
w.u(1, 1);
|
||||
w.u(12, 8);
|
||||
w.u(13, 8);
|
||||
w.u(6, 8);
|
||||
},
|
||||
two_rps,
|
||||
);
|
||||
let s = signal(&[&[0x40, 1, 0xc], &nal, &[0x44, 1]]);
|
||||
assert_eq!(s, Some(Signal { full_range: true, primaries: 12, transfer: 13, matrix: 6 }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn absent_fields_take_the_standard_defaults() {
|
||||
let no_vui = sps(|w| w.u(0, 1), |w| w.ue(0));
|
||||
assert_eq!(signal(&[&no_vui]), Some(Signal::default()));
|
||||
let range_only = sps(
|
||||
|w| {
|
||||
w.u(1, 1);
|
||||
w.u(0, 2);
|
||||
w.u(1, 1);
|
||||
w.u(5, 3);
|
||||
w.u(1, 1);
|
||||
w.u(0, 1); // no colour description
|
||||
},
|
||||
|w| w.ue(0),
|
||||
);
|
||||
assert_eq!(signal(&[&range_only]), Some(Signal { full_range: true, ..Signal::default() }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_or_truncated_sps_is_none() {
|
||||
assert_eq!(signal(&[]), None);
|
||||
assert_eq!(signal(&[&[0x40, 1, 2, 3]]), None);
|
||||
let nal = sps(|w| w.u(0, 1), |w| w.ue(0));
|
||||
for cut in 2..nal.len() - 2 {
|
||||
// never panics; most cuts are None
|
||||
let _ = signal(&[nal.get(..cut).unwrap_or_default()]);
|
||||
}
|
||||
assert_eq!(signal(&[&[SPS_NUT << 1, 1, 0xff, 0xff, 0xff]]), None);
|
||||
}
|
||||
}
|
||||
@@ -1,352 +0,0 @@
|
||||
//! YCbCr → RGB for decoded HEVC pictures, done here rather than in heic-rs so the result matches
|
||||
//! libheif (the reference every HEIF tool is checked against) to within one code value:
|
||||
//!
|
||||
//! - **Which matrix and range.** The item's `colr` `nclx` box when there is one; otherwise the
|
||||
//! HEVC VUI ([`crate::vui`]); otherwise the H.265 defaults (limited range). An unspecified matrix
|
||||
//! is BT.601, as libheif reads it. heic-rs 0.1.1 ignores the VUI and assumes BT.709 limited
|
||||
//! range for every file without `nclx`, which shifts every iPhone photo (full range, ICC only)
|
||||
//! by about 8 code values.
|
||||
//! - **Chroma upsampling.** Nearest neighbour (each chroma sample covers its 2×2 or 2×1 luma
|
||||
//! block), as libheif does by default. libheif switches to a centred bilinear filter (weights
|
||||
//! 9/3/3/1 sixteenths, in integers) when a `clap` crop starts at an odd column or row; so does
|
||||
//! [`Upsampling::Bilinear`].
|
||||
//!
|
||||
//! The arithmetic is floating point and the result 16-bit, whatever the coded depth, so the
|
||||
//! develop pipeline gets every bit the conversion produces.
|
||||
|
||||
use heic_rs::grid::Mosaic;
|
||||
use heic_rs::hevc::{ChromaFormat, Frame};
|
||||
|
||||
/// The coded pictures one image is made of.
|
||||
pub enum Planes<'a> {
|
||||
Picture(&'a Frame),
|
||||
Grid(Mosaic<'a>),
|
||||
}
|
||||
|
||||
impl Planes<'_> {
|
||||
pub fn size(&self) -> (usize, usize) {
|
||||
match self {
|
||||
Planes::Picture(f) => (f.width as usize, f.height as usize),
|
||||
Planes::Grid(m) => (m.width() as usize, m.height() as usize),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bit_depth(&self) -> u8 {
|
||||
match self {
|
||||
Planes::Picture(f) => f.bit_depth,
|
||||
Planes::Grid(m) => m.bit_depth(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn chroma(&self) -> ChromaFormat {
|
||||
match self {
|
||||
Planes::Picture(f) => f.chroma,
|
||||
Planes::Grid(m) => m.chroma(),
|
||||
}
|
||||
}
|
||||
|
||||
fn chroma_width(&self) -> usize {
|
||||
let (w, h) = self.size();
|
||||
self.chroma().chroma_size(w as u32, h as u32).0 as usize
|
||||
}
|
||||
|
||||
fn luma_row(&self, y: usize, dst: &mut [u16]) -> Option<()> {
|
||||
match self {
|
||||
Planes::Picture(f) => copy_row(&f.y, f.y_stride as usize, y, dst),
|
||||
Planes::Grid(m) => m.luma_row(y, dst),
|
||||
}
|
||||
}
|
||||
|
||||
fn chroma_row(&self, cr: bool, r: usize, dst: &mut [u16]) -> Option<()> {
|
||||
match self {
|
||||
Planes::Picture(f) => copy_row(if cr { &f.cr } else { &f.cb }, f.c_stride as usize, r, dst),
|
||||
Planes::Grid(m) => m.chroma_row(cr, r, dst),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn copy_row(plane: &[u16], stride: usize, y: usize, dst: &mut [u16]) -> Option<()> {
|
||||
let start = y.checked_mul(stride)?;
|
||||
dst.copy_from_slice(plane.get(start..start.checked_add(dst.len())?)?);
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// How the three coded channels map to R, G, B (H.273 `MatrixCoefficients`).
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
enum Matrix {
|
||||
/// 0: the channels are G, B, R.
|
||||
Identity,
|
||||
/// 8: YCgCo.
|
||||
YCgCo,
|
||||
/// Every Kr/Kb matrix: R = Y + `cr_r`·Cr, G = Y − `cb_g`·Cb − `cr_g`·Cr, B = Y + `cb_b`·Cb.
|
||||
Kr { cr_r: f32, cb_g: f32, cr_g: f32, cb_b: f32 },
|
||||
}
|
||||
|
||||
impl Matrix {
|
||||
/// Unknown, reserved and unspecified codes are BT.601, like libheif.
|
||||
fn from_code(code: u16) -> Matrix {
|
||||
let (kr, kb) = match code {
|
||||
0 => return Matrix::Identity,
|
||||
8 => return Matrix::YCgCo,
|
||||
1 => (0.2126, 0.0722),
|
||||
4 => (0.30, 0.11),
|
||||
7 => (0.212, 0.087),
|
||||
// 10 (BT.2020 constant luminance) is approximated by its non-constant matrix.
|
||||
9 | 10 => (0.2627, 0.0593),
|
||||
_ => (0.299, 0.114),
|
||||
};
|
||||
let kg = 1.0 - kr - kb;
|
||||
Matrix::Kr { cr_r: 2.0 * (1.0 - kr), cb_g: 2.0 * kb * (1.0 - kb) / kg, cr_g: 2.0 * kr * (1.0 - kr) / kg, cb_b: 2.0 * (1.0 - kb) }
|
||||
}
|
||||
}
|
||||
|
||||
/// How the samples are to be read: the matrix and the range.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Signal {
|
||||
pub matrix: u16,
|
||||
pub full_range: bool,
|
||||
}
|
||||
|
||||
/// How subsampled chroma is brought to the luma resolution.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Upsampling {
|
||||
Nearest,
|
||||
/// Centred bilinear: chroma sits between its two luma columns (and rows for 4:2:0).
|
||||
Bilinear,
|
||||
}
|
||||
|
||||
/// One chroma row brought to the full width `w` of luma row `y`.
|
||||
fn chroma_at(planes: &Planes<'_>, cr: bool, y: usize, w: usize, mode: Upsampling, buf: &mut [u16], next: &mut [u16]) -> Option<Vec<u16>> {
|
||||
let chroma = planes.chroma();
|
||||
let (xs, ys) = (chroma.x_shift(), chroma.y_shift());
|
||||
let (wf, hf) = planes.size();
|
||||
let rows = chroma.chroma_size(wf as u32, hf as u32).1 as usize;
|
||||
let cy = y >> ys;
|
||||
planes.chroma_row(cr, cy, buf)?;
|
||||
let last = buf.len().checked_sub(1)?;
|
||||
if mode == Upsampling::Nearest || xs == 0 {
|
||||
return (0..w).map(|x| buf.get(x >> xs).copied()).collect();
|
||||
}
|
||||
// The nearer neighbour on each axis takes a quarter of the weight.
|
||||
let ny = if ys == 0 {
|
||||
cy
|
||||
} else if y.is_multiple_of(2) {
|
||||
cy.saturating_sub(1)
|
||||
} else {
|
||||
(cy + 1).min(rows.checked_sub(1)?)
|
||||
};
|
||||
planes.chroma_row(cr, ny, next)?;
|
||||
(0..w)
|
||||
.map(|x| {
|
||||
let cx = x >> 1;
|
||||
let nx = if x % 2 == 0 { cx.saturating_sub(1) } else { (cx + 1).min(last) };
|
||||
let (a, an) = (u32::from(*buf.get(cx)?), u32::from(*buf.get(nx)?));
|
||||
let v = if ys == 0 {
|
||||
(3 * a + an + 2) >> 2
|
||||
} else {
|
||||
let (b, bn) = (u32::from(*next.get(cx)?), u32::from(*next.get(nx)?));
|
||||
(9 * a + 3 * b + 3 * an + bn + 8) >> 4
|
||||
};
|
||||
u16::try_from(v).ok()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Converts `planes` to interleaved 16-bit RGB, or RGBA with room left for alpha (filled with
|
||||
/// 65535 here). `None` when the planes can't supply a row they promised (a malformed grid).
|
||||
pub fn to_rgb16(planes: &Planes<'_>, signal: Signal, alpha: bool, mode: Upsampling) -> Option<Vec<u16>> {
|
||||
let (w, h) = planes.size();
|
||||
let ch = if alpha { 4 } else { 3 };
|
||||
let bd = u32::from(planes.bit_depth());
|
||||
if !(8..=16).contains(&bd) || w == 0 || h == 0 {
|
||||
return None;
|
||||
}
|
||||
// The default caller limit is 2²8 pixels, at most four channels per pixel, so the largest
|
||||
// legitimate `w·h·ch` is 2²30 samples. Anything more is a malformed or hostile canvas the
|
||||
// decode path would have refused before calling here (CodeRabbit #48): refuse rather than
|
||||
// ask for a multi-gigabuffer (or worse, over usize).
|
||||
if w.checked_mul(h)?.checked_mul(ch)? > 1 << 30 {
|
||||
return None;
|
||||
}
|
||||
let max = ((1u32 << bd) - 1) as f32;
|
||||
let half = (1u32 << (bd - 1)) as f32;
|
||||
let scale = (1u32 << (bd - 8)) as f32;
|
||||
// Normalised Y = (v − y_off)·y_mul, Cb/Cr = (v − half)·c_mul.
|
||||
let (y_off, y_mul, c_mul) =
|
||||
if signal.full_range { (0.0, 1.0 / max, 1.0 / max) } else { (16.0 * scale, 1.0 / (219.0 * scale), 1.0 / (224.0 * scale)) };
|
||||
let matrix = Matrix::from_code(signal.matrix);
|
||||
let mono = planes.chroma() == ChromaFormat::Monochrome;
|
||||
let cw = planes.chroma_width();
|
||||
|
||||
let mut out = vec![0u16; w.checked_mul(h)?.checked_mul(ch)?];
|
||||
let row = |y: usize, dst: &mut [u16]| -> Option<()> {
|
||||
let mut luma = vec![0u16; w];
|
||||
planes.luma_row(y, &mut luma)?;
|
||||
let (cb, cr) = if mono {
|
||||
(Vec::new(), Vec::new())
|
||||
} else {
|
||||
let (mut buf, mut next) = (vec![0u16; cw], vec![0u16; cw]);
|
||||
(chroma_at(planes, false, y, w, mode, &mut buf, &mut next)?, chroma_at(planes, true, y, w, mode, &mut buf, &mut next)?)
|
||||
};
|
||||
for (x, (px, &yv)) in dst.chunks_exact_mut(ch).zip(&luma).enumerate() {
|
||||
let rgb = if mono {
|
||||
let v = (f32::from(yv) - y_off) * y_mul;
|
||||
[v, v, v]
|
||||
} else {
|
||||
let (u, v) = (f32::from(*cb.get(x)?), f32::from(*cr.get(x)?));
|
||||
match matrix {
|
||||
// G, B, R all read like luma.
|
||||
Matrix::Identity => [(v - y_off) * y_mul, (f32::from(yv) - y_off) * y_mul, (u - y_off) * y_mul],
|
||||
Matrix::YCgCo => {
|
||||
let (yn, cg, co) = ((f32::from(yv) - y_off) * y_mul, (u - half) * c_mul, (v - half) * c_mul);
|
||||
let t = yn - cg;
|
||||
[t + co, yn + cg, t - co]
|
||||
}
|
||||
Matrix::Kr { cr_r, cb_g, cr_g, cb_b } => {
|
||||
let (yn, u, v) = ((f32::from(yv) - y_off) * y_mul, (u - half) * c_mul, (v - half) * c_mul);
|
||||
[yn + cr_r * v, yn - cb_g * u - cr_g * v, yn + cb_b * u]
|
||||
}
|
||||
}
|
||||
};
|
||||
for (o, c) in px.iter_mut().zip(rgb) {
|
||||
*o = (c.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16;
|
||||
}
|
||||
if alpha && let Some(a) = px.get_mut(3) {
|
||||
*a = u16::MAX;
|
||||
}
|
||||
}
|
||||
Some(())
|
||||
};
|
||||
let row_len = w * ch;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let ok = {
|
||||
use rayon::prelude::*;
|
||||
out.par_chunks_mut(row_len).enumerate().all(|(y, dst)| row(y, dst).is_some())
|
||||
};
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let ok = out.chunks_mut(row_len).enumerate().all(|(y, dst)| row(y, dst).is_some());
|
||||
ok.then_some(out)
|
||||
}
|
||||
|
||||
/// Writes an alpha picture's luma, full range, into channel 3 of `rgba`.
|
||||
pub fn put_alpha(planes: &Planes<'_>, rgba: &mut [u16]) -> Option<()> {
|
||||
let (w, h) = planes.size();
|
||||
let max = ((1u32 << u32::from(planes.bit_depth()).clamp(1, 16)) - 1) as f32;
|
||||
let mut luma = vec![0u16; w];
|
||||
for (y, row) in rgba.chunks_exact_mut(w * 4).enumerate().take(h) {
|
||||
planes.luma_row(y, &mut luma)?;
|
||||
for (px, &a) in row.as_chunks_mut::<4>().0.iter_mut().zip(&luma) {
|
||||
let v = (f32::from(a) / max).clamp(0.0, 1.0);
|
||||
px[3] = (v * 65535.0 + 0.5) as u16;
|
||||
}
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn frame(w: u32, h: u32, bd: u8, chroma: ChromaFormat, y: u16, cb: u16, cr: u16) -> Frame {
|
||||
let (cw, chh) = chroma.chroma_size(w, h);
|
||||
let n = (cw * chh) as usize;
|
||||
Frame {
|
||||
width: w,
|
||||
height: h,
|
||||
bit_depth: bd,
|
||||
chroma,
|
||||
y: vec![y; (w * h) as usize],
|
||||
cb: vec![cb; n],
|
||||
cr: vec![cr; n],
|
||||
y_stride: w,
|
||||
c_stride: cw,
|
||||
}
|
||||
}
|
||||
|
||||
fn rgb(f: &Frame, matrix: u16, full_range: bool) -> [u16; 3] {
|
||||
let v = to_rgb16(&Planes::Picture(f), Signal { matrix, full_range }, false, Upsampling::Nearest).unwrap();
|
||||
[v[0], v[1], v[2]]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn neutral_and_primaries_convert_exactly() {
|
||||
// Full-range grey stays grey; limited-range black and white reach the ends.
|
||||
assert_eq!(rgb(&frame(4, 4, 8, ChromaFormat::Yuv420, 128, 128, 128), 6, true), [128 * 257; 3]);
|
||||
assert_eq!(rgb(&frame(4, 4, 8, ChromaFormat::Yuv420, 16, 128, 128), 1, false), [0; 3]);
|
||||
assert_eq!(rgb(&frame(4, 4, 8, ChromaFormat::Yuv420, 235, 128, 128), 1, false), [65535; 3]);
|
||||
assert_eq!(rgb(&frame(4, 4, 10, ChromaFormat::Yuv420, 940, 512, 512), 9, false), [65535; 3]);
|
||||
// BT.601 full-range pure red: Y = 76.245, Cb = 84.97, Cr = 255.5 → (255, 0, 0) within rounding.
|
||||
let red = rgb(&frame(2, 2, 8, ChromaFormat::Yuv444, 76, 85, 255), 6, true);
|
||||
assert!(red[0] > 65000 && red[1] < 300 && red[2] < 300, "{red:?}");
|
||||
// Identity: the channels are G, B, R.
|
||||
assert_eq!(rgb(&frame(2, 2, 8, ChromaFormat::Yuv444, 10, 20, 30), 0, true), [30 * 257, 10 * 257, 20 * 257]);
|
||||
// Monochrome.
|
||||
assert_eq!(rgb(&frame(2, 2, 8, ChromaFormat::Monochrome, 51, 0, 0), 6, true), [51 * 257; 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chroma_is_nearest_neighbour() {
|
||||
// 4:2:0, 4×2 luma, chroma 2×1: the left chroma sample covers columns 0–1 only.
|
||||
let mut f = frame(4, 2, 8, ChromaFormat::Yuv420, 128, 128, 128);
|
||||
f.cr = vec![200, 128];
|
||||
let v = to_rgb16(&Planes::Picture(&f), Signal { matrix: 6, full_range: true }, false, Upsampling::Nearest).unwrap();
|
||||
let red: Vec<u16> = v.chunks(3).map(|p| p[0]).collect();
|
||||
assert!(red[0] == red[1] && red[1] == red[4] && red[0] > red[2] && red[2] == 128 * 257, "{red:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bilinear_chroma_is_centred_9_3_3_1() {
|
||||
// 4:2:0, 4×4 luma, chroma 2×2 with one bright corner sample.
|
||||
let mut f = frame(4, 4, 8, ChromaFormat::Yuv420, 128, 128, 128);
|
||||
f.cr = vec![200, 128, 128, 128];
|
||||
let p = Planes::Picture(&f);
|
||||
let (mut buf, mut next) = (vec![0; 2], vec![0; 2]);
|
||||
let rows: Vec<Vec<u16>> = (0..4).map(|y| chroma_at(&p, true, y, 4, Upsampling::Bilinear, &mut buf, &mut next).unwrap()).collect();
|
||||
// Row 0 sits nearest chroma row 0 (and clamps upward); row 1 blends a quarter of row 1.
|
||||
assert_eq!(rows[0], [200, 182, 146, 128]);
|
||||
assert_eq!(rows[1], [182, 169, 142, 128]);
|
||||
assert_eq!(rows[2], [146, 142, 133, 128]);
|
||||
assert_eq!(rows[3], [128; 4]);
|
||||
assert_eq!(chroma_at(&p, true, 1, 4, Upsampling::Nearest, &mut buf, &mut next).unwrap(), [200, 200, 128, 128]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_and_bad_planes() {
|
||||
let f = frame(2, 2, 8, ChromaFormat::Yuv420, 128, 128, 128);
|
||||
let mut v = to_rgb16(&Planes::Picture(&f), Signal { matrix: 6, full_range: true }, true, Upsampling::Nearest).unwrap();
|
||||
assert_eq!(v[3], 65535);
|
||||
put_alpha(&Planes::Picture(&frame(2, 2, 8, ChromaFormat::Monochrome, 51, 0, 0)), &mut v).unwrap();
|
||||
assert_eq!(v[3], 51 * 257);
|
||||
let mut short = frame(4, 4, 8, ChromaFormat::Yuv420, 128, 128, 128);
|
||||
short.y.truncate(5);
|
||||
assert!(to_rgb16(&Planes::Picture(&short), Signal { matrix: 6, full_range: true }, false, Upsampling::Bilinear).is_none());
|
||||
assert!(
|
||||
to_rgb16(
|
||||
&Planes::Picture(&frame(2, 2, 7, ChromaFormat::Yuv420, 0, 0, 0)),
|
||||
Signal { matrix: 6, full_range: true },
|
||||
false,
|
||||
Upsampling::Nearest
|
||||
)
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
|
||||
/// CodeRabbit #48: a plane claiming 500 000 × 500 000 pixels must be refused without ever
|
||||
/// allocating for it (before the cap this vec was never built; upper-bounding allocation
|
||||
/// would have been a 7.5-gibabuffer or a panic instead of an error).
|
||||
#[test]
|
||||
fn an_oversized_canvas_is_refused_without_allocation() {
|
||||
let f = Frame {
|
||||
width: 500_000,
|
||||
height: 500_000,
|
||||
bit_depth: 8,
|
||||
chroma: ChromaFormat::Yuv420,
|
||||
y: vec![128],
|
||||
cb: vec![128; 4],
|
||||
cr: vec![128; 4],
|
||||
y_stride: 0,
|
||||
c_stride: 0,
|
||||
};
|
||||
assert!(to_rgb16(&Planes::Picture(&f), Signal { matrix: 6, full_range: true }, false, Upsampling::Nearest).is_none());
|
||||
}
|
||||
}
|
||||
@@ -169,6 +169,10 @@ pub fn membrane_fill(w: usize, h: usize, ch: usize, img: &[f32], hole: &[bool])
|
||||
if Some(hole.len()) != w.checked_mul(h) || Some(img.len()) != hole.len().checked_mul(ch) {
|
||||
return img.to_vec();
|
||||
}
|
||||
// no pixels: nothing to fill (and `ch` would otherwise size one plane per channel, whatever it claims)
|
||||
if hole.is_empty() {
|
||||
return img.to_vec();
|
||||
}
|
||||
per_channel(w, h, ch, |c| {
|
||||
let mut v: Vec<f32> = (0..w * h).map(|i| if hole[i] { 0.0 } else { img[i * ch + c] }).collect();
|
||||
solve_membrane(w, h, hole, &mut v);
|
||||
@@ -183,6 +187,12 @@ mod tests {
|
||||
(0..w * h).map(|i| ((i % w) as f32 - cx).hypot((i / w) as f32 - cy) < r).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_zero_area_image_comes_back_unchanged_whatever_its_channel_count() {
|
||||
assert!(membrane_fill(0, 5, usize::MAX, &[], &[]).is_empty());
|
||||
assert!(membrane_fill(3, 0, 1 << 40, &[], &[]).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn membrane_reproduces_linear_boundary() {
|
||||
// A linear function is harmonic: the solve must reproduce it exactly inside the hole.
|
||||
|
||||
@@ -53,13 +53,21 @@ fn entity(name: &str) -> Option<char> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Index of the `;` that ends a reference starting at `rest` (its `&`). The scan stops at the first
|
||||
/// byte that cannot appear in a name or a numeric reference, so zero-padded references such as
|
||||
/// `a` are found and each run of name bytes is read once, not once per `&`.
|
||||
fn entity_end(rest: &str) -> Option<usize> {
|
||||
let n = rest.bytes().skip(1).take_while(|b| b.is_ascii_alphanumeric() || *b == b'#').count();
|
||||
(rest.as_bytes().get(n + 1) == Some(&b';')).then_some(n + 1)
|
||||
}
|
||||
|
||||
fn unescape(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len());
|
||||
let mut rest = s;
|
||||
while let Some(i) = rest.find('&') {
|
||||
out.push_str(rest.get(..i).unwrap_or_default());
|
||||
let tail = rest.get(i..).unwrap_or_default();
|
||||
match tail.find(';').and_then(|j| entity(tail.get(1..j)?).map(|c| (c, j))) {
|
||||
match entity_end(tail).and_then(|j| entity(tail.get(1..j)?).map(|c| (c, j))) {
|
||||
Some((c, j)) => {
|
||||
out.push(c);
|
||||
rest = tail.get(j + 1..).unwrap_or_default();
|
||||
@@ -140,6 +148,20 @@ fn close(stack: &mut Vec<Node>) {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn many_ampersands_before_one_semicolon_are_read_once() {
|
||||
// 200 000 `&` then a `;`: each `&` once searched the whole rest for the `;`
|
||||
let hostile = format!("{};&", "&".repeat(200_000));
|
||||
assert_eq!(unescape(&hostile), format!("{};&", "&".repeat(200_000)));
|
||||
assert_eq!(unescape("<a>  &unknownreference;"), "<a> \u{10FFFF} &unknownreference;");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_padded_numeric_references_decode() {
|
||||
assert_eq!(unescape("Canon A A"), "Canon A A");
|
||||
assert_eq!(unescape("&abc&#xyz;&;"), "&abc&#xyz;&;");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entity_rejects_signed_numeric_references() {
|
||||
assert_eq!(entity("#+65"), None);
|
||||
|
||||
@@ -119,6 +119,10 @@ pub fn merge_hdr(mut frames: Vec<Frame>, opts: &HdrOptions, progress: &Progress)
|
||||
if frames.iter().any(|f| f.width() != w || f.height() != h) {
|
||||
return Err(MergeError::Mismatch("HDR merge needs photos of the same size".into()));
|
||||
}
|
||||
// every buffer must hold exactly its frame's pixels before anything is sized from the dimensions
|
||||
if frames.iter().any(|f| w.checked_mul(h) != Some(f.image.data.len())) {
|
||||
return Err(MergeError::Mismatch("an HDR frame's pixels do not match its size".into()));
|
||||
}
|
||||
if frames.iter().any(|f| f.orientation != frames[0].orientation) {
|
||||
for f in frames.iter_mut() {
|
||||
f.orient();
|
||||
|
||||
@@ -9,7 +9,7 @@ description = "LightCraft metadata: EXIF, XMP, IPTC (L1)"
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
lightcraft-tiff = { workspace = true }
|
||||
craft-tiff = { workspace = true }
|
||||
lightcraft-geom = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
quick-xml = { workspace = true }
|
||||
@@ -17,6 +17,6 @@ chrono = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
# Lossless synthetic HEIC files: the HEIF item reader is proved against a real container writer.
|
||||
lightcraft-heif = { workspace = true, features = ["testdata"] }
|
||||
craft-heif = { workspace = true, features = ["testdata"] }
|
||||
proptest = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
@@ -14,7 +14,7 @@
|
||||
//! - HEIF/HEIC/AVIF (ISO base media file, see [`crate::heif`]): the `Exif` item's TIFF stream and the XMP
|
||||
//! `mime` item.
|
||||
|
||||
use lightcraft_tiff::{Tiff, tags};
|
||||
use craft_tiff::{Tiff, tags};
|
||||
|
||||
/// Metadata blocks found in a file.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
@@ -71,8 +71,8 @@ pub fn embedded(bytes: &[u8]) -> Embedded {
|
||||
xmp_extended: None,
|
||||
icc: ifd0.bytes(tags::ICC_PROFILE).map(|b| b.to_vec()),
|
||||
iptc: ifd0.value(tags::IPTC_NAA).map(|v| match v {
|
||||
lightcraft_tiff::Value::Byte(b) | lightcraft_tiff::Value::Undefined(b) => b.clone(),
|
||||
other => lightcraft_tiff::writer::encode(t.order, other),
|
||||
craft_tiff::Value::Byte(b) | craft_tiff::Value::Undefined(b) => b.clone(),
|
||||
other => craft_tiff::writer::encode(t.order, other),
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
@@ -275,7 +275,7 @@ pub fn webp_chunks(b: &[u8]) -> Embedded {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::exif::tests::sample_exif;
|
||||
use lightcraft_tiff::ByteOrder;
|
||||
use craft_tiff::ByteOrder;
|
||||
|
||||
fn seg(marker: u8, payload: &[u8]) -> Vec<u8> {
|
||||
let mut v = vec![0xff, marker];
|
||||
|
||||
@@ -436,13 +436,13 @@ fn le32(b: &[u8], at: usize) -> Option<u32> {
|
||||
/// Merge `CMT1` (IFD0), `CMT2` (Exif) and `CMT4` (GPS) into one TIFF stream, so the Exif readers see a CR3
|
||||
/// like any TIFF-based raw. The maker note (`CMT3`) is left out: its offsets are relative to its own block.
|
||||
pub fn merged_exif(c: &Cr3<'_>) -> Option<Vec<u8>> {
|
||||
use lightcraft_tiff::{IfdBuilder, Tiff, TiffWriter, tags as t};
|
||||
use craft_tiff::{IfdBuilder, Tiff, TiffWriter, tags as t};
|
||||
let ifd0 = Tiff::parse(c.cmt[0]?).ok()?;
|
||||
let order = ifd0.order;
|
||||
let first = |i: usize| c.cmt[i].and_then(|b| Tiff::parse(b).ok()).and_then(|t| t.ifds.into_iter().next());
|
||||
// pointers and offsets that would dangle once the IFDs move
|
||||
const SKIP: [u16; 9] = [t::EXIF_IFD, t::GPS_IFD, t::INTEROP_IFD, t::SUB_IFDS, t::MAKER_NOTE, 273, 279, 513, 514];
|
||||
let build = |ifd: &lightcraft_tiff::Ifd| {
|
||||
let build = |ifd: &craft_tiff::Ifd| {
|
||||
let mut b = IfdBuilder::new();
|
||||
for e in ifd.entries.iter().filter(|e| !SKIP.contains(&e.tag)) {
|
||||
b.set(e.tag, e.value.clone());
|
||||
@@ -497,14 +497,14 @@ mod tests {
|
||||
let stbl = [full(b"stsd", &stsd), full(b"stsz", &stsz), full(b"co64", &co64)].concat();
|
||||
bx(b"trak", &bx(b"mdia", &bx(b"minf", &bx(b"stbl", &stbl))))
|
||||
}
|
||||
fn tiff_with(tag: u16, v: lightcraft_tiff::Value) -> Vec<u8> {
|
||||
let b = lightcraft_tiff::IfdBuilder::new().with(tag, v);
|
||||
lightcraft_tiff::TiffWriter::new(lightcraft_tiff::ByteOrder::Little, false).write(&[b]).unwrap()
|
||||
fn tiff_with(tag: u16, v: craft_tiff::Value) -> Vec<u8> {
|
||||
let b = craft_tiff::IfdBuilder::new().with(tag, v);
|
||||
craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[b]).unwrap()
|
||||
}
|
||||
|
||||
/// A small synthetic CR3: Canon uuid with CMT1/CMT2/CMT4, a JPEG track, a raw track, XMP.
|
||||
pub(crate) fn sample() -> Vec<u8> {
|
||||
use lightcraft_tiff::{Value, tags as t};
|
||||
use craft_tiff::{Value, tags as t};
|
||||
let cmt1 = tiff_with(t::MODEL, Value::Ascii("Canon EOS Test".into()));
|
||||
let cmt2 = tiff_with(t::ISO_SPEED, Value::Short(vec![400]));
|
||||
let cmt4 = tiff_with(1, Value::Ascii("N".into()));
|
||||
@@ -689,7 +689,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn timed_metadata_exposes_the_dynamic_maker_note() {
|
||||
use lightcraft_tiff::{Value, tags as t};
|
||||
use craft_tiff::{Value, tags as t};
|
||||
let exif = tiff_with(t::ISO_SPEED, Value::Short(vec![800]));
|
||||
let maker = tiff_with(0x4001, Value::Short(vec![48, 100, 200]));
|
||||
let item = |tag: u32, data: &[u8]| {
|
||||
|
||||
+13
-13
@@ -1,7 +1,7 @@
|
||||
//! Exif / TIFF tags → [`Metadata`] (CIPA DC-008 Exif 2.32, TIFF 6.0, DNG 1.7 tag semantics).
|
||||
|
||||
use crate::{DateTime, Flash, Gps, Metadata, Orientation};
|
||||
use lightcraft_tiff::{Ifd, ParseOptions, Tiff, TiffError, tags as t};
|
||||
use craft_tiff::{Ifd, ParseOptions, Tiff, TiffError, tags as t};
|
||||
|
||||
/// Strip an `Exif\0\0` (or `Exif\0\xff`) prefix as found in JPEG APP1 and some WebP `EXIF` chunks.
|
||||
pub fn strip_exif_header(b: &[u8]) -> &[u8] {
|
||||
@@ -129,8 +129,8 @@ pub fn from_tiff(tiff: &Tiff) -> Metadata {
|
||||
if let Some(iptc) = ifd0.and_then(|i| i.value(t::IPTC_NAA)) {
|
||||
// IPTC-NAA is often typed LONG; re-serialise to bytes in file order.
|
||||
let bytes = match iptc {
|
||||
lightcraft_tiff::Value::Byte(b) | lightcraft_tiff::Value::Undefined(b) => b.clone(),
|
||||
other => lightcraft_tiff::writer::encode(tiff.order, other),
|
||||
craft_tiff::Value::Byte(b) | craft_tiff::Value::Undefined(b) => b.clone(),
|
||||
other => craft_tiff::writer::encode(tiff.order, other),
|
||||
};
|
||||
m.fill_missing(&crate::parse_iptc(&bytes));
|
||||
}
|
||||
@@ -200,7 +200,7 @@ fn panasonic_lens(data: &[u8], tiff: &Tiff) -> Option<String> {
|
||||
}
|
||||
let e = exif.get(t::MAKER_NOTE)?;
|
||||
let make = tiff.ifds.first().and_then(|i| i.string(t::MAKE)).unwrap_or_default();
|
||||
let mn = lightcraft_tiff::makernote::parse_makernote(data, e.offset, e.count() as u64, tiff.order, &make)?;
|
||||
let mn = craft_tiff::makernote::parse_makernote(data, e.offset, e.count() as u64, tiff.order, &make)?;
|
||||
text(Some(&mn.ifd), PANASONIC_LENS_TYPE)
|
||||
}
|
||||
|
||||
@@ -225,7 +225,7 @@ fn canon_maker_note_lens(data: &[u8], tiff: &Tiff) -> Option<String> {
|
||||
}
|
||||
let exif = tiff.exif()?;
|
||||
let e = exif.get(t::MAKER_NOTE)?;
|
||||
let mn = lightcraft_tiff::makernote::parse_makernote(data, e.offset, e.count() as u64, tiff.order, &make)?;
|
||||
let mn = craft_tiff::makernote::parse_makernote(data, e.offset, e.count() as u64, tiff.order, &make)?;
|
||||
text(Some(&mn.ifd), CANON_LENS_MODEL)
|
||||
}
|
||||
|
||||
@@ -234,13 +234,13 @@ fn canon_maker_note_lens(data: &[u8], tiff: &Tiff) -> Option<String> {
|
||||
const NIKON_LENS: u16 = 0x0084;
|
||||
|
||||
/// The lens a Nikon camera records in its maker note, when it's plausible.
|
||||
fn nikon_lens_spec(exif: Option<&Ifd>, make: Option<&str>, order: lightcraft_tiff::ByteOrder) -> Option<[f64; 4]> {
|
||||
fn nikon_lens_spec(exif: Option<&Ifd>, make: Option<&str>, order: craft_tiff::ByteOrder) -> Option<[f64; 4]> {
|
||||
let make = make?;
|
||||
if !make.trim().to_ascii_uppercase().starts_with("NIKON") {
|
||||
return None;
|
||||
}
|
||||
let note = exif?.bytes(t::MAKER_NOTE)?;
|
||||
let mn = lightcraft_tiff::makernote::parse_makernote(note, 0, note.len() as u64, order, make)?;
|
||||
let mn = craft_tiff::makernote::parse_makernote(note, 0, note.len() as u64, order, make)?;
|
||||
let s = mn.ifd.f64s(NIKON_LENS)?;
|
||||
let [short, long, wide, narrow] = s.as_slice() else { return None };
|
||||
let focal = |f: f64| f.is_finite() && f > 0.0 && f < 10_000.0;
|
||||
@@ -251,7 +251,7 @@ fn nikon_lens_spec(exif: Option<&Ifd>, make: Option<&str>, order: lightcraft_tif
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value};
|
||||
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value};
|
||||
|
||||
fn capture_time_tiff(original: Option<&str>, digitized: Option<&str>, ifd0_time: Option<&str>, xmp: Option<&str>) -> Vec<u8> {
|
||||
let mut exif = IfdBuilder::new();
|
||||
@@ -602,19 +602,19 @@ pub(crate) mod tests {
|
||||
}
|
||||
|
||||
/// An exact `1/N` rational for a sub-second value whose reciprocal is a whole number (shutter speeds such as
|
||||
/// 1/12800 s, which [`lightcraft_tiff::writer::rational`]'s decimal denominators cannot represent).
|
||||
fn reciprocal(v: Option<f64>) -> Option<lightcraft_tiff::Value> {
|
||||
/// 1/12800 s, which [`craft_tiff::writer::rational`]'s decimal denominators cannot represent).
|
||||
fn reciprocal(v: Option<f64>) -> Option<craft_tiff::Value> {
|
||||
let v = v.filter(|v| v.is_finite() && *v > 0.0 && *v < 1.0)?;
|
||||
let inv = 1.0 / v;
|
||||
((inv - inv.round()).abs() < 1e-6 && inv.round() <= u32::MAX as f64).then(|| lightcraft_tiff::Value::Rational(vec![(1, inv.round() as u32)]))
|
||||
((inv - inv.round()).abs() < 1e-6 && inv.round() <= u32::MAX as f64).then(|| craft_tiff::Value::Rational(vec![(1, inv.round() as u32)]))
|
||||
}
|
||||
|
||||
/// Serialise the interchange subset of `meta` as a TIFF-structured Exif block (little-endian, no
|
||||
/// `Exif\0\0` prefix): IFD0 (make, model, software, artist, copyright, description, orientation,
|
||||
/// date), the Exif IFD (capture settings, lens) and, when present, the GPS IFD.
|
||||
pub fn write_exif(meta: &Metadata) -> Vec<u8> {
|
||||
use lightcraft_tiff::writer::{rational, srational};
|
||||
use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value};
|
||||
use craft_tiff::writer::{rational, srational};
|
||||
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value};
|
||||
let ascii = |s: &Option<String>| s.as_ref().filter(|v| !v.is_empty()).map(|v| Value::Ascii(v.clone()));
|
||||
let mut ifd0 = IfdBuilder::new();
|
||||
for (tag, v) in [
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
|
||||
pub use crate::focus_read::{HEADER_PREFIX, HeaderRead, MAX_HEADER_BYTES, read_header};
|
||||
use crate::{Orientation, Rect};
|
||||
use lightcraft_tiff::{ByteOrder, Ifd, Tiff, makernote, tags as t};
|
||||
use craft_tiff::{ByteOrder, Ifd, Tiff, makernote, tags as t};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Where the camera focused, on the upright photo.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::*;
|
||||
use lightcraft_tiff::{Entry, IfdBuilder, TiffWriter, Value};
|
||||
use craft_tiff::{Entry, IfdBuilder, TiffWriter, Value};
|
||||
|
||||
fn encipher(p: u8) -> u8 {
|
||||
if p >= 249 { p } else { ((u32::from(p).pow(3)) % 249) as u8 }
|
||||
@@ -206,7 +206,7 @@ fn sony_file_with(make: &str, model: &str, orientation: u16, note_entries: &[(u1
|
||||
let mut outside: Vec<u8> = Vec::new();
|
||||
order.put_u16(&mut note, note_entries.len() as u16);
|
||||
for (tag, v) in note_entries {
|
||||
let raw = lightcraft_tiff::writer::encode(order, v);
|
||||
let raw = craft_tiff::writer::encode(order, v);
|
||||
order.put_u16(&mut note, *tag);
|
||||
order.put_u16(&mut note, v.field_type() as u16);
|
||||
order.put_u32(&mut note, v.count() as u32);
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
use std::io;
|
||||
|
||||
use crate::focus::FocusPoint;
|
||||
use lightcraft_tiff::{ByteOrder, FieldType, Tiff, tags as t};
|
||||
use craft_tiff::{ByteOrder, FieldType, Tiff, tags as t};
|
||||
|
||||
/// Bytes read first: the α1's and A7S III's focus data ends before 16 KB, so one read covers it.
|
||||
pub const HEADER_PREFIX: u64 = 32 * 1024;
|
||||
@@ -176,8 +176,7 @@ fn tiff(r: &mut Reader, head: Vec<u8>) -> io::Result<Option<FocusPoint>> {
|
||||
// file length: only the loaded ranges are data, and an unrelated entry that claims a
|
||||
// huge value must be cut by what leaving it cost, not by what the buffer claims to hold.
|
||||
// The same 8 × / + 1 MiB shape the general TIFF reader uses, over `reader.bytes` instead.
|
||||
let opts =
|
||||
lightcraft_tiff::ParseOptions { value_budget: Some(r.bytes.saturating_mul(8).saturating_add(1 << 20)), ..Default::default() };
|
||||
let opts = craft_tiff::ParseOptions { value_budget: Some(r.bytes.saturating_mul(8).saturating_add(1 << 20)), ..Default::default() };
|
||||
return Ok(crate::try_read_exif_with(&buf, &opts).ok().and_then(|m| m.focus));
|
||||
}
|
||||
Step::Nothing => return Ok(None),
|
||||
@@ -354,7 +353,7 @@ impl Want<'_> {
|
||||
|
||||
/// What of a classic TIFF the focus decoder still needs, given what is `loaded`: IFD0 (Make, Model,
|
||||
/// the Exif pointer), the Exif IFD (the maker-note entry), the maker note's header and IFD as
|
||||
/// `lightcraft_tiff::makernote` locates it, and the focus tags' values.
|
||||
/// `craft_tiff::makernote` locates it, and the focus tags' values.
|
||||
fn plan(buf: &[u8], loaded: &Loaded) -> Step {
|
||||
let Some((order, false)) = Tiff::sniff(buf) else { return Step::Nothing };
|
||||
let mut w = Want { buf, order, loaded, need: Vec::new() };
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//! (box structure from ISO/IEC 14496-12, item model from ISO/IEC 23008-12).
|
||||
//!
|
||||
//! HEIC is what iPhones and Macs write. Its pixels need an HEVC decoder (the optional
|
||||
//! `lightcraft-heif` crate), its metadata does not, so this reader is in every build: a HEIC
|
||||
//! `craft-heif` crate), its metadata does not, so this reader is in every build: a HEIC
|
||||
//! imports with its capture time, camera and lens whether or not the build can show the picture.
|
||||
//!
|
||||
//! The `meta` box holds an item information box (`iinf`, one `infe` per item) naming each item's
|
||||
@@ -198,7 +198,7 @@ mod tests {
|
||||
use super::*;
|
||||
// the whole routing, not just this module: a HEIC must reach the HEIF branch of `embedded`
|
||||
use crate::embedded;
|
||||
use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t};
|
||||
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t};
|
||||
|
||||
/// One box: its own size, its four-letter kind, then its payload.
|
||||
fn bx(kind: &[u8; 4], payload: &[u8]) -> Vec<u8> {
|
||||
@@ -390,14 +390,14 @@ mod tests {
|
||||
assert!(embedded(&lying).exif.is_none());
|
||||
}
|
||||
|
||||
/// The real container writer of `lightcraft-heif`, so the reader is proved against the layout
|
||||
/// The real container writer of `craft-heif`, so the reader is proved against the layout
|
||||
/// a HEIF encoder produces and not only against this module's own test builder.
|
||||
#[test]
|
||||
fn a_synthetic_heif_file_carries_its_exif() {
|
||||
let tiff = iphone_exif("2026:10:18 13:00:00", "-05:00");
|
||||
let mut spec = lightcraft_heif::testdata::Spec::new(64, 64, &|x, _| [x as u16 * 4, 0, 0]);
|
||||
let mut spec = craft_heif::testdata::Spec::new(64, 64, &|x, _| [x as u16 * 4, 0, 0]);
|
||||
spec.exif = Some(tiff.clone());
|
||||
let file = lightcraft_heif::testdata::build(&spec);
|
||||
let file = craft_heif::testdata::build(&spec);
|
||||
assert!(is_heif(&file));
|
||||
assert_eq!(embedded(&file).exif.as_deref(), Some(tiff.as_slice()));
|
||||
assert_eq!(crate::extract(&file).capture_time.map(|d| d.to_iso()).as_deref(), Some("2026-10-18T13:00:00-05:00"));
|
||||
|
||||
Loaded 100 of 246 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user