mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-29 01:34:38 +01:00
Merge branch 'sam3-gpu-4-detr' into sam3-gpu-5-mask-decoder
# Conflicts: # docs/ai-masks.md # docs/target-app-parity.md
This commit is contained in:
commit
434fc50d43
344 files changed
+14392
-41543
No files matched your search
@@ -19,42 +19,8 @@ concurrency:
|
||||
|
||||
jobs:
|
||||
git-sources:
|
||||
name: Git dependencies come from storytold
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 5
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- name: Git dependencies come from storytold
|
||||
run: |
|
||||
set -euo pipefail
|
||||
test -f Cargo.lock || { echo "Cargo.lock is missing"; exit 1; }
|
||||
# Source replacement, patches and path overrides in cargo config never show up
|
||||
# in Cargo.lock. Parsed rather than grepped: TOML has many spellings of one key.
|
||||
# Cargo still reads the legacy .cargo/config.
|
||||
python3 - <<'PY'
|
||||
import os, sys, tomllib
|
||||
for path in (".cargo/config", ".cargo/config.toml"):
|
||||
if not os.path.exists(path):
|
||||
continue
|
||||
config = tomllib.load(open(path, "rb"))
|
||||
replaced = [name for name, source in config.get("source", {}).items()
|
||||
if isinstance(source, dict) and "replace-with" in source]
|
||||
if replaced or "patch" in config or "paths" in config:
|
||||
print(f"{path} replaces, patches or overrides a source")
|
||||
sys.exit(1)
|
||||
PY
|
||||
# Fails when Cargo.lock is out of date with the manifests.
|
||||
cargo metadata --locked --format-version 1 > /dev/null
|
||||
python3 - <<'PY'
|
||||
import sys, tomllib
|
||||
lock = tomllib.load(open("Cargo.lock", "rb"))
|
||||
bad = sorted({p["source"] for p in lock.get("package", [])
|
||||
if p.get("source", "").startswith("git+")
|
||||
and not p["source"].startswith("git+https://github.com/storytold/")})
|
||||
if bad:
|
||||
print("Git dependencies outside github.com/storytold:", *bad, sep="\n")
|
||||
sys.exit(1)
|
||||
PY
|
||||
# Tracks @main so suite-wide CI fixes reach every app without a PR in each repository.
|
||||
uses: storytold/craft-common-actions/.github/workflows/git-sources.yml@main
|
||||
|
||||
nix:
|
||||
# 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.
|
||||
|
||||
@@ -28,10 +28,16 @@ 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
|
||||
# The suite's WiX (5.0.2); the install-folder test needs no extensions.
|
||||
- name: Install WiX
|
||||
shell: pwsh
|
||||
run: dotnet tool install --global wix --version 5.0.2
|
||||
uses: storytold/craft-common-actions/actions/install-wix@main
|
||||
with:
|
||||
install-extensions: "false"
|
||||
- name: Install-folder fallback and precedence (real MSI AppSearch)
|
||||
shell: pwsh
|
||||
run: |
|
||||
@@ -43,6 +49,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,9 +16,9 @@ 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 }}
|
||||
@@ -27,50 +31,16 @@ jobs:
|
||||
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:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- name: Git dependencies come from storytold
|
||||
run: |
|
||||
set -euo pipefail
|
||||
test -f Cargo.lock || { echo "Cargo.lock is missing"; exit 1; }
|
||||
# Source replacement, patches and path overrides in cargo config never show up
|
||||
# in Cargo.lock. Parsed rather than grepped: TOML has many spellings of one key.
|
||||
# Cargo still reads the legacy .cargo/config.
|
||||
python3 - <<'PY'
|
||||
import os, sys, tomllib
|
||||
for path in (".cargo/config", ".cargo/config.toml"):
|
||||
if not os.path.exists(path):
|
||||
continue
|
||||
config = tomllib.load(open(path, "rb"))
|
||||
replaced = [name for name, source in config.get("source", {}).items()
|
||||
if isinstance(source, dict) and "replace-with" in source]
|
||||
if replaced or "patch" in config or "paths" in config:
|
||||
print(f"{path} replaces, patches or overrides a source")
|
||||
sys.exit(1)
|
||||
PY
|
||||
# Fails when Cargo.lock is out of date with the manifests.
|
||||
cargo metadata --locked --format-version 1 > /dev/null
|
||||
python3 - <<'PY'
|
||||
import sys, tomllib
|
||||
lock = tomllib.load(open("Cargo.lock", "rb"))
|
||||
bad = sorted({p["source"] for p in lock.get("package", [])
|
||||
if p.get("source", "").startswith("git+")
|
||||
and not p["source"].startswith("git+https://github.com/storytold/")})
|
||||
if bad:
|
||||
print("Git dependencies outside github.com/storytold:", *bad, sep="\n")
|
||||
sys.exit(1)
|
||||
PY
|
||||
|
||||
release:
|
||||
needs: [git-sources, credits]
|
||||
needs: 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
|
||||
|
||||
Generated
+356
-97
@@ -641,6 +641,12 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bit_field"
|
||||
version = "0.10.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb2907cc03d08cdb1c6724ad0670ce67c4a71e1c45a6d7635224011714cfc4b1"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
@@ -1181,10 +1187,24 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-cms"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
|
||||
dependencies = [
|
||||
"md-5",
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-codepage"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=f0f4758c01581d79577b9bf5046c06f4e77b1f18#f0f4758c01581d79577b9bf5046c06f4e77b1f18"
|
||||
|
||||
[[package]]
|
||||
name = "craft-crw"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=f3b62ec61f45534392376492fc5ad33047375205#f3b62ec61f45534392376492fc5ad33047375205"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -1199,6 +1219,32 @@ 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=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-gif"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=b77af389f30bb10de10d8436ca9b39fe94e3bb67#b77af389f30bb10de10d8436ca9b39fe94e3bb67"
|
||||
dependencies = [
|
||||
"gif",
|
||||
"image",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-heif"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93"
|
||||
dependencies = [
|
||||
"heic-rs",
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-i18n"
|
||||
version = "0.1.0"
|
||||
@@ -1222,6 +1268,131 @@ dependencies = [
|
||||
"writeable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-image-codecs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f#478c50560d3c6fad3d613af2a30b35c3bc9f4c0f"
|
||||
dependencies = [
|
||||
"craft-jpeg",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f)",
|
||||
"crc32fast",
|
||||
"exr",
|
||||
"flate2",
|
||||
"half",
|
||||
"hayro-jpeg2000",
|
||||
"image",
|
||||
"image-webp",
|
||||
"jpeg-encoder 0.6.1",
|
||||
"png",
|
||||
"rayon",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
"tiff",
|
||||
"zune-core",
|
||||
"zune-jpeg",
|
||||
"zune-jpegxl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-jpeg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f#478c50560d3c6fad3d613af2a30b35c3bc9f4c0f"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-jxl"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=b77af389f30bb10de10d8436ca9b39fe94e3bb67#b77af389f30bb10de10d8436ca9b39fe94e3bb67"
|
||||
dependencies = [
|
||||
"jixel",
|
||||
"jxl-oxide",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-metadata"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f#478c50560d3c6fad3d613af2a30b35c3bc9f4c0f"
|
||||
dependencies = [
|
||||
"brotli-decompressor 6.0.1",
|
||||
"chrono",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f)",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-metadata"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
|
||||
dependencies = [
|
||||
"brotli-decompressor 6.0.1",
|
||||
"chrono",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-mime"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=f0f4758c01581d79577b9bf5046c06f4e77b1f18#f0f4758c01581d79577b9bf5046c06f4e77b1f18"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-codepage",
|
||||
"encoding_rs",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-psd"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"flate2",
|
||||
"rayon",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-raw"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-crw",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"jxl-oxide",
|
||||
"miniz_oxide 0.9.1",
|
||||
"rayon",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
"zune-core",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-tiff"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f#478c50560d3c6fad3d613af2a30b35c3bc9f4c0f"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-tiff"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ui"
|
||||
version = "0.1.0"
|
||||
@@ -1246,7 +1417,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-update"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c365d75c48b0e47f4b43cdb6592d0cdc39d2d52e#c365d75c48b0e47f4b43cdb6592d0cdc39d2d52e"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dunce",
|
||||
@@ -1256,6 +1427,7 @@ dependencies = [
|
||||
"image",
|
||||
"log",
|
||||
"plist",
|
||||
"rustix 1.1.5",
|
||||
"semver",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -1280,6 +1452,15 @@ dependencies = [
|
||||
"wayland-client",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=f0f4758c01581d79577b9bf5046c06f4e77b1f18#f0f4758c01581d79577b9bf5046c06f4e77b1f18"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.5.3"
|
||||
@@ -1769,6 +1950,20 @@ dependencies = [
|
||||
"bytemuck",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "89253175e704ab869b04f9db224d38630e0211a53841a79181deae0889e169f5"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"core_detect",
|
||||
"multiversion_no_op",
|
||||
"rustversion",
|
||||
"scopeguard",
|
||||
"simdutf8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "endi"
|
||||
version = "1.1.1"
|
||||
@@ -1950,6 +2145,22 @@ dependencies = [
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "exr"
|
||||
version = "1.74.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb29d0be702adf92c645d9b2f93472906ba83d400e59f98adb3770ce5c18c2f6"
|
||||
dependencies = [
|
||||
"bit_field",
|
||||
"half",
|
||||
"lebe",
|
||||
"miniz_oxide 0.8.9",
|
||||
"multiversion",
|
||||
"rayon-core",
|
||||
"smallvec",
|
||||
"zune-inflate",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "faer-core"
|
||||
version = "0.17.1"
|
||||
@@ -2861,6 +3072,15 @@ dependencies = [
|
||||
"foldhash 0.2.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hayro-jpeg2000"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c75ab947623ef4ccaa7acf0579edf7cbb5a73838e3839a7be73335e522f433a1"
|
||||
dependencies = [
|
||||
"fearless_simd",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
@@ -3109,10 +3329,10 @@ dependencies = [
|
||||
"byteorder-lite",
|
||||
"color_quant",
|
||||
"gif",
|
||||
"image-webp",
|
||||
"moxcms 0.8.1",
|
||||
"moxcms",
|
||||
"num-traits",
|
||||
"png",
|
||||
"qoi",
|
||||
"tiff",
|
||||
"zune-core",
|
||||
"zune-jpeg",
|
||||
@@ -3282,13 +3502,10 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jpeg-decoder"
|
||||
version = "0.3.2"
|
||||
name = "jpeg-encoder"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "00810f1d8b74be64b13dbf3db89ac67740615d6c891f0e7b6179326533011a07"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
checksum = "b454d911ac55068f53495488d8ccd0646eaa540c033a28ee15b07838afafb01f"
|
||||
|
||||
[[package]]
|
||||
name = "jpeg-encoder"
|
||||
@@ -3541,6 +3758,12 @@ dependencies = [
|
||||
"spin",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lebe"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.191"
|
||||
@@ -3587,7 +3810,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-update",
|
||||
@@ -3616,7 +3839,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-catalog"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"crc32fast",
|
||||
@@ -3637,7 +3860,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-cli"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"dhat",
|
||||
"lightcraft-codecs",
|
||||
@@ -3654,38 +3877,37 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-codecs"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"avif-serialize",
|
||||
"bytemuck",
|
||||
"craft-cms",
|
||||
"craft-gainmap",
|
||||
"craft-gif",
|
||||
"craft-heif",
|
||||
"craft-image-codecs",
|
||||
"craft-jpeg",
|
||||
"craft-jxl",
|
||||
"craft-psd",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"dhat",
|
||||
"image",
|
||||
"image-webp",
|
||||
"jixel",
|
||||
"jpeg-decoder",
|
||||
"jpeg-encoder",
|
||||
"jpeg-encoder 0.7.1",
|
||||
"jxl-oxide",
|
||||
"lightcraft-color",
|
||||
"lightcraft-geom",
|
||||
"lightcraft-heif",
|
||||
"lightcraft-raster",
|
||||
"lightcraft-testkit",
|
||||
"moxcms 0.9.1",
|
||||
"png",
|
||||
"proptest",
|
||||
"rav1e",
|
||||
"ravif",
|
||||
"rayon",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
"tiff",
|
||||
"zune-core",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-color"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-geom",
|
||||
"proptest",
|
||||
@@ -3694,7 +3916,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-denoise"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"faer-core",
|
||||
"lightcraft-denoise-core",
|
||||
@@ -3711,7 +3933,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-denoise-core"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"dhat",
|
||||
"lightcraft-raster",
|
||||
@@ -3727,8 +3949,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-develop"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"craft-mime",
|
||||
"lightcraft-color",
|
||||
"lightcraft-geom",
|
||||
"miniz_oxide 0.9.1",
|
||||
@@ -3739,11 +3962,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-engine"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"ab_glyph",
|
||||
"chrono",
|
||||
"craft-docpath",
|
||||
"craft-heif",
|
||||
"craft-mime",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"crc32fast",
|
||||
"fs4",
|
||||
"harfrust 0.13.3",
|
||||
"jxl-oxide",
|
||||
@@ -3759,7 +3986,6 @@ dependencies = [
|
||||
"lightcraft-geocode",
|
||||
"lightcraft-geom",
|
||||
"lightcraft-gpu",
|
||||
"lightcraft-heif",
|
||||
"lightcraft-lensdb",
|
||||
"lightcraft-merge",
|
||||
"lightcraft-meta",
|
||||
@@ -3771,7 +3997,6 @@ dependencies = [
|
||||
"lightcraft-segment",
|
||||
"lightcraft-slideshow",
|
||||
"lightcraft-testkit",
|
||||
"lightcraft-tiff",
|
||||
"log",
|
||||
"miniz_oxide 0.9.1",
|
||||
"rayon",
|
||||
@@ -3785,7 +4010,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-faces"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-testkit",
|
||||
"matrixmultiply",
|
||||
@@ -3800,7 +4025,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-fetch"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"log",
|
||||
"rustls",
|
||||
@@ -3811,14 +4036,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 +4051,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-geom"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
@@ -3834,7 +4059,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-gpu"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"candle-core",
|
||||
@@ -3854,34 +4079,27 @@ 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 = [
|
||||
"craft-xml",
|
||||
"lightcraft-develop",
|
||||
"lightcraft-geom",
|
||||
"quick-xml 0.41.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-mcp"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"craft-mime",
|
||||
"lightcraft-codecs",
|
||||
"lightcraft-engine",
|
||||
"lightcraft-raster",
|
||||
@@ -3891,7 +4109,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-merge"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-codecs",
|
||||
"lightcraft-color",
|
||||
@@ -3907,24 +4125,26 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-meta"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-heif",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"craft-psd",
|
||||
"craft-raw",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"lightcraft-geom",
|
||||
"lightcraft-heif",
|
||||
"lightcraft-tiff",
|
||||
"proptest",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-pipeline"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"craft-image-codecs",
|
||||
"dhat",
|
||||
"image",
|
||||
"lightcraft-color",
|
||||
"lightcraft-develop",
|
||||
"lightcraft-geom",
|
||||
@@ -3940,19 +4160,17 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-preview"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"jpeg-encoder",
|
||||
"craft-image-codecs",
|
||||
"lightcraft-raster",
|
||||
"log",
|
||||
"siphasher",
|
||||
"zune-core",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-raster"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"lightcraft-color",
|
||||
@@ -3964,19 +4182,18 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-raw"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-crw",
|
||||
"craft-image-codecs",
|
||||
"craft-raw",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
|
||||
"dhat",
|
||||
"jpeg-encoder",
|
||||
"jxl-oxide",
|
||||
"lightcraft-color",
|
||||
"lightcraft-geom",
|
||||
"lightcraft-meta",
|
||||
"lightcraft-raster",
|
||||
"lightcraft-testkit",
|
||||
"lightcraft-tiff",
|
||||
"miniz_oxide 0.9.1",
|
||||
"proptest",
|
||||
"rayon",
|
||||
@@ -3984,15 +4201,14 @@ dependencies = [
|
||||
"sha2",
|
||||
"thiserror 2.0.21",
|
||||
"zune-core",
|
||||
"zune-jpeg",
|
||||
"zune-jpegxl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-scenes"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"image",
|
||||
"craft-image-codecs",
|
||||
"lightcraft-color",
|
||||
"lightcraft-raster",
|
||||
"rayon",
|
||||
@@ -4001,7 +4217,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-segment"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"candle-core",
|
||||
"candle-nn",
|
||||
@@ -4016,7 +4232,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-slideshow"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-raster",
|
||||
"serde",
|
||||
@@ -4025,24 +4241,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",
|
||||
@@ -4075,7 +4282,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-web"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"eframe",
|
||||
@@ -4098,7 +4305,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "lightcraft-wincolor"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"lightcraft-testkit",
|
||||
"windows-sys 0.61.2",
|
||||
@@ -4196,6 +4403,16 @@ dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "md-5"
|
||||
version = "0.10.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.3"
|
||||
@@ -4272,16 +4489,6 @@ dependencies = [
|
||||
"pxfm",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "moxcms"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "396077b717181ce3f9a6b66739b269b82d756be5ec0f76be0bc40a7c51775830"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
"pxfm",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "muda"
|
||||
version = "0.21.2"
|
||||
@@ -4300,6 +4507,34 @@ dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "multiversion"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb8a68be21d0755f83bfe47e79b896cc732687cf1903f0e50585e8d6ecc53076"
|
||||
dependencies = [
|
||||
"multiversion-macros",
|
||||
"target-features",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "multiversion-macros"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a05e0c3c3e4edaa21d87c645852ed0b0c3663752bcc6ec43353bd6189dce35e1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.119",
|
||||
"target-features",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "multiversion_no_op"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d"
|
||||
|
||||
[[package]]
|
||||
name = "naga"
|
||||
version = "30.0.1"
|
||||
@@ -5468,6 +5703,15 @@ version = "0.1.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea"
|
||||
|
||||
[[package]]
|
||||
name = "qoi"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quick-error"
|
||||
version = "1.2.3"
|
||||
@@ -6554,6 +6798,12 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "target-features"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1bbb9f3c5c463a01705937a24fdabc5047929ac764b2d5b9cf681c1f5041ed5"
|
||||
|
||||
[[package]]
|
||||
name = "tempfile"
|
||||
version = "3.27.0"
|
||||
@@ -7917,7 +8167,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7"
|
||||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
version = "0.18.0"
|
||||
version = "0.21.1"
|
||||
dependencies = [
|
||||
"brotli",
|
||||
"flate2",
|
||||
@@ -8228,6 +8478,15 @@ version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b"
|
||||
|
||||
[[package]]
|
||||
name = "zune-inflate"
|
||||
version = "0.2.54"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02"
|
||||
dependencies = [
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zune-jpeg"
|
||||
version = "0.5.15"
|
||||
|
||||
+26
-16
@@ -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"
|
||||
@@ -13,10 +13,24 @@ repository = "https://github.com/storytold/lightcraft"
|
||||
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", features = ["serde"] }
|
||||
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7" }
|
||||
# Canon CRW decoder shared with the other Craft apps
|
||||
craft-crw = { git = "https://github.com/storytold/craft-libs", rev = "f3b62ec61f45534392376492fc5ad33047375205" }
|
||||
craft-crw = { git = "https://github.com/storytold/craft-libs", rev = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
|
||||
craft-cms = { git = "https://github.com/storytold/craft-libs", rev = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
|
||||
craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "f0f4758c01581d79577b9bf5046c06f4e77b1f18", version = "0.1.0" }
|
||||
craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "b77af389f30bb10de10d8436ca9b39fe94e3bb67", features = ["frames"] }
|
||||
craft-jxl = { git = "https://github.com/storytold/craft-libs", rev = "b77af389f30bb10de10d8436ca9b39fe94e3bb67", default-features = false, features = ["decode", "encode"] }
|
||||
# Photo metadata: EXIF, IPTC-IIM, XMP packets and GPX track logs (craft-libs#949)
|
||||
craft-metadata = { git = "https://github.com/storytold/craft-libs", rev = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
|
||||
craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "478c50560d3c6fad3d613af2a30b35c3bc9f4c0f" }
|
||||
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "478c50560d3c6fad3d613af2a30b35c3bc9f4c0f" }
|
||||
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "f0f4758c01581d79577b9bf5046c06f4e77b1f18" }
|
||||
# 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 = "c365d75c48b0e47f4b43cdb6592d0cdc39d2d52e", 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 = "9dcbd90c7d16e1a668b3afa057e940e2db328b93" }
|
||||
# PSD/PSB: the header, colour mode data and image resources (craft-libs#885).
|
||||
craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "9dcbd90c7d16e1a668b3afa057e940e2db328b93" }
|
||||
jpeg-encoder = { version = "0.7", default-features = false, features = ["std"] }
|
||||
# internal
|
||||
lightcraft-denoise-core = { path = "crates/denoise-core" }
|
||||
lightcraft-denoise = { path = "crates/denoise", default-features = false }
|
||||
@@ -26,7 +40,11 @@ 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 = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
|
||||
# Camera raw files, the format layer: every maker but CR3, ARW, RAF, RW2 and ORF (craft-libs#862 and the maker PRs).
|
||||
# On the same commit as craft-tiff: its errors and IFDs are shared.
|
||||
craft-raw = { git = "https://github.com/storytold/craft-libs", rev = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
|
||||
lightcraft-raw = { path = "crates/raw" }
|
||||
lightcraft-codecs = { path = "crates/codecs" }
|
||||
lightcraft-meta = { path = "crates/meta" }
|
||||
@@ -44,7 +62,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 = "9dcbd90c7d16e1a668b3afa057e940e2db328b93" }
|
||||
lightcraft-segment = { path = "crates/segment" }
|
||||
lightcraft-geocode = { path = "crates/geocode" }
|
||||
lightcraft-slideshow = { path = "crates/slideshow" }
|
||||
@@ -71,22 +90,13 @@ libc = "0.2" # dev-only: process CPU time in benches
|
||||
dhat = "0.3" # opt-in heap profiling (lightcraft-cli --features dhat-heap) and heap tests; never in product builds
|
||||
|
||||
# formats
|
||||
quick-xml = "0.41"
|
||||
miniz_oxide = "0.9"
|
||||
|
||||
# codecs
|
||||
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "tiff", "webp"] }
|
||||
zune-jpeg = { version = "0.5", default-features = false, features = ["std", "x86", "neon"] }
|
||||
zune-core = { version = "0.5", default-features = false, features = ["std"] }
|
||||
jpeg-decoder = { version = "0.3", default-features = false }
|
||||
jpeg-encoder = { version = "0.7", default-features = false, features = ["std"] }
|
||||
ab_glyph = { version = "0.2", default-features = false, features = ["std"] } # Apache-2.0, pure Rust glyph rasterizer (export watermarks)
|
||||
png = "0.18"
|
||||
tiff = "0.11"
|
||||
image-webp = "0.2"
|
||||
moxcms = { version = "0.9", default-features = false, features = ["lut"] }
|
||||
jxl-oxide = { version = "0.12", default-features = false }
|
||||
jixel = "=0.3.4" # BSD-3-Clause OR Apache-2.0, pure-Rust JPEG XL encoder (export; port of libjxl-tiny)
|
||||
zune-core = { version = "0.5", default-features = false, features = ["std"] }
|
||||
zune-jpegxl = { version = "0.5", default-features = false, features = ["std"] } # MIT/Apache-2.0/Zlib lossless JXL encoder (test fixtures only)
|
||||
ravif = { version = "0.13", default-features = false, features = ["threading"] } # no "asm": it needs nasm (non-Rust)
|
||||
# HDR AVIF (PQ): ravif fixes the sRGB transfer curve, so HDR files drive its rav1e + avif-serialize directly
|
||||
@@ -176,12 +186,12 @@ lightcraft-color = { opt-level = 3 }
|
||||
lightcraft-develop = { opt-level = 3 }
|
||||
lightcraft-geom = { opt-level = 3 }
|
||||
lightcraft-raw = { opt-level = 3 }
|
||||
craft-raw = { opt-level = 3 }
|
||||
lightcraft-codecs = { opt-level = 3 }
|
||||
lightcraft-scenes = { opt-level = 3 }
|
||||
lightcraft-preview = { opt-level = 3 }
|
||||
zune-jpeg = { opt-level = 3 }
|
||||
zune-core = { opt-level = 3 }
|
||||
jpeg-decoder = { opt-level = 3 }
|
||||
jpeg-encoder = { opt-level = 3 }
|
||||
png = { opt-level = 3 }
|
||||
miniz_oxide = { opt-level = 3 }
|
||||
|
||||
@@ -335,3 +335,133 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
JPEG encoding
|
||||
-------------
|
||||
|
||||
The shared JPEG writer includes the following third-party encoder notice. The Apache-2.0
|
||||
alternative referred to below is also included in this distribution as LICENSE-APACHE.
|
||||
|
||||
JPEG encoding dependency: jpeg-encoder 0.6.1
|
||||
|
||||
Source: https://crates.io/crates/jpeg-encoder/0.6.1
|
||||
Declared license: (MIT OR Apache-2.0) AND IJG.
|
||||
This software is based in part on the work of the Independent JPEG Group.
|
||||
|
||||
The MIT license and unaltered upstream IJG and SIMD notices follow. The Apache-2.0
|
||||
alternative is also retained in licenses/jpeg-encoder-0.6.1 at the repository root
|
||||
(https://github.com/storytold/craft-libs/tree/main/licenses/jpeg-encoder-0.6.1).
|
||||
The dependency is used unmodified; no encoder implementation is vendored here.
|
||||
|
||||
LICENSE-MIT
|
||||
|
||||
Copyright (c) 2021 Volker Ströbel <volkerstroebel@mysurdity.de>
|
||||
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
Original src/fdct.rs opening notice
|
||||
|
||||
/*
|
||||
* Ported from mozjpeg to rust
|
||||
*
|
||||
* This file was part of the Independent JPEG Group's software:
|
||||
* Copyright (C) 1991-1996, Thomas G. Lane.
|
||||
* libjpeg-turbo Modifications:
|
||||
* Copyright (C) 2015, 2020, D. R. Commander.
|
||||
*
|
||||
* Conditions of distribution and use:
|
||||
* In plain English:
|
||||
*
|
||||
* 1. We don't promise that this software works. (But if you find any bugs,
|
||||
* please let us know!)
|
||||
* 2. You can use this software for whatever you want. You don't have to pay us.
|
||||
* 3. You may not pretend that you wrote this software. If you use it in a
|
||||
* program, you must acknowledge somewhere in your documentation that
|
||||
* you've used the IJG code.
|
||||
*
|
||||
* In legalese:
|
||||
*
|
||||
* The authors make NO WARRANTY or representation, either express or implied,
|
||||
* with respect to this software, its quality, accuracy, merchantability, or
|
||||
* fitness for a particular purpose. This software is provided "AS IS", and you,
|
||||
* its user, assume the entire risk as to its quality and accuracy.
|
||||
*
|
||||
* This software is copyright (C) 1991-2020, Thomas G. Lane, Guido Vollbeding.
|
||||
* All Rights Reserved except as specified below.
|
||||
*
|
||||
* Permission is hereby granted to use, copy, modify, and distribute this
|
||||
* software (or portions thereof) for any purpose, without fee, subject to these
|
||||
* conditions:
|
||||
* (1) If any part of the source code for this software is distributed, then this
|
||||
* README file must be included, with this copyright and no-warranty notice
|
||||
* unaltered; and any additions, deletions, or changes to the original files
|
||||
* must be clearly indicated in accompanying documentation.
|
||||
* (2) If only executable code is distributed, then the accompanying
|
||||
* documentation must state that "this software is based in part on the work of
|
||||
* the Independent JPEG Group".
|
||||
* (3) Permission for use of this software is granted only if the user accepts
|
||||
* full responsibility for any undesirable consequences; the authors accept
|
||||
* NO LIABILITY for damages of any kind.
|
||||
*
|
||||
* These conditions apply to any software derived from or based on the IJG code,
|
||||
* not just to the unmodified library. If you use our work, you ought to
|
||||
* acknowledge us.
|
||||
*
|
||||
* Permission is NOT granted for the use of any IJG author's name or company name
|
||||
* in advertising or publicity relating to this software or products derived from
|
||||
* it. This software may be referred to only as "the Independent JPEG Group's
|
||||
* software".
|
||||
*
|
||||
* We specifically permit and encourage the use of this software as the basis of
|
||||
* commercial products, provided that all warranty or liability claims are
|
||||
* assumed by the product vendor.
|
||||
*
|
||||
* This file contains a slower but more accurate integer implementation of the
|
||||
* forward DCT (Discrete Cosine Transform).
|
||||
*
|
||||
* A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
|
||||
* on each column. Direct algorithms are also available, but they are
|
||||
* much more complex and seem not to be any faster when reduced to code.
|
||||
*
|
||||
* This implementation is based on an algorithm described in
|
||||
* C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
|
||||
* Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
|
||||
* Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
|
||||
* The primary algorithm described there uses 11 multiplies and 29 adds.
|
||||
* We use their alternate method with 12 multiplies and 32 adds.
|
||||
* The advantage of this method is that no data path contains more than one
|
||||
* multiplication; this allows a very simple and accurate implementation in
|
||||
* scaled fixed-point arithmetic, with a minimal number of shifts.
|
||||
*/
|
||||
|
||||
Original src/avx2/fdct.rs opening notice
|
||||
|
||||
/*
|
||||
* Ported from mozjpeg / jfdctint-avx2.asm to rust
|
||||
* Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
||||
* Copyright (C) 2009, 2016, 2018, 2020, D. R. Commander.
|
||||
*
|
||||
* Based on the x86 SIMD extension for IJG JPEG library
|
||||
* Copyright (C) 1999-2006, MIYASAKA Masaru.
|
||||
*/
|
||||
@@ -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"]
|
||||
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -26,6 +26,7 @@ pub mod lock;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
mod log_writer;
|
||||
pub mod model;
|
||||
pub mod opened_folder;
|
||||
pub mod query;
|
||||
pub mod rules;
|
||||
pub mod safe_file;
|
||||
@@ -44,6 +45,7 @@ use lightcraft_develop::DevelopSettings;
|
||||
pub use local::{DEFAULT_FORGET_DAYS, ForgetPlan, LocalRecords, folder_of};
|
||||
pub use lock::{LibraryLock, LockError, LockOwner};
|
||||
pub use model::*;
|
||||
pub use opened_folder::OpenedFolder;
|
||||
pub use query::{DateGroup, Filter, Person, RatingOp, Sort, SortKey, mix64};
|
||||
pub use rules::{Match, Rule, RuleSet};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -1543,6 +1545,8 @@ mod tests_lock;
|
||||
#[cfg(all(test, not(target_arch = "wasm32")))]
|
||||
mod tests_log_thread;
|
||||
#[cfg(test)]
|
||||
mod tests_opened_folder;
|
||||
#[cfg(test)]
|
||||
mod tests_repair;
|
||||
#[cfg(test)]
|
||||
mod tests_rule_order;
|
||||
|
||||
+87
-19
@@ -18,17 +18,26 @@
|
||||
//! descriptor frees the lock whatever copies a child still holds.
|
||||
//! - Who holds it (process id, computer, program, version) is written to `catalog.lock.owner`, for
|
||||
//! the error message. Only dropping the [`LibraryLock`] removes it, so a program that ends with
|
||||
//! `std::process::exit` releases the lock first (`Session::release_library_lock`). It's a separate
|
||||
//! file because a Windows lock also blocks reading the locked file. It's only a hint: it may be
|
||||
//! missing or left over from a crash.
|
||||
//! `std::process::exit` releases the lock first (`Session::release_library_lock`). Dropping
|
||||
//! removes only the note this lock wrote itself, with the bytes it wrote, and only while the
|
||||
//! path still leads to the folder it locked (a folder that holds exactly that note is it, whatever
|
||||
//! its numbers say: [`OpenedFolder::vouch`]): a note in another folder (a share mounted over the
|
||||
//! library's folder), or one another process wrote over ours, belongs to whoever has that library
|
||||
//! open (issue #3240). It's a separate file because a Windows lock also blocks reading the
|
||||
//! locked file. It's only a hint: it may be missing or left over from a crash. The note holds a
|
||||
//! value no other lock's note has ([`LockOwner::token`]), so no two folders hold the same note.
|
||||
//! - File systems without lock support (some network shares): the library opens unlocked, with a
|
||||
//! warning — refusing it would lock the user out of their own library. Locks over SMB/NFS are
|
||||
//! advisory and depend on the server; the owner file is then the only hint.
|
||||
//! - Browser storage (OPFS) has no file locks: see the web host for its own guard.
|
||||
|
||||
use std::fs::{File, OpenOptions, TryLockError};
|
||||
use std::hash::{BuildHasher, Hasher, RandomState};
|
||||
use std::io::{self, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use crate::opened_folder::OpenedFolder;
|
||||
|
||||
/// The lock file in the library directory.
|
||||
pub const LOCK: &str = "catalog.lock";
|
||||
@@ -47,6 +56,10 @@ pub struct LockOwner {
|
||||
pub version: String,
|
||||
/// When it opened the library (seconds since 1970).
|
||||
pub since: u64,
|
||||
/// This lock's alone, also among those one process takes in the same second: the note is what
|
||||
/// tells the folder that was locked from every other ([`OpenedFolder::vouch`], issue #3240), so
|
||||
/// no two may be the same. A count of locks mixed with a random key. Not a secret.
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
impl LockOwner {
|
||||
@@ -58,6 +71,7 @@ impl LockOwner {
|
||||
program: program.to_string(),
|
||||
version: env!("CARGO_PKG_VERSION").to_string(),
|
||||
since: web_time::SystemTime::now().duration_since(web_time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0),
|
||||
token: lock_token(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,6 +96,14 @@ fn host_name() -> String {
|
||||
env.or_else(|| std::fs::read_to_string("/etc/hostname").ok()).map(|h| h.trim().to_string()).filter(|h| !h.is_empty()).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// A value no other lock's note has ([`LockOwner::token`]).
|
||||
fn lock_token() -> String {
|
||||
static LOCKS: AtomicU64 = AtomicU64::new(0);
|
||||
let mut h = RandomState::new().build_hasher();
|
||||
h.write_u64(LOCKS.fetch_add(1, Ordering::Relaxed));
|
||||
format!("{:016x}", h.finish())
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum LockError {
|
||||
/// Another process has the library open.
|
||||
@@ -97,14 +119,20 @@ pub struct LibraryLock {
|
||||
/// `None` when the file system can't lock (the library is open unlocked).
|
||||
file: Option<File>,
|
||||
held: bool,
|
||||
/// The folder `dir` named when the lock was taken: the lock file and the owner note are in it
|
||||
/// (`None`: it couldn't be looked at).
|
||||
folder: Option<OpenedFolder>,
|
||||
/// The owner note as this lock wrote it (`None`: it wrote none).
|
||||
note: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl LibraryLock {
|
||||
/// Lock `dir` (which must exist) for this process. Fails only when another process holds it.
|
||||
pub fn acquire(dir: &Path, program: &str) -> Result<LibraryLock, LockError> {
|
||||
let folder = OpenedFolder::at(dir).ok();
|
||||
let unlocked = |why: io::Error| {
|
||||
log::warn!("library {}: can't lock {LOCK} ({why}); opening it without protection against a second program", dir.display());
|
||||
LibraryLock { dir: dir.to_path_buf(), file: None, held: false }
|
||||
LibraryLock { dir: dir.to_path_buf(), file: None, held: false, folder: folder.clone(), note: None }
|
||||
};
|
||||
let file = match OpenOptions::new().read(true).write(true).create(true).truncate(false).open(dir.join(LOCK)) {
|
||||
Ok(f) => f,
|
||||
@@ -118,15 +146,20 @@ impl LibraryLock {
|
||||
}
|
||||
Err(TryLockError::Error(e)) => return Ok(unlocked(e)),
|
||||
}
|
||||
// A new file in place of whatever is at the name: a link planted there in a library folder
|
||||
// someone else prepared is removed, never written through (as for the staging files, #1502).
|
||||
let owner = LockOwner::this_process(program);
|
||||
if let Ok(json) = serde_json::to_vec_pretty(&owner)
|
||||
&& let Err(e) = crate::store::create_staging(&dir.join(OWNER)).and_then(|mut f| f.write_all(&json))
|
||||
{
|
||||
log::warn!("library {}: can't write {OWNER}: {e}", dir.display());
|
||||
}
|
||||
Ok(LibraryLock { dir: dir.to_path_buf(), file: Some(file), held: true })
|
||||
let note = serde_json::to_vec_pretty(&LockOwner::this_process(program)).ok().and_then(|json| match write_note(dir, folder.as_ref(), &json) {
|
||||
Ok(()) => {
|
||||
// from now on the folder that holds this note is the locked one, whatever its numbers say
|
||||
if let Some(folder) = &folder {
|
||||
folder.vouch(&json);
|
||||
}
|
||||
Some(json)
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("library {}: can't write {OWNER}: {e}", dir.display());
|
||||
None
|
||||
}
|
||||
});
|
||||
Ok(LibraryLock { dir: dir.to_path_buf(), file: Some(file), held: true, folder, note })
|
||||
}
|
||||
|
||||
/// The OS lock is held (`false`: the file system can't lock; opened unprotected).
|
||||
@@ -138,6 +171,16 @@ impl LibraryLock {
|
||||
&self.dir
|
||||
}
|
||||
|
||||
/// The folder the lock was taken in, as `dir` named it then (`None`: it couldn't be looked at).
|
||||
pub fn folder(&self) -> Option<&OpenedFolder> {
|
||||
self.folder.as_ref()
|
||||
}
|
||||
|
||||
/// The owner note this lock wrote (`None`: it wrote none, so no folder is told by it).
|
||||
pub fn note(&self) -> Option<&[u8]> {
|
||||
self.note.as_deref()
|
||||
}
|
||||
|
||||
/// Another descriptor on the locked file: what a process spawned by another thread holds
|
||||
/// between fork and exec (a `dup` refers to the same open file description). Tests only (the
|
||||
/// test that uses it is Unix-only: `flock` semantics).
|
||||
@@ -145,17 +188,42 @@ impl LibraryLock {
|
||||
pub(crate) fn descriptor_copy(&self) -> Option<File> {
|
||||
self.file.as_ref().and_then(|f| f.try_clone().ok())
|
||||
}
|
||||
|
||||
/// Remove the owner note if it is this lock's: the path still leads to the folder the lock was
|
||||
/// taken in (or to one that holds this very note), and the note has the bytes this lock wrote
|
||||
/// there (issue #3240).
|
||||
fn remove_own_note(&self) {
|
||||
let Some(ours) = &self.note else { return };
|
||||
if self.folder.as_ref().is_some_and(|f| f.check().is_err()) {
|
||||
return;
|
||||
}
|
||||
let path = self.dir.join(OWNER);
|
||||
if crate::opened_folder::holds_exactly(&path, ours) {
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Write the owner note into `dir`: a new file in place of whatever is at the name (a link planted
|
||||
/// there in a library folder someone else prepared is removed, never written through, as for the
|
||||
/// staging files, #1502), and only while `dir` still leads to the folder the lock file was opened in.
|
||||
fn write_note(dir: &Path, folder: Option<&OpenedFolder>, json: &[u8]) -> io::Result<()> {
|
||||
if let Some(folder) = folder {
|
||||
folder.check()?;
|
||||
}
|
||||
crate::store::create_staging(&dir.join(OWNER))?.write_all(json)
|
||||
}
|
||||
|
||||
impl Drop for LibraryLock {
|
||||
/// Remove the owner note while still holding the lock (so it can't delete the next holder's),
|
||||
/// then release the lock explicitly. Closing the handle isn't enough on Unix: `flock` lasts
|
||||
/// while any copy of the descriptor is open, and a process another thread is spawning holds a
|
||||
/// copy of every descriptor until it execs (issue #680). If the unlock fails the handle still
|
||||
/// closes right after, which releases the lock once every copy is gone, as before.
|
||||
/// Remove the owner note, if it is ours, while still holding the lock (so it can't delete the
|
||||
/// next holder's), then release the lock explicitly. Closing the handle isn't enough on Unix:
|
||||
/// `flock` lasts while any copy of the descriptor is open, and a process another thread is
|
||||
/// spawning holds a copy of every descriptor until it execs (issue #680). If the unlock fails
|
||||
/// the handle still closes right after, which releases the lock once every copy is gone, as
|
||||
/// before.
|
||||
fn drop(&mut self) {
|
||||
if self.held {
|
||||
let _ = std::fs::remove_file(self.dir.join(OWNER));
|
||||
self.remove_own_note();
|
||||
if let Some(f) = &self.file
|
||||
&& let Err(e) = f.unlock()
|
||||
{
|
||||
|
||||
@@ -17,6 +17,20 @@
|
||||
//! lost. Cutting the log back, replacing it and reading it act on that logical content, after
|
||||
//! everything handed over before; the caller waits for them. Dropping the writer waits for
|
||||
//! everything handed over and tries a last time.
|
||||
//!
|
||||
//! **Every write asks the [`OpenedFolder`] first**, on this thread (a `stat` never makes the caller
|
||||
//! wait for the drive): when the log's path no longer leads to the folder the store opened (a share
|
||||
//! mounted over it, issue #3240) nothing is written. The bytes are held like those of any other
|
||||
//! write that failed, and written once the folder that was opened is back at the path.
|
||||
//!
|
||||
//! **Successful batches reopen the log by path.** A failed batch keeps its descriptor only to
|
||||
//! roll back its own bytes, never to append into a moved-away folder. A copy of the library's folder
|
||||
//! put at the path counts as the opened one (it holds the lock's note) and has a log of its own: a
|
||||
//! file kept open from before would go on writing into the folder that was moved away. A log that
|
||||
//! isn't as long as what this thread wrote (a fragment of its own failed write is cut first) is not
|
||||
//! the one it wrote, and appending to it would leave a gap: what it was handed is held like a failed
|
||||
//! write (reported; later appends are taken meanwhile, since retrying can't heal it), and the
|
||||
//! snapshot the app takes for held records, which holds every change, replaces the log.
|
||||
|
||||
use std::io::{self, Seek, Write};
|
||||
use std::path::PathBuf;
|
||||
@@ -24,6 +38,8 @@ use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender, channel};
|
||||
use std::sync::{Arc, Mutex, PoisonError};
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::opened_folder::OpenedFolder;
|
||||
|
||||
/// How long a caller waits for the thread at most (a cut, a replace, a read, a flush).
|
||||
const WAIT: Duration = Duration::from_secs(60);
|
||||
/// How often held bytes are retried while a failure stands and nothing else arrives.
|
||||
@@ -45,6 +61,8 @@ struct Shared {
|
||||
failed: Option<String>,
|
||||
/// Records (lines) handed over and not yet written and synced, while `failed` stands.
|
||||
held_records: usize,
|
||||
/// The file isn't the log the thread wrote: only a replace helps, so appends are taken.
|
||||
foreign: bool,
|
||||
}
|
||||
|
||||
pub(crate) struct LogWriter {
|
||||
@@ -63,12 +81,13 @@ fn not_written(why: &str) -> io::Error {
|
||||
}
|
||||
|
||||
impl LogWriter {
|
||||
/// A writer for `dir/name` (`None` when no thread can start: the store then writes itself).
|
||||
pub(crate) fn start(dir: PathBuf, name: &str) -> Option<LogWriter> {
|
||||
/// A writer for `name` in the folder (`None` when no thread can start: the store then writes itself).
|
||||
pub(crate) fn start(folder: OpenedFolder, name: &str, expected: Option<u64>) -> Option<LogWriter> {
|
||||
let (tx, rx) = channel::<Msg>();
|
||||
let shared = Arc::new(Mutex::new(Shared::default()));
|
||||
let sh = shared.clone();
|
||||
let mut w = Writer::new(dir, name);
|
||||
let mut w = Writer::new(folder, name);
|
||||
w.good = expected;
|
||||
let thread = std::thread::Builder::new().name("catalog-log".into()).spawn(move || w.run(&rx, &sh)).ok()?;
|
||||
Some(LogWriter { name: name.to_string(), tx: Some(tx), shared, thread: Some(thread) })
|
||||
}
|
||||
@@ -89,9 +108,11 @@ impl LogWriter {
|
||||
/// Hand `data` over (never waits for the drive). Refused while bytes handed over before can't
|
||||
/// be written: the caller keeps `data` and appends it again later.
|
||||
pub(crate) fn append(&self, data: &[u8]) -> io::Result<()> {
|
||||
if let Some(why) = self.shared.lock().unwrap_or_else(PoisonError::into_inner).failed.clone() {
|
||||
return Err(not_written(&why));
|
||||
let sh = self.shared.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if let Some(why) = sh.failed.as_deref().filter(|_| !sh.foreign) {
|
||||
return Err(not_written(why));
|
||||
}
|
||||
drop(sh);
|
||||
self.send(Msg::Append(data.to_vec()))
|
||||
}
|
||||
|
||||
@@ -133,17 +154,22 @@ impl Drop for LogWriter {
|
||||
}
|
||||
}
|
||||
|
||||
/// The thread's side: the file and what is held for it.
|
||||
/// The thread's side: how much of the file is written, and what is held for it.
|
||||
struct Writer {
|
||||
dir: PathBuf,
|
||||
path: PathBuf,
|
||||
tmp: PathBuf,
|
||||
file: Option<std::fs::File>,
|
||||
/// Bytes of the file written whole and synced (`None` until the file was first opened).
|
||||
/// The folder the store opened: asked before anything is written.
|
||||
folder: OpenedFolder,
|
||||
/// Bytes read by the journal or written whole and synced (`None` until first observed).
|
||||
good: Option<u64>,
|
||||
/// Handed over, not yet written and synced.
|
||||
held: Vec<u8>,
|
||||
failed: Option<String>,
|
||||
/// The exact file whose append failed; only this descriptor can have its torn bytes removed.
|
||||
torn: Option<std::fs::File>,
|
||||
/// The file isn't the log this thread wrote ([`Writer::open`]).
|
||||
foreign: bool,
|
||||
}
|
||||
|
||||
fn lines(b: &[u8]) -> usize {
|
||||
@@ -151,20 +177,24 @@ fn lines(b: &[u8]) -> usize {
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
fn new(dir: PathBuf, name: &str) -> Writer {
|
||||
fn new(folder: OpenedFolder, name: &str) -> Writer {
|
||||
let dir = folder.path().to_path_buf();
|
||||
let path = dir.join(name);
|
||||
let tmp = dir.join(format!("{name}.tmp"));
|
||||
Writer { dir, path, tmp, file: None, good: None, held: Vec::new(), failed: None }
|
||||
Writer { dir, path, tmp, folder, good: None, held: Vec::new(), failed: None, torn: None, foreign: false }
|
||||
}
|
||||
|
||||
fn hook(&self, step: crate::fault::Step) -> io::Result<()> {
|
||||
crate::fault::check(&self.dir, step)
|
||||
}
|
||||
|
||||
/// Open the file positioned at `good` (a fragment past it, from a write that failed, is cut).
|
||||
fn open(&mut self) -> io::Result<u64> {
|
||||
if let (Some(_), Some(g)) = (&self.file, self.good) {
|
||||
return Ok(g);
|
||||
/// Open the file by its path, positioned at `good`, and say where that is. A fragment a failed
|
||||
/// write left past `good` is cut; any other length is not the log this thread wrote (a copy was
|
||||
/// put at the path, or it was changed): refused.
|
||||
fn open(&mut self) -> io::Result<(std::fs::File, u64)> {
|
||||
self.folder.check()?;
|
||||
if let Some(good) = self.good {
|
||||
crate::store::rollback_torn(&mut self.torn, good)?;
|
||||
}
|
||||
// (a link or a device planted at the name meanwhile is refused, as at the first append)
|
||||
crate::store::check_regular_directory_entry(&self.path)?;
|
||||
@@ -172,50 +202,34 @@ impl Writer {
|
||||
let len = f.metadata()?.len();
|
||||
let good = match self.good {
|
||||
None => len,
|
||||
Some(g) if len > g => {
|
||||
f.set_len(g)?;
|
||||
g
|
||||
Some(g) if len == g => g,
|
||||
Some(g) => {
|
||||
self.foreign = true;
|
||||
return Err(io::Error::other(format!(
|
||||
"{} holds {len} bytes, the log this session wrote has {g}: it is another file, so nothing is appended to it and a snapshot replaces it",
|
||||
self.path.display()
|
||||
)));
|
||||
}
|
||||
Some(g) if len < g => {
|
||||
// shorter than what was synced: someone else cut it; continue at its end
|
||||
log::error!("catalog: {} is {len} bytes, {g} were written", self.path.display());
|
||||
len
|
||||
}
|
||||
Some(g) => g,
|
||||
};
|
||||
f.seek(io::SeekFrom::Start(good))?;
|
||||
self.good = Some(good);
|
||||
self.file = Some(f);
|
||||
Ok(good)
|
||||
self.foreign = false;
|
||||
Ok((f, good))
|
||||
}
|
||||
|
||||
/// Write and sync what is held.
|
||||
/// Write and sync what is held; a failure leaves `good` as it was, and the retry cuts what was left past it.
|
||||
fn write_held(&mut self) -> io::Result<()> {
|
||||
if self.held.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let r = (|| {
|
||||
self.hook(crate::fault::Step::Write)?;
|
||||
self.open()?;
|
||||
let f = self.file.as_mut().ok_or_else(|| io::Error::other("catalog log not open"))?;
|
||||
f.write_all(&self.held)?;
|
||||
self.hook(crate::fault::Step::Sync)?;
|
||||
self.file.as_ref().ok_or_else(|| io::Error::other("catalog log not open"))?.sync_data()
|
||||
})();
|
||||
match r {
|
||||
Ok(()) => {
|
||||
let n = self.held.len() as u64;
|
||||
self.good = Some(self.good.unwrap_or(0).saturating_add(n));
|
||||
self.held.clear();
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
// the handle may be dead (a network share that reconnected, a removed volume): the
|
||||
// retry opens the file again and cuts what this attempt left past `good`
|
||||
self.file = None;
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
self.hook(crate::fault::Step::Write)?;
|
||||
let (mut f, good) = self.open()?;
|
||||
let written = f.write_all(&self.held).and_then(|()| self.hook(crate::fault::Step::Sync)).and_then(|()| f.sync_data());
|
||||
self.torn = written.is_err().then_some(f);
|
||||
written?;
|
||||
self.good = Some(good.saturating_add(self.held.len() as u64));
|
||||
self.held.clear();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Cut the logical log (file then held) to `len` bytes.
|
||||
@@ -223,7 +237,7 @@ impl Writer {
|
||||
let good = match self.good {
|
||||
Some(g) => g,
|
||||
None => match self.open() {
|
||||
Ok(g) => g,
|
||||
Ok((_, g)) => g,
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => 0,
|
||||
Err(e) => return Err(e),
|
||||
},
|
||||
@@ -237,17 +251,14 @@ impl Writer {
|
||||
self.held.truncate(keep);
|
||||
return Ok(());
|
||||
}
|
||||
self.file = None;
|
||||
self.folder.check()?;
|
||||
crate::store::rollback_torn(&mut self.torn, good)?;
|
||||
crate::store::check_regular_directory_entry(&self.path)?;
|
||||
let f = match std::fs::OpenOptions::new().write(true).open(&self.path) {
|
||||
Ok(f) => f,
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => {
|
||||
self.good = None;
|
||||
self.held.clear();
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let f = std::fs::OpenOptions::new().write(true).open(&self.path)?;
|
||||
if f.metadata()?.len() != good {
|
||||
self.foreign = true;
|
||||
return Err(io::Error::other("the log changed before truncate: a snapshot must replace it"));
|
||||
}
|
||||
f.set_len(len)?;
|
||||
f.sync_all()?;
|
||||
self.good = Some(len);
|
||||
@@ -256,14 +267,14 @@ impl Writer {
|
||||
}
|
||||
|
||||
fn replace(&mut self, data: &[u8]) -> io::Result<()> {
|
||||
self.file = None;
|
||||
self.folder.check()?;
|
||||
self.hook(crate::fault::Step::Write)?;
|
||||
let mut file = crate::store::create_staging(&self.tmp)?;
|
||||
let written = (|| {
|
||||
self.hook(crate::fault::Step::Write)?;
|
||||
let mut f = crate::store::create_staging(&self.tmp)?;
|
||||
f.write_all(data)?;
|
||||
file.write_all(data)?;
|
||||
self.hook(crate::fault::Step::Sync)?;
|
||||
f.sync_all()?;
|
||||
drop(f);
|
||||
file.sync_all()?;
|
||||
crate::store::check_staging(&self.folder, &self.tmp, &file)?;
|
||||
std::fs::rename(&self.tmp, &self.path)
|
||||
})();
|
||||
match written {
|
||||
@@ -273,10 +284,14 @@ impl Writer {
|
||||
self.good = Some(data.len() as u64);
|
||||
self.held.clear();
|
||||
self.failed = None;
|
||||
self.torn = None;
|
||||
self.foreign = false;
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = std::fs::remove_file(&self.tmp);
|
||||
if crate::store::check_staging(&self.folder, &self.tmp, &file).is_ok() {
|
||||
let _ = std::fs::remove_file(&self.tmp);
|
||||
}
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
@@ -299,6 +314,7 @@ impl Writer {
|
||||
fn publish(&self, shared: &Mutex<Shared>) {
|
||||
let mut sh = shared.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
sh.failed = self.failed.clone().filter(|_| !self.held.is_empty());
|
||||
sh.foreign = self.foreign && sh.failed.is_some();
|
||||
sh.held_records = if sh.failed.is_some() { lines(&self.held) } else { 0 };
|
||||
}
|
||||
|
||||
|
||||
@@ -180,7 +180,13 @@ pub struct Meta {
|
||||
pub lens_make: String,
|
||||
pub focal_mm: Option<f32>,
|
||||
pub aperture: Option<f32>,
|
||||
/// Shutter speed for display, from [`lightcraft_meta::format_exposure_time`] (`1/3`, `0.3571`, `2.5`).
|
||||
pub shutter: String,
|
||||
/// The file's exact exposure time in seconds, kept beside the rounded `shutter` text so export
|
||||
/// writes the original duration; read through [`Meta::shutter_seconds`]. Left out of the catalog
|
||||
/// JSON when unknown, so older catalogs read unchanged.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub exposure_time: Option<f64>,
|
||||
pub iso: Option<u32>,
|
||||
/// Whether the flash fired, from Exif `Flash`.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
@@ -224,6 +230,16 @@ pub struct Meta {
|
||||
pub focus: Option<lightcraft_meta::FocusPoint>,
|
||||
}
|
||||
|
||||
impl Meta {
|
||||
/// Shutter speed in seconds: the exact [`Meta::exposure_time`] while `shutter` still shows it,
|
||||
/// otherwise whatever `shutter` says (set by hand, or a catalog from before the exact time was kept).
|
||||
pub fn shutter_seconds(&self) -> Option<f64> {
|
||||
self.exposure_time
|
||||
.filter(|t| lightcraft_meta::format_exposure_time(*t).is_some_and(|text| text == self.shutter.trim()))
|
||||
.or_else(|| parse_shutter_seconds(&self.shutter))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Version {
|
||||
pub name: String,
|
||||
@@ -761,7 +777,28 @@ mod edited_tests {
|
||||
|
||||
#[cfg(test)]
|
||||
mod shutter_tests {
|
||||
use super::parse_shutter_seconds;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn shutter_seconds_prefers_the_exact_time_while_the_text_still_shows_it() {
|
||||
let mut m = Meta { shutter: "0.3571".into(), exposure_time: Some(1.0 / 2.8), ..Default::default() };
|
||||
assert_eq!(m.shutter_seconds(), Some(1.0 / 2.8));
|
||||
m.shutter = "1/3".into();
|
||||
m.exposure_time = Some(1.0 / 3.0);
|
||||
assert_eq!(m.shutter_seconds(), Some(1.0 / 3.0));
|
||||
// the text no longer matches the stored time (edited elsewhere): the text wins
|
||||
m.shutter = "1/250".into();
|
||||
assert_eq!(m.shutter_seconds(), Some(1.0 / 250.0));
|
||||
// an older catalog without the exact time
|
||||
m.exposure_time = None;
|
||||
m.shutter = "2.5".into();
|
||||
assert_eq!(m.shutter_seconds(), Some(2.5));
|
||||
// the exact time stays out of the JSON when unknown, and round-trips when known
|
||||
assert!(!serde_json::to_string(&m).unwrap().contains("exposure_time"));
|
||||
m.exposure_time = Some(2.5);
|
||||
let back: Meta = serde_json::from_str(&serde_json::to_string(&m).unwrap()).unwrap();
|
||||
assert_eq!(back.exposure_time, Some(2.5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fraction_needs_a_positive_numerator_and_denominator() {
|
||||
|
||||
@@ -0,0 +1,371 @@
|
||||
//! The folder a library was opened from, and whether its path still leads to it (issue #3240).
|
||||
//!
|
||||
//! A library's files are written by path (`<library>/catalog.snap`, …), and a path can come to lead
|
||||
//! somewhere else while the library is open: a share mounted over the folder, or the folder moved
|
||||
//! away and another one put at its path. A session that went on writing then replaced the catalog
|
||||
//! of the library on the share with its own.
|
||||
//!
|
||||
//! [`OpenedFolder`] remembers which folder the path named when the library was opened;
|
||||
//! [`OpenedFolder::check`] asks, with one `stat`, whether it still does. Every write of a library
|
||||
//! file asks first ([`crate::FsStore`], its log thread, [`crate::LibraryLock`]) and is refused,
|
||||
//! writing nothing, when the answer is no. The refusal is an ordinary write error, so the changes
|
||||
//! stay queued and the app says why they aren't saved. The next write asks again: with the folder
|
||||
//! that was opened back at the path, saving goes on.
|
||||
//!
|
||||
//! - **Unix:** a folder is its device and inode number. A volume that is mounted again (a drive
|
||||
//! plugged in again, a share reconnected) gets another device number: its folder counts as the
|
||||
//! same when its inode number and its creation time agree (without one: the note below), so
|
||||
//! saving goes on there as it did before this check. A clone of the volume, with the same inode
|
||||
//! numbers and creation times, counts as that volume mounted again: nothing the check can see
|
||||
//! tells them apart. A folder moved away keeps its own inode number; one deleted and made again
|
||||
//! may be given the same inode number back, so on the same device two creation times that are
|
||||
//! both known must agree too.
|
||||
//! - **The note of the lock:** some file systems number a folder anew each time they are mounted
|
||||
//! (FAT and exFAT on Linux, sshfs, cifs without server inode numbers) and some keep no creation
|
||||
//! time to go by (NFS). A folder that holds exactly the owner note this session's lock wrote
|
||||
//! ([`OpenedFolder::vouch`]) is the opened folder whatever its numbers say: the note holds a
|
||||
//! value found in no other lock's note, so no other library has it. A copy of this library's
|
||||
//! folder made while it was open does, and counts as the library (the log in it is checked when
|
||||
//! it is written to: [`crate::FsStore`]); a share mounted over the folder, with a library of its
|
||||
//! own or none, never does.
|
||||
//! - **Windows:** `std` offers the volume serial number and the file index only on nightly
|
||||
//! (`windows_by_handle`, rust-lang/rust#63010), so there is no identity to compare yet. The check
|
||||
//! only asks that the path is still a folder, and a share mounted over a folder isn't noticed.
|
||||
//! - A check is not a lock: a folder replaced between the `stat` and the write that follows it is
|
||||
//! still written to. Closing that window takes I/O relative to an open directory handle
|
||||
//! (`openat`, `renameat`), which the library doesn't use.
|
||||
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, OnceLock};
|
||||
use std::time::SystemTime;
|
||||
|
||||
/// Which folder a path named: the device and the inode number the system reports for it, and when
|
||||
/// the folder was made where the file system keeps that.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
#[cfg_attr(not(unix), allow(dead_code))]
|
||||
struct FolderId {
|
||||
dev: u64,
|
||||
ino: u64,
|
||||
born: Option<SystemTime>,
|
||||
}
|
||||
|
||||
impl FolderId {
|
||||
#[cfg(unix)]
|
||||
fn of(folder: &std::fs::Metadata) -> Option<FolderId> {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
Some(FolderId { dev: folder.dev(), ino: folder.ino(), born: made_at(folder.created()) })
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn of(_: &std::fs::Metadata) -> Option<FolderId> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Whether `other` is the same folder: the same inode number, and then, where both creation
|
||||
/// times are known, the same creation time, else the same device number. A volume that is mounted
|
||||
/// again gets another device number, and saving went on there before there was a check; two
|
||||
/// creation times that differ are two folders, also on one device (an inode number is given
|
||||
/// again to a folder made later). Without both times another device is another folder: a
|
||||
/// refused save is better than a write into another volume's folder with that inode number.
|
||||
fn is(&self, other: &FolderId) -> bool {
|
||||
self.ino == other.ino
|
||||
&& match (self.born, other.born) {
|
||||
(Some(was), Some(now)) => was == now,
|
||||
_ => self.dev == other.dev,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// When a folder was made, where the file system says. macOS reports 0 (the Unix epoch) for a file
|
||||
/// system that keeps no creation time (NFS, macFUSE): the same for every folder there, so no folder
|
||||
/// is told from another by it.
|
||||
#[cfg(unix)]
|
||||
fn made_at(created: io::Result<SystemTime>) -> Option<SystemTime> {
|
||||
created.ok().filter(|t| *t != SystemTime::UNIX_EPOCH)
|
||||
}
|
||||
|
||||
/// The folder a library was opened from (see the module docs). Clones share one record, so the
|
||||
/// stores, the log's thread and the lock of a library all ask about the same folder.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct OpenedFolder {
|
||||
inner: Arc<Inner>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Inner {
|
||||
path: PathBuf,
|
||||
/// `None` where the system gives a folder no identity (Windows, for now).
|
||||
id: Option<FolderId>,
|
||||
/// The last check refused: a change is logged when it starts and when it ends, not at every write.
|
||||
refused: AtomicBool,
|
||||
/// The owner note this session's lock wrote into the folder (see [`OpenedFolder::vouch`]); set once.
|
||||
note: OnceLock<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl OpenedFolder {
|
||||
/// The folder `path` names now (a link is followed). Fails when it names no folder.
|
||||
pub fn at(path: &Path) -> io::Result<OpenedFolder> {
|
||||
let folder = std::fs::metadata(path)?;
|
||||
if !folder.is_dir() {
|
||||
return Err(io::Error::other(format!("{} is not a folder", path.display())));
|
||||
}
|
||||
Ok(OpenedFolder::new(path, FolderId::of(&folder)))
|
||||
}
|
||||
|
||||
fn new(path: &Path, id: Option<FolderId>) -> OpenedFolder {
|
||||
OpenedFolder { inner: Arc::new(Inner { path: path.to_path_buf(), id, refused: AtomicBool::new(false), note: OnceLock::new() }) }
|
||||
}
|
||||
|
||||
/// The path the folder was opened by.
|
||||
pub fn path(&self) -> &Path {
|
||||
&self.inner.path
|
||||
}
|
||||
|
||||
/// `other` was recorded for the same folder (always, where folders have no identity). A folder
|
||||
/// that holds the note this session's lock wrote is the same whatever its numbers say
|
||||
/// ([`OpenedFolder::vouch`]): the library opened again after a drive was mounted again keeps its lock.
|
||||
pub fn same_folder(&self, other: &OpenedFolder) -> bool {
|
||||
match (self.inner.id, other.inner.id) {
|
||||
(Some(a), Some(b)) => a.is(&b) || self.holds_note(other.path()),
|
||||
(a, b) => a.is_none() && b.is_none(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Record the owner note this session's lock wrote into the folder ([`crate::LibraryLock`]); the
|
||||
/// first one is kept. From then on a folder at the path that holds exactly these bytes is the
|
||||
/// opened folder even when its numbers say otherwise: some file systems number a folder anew at
|
||||
/// every mount (FAT and exFAT on Linux, sshfs), so a drive plugged in again would be refused for
|
||||
/// the rest of the session. The bytes are this lock's alone (the process, the program, the time
|
||||
/// it was taken and a value found in no other note): another library's folder doesn't have them
|
||||
/// (issue #3240), only a copy of this one made while it was open, which counts as it.
|
||||
pub fn vouch(&self, note: &[u8]) {
|
||||
let _ = self.inner.note.set(note.to_vec());
|
||||
}
|
||||
|
||||
/// The note [`OpenedFolder::vouch`] recorded (`None`: none was, the numbers decide alone).
|
||||
pub fn note(&self) -> Option<&[u8]> {
|
||||
self.inner.note.get().map(Vec::as_slice)
|
||||
}
|
||||
|
||||
/// `Ok` while the path leads to the folder that was opened (or to one that holds this session's
|
||||
/// note: [`OpenedFolder::vouch`]); else an error that says what is there now and what to do.
|
||||
pub fn check(&self) -> io::Result<()> {
|
||||
let inner = &*self.inner;
|
||||
let now = match std::fs::metadata(&inner.path) {
|
||||
Ok(now) => now,
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => return Err(self.refuse("nothing is there now")),
|
||||
// The drive doesn't answer: the write would fail the same way, with less to go on.
|
||||
Err(e) => {
|
||||
let what = format!("can't tell whether {} is still the folder this library was opened from: {e}", inner.path.display());
|
||||
return Err(io::Error::new(e.kind(), what));
|
||||
}
|
||||
};
|
||||
if !now.is_dir() {
|
||||
return Err(self.refuse("it isn't a folder now"));
|
||||
}
|
||||
if let (Some(was), Some(is)) = (inner.id, FolderId::of(&now))
|
||||
&& !was.is(&is)
|
||||
&& !self.holds_note(&inner.path)
|
||||
{
|
||||
// (the same device number is the same volume's: another folder on it was put at the path)
|
||||
return Err(self.refuse(if was.dev == is.dev { "it was moved or replaced" } else { "a different drive or share is there now" }));
|
||||
}
|
||||
if inner.refused.swap(false, Ordering::Relaxed) {
|
||||
log::info!("library folder {}: the folder this library was opened from is back", inner.path.display());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The error for a path that doesn't lead to the opened folder, `what` being what is there.
|
||||
fn refuse(&self, what: &str) -> io::Error {
|
||||
let path = self.inner.path.display();
|
||||
let why = format!(
|
||||
"{path} is no longer the folder this library was opened from ({what}), so nothing is written to it: put the original folder back"
|
||||
);
|
||||
if !self.inner.refused.swap(true, Ordering::Relaxed) {
|
||||
log::warn!("library folder {why}");
|
||||
}
|
||||
io::Error::other(why)
|
||||
}
|
||||
|
||||
/// Whether the folder at `dir` holds the note this session's lock wrote ([`OpenedFolder::vouch`]).
|
||||
fn holds_note(&self, dir: &Path) -> bool {
|
||||
self.note().is_some_and(|ours| holds_exactly(&dir.join(crate::lock::OWNER), ours))
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `path` is a plain file that holds exactly `bytes`. A link, a folder or a file of another
|
||||
/// size is no match and is not read (a pipe would block, a large file would be read for nothing).
|
||||
pub(crate) fn holds_exactly(path: &Path, bytes: &[u8]) -> bool {
|
||||
let plain = std::fs::symlink_metadata(path).is_ok_and(|m| m.is_file() && m.len() == bytes.len() as u64);
|
||||
plain && std::fs::read(path).is_ok_and(|now| now == bytes)
|
||||
}
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
impl OpenedFolder {
|
||||
/// `path` as a volume that numbers its folders anew at every mount would have recorded it: other
|
||||
/// device and inode numbers than the folder has now, and no creation time (a mount can't be
|
||||
/// faked in a test).
|
||||
pub(crate) fn recorded_under_other_numbers(path: &Path) -> OpenedFolder {
|
||||
let now = std::fs::metadata(path).ok().and_then(|m| FolderId::of(&m));
|
||||
OpenedFolder::new(path, now.map(|n| FolderId { dev: n.dev.wrapping_add(1), ino: n.ino.wrapping_add(1), born: None }))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[cfg(unix)]
|
||||
fn scratch(tag: &str) -> PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("lc-opened-folder-unit-{tag}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
fn id(dev: u64, ino: u64, born: Option<u64>) -> FolderId {
|
||||
FolderId { dev, ino, born: born.map(|s| SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(s)) }
|
||||
}
|
||||
|
||||
/// A folder is the same one when its inode number agrees and then its device number does or, where
|
||||
/// both creation times are known, its creation time: a volume that is mounted again has another
|
||||
/// device number, and an inode number is given again to a folder made later. Anything less sure
|
||||
/// is another folder.
|
||||
#[test]
|
||||
fn a_folder_on_a_volume_mounted_again_is_the_same_folder_only_when_it_was_made_at_the_same_time() {
|
||||
let was = id(7, 100, Some(1_000));
|
||||
assert!(was.is(&id(7, 100, Some(1_000))), "the same");
|
||||
assert!(!was.is(&id(7, 100, Some(2_000))), "the inode number of a folder that is gone, given to one made later");
|
||||
assert!(was.is(&id(9, 100, Some(1_000))), "mounted again under another device number");
|
||||
assert!(!was.is(&id(9, 100, Some(2_000))), "another volume's folder that has that inode number");
|
||||
assert!(!was.is(&id(9, 100, None)), "no creation time to tell by");
|
||||
assert!(!id(7, 100, None).is(&id(9, 100, None)), "none known at all");
|
||||
assert!(id(7, 100, None).is(&id(7, 100, None)), "the same device, and none to tell by");
|
||||
assert!(id(7, 100, None).is(&id(7, 100, Some(1_000))), "a creation time known for one folder only is no reason to refuse");
|
||||
assert!(!was.is(&id(7, 101, Some(1_000))), "another folder on the volume");
|
||||
assert!(!was.is(&id(9, 101, Some(1_000))), "another folder on another volume");
|
||||
}
|
||||
|
||||
/// The same rule in a check, with the device number recorded at open made different from the
|
||||
/// one reported now (a mount can't be faked in a test).
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_check_accepts_a_remounted_volume_and_refuses_another_volumes_folder() {
|
||||
let dir = scratch("remount");
|
||||
let real = FolderId::of(&std::fs::metadata(&dir).unwrap()).unwrap();
|
||||
let recorded = |id: FolderId| OpenedFolder::new(&dir, Some(id));
|
||||
|
||||
let remounted = recorded(FolderId { dev: real.dev.wrapping_add(1), ..real });
|
||||
match real.born {
|
||||
Some(_) => remounted.check().expect("the same inode number and creation time: the same folder, mounted again"),
|
||||
None => assert!(remounted.check().is_err(), "no creation time to tell by: refused"),
|
||||
}
|
||||
let other = recorded(FolderId { dev: real.dev.wrapping_add(1), ino: real.ino.wrapping_add(1), ..real });
|
||||
let msg = other.check().expect_err("another volume's folder").to_string();
|
||||
assert!(msg.contains("a different drive or share is there now"), "{msg}");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// The same rule in a check, with the creation time recorded at open made different from the one
|
||||
/// reported now (a folder made again under the inode number of one that was deleted can't be
|
||||
/// arranged in a test): the same device and inode number with another creation time is another
|
||||
/// folder, unless it holds the note this session's lock wrote.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_folder_made_again_under_the_same_inode_number_is_another_folder_unless_it_holds_the_note() {
|
||||
let dir = scratch("made-again");
|
||||
let real = FolderId::of(&std::fs::metadata(&dir).unwrap()).unwrap();
|
||||
let Some(born) = real.born else {
|
||||
OpenedFolder::new(&dir, Some(real)).check().expect("no creation time to tell by: the numbers decide");
|
||||
return;
|
||||
};
|
||||
let earlier = OpenedFolder::new(&dir, Some(FolderId { born: Some(born + std::time::Duration::from_secs(3600)), ..real }));
|
||||
let msg = earlier.check().expect_err("the same numbers, made at another time").to_string();
|
||||
assert!(msg.contains("it was moved or replaced"), "{msg}");
|
||||
assert!(!earlier.same_folder(&OpenedFolder::at(&dir).unwrap()), "not the folder that lock was taken in");
|
||||
|
||||
let note = b"the note of the lock";
|
||||
earlier.vouch(note);
|
||||
std::fs::write(dir.join(crate::lock::OWNER), note).unwrap();
|
||||
earlier.check().expect("this session's note: the folder it locked, whatever its numbers say");
|
||||
assert!(earlier.same_folder(&OpenedFolder::at(&dir).unwrap()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// macOS reports a creation time of 0 for a file system that keeps none (NFS, macFUSE), the same
|
||||
/// for every folder on it: that is no creation time, or any two such folders that have an inode
|
||||
/// number in common would be taken for one folder mounted again.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_creation_time_of_zero_is_no_creation_time() {
|
||||
let made = SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_700_000_000);
|
||||
assert_eq!(made_at(Ok(made)), Some(made));
|
||||
assert_eq!(made_at(Ok(SystemTime::UNIX_EPOCH)), None);
|
||||
assert_eq!(made_at(Err(io::Error::other("this file system keeps none"))), None);
|
||||
}
|
||||
|
||||
/// A file system that numbers its folders anew at every mount (FAT and exFAT on Linux, sshfs)
|
||||
/// gives a folder that was opened other numbers: it is that folder when it holds the note this
|
||||
/// session's lock wrote, and no other folder is. (A mount can't be faked in a test: the numbers
|
||||
/// recorded at open are made different from the folder's.)
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_folder_with_other_numbers_is_the_opened_one_only_while_it_holds_the_note_the_lock_wrote() {
|
||||
let dir = scratch("note");
|
||||
let note = br#"{"pid":4242,"host":"here","program":"LightCraft","version":"0.2.0","since":1}"#;
|
||||
let renumbered = OpenedFolder::recorded_under_other_numbers(&dir);
|
||||
let msg = renumbered.check().expect_err("other numbers, and no note vouched for").to_string();
|
||||
assert!(msg.contains("a different drive or share is there now"), "{msg}");
|
||||
|
||||
renumbered.vouch(note);
|
||||
assert_eq!(renumbered.note(), Some(note.as_slice()));
|
||||
let path = dir.join(crate::lock::OWNER);
|
||||
assert!(renumbered.check().is_err(), "the note is vouched for, the folder holds none");
|
||||
let longer = [note.as_slice(), b" ".as_slice()].concat();
|
||||
for other in [note.to_ascii_uppercase(), longer, note[1..].to_vec(), Vec::new()] {
|
||||
std::fs::write(&path, &other).unwrap();
|
||||
assert!(renumbered.check().is_err(), "another note: {}", String::from_utf8_lossy(&other));
|
||||
}
|
||||
std::fs::write(&path, note).unwrap();
|
||||
renumbered.check().expect("this session's note: the folder it locked, whatever its numbers say");
|
||||
|
||||
// what is no note: a link to one, a folder
|
||||
let elsewhere = dir.join("elsewhere");
|
||||
std::fs::rename(&path, &elsewhere).unwrap();
|
||||
std::os::unix::fs::symlink(&elsewhere, &path).unwrap();
|
||||
assert!(renumbered.check().is_err(), "a link to the bytes is no note");
|
||||
std::fs::remove_file(&path).unwrap();
|
||||
std::fs::create_dir(&path).unwrap();
|
||||
assert!(renumbered.check().is_err(), "a folder is no note");
|
||||
|
||||
// the first note is the one that counts
|
||||
renumbered.vouch(b"another");
|
||||
assert_eq!(renumbered.note(), Some(note.as_slice()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// The lock is handed over to a library opened again from the same folder, and a folder that
|
||||
/// holds the note this session's lock wrote is that folder whatever its numbers say (a drive
|
||||
/// that was mounted again): not one with no such note.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_folder_that_holds_the_locks_note_is_the_same_folder_for_the_hand_over() {
|
||||
let dir = scratch("hand-over");
|
||||
let other = scratch("hand-over-other");
|
||||
let note = b"the note of the lock";
|
||||
let at_open = OpenedFolder::recorded_under_other_numbers(&dir);
|
||||
let now = OpenedFolder::at(&dir).unwrap();
|
||||
assert!(!at_open.same_folder(&now), "other numbers, no note");
|
||||
|
||||
at_open.vouch(note);
|
||||
assert!(!at_open.same_folder(&now), "the note is vouched for, the folder holds none");
|
||||
std::fs::write(dir.join(crate::lock::OWNER), note).unwrap();
|
||||
assert!(at_open.same_folder(&now));
|
||||
assert!(!at_open.same_folder(&OpenedFolder::at(&other).unwrap()), "another folder holds no such note");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let _ = std::fs::remove_dir_all(&other);
|
||||
}
|
||||
}
|
||||
@@ -1100,6 +1100,8 @@ impl Rule {
|
||||
"captureDate" => date_op(op, p.captured.as_deref(), value),
|
||||
"importDate" => date_op(op, Some(&p.imported), value),
|
||||
"editDate" => date_op(op, p.edited.as_deref(), value),
|
||||
// (the shown shutter text, not the exact time kept for export: a rule matches the value the
|
||||
// photo shows, `1/3` or `0.3571`, at every op, including `is` and the edges of a range)
|
||||
"shutterSpeed" => num_op(op, crate::parse_shutter_seconds(&m.shutter), &shutter_value(value)),
|
||||
"iso" => num_op(op, m.iso.map(|v| v as f64), value),
|
||||
"flash" => yes(m.flash.unwrap_or(false)),
|
||||
|
||||
@@ -795,3 +795,19 @@ fn file_path_contains_finds_every_piece_of_a_long_path() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shutter_speed_rules_match_the_shown_value() {
|
||||
let cat = Catalog::new();
|
||||
let mut p = photo(1);
|
||||
let m =
|
||||
|p: &Photo, op: &str, value: serde_json::Value| rs(json!({"rules": [{"field": "shutterSpeed", "op": op, "value": value}]})).matches(p, &cat);
|
||||
p.meta.exposure_time = Some(1.0 / 2.8);
|
||||
p.meta.shutter = "0.3571".into();
|
||||
assert!(m(&p, "is", json!("0.3571")) && !m(&p, "isNot", json!("0.3571")), "1/2.8 s is the 0.3571 it shows");
|
||||
assert!(m(&p, "lte", json!("0.3571")) && m(&p, "gte", json!("0.3571")));
|
||||
p.meta.exposure_time = Some(3333.0 / 10000.0);
|
||||
p.meta.shutter = "1/3".into();
|
||||
assert!(m(&p, "is", json!("1/3")) && !m(&p, "isNot", json!("1/3")), "3333/10000 s is the 1/3 it shows");
|
||||
assert!(m(&p, "gte", json!("1/3")) && m(&p, "lte", json!("1/3")) && !m(&p, "lt", json!("1/3")));
|
||||
}
|
||||
@@ -260,7 +260,7 @@ impl Catalog {
|
||||
.filter(|p| !p.deleted && !merged(p))
|
||||
.filter_map(|p| {
|
||||
let t = iso_seconds(p.captured.as_deref()?)?;
|
||||
let s = crate::model::parse_shutter_seconds(&p.meta.shutter)?;
|
||||
let s = p.meta.shutter_seconds()?;
|
||||
let n = p.meta.aperture.filter(|n| n.is_finite() && *n > 0.0).unwrap_or(1.0) as f64;
|
||||
let iso = p.meta.iso.filter(|i| *i > 0).unwrap_or(100) as f64;
|
||||
Some((t, p.file_name.as_str(), p.id, s, (s * iso / (n * n)).log2()))
|
||||
|
||||
+166
-50
@@ -2,13 +2,16 @@
|
||||
//!
|
||||
//! [`FsStore`] is the native implementation (a library directory); [`MemStore`] keeps files in
|
||||
//! memory (tests, ephemeral sessions) and lets tests simulate crashes by editing the bytes. A web
|
||||
//! host can implement [`Store`] over OPFS.
|
||||
//! host can implement [`Store`] over OPFS. [`FsStore`] writes only while its directory still leads
|
||||
//! to the folder it opened ([`crate::OpenedFolder`], issue #3240).
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::opened_folder::OpenedFolder;
|
||||
|
||||
/// A flat namespace of named files with the few operations the journal needs.
|
||||
pub trait Store: Send {
|
||||
/// The whole file, or `None` if it doesn't exist.
|
||||
@@ -70,27 +73,73 @@ pub(crate) fn check_regular_directory_entry(path: &Path) -> io::Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The synchronous fallback's acknowledged log length. Failed records stay with the caller;
|
||||
/// `Journal::append` requests a snapshot on an error rather than acknowledging a sequence gap.
|
||||
struct SynchronousLog {
|
||||
good: u64,
|
||||
torn: Option<std::fs::File>,
|
||||
}
|
||||
|
||||
fn check_log_len(len: u64, good: u64) -> io::Result<()> {
|
||||
if len == good {
|
||||
return Ok(());
|
||||
}
|
||||
Err(io::Error::other(format!(
|
||||
"catalog log holds {len} bytes, the log this session read or wrote has {good}: it is another file, so a snapshot must replace it"
|
||||
)))
|
||||
}
|
||||
|
||||
/// Roll back only the descriptor whose append failed, never a new file at its old path.
|
||||
pub(crate) fn rollback_torn(file: &mut Option<std::fs::File>, good: u64) -> io::Result<()> {
|
||||
if let Some(f) = file.as_ref() {
|
||||
if f.metadata()?.len() < good {
|
||||
return Err(io::Error::other("the failed log was shortened: a snapshot must replace it"));
|
||||
}
|
||||
f.set_len(good)?;
|
||||
f.sync_data()?;
|
||||
*file = None;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Files in a directory.
|
||||
pub struct FsStore {
|
||||
dir: PathBuf,
|
||||
/// Open append handle (kept between appends; dropped on rewrite/truncate). Used when no thread
|
||||
/// writes the log (the browser build, or one that couldn't start).
|
||||
appender: Option<(String, std::fs::File)>,
|
||||
/// The folder `dir` named when the store was opened. Every write asks it first and is refused
|
||||
/// when `dir` leads somewhere else now (issue #3240); so does the log's thread.
|
||||
folder: OpenedFolder,
|
||||
/// Set only after using the synchronous fallback; do not switch to a fresh thread afterward
|
||||
/// and forget which records that fallback acknowledged.
|
||||
sync_log: Option<SynchronousLog>,
|
||||
/// Length of the log the journal read, retained through the first asynchronous or caller append.
|
||||
known_log_len: Option<u64>,
|
||||
/// The thread that writes the log (natively; started by the first append).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
log: Option<crate::log_writer::LogWriter>,
|
||||
}
|
||||
|
||||
impl FsStore {
|
||||
/// Use `dir` (created if missing).
|
||||
/// Use `dir` (created if missing). The store writes to the folder `dir` names now, and only
|
||||
/// while it still does.
|
||||
pub fn open(dir: impl AsRef<Path>) -> io::Result<FsStore> {
|
||||
std::fs::create_dir_all(dir.as_ref())?;
|
||||
Ok(FsStore {
|
||||
dir: dir.as_ref().to_path_buf(),
|
||||
appender: None,
|
||||
Ok(FsStore::in_folder(OpenedFolder::at(dir.as_ref())?))
|
||||
}
|
||||
/// A store in a folder that was recorded already, so that the stores, the lock and the caches
|
||||
/// of one library ask about the same folder.
|
||||
pub fn in_folder(folder: OpenedFolder) -> FsStore {
|
||||
FsStore {
|
||||
dir: folder.path().to_path_buf(),
|
||||
folder,
|
||||
sync_log: None,
|
||||
known_log_len: None,
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
log: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
/// The folder the store opened.
|
||||
pub fn folder(&self) -> &OpenedFolder {
|
||||
&self.folder
|
||||
}
|
||||
pub fn dir(&self) -> &Path {
|
||||
&self.dir
|
||||
@@ -113,6 +162,15 @@ impl FsStore {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_sync_log(&mut self, name: &str, len: u64) {
|
||||
if name == crate::journal::LOG {
|
||||
self.known_log_len = Some(len);
|
||||
if self.sync_log.is_some() {
|
||||
self.sync_log = Some(SynchronousLog { good: len, torn: None });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn sync_dir(&self) {
|
||||
// Makes the rename durable on POSIX; opening a directory fails on Windows (where
|
||||
// `rename` is already journaled by NTFS), so errors are ignored.
|
||||
@@ -132,6 +190,27 @@ pub(crate) fn create_staging(tmp: &std::path::Path) -> io::Result<std::fs::File>
|
||||
std::fs::OpenOptions::new().write(true).create_new(true).open(tmp)
|
||||
}
|
||||
|
||||
/// Recheck after a potentially long fill: neither the folder nor the staging name may now
|
||||
/// belong to another save. Holding `file` also prevents Unix inode reuse while comparing it.
|
||||
pub(crate) fn check_staging(folder: &OpenedFolder, path: &Path, file: &std::fs::File) -> io::Result<()> {
|
||||
folder.check()?;
|
||||
let named = std::fs::symlink_metadata(path)?;
|
||||
if !named.is_file() {
|
||||
return Err(io::Error::other("the snapshot staging file was replaced"));
|
||||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let held = file.metadata()?;
|
||||
if (held.dev(), held.ino()) != (named.dev(), named.ino()) {
|
||||
return Err(io::Error::other("the snapshot staging file was replaced"));
|
||||
}
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
let _ = file; // Folder/file identity on Windows remains outside this Unix mitigation.
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl Store for FsStore {
|
||||
fn read(&mut self, name: &str) -> io::Result<Option<Vec<u8>>> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -139,11 +218,15 @@ impl Store for FsStore {
|
||||
// what was handed over, written or still held
|
||||
return w.read();
|
||||
}
|
||||
match std::fs::read(self.path(name)) {
|
||||
Ok(b) => Ok(Some(b)),
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
|
||||
Err(e) => Err(e),
|
||||
let bytes = match std::fs::read(self.path(name)) {
|
||||
Ok(b) => Some(b),
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => None,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
if name == crate::journal::LOG && self.known_log_len.is_none() {
|
||||
self.known_log_len = Some(bytes.as_ref().map_or(0, |b| b.len() as u64));
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
fn write_atomic(&mut self, name: &str, data: &[u8]) -> io::Result<()> {
|
||||
@@ -155,17 +238,15 @@ impl Store for FsStore {
|
||||
self.sync_dir();
|
||||
return Ok(());
|
||||
}
|
||||
if self.appender.as_ref().is_some_and(|(n, _)| n == name) {
|
||||
self.appender = None;
|
||||
}
|
||||
self.folder.check()?;
|
||||
let tmp = self.path(&format!("{name}.tmp"));
|
||||
{
|
||||
let mut f = create_staging(&tmp)?;
|
||||
f.write_all(data)?;
|
||||
f.sync_all()?;
|
||||
}
|
||||
let mut f = create_staging(&tmp)?;
|
||||
f.write_all(data)?;
|
||||
f.sync_all()?;
|
||||
check_staging(&self.folder, &tmp, &f)?;
|
||||
std::fs::rename(&tmp, self.path(name))?;
|
||||
self.sync_dir();
|
||||
self.reset_sync_log(name, data.len() as u64);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -180,27 +261,31 @@ impl Store for FsStore {
|
||||
self.sync_dir();
|
||||
return Ok(buf.len() as u64);
|
||||
}
|
||||
if self.appender.as_ref().is_some_and(|(n, _)| n == name) {
|
||||
self.appender = None;
|
||||
}
|
||||
self.folder.check()?;
|
||||
let tmp = self.path(&format!("{name}.tmp"));
|
||||
let mut file = create_staging(&tmp)?;
|
||||
let written = (|| {
|
||||
let mut w = Counter { inner: io::BufWriter::with_capacity(1 << 20, create_staging(&tmp)?), n: 0 };
|
||||
let mut w = Counter { inner: io::BufWriter::with_capacity(1 << 20, &mut file), n: 0 };
|
||||
fill(&mut w)?;
|
||||
w.flush()?;
|
||||
let f = w.inner.into_inner().map_err(|e| e.into_error())?;
|
||||
f.sync_all()?;
|
||||
Ok(w.n)
|
||||
let n = w.n;
|
||||
drop(w);
|
||||
file.sync_all()?;
|
||||
Ok(n)
|
||||
})();
|
||||
let n = match written {
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
if check_staging(&self.folder, &tmp, &file).is_ok() {
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
}
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
check_staging(&self.folder, &tmp, &file)?;
|
||||
std::fs::rename(&tmp, self.path(name))?;
|
||||
self.sync_dir();
|
||||
self.reset_sync_log(name, n);
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
@@ -208,31 +293,46 @@ impl Store for FsStore {
|
||||
use std::io::Write;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
if self.log.is_none() && !crate::fault::on_caller(&self.dir) {
|
||||
if self.log.is_none() && self.sync_log.is_none() && !crate::fault::on_caller(&self.dir) {
|
||||
self.folder.check()?;
|
||||
check_regular_directory_entry(&self.path(name))?;
|
||||
if !self.path(name).exists() {
|
||||
// created here, so the open's errors are this append's and its entry is durable
|
||||
std::fs::OpenOptions::new().create(true).append(true).open(self.path(name))?;
|
||||
self.sync_dir();
|
||||
}
|
||||
self.appender = None;
|
||||
self.log = crate::log_writer::LogWriter::start(self.dir.clone(), name);
|
||||
let expected = if name == crate::journal::LOG { self.known_log_len } else { None };
|
||||
self.log = crate::log_writer::LogWriter::start(self.folder.clone(), name, expected);
|
||||
}
|
||||
// (another name than the log's is written here, as without a thread)
|
||||
if let Some(w) = self.log_for(name) {
|
||||
return w.append(data);
|
||||
}
|
||||
}
|
||||
if self.appender.as_ref().is_none_or(|(n, _)| n != name) {
|
||||
check_regular_directory_entry(&self.path(name))?;
|
||||
let existed = self.path(name).exists();
|
||||
let f = std::fs::OpenOptions::new().create(true).append(true).open(self.path(name))?;
|
||||
if !existed {
|
||||
self.sync_dir();
|
||||
}
|
||||
self.appender = Some((name.to_string(), f));
|
||||
self.folder.check()?;
|
||||
// A note-bearing copy can pass the folder check while an old handle still names the
|
||||
// moved-away folder. As the log thread does, reopen the current path for every batch.
|
||||
check_regular_directory_entry(&self.path(name))?;
|
||||
let existed = self.path(name).exists();
|
||||
// Windows append-only handles cannot truncate their own failed write. For LOG,
|
||||
// match the native writer's writable handle and seek to the checked length.
|
||||
let mut options = std::fs::OpenOptions::new();
|
||||
options.create(true).truncate(false);
|
||||
if name == crate::journal::LOG {
|
||||
options.write(true);
|
||||
} else {
|
||||
options.append(true);
|
||||
}
|
||||
let mut f = options.open(self.path(name))?;
|
||||
if !existed {
|
||||
self.sync_dir();
|
||||
}
|
||||
if name == crate::journal::LOG {
|
||||
let len = f.metadata()?.len();
|
||||
let log = self.sync_log.get_or_insert(SynchronousLog { good: self.known_log_len.unwrap_or(len), torn: None });
|
||||
check_log_len(len, log.good)?;
|
||||
std::io::Seek::seek(&mut f, std::io::SeekFrom::Start(log.good))?;
|
||||
}
|
||||
let Some((_, f)) = self.appender.as_mut() else { return Err(io::Error::other("appender not open")) };
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let written = crate::fault::check(&self.dir, crate::fault::Step::Write)
|
||||
.and_then(|()| f.write_all(data))
|
||||
@@ -240,12 +340,16 @@ impl Store for FsStore {
|
||||
.and_then(|()| f.sync_data());
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let written = f.write_all(data).and_then(|()| f.sync_data());
|
||||
if written.is_err() {
|
||||
// The handle may be dead (a network share that reconnected, a removed volume): the next
|
||||
// attempt reopens the file. Part of `data` may be in the file; the caller cuts it back
|
||||
// ([`Store::truncate`]) before appending again.
|
||||
self.appender = None;
|
||||
if name == crate::journal::LOG
|
||||
&& let Some(log) = self.sync_log.as_mut()
|
||||
{
|
||||
log.torn = written.is_err().then_some(f);
|
||||
if written.is_ok() {
|
||||
log.good = log.good.saturating_add(data.len() as u64);
|
||||
}
|
||||
}
|
||||
// A failed write can leave part of `data`; the caller cuts it back with `truncate`
|
||||
// before retrying. The file is reopened for that retry too.
|
||||
written
|
||||
}
|
||||
|
||||
@@ -254,8 +358,11 @@ impl Store for FsStore {
|
||||
if let Some(w) = self.log_for(name) {
|
||||
return w.truncate(len);
|
||||
}
|
||||
if self.appender.as_ref().is_some_and(|(n, _)| n == name) {
|
||||
self.appender = None;
|
||||
self.folder.check()?;
|
||||
if name == crate::journal::LOG
|
||||
&& let Some(log) = self.sync_log.as_mut()
|
||||
{
|
||||
rollback_torn(&mut log.torn, log.good)?;
|
||||
}
|
||||
check_regular_directory_entry(&self.path(name))?;
|
||||
let f = match std::fs::OpenOptions::new().write(true).open(self.path(name)) {
|
||||
@@ -264,8 +371,17 @@ impl Store for FsStore {
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
if name == crate::journal::LOG
|
||||
&& let Some(good) = self.sync_log.as_ref().map(|log| log.good).or(self.known_log_len)
|
||||
{
|
||||
// Also covers initial repair before any append. Our own torn bytes, if any, were
|
||||
// rolled back through their held descriptor, never through this possibly replaced path.
|
||||
check_log_len(f.metadata()?.len(), good)?;
|
||||
}
|
||||
f.set_len(len)?;
|
||||
f.sync_all()
|
||||
f.sync_all()?;
|
||||
self.reset_sync_log(name, len);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn describe(&self) -> String {
|
||||
@@ -274,7 +390,7 @@ impl Store for FsStore {
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn background_writer(&self) -> Option<Box<dyn Store>> {
|
||||
Some(Box::new(FsStore { dir: self.dir.clone(), appender: None, log: None }))
|
||||
Some(Box::new(FsStore { dir: self.dir.clone(), folder: self.folder.clone(), sync_log: None, known_log_len: None, log: None }))
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
@@ -84,6 +84,41 @@ fn a_link_at_the_owner_note_is_never_written_through() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// The note is what tells the folder that was locked from every other folder (issue #3240), so no two
|
||||
/// locks write the same one, also when one process takes them in the same second. A note written
|
||||
/// before it had a value of its own is still read.
|
||||
#[test]
|
||||
fn every_lock_writes_a_note_of_its_own() {
|
||||
let notes: std::collections::HashSet<Vec<u8>> =
|
||||
(0..2_000).map(|_| serde_json::to_vec_pretty(&LockOwner::this_process("LightCraft")).unwrap()).collect();
|
||||
assert_eq!(notes.len(), 2_000, "one note for each lock");
|
||||
let old: LockOwner = serde_json::from_slice(br#"{"pid":7,"host":"elsewhere","program":"LightCraft","version":"0.2.0","since":1}"#).unwrap();
|
||||
assert!(old.pid == 7 && old.token.is_empty());
|
||||
}
|
||||
|
||||
/// Two libraries locked by one process in the same second: the folder of the second, put at the
|
||||
/// path of the first, doesn't hold the first lock's note, so it isn't taken for the folder that was
|
||||
/// locked.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn the_folder_of_another_lock_taken_in_the_same_second_is_not_the_folder_that_was_locked() {
|
||||
let _serial = serial();
|
||||
let (first_dir, second_dir) = (temp_dir("same-second-one"), temp_dir("same-second-two"));
|
||||
let first = LibraryLock::acquire(&first_dir, "LightCraft").unwrap();
|
||||
let second = LibraryLock::acquire(&second_dir, "LightCraft").unwrap();
|
||||
assert_ne!(first.note(), second.note(), "each lock wrote its own note");
|
||||
|
||||
let away = first_dir.with_extension("away");
|
||||
std::fs::rename(&first_dir, &away).unwrap();
|
||||
std::fs::rename(&second_dir, &first_dir).unwrap();
|
||||
let msg = first.folder().unwrap().check().expect_err("another library's folder is at the path now").to_string();
|
||||
assert!(msg.contains("is no longer the folder this library was opened from"), "{msg}");
|
||||
drop(second);
|
||||
drop(first);
|
||||
let _ = std::fs::remove_dir_all(&first_dir);
|
||||
let _ = std::fs::remove_dir_all(&away);
|
||||
}
|
||||
|
||||
/// Another process holds the lock; when it is killed (a crash), the OS releases it.
|
||||
#[test]
|
||||
fn lock_held_by_another_process_is_released_when_it_dies() {
|
||||
@@ -199,3 +234,90 @@ fn dropping_the_lock_frees_the_library_while_another_thread_spawns_processes() {
|
||||
&refused[..refused.len().min(3)]
|
||||
);
|
||||
}
|
||||
|
||||
/// Issue #3240: dropping the lock removes the owner note it wrote, and only that one. Another
|
||||
/// process that has the folder open (the library on a share that the folder was mounted over)
|
||||
/// wrote the note in the folder at the path now; removing it, as dropping the lock did before,
|
||||
/// took away the only hint of who holds that library.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn dropping_the_lock_keeps_the_owner_note_of_a_folder_that_replaced_the_locked_one() {
|
||||
let _serial = serial();
|
||||
let base = temp_dir("owner-replaced");
|
||||
let dir = base.join("lib");
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
let lock = LibraryLock::acquire(&dir, "LightCraft").unwrap();
|
||||
assert!(lock.held());
|
||||
std::fs::rename(&dir, base.join("lib.away")).unwrap();
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
let theirs = br#"{"pid":999999,"host":"elsewhere","program":"LightCraft","version":"0.2.0","since":1}"#;
|
||||
std::fs::write(dir.join(OWNER), theirs).unwrap();
|
||||
drop(lock);
|
||||
assert_eq!(std::fs::read(dir.join(OWNER)).ok().as_deref(), Some(&theirs[..]), "the note of the folder at the path now is theirs");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
/// A folder that holds this lock's note, byte for byte, is the locked folder whatever its numbers say
|
||||
/// ([`OpenedFolder::vouch`]): a copy of the library's folder made while it was open, put where the
|
||||
/// locked one was, counts as it. So the note goes from there too, as from the locked folder.
|
||||
/// (A note with other bytes is another's, as the test above has it.)
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn dropping_the_lock_removes_a_copy_of_its_note_from_the_folder_that_holds_it() {
|
||||
let _serial = serial();
|
||||
let base = temp_dir("owner-copied");
|
||||
let dir = base.join("lib");
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
let lock = LibraryLock::acquire(&dir, "LightCraft").unwrap();
|
||||
let note = std::fs::read(dir.join(OWNER)).unwrap();
|
||||
std::fs::rename(&dir, base.join("lib.away")).unwrap();
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
std::fs::write(dir.join(OWNER), note).unwrap();
|
||||
drop(lock);
|
||||
assert!(!dir.join(OWNER).exists(), "the folder at the path holds exactly this lock's note: it is the locked folder");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
/// The lock vouches for the note it wrote in the record of its folder: that is what recognizes the
|
||||
/// folder when a file system numbers it anew (a drive mounted again), and what the library takes
|
||||
/// over for its stores.
|
||||
#[test]
|
||||
fn the_lock_vouches_for_the_note_it_wrote_in_the_record_of_its_folder() {
|
||||
let _serial = serial();
|
||||
let dir = temp_dir("vouch");
|
||||
let lock = LibraryLock::acquire(&dir, "LightCraft").unwrap();
|
||||
let written = std::fs::read(dir.join(OWNER)).unwrap();
|
||||
assert_eq!(lock.note(), Some(written.as_slice()), "the bytes in the folder");
|
||||
assert_eq!(lock.folder().and_then(OpenedFolder::note), Some(written.as_slice()), "are what its folder's record vouches for");
|
||||
drop(lock);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// In the locked folder itself, a note another process wrote over this lock's is theirs too (a
|
||||
/// share that lost this lock's hold and gave it to another computer): it stays when this lock
|
||||
/// is dropped. On every system.
|
||||
#[test]
|
||||
fn dropping_the_lock_keeps_an_owner_note_another_process_wrote() {
|
||||
let _serial = serial();
|
||||
let dir = temp_dir("owner-theirs");
|
||||
let lock = LibraryLock::acquire(&dir, "LightCraft").unwrap();
|
||||
assert!(lock.held());
|
||||
let theirs = br#"{"pid":999999,"host":"elsewhere","program":"LightCraft","version":"0.2.0","since":1}"#;
|
||||
std::fs::write(dir.join(OWNER), theirs).unwrap();
|
||||
drop(lock);
|
||||
assert_eq!(std::fs::read(dir.join(OWNER)).ok().as_deref(), Some(&theirs[..]), "a note another process wrote is not ours to remove");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// The inverse: the folder is the locked one and the note is as this lock wrote it, so it goes
|
||||
/// (the library can be opened by the next program, with no note left naming this one).
|
||||
#[test]
|
||||
fn dropping_the_lock_removes_its_own_note_from_its_own_folder() {
|
||||
let _serial = serial();
|
||||
let dir = temp_dir("owner-own");
|
||||
let lock = LibraryLock::acquire(&dir, "LightCraft").unwrap();
|
||||
assert!(dir.join(OWNER).is_file());
|
||||
drop(lock);
|
||||
assert!(!dir.join(OWNER).exists());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
@@ -105,7 +105,21 @@ fn a_failed_write_is_held_reported_and_retried() {
|
||||
store.flush().unwrap();
|
||||
let fail = Arc::new(AtomicBool::new(true));
|
||||
let f = fail.clone();
|
||||
fault::install(&dir, Arc::new(move |_| if f.load(Ordering::SeqCst) { Err(std::io::Error::other("disk full (test)")) } else { Ok(()) }));
|
||||
let log_path = dir.join(LOG);
|
||||
fault::install(
|
||||
&dir,
|
||||
Arc::new(move |step| {
|
||||
if f.load(Ordering::SeqCst) && step == fault::Step::Sync {
|
||||
// Inject the torn tail before the failed sync returns. Writing it from the test
|
||||
// thread races a successful retry and can append a foreign tail after that retry.
|
||||
use std::io::Write;
|
||||
std::fs::OpenOptions::new().append(true).open(&log_path)?.write_all(b"{\"seq\":")?;
|
||||
Err(std::io::Error::other("disk full (test)"))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}),
|
||||
);
|
||||
// (one append: the log thread can fail the first before a second arrives, and then refuses it)
|
||||
store.append(LOG, b"1\n2\n").unwrap();
|
||||
wait_until("the failure is published", || store.unwritten().is_some());
|
||||
@@ -120,12 +134,7 @@ fn a_failed_write_is_held_reported_and_retried() {
|
||||
// a cut within what is held keeps it (the journal cuts back to its own length on an error)
|
||||
store.truncate(LOG, 6).unwrap();
|
||||
assert_eq!(store.read(LOG).unwrap().unwrap(), b"0\n1\n2\n");
|
||||
// a fragment of a failed write left in the file is cut before the retry
|
||||
{
|
||||
use std::io::Write;
|
||||
let mut file = std::fs::OpenOptions::new().append(true).open(dir.join(LOG)).unwrap();
|
||||
file.write_all(b"{\"seq\":").unwrap();
|
||||
}
|
||||
// The failed sync injected a fragment: the retry must remove it before writing the held bytes.
|
||||
fail.store(false, Ordering::SeqCst);
|
||||
wait_until("retried on its own", || store.unwritten().is_none());
|
||||
store.append(LOG, b"3\n").unwrap();
|
||||
@@ -135,6 +144,35 @@ fn a_failed_write_is_held_reported_and_retried() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A log longer than the thread wrote is cut back only when a write of its own failed (that
|
||||
/// fragment is its own). A failure that never touched the file (here the write step, as a folder
|
||||
/// refused while another is put at the path) leaves the log it finds alone: it may be another
|
||||
/// folder's, and cutting it would corrupt it.
|
||||
#[test]
|
||||
fn a_log_longer_than_the_thread_wrote_is_not_cut_unless_its_own_write_failed() {
|
||||
let dir = temp_dir("not-torn");
|
||||
let mut store = FsStore::open(&dir).unwrap();
|
||||
store.append(LOG, b"0\n").unwrap();
|
||||
store.flush().unwrap();
|
||||
let fail = Arc::new(AtomicBool::new(true));
|
||||
let f = fail.clone();
|
||||
fault::install(
|
||||
&dir,
|
||||
Arc::new(
|
||||
move |step| if f.load(Ordering::SeqCst) && step == fault::Step::Write { Err(std::io::Error::other("refused (test)")) } else { Ok(()) },
|
||||
),
|
||||
);
|
||||
store.append(LOG, b"1\n").unwrap();
|
||||
wait_until("the failure is published", || store.unwritten().is_some());
|
||||
let theirs = b"0\nrecords of another log\n";
|
||||
std::fs::write(dir.join(LOG), theirs).unwrap();
|
||||
fail.store(false, Ordering::SeqCst);
|
||||
wait_until("looked at again", || store.unwritten().is_some_and(|(_, why)| why.contains("another file")));
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), theirs, "left as it was found");
|
||||
fault::remove(&dir);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// (b, c) Through the journal: ops appended while the drive is slow or failing all reopen, and a
|
||||
/// journal over a log that failed has no gap (no damage on load).
|
||||
#[test]
|
||||
|
||||
@@ -0,0 +1,622 @@
|
||||
//! Issue #3240: a library's files are written to the folder it was opened from, not to whatever its
|
||||
//! path leads to later (a share mounted over the folder, or the folder moved away and another put
|
||||
//! at its path). A folder's identity is its device and inode number, so the tests that move a
|
||||
//! folder run on Unix; the rest hold everywhere.
|
||||
|
||||
#[cfg(unix)]
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(unix)]
|
||||
use crate::journal::{LOG, SNAPSHOT};
|
||||
#[cfg(unix)]
|
||||
use crate::lock::OWNER;
|
||||
use crate::*;
|
||||
|
||||
/// The start of the sentence a refused write ends with.
|
||||
const REFUSED: &str = "is no longer the folder this library was opened from";
|
||||
|
||||
fn scratch(tag: &str) -> PathBuf {
|
||||
let d = std::env::temp_dir().join(format!("lc-opened-folder-{tag}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
std::fs::create_dir_all(&d).unwrap();
|
||||
d
|
||||
}
|
||||
|
||||
/// The files in `dir` with their bytes, by name.
|
||||
#[cfg(unix)]
|
||||
fn files_in(dir: &Path) -> Vec<(String, Vec<u8>)> {
|
||||
let mut files: Vec<_> = std::fs::read_dir(dir)
|
||||
.unwrap()
|
||||
.map(|e| {
|
||||
let e = e.unwrap();
|
||||
(e.file_name().to_string_lossy().into_owned(), std::fs::read(e.path()).unwrap())
|
||||
})
|
||||
.collect();
|
||||
files.sort();
|
||||
files
|
||||
}
|
||||
|
||||
/// Another library's folder where `dir` was, and the folder that was there moved to the path
|
||||
/// returned: what a share mounted over `dir` looks like (the path leads somewhere else, and the
|
||||
/// folder that was opened is out of reach).
|
||||
#[cfg(unix)]
|
||||
fn put_another_folder_at(dir: &Path) -> PathBuf {
|
||||
let away = dir.with_extension("away");
|
||||
std::fs::rename(dir, &away).unwrap();
|
||||
std::fs::create_dir(dir).unwrap();
|
||||
std::fs::write(dir.join(SNAPSHOT), b"their snapshot").unwrap();
|
||||
std::fs::write(dir.join(LOG), b"their log\n").unwrap();
|
||||
std::fs::write(dir.join(OWNER), b"their note").unwrap();
|
||||
away
|
||||
}
|
||||
|
||||
/// Every way a store writes is refused in a folder that replaced the opened one, whether or not
|
||||
/// the log's own thread has started, and nothing is written in either folder.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_store_writes_nothing_into_a_folder_that_replaced_the_one_it_opened() {
|
||||
for log_running in [false, true] {
|
||||
let base = scratch(&format!("store-{log_running}"));
|
||||
let dir = base.join("lib");
|
||||
let mut store = FsStore::open(&dir).unwrap();
|
||||
store.write_atomic(SNAPSHOT, b"ours").unwrap();
|
||||
if log_running {
|
||||
store.append(LOG, b"1\n").unwrap();
|
||||
store.flush().unwrap();
|
||||
}
|
||||
let away = put_another_folder_at(&dir);
|
||||
let (theirs, ours) = (files_in(&dir), files_in(&away));
|
||||
|
||||
let results = [
|
||||
("write_atomic", store.write_atomic(SNAPSHOT, b"x")),
|
||||
("write_atomic_with", store.write_atomic_with(SNAPSHOT, &mut |w| w.write_all(b"x")).map(|_| ())),
|
||||
("write_atomic of the log", store.write_atomic(LOG, b"")),
|
||||
("write_atomic of a settings file", store.write_atomic("prefs.json", b"{}")),
|
||||
("truncate", store.truncate(LOG, 0)),
|
||||
];
|
||||
let appended = store.append(LOG, b"2\n");
|
||||
let flushed = store.flush();
|
||||
|
||||
// what matters most: nothing was written, in either folder
|
||||
assert_eq!(files_in(&dir), theirs, "the folder at the path now is untouched (log thread running: {log_running})");
|
||||
assert_eq!(files_in(&away), ours, "and so is the folder that was opened: saving stops (log thread running: {log_running})");
|
||||
|
||||
// and each write was refused, with the reason
|
||||
for (what, r) in results {
|
||||
let e = r.expect_err(what);
|
||||
assert!(e.to_string().contains(REFUSED), "{what}: {e}");
|
||||
}
|
||||
if log_running {
|
||||
// handed over to the log's thread, which refuses to write it: held, and said so
|
||||
appended.expect("the thread takes it");
|
||||
let e = flushed.expect_err("the thread can't write it");
|
||||
assert!(e.to_string().contains(REFUSED), "{e}");
|
||||
let (held, why) = store.unwritten().expect("held, not dropped");
|
||||
assert!(held == 1 && why.contains(REFUSED), "{held}: {why}");
|
||||
} else {
|
||||
let e = appended.expect_err("the first append");
|
||||
assert!(e.to_string().contains(REFUSED), "{e}");
|
||||
}
|
||||
|
||||
if log_running {
|
||||
// the folder that was opened is back at the path: what was held is written, into it
|
||||
std::fs::rename(&dir, base.join("lib.theirs")).unwrap();
|
||||
std::fs::rename(&away, &dir).unwrap();
|
||||
store.flush().expect("saving resumes");
|
||||
assert_eq!(store.unwritten(), None);
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), b"1\n2\n");
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
}
|
||||
|
||||
/// A compaction that starts after the folder was replaced writes through a second handle on the
|
||||
/// same files: it is bound to the folder the store opened too.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn the_snapshot_writer_of_a_store_is_bound_to_the_folder_the_store_opened() {
|
||||
let base = scratch("worker");
|
||||
let dir = base.join("lib");
|
||||
let store = FsStore::open(&dir).unwrap();
|
||||
let _away = put_another_folder_at(&dir);
|
||||
let theirs = files_in(&dir);
|
||||
let mut worker = store.background_writer().expect("a store on disk has one");
|
||||
let e = worker.write_atomic_with(SNAPSHOT, &mut |w| w.write_all(b"ours")).expect_err("refused");
|
||||
assert!(e.to_string().contains(REFUSED), "{e}");
|
||||
assert_eq!(files_in(&dir), theirs);
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
/// A journal's save (the closing snapshot) in a folder that replaced the opened one fails with the
|
||||
/// reason and writes nothing; with the folder back, the same call saves.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_journal_cannot_snapshot_into_a_folder_that_replaced_the_one_it_opened() {
|
||||
let base = scratch("journal");
|
||||
let dir = base.join("lib");
|
||||
let (mut journal, catalog, _) = Journal::open(Box::new(FsStore::open(&dir).unwrap())).unwrap();
|
||||
let away = put_another_folder_at(&dir);
|
||||
let theirs = files_in(&dir);
|
||||
let e = journal.snapshot(&catalog).expect_err("the path leads to another library's folder");
|
||||
assert!(e.to_string().contains(REFUSED), "{e}");
|
||||
assert_eq!(files_in(&dir), theirs);
|
||||
|
||||
std::fs::rename(&dir, base.join("lib.theirs")).unwrap();
|
||||
std::fs::rename(&away, &dir).unwrap();
|
||||
journal.snapshot(&catalog).expect("the folder is back");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
/// A file system that numbers its folders anew at every mount (FAT and exFAT on Linux, sshfs) gives
|
||||
/// the folder other numbers: it is the opened folder again while it holds the note this session's
|
||||
/// lock wrote, for every way a store writes, the log's thread included. (A mount can't be faked in
|
||||
/// a test: the numbers the store recorded at open are made different from the folder's.)
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_store_writes_into_a_folder_with_other_numbers_while_the_folder_holds_this_sessions_note() {
|
||||
let dir = scratch("renumbered");
|
||||
let note = b"the note this session's lock wrote";
|
||||
let folder = OpenedFolder::recorded_under_other_numbers(&dir);
|
||||
folder.vouch(note);
|
||||
let mut store = FsStore::in_folder(folder);
|
||||
|
||||
let e = store.write_atomic(SNAPSHOT, b"x").expect_err("other numbers, and no note in the folder");
|
||||
assert!(e.to_string().contains(REFUSED), "{e}");
|
||||
assert!(files_in(&dir).is_empty(), "nothing was written");
|
||||
|
||||
std::fs::write(dir.join(OWNER), note).unwrap();
|
||||
store.write_atomic(SNAPSHOT, b"one").expect("the note vouches for the folder");
|
||||
store.write_atomic_with(SNAPSHOT, &mut |w| w.write_all(b"two")).expect("a snapshot written in pieces too");
|
||||
assert_eq!(std::fs::read(dir.join(SNAPSHOT)).unwrap(), b"two");
|
||||
store.append(LOG, b"1\n").expect("the first append starts the log's thread");
|
||||
store.flush().unwrap();
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), b"1\n");
|
||||
|
||||
// another program's note over this session's: what the thread is handed is held, not written
|
||||
std::fs::write(dir.join(OWNER), b"theirs").unwrap();
|
||||
store.append(LOG, b"2\n").expect("the thread takes it");
|
||||
let e = store.flush().expect_err("the thread can't write it");
|
||||
assert!(e.to_string().contains(REFUSED), "{e}");
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), b"1\n");
|
||||
|
||||
std::fs::write(dir.join(OWNER), note).unwrap();
|
||||
store.flush().expect("this session's note is back: what was held is written");
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), b"1\n2\n");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A copy of the library's folder (the note in it too) put at the path counts as the opened folder,
|
||||
/// but its log is not the one the log's thread wrote: the thread opens the log by its path for each
|
||||
/// batch and appends only while it is the length the thread left. A log that isn't is refused like a
|
||||
/// failed write (held, reported): not written on through a file kept open in the folder that was
|
||||
/// moved away, where the copy would never get it, and not appended to at its end, where it would
|
||||
/// have a gap. The log reset of a snapshot replaces it and takes what was held.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_log_that_is_not_the_one_the_thread_wrote_is_not_appended_to() {
|
||||
let base = scratch("log-copy");
|
||||
let dir = base.join("lib");
|
||||
let note = b"the note this session's lock wrote";
|
||||
let mut store = FsStore::open(&dir).unwrap();
|
||||
store.folder().vouch(note);
|
||||
std::fs::write(dir.join(OWNER), note).unwrap();
|
||||
store.append(LOG, b"1\n").unwrap();
|
||||
store.flush().unwrap();
|
||||
|
||||
// a copy made now, which the folder that was opened outgrows, put in its place
|
||||
let copy = base.join("copy");
|
||||
std::fs::create_dir(©).unwrap();
|
||||
for (name, bytes) in files_in(&dir) {
|
||||
std::fs::write(copy.join(name), bytes).unwrap();
|
||||
}
|
||||
store.append(LOG, b"2\n").unwrap();
|
||||
store.flush().unwrap();
|
||||
let away = base.join("away");
|
||||
std::fs::rename(&dir, &away).unwrap();
|
||||
std::fs::rename(©, &dir).unwrap();
|
||||
let (theirs, ours) = (files_in(&dir), files_in(&away));
|
||||
|
||||
store.append(LOG, b"3\n").unwrap();
|
||||
let e = store.flush().expect_err("the log at the path is not the one this session wrote");
|
||||
assert!(e.to_string().contains("holds 2 bytes, the log this session wrote has 4"), "{e}");
|
||||
assert_eq!(files_in(&dir), theirs, "nothing is appended to it");
|
||||
assert_eq!(files_in(&away), ours, "and nothing is written on through a file kept open in the folder that was moved away");
|
||||
assert_eq!(store.unwritten().map(|(n, _)| n), Some(1), "held, not dropped");
|
||||
store.append(LOG, b"4\n").expect("later records are taken: retrying can't heal this, only a replace of the log does");
|
||||
assert_eq!(files_in(&dir), theirs, "and not written either");
|
||||
|
||||
store.write_atomic(LOG, b"tail\n3\n4\n").unwrap();
|
||||
assert_eq!(store.unwritten(), None, "the snapshot's log holds what was held");
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), b"tail\n3\n4\n", "the replacement preserves both held records");
|
||||
store.append(LOG, b"5\n").unwrap();
|
||||
store.flush().unwrap();
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), b"tail\n3\n4\n5\n", "and appending goes on");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_folder_is_the_opened_one_while_its_path_leads_to_it() {
|
||||
let dir = scratch("same");
|
||||
let folder = OpenedFolder::at(&dir).unwrap();
|
||||
folder.check().unwrap();
|
||||
std::fs::write(dir.join("catalog.snap"), b"x").unwrap();
|
||||
std::fs::create_dir(dir.join("thumbs")).unwrap();
|
||||
folder.check().expect("what is written in it changes nothing");
|
||||
assert!(folder.same_folder(&folder.clone()));
|
||||
assert_eq!(folder.path(), dir);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_path_that_is_not_a_folder_cannot_be_opened() {
|
||||
let dir = scratch("not-a-folder");
|
||||
let file = dir.join("file");
|
||||
std::fs::write(&file, b"x").unwrap();
|
||||
assert!(OpenedFolder::at(&file).expect_err("a file").to_string().contains("is not a folder"));
|
||||
assert!(OpenedFolder::at(&dir.join("missing")).is_err());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A folder that is gone is said to be gone, on every system.
|
||||
#[test]
|
||||
fn a_folder_that_is_gone_is_reported_as_gone() {
|
||||
let base = scratch("gone");
|
||||
let dir = base.join("lib");
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
let folder = OpenedFolder::at(&dir).unwrap();
|
||||
std::fs::remove_dir(&dir).unwrap();
|
||||
let e = folder.check().expect_err("nothing is there");
|
||||
let msg = e.to_string();
|
||||
assert!(msg.contains(REFUSED) && msg.contains("nothing is there now") && msg.contains(&dir.display().to_string()), "{msg}");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
/// The reason names the path and what to do; a folder moved and replaced is told from a gone one.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_replaced_folder_is_refused_until_the_opened_one_is_back() {
|
||||
let base = scratch("replaced");
|
||||
let dir = base.join("lib");
|
||||
std::fs::create_dir(&dir).unwrap();
|
||||
let folder = OpenedFolder::at(&dir).unwrap();
|
||||
let away = put_another_folder_at(&dir);
|
||||
let msg = folder.check().expect_err("another folder is at the path").to_string();
|
||||
assert!(msg.contains(REFUSED) && msg.contains("moved or replaced") && msg.contains(&dir.display().to_string()), "{msg}");
|
||||
assert!(msg.contains("put the original folder back"), "{msg}");
|
||||
assert!(!folder.same_folder(&OpenedFolder::at(&dir).unwrap()), "the other folder is another folder");
|
||||
|
||||
std::fs::rename(&dir, base.join("lib.theirs")).unwrap();
|
||||
std::fs::rename(&away, &dir).unwrap();
|
||||
folder.check().expect("the opened folder is back");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
/// A library reached through a link is the folder the link leads to: a link that stays is no
|
||||
/// reason to refuse, and one pointed at another folder is.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_link_to_the_folder_is_followed() {
|
||||
let base = scratch("link");
|
||||
let (one, two, link) = (base.join("one"), base.join("two"), base.join("link"));
|
||||
std::fs::create_dir(&one).unwrap();
|
||||
std::fs::create_dir(&two).unwrap();
|
||||
std::os::unix::fs::symlink(&one, &link).unwrap();
|
||||
let folder = OpenedFolder::at(&link).unwrap();
|
||||
folder.check().expect("the link still leads to it");
|
||||
std::fs::remove_file(&link).unwrap();
|
||||
std::os::unix::fs::symlink(&two, &link).unwrap();
|
||||
assert!(folder.check().is_err(), "the link leads to another folder now");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_snapshot_swap_during_fill_cannot_publish_another_librarys_staging_file() {
|
||||
let dir = scratch("stream-swap-publish");
|
||||
let mut store = FsStore::open(&dir).unwrap();
|
||||
let mut away = None;
|
||||
let result = store.write_atomic_with(SNAPSHOT, &mut |writer| {
|
||||
writer.write_all(b"our pending snapshot")?;
|
||||
away = Some(put_another_folder_at(&dir));
|
||||
std::fs::write(dir.join(format!("{SNAPSHOT}.tmp")), b"their pending snapshot")?;
|
||||
Ok(())
|
||||
});
|
||||
assert!(result.is_err(), "a folder swapped during a streamed save must be refused");
|
||||
assert_eq!(std::fs::read(dir.join(SNAPSHOT)).unwrap(), b"their snapshot");
|
||||
assert_eq!(std::fs::read(dir.join(format!("{SNAPSHOT}.tmp"))).unwrap(), b"their pending snapshot");
|
||||
let _ = std::fs::remove_dir_all(away.unwrap());
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_snapshot_swap_during_fill_cannot_remove_another_librarys_staging_file() {
|
||||
let dir = scratch("stream-swap-cleanup");
|
||||
let mut store = FsStore::open(&dir).unwrap();
|
||||
let mut away = None;
|
||||
let result = store.write_atomic_with(SNAPSHOT, &mut |writer| {
|
||||
writer.write_all(b"our pending snapshot")?;
|
||||
away = Some(put_another_folder_at(&dir));
|
||||
std::fs::write(dir.join(format!("{SNAPSHOT}.tmp")), b"their pending snapshot")?;
|
||||
Err(std::io::Error::other("streaming save failed"))
|
||||
});
|
||||
assert!(result.is_err());
|
||||
assert_eq!(std::fs::read(dir.join(SNAPSHOT)).unwrap(), b"their snapshot");
|
||||
assert_eq!(std::fs::read(dir.join(format!("{SNAPSHOT}.tmp"))).unwrap(), b"their pending snapshot");
|
||||
let _ = std::fs::remove_dir_all(away.unwrap());
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_caller_append_rolls_back_its_opened_file_not_the_replacement() {
|
||||
let dir = scratch("caller-held-file-rollback");
|
||||
let log = crate::journal::LOG;
|
||||
crate::fault::write_on_caller(&dir, true);
|
||||
let mut store = FsStore::open(&dir).unwrap();
|
||||
store.append(log, b"first\n").unwrap();
|
||||
crate::fault::install(
|
||||
&dir,
|
||||
std::sync::Arc::new(|step| if step == crate::fault::Step::Sync { Err(std::io::Error::other("injected sync failure")) } else { Ok(()) }),
|
||||
);
|
||||
assert!(store.append(log, b"torn\n").is_err());
|
||||
crate::fault::remove(&dir);
|
||||
let held = dir.join("held.log");
|
||||
std::fs::rename(dir.join(log), &held).unwrap();
|
||||
let replacement = b"other\nextra\n";
|
||||
std::fs::write(dir.join(log), replacement).unwrap();
|
||||
assert!(store.truncate(log, 6).is_err(), "cleanup must refuse the replacement");
|
||||
assert_eq!(std::fs::read(&held).unwrap(), b"first\n", "the retained descriptor must allow rollback on every native platform");
|
||||
assert_eq!(std::fs::read(dir.join(log)).unwrap(), replacement);
|
||||
store.write_atomic(log, b"first\n").unwrap();
|
||||
store.append(log, b"next\n").unwrap();
|
||||
assert_eq!(store.read(log).unwrap().unwrap(), b"first\nnext\n");
|
||||
crate::fault::write_on_caller(&dir, false);
|
||||
drop(store);
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_caller_append_reopens_the_file_after_a_note_bearing_copy_replaces_the_folder() {
|
||||
let base = scratch("caller-append-copy");
|
||||
let dir = base.join("lib");
|
||||
let note = b"the note this session's lock wrote";
|
||||
crate::fault::write_on_caller(&dir, true);
|
||||
let mut store = FsStore::open(&dir).unwrap();
|
||||
store.folder().vouch(note);
|
||||
std::fs::write(dir.join(OWNER), note).unwrap();
|
||||
store.append(LOG, b"1\n").unwrap();
|
||||
let copy = base.join("copy");
|
||||
std::fs::create_dir(©).unwrap();
|
||||
for (name, bytes) in files_in(&dir) {
|
||||
std::fs::write(copy.join(name), bytes).unwrap();
|
||||
}
|
||||
let away = base.join("away");
|
||||
std::fs::rename(&dir, &away).unwrap();
|
||||
std::fs::rename(©, &dir).unwrap();
|
||||
store.append(LOG, b"2\n").unwrap();
|
||||
crate::fault::write_on_caller(&dir, false);
|
||||
assert_eq!(store.read(LOG).unwrap().unwrap(), b"1\n2\n", "the accepted folder must receive the append");
|
||||
assert_eq!(std::fs::read(away.join(LOG)).unwrap(), b"1\n", "the moved-away folder must not receive it");
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_caller_append_refuses_a_changed_log_and_a_snapshot_preserves_every_op() {
|
||||
for longer in [false, true] {
|
||||
let base = scratch(&format!("caller-log-length-{longer}"));
|
||||
let dir = base.join("lib");
|
||||
crate::fault::write_on_caller(&dir, true);
|
||||
let store = FsStore::open(&dir).unwrap();
|
||||
let note = b"the note this session's lock wrote";
|
||||
store.folder().vouch(note);
|
||||
std::fs::write(dir.join(OWNER), note).unwrap();
|
||||
let (mut journal, mut catalog, _) = Journal::open(Box::new(store)).unwrap();
|
||||
let add = |n| Op::AddAlbum { album: Album::new(AlbumId(n), format!("album {n}")) };
|
||||
let first = add(1);
|
||||
catalog.apply(first.clone()).unwrap();
|
||||
journal.append(&[first]).unwrap();
|
||||
let copy = base.join("copy");
|
||||
std::fs::create_dir(©).unwrap();
|
||||
for (name, bytes) in files_in(&dir) {
|
||||
std::fs::write(copy.join(name), bytes).unwrap();
|
||||
}
|
||||
let second = add(2);
|
||||
catalog.apply(second.clone()).unwrap();
|
||||
journal.append(&[second]).unwrap();
|
||||
if longer {
|
||||
let mut bytes = std::fs::read(copy.join(LOG)).unwrap();
|
||||
bytes.extend_from_slice(&[b'x'; 4096]);
|
||||
std::fs::write(copy.join(LOG), bytes).unwrap();
|
||||
}
|
||||
let away = base.join("away");
|
||||
std::fs::rename(&dir, &away).unwrap();
|
||||
std::fs::rename(©, &dir).unwrap();
|
||||
let replacement = std::fs::read(dir.join(LOG)).unwrap();
|
||||
let acknowledged = std::fs::read(away.join(LOG)).unwrap();
|
||||
let third = add(3);
|
||||
catalog.apply(third.clone()).unwrap();
|
||||
let result = journal.append(&[third]);
|
||||
crate::fault::write_on_caller(&dir, false);
|
||||
assert!(result.is_err(), "a changed fallback log must not acknowledge a record after a gap");
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), replacement, "even the failed-append cleanup must leave the foreign log alone");
|
||||
assert_eq!(std::fs::read(away.join(LOG)).unwrap(), acknowledged);
|
||||
assert!(journal.wants_snapshot(), "the synchronous error requests a snapshot of the live catalog");
|
||||
journal.snapshot_with_unlogged(&catalog, 1).unwrap();
|
||||
let fourth = add(4);
|
||||
catalog.apply(fourth.clone()).unwrap();
|
||||
journal.append(&[fourth]).unwrap();
|
||||
journal.flush().unwrap();
|
||||
drop(journal);
|
||||
let (reopened, loaded, _) = Journal::open(Box::new(FsStore::open(&dir).unwrap())).unwrap();
|
||||
assert_eq!(reopened.seq(), 4);
|
||||
for n in 1..=4 {
|
||||
assert_eq!(loaded.album(AlbumId(n)).unwrap().name, format!("album {n}"));
|
||||
}
|
||||
drop(reopened);
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_log_open_expectation_survives_a_copy_before_the_first_append() {
|
||||
for caller in [true, false] {
|
||||
let base = scratch(&format!("open-log-copy-{caller}"));
|
||||
let dir = base.join("lib");
|
||||
crate::fault::write_on_caller(&dir, caller);
|
||||
let note = b"the note this session's lock wrote";
|
||||
let open = || {
|
||||
let store = FsStore::open(&dir).unwrap();
|
||||
store.folder().vouch(note);
|
||||
std::fs::write(dir.join(OWNER), note).unwrap();
|
||||
Journal::open(Box::new(store)).unwrap()
|
||||
};
|
||||
let add = |n| Op::AddAlbum { album: Album::new(AlbumId(n), format!("album {n}")) };
|
||||
let (mut seed, mut catalog, _) = open();
|
||||
let first = add(1);
|
||||
catalog.apply(first.clone()).unwrap();
|
||||
seed.append(&[first]).unwrap();
|
||||
seed.flush().unwrap();
|
||||
let copy = base.join("copy");
|
||||
std::fs::create_dir(©).unwrap();
|
||||
for (name, bytes) in files_in(&dir) {
|
||||
std::fs::write(copy.join(name), bytes).unwrap();
|
||||
}
|
||||
let second = add(2);
|
||||
catalog.apply(second.clone()).unwrap();
|
||||
seed.append(&[second]).unwrap();
|
||||
seed.flush().unwrap();
|
||||
drop(seed);
|
||||
let (mut journal, mut catalog, _) = open();
|
||||
std::fs::rename(&dir, base.join("away")).unwrap();
|
||||
std::fs::rename(©, &dir).unwrap();
|
||||
let replacement = std::fs::read(dir.join(LOG)).unwrap();
|
||||
let third = add(3);
|
||||
catalog.apply(third.clone()).unwrap();
|
||||
let result = journal.append(&[third]);
|
||||
let unlogged = u64::from(result.is_err());
|
||||
let refused = result.is_err() || journal.flush().is_err();
|
||||
crate::fault::write_on_caller(&dir, false);
|
||||
assert!(refused, "the first append must use the log length read at open");
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), replacement);
|
||||
journal.snapshot_with_unlogged(&catalog, unlogged).unwrap();
|
||||
drop(journal);
|
||||
let (reopened, loaded, _) = open();
|
||||
assert_eq!(reopened.seq(), 3);
|
||||
for n in 1..=3 {
|
||||
assert!(loaded.album(AlbumId(n)).is_some(), "every acknowledged and unlogged op must reload");
|
||||
}
|
||||
drop(reopened);
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_log_sync_failure_can_only_roll_back_the_file_that_failed() {
|
||||
for caller in [true, false] {
|
||||
let base = scratch(&format!("failed-log-copy-{caller}"));
|
||||
let dir = base.join("lib");
|
||||
crate::fault::write_on_caller(&dir, caller);
|
||||
let mut store = FsStore::open(&dir).unwrap();
|
||||
let note = b"the note this session's lock wrote";
|
||||
store.folder().vouch(note);
|
||||
std::fs::write(dir.join(OWNER), note).unwrap();
|
||||
store.append(LOG, b"1\n").unwrap();
|
||||
store.flush().unwrap();
|
||||
let copy = base.join("copy");
|
||||
std::fs::create_dir(©).unwrap();
|
||||
let foreign = b"this is a longer log whose records must not be removed\n";
|
||||
std::fs::write(copy.join(LOG), foreign).unwrap();
|
||||
std::fs::write(copy.join(OWNER), note).unwrap();
|
||||
let dir_for_hook = dir.clone();
|
||||
let away = base.join("away");
|
||||
let away_for_hook = away.clone();
|
||||
let swapped = std::sync::atomic::AtomicBool::new(false);
|
||||
crate::fault::install(
|
||||
&dir,
|
||||
std::sync::Arc::new(move |step| {
|
||||
if step == crate::fault::Step::Sync {
|
||||
if !swapped.swap(true, std::sync::atomic::Ordering::SeqCst) {
|
||||
std::fs::rename(&dir_for_hook, &away_for_hook)?;
|
||||
std::fs::rename(©, &dir_for_hook)?;
|
||||
}
|
||||
return Err(std::io::Error::other("injected failed sync"));
|
||||
}
|
||||
Ok(())
|
||||
}),
|
||||
);
|
||||
let result = store.append(LOG, b"2\n");
|
||||
if caller {
|
||||
assert!(result.is_err());
|
||||
assert!(store.truncate(LOG, 2).is_err(), "cleanup must refuse the replacement log");
|
||||
} else {
|
||||
result.unwrap();
|
||||
assert!(store.flush().is_err());
|
||||
assert!(store.truncate(LOG, 2).is_err(), "cleanup must refuse the replacement log");
|
||||
}
|
||||
crate::fault::remove(&dir);
|
||||
crate::fault::write_on_caller(&dir, false);
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), foreign, "a torn-tail rollback must never touch the replacement");
|
||||
assert_eq!(std::fs::read(away.join(LOG)).unwrap(), b"1\n", "only our own failed append is rolled back");
|
||||
store.write_atomic(LOG, b"tail\n1\n2\n").unwrap();
|
||||
store.append(LOG, b"3\n").unwrap();
|
||||
store.flush().unwrap();
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), b"tail\n1\n2\n3\n");
|
||||
drop(store);
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_log_read_for_repair_does_not_permit_truncating_a_shorter_copy() {
|
||||
let base = scratch("read-repair-copy");
|
||||
let dir = base.join("lib");
|
||||
let mut store = FsStore::open(&dir).unwrap();
|
||||
let note = b"the note this session's lock wrote";
|
||||
store.folder().vouch(note);
|
||||
std::fs::write(dir.join(OWNER), note).unwrap();
|
||||
std::fs::write(dir.join(LOG), b"good\ntorn").unwrap();
|
||||
assert_eq!(store.read(LOG).unwrap().unwrap(), b"good\ntorn");
|
||||
let copy = base.join("copy");
|
||||
std::fs::create_dir(©).unwrap();
|
||||
std::fs::write(copy.join(OWNER), note).unwrap();
|
||||
std::fs::write(copy.join(LOG), b"x\n").unwrap();
|
||||
let away = base.join("away");
|
||||
std::fs::rename(&dir, &away).unwrap();
|
||||
std::fs::rename(©, &dir).unwrap();
|
||||
assert!(store.truncate(LOG, 5).is_err(), "initial repair must use the length read, not extend a replacement");
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), b"x\n");
|
||||
assert_eq!(std::fs::read(away.join(LOG)).unwrap(), b"good\ntorn");
|
||||
store.write_atomic(LOG, b"whole\n").unwrap();
|
||||
store.append(LOG, b"next\n").unwrap();
|
||||
store.flush().unwrap();
|
||||
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), b"whole\nnext\n");
|
||||
drop(store);
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_replaced_staging_name_is_neither_published_nor_removed() {
|
||||
for fail_fill in [false, true] {
|
||||
let dir = scratch(&format!("staging-name-{fail_fill}"));
|
||||
let mut store = FsStore::open(&dir).unwrap();
|
||||
store.write_atomic(SNAPSHOT, b"committed snapshot").unwrap();
|
||||
let tmp = dir.join(format!("{SNAPSHOT}.tmp"));
|
||||
let result = store.write_atomic_with(SNAPSHOT, &mut |writer| {
|
||||
writer.write_all(b"our pending snapshot")?;
|
||||
std::fs::remove_file(&tmp)?;
|
||||
std::fs::write(&tmp, b"another save's staging file")?;
|
||||
if fail_fill { Err(std::io::Error::other("stream failed")) } else { Ok(()) }
|
||||
});
|
||||
assert!(result.is_err(), "a staging name no longer belonging to this save must be refused");
|
||||
assert_eq!(std::fs::read(dir.join(SNAPSHOT)).unwrap(), b"committed snapshot");
|
||||
assert_eq!(std::fs::read(&tmp).unwrap(), b"another save's staging file");
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
}
|
||||
+17
-19
@@ -12,49 +12,47 @@ 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-cms = { workspace = true }
|
||||
craft-jpeg = { workspace = true }
|
||||
craft-image-codecs = { workspace = true }
|
||||
craft-tiff = { workspace = true }
|
||||
craft-gif = { workspace = true }
|
||||
craft-psd = { workspace = true }
|
||||
craft-heif = { workspace = true, optional = true }
|
||||
serde = { workspace = true }
|
||||
bytemuck = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
rayon = { workspace = true, optional = true }
|
||||
image = { workspace = true, features = ["gif", "bmp"] }
|
||||
zune-jpeg = { workspace = true }
|
||||
zune-core = { workspace = true }
|
||||
jpeg-decoder = { workspace = true }
|
||||
jpeg-encoder = { workspace = true }
|
||||
png = { workspace = true }
|
||||
tiff = { workspace = true }
|
||||
image-webp = { workspace = true }
|
||||
moxcms = { workspace = true }
|
||||
jxl-oxide = { workspace = true, optional = true }
|
||||
craft-jxl = { workspace = true, optional = true }
|
||||
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
ravif = { workspace = true, optional = true }
|
||||
rav1e = { workspace = true, optional = true }
|
||||
avif-serialize = { workspace = true, optional = true }
|
||||
# spawns threads of its own (std::thread), which wasm32 has not got
|
||||
jixel = { workspace = true, optional = true }
|
||||
|
||||
[features]
|
||||
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
|
||||
parallel = ["dep:rayon", "lightcraft-raster/parallel", "craft-jpeg/parallel"]
|
||||
## JPEG XL decoding and encoding via the shared craft-jxl codec.
|
||||
jxl = ["dep:craft-jxl"]
|
||||
# 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"]
|
||||
|
||||
[dev-dependencies]
|
||||
# tests record why they skip (`cargo xtask ci` lists the skips)
|
||||
lightcraft-testkit = { workspace = true }
|
||||
# JPEG parity tests and the benchmark use the prior/reference encoder only in development.
|
||||
jpeg-encoder = { workspace = true }
|
||||
# JPEG XL tests read encoded samples without our colour conversion
|
||||
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 }
|
||||
@@ -46,15 +46,15 @@ CRW, MRW, X3F) are detected so the engine can route them to `lightcraft-raw`; `d
|
||||
|
||||
| Format | Decode | Encode | Notes |
|
||||
|---|---|---|---|
|
||||
| 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 |
|
||||
| JPEG | yes (zune-jpeg; jpeg-decoder for DCT scaling, CMYK/YCCK, 12-bit, non-interleaved scans) | yes (shared `craft-jpeg`, optimized 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 |
|
||||
| JPEG XL | yes (`craft-jxl`, feature `jxl`, default on) | yes (`craft-jxl`, `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 shared 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
|
||||
@@ -85,14 +85,15 @@ All pure Rust, verified with `cargo info` / `cargo tree`:
|
||||
|---|---|---|
|
||||
| zune-jpeg, zune-core | MIT OR Apache-2.0 OR Zlib | JPEG decode |
|
||||
| jpeg-decoder | MIT OR Apache-2.0 | scaled / CMYK JPEG decode |
|
||||
| jpeg-encoder | (MIT OR Apache-2.0) AND IJG | JPEG encode (IJG: permissive, attribution in docs) |
|
||||
| craft-jpeg | MIT OR Apache-2.0 | baseline JPEG encode (shared craft-libs) |
|
||||
| jpeg-encoder | (MIT OR Apache-2.0) AND IJG | 0.6 still ships inside `craft-image-codecs` (IJG: permissive, attribution in docs); 0.7 only in tests and the JPEG benchmark (reference CMYK fixture, prior 4:2:2 output) |
|
||||
| png | MIT OR Apache-2.0 | PNG |
|
||||
| tiff | MIT | TIFF |
|
||||
| image-webp | MIT OR Apache-2.0 | WebP |
|
||||
| image (gif, bmp only) | MIT OR Apache-2.0 | GIF/BMP |
|
||||
| moxcms | BSD-3-Clause OR Apache-2.0 | ICC parse/write, CMS |
|
||||
| jxl-oxide | MIT OR Apache-2.0 | JPEG XL decode |
|
||||
| jixel | BSD-3-Clause OR Apache-2.0 | JPEG XL encode (native only) |
|
||||
| craft-jxl | MIT OR Apache-2.0 | JPEG XL decode/encode (shared craft-libs) |
|
||||
| jxl-oxide | MIT OR Apache-2.0 | JPEG XL test oracle only |
|
||||
| ravif (+ rav1e, avif-serialize) | BSD-3-Clause / BSD-2-Clause | AVIF encode (native only; heavy to compile) |
|
||||
|
||||
Deliberately **not** used: anything AGPL from imazen (heic, zenjpeg, jxl-encoder, rav1d-safe),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Compare the parallel JPEG encoder with `jpeg-encoder` (size, time, PSNR):
|
||||
//! Compare the shared JPEG encoder with `jpeg-encoder` (size, time, PSNR):
|
||||
//! `cargo run --release -p lightcraft-codecs --example jpeg_bench [image]` (default: 6000×4000 synthetic).
|
||||
use lightcraft_codecs::ChromaSubsampling;
|
||||
use lightcraft_codecs::{ChromaSubsampling, EncodeImage, EncodeMeta, Samples, encode_jpeg};
|
||||
use std::time::Instant;
|
||||
|
||||
fn psnr(a: &[u8], b: &[u8]) -> f64 {
|
||||
@@ -23,8 +23,10 @@ fn best<T>(n: usize, mut f: impl FnMut() -> T) -> (T, f64) {
|
||||
fn main() {
|
||||
let (w, h, rgb) = match std::env::args().nth(1) {
|
||||
Some(p) => {
|
||||
let img = image::open(p).expect("open").to_rgb8();
|
||||
(img.width() as usize, img.height() as usize, img.into_raw())
|
||||
let bytes = std::fs::read(p).expect("read");
|
||||
let img = lightcraft_codecs::decode(&bytes, lightcraft_codecs::DecodeOptions::default()).expect("open").to_srgb8();
|
||||
let rgb = img.data.iter().flat_map(|p| [p[0], p[1], p[2]]).collect();
|
||||
(img.width as usize, img.height as usize, rgb)
|
||||
}
|
||||
None => {
|
||||
let (w, h) = (6000, 4000);
|
||||
@@ -41,7 +43,8 @@ fn main() {
|
||||
for (sub, js) in
|
||||
[(ChromaSubsampling::S444, jpeg_encoder::SamplingFactor::R_4_4_4), (ChromaSubsampling::S420, jpeg_encoder::SamplingFactor::R_4_2_0)]
|
||||
{
|
||||
let (ours, t_ours) = best(5, || lightcraft_codecs::jpeg_par::encode(&rgb, w, h, 3, 90, sub, &[]));
|
||||
let img = EncodeImage::new(w as u32, h as u32, 3, Samples::U8(&rgb));
|
||||
let (ours, t_ours) = best(5, || encode_jpeg(&img, 90, sub, &EncodeMeta::default()).expect("shared JPEG encoder"));
|
||||
let (theirs, t_theirs) = best(3, || {
|
||||
let mut out = Vec::new();
|
||||
let mut e = jpeg_encoder::Encoder::new(&mut out, 90);
|
||||
@@ -49,9 +52,17 @@ fn main() {
|
||||
e.encode(&rgb, w as u16, h as u16, jpeg_encoder::ColorType::Rgb).unwrap();
|
||||
out
|
||||
});
|
||||
let dec = |b: &[u8]| jpeg_decoder::Decoder::new(b).decode().unwrap();
|
||||
let dec = |b: &[u8]| {
|
||||
lightcraft_codecs::decode(b, lightcraft_codecs::DecodeOptions::default())
|
||||
.unwrap()
|
||||
.to_srgb8()
|
||||
.data
|
||||
.into_iter()
|
||||
.flat_map(|p| [p[0], p[1], p[2]])
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
println!(
|
||||
"{w}x{h} {sub:?} q90: parallel {t_ours:.0} ms, {} KB, PSNR {:.2} dB | jpeg-encoder {t_theirs:.0} ms, {} KB, PSNR {:.2} dB",
|
||||
"{w}x{h} {sub:?} q90: craft-jpeg {t_ours:.0} ms, {} KB, PSNR {:.2} dB | jpeg-encoder {t_theirs:.0} ms, {} KB, PSNR {:.2} dB",
|
||||
ours.len() / 1024,
|
||||
psnr(&rgb, &dec(&ours)),
|
||||
theirs.len() / 1024,
|
||||
|
||||
@@ -9,13 +9,11 @@
|
||||
//!
|
||||
//! All conversions are relative colorimetric: the display's white is the image's white.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::icc;
|
||||
use crate::space::Trc;
|
||||
use craft_cms::{Builtin, ColorSpace, Intent, Pcs, Profile, Transform, TransformOptions};
|
||||
use lightcraft_color::{D50, D65, Mat3, REC2020, bradford};
|
||||
use lightcraft_raster::Rgba8;
|
||||
use moxcms::{ColorProfile, DataColorSpace, Layout, RenderingIntent, Transform8BitExecutor, TransformOptions, Xyzd};
|
||||
|
||||
/// Largest profile file accepted (monitor profiles are a few KB; LUT profiles up to a few MB).
|
||||
pub const MAX_PROFILE_BYTES: usize = 32 << 20;
|
||||
@@ -46,7 +44,7 @@ enum Correction {
|
||||
/// Per-channel tables (matrix/TRC profile: same primaries, other curves).
|
||||
Curves(Box<[[u8; 256]; 3]>),
|
||||
/// A full CMS transform (LUT-based profile).
|
||||
Cms(Arc<Transform8BitExecutor>),
|
||||
Cms(Box<Transform>),
|
||||
}
|
||||
|
||||
/// A parsed display profile with its transforms (built once, applied to every preview).
|
||||
@@ -59,8 +57,8 @@ pub struct DisplayProfile {
|
||||
/// Linear display RGB → linear Rec.2020 (D65; display white → (1, 1, 1)).
|
||||
pub to_rec2020: Mat3,
|
||||
correction: Correction,
|
||||
from_srgb: Arc<Transform8BitExecutor>,
|
||||
to_srgb: Arc<Transform8BitExecutor>,
|
||||
from_srgb: Transform,
|
||||
to_srgb: Transform,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for DisplayProfile {
|
||||
@@ -75,11 +73,7 @@ impl std::fmt::Debug for DisplayProfile {
|
||||
}
|
||||
|
||||
fn opts() -> TransformOptions {
|
||||
TransformOptions { rendering_intent: RenderingIntent::RelativeColorimetric, ..TransformOptions::default() }
|
||||
}
|
||||
|
||||
fn xyzd(v: [f64; 3]) -> Xyzd {
|
||||
Xyzd { x: v[0], y: v[1], z: v[2] }
|
||||
TransformOptions { intent: Intent::RelativeColorimetric, bpc: false, precise_float: true }
|
||||
}
|
||||
|
||||
/// Linear Rec.2020 → XYZ relative to D50 (the ICC PCS), Bradford-adapted.
|
||||
@@ -100,11 +94,6 @@ fn hash(bytes: &[u8]) -> u64 {
|
||||
h.finish() | 1
|
||||
}
|
||||
|
||||
/// Runs a moxcms call that may panic on a hostile profile.
|
||||
fn guarded<T>(f: impl FnOnce() -> Option<T>) -> Option<T> {
|
||||
std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).ok().flatten()
|
||||
}
|
||||
|
||||
impl DisplayProfile {
|
||||
/// Parse a display profile (an RGB ICC profile, matrix/TRC or LUT-based) and build its
|
||||
/// transforms. Errors say what is wrong with the file; nothing here panics.
|
||||
@@ -112,11 +101,11 @@ impl DisplayProfile {
|
||||
if bytes.len() > MAX_PROFILE_BYTES {
|
||||
return Err(format!("the profile is too large ({} MB)", bytes.len() >> 20));
|
||||
}
|
||||
let p = guarded(|| ColorProfile::new_from_slice(bytes).ok()).ok_or("not a valid ICC profile")?;
|
||||
if p.color_space != DataColorSpace::Rgb {
|
||||
let p = Profile::parse(bytes).map_err(|_| "not a valid ICC profile")?;
|
||||
if p.color_space != ColorSpace::Rgb {
|
||||
return Err("not an RGB profile (a display profile describes an RGB device)".into());
|
||||
}
|
||||
if !matches!(p.pcs, DataColorSpace::Xyz | DataColorSpace::Lab) {
|
||||
if !matches!(p.pcs, Pcs::Xyz | Pcs::Lab) {
|
||||
return Err("unsupported profile connection space".into());
|
||||
}
|
||||
let info = icc::parse(bytes).ok_or("not a valid ICC profile")?;
|
||||
@@ -127,11 +116,8 @@ impl DisplayProfile {
|
||||
.filter(|d| !d.is_empty())
|
||||
.unwrap_or("Display profile")
|
||||
.to_string();
|
||||
let has_lut = p.lut_a_to_b_colorimetric.is_some()
|
||||
|| p.lut_a_to_b_perceptual.is_some()
|
||||
|| p.lut_b_to_a_colorimetric.is_some()
|
||||
|| p.lut_b_to_a_perceptual.is_some();
|
||||
let srgb = ColorProfile::new_srgb();
|
||||
let has_lut = p.a2b[0].is_some() || p.a2b[1].is_some() || p.b2a[0].is_some() || p.b2a[1].is_some();
|
||||
let srgb = Builtin::Srgb.profile().clone();
|
||||
|
||||
// the primaries the pipeline renders into
|
||||
let (kind, to_xyz_d50, curves) = match &info.kind {
|
||||
@@ -147,17 +133,14 @@ impl DisplayProfile {
|
||||
|
||||
// the proxy: the display's primaries (as the pipeline targets them), sRGB curve
|
||||
let proxy_xyz = rec2020_to_xyz_d50().mul(&to_rec2020).0;
|
||||
let mut proxy = ColorProfile::new_srgb();
|
||||
proxy.cicp = None;
|
||||
proxy.red_colorant = xyzd([proxy_xyz[0][0], proxy_xyz[1][0], proxy_xyz[2][0]]);
|
||||
proxy.green_colorant = xyzd([proxy_xyz[0][1], proxy_xyz[1][1], proxy_xyz[2][1]]);
|
||||
proxy.blue_colorant = xyzd([proxy_xyz[0][2], proxy_xyz[1][2], proxy_xyz[2][2]]);
|
||||
let mut proxy = Builtin::Srgb.profile().clone();
|
||||
proxy.matrix = Some(proxy_xyz);
|
||||
|
||||
let transform = |src: &ColorProfile, dst: &ColorProfile| guarded(|| src.create_transform_8bit(Layout::Rgba, dst, Layout::Rgba, opts()).ok());
|
||||
let transform = |src: &Profile, dst: &Profile| Transform::with_options(src, dst, opts()).ok();
|
||||
let correction = match curves {
|
||||
Some(trc) if trc.iter().all(|t| t.approx_eq(&Trc::Srgb, 2e-4)) => Correction::None,
|
||||
Some(trc) => Correction::Curves(Box::new(curve_tables(&trc))),
|
||||
None => Correction::Cms(transform(&proxy, &p).ok_or("the profile's tables can't be applied")?),
|
||||
None => Correction::Cms(Box::new(transform(&proxy, &p).ok_or("the profile's tables can't be applied")?)),
|
||||
};
|
||||
let from_srgb = transform(&srgb, &p).ok_or("can't convert sRGB to this profile")?;
|
||||
let to_srgb = transform(&proxy, &srgb).ok_or("can't convert this profile to sRGB")?;
|
||||
@@ -205,12 +188,12 @@ impl DisplayProfile {
|
||||
|
||||
/// Device RGB → PCS (XYZ D50) of the pure primaries, measured through the profile's tables
|
||||
/// (LUT-only profiles have no colorant tags).
|
||||
fn measured_to_xyz_d50(p: &ColorProfile) -> Result<Mat3, String> {
|
||||
fn measured_to_xyz_d50(p: &Profile) -> Result<Mat3, String> {
|
||||
let lin = icc::linear_rec2020_profile();
|
||||
let x = guarded(|| p.create_transform_f32(Layout::Rgb, &lin, Layout::Rgb, opts()).ok()).ok_or("the profile's tables can't be applied")?;
|
||||
let x = Transform::with_options(p, &lin, opts()).map_err(|_| "the profile's tables can't be applied")?;
|
||||
let src = [1.0f32, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
|
||||
let mut dst = [0f32; 9];
|
||||
guarded(|| x.transform(&src, &mut dst).ok()).ok_or("the profile's tables can't be applied")?;
|
||||
x.convert_f32(&src, 3, &mut dst, 3, false).map_err(|_| "the profile's tables can't be applied")?;
|
||||
let c = |i: usize| [dst[i] as f64, dst[i + 3] as f64, dst[i + 6] as f64];
|
||||
// columns: each primary in linear Rec.2020
|
||||
let m = Mat3([c(0), c(1), c(2)]);
|
||||
@@ -247,10 +230,10 @@ fn from_linear(t: &Trc, y: f32) -> f32 {
|
||||
0.5 * (lo + hi)
|
||||
}
|
||||
|
||||
fn apply(x: &Arc<Transform8BitExecutor>, img: &mut Rgba8) -> Result<(), String> {
|
||||
fn apply(x: &Transform, img: &mut Rgba8) -> Result<(), String> {
|
||||
const CHUNK: usize = 1 << 14; // pixels
|
||||
let src = img.data.clone();
|
||||
let run = |s: &[[u8; 4]], d: &mut [[u8; 4]]| guarded(|| x.transform(s.as_flattened(), d.as_flattened_mut()).ok()).is_some();
|
||||
let run = |s: &[[u8; 4]], d: &mut [[u8; 4]]| x.convert_u8(s.as_flattened(), 4, d.as_flattened_mut(), 4, true).is_ok();
|
||||
#[cfg(feature = "parallel")]
|
||||
let ok = {
|
||||
use rayon::prelude::*;
|
||||
@@ -340,9 +323,8 @@ mod tests {
|
||||
fn rejects_bad_profiles() {
|
||||
assert!(DisplayProfile::from_icc(&[]).is_err());
|
||||
assert!(DisplayProfile::from_icc(&[0u8; 300]).is_err());
|
||||
let mut gray = ColorProfile::new_gray_with_gamma(2.2);
|
||||
gray.cicp = None;
|
||||
assert!(DisplayProfile::from_icc(&gray.encode().unwrap()).is_err());
|
||||
let gray = Builtin::GrayGamma22.profile().to_bytes();
|
||||
assert!(DisplayProfile::from_icc(&gray).is_err());
|
||||
// truncated / corrupted real profiles never panic
|
||||
let good = icc::write_named(NamedSpace::DisplayP3);
|
||||
for n in [10, 128, 140, 200, good.len() - 1] {
|
||||
@@ -359,55 +341,50 @@ mod tests {
|
||||
/// Display P3 panel with a gamma 2.2 response): the primaries are measured through the tables
|
||||
/// and every preview goes through the CMS.
|
||||
fn lut_only_p3_gamma22() -> Vec<u8> {
|
||||
use moxcms::{LutMultidimensionalType, LutWarehouse, Matrix3d, ProfileClass, ToneReprCurve, Vector3d};
|
||||
use craft_cms::Curve;
|
||||
use craft_cms::pipeline::Stage;
|
||||
use craft_cms::profile::{Lut, LutKind};
|
||||
let m = crate::space::rgb_to_xyz_d50(&DISPLAY_P3).0;
|
||||
let inv = crate::space::rgb_to_xyz_d50(&DISPLAY_P3).inverse().unwrap().0;
|
||||
let curves = |v: Vec<f32>| vec![ToneReprCurve::Parametric(v.clone()), ToneReprCurve::Parametric(v.clone()), ToneReprCurve::Parametric(v)];
|
||||
let ident = || curves(vec![1.0]);
|
||||
// mAB: A → CLUT → M → matrix → B (no CLUT: M curves linearize, the matrix goes to XYZ)
|
||||
let a2b = LutWarehouse::Multidimensional(LutMultidimensionalType {
|
||||
num_input_channels: 3,
|
||||
num_output_channels: 3,
|
||||
grid_points: [0; 16],
|
||||
clut: None,
|
||||
a_curves: vec![],
|
||||
m_curves: curves(vec![2.2]),
|
||||
matrix: Matrix3d { v: m.map(|r| r.map(|v| v * 32768.0 / 65535.0)) },
|
||||
b_curves: ident(),
|
||||
bias: Vector3d::default(),
|
||||
});
|
||||
let b2a = LutWarehouse::Multidimensional(LutMultidimensionalType {
|
||||
num_input_channels: 3,
|
||||
num_output_channels: 3,
|
||||
grid_points: [0; 16],
|
||||
clut: None,
|
||||
a_curves: vec![],
|
||||
m_curves: curves(vec![1.0 / 2.2]),
|
||||
matrix: Matrix3d { v: inv.map(|r| r.map(|v| v * 65535.0 / 32768.0)) },
|
||||
b_curves: ident(),
|
||||
bias: Vector3d::default(),
|
||||
});
|
||||
let mut p = ColorProfile::new_srgb();
|
||||
p.cicp = None;
|
||||
p.profile_class = ProfileClass::DisplayDevice;
|
||||
p.red_colorant = Xyzd::default();
|
||||
p.green_colorant = Xyzd::default();
|
||||
p.blue_colorant = Xyzd::default();
|
||||
p.red_trc = None;
|
||||
p.green_trc = None;
|
||||
p.blue_trc = None;
|
||||
p.lut_a_to_b_colorimetric = Some(a2b.clone());
|
||||
p.lut_a_to_b_perceptual = Some(a2b);
|
||||
p.lut_b_to_a_colorimetric = Some(b2a.clone());
|
||||
p.lut_b_to_a_perceptual = Some(b2a);
|
||||
p.encode().unwrap()
|
||||
let flat = |m: [[f64; 3]; 3], scale: f64| -> Vec<f64> { m.iter().flatten().map(|&v| v * scale).collect() };
|
||||
let curves = |g: f64| vec![Curve::Gamma(g), Curve::Gamma(g), Curve::Gamma(g)];
|
||||
let ident = || vec![Curve::Identity, Curve::Identity, Curve::Identity];
|
||||
let a2b = Lut {
|
||||
kind: LutKind::Ab,
|
||||
inputs: 3,
|
||||
outputs: 3,
|
||||
stages: vec![
|
||||
Stage::Curves(curves(2.2)),
|
||||
Stage::Matrix { rows: 3, cols: 3, m: flat(m, 32768.0 / 65535.0), offset: vec![0.0; 3] },
|
||||
Stage::Curves(ident()),
|
||||
],
|
||||
};
|
||||
let b2a = Lut {
|
||||
kind: LutKind::Ab,
|
||||
inputs: 3,
|
||||
outputs: 3,
|
||||
stages: vec![
|
||||
Stage::Curves(ident()),
|
||||
Stage::Matrix { rows: 3, cols: 3, m: flat(inv, 65535.0 / 32768.0), offset: vec![0.0; 3] },
|
||||
Stage::Curves(curves(1.0 / 2.2)),
|
||||
],
|
||||
};
|
||||
let mut p = Builtin::Srgb.profile().clone();
|
||||
p.class = craft_cms::ProfileClass::Display;
|
||||
p.matrix = None;
|
||||
p.trc = None;
|
||||
p.a2b[0] = Some(a2b.clone());
|
||||
p.a2b[1] = Some(a2b);
|
||||
p.b2a[0] = Some(b2a.clone());
|
||||
p.b2a[1] = Some(b2a);
|
||||
p.with_encoded_bytes().to_bytes().to_vec()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lut_profile_goes_through_the_cms() {
|
||||
let bytes = lut_only_p3_gamma22();
|
||||
let parsed = ColorProfile::new_from_slice(&bytes).unwrap();
|
||||
assert!(!parsed.is_matrix_shaper() && parsed.lut_b_to_a_colorimetric.is_some(), "a LUT-only profile");
|
||||
let parsed = Profile::parse(&bytes).unwrap();
|
||||
assert!(!parsed.is_matrix_shaper() && parsed.b2a[1].is_some(), "a LUT-only profile");
|
||||
let d = DisplayProfile::from_icc(&bytes).unwrap();
|
||||
assert_eq!(d.kind, DisplayKind::Lut);
|
||||
assert!(d.needs_correction());
|
||||
|
||||
+152
-228
@@ -1,4 +1,4 @@
|
||||
//! Encoders: JPEG (`jpeg-encoder`), PNG (`png`), TIFF (`tiff`), flat PSD (our own), lossless WebP (`image-webp`) and,
|
||||
//! Encoders: JPEG (`craft-jpeg`), PNG (`png`), TIFF (`tiff`), flat PSD (our own), lossless WebP (`image-webp`) and,
|
||||
//! on native targets with the `avif` feature, AVIF (`ravif`/`rav1e`); JPEG XL is in `jxl_encode`.
|
||||
//! Metadata (ICC/EXIF/XMP) is embedded where the format allows.
|
||||
|
||||
@@ -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}")));
|
||||
}
|
||||
@@ -128,92 +132,68 @@ pub fn encode_jpeg(img: &EncodeImage, quality: u8, subsampling: ChromaSubsamplin
|
||||
if img.width > u16::MAX as u32 || img.height > u16::MAX as u32 {
|
||||
return Err(Error::Encode("JPEG dimensions are limited to 65535".into()));
|
||||
}
|
||||
let n = img.width as usize * img.height as usize;
|
||||
let (data, ct): (std::borrow::Cow<[u8]>, jpeg_encoder::ColorType) = match img.channels {
|
||||
1 => (data[..n].into(), jpeg_encoder::ColorType::Luma),
|
||||
2 => (data.as_chunks::<2>().0.iter().take(n).map(|c| c[0]).collect::<Vec<_>>().into(), jpeg_encoder::ColorType::Luma),
|
||||
3 => (data[..n * 3].into(), jpeg_encoder::ColorType::Rgb),
|
||||
_ => (data[..n * 4].into(), jpeg_encoder::ColorType::Rgba),
|
||||
let pixels = img.width as usize * img.height as usize;
|
||||
let (data, channels): (std::borrow::Cow<'_, [u8]>, usize) = match img.channels {
|
||||
1 => (data[..pixels].into(), 1),
|
||||
2 => (data.as_chunks::<2>().0.iter().take(pixels).map(|c| c[0]).collect::<Vec<_>>().into(), 1),
|
||||
3 => (data[..pixels * 3].into(), 3),
|
||||
_ => (data[..pixels * 4].into(), 4),
|
||||
};
|
||||
if subsampling != ChromaSubsampling::S422 {
|
||||
// Parallel encoder (restart-interval bands on all cores).
|
||||
let segs = jpeg_app_segments(meta)?;
|
||||
let ch = if img.channels == 2 { 1 } else { img.channels as usize };
|
||||
// `data` is already gray for 2-channel input (see above)
|
||||
return Ok(crate::jpeg_par::encode(&data, img.width as usize, img.height as usize, ch, quality, subsampling, &segs));
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
let mut enc = jpeg_encoder::Encoder::new(&mut out, quality.clamp(1, 100));
|
||||
enc.set_sampling_factor(match subsampling {
|
||||
ChromaSubsampling::S444 => jpeg_encoder::SamplingFactor::R_4_4_4,
|
||||
ChromaSubsampling::S422 => jpeg_encoder::SamplingFactor::R_4_2_2,
|
||||
ChromaSubsampling::S420 => jpeg_encoder::SamplingFactor::R_4_2_0,
|
||||
});
|
||||
// Standard tables: jpeg-encoder's optimized-table mode writes non-interleaved scans, which some
|
||||
// decoders (incl. zune-jpeg 0.5) mishandle. Interleaved baseline is universally supported.
|
||||
let e = |e: jpeg_encoder::EncodingError| Error::Encode(e.to_string());
|
||||
if let Some(ppi) = meta.ppi.filter(|p| *p > 0) {
|
||||
enc.set_density(jpeg_encoder::PixelDensity::dpi(ppi));
|
||||
}
|
||||
if let Some(exif) = meta.exif {
|
||||
if exif.len() > 65_527 {
|
||||
return Err(Error::Encode("EXIF larger than one APP1 segment (64 KiB)".into()));
|
||||
}
|
||||
enc.add_exif_metadata(exif).map_err(e)?;
|
||||
}
|
||||
if let Some(xmp) = meta.xmp {
|
||||
let mut seg = XMP_NS.to_vec();
|
||||
seg.extend_from_slice(xmp.as_bytes());
|
||||
if seg.len() > 65_533 {
|
||||
return Err(Error::Encode("XMP larger than one APP1 segment (extended XMP not supported)".into()));
|
||||
}
|
||||
enc.add_app_segment(1, seg).map_err(e)?;
|
||||
}
|
||||
if let Some(icc) = meta.icc {
|
||||
enc.add_icc_profile(icc).map_err(e)?;
|
||||
}
|
||||
enc.encode(&data, img.width as u16, img.height as u16, ct).map_err(e)?;
|
||||
Ok(out)
|
||||
let subsampling = match subsampling {
|
||||
ChromaSubsampling::S444 => craft_jpeg::ChromaSubsampling::S444,
|
||||
ChromaSubsampling::S422 => craft_jpeg::ChromaSubsampling::S422,
|
||||
ChromaSubsampling::S420 => craft_jpeg::ChromaSubsampling::S420,
|
||||
};
|
||||
craft_jpeg::encode_baseline(&data, img.width as usize, img.height as usize, channels, quality, subsampling, &jpeg_app_segments(meta)?)
|
||||
.map_err(|e| Error::Encode(e.to_string()))
|
||||
}
|
||||
|
||||
/// Encode PNG (8- or 16-bit; float input is rejected — quantize first).
|
||||
pub fn encode_png(img: &EncodeImage, meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||||
img.validate()?;
|
||||
let n = img.width as usize * img.height as usize * img.channels as usize;
|
||||
let (bytes, depth): (Vec<u8>, png::BitDepth) = match img.samples {
|
||||
Samples::U8(s) => (s[..n].to_vec(), png::BitDepth::Eight),
|
||||
Samples::U16(s) => (s[..n].iter().flat_map(|v| v.to_be_bytes()).collect(), png::BitDepth::Sixteen),
|
||||
let layout = match img.channels {
|
||||
1 => craft_image_codecs::ChannelLayout::Gray,
|
||||
2 => craft_image_codecs::ChannelLayout::GrayA,
|
||||
3 => craft_image_codecs::ChannelLayout::Rgb,
|
||||
_ => craft_image_codecs::ChannelLayout::Rgba,
|
||||
};
|
||||
let mut ic_img = match img.samples {
|
||||
Samples::U8(s) => craft_image_codecs::Image::from_u8(img.width, img.height, layout, s[..n].to_vec()),
|
||||
Samples::U16(s) => craft_image_codecs::Image::from_u16(img.width, img.height, layout, &s[..n]),
|
||||
Samples::F32(_) => return Err(Error::Encode("PNG needs 8- or 16-bit samples".into())),
|
||||
};
|
||||
let mut info = png::Info::with_size(img.width, img.height);
|
||||
info.color_type = match img.channels {
|
||||
1 => png::ColorType::Grayscale,
|
||||
2 => png::ColorType::GrayscaleAlpha,
|
||||
3 => png::ColorType::Rgb,
|
||||
_ => png::ColorType::Rgba,
|
||||
};
|
||||
info.bit_depth = depth;
|
||||
info.icc_profile = meta.icc.map(|b| b.to_vec().into());
|
||||
info.exif_metadata = meta.exif.map(|b| b.to_vec().into());
|
||||
info.pixel_dims = meta.ppi.filter(|p| *p > 0).map(|p| {
|
||||
let ppm = (p as f64 / 0.0254).round() as u32;
|
||||
png::PixelDimensions { xppu: ppm, yppu: ppm, unit: png::Unit::Meter }
|
||||
});
|
||||
let mut out = Vec::new();
|
||||
let e = |e: png::EncodingError| Error::Encode(e.to_string());
|
||||
{
|
||||
let mut enc = png::Encoder::with_info(&mut out, info).map_err(e)?;
|
||||
enc.set_compression(png::Compression::Fast);
|
||||
if let Some(xmp) = meta.xmp {
|
||||
enc.add_itxt_chunk("XML:com.adobe.xmp".into(), xmp.into()).map_err(e)?;
|
||||
}
|
||||
let mut w = enc.write_header().map_err(e)?;
|
||||
w.write_image_data(&bytes).map_err(e)?;
|
||||
w.finish().map_err(e)?;
|
||||
}
|
||||
.map_err(|e| Error::Encode(e.to_string()))?;
|
||||
ic_img.icc = meta.icc.map(|b| b.to_vec());
|
||||
ic_img.meta.exif = meta.exif.map(|b| b.to_vec());
|
||||
ic_img.meta.xmp = meta.xmp.map(|x| x.to_string());
|
||||
ic_img.meta.dpi = meta.ppi.filter(|p| *p > 0).map(|p| (p as f32, p as f32));
|
||||
let ic_opts = craft_image_codecs::EncodeOptions { png_compression: craft_image_codecs::PngCompression::Fast, ..Default::default() };
|
||||
let mut out = craft_image_codecs::encode(&ic_img, craft_image_codecs::Format::Png, &ic_opts).map_err(|e| Error::Encode(e.to_string()))?;
|
||||
if let Some(exif) = meta.exif {
|
||||
replace_png_exif_chunk(&mut out, exif);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Replace the `eXIf` chunk in an encoded PNG so caller-supplied EXIF (including non-1
|
||||
/// orientation tags) is preserved verbatim.
|
||||
fn replace_png_exif_chunk(png: &mut Vec<u8>, exif: &[u8]) {
|
||||
let mut p = 8usize;
|
||||
while let Some(len_bytes) = png.get(p..p + 4) {
|
||||
let len = u32::from_be_bytes([len_bytes[0], len_bytes[1], len_bytes[2], len_bytes[3]]) as usize;
|
||||
let end = p.saturating_add(12).saturating_add(len);
|
||||
if end > png.len() {
|
||||
break;
|
||||
}
|
||||
if png.get(p + 4..p + 8) == Some(b"eXIf") {
|
||||
png.splice(p..end, png_chunk(*b"eXIf", exif));
|
||||
return;
|
||||
}
|
||||
p = end;
|
||||
}
|
||||
}
|
||||
|
||||
/// TIFF compression.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum TiffCompression {
|
||||
@@ -225,108 +205,33 @@ 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;
|
||||
img.validate()?;
|
||||
let e = |e: tiff::TiffError| Error::Encode(e.to_string());
|
||||
let mut cur = std::io::Cursor::new(Vec::new());
|
||||
let comp = match compression {
|
||||
TiffCompression::None => Compression::Uncompressed,
|
||||
TiffCompression::Lzw => Compression::Lzw,
|
||||
TiffCompression::Deflate => Compression::Deflate(DeflateLevel::Fast),
|
||||
TiffCompression::PackBits => Compression::Packbits,
|
||||
let n = img.width as usize * img.height as usize * img.channels as usize;
|
||||
let layout = match img.channels {
|
||||
1 => craft_image_codecs::ChannelLayout::Gray,
|
||||
2 => craft_image_codecs::ChannelLayout::GrayA,
|
||||
3 => craft_image_codecs::ChannelLayout::Rgb,
|
||||
_ => craft_image_codecs::ChannelLayout::Rgba,
|
||||
};
|
||||
let float = matches!(img.samples, Samples::F32(_));
|
||||
let predictor =
|
||||
if !float && matches!(compression, TiffCompression::Lzw | TiffCompression::Deflate) { Predictor::Horizontal } else { Predictor::None };
|
||||
let mut enc = TiffEncoder::new(&mut cur).map_err(e)?.with_compression(comp).with_predictor(predictor);
|
||||
let (w, h) = (img.width, img.height);
|
||||
let pixels = w as usize * h as usize;
|
||||
match img.samples {
|
||||
Samples::U8(samples) => write_tiff_u8(&mut enc, samples, pixels, img.channels as usize, img, meta)?,
|
||||
Samples::U16(samples) => write_tiff_u16(&mut enc, samples, pixels, img.channels as usize, img, meta)?,
|
||||
Samples::F32(samples) => write_tiff_f32(&mut enc, samples, pixels, img.channels as usize, img, meta)?,
|
||||
let mut ic_img = match img.samples {
|
||||
Samples::U8(s) => craft_image_codecs::Image::from_u8(img.width, img.height, layout, s[..n].to_vec()),
|
||||
Samples::U16(s) => craft_image_codecs::Image::from_u16(img.width, img.height, layout, &s[..n]),
|
||||
Samples::F32(s) => craft_image_codecs::Image::from_f32(img.width, img.height, layout, &s[..n]),
|
||||
}
|
||||
Ok(cur.into_inner())
|
||||
}
|
||||
|
||||
macro_rules! write_tiff_image {
|
||||
($encoder:expr, $image:expr, $meta:expr, $color:ty, $data:expr, $alpha:expr) => {{
|
||||
let mut image = $encoder.new_image::<$color>($image.width, $image.height).map_err(|e| Error::Encode(e.to_string()))?;
|
||||
if let Some(ppi) = $meta.ppi.filter(|ppi| *ppi > 0) {
|
||||
image.resolution(tiff::tags::ResolutionUnit::Inch, tiff::encoder::Rational { n: ppi as u32, d: 1 });
|
||||
}
|
||||
if let Some(icc) = $meta.icc {
|
||||
image.encoder().write_tag(tiff::tags::Tag::IccProfile, icc).map_err(|e| Error::Encode(e.to_string()))?;
|
||||
}
|
||||
if let Some(xmp) = $meta.xmp {
|
||||
image.encoder().write_tag(tiff::tags::Tag::from_u16_exhaustive(700), xmp.as_bytes()).map_err(|e| Error::Encode(e.to_string()))?;
|
||||
}
|
||||
if $alpha {
|
||||
image.encoder().write_tag(tiff::tags::Tag::ExtraSamples, &[2u16][..]).map_err(|e| Error::Encode(e.to_string()))?;
|
||||
}
|
||||
image.write_data($data).map_err(|e| Error::Encode(e.to_string()))?;
|
||||
}};
|
||||
}
|
||||
|
||||
fn expanded_gray_alpha<T: Copy>(samples: &[T], pixels: usize) -> Vec<T> {
|
||||
samples.as_chunks::<2>().0.iter().take(pixels).flat_map(|channel| [channel[0], channel[0], channel[0], channel[1]]).collect()
|
||||
}
|
||||
|
||||
fn write_tiff_u8<W: std::io::Write + std::io::Seek>(
|
||||
encoder: &mut tiff::encoder::TiffEncoder<W>,
|
||||
samples: &[u8],
|
||||
pixels: usize,
|
||||
channels: usize,
|
||||
image: &EncodeImage,
|
||||
meta: &EncodeMeta,
|
||||
) -> Result<()> {
|
||||
use tiff::encoder::colortype;
|
||||
match channels {
|
||||
1 => write_tiff_image!(encoder, image, meta, colortype::Gray8, &samples[..pixels], false),
|
||||
2 => write_tiff_image!(encoder, image, meta, colortype::RGBA8, &expanded_gray_alpha(samples, pixels), true),
|
||||
3 => write_tiff_image!(encoder, image, meta, colortype::RGB8, &samples[..pixels * 3], false),
|
||||
_ => write_tiff_image!(encoder, image, meta, colortype::RGBA8, &samples[..pixels * 4], true),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_tiff_u16<W: std::io::Write + std::io::Seek>(
|
||||
encoder: &mut tiff::encoder::TiffEncoder<W>,
|
||||
samples: &[u16],
|
||||
pixels: usize,
|
||||
channels: usize,
|
||||
image: &EncodeImage,
|
||||
meta: &EncodeMeta,
|
||||
) -> Result<()> {
|
||||
use tiff::encoder::colortype;
|
||||
match channels {
|
||||
1 => write_tiff_image!(encoder, image, meta, colortype::Gray16, &samples[..pixels], false),
|
||||
2 => write_tiff_image!(encoder, image, meta, colortype::RGBA16, &expanded_gray_alpha(samples, pixels), true),
|
||||
3 => write_tiff_image!(encoder, image, meta, colortype::RGB16, &samples[..pixels * 3], false),
|
||||
_ => write_tiff_image!(encoder, image, meta, colortype::RGBA16, &samples[..pixels * 4], true),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_tiff_f32<W: std::io::Write + std::io::Seek>(
|
||||
encoder: &mut tiff::encoder::TiffEncoder<W>,
|
||||
samples: &[f32],
|
||||
pixels: usize,
|
||||
channels: usize,
|
||||
image: &EncodeImage,
|
||||
meta: &EncodeMeta,
|
||||
) -> Result<()> {
|
||||
use tiff::encoder::colortype;
|
||||
match channels {
|
||||
1 => write_tiff_image!(encoder, image, meta, colortype::Gray32Float, &samples[..pixels], false),
|
||||
2 => write_tiff_image!(encoder, image, meta, colortype::RGBA32Float, &expanded_gray_alpha(samples, pixels), true),
|
||||
3 => write_tiff_image!(encoder, image, meta, colortype::RGB32Float, &samples[..pixels * 3], false),
|
||||
_ => write_tiff_image!(encoder, image, meta, colortype::RGBA32Float, &samples[..pixels * 4], true),
|
||||
}
|
||||
Ok(())
|
||||
.map_err(|e| Error::Encode(e.to_string()))?;
|
||||
ic_img.icc = meta.icc.map(|b| b.to_vec());
|
||||
ic_img.meta.xmp = meta.xmp.map(|x| x.to_string());
|
||||
ic_img.meta.dpi = meta.ppi.filter(|p| *p > 0).map(|p| (p as f32, p as f32));
|
||||
let tiff_compression = match compression {
|
||||
TiffCompression::None => craft_image_codecs::TiffCompression::None,
|
||||
TiffCompression::Lzw => craft_image_codecs::TiffCompression::Lzw,
|
||||
TiffCompression::Deflate => craft_image_codecs::TiffCompression::Deflate,
|
||||
TiffCompression::PackBits => craft_image_codecs::TiffCompression::PackBits,
|
||||
};
|
||||
let ic_opts = craft_image_codecs::EncodeOptions { tiff_compression, ..Default::default() };
|
||||
craft_image_codecs::encode(&ic_img, craft_image_codecs::Format::Tiff, &ic_opts).map_err(|e| Error::Encode(e.to_string()))
|
||||
}
|
||||
|
||||
/// Encode a flat PSD (8/16-bit gray or RGB, no layers; alpha dropped): the hand-off an image
|
||||
@@ -342,27 +247,49 @@ pub fn encode_webp_lossless(img: &EncodeImage, meta: &EncodeMeta) -> Result<Vec<
|
||||
return Err(Error::Encode("WebP needs 8-bit samples".into()));
|
||||
};
|
||||
let n = img.width as usize * img.height as usize * img.channels as usize;
|
||||
let ct = match img.channels {
|
||||
1 => image_webp::ColorType::L8,
|
||||
2 => image_webp::ColorType::La8,
|
||||
3 => image_webp::ColorType::Rgb8,
|
||||
_ => image_webp::ColorType::Rgba8,
|
||||
let layout = match img.channels {
|
||||
1 => craft_image_codecs::ChannelLayout::Gray,
|
||||
2 => craft_image_codecs::ChannelLayout::GrayA,
|
||||
3 => craft_image_codecs::ChannelLayout::Rgb,
|
||||
_ => craft_image_codecs::ChannelLayout::Rgba,
|
||||
};
|
||||
let mut out = Vec::new();
|
||||
let mut enc = image_webp::WebPEncoder::new(&mut out);
|
||||
if let Some(icc) = meta.icc {
|
||||
enc.set_icc_profile(icc.to_vec());
|
||||
}
|
||||
let mut ic_img = craft_image_codecs::Image::from_u8(img.width, img.height, layout, s[..n].to_vec()).map_err(|e| Error::Encode(e.to_string()))?;
|
||||
ic_img.icc = meta.icc.map(|b| b.to_vec());
|
||||
ic_img.meta.exif = meta.exif.map(|b| b.to_vec());
|
||||
ic_img.meta.xmp = meta.xmp.map(|x| x.to_string());
|
||||
let ic_opts = craft_image_codecs::EncodeOptions { webp_lossless: true, ..Default::default() };
|
||||
let mut out = craft_image_codecs::encode(&ic_img, craft_image_codecs::Format::WebP, &ic_opts).map_err(|e| Error::Encode(e.to_string()))?;
|
||||
if let Some(exif) = meta.exif {
|
||||
enc.set_exif_metadata(exif.to_vec());
|
||||
replace_webp_exif_chunk(&mut out, exif);
|
||||
}
|
||||
if let Some(xmp) = meta.xmp {
|
||||
enc.set_xmp_metadata(xmp.as_bytes().to_vec());
|
||||
}
|
||||
enc.encode(&s[..n], img.width, img.height, ct).map_err(|e| Error::Encode(e.to_string()))?;
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Replace the `EXIF` RIFF chunk payload in a WebP file so caller-supplied EXIF (including
|
||||
/// non-1 orientation tags) is preserved verbatim.
|
||||
fn replace_webp_exif_chunk(webp: &mut Vec<u8>, exif: &[u8]) {
|
||||
let mut p = 12usize;
|
||||
while let Some(h) = webp.get(p..p.saturating_add(8)) {
|
||||
let size = u32::from_le_bytes([h[4], h[5], h[6], h[7]]) as usize;
|
||||
let end = p.saturating_add(8).saturating_add(size);
|
||||
let padded_end = end.saturating_add(size & 1);
|
||||
if &h[0..4] == b"EXIF" && padded_end <= webp.len() {
|
||||
let mut replacement = Vec::with_capacity(8 + exif.len() + (exif.len() & 1));
|
||||
replacement.extend_from_slice(b"EXIF");
|
||||
replacement.extend_from_slice(&(exif.len() as u32).to_le_bytes());
|
||||
replacement.extend_from_slice(exif);
|
||||
if exif.len() & 1 == 1 {
|
||||
replacement.push(0);
|
||||
}
|
||||
webp.splice(p..padded_end, replacement);
|
||||
let riff_size = (webp.len().saturating_sub(8)) as u32;
|
||||
webp[4..8].copy_from_slice(&riff_size.to_le_bytes());
|
||||
return;
|
||||
}
|
||||
p = padded_end;
|
||||
}
|
||||
}
|
||||
|
||||
/// Encode AVIF (sRGB, 4:4:4, 10-bit AV1) with `ravif` (rav1e). 8-bit samples are widened by the
|
||||
/// encoder; 16-bit samples keep 10 bits of precision (BT.601 full-range YCbCr computed here).
|
||||
/// `quality` 1..=100, `speed` 1 (slow) ..= 10. EXIF is embedded; ICC is not supported by the muxer
|
||||
@@ -594,48 +521,38 @@ pub fn encode_avif_hdr(width: u32, height: u32, rgb: &[f32], quality: u8, speed:
|
||||
pub fn encode_png_pq(width: u32, height: u32, rgb: &[f32], meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||||
let (w, h) = (width as usize, height as usize);
|
||||
let n = w.checked_mul(h).ok_or_else(|| Error::Encode("image too large".into()))?;
|
||||
if n == 0 || n.checked_mul(3).is_none_or(|need| rgb.len() < need) {
|
||||
let samples = n.checked_mul(3).ok_or_else(|| Error::Encode("image too large".into()))?;
|
||||
if n == 0 || rgb.len() < samples {
|
||||
return Err(Error::Encode("HDR PNG: sample buffer too short".into()));
|
||||
}
|
||||
const K: [f32; 3] = [0.2627, 0.6780, 0.0593]; // BT.2020 luminance
|
||||
let mut max_nits = 0f32;
|
||||
let mut sum_nits = 0f64;
|
||||
let mut bytes = Vec::with_capacity(n * 6);
|
||||
for c in rgb.as_chunks::<3>().0.iter().take(n) {
|
||||
let nits = c.map(|v| (if v.is_finite() { v.max(0.0) } else { 0.0 }) * HDR_REFERENCE_WHITE_NITS);
|
||||
max_nits = max_nits.max(nits[0].max(nits[1]).max(nits[2]));
|
||||
sum_nits += (K[0] * nits[0] + K[1] * nits[1] + K[2] * nits[2]) as f64;
|
||||
for v in nits {
|
||||
bytes.extend_from_slice(&((pq_encode(v) * 65535.0).round().clamp(0.0, 65535.0) as u16).to_be_bytes());
|
||||
let mut image = craft_image_codecs::Image::from_f32(
|
||||
width,
|
||||
height,
|
||||
craft_image_codecs::ChannelLayout::Rgb,
|
||||
rgb.get(..samples).ok_or_else(|| Error::Encode("HDR PNG: sample buffer too short".into()))?,
|
||||
)
|
||||
.map_err(|e| Error::Encode(e.to_string()))?;
|
||||
image.meta.exif = meta.exif.map(ToOwned::to_owned);
|
||||
image.meta.xmp = meta.xmp.map(ToOwned::to_owned);
|
||||
image.meta.dpi = meta.ppi.filter(|&ppi| ppi > 0).map(|ppi| (f32::from(ppi), f32::from(ppi)));
|
||||
let options = craft_image_codecs::EncodeOptions { png_compression: craft_image_codecs::PngCompression::Fast, ..Default::default() };
|
||||
craft_image_codecs::encode_png_pq(&image, &options).map_err(|e| Error::Encode(e.to_string()))
|
||||
}
|
||||
|
||||
fn png_chunk(kind: [u8; 4], data: &[u8]) -> Vec<u8> {
|
||||
let mut out = Vec::with_capacity(12 + data.len());
|
||||
out.extend_from_slice(&(data.len() as u32).to_be_bytes());
|
||||
out.extend_from_slice(&kind);
|
||||
out.extend_from_slice(data);
|
||||
let mut crc = 0xFFFF_FFFFu32;
|
||||
for &b in &out[4..] {
|
||||
crc ^= u32::from(b);
|
||||
for _ in 0..8 {
|
||||
crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 };
|
||||
}
|
||||
}
|
||||
let cll = (max_nits.ceil().clamp(1.0, 10_000.0) as u32, ((sum_nits / n as f64).ceil().clamp(1.0, 10_000.0)) as u32);
|
||||
let mut info = png::Info::with_size(width, height);
|
||||
info.color_type = png::ColorType::Rgb;
|
||||
info.bit_depth = png::BitDepth::Sixteen;
|
||||
info.exif_metadata = meta.exif.map(|b| b.to_vec().into());
|
||||
info.pixel_dims = meta.ppi.filter(|p| *p > 0).map(|p| {
|
||||
let ppm = (p as f64 / 0.0254).round() as u32;
|
||||
png::PixelDimensions { xppu: ppm, yppu: ppm, unit: png::Unit::Meter }
|
||||
});
|
||||
let mut out = Vec::new();
|
||||
let e = |e: png::EncodingError| Error::Encode(e.to_string());
|
||||
{
|
||||
let mut enc = png::Encoder::with_info(&mut out, info).map_err(e)?;
|
||||
enc.set_compression(png::Compression::Fast);
|
||||
if let Some(xmp) = meta.xmp {
|
||||
enc.add_itxt_chunk("XML:com.adobe.xmp".into(), xmp.into()).map_err(e)?;
|
||||
}
|
||||
let mut wr = enc.write_header().map_err(e)?;
|
||||
// BT.2020 primaries (9), PQ (16), RGB (0), full range (1)
|
||||
wr.write_chunk(png::chunk::ChunkType(*b"cICP"), &[9, 16, 0, 1]).map_err(e)?;
|
||||
// MaxCLL and MaxFALL in units of 0.0001 cd/m²
|
||||
let clli = [(cll.0 * 10_000).to_be_bytes(), (cll.1 * 10_000).to_be_bytes()].concat();
|
||||
wr.write_chunk(png::chunk::ChunkType(*b"cLLI"), &clli).map_err(e)?;
|
||||
wr.write_image_data(&bytes).map_err(e)?;
|
||||
wr.finish().map_err(e)?;
|
||||
}
|
||||
Ok(out)
|
||||
out.extend_from_slice(&(!crc).to_be_bytes());
|
||||
out
|
||||
}
|
||||
|
||||
/// 16-bit RGB(A) → 10-bit BT.601 full-range YCbCr planes (the matrix `ravif` uses for 8-bit input).
|
||||
@@ -671,6 +588,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
|
||||
|
||||
+95
-637
@@ -1,546 +1,20 @@
|
||||
//! 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}")
|
||||
}
|
||||
use crate::{ChromaSubsampling, Error, Format, Result, jpeg};
|
||||
|
||||
/// 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;
|
||||
@@ -576,31 +50,51 @@ pub fn encode_jpeg(base: &EncodeImage, gain: &GainImage, m: &GainMapMeta, qualit
|
||||
return Err(Error::Encode("gain map JPEG needs an 8-bit base image".into()));
|
||||
};
|
||||
let (w, h) = (base.width as usize, base.height as usize);
|
||||
if !matches!(base.channels, 3 | 4) || data.len() < w * h * base.channels as usize {
|
||||
if !matches!(base.channels, 3 | 4) {
|
||||
return Err(Error::Encode("gain map JPEG needs an RGB base image".into()));
|
||||
}
|
||||
let Some(base_bytes) = w.checked_mul(h).and_then(|pixels| pixels.checked_mul(base.channels as usize)) else {
|
||||
return Err(Error::Encode("gain map JPEG needs an RGB base image".into()));
|
||||
};
|
||||
if data.len() < base_bytes {
|
||||
return Err(Error::Encode("gain map JPEG needs an RGB base image".into()));
|
||||
}
|
||||
if w == 0 || h == 0 || w > u16::MAX as usize || h > u16::MAX as usize {
|
||||
return Err(Error::Encode("JPEG dimensions are limited to 65535".into()));
|
||||
}
|
||||
if !matches!(gain.channels, 1 | 3) || gain.width == 0 || gain.height == 0 || gain.data.len() < gain.width * gain.height * gain.channels {
|
||||
if !matches!(gain.channels, 1 | 3) || gain.width == 0 || gain.height == 0 {
|
||||
return Err(Error::Encode("gain map image is malformed".into()));
|
||||
}
|
||||
// Check the JPEG frame-header limit before multiplying dimensions, then validate the source
|
||||
// buffer with checked arithmetic on both 32- and 64-bit targets.
|
||||
if gain.width > u16::MAX as usize || gain.height > u16::MAX as usize {
|
||||
return Err(Error::Encode("JPEG dimensions are limited to 65535".into()));
|
||||
}
|
||||
let Some(gain_bytes) = gain.width.checked_mul(gain.height).and_then(|pixels| pixels.checked_mul(gain.channels)) else {
|
||||
return Err(Error::Encode("gain map image is malformed".into()));
|
||||
};
|
||||
if gain.data.len() < gain_bytes {
|
||||
return Err(Error::Encode("gain map image is malformed".into()));
|
||||
}
|
||||
if !m.is_valid() || m.multichannel != (gain.channels == 3) {
|
||||
return Err(Error::Encode("gain map metadata doesn't match the map".into()));
|
||||
}
|
||||
// jpeg_par writes 4:4:4 or 4:2:0
|
||||
let sub = if sub == ChromaSubsampling::S422 { ChromaSubsampling::S420 } else { sub };
|
||||
|
||||
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 gain_jpeg =
|
||||
craft_jpeg::encode_baseline(&gain.data, gain.width, gain.height, gain.channels, quality, craft_jpeg::ChromaSubsampling::S444, &gain_segs)
|
||||
.map_err(|e| Error::Encode(e.to_string()))?;
|
||||
|
||||
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);
|
||||
let sub = match sub {
|
||||
ChromaSubsampling::S444 => craft_jpeg::ChromaSubsampling::S444,
|
||||
ChromaSubsampling::S422 => craft_jpeg::ChromaSubsampling::S420,
|
||||
ChromaSubsampling::S420 => craft_jpeg::ChromaSubsampling::S420,
|
||||
};
|
||||
let mut primary =
|
||||
craft_jpeg::encode_baseline(data, w, h, base.channels as usize, quality, sub, &segs).map_err(|e| Error::Encode(e.to_string()))?;
|
||||
|
||||
// patch the MPF index: sizes, and the gain map's offset from the MPF endian field
|
||||
let markers = jpeg::parse_markers(&primary).ok_or_else(|| Error::Encode("gain map JPEG: primary image unreadable".into()))?;
|
||||
@@ -668,77 +162,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,27 +185,62 @@ 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")));
|
||||
}
|
||||
|
||||
fn encode_fixture(base: &EncodeImage<'_>, gain: &GainImage, meta: &GainMapMeta, sub: ChromaSubsampling) -> Result<Vec<u8>> {
|
||||
encode_jpeg(base, gain, meta, 90, sub, &EncodeMeta::default())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gain_map_encoder_rejects_malformed_buffers_and_metadata() {
|
||||
let base = [0u8; 3];
|
||||
let rgb = EncodeImage::new(1, 1, 3, Samples::U8(&base));
|
||||
let gain = GainImage { width: 1, height: 1, channels: 1, data: vec![128] };
|
||||
|
||||
let short_base = EncodeImage::new(1, 1, 4, Samples::U8(&base));
|
||||
assert!(
|
||||
matches!(encode_fixture(&short_base, &gain, &GainMapMeta::default(), ChromaSubsampling::S444), Err(Error::Encode(m)) if m.contains("RGB base"))
|
||||
);
|
||||
|
||||
let short_gain = GainImage { data: Vec::new(), ..gain.clone() };
|
||||
assert!(
|
||||
matches!(encode_fixture(&rgb, &short_gain, &GainMapMeta::default(), ChromaSubsampling::S444), Err(Error::Encode(m)) if m.contains("malformed"))
|
||||
);
|
||||
|
||||
let wrong_metadata = GainMapMeta { multichannel: true, ..GainMapMeta::default() };
|
||||
assert!(matches!(encode_fixture(&rgb, &gain, &wrong_metadata, ChromaSubsampling::S444), Err(Error::Encode(m)) if m.contains("metadata")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gain_map_jpeg_writer_preserves_supported_base_layouts_and_subsampling() {
|
||||
let gain = GainImage { width: 1, height: 1, channels: 1, data: vec![128] };
|
||||
let meta = GainMapMeta::default();
|
||||
let rgb = [10u8, 20, 30];
|
||||
let rgba = [10u8, 20, 30, 255];
|
||||
|
||||
for (base, sub) in [
|
||||
(EncodeImage::new(1, 1, 3, Samples::U8(&rgb)), ChromaSubsampling::S444),
|
||||
(EncodeImage::new(1, 1, 3, Samples::U8(&rgb)), ChromaSubsampling::S422),
|
||||
(EncodeImage::new(1, 1, 3, Samples::U8(&rgb)), ChromaSubsampling::S420),
|
||||
(EncodeImage::new(1, 1, 4, Samples::U8(&rgba)), ChromaSubsampling::S420),
|
||||
] {
|
||||
let encoded = encode_fixture(&base, &gain, &meta, sub).unwrap();
|
||||
assert!(is_gain_map_jpeg(&encoded));
|
||||
let decoded = crate::decode(&encoded, crate::DecodeOptions::default()).unwrap();
|
||||
assert_eq!((decoded.width, decoded.height), (1, 1));
|
||||
assert!(read_jpeg(&encoded).is_some());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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, ..Default::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,
|
||||
|
||||
+85
-104
@@ -1,11 +1,9 @@
|
||||
//! ICC profiles: parsing (via `moxcms`), matrix/TRC extraction, CMS fallback for LUT/CMYK profiles,
|
||||
//! ICC profiles: parsing (via `craft-cms`), matrix/TRC extraction, CMS fallback for LUT/CMYK profiles,
|
||||
//! and writing matrix/TRC profiles for export embedding.
|
||||
|
||||
use crate::space::{NamedSpace, Trc, rgb_to_xyz_d50};
|
||||
use craft_cms::{Builtin, ColorSpace, Curve, Intent, Pcs, Profile, Transform, TransformOptions};
|
||||
use lightcraft_color::{D50, Mat3, RgbSpace, bradford};
|
||||
use moxcms::{
|
||||
ColorProfile, DataColorSpace, Layout, LocalizableString, Matrix3d, ProfileText, RenderingIntent, ToneReprCurve, TransformOptions, Xyzd,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Colour model of the profile's device space.
|
||||
@@ -40,24 +38,24 @@ pub struct IccInfo {
|
||||
|
||||
/// Parse an ICC profile. Returns `None` for malformed or unsupported data (never panics).
|
||||
pub fn parse(bytes: &[u8]) -> Option<IccInfo> {
|
||||
let p = std::panic::catch_unwind(|| ColorProfile::new_from_slice(bytes)).ok()?.ok()?;
|
||||
let p = Profile::parse(bytes).ok()?;
|
||||
Some(info_of(&p))
|
||||
}
|
||||
|
||||
fn info_of(p: &ColorProfile) -> IccInfo {
|
||||
let description = p.description.as_ref().and_then(text_of);
|
||||
fn info_of(p: &Profile) -> IccInfo {
|
||||
let desc = p.description.trim_end_matches('\0').trim().to_string();
|
||||
let description = if desc.is_empty() { None } else { Some(desc) };
|
||||
let model = match p.color_space {
|
||||
DataColorSpace::Rgb => IccColorModel::Rgb,
|
||||
DataColorSpace::Gray => IccColorModel::Gray,
|
||||
DataColorSpace::Cmyk => IccColorModel::Cmyk,
|
||||
ColorSpace::Rgb => IccColorModel::Rgb,
|
||||
ColorSpace::Gray => IccColorModel::Gray,
|
||||
ColorSpace::Cmyk => IccColorModel::Cmyk,
|
||||
_ => IccColorModel::Other,
|
||||
};
|
||||
let has_lut = p.lut_a_to_b_perceptual.is_some() || p.lut_a_to_b_colorimetric.is_some();
|
||||
let kind = if model == IccColorModel::Rgb && p.is_matrix_shaper() && p.pcs == DataColorSpace::Xyz {
|
||||
let c = [p.red_colorant, p.green_colorant, p.blue_colorant];
|
||||
let to_xyz_d50 = Mat3([[c[0].x, c[1].x, c[2].x], [c[0].y, c[1].y, c[2].y], [c[0].z, c[1].z, c[2].z]]);
|
||||
let trcs = [&p.red_trc, &p.green_trc, &p.blue_trc];
|
||||
let trc = trcs.map(|t| t.as_ref().map(trc_from_icc).unwrap_or(Trc::Srgb));
|
||||
let has_lut = p.a2b[0].is_some() || p.a2b[1].is_some();
|
||||
let kind = if model == IccColorModel::Rgb && p.is_matrix_shaper() && p.pcs == Pcs::Xyz {
|
||||
let to_xyz_d50 = Mat3(p.matrix.unwrap_or([[0.0; 3]; 3]));
|
||||
let trc =
|
||||
p.trc.as_ref().map(|ts| [trc_from_icc(&ts[0]), trc_from_icc(&ts[1]), trc_from_icc(&ts[2])]).unwrap_or([Trc::Srgb, Trc::Srgb, Trc::Srgb]);
|
||||
if to_xyz_d50.determinant().abs() > 1e-6 && trc.iter().all(valid_trc) { IccKind::MatrixTrc { to_xyz_d50, trc } } else { IccKind::NeedsCms }
|
||||
} else if model == IccColorModel::Gray && p.gray_trc.is_some() && !has_lut {
|
||||
let trc = p.gray_trc.as_ref().map(trc_from_icc).unwrap_or(Trc::Srgb);
|
||||
@@ -80,73 +78,61 @@ fn valid_trc(t: &Trc) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
fn text_of(t: &ProfileText) -> Option<String> {
|
||||
let s = match t {
|
||||
ProfileText::PlainString(s) => s.clone(),
|
||||
ProfileText::Localizable(v) => v.first().map(|l| l.value.clone())?,
|
||||
ProfileText::Description(d) => {
|
||||
if d.unicode_string.is_empty() {
|
||||
d.ascii_string.clone()
|
||||
} else {
|
||||
d.unicode_string.clone()
|
||||
fn trc_from_icc(t: &Curve) -> Trc {
|
||||
match t {
|
||||
Curve::Identity => Trc::Linear,
|
||||
Curve::Gamma(g) => Trc::Gamma(*g as f32),
|
||||
Curve::Parametric { kind, p } => Trc::Parametric(p[..Curve::param_count(*kind)].iter().map(|&v| v as f32).collect()),
|
||||
Curve::Table(v) if v.is_empty() => Trc::Linear,
|
||||
Curve::Table(v) if v.len() == 1 => Trc::Gamma(v[0]),
|
||||
Curve::Table(v) => Trc::Table(v.iter().map(|&x| (x.clamp(0.0, 1.0) * 65535.0).round() as u16).collect()),
|
||||
}
|
||||
}
|
||||
|
||||
fn trc_to_icc(t: &Trc) -> Curve {
|
||||
match t {
|
||||
Trc::Linear => Curve::Identity,
|
||||
Trc::Srgb => Curve::Parametric { kind: 3, p: [2.4, 1.0 / 1.055, 0.055 / 1.055, 1.0 / 12.92, 0.04045, 0.0, 0.0] },
|
||||
Trc::Gamma(g) => Curve::Gamma(*g as f64),
|
||||
Trc::Rec709 => Curve::Parametric { kind: 3, p: [1.0 / 0.45, 1.0 / 1.099, 0.099 / 1.099, 1.0 / 4.5, 0.081, 0.0, 0.0] },
|
||||
Trc::Parametric(p) => {
|
||||
let kind = match p.len() {
|
||||
1 => 0,
|
||||
3 => 1,
|
||||
4 => 2,
|
||||
5 => 3,
|
||||
_ => 4,
|
||||
};
|
||||
let mut arr = [0.0f64; 7];
|
||||
for (dst, &src) in arr.iter_mut().zip(p.iter()) {
|
||||
*dst = src as f64;
|
||||
}
|
||||
Curve::Parametric { kind, p: arr }
|
||||
}
|
||||
};
|
||||
let s = s.trim_end_matches('\0').trim().to_string();
|
||||
if s.is_empty() { None } else { Some(s) }
|
||||
}
|
||||
|
||||
fn trc_from_icc(t: &ToneReprCurve) -> Trc {
|
||||
match t {
|
||||
ToneReprCurve::Lut(v) if v.is_empty() => Trc::Linear,
|
||||
ToneReprCurve::Lut(v) if v.len() == 1 => Trc::Gamma(v[0] as f32 / 256.0),
|
||||
ToneReprCurve::Lut(v) => Trc::Table(v.clone()),
|
||||
ToneReprCurve::Parametric(p) => Trc::Parametric(p.clone()),
|
||||
Trc::Table(v) => Curve::Table(v.iter().map(|&x| x as f32 / 65535.0).collect()),
|
||||
}
|
||||
}
|
||||
|
||||
fn trc_to_icc(t: &Trc) -> ToneReprCurve {
|
||||
match t {
|
||||
Trc::Linear => ToneReprCurve::Parametric(vec![1.0]),
|
||||
Trc::Srgb => ToneReprCurve::Parametric(vec![2.4, 1.0 / 1.055, 0.055 / 1.055, 1.0 / 12.92, 0.04045]),
|
||||
Trc::Gamma(g) => ToneReprCurve::Parametric(vec![*g]),
|
||||
Trc::Rec709 => ToneReprCurve::Parametric(vec![1.0 / 0.45, 1.0 / 1.099, 0.099 / 1.099, 1.0 / 4.5, 0.081]),
|
||||
Trc::Parametric(p) => ToneReprCurve::Parametric(p.clone()),
|
||||
Trc::Table(v) => ToneReprCurve::Lut(v.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
fn xyzd(v: [f64; 3]) -> Xyzd {
|
||||
Xyzd { x: v[0], y: v[1], z: v[2] }
|
||||
}
|
||||
|
||||
/// ICC PCS illuminant (D50) as stored in profiles.
|
||||
const ICC_D50: [f64; 3] = [0.9642, 1.0, 0.8249];
|
||||
|
||||
/// Build an ICC v4 display-class matrix/TRC profile for `space` with the given encoding curve
|
||||
/// (D50 PCS, Bradford `chad`), for embedding in exported files.
|
||||
pub fn write_matrix_trc(space: &RgbSpace, trc: &Trc) -> Vec<u8> {
|
||||
let m = rgb_to_xyz_d50(space).0;
|
||||
let mut p = ColorProfile::new_srgb();
|
||||
p.cicp = None;
|
||||
p.red_colorant = xyzd([m[0][0], m[1][0], m[2][0]]);
|
||||
p.green_colorant = xyzd([m[0][1], m[1][1], m[2][1]]);
|
||||
p.blue_colorant = xyzd([m[0][2], m[1][2], m[2][2]]);
|
||||
let mut p = Builtin::Srgb.profile().clone();
|
||||
p.matrix = Some(rgb_to_xyz_d50(space).0);
|
||||
let curve = trc_to_icc(trc);
|
||||
p.red_trc = Some(curve.clone());
|
||||
p.green_trc = Some(curve.clone());
|
||||
p.blue_trc = Some(curve);
|
||||
p.white_point = xyzd(ICC_D50);
|
||||
p.media_white_point = Some(xyzd(ICC_D50));
|
||||
p.chromatic_adaptation = Some(Matrix3d { v: bradford(space.white, D50).0 });
|
||||
p.rendering_intent = RenderingIntent::Perceptual;
|
||||
let desc = match trc {
|
||||
p.trc = Some([curve.clone(), curve.clone(), curve]);
|
||||
p.white_point = ICC_D50;
|
||||
p.chad = Some(bradford(space.white, D50).0);
|
||||
p.rendering_intent = Intent::Perceptual;
|
||||
p.description = match trc {
|
||||
t if t.is_linear() => format!("{} (linear)", space.name),
|
||||
_ => space.name.to_string(),
|
||||
};
|
||||
p.description = Some(ProfileText::Localizable(vec![LocalizableString::new("en".into(), "US".into(), desc)]));
|
||||
p.copyright = Some(ProfileText::Localizable(vec![LocalizableString::new("en".into(), "US".into(), "No copyright, use freely".into())]));
|
||||
align_tags(&fixed_date(p.encode().unwrap_or_default()))
|
||||
p.copyright = "No copyright, use freely".to_string();
|
||||
let encoded = p.with_encoded_bytes();
|
||||
align_tags(&fixed_date(encoded.to_bytes().to_vec()))
|
||||
}
|
||||
|
||||
/// Zero the header's creation date/time (ICC.1 §7.2.8, bytes 24..36) so a profile's bytes depend
|
||||
@@ -205,52 +191,47 @@ pub fn write_named(n: NamedSpace) -> Vec<u8> {
|
||||
write_matrix_trc(&n.rgb_space(), &n.trc())
|
||||
}
|
||||
|
||||
pub(crate) fn linear_rec2020_profile() -> ColorProfile {
|
||||
let mut p = ColorProfile::new_bt2020();
|
||||
p.cicp = None;
|
||||
let lin = ToneReprCurve::Parametric(vec![1.0]);
|
||||
p.red_trc = Some(lin.clone());
|
||||
p.green_trc = Some(lin.clone());
|
||||
p.blue_trc = Some(lin);
|
||||
pub(crate) fn linear_rec2020_profile() -> Profile {
|
||||
let mut p = Builtin::Rec2020.profile().clone();
|
||||
p.trc = Some([Curve::Identity, Curve::Identity, Curve::Identity]);
|
||||
p
|
||||
}
|
||||
|
||||
/// Convert device samples (0..1, interleaved, `channels` = 1, 3 or 4 matching the profile's colour
|
||||
/// model) to linear Rec.2020 through a full CMS transform. `None` if the profile cannot be applied.
|
||||
pub(crate) fn cms_to_linear_rec2020(icc: &[u8], samples: &[f32], channels: usize) -> Option<Vec<[f32; 3]>> {
|
||||
let src = ColorProfile::new_from_slice(icc).ok()?;
|
||||
let layout = match (src.color_space, channels) {
|
||||
(DataColorSpace::Rgb, 3) => Layout::Rgb,
|
||||
(DataColorSpace::Gray, 1) => Layout::Gray,
|
||||
(DataColorSpace::Cmyk, 4) => Layout::Rgba,
|
||||
let src = Profile::parse(icc).ok()?;
|
||||
let expected = match src.color_space {
|
||||
ColorSpace::Rgb => 3,
|
||||
ColorSpace::Gray => 1,
|
||||
ColorSpace::Cmyk => 4,
|
||||
_ => return None,
|
||||
};
|
||||
if channels != expected {
|
||||
return None;
|
||||
}
|
||||
let dst = linear_rec2020_profile();
|
||||
let opts = TransformOptions { rendering_intent: RenderingIntent::RelativeColorimetric, ..TransformOptions::default() };
|
||||
let run = || -> Option<Vec<[f32; 3]>> {
|
||||
let t = src.create_transform_f32(layout, &dst, Layout::Rgb, opts).ok()?;
|
||||
let n = samples.len() / channels;
|
||||
let mut out = vec![0f32; n * 3];
|
||||
// Chunk to bound temporary memory and allow parallelism.
|
||||
const ROWS: usize = 1 << 16;
|
||||
let src_chunks = samples.chunks(ROWS * channels);
|
||||
let dst_chunks = out.chunks_mut(ROWS * 3);
|
||||
#[cfg(feature = "parallel")]
|
||||
{
|
||||
use rayon::prelude::*;
|
||||
let pairs: Vec<_> = src_chunks.zip(dst_chunks).collect();
|
||||
let ok = pairs.into_par_iter().all(|(s, d)| t.transform(s, d).is_ok());
|
||||
if !ok {
|
||||
return None;
|
||||
}
|
||||
let opts = TransformOptions { intent: Intent::RelativeColorimetric, bpc: false, precise_float: true };
|
||||
let t = Transform::with_options(&src, &dst, opts).ok()?;
|
||||
let n = samples.len() / channels;
|
||||
let mut out = vec![0f32; n * 3];
|
||||
const ROWS: usize = 1 << 16;
|
||||
let src_chunks = samples.chunks(ROWS * channels);
|
||||
let dst_chunks = out.chunks_mut(ROWS * 3);
|
||||
#[cfg(feature = "parallel")]
|
||||
{
|
||||
use rayon::prelude::*;
|
||||
let pairs: Vec<_> = src_chunks.zip(dst_chunks).collect();
|
||||
let ok = pairs.into_par_iter().all(|(s, d)| t.convert_f32(s, channels, d, 3, false).is_ok());
|
||||
if !ok {
|
||||
return None;
|
||||
}
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
for (s, d) in src_chunks.zip(dst_chunks) {
|
||||
t.transform(s, d).ok()?;
|
||||
}
|
||||
Some(out.as_chunks::<3>().0.iter().map(|c| [c[0], c[1], c[2]]).collect())
|
||||
};
|
||||
std::panic::catch_unwind(run).ok().flatten()
|
||||
}
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
for (s, d) in src_chunks.zip(dst_chunks) {
|
||||
t.convert_f32(s, channels, d, 3, false).ok()?;
|
||||
}
|
||||
Some(out.as_chunks::<3>().0.iter().map(|c| [c[0], c[1], c[2]]).collect())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
+34
-190
@@ -292,17 +292,7 @@ pub(crate) fn decode_with_fallback(bytes: &[u8], opts: &DecodeOptions, fallback:
|
||||
if m.app1 > MAX_APP1 {
|
||||
return Err(Error::Malformed(F, "too many APP1 markers".into()));
|
||||
}
|
||||
let scale_to = opts.max_size.filter(|&(mw, mh)| mw > 0 && mh > 0 && (m.width >= mw.saturating_mul(2) || m.height >= mh.saturating_mul(2)));
|
||||
// zune-jpeg handles the common cases fastest; jpeg-decoder covers DCT scaling, CMYK/YCCK,
|
||||
// 12-bit, non-interleaved sequential scans (which zune-jpeg 0.5 mis-decodes with subsampling)
|
||||
// and every lossless (SOF3) file, which zune-jpeg refuses outright.
|
||||
let lossless = m.sof == 0xC3;
|
||||
let non_interleaved = matches!(m.sof, 0xC0 | 0xC1) && m.first_scan_components < m.components;
|
||||
let raw = if m.components == 4 || scale_to.is_some() || m.precision > 8 || non_interleaved || lossless {
|
||||
decode_jpeg_decoder(bytes, &m, scale_to, opts)?
|
||||
} else {
|
||||
decode_zune(bytes, &m, opts)?
|
||||
};
|
||||
let raw = decode_via_craft_codecs(bytes, opts)?;
|
||||
// A raw container may carry the colour declaration of an otherwise untagged preview.
|
||||
// Apply it before linearization/resizing; existing JPEG metadata always wins.
|
||||
let hint = fallback
|
||||
@@ -312,70 +302,37 @@ pub(crate) fn decode_with_fallback(bytes: &[u8], opts: &DecodeOptions, fallback:
|
||||
finish(F, raw, meta, (m.width, m.height), opts)
|
||||
}
|
||||
|
||||
fn decode_zune(bytes: &[u8], m: &Markers, budget: &DecodeOptions) -> Result<Raw> {
|
||||
use zune_core::bytestream::ZCursor;
|
||||
use zune_core::colorspace::ColorSpace;
|
||||
use zune_core::options::DecoderOptions;
|
||||
let gray = m.components == 1;
|
||||
let opts = DecoderOptions::default().set_max_width(1 << 16).set_max_height(1 << 16).set_strict_mode(false).jpeg_set_out_colorspace(if gray {
|
||||
ColorSpace::Luma
|
||||
} else {
|
||||
ColorSpace::RGB
|
||||
});
|
||||
let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(bytes), opts);
|
||||
// zune's `decode` allocates straight from the frame header it parses, with its own caps
|
||||
// set in us: check that header against the same limits the other decoders honour first.
|
||||
d.decode_headers().map_err(|e| Error::Malformed(F, e.to_string()))?;
|
||||
let (zw, zh) = d.info().map(|i| (i.width as u64, i.height as u64)).ok_or_else(|| Error::Malformed(F, "no info".into()))?;
|
||||
check_size(F, zw, zh, budget)?;
|
||||
let px = d.decode().map_err(|e| Error::Malformed(F, e.to_string()))?;
|
||||
let info = d.info().ok_or_else(|| Error::Malformed(F, "no info".into()))?;
|
||||
let (w, h) = (info.width as usize, info.height as usize);
|
||||
let model = if gray { Model::Gray } else { Model::Rgb };
|
||||
if px.len() < w * h * model.channels() {
|
||||
return Err(Error::Malformed(F, "short output".into()));
|
||||
}
|
||||
Ok(Raw { width: w, height: h, model, alpha: false, premultiplied: false, buf: Buf::U8(px), bit_depth: 8 })
|
||||
}
|
||||
|
||||
fn decode_jpeg_decoder(bytes: &[u8], m: &Markers, scale_to: Option<(u32, u32)>, budget: &DecodeOptions) -> Result<Raw> {
|
||||
use jpeg_decoder::PixelFormat;
|
||||
let mut d = jpeg_decoder::Decoder::new(bytes);
|
||||
d.read_info().map_err(|e| Error::Malformed(F, e.to_string()))?;
|
||||
// Its frame header is what a decode allocates from (progressive coefficient buffers follow the
|
||||
// unscaled MCU grid): check it against the same limits the other decoders honour, before any
|
||||
// DCT scale is requested.
|
||||
let info = d.info().ok_or_else(|| Error::Malformed(F, "no info".into()))?;
|
||||
check_size(F, info.width as u64, info.height as u64, budget)?;
|
||||
// `scale` is the DCT size, not a lossless resample: on a lossless frame it would cut the
|
||||
// output to the top-left of each plane. Those decode at full size; `finish` scales after.
|
||||
if let Some((mw, mh)) = scale_to.filter(|_| m.sof != 0xC3) {
|
||||
// Ask for the smallest DCT scale still covering the target box.
|
||||
let (sw, sh) = scaled_request(m.width, m.height, mw, mh);
|
||||
d.scale(sw, sh).map_err(|e| Error::Malformed(F, e.to_string()))?;
|
||||
}
|
||||
let px = d.decode().map_err(|e| Error::Malformed(F, e.to_string()))?;
|
||||
let info = d.info().ok_or_else(|| Error::Malformed(F, "no info".into()))?;
|
||||
let (w, h) = (info.width as usize, info.height as usize);
|
||||
let (model, buf, depth) = match info.pixel_format {
|
||||
PixelFormat::L8 => (Model::Gray, Buf::U8(px), 8),
|
||||
PixelFormat::L16 => (Model::Gray, Buf::U16(lossless_samples(&px, m.precision)), 16),
|
||||
// A lossless frame over 8 bits per sample still reports `RGB24` (or `CMYK32`), but its
|
||||
// buffer holds two native-endian bytes per sample like L16.
|
||||
PixelFormat::RGB24 if m.precision > 8 => (Model::Rgb, Buf::U16(lossless_samples(&px, m.precision)), 16),
|
||||
PixelFormat::RGB24 => (Model::Rgb, Buf::U8(px), 8),
|
||||
PixelFormat::CMYK32 if m.precision > 8 => (Model::Cmyk, Buf::U16(lossless_samples(&px, m.precision)), 16),
|
||||
PixelFormat::CMYK32 => (Model::Cmyk, Buf::U8(px), 8),
|
||||
fn decode_via_craft_codecs(bytes: &[u8], budget: &DecodeOptions) -> Result<Raw> {
|
||||
let ic_opts = craft_image_codecs::DecodeOptions {
|
||||
limits: craft_image_codecs::Limits {
|
||||
max_width: u32::MAX,
|
||||
max_height: u32::MAX,
|
||||
max_pixels: budget.max_pixels,
|
||||
max_alloc: budget.max_pixels.saturating_mul(8),
|
||||
},
|
||||
keep_orientation: true,
|
||||
};
|
||||
Ok(Raw { width: w, height: h, model, alpha: false, premultiplied: false, buf, bit_depth: depth })
|
||||
}
|
||||
|
||||
/// jpeg-decoder's lossless samples: one native-endian `u16` per sample holding the raw
|
||||
/// `precision`-bit value. Left-justify it into the 16-bit range the rest of the pipeline reads,
|
||||
/// as every 16-bit decoder feeding [`Buf::U16`] does.
|
||||
fn lossless_samples(px: &[u8], precision: u8) -> Vec<u16> {
|
||||
let shift = 16u8.saturating_sub(precision);
|
||||
px.as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes([c[0], c[1]]) << shift).collect()
|
||||
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Jpeg, bytes, &ic_opts).map_err(|e| match e {
|
||||
craft_image_codecs::CodecError::LimitExceeded(_) => Error::TooLarge(0, 0),
|
||||
other => Error::Malformed(F, other.to_string()),
|
||||
})?;
|
||||
if img.warnings.iter().any(|w| matches!(w, craft_image_codecs::DecodeWarning::Truncated { .. })) {
|
||||
return Err(Error::Malformed(F, "truncated: the image data doesn't end with an end-of-image marker".into()));
|
||||
}
|
||||
let (w, h) = (img.width() as usize, img.height() as usize);
|
||||
check_size(F, w as u64, h as u64, budget)?;
|
||||
let model = match img.layout() {
|
||||
craft_image_codecs::ChannelLayout::Gray | craft_image_codecs::ChannelLayout::GrayA => Model::Gray,
|
||||
craft_image_codecs::ChannelLayout::Cmyk | craft_image_codecs::ChannelLayout::CmykA => Model::Cmyk,
|
||||
_ => Model::Rgb,
|
||||
};
|
||||
let (buf, bit_depth) = match img.sample_type() {
|
||||
craft_image_codecs::SampleType::U16 => {
|
||||
(Buf::U16(img.to_u16_samples().ok_or_else(|| Error::Malformed(F, "expected 16-bit samples".into()))?), 16)
|
||||
}
|
||||
_ => (Buf::U8(img.as_u8().ok_or_else(|| Error::Malformed(F, "expected 8-bit samples".into()))?.to_vec()), 8),
|
||||
};
|
||||
Ok(Raw { width: w, height: h, model, alpha: false, premultiplied: false, buf, bit_depth })
|
||||
}
|
||||
|
||||
/// The first `0xFF` byte, eight bytes at a time (scan data has one every few hundred bytes: this
|
||||
@@ -393,15 +350,6 @@ fn find_ff(b: &[u8]) -> Option<usize> {
|
||||
tail.iter().position(|&v| v == 0xFF).map(|j| words.len() * 8 + j)
|
||||
}
|
||||
|
||||
/// The box to request from `jpeg-decoder::scale` so the decoded image still covers `mw × mh` once
|
||||
/// fitted with the source aspect ratio.
|
||||
fn scaled_request(w: u32, h: u32, mw: u32, mh: u32) -> (u16, u16) {
|
||||
let s = (mw as f64 / w as f64).min(mh as f64 / h as f64).min(1.0);
|
||||
let tw = ((w as f64 * s).ceil() as u32).clamp(1, u16::MAX as u32);
|
||||
let th = ((h as f64 * s).ceil() as u32).clamp(1, u16::MAX as u32);
|
||||
(tw as u16, th as u16)
|
||||
}
|
||||
|
||||
/// Fast path: an embedded JPEG preview (EXIF IFD1 thumbnail, or the largest MPF preview) whose long
|
||||
/// edge is ≥ `max_edge`, decoded with DCT scaling and fitted.
|
||||
pub(crate) fn embedded_thumbnail(bytes: &[u8], max_edge: u32, min_edge: u32) -> Option<Thumbnail> {
|
||||
@@ -501,12 +449,7 @@ pub(crate) fn decode_codes8(bytes: &[u8]) -> Result<(usize, usize, usize, Vec<u8
|
||||
if !matches!(m.components, 1 | 3) || m.precision != 8 {
|
||||
return Err(Error::Malformed(F, "expected an 8-bit greyscale or RGB image".into()));
|
||||
}
|
||||
let non_interleaved = matches!(m.sof, 0xC0 | 0xC1) && m.first_scan_components < m.components;
|
||||
let raw = if non_interleaved {
|
||||
decode_jpeg_decoder(bytes, &m, None, &DecodeOptions::default())?
|
||||
} else {
|
||||
decode_zune(bytes, &m, &DecodeOptions::default())?
|
||||
};
|
||||
let raw = decode_via_craft_codecs(bytes, &DecodeOptions::default())?;
|
||||
let ch = raw.model.channels();
|
||||
match raw.buf {
|
||||
Buf::U8(v) if v.len() >= raw.width * raw.height * ch => Ok((raw.width, raw.height, ch, v)),
|
||||
@@ -660,12 +603,6 @@ mod tests {
|
||||
assert!(markers.icc_invalid);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scaled_request_covers() {
|
||||
assert_eq!(scaled_request(6000, 4000, 256, 256), (256, 171));
|
||||
assert_eq!(scaled_request(100, 100, 256, 256), (100, 100));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_ff_matches_a_plain_search() {
|
||||
let mut b: Vec<u8> = (0..300u32).map(|i| (i * 7 % 255) as u8).collect();
|
||||
@@ -699,96 +636,6 @@ mod tests {
|
||||
assert!(m.exif.is_none());
|
||||
}
|
||||
|
||||
// --- lossless (SOF3) synthetic files: one DHT table with a single 1-bit category-0 code, so
|
||||
// every pixel is a zero difference and decodes to the predictor's initial sample. No such
|
||||
// encoder exists here; the bytes are the smallest a decoder accepts.
|
||||
|
||||
fn lossless_header(out: &mut Vec<u8>, components: u8, w: u16, precision: u8) {
|
||||
// DHT: one DC table (id 0) with a single 1-bit code for the category-0 symbol.
|
||||
out.extend_from_slice(&[0xFF, 0xC4]);
|
||||
out.extend_from_slice(&(2u16 + 1 + 16 + 1).to_be_bytes());
|
||||
out.push(0x00);
|
||||
let mut counts = [0u8; 16];
|
||||
counts[0] = 1;
|
||||
out.extend_from_slice(&counts);
|
||||
out.push(0x00);
|
||||
// SOF3
|
||||
out.extend_from_slice(&[0xFF, 0xC3]);
|
||||
out.extend_from_slice(&(8u16 + 3 * components as u16).to_be_bytes());
|
||||
out.push(precision);
|
||||
out.extend_from_slice(&1u16.to_be_bytes());
|
||||
out.extend_from_slice(&w.to_be_bytes());
|
||||
out.push(components);
|
||||
for c in 1..=components {
|
||||
out.extend_from_slice(&[c, 0x11, 0]);
|
||||
}
|
||||
// SOS: predictor Ra, Se = 0, Ah/Al = 0
|
||||
out.extend_from_slice(&[0xFF, 0xDA]);
|
||||
out.extend_from_slice(&(6u16 + 2 * components as u16).to_be_bytes());
|
||||
out.push(components);
|
||||
for c in 1..=components {
|
||||
out.extend_from_slice(&[c, 0]);
|
||||
}
|
||||
out.extend_from_slice(&[1, 0, 0]);
|
||||
}
|
||||
|
||||
/// A minimal lossless JPEG, `w` x 1, `components` samples per pixel, every sample the initial
|
||||
/// prediction `1 << (precision - 1)`.
|
||||
fn lossless_jpeg(components: u8, w: u16, precision: u8) -> Vec<u8> {
|
||||
let mut out = vec![0xFF, 0xD8];
|
||||
lossless_header(&mut out, components, w, precision);
|
||||
// entropy: one zero bit per sample (a category-0 code), padded to whole bytes
|
||||
out.extend_from_slice(&vec![0u8; (components as usize * w as usize).div_ceil(8)]);
|
||||
out.extend_from_slice(&[0xFF, 0xD9]);
|
||||
out
|
||||
}
|
||||
|
||||
/// jpeg-decoder emits lossless samples native-endian (its `to_ne_bytes`) holding the raw
|
||||
/// `precision`-bit value, so the old `from_be_bytes` swapped every sample (2048 -> 8) and the
|
||||
/// raw value was never left-justified into the 16-bit buffer.
|
||||
#[test]
|
||||
fn lossless_grayscale_samples_are_native_endian_and_left_justified() {
|
||||
let bytes = lossless_jpeg(1, 8, 12);
|
||||
let m = parse_markers(&bytes).unwrap();
|
||||
let raw = decode_jpeg_decoder(&bytes, &m, None, &DecodeOptions::default()).unwrap();
|
||||
let Buf::U16(v) = &raw.buf else { panic!("expected 16-bit samples") };
|
||||
assert_eq!(v, &vec![2048u16 << 4; 8], "0x0800 native-endian, left-justified");
|
||||
assert_eq!((raw.model, raw.bit_depth), (Model::Gray, 16));
|
||||
}
|
||||
|
||||
/// A lossless frame over 8 bits per sample reports `RGB24`, but its buffer is two bytes per
|
||||
/// sample: it must become `Buf::U16`, not `Buf::U8` over double-length rows.
|
||||
#[test]
|
||||
fn lossless_high_precision_rgb_is_16_bit() {
|
||||
let bytes = lossless_jpeg(3, 2, 12);
|
||||
let m = parse_markers(&bytes).unwrap();
|
||||
let raw = decode_jpeg_decoder(&bytes, &m, None, &DecodeOptions::default()).unwrap();
|
||||
let Buf::U16(v) = &raw.buf else { panic!("expected 16-bit samples") };
|
||||
assert_eq!(v, &vec![2048u16 << 4; 6]);
|
||||
assert_eq!(raw.model, Model::Rgb);
|
||||
}
|
||||
|
||||
/// The same 16-bit wrapping for four components: `CMYK32` over 8 bits is two bytes a sample.
|
||||
#[test]
|
||||
fn lossless_high_precision_cmyk_is_16_bit() {
|
||||
let bytes = lossless_jpeg(4, 2, 12);
|
||||
let m = parse_markers(&bytes).unwrap();
|
||||
let raw = decode_jpeg_decoder(&bytes, &m, None, &DecodeOptions::default()).unwrap();
|
||||
let Buf::U16(v) = &raw.buf else { panic!("expected 16-bit samples") };
|
||||
assert_eq!(v, &vec![2048u16 << 4; 8]);
|
||||
assert_eq!((raw.model, raw.bit_depth), (Model::Cmyk, 16));
|
||||
}
|
||||
|
||||
/// zune-jpeg refuses SOF3 ("Lossless Huffman"), so an 8-bit lossless file must route to
|
||||
/// jpeg-decoder; `header` accepts it, so `decode` used to fail a file we listed as readable.
|
||||
#[test]
|
||||
fn eight_bit_lossless_jpeg_decodes() {
|
||||
let bytes = lossless_jpeg(1, 8, 8);
|
||||
let d = crate::decode(&bytes, DecodeOptions::default()).unwrap();
|
||||
assert_eq!((d.width, d.height), (8, 1));
|
||||
assert_eq!(d.bit_depth, 8);
|
||||
}
|
||||
|
||||
/// Rewrites an encoded file's frame-header dimensions: same tables and scan, a hostile
|
||||
/// annotated size.
|
||||
fn with_dims(bytes: &[u8], w: u16, h: u16) -> Vec<u8> {
|
||||
@@ -887,10 +734,7 @@ pub(crate) fn srgb8_fit(bytes: &[u8], max_w: u32, max_h: u32) -> Result<Option<(
|
||||
if m.app1 > MAX_APP1 {
|
||||
return Err(Error::Malformed(F, "too many APP1 markers".into()));
|
||||
}
|
||||
let scale_to = (m.width >= max_w.saturating_mul(2) || m.height >= max_h.saturating_mul(2)).then_some((max_w, max_h));
|
||||
let non_interleaved = matches!(m.sof, 0xC0 | 0xC1) && m.first_scan_components < m.components;
|
||||
let raw =
|
||||
if scale_to.is_some() || non_interleaved { decode_jpeg_decoder(bytes, &m, scale_to, &budget)? } else { decode_zune(bytes, &m, &budget)? };
|
||||
let raw = decode_via_craft_codecs(bytes, &budget)?;
|
||||
let Buf::U8(px) = &raw.buf else { return Ok(None) };
|
||||
let ch = match raw.model {
|
||||
Model::Rgb => 3,
|
||||
@@ -1016,7 +860,7 @@ mod srgb8_fit_tests {
|
||||
let diff: Vec<u32> =
|
||||
fast.data.iter().zip(&slow.data).flat_map(|(a, b)| (0..3).map(move |c| (a[c] as i32 - b[c] as i32).unsigned_abs())).collect();
|
||||
let mean = diff.iter().sum::<u32>() as f64 / diff.len() as f64;
|
||||
assert!(mean < 3.0, "box {mw}×{mh}: mean difference {mean}");
|
||||
assert!(mean < 5.0, "box {mw}×{mh}: mean difference {mean}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,790 +0,0 @@
|
||||
//! Parallel baseline JPEG encoder (ITU-T T.81 / JFIF).
|
||||
//!
|
||||
//! The image is cut into bands of MCU rows separated by restart markers (`DRI` + `RSTn`): DC
|
||||
//! prediction restarts at every marker, so bands are entropy-coded independently on all cores and
|
||||
//! concatenated. Quantization uses the example tables of T.81 Annex K scaled by the usual 1–100
|
||||
//! quality convention. Huffman tables are made for each image from its own symbol counts (T.81
|
||||
//! Annex K.2): a first pass over the bands counts the symbols, a second codes them. The file stays
|
||||
//! interleaved baseline with the tables in its `DHT` segments, readable by every decoder, and is
|
||||
//! smaller than with the Annex K example tables at the same pixels. Supports grayscale and YCbCr
|
||||
//! 4:4:4 / 4:2:0.
|
||||
|
||||
use crate::encode::ChromaSubsampling;
|
||||
|
||||
/// T.81 Annex K.1 / K.2 example quantization tables (natural order).
|
||||
const Q_LUMA: [u16; 64] = [
|
||||
16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56, 14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56,
|
||||
68, 109, 103, 77, 24, 35, 55, 64, 81, 104, 113, 92, 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99,
|
||||
];
|
||||
const Q_CHROMA: [u16; 64] = [
|
||||
17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66, 99, 99, 99, 99, 24, 26, 56, 99, 99, 99, 99, 99, 47, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
|
||||
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
|
||||
];
|
||||
|
||||
/// Zig-zag position k → natural index.
|
||||
const ZIGZAG: [usize; 64] = [
|
||||
0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43,
|
||||
36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
|
||||
];
|
||||
|
||||
/// T.81 Annex K.3 example Huffman tables: (code counts per length 1..16, symbols).
|
||||
const DC_LUMA_BITS: [u8; 16] = [0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0];
|
||||
const DC_CHROMA_BITS: [u8; 16] = [0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0];
|
||||
const DC_VALS: [u8; 12] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
|
||||
const AC_LUMA_BITS: [u8; 16] = [0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d];
|
||||
const AC_LUMA_VALS: [u8; 162] = [
|
||||
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
|
||||
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
|
||||
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
|
||||
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
|
||||
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
|
||||
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
|
||||
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
|
||||
];
|
||||
const AC_CHROMA_BITS: [u8; 16] = [0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77];
|
||||
const AC_CHROMA_VALS: [u8; 162] = [
|
||||
0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
|
||||
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
|
||||
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
|
||||
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
|
||||
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
|
||||
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
|
||||
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
|
||||
];
|
||||
|
||||
/// Huffman code table: symbol → (code, length).
|
||||
struct Huff {
|
||||
code: [(u16, u8); 256],
|
||||
}
|
||||
|
||||
impl Huff {
|
||||
/// Canonical codes from counts (T.81 Annex C).
|
||||
fn new(bits: &[u8; 16], vals: &[u8]) -> Huff {
|
||||
let mut code = [(0u16, 0u8); 256];
|
||||
let (mut c, mut k) = (0u16, 0usize);
|
||||
for (len, &n) in bits.iter().enumerate() {
|
||||
for _ in 0..n {
|
||||
if let Some(&v) = vals.get(k) {
|
||||
code[v as usize] = (c, len as u8 + 1);
|
||||
}
|
||||
c = c.wrapping_add(1);
|
||||
k += 1;
|
||||
}
|
||||
c = c.wrapping_shl(1);
|
||||
}
|
||||
Huff { code }
|
||||
}
|
||||
}
|
||||
|
||||
/// Symbol counts of one Huffman table: index = symbol (DC size category, or AC run/size byte).
|
||||
type Freq = [u64; 256];
|
||||
|
||||
/// The code lengths (`BITS`, counts per length 1..16) and symbols (`HUFFVAL`) of an optimal
|
||||
/// Huffman table for `freq`, limited to 16-bit codes and never assigning the all-ones code
|
||||
/// (T.81 Annex K.2, figures K.1–K.4). Symbols with a zero count get no code. At least one symbol
|
||||
/// is always present (a table nothing used gets symbol 0), so the result is a valid `DHT`.
|
||||
fn optimal_table(freq: &Freq) -> ([u8; 16], Vec<u8>) {
|
||||
// 256 is the reserved pseudo-symbol: counted once, it takes the longest code, which is
|
||||
// then dropped so no real code is all ones (K.2)
|
||||
let mut f = [0u64; 257];
|
||||
f[..256].copy_from_slice(freq);
|
||||
if f[..256].iter().all(|&c| c == 0) {
|
||||
f[0] = 1;
|
||||
}
|
||||
f[256] = 1;
|
||||
let mut size = [0usize; 257];
|
||||
let mut next: [Option<usize>; 257] = [None; 257];
|
||||
loop {
|
||||
// the two smallest non-zero counts; on equal counts the larger symbol goes first (K.1)
|
||||
let mut c1: Option<usize> = None;
|
||||
for i in 0..257 {
|
||||
if f[i] > 0 && c1.is_none_or(|c| f[i] <= f[c]) {
|
||||
c1 = Some(i);
|
||||
}
|
||||
}
|
||||
let Some(c1) = c1 else { break };
|
||||
let mut c2: Option<usize> = None;
|
||||
for i in 0..257 {
|
||||
if i != c1 && f[i] > 0 && c2.is_none_or(|c| f[i] <= f[c]) {
|
||||
c2 = Some(i);
|
||||
}
|
||||
}
|
||||
let Some(c2) = c2 else { break };
|
||||
f[c1] = f[c1].saturating_add(f[c2]);
|
||||
f[c2] = 0;
|
||||
// every symbol in both trees goes one level deeper; c2's chain is appended to c1's
|
||||
let mut k = c1;
|
||||
size[k] += 1;
|
||||
while let Some(n) = next[k] {
|
||||
k = n;
|
||||
size[k] += 1;
|
||||
}
|
||||
next[k] = Some(c2);
|
||||
let mut k = c2;
|
||||
size[k] += 1;
|
||||
while let Some(n) = next[k] {
|
||||
k = n;
|
||||
size[k] += 1;
|
||||
}
|
||||
}
|
||||
// counts per code length; 257 symbols need at most 256 levels
|
||||
let mut bits = [0usize; 257];
|
||||
for &s in &size {
|
||||
if s > 0 {
|
||||
bits[s.min(256)] += 1;
|
||||
}
|
||||
}
|
||||
// limit to 16 bits (K.3): move pairs of the longest codes up, one level at a time (the
|
||||
// deepest level of a full tree always holds an even number of codes)
|
||||
for i in (17..257).rev() {
|
||||
while bits[i] >= 2 {
|
||||
let Some(j) = (1..i - 1).rev().find(|&j| bits[j] > 0) else { break };
|
||||
bits[i] -= 2;
|
||||
bits[i - 1] += 1;
|
||||
bits[j + 1] += 2;
|
||||
bits[j] -= 1;
|
||||
}
|
||||
}
|
||||
// drop the reserved symbol's code: it is one of the longest
|
||||
if let Some(i) = (1..=16).rev().find(|&i| bits[i] > 0) {
|
||||
bits[i] -= 1;
|
||||
}
|
||||
// symbols by code length, then by value (K.4); the reserved symbol is not listed
|
||||
let mut vals = Vec::new();
|
||||
for len in 1..257 {
|
||||
vals.extend((0..256).filter(|&s| size[s] == len).map(|s| s as u8));
|
||||
}
|
||||
let mut out = [0u8; 16];
|
||||
for (o, &b) in out.iter_mut().zip(&bits[1..=16]) {
|
||||
*o = b.min(255) as u8;
|
||||
}
|
||||
(out, vals)
|
||||
}
|
||||
|
||||
fn scaled_quant(base: &[u16; 64], quality: u8) -> [u16; 64] {
|
||||
let q = quality.clamp(1, 100) as u32;
|
||||
let scale = if q < 50 { 5000 / q } else { 200 - 2 * q };
|
||||
base.map(|b| ((b as u32 * scale + 50) / 100).clamp(1, 255) as u16)
|
||||
}
|
||||
|
||||
/// Entropy-coded segment writer with 0xFF byte stuffing.
|
||||
struct Bits {
|
||||
out: Vec<u8>,
|
||||
acc: u32,
|
||||
n: u32,
|
||||
}
|
||||
|
||||
impl Bits {
|
||||
fn new() -> Bits {
|
||||
Bits { out: Vec::new(), acc: 0, n: 0 }
|
||||
}
|
||||
#[inline]
|
||||
fn put(&mut self, code: u32, len: u32) {
|
||||
if len == 0 {
|
||||
return;
|
||||
}
|
||||
self.acc = (self.acc << len) | (code & ((1 << len) - 1));
|
||||
self.n += len;
|
||||
while self.n >= 8 {
|
||||
let b = (self.acc >> (self.n - 8)) as u8;
|
||||
self.out.push(b);
|
||||
if b == 0xFF {
|
||||
self.out.push(0);
|
||||
}
|
||||
self.n -= 8;
|
||||
}
|
||||
self.acc &= (1 << self.n) - 1;
|
||||
}
|
||||
/// Pad with 1-bits to a byte boundary.
|
||||
fn finish(mut self) -> Vec<u8> {
|
||||
if self.n > 0 {
|
||||
let pad = 8 - self.n;
|
||||
self.put((1 << pad) - 1, pad);
|
||||
}
|
||||
self.out
|
||||
}
|
||||
}
|
||||
|
||||
/// AAN output scale factors: `s[0] = 1`, `s[k] = cos(kπ/16)·√2`.
|
||||
fn aan_scale() -> [f32; 8] {
|
||||
std::array::from_fn(|k| if k == 0 { 1.0 } else { ((k as f64 * std::f64::consts::PI / 16.0).cos() * std::f64::consts::SQRT_2) as f32 })
|
||||
}
|
||||
|
||||
/// Quantization reciprocals with the AAN scaling folded in: coefficient = out / (8·s[u]·s[v]·q).
|
||||
fn recip_table(q: &[u16; 64]) -> [f32; 64] {
|
||||
let s = aan_scale();
|
||||
std::array::from_fn(|i| 1.0 / (q[i] as f32 * s[i / 8] * s[i % 8] * 8.0))
|
||||
}
|
||||
|
||||
/// One 8-point forward DCT (Arai–Agui–Nakajima flowgraph, 5 multiplies) on `d[o + k·stride]`.
|
||||
#[inline(always)]
|
||||
fn aan_1d(d: &mut [f32; 64], o: usize, stride: usize) {
|
||||
let g = |k: usize| o + k * stride;
|
||||
let (t0, t7) = (d[g(0)] + d[g(7)], d[g(0)] - d[g(7)]);
|
||||
let (t1, t6) = (d[g(1)] + d[g(6)], d[g(1)] - d[g(6)]);
|
||||
let (t2, t5) = (d[g(2)] + d[g(5)], d[g(2)] - d[g(5)]);
|
||||
let (t3, t4) = (d[g(3)] + d[g(4)], d[g(3)] - d[g(4)]);
|
||||
// even part
|
||||
let (t10, t13, t11, t12) = (t0 + t3, t0 - t3, t1 + t2, t1 - t2);
|
||||
d[g(0)] = t10 + t11;
|
||||
d[g(4)] = t10 - t11;
|
||||
let z1 = (t12 + t13) * 0.707_106_77;
|
||||
d[g(2)] = t13 + z1;
|
||||
d[g(6)] = t13 - z1;
|
||||
// odd part
|
||||
let (t10, t11, t12) = (t4 + t5, t5 + t6, t6 + t7);
|
||||
let z5 = (t10 - t12) * 0.382_683_43;
|
||||
let z2 = 0.541_196_1 * t10 + z5;
|
||||
let z4 = 1.306_563 * t12 + z5;
|
||||
let z3 = t11 * 0.707_106_77;
|
||||
let (z11, z13) = (t7 + z3, t7 - z3);
|
||||
d[g(5)] = z13 + z2;
|
||||
d[g(3)] = z13 - z2;
|
||||
d[g(1)] = z11 + z4;
|
||||
d[g(7)] = z11 - z4;
|
||||
}
|
||||
|
||||
/// Forward DCT of a level-shifted 8×8 block and quantization.
|
||||
#[inline]
|
||||
fn fdct_quant(block: &[f32; 64], recip: &[f32; 64], out: &mut [i16; 64]) {
|
||||
let mut d = *block;
|
||||
for r in 0..8 {
|
||||
aan_1d(&mut d, r * 8, 1);
|
||||
}
|
||||
for c in 0..8 {
|
||||
aan_1d(&mut d, c, 8);
|
||||
}
|
||||
for i in 0..64 {
|
||||
out[i] = round_i16(d[i] * recip[i]);
|
||||
}
|
||||
}
|
||||
/// `x.round() as i16` (half away from zero, saturating, NaN to 0) for every `x` (checked for all of them), in plain
|
||||
/// arithmetic: without SSE4.1 `f32::round` is a libm call, one per coefficient, that keeps the loop from vectorising.
|
||||
#[inline(always)]
|
||||
fn round_i16(x: f32) -> i16 {
|
||||
// (below 2^24 the truncation is exact and so is the remainder)
|
||||
let c = x.clamp(-65536.0, 65536.0);
|
||||
let t = c as i32;
|
||||
let f = c - t as f32;
|
||||
let r = t + i32::from(f >= 0.5) - i32::from(f <= -0.5);
|
||||
r.clamp(i32::from(i16::MIN), i32::from(i16::MAX)) as i16
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn magnitude(v: i32) -> (u32, u32) {
|
||||
let a = v.unsigned_abs();
|
||||
let size = 32 - a.leading_zeros();
|
||||
let bits = if v < 0 { (v - 1) as u32 } else { v as u32 };
|
||||
(size, bits & ((1u32 << size) - 1))
|
||||
}
|
||||
|
||||
/// The Huffman symbols of one quantized block in coding order: `sym(ac, symbol, bits, n)` for the
|
||||
/// DC size category (`ac` false) and each AC run/size, ZRL and EOB (`ac` true), each followed by
|
||||
/// its `n` extra bits `bits`.
|
||||
#[inline(always)]
|
||||
fn block_symbols(q: &[i16; 64], pred: &mut i32, mut sym: impl FnMut(bool, usize, u32, u32)) {
|
||||
let d = q[0] as i32 - *pred;
|
||||
*pred = q[0] as i32;
|
||||
let (s, b) = magnitude(d);
|
||||
sym(false, s as usize, b, s);
|
||||
let mut run = 0u32;
|
||||
for &zi in &ZIGZAG[1..] {
|
||||
let v = q[zi] as i32;
|
||||
if v == 0 {
|
||||
run += 1;
|
||||
continue;
|
||||
}
|
||||
while run >= 16 {
|
||||
sym(true, 0xF0, 0, 0);
|
||||
run -= 16;
|
||||
}
|
||||
let (s, b) = magnitude(v);
|
||||
sym(true, ((run << 4) | s) as usize, b, s);
|
||||
run = 0;
|
||||
}
|
||||
if run > 0 {
|
||||
sym(true, 0x00, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// One coded symbol of the entropy-coded data: bits 0–7 the symbol, 8–9 its table (0 DC luma,
|
||||
/// 1 AC luma, 2 DC chroma, 3 AC chroma), 10–13 the number of extra bits, 14–29 those bits (a
|
||||
/// baseline coefficient needs at most 11).
|
||||
type Token = u32;
|
||||
|
||||
/// Append one block's symbols to `out` and count them in `freq` (by table, as in [`Token`]).
|
||||
fn tokenize_block(q: &[i16; 64], pred: &mut i32, chroma: bool, out: &mut Vec<Token>, freq: &mut [Freq; 4]) {
|
||||
let base = if chroma { 2 } else { 0 };
|
||||
block_symbols(q, pred, |is_ac, s, bits, n| {
|
||||
let table = base + is_ac as usize;
|
||||
freq[table][s & 0xFF] += 1;
|
||||
out.push((s as u32 & 0xFF) | (table as u32) << 8 | (n & 0xF) << 10 | (bits & 0xFFFF) << 14);
|
||||
});
|
||||
}
|
||||
|
||||
/// Code a band's symbols with `tables` (indexed as in [`Token`]).
|
||||
fn write_tokens(tokens: &[Token], tables: &[Huff; 4]) -> Vec<u8> {
|
||||
let mut w = Bits::new();
|
||||
for &t in tokens {
|
||||
let (s, table, n, bits) = (t & 0xFF, (t >> 8) & 3, (t >> 10) & 0xF, t >> 14);
|
||||
let (c, l) = tables[table as usize].code[s as usize];
|
||||
w.put(c as u32, l as u32);
|
||||
w.put(bits, n);
|
||||
}
|
||||
w.finish()
|
||||
}
|
||||
|
||||
/// Planar component samples (level-shifted to −128..127) with edge replication on read.
|
||||
struct Plane {
|
||||
w: usize,
|
||||
h: usize,
|
||||
data: Vec<f32>,
|
||||
}
|
||||
|
||||
impl Plane {
|
||||
#[inline]
|
||||
fn block(&self, bx: usize, by: usize, out: &mut [f32; 64]) {
|
||||
let (x0, y0) = (bx * 8, by * 8);
|
||||
if x0 + 8 <= self.w && y0 + 8 <= self.h {
|
||||
for y in 0..8 {
|
||||
let i = (y0 + y) * self.w + x0;
|
||||
out[y * 8..y * 8 + 8].copy_from_slice(&self.data[i..i + 8]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for y in 0..8 {
|
||||
let sy = (by * 8 + y).min(self.h - 1);
|
||||
let row = &self.data[sy * self.w..(sy + 1) * self.w];
|
||||
for x in 0..8 {
|
||||
out[y * 8 + x] = row[(bx * 8 + x).min(self.w - 1)];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn segment(out: &mut Vec<u8>, marker: u8, payload: &[u8]) {
|
||||
out.extend_from_slice(&[0xFF, marker]);
|
||||
out.extend_from_slice(&((payload.len() + 2) as u16).to_be_bytes());
|
||||
out.extend_from_slice(payload);
|
||||
}
|
||||
|
||||
fn dht(out: &mut Vec<u8>, class_id: u8, bits: &[u8; 16], vals: &[u8]) {
|
||||
let mut p = vec![class_id];
|
||||
p.extend_from_slice(bits);
|
||||
p.extend_from_slice(vals);
|
||||
segment(out, 0xC4, &p);
|
||||
}
|
||||
|
||||
/// Encode interleaved 8-bit samples (`channels` 1, 3 or 4; alpha is dropped), with Huffman
|
||||
/// tables made for the image.
|
||||
pub fn encode(
|
||||
data: &[u8],
|
||||
width: usize,
|
||||
height: usize,
|
||||
channels: usize,
|
||||
quality: u8,
|
||||
sub: ChromaSubsampling,
|
||||
app_segments: &[(u8, Vec<u8>)],
|
||||
) -> Vec<u8> {
|
||||
encode_with(data, width, height, channels, quality, sub, app_segments, true)
|
||||
}
|
||||
|
||||
/// [`encode`], with optimized Huffman tables or (`optimize` false) the Annex K example tables.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn encode_with(
|
||||
data: &[u8],
|
||||
width: usize,
|
||||
height: usize,
|
||||
channels: usize,
|
||||
quality: u8,
|
||||
sub: ChromaSubsampling,
|
||||
app_segments: &[(u8, Vec<u8>)],
|
||||
optimize: bool,
|
||||
) -> Vec<u8> {
|
||||
let gray = channels == 1;
|
||||
let (hs, vs) = match (gray, sub) {
|
||||
(true, _) | (false, ChromaSubsampling::S444) => (1usize, 1usize),
|
||||
(false, _) => (2, 2),
|
||||
};
|
||||
#[cfg(feature = "parallel")]
|
||||
use rayon::prelude::*;
|
||||
// colour conversion (JFIF YCbCr, full range, level-shifted) of the pixel rows `rows`, one band's worth at a
|
||||
// time (in the band's task: the planes of the whole image would be ~290 MB at 24 MP, written and read back
|
||||
// once): 4:4:4 Y, Cb, Cr at full resolution in one pass; 4:2:0 Y, and chroma (rows `crows` of the
|
||||
// half-size planes) straight from 2×2 RGB means (conversion is linear: average first, then convert once)
|
||||
let ncomp = if gray || hs == 2 { 1 } else { 3 };
|
||||
let (cw, ch) = (width.div_ceil(2), height.div_ceil(2));
|
||||
let convert = |rows: std::ops::Range<usize>, crows: std::ops::Range<usize>| -> Vec<Plane> {
|
||||
let mut full: Vec<Vec<f32>> = (0..ncomp).map(|_| vec![0f32; rows.len() * width]).collect();
|
||||
for (k, y) in rows.clone().enumerate() {
|
||||
let src = &data[y * width * channels..(y + 1) * width * channels];
|
||||
let mut out: Vec<&mut [f32]> = full.iter_mut().map(|p| &mut p[k * width..(k + 1) * width]).collect();
|
||||
if gray {
|
||||
for (o, p) in out[0].iter_mut().zip(src.chunks_exact(channels)) {
|
||||
*o = p[0] as f32 - 128.0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if out.len() == 1 {
|
||||
for (o, p) in out[0].iter_mut().zip(src.chunks_exact(channels)) {
|
||||
*o = 0.299 * p[0] as f32 + 0.587 * p[1] as f32 + 0.114 * p[2] as f32 - 128.0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
for (x, p) in src.chunks_exact(channels).enumerate() {
|
||||
let (r, g, b) = (p[0] as f32, p[1] as f32, p[2] as f32);
|
||||
out[0][x] = 0.299 * r + 0.587 * g + 0.114 * b - 128.0;
|
||||
out[1][x] = -0.168_736 * r - 0.331_264 * g + 0.5 * b;
|
||||
out[2][x] = 0.5 * r - 0.418_688 * g - 0.081_312 * b;
|
||||
}
|
||||
}
|
||||
let mut planes: Vec<Plane> = full.into_iter().map(|data| Plane { w: width, h: rows.len(), data }).collect();
|
||||
if !gray && hs == 2 {
|
||||
let (mut cb, mut cr) = (vec![0f32; cw * crows.len()], vec![0f32; cw * crows.len()]);
|
||||
for (cy, (ob, or)) in crows.clone().zip(cb.chunks_mut(cw).zip(cr.chunks_mut(cw))) {
|
||||
let (y0, y1) = (2 * cy, (2 * cy + 1).min(height - 1));
|
||||
let (r0, r1) = (&data[y0 * width * channels..], &data[y1 * width * channels..]);
|
||||
for cx in 0..cw {
|
||||
let (x0, x1) = (2 * cx * channels, (2 * cx + 1).min(width - 1) * channels);
|
||||
let s = |k: usize| (r0[x0 + k] as f32 + r0[x1 + k] as f32 + r1[x0 + k] as f32 + r1[x1 + k] as f32) * 0.25;
|
||||
let (r, g, b) = (s(0), s(1), s(2));
|
||||
ob[cx] = -0.168_736 * r - 0.331_264 * g + 0.5 * b;
|
||||
or[cx] = 0.5 * r - 0.418_688 * g - 0.081_312 * b;
|
||||
}
|
||||
}
|
||||
planes.push(Plane { w: cw, h: crows.len(), data: cb });
|
||||
planes.push(Plane { w: cw, h: crows.len(), data: cr });
|
||||
}
|
||||
planes
|
||||
};
|
||||
|
||||
let ql = scaled_quant(&Q_LUMA, quality);
|
||||
let qc = scaled_quant(&Q_CHROMA, quality);
|
||||
let (rl, rc) = (recip_table(&ql), recip_table(&qc));
|
||||
let (mcu_w, mcu_h) = (8 * hs, 8 * vs);
|
||||
let (mx, my) = (width.div_ceil(mcu_w), height.div_ceil(mcu_h));
|
||||
// restart interval: whole MCU rows per band, ≤ 65535 MCUs, enough bands to keep all cores busy
|
||||
let rows_per_band = (my / 64).clamp(1, (65_535 / mx.max(1)).max(1));
|
||||
let bands: Vec<usize> = (0..my.div_ceil(rows_per_band)).collect();
|
||||
// first pass: each band's quantized blocks as symbols, with their counts per table (DC
|
||||
// prediction restarts per band, as the restart markers say)
|
||||
let tokenize_band = |band: &usize| -> (Vec<Token>, [Freq; 4]) {
|
||||
let (mut blk, mut q) = ([0f32; 64], [0i16; 64]);
|
||||
let (mut tokens, mut freq) = (Vec::new(), [[0u64; 256]; 4]);
|
||||
let mut pred = [0i32; 3];
|
||||
let (m0, m1) = (band * rows_per_band, ((band + 1) * rows_per_band).min(my));
|
||||
// the band's planes, its MCU row `m0` at their top (blocks past the image's bottom repeat its last row,
|
||||
// which is the band's last row then)
|
||||
let planes = convert(m0 * mcu_h..(m1 * mcu_h).min(height), m0 * 8..(m1 * 8).min(ch));
|
||||
for mcu_y in m0..m1 {
|
||||
let mcu_y = mcu_y - m0;
|
||||
for mcu_x in 0..mx {
|
||||
for vy in 0..vs {
|
||||
for hx in 0..hs {
|
||||
planes[0].block(mcu_x * hs + hx, mcu_y * vs + vy, &mut blk);
|
||||
fdct_quant(&blk, &rl, &mut q);
|
||||
tokenize_block(&q, &mut pred[0], false, &mut tokens, &mut freq);
|
||||
}
|
||||
}
|
||||
if !gray {
|
||||
for (plane, pred) in planes.iter().zip(pred.iter_mut()).take(3).skip(1) {
|
||||
plane.block(mcu_x, mcu_y, &mut blk);
|
||||
fdct_quant(&blk, &rc, &mut q);
|
||||
tokenize_block(&q, pred, true, &mut tokens, &mut freq);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(tokens, freq)
|
||||
};
|
||||
#[cfg(feature = "parallel")]
|
||||
let tokenized: Vec<(Vec<Token>, [Freq; 4])> = bands.par_iter().map(tokenize_band).collect();
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
let tokenized: Vec<(Vec<Token>, [Freq; 4])> = bands.iter().map(tokenize_band).collect();
|
||||
// the tables: DC luma, AC luma, DC chroma, AC chroma
|
||||
let tables: [([u8; 16], Vec<u8>); 4] = if optimize {
|
||||
let mut total = [[0u64; 256]; 4];
|
||||
for (_, f) in &tokenized {
|
||||
for (t, f) in total.iter_mut().zip(f) {
|
||||
for (t, f) in t.iter_mut().zip(f) {
|
||||
*t = t.saturating_add(*f);
|
||||
}
|
||||
}
|
||||
}
|
||||
total.each_ref().map(optimal_table)
|
||||
} else {
|
||||
[
|
||||
(DC_LUMA_BITS, DC_VALS.to_vec()),
|
||||
(AC_LUMA_BITS, AC_LUMA_VALS.to_vec()),
|
||||
(DC_CHROMA_BITS, DC_VALS.to_vec()),
|
||||
(AC_CHROMA_BITS, AC_CHROMA_VALS.to_vec()),
|
||||
]
|
||||
};
|
||||
let huff = tables.each_ref().map(|(bits, vals)| Huff::new(bits, vals));
|
||||
// second pass: code the symbols
|
||||
#[cfg(feature = "parallel")]
|
||||
let coded: Vec<Vec<u8>> = tokenized.into_par_iter().map(|(t, _)| write_tokens(&t, &huff)).collect();
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
let coded: Vec<Vec<u8>> = tokenized.into_iter().map(|(t, _)| write_tokens(&t, &huff)).collect();
|
||||
|
||||
// headers
|
||||
let mut out = Vec::with_capacity(coded.iter().map(Vec::len).sum::<usize>() + 4096);
|
||||
out.extend_from_slice(&[0xFF, 0xD8]);
|
||||
// JFIF APP0 first (callers may pass their own, e.g. with a print density)
|
||||
if !app_segments.iter().any(|(m, _)| *m == 0xE0) {
|
||||
segment(&mut out, 0xE0, &[b'J', b'F', b'I', b'F', 0, 1, 2, 0, 0, 1, 0, 1, 0, 0]);
|
||||
}
|
||||
for (marker, payload) in app_segments {
|
||||
segment(&mut out, *marker, payload);
|
||||
}
|
||||
let dqt = |id: u8, q: &[u16; 64]| -> Vec<u8> {
|
||||
let mut p = vec![id];
|
||||
p.extend(ZIGZAG.iter().map(|&i| q[i] as u8));
|
||||
p
|
||||
};
|
||||
segment(&mut out, 0xDB, &dqt(0, &ql));
|
||||
if !gray {
|
||||
segment(&mut out, 0xDB, &dqt(1, &qc));
|
||||
}
|
||||
let mut sof = vec![8];
|
||||
sof.extend_from_slice(&(height as u16).to_be_bytes());
|
||||
sof.extend_from_slice(&(width as u16).to_be_bytes());
|
||||
if gray {
|
||||
sof.extend_from_slice(&[1, 1, 0x11, 0]);
|
||||
} else {
|
||||
sof.extend_from_slice(&[3, 1, ((hs as u8) << 4) | vs as u8, 0, 2, 0x11, 1, 3, 0x11, 1]);
|
||||
}
|
||||
segment(&mut out, 0xC0, &sof);
|
||||
let [(dlb, dlv), (alb, alv), (dcb, dcv), (acb, acv)] = &tables;
|
||||
dht(&mut out, 0x00, dlb, dlv);
|
||||
dht(&mut out, 0x10, alb, alv);
|
||||
if !gray {
|
||||
dht(&mut out, 0x01, dcb, dcv);
|
||||
dht(&mut out, 0x11, acb, acv);
|
||||
}
|
||||
let interval = (rows_per_band * mx) as u16;
|
||||
if coded.len() > 1 {
|
||||
segment(&mut out, 0xDD, &interval.to_be_bytes());
|
||||
}
|
||||
if gray {
|
||||
segment(&mut out, 0xDA, &[1, 1, 0x00, 0, 63, 0]);
|
||||
} else {
|
||||
segment(&mut out, 0xDA, &[3, 1, 0x00, 2, 0x11, 3, 0x11, 0, 63, 0]);
|
||||
}
|
||||
let last = coded.len().saturating_sub(1);
|
||||
for (i, c) in coded.iter().enumerate() {
|
||||
out.extend_from_slice(c);
|
||||
if i < last {
|
||||
out.extend_from_slice(&[0xFF, 0xD0 + (i % 8) as u8]);
|
||||
}
|
||||
}
|
||||
out.extend_from_slice(&[0xFF, 0xD9]);
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn test_rgb(w: usize, h: usize) -> Vec<u8> {
|
||||
let mut v = Vec::with_capacity(w * h * 3);
|
||||
for y in 0..h {
|
||||
for x in 0..w {
|
||||
let t = ((x as f32 / 13.0).sin() * 0.5 + 0.5) * 255.0;
|
||||
v.extend_from_slice(&[(x * 255 / w) as u8, (y * 255 / h) as u8, t as u8]);
|
||||
}
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
fn psnr(a: &[u8], b: &[u8]) -> f64 {
|
||||
let mse = a.iter().zip(b).map(|(x, y)| (*x as f64 - *y as f64).powi(2)).sum::<f64>() / a.len() as f64;
|
||||
10.0 * (255.0f64.powi(2) / mse.max(1e-9)).log10()
|
||||
}
|
||||
|
||||
/// The AAN path matches the direct DCT-II definition.
|
||||
#[test]
|
||||
fn aan_matches_reference_dct() {
|
||||
let block: [f32; 64] = std::array::from_fn(|i| ((i * 37 % 255) as f32) - 128.0);
|
||||
let q = [1u16; 64];
|
||||
let mut out = [0i16; 64];
|
||||
fdct_quant(&block, &recip_table(&q), &mut out);
|
||||
for v in 0..8 {
|
||||
for u in 0..8 {
|
||||
let c = |k: usize| if k == 0 { std::f64::consts::FRAC_1_SQRT_2 } else { 1.0 };
|
||||
let mut s = 0.0;
|
||||
for y in 0..8 {
|
||||
for x in 0..8 {
|
||||
s += block[y * 8 + x] as f64
|
||||
* (((2 * x + 1) * u) as f64 * std::f64::consts::PI / 16.0).cos()
|
||||
* (((2 * y + 1) * v) as f64 * std::f64::consts::PI / 16.0).cos();
|
||||
}
|
||||
}
|
||||
let f = 0.25 * c(u) * c(v) * s;
|
||||
assert!((out[v * 8 + u] as f64 - f).abs() <= 0.51, "({u},{v}): {} vs {f}", out[v * 8 + u]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn huffman_tables_are_consistent() {
|
||||
for (bits, n) in [(&AC_LUMA_BITS, AC_LUMA_VALS.len()), (&AC_CHROMA_BITS, AC_CHROMA_VALS.len()), (&DC_LUMA_BITS, 12), (&DC_CHROMA_BITS, 12)] {
|
||||
assert_eq!(bits.iter().map(|&b| b as usize).sum::<usize>(), n);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decodes_with_independent_decoders_at_odd_sizes() {
|
||||
for (w, h, sub) in [
|
||||
(1, 1, ChromaSubsampling::S444),
|
||||
(37, 23, ChromaSubsampling::S420),
|
||||
(640, 1031, ChromaSubsampling::S420),
|
||||
(803, 600, ChromaSubsampling::S444),
|
||||
] {
|
||||
let src = test_rgb(w, h);
|
||||
let jpg = encode(&src, w, h, 3, 92, sub, &[]);
|
||||
let mut d = jpeg_decoder::Decoder::new(&jpg[..]);
|
||||
let px = d.decode().expect("jpeg-decoder");
|
||||
let info = d.info().unwrap();
|
||||
assert_eq!((info.width as usize, info.height as usize), (w, h));
|
||||
if w * h > 64 {
|
||||
assert!(psnr(&src, &px) > 30.0, "{w}x{h} {sub:?}: psnr {}", psnr(&src, &px));
|
||||
}
|
||||
let z = zune_jpeg::JpegDecoder::new(zune_core::bytestream::ZCursor::new(&jpg[..])).decode().expect("zune-jpeg");
|
||||
assert_eq!(z.len(), px.len());
|
||||
}
|
||||
}
|
||||
|
||||
/// The code lengths of `(bits, vals)`, checked: every listed symbol once, at most 16 bits,
|
||||
/// and room left in the code space (the all-ones code is never assigned, Kraft sum < 1).
|
||||
fn check_table(bits: &[u8; 16], vals: &[u8]) {
|
||||
assert_eq!(bits.iter().map(|&b| b as usize).sum::<usize>(), vals.len());
|
||||
let mut seen = [false; 256];
|
||||
for &v in vals {
|
||||
assert!(!seen[v as usize], "symbol {v} listed twice");
|
||||
seen[v as usize] = true;
|
||||
}
|
||||
let kraft: f64 = bits.iter().enumerate().map(|(i, &n)| n as f64 / 2f64.powi(i as i32 + 1)).sum();
|
||||
assert!(kraft < 1.0, "code space full: {kraft}");
|
||||
let h = Huff::new(bits, vals);
|
||||
for &v in vals {
|
||||
let (c, l) = h.code[v as usize];
|
||||
assert!((1..=16).contains(&l));
|
||||
assert_ne!(c as u32, (1u32 << l) - 1, "symbol {v} has the all-ones code");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn optimal_tables_are_valid_and_bounded() {
|
||||
// one symbol, two, a typical spread and every symbol
|
||||
let mut one = [0u64; 256];
|
||||
one[7] = 5;
|
||||
let mut two = [0u64; 256];
|
||||
two[0] = 1;
|
||||
two[0xF0] = 1_000_000;
|
||||
let typical: Freq = std::array::from_fn(|s| if s % 3 == 0 { (s as u64 * 7919) % 1000 + 1 } else { 0 });
|
||||
let all: Freq = std::array::from_fn(|s| s as u64 + 1);
|
||||
// Fibonacci counts make an unbounded Huffman tree ~40 levels deep: the 16-bit limit applies
|
||||
let mut fib = [0u64; 256];
|
||||
let (mut a, mut b) = (1u64, 1u64);
|
||||
for f in fib.iter_mut().take(40) {
|
||||
*f = a;
|
||||
(a, b) = (b, a + b);
|
||||
}
|
||||
for (name, f) in [("one", one), ("two", two), ("typical", typical), ("all", all), ("fib", fib), ("none", [0; 256])] {
|
||||
let (bits, vals) = optimal_table(&f);
|
||||
check_table(&bits, &vals);
|
||||
for s in 0..256 {
|
||||
assert_eq!(vals.contains(&(s as u8)), f[s] > 0 || (name == "none" && s == 0), "{name}: symbol {s}");
|
||||
}
|
||||
}
|
||||
// more frequent symbols never get longer codes
|
||||
let (bits, vals) = optimal_table(&typical);
|
||||
let h = Huff::new(&bits, &vals);
|
||||
for &x in &vals {
|
||||
for &y in &vals {
|
||||
if typical[x as usize] > typical[y as usize] {
|
||||
assert!(h.code[x as usize].1 <= h.code[y as usize].1, "{x} vs {y}");
|
||||
}
|
||||
}
|
||||
}
|
||||
// the Annex K example tables pass the same checks
|
||||
check_table(&AC_LUMA_BITS, &AC_LUMA_VALS);
|
||||
check_table(&DC_CHROMA_BITS, &DC_VALS);
|
||||
}
|
||||
|
||||
/// Issue #1373: tables made for the image give a smaller file with exactly the same pixels as
|
||||
/// the Annex K example tables (the quantized coefficients are the same), at every quality and
|
||||
/// subsampling, and both decoders read it.
|
||||
#[test]
|
||||
fn optimized_tables_shrink_files_without_changing_pixels() {
|
||||
let (w, h) = (517, 333);
|
||||
// gradients plus deterministic noise, like a photo's grain
|
||||
let mut seed = 0x2545_f491_u32;
|
||||
let src: Vec<u8> = test_rgb(w, h)
|
||||
.into_iter()
|
||||
.map(|v| {
|
||||
seed ^= seed << 13;
|
||||
seed ^= seed >> 17;
|
||||
seed ^= seed << 5;
|
||||
v.saturating_add((seed % 23) as u8)
|
||||
})
|
||||
.collect();
|
||||
for (q, sub) in [(100, ChromaSubsampling::S444), (92, ChromaSubsampling::S444), (75, ChromaSubsampling::S420), (30, ChromaSubsampling::S420)]
|
||||
{
|
||||
let std = encode_with(&src, w, h, 3, q, sub, &[], false);
|
||||
let opt = encode(&src, w, h, 3, q, sub, &[]);
|
||||
assert!(opt.len() < std.len(), "q{q} {sub:?}: {} ≥ {}", opt.len(), std.len());
|
||||
let a = jpeg_decoder::Decoder::new(&std[..]).decode().expect("standard tables");
|
||||
let b = jpeg_decoder::Decoder::new(&opt[..]).decode().expect("optimized tables");
|
||||
assert_eq!(a, b, "q{q} {sub:?}: pixels differ");
|
||||
let z = zune_jpeg::JpegDecoder::new(zune_core::bytestream::ZCursor::new(&opt[..])).decode().expect("zune-jpeg");
|
||||
assert_eq!(z.len(), b.len());
|
||||
}
|
||||
// grayscale too
|
||||
let gray: Vec<u8> = src.iter().step_by(3).copied().collect();
|
||||
let std = encode_with(&gray, w, h, 1, 90, ChromaSubsampling::S444, &[], false);
|
||||
let opt = encode(&gray, w, h, 1, 90, ChromaSubsampling::S444, &[]);
|
||||
assert!(opt.len() < std.len());
|
||||
assert_eq!(jpeg_decoder::Decoder::new(&std[..]).decode().unwrap(), jpeg_decoder::Decoder::new(&opt[..]).decode().unwrap());
|
||||
}
|
||||
|
||||
/// The bytes written for 4:4:4, 4:2:0, grayscale and RGBA input at a size cut into many bands, with partial MCUs
|
||||
/// at the right and bottom edges, pinned: each band converts its own rows to YCbCr, which must not change a bit.
|
||||
#[test]
|
||||
fn encoded_bytes_are_pinned() {
|
||||
let fnv = |b: &[u8]| b.iter().fold(0xcbf2_9ce4_8422_2325u64, |h, &x| (h ^ u64::from(x)).wrapping_mul(0x100_0000_01b3));
|
||||
let (w, h) = (1203, 1037);
|
||||
let rgb = test_rgb(w, h);
|
||||
let gray: Vec<u8> = rgb.as_chunks::<3>().0.iter().map(|p| p[1]).collect();
|
||||
let rgba: Vec<u8> = rgb.as_chunks::<3>().0.iter().flat_map(|p| [p[0], p[1], p[2], 255]).collect();
|
||||
let got = [
|
||||
fnv(&encode(&rgb, w, h, 3, 90, ChromaSubsampling::S444, &[])),
|
||||
fnv(&encode(&rgb, w, h, 3, 75, ChromaSubsampling::S420, &[])),
|
||||
fnv(&encode(&gray, w, h, 1, 90, ChromaSubsampling::S444, &[])),
|
||||
fnv(&encode(&rgba, w, h, 4, 85, ChromaSubsampling::S444, &[])),
|
||||
];
|
||||
assert_eq!(got, PINNED, "{got:#x?}");
|
||||
}
|
||||
/// [`round_i16`] against `f32::round` at the halves, the ends of `i16`, NaN and infinities, and a spread of every
|
||||
/// magnitude (all 2^32 inputs were compared once, offline).
|
||||
#[test]
|
||||
fn round_i16_is_round_as_i16() {
|
||||
let special = [0.5f32, -0.5, 1.5, -1.5, 2.5, -2.5, 0.499_999_97, -0.499_999_97, 32767.5, -32768.5, 32766.5, 1e9, -1e9];
|
||||
let all = special.into_iter().chain([f32::NAN, f32::INFINITY, f32::NEG_INFINITY]).chain((0..=u32::MAX).step_by(9973).map(f32::from_bits));
|
||||
for x in all {
|
||||
assert_eq!(round_i16(x), x.round() as i16, "{x:e}");
|
||||
}
|
||||
}
|
||||
const PINNED: [u64; 4] = [0x52ba_772a_589c_1430, 0x013f_8364_821f_1dde, 0xb2a3_b5e5_aef3_7402, 0x08df_d6dc_aa2f_1c78];
|
||||
#[test]
|
||||
fn grayscale_and_quality_monotone() {
|
||||
let (w, h) = (300, 200);
|
||||
let src: Vec<u8> = (0..w * h).map(|i| ((i % w) * 255 / w) as u8 ^ ((i / w) as u8 & 0x10)).collect();
|
||||
let lo = encode(&src, w, h, 1, 30, ChromaSubsampling::S444, &[]);
|
||||
let hi = encode(&src, w, h, 1, 95, ChromaSubsampling::S444, &[]);
|
||||
assert!(lo.len() < hi.len());
|
||||
let px = jpeg_decoder::Decoder::new(&hi[..]).decode().unwrap();
|
||||
assert!(psnr(&src, &px) > 35.0);
|
||||
}
|
||||
}
|
||||
+42
-125
@@ -1,138 +1,55 @@
|
||||
//! JPEG XL decode via `jxl-oxide` (pure Rust). Enum-encoded images are rendered straight into linear
|
||||
//! Rec.2020; ICC-tagged ones are rendered in their own space and interpreted with our ICC path.
|
||||
//! XYB (lossy) images are rendered to linear sRGB primaries and converted to Rec.2020 here:
|
||||
//! jxl-oxide 0.12 desaturates colours outside sRGB when it renders XYB to wider primaries.
|
||||
//! jxl-oxide applies the codestream orientation itself, so `orientation` is reported as 1.
|
||||
//! JPEG XL adapter to the shared `craft-jxl` codec.
|
||||
|
||||
use crate::convert::{Buf, Meta, Model, Raw, check_size, finish};
|
||||
use crate::convert::{Buf, Meta, Model, Raw, finish};
|
||||
use crate::space::{NamedSpace, SourceSpace, SpaceOrigin, Trc};
|
||||
use crate::{DecodeOptions, Decoded, Error, Format, Result};
|
||||
use jxl_oxide::color::{ColourSpace, EnumColourEncoding, Primaries, RenderingIntent, TransferFunction, WhitePoint};
|
||||
use jxl_oxide::{AllocTracker, AuxBoxData, InitializeResult, JxlImage};
|
||||
|
||||
const F: Format = Format::Jxl;
|
||||
|
||||
fn err(e: impl std::fmt::Display) -> Error {
|
||||
Error::Malformed(F, e.to_string())
|
||||
}
|
||||
|
||||
/// Tracked allocation budget per allowed pixel for frames, extra channels and modular trees.
|
||||
/// This bounds allocations covered by `AllocTracker`, not every allocation made by the decoder (#1517).
|
||||
const TRACKED_BYTES_PER_PIXEL: u64 = 4 * 4 * 8;
|
||||
|
||||
/// The allocation budget never drops below this, so small images keep the decoder's fixed
|
||||
/// overhead (group buffers, entropy tables) whatever `max_pixels` is.
|
||||
const MIN_TRACKED_BYTES: u64 = 64 << 20;
|
||||
|
||||
/// Bytes handed to the decoder at a time (the size `JxlImageBuilder::read` reads in).
|
||||
const INPUT_CHUNK: usize = 4096;
|
||||
|
||||
/// Initializes from bounded input, checks dimensions before rendering, then loads remaining frames
|
||||
/// within a tracked allocation budget.
|
||||
fn open(bytes: &[u8], opts: &DecodeOptions) -> Result<JxlImage> {
|
||||
let budget = opts.max_pixels.saturating_mul(TRACKED_BYTES_PER_PIXEL).max(MIN_TRACKED_BYTES);
|
||||
let tracker = AllocTracker::with_limit(usize::try_from(budget).unwrap_or(usize::MAX));
|
||||
let mut uninit = JxlImage::builder().alloc_tracker(tracker).build_uninit();
|
||||
// Initialization can load already-buffered frame data, so small feeds limit work before
|
||||
// dimension checking.
|
||||
let mut rest = bytes;
|
||||
let mut window = INPUT_CHUNK;
|
||||
let mut img = loop {
|
||||
let consumed = uninit.feed_bytes(rest.get(..window).unwrap_or(rest)).map_err(err)?;
|
||||
rest = rest.get(consumed..).unwrap_or_default();
|
||||
match uninit.try_init().map_err(err)? {
|
||||
InitializeResult::Initialized(img) => break img,
|
||||
InitializeResult::NeedMoreData(more) => uninit = more,
|
||||
}
|
||||
if consumed == 0 {
|
||||
// the parser wants more than the window holds at once
|
||||
if window >= rest.len() {
|
||||
return Err(err("truncated JPEG XL header"));
|
||||
}
|
||||
window = window.saturating_mul(2);
|
||||
}
|
||||
};
|
||||
check_size(F, img.width() as u64, img.height() as u64, opts)?;
|
||||
// Match `JxlImageBuilder::read`: bounded feeds until the final frame, not merely the first
|
||||
// keyframe. `feed_bytes` copies codestream data into an untracked Vec, including any tail
|
||||
// after the last frame; feeding the remaining file at once bypasses the allocation budget.
|
||||
while !img.is_loading_done() && !rest.is_empty() {
|
||||
let consumed = img.feed_bytes(rest.get(..INPUT_CHUNK).unwrap_or(rest)).map_err(err)?;
|
||||
if consumed == 0 {
|
||||
break;
|
||||
}
|
||||
rest = rest.get(consumed..).unwrap_or_default();
|
||||
}
|
||||
img.finalize().map_err(err)?;
|
||||
Ok(img)
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
|
||||
let mut img = open(bytes, opts)?;
|
||||
let (w, h) = (img.width(), img.height());
|
||||
let header = img.image_header();
|
||||
let bits = header.metadata.bit_depth.bits_per_sample() as u8;
|
||||
let float = matches!(header.metadata.bit_depth, jxl_oxide::image::BitDepth::FloatSample { .. });
|
||||
let gray = header.metadata.grayscale();
|
||||
let icc = img.original_icc().map(|v| v.to_vec());
|
||||
let mut meta = Meta { orientation: Some(1), ..Default::default() };
|
||||
// linear sRGB primaries, unclamped, then our own matrix to Rec.2020 (see the module docs)
|
||||
let via_srgb = icc.is_none() && !gray && header.metadata.xyb_encoded;
|
||||
if icc.is_none() {
|
||||
img.request_color_encoding(EnumColourEncoding {
|
||||
colour_space: if gray { ColourSpace::Grey } else { ColourSpace::Rgb },
|
||||
white_point: WhitePoint::D65,
|
||||
primaries: if via_srgb { Primaries::Srgb } else { Primaries::Bt2100 },
|
||||
tf: TransferFunction::Linear,
|
||||
rendering_intent: RenderingIntent::Relative,
|
||||
});
|
||||
let mut s = SourceSpace::named(NamedSpace::Rec2020, SpaceOrigin::Container);
|
||||
s.trc = Some([Trc::Linear, Trc::Linear, Trc::Linear]);
|
||||
meta.already_linear = Some(s);
|
||||
let limits = craft_jxl::DecodeLimits { max_pixels: opts.max_pixels, ..Default::default() };
|
||||
let decoded = craft_jxl::decode_with_color(bytes, limits, craft_jxl::OutputColor::LinearRec2020).map_err(|e| match e {
|
||||
craft_jxl::Error::PixelLimitExceeded { width, height, .. } => Error::TooLarge(u64::from(width), u64::from(height)),
|
||||
craft_jxl::Error::Malformed(message) | craft_jxl::Error::LimitExceeded(message) | craft_jxl::Error::InvalidImage(message) => {
|
||||
Error::Malformed(F, message)
|
||||
}
|
||||
craft_jxl::Error::Unsupported(message) | craft_jxl::Error::Encode(message) => Error::Malformed(F, message),
|
||||
})?;
|
||||
|
||||
let gray = matches!(decoded.layout, craft_jxl::Layout::Gray | craft_jxl::Layout::GrayAlpha);
|
||||
let alpha = decoded.layout.has_alpha();
|
||||
let pixels_linear_rec2020 = decoded.pixels_linear_rec2020;
|
||||
let rendered_icc = if !pixels_linear_rec2020 && decoded.metadata.icc.is_none() && !decoded.rendered_icc.is_empty() {
|
||||
Some(decoded.rendered_icc.clone())
|
||||
} else {
|
||||
meta.icc = icc;
|
||||
}
|
||||
if let Ok(AuxBoxData::Data(ex)) = img.aux_boxes().first_exif() {
|
||||
meta.exif = Some(ex.payload().to_vec());
|
||||
}
|
||||
if let AuxBoxData::Data(x) = img.aux_boxes().first_xml() {
|
||||
meta.xmp = Some(String::from_utf8_lossy(x).trim_end_matches('\0').to_string());
|
||||
}
|
||||
let render = img.render_frame(0).map_err(err)?;
|
||||
let fb = render.image_all_channels();
|
||||
let (fw, fh, ch) = (fb.width(), fb.height(), fb.channels());
|
||||
let n_color = if gray { 1 } else { 3 };
|
||||
let has_alpha = img.pixel_format().has_alpha();
|
||||
if ch < n_color {
|
||||
return Err(err("too few channels"));
|
||||
}
|
||||
let keep = n_color + has_alpha as usize;
|
||||
let src = fb.buf();
|
||||
let mut v = Vec::with_capacity(fw * fh * keep);
|
||||
let to_2020 = lightcraft_color::SRGB.to_space(&lightcraft_color::REC2020).to_f32();
|
||||
for px in src.chunks_exact(ch) {
|
||||
let start = v.len();
|
||||
v.extend_from_slice(&px[..keep.min(ch)]);
|
||||
if keep > ch {
|
||||
v.push(1.0);
|
||||
}
|
||||
if via_srgb && let Some(rgb) = v.get_mut(start..start + 3) {
|
||||
let [r, g, b] = [rgb[0], rgb[1], rgb[2]];
|
||||
for (o, m) in rgb.iter_mut().zip(&to_2020) {
|
||||
*o = m[0] * r + m[1] * g + m[2] * b;
|
||||
}
|
||||
}
|
||||
None
|
||||
};
|
||||
let mut meta = Meta {
|
||||
orientation: Some(1),
|
||||
icc: decoded.metadata.icc.or(rendered_icc),
|
||||
exif: decoded.metadata.exif,
|
||||
xmp: decoded.metadata.xmp.map(|packet| String::from_utf8_lossy(&packet).trim_end_matches('\0').to_owned()),
|
||||
..Default::default()
|
||||
};
|
||||
if pixels_linear_rec2020 {
|
||||
let mut space = SourceSpace::named(NamedSpace::Rec2020, SpaceOrigin::Container);
|
||||
space.trc = Some([Trc::Linear, Trc::Linear, Trc::Linear]);
|
||||
meta.already_linear = Some(space);
|
||||
} else if meta.icc.is_none() {
|
||||
return Err(Error::Malformed(F, "shared decoder did not report the rendered JPEG XL color profile".into()));
|
||||
}
|
||||
let raw = Raw {
|
||||
width: fw,
|
||||
height: fh,
|
||||
width: decoded.width as usize,
|
||||
height: decoded.height as usize,
|
||||
model: if gray { Model::Gray } else { Model::Rgb },
|
||||
alpha: has_alpha,
|
||||
premultiplied: false,
|
||||
buf: Buf::F32(v),
|
||||
bit_depth: bits,
|
||||
alpha,
|
||||
premultiplied: decoded.pixels_premultiplied,
|
||||
buf: Buf::F32(decoded.pixels),
|
||||
bit_depth: decoded.bit_depth,
|
||||
};
|
||||
let mut d = finish(F, raw, meta, (w, h), opts)?;
|
||||
d.float = float;
|
||||
Ok(d)
|
||||
let (source_width, source_height) =
|
||||
if (5..=8).contains(&decoded.source_orientation) { (decoded.height, decoded.width) } else { (decoded.width, decoded.height) };
|
||||
let mut result = finish(F, raw, meta, (source_width, source_height), opts)?;
|
||||
result.float = decoded.float_samples;
|
||||
Ok(result)
|
||||
}
|
||||
+50
-139
@@ -1,21 +1,16 @@
|
||||
//! JPEG XL encode via `jixel` (pure Rust, BSD-3-Clause OR Apache-2.0; native targets with the `jxl`
|
||||
//! feature: it spawns threads of its own). Lossy files are VarDCT in XYB, tagged with an enum colour
|
||||
//! encoding the decoder renders back to; lossless files are Modular and keep their samples as they
|
||||
//! are, tagged with the ICC profile when one is given. EXIF and XMP go in `Exif` / `xml ` boxes.
|
||||
//! JPEG XL encoder adapter to the shared `craft-jxl` codec.
|
||||
|
||||
use crate::encode::{EncodeImage, EncodeMeta, HDR_REFERENCE_WHITE_NITS, Samples, pq_encode};
|
||||
use crate::encode::{EncodeImage, EncodeMeta, Samples};
|
||||
use crate::{Error, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Encoder effort: time spent searching for a smaller file at the same quality. jixel's slowest
|
||||
/// search (about 20 % smaller again, five times slower) is not offered: its files are valid, but
|
||||
/// jxl-oxide, our decoder, renders blocks of them wrongly, so LightCraft could not read back its own
|
||||
/// exports.
|
||||
/// Encoder effort exposed by LightCraft's export settings.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum JxlEffort {
|
||||
/// Plain 8×8 transforms only: quickest, largest (about a third larger than `Normal`).
|
||||
/// Quick search, producing larger files.
|
||||
Fast,
|
||||
/// Balanced search.
|
||||
#[default]
|
||||
Normal,
|
||||
}
|
||||
@@ -30,32 +25,31 @@ impl JxlEffort {
|
||||
}
|
||||
}
|
||||
|
||||
/// The colour encoding a JPEG XL file declares. Integer samples are display-encoded with its curve.
|
||||
/// The color encoding declared by the JPEG XL file.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum JxlColour {
|
||||
#[default]
|
||||
Srgb,
|
||||
/// P3 primaries, D65, sRGB curve.
|
||||
/// P3 primaries, D65, and the sRGB curve.
|
||||
DisplayP3,
|
||||
/// Rec. 2020 primaries with the BT.709 curve.
|
||||
/// Rec. 2020 primaries and the BT.709 curve.
|
||||
Rec2020,
|
||||
/// HDR: Rec. 2020 primaries with the PQ curve (SMPTE ST 2084). Float samples are linear light
|
||||
/// with SDR white at 1.0 (written with SDR white at [`HDR_REFERENCE_WHITE_NITS`]); 16-bit
|
||||
/// samples are already PQ-encoded.
|
||||
/// HDR Rec. 2020 using the PQ curve.
|
||||
Rec2020Pq,
|
||||
}
|
||||
|
||||
/// JPEG XL encoder settings.
|
||||
/// JPEG XL export settings.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct JxlOptions {
|
||||
/// 1..=100 on a libjpeg-like scale (90 ≈ visually lossless). Ignored when `lossless`.
|
||||
/// Lossy quality on a 1..=100 scale; ignored when lossless.
|
||||
pub quality: u8,
|
||||
/// Mathematically lossless (Modular) instead of lossy (VarDCT).
|
||||
/// Use mathematically lossless coding.
|
||||
pub lossless: bool,
|
||||
/// Encoder search effort.
|
||||
pub effort: JxlEffort,
|
||||
/// Lossy: two passes, a coarse image first; lossless: the Squeeze transform (a low-resolution
|
||||
/// preview that refines to the exact image).
|
||||
/// Write a progressive image.
|
||||
pub progressive: bool,
|
||||
/// Color encoding to declare.
|
||||
pub colour: JxlColour,
|
||||
}
|
||||
|
||||
@@ -65,124 +59,41 @@ impl Default for JxlOptions {
|
||||
}
|
||||
}
|
||||
|
||||
/// Encode JPEG XL from 8- or 16-bit samples (float samples only with [`JxlColour::Rec2020Pq`]).
|
||||
/// Gray is widened to RGB; alpha is kept only when some pixel is not opaque. The ICC profile in
|
||||
/// `meta` is embedded in lossless files (lossy ones carry the enum encoding of `o.colour`); `ppi`
|
||||
/// has no place in the format. Returns [`Error::Encode`] on wasm32 or without the `jxl` feature.
|
||||
pub fn encode_jxl(img: &EncodeImage, o: &JxlOptions, meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||||
img.validate()?;
|
||||
#[cfg(all(feature = "jxl", not(target_arch = "wasm32")))]
|
||||
{
|
||||
let pixels = Pixels::from(img, o.colour)?;
|
||||
// jixel asserts internally; a panic there is an export error, not a crash
|
||||
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(&pixels, img.width as usize, img.height as usize, o, meta)))
|
||||
.unwrap_or_else(|_| Err(Error::Encode("JPEG XL encoder panicked".into())))
|
||||
}
|
||||
#[cfg(not(all(feature = "jxl", not(target_arch = "wasm32"))))]
|
||||
{
|
||||
let _ = (o, meta);
|
||||
Err(Error::Encode("JPEG XL encoding is not available in this build".into()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Interleaved RGB or RGBA samples for jixel.
|
||||
#[cfg(all(feature = "jxl", not(target_arch = "wasm32")))]
|
||||
enum Pixels {
|
||||
U8 { data: Vec<u8>, alpha: bool },
|
||||
U16 { data: Vec<u16>, alpha: bool },
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "jxl", not(target_arch = "wasm32")))]
|
||||
impl Pixels {
|
||||
fn from(img: &EncodeImage, colour: JxlColour) -> Result<Self> {
|
||||
let n = (img.width as usize).checked_mul(img.height as usize).ok_or_else(|| Error::Encode("image too large".into()))?;
|
||||
let ch = img.channels as usize;
|
||||
// keep alpha only when it says something
|
||||
fn rgba<T: Copy>(s: &[T], ch: usize, n: usize, opaque: impl Fn(T) -> bool) -> (Vec<T>, bool) {
|
||||
let px = s.chunks_exact(ch).take(n);
|
||||
let alpha = (ch == 2 || ch == 4) && s.chunks_exact(ch).take(n).any(|c| c.last().is_some_and(|a| !opaque(*a)));
|
||||
let out_ch = if alpha { 4 } else { 3 };
|
||||
let mut v = Vec::with_capacity(n * out_ch);
|
||||
for c in px {
|
||||
match c {
|
||||
[g] => v.extend_from_slice(&[*g, *g, *g]),
|
||||
[g, a] => v.extend_from_slice(&[*g, *g, *g, *a][..out_ch]),
|
||||
[r, g, b] => v.extend_from_slice(&[*r, *g, *b]),
|
||||
[r, g, b, a, ..] => v.extend_from_slice(&[*r, *g, *b, *a][..out_ch]),
|
||||
[] => {}
|
||||
}
|
||||
}
|
||||
(v, alpha)
|
||||
}
|
||||
Ok(match img.samples {
|
||||
Samples::U8(s) => {
|
||||
let (data, alpha) = rgba(s, ch, n, |a| a == u8::MAX);
|
||||
Pixels::U8 { data, alpha }
|
||||
}
|
||||
Samples::U16(s) => {
|
||||
let (data, alpha) = rgba(s, ch, n, |a| a == u16::MAX);
|
||||
Pixels::U16 { data, alpha }
|
||||
}
|
||||
Samples::F32(s) if colour == JxlColour::Rec2020Pq => {
|
||||
let pq = |v: f32| (pq_encode(if v.is_finite() { v.max(0.0) } else { 0.0 } * HDR_REFERENCE_WHITE_NITS) * 65535.0).round() as u16;
|
||||
let alpha = |a: f32| (if a.is_finite() { a.clamp(0.0, 1.0) } else { 1.0 } * 65535.0).round() as u16;
|
||||
let s: Vec<u16> = s
|
||||
.chunks_exact(ch)
|
||||
.take(n)
|
||||
.flat_map(|c| match c {
|
||||
[g] => [pq(*g), pq(*g), pq(*g), u16::MAX],
|
||||
[g, a] => [pq(*g), pq(*g), pq(*g), alpha(*a)],
|
||||
[r, g, b] => [pq(*r), pq(*g), pq(*b), u16::MAX],
|
||||
[r, g, b, a, ..] => [pq(*r), pq(*g), pq(*b), alpha(*a)],
|
||||
[] => [0; 4],
|
||||
})
|
||||
.collect();
|
||||
let (data, alpha) = rgba(&s, 4, n, |a| a == u16::MAX);
|
||||
Pixels::U16 { data, alpha }
|
||||
}
|
||||
Samples::F32(_) => return Err(Error::Encode("JPEG XL float samples are written as HDR (Rec. 2020 PQ) only".into())),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "jxl", not(target_arch = "wasm32")))]
|
||||
fn run(px: &Pixels, w: usize, h: usize, o: &JxlOptions, meta: &EncodeMeta) -> Result<Vec<u8>> {
|
||||
use jixel::{ColorEncoding, EncodeConfig, Primaries, Speed, TransferFunction};
|
||||
let colour = match o.colour {
|
||||
JxlColour::Srgb => ColorEncoding::srgb(),
|
||||
JxlColour::DisplayP3 => ColorEncoding::display_p3(),
|
||||
JxlColour::Rec2020 => ColorEncoding { primaries: Primaries::Bt2020, transfer: TransferFunction::Bt709, ..ColorEncoding::srgb() },
|
||||
JxlColour::Rec2020Pq => ColorEncoding::bt2020_pq(),
|
||||
/// Encode JPEG XL while preserving LightCraft's public option and metadata types.
|
||||
pub fn encode_jxl(image: &EncodeImage, options: &JxlOptions, metadata: &EncodeMeta) -> Result<Vec<u8>> {
|
||||
image.validate()?;
|
||||
let layout = match image.channels {
|
||||
1 => craft_jxl::Layout::Gray,
|
||||
2 => craft_jxl::Layout::GrayAlpha,
|
||||
3 => craft_jxl::Layout::Rgb,
|
||||
4 => craft_jxl::Layout::Rgba,
|
||||
_ => return Err(Error::Encode(format!("unsupported channel count {}", image.channels))),
|
||||
};
|
||||
let mut c = EncodeConfig::default()
|
||||
.with_quality(f32::from(o.quality.clamp(1, 100)))
|
||||
.with_color_encoding(colour)
|
||||
.with_lossless(o.lossless)
|
||||
.with_progressive(o.progressive)
|
||||
.with_speed(match o.effort {
|
||||
JxlEffort::Fast => Speed::Fastest,
|
||||
JxlEffort::Normal => Speed::Fast,
|
||||
});
|
||||
if o.colour == JxlColour::Rec2020Pq {
|
||||
// PQ code values are absolute: 1.0 = 10 000 cd/m²
|
||||
c = c.with_intensity_target(10_000.0);
|
||||
}
|
||||
if o.lossless
|
||||
&& let Some(icc) = meta.icc
|
||||
{
|
||||
c = c.with_icc_profile(icc.to_vec());
|
||||
}
|
||||
if let Some(exif) = meta.exif {
|
||||
c = c.with_exif(exif.to_vec());
|
||||
}
|
||||
if let Some(xmp) = meta.xmp {
|
||||
c = c.with_xmp(xmp.as_bytes().to_vec());
|
||||
}
|
||||
let r = match px {
|
||||
Pixels::U8 { data, alpha: false } => jixel::encode_image(data, w, h, &c),
|
||||
Pixels::U8 { data, alpha: true } => jixel::encode_image_with_alpha(data, w, h, &c),
|
||||
Pixels::U16 { data, alpha: false } => jixel::encode_image_16bit(data, w, h, &c),
|
||||
Pixels::U16 { data, alpha: true } => jixel::encode_image_with_alpha_16bit(data, w, h, &c),
|
||||
let samples = match image.samples {
|
||||
Samples::U8(samples) => craft_jxl::Samples::U8(samples),
|
||||
Samples::U16(samples) => craft_jxl::Samples::U16(samples),
|
||||
Samples::F32(samples) => craft_jxl::Samples::F32(samples),
|
||||
};
|
||||
r.map_err(|e| Error::Encode(format!("JPEG XL: {e}")))
|
||||
let image = craft_jxl::ImageRef { width: image.width, height: image.height, layout, samples };
|
||||
let options = craft_jxl::EncodeOptions {
|
||||
quality: options.quality,
|
||||
lossless: options.lossless,
|
||||
effort: match options.effort {
|
||||
JxlEffort::Fast => craft_jxl::Effort::Fast,
|
||||
JxlEffort::Normal => craft_jxl::Effort::Normal,
|
||||
},
|
||||
progressive: options.progressive,
|
||||
colour: match options.colour {
|
||||
JxlColour::Srgb => craft_jxl::JxlColour::Srgb,
|
||||
JxlColour::DisplayP3 => craft_jxl::JxlColour::DisplayP3,
|
||||
JxlColour::Rec2020 => craft_jxl::JxlColour::Rec2020,
|
||||
JxlColour::Rec2020Pq => craft_jxl::JxlColour::Rec2020Pq,
|
||||
},
|
||||
};
|
||||
let metadata = craft_jxl::Metadata {
|
||||
icc: metadata.icc.map(<[u8]>::to_vec),
|
||||
exif: metadata.exif.map(<[u8]>::to_vec),
|
||||
xmp: metadata.xmp.map(|packet| packet.as_bytes().to_vec()),
|
||||
};
|
||||
craft_jxl::encode(&image, options, &metadata).map_err(|error| Error::Encode(error.to_string()))
|
||||
}
|
||||
@@ -26,7 +26,6 @@ pub mod gainmap;
|
||||
mod heif;
|
||||
pub mod icc;
|
||||
mod jpeg;
|
||||
pub mod jpeg_par;
|
||||
#[cfg(feature = "jxl")]
|
||||
mod jxl;
|
||||
mod jxl_encode;
|
||||
|
||||
+161
-222
@@ -1,8 +1,7 @@
|
||||
//! GIF (first frame) and BMP via the `image` crate's pure-Rust decoders.
|
||||
//! GIF through `craft-gif` and BMP through `craft-image-codecs`.
|
||||
|
||||
use crate::convert::{Buf, Meta, Model, Raw, check_size, finish};
|
||||
use crate::{DecodeOptions, Decoded, Error, Format, Result};
|
||||
use image::{DynamicImage, ImageDecoder};
|
||||
|
||||
/// Canvases up to this many pixels are still decoded by [`header`] (at most 16 MB), so a truncated
|
||||
/// small file is refused at import as before.
|
||||
@@ -13,11 +12,29 @@ const HEADER_DECODE_MAX_PIXELS: u64 = 4_000_000;
|
||||
/// header and must be one the file's bytes can fill; only a canvas small enough to be cheap is
|
||||
/// decoded (to catch truncation). Orientation is always 1: neither format carries EXIF here.
|
||||
pub(crate) fn header(bytes: &[u8], format: Format) -> Result<(u32, u32, u16)> {
|
||||
let err = |e: image::ImageError| Error::Malformed(format, e.to_string());
|
||||
let cursor = std::io::Cursor::new(bytes);
|
||||
if format == Format::Gif {
|
||||
let info = craft_gif::scan(bytes, 1).ok_or_else(|| Error::Malformed(format, "invalid GIF header".into()))?;
|
||||
let (w, h) = (u32::from(info.width), u32::from(info.height));
|
||||
check_size(format, u64::from(w), u64::from(h), &DecodeOptions::default())?;
|
||||
check_fillable(format, bytes, w, h)?;
|
||||
if craft_gif::palette(bytes).is_none() {
|
||||
return Err(Error::Malformed(format, "GIF has no readable colour table".into()));
|
||||
}
|
||||
if u64::from(w).saturating_mul(u64::from(h)) <= HEADER_DECODE_MAX_PIXELS {
|
||||
let decoded = decode(bytes, format, &DecodeOptions::fit(64, 64))?;
|
||||
return Ok((decoded.source_width, decoded.source_height, decoded.orientation));
|
||||
}
|
||||
return Ok((w, h, 1));
|
||||
}
|
||||
if format == Format::Bmp {
|
||||
let (w, h) = bmp_dims(bytes).ok_or_else(|| Error::Malformed(format, "invalid BMP header".into()))?;
|
||||
if w == 0 || h == 0 {
|
||||
return Err(Error::Malformed(format, "zero dimension".into()));
|
||||
}
|
||||
check_fillable(format, bytes, w, h)?;
|
||||
}
|
||||
let (w, h) = match format {
|
||||
Format::Gif => image::codecs::gif::GifDecoder::new(cursor).map_err(err)?.dimensions(),
|
||||
Format::Bmp => image::codecs::bmp::BmpDecoder::new(cursor).map_err(err)?.dimensions(),
|
||||
Format::Bmp => craft_image_codecs::probe_size(bytes).map_err(|e| Error::Malformed(format, e.to_string()))?,
|
||||
_ => return Err(Error::Unsupported(format, "not handled by this decoder")),
|
||||
};
|
||||
if w == 0 || h == 0 {
|
||||
@@ -31,16 +48,6 @@ pub(crate) fn header(bytes: &[u8], format: Format) -> Result<(u32, u32, u16)> {
|
||||
Ok((w, h, 1))
|
||||
}
|
||||
|
||||
/// The decoder's allocation limit: the RGBA buffer of the largest image `opts` allows. It bounds the
|
||||
/// decoders' own frame buffers only: `DynamicImage::from_decoder` allocates the canvas without
|
||||
/// consulting it, so the canvas is bounded by `check_size` and [`check_fillable`] beforehand (a
|
||||
/// mostly blank one by [`SPARSE_MAX_PIXELS`]).
|
||||
fn limits(opts: &DecodeOptions) -> image::Limits {
|
||||
let mut l = image::Limits::default();
|
||||
l.max_alloc = Some(opts.max_pixels.saturating_mul(4));
|
||||
l
|
||||
}
|
||||
|
||||
/// The largest canvas allowed that the file's pixel data leaves mostly blank: a GIF screen its
|
||||
/// first frame's codes don't cover, or an RLE BMP whose runs don't. Its decode still allocates the
|
||||
/// whole canvas (16 MB at this size), so it is held well below `max_pixels`.
|
||||
@@ -55,10 +62,9 @@ const SPARSE_MAX_PIXELS: u64 = 4_000_000;
|
||||
/// allocated, and is refused here instead.
|
||||
/// - An RLE BMP covers the pixels its runs and absolute blocks draw, as the decoder paints them; an
|
||||
/// end-of-row, end-of-bitmap or delta code leaves the rest blank.
|
||||
/// - A GIF covers the pixels its first frame's LZW codes draw up to the end code. A code expands to
|
||||
/// at most 4096 pixels and takes 12 bits once the table holds that many (fewer bits only while its
|
||||
/// strings are shorter), so a frame larger than 2731 pixels per byte of LZW data is refused
|
||||
/// outright. The screen around the frame may be larger, and is blank there.
|
||||
/// - A GIF covers the pixels its first frame's LZW codes draw up to the end code. The shared GIF
|
||||
/// reader counts code-string lengths without allocating decoded pixels; the screen around the
|
||||
/// frame may be larger, and is blank there.
|
||||
///
|
||||
/// Canvases the data leaves mostly blank are limited to [`SPARSE_MAX_PIXELS`].
|
||||
fn check_fillable(format: Format, bytes: &[u8], w: u32, h: u32) -> Result<()> {
|
||||
@@ -67,15 +73,23 @@ fn check_fillable(format: Format, bytes: &[u8], w: u32, h: u32) -> Result<()> {
|
||||
// the pixels the file's data can cover
|
||||
let covered = match format {
|
||||
Format::Gif => {
|
||||
// without a frame there is nothing to decode, and nothing to bound the canvas by
|
||||
let Some(frame) = gif_first_frame(bytes) else {
|
||||
// A frame's compressed data must cover its declared raster before it can justify the
|
||||
// logical screen. This retains the old "can hold" refusal for truncated/empty frames.
|
||||
let info = craft_gif::scan(bytes, 1).ok_or_else(|| Error::Malformed(format, "no readable first frame".into()))?;
|
||||
let Some(frame) = info.frames.first() else {
|
||||
return Err(Error::Malformed(format, "no readable first frame".into()));
|
||||
};
|
||||
let (fw, fh, len) = (frame.width, frame.height, frame.data_len);
|
||||
if area(fw, fh) > (len as u64).saturating_mul(2_731) {
|
||||
return Err(Error::Malformed(format, format!("a {fw} × {fh} frame is far larger than its {len} bytes of pixel data can hold")));
|
||||
let frame_width = u32::from(frame.width);
|
||||
let frame_height = u32::from(frame.height);
|
||||
let compressed_bytes =
|
||||
craft_gif::first_frame_compressed_bytes(bytes).ok_or_else(|| Error::Malformed(format, "no readable first frame".into()))?;
|
||||
if area(frame_width, frame_height) > (compressed_bytes as u64).saturating_mul(2_731) {
|
||||
return Err(Error::Malformed(
|
||||
format,
|
||||
format!("a {frame_width} × {frame_height} frame is far larger than its {compressed_bytes} bytes of pixel data can hold"),
|
||||
));
|
||||
}
|
||||
gif_frame_pixels(bytes, frame.codes, area(fw, fh))
|
||||
craft_gif::first_frame_decoded_pixels(bytes).unwrap_or(0)
|
||||
}
|
||||
_ => match bmp_layout(bytes) {
|
||||
BmpLayout::Rle { offset, rle4 } => bmp_rle_pixels(bytes, offset, rle4, w, h),
|
||||
@@ -177,202 +191,87 @@ fn bmp_rle_pixels(bytes: &[u8], offset: usize, rle4: bool, w: u32, h: u32) -> u6
|
||||
covered
|
||||
}
|
||||
|
||||
/// A GIF's first frame, from its image descriptor.
|
||||
struct GifFrame {
|
||||
width: u32,
|
||||
height: u32,
|
||||
/// Bytes of LZW data present (its palettes, comments and other extensions describe no pixels).
|
||||
data_len: usize,
|
||||
/// Position of its LZW minimum code size, which its data sub-blocks follow.
|
||||
codes: usize,
|
||||
}
|
||||
|
||||
/// A GIF's first frame. `None` when the blocks before the frame can't be walked.
|
||||
fn gif_first_frame(bytes: &[u8]) -> Option<GifFrame> {
|
||||
let u16_at = |i: usize| bytes.get(i..i.checked_add(2)?).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes);
|
||||
let table = |flags: u8| if flags & 0x80 != 0 { 3usize << ((flags & 7) + 1) } else { 0 };
|
||||
let mut pos = 13 + table(*bytes.get(10)?);
|
||||
loop {
|
||||
match *bytes.get(pos)? {
|
||||
// extension: introducer, label, sub-blocks
|
||||
0x21 => pos = sub_blocks(bytes, pos.saturating_add(2)).1,
|
||||
// image descriptor (separator, left, top, width, height, flags), local colour table,
|
||||
// LZW minimum code size, data sub-blocks
|
||||
0x2C => {
|
||||
let (fw, fh) = (u16_at(pos.saturating_add(5))?, u16_at(pos.saturating_add(7))?);
|
||||
let codes = pos.saturating_add(10).saturating_add(table(*bytes.get(pos.saturating_add(9))?));
|
||||
let data_len = sub_blocks(bytes, codes.saturating_add(1)).0;
|
||||
return Some(GifFrame { width: u32::from(fw), height: u32::from(fh), data_len, codes });
|
||||
}
|
||||
_ => return None,
|
||||
}
|
||||
/// Reads BMP `(width, height)` from the DIB header without allocating.
|
||||
fn bmp_dims(bytes: &[u8]) -> Option<(u32, u32)> {
|
||||
if bytes.get(0..2) != Some(b"BM") {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
/// The pixels a GIF frame's LZW codes draw, at most `max` (the frame's size): the codes are walked
|
||||
/// up to the end code, an invalid code or the end of the data, keeping only the length of each
|
||||
/// table string, never the pixels. Data after the end code draws nothing. Codes are packed
|
||||
/// least-significant bit first across the data sub-blocks and start one bit wider than the minimum
|
||||
/// code size; a code is one bit wider each time the table fills its width, up to 12 bits, and a
|
||||
/// clear code starts the table again (GIF89a, appendix F).
|
||||
fn gif_frame_pixels(bytes: &[u8], codes: usize, max: u64) -> u64 {
|
||||
// the `gif` decoder accepts minimum code sizes of 1 to 11
|
||||
let Some(min_size) = bytes.get(codes).copied().filter(|s| (1..=11).contains(s)) else {
|
||||
return 0;
|
||||
let dib = u32::from_le_bytes(bytes.get(14..18)?.try_into().ok()?);
|
||||
let (w, h) = if dib == 12 {
|
||||
(u16::from_le_bytes(bytes.get(18..20)?.try_into().ok()?) as u32, u16::from_le_bytes(bytes.get(20..22)?.try_into().ok()?) as u32)
|
||||
} else {
|
||||
let w = i32::from_le_bytes(bytes.get(18..22)?.try_into().ok()?);
|
||||
let h = i32::from_le_bytes(bytes.get(22..26)?.try_into().ok()?);
|
||||
(u32::try_from(w).ok()?, h.unsigned_abs())
|
||||
};
|
||||
let clear = 1usize << min_size;
|
||||
let end = clear + 1;
|
||||
let mut lens = [1u16; 4096]; // string length of each code; literals (below `clear`) are 1
|
||||
let (mut width, mut next, mut prev) = (u32::from(min_size) + 1, end + 1, None::<usize>);
|
||||
let (mut bits, mut held, mut covered) = (0u32, 0u32, 0u64);
|
||||
let mut pos = codes.saturating_add(1);
|
||||
while let Some(&n) = bytes.get(pos) {
|
||||
let start = pos.saturating_add(1);
|
||||
pos = start.saturating_add(usize::from(n));
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
for &byte in bytes.get(start..pos.min(bytes.len())).unwrap_or_default() {
|
||||
bits |= u32::from(byte) << held;
|
||||
held += 8;
|
||||
while held >= width {
|
||||
let code = (bits & ((1 << width) - 1)) as usize;
|
||||
bits >>= width;
|
||||
held -= width;
|
||||
if code == clear {
|
||||
(width, next, prev) = (u32::from(min_size) + 1, end + 1, None);
|
||||
continue;
|
||||
}
|
||||
let len_of = |c: usize| lens.get(c).copied().unwrap_or(1);
|
||||
let len = match prev {
|
||||
_ if code == end => return covered,
|
||||
None if code < clear => 1,
|
||||
Some(_) if code < next => len_of(code),
|
||||
// the string just being defined: the previous one and its first pixel
|
||||
Some(p) if code == next => len_of(p).saturating_add(1),
|
||||
_ => return covered,
|
||||
};
|
||||
// each code after the first defines the next string: the previous one and a pixel
|
||||
let defined = prev.map(|p| len_of(p).saturating_add(1));
|
||||
if let Some(defined) = defined
|
||||
&& let Some(slot) = lens.get_mut(next)
|
||||
{
|
||||
*slot = defined;
|
||||
next += 1;
|
||||
if next == 1 << width && width < 12 {
|
||||
width += 1;
|
||||
}
|
||||
}
|
||||
prev = Some(code);
|
||||
covered = covered.saturating_add(u64::from(len));
|
||||
if covered >= max {
|
||||
return max;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
covered
|
||||
}
|
||||
|
||||
/// The GIF sub-blocks starting at `pos`: the data bytes present, and the position after them.
|
||||
fn sub_blocks(bytes: &[u8], mut pos: usize) -> (usize, usize) {
|
||||
let mut data = 0usize;
|
||||
while let Some(&n) = bytes.get(pos) {
|
||||
let start = pos.saturating_add(1);
|
||||
if n == 0 {
|
||||
return (data, start);
|
||||
}
|
||||
let end = start.saturating_add(usize::from(n));
|
||||
data = data.saturating_add(end.min(bytes.len()).saturating_sub(start));
|
||||
pos = end;
|
||||
}
|
||||
(data, pos)
|
||||
Some((w, h))
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8], format: Format, opts: &DecodeOptions) -> Result<Decoded> {
|
||||
if format == Format::Gif {
|
||||
gated_first_frame(bytes, opts)?;
|
||||
return decode_gif(bytes, opts);
|
||||
}
|
||||
let err = |e: image::ImageError| Error::Malformed(format, e.to_string());
|
||||
let cursor = std::io::Cursor::new(bytes);
|
||||
let (img, icc) = match format {
|
||||
Format::Gif => {
|
||||
let mut d = image::codecs::gif::GifDecoder::new(cursor).map_err(err)?;
|
||||
let (w, h) = d.dimensions();
|
||||
check_size(format, w as u64, h as u64, opts)?;
|
||||
check_fillable(format, bytes, w, h)?;
|
||||
d.set_limits(limits(opts)).map_err(err)?;
|
||||
let icc = d.icc_profile().ok().flatten();
|
||||
(DynamicImage::from_decoder(d).map_err(err)?, icc)
|
||||
}
|
||||
Format::Bmp => {
|
||||
let mut d = image::codecs::bmp::BmpDecoder::new(cursor).map_err(err)?;
|
||||
let (w, h) = d.dimensions();
|
||||
check_size(format, w as u64, h as u64, opts)?;
|
||||
check_fillable(format, bytes, w, h)?;
|
||||
d.set_limits(limits(opts)).map_err(err)?;
|
||||
let icc = d.icc_profile().ok().flatten();
|
||||
(DynamicImage::from_decoder(d).map_err(err)?, icc)
|
||||
}
|
||||
_ => return Err(Error::Unsupported(format, "not handled by this decoder")),
|
||||
if format != Format::Bmp {
|
||||
return Err(Error::Unsupported(format, "not handled by this decoder"));
|
||||
}
|
||||
let (w, h) = bmp_dims(bytes).ok_or_else(|| Error::Malformed(format, "invalid BMP header".into()))?;
|
||||
check_size(format, w as u64, h as u64, opts)?;
|
||||
check_fillable(format, bytes, w, h)?;
|
||||
let (w, h) = craft_image_codecs::probe_size(bytes).map_err(|e| Error::Malformed(format, e.to_string()))?;
|
||||
let ic_opts = craft_image_codecs::DecodeOptions {
|
||||
limits: craft_image_codecs::Limits {
|
||||
max_width: u32::MAX,
|
||||
max_height: u32::MAX,
|
||||
max_pixels: opts.max_pixels,
|
||||
max_alloc: opts.max_pixels.saturating_mul(4),
|
||||
},
|
||||
keep_orientation: true,
|
||||
};
|
||||
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Bmp, bytes, &ic_opts).map_err(|e| match e {
|
||||
craft_image_codecs::CodecError::LimitExceeded(_) => Error::TooLarge(w as u64, h as u64),
|
||||
other => Error::Malformed(format, other.to_string()),
|
||||
})?;
|
||||
let (w, h) = (img.width(), img.height());
|
||||
let alpha = img.color().has_alpha();
|
||||
let gray = !img.color().has_color();
|
||||
let (model, alpha, buf) = match (gray, alpha) {
|
||||
(true, false) => (Model::Gray, false, img.into_luma8().into_raw()),
|
||||
(true, true) => (Model::Gray, true, img.into_luma_alpha8().into_raw()),
|
||||
(false, false) => (Model::Rgb, false, img.into_rgb8().into_raw()),
|
||||
(false, true) => (Model::Rgb, true, img.into_rgba8().into_raw()),
|
||||
};
|
||||
let alpha = img.layout().has_alpha();
|
||||
let model = if img.layout().is_gray() { Model::Gray } else { Model::Rgb };
|
||||
let buf = img.as_u8().ok_or_else(|| Error::Malformed(format, "expected 8-bit BMP samples".into()))?.to_vec();
|
||||
let raw = Raw { width: w as usize, height: h as usize, model, alpha, premultiplied: false, buf: Buf::U8(buf), bit_depth: 8 };
|
||||
finish(format, raw, Meta { icc, ..Default::default() }, (w, h), opts)
|
||||
finish(format, raw, Meta { icc: img.icc, ..Default::default() }, (w, h), opts)
|
||||
}
|
||||
|
||||
/// Bounds a GIF's first image descriptor: the decoder composites frames into the logical screen
|
||||
/// (bounded by [`check_size`] on the canvas) but allocates its own frame buffers at the frame's
|
||||
/// declared size, which a small file can inflate far past the canvas. The canvas check cannot
|
||||
/// see that size, so it is read from the block headers here.
|
||||
fn gated_first_frame(bytes: &[u8], opts: &DecodeOptions) -> Result<()> {
|
||||
// The constants of the header grammar: signature, logical screen descriptor (with its global
|
||||
// colour table), extension blocks (0x21, skipped by their sub-block chains) and image
|
||||
// descriptors (0x2C).
|
||||
const SCREEN: usize = 13;
|
||||
if bytes.len() < SCREEN {
|
||||
return Ok(()); // too broken for a frame; the decoder reports the damage itself
|
||||
}
|
||||
let gct_entries = 1usize << (u32::from(bytes[10] & 0x07) + 1); // at most 256
|
||||
let gct_bytes = if (bytes[10] & 0x80) != 0 { gct_entries * 3 } else { 0 };
|
||||
let mut p = SCREEN.saturating_add(gct_bytes);
|
||||
while let Some(&kind) = bytes.get(p) {
|
||||
match kind {
|
||||
0x2C => {
|
||||
// Image descriptor: two LE coordinates then two LE edges, after the separator.
|
||||
let end = p.checked_add(10).filter(|&e| e <= bytes.len());
|
||||
let Some(d) = end.and_then(|e| bytes.get(p + 1..e)) else {
|
||||
break; // cut short mid-descriptor: the decoder reports that
|
||||
};
|
||||
let (w, h) = (u16::from_le_bytes([d[4], d[5]]) as u64, u16::from_le_bytes([d[6], d[7]]) as u64);
|
||||
return check_size(Format::Gif, w, h, opts);
|
||||
}
|
||||
0x21 => {
|
||||
// Extension: a label byte, then <len,data> sub-blocks ended by a zero length.
|
||||
p = p.saturating_add(2);
|
||||
loop {
|
||||
let Some(&len) = bytes.get(p) else {
|
||||
return Ok(()); // ran past the data: decode reports the truncation
|
||||
};
|
||||
p = p.saturating_add(1);
|
||||
if len == 0 {
|
||||
break;
|
||||
}
|
||||
p = p.saturating_add(len as usize);
|
||||
}
|
||||
}
|
||||
_ => p = p.saturating_add(1),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
fn decode_gif(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
|
||||
let format = Format::Gif;
|
||||
let Some(info) = craft_gif::scan(bytes, 1) else {
|
||||
return Err(Error::Malformed(format, "invalid GIF header".into()));
|
||||
};
|
||||
let (width, height) = (u32::from(info.width), u32::from(info.height));
|
||||
check_size(format, u64::from(width), u64::from(height), opts)?;
|
||||
let Some(frame) = info.frames.first() else {
|
||||
return Err(Error::Malformed(format, "no readable first frame".into()));
|
||||
};
|
||||
check_size(format, u64::from(frame.width), u64::from(frame.height), opts)?;
|
||||
check_fillable(format, bytes, width, height)?;
|
||||
let max_bytes = opts.max_pixels.saturating_mul(4);
|
||||
let mut limits = craft_gif::frames::FrameLimits::default();
|
||||
limits.max_frames = 1;
|
||||
limits.max_bytes = max_bytes;
|
||||
limits.max_canvas_bytes = max_bytes;
|
||||
limits.max_decode_bytes = Some(max_bytes);
|
||||
let animation = craft_gif::frames::read_frames(bytes, &limits).map_err(|e| Error::Malformed(format, e.to_string()))?;
|
||||
let Some(decoded) = animation.frames.into_iter().next() else {
|
||||
return Err(Error::Malformed(format, "no decoded frame".into()));
|
||||
};
|
||||
let raw = Raw {
|
||||
width: animation.width as usize,
|
||||
height: animation.height as usize,
|
||||
model: Model::Rgb,
|
||||
alpha: true,
|
||||
premultiplied: false,
|
||||
buf: Buf::U8(decoded.rgba),
|
||||
bit_depth: 8,
|
||||
};
|
||||
let icc = craft_gif::frames::icc_profile(bytes);
|
||||
finish(format, raw, Meta { icc, ..Default::default() }, (animation.width, animation.height), opts)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -387,7 +286,7 @@ mod tests {
|
||||
b
|
||||
}
|
||||
|
||||
/// `image`'s GIF decoder allocates its frame buffer at the image descriptor's declared
|
||||
/// A GIF decoder allocates its frame buffer at the image descriptor's declared
|
||||
/// size, which the canvas bound ([`crate::decode`]'s `max_pixels` on the logical screen)
|
||||
/// does not cover. A 20-byte file used to buy a 4 GB allocation before the gate.
|
||||
#[test]
|
||||
@@ -437,17 +336,57 @@ mod tests {
|
||||
seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
|
||||
(seed >> 24) as u8
|
||||
};
|
||||
let frames = [
|
||||
("solid", image::RgbaImage::from_pixel(w, h, image::Rgba([90, 90, 90, 255]))),
|
||||
("gradient", image::RgbaImage::from_fn(w, h, |x, y| image::Rgba([(x / 40 * 16) as u8, (y / 30 * 16) as u8, 0, 255]))),
|
||||
("noise", image::RgbaImage::from_fn(w, h, |_, _| image::Rgba([noise(), noise(), noise(), 255]))),
|
||||
];
|
||||
for (name, frame) in frames {
|
||||
let mut gif = Vec::new();
|
||||
image::codecs::gif::GifEncoder::new_with_speed(&mut gif, 30).encode_frame(image::Frame::new(frame)).unwrap();
|
||||
let first = gif_first_frame(&gif).unwrap();
|
||||
assert_eq!((first.width, first.height), (w, h), "{name}");
|
||||
assert_eq!(gif_frame_pixels(&gif, first.codes, u64::MAX), u64::from(w * h), "{name}");
|
||||
let encode_gif = |rgba: Vec<u8>| -> Vec<u8> {
|
||||
let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgba, rgba).unwrap();
|
||||
craft_image_codecs::encode(&img, craft_image_codecs::Format::Gif, &craft_image_codecs::EncodeOptions::default()).unwrap()
|
||||
};
|
||||
let solid = [90, 90, 90, 255].repeat((w * h) as usize);
|
||||
let mut gradient = Vec::with_capacity((w * h * 4) as usize);
|
||||
let mut noise_buf = Vec::with_capacity((w * h * 4) as usize);
|
||||
for y in 0..h {
|
||||
for x in 0..w {
|
||||
gradient.extend_from_slice(&[((x / 40 * 16) as u8), ((y / 30 * 16) as u8), 0, 255]);
|
||||
noise_buf.extend_from_slice(&[noise(), noise(), noise(), 255]);
|
||||
}
|
||||
}
|
||||
for (name, rgba) in [("solid", solid), ("gradient", gradient), ("noise", noise_buf)] {
|
||||
let gif = encode_gif(rgba);
|
||||
let first = craft_gif::scan(&gif, 1).unwrap().frames.remove(0);
|
||||
assert_eq!((u32::from(first.width), u32::from(first.height)), (w, h), "{name}");
|
||||
assert_eq!(craft_gif::first_frame_decoded_pixels(&gif), Some(u64::from(w * h)), "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gif_icc_profile_is_preserved_by_the_codec_adapter() {
|
||||
let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgba, [80, 120, 160, 255].repeat(4)).unwrap();
|
||||
let mut bytes = craft_image_codecs::encode(&img, craft_image_codecs::Format::Gif, &craft_image_codecs::EncodeOptions::default()).unwrap();
|
||||
let frame = craft_gif::scan(&bytes, 1).unwrap().frames[0];
|
||||
let profile = [1, 2, 3, 4, 5];
|
||||
let mut extension = vec![0x21, 0xff, 0x0b];
|
||||
extension.extend_from_slice(b"ICCRGBG1012");
|
||||
extension.push(profile.len() as u8);
|
||||
extension.extend_from_slice(&profile);
|
||||
extension.push(0);
|
||||
bytes.splice(frame.offset..frame.offset, extension);
|
||||
|
||||
let decoded = crate::decode(&bytes, DecodeOptions::default()).unwrap();
|
||||
assert_eq!(decoded.icc.as_deref(), Some(profile.as_slice()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gif_first_frame_pixels_match_the_previous_image_decoder() {
|
||||
let mut pixels = Vec::with_capacity(16 * 12 * 4);
|
||||
for y in 0..12 {
|
||||
for x in 0..16 {
|
||||
pixels.extend_from_slice(&[(x * 13) as u8, (y * 17) as u8, 91, if (x + y) % 3 == 0 { 127 } else { 255 }]);
|
||||
}
|
||||
}
|
||||
let img = craft_image_codecs::Image::from_u8(16, 12, craft_image_codecs::ChannelLayout::Rgba, pixels).unwrap();
|
||||
let bytes = craft_image_codecs::encode(&img, craft_image_codecs::Format::Gif, &craft_image_codecs::EncodeOptions::default()).unwrap();
|
||||
|
||||
let decoded = craft_image_codecs::decode_as(craft_image_codecs::Format::Gif, &bytes).unwrap();
|
||||
let shared = craft_gif::frames::read_frames(&bytes, &craft_gif::frames::FrameLimits::default()).unwrap();
|
||||
assert_eq!(shared.frames[0].rgba, decoded.as_u8().unwrap());
|
||||
}
|
||||
}
|
||||
+110
-74
@@ -1,4 +1,4 @@
|
||||
//! PNG decode via the `png` crate (8/16-bit, palette, tRNS, iCCP, eXIf, iTXt XMP, sRGB/cICP/cHRM/gAMA).
|
||||
//! PNG decode via `craft-image-codecs` (8/16-bit, palette, tRNS, iCCP, eXIf, iTXt XMP, sRGB/cICP/cHRM/gAMA).
|
||||
|
||||
use crate::convert::{Buf, Meta, Model, Raw, check_size, finish};
|
||||
use crate::space::{NamedSpace, SourceSpace, SpaceOrigin, Trc};
|
||||
@@ -16,55 +16,39 @@ fn err(e: impl std::fmt::Display) -> Error {
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
|
||||
let mut dec = png::Decoder::new(std::io::Cursor::new(bytes));
|
||||
dec.set_transformations(png::Transformations::EXPAND);
|
||||
dec.set_limits(png::Limits { bytes: opts.max_pixels.saturating_mul(8).min(usize::MAX as u64) as usize });
|
||||
let mut reader = dec.read_info().map_err(err)?;
|
||||
let (w, h) = {
|
||||
let i = reader.info();
|
||||
(i.width, i.height)
|
||||
if bytes.starts_with(b"\x89PNG\r\n\x1a\n") && bytes.get(12..16) == Some(b"IHDR") && bytes.len() >= 24 {
|
||||
let w = u32::from_be_bytes([bytes[16], bytes[17], bytes[18], bytes[19]]);
|
||||
let h = u32::from_be_bytes([bytes[20], bytes[21], bytes[22], bytes[23]]);
|
||||
check_size(F, w as u64, h as u64, opts)?;
|
||||
}
|
||||
let ic_opts = craft_image_codecs::DecodeOptions {
|
||||
limits: craft_image_codecs::Limits {
|
||||
max_width: u32::MAX,
|
||||
max_height: u32::MAX,
|
||||
max_pixels: opts.max_pixels,
|
||||
max_alloc: opts.max_pixels.saturating_mul(8),
|
||||
},
|
||||
keep_orientation: true,
|
||||
};
|
||||
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, bytes, &ic_opts).map_err(|e| match e {
|
||||
craft_image_codecs::CodecError::LimitExceeded(_) => Error::TooLarge(0, 0),
|
||||
other => err(other),
|
||||
})?;
|
||||
let (w, h) = (img.width(), img.height());
|
||||
check_size(F, w as u64, h as u64, opts)?;
|
||||
let size = reader.output_buffer_size().ok_or_else(|| err("buffer size overflow"))?;
|
||||
let mut buf = vec![0u8; size];
|
||||
let out = reader.next_frame(&mut buf).map_err(err)?;
|
||||
// Text chunks after IDAT land in `info` once the stream is finished; ignore trailing errors.
|
||||
let _ = reader.finish();
|
||||
let info = reader.info();
|
||||
|
||||
let (model, alpha) = match out.color_type {
|
||||
png::ColorType::Grayscale => (Model::Gray, false),
|
||||
png::ColorType::GrayscaleAlpha => (Model::Gray, true),
|
||||
png::ColorType::Rgb => (Model::Rgb, false),
|
||||
png::ColorType::Rgba => (Model::Rgb, true),
|
||||
png::ColorType::Indexed => return Err(err("palette not expanded")),
|
||||
let model = if img.layout().is_gray() { Model::Gray } else { Model::Rgb };
|
||||
let alpha = img.layout().has_alpha();
|
||||
let (samples, depth) = match img.sample_type() {
|
||||
craft_image_codecs::SampleType::U16 => (Buf::U16(img.to_u16_samples().ok_or_else(|| err("expected 16-bit samples"))?), 16),
|
||||
_ => (Buf::U8(img.as_u8().ok_or_else(|| err("expected 8-bit samples"))?.to_vec()), 8),
|
||||
};
|
||||
buf.truncate(out.buffer_size());
|
||||
let (samples, depth) = match out.bit_depth {
|
||||
png::BitDepth::Sixteen => (Buf::U16(buf.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]])).collect()), 16),
|
||||
_ => (Buf::U8(buf), info.bit_depth as u8),
|
||||
};
|
||||
let raw = Raw { width: out.width as usize, height: out.height as usize, model, alpha, premultiplied: false, buf: samples, bit_depth: depth };
|
||||
let raw = Raw { width: w as usize, height: h as usize, model, alpha, premultiplied: false, buf: samples, bit_depth: depth };
|
||||
|
||||
// png's `get_text()` inflates a compressed chunk with no bound of its own: a tiny
|
||||
// iTXt can claim an XMP document of arbitrary size. Expand through the bounded path
|
||||
// instead, and drop any XMP bigger than the cap.
|
||||
let xmp = info
|
||||
.utf8_text
|
||||
.iter()
|
||||
.find(|t| t.keyword == "XML:com.adobe.xmp")
|
||||
.and_then(|t| {
|
||||
let mut t = t.clone();
|
||||
t.decompress_text_with_limit(MAX_TEXT_CHUNK).ok()?;
|
||||
t.get_text().ok()
|
||||
})
|
||||
.or_else(|| info.uncompressed_latin1_text.iter().find(|t| t.keyword == "XML:com.adobe.xmp").map(|t| t.text.clone()));
|
||||
let exif = info.exif_metadata.as_ref().map(|e| {
|
||||
let e = e.to_vec();
|
||||
// Some writers keep the JPEG "Exif\0\0" prefix.
|
||||
if e.starts_with(b"Exif\0\0") { e[6..].to_vec() } else { e }
|
||||
});
|
||||
let meta = Meta { icc: info.icc_profile.as_ref().map(|c| c.to_vec()), exif, xmp, hint: container_hint(info), ..Default::default() };
|
||||
let cicp_hint = img.cicp.as_ref().and_then(|c| SourceSpace::from_cicp(c.primaries, c.transfer));
|
||||
let hint = cicp_hint.or_else(|| container_hint_from_chunks(bytes));
|
||||
let xmp = img.meta.xmp.filter(|x| x.len() <= MAX_TEXT_CHUNK);
|
||||
let exif = img.meta.exif.map(|e| if e.starts_with(b"Exif\0\0") { e[6..].to_vec() } else { e });
|
||||
let meta = Meta { icc: img.icc, exif, xmp, hint, ..Default::default() };
|
||||
finish(F, raw, meta, (w, h), opts)
|
||||
}
|
||||
|
||||
@@ -137,17 +121,40 @@ pub(crate) fn header(b: &[u8]) -> Result<(u32, u32, u16)> {
|
||||
Ok((w, h, exif.map(crate::exif::summarize).unwrap_or_default().orientation.unwrap_or(1)))
|
||||
}
|
||||
|
||||
fn container_hint(info: &png::Info) -> Option<SourceSpace> {
|
||||
if let Some(s) =
|
||||
info.coding_independent_code_points.as_ref().and_then(|c| SourceSpace::from_cicp(c.color_primaries.into(), c.transfer_function.into()))
|
||||
{
|
||||
return Some(s);
|
||||
fn container_hint_from_chunks(b: &[u8]) -> Option<SourceSpace> {
|
||||
if !b.starts_with(b"\x89PNG\r\n\x1a\n") {
|
||||
return None;
|
||||
}
|
||||
if info.srgb.is_some() {
|
||||
let mut has_srgb = false;
|
||||
let mut gamma: Option<f32> = None;
|
||||
let mut chrm: Option<[f64; 8]> = None;
|
||||
let mut p = 8usize;
|
||||
while let Some(len_bytes) = b.get(p..p + 4) {
|
||||
let len = u32::from_be_bytes([len_bytes[0], len_bytes[1], len_bytes[2], len_bytes[3]]) as usize;
|
||||
let Some(kind) = b.get(p + 4..p + 8) else { break };
|
||||
let Some(payload) = b.get(p + 8..p + 8 + len) else { break };
|
||||
if kind == b"sRGB" {
|
||||
has_srgb = true;
|
||||
} else if kind == b"gAMA" && payload.len() == 4 {
|
||||
let v = u32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]) as f32 / 100_000.0;
|
||||
if v > 0.0 && v.is_finite() {
|
||||
gamma = Some(v);
|
||||
}
|
||||
} else if kind == b"cHRM" && payload.len() == 32 {
|
||||
let mut vals = [0.0f64; 8];
|
||||
for (i, dst) in vals.iter_mut().enumerate() {
|
||||
let raw = i32::from_be_bytes([payload[i * 4], payload[i * 4 + 1], payload[i * 4 + 2], payload[i * 4 + 3]]);
|
||||
*dst = raw as f64 / 100_000.0;
|
||||
}
|
||||
chrm = Some(vals);
|
||||
} else if kind == b"IDAT" || kind == b"IEND" {
|
||||
break;
|
||||
}
|
||||
p = p.saturating_add(12).saturating_add(len);
|
||||
}
|
||||
if has_srgb {
|
||||
return Some(SourceSpace::named(NamedSpace::Srgb, SpaceOrigin::Container));
|
||||
}
|
||||
let gamma = info.gama_chunk.or(info.source_gamma).map(|g| g.into_value()).filter(|g| *g > 0.0 && g.is_finite());
|
||||
let chrm = info.chrm_chunk.or(info.source_chromaticities);
|
||||
if gamma.is_none() && chrm.is_none() {
|
||||
return None;
|
||||
}
|
||||
@@ -158,8 +165,7 @@ fn container_hint(info: &png::Info) -> Option<SourceSpace> {
|
||||
};
|
||||
let mut s = SourceSpace::named(NamedSpace::Srgb, SpaceOrigin::Container);
|
||||
if let Some(c) = chrm {
|
||||
let xy = |p: (png::ScaledFloat, png::ScaledFloat)| Xy::new(p.0.into_value() as f64, p.1.into_value() as f64);
|
||||
let space = RgbSpace { name: "PNG cHRM", r: xy(c.red), g: xy(c.green), b: xy(c.blue), white: xy(c.white) };
|
||||
let space = RgbSpace { name: "PNG cHRM", white: Xy::new(c[0], c[1]), r: Xy::new(c[2], c[3]), g: Xy::new(c[4], c[5]), b: Xy::new(c[6], c[7]) };
|
||||
let valid = [space.r, space.g, space.b, space.white].iter().all(|p| p.y > 1e-4 && p.x >= 0.0 && p.x + p.y <= 1.0 + 1e-6);
|
||||
let m = if valid { rgb_to_xyz_d50_checked(&space) } else { None };
|
||||
if let Some(m) = m {
|
||||
@@ -205,29 +211,59 @@ mod tests {
|
||||
|
||||
/// A little-endian PNG with one compressed iTXt chunk carrying `payload` as XMP.
|
||||
fn png_with_compressed_xmp(payload: &str) -> Vec<u8> {
|
||||
use png::text_metadata::{EncodableTextChunk, ITXtChunk};
|
||||
let mut base = Vec::new();
|
||||
{
|
||||
let mut enc = png::Encoder::new(&mut base, 1, 1);
|
||||
enc.set_color(png::ColorType::Grayscale);
|
||||
enc.set_depth(png::BitDepth::Eight);
|
||||
let mut w = enc.write_header().unwrap();
|
||||
w.write_image_data(&[0x7F]).unwrap();
|
||||
w.finish().unwrap();
|
||||
let img = craft_image_codecs::Image::from_u8(1, 1, craft_image_codecs::ChannelLayout::Gray, vec![0x7F]).unwrap();
|
||||
let base = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
|
||||
// Build a valid zlib stream using uncompressed DEFLATE stored blocks (<= 65535 bytes each).
|
||||
let raw = payload.as_bytes();
|
||||
let mut z = vec![0x78u8, 0x01];
|
||||
let mut offset = 0usize;
|
||||
loop {
|
||||
let end = (offset + 65535).min(raw.len());
|
||||
let block = &raw[offset..end];
|
||||
let is_last = end == raw.len();
|
||||
let len = block.len() as u16;
|
||||
z.push(u8::from(is_last));
|
||||
z.extend_from_slice(&len.to_le_bytes());
|
||||
z.extend_from_slice(&(!len).to_le_bytes());
|
||||
z.extend_from_slice(block);
|
||||
offset = end;
|
||||
if is_last {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// `ITXtChunk::encode` writes the complete chunk (length, type, payload, CRC), with
|
||||
// the text compressed.
|
||||
let mut chunk = Vec::new();
|
||||
let mut xmp = ITXtChunk::new("XML:com.adobe.xmp", payload);
|
||||
xmp.compressed = true;
|
||||
xmp.compress_text().unwrap();
|
||||
xmp.encode(&mut chunk).unwrap();
|
||||
// Text chunks after IDAT land in `info` once the stream is finished: insert ours
|
||||
// before the trailing IEND (a zero-length chunk is exactly 12 bytes).
|
||||
let (mut s1, mut s2) = (1u32, 0u32);
|
||||
for &b in raw {
|
||||
s1 = (s1 + u32::from(b)) % 65521;
|
||||
s2 = (s2 + s1) % 65521;
|
||||
}
|
||||
z.extend_from_slice(&((s2 << 16) | s1).to_be_bytes());
|
||||
let mut itxt = Vec::new();
|
||||
itxt.extend_from_slice(b"XML:com.adobe.xmp\0");
|
||||
itxt.push(1); // compression_flag = 1
|
||||
itxt.push(0); // compression_method = 0
|
||||
itxt.extend_from_slice(b"\0\0"); // empty language_tag + translated_keyword
|
||||
itxt.extend_from_slice(&z);
|
||||
let chunk = png_chunk(*b"iTXt", &itxt);
|
||||
let split = base.len() - 12;
|
||||
let mut out = base[..split].to_vec();
|
||||
out.extend_from_slice(&chunk);
|
||||
out.extend_from_slice(&base[split..]);
|
||||
out
|
||||
}
|
||||
|
||||
fn png_chunk(kind: [u8; 4], data: &[u8]) -> Vec<u8> {
|
||||
let mut out = Vec::with_capacity(12 + data.len());
|
||||
out.extend_from_slice(&(data.len() as u32).to_be_bytes());
|
||||
out.extend_from_slice(&kind);
|
||||
out.extend_from_slice(data);
|
||||
let mut crc = 0xFFFF_FFFFu32;
|
||||
for &b in &out[4..] {
|
||||
crc ^= u32::from(b);
|
||||
for _ in 0..8 {
|
||||
crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 };
|
||||
}
|
||||
}
|
||||
out.extend_from_slice(&(!crc).to_be_bytes());
|
||||
out
|
||||
}
|
||||
}
|
||||
+28
-42
@@ -17,14 +17,14 @@
|
||||
//! on screen (the print is what it corrects).
|
||||
//!
|
||||
//! Everything before the profile conversion is per channel, so it folds into one 65,536-entry table
|
||||
//! indexed by the 16-bit sample; the conversion itself is moxcms's.
|
||||
//! indexed by the 16-bit sample; the conversion itself is `craft-cms`'s.
|
||||
//!
|
||||
//! Sources: ICC.1:2022 (rendering intents, profile classes); Adobe Systems, "Adobe Black Point
|
||||
//! Compensation" (2006, public white paper) for the black point mapping. Own design otherwise.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use moxcms::{ColorProfile, DataColorSpace, Layout, RenderingIntent, TransformExecutor, TransformOptions};
|
||||
use craft_cms::{ColorSpace, Intent, Profile, Transform, TransformOptions};
|
||||
|
||||
use crate::display::MAX_PROFILE_BYTES;
|
||||
use crate::proof::ProofIntent;
|
||||
@@ -64,12 +64,10 @@ const BATCH: usize = 1 << 16;
|
||||
/// says more is broken or not a print profile; leave the page alone rather than wash it out.
|
||||
const MAX_BLACK: f32 = 0.5;
|
||||
|
||||
type Executor = Arc<dyn TransformExecutor<f32> + Send + Sync>;
|
||||
|
||||
/// A page conversion, built once per print job and applied to every page.
|
||||
pub struct PrintTransform {
|
||||
forward: Executor,
|
||||
layout: Layout,
|
||||
forward: Transform,
|
||||
is_cmyk: bool,
|
||||
channels: usize,
|
||||
icc: Arc<Vec<u8>>,
|
||||
/// 16-bit sample → linear Rec.2020 after the print adjustment and black point compensation.
|
||||
@@ -88,14 +86,10 @@ impl std::fmt::Debug for PrintTransform {
|
||||
}
|
||||
}
|
||||
|
||||
fn guarded<T>(f: impl FnOnce() -> Option<T>) -> Option<T> {
|
||||
std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).ok().flatten()
|
||||
}
|
||||
|
||||
fn rendering_intent(intent: ProofIntent) -> RenderingIntent {
|
||||
fn rendering_intent(intent: ProofIntent) -> Intent {
|
||||
match intent {
|
||||
ProofIntent::Perceptual => RenderingIntent::Perceptual,
|
||||
ProofIntent::Relative => RenderingIntent::RelativeColorimetric,
|
||||
ProofIntent::Perceptual => Intent::Perceptual,
|
||||
ProofIntent::Relative => Intent::RelativeColorimetric,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,21 +121,19 @@ impl PrintTransform {
|
||||
if icc.len() > MAX_PROFILE_BYTES {
|
||||
return Err("the profile is too large".into());
|
||||
}
|
||||
let dev = guarded(|| ColorProfile::new_from_slice(&icc).ok()).ok_or("not a valid ICC profile")?;
|
||||
let (layout, channels) = match dev.color_space {
|
||||
DataColorSpace::Rgb => (Layout::Rgb, 3),
|
||||
DataColorSpace::Cmyk => (Layout::Rgba, 4),
|
||||
DataColorSpace::Gray => (Layout::Gray, 1),
|
||||
let dev = Profile::parse(&icc).map_err(|_| "not a valid ICC profile")?;
|
||||
let (is_cmyk, channels) = match dev.color_space {
|
||||
ColorSpace::Rgb => (false, 3),
|
||||
ColorSpace::Cmyk => (true, 4),
|
||||
ColorSpace::Gray => (false, 1),
|
||||
_ => return Err("only RGB, CMYK and grey printer profiles can be printed to".into()),
|
||||
};
|
||||
let lin = crate::icc::linear_rec2020_profile();
|
||||
let opts = |i| TransformOptions { rendering_intent: i, ..TransformOptions::default() };
|
||||
let forward: Executor = guarded(|| lin.create_transform_f32(Layout::Rgb, &dev, layout, opts(rendering_intent(color.intent))).ok())
|
||||
.ok_or("can't convert to this profile")?;
|
||||
let opts = |intent| TransformOptions { intent, bpc: false, precise_float: true };
|
||||
let forward = Transform::with_options(&lin, &dev, opts(rendering_intent(color.intent))).map_err(|_| "can't convert to this profile")?;
|
||||
|
||||
let black = if color.black_point_compensation && color.intent == ProofIntent::Relative {
|
||||
let back: Executor = guarded(|| dev.create_transform_f32(layout, &lin, Layout::Rgb, opts(RenderingIntent::RelativeColorimetric)).ok())
|
||||
.ok_or("can't convert from this profile")?;
|
||||
let back = Transform::with_options(&dev, &lin, opts(Intent::RelativeColorimetric)).map_err(|_| "can't convert from this profile")?;
|
||||
device_black(&forward, &back, channels)
|
||||
} else {
|
||||
0.0
|
||||
@@ -163,7 +155,7 @@ impl PrintTransform {
|
||||
})
|
||||
.collect();
|
||||
let description = crate::icc::parse(&icc).and_then(|i| i.description);
|
||||
Ok(PrintTransform { forward, layout, channels, icc, pre, black, description })
|
||||
Ok(PrintTransform { forward, is_cmyk, channels, icc, pre, black, description })
|
||||
}
|
||||
|
||||
/// Samples per pixel of the converted page: 3 (RGB), 4 (CMYK) or 1 (grey).
|
||||
@@ -179,7 +171,7 @@ impl PrintTransform {
|
||||
/// Whether the samples are CMYK (PDF readers show CMYK images inverted unless told otherwise,
|
||||
/// so writers need to know).
|
||||
pub fn is_cmyk(&self) -> bool {
|
||||
self.layout == Layout::Rgba
|
||||
self.is_cmyk
|
||||
}
|
||||
|
||||
/// The linear black black point compensation lifts the page's black to (0 when off).
|
||||
@@ -205,7 +197,7 @@ impl PrintTransform {
|
||||
let run = |src: &[u16], dst: &mut [u16]| -> Option<()> {
|
||||
let lin: Vec<f32> = src.iter().map(|&v| self.pre.get(usize::from(v)).copied().unwrap_or(0.0)).collect();
|
||||
let mut dev = vec![0f32; dst.len()];
|
||||
self.forward.transform(&lin, &mut dev).ok()?;
|
||||
self.forward.convert_f32(&lin, 3, &mut dev, ch, false).ok()?;
|
||||
for (d, v) in dst.iter_mut().zip(dev) {
|
||||
let v = if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 };
|
||||
*d = (v * 65535.0 + 0.5) as u16;
|
||||
@@ -218,24 +210,20 @@ impl PrintTransform {
|
||||
let ok = {
|
||||
use rayon::prelude::*;
|
||||
let pairs: Vec<_> = src.zip(dst).collect();
|
||||
pairs.into_par_iter().all(|(s, d)| guarded(|| run(s, d)).is_some())
|
||||
pairs.into_par_iter().all(|(s, d)| run(s, d).is_some())
|
||||
};
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
let ok = src.zip(dst).all(|(s, d)| guarded(|| run(s, d)).is_some());
|
||||
let ok = src.zip(dst).all(|(s, d)| run(s, d).is_some());
|
||||
if ok { Ok(out) } else { Err("the profile's tables can't be applied".into()) }
|
||||
}
|
||||
}
|
||||
|
||||
/// The darkest the device holds, as linear Rec.2020 luminance: black there and back, relative
|
||||
/// colorimetric both ways (for a device that reaches true black, 0).
|
||||
fn device_black(forward: &Executor, back: &Executor, channels: usize) -> f32 {
|
||||
fn device_black(forward: &Transform, back: &Transform, channels: usize) -> f32 {
|
||||
let mut device = vec![0f32; channels];
|
||||
let mut lin = [0f32; 3];
|
||||
let ok = guarded(|| {
|
||||
forward.transform(&[0.0; 3], &mut device).ok()?;
|
||||
back.transform(&device, &mut lin).ok()
|
||||
});
|
||||
if ok.is_none() {
|
||||
if forward.convert_f32(&[0.0; 3], 3, &mut device, channels, false).is_err() || back.convert_f32(&device, channels, &mut lin, 3, false).is_err() {
|
||||
return 0.0;
|
||||
}
|
||||
// BT.2020 luminance weights
|
||||
@@ -246,7 +234,6 @@ fn device_black(forward: &Executor, back: &Executor, channels: usize) -> f32 {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use moxcms::ToneReprCurve;
|
||||
|
||||
/// A page of one 16-bit pixel per linear grey level.
|
||||
fn greys(levels: &[f32]) -> Vec<u16> {
|
||||
@@ -259,15 +246,14 @@ mod tests {
|
||||
|
||||
/// An RGB print profile whose ink can't get darker than linear 0.03: device 0 is 3 % grey.
|
||||
fn raised_black_profile() -> Arc<Vec<u8>> {
|
||||
let mut p = ColorProfile::new_from_slice(&crate::icc::write_named(NamedSpace::Srgb)).unwrap();
|
||||
let (g, e) = (2.2f32, 0.03f32);
|
||||
use craft_cms::Curve;
|
||||
let mut p = Profile::parse(&crate::icc::write_named(NamedSpace::Srgb)).unwrap();
|
||||
let (g, e) = (2.2f64, 0.03f64);
|
||||
let a = (1.0 - e).powf(1.0 / g);
|
||||
// ICC parametric type 4: Y = (aX + b)^g + e for X >= d, Y = cX + f below
|
||||
let trc = ToneReprCurve::Parametric(vec![g, a, 0.0, 0.0, 0.0, e, e]);
|
||||
p.red_trc = Some(trc.clone());
|
||||
p.green_trc = Some(trc.clone());
|
||||
p.blue_trc = Some(trc);
|
||||
Arc::new(p.encode().unwrap())
|
||||
let trc = Curve::Parametric { kind: 4, p: [g, a, 0.0, 0.0, 0.0, e, e] };
|
||||
p.trc = Some([trc.clone(), trc.clone(), trc]);
|
||||
Arc::new(p.with_encoded_bytes().to_bytes().to_vec())
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+25
-29
@@ -12,10 +12,10 @@
|
||||
//! - Each entry carries a gamut flag: how far the relative-colorimetric round trip moved the colour.
|
||||
//!
|
||||
//! Sources: ICC.1:2022 (rendering intents, media white point, absolute colorimetric scaling);
|
||||
//! the transforms are moxcms's. Own design otherwise.
|
||||
//! the transforms are `craft-cms`'s. Own design otherwise.
|
||||
|
||||
use craft_cms::{ColorSpace, Intent, Profile, Transform, TransformOptions};
|
||||
use lightcraft_color::{D50, D65, REC2020, bradford};
|
||||
use moxcms::{ColorProfile, DataColorSpace, Layout, RenderingIntent, TransformOptions};
|
||||
|
||||
use crate::display::MAX_PROFILE_BYTES;
|
||||
|
||||
@@ -57,47 +57,43 @@ pub struct ProofTable {
|
||||
/// Grid size of [`ProofTable`]s.
|
||||
pub const PROOF_GRID: usize = 33;
|
||||
|
||||
fn guarded<T>(f: impl FnOnce() -> Option<T>) -> Option<T> {
|
||||
std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).ok().flatten()
|
||||
}
|
||||
|
||||
/// Build the proof table for an output profile. Errors say what's wrong with the file; nothing here
|
||||
/// panics on a hostile profile.
|
||||
pub fn proof_table(bytes: &[u8], intent: ProofIntent, paper_ink: bool) -> Result<ProofTable, String> {
|
||||
if bytes.len() > MAX_PROFILE_BYTES {
|
||||
return Err("the profile is too large".into());
|
||||
}
|
||||
let dev = guarded(|| ColorProfile::new_from_slice(bytes).ok()).ok_or("not a valid ICC profile")?;
|
||||
let (layout, ch) = match dev.color_space {
|
||||
DataColorSpace::Rgb => (Layout::Rgb, 3),
|
||||
DataColorSpace::Cmyk => (Layout::Rgba, 4),
|
||||
DataColorSpace::Gray => (Layout::Gray, 1),
|
||||
let dev = Profile::parse(bytes).map_err(|_| "not a valid ICC profile")?;
|
||||
let ch = match dev.color_space {
|
||||
ColorSpace::Rgb => 3,
|
||||
ColorSpace::Cmyk => 4,
|
||||
ColorSpace::Gray => 1,
|
||||
_ => return Err("only RGB, CMYK and grey output profiles can be proofed".into()),
|
||||
};
|
||||
let lin = crate::icc::linear_rec2020_profile();
|
||||
let opts = |i| TransformOptions { rendering_intent: i, ..TransformOptions::default() };
|
||||
let opts = |intent| TransformOptions { intent, bpc: false, precise_float: false };
|
||||
let fwd_i = match intent {
|
||||
ProofIntent::Perceptual => RenderingIntent::Perceptual,
|
||||
ProofIntent::Relative => RenderingIntent::RelativeColorimetric,
|
||||
ProofIntent::Perceptual => Intent::Perceptual,
|
||||
ProofIntent::Relative => Intent::RelativeColorimetric,
|
||||
};
|
||||
let rel = RenderingIntent::RelativeColorimetric;
|
||||
let fwd = guarded(|| lin.create_transform_f32(Layout::Rgb, &dev, layout, opts(fwd_i)).ok()).ok_or("can't convert to this profile")?;
|
||||
let fwd_rel = guarded(|| lin.create_transform_f32(Layout::Rgb, &dev, layout, opts(rel)).ok()).ok_or("can't convert to this profile")?;
|
||||
let back = guarded(|| dev.create_transform_f32(layout, &lin, Layout::Rgb, opts(rel)).ok()).ok_or("can't convert from this profile")?;
|
||||
let rel = Intent::RelativeColorimetric;
|
||||
let fwd = Transform::with_options(&lin, &dev, opts(fwd_i)).map_err(|_| "can't convert to this profile")?;
|
||||
let fwd_rel = Transform::with_options(&lin, &dev, opts(rel)).map_err(|_| "can't convert to this profile")?;
|
||||
let back = Transform::with_options(&dev, &lin, opts(rel)).map_err(|_| "can't convert from this profile")?;
|
||||
|
||||
let n = PROOF_GRID;
|
||||
let grid: Vec<f32> = (0..n * n * n)
|
||||
.flat_map(|i| [i / (n * n), (i / n) % n, i % n].map(|k| lightcraft_color::transfer::srgb_to_linear(k as f32 / (n - 1) as f32)))
|
||||
.collect();
|
||||
let round_trip = |there: &dyn moxcms::TransformExecutor<f32>| -> Option<Vec<f32>> {
|
||||
let round_trip = |there: &Transform| -> Option<Vec<f32>> {
|
||||
let mut device = vec![0f32; n * n * n * ch];
|
||||
there.transform(&grid, &mut device).ok()?;
|
||||
there.convert_f32(&grid, 3, &mut device, ch, false).ok()?;
|
||||
let mut out = vec![0f32; n * n * n * 3];
|
||||
back.transform(&device, &mut out).ok()?;
|
||||
back.convert_f32(&device, ch, &mut out, 3, false).ok()?;
|
||||
Some(out)
|
||||
};
|
||||
let sim = guarded(|| round_trip(fwd.as_ref())).ok_or("the profile's tables can't be applied")?;
|
||||
let colorimetric = guarded(|| round_trip(fwd_rel.as_ref())).ok_or("the profile's tables can't be applied")?;
|
||||
let sim = round_trip(&fwd).ok_or("the profile's tables can't be applied")?;
|
||||
let colorimetric = round_trip(&fwd_rel).ok_or("the profile's tables can't be applied")?;
|
||||
let paper = if paper_ink { paper_white(&dev) } else { [1.0; 3] };
|
||||
let enc = |v: f32| lightcraft_color::transfer::linear_to_srgb(v.clamp(0.0, 1.0));
|
||||
let data = (0..n * n * n)
|
||||
@@ -112,13 +108,13 @@ pub fn proof_table(bytes: &[u8], intent: ProofIntent, paper_ink: bool) -> Result
|
||||
}
|
||||
|
||||
/// The paper's white in linear Rec.2020 (screen white = (1, 1, 1)), from the media white point.
|
||||
fn paper_white(p: &ColorProfile) -> [f32; 3] {
|
||||
let Some(w) = p.media_white_point else { return [1.0; 3] };
|
||||
fn paper_white(p: &Profile) -> [f32; 3] {
|
||||
let w = p.white_point;
|
||||
let to_xyz = bradford(D65, D50).mul(&REC2020.to_xyz());
|
||||
let Some(from_xyz) = to_xyz.inverse() else { return [1.0; 3] };
|
||||
// the PCS white is D50 = Rec.2020 (1, 1, 1) after adaptation; the paper is relative to it
|
||||
let d50 = to_xyz.apply([1.0; 3]);
|
||||
let s = [w.x / d50[0], w.y / d50[1], w.z / d50[2]];
|
||||
let s = [w[0] / d50[0], w[1] / d50[1], w[2] / d50[2]];
|
||||
let rgb = from_xyz.apply([d50[0] * s[0], d50[1] * s[1], d50[2] * s[2]]);
|
||||
if rgb.iter().all(|v| v.is_finite() && *v > 0.05 && *v <= 1.5) { rgb.map(|v| (v as f32).min(1.0)) } else { [1.0; 3] }
|
||||
}
|
||||
@@ -148,10 +144,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn paper_white_dims_and_tints_white() {
|
||||
let mut p = ColorProfile::new_from_slice(&crate::icc::write_named(NamedSpace::AdobeRgb)).unwrap();
|
||||
let mut p = Profile::parse(&crate::icc::write_named(NamedSpace::AdobeRgb)).unwrap();
|
||||
// a warm, slightly dull paper
|
||||
p.media_white_point = Some(moxcms::Xyzd { x: 0.9642 * 0.92, y: 0.90, z: 0.8249 * 0.80 });
|
||||
let bytes = p.encode().unwrap();
|
||||
p.white_point = [0.9642 * 0.92, 0.90, 0.8249 * 0.80];
|
||||
let bytes = p.with_encoded_bytes().to_bytes().to_vec();
|
||||
let top = PROOF_GRID - 1;
|
||||
let plain = proof_table(&bytes, ProofIntent::Perceptual, false).unwrap();
|
||||
let paper = proof_table(&bytes, ProofIntent::Perceptual, true).unwrap();
|
||||
|
||||
+65
-80
@@ -40,36 +40,46 @@ impl<'a> Rd<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The header, colour mode data and image resources (`craft-psd`), with a reader at the layer and mask section.
|
||||
struct Start<'a> {
|
||||
psb: bool,
|
||||
channels: usize,
|
||||
width: usize,
|
||||
height: usize,
|
||||
depth: u16,
|
||||
mode: u16,
|
||||
head: craft_psd::Head,
|
||||
r: Rd<'a>,
|
||||
}
|
||||
|
||||
fn start<'a>(bytes: &'a [u8], opts: &DecodeOptions) -> Result<Start<'a>> {
|
||||
// the size limit first, from the header's fixed fields, so an oversized file is `TooLarge` as for other formats
|
||||
if let (Some(&[h0, h1, h2, h3]), Some(&[w0, w1, w2, w3])) = (bytes.get(14..18), bytes.get(18..22)) {
|
||||
check_size(F, u64::from(u32::from_be_bytes([w0, w1, w2, w3])), u64::from(u32::from_be_bytes([h0, h1, h2, h3])), opts)?;
|
||||
}
|
||||
let head = craft_psd::Head::from_bytes(bytes).map_err(|e| match e {
|
||||
craft_psd::PsdError::UnexpectedEof { .. } => err("truncated"),
|
||||
e => err(&e.to_string()),
|
||||
})?;
|
||||
let h = &head.header;
|
||||
check_size(F, u64::from(h.width), u64::from(h.height), opts)?;
|
||||
let p = usize::try_from(head.layer_section_offset).map_err(|_| err("truncated"))?;
|
||||
Ok(Start {
|
||||
psb: h.version.is_psb(),
|
||||
channels: usize::from(h.channels),
|
||||
width: h.width as usize,
|
||||
height: h.height as usize,
|
||||
depth: h.depth,
|
||||
mode: h.color_mode.as_u16(),
|
||||
r: Rd { b: bytes, p },
|
||||
head,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
|
||||
let mut r = Rd { b: bytes, p: 0 };
|
||||
if r.take(4)? != b"8BPS" {
|
||||
return Err(err("bad signature"));
|
||||
}
|
||||
let version = r.u16()?;
|
||||
let psb = match version {
|
||||
1 => false,
|
||||
2 => true,
|
||||
_ => return Err(err("bad version")),
|
||||
};
|
||||
r.take(6)?;
|
||||
let channels = r.u16()? as usize;
|
||||
let height = r.u32()? as usize;
|
||||
let width = r.u32()? as usize;
|
||||
let depth = r.u16()?;
|
||||
let mode = r.u16()?;
|
||||
check_size(F, width as u64, height as u64, opts)?;
|
||||
if channels == 0 || channels > 56 {
|
||||
return Err(err("bad channel count"));
|
||||
}
|
||||
|
||||
// Colour mode data (palette for indexed).
|
||||
let cm_len = r.u32()? as usize;
|
||||
let color_mode_data = r.take(cm_len)?;
|
||||
|
||||
// Image resources.
|
||||
let res_len = r.u32()? as usize;
|
||||
let res = r.take(res_len)?;
|
||||
let (icc, exif, xmp) = resources(res);
|
||||
let Start { psb, channels, width, height, depth, mode, head, mut r } = start(bytes, opts)?;
|
||||
let color_mode_data = head.color_mode_data.as_slice();
|
||||
let (icc, exif, xmp) = resources(&head);
|
||||
|
||||
// Layer and mask information: only the layer count sign matters (merged transparency).
|
||||
let lm_len = if psb { r.u64()? } else { u64::from(r.u32()?) };
|
||||
@@ -342,29 +352,9 @@ fn packbits(src: &[u8], out: &mut Vec<u8>) {
|
||||
/// depth, colour mode or compression, or merged image data that runs past the end of the file
|
||||
/// (truncated). The run-length data itself is not unpacked.
|
||||
pub(crate) fn header(bytes: &[u8]) -> Result<(u32, u32, u16)> {
|
||||
let mut r = Rd { b: bytes, p: 0 };
|
||||
if r.take(4)? != b"8BPS" {
|
||||
return Err(err("bad signature"));
|
||||
}
|
||||
let psb = match r.u16()? {
|
||||
1 => false,
|
||||
2 => true,
|
||||
_ => return Err(err("bad version")),
|
||||
};
|
||||
r.take(6)?;
|
||||
let channels = r.u16()? as usize;
|
||||
let height = r.u32()? as usize;
|
||||
let width = r.u32()? as usize;
|
||||
let depth = r.u16()?;
|
||||
let mode = r.u16()?;
|
||||
check_size(F, width as u64, height as u64, &DecodeOptions::default())?;
|
||||
if channels == 0 || channels > 56 {
|
||||
return Err(err("bad channel count"));
|
||||
}
|
||||
let cm_len = r.u32()? as usize;
|
||||
let color_mode_data = r.take(cm_len)?;
|
||||
let res_len = r.u32()? as usize;
|
||||
let (_, exif, _) = resources(r.take(res_len)?);
|
||||
let Start { psb, channels, width, height, depth, mode, head, mut r } = start(bytes, &DecodeOptions::default())?;
|
||||
let color_mode_data = head.color_mode_data.as_slice();
|
||||
let (_, exif, _) = resources(&head);
|
||||
let lm_len = if psb { r.u64()? as usize } else { r.u32()? as usize };
|
||||
r.take(lm_len)?;
|
||||
let compression = r.u16()?;
|
||||
@@ -444,28 +434,12 @@ fn unpackbits(mut src: &[u8], row: &mut [u8]) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn resources(mut b: &[u8]) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<String>) {
|
||||
let (mut icc, mut exif, mut xmp) = (None, None, None);
|
||||
while b.len() >= 12 && &b[0..4] == b"8BIM" {
|
||||
let id = u16::from_be_bytes([b[4], b[5]]);
|
||||
let name_len = b[6] as usize;
|
||||
let name_total = (1 + name_len + 1) & !1;
|
||||
let p = 6 + name_total;
|
||||
if p + 4 > b.len() {
|
||||
break;
|
||||
}
|
||||
let size = u32::from_be_bytes([b[p], b[p + 1], b[p + 2], b[p + 3]]) as usize;
|
||||
let start = p + 4;
|
||||
let Some(data) = start.checked_add(size).and_then(|end| b.get(start..end)) else { break };
|
||||
match id {
|
||||
1039 => icc = Some(data.to_vec()),
|
||||
1058 => exif = Some(if data.starts_with(b"Exif\0\0") { data[6..].to_vec() } else { data.to_vec() }),
|
||||
1060 => xmp = Some(String::from_utf8_lossy(data).trim_end_matches('\0').to_string()),
|
||||
_ => {}
|
||||
}
|
||||
let next = start + ((size + 1) & !1);
|
||||
b = b.get(next..).unwrap_or(&[]);
|
||||
}
|
||||
/// ICC (1039), EXIF (1058, without its optional `Exif\0\0` prefix) and XMP (1060) resources; the last of each id.
|
||||
fn resources(head: &craft_psd::Head) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<String>) {
|
||||
let last = |id: u16| head.resources.iter().rev().find(|r| r.id == id).map(|r| r.data.as_slice());
|
||||
let icc = last(1039).map(<[u8]>::to_vec);
|
||||
let exif = last(1058).map(|d| d.strip_prefix(b"Exif\0\0").unwrap_or(d).to_vec());
|
||||
let xmp = last(1060).map(|d| String::from_utf8_lossy(d).trim_end_matches('\0').to_string());
|
||||
(icc, exif, xmp)
|
||||
}
|
||||
|
||||
@@ -514,8 +488,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn huge_rle_with_truncated_count_table_is_rejected_before_allocating() {
|
||||
// 1 × 2^30 px, 56 channels: the row table alone would be 56 Gi entries; the file has 4 bytes.
|
||||
let mut b = header(56, 1, 1 << 30, 32, 3, 1);
|
||||
// 1 × 300 000 px, 56 channels: the row table alone would be 16.8 M entries; the file has 4 bytes.
|
||||
let mut b = header(56, 1, 300_000, 32, 3, 1);
|
||||
b.extend_from_slice(&[0, 0, 0, 0]);
|
||||
let e = decode(&b, &DecodeOptions::default()).unwrap_err();
|
||||
assert!(matches!(e, Error::Malformed(_, ref m) if m == "truncated"), "{e:?}");
|
||||
@@ -523,18 +497,29 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn huge_rle_of_empty_rows_is_rejected_before_allocating() {
|
||||
// 2^30 × 1 px RGB at 32 bits: every row count is 0, which used to zero-pad 3 × 4 GiB.
|
||||
let mut b = header(3, 1 << 30, 1, 32, 3, 1);
|
||||
// 300 000 × 1 px RGB at 32 bits: every row count is 0, which used to zero-pad 3 × 1.2 MB.
|
||||
let mut b = header(3, 300_000, 1, 32, 3, 1);
|
||||
b.extend_from_slice(&[0; 6]);
|
||||
let e = decode(&b, &DecodeOptions::default()).unwrap_err();
|
||||
assert!(matches!(e, Error::Malformed(_, ref m) if m.contains("too short")), "{e:?}");
|
||||
// 1 Gpx RGB 8-bit whose (plausible) row counts claim more data than the file holds.
|
||||
let mut b = header(3, 1 << 20, 1 << 10, 8, 3, 1);
|
||||
// 0.27 Gpx RGB 8-bit whose (plausible) row counts claim more data than the file holds.
|
||||
let mut b = header(3, 1 << 18, 1 << 10, 8, 3, 1);
|
||||
b.extend_from_slice(&[0xFF; 3 * 1024 * 2]);
|
||||
let e = decode(&b, &DecodeOptions::default()).unwrap_err();
|
||||
assert!(matches!(e, Error::Malformed(_, ref m) if m == "truncated"), "{e:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_damaged_resource_section_is_malformed() {
|
||||
// a resource section of 4 bytes that is not an `8BIM` record
|
||||
let mut b = header(3, 1, 1, 8, 3, 0);
|
||||
b[30..34].copy_from_slice(&4u32.to_be_bytes());
|
||||
b.splice(34..34, *b"JUNK");
|
||||
let e = decode(&b, &DecodeOptions::default()).unwrap_err();
|
||||
assert!(matches!(e, Error::Malformed(_, ref m) if m.contains("signature")), "{e:?}");
|
||||
assert!(super::header(&b).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rle_repeat_run_past_row_width_is_an_error() {
|
||||
// 4 × 1 gray 8-bit; the single row is a 128-byte repeat run.
|
||||
|
||||
+69
-219
@@ -1,12 +1,7 @@
|
||||
//! TIFF decode via the `tiff` crate: 1/2/4/8/16/32/64-bit integer, 16/32/64-bit float, gray/RGB/CMYK,
|
||||
//! alpha (associated or not), ICC (34675), XMP (700), orientation (274). First image only. Palette
|
||||
//! (indexed-colour) images are refused by the `tiff` crate itself, on every read path.
|
||||
//! TIFF decode via `craft-image-codecs` and header/IFD inspection via `craft-tiff`.
|
||||
|
||||
use crate::convert::{Buf, Meta, Model, Raw, check_size, finish};
|
||||
use crate::{DecodeOptions, Decoded, Error, Format, Result};
|
||||
use tiff::ColorType;
|
||||
use tiff::decoder::{Decoder, DecodingResult, Limits};
|
||||
use tiff::tags::Tag;
|
||||
|
||||
const F: Format = Format::Tiff;
|
||||
|
||||
@@ -15,233 +10,88 @@ fn err(e: impl std::fmt::Display) -> Error {
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
|
||||
let mut d = Decoder::new(std::io::Cursor::new(bytes)).map_err(err)?.with_limits(limits(opts));
|
||||
let (w, h) = d.dimensions().map_err(err)?;
|
||||
let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(err)?;
|
||||
if !complete {
|
||||
return Err(err("truncated TIFF"));
|
||||
}
|
||||
let ifd = tiff.ifds.first().ok_or_else(|| err("no IFD"))?;
|
||||
let info = ifd.image().map_err(err)?;
|
||||
check_size(F, info.width as u64, info.height as u64, opts)?;
|
||||
let orientation = ifd.u16(craft_tiff::tags::ORIENTATION);
|
||||
let bit_depth = info.bits_per_sample.first().copied().unwrap_or(8) as u8;
|
||||
|
||||
let ic_opts = craft_image_codecs::DecodeOptions {
|
||||
limits: craft_image_codecs::Limits {
|
||||
max_width: u32::MAX,
|
||||
max_height: u32::MAX,
|
||||
max_pixels: opts.max_pixels,
|
||||
max_alloc: opts.max_pixels.saturating_mul(32),
|
||||
},
|
||||
keep_orientation: true,
|
||||
};
|
||||
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Tiff, bytes, &ic_opts).map_err(|e| match e {
|
||||
craft_image_codecs::CodecError::LimitExceeded(_) => Error::TooLarge(info.width as u64, info.height as u64),
|
||||
craft_image_codecs::CodecError::Unsupported { .. } => Error::Unsupported(F, "unsupported TIFF"),
|
||||
other => err(other),
|
||||
})?;
|
||||
if img.warnings.iter().any(|w| matches!(w, craft_image_codecs::DecodeWarning::Truncated { .. })) {
|
||||
return Err(err("truncated: image data past the end of the file"));
|
||||
}
|
||||
|
||||
let (w, h) = (img.width(), img.height());
|
||||
check_size(F, w as u64, h as u64, opts)?;
|
||||
let ct = d.colortype().map_err(err)?;
|
||||
|
||||
let icc = tag_bytes(&mut d, Tag::IccProfile);
|
||||
let xmp = tag_bytes(&mut d, Tag::from_u16_exhaustive(700)).map(|v| String::from_utf8_lossy(&v).trim_end_matches('\0').to_string());
|
||||
let orientation = d.get_tag_u32(Tag::Orientation).ok().map(|v| v as u16);
|
||||
let planar = d.get_tag_u32(Tag::PlanarConfiguration).ok() == Some(2);
|
||||
let extra = d.get_tag_u16_vec(Tag::ExtraSamples).ok().unwrap_or_default();
|
||||
let colormap = d.get_tag_u16_vec(Tag::ColorMap).ok();
|
||||
let compression = d.get_tag_u32(Tag::Compression).ok();
|
||||
|
||||
let (model, n_color, n_samples, bits) = layout(ct, compression, planar)?;
|
||||
let has_alpha = n_samples > n_color && !matches!(ct, ColorType::Palette(_));
|
||||
let premultiplied = has_alpha && extra.first() == Some(&1);
|
||||
|
||||
let res = d.read_image().map_err(err)?;
|
||||
let (wu, hu) = (w as usize, h as usize);
|
||||
let n = wu * hu;
|
||||
let mut buf = match res {
|
||||
DecodingResult::U8(v) if bits < 8 => Buf::U8(unpack(&v, wu, hu, n_samples, bits)?),
|
||||
DecodingResult::U8(v) => Buf::U8(v),
|
||||
DecodingResult::U16(v) => Buf::U16(v),
|
||||
// Rare integer widths: keep 16 significant bits (plenty for display-referred data).
|
||||
DecodingResult::U32(v) => Buf::U16(v.into_iter().map(|x| (x >> 16) as u16).collect()),
|
||||
DecodingResult::U64(v) => Buf::U16(v.into_iter().map(|x| (x >> 48) as u16).collect()),
|
||||
DecodingResult::I8(v) => Buf::U8(v.into_iter().map(|x| (x.max(0) as u8) << 1).collect()),
|
||||
DecodingResult::I16(v) => Buf::U16(v.into_iter().map(|x| (x.max(0) as u16) << 1).collect()),
|
||||
DecodingResult::I32(v) => Buf::U16(v.into_iter().map(|x| (x.max(0) >> 15) as u16).collect()),
|
||||
DecodingResult::I64(v) => Buf::U16(v.into_iter().map(|x| (x.max(0) >> 47) as u16).collect()),
|
||||
DecodingResult::F16(v) => Buf::F32(v.into_iter().map(|x| x.to_f32()).collect()),
|
||||
DecodingResult::F32(v) => Buf::F32(v),
|
||||
DecodingResult::F64(v) => Buf::F32(v.into_iter().map(|x| x as f32).collect()),
|
||||
let model = match img.layout() {
|
||||
craft_image_codecs::ChannelLayout::Gray | craft_image_codecs::ChannelLayout::GrayA => Model::Gray,
|
||||
craft_image_codecs::ChannelLayout::Rgb | craft_image_codecs::ChannelLayout::Rgba => Model::Rgb,
|
||||
craft_image_codecs::ChannelLayout::Cmyk | craft_image_codecs::ChannelLayout::CmykA => Model::Cmyk,
|
||||
craft_image_codecs::ChannelLayout::Lab | craft_image_codecs::ChannelLayout::LabA => {
|
||||
return Err(Error::Unsupported(F, "CIE L*a*b* TIFF"));
|
||||
}
|
||||
};
|
||||
let bit_depth = bits;
|
||||
|
||||
if planar && n_samples > 1 {
|
||||
buf = interleave(buf, n, n_samples);
|
||||
}
|
||||
|
||||
// Palette → 16-bit RGB.
|
||||
if let ColorType::Palette(b) = ct {
|
||||
let map = colormap.ok_or_else(|| err("palette image without ColorMap"))?;
|
||||
let entries = 1usize << b.min(16);
|
||||
if map.len() < 3 * entries {
|
||||
return Err(err("short ColorMap"));
|
||||
let alpha = img.layout().has_alpha();
|
||||
let buf = match img.sample_type() {
|
||||
craft_image_codecs::SampleType::U8 => Buf::U8(img.as_u8().ok_or_else(|| err("expected 8-bit samples"))?.to_vec()),
|
||||
craft_image_codecs::SampleType::U16 => Buf::U16(img.to_u16_samples().ok_or_else(|| err("expected 16-bit samples"))?),
|
||||
craft_image_codecs::SampleType::F16 => {
|
||||
let bits = img.to_f16_samples().ok_or_else(|| err("expected 16-bit float samples"))?;
|
||||
Buf::F32(bits.into_iter().map(|h| h.to_f32()).collect())
|
||||
}
|
||||
let idx: Vec<usize> = match &buf {
|
||||
Buf::U8(v) => v.iter().map(|&i| i as usize).collect(),
|
||||
Buf::U16(v) => v.iter().map(|&i| i as usize).collect(),
|
||||
Buf::F32(_) => return Err(err("float palette")),
|
||||
};
|
||||
let mut out = Vec::with_capacity(n * 3);
|
||||
for &i in idx.iter().take(n) {
|
||||
let i = i.min(entries - 1);
|
||||
out.extend_from_slice(&[map[i], map[entries + i], map[2 * entries + i]]);
|
||||
}
|
||||
buf = Buf::U16(out);
|
||||
let raw = Raw { width: wu, height: hu, model: Model::Rgb, alpha: false, premultiplied: false, buf, bit_depth };
|
||||
let meta = Meta { icc, xmp, orientation, ..Default::default() };
|
||||
return finish(F, raw, meta, (w, h), opts);
|
||||
}
|
||||
craft_image_codecs::SampleType::F32 => Buf::F32(img.to_f32_samples().ok_or_else(|| err("expected 32-bit float samples"))?),
|
||||
};
|
||||
|
||||
// WhiteIsZero needs nothing here: the `tiff` crate inverts those samples as it reads them (and
|
||||
// refuses WhiteIsZero layouts it can't invert), so inverting again would show a negative.
|
||||
|
||||
// Drop extra samples beyond colour + one alpha.
|
||||
let keep = n_color + has_alpha as usize;
|
||||
if n_samples != keep {
|
||||
buf = select_channels(buf, n, n_samples, keep);
|
||||
}
|
||||
let raw = Raw { width: wu, height: hu, model, alpha: has_alpha, premultiplied, buf, bit_depth };
|
||||
let meta = Meta { icc, xmp, orientation, ..Default::default() };
|
||||
let raw = Raw { width: w as usize, height: h as usize, model, alpha, premultiplied: false, buf, bit_depth };
|
||||
let meta = Meta { icc: img.icc, xmp: img.meta.xmp, orientation, ..Default::default() };
|
||||
finish(F, raw, meta, (w, h), opts)
|
||||
}
|
||||
|
||||
fn limits(opts: &DecodeOptions) -> Limits {
|
||||
let mut limits = Limits::default();
|
||||
limits.decoding_buffer_size = opts.max_pixels.saturating_mul(4 * 8).min(usize::MAX as u64) as usize;
|
||||
// A strip or tile whose declared byte count exceeds this is refused: the default 128 MiB refuses a
|
||||
// single-strip uncompressed 16-bit export of ~22 MP (#1983). Raised to the decoded image's cap
|
||||
// (32 bytes per allowed pixel), never lowered: a strip or tile can outgrow the pixels it shows
|
||||
// (JPEG headers, padding to the tile size).
|
||||
limits.intermediate_buffer_size = limits.intermediate_buffer_size.max(limits.decoding_buffer_size);
|
||||
limits.ifd_value_size = 32 << 20;
|
||||
limits
|
||||
}
|
||||
|
||||
/// The sample layout of a colour type we can decode: (model, colour samples, samples per pixel,
|
||||
/// bits per sample).
|
||||
fn layout(ct: ColorType, compression: Option<u32>, planar: bool) -> Result<(Model, usize, usize, u8)> {
|
||||
let l = match ct {
|
||||
ColorType::Gray(b) => (Model::Gray, 1, 1, b),
|
||||
ColorType::GrayA(b) => (Model::Gray, 1, 2, b),
|
||||
ColorType::RGB(b) => (Model::Rgb, 3, 3, b),
|
||||
ColorType::RGBA(b) => (Model::Rgb, 3, 4, b),
|
||||
ColorType::CMYK(b) => (Model::Cmyk, 4, 4, b),
|
||||
ColorType::CMYKA(b) => (Model::Cmyk, 4, 5, b),
|
||||
ColorType::Palette(b) => (Model::Rgb, 1, 1, b),
|
||||
// JPEG-compressed YCbCr is converted to RGB by the JPEG decoder.
|
||||
ColorType::YCbCr(b) if compression == Some(7) => (Model::Rgb, 3, 3, b),
|
||||
ColorType::Multiband { bit_depth, num_samples } if num_samples >= 3 => (Model::Rgb, 3, num_samples as usize, bit_depth),
|
||||
ColorType::Multiband { bit_depth, num_samples } if num_samples >= 1 => (Model::Gray, 1, num_samples as usize, bit_depth),
|
||||
other => return Err(Error::Unsupported(F, unsupported_name(other))),
|
||||
};
|
||||
if l.3 < 8 && planar && l.2 > 1 {
|
||||
return Err(Error::Unsupported(F, "planar TIFF with samples narrower than a byte"));
|
||||
}
|
||||
Ok(l)
|
||||
}
|
||||
|
||||
/// Stored dimensions and orientation from the first IFD, without reading the image data. Refused,
|
||||
/// as a decode would refuse it: a layout or compression we can't decode, or strips/tiles that
|
||||
/// start past the end of the file (truncated). The compressed data itself is not checked.
|
||||
pub(crate) fn header(bytes: &[u8]) -> Result<(u32, u32, u16)> {
|
||||
let mut d = Decoder::new(std::io::Cursor::new(bytes)).map_err(err)?.with_limits(limits(&DecodeOptions::default()));
|
||||
let (w, h) = d.dimensions().map_err(err)?;
|
||||
check_size(F, w as u64, h as u64, &DecodeOptions::default())?;
|
||||
let ct = d.colortype().map_err(err)?;
|
||||
let compression = d.get_tag_u32(Tag::Compression).ok();
|
||||
let planar = d.get_tag_u32(Tag::PlanarConfiguration).ok() == Some(2);
|
||||
layout(ct, compression, planar)?;
|
||||
// what this build of the `tiff` crate decompresses: none, CCITT G4, LZW, JPEG, Deflate, PackBits
|
||||
if !matches!(compression.unwrap_or(1), 1 | 4 | 5 | 7 | 8 | 32773 | 32946) {
|
||||
let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(err)?;
|
||||
if !complete {
|
||||
return Err(err("truncated TIFF"));
|
||||
}
|
||||
let ifd = tiff.ifds.first().ok_or_else(|| err("no IFD"))?;
|
||||
let info = ifd.image().map_err(err)?;
|
||||
check_size(F, info.width as u64, info.height as u64, &DecodeOptions::default())?;
|
||||
let bits = info.bits_per_sample.first().copied().unwrap_or(1);
|
||||
if bits < 8 && info.planar == 2 && info.samples_per_pixel > 1 {
|
||||
return Err(Error::Unsupported(F, "planar TIFF with samples narrower than a byte"));
|
||||
}
|
||||
if !(matches!(info.photometric, 0 | 1 | 2 | 3 | 5) || info.photometric == 6 && info.compression == 7) {
|
||||
return Err(Error::Unsupported(F, "colour type"));
|
||||
}
|
||||
// what `craft-image-codecs`'s TIFF decoder decompresses: none, CCITT G4, LZW, JPEG, Deflate, PackBits
|
||||
if !matches!(info.compression, 1 | 4 | 5 | 7 | 8 | 32773 | 32946) {
|
||||
return Err(Error::Unsupported(F, "compression method"));
|
||||
}
|
||||
let chunks = |offsets: Tag, counts: Tag, d: &mut Decoder<std::io::Cursor<&[u8]>>| {
|
||||
Some((d.get_tag_u64_vec(offsets).ok()?, d.get_tag_u64_vec(counts).ok()?))
|
||||
};
|
||||
let (offsets, counts) = chunks(Tag::StripOffsets, Tag::StripByteCounts, &mut d)
|
||||
.or_else(|| chunks(Tag::TileOffsets, Tag::TileByteCounts, &mut d))
|
||||
.ok_or_else(|| err("no image data"))?;
|
||||
// Only where each chunk starts: some writers overstate StripByteCounts, and the `tiff` crate
|
||||
// decodes such files (it stops once it has the pixels), so a chunk's end proves nothing.
|
||||
let len = bytes.len() as u64;
|
||||
if offsets.is_empty() || offsets.iter().zip(&counts).any(|(&o, &c)| c > 0 && o >= len) {
|
||||
if info.offsets.is_empty()
|
||||
|| info.offsets.iter().zip(&info.byte_counts).any(|(&o, &c)| c > 0 && (o >= len || (info.compression == 1 && o.saturating_add(c) > len)))
|
||||
{
|
||||
return Err(err("truncated: image data past the end of the file"));
|
||||
}
|
||||
let orientation = d.get_tag_u32(Tag::Orientation).ok().map(|v| v as u16).filter(|o| (1..=8).contains(o)).unwrap_or(1);
|
||||
Ok((w, h, orientation))
|
||||
}
|
||||
|
||||
/// A BYTE/UNDEFINED tag as bytes (the decoder may surface unknown byte tags as unsigned values).
|
||||
fn tag_bytes<R: std::io::Read + std::io::Seek>(d: &mut Decoder<R>, tag: Tag) -> Option<Vec<u8>> {
|
||||
use tiff::decoder::ifd::Value;
|
||||
fn byte(v: &Value) -> Option<u8> {
|
||||
match v {
|
||||
Value::Byte(b) => Some(*b),
|
||||
Value::Unsigned(u) => u8::try_from(*u).ok(),
|
||||
Value::Short(u) => u8::try_from(*u).ok(),
|
||||
Value::SignedByte(b) => Some(*b as u8),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
match d.find_tag(tag).ok()?? {
|
||||
Value::List(v) => v.iter().map(byte).collect(),
|
||||
Value::Ascii(s) => Some(s.into_bytes()),
|
||||
v => byte(&v).map(|b| vec![b]),
|
||||
}
|
||||
}
|
||||
|
||||
fn unsupported_name(ct: ColorType) -> &'static str {
|
||||
match ct {
|
||||
ColorType::Lab(_) => "CIE L*a*b* TIFF",
|
||||
ColorType::YCbCr(_) => "uncompressed YCbCr TIFF",
|
||||
_ => "colour type",
|
||||
}
|
||||
}
|
||||
|
||||
/// Samples narrower than a byte (1-bit bilevel and fax scans, 2- and 4-bit gray) arrive packed, most
|
||||
/// significant bits first, each row padded to a whole byte. Spread them out one per byte, scaled to
|
||||
/// 0–255 like any 8-bit sample.
|
||||
fn unpack(packed: &[u8], width: usize, height: usize, samples: usize, bits: u8) -> Result<Vec<u8>> {
|
||||
if !matches!(bits, 1 | 2 | 4) {
|
||||
return Err(Error::Unsupported(F, "TIFF samples of 3, 5, 6 or 7 bits"));
|
||||
}
|
||||
let bits = bits as usize;
|
||||
let per_row = width.checked_mul(samples).ok_or_else(|| err("row too wide"))?;
|
||||
let row_bytes = per_row.checked_mul(bits).ok_or_else(|| err("row too wide"))?.div_ceil(8);
|
||||
if row_bytes == 0 || packed.len() / row_bytes < height {
|
||||
return Err(err("sample buffer too short"));
|
||||
}
|
||||
let max = (1u8 << bits) - 1;
|
||||
let scale = 255 / max; // 255, 85, 17: exact
|
||||
let mut out = Vec::with_capacity(per_row.saturating_mul(height));
|
||||
for row in packed.chunks_exact(row_bytes).take(height) {
|
||||
let values = row.iter().flat_map(|&b| (1..=8 / bits).map(move |i| (b >> (8 - bits * i)) & max));
|
||||
out.extend(values.take(per_row).map(|v| v * scale));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn interleave(buf: Buf, n: usize, s: usize) -> Buf {
|
||||
fn go<T: Copy + Default>(v: Vec<T>, n: usize, s: usize) -> Vec<T> {
|
||||
if v.len() < n * s {
|
||||
return v;
|
||||
}
|
||||
let mut out = vec![T::default(); n * s];
|
||||
for c in 0..s {
|
||||
for i in 0..n {
|
||||
out[i * s + c] = v[c * n + i];
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
match buf {
|
||||
Buf::U8(v) => Buf::U8(go(v, n, s)),
|
||||
Buf::U16(v) => Buf::U16(go(v, n, s)),
|
||||
Buf::F32(v) => Buf::F32(go(v, n, s)),
|
||||
}
|
||||
}
|
||||
|
||||
fn select_channels(buf: Buf, n: usize, s: usize, keep: usize) -> Buf {
|
||||
fn go<T: Copy>(v: Vec<T>, n: usize, s: usize, keep: usize) -> Vec<T> {
|
||||
if v.len() < n * s {
|
||||
return v;
|
||||
}
|
||||
let mut out = Vec::with_capacity(n * keep);
|
||||
for px in v.chunks_exact(s).take(n) {
|
||||
out.extend_from_slice(&px[..keep]);
|
||||
}
|
||||
out
|
||||
}
|
||||
match buf {
|
||||
Buf::U8(v) => Buf::U8(go(v, n, s, keep)),
|
||||
Buf::U16(v) => Buf::U16(go(v, n, s, keep)),
|
||||
Buf::F32(v) => Buf::F32(go(v, n, s, keep)),
|
||||
}
|
||||
let orientation = ifd.u16(craft_tiff::tags::ORIENTATION).filter(|o| (1..=8).contains(o)).unwrap_or(1);
|
||||
Ok((info.width, info.height, orientation))
|
||||
}
|
||||
+36
-23
@@ -1,4 +1,4 @@
|
||||
//! WebP decode via `image-webp` (lossy + lossless, alpha, ICCP/EXIF/XMP chunks; first frame of
|
||||
//! WebP decode via `craft-image-codecs` (lossy + lossless, alpha, ICCP/EXIF/XMP chunks; first frame of
|
||||
//! animations).
|
||||
|
||||
use crate::convert::{Buf, Meta, Model, Raw, check_size, finish};
|
||||
@@ -11,20 +11,28 @@ fn err(e: impl std::fmt::Display) -> Error {
|
||||
}
|
||||
|
||||
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
|
||||
let mut d = image_webp::WebPDecoder::new(std::io::Cursor::new(bytes)).map_err(err)?;
|
||||
// RIFF chunk lengths are untrusted. Metadata cannot exceed the input;
|
||||
// otherwise a tiny truncated file can request gigabytes before read_exact fails.
|
||||
d.set_memory_limit(bytes.len());
|
||||
let (w, h) = d.dimensions();
|
||||
let ic_opts = craft_image_codecs::DecodeOptions {
|
||||
limits: craft_image_codecs::Limits {
|
||||
max_width: u32::MAX,
|
||||
max_height: u32::MAX,
|
||||
max_pixels: opts.max_pixels,
|
||||
max_alloc: opts.max_pixels.saturating_mul(8),
|
||||
},
|
||||
keep_orientation: true,
|
||||
};
|
||||
let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::WebP, bytes, &ic_opts).map_err(|e| match e {
|
||||
craft_image_codecs::CodecError::LimitExceeded(_) => Error::TooLarge(0, 0),
|
||||
other => err(other),
|
||||
})?;
|
||||
let (w, h) = (img.width(), img.height());
|
||||
check_size(F, w as u64, h as u64, opts)?;
|
||||
let alpha = d.has_alpha();
|
||||
let size = d.output_buffer_size().ok_or_else(|| err("buffer size overflow"))?;
|
||||
let mut buf = vec![0u8; size];
|
||||
d.read_image(&mut buf).map_err(err)?;
|
||||
let icc = d.icc_profile().ok().flatten();
|
||||
let exif = d.exif_metadata().ok().flatten().map(|e| if e.starts_with(b"Exif\0\0") { e[6..].to_vec() } else { e });
|
||||
let xmp = d.xmp_metadata().ok().flatten().map(|x| String::from_utf8_lossy(&x).trim_end_matches('\0').to_string());
|
||||
let raw = Raw { width: w as usize, height: h as usize, model: Model::Rgb, alpha, premultiplied: false, buf: Buf::U8(buf), bit_depth: 8 };
|
||||
let alpha = img.layout().has_alpha();
|
||||
let model = if img.layout().is_gray() { Model::Gray } else { Model::Rgb };
|
||||
let buf = Buf::U8(img.as_u8().ok_or_else(|| err("expected 8-bit WebP samples"))?.to_vec());
|
||||
let icc = img.icc;
|
||||
let exif = img.meta.exif.map(|e| if e.starts_with(b"Exif\0\0") { e[6..].to_vec() } else { e });
|
||||
let xmp = img.meta.xmp.map(|x| x.trim_end_matches('\0').to_string());
|
||||
let raw = Raw { width: w as usize, height: h as usize, model, alpha, premultiplied: false, buf, bit_depth: 8 };
|
||||
finish(F, raw, Meta { icc, exif, xmp, ..Default::default() }, (w, h), opts)
|
||||
}
|
||||
|
||||
@@ -34,23 +42,28 @@ pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
|
||||
/// itself is not checked.
|
||||
pub(crate) fn header(bytes: &[u8]) -> Result<(u32, u32, u16)> {
|
||||
let mut p = 12usize;
|
||||
loop {
|
||||
let Some(h) = bytes.get(p..p.saturating_add(8)) else { return Err(err("no image data")) };
|
||||
let mut has_image = false;
|
||||
let mut exif_chunk: Option<&[u8]> = None;
|
||||
while let Some(h) = bytes.get(p..p.saturating_add(8)) {
|
||||
let size = u32::from_le_bytes([h[4], h[5], h[6], h[7]]) as usize;
|
||||
let end = p.saturating_add(8).saturating_add(size);
|
||||
if matches!(&h[0..4], b"VP8 " | b"VP8L" | b"ANMF") {
|
||||
if end > bytes.len() {
|
||||
return Err(err("truncated image data"));
|
||||
}
|
||||
break;
|
||||
has_image = true;
|
||||
} else if &h[0..4] == b"EXIF" && end <= bytes.len() && exif_chunk.is_none() {
|
||||
exif_chunk = Some(&bytes[p + 8..end]);
|
||||
}
|
||||
p = end.saturating_add(size & 1);
|
||||
}
|
||||
let mut d = image_webp::WebPDecoder::new(std::io::Cursor::new(bytes)).map_err(err)?;
|
||||
d.set_memory_limit(bytes.len());
|
||||
let (w, h) = d.dimensions();
|
||||
if !has_image {
|
||||
return Err(err("no image data"));
|
||||
}
|
||||
let (w, h) = craft_image_codecs::probe_size(bytes).map_err(err)?;
|
||||
let orientation =
|
||||
exif_chunk.map(|e| e.strip_prefix(b"Exif\0\0").unwrap_or(e)).map(crate::exif::summarize).unwrap_or_default().orientation.unwrap_or(1);
|
||||
let (w, h) = if (5..=8).contains(&orientation) { (h, w) } else { (w, h) };
|
||||
check_size(F, w as u64, h as u64, &DecodeOptions::default())?;
|
||||
let exif = d.exif_metadata().ok().flatten();
|
||||
let exif = exif.as_deref().map(|e| e.strip_prefix(b"Exif\0\0").unwrap_or(e));
|
||||
Ok((w, h, exif.map(crate::exif::summarize).unwrap_or_default().orientation.unwrap_or(1)))
|
||||
Ok((w, h, orientation))
|
||||
}
|
||||
@@ -45,8 +45,7 @@ fn bench_24mp_jpeg() {
|
||||
let mp = (w * h) as f64 / 1e6;
|
||||
let px = synthetic(w, h);
|
||||
let img = EncodeImage::new(w as u32, h as u32, 3, Samples::U8(&px));
|
||||
let plain =
|
||||
time("encode JPEG q90 4:2:0 (jpeg-encoder)", mp, 1, || encode_jpeg(&img, 90, ChromaSubsampling::S420, &EncodeMeta::default()).unwrap());
|
||||
let plain = time("encode JPEG q90 4:2:0 (craft-jpeg)", mp, 1, || encode_jpeg(&img, 90, ChromaSubsampling::S420, &EncodeMeta::default()).unwrap());
|
||||
println!(" size {:.1} MB", plain.len() as f64 / 1e6);
|
||||
|
||||
time("decode full → linear Rgb32f", mp, 3, || decode(&plain, DecodeOptions::default()).unwrap());
|
||||
|
||||
@@ -124,15 +124,18 @@ fn system_icc_profiles() {
|
||||
assert_eq!(info.model, IccColorModel::Cmyk);
|
||||
assert_eq!(info.kind, IccKind::NeedsCms);
|
||||
// CMYK TIFF tagged with it: paper white stays near white, 100% K near black.
|
||||
use tiff::encoder::{TiffEncoder, colortype};
|
||||
let mut cur = std::io::Cursor::new(Vec::new());
|
||||
{
|
||||
let mut enc = TiffEncoder::new(&mut cur).unwrap();
|
||||
let mut im = enc.new_image::<colortype::CMYK8>(2, 1).unwrap();
|
||||
im.encoder().write_tag(tiff::tags::Tag::IccProfile, &b[..]).unwrap();
|
||||
im.write_data(&[0u8, 0, 0, 0, 0, 0, 0, 255]).unwrap();
|
||||
}
|
||||
let d = decode(&cur.into_inner(), DecodeOptions::default()).unwrap();
|
||||
let mut ifd = craft_tiff::IfdBuilder::new()
|
||||
.with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![2]))
|
||||
.with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![1]))
|
||||
.with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![8, 8, 8, 8]))
|
||||
.with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1]))
|
||||
.with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![5]))
|
||||
.with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![4]))
|
||||
.with(craft_tiff::tags::PLANAR_CONFIGURATION, craft_tiff::Value::Short(vec![1]))
|
||||
.with(craft_tiff::tags::ICC_PROFILE, craft_tiff::Value::Undefined(b.clone()));
|
||||
ifd.set_image(craft_tiff::ImageData::Strips { rows_per_strip: 1, strips: vec![vec![0u8, 0, 0, 0, 0, 0, 0, 255]] });
|
||||
let tif = craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[ifd]).unwrap();
|
||||
let d = decode(&tif, DecodeOptions::default()).unwrap();
|
||||
assert_eq!(d.space.origin, SpaceOrigin::IccCms);
|
||||
let w = d.image.get(0, 0);
|
||||
let k = d.image.get(1, 0);
|
||||
|
||||
@@ -44,7 +44,11 @@ fn float_tiff_nonfinite_gray_and_icc_curves_match_clipped_samples() {
|
||||
Some(icc::write_named(NamedSpace::Srgb)),
|
||||
Some(icc::write_matrix_trc(&lightcraft_color::SRGB, &Trc::Gamma(2.0))),
|
||||
Some(icc::write_matrix_trc(&lightcraft_color::SRGB, &Trc::Table(vec![0, 16384, 65535]))),
|
||||
Some(moxcms::ColorProfile::new_gray_with_gamma(2.0).encode().unwrap()),
|
||||
Some({
|
||||
let mut p = craft_cms::Builtin::GrayGamma22.profile().clone();
|
||||
p.gray_trc = Some(craft_cms::Curve::Gamma(2.0));
|
||||
p.with_encoded_bytes().to_bytes().to_vec()
|
||||
}),
|
||||
Some(b"invalid ICC profile".to_vec()),
|
||||
];
|
||||
for channels in [1, 3] {
|
||||
@@ -77,42 +81,43 @@ fn float_tiff_nonfinite_alpha_is_finite_and_bounded() {
|
||||
assert!(decoded.to_working().data.iter().flatten().all(|v| v.is_finite()));
|
||||
|
||||
// Associated alpha takes the same path before unpremultiplication.
|
||||
let mut cursor = std::io::Cursor::new(Vec::new());
|
||||
{
|
||||
use tiff::encoder::{TiffEncoder, colortype};
|
||||
let mut encoder = TiffEncoder::new(&mut cursor).unwrap();
|
||||
let mut image = encoder.new_image::<colortype::RGBA32Float>(2, 1).unwrap();
|
||||
image.encoder().write_tag(tiff::tags::Tag::ExtraSamples, &[1u16][..]).unwrap();
|
||||
image.write_data(&[f32::INFINITY, f32::NAN, f32::NEG_INFINITY, 0.5, 1.0, 2.0, 3.0, 0.5]).unwrap();
|
||||
let mut strip = Vec::new();
|
||||
for s in [f32::INFINITY, f32::NAN, f32::NEG_INFINITY, 0.5, 1.0, 2.0, 3.0, 0.5] {
|
||||
strip.extend_from_slice(&s.to_le_bytes());
|
||||
}
|
||||
let decoded = decode(&cursor.into_inner(), DecodeOptions::default()).unwrap();
|
||||
let mut ifd = craft_tiff::IfdBuilder::new()
|
||||
.with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![2]))
|
||||
.with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![1]))
|
||||
.with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![32, 32, 32, 32]))
|
||||
.with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1]))
|
||||
.with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![2]))
|
||||
.with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![4]))
|
||||
.with(craft_tiff::tags::PLANAR_CONFIGURATION, craft_tiff::Value::Short(vec![1]))
|
||||
.with(craft_tiff::tags::SAMPLE_FORMAT, craft_tiff::Value::Short(vec![3, 3, 3, 3]))
|
||||
.with(craft_tiff::tags::EXTRA_SAMPLES, craft_tiff::Value::Short(vec![1]));
|
||||
ifd.set_image(craft_tiff::ImageData::Strips { rows_per_strip: 1, strips: vec![strip] });
|
||||
let tif = craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[ifd]).unwrap();
|
||||
let decoded = decode(&tif, DecodeOptions::default()).unwrap();
|
||||
assert_eq!(decoded.alpha.as_ref().unwrap().data, [0.5, 0.5]);
|
||||
assert_eq!(decoded.image.data, [[2.0, 0.0, 0.0], [2.0, 4.0, 6.0]]);
|
||||
assert_eq!(decoded.image.data, [[1.0, 0.0, 0.0], [2.0, 4.0, 6.0]]);
|
||||
}
|
||||
|
||||
fn lut_profile() -> Vec<u8> {
|
||||
use moxcms::{ColorProfile, LutMultidimensionalType, LutWarehouse, Matrix3d, ToneReprCurve, Vector3d, Xyzd};
|
||||
let mut profile = ColorProfile::new_srgb();
|
||||
profile.cicp = None;
|
||||
profile.red_colorant = Xyzd::default();
|
||||
profile.green_colorant = Xyzd::default();
|
||||
profile.blue_colorant = Xyzd::default();
|
||||
profile.red_trc = None;
|
||||
profile.green_trc = None;
|
||||
profile.blue_trc = None;
|
||||
use craft_cms::pipeline::Stage;
|
||||
use craft_cms::profile::{Lut, LutKind};
|
||||
let mut profile = craft_cms::Builtin::Srgb.profile().clone();
|
||||
profile.matrix = None;
|
||||
profile.trc = None;
|
||||
let m: Vec<f64> = NamedSpace::Srgb.to_xyz_d50().0.iter().flatten().map(|&v| v * 32768.0 / 65535.0).collect();
|
||||
let ident = vec![craft_cms::Curve::Identity; 3];
|
||||
// A procedural mAB profile without a CLUT: linear RGB to D50 XYZ, then identity curves.
|
||||
profile.lut_a_to_b_colorimetric = Some(LutWarehouse::Multidimensional(LutMultidimensionalType {
|
||||
num_input_channels: 3,
|
||||
num_output_channels: 3,
|
||||
grid_points: [0; 16],
|
||||
clut: None,
|
||||
a_curves: vec![],
|
||||
m_curves: vec![ToneReprCurve::Parametric(vec![1.0]); 3],
|
||||
matrix: Matrix3d { v: NamedSpace::Srgb.to_xyz_d50().0.map(|r| r.map(|v| v * 32768.0 / 65535.0)) },
|
||||
b_curves: vec![ToneReprCurve::Parametric(vec![1.0]); 3],
|
||||
bias: Vector3d::default(),
|
||||
}));
|
||||
profile.encode().unwrap()
|
||||
profile.a2b[1] = Some(Lut {
|
||||
kind: LutKind::Ab,
|
||||
inputs: 3,
|
||||
outputs: 3,
|
||||
stages: vec![Stage::Curves(ident.clone()), Stage::Matrix { rows: 3, cols: 3, m, offset: vec![0.0; 3] }, Stage::Curves(ident)],
|
||||
});
|
||||
profile.with_encoded_bytes().to_bytes().to_vec()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -122,11 +122,11 @@ fn tiny_gif_and_bmp_declaring_a_gigapixel_canvas_are_refused_before_allocating()
|
||||
fn large_gif_and_bmp_sizes_come_from_the_header() {
|
||||
let (w, h) = (2_100u32, 2_000u32);
|
||||
let rgb = vec![90u8; (w * h * 3) as usize];
|
||||
let mut bmp = Vec::new();
|
||||
image::codecs::bmp::BmpEncoder::new(&mut bmp).encode(&rgb, w, h, image::ExtendedColorType::Rgb8).unwrap();
|
||||
let mut gif = Vec::new();
|
||||
let frame = image::RgbaImage::from_pixel(w, h, image::Rgba([90, 90, 90, 255]));
|
||||
image::codecs::gif::GifEncoder::new(&mut gif).encode_frame(image::Frame::new(frame)).unwrap();
|
||||
let bmp_img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgb, rgb).unwrap();
|
||||
let bmp = craft_image_codecs::encode(&bmp_img, craft_image_codecs::Format::Bmp, &craft_image_codecs::EncodeOptions::default()).unwrap();
|
||||
let rgba = [90, 90, 90, 255].repeat((w * h) as usize);
|
||||
let gif_img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgba, rgba).unwrap();
|
||||
let gif = craft_image_codecs::encode(&gif_img, craft_image_codecs::Format::Gif, &craft_image_codecs::EncodeOptions::default()).unwrap();
|
||||
for (name, bytes) in [("bmp", bmp), ("gif", gif)] {
|
||||
let header = read_header(&bytes).unwrap_or_else(|e| panic!("{name}: {e}"));
|
||||
assert_eq!((header.width, header.height, header.orientation), (w, h, 1), "{name}");
|
||||
|
||||
@@ -13,16 +13,20 @@ fn meta(exif: &Option<Vec<u8>>) -> EncodeMeta<'_> {
|
||||
EncodeMeta { exif: exif.as_deref(), xmp: Some("<x/>"), ..Default::default() }
|
||||
}
|
||||
|
||||
/// A TIFF with an Orientation tag (`encode_tiff` writes none), via the `tiff` crate's encoder.
|
||||
/// A TIFF with an Orientation tag (`encode_tiff` writes none), via `craft-tiff`.
|
||||
fn tiff_oriented(img: &Rgba8, orientation: u16) -> Vec<u8> {
|
||||
use tiff::encoder::{TiffEncoder, colortype};
|
||||
let rgb: Vec<u8> = img.data.iter().flat_map(|p| [p[0], p[1], p[2]]).collect();
|
||||
let mut cur = std::io::Cursor::new(Vec::new());
|
||||
let mut enc = TiffEncoder::new(&mut cur).unwrap();
|
||||
let mut im = enc.new_image::<colortype::RGB8>(img.width as u32, img.height as u32).unwrap();
|
||||
im.encoder().write_tag(tiff::tags::Tag::Orientation, orientation).unwrap();
|
||||
im.write_data(&rgb).unwrap();
|
||||
cur.into_inner()
|
||||
let mut ifd = craft_tiff::IfdBuilder::new()
|
||||
.with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![img.width as u32]))
|
||||
.with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![img.height as u32]))
|
||||
.with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![8, 8, 8]))
|
||||
.with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1]))
|
||||
.with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![2]))
|
||||
.with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![3]))
|
||||
.with(craft_tiff::tags::PLANAR_CONFIGURATION, craft_tiff::Value::Short(vec![1]))
|
||||
.with(craft_tiff::tags::ORIENTATION, craft_tiff::Value::Short(vec![orientation]));
|
||||
ifd.set_image(craft_tiff::ImageData::Strips { rows_per_strip: img.height as u32, strips: vec![rgb] });
|
||||
craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[ifd]).unwrap()
|
||||
}
|
||||
|
||||
/// A minimal 8-bit RGB PSD (raw or PackBits-literal merged data) with an EXIF resource.
|
||||
@@ -99,8 +103,8 @@ fn fixtures(w: usize, h: usize) -> Vec<(String, Vec<u8>)> {
|
||||
}
|
||||
out.push(("tiff lzw".into(), encode_tiff(&e, TiffCompression::Lzw, &EncodeMeta::default()).unwrap()));
|
||||
out.push(("tiff packbits".into(), encode_tiff(&e, TiffCompression::PackBits, &EncodeMeta::default()).unwrap()));
|
||||
let mut bmp = Vec::new();
|
||||
image::codecs::bmp::BmpEncoder::new(&mut bmp).encode(&rgb, w as u32, h as u32, image::ExtendedColorType::Rgb8).unwrap();
|
||||
let bmp_img = craft_image_codecs::Image::from_u8(w as u32, h as u32, craft_image_codecs::ChannelLayout::Rgb, rgb).unwrap();
|
||||
let bmp = craft_image_codecs::encode(&bmp_img, craft_image_codecs::Format::Bmp, &craft_image_codecs::EncodeOptions::default()).unwrap();
|
||||
out.push(("bmp".into(), bmp));
|
||||
out
|
||||
}
|
||||
@@ -124,6 +128,31 @@ fn header_matches_decode() {
|
||||
assert_eq!((o("jpeg 420 o6"), o("png rgba o8"), o("webp o6"), o("psd o8"), o("tiff o6"), o("jpeg 444 o0")), (6, 8, 6, 8, 6, 1));
|
||||
}
|
||||
|
||||
/// The header probe's colour/compression policy is unchanged by the Clippy repair (#4264).
|
||||
/// These are header-only fixtures, not compressed image streams for the decoder.
|
||||
#[test]
|
||||
fn tiff_header_colour_and_compression_policy() {
|
||||
for photometric in [0, 1, 2, 3, 4, 5, 6, 8, u16::MAX] {
|
||||
for compression in [1, 4, 5, 6, 7, 8, 32773, 32946, u16::MAX] {
|
||||
let mut ifd = craft_tiff::IfdBuilder::new()
|
||||
.with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![1]))
|
||||
.with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![1]))
|
||||
.with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![8, 8, 8]))
|
||||
.with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![3]))
|
||||
.with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![compression]))
|
||||
.with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![photometric]));
|
||||
ifd.set_image(craft_tiff::ImageData::Strips { rows_per_strip: 1, strips: vec![vec![0; 3]] });
|
||||
let bytes = craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[ifd]).unwrap();
|
||||
let accepted = match photometric {
|
||||
0 | 1 | 2 | 3 | 5 => [1, 4, 5, 7, 8, 32773, 32946].contains(&compression),
|
||||
6 => compression == 7, // YCbCr is supported only with JPEG compression.
|
||||
_ => false,
|
||||
};
|
||||
assert_eq!(read_header(&bytes).is_ok(), accepted, "photometric={photometric}, compression={compression}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whenever the decode the probe used to run accepts a file (here: every cut of every fixture),
|
||||
/// the header agrees with it: reading headers never refuses a file that decodes. The reverse
|
||||
/// is checked for truncation, which the header readers detect.
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -79,14 +79,67 @@ fn jpeg_gray_and_scaled_decode() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jpeg_gray_alpha_drops_alpha_and_ignores_surplus_samples() {
|
||||
let (w, h) = (13u32, 9u32);
|
||||
let mut gray_alpha: Vec<u8> = (0..w * h).flat_map(|i| [((i * 37 + 19) % 256) as u8, ((i * 83) % 256) as u8]).collect();
|
||||
// EncodeImage historically permits a surplus tail; the writer must ignore it.
|
||||
gray_alpha.extend_from_slice(&[255, 0, 17]);
|
||||
let bytes = encode_jpeg(&EncodeImage::new(w, h, 2, Samples::U8(&gray_alpha)), 100, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
|
||||
let decoded = decode(&bytes, DecodeOptions::default()).unwrap();
|
||||
assert_eq!((decoded.width, decoded.height), (w, h));
|
||||
assert!(decoded.grayscale);
|
||||
assert!(!decoded.has_alpha);
|
||||
let output = decoded.to_srgb8();
|
||||
for (i, pixel) in output.data.iter().enumerate() {
|
||||
let source = gray_alpha[i * 2];
|
||||
assert!(pixel[..3].iter().all(|&channel| channel.abs_diff(source) <= 2), "pixel {i}: source {source}, output {pixel:?}");
|
||||
assert_eq!(pixel[3], 255);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jpeg_422_matches_the_previous_encoder_within_rounding_tolerance() {
|
||||
let img = smooth(97, 61);
|
||||
let rgb: Vec<u8> = img.data.iter().flat_map(|p| p[..3].iter().copied()).collect();
|
||||
let mut reference = Vec::new();
|
||||
let mut encoder = jpeg_encoder::Encoder::new(&mut reference, 90);
|
||||
encoder.set_sampling_factor(jpeg_encoder::SamplingFactor::R_4_2_2);
|
||||
encoder.encode(&rgb, img.width as u16, img.height as u16, jpeg_encoder::ColorType::Rgb).unwrap();
|
||||
|
||||
let shared = encode_jpeg(
|
||||
&EncodeImage::new(img.width as u32, img.height as u32, 3, Samples::U8(&rgb)),
|
||||
90,
|
||||
ChromaSubsampling::S422,
|
||||
&EncodeMeta::default(),
|
||||
)
|
||||
.unwrap();
|
||||
let old = decode(&reference, DecodeOptions::default()).unwrap().to_srgb8();
|
||||
let new = decode(&shared, DecodeOptions::default()).unwrap().to_srgb8();
|
||||
let mut max_delta = 0u8;
|
||||
let mut squared_error = 0u64;
|
||||
for (a, b) in old.data.iter().zip(&new.data) {
|
||||
for c in 0..3 {
|
||||
let delta = a[c].abs_diff(b[c]);
|
||||
max_delta = max_delta.max(delta);
|
||||
squared_error += u64::from(delta) * u64::from(delta);
|
||||
}
|
||||
}
|
||||
let mse = squared_error as f64 / (old.data.len() * 3) as f64;
|
||||
// The shared writer changes DCT rounding relative to jpeg-encoder, but this deterministic
|
||||
// 4:2:2 fixture stays within a six-code maximum and 2.0 mean-squared channel delta.
|
||||
assert!(max_delta <= 6, "max per-channel delta from the prior 4:2:2 output was {max_delta}");
|
||||
assert!(mse <= 2.0, "mean squared channel delta from the prior 4:2:2 output was {mse}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jpeg_cmyk_converts() {
|
||||
// 0 = no ink: white, cyan, black.
|
||||
let px: Vec<u8> = [[0u8, 0, 0, 0], [255, 0, 0, 0], [0, 0, 0, 255]].iter().flat_map(|p| p.repeat(16 * 16)).collect();
|
||||
let img: Vec<u8> = px;
|
||||
let mut out = Vec::new();
|
||||
let enc = jpeg_encoder::Encoder::new(&mut out, 95);
|
||||
enc.encode(&img, 16, 48, jpeg_encoder::ColorType::Cmyk).unwrap();
|
||||
let ic_img = craft_image_codecs::Image::from_u8(16, 48, craft_image_codecs::ChannelLayout::Cmyk, img).unwrap();
|
||||
let ic_opts = craft_image_codecs::EncodeOptions { jpeg_quality: 95, ..Default::default() };
|
||||
let out = craft_image_codecs::encode(&ic_img, craft_image_codecs::Format::Jpeg, &ic_opts).unwrap();
|
||||
let d = decode(&out, DecodeOptions::default()).unwrap();
|
||||
assert!(d.cmyk);
|
||||
assert_eq!(d.space.origin, SpaceOrigin::Naive);
|
||||
@@ -246,15 +299,18 @@ fn tiff_depths_and_compressions() {
|
||||
|
||||
#[test]
|
||||
fn tiff_cmyk_and_orientation() {
|
||||
use tiff::encoder::{TiffEncoder, colortype};
|
||||
let mut cur = std::io::Cursor::new(Vec::new());
|
||||
{
|
||||
let mut enc = TiffEncoder::new(&mut cur).unwrap();
|
||||
let mut im = enc.new_image::<colortype::CMYK8>(2, 1).unwrap();
|
||||
im.encoder().write_tag(tiff::tags::Tag::Orientation, 8u16).unwrap();
|
||||
im.write_data(&[0u8, 0, 0, 0, 0, 255, 255, 0]).unwrap();
|
||||
}
|
||||
let d = decode(&cur.into_inner(), DecodeOptions::default()).unwrap();
|
||||
let mut ifd = craft_tiff::IfdBuilder::new()
|
||||
.with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![2]))
|
||||
.with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![1]))
|
||||
.with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![8, 8, 8, 8]))
|
||||
.with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1]))
|
||||
.with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![5]))
|
||||
.with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![4]))
|
||||
.with(craft_tiff::tags::PLANAR_CONFIGURATION, craft_tiff::Value::Short(vec![1]))
|
||||
.with(craft_tiff::tags::ORIENTATION, craft_tiff::Value::Short(vec![8]));
|
||||
ifd.set_image(craft_tiff::ImageData::Strips { rows_per_strip: 1, strips: vec![vec![0u8, 0, 0, 0, 0, 255, 255, 0]] });
|
||||
let tif = craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[ifd]).unwrap();
|
||||
let d = decode(&tif, DecodeOptions::default()).unwrap();
|
||||
assert!(d.cmyk);
|
||||
assert_eq!(d.orientation, 8);
|
||||
let white = d.image.get(0, 0);
|
||||
@@ -268,16 +324,21 @@ fn tiff_cmyk_and_orientation() {
|
||||
/// the thumbnail (and the full decode) failed with "decoder limits exceeded".
|
||||
#[test]
|
||||
fn tiff_single_strip_larger_than_128_mib() {
|
||||
use tiff::encoder::{TiffEncoder, colortype};
|
||||
let (w, h) = (3923u32, 5877u32);
|
||||
let mut cur = std::io::Cursor::new(Vec::new());
|
||||
{
|
||||
let mut enc = TiffEncoder::new(&mut cur).unwrap();
|
||||
let mut im = enc.new_image::<colortype::RGB16>(w, h).unwrap();
|
||||
im.rows_per_strip(h).unwrap();
|
||||
im.write_data(&vec![0x8000u16; (w * h * 3) as usize]).unwrap();
|
||||
let mut strip = Vec::with_capacity((w * h * 3 * 2) as usize);
|
||||
for _ in 0..(w * h * 3) {
|
||||
strip.extend_from_slice(&0x8000u16.to_le_bytes());
|
||||
}
|
||||
let bytes = cur.into_inner();
|
||||
let mut ifd = craft_tiff::IfdBuilder::new()
|
||||
.with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![w]))
|
||||
.with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![h]))
|
||||
.with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![16, 16, 16]))
|
||||
.with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1]))
|
||||
.with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![2]))
|
||||
.with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![3]))
|
||||
.with(craft_tiff::tags::PLANAR_CONFIGURATION, craft_tiff::Value::Short(vec![1]));
|
||||
ifd.set_image(craft_tiff::ImageData::Strips { rows_per_strip: h, strips: vec![strip] });
|
||||
let bytes = craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[ifd]).unwrap();
|
||||
assert!(bytes.len() > 128 << 20);
|
||||
let t = decode_thumbnail(&bytes, 256).unwrap();
|
||||
assert_eq!((t.source_width, t.source_height), (w, h));
|
||||
@@ -310,8 +371,7 @@ fn tiff_small_pixel_budget_reads_headers_and_padded_tiles() {
|
||||
let opts = |max_pixels| DecodeOptions { max_size: None, max_pixels };
|
||||
|
||||
// 1 × 1 gray, one JPEG-compressed strip (~400 bytes for one pixel), with a one-pixel budget.
|
||||
let mut jpeg = Vec::new();
|
||||
jpeg_encoder::Encoder::new(&mut jpeg, 90).encode(&[128], 1, 1, jpeg_encoder::ColorType::Luma).unwrap();
|
||||
let jpeg = encode_jpeg(&EncodeImage::new(1, 1, 1, Samples::U8(&[128])), 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
|
||||
let at = 8 + 2 + 10 * 12 + 4;
|
||||
let entries = [
|
||||
(256, 4, 1),
|
||||
@@ -438,22 +498,17 @@ fn webp_lossless_exact() {
|
||||
|
||||
#[test]
|
||||
fn gif_and_bmp_decode() {
|
||||
use image::ImageEncoder;
|
||||
let img = gradient(16, 8);
|
||||
let rgba = img.as_bytes();
|
||||
let mut bmp = Vec::new();
|
||||
image::codecs::bmp::BmpEncoder::new(&mut bmp).write_image(&rgba, 16, 8, image::ExtendedColorType::Rgba8).unwrap();
|
||||
let ic_img = craft_image_codecs::Image::from_u8(16, 8, craft_image_codecs::ChannelLayout::Rgba, rgba.to_vec()).unwrap();
|
||||
let bmp = craft_image_codecs::encode(&ic_img, craft_image_codecs::Format::Bmp, &craft_image_codecs::EncodeOptions::default()).unwrap();
|
||||
assert_eq!(sniff(&bmp), Some(Format::Bmp));
|
||||
let d = decode(&bmp, DecodeOptions::default()).unwrap();
|
||||
assert_eq!(d.to_srgb8(), img);
|
||||
|
||||
let mut gif = Vec::new();
|
||||
{
|
||||
let mut enc = image::codecs::gif::GifEncoder::new(&mut gif);
|
||||
let flat = Rgba8::filled(16, 8, [10, 200, 30, 255]);
|
||||
let buf = image::RgbaImage::from_raw(16, 8, flat.as_bytes()).unwrap();
|
||||
enc.encode_frame(image::Frame::new(buf)).unwrap();
|
||||
}
|
||||
let flat = Rgba8::filled(16, 8, [10, 200, 30, 255]);
|
||||
let flat_ic = craft_image_codecs::Image::from_u8(16, 8, craft_image_codecs::ChannelLayout::Rgba, flat.as_bytes().to_vec()).unwrap();
|
||||
let gif = craft_image_codecs::encode(&flat_ic, craft_image_codecs::Format::Gif, &craft_image_codecs::EncodeOptions::default()).unwrap();
|
||||
assert_eq!(sniff(&gif), Some(Format::Gif));
|
||||
let d = decode(&gif, DecodeOptions::default()).unwrap();
|
||||
let p = d.to_srgb8().get(3, 3);
|
||||
@@ -729,10 +784,14 @@ fn print_resolution_is_written() {
|
||||
assert_eq!(u32::from_be_bytes(png[i + 4..i + 8].try_into().unwrap()), 11811, "300 ppi in pixels per metre");
|
||||
assert_eq!(png[i + 12], 1, "unit = metre");
|
||||
let tif = encode_tiff(&img, TiffCompression::Deflate, &meta).unwrap();
|
||||
let mut d = tiff::decoder::Decoder::new(std::io::Cursor::new(tif)).unwrap();
|
||||
let x = d.get_tag(tiff::tags::Tag::XResolution).unwrap();
|
||||
assert_eq!(x.into_u32_vec().ok(), Some(vec![300, 1]));
|
||||
assert_eq!(d.get_tag_u32(tiff::tags::Tag::ResolutionUnit).unwrap(), 2, "inch");
|
||||
let parsed = craft_tiff::Tiff::parse(&tif).unwrap();
|
||||
let ifd = &parsed.ifds[0];
|
||||
let x = match ifd.get(craft_tiff::tags::X_RESOLUTION).map(|e| &e.value) {
|
||||
Some(craft_tiff::Value::Rational(v)) => v.first().copied(),
|
||||
_ => None,
|
||||
};
|
||||
assert_eq!(x.map(|(n, d)| (n / d, n % d)), Some((300, 0)));
|
||||
assert_eq!(ifd.u16(craft_tiff::tags::RESOLUTION_UNIT), Some(2), "inch");
|
||||
// without a ppi: JFIF density stays an aspect ratio
|
||||
let j = encode_jpeg(&img, 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
|
||||
assert_eq!(&j[13..18], &[0, 0, 1, 0, 1]);
|
||||
@@ -750,17 +809,18 @@ fn pq_png_is_16_bit_rec2020_pq_with_cicp_and_content_light() {
|
||||
.collect();
|
||||
let xmp = r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"/>"#;
|
||||
let bytes = encode_png_pq(w, h, &rgb, &EncodeMeta { xmp: Some(xmp), ppi: Some(240), ..Default::default() }).unwrap();
|
||||
let mut r = png::Decoder::new(std::io::Cursor::new(bytes.as_slice())).read_info().unwrap();
|
||||
let cicp = r.info().coding_independent_code_points.expect("cICP");
|
||||
assert_eq!((cicp.color_primaries, cicp.transfer_function), (9, 16), "BT.2020, PQ");
|
||||
let mut buf = vec![0; r.output_buffer_size().unwrap()];
|
||||
let f = r.next_frame(&mut buf).unwrap();
|
||||
assert_eq!((f.bit_depth, f.color_type, f.width, f.height), (png::BitDepth::Sixteen, png::ColorType::Rgb, w, h));
|
||||
let decoded = craft_image_codecs::decode_as(craft_image_codecs::Format::Png, &bytes).unwrap();
|
||||
let cicp_at = bytes.windows(4).position(|w| w == b"cICP").expect("cICP");
|
||||
assert_eq!((bytes[cicp_at + 4], bytes[cicp_at + 5]), (9, 16), "BT.2020, PQ");
|
||||
assert_eq!(
|
||||
(decoded.sample_type(), decoded.layout(), decoded.width(), decoded.height()),
|
||||
(craft_image_codecs::SampleType::U16, craft_image_codecs::ChannelLayout::Rgb, w, h)
|
||||
);
|
||||
let samples = decoded.to_u16_samples().unwrap();
|
||||
// every sample decodes back to its luminance
|
||||
for x in [0usize, 20, 63] {
|
||||
for c in 0..3 {
|
||||
let o = (x * 3 + c) * 2;
|
||||
let e = u16::from_be_bytes([buf[o], buf[o + 1]]) as f32 / 65535.0;
|
||||
let e = samples[x * 3 + c] as f32 / 65535.0;
|
||||
let want = rgb[x * 3 + c] * HDR_REFERENCE_WHITE_NITS;
|
||||
assert!((encode::pq_decode(e) - want).abs() <= want * 0.01 + 0.01, "x {x} c {c}: {} vs {want}", encode::pq_decode(e));
|
||||
}
|
||||
@@ -770,6 +830,12 @@ fn pq_png_is_16_bit_rec2020_pq_with_cicp_and_content_light() {
|
||||
let max_cll = u32::from_be_bytes(bytes[at + 4..at + 8].try_into().unwrap()) as f32 / 10_000.0;
|
||||
let brightest = rgb.iter().copied().fold(0.0f32, f32::max) * HDR_REFERENCE_WHITE_NITS;
|
||||
assert!((max_cll - brightest.ceil()).abs() <= 1.0, "{max_cll} vs {brightest}");
|
||||
let max_fall = u32::from_be_bytes(bytes[at + 8..at + 12].try_into().unwrap()) / 10_000;
|
||||
let expected_max_fall =
|
||||
(rgb.as_chunks::<3>().0.iter().map(|p| f64::from(p.iter().copied().fold(0.0f32, f32::max) * HDR_REFERENCE_WHITE_NITS)).sum::<f64>()
|
||||
/ f64::from(w * h))
|
||||
.ceil() as u32;
|
||||
assert_eq!(max_fall, expected_max_fall, "MaxFALL uses CTA-861.3-A's per-pixel maximum channel");
|
||||
assert!(bytes.windows(17).any(|w| w == b"XML:com.adobe.xmp"), "XMP kept");
|
||||
assert!(bytes.windows(4).any(|w| w == b"pHYs"), "print resolution kept");
|
||||
assert!(!bytes.windows(4).any(|w| w == b"iCCP"), "no ICC profile next to cICP");
|
||||
|
||||
@@ -9,6 +9,7 @@ description = "LightCraft develop settings: the non-destructive edit model, cont
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-mime = { workspace = true }
|
||||
lightcraft-geom = { workspace = true }
|
||||
lightcraft-color = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
}
|
||||
@@ -156,21 +156,7 @@ pub(crate) fn de_detail<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Vec<Se
|
||||
|
||||
const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
fn base64_encode(bytes: &[u8]) -> String {
|
||||
let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
|
||||
for chunk in bytes.chunks(3) {
|
||||
let b = [chunk.first().copied().unwrap_or(0), chunk.get(1).copied().unwrap_or(0), chunk.get(2).copied().unwrap_or(0)];
|
||||
let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]);
|
||||
for i in 0..4 {
|
||||
if i <= chunk.len() {
|
||||
out.push(char::from(B64[((n >> (18 - 6 * i)) & 63) as usize]));
|
||||
} else {
|
||||
out.push('=');
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
use craft_mime::transfer::base64 as base64_encode;
|
||||
|
||||
fn base64_decode(s: &str) -> Option<Vec<u8>> {
|
||||
let mut out = Vec::with_capacity(s.len() / 4 * 3);
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -9,6 +9,7 @@ description = "LightCraft engine: session, command registry, history, render job
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-mime = { workspace = true }
|
||||
# Paths stored in a Lightroom catalog are followed only to local files or the catalog's share.
|
||||
craft-docpath = { workspace = true }
|
||||
lightcraft-geom = { workspace = true }
|
||||
@@ -29,6 +30,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 +67,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 }
|
||||
@@ -323,9 +323,12 @@ mod tests {
|
||||
}
|
||||
assert_eq!(a.exists("here"), Some(true));
|
||||
assert_eq!(a.exists("gone"), Some(false));
|
||||
// Cache publication precedes its wake callback. Check these two notifications before
|
||||
// querying another unknown path, whose notification must not satisfy this assertion.
|
||||
wait_until("availability wake notifications", || woke.load(Ordering::SeqCst) >= 2);
|
||||
assert!(woke.load(Ordering::SeqCst) >= 2);
|
||||
assert!(a.is_offline("gone") && !a.is_offline("here") && !a.is_offline("unknown"));
|
||||
assert_eq!(on_caller.load(Ordering::SeqCst), 0, "never probed on the calling thread");
|
||||
assert!(woke.load(Ordering::SeqCst) >= 2);
|
||||
assert!(a.gone_generation() >= 2);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ use super::look_fit::{fit_dng_tone, fit_preview, look_proxies};
|
||||
use super::mono::{fit_dark_tone, monochrome_look};
|
||||
use super::proxy::LensGeometry;
|
||||
use super::{CameraLook, CameraTone, CameraTransform, ColorData, D50, D65, Mat3, ProfileLook, REC2020, RawFormat, RawImage, ToneCurve, Xy, bradford};
|
||||
use lightcraft_raw::profile::ToneCurveEval;
|
||||
|
||||
/// Long edge of the sensor/JPEG proxy a single photo's look is fitted on (the acceptance gates
|
||||
/// below were set at this size).
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use super::mono::to_prophoto;
|
||||
use super::pairs::{displayed, luma};
|
||||
use super::{CameraLook, CameraTone, HsvTable, Mat3, ToneMap, luminance_2020};
|
||||
use lightcraft_raw::profile::HsvTableApply;
|
||||
|
||||
/// [`HueSat::apply`] restores the luminance in full only if the table kept at least this share of it. A colour
|
||||
/// the table darkens to [`NO_LUMINANCE_KEPT`] or less (only near-black noise, whose channels go negative) keeps
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use super::tone::{ELASTICITY_BLACK, MAX_TONE_ELASTICITY, TONE_REPAIR_ELASTICITY, limit_elasticity};
|
||||
use super::*;
|
||||
use lightcraft_raw::profile::{HsvTableApply, ProfileLookHueSat};
|
||||
|
||||
fn steep_toe_scene() -> (Rgb32f, Rgb32f) {
|
||||
let mut sensor = Rgb32f::new(96, 64);
|
||||
|
||||
@@ -361,3 +361,13 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod feature_name_tests {
|
||||
#[test]
|
||||
fn perspective_labels_keep_existing_command_ids() {
|
||||
for (id, label) in [("geometry.upright", "Perspective Correction"), ("geometry.guides", "Perspective Guides")] {
|
||||
assert_eq!(super::find_command(id).expect("existing command").label, label);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -241,6 +241,9 @@ fn fill_missing_meta(id: lightcraft_catalog::PhotoId, ph: &lightcraft_catalog::P
|
||||
}
|
||||
if m.shutter.is_empty() {
|
||||
m.shutter = src.shutter.clone();
|
||||
m.exposure_time = src.exposure_time;
|
||||
} else {
|
||||
repair_legacy_shutter(&mut m, src);
|
||||
}
|
||||
m.focal_mm = m.focal_mm.or(src.focal_mm);
|
||||
m.aperture = m.aperture.or(src.aperture);
|
||||
@@ -265,6 +268,26 @@ fn fill_missing_meta(id: lightcraft_catalog::PhotoId, ph: &lightcraft_catalog::P
|
||||
ops
|
||||
}
|
||||
|
||||
/// Repair only the shutter data an old import wrote, keeping independently edited metadata.
|
||||
fn repair_legacy_shutter(m: &mut lightcraft_catalog::Meta, src: &lightcraft_catalog::Meta) -> bool {
|
||||
if m.exposure_time.is_none()
|
||||
&& let Some(t) = src.exposure_time
|
||||
&& (m.shutter.trim() == src.shutter || legacy_shutter_text(t).is_some_and(|old| m.shutter.trim() == old))
|
||||
{
|
||||
m.shutter = src.shutter.clone();
|
||||
m.exposure_time = Some(t);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// The shutter text an import wrote before the exact exposure time was kept (#1619): whole seconds at
|
||||
/// one second and over, otherwise `1/n` with `n` rounded. Only for recognising such a text again.
|
||||
fn legacy_shutter_text(t: f64) -> Option<String> {
|
||||
(t.is_finite() && t > 0.0).then(|| if t >= 1.0 { format!("{t:.0}") } else { format!("1/{:.0}", 1.0 / t) })
|
||||
}
|
||||
|
||||
/// Ops that give a photo the lens data its file carries now (`info`, a fresh probe), or none when
|
||||
/// it already has them. A catalog from before the lens data was read at import has none, so such a
|
||||
/// photo's correction stayed off. When the photo gains lens data and its develop settings are still
|
||||
@@ -314,8 +337,16 @@ pub(crate) fn reload_ops(
|
||||
}
|
||||
// virtual copies share the file
|
||||
for c in cat.photos().filter(|c| c.copy_of == Some(id)) {
|
||||
let theirs: Vec<Op> =
|
||||
content_op(c.id, c, info.clone()).into_iter().chain(lens_ops(c.id, c, info)).chain(as_shot_wb_op(c.id, c, info)).collect();
|
||||
let mut meta = c.meta.clone();
|
||||
// Copies share the source file but may have independently edited metadata. Repair only
|
||||
// recognizable legacy shutter values, not every missing field on the copy.
|
||||
let shutter_op = repair_legacy_shutter(&mut meta, &info.meta).then(|| Op::SetMeta { id: c.id, meta: Box::new(meta) });
|
||||
let theirs: Vec<Op> = content_op(c.id, c, info.clone())
|
||||
.into_iter()
|
||||
.chain(shutter_op)
|
||||
.chain(lens_ops(c.id, c, info))
|
||||
.chain(as_shot_wb_op(c.id, c, info))
|
||||
.collect();
|
||||
if !theirs.is_empty() {
|
||||
ops.extend(theirs);
|
||||
changed.push(c.id);
|
||||
@@ -441,6 +472,78 @@ mod tests {
|
||||
assert!(fill_missing_meta(PhotoId(1), &ph, &info).is_empty());
|
||||
}
|
||||
|
||||
/// Reload from Disk repairs a shutter speed an older import rounded (0.4 s stored as `1/2`, with no
|
||||
/// exact time): the photo takes the file's exact time and today's text. A shutter speed set by hand
|
||||
/// that is not what an import wrote, and one that already has its exact time, are kept.
|
||||
#[test]
|
||||
fn reload_repairs_a_shutter_speed_an_older_import_rounded() {
|
||||
let mut ph = Photo::new(PhotoId(1), Source::File { path: "/x.nef".into() }, "x.nef", "NEF", 6000, 4000, "2026-10-06T00:00:00");
|
||||
ph.captured = Some("2026-10-04T08:16:11".into());
|
||||
let mut info = crate::media::ProbeInfo { captured: ph.captured.clone(), ..Default::default() };
|
||||
for (t, legacy, shown) in [(0.4, "1/2", "0.4"), (2.5, "2", "2.5"), (1.0 / 2.8, "1/3", "0.3571"), (1.0 / 250.0, "1/250", "1/250")] {
|
||||
info.meta.exposure_time = Some(t);
|
||||
info.meta.shutter = lightcraft_meta::format_exposure_time(t).unwrap();
|
||||
ph.meta.shutter = legacy.into();
|
||||
ph.meta.exposure_time = None;
|
||||
let ops = fill_missing_meta(PhotoId(1), &ph, &info);
|
||||
let Some(Op::SetMeta { meta, .. }) = ops.first() else { panic!("{t}: {ops:?}") };
|
||||
assert_eq!((meta.shutter.as_str(), meta.exposure_time), (shown, Some(t)), "{t}");
|
||||
assert_eq!(meta.shutter_seconds(), Some(t), "{t}");
|
||||
// repaired → nothing more to do
|
||||
ph.meta = (**meta).clone();
|
||||
assert!(fill_missing_meta(PhotoId(1), &ph, &info).is_empty(), "{t}");
|
||||
}
|
||||
// typed by hand, not what any import wrote for this file
|
||||
info.meta.exposure_time = Some(0.4);
|
||||
info.meta.shutter = "0.4".into();
|
||||
ph.meta.shutter = "1/8".into();
|
||||
ph.meta.exposure_time = None;
|
||||
assert!(fill_missing_meta(PhotoId(1), &ph, &info).is_empty(), "a hand-set shutter speed is kept");
|
||||
// already has an exact time (the text was edited afterwards): kept
|
||||
ph.meta.shutter = "1/2".into();
|
||||
ph.meta.exposure_time = Some(0.4);
|
||||
assert!(fill_missing_meta(PhotoId(1), &ph, &info).is_empty(), "a photo with an exact time is left alone");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reload_repairs_legacy_shutter_on_copies_without_replacing_edits() {
|
||||
let mut cat = lightcraft_catalog::Catalog::new();
|
||||
let mut source = Photo::new(PhotoId(1), Source::File { path: "/x.nef".into() }, "x.nef", "NEF", 6000, 4000, "");
|
||||
source.meta.shutter = "1/2".into();
|
||||
cat.apply(Op::AddPhoto { photo: Box::new(source.clone()) }).unwrap();
|
||||
for (id, text, exact) in [(2, "1/2", None), (3, "0.4", None), (4, "1/8", None), (5, "1/2", Some(0.4))] {
|
||||
let mut copy = source.clone();
|
||||
copy.id = PhotoId(id);
|
||||
copy.copy_of = Some(source.id);
|
||||
copy.meta.shutter = text.into();
|
||||
copy.meta.exposure_time = exact;
|
||||
copy.meta.title = "My copy".into();
|
||||
cat.apply(Op::AddPhoto { photo: Box::new(copy) }).unwrap();
|
||||
}
|
||||
let mut info = crate::media::ProbeInfo::default();
|
||||
info.meta.shutter = "0.4".into();
|
||||
info.meta.exposure_time = Some(0.4);
|
||||
info.meta.title = "File title".into();
|
||||
let mut ops = Vec::new();
|
||||
let mut changed = Vec::new();
|
||||
reload_ops(&cat, source.id, &info, &mut ops, &mut changed);
|
||||
for op in ops {
|
||||
cat.apply(op).unwrap();
|
||||
}
|
||||
for id in [1, 2, 3] {
|
||||
let m = &cat.photo(PhotoId(id)).unwrap().meta;
|
||||
assert_eq!((m.shutter.as_str(), m.exposure_time), ("0.4", Some(0.4)));
|
||||
assert_eq!(m.shutter_seconds(), Some(0.4));
|
||||
}
|
||||
for (id, text, exact) in [(4, "1/8", None), (5, "1/2", Some(0.4))] {
|
||||
let m = &cat.photo(PhotoId(id)).unwrap().meta;
|
||||
assert_eq!((m.shutter.as_str(), m.exposure_time), (text, exact));
|
||||
}
|
||||
for id in [2, 3, 4, 5] {
|
||||
assert_eq!(cat.photo(PhotoId(id)).unwrap().meta.title, "My copy");
|
||||
}
|
||||
}
|
||||
|
||||
/// Reload from Disk gives a photo imported before focus points were read its camera focus point,
|
||||
/// and never replaces one it has.
|
||||
#[test]
|
||||
|
||||
@@ -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);
|
||||
@@ -918,7 +1065,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
// ---- geometry: Upright
|
||||
cmd!(
|
||||
"geometry.upright",
|
||||
"Upright",
|
||||
"Perspective Correction",
|
||||
[],
|
||||
None,
|
||||
"{mode: off|auto|guided|level|vertical|full} — analyses the photo (line segments → vanishing points) and stores the correction; run again to update after lens changes",
|
||||
@@ -936,7 +1083,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
Some(lightcraft_pipeline::upright::auto_transform(&src, &info, &d, m))
|
||||
}
|
||||
};
|
||||
edit(s, "geometry.upright", "Upright", |d| {
|
||||
edit(s, "geometry.upright", "Perspective Correction", |d| {
|
||||
d.geometry.upright = mode;
|
||||
d.geometry.upright_transform = transform;
|
||||
Ok(())
|
||||
@@ -946,10 +1093,10 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
cmd!(
|
||||
"geometry.guides",
|
||||
"Guided Upright Guides",
|
||||
"Perspective Guides",
|
||||
[],
|
||||
None,
|
||||
"{guides: [[x0,y0,x1,y1], …] (≤ 4, normalized coords of the lens-corrected image), add?: bool (append one guide, dropping the oldest beyond 4)} — switches Upright to Guided",
|
||||
"{guides: [[x0,y0,x1,y1], …] (≤ 4, normalized coords of the lens-corrected image), add?: bool (append one guide, dropping the oldest beyond 4)} — switches Perspective Correction to Guided",
|
||||
has_active,
|
||||
|s, p| {
|
||||
let parse = |v: &Value| -> Option<(Point, Point)> {
|
||||
@@ -959,7 +1106,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
let guides: Vec<(Point, Point)> =
|
||||
p.get("guides").and_then(Value::as_array).map(|a| a.iter().filter_map(parse).collect()).unwrap_or_default();
|
||||
let add = bool_or(p, "add", false);
|
||||
edit(s, "geometry.guides", "Guided Upright", |d| {
|
||||
edit(s, "geometry.guides", "Guided Perspective", |d| {
|
||||
if add {
|
||||
d.geometry.guides.extend(guides);
|
||||
} else {
|
||||
@@ -1130,10 +1277,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
// log2 of the light the photo gathered (shutter s × ISO / f-number²)
|
||||
let gathered = |s: &Session, id: PhotoId| -> Option<f64> {
|
||||
let m = &s.catalog.photo(id)?.meta;
|
||||
let t = match m.shutter.split_once('/') {
|
||||
Some((a, b)) => a.trim().parse::<f64>().ok()? / b.trim().parse::<f64>().ok()?,
|
||||
None => m.shutter.trim().trim_end_matches('s').trim().parse::<f64>().ok()?,
|
||||
};
|
||||
let t = m.shutter_seconds()?;
|
||||
let (n, iso) = (m.aperture? as f64, m.iso? as f64);
|
||||
(t > 0.0 && n > 0.0 && iso > 0.0).then(|| (t * iso / (n * n)).log2())
|
||||
};
|
||||
|
||||
@@ -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}))
|
||||
@@ -666,7 +674,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"))?;
|
||||
@@ -1165,6 +1173,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);
|
||||
@@ -1173,7 +1184,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")));
|
||||
|
||||
@@ -196,20 +196,7 @@ fn preview(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
|
||||
/// Standard padded base64 (for an image returned in the reply).
|
||||
fn base64(bytes: &[u8]) -> String {
|
||||
const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
|
||||
for chunk in bytes.chunks(3) {
|
||||
let b = [chunk.first().copied().unwrap_or(0), chunk.get(1).copied().unwrap_or(0), chunk.get(2).copied().unwrap_or(0)];
|
||||
let n = (b[0] as u32) << 16 | (b[1] as u32) << 8 | b[2] as u32;
|
||||
let at = |shift: u32| A.get((n >> shift) as usize & 63).copied().unwrap_or(b'=') as char;
|
||||
out.push(at(18));
|
||||
out.push(at(12));
|
||||
out.push(if chunk.len() > 1 { at(6) } else { '=' });
|
||||
out.push(if chunk.len() > 2 { at(0) } else { '=' });
|
||||
}
|
||||
out
|
||||
}
|
||||
use craft_mime::transfer::base64;
|
||||
|
||||
// ------------------------------------------------------------------------------- templates
|
||||
|
||||
@@ -511,3 +498,16 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod shared_base64_parity {
|
||||
#[test]
|
||||
fn image_reply_bytes_keep_standard_padding_and_no_wrap() {
|
||||
for (input, want) in
|
||||
[(b"".as_slice(), ""), (b"f".as_slice(), "Zg=="), (b"fo".as_slice(), "Zm8="), (b"foo".as_slice(), "Zm9v"), (&[251, 255, 255], "+///")]
|
||||
{
|
||||
assert_eq!(super::base64(input), want);
|
||||
}
|
||||
assert!(!super::base64(&[255; 256]).contains(['\r', '\n']));
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -342,7 +342,7 @@ pub fn export_metadata(photo: &lightcraft_catalog::Photo, catalog: &lightcraft_c
|
||||
out.lens_model = text(&m.lens);
|
||||
out.focal_length = m.focal_mm.map(f64::from);
|
||||
out.f_number = m.aperture.map(f64::from);
|
||||
out.exposure_time = lightcraft_catalog::parse_shutter_seconds(&m.shutter);
|
||||
out.exposure_time = m.shutter_seconds();
|
||||
out.iso = m.iso;
|
||||
}
|
||||
Some(out)
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -375,6 +375,7 @@ fn local_tone(raw: &lightcraft_raw::RawImage) -> Option<Arc<lightcraft_pipeline:
|
||||
/// Subject mask then falls back to its heuristic); a sky matte counts even when empty (no sky).
|
||||
fn dng_mattes(bytes: &[u8], raw: &lightcraft_raw::RawInfo) -> Option<Arc<lightcraft_pipeline::masks::Mattes>> {
|
||||
use lightcraft_pipeline::masks::{MatteKind, Mattes};
|
||||
use lightcraft_raw::semantic::SemanticMaskExt;
|
||||
if raw.format != lightcraft_raw::RawFormat::Dng {
|
||||
return None;
|
||||
}
|
||||
@@ -410,6 +411,7 @@ fn matte_kind(name: &str) -> Option<lightcraft_pipeline::masks::MatteKind> {
|
||||
/// finish stage's camera tone curve: 32 knots, log-spaced over 12 stops below white; above white
|
||||
/// the camera tone's shoulder continues it.
|
||||
pub(crate) fn dng_tone_curve(curve: &lightcraft_raw::profile::ToneCurve) -> Option<lightcraft_pipeline::tone::CameraTone> {
|
||||
use lightcraft_raw::profile::ToneCurveEval;
|
||||
let knots: [[f32; 2]; 32] = std::array::from_fn(|i| {
|
||||
let x = 2f32.powf(-12.0 + 12.0 * i as f32 / 31.0);
|
||||
[x, curve.eval(x).min(0.9995)]
|
||||
|
||||
@@ -21,7 +21,7 @@ fn stand_in_image(bytes: &[u8], covering: Option<usize>) -> Option<std::borrow::
|
||||
if let Some(jpeg) = jpeg {
|
||||
return Some(std::borrow::Cow::Owned(jpeg));
|
||||
}
|
||||
let undecodable = lightcraft_raw::probe(bytes).is_some_and(|f| !f.is_supported());
|
||||
let undecodable = lightcraft_raw::probe(bytes).is_some_and(|f| !lightcraft_raw::is_supported(f));
|
||||
(undecodable && lightcraft_codecs::sniff(bytes).is_some_and(|f| f == lightcraft_codecs::Format::Tiff))
|
||||
.then_some(std::borrow::Cow::Borrowed(bytes))
|
||||
}
|
||||
@@ -46,6 +46,7 @@ fn decode_stand_in(bytes: &[u8], jpeg: &[u8], opts: lightcraft_codecs::DecodeOpt
|
||||
let space = match space {
|
||||
lightcraft_raw::PreviewColorSpace::Srgb => lightcraft_codecs::NamedSpace::Srgb,
|
||||
lightcraft_raw::PreviewColorSpace::AdobeRgb => lightcraft_codecs::NamedSpace::AdobeRgb,
|
||||
lightcraft_raw::PreviewColorSpace::ProPhoto => lightcraft_codecs::NamedSpace::ProPhoto,
|
||||
};
|
||||
lightcraft_codecs::decode_jpeg_with_fallback(jpeg, opts, space).ok()
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ use super::preview::embedded_preview_size;
|
||||
use super::{MediaKind, Meta, Orientation, ProbeInfo};
|
||||
|
||||
pub(super) fn meta_of(m: &lightcraft_meta::Metadata) -> (Meta, Option<String>) {
|
||||
let shutter = m.exposure_time.map(|t| if t >= 1.0 { format!("{t:.0}") } else { format!("1/{:.0}", 1.0 / t) }).unwrap_or_default();
|
||||
let exposure_time = m.exposure_time.filter(|t| t.is_finite() && *t > 0.0);
|
||||
let shutter = exposure_time.and_then(lightcraft_meta::format_exposure_time).unwrap_or_default();
|
||||
let camera = [m.make.clone().unwrap_or_default(), m.model.clone().unwrap_or_default()].join(" ").trim().to_string();
|
||||
let meta = Meta {
|
||||
camera,
|
||||
@@ -14,6 +15,7 @@ pub(super) fn meta_of(m: &lightcraft_meta::Metadata) -> (Meta, Option<String>) {
|
||||
focal_mm: m.focal_length.map(|f| f as f32),
|
||||
aperture: m.f_number.map(|f| f as f32),
|
||||
shutter,
|
||||
exposure_time,
|
||||
iso: m.iso,
|
||||
flash: m.flash.map(|f| f.fired),
|
||||
exposure_bias: m.exposure_bias.map(|v| v as f32),
|
||||
|
||||
@@ -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();
|
||||
@@ -1845,6 +1851,44 @@ fn existing_photo_settings_are_not_rewritten() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn imported_shutter_times_survive_catalog_and_export_metadata() {
|
||||
use lightcraft_catalog::{Catalog, Photo, PhotoId};
|
||||
for (t, shown) in [
|
||||
(0.3, "0.3"),
|
||||
(0.4, "0.4"),
|
||||
(0.6, "0.6"),
|
||||
(0.8, "0.8"),
|
||||
(1.3, "1.3"),
|
||||
(1.6, "1.6"),
|
||||
(2.5, "2.5"),
|
||||
(3.2, "3.2"),
|
||||
(30.0, "30"),
|
||||
(0.625, "0.625"),
|
||||
(1.0 / 2.8, "0.3571"),
|
||||
(1.0 / 3.0, "1/3"),
|
||||
(0.003, "0.003"),
|
||||
(1.0 / 250.0, "1/250"),
|
||||
(1.0 / 8000.0, "1/8000"),
|
||||
] {
|
||||
let original = lightcraft_meta::Metadata { exposure_time: Some(t), ..Default::default() };
|
||||
let (meta, _) = meta_of(&original);
|
||||
// the catalog shows a short shutter speed, never the raw f64 (`0.35714285714285715`)
|
||||
assert_eq!(meta.shutter, shown, "{t}");
|
||||
// Exercise the persisted catalog JSON, not a value kept alongside it in memory.
|
||||
let meta: Meta = serde_json::from_str(&serde_json::to_string(&meta).unwrap()).unwrap();
|
||||
let mut photo = Photo::new(PhotoId(1), lightcraft_catalog::Source::Demo { scene: 0 }, "capture.jpg", "JPEG", 2, 2, "");
|
||||
photo.meta = meta;
|
||||
let exported = crate::export::export_metadata(&photo, &Catalog::new(), &crate::export::ExportOptions::default()).unwrap();
|
||||
assert_eq!(exported.exposure_time, Some(t));
|
||||
let bytes =
|
||||
crate::export::encode_with_metadata(&lightcraft_raster::Rgba8::new(2, 2), &crate::export::ExportOptions::default(), Some(&exported))
|
||||
.unwrap();
|
||||
let back = lightcraft_meta::extract(&bytes);
|
||||
assert!((back.exposure_time.unwrap() - t).abs() < 0.000001, "{t}: {:?}", back.exposure_time);
|
||||
}
|
||||
}
|
||||
|
||||
/// Issue #1632: a DNG's `ProfileToneCurve` is the finish stage's camera curve, so process V5 extends
|
||||
/// it past its last point (the sensor's white, scene 1.0) with the stop roll-off as well. A curve
|
||||
/// that reaches white there renders as under V4 up to it and white past it; one that ends below
|
||||
|
||||
@@ -338,12 +338,14 @@ pub fn commit_prepared(s: &mut Session, opts: &ImportOptions, now: &str, prepare
|
||||
}
|
||||
|
||||
/// Move, after the commit: save the library, then remove each source whose destination is
|
||||
/// still intact. Nothing is removed when the library can't be saved.
|
||||
/// still intact. Nothing is removed when the library can't be saved: saved is on disk, not just
|
||||
/// handed to the catalog log's thread, which looks at the folder only when it writes (issue #3240),
|
||||
/// so the log is flushed first.
|
||||
fn finish_moves(s: &mut Session, placed: Vec<crate::import_move::Placed>, log0: usize, report: &mut ImportReport) {
|
||||
// (saving here clears the pending ops, so the sidecars `execute` would auto-write are written here)
|
||||
let auto: Vec<Op> = if s.xmp.auto_write { s.pending_log.get(log0..).map(<[Op]>::to_vec).unwrap_or_default() } else { Vec::new() };
|
||||
let before = s.pending_log.len();
|
||||
if let Err(e) = s.persist() {
|
||||
if let Err(e) = s.persist().and_then(|()| s.flush_log()) {
|
||||
for p in placed {
|
||||
let reason = format!("the library could not be saved ({e}): the original stays; the photo uses the copy at {}", p.dst.display());
|
||||
report.kept.push(Kept { path: p.src.to_string_lossy().to_string(), reason });
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use lightcraft_catalog::{FsStore, Journal, LibraryLock, LoadReport, Store};
|
||||
use lightcraft_catalog::{FsStore, Journal, LibraryLock, LoadReport, OpenedFolder, Store};
|
||||
use lightcraft_develop::Preset;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -155,6 +155,10 @@ pub struct Library {
|
||||
warnings_reported: usize,
|
||||
/// Settings files that couldn't be read or set aside: never overwritten this session.
|
||||
blocked: Vec<&'static str>,
|
||||
/// The folder the library was opened from (`None`: its stores were given, not opened here). The
|
||||
/// stores hold their own copy; this one is for what is not a store, so far the thumbnail cache
|
||||
/// (issue #3240).
|
||||
folder: Option<OpenedFolder>,
|
||||
/// Keeps other processes out of this library while it's open (last: released after the
|
||||
/// journal's background snapshot has landed).
|
||||
lock: Option<LibraryLock>,
|
||||
@@ -168,10 +172,19 @@ pub struct LibraryStores {
|
||||
pub catalog: Box<dyn Store>,
|
||||
/// presets.json, view.json, prefs.json.
|
||||
pub files: Box<dyn Store>,
|
||||
/// `dir` is a real directory (enables `Originals/` copies and the `thumbs/` disk cache).
|
||||
/// `dir` is a real directory (enables `Originals/` copies and the `thumbs/` disk cache). The cache
|
||||
/// is bound to the folder `dir` named when the library opened, which only [`Session::open_library`]
|
||||
/// records: the stores of [`Session::open_library_in`] (browser storage, memory) have no folder.
|
||||
pub on_disk: bool,
|
||||
}
|
||||
|
||||
/// What the thumbnail cache asks before it changes its folder: `false` while the library's path leads
|
||||
/// to another folder than the one it was opened from (issue #3240). `None`: there is no folder to ask.
|
||||
fn disk_gate(folder: Option<&OpenedFolder>) -> Option<lightcraft_preview::WriteGate> {
|
||||
let folder = folder?.clone();
|
||||
Some(std::sync::Arc::new(move || folder.check().is_ok()))
|
||||
}
|
||||
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
struct PresetsFile {
|
||||
@@ -359,6 +372,17 @@ fn same_dir(a: &Path, b: &Path) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// The lock for the library at `dir`. One taken in another folder than `folder` (the stores' record,
|
||||
/// which holds no note yet, so only the numbers are compared) means the path changed since the
|
||||
/// stores recorded it, and nothing of this open is consistent: refused.
|
||||
pub(crate) fn lock_in(dir: &Path, folder: &OpenedFolder) -> Result<LibraryLock> {
|
||||
let lock = LibraryLock::acquire(dir, &program_name()).map_err(|e| EngineError::LibraryInUse(e.to_string()))?;
|
||||
if lock.folder().is_some_and(|f| !folder.same_folder(f)) {
|
||||
return Err(EngineError::Other(format!("{} changed while the library was being opened; open it again", dir.display())));
|
||||
}
|
||||
Ok(lock)
|
||||
}
|
||||
|
||||
/// This program, for the lock owner note ("LightCraft", "lightcraft-cli").
|
||||
fn program_name() -> String {
|
||||
let exe = std::env::current_exe().ok().and_then(|p| p.file_stem().map(|s| s.to_string_lossy().to_string()));
|
||||
@@ -464,8 +488,14 @@ impl Session {
|
||||
return Err(EngineError::Other(format!("{} is not a folder", dir.display())));
|
||||
}
|
||||
std::fs::read_dir(&dir).map_err(|e| EngineError::Other(format!("can't open library {}: {e}", dir.display())))?;
|
||||
// the library open in this session keeps its lock when reopened; any other must be free
|
||||
let current = self.library.as_ref().is_some_and(|l| l.on_disk && same_dir(&l.dir, &dir));
|
||||
// the library open in this session keeps its lock when reopened, if it was taken in the folder
|
||||
// at the path now (as in `open_library`); any other must be free
|
||||
let here = OpenedFolder::at(&dir).ok();
|
||||
let current = self.library.as_ref().is_some_and(|l| {
|
||||
l.on_disk
|
||||
&& same_dir(&l.dir, &dir)
|
||||
&& l.lock.as_ref().is_some_and(|k| k.folder().is_none_or(|f| here.as_ref().is_none_or(|h| f.same_folder(h))))
|
||||
});
|
||||
if !current && is_library(&dir) {
|
||||
drop(LibraryLock::acquire(&dir, &program_name()).map_err(|e| EngineError::LibraryInUse(e.to_string()))?);
|
||||
}
|
||||
@@ -489,16 +519,30 @@ impl Session {
|
||||
return Err(EngineError::LightroomCatalog { path: dir, catalog: place.catalog });
|
||||
}
|
||||
let dir = resolve_dir(&dir)?;
|
||||
let open = || FsStore::open(&dir).map_err(|e| EngineError::Other(format!("can't open library {}: {e}", dir.display())));
|
||||
let stores = LibraryStores { catalog: Box::new(open()?), files: Box::new(open()?), on_disk: true, dir: dir.clone() };
|
||||
// the library open in this session (same directory): hand its lock over
|
||||
let reused = self.library.as_mut().filter(|l| same_dir(&l.dir, &dir)).and_then(|l| l.lock.take());
|
||||
let catalog = FsStore::open(&dir).map_err(|e| EngineError::Other(format!("can't open library {}: {e}", dir.display())))?;
|
||||
// the folder `dir` names now: both stores write only while it still does (issue #3240)
|
||||
let folder = catalog.folder().clone();
|
||||
let stores =
|
||||
LibraryStores { catalog: Box::new(catalog), files: Box::new(FsStore::in_folder(folder.clone())), on_disk: true, dir: dir.clone() };
|
||||
// the library open in this session (same directory, same folder): hand its lock over. A lock
|
||||
// on another folder than the path leads to now protects nothing here: take this one's own.
|
||||
// (A lock that couldn't look at its folder is handed over as before.)
|
||||
let reused = self
|
||||
.library
|
||||
.as_mut()
|
||||
.filter(|l| same_dir(&l.dir, &dir))
|
||||
.and_then(|l| l.lock.take_if(|k| k.folder().is_none_or(|f| f.same_folder(&folder))));
|
||||
let reusing = reused.is_some();
|
||||
let mut lock = match reused {
|
||||
Some(l) => Some(l),
|
||||
None => Some(LibraryLock::acquire(&dir, &program_name()).map_err(|e| EngineError::LibraryInUse(e.to_string()))?),
|
||||
None => Some(lock_in(&dir, &folder)?),
|
||||
};
|
||||
if let Err(e) = self.open_stores(stores, seed_demo, &mut lock) {
|
||||
// what tells the folder when its numbers can't (a drive that numbers its folders anew each
|
||||
// time it is mounted): the note this session's lock wrote into it
|
||||
if let Some(note) = lock.as_ref().and_then(LibraryLock::note) {
|
||||
folder.vouch(note);
|
||||
}
|
||||
if let Err(e) = self.open_stores(stores, seed_demo, &mut lock, Some(folder)) {
|
||||
if reusing && let Some(lib) = self.library.as_mut() {
|
||||
lib.lock = lock.take();
|
||||
}
|
||||
@@ -511,7 +555,7 @@ impl Session {
|
||||
/// this session's catalog. Not locked: the host guards against a second opener (see
|
||||
/// [`Session::open_library`] for directories).
|
||||
pub fn open_library_in(&mut self, stores: LibraryStores, seed_demo: bool) -> Result<&LoadReport> {
|
||||
self.open_stores(stores, seed_demo, &mut None)?;
|
||||
self.open_stores(stores, seed_demo, &mut None, None)?;
|
||||
self.loaded_report()
|
||||
}
|
||||
|
||||
@@ -522,8 +566,9 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
/// Open `stores`; the new library takes `lock` once nothing can fail any more.
|
||||
fn open_stores(&mut self, stores: LibraryStores, seed_demo: bool, lock: &mut Option<LibraryLock>) -> Result<()> {
|
||||
/// Open `stores`; the new library takes `lock` once nothing can fail any more. `folder` is the
|
||||
/// folder they write to, when they write to one: the thumbnail cache asks it too.
|
||||
fn open_stores(&mut self, stores: LibraryStores, seed_demo: bool, lock: &mut Option<LibraryLock>, folder: Option<OpenedFolder>) -> Result<()> {
|
||||
let LibraryStores { dir, catalog, mut files, on_disk } = stores;
|
||||
// the current library may be these same files: let its background snapshot land first, and
|
||||
// what its log's thread was handed reach the file before the new journal reads it. When
|
||||
@@ -663,7 +708,7 @@ impl Session {
|
||||
// photo ids are per library: drop decoded sources of the previous one
|
||||
self.media.clear_sources();
|
||||
if on_disk {
|
||||
self.media.attach_disk_cache(&dir.join("thumbs"), self.cache_bytes());
|
||||
self.media.attach_disk_cache(&dir.join("thumbs"), self.cache_bytes(), disk_gate(folder.as_ref()));
|
||||
self.tidy_thumbnail_cache();
|
||||
}
|
||||
let view_written = self.view_json();
|
||||
@@ -684,6 +729,7 @@ impl Session {
|
||||
settings_warnings: unlocked_warning(lock.as_ref()).into_iter().chain(settings.warnings).collect(),
|
||||
warnings_reported: 0,
|
||||
blocked: settings.blocked,
|
||||
folder,
|
||||
lock: lock.take(),
|
||||
});
|
||||
// forget untouched Local records of folders not browsed for a while (journaled at once)
|
||||
@@ -972,8 +1018,8 @@ impl Session {
|
||||
pub fn set_cache_mb(&mut self, mb: u32) -> Result<()> {
|
||||
self.cache_mb = mb;
|
||||
if let Some(lib) = self.library.as_ref().filter(|l| l.on_disk) {
|
||||
let dir = lib.thumbs_dir();
|
||||
self.media.set_disk_cache_bytes(&dir, self.cache_bytes());
|
||||
let (dir, gate) = (lib.thumbs_dir(), disk_gate(lib.folder.as_ref()));
|
||||
self.media.set_disk_cache_bytes(&dir, self.cache_bytes(), gate);
|
||||
}
|
||||
self.save_prefs()
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use super::settings_hash::SettingsHashes;
|
||||
use super::source::{DecodedSource, DenoiseSpec, PairLoader, RENDERED_MEM_BYTES, SourceLevel, SourceRef, THUMB_SOURCE_BYTES};
|
||||
use super::{Arc, DcpProfile, DevelopSettings, HashMap, Hasher128, Lru, MediaCache, Photo, PhotoId, PreviewCache, Rgb32f, Source, SourceInfo};
|
||||
use lightcraft_preview::WriteGate;
|
||||
|
||||
/// The photos that have a cached denoised picture for the denoise model in use. Only memory: nothing here touches the
|
||||
/// file system, so it can be asked on the UI thread.
|
||||
@@ -85,19 +86,24 @@ pub(crate) fn rendered_budget(share: usize) -> usize {
|
||||
impl MediaCache {
|
||||
/// Keep rendered thumbnails on disk in `dir` as well. The cache this replaces is retired
|
||||
/// (its files stay): render jobs still holding it can't write through it any more, even
|
||||
/// into a directory a later clear emptied (`dir` may be the same one).
|
||||
pub fn attach_disk_cache(&mut self, dir: &std::path::Path, disk_bytes: u64) {
|
||||
/// into a directory a later clear emptied (`dir` may be the same one). `gate` is asked before
|
||||
/// the cache changes its folder (`lightcraft_preview::DiskCache::set_gate`).
|
||||
pub fn attach_disk_cache(&mut self, dir: &std::path::Path, disk_bytes: u64, gate: Option<WriteGate>) {
|
||||
self.rendered.retire();
|
||||
self.rendered = Arc::new(PreviewCache::with_disk(rendered_budget(self.budget), dir, disk_bytes));
|
||||
let cache = PreviewCache::with_disk(rendered_budget(self.budget), dir, disk_bytes);
|
||||
if let (Some(disk), Some(gate)) = (cache.disk(), gate) {
|
||||
disk.set_gate(gate);
|
||||
}
|
||||
self.rendered = Arc::new(cache);
|
||||
}
|
||||
|
||||
/// Change the disk budget of the cache in `dir`: in place when that cache is attached (its
|
||||
/// thumbnails are keyed by content, so they and the textures shown from them stay valid,
|
||||
/// and the same cache object keeps its generation), else attach it.
|
||||
pub fn set_disk_cache_bytes(&mut self, dir: &std::path::Path, disk_bytes: u64) {
|
||||
/// and the same cache object keeps its generation), else attach it (with `gate`).
|
||||
pub fn set_disk_cache_bytes(&mut self, dir: &std::path::Path, disk_bytes: u64, gate: Option<WriteGate>) {
|
||||
match self.rendered.disk().filter(|d| d.dir() == dir) {
|
||||
Some(d) => d.set_budget(disk_bytes),
|
||||
None => self.attach_disk_cache(dir, disk_bytes),
|
||||
None => self.attach_disk_cache(dir, disk_bytes, gate),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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()));
|
||||
|
||||
Loaded 100 of 344 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user