diff --git a/.github/workflows/nix.yml b/.github/workflows/nix.yml index 2fe7cd3ac9..e0443466a6 100644 --- a/.github/workflows/nix.yml +++ b/.github/workflows/nix.yml @@ -23,6 +23,10 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 5 steps: + - name: Harden runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Git dependencies come from storytold run: | diff --git a/.github/workflows/packaging-lint.yml b/.github/workflows/packaging-lint.yml index dff954e931..8114567e50 100644 --- a/.github/workflows/packaging-lint.yml +++ b/.github/workflows/packaging-lint.yml @@ -28,6 +28,10 @@ jobs: runs-on: windows-latest timeout-minutes: 10 steps: + - name: Harden runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Install WiX shell: pwsh @@ -43,6 +47,10 @@ jobs: name: Windows installation policy (no app build) runs-on: windows-latest steps: + - name: Harden runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a9d4d065a3..09fbcd0dc4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -3,6 +3,10 @@ name: Release on: + # A nightly dry run on main: every release build except macOS, nothing signed or released + # (craft-common-actions release.yml `dry-run`). Finds release-only breakage before release day. + schedule: + - cron: "4 2 * * *" push: branches: [release] workflow_dispatch: @@ -12,21 +16,31 @@ on: type: string default: "" -permissions: - contents: write - packages: write +# Nothing by default; each job names what it needs. Only `release` writes (the draft GitHub Release +# and its packages). +permissions: {} concurrency: group: release-${{ github.ref }} cancel-in-progress: false jobs: + # Regenerates contributors/contributors.json for the release commit; every build bundles it. + credits: + permissions: + contents: read + pull-requests: read + uses: storytold/craft-common-actions/.github/workflows/refresh-contributors.yml@main git-sources: name: Git dependencies come from storytold runs-on: ubuntu-latest permissions: contents: read steps: + - name: Harden runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Git dependencies come from storytold run: | @@ -61,15 +75,20 @@ jobs: PY release: - needs: git-sources + needs: [git-sources, credits] + permissions: + contents: write + packages: write # Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos. uses: storytold/craft-common-actions/.github/workflows/release.yml@main with: + dry-run: ${{ github.event_name == 'schedule' }} app-name: lightcraft app-title: LightCraft craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 web-craft-fonts: true version: ${{ inputs.version }} + contributors-artifact: ${{ needs.credits.outputs.artifact-name }} secrets: APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }} APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }} diff --git a/Cargo.lock b/Cargo.lock index acd433bc27..a715ac36cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1199,6 +1199,23 @@ name = "craft-fonts-build" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7" +[[package]] +name = "craft-gainmap" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=430a908f68819bf23d14fe73ab9b18d8f4fd4e5d#430a908f68819bf23d14fe73ab9b18d8f4fd4e5d" +dependencies = [ + "thiserror 2.0.21", +] + +[[package]] +name = "craft-heif" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=430a908f68819bf23d14fe73ab9b18d8f4fd4e5d#430a908f68819bf23d14fe73ab9b18d8f4fd4e5d" +dependencies = [ + "heic-rs", + "rayon", +] + [[package]] name = "craft-i18n" version = "0.1.0" @@ -1222,6 +1239,15 @@ dependencies = [ "writeable", ] +[[package]] +name = "craft-tiff" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=430a908f68819bf23d14fe73ab9b18d8f4fd4e5d#430a908f68819bf23d14fe73ab9b18d8f4fd4e5d" +dependencies = [ + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-ui" version = "0.1.0" @@ -1246,7 +1272,7 @@ dependencies = [ [[package]] name = "craft-update" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=a231e517c3b371e1bf0ee8af71ed807424a9a05d#a231e517c3b371e1bf0ee8af71ed807424a9a05d" +source = "git+https://github.com/storytold/craft-libs?rev=6e03f65bf45181db77ba6229716e6624db9eb25b#6e03f65bf45181db77ba6229716e6624db9eb25b" dependencies = [ "chrono", "dunce", @@ -1256,6 +1282,7 @@ dependencies = [ "image", "log", "plist", + "rustix 1.1.5", "semver", "serde", "serde_json", @@ -3587,7 +3614,7 @@ dependencies = [ [[package]] name = "lightcraft" -version = "0.18.0" +version = "0.21.1" dependencies = [ "chrono", "craft-update", @@ -3616,7 +3643,7 @@ dependencies = [ [[package]] name = "lightcraft-catalog" -version = "0.18.0" +version = "0.21.1" dependencies = [ "chrono", "crc32fast", @@ -3637,7 +3664,7 @@ dependencies = [ [[package]] name = "lightcraft-cli" -version = "0.18.0" +version = "0.21.1" dependencies = [ "dhat", "lightcraft-codecs", @@ -3654,10 +3681,12 @@ dependencies = [ [[package]] name = "lightcraft-codecs" -version = "0.18.0" +version = "0.21.1" dependencies = [ "avif-serialize", "bytemuck", + "craft-gainmap", + "craft-heif", "dhat", "image", "image-webp", @@ -3667,7 +3696,6 @@ dependencies = [ "jxl-oxide", "lightcraft-color", "lightcraft-geom", - "lightcraft-heif", "lightcraft-raster", "lightcraft-testkit", "moxcms 0.9.1", @@ -3685,7 +3713,7 @@ dependencies = [ [[package]] name = "lightcraft-color" -version = "0.18.0" +version = "0.21.1" dependencies = [ "lightcraft-geom", "proptest", @@ -3694,7 +3722,7 @@ dependencies = [ [[package]] name = "lightcraft-denoise" -version = "0.18.0" +version = "0.21.1" dependencies = [ "faer-core", "lightcraft-denoise-core", @@ -3711,7 +3739,7 @@ dependencies = [ [[package]] name = "lightcraft-denoise-core" -version = "0.18.0" +version = "0.21.1" dependencies = [ "dhat", "lightcraft-raster", @@ -3727,7 +3755,7 @@ dependencies = [ [[package]] name = "lightcraft-develop" -version = "0.18.0" +version = "0.21.1" dependencies = [ "lightcraft-color", "lightcraft-geom", @@ -3739,11 +3767,14 @@ dependencies = [ [[package]] name = "lightcraft-engine" -version = "0.18.0" +version = "0.21.1" dependencies = [ "ab_glyph", "chrono", "craft-docpath", + "craft-heif", + "craft-tiff", + "crc32fast", "fs4", "harfrust 0.13.3", "jxl-oxide", @@ -3759,7 +3790,6 @@ dependencies = [ "lightcraft-geocode", "lightcraft-geom", "lightcraft-gpu", - "lightcraft-heif", "lightcraft-lensdb", "lightcraft-merge", "lightcraft-meta", @@ -3771,7 +3801,6 @@ dependencies = [ "lightcraft-segment", "lightcraft-slideshow", "lightcraft-testkit", - "lightcraft-tiff", "log", "miniz_oxide 0.9.1", "rayon", @@ -3785,7 +3814,7 @@ dependencies = [ [[package]] name = "lightcraft-faces" -version = "0.18.0" +version = "0.21.1" dependencies = [ "lightcraft-testkit", "matrixmultiply", @@ -3800,7 +3829,7 @@ dependencies = [ [[package]] name = "lightcraft-fetch" -version = "0.18.0" +version = "0.21.1" dependencies = [ "log", "rustls", @@ -3811,14 +3840,14 @@ dependencies = [ [[package]] name = "lightcraft-film" -version = "0.18.0" +version = "0.21.1" dependencies = [ "libm", ] [[package]] name = "lightcraft-geocode" -version = "0.18.0" +version = "0.21.1" dependencies = [ "miniz_oxide 0.9.1", "serde", @@ -3826,7 +3855,7 @@ dependencies = [ [[package]] name = "lightcraft-geom" -version = "0.18.0" +version = "0.21.1" dependencies = [ "proptest", "serde", @@ -3834,7 +3863,7 @@ dependencies = [ [[package]] name = "lightcraft-gpu" -version = "0.18.0" +version = "0.21.1" dependencies = [ "bytemuck", "lightcraft-color", @@ -3852,24 +3881,16 @@ dependencies = [ "wgpu", ] -[[package]] -name = "lightcraft-heif" -version = "0.18.0" -dependencies = [ - "heic-rs", - "rayon", -] - [[package]] name = "lightcraft-inpaint" -version = "0.18.0" +version = "0.21.1" dependencies = [ "rayon", ] [[package]] name = "lightcraft-lensdb" -version = "0.18.0" +version = "0.21.1" dependencies = [ "lightcraft-develop", "lightcraft-geom", @@ -3878,7 +3899,7 @@ dependencies = [ [[package]] name = "lightcraft-mcp" -version = "0.18.0" +version = "0.21.1" dependencies = [ "lightcraft-codecs", "lightcraft-engine", @@ -3889,7 +3910,7 @@ dependencies = [ [[package]] name = "lightcraft-merge" -version = "0.18.0" +version = "0.21.1" dependencies = [ "lightcraft-codecs", "lightcraft-color", @@ -3905,12 +3926,12 @@ dependencies = [ [[package]] name = "lightcraft-meta" -version = "0.18.0" +version = "0.21.1" dependencies = [ "chrono", + "craft-heif", + "craft-tiff", "lightcraft-geom", - "lightcraft-heif", - "lightcraft-tiff", "proptest", "quick-xml 0.41.0", "serde", @@ -3919,7 +3940,7 @@ dependencies = [ [[package]] name = "lightcraft-pipeline" -version = "0.18.0" +version = "0.21.1" dependencies = [ "dhat", "image", @@ -3938,7 +3959,7 @@ dependencies = [ [[package]] name = "lightcraft-preview" -version = "0.18.0" +version = "0.21.1" dependencies = [ "jpeg-encoder", "lightcraft-raster", @@ -3950,7 +3971,7 @@ dependencies = [ [[package]] name = "lightcraft-raster" -version = "0.18.0" +version = "0.21.1" dependencies = [ "libc", "lightcraft-color", @@ -3962,10 +3983,11 @@ dependencies = [ [[package]] name = "lightcraft-raw" -version = "0.18.0" +version = "0.21.1" dependencies = [ "chrono", "craft-crw", + "craft-tiff", "dhat", "jpeg-encoder", "jxl-oxide", @@ -3974,7 +3996,6 @@ dependencies = [ "lightcraft-meta", "lightcraft-raster", "lightcraft-testkit", - "lightcraft-tiff", "miniz_oxide 0.9.1", "proptest", "rayon", @@ -3988,7 +4009,7 @@ dependencies = [ [[package]] name = "lightcraft-scenes" -version = "0.18.0" +version = "0.21.1" dependencies = [ "image", "lightcraft-color", @@ -3999,7 +4020,7 @@ dependencies = [ [[package]] name = "lightcraft-segment" -version = "0.18.0" +version = "0.21.1" dependencies = [ "candle-core", "candle-nn", @@ -4013,7 +4034,7 @@ dependencies = [ [[package]] name = "lightcraft-slideshow" -version = "0.18.0" +version = "0.21.1" dependencies = [ "lightcraft-raster", "serde", @@ -4022,24 +4043,15 @@ dependencies = [ [[package]] name = "lightcraft-sysmem" -version = "0.18.0" +version = "0.21.1" [[package]] name = "lightcraft-testkit" -version = "0.18.0" - -[[package]] -name = "lightcraft-tiff" -version = "0.18.0" -dependencies = [ - "proptest", - "serde", - "thiserror 2.0.21", -] +version = "0.21.1" [[package]] name = "lightcraft-ui-egui" -version = "0.18.0" +version = "0.21.1" dependencies = [ "craft-ui", "craft-update", @@ -4072,7 +4084,7 @@ dependencies = [ [[package]] name = "lightcraft-web" -version = "0.18.0" +version = "0.21.1" dependencies = [ "crc32fast", "eframe", @@ -4095,7 +4107,7 @@ dependencies = [ [[package]] name = "lightcraft-wincolor" -version = "0.18.0" +version = "0.21.1" dependencies = [ "lightcraft-testkit", "windows-sys 0.61.2", @@ -7914,7 +7926,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7" [[package]] name = "xtask" -version = "0.18.0" +version = "0.21.1" dependencies = [ "brotli", "flate2", diff --git a/Cargo.toml b/Cargo.toml index 8549d6167e..f7dd56589e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "3" members = ["crates/*", "apps/*", "xtask"] [workspace.package] -version = "0.18.0" +version = "0.21.1" edition = "2024" license = "MIT OR Apache-2.0" rust-version = "1.95" @@ -15,8 +15,10 @@ craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-l # Canon CRW decoder shared with the other Craft apps craft-crw = { git = "https://github.com/storytold/craft-libs", rev = "f3b62ec61f45534392376492fc5ad33047375205" } # craft-update: app-specific pretend versions apply to startup and manual checks. -craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "a231e517c3b371e1bf0ee8af71ed807424a9a05d", features = ["egui"] } +craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] } craft-window = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", features = ["egui"] } +# HDR gain maps: ISO 21496-1 and hdrgm metadata, compute and apply (moved from crates/codecs; craft-libs#904). +craft-gainmap = { git = "https://github.com/storytold/craft-libs", rev = "430a908f68819bf23d14fe73ab9b18d8f4fd4e5d" } # internal lightcraft-denoise-core = { path = "crates/denoise-core" } lightcraft-denoise = { path = "crates/denoise", default-features = false } @@ -26,7 +28,8 @@ lightcraft-sysmem = { path = "crates/sysmem" } lightcraft-wincolor = { path = "crates/wincolor" } lightcraft-color = { path = "crates/color" } lightcraft-raster = { path = "crates/raster" } -lightcraft-tiff = { path = "crates/tiff" } +# TIFF/IFD: the shared reader and writer (moved from crates/tiff; craft-libs#783). +craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "430a908f68819bf23d14fe73ab9b18d8f4fd4e5d" } lightcraft-raw = { path = "crates/raw" } lightcraft-codecs = { path = "crates/codecs" } lightcraft-meta = { path = "crates/meta" } @@ -44,7 +47,8 @@ lightcraft-engine = { path = "crates/engine" } lightcraft-ui-egui = { path = "crates/ui-egui" } lightcraft-mcp = { path = "crates/mcp" } lightcraft-fetch = { path = "crates/fetch" } -lightcraft-heif = { path = "crates/heif" } +# HEIC/HEIF: the shared decoder (moved from crates/heif; craft-libs#784). +craft-heif = { git = "https://github.com/storytold/craft-libs", rev = "430a908f68819bf23d14fe73ab9b18d8f4fd4e5d" } lightcraft-segment = { path = "crates/segment" } lightcraft-geocode = { path = "crates/geocode" } lightcraft-slideshow = { path = "crates/slideshow" } diff --git a/apps/lightcraft-cli/Cargo.toml b/apps/lightcraft-cli/Cargo.toml index e55d458b32..380bace5f2 100644 --- a/apps/lightcraft-cli/Cargo.toml +++ b/apps/lightcraft-cli/Cargo.toml @@ -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"] diff --git a/apps/lightcraft-web/Cargo.toml b/apps/lightcraft-web/Cargo.toml index 62fb546991..8946efb048 100644 --- a/apps/lightcraft-web/Cargo.toml +++ b/apps/lightcraft-web/Cargo.toml @@ -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"] diff --git a/apps/lightcraft-web/src/wire.rs b/apps/lightcraft-web/src/wire.rs index e7202bbc59..9854798673 100644 --- a/apps/lightcraft-web/src/wire.rs +++ b/apps/lightcraft-web/src/wire.rs @@ -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() { diff --git a/apps/lightcraft/Cargo.toml b/apps/lightcraft/Cargo.toml index cf8aaf2e06..18024a32f6 100644 --- a/apps/lightcraft/Cargo.toml +++ b/apps/lightcraft/Cargo.toml @@ -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"] diff --git a/apps/lightcraft/src/main.rs b/apps/lightcraft/src/main.rs index 3a7bda6696..97479ad68a 100644 --- a/apps/lightcraft/src/main.rs +++ b/apps/lightcraft/src/main.rs @@ -201,6 +201,10 @@ impl eframe::App for App { // app after this and the session's last writes (face data) still go out under the lock. #[cfg(target_os = "macos")] self.0.session.release_library_lock(); + // macOS only: the process ends below without dropping the app, so a ready update's helper + // starts here. Elsewhere dropping the app starts it. + #[cfg(target_os = "macos")] + self.0.start_pending_update(); // macOS (#1331): end the process here, before eframe tears the window down, as Quit from // the menu bar already does (PR #444). AccessKit's macOS adapter subclasses the window's // content view and restores its class on teardown; AppKit's Touch Bar support observes diff --git a/apps/lightcraft/src/session.rs b/apps/lightcraft/src/session.rs index c02ce47a1e..52a5a5ba03 100644 --- a/apps/lightcraft/src/session.rs +++ b/apps/lightcraft/src/session.rs @@ -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()) })), diff --git a/crates/catalog/src/journal.rs b/crates/catalog/src/journal.rs index 75ec07e59f..fc93ea9f13 100644 --- a/crates/catalog/src/journal.rs +++ b/crates/catalog/src/journal.rs @@ -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)] diff --git a/crates/catalog/src/tests_format_version.rs b/crates/catalog/src/tests_format_version.rs index 612ee4586b..248933a4a2 100644 --- a/crates/catalog/src/tests_format_version.rs +++ b/crates/catalog/src/tests_format_version.rs @@ -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"); } diff --git a/crates/codecs/Cargo.toml b/crates/codecs/Cargo.toml index 589d285fae..019009d7c1 100644 --- a/crates/codecs/Cargo.toml +++ b/crates/codecs/Cargo.toml @@ -12,7 +12,8 @@ workspace = true lightcraft-geom = { workspace = true } lightcraft-color = { workspace = true } lightcraft-raster = { workspace = true } -lightcraft-heif = { workspace = true, optional = true } +craft-gainmap = { workspace = true } +craft-heif = { workspace = true, optional = true } serde = { workspace = true } bytemuck = { workspace = true } thiserror = { workspace = true } @@ -40,10 +41,10 @@ default = ["parallel", "jxl", "avif"] parallel = ["dep:rayon", "lightcraft-raster/parallel", "jpeg-decoder/rayon"] ## JPEG XL decode (jxl-oxide) and, on native targets, encode (jixel); both pure Rust. jxl = ["dep:jxl-oxide", "dep:jixel"] -# HEIC/HEIF decode via the optional lightcraft-heif crate (heic-rs, pure Rust). Off by default so +# HEIC/HEIF decode via the optional craft-heif crate (heic-rs, pure Rust). Off by default so # distributors choose it at build time (HEVC patents are a distributor's call, same policy as # PhotoCraft); `cargo xtask ci` clippies and tests it (`--features heif`), releases don't ship it yet. -heif = ["dep:lightcraft-heif"] +heif = ["dep:craft-heif"] ## AVIF encoding via ravif/rav1e (pure Rust, BSD). Native targets only; a no-op on wasm32. avif = ["dep:ravif", "dep:rav1e", "dep:avif-serialize"] @@ -54,7 +55,7 @@ lightcraft-testkit = { workspace = true } jxl-oxide = { workspace = true } proptest = { workspace = true } # Lossless synthetic HEIC files for the `heif` feature's tests. -lightcraft-heif = { workspace = true, features = ["testdata"] } +craft-heif = { workspace = true, features = ["testdata"] } [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] dhat = { workspace = true } diff --git a/crates/codecs/README.md b/crates/codecs/README.md index b993c52efe..51b07eb66d 100644 --- a/crates/codecs/README.md +++ b/crates/codecs/README.md @@ -48,13 +48,13 @@ CRW, MRW, X3F) are detected so the engine can route them to `lightcraft-raw`; `d |---|---|---|---| | JPEG | yes (zune-jpeg; jpeg-decoder for DCT scaling, CMYK/YCCK, 12-bit, non-interleaved scans) | yes (jpeg-encoder, baseline, 4:4:4/4:2:2/4:2:0) | EXIF (APP1), XMP (APP1, ≤ 64 KiB), ICC (APP2; only complete, consistently counted, uniquely numbered chunk sequences are assembled; malformed profiles use the existing unsupported-profile fallback), Adobe APP14, MPF previews | | PNG | yes, 1–16-bit, palette, tRNS, interlaced | 8/16-bit, gray/GA/RGB/RGBA | iCCP, eXIf, iTXt XMP; sRGB/cICP/cHRM/gAMA honoured when no ICC | -| TIFF | 8/16-bit int (32/64-bit int kept to 16 bits), 16/32/64-bit float, gray/RGB/CMYK/palette, alpha (assoc./unassoc.), planar, LZW/Deflate/PackBits/JPEG | 8/16-bit, 32-bit float; none/LZW/Deflate/PackBits | ICC (34675), XMP (700), Orientation; first IFD only; EXIF not written (use `lightcraft-tiff`) | +| TIFF | 8/16-bit int (32/64-bit int kept to 16 bits), 16/32/64-bit float, gray/RGB/CMYK/palette, alpha (assoc./unassoc.), planar, LZW/Deflate/PackBits/JPEG | 8/16-bit, 32-bit float; none/LZW/Deflate/PackBits | ICC (34675), XMP (700), Orientation; first IFD only; EXIF not written (use `craft-tiff`) | | WebP | lossy + lossless, alpha, first frame of animations | lossless only | ICCP/EXIF/XMP chunks | | GIF / BMP | yes (first GIF frame) | — | via `image` | | PSD / PSB | merged composite: 8/16/32-bit gray, RGB, CMYK, indexed, duotone (as gray); raw/RLE | — | ICC (1039), EXIF (1058), XMP (1060); ZIP-compressed composite, Lab and 1-bit unsupported | | JPEG XL | yes (jxl-oxide, feature `jxl`, default on) | yes (jixel, `encode_jxl`, feature `jxl`, native only): lossy VarDCT / lossless Modular, 8/16-bit, effort (fast / normal), progressive; HDR as 16-bit Rec. 2020 PQ | decode: enum colour → rendered straight to linear Rec.2020 (XYB via linear sRGB primaries); ICC → our ICC path; orientation applied by the decoder (reported as 1). Encode: lossy files declare an enum encoding (sRGB, Display P3, Rec. 2020 BT.709 curve, Rec. 2020 PQ), lossless ones embed the ICC profile; EXIF / XMP in `Exif` / `xml ` boxes | | AVIF | **no** | yes (ravif/rav1e, native only, feature `avif`) | 8/10-bit sRGB, EXIF; no ICC in the muxer. HDR: `encode_avif_hdr` writes 10-bit BT.2020 PQ (SDR white at 203 cd/m², CICP `nclx` + `clli`) or HLG (`hlg_signal`, SDR white at 0.75) | -| HEIC/HEIF | yes with feature `heif` (lightcraft-heif: heic-rs + libheif-matching colour; 4:2:0/mono, 8–10-bit, grids, irot/imir/clap, ICC/nclx/VUI, EXIF/XMP, thumbnail item) | — | 4:2:2/4:4:4 (Canon/Sony HIF) unsupported | +| HEIC/HEIF | yes with feature `heif` (craft-heif: heic-rs + libheif-matching colour; 4:2:0/mono, 8–10-bit, grids, irot/imir/clap, ICC/nclx/VUI, EXIF/XMP, thumbnail item) | — | 4:2:2/4:4:4 (Canon/Sony HIF) unsupported | | Gain map JPEG (HDR) | gain map found and decoded (`gainmap::read_jpeg`): ISO 21496-1 metadata, else Adobe `hdrgm` XMP | yes (`gainmap::encode_jpeg`) | CIPA DC-007 MPF index (gain map typed `0x050000`), ISO 21496-1 APP2 on both images, `hdrgm` + Container XMP for Android/Chrome, Apple `HDRGainMap`/`HDRToneMap` XMP; gain maps are never used as thumbnails | ## Colour diff --git a/crates/codecs/src/encode.rs b/crates/codecs/src/encode.rs index 8aff31c5cb..18e8be64aa 100644 --- a/crates/codecs/src/encode.rs +++ b/crates/codecs/src/encode.rs @@ -39,12 +39,16 @@ impl<'a> EncodeImage<'a> { if self.width == 0 || self.height == 0 { return Err(Error::Encode("zero dimension".into())); } - let need = self.width as u64 * self.height as u64 * self.channels as u64; + // u128: 2^32 x 2^32 x 4 does not fit u64, and every later `width * height * channels` is bounded by this + let need = self.width as u128 * self.height as u128 * self.channels as u128; + if need > usize::MAX as u128 { + return Err(Error::Encode("image too large".into())); + } let have = match self.samples { Samples::U8(s) => s.len(), Samples::U16(s) => s.len(), Samples::F32(s) => s.len(), - } as u64; + } as u128; if have < need { return Err(Error::Encode(format!("sample buffer too short: {have} < {need}"))); } @@ -225,7 +229,7 @@ pub enum TiffCompression { } /// Encode TIFF: 8/16-bit integer or 32-bit float, gray/RGB with optional (unassociated) alpha. -/// ICC and XMP are embedded; EXIF is not (write it through the TIFF writer in `lightcraft-tiff`). +/// ICC and XMP are embedded; EXIF is not (write it through the TIFF writer in `craft-tiff`). pub fn encode_tiff(img: &EncodeImage, compression: TiffCompression, meta: &EncodeMeta) -> Result> { use tiff::encoder::{Compression, DeflateLevel, TiffEncoder}; use tiff::tags::Predictor; @@ -671,6 +675,13 @@ fn avif_10bit(enc: &ravif::Encoder, img: &EncodeImage, s: &[u16]) -> Result 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 { - 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, 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, 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 { - 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 { - 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 { - 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 { - 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 = v.iter().map(|s| s.trim().parse::().ok()).collect::>()?; - 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#"'); - // 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!("")); - for x in v { - s.push_str(&format!("{}", num(*x))); - } - s.push_str(&format!("")); - } - } - s.push_str(""); - 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#""#, - ); - s.push_str("urn:com:apple:photo:2020:aux:hdrgainmap"); - s.push_str(&format!("{format}{format}")); - s.push_str("131072"); - // linear, not log2: the headroom multiplier itself - s.push_str(&format!("{}", num(self.alternate_headroom.exp2()))); - s.push_str(&format!("{}", num(self.alternate_headroom))); - s.push_str(""); - for c in 0..self.channels() { - s.push_str(&format!( - r#"{}{}{}{}{}"#, - 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(""); - s.push_str(&format!("{}", num(self.base_headroom))); - s.push_str("True1"); - s.push_str(""); - 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 = ""; -const XMP_CLOSE: &str = ""; - -/// 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:`: an attribute (`name="v"`) or an element holding an `rdf:Seq`. -fn xmp_values(xmp: &str, name: &str) -> Option> { - 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!(""); - let close = format!(""); - 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("") { - let after = rest.get(a + 8..)?; - let b = after.find("")?; - 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(&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 { - self.take::<1>().map(|a| a[0]) - } - fn u16(&mut self) -> Option { - self.take().map(u16::from_be_bytes) - } - fn u32(&mut self) -> Option { - 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, -} - -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#""#, - r#""#, - r#""#, - r#""#, - r#""# - ), - 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("") - && let (Some(a), Some(b)) = (x.get(..i), x.get(i..)) - { - return format!("{a}{description}{b}"); - } - format!("{XMP_OPEN}{description}{XMP_CLOSE}") -} - /// Offsets within the MPF payload (from "MPF\0"): the endian field, then the index IFD (3 tags). const MPF_ENTRIES_FROM_ENDIAN: usize = 8 + 2 + 3 * 12 + 4; @@ -585,6 +59,10 @@ pub fn encode_jpeg(base: &EncodeImage, gain: &GainImage, m: &GainMapMeta, qualit if !matches!(gain.channels, 1 | 3) || gain.width == 0 || gain.height == 0 || gain.data.len() < gain.width * gain.height * gain.channels { return Err(Error::Encode("gain map image is malformed".into())); } + // (jpeg_par::encode gives an empty JPEG past this, which would be appended as a size-0 gain map) + if gain.width > u16::MAX as usize || gain.height > u16::MAX as usize { + return Err(Error::Encode("JPEG dimensions are limited to 65535".into())); + } if !m.is_valid() || m.multichannel != (gain.channels == 3) { return Err(Error::Encode("gain map metadata doesn't match the map".into())); } @@ -594,11 +72,9 @@ pub fn encode_jpeg(base: &EncodeImage, gain: &GainImage, m: &GainMapMeta, qualit let gain_segs = vec![(0xE1, xmp_segment(&m.to_gain_map_xmp())?), (0xE2, m.to_iso())]; let gain_jpeg = jpeg_par::encode(&gain.data, gain.width, gain.height, gain.channels, quality, ChromaSubsampling::S444, &gain_segs); - let xmp = merge_xmp(meta.xmp, &primary_description(gain_jpeg.len())); + let xmp = craft_gainmap::primary_xmp(meta.xmp, gain_jpeg.len()); let mut segs = jpeg_app_segments(&EncodeMeta { xmp: Some(&xmp), ..*meta })?; - let mut iso_marker = ISO_URN.to_vec(); - iso_marker.extend_from_slice(&[0, 0, 0, 0]); // versions only: "this image has a gain map" - segs.push((0xE2, iso_marker)); + segs.push((0xE2, craft_gainmap::primary_iso_marker())); segs.push((0xE2, mpf_placeholder())); let mut primary = jpeg_par::encode(data, w, h, base.channels as usize, quality, sub, &segs); @@ -668,77 +144,6 @@ pub fn is_gain_map_jpeg(bytes: &[u8]) -> bool { mod tests { use super::*; - fn meta3() -> GainMapMeta { - GainMapMeta { - multichannel: true, - use_base_colour_space: true, - base_headroom: 0.0, - alternate_headroom: 2.5, - min: [-0.25, -0.5, 0.0], - max: [2.5, 2.25, 2.0], - gamma: [1.0, 1.25, 0.75], - base_offset: [0.015625; 3], - alternate_offset: [0.015625, 0.03125, 0.015625], - } - } - - #[test] - fn iso_metadata_round_trips() { - for m in [GainMapMeta { alternate_headroom: 3.0, max: [3.0; 3], ..GainMapMeta::default() }, meta3()] { - let p = m.to_iso(); - assert!(p.starts_with(ISO_URN)); - let back = GainMapMeta::from_iso(&p[ISO_URN.len()..]).unwrap(); - for (a, b) in [(back.min, m.min), (back.max, m.max), (back.gamma, m.gamma), (back.alternate_offset, m.alternate_offset)] { - for c in 0..3 { - assert!((a[c] - b[c]).abs() < 1e-6, "{a:?} {b:?}"); - } - } - assert_eq!(back.multichannel, m.multichannel); - assert!((back.alternate_headroom - m.alternate_headroom).abs() < 1e-6); - } - } - - #[test] - fn iso_common_denominator_and_rejections() { - // common-denominator form, single channel: d = 4, headrooms 0 and 2, min 0, max 2, gamma 1, offsets 0 - let mut p = vec![0, 0, 0, 0, FLAG_COMMON_DENOMINATOR]; - for v in [4u32, 0, 8, 0, 8, 4, 0, 0] { - p.extend_from_slice(&v.to_be_bytes()); - } - let m = GainMapMeta::from_iso(&p).unwrap(); - assert_eq!((m.alternate_headroom, m.max[2], m.gamma[1]), (2.0, 2.0, 1.0)); - // versions only (a primary image's marker), unknown version, truncated, backward - assert!(GainMapMeta::from_iso(&[0, 0, 0, 0]).is_none()); - assert!(GainMapMeta::from_iso(&[0, 1, 0, 0, 0]).is_none()); - assert!(GainMapMeta::from_iso(&p[..p.len() - 1]).is_none()); - let mut back = p.clone(); - back[4] |= FLAG_BACKWARD; - assert!(GainMapMeta::from_iso(&back).is_none()); - // a zero denominator is rejected, not divided by - let mut z = p.clone(); - z[5..9].copy_from_slice(&0u32.to_be_bytes()); - assert!(GainMapMeta::from_iso(&z).is_none()); - } - - #[test] - fn hdrgm_xmp_round_trips_including_per_channel_sequences() { - for m in [GainMapMeta { alternate_headroom: 2.0, max: [2.0; 3], ..GainMapMeta::default() }, meta3()] { - let x = m.to_gain_map_xmp(); - assert!(x.contains("apdi:AuxiliaryImageType")); - if m.multichannel { - assert!(x.contains("2.5"), "{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("").is_none(), "no GainMapMax"); - assert!(GainMapMeta::from_hdrgm_xmp("hdrgm:GainMapMax=\"nope\"").is_none()); - } - fn renditions(w: usize, h: usize) -> (Vec<[f32; 3]>, Vec<[f32; 3]>) { // a ramp: SDR rolls off at 1, HDR keeps going to 6 let hdr: Vec<[f32; 3]> = (0..w * h) @@ -762,28 +167,18 @@ mod tests { } #[test] - fn compute_then_apply_recovers_the_hdr_rendition() { - let (w, h) = (64, 8); - let (sdr, hdr) = renditions(w, h); - for multichannel in [false, true] { - let o = GainMapOptions { scale: 1, multichannel, ..GainMapOptions::default() }; - let (g, m) = compute(&sdr, &hdr, w, h, [0.2126, 0.7152, 0.0722], &o).unwrap(); - assert!(m.is_valid() && m.alternate_headroom > 1.0); - let mut out = sdr.clone(); - apply(&mut out, w, h, &g, &m, 1.0).unwrap(); - for (a, b) in out.iter().zip(&hdr) { - for c in 0..3 { - // 8-bit codes over the gain range: within ~2 % in linear light - assert!((a[c] - b[c]).abs() <= 0.03 * b[c] + 0.01, "{multichannel}: {a:?} vs {b:?}"); - } - } - // weight 0 is the base - let mut base = sdr.clone(); - apply(&mut base, w, h, &g, &m, 0.0).unwrap(); - for (a, b) in base.iter().zip(&sdr) { - assert!((a[0] - b[0]).abs() < 1e-5); - } - } + fn a_gain_map_wider_than_a_jpeg_allows_is_refused() { + let base = [0u8; 3]; + let gain = GainImage { width: 65_536, height: 1, channels: 1, data: vec![0; 65_536] }; + let r = encode_jpeg( + &EncodeImage::new(1, 1, 3, Samples::U8(&base)), + &gain, + &GainMapMeta::default(), + 90, + ChromaSubsampling::S444, + &EncodeMeta::default(), + ); + assert!(matches!(r, Err(Error::Encode(m)) if m.contains("65535"))); } #[test] diff --git a/crates/codecs/src/heif.rs b/crates/codecs/src/heif.rs index 702a0d27d2..2d4d5a7a1d 100644 --- a/crates/codecs/src/heif.rs +++ b/crates/codecs/src/heif.rs @@ -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 { // The container alone: the declared size is checked before any pixel is decoded. - let info = lightcraft_heif::probe(bytes).map_err(err)?; + let info = craft_heif::probe(bytes).map_err(err)?; if (info.width as u64).saturating_mul(info.height as u64) > opts.max_pixels { return Err(Error::TooLarge(info.width as u64, info.height as u64)); } - let options = lightcraft_heif::Options { max_pixels: opts.max_pixels, apply_transforms: true }; + let options = craft_heif::Options { max_pixels: opts.max_pixels, apply_transforms: true, ..craft_heif::Options::default() }; // A small decode (a thumbnail or a cataloguing probe) uses the file's own thumbnail image when // it is at least as large as asked: iPhones store one of 320 × 240, and decoding it is a // hundredth of the work of the 12 MP photo. let thumb = match opts.max_size { Some((mw, mh)) if mw > 0 && mh > 0 && mw.max(mh) < info.width.max(info.height) => { - lightcraft_heif::decode_thumbnail(bytes, mw.max(mh), &options).ok().flatten().filter(|t| { + craft_heif::decode_thumbnail(bytes, mw.max(mh), &options).ok().flatten().filter(|t| { // Only a thumbnail of the same picture: same orientation and aspect within 1 %. let aspect = |w: u32, h: u32| w as f64 / h.max(1) as f64; (aspect(t.width, t.height) / aspect(info.width, info.height) - 1.0).abs() < 0.01 @@ -73,7 +73,7 @@ pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result { }; 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, diff --git a/crates/codecs/src/jpeg_par.rs b/crates/codecs/src/jpeg_par.rs index 763b73afa1..cc2d41629a 100644 --- a/crates/codecs/src/jpeg_par.rs +++ b/crates/codecs/src/jpeg_par.rs @@ -401,6 +401,12 @@ fn encode_with( app_segments: &[(u8, Vec)], optimize: bool, ) -> Vec { + // The public entry takes caller-sized dimensions: refuse (an empty output) what would size an allocation from + // them or index past `data`, rather than panic or allocate for them. + let fits = width.checked_mul(height).and_then(|n| n.checked_mul(channels)).is_some_and(|n| n <= data.len()); + if !fits || width == 0 || height == 0 || width > 65_535 || height > 65_535 || !matches!(channels, 1 | 3 | 4) { + return Vec::new(); + } let gray = channels == 1; let (hs, vs) = match (gray, sub) { (true, _) | (false, ChromaSubsampling::S444) => (1usize, 1usize), @@ -586,6 +592,13 @@ fn encode_with( mod tests { use super::*; + #[test] + fn hostile_dimensions_give_no_output_instead_of_a_panic_or_allocation() { + for (w, h, c) in [(1usize << 40, 1usize << 40, 3usize), (0, 8, 3), (70_000, 2, 3), (8, 8, 7), (8, 8, 3)] { + assert!(encode(&[0u8; 16], w, h, c, 90, ChromaSubsampling::S444, &[]).is_empty(), "{w} x {h} x {c}"); + } + } + fn test_rgb(w: usize, h: usize) -> Vec { let mut v = Vec::with_capacity(w * h * 3); for y in 0..h { diff --git a/crates/codecs/tests/it/heif.rs b/crates/codecs/tests/it/heif.rs index 3364aec4d3..5cd2e63d0b 100644 --- a/crates/codecs/tests/it/heif.rs +++ b/crates/codecs/tests/it/heif.rs @@ -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] diff --git a/crates/develop/src/controls.rs b/crates/develop/src/controls.rs index 9d384f57c4..d3447c2348 100644 --- a/crates/develop/src/controls.rs +++ b/crates/develop/src/controls.rs @@ -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 { - // (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 { diff --git a/crates/develop/src/lib.rs b/crates/develop/src/lib.rs index cb5acab6f5..0e90df716a 100644 --- a/crates/develop/src/lib.rs +++ b/crates/develop/src/lib.rs @@ -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 { diff --git a/crates/develop/src/presets.rs b/crates/develop/src/presets.rs index a1072a89ac..0ea3d12f8c 100644 --- a/crates/develop/src/presets.rs +++ b/crates/develop/src/presets.rs @@ -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> = 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 = out.masks.iter().map(|q| q.id).collect(); for m in masks { let Ok(mut m) = serde_json::from_value::(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 = p.apply(&s, 1.0).masks.iter().map(|m| m.id).collect(); + ids.sort_unstable(); + assert_eq!(ids, [1, 2, 3, u32::MAX]); + } } diff --git a/crates/develop/src/settings.rs b/crates/develop/src/settings.rs index c4197c2911..a4e6eab147 100644 --- a/crates/develop/src/settings.rs +++ b/crates/develop/src/settings.rs @@ -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, + /// 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, + /// 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, + /// 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, +} + +/// 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 { + 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)] diff --git a/crates/develop/src/tests_process.rs b/crates/develop/src/tests_process.rs index f89ebfa115..1694a3ea07 100644 --- a/crates/develop/src/tests_process.rs +++ b/crates/develop/src/tests_process.rs @@ -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)); diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index a01f28510b..1486642429 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -29,6 +29,8 @@ lightcraft-meta = { workspace = true } lightcraft-lensdb = { workspace = true } lightcraft-faces = { workspace = true } miniz_oxide = { workspace = true } +# CRC-32 of a catalog unpacked from a Lightroom backup zip (lightroom_backup.rs). +crc32fast = { workspace = true } lightcraft-merge = { workspace = true } lightcraft-geocode = { workspace = true } lightcraft-slideshow = { workspace = true } @@ -64,6 +66,6 @@ lightcraft-testkit = { workspace = true } # HDR JPEG XL tests read the file's colour encoding jxl-oxide = { workspace = true } # Lossless synthetic HEIC files (the import test; decoded when the codecs' `heif` feature is on). -lightcraft-heif = { workspace = true, features = ["testdata"] } +craft-heif = { workspace = true, features = ["testdata"] } libc = { workspace = true } -lightcraft-tiff = { workspace = true } +craft-tiff = { workspace = true } diff --git a/crates/engine/src/cmd/curves.rs b/crates/engine/src/cmd/curves.rs index 7388f8e573..0e52ad0929 100644 --- a/crates/engine/src/cmd/curves.rs +++ b/crates/engine/src/cmd/curves.rs @@ -311,6 +311,13 @@ fn import_presets(s: &mut Session, p: &Value) -> Result { 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 = 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 { 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])), diff --git a/crates/engine/src/cmd/develop.rs b/crates/engine/src/cmd/develop.rs index 5403ad6eb2..5223710ed6 100644 --- a/crates/engine/src/cmd/develop.rs +++ b/crates/engine/src/cmd/develop.rs @@ -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 { s.active().ok_or_else(|| bad(c, "no active photo")) } @@ -482,6 +485,150 @@ pub fn specs() -> Vec { 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 = 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); diff --git a/crates/engine/src/cmd/library/specs.rs b/crates/engine/src/cmd/library/specs.rs index c725ac216b..bbd618f9f1 100644 --- a/crates/engine/src/cmd/library/specs.rs +++ b/crates/engine/src/cmd/library/specs.rs @@ -1302,10 +1302,13 @@ pub fn specs() -> Vec { }), // ---- 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::>(),"warnings":data.warnings,"skipped":data.skipped})) }), cmd!( @@ -1313,7 +1316,7 @@ pub fn specs() -> Vec { "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"))?; diff --git a/crates/engine/src/cmd/lut_profiles.rs b/crates/engine/src/cmd/lut_profiles.rs index 3f3c2b3428..50cb122300 100644 --- a/crates/engine/src/cmd/lut_profiles.rs +++ b/crates/engine/src/cmd/lut_profiles.rs @@ -103,6 +103,10 @@ fn import(s: &mut Session, p: &Value) -> Result { } 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 = s.lut_profiles.iter().map(|x| x.id.clone()).collect(); + let mut next_suffix: std::collections::HashMap = 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 { 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:"))); diff --git a/crates/engine/src/cmd/manage.rs b/crates/engine/src/cmd/manage.rs index 9b1f7e9f74..6e4cdfb105 100644 --- a/crates/engine/src/cmd/manage.rs +++ b/crates/engine/src/cmd/manage.rs @@ -64,6 +64,9 @@ fn set_names_ops(s: &crate::Session, names: &[String; 5]) -> Vec { .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 { 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()))?; diff --git a/crates/engine/src/cmd/mask_effect_files.rs b/crates/engine/src/cmd/mask_effect_files.rs index e1e099bc8a..0b36c73d17 100644 --- a/crates/engine/src/cmd/mask_effect_files.rs +++ b/crates/engine/src/cmd/mask_effect_files.rs @@ -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, 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 = 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 { 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()})) } diff --git a/crates/engine/src/cmd/mask_effects.rs b/crates/engine/src/cmd/mask_effects.rs index d791b42606..529566f3bb 100644 --- a/crates/engine/src/cmd/mask_effects.rs +++ b/crates/engine/src/cmd/mask_effects.rs @@ -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 { diff --git a/crates/engine/src/cmd/masks.rs b/crates/engine/src/cmd/masks.rs index 171435e22b..e4017f4003 100644 --- a/crates/engine/src/cmd/masks.rs +++ b/crates/engine/src/cmd/masks.rs @@ -98,6 +98,10 @@ fn resolve_ai(s: &mut Session, shape: &mut MaskShape, c: &str) -> Result 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 Vec { "Set Mask Adjustments", [], None, - "{id?, values: {exposure, contrast, highlights, shadows, whites, blacks, temp, tint, texture, clarity, dehaze, hue, saturation, sharpness, noise, moire, defringe, color_hue, color_sat, amount}}", + "{id?, values: {exposure, contrast, highlights, shadows, whites, blacks, temp, tint, texture, clarity, dehaze, hue, saturation, sharpness, noise, moire, defringe, lens_blur, lens_blur_off, lens_blur_outside, lens_blur_boost, lens_blur_transition, color_hue, color_sat, amount}}", has_active, |s, p| { let vals = p.get("values").cloned().ok_or_else(|| bad("mask.adjust", "missing values"))?; @@ -1147,6 +1155,9 @@ pub(crate) fn clamp_local(mut a: LocalAdjustments) -> LocalAdjustments { ] { c(v, -100.0, 100.0); } + c(&mut a.lens_blur, 0.0, 100.0); + c(&mut a.lens_blur_boost, 0.0, 100.0); + c(&mut a.lens_blur_transition, 0.0, 100.0); c(&mut a.color_hue, 0.0, 360.0); c(&mut a.color_sat, 0.0, 100.0); c(&mut a.amount, 0.0, 200.0); @@ -1155,7 +1166,7 @@ pub(crate) fn clamp_local(mut a: LocalAdjustments) -> LocalAdjustments { /// `mask.brushStroke`'s `pressure`: one number 0..1 per point (clamped), at most one per point; absent or `null` /// is no pressure data. -fn pressure_list(v: Option<&Value>, points: usize) -> Result> { +pub(super) fn pressure_list(v: Option<&Value>, points: usize) -> Result> { 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 { diff --git a/crates/engine/src/cmd/preset_files.rs b/crates/engine/src/cmd/preset_files.rs index b64a69fc76..9c555f9fb5 100644 --- a/crates/engine/src/cmd/preset_files.rs +++ b/crates/engine/src/cmd/preset_files.rs @@ -23,6 +23,8 @@ fn import(s: &mut Session, p: &Value) -> Result { // .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 { 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 { } } } + // 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})) diff --git a/crates/engine/src/cmd/print.rs b/crates/engine/src/cmd/print.rs index 1390ca1677..51f094866e 100644 --- a/crates/engine/src/cmd/print.rs +++ b/crates/engine/src/cmd/print.rs @@ -363,7 +363,7 @@ fn template_import(s: &mut Session, p: &Value) -> Result { 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"))); diff --git a/crates/engine/src/crs_masks.rs b/crates/engine/src/crs_masks.rs index a54bd620d5..2bc2298573 100644 --- a/crates/engine/src/crs_masks.rs +++ b/crates/engine/src/crs_masks.rs @@ -274,15 +274,20 @@ pub fn masks(values: &Values, aspect: f64) -> (Vec, Vec) { skipped.extend(unmapped_local(c)); // brush dabs of one correction are one brush component let mut merged: Vec = Vec::new(); + // where the brush of each op sits in `merged` (no scan of it per dab) + let mut brush_at: std::collections::HashMap = 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; diff --git a/crates/engine/src/display.rs b/crates/engine/src/display.rs index 34840e46d3..d9fa767207 100644 --- a/crates/engine/src/display.rs +++ b/crates/engine/src/display.rs @@ -29,11 +29,7 @@ impl Display { /// Load the ICC profile at `path`. pub fn load(path: &str) -> Result { 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) } diff --git a/crates/engine/src/export/params.rs b/crates/engine/src/export/params.rs index 34397fad3c..aef0fb001a 100644 --- a/crates/engine/src/export/params.rs +++ b/crates/engine/src/export/params.rs @@ -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('.'); diff --git a/crates/engine/src/export/tests.rs b/crates/engine/src/export/tests.rs index d1d387b26f..c4dc644fd6 100644 --- a/crates/engine/src/export/tests.rs +++ b/crates/engine/src/export/tests.rs @@ -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); +} diff --git a/crates/engine/src/export/watermark.rs b/crates/engine/src/export/watermark.rs index c074808eae..630c2ae0c8 100644 --- a/crates/engine/src/export/watermark.rs +++ b/crates/engine/src/export/watermark.rs @@ -236,12 +236,20 @@ pub(crate) fn watermark_logo(path: &str) -> Option> { 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 { - 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))) } diff --git a/crates/engine/src/files/fs.rs b/crates/engine/src/files/fs.rs index e839873cf0..bb25aff888 100644 --- a/crates/engine/src/files/fs.rs +++ b/crates/engine/src/files/fs.rs @@ -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(path: &str, max_edge: usize, load: impl FnOnce(Vec) -> 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(); diff --git a/crates/engine/src/files/tests.rs b/crates/engine/src/files/tests.rs index 599d173f7d..ff50d94ab6 100644 --- a/crates/engine/src/files/tests.rs +++ b/crates/engine/src/files/tests.rs @@ -281,7 +281,7 @@ fn tiff_with_preview_strip(strip: &[u8], tail: usize, what: Fixture) -> Vec /// [`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 { - 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| { 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 { - 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 = (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 { - 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(); diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 53077f95c6..dd67b755f9 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -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; diff --git a/crates/engine/src/lightroom_archive.rs b/crates/engine/src/lightroom_archive.rs index 9c9979d970..511f81f308 100644 --- a/crates/engine/src/lightroom_archive.rs +++ b/crates/engine/src/lightroom_archive.rs @@ -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))])], diff --git a/crates/engine/src/lightroom_backup.rs b/crates/engine/src/lightroom_backup.rs new file mode 100644 index 0000000000..ab51ffd3de --- /dev/null +++ b/crates/engine/src/lightroom_backup.rs @@ -0,0 +1,558 @@ +//! Lightroom Classic catalog backups (issue #2592): `Backups/YYYY-MM-DD HHMM/.zip`, which +//! holds the catalog `.lrcat` (and, since Classic 12, a small `.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: `/Backups//.zip` (Lightroom's default +/// layout) → `/.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 { + 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 { + 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 { + 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, 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, 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) -> 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 { + 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 { + 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 { + 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 = (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)> = 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, 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(); + } +} diff --git a/crates/engine/src/lightroom_catalog.rs b/crates/engine/src/lightroom_catalog.rs index f1602eabde..9a84b37ddb 100644 --- a/crates/engine/src/lightroom_catalog.rs +++ b/crates/engine/src/lightroom_catalog.rs @@ -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 { + 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) -> Result, 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, + /// 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, pub photos: Vec, pub collections: Vec, pub members: Vec, @@ -215,6 +234,11 @@ pub enum SkipReason { pub(crate) struct ImportIndex { pub(crate) photos: BTreeMap, pub(crate) collections: BTreeMap, + /// 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, } pub(crate) fn load_index(path: Option<&Path>) -> crate::Result { @@ -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 { /// 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 { + 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 { + 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 { // 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 { 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(); diff --git a/crates/engine/src/lightroom_catalog/tests.rs b/crates/engine/src/lightroom_catalog/tests.rs index f1d0dd1fc6..837db27c90 100644 --- a/crates/engine/src/lightroom_catalog/tests.rs +++ b/crates/engine/src/lightroom_catalog/tests.rs @@ -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//.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>)> = 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)], l /// table's root page. fn native_catalog_rows(tables: &[(&str, &str, Vec>)], last_page: Option) -> Vec { 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>)], 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(); diff --git a/crates/engine/src/lightroom_folder.rs b/crates/engine/src/lightroom_folder.rs index 0b5a85659a..5bd2c52d3a 100644 --- a/crates/engine/src/lightroom_folder.rs +++ b/crates/engine/src/lightroom_folder.rs @@ -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 { { 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 { + 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> { + let entries = std::fs::read_dir(backups).ok()?; + let zips: Vec = + 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) +} diff --git a/crates/engine/src/lightroom_job.rs b/crates/engine/src/lightroom_job.rs index 762c8588df..defdb850f0 100644 --- a/crates/engine/src/lightroom_job.rs +++ b/crates/engine/src/lightroom_job.rs @@ -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(), diff --git a/crates/engine/src/media/render.rs b/crates/engine/src/media/render.rs index bd8ee5292f..af7d10e268 100644 --- a/crates/engine/src/media/render.rs +++ b/crates/engine/src/media/render.rs @@ -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) diff --git a/crates/engine/src/originals.rs b/crates/engine/src/originals.rs index 7a82b3a93c..e16a4963f8 100644 --- a/crates/engine/src/originals.rs +++ b/crates/engine/src/originals.rs @@ -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"").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())); diff --git a/crates/engine/src/preset_import.rs b/crates/engine/src/preset_import.rs index d1dbc946c6..f224c39972 100644 --- a/crates/engine/src/preset_import.rs +++ b/crates/engine/src/preset_import.rs @@ -42,7 +42,13 @@ pub fn read_zip(bytes: &[u8]) -> Result)>, String> { read_zip_limited(bytes, MAX_ZIP_ENTRY, MAX_ZIP_TOTAL) } -fn read_zip_limited(bytes: &[u8], max_entry: usize, max_total: usize) -> Result)>, String> { +pub(crate) fn read_zip_limited(bytes: &[u8], max_entry: usize, max_total: usize) -> Result)>, String> { + read_zip_spending(bytes, max_entry, max_total, &mut 0) +} + +/// [`read_zip_limited`] that adds to `spent` the bytes of each entry as it is unpacked, so they +/// are counted when a later entry fails; an entry that fails for size spends its whole limit. +pub(crate) fn read_zip_spending(bytes: &[u8], max_entry: usize, max_total: usize, spent: &mut usize) -> Result)>, String> { let u16_at = |o: usize| bytes.get(o..o.checked_add(2)?).map(|b| u16::from_le_bytes([b[0], b[1]]) as usize); let u32_at = |o: usize| bytes.get(o..o.checked_add(4)?).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize); // offsets from the file, added up without overflowing (a 32-bit build reads them too) @@ -53,7 +59,7 @@ fn read_zip_limited(bytes: &[u8], max_entry: usize, max_total: usize) -> Result< let count = u16_at(eocd + 10).ok_or("bad zip")?; let mut at = u32_at(eocd + 16).ok_or("bad zip")?; let mut out = Vec::new(); - let mut total = 0usize; + let total = spent; for _ in 0..count { if !bytes.get(at..).is_some_and(|b| b.starts_with(b"PK\x01\x02")) { return Err("bad zip central directory".into()); @@ -76,18 +82,40 @@ fn read_zip_limited(bytes: &[u8], max_entry: usize, max_total: usize) -> Result< } let data_at = add(add(add(local, 30)?, u16_at(add(local, 26)?).ok_or("bad zip")?)?, u16_at(add(local, 28)?).ok_or("bad zip")?)?; let data = bytes.get(data_at..add(data_at, csize)?).ok_or_else(|| format!("{name}: truncated"))?; - let left = max_total.saturating_sub(total); + let left = max_total.saturating_sub(*total); let limit = max_entry.min(left); let too_big = || format!("{name}: unpacks to more than {} MB, too much for a preset", limit >> 20); let content = match method { - 0 if data.len() > limit => return Err(too_big()), + 0 if data.len() > limit => { + *total = total.saturating_add(limit); + return Err(too_big()); + } 0 => data.to_vec(), - 8 => miniz_oxide::inflate::decompress_to_vec_with_limit(data, usize_.max(1).saturating_mul(2).saturating_add(1024).min(limit)).map_err( - |e| if matches!(e.status, miniz_oxide::inflate::TINFLStatus::HasMoreOutput) { too_big() } else { format!("{name}: {e:?}") }, - )?, + 8 => { + // the entry's own stated size, with slack, caps the inflate below `limit` + let cap = usize_.max(1).saturating_mul(2).saturating_add(1024); + match miniz_oxide::inflate::decompress_to_vec_with_limit(data, cap.min(limit)) { + Ok(v) => v, + Err(e) if matches!(e.status, miniz_oxide::inflate::TINFLStatus::HasMoreOutput) && cap < limit => { + // not over the budget: only what was unpacked is spent + *total = total.saturating_add(e.output.len()); + return Err(format!("{name}: unpacks to more than its stated size")); + } + Err(e) if matches!(e.status, miniz_oxide::inflate::TINFLStatus::HasMoreOutput) => { + // past the budget (or the entry limit): its whole limit is spent + *total = total.saturating_add(limit); + return Err(too_big()); + } + Err(e) => { + // what a corrupt stream unpacked before failing was still unpacked: charge it + *total = total.saturating_add(e.output.len()); + return Err(format!("{name}: {:?}", e.status)); + } + } + } m => return Err(format!("{name}: unsupported compression method {m}")), }; - total = total.saturating_add(content.len()); + *total = total.saturating_add(content.len()); out.push((name, content)); } Ok(out) @@ -139,7 +167,11 @@ impl LuaParser<'_> { if self.s[self.i..].starts_with(b"--") { self.i += 2; if let Some(level) = self.long_bracket_level() { - let _ = self.long_string(level); + // an unterminated long comment runs to the end (as in Lua): going on past its + // opening would search the rest of the file again for every such opening + if self.long_string(level).is_err() { + self.i = self.s.len(); + } } else { while self.i < self.s.len() && self.s[self.i] != b'\n' { self.i += 1; @@ -546,14 +578,23 @@ pub fn read_presets(name: &str, bytes: &[u8], group: Option) -> Result { let mut out = Vec::new(); let mut errors = Vec::new(); - for (inner, data) in read_zip(bytes)? { + let entries = read_zip(bytes)?; + // what the archive's entries left of the unpack budget: the Luminar packs inside it + // (archives themselves) unpack within it, not each with a budget of its own + let mut budget = MAX_ZIP_TOTAL.saturating_sub(entries.iter().map(|(_, d)| d.len()).sum()); + for (inner, data) in entries { let iext = Path::new(&inner).extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).unwrap_or_default(); if !(PRESET_EXTS.contains(&iext.as_str()) || iext == crate::presets::LCPRESET_EXT) || iext == "zip" { continue; } let dir_group = inner.rsplit_once('/').and_then(|(d, _)| group_from_dir(d)).or_else(|| group_from_dir(&stem)).or_else(|| group.clone()); - match read_presets(&inner, &data, dir_group) { + let read = if iext == "mplumpack" { + crate::preset_luminar::read_mplumpack_within(&inner, &data, &mut budget) + } else { + read_presets(&inner, &data, dir_group) + }; + match read { Ok(v) => out.extend(v), Err(e) => errors.push(format!("{inner}: {e}")), } @@ -771,6 +812,68 @@ text]], z = ZSTR "loc" }"#, assert!(photo(r#"crs:WhiteBalance="As Shot""#).unmapped.is_empty()); } + #[test] + fn packs_inside_a_zip_share_its_unpack_budget() { + // two packs unpacking to 2 MB each on a 3 MB budget: the first spends it, the second is refused + let pad = vec![b' '; 2 << 20]; + let pack = zip(&[("a.lmp", b""), ("pad.txt", &pad)], true); + let mut budget = 3 << 20; + let first = crate::preset_luminar::read_mplumpack_within("one.mplumpack", &pack, &mut budget).unwrap_err(); + assert!(!first.contains("too much"), "{first}"); + assert_eq!(budget, (1 << 20) - 8); + let second = crate::preset_luminar::read_mplumpack_within("two.mplumpack", &pack, &mut budget).unwrap_err(); + assert!(second.contains("too much"), "{second}"); + } + + #[test] + fn a_pack_that_fails_part_way_still_spends_what_it_unpacked() { + // 2 MB unpacks, then an entry that does not fit the 3 MB budget: the next pack gets nothing + let pad = vec![b' '; 2 << 20]; + let pack = zip(&[("a.txt", &pad), ("b.txt", &pad)], true); + let mut budget = 3 << 20; + let first = crate::preset_luminar::read_mplumpack_within("one.mplumpack", &pack, &mut budget).unwrap_err(); + assert!(first.contains("too much"), "{first}"); + assert_eq!(budget, 0); + } + + #[test] + fn a_corrupt_deflate_entry_spends_what_it_unpacked() { + // 2 MB declared, the stream cut in half: it unpacks a part, then fails; the budget keeps the part + let pad = vec![b' '; 2 << 20]; + let deflated = miniz_oxide::deflate::compress_to_vec(&pad, 6); + let mut pack = zip(&[("a.txt", &deflated[..deflated.len() / 2])], false); + let central = 30 + "a.txt".len() + deflated.len() / 2; + pack[8] = 8; // local header: method deflate + pack[central + 10] = 8; // central header: method deflate + pack[central + 24..central + 28].copy_from_slice(&(pad.len() as u32).to_le_bytes()); + let mut budget = 3 << 20; + let err = crate::preset_luminar::read_mplumpack_within("one.mplumpack", &pack, &mut budget).unwrap_err(); + assert!(!err.contains("too much"), "{err}"); + assert!(budget < 3 << 20, "{budget}"); + } + + #[test] + fn an_unterminated_long_comment_ends_the_template() { + // 20 000 openings of a long comment that never closes: each used to search the rest of + // the file for its close (seconds to minutes); now the first ends the file + let text = format!("s = {{ a = 1 }} {}", "--[==[ ".repeat(20_000)); + let t = std::time::Instant::now(); + let _ = parse_lua(&text); + assert!(t.elapsed().as_secs() < 5, "took {:?}", t.elapsed()); + } + + #[test] + fn an_entry_past_its_stated_size_spends_only_what_it_unpacked() { + // the stated size (1 byte) caps the inflate at 1026 bytes, far below the 1 MB limit + let mut lying = zip(&[("a.xmp", &vec![b' '; 10_000])], true); + let cd = lying.windows(4).position(|w| w == b"PK\x01\x02").unwrap(); + lying[cd + 24..cd + 28].copy_from_slice(&1u32.to_le_bytes()); + let mut spent = 0; + let e = read_zip_spending(&lying, 1 << 20, 1 << 20, &mut spent).unwrap_err(); + assert!(e.contains("stated size"), "{e}"); + assert!(spent > 0 && spent <= 2048, "{spent}"); + } + #[test] fn zip_bombs_are_refused() { // an entry unpacking past the limit (3 MB of zeros pack into a few kB): refused, named diff --git a/crates/engine/src/preset_luminar.rs b/crates/engine/src/preset_luminar.rs index 73b008d9ff..5a4fb21582 100644 --- a/crates/engine/src/preset_luminar.rs +++ b/crates/engine/src/preset_luminar.rs @@ -19,7 +19,7 @@ use std::collections::BTreeMap; use std::path::Path; use crate::crs::Props; -use crate::preset_import::{Imported, build, group_from_dir, read_zip}; +use crate::preset_import::{Imported, MAX_ZIP_ENTRY, MAX_ZIP_TOTAL, build, group_from_dir, read_zip_spending}; // ----------------------------------------------------------------------------- property lists @@ -72,13 +72,26 @@ struct Parser<'a> { /// Deepest `` / `` nesting read (real looks nest a handful of levels). const MAX_DEPTH: usize = 64; +/// Index of the `;` that ends a reference starting at `rest` (its `&`). The scan stops at the first +/// byte that cannot appear in a name or a numeric reference, so zero-padded references such as +/// `a` are found and each run of name bytes is read once, not once per `&`. +fn entity_end(rest: &str) -> Option { + let n = rest.bytes().skip(1).take_while(|b| b.is_ascii_alphanumeric() || *b == b'#').count(); + (rest.as_bytes().get(n + 1) == Some(&b';')).then_some(n + 1) +} + fn unescape(t: &str) -> String { let mut out = String::with_capacity(t.len()); let mut rest = t; while let Some(p) = rest.find('&') { out.push_str(&rest[..p]); rest = &rest[p..]; - let Some(end) = rest.find(';') else { break }; + // no `;` within a reference's reach: the `&` is kept and the search goes on after it + let Some(end) = entity_end(rest) else { + out.push('&'); + rest = &rest[1..]; + continue; + }; let ent = &rest[1..end]; let c = match ent { "amp" => Some('&'), @@ -335,11 +348,13 @@ struct Acc { sums: BTreeMap, curves: BTreeMap>, unmapped: Vec, + /// What `unmapped` holds, for the look-up (a look names each unsupported setting once). + seen: std::collections::HashSet, } impl Acc { fn skip(&mut self, what: String) { - if !self.unmapped.contains(&what) { + if self.seen.insert(what.clone()) { self.unmapped.push(what); } } @@ -455,7 +470,18 @@ pub fn read_lmp(path: &str, bytes: &[u8], group: Option) -> Result Result, String> { - let entries = read_zip(bytes)?; + let mut budget = MAX_ZIP_TOTAL; + read_mplumpack_within(path, bytes, &mut budget) +} + +/// [`read_mplumpack`] unpacking at most `budget` bytes, which it spends: packs inside one zip +/// share the zip's budget. +pub(crate) fn read_mplumpack_within(path: &str, bytes: &[u8], budget: &mut usize) -> Result, String> { + // spent as each entry unpacks: a pack that fails part way still used its budget + let mut spent = 0; + let entries = read_zip_spending(bytes, MAX_ZIP_ENTRY, *budget, &mut spent); + *budget = budget.saturating_sub(spent); + let entries = entries?; let info = entries .iter() .find(|(n, _)| n.rsplit('/').next().is_some_and(|b| b.eq_ignore_ascii_case("PresetsInfo.plist"))) @@ -486,6 +512,19 @@ mod tests { use crate::preset_import::read_presets; use serde_json::json; + #[test] + fn many_ampersands_before_one_semicolon_are_read_once() { + let hostile = format!("{};&", "&".repeat(200_000)); + assert_eq!(unescape(&hostile), format!("{};&", "&".repeat(200_000))); + assert_eq!(unescape("<a> 􏿿 &unknownreference;"), " \u{10FFFF} &unknownreference;"); + } + + #[test] + fn zero_padded_numeric_references_decode() { + assert_eq!(unescape("Canon A A"), "Canon A A"); + assert_eq!(unescape("&abc&#xyz;&;"), "&abc&#xyz;&;"); + } + #[test] fn unescape_rejects_signed_numeric_references() { assert_eq!(unescape("&#+65;&#x+41;"), "&#+65;&#x+41;"); diff --git a/crates/engine/src/presets.rs b/crates/engine/src/presets.rs index 2ad6e5d2ef..a1aa603c83 100644 --- a/crates/engine/src/presets.rs +++ b/crates/engine/src/presets.rs @@ -198,40 +198,62 @@ impl Session { /// Add imported presets. Identical presets (same name, group and settings) already present are /// skipped; id clashes get a fresh id. Returns the ids added. pub fn add_presets(&mut self, presets: Vec) -> Vec { + self.add_presets_each(presets).into_iter().flatten().collect() + } + + /// [`Self::add_presets`] reporting per preset, in order: its id, or `None` when it was skipped + /// as already present. One call indexes the library once, so an importer adds a whole file's + /// presets through it rather than one at a time. + pub fn add_presets_each(&mut self, presets: Vec) -> Vec> { use std::collections::{HashMap, HashSet}; // indexed: a collection of 10 000+ presets was quadratic (minutes) to import let mut ids: HashSet = self.presets.iter().map(|q| q.id.clone()).collect(); - let mut by_name: HashMap<(String, String), Vec> = HashMap::new(); - for (i, q) in self.presets.iter().enumerate() { - by_name.entry((q.name.clone(), q.group.clone())).or_default().push(i); - } - let mut added = Vec::new(); + // (name, group, settings): a preset is looked up rather than compared with every namesake. + // Every number is keyed as an f64 with -0.0 folded into 0.0: `0` and `0.0` are unequal + // `Value`s with equal hashes, so a crafted file of them would collide in one bucket. + // shortcut: integers past 2^53 lose precision in the key, so two such presets are one + let key_of = |p: &Preset| (p.name.clone(), p.group.clone(), canonical_numbers(&p.settings)); + let mut present: HashSet<(String, String, serde_json::Value)> = self.presets.iter().map(key_of).collect(); + // the next suffix to try per generated id, so a batch of clashes doesn't probe from 2 each time + let mut next_suffix: HashMap = HashMap::new(); + let mut added = Vec::with_capacity(presets.len()); for mut p in presets { - let key = (p.name.clone(), p.group.clone()); - let same = by_name.get(&key).is_some_and(|ix| ix.iter().any(|&i| self.presets.get(i).is_some_and(|q| q.settings == p.settings))); - if same { + let key = key_of(&p); + if present.contains(&key) { + added.push(None); continue; } if p.id.trim().is_empty() || p.id.starts_with("lc.") || ids.contains(&p.id) { let base = format!("user.{}", slug(&format!("{}-{}", p.group, p.name))); let mut id = base.clone(); - let mut n = 2; + let n = next_suffix.entry(base.clone()).or_insert(2); while ids.contains(&id) { id = format!("{base}-{n}"); - n += 1; + *n += 1; } p.id = id; } p.builtin = false; ids.insert(p.id.clone()); - by_name.entry(key).or_default().push(self.presets.len()); - added.push(p.id.clone()); + present.insert(key); + added.push(Some(p.id.clone())); self.presets.push(p); } added } } +/// `v` with every number replaced by its f64 (`-0.0` as `0.0`), so equal numbers hash and compare alike. +fn canonical_numbers(v: &serde_json::Value) -> serde_json::Value { + use serde_json::Value; + match v { + Value::Number(n) => n.as_f64().and_then(|f| serde_json::Number::from_f64(f + 0.0)).map_or_else(|| v.clone(), Value::Number), + Value::Array(a) => Value::Array(a.iter().map(canonical_numbers).collect()), + Value::Object(o) => Value::Object(o.iter().map(|(k, x)| (k.clone(), canonical_numbers(x))).collect()), + _ => v.clone(), + } +} + fn p(group: &str, id: &str, name: &str, settings: serde_json::Value) -> Preset { Preset { id: format!("lc.{id}"), name: name.into(), group: group.into(), settings, favorite: false, builtin: true } } diff --git a/crates/engine/src/read_file.rs b/crates/engine/src/read_file.rs new file mode 100644 index 0000000000..2dfb20f2b5 --- /dev/null +++ b/crates/engine/src/read_file.rs @@ -0,0 +1,108 @@ +//! Reads of files named by a user, a sidecar or a library: the OPENED file must be a regular file within a byte +//! cap, and no more than the cap is ever read. (A size taken from the pathname, then a second open and a read to EOF, +//! lets a link, a FIFO, a device or a file that grows in between turn into an unbounded read.) + +use std::io::{self, Read}; +use std::path::Path; + +/// Most bytes of a photo, video or other media file read into memory. +pub(crate) const MAX_MEDIA_BYTES: u64 = 8 << 30; + +/// `O_NONBLOCK` of the platforms that have a FIFO to wait on (std has no name for it, and libc is a +/// dev-dependency only); 0 elsewhere, where a FIFO open still waits for a writer. +#[cfg(unix)] +const O_NONBLOCK: i32 = if cfg!(all( + any(target_os = "linux", target_os = "android"), + not(any( + target_arch = "mips", + target_arch = "mips32r6", + target_arch = "mips64", + target_arch = "mips64r6", + target_arch = "sparc", + target_arch = "sparc64" + )) +)) { + 0x800 +} else if cfg!(any( + target_os = "macos", + target_os = "ios", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd", + target_os = "dragonfly" +)) { + 0x4 +} else { + 0 +}; + +/// Opens `path` for reading without waiting for a FIFO's writer (a FIFO is refused by [`read_open`], not waited on). A +/// regular file is unaffected by the flag. +pub(crate) fn open_for_read(path: impl AsRef) -> io::Result { + let mut options = std::fs::OpenOptions::new(); + options.read(true); + #[cfg(unix)] + std::os::unix::fs::OpenOptionsExt::custom_flags(&mut options, O_NONBLOCK); + options.open(path) +} + +/// The whole of the regular file at `path`, at most `max` bytes. +pub(crate) fn read_regular(path: impl AsRef, max: u64) -> io::Result> { + read_open(open_for_read(path)?, max) +} + +/// The whole of an already opened regular file, at most `max` bytes. +pub(crate) fn read_open(file: std::fs::File, max: u64) -> io::Result> { + let meta = file.metadata()?; + if !meta.is_file() { + return Err(io::Error::new(io::ErrorKind::InvalidInput, "not a regular file")); + } + if meta.len() > max { + return Err(io::Error::new(io::ErrorKind::InvalidData, format!("{} bytes is more than the {max} allowed", meta.len()))); + } + let mut bytes = Vec::with_capacity(usize::try_from(meta.len()).unwrap_or(0)); + file.take(max.saturating_add(1)).read_to_end(&mut bytes)?; + if bytes.len() as u64 > max { + return Err(io::Error::new(io::ErrorKind::InvalidData, format!("more than the {max} bytes allowed"))); + } + Ok(bytes) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_regular_file_within_the_cap_is_read_and_others_are_refused() { + let dir = std::env::temp_dir().join(format!("read-file-{}", std::process::id())); + std::fs::create_dir(&dir).unwrap(); + let f = dir.join("a"); + std::fs::write(&f, b"abcd").unwrap(); + assert_eq!(read_regular(&f, 4).unwrap(), b"abcd"); + assert!(read_regular(&f, 3).is_err()); + assert!(read_regular(&dir, 100).is_err()); + #[cfg(unix)] + { + std::os::unix::fs::symlink("/dev/zero", dir.join("z")).unwrap(); + assert!(read_regular(dir.join("z"), 1 << 20).is_err()); + } + std::fs::remove_dir_all(&dir).unwrap(); + } + + /// A FIFO with no writer is refused, not waited on forever in `open`. + #[cfg(target_os = "linux")] + #[test] + fn a_fifo_without_a_writer_is_refused_without_blocking() { + let dir = std::env::temp_dir().join(format!("read-file-fifo-{}", std::process::id())); + std::fs::create_dir(&dir).unwrap(); + let fifo = dir.join("pipe"); + // (no unsafe code in this crate: the coreutils tool makes the FIFO) + if !std::process::Command::new("mkfifo").arg(&fifo).status().is_ok_and(|s| s.success()) { + eprintln!("skipped: no mkfifo"); + return; + } + let err = read_regular(&fifo, 1 << 20).unwrap_err(); + assert_eq!(err.kind(), io::ErrorKind::InvalidInput, "{err}"); + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/engine/src/rename/tests.rs b/crates/engine/src/rename/tests.rs index fcad01d274..e0326eb548 100644 --- a/crates/engine/src/rename/tests.rs +++ b/crates/engine/src/rename/tests.rs @@ -622,3 +622,14 @@ fn photos_sharing_a_target_name_do_not_rescan_the_suffixes_already_claimed() { assert_eq!(names.len(), 300); assert!(checks.get() <= 3 * 300, "{} disk checks", checks.get()); } + +/// A token repeated many times with a long value is cut at the expansion cap, not multiplied into gigabytes. +#[test] +fn token_expansion_stops_at_the_output_cap() { + let mut p = sample_photo(); + p.meta.title = "é".repeat(100_000); + let out = expand_tokens(&"{title}".repeat(10_000), &p, 1, 1); + assert!(out.len() <= tokens::MAX_EXPANDED_BYTES && out.len() > tokens::MAX_EXPANDED_BYTES - 2, "{}", out.len()); + p.meta.title = "Harbour".into(); + assert_eq!(expand_tokens("a-{title}-b", &p, 1, 1), "a-Harbour-b", "ordinary templates are unchanged"); +} diff --git a/crates/engine/src/rename/tokens.rs b/crates/engine/src/rename/tokens.rs index ee15836e87..1d09088c89 100644 --- a/crates/engine/src/rename/tokens.rs +++ b/crates/engine/src/rename/tokens.rs @@ -205,6 +205,20 @@ pub fn expand(template: &str, p: &Photo, seq: usize) -> String { if ext.is_empty() { stem_out } else { format!("{stem_out}.{ext}") } } +/// Most bytes one expansion yields (far above any file name or overlay line). A token repeated many times +/// with a long metadata value would otherwise multiply into gigabytes; the result is cut at this size. +pub const MAX_EXPANDED_BYTES: usize = 1 << 20; + +/// `s` appended to `out`, cut at a character boundary where `out` would pass [`MAX_EXPANDED_BYTES`]. +fn push_capped(out: &mut String, s: &str) { + let room = MAX_EXPANDED_BYTES.saturating_sub(out.len()); + let mut n = s.len().min(room); + while !s.is_char_boundary(n) { + n -= 1; + } + out.push_str(&s[..n]); +} + /// The template's tokens replaced for `p` (no extension added, nothing sanitized). A bare `{seq}` /// is zero-padded to `seq_width` digits. pub fn expand_tokens(template: &str, p: &Photo, seq: usize, seq_width: usize) -> String { @@ -225,9 +239,12 @@ pub fn expand_tokens_with( let mut out = String::new(); let mut rest = template; while let Some(i) = rest.find('{') { - out.push_str(&rest[..i]); + if out.len() >= MAX_EXPANDED_BYTES { + return out; + } + push_capped(&mut out, &rest[..i]); let Some(j) = rest[i..].find('}') else { - out.push_str(&rest[i..]); + push_capped(&mut out, &rest[i..]); rest = ""; break; }; @@ -263,9 +280,9 @@ pub fn expand_tokens_with( _ => format!("{{{tok}}}"), }, }; - out.push_str(&v); + push_capped(&mut out, &v); rest = &rest[i + j + 1..]; } - out.push_str(rest); + push_capped(&mut out, rest); out } diff --git a/crates/engine/src/segment.rs b/crates/engine/src/segment.rs index 3f95c310bd..9c479cb176 100644 --- a/crates/engine/src/segment.rs +++ b/crates/engine/src/segment.rs @@ -64,6 +64,9 @@ pub(crate) enum Tag { NewPrompt { photo: PhotoId, mask: Option, op: String, name: Option, text: String }, /// A zoomed-in pass for a component, computed from `shape` (without its detail). Detail { photo: PhotoId, mask: u32, comp: usize, shape: MaskShape }, + /// Find Subject for component `comp` of mask `mask` (an Object waiting for it): the people / + /// animals found become a "person" Describe selection; none, the object at `point`. + Subject { photo: PhotoId, mask: u32, comp: usize, point: Point }, } /// The model input: 8-bit RGB. @@ -498,6 +501,9 @@ impl Session { if !clicks.iter().any(|c| c.include) { return Err("click the object to include it first".into()); } + if clicks.len() > MAX_CLICKS { + return Err(format!("an Object selection takes at most {MAX_CLICKS} clicks")); + } #[cfg(feature = "sam")] { self.segment_wait(id, worker::Kind::Clicks(Self::model_clicks(clicks)), None)?.ok_or_else(|| "nothing was selected".to_string()) @@ -570,6 +576,35 @@ impl Session { } } + /// Find Subject for component `comp` of mask `mask` on photo `id` (an Object without clicks): + /// the photo's main people / animals ([`worker::Kind::Subject`]), those the camera's `focus` + /// falls on when the file records it; when the subject is neither, the object at `point`. In + /// background mode [`Session::segment_poll`] applies the result; otherwise it is applied + /// before this returns. + pub(crate) fn segment_subject_later(&mut self, id: PhotoId, mask: u32, comp: usize, point: Point, focus: Option) -> Result<(), String> { + self.segmenter.model_dir()?; + #[cfg(feature = "sam")] + { + let kind = worker::Kind::Subject { focus: focus.map(|p| (p.x as f32, p.y as f32)) }; + let tag = Tag::Subject { photo: id, mask, comp, point }; + if self.segmenter.background { + return self.segment_queue(id, kind, tag, None); + } + let seg = self.segment_wait(id, kind, None)?; + let mut polled = Polled::default(); + self.apply_outcome(worker::Outcome { tag, result: Ok(seg), superseded: false, notice: None }, &mut polled); + match polled.messages.into_iter().next() { + Some(e) => Err(e), + None => Ok(()), + } + } + #[cfg(not(feature = "sam"))] + { + let _ = (id, mask, comp, point, focus); + Err("AI masks are not available in this build".into()) + } + } + /// Start a zoomed-in pass for component `comp` of mask `mask` on photo `id` (an Object or /// Describe selection): the photo around the selection is rendered at a higher resolution /// and segmented again. In background mode [`Session::segment_poll`] applies the result; @@ -706,6 +741,51 @@ impl Session { Err(e) => polled.messages.push(e.to_string()), } } + Tag::Subject { photo, mask, comp, point } => { + let found = match o.result { + Ok(found) => found, + Err(e) => return polled.messages.push(e), + }; + let Some(d) = self.develop_of(photo) else { return }; + let mut d = (*d).clone(); + let Some(c) = d.masks.iter_mut().find(|m| m.id == mask).and_then(|m| m.components.get_mut(comp)) else { return }; + // still the Object waiting for it (not clicked or replaced meanwhile) + let MaskShape::Object { hint, seg: None, edge, .. } = &c.shape else { return }; + if !hint.is_empty() { + return; + } + let edge = *edge; + let seg = match found { + Some(seg) => seg, + // neither a person nor an animal: the object at the point + None if self.segmenter.background => { + if let Err(e) = self.segment_clicks_later(photo, mask, comp, vec![point], Vec::new()) { + polled.messages.push(e); + } + return; + } + None => { + match self.segment_clicks(photo, &clicks_of(&[point], &[])) { + Ok(seg) => { + c.shape = MaskShape::Object { hint: vec![point], exclude: Vec::new(), seg: Some(seg), detail: Vec::new(), edge } + } + Err(e) => return polled.messages.push(e), + } + if let Err(e) = self.set_develop(photo, d, "Find Subject") { + polled.messages.push(e.to_string()); + } else { + polled.changed = true; + } + return; + } + }; + let text = lightcraft_segment::SUBJECT_CONCEPTS.join(", "); + c.shape = MaskShape::Prompt { text, seg: Some(seg), detail: Vec::new(), edge }; + match self.set_develop(photo, d, "Find Subject") { + Ok(()) => polled.changed = true, + Err(e) => polled.messages.push(e.to_string()), + } + } Tag::Detail { photo, mask, comp, shape } => { let patch = match o.result { Ok(Some(p)) => p, @@ -747,6 +827,47 @@ mod tests { assert!(s.segment_poll().messages.is_empty(), "once"); } + /// Find Subject's result: people / animals found become a Describe selection of the waiting + /// Object (one undo step), not after it was clicked meanwhile; nothing found queues the object + /// at the point (here: no model, so a message, and the mask still waits). + #[cfg(feature = "sam")] + #[test] + fn find_subject_results_fill_the_waiting_mask() { + use lightcraft_develop::{Mask, MaskComponent, MaskOp}; + let mut s = Session::with_demo(); + s.segmenter.dir = Some(std::env::temp_dir().join(format!("lc-no-sam3-subject-{}", std::process::id()))); + let id = s.active().unwrap(); + let waiting = || MaskShape::Object { hint: vec![], exclude: vec![], seg: None, detail: vec![], edge: 0.0 }; + let mut d = (*s.develop_of(id).unwrap()).clone(); + d.masks.push(Mask { + id: 3, + components: vec![MaskComponent { name: None, op: MaskOp::Add, invert: false, shape: waiting() }], + ..Default::default() + }); + s.set_develop(id, d, "test").unwrap(); + let point = Point::new(0.4, 0.5); + let send = |s: &mut Session, result| { + let tag = Tag::Subject { photo: id, mask: 3, comp: 0, point }; + s.segmenter.results.0.send(worker::Outcome { tag, result, superseded: false, notice: None }).unwrap(); + s.segment_poll() + }; + let shape = |s: &Session| s.develop_of(id).unwrap().masks[0].components[0].shape.clone(); + // nothing found, no model to click with: said so, and the mask still waits + let polled = send(&mut s, Ok(None)); + assert!(!polled.messages.is_empty() && shape(&s) == waiting(), "{:?}", polled.messages); + // people found: a Describe selection of them + let polled = send(&mut s, Ok(Some(seg_box(20, 5, 5, 15, 15)))); + assert!(polled.changed && polled.messages.is_empty()); + let MaskShape::Prompt { text, seg: Some(_), .. } = shape(&s) else { panic!("{:?}", shape(&s)) }; + assert_eq!(text, "person, animal"); + // a result for a mask that changed meanwhile is dropped + let before = shape(&s); + assert!(!send(&mut s, Ok(Some(seg_box(20, 0, 0, 4, 4)))).changed); + assert_eq!(shape(&s), before); + // an error reaches the user + assert_eq!(send(&mut s, Err("boom".into())).messages, vec!["boom".to_string()]); + } + fn seg_box(side: usize, x0: usize, y0: usize, x1: usize, y1: usize) -> SegMask { let l: Vec = (0..side * side).map(|i| if (x0..x1).contains(&(i % side)) && (y0..y1).contains(&(i / side)) { 8.0 } else { -8.0 }).collect(); diff --git a/crates/engine/src/segment/worker.rs b/crates/engine/src/segment/worker.rs index efbb1c21c4..1e4bf6e8d9 100644 --- a/crates/engine/src/segment/worker.rs +++ b/crates/engine/src/segment/worker.rs @@ -42,6 +42,12 @@ pub enum Kind { Prepare, Clicks(Vec), Text(String), + /// The photo's subject among its people and animals (`Sam3::segment_subject`); `focus`: where + /// the camera focused (normalized), when the file records it. No result: the subject is + /// something else. + Subject { + focus: Option<(f32, f32)>, + }, /// A zoomed-in pass over `region` (normalized) of the photo, rendered by the job's `render`. Detail { prompt: Prompt, @@ -420,6 +426,7 @@ fn remote_job(state: &mut State, shared: &Shared, job: &mut Job, endpoint: &str) let operation = match std::mem::replace(&mut job.kind, Kind::Prepare) { Kind::Prepare => Operation::Prepare, Kind::Text(text) => Operation::Text(text), + Kind::Subject { focus } => Operation::Subject(focus.map(|(x, y)| [x, y])), Kind::Clicks(clicks) => Operation::Clicks(clicks.iter().map(|c| [c.x, c.y, if c.positive { 1.0 } else { 0.0 }]).collect()), Kind::Detail { prompt, region: r } => { region = Some(r); @@ -480,6 +487,11 @@ fn run_job(state: &mut State, shared: &Shared, job: &mut Job, placement: Placeme let (m, enc) = encoded(state, shared, job, placement)?; Ok(text_logits(m, enc, &text)?.map(|l| SegMask::from_logits(side, &l))) } + Kind::Subject { focus } => { + let (m, enc) = encoded(state, shared, job, placement)?; + // (the reference score threshold, as Describe uses) + Ok(m.segment_subject(enc, focus, 0.5)?.map(|p| SegMask::from_logits(side, &p.logits))) + } #[cfg(test)] #[allow(clippy::panic)] Kind::Panic => panic!("a test panic"), diff --git a/crates/engine/src/sidecar.rs b/crates/engine/src/sidecar.rs index b927aa8f31..fdc72522f7 100644 --- a/crates/engine/src/sidecar.rs +++ b/crates/engine/src/sidecar.rs @@ -743,7 +743,7 @@ fn backup(path: &Path, stamp: &str) -> std::io::Result { if !std::fs::symlink_metadata(path)?.is_file() { return Err(std::io::Error::other(format!("{} is not a regular file", path.display()))); } - let bytes = std::fs::read(path)?; + let bytes = crate::read_file::read_regular(path, SIDECAR_READ_MAX)?; let stamp: String = stamp.chars().filter(char::is_ascii_alphanumeric).collect(); let name = path.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default(); for k in 0..1000 { @@ -774,7 +774,7 @@ pub struct SidecarSaved { /// The new contents for the sidecar at `path`: `fresh` merged into the existing file, if any. /// Returns (contents, merged, backup). fn sidecar_contents(path: &Path, fresh: String, stamp: &str) -> std::io::Result<(String, bool, Option)> { - let bytes = match std::fs::read(path) { + let bytes = match crate::read_file::read_regular(path, SIDECAR_READ_MAX) { Ok(b) => b, Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok((fresh, false, None)), // can't see what is there: never overwrite it blindly @@ -784,17 +784,28 @@ fn sidecar_contents(path: &Path, fresh: String, stamp: &str) -> std::io::Result< return Ok((fresh, false, None)); } let rules = lightcraft_meta::MergeRules { owned: OWNED, owned_if_present: OWNED_IF_STATED }; - match std::str::from_utf8(&bytes) - .map_err(|e| e.to_string()) - .and_then(|old| lightcraft_meta::merge_xmp(old, &fresh, rules).map_err(|e| e.to_string())) - { - Ok(merged) => Ok((merged, true, None)), - Err(why) => { - let b = backup(path, stamp)?; - log::warn!("{}: not readable as XMP ({why}); kept a copy as {} before replacing it", path.display(), b.display()); - Ok((fresh, false, Some(b))) - } - } + let why = match std::str::from_utf8(&bytes) { + Ok(old) => match lightcraft_meta::merge_xmp(old, &fresh, rules) { + Ok(merged) => return Ok((merged, true, None)), + // a readable sidecar whose merge fails (e.g. the merged packet exceeds the XMP limits), or one + // that is valid but over the parser's complexity limits: fail the save and keep the file + // as it is, rather than replace metadata that could be read + // (text with no `rdf:RDF` element parses as empty metadata but holds nothing to keep: it is replaced + // after a backup, as any sidecar that is not XMP) + Err(why) + if why == lightcraft_meta::XmpError::TooComplex + || (lightcraft_meta::parse_xmp(old.strip_prefix('\u{feff}').unwrap_or(old)).is_ok() + && !matches!(&why, lightcraft_meta::XmpError::Xml(m) if m.contains("no rdf:RDF element"))) => + { + return Err(std::io::Error::other(format!("the sidecar is readable but could not be merged ({why}); it was left unchanged"))); + } + Err(why) => why.to_string(), + }, + Err(why) => why.to_string(), + }; + let b = backup(path, stamp)?; + log::warn!("{}: not readable as XMP ({why}); kept a copy as {} before replacing it", path.display(), b.display()); + Ok((fresh, false, Some(b))) } /// Key of the stem sidecar a file maps to (case-insensitive). @@ -840,10 +851,10 @@ fn file_path(p: &Photo) -> Option<&str> { pub fn read_packet(original: &str, kind: MediaKind, lookup: SidecarLookup) -> Option<(String, PathBuf)> { if let Some(sc) = lookup.find(original) { // A discovered sidecar keeps precedence even when reading it fails. - return std::fs::read_to_string(&sc).ok().map(|s| (s, sc)); + return crate::read_file::read_regular(&sc, SIDECAR_READ_MAX).ok().and_then(|b| String::from_utf8(b).ok()).map(|s| (s, sc)); } if kind == MediaKind::Raw { - let bytes = std::fs::read(original).ok()?; + let bytes = crate::read_file::read_regular(original, crate::read_file::MAX_MEDIA_BYTES).ok()?; let x = lightcraft_meta::embedded(&bytes).xmp?; return Some((x, PathBuf::from(original))); } @@ -855,6 +866,9 @@ pub fn read_packet(original: &str, kind: MediaKind, lookup: SidecarLookup) -> Op None } +/// Most bytes of a sidecar read whole (the opened file must be a regular file). +const SIDECAR_READ_MAX: u64 = 256 << 20; + const JPEG_XMP_HEADER_SPAN: u64 = 16 * 1024 * 1024; const JPEG_XMP_HEADER_STEPS: usize = 4096; @@ -1592,6 +1606,62 @@ mod tests { std::fs::remove_dir_all(dir).unwrap(); } + /// A readable sidecar whose merged packet fails read-back (too many namespace declarations in scope) is kept: the + /// save fails instead of moving it to a backup and writing fresh content only. + #[test] + fn a_readable_sidecar_that_cannot_be_merged_is_kept() { + let dir = std::env::temp_dir().join(format!("lightcraft-sidecar-keep-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let decls: String = (0..500).map(|i| format!(" xmlns:n{i}=\"urn:n:{i}\"")).collect(); + let old = format!( + "" + ); + assert!(lightcraft_meta::parse_xmp(&old).is_ok(), "the existing sidecar is readable"); + let path = dir.join("a.xmp"); + std::fs::write(&path, &old).unwrap(); + let fresh = lightcraft_meta::write_xmp_lc(&lightcraft_meta::Metadata { rating: Some(3), ..Default::default() }, &[("settings", "{}")]); + assert!(super::sidecar_contents(&path, fresh, "t").is_err()); + assert_eq!(std::fs::read_to_string(&path).unwrap(), old); + assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 1, "no backup was made"); + std::fs::remove_dir_all(dir).unwrap(); + } + + /// Text that is not XMP at all (it parses as empty metadata, but has no `rdf:RDF` element) is backed up and replaced. + #[test] + fn a_sidecar_with_no_rdf_element_is_backed_up_and_replaced() { + let dir = std::env::temp_dir().join(format!("lightcraft-sidecar-plain-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("a.xmp"); + std::fs::write(&path, "not xml at all").unwrap(); + let fresh = lightcraft_meta::write_xmp_lc(&lightcraft_meta::Metadata { rating: Some(3), ..Default::default() }, &[("settings", "{}")]); + let (data, merged, backup) = super::sidecar_contents(&path, fresh.clone(), "t").unwrap(); + assert!(!merged && data == fresh); + assert!(backup.is_some_and(|b| std::fs::read_to_string(b).unwrap() == "not xml at all")); + std::fs::remove_dir_all(dir).unwrap(); + } + + /// A sidecar over the parser's namespace-declaration limit cannot be read or merged: the save fails and the file stays. + #[test] + fn a_sidecar_over_the_complexity_limit_is_kept() { + let dir = std::env::temp_dir().join(format!("lightcraft-sidecar-complex-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let decls: String = (0..600).map(|i| format!(" xmlns:n{i}=\"urn:n:{i}\"")).collect(); + let old = format!( + "" + ); + assert_eq!(lightcraft_meta::parse_xmp(&old).unwrap_err(), lightcraft_meta::XmpError::TooComplex); + let path = dir.join("a.xmp"); + std::fs::write(&path, &old).unwrap(); + let fresh = lightcraft_meta::write_xmp_lc(&lightcraft_meta::Metadata { rating: Some(3), ..Default::default() }, &[("settings", "{}")]); + assert!(super::sidecar_contents(&path, fresh, "t").is_err()); + assert_eq!(std::fs::read_to_string(&path).unwrap(), old); + assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 1, "no backup was made"); + std::fs::remove_dir_all(dir).unwrap(); + } + /// A sidecar written by Lightroom Classic (names flat, paths in `lr:hierarchicalSubject`) /// gives the photo its keywords as paths. #[test] diff --git a/crates/engine/src/slide_template.rs b/crates/engine/src/slide_template.rs index bf56a65694..0ff0c09878 100644 --- a/crates/engine/src/slide_template.rs +++ b/crates/engine/src/slide_template.rs @@ -389,7 +389,7 @@ fn read_user(file: &str, path: &Path) -> Result { if meta.len() > MAX_FILE { return Err(format!("{} bytes is too large for a slideshow template (at most {} KB)", meta.len(), MAX_FILE / 1024)); } - let bytes = std::fs::read(path).map_err(|e| e.to_string())?; + let bytes = crate::read_file::read_regular(path, MAX_FILE).map_err(|e| e.to_string())?; let p = parse(&bytes)?; Ok(Template { id: format!("{USER}{stem}"), @@ -411,7 +411,7 @@ pub fn read_file(path: &Path) -> Result { if meta.len() > MAX_FILE { return Err(format!("{}: {} bytes is too large for a slideshow template (at most {} KB)", path.display(), meta.len(), MAX_FILE / 1024)); } - let bytes = std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))?; + let bytes = crate::read_file::read_regular(path, MAX_FILE).map_err(|e| format!("{}: {e}", path.display()))?; let mut p = parse(&bytes).map_err(|e| format!("{}: {e}", path.display()))?; if p.name.is_none() { let stem = path.file_stem().map(|s| s.to_string_lossy().to_string()).unwrap_or_default(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 00d903a132..958c122871 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -387,6 +387,84 @@ fn permanently_deleting_explicit_targets_keeps_the_surviving_selection() { assert_eq!(s.catalog.to_snapshot(), snapshot); } +/// `lensBlur.brushStroke` adds a stroke to the Focus or the Blur brush (clamped, undoable); hostile +/// input is an error; `lensBlur.brushReset` clears one brush or both. +#[test] +fn lens_blur_brush_strokes_and_reset() { + let mut s = demo(); + let id = s.active().unwrap(); + s.execute( + "lensBlur.brushStroke", + &json!({"mode": "focus", "points": [[0.2, 0.3], [0.25, 0.3]], "size": 9.0, "amount": 40, "feather": f64::NAN, "autoMask": true}), + ) + .unwrap(); + s.execute("lensBlur.brushStroke", &json!({"mode": "blur", "points": [[0.7, 0.6]]})).unwrap(); + let d = s.develop_of(id).unwrap(); + let (f, b) = (&d.lens_blur.focus_strokes, &d.lens_blur.blur_strokes); + assert_eq!((f.len(), b.len()), (1, 1)); + assert_eq!(f[0].points.len(), 2); + assert!(f[0].size == 0.5 && f[0].density == 40.0 && f[0].feather == lightcraft_develop::BrushStroke::default().feather && f[0].auto_mask); + for bad in [ + json!({"mode": "sideways", "points": [[0.5, 0.5]]}), + json!({"mode": "focus", "points": []}), + json!({"mode": "focus"}), + json!({"mode": "blur", "points": [[0.5, 0.5]], "pressure": [0.5, 0.5]}), + ] { + assert!(s.execute("lensBlur.brushStroke", &bad).is_err(), "{bad}"); + } + s.execute("edit.undo", &json!({})).unwrap(); + assert!(s.develop_of(id).unwrap().lens_blur.blur_strokes.is_empty(), "a stroke is one undo step"); + s.execute("lensBlur.brushStroke", &json!({"mode": "blur", "points": [[0.7, 0.6]]})).unwrap(); + s.execute("lensBlur.brushReset", &json!({"mode": "blur"})).unwrap(); + let d = s.develop_of(id).unwrap(); + assert!(d.lens_blur.blur_strokes.is_empty() && d.lens_blur.focus_strokes.len() == 1); + assert!(s.execute("lensBlur.brushReset", &json!({"mode": "nope"})).is_err()); + s.execute("lensBlur.brushReset", &json!({})).unwrap(); + assert!(s.develop_of(id).unwrap().lens_blur.focus_strokes.is_empty()); +} + +/// `lensBlur.findSubject` without the SAM 3 model (or in a build without AI masks) is an error that +/// leaves the photo as it was: no empty mask, the blur not switched on. +#[test] +fn find_subject_without_the_model_changes_nothing() { + let mut s = demo(); + let id = s.active().unwrap(); + // (never the machine's own model: a folder that has none) + s.segmenter.dir = Some(std::env::temp_dir().join(format!("lc-no-sam3-{}", std::process::id()))); + let before = s.develop_of(id).unwrap(); + let e = s.execute("lensBlur.findSubject", &json!({})).unwrap_err(); + assert!(e.to_string().contains("SAM 3") || e.to_string().contains("AI masks"), "{e}"); + let after = s.develop_of(id).unwrap(); + assert!(after.masks.is_empty() && !after.lens_blur.enabled); + assert_eq!(*before, *after); +} + +/// Deleting the mask that is the Lens Blur focus clears the focus, so a new mask that gets the same +/// id doesn't become the focus; undo brings both back. +#[test] +fn deleting_the_focus_mask_clears_the_lens_blur_focus() { + let mut s = demo(); + let id = s.active().unwrap(); + let radial = json!({"kind": "radial", "center": [0.5, 0.5], "rx": 0.2, "ry": 0.2}); + s.execute("mask.add", &radial).unwrap(); + let mid = s.develop_of(id).unwrap().masks[0].id; + s.execute("develop.merge", &json!({"settings": {"lens_blur": {"enabled": true, "amount": 50, "focus_mask": mid}}})).unwrap(); + s.execute("mask.delete", &json!({"id": mid})).unwrap(); + assert_eq!(s.develop_of(id).unwrap().lens_blur.focus_mask, None); + // a new mask takes the freed id, and stays an ordinary mask + s.execute("mask.add", &radial).unwrap(); + let d = s.develop_of(id).unwrap(); + assert_eq!(d.masks[0].id, mid); + assert_eq!(d.lens_blur.focus_mask, None); + // undoing the add and the delete brings the focus back with its mask + s.execute("edit.undo", &json!({})).unwrap(); + s.execute("edit.undo", &json!({})).unwrap(); + assert_eq!(s.develop_of(id).unwrap().lens_blur.focus_mask, Some(mid)); + // other mask edits keep it + s.execute("mask.adjust", &json!({"values": {"exposure": 0.5}})).unwrap(); + assert_eq!(s.develop_of(id).unwrap().lens_blur.focus_mask, Some(mid)); +} + /// `mask.brushStroke {connect: true}` (a ⇧-click) starts from the last point of the mask's last stroke: one straight /// stroke when the brush is unchanged, pieces whose size, feather and flow step from the last stroke's to the new /// values when it changed, and a plain dab when there is nothing to connect to. diff --git a/crates/engine/src/tests/export.rs b/crates/engine/src/tests/export.rs index 1bddd2c44c..ca20da3489 100644 --- a/crates/engine/src/tests/export.rs +++ b/crates/engine/src/tests/export.rs @@ -374,6 +374,8 @@ fn library_with_jpeg(tag: &str) -> (Session, lightcraft_catalog::PhotoId, std::p fn preset_export_preserves_originals(command: &str, selection: serde_json::Value, tag: &str) { let (mut s, _, dir, src, original) = library_with_jpeg(tag); + // something for mask.exportEffects to write (the other commands ignore it) + s.mask_effects.push(crate::cmd::mask_effects::MaskEffect { name: "Glow".into(), ..Default::default() }); let sidecar = src.with_extension("xmp"); let appended_sidecar = dir.join("IMG_1.jpg.xmp"); std::fs::write(&sidecar, b"existing sidecar").unwrap(); @@ -399,6 +401,14 @@ fn preset_export_preserves_originals(command: &str, selection: serde_json::Value assert_eq!(std::fs::read(&sidecar).unwrap(), b"existing sidecar"); assert_eq!(std::fs::read(&appended_sidecar).unwrap(), b"appended sidecar"); } + // `..` through a folder that does not exist yet could reach the original once it is created + let mut params = selection.clone(); + params["path"] = json!(dir.join("missing/../IMG_1.jpg").to_string_lossy()); + let error = s.execute(command, ¶ms).unwrap_err().to_string(); + // Windows resolves `..` lexically and finds the original; Unix cannot resolve it and refuses + assert!(error.contains("cannot be resolved") || error.contains("never writes over an original"), "{error}"); + assert!(!dir.join("missing").exists()); + assert_eq!(std::fs::read(&src).unwrap(), original); let _ = std::fs::remove_dir_all(dir); } @@ -460,9 +470,12 @@ fn keyword_export_preserves_the_previous_file_on_write_failure() { fn preset_exports_preserve_previous_files_on_write_failure() { let dir = temp_dir("preset-export-failure"); let mut s = Session::new(); - for (command, selection, file) in - [("preset.export", json!({"ids": ["lc.warm-glow"]}), "look.lcpreset"), ("curve.exportPresets", json!({"names": ["Linear"]}), "curve.lccurve")] - { + s.mask_effects.push(crate::cmd::mask_effects::MaskEffect { name: "Glow".into(), ..Default::default() }); + for (command, selection, file) in [ + ("preset.export", json!({"ids": ["lc.warm-glow"]}), "look.lcpreset"), + ("curve.exportPresets", json!({"names": ["Linear"]}), "curve.lccurve"), + ("mask.exportEffects", json!({"names": ["Glow"]}), "effects.lceffect"), + ] { let path = dir.join(file); let mut params = selection; params["path"] = json!(path.to_string_lossy()); @@ -475,10 +488,15 @@ fn preset_exports_preserve_previous_files_on_write_failure() { assert_eq!(std::fs::read(&path).unwrap(), previous, "{command} preserves the prior export"); assert_eq!(s.execute(command, ¶ms).unwrap()["count"], 1, "replacement still succeeds"); } - assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 2, "no temporary files remain"); + assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 3, "no temporary files remain"); let _ = std::fs::remove_dir_all(dir); } +#[test] +fn mask_effect_export_never_overwrites_originals_or_sidecars() { + preset_export_preserves_originals("mask.exportEffects", json!({"names": ["Glow"]}), "effect-export-guard"); +} + fn disk_batch( s: &mut Session, id: lightcraft_catalog::PhotoId, diff --git a/crates/engine/src/tests/face_tiles.rs b/crates/engine/src/tests/face_tiles.rs index d3b50aefc4..37d20b58b0 100644 --- a/crates/engine/src/tests/face_tiles.rs +++ b/crates/engine/src/tests/face_tiles.rs @@ -46,7 +46,7 @@ fn jpeg(w: u32, h: u32, px: impl Fn(u32, u32) -> [u8; 3]) -> Vec { /// photo by its largest preview (600 × 400) and the develop path renders from it, after reading /// the whole file. fn raw_with_two_previews(tail: usize) -> Vec { - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t}; let preview = |w: u32, h: u32, bytes: Vec, first: bool| { let mut ifd = IfdBuilder::new(); if first { diff --git a/crates/engine/src/tests/import.rs b/crates/engine/src/tests/import.rs index 4885b5d30b..4de20ab3ee 100644 --- a/crates/engine/src/tests/import.rs +++ b/crates/engine/src/tests/import.rs @@ -34,7 +34,7 @@ fn write_png(path: &Path, seed: u8) { } fn exif_clock_block(original: Option<&str>, digitized: Option<&str>) -> Vec { - use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; + use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; let mut exif = IfdBuilder::new(); if let Some(value) = original { @@ -2820,7 +2820,7 @@ fn a_file_gone_since_the_import_review_is_not_imported() { /// a JPEG. In a build without the codecs' `heif` feature it is reported as failed, saying why. #[test] fn heic_imports_as_a_normal_photo_or_says_why_not() { - use lightcraft_heif::testdata::{Prop, Spec, build}; + use craft_heif::testdata::{Prop, Spec, build}; let src = temp_dir("heic"); let photo = |x: u32, y: u32| [((x * 7 + y * 13) % 200 + 30) as u16, 110, 150]; let p3 = lightcraft_codecs::icc::write_named(lightcraft_codecs::NamedSpace::DisplayP3); @@ -2857,8 +2857,8 @@ fn heic_imports_as_a_normal_photo_or_says_why_not() { /// writes as UTC: an iPhone photo landed five hours late and after 19:00 on the wrong day. #[test] fn a_heic_is_dated_by_its_exif_and_not_by_the_file() { - use lightcraft_heif::testdata::{Spec, build}; - use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t}; + use craft_heif::testdata::{Spec, build}; + use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t}; let src = temp_dir("heic-capture-time"); let photo = |x: u32, y: u32| [((x * 7 + y * 13) % 200 + 30) as u16, 110, 150]; let mut exif = IfdBuilder::new(); diff --git a/crates/engine/src/tests/lightroom_folder.rs b/crates/engine/src/tests/lightroom_folder.rs index eccff23ac9..a6ed212335 100644 --- a/crates/engine/src/tests/lightroom_folder.rs +++ b/crates/engine/src/tests/lightroom_folder.rs @@ -98,6 +98,37 @@ fn an_lrdata_folder_whose_catalog_was_renamed_finds_the_one_next_to_it() { let _ = std::fs::remove_dir_all(&root); } +/// Issue #2592: someone who deleted Lightroom and its catalog keeps only `Backups/`; that folder and +/// each dated folder in it are refused. A dated folder names its zip; `Backups/` names one only when +/// it is the only backup (with several, the import asks which). +#[test] +fn lightroom_backups_without_their_catalog_are_refused_and_name_the_zip() { + let root = temp_dir("backups"); + let backups = root.join("Lightroom").join("Backups"); + let backup = crate::lightroom_backup::tests::zip(&[("Cat.lrcat", b"SQLite format 3\0")], true, false); + for (dated, zip) in [("2019-03-26 0751", "Cat-2.lrcat.zip"), ("2023-11-07 0900", "Cat-v13.zip")] { + std::fs::create_dir_all(backups.join(dated)).unwrap(); + std::fs::write(backups.join(dated).join(zip), &backup).unwrap(); + } + // a zip without a catalog in a dated folder is not a backup + std::fs::create_dir_all(backups.join("2024-01-01 1200")).unwrap(); + std::fs::write(backups.join("2024-01-01 1200").join("photos.zip"), crate::lightroom_backup::tests::zip(&[("a.jpg", b"x")], false, false)) + .unwrap(); + assert_eq!(lightroom_catalog_at(&backups.join("2024-01-01 1200")), None, "no catalog in the zip"); + let newest = backups.join("2023-11-07 0900").join("Cat-v13.zip"); + assert_eq!(lightroom_catalog_at(&backups), Some(LightroomPlace { catalog: None }), "two backups: no arbitrary pick"); + refused(&root, &backups.join("2023-11-07 0900"), &newest); + refused(&root, &backups.join("2019-03-26 0751"), &backups.join("2019-03-26 0751").join("Cat-2.lrcat.zip")); + std::fs::remove_dir_all(backups.join("2019-03-26 0751")).unwrap(); + refused(&root, &backups, &newest); + // a folder of zips that isn't in a `Backups/` folder is not Lightroom's + let other = root.join("Downloads"); + std::fs::create_dir_all(&other).unwrap(); + std::fs::write(other.join("photos.zip"), &backup).unwrap(); + assert_eq!(lightroom_catalog_at(&other), None); + let _ = std::fs::remove_dir_all(&root); +} + #[test] fn an_empty_folder_still_becomes_a_library() { let root = temp_dir("empty"); diff --git a/crates/engine/src/tests/presets_scale.rs b/crates/engine/src/tests/presets_scale.rs index 866b7deb31..149c82ccce 100644 --- a/crates/engine/src/tests/presets_scale.rs +++ b/crates/engine/src/tests/presets_scale.rs @@ -94,6 +94,50 @@ fn importing_thousands_of_presets_is_not_quadratic() { assert!(t.elapsed().as_secs() < 20, "took {:?}", t.elapsed()); } +#[test] +fn a_preset_whose_settings_differ_only_in_the_sign_of_zero_is_a_repeat() { + let mut s = Session::new(); + let preset = |x: f64| Preset { + id: "user.z".into(), + name: "Zero".into(), + group: "Coll".into(), + settings: json!({"light": {"exposure": x}}), + favorite: false, + builtin: false, + }; + assert!(s.add_presets_each(vec![preset(0.0)])[0].is_some()); + assert_eq!(s.add_presets_each(vec![preset(-0.0)]), vec![None]); +} + +#[test] +fn thousands_of_presets_sharing_a_name_and_id_get_unique_ids_in_one_pass() { + let mut s = Session::new(); + let t = Instant::now(); + // the same name, group and id throughout, each with its own settings: every one clashes + let same: Vec = (0..20_000) + .map(|i| Preset { + id: "user.x".into(), + name: "Look".into(), + group: "Coll".into(), + settings: json!({"light": {"exposure": i}}), + favorite: false, + builtin: false, + }) + .collect(); + let each = s.add_presets_each(same); + assert_eq!(each.len(), 20_000); + assert!(each.iter().all(Option::is_some)); + // (the session may hold built-in presets too: only the added ones are counted) + let ids: std::collections::HashSet<_> = s.presets.iter().filter(|p| p.group == "Coll").map(|p| p.id.as_str()).collect(); + assert_eq!(ids.len(), 20_000); + // a repeat is skipped, per preset + let last = s.presets.last().cloned().unwrap(); + let again = s.add_presets_each(vec![last.clone(), Preset { settings: json!({"light": {"exposure": -1}}), ..last }]); + assert_eq!(again[0], None); + assert!(again[1].is_some()); + assert!(t.elapsed().as_secs() < 20, "took {:?}", t.elapsed()); +} + /// Timing with a real preset collection: `LIGHTCRAFT_BENCH_PRESETS=path/to/presets.json cargo test /// -p lightcraft-engine --release presets_scale_bench -- --ignored --nocapture`. The file is copied /// into a temporary library; the original is only read. @@ -124,3 +168,22 @@ fn presets_scale_bench() { eprintln!("preset.favorite (saves presets.json): {:?}", t.elapsed()); let _ = std::fs::remove_dir_all(&lib); } + +#[test] +fn integer_and_float_spellings_of_one_number_are_one_preset() { + // 15 slots of `0` / `0.0` / `-0.0`: all spellings are the same preset, so none collide in the index + let mut s = Session::new(); + let preset = |i: usize| { + let spell = |bit: usize| match (i >> bit) % 3 { + 0 => json!(0), + 1 => json!(0.0), + _ => json!(-0.0), + }; + let slots: Vec<_> = (0..15).map(spell).collect(); + Preset { id: "user.n".into(), name: "N".into(), group: "G".into(), settings: json!({"v": slots}), favorite: false, builtin: false } + }; + let t = Instant::now(); + let added = s.add_presets_each((0..4000).map(preset).collect()); + assert_eq!(added.iter().filter(|a| a.is_some()).count(), 1); + assert!(t.elapsed().as_secs() < 20); +} diff --git a/crates/engine/src/tests/segment.rs b/crates/engine/src/tests/segment.rs index b649dbdd7e..51d6e26e8f 100644 --- a/crates/engine/src/tests/segment.rs +++ b/crates/engine/src/tests/segment.rs @@ -240,6 +240,25 @@ fn object_clicks_are_capped() { let _ = std::fs::remove_dir_all(&dir); } +/// In background mode a new Object mask with too many clicks is refused up front, not queued. +#[test] +fn background_object_clicks_over_the_cap_are_refused_before_queueing() { + if !Segmenter::AVAILABLE { + return; + } + let mut s = Session::with_demo(); + s.segmenter.background = true; + let dir = damaged_model("later-clicks"); + s.segmenter.dir = Some(dir.clone()); + let id = s.active().unwrap(); + let points: Vec<_> = (0..=crate::segment::MAX_CLICKS).map(|_| json!([0.5, 0.5])).collect(); + let e = s.execute("mask.add", &json!({"kind": "object", "points": points})).unwrap_err().to_string(); + assert!(e.contains("at most"), "{e}"); + assert!(s.develop_of(id).unwrap().masks.is_empty()); + assert!(!s.segmenter.busy()); + let _ = std::fs::remove_dir_all(&dir); +} + #[cfg(feature = "sam")] #[test] fn a_running_model_download_shows_in_activity_and_cancels() { diff --git a/crates/engine/src/tests/xmp.rs b/crates/engine/src/tests/xmp.rs index 0d27be67c9..006a0a488a 100644 --- a/crates/engine/src/tests/xmp.rs +++ b/crates/engine/src/tests/xmp.rs @@ -638,7 +638,7 @@ fn an_orientation_only_the_xmp_states_leaves_stored_frame_regions_as_written() { let img = lightcraft_raster::Rgba8::from_fn(32, 24, |x, y| [(x * 7) as u8, (y * 9) as u8, 90, 255]); // an Exif that states the size as stored (32 × 24, as cameras do) and no orientation let exif = { - use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t}; + use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t}; let mut sub = IfdBuilder::new(); sub.set(t::PIXEL_X_DIMENSION, Value::Long(vec![32])); sub.set(t::PIXEL_Y_DIMENSION, Value::Long(vec![24])); @@ -681,7 +681,7 @@ fn a_raws_decoder_orientation_turns_stored_frame_regions_on_import_and_reread() let px: Vec = (0..16 * 6).map(|i| (i * 41 % 4096) as u16).collect(); let strip: Vec = px.chunks(2).flat_map(|p| [(p[0] >> 4) as u8, ((p[0] & 15) << 4) as u8 | (p[1] >> 8) as u8, p[1] as u8]).collect(); { - use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value, tags as t}; + use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value, tags as t}; let mut raw = IfdBuilder::new(); raw.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0])); raw.set(t::IMAGE_WIDTH, Value::Long(vec![16])); diff --git a/crates/fetch/src/lib.rs b/crates/fetch/src/lib.rs index 07552e4bb3..8e1b8e7346 100644 --- a/crates/fetch/src/lib.rs +++ b/crates/fetch/src/lib.rs @@ -242,6 +242,7 @@ fn validate_files(files: &[FileSpec<'_>]) -> Result { if files.len() > 256 { return Err(DownloadError::Invalid("too many files".into())); } + // (names that differ only in case are one file on a case-insensitive file system) let mut seen = std::collections::HashSet::new(); let mut total = 0u64; for f in files { @@ -259,7 +260,7 @@ fn validate_files(files: &[FileSpec<'_>]) -> Result { || f.name.starts_with('.') || f.name.contains(['/', '\\', ':']) || f.name.chars().any(char::is_control) - || !seen.insert(f.name) + || !seen.insert(f.name.to_lowercase()) { return Err(DownloadError::Invalid("a filename must be a unique, ordinary filename".into())); } diff --git a/crates/fetch/src/tests.rs b/crates/fetch/src/tests.rs index 5144bfc097..d144e08d29 100644 --- a/crates/fetch/src/tests.rs +++ b/crates/fetch/src/tests.rs @@ -425,3 +425,10 @@ fn hostile_file_specs_are_rejected_before_touching_disk() { assert!(!dir.exists()); } } + +#[test] +fn names_differing_only_in_case_are_one_file() { + let spec = |name| FileSpec { name, size: Some(1), sha256: None, max: 1 }; + assert!(super::validate_files(&[spec("Model.bin")]).is_ok()); + assert!(super::validate_files(&[spec("Model.bin"), spec("model.BIN")]).is_err()); +} diff --git a/crates/gpu/examples/grain_drag_bench.rs b/crates/gpu/examples/grain_drag_bench.rs new file mode 100644 index 0000000000..5a47cb6740 --- /dev/null +++ b/crates/gpu/examples/grain_drag_bench.rs @@ -0,0 +1,56 @@ +//! Grain slider drags on the GPU: the time per frame of the draft renders the loupe makes while +//! Amount or Size is dragged, on a ~2.5 MP preview (1936 × 1290, the procedural demo photo). +//! `cargo run --release -p lightcraft-gpu --example grain_drag_bench` (`FRAMES=n`, default 40). +//! +//! The same source, settings and request on every build, so it can be copied unchanged into +//! another branch to compare grain models (see `docs/grain.md` → Performance). Each drag first +//! renders four frames that aren't counted (stage cache, GPU pipelines), then moves the slider one +//! step a frame; prints the median, fastest and slowest frame. +#![allow(clippy::unwrap_used, clippy::expect_used)] + +use std::sync::Arc; +use std::time::Instant; + +use lightcraft_develop::DevelopSettings; +use lightcraft_pipeline::{Quality, RenderRequest, SourceInfo, StageCache}; + +fn main() { + let frames: usize = std::env::var("FRAMES").ok().and_then(|v| v.parse().ok()).unwrap_or(40).max(1); + let (w, h) = (1936, 1290); + let src = Arc::new(lightcraft_scenes::demo_library()[3].render(w, h)); + let info = SourceInfo { raw: true, ..Default::default() }; + let draft = RenderRequest { quality: Quality::Draft, ..RenderRequest::fit(w, h) }; + let Some(adapter) = lightcraft_gpu::adapter_name() else { + eprintln!("no GPU adapter: nothing to measure"); + return; + }; + println!("gpu: {adapter}; {w}×{h} draft renders, {frames} frames per drag"); + let mut s = DevelopSettings::default(); + s.light.exposure = 0.3; + let mut drag = |label: &str, step: &dyn Fn(&mut DevelopSettings, usize)| { + let cache = StageCache::default(); + let mut ms = Vec::with_capacity(frames); + for i in 0..frames + 4 { + step(&mut s, i); + let t = Instant::now(); + assert!(lightcraft_gpu::render(&src, &info, &s, &draft, Some(&cache)).is_some(), "GPU render failed"); + if i >= 4 { + ms.push(t.elapsed().as_secs_f64() * 1e3); + } + } + ms.sort_by(f64::total_cmp); + println!("{label:<30} median {:5.1} ms min {:5.1} max {:5.1}", ms[ms.len() / 2], ms[0], ms[ms.len() - 1]); + }; + drag("exposure drag, no grain", &|s, i| { + s.grain.amount = 0.0; + s.light.exposure = 0.3 + i as f64 * 0.01; + }); + drag("Amount drag (20 → 100)", &|s, i| { + s.grain.size = 25.0; + s.grain.amount = 20.0 + (i * 2 % 80) as f64; + }); + drag("Size drag (amount 50, 0 → 100)", &|s, i| { + s.grain.amount = 50.0; + s.grain.size = (i * 2 % 100) as f64; + }); +} diff --git a/crates/gpu/src/ctx.rs b/crates/gpu/src/ctx.rs index 6b683783f5..6be62a9bcf 100644 --- a/crates/gpu/src/ctx.rs +++ b/crates/gpu/src/ctx.rs @@ -51,6 +51,7 @@ const MODULES: &[Module] = &[ "guided_pre", "guided_ab", "guided_apply", + "clamp_between_k", "wb_k", "nr_lum", "chroma_k", @@ -197,7 +198,11 @@ fn build_jobs( layout: Some(&p.pipeline_layout), module: &p.module, entry_point: Some(entry), - compilation_options: Default::default(), + // Only the median kernels (`blur.wgsl`, `med_tile`) use workgroup memory, and they + // load every element they read before the barrier, so wgpu's default zeroing is not + // needed. On DX12 it is written out as one assignment per element, which made FXC take + // about 87 s over these kernels at every start of a release build. + compilation_options: wgpu::PipelineCompilationOptions { zero_initialize_workgroup_memory: false, ..Default::default() }, cache: None, }); let kernel = Kernel { pipeline, layout: p.layout.clone(), nbuf: p.nbuf }; @@ -382,6 +387,11 @@ pub struct Gpu { free: std::sync::Mutex>, /// Largest storage buffer the device accepts (bytes). pub max_buffer: u64, + /// The crystal grain tables of the last grain pattern, resident: a render (each Amount step + /// of a drag) copies them on the GPU instead of uploading their 12 MB again ([`Gpu::grain_tables`]). + /// Shared with each render that recorded a copy from them until it submits: replacing them + /// while another thread's copy is unsubmitted must not hand the buffer back to the pool. + grain: std::sync::Mutex, std::sync::Arc)>>, /// The device is the window's (eframe's): renders can hand it textures to draw directly. pub window: bool, } @@ -430,6 +440,11 @@ fn constants() -> String { for (i, h) in GRAIN_HASH.iter().enumerate() { s += &format!("const GRAIN_H{i}: u32 = {h}u;\n"); } + use lightcraft_pipeline::crystal::{TILE_PER_LONG, TORI}; + s += &format!( + "const CRYSTAL_T0: u32 = {}u;\nconst CRYSTAL_T1: u32 = {}u;\nconst CRYSTAL_TPL: f32 = {:?};\n", + TORI[0], TORI[1], TILE_PER_LONG as f32 + ); s += &format!("const DEEP_WRITTEN: u32 = {}u;\n", crate::params::DEEP_WRITTEN); for (name, idx) in crate::params::finish_fields() { s += &format!("const F_{name}: u32 = {idx}u;\n"); @@ -543,7 +558,7 @@ impl Gpu { max_buffer >> 20, if window { ", the window's device" } else { "" } ); - Ok(Gpu { device, queue, info, kernels, dummy, free: Default::default(), max_buffer, window }) + Ok(Gpu { device, queue, info, kernels, dummy, free: Default::default(), max_buffer, window, grain: Default::default() }) } /// A buffer of `len` 32-bit values with undefined contents (a recycled one when possible). @@ -718,6 +733,44 @@ impl Gpu { ); } + /// Record into `enc` a copy of `tile`'s summed-area tables (both tori, back to back) into + /// `dst` at value offset `at`, uploading them first unless they are the resident ones. + /// Returns the buffer copied from, which the caller keeps until it has submitted `enc` (another + /// render may replace the resident tables meanwhile). None when `dst` can't hold them (an + /// over-limit placeholder: the render has failed already). + pub fn grain_tables( + &self, + enc: &mut wgpu::CommandEncoder, + tile: &std::sync::Arc, + dst: &Buf, + at: usize, + ) -> Option> { + let len: usize = tile.sats.iter().map(Vec::len).sum(); + let (start, bytes) = ((at as u64) * 4, (len as u64) * 4); + if len == 0 || dst.raw().size() < start.saturating_add(bytes) { + return None; + } + let mut resident = self.grain.lock().unwrap_or_else(|e| e.into_inner()); + if !resident.as_ref().is_some_and(|(t, b)| std::sync::Arc::ptr_eq(t, tile) && b.raw().size() >= bytes) { + // the old tables go back to the pool first (once no unsubmitted copy holds them): one + // pattern's tables are resident at a time + *resident = None; + let buf = self.buffer(len); + if buf.raw().size() < bytes { + return None; + } + let mut off = 0u64; + for sat in &tile.sats { + self.queue.write_buffer(buf.raw(), off, bytemuck::cast_slice(sat)); + off += (sat.len() as u64) * 4; + } + *resident = Some((tile.clone(), std::sync::Arc::new(buf))); + } + let (_, src) = resident.as_ref()?; + enc.copy_buffer_to_buffer(src.raw(), 0, dst.raw(), start, bytes); + Some(src.clone()) + } + /// Stands in for a buffer that could not be created (the render has failed already). fn placeholder(&self) -> wgpu::Buffer { self.device.create_buffer(&wgpu::BufferDescriptor { @@ -1321,6 +1374,35 @@ mod tests { } } + /// Resident grain tables replaced by another pattern stay out of the pool while a render + /// that recorded a copy from them hasn't submitted it: reusing them then would hand that + /// render another tile's tables. + #[test] + fn replaced_grain_tables_stay_alive_until_their_copy_is_submitted() { + use lightcraft_pipeline::crystal::{CrystalLook, CrystalTile}; + let Some(g) = crate::device() else { + lightcraft_testkit::skip!(Gpu, "no GPU device"); + return; + }; + let look = CrystalLook::new(25.0, 50.0); + let (Some(a), Some(b)) = (CrystalTile::new(look, 0x7ab1_0001), CrystalTile::new(look, 0x7ab1_0002)) else { panic!("tiles") }; + let (a, b) = (std::sync::Arc::new(a), std::sync::Arc::new(b)); + let len: usize = a.sats.iter().map(Vec::len).sum(); + let (dst_a, dst_b) = (g.buffer(len), g.buffer(len)); + let mut first = g.encoder(); + let held = g.grain_tables(&mut first, &a, &dst_a, 0).expect("tables copied"); + // another render replaces the resident tables before `first` is submitted + let mut second = g.encoder(); + assert!(g.grain_tables(&mut second, &b, &dst_b, 0).is_some()); + let next = g.buffer(len); + assert!(next.raw() != held.raw(), "the replaced tables went back to the pool before their copy was submitted"); + g.submit([first.finish()]); + // each render read its own tile's tables + let tables = |t: &lightcraft_pipeline::crystal::CrystalTile| t.sats.concat(); + assert!(g.finish_and_read::(second, &dst_a, len) == tables(&a), "the first render copied other tables"); + assert!(g.finish_and_read::(g.encoder(), &dst_b, len) == tables(&b), "the second render copied other tables"); + } + /// A GPU retired by another failure (a validation error) keeps a live device: its buffers are /// still released, which takes a poll. #[test] diff --git a/crates/gpu/src/params.rs b/crates/gpu/src/params.rs index 5776c48caa..9140fe81d5 100644 --- a/crates/gpu/src/params.rs +++ b/crates/gpu/src/params.rs @@ -3,7 +3,7 @@ use lightcraft_develop::VignetteStyle; use lightcraft_pipeline::colorops::POINT_WORDS; -use lightcraft_pipeline::finish::{FinishParams, MASK_TERMS, srgb_lut}; +use lightcraft_pipeline::finish::{FinishParams, GrainModel, MASK_TERMS, srgb_lut}; use crate::ctx::{FailKind, fail}; @@ -71,6 +71,12 @@ const FIELDS: &[(&str, usize)] = &[ // the window's offset in the whole output and the whole output's size (pixels) ("VIG_VIEW", 4), ("GRAIN", 1), + // 0 = value noise (processes V1 to V6: GRAIN_AMT, GRAIN_SC, GRAIN_ROUGH), 1 = crystal layers (V7) + ("GRAIN_MODEL", 1), + // crystal grain: amplitude, footprint (tile px per output px), the tori's tables in `aux` + ("GRAIN_K", 1), + ("GRAIN_FOOT", 1), + ("GRAIN_SAT", 2), ("GRAIN_AMT", 1), ("GRAIN_SC", 1), ("GRAIN_ROUGH", 1), @@ -178,7 +184,8 @@ pub struct Present { } /// The `finish` kernel's parameter block and auxiliary table (tone LUT | chroma curve | sRGB LUT | curve LUTs | -/// mask terms | light operator tables) for `fp` with `masks` (their local adjustments' terms). +/// mask terms | light operator tables) for `fp` with `masks` (their local adjustments' terms). The crystal +/// grain's tables go after `aux` in the kernel's buffer ([`crate::render`] copies them there). /// /// `deep`: the kernel writes float samples, as the pipeline's `finish_deep` does. /// @@ -220,6 +227,16 @@ pub fn finish_block(fp: &FinishParams, masks: &[[f32; MASK_TERMS]], present: &Pr if let Some((l, _)) = &fp.lut { aux.extend(l.data.iter().flatten()); } + // the crystal grain's tables follow `aux` on the GPU without being part of it: they stay + // resident between renders ([`crate::Gpu::grain_tables`]) and the render copies them in + let mut sat_off = [0u32; 2]; + if let Some(GrainModel::Crystal(g)) = &fp.grain { + let mut off = aux.len(); + for (slot, sat) in sat_off.iter_mut().zip(&g.tile.sats) { + *slot = off as u32; + off += sat.len(); + } + } let mut p = Block::new(); p.u("LIGHT_OPS", light.len() as u32); @@ -330,12 +347,27 @@ pub fn finish_block(fp: &FinishParams, masks: &[[f32; MASK_TERMS]], present: &Pr p.b("PROOF_DEST_WARN", pp.dest_warning); p.b("PROOF_DISP_WARN", pp.display_warning); } - if let Some((amt, cell, rough, seed)) = fp.grain { + if let Some(model) = &fp.grain { p.b("GRAIN", true); - p.f("GRAIN_AMT", amt); - p.f("GRAIN_SC", fp.px_per_long as f32 / cell); - p.f("GRAIN_ROUGH", rough); - p.u("GRAIN_SEED", seed); + match model { + GrainModel::Value(g) => { + p.u("GRAIN_MODEL", 0); + p.f("GRAIN_AMT", g.amt); + p.f("GRAIN_SC", fp.px_per_long as f32 / g.cell); + p.f("GRAIN_ROUGH", g.rough); + p.u("GRAIN_SEED", g.seed); + } + GrainModel::Crystal(g) => { + p.u("GRAIN_MODEL", 1); + p.f("GRAIN_K", g.k); + p.f("GRAIN_FOOT", g.foot); + if let Some(i) = index("GRAIN_SAT") { + for (slot, off) in p.0.iter_mut().skip(i).zip(sat_off) { + *slot = off; + } + } + } + } let long = fp.ow.max(fp.oh); let [a, b, c, d, e, f] = fp.out_to_norm.0; let (kx, ky) = (fp.ow / long, fp.oh / long); diff --git a/crates/gpu/src/render.rs b/crates/gpu/src/render.rs index 2b20866e64..a392ee6ee1 100644 --- a/crates/gpu/src/render.rs +++ b/crates/gpu/src/render.rs @@ -136,6 +136,10 @@ pub(crate) struct Cx<'a> { enc: Option, /// Kernel invocations recorded since the last submit. pending: u64, + /// Buffers shared with other renders that recorded commands read from (the resident grain + /// tables): kept until those commands are submitted. Declared after `enc`, so an unsubmitted + /// encoder is dropped before them. + held: Vec>, } /// Kernel invocations recorded before a render submits them (under one full-size pass over a @@ -147,7 +151,7 @@ const BAND_PIXELS: usize = 4 << 20; impl<'a> Cx<'a> { pub fn new(gpu: &'a Gpu) -> Cx<'a> { - Cx { gpu, enc: Some(gpu.encoder()), pending: 0 } + Cx { gpu, enc: Some(gpu.encoder()), pending: 0, held: Vec::new() } } /// Record kernel `name` (see [`Gpu::run`]). @@ -161,11 +165,27 @@ impl<'a> Cx<'a> { } } + /// The finish kernel's `aux` buffer: `aux`, then the crystal grain's tables when it has them + /// (copied on the GPU from the resident ones, see [`Gpu::grain_tables`]). + pub fn finish_aux(&mut self, aux: &[f32], grain: Option<&lightcraft_pipeline::finish::GrainModel>) -> Buf { + let Some(lightcraft_pipeline::finish::GrainModel::Crystal(g)) = grain else { return self.gpu.upload(aux) }; + let tables: usize = g.tile.sats.iter().map(Vec::len).sum(); + let out = self.gpu.buffer(aux.len().saturating_add(tables)); + if !aux.is_empty() && out.raw().size() >= (aux.len() as u64) * 4 { + self.gpu.queue.write_buffer(out.raw(), 0, bytemuck::cast_slice(aux)); + } + let enc = self.enc.get_or_insert_with(|| self.gpu.encoder()); + self.held.extend(self.gpu.grain_tables(enc, &g.tile, &out, aux.len())); + out + } + /// Submit what is recorded and read back `len` values of `b`. pub fn read(&mut self, b: &Buf, len: usize) -> Vec { self.pending = 0; let enc = self.enc.take().unwrap_or_else(|| self.gpu.encoder()); - self.gpu.finish_and_read(enc, b, len) + let out = self.gpu.finish_and_read(enc, b, len); + self.held.clear(); + out } /// Submit what is recorded so far without waiting. Renders submit stage by stage and every @@ -177,6 +197,7 @@ impl<'a> Cx<'a> { if let Some(enc) = self.enc.take() { self.gpu.submit([enc.finish()]); } + self.held.clear(); } /// Submit what is recorded and wait for it (profiling only: stage timings). @@ -185,6 +206,7 @@ impl<'a> Cx<'a> { if let Some(enc) = self.enc.take() { self.gpu.submit([enc.finish()]); } + self.held.clear(); let _ = self.gpu.poll(wgpu::PollType::wait_indefinitely()); } @@ -349,6 +371,9 @@ fn guided_coeffs(cx: &mut Cx<'_>, p: &Buf, w: usize, h: usize, sigma: f32, eps: gaussian(cx, &ab, w, h, 2, sigma) } +/// Pixels per thread along the pass of `median_h` / `median_v` (`MED_K` in `blur.wgsl`). +const MEDIAN_K: usize = 4; + /// Separable median of a plane (`local::median_hv`). fn median_hv(cx: &mut Cx<'_>, p: &Buf, w: usize, h: usize, r: usize) -> Buf { if r == 0 || w * h == 0 { @@ -356,21 +381,38 @@ fn median_hv(cx: &mut Cx<'_>, p: &Buf, w: usize, h: usize, r: usize) -> Buf { } let pr = [w as u32, h as u32, 1, r.min(local::BASE_DETAIL_MAX) as u32]; let tmp = cx.gpu.buffer(w * h); - cx.run("median_h", &pr, &[Some(p), Some(&tmp)], groups2(w, h, [16, 16])); + cx.run("median_h", &pr, &[Some(p), Some(&tmp)], groups2(w.div_ceil(MEDIAN_K), h, [16, 16])); let out = cx.gpu.buffer(w * h); - cx.run("median_v", &pr, &[Some(&tmp), Some(&out)], groups2(w, h, [16, 16])); + cx.run("median_v", &pr, &[Some(&tmp), Some(&out)], groups2(w, h.div_ceil(MEDIAN_K), [16, 16])); out } -/// The highlights/shadows base (`local::tone_base`; `detail` 0: V1, no median). -fn tone_base(cx: &mut Cx<'_>, l: &Buf, w: usize, h: usize, sigma: f32, detail: usize) -> Buf { +/// The highlights/shadows base (`local::tone_base`; `detail` 0: V1, no detail removal). +fn tone_base(cx: &mut Cx<'_>, l: &Buf, w: usize, h: usize, sigma: f32, detail: usize, keeps_gaps: bool) -> Buf { if detail == 0 { return guided_fast(cx, l, w, h, sigma, local::BASE_EPS); } - let m = median_hv(cx, l, w, h, detail); + let m = if keeps_gaps { detail_free(cx, l, w, h, detail) } else { median_hv(cx, l, w, h, detail) }; guided_fast(cx, &m, w, h, sigma, local::BASE_EPS) } +/// Log luminance without detail finer than `r` px (`local::detail_free`): clamped between its +/// median at `r` and the median of a `local::BASE_COARSE_STEP`× downscaled copy, enlarged. +fn detail_free(cx: &mut Cx<'_>, l: &Buf, w: usize, h: usize, r: usize) -> Buf { + if r == 0 || w * h == 0 { + return cx.copy(l); + } + let fine = median_hv(cx, l, w, h, r); + let s = local::BASE_COARSE_STEP; + let (lw, lh) = (w.div_ceil(s).max(1), h.div_ceil(s).max(1)); + let lo = resize(cx, l, (w, h), (lw, lh), 1, Filter::Box); + let lo_median = median_hv(cx, &lo, lw, lh, r); + let coarse = resize(cx, &lo_median, (lw, lh), (w, h), 1, Filter::Bilinear); + let out = cx.gpu.buffer(w * h); + map(cx, "clamp_between_k", w * h, &[], [Some(l), Some(&fine), Some(&coarse)], &out); + out +} + /// Fast guided filter (`local::guided_fast`): coefficients on a subsampled grid. fn guided_fast(cx: &mut Cx<'_>, p: &Buf, w: usize, h: usize, sigma: f32, eps: f32) -> Buf { let s = local::guided_fast_step(sigma); @@ -749,6 +791,7 @@ pub fn render( px_per_long: plan.px_per_long, air_pre: prep.air, space: req.space, + draft: req.quality == lightcraft_pipeline::Quality::Draft, }); // soft proofing as the CPU's `finish` sets it up (an output-device table never gets here: see `render`) fp.proof = req.proof.map(|pr| pr.params(req.space)); @@ -780,7 +823,7 @@ pub fn render( if crate::ctx::failed() { return None; // a malformed parameter block failed the render: the CPU fallback redoes it } - let aux = gpu.upload(&aux); + let aux = cx.finish_aux(&aux, fp.grain.as_ref()); // cleared, so that work which never ran cannot pass for an image (see the check below) let out = cx.zeroed(if deep { 3 * n } else { n }); if fault == Some(crate::Fault::Validation) { @@ -1011,7 +1054,14 @@ fn linear(cx: &mut Cx<'_>, sampled: &Buf, info: &SourceInfo, plan: &Plan<'_>, ho eyed } }; - denoise(cx, img, plan, colour_noise) + let img = denoise(cx, img, plan, colour_noise); + if !lightcraft_pipeline::lens_blur::renders(s) { + return img; + } + // Lens Blur (global or a mask's): on the CPU, like the CPU renderer's (`lens_blur::apply`) + let mut host_img = cx.read_rgb(&img, w, h); + lightcraft_pipeline::lens_blur::apply_with(&mut host_img, s, &plan.frame, plan.px_per_long, plan.mattes.as_deref(), plan.lens_subject.as_deref()); + cx.gpu.upload(rgb_words(&host_img)) } /// Defringe (`optics::defringe`) of the white-balanced image. @@ -1395,7 +1445,7 @@ fn prepare(cx: &mut Cx<'_>, lin: &Buf, plan: &Plan<'_>, req: &RenderRequest, pla let base = match sig.base { Some(sg) => { let key = local::base_key(&plan.settings, sg, sig.base_detail); - plane_at(&mut planes.base, key, || tone_base(cx, &log_l, w, h, sg, sig.base_detail)) + plane_at(&mut planes.base, key, || tone_base(cx, &log_l, w, h, sg, sig.base_detail, local::base_keeps_gaps(&plan.settings))) } None => log_l.clone(), }; @@ -1818,6 +1868,76 @@ mod tests { time("guided sigma=1.75", &mut |cx| drop(guided(cx, &src, w, h, 1.75, 0.01))); } + /// The tiled median kernels select exactly what the CPU's sorted window does: edge tiles, + /// planes smaller than a tile or a window, every radius and many ties. + #[test] + fn median_matches_cpu_bit_for_bit() { + // (creating the device writes the crash sentinel another test swaps out) + let _serial = crate::DEVICE_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let Some(gpu) = crate::device() else { return lightcraft_testkit::skip!(Gpu, "no GPU adapter") }; + let _scope = crate::ctx::RenderScope::new(gpu); + let mut seed = 0x9e37_79b9_u32; + let mut next = move || { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + seed + }; + for (w, h) in [(1, 1), (2, 3), (5, 70), (70, 5), (63, 65), (130, 67), (200, 129)] { + // a coarse level set gives ties; a smooth ramp and fine noise give distinct values + let mut p = Plane::new(w, h); + for (i, o) in p.data.iter_mut().enumerate() { + let (x, y) = (i % w, i / w); + *o = match (next() % 3, next()) { + (0, v) => (v % 7) as f32 * 0.5 - 2.0, + (1, v) => (x + 2 * y) as f32 * 0.01 + (v % 100) as f32 * 1e-4, + (_, v) => (v % 10_000) as f32 * 1e-3 - 5.0, + }; + } + let src = gpu.upload(&p.data); + for r in 1..=local::BASE_DETAIL_MAX { + let mut cx = Cx::new(gpu); + let out = median_hv(&mut cx, &src, w, h, r); + let got: Vec = cx.read(&out, w * h); + let want = local::median_hv(&p, r); + let bad = got.iter().zip(&want.data).position(|(g, c)| g.to_bits() != c.to_bits()); + assert!(bad.is_none(), "{w}×{h} r={r}: pixel {bad:?}"); + } + } + } + + /// Highlights/shadows base timings on the #3460 frame size: + /// `cargo test --release -p lightcraft-gpu bench_tone_base -- --ignored --nocapture`. + #[test] + #[ignore] + fn bench_tone_base() { + let Some(gpu) = crate::device() else { return eprintln!("no gpu: {:?}", crate::unavailable_reason()) }; + let _scope = crate::ctx::RenderScope::new(gpu); + let (w, h) = (6960, 4640); + let r = local::base_detail_radius(w as f64); + eprintln!("{} ({:?}) r={r}", gpu.info.name, gpu.info.backend); + let l: Vec = (0..w * h).map(|i: usize| ((i.wrapping_mul(2_654_435_761)) % 1000) as f32 / 250.0 - 6.0).collect(); + let src = gpu.upload(&l); + let time = |name: &str, f: &mut dyn FnMut(&mut Cx<'_>)| { + let mut ts = Vec::new(); + for _ in 0..9 { + let mut cx = Cx::new(gpu); + cx.sync(); + let t = std::time::Instant::now(); + f(&mut cx); + cx.sync(); + ts.push(t.elapsed().as_secs_f64() * 1e3); + } + ts.sort_by(f64::total_cmp); + eprintln!("{name:<40} min {:.1} ms median {:.1} ms", ts[0], ts[4]); + }; + let sigma = 0.015 * w as f32; + time("median_hv", &mut |cx| drop(median_hv(cx, &src, w, h, r))); + time("detail_free", &mut |cx| drop(detail_free(cx, &src, w, h, r))); + time("guided_fast", &mut |cx| drop(guided_fast(cx, &src, w, h, sigma, local::BASE_EPS))); + time("tone_base", &mut |cx| drop(tone_base(cx, &src, w, h, sigma, r, true))); + } + /// The device scores Remove's candidate sources like the CPU and picks the same one. #[test] fn auto_source_picks_the_cpu_candidate() { diff --git a/crates/gpu/src/wgsl/blur.wgsl b/crates/gpu/src/wgsl/blur.wgsl index 756457ade4..e6a624dffb 100644 --- a/crates/gpu/src/wgsl/blur.wgsl +++ b/crates/gpu/src/wgsl/blur.wgsl @@ -113,56 +113,114 @@ fn minmax(@builtin(global_invocation_id) g: vec3) { } // Separable median of a single-channel plane (`lightcraft_pipeline::local::median_hv`): 2r+1 -// taps (r ≤ 24, clamped edges) along a row (median_h) or a column (median_v), one thread per -// pixel. The median is a selection, so the result matches the CPU's bit for bit. +// taps (1 ≤ r ≤ 24, clamped edges) along a row (median_h) or a column (median_v). The median is a +// selection, so the result matches the CPU's bit for bit. // P: w, h, nc (unused), r. -fn median_of(n: u32, r: u32, v: ptr>) -> f32 { - // partial selection sort up to the middle - for (var i = 0u; i <= r; i++) { - var m = i; - for (var j = i + 1u; j < n; j++) { - if ((*v)[j] < (*v)[m]) { - m = j; - } +// +// Each thread finds the medians of 4 neighbouring pixels along the pass by rank: a tap is the +// median of a window when fewer than r+1 of its taps are smaller and at least r+1 are smaller or +// equal. The 4 windows share all but their first and last 3 taps, so one count over the shared +// taps serves all four. A 16×16 workgroup (64 pixels along the pass, 16 lines across) first loads +// its taps into `med_tile`. On a 6960×4640 plane at r = 10 this takes 47 ms on an M2 Max, against +// 191 ms for a partial selection sort per pixel over a private array of taps, which drivers keep in +// scratch memory because it is indexed dynamically. +const MED_K: u32 = 4u; +const MED_LINE: u32 = 113u; // 16·MED_K + 2·24, odd +// Not zero-initialised (`build_jobs` in ctx.rs turns that off): write every element read. +var med_tile: array; // 16 lines + +// The medians of the 2r+1-tap windows starting at `med_tile[base + k]`, k < 4 (r ≥ 1). Taps 3 to +// n-1 are in all four windows; the three at each end are in some. +fn median4(base: u32, r: u32) -> vec4 { + let n = 2u * r + 1u; + let e0 = vec3(med_tile[base], med_tile[base + 1u], med_tile[base + 2u]); + let e1 = vec3(med_tile[base + n], med_tile[base + n + 1u], med_tile[base + n + 2u]); + var out = vec4(0.0); + var found = vec4(false); + for (var i = 0u; i < n + 3u; i++) { + let c = med_tile[base + i]; + var less = 0u; + var le = 0u; + for (var j = 3u; j < n; j++) { + let v = med_tile[base + j]; + less += select(0u, 1u, v < c); + le += select(0u, 1u, v <= c); + } + let l0 = select(vec3(0u), vec3(1u), e0 < vec3(c)); + let l1 = select(vec3(0u), vec3(1u), e1 < vec3(c)); + let q0 = select(vec3(0u), vec3(1u), e0 <= vec3(c)); + let q1 = select(vec3(0u), vec3(1u), e1 <= vec3(c)); + // window k: taps k..k+n-1 = taps k..2 of the start, the middle, taps n..n+k-1 of the end + let lw = vec4(l0.x + l0.y + l0.z, l0.y + l0.z + l1.x, l0.z + l1.x + l1.y, l1.x + l1.y + l1.z) + vec4(less); + let qw = vec4(q0.x + q0.y + q0.z, q0.y + q0.z + q1.x, q0.z + q1.x + q1.y, q1.x + q1.y + q1.z) + vec4(le); + let has = vec4((i < n), (i >= 1u) && (i < n + 1u), (i >= 2u) && (i < n + 2u), (i >= 3u)); + let hit = has & !found & (lw <= vec4(r)) & (qw > vec4(r)); + out = select(out, vec4(c), hit); + found = found | hit; + if (all(found)) { + break; } - let t = (*v)[i]; - (*v)[i] = (*v)[m]; - (*v)[m] = t; } - return (*v)[r]; + return out; } +// Thread (l.x, l.y) of a workgroup takes line l.x and pixels 4·l.y to 4·l.y + 3 along it, so the +// threads of a SIMD group read 16 different lines at once; with an odd line length those fall in +// different banks of workgroup memory. @compute @workgroup_size(16, 16) -fn median_h(@builtin(global_invocation_id) g: vec3) { +fn median_h(@builtin(local_invocation_id) l: vec3, @builtin(workgroup_id) wg: vec3) { let w = pu(0u); let h = pu(1u); let r = min(pu(3u), 24u); - if (g.x >= w || g.y >= h) { + let span = 16u * MED_K + 2u * r; + let x0 = i32(wg.x * 16u * MED_K) - i32(r); + // load: consecutive threads read consecutive pixels of a row + for (var e = l.y * 16u + l.x; e < 16u * span; e += 256u) { + let line = e / span; + let k = e % span; + let y = min(wg.y * 16u + line, h - 1u); + let x = clamp(x0 + i32(k), 0, i32(w) - 1); + med_tile[line * MED_LINE + k] = src[y * w + u32(x)]; + } + workgroupBarrier(); + let y = wg.y * 16u + l.x; + let x = (wg.x * 16u + l.y) * MED_K; + if (x >= w || y >= h) { return; } - let n = 2u * r + 1u; - var v: array; - let row = g.y * w; - for (var k = 0u; k < n; k++) { - let x = clamp(i32(g.x + k) - i32(r), 0, i32(w) - 1); - v[k] = src[row + u32(x)]; + let m = median4(l.x * MED_LINE + l.y * MED_K, r); + for (var k = 0u; k < MED_K; k++) { + if (x + k < w) { + dst[y * w + x + k] = m[k]; + } } - dst[row + g.x] = median_of(n, r, &v); } @compute @workgroup_size(16, 16) -fn median_v(@builtin(global_invocation_id) g: vec3) { +fn median_v(@builtin(local_invocation_id) l: vec3, @builtin(workgroup_id) wg: vec3) { let w = pu(0u); let h = pu(1u); let r = min(pu(3u), 24u); - if (g.x >= w || g.y >= h) { + let span = 16u * MED_K + 2u * r; + let y0 = i32(wg.y * 16u * MED_K) - i32(r); + // load: consecutive threads read consecutive pixels of a row (16 columns) + for (var e = l.y * 16u + l.x; e < 16u * span; e += 256u) { + let line = e % 16u; + let k = e / 16u; + let x = min(wg.x * 16u + line, w - 1u); + let y = clamp(y0 + i32(k), 0, i32(h) - 1); + med_tile[line * MED_LINE + k] = src[u32(y) * w + x]; + } + workgroupBarrier(); + let x = wg.x * 16u + l.x; + let y = (wg.y * 16u + l.y) * MED_K; + if (x >= w || y >= h) { return; } - let n = 2u * r + 1u; - var v: array; - for (var k = 0u; k < n; k++) { - let y = clamp(i32(g.y + k) - i32(r), 0, i32(h) - 1); - v[k] = src[u32(y) * w + g.x]; + let m = median4(l.x * MED_LINE + l.y * MED_K, r); + for (var k = 0u; k < MED_K; k++) { + if (y + k < h) { + dst[(y + k) * w + x] = m[k]; + } } - dst[g.y * w + g.x] = median_of(n, r, &v); } diff --git a/crates/gpu/src/wgsl/finish.wgsl b/crates/gpu/src/wgsl/finish.wgsl index 266599644a..8a2f72f89f 100644 --- a/crates/gpu/src/wgsl/finish.wgsl +++ b/crates/gpu/src/wgsl/finish.wgsl @@ -291,6 +291,65 @@ fn ghash(i: i32, j: i32, seed: u32) -> f32 { return f32(v & 0xffffu) / 32768.0 - 1.0; } +// Crystal grain (process V4): `lightcraft_pipeline::crystal` (keep in step with it). The tori's +// summed-area tables follow the other tables in `aux` (offsets F_GRAIN_SAT). +fn cg_bilinear(o: u32, t: u32, x: f32, y: f32) -> f32 { + let s = t + 1u; + let i = min(u32(max(x, 0.0)), t - 1u); + let j = min(u32(max(y, 0.0)), t - 1u); + let fx = x - f32(i); + let fy = y - f32(j); + let b = o + j * s + i; + let a = aux[b] + (aux[b + 1u] - aux[b]) * fx; + let c = aux[b + s] + (aux[b + s + 1u] - aux[b + s]) * fx; + return a + (c - a) * fy; +} + +fn cg_integral(o: u32, t: u32, x: f32, y: f32) -> f32 { + let tf = f32(t); + let wx = x >= tf; + let wy = y >= tf; + let rx = select(x, x - tf, wx); + let ry = select(y, y - tf, wy); + var v = cg_bilinear(o, t, rx, ry); + if (wx) { + v += cg_bilinear(o, t, tf, ry); + } + if (wy) { + v += cg_bilinear(o, t, rx, tf); + } + if (wx && wy) { + v += cg_bilinear(o, t, tf, tf); + } + return v; +} + +fn cg_box(o: u32, t: u32, x0_: f32, y0_: f32, w: f32) -> f32 { + let tf = f32(t); + let x0 = x0_ - floor(x0_ / tf) * tf; + let y0 = y0_ - floor(y0_ / tf) * tf; + return cg_integral(o, t, x0 + w, y0 + w) - cg_integral(o, t, x0, y0 + w) - cg_integral(o, t, x0 + w, y0) + cg_integral(o, t, x0, y0); +} + +fn cg_lift(v: f32) -> f32 { + let c = clamp(v, 0.0, 1.0); + return select((c + 0.055) / 2.4, c, c <= 0.04045); +} + +fn cg_srgb_to_linear(v: f32) -> f32 { + return select(pow((v + 0.055) / 1.055, 2.4), v / 12.92, v <= 0.04045); +} + +fn crystal_grain(e: vec3, gx: f32, gy: f32) -> vec3 { + let w = pf(F_GRAIN_FOOT); + let x0 = gx * CRYSTAL_TPL - 0.5 * w; + let y0 = gy * CRYSTAL_TPL - 0.5 * w; + let g = (cg_box(pu(F_GRAIN_SAT), CRYSTAL_T0, x0, y0, w) + cg_box(pu(F_GRAIN_SAT + 1u), CRYSTAL_T1, x0, y0, w)) / (w * w); + let lum = clamp(0.2126 * e.x + 0.7152 * e.y + 0.0722 * e.z, 0.0, 1.0); + let k = pf(F_GRAIN_K) * g * (1.0 - cg_srgb_to_linear(lum)); + return e + k * vec3(cg_lift(e.x), cg_lift(e.y), cg_lift(e.z)); +} + fn grain_noise(x: f32, y: f32, seed: u32) -> f32 { let x0 = floor(x); let y0 = floor(y); @@ -694,17 +753,21 @@ fn main(@builtin(global_invocation_id) gid: vec3) { let py = f32(y) + 0.5; let gx = pf(F_GRAIN_AFF) * px + pf(F_GRAIN_AFF + 2u) * py + pf(F_GRAIN_AFF + 4u); let gy = pf(F_GRAIN_AFF + 1u) * px + pf(F_GRAIN_AFF + 3u) * py + pf(F_GRAIN_AFF + 5u); - let sc = pf(F_GRAIN_SC); - let rough = pf(F_GRAIN_ROUGH); - let seed = pu(F_GRAIN_SEED); - var g = grain_noise(gx * sc, gy * sc, seed); - g = g * (1.0 - rough * 0.5) + grain_noise(gx * sc * 2.3, gy * sc * 2.3, seed ^ 0x55u) * rough * 0.7; - var lum = 0.2126 * e.x + 0.7152 * e.y + 0.0722 * e.z; - if (hdr) { - lum = clamp(lum, 0.0, 1.0); + if (pu(F_GRAIN_MODEL) == 1u) { + e = crystal_grain(e, gx, gy); + } else { + let sc = pf(F_GRAIN_SC); + let rough = pf(F_GRAIN_ROUGH); + let seed = pu(F_GRAIN_SEED); + var g = grain_noise(gx * sc, gy * sc, seed); + g = g * (1.0 - rough * 0.5) + grain_noise(gx * sc * 2.3, gy * sc * 2.3, seed ^ 0x55u) * rough * 0.7; + var lum = 0.2126 * e.x + 0.7152 * e.y + 0.0722 * e.z; + if (hdr) { + lum = clamp(lum, 0.0, 1.0); + } + let k = pf(F_GRAIN_AMT) * g * (0.35 + 2.6 * lum * (1.0 - lum)); + e = e + k; } - let k = pf(F_GRAIN_AMT) * g * (0.35 + 2.6 * lum * (1.0 - lum)); - e = e + k; } if (pu(F_LUT) != 0u) { let lc = clamp(e, vec3(0.0), vec3(1.0)); diff --git a/crates/gpu/src/wgsl/map.wgsl b/crates/gpu/src/wgsl/map.wgsl index 9b84b8d89d..2b5f658931 100644 --- a/crates/gpu/src/wgsl/map.wgsl +++ b/crates/gpu/src/wgsl/map.wgsl @@ -70,6 +70,25 @@ fn guided_apply(@builtin(global_invocation_id) g: vec3, @builtin(num_workgr dst[i] = b[2u * i] * a[i] + b[2u * i + 1u]; } +// `clamp_between` (`local::detail_free`): a clamped between b and c, in either order. +@compute @workgroup_size(256) +fn clamp_between_k(@builtin(global_invocation_id) g: vec3, @builtin(num_workgroups) nw: vec3) { + let i = lin_index(g, nw); + if (i >= pu(0u)) { + return; + } + let lo = min(b[i], c[i]); + let hi = max(b[i], c[i]); + let v = a[i]; + if (v < lo) { + dst[i] = lo; + } else if (v > hi) { + dst[i] = hi; + } else { + dst[i] = v; + } +} + // Cross guided filter (`masks::guided_cross`), a: guide, b: input. P[1] = 0: (guide, input); // 1: (guide·input, guide²). @compute @workgroup_size(256) diff --git a/crates/gpu/tests/it/equivalence.rs b/crates/gpu/tests/it/equivalence.rs index 8c87dd7e26..a8fa80f812 100644 --- a/crates/gpu/tests/it/equivalence.rs +++ b/crates/gpu/tests/it/equivalence.rs @@ -210,6 +210,33 @@ fn banded_full_size_render_matches() { check("banded 3000×2000", &src, &SourceInfo { raw: true, ..Default::default() }, &s, &RenderRequest::fit(w, h)); } +/// The crystal grain's tables stay on the GPU between renders: an Amount change reuses them, +/// another pattern (Size, Roughness, seed) replaces them, and going back draws the first again. +#[test] +fn grain_patterns_in_turn_match() { + if !gpu() { + return; + } + let src = scene(1, 480, 320); + let info = SourceInfo { raw: true, ..Default::default() }; + let steps: [(&str, f64, f64, f64, u32); 6] = [ + ("grain 50/25/50", 50.0, 25.0, 50.0, 1), + ("grain 90/25/50 (same pattern)", 90.0, 25.0, 50.0, 1), + ("grain 50/70/50 (new size)", 50.0, 70.0, 50.0, 1), + ("grain 50/70/0 (new roughness)", 50.0, 70.0, 0.0, 1), + ("grain 50/70/0 seed 7", 50.0, 70.0, 0.0, 7), + ("grain 50/25/50 again", 50.0, 25.0, 50.0, 1), + ]; + for (name, amount, size, roughness, seed) in steps { + let mut s = DevelopSettings::default(); + s.grain.amount = amount; + s.grain.size = size; + s.grain.roughness = roughness; + s.grain.seed = seed; + check(name, &src, &info, &s, &RenderRequest::fit(480, 320)); + } +} + fn cases() -> Vec<(&'static str, Edit)> { vec![ ("default", |_| {}), @@ -287,6 +314,25 @@ fn cases() -> Vec<(&'static str, Edit)> { s.grain.size = 40.0; s.grain.roughness = 60.0; }), + // the frozen value-noise grain of the earlier processes (the default is V7's crystals) + ("grain, process V6", |s| { + s.process = Process::V6.version(); + s.grain.amount = 50.0; + s.grain.size = 40.0; + s.grain.roughness = 60.0; + }), + ("grain, process V1", |s| { + s.process = Process::V1.version(); + s.grain.amount = 50.0; + s.grain.size = 40.0; + s.grain.roughness = 60.0; + }), + ("grain, process V2", |s| { + s.process = Process::V2.version(); + s.grain.amount = 50.0; + s.grain.size = 40.0; + s.grain.roughness = 60.0; + }), ("dehaze -", |s| s.effects.dehaze = -50.0), ("dehaze +", |s| s.effects.dehaze = 60.0), ("clarity/texture -", |s| { @@ -761,7 +807,7 @@ fn light_v3_operators_match() { } /// Issue #632: the Highlights / Shadows base of each process (V1: the edge-aware filter of log -/// luminance; V2: of its fine median, a radius that grows with the render) matches the CPU's, and +/// luminance; V2 to V7: of its fine median, V8 on: of its detail-free plane, a radius that grows with the render) matches the CPU's, and /// a warm cache never hands one process the other's base. #[test] fn highlights_shadows_base_matches_on_every_process() { @@ -792,7 +838,7 @@ fn highlights_shadows_base_matches_on_every_process() { }]; check(&format!("{p:?} local highlights {w}"), &src, &raw, &s, &req); } - for p in [Process::V1, Process::V2, Process::V4, Process::V1, Process::V2, Process::V4] { + for p in [Process::V1, Process::V2, Process::V4, Process::V8, Process::V1, Process::V2, Process::V4, Process::V8] { let mut s = DevelopSettings { process: p.version(), ..DevelopSettings::default() }; s.light.highlights = -100.0; let warm = lightcraft_gpu::render(&src, &raw, &s, &req, Some(&cache)).expect("gpu"); diff --git a/crates/heif/Cargo.toml b/crates/heif/Cargo.toml deleted file mode 100644 index 95641fb2fd..0000000000 --- a/crates/heif/Cargo.toml +++ /dev/null @@ -1,26 +0,0 @@ -[package] -name = "lightcraft-heif" -description = "Optional HEIF/HEIC still-image decoder for LightCraft (heic-rs for the container and HEVC, libheif-matching colour conversion, panic-guarded)" -version.workspace = true -edition.workspace = true -license.workspace = true - -[dependencies] -# Pure Rust, no unsafe. Pinned exactly: a young, single-owner crate that parses untrusted files; -# moving the pin is a reviewed change (re-run the heif tests and the codecs corpus tests). -# Mirrors PhotoCraft's photocraft-heif (same team, same decision): heic-rs was chosen over the -# other permissive candidates (heif-oxide's HEVC decoder, rust_h265, follows FFmpeg's (LGPL) -# code structure; gamut-heic has no HEVC decoder of its own). -heic-rs = { version = "=0.1.4", default-features = false, features = ["std"] } - -# The tiles of a HEIC photo decode in parallel on native; wasm stays single-threaded. -[target.'cfg(not(target_arch = "wasm32"))'.dependencies] -heic-rs = { version = "=0.1.4", default-features = false, features = ["std", "parallel"] } -rayon = { workspace = true } - -[features] -# `lightcraft_heif::testdata`: lossless synthetic HEIF files, for the tests of the crates above. -testdata = [] - -[lints] -workspace = true diff --git a/crates/heif/README.md b/crates/heif/README.md deleted file mode 100644 index 75db814b0d..0000000000 --- a/crates/heif/README.md +++ /dev/null @@ -1,47 +0,0 @@ -# lightcraft-heif - -The optional HEIF / HEIC decoder of LightCraft: iPhone and Mac photos. [`heic-rs`](https://github.com/tbraun96/heic-rs) -(pure Rust, no `unsafe`, MIT OR Apache-2.0, written from the H.265 / HEIF specifications) reads the -container and reconstructs the HEVC pictures; this crate turns them into 16-bit RGB itself so the -result matches libheif to within one code value: - -- the colour description is the item's `colr nclx` (or its first tile's), else the HEVC VUI - (iPhones: full range), else the H.265 defaults; an unspecified matrix is BT.601. heic-rs alone - assumes BT.709 limited range, about 8 code values off on every iPhone photo; -- chroma is upsampled by nearest neighbour, or centred bilinear when a `clap` starts on an odd - pixel, as libheif does; -- `imir` axis 0 flips top and bottom, as libheif writes and reads it (heic-rs names it the other way); -- a grid's ICC profile may sit on its tiles. - -Single pictures and grid-tiled photos, 4:2:0 and monochrome, 8- to 10-bit, alpha auxiliary images -(other auxiliary images — HDR gain maps, depth — are ignored), the container's rotation/mirror/crop, -ICC, EXIF and XMP, and the file's thumbnail item. 4:2:2 and 4:4:4 (Canon/Sony "HIF") are refused -with `Error::Unsupported`: heic-rs 0.1.1 loses sync on them. Read-only. - -```rust -let info = lightcraft_heif::probe(&bytes)?; // size, depth, alpha: check limits first -let img = lightcraft_heif::decode(&bytes, &lightcraft_heif::Options::default())?; -// img.width, img.height, img.has_alpha, img.bit_depth, img.data (RGB/RGBA u16), img.colour, img.icc, img.exif, img.xmp -let thumb = lightcraft_heif::decode_thumbnail(&bytes, 256, &Default::default())?; // Option -``` - -Errors are `Error::Unsupported` (image sequences, overlays, 4:2:2/4:4:4, …), `Error::Limit` or -`Error::Malformed`. It never panics: every call into heic-rs runs under `catch_unwind`, and a panic -inside it (PhotoCraft's fuzzing found two in 0.1.1) becomes `Error::Malformed`. - -Feature `testdata` exposes `lightcraft_heif::testdata`: lossless synthetic HEIF files (PCM-coded -32 × 32 HEVC pictures, grids, every property above) for the tests of the crates above. - -## Why it is a separate, optional crate - -- **A young decoder.** heic-rs is new and has a single maintainer. It is pinned exactly - (`=0.1.1`); moving the pin is a reviewed change (re-run the crate tests and - `cargo test -p lightcraft-codecs --features heif`). The other permissive candidates were - rejected: heif-oxide's HEVC decoder (rust_h265) follows FFmpeg's (LGPL) code structure, and - gamut-heic has no HEVC decoder of its own. -- **HEVC patents are a distributor's call.** `lightcraft-codecs` uses this crate only behind its - non-default `heif` feature; the apps forward it and the release packages enable it. Without it, - HEIC files are still recognised and opening one is a clear "HEIC/HEIF support isn't included in - this build" error — the same policy as PhotoCraft. - -The crate depends on no other LightCraft crate; `codecs` → `heif` is the only edge between them. diff --git a/crates/heif/src/census.rs b/crates/heif/src/census.rs deleted file mode 100644 index 6dbe035fca..0000000000 --- a/crates/heif/src/census.rs +++ /dev/null @@ -1,122 +0,0 @@ -//! Caps on the container's metadata boxes, counted before heic-rs touches the file. -//! -//! heic-rs 0.1.4 (pinned `=0.1.4`) has no aggregate work budget inside its container parsing: -//! every `ipma` association entry rescans the vector of already-seen item ids (quadratic in -//! the number of entries), and an `iloc` extent using construction method 2 reconstructs its -//! source item afresh for every extent that references it, with no shared cache. Legitimate -//! phone-photo metadata never comes near these caps — real corpora keep both well under a few -//! kilobytes — so anything above them is hostile or corrupt and is refused before the -//! countable CPU work happens. -//! -//! [`depth_boxes`] stops silently at structural blips: heic-rs's own parser reports those -//! better than this walk could, and an `ipma` box past the cap is caught wherever it parses. - -use heic_rs::boxes::BoxIter; - -use crate::Error; - -/// The largest `ipma` box (item ↔ property associations) payload accepted, 64 KiB. An entry -/// costs at least 3 bytes, so this bounds heic-rs's per-entry rescan of earlier item ids to -/// ~21 000 entries ≈ 220 million comparisons: long enough to be annoying, never minutes. -const MAX_IPMA_BYTES: usize = 64 << 10; - -/// The largest `iloc` box (item locations) payload accepted, 64 KiB. An extent costs at least -/// 12 bytes, so this bounds both the parse itself and how many extents a -/// construction-method-2 reconstruction chain can visit. -const MAX_ILOC_BYTES: usize = 64 << 10; - -/// Falls through unless the container declares more `ipma` or `iloc` payload than a -/// legitimate phone photo ever does. -pub fn cap(bytes: &[u8]) -> Result<(), Error> { - let mut c = Census::default(); - depth_boxes(&mut c, BoxIter::new(bytes), 0); - if c.ipma > MAX_IPMA_BYTES { - return Err(Error::Limit(format!("the image carries {} bytes of property associations, over the {MAX_IPMA_BYTES}-byte cap", c.ipma))); - } - if c.iloc > MAX_ILOC_BYTES { - return Err(Error::Limit(format!("the image carries {} bytes of item locations, over the {MAX_ILOC_BYTES}-byte cap", c.iloc))); - } - Ok(()) -} - -/// The metadata-box payload sizes seen so far. -#[derive(Default)] -struct Census { - ipma: usize, - iloc: usize, -} - -/// Walks boxes, adding the payload sizes of those named in [`Census`]. -/// -/// Only `meta` and `iprp` (the two nesting boxes the metadata is built from) are descended -/// into, no deeper than that depth, so a nested-box trick can't hide a box from the count. -fn depth_boxes(c: &mut Census, boxes: BoxIter<'_>, depth: u32) { - if depth > 3 { - return; - } - for b in boxes { - // A structural error stops the walk; `heic_rs::probe` reports it properly afterwards. - let Ok(b) = b else { return }; - if b.is(b"ipma") { - c.ipma = c.ipma.saturating_add(b.payload.len()); - } else if b.is(b"iloc") { - c.iloc = c.iloc.saturating_add(b.payload.len()); - } else if b.is(b"meta") { - // `meta` is a FullBox: version and flags sit between its header and its children. - if b.payload.len() >= 4 { - depth_boxes(c, BoxIter::new(&b.payload[4..]), depth + 1); - } - } else if b.is(b"iprp") { - depth_boxes(c, b.children(), depth + 1); - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - /// Assembles ftyp + meta(version/flags + iprp(ipco, ipma)) by hand; only the structure - /// the census walks matters, no item is ever defined. - fn file_with(ipma_payload: &[u8]) -> Vec { - let mut out = b"\0\0\0\x18ftypheic\0\0\0\0mif1heic".to_vec(); - let mut iprp = Vec::new(); - iprp.extend(box_of(b"ipco", b"")); - iprp.extend(box_of(b"ipma", ipma_payload)); - let mut meta = vec![0u8, 0, 0, 0]; // version 0, flags 0 - meta.extend(box_of(b"iprp", &iprp)); - out.extend(box_of(b"meta", &meta)); - out - } - - fn box_of(kind: &[u8; 4], body: &[u8]) -> Vec { - let mut out = Vec::with_capacity(8 + body.len()); - let size = u32::try_from(8 + body.len()).expect("test box fits in u32"); - out.extend_from_slice(&size.to_be_bytes()); - out.extend_from_slice(kind); - out.extend_from_slice(body); - out - } - - #[test] - fn an_oversized_ipma_is_refused() { - // 40 000 entries of 6 bytes: 240 KiB of ipma, far over the cap, and quadratic if - // parsed (each entry rescans the earlier item ids in heic-rs 0.1.4). - let mut ipma = Vec::new(); - for _ in 0..40_000u32 { - ipma.extend_from_slice(&[0u8, 0, 0, 0, 0, 0]); - } - let bytes = file_with(&ipma); - let r = crate::probe(&bytes).map(|_| ()); - assert!(matches!(r, Err(Error::Limit(ref m)) if m.contains("associations")), "{r:?}"); - } - - #[test] - fn a_undersized_ipma_is_untouched_by_the_caps() { - // The same shape with a normal ipma (two entries): the caps pass, and whatever error - // follows must not come from them. - let bytes = file_with(&[0, 0, 4, 1, 1, 2, 0, 0]); - let r = crate::probe(&bytes); - assert!(!matches!(r, Err(Error::Limit(ref m)) if m.contains("cap")), "{r:?}"); - } -} diff --git a/crates/heif/src/lib.rs b/crates/heif/src/lib.rs deleted file mode 100644 index 5b20e31d75..0000000000 --- a/crates/heif/src/lib.rs +++ /dev/null @@ -1,528 +0,0 @@ -//! `lightcraft-heif`: the optional HEIF / HEIC (iPhone and Mac photo) decoder. -//! -//! `heic-rs`, a pure-Rust HEIF container and HEVC still-picture decoder, reads the container and -//! reconstructs the coded pictures (bit-exact against libde265 on iPhone photos); this crate turns -//! them into RGB itself ([`ycc`]) so the colours match libheif: it honours the HEVC VUI's range -//! and matrix ([`vui`]) where heic-rs assumes BT.709 limited range, and upsamples chroma like -//! libheif. Single pictures and grid-tiled photos, 8- to 16-bit, monochrome, alpha auxiliary -//! images, ICC, EXIF and XMP. The API is plain data ([`Info`], [`Decoded`], [`Error`]) so the -//! crate knows nothing about the rest of LightCraft; `lightcraft-codecs` adapts it behind its -//! `heif` feature. Shares heic-rs and its regression fixtures with PhotoCraft's `photocraft-heif`. -//! -//! HEIF records orientation in the container (`irot`/`imir`, plus a `clap` crop), not in EXIF. -//! [`Options::apply_transforms`] applies them; with it off, the pixels come back as coded. -//! Auxiliary images other than alpha (HDR gain maps, depth maps) are ignored. -//! -//! Never panics: heic-rs is young, so every call into it runs under `catch_unwind` and a panic -//! inside it becomes [`Error::Malformed`]. - -#![forbid(unsafe_code)] -#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] - -mod census; -mod orient; -/// Lossless synthetic HEIF files for tests, here and in the crates above (feature `testdata`). -#[cfg(any(test, feature = "testdata"))] -#[doc(hidden)] -pub mod testdata; -mod vui; -mod ycc; - -use std::borrow::Cow; - -use heic_rs::context::Context; -use heic_rs::hevc::Frame; -use heic_rs::props::ItemProps; -use heic_rs::props::colr::Range; - -/// What a HEIF file declares, read from the container alone (no pixel is decoded). -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Info { - /// Size after the container transforms (rotation, mirror, crop): what a viewer shows. - pub width: u32, - pub height: u32, - /// Size as coded, before the transforms. - pub coded_width: u32, - pub coded_height: u32, - /// The primary image has an alpha auxiliary image. - pub has_alpha: bool, - /// Bits per sample as coded (8 for most photos, 10 for HDR-capable ones). - pub bit_depth: u8, -} - -/// Decode settings. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Options { - /// Refuse images with more pixels than this (checked before decoding). - pub max_pixels: u64, - /// Apply the container's rotation, mirror and crop (`irot`/`imir`/`clap`). - pub apply_transforms: bool, -} - -impl Default for Options { - fn default() -> Self { - Options { max_pixels: 1 << 28, apply_transforms: true } - } -} - -/// How the file describes its colour: the code points of ITU-T H.273, from the `colr` `nclx` -/// box, else the HEVC VUI, else the standard defaults (2 = unspecified, limited range). -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Colour { - pub primaries: u16, - pub transfer: u16, - pub matrix: u16, - pub full_range: bool, -} - -/// A decoded image: interleaved RGB or RGBA, 16 bits per sample whatever the coded depth, -/// row-major, no padding. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Decoded { - pub width: u32, - pub height: u32, - /// Four channels (RGBA) instead of three (RGB). - pub has_alpha: bool, - /// Bits per sample as coded. - pub bit_depth: u8, - pub data: Vec, - pub colour: Colour, - /// The primary image's ICC profile. - pub icc: Option>, - /// EXIF as a TIFF structure (without HEIF's offset header). Its Orientation tag only mirrors - /// what the container says; it is returned verbatim. - pub exif: Option>, - /// The XMP packet. - pub xmp: Option, -} - -/// Why a file could not be decoded. -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum Error { - /// A valid file using something this decoder does not handle (image sequences, overlays…). - Unsupported(&'static str), - /// The image is larger than [`Options::max_pixels`] or holds an oversized box. - Limit(String), - /// Broken or truncated data (or a decoder bug, reported the same way). - Malformed(String), -} - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Error::Unsupported(m) => write!(f, "unsupported HEIF: {m}"), - Error::Limit(m) => write!(f, "limit exceeded: {m}"), - Error::Malformed(m) => write!(f, "malformed HEIF: {m}"), - } - } -} - -impl std::error::Error for Error {} - -const SEQUENCE: &str = "this file holds an image sequence; only HEIF still images can be opened"; - -/// heic-rs 0.1.1 reconstructs 4:2:0 and monochrome pictures exactly but fails on the 4:2:2 and -/// 4:4:4 ones some cameras write (Canon and Sony "HIF"); those failures are reported as this. -const CHROMA_422_444: &str = "4:2:2 and 4:4:4 HEIF images (as some Canon and Sony cameras write) can't be decoded yet"; - -pub(crate) fn err(e: heic_rs::Error) -> Error { - match e { - heic_rs::Error::Unsupported(what) => Error::Unsupported(what), - // The primary item is neither an HEVC picture nor a grid of them. - heic_rs::Error::MissingBox("hvcC") => Error::Unsupported("the image is an overlay, an identity derivation or not HEVC-coded"), - heic_rs::Error::PixelLimit { .. } | heic_rs::Error::BoxTooLarge { .. } => Error::Limit(e.to_string()), - e => Error::Malformed(e.to_string()), - } -} - -/// Runs `f`, turning a heic-rs panic into an error. Fuzzing in the sibling app found two -/// out-of-range slices in heic-rs 0.1.1 on malformed files: in its box parser (`boxes.rs:130`) -/// and during reconstruction (`hevc/decode/recon.rs:70`). -fn guarded(f: impl FnOnce() -> Result) -> Result { - std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).unwrap_or_else(|_| Err(Error::Malformed("the HEIF decoder failed on this file".into()))) -} - -/// Reads the container's declared size, depth and alpha without decoding pixels, so callers can -/// check their limits first. Also refuses a file cut short: every coded picture of the primary -/// image must lie within `bytes`. -pub fn probe(bytes: &[u8]) -> Result { - guarded(|| probe_unguarded(bytes)) -} - -fn probe_unguarded(bytes: &[u8]) -> Result { - census::cap(bytes)?; - // No `meta` box: no still image, only a `moov` image sequence (a video track). - let open_error = |e| match e { - heic_rs::Error::MissingBox("meta") => Error::Unsupported(SEQUENCE), - e => err(e), - }; - let ctx = Context::open(bytes).map_err(open_error)?; - let id = ctx.meta.primary; - // heic-rs 0.1.4 caps the size of what it assembles, not the work: an item built from other - // items (`iloc` construction method 2) is rebuilt once per extent, so a chain of such items - // multiplies at every level while each result stays small. Count those visits from the - // `iloc` table before reconstructing anything: the primary item first (`heic_rs::probe` and - // `Context::grid` both rebuild its descriptor), then its alpha plane, then every tile - // reference, repeats included. - let mut visits = 0_usize; - item_visits(&ctx, id, 0, &mut visits)?; - let info = heic_rs::probe(bytes).map_err(open_error)?; - if let Some(alpha) = ctx.alpha_item(id).map_err(err)? { - item_visits(&ctx, alpha, 0, &mut visits)?; - } - let mut coded = match ctx.grid(id).map_err(err)? { - Some((_, tiles)) => tiles, - None => vec![id], - }; - for &item in coded.iter().filter(|&&item| item != id) { - item_visits(&ctx, item, 0, &mut visits)?; - } - // A `dimg` listing may name the same tile more than once, and reconstructable items are - // moreover rebuilt for every extent that references them in heic-rs 0.1.4. For the size - // check each unique item counts once: the total reconstruction (all of it assembled from - // `bytes` by heic-rs) is held below one copy of the file, above which a repeat or chain - // wants more bytes than exist. Order doesn't matter here: this only checks that every - // tile's bytes lie in the file. - coded.sort_unstable(); - coded.dedup(); - let mut reconstructed = 0_usize; - for item in coded { - reconstructed = reconstructed.saturating_add(ctx.item_data(item).map_err(err)?.len()); - if reconstructed > bytes.len() { - return Err(Error::Limit(format!( - "the container holds {reconstructed} bytes of item data, over the {} bytes of the file itself", - bytes.len() - ))); - } - } - Ok(Info { - width: info.width, - height: info.height, - coded_width: info.coded_width, - coded_height: info.coded_height, - has_alpha: info.has_alpha, - bit_depth: info.bit_depth, - }) -} - -/// Most extent visits heic-rs may spend reconstructing the primary image's coded items; real -/// files need one per tile (plus a few for the rare derived item). -const MAX_ITEM_VISITS: usize = 4096; -/// Deepest chain of items built from items we follow (heic-rs refuses beyond eight itself). -const MAX_ITEM_CHAIN: u32 = 8; - -/// Adds to `visits` the extents heic-rs would read to reconstruct `id`, following items built -/// from other items, and fails once that passes [`MAX_ITEM_VISITS`] or the chain is too deep. -fn item_visits(ctx: &Context<'_>, id: u32, depth: u32, visits: &mut usize) -> Result<(), Error> { - use heic_rs::meta::iloc::Construction; - if depth > MAX_ITEM_CHAIN { - return Err(Error::Limit(format!("items built from items nest deeper than {MAX_ITEM_CHAIN} levels"))); - } - let Some(loc) = ctx.meta.location(id) else { return Ok(()) }; - *visits = visits.saturating_add(loc.extents.len().max(1)); - if *visits > MAX_ITEM_VISITS { - return Err(Error::Limit(format!("reconstructing the image takes over {MAX_ITEM_VISITS} item extent reads"))); - } - if loc.construction == Construction::Item { - for e in &loc.extents { - if let Ok(source) = u32::try_from(e.index) - && source != id - { - item_visits(ctx, source, depth + 1, visits)?; - } - } - } - Ok(()) -} - -/// Decodes the primary image, with its metadata. -pub fn decode(bytes: &[u8], options: &Options) -> Result { - guarded(|| decode_unguarded(bytes, options)) -} - -/// Decodes the smallest thumbnail image the file carries whose long edge is at least -/// `min_edge`, if any (iPhones store one of about 320 × 240). Transforms are applied as for the -/// primary image; the metadata (and, when the thumbnail has no profile of its own, the ICC -/// profile) are the primary image's. -pub fn decode_thumbnail(bytes: &[u8], min_edge: u32, options: &Options) -> Result, Error> { - guarded(|| { - let ctx = Context::open(bytes).map_err(err)?; - let primary = ctx.meta.primary; - let mut best: Option<(u32, u32)> = None; - // A thumbnail whose own reconstruction would cost too much is not a candidate; the visits - // of all the candidates share one budget. - let mut candidate_visits = 0_usize; - for r in ctx.meta.refs.iter().filter(|r| r.kind == heic_rs::meta::iref::RefKind::Thmb && r.to.contains(&primary)) { - if item_visits(&ctx, r.from, 0, &mut candidate_visits).is_err() { - continue; - } - let Ok(p) = ctx.props(r.from) else { continue }; - let Ok((w, h)) = ctx.coded_size(r.from, &p) else { continue }; - let edge = w.max(h); - if edge >= min_edge && best.is_none_or(|(_, e)| edge < e) { - best = Some((r.from, edge)); - } - } - let Some((id, _)) = best else { return Ok(None) }; - // The same pre-scan as `probe`: the thumbnail's own descriptor and every tile reference - // are counted before heic-rs rebuilds any of them. - let mut visits = 0_usize; - item_visits(&ctx, id, 0, &mut visits)?; - if let Some((_, tiles)) = ctx.grid(id).map_err(err)? { - for &tile in &tiles { - item_visits(&ctx, tile, 0, &mut visits)?; - } - } - let mut decoded = decode_item(&ctx, id, options, false)?; - if decoded.icc.is_none() { - decoded.icc = item_icc(&ctx, primary); - } - (decoded.exif, decoded.xmp) = metadata(&ctx); - Ok(Some(decoded)) - }) -} - -/// An item's ICC profile; a grid whose own properties carry none takes its first tile's (as on -/// some iPhones). -fn item_icc(ctx: &Context<'_>, id: u32) -> Option> { - let p = ctx.props(id).ok()?; - let icc = p.icc.or_else(|| { - let (_, tiles) = ctx.grid(id).ok()??; - ctx.props(*tiles.first()?).ok()?.icc - }); - icc.map(<[u8]>::to_vec) -} - -fn decode_unguarded(bytes: &[u8], options: &Options) -> Result { - // Refuses sequences with a clear message before anything else is read. - probe_unguarded(bytes)?; - let ctx = Context::open(bytes).map_err(err)?; - let mut decoded = decode_item(&ctx, ctx.meta.primary, options, true)?; - let (exif, xmp) = metadata(&ctx); - decoded.exif = exif; - decoded.xmp = xmp; - Ok(decoded) -} - -/// Decodes one image item (a coded picture or a grid of them) to RGB(A), alpha included when -/// `with_alpha` and the item has an alpha auxiliary image. -fn decode_item(ctx: &Context<'_>, id: u32, options: &Options, with_alpha: bool) -> Result { - let p = ctx.props(id).map_err(err)?; - // heic-rs 0.1.4 keeps every associated transform and its [`orient::apply`] allocates the - // full picture again for each: a file could chain the repetition and multiply decode CPU - // without changing any checked size. Real files carry at most one `clap`, `irot` and - // `imir`, so refuse anything approaching [`MAX_TRANSFORMS`] (CodeRabbit #44). - if p.transforms.len() > MAX_TRANSFORMS { - return Err(Error::Limit(format!("the image carries {} transform properties, over the {MAX_TRANSFORMS}-property cap", p.transforms.len()))); - } - let limit = options.max_pixels; - // The declared sizes are refused before anything is decoded or allocated. - let (cw, ch) = ctx.coded_size(id, &p).map_err(err)?; - heic_rs::image::check_pixels(cw, ch, limit).map_err(err)?; - let (tw, th) = heic_rs::transform::transformed_size(cw, ch, &p.transforms).map_err(err)?; - heic_rs::image::check_pixels(tw, th, limit).map_err(err)?; - - let coded = Coded::decode(ctx, id, &p, limit)?; - let planes = coded.planes()?; - let colour = colour(&p, &coded.first_props); - let signal = ycc::Signal { matrix: colour.matrix, full_range: colour.full_range }; - let alpha_id = if with_alpha { ctx.alpha_item(id).map_err(err)? } else { None }; - let (w, h) = planes.size(); - // A grid's canvas dimensions come from its payload, not the checked `ispe` (CodeRabbit - // #48): check the real one, before [`ycc::to_rgb16`] allocates for it. - match (u32::try_from(w), u32::try_from(h)) { - (Ok(wu), Ok(hu)) => { - heic_rs::image::check_pixels(wu, hu, limit).map_err(err)?; - } - _ => return Err(Error::Limit(format!("the image canvas is {w}x{h}, beyond the {limit}-pixel limit"))), - } - let mode = if options.apply_transforms && orient::odd_crop(w, h, &p.transforms)? { ycc::Upsampling::Bilinear } else { ycc::Upsampling::Nearest }; - let mut data = ycc::to_rgb16(&planes, signal, alpha_id.is_some(), mode) - .ok_or_else(|| Error::Malformed("the decoded pictures don't cover the image".into()))?; - if let Some(aux) = alpha_id { - let ap = ctx.props(aux).map_err(err)?; - let alpha = Coded::decode(ctx, aux, &ap, limit)?; - let alpha_planes = alpha.planes()?; - if alpha_planes.size() != (w, h) { - return Err(Error::Unsupported("an alpha image of a different size than the picture")); - } - ycc::put_alpha(&alpha_planes, &mut data).ok_or_else(|| Error::Malformed("the alpha image is incomplete".into()))?; - } - let bit_depth = planes.bit_depth(); - let has_alpha = alpha_id.is_some(); - let mut img = orient::Pixels { width: w, height: h, ch: if has_alpha { 4 } else { 3 }, data }; - if options.apply_transforms { - img = orient::apply(img, &p.transforms)?; - } - Ok(Decoded { - width: u32::try_from(img.width).map_err(|_| Error::Limit("image too wide".into()))?, - height: u32::try_from(img.height).map_err(|_| Error::Limit("image too tall".into()))?, - has_alpha, - bit_depth, - data: img.data, - colour, - // A grid whose own properties carry no profile takes its tiles' (as on some iPhones). - icc: p.icc.or(coded.first_props.icc).map(<[u8]>::to_vec), - exif: None, - xmp: None, - }) -} - -/// How many times `max_pixels` the coded pictures of one item may hold together: a grid's tiles -/// cover its canvas rounded up to whole tiles, so legitimate grids sit near 1. -const GRID_BUDGET: u64 = 2; - -/// The most transform properties (`clap`/`irot`/`imir`) one item may carry. Real files have at -/// most one of each, so anything approaching this is a reordered or hostile association list. -const MAX_TRANSFORMS: usize = 12; - -/// The decoded pictures of an item: one, or a grid's tiles in `dimg` order. -struct Coded<'a> { - frames: Vec, - grid: Option, - /// The properties of the first coded picture (the item itself, or the first tile): where the - /// HEVC configuration and its VUI live. - first_props: ItemProps<'a>, - /// A failed 4:2:2 / 4:4:4 picture, for the error message. - chroma_format: u8, -} - -impl<'a> Coded<'a> { - fn decode(ctx: &Context<'a>, id: u32, p: &ItemProps<'a>, limit: u64) -> Result, Error> { - let (grid, ids) = match ctx.grid(id).map_err(err)? { - Some((g, tiles)) => (Some(g), tiles), - None => (None, vec![id]), - }; - let first = *ids.first().ok_or_else(|| Error::Malformed("grid lists no tiles".into()))?; - let first_props = if first == id { p.clone() } else { ctx.props(first).map_err(err)? }; - let chroma_format = first_props.hvcc.as_ref().map_or(1, |c| c.chroma_format); - // #1518: the decoder allocates each picture at its SPS's uncropped size, which a - // conformance window (or a slice picking another SPS) can make far larger than the - // declared one, and a grid holds all its tiles at once. Both are refused before decoding. - let mut total = 0u64; - for t in &ids { - let tp = ctx.props(*t).map_err(err)?; - let hvcc = tp.hvcc.as_ref().ok_or(heic_rs::Error::MissingBox("hvcC")).map_err(err)?; - let sizes = vui::coded_sizes(&hvcc.parameter_sets()).ok_or_else(|| Error::Malformed("unreadable sequence parameter set".into()))?; - let mut largest = 0u64; - for (w, h) in sizes { - heic_rs::image::check_pixels(w, h, limit).map_err(err)?; - largest = largest.max(u64::from(w) * u64::from(h)); - } - total = total.saturating_add(largest); - } - if total > limit.saturating_mul(GRID_BUDGET) { - return Err(Error::Limit(format!("the coded pictures hold {total} pixels, over the {limit}-pixel limit"))); - } - let decode_one = |t: &u32| -> Result { - let tp = ctx.props(*t).map_err(err)?; - let hvcc = tp.hvcc.as_ref().ok_or(heic_rs::Error::MissingBox("hvcC")).map_err(err)?; - let data = ctx.item_data(*t).map_err(err)?; - let nals = hvcc.split_nals(&data).map_err(err)?; - let params = hvcc.parameter_sets(); - if params.is_empty() { - return Err(Error::Malformed("hvcC carries no parameter sets".into())); - } - let frame = heic_rs::hevc::decode_still(¶ms, &nals).map_err(err)?; - frame.validate().map_err(err)?; - heic_rs::image::check_pixels(frame.width, frame.height, limit).map_err(err)?; - Ok(frame) - }; - #[cfg(not(target_arch = "wasm32"))] - let frames: Result, Error> = { - use rayon::prelude::*; - ids.par_iter().map(decode_one).collect() - }; - #[cfg(target_arch = "wasm32")] - let frames: Result, Error> = ids.iter().map(decode_one).collect(); - let frames = frames.map_err(|e| match e { - Error::Malformed(_) if chroma_format >= 2 => Error::Unsupported(CHROMA_422_444), - e => e, - })?; - Ok(Coded { frames, grid, first_props, chroma_format }) - } - - fn planes(&self) -> Result, Error> { - match (&self.grid, self.frames.as_slice()) { - (Some(g), tiles) => heic_rs::grid::Mosaic::new(g, tiles).map(ycc::Planes::Grid).map_err(err), - (None, [frame]) => Ok(ycc::Planes::Picture(frame)), - (None, _) => Err(Error::Malformed(format!("expected one coded picture (chroma format {})", self.chroma_format))), - } - } -} - -/// The colour description in force: `nclx` on the item (or, for a grid without one, on its -/// first tile), else the VUI of its (first) coded picture, else the H.265 defaults. -fn colour(p: &ItemProps<'_>, coded: &ItemProps<'_>) -> Colour { - if let Some(n) = p.nclx.or(coded.nclx) { - return Colour { primaries: n.primaries, transfer: n.transfer, matrix: n.matrix_code, full_range: n.range == Range::Full }; - } - let s = coded.hvcc.as_ref().and_then(|c| vui::signal(&c.parameter_sets())).unwrap_or_default(); - Colour { primaries: s.primaries.into(), transfer: s.transfer.into(), matrix: s.matrix.into(), full_range: s.full_range } -} - -/// The primary image's EXIF (TIFF structure) and XMP packet. Metadata is a courtesy: a broken -/// metadata item never fails a decode whose pixels came out fine. -fn metadata(ctx: &Context<'_>) -> (Option>, Option) { - let id = ctx.meta.primary; - // Metadata is best effort, so an item that would cost too much to rebuild is skipped, not an error. - let mut visits = 0_usize; - let cheap = |item: Option, visits: &mut usize| item.is_none_or(|item| item_visits(ctx, item, 0, visits).is_ok()); - let exif = if cheap(ctx.exif_item(id), &mut visits) { ctx.exif(id).ok().flatten().map(<[u8]>::to_vec) } else { None }; - let xmp_item = ctx.xmp_item(id).filter(|&x| cheap(Some(x), &mut visits)); - let xmp = xmp_item.and_then(|x| ctx.item_data(x).ok()).and_then(|d| match d { - Cow::Borrowed(b) => String::from_utf8(b.to_vec()).ok(), - Cow::Owned(b) => String::from_utf8(b).ok(), - }); - (exif, xmp.map(|x| x.trim_end_matches('\0').to_string())) -} - -#[cfg(test)] -mod tests_synthetic; - -#[cfg(test)] -mod tests { - use super::*; - - /// Found by PhotoCraft's `decode_heif` fuzz target: a malformed box makes heic-rs 0.1.1 slice - /// out of range (`boxes.rs:130`). It must be an error, not a crash. - const HEIC_RS_BOX_PANIC: [u8; 72] = [ - 0x00, 0x00, 0x00, 0x24, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x69, 0x66, 0x31, 0x4d, 0x69, 0x50, - 0x72, 0x6d, 0x69, 0x61, 0x66, 0x4d, 0x69, 0x48, 0x42, 0x72, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x65, 0x74, 0x61, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x75, 0x75, 0x69, 0x64, 0x00, 0x00, 0x00, 0x00, 0x03, 0x08, 0x08, 0x08, 0x00, 0x03, 0x01, 0x03, 0x70, - 0x00, 0xa8, 0x00, - ]; - - #[test] - fn a_heic_rs_panic_is_a_malformed_error() { - for apply_transforms in [true, false] { - let r = decode(&HEIC_RS_BOX_PANIC, &Options { apply_transforms, ..Default::default() }); - assert!(matches!(r, Err(Error::Malformed(_))), "{r:?}"); - } - } - - #[test] - fn image_sequence_is_unsupported_with_a_clear_message() { - // An ftyp and nothing else: a HEIF image sequence would have a moov here instead of a meta. - let bytes = b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"; - for r in [probe(bytes).map(|_| ()), decode(bytes, &Options::default()).map(|_| ())] { - assert!(matches!(&r, Err(Error::Unsupported(m)) if m.contains("sequence")), "{r:?}"); - } - } - - #[test] - fn garbage_is_an_error() { - for bytes in [&b""[..], b"\0\0\0\x18ftypheic\0\0\0\0mif1heic", &[0xFF; 64]] { - assert!(probe(bytes).is_err()); - assert!(decode(bytes, &Options::default()).is_err()); - assert!(decode_thumbnail(bytes, 1, &Options::default()).is_err()); - } - } - - #[test] - fn errors_display_their_kind() { - assert!(Error::Unsupported("x").to_string().contains("unsupported")); - assert!(Error::Limit("x".into()).to_string().contains("limit")); - assert!(Error::Malformed("x".into()).to_string().contains("malformed")); - } -} diff --git a/crates/heif/src/orient.rs b/crates/heif/src/orient.rs deleted file mode 100644 index 487eb7e220..0000000000 --- a/crates/heif/src/orient.rs +++ /dev/null @@ -1,178 +0,0 @@ -//! The container's transformative properties (`clap` crop, `irot` rotation, `imir` mirror), -//! applied to interleaved 16-bit pixels in the order the file associates them. -//! -//! `imir` follows libheif, which writes and reads `axis = 0` as a flip about the horizontal axis -//! (top and bottom exchanged) and `axis = 1` as a left-right flip. heic-rs 0.1.1 names the coded -//! values the other way round (`Mirror::LeftRight` is `axis = 0`), so they are swapped here. - -use heic_rs::props::Transform; -use heic_rs::props::simple::{Clap, Mirror, Rotation}; - -use crate::Error; - -/// Interleaved samples, `ch` per pixel, row-major. -pub struct Pixels { - pub width: usize, - pub height: usize, - pub ch: usize, - pub data: Vec, -} - -impl Pixels { - fn remap(&self, width: usize, height: usize, src: impl Fn(usize, usize) -> (usize, usize)) -> Pixels { - let ch = self.ch; - let mut data = vec![0u16; width * height * ch]; - for (y, row) in data.chunks_exact_mut(width * ch).enumerate() { - for (x, px) in row.chunks_exact_mut(ch).enumerate() { - let (sx, sy) = src(x, y); - let i = (sy * self.width + sx) * ch; - if let Some(s) = self.data.get(i..i + ch) { - px.copy_from_slice(s); - } - } - } - Pixels { width, height, ch, data } - } -} - -/// The rectangle `(left, top, width, height)` a `clap` selects from a `w` × `h` image. Its centre -/// is `((w - 1) / 2 + horizOff, (h - 1) / 2 + vertOff)`; a corner that falls between two pixels -/// is rounded as libheif does: left down, top up. -pub fn crop_rect(w: usize, h: usize, c: Clap) -> Result<(usize, usize, usize, usize), Error> { - let bad = || Error::Malformed("clap selects an impossible rectangle".into()); - let size = |(n, d): (u32, u32)| (d != 0).then(|| n / d).ok_or_else(bad); - let (cw, ch) = (size(c.width)? as usize, size(c.height)? as usize); - if cw == 0 || ch == 0 || cw > w || ch > h { - return Err(bad()); - } - // Twice the corner, so the half pixels of odd sizes stay exact. - let twice = |full: usize, cropped: usize, (n, d): (i32, u32)| -> Result { - if d == 0 { - return Err(bad()); - } - Ok((full - cropped) as i64 + (2 * i64::from(n)).div_euclid(i64::from(d))) - }; - let left = twice(w, cw, c.horiz_off)?.div_euclid(2); - let top = (twice(h, ch, c.vert_off)? + 1).div_euclid(2); - let left = usize::try_from(left).map_err(|_| bad())?; - let top = usize::try_from(top).map_err(|_| bad())?; - if left + cw > w || top + ch > h { - return Err(bad()); - } - Ok((left, top, cw, ch)) -} - -/// Whether a `clap` among `transforms` (applied to a `w` × `h` image) starts at an odd column -/// or row. libheif crops such images after upsampling their chroma bilinearly instead of with -/// its default nearest neighbour; [`crate::ycc`] follows it so the pixels match. -pub fn odd_crop(mut w: usize, mut h: usize, transforms: &[Transform]) -> Result { - for t in transforms { - match *t { - Transform::Crop(c) => { - let (left, top, cw, ch) = crop_rect(w, h, c)?; - if left % 2 == 1 || top % 2 == 1 { - return Ok(true); - } - (w, h) = (cw, ch); - } - Transform::Rotate(Rotation::Ccw90 | Rotation::Ccw270) => (w, h) = (h, w), - _ => {} - } - } - Ok(false) -} - -pub fn apply(mut img: Pixels, transforms: &[Transform]) -> Result { - for t in transforms { - let (w, h) = (img.width, img.height); - img = match *t { - Transform::Crop(c) => { - let (left, top, cw, ch) = crop_rect(w, h, c)?; - img.remap(cw, ch, |x, y| (x + left, y + top)) - } - // Counter-clockwise: the right column becomes the top row. - Transform::Rotate(Rotation::Ccw90) => img.remap(h, w, |x, y| (w - 1 - y, x)), - Transform::Rotate(Rotation::Ccw180) => img.remap(w, h, |x, y| (w - 1 - x, h - 1 - y)), - Transform::Rotate(Rotation::Ccw270) => img.remap(h, w, |x, y| (y, h - 1 - x)), - Transform::Rotate(Rotation::None) => img, - // `axis = 0` (heic-rs's `LeftRight`): top and bottom exchanged, as libheif reads it. - Transform::Mirror(Mirror::LeftRight) => img.remap(w, h, |x, y| (x, h - 1 - y)), - // `axis = 1`: left and right exchanged. - Transform::Mirror(Mirror::TopBottom) => img.remap(w, h, |x, y| (w - 1 - x, y)), - }; - } - Ok(img) -} - -#[cfg(test)] -mod tests { - use super::*; - - /// 3×2, one channel: 1 2 3 / 4 5 6. - fn img() -> Pixels { - Pixels { width: 3, height: 2, ch: 1, data: vec![1, 2, 3, 4, 5, 6] } - } - - fn run(t: Transform) -> (usize, usize, Vec) { - let p = apply(img(), &[t]).unwrap(); - (p.width, p.height, p.data) - } - - #[test] - fn rotations_are_counter_clockwise() { - assert_eq!(run(Transform::Rotate(Rotation::Ccw90)), (2, 3, vec![3, 6, 2, 5, 1, 4])); - assert_eq!(run(Transform::Rotate(Rotation::Ccw180)), (3, 2, vec![6, 5, 4, 3, 2, 1])); - assert_eq!(run(Transform::Rotate(Rotation::Ccw270)), (2, 3, vec![4, 1, 5, 2, 6, 3])); - assert_eq!(run(Transform::Rotate(Rotation::None)), (3, 2, vec![1, 2, 3, 4, 5, 6])); - } - - #[test] - fn mirrors_follow_libheif() { - // axis 0 (heic-rs `LeftRight`) exchanges top and bottom; axis 1 left and right. - assert_eq!(run(Transform::Mirror(Mirror::LeftRight)), (3, 2, vec![4, 5, 6, 1, 2, 3])); - assert_eq!(run(Transform::Mirror(Mirror::TopBottom)), (3, 2, vec![3, 2, 1, 6, 5, 4])); - } - - fn clap(w: u32, h: u32, ho: i32, vo: i32) -> Clap { - Clap { width: (w, 1), height: (h, 1), horiz_off: (ho, 1), vert_off: (vo, 1) } - } - - #[test] - fn clap_corners_round_like_libheif() { - // Measured against libheif 1.23 on a 512 × 512 picture. - assert_eq!(crop_rect(512, 512, clap(301, 201, 0, 0)).unwrap(), (105, 156, 301, 201)); - assert_eq!(crop_rect(512, 512, clap(300, 200, 0, 0)).unwrap(), (106, 156, 300, 200)); - assert_eq!(crop_rect(512, 512, clap(301, 201, 37, -21)).unwrap(), (142, 135, 301, 201)); - assert_eq!(crop_rect(512, 512, clap(301, 201, -3, 5)).unwrap(), (102, 161, 301, 201)); - assert_eq!(crop_rect(512, 512, clap(300, 201, 0, 0)).unwrap(), (106, 156, 300, 201)); - assert_eq!(crop_rect(1500, 1000, clap(1001, 701, 37, -21)).unwrap(), (286, 129, 1001, 701)); - let p = apply(img(), &[Transform::Crop(clap(2, 1, 1, 0))]).unwrap(); - // The half-pixel top rounds down a row. - assert_eq!((p.width, p.height, p.data), (2, 1, vec![5, 6])); - } - - #[test] - fn impossible_claps_are_errors() { - for c in [ - clap(0, 1, 0, 0), - clap(4, 1, 0, 0), - clap(3, 2, 1, 0), - clap(3, 2, 0, -1), - Clap { width: (1, 0), ..clap(1, 1, 0, 0) }, - Clap { horiz_off: (0, 0), ..clap(1, 1, 0, 0) }, - ] { - assert!(crop_rect(3, 2, c).is_err(), "{c:?}"); - } - assert!(apply(img(), &[Transform::Crop(clap(9, 9, 0, 0))]).is_err()); - } - - #[test] - fn odd_crops_are_detected_through_rotations() { - assert!(!odd_crop(512, 512, &[Transform::Crop(clap(300, 200, 0, 0))]).unwrap()); - assert!(odd_crop(512, 512, &[Transform::Crop(clap(301, 200, 0, 0))]).unwrap()); - // After a quarter turn the crop applies to a 200 × 300 image. - let t = [Transform::Crop(clap(300, 200, 0, 0)), Transform::Rotate(Rotation::Ccw90), Transform::Crop(clap(197, 300, 0, 0))]; - assert!(odd_crop(512, 512, &t).unwrap()); - assert!(!odd_crop(512, 512, &[Transform::Rotate(Rotation::Ccw90)]).unwrap()); - } -} diff --git a/crates/heif/src/testdata.rs b/crates/heif/src/testdata.rs deleted file mode 100644 index 595cfa33c5..0000000000 --- a/crates/heif/src/testdata.rs +++ /dev/null @@ -1,542 +0,0 @@ -//! Synthetic HEIF files for tests (feature `testdata`): lossless, so every decoded sample is known. -//! -//! Each coded picture is one 32 × 32 coding tree unit stored as PCM samples (H.265 7.3.8.7): -//! the bitstream carries the YCbCr values verbatim, so the decoder's HEVC stage reproduces them -//! exactly and a test checks everything after it (grid composition, colour, transforms, -//! metadata) against values it chose. The few CABAC bins around the PCM data are written with a -//! minimal arithmetic encoder (9.3.4.4). Larger images are grids of such pictures, which is how -//! iPhones store photos too (with 512 × 512 tiles). -//! -//! Not a general HEIF or HEVC writer: 4:2:0 or monochrome, 8 or 10 bits, a CTU per picture. - -/// Side of every coded picture. -pub const TILE: u32 = 32; - -/// YCbCr samples at a luma position (chroma is taken at even positions, 4:2:0). -pub type Sampler<'a> = &'a dyn Fn(u32, u32) -> [u16; 3]; - -/// A property of the primary image. -#[derive(Debug, Clone, PartialEq)] -pub enum Prop { - /// `colr` of type `prof`. - Icc(Vec), - /// `colr` of type `nclx`: primaries, transfer, matrix, full range. - Nclx(u16, u16, u16, bool), - /// `irot`: quarter turns counter-clockwise. - Irot(u8), - /// `imir` with this axis. - Imir(u8), - /// `clap`: width, height, horizontal and vertical offset (whole pixels). - Clap(u32, u32, i32, i32), -} - -/// What [`build`] writes. -pub struct Spec<'a> { - /// Displayed (grid output) size, before transforms. - pub width: u32, - pub height: u32, - pub bit_depth: u8, - pub sample: Sampler<'a>, - /// Properties of the primary image, in association order. - pub props: Vec, - /// EXIF as a TIFF structure (written with HEIF's 4-byte offset header). - pub exif: Option>, - /// An alpha auxiliary image (monochrome, full range). - pub alpha: Option<&'a dyn Fn(u32, u32) -> u16>, - /// A thumbnail item of half the size with these samples (in its own coordinates). - pub thumbnail: Option>, - /// Auxiliary images of these types the decoder must ignore (HDR gain map, depth…). - pub ignored_aux: Vec<&'static str>, -} - -impl<'a> Spec<'a> { - pub fn new(width: u32, height: u32, sample: Sampler<'a>) -> Spec<'a> { - Spec { width, height, bit_depth: 8, sample, props: Vec::new(), exif: None, alpha: None, thumbnail: None, ignored_aux: Vec::new() } - } -} - -#[derive(Default)] -struct Bits { - bytes: Vec, - n: usize, -} - -impl Bits { - fn put(&mut self, v: u32, len: u32) { - for i in (0..len).rev() { - if self.n.is_multiple_of(8) { - self.bytes.push(0); - } - if (v >> i) & 1 == 1 - && let Some(b) = self.bytes.last_mut() - { - *b |= 0x80 >> (self.n % 8); - } - self.n += 1; - } - } - - fn ue(&mut self, v: u32) { - let x = u64::from(v) + 1; - let len = 64 - x.leading_zeros(); - self.put(0, len - 1); - self.put((x & 0xffff_ffff) as u32, len); - } - - fn align_zero(&mut self) { - while !self.n.is_multiple_of(8) { - self.put(0, 1); - } - } - - fn trailing(&mut self) { - self.put(1, 1); - self.align_zero(); - } -} - -/// The CABAC arithmetic encoder of H.265 9.3.4.4, for the bins these pictures need. -struct Cabac { - low: u32, - range: u32, - first: bool, - outstanding: u32, -} - -impl Cabac { - fn new() -> Cabac { - Cabac { low: 0, range: 510, first: true, outstanding: 0 } - } - - fn put_bit(&mut self, out: &mut Bits, b: u32) { - if self.first { - self.first = false; - } else { - out.put(b, 1); - } - while self.outstanding > 0 { - out.put(1 - b, 1); - self.outstanding -= 1; - } - } - - fn renorm(&mut self, out: &mut Bits) { - while self.range < 256 { - if self.low < 256 { - self.put_bit(out, 0); - } else if self.low >= 512 { - self.low -= 512; - self.put_bit(out, 1); - } else { - self.low -= 256; - self.outstanding += 1; - } - self.range <<= 1; - self.low <<= 1; - } - } - - /// `part_mode` = PART_2Nx2N: the most probable symbol of a fresh context (initValue 184 at - /// QP 26 gives pStateIdx 0, valMps 1), so the LPS range is `rangeTabLps[0][3]` = 240. - fn part_mode_2nx2n(&mut self, out: &mut Bits) { - let lps = [128, 176, 208, 240][((self.range >> 6) & 3) as usize]; - self.range -= lps; - self.renorm(out); - } - - /// A terminating bin equal to 1 (`pcm_flag`, `end_of_slice_segment_flag`), with the flush. - fn terminate_one(&mut self, out: &mut Bits) { - self.range -= 2; - self.low += self.range; - self.range = 2; - self.renorm(out); - self.put_bit(out, (self.low >> 9) & 1); - out.put(((self.low >> 7) & 3) | 1, 2); - } -} - -fn nal(kind: u8, rbsp: &[u8]) -> Vec { - let mut out = vec![kind << 1, 1]; - let mut zeros = 0; - for &b in rbsp { - if zeros >= 2 && b <= 3 { - out.push(3); - zeros = 0; - } - zeros = if b == 0 { zeros + 1 } else { 0 }; - out.push(b); - } - out -} - -fn profile_idc(bit_depth: u8, mono: bool) -> u32 { - match (bit_depth, mono) { - (_, true) => 4, // range extensions - (8, false) => 1, - _ => 2, - } -} - -fn ptl(w: &mut Bits, idc: u32) { - w.put(0, 2); - w.put(0, 1); - w.put(idc, 5); - w.put(1 << (31 - idc), 32); // compatible with itself - w.put(0b1001, 4); // progressive, frame only - w.put(0, 22); - w.put(0, 21); - w.put(0, 1); - w.put(93, 8); // level 3.1 -} - -fn parameter_sets(bit_depth: u8, mono: bool) -> [Vec; 3] { - let idc = profile_idc(bit_depth, mono); - let bd = u32::from(bit_depth); - let mut v = Bits::default(); - v.put(0, 4); - v.put(3, 2); - v.put(0, 6); - v.put(0, 3); - v.put(1, 1); - v.put(0xffff, 16); - ptl(&mut v, idc); - v.put(0, 1); // vps_sub_layer_ordering_info_present_flag - v.ue(0); - v.ue(0); - v.ue(0); - v.put(0, 6); // vps_max_layer_id - v.ue(0); // vps_num_layer_sets_minus1 - v.put(0, 1); // vps_timing_info_present_flag - v.put(0, 1); // vps_extension_flag - v.trailing(); - - let mut s = Bits::default(); - s.put(0, 4); - s.put(0, 3); - s.put(1, 1); - ptl(&mut s, idc); - s.ue(0); - s.ue(if mono { 0 } else { 1 }); - s.ue(TILE); - s.ue(TILE); - s.put(0, 1); // conformance_window_flag - s.ue(bd - 8); - s.ue(bd - 8); - s.ue(4); - s.put(1, 1); - s.ue(0); - s.ue(0); - s.ue(0); - s.ue(2); // log2_min_luma_coding_block_size_minus3: 32 - s.ue(0); // log2_diff_max_min_luma_coding_block_size: CTB 32 - s.ue(0); // log2_min_luma_transform_block_size_minus2: 4 - s.ue(3); // log2_diff_max_min_luma_transform_block_size: 32 - s.ue(0); - s.ue(0); - s.put(0, 1); // scaling_list_enabled_flag - s.put(0, 1); // amp_enabled_flag - s.put(0, 1); // sample_adaptive_offset_enabled_flag - s.put(1, 1); // pcm_enabled_flag - s.put(bd - 1, 4); // pcm_sample_bit_depth_luma_minus1 - s.put(bd - 1, 4); // pcm_sample_bit_depth_chroma_minus1 - s.ue(2); // log2_min_pcm_luma_coding_block_size_minus3: 32 - s.ue(0); // log2_diff_max_min_pcm_luma_coding_block_size - s.put(1, 1); // pcm_loop_filter_disabled_flag - s.ue(0); // num_short_term_ref_pic_sets - s.put(0, 1); // long_term_ref_pics_present_flag - s.put(0, 1); // sps_temporal_mvp_enabled_flag - s.put(0, 1); // strong_intra_smoothing_enabled_flag - s.put(0, 1); // vui_parameters_present_flag - s.put(0, 1); // sps_extension_present_flag - s.trailing(); - - let mut p = Bits::default(); - p.ue(0); - p.ue(0); - p.put(0, 6); // dependent slices, output flag, 3 extra header bits, sign hiding - p.put(0, 1); // cabac_init_present_flag - p.ue(0); - p.ue(0); - p.ue(0); // init_qp_minus26 (se(0) is the same bits) - p.put(0, 3); // constrained intra, transform skip, cu_qp_delta - p.ue(0); - p.ue(0); - p.put(0, 1); - p.put(0, 2); - p.put(0, 1); // transquant_bypass_enabled_flag - p.put(0, 1); // tiles_enabled_flag - p.put(0, 1); // entropy_coding_sync_enabled_flag - p.put(0, 1); // pps_loop_filter_across_slices_enabled_flag - p.put(1, 1); // deblocking_filter_control_present_flag - p.put(0, 1); // deblocking_filter_override_enabled_flag - p.put(1, 1); // pps_deblocking_filter_disabled_flag - p.put(0, 1); - p.put(0, 1); - p.ue(0); - p.put(0, 1); - p.put(0, 1); - p.trailing(); - [nal(32, &v.bytes), nal(33, &s.bytes), nal(34, &p.bytes)] -} - -/// One coded 32 × 32 picture: the `hvcC` payload and the item data (length-prefixed NAL). -struct Coded { - hvcc: Vec, - data: Vec, -} - -fn picture(bit_depth: u8, mono: bool, sample: &dyn Fn(u32, u32) -> [u16; 3]) -> Coded { - let bd = u32::from(bit_depth); - let mut w = Bits::default(); - w.put(1, 1); // first_slice_segment_in_pic_flag - w.put(0, 1); // no_output_of_prior_pics_flag - w.ue(0); - w.ue(2); // I slice - w.ue(0); // slice_qp_delta, se(0) - w.trailing(); // byte_alignment() - let mut c = Cabac::new(); - c.part_mode_2nx2n(&mut w); - c.terminate_one(&mut w); // pcm_flag - w.align_zero(); // pcm_alignment_zero_bit - for y in 0..TILE { - for x in 0..TILE { - w.put(u32::from(sample(x, y)[0]), bd); - } - } - if !mono { - for k in 1..3 { - for y in 0..TILE / 2 { - for x in 0..TILE / 2 { - w.put(u32::from(sample(2 * x, 2 * y)[k]), bd); - } - } - } - } - let mut c = Cabac::new(); - c.terminate_one(&mut w); // end_of_slice_segment_flag (its flush ends in the stop bit) - w.align_zero(); - let slice = nal(19, &w.bytes); - - let sets = parameter_sets(bit_depth, mono); - let mut h = vec![1u8, profile_idc(bit_depth, mono) as u8]; - h.extend_from_slice(&(1u32 << (31 - profile_idc(bit_depth, mono))).to_be_bytes()); - h.extend_from_slice(&[ - 0x90, - 0, - 0, - 0, - 0, - 0, - 93, - 0xf0, - 0, - 0xfc, - 0xfc | u8::from(!mono), - 0xf8 | (bit_depth - 8), - 0xf8 | (bit_depth - 8), - 0, - 0, - 0x0f, - 3, - ]); - for (kind, set) in [32u8, 33, 34].into_iter().zip(&sets) { - h.push(0x80 | kind); - h.extend_from_slice(&1u16.to_be_bytes()); - h.extend_from_slice(&(set.len() as u16).to_be_bytes()); - h.extend_from_slice(set); - } - let mut data = (slice.len() as u32).to_be_bytes().to_vec(); - data.extend_from_slice(&slice); - Coded { hvcc: h, data } -} - -fn bx(kind: &[u8; 4], payload: &[u8]) -> Vec { - let mut v = ((payload.len() + 8) as u32).to_be_bytes().to_vec(); - v.extend_from_slice(kind); - v.extend_from_slice(payload); - v -} - -fn full_box(kind: &[u8; 4], version: u8, payload: &[u8]) -> Vec { - let mut p = vec![version, 0, 0, 0]; - p.extend_from_slice(payload); - bx(kind, &p) -} - -struct Item { - id: u16, - kind: &'static [u8; 4], - data: Vec, - /// Indices (1-based) into the property list, with the essential flag. - props: Vec<(u8, bool)>, - /// Not an image of its own (a grid tile). - hidden: bool, -} - -#[derive(Default)] -struct File { - items: Vec, - props: Vec>, - refs: Vec<(&'static [u8; 4], u16, Vec)>, -} - -impl File { - fn prop(&mut self, b: Vec) -> u8 { - self.props.push(b); - self.props.len() as u8 - } - - fn add(&mut self, kind: &'static [u8; 4], data: Vec, props: Vec<(u8, bool)>) -> u16 { - let id = self.items.len() as u16 + 1; - self.items.push(Item { id, kind, data, props, hidden: false }); - id - } - - /// A coded image of `w` × `h` (a grid when larger than a tile); returns its item id. - fn image(&mut self, w: u32, h: u32, bit_depth: u8, mono: bool, sample: &dyn Fn(u32, u32) -> [u16; 3]) -> u16 { - let (cols, rows) = (w.div_ceil(TILE), h.div_ceil(TILE)); - let ispe = |w: u32, h: u32| { - let mut p = w.to_be_bytes().to_vec(); - p.extend_from_slice(&h.to_be_bytes()); - full_box(b"ispe", 0, &p) - }; - let mut hvcc_prop = None; - let tile_ispe = self.prop(ispe(TILE, TILE)); - let mut tiles = Vec::new(); - for r in 0..rows { - for c in 0..cols { - let coded = picture(bit_depth, mono, &|x, y| sample((c * TILE + x).min(w - 1), (r * TILE + y).min(h - 1))); - let hv = *hvcc_prop.get_or_insert_with(|| self.prop(bx(b"hvcC", &coded.hvcc))); - tiles.push(self.add(b"hvc1", coded.data, vec![(hv, true), (tile_ispe, false)])); - } - } - if let ([only], true) = (tiles.as_slice(), w == TILE && h == TILE) { - return *only; - } - for it in self.items.iter_mut().filter(|i| tiles.contains(&i.id)) { - it.hidden = true; - } - let mut g = vec![0u8, 0, (rows - 1) as u8, (cols - 1) as u8]; - g.extend_from_slice(&(w as u16).to_be_bytes()); - g.extend_from_slice(&(h as u16).to_be_bytes()); - let grid_ispe = self.prop(ispe(w, h)); - let id = self.add(b"grid", g, vec![(grid_ispe, false)]); - self.refs.push((b"dimg", id, tiles)); - id - } - - fn write(self, primary: u16) -> Vec { - let ftyp = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); - let mut hdlr = vec![0u8; 4]; - hdlr.extend_from_slice(b"pict"); - hdlr.extend_from_slice(&[0; 13]); - let hdlr = full_box(b"hdlr", 0, &hdlr); - let pitm = full_box(b"pitm", 0, &primary.to_be_bytes()); - let mut iinf = (self.items.len() as u16).to_be_bytes().to_vec(); - for it in &self.items { - let mut e = vec![2, 0, 0, u8::from(it.hidden)]; // version 2, flags - e.extend_from_slice(&it.id.to_be_bytes()); - e.extend_from_slice(&[0, 0]); - e.extend_from_slice(it.kind); - e.push(0); - iinf.extend_from_slice(&bx(b"infe", &e)); - } - let iinf = full_box(b"iinf", 0, &iinf); - let mut iref = Vec::new(); - for (kind, from, to) in &self.refs { - let mut r = from.to_be_bytes().to_vec(); - r.extend_from_slice(&(to.len() as u16).to_be_bytes()); - for t in to { - r.extend_from_slice(&t.to_be_bytes()); - } - iref.extend_from_slice(&bx(kind, &r)); - } - let iref = full_box(b"iref", 0, &iref); - let ipco = bx(b"ipco", &self.props.concat()); - let mut ipma = (self.items.iter().filter(|i| !i.props.is_empty()).count() as u32).to_be_bytes().to_vec(); - for it in self.items.iter().filter(|i| !i.props.is_empty()) { - ipma.extend_from_slice(&it.id.to_be_bytes()); - ipma.push(it.props.len() as u8); - ipma.extend(it.props.iter().map(|&(i, essential)| i | if essential { 0x80 } else { 0 })); - } - let iprp = bx(b"iprp", &[ipco, full_box(b"ipma", 0, &ipma)].concat()); - let iloc = |base: u32| { - let mut l = vec![0x44, 0x00]; - l.extend_from_slice(&(self.items.len() as u16).to_be_bytes()); - let mut offset = base; - for it in &self.items { - l.extend_from_slice(&it.id.to_be_bytes()); - l.extend_from_slice(&[0, 0, 0, 0, 0, 1]); // construction method 0, data ref 0, one extent - l.extend_from_slice(&offset.to_be_bytes()); - l.extend_from_slice(&(it.data.len() as u32).to_be_bytes()); - offset += it.data.len() as u32; - } - full_box(b"iloc", 1, &l) - }; - let meta = |base| full_box(b"meta", 0, &[hdlr.clone(), pitm.clone(), iloc(base), iinf.clone(), iref.clone(), iprp.clone()].concat()); - let start = (ftyp.len() + meta(0).len() + 8) as u32; - let mdat = bx(b"mdat", &self.items.iter().flat_map(|i| i.data.iter().copied()).collect::>()); - [ftyp, meta(start), mdat].concat() - } -} - -/// Writes the HEIF file `spec` describes. -pub fn build(spec: &Spec<'_>) -> Vec { - let mut f = File::default(); - let primary = f.image(spec.width, spec.height, spec.bit_depth, false, spec.sample); - let mut assoc = Vec::new(); - for p in &spec.props { - let b = match p { - Prop::Icc(icc) => bx(b"colr", &[b"prof".as_slice(), icc].concat()), - Prop::Nclx(pr, tc, mc, full) => { - let mut c = b"nclx".to_vec(); - for v in [pr, tc, mc] { - c.extend_from_slice(&v.to_be_bytes()); - } - c.push(if *full { 0x80 } else { 0 }); - bx(b"colr", &c) - } - Prop::Irot(a) => bx(b"irot", &[a & 3]), - Prop::Imir(a) => bx(b"imir", &[a & 1]), - Prop::Clap(w, h, ho, vo) => { - let mut c = Vec::new(); - for v in [*w as i32, 1, *h as i32, 1, *ho, 1, *vo, 1] { - c.extend_from_slice(&v.to_be_bytes()); - } - bx(b"clap", &c) - } - }; - assoc.push((f.prop(b), true, matches!(p, Prop::Clap(..)))); - } - if let Some(it) = f.items.iter_mut().find(|i| i.id == primary) { - it.props.extend(assoc.iter().map(|&(i, e, _)| (i, e))); - } - if let Some(exif) = &spec.exif { - let id = f.add(b"Exif", [&[0u8, 0, 0, 0][..], exif].concat(), Vec::new()); - f.refs.push((b"cdsc", id, vec![primary])); - } - let bd = spec.bit_depth; - let aux = |f: &mut File, urn: &str, sample: &dyn Fn(u32, u32) -> [u16; 3]| { - let id = f.image(spec.width, spec.height, bd, true, sample); - let auxc = f.prop(full_box(b"auxC", 0, &[urn.as_bytes(), &[0]].concat())); - if let Some(it) = f.items.iter_mut().find(|i| i.id == id) { - it.props.push((auxc, true)); - } - f.refs.push((b"auxl", id, vec![primary])); - }; - if let Some(alpha) = spec.alpha { - aux(&mut f, "urn:mpeg:hevc:2015:auxid:1", &|x, y| [alpha(x, y), 0, 0]); - } - for urn in &spec.ignored_aux { - aux(&mut f, urn, &|x, y| [((x + y) % 256) as u16, 0, 0]); - } - if let Some(sample) = spec.thumbnail { - // Half the size, with the primary image's colour and orientation (not its crop). - let id = f.image(spec.width / 2, spec.height / 2, spec.bit_depth, false, sample); - if let Some(it) = f.items.iter_mut().find(|i| i.id == id) { - it.props.extend(assoc.iter().filter(|a| !a.2).map(|&(i, e, _)| (i, e))); - } - f.refs.push((b"thmb", id, vec![primary])); - } - f.write(primary) -} diff --git a/crates/heif/src/tests_synthetic.rs b/crates/heif/src/tests_synthetic.rs deleted file mode 100644 index 1286ff82a9..0000000000 --- a/crates/heif/src/tests_synthetic.rs +++ /dev/null @@ -1,200 +0,0 @@ -//! Decoding lossless synthetic HEIF files ([`crate::testdata`]): every expected sample is known. - -use crate::testdata::{Prop, Spec, build}; -use crate::{Error, Options, decode, decode_thumbnail, probe}; - -/// A luma value unique enough to identify its position; chroma neutral. -fn luma(x: u32, y: u32) -> u16 { - ((x * 7 + y * 13) % 220 + 20) as u16 -} - -fn grey(x: u32, y: u32) -> [u16; 3] { - [luma(x, y), 128, 128] -} - -const FULL_601: Prop = Prop::Nclx(1, 13, 6, true); - -/// Decoded red channel at `(x, y)`, back in 8-bit code values. -fn at(d: &crate::Decoded, x: u32, y: u32) -> u16 { - let ch = if d.has_alpha { 4 } else { 3 }; - d.data[((y * d.width + x) as usize) * ch] / 257 -} - -fn decoded(spec: &Spec) -> crate::Decoded { - decode(&build(spec), &Options::default()).unwrap() -} - -#[test] -fn a_grid_reassembles_exactly() { - // 4 × 3 tiles of 32, cropped by the grid to 100 × 70. - let d = decoded(&Spec { props: vec![FULL_601], ..Spec::new(100, 70, &grey) }); - assert_eq!((d.width, d.height, d.bit_depth, d.has_alpha), (100, 70, 8, false)); - for y in 0..70 { - for x in 0..100 { - let px = &d.data[((y * 100 + x) * 3) as usize..][..3]; - assert_eq!(px, [luma(x, y) * 257; 3], "({x}, {y})"); - } - } - let info = probe(&build(&Spec::new(100, 70, &grey))).unwrap(); - assert_eq!((info.width, info.height, info.coded_width, info.coded_height, info.bit_depth), (100, 70, 100, 70, 8)); -} - -#[test] -fn colour_follows_the_matrix_and_range() { - // BT.601 full range, a saturated orange: within one code value of the exact conversion. - let (y, cb, cr) = (150.0f32, 80.0, 190.0); - let s = |_: u32, _: u32| [y as u16, cb as u16, cr as u16]; - let d = decoded(&Spec { props: vec![FULL_601], ..Spec::new(32, 32, &s) }); - let (u, v) = (cb - 128.0, cr - 128.0); - let expected = [y + 1.402 * v, y - 0.344_136 * u - 0.714_136 * v, y + 1.772 * u].map(|c| c.clamp(0.0, 255.0)); - for (got, want) in d.data[..3].iter().zip(expected) { - assert!((f32::from(*got) / 257.0 - want).abs() <= 1.0, "{:?} vs {expected:?}", &d.data[..3]); - } - assert_eq!(d.colour, crate::Colour { primaries: 1, transfer: 13, matrix: 6, full_range: true }); - - // No nclx and no VUI: the H.265 defaults, limited range (16 → black, 235 → white). - let ends = |x: u32, _: u32| [if x < 16 { 16 } else { 235 }, 128, 128]; - let d = decoded(&Spec::new(32, 32, &ends)); - assert!(!d.colour.full_range); - assert_eq!((d.data[0], d.data[20 * 3]), (0, 65535)); -} - -#[test] -fn ten_bit_samples_keep_their_precision() { - let s = |x: u32, y: u32| [(x * 31 + y) as u16 % 1024, 512, 512]; - let d = decoded(&Spec { bit_depth: 10, props: vec![FULL_601], ..Spec::new(64, 32, &s) }); - assert_eq!(d.bit_depth, 10); - for x in [0, 1, 5, 33, 63] { - let want = ((x * 31) % 1024) as f64 / 1023.0 * 65535.0; - assert!((f64::from(d.data[(x * 3) as usize]) - want).abs() <= 1.0, "x {x}"); - } -} - -#[test] -fn container_transforms_are_applied_like_libheif() { - let (w, h) = (70u32, 40u32); - let spec = |props: Vec| Spec { props: [vec![FULL_601], props].concat(), ..Spec::new(w, h, &grey) }; - // irot: counter-clockwise quarter turns. - let d = decoded(&spec(vec![Prop::Irot(1)])); - assert_eq!((d.width, d.height), (h, w)); - assert_eq!(at(&d, 0, 0), luma(w - 1, 0)); // the right column becomes the top row - assert_eq!(at(&d, h - 1, w - 1), luma(0, h - 1)); - let d = decoded(&spec(vec![Prop::Irot(2)])); - assert_eq!(at(&d, 0, 0), luma(w - 1, h - 1)); - let d = decoded(&spec(vec![Prop::Irot(3)])); - assert_eq!(at(&d, 0, 0), luma(0, h - 1)); - // imir: axis 0 exchanges top and bottom, axis 1 left and right (libheif's reading). - let d = decoded(&spec(vec![Prop::Imir(0)])); - assert_eq!((at(&d, 0, 0), at(&d, 5, 0)), (luma(0, h - 1), luma(5, h - 1))); - let d = decoded(&spec(vec![Prop::Imir(1)])); - assert_eq!((at(&d, 0, 0), at(&d, 0, 5)), (luma(w - 1, 0), luma(w - 1, 5))); - // clap: centred crop moved by its offsets; a half-pixel corner goes left and down. - let d = decoded(&spec(vec![Prop::Clap(31, 21, 3, -2)])); - assert_eq!((d.width, d.height), (31, 21)); - // left = (70 - 31 + 6) / 2 = 22.5 → 22, top = (40 - 21 - 4) / 2 = 7.5 → 8 - assert_eq!(at(&d, 0, 0), luma(22, 8)); - // In association order: crop, then turn, then mirror. - let d = decoded(&spec(vec![Prop::Clap(30, 20, 0, 0), Prop::Irot(1), Prop::Imir(1)])); - assert_eq!((d.width, d.height), (20, 30)); - // The crop is (20, 10) to (49, 29); the turn brings its bottom-right corner to the top-right, - // the left-right mirror to the top-left. - assert_eq!(at(&d, 0, 0), luma(49, 29)); - let info = probe(&build(&spec(vec![Prop::Irot(1)]))).unwrap(); - assert_eq!((info.width, info.height, info.coded_width, info.coded_height), (h, w, w, h)); - // Transforms off: as coded. - let d = decode(&build(&spec(vec![Prop::Irot(1)])), &Options { apply_transforms: false, ..Default::default() }).unwrap(); - assert_eq!((d.width, d.height, at(&d, 3, 2)), (w, h, luma(3, 2))); -} - -#[test] -fn metadata_aux_images_and_thumbnails() { - let icc = b"not parsed here: carried verbatim".to_vec(); - let exif = b"MM\0\x2a\0\0\0\x08\0\0".to_vec(); - let spec = Spec { - props: vec![Prop::Icc(icc.clone()), FULL_601], - exif: Some(exif.clone()), - thumbnail: Some(&|_, _| [77, 128, 128]), - ignored_aux: vec!["urn:com:apple:photo:2020:aux:hdrgainmap", "urn:mpeg:hevc:2015:auxid:2"], - ..Spec::new(64, 64, &grey) - }; - let bytes = build(&spec); - let d = decode(&bytes, &Options::default()).unwrap(); - assert_eq!((d.width, d.height, d.has_alpha), (64, 64, false)); - assert_eq!(d.icc.as_deref(), Some(icc.as_slice())); - assert_eq!(d.exif.as_deref(), Some(exif.as_slice())); - let t = decode_thumbnail(&bytes, 32, &Options::default()).unwrap().unwrap(); - assert_eq!((t.width, t.height, t.data[0]), (32, 32, 77 * 257)); - assert_eq!(t.exif.as_deref(), Some(exif.as_slice())); - assert_eq!(t.icc.as_deref(), Some(icc.as_slice())); - assert!(decode_thumbnail(&bytes, 33, &Options::default()).unwrap().is_none()); -} - -#[test] -fn alpha_comes_from_its_auxiliary_image() { - let alpha = |x: u32, _: u32| (x * 4) as u16; - let d = decoded(&Spec { props: vec![FULL_601], alpha: Some(&alpha), ..Spec::new(64, 32, &grey) }); - assert!(d.has_alpha); - assert_eq!(d.data.len(), 64 * 32 * 4); - assert_eq!((d.data[3], d.data[10 * 4 + 3]), (0, 40 * 257)); - assert_eq!(d.data[..3], [luma(0, 0) * 257; 3]); -} - -#[test] -fn limits_are_checked_before_decoding() { - let bytes = build(&Spec::new(96, 64, &grey)); - let r = decode(&bytes, &Options { max_pixels: 96 * 64 - 1, ..Default::default() }); - assert!(matches!(r, Err(Error::Limit(_))), "{r:?}"); -} - -/// #1518: a grid's tiles together are held to the pixel budget before any is decoded. A -/// 33 × 33 canvas fits 1089 pixels, but its four 32 × 32 tiles hold 4096: over twice the limit. -#[test] -fn grid_tiles_are_held_to_the_pixel_budget_together() { - let bytes = build(&Spec::new(33, 33, &grey)); - let r = decode(&bytes, &Options { max_pixels: 33 * 33, ..Default::default() }); - assert!(matches!(r, Err(Error::Limit(_))), "{r:?}"); - let d = decode(&bytes, &Options { max_pixels: 2 * 32 * 32, ..Default::default() }).unwrap(); - assert_eq!((d.width, d.height), (33, 33)); -} - -#[test] -fn truncated_and_corrupted_files_are_errors_not_panics() { - let bytes = build(&Spec { props: vec![FULL_601, Prop::Irot(1)], thumbnail: Some(&grey), ..Spec::new(64, 64, &grey) }); - let full = decode(&bytes, &Options::default()).unwrap(); - for cut in (0..bytes.len()).step_by(7) { - let b = &bytes[..cut]; - // Cut inside the photo: an error. Cut in the trailing thumbnail: the whole photo. - if let (Ok(_), Ok(d)) = (probe(b), decode(b, &Options::default())) { - assert_eq!(d.data, full.data, "cut at {cut} of {}", bytes.len()); - } - let _ = decode_thumbnail(b, 1, &Options::default()); - } - // Flip bits all over the file: any outcome but a panic. - let mut state = 0x2545_f491_u32; - for _ in 0..400 { - let mut b = bytes.clone(); - for _ in 0..3 { - state ^= state << 13; - state ^= state >> 17; - state ^= state << 5; - let i = state as usize % b.len(); - b[i] ^= 1 << (state % 8); - } - let _ = probe(&b); - let _ = decode(&b, &Options::default()); - let _ = decode_thumbnail(&b, 1, &Options::default()); - } -} - -/// CodeRabbit #44: heic-rs keeps every associated transform and each one re-allocates the -/// whole picture, so 13 of them are over the [`crate::MAX_TRANSFORMS`] cap and refused. -#[test] -fn too_many_transform_properties_are_refused() { - let bytes = build(&Spec { props: std::iter::repeat_n(Prop::Irot(0), 13).collect(), ..Spec::new(32, 32, &grey) }); - let r = decode(&bytes, &Options::default()); - assert!(matches!(r, Err(Error::Limit(ref m)) if m.contains("transform")), "{r:?}"); - // At the cap they're not: 12 identity rotations decode normally. - let bytes = build(&Spec { props: std::iter::repeat_n(Prop::Irot(0), 12).collect(), ..Spec::new(32, 32, &grey) }); - let d = decode(&bytes, &Options::default()).unwrap(); - assert_eq!((d.width, d.height), (32, 32)); -} diff --git a/crates/heif/src/vui.rs b/crates/heif/src/vui.rs deleted file mode 100644 index 6bc629fa57..0000000000 --- a/crates/heif/src/vui.rs +++ /dev/null @@ -1,459 +0,0 @@ -//! The colour description in an HEVC sequence parameter set's VUI (Rec. ITU-T H.265, 7.3.2.2 and -//! Annex E): the fallback a HEIF reader uses when the item carries no `colr` box of type `nclx`. -//! iPhone photos are the common case: they label their colour with an ICC profile only and say in -//! the VUI that their YCbCr is full range. -//! -//! Only the syntax up to `matrix_coeffs` is read. Anything this parser can't follow returns `None` -//! and the caller falls back to the H.265 defaults; it never fails a decode. - -/// What the VUI's `video_signal_type` says (H.265 E.3.1). Absent fields keep the defaults the -/// standard infers: limited range, every code point 2 ("unspecified"). -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Signal { - pub full_range: bool, - pub primaries: u8, - pub transfer: u8, - pub matrix: u8, -} - -impl Default for Signal { - fn default() -> Self { - Signal { full_range: false, primaries: 2, transfer: 2, matrix: 2 } - } -} - -/// NAL unit type of a sequence parameter set. -const SPS_NUT: u8 = 33; - -/// Reads the signal description from the SPS among `parameter_sets` (NAL units with their -/// two-byte header, as an `hvcC` record stores them). `None`: no SPS, or one this reader can't -/// follow; `Some(Signal::default())`: an SPS without VUI colour fields. -pub fn signal(parameter_sets: &[&[u8]]) -> Option { - let sps = parameter_sets.iter().find(|n| n.first().is_some_and(|b| (b >> 1) & 0x3f == SPS_NUT))?; - parse_sps(&unescape(sps.get(2..)?)) -} - -/// The uncropped picture size (`pic_width/height_in_luma_samples`) of every SPS among -/// `parameter_sets`: what the decoder allocates before it applies the conformance window, so the -/// pixel budget is checked against it (heic-rs reports only the first SPS, cropped). `None`: an -/// SPS this reader can't follow. -pub fn coded_sizes(parameter_sets: &[&[u8]]) -> Option> { - parameter_sets - .iter() - .filter(|n| n.first().is_some_and(|b| (b >> 1) & 0x3f == SPS_NUT)) - .map(|sps| { - let rbsp = unescape(sps.get(2..)?); - let r = &mut Bits { data: &rbsp, pos: 0 }; - sps_size(r).map(|(w, h, _)| (w, h)) - }) - .collect() -} - -/// Reads an SPS up to and including the picture size, leaving `r` after it: width, height and -/// `sps_max_sub_layers_minus1`. -fn sps_size(r: &mut Bits) -> Option<(u32, u32, u32)> { - r.skip(4)?; // sps_video_parameter_set_id - let max_sub_layers_minus1 = r.u(3)?; - if max_sub_layers_minus1 > 6 { - return None; - } - r.skip(1)?; // sps_temporal_id_nesting_flag - profile_tier_level(r, max_sub_layers_minus1)?; - r.ue()?; // sps_seq_parameter_set_id - if r.ue()? == 3 { - r.skip(1)?; // separate_colour_plane_flag - } - let width = r.ue()?; // pic_width_in_luma_samples - let height = r.ue()?; // pic_height_in_luma_samples - Some((width, height, max_sub_layers_minus1)) -} - -/// Removes emulation prevention bytes (`00 00 03` → `00 00`). -fn unescape(nal: &[u8]) -> Vec { - let mut out = Vec::with_capacity(nal.len()); - let mut zeros = 0usize; - for &b in nal { - if zeros >= 2 && b == 3 { - zeros = 0; - continue; - } - zeros = if b == 0 { zeros + 1 } else { 0 }; - out.push(b); - } - out -} - -struct Bits<'a> { - data: &'a [u8], - pos: usize, -} - -impl Bits<'_> { - fn u(&mut self, n: u32) -> Option { - let mut v = 0u32; - for _ in 0..n { - let byte = *self.data.get(self.pos / 8)?; - v = (v << 1) | u32::from((byte >> (7 - self.pos % 8)) & 1); - self.pos += 1; - } - Some(v) - } - - fn flag(&mut self) -> Option { - self.u(1).map(|b| b == 1) - } - - fn skip(&mut self, n: usize) -> Option<()> { - self.pos = self.pos.checked_add(n)?; - (self.pos <= self.data.len().saturating_mul(8)).then_some(()) - } - - /// `ue(v)`; values past 32 bits are refused (no valid field here needs them). - fn ue(&mut self) -> Option { - let mut zeros = 0u32; - while !self.flag()? { - zeros += 1; - if zeros > 31 { - return None; - } - } - let rest = self.u(zeros)?; - ((1u64 << zeros) - 1 + u64::from(rest)).try_into().ok() - } - - fn se(&mut self) -> Option<()> { - self.ue().map(|_| ()) - } -} - -/// `profile_tier_level(1, max_sub_layers_minus1)` (7.3.3): only skipped. -fn profile_tier_level(r: &mut Bits, max_sub_layers_minus1: u32) -> Option<()> { - r.skip(88)?; // general profile space .. general_inbld/reserved flag - r.skip(8)?; // general_level_idc - let mut present = [(false, false); 8]; - for p in present.iter_mut().take(max_sub_layers_minus1 as usize) { - *p = (r.flag()?, r.flag()?); - } - if max_sub_layers_minus1 > 0 { - r.skip(2 * (8 - max_sub_layers_minus1 as usize))?; // reserved_zero_2bits - } - for &(profile, level) in present.iter().take(max_sub_layers_minus1 as usize) { - if profile { - r.skip(88)?; - } - if level { - r.skip(8)?; - } - } - Some(()) -} - -/// `scaling_list_data()` (7.3.4): only skipped. -fn scaling_list_data(r: &mut Bits) -> Option<()> { - for size_id in 0..4u32 { - let step = if size_id == 3 { 3 } else { 1 }; - for _ in (0..6).step_by(step) { - if !r.flag()? { - r.ue()?; // scaling_list_pred_matrix_id_delta - } else { - let coefs = 64.min(1 << (4 + (size_id << 1))); - if size_id > 1 { - r.se()?; // scaling_list_dc_coef_minus8 - } - for _ in 0..coefs { - r.se()?; // scaling_list_delta_coef - } - } - } - } - Some(()) -} - -/// `st_ref_pic_set(idx)` (7.3.7) as it appears in an SPS; returns `NumDeltaPocs[idx]`. -fn st_ref_pic_set(r: &mut Bits, idx: usize, num_delta_pocs: &[u32]) -> Option { - if idx != 0 && r.flag()? { - // inter_ref_pic_set_prediction_flag: predicted from the previous set (in an SPS, - // delta_idx_minus1 is absent and RefRpsIdx = idx - 1). - r.skip(1)?; // delta_rps_sign - r.ue()?; // abs_delta_rps_minus1 - let reference = *num_delta_pocs.get(idx - 1)?; - let mut count = 0u32; - for _ in 0..=reference { - let used = r.flag()?; - let kept = used || r.flag()?; // use_delta_flag when not used_by_curr_pic_flag - count += u32::from(kept); - } - return Some(count); - } - let negative = r.ue()?; - let positive = r.ue()?; - let total = negative.checked_add(positive)?; - if total > 32 { - return None; // at most 16 + 16 for any level - } - for _ in 0..total { - r.ue()?; // delta_poc_s*_minus1 - r.skip(1)?; // used_by_curr_pic_s*_flag - } - Some(total) -} - -fn parse_sps(rbsp: &[u8]) -> Option { - let r = &mut Bits { data: rbsp, pos: 0 }; - let (_, _, max_sub_layers_minus1) = sps_size(r)?; - parse_sps_parameters(r, max_sub_layers_minus1)?; - parse_vui(r) -} - -fn parse_sps_parameters(r: &mut Bits<'_>, max_sub_layers_minus1: u32) -> Option<()> { - if r.flag()? { - for _ in 0..4 { - r.ue()?; // conf_win_*_offset - } - } - r.ue()?; // bit_depth_luma_minus8 - r.ue()?; // bit_depth_chroma_minus8 - let log2_max_poc_lsb = r.ue()?.checked_add(4)?; - if log2_max_poc_lsb > 16 { - return None; - } - let first = if r.flag()? { 0 } else { max_sub_layers_minus1 }; - for _ in first..=max_sub_layers_minus1 { - r.ue()?; // sps_max_dec_pic_buffering_minus1 - r.ue()?; // sps_max_num_reorder_pics - r.ue()?; // sps_max_latency_increase_plus1 - } - for _ in 0..6 { - r.ue()?; // coding/transform block sizes, transform hierarchy depths - } - if r.flag()? && r.flag()? { - // scaling_list_enabled_flag, sps_scaling_list_data_present_flag - scaling_list_data(r)?; - } - r.skip(2)?; // amp_enabled_flag, sample_adaptive_offset_enabled_flag - if r.flag()? { - // pcm_enabled_flag - r.skip(8)?; // pcm sample bit depths - r.ue()?; - r.ue()?; - r.skip(1)?; // pcm_loop_filter_disabled_flag - } - let sets = r.ue()? as usize; - if sets > 64 { - return None; - } - let mut num_delta_pocs = Vec::with_capacity(sets); - for idx in 0..sets { - let n = st_ref_pic_set(r, idx, &num_delta_pocs)?; - num_delta_pocs.push(n); - } - if r.flag()? { - // long_term_ref_pics_present_flag - let n = r.ue()?; - if n > 32 { - return None; - } - for _ in 0..n { - r.skip(log2_max_poc_lsb as usize + 1)?; // lt_ref_pic_poc_lsb_sps, used_by_curr_pic_lt_sps_flag - } - } - r.skip(2)?; // sps_temporal_mvp_enabled_flag, strong_intra_smoothing_enabled_flag - Some(()) -} - -fn parse_vui(r: &mut Bits<'_>) -> Option { - let mut signal = Signal::default(); - if !r.flag()? { - return Some(signal); // no VUI - } - if r.flag()? && r.u(8)? == 255 { - // aspect_ratio_info_present_flag, aspect_ratio_idc == EXTENDED_SAR - r.skip(32)?; - } - if r.flag()? { - r.skip(1)?; // overscan_appropriate_flag - } - if r.flag()? { - // video_signal_type_present_flag - r.skip(3)?; // video_format - signal.full_range = r.flag()?; - if r.flag()? { - signal.primaries = r.u(8)? as u8; - signal.transfer = r.u(8)? as u8; - signal.matrix = r.u(8)? as u8; - } - } - Some(signal) -} - -#[cfg(test)] -mod tests { - use super::*; - - /// A bit writer for building parameter sets in tests. - #[derive(Default)] - pub(crate) struct Writer { - pub bits: Vec, - } - - impl Writer { - pub fn u(&mut self, v: u32, n: u32) { - for i in (0..n).rev() { - self.bits.push(i < 32 && (v >> i) & 1 == 1); - } - } - pub fn ue(&mut self, v: u32) { - let x = u64::from(v) + 1; - let len = 64 - x.leading_zeros(); - self.u(0, len - 1); - for i in (0..len).rev() { - self.bits.push((x >> i) & 1 == 1); - } - } - pub fn bytes(mut self) -> Vec { - self.bits.push(true); // rbsp_stop_one_bit - while !self.bits.len().is_multiple_of(8) { - self.bits.push(false); - } - self.bits.chunks(8).map(|c| c.iter().fold(0u8, |a, &b| (a << 1) | u8::from(b))).collect() - } - } - - fn sps(vui: impl FnOnce(&mut Writer), st_sets: impl FnOnce(&mut Writer)) -> Vec { - let mut w = Writer::default(); - w.u(0, 4); - w.u(1, 3); // one sub-layer above the base - w.u(1, 1); - w.u(0, 88); - w.u(93, 8); - w.u(0b11, 2); // sub-layer profile and level present - w.u(0, 2 * 7); - w.u(0, 88); - w.u(0, 8); - w.ue(0); - w.ue(1); // 4:2:0 - w.ue(4032); - w.ue(3024); - w.u(1, 1); // conformance window - for _ in 0..4 { - w.ue(3); - } - w.ue(0); - w.ue(0); - w.ue(4); - w.u(1, 1); - for _ in 0..2 { - w.ue(1); - w.ue(0); - w.ue(0); - } - for v in [0, 3, 0, 3, 0, 0] { - w.ue(v); - } - w.u(1, 1); // scaling lists, with data - w.u(1, 1); - for size_id in 0..4u32 { - for _ in (0..6).step_by(if size_id == 3 { 3 } else { 1 }) { - w.u(1, 1); - if size_id > 1 { - w.ue(0); - } - for _ in 0..64.min(1 << (4 + (size_id << 1))) { - w.ue(0); - } - } - } - w.u(0, 2); - w.u(1, 1); // pcm - w.u(0x77, 8); - w.ue(0); - w.ue(1); - w.u(1, 1); - st_sets(&mut w); - w.u(1, 1); // long-term pictures - w.ue(1); - w.u(0, 8 + 1); - w.u(0, 2); - vui(&mut w); - let mut nal = vec![SPS_NUT << 1, 1]; - // escape like a real NAL so `signal` has to unescape - let mut zeros = 0; - for b in w.bytes() { - if zeros >= 2 && b <= 3 { - nal.push(3); - zeros = 0; - } - zeros = if b == 0 { zeros + 1 } else { 0 }; - nal.push(b); - } - nal - } - - fn two_rps(w: &mut Writer) { - w.ue(2); - w.ue(1); // set 0: one negative picture - w.ue(0); - w.ue(0); - w.u(1, 1); - w.u(1, 1); // set 1: predicted from set 0 - w.u(0, 1); - w.ue(0); - w.u(0, 1); // not used ... - w.u(1, 1); // ... but kept - w.u(1, 1); - } - - #[test] - fn reads_full_range_and_matrix_from_the_vui() { - let nal = sps( - |w| { - w.u(1, 1); // vui present - w.u(1, 1); - w.u(255, 8); - w.u(0x0001_0001, 32); - w.u(1, 1); - w.u(0, 1); - w.u(1, 1); // video_signal_type_present_flag - w.u(5, 3); - w.u(1, 1); // full range - w.u(1, 1); - w.u(12, 8); - w.u(13, 8); - w.u(6, 8); - }, - two_rps, - ); - let s = signal(&[&[0x40, 1, 0xc], &nal, &[0x44, 1]]); - assert_eq!(s, Some(Signal { full_range: true, primaries: 12, transfer: 13, matrix: 6 })); - } - - #[test] - fn absent_fields_take_the_standard_defaults() { - let no_vui = sps(|w| w.u(0, 1), |w| w.ue(0)); - assert_eq!(signal(&[&no_vui]), Some(Signal::default())); - let range_only = sps( - |w| { - w.u(1, 1); - w.u(0, 2); - w.u(1, 1); - w.u(5, 3); - w.u(1, 1); - w.u(0, 1); // no colour description - }, - |w| w.ue(0), - ); - assert_eq!(signal(&[&range_only]), Some(Signal { full_range: true, ..Signal::default() })); - } - - #[test] - fn missing_or_truncated_sps_is_none() { - assert_eq!(signal(&[]), None); - assert_eq!(signal(&[&[0x40, 1, 2, 3]]), None); - let nal = sps(|w| w.u(0, 1), |w| w.ue(0)); - for cut in 2..nal.len() - 2 { - // never panics; most cuts are None - let _ = signal(&[nal.get(..cut).unwrap_or_default()]); - } - assert_eq!(signal(&[&[SPS_NUT << 1, 1, 0xff, 0xff, 0xff]]), None); - } -} diff --git a/crates/heif/src/ycc.rs b/crates/heif/src/ycc.rs deleted file mode 100644 index 384d25f2ae..0000000000 --- a/crates/heif/src/ycc.rs +++ /dev/null @@ -1,352 +0,0 @@ -//! YCbCr → RGB for decoded HEVC pictures, done here rather than in heic-rs so the result matches -//! libheif (the reference every HEIF tool is checked against) to within one code value: -//! -//! - **Which matrix and range.** The item's `colr` `nclx` box when there is one; otherwise the -//! HEVC VUI ([`crate::vui`]); otherwise the H.265 defaults (limited range). An unspecified matrix -//! is BT.601, as libheif reads it. heic-rs 0.1.1 ignores the VUI and assumes BT.709 limited -//! range for every file without `nclx`, which shifts every iPhone photo (full range, ICC only) -//! by about 8 code values. -//! - **Chroma upsampling.** Nearest neighbour (each chroma sample covers its 2×2 or 2×1 luma -//! block), as libheif does by default. libheif switches to a centred bilinear filter (weights -//! 9/3/3/1 sixteenths, in integers) when a `clap` crop starts at an odd column or row; so does -//! [`Upsampling::Bilinear`]. -//! -//! The arithmetic is floating point and the result 16-bit, whatever the coded depth, so the -//! develop pipeline gets every bit the conversion produces. - -use heic_rs::grid::Mosaic; -use heic_rs::hevc::{ChromaFormat, Frame}; - -/// The coded pictures one image is made of. -pub enum Planes<'a> { - Picture(&'a Frame), - Grid(Mosaic<'a>), -} - -impl Planes<'_> { - pub fn size(&self) -> (usize, usize) { - match self { - Planes::Picture(f) => (f.width as usize, f.height as usize), - Planes::Grid(m) => (m.width() as usize, m.height() as usize), - } - } - - pub fn bit_depth(&self) -> u8 { - match self { - Planes::Picture(f) => f.bit_depth, - Planes::Grid(m) => m.bit_depth(), - } - } - - pub fn chroma(&self) -> ChromaFormat { - match self { - Planes::Picture(f) => f.chroma, - Planes::Grid(m) => m.chroma(), - } - } - - fn chroma_width(&self) -> usize { - let (w, h) = self.size(); - self.chroma().chroma_size(w as u32, h as u32).0 as usize - } - - fn luma_row(&self, y: usize, dst: &mut [u16]) -> Option<()> { - match self { - Planes::Picture(f) => copy_row(&f.y, f.y_stride as usize, y, dst), - Planes::Grid(m) => m.luma_row(y, dst), - } - } - - fn chroma_row(&self, cr: bool, r: usize, dst: &mut [u16]) -> Option<()> { - match self { - Planes::Picture(f) => copy_row(if cr { &f.cr } else { &f.cb }, f.c_stride as usize, r, dst), - Planes::Grid(m) => m.chroma_row(cr, r, dst), - } - } -} - -fn copy_row(plane: &[u16], stride: usize, y: usize, dst: &mut [u16]) -> Option<()> { - let start = y.checked_mul(stride)?; - dst.copy_from_slice(plane.get(start..start.checked_add(dst.len())?)?); - Some(()) -} - -/// How the three coded channels map to R, G, B (H.273 `MatrixCoefficients`). -#[derive(Debug, Clone, Copy, PartialEq)] -enum Matrix { - /// 0: the channels are G, B, R. - Identity, - /// 8: YCgCo. - YCgCo, - /// Every Kr/Kb matrix: R = Y + `cr_r`·Cr, G = Y − `cb_g`·Cb − `cr_g`·Cr, B = Y + `cb_b`·Cb. - Kr { cr_r: f32, cb_g: f32, cr_g: f32, cb_b: f32 }, -} - -impl Matrix { - /// Unknown, reserved and unspecified codes are BT.601, like libheif. - fn from_code(code: u16) -> Matrix { - let (kr, kb) = match code { - 0 => return Matrix::Identity, - 8 => return Matrix::YCgCo, - 1 => (0.2126, 0.0722), - 4 => (0.30, 0.11), - 7 => (0.212, 0.087), - // 10 (BT.2020 constant luminance) is approximated by its non-constant matrix. - 9 | 10 => (0.2627, 0.0593), - _ => (0.299, 0.114), - }; - let kg = 1.0 - kr - kb; - Matrix::Kr { cr_r: 2.0 * (1.0 - kr), cb_g: 2.0 * kb * (1.0 - kb) / kg, cr_g: 2.0 * kr * (1.0 - kr) / kg, cb_b: 2.0 * (1.0 - kb) } - } -} - -/// How the samples are to be read: the matrix and the range. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Signal { - pub matrix: u16, - pub full_range: bool, -} - -/// How subsampled chroma is brought to the luma resolution. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Upsampling { - Nearest, - /// Centred bilinear: chroma sits between its two luma columns (and rows for 4:2:0). - Bilinear, -} - -/// One chroma row brought to the full width `w` of luma row `y`. -fn chroma_at(planes: &Planes<'_>, cr: bool, y: usize, w: usize, mode: Upsampling, buf: &mut [u16], next: &mut [u16]) -> Option> { - let chroma = planes.chroma(); - let (xs, ys) = (chroma.x_shift(), chroma.y_shift()); - let (wf, hf) = planes.size(); - let rows = chroma.chroma_size(wf as u32, hf as u32).1 as usize; - let cy = y >> ys; - planes.chroma_row(cr, cy, buf)?; - let last = buf.len().checked_sub(1)?; - if mode == Upsampling::Nearest || xs == 0 { - return (0..w).map(|x| buf.get(x >> xs).copied()).collect(); - } - // The nearer neighbour on each axis takes a quarter of the weight. - let ny = if ys == 0 { - cy - } else if y.is_multiple_of(2) { - cy.saturating_sub(1) - } else { - (cy + 1).min(rows.checked_sub(1)?) - }; - planes.chroma_row(cr, ny, next)?; - (0..w) - .map(|x| { - let cx = x >> 1; - let nx = if x % 2 == 0 { cx.saturating_sub(1) } else { (cx + 1).min(last) }; - let (a, an) = (u32::from(*buf.get(cx)?), u32::from(*buf.get(nx)?)); - let v = if ys == 0 { - (3 * a + an + 2) >> 2 - } else { - let (b, bn) = (u32::from(*next.get(cx)?), u32::from(*next.get(nx)?)); - (9 * a + 3 * b + 3 * an + bn + 8) >> 4 - }; - u16::try_from(v).ok() - }) - .collect() -} - -/// Converts `planes` to interleaved 16-bit RGB, or RGBA with room left for alpha (filled with -/// 65535 here). `None` when the planes can't supply a row they promised (a malformed grid). -pub fn to_rgb16(planes: &Planes<'_>, signal: Signal, alpha: bool, mode: Upsampling) -> Option> { - let (w, h) = planes.size(); - let ch = if alpha { 4 } else { 3 }; - let bd = u32::from(planes.bit_depth()); - if !(8..=16).contains(&bd) || w == 0 || h == 0 { - return None; - } - // The default caller limit is 2²8 pixels, at most four channels per pixel, so the largest - // legitimate `w·h·ch` is 2²30 samples. Anything more is a malformed or hostile canvas the - // decode path would have refused before calling here (CodeRabbit #48): refuse rather than - // ask for a multi-gigabuffer (or worse, over usize). - if w.checked_mul(h)?.checked_mul(ch)? > 1 << 30 { - return None; - } - let max = ((1u32 << bd) - 1) as f32; - let half = (1u32 << (bd - 1)) as f32; - let scale = (1u32 << (bd - 8)) as f32; - // Normalised Y = (v − y_off)·y_mul, Cb/Cr = (v − half)·c_mul. - let (y_off, y_mul, c_mul) = - if signal.full_range { (0.0, 1.0 / max, 1.0 / max) } else { (16.0 * scale, 1.0 / (219.0 * scale), 1.0 / (224.0 * scale)) }; - let matrix = Matrix::from_code(signal.matrix); - let mono = planes.chroma() == ChromaFormat::Monochrome; - let cw = planes.chroma_width(); - - let mut out = vec![0u16; w.checked_mul(h)?.checked_mul(ch)?]; - let row = |y: usize, dst: &mut [u16]| -> Option<()> { - let mut luma = vec![0u16; w]; - planes.luma_row(y, &mut luma)?; - let (cb, cr) = if mono { - (Vec::new(), Vec::new()) - } else { - let (mut buf, mut next) = (vec![0u16; cw], vec![0u16; cw]); - (chroma_at(planes, false, y, w, mode, &mut buf, &mut next)?, chroma_at(planes, true, y, w, mode, &mut buf, &mut next)?) - }; - for (x, (px, &yv)) in dst.chunks_exact_mut(ch).zip(&luma).enumerate() { - let rgb = if mono { - let v = (f32::from(yv) - y_off) * y_mul; - [v, v, v] - } else { - let (u, v) = (f32::from(*cb.get(x)?), f32::from(*cr.get(x)?)); - match matrix { - // G, B, R all read like luma. - Matrix::Identity => [(v - y_off) * y_mul, (f32::from(yv) - y_off) * y_mul, (u - y_off) * y_mul], - Matrix::YCgCo => { - let (yn, cg, co) = ((f32::from(yv) - y_off) * y_mul, (u - half) * c_mul, (v - half) * c_mul); - let t = yn - cg; - [t + co, yn + cg, t - co] - } - Matrix::Kr { cr_r, cb_g, cr_g, cb_b } => { - let (yn, u, v) = ((f32::from(yv) - y_off) * y_mul, (u - half) * c_mul, (v - half) * c_mul); - [yn + cr_r * v, yn - cb_g * u - cr_g * v, yn + cb_b * u] - } - } - }; - for (o, c) in px.iter_mut().zip(rgb) { - *o = (c.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16; - } - if alpha && let Some(a) = px.get_mut(3) { - *a = u16::MAX; - } - } - Some(()) - }; - let row_len = w * ch; - #[cfg(not(target_arch = "wasm32"))] - let ok = { - use rayon::prelude::*; - out.par_chunks_mut(row_len).enumerate().all(|(y, dst)| row(y, dst).is_some()) - }; - #[cfg(target_arch = "wasm32")] - let ok = out.chunks_mut(row_len).enumerate().all(|(y, dst)| row(y, dst).is_some()); - ok.then_some(out) -} - -/// Writes an alpha picture's luma, full range, into channel 3 of `rgba`. -pub fn put_alpha(planes: &Planes<'_>, rgba: &mut [u16]) -> Option<()> { - let (w, h) = planes.size(); - let max = ((1u32 << u32::from(planes.bit_depth()).clamp(1, 16)) - 1) as f32; - let mut luma = vec![0u16; w]; - for (y, row) in rgba.chunks_exact_mut(w * 4).enumerate().take(h) { - planes.luma_row(y, &mut luma)?; - for (px, &a) in row.as_chunks_mut::<4>().0.iter_mut().zip(&luma) { - let v = (f32::from(a) / max).clamp(0.0, 1.0); - px[3] = (v * 65535.0 + 0.5) as u16; - } - } - Some(()) -} - -#[cfg(test)] -mod tests { - use super::*; - - fn frame(w: u32, h: u32, bd: u8, chroma: ChromaFormat, y: u16, cb: u16, cr: u16) -> Frame { - let (cw, chh) = chroma.chroma_size(w, h); - let n = (cw * chh) as usize; - Frame { - width: w, - height: h, - bit_depth: bd, - chroma, - y: vec![y; (w * h) as usize], - cb: vec![cb; n], - cr: vec![cr; n], - y_stride: w, - c_stride: cw, - } - } - - fn rgb(f: &Frame, matrix: u16, full_range: bool) -> [u16; 3] { - let v = to_rgb16(&Planes::Picture(f), Signal { matrix, full_range }, false, Upsampling::Nearest).unwrap(); - [v[0], v[1], v[2]] - } - - #[test] - fn neutral_and_primaries_convert_exactly() { - // Full-range grey stays grey; limited-range black and white reach the ends. - assert_eq!(rgb(&frame(4, 4, 8, ChromaFormat::Yuv420, 128, 128, 128), 6, true), [128 * 257; 3]); - assert_eq!(rgb(&frame(4, 4, 8, ChromaFormat::Yuv420, 16, 128, 128), 1, false), [0; 3]); - assert_eq!(rgb(&frame(4, 4, 8, ChromaFormat::Yuv420, 235, 128, 128), 1, false), [65535; 3]); - assert_eq!(rgb(&frame(4, 4, 10, ChromaFormat::Yuv420, 940, 512, 512), 9, false), [65535; 3]); - // BT.601 full-range pure red: Y = 76.245, Cb = 84.97, Cr = 255.5 → (255, 0, 0) within rounding. - let red = rgb(&frame(2, 2, 8, ChromaFormat::Yuv444, 76, 85, 255), 6, true); - assert!(red[0] > 65000 && red[1] < 300 && red[2] < 300, "{red:?}"); - // Identity: the channels are G, B, R. - assert_eq!(rgb(&frame(2, 2, 8, ChromaFormat::Yuv444, 10, 20, 30), 0, true), [30 * 257, 10 * 257, 20 * 257]); - // Monochrome. - assert_eq!(rgb(&frame(2, 2, 8, ChromaFormat::Monochrome, 51, 0, 0), 6, true), [51 * 257; 3]); - } - - #[test] - fn chroma_is_nearest_neighbour() { - // 4:2:0, 4×2 luma, chroma 2×1: the left chroma sample covers columns 0–1 only. - let mut f = frame(4, 2, 8, ChromaFormat::Yuv420, 128, 128, 128); - f.cr = vec![200, 128]; - let v = to_rgb16(&Planes::Picture(&f), Signal { matrix: 6, full_range: true }, false, Upsampling::Nearest).unwrap(); - let red: Vec = v.chunks(3).map(|p| p[0]).collect(); - assert!(red[0] == red[1] && red[1] == red[4] && red[0] > red[2] && red[2] == 128 * 257, "{red:?}"); - } - - #[test] - fn bilinear_chroma_is_centred_9_3_3_1() { - // 4:2:0, 4×4 luma, chroma 2×2 with one bright corner sample. - let mut f = frame(4, 4, 8, ChromaFormat::Yuv420, 128, 128, 128); - f.cr = vec![200, 128, 128, 128]; - let p = Planes::Picture(&f); - let (mut buf, mut next) = (vec![0; 2], vec![0; 2]); - let rows: Vec> = (0..4).map(|y| chroma_at(&p, true, y, 4, Upsampling::Bilinear, &mut buf, &mut next).unwrap()).collect(); - // Row 0 sits nearest chroma row 0 (and clamps upward); row 1 blends a quarter of row 1. - assert_eq!(rows[0], [200, 182, 146, 128]); - assert_eq!(rows[1], [182, 169, 142, 128]); - assert_eq!(rows[2], [146, 142, 133, 128]); - assert_eq!(rows[3], [128; 4]); - assert_eq!(chroma_at(&p, true, 1, 4, Upsampling::Nearest, &mut buf, &mut next).unwrap(), [200, 200, 128, 128]); - } - - #[test] - fn alpha_and_bad_planes() { - let f = frame(2, 2, 8, ChromaFormat::Yuv420, 128, 128, 128); - let mut v = to_rgb16(&Planes::Picture(&f), Signal { matrix: 6, full_range: true }, true, Upsampling::Nearest).unwrap(); - assert_eq!(v[3], 65535); - put_alpha(&Planes::Picture(&frame(2, 2, 8, ChromaFormat::Monochrome, 51, 0, 0)), &mut v).unwrap(); - assert_eq!(v[3], 51 * 257); - let mut short = frame(4, 4, 8, ChromaFormat::Yuv420, 128, 128, 128); - short.y.truncate(5); - assert!(to_rgb16(&Planes::Picture(&short), Signal { matrix: 6, full_range: true }, false, Upsampling::Bilinear).is_none()); - assert!( - to_rgb16( - &Planes::Picture(&frame(2, 2, 7, ChromaFormat::Yuv420, 0, 0, 0)), - Signal { matrix: 6, full_range: true }, - false, - Upsampling::Nearest - ) - .is_none() - ); - } - - /// CodeRabbit #48: a plane claiming 500 000 × 500 000 pixels must be refused without ever - /// allocating for it (before the cap this vec was never built; upper-bounding allocation - /// would have been a 7.5-gibabuffer or a panic instead of an error). - #[test] - fn an_oversized_canvas_is_refused_without_allocation() { - let f = Frame { - width: 500_000, - height: 500_000, - bit_depth: 8, - chroma: ChromaFormat::Yuv420, - y: vec![128], - cb: vec![128; 4], - cr: vec![128; 4], - y_stride: 0, - c_stride: 0, - }; - assert!(to_rgb16(&Planes::Picture(&f), Signal { matrix: 6, full_range: true }, false, Upsampling::Nearest).is_none()); - } -} diff --git a/crates/inpaint/src/membrane.rs b/crates/inpaint/src/membrane.rs index 1f259b02fe..9fe3bf16fd 100644 --- a/crates/inpaint/src/membrane.rs +++ b/crates/inpaint/src/membrane.rs @@ -169,6 +169,10 @@ pub fn membrane_fill(w: usize, h: usize, ch: usize, img: &[f32], hole: &[bool]) if Some(hole.len()) != w.checked_mul(h) || Some(img.len()) != hole.len().checked_mul(ch) { return img.to_vec(); } + // no pixels: nothing to fill (and `ch` would otherwise size one plane per channel, whatever it claims) + if hole.is_empty() { + return img.to_vec(); + } per_channel(w, h, ch, |c| { let mut v: Vec = (0..w * h).map(|i| if hole[i] { 0.0 } else { img[i * ch + c] }).collect(); solve_membrane(w, h, hole, &mut v); @@ -183,6 +187,12 @@ mod tests { (0..w * h).map(|i| ((i % w) as f32 - cx).hypot((i / w) as f32 - cy) < r).collect() } + #[test] + fn a_zero_area_image_comes_back_unchanged_whatever_its_channel_count() { + assert!(membrane_fill(0, 5, usize::MAX, &[], &[]).is_empty()); + assert!(membrane_fill(3, 0, 1 << 40, &[], &[]).is_empty()); + } + #[test] fn membrane_reproduces_linear_boundary() { // A linear function is harmonic: the solve must reproduce it exactly inside the hole. diff --git a/crates/lensdb/src/xml.rs b/crates/lensdb/src/xml.rs index 5eed2dd288..b3a1b520bc 100644 --- a/crates/lensdb/src/xml.rs +++ b/crates/lensdb/src/xml.rs @@ -53,13 +53,21 @@ fn entity(name: &str) -> Option { }) } +/// Index of the `;` that ends a reference starting at `rest` (its `&`). The scan stops at the first +/// byte that cannot appear in a name or a numeric reference, so zero-padded references such as +/// `a` are found and each run of name bytes is read once, not once per `&`. +fn entity_end(rest: &str) -> Option { + let n = rest.bytes().skip(1).take_while(|b| b.is_ascii_alphanumeric() || *b == b'#').count(); + (rest.as_bytes().get(n + 1) == Some(&b';')).then_some(n + 1) +} + fn unescape(s: &str) -> String { let mut out = String::with_capacity(s.len()); let mut rest = s; while let Some(i) = rest.find('&') { out.push_str(rest.get(..i).unwrap_or_default()); let tail = rest.get(i..).unwrap_or_default(); - match tail.find(';').and_then(|j| entity(tail.get(1..j)?).map(|c| (c, j))) { + match entity_end(tail).and_then(|j| entity(tail.get(1..j)?).map(|c| (c, j))) { Some((c, j)) => { out.push(c); rest = tail.get(j + 1..).unwrap_or_default(); @@ -140,6 +148,20 @@ fn close(stack: &mut Vec) { mod tests { use super::*; + #[test] + fn many_ampersands_before_one_semicolon_are_read_once() { + // 200 000 `&` then a `;`: each `&` once searched the whole rest for the `;` + let hostile = format!("{};&", "&".repeat(200_000)); + assert_eq!(unescape(&hostile), format!("{};&", "&".repeat(200_000))); + assert_eq!(unescape("<a> 􏿿 &unknownreference;"), " \u{10FFFF} &unknownreference;"); + } + + #[test] + fn zero_padded_numeric_references_decode() { + assert_eq!(unescape("Canon A A"), "Canon A A"); + assert_eq!(unescape("&abc&#xyz;&;"), "&abc&#xyz;&;"); + } + #[test] fn entity_rejects_signed_numeric_references() { assert_eq!(entity("#+65"), None); diff --git a/crates/merge/src/hdr.rs b/crates/merge/src/hdr.rs index dd1a9ee537..05aac1b082 100644 --- a/crates/merge/src/hdr.rs +++ b/crates/merge/src/hdr.rs @@ -119,6 +119,10 @@ pub fn merge_hdr(mut frames: Vec, opts: &HdrOptions, progress: &Progress) if frames.iter().any(|f| f.width() != w || f.height() != h) { return Err(MergeError::Mismatch("HDR merge needs photos of the same size".into())); } + // every buffer must hold exactly its frame's pixels before anything is sized from the dimensions + if frames.iter().any(|f| w.checked_mul(h) != Some(f.image.data.len())) { + return Err(MergeError::Mismatch("an HDR frame's pixels do not match its size".into())); + } if frames.iter().any(|f| f.orientation != frames[0].orientation) { for f in frames.iter_mut() { f.orient(); diff --git a/crates/meta/Cargo.toml b/crates/meta/Cargo.toml index 4f2fad315e..1b146c99da 100644 --- a/crates/meta/Cargo.toml +++ b/crates/meta/Cargo.toml @@ -9,7 +9,7 @@ description = "LightCraft metadata: EXIF, XMP, IPTC (L1)" workspace = true [dependencies] -lightcraft-tiff = { workspace = true } +craft-tiff = { workspace = true } lightcraft-geom = { workspace = true } serde = { workspace = true } quick-xml = { workspace = true } @@ -17,6 +17,6 @@ chrono = { workspace = true } [dev-dependencies] # Lossless synthetic HEIC files: the HEIF item reader is proved against a real container writer. -lightcraft-heif = { workspace = true, features = ["testdata"] } +craft-heif = { workspace = true, features = ["testdata"] } proptest = { workspace = true } serde_json = { workspace = true } diff --git a/crates/meta/src/container.rs b/crates/meta/src/container.rs index 0136bf6846..5b4242bfff 100644 --- a/crates/meta/src/container.rs +++ b/crates/meta/src/container.rs @@ -14,7 +14,7 @@ //! - HEIF/HEIC/AVIF (ISO base media file, see [`crate::heif`]): the `Exif` item's TIFF stream and the XMP //! `mime` item. -use lightcraft_tiff::{Tiff, tags}; +use craft_tiff::{Tiff, tags}; /// Metadata blocks found in a file. #[derive(Clone, Debug, Default, PartialEq, Eq)] @@ -71,8 +71,8 @@ pub fn embedded(bytes: &[u8]) -> Embedded { xmp_extended: None, icc: ifd0.bytes(tags::ICC_PROFILE).map(|b| b.to_vec()), iptc: ifd0.value(tags::IPTC_NAA).map(|v| match v { - lightcraft_tiff::Value::Byte(b) | lightcraft_tiff::Value::Undefined(b) => b.clone(), - other => lightcraft_tiff::writer::encode(t.order, other), + craft_tiff::Value::Byte(b) | craft_tiff::Value::Undefined(b) => b.clone(), + other => craft_tiff::writer::encode(t.order, other), }), } } else { @@ -275,7 +275,7 @@ pub fn webp_chunks(b: &[u8]) -> Embedded { mod tests { use super::*; use crate::exif::tests::sample_exif; - use lightcraft_tiff::ByteOrder; + use craft_tiff::ByteOrder; fn seg(marker: u8, payload: &[u8]) -> Vec { let mut v = vec![0xff, marker]; diff --git a/crates/meta/src/cr3.rs b/crates/meta/src/cr3.rs index a95b8baada..0a90f733e9 100644 --- a/crates/meta/src/cr3.rs +++ b/crates/meta/src/cr3.rs @@ -436,13 +436,13 @@ fn le32(b: &[u8], at: usize) -> Option { /// Merge `CMT1` (IFD0), `CMT2` (Exif) and `CMT4` (GPS) into one TIFF stream, so the Exif readers see a CR3 /// like any TIFF-based raw. The maker note (`CMT3`) is left out: its offsets are relative to its own block. pub fn merged_exif(c: &Cr3<'_>) -> Option> { - use lightcraft_tiff::{IfdBuilder, Tiff, TiffWriter, tags as t}; + use craft_tiff::{IfdBuilder, Tiff, TiffWriter, tags as t}; let ifd0 = Tiff::parse(c.cmt[0]?).ok()?; let order = ifd0.order; let first = |i: usize| c.cmt[i].and_then(|b| Tiff::parse(b).ok()).and_then(|t| t.ifds.into_iter().next()); // pointers and offsets that would dangle once the IFDs move const SKIP: [u16; 9] = [t::EXIF_IFD, t::GPS_IFD, t::INTEROP_IFD, t::SUB_IFDS, t::MAKER_NOTE, 273, 279, 513, 514]; - let build = |ifd: &lightcraft_tiff::Ifd| { + let build = |ifd: &craft_tiff::Ifd| { let mut b = IfdBuilder::new(); for e in ifd.entries.iter().filter(|e| !SKIP.contains(&e.tag)) { b.set(e.tag, e.value.clone()); @@ -497,14 +497,14 @@ mod tests { let stbl = [full(b"stsd", &stsd), full(b"stsz", &stsz), full(b"co64", &co64)].concat(); bx(b"trak", &bx(b"mdia", &bx(b"minf", &bx(b"stbl", &stbl)))) } - fn tiff_with(tag: u16, v: lightcraft_tiff::Value) -> Vec { - let b = lightcraft_tiff::IfdBuilder::new().with(tag, v); - lightcraft_tiff::TiffWriter::new(lightcraft_tiff::ByteOrder::Little, false).write(&[b]).unwrap() + fn tiff_with(tag: u16, v: craft_tiff::Value) -> Vec { + let b = craft_tiff::IfdBuilder::new().with(tag, v); + craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[b]).unwrap() } /// A small synthetic CR3: Canon uuid with CMT1/CMT2/CMT4, a JPEG track, a raw track, XMP. pub(crate) fn sample() -> Vec { - use lightcraft_tiff::{Value, tags as t}; + use craft_tiff::{Value, tags as t}; let cmt1 = tiff_with(t::MODEL, Value::Ascii("Canon EOS Test".into())); let cmt2 = tiff_with(t::ISO_SPEED, Value::Short(vec![400])); let cmt4 = tiff_with(1, Value::Ascii("N".into())); @@ -689,7 +689,7 @@ mod tests { #[test] fn timed_metadata_exposes_the_dynamic_maker_note() { - use lightcraft_tiff::{Value, tags as t}; + use craft_tiff::{Value, tags as t}; let exif = tiff_with(t::ISO_SPEED, Value::Short(vec![800])); let maker = tiff_with(0x4001, Value::Short(vec![48, 100, 200])); let item = |tag: u32, data: &[u8]| { diff --git a/crates/meta/src/exif.rs b/crates/meta/src/exif.rs index 9894f016ba..7602e115de 100644 --- a/crates/meta/src/exif.rs +++ b/crates/meta/src/exif.rs @@ -1,7 +1,7 @@ //! Exif / TIFF tags → [`Metadata`] (CIPA DC-008 Exif 2.32, TIFF 6.0, DNG 1.7 tag semantics). use crate::{DateTime, Flash, Gps, Metadata, Orientation}; -use lightcraft_tiff::{Ifd, ParseOptions, Tiff, TiffError, tags as t}; +use craft_tiff::{Ifd, ParseOptions, Tiff, TiffError, tags as t}; /// Strip an `Exif\0\0` (or `Exif\0\xff`) prefix as found in JPEG APP1 and some WebP `EXIF` chunks. pub fn strip_exif_header(b: &[u8]) -> &[u8] { @@ -129,8 +129,8 @@ pub fn from_tiff(tiff: &Tiff) -> Metadata { if let Some(iptc) = ifd0.and_then(|i| i.value(t::IPTC_NAA)) { // IPTC-NAA is often typed LONG; re-serialise to bytes in file order. let bytes = match iptc { - lightcraft_tiff::Value::Byte(b) | lightcraft_tiff::Value::Undefined(b) => b.clone(), - other => lightcraft_tiff::writer::encode(tiff.order, other), + craft_tiff::Value::Byte(b) | craft_tiff::Value::Undefined(b) => b.clone(), + other => craft_tiff::writer::encode(tiff.order, other), }; m.fill_missing(&crate::parse_iptc(&bytes)); } @@ -200,7 +200,7 @@ fn panasonic_lens(data: &[u8], tiff: &Tiff) -> Option { } let e = exif.get(t::MAKER_NOTE)?; let make = tiff.ifds.first().and_then(|i| i.string(t::MAKE)).unwrap_or_default(); - let mn = lightcraft_tiff::makernote::parse_makernote(data, e.offset, e.count() as u64, tiff.order, &make)?; + let mn = craft_tiff::makernote::parse_makernote(data, e.offset, e.count() as u64, tiff.order, &make)?; text(Some(&mn.ifd), PANASONIC_LENS_TYPE) } @@ -225,7 +225,7 @@ fn canon_maker_note_lens(data: &[u8], tiff: &Tiff) -> Option { } let exif = tiff.exif()?; let e = exif.get(t::MAKER_NOTE)?; - let mn = lightcraft_tiff::makernote::parse_makernote(data, e.offset, e.count() as u64, tiff.order, &make)?; + let mn = craft_tiff::makernote::parse_makernote(data, e.offset, e.count() as u64, tiff.order, &make)?; text(Some(&mn.ifd), CANON_LENS_MODEL) } @@ -234,13 +234,13 @@ fn canon_maker_note_lens(data: &[u8], tiff: &Tiff) -> Option { const NIKON_LENS: u16 = 0x0084; /// The lens a Nikon camera records in its maker note, when it's plausible. -fn nikon_lens_spec(exif: Option<&Ifd>, make: Option<&str>, order: lightcraft_tiff::ByteOrder) -> Option<[f64; 4]> { +fn nikon_lens_spec(exif: Option<&Ifd>, make: Option<&str>, order: craft_tiff::ByteOrder) -> Option<[f64; 4]> { let make = make?; if !make.trim().to_ascii_uppercase().starts_with("NIKON") { return None; } let note = exif?.bytes(t::MAKER_NOTE)?; - let mn = lightcraft_tiff::makernote::parse_makernote(note, 0, note.len() as u64, order, make)?; + let mn = craft_tiff::makernote::parse_makernote(note, 0, note.len() as u64, order, make)?; let s = mn.ifd.f64s(NIKON_LENS)?; let [short, long, wide, narrow] = s.as_slice() else { return None }; let focal = |f: f64| f.is_finite() && f > 0.0 && f < 10_000.0; @@ -251,7 +251,7 @@ fn nikon_lens_spec(exif: Option<&Ifd>, make: Option<&str>, order: lightcraft_tif #[cfg(test)] pub(crate) mod tests { use super::*; - use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; + use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; fn capture_time_tiff(original: Option<&str>, digitized: Option<&str>, ifd0_time: Option<&str>, xmp: Option<&str>) -> Vec { let mut exif = IfdBuilder::new(); @@ -602,19 +602,19 @@ pub(crate) mod tests { } /// An exact `1/N` rational for a sub-second value whose reciprocal is a whole number (shutter speeds such as -/// 1/12800 s, which [`lightcraft_tiff::writer::rational`]'s decimal denominators cannot represent). -fn reciprocal(v: Option) -> Option { +/// 1/12800 s, which [`craft_tiff::writer::rational`]'s decimal denominators cannot represent). +fn reciprocal(v: Option) -> Option { let v = v.filter(|v| v.is_finite() && *v > 0.0 && *v < 1.0)?; let inv = 1.0 / v; - ((inv - inv.round()).abs() < 1e-6 && inv.round() <= u32::MAX as f64).then(|| lightcraft_tiff::Value::Rational(vec![(1, inv.round() as u32)])) + ((inv - inv.round()).abs() < 1e-6 && inv.round() <= u32::MAX as f64).then(|| craft_tiff::Value::Rational(vec![(1, inv.round() as u32)])) } /// Serialise the interchange subset of `meta` as a TIFF-structured Exif block (little-endian, no /// `Exif\0\0` prefix): IFD0 (make, model, software, artist, copyright, description, orientation, /// date), the Exif IFD (capture settings, lens) and, when present, the GPS IFD. pub fn write_exif(meta: &Metadata) -> Vec { - use lightcraft_tiff::writer::{rational, srational}; - use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; + use craft_tiff::writer::{rational, srational}; + use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; let ascii = |s: &Option| s.as_ref().filter(|v| !v.is_empty()).map(|v| Value::Ascii(v.clone())); let mut ifd0 = IfdBuilder::new(); for (tag, v) in [ diff --git a/crates/meta/src/focus.rs b/crates/meta/src/focus.rs index 5107829989..b956e667ed 100644 --- a/crates/meta/src/focus.rs +++ b/crates/meta/src/focus.rs @@ -31,7 +31,7 @@ pub use crate::focus_read::{HEADER_PREFIX, HeaderRead, MAX_HEADER_BYTES, read_header}; use crate::{Orientation, Rect}; -use lightcraft_tiff::{ByteOrder, Ifd, Tiff, makernote, tags as t}; +use craft_tiff::{ByteOrder, Ifd, Tiff, makernote, tags as t}; use serde::{Deserialize, Serialize}; /// Where the camera focused, on the upright photo. diff --git a/crates/meta/src/focus/tests.rs b/crates/meta/src/focus/tests.rs index e5f79fbfe5..148f5ed999 100644 --- a/crates/meta/src/focus/tests.rs +++ b/crates/meta/src/focus/tests.rs @@ -1,5 +1,5 @@ use super::*; -use lightcraft_tiff::{Entry, IfdBuilder, TiffWriter, Value}; +use craft_tiff::{Entry, IfdBuilder, TiffWriter, Value}; fn encipher(p: u8) -> u8 { if p >= 249 { p } else { ((u32::from(p).pow(3)) % 249) as u8 } @@ -206,7 +206,7 @@ fn sony_file_with(make: &str, model: &str, orientation: u16, note_entries: &[(u1 let mut outside: Vec = Vec::new(); order.put_u16(&mut note, note_entries.len() as u16); for (tag, v) in note_entries { - let raw = lightcraft_tiff::writer::encode(order, v); + let raw = craft_tiff::writer::encode(order, v); order.put_u16(&mut note, *tag); order.put_u16(&mut note, v.field_type() as u16); order.put_u32(&mut note, v.count() as u32); diff --git a/crates/meta/src/focus_read.rs b/crates/meta/src/focus_read.rs index 65adbee754..14547b051e 100644 --- a/crates/meta/src/focus_read.rs +++ b/crates/meta/src/focus_read.rs @@ -14,7 +14,7 @@ use std::io; use crate::focus::FocusPoint; -use lightcraft_tiff::{ByteOrder, FieldType, Tiff, tags as t}; +use craft_tiff::{ByteOrder, FieldType, Tiff, tags as t}; /// Bytes read first: the α1's and A7S III's focus data ends before 16 KB, so one read covers it. pub const HEADER_PREFIX: u64 = 32 * 1024; @@ -176,8 +176,7 @@ fn tiff(r: &mut Reader, head: Vec) -> io::Result> { // file length: only the loaded ranges are data, and an unrelated entry that claims a // huge value must be cut by what leaving it cost, not by what the buffer claims to hold. // The same 8 × / + 1 MiB shape the general TIFF reader uses, over `reader.bytes` instead. - let opts = - lightcraft_tiff::ParseOptions { value_budget: Some(r.bytes.saturating_mul(8).saturating_add(1 << 20)), ..Default::default() }; + let opts = craft_tiff::ParseOptions { value_budget: Some(r.bytes.saturating_mul(8).saturating_add(1 << 20)), ..Default::default() }; return Ok(crate::try_read_exif_with(&buf, &opts).ok().and_then(|m| m.focus)); } Step::Nothing => return Ok(None), @@ -354,7 +353,7 @@ impl Want<'_> { /// What of a classic TIFF the focus decoder still needs, given what is `loaded`: IFD0 (Make, Model, /// the Exif pointer), the Exif IFD (the maker-note entry), the maker note's header and IFD as -/// `lightcraft_tiff::makernote` locates it, and the focus tags' values. +/// `craft_tiff::makernote` locates it, and the focus tags' values. fn plan(buf: &[u8], loaded: &Loaded) -> Step { let Some((order, false)) = Tiff::sniff(buf) else { return Step::Nothing }; let mut w = Want { buf, order, loaded, need: Vec::new() }; diff --git a/crates/meta/src/heif.rs b/crates/meta/src/heif.rs index 1d768f7a69..3aae1e284a 100644 --- a/crates/meta/src/heif.rs +++ b/crates/meta/src/heif.rs @@ -2,7 +2,7 @@ //! (box structure from ISO/IEC 14496-12, item model from ISO/IEC 23008-12). //! //! HEIC is what iPhones and Macs write. Its pixels need an HEVC decoder (the optional -//! `lightcraft-heif` crate), its metadata does not, so this reader is in every build: a HEIC +//! `craft-heif` crate), its metadata does not, so this reader is in every build: a HEIC //! imports with its capture time, camera and lens whether or not the build can show the picture. //! //! The `meta` box holds an item information box (`iinf`, one `infe` per item) naming each item's @@ -198,7 +198,7 @@ mod tests { use super::*; // the whole routing, not just this module: a HEIC must reach the HEIF branch of `embedded` use crate::embedded; - use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t}; + use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t}; /// One box: its own size, its four-letter kind, then its payload. fn bx(kind: &[u8; 4], payload: &[u8]) -> Vec { @@ -390,14 +390,14 @@ mod tests { assert!(embedded(&lying).exif.is_none()); } - /// The real container writer of `lightcraft-heif`, so the reader is proved against the layout + /// The real container writer of `craft-heif`, so the reader is proved against the layout /// a HEIF encoder produces and not only against this module's own test builder. #[test] fn a_synthetic_heif_file_carries_its_exif() { let tiff = iphone_exif("2026:10:18 13:00:00", "-05:00"); - let mut spec = lightcraft_heif::testdata::Spec::new(64, 64, &|x, _| [x as u16 * 4, 0, 0]); + let mut spec = craft_heif::testdata::Spec::new(64, 64, &|x, _| [x as u16 * 4, 0, 0]); spec.exif = Some(tiff.clone()); - let file = lightcraft_heif::testdata::build(&spec); + let file = craft_heif::testdata::build(&spec); assert!(is_heif(&file)); assert_eq!(embedded(&file).exif.as_deref(), Some(tiff.as_slice())); assert_eq!(crate::extract(&file).capture_time.map(|d| d.to_iso()).as_deref(), Some("2026-10-18T13:00:00-05:00")); diff --git a/crates/meta/src/iptc.rs b/crates/meta/src/iptc.rs index 391774287f..a398c6fc49 100644 --- a/crates/meta/src/iptc.rs +++ b/crates/meta/src/iptc.rs @@ -20,6 +20,7 @@ pub fn parse_iptc(b: &[u8]) -> Metadata { let mut utf8 = false; let mut date: Option = None; let mut time: Option = None; + let mut seen_keywords = std::collections::HashSet::new(); let mut i = 0usize; while i + 5 <= b.len() { if b[i] != 0x1c { @@ -43,7 +44,7 @@ pub fn parse_iptc(b: &[u8]) -> Metadata { (2, 5) => m.title = Some(decode(data, utf8)).filter(|s| !s.is_empty()), (2, 25) => { let k = decode(data, utf8); - if !k.is_empty() && !m.keywords.contains(&k) { + if !k.is_empty() && seen_keywords.insert(k.clone()) { m.keywords.push(k); } } @@ -79,6 +80,18 @@ pub(crate) mod tests { out.extend_from_slice(data); } + #[test] + fn distinct_keywords_are_kept_once_each_without_rescanning() { + let mut b = Vec::new(); + for i in 0..50_000u32 { + ds(&mut b, 2, 25, format!("k{i}").as_bytes()); + ds(&mut b, 2, 25, b"same"); + } + let m = parse_iptc(&b); + assert_eq!(m.keywords.len(), 50_001); + assert_eq!(m.keywords.iter().filter(|k| *k == "same").count(), 1); + } + pub(crate) fn sample_iim() -> Vec { let mut v = Vec::new(); ds(&mut v, 1, 90, b"\x1b%G"); diff --git a/crates/meta/src/lib.rs b/crates/meta/src/lib.rs index 91186e3d8c..4844168119 100644 --- a/crates/meta/src/lib.rs +++ b/crates/meta/src/lib.rs @@ -555,7 +555,7 @@ mod tests { j.extend(payload); }; // orientation 6, 6000 × 4000 as stored - seg([b"Exif\0\0".as_slice(), &crate::exif::tests::sample_exif(lightcraft_tiff::ByteOrder::Little)].concat()); + seg([b"Exif\0\0".as_slice(), &crate::exif::tests::sample_exif(craft_tiff::ByteOrder::Little)].concat()); seg([b"http://ns.adobe.com/xap/1.0/\0".as_slice(), packet.as_bytes()].concat()); j.extend([0xff, 0xd9]); j @@ -600,10 +600,10 @@ mod tests { assert!(near(m.regions[0].rect, DRAWN), "{:?}", m.regions[0].rect); assert_eq!(try_extract(&j).unwrap().regions, m.regions); // a TIFF-based file: the whole file is the Exif block, with the XMP in its first IFD - let mut ifd0 = lightcraft_tiff::IfdBuilder::new(); - ifd0.set(lightcraft_tiff::tags::ORIENTATION, lightcraft_tiff::Value::Short(vec![6])); - ifd0.set(lightcraft_tiff::tags::XMP, lightcraft_tiff::Value::Byte(packet.into_bytes())); - let m = extract(&lightcraft_tiff::TiffWriter::default().write(&[ifd0]).unwrap()); + let mut ifd0 = craft_tiff::IfdBuilder::new(); + ifd0.set(craft_tiff::tags::ORIENTATION, craft_tiff::Value::Short(vec![6])); + ifd0.set(craft_tiff::tags::XMP, craft_tiff::Value::Byte(packet.into_bytes())); + let m = extract(&craft_tiff::TiffWriter::default().write(&[ifd0]).unwrap()); assert_eq!((m.orientation, m.width, m.height), (Some(Orientation::Rotate90), Some(4000), Some(6000))); assert!(near(m.regions[0].rect, DRAWN), "TIFF: {:?}", m.regions[0].rect); } @@ -615,14 +615,14 @@ mod tests { fn an_orientation_only_the_xmp_states_turns_no_regions() { // 6000 × 4000 in the Exif, and no orientation let size_ifd = || { - let mut sub = lightcraft_tiff::IfdBuilder::new(); - sub.set(lightcraft_tiff::tags::PIXEL_X_DIMENSION, lightcraft_tiff::Value::Long(vec![6000])); - sub.set(lightcraft_tiff::tags::PIXEL_Y_DIMENSION, lightcraft_tiff::Value::Long(vec![4000])); + let mut sub = craft_tiff::IfdBuilder::new(); + sub.set(craft_tiff::tags::PIXEL_X_DIMENSION, craft_tiff::Value::Long(vec![6000])); + sub.set(craft_tiff::tags::PIXEL_Y_DIMENSION, craft_tiff::Value::Long(vec![4000])); sub }; - let mut exif_ifd0 = lightcraft_tiff::IfdBuilder::new(); - exif_ifd0.set_child(lightcraft_tiff::tags::EXIF_IFD, size_ifd()); - let exif = lightcraft_tiff::TiffWriter::new(lightcraft_tiff::ByteOrder::Little, false).write(&[exif_ifd0]).unwrap(); + let mut exif_ifd0 = craft_tiff::IfdBuilder::new(); + exif_ifd0.set_child(craft_tiff::tags::EXIF_IFD, size_ifd()); + let exif = craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[exif_ifd0]).unwrap(); let packet = regions_packet(6000, 4000, "") .replace(r#"rdf:about="""#, r#"rdf:about="" xmlns:tiff="http://ns.adobe.com/tiff/1.0/" tiff:Orientation="6""#); let mut j = vec![0xff, 0xd8]; @@ -641,10 +641,10 @@ mod tests { assert!(near(m.regions[0].rect, DRAWN), "turned by an orientation the Exif doesn't state: {:?}", m.regions[0].rect); assert_eq!(try_extract(&j).unwrap().regions, m.regions); // a TIFF-based file: the first IFD has no orientation, its XMP (tag 700) has - let mut ifd0 = lightcraft_tiff::IfdBuilder::new(); - ifd0.set(lightcraft_tiff::tags::XMP, lightcraft_tiff::Value::Byte(packet.into_bytes())); - ifd0.set_child(lightcraft_tiff::tags::EXIF_IFD, size_ifd()); - let m = extract(&lightcraft_tiff::TiffWriter::default().write(&[ifd0]).unwrap()); + let mut ifd0 = craft_tiff::IfdBuilder::new(); + ifd0.set(craft_tiff::tags::XMP, craft_tiff::Value::Byte(packet.into_bytes())); + ifd0.set_child(craft_tiff::tags::EXIF_IFD, size_ifd()); + let m = extract(&craft_tiff::TiffWriter::default().write(&[ifd0]).unwrap()); assert_eq!(m.orientation, Some(Orientation::Rotate90)); assert!(near(m.regions[0].rect, DRAWN), "TIFF: {:?}", m.regions[0].rect); } diff --git a/crates/meta/src/psd.rs b/crates/meta/src/psd.rs index bda16d9602..ebdbdaf723 100644 --- a/crates/meta/src/psd.rs +++ b/crates/meta/src/psd.rs @@ -177,9 +177,9 @@ mod tests { #[test] fn try_extract_accepts_unknown_exif_tags_in_psd() { - let mut ifd0 = lightcraft_tiff::IfdBuilder::new(); - ifd0.set(0xc001, lightcraft_tiff::Value::Ascii("vendor metadata".into())); - let exif = lightcraft_tiff::TiffWriter::default().write(&[ifd0]).unwrap(); + let mut ifd0 = craft_tiff::IfdBuilder::new(); + ifd0.set(0xc001, craft_tiff::Value::Ascii("vendor metadata".into())); + let exif = craft_tiff::TiffWriter::default().write(&[ifd0]).unwrap(); let file = image(1, &[], &resource(1058, b"", &exif)); assert!(crate::try_extract(&file).is_ok()); } diff --git a/crates/meta/src/tags.rs b/crates/meta/src/tags.rs index 643d0fb28d..e16e5663a4 100644 --- a/crates/meta/src/tags.rs +++ b/crates/meta/src/tags.rs @@ -4,7 +4,7 @@ //! binary blobs are summarised by size; of a maker note only the lens name is listed (the //! Panasonic one, as Panasonic writes no Exif LensModel). -use lightcraft_tiff::{Ifd, Tiff, Value}; +use craft_tiff::{Ifd, Tiff, Value}; /// One metadata row. #[derive(Clone, Debug, PartialEq)] @@ -318,7 +318,7 @@ fn nikon_nef_compression(tiff: &Tiff, bytes: &[u8]) -> Option<&'static str> { return None; } let e = ifd0.exif.as_ref()?.get(0x927c)?; - let mn = lightcraft_tiff::makernote::parse_makernote(bytes, e.offset, e.count() as u64, tiff.order, &make)?; + let mn = craft_tiff::makernote::parse_makernote(bytes, e.offset, e.count() as u64, tiff.order, &make)?; nef_compression(mn.ifd.u32(0x0093)?) } @@ -516,7 +516,7 @@ mod tests { let rw2 = crate::exif::tests::panasonic_rw2("LUMIX S 85/F1.8"); assert_eq!(lens(&file_tag_rows(&rw2)), ["LUMIX S 85/F1.8"]); // a JPEG: the note is in its own Exif - let exif = crate::exif::tests::panasonic_exif(lightcraft_tiff::ByteOrder::Big, Some("LUMIX G 25/F1.7"), None); + let exif = crate::exif::tests::panasonic_exif(craft_tiff::ByteOrder::Big, Some("LUMIX G 25/F1.7"), None); let mut jpg = vec![0xff, 0xd8, 0xff, 0xe1]; jpg.extend_from_slice(&((exif.len() + 8) as u16).to_be_bytes()); jpg.extend(b"Exif\0\0"); diff --git a/crates/meta/src/xmp.rs b/crates/meta/src/xmp.rs index 958abed653..2326b0f722 100644 --- a/crates/meta/src/xmp.rs +++ b/crates/meta/src/xmp.rs @@ -170,6 +170,18 @@ pub(crate) const MAX_OPAQUE_NAME_BYTES: usize = 8 * 1024 * 1024; /// retains its prefix once per property: node limits alone cannot bound that duplication. const MAX_KEY_BYTES: usize = 1 << 20; +/// Most namespace declarations in scope at once. Prefix lookup scans them, so declarations times the names of a +/// start tag is the work; real packets declare a few dozen. +const MAX_NS_DECLS: usize = 512; + +/// Refuses a scope stack holding more than [`MAX_NS_DECLS`] declarations (one count per start tag over a stack at most [`MAX_DEPTH`] deep). +pub(crate) fn check_scopes(scopes: &[Vec<(String, String)>]) -> Result<(), XmpError> { + if scopes.iter().map(Vec::len).sum::() > MAX_NS_DECLS { + return Err(XmpError::TooComplex); + } + Ok(()) +} + /// Names canonicalised by namespace URI (see [`NAMESPACES`]); conflicting foreign prefixes are opaque. pub(crate) fn canonical_name(qname: &str, scopes: &[Vec<(String, String)>], opaque_budget: &mut usize) -> Result { canonical(qname, scopes, opaque_budget) @@ -240,14 +252,21 @@ fn xml_safe(s: &str) -> String { s.chars().filter(|&c| is_xml_char(c)).collect() } -/// Unescape attribute values (predefined + numeric references; unknown references kept verbatim). +/// Index of the `;` that ends a reference starting at `rest` (its `&`). The scan stops at the first +/// byte that cannot appear in a name or a numeric reference, so zero-padded references such as +/// `a` are found and each run of name bytes is read once, not once per `&`. +fn entity_end(rest: &str) -> Option { + let n = rest.bytes().skip(1).take_while(|b| b.is_ascii_alphanumeric() || *b == b'#').count(); + (rest.as_bytes().get(n + 1) == Some(&b';')).then_some(n + 1) +} + pub(crate) fn unescape_attr(s: &str) -> String { let mut out = String::with_capacity(s.len()); let mut rest = s; while let Some(i) = rest.find('&') { out.push_str(&rest[..i]); rest = &rest[i..]; - match rest.find(';').and_then(|j| entity(&rest[1..j]).map(|c| (c, j))) { + match entity_end(rest).and_then(|j| entity(&rest[1..j]).map(|c| (c, j))) { Some((c, j)) => { out.push(c); rest = &rest[j + 1..]; @@ -291,6 +310,7 @@ fn parse_tree(s: &str) -> Result { } } scopes.push(scope); + check_scopes(&scopes)?; let name = canonical(&String::from_utf8_lossy(e.name().as_ref()), &scopes, &mut opaque_budget)?; let attrs = raw_attrs.into_iter().map(|(k, v)| Ok((canonical(&k, &scopes, &mut opaque_budget)?, v))).collect::>()?; @@ -353,8 +373,13 @@ fn is_rdf_meta_attr(k: &str) -> bool { fn collect_description(d: &Node, prefix: &str, props: &mut BTreeMap>, budget: &mut usize) { for (k, v) in &d.attrs { - if !is_rdf_meta_attr(k) && take_key_bytes(&format!("{prefix}{k}"), budget) { - props.entry(format!("{prefix}{k}")).or_default().push(v.clone()); + // the budget is checked before the path is built, not after + if is_rdf_meta_attr(k) || *budget < prefix.len().saturating_add(k.len()) { + continue; + } + let key = format!("{prefix}{k}"); + if take_key_bytes(&key, budget) { + props.entry(key).or_default().push(v.clone()); } } for c in &d.children { @@ -374,6 +399,11 @@ fn take_key_bytes(key: &str, budget: &mut usize) -> bool { } fn collect_property(p: &Node, prefix: &str, props: &mut BTreeMap>, budget: &mut usize) { + // a path that can't fit the budget isn't built (a long ancestor path shared by many children + // would otherwise be copied for each of them after the budget is spent) + if *budget < prefix.len().saturating_add(p.name.len()) { + return; + } let key = format!("{prefix}{}", p.name); if let Some(arr) = p.children.iter().find(|c| matches!(c.name.as_str(), "rdf:Seq" | "rdf:Bag" | "rdf:Alt")) { let mut items: Vec<(bool, String)> = arr @@ -393,6 +423,12 @@ fn collect_property(p: &Node, prefix: &str, props: &mut BTreeMap String { return format!("1/{}", inv.round() as u64); } } - let (n, d) = lightcraft_tiff::writer::rational(v); + let (n, d) = craft_tiff::writer::rational(v); format!("{n}/{d}") } fn sfrac(v: f64) -> String { - let (n, d) = lightcraft_tiff::writer::srational(v); + let (n, d) = craft_tiff::writer::srational(v); format!("{n}/{d}") } diff --git a/crates/meta/src/xmp/tests.rs b/crates/meta/src/xmp/tests.rs index 6281546041..66d807a7d6 100644 --- a/crates/meta/src/xmp/tests.rs +++ b/crates/meta/src/xmp/tests.rs @@ -530,6 +530,12 @@ fn flattened_keys_stay_within_the_cumulative_budget() { assert!(!d.properties.is_empty(), "properties parsed before the budget are kept"); } +#[test] +fn zero_padded_numeric_references_decode() { + assert_eq!(unescape_attr("Canon A A"), "Canon A A"); + assert_eq!(unescape_attr("&abc&#xyz;&;"), "&abc&#xyz;&;"); +} + #[test] fn entity_rejects_signed_numeric_references() { assert_eq!(entity("#+65"), None); @@ -556,3 +562,43 @@ fn malformed_capture_time_without_fallback_is_not_projected() { assert_eq!(parsed.metadata.capture_time, None); assert_eq!(parsed.properties["exif:DateTimeOriginal"], vec!["2026-01-01T".to_string()]); } + +#[test] +fn a_start_tag_with_thousands_of_namespace_declarations_is_refused() { + let decls: String = (0..2000).map(|i| format!(" xmlns:p{i}=\"urn:x{i}\"")).collect(); + let xmp = format!( + "" + ); + assert!(matches!(parse_xmp(&xmp), Err(XmpError::TooComplex))); +} + +#[test] +fn many_ampersands_before_one_semicolon_are_read_once() { + // 200 000 `&` then a `;`: each `&` once searched the whole rest of the attribute for the `;` + let hostile = format!("{};&", "&".repeat(200_000)); + assert_eq!(unescape_attr(&hostile), format!("{};&", "&".repeat(200_000))); + assert_eq!(unescape_attr("<a> 􏿿 &unknownreference;"), " \u{10FFFF} &unknownreference;"); +} + +/// Children that add no key (empty structs) still had their whole ancestor path copied, each, +/// after the budget was spent: a 250 KB path under 100 000 such children was 25 GB of copying. +#[test] +fn paths_are_not_built_past_the_key_budget() { + let level = "n".repeat(60_000); + let mut doc = String::from( + "", + ); + for _ in 0..4 { + doc.push_str(&format!("")); + } + for _ in 0..100_000 { + doc.push_str(""); + } + for _ in 0..4 { + doc.push_str(&format!("")); + } + doc.push_str(""); + let t = std::time::Instant::now(); + let _ = parse_xmp(&doc); + assert!(t.elapsed().as_secs() < 10, "took {:?}", t.elapsed()); +} diff --git a/crates/meta/src/xmp_merge.rs b/crates/meta/src/xmp_merge.rs index e1e4bca5e5..f9c0efcf4c 100644 --- a/crates/meta/src/xmp_merge.rs +++ b/crates/meta/src/xmp_merge.rs @@ -23,7 +23,7 @@ use std::collections::{BTreeSet, HashSet}; use quick_xml::events::Event; -use crate::xmp::{MAX_OPAQUE_NAME_BYTES, XmpError, canonical_name, parse_xmp, unescape_attr}; +use crate::xmp::{MAX_OPAQUE_NAME_BYTES, XmpError, canonical_name, check_scopes, parse_xmp, unescape_attr}; /// Which properties [`merge_xmp`] takes from the fresh packet. Names are canonical /// `prefix:name` (prefixes as [`crate::parse_xmp`] reports them); `prefix:*` is a whole namespace. @@ -75,6 +75,8 @@ struct Scan { descs: Vec, /// Where the first `` starts. rdf_close: Option, + /// The span of a self-closing `` (used only when there is no ``). + rdf_empty: Option<(usize, usize)>, } enum Kind { @@ -132,6 +134,7 @@ fn scan(s: &str) -> Result { raw_attrs.push((k, v)); } scopes.push(scope); + check_scopes(&scopes)?; let raw_name = String::from_utf8_lossy(e.name().as_ref()).into_owned(); let name = canonical_name(&raw_name, &scopes, &mut opaque_budget)?; let kind = match stack.last() { @@ -148,6 +151,9 @@ fn scan(s: &str) -> Result { _ => Kind::Other, }; if empty { + if matches!(kind, Kind::Rdf) { + out.rdf_empty = out.rdf_empty.or(Some((before, after))); + } scopes.pop(); if let Kind::Prop { desc, start, name } = kind && let Some(d) = out.descs.get_mut(desc) @@ -213,7 +219,9 @@ pub fn merge_xmp(existing: &str, fresh: &str, rules: MergeRules) -> Result = fresh_desc.props.iter().map(|p| p.name.as_str()).collect(); @@ -261,8 +269,16 @@ pub fn merge_xmp(existing: &str, fresh: &str, rules: MergeRules) -> Result"); if written > 0 { - let at = old.get(..rdf_close).map_or(rdf_close, |h| h.trim_end().len()); - edits.push((at, at, block)); + if let Some(rdf_close) = ex.rdf_close { + let at = old.get(..rdf_close).map_or(rdf_close, |h| h.trim_end().len()); + edits.push((at, at, block)); + } else if let Some((s, e)) = ex.rdf_empty { + // a self-closing `` is opened up around the new description + let tag = old.get(s..e).ok_or_else(|| XmpError::Xml("bad span".into()))?; + let open = tag.trim_end().strip_suffix("/>").ok_or_else(|| XmpError::Xml("bad rdf:RDF tag".into()))?; + let name = tag.get(1..).and_then(|t| t.split(|c: char| c.is_whitespace() || c == '/' || c == '>').next()).unwrap_or("rdf:RDF"); + edits.push((s, e, format!("{open}>{block}\n"))); + } } edits.sort_by_key(|e| (e.0, e.1)); // overlapping edits can't happen (properties sit inside their description, which is either @@ -386,6 +402,16 @@ mod tests { assert_eq!(merge_xmp(&again, &fresh(1, &[]), RULES).unwrap(), again, "idempotent"); } + #[test] + fn a_self_closing_rdf_element_is_opened_not_replaced() { + let ex = ""; + let out = merge_xmp(ex, &fresh(3, &[]), RULES).unwrap(); + assert!(out.starts_with(""), "{out}"); + assert_eq!(parse_xmp(&out).unwrap().properties.get("xmp:Rating"), Some(&vec!["3".to_string()])); + assert!(merge_xmp("", &fresh(3, &[]), RULES).is_err(), "no RDF element at all"); + } + #[test] fn owned_if_present_and_prefix_independence() { // the existing packet binds the namespaces to other prefixes, and has a capture time diff --git a/crates/pipeline/src/crystal.rs b/crates/pipeline/src/crystal.rs new file mode 100644 index 0000000000..325329d3ff --- /dev/null +++ b/crates/pipeline/src/crystal.rs @@ -0,0 +1,1082 @@ +//! Crystal-layer film grain, the grain of process V7 and later ([`crate::finish::GrainModel`]). +//! +//! After the model of Aurélien Pierre, *Stochastic photographic grain synthesis from +//! crystallographic structure simulation* (2023), implemented from the article's description of +//! the method; no code was taken from it or from any implementation. `docs/grain.md` has the +//! method, the calibration and the measurements. +//! +//! - **Emulsion.** A stack of layers. Each layer has one crystal shape (a regular polygon of 3–8 +//! vertices, a random rotation, a log-normal width) and is a Boolean model: Poisson seeds grown +//! into crystals, at the density that leaves half the layer covered ([`FILLING`]). +//! - **Capture.** A point's grain is the number of layers covering it, less its mean: many +//! independent layers of different crystal sizes give dense, soft, cloudy grain. +//! - **Print.** The light a point captures is proportional to the light falling on it, and the +//! print shows less grain towards paper white: the grain is `Y·(1 − Y)` in linear light, so +//! it fades out at black and at white. It is added in the display-encoded domain (first order), +//! where it is symmetric: it neither darkens shadows nor greys blown highlights. +//! +//! **Resolution independence.** The emulsion is drawn once per setting on two periodic tiles +//! (tori) in frame coordinates, at twice the resolution of a 6000 px export ([`PITCH`]). Each +//! output pixel takes the exact mean of the emulsion under its footprint from the tiles' summed +//! area tables (a rotated frame's pixel: of an axis-aligned square of its size), so a preview +//! shows exactly the export's grain box-downscaled to its size, and an +//! export larger than 6000 px still has crystals rather than enlarged pixels. The layers are +//! split between two tori of different sizes ([`TORI`]); their pattern only repeats as a whole +//! after tens of thousands of pixels. +//! +//! **Cost.** Drawing the tiles takes a few milliseconds and is cached per setting +//! ([`CrystalGrain::new`]); a pixel then costs two box sums (16 table reads), on the CPU and in the +//! GPU kernel (`finish.wgsl`, which reads the same tables). + +use std::f64::consts::TAU; +use std::sync::{Arc, Condvar, Mutex, OnceLock, PoisonError}; + +use lightcraft_color::transfer::srgb_to_linear; +use lightcraft_develop::Grain; +use lightcraft_raster::par_rows; + +/// Long edge (output pixels) at which grain sizes and strengths are specified: a 24 MP export. +pub const REF_LONG: f64 = 6000.0; +/// Size of a tile pixel in reference pixels: tiles are drawn at twice the reference resolution. +pub const PITCH: f64 = 0.5; +/// Tile pixels per long edge of the frame. +pub const TILE_PER_LONG: f64 = REF_LONG / PITCH; +/// Sides of the two tori (tile pixels: 640 and 567 reference pixels). Layers alternate between +/// them; the sizes share no small factor, so the sum repeats only after 362,880 reference pixels. +pub const TORI: [usize; 2] = [1280, 1134]; +/// Fraction of each layer covered by its crystals. +pub const FILLING: f64 = 0.5; +/// Rows per band when drawing a tile (each band is drawn in parallel). +const BAND: usize = 64; +/// Encoded luminance at which the strength is specified (mid grey, 128 of 255). +const MID_GREY: f32 = 128.0 / 255.0; +/// A footprint this large (tile pixels: a render 24 px long or less) gets no grain. +const MAX_FOOTPRINT: f32 = 500.0; +/// Settings whose tiles are kept (a preview and an export, a few thumbnails). +const CACHE: usize = 4; + +/// The look of a grain setting: what the tiles are drawn from. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct CrystalLook { + /// Median crystal width (circumscribed diameter, reference pixels). + pub width: f64, + /// Log standard deviation of the layers' crystal widths. + pub spread: f64, + /// Number of layers. + pub layers: usize, +} + +/// Piecewise-linear interpolation through `(x, y)` knots sorted by `x`, constant outside them. +fn interp(knots: &[(f64, f64)], x: f64) -> f64 { + let (Some(first), Some(last)) = (knots.first(), knots.last()) else { return 0.0 }; + if x <= first.0 { + return first.1; + } + for w in knots.windows(2) { + if let [(x0, y0), (x1, y1)] = *w + && x <= x1 + { + return y0 + (y1 - y0) * (x - x0) / (x1 - x0).max(1e-9); + } + } + last.1 +} + +/// Median crystal width at Roughness 50 by Size (reference pixels), fitted so the grain's width +/// matches Lightroom Classic's (`fit_widths`, `docs/grain.md`). Size 0 is as fine as the model +/// gets: crystals narrower than a pixel, uncorrelated pixels. +const WIDTH_BY_SIZE: [(f64, f64); 6] = [(0.0, 0.55), (10.0, 1.39), (25.0, 2.95), (50.0, 5.17), (75.0, 8.78), (100.0, 12.38)]; +/// Width factor by Roughness. +const WIDTH_BY_ROUGHNESS: [(f64, f64); 3] = [(0.0, 0.734), (50.0, 1.0), (100.0, 1.422)]; +/// Layers by Size: coarser grain averages more layers, so it stays a soft clump texture. +const LAYERS_BY_SIZE: [(f64, f64); 4] = [(0.0, 16.0), (25.0, 31.0), (50.0, 54.0), (100.0, 70.0)]; + +/// Log-width spread every look has on top of Roughness's. The look the tables were chosen from (a +/// prototype on whole-pixel crystals, `docs/grain.md`) rounded crystal widths to whole pixels, +/// which spreads their sizes by about this much; with it, the anti-aliased crystals here have the +/// prototype's texture at every scale, not just its width. +const SPREAD_FLOOR: f64 = 0.45; + +/// A slider value as a number in `0..=100`; `fallback` for NaN. +fn slider(v: f64, fallback: f64) -> f64 { + if v.is_nan() { fallback } else { v.clamp(0.0, 100.0) } +} + +impl CrystalLook { + pub fn new(size: f64, roughness: f64) -> CrystalLook { + let (s, r) = (slider(size, 25.0), slider(roughness, 50.0)); + CrystalLook { + width: interp(&WIDTH_BY_SIZE, s) * interp(&WIDTH_BY_ROUGHNESS, r), + spread: (0.1 + 0.6 * r / 100.0).hypot(SPREAD_FLOOR), + layers: interp(&LAYERS_BY_SIZE, s).round().clamp(2.0, 120.0) as usize, + } + } +} + +/// Grain strength (8-bit levels at mid grey on a 6000 px export) for Amount, Size and Roughness, +/// measured on Lightroom Classic exports (the fit of open PR #1060's `docs/grain.md`). +pub fn strength_levels(amount: f64, size: f64, roughness: f64) -> f64 { + let (s, d) = (slider(size, 25.0), slider(roughness, 50.0) / 100.0 - 0.5); + slider(amount, 0.0) / 100.0 * (2.447 + 1.386 * (-s / 19.88).exp()).exp() * (1.0 - 0.084 * d + 1.6384 * d * d) +} + +// ---- hashing + +#[inline] +fn mix(mut x: u32) -> u32 { + x ^= x >> 16; + x = x.wrapping_mul(0x7feb_352d); + x ^= x >> 15; + x = x.wrapping_mul(0x846c_a68b); + x ^ (x >> 16) +} + +#[inline] +fn hash(a: u32, b: u32, c: u32, d: u32) -> u32 { + mix(mix(mix(mix(a ^ 0x9e37_79b9) ^ b) ^ c.wrapping_mul(0x85eb_ca6b)) ^ d.wrapping_mul(0xc2b2_ae35)) +} + +/// A hash as a number in (0, 1]. +#[inline] +fn unit(h: u32) -> f64 { + (h as f64 + 0.5) / 4_294_967_296.0 +} + +/// The standard normal quantile of `p` in (0, 1) (Acklam's rational approximation, ~1e-9). +fn normal_quantile(p: f64) -> f64 { + const A: [f64; 6] = + [-39.696_830_286_653_76, 220.946_098_424_520_5, -275.928_510_446_968_7, 138.357_751_867_269, -30.664_798_066_147_16, 2.506_628_277_459_239]; + const B: [f64; 5] = [-54.476_098_798_224_06, 161.585_836_858_040_9, -155.698_979_859_886_6, 66.801_311_887_719_72, -13.280_681_552_885_72]; + const C: [f64; 6] = [ + -0.007_784_894_002_430_293, + -0.322_396_458_041_136_5, + -2.400_758_277_161_838, + -2.549_732_539_343_734, + 4.374_664_141_464_968, + 2.938_163_982_698_783, + ]; + const D: [f64; 4] = [0.007_784_695_709_041_462, 0.322_467_129_070_039_8, 2.445_134_137_142_996, 3.754_408_661_907_416]; + let p = p.clamp(1e-12, 1.0 - 1e-12); + let tail = + |q: f64| (((((C[0] * q + C[1]) * q + C[2]) * q + C[3]) * q + C[4]) * q + C[5]) / ((((D[0] * q + D[1]) * q + D[2]) * q + D[3]) * q + 1.0); + if p < 0.024_25 { + tail((-2.0 * p.ln()).sqrt()) + } else if p > 1.0 - 0.024_25 { + -tail((-2.0 * (1.0 - p).ln()).sqrt()) + } else { + let q = p - 0.5; + let r = q * q; + (((((A[0] * r + A[1]) * r + A[2]) * r + A[3]) * r + A[4]) * r + A[5]) * q + / (((((B[0] * r + B[1]) * r + B[2]) * r + B[3]) * r + B[4]) * r + 1.0) + } +} + +/// The emulsion's layers. Their crystal widths are the `layers` quantiles of the log-normal +/// width distribution, dealt to the layers in an order the seed shuffles: every seed has the same +/// mix of sizes (so the same grain width and strength), in a different arrangement. +fn emulsion(seed: u32, look: &CrystalLook) -> Vec { + let n = look.layers.max(1); + let mut order: Vec<(u32, usize)> = (0..n).map(|l| (hash(seed, l as u32, 0x5047, 0), l)).collect(); + order.sort_unstable(); + let mut rank = vec![0usize; n]; + for (r, &(_, l)) in order.iter().enumerate() { + if let Some(slot) = rank.get_mut(l) { + *slot = r; + } + } + (0..n).map(|l| Layer::new(seed, l as u32, look, normal_quantile((rank.get(l).copied().unwrap_or(0) as f64 + 0.5) / n as f64))).collect() +} + +fn normal(h1: u32, h2: u32) -> f64 { + (-2.0 * unit(h1).ln()).sqrt() * (TAU * unit(h2)).cos() +} + +/// The cumulative distribution of a Poisson count of mean `mu`, up to where it reaches 1 (at +/// most 512 entries; `mu` stays below ~150 here). +fn poisson_cdf(mu: f64) -> Vec { + let mut cdf = Vec::new(); + let mut p = (-mu).exp(); + let mut cum = p; + for k in 1..512u32 { + cdf.push(cum); + if cum >= 1.0 - 1e-12 { + break; + } + p *= mu / k as f64; + cum += p; + } + cdf +} + +/// A Poisson count drawn from its `cdf` by hash `h`. +#[inline] +fn poisson(cdf: &[f64], h: u32) -> usize { + let u = unit(h); + cdf.partition_point(|&c| c < u) +} + +// ---- one layer + +/// A stream of hashes: a Weyl sequence through [`mix`]. +struct Stream(u32); + +impl Stream { + #[inline] + fn next(&mut self) -> u32 { + self.0 = self.0.wrapping_add(0x9e37_79b9); + mix(self.0) + } +} + +/// Sub-pixel positions a crystal centre is rounded to, per axis, when drawn from its [`Stamp`]. +const PHASES: usize = 16; +/// Crystals up to this circumradius (tile pixels) are drawn from a stamp. +const STAMP_R: f64 = 16.0; + +/// A layer's crystal rasterised at every sub-pixel phase of its centre: for a centre at +/// `(X + qx/PHASES, Y + qy/PHASES)` (`X`, `Y` integers) it covers rows `Y + rows[qy].0 ..= Y + +/// rows[qy].1`, and in row `Y + rows[qy].0 + k` the pixels `X + o.0 ..= X + o.1`, with +/// `o = spans[(qy·PHASES + qx)·height + k]`. +/// +/// The same rows as bit masks, for drawing a crystal without a branch per row: phase +/// `p = qy·PHASES + qx` spans pixels `X + extent[p].0 ..= X + extent[p].1`, and its row `k` is +/// `masks[p·height + k]` shifted to `X + extent[p].0` (rows past the phase's own are empty). +/// `masks` is empty when a phase is wider than 64 pixels. +#[derive(Clone, Debug)] +struct Stamp { + rows: [(i32, i32); PHASES], + height: usize, + spans: Vec<(i32, i32)>, + extent: Vec<(i32, i32)>, + masks: Vec, +} + +/// Where [`Layer::draw`] draws: rows `y0..y0 + rows` of a torus of side `t`, `w` words per row, +/// after `margin` scratch rows. +#[derive(Clone, Copy)] +struct Band { + t: usize, + y0: usize, + rows: usize, + w: usize, + margin: usize, +} + +/// One emulsion layer: its crystal (a regular polygon) and seed density, in tile pixels. +#[derive(Clone, Debug)] +struct Layer { + /// The crystal as half-planes, relative to its centre: `x ≥ a + s·y` for each `(a, s)` of + /// `left` (`nl` used), `x ≤ a + s·y` for `right` (`nr` used), `y0 ≤ y ≤ y1`. + left: [(f64, f64); 8], + right: [(f64, f64); 8], + nl: usize, + nr: usize, + y0: f64, + y1: f64, + /// Circumradius. + r: f64, + /// Seeds per tile pixel. + lambda: f64, + seed: u32, + index: u32, + stamp: Option, +} + +/// Crystals narrower than this (tile pixels) cover at most two neighbouring pixel centres: their +/// layer is drawn as independent pixels. +const FINE: f64 = 1.5; + +impl Layer { + /// Layer `index` of the emulsion of `seed`, its crystal width `z` log standard deviations + /// from the median. + fn new(seed: u32, index: u32, look: &CrystalLook, z: f64) -> Layer { + let h = |k: u32| hash(seed, index, k, 0x51ed); + let n = (4.5 + normal(h(1), h(2))).round().clamp(3.0, 8.0) as usize; + let rot = TAU * unit(h(3)); + let width = look.width.max(0.05) * (look.spread * z.clamp(-2.5, 2.5)).exp(); + let r = width / 2.0 / PITCH; + let vert = |k: usize| { + let a = rot + TAU * k as f64 / n as f64; + (r * a.cos(), r * a.sin()) + }; + let mut layer = Layer { + left: [(0.0, 0.0); 8], + right: [(0.0, 0.0); 8], + nl: 0, + nr: 0, + y0: f64::INFINITY, + y1: f64::NEG_INFINITY, + r, + lambda: -(1.0 - FILLING).ln() / (0.5 * n as f64 * r * r * (TAU / n as f64).sin()).max(1e-9), + seed, + index, + stamp: None, + }; + for k in 0..n { + let ((x0, y0), (x1, y1)) = (vert(k), vert(k + 1)); + layer.y0 = layer.y0.min(y0); + layer.y1 = layer.y1.max(y0); + let dy = y1 - y0; + if dy.abs() < 1e-12 { + continue; + } + // counter-clockwise: the inside is left of each edge + let sl = (x1 - x0) / dy; + let line = (x0 - sl * y0, sl); + if dy > 0.0 { + if let Some(slot) = layer.right.get_mut(layer.nr) { + *slot = line; + layer.nr += 1; + } + } else if let Some(slot) = layer.left.get_mut(layer.nl) { + *slot = line; + layer.nl += 1; + } + } + if r <= STAMP_R && 2.0 * r >= FINE { + layer.stamp = Some(layer.stamp()); + } + layer + } + + fn stamp(&self) -> Stamp { + let q = PHASES as f64; + let mut rows = [(0, -1); PHASES]; + for (qy, r) in rows.iter_mut().enumerate() { + let g = qy as f64 / q; + *r = ((g + self.y0 - 0.5).ceil() as i32, (g + self.y1 - 0.5).floor() as i32); + } + let height = rows.iter().map(|(a, b)| (b - a + 1).max(0) as usize).max().unwrap_or(0); + let mut spans = vec![(0, -1); PHASES * PHASES * height]; + for (qy, &(a, b)) in rows.iter().enumerate() { + for k in 0..(b - a + 1).max(0) { + let (xl, xr) = self.span((a + k) as f64 + 0.5 - qy as f64 / q); + for qx in 0..PHASES { + let f = qx as f64 / q; + if let Some(o) = spans.get_mut((qy * PHASES + qx) * height + k as usize) { + *o = ((f + xl - 0.5).ceil() as i32, (f + xr - 0.5).floor() as i32); + } + } + } + } + let mut extent = vec![(0, -1); PHASES * PHASES]; + let mut masks = vec![0u64; PHASES * PHASES * height]; + let mut fits = true; + for (p, e) in extent.iter_mut().enumerate() { + let Some(rows) = spans.get(p * height..(p + 1) * height) else { continue }; + let used = || rows.iter().filter(|(o0, o1)| o0 <= o1); + let (Some(lo), Some(hi)) = (used().map(|o| o.0).min(), used().map(|o| o.1).max()) else { continue }; + *e = (lo, hi); + fits &= hi - lo < 64; + for (m, &(o0, o1)) in masks.iter_mut().skip(p * height).zip(rows) { + if o0 <= o1 && o1 - lo < 64 { + *m = (!0u64 >> (63 - (o1 - o0))) << (o0 - lo); + } + } + } + if !fits { + masks.clear(); + } + Stamp { rows, height, spans, extent, masks } + } + + /// The crystal's horizontal extent at height `y` above its centre (convex: one interval). + #[inline] + fn span(&self, y: f64) -> (f64, f64) { + let lo = self.left.iter().take(self.nl).fold(f64::NEG_INFINITY, |m, &(a, s)| m.max(a + s * y)); + let hi = self.right.iter().take(self.nr).fold(f64::INFINITY, |m, &(a, s)| m.min(a + s * y)); + (lo, hi) + } + + /// Sets the bits of the pixels the layer covers in rows `y0..` of a torus of side `t`: + /// `cover` holds whole rows of `w` words (bit `i % 64` of word `i / 64` is pixel `i`), the + /// band's rows between `margin` rows above and below it (scratch: what lands there is not part + /// of the band; `margin` is at least the stamp's height). + fn draw(&self, t: usize, y0: usize, w: usize, margin: usize, cover: &mut [u64]) { + let rows = (cover.len() / w.max(1)).saturating_sub(2 * margin); + if 2.0 * self.r < FINE { + // each pixel covered with probability FILLING (one half: one random bit) + for (j, row) in cover.chunks_mut(w.max(1)).skip(margin).take(rows).enumerate() { + let mut bits = Stream(hash(self.seed, self.index, (y0 + j) as u32, 0xb175)); + for v in row.iter_mut() { + *v = bits.next() as u64 | (bits.next() as u64) << 32; + } + if let (Some(last), r @ 1..=63) = (row.last_mut(), t % 64) { + *last &= (1u64 << r) - 1; + } + } + return; + } + let tf = t as f64; + let m = (tf / (4.0 * self.r).max(16.0)).floor().clamp(1.0, tf) as i64; + let c = tf / m as f64; + let cdf = poisson_cdf(self.lambda * c * c); + let (lo, hi) = (y0 as f64 - self.r - 1.0, (y0 + rows) as f64 + self.r + 1.0); + for cy in (lo / c).floor() as i64..=(hi / c).floor() as i64 { + let wy = cy.rem_euclid(m) as u32; + for cx in 0..m { + let mut h = Stream(hash(self.seed ^ (self.index << 20), cx as u32, wy, 0xce11)); + for _ in 0..poisson(&cdf, h.next()) { + let (u, v) = (unit(h.next()), unit(h.next())); + self.splat((cx as f64 + u) * c, (cy as f64 + v) * c, Band { t, y0, rows, w, margin }, cover); + } + } + } + } + + /// Marks the pixels whose centres the crystal at (`px`, `py`) covers (see [`Self::draw`]). + #[inline] + fn splat(&self, px: f64, py: f64, band: Band, cover: &mut [u64]) { + let Band { t, y0, rows, w, margin } = band; + if let Some(st) = &self.stamp { + let (bx, by) = (px.floor(), py.floor()); + let (qx, qy) = ((((px - bx) * PHASES as f64) as usize).min(PHASES - 1), (((py - by) * PHASES as f64) as usize).min(PHASES - 1)); + let (bx, by) = (bx as i64, by as i64); + let Some(&(a, b)) = st.rows.get(qy) else { return }; + let (lo, hi) = ((by + a as i64).max(y0 as i64), (by + b as i64).min((y0 + rows) as i64 - 1)); + if hi < lo { + return; + } + // within the torus across: the whole stamp, a fixed number of rows of masks (those + // above or below the band land in the margins) + let p = qy * PHASES + qx; + if let (Some(&(e0, e1)), Some(masks)) = (st.extent.get(p), st.masks.get(p * st.height..(p + 1) * st.height)) { + let (xs, top) = (bx + e0 as i64, by + a as i64 - y0 as i64 + margin as i64); + if xs >= 0 && bx + (e1 as i64) < t as i64 && top >= 0 { + let (word, shift) = ((xs >> 6) as usize, (xs & 63) as u32); + // a bit past the row's last word would be a pixel ≥ t: the high word is then 0 + let start = top as usize * w + word; + for (k, &m) in masks.iter().enumerate() { + let v = (m as u128) << shift; + let i = start + k * w; + if let Some(c) = cover.get_mut(i) { + *c |= v as u64; + } + if let Some(c) = cover.get_mut(i + 1) { + *c |= (v >> 64) as u64; + } + } + return; + } + } + // rows `lo..=hi` of the band, spans `k0..` of the stamp's rows (`hi ≤ by + b`: within its height) + let (k0, n) = ((lo - by - a as i64) as usize, (hi - lo + 1) as usize); + let base = (qy * PHASES + qx) * st.height + k0; + let (r0, r1) = (lo as usize - y0 + margin, hi as usize + 1 - y0 + margin); + let (Some(spans), Some(band)) = (st.spans.get(base..base + n), cover.get_mut(r0 * w..r1 * w)) else { return }; + for (row, &(o0, o1)) in band.chunks_exact_mut(w.max(1)).zip(spans) { + if o0 <= o1 { + set_wrapped(row, t, bx + o0 as i64, bx + o1 as i64); + } + } + return; + } + let first = (py + self.y0 - 0.5).ceil().max(y0 as f64); + let last = (py + self.y1 - 0.5).floor().min((y0 + rows) as f64 - 1.0); + if last < first { + return; + } + for j in first as usize..=last as usize { + let (xl, xr) = self.span(j as f64 + 0.5 - py); + let (i0, i1) = ((px + xl - 0.5).ceil() as i64, (px + xr - 0.5).floor() as i64); + if i1 < i0 { + continue; + } + let Some(row) = cover.get_mut((j - y0 + margin) * w..(j - y0 + margin + 1) * w) else { continue }; + set_wrapped(row, t, i0, i1); + } + } +} + +/// Sets bits `a..=b` (`a ≤ b`) of a row of words. +#[inline] +fn set_bits(row: &mut [u64], a: usize, b: usize) { + let (wa, wb) = (a >> 6, b >> 6); + let (lo, hi) = (!0u64 << (a & 63), !0u64 >> (63 - (b & 63))); + if wa == wb { + if let Some(v) = row.get_mut(wa) { + *v |= lo & hi; + } + return; + } + if let Some(v) = row.get_mut(wa) { + *v |= lo; + } + for v in row.iter_mut().take(wb).skip(wa + 1) { + *v = !0; + } + if let Some(v) = row.get_mut(wb) { + *v |= hi; + } +} + +/// Sets pixels `i0..=i1` of a periodic row of `t` pixels (`i0 ≤ i1`, within one period of +/// `0..t`: the crystal centres are in `0..t`). +#[inline] +fn set_wrapped(row: &mut [u64], t: usize, i0: i64, i1: i64) { + let ti = t as i64; + if i0 >= 0 && i1 < ti { + set_bits(row, i0 as usize, i1 as usize); + return; + } + if t == 0 { + return; + } + if i1 - i0 + 1 >= ti { + set_bits(row, 0, t - 1); + return; + } + let (a, b) = if i0 < 0 { + (i0 + ti, i1 + ti) + } else if i0 >= ti { + (i0 - ti, i1 - ti) + } else { + (i0, i1) + }; + let (a, b) = (a.clamp(0, ti - 1) as usize, b.max(0) as usize); + if b < t { + set_bits(row, a, b); + } else { + set_bits(row, a, t - 1); + set_bits(row, 0, (b - t).min(t - 1)); + } +} + +/// Bit planes of the layer counts (enough for 127 layers per torus). +const PLANES: usize = 7; + +/// Adds one layer's `cover` bits to the bit-sliced counters `planes` (`PLANES × cover.len()`). +#[inline] +fn add_layer(planes: &mut [u64], cover: &[u64]) { + let n = cover.len(); + for (i, &c) in cover.iter().enumerate() { + let mut carry = c; + for p in 0..PLANES { + if carry == 0 { + break; + } + let Some(slot) = planes.get_mut(p * n + i) else { break }; + let s = *slot; + *slot = s ^ carry; + carry &= s; + } + } +} + +// ---- tiles + +/// The emulsion drawn on the two tori: each as a summed-area table of its grain (layer count less +/// its mean), scaled so that the sum of both, averaged over a reference pixel, has unit variance. +pub struct CrystalTile { + pub look: CrystalLook, + /// `(side + 1)²` entries each: `sat[t][y·(side + 1) + x]` = sum over `[0, x) × [0, y)`. + pub sats: [Vec; 2], +} + +impl std::fmt::Debug for CrystalTile { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CrystalTile").field("look", &self.look).finish_non_exhaustive() + } +} + +/// Layer counts of torus `which` (side `t`): `t × t`, row-major. +fn draw_torus(which: usize, t: usize, seed: u32, look: &CrystalLook) -> Vec { + let layers: Vec = emulsion(seed, look).into_iter().filter(|l| l.index as usize % 2 == which).collect(); + let w = t.div_ceil(64); + // scratch rows above and below a band, for the stamps' rows outside it (`Layer::splat`) + let margin = layers.iter().filter_map(|l| l.stamp.as_ref().map(|s| s.height)).max().unwrap_or(0); + let mut count = vec![0u8; t * t]; + par_rows(&mut count, t * BAND, |band, out| { + let rows = out.len() / t.max(1); + let n = rows * w; + let (mut cover, mut planes) = (vec![0u64; n + 2 * margin * w], vec![0u64; PLANES * n]); + for layer in &layers { + cover.fill(0); + layer.draw(t, band * BAND, w, margin, &mut cover); + add_layer(&mut planes, cover.get(margin * w..margin * w + n).unwrap_or_default()); + } + for (j, row) in out.chunks_mut(t.max(1)).enumerate() { + for (i, o) in row.iter_mut().enumerate() { + let (word, bit) = (j * w + i / 64, i % 64); + *o = (0..PLANES).fold(0u8, |c, p| c | ((planes.get(p * n + word).map_or(0, |v| (v >> bit) & 1) as u8) << p)); + } + } + }); + count +} + +/// Integer summed-area table of `count` (`t × t`): `(t + 1)²` entries. +fn summed_area(count: &[u8], t: usize) -> Vec { + let s = t + 1; + let mut sat = vec![0u32; s * s]; + // row prefix sums in parallel, then each row adds the one above + par_rows(&mut sat, s, |y, row| { + let Some(src) = y.checked_sub(1).and_then(|y| count.get(y * t..(y + 1) * t)) else { return }; + let mut acc = 0u32; + for (o, &c) in row.iter_mut().skip(1).zip(src) { + acc += c as u32; + *o = acc; + } + }); + for y in 1..s { + let (above, below) = sat.split_at_mut(y * s); + let (Some(prev), Some(row)) = (above.get((y - 1) * s..), below.get_mut(..s)) else { continue }; + for (o, &p) in row.iter_mut().zip(prev) { + *o = o.wrapping_add(p); + } + } + sat +} + +/// Sum over the reference pixels (2 × 2 tile pixels) of torus `count` (`t × t`, `t` even) of +/// their squared deviation from `mean` (per tile pixel), and the number of reference pixels. +fn reference_square_sum(count: &[u8], t: usize, mean: f64) -> (f64, usize) { + let h = t / 2; + let mut rows = vec![0.0f64; h]; + par_rows(&mut rows, 1, |y, out| { + let (Some(a), Some(b)) = (count.get(2 * y * t..(2 * y + 1) * t), count.get((2 * y + 1) * t..(2 * y + 2) * t)) else { return }; + let mut s = 0.0; + for (&[a0, a1], &[b0, b1]) in a.as_chunks::<2>().0.iter().zip(b.as_chunks::<2>().0) { + let q = (a0 as u32 + a1 as u32 + b0 as u32 + b1 as u32) as f64 / 4.0 - mean; + s += q * q; + } + if let Some(o) = out.first_mut() { + *o = s; + } + }); + (rows.iter().sum(), h * h) +} + +impl CrystalTile { + /// Draws the emulsion of `look` with `seed`, or `None` if it has no grain (no variance). + pub fn new(look: CrystalLook, seed: u32) -> Option { + let [t0, t1] = TORI; + let (c0, c1) = lightcraft_raster::par_join(|| draw_torus(0, t0, seed, &look), || draw_torus(1, t1, seed, &look)); + let mut var = 0.0; + let mut tori = Vec::with_capacity(2); + for (count, t) in [(c0, t0), (c1, t1)] { + let mean = count.iter().map(|&c| c as u64).sum::() as f64 / (t * t).max(1) as f64; + let (sq, n) = reference_square_sum(&count, t, mean); + var += sq / n.max(1) as f64; + tori.push((summed_area(&count, t), t, mean)); + } + if !(var > 1e-12 && var.is_finite()) { + return None; + } + let k = 1.0 / var.sqrt(); + let mut sats = tori.into_iter().map(|(sat, t, mean)| { + let s = t + 1; + let mut out = vec![0.0f32; s * s]; + par_rows(&mut out, s, |y, row| { + for (x, (o, &v)) in row.iter_mut().zip(sat.get(y * s..).unwrap_or_default()).enumerate() { + *o = ((v as f64 - mean * (x * y) as f64) * k) as f32; + } + }); + out + }); + Some(CrystalTile { look, sats: [sats.next()?, sats.next()?] }) + } + + /// Sum of torus `which`'s grain over the box `[x0, x0 + w] × [y0, y0 + w]` (tile pixels, + /// `0 ≤ w < side`), the table bilinearly interpolated: the exact integral of the pixels. + #[inline] + fn box_sum(&self, which: usize, x0: f32, y0: f32, w: f32) -> f32 { + let (Some(sat), Some(&t)) = (self.sats.get(which), TORI.get(which)) else { return 0.0 }; + let tf = t as f32; + let (x0, y0) = (x0 - (x0 / tf).floor() * tf, y0 - (y0 / tf).floor() * tf); + let at = |x: f32, y: f32| integral(sat, t, x, y); + at(x0 + w, y0 + w) - at(x0, y0 + w) - at(x0 + w, y0) + at(x0, y0) + } + + /// Mean grain over the square of side `w` centred on (`x`, `y`) (tile pixels): unit variance + /// at `w` = 2 (a 6000 px render). + #[inline] + pub fn field(&self, x: f32, y: f32, w: f32) -> f32 { + let (x0, y0) = (x - 0.5 * w, y - 0.5 * w); + (self.box_sum(0, x0, y0, w) + self.box_sum(1, x0, y0, w)) / (w * w) + } +} + +/// The table `sat` (side `t + 1`) bilinearly interpolated at (`x`, `y`) in `0..=t`. +#[inline] +fn bilinear(sat: &[f32], t: usize, x: f32, y: f32) -> f32 { + let s = t + 1; + let (i, j) = ((x.max(0.0) as usize).min(t - 1), (y.max(0.0) as usize).min(t - 1)); + let (fx, fy) = (x - i as f32, y - j as f32); + let g = |i: usize, j: usize| sat.get(j * s + i).copied().unwrap_or(0.0); + let a = g(i, j) + (g(i + 1, j) - g(i, j)) * fx; + let b = g(i, j + 1) + (g(i + 1, j + 1) - g(i, j + 1)) * fx; + a + (b - a) * fy +} + +/// The periodic grain's integral over `[0, x) × [0, y)` for `x`, `y` in `0..2t`. +#[inline] +fn integral(sat: &[f32], t: usize, x: f32, y: f32) -> f32 { + let tf = t as f32; + let (wx, wy) = (x >= tf, y >= tf); + let (rx, ry) = (if wx { x - tf } else { x }, if wy { y - tf } else { y }); + let mut v = bilinear(sat, t, rx, ry); + if wx { + v += bilinear(sat, t, tf, ry); + } + if wy { + v += bilinear(sat, t, rx, tf); + } + if wx && wy { + v += bilinear(sat, t, tf, tf); + } + v +} + +/// The integral of torus `sat` (side `t`) over `[0, i) × [0, across)` (`along_x`) or +/// `[0, across) × [0, i)`, for `i` in `0..=t`: the table cut along a line, with the periodic part +/// of `across` folded in (`across` in `0..2t`). +fn line(sat: &[f32], t: usize, along_x: bool, across: f32, out: &mut Vec) { + let (tf, n) = (t as f32, t + 1); + let wrap = across >= tf; + let a = (if wrap { across - tf } else { across }).clamp(0.0, tf); + let j = (a as usize).min(t.saturating_sub(1)); + let f = (a - j as f32) as f64; + out.clear(); + out.resize(n, 0.0); + // entry `i` of table line `k` (a row when cutting along x, a column when along y) + let at = |k: usize, i: usize| if along_x { sat.get(k * n + i) } else { sat.get(i * n + k) }.copied().unwrap_or(0.0) as f64; + for (i, o) in out.iter_mut().enumerate() { + let (p, q) = (at(j, i), at(j + 1, i)); + *o = p + (q - p) * f + if wrap { at(t, i) } else { 0.0 }; + } +} + +/// [`line`] interpolated at `x ≥ 0` (any number of periods along). +#[inline] +fn along(line: &[f64], t: usize, inv_t: f64, x: f64) -> f64 { + let tf = t as f64; + let q = (x * inv_t).floor().max(0.0); + let r = (x - q * tf).clamp(0.0, tf); + let i = (r as usize).min(t.saturating_sub(1)); + let f = r - i as f64; + let (Some(&a), Some(&b), Some(&end)) = (line.get(i), line.get(i + 1), line.get(t)) else { return 0.0 }; + a + (b - a) * f + q * end +} + +impl CrystalTile { + /// The grain of a line of `out.len()` output pixels side by side, each a square of side `w` + /// (tile pixels), the first centred on (`x`, `y`), the next `step` (`±w`) along x (`along_x`) + /// or y. The table is cut once along the line's two box edges; each pixel then costs one + /// interpolation per torus instead of [`Self::field`]'s eight table lookups. + pub fn row_field(&self, x: f32, y: f32, w: f32, step: f32, along_x: bool, out: &mut [f32]) { + out.fill(0.0); + let n = out.len(); + let (pos, across) = if along_x { (x, y) } else { (y, x) }; + let sign = if step < 0.0 { -1.0 } else { 1.0 }; + let first = pos as f64 - 0.5 * step as f64; + let lowest = first.min(first + n as f64 * step as f64); + let (mut near, mut far) = (Vec::new(), Vec::new()); + for (which, &t) in TORI.iter().enumerate() { + let Some(sat) = self.sats.get(which) else { continue }; + let tf = t as f32; + let lo = across - 0.5 * w; + let lo = lo - (lo / tf).floor() * tf; + line(sat, t, along_x, lo, &mut near); + line(sat, t, along_x, lo + w, &mut far); + let shift = (lowest / t as f64).floor() * t as f64; + let inv_t = 1.0 / t as f64; + let edge = |k: usize| { + let e = first + k as f64 * step as f64 - shift; + along(&far, t, inv_t, e) - along(&near, t, inv_t, e) + }; + let mut prev = edge(0); + for (k, o) in out.iter_mut().enumerate() { + let next = edge(k + 1); + *o += (sign * (next - prev)) as f32; + prev = next; + } + } + let area = w * w; + out.iter_mut().for_each(|v| *v /= area); + } +} + +// ---- the render's grain + +/// The crystal grain of one render. +#[derive(Clone, Debug)] +pub struct CrystalGrain { + pub tile: Arc, + /// Encoded-luminance amplitude at unit field and unit tone weight. + pub k: f32, + /// An output pixel's side in tile pixels. + pub foot: f32, +} + +type Key = (u64, u64, u32); + +/// The cache key of the tile of `look` and `seed`. +fn key_of(look: CrystalLook, seed: u32) -> Key { + (look.width.to_bits(), look.spread.to_bits() ^ (look.layers as u64).rotate_left(40), seed) +} +/// Recently drawn tiles (the most recent last). +type Cache = Mutex)>>; +static TILES: Cache = Mutex::new(Vec::new()); + +/// The tile being drawn in the background for a drag ([`tile`]): one at a time. +static DRAWING: Mutex> = Mutex::new(None); + +/// The tile a drawing hands to the renders waiting for it, set when it is drawn. +type Slot = Arc>>; + +/// The tiles being drawn, by cache and key, and the condition their drawers signal when done: a +/// render that misses a tile another is drawing waits for it rather than drawing it again. The +/// drawer hands the tile over in its slot, so the waiters get it even if the cache has evicted it +/// by the time they wake. +static IN_FLIGHT: Mutex> = Mutex::new(Vec::new()); +static DRAWN: Condvar = Condvar::new(); + +/// Marks a tile drawn (or failed) when dropped and wakes the renders waiting for it. +struct Drawing { + id: (usize, Key), + done: Slot, +} + +impl Drawing { + /// Registers the draw of `id` in `busy` (the locked [`IN_FLIGHT`]). + fn register(busy: &mut Vec<((usize, Key), Slot)>, id: (usize, Key)) -> Drawing { + let done = Slot::default(); + busy.push((id, done.clone())); + Drawing { id, done } + } + + /// Hands `t` to the renders waiting for it (they wake when this is dropped). + fn finish(&self, t: &Arc) { + // (already set: only the first tile counts, and every drawing draws the same one) + let _ = self.done.set(t.clone()); + } +} + +impl Drop for Drawing { + fn drop(&mut self) { + IN_FLIGHT.lock().unwrap_or_else(PoisonError::into_inner).retain(|(d, _)| *d != self.id); + DRAWN.notify_all(); + } +} + +/// Registers the tile of `id` as being drawn, or `None` if another thread is drawing it. +fn start_drawing(id: (usize, Key)) -> Option { + let mut busy = IN_FLIGHT.lock().unwrap_or_else(PoisonError::into_inner); + if busy.iter().any(|(d, _)| *d == id) { + return None; + } + Some(Drawing::register(&mut busy, id)) +} + +/// The tile of `key` in `cache`. +fn cached(cache: &Cache, key: Key) -> Option> { + let tiles = cache.lock().unwrap_or_else(PoisonError::into_inner); + tiles.iter().find(|(k, _)| *k == key).map(|(_, t)| t.clone()) +} + +/// The tile of `look` and `seed`, drawn or from the cache. Unless `wait`, a tile not drawn yet +/// is drawn in the background and the last one drawn for `seed` stands in for it meanwhile: a +/// Size or Roughness drag doesn't wait for every step's tile (tens of milliseconds on a slow +/// CPU), and the full-quality render when it ends waits for the exact one. With no tile cached +/// for `seed` nothing can stand in, so even without `wait` this waits for a draw already running +/// or draws the tile. +fn tile(look: CrystalLook, seed: u32, wait: bool) -> Option> { + tile_in(&TILES, look, seed, wait) +} + +/// [`tile`] with the tiles cached in `cache` (a test's own, which other tests can't evict from). +fn tile_in(cache: &'static Cache, look: CrystalLook, seed: u32, wait: bool) -> Option> { + let key = key_of(look, seed); + { + let tiles = cache.lock().unwrap_or_else(PoisonError::into_inner); + if let Some((_, t)) = tiles.iter().find(|(k, _)| *k == key) { + return Some(t.clone()); + } + // (wasm has no threads to draw on) + if !wait + && !cfg!(target_arch = "wasm32") + && let Some((_, stand_in)) = tiles.iter().rev().find(|(k, _)| k.2 == seed) + { + let stand_in = stand_in.clone(); + drop(tiles); + draw_in_background(cache, key, look, seed); + return Some(stand_in); + } + } + let id = (std::ptr::from_ref(cache) as usize, key); + // a pool worker draws the tile itself: blocked, it could hold up the drawing it waits for + // (its stack may carry a part of that drawing's parallel work); wasm has no other threads + let drawing = if !cfg!(target_arch = "wasm32") && rayon::current_thread_index().is_none() { + // checked, waited for and registered under one lock: two renders that miss the tile + // together can't both find it neither cached nor being drawn (`cached` takes the cache's + // lock under this one; nothing takes them the other way round) + let mut busy = IN_FLIGHT.lock().unwrap_or_else(PoisonError::into_inner); + let mut waited_for: Option = None; + loop { + if let Some(t) = waited_for.as_ref().and_then(|done| done.get().cloned()).or_else(|| cached(cache, key)) { + return Some(t); + } + let Some((_, done)) = busy.iter().find(|(d, _)| *d == id) else { break }; + waited_for = Some(done.clone()); + busy = DRAWN.wait(busy).unwrap_or_else(PoisonError::into_inner); + } + Some(Drawing::register(&mut busy, id)) + } else { + start_drawing(id) + }; + #[cfg(test)] + if let Some(d) = &drawing { + tests::hold_for_waiters(d); + } + let t = Arc::new(CrystalTile::new(look, seed)?); + if let Some(d) = &drawing { + d.finish(&t); + } + keep(cache, key, t.clone()); + #[cfg(test)] + if let Some(d) = &drawing { + tests::evict_before_waking(cache, d); + } + Some(t) +} + +/// Adds `t` to `cache` (the most recent last). +fn keep(cache: &Cache, key: Key, t: Arc) { + let mut tiles = cache.lock().unwrap_or_else(PoisonError::into_inner); + tiles.retain(|(k, _)| *k != key); + if tiles.len() >= CACHE { + tiles.remove(0); + } + tiles.push((key, t)); +} + +/// Draws the tile of `key` on the thread pool unless another is being drawn (the next render +/// asks again for what it needs then). +fn draw_in_background(cache: &'static Cache, key: Key, look: CrystalLook, seed: u32) { + let marker = { + let mut drawing = DRAWING.lock().unwrap_or_else(PoisonError::into_inner); + if drawing.is_some() { + return; + } + // (`None`: a render is drawing it already) + let Some(marker) = start_drawing((std::ptr::from_ref(cache) as usize, key)) else { return }; + *drawing = Some(key); + marker + }; + spawn_in_background(move || { + if let Some(t) = CrystalTile::new(look, seed) { + let t = Arc::new(t); + marker.finish(&t); + keep(cache, key, t); + } + drop(marker); + *DRAWING.lock().unwrap_or_else(PoisonError::into_inner) = None; + }); +} + +/// Runs `draw` on the background pool. +fn spawn_in_background(draw: impl FnOnce() + Send + 'static) { + // on a pool of its own: the renders of the drag don't queue behind its parallel bands + match background_pool() { + Some(pool) => pool.spawn(draw), + None => rayon::spawn(draw), + } +} + +/// Half the cores, for tiles drawn in the background (`None`: the pool couldn't be made). +fn background_pool() -> Option<&'static rayon::ThreadPool> { + static POOL: OnceLock> = OnceLock::new(); + POOL.get_or_init(|| { + let n = std::thread::available_parallelism().map_or(2, |n| n.get()).div_ceil(2); + rayon::ThreadPoolBuilder::new().num_threads(n).thread_name(|i| format!("grain-tile-{i}")).build().ok() + }) + .as_ref() +} + +/// The tone weight of an encoded channel value `v` (0..1): `Y·dV/dY` of the sRGB curve, i.e. how +/// much `v` moves when the channel's linear light is scaled. +#[inline] +pub fn lift(v: f32) -> f32 { + let v = v.clamp(0.0, 1.0); + if v <= 0.040_45 { v } else { (v + 0.055) / 2.4 } +} + +/// The tone weight at mid grey (what [`CrystalGrain::k`] is calibrated at). +fn mid_weight() -> f32 { + lift(MID_GREY) * (1.0 - srgb_to_linear(MID_GREY)) +} + +impl CrystalGrain { + /// The grain of `g` at `px_per_long` output pixels per long edge (`None`: no grain). + pub fn new(g: &Grain, px_per_long: f64) -> Option { + Self::with(g, px_per_long, true) + } + + /// [`Self::new`] for a draft render (a drag): while its tile is drawn, the last tile drawn for + /// the same seed stands in for it (see [`tile`]). + pub fn draft(g: &Grain, px_per_long: f64) -> Option { + Self::with(g, px_per_long, false) + } + + fn with(g: &Grain, px_per_long: f64, wait: bool) -> Option { + let levels = strength_levels(g.amount, g.size, g.roughness); + if !(levels > 0.0 && px_per_long.is_finite() && px_per_long > 0.0) { + return None; + } + let foot = (TILE_PER_LONG / px_per_long) as f32; + if !(foot.is_finite() && foot > 0.0 && foot < MAX_FOOTPRINT) { + return None; + } + let tile = tile(CrystalLook::new(g.size, g.roughness), g.seed, wait)?; + Some(CrystalGrain { tile, k: (levels / 255.0) as f32 / mid_weight(), foot }) + } + + /// Display-encoded colour `e` with grain, at frame position (`gx`, `gy`) (fractions of the + /// frame's long edge): the mean over an axis-aligned square footprint centred there, which + /// for a rotated frame's pixel stands in for its rotated footprint (`docs/grain.md`). + #[inline] + pub fn apply(&self, e: [f32; 3], gx: f32, gy: f32) -> [f32; 3] { + if !(gx.is_finite() && gy.is_finite()) { + return e; + } + let tpl = TILE_PER_LONG as f32; + self.apply_field(e, self.tile.field(gx * tpl, gy * tpl, self.foot)) + } + + /// The grain field of a row of `out.len()` pixels whose first centre is at (`gx`, `gy`) and + /// which step (`dx`, `dy`) (fractions of the long edge), when the step is along x or y (an + /// unrotated frame in any orientation) and the pixels are squares of the render's size + /// (`false` otherwise: use [`Self::apply`] per pixel). + pub fn row(&self, gx: f64, gy: f64, dx: f64, dy: f64, out: &mut [f32]) -> bool { + let tpl = TILE_PER_LONG; + let ((sx, sy), w) = ((dx * tpl, dy * tpl), self.foot as f64); + let (step, along_x) = if sy.abs() <= 1e-6 { + (sx, true) + } else if sx.abs() <= 1e-6 { + (sy, false) + } else { + return false; + }; + if !(gx.is_finite() && gy.is_finite() && step.is_finite()) || (step.abs() - w).abs() > 1e-4 * w { + return false; + } + self.tile.row_field((gx * tpl) as f32, (gy * tpl) as f32, self.foot, step as f32, along_x, out); + true + } + + /// Display-encoded colour `e` with grain field value `g` (see [`CrystalTile::field`]). + #[inline] + pub fn apply_field(&self, e: [f32; 3], g: f32) -> [f32; 3] { + let lum = (0.2126 * e[0] + 0.7152 * e[1] + 0.0722 * e[2]).clamp(0.0, 1.0); + let k = self.k * g * (1.0 - srgb_to_linear(lum)); + if !k.is_finite() { + return e; + } + e.map(|v| v + k * lift(v)) + } +} + +#[cfg(test)] +#[path = "tests_crystal.rs"] +mod tests; diff --git a/crates/pipeline/src/finish.rs b/crates/pipeline/src/finish.rs index 4cb3388127..a4ec7ddec3 100644 --- a/crates/pipeline/src/finish.rs +++ b/crates/pipeline/src/finish.rs @@ -110,6 +110,50 @@ fn vignette(s: &DevelopSettings) -> Option { /// Hash constants of [`grain_noise`] (shared with the GPU kernel). pub const GRAIN_HASH: [u32; 4] = [0x8da6_b343, 0xd816_3841, 0xcb1a_b31f, 0x5bd1_e995]; +/// The grain of processes V1 to V3: two octaves of value noise. Frozen: photos on those +/// processes keep rendering with it (`docs/process-versions.md`). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct ValueGrain { + /// Amplitude (display-encoded). + pub amt: f32, + /// Noise cell size (output pixels). + pub cell: f32, + /// Roughness 0..1: weight of the finer octave. + pub rough: f32, + pub seed: u32, +} + +/// A render's film grain. The model follows the photo's process: changing it would change how +/// existing photos look, so the crystal grain came with process V4. +#[derive(Clone, Debug)] +pub enum GrainModel { + /// V1 to V3: value noise. + Value(ValueGrain), + /// V4 and later: crystal layers ([`crate::crystal`]). + Crystal(crate::crystal::CrystalGrain), +} + +impl GrainModel { + /// The grain `s` renders with at `px_per_long` output pixels per long edge (`None`: none); + /// `draft`: for a drag ([`crate::crystal::CrystalGrain::draft`]). + pub fn new(s: &DevelopSettings, px_per_long: f64, draft: bool) -> Option { + let g = &s.grain; + match s.process.process() { + Process::V1 | Process::V2 | Process::V3 | Process::V4 | Process::V5 | Process::V6 => (g.amount > 0.0).then(|| { + let cell = (0.0006 + (g.size / 100.0) as f32 * 0.0024) * px_per_long as f32; + GrainModel::Value(ValueGrain { + amt: (g.amount / 100.0) as f32 * 0.13, + cell: cell.max(0.6), + rough: (g.roughness / 100.0) as f32, + seed: g.seed, + }) + }), + Process::V7 | Process::V8 if draft => crate::crystal::CrystalGrain::draft(g, px_per_long).map(GrainModel::Crystal), + Process::V7 | Process::V8 => crate::crystal::CrystalGrain::new(g, px_per_long).map(GrainModel::Crystal), + } + } +} + #[inline] fn grain_noise(x: f32, y: f32, seed: u32) -> f32 { // A degenerate frame (zero-width crop) maps pixels to NaN/inf coordinates: no grain there. @@ -132,6 +176,21 @@ fn grain_noise(x: f32, y: f32, seed: u32) -> f32 { a + (b - a) * v } +/// The crystal grain of output row `y` (`w` pixels) when its pixels are axis-aligned; empty +/// otherwise (and without crystal grain): the pixels then take it one by one. +fn crystal_row(fp: &FinishParams, y: usize, w: usize) -> Vec { + let Some(GrainModel::Crystal(g)) = &fp.grain else { return Vec::new() }; + let long = fp.ow.max(fp.oh); + let (a, b) = (fp.out_to_norm.apply(Point::new(0.5, y as f64 + 0.5)), fp.out_to_norm.apply(Point::new(1.5, y as f64 + 0.5))); + let (gx, gy) = (a.x * fp.ow / long, a.y * fp.oh / long); + let (dx, dy) = ((b.x - a.x) * fp.ow / long, (b.y - a.y) * fp.oh / long); + let mut out = vec![0.0; w]; + if !g.row(gx, gy, dx, dy, &mut out) { + out.clear(); + } + out +} + /// Number of per-mask terms in [`mask_terms`]. pub const MASK_TERMS: usize = 21; @@ -217,8 +276,8 @@ pub struct FinishParams { pub air_pre: f32, pub gain: f32, pub ev: f32, - /// Grain: amount, cell size (px), roughness, seed. - pub grain: Option<(f32, f32, f32, u32)>, + /// Film grain: the model of the photo's process ([`GrainModel`]). + pub grain: Option, /// Output px → normalized oriented coordinates. pub out_to_norm: lightcraft_geom::Affine, pub ow: f64, @@ -241,12 +300,14 @@ pub struct FinishParamsInput<'a> { pub px_per_long: f64, pub air_pre: f32, pub space: OutputSpace, + /// A draft render (a drag, [`crate::Quality::Draft`]): see [`GrainModel::new`]. + pub draft: bool, } impl FinishParams { /// Parameters for a `w × h` render into `space`; `ev`/`air_pre` as in [`crate::Prepared`]. pub fn new(input: FinishParamsInput<'_>) -> FinishParams { - let FinishParamsInput { settings: s, frame, info, image_size: (w, h), px_per_long, air_pre, space } = input; + let FinishParamsInput { settings: s, frame, info, image_size: (w, h), px_per_long, air_pre, space, draft } = input; let effects = s.section_enabled("effects"); let (clar, tex, dehaze) = if effects { ((s.effects.clarity / 100.0) as f32, (s.effects.texture / 100.0) as f32, (s.effects.dehaze / 100.0) as f32) @@ -263,10 +324,7 @@ impl FinishParams { && frame.oh.is_finite() && frame.ow.max(frame.oh) > 0.0 && out_to_norm.0.iter().all(|v| v.is_finite()); - let grain = (s.grain.amount > 0.0 && effects && finite_frame).then(|| { - let cell = (0.0006 + (s.grain.size / 100.0) as f32 * 0.0024) * px_per_long as f32; - ((s.grain.amount / 100.0) as f32 * 0.13, cell.max(0.6), (s.grain.roughness / 100.0) as f32, s.grain.seed) - }); + let grain = if effects && finite_frame { GrainModel::new(s, px_per_long, draft) } else { None }; let calibration = s.section_enabled("calibration"); let peak = s.hdr.peak(); FinishParams { @@ -349,7 +407,7 @@ pub fn base_tone(s: &DevelopSettings, info: &SourceInfo, peak: f32) -> ToneMap { }, // V4, with a raw's tone curve rolling off into white in stops: a camera curve past its last // knot, the generic curve from half a stop below its white point instead of clipping (issue #1632) - Process::V5 | Process::V6 => match info.camera_tone.as_ref().filter(|_| info.raw) { + Process::V5 | Process::V6 | Process::V7 | Process::V8 => match info.camera_tone.as_ref().filter(|_| info.raw) { Some(curve) => ToneMap::camera_rolloff_hdr(curve, 0.0, l.whites, 0.0, peak), None if info.raw => ToneMap::rolloff_hdr(l.contrast, l.whites, l.blacks, peak), None => ToneMap::display_hdr(l.contrast, l.whites, l.blacks, peak), @@ -358,18 +416,20 @@ pub fn base_tone(s: &DevelopSettings, info: &SourceInfo, peak: f32) -> ToneMap { } /// Finish the prepared pipeline stages into an 8-bit RGBA image in `space` (or `display`). -pub(crate) fn finish( - p: &Prepared, - s: &DevelopSettings, - frame: &Frame, - info: &SourceInfo, - space: OutputSpace, - display: Option<&crate::DisplaySpace>, - proof: Option, -) -> Rgba8 { +pub(crate) fn finish(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInfo, req: &crate::RenderRequest) -> Rgba8 { + let (space, display, proof) = (req.space, req.display.as_ref(), req.proof); let (w, h) = (p.img.width, p.img.height); - let mut fp = - FinishParams::new(FinishParamsInput { settings: s, frame, info, image_size: (w, h), px_per_long: p.px_per_long, air_pre: p.air, space }); + let draft = req.quality == crate::Quality::Draft; + let mut fp = FinishParams::new(FinishParamsInput { + settings: s, + frame, + info, + image_size: (w, h), + px_per_long: p.px_per_long, + air_pre: p.air, + space, + draft, + }); fp.proof = proof.map(|pr| pr.params(space)); if let Some(d) = display { fp.for_display(d, proof); @@ -389,19 +449,21 @@ pub(crate) fn finish( } /// [`finish`] into 16-bit display-encoded or 32-bit float linear samples (output primaries). -pub(crate) fn finish_deep( - p: &Prepared, - s: &DevelopSettings, - frame: &Frame, - info: &SourceInfo, - space: OutputSpace, - depth: OutputDepth, - proof: Option, -) -> DeepImage { +pub(crate) fn finish_deep(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInfo, req: &crate::RenderRequest) -> DeepImage { use lightcraft_color::transfer::srgb_to_linear; + let (space, depth, proof) = (req.space, req.depth, req.proof); let (w, h) = (p.img.width, p.img.height); - let mut fp = - FinishParams::new(FinishParamsInput { settings: s, frame, info, image_size: (w, h), px_per_long: p.px_per_long, air_pre: p.air, space }); + let draft = req.quality == crate::Quality::Draft; + let mut fp = FinishParams::new(FinishParamsInput { + settings: s, + frame, + info, + image_size: (w, h), + px_per_long: p.px_per_long, + air_pre: p.air, + space, + draft, + }); fp.proof = proof.map(|pr| pr.params(space)); let trc = fp.out_trc; let samples = match depth { @@ -453,6 +515,8 @@ pub(crate) fn finish_with( let hdr = peak > 1.0; let mut out = vec![T::default(); w * h]; for_rows(&mut out, w, |y, row| { + // the crystal grain of the whole row at once when its pixels are axis-aligned + let crystal_row = crystal_row(fp, y, row.len()); for (x, px) in row.iter_mut().enumerate() { let i = y * w + x; let raw = p.img.data[i]; @@ -587,20 +651,31 @@ pub(crate) fn finish_with( e = refine_saturation(e0, e, fp.refine_sat); } } - if let Some((amt, cell, rough, seed)) = *grain { - let n = out_to_norm.apply(Point::new(x as f64 + 0.5, y as f64 + 0.5)); - let (gx, gy) = ((n.x * fp.ow) as f32 / long as f32, (n.y * fp.oh) as f32 / long as f32); - let sc = fp.px_per_long as f32 / cell; - let mut g = grain_noise(gx * sc, gy * sc, seed); - g = g * (1.0 - rough * 0.5) + grain_noise(gx * sc * 2.3, gy * sc * 2.3, seed ^ 0x55) * rough * 0.7; - let mut lum = 0.2126 * e[0] + 0.7152 * e[1] + 0.0722 * e[2]; - if hdr { - lum = lum.clamp(0.0, 1.0); - } - let k = amt * g * (0.35 + 2.6 * lum * (1.0 - lum)); - if k.is_finite() { - e = e.map(|v| v + k); + match grain { + Some(GrainModel::Value(g)) => { + // V1/V2 (frozen): exactly the arithmetic those processes always had + let n = out_to_norm.apply(Point::new(x as f64 + 0.5, y as f64 + 0.5)); + let (gx, gy) = ((n.x * fp.ow) as f32 / long as f32, (n.y * fp.oh) as f32 / long as f32); + let sc = fp.px_per_long as f32 / g.cell; + let mut v = grain_noise(gx * sc, gy * sc, g.seed); + v = v * (1.0 - g.rough * 0.5) + grain_noise(gx * sc * 2.3, gy * sc * 2.3, g.seed ^ 0x55) * g.rough * 0.7; + let mut lum = 0.2126 * e[0] + 0.7152 * e[1] + 0.0722 * e[2]; + if hdr { + lum = lum.clamp(0.0, 1.0); + } + let k = g.amt * v * (0.35 + 2.6 * lum * (1.0 - lum)); + if k.is_finite() { + e = e.map(|v| v + k); + } } + Some(GrainModel::Crystal(g)) => match crystal_row.get(x) { + Some(&f) => e = g.apply_field(e, f), + None => { + let n = out_to_norm.apply(Point::new(x as f64 + 0.5, y as f64 + 0.5)); + e = g.apply(e, (n.x * fp.ow / long) as f32, (n.y * fp.oh / long) as f32); + } + }, + None => {} } let e = match &p_lut { Some((l, k)) if hdr => { diff --git a/crates/pipeline/src/lens_blur.rs b/crates/pipeline/src/lens_blur.rs new file mode 100644 index 0000000000..f518eb5de7 --- /dev/null +++ b/crates/pipeline/src/lens_blur.rs @@ -0,0 +1,1109 @@ +//! Lens Blur: a shallow depth of field added after the fact. +//! +//! Without a depth map, the scene has two depths: what is in focus (the photo's subject, or the +//! selection of the mask [`LensBlur::focus_mask`](lightcraft_develop::LensBlur) names) and +//! everything else. A pixel's circle of confusion is `R · (1 − focus)`, `R` set by Blur Amount +//! relative to the long edge, so previews and exports match. +//! +//! The blur is gathered from [`LEVELS`] disc blurs of growing radius, blended per pixel by its +//! circle of confusion. Each disc blur runs on a grid coarse enough that the disc spans about +//! [`DISC_CELLS`] cells (its cost doesn't grow with the radius) and is a normalized convolution +//! weighted by how far out of focus each pixel is, so the sharp subject doesn't bleed into the +//! blurred background around it (no halo). Boost brightens highlights before they are spread, so +//! they turn into bright discs (bokeh), as a lens does with scene-linear light. +//! +//! Brush Refinement paints the focus by hand: Focus brush strokes stay sharp, Blur brush strokes +//! are blurred whatever the focus says. +//! +//! A mask can blur too (Lens Blur among its local adjustments): there the mask's strength is the +//! circle of confusion, so what it fully selects gets the whole blur, its feathered edge less, and +//! what it leaves out stays sharp and doesn't bleed into the blur. +//! +//! Amount is capped at 3.5% of the output's long edge. The disc convolution runs on a grid whose +//! disc radius is at most [`DISC_CELLS`] cells and whose size is capped by [`MAX_CELLS`] per level, +//! so increasing the output resolution or blur radius does not make the convolution kernel grow. +//! The full-resolution passes remain linear in the output pixel count. +//! +//! It runs on the scene-linear image (white-balanced and denoised, before exposure: a blur is +//! linear, so exposure commutes with it), on the CPU, for both renderers. + +use lightcraft_develop::{BrushStroke, DevelopSettings, Mask, MaskShape}; +use lightcraft_raster::{Plane, Rgb32f}; + +use crate::geometry::Frame; +use crate::masks::{self, Mattes}; + +/// The largest circle of confusion (Blur Amount 100), as a fraction of the long edge. +pub const MAX_RADIUS: f64 = 0.035; +/// How far from the focus the blur reaches its full size at Transition 100, as a fraction of the +/// long edge (a stand-in for depth: what is near the subject in the picture is usually near it in +/// the scene too, as a lens renders a floor or a wall running away from it). +pub const MAX_TRANSITION: f64 = 0.15; +/// The blur right at the focus's edge, as a share of the full blur (with a transition): the +/// background around a subject is a little soft at once, not cut out sharp. +const EDGE_BLUR: f32 = 0.1; +/// The feather (0..100) of a fixed focus shape's edge ([`lightcraft_develop::FocusShape`]). +const SHAPE_FEATHER: f64 = 40.0; +/// Most cells along the long edge of the grid the distance to the focus is measured on. +const DISTANCE_CELLS: usize = 384; +/// Disc blurs the per-pixel circle of confusion is interpolated between. +const LEVELS: usize = 4; +/// Radius, in grid cells, a level's disc blur works at. +const DISC_CELLS: f32 = 8.0; +/// Most cells a level's grid holds (bounds the memory of a full-size export). +const MAX_CELLS: usize = 3_000_000; +/// Weight of in-focus pixels in the blurred levels: enough to keep a level defined where only +/// in-focus pixels are near, too little to bleed them into the background. +const FOCUS_WEIGHT: f32 = 1e-4; +/// The brightest highlights grow this much brighter at Boost 100 before they are spread. +const BOOST_GAIN: f32 = 4.0; + +/// Whether `s` renders a lens blur (switched on, with some amount, and its section's eye open). +pub fn active(s: &DevelopSettings) -> bool { + let b = &s.lens_blur; + b.enabled && b.amount.is_finite() && b.amount > 0.0 && s.section_enabled("lensBlur") +} + +/// Whether [`apply`] changes anything: the global lens blur, or a mask's. +pub fn renders(s: &DevelopSettings) -> bool { + active(s) || blurring_masks(s).next().is_some() +} + +/// The largest circle of confusion (radius in output pixels) at `px_per_long` output pixels per +/// long edge; 0 when [`active`] is false. +pub fn radius_px(s: &DevelopSettings, px_per_long: f64) -> f32 { + if !active(s) { + return 0.0; + } + let r = s.lens_blur.amount.clamp(0.0, 100.0) / 100.0 * MAX_RADIUS * px_per_long; + if r.is_finite() { r.max(0.0) as f32 } else { 0.0 } +} + +/// The masks that blur what they select, or what they leave out (the Lens Blur block among a +/// mask's local adjustments): shown, with components, the block on and some blur. +fn blurring_masks(s: &DevelopSettings) -> impl Iterator { + s.masks + .iter() + .filter(|m| m.visible && !m.components.is_empty() && !m.adjust.lens_blur_off && m.adjust.lens_blur.is_finite() && m.adjust.lens_blur > 0.0) +} + +/// The circle of confusion (radius in output pixels) where mask `m` fully selects. +fn mask_radius_px(m: &Mask, px_per_long: f64) -> f32 { + let r = m.adjust.lens_blur.clamp(0.0, 100.0) / 100.0 * MAX_RADIUS * px_per_long; + if r.is_finite() { r.max(0.0) as f32 } else { 0.0 } +} + +/// The distance from the focus (output pixels) at which the blur is full; 0: a sharp cut. +pub fn transition_px(s: &DevelopSettings, px_per_long: f64) -> f32 { + if !active(s) { + return 0.0; + } + transition_of(s.lens_blur.transition, px_per_long) +} + +/// [`transition_px`] for a Transition slider value `t` (0..100). +fn transition_of(t: f64, px_per_long: f64) -> f32 { + if !t.is_finite() || t <= 0.0 { + return 0.0; + } + let d = t.min(100.0) / 100.0 * MAX_TRANSITION * px_per_long; + if d.is_finite() { d.max(0.0) as f32 } else { 0.0 } +} + +/// A Boost slider value (0..100) as 0..1. +fn boost_of(b: f64) -> f32 { + if b.is_finite() { (b.clamp(0.0, 100.0) / 100.0) as f32 } else { 0.0 } +} + +/// The farthest any lens blur of `s` reads (output pixels): the global one's (its blur, and how far +/// it looks for the focus) or a mask's. +pub fn reach_px(s: &DevelopSettings, px_per_long: f64) -> f32 { + let global = radius_px(s, px_per_long) + transition_px(s, px_per_long); + blurring_masks(s).map(|m| mask_radius_px(m, px_per_long) + transition_of(m.adjust.lens_blur_transition, px_per_long)).fold(global, f32::max) +} + +/// `win` (a window of the `full_w × full_h` output) grown by the blur's reach, so a zoomed view +/// blurs its edges with the pixels around them, as the whole render does (the render is cut back +/// to `win`, see [`crate::Plan::keep`]). +pub fn window_for_reads(s: &DevelopSettings, full_w: usize, full_h: usize, px_per_long: f64, win: crate::PixelWindow) -> crate::PixelWindow { + let r = reach_px(s, px_per_long); + if r <= 0.0 { + return win; + } + let m = (r * 1.25).ceil() as usize + 2; + let (x0, y0) = (win.x.saturating_sub(m), win.y.saturating_sub(m)); + let x1 = win.x.saturating_add(win.w).saturating_add(m).min(full_w.max(1)); + let y1 = win.y.saturating_add(win.h).saturating_add(m).min(full_h.max(1)); + crate::PixelWindow { x: x0, y: y0, w: x1.saturating_sub(x0).max(1), h: y1.saturating_sub(y0).max(1) } +} + +/// What [`apply`] reads from `s`, for render cache keys (empty when no lens blur renders). +pub fn key(s: &DevelopSettings) -> String { + let mut k = String::new(); + if active(s) { + k = match focus_mask(s) { + Some(m) => format!("{:?}|{m:?}", s.lens_blur), + None => format!("{:?}", s.lens_blur), + }; + } + for m in blurring_masks(s) { + k.push_str(&format!("|{m:?}")); + } + // a mask's luminance / colour ranges select on the exposed image + if !k.is_empty() { + k.push_str(&format!("|{}", s.light.exposure.to_bits())); + } + k +} + +/// The mask that says what is in focus, if the settings name one that exists and selects something. +/// An AI mask whose selection is still being computed (Find Subject on a slow machine) doesn't +/// count yet: until it is, the subject estimate stays in focus rather than nothing. +fn focus_mask(s: &DevelopSettings) -> Option<&Mask> { + let id = s.lens_blur.focus_mask?; + s.masks.iter().find(|m| m.id == id && !m.components.is_empty() && !awaiting_ai(m)) +} + +/// Whether every component of `m` is an AI selection (Object / Describe) not computed yet. +pub fn awaiting_ai(m: &Mask) -> bool { + m.components.iter().all(|c| match &c.shape { + MaskShape::Object { seg, detail, .. } | MaskShape::Prompt { seg, detail, .. } => seg.is_none() && detail.is_empty(), + _ => false, + }) +} + +/// Smoothstep: 0 below `e0`, 1 above `e1`, a smooth S-curve between. +#[inline] +fn smooth(e0: f32, e1: f32, x: f32) -> f32 { + let t = ((x - e0) / (e1 - e0)).clamp(0.0, 1.0); + t * t * (3.0 - 2.0 * t) +} + +/// What is in focus, 0..1 per pixel of `img` (scene-linear, before exposure, output resolution). +pub fn focus(img: &Rgb32f, s: &DevelopSettings, frame: &Frame, mattes: Option<&Mattes>) -> Plane { + focus_with(img, s, frame, mattes, None) +} + +/// [`focus`], taking the automatic subject estimate from `subject` (made from the whole photo, +/// see [`subject_estimate`]) when there is one, so a window of the output gets the same focus as +/// the whole render. +fn focus_with(img: &Rgb32f, s: &DevelopSettings, frame: &Frame, mattes: Option<&Mattes>, subject: Option<&SubjectEstimate>) -> Plane { + let (w, h) = (img.width, img.height); + let log_l = img.map(crate::local::log_lum); + let mut a = match (focus_mask(s), s.lens_blur.focus_area(s.crop_center())) { + (Some(m), _) => masks::evaluate_one(m, frame, w, h, img, &log_l, s.light.exposure as f32, mattes), + // a fixed shape: where it was moved to, else the middle of the crop (in photo + // coordinates, so it stays put in a zoomed view) + (None, Some(area)) => { + let shape = MaskShape::Radial { center: area.center, rx: area.rx, ry: area.ry, angle: 0.0, feather: SHAPE_FEATHER, invert: false }; + masks::shape_alpha(&shape, frame, w, h, img, &log_l, 0.0, mattes) + } + (None, None) if let Some(est) = subject => est.sample(frame, w, h), + (None, None) => { + let mut a = masks::shape_alpha(&MaskShape::Subject, frame, w, h, img, &log_l, 0.0, mattes); + // the saliency heuristic gives a flat centre about 0.5: in focus too (person mattes + // are 0 or 1 and stay so) + a.data.iter_mut().for_each(|v| *v = smooth(0.25, 0.55, *v)); + a + } + }; + // Brush Refinement: the Focus brush adds focus, the Blur brush takes it away (last word) + let b = &s.lens_blur; + let brush = |strokes: &[BrushStroke]| { + (!strokes.is_empty()).then(|| masks::shape_alpha(&MaskShape::Brush { strokes: strokes.to_vec() }, frame, w, h, img, &log_l, 0.0, mattes)) + }; + if let Some(f) = brush(&b.focus_strokes) { + a.data.iter_mut().zip(&f.data).for_each(|(v, f)| *v = v.max(*f)); + } + if let Some(bl) = brush(&b.blur_strokes) { + a.data.iter_mut().zip(&bl.data).for_each(|(v, b)| *v *= 1.0 - b.clamp(0.0, 1.0)); + } + a.data.iter_mut().for_each(|v| *v = if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 }); + a +} + +/// Long edge of the small render [`subject_point`] looks at. +const SUBJECT_EDGE: usize = 256; + +/// Long edge of the small render [`subject_estimate`] makes of the whole photo. +const ESTIMATE_EDGE: usize = 384; + +/// The automatic subject estimate (the saliency heuristic, for a photo without a person matte) +/// made once from the whole, uncropped photo at [`ESTIMATE_EDGE`]: every render of the photo, +/// whole or a window of it, samples the same estimate, so a zoomed view keeps the focus the +/// whole render has (the heuristic reads its neighbourhood, which a window cuts off). +#[derive(Debug)] +pub struct SubjectEstimate { + plane: Plane, + frame: Frame, +} + +impl SubjectEstimate { + /// The estimate at each pixel of a `w × h` output of `frame` (bilinear). + fn sample(&self, frame: &Frame, w: usize, h: usize) -> Plane { + let (pw, ph) = (self.plane.width, self.plane.height); + let mut out = Plane::new(w, h); + if pw == 0 || ph == 0 || self.plane.data.len() != pw * ph { + return out; + } + let to_norm = frame.out_to_norm(w, h); + let to_proxy = self.frame.norm_to_out(pw, ph); + let at = |x: usize, y: usize| self.plane.data.get(y.min(ph - 1) * pw + x.min(pw - 1)).copied().unwrap_or(0.0); + for (i, v) in out.data.iter_mut().enumerate() { + let n = to_norm.apply(lightcraft_geom::Point::new((i % w) as f64 + 0.5, (i / w) as f64 + 0.5)); + let p = to_proxy.apply(n); + let (fx, fy) = ((p.x - 0.5).clamp(0.0, (pw - 1) as f64), (p.y - 0.5).clamp(0.0, (ph - 1) as f64)); + if !(fx.is_finite() && fy.is_finite()) { + continue; + } + let (x0, y0) = (fx as usize, fy as usize); + let (tx, ty) = ((fx - x0 as f64) as f32, (fy - y0 as f64) as f32); + let top = at(x0, y0) + (at(x0 + 1, y0) - at(x0, y0)) * tx; + let bot = at(x0, y0 + 1) + (at(x0 + 1, y0 + 1) - at(x0, y0 + 1)) * tx; + *v = top + (bot - top) * ty; + } + out + } +} + +/// The [`SubjectEstimate`] the lens blur of `s` focuses on, when it focuses on the automatic +/// subject estimate (switched on, no focus mask, no fixed shape) of a photo without a person +/// matte; `None` otherwise (the focus then needs no whole-photo data). +pub fn subject_estimate(src: &Rgb32f, s: &DevelopSettings, mattes: Option<&Mattes>) -> Option { + if !active(s) || focus_mask(s).is_some() || s.lens_blur.focus_area(s.crop_center()).is_some() || mattes.is_some_and(Mattes::selects_subject) { + return None; + } + if src.width == 0 || src.height == 0 || src.data.len() != src.width.saturating_mul(src.height) { + return None; + } + let frame = Frame::new(src.width, src.height, s, false); + let (w, h) = frame.fit(ESTIMATE_EDGE, ESTIMATE_EDGE); + if w == 0 || h == 0 { + return None; + } + let img = frame.sample(src, w, h); + let log_l = img.map(crate::local::log_lum); + let mut plane = masks::shape_alpha(&MaskShape::Subject, &frame, w, h, &img, &log_l, 0.0, None); + // the saliency heuristic gives a flat centre about 0.5: in focus too + plane.data.iter_mut().for_each(|v| *v = smooth(0.25, 0.55, *v)); + Some(SubjectEstimate { plane, frame }) +} + +/// Where the photo's subject most likely is, for a photo whose camera recorded no focus point: the +/// peak of the subject estimate (person mattes, else the saliency heuristic) over the whole, +/// uncropped photo, in normalized mask coordinates. `None` for an empty source. +pub fn subject_point(src: &Rgb32f, s: &DevelopSettings, mattes: Option<&Mattes>) -> Option { + if src.width == 0 || src.height == 0 || src.data.len() != src.width.saturating_mul(src.height) { + return None; + } + let frame = Frame::new(src.width, src.height, s, false); + let (w, h) = frame.fit(SUBJECT_EDGE, SUBJECT_EDGE); + if w == 0 || h == 0 { + return None; + } + let img = frame.sample(src, w, h); + let log_l = img.map(crate::local::log_lum); + let a = masks::shape_alpha(&MaskShape::Subject, &frame, w, h, &img, &log_l, 0.0, mattes); + let (i, _) = a.data.iter().enumerate().filter(|(_, v)| v.is_finite()).max_by(|x, y| x.1.total_cmp(y.1))?; + let n = frame.out_to_norm(w, h).apply(lightcraft_geom::Point::new((i % w) as f64 + 0.5, (i / w) as f64 + 0.5)); + (n.x.is_finite() && n.y.is_finite()).then(|| lightcraft_geom::Point::new(n.x.clamp(0.0, 1.0), n.y.clamp(0.0, 1.0))) +} + +/// Blur what is out of focus in `img` (scene-linear, before exposure, at output resolution; +/// `frame` and `px_per_long` describe that output), then what each mask with a Lens Blur +/// adjustment selects (in mask order). Does nothing when no lens blur renders. +pub fn apply(img: &mut Rgb32f, s: &DevelopSettings, frame: &Frame, px_per_long: f64, mattes: Option<&Mattes>) { + apply_with(img, s, frame, px_per_long, mattes, None); +} + +/// [`apply`] with the photo's [`SubjectEstimate`] (renders pass [`crate::Plan::lens_subject`]). +pub fn apply_with( + img: &mut Rgb32f, + s: &DevelopSettings, + frame: &Frame, + px_per_long: f64, + mattes: Option<&Mattes>, + subject: Option<&SubjectEstimate>, +) { + let (w, h) = (img.width, img.height); + if w == 0 || h == 0 || img.data.len() != w.saturating_mul(h) { + return; + } + // a zoomed view's place in the whole output: its coarse grids line up with the whole render's + let at = |v: f64| if v.is_finite() && v > 0.0 { v.round() as usize } else { 0 }; + let origin = frame.view.map_or((0, 0), |v| (at(v.x), at(v.y))); + let r = radius_px(s, px_per_long); + if r >= 0.5 { + let focus = crate::timed("lens blur focus", || focus_with(img, s, frame, mattes, subject)); + if focus.data.len() == img.data.len() { + // the blur grows with the distance from what is in focus, as with depth + let ramp = crate::timed("lens blur transition", || transition_ramp(&focus, transition_px(s, px_per_long), px_per_long, origin)); + let coc: Vec = focus.data.iter().enumerate().map(|(i, f)| (1.0 - f) * ramp.as_ref().map_or(1.0, |r| r.at(i))).collect(); + blur(img, &coc, r, boost_of(s.lens_blur.boost), origin); + } + } + let ev = s.light.exposure as f32; + for m in blurring_masks(s) { + let r = mask_radius_px(m, px_per_long); + if r < 0.5 { + continue; + } + // the mask's strength (its Amount included) is how far out of focus a pixel is; blurring + // the outside, it is how far the mask stays below its Amount + let log_l = img.map(crate::local::log_lum); + let alpha = crate::timed("lens blur mask", || masks::evaluate_one(m, frame, w, h, img, &log_l, ev, mattes)); + if alpha.data.len() != img.data.len() { + continue; + } + let a = &m.adjust; + let top = if a.amount.is_finite() { (a.amount / 100.0).clamp(0.0, 1.0) as f32 } else { 0.0 }; + let mut coc: Vec = alpha + .data + .iter() + .map(|v| { + let v = if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 }; + if a.lens_blur_outside { (top - v).max(0.0) } else { v } + }) + .collect(); + // the blur grows away from what stays sharp, as the global one's does + let sharp = Plane { width: w, height: h, data: coc.iter().map(|c| 1.0 - c).collect() }; + if let Some(ramp) = transition_ramp(&sharp, transition_of(a.lens_blur_transition, px_per_long), px_per_long, origin) { + coc.iter_mut().enumerate().for_each(|(i, c)| *c *= ramp.at(i)); + } + blur(img, &coc, r, boost_of(a.lens_blur_boost), origin); + } +} + +/// How much of the full blur each pixel gets for its distance from the focus: [`EDGE_BLUR`] at +/// the focus's edge, growing smoothly to all of it `d` pixels away. +struct Ramp { + /// Distance from the focus, in grid cells. + dist: Plane, + /// Pixels per cell. + g: usize, + w: usize, + d: f32, + /// Where pixel (0, 0) sits in the grid (the grid follows the whole output's cells). + shift: (usize, usize), +} + +impl Ramp { + /// The share of the full blur pixel `i` (row-major in the window) gets. + fn at(&self, i: usize) -> f32 { + let (x, y) = (i % self.w.max(1) + self.shift.0, i / self.w.max(1) + self.shift.1); + let g = self.g as f32; + // bilinear in the grid, cell centres at +0.5 + let (fx, fy) = (((x as f32 + 0.5) / g - 0.5).max(0.0), ((y as f32 + 0.5) / g - 0.5).max(0.0)); + let (gw, gh) = (self.dist.width, self.dist.height); + let (x0, y0) = ((fx as usize).min(gw - 1), (fy as usize).min(gh - 1)); + let (x1, y1) = ((x0 + 1).min(gw - 1), (y0 + 1).min(gh - 1)); + let (tx, ty) = (fx - x0 as f32, fy - y0 as f32); + let v = |x: usize, y: usize| self.dist.data.get(y * gw + x).copied().unwrap_or(f32::MAX); + let top = v(x0, y0) + (v(x1, y0) - v(x0, y0)) * tx; + let bot = v(x0, y1) + (v(x1, y1) - v(x0, y1)) * tx; + let px = (top + (bot - top) * ty) * g; + EDGE_BLUR + (1.0 - EDGE_BLUR) * smooth(0.0, self.d, px) + } +} + +/// The [`Ramp`] for `focus` over `d` pixels; `None` for a sharp cut (`d` 0) or nothing in focus. +/// Distances are chamfer distances (steps of 1 and √2 cells) on a grid of about +/// [`DISTANCE_CELLS`] along the photo's long edge (`px_per_long`: the cell size follows the +/// photo's scale, not the rendered window's, and its cells are the whole output's: `origin` is +/// where a window's pixel (0, 0) sits in it, so a zoomed view measures as the whole render does): +/// what is in focus are the cells mostly in focus. +fn transition_ramp(focus: &Plane, d: f32, px_per_long: f64, origin: (usize, usize)) -> Option { + let (w, h) = (focus.width, focus.height); + // (a NaN distance is no transition) + if d.is_nan() || d <= 0.5 || w == 0 || h == 0 || focus.data.len() != w * h { + return None; + } + let per_cell = px_per_long / DISTANCE_CELLS as f64; + let g = if per_cell.is_finite() { (per_cell.ceil() as usize).clamp(1, w.max(h)) } else { 1 }; + let shift = (origin.0 % g, origin.1 % g); + // the grid covers the window shifted into the whole output's cells + let (gw, gh) = ((w + shift.0).div_ceil(g), (h + shift.1).div_ceil(g)); + let mut dist = Plane::new(gw, gh); + let mut any = false; + for cy in 0..gh { + for cx in 0..gw { + let (mut sum, mut n) = (0.0f32, 0usize); + for vy in cy * g..((cy + 1) * g).min(h + shift.1) { + for vx in cx * g..((cx + 1) * g).min(w + shift.0) { + let (Some(x), Some(y)) = (vx.checked_sub(shift.0), vy.checked_sub(shift.1)) else { continue }; + sum += focus.data.get(y * w + x).copied().unwrap_or(0.0); + n += 1; + } + } + let inside = n > 0 && sum / n as f32 > 0.5; + any |= inside; + if let Some(c) = dist.data.get_mut(cy * gw + cx) { + *c = if inside { 0.0 } else { f32::MAX }; + } + } + } + if !any { + return None; + } + let diag = std::f32::consts::SQRT_2; + let at = |d: &Plane, x: isize, y: isize| { + if x < 0 || y < 0 || x >= gw as isize || y >= gh as isize { + f32::MAX + } else { + d.data.get(y as usize * gw + x as usize).copied().unwrap_or(f32::MAX) + } + }; + // forward then backward chamfer passes + for y in 0..gh as isize { + for x in 0..gw as isize { + let c = at(&dist, x, y) + .min(at(&dist, x - 1, y) + 1.0) + .min(at(&dist, x, y - 1) + 1.0) + .min(at(&dist, x - 1, y - 1) + diag) + .min(at(&dist, x + 1, y - 1) + diag); + if let Some(v) = dist.data.get_mut(y as usize * gw + x as usize) { + *v = c; + } + } + } + for y in (0..gh as isize).rev() { + for x in (0..gw as isize).rev() { + let c = at(&dist, x, y) + .min(at(&dist, x + 1, y) + 1.0) + .min(at(&dist, x, y + 1) + 1.0) + .min(at(&dist, x + 1, y + 1) + diag) + .min(at(&dist, x - 1, y + 1) + diag); + if let Some(v) = dist.data.get_mut(y as usize * gw + x as usize) { + *v = c; + } + } + } + Some(Ramp { dist, g, w, d, shift }) +} + +/// Blur `img` by a circle of confusion `coc · r` pixels per pixel (`coc` 0..1, one per pixel), +/// highlights brightened by `boost` (0..1) before they are spread. +/// `origin`: where `img`'s pixel (0, 0) sits in the whole output (a zoomed view's window), so the +/// coarse grids line up with the whole render's. +fn blur(img: &mut Rgb32f, coc: &[f32], r: f32, boost: f32, origin: (usize, usize)) { + let w = img.width; + // highlights brightened before they are spread (the bokeh), the rest as it is + let gain = |c: [f32; 3]| 1.0 + BOOST_GAIN * boost * smooth(0.5, 1.0, lightcraft_color::luminance_2020(c)); + let levels: Vec = (1..=LEVELS).map(|k| crate::timed("lens blur level", || level(img, coc, k, r, &gain, origin))).collect(); + // each pixel between the two levels around its circle of confusion (level 0: the image) + let src = img.clone(); + crate::for_rows(&mut img.data, w, |y, row| { + for (x, px) in row.iter_mut().enumerate() { + let i = y * w + x; + let t = coc.get(i).copied().unwrap_or(0.0) * LEVELS as f32; + let k = (t.floor().max(0.0) as usize).min(LEVELS - 1); + let fr = (t - k as f32).clamp(0.0, 1.0); + let at = |k: usize| match k { + 0 => src.data.get(i).copied().unwrap_or(*px), + k => levels.get(k - 1).map_or(*px, |l| l.sample(x, y)), + }; + let (a, b) = (at(k), at(k + 1)); + *px = std::array::from_fn(|c| a[c] + (b[c] - a[c]) * fr); + } + }); +} + +/// One disc blur, on a grid `f` times coarser than the image. +struct Level { + img: Rgb32f, + f: usize, + /// Where image pixel (0, 0) sits on the grid's pixels (the whole output's cells). + shift: (usize, usize), +} + +impl Level { + /// The blurred value at image pixel `(x, y)`. + fn sample(&self, x: usize, y: usize) -> [f32; 3] { + let f = self.f as f32; + self.img.sample_bilinear(((x + self.shift.0) as f32 + 0.5) / f, ((y + self.shift.1) as f32 + 0.5) / f) + } +} + +/// Level `k` of [`LEVELS`] (radius `k / LEVELS · r` pixels): the disc blur of the pixels at least +/// that far out of focus. +fn level(img: &Rgb32f, coc: &[f32], k: usize, r: f32, gain: &(impl Fn([f32; 3]) -> f32 + Sync), origin: (usize, usize)) -> Level { + let (w, h) = (img.width, img.height); + let radius = r * k as f32 / LEVELS as f32; + let (f, shift, lw, lh) = level_grid(w, h, radius, origin); + let (grid_w, grid_h) = (w.saturating_add(shift.0), h.saturating_add(shift.1)); + // a pixel counts in this level when its own circle of confusion reaches it + let (lo, hi) = ((k - 1) as f32 / LEVELS as f32, k as f32 / LEVELS as f32); + let mut cells = vec![[0f32; 4]; lw * lh]; + crate::for_rows(&mut cells, lw, |cy, row| { + for (cx, cell) in row.iter_mut().enumerate() { + let mut acc = [0f32; 4]; + for vy in cy * f..((cy + 1) * f).min(grid_h) { + for vx in cx * f..((cx + 1) * f).min(grid_w) { + let (Some(x), Some(y)) = (vx.checked_sub(shift.0), vy.checked_sub(shift.1)) else { continue }; + let i = y * w + x; + let (Some(c), Some(d)) = (img.data.get(i), coc.get(i)) else { continue }; + // (a non-finite pixel would spread over the whole disc: left out) + if !c.iter().all(|v| v.is_finite()) { + continue; + } + let wt = FOCUS_WEIGHT + smooth(lo, hi, *d); + let g = gain(*c) * wt; + acc = [acc[0] + c[0] * g, acc[1] + c[1] * g, acc[2] + c[2] * g, acc[3] + wt]; + } + } + *cell = acc; + } + }); + Level { img: disc(&cells, lw, lh, radius / f as f32), f, shift } +} + +/// Grid scale and dimensions for one disc level, bounded even when a window's grid is offset. +fn level_grid(w: usize, h: usize, radius: f32, origin: (usize, usize)) -> (usize, (usize, usize), usize, usize) { + let at_least = ((w as f64 * h as f64) / MAX_CELLS as f64).sqrt().ceil() as usize; + let max_edge = w.max(h).max(1); + let mut f = ((radius / DISC_CELLS).ceil() as usize).max(at_least).clamp(1, max_edge); + loop { + let shift = (origin.0 % f, origin.1 % f); + let (lw, lh) = (w.saturating_add(shift.0).div_ceil(f), h.saturating_add(shift.1).div_ceil(f)); + if lw.saturating_mul(lh) <= MAX_CELLS || f == max_edge { + return (f, shift, lw, lh); + } + f = f.saturating_add(1); + } +} + +/// The normalized disc blur of radius `rho` cells of weighted sums `cells` (`[r·w, g·w, b·w, w]`, +/// `lw × lh`): per row of the disc, a span read from that row's prefix sums, its two end cells +/// weighted by the fraction of them inside. +fn disc(cells: &[[f32; 4]], lw: usize, lh: usize, rho: f32) -> Rgb32f { + let stride = lw + 1; + let mut pre = vec![[0f64; 4]; stride * lh]; + crate::for_rows(&mut pre, stride, |y, p| { + let Some(row) = cells.get(y * lw..(y + 1) * lw) else { return }; + let mut acc = [0f64; 4]; + for (x, c) in row.iter().enumerate() { + for ch in 0..4 { + acc[ch] += c[ch] as f64; + } + if let Some(slot) = p.get_mut(x + 1) { + *slot = acc; + } + } + }); + // (a disc past the grid reads nothing more) + let rho = if rho.is_finite() { rho.clamp(0.0, lw.max(lh) as f32 + 1.0) } else { 0.0 }; + let n = rho.ceil() as isize; + let spans: Vec<(isize, isize, f64)> = (-n..=n) + .filter_map(|dy| { + let q = rho * rho - (dy * dy) as f32; + (q >= 0.0).then(|| { + let hw = q.sqrt(); + (dy, hw.floor() as isize, (hw - hw.floor()) as f64) + }) + }) + .collect(); + let mut out = Rgb32f::new(lw, lh); + let (lwi, lhi) = (lw as isize, lh as isize); + crate::for_rows(&mut out.data, lw, |y, row| { + for (x, px) in row.iter_mut().enumerate() { + let x = x as isize; + let mut acc = [0f64; 4]; + for &(dy, hw, fr) in &spans { + let yy = y as isize + dy; + if yy < 0 || yy >= lhi { + continue; + } + let base = yy as usize * stride; + let (a, b) = ((x - hw).max(0), (x + hw + 1).min(lwi)); + if a < b + && let (Some(pa), Some(pb)) = (pre.get(base + a as usize), pre.get(base + b as usize)) + { + for ch in 0..4 { + acc[ch] += pb[ch] - pa[ch]; + } + } + if fr > 0.0 { + for xx in [x - hw - 1, x + hw + 1] { + if xx >= 0 + && xx < lwi + && let Some(c) = cells.get(yy as usize * lw + xx as usize) + { + for ch in 0..4 { + acc[ch] += fr * c[ch] as f64; + } + } + } + } + } + *px = if acc[3] > 0.0 { std::array::from_fn(|c| (acc[c] / acc[3]) as f32) } else { [0.0; 3] }; + } + }); + out +} + +#[cfg(test)] +mod tests { + use super::*; + use lightcraft_develop::{MaskComponent, MaskOp}; + use lightcraft_geom::Point; + + /// Settings with the lens blur switched on at `amount`. + fn on(amount: f64) -> DevelopSettings { + let mut s = DevelopSettings::default(); + s.lens_blur.enabled = true; + s.lens_blur.amount = amount; + s + } + + /// A frame for a `w × h` source rendered at its own size. + fn frame(s: &DevelopSettings, w: usize, h: usize) -> Frame { + Frame::new(w, h, s, true) + } + + /// A radial mask around the centre (radius `r`, long-edge units). + fn centre_mask(id: u32, r: f64) -> Mask { + let mut m = Mask { id, ..Mask::default() }; + m.components.push(MaskComponent { + name: None, + op: MaskOp::Add, + invert: false, + shape: MaskShape::Radial { center: Point::new(0.5, 0.5), rx: r, ry: r, angle: 0.0, feather: 0.0, invert: false }, + }); + m + } + + /// A black image with one bright point at `(px, py)` and white stripes in the left third. + fn scene(w: usize, h: usize, px: usize, py: usize) -> Rgb32f { + let mut img = Rgb32f::from_fn(w, h, |x, _| if x < w / 3 && x % 4 < 2 { [0.6; 3] } else { [0.02; 3] }); + img.set(px, py, [4.0; 3]); + img + } + + /// On a 60 MP export, the strongest global and per-mask settings keep a fixed-size disc kernel + /// and every level's coarse grid within its cell budget, including an offset render window. + #[test] + fn large_image_blur_work_is_bounded() { + let (w, h) = (9500, 6316); + let long_edge = w.max(h) as f64; + let s = on(100.0); + let r = radius_px(&s, long_edge); + assert!((r - (MAX_RADIUS * long_edge) as f32).abs() < 1e-3); + + let mut mask = Mask::default(); + mask.adjust.lens_blur = 100.0; + assert!((mask_radius_px(&mask, long_edge) - r).abs() < 1e-3); + + let mut total_cells = 0usize; + for k in 1..=LEVELS { + let radius = r * k as f32 / LEVELS as f32; + let (f, _, lw, lh) = level_grid(w, h, radius, (17, 29)); + let cells = lw.saturating_mul(lh); + assert!(cells <= MAX_CELLS, "level {k}: {lw} × {lh} = {cells}"); + total_cells = total_cells.saturating_add(cells); + assert!(radius / f as f32 <= DISC_CELLS); + } + assert!(total_cells <= LEVELS * MAX_CELLS); + + let (_, _, lw, lh) = level_grid(w, h, r, (w - 1, h - 1)); + assert!(lw.saturating_mul(lh) <= MAX_CELLS, "offset window: {lw} × {lh}"); + } + + /// Switched off, at Amount 0 or with the section's eye closed, the photo is untouched. + #[test] + fn off_amount_zero_or_section_hidden_changes_nothing() { + let img = scene(64, 48, 50, 10); + for s in [DevelopSettings::default(), on(0.0), { + let mut s = on(80.0); + s.set_section_enabled("lensBlur", false); + s + }] { + let mut out = img.clone(); + apply(&mut out, &s, &frame(&s, 64, 48), 64.0, None); + assert_eq!(out, img); + assert_eq!(key(&s), ""); + } + } + + /// An out-of-focus point of light spreads into a disc of the expected radius and keeps its energy. + #[test] + fn out_of_focus_point_becomes_a_disc_and_keeps_its_energy() { + let (w, h) = (200, 200); + let mut s = on(100.0); + // in focus: a small disc in the middle; the point sits far outside it + s.masks.push(centre_mask(7, 0.05)); + s.lens_blur.focus_mask = Some(7); + let mut img = Rgb32f::filled(w, h, [0.0; 3]); + img.set(40, 40, [100.0; 3]); + let before: f32 = img.data.iter().map(|c| c[1]).sum(); + apply(&mut img, &s, &frame(&s, w, h), w as f64, None); + let r = radius_px(&s, w as f64); + assert!((r - 7.0).abs() < 1e-3, "{r}"); + // spread over a disc: inside it lit, well outside it dark, the centre no longer a spike + let at = |x: usize, y: usize| img.get(x, y)[1]; + assert!(at(40, 40) < 10.0, "{}", at(40, 40)); + for (x, y) in [(44, 40), (40, 36), (37, 43)] { + assert!(at(x, y) > 0.2, "({x}, {y}) {}", at(x, y)); + } + for (x, y) in [(52, 40), (40, 28), (50, 50)] { + assert!(at(x, y) < 0.05, "({x}, {y}) {}", at(x, y)); + } + let after: f32 = img.data.iter().map(|c| c[1]).sum(); + assert!((after - before).abs() / before < 0.15, "{before} → {after}"); + } + + /// What is in focus stays sharp, and its brightness doesn't bleed into the blurred background as a halo. + #[test] + fn the_focus_stays_sharp_and_does_not_bleed_into_the_background() { + let (w, h) = (160, 160); + let mut s = on(100.0); + s.masks.push(centre_mask(3, 0.2)); + s.lens_blur.focus_mask = Some(3); + // a bright subject filling the focus (a disc of 30 px; the focus is 32 px), grey stripes + // around it + let mut img = Rgb32f::from_fn(w, h, |x, y| { + let d = ((x as f32 + 0.5 - 80.0).powi(2) + (y as f32 + 0.5 - 80.0).powi(2)).sqrt(); + if d < 30.0 { + [2.0; 3] + } else if x % 4 < 2 { + [0.3; 3] + } else { + [0.1; 3] + } + }); + let src = img.clone(); + apply(&mut img, &s, &frame(&s, w, h), w as f64, None); + // the subject is untouched + assert_eq!(img.get(80, 80), src.get(80, 80)); + assert_eq!(img.get(60, 70), src.get(60, 70)); + // the background's stripes are smoothed out… + let row: Vec = (4..40).map(|x| img.get(x, 10)[1]).collect(); + let (lo, hi) = row.iter().fold((f32::MAX, f32::MIN), |(a, b), v| (a.min(*v), b.max(*v))); + assert!(hi - lo < 0.04, "{lo}..{hi}"); + // …to their mean, also 5 px from the bright subject (within the blur's reach): no halo + let near = img.get(45, 80)[1]; + assert!((near - 0.2).abs() < 0.04, "{near}"); + } + + /// Boost brightens the blurred highlights only: a mid-grey background stays as it is. + #[test] + fn boost_brightens_bokeh_only() { + let (w, h) = (120, 120); + let mut s = on(60.0); + s.masks.push(centre_mask(1, 0.05)); + s.lens_blur.focus_mask = Some(1); + let mut img = Rgb32f::filled(w, h, [0.1; 3]); + img.set(20, 20, [8.0; 3]); + let (mut plain, mut boosted) = (img.clone(), img.clone()); + apply(&mut plain, &s, &frame(&s, w, h), w as f64, None); + s.lens_blur.boost = 100.0; + apply(&mut boosted, &s, &frame(&s, w, h), w as f64, None); + assert!(boosted.get(22, 20)[0] > plain.get(22, 20)[0] * 2.0, "{} vs {}", boosted.get(22, 20)[0], plain.get(22, 20)[0]); + // a mid-grey background stays as it is + assert!((boosted.get(100, 100)[0] - plain.get(100, 100)[0]).abs() < 1e-4); + } + + /// A small preview and a full-size export blur alike (the radius is relative to the long edge). + #[test] + fn previews_and_exports_blur_alike() { + // the same photo at two sizes: the blur scales with the long edge + let s = on(100.0); + let big = scene(240, 180, 200, 30); + let small = Rgb32f::from_fn(120, 90, |x, y| { + let p = |dx, dy| big.get(2 * x + dx, 2 * y + dy); + let q = [p(0, 0), p(1, 0), p(0, 1), p(1, 1)]; + std::array::from_fn(|c| q.iter().map(|v| v[c]).sum::() / 4.0) + }); + let (mut a, mut b) = (big.clone(), small.clone()); + apply(&mut a, &s, &frame(&s, 240, 180), 240.0, None); + apply(&mut b, &s, &frame(&s, 120, 90), 120.0, None); + let mut diff = 0.0; + for y in 0..90 { + for x in 0..120 { + let d = a.get(2 * x, 2 * y)[1] - b.get(x, y)[1]; + diff += d.abs(); + } + } + let mean = diff / (120.0 * 90.0); + assert!(mean < 0.02, "{mean}"); + } + + /// Brush Refinement: a Focus stroke keeps what it covers sharp, a Blur stroke blurs what it + /// covers even inside the focus. + #[test] + fn focus_and_blur_brushes_refine_the_focus() { + let (w, h) = (160, 120); + let img = Rgb32f::from_fn(w, h, |x, _| if x % 4 < 2 { [0.3; 3] } else { [0.1; 3] }); + let mut s = on(100.0); + // in focus: the middle + s.masks.push(centre_mask(5, 0.15)); + s.lens_blur.focus_mask = Some(5); + let dab = |x: f64, y: f64| BrushStroke { points: vec![Point::new(x, y)], size: 0.08, feather: 0.0, ..BrushStroke::default() }; + // Focus brush on the top-left corner (out of focus otherwise), Blur brush on the centre + s.lens_blur.focus_strokes.push(dab(0.1, 0.12)); + s.lens_blur.blur_strokes.push(dab(0.5, 0.5)); + let mut out = img.clone(); + apply(&mut out, &s, &frame(&s, w, h), w as f64, None); + let stripes = |o: &Rgb32f, cx: usize, cy: usize| { + let v: Vec = (cx - 4..cx + 4).map(|x| o.get(x, cy)[1]).collect(); + v.iter().fold(f32::MIN, |a, b| a.max(*b)) - v.iter().fold(f32::MAX, |a, b| a.min(*b)) + }; + // the corner the Focus brush painted keeps its stripes; one it didn't is smooth + assert!(stripes(&out, 16, 14) > 0.15, "{}", stripes(&out, 16, 14)); + assert!(stripes(&out, 140, 100) < 0.05, "{}", stripes(&out, 140, 100)); + // the centre is in focus, but the Blur brush wins + assert!(stripes(&out, 80, 60) < 0.05, "{}", stripes(&out, 80, 60)); + // without the strokes, the centre stays sharp + let mut plain = s.clone(); + plain.lens_blur.focus_strokes.clear(); + plain.lens_blur.blur_strokes.clear(); + let mut out = img.clone(); + apply(&mut out, &plain, &frame(&plain, w, h), w as f64, None); + assert!(stripes(&out, 80, 60) > 0.15); + assert_ne!(key(&plain), key(&s)); + } + + /// Without a focus mask, a fixed shape keeps a circle, a wide or a tall oval sharp in the + /// middle: a wide one reaches farther sideways than up, a tall one the other way round. + #[test] + fn fixed_focus_shapes() { + use lightcraft_develop::FocusShape; + let (w, h) = (300, 300); + let s0 = on(100.0); + let fr = frame(&s0, w, h); + let img = Rgb32f::filled(w, h, [0.2; 3]); + let alpha = |shape: FocusShape| { + let mut s = s0.clone(); + s.lens_blur.shape = shape; + focus(&img, &s, &fr, None) + }; + // sideways and upwards from the centre, 0.2 of the long edge away + let side = |a: &Plane| a.get(150 + 60, 150); + let up = |a: &Plane| a.get(150, 150 - 60); + let (round, wide, tall) = (alpha(FocusShape::Round), alpha(FocusShape::Wide), alpha(FocusShape::Tall)); + for a in [&round, &wide, &tall] { + assert!(a.get(150, 150) > 0.99, "the middle is in focus"); + assert!(a.get(5, 5) < 0.01, "a corner isn't"); + } + assert!((side(&round) - up(&round)).abs() < 0.05); + assert!(side(&wide) > 0.9 && up(&wide) < 0.1, "{} {}", side(&wide), up(&wide)); + assert!(up(&tall) > 0.9 && side(&tall) < 0.1, "{} {}", side(&tall), up(&tall)); + // moved and resized: the area follows; hostile values are tamed + let mut s = s0.clone(); + s.lens_blur.shape = FocusShape::Round; + s.lens_blur.area = Some(lightcraft_develop::FocusArea { center: Point::new(0.2, 0.2), rx: 0.1, ry: 0.1 }); + let a = focus(&img, &s, &fr, None); + assert!(a.get(60, 60) > 0.99 && a.get(150, 150) < 0.01, "moved to the top left"); + s.lens_blur.area = Some(lightcraft_develop::FocusArea { center: Point::new(f64::NAN, 9e9), rx: f64::INFINITY, ry: -3.0 }); + let a = focus(&img, &s, &fr, None); + assert!(a.data.iter().all(|v| v.is_finite())); + // an unknown shape in the JSON reads as Auto (never an error) + let b: lightcraft_develop::LensBlur = serde_json::from_value(serde_json::json!({"shape": "hexagon"})).unwrap(); + assert_eq!(b.shape, FocusShape::Auto); + } + + /// With a Transition the blur grows with the distance from the focus, as a lens renders what + /// is near the subject: soft right beside it, full far away. At 0 it is a sharp cut. + #[test] + fn the_blur_grows_away_from_the_focus() { + let (w, h) = (400, 200); + // wide stripes: how much of them a blur leaves says how large it is + let img = Rgb32f::from_fn(w, h, |x, _| if (x / 12) % 2 == 0 { [0.3; 3] } else { [0.1; 3] }); + let mut s = on(100.0); + // in focus: a disc left of centre + let mut m = centre_mask(2, 0.08); + if let MaskShape::Radial { center, .. } = &mut m.components[0].shape { + *center = Point::new(0.2, 0.5); + } + s.masks.push(m); + s.lens_blur.focus_mask = Some(2); + // how much detail is left (stripe contrast) around x, on the middle row + let detail = |o: &Rgb32f, x: usize| { + let v: Vec = (x - 12..x + 12).map(|x| o.get(x, 100)[1]).collect(); + v.iter().fold(f32::MIN, |a, b| a.max(*b)) - v.iter().fold(f32::MAX, |a, b| a.min(*b)) + }; + let render = |t: f64| { + let mut s = s.clone(); + s.lens_blur.transition = t; + let mut o = img.clone(); + apply(&mut o, &s, &frame(&s, w, h), w as f64, None); + o + }; + let (soft, cut) = (render(100.0), render(0.0)); + // the focus disc ends at x ≈ 112 (the ramp is 60 px long): just past it, then farther + let (near, mid, far) = (detail(&soft, 130), detail(&soft, 150), detail(&soft, 330)); + assert!(near > mid + 0.02 && mid > far + 0.02, "{near} > {mid} > {far}"); + // a sharp cut blurs right beside the focus as much as far away + let cut_near = detail(&cut, 130); + assert!((cut_near - detail(&cut, 330)).abs() < 0.02 && near > cut_near + 0.05, "{cut_near} vs {near}"); + // the focus itself stays sharp either way + assert!(detail(&soft, 70) > 0.19 && detail(&cut, 70) > 0.19); + } + + /// A focus mask whose AI selection isn't computed yet (Find Subject still running) leaves the + /// subject estimate in focus: the photo is never blurred all over meanwhile. + #[test] + fn a_pending_ai_focus_mask_falls_back_to_the_subject_estimate() { + let (w, h) = (160, 120); + let img = Rgb32f::from_fn(w, h, |x, _| if x % 4 < 2 { [0.3; 3] } else { [0.1; 3] }); + let mut s = on(100.0); + let mut m = Mask { id: 9, ..Mask::default() }; + let pending = MaskShape::Object { hint: vec![], exclude: vec![], seg: None, detail: vec![], edge: 0.0 }; + m.components.push(MaskComponent { name: None, op: MaskOp::Add, invert: false, shape: pending }); + assert!(awaiting_ai(&m)); + s.masks.push(m); + s.lens_blur.focus_mask = Some(9); + let mut subject = s.clone(); + subject.lens_blur.focus_mask = None; + let (mut a, mut b) = (img.clone(), img.clone()); + apply(&mut a, &s, &frame(&s, w, h), w as f64, None); + apply(&mut b, &subject, &frame(&subject, w, h), w as f64, None); + assert_eq!(a, b, "the same as no focus mask"); + // the middle (the estimate's subject) keeps its stripes + let v: Vec = (76..84).map(|x| a.get(x, 60)[1]).collect(); + assert!(v.iter().fold(f32::MIN, |p, q| p.max(*q)) - v.iter().fold(f32::MAX, |p, q| p.min(*q)) > 0.1, "{v:?}"); + } + + /// A mask with Lens Blur blurs what it selects and nothing else; hidden, it blurs nothing. + #[test] + fn a_mask_blurs_only_what_it_selects() { + let (w, h) = (160, 120); + // vertical stripes everywhere + let img = Rgb32f::from_fn(w, h, |x, _| if x % 4 < 2 { [0.3; 3] } else { [0.1; 3] }); + let mut s = DevelopSettings::default(); + // the left half (a linear gradient with a short ramp in the middle) + let mut m = Mask { id: 2, ..Mask::default() }; + m.components.push(MaskComponent { + name: None, + op: MaskOp::Add, + invert: false, + shape: MaskShape::Linear { start: Point::new(0.45, 0.5), end: Point::new(0.55, 0.5) }, + }); + m.adjust.lens_blur = 100.0; + s.masks.push(m); + assert!(renders(&s) && !active(&s) && !key(&s).is_empty()); + let mut out = img.clone(); + apply(&mut out, &s, &frame(&s, w, h), w as f64, None); + // left: the stripes are smoothed to their mean + let left: Vec = (8..50).map(|x| out.get(x, 60)[1]).collect(); + let (lo, hi) = left.iter().fold((f32::MAX, f32::MIN), |(a, b), v| (a.min(*v), b.max(*v))); + assert!(hi - lo < 0.05 && (lo + hi) / 2.0 > 0.15 && (lo + hi) / 2.0 < 0.25, "{lo}..{hi}"); + // right: untouched + for x in 100..150 { + assert_eq!(out.get(x, 60), img.get(x, 60), "x {x}"); + } + // the window grows by the mask's blur + let win = crate::PixelWindow { x: 60, y: 40, w: 20, h: 20 }; + assert!(window_for_reads(&s, w, h, w as f64, win).w > 20); + // hidden or at 0: nothing + for change in [|m: &mut Mask| m.visible = false, |m: &mut Mask| m.adjust.lens_blur = 0.0, |m: &mut Mask| m.adjust.lens_blur_off = true] { + let mut t = s.clone(); + change(&mut t.masks[0]); + let mut out = img.clone(); + apply(&mut out, &t, &frame(&t, w, h), w as f64, None); + assert_eq!(out, img); + assert!(!renders(&t) && key(&t).is_empty()); + } + } + + /// A mask's Lens Blur block can blur what the mask leaves out, and its Transition makes that + /// blur grow away from what stays sharp. + #[test] + fn a_mask_blurs_its_outside_gradually() { + let (w, h) = (320, 120); + let img = Rgb32f::from_fn(w, h, |x, _| if x % 4 < 2 { [0.3; 3] } else { [0.1; 3] }); + let mut s = DevelopSettings::default(); + // the left third selected + let mut m = Mask { id: 2, ..Mask::default() }; + m.components.push(MaskComponent { + name: None, + op: MaskOp::Add, + invert: false, + shape: MaskShape::Linear { start: Point::new(0.30, 0.5), end: Point::new(0.34, 0.5) }, + }); + m.adjust.lens_blur = 100.0; + m.adjust.lens_blur_outside = true; + s.masks.push(m); + // how much of the stripes is left around column x + let spread = |out: &Rgb32f, x: usize| { + let v: Vec = (x..x + 8).map(|x| out.get(x, 60)[1]).collect(); + v.iter().fold(f32::MIN, |a, b| a.max(*b)) - v.iter().fold(f32::MAX, |a, b| a.min(*b)) + }; + let mut sharp_cut = img.clone(); + apply(&mut sharp_cut, &s, &frame(&s, w, h), w as f64, None); + // the selection stays sharp, the rest is blurred + for x in 8..80 { + assert_eq!(sharp_cut.get(x, 60), img.get(x, 60), "x {x}"); + } + assert!(spread(&sharp_cut, 150) < 0.05 && spread(&sharp_cut, 290) < 0.05); + // with a Transition, near the selection keeps more of the stripes than far from it + s.masks[0].adjust.lens_blur_transition = 100.0; + s.masks[0].adjust.lens_blur_boost = 50.0; + let mut gradual = img.clone(); + apply(&mut gradual, &s, &frame(&s, w, h), w as f64, None); + assert!(spread(&gradual, 112) > spread(&sharp_cut, 112) + 0.02, "{} vs {}", spread(&gradual, 112), spread(&sharp_cut, 112)); + assert!(spread(&gradual, 112) > spread(&gradual, 290), "{} vs {}", spread(&gradual, 112), spread(&gradual, 290)); + // the window reads farther with the transition + let win = crate::PixelWindow { x: 200, y: 40, w: 20, h: 20 }; + let mut no_t = s.clone(); + no_t.masks[0].adjust.lens_blur_transition = 0.0; + assert!(window_for_reads(&s, w, h, w as f64, win).w > window_for_reads(&no_t, w, h, w as f64, win).w); + // at Amount 0 the outside isn't blurred either + s.masks[0].adjust.amount = 0.0; + let mut out = img.clone(); + apply(&mut out, &s, &frame(&s, w, h), w as f64, None); + assert_eq!(out, img); + // hostile values + s.masks[0].adjust.amount = f64::NAN; + s.masks[0].adjust.lens_blur_transition = f64::INFINITY; + s.masks[0].adjust.lens_blur_boost = f64::NAN; + let mut out = img.clone(); + apply(&mut out, &s, &frame(&s, w, h), w as f64, None); + assert!(out.data.iter().all(|p| p.iter().all(|v| v.is_finite()))); + } + + /// The subject point lands on a person matte when the photo carries one, and is always inside + /// the photo; an empty source has none. + #[test] + fn subject_point_finds_the_person_matte() { + use crate::masks::{MatteKind, Mattes}; + let s = DevelopSettings::default(); + let src = Rgb32f::filled(300, 200, [0.2; 3]); + // a person in the top-right corner, far from the centre the heuristic favours + let mut person = lightcraft_raster::Image::::new(300, 200); + for y in 10..60 { + for x in 220..290 { + person.set(x, y, 255); + } + } + let mut mattes = Mattes::default(); + mattes.push(MatteKind::Person, person); + let p = subject_point(&src, &s, Some(&mattes)).unwrap(); + assert!(p.x > 0.72 && p.x < 0.97 && p.y > 0.04 && p.y < 0.3, "{p:?}"); + // without mattes: somewhere in the photo (the heuristic's centre-weighted peak) + let p = subject_point(&src, &s, None).unwrap(); + assert!((0.0..=1.0).contains(&p.x) && (0.0..=1.0).contains(&p.y), "{p:?}"); + assert!(subject_point(&Rgb32f::new(0, 0), &s, None).is_none()); + } + + /// Hostile settings and images: no panic, no NaN. + #[test] + fn hostile_input_never_panics() { + for amount in [f64::NAN, f64::INFINITY, -5.0, 1e9] { + let mut s = on(amount); + s.lens_blur.boost = f64::NAN; + s.lens_blur.focus_mask = Some(99); + for (w, h) in [(1, 1), (1, 7), (9, 1), (33, 5)] { + let mut img = Rgb32f::filled(w, h, [0.5, f32::NAN, f32::INFINITY]); + apply(&mut img, &s, &frame(&s, w, h), 1e6, None); + let mut img = Rgb32f::filled(w, h, [0.5; 3]); + apply(&mut img, &s, &frame(&s, w, h), w.max(h) as f64, None); + assert!(img.data.iter().flatten().all(|v| v.is_finite()), "{amount} {w}×{h}"); + } + } + // a mismatched buffer is left alone + let s = on(50.0); + let mut img = Rgb32f { width: 10, height: 10, data: vec![[0.5; 3]; 7] }; + apply(&mut img, &s, &frame(&s, 10, 10), 10.0, None); + assert_eq!(img.data.len(), 7); + } +} diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index d8835f4ef1..7ca709ac3f 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -26,11 +26,13 @@ pub mod auto; pub mod blemish; pub mod colorops; +pub mod crystal; pub mod cull; pub mod curve; pub mod dust; pub mod finish; pub mod geometry; +pub mod lens_blur; pub mod light_v3; // generated (`light_v3_params.rs`), kept in its own compact layout #[rustfmt::skip] @@ -200,6 +202,27 @@ pub fn check_output_size(max_w: usize, max_h: usize) -> Result<(), String> { Ok(()) } +/// Fails when a visible brush mask holds more dabs than its budget ([`masks::MAX_STROKE_DABS`]) at this render's +/// output size: the render would otherwise skip its later strokes and show a wrong mask. +pub fn check_brush_budget(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings, req: &RenderRequest) -> Result<(), String> { + let brush = + |m: &lightcraft_develop::Mask| m.visible && m.components.iter().any(|c| matches!(c.shape, lightcraft_develop::MaskShape::Brush { .. })); + if !s.masks.iter().any(brush) { + return Ok(()); + } + let p = plan(src, info, s, req); + for m in s.masks.iter().filter(|m| brush(m)) { + for c in &m.components { + if let lightcraft_develop::MaskShape::Brush { strokes } = &c.shape + && masks::brush_over_budget(strokes, &p.frame.for_mask(m.on_photo), p.w, p.h) + { + return Err(format!("mask \"{}\": the brush strokes hold more than {} dabs at this size", m.name, masks::MAX_STROKE_DABS)); + } + } + } + Ok(()) +} + impl RenderRequest { pub fn fit(max_w: usize, max_h: usize) -> Self { Self { @@ -443,6 +466,8 @@ pub struct Plan<'a> { pub keep: Option, /// The source's segmentation mattes ([`SourceInfo::mattes`]). pub mattes: Option>, + /// The whole photo's subject estimate a lens blur focuses on ([`lens_blur::subject_estimate`]). + pub lens_subject: Option>, /// The whole frame and its output size (a windowed render's [`Self::frame`] is part of it). pub full: (geometry::Frame, usize, usize), } @@ -486,8 +511,10 @@ pub fn plan<'a>(src: &Rgb32f, info: &SourceInfo, s: &'a DevelopSettings, req: &R let (pw, ph) = (((full_w as f64 * k).round() as usize).max(1), ((full_h as f64 * k).round() as usize).max(1)); fixed_air = Some(local::frame_airlight(src, info, s, &frame, pw, ph)); } - // (spots grow the rendered window of 8-bit renders; a grown window is cut back to the request) - let mut work = if req.depth == OutputDepth::U8 { spots::window_for_reads(s, &frame, full_w, full_h, px_per_long, win) } else { win }; + // (spots grow the rendered window of 8-bit renders, the lens blur that of every + // render; a grown window is cut back to the request) + let work = if req.depth == OutputDepth::U8 { spots::window_for_reads(s, &frame, full_w, full_h, px_per_long, win) } else { win }; + let mut work = lens_blur::window_for_reads(s, full_w, full_h, px_per_long, work); if local::joint_colour_nr(s) && let Some(params) = local::nrc_params(s, detail_scale) { @@ -521,6 +548,8 @@ pub fn plan<'a>(src: &Rgb32f, info: &SourceInfo, s: &'a DevelopSettings, req: &R // and the local tone mapping local_tone::enabled(info, s), src_long, + // and the lens blur (after noise reduction, but a draft must not reuse planes without it) + lens_blur::key(s), )); let lin_key = hash_of(( pre_nr_key, @@ -529,6 +558,7 @@ pub fn plan<'a>(src: &Rgb32f, info: &SourceInfo, s: &'a DevelopSettings, req: &R // the process picks the colour filter (Update to Current Process, undo) local::joint_colour_nr(s), )); + let lens_subject = lens_blur::subject_estimate(src, s, info.mattes.as_deref()).map(Arc::new); let noise_key = hash_of((pre_nr_key, [d.nr_luminance, d.nr_detail].map(f64::to_bits), detail_scale.to_bits())); Plan { settings, @@ -547,6 +577,7 @@ pub fn plan<'a>(src: &Rgb32f, info: &SourceInfo, s: &'a DevelopSettings, req: &R fixed_air, keep, mattes: info.mattes.clone(), + lens_subject, full, } } @@ -584,6 +615,7 @@ pub fn colour_noise(src: &Rgb32f, info: &SourceInfo, p: &Plan<'_>) -> Option, info: &SourceInfo, s: &DevelopSettings, req: &Rende colour_noise = self::colour_noise(src_img, info, &plan).map(|n| (plan.noise_key, n)); } local::denoise(&mut img, s, detail_scale, colour_noise.as_ref().map(|c| &c.1)); + lens_blur::apply_with(&mut img, s, frame, px_per_long, plan.mattes.as_deref(), plan.lens_subject.as_deref()); Arc::new(img) } }; @@ -743,7 +776,7 @@ fn render_impl(src: Src<'_>, info: &SourceInfo, s: &DevelopSettings, req: &Rende c.put(CacheEntry { src: a.clone(), geo, sampled, lin: Some((lin_key, lin)), colour_noise, planes }); } if req.depth != OutputDepth::U8 { - let deep = finish::finish_deep(&prep, s, frame, info, req.space, req.depth, req.proof); + let deep = finish::finish_deep(&prep, s, frame, info, req); let deep = match plan.keep { Some(k) => deep.crop(k.x, k.y, k.w, k.h), None => deep, @@ -753,7 +786,7 @@ fn render_impl(src: Src<'_>, info: &SourceInfo, s: &DevelopSettings, req: &Rende lap("finish (deep)", &mut t); return Rendered { image, histogram, deep: Some(deep) }; } - let image = finish::finish(&prep, s, frame, info, req.space, req.display.as_ref(), req.proof); + let image = finish::finish(&prep, s, frame, info, req); lap("finish", &mut t); let cut = |i: &Rgba8| match plan.keep { Some(k) => i.crop(k.x, k.y, k.w, k.h), @@ -905,6 +938,8 @@ mod tests; #[cfg(test)] mod tests_geometry; #[cfg(test)] +mod tests_grain; +#[cfg(test)] mod tests_hdr; #[cfg(test)] mod tests_legacy; diff --git a/crates/pipeline/src/light_v3.rs b/crates/pipeline/src/light_v3.rs index 394c7c0829..5f3e0f9bf2 100644 --- a/crates/pipeline/src/light_v3.rs +++ b/crates/pipeline/src/light_v3.rs @@ -284,7 +284,7 @@ pub fn applies(s: &DevelopSettings, info: &SourceInfo) -> bool { let camera_look = info.raw && info.camera_tone.is_some(); match s.process.process() { Process::V1 | Process::V2 | Process::V3 => false, - Process::V4 | Process::V5 | Process::V6 => camera_look, + Process::V4 | Process::V5 | Process::V6 | Process::V7 | Process::V8 => camera_look, } } diff --git a/crates/pipeline/src/local.rs b/crates/pipeline/src/local.rs index debb5e8db6..749e10139d 100644 --- a/crates/pipeline/src/local.rs +++ b/crates/pipeline/src/local.rs @@ -357,7 +357,7 @@ fn transpose(p: &Plane) -> Plane { } /// Detail scale of the highlights/shadows base from process V2, as a fraction of the long edge: -/// the base is the edge-aware filter of the [`median_hv`] of log luminance at this radius, so +/// the base is the edge-aware filter of log luminance without its detail at this radius, so /// twigs, lines and fine texture brighter or darker than their surroundings take their /// surroundings' shift (and keep their contrast, as with Exposure) while region boundaries stay /// sharp (no halos). V1's base is the edge-aware filter of log luminance itself (issue #632). @@ -375,17 +375,76 @@ pub fn base_detail_radius(px_per_long: f64) -> usize { pub fn tone_base_detail(s: &DevelopSettings, px_per_long: f64) -> usize { match s.process.process() { Process::V1 => 0, - Process::V2 | Process::V3 | Process::V4 | Process::V5 | Process::V6 => base_detail_radius(px_per_long), + Process::V2 | Process::V3 | Process::V4 | Process::V5 | Process::V6 | Process::V7 | Process::V8 => base_detail_radius(px_per_long), } } /// The highlights/shadows base layer of log luminance `l`: the fast guided filter ([`BASE_EPS`]) -/// of its [`median_hv`] at `detail` px ([`tone_base_detail`]); at 0, of `l` itself (V1). -pub fn tone_base(l: &Plane, sigma: f32, detail: usize) -> Plane { +/// at `detail` px ([`tone_base_detail`]) of its [`detail_free`] plane when `keeps_gaps` +/// ([`base_keeps_gaps`], V8 on), else of its [`median_hv`] (V2 to V7); at 0, of `l` itself (V1). +pub fn tone_base(l: &Plane, sigma: f32, detail: usize, keeps_gaps: bool) -> Plane { if detail == 0 { return guided_fast(l, sigma, BASE_EPS); } - guided_fast(&median_hv(l, detail), sigma, BASE_EPS) + let m = if keeps_gaps { detail_free(l, detail) } else { median_hv(l, detail) }; + guided_fast(&m, sigma, BASE_EPS) +} + +/// Downscale factor of the coarse median in [`detail_free`]: its radius at full size is this many +/// times the fine one's. +pub const BASE_COARSE_STEP: usize = 3; + +/// Log luminance `l` without detail finer than `r` px (the highlights/shadows base of process V8 +/// on takes its edge-aware filter): each value moves toward its [`median_hv`] at `r` only as far as the +/// median of a [`BASE_COARSE_STEP`]× wider neighbourhood agrees (`l` clamped between the two). +/// A twig, line or speck in an even surround has both medians on the surround's side and takes +/// its tone, as #632 wants. The gap between two letter strokes, which a median at `r` fills with the +/// strokes' tone, sits in a wider neighbourhood mostly of the panel's tone: the two medians +/// disagree and the gap keeps its own. The coarse median is taken at `r` of a box-downscaled copy and +/// enlarged bilinearly, like [`guided_fast`]'s coefficients. +pub fn detail_free(l: &Plane, r: usize) -> Plane { + use lightcraft_raster::resample::{Filter, resize}; + if r == 0 || l.data.is_empty() { + return l.clone(); + } + let (w, h) = (l.width, l.height); + let (lw, lh) = (w.div_ceil(BASE_COARSE_STEP).max(1), h.div_ceil(BASE_COARSE_STEP).max(1)); + if (lw, lh) == (w, h) { + // nothing to downscale: both medians are the same + return median_hv(l, r); + } + let lo = median_hv(&resize(l, lw, lh, Filter::Box), r); + // The bilinear enlargement of `lo` (`resize`'s horizontal pass, then its vertical pass taken + // row by row with the same sums) is clamped into `out` as it is made, not stored. + let wide = resize(&lo, w, lh, Filter::Bilinear); + let wy = lightcraft_raster::resample::weights(lh, h, Filter::Bilinear); + let mut out = median_hv(l, r); + for_rows(&mut out.data, w, |y, row| { + let Some((y0, ws)) = wy.get(y) else { return }; + let mut coarse = vec![0.0f32; w]; + for (k, &wt) in ws.iter().enumerate() { + let Some(t) = wide.data.get((y0 + k) * w..(y0 + k + 1) * w) else { continue }; + for (c, &v) in coarse.iter_mut().zip(t) { + *c += v * wt; + } + } + for ((o, &v), &c) in row.iter_mut().zip(l.row(y)).zip(&coarse) { + *o = clamp_between(v, *o, c); + } + }); + out +} + +/// `v` clamped between `a` and `b` (in either order); a NaN bound is ignored, not a panic. +fn clamp_between(v: f32, a: f32, b: f32) -> f32 { + let (lo, hi) = (a.min(b), a.max(b)); + if v < lo { + lo + } else if v > hi { + hi + } else { + v + } } /// Subsampling step of [`guided_fast`] for `sigma` (1 = no subsampling: the plain guided filter). @@ -638,6 +697,12 @@ pub fn sharpen_key(s: &DevelopSettings, sigma: f32) -> SharpenKey { (sigma.to_bits(), s.process.process()) } +/// Whether `s`'s highlights/shadows base takes [`detail_free`] (process V8 on) instead of V2's plain +/// [`median_hv`] (V2 to V7, which shipped with it and stay frozen). +pub fn base_keeps_gaps(s: &DevelopSettings) -> bool { + s.process.process() >= Process::V8 +} + pub fn pixel_sharpen(s: &DevelopSettings) -> bool { s.process.process() >= Process::V3 } @@ -677,7 +742,7 @@ pub const CHROMA_SIGMA: f32 = 0.004; #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct PlaneSigmas { /// Edge-aware base for highlights/shadows ([`tone_base`]: fast guided filter, [`BASE_EPS`]; from - /// process V2 of the [`BASE_DETAIL`] median). + /// process V2 of a median at [`BASE_DETAIL`], from V8 of a detail-free plane). pub base: Option, /// Median radius (px) of that base ([`tone_base_detail`]: 0 under V1). pub base_detail: usize, @@ -806,7 +871,7 @@ pub(crate) fn prepare( }); let l = &log_l; let (base, (clarity_blur, ((texture_blur, sharpen_blur), dark))) = par_join( - || base_sigma.map(|sg| plane_at(sb, base_key(s, sg, base_detail), || timed("base", || tone_base(l, sg, base_detail)))), + || base_sigma.map(|sg| plane_at(sb, base_key(s, sg, base_detail), || timed("base", || tone_base(l, sg, base_detail, base_keeps_gaps(s))))), || { par_join( || clarity_sigma.map(|sg| plane_at(sc, sg.to_bits(), || timed("clarity", || guided_fast(l, sg, CLARITY_EPS)))), @@ -1309,6 +1374,50 @@ mod tests { mod fast_tests { use super::*; + /// [`detail_free`] enlarges the coarse median as it clamps; the result is the plain + /// composition of `resize` and [`median_hv`], bit for bit. + #[test] + fn detail_free_matches_its_reference() { + use lightcraft_raster::resample::{Filter, resize}; + for (w, h) in [(1, 1), (2, 1), (3, 3), (4, 7), (61, 47), (200, 31)] { + let p = Plane::from_fn(w, h, |x, y| ((x * 37 + y * 101) % 23) as f32 * 0.4 - 4.0 + if (x / 5 + y / 7) % 2 == 0 { 2.0 } else { 0.0 }); + for r in [1, 2, 5, 10] { + let fine = median_hv(&p, r); + let (lw, lh) = (w.div_ceil(BASE_COARSE_STEP), h.div_ceil(BASE_COARSE_STEP)); + let coarse = resize(&median_hv(&resize(&p, lw, lh, Filter::Box), r), w, h, Filter::Bilinear); + let want: Vec = (0..w * h).map(|i| clamp_between(p.data[i], fine.data[i], coarse.data[i]).to_bits()).collect(); + let got: Vec = detail_free(&p, r).data.iter().map(|v| v.to_bits()).collect(); + assert_eq!(got, want, "{w}×{h} r={r}"); + } + } + } + + /// CPU highlights/shadows base timings on the #3460 frame size: + /// `cargo test --release -p lightcraft-pipeline bench_tone_base_cpu -- --ignored --nocapture`. + #[test] + #[ignore] + fn bench_tone_base_cpu() { + let (w, h) = (6960, 4640); + let r = base_detail_radius(w as f64); + let p = Plane::from_fn(w, h, |x, y| ((x * 7 + y * 13) % 1000) as f32 / 250.0 - 6.0 + if (x / 40 + y / 40) % 2 == 0 { 1.0 } else { 0.0 }); + let sigma = 0.015 * w as f32; + let time = |name: &str, f: &dyn Fn() -> Plane| { + let mut ts: Vec = (0..5) + .map(|_| { + let t = std::time::Instant::now(); + std::hint::black_box(f()); + t.elapsed().as_secs_f64() * 1e3 + }) + .collect(); + ts.sort_by(f64::total_cmp); + eprintln!("{name:<20} min {:.1} ms median {:.1} ms", ts[0], ts[2]); + }; + time("median_hv", &|| median_hv(&p, r)); + time("detail_free", &|| detail_free(&p, r)); + time("guided_fast", &|| guided_fast(&p, sigma, BASE_EPS)); + time("tone_base", &|| tone_base(&p, sigma, r, true)); + } + /// The previous fast path, including its two materialized coefficient resizes. fn resized_reference(p: &Plane, sigma: f32, eps: f32) -> Plane { use lightcraft_raster::resample::{Filter, resize}; @@ -1353,12 +1462,12 @@ mod fast_tests { } #[test] - fn sampled_tone_base_keeps_the_median_before_the_guided_filter() { + fn sampled_tone_base_keeps_the_detail_free_plane_before_the_guided_filter() { let p = Plane::from_fn(73, 51, |x, y| if x % 9 == 0 { 3.0 } else { -4.0 + ((x + y) % 7) as f32 * 0.03 }); for sigma in [6.0, 24.0, 120.0] { for detail in [0, 1, 3, BASE_DETAIL_MAX] { - let median = median_hv(&p, detail); - assert_eq!(tone_base(&p, sigma, detail).data, resized_reference(&median, sigma, BASE_EPS).data, "{sigma}, {detail}"); + let free = detail_free(&p, detail); + assert_eq!(tone_base(&p, sigma, detail, true).data, resized_reference(&free, sigma, BASE_EPS).data, "{sigma}, {detail}"); } } } diff --git a/crates/pipeline/src/masks.rs b/crates/pipeline/src/masks.rs index b40e3dcd97..84bbdadde1 100644 --- a/crates/pipeline/src/masks.rs +++ b/crates/pipeline/src/masks.rs @@ -93,6 +93,12 @@ impl Mattes { self.mattes.iter().filter(|(k, _)| kinds.contains(k)).map(|(_, m)| m).collect() } + /// Whether a mask of the photo's Subject comes from these mattes (a person, or a person's + /// parts) rather than from the saliency heuristic. + pub fn selects_subject(&self) -> bool { + !self.for_shape(&MaskShape::Subject).is_empty() + } + /// The mattes a shape uses instead of its heuristic (empty: none fits). fn for_shape(&self, shape: &MaskShape) -> Vec<&Image> { use MatteKind::*; @@ -813,6 +819,31 @@ fn guided_cross_fast(guide: &Plane, p: &mut Plane, sigma: f32, eps: f32, weight: }); } +/// Whether the strokes of one brush mask hold more dabs than the mask's shared budget ([`MAX_STROKE_DABS`]), so that +/// rasterizing it would leave later strokes (an erase among them) unapplied. Callers that can fail check this and +/// report an error rather than render a wrong mask. +pub fn brush_over_budget(strokes: &[BrushStroke], frame: &Frame, w: usize, h: usize) -> bool { + let mut room = MAX_STROKE_DABS; + for s in strokes { + if s.points.is_empty() { + continue; + } + if room == 0 { + return true; + } + let n = brush_dabs_within(s, frame, w, h, room).dabs.len(); + if n >= room { + // the budget is exactly filled, or the stroke was cut off at it: one more dab says which (a stroke + // already at its own cap [`MAX_STROKE_DABS`] is cut off, whatever it would have held) + if room >= MAX_STROKE_DABS || brush_dabs_within(s, frame, w, h, room + 1).dabs.len() > room { + return true; + } + } + room -= n; + } + false +} + /// Stamp brush strokes into `out` (max-combine; erase strokes subtract). Auto Mask strokes weight /// each dab by [`auto_similarity`] to the pixel under its centre and are refined with /// [`guided_cross`] on log luminance. @@ -820,8 +851,15 @@ fn rasterize_brush(strokes: &[BrushStroke], frame: &Frame, w: usize, h: usize, i if w == 0 || h == 0 { return; } + // one dab budget for the whole mask, as the GPU path keeps: a stroke's own cap alone let a + // thousand far-flung two-point strokes build and stamp a billion dabs + let mut room = MAX_STROKE_DABS; for s in strokes { - let bd = brush_dabs(s, frame, w, h); + if room == 0 { + break; + } + let bd = brush_dabs_within(s, frame, w, h, room); + room = room.saturating_sub(bd.dabs.len()); let (dens, erase, auto) = (bd.density, bd.erase, bd.auto); let mut stroke_alpha = Plane::new(w, h); for (k, d) in bd.dabs.iter().enumerate() { diff --git a/crates/pipeline/src/masks/tests.rs b/crates/pipeline/src/masks/tests.rs index b57f9f6688..8b96e92696 100644 --- a/crates/pipeline/src/masks/tests.rs +++ b/crates/pipeline/src/masks/tests.rs @@ -630,6 +630,51 @@ fn people_and_subject_use_person_mattes() { assert_eq!(eval(&lips), shape_alpha(&lips, &f, w, h, &img, &l, 0.0, None)); } +/// A thousand two-point strokes, each a sweep far past the photo that lays most of a stroke's +/// own budget: the mask shares one budget across its strokes, so it renders in moments rather +/// than building and visiting most of a billion dabs. +#[test] +fn many_far_flung_strokes_share_one_dab_budget() { + let (w, h) = (64, 64); + let s = BrushStroke { points: vec![Point::new(-4000.0, 0.5), Point::new(4000.0, 0.5)], size: 0.05, ..Default::default() }; + assert!(brush_dabs(&s, &frame(w, h), w, h).dabs.len() > 500_000, "each stroke alone is large"); + let strokes = vec![s; 1000]; + let t = std::time::Instant::now(); + let mut out = Plane::new(w, h); + rasterize_brush(&strokes, &frame(w, h), w, h, &Rgb32f::new(w, h), &Plane::new(w, h), &mut out); + assert!(t.elapsed().as_secs() < 30, "took {:?}", t.elapsed()); + assert!(out.data.iter().any(|&v| v > 0.0), "the strokes crossing the photo still paint it"); +} + +/// Past the shared dab budget a brush mask is an error for the caller, not a mask missing its later (erase) strokes. +#[test] +fn an_over_budget_brush_mask_is_reported() { + let (w, h) = (64, 64); + let far = BrushStroke { points: vec![Point::new(-4000.0, 0.5), Point::new(4000.0, 0.5)], size: 0.05, ..Default::default() }; + let near = BrushStroke { points: vec![Point::new(0.1, 0.1), Point::new(0.9, 0.9)], size: 0.05, ..Default::default() }; + assert!(!brush_over_budget(std::slice::from_ref(&near), &frame(w, h), w, h)); + assert!(brush_over_budget(&vec![far; 10], &frame(w, h), w, h)); +} + +/// Strokes that fill the budget exactly are not over it; one more dab is. +#[test] +fn a_brush_mask_that_exactly_fills_the_budget_is_not_over_it() { + let (w, h) = (64, 64); + let stroke = |x: f64| BrushStroke { points: vec![Point::new(0.0, 0.5), Point::new(x, 0.5)], size: 0.05, ..Default::default() }; + // a stroke whose dab count divides the budget evenly + let (s, m) = (0..14_000) + .map(|i| stroke(6.0 + f64::from(i) * 0.001)) + .map(|s| { + let m = brush_dabs(&s, &frame(w, h), w, h).dabs.len(); + (s, m) + }) + .find(|(_, m)| *m >= 500 && MAX_STROKE_DABS.is_multiple_of(*m)) + .expect("a stroke whose dabs divide the budget"); + let k = MAX_STROKE_DABS / m; + assert!(!brush_over_budget(&vec![s.clone(); k], &frame(w, h), w, h), "{k} strokes of {m} dabs fill the budget exactly"); + assert!(brush_over_budget(&vec![s; k + 1], &frame(w, h), w, h), "one more stroke is over it"); +} + #[test] fn intersect_first_component_selects_its_shape() { let f = frame(100, 100); diff --git a/crates/pipeline/src/optics.rs b/crates/pipeline/src/optics.rs index 883be6730f..e8dc284c36 100644 --- a/crates/pipeline/src/optics.rs +++ b/crates/pipeline/src/optics.rs @@ -681,31 +681,55 @@ pub fn defringe(img: &mut Rgb32f, s: &DevelopSettings, scale: f64) { }); } -/// Separable square min (or max) filter of radius `r`. +/// Separable square min (or max) filter of radius `r`, linear in the pixels whatever `r` is (a +/// crop can make the radius as wide as the output, and a scan of every neighbourhood was then +/// cubic in the side). fn min_filter(p: &Plane, r: usize, max: bool) -> Plane { let (w, h) = (p.width, p.height); - let pick = |a: f32, b: f32| if max { a.max(b) } else { a.min(b) }; let mut tmp = Plane::new(w, h); - for_rows(&mut tmp.data, w, |y, row| { - let src = &p.data[y * w..(y + 1) * w]; - for (x, v) in row.iter_mut().enumerate() { - let (a, b) = (x.saturating_sub(r), (x + r).min(w - 1)); - *v = src[a..=b].iter().copied().fold(src[x], pick); - } - }); + for_rows(&mut tmp.data, w, |y, row| extreme_1d(&p.data[y * w..(y + 1) * w], r, max, row)); + // the vertical pass runs along the rows of the transposed plane + let mut t = vec![0f32; w * h]; + for (i, v) in tmp.data.iter().enumerate() { + t[(i % w) * h + i / w] = *v; + } + // each buffer goes once the next is built, so no more than three planes are alive at once + drop(tmp); + let mut tt = vec![0f32; w * h]; + for_rows(&mut tt, h, |x, col| extreme_1d(&t[x * h..(x + 1) * h], r, max, col)); + drop(t); let mut out = Plane::new(w, h); - for_rows(&mut out.data, w, |y, row| { - let (a, b) = (y.saturating_sub(r), (y + r).min(h - 1)); - for (x, v) in row.iter_mut().enumerate() { - let mut m = tmp.data[y * w + x]; - for yy in a..=b { - m = pick(m, tmp.data[yy * w + x]); - } - *v = m; - } - }); + for (i, v) in tt.iter().enumerate() { + out.data[(i % h) * w + i / h] = *v; + } out } +/// `out[x]` = the min (or max) of `src[x - r ..= x + r]` clipped to the slice, by the van Herk / +/// Gil–Werman blocks: three passes over the line however wide the window. +fn extreme_1d(src: &[f32], r: usize, max: bool, out: &mut [f32]) { + let n = src.len(); + if n == 0 { + return; + } + // past the line's length a wider window takes in nothing more + let r = r.min(n); + let id = if max { f32::NEG_INFINITY } else { f32::INFINITY }; + let pick = |a: f32, b: f32| if max { a.max(b) } else { a.min(b) }; + let (k, m) = (2 * r + 1, n + 2 * r); + let pad = |i: usize| if i < r || i >= r + n { id } else { src[i - r] }; + let mut g = vec![id; m]; + let mut hh = vec![id; m]; + for i in 0..m { + g[i] = if i % k == 0 { pad(i) } else { pick(g[i - 1], pad(i)) }; + } + for i in (0..m).rev() { + hh[i] = if i % k == k - 1 || i == m - 1 { pad(i) } else { pick(hh[i + 1], pad(i)) }; + } + for (x, o) in out.iter_mut().enumerate().take(n) { + *o = pick(hh[x], g[x + k - 1]); + } +} + #[cfg(test)] mod tests; diff --git a/crates/pipeline/src/optics/tests.rs b/crates/pipeline/src/optics/tests.rs index a404222e82..28518981ea 100644 --- a/crates/pipeline/src/optics/tests.rs +++ b/crates/pipeline/src/optics/tests.rs @@ -475,3 +475,34 @@ fn defringe_amounts_past_the_sliders_act_as_their_end() { assert_eq!(at(1e300, 1.0).data, at(20.0, 1.0).data); assert_eq!(at(f64::MAX, 1e300).data.len(), 800, "a huge scale too"); } + +#[test] +fn the_min_max_filter_matches_a_brute_force_scan_and_is_linear_in_its_radius() { + let (w, h) = (37, 23); + let mut p = Plane::new(w, h); + for (i, v) in p.data.iter_mut().enumerate() { + *v = ((i * 7919) % 101) as f32 / 101.0; + } + for r in [0, 1, 2, 5, 22, 40] { + for max in [false, true] { + let got = min_filter(&p, r, max); + for y in 0..h { + for x in 0..w { + let mut m = p.data[y * w + x]; + for yy in y.saturating_sub(r)..=(y + r).min(h - 1) { + for xx in x.saturating_sub(r)..=(x + r).min(w - 1) { + let v = p.data[yy * w + xx]; + m = if max { m.max(v) } else { m.min(v) }; + } + } + assert_eq!(got.data[y * w + x], m, "r {r} max {max} at {x},{y}"); + } + } + } + } + // a radius as wide as a large output: was ~4 L³ comparisons + let big = Plane::new(2000, 2000); + let t = std::time::Instant::now(); + let _ = min_filter(&big, 2000, true); + assert!(t.elapsed().as_secs() < 20, "took {:?}", t.elapsed()); +} diff --git a/crates/pipeline/src/output.rs b/crates/pipeline/src/output.rs index b514c25503..c3fa176eb6 100644 --- a/crates/pipeline/src/output.rs +++ b/crates/pipeline/src/output.rs @@ -346,6 +346,30 @@ pub struct DeepImage { } impl DeepImage { + /// The `w × h` part of the image at (`x`, `y`), clamped to the image (at least 1 × 1 when the + /// image has pixels). Samples are RGB, three per pixel, row-major. + pub fn crop(&self, x: usize, y: usize, w: usize, h: usize) -> DeepImage { + let x = x.min(self.width.saturating_sub(1)); + let y = y.min(self.height.saturating_sub(1)); + let w = w.min(self.width.saturating_sub(x)).max(usize::from(self.width > 0)); + let h = h.min(self.height.saturating_sub(y)).max(usize::from(self.height > 0)); + fn cut(v: &[T], width: usize, x: usize, y: usize, w: usize, h: usize) -> Vec { + let mut out = Vec::with_capacity(w.saturating_mul(h).saturating_mul(3)); + for row in y..y + h { + let start = row.saturating_mul(width).saturating_add(x).saturating_mul(3); + if let Some(r) = v.get(start..start.saturating_add(w * 3)) { + out.extend_from_slice(r); + } + } + out + } + let samples = match &self.samples { + DeepSamples::U16(v) => DeepSamples::U16(cut(v, self.width, x, y, w, h)), + DeepSamples::F32(v) => DeepSamples::F32(cut(v, self.width, x, y, w, h)), + }; + DeepImage { width: w, height: h, space: self.space, samples } + } + /// The same image reduced to 8 bits, display-encoded with the space's curve. pub fn to_rgba8(&self) -> lightcraft_raster::Rgba8 { let mut out = lightcraft_raster::Rgba8::new(self.width, self.height); @@ -364,27 +388,6 @@ impl DeepImage { } out } - - /// Crop to `[x0, x0+w) × [y0, y0+h)` (clamped to the image). - pub fn crop(&self, x0: usize, y0: usize, w: usize, h: usize) -> Self { - let x0 = x0.min(self.width); - let y0 = y0.min(self.height); - let w = w.min(self.width - x0); - let h = h.min(self.height - y0); - fn rows(v: &[T], width: usize, x0: usize, y0: usize, w: usize, h: usize) -> Vec { - let mut out = Vec::with_capacity(3 * w * h); - for y in y0..y0 + h { - let at = 3 * (y * width + x0); - out.extend_from_slice(v.get(at..at + 3 * w).unwrap_or_default()); - } - out - } - let samples = match &self.samples { - DeepSamples::U16(v) => DeepSamples::U16(rows(v, self.width, x0, y0, w, h)), - DeepSamples::F32(v) => DeepSamples::F32(rows(v, self.width, x0, y0, w, h)), - }; - Self { width: w, height: h, space: self.space, samples } - } } trait IntoRgba { @@ -401,6 +404,23 @@ impl IntoRgba for [u8; 3] { mod tests { use super::*; + /// A crop keeps the right pixels and never reads past the image, whatever the request. + #[test] + fn deep_crop_keeps_the_window_and_clamps() { + // 4 × 3, pixel (x, y) holds x + 10 y in every channel + let v: Vec = (0..3).flat_map(|y| (0..4).flat_map(move |x| [x + 10 * y; 3])).collect(); + let d = DeepImage { width: 4, height: 3, space: OutputSpace::Srgb, samples: DeepSamples::U16(v) }; + let c = d.crop(1, 1, 2, 2); + assert_eq!((c.width, c.height), (2, 2)); + assert_eq!(c.samples, DeepSamples::U16(vec![11, 11, 11, 12, 12, 12, 21, 21, 21, 22, 22, 22])); + for (x, y, w, h) in [(9, 9, 5, 5), (0, 0, usize::MAX, usize::MAX), (3, 2, 0, 0)] { + let c = d.crop(x, y, w, h); + let DeepSamples::U16(s) = &c.samples else { panic!("a U16 image crops to U16") }; + assert!(c.width >= 1 && c.height >= 1 && c.width <= 4 && c.height <= 3); + assert_eq!(s.len(), c.width * c.height * 3, "{x} {y} {w} {h}"); + } + } + #[test] fn trcs_invert() { for o in OutputSpace::ALL { diff --git a/crates/pipeline/src/tests_crystal.rs b/crates/pipeline/src/tests_crystal.rs new file mode 100644 index 0000000000..b784f219a8 --- /dev/null +++ b/crates/pipeline/src/tests_crystal.rs @@ -0,0 +1,661 @@ +use super::*; + +/// The draws [`hold_for_waiters`] holds, by cache: how many renders wait for each, and how many +/// draws are left to hold. +static HOLDS: Mutex> = Mutex::new(Vec::new()); + +/// Holds the draw of `d` until the renders [`HOLDS`] names for its cache are waiting for it (each +/// holds a clone of its slot, as do the drawing and its in-flight entry), so a test can make +/// renders miss a tile together. +pub(super) fn hold_for_waiters(d: &Drawing) { + let waiters = { + let mut holds = HOLDS.lock().unwrap_or_else(PoisonError::into_inner); + let Some(h) = holds.iter_mut().find(|h| h.0 == d.id.0 && h.2 > 0) else { return }; + h.2 -= 1; + h.1 + }; + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20); + while Arc::strong_count(&d.done) < 2 + waiters { + assert!(std::time::Instant::now() < deadline, "the renders never waited"); + std::thread::sleep(std::time::Duration::from_millis(1)); + } +} + +/// The caches [`evict_before_waking`] empties. +static EVICTS: Mutex> = Mutex::new(Vec::new()); + +/// Empties `cache` if [`EVICTS`] names it, after its drawer `d` has cached its tile and before it +/// wakes the renders waiting for it, as other renders' tiles can push it out in that gap. +pub(super) fn evict_before_waking(cache: &Cache, d: &Drawing) { + if EVICTS.lock().unwrap_or_else(PoisonError::into_inner).contains(&d.id.0) { + cache.lock().unwrap_or_else(PoisonError::into_inner).clear(); + } +} + +/// Waits until `n` renders wait for the draw of `d`. +fn until_waiting(d: &Drawing, n: usize) { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20); + while Arc::strong_count(&d.done) < 2 + n { + assert!(std::time::Instant::now() < deadline, "the renders never waited"); + std::thread::sleep(std::time::Duration::from_millis(1)); + } +} + +/// The field rendered at `long` px per long edge on an `n × n` patch starting at pixel `origin`. +fn patch(tile: &CrystalTile, long: f64, n: usize, origin: usize) -> Vec { + let w = (TILE_PER_LONG / long) as f32; + (0..n * n).map(|k| tile.field(((origin + k % n) as f32 + 0.5) * w, ((origin + k / n) as f32 + 0.5) * w, w)).collect() +} + +fn std(v: &[f32]) -> f64 { + let m = v.iter().map(|&x| x as f64).sum::() / v.len() as f64; + (v.iter().map(|&x| (x as f64 - m).powi(2)).sum::() / v.len() as f64).sqrt() +} + +/// Lag (px) at which the autocorrelation (mean of horizontal and vertical) falls to ½. +fn half_width(v: &[f32], n: usize) -> f64 { + let m = v.iter().map(|&x| x as f64).sum::() / v.len() as f64; + let d: Vec = v.iter().map(|&x| x as f64 - m).collect(); + let var = d.iter().map(|x| x * x).sum::() / d.len() as f64; + let corr = |k: usize, horizontal: bool| { + let mut s = 0.0; + let mut c = 0usize; + for y in 0..n - if horizontal { 0 } else { k } { + for x in 0..n - if horizontal { k } else { 0 } { + let b = if horizontal { y * n + x + k } else { (y + k) * n + x }; + s += d[y * n + x] * d[b]; + c += 1; + } + } + s / c as f64 / var + }; + let mut out = 0.0; + for horizontal in [true, false] { + let mut prev = 1.0; + let mut w = 16.0; + for k in 1..16 { + let c = corr(k, horizontal); + if c <= 0.5 { + w = k as f64 - 1.0 + (prev - 0.5) / (prev - c).max(1e-9); + break; + } + prev = c; + } + out += w / 2.0; + } + out +} + +fn tile_of(size: f64, roughness: f64) -> Arc { + tile(CrystalLook::new(size, roughness), 7, true).expect("a tile") +} + +#[test] +fn reference_render_has_unit_variance_and_no_mean() { + let t = tile_of(25.0, 50.0); + let v = patch(&t, REF_LONG, 600, 37); + let m = v.iter().map(|&x| x as f64).sum::() / v.len() as f64; + assert!((std(&v) - 1.0).abs() < 0.05, "std {}", std(&v)); + assert!(m.abs() < 0.03, "mean {m}"); +} + +#[test] +fn small_render_is_the_downscaled_reference() { + for (size, rough) in [(0.0, 50.0), (25.0, 50.0), (100.0, 50.0), (25.0, 0.0), (25.0, 100.0)] { + let t = tile_of(size, rough); + for k in [2usize, 4, 8] { + let full = patch(&t, REF_LONG, 120 * k, 0); + let n = 120; + let down: Vec = (0..n * n) + .map(|q| { + let (x, y) = (q % n, q / n); + let mut s = 0.0; + for dy in 0..k { + for dx in 0..k { + s += full[(y * k + dy) * n * k + x * k + dx]; + } + } + s / (k * k) as f32 + }) + .collect(); + let small = patch(&t, REF_LONG / k as f64, n, 0); + let worst = down.iter().zip(&small).map(|(a, b)| (a - b).abs()).fold(0.0, f32::max); + assert!(worst < 2e-3, "size {size} roughness {rough} 1/{k}: differs by {worst}"); + } + } +} + +#[test] +fn strength_matches_the_target_at_mid_grey() { + for (a, s, r) in [(50.0, 25.0, 50.0), (25.0, 25.0, 50.0), (50.0, 0.0, 50.0), (50.0, 100.0, 50.0), (50.0, 25.0, 0.0)] { + let g = CrystalGrain::new(&Grain { amount: a, size: s, roughness: r, seed: 3 }, REF_LONG).expect("grain"); + let n = 500; + let lum: Vec = (0..n * n) + .map(|k| { + let (x, y) = ((k % n) as f32 + 0.5, (k / n) as f32 + 0.5); + let e = g.apply([MID_GREY; 3], x / REF_LONG as f32, y / REF_LONG as f32); + (0.2126 * e[0] + 0.7152 * e[1] + 0.0722 * e[2]) * 255.0 + }) + .collect(); + let want = strength_levels(a, s, r); + assert!((std(&lum) / want - 1.0).abs() < 0.06, "{a}/{s}/{r}: {} levels, want {want}", std(&lum)); + } +} + +#[test] +fn tones_keep_their_mean() { + // the strongest settings in the shadows; near white, strong grain clips at 255 like any grain + let cases = [ + (100.0, 0.0, &[5.0f32, 20.0, 40.0, 60.0, 128.0, 255.0][..]), + (50.0, 0.0, &[200.0, 245.0][..]), + (50.0, 25.0, &[20.0, 128.0, 245.0, 255.0][..]), + ]; + for (amount, size, levels) in cases { + let g = CrystalGrain::new(&Grain { amount, size, roughness: 50.0, seed: 3 }, REF_LONG).expect("grain"); + for &level in levels { + let n = 400; + let mut sum = 0.0f64; + for k in 0..n * n { + let (x, y) = ((k % n) as f32 + 0.5, (k / n) as f32 + 0.5); + let e = g.apply([level / 255.0; 3], x / REF_LONG as f32, y / REF_LONG as f32); + sum += (e[1].clamp(0.0, 1.0) * 255.0) as f64; + } + let shift = sum / (n * n) as f64 - level as f64; + assert!(shift.abs() < 0.5, "{amount}/{size}: level {level} moved {shift:+.2}"); + } + } +} + +#[test] +fn hostile_settings_render_finite_or_no_grain() { + let bad = [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, -1e9, 1e9, 0.0]; + for &a in &bad { + for &s in &bad { + for &ppl in &[f64::NAN, 0.0, -5.0, 1.0, 24.0, 1e12, 6000.0] { + let g = Grain { amount: if a == 0.0 { 50.0 } else { a }, size: s, roughness: s, seed: u32::MAX }; + if let Some(c) = CrystalGrain::new(&g, ppl) { + for (x, y) in [(0.0, 0.0), (-3.5, 1e7), (f32::NAN, 0.2), (0.5, f32::INFINITY), (1.0, 0.6667)] { + let e = c.apply([0.5, 0.2, 0.9], x, y); + assert!(e.iter().all(|v| v.is_finite()), "{g:?} at {ppl}: {e:?}"); + } + } + } + } + } +} + +#[test] +fn seed_picks_the_pattern() { + let look = CrystalLook::new(25.0, 50.0); + let (Some(a), Some(c)) = (tile(look, 1, true), tile(look, 2, true)) else { panic!("tiles") }; + assert_ne!(patch(&a, REF_LONG, 64, 0), patch(&c, REF_LONG, 64, 0)); +} + +/// A tile drawn again (its cache entry evicted, another process, an export after a preview) is the +/// same to the bit, however many threads draw it. These draw directly: through [`tile`], the second +/// would be the first one out of the cache. +#[test] +fn a_tile_draws_the_same_every_time() { + let look = CrystalLook::new(25.0, 50.0); + let one_thread = rayon::ThreadPoolBuilder::new().num_threads(1).build().expect("a pool"); + let (Some(a), Some(b)) = (CrystalTile::new(look, 1), one_thread.install(|| CrystalTile::new(look, 1))) else { panic!("tiles") }; + assert!(a.sats == b.sats, "the same seed drew different tiles"); +} + +/// Every seed has the same mix of crystal sizes, dealt to the layers in its own order. +#[test] +fn seeds_share_the_mix_of_crystal_sizes() { + let look = CrystalLook::new(50.0, 80.0); + let radii = |seed| emulsion(seed, &look).iter().map(|l| l.r).collect::>(); + let sorted = |mut v: Vec| { + v.sort_by(f64::total_cmp); + v + }; + let (a, b) = (radii(1), radii(2)); + assert_eq!(a.len(), look.layers); + assert_eq!(a, radii(1), "the same seed deals the same order"); + assert_ne!(a, b, "another seed deals another order"); + assert_eq!(sorted(a), sorted(b)); + assert_eq!(emulsion(1, &CrystalLook { layers: 0, ..look }).len(), 1, "an emulsion has a layer"); +} + +#[test] +fn sliders_clamp_and_fall_back() { + let look = CrystalLook::new; + assert_eq!(look(f64::NAN, f64::NAN), look(25.0, 50.0), "NaN is the default"); + assert_eq!(look(-1e9, f64::NEG_INFINITY), look(0.0, 0.0)); + assert_eq!(look(1e9, f64::INFINITY), look(100.0, 100.0)); + // the ends of the tables + assert_eq!((look(0.0, 50.0).width, look(0.0, 50.0).layers), (0.55, 16)); + assert_eq!((look(100.0, 50.0).width, look(100.0, 50.0).layers), (12.38, 70)); + assert_eq!(look(100.0, 0.0).width, 12.38 * 0.734); + assert_eq!(look(100.0, 100.0).width, 12.38 * 1.422); + assert!(look(25.0, 100.0).spread > look(25.0, 0.0).spread); + + let default = strength_levels(50.0, 25.0, 50.0); + assert_eq!(strength_levels(f64::NAN, 25.0, 50.0), 0.0, "a NaN Amount is no grain"); + assert_eq!(strength_levels(-5.0, 25.0, 50.0), 0.0); + assert_eq!(strength_levels(50.0, f64::NAN, f64::NAN), default); + assert_eq!(strength_levels(1e9, 25.0, 50.0), 2.0 * default); + assert_eq!(strength_levels(50.0, -1e9, 50.0), strength_levels(50.0, 0.0, 50.0)); + assert_eq!(strength_levels(50.0, 25.0, f64::INFINITY), strength_levels(50.0, 25.0, 100.0)); + // Lightroom Classic's at the ends of each slider (`docs/grain.md`) + for (want, (a, s, r)) in [(23.08, (50.0, 0.0, 50.0)), (5.83, (50.0, 100.0, 50.0)), (12.42, (50.0, 25.0, 0.0)), (11.68, (50.0, 25.0, 100.0))] { + let got = strength_levels(a, s, r); + assert!((got / want - 1.0).abs() < 0.01, "{a}/{s}/{r}: {got}, want {want}"); + } +} + +#[test] +fn interp_is_linear_between_knots_and_constant_outside() { + let knots = [(0.0, 1.0), (10.0, 3.0), (20.0, 2.0)]; + for (x, want) in [(-5.0, 1.0), (0.0, 1.0), (5.0, 2.0), (10.0, 3.0), (15.0, 2.5), (20.0, 2.0), (99.0, 2.0)] { + assert_eq!(interp(&knots, x), want, "at {x}"); + } + assert_eq!(interp(&[], 5.0), 0.0); + assert_eq!(interp(&[(3.0, 7.0)], 5.0), 7.0); +} + +#[test] +fn normal_quantile_matches_the_table() { + for (p, z) in [(0.5, 0.0), (0.975, 1.959_963_985), (0.841_344_746, 1.0), (0.001, -3.090_232_306), (1e-6, -4.753_424_309)] { + assert!((normal_quantile(p) - z).abs() < 1e-7, "at {p}: {}, want {z}", normal_quantile(p)); + assert!((normal_quantile(1.0 - p) + z).abs() < 1e-7, "at 1 - {p}"); + } + // increasing across the approximation's joins (0.02425 and its mirror), finite at the ends + let sweep: Vec = (0..=1000).map(|k| normal_quantile(k as f64 / 1000.0)).collect(); + assert!(sweep.iter().all(|z| z.is_finite())); + assert!(sweep.windows(2).all(|w| w[0] < w[1])); +} + +#[test] +fn poisson_counts_have_the_mean_and_variance_of_mu() { + let n = 40_000u32; + for mu in [0.5, 4.0, 40.0, 150.0] { + let cdf = poisson_cdf(mu); + assert!(cdf.windows(2).all(|w| w[0] <= w[1]), "mu {mu}: the distribution must not fall"); + assert!(cdf.last().is_some_and(|&c| c >= 1.0 - 1e-12), "mu {mu}: the distribution must reach 1"); + let draws: Vec = (0..n).map(|i| poisson(&cdf, mix(i)) as f64).collect(); + let mean = draws.iter().sum::() / n as f64; + let var = draws.iter().map(|d| (d - mean).powi(2)).sum::() / n as f64; + assert!((mean - mu).abs() < 4.0 * (mu / n as f64).sqrt(), "mu {mu}: mean {mean}"); + assert!((var / mu - 1.0).abs() < 0.05, "mu {mu}: variance {var}"); + } +} + +#[test] +fn poisson_stays_in_range_at_the_ends() { + for h in [0, u32::MAX] { + assert_eq!(poisson(&poisson_cdf(0.0), h), 0, "mean 0 is never a crystal"); + // past what the model uses, e^-mu underflows: a capped count, not a panic + assert!(poisson(&poisson_cdf(1e6), h) <= 512); + assert_eq!(poisson(&[], h), 0); + } +} + +#[test] +fn summed_area_matches_brute_force() { + let t = 7; + let count: Vec = (0..t * t).map(|i| (mix(i as u32) % 71) as u8).collect(); + let sat = summed_area(&count, t); + assert_eq!(sat.len(), (t + 1) * (t + 1)); + for y in 0..=t { + for x in 0..=t { + let want: u32 = (0..y).flat_map(|j| (0..x).map(move |i| (i, j))).map(|(i, j)| count[j * t + i] as u32).sum(); + assert_eq!(sat[y * (t + 1) + x], want, "[0, {x}) × [0, {y})"); + } + } +} + +/// Boxes against the emulsion's own pixels (each table's counts, less their mean, scaled to unit +/// variance as [`CrystalTile::new`] does), integrated by hand. Whole and fractional boxes, across +/// the torus's seams, at negative coordinates and periods away. +#[test] +fn box_sum_is_the_integral_of_the_emulsion() { + let (look, seed) = (CrystalLook::new(25.0, 50.0), 7); + let tile = CrystalTile::new(look, seed).expect("a tile"); + let counts = [0, 1].map(|which| draw_torus(which, TORI[which], seed, &look)); + let means: [f64; 2] = [0, 1].map(|w| counts[w].iter().map(|&c| c as f64).sum::() / (TORI[w] * TORI[w]) as f64); + let var: f64 = [0, 1] + .map(|w| { + let (sq, n) = reference_square_sum(&counts[w], TORI[w], means[w]); + sq / n as f64 + }) + .iter() + .sum(); + let k = 1.0 / var.sqrt(); + // The overlap of the cell at `i` with `[a, a + w]`. + let overlap = |i: i64, a: f64, w: f64| ((i + 1) as f64).min(a + w) - (i as f64).max(a); + let integral_by_hand = |which: usize, x0: f32, y0: f32, w: f32| { + let (x0, y0, w, t) = (x0 as f64, y0 as f64, w as f64, TORI[which] as i64); + let mut sum = 0.0; + for j in y0.floor() as i64..(y0 + w).ceil() as i64 { + for i in x0.floor() as i64..(x0 + w).ceil() as i64 { + let c = counts[which][(j.rem_euclid(t) * t + i.rem_euclid(t)) as usize] as f64; + sum += (c - means[which]) * k * overlap(i, x0, w) * overlap(j, y0, w); + } + } + sum + }; + for which in [0, 1] { + let t = TORI[which] as f32; + for (x0, y0, w) in [ + (0.0, 0.0, 2.0), + (100.0, 200.0, 37.0), + (3.25, 7.5, 2.0), + (10.6, 11.3, 0.7), + (50.5, 60.25, 400.0), + (t - 1.5, 20.0, 4.0), + (20.0, t - 1.5, 4.0), + (t - 0.5, t - 0.25, 31.0), + (-1.5, -2.25, 8.0), + (3.0 * t + 12.5, -2.0 * t + 40.5, 8.0), + ] { + let (got, want) = (tile.box_sum(which, x0, y0, w) as f64, integral_by_hand(which, x0, y0, w)); + let area = (w * w) as f64; + assert!((got - want).abs() / area < 2e-3, "torus {which}, ({x0}, {y0}) side {w}: {got} vs {want}"); + } + } + // the field is the two tori's mean, centred on its position + let (x, y, w) = (640.25, 1133.5, 3.0); + let want = (integral_by_hand(0, x - 0.5 * w, y - 0.5 * w, w) + integral_by_hand(1, x - 0.5 * w, y - 0.5 * w, w)) / (w * w) as f64; + assert!((tile.field(x, y, w) as f64 - want).abs() < 2e-3, "field {} vs {want}", tile.field(x, y, w)); +} + +/// A render under the footprint limit gets grain, one at or past it none, whatever else it asks. +#[test] +fn only_a_render_past_the_footprint_limit_loses_its_grain() { + let g = Grain { amount: 50.0, size: 25.0, roughness: 50.0, seed: 3 }; + let limit = TILE_PER_LONG / MAX_FOOTPRINT as f64; + assert_eq!(limit, 24.0); + for none in [limit, limit - 1.0, 1.0, 0.0, -5.0, f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { + assert!(CrystalGrain::new(&g, none).is_none(), "{none} px per long edge"); + } + assert_eq!(CrystalGrain::new(&g, limit + 1.0).expect("grain").foot, 480.0); + assert_eq!(CrystalGrain::new(&g, REF_LONG).expect("grain").foot, 2.0); + assert!(CrystalGrain::new(&Grain { amount: 0.0, ..g }, REF_LONG).is_none(), "no grain at Amount 0"); +} + +/// A draft (a Size or Roughness drag) doesn't wait for a new tile: the last tile of the seed stands +/// in while the new one is drawn in the background, and then the drag gets it. A render that +/// waits always gets the exact tile, as does a draft for a seed with no tile yet. +#[test] +fn a_drag_takes_the_last_tile_while_the_next_is_drawn() { + // a cache of its own: other tests' tiles (through any V7 render) would evict from the shared one + static OWN: Cache = Mutex::new(Vec::new()); + let tile = |look, seed, wait| tile_in(&OWN, look, seed, wait); + let (seed, fresh) = (0x0d4a_0001, 0x0d4a_0002); + let (a, b) = (CrystalLook::new(30.0, 50.0), CrystalLook::new(31.0, 50.0)); + let exact = |look: CrystalLook, seed: u32| patch(&CrystalTile::new(look, seed).expect("tile"), REF_LONG, 32, 0); + let first = tile(a, seed, true).expect("tile"); + assert_eq!(first.look, a); + let stand_in = tile(b, seed, false).expect("tile"); + assert_eq!(stand_in.look, a, "the draft waited for its tile"); + // the drag's next renders get the new tile once it is drawn + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20); + let drawn = loop { + let t = tile(b, seed, false).expect("tile"); + if t.look == b { + break t; + } + assert!(std::time::Instant::now() < deadline, "the background tile never arrived"); + std::thread::sleep(std::time::Duration::from_millis(5)); + }; + assert_eq!(patch(&drawn, REF_LONG, 32, 0), exact(b, seed)); + assert_eq!(patch(&tile(b, seed, true).expect("tile"), REF_LONG, 32, 0), exact(b, seed)); + // nothing drawn for this seed: nothing to stand in, the draft draws it + assert_eq!(patch(&tile(a, fresh, false).expect("tile"), REF_LONG, 32, 0), exact(a, fresh)); +} + +/// A render that misses a tile another thread is drawing waits for that tile rather than drawing +/// it a second time. +#[test] +fn a_render_waits_for_a_tile_being_drawn() { + static OWN: Cache = Mutex::new(Vec::new()); + let (look, seed) = (CrystalLook::new(40.0, 50.0), 0x0d4a_0003); + let key = key_of(look, seed); + let marker = start_drawing((std::ptr::from_ref(&OWN) as usize, key)).expect("not drawn yet"); + let render = std::thread::spawn(move || tile_in(&OWN, look, seed, true)); + until_waiting(&marker, 1); + let drawn = Arc::new(CrystalTile::new(look, seed).expect("tile")); + keep(&OWN, key, drawn.clone()); + drop(marker); + let got = render.join().expect("render thread").expect("tile"); + assert!(Arc::ptr_eq(&got, &drawn), "the render drew the tile again"); +} + +/// A render waiting for a tile gets it from its drawer even when the cache evicted it before the +/// render woke. +#[test] +fn a_waiting_render_gets_the_tile_the_cache_evicted() { + static OWN: Cache = Mutex::new(Vec::new()); + let (look, seed) = (CrystalLook::new(40.0, 50.0), 0x0d4a_0004); + let key = key_of(look, seed); + let marker = start_drawing((std::ptr::from_ref(&OWN) as usize, key)).expect("not drawn yet"); + let render = std::thread::spawn(move || tile_in(&OWN, look, seed, true)); + until_waiting(&marker, 1); + let drawn = Arc::new(CrystalTile::new(look, seed).expect("tile")); + marker.finish(&drawn); + keep(&OWN, key, drawn.clone()); + // other renders' tiles push it out of the cache before the waiter wakes + // (each its own tile: a waiter that took another key's would fail the check below) + for other in 1..=CACHE as u32 { + keep(&OWN, key_of(look, seed + other), Arc::new(CrystalTile::new(look, seed + other).expect("tile"))); + } + assert!(cached(&OWN, key).is_none(), "the tile is still cached"); + drop(marker); + let got = render.join().expect("render thread").expect("tile"); + assert!(Arc::ptr_eq(&got, &drawn), "the render drew the tile again"); +} + +/// A render waiting for a tile another render is drawing gets the drawer's tile even when the +/// cache drops it before the waiter wakes. +#[test] +fn a_render_drawing_a_tile_hands_it_to_its_waiter() { + static OWN: Cache = Mutex::new(Vec::new()); + let id = std::ptr::from_ref(&OWN) as usize; + // the first render's draw (only: a second would be the waiter drawing it again) waits for the + // second render to wait for it, and the cache empties before the second wakes + HOLDS.lock().unwrap_or_else(PoisonError::into_inner).push((id, 1, 1)); + EVICTS.lock().unwrap_or_else(PoisonError::into_inner).push(id); + let (look, seed) = (CrystalLook::new(0.0, 50.0), 0x0d4a_0005); + let renders: Vec<_> = (0..2).map(|_| std::thread::spawn(move || tile_in(&OWN, look, seed, true))).collect(); + let got: Vec<_> = renders.into_iter().map(|r| r.join().expect("render thread").expect("tile")).collect(); + assert!(cached(&OWN, key_of(look, seed)).is_none(), "the cache kept the tile"); + assert!(Arc::ptr_eq(&got[0], &got[1]), "the waiter drew the tile again"); +} + +/// Renders that miss the same tile together draw it once: the first registers the draw and the +/// others wait for it, as no gap lets a second render find it neither cached nor being drawn. +/// The first draw is held until the other renders all wait for it, so the misses overlap. +#[test] +fn renders_that_miss_a_tile_together_draw_it_once() { + static OWN: Cache = Mutex::new(Vec::new()); + const N: usize = 8; + // the first render's draw waits for the other seven to miss the tile too + const SEEDS: u32 = 40; + HOLDS.lock().unwrap_or_else(PoisonError::into_inner).push((std::ptr::from_ref(&OWN) as usize, N - 1, SEEDS as usize)); + let look = CrystalLook::new(0.0, 50.0); + for seed in 0x0d4a_1000..0x0d4a_1000 + SEEDS { + let start = Arc::new(std::sync::Barrier::new(N)); + let renders: Vec<_> = (0..N) + .map(|_| { + let start = start.clone(); + std::thread::spawn(move || { + start.wait(); + tile_in(&OWN, look, seed, true) + }) + }) + .collect(); + let got: Vec<_> = renders.into_iter().map(|r| r.join().expect("render thread").expect("tile")).collect(); + assert!(got.iter().all(|t| Arc::ptr_eq(t, &got[0])), "seed {seed:#x}: a render drew the tile again"); + } +} + +/// Grain width (autocorrelation half-width at mid grey, 6000 px) at Lightroom Classic's +/// measured settings (open PR #1060's table); Size 0 is as fine as uncorrelated pixels get (0.5). +#[test] +fn width_matches_lightroom_classic() { + let lrc = [(0.0, 50.0, 0.5), (25.0, 50.0, 1.13), (100.0, 50.0, 3.38), (25.0, 0.0, 0.86), (25.0, 100.0, 1.54)]; + for (s, r, want) in lrc { + let got = half_width(&patch(&tile_of(s, r), REF_LONG, 360, 0), 360); + assert!((got / want - 1.0).abs() < 0.1, "size {s} roughness {r}: width {got:.3}, want {want}"); + } +} + +/// Prints how long drawing a tile takes per setting (`--ignored --nocapture`, alone). +#[test] +#[ignore] +fn tile_timing_report() { + for (s, r) in [(0.0, 50.0), (10.0, 50.0), (25.0, 50.0), (50.0, 50.0), (100.0, 50.0), (25.0, 0.0), (25.0, 100.0), (100.0, 100.0)] { + let look = CrystalLook::new(s, r); + let mut best = f64::INFINITY; + for seed in 0..5 { + let start = std::time::Instant::now(); + let t = CrystalTile::new(look, 100 + seed); + best = best.min(start.elapsed().as_secs_f64() * 1e3); + assert!(t.is_some()); + } + eprintln!("size {s:>5} roughness {r:>5}: {best:.1} ms ({:?})", look); + } +} + +/// Fits the median crystal width per Size (Roughness 50) and the Roughness factors at Size 25 to +/// Lightroom Classic's grain widths (`--ignored --nocapture`; paste into the look tables). +#[test] +#[ignore] +fn fit_widths() { + let width_of = |look: CrystalLook| { + let mut w = 0.0; + for seed in [11, 12] { + let t = CrystalTile::new(look, seed).expect("tile"); + w += half_width(&patch(&t, REF_LONG, 360, 0), 360) / 2.0; + } + w + }; + let fit = |s: f64, r: f64, want: f64| { + let base = CrystalLook::new(s, r); + let (mut lo, mut hi) = (0.05f64, 40.0f64); + for _ in 0..18 { + let mid = (lo * hi).sqrt(); + if width_of(CrystalLook { width: mid, ..base }) < want { lo = mid } else { hi = mid } + } + let d = (lo * hi).sqrt(); + (d, width_of(CrystalLook { width: d, ..base })) + }; + for (s, want) in [(0.0, 0.43), (10.0, 0.71), (25.0, 1.13), (50.0, 1.70), (75.0, 2.54), (100.0, 3.38)] { + let (d, got) = fit(s, 50.0, want); + eprintln!("size {s}: width {d:.3} gives {got:.3} (want {want})"); + } + let (d50, _) = fit(25.0, 50.0, 1.13); + for (r, want) in [(0.0, 0.86), (100.0, 1.54)] { + let (d, got) = fit(25.0, r, want); + eprintln!("roughness {r}: factor {:.3} gives {got:.3} (want {want})", d / d50); + } +} + +/// Autocorrelation at lags 1–4 and the strength kept by box downscales of 2–16 (relative), of +/// candidate looks (`--ignored --nocapture`): compares a look's texture with a reference's. +#[test] +#[ignore] +fn texture_report() { + let profile = |look: CrystalLook| { + let n = 512; + let (mut acf, mut down) = ([0.0; 4], [0.0; 4]); + for seed in [21, 22] { + let t = CrystalTile::new(look, seed).expect("tile"); + let v = patch(&t, REF_LONG, n, 0); + let s0 = std(&v); + for (k, a) in acf.iter_mut().enumerate() { + let lag = k + 1; + let mut s = 0.0f64; + for y in 0..n { + for x in 0..n - lag { + s += (v[y * n + x] * v[y * n + x + lag] + v[x * n + y] * v[(x + lag) * n + y]) as f64; + } + } + *a += s / (2 * n * (n - lag)) as f64 / (s0 * s0) / 2.0; + } + for (k, d) in down.iter_mut().enumerate() { + let f = 2usize << k; + let m = n / f; + let b: Vec = (0..m * m) + .map(|q| { + let (x, y) = (q % m * f, q / m * f); + (0..f * f).map(|i| v[(y + i / f) * n + x + i % f]).sum::() / (f * f) as f32 + }) + .collect(); + *d += std(&b) / s0 / 2.0; + } + } + (acf, down) + }; + for (s, r) in [(25.0, 50.0), (50.0, 50.0), (100.0, 50.0), (25.0, 0.0), (25.0, 100.0)] { + let (a, d) = profile(CrystalLook::new(s, r)); + eprintln!( + "size {s} roughness {r}: acf {:.2} {:.2} {:.2} {:.2} down {:.2} {:.2} {:.2} {:.3}", + a[0], a[1], a[2], a[3], d[0], d[1], d[2], d[3] + ); + } +} + +/// A row computed with shared box edges equals the per-pixel boxes: any size, mirrored rows, rows +/// across the tori's seams and far from the origin. +#[test] +fn row_field_matches_per_pixel_field() { + let t = tile_of(25.0, 50.0); + for (w, sign, x, y) in [ + (2.0f32, 1.0f32, 3.3f32, 7.9f32), + (2.0, -1.0, 9000.2, 1275.0), + (8.0, 1.0, 11990.0, 2260.5), + (0.7, 1.0, 1279.6, 0.1), + (31.0, -1.0, 400.0, 1133.9), + ] { + for along_x in [true, false] { + let mut row = vec![0.0f32; 700]; + t.row_field(x, y, w, sign * w, along_x, &mut row); + for (k, &v) in row.iter().enumerate() { + let d = sign * w * k as f32; + let want = if along_x { t.field(x + d, y, w) } else { t.field(x, y + d, w) }; + assert!((v - want).abs() < 2e-3, "w {w} sign {sign} x {along_x} from ({x}, {y}), pixel {k}: {v} vs {want}"); + } + } + } +} + +/// Per-pixel cost of the grain on one thread (`--ignored --nocapture`). +#[test] +#[ignore] +fn pixel_cost_report() { + let g = CrystalGrain::new(&Grain { amount: 50.0, size: 25.0, roughness: 50.0, seed: 1 }, REF_LONG).expect("grain"); + let (w, h) = (4000usize, 600usize); + let mut sink = 0.0f32; + let t0 = std::time::Instant::now(); + for y in 0..h { + for x in 0..w { + sink += g.tile.field((x as f32 + 0.5) * 2.0, (y as f32 + 0.5) * 2.0, 2.0); + } + } + let per_pixel = t0.elapsed().as_secs_f64() / (w * h) as f64 * 1e9; + let mut row = vec![0.0; w]; + let t1 = std::time::Instant::now(); + for y in 0..h { + g.tile.row_field(1.0, (y as f32 + 0.5) * 2.0, 2.0, 2.0, true, &mut row); + sink += row[7]; + } + let per_row = t1.elapsed().as_secs_f64() / (w * h) as f64 * 1e9; + let t3 = std::time::Instant::now(); + for y in 0..h { + g.tile.row_field((y as f32 + 0.5) * 2.0, 1.0, 2.0, 2.0, false, &mut row); + sink += row[7]; + } + eprintln!("column {:.1} ns/px", t3.elapsed().as_secs_f64() / (w * h) as f64 * 1e9); + let t2 = std::time::Instant::now(); + for y in 0..h { + for x in 0..w { + sink += g.apply_field([0.4, 0.5, 0.6], (x + y) as f32 * 1e-4)[1]; + } + } + let tone = t2.elapsed().as_secs_f64() / (w * h) as f64 * 1e9; + eprintln!("field {per_pixel:.1} ns/px, row {per_row:.1} ns/px, tone {tone:.1} ns/px ({sink})"); +} diff --git a/crates/pipeline/src/tests_grain.rs b/crates/pipeline/src/tests_grain.rs new file mode 100644 index 0000000000..9dc4892453 --- /dev/null +++ b/crates/pipeline/src/tests_grain.rs @@ -0,0 +1,99 @@ +//! Film grain through the whole pipeline: which model each process renders with, and the crystal +//! grain's resolution independence (`crate::crystal`). + +use lightcraft_develop::{DevelopSettings, Process}; +use lightcraft_raster::Rgb32f; + +use crate::finish::GrainModel; +use crate::{OutputDepth, RenderRequest, SourceInfo, render}; + +fn on(process: Process, amount: f64) -> DevelopSettings { + let mut s = DevelopSettings { process: process.version(), ..Default::default() }; + s.grain.amount = amount; + s +} + +/// Display-encoded luminance (0..255, 16-bit precision) of a render of `src` at `w × h`. +fn luma(src: &Rgb32f, s: &DevelopSettings, w: usize, h: usize) -> Vec { + let mut req = RenderRequest::fit(w, h); + req.depth = OutputDepth::U16; + let r = render(src, &SourceInfo::default(), s, &req); + let Some(crate::DeepSamples::U16(v)) = r.deep.map(|d| d.samples) else { panic!("no 16-bit render") }; + v.chunks(3).map(|p| (0.2126 * p[0] as f64 + 0.7152 * p[1] as f64 + 0.0722 * p[2] as f64) / 257.0).collect() +} + +fn std(v: &[f64]) -> f64 { + let m = v.iter().sum::() / v.len() as f64; + (v.iter().map(|x| (x - m).powi(2)).sum::() / v.len() as f64).sqrt() +} + +#[test] +fn processes_before_v7_keep_the_value_noise() { + for p in Process::ALL { + let model = GrainModel::new(&on(p, 40.0), 2000.0, false); + match p { + Process::V1 | Process::V2 | Process::V3 | Process::V4 | Process::V5 | Process::V6 => { + assert!(matches!(model, Some(GrainModel::Value(_))), "{p:?}") + } + Process::V7 | Process::V8 => assert!(matches!(model, Some(GrainModel::Crystal(_))), "{p:?}"), + } + assert!(GrainModel::new(&on(p, 0.0), 2000.0, false).is_none(), "{p:?}: no grain at Amount 0"); + } + let src = Rgb32f::filled(160, 120, [0.18; 3]); + let (v6, v7) = (luma(&src, &on(Process::V6, 50.0), 160, 120), luma(&src, &on(Process::V7, 50.0), 160, 120)); + assert_ne!(v6, v7, "V7 renders the crystal grain"); +} + +/// Grain is all that V7 changes: without it, a photo renders as under V6. +#[test] +fn without_grain_v7_renders_as_v6() { + let src = Rgb32f::filled(160, 120, [0.18, 0.4, 0.7]); + assert_eq!(luma(&src, &on(Process::V6, 0.0), 160, 120), luma(&src, &on(Process::V7, 0.0), 160, 120)); +} + +/// A preview shows what the export shows, box-downscaled to its size (whole pipeline). +#[test] +fn small_render_matches_the_downscaled_large_render() { + let src = Rgb32f::filled(300, 200, [0.18; 3]); + let (w, h, k) = (1200, 800, 4); + for size in [0.0, 25.0, 100.0] { + let mut s = on(Process::V7, 60.0); + s.grain.size = size; + let base = luma(&src, &on(Process::V7, 0.0), w, h); + let big: Vec = luma(&src, &s, w, h).iter().zip(&base).map(|(a, b)| a - b).collect(); + let down: Vec = (0..(w / k) * (h / k)) + .map(|q| { + let (x, y) = (q % (w / k) * k, q / (w / k) * k); + (0..k * k).map(|i| big[(y + i / k) * w + x + i % k]).sum::() / (k * k) as f64 + }) + .collect(); + let small = luma(&src, &s, w / k, h / k); + let ratio = std(&small) / std(&down); + assert!((0.95..1.05).contains(&ratio), "size {size}: preview / export grain {ratio:.3}"); + // and the same grain pixel by pixel, not just as strong: the preview's grain (less its + // no-grain render) against the downscaled export's + let small_base = luma(&src, &on(Process::V7, 0.0), w / k, h / k); + let delta: Vec = small.iter().zip(&small_base).map(|(a, b)| a - b).collect(); + // (measured: at most 0.003 of 255 levels apart, the 16-bit output's rounding; the grain's + // standard deviation is 1.4–2.9 levels, so a shifted or reseeded pattern is far outside) + let worst = delta.iter().zip(&down).map(|(a, b)| (a - b).abs()).fold(0.0, f64::max); + assert!(worst < 0.01, "size {size}: preview grain {worst:.4} levels from the downscaled export's"); + } +} + +/// Degenerate and hostile grain settings render finite pixels under V7. +#[test] +fn hostile_grain_settings_render() { + let src = Rgb32f::filled(40, 30, [0.5, 0.2, 0.05]); + for (amount, size, rough) in + [(f64::NAN, 25.0, 50.0), (1e9, -1e9, f64::INFINITY), (50.0, f64::NAN, f64::NAN), (100.0, 100.0, 100.0), (-5.0, 0.0, 0.0)] + { + let mut s = on(Process::V7, amount); + s.grain.size = size; + s.grain.roughness = rough; + s.grain.seed = u32::MAX; + for (w, h) in [(40, 30), (1, 1), (3, 400)] { + assert!(luma(&src, &s, w, h).iter().all(|v| v.is_finite()), "{amount}/{size}/{rough} at {w}×{h}"); + } + } +} diff --git a/crates/pipeline/src/tests_tone.rs b/crates/pipeline/src/tests_tone.rs index f76f44775f..f8926e780c 100644 --- a/crates/pipeline/src/tests_tone.rs +++ b/crates/pipeline/src/tests_tone.rs @@ -208,6 +208,98 @@ fn median_orders_special_values_and_ties_like_total_cmp() { } } +/// A row of "8"s (two rings each, strokes 1.3 detail radii `r` wide, neighbours 0.8 radii apart): +/// dark lettering on a bright panel at the size of a bus number, as log luminance. +fn lettering(r: usize) -> (Plane, impl Fn(usize, usize) -> bool) { + let rf = r as f32; + let ring = move |x: f32, y: f32, cx: f32, cy: f32, outer: f32| { + let d = ((x - cx).powi(2) + (y - cy).powi(2)).sqrt(); + d <= outer && d >= outer - 1.3 * rf + }; + let stroke = move |x: usize, y: usize| { + let (x, y) = (x as f32, y as f32); + (0..5).any(|i| { + let cx = 150.0 + i as f32 * 7.4 * rf; + ring(x, y, cx, 200.0 - 2.8 * rf, 2.9 * rf) || ring(x, y, cx, 200.0 + 3.2 * rf, 3.3 * rf) + }) + }; + (Plane::from_fn(900, 400, |x, y| if stroke(x, y) { -4.6 } else { -1.3 }), stroke) +} + +#[test] +fn gaps_between_letters_keep_their_own_tone_in_the_base() { + // At the size of a bus number in a 7000 px photo, a median at the detail radius filled the + // gaps between the digits and around their waists with the letters' tone: 360 panel pixels + // sat over 1 EV (up to 2.3 EV) above their base, so Shadows lifted them by up to 1.3 EV into + // bright blocks. The wider median sees the panel there, so the gaps keep their own tone. + let ppl = 6960.0; + let r = local::base_detail_radius(ppl); + assert_eq!(r, 10); + let (l, stroke) = lettering(r); + let (w, h) = (l.width, l.height); + let base = local::tone_base(&l, 0.015 * ppl as f32, r, true); + // panel pixels at least 3 px from a stroke (the guided filter's own edge softness aside) + let panel = |x: usize, y: usize| !(x.saturating_sub(2)..(x + 3).min(w)).any(|a| (y.saturating_sub(2)..(y + 3).min(h)).any(|b| stroke(a, b))); + let (mut worst, mut under) = (0.0f32, 0usize); + for y in 0..h { + for x in (0..w).filter(|&x| panel(x, y)) { + let d = l.get(x, y) - base.get(x, y); + worst = worst.max(d); + under += usize::from(d > 1.0); + } + } + assert!(under == 0 && worst < 1.0, "{under} panel px over 1 EV above their base; worst {worst} EV"); +} + +#[test] +fn isolated_lines_up_to_the_detail_radius_stay_out_of_the_base() { + // Light lines 1 to 9 px wide on a dark ground (twigs against a dark wall, r = 10): the base + // keeps the ground's tone under each, as under the median (#632), so Shadows lifts them with + // the ground and they keep their contrast. Clamping to the window's quartiles instead let lines + // of 6 px and more a third to two thirds of the way into the base. + let ppl = 6960.0; + let r = local::base_detail_radius(ppl); + let widths = [1usize, 2, 3, 4, 5, 6, 7, 8, 9]; + let l = Plane::from_fn(1300, 200, |x, _| { + let (i, off) = (x / 130, x % 130); + if i < widths.len() && (60..60 + widths[i]).contains(&off) { -2.0 } else { -6.0 } + }); + let base = local::tone_base(&l, 0.015 * ppl as f32, r, true); + for (i, wd) in widths.iter().enumerate() { + let leak = (base.get(i * 130 + 60 + wd / 2, 100) + 6.0) / 4.0; + assert!(leak.abs() < 0.05, "a {wd} px line is {leak} of the way into the base"); + } +} + +#[test] +fn only_process_v8_on_takes_the_detail_free_base() { + // V2 to V7 keep the plain median (V1 has none): they are released and frozen (`tests_legacy`) + for p in Process::ALL { + let s = DevelopSettings { process: p.version(), ..Default::default() }; + assert_eq!(local::base_keeps_gaps(&s), p >= Process::V8, "{p:?}"); + } + let (l, _) = lettering(10); + let plain = local::guided_fast(&local::median_hv(&l, 10), 100.0, local::BASE_EPS); + assert_eq!(local::tone_base(&l, 100.0, 10, false).data, plain.data); + assert_ne!(local::tone_base(&l, 100.0, 10, true).data, plain.data); +} + +#[test] +fn detail_free_keeps_steps_and_regions() { + // a step between two flat regions, and a flat field, come out as they went in + let step = Plane::from_fn(90, 60, |x, _| if x < 45 { -4.0 } else { 0.5 }); + assert_eq!(local::detail_free(&step, 6).data, step.data); + let flat = Plane::from_fn(31, 17, |_, _| -1.25); + assert_eq!(local::detail_free(&flat, 3).data, flat.data); + assert_eq!(local::detail_free(&step, 0).data, step.data); + // tiny and empty planes, and NaNs, are not a panic + for (w, h) in [(1, 1), (2, 3), (5, 1)] { + let p = Plane::from_fn(w, h, |x, y| if (x + y) % 2 == 0 { f32::NAN } else { 1.0 }); + assert_eq!(local::detail_free(&p, 4).data.len(), w * h); + } + assert!(local::detail_free(&Plane::new(0, 0), 4).data.is_empty()); +} + #[test] fn v1_keeps_the_base_it_always_had() { // V1's base is the edge-aware filter of log luminance itself, as before process V2: no median @@ -225,7 +317,7 @@ fn v1_keeps_the_base_it_always_had() { let l = Plane::from_fn(301, 157, |x, y| (((x * 31 + y * 17) * 2_654_435_761usize) % 1000) as f32 / 250.0 - 2.0 + if x % 5 == 0 { 0.5 } else { 0.0 }); for sg in [1.0f32, 4.5, 18.0] { - assert_eq!(local::tone_base(&l, sg, 0).data, local::guided_fast(&l, sg, local::BASE_EPS).data, "sigma {sg}"); + assert_eq!(local::tone_base(&l, sg, 0, true).data, local::guided_fast(&l, sg, local::BASE_EPS).data, "sigma {sg}"); } // V2 takes the median wherever the image is big enough for one let v2 = with("light.highlights", -100.0); diff --git a/crates/pipeline/src/tests_window.rs b/crates/pipeline/src/tests_window.rs index e5fb6ea801..7fa71a5f25 100644 --- a/crates/pipeline/src/tests_window.rs +++ b/crates/pipeline/src/tests_window.rs @@ -6,7 +6,7 @@ use lightcraft_develop::{DevelopSettings, controls}; use lightcraft_geom::{Orientation, Rect}; use lightcraft_raster::{Rgb32f, Rgba8}; -use crate::{PixelWindow, RenderRequest, SourceInfo, render}; +use crate::{OutputDepth, PixelWindow, RenderRequest, SourceInfo, render}; const W: usize = 960; const H: usize = 640; @@ -422,6 +422,73 @@ fn auto_mask_strokes_match_the_whole_render() { check_noisy("auto mask", &s, PixelWindow { x: 400, y: 300, w: 300, h: 250 }, 2, 0.1); } +// Given a lens blur, a window (zoomed view) blurs right up to its edges as the whole render does: +// it is rendered with the blur's reach around it +#[test] +fn lens_blur_matches_the_whole_render_up_to_the_window_edges() { + lens_blur_window_matches(OutputDepth::U8); + lens_blur_window_matches(OutputDepth::U16); +} + +/// A high-bit-depth window grows for the lens blur too, and is cut back to the request. +fn lens_blur_window_matches(depth: OutputDepth) { + use lightcraft_develop::{Mask, MaskComponent, MaskOp, MaskShape}; + use lightcraft_geom::Point; + let radial = MaskShape::Radial { center: Point::new(0.5, 0.5), rx: 0.15, ry: 0.2, angle: 0.0, feather: 30.0, invert: false }; + let mut s = DevelopSettings { + masks: vec![Mask { + id: 4, + components: vec![MaskComponent { name: None, op: MaskOp::Add, invert: false, shape: radial }], + ..Default::default() + }], + ..Default::default() + }; + s.lens_blur.enabled = true; + s.lens_blur.amount = 80.0; + s.lens_blur.boost = 40.0; + s.lens_blur.focus_mask = Some(4); + let deep = |win: Option| { + let r = render(&scene(), &SourceInfo::default(), &s, &RenderRequest { window: win, depth, ..RenderRequest::fit(W, H) }); + if let Some(d) = &r.deep { + assert_eq!((d.width, d.height), (r.image.width, r.image.height)); + } + r.image + }; + let (f, w) = (deep(None), deep(Some(MID))); + assert_eq!((w.width, w.height), (MID.w, MID.h)); + let (max, mean) = compare_inset(&f, &w, MID, 0); + assert!(max <= 6 && mean <= 0.4, "max {max}, mean {mean:.3}"); + // and the blur shows: the whole render differs from the one without it + let mut off = s.clone(); + off.lens_blur.enabled = false; + let (_, mean) = compare_inset(&full(&off), &f, PixelWindow { x: 0, y: 0, w: f.width, h: f.height }, 0); + assert!(mean > 1.0, "{mean:.3}"); +} + +/// With the automatic subject focus (no focus mask, no fixed shape, no person matte) a window +/// focuses where the whole render does: the estimate comes from the whole photo, not from the +/// pixels the window holds. +#[test] +fn lens_blur_on_the_estimated_subject_matches_the_whole_render() { + let mut s = DevelopSettings::default(); + s.lens_blur.enabled = true; + s.lens_blur.amount = 80.0; + // a sharp cut: the window then grows only by the blur's radius, less than the estimate reads + s.lens_blur.transition = 0.0; + for depth in [OutputDepth::U8, OutputDepth::U16] { + let r = |win: Option| { + render(&scene(), &SourceInfo::default(), &s, &RenderRequest { window: win, depth, ..RenderRequest::fit(W, H) }).image + }; + // a window at the middle and one at an edge, where the estimate's neighbourhood is cut off + for win in [MID, PixelWindow { x: 0, y: 0, w: 300, h: 220 }] { + let (f, w) = (r(None), r(Some(win))); + assert_eq!((w.width, w.height), (win.w, win.h)); + let (max, mean) = compare_inset(&f, &w, win, 0); + assert!(max <= 6 && mean <= 0.4, "{depth:?} {win:?}: max {max}, mean {mean:.3}"); + } + } +} + // Given the joint colour NR, a window is filtered with the whole frame's noise model: a quiet part of // the frame does not make a window of its noisy part (or the other way round) smooth differently #[test] diff --git a/crates/raster/src/blur.rs b/crates/raster/src/blur.rs index 9fefea9ca2..c038340a38 100644 --- a/crates/raster/src/blur.rs +++ b/crates/raster/src/blur.rs @@ -49,8 +49,15 @@ fn box_row(row: &mut [T], tmp: &mut Vec, r: usize) { let last = w - 1; let inv = 1.0 / (2 * r + 1) as f32; let mut acc = T::zero(); - for i in 0..=2 * r { - acc = acc.madd(s[i.saturating_sub(r).min(last)], 1.0); + // the window [-r, r] with clamped edges: r copies of the first pixel, the pixels 0..=min(r, last), then the + // last pixel for each offset past it (O(w), not O(r), however large the radius) + acc = acc.madd(s[0], r as f32); + for v in &s[..=r.min(last)] { + acc = acc.madd(*v, 1.0); + } + // only when the window reaches past the last pixel: a NaN there must not poison a row it isn't in + if r > last { + acc = acc.madd(s[last], (r - last) as f32); } for x in 0..w { row[x] = T::zero().madd(acc, inv); @@ -177,6 +184,14 @@ pub fn gaussian_taps(img: &Image, taps: &[f32]) -> Image { #[cfg(test)] mod tests { + #[test] + fn a_non_finite_last_pixel_stays_out_of_windows_that_do_not_reach_it() { + let mut row = [1.0f32; 8]; + row[7] = f32::NAN; + box_row(&mut row, &mut Vec::new(), 2); + assert!(row[..5].iter().all(|v| v.is_finite()), "{row:?}"); + } + #[test] fn fir_gaussian_has_bounded_finite_taps_for_hostile_sigmas() { for sigma in [f32::NAN, f32::NEG_INFINITY, f32::INFINITY, f32::MAX, -5.0, 0.0, 0.1, 4.0] { @@ -277,4 +292,18 @@ mod tests { let var: f32 = b.data.iter().enumerate().map(|(i, v)| v * ((i as f32 - 100.0).powi(2))).sum(); assert!((var.sqrt() - sigma).abs() / sigma < 0.1, "{}", var.sqrt()); } + + #[test] + fn box_row_matches_the_clamped_window_sum_for_radii_past_the_row() { + for (w, r) in [(1usize, 1usize), (5, 1), (5, 4), (5, 5), (5, 9), (7, 3)] { + let mut row: Vec = (0..w).map(|i| (i * i + 1) as f32).collect(); + let want: Vec = (0..w as isize) + .map(|x| (-(r as isize)..=r as isize).map(|j| row[(x + j).clamp(0, w as isize - 1) as usize]).sum::() / (2 * r + 1) as f32) + .collect(); + box_row(&mut row, &mut Vec::new(), r); + for (a, b) in row.iter().zip(&want) { + assert!((a - b).abs() < 1e-3, "w {w} r {r}: {a} vs {b}"); + } + } + } } diff --git a/crates/raw/Cargo.toml b/crates/raw/Cargo.toml index 50ecfc3719..18bf6d76c2 100644 --- a/crates/raw/Cargo.toml +++ b/crates/raw/Cargo.toml @@ -9,7 +9,7 @@ description = "LightCraft RAW decoding, demosaicing and DNG colour model (L1)" workspace = true [dependencies] -lightcraft-tiff = { workspace = true } +craft-tiff = { workspace = true } lightcraft-meta = { workspace = true } lightcraft-raster = { workspace = true } lightcraft-color = { workspace = true } diff --git a/crates/raw/examples/rawinfo.rs b/crates/raw/examples/rawinfo.rs index 23e540b965..2271aa0bd2 100644 --- a/crates/raw/examples/rawinfo.rs +++ b/crates/raw/examples/rawinfo.rs @@ -3,8 +3,8 @@ //! Prints the TIFF structure (with `--tree`), decode result, timings (MP/s) and basic statistics. //! Exits unsuccessfully if any input cannot be read, decoded or developed, while inspecting the remaining files. +use craft_tiff::{Ifd, Tiff, Value}; use lightcraft_raw::{Method, RawData, decode, embedded_preview, probe}; -use lightcraft_tiff::{Ifd, Tiff, Value}; use std::process::ExitCode; use std::time::Instant; diff --git a/crates/raw/examples/rawrender.rs b/crates/raw/examples/rawrender.rs index abb1fe7c94..c1715a9ced 100644 --- a/crates/raw/examples/rawrender.rs +++ b/crates/raw/examples/rawrender.rs @@ -2,9 +2,9 @@ //! //! Quick visual check: develop → as-shot WB + camera matrix → sRGB, oriented, box-downscaled, 8-bit TIFF. +use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags}; use lightcraft_color::{REC2020, SRGB, transfer::encode_srgb8}; use lightcraft_raw::{Method, color, decode}; -use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags}; fn main() { let a: Vec = std::env::args().skip(1).collect(); diff --git a/crates/raw/src/dcp.rs b/crates/raw/src/dcp.rs index 774b1ceab1..a577fbe4e5 100644 --- a/crates/raw/src/dcp.rs +++ b/crates/raw/src/dcp.rs @@ -4,7 +4,7 @@ //! `UniqueCameraModel`, `ProfileName`, the colour/forward matrices and calibration illuminants, the //! hue/saturation map, look table and tone curve. Nothing here ships a profile. -use lightcraft_tiff::{Ifd, ParseOptions, Tiff, tags as t}; +use craft_tiff::{Ifd, ParseOptions, Tiff, tags as t}; use crate::ColorData; use crate::dng::color_data; @@ -118,8 +118,8 @@ pub fn parse_dcp(bytes: &[u8]) -> Result { /// Write `p` as a `.dcp` file (little-endian): its name, camera, illuminants, matrices, hue/saturation /// map, look table, tone curve and baseline exposure offset. pub fn write_dcp(p: &DcpProfile) -> Result, DcpError> { - use lightcraft_tiff::writer::srational; - use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; + use craft_tiff::writer::srational; + use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; let c = &p.color; let mut ifd = IfdBuilder::new(); ifd.set(t::UNIQUE_CAMERA_MODEL, Value::Ascii(p.camera_model.clone())); diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index bbea2c8c1a..c156e8f4cb 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -5,10 +5,10 @@ use crate::gaintable::GainTableMap; use crate::profile::{HsvTable, ProfileLook, ToneCurve}; use crate::tiffraw::{Packing, read_image_in}; use crate::{BlackLevel, Cfa, ColorData, Mat3, Mode, RawData, RawError, RawFormat, RawImage, Rect, Result, opcodes}; +use craft_tiff::tags::{self as t, photometric}; +use craft_tiff::{ByteOrder, Ifd, Tiff}; use lightcraft_color::Xy; use lightcraft_geom::Orientation; -use lightcraft_tiff::tags::{self as t, photometric}; -use lightcraft_tiff::{ByteOrder, Ifd, Tiff}; /// The main raw IFD: full-resolution (NewSubfileType 0) CFA or LinearRaw image with the most pixels. pub(crate) fn raw_ifd(tiff: &Tiff) -> Option<&Ifd> { @@ -183,7 +183,7 @@ pub(crate) fn is_plain_cfa_tiff(tiff: &Tiff, bytes: &[u8]) -> bool { t::compression::JPEG => info .chunks(bytes.len() as u64) .first() - .and_then(|c| lightcraft_tiff::image::chunk_bytes(bytes, c)) + .and_then(|c| craft_tiff::image::chunk_bytes(bytes, c)) .is_some_and(|src| crate::ljpeg::frame_info(src).is_ok()), _ => false, } diff --git a/crates/raw/src/dngwrite.rs b/crates/raw/src/dngwrite.rs index bdafb58658..303165f385 100644 --- a/crates/raw/src/dngwrite.rs +++ b/crates/raw/src/dngwrite.rs @@ -2,10 +2,10 @@ //! bit-exact decoder round-trip tests. The raw image is stored in IFD0 (allowed by DNG 1.7 §"File Structure"). use crate::{RawData, RawError, RawImage, Result, ljpeg, opcodes}; +use craft_tiff::tags::{self as t, compression, photometric}; +use craft_tiff::writer::{rational, srational}; +use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; use lightcraft_color::Mat3; -use lightcraft_tiff::tags::{self as t, compression, photometric}; -use lightcraft_tiff::writer::{rational, srational}; -use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; use rayon::prelude::*; #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -605,5 +605,8 @@ mod tests { assert_eq!(hostile, write_dng(&raw, &opts(2048)).expect("the cap itself writes"), "u32::MAX is clamped to the cap"); assert_ne!(hostile, write_dng(&raw, &opts(256)).expect("a normal tile writes"), "the clamp is not a no-op"); assert_eq!(crate::probe(&hostile), Some(RawFormat::Dng), "the clamped output is still a DNG"); + // the writer's own padded tile (2048 × 2048 around an 8 × 8 image) reads back + let back = crate::decode(&hostile).expect("a tile this writer pads to decodes"); + assert_eq!((back.width, back.height), (w, h)); } } diff --git a/crates/raw/src/gaintable.rs b/crates/raw/src/gaintable.rs index cec1d6828d..c6a1b4c91e 100644 --- a/crates/raw/src/gaintable.rs +++ b/crates/raw/src/gaintable.rs @@ -40,7 +40,7 @@ //! `MapPointsV` / `MapPointsH` is below 256 (their product is at most [`MAX_TABLES`]), so swapped //! it is at least 2^24 and the size checks reject it. We write in the file's byte order. -use lightcraft_tiff::ByteOrder; +use craft_tiff::ByteOrder; use serde::{Deserialize, Serialize}; /// Upper bound on `MapPointsV × MapPointsH` (Apple uses 6 × 8). diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index ff9ee048b7..f246612cae 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -70,8 +70,8 @@ pub use preview::{ }; pub use semantic::{SemanticMask, semantic_masks}; +use craft_tiff::{Tiff, TiffError}; use lightcraft_color::Xy; -use lightcraft_tiff::{Tiff, TiffError}; use serde::{Deserialize, Serialize}; /// Upper bound on decoded samples (guards allocations driven by header values). @@ -189,28 +189,28 @@ pub fn probe(bytes: &[u8]) -> Option { return Some(RawFormat::Cr2); } let (_, _) = Tiff::sniff(bytes)?; - let opts = lightcraft_tiff::ParseOptions { max_ifds: 256, ..Default::default() }; + let opts = craft_tiff::ParseOptions { max_ifds: 256, ..Default::default() }; let t = Tiff::parse_with(bytes, &opts).ok()?; let ifd0 = t.ifds.first()?; - if ifd0.contains(lightcraft_tiff::tags::DNG_VERSION) { + if ifd0.contains(craft_tiff::tags::DNG_VERSION) { return Some(RawFormat::Dng); } - let make = t.find(lightcraft_tiff::tags::MAKE).and_then(|e| e.value.as_str()).unwrap_or_default().to_ascii_uppercase(); + let make = t.find(craft_tiff::tags::MAKE).and_then(|e| e.value.as_str()).unwrap_or_default().to_ascii_uppercase(); let has_cfa = has_raw_ifd(&t); // Pentax's reader takes the layout from the Exif `CFAPattern` when no IFD has a CFA photometric (uncompressed // and PackBits PEFs), so for the Pentax family that tag counts as raw evidence too let pentax_raw = has_cfa || t.exif().is_some_and(|e| e.contains(vendor::EXIF_CFA_PATTERN)); // Samsung's raw IFD uses private compressions 32769..=32773 (`vendor::srw`), 32773 being PackBits elsewhere - let samsung_raw = has_cfa || t.all_ifds().iter().any(|i| i.u16(lightcraft_tiff::tags::COMPRESSION).is_some_and(|c| (32769..=32773).contains(&c))); + let samsung_raw = has_cfa || t.all_ifds().iter().any(|i| i.u16(craft_tiff::tags::COMPRESSION).is_some_and(|c| (32769..=32773).contains(&c))); // Sony raws without a CFA IFD (#702): SRF (DSC-F828), whose IFD0 looks like RGB but holds 14-bit samples (12 or // 14 count: raw sensor depths; the raw is in Sony's SRF blocks), and the DSLR-A100, whose IFD0 is an old-style // JPEG thumbnail (compression 6, which today's TIFF writers don't produce) and whose `SubIFDs` tag addresses its // raw data. A camera-authored or exported TIFF has 8- or 16-bit samples, and its SubIFDs hold ordinary images - let model = t.find(lightcraft_tiff::tags::MODEL).and_then(|e| e.value.as_str()).unwrap_or_default().trim().to_ascii_uppercase(); + let model = t.find(craft_tiff::tags::MODEL).and_then(|e| e.value.as_str()).unwrap_or_default().trim().to_ascii_uppercase(); let sony_raw = has_cfa - || ifd0.u64s(lightcraft_tiff::tags::BITS_PER_SAMPLE).is_some_and(|b| b.iter().any(|v| matches!(v, 12 | 14))) - || (model == "DSLR-A100" && ifd0.u16(lightcraft_tiff::tags::COMPRESSION) == Some(6) && ifd0.contains(lightcraft_tiff::tags::SUB_IFDS)); - if make.starts_with("CANON") && t.ifds.len() >= 4 && t.ifds[3].u16(lightcraft_tiff::tags::COMPRESSION) == Some(6) { + || ifd0.u64s(craft_tiff::tags::BITS_PER_SAMPLE).is_some_and(|b| b.iter().any(|v| matches!(v, 12 | 14))) + || (model == "DSLR-A100" && ifd0.u16(craft_tiff::tags::COMPRESSION) == Some(6) && ifd0.contains(craft_tiff::tags::SUB_IFDS)); + if make.starts_with("CANON") && t.ifds.len() >= 4 && t.ifds[3].u16(craft_tiff::tags::COMPRESSION) == Some(6) { return Some(RawFormat::Cr2); } if make.starts_with("NIKON") && has_cfa { @@ -218,7 +218,7 @@ pub fn probe(bytes: &[u8]) -> Option { } // Hasselblad's Sony-built bodies (the Lunar is a NEX-7 inside) write a Sony ARW under their own Make. Sony's // raw coding gives them away: compression 32767 or the raw IFD's tone-curve tag 0x7010, which no 3FR / FFF has - let sony_coded = || t.all_ifds().iter().any(|i| i.u16(lightcraft_tiff::tags::COMPRESSION) == Some(32767) || i.contains(0x7010)); + let sony_coded = || t.all_ifds().iter().any(|i| i.u16(craft_tiff::tags::COMPRESSION) == Some(32767) || i.contains(0x7010)); if (make.starts_with("SONY") && sony_raw) || (make.starts_with("HASSELBLAD") && sony_coded()) { return Some(RawFormat::Arw); } @@ -257,13 +257,13 @@ const LEAF_DATA: u16 = 0x8606; /// An uncompressed Leaf MOS: IFD0 is the 16-bit mosaic itself (compression 1, BlackIsZero, one sample per pixel), /// with no Make tag and no CFA photometric, so only Leaf's data block marks it as a raw (issue #1275). A rendered /// RGB image that kept the block stays an ordinary image. -fn is_leaf_mosaic(ifd0: &lightcraft_tiff::Ifd) -> bool { - ifd0.contains(LEAF_DATA) && ifd0.u16(lightcraft_tiff::tags::SAMPLES_PER_PIXEL).unwrap_or(1) == 1 +fn is_leaf_mosaic(ifd0: &craft_tiff::Ifd) -> bool { + ifd0.contains(LEAF_DATA) && ifd0.u16(craft_tiff::tags::SAMPLES_PER_PIXEL).unwrap_or(1) == 1 } /// Whether the Exif maker note starts with the signature of the Pentax layouts (`AOC\0` or `PENTAX \0`). fn has_pentax_maker_note(t: &Tiff, bytes: &[u8]) -> bool { - let Some(e) = t.exif().and_then(|e| e.get(lightcraft_tiff::tags::MAKER_NOTE)) else { return false }; + let Some(e) = t.exif().and_then(|e| e.get(craft_tiff::tags::MAKER_NOTE)) else { return false }; bytes.get(e.offset as usize..).is_some_and(|n| n.starts_with(b"AOC\0") || n.starts_with(b"PENTAX \0")) } @@ -272,19 +272,19 @@ fn has_pentax_maker_note(t: &Tiff, bytes: &[u8]) -> bool { /// or vendor-specific raw tags (Sony tone curve, CFA pattern, or Pentax compression 65535). fn has_raw_ifd(t: &Tiff) -> bool { t.all_ifds().iter().any(|i| { - i.u16(lightcraft_tiff::tags::PHOTOMETRIC) == Some(lightcraft_tiff::tags::photometric::CFA) - || i.u16(lightcraft_tiff::tags::COMPRESSION).is_some_and(|c| matches!(c, 34713 | 32767 | 32769 | 32770 | 99 | 65535)) + i.u16(craft_tiff::tags::PHOTOMETRIC) == Some(craft_tiff::tags::photometric::CFA) + || i.u16(craft_tiff::tags::COMPRESSION).is_some_and(|c| matches!(c, 34713 | 32767 | 32769 | 32770 | 99 | 65535)) || i.contains(0x7010) - || i.contains(lightcraft_tiff::tags::CFA_PATTERN_EP) - || i.contains(lightcraft_tiff::tags::CFA_REPEAT_PATTERN_DIM) + || i.contains(craft_tiff::tags::CFA_PATTERN_EP) + || i.contains(craft_tiff::tags::CFA_REPEAT_PATTERN_DIM) }) } /// IFD0 that is not an image at all: no `ImageWidth` / `ImageLength`, but a JPEG preview pointer or /// `SubIFDs` (the layout of Kodak KDC files, whose pictures sit in private tags). A plain TIFF reader /// rejects such a file as malformed even though it carries a preview. -fn is_preview_container(ifd0: &lightcraft_tiff::Ifd) -> bool { - use lightcraft_tiff::tags::{IMAGE_LENGTH, IMAGE_WIDTH, JPEG_INTERCHANGE_FORMAT, SUB_IFDS}; +fn is_preview_container(ifd0: &craft_tiff::Ifd) -> bool { + use craft_tiff::tags::{IMAGE_LENGTH, IMAGE_WIDTH, JPEG_INTERCHANGE_FORMAT, SUB_IFDS}; !ifd0.contains(IMAGE_WIDTH) && !ifd0.contains(IMAGE_LENGTH) && (ifd0.contains(JPEG_INTERCHANGE_FORMAT) || ifd0.contains(SUB_IFDS)) } @@ -309,7 +309,7 @@ pub(crate) struct ThumbnailShell { /// A multi-page TIFF is not caught (its pages are the IFD chain, not `SubIFDs`), nor a pyramid whose /// IFD0 is the full image. fn thumbnail_shell(t: &Tiff, len: usize) -> Option { - use lightcraft_tiff::tags::{IMAGE_LENGTH, IMAGE_WIDTH, PIXEL_X_DIMENSION, PIXEL_Y_DIMENSION}; + use craft_tiff::tags::{IMAGE_LENGTH, IMAGE_WIDTH, PIXEL_X_DIMENSION, PIXEL_Y_DIMENSION}; let ifd0 = t.ifds.first()?; let (w0, h0) = (ifd0.u32(IMAGE_WIDTH)?, ifd0.u32(IMAGE_LENGTH)?); if w0 == 0 || h0 == 0 { @@ -352,12 +352,12 @@ fn thumbnail_shell(t: &Tiff, len: usize) -> Option { /// Why [`decode`] gives up on a file [`probe`] called [`RawFormat::OtherTiff`]. fn other_tiff_reason(bytes: &[u8]) -> String { - let t = Tiff::parse_with(bytes, &lightcraft_tiff::ParseOptions { max_ifds: 256, ..Default::default() }).ok(); + let t = Tiff::parse_with(bytes, &craft_tiff::ParseOptions { max_ifds: 256, ..Default::default() }).ok(); // a DNG-style CFA IFD whose lossless JPEG the lossless decoder rejects: say why if let Some(t) = &t && let Some(info) = dng::raw_ifd(t).and_then(|i| i.image().ok()) - && info.compression == lightcraft_tiff::tags::compression::JPEG - && let Some(src) = info.chunks(bytes.len() as u64).first().and_then(|c| lightcraft_tiff::image::chunk_bytes(bytes, c)) + && info.compression == craft_tiff::tags::compression::JPEG + && let Some(src) = info.chunks(bytes.len() as u64).first().and_then(|c| craft_tiff::image::chunk_bytes(bytes, c)) && let Err(e) = ljpeg::frame_info(src) { return format!("raw image coded as lossless JPEG that is not decoded yet ({e})"); diff --git a/crates/raw/src/preview.rs b/crates/raw/src/preview.rs index d661439780..cc4535af2b 100644 --- a/crates/raw/src/preview.rs +++ b/crates/raw/src/preview.rs @@ -3,9 +3,9 @@ //! or a DNG 1.7 JPEG XL preview IFD stored as a single tile/strip (a standalone `.jxl` file, as the spec //! recommends for previews). -use lightcraft_tiff::image::chunk_bytes; -use lightcraft_tiff::tags as t; -use lightcraft_tiff::{Ifd, Tiff, makernote}; +use craft_tiff::image::chunk_bytes; +use craft_tiff::tags as t; +use craft_tiff::{Ifd, Tiff, makernote}; /// Colour space of an embedded JPEG when the enclosing raw supplies it instead of the JPEG. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -98,11 +98,17 @@ pub fn embedded_preview_dynamic_range_optimized(bytes: &[u8]) -> Option { /// The first frame (SOF) marker of a JPEG and its position, from the marker segments before the first scan. fn first_frame(b: &[u8]) -> Option<(u8, usize)> { + first_frame_within(b, &mut { usize::MAX }) +} + +/// [`first_frame`] charging `work` one per step (a segment, or a fill byte); `None` too when it runs out. +fn first_frame_within(b: &[u8], work: &mut usize) -> Option<(u8, usize)> { if b.len() < 4 || !b.starts_with(&[0xff, 0xd8]) { return None; } let mut i = 2usize; while i.checked_add(4)? <= b.len() { + *work = work.checked_sub(1)?; if *b.get(i)? != 0xff { return None; } @@ -197,7 +203,7 @@ fn jpeg_end(s: &[u8], budget: &mut usize) -> Option { fn candidates<'a>(data: &'a [u8], ifd: &Ifd, base: u64, out: &mut Vec<&'a [u8]>, budget: &mut usize) { // whole JPEG files stored as an undefined-type tag value (e.g. Panasonic `JpgFromRaw` 0x002e) for e in &ifd.entries { - if matches!(e.value, lightcraft_tiff::Value::Undefined(_)) + if matches!(e.value, craft_tiff::Value::Undefined(_)) && e.count() > 1024 && let Some(s) = file_span(data, e.offset, e.count() as u64, budget) && s.starts_with(&[0xff, 0xd8]) @@ -304,7 +310,7 @@ fn preview_covering(bytes: &[u8], min_edge: Option) -> Option> { if let Some(mn) = makernote::parse_makernote(bytes, e.offset, e.count() as u64, tiff.order, &make) { candidates(bytes, &mn.ifd, mn.base, &mut found, &mut budget); if let Some(off) = mn.ifd.u64(0x0011) - && let Ok((pifd, _)) = lightcraft_tiff::parse_ifd_at(bytes, mn.base + off, mn.order, mn.base, false, &Default::default()) + && let Ok((pifd, _)) = craft_tiff::parse_ifd_at(bytes, mn.base + off, mn.order, mn.base, false, &Default::default()) { candidates(bytes, &pifd, mn.base, &mut found, &mut budget); } @@ -314,6 +320,7 @@ fn preview_covering(bytes: &[u8], min_edge: Option) -> Option> { if let Some(p) = crate::vendor::orf::preview(bytes) { found.push(p); } + bound_candidates(&mut found, bytes.len()); let largest = found.iter().copied().filter(|s| is_dct_jpeg(s) || is_jxl(s)).max_by_key(|s| s.len()); let best = min_edge .zip(largest) @@ -326,6 +333,25 @@ fn preview_covering(bytes: &[u8], min_edge: Option) -> Option> { .or_else(|| (format == Some(crate::RawFormat::CfaTiff)).then(|| crate::vendor::hasselblad::rgb_preview(&tiff, bytes)).flatten()) } +/// Drops candidate ranges that repeat an earlier one, that are not a displayable JPEG or JPEG XL, and valid ones +/// past a shared budget of [`scan_budget`] bytes: a range is charged only once it validates, so invalid ranges +/// covering most of the file cannot use up the budget before a valid preview. The header walks that validate +/// (or reject) the ranges share a second budget of the same size, charged for every walk, so many ranges +/// that start a JPEG header and run through fill bytes cannot each cost a walk of their length. +fn bound_candidates(found: &mut Vec<&[u8]>, file_len: usize) { + let mut seen = std::collections::HashSet::new(); + let mut left = scan_budget(file_len); + let mut work = scan_budget(file_len); + found.retain(|s| { + let mut dct = || first_frame_within(s, &mut work).is_some_and(|(m, _)| matches!(m, 0xc0..=0xc2)); + if !seen.insert((s.as_ptr() as usize, s.len())) || !(dct() || is_jxl(s)) || s.len() > left { + return false; + } + left -= s.len(); + true + }); +} + /// The end of the DCT JPEG whose SOI is at `soi` (exclusive index after its EOI) and its component count, /// walking the marker segments and the entropy-coded data. `None` when it is not a complete, displayable /// (baseline or progressive) JPEG, so a stray `D8 FF` in other data is rejected. `*reach` is left at @@ -645,3 +671,52 @@ fn trim_eoi(s: &[u8]) -> &[u8] { #[cfg(test)] mod tests; + +#[cfg(test)] +mod bound_tests { + #[test] + fn repeated_and_over_budget_candidates_are_dropped() { + let mut data = vec![0u8; 4 << 20]; + // a valid JPEG header every 16 bytes, so each range below validates + for i in 0..64 { + data[i * 16..i * 16 + 15].copy_from_slice(&[0xff, 0xd8, 0xff, 0xc0, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0]); + } + let mut found: Vec<&[u8]> = vec![&data[..], &data[..], &data[..]]; + super::bound_candidates(&mut found, data.len()); + assert_eq!(found.len(), 1); + // overlapping but unequal ranges stop at the shared budget (8 x the file, at least 1 MiB) + let mut found: Vec<&[u8]> = (0..64).map(|i| &data[i * 16..]).collect(); + super::bound_candidates(&mut found, data.len()); + assert!(found.len() <= 8 && found.iter().map(|s| s.len()).sum::() <= 8 * data.len()); + } + + #[test] + fn invalid_ranges_do_not_use_up_the_budget_before_a_valid_preview() { + let mut data = vec![0u8; 2 << 20]; + let jpeg = [0xff, 0xd8, 0xff, 0xc0, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0]; + data[1 << 20..(1 << 20) + jpeg.len()].copy_from_slice(&jpeg); + // 40 distinct invalid ranges of nearly the whole file (far past the 8 x budget), then the valid one + let mut found: Vec<&[u8]> = (0..40).map(|i| &data[i..1 << 20]).collect(); + found.push(&data[1 << 20..]); + super::bound_candidates(&mut found, data.len()); + assert_eq!(found.len(), 1); + assert!(found[0].starts_with(&[0xff, 0xd8])); + } + + #[test] + fn header_walks_of_many_fill_byte_ranges_share_a_budget() { + // a JPEG header followed by 100 000 fill bytes and no frame: each walk would run to the end + let mut data = vec![0xffu8; 100_100]; + data[..2].copy_from_slice(&[0xff, 0xd8]); + let valid = [0xff, 0xd8, 0xff, 0xc0, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0]; + // 50 distinct such ranges (5 M steps against the 1 MiB budget), then a valid header + let mut found: Vec<&[u8]> = (0..50).map(|i| &data[..100_000 + i]).collect(); + found.push(&valid); + super::bound_candidates(&mut found, data.len()); + assert!(found.is_empty(), "the walks stop at the shared budget, so the later header is not reached"); + // the same header with no hostile ranges before it is kept + let mut found: Vec<&[u8]> = vec![&valid]; + super::bound_candidates(&mut found, data.len()); + assert_eq!(found.len(), 1); + } +} diff --git a/crates/raw/src/preview/tests.rs b/crates/raw/src/preview/tests.rs index 561d39645d..924c4b4213 100644 --- a/crates/raw/src/preview/tests.rs +++ b/crates/raw/src/preview/tests.rs @@ -1,5 +1,5 @@ use super::*; -use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; +use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; fn fake_jpeg(n: usize) -> Vec { let mut j = vec![0xff, 0xd8, 0xff, 0xc0, 0x00, 0x0b, 8, 0, 1, 0, 1, 1, 1, 0x11, 0]; @@ -394,10 +394,9 @@ fn cr3_with_color_space(exif: Option, note: Option) -> Vec { let block = |tag: u16, v: Option| { let ifd = v.map_or_else(|| IfdBuilder::new().with(t::ISO_SPEED, Value::Short(vec![100])), |v| IfdBuilder::new().with(tag, Value::Short(vec![v]))); - TiffWriter::new(lightcraft_tiff::ByteOrder::Little, false).write(&[ifd]).unwrap() + TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[ifd]).unwrap() }; - let cmt1 = - TiffWriter::new(lightcraft_tiff::ByteOrder::Little, false).write(&[IfdBuilder::new().with(t::MAKE, Value::Ascii("Canon".into()))]).unwrap(); + let cmt1 = TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[IfdBuilder::new().with(t::MAKE, Value::Ascii("Canon".into()))]).unwrap(); let canon_uuid = [0x85, 0xc0, 0xb6, 0x87, 0x82, 0x0f, 0x11, 0xe0, 0x81, 0x11, 0xf4, 0xce, 0x46, 0x2b, 0x6a, 0x48]; let boxes = [bx(b"CMT1", &cmt1), bx(b"CMT2", &block(0xA001, exif)), bx(b"CMT3", &block(0x00b4, note))].concat(); let mut file = bx(b"ftyp", b"crx \0\0\0\x01crx isom"); @@ -444,7 +443,7 @@ fn cr2_preview_colour_space_comes_from_the_canon_maker_note() { } let mut ifd0 = IfdBuilder::new().with(t::MAKE, Value::Ascii(make.into())); ifd0.set_child(t::EXIF_IFD, exif); - TiffWriter::new(lightcraft_tiff::ByteOrder::Little, false).write(&[ifd0]).unwrap() + TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[ifd0]).unwrap() }; assert_eq!(embedded_preview_color_space(&cr2("Canon", 0xFFFF, Some(2))), Some(AdobeRgb)); assert_eq!(embedded_preview_color_space(&cr2("Canon", 1, Some(1))), Some(Srgb)); diff --git a/crates/raw/src/semantic.rs b/crates/raw/src/semantic.rs index e9af87c08a..fba28596e1 100644 --- a/crates/raw/src/semantic.rs +++ b/crates/raw/src/semantic.rs @@ -9,9 +9,9 @@ use crate::Rect; use crate::tiffraw::{Packing, read_image}; use crate::{RawData, Result}; +use craft_tiff::Tiff; +use craft_tiff::tags::{self as t, photometric}; use lightcraft_raster::Image; -use lightcraft_tiff::Tiff; -use lightcraft_tiff::tags::{self as t, photometric}; /// At most this many mask IFDs are read from one file. pub const MAX_MASKS: usize = 16; @@ -55,7 +55,7 @@ pub fn semantic_masks(bytes: &[u8]) -> Vec { .collect() } -fn read_mask(bytes: &[u8], tiff: &Tiff, ifd: &lightcraft_tiff::Ifd) -> Result> { +fn read_mask(bytes: &[u8], tiff: &Tiff, ifd: &craft_tiff::Ifd) -> Result> { let Some(name) = ifd.string(t::SEMANTIC_NAME).filter(|n| !n.is_empty()) else { return Ok(None) }; let info = ifd.image()?; let (w, h) = (info.width as usize, info.height as usize); diff --git a/crates/raw/src/sparse.rs b/crates/raw/src/sparse.rs index 893ba79b20..dd81263e58 100644 --- a/crates/raw/src/sparse.rs +++ b/crates/raw/src/sparse.rs @@ -19,8 +19,8 @@ use std::collections::HashSet; +use craft_tiff::{ByteOrder, FieldType, Tiff, tags as t}; use lightcraft_meta::cr3::{Cr3TrackKind, XMP_UUID, is_cr3, parse_cr3}; -use lightcraft_tiff::{ByteOrder, FieldType, Tiff, tags as t}; use crate::RawFormat; @@ -29,7 +29,7 @@ use crate::RawFormat; const HEAD: u64 = 64 * 1024; /// An image whose chunks hold fewer bytes than this is read (thumbnails, small previews). const BULK: u64 = 256 * 1024; -/// The same bounds as `lightcraft_tiff`'s reader. +/// The same bounds as `craft_tiff`'s reader. const MAX_IFDS: usize = 1024; const MAX_ENTRIES: u64 = 8192; const MAX_BOXES: usize = 4096; @@ -301,7 +301,7 @@ fn tiff_plan(bytes: &[u8], loaded: &Ranges, preview: bool) -> HeaderPlan { }) } -/// Walk the IFDs of the TIFF in `data` (which starts at `origin` in the file) as `lightcraft_tiff`'s +/// Walk the IFDs of the TIFF in `data` (which starts at `origin` in the file) as `craft_tiff`'s /// reader does — the chain, `SubIFDs`, Exif, GPS and Interop, plus the `extra` pointer tags — /// asking for every IFD table and for the values that locate IFDs and image chunks. Returns the IFD /// tables and out-of-line values found (absolute ranges); incomplete while `w` needs more. diff --git a/crates/raw/src/tests.rs b/crates/raw/src/tests.rs index e58991ccc8..0cff6f2006 100644 --- a/crates/raw/src/tests.rs +++ b/crates/raw/src/tests.rs @@ -120,7 +120,7 @@ fn black_levels() { // --- TIFF shells around a private raw block (thumbnail-only IFD0) --- -use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t}; +use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t}; /// An uncompressed 8-bit RGB image IFD. fn rgb_ifd(w: u32, h: u32) -> IfdBuilder { @@ -408,7 +408,7 @@ fn uncompressed_leaf_mos_is_a_raw() { } ifd }; - let bytes = TiffWriter::new(lightcraft_tiff::ByteOrder::Big, false).write(&[mosaic(true)]).unwrap(); + let bytes = TiffWriter::new(craft_tiff::ByteOrder::Big, false).write(&[mosaic(true)]).unwrap(); assert_eq!(probe(&bytes), Some(RawFormat::OtherTiff)); assert!(matches!(decode(&bytes), Err(RawError::Unsupported(_)))); assert!(matches!(probe_info(&bytes), Err(RawError::Unsupported(_)))); diff --git a/crates/raw/src/tiffraw.rs b/crates/raw/src/tiffraw.rs index deb64ea595..9d6b0ca1e6 100644 --- a/crates/raw/src/tiffraw.rs +++ b/crates/raw/src/tiffraw.rs @@ -2,8 +2,8 @@ use crate::unpack::{f16_to_f32, f24_to_f32, inflate, read_u16s, undo_diff_u16, undo_float_predictor, unpack_lsb, unpack_msb, unpack_words32_msb}; use crate::{MAX_SAMPLES, RawData, RawError, Result, ljpeg}; -use lightcraft_tiff::image::{Chunk, ImageInfo, chunk_bytes}; -use lightcraft_tiff::{ByteOrder, tags::compression as comp}; +use craft_tiff::image::{Chunk, ImageInfo, chunk_bytes}; +use craft_tiff::{ByteOrder, tags::compression as comp}; use rayon::prelude::*; /// Bit packing for uncompressed integer data with bit depths other than 8/16. @@ -51,8 +51,11 @@ pub fn check_image(data: &[u8], info: &ImageInfo) -> Result { let (across, down) = info.grid(); let planes = if info.planar == 2 { cpp } else { 1 }; let expected = (across as usize).saturating_mul(down as usize).saturating_mul(planes); - if chunks.len() < expected { - return Err(RawError::Corrupt(format!("{} image data chunks, expected {expected}", chunks.len()))); + // the offsets table says how many chunks the image declares: craft-tiff builds at most one + // descriptor per byte of file (craft-libs#827), so `chunks` may be shorter on a grid of tiles + // sharing one payload, and the bytes behind it are what the bounds below count. + if info.offsets.len() < expected { + return Err(RawError::Corrupt(format!("{} image data chunks, expected {expected}", info.offsets.len()))); } // Tile sizes don't depend on the image size, and every tile is decoded at its nominal size and held until the copy: // a grid of tiles no larger than the image covers less than twice its width and height, so more than four times @@ -247,7 +250,12 @@ fn decode_chunk(data: &[u8], info: &ImageInfo, c: &Chunk, order: ByteOrder, pack } comp::LOSSY_JPEG => { // lossy DNG: each chunk is a baseline (DCT) JPEG of 8-bit samples - let (px, jw, jh, jc) = lossy_jpeg(src, cpp, MAX_JPEG_SAMPLES)?; + // a JPEG decoded for a tile may hold no more samples than the tile (a real one is exactly the tile): + // many small tiles pointing at one big JPEG would otherwise each decode all of it + // (the JPEG codes whole MCUs, at most 32 × 32 pixels each, so its size is the tile's rounded up to them) + let mcu = |v: usize| v.div_ceil(32).saturating_mul(32); + let tile_budget = mcu(cw).saturating_mul(mcu(ch)).saturating_mul(if cpp == 1 { 1 } else { 3 }).clamp(1, MAX_JPEG_SAMPLES); + let (px, jw, jh, jc) = lossy_jpeg(src, cpp, tile_budget)?; // the buffer holds the rows the decoded JPEG covers (the caller zero-fills the tile's // remainder and reads only rows it fills): a tile-many-larger decoded JPEG does not // get the nominal tile's full buffer (issue #1517) @@ -271,6 +279,16 @@ fn decode_chunk(data: &[u8], info: &ImageInfo, c: &Chunk, order: ByteOrder, pack // Deflate compresses at most about 1:1032, so a source this far under half of its // nominal length cannot reconstruct the chunk: refused before the nominal buffer is // inflated and predicted, which padding-only work would make gigabytes. + // Nor may the nominal chunk be mostly padding outside the image (a tile far larger than the picture): + // its allocation and predictor pass would be spent on samples nobody reads. + if n > vw + .saturating_mul(vh) + .saturating_mul(cpp) + .saturating_mul(MAX_PADDING_FACTOR) + .saturating_add(MAX_WRITER_TILE_SAMPLES.saturating_mul(cpp)) + { + return Err(RawError::Limit("deflated chunk is mostly padding outside the image")); + } if (src.len() as u64).saturating_mul(2048) < inflate_len { return Err(RawError::Corrupt(format!("a deflated chunk of {inflate_len} bytes is packed into only {} bytes", src.len()))); } @@ -315,6 +333,13 @@ fn jxl_chunk_check(src: &[u8], cw: usize, ch: usize, vw: usize, vh: usize, cpp: jxl_chunk(src, cw, ch, vw, vh, cpp, bits, float).map(|_| ()) } +/// How many times its in-image samples a deflated chunk's nominal size may reach (plus a writer tile of slack): +/// edge tiles of real files are mostly inside the image. +const MAX_PADDING_FACTOR: usize = 32; +/// Samples per plane of the largest Deflate tile the DNG writer pads to (2048 × 2048, `dngwrite::MAX_DEFLATE_TILE`), +/// which a small image's one tile is nearly all padding of: such files still decode. +const MAX_WRITER_TILE_SAMPLES: usize = 2048 * 2048; + /// The most samples a tiled JPEG chunk or a white-balance preview may decode to: far more than /// any real lossy tile or preview carries, and small enough that a JPEG header alone cannot size /// a multi-gigabyte allocation. @@ -439,7 +464,7 @@ fn unpack_chunk( #[cfg(test)] mod lossy_tests { use super::*; - use lightcraft_tiff::image::Layout; + use craft_tiff::image::Layout; /// 65 536 tiles all pointing at the same 256 bytes: the image's samples can't come from them, /// and nothing of its size is allocated to find that out. @@ -505,6 +530,39 @@ mod lossy_tests { assert!((px(10, 5)[1] as i32 - 150).abs() <= 6, "green ramp: {:?}", px(10, 5)); } + /// A small tile may not decode a JPEG much larger than itself (a table of tiny tiles all pointing at one big + /// JPEG would decode it once per tile); a JPEG that fills the tile's whole 32 × 32 MCU block still decodes. + #[test] + fn a_lossy_tile_may_not_decode_a_much_larger_jpeg() { + let encode = |w: usize, h: usize| { + let mut out = Vec::new(); + jpeg_encoder::Encoder::new(&mut out, 90).encode(&vec![128u8; w * h * 3], w as u16, h as u16, jpeg_encoder::ColorType::Rgb).unwrap(); + out + }; + let image = |jpeg: &[u8]| { + let mut file = vec![0u8; 8]; + file.extend_from_slice(jpeg); + let info = ImageInfo { + width: 4, + height: 4, + bits_per_sample: vec![8, 8, 8], + samples_per_pixel: 3, + compression: comp::LOSSY_JPEG, + photometric: 34892, + planar: 1, + predictor: 1, + sample_format: 1, + new_subfile_type: 0, + layout: Layout::Tiles { tile_width: 16, tile_height: 16 }, + offsets: vec![8], + byte_counts: vec![jpeg.len() as u64], + }; + read_image(&file, &info, ByteOrder::Little, Packing::Msb) + }; + assert!(image(&encode(32, 32)).is_ok()); + assert!(matches!(image(&encode(256, 256)), Err(RawError::Corrupt(_)))); + } + fn strip_info(compression: u16, offsets: Vec, byte_counts: Vec) -> ImageInfo { ImageInfo { width: 4, diff --git a/crates/raw/src/unpack.rs b/crates/raw/src/unpack.rs index 77c69c00d5..bf7040a8a7 100644 --- a/crates/raw/src/unpack.rs +++ b/crates/raw/src/unpack.rs @@ -4,7 +4,7 @@ //! predictor), DNG 1.7 (predictors 34892–34895, bit packing, 16/24-bit floats), RFC 1950/1951 (zlib/deflate). use crate::{RawError, Result}; -use lightcraft_tiff::ByteOrder; +use craft_tiff::ByteOrder; /// Unpack `out.len()` MSB-first packed samples of `bits` (1..=16) from `src` (TIFF bit order). pub fn unpack_msb(src: &[u8], bits: u32, out: &mut [u16]) { diff --git a/crates/raw/src/vendor.rs b/crates/raw/src/vendor.rs index a44262a2c4..dea94043e5 100644 --- a/crates/raw/src/vendor.rs +++ b/crates/raw/src/vendor.rs @@ -30,7 +30,7 @@ mod sr2; pub mod srw; use crate::{BlackLevel, Cfa, Rect}; -use lightcraft_tiff::Tiff; +use craft_tiff::Tiff; use std::ops::Range; /// Exif `CFAPattern`. @@ -247,7 +247,7 @@ pub(crate) fn declare_strip_len(file: &mut [u8], len: u32, declared: u32) { #[cfg(test)] mod tests { use super::*; - use lightcraft_tiff::tags as t; + use craft_tiff::tags as t; /// `file`, whose image IFD holds one strip, as two strips at the same offset whose StripByteCounts are LONG8 (which /// classic TIFF readers accept) `[u64::MAX, 1]`: their sum is past `u64::MAX`. diff --git a/crates/raw/src/vendor/arw.rs b/crates/raw/src/vendor/arw.rs index ba1c556d7a..950e3bec47 100644 --- a/crates/raw/src/vendor/arw.rs +++ b/crates/raw/src/vendor/arw.rs @@ -48,10 +48,10 @@ use super::arw6; use crate::lens_metadata::SonyLensGeometry; use crate::tiffraw::{Packing, check_image, read_image_in}; use crate::{BlackLevel, Cfa, ColorData, LensMetadata, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result, ljpeg}; +use craft_tiff::image::{Chunk, ImageInfo, Layout, chunk_bytes}; +use craft_tiff::tags::{self as t, photometric}; +use craft_tiff::{Ifd, Tiff, makernote}; use lightcraft_geom::Orientation; -use lightcraft_tiff::image::{Chunk, ImageInfo, Layout, chunk_bytes}; -use lightcraft_tiff::tags::{self as t, photometric}; -use lightcraft_tiff::{Ifd, Tiff, makernote}; use rayon::prelude::*; const TONE_CURVE: u16 = 0x7010; @@ -226,7 +226,7 @@ fn sony_body<'a>(make: &str, model: &'a str, mn: Option<&makernote::MakerNote>) /// White balance from the maker note's enciphered `Tag2010` block, the fallback described at [`TAG2010_WB`]. /// `model` is the Sony body ([`sony_body`]); Sony appends a regional "V" to some names (SLT-A77V), which the /// documented lists omit. -fn tag2010_wb(model: &str, block: &[u8], order: lightcraft_tiff::ByteOrder) -> Option<[f32; 3]> { +fn tag2010_wb(model: &str, block: &[u8], order: craft_tiff::ByteOrder) -> Option<[f32; 3]> { let model = model.trim(); let &(_, offset) = TAG2010_WB.iter().find(|(models, _)| models.iter().any(|m| model == *m || model.strip_suffix('V') == Some(*m)))?; let bytes: Vec = block.get(offset..offset + 6)?.iter().map(|&b| DECIPHER[b as usize]).collect(); @@ -358,7 +358,9 @@ fn read_quad_tiles(bytes: &[u8], info: &ImageInfo) -> Result> { .map(|c| { let src = chunk_bytes(bytes, c).ok_or_else(|| RawError::Corrupt("tile offset past end of file".into()))?; let f = ljpeg::decode(src, (c.width as usize * c.height as usize).max(1 << 16))?; - if f.components != 4 || f.data.len() < f.width * f.height * 4 { + // a tile's frame is half its nominal width and height: a larger one (which the sample cap above would + // still let through when the tile is small) is no Sony tile + if f.components != 4 || f.data.len() < f.width * f.height * 4 || f.width * 2 > c.width as usize || f.height * 2 > c.height as usize { return Err(RawError::Corrupt(format!( "lossless ARW tile: {} samples in a {}×{}×{} frame", f.data.len(), @@ -521,7 +523,7 @@ fn sr2_old_crop(sr2: &super::sr2::SubIfd, w: usize, h: usize) -> Option { } /// One black level from the four deciphered per-channel levels (their mean), when they are near-equal and plausible. -fn sr2_black_levels(plain: &[u8], order: lightcraft_tiff::ByteOrder) -> Option { +fn sr2_black_levels(plain: &[u8], order: craft_tiff::ByteOrder) -> Option { let levels: Vec = (0..4).filter_map(|i| order.read_u16(plain, 2 * i)).collect(); let (&lo, &hi) = (levels.iter().min()?, levels.iter().max()?); (levels.len() == 4 && (64..=4096).contains(&lo) && hi - lo <= 64).then(|| levels.iter().map(|&v| f32::from(v)).sum::() / 4.0) @@ -530,8 +532,8 @@ fn sr2_black_levels(plain: &[u8], order: lightcraft_tiff::ByteOrder) -> Option lightcraft_tiff::ByteOrder { - use lightcraft_tiff::ByteOrder::{Big, Little}; +fn word16_order(strip: &[u8], bits: u32, file: craft_tiff::ByteOrder) -> craft_tiff::ByteOrder { + use craft_tiff::ByteOrder::{Big, Little}; let other = if file == Little { Big } else { Little }; let limit = 1u32 << bits.clamp(14, 16); let (mut n, mut over_file, mut over_other) = (0usize, 0usize, 0usize); @@ -834,7 +836,7 @@ mod tests { #[test] fn downsized_lossless_is_linear_rgb_not_cfa() { - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; let mut raw = IfdBuilder::new(); raw.set(t::MAKE, Value::Ascii("SONY".into())); raw.set(t::MODEL, Value::Ascii("ILCE-7M4".into())); @@ -892,7 +894,7 @@ mod tests { /// A 32767-compressed ARW whose single strip is `strip` bytes for a 32 x 4 image. fn arw_with_strip(strip: usize) -> Vec { - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; let mut raw = IfdBuilder::new(); raw.set(t::MAKE, Value::Ascii("SONY".into())); raw.set(t::MODEL, Value::Ascii("DSLR-A200".into())); @@ -921,7 +923,7 @@ mod tests { /// [`super::super::arw_lens_rm6`]). #[test] fn lens_tables_reach_opcode_list3() { - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; let file = |model: &str, ca: bool| { let mut raw = IfdBuilder::new(); raw.set(t::MAKE, Value::Ascii("SONY".into())); @@ -972,7 +974,7 @@ mod tests { /// A 32767-compressed, 12-bit ARW of `w` x `h` whose single strip is `strip`. fn packed_arw(w: u32, h: u32, strip: Vec) -> Vec { - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; let mut raw = IfdBuilder::new(); raw.set(t::MAKE, Value::Ascii("SONY".into())); raw.set(t::MODEL, Value::Ascii("DSLR-A900".into())); @@ -1109,7 +1111,7 @@ mod tests { #[test] fn tag2010_white_balance_by_model() { - use lightcraft_tiff::ByteOrder::Little; + use craft_tiff::ByteOrder::Little; let mut plain = vec![0u8; 700]; for (i, v) in [669u16, 256, 441].iter().enumerate() { plain[612 + 2 * i..614 + 2 * i].copy_from_slice(&v.to_le_bytes()); @@ -1132,8 +1134,8 @@ mod tests { /// A Sony-style file: IFD0 (`make`) → Exif → maker note `SONY DSC \0\0\0` + an IFD of `entries` (tag, type, /// count, inline value bytes), the layout of the camera's own notes; `count` overrides the entry count. - fn sony_file(order: lightcraft_tiff::ByteOrder, make: &str, entries: &[(u16, u16, u32, [u8; 4])], count: Option) -> Vec { - use lightcraft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; + fn sony_file(order: craft_tiff::ByteOrder, make: &str, entries: &[(u16, u16, u32, [u8; 4])], count: Option) -> Vec { + use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; let (u16b, u32b) = match order { ByteOrder::Little => (u16::to_le_bytes as fn(u16) -> [u8; 2], u32::to_le_bytes as fn(u32) -> [u8; 4]), ByteOrder::Big => (u16::to_be_bytes as fn(u16) -> [u8; 2], u32::to_be_bytes as fn(u32) -> [u8; 4]), @@ -1158,7 +1160,7 @@ mod tests { #[test] fn dynamic_range_optimizer_is_read_from_the_sony_note() { - use lightcraft_tiff::ByteOrder::{Big, Little}; + use craft_tiff::ByteOrder::{Big, Little}; let long = |order, v: u32| match order { Little => v.to_le_bytes(), Big => v.to_be_bytes(), @@ -1193,7 +1195,7 @@ mod tests { #[test] fn hostile_sony_notes_are_read_safely() { - use lightcraft_tiff::ByteOrder::Little; + use craft_tiff::ByteOrder::Little; let entry = [(MN_DYNAMIC_RANGE_OPTIMIZER, 4, 1, 3u32.to_le_bytes())]; let good = sony_file(Little, "SONY", &entry, None); assert_eq!(crate::embedded_preview_dynamic_range_optimized(&good), Some(true)); @@ -1209,9 +1211,9 @@ mod tests { assert_eq!(crate::embedded_preview_dynamic_range_optimized(b"not a TIFF"), None); assert_eq!(crate::embedded_preview_dynamic_range_optimized(&[]), None); // a Sony file without a maker note - let mut ifd0 = lightcraft_tiff::IfdBuilder::new(); - ifd0.set(t::MAKE, lightcraft_tiff::Value::Ascii("SONY".into())); - assert_eq!(crate::embedded_preview_dynamic_range_optimized(&lightcraft_tiff::TiffWriter::new(Little, false).write(&[ifd0]).unwrap()), None); + let mut ifd0 = craft_tiff::IfdBuilder::new(); + ifd0.set(t::MAKE, craft_tiff::Value::Ascii("SONY".into())); + assert_eq!(crate::embedded_preview_dynamic_range_optimized(&craft_tiff::TiffWriter::new(Little, false).write(&[ifd0]).unwrap()), None); } #[test] @@ -1236,7 +1238,7 @@ mod tests { /// (reached through `DNGPrivateData` and the `SR2Private` IFD, as Sony stores it, with `key`; black level 800) /// and a maker note whose `Tag2010` block (RX100 III layout) holds `WB_RGBLevels`. fn arw_with_wb(plain: Option<[u16; 4]>, sr2: Option<[u16; 4]>, tag2010: Option<[u16; 3]>, key: [u8; 4]) -> Vec { - use lightcraft_tiff::{ByteOrder::Little, IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{ByteOrder::Little, IfdBuilder, ImageData, TiffWriter, Value}; let mut tag2010_block = vec![0u8; 700]; for (i, v) in tag2010.unwrap_or([0; 3]).iter().enumerate() { tag2010_block[612 + 2 * i..614 + 2 * i].copy_from_slice(&v.to_le_bytes()); @@ -1305,7 +1307,7 @@ mod tests { /// balance; without the ID, or with one that isn't listed, there is no layout to read. #[test] fn rebadged_body_takes_its_tag2010_layout_from_the_sony_model_id() { - use lightcraft_tiff::{ByteOrder::Little, IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{ByteOrder::Little, IfdBuilder, ImageData, TiffWriter, Value}; let file = |make: &str, model: &str, id: Option| { let mut block = vec![0u8; 4600]; for (i, v) in [639u16, 256, 368].iter().enumerate() { @@ -1369,7 +1371,7 @@ mod tests { #[test] fn dsc_r1_sr2_subifd_gives_black_wb_and_crop() { - use lightcraft_tiff::ByteOrder::Little; + use craft_tiff::ByteOrder::Little; let key = [0x63, 0xbe, 0x01, 0x3e]; let enc = |crop: [u16; 4], wb: [u16; 4]| { let b = super::super::sr2::tests::block_keyed( @@ -1451,7 +1453,7 @@ mod tests { #[test] fn sr2_black_level_decodes_and_rejects_garbage() { - use lightcraft_tiff::ByteOrder::Little; + use craft_tiff::ByteOrder::Little; let plain = |levels: [u16; 4]| -> Vec { levels.iter().flat_map(|v| v.to_le_bytes()).collect() }; assert_eq!(sr2_black_levels(&plain([800; 4]), Little), Some(800.0)); assert_eq!(sr2_black_levels(&plain([512, 512, 514, 512]), Little), Some(512.5)); @@ -1484,13 +1486,13 @@ mod tests { } /// [`sr2_black`] on the encrypted `SR2SubIFD` bytes `block`, which start at file offset `start`. - fn sr2_black_in(block: &[u8], start: usize, order: lightcraft_tiff::ByteOrder) -> Option { + fn sr2_black_in(block: &[u8], start: usize, order: craft_tiff::ByteOrder) -> Option { sr2_black(&super::super::sr2::SubIfd::decrypt(block, start, order)) } #[test] fn sr2_black_level_follows_the_first_entry_to_its_layout() { - use lightcraft_tiff::ByteOrder::Little; + use craft_tiff::ByteOrder::Little; let start = 37584; // the value position of every layout seen, e.g. the DSLR-A700's (62112 bytes, value at 1638) and the // DSLR-A450/A500/A550's (27152 bytes, value at 2166); the first four are those of bodies without a plain 0x7310 @@ -1519,7 +1521,7 @@ mod tests { #[test] fn word16_byte_order_follows_the_sample_range() { - use lightcraft_tiff::ByteOrder::{Big, Little}; + use craft_tiff::ByteOrder::{Big, Little}; let be: Vec = (0..4096u16).flat_map(|i| (512 + i * 3).to_be_bytes()).collect(); // 512..12800 big-endian words assert_eq!(word16_order(&be, 14, Little), Big); let le: Vec = (0..4096u16).flat_map(|i| (512 + i * 3).to_le_bytes()).collect(); @@ -1582,7 +1584,7 @@ mod tests { /// decode allocates the frame. #[test] fn a_strip_cut_short_is_refused_before_allocating() { - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; let (w, h) = (3000u32, 2000u32); for declared in [w * h, w * h / 2 * 3] { let mut raw = IfdBuilder::new(); diff --git a/crates/raw/src/vendor/arw6.rs b/crates/raw/src/vendor/arw6.rs index 9837f3e7be..b8ee711732 100644 --- a/crates/raw/src/vendor/arw6.rs +++ b/crates/raw/src/vendor/arw6.rs @@ -543,9 +543,9 @@ mod tests { use super::*; use crate::vendor::arw6_testenc::*; use crate::vendor::pef::Bits; - use lightcraft_tiff::Tiff; - use lightcraft_tiff::image::chunk_bytes; - use lightcraft_tiff::tags as t; + use craft_tiff::Tiff; + use craft_tiff::image::chunk_bytes; + use craft_tiff::tags as t; fn planes(w2: usize, h2: usize, seed: u64) -> (Plane, Plane, Plane, Plane) { let mut x = seed.max(1); diff --git a/crates/raw/src/vendor/arw6_testenc.rs b/crates/raw/src/vendor/arw6_testenc.rs index 5493311237..47d93a5639 100644 --- a/crates/raw/src/vendor/arw6_testenc.rs +++ b/crates/raw/src/vendor/arw6_testenc.rs @@ -2,8 +2,8 @@ //! Section names in italics refer to `docs/arw6-compression.md`. use crate::llvc::{Bands3, Plane, band_rows}; -use lightcraft_tiff::tags as t; -use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; +use craft_tiff::tags as t; +use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; /// Exact inverse of `dequant` on representable values: `sign(v) * (|v| >> qi)`. pub(crate) fn quantize(v: i32, qi: u32) -> i32 { diff --git a/crates/raw/src/vendor/arw_lens.rs b/crates/raw/src/vendor/arw_lens.rs index 4cebf221ec..94f1b67883 100644 --- a/crates/raw/src/vendor/arw_lens.rs +++ b/crates/raw/src/vendor/arw_lens.rs @@ -25,7 +25,7 @@ use crate::lens_metadata::{ SonySource, }; use crate::{Opcode, Rect}; -use lightcraft_tiff::{ByteOrder, FieldType, Ifd, Value}; +use craft_tiff::{ByteOrder, FieldType, Ifd, Value}; /// Each component's setting and table tags, `(raw IFD, SR2SubIFD)`. const SHADING: Tags = Tags { setting: (0x7031, 0x797c), table: (0x7032, 0x797d) }; @@ -545,8 +545,8 @@ mod tests { use crate::lens_metadata::SonyCopy::{Absent, Rejected, Valid}; use crate::lens_metadata::SonyFieldError::*; use crate::{LensMetadata, Mode, RawImage}; - use lightcraft_tiff::ByteOrder::{Big, Little}; - use lightcraft_tiff::{IfdBuilder, ImageData, Tiff, TiffWriter, Value, tags as t}; + use craft_tiff::ByteOrder::{Big, Little}; + use craft_tiff::{IfdBuilder, ImageData, Tiff, TiffWriter, Value, tags as t}; const TELE: [i16; 16] = [-2, 1, 7, 17, 31, 48, 68, 93, 120, 152, 187, 225, 265, 309, 356, 403]; const WIDE: [i16; 16] = [14, 0, -27, -59, -106, -158, -223, -291, -367, -440, -519, -593, -667, -731, -792, -839]; diff --git a/crates/raw/src/vendor/arw_lens_rm6.rs b/crates/raw/src/vendor/arw_lens_rm6.rs index 5a215ef7a2..e537e009ea 100644 --- a/crates/raw/src/vendor/arw_lens_rm6.rs +++ b/crates/raw/src/vendor/arw_lens_rm6.rs @@ -47,7 +47,7 @@ use super::fit_radial_warp; use crate::opcodes::max_radius; use crate::{Opcode, Rect}; -use lightcraft_tiff::Ifd; +use craft_tiff::Ifd; use std::ops::RangeInclusive; pub(crate) const DISTORTION_PARAMS: u16 = 0x7037; @@ -272,7 +272,7 @@ pub(crate) fn lens_opcodes(raw: &Ifd, model: &str, lens: Option<&str>, focal_mm: #[cfg(test)] mod tests { use super::*; - use lightcraft_tiff::{Entry, Value}; + use craft_tiff::{Entry, Value}; /// The FF 16–35 mm example (corpus `arw-sony-ilce7rm6-ff-land-lossless.arw`, docs/sony-ilce7rm6-lens-corrections.md). const FF_DISTORTION: [i16; 17] = [16, 0, 2, 7, 16, 28, 43, 61, 83, 109, 139, 175, 217, 267, 325, 395, 479]; diff --git a/crates/raw/src/vendor/cr2.rs b/crates/raw/src/vendor/cr2.rs index 6cab70d2fa..83367e2324 100644 --- a/crates/raw/src/vendor/cr2.rs +++ b/crates/raw/src/vendor/cr2.rs @@ -27,9 +27,9 @@ mod sraw; use super::{active_rows, black_from_columns, white_from_data}; use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result, ljpeg}; +use craft_tiff::image::chunk_bytes; +use craft_tiff::{ByteOrder, Ifd, Tiff, Value, makernote, tags as t}; use lightcraft_geom::Orientation; -use lightcraft_tiff::image::chunk_bytes; -use lightcraft_tiff::{ByteOrder, Ifd, Tiff, Value, makernote, tags as t}; use std::ops::Range; const CR2_SLICE: u16 = 0xc640; @@ -192,9 +192,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result { Some(v) if v != 1 => return Err(RawError::Unsupported(format!("Canon raw type {v}"))), _ => {} } - let off = raw.u64(t::STRIP_OFFSETS).ok_or(RawError::Tiff(lightcraft_tiff::TiffError::MissingTag(t::STRIP_OFFSETS)))?; + let off = raw.u64(t::STRIP_OFFSETS).ok_or(RawError::Tiff(craft_tiff::TiffError::MissingTag(t::STRIP_OFFSETS)))?; let len = raw.u64(t::STRIP_BYTE_COUNTS).unwrap_or(bytes.len() as u64 - off.min(bytes.len() as u64)); - let chunk = lightcraft_tiff::image::Chunk { index: 0, x: 0, y: 0, width: 0, height: 0, plane: 0, offset: off, len }; + let chunk = craft_tiff::image::Chunk { index: 0, x: 0, y: 0, width: 0, height: 0, plane: 0, offset: off, len }; let src = chunk_bytes(bytes, &chunk).ok_or_else(|| RawError::Corrupt("raw strip outside file".into()))?; let (fw, fh, nc, prec) = ljpeg::frame_info(src)?; let frame = match mode { @@ -306,7 +306,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result { #[cfg(test)] pub(crate) mod tests { use super::*; - use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; /// Build a synthetic CR2: IFD0..IFD2 placeholders + IFD3 holding a sliced 2-component LJ92 frame. pub(crate) fn synthetic_cr2(w: usize, h: usize, slices: Option<[u16; 3]>) -> (Vec, Vec) { diff --git a/crates/raw/src/vendor/cr2/sraw.rs b/crates/raw/src/vendor/cr2/sraw.rs index 6a37d627e5..742ce3a58a 100644 --- a/crates/raw/src/vendor/cr2/sraw.rs +++ b/crates/raw/src/vendor/cr2/sraw.rs @@ -28,9 +28,9 @@ use super::{COLOR_BALANCE, CR2_SLICE, SENSOR_INFO, words}; use crate::{BlackLevel, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result, ljpeg}; +use craft_tiff::image::{Chunk, chunk_bytes}; +use craft_tiff::{ByteOrder, Ifd, Tiff, makernote, tags as t}; use lightcraft_geom::Orientation; -use lightcraft_tiff::image::{Chunk, chunk_bytes}; -use lightcraft_tiff::{ByteOrder, Ifd, Tiff, makernote, tags as t}; use rayon::prelude::*; /// Chroma samples are centred on this value. @@ -335,7 +335,7 @@ fn colour_data_black(words: &[u64]) -> Option { /// Decode a Canon sRAW / mRAW CR2 into linear white-balanced camera RGB (`cpp` 3). pub(super) fn decode(bytes: &[u8], tiff: &Tiff, raw: &Ifd, mode: Mode) -> Result { let ifd0 = &tiff.ifds[0]; - let off = raw.u64(t::STRIP_OFFSETS).ok_or(RawError::Tiff(lightcraft_tiff::TiffError::MissingTag(t::STRIP_OFFSETS)))?; + let off = raw.u64(t::STRIP_OFFSETS).ok_or(RawError::Tiff(craft_tiff::TiffError::MissingTag(t::STRIP_OFFSETS)))?; let len = raw.u64(t::STRIP_BYTE_COUNTS).unwrap_or(bytes.len() as u64 - off.min(bytes.len() as u64)); let chunk = Chunk { index: 0, x: 0, y: 0, width: 0, height: 0, plane: 0, offset: off, len }; let src = chunk_bytes(bytes, &chunk).ok_or_else(|| RawError::Corrupt("raw strip outside file".into()))?; @@ -403,7 +403,7 @@ pub(super) fn decode(bytes: &[u8], tiff: &Tiff, raw: &Ifd, mode: Mode) -> Result #[cfg(test)] mod tests { use super::*; - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; const SRAW_TYPE_TAG: u16 = 0xc6c5; diff --git a/crates/raw/src/vendor/cr3.rs b/crates/raw/src/vendor/cr3.rs index 6136573245..8a0a54c8c6 100644 --- a/crates/raw/src/vendor/cr3.rs +++ b/crates/raw/src/vendor/cr3.rs @@ -20,8 +20,8 @@ use super::{black_from_columns, crx, white_from_data}; use crate::{BlackLevel, Cfa, ColorData, MAX_SAMPLES, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result}; +use craft_tiff::{Ifd, Tiff}; use lightcraft_meta::cr3::{Cr3ImageArea, Cr3Track, Cr3TrackKind, parse_cr3}; -use lightcraft_tiff::{Ifd, Tiff}; const SENSOR_INFO: u16 = 0x00e0; const COLOR_DATA: u16 = 0x4001; @@ -215,10 +215,22 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result { let maker = static_maker.as_ref().and_then(|m| m.ifds.first()); // Retain only the first useful timed maker note: repeated CTMD records must not accumulate // independent copies of every bounded TIFF value in memory. + // Records aliasing the same bytes are parsed once, and all parses share a budget of twice the file (at least 1 MiB); + // a record past what is left is skipped, a later smaller one still parsed. + let mut seen = std::collections::HashSet::new(); + let mut left = bytes.len().saturating_mul(2).max(1 << 20); let timed_maker = container .timed_exif(bytes) .into_iter() .filter(|b| b.tag == 0x927c) + .filter(|b| seen.insert((b.data.as_ptr() as usize, b.data.len()))) + .filter(|b| match left.checked_sub(b.data.len()) { + Some(rest) => { + left = rest; + true + } + None => false, + }) .filter_map(|b| Tiff::parse(b.data).ok()) .find(|t| t.ifds.first().is_some_and(|m| m.contains(COLOR_DATA))); let color_maker = timed_maker.as_ref().and_then(|t| t.ifds.first()).or(maker); @@ -270,7 +282,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result { #[cfg(test)] mod tests { use super::*; - use lightcraft_tiff::{IfdBuilder, Value}; + use craft_tiff::{IfdBuilder, Value}; fn maker(version: u16, wb_at: usize, black_at: usize, white_at: usize) -> Ifd { let mut words = vec![0u16; white_at + 1]; @@ -279,7 +291,7 @@ mod tests { words[black_at..black_at + 4].copy_from_slice(&[100, 101, 102, 103]); words[white_at] = 15000; let b = IfdBuilder::new().with(COLOR_DATA, Value::Short(words)); - let bytes = lightcraft_tiff::TiffWriter::new(lightcraft_tiff::ByteOrder::Little, false).write(&[b]).unwrap(); + let bytes = craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[b]).unwrap(); Tiff::parse(&bytes).unwrap().ifds.remove(0) } @@ -309,7 +321,7 @@ mod tests { } fn synthetic_header_with(aspect: Option<[u32; 5]>) -> Vec { - use lightcraft_tiff::{ByteOrder, TiffWriter, tags as t}; + use craft_tiff::{ByteOrder, TiffWriter, tags as t}; let tiff = |b: IfdBuilder| TiffWriter::new(ByteOrder::Little, false).write(&[b]).unwrap(); let cmt1 = tiff( IfdBuilder::new() @@ -448,8 +460,8 @@ mod tests { /// and `AspectInfo` applies from its origin. #[test] fn aspect_info_needs_a_consistent_recommended_crop() { - let aspect = lightcraft_tiff::IfdBuilder::new().with(ASPECT_INFO, Value::Long(vec![13, 5088, 3392, 0, 0])); - let bytes = lightcraft_tiff::TiffWriter::new(lightcraft_tiff::ByteOrder::Little, false).write(&[aspect]).unwrap(); + let aspect = craft_tiff::IfdBuilder::new().with(ASPECT_INFO, Value::Long(vec![13, 5088, 3392, 0, 0])); + let bytes = craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[aspect]).unwrap(); let maker = Tiff::parse(&bytes).unwrap().ifds.remove(0); let area = |crop| Cr3ImageArea { width: 5376, height: 3574, crop, active: Some([132, 160, 5243, 3567]), masked_left: [0; 4], masked_top: None }; diff --git a/crates/raw/src/vendor/erf.rs b/crates/raw/src/vendor/erf.rs index 7f88dae2af..0a1316448b 100644 --- a/crates/raw/src/vendor/erf.rs +++ b/crates/raw/src/vendor/erf.rs @@ -20,9 +20,9 @@ //! - The preview is the maker note's tag `0x0280`, a JPEG whose first byte is stored as `ee` (found by the //! structural scan in `preview::scan_for_jpeg`). use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result}; +use craft_tiff::image::chunk_bytes; +use craft_tiff::{Ifd, Tiff, tags as t}; use lightcraft_geom::Orientation; -use lightcraft_tiff::image::chunk_bytes; -use lightcraft_tiff::{Ifd, Tiff, tags as t}; /// Epson's private compression code for the 16-byte block layout. const BLOCKS16: u16 = 32769; @@ -58,8 +58,8 @@ fn maker_note(bytes: &[u8], tiff: &Tiff) -> Option { if !bytes.get(start..)?.starts_with(b"EPSON\0") { return None; } - let opts = lightcraft_tiff::ParseOptions { max_ifds: 1, max_depth: 0, follow_children: false, ..Default::default() }; - lightcraft_tiff::parse_ifd_at(bytes, e.offset.checked_add(8)?, tiff.order, 0, false, &opts).ok().map(|(i, _)| i) + let opts = craft_tiff::ParseOptions { max_ifds: 1, max_depth: 0, follow_children: false, ..Default::default() }; + craft_tiff::parse_ifd_at(bytes, e.offset.checked_add(8)?, tiff.order, 0, false, &opts).ok().map(|(i, _)| i) } /// Unpack a strip of 16-byte blocks (15 bytes of ten MSB-first 12-bit samples, one pad byte). @@ -137,7 +137,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result { #[cfg(test)] mod tests { use super::*; - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; /// Pack ten-sample groups into 16-byte blocks, MSB first, zero pad. fn pack(px: &[u16]) -> Vec { @@ -168,7 +168,7 @@ mod tests { ifd0.set(t::MODEL, Value::Ascii("R-D1".into())); ifd0.set(t::ORIENTATION, Value::Short(vec![1])); ifd0.add_sub_ifd(raw); - TiffWriter::new(lightcraft_tiff::ByteOrder::Big, false).write(&[ifd0]).unwrap() + TiffWriter::new(craft_tiff::ByteOrder::Big, false).write(&[ifd0]).unwrap() } #[test] diff --git a/crates/raw/src/vendor/hasselblad.rs b/crates/raw/src/vendor/hasselblad.rs index 5f3342f9ff..9ec21d85c9 100644 --- a/crates/raw/src/vendor/hasselblad.rs +++ b/crates/raw/src/vendor/hasselblad.rs @@ -25,8 +25,8 @@ //! [`rgb_preview`] hands the largest 8-bit one on as a small baseline TIFF. use crate::dng; use crate::{ColorData, Mode, RawFormat, RawImage, Result}; -use lightcraft_tiff::image::{Layout, chunk_bytes}; -use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, ParseOptions, Tiff, TiffWriter, Value, tags as t}; +use craft_tiff::image::{Layout, chunk_bytes}; +use craft_tiff::{ByteOrder, IfdBuilder, ImageData, ParseOptions, Tiff, TiffWriter, Value, tags as t}; /// Private IFD0 tag holding the original 3FR header (a complete TIFF) inside an FFF. pub(crate) const ORIGINAL_HEADER: u16 = 0xc51b; @@ -124,7 +124,7 @@ fn adopt_matrices(color: &mut ColorData, from: &ColorData) { mod tests { use super::*; use crate::{RawData, RawError, decode as decode_file, probe, probe_info}; - use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value, tags as t}; + use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value, tags as t}; const MATRIX: [(i32, i32); 9] = [(686, 1000), (-196, 1000), (-32, 1000), (-437, 1000), (1228, 1000), (235, 1000), (-165, 1000), (317, 1000), (675, 1000)]; diff --git a/crates/raw/src/vendor/kodak.rs b/crates/raw/src/vendor/kodak.rs index 1e972873e5..5d77108ffc 100644 --- a/crates/raw/src/vendor/kodak.rs +++ b/crates/raw/src/vendor/kodak.rs @@ -24,9 +24,9 @@ //! each other) the raw G/R is 0.94 and G/B 1.96; `0x848` (1913, 1834, 921) gives 0.96 and 1.99, the others //! 0.77..1.25 and 2.08..2.96, so its green over red and over blue are the gains. use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result, ljpeg}; +use craft_tiff::image::chunk_bytes; +use craft_tiff::{Ifd, Tiff, tags as t}; use lightcraft_geom::Orientation; -use lightcraft_tiff::image::chunk_bytes; -use lightcraft_tiff::{Ifd, Tiff, tags as t}; /// The vendor IFD pointer (`KodakIFD`). const KODAK_IFD: u16 = 0x8290; @@ -127,8 +127,8 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result { /// The vendor IFD (`KodakIFD`): a plain IFD with absolute value offsets. fn kodak_ifd(bytes: &[u8], tiff: &Tiff, ifd0: &Ifd) -> Option { let off = u64::from(ifd0.u32(KODAK_IFD)?); - let opts = lightcraft_tiff::ParseOptions { max_ifds: 4, max_depth: 1, follow_children: false, ..Default::default() }; - lightcraft_tiff::parse_ifd_at(bytes, off, tiff.order, 0, false, &opts).ok().map(|(i, _)| i) + let opts = craft_tiff::ParseOptions { max_ifds: 4, max_depth: 1, follow_children: false, ..Default::default() }; + craft_tiff::parse_ifd_at(bytes, off, tiff.order, 0, false, &opts).ok().map(|(i, _)| i) } /// Black level per CFA position (the mosaic's first sample is position 0): the value below which only 0.01% of that @@ -167,7 +167,7 @@ fn measured_black(d: &[u16], w: usize) -> BlackLevel { mod tests { use super::*; use crate::{decode, probe, probe_info}; - use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; const W: usize = 16; const H: usize = 8; diff --git a/crates/raw/src/vendor/mef.rs b/crates/raw/src/vendor/mef.rs index d1c9a7754d..8755e9e06c 100644 --- a/crates/raw/src/vendor/mef.rs +++ b/crates/raw/src/vendor/mef.rs @@ -16,9 +16,9 @@ //! - No white-balance tag was found (the maker note holds an ICC-like profile), so the as-shot gains are measured //! from that preview's neutral pixels ([`crate::wb_estimate`]). use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result}; +use craft_tiff::image::chunk_bytes; +use craft_tiff::{Ifd, Tiff, tags as t}; use lightcraft_geom::Orientation; -use lightcraft_tiff::image::chunk_bytes; -use lightcraft_tiff::{Ifd, Tiff, tags as t}; /// Where the preview's window starts in the sensor frame, and how many sensor pixels one preview pixel covers. const CROP_ORIGIN: (usize, usize) = (24, 12); @@ -142,7 +142,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result { #[cfg(test)] mod tests { use super::*; - use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; fn pack(px: &[u16]) -> Vec { px.chunks(2).flat_map(|p| [(p[0] >> 4) as u8, ((p[0] & 15) << 4) as u8 | (p[1] >> 8) as u8, p[1] as u8]).collect() diff --git a/crates/raw/src/vendor/mrw.rs b/crates/raw/src/vendor/mrw.rs index 6404ec9aa2..a59de3d3ef 100644 --- a/crates/raw/src/vendor/mrw.rs +++ b/crates/raw/src/vendor/mrw.rs @@ -24,8 +24,8 @@ //! - `TTW` is a TIFF with IFD0 (orientation), Exif and the maker note; the preview JPEG sits in it with its first //! byte overwritten (see `preview::scan_for_jpeg`). use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result, unpack}; +use craft_tiff::{Tiff, tags as t}; use lightcraft_geom::Orientation; -use lightcraft_tiff::{Tiff, tags as t}; /// Storage method codes in `PRD`. const PACKED_12: u8 = 0x59; @@ -80,7 +80,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result { if p.packed { unpack::unpack_msb(src, 12, &mut d); } else { - unpack::read_u16s(src, lightcraft_tiff::ByteOrder::Big, &mut d); + unpack::read_u16s(src, craft_tiff::ByteOrder::Big, &mut d); for v in &mut d { *v = (*v).min(4095); } @@ -154,10 +154,10 @@ pub(crate) mod tests { for v in wbg { wb.extend_from_slice(&v.to_be_bytes()); } - let mut ifd0 = lightcraft_tiff::IfdBuilder::new(); - ifd0.set(t::MAKE, lightcraft_tiff::Value::Ascii("Minolta Co., Ltd.".into())); - ifd0.set(t::ORIENTATION, lightcraft_tiff::Value::Short(vec![6])); - let ttw = lightcraft_tiff::TiffWriter::default().write(&[ifd0]).unwrap(); + let mut ifd0 = craft_tiff::IfdBuilder::new(); + ifd0.set(t::MAKE, craft_tiff::Value::Ascii("Minolta Co., Ltd.".into())); + ifd0.set(t::ORIENTATION, craft_tiff::Value::Short(vec![6])); + let ttw = craft_tiff::TiffWriter::default().write(&[ifd0]).unwrap(); let mut body = Vec::new(); for (name, data) in [(b"PRD", &prd), (b"WBG", &wb), (b"TTW", &ttw)] { body.push(0); diff --git a/crates/raw/src/vendor/nef.rs b/crates/raw/src/vendor/nef.rs index 10bb24d501..916acae60f 100644 --- a/crates/raw/src/vendor/nef.rs +++ b/crates/raw/src/vendor/nef.rs @@ -15,10 +15,10 @@ use super::{nefc, white_from_data}; use crate::tiffraw::{Packing, check_image, read_image}; use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result}; +use craft_tiff::image::ImageInfo; +use craft_tiff::tags::{self as t, photometric}; +use craft_tiff::{ByteOrder, Ifd, Tiff, makernote}; use lightcraft_geom::Orientation; -use lightcraft_tiff::image::ImageInfo; -use lightcraft_tiff::tags::{self as t, photometric}; -use lightcraft_tiff::{ByteOrder, Ifd, Tiff, makernote}; const WB_RB_LEVELS: u16 = 0x000c; const BLACK_LEVEL: u16 = 0x003d; @@ -500,7 +500,7 @@ fn packed(bytes: &[u8], info: &ImageInfo, rows: usize) -> Result { let (Some(layout), [c]) = (layout, chunks.as_slice()) else { return Err(RawError::Unsupported("Nikon compressed NEF without a linearization table (maker note 0x96)".into())); }; - let src = lightcraft_tiff::image::chunk_bytes(bytes, c) + let src = craft_tiff::image::chunk_bytes(bytes, c) .filter(|src| u64::try_from(src.len()).ok() == Some(c.len)) .ok_or_else(|| RawError::Corrupt("NEF: incomplete image strip".into()))?; if (w as u64).saturating_mul(h as u64) > crate::MAX_SAMPLES as u64 { @@ -538,7 +538,7 @@ const SMALL_RAW_B_GAIN: f32 = 4.4; /// The strip of a small raw: compression 34713, 12 bits, no maker note `0x0096`, one strip of exactly /// `3 * width * height` bytes (even width). -fn small_raw_strip(info: &ImageInfo, file_len: usize, mn: Option<&makernote::MakerNote>) -> Option { +fn small_raw_strip(info: &ImageInfo, file_len: usize, mn: Option<&makernote::MakerNote>) -> Option { if info.compression != t::compression::NIKON || info.bits() != 12 || info.samples_per_pixel != 1 { return None; } @@ -584,12 +584,12 @@ fn small_raw_row(src: &[u8], w: usize, out: &mut [u16]) { } } -fn small_raw(bytes: &[u8], tiff: &Tiff, info: &ImageInfo, strip: lightcraft_tiff::image::Chunk, mode: Mode) -> Result { +fn small_raw(bytes: &[u8], tiff: &Tiff, info: &ImageInfo, strip: craft_tiff::image::Chunk, mode: Mode) -> Result { use rayon::prelude::*; let (w, h) = (info.width as usize, info.height as usize); let stride = w.checked_mul(3).ok_or(RawError::Limit("image too large"))?; let count = stride.checked_mul(h).filter(|n| *n > 0 && *n <= crate::MAX_SAMPLES).ok_or(RawError::Limit("image too large"))?; - let src = lightcraft_tiff::image::chunk_bytes(bytes, &strip) + let src = craft_tiff::image::chunk_bytes(bytes, &strip) .filter(|src| u64::try_from(src.len()).ok() == Some(strip.len)) .ok_or_else(|| RawError::Corrupt("NEF: incomplete image strip".into()))?; let data = match mode { @@ -634,7 +634,7 @@ fn small_raw(bytes: &[u8], tiff: &Tiff, info: &ImageInfo, strip: lightcraft_tiff #[cfg(test)] mod tests { use super::*; - use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; fn nef(compression: u16, bits: u16, strips: Vec>, w: u32, h: u32, rps: u32) -> Vec { nef_with_note(compression, bits, strips, w, h, rps, None) @@ -1397,7 +1397,7 @@ mod tests { let raw = raw_ifd(&tiff).unwrap(); let info = raw.image().unwrap(); let chunk = info.chunks(bytes.len() as u64)[0]; - let data = lightcraft_tiff::image::chunk_bytes(&bytes, &chunk).unwrap().to_vec(); + let data = craft_tiff::image::chunk_bytes(&bytes, &chunk).unwrap().to_vec(); let offset_entry = raw.get(t::STRIP_OFFSETS).unwrap().offset as usize; let start = bytes.len() as u32; bytes[offset_entry..offset_entry + 4].copy_from_slice(&start.to_be_bytes()); diff --git a/crates/raw/src/vendor/nef_lens.rs b/crates/raw/src/vendor/nef_lens.rs index c5e9490ebd..ee320c36d1 100644 --- a/crates/raw/src/vendor/nef_lens.rs +++ b/crates/raw/src/vendor/nef_lens.rs @@ -8,7 +8,7 @@ //! No lens database or external decoder code. use crate::{Opcode, Rect}; -use lightcraft_tiff::{ByteOrder, Ifd, ParseOptions, parse_ifd_at}; +use craft_tiff::{ByteOrder, Ifd, ParseOptions, parse_ifd_at}; const NEF_INFO: u16 = 0xc7d5; const DISTORTION_INFO: u16 = 0x0005; @@ -38,7 +38,7 @@ fn rational(b: &[u8], at: usize, order: ByteOrder) -> Option { /// The NikonNEFInfo entries: the blob is `"Nikon\0"`, a 4-byte version, then a little TIFF stream. fn nef_info(raw: &Ifd) -> Option<(Ifd, ByteOrder)> { let tiff = raw.bytes(NEF_INFO)?.strip_prefix(b"Nikon\0")?.get(4..)?; - let (order, false) = lightcraft_tiff::Tiff::sniff(tiff)? else { return None }; + let (order, false) = craft_tiff::Tiff::sniff(tiff)? else { return None }; let first = order.u32(tiff.get(4..8)?.try_into().ok()?); let opts = ParseOptions { follow_children: false, ..Default::default() }; let (ifd, _) = parse_ifd_at(tiff, u64::from(first), order, 0, false, &opts).ok()?; @@ -121,7 +121,7 @@ pub(super) fn distortion(model: &str, raw: &Ifd, active: Rect, crop: Rect) -> Op #[cfg(test)] pub(in crate::vendor) mod tests { use super::*; - use lightcraft_tiff::{Entry, IfdBuilder, TiffWriter, Value}; + use craft_tiff::{Entry, IfdBuilder, TiffWriter, Value}; const AREA: Rect = Rect { x: 0, y: 0, width: 6064, height: 4040 }; const CROP: Rect = Rect { x: 8, y: 8, width: 6048, height: 4024 }; diff --git a/crates/raw/src/vendor/nefc.rs b/crates/raw/src/vendor/nefc.rs index fd8e964b7f..27c7467f9b 100644 --- a/crates/raw/src/vendor/nefc.rs +++ b/crates/raw/src/vendor/nefc.rs @@ -58,7 +58,7 @@ use super::pef::{Bits, Huffman, diff}; use crate::{MAX_SAMPLES, RawError, Result}; -use lightcraft_tiff::ByteOrder; +use craft_tiff::ByteOrder; /// A table code word whose symbol was never observed (see the module docs). const UNSEEN: u8 = u8::MAX; @@ -633,8 +633,8 @@ mod tests { /// A minimal NEF: CFA SubIFD with one compressed strip, Exif maker note (`Nikon\0` v2 header + embedded TIFF in /// `mn_order`) holding the linearization table. fn nef_file(strip: Vec, w: u32, h: u32, bits: u16, table: Vec, mn_order: ByteOrder) -> Vec { - use lightcraft_tiff::tags::{self as t, photometric}; - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::tags::{self as t, photometric}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; let mut mn = IfdBuilder::new(); mn.set(0x0096, Value::Undefined(table)); let mut note = b"Nikon\0\x02\x10\0\0".to_vec(); diff --git a/crates/raw/src/vendor/orf.rs b/crates/raw/src/vendor/orf.rs index 8c2bd28398..78cfdbe54b 100644 --- a/crates/raw/src/vendor/orf.rs +++ b/crates/raw/src/vendor/orf.rs @@ -40,10 +40,10 @@ use super::{cfa_from_exif, white_from_data}; use crate::tiffraw::{Packing, read_image}; use crate::unpack::unpack_msb; use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result}; +use craft_tiff::image::chunk_bytes; +use craft_tiff::makernote::MakerNote; +use craft_tiff::{Ifd, Tiff, Value, makernote, tags as t}; use lightcraft_geom::Orientation; -use lightcraft_tiff::image::chunk_bytes; -use lightcraft_tiff::makernote::MakerNote; -use lightcraft_tiff::{Ifd, Tiff, Value, makernote, tags as t}; use rayon::prelude::*; pub(crate) const CAMERA_SETTINGS: u16 = 0x2020; @@ -73,8 +73,8 @@ pub(crate) fn sub_ifd(bytes: &[u8], mn: &MakerNote, tag: u16) -> Option { Value::Undefined(_) | Value::Byte(_) => e.offset, _ => mn.base.checked_add(mn.ifd.u64(tag)?)?, }; - let opts = lightcraft_tiff::ParseOptions { max_ifds: 4, max_depth: 1, follow_children: false, ..Default::default() }; - lightcraft_tiff::parse_ifd_at(bytes, at, mn.order, mn.base, false, &opts).ok().map(|(i, _)| i) + let opts = craft_tiff::ParseOptions { max_ifds: 4, max_depth: 1, follow_children: false, ..Default::default() }; + craft_tiff::parse_ifd_at(bytes, at, mn.order, mn.base, false, &opts).ok().map(|(i, _)| i) } /// Black level of a body without ImageProcessing: the main note's `0x1012` BlackLevel (the mean of its four values) @@ -365,7 +365,7 @@ pub(crate) fn preview(bytes: &[u8]) -> Option<&[u8]> { mod tests { use super::*; use crate::vendor::EXIF_CFA_PATTERN; - use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter}; + use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter}; fn orf(w: u32, h: u32, bits: u16, strip: Vec) -> Vec { orf_with(w, h, bits, strip, None) diff --git a/crates/raw/src/vendor/pef.rs b/crates/raw/src/vendor/pef.rs index 45efeec685..466ccdd023 100644 --- a/crates/raw/src/vendor/pef.rs +++ b/crates/raw/src/vendor/pef.rs @@ -19,9 +19,9 @@ use super::{black_from_columns, cfa_from_exif, white_from_data}; use crate::tiffraw::{Packing, read_image_in}; use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result}; +use craft_tiff::image::{ImageInfo, chunk_bytes}; +use craft_tiff::{ByteOrder, Tiff, makernote, tags as t}; use lightcraft_geom::Orientation; -use lightcraft_tiff::image::{ImageInfo, chunk_bytes}; -use lightcraft_tiff::{ByteOrder, Tiff, makernote, tags as t}; const CROP_ORIGIN: u16 = 0x0038; const CROP_SIZE: u16 = 0x0039; @@ -523,7 +523,7 @@ mod tests { /// `CFAPattern` `pattern` and an AOC maker note with the area tags (and, for 65535, a Huffman table). The samples /// are plain 16-bit words (1), packed 12-bit (32773) or Huffman coded (65535). fn pef_with_pattern(compression: u16, pattern: [u8; 4]) -> Vec { - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; let (w, h) = (16usize, 8usize); let px: Vec = (0..w * h).map(|i| ((i * 37) % 900 + 100) as u16).collect(); let (tb, codes) = test_table(); @@ -589,7 +589,7 @@ mod tests { /// A `w x h` big-endian 14-bit PEF of plain 16-bit words without area tags, whose maker note has only a /// BlackPoint of `black`, with `px(x, y)` as the samples. fn pef_with_black_point(w: usize, h: usize, black: u16, px: impl Fn(usize, usize) -> u16) -> Vec { - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; let strip: Vec = (0..w * h).flat_map(|i| px(i % w, i / w).to_be_bytes()).collect(); let build = |levels_at: u32| { let mut ifd = IfdBuilder::new(); diff --git a/crates/raw/src/vendor/raf.rs b/crates/raw/src/vendor/raf.rs index b60a742660..e485931114 100644 --- a/crates/raw/src/vendor/raf.rs +++ b/crates/raw/src/vendor/raf.rs @@ -24,8 +24,8 @@ use super::white_from_data; use crate::unpack::{unpack_lsb, unpack_words32_msb}; use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result}; +use craft_tiff::{ByteOrder, Ifd, Tiff}; use lightcraft_geom::Orientation; -use lightcraft_tiff::{ByteOrder, Ifd, Tiff}; use rayon::prelude::*; const CROP_TOP_LEFT: u16 = 0x0110; @@ -88,7 +88,7 @@ fn pair(h: &Header, tag: u16) -> Option<(usize, usize)> { pub(crate) fn raw_ifd(raw: &[u8]) -> Result { let tiff = Tiff::parse(raw).map_err(|_| RawError::Unsupported("RAF without a raw IFD (older FinePix layout)".into()))?; let off = tiff.ifds.first().and_then(|i| i.u64(0xf000)).ok_or_else(|| RawError::Unsupported("RAF without a raw IFD".into()))?; - let (ifd, _) = lightcraft_tiff::parse_ifd_at(raw, off, ByteOrder::Little, 0, false, &Default::default())?; + let (ifd, _) = craft_tiff::parse_ifd_at(raw, off, ByteOrder::Little, 0, false, &Default::default())?; Ok(ifd) } diff --git a/crates/raw/src/vendor/raf_lens.rs b/crates/raw/src/vendor/raf_lens.rs index 283a3d0ea3..60275cd0d8 100644 --- a/crates/raw/src/vendor/raf_lens.rs +++ b/crates/raw/src/vendor/raf_lens.rs @@ -18,7 +18,7 @@ //! - vignette: relative illumination in percent at the source radius; the correction is the gain `100 / V`. use crate::{Opcode, Rect}; -use lightcraft_tiff::Ifd; +use craft_tiff::Ifd; const DISTORTION: u16 = 0xf00b; const VIGNETTING: u16 = 0xf010; @@ -334,7 +334,7 @@ pub(super) fn corrections(model: &str, raw: &Ifd, active: Rect) -> Vec { #[cfg(test)] mod tests { use super::*; - use lightcraft_tiff::{Entry, Value}; + use craft_tiff::{Entry, Value}; pub(crate) const KNOTS9: [f64; 9] = [0.3535211268, 0.5, 0.6126760563, 0.7070422535, 0.7908450704, 0.8661971831, 0.9352112676, 1.0, 1.06056338]; /// X-T4 with the XF16-55 at 16 mm (3913): the tables as ExifTool prints them. diff --git a/crates/raw/src/vendor/rw2.rs b/crates/raw/src/vendor/rw2.rs index 4acd53a020..7938da9d8e 100644 --- a/crates/raw/src/vendor/rw2.rs +++ b/crates/raw/src/vendor/rw2.rs @@ -105,8 +105,8 @@ use super::pef::{Bits, Huffman, diff}; use super::{active_rows, white_from_data}; use crate::{BlackLevel, Cfa, ColorData, MAX_SAMPLES, Mode, Opcode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result}; +use craft_tiff::{ByteOrder, Ifd, Tiff, Value, tags as t}; use lightcraft_geom::Orientation; -use lightcraft_tiff::{ByteOrder, Ifd, Tiff, Value, tags as t}; use rayon::prelude::*; const SENSOR_WIDTH: u16 = 0x0002; @@ -698,7 +698,7 @@ fn distortion(ifd: &Ifd, active: Rect) -> Option { #[cfg(test)] mod tests { use super::*; - use lightcraft_tiff::{IfdBuilder, TiffWriter}; + use craft_tiff::{IfdBuilder, TiffWriter}; /// Writes bit fields into a 16-byte block from bit 127 down (the inverse of [`Fields`]). struct FieldWriter { diff --git a/crates/raw/src/vendor/sr2.rs b/crates/raw/src/vendor/sr2.rs index 10fc15074b..01535a228e 100644 --- a/crates/raw/src/vendor/sr2.rs +++ b/crates/raw/src/vendor/sr2.rs @@ -45,7 +45,7 @@ //! of an accident 2⁻⁴⁰⁶⁴). For the DSC-R1's key `63 be 01 3e` the 11932-byte block becomes a well-formed 101-entry //! directory (black `0x7300`, white balance `0x7303`, crop `0x74c3`). -use lightcraft_tiff::{ByteOrder, EntrySpan, Ifd, ParseOptions, decode_value, directory_entries, entry_span, tags as t}; +use craft_tiff::{ByteOrder, EntrySpan, Ifd, ParseOptions, decode_value, directory_entries, entry_span, tags as t}; /// `SR2Private` tags. const SUBIFD_OFFSET: u16 = 0x7200; @@ -260,7 +260,7 @@ pub(crate) mod tests { const LONG: u16 = 4; const UNDEFINED: u16 = 7; const IFD: u16 = 13; - use lightcraft_tiff::ByteOrder::{Big, Little}; + use craft_tiff::ByteOrder::{Big, Little}; /// An `SR2SubIFD` block for file offset `start`, encrypted: SHORT/SSHORT entries `(tag, type, values)`, values /// of more than two shorts stored after the table at file offsets, as Sony does. @@ -429,8 +429,8 @@ pub(crate) mod tests { /// What `SubIfd::read` on main selects: the TIFF reader's parse of the directory with main's options, then the /// directory's `u64` and `bytes` lookups. fn reader_selects(bytes: &[u8], at: usize, order: ByteOrder) -> Selected { - let opts = lightcraft_tiff::ParseOptions { max_ifds: 1, max_depth: 0, follow_children: false, ..Default::default() }; - let Ok((private, _)) = lightcraft_tiff::parse_ifd_at(bytes, at as u64, order, 0, false, &opts) else { return (None, None, None) }; + let opts = craft_tiff::ParseOptions { max_ifds: 1, max_depth: 0, follow_children: false, ..Default::default() }; + let Ok((private, _)) = craft_tiff::parse_ifd_at(bytes, at as u64, order, 0, false, &opts) else { return (None, None, None) }; let n = |tag: u16| private.u64(tag).and_then(|v| usize::try_from(v).ok()); (n(SUBIFD_OFFSET), n(SUBIFD_LENGTH), private.bytes(SUBIFD_KEY).and_then(|b| <[u8; 4]>::try_from(b).ok())) } @@ -594,7 +594,7 @@ pub(crate) mod tests { let mut pointer = Vec::new(); order.put_u32(&mut pointer, 0); let ifd0 = Ifd { - entries: vec![lightcraft_tiff::Entry { tag: t::DNG_PRIVATE_DATA, value: lightcraft_tiff::Value::Byte(pointer), offset: 0 }], + entries: vec![craft_tiff::Entry { tag: t::DNG_PRIVATE_DATA, value: craft_tiff::Value::Byte(pointer), offset: 0 }], ..Default::default() }; let black = SubIfd::read(&file, &ifd0, order).and_then(|s| s.shorts(0x7310)); diff --git a/crates/raw/src/vendor/srw.rs b/crates/raw/src/vendor/srw.rs index dbdb21b52f..298aeed238 100644 --- a/crates/raw/src/vendor/srw.rs +++ b/crates/raw/src/vendor/srw.rs @@ -36,9 +36,9 @@ use super::{black_from_columns, white_from_data}; use crate::unpack::{read_u16s, unpack_lsb, unpack_msb}; use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result}; +use craft_tiff::image::chunk_bytes; +use craft_tiff::{ByteOrder, Ifd, Tiff, tags as t}; use lightcraft_geom::Orientation; -use lightcraft_tiff::image::chunk_bytes; -use lightcraft_tiff::{ByteOrder, Ifd, Tiff, tags as t}; use rayon::prelude::*; const SENSOR_AREAS: u16 = 0xa010; @@ -53,8 +53,8 @@ const COMPRESSIONS: std::ops::RangeInclusive = 32769..=32773; /// The maker-note IFD (offsets relative to the note's own start). fn maker_note(bytes: &[u8], tiff: &Tiff) -> Option { let e = tiff.exif()?.get(t::MAKER_NOTE)?; - let opts = lightcraft_tiff::ParseOptions { max_ifds: 4, max_depth: 1, follow_children: false, ..Default::default() }; - lightcraft_tiff::parse_ifd_at(bytes, e.offset, tiff.order, e.offset, false, &opts).ok().map(|(i, _)| i) + let opts = craft_tiff::ParseOptions { max_ifds: 4, max_depth: 1, follow_children: false, ..Default::default() }; + craft_tiff::parse_ifd_at(bytes, e.offset, tiff.order, e.offset, false, &opts).ok().map(|(i, _)| i) } /// A key-offset vector: the entries of `tag` shifted by the maker note's key (`sign` is +1 or -1 times the key). @@ -281,7 +281,7 @@ fn masked_black(d: &[u16], w: usize, h: usize, a: Rect, white: f32) -> BlackLeve mod tests { use super::*; use crate::{decode, probe, probe_info}; - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; const KEY: [u32; 11] = [305, 72, 737, 456, 282, 307, 519, 724, 13, 505, 193]; @@ -362,7 +362,7 @@ mod tests { } ifd0.set_child(t::EXIF_IFD, exif); ifd0.add_sub_ifd(raw); - TiffWriter::new(lightcraft_tiff::ByteOrder::Big, false).write(&[ifd0]).unwrap() + TiffWriter::new(craft_tiff::ByteOrder::Big, false).write(&[ifd0]).unwrap() } /// Strip byte counts that add up past `u64::MAX` (LONG8 `[u64::MAX, 1]`) are an error or a picture, not an overflow. diff --git a/crates/raw/tests/it/corpus.rs b/crates/raw/tests/it/corpus.rs index 0be98991de..44ac741076 100644 --- a/crates/raw/tests/it/corpus.rs +++ b/crates/raw/tests/it/corpus.rs @@ -1378,7 +1378,7 @@ fn a7cr_enabled_distortion_agrees_in_header_full_and_dng() { let Some(mut bytes) = pinned_sony(&root, name) else { continue }; let original = probe_info(&bytes).unwrap(); assert!(original.opcodes.list3.is_empty(), "Off files must remain uncorrected"); - 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); diff --git a/crates/raw/tests/it/dng.rs b/crates/raw/tests/it/dng.rs index dddafd5d0d..e8e5185374 100644 --- a/crates/raw/tests/it/dng.rs +++ b/crates/raw/tests/it/dng.rs @@ -1,14 +1,14 @@ //! Synthetic DNG round trips: every storage variant we decode is written with our own TIFF writer (and LJ92 //! encoder) and must decode bit-exactly. +use craft_tiff::tags::{self as t, compression, photometric}; +use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; use lightcraft_color::{D65, Mat3, Xy}; use lightcraft_raw::opcodes::{Area, Opcode}; use lightcraft_raw::{ BlackLevel, Cfa, ColorData, DngCompression, DngWriteOptions, Method, OpcodeLists, Orientation, RawData, RawFormat, RawImage, Rect, decode, embedded_preview, probe, write_dng, }; -use lightcraft_tiff::tags::{self as t, compression, photometric}; -use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; fn scene_value(x: usize, y: usize, c: u8, max: f32) -> f32 { let base = [0.55, 0.8, 0.4][c as usize]; @@ -120,7 +120,7 @@ fn writer_camera_identity_tags_roundtrip() { raw.metadata.model = model.map(str::to_owned); for order in [ByteOrder::Little, ByteOrder::Big] { let bytes = write_dng(&raw, &DngWriteOptions { compression: DngCompression::Uncompressed, order, xmp: None }).unwrap(); - let tiff = lightcraft_tiff::Tiff::parse(&bytes).unwrap(); + let tiff = craft_tiff::Tiff::parse(&bytes).unwrap(); let ifd = &tiff.ifds[0]; assert_eq!(ifd.value(t::UNIQUE_CAMERA_MODEL), Some(&Value::Ascii(unique.into())), "{make:?} / {model:?}, {order:?}"); for (tag, original) in [(t::MAKE, make), (t::MODEL, model)] { diff --git a/crates/raw/tests/it/robust.rs b/crates/raw/tests/it/robust.rs index ce41c1710c..3cd54e47e9 100644 --- a/crates/raw/tests/it/robust.rs +++ b/crates/raw/tests/it/robust.rs @@ -97,8 +97,8 @@ fn truncation_at_every_length() { #[test] fn hostile_dimensions_are_rejected_quickly() { // a DNG claiming a 60000 × 60000 16-bit image with 10 bytes of data - use lightcraft_tiff::tags as t; - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::tags as t; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; let mut ifd = IfdBuilder::new(); ifd.set(t::DNG_VERSION, Value::Byte(vec![1, 4, 0, 0])); ifd.set(t::IMAGE_WIDTH, Value::Long(vec![60000])); @@ -120,8 +120,8 @@ fn hostile_dimensions_are_rejected_quickly() { /// read as 0 and non-finite samples as black: the photo develops to finite numbers. #[test] fn non_finite_black_levels_and_samples_develop() { - use lightcraft_tiff::tags as t; - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + use craft_tiff::tags as t; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; let (w, h) = (32u32, 16u32); for black in [Value::Float(vec![f32::NAN]), Value::Double(vec![f64::INFINITY]), Value::Float(vec![64.0, f32::NAN, 64.0, 64.0])] { let mut ifd = IfdBuilder::new(); diff --git a/crates/raw/tests/it/sony_lens_inventory.rs b/crates/raw/tests/it/sony_lens_inventory.rs index 3eda884187..bea6dfde48 100644 --- a/crates/raw/tests/it/sony_lens_inventory.rs +++ b/crates/raw/tests/it/sony_lens_inventory.rs @@ -292,7 +292,7 @@ impl Drop for Scratch { /// A small Sony CFA file with a distortion setting (a lens record), or, `unsupported`, the packed 12-bit ARW variant /// the decoder refuses as unsupported. fn synthetic_arw(unsupported: bool) -> Vec { - use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value, tags as t}; let mut raw = IfdBuilder::new(); raw.set(t::MAKE, Value::Ascii("SONY".into())); raw.set(t::MODEL, Value::Ascii("ILCE-7CR".into())); diff --git a/crates/raw/tests/it/vendor_robust.rs b/crates/raw/tests/it/vendor_robust.rs index 446c1fb60a..1ebbae6633 100644 --- a/crates/raw/tests/it/vendor_robust.rs +++ b/crates/raw/tests/it/vendor_robust.rs @@ -1,7 +1,7 @@ //! Malformed vendor raw files (NEF, ARW, PEF, ORF, RW2 of every raw format, CR2, RAF, CRW) must decode to an error, never panic. -use lightcraft_tiff::tags as t; -use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; +use craft_tiff::tags as t; +use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; use proptest::prelude::*; const W: u32 = 32; diff --git a/crates/raw/tests/sr2_heap.rs b/crates/raw/tests/sr2_heap.rs index 2b936d18c2..8839e66c8d 100644 --- a/crates/raw/tests/sr2_heap.rs +++ b/crates/raw/tests/sr2_heap.rs @@ -3,7 +3,7 @@ //! it must not cost memory sized by its other entries: a hostile file can give it fields that each alias most of the //! file. Only the block's offset, length and key may be read. One test per binary (dhat's profiler is global). -use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, Tiff, TiffWriter, Value, tags as t}; +use craft_tiff::{ByteOrder, IfdBuilder, ImageData, Tiff, TiffWriter, Value, tags as t}; #[global_allocator] static ALLOC: dhat::Alloc = dhat::Alloc; diff --git a/crates/segment/src/detector.rs b/crates/segment/src/detector.rs index c302aafe76..3099af44af 100644 --- a/crates/segment/src/detector.rs +++ b/crates/segment/src/detector.rs @@ -333,10 +333,117 @@ pub fn merge(out: &Output, threshold: f32) -> Option { merged.map(|logits| Prediction { logits, score: best }) } +/// Share of the largest candidate's area another needs to count among the photo's main subjects +/// (people in front, not the crowd behind them). +pub const PROMINENT: f32 = 0.25; + +/// Which candidates are the photo's subject, given each one's area (cells it selects) and, when the +/// camera recorded where it focused, whether it covers that point. The focus decides: the +/// candidates it falls on, with the others about as large either way (a group at the same +/// distance); a focus on none of them means the subject is something else (empty: the caller +/// selects what is at the point). +/// Without a focus: the largest, and the others about as large. +pub fn pick_prominent(areas: &[usize], on_focus: Option<&[bool]>) -> Vec { + let largest = |ks: &mut dyn Iterator| ks.filter_map(|k| areas.get(k).copied()).max().unwrap_or(0); + let near = |a: usize, of: usize| of > 0 && a as f32 >= PROMINENT * of as f32; + match on_focus { + Some(hit) => { + let focused: Vec = (0..areas.len()).filter(|k| hit.get(*k).copied().unwrap_or(false) && areas[*k] > 0).collect(); + if focused.is_empty() { + return Vec::new(); + } + // about as large as what is in focus, either way: much larger is nearer the camera + // than the focus, so out of focus too + let of = largest(&mut focused.iter().copied()); + let alike = |a: usize| near(a, of) && (a as f32) * PROMINENT <= of as f32; + (0..areas.len()).filter(|k| focused.contains(k) || alike(areas[*k])).collect() + } + None => { + let of = largest(&mut (0..areas.len())); + (0..areas.len()).filter(|k| near(areas[*k], of)).collect() + } + } +} + +/// The instances of several detector outputs (one per concept: "person", "animal") above +/// `threshold` that [`pick_prominent`] keeps, each with its share of its concept's semantic mask, +/// merged into one mask. `focus`: where the camera focused (normalized). `None` when nothing is kept. +pub fn subject(outs: &[Output], threshold: f32, focus: Option<(f32, f32)>) -> Option { + let s = |x: f32| 1.0 / (1.0 + (-x).exp()); + // (mask, score, concept) + let mut cands: Vec<(Vec, f32, usize)> = Vec::new(); + for (c, out) in outs.iter().enumerate() { + let presence = s(out.presence); + for (i, l) in out.logits.iter().enumerate() { + let p = s(*l) * presence; + if p > threshold + && let Some(m) = out.masks.i(i).ok().and_then(|t| to_vec(&t).ok()) + { + cands.push((m, p, c)); + } + } + } + let n = cands.first()?.0.len(); + let side = (n as f64).sqrt() as usize; + if side == 0 || side * side != n || cands.iter().any(|c| c.0.len() != n) { + return None; + } + let areas: Vec = cands.iter().map(|c| c.0.iter().filter(|l| **l > 0.0).count()).collect(); + let cell = focus.filter(|(x, y)| x.is_finite() && y.is_finite()).map(|(x, y)| { + let at = |v: f32| ((v.clamp(0.0, 1.0) * side as f32) as usize).min(side - 1); + at(y) * side + at(x) + }); + let hits: Option> = cell.map(|i| cands.iter().map(|c| c.0.get(i).is_some_and(|l| *l > 0.0)).collect()); + let keep = pick_prominent(&areas, hits.as_deref()); + if keep.is_empty() { + return None; + } + let best = keep.iter().filter_map(|k| cands.get(*k)).map(|c| c.1).fold(0f32, f32::max); + let mut merged: Option> = None; + for (c, out) in outs.iter().enumerate() { + let mut masks: Vec> = keep.iter().filter_map(|k| cands.get(*k)).filter(|x| x.2 == c).map(|x| x.0.clone()).collect(); + if masks.is_empty() { + continue; + } + share(&mut masks, &out.semantic); + for m in masks { + merged = Some(match merged { + Some(acc) => acc.iter().zip(&m).map(|(a, b)| a.max(*b)).collect(), + None => m, + }); + } + } + merged.map(|logits| Prediction { logits, score: best }) +} + #[cfg(test)] mod tests { use super::*; + /// A group in front is kept, the crowd behind isn't; the camera's focus decides: on a person, + /// that person and the others as large; on nobody, nothing (the object there is the subject). + #[test] + fn prominent_subjects() { + // four people in front, two small ones far behind + let areas = [900, 1000, 800, 950, 60, 40]; + assert_eq!(pick_prominent(&areas, None), vec![0, 1, 2, 3]); + // focused on the first: the group + let hit = [true, false, false, false, false, false]; + assert_eq!(pick_prominent(&areas, Some(&hit)), vec![0, 1, 2, 3]); + // focused on someone small behind: that one and whoever is about as large, not the + // (much larger, so nearer and out of focus) group in front + let hit = [false, false, false, false, true, false]; + assert_eq!(pick_prominent(&areas, Some(&hit)), vec![4, 5]); + // a product in front of people: the focus is on none of them + assert!(pick_prominent(&areas, Some(&[false; 6])).is_empty()); + // nobody at all, or empty masks + assert!(pick_prominent(&[], None).is_empty()); + assert!(pick_prominent(&[0, 0], None).is_empty()); + assert!(pick_prominent(&[0, 5], Some(&[true, false])).is_empty()); + // a mismatched focus list is read as "not on it" + assert_eq!(pick_prominent(&[10, 9], Some(&[true])), vec![0, 1]); + } + /// Two people side by side on a row of cells, each only weakly sure of themself and both fading where /// they touch; the semantic mask is sure of both and runs across. Each takes the semantic logits where /// it is the strongest instance, a third region nobody is near keeps its own. diff --git a/crates/segment/src/lib.rs b/crates/segment/src/lib.rs index 6fd077ff96..98a47d9888 100644 --- a/crates/segment/src/lib.rs +++ b/crates/segment/src/lib.rs @@ -76,9 +76,15 @@ pub type Result = std::result::Result; pub const INPUT: usize = 1008; /// Side of the low-resolution masks the model predicts. pub const MASK_SIDE: usize = 288; +/// Most clicks one Object selection may carry (the engine's `MAX_CLICKS`). +pub const MAX_CLICKS: usize = 64; #[cfg(not(target_arch = "wasm32"))] const _: () = assert!(MASK_SIDE == tracker::LOW_RES && INPUT == vit::IMAGE); +/// What a photo's subject usually is, as concept prompts ([`Sam3::segment_subject`]); anything else +/// (an object, a landscape's feature) is selected at the focus point instead. +pub const SUBJECT_CONCEPTS: &[&str] = &["person", "animal"]; + /// The files of a model directory. pub const WEIGHTS_FILE: &str = "model.safetensors"; @@ -203,6 +209,10 @@ impl Sam3 { if clicks.is_empty() { return Err(Error::Model("no clicks".into())); } + // the prompt grows with every click: the editor takes at most this many (the engine's `MAX_CLICKS`) + if clicks.len() > MAX_CLICKS { + return Err(Error::Model(format!("at most {MAX_CLICKS} clicks, not {}", clicks.len()))); + } let (neck, tracker) = self.tracker_parts()?; if enc.tracker.is_none() { let levels = neck.forward(&enc.backbone, 3)?; @@ -241,6 +251,15 @@ impl Sam3 { Ok(detector::merge(&out, threshold)) } + /// The photo's subject among its people and animals ([`detector::subject`]): the ones the + /// camera's focus (`focus`, normalized; `None` when the file doesn't record it) falls on, or + /// without it the most prominent, with whoever is about as large. `None` when the subject is + /// neither (a focus on something else, or nobody in the photo): select what is at the point. + pub fn segment_subject(&mut self, enc: &mut Encoded, focus: Option<(f32, f32)>, threshold: f32) -> Result> { + let outs = SUBJECT_CONCEPTS.iter().map(|c| self.detect(enc, c)).collect::>>()?; + Ok(detector::subject(&outs, threshold, focus)) + } + /// Raw detector outputs for `text` (tests and tools). pub fn detect(&mut self, enc: &mut Encoded, text: &str) -> Result { let (det, tok) = self.detector_parts()?; diff --git a/crates/segment/src/remote.rs b/crates/segment/src/remote.rs index 10f7b2931a..4f23d10bc7 100644 --- a/crates/segment/src/remote.rs +++ b/crates/segment/src/remote.rs @@ -17,6 +17,9 @@ pub enum Operation { Prepare, Clicks(Vec<[f32; 3]>), Text(String), + /// The photo's subject among its people and animals ([`Sam3::segment_subject`]), from where + /// the camera focused (normalized `[x, y]`) when the file records it. + Subject(Option<[f32; 2]>), } #[derive(Serialize, Deserialize)] @@ -36,7 +39,8 @@ impl Request { return Err("detail pass requires an image".into()); } match &self.operation { - Operation::Prepare => {} + Operation::Prepare | Operation::Subject(None) => {} + Operation::Subject(Some(f)) if f.iter().all(|v| v.is_finite() && (0.0..=1.0).contains(v)) => {} Operation::Text(text) if text.len() <= 4096 && text.split([',', ';']).any(|p| !p.trim().is_empty()) => {} Operation::Clicks(clicks) if !clicks.is_empty() @@ -131,6 +135,11 @@ fn predict(model: &mut Sam3, enc: &mut Encoded, operation: Operation) -> Result< Ok(None) } Operation::Text(text) => text_logits(model, enc, &text).map_err(|e| e.to_string()), + // (the reference score threshold, as a local Find Subject uses) + Operation::Subject(focus) => { + let p = model.segment_subject(enc, focus.map(|f| (f[0], f[1])), 0.5).map_err(|e| e.to_string())?; + Ok(p.map(|p| p.logits)) + } Operation::Clicks(points) => { let clicks: Vec<_> = points.into_iter().map(|p| Click { x: p[0], y: p[1], positive: p[2] == 1.0 }).collect(); Ok(Some(model.segment_clicks(enc, &clicks).map_err(|e| e.to_string())?.logits)) diff --git a/crates/segment/tests/it/remote.rs b/crates/segment/tests/it/remote.rs index ee20ea2066..7b612425d3 100644 --- a/crates/segment/tests/it/remote.rs +++ b/crates/segment/tests/it/remote.rs @@ -18,4 +18,10 @@ fn validates_images_and_prompt_boundaries() { assert!(request(4, 3, Operation::Clicks(vec![[0.5, f32::NAN, 1.0]])).validate().is_err()); assert!(request(4, 3, Operation::Clicks(vec![[0.5, 0.5, 0.0]])).validate().is_err()); assert!(request(4, 3, Operation::Text(" ".into())).validate().is_err()); + // Find Subject: with or without the camera's focus point, which must lie in the photo + assert!(request(4, 3, Operation::Subject(None)).validate().is_ok()); + assert!(request(4, 3, Operation::Subject(Some([0.3, 0.7]))).validate().is_ok()); + for bad in [[1.5, 0.5], [0.5, -0.1], [f32::NAN, 0.5], [0.5, f32::INFINITY]] { + assert!(request(4, 3, Operation::Subject(Some(bad))).validate().is_err(), "{bad:?}"); + } } diff --git a/crates/slideshow/src/compose.rs b/crates/slideshow/src/compose.rs index 61ce91f3ff..87e4f1880e 100644 --- a/crates/slideshow/src/compose.rs +++ b/crates/slideshow/src/compose.rs @@ -220,7 +220,7 @@ pub fn compose_with( ) -> (Rgba8, Layout) { let s = s.clone().sanitized(); let (w, h) = slide_size(out_w, out_h); - let has_photo = photo.width > 0 && photo.height > 0 && photo.data.len() == photo.width * photo.height; + let has_photo = photo.width > 0 && photo.height > 0 && Some(photo.data.len()) == photo.width.checked_mul(photo.height); let aspect = if has_photo { photo.width as f64 / photo.height as f64 } else { f64::NAN }; let l = layout(&s, aspect, w, h); let c = s.backdrop_color; diff --git a/crates/tiff/Cargo.toml b/crates/tiff/Cargo.toml deleted file mode 100644 index 74919c6928..0000000000 --- a/crates/tiff/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "lightcraft-tiff" -version.workspace = true -edition.workspace = true -license.workspace = true -description = "LightCraft TIFF/IFD reader and writer (L0)" - -[lints] -workspace = true - -[dependencies] -serde = { workspace = true } -thiserror = { workspace = true } - -[dev-dependencies] -proptest = { workspace = true } diff --git a/crates/tiff/README.md b/crates/tiff/README.md deleted file mode 100644 index 2436b0d311..0000000000 --- a/crates/tiff/README.md +++ /dev/null @@ -1,45 +0,0 @@ -# lightcraft-tiff (L0) - -TIFF / IFD reader and writer. Used by `lightcraft-raw` (DNG, CR2, NEF, ARW…), `lightcraft-meta` (Exif) and -later the TIFF/DNG exporters. - -## Features - -- Classic TIFF and BigTIFF, little- and big-endian (`II`/`MM`). Also accepts the TIFF-like magic numbers of - some raw formats (ORF `IIRO`/`IIRS`, RW2 `IIU`) when `ParseOptions::accept_raw_magic` is set (default). -- Every field type: BYTE, ASCII, SHORT, LONG, RATIONAL, SBYTE, UNDEFINED, SSHORT, SLONG, SRATIONAL, FLOAT, - DOUBLE, IFD, LONG8, SLONG8, IFD8. -- IFD chain + `SubIFDs` (330) + Exif (34665) / GPS (34853) / Interoperability (40965) pointer IFDs, resolved - into a tree (`Ifd::sub_ifds`, `exif`, `gps`, `interop`). `Entry::offset` records the absolute position of - each value (needed for maker notes and embedded blobs). -- `parse_ifd_at(data, offset, order, base, bigtiff, opts)` parses an IFD whose offsets are relative to an - arbitrary base; `makernote::parse_makernote` knows the header / byte-order / offset-base conventions of - Canon, Nikon (types 1 and 3), Sony, Fujifilm, Olympus (old and new), Panasonic and Pentax notes. -- `image::ImageInfo` — width/height, bits, samples, compression, photometric, planar config, predictor, - sample format and the strip or tile chunk list (`chunks()`, `chunk_bytes()`); missing byte counts are - inferred. -- `directory_entries`, `entry_span`, `ParseOptions::budget_for` and `decode_value` are the reader's own rules - (directory layout and entry limit, which entries it reads, its value budget, value decoding), for callers that - must select entries exactly as `parse_ifd_at` would without decoding the whole directory (`lightcraft-raw`'s - Sony `SR2Private` lookup). -- `TiffWriter` writes IFD chains with strips or tiles, `SubIFDs` and pointer IFDs, in either byte order, - classic or BigTIFF; `writer::rational` / `srational` approximate floats as rationals. - -## Robustness - -The reader never panics on malformed input (proptests feed random and mutated files): offsets are -bounds-checked; out-of-range or unknown-type entries are skipped; IFD loops are detected; IFD count, -nesting depth and entries per IFD are limited; at most 65,536 directory entry slots -(`ParseOptions::max_entries_total`) are accepted across the main IFD chain and all child IFDs, even when tables -overlap at distinct offsets, and an IFD past that bound ends the chain or is skipped like any other bad IFD after -the first; a next-IFD pointer is followed only when every declared entry was read, with the counts compared as -64-bit numbers (a BigTIFF declaring 4,294,967,297 entries but holding one can't pass for a whole directory on a -32-bit target); the total bytes of decoded values are capped at -`8 × input + 1 MiB` so a file cannot make us allocate far more than its own size. - -## Sources - -Public specifications only: TIFF 6.0 (Adobe, 1992), the TIFF 6.0 supplements (IFD type, SubIFDs), the -BigTIFF format description (AWare Systems), Adobe DNG Specification 1.7 (tag numbers), CIPA DC-008 Exif 2.32, -and the ExifTool tag-name documentation (maker-note header conventions, prose only). No third-party code -was consulted. diff --git a/crates/tiff/src/image.rs b/crates/tiff/src/image.rs deleted file mode 100644 index 16e24f3676..0000000000 --- a/crates/tiff/src/image.rs +++ /dev/null @@ -1,248 +0,0 @@ -//! Image layout of an IFD: dimensions, sample format and the strip / tile chunks holding the pixels. - -use crate::{Ifd, Result, TiffError, tags}; -use serde::{Deserialize, Serialize}; - -/// How pixel data is split into chunks. -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub enum Layout { - Strips { rows_per_strip: u32 }, - Tiles { tile_width: u32, tile_height: u32 }, -} - -/// Pixel layout description of an IFD (TIFF 6.0 §3–§8, §15). -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -pub struct ImageInfo { - pub width: u32, - pub height: u32, - pub bits_per_sample: Vec, - pub samples_per_pixel: u16, - pub compression: u16, - pub photometric: u16, - /// 1 = chunky (interleaved), 2 = planar. - pub planar: u16, - pub predictor: u16, - /// 1 = unsigned int, 2 = signed int, 3 = IEEE float. - pub sample_format: u16, - pub new_subfile_type: u32, - pub layout: Layout, - pub offsets: Vec, - pub byte_counts: Vec, -} - -/// One strip or tile. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct Chunk { - pub index: usize, - /// Top-left pixel of the chunk. - pub x: u32, - pub y: u32, - /// Nominal chunk size (tiles may extend past the image; strips are clipped to the image height). - pub width: u32, - pub height: u32, - /// Plane index for planar configuration 2 (else 0). - pub plane: u16, - pub offset: u64, - pub len: u64, -} - -impl ImageInfo { - pub fn from_ifd(ifd: &Ifd) -> Result { - let width = ifd.u32(tags::IMAGE_WIDTH).ok_or(TiffError::MissingTag(tags::IMAGE_WIDTH))?; - let height = ifd.u32(tags::IMAGE_LENGTH).ok_or(TiffError::MissingTag(tags::IMAGE_LENGTH))?; - if width == 0 || height == 0 { - return Err(TiffError::Invalid("zero image dimension".into())); - } - let samples_per_pixel = ifd.u16(tags::SAMPLES_PER_PIXEL).unwrap_or(1).max(1); - let bits_per_sample: Vec = - ifd.u64s(tags::BITS_PER_SAMPLE).map(|v| v.into_iter().map(|b| b.min(u16::MAX as u64) as u16).collect()).unwrap_or_else(|| vec![1]); - let bits_per_sample = if bits_per_sample.is_empty() { vec![1] } else { bits_per_sample }; - let planar = ifd.u16(tags::PLANAR_CONFIGURATION).unwrap_or(1); - let (layout, offsets, byte_counts) = if let Some(tw) = ifd.u32(tags::TILE_WIDTH) { - let th = ifd.u32(tags::TILE_LENGTH).ok_or(TiffError::MissingTag(tags::TILE_LENGTH))?; - if tw == 0 || th == 0 { - return Err(TiffError::Invalid("zero tile size".into())); - } - let offs = ifd.u64s(tags::TILE_OFFSETS).or_else(|| ifd.u64s(tags::STRIP_OFFSETS)).ok_or(TiffError::MissingTag(tags::TILE_OFFSETS))?; - let cnts = ifd - .u64s(tags::TILE_BYTE_COUNTS) - .or_else(|| ifd.u64s(tags::STRIP_BYTE_COUNTS)) - .ok_or(TiffError::MissingTag(tags::TILE_BYTE_COUNTS))?; - (Layout::Tiles { tile_width: tw, tile_height: th }, offs, cnts) - } else { - let offs = ifd.u64s(tags::STRIP_OFFSETS).ok_or(TiffError::MissingTag(tags::STRIP_OFFSETS))?; - let cnts = ifd.u64s(tags::STRIP_BYTE_COUNTS).unwrap_or_default(); - let rps = ifd.u32(tags::ROWS_PER_STRIP).unwrap_or(height).clamp(1, height); - (Layout::Strips { rows_per_strip: rps }, offs, cnts) - }; - Ok(ImageInfo { - width, - height, - bits_per_sample, - samples_per_pixel, - compression: ifd.u16(tags::COMPRESSION).unwrap_or(1), - photometric: ifd.u16(tags::PHOTOMETRIC).unwrap_or(1), - planar, - predictor: ifd.u16(tags::PREDICTOR).unwrap_or(1), - sample_format: ifd.u16(tags::SAMPLE_FORMAT).unwrap_or(1), - new_subfile_type: ifd.u32(tags::NEW_SUBFILE_TYPE).unwrap_or(0), - layout, - offsets, - byte_counts, - }) - } - - /// Bits of the first sample. - pub fn bits(&self) -> u16 { - self.bits_per_sample[0] - } - - /// Whether this IFD is a reduced-resolution image (NewSubfileType bit 0). - pub fn is_reduced(&self) -> bool { - self.new_subfile_type & 1 != 0 - } - - /// Number of chunks across and down per plane. - pub fn grid(&self) -> (u32, u32) { - match self.layout { - Layout::Strips { rows_per_strip } => (1, self.height.div_ceil(rows_per_strip)), - Layout::Tiles { tile_width, tile_height } => (self.width.div_ceil(tile_width), self.height.div_ceil(tile_height)), - } - } - - /// Nominal chunk dimensions. - pub fn chunk_size(&self) -> (u32, u32) { - match self.layout { - Layout::Strips { rows_per_strip } => (self.width, rows_per_strip), - Layout::Tiles { tile_width, tile_height } => (tile_width, tile_height), - } - } - - /// The chunk count the image declares (offsets present, cut to the layout's plane capacity) with the - /// geometry `chunks` needs to place each one. No descriptors are built, so a pathological strip array - /// costs it a length check. - fn chunk_grid(&self) -> (usize, u32, usize) { - let (across, down) = self.grid(); - let per_plane = across as usize * down as usize; - let planes = if self.planar == 2 { self.samples_per_pixel as usize } else { 1 }; - (self.offsets.len().min(per_plane.saturating_mul(planes)), across, per_plane) - } - - fn declared_chunk_count(&self) -> usize { - self.chunk_grid().0 - } - - /// The one chunk when the image has exactly one, else `None` — and no descriptor vector beyond it. Preview - /// extraction only serves single-chunk images, so it asks here before serving a hostile multi-strip layout. - pub fn single_chunk(&self, data_len: u64) -> Option { - if self.declared_chunk_count() != 1 { - return None; - } - self.chunks(data_len).into_iter().next() - } - - /// All chunks that have an offset. Missing byte counts are inferred as "to the next offset / end of data" - /// using `data_len`. - pub fn chunks(&self, data_len: u64) -> Vec { - let (n, across, per_plane) = self.chunk_grid(); - let (cw, ch) = self.chunk_size(); - let mut out = Vec::with_capacity(n); - // the distinct offsets in order, for chunks without a byte count (sorted once, not scanned per chunk) - let mut sorted: Vec = Vec::new(); - if (0..n).any(|i| self.byte_counts.get(i).is_none_or(|&l| l == 0)) { - sorted = self.offsets.clone(); - sorted.sort_unstable(); - sorted.dedup(); - } - for i in 0..n { - let plane = i / per_plane.max(1); - let j = i % per_plane.max(1); - let (cx, cy) = (j as u32 % across, j as u32 / across); - let x = cx * cw; - let y = cy * ch; - let height = match self.layout { - Layout::Strips { .. } => ch.min(self.height - y), - Layout::Tiles { .. } => ch, - }; - let offset = self.offsets[i]; - let len = match self.byte_counts.get(i) { - Some(&l) if l > 0 => l, - _ => { - // infer: up to the next larger offset, else end of data - let next = sorted.get(sorted.partition_point(|&o| o <= offset)).copied().unwrap_or(data_len); - next.saturating_sub(offset) - } - }; - out.push(Chunk { index: i, x, y, width: cw, height, plane: plane as u16, offset, len }); - } - out - } -} - -/// The bytes of a chunk, clipped to the available data (truncated files yield a shorter slice). -/// Returns `None` only when the chunk starts past the end of `data`. -pub fn chunk_bytes<'a>(data: &'a [u8], c: &Chunk) -> Option<&'a [u8]> { - let start = usize::try_from(c.offset).ok()?; - if start >= data.len() { - return None; - } - let end = (c.offset.saturating_add(c.len)).min(data.len() as u64) as usize; - Some(&data[start..end]) -} - -#[cfg(test)] -mod tests { - use super::*; - - fn strips(offsets: Vec, byte_counts: Vec) -> ImageInfo { - ImageInfo { - width: 1, - height: offsets.len() as u32, - bits_per_sample: vec![8], - samples_per_pixel: 1, - compression: 1, - photometric: 1, - planar: 1, - predictor: 1, - sample_format: 1, - new_subfile_type: 0, - layout: Layout::Strips { rows_per_strip: 1 }, - offsets, - byte_counts, - } - } - - /// One offset is one chunk, and `single_chunk` refuses to build descriptors when the image declares more. - #[test] - fn single_chunk_is_the_only_chunk() { - let one = strips(vec![50], vec![10]); - assert_eq!(one.single_chunk(100).map(|c| (c.index, c.offset, c.len)), one.chunks(100).first().map(|c| (c.index, c.offset, c.len))); - // the inferred length matches too, and never falls back past the data - let inferred = strips(vec![70], vec![]); - assert_eq!(inferred.single_chunk(100).map(|c| c.len), Some(30)); - assert!(strips(vec![50, 10], vec![10, 5]).single_chunk(100).is_none(), "a multi-chunk image has no single chunk"); - assert!(strips(Vec::new(), Vec::new()).single_chunk(100).is_none()); - } - - #[test] - fn missing_byte_counts_run_to_the_next_larger_offset() { - let info = strips(vec![50, 10, 30, 10, 70], vec![0, 0, 5]); - let lens: Vec = info.chunks(100).iter().map(|c| c.len).collect(); - assert_eq!(lens, vec![20, 20, 5, 20, 30]); - } - - /// A million strips without byte counts (offsets in descending order) are measured in one sort, not one - /// scan of all offsets per strip. - #[test] - fn many_strips_without_byte_counts_are_fast() { - let n = 1_000_000u64; - let info = strips((0..n).rev().map(|i| 1000 + i * 2).collect(), Vec::new()); - let (tx, rx) = std::sync::mpsc::channel(); - std::thread::spawn(move || { - let chunks = info.chunks(1000 + n * 2); - let _ = tx.send(chunks.iter().map(|c| c.len).collect::>()); - }); - let lens = rx.recv_timeout(std::time::Duration::from_secs(20)).expect("chunks() took longer than 20 s"); - assert!(lens.iter().all(|&l| l == 2)); - } -} diff --git a/crates/tiff/src/lib.rs b/crates/tiff/src/lib.rs deleted file mode 100644 index 2072c4936e..0000000000 --- a/crates/tiff/src/lib.rs +++ /dev/null @@ -1,468 +0,0 @@ -//! TIFF / IFD reading and writing for LightCraft. -//! -//! - [`Tiff::parse`] reads classic TIFF and BigTIFF in both byte orders, following the IFD chain, -//! `SubIFDs`, and the Exif / GPS / Interoperability IFD pointers. All field types of TIFF 6.0, -//! the TIFF 6.0 supplement (`IFD`) and BigTIFF (`LONG8`, `SLONG8`, `IFD8`) are decoded. -//! - [`parse_ifd_at`] parses a single IFD with an arbitrary offset base — the building block for -//! maker-note IFDs ([`makernote`]). -//! - [`image::ImageInfo`] describes the pixel layout of an IFD (strips or tiles, planar config, -//! compression, predictor) and enumerates its [`image::Chunk`]s. -//! - [`writer::TiffWriter`] writes IFD trees (with strips/tiles, `SubIFDs` and pointer IFDs) in -//! either byte order, classic or BigTIFF. -//! -//! The reader never panics on malformed input: every offset is bounds-checked, IFD loops are -//! detected, nesting depth / IFD count are limited, at most 65,536 directory entry slots are read -//! across the main chain and child IFDs, and decoded value bytes are capped relative to the input -//! size (so overlapping tables cannot multiply entry-vector allocations without bound). -//! -//! Sources: TIFF 6.0 specification (Adobe, 1992), TIFF Technical Note / supplements, the BigTIFF -//! design notes (AWare Systems), Adobe DNG specification 1.7 (tag numbers), CIPA DC-008 (Exif 2.32). -#![forbid(unsafe_code)] -#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] - -pub mod image; -pub mod makernote; -mod reader; -pub mod tags; -pub mod writer; - -pub use reader::{DirectoryEntries, EntrySpan, ParseOptions, decode as decode_value, directory_entries, entry_span, parse_ifd_at}; -pub use writer::{IfdBuilder, ImageData, TiffWriter}; - -use serde::{Deserialize, Serialize}; - -/// Errors from TIFF parsing / writing. -#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] -pub enum TiffError { - #[error("not a TIFF stream (bad header)")] - NotTiff, - #[error("data truncated at offset {0}")] - Truncated(u64), - #[error("invalid TIFF structure: {0}")] - Invalid(String), - #[error("resource limit exceeded: {0}")] - Limit(&'static str), - #[error("missing required tag {0}")] - MissingTag(u16), -} - -pub type Result = std::result::Result; - -/// Byte order of a TIFF stream. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Default)] -pub enum ByteOrder { - /// `II` — Intel, little-endian. - #[default] - Little, - /// `MM` — Motorola, big-endian. - Big, -} - -impl ByteOrder { - #[inline] - pub fn u16(self, b: [u8; 2]) -> u16 { - match self { - ByteOrder::Little => u16::from_le_bytes(b), - ByteOrder::Big => u16::from_be_bytes(b), - } - } - #[inline] - pub fn u32(self, b: [u8; 4]) -> u32 { - match self { - ByteOrder::Little => u32::from_le_bytes(b), - ByteOrder::Big => u32::from_be_bytes(b), - } - } - #[inline] - pub fn u64(self, b: [u8; 8]) -> u64 { - match self { - ByteOrder::Little => u64::from_le_bytes(b), - ByteOrder::Big => u64::from_be_bytes(b), - } - } - /// Read a `u16` at `off`, or `None` if out of bounds. - #[inline] - pub fn read_u16(self, data: &[u8], off: usize) -> Option { - data.get(off..off.checked_add(2)?).map(|s| self.u16([s[0], s[1]])) - } - #[inline] - pub fn read_u32(self, data: &[u8], off: usize) -> Option { - data.get(off..off.checked_add(4)?).map(|s| self.u32([s[0], s[1], s[2], s[3]])) - } - #[inline] - pub fn read_u64(self, data: &[u8], off: usize) -> Option { - data.get(off..off.checked_add(8)?).map(|s| self.u64([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]])) - } - pub fn put_u16(self, out: &mut Vec, v: u16) { - out.extend_from_slice(&match self { - ByteOrder::Little => v.to_le_bytes(), - ByteOrder::Big => v.to_be_bytes(), - }); - } - pub fn put_u32(self, out: &mut Vec, v: u32) { - out.extend_from_slice(&match self { - ByteOrder::Little => v.to_le_bytes(), - ByteOrder::Big => v.to_be_bytes(), - }); - } - pub fn put_u64(self, out: &mut Vec, v: u64) { - out.extend_from_slice(&match self { - ByteOrder::Little => v.to_le_bytes(), - ByteOrder::Big => v.to_be_bytes(), - }); - } -} - -/// TIFF field types (TIFF 6.0 §2 + supplements + BigTIFF). -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] -#[repr(u16)] -pub enum FieldType { - Byte = 1, - Ascii = 2, - Short = 3, - Long = 4, - Rational = 5, - SByte = 6, - Undefined = 7, - SShort = 8, - SLong = 9, - SRational = 10, - Float = 11, - Double = 12, - Ifd = 13, - Long8 = 16, - SLong8 = 17, - Ifd8 = 18, -} - -impl FieldType { - pub fn from_u16(v: u16) -> Option { - use FieldType::*; - Some(match v { - 1 => Byte, - 2 => Ascii, - 3 => Short, - 4 => Long, - 5 => Rational, - 6 => SByte, - 7 => Undefined, - 8 => SShort, - 9 => SLong, - 10 => SRational, - 11 => Float, - 12 => Double, - 13 => Ifd, - 16 => Long8, - 17 => SLong8, - 18 => Ifd8, - _ => return None, - }) - } - /// Size in bytes of one value. - pub fn size(self) -> usize { - use FieldType::*; - match self { - Byte | Ascii | SByte | Undefined => 1, - Short | SShort => 2, - Long | SLong | Float | Ifd => 4, - Rational | SRational | Double | Long8 | SLong8 | Ifd8 => 8, - } - } -} - -/// A decoded field value. Arrays are stored in their native type. -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -pub enum Value { - Byte(Vec), - /// ASCII text (bytes up to the first NUL, decoded lossily as UTF-8). - Ascii(String), - Short(Vec), - Long(Vec), - Rational(Vec<(u32, u32)>), - SByte(Vec), - Undefined(Vec), - SShort(Vec), - SLong(Vec), - SRational(Vec<(i32, i32)>), - Float(Vec), - Double(Vec), - Ifd(Vec), - Long8(Vec), - SLong8(Vec), - Ifd8(Vec), -} - -impl Value { - pub fn field_type(&self) -> FieldType { - match self { - Value::Byte(_) => FieldType::Byte, - Value::Ascii(_) => FieldType::Ascii, - Value::Short(_) => FieldType::Short, - Value::Long(_) => FieldType::Long, - Value::Rational(_) => FieldType::Rational, - Value::SByte(_) => FieldType::SByte, - Value::Undefined(_) => FieldType::Undefined, - Value::SShort(_) => FieldType::SShort, - Value::SLong(_) => FieldType::SLong, - Value::SRational(_) => FieldType::SRational, - Value::Float(_) => FieldType::Float, - Value::Double(_) => FieldType::Double, - Value::Ifd(_) => FieldType::Ifd, - Value::Long8(_) => FieldType::Long8, - Value::SLong8(_) => FieldType::SLong8, - Value::Ifd8(_) => FieldType::Ifd8, - } - } - - /// Number of values (for ASCII: bytes including the terminating NUL). - pub fn count(&self) -> usize { - match self { - Value::Byte(v) | Value::Undefined(v) => v.len(), - Value::Ascii(s) => s.len() + 1, - Value::Short(v) => v.len(), - Value::Long(v) | Value::Ifd(v) => v.len(), - Value::Rational(v) => v.len(), - Value::SByte(v) => v.len(), - Value::SShort(v) => v.len(), - Value::SLong(v) => v.len(), - Value::SRational(v) => v.len(), - Value::Float(v) => v.len(), - Value::Double(v) => v.len(), - Value::Long8(v) | Value::Ifd8(v) => v.len(), - Value::SLong8(v) => v.len(), - } - } - - /// Value `i` as `f64` (rationals divided; zero denominators give `None`). ASCII has no numbers. - pub fn get_f64(&self, i: usize) -> Option { - Some(match self { - Value::Byte(v) | Value::Undefined(v) => *v.get(i)? as f64, - Value::Ascii(_) => return None, - Value::Short(v) => *v.get(i)? as f64, - Value::Long(v) | Value::Ifd(v) => *v.get(i)? as f64, - Value::Rational(v) => { - let (n, d) = *v.get(i)?; - if d == 0 { - return None; - } - n as f64 / d as f64 - } - Value::SByte(v) => *v.get(i)? as f64, - Value::SShort(v) => *v.get(i)? as f64, - Value::SLong(v) => *v.get(i)? as f64, - Value::SRational(v) => { - let (n, d) = *v.get(i)?; - if d == 0 { - return None; - } - n as f64 / d as f64 - } - Value::Float(v) => *v.get(i)? as f64, - Value::Double(v) => *v.get(i)?, - Value::Long8(v) | Value::Ifd8(v) => *v.get(i)? as f64, - Value::SLong8(v) => *v.get(i)? as f64, - }) - } - - /// Value `i` as an unsigned integer (integer types only; negative values give `None`). - pub fn get_u64(&self, i: usize) -> Option { - match self { - Value::Byte(v) | Value::Undefined(v) => v.get(i).map(|&x| x as u64), - Value::Short(v) => v.get(i).map(|&x| x as u64), - Value::Long(v) | Value::Ifd(v) => v.get(i).map(|&x| x as u64), - Value::Long8(v) | Value::Ifd8(v) => v.get(i).copied(), - Value::SByte(v) => v.get(i).and_then(|&x| u64::try_from(x).ok()), - Value::SShort(v) => v.get(i).and_then(|&x| u64::try_from(x).ok()), - Value::SLong(v) => v.get(i).and_then(|&x| u64::try_from(x).ok()), - Value::SLong8(v) => v.get(i).and_then(|&x| u64::try_from(x).ok()), - _ => None, - } - } - - /// Value `i` as a signed integer (integer types only). - pub fn get_i64(&self, i: usize) -> Option { - match self { - Value::SByte(v) => v.get(i).map(|&x| x as i64), - Value::SShort(v) => v.get(i).map(|&x| x as i64), - Value::SLong(v) => v.get(i).map(|&x| x as i64), - Value::SLong8(v) => v.get(i).copied(), - _ => self.get_u64(i).and_then(|x| i64::try_from(x).ok()), - } - } - - /// All values as `f64` (missing/invalid entries skipped for rationals with zero denominators → 0). - pub fn to_f64_vec(&self) -> Vec { - (0..self.len_values()).map(|i| self.get_f64(i).unwrap_or(0.0)).collect() - } - - /// All values as `u64` (integer types only; otherwise empty). - pub fn to_u64_vec(&self) -> Vec { - (0..self.len_values()).map_while(|i| self.get_u64(i)).collect() - } - - fn len_values(&self) -> usize { - match self { - Value::Ascii(_) => 0, - _ => self.count(), - } - } - - /// Text for ASCII values (trimmed of trailing NULs / spaces); `Byte`/`Undefined` are decoded as UTF-8 (lossy) - /// up to the first NUL, which covers XMP packets and UCS-less Exif strings. - pub fn as_str(&self) -> Option { - match self { - Value::Ascii(s) => Some(s.trim_end_matches([' ', '\0']).to_string()), - Value::Byte(b) | Value::Undefined(b) => { - let end = b.iter().position(|&c| c == 0).unwrap_or(b.len()); - Some(String::from_utf8_lossy(&b[..end]).trim_end().to_string()) - } - _ => None, - } - } - - /// Raw bytes for `Byte` / `Undefined` / `SByte` / `Ascii`. - pub fn as_bytes(&self) -> Option<&[u8]> { - match self { - Value::Byte(b) | Value::Undefined(b) => Some(b), - Value::Ascii(s) => Some(s.as_bytes()), - _ => None, - } - } -} - -/// One IFD entry. -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -pub struct Entry { - pub tag: u16, - pub value: Value, - /// Absolute position in the parsed buffer of the value bytes (inline or out-of-line). - pub offset: u64, -} - -impl Entry { - pub fn field_type(&self) -> FieldType { - self.value.field_type() - } - pub fn count(&self) -> usize { - self.value.count() - } -} - -/// An Image File Directory with its child IFDs already resolved. -#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] -pub struct Ifd { - /// Absolute offset of the IFD in the buffer. - pub offset: u64, - /// Entries sorted by tag (duplicate tags: the first occurrence wins). - pub entries: Vec, - /// IFDs referenced by `SubIFDs` (330). - pub sub_ifds: Vec, - /// Exif private IFD (34665). - pub exif: Option>, - /// GPS IFD (34853). - pub gps: Option>, - /// Interoperability IFD (40965). - pub interop: Option>, -} - -impl Ifd { - pub fn get(&self, tag: u16) -> Option<&Entry> { - self.entries.binary_search_by_key(&tag, |e| e.tag).ok().map(|i| &self.entries[i]) - } - pub fn contains(&self, tag: u16) -> bool { - self.get(tag).is_some() - } - pub fn value(&self, tag: u16) -> Option<&Value> { - self.get(tag).map(|e| &e.value) - } - /// First value as `u32` (integer types, saturating from wider types). - pub fn u32(&self, tag: u16) -> Option { - self.value(tag)?.get_u64(0).map(|v| v.min(u32::MAX as u64) as u32) - } - pub fn u64(&self, tag: u16) -> Option { - self.value(tag)?.get_u64(0) - } - pub fn u16(&self, tag: u16) -> Option { - self.value(tag)?.get_u64(0).map(|v| v.min(u16::MAX as u64) as u16) - } - pub fn i64(&self, tag: u16) -> Option { - self.value(tag)?.get_i64(0) - } - pub fn f64(&self, tag: u16) -> Option { - self.value(tag)?.get_f64(0) - } - pub fn f64s(&self, tag: u16) -> Option> { - self.value(tag).map(|v| v.to_f64_vec()) - } - pub fn u64s(&self, tag: u16) -> Option> { - self.value(tag).map(|v| v.to_u64_vec()) - } - /// Text value (ASCII, or UTF-8 bytes). - pub fn string(&self, tag: u16) -> Option { - self.value(tag)?.as_str().filter(|s| !s.is_empty()) - } - pub fn bytes(&self, tag: u16) -> Option<&[u8]> { - self.value(tag)?.as_bytes() - } - - /// This IFD and all descendants (sub-IFDs, Exif, GPS, Interop), depth-first, pre-order. - pub fn walk(&self) -> Vec<&Ifd> { - let mut out = vec![self]; - for s in &self.sub_ifds { - out.extend(s.walk()); - } - for c in [&self.exif, &self.gps, &self.interop].into_iter().flatten() { - out.extend(c.walk()); - } - out - } - - /// Image layout of this IFD, if it describes an image. - pub fn image(&self) -> Result { - image::ImageInfo::from_ifd(self) - } -} - -/// A parsed TIFF stream (IFD chain with resolved children). -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -pub struct Tiff { - pub order: ByteOrder, - pub bigtiff: bool, - /// The main IFD chain (IFD0, IFD1, …). - pub ifds: Vec, -} - -impl Tiff { - /// Parse a TIFF stream (the header must be at offset 0 of `data`). - pub fn parse(data: &[u8]) -> Result { - reader::parse(data, &ParseOptions::default()) - } - pub fn parse_with(data: &[u8], opts: &ParseOptions) -> Result { - reader::parse(data, opts) - } - /// Header check only: returns the byte order and BigTIFF flag. - pub fn sniff(data: &[u8]) -> Option<(ByteOrder, bool)> { - reader::header(data).ok().map(|(o, b, _)| (o, b)) - } - /// Every IFD (main chain and all children), depth-first. - pub fn all_ifds(&self) -> Vec<&Ifd> { - self.ifds.iter().flat_map(|i| i.walk()).collect() - } - /// The Exif IFD attached to IFD0 (or any IFD). - pub fn exif(&self) -> Option<&Ifd> { - self.all_ifds().into_iter().find_map(|i| i.exif.as_deref()) - } - pub fn gps(&self) -> Option<&Ifd> { - self.all_ifds().into_iter().find_map(|i| i.gps.as_deref()) - } - /// First entry for `tag` in IFD0, then the Exif IFD, then any IFD. - pub fn find(&self, tag: u16) -> Option<&Entry> { - if let Some(e) = self.ifds.first().and_then(|i| i.get(tag)) { - return Some(e); - } - if let Some(e) = self.exif().and_then(|i| i.get(tag)) { - return Some(e); - } - self.all_ifds().into_iter().find_map(|i| i.get(tag)) - } -} diff --git a/crates/tiff/src/makernote.rs b/crates/tiff/src/makernote.rs deleted file mode 100644 index 288b366f64..0000000000 --- a/crates/tiff/src/makernote.rs +++ /dev/null @@ -1,235 +0,0 @@ -//! Maker-note IFD helpers. -//! -//! Most vendors store their maker note (Exif tag 37500) as a TIFF IFD, but each uses its own header, -//! byte order and *offset base* (what stored value offsets are relative to). The header layouts here -//! follow the public descriptions in the ExifTool tag documentation (`MakerNotes` table) and the -//! vendors' own published notes; no third-party code was consulted. - -use crate::{ByteOrder, Ifd, ParseOptions, parse_ifd_at}; -use serde::{Deserialize, Serialize}; - -/// Maker-note dialects we know how to locate. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub enum MakerNoteKind { - /// Plain IFD at the start of the note, offsets relative to the enclosing TIFF header (Canon, many others). - Plain, - /// `Nikon\0` + version + embedded TIFF header at +10; offsets relative to that header. - NikonV3, - /// `Nikon\0\x01\0`: IFD at +8, offsets relative to the enclosing TIFF header. - NikonV1, - /// `SONY DSC `/`SONY CAM `/`SONY MOBILE` etc.: 12-byte header, offsets relative to the TIFF header. - Sony, - /// `FUJIFILM` + LE u32 IFD offset; little-endian, offsets relative to the note start. - Fujifilm, - /// `OLYMPUS\0` + byte order + version: IFD at +12, offsets relative to the note start. - OlympusNew, - /// `OLYMP\0` / `EPSON\0` etc.: IFD at +8, offsets relative to the TIFF header. - OlympusOld, - /// `Panasonic\0\0\0`: IFD at +12, offsets relative to the TIFF header. - Panasonic, - /// `AOC\0` + byte order: IFD at +6, offsets relative to the TIFF header. - PentaxAoc, - /// `PENTAX \0` + byte order: IFD at +10, offsets relative to the note start. - Pentax, -} - -/// A parsed maker-note IFD. -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -pub struct MakerNote { - pub kind: MakerNoteKind, - pub order: ByteOrder, - /// Absolute offset that stored value offsets are relative to. - pub base: u64, - pub ifd: Ifd, -} - -/// Locate and parse the maker note stored at absolute `offset` (length `len`) inside `data`, the buffer whose -/// start is the enclosing TIFF header. `order` is the enclosing stream's byte order. `make` (the Exif Make) is -/// used only as a hint for header-less notes. Returns `None` for unknown or unparsable notes. -pub fn parse_makernote(data: &[u8], offset: u64, len: u64, order: ByteOrder, make: &str) -> Option { - let start = usize::try_from(offset).ok()?; - let end = start.checked_add(usize::try_from(len).ok()?)?.min(data.len()); - let note = data.get(start..end)?; - let order_at = |i: usize| match note.get(i..i + 2) { - Some(b"II") => Some(ByteOrder::Little), - Some(b"MM") => Some(ByteOrder::Big), - _ => None, - }; - // A fixed value budget: a maker note is at most tens of kilobytes of values in any honest file, and - // computing the budget from the buffer's length multiplies the file length (a hostile entry in a - // sparse header-read buffer, where the buffer holds the file's logical size but only a few - // kilobytes are loaded, would decode allocations no actual data backs). - let opts = ParseOptions { max_ifds: 64, max_depth: 3, value_budget: Some(1 << 20), ..Default::default() }; - let try_parse = |kind: MakerNoteKind, ifd_at: u64, base: u64, ord: ByteOrder| -> Option { - let (ifd, _) = parse_ifd_at(data, ifd_at, ord, base, false, &opts).ok()?; - (!ifd.entries.is_empty()).then_some(MakerNote { kind, order: ord, base, ifd }) - }; - if note.starts_with(b"Nikon\0") { - if note.get(6) == Some(&1) { - return try_parse(MakerNoteKind::NikonV1, offset + 8, 0, order); - } - let ord = order_at(10)?; - let ifd_rel = ord.read_u32(note, 14)? as u64; - let base = offset + 10; - return try_parse(MakerNoteKind::NikonV3, base + ifd_rel, base, ord); - } - if note.starts_with(b"FUJIFILM") || note.starts_with(b"GENERALE") { - let rel = ByteOrder::Little.read_u32(note, 8)? as u64; - return try_parse(MakerNoteKind::Fujifilm, offset + rel, offset, ByteOrder::Little); - } - if note.starts_with(b"OLYMPUS\0") || note.starts_with(b"OM SYSTEM\0") { - let (ord_at, ifd_at) = if note.starts_with(b"OM SYSTEM\0") { (12, 16) } else { (8, 12) }; - let ord = order_at(ord_at).unwrap_or(order); - return try_parse(MakerNoteKind::OlympusNew, offset + ifd_at, offset, ord); - } - for p in [b"OLYMP\0", b"EPSON\0", b"MINOL\0", b"CAMER\0", b"SANYO\0"] { - if note.starts_with(p) { - return try_parse(MakerNoteKind::OlympusOld, offset + 8, 0, order); - } - } - if note.starts_with(b"Panasonic\0") { - return try_parse(MakerNoteKind::Panasonic, offset + 12, 0, order); - } - if note.starts_with(b"AOC\0") { - let ord = order_at(4).unwrap_or(order); - return try_parse(MakerNoteKind::PentaxAoc, offset + 6, 0, ord); - } - if note.starts_with(b"PENTAX \0") { - let ord = order_at(8).unwrap_or(order); - return try_parse(MakerNoteKind::Pentax, offset + 10, offset, ord); - } - if (note.starts_with(b"SONY") || note.starts_with(b"VHAB \0")) - && let Some(m) = try_parse(MakerNoteKind::Sony, offset + 12, 0, order) - { - return Some(m); - } - let _ = make; - try_parse(MakerNoteKind::Plain, offset, 0, order) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::{IfdBuilder, TiffWriter, Value, tags}; - - /// Build a TIFF whose Exif IFD carries `note` as the maker note, return (bytes, note offset, len). - fn with_note(order: ByteOrder, note: Vec) -> (Vec, u64, u64) { - let mut exif = IfdBuilder::new(); - exif.set(tags::MAKER_NOTE, Value::Undefined(note)); - let mut ifd0 = IfdBuilder::new(); - ifd0.set(tags::MAKE, Value::Ascii("Test".into())); - ifd0.set_child(tags::EXIF_IFD, exif); - let bytes = TiffWriter { order, bigtiff: false }.write(&[ifd0]).unwrap(); - let t = crate::Tiff::parse(&bytes).unwrap(); - let e = t.exif().unwrap().get(tags::MAKER_NOTE).unwrap(); - let (off, len) = (e.offset, e.count() as u64); - (bytes, off, len) - } - - /// An IFD with one SHORT entry (tag 1, value 7) and one out-of-line LONG[2] entry at `rel` (relative to base). - fn tiny_ifd(order: ByteOrder, rel_values: u32) -> Vec { - let mut v = Vec::new(); - order.put_u16(&mut v, 2); - order.put_u16(&mut v, 1); - order.put_u16(&mut v, 3); - order.put_u32(&mut v, 1); - order.put_u16(&mut v, 7); - order.put_u16(&mut v, 0); - order.put_u16(&mut v, 2); - order.put_u16(&mut v, 4); - order.put_u32(&mut v, 2); - order.put_u32(&mut v, rel_values); - order.put_u32(&mut v, 0); - v - } - - #[test] - fn nikon_v3_uses_embedded_header_base() { - for order in [ByteOrder::Little, ByteOrder::Big] { - let mut note = b"Nikon\0\x02\x10\0\0".to_vec(); - let hdr_at = note.len(); - note.extend_from_slice(if order == ByteOrder::Little { b"II" } else { b"MM" }); - order.put_u16(&mut note, 42); - order.put_u32(&mut note, 8); - // IFD at +8 (relative to embedded header), values after IFD: 8 + 2 + 24 + 4 = 38 - note.extend(tiny_ifd(order, 38)); - order.put_u32(&mut note, 111); - order.put_u32(&mut note, 222); - let (bytes, off, len) = with_note(order, note); - let m = parse_makernote(&bytes, off, len, order, "NIKON CORPORATION").unwrap(); - assert_eq!(m.kind, MakerNoteKind::NikonV3); - assert_eq!(m.base, off + hdr_at as u64); - assert_eq!(m.ifd.u32(1), Some(7)); - assert_eq!(m.ifd.u64s(2).unwrap(), vec![111, 222]); - } - } - - #[test] - fn fujifilm_offsets_relative_to_note() { - let mut note = b"FUJIFILM".to_vec(); - ByteOrder::Little.put_u32(&mut note, 12); - // IFD at 12 (relative to note), values at 12 + 30 = 42 - note.extend(tiny_ifd(ByteOrder::Little, 42)); - ByteOrder::Little.put_u32(&mut note, 5); - ByteOrder::Little.put_u32(&mut note, 6); - // enclosing file is big-endian: Fuji notes are always little-endian - let (bytes, off, len) = with_note(ByteOrder::Big, note); - let m = parse_makernote(&bytes, off, len, ByteOrder::Big, "FUJIFILM").unwrap(); - assert_eq!(m.kind, MakerNoteKind::Fujifilm); - assert_eq!(m.order, ByteOrder::Little); - assert_eq!(m.ifd.u64s(2).unwrap(), vec![5, 6]); - } - - #[test] - fn plain_canon_style_note() { - // Plain IFD whose out-of-line offset is relative to the TIFF header: we need the absolute - // position, which we only know after writing. Write once to learn the offset, then patch. - let order = ByteOrder::Little; - let mut note = tiny_ifd(order, 0); - note.extend_from_slice(&[0; 8]); - let (mut bytes, off, len) = with_note(order, note); - let values_abs = off as u32 + 30; - bytes[off as usize + 22..off as usize + 26].copy_from_slice(&values_abs.to_le_bytes()); - bytes[off as usize + 30..off as usize + 34].copy_from_slice(&9u32.to_le_bytes()); - let m = parse_makernote(&bytes, off, len, order, "Canon").unwrap(); - assert_eq!(m.kind, MakerNoteKind::Plain); - assert_eq!(m.ifd.u64s(2).unwrap(), vec![9, 0]); - } - - /// A maker note's value budget is fixed, not computed from the length of the buffer it sits in: - /// an entry claiming more than the note budget (here 2 MB) is dropped, a fitting one is read. In a - /// sparse header read the buffer holds the file's logical size while only kilobytes were loaded, - /// so a length-scaled budget would let an unrelated entry decode megabytes of zeros (see the focus - /// reader, try_read_exif_with). - #[test] - fn a_note_entry_past_the_fixed_value_budget_is_dropped() { - let order = ByteOrder::Little; - let mut note = b"SONY DSC \0\0\0".to_vec(); - // the Sony IFD directly after the 12-byte header, one fitting BYTE entry and a decoy - // BYTE[2 MB] entry pointing into the file's own (zero-filled) body - order.put_u16(&mut note, 2); - order.put_u16(&mut note, 0x201b); - order.put_u16(&mut note, 1); - order.put_u32(&mut note, 1); - note.extend([3, 0, 0, 0]); - order.put_u16(&mut note, 0x9900); - order.put_u16(&mut note, 1); - order.put_u32(&mut note, 2 << 20); - order.put_u32(&mut note, 200); - order.put_u32(&mut note, 0); - let mut bytes = note.clone(); - bytes.resize(200 + (2 << 20), 0); // the decoy's value range is inside the real bytes - let m = parse_makernote(&bytes, 0, note.len() as u64, order, "SONY").unwrap(); - assert_eq!(m.kind, MakerNoteKind::Sony); - assert!(m.ifd.get(0x201b).is_some(), "the fitting entry is read"); - assert!(m.ifd.get(0x9900).is_none(), "the decoy is dropped by the value budget"); - } - - #[test] - fn garbage_note_is_none() { - let (bytes, off, len) = with_note(ByteOrder::Little, vec![0xff; 40]); - assert!(parse_makernote(&bytes, off, len, ByteOrder::Little, "X").is_none()); - assert!(parse_makernote(&bytes, 1 << 40, 10, ByteOrder::Little, "X").is_none()); - assert!(parse_makernote(&bytes, off, u64::MAX, ByteOrder::Little, "X").is_none()); - } -} diff --git a/crates/tiff/src/reader.rs b/crates/tiff/src/reader.rs deleted file mode 100644 index 70c66c5a74..0000000000 --- a/crates/tiff/src/reader.rs +++ /dev/null @@ -1,536 +0,0 @@ -//! Bounds-checked IFD parsing. - -use crate::{ByteOrder, Entry, FieldType, Ifd, Result, Tiff, TiffError, Value, tags}; -use std::collections::HashSet; - -/// Limits applied while parsing. The defaults are generous for real files and safe for hostile ones. -#[derive(Clone, Debug)] -pub struct ParseOptions { - /// Maximum number of IFDs visited in total (chain + children). - pub max_ifds: usize, - /// Maximum nesting depth of child IFDs. - pub max_depth: usize, - /// Maximum number of entries read from one IFD. - pub max_entries: usize, - /// Maximum total number of entries read across all IFDs (chain + children). - /// - /// Hostile files can reuse the same bytes for up to `max_ifds` tables of `max_entries` - /// entries each (overlapping IFD tables via SubIFD pointers shifted by one entry), which can - /// allocate millions of `Entry`s from a small input (~100 KB → ~400 MB). This hard cap bounds - /// that amplification regardless of entry size. 65 536 is generous for real files — a 50 MB - /// scanned TIFF with thousands of entries across chain + children stays well under it — yet - /// stops a small file from allocating hundreds of megabytes of `Entry`s. - pub max_entries_total: usize, - /// Maximum total bytes of decoded values; `None` = `8 × input length + 1 MiB`. - pub value_budget: Option, - /// Follow `SubIFDs` / Exif / GPS / Interop pointers. - pub follow_children: bool, - /// Accept the TIFF-like magic numbers used by some raw formats (ORF `IIRO`/`IIRS`, RW2 `IIU`). - pub accept_raw_magic: bool, -} - -impl Default for ParseOptions { - fn default() -> Self { - Self { - max_ifds: 1024, - max_depth: 10, - max_entries: 8192, - max_entries_total: 65536, - value_budget: None, - follow_children: true, - accept_raw_magic: true, - } - } -} - -impl ParseOptions { - /// The value budget for `data_len` bytes of input: [`Self::value_budget`], else `8 × data_len + 1 MiB`. - pub fn budget_for(&self, data_len: usize) -> u64 { - self.value_budget.unwrap_or((data_len as u64).saturating_mul(8).saturating_add(1 << 20)) - } -} - -/// Where a directory's entries are and how many of them the reader reads, as [`directory_entries`] found them. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct DirectoryEntries { - /// Position of the first entry. - table: usize, - /// Entries read: the declared count, cut to the entries the data holds and to the `max_entries` limit. - read: usize, - /// The declared entry count. - declared: u64, - /// Bytes per entry: 12, or 20 in BigTIFF. - entry_size: usize, -} - -impl DirectoryEntries { - /// Positions of the entries read, in directory order. Checked arithmetic: the iteration ends rather than - /// overflow, whatever the layout. - pub fn positions(&self) -> impl Iterator + use<> { - let (table, size) = (self.table, self.entry_size); - (0..self.read).map_while(move |i| i.checked_mul(size)?.checked_add(table)) - } - - /// The number of entries read. - pub fn read(&self) -> usize { - self.read - } - - /// The entry count the directory declares. - pub fn declared(&self) -> u64 { - self.declared - } -} - -/// The directory at `offset` as the reader walks it: its entry table and how many entries it reads. The same errors -/// as [`parse_ifd_at`] for a directory it can't read: past the data, empty, or without room for one entry. -pub fn directory_entries(data: &[u8], offset: u64, order: ByteOrder, bigtiff: bool, max_entries: usize) -> Result { - let pos = usize::try_from(offset).map_err(|_| TiffError::Truncated(offset))?; - let (n, head, entry_size) = if bigtiff { - (order.read_u64(data, pos).ok_or(TiffError::Truncated(offset))?, 8usize, 20usize) - } else { - (order.read_u16(data, pos).ok_or(TiffError::Truncated(offset))? as u64, 2, 12) - }; - let table = pos + head; - let fit = (data.len().saturating_sub(table) / entry_size) as u64; - let read = n.min(fit).min(max_entries as u64) as usize; - if n == 0 { - return Err(TiffError::Invalid(format!("empty IFD at {offset}"))); - } - if read == 0 { - return Err(TiffError::Truncated(offset)); - } - Ok(DirectoryEntries { table, read, declared: n, entry_size }) -} - -/// A directory entry the reader can read: its tag, type and count, and where its values are. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct EntrySpan { - pub tag: u16, - pub field_type: FieldType, - pub count: u64, - /// Position of the values in the data: the entry's own value field when they fit it, else `base` plus the offset - /// stored there. - pub start: u64, - /// `count × size` bytes. - pub len: u64, -} - -/// The entry at `pos`, when the reader reads it: a known field type, `count × size` without overflow, and values -/// inside `data`. Nothing is decoded; [`parse_ifd_at`] also drops an entry whose values exceed what is left of its -/// value budget ([`ParseOptions::budget_for`]), which this leaves to the caller. -pub fn entry_span(data: &[u8], pos: usize, order: ByteOrder, bigtiff: bool, base: u64) -> Option { - let tag = order.read_u16(data, pos)?; - let field_type = FieldType::from_u16(order.read_u16(data, pos.checked_add(2)?)?)?; - let (count, field, inline) = if bigtiff { - (order.read_u64(data, pos.checked_add(4)?)?, pos.checked_add(12)?, 8u64) - } else { - (order.read_u32(data, pos.checked_add(4)?)? as u64, pos.checked_add(8)?, 4) - }; - let len = count.checked_mul(field_type.size() as u64)?; - let start = if len <= inline { - field as u64 - } else { - let rel = if bigtiff { order.read_u64(data, field)? } else { order.read_u32(data, field)? as u64 }; - base.checked_add(rel)? - }; - let end = start.checked_add(len)?; - (end <= data.len() as u64).then_some(EntrySpan { tag, field_type, count, start, len }) -} - -/// Whether a directory declaring `declared` entries was read whole (`read` entries), so that its next-IFD pointer -/// follows them. The counts compare as 64-bit numbers: narrowing the declared count to `usize` instead would, on a -/// 32-bit target, let a BigTIFF declaring 4,294,967,297 entries but holding one pass for a whole directory. -fn reads_every_entry(declared: u64, read: usize) -> bool { - u64::try_from(read).is_ok_and(|read| read == declared) -} - -/// Parse the header: (byte order, bigtiff, first IFD offset). -pub(crate) fn header(data: &[u8]) -> Result<(ByteOrder, bool, u64)> { - header_with(data, true) -} - -fn header_with(data: &[u8], raw_magic: bool) -> Result<(ByteOrder, bool, u64)> { - let order = match data.get(0..2) { - Some(b"II") => ByteOrder::Little, - Some(b"MM") => ByteOrder::Big, - _ => return Err(TiffError::NotTiff), - }; - let magic = order.read_u16(data, 2).ok_or(TiffError::NotTiff)?; - match magic { - 42 => Ok((order, false, order.read_u32(data, 4).ok_or(TiffError::Truncated(4))? as u64)), - 43 => { - let bytesize = order.read_u16(data, 4).ok_or(TiffError::Truncated(4))?; - let zero = order.read_u16(data, 6).ok_or(TiffError::Truncated(6))?; - if bytesize != 8 || zero != 0 { - return Err(TiffError::NotTiff); - } - Ok((order, true, order.read_u64(data, 8).ok_or(TiffError::Truncated(8))?)) - } - 0x4f52 | 0x5352 | 0x0055 if raw_magic => Ok((order, false, order.read_u32(data, 4).ok_or(TiffError::Truncated(4))? as u64)), - _ => Err(TiffError::NotTiff), - } -} - -pub(crate) fn parse(data: &[u8], opts: &ParseOptions) -> Result { - let (order, big, first) = header_with(data, opts.accept_raw_magic)?; - let mut ctx = Ctx::new(data, order, big, opts); - let mut ifds = Vec::new(); - let mut off = first; - while off != 0 { - match ctx.ifd(off, 0, 0) { - Ok((ifd, next)) => { - ifds.push(ifd); - off = next; - } - Err(e) => { - if ifds.is_empty() { - return Err(e); - } - break; - } - } - } - if ifds.is_empty() { - return Err(TiffError::Invalid("no IFDs".into())); - } - Ok(Tiff { order, bigtiff: big, ifds }) -} - -/// Parse one IFD at absolute `offset` whose value offsets (and child IFD pointers) are relative to -/// `base` (absolute position = `base + stored offset`). Returns the IFD and the absolute offset of the -/// next IFD (0 when there is none). Used for maker notes, which often use their own offset base. -pub fn parse_ifd_at(data: &[u8], offset: u64, order: ByteOrder, base: u64, bigtiff: bool, opts: &ParseOptions) -> Result<(Ifd, u64)> { - let mut ctx = Ctx::new(data, order, bigtiff, opts); - ctx.ifd(offset, base, 0) -} - -struct Ctx<'a> { - data: &'a [u8], - order: ByteOrder, - big: bool, - opts: &'a ParseOptions, - visited: HashSet, - budget: u64, - entries_total: usize, -} - -impl<'a> Ctx<'a> { - fn new(data: &'a [u8], order: ByteOrder, big: bool, opts: &'a ParseOptions) -> Self { - let budget = opts.budget_for(data.len()); - Self { data, order, big, opts, visited: HashSet::new(), budget, entries_total: 0 } - } - - fn read_off(&self, pos: usize) -> Option { - if self.big { self.order.read_u64(self.data, pos) } else { self.order.read_u32(self.data, pos).map(|v| v as u64) } - } - - /// Returns (ifd, absolute next offset or 0). - fn ifd(&mut self, offset: u64, base: u64, depth: usize) -> Result<(Ifd, u64)> { - if depth > self.opts.max_depth { - return Err(TiffError::Limit("IFD nesting depth")); - } - if self.visited.len() >= self.opts.max_ifds { - return Err(TiffError::Limit("IFD count")); - } - if !self.visited.insert(offset) { - return Err(TiffError::Invalid(format!("IFD loop at {offset}"))); - } - let dir = directory_entries(self.data, offset, self.order, self.big, self.opts.max_entries)?; - // Bound total entries across the whole parse so overlapping IFD tables (SubIFD pointers - // shifted by one entry) cannot allocate millions of `Entry`s from a small input. - self.entries_total = self.entries_total.saturating_add(dir.read); - if self.entries_total > self.opts.max_entries_total { - return Err(TiffError::Limit("entry count")); - } - let mut entries = Vec::with_capacity(dir.read); - for p in dir.positions() { - if let Some(e) = self.entry(p, base) { - entries.push(e); - } - } - entries.sort_by_key(|e| e.tag); - entries.dedup_by_key(|e| e.tag); - // the next-IFD pointer follows the last entry, read only when every declared entry was (checked: a BigTIFF - // count can be anything up to u64::MAX) - let next = if reads_every_entry(dir.declared, dir.read) { - match dir.read.checked_mul(dir.entry_size).and_then(|n| n.checked_add(dir.table)).and_then(|p| self.read_off(p)) { - Some(0) | None => 0, - Some(v) => base.checked_add(v).unwrap_or(0), - } - } else { - 0 - }; - let mut ifd = Ifd { offset, entries, ..Default::default() }; - if self.opts.follow_children { - self.children(&mut ifd, base, depth); - } - Ok((ifd, next)) - } - - fn children(&mut self, ifd: &mut Ifd, base: u64, depth: usize) { - if let Some(v) = ifd.value(tags::SUB_IFDS) { - let offs = v.to_u64_vec(); - for o in offs { - if let Some(abs) = base.checked_add(o) - && let Ok((sub, _)) = self.ifd(abs, base, depth + 1) - { - ifd.sub_ifds.push(sub); - } - } - } - let child = |tag: u16, ctx: &mut Self| -> Option> { - let o = ifd.value(tag)?.get_u64(0)?; - let abs = base.checked_add(o)?; - ctx.ifd(abs, base, depth + 1).ok().map(|(i, _)| Box::new(i)) - }; - let exif = child(tags::EXIF_IFD, self); - let gps = child(tags::GPS_IFD, self); - let interop = child(tags::INTEROP_IFD, self); - ifd.exif = exif; - ifd.gps = gps; - ifd.interop = interop; - } - - fn entry(&mut self, p: usize, base: u64) -> Option { - let span = entry_span(self.data, p, self.order, self.big, base)?; - if span.len > self.budget { - return None; - } - self.budget -= span.len; - let bytes = self.data.get(usize::try_from(span.start).ok()?..usize::try_from(span.start + span.len).ok()?)?; - Some(Entry { tag: span.tag, value: decode(self.order, span.field_type, bytes), offset: span.start }) - } -} - -/// Decode `bytes` (exactly `count × size` long, or the first values of an entry) as values of type `typ`. -pub fn decode(o: ByteOrder, typ: FieldType, b: &[u8]) -> Value { - fn arr(c: &[u8]) -> [u8; N] { - let mut a = [0u8; N]; - a.copy_from_slice(c); - a - } - match typ { - FieldType::Byte => Value::Byte(b.to_vec()), - FieldType::Undefined => Value::Undefined(b.to_vec()), - FieldType::Ascii => { - let end = b.iter().position(|&c| c == 0).unwrap_or(b.len()); - Value::Ascii(String::from_utf8_lossy(&b[..end]).into_owned()) - } - FieldType::SByte => Value::SByte(b.iter().map(|&x| x as i8).collect()), - FieldType::Short => Value::Short(b.as_chunks::<2>().0.iter().map(|c| o.u16(arr(c))).collect()), - FieldType::SShort => Value::SShort(b.as_chunks::<2>().0.iter().map(|c| o.u16(arr(c)) as i16).collect()), - FieldType::Long => Value::Long(b.as_chunks::<4>().0.iter().map(|c| o.u32(arr(c))).collect()), - FieldType::Ifd => Value::Ifd(b.as_chunks::<4>().0.iter().map(|c| o.u32(arr(c))).collect()), - FieldType::SLong => Value::SLong(b.as_chunks::<4>().0.iter().map(|c| o.u32(arr(c)) as i32).collect()), - FieldType::Float => Value::Float(b.as_chunks::<4>().0.iter().map(|c| f32::from_bits(o.u32(arr(c)))).collect()), - FieldType::Rational => Value::Rational(b.as_chunks::<8>().0.iter().map(|c| (o.u32(arr(&c[..4])), o.u32(arr(&c[4..])))).collect()), - FieldType::SRational => { - Value::SRational(b.as_chunks::<8>().0.iter().map(|c| (o.u32(arr(&c[..4])) as i32, o.u32(arr(&c[4..])) as i32)).collect()) - } - FieldType::Double => Value::Double(b.as_chunks::<8>().0.iter().map(|c| f64::from_bits(o.u64(arr(c)))).collect()), - FieldType::Long8 => Value::Long8(b.as_chunks::<8>().0.iter().map(|c| o.u64(arr(c))).collect()), - FieldType::Ifd8 => Value::Ifd8(b.as_chunks::<8>().0.iter().map(|c| o.u64(arr(c))).collect()), - FieldType::SLong8 => Value::SLong8(b.as_chunks::<8>().0.iter().map(|c| o.u64(arr(c)) as i64).collect()), - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::Value; - use crate::tags as t; - use crate::writer::{IfdBuilder, TiffWriter}; - - /// A value budget the caller sets bounds what is decoded: an entry claiming more value bytes than - /// the budget allows is dropped, entries that fit are read (the sparse header-read path, where the - /// buffer holds the file's logical size but only a few kilobytes were ever loaded, sizes its - /// budget from what was fetched, not from whatever length the file claims). - #[test] - fn the_value_budget_drops_over_budget_entries_and_keeps_the_rest() { - let mut ifd0 = IfdBuilder::new(); - ifd0.set(t::MAKE, Value::Ascii("Keep".into())); - ifd0.set(0x9000, Value::Byte(vec![0; 2 << 20])); - let file = TiffWriter { order: ByteOrder::Little, bigtiff: false }.write(&[ifd0]).unwrap(); - let opts = ParseOptions { value_budget: Some(1 << 20), ..Default::default() }; - let tiff = Tiff::parse_with(&file, &opts).unwrap(); - assert_eq!(tiff.ifds[0].string(t::MAKE), Some("Keep".to_string()), "a fitting entry is read"); - assert!(tiff.ifds[0].get(0x9000).is_none(), "the over-budget entry is dropped"); - } - - /// A BigTIFF whose first IFD, at 80, declares `declared` entries but holds one, followed by a next-IFD pointer - /// `next`; a valid one-entry IFD sits at 16. - fn bigtiff_chain(order: ByteOrder, declared: u64, next: u64) -> Vec { - let ifd = |d: &mut Vec, count: u64, tag: u16, next: u64| { - order.put_u64(d, count); - order.put_u16(d, tag); - order.put_u16(d, 3); - order.put_u64(d, 1); - order.put_u16(d, 8); - d.extend([0; 6]); - order.put_u64(d, next); - }; - let mut d = if order == ByteOrder::Little { b"II".to_vec() } else { b"MM".to_vec() }; - order.put_u16(&mut d, 43); - order.put_u16(&mut d, 8); - order.put_u16(&mut d, 0); - order.put_u64(&mut d, 80); - ifd(&mut d, 1, 0x0101, 0); - d.resize(80, 0); - ifd(&mut d, declared, 0x0100, next); - d - } - - #[test] - fn only_a_directory_read_whole_has_a_next_pointer() { - // the comparison itself, on every target: a declared count past u32::MAX never matches a small read count - for (declared, read, whole) in [ - (0, 0, true), - (1, 1, true), - (8192, 8192, true), - (8193, 8192, false), - (1, 0, false), - ((1 << 32) + 1, 1, false), - ((2 << 32) + 1, 1, false), - ((1 << 63) + 1, 1, false), - (u64::MAX, 1, false), - ] { - assert_eq!(reads_every_entry(declared, read), whole, "{declared} declared, {read} read"); - } - } - - #[test] - fn a_bigtiff_count_past_u32_max_never_follows_the_next_pointer() { - // the whole chain: on 32-bit targets (wasm32) the old cast truncated these counts to 1 and followed the pointer - for order in [ByteOrder::Little, ByteOrder::Big] { - for declared in [(1u64 << 32) + 1, (2u64 << 32) + 1, (1u64 << 63) + 1] { - let d = bigtiff_chain(order, declared, 16); - let tiff = Tiff::parse(&d).unwrap(); - assert_eq!(tiff.ifds.len(), 1, "{order:?} {declared}"); - assert_eq!(tiff.ifds[0].u64(0x0100), Some(8)); - assert_eq!(parse_ifd_at(&d, 80, order, 0, true, &ParseOptions::default()).unwrap().1, 0, "{order:?} {declared}"); - } - // control: the same file declaring its one entry follows the pointer to the IFD at 16 - let whole = Tiff::parse(&bigtiff_chain(order, 1, 16)).unwrap(); - assert_eq!(whole.ifds.len(), 2, "{order:?}"); - assert_eq!(whole.ifds[1].u64(0x0101), Some(8)); - } - } - - #[test] - fn directory_positions_never_overflow() { - // a hand-built layout the reader never produces: the iterator stops instead of overflowing - let hostile = DirectoryEntries { table: usize::MAX, read: 2, declared: 2, entry_size: 12 }; - assert_eq!(hostile.positions().collect::>(), vec![usize::MAX]); - assert_eq!(hostile.positions().nth(1), None); - let wide = DirectoryEntries { table: usize::MAX - 30, read: 4, declared: 4, entry_size: 12 }; - assert_eq!(wide.positions().collect::>(), vec![usize::MAX - 30, usize::MAX - 18, usize::MAX - 6]); - // a directory read from data lists its entries - let mut data = Vec::new(); - ByteOrder::Little.put_u16(&mut data, 2); - data.extend([0; 24]); - let dir = directory_entries(&data, 0, ByteOrder::Little, false, 8192).unwrap(); - assert_eq!(dir.positions().collect::>(), vec![2, 14]); - } - - #[test] - fn a_bigtiff_directory_declaring_u64_max_entries_parses_without_overflow() { - // 44 bytes: BigTIFF header (first IFD at 16), an entry count of u64::MAX, one 20-byte entry - let mut data = b"II\x2b\x00\x08\x00\x00\x00".to_vec(); - data.extend(16u64.to_le_bytes()); - data.extend(u64::MAX.to_le_bytes()); - data.extend(0x0100u16.to_le_bytes()); - data.extend(3u16.to_le_bytes()); - data.extend(1u64.to_le_bytes()); - data.extend([8, 0, 0, 0, 0, 0, 0, 0]); - assert_eq!(data.len(), 44); - let tiff = Tiff::parse(&data).unwrap(); - assert_eq!(tiff.ifds.len(), 1); - assert_eq!(tiff.ifds[0].u64(0x0100), Some(8)); - } - #[test] - fn overlapping_ifd_tables_hit_entry_cap_instead_of_amplifying() { - // classic TIFF; IFDs at 8, 20, 32, ... each declaring 8192 entries whose tables overlap - // (next table starts one entry after the previous table's start). Without the cap, a single - // `Tiff::parse` would run `Vec::with_capacity(8192)` per IFD inside one `Ctx`, amplifying a - // small file into millions of `Entry`s; the per-`Ctx` `max_entries_total` cap rejects the - // 9th IFD before any allocation. - let mut data = vec![0u8; 200_000]; - data[0..2].copy_from_slice(b"II"); - data[2..4].copy_from_slice(&42u16.to_le_bytes()); - data[4..8].copy_from_slice(&8u32.to_le_bytes()); // first IFD at 8 - - for k in 0..9u64 { - let ifd_off = (8 + 12 * k) as usize; - data[ifd_off..ifd_off + 2].copy_from_slice(&8192u16.to_le_bytes()); // count - let table = ifd_off + 2; - let ptr = table + 8192 * 12; // next-IFD pointer lives right after the entries - let next = if k + 1 < 9 { (8 + 12 * (k + 1)) as u32 } else { 0 }; - data[ptr..ptr + 4].copy_from_slice(&next.to_le_bytes()); - } - - let tiff = Tiff::parse(&data).unwrap(); - assert_eq!(tiff.ifds.len(), 8, "8 IFDs parsed, 9th rejected by max_entries_total before allocation"); - } - - #[test] - fn a_legitimate_multi_ifd_file_still_parses_under_the_entry_cap() { - // classic TIFF: IFD0 @8 (3 entries: value + Exif + GPS child pointers) -> IFD1 @58 chain, - // plus Exif child @60 and GPS child @90. Total entries = 6, far under max_entries_total. - let mut data = vec![0u8; 128]; - data[0..2].copy_from_slice(b"II"); - data[2..4].copy_from_slice(&42u16.to_le_bytes()); - data[4..8].copy_from_slice(&8u32.to_le_bytes()); // first IFD at 8 - - // IFD0 @8: count 3, table @10, next-IFD @46 -> IFD1 @58 - data[8..10].copy_from_slice(&3u16.to_le_bytes()); - // entry0 @10: tag 0x0100, SHORT, count 1, value 8 (inline @18..22) - data[10..12].copy_from_slice(&0x0100u16.to_le_bytes()); - data[12..14].copy_from_slice(&3u16.to_le_bytes()); - data[14..18].copy_from_slice(&1u32.to_le_bytes()); - data[18..22].copy_from_slice(&8u32.to_le_bytes()); - // entry1 @22: tag 0x8769 ExifIFD, LONG, count 1, value 60 (inline @30..34) - data[22..24].copy_from_slice(&0x8769u16.to_le_bytes()); - data[24..26].copy_from_slice(&4u16.to_le_bytes()); - data[26..30].copy_from_slice(&1u32.to_le_bytes()); - data[30..34].copy_from_slice(&60u32.to_le_bytes()); - // entry2 @34: tag 0x8825 GPSIFD, LONG, count 1, value 90 (inline @42..46) - data[34..36].copy_from_slice(&0x8825u16.to_le_bytes()); - data[36..38].copy_from_slice(&4u16.to_le_bytes()); - data[38..42].copy_from_slice(&1u32.to_le_bytes()); - data[42..46].copy_from_slice(&90u32.to_le_bytes()); - data[46..50].copy_from_slice(&58u32.to_le_bytes()); // next IFD - - // IFD1 @58: count 1, table @60, next 0 - data[58..60].copy_from_slice(&1u16.to_le_bytes()); - data[60..62].copy_from_slice(&0x0100u16.to_le_bytes()); - data[62..64].copy_from_slice(&3u16.to_le_bytes()); - data[64..68].copy_from_slice(&1u32.to_le_bytes()); - data[68..72].copy_from_slice(&8u32.to_le_bytes()); - data[72..76].copy_from_slice(&0u32.to_le_bytes()); - - // Exif child IFD @60: count 1, entry tag 0x9000 value 1, next 0 - data[60..62].copy_from_slice(&1u16.to_le_bytes()); - data[62..64].copy_from_slice(&0x9000u16.to_le_bytes()); - data[64..66].copy_from_slice(&3u16.to_le_bytes()); - data[66..70].copy_from_slice(&1u32.to_le_bytes()); - data[70..74].copy_from_slice(&1u32.to_le_bytes()); - data[74..78].copy_from_slice(&0u32.to_le_bytes()); - - // GPS child IFD @90: count 1, entry tag 0 value 1, next 0 - data[90..92].copy_from_slice(&1u16.to_le_bytes()); - data[92..94].copy_from_slice(&0u16.to_le_bytes()); - data[94..96].copy_from_slice(&3u16.to_le_bytes()); - data[96..100].copy_from_slice(&1u32.to_le_bytes()); - data[100..104].copy_from_slice(&1u32.to_le_bytes()); - data[104..108].copy_from_slice(&0u32.to_le_bytes()); - - let tiff = Tiff::parse(&data).unwrap(); - assert_eq!(tiff.ifds.len(), 2, "chain IFDs parse fully"); - assert_eq!(tiff.ifds[0].u64(0x0100), Some(8)); - let exif = tiff.ifds[0].exif.as_ref().expect("Exif child present"); - assert_eq!(exif.u64(0x9000), Some(1)); - let gps = tiff.ifds[0].gps.as_ref().expect("GPS child present"); - assert_eq!(gps.u64(0x0000), Some(1)); - } -} diff --git a/crates/tiff/src/tags.rs b/crates/tiff/src/tags.rs deleted file mode 100644 index 458aefe535..0000000000 --- a/crates/tiff/src/tags.rs +++ /dev/null @@ -1,226 +0,0 @@ -//! Tag numbers (TIFF 6.0, TIFF/EP, Exif 2.32, GPS, DNG 1.7). Public specification constants. - -// --- TIFF baseline / extensions -pub const NEW_SUBFILE_TYPE: u16 = 254; -pub const SUBFILE_TYPE: u16 = 255; -pub const IMAGE_WIDTH: u16 = 256; -pub const IMAGE_LENGTH: u16 = 257; -pub const BITS_PER_SAMPLE: u16 = 258; -pub const COMPRESSION: u16 = 259; -pub const PHOTOMETRIC: u16 = 262; -pub const FILL_ORDER: u16 = 266; -pub const DOCUMENT_NAME: u16 = 269; -pub const IMAGE_DESCRIPTION: u16 = 270; -pub const MAKE: u16 = 271; -pub const MODEL: u16 = 272; -pub const STRIP_OFFSETS: u16 = 273; -pub const ORIENTATION: u16 = 274; -pub const SAMPLES_PER_PIXEL: u16 = 277; -pub const ROWS_PER_STRIP: u16 = 278; -pub const STRIP_BYTE_COUNTS: u16 = 279; -pub const X_RESOLUTION: u16 = 282; -pub const Y_RESOLUTION: u16 = 283; -pub const PLANAR_CONFIGURATION: u16 = 284; -pub const RESOLUTION_UNIT: u16 = 296; -pub const SOFTWARE: u16 = 305; -pub const DATE_TIME: u16 = 306; -pub const ARTIST: u16 = 315; -pub const PREDICTOR: u16 = 317; -pub const TILE_WIDTH: u16 = 322; -pub const TILE_LENGTH: u16 = 323; -pub const TILE_OFFSETS: u16 = 324; -pub const TILE_BYTE_COUNTS: u16 = 325; -pub const SUB_IFDS: u16 = 330; -pub const EXTRA_SAMPLES: u16 = 338; -pub const SAMPLE_FORMAT: u16 = 339; -pub const JPEG_INTERCHANGE_FORMAT: u16 = 513; -pub const JPEG_INTERCHANGE_FORMAT_LENGTH: u16 = 514; -pub const YCBCR_SUBSAMPLING: u16 = 530; -pub const XMP: u16 = 700; -pub const RATING: u16 = 18246; -pub const RATING_PERCENT: u16 = 18249; -pub const CFA_REPEAT_PATTERN_DIM: u16 = 33421; -pub const CFA_PATTERN_EP: u16 = 33422; -pub const COPYRIGHT: u16 = 33432; -pub const EXPOSURE_TIME_EP: u16 = 33434; -pub const IPTC_NAA: u16 = 33723; -pub const EXIF_IFD: u16 = 34665; -pub const ICC_PROFILE: u16 = 34675; -pub const GPS_IFD: u16 = 34853; -pub const INTEROP_IFD: u16 = 40965; - -// --- Exif private IFD -pub const EXPOSURE_TIME: u16 = 33434; -pub const F_NUMBER: u16 = 33437; -pub const EXPOSURE_PROGRAM: u16 = 34850; -pub const ISO_SPEED: u16 = 34855; -pub const SENSITIVITY_TYPE: u16 = 34864; -pub const RECOMMENDED_EXPOSURE_INDEX: u16 = 34866; -pub const ISO_SPEED_RATINGS_EXT: u16 = 34867; -pub const EXIF_VERSION: u16 = 36864; -pub const DATE_TIME_ORIGINAL: u16 = 36867; -pub const DATE_TIME_DIGITIZED: u16 = 36868; -pub const OFFSET_TIME: u16 = 36880; -pub const OFFSET_TIME_ORIGINAL: u16 = 36881; -pub const OFFSET_TIME_DIGITIZED: u16 = 36882; -pub const SHUTTER_SPEED_VALUE: u16 = 37377; -pub const APERTURE_VALUE: u16 = 37378; -pub const EXPOSURE_BIAS: u16 = 37380; -pub const MAX_APERTURE: u16 = 37381; -pub const METERING_MODE: u16 = 37383; -pub const LIGHT_SOURCE: u16 = 37384; -pub const FLASH: u16 = 37385; -pub const FOCAL_LENGTH: u16 = 37386; -pub const MAKER_NOTE: u16 = 37500; -pub const USER_COMMENT: u16 = 37510; -pub const SUBSEC_TIME: u16 = 37520; -pub const SUBSEC_TIME_ORIGINAL: u16 = 37521; -pub const SUBSEC_TIME_DIGITIZED: u16 = 37522; -pub const PIXEL_X_DIMENSION: u16 = 40962; -pub const PIXEL_Y_DIMENSION: u16 = 40963; -pub const FOCAL_PLANE_X_RESOLUTION: u16 = 41486; -pub const EXPOSURE_MODE: u16 = 41986; -pub const WHITE_BALANCE: u16 = 41987; -pub const FOCAL_LENGTH_35MM: u16 = 41989; -pub const SCENE_CAPTURE_TYPE: u16 = 41990; -pub const BODY_SERIAL_NUMBER: u16 = 42033; -pub const LENS_SPECIFICATION: u16 = 42034; -pub const LENS_MAKE: u16 = 42035; -pub const LENS_MODEL: u16 = 42036; -pub const LENS_SERIAL_NUMBER: u16 = 42037; - -// --- GPS IFD -pub const GPS_VERSION_ID: u16 = 0; -pub const GPS_LATITUDE_REF: u16 = 1; -pub const GPS_LATITUDE: u16 = 2; -pub const GPS_LONGITUDE_REF: u16 = 3; -pub const GPS_LONGITUDE: u16 = 4; -pub const GPS_ALTITUDE_REF: u16 = 5; -pub const GPS_ALTITUDE: u16 = 6; -pub const GPS_TIME_STAMP: u16 = 7; -pub const GPS_IMG_DIRECTION_REF: u16 = 16; -pub const GPS_IMG_DIRECTION: u16 = 17; -pub const GPS_DATE_STAMP: u16 = 29; - -// --- DNG -pub const DNG_VERSION: u16 = 50706; -pub const DNG_BACKWARD_VERSION: u16 = 50707; -pub const UNIQUE_CAMERA_MODEL: u16 = 50708; -pub const LOCALIZED_CAMERA_MODEL: u16 = 50709; -pub const CFA_PLANE_COLOR: u16 = 50710; -pub const CFA_LAYOUT: u16 = 50711; -pub const LINEARIZATION_TABLE: u16 = 50712; -pub const BLACK_LEVEL_REPEAT_DIM: u16 = 50713; -pub const BLACK_LEVEL: u16 = 50714; -pub const BLACK_LEVEL_DELTA_H: u16 = 50715; -pub const BLACK_LEVEL_DELTA_V: u16 = 50716; -pub const WHITE_LEVEL: u16 = 50717; -pub const DEFAULT_SCALE: u16 = 50718; -pub const DEFAULT_CROP_ORIGIN: u16 = 50719; -pub const DEFAULT_CROP_SIZE: u16 = 50720; -pub const COLOR_MATRIX_1: u16 = 50721; -pub const COLOR_MATRIX_2: u16 = 50722; -pub const CAMERA_CALIBRATION_1: u16 = 50723; -pub const CAMERA_CALIBRATION_2: u16 = 50724; -pub const REDUCTION_MATRIX_1: u16 = 50725; -pub const REDUCTION_MATRIX_2: u16 = 50726; -pub const ANALOG_BALANCE: u16 = 50727; -pub const AS_SHOT_NEUTRAL: u16 = 50728; -pub const AS_SHOT_WHITE_XY: u16 = 50729; -pub const BASELINE_EXPOSURE: u16 = 50730; -pub const BASELINE_NOISE: u16 = 50731; -pub const BASELINE_SHARPNESS: u16 = 50732; -pub const BAYER_GREEN_SPLIT: u16 = 50733; -pub const LINEAR_RESPONSE_LIMIT: u16 = 50734; -pub const CAMERA_SERIAL_NUMBER: u16 = 50735; -pub const LENS_INFO: u16 = 50736; -pub const CHROMA_BLUR_RADIUS: u16 = 50737; -pub const ANTI_ALIAS_STRENGTH: u16 = 50738; -pub const SHADOW_SCALE: u16 = 50739; -pub const DNG_PRIVATE_DATA: u16 = 50740; -pub const MAKER_NOTE_SAFETY: u16 = 50741; -pub const CALIBRATION_ILLUMINANT_1: u16 = 50778; -pub const CALIBRATION_ILLUMINANT_2: u16 = 50779; -pub const BEST_QUALITY_SCALE: u16 = 50780; -pub const RAW_DATA_UNIQUE_ID: u16 = 50781; -pub const ORIGINAL_RAW_FILE_NAME: u16 = 50827; -pub const ACTIVE_AREA: u16 = 50829; -pub const MASKED_AREAS: u16 = 50830; -pub const AS_SHOT_ICC_PROFILE: u16 = 50831; -pub const COLORIMETRIC_REFERENCE: u16 = 50879; -pub const CAMERA_CALIBRATION_SIGNATURE: u16 = 50931; -pub const PROFILE_CALIBRATION_SIGNATURE: u16 = 50932; -pub const AS_SHOT_PROFILE_NAME: u16 = 50934; -pub const PROFILE_NAME: u16 = 50936; -pub const PROFILE_HUE_SAT_MAP_DIMS: u16 = 50937; -pub const PROFILE_HUE_SAT_MAP_DATA_1: u16 = 50938; -pub const PROFILE_HUE_SAT_MAP_DATA_2: u16 = 50939; -pub const PROFILE_TONE_CURVE: u16 = 50940; -pub const PROFILE_EMBED_POLICY: u16 = 50941; -pub const PROFILE_LOOK_TABLE_DIMS: u16 = 50981; -pub const PROFILE_LOOK_TABLE_DATA: u16 = 50982; -pub const PROFILE_HUE_SAT_MAP_ENCODING: u16 = 51107; -pub const PROFILE_LOOK_TABLE_ENCODING: u16 = 51108; -pub const BASELINE_EXPOSURE_OFFSET: u16 = 51109; -pub const DEFAULT_BLACK_RENDER: u16 = 51110; -pub const FORWARD_MATRIX_1: u16 = 50964; -pub const FORWARD_MATRIX_2: u16 = 50965; -pub const PREVIEW_COLOR_SPACE: u16 = 50970; -pub const CURRENT_PREVIEW_DATE_TIME: u16 = 50971; -pub const OPCODE_LIST_1: u16 = 51008; -pub const OPCODE_LIST_2: u16 = 51009; -pub const OPCODE_LIST_3: u16 = 51022; -pub const NOISE_PROFILE: u16 = 51041; -pub const DEFAULT_USER_CROP: u16 = 51125; -pub const PROFILE_GAIN_TABLE_MAP: u16 = 52525; -/// DNG 1.7 row interleave: the stored raster holds this many interleaved row groups (each group is -/// every `n`-th row of the image), so it must be un-interleaved before the data is a mosaic. -pub const ROW_INTERLEAVE_FACTOR: u16 = 50975; -/// DNG 1.7 column interleave, the column-wise counterpart of [`ROW_INTERLEAVE_FACTOR`]. -pub const COLUMN_INTERLEAVE_FACTOR: u16 = 52547; -pub const CALIBRATION_ILLUMINANT_3: u16 = 52529; -pub const CAMERA_CALIBRATION_3: u16 = 52530; -pub const COLOR_MATRIX_3: u16 = 52531; -pub const FORWARD_MATRIX_3: u16 = 52532; -pub const ILLUMINANT_DATA_1: u16 = 52533; -pub const ILLUMINANT_DATA_2: u16 = 52534; -pub const ILLUMINANT_DATA_3: u16 = 52535; -// DNG 1.6 semantic masks (spec chapter 4, "Semantic Masks") -pub const SEMANTIC_NAME: u16 = 52526; -pub const SEMANTIC_INSTANCE_ID: u16 = 52528; -pub const MASK_SUB_AREA: u16 = 52536; -/// NewSubFileType of a semantic mask IFD. -pub const SUBFILE_SEMANTIC_MASK: u32 = 0x10004; -pub const PROFILE_GAIN_TABLE_MAP_2: u16 = 52544; - -// --- Compression codes (TIFF 6.0, DNG, and widely used registrations) -pub mod compression { - pub const NONE: u16 = 1; - pub const LZW: u16 = 5; - pub const OLD_JPEG: u16 = 6; - pub const JPEG: u16 = 7; - pub const ADOBE_DEFLATE: u16 = 8; - pub const DEFLATE: u16 = 32946; - pub const PACKBITS: u16 = 32773; - /// DNG lossy JPEG (baseline DCT, 8-bit). - pub const LOSSY_JPEG: u16 = 34892; - pub const JPEG_XL: u16 = 52546; - /// SMPTE ST 2073 (VC-5) as written by GoPro's GPR files (the value measured in them). - pub const VC5: u16 = 9; - /// Nikon NEF Huffman compression. - pub const NIKON: u16 = 34713; - /// Sony ARW compressed (the value used by ARW files). - pub const SONY_ARW: u16 = 32767; -} - -// --- PhotometricInterpretation -pub mod photometric { - pub const WHITE_IS_ZERO: u16 = 0; - pub const BLACK_IS_ZERO: u16 = 1; - pub const RGB: u16 = 2; - pub const YCBCR: u16 = 6; - pub const CFA: u16 = 32803; - pub const LINEAR_RAW: u16 = 34892; - /// DNG 1.6 semantic mask. - pub const MASK: u16 = 52527; -} diff --git a/crates/tiff/src/writer.rs b/crates/tiff/src/writer.rs deleted file mode 100644 index 5732e7d060..0000000000 --- a/crates/tiff/src/writer.rs +++ /dev/null @@ -1,293 +0,0 @@ -//! TIFF writer: IFD trees with strips / tiles, `SubIFDs` and pointer IFDs (Exif, GPS, Interop, …). -//! -//! Layout: header, then for each IFD of the main chain its image chunks, child IFDs, out-of-line values -//! and finally the IFD itself (all word-aligned). Children are written before their parent so every -//! pointer is known when the parent's entries are emitted; only the chain's next-IFD pointers are patched. - -use crate::{ByteOrder, Result, TiffError, Value, tags}; -use std::collections::BTreeMap; - -/// Pixel data attached to an IFD. The writer fills in the offset / byte-count tags. -#[derive(Clone, Debug, PartialEq)] -pub enum ImageData { - /// Strips (each already compressed as the IFD's `Compression` says). Sets `RowsPerStrip`. - Strips { rows_per_strip: u32, strips: Vec> }, - /// Tiles in row-major tile order. Sets `TileWidth` / `TileLength`. - Tiles { tile_width: u32, tile_height: u32, tiles: Vec> }, -} - -/// An IFD to be written. -#[derive(Clone, Debug, Default, PartialEq)] -pub struct IfdBuilder { - pub entries: BTreeMap, - /// Written and referenced through the `SubIFDs` (330) tag. - pub sub_ifds: Vec, - /// Pointer IFDs keyed by the pointer tag (e.g. [`tags::EXIF_IFD`], [`tags::GPS_IFD`], [`tags::INTEROP_IFD`]). - pub children: BTreeMap, - pub image: Option, -} - -impl IfdBuilder { - pub fn new() -> Self { - Self::default() - } - pub fn set(&mut self, tag: u16, value: Value) -> &mut Self { - self.entries.insert(tag, value); - self - } - /// Builder-style [`IfdBuilder::set`]. - pub fn with(mut self, tag: u16, value: Value) -> Self { - self.entries.insert(tag, value); - self - } - pub fn set_child(&mut self, tag: u16, child: IfdBuilder) -> &mut Self { - self.children.insert(tag, child); - self - } - pub fn add_sub_ifd(&mut self, sub: IfdBuilder) -> &mut Self { - self.sub_ifds.push(sub); - self - } - pub fn set_image(&mut self, image: ImageData) -> &mut Self { - self.image = Some(image); - self - } -} - -/// Serialises IFD chains. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub struct TiffWriter { - pub order: ByteOrder, - pub bigtiff: bool, -} - -impl TiffWriter { - pub fn new(order: ByteOrder, bigtiff: bool) -> Self { - Self { order, bigtiff } - } - - /// Write `chain` (IFD0, IFD1, …) as a complete TIFF stream. - pub fn write(&self, chain: &[IfdBuilder]) -> Result> { - if chain.is_empty() { - return Err(TiffError::Invalid("no IFDs to write".into())); - } - let o = self.order; - let mut buf = Vec::new(); - buf.extend_from_slice(match o { - ByteOrder::Little => b"II", - ByteOrder::Big => b"MM", - }); - let mut patch = if self.bigtiff { - o.put_u16(&mut buf, 43); - o.put_u16(&mut buf, 8); - o.put_u16(&mut buf, 0); - let p = buf.len(); - o.put_u64(&mut buf, 0); - p - } else { - o.put_u16(&mut buf, 42); - let p = buf.len(); - o.put_u32(&mut buf, 0); - p - }; - for ifd in chain { - let (off, next_pos) = self.write_ifd(&mut buf, ifd, 0)?; - self.patch_offset(&mut buf, patch, off)?; - patch = next_pos; - } - Ok(buf) - } - - fn patch_offset(&self, buf: &mut [u8], pos: usize, v: u64) -> Result<()> { - let mut tmp = Vec::with_capacity(8); - if self.bigtiff { - self.order.put_u64(&mut tmp, v); - } else { - self.order.put_u32(&mut tmp, u32::try_from(v).map_err(|_| TiffError::Limit("classic TIFF larger than 4 GiB"))?); - } - buf[pos..pos + tmp.len()].copy_from_slice(&tmp); - Ok(()) - } - - fn offset_value(&self, v: Vec) -> Result { - if self.bigtiff { - Ok(Value::Long8(v)) - } else { - Ok(Value::Long( - v.into_iter().map(|x| u32::try_from(x).map_err(|_| TiffError::Limit("classic TIFF larger than 4 GiB"))).collect::>()?, - )) - } - } - - /// Writes the IFD (and everything it references); returns (IFD offset, position of its next-IFD pointer). - fn write_ifd(&self, buf: &mut Vec, ifd: &IfdBuilder, depth: usize) -> Result<(u64, usize)> { - if depth > 16 { - return Err(TiffError::Limit("IFD nesting depth")); - } - let o = self.order; - let mut entries = ifd.entries.clone(); - if let Some(img) = &ifd.image { - let chunks = match img { - ImageData::Strips { rows_per_strip, strips } => { - entries.insert(tags::ROWS_PER_STRIP, Value::Long(vec![*rows_per_strip])); - strips - } - ImageData::Tiles { tile_width, tile_height, tiles } => { - entries.insert(tags::TILE_WIDTH, Value::Long(vec![*tile_width])); - entries.insert(tags::TILE_LENGTH, Value::Long(vec![*tile_height])); - tiles - } - }; - let mut offs = Vec::with_capacity(chunks.len()); - let mut counts = Vec::with_capacity(chunks.len()); - for c in chunks { - align(buf); - offs.push(buf.len() as u64); - counts.push(c.len() as u64); - buf.extend_from_slice(c); - } - let (ot, ct) = match img { - ImageData::Strips { .. } => (tags::STRIP_OFFSETS, tags::STRIP_BYTE_COUNTS), - ImageData::Tiles { .. } => (tags::TILE_OFFSETS, tags::TILE_BYTE_COUNTS), - }; - entries.insert(ot, self.offset_value(offs)?); - entries.insert(ct, self.offset_value(counts)?); - } - if !ifd.sub_ifds.is_empty() { - let mut offs = Vec::new(); - for s in &ifd.sub_ifds { - offs.push(self.write_ifd(buf, s, depth + 1)?.0); - } - entries.insert(tags::SUB_IFDS, self.offset_value(offs)?); - } - for (&tag, child) in &ifd.children { - let off = self.write_ifd(buf, child, depth + 1)?.0; - entries.insert(tag, self.offset_value(vec![off])?); - } - let inline = if self.bigtiff { 8 } else { 4 }; - // out-of-line values - let mut encoded: Vec<(u16, &Value, Vec, Option)> = Vec::with_capacity(entries.len()); - for (&tag, v) in &entries { - let bytes = encode(o, v); - let pos = if bytes.len() > inline { - align(buf); - let p = buf.len() as u64; - buf.extend_from_slice(&bytes); - Some(p) - } else { - None - }; - encoded.push((tag, v, bytes, pos)); - } - align(buf); - let ifd_off = buf.len() as u64; - if !self.bigtiff && ifd_off > u32::MAX as u64 { - return Err(TiffError::Limit("classic TIFF larger than 4 GiB")); - } - if self.bigtiff { - o.put_u64(buf, encoded.len() as u64); - } else { - o.put_u16(buf, u16::try_from(encoded.len()).map_err(|_| TiffError::Limit("more than 65535 entries"))?); - } - for (tag, v, bytes, pos) in &encoded { - o.put_u16(buf, *tag); - o.put_u16(buf, v.field_type() as u16); - if self.bigtiff { - o.put_u64(buf, v.count() as u64); - } else { - o.put_u32(buf, u32::try_from(v.count()).map_err(|_| TiffError::Limit("value count"))?); - } - match pos { - Some(p) => { - if self.bigtiff { - o.put_u64(buf, *p); - } else { - o.put_u32(buf, u32::try_from(*p).map_err(|_| TiffError::Limit("classic TIFF larger than 4 GiB"))?); - } - } - None => { - buf.extend_from_slice(bytes); - buf.extend(std::iter::repeat_n(0u8, inline - bytes.len())); - } - } - } - let next_pos = buf.len(); - if self.bigtiff { - o.put_u64(buf, 0); - } else { - o.put_u32(buf, 0); - } - Ok((ifd_off, next_pos)) - } -} - -fn align(buf: &mut Vec) { - if buf.len() % 2 == 1 { - buf.push(0); - } -} - -/// Serialise a value's bytes in `o` order. -pub fn encode(o: ByteOrder, v: &Value) -> Vec { - let mut b = Vec::new(); - match v { - Value::Byte(x) | Value::Undefined(x) => b.extend_from_slice(x), - Value::Ascii(s) => { - b.extend_from_slice(s.as_bytes()); - b.push(0); - } - Value::SByte(x) => b.extend(x.iter().map(|&c| c as u8)), - Value::Short(x) => x.iter().for_each(|&c| o.put_u16(&mut b, c)), - Value::SShort(x) => x.iter().for_each(|&c| o.put_u16(&mut b, c as u16)), - Value::Long(x) | Value::Ifd(x) => x.iter().for_each(|&c| o.put_u32(&mut b, c)), - Value::SLong(x) => x.iter().for_each(|&c| o.put_u32(&mut b, c as u32)), - Value::Float(x) => x.iter().for_each(|&c| o.put_u32(&mut b, c.to_bits())), - Value::Rational(x) => x.iter().for_each(|&(n, d)| { - o.put_u32(&mut b, n); - o.put_u32(&mut b, d); - }), - Value::SRational(x) => x.iter().for_each(|&(n, d)| { - o.put_u32(&mut b, n as u32); - o.put_u32(&mut b, d as u32); - }), - Value::Double(x) => x.iter().for_each(|&c| o.put_u64(&mut b, c.to_bits())), - Value::Long8(x) | Value::Ifd8(x) => x.iter().for_each(|&c| o.put_u64(&mut b, c)), - Value::SLong8(x) => x.iter().for_each(|&c| o.put_u64(&mut b, c as u64)), - } - b -} - -/// Convenience: approximate `v` by an unsigned rational with denominator up to 1e6 (for writing Exif rationals). -pub fn rational(v: f64) -> (u32, u32) { - if !v.is_finite() || v <= 0.0 { - return (0, 1); - } - for d in [1u32, 10, 100, 1000, 10_000, 100_000, 1_000_000] { - let n = v * d as f64; - if n > u32::MAX as f64 { - return ((v * (d / 10).max(1) as f64).round().min(u32::MAX as f64) as u32, (d / 10).max(1)); - } - if (n - n.round()).abs() < 1e-9 * d as f64 || d == 1_000_000 { - return (n.round() as u32, d); - } - } - (0, 1) -} - -/// Signed variant of [`rational`]. -pub fn srational(v: f64) -> (i32, i32) { - if !v.is_finite() { - return (0, 1); - } - for d in [1i32, 10, 100, 1000, 10_000, 100_000, 1_000_000] { - let n = v * d as f64; - if n.abs() > i32::MAX as f64 { - let d = (d / 10).max(1); - return ((v * d as f64).round().clamp(i32::MIN as f64, i32::MAX as f64) as i32, d); - } - if (n - n.round()).abs() < 1e-9 * d as f64 || d == 1_000_000 { - return (n.round() as i32, d); - } - } - (0, 1) -} diff --git a/crates/tiff/tests/it/main.rs b/crates/tiff/tests/it/main.rs deleted file mode 100644 index e415ed5377..0000000000 --- a/crates/tiff/tests/it/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -//! The integration tests, one module per file, linked into one test binary. - -mod roundtrip; diff --git a/crates/tiff/tests/it/roundtrip.rs b/crates/tiff/tests/it/roundtrip.rs deleted file mode 100644 index 3b9a1bd1cf..0000000000 --- a/crates/tiff/tests/it/roundtrip.rs +++ /dev/null @@ -1,429 +0,0 @@ -use lightcraft_tiff::image::{Layout, chunk_bytes}; -use lightcraft_tiff::writer::{rational, srational}; -use lightcraft_tiff::{ByteOrder, FieldType, IfdBuilder, ImageData, ParseOptions, Tiff, TiffError, TiffWriter, Value, parse_ifd_at, tags}; -use proptest::prelude::*; - -const ORDERS: [ByteOrder; 2] = [ByteOrder::Little, ByteOrder::Big]; - -fn all_values() -> Vec<(u16, Value)> { - vec![ - (1000, Value::Byte(vec![1, 2, 3, 255])), - (1001, Value::Ascii("hello tiff".into())), - (1002, Value::Short(vec![1, 65535, 3])), - (1003, Value::Long(vec![7])), - (1004, Value::Rational(vec![(1, 3), (22, 7)])), - (1005, Value::SByte(vec![-1, 0, 127, -128])), - (1006, Value::Undefined(vec![9; 17])), - (1007, Value::SShort(vec![-32768, 32767])), - (1008, Value::SLong(vec![-5, i32::MAX])), - (1009, Value::SRational(vec![(-1, 3), (5, -2)])), - (1010, Value::Float(vec![1.5, -0.25, f32::MAX])), - (1011, Value::Double(vec![std::f64::consts::PI])), - (1012, Value::Ifd(vec![0])), - (1013, Value::Short(vec![42])), - (1014, Value::Ascii(String::new())), - ] -} - -fn big_values() -> Vec<(u16, Value)> { - vec![(1020, Value::Long8(vec![1 << 40, 3])), (1021, Value::SLong8(vec![-(1 << 50)])), (1022, Value::Ifd8(vec![0]))] -} - -#[test] -fn all_field_types_roundtrip_every_variant() { - for order in ORDERS { - for big in [false, true] { - let mut ifd = IfdBuilder::new(); - for (t, v) in all_values() { - ifd.set(t, v); - } - if big { - for (t, v) in big_values() { - ifd.set(t, v); - } - } - let bytes = TiffWriter::new(order, big).write(&[ifd]).unwrap(); - let t = Tiff::parse(&bytes).unwrap(); - assert_eq!(t.order, order); - assert_eq!(t.bigtiff, big); - assert_eq!(Tiff::sniff(&bytes), Some((order, big))); - let ifd0 = &t.ifds[0]; - for (tag, v) in all_values().into_iter().chain(if big { big_values() } else { vec![] }) { - assert_eq!(ifd0.value(tag), Some(&v), "tag {tag} order {order:?} big {big}"); - } - } - } -} - -#[test] -fn accessors() { - let mut ifd = IfdBuilder::new(); - ifd.set(1, Value::Rational(vec![(28, 10), (0, 0)])); - ifd.set(2, Value::Short(vec![5, 6])); - ifd.set(3, Value::SLong(vec![-3])); - ifd.set(4, Value::Ascii("Canon ".into())); - ifd.set(5, Value::Byte(b"\0junk".to_vec())); - let bytes = TiffWriter::default().write(&[ifd]).unwrap(); - let t = Tiff::parse(&bytes).unwrap(); - let i = &t.ifds[0]; - assert_eq!(i.f64(1), Some(2.8)); - assert_eq!(i.f64s(1).unwrap(), vec![2.8, 0.0]); - assert_eq!(i.u32(2), Some(5)); - assert_eq!(i.u64s(2).unwrap(), vec![5, 6]); - assert_eq!(i.u32(3), None); - assert_eq!(i.i64(3), Some(-3)); - assert_eq!(i.string(4).as_deref(), Some("Canon")); - assert_eq!(i.string(5).as_deref(), Some("")); - assert_eq!(i.get(4).unwrap().field_type(), FieldType::Ascii); - assert!(i.u32(999).is_none()); - assert_eq!(t.find(2).map(|e| e.tag), Some(2)); -} - -#[test] -fn chain_subifds_and_pointer_ifds() { - for order in ORDERS { - for big in [false, true] { - let mut interop = IfdBuilder::new(); - interop.set(1, Value::Ascii("R98".into())); - let mut exif = IfdBuilder::new(); - exif.set(tags::F_NUMBER, Value::Rational(vec![(56, 10)])); - exif.set_child(tags::INTEROP_IFD, interop); - let mut gps = IfdBuilder::new(); - gps.set(tags::GPS_LATITUDE_REF, Value::Ascii("N".into())); - let mut ifd0 = IfdBuilder::new(); - ifd0.set(tags::MAKE, Value::Ascii("Maker".into())); - ifd0.set_child(tags::EXIF_IFD, exif); - ifd0.set_child(tags::GPS_IFD, gps); - let mut raw = IfdBuilder::new(); - raw.set(tags::IMAGE_WIDTH, Value::Long(vec![4])); - let mut nested = IfdBuilder::new(); - nested.set(tags::IMAGE_WIDTH, Value::Long(vec![2])); - raw.add_sub_ifd(nested); - ifd0.add_sub_ifd(raw); - ifd0.add_sub_ifd(IfdBuilder::new().with(tags::IMAGE_WIDTH, Value::Short(vec![3]))); - let ifd1 = IfdBuilder::new().with(tags::IMAGE_WIDTH, Value::Short(vec![160])); - let ifd2 = IfdBuilder::new().with(tags::IMAGE_WIDTH, Value::Short(vec![80])); - let bytes = TiffWriter::new(order, big).write(&[ifd0, ifd1, ifd2]).unwrap(); - let t = Tiff::parse(&bytes).unwrap(); - assert_eq!(t.ifds.len(), 3); - assert_eq!(t.ifds[1].u32(tags::IMAGE_WIDTH), Some(160)); - assert_eq!(t.ifds[2].u32(tags::IMAGE_WIDTH), Some(80)); - let i0 = &t.ifds[0]; - assert_eq!(i0.sub_ifds.len(), 2); - assert_eq!(i0.sub_ifds[0].u32(tags::IMAGE_WIDTH), Some(4)); - assert_eq!(i0.sub_ifds[0].sub_ifds[0].u32(tags::IMAGE_WIDTH), Some(2)); - assert_eq!(i0.sub_ifds[1].u32(tags::IMAGE_WIDTH), Some(3)); - let exif = t.exif().unwrap(); - assert_eq!(exif.f64(tags::F_NUMBER), Some(5.6)); - assert_eq!(exif.interop.as_ref().unwrap().string(1).as_deref(), Some("R98")); - assert_eq!(t.gps().unwrap().string(tags::GPS_LATITUDE_REF).as_deref(), Some("N")); - // IFD0, sub0, sub0.sub, sub1, exif, interop, gps, ifd1, ifd2 - assert_eq!(t.all_ifds().len(), 9); - } - } -} - -fn gradient(w: usize, h: usize) -> Vec { - (0..w * h).map(|i| (i * 7 % 251) as u8).collect() -} - -#[test] -fn strips_layout_and_bytes() { - let (w, h) = (10usize, 7usize); - let px = gradient(w, h); - let rps = 3; - let strips: Vec> = px.chunks(w * rps).map(|c| c.to_vec()).collect(); - let mut ifd = IfdBuilder::new(); - ifd.set(tags::IMAGE_WIDTH, Value::Long(vec![w as u32])); - ifd.set(tags::IMAGE_LENGTH, Value::Long(vec![h as u32])); - ifd.set(tags::BITS_PER_SAMPLE, Value::Short(vec![8])); - ifd.set(tags::COMPRESSION, Value::Short(vec![1])); - ifd.set_image(ImageData::Strips { rows_per_strip: rps as u32, strips }); - for order in ORDERS { - for big in [false, true] { - let bytes = TiffWriter::new(order, big).write(std::slice::from_ref(&ifd)).unwrap(); - let t = Tiff::parse(&bytes).unwrap(); - let info = t.ifds[0].image().unwrap(); - assert_eq!(info.layout, Layout::Strips { rows_per_strip: 3 }); - assert_eq!(info.grid(), (1, 3)); - let chunks = info.chunks(bytes.len() as u64); - assert_eq!(chunks.len(), 3); - assert_eq!(chunks[2].height, 1); - let mut out = Vec::new(); - for c in &chunks { - out.extend_from_slice(chunk_bytes(&bytes, c).unwrap()); - } - assert_eq!(out, px); - } - } -} - -#[test] -fn tiles_layout_and_bytes() { - let (w, h, tw, th) = (20usize, 13usize, 16usize, 8usize); - let px = gradient(w, h); - let (ta, td) = (w.div_ceil(tw), h.div_ceil(th)); - let mut tiles = Vec::new(); - for ty in 0..td { - for tx in 0..ta { - let mut t = vec![0u8; tw * th]; - for y in 0..th { - for x in 0..tw { - let (sx, sy) = (tx * tw + x, ty * th + y); - if sx < w && sy < h { - t[y * tw + x] = px[sy * w + sx]; - } - } - } - tiles.push(t); - } - } - let mut ifd = IfdBuilder::new(); - ifd.set(tags::IMAGE_WIDTH, Value::Short(vec![w as u16])); - ifd.set(tags::IMAGE_LENGTH, Value::Short(vec![h as u16])); - ifd.set_image(ImageData::Tiles { tile_width: tw as u32, tile_height: th as u32, tiles }); - let bytes = TiffWriter::new(ByteOrder::Big, false).write(&[ifd]).unwrap(); - let t = Tiff::parse(&bytes).unwrap(); - let info = t.ifds[0].image().unwrap(); - assert_eq!(info.grid(), (2, 2)); - let chunks = info.chunks(bytes.len() as u64); - assert_eq!(chunks.len(), 4); - let mut out = vec![0u8; w * h]; - for c in &chunks { - let b = chunk_bytes(&bytes, c).unwrap(); - for y in 0..c.height as usize { - for x in 0..c.width as usize { - let (sx, sy) = (c.x as usize + x, c.y as usize + y); - if sx < w && sy < h { - out[sy * w + sx] = b[y * tw + x]; - } - } - } - } - assert_eq!(out, px); -} - -#[test] -fn missing_byte_counts_are_inferred() { - let mut ifd = IfdBuilder::new(); - ifd.set(tags::IMAGE_WIDTH, Value::Long(vec![4])); - ifd.set(tags::IMAGE_LENGTH, Value::Long(vec![2])); - ifd.set_image(ImageData::Strips { rows_per_strip: 1, strips: vec![vec![1; 4], vec![2; 4]] }); - let bytes = TiffWriter::default().write(&[ifd]).unwrap(); - let t = Tiff::parse(&bytes).unwrap(); - let mut info = t.ifds[0].image().unwrap(); - info.byte_counts.clear(); - let c = info.chunks(bytes.len() as u64); - assert_eq!(c[0].len, 4); - assert_eq!(&chunk_bytes(&bytes, &c[0]).unwrap()[..4], &[1; 4]); - assert!(c[1].len >= 4); -} - -#[test] -fn bad_headers() { - assert_eq!(Tiff::parse(b""), Err(TiffError::NotTiff)); - assert_eq!(Tiff::parse(b"XX*\0\0\0\0\0"), Err(TiffError::NotTiff)); - assert_eq!(Tiff::parse(b"II\x2b\0\x04\0\0\0"), Err(TiffError::NotTiff)); - assert!(Tiff::parse(b"II*\0").is_err()); - assert!(Tiff::parse(b"II*\0\x08\0\0\0").is_err()); - assert!(Tiff::parse(b"II*\0\x08\0\0\0\0\0").is_err()); - assert!(Tiff::sniff(b"MM\0*\0\0\0\x08").is_some()); -} - -#[test] -fn ifd_loops_are_detected() { - // IFD0 at 8 whose next pointer points back to itself, and whose SubIFDs point to itself too. - let mut b = b"II*\0\x08\0\0\0".to_vec(); - b.extend_from_slice(&1u16.to_le_bytes()); - b.extend_from_slice(&330u16.to_le_bytes()); - b.extend_from_slice(&4u16.to_le_bytes()); - b.extend_from_slice(&1u32.to_le_bytes()); - b.extend_from_slice(&8u32.to_le_bytes()); - b.extend_from_slice(&8u32.to_le_bytes()); - let t = Tiff::parse(&b).unwrap(); - assert_eq!(t.ifds.len(), 1); - assert!(t.ifds[0].sub_ifds.is_empty()); -} - -#[test] -fn out_of_range_entries_are_skipped() { - let mut ifd = IfdBuilder::new(); - ifd.set(1, Value::Long(vec![1, 2, 3])); - ifd.set(2, Value::Short(vec![5])); - let mut bytes = TiffWriter::default().write(&[ifd]).unwrap(); - let t = Tiff::parse(&bytes).unwrap(); - let e = t.ifds[0].get(1).unwrap().clone(); - // point tag 1's value offset past EOF - let ifd_off = t.ifds[0].offset as usize; - let field = ifd_off + 2 + 8; - assert_eq!(u32::from_le_bytes(bytes[field..field + 4].try_into().unwrap()) as u64, e.offset); - bytes[field..field + 4].copy_from_slice(&0xffff_fff0u32.to_le_bytes()); - let t = Tiff::parse(&bytes).unwrap(); - assert!(t.ifds[0].get(1).is_none()); - assert_eq!(t.ifds[0].u32(2), Some(5)); -} - -#[test] -fn value_budget_limits_allocation() { - // 50 entries all pointing at the same 1000-byte blob. - let mut ifd = IfdBuilder::new(); - ifd.set(1, Value::Undefined(vec![7; 1000])); - let bytes = TiffWriter::default().write(&[ifd]).unwrap(); - let t = Tiff::parse(&bytes).unwrap(); - let blob = t.ifds[0].get(1).unwrap().offset as u32; - let mut b = b"II*\0".to_vec(); - b.extend_from_slice(&(bytes.len() as u32).to_le_bytes()); - let mut file = bytes.clone(); - file[..8].copy_from_slice(&b); - file.extend_from_slice(&50u16.to_le_bytes()); - for i in 0..50u16 { - file.extend_from_slice(&i.to_le_bytes()); - file.extend_from_slice(&7u16.to_le_bytes()); - file.extend_from_slice(&1000u32.to_le_bytes()); - file.extend_from_slice(&blob.to_le_bytes()); - } - file.extend_from_slice(&0u32.to_le_bytes()); - let opts = ParseOptions { value_budget: Some(10_000), ..Default::default() }; - let t = Tiff::parse_with(&file, &opts).unwrap(); - assert_eq!(t.ifds[0].entries.len(), 10); - let t = Tiff::parse(&file).unwrap(); - assert_eq!(t.ifds[0].entries.len(), 50); -} - -#[test] -fn parse_ifd_with_base() { - // an IFD blob embedded at 100 whose offsets are relative to 100 - let mut blob = Vec::new(); - let o = ByteOrder::Big; - o.put_u16(&mut blob, 1); - o.put_u16(&mut blob, 5); - o.put_u16(&mut blob, 4); - o.put_u32(&mut blob, 3); - o.put_u32(&mut blob, 18); - o.put_u32(&mut blob, 0); - for v in [10u32, 20, 30] { - o.put_u32(&mut blob, v); - } - let mut data = vec![0u8; 100]; - data.extend(blob); - let (ifd, next) = parse_ifd_at(&data, 100, o, 100, false, &ParseOptions::default()).unwrap(); - assert_eq!(next, 0); - assert_eq!(ifd.u64s(5).unwrap(), vec![10, 20, 30]); - assert_eq!(ifd.get(5).unwrap().offset, 118); -} - -#[test] -fn rationals() { - assert_eq!(rational(2.8), (28, 10)); - assert_eq!(rational(0.004), (4, 1000)); - assert_eq!(rational(-1.0), (0, 1)); - assert_eq!(srational(-0.333), (-333, 1000)); - assert_eq!(srational(7.0), (7, 1)); - let (n, d) = rational(1.0 / 3.0); - assert!((n as f64 / d as f64 - 1.0 / 3.0).abs() < 1e-6); -} - -fn sample_file() -> Vec { - let mut exif = IfdBuilder::new(); - exif.set(tags::F_NUMBER, Value::Rational(vec![(4, 1)])); - exif.set(tags::MAKER_NOTE, Value::Undefined(b"Nikon\0\x02\x10\0\0II*\0\x08\0\0\0\x01\0\x01\0\x03\0\x01\0\0\0\x05\0\0\0\0\0\0\0".to_vec())); - let mut ifd0 = IfdBuilder::new(); - ifd0.set(tags::IMAGE_WIDTH, Value::Long(vec![8])); - ifd0.set(tags::IMAGE_LENGTH, Value::Long(vec![4])); - ifd0.set(tags::MAKE, Value::Ascii("NIKON".into())); - ifd0.set_child(tags::EXIF_IFD, exif); - ifd0.set_image(ImageData::Strips { rows_per_strip: 2, strips: vec![vec![1; 16], vec![2; 16]] }); - ifd0.add_sub_ifd(IfdBuilder::new().with(tags::IMAGE_WIDTH, Value::Long(vec![2]))); - TiffWriter::default().write(&[ifd0, IfdBuilder::new().with(1, Value::Short(vec![1]))]).unwrap() -} - -fn exercise(t: &Tiff, data: &[u8]) { - for ifd in t.all_ifds() { - if let Ok(info) = ifd.image() { - for c in info.chunks(data.len() as u64).iter().take(64) { - let _ = chunk_bytes(data, c); - } - } - for e in &ifd.entries { - let _ = e.value.to_f64_vec(); - let _ = e.value.as_str(); - } - } - if let Some(e) = t.exif().and_then(|x| x.get(tags::MAKER_NOTE)) { - let _ = lightcraft_tiff::makernote::parse_makernote(data, e.offset, e.count() as u64, t.order, "NIKON"); - } -} - -proptest! { - #![proptest_config(ProptestConfig { cases: 2000, .. ProptestConfig::default() })] - - #[test] - fn random_bytes_never_panic(mut data in proptest::collection::vec(any::(), 0..512), hdr in 0usize..4) { - let heads: [&[u8]; 4] = [b"II*\0", b"MM\0*", b"II+\0\x08\0\0\0", b"MM\0+\0\x08\0\0"]; - let h = heads[hdr]; - if data.len() >= h.len() { - data[..h.len()].copy_from_slice(h); - } - if let Ok(t) = Tiff::parse(&data) { - exercise(&t, &data); - } - } - - #[test] - fn mutated_files_never_panic(flips in proptest::collection::vec((any::(), any::()), 1..16), cut in any::()) { - let mut data = sample_file(); - for (i, v) in flips { - let n = data.len(); - data[i % n] = v; - } - let keep = if cut % 4 == 0 { cut % data.len() } else { data.len() }; - let data = &data[..keep]; - if let Ok(t) = Tiff::parse(data) { - exercise(&t, data); - } - } - - #[test] - fn random_short_arrays_roundtrip(v in proptest::collection::vec(any::(), 1..300), big in any::(), le in any::()) { - let order = if le { ByteOrder::Little } else { ByteOrder::Big }; - let ifd = IfdBuilder::new().with(500, Value::Short(v.clone())).with(501, Value::Double(v.iter().map(|&x| x as f64 / 3.0).collect())); - let bytes = TiffWriter::new(order, big).write(&[ifd]).unwrap(); - let t = Tiff::parse(&bytes).unwrap(); - prop_assert_eq!(t.ifds[0].value(500), Some(&Value::Short(v.clone()))); - prop_assert_eq!(t.ifds[0].f64s(501).unwrap().len(), v.len()); - } -} - -/// A BigTIFF whose IFD says it has an enormous number of entries, with one readable, computed where the next-IFD -/// offset would be from the advertised count: an add overflow, a panic in builds with overflow checks (issue -/// #1516). The readable entry is kept and there is no next IFD; complete chains still link. -#[test] -fn bigtiff_with_an_enormous_entry_count_does_not_panic() { - for order in ORDERS { - for count in [u64::MAX, 1 << 62, 2] { - // little-endian bytes, reversed for big-endian files - let e = |le: &[u8]| -> Vec { if order == ByteOrder::Little { le.to_vec() } else { le.iter().rev().copied().collect() } }; - let mut f = if order == ByteOrder::Little { b"II".to_vec() } else { b"MM".to_vec() }; - for part in [e(&43u16.to_le_bytes()), e(&8u16.to_le_bytes()), e(&0u16.to_le_bytes()), e(&16u64.to_le_bytes()), e(&count.to_le_bytes())] { - f.extend(part); - } - // one entry: ImageWidth, LONG, 1 value (inline in the 8-byte field) - for part in [e(&tags::IMAGE_WIDTH.to_le_bytes()), e(&4u16.to_le_bytes()), e(&1u64.to_le_bytes()), e(&640u32.to_le_bytes())] { - f.extend(part); - } - f.extend([0; 4]); - let (ifd, next) = parse_ifd_at(&f, 16, order, 0, true, &ParseOptions::default()).unwrap(); - assert_eq!(ifd.value(tags::IMAGE_WIDTH), Some(&Value::Long(vec![640])), "{order:?} {count}"); - assert_eq!(next, 0, "{order:?} {count}"); - let t = Tiff::parse(&f).unwrap(); - assert_eq!(t.ifds.len(), 1); - } - // a complete chain of two IFDs still links - let mut a = IfdBuilder::new(); - a.set(1000, Value::Short(vec![1])); - let mut b = IfdBuilder::new(); - b.set(1000, Value::Short(vec![2])); - let t = Tiff::parse(&TiffWriter::new(order, true).write(&[a, b]).unwrap()).unwrap(); - assert_eq!(t.ifds.len(), 2, "{order:?}"); - } -} diff --git a/crates/ui-egui/locales/cs.json b/crates/ui-egui/locales/cs.json index ea10219217..5ba9c5098f 100644 --- a/crates/ui-egui/locales/cs.json +++ b/crates/ui-egui/locales/cs.json @@ -2400,6 +2400,34 @@ "Save Current Settings as New Effect Preset…": "Uložit aktuální nastavení jako nové přednastavení efektu…", "Manage Effect Presets…": "Spravovat přednastavení efektů…", "Export Effect Presets…": "Exportovat přednastavení efektů…", + "Blur Amount": "Míra rozostření", + "Boost": "Zesílení", + "In Focus": "Zaostřeno", + "Blur what is out of focus, as a wide aperture would": "Rozostří, co je mimo zaostření, jako by to udělala široce otevřená clona", + "What stays sharp: the photo's subject, or the selection of one of its masks": "Co zůstane ostré: subjekt fotky nebo výběr jedné z jejích masek", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Tip: subjekt přesně vyberte maskou Objekt (Maskování, M) a pak tu masku zvolte zde.", + "Brush Refinement": "Doladění štětcem", + "Blur": "Rozostření", + "Paint what should stay sharp": "Namalujte, co má zůstat ostré", + "Paint what should be blurred": "Namalujte, co se má rozostřit", + "Remove the strokes of both brushes": "Odstranit tahy obou štětců", + "Find Subject": "Najít subjekt", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Vybrat subjekt pomocí AI (SAM 3), od místa, kam zaostřil fotoaparát, pokud ho soubor uvádí, a ponechat ho zaostřený", + "Selecting the subject at the camera's focus point…": "Vybírá se subjekt v bodě zaostření fotoaparátu…", + "Selecting the subject…": "Vybírá se subjekt…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "Dolaďte ho v Maskování (M): + → Objekt přidá, co chybí, − → Objekt část vynechá.", + "Finding the subject… The first time on a photo takes a while.": "Hledá se subjekt… Poprvé to u fotky chvíli trvá.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "Subjekt nebyl vybrán. Zkuste znovu Najít subjekt, nebo na něj klikněte v Maskování (M).", + "Transition": "Přechod", + "Selection": "Výběr", + "Outside": "Mimo výběr", + "Shape": "Tvar", + "Round": "Kruh", + "Auto: the photo's subject, estimated": "Automaticky: odhadnutý subjekt fotky", + "Horizontal oval": "Vodorovný ovál", + "Vertical oval": "Svislý ovál", + "Move & Resize": "Přesunout a změnit velikost", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Tažením tvaru na fotce ho přesunete; tažením boční nebo spodní úchytky ho roztáhnete. Tlačítko tvaru ho vrátí doprostřed.", "Before/After Side by Side": "Před/Po vedle sebe", "Clipping": "Ořezání tónů", "Mask Overlay": "Překrytí masky", diff --git a/crates/ui-egui/locales/de.json b/crates/ui-egui/locales/de.json index bc67606eb6..70fa252dc8 100644 --- a/crates/ui-egui/locales/de.json +++ b/crates/ui-egui/locales/de.json @@ -3433,6 +3433,34 @@ "Optional remote SAM 3: keep the native editor local and run Object, Describe, and detail": "Optionales entferntes SAM 3: Der native Editor bleibt lokal, und Objekt-, Beschreibungs- und Detail-", "inference on a Mac through SSH. Saved masks still render and export offline.": "Inferenz laufen über SSH auf einem Mac. Gespeicherte Masken werden weiterhin offline gerendert und exportiert.", "See [remote Metal inference](ai-masks.md#remote-metal-inference).": "Siehe [remote Metal inference](ai-masks.md#remote-metal-inference).", + "Blur Amount": "Unschärfestärke", + "Boost": "Verstärkung", + "In Focus": "Im Fokus", + "Blur what is out of focus, as a wide aperture would": "Zeichnet unscharf, was außerhalb des Fokus liegt, wie eine weit geöffnete Blende", + "What stays sharp: the photo's subject, or the selection of one of its masks": "Was scharf bleibt: das Motiv des Fotos oder die Auswahl einer seiner Masken", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Tipp: Wählen Sie das Motiv mit einer Objekt-Maske (Maskieren, M) genau aus und wählen Sie diese Maske dann hier.", + "Brush Refinement": "Pinselkorrektur", + "Blur": "Unschärfe", + "Paint what should stay sharp": "Malen, was scharf bleiben soll", + "Paint what should be blurred": "Malen, was unscharf werden soll", + "Remove the strokes of both brushes": "Die Striche beider Pinsel entfernen", + "Find Subject": "Motiv finden", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Das Motiv per KI (SAM 3) auswählen, ausgehend vom Fokuspunkt der Kamera, wenn die Datei ihn enthält, und scharf halten", + "Selecting the subject at the camera's focus point…": "Motiv am Fokuspunkt der Kamera wird ausgewählt…", + "Selecting the subject…": "Motiv wird ausgewählt…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "In Maskieren (M) verfeinern: + → Objekt fügt Fehlendes hinzu, − → Objekt lässt einen Teil weg.", + "Finding the subject… The first time on a photo takes a while.": "Motiv wird gesucht… Beim ersten Mal dauert das bei einem Foto eine Weile.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "Das Motiv wurde nicht ausgewählt. Versuchen Sie erneut „Motiv finden“ oder klicken Sie es in Maskieren (M) an.", + "Transition": "Übergang", + "Selection": "Auswahl", + "Outside": "Außerhalb", + "Shape": "Form", + "Round": "Rund", + "Auto: the photo's subject, estimated": "Automatisch: das geschätzte Motiv des Fotos", + "Horizontal oval": "Liegendes Oval", + "Vertical oval": "Stehendes Oval", + "Move & Resize": "Verschieben & Größe", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Ziehen Sie die Form auf dem Foto, um sie zu verschieben; ziehen Sie den seitlichen oder unteren Griff, um sie zu dehnen. Eine Form-Schaltfläche setzt sie wieder in die Mitte.", "Before/After Side by Side": "Vorher/Nachher nebeneinander", "Clipping": "Beschneidung", "Mask Overlay": "Maskenüberlagerung", diff --git a/crates/ui-egui/locales/es-formats.json b/crates/ui-egui/locales/es-formats.json index 77d40f0ec0..eb5edf4866 100644 --- a/crates/ui-egui/locales/es-formats.json +++ b/crates/ui-egui/locales/es-formats.json @@ -172,7 +172,7 @@ "{n} photo{} now · updates automatically as photos change": "{n} foto{} ahora · se actualiza automáticamente cuando cambian las fotos", "{n} selected photo{} · {pages} page{}": "{n} foto{0} seleccionada{0} · {pages} página{1}", "{pid} (missing)": "{pid} (falta)", - "{sel_n} selected · {counted}": "{sel_n} seleccionadas · {counted}", + "{sel_n} selected · {counted}": "Seleccionadas: {sel_n} · {counted}", "{stage}… {:.0}%": "{stage}… {:.0}%", "{verb} {n} Photo{}": "{verb} {n} foto{}", "{weekday}, {day}": "{weekday}, {day}", diff --git a/crates/ui-egui/locales/es.json b/crates/ui-egui/locales/es.json index c34057bdfe..7b07c40a1f 100644 --- a/crates/ui-egui/locales/es.json +++ b/crates/ui-egui/locales/es.json @@ -2358,6 +2358,34 @@ "CPU": "CPU", "A face model uses the GPU only when a check shows it gives the same results faster.": "Un modelo de caras usa la GPU solo cuando una comprobación muestra que da los mismos resultados más rápido.", "Checking the GPU for faces": "Comprobando la GPU para las caras", + "Blur Amount": "Cantidad de desenfoque", + "Boost": "Realce", + "In Focus": "Enfocado", + "Blur what is out of focus, as a wide aperture would": "Desenfoca lo que está fuera de foco, como lo haría una apertura amplia", + "What stays sharp: the photo's subject, or the selection of one of its masks": "Lo que queda nítido: el sujeto de la foto o la selección de una de sus máscaras", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Consejo: selecciona el sujeto con precisión con una máscara Objeto (Máscaras, M) y luego elige esa máscara aquí.", + "Brush Refinement": "Refinamiento con pincel", + "Blur": "Desenfoque", + "Paint what should stay sharp": "Pinta lo que debe quedar nítido", + "Paint what should be blurred": "Pinta lo que debe desenfocarse", + "Remove the strokes of both brushes": "Quitar los trazos de ambos pinceles", + "Find Subject": "Buscar sujeto", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Selecciona el sujeto con IA (SAM 3), empezando donde enfocó la cámara si el archivo lo registra, y lo mantiene enfocado", + "Selecting the subject at the camera's focus point…": "Seleccionando el sujeto en el punto de enfoque de la cámara…", + "Selecting the subject…": "Seleccionando el sujeto…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "Ajústalo en Máscaras (M): + → Objeto añade lo que faltó, − → Objeto excluye una parte.", + "Finding the subject… The first time on a photo takes a while.": "Buscando el sujeto… La primera vez en una foto tarda un poco.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "No se seleccionó el sujeto. Vuelve a probar Buscar sujeto o haz clic en él en Máscaras (M).", + "Transition": "Transición", + "Selection": "Selección", + "Outside": "Fuera", + "Shape": "Forma", + "Round": "Redonda", + "Auto: the photo's subject, estimated": "Automático: el sujeto estimado de la foto", + "Horizontal oval": "Óvalo horizontal", + "Vertical oval": "Óvalo vertical", + "Move & Resize": "Mover y redimensionar", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Arrastra la forma en la foto para moverla; arrastra su tirador lateral o inferior para estirarla. Un botón de forma la devuelve al centro.", "Layout Image": "Imagen de diseño", "Detail View": "Vista de detalle", "Edit Panel": "Panel Editar", diff --git a/crates/ui-egui/locales/fi.json b/crates/ui-egui/locales/fi.json index 59cc611cd6..f06ce0d4d2 100644 --- a/crates/ui-egui/locales/fi.json +++ b/crates/ui-egui/locales/fi.json @@ -2340,6 +2340,34 @@ "≤ at most": "≤ enintään", "≥ at least": "≥ vähintään", "★ Favorites": "★ Suosikit", + "Blur Amount": "Epätarkkuuden määrä", + "Boost": "Korostus", + "In Focus": "Tarkka alue", + "Blur what is out of focus, as a wide aperture would": "Sumentaa tarkennuksen ulkopuolisen alueen, kuten suuri aukko tekisi", + "What stays sharp: the photo's subject, or the selection of one of its masks": "Mikä pysyy tarkkana: kuvan kohde tai jonkin sen maskin valinta", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Vinkki: valitse kohde tarkasti Kohde-maskilla (Maskaus, M) ja valitse sitten se maski tästä.", + "Brush Refinement": "Siveltimellä tarkennus", + "Blur": "Sumennus", + "Paint what should stay sharp": "Maalaa, minkä pitää pysyä tarkkana", + "Paint what should be blurred": "Maalaa, minkä pitää sumentua", + "Remove the strokes of both brushes": "Poista molempien siveltimien vedot", + "Find Subject": "Etsi kohde", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Valitse kohde tekoälyllä (SAM 3) aloittaen kameran tarkennuskohdasta, jos tiedosto kertoo sen, ja pidä se tarkkana", + "Selecting the subject at the camera's focus point…": "Valitaan kohdetta kameran tarkennuspisteestä…", + "Selecting the subject…": "Valitaan kohdetta…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "Hienosäädä Maskauksessa (M): + → Kohde lisää puuttuvan, − → Kohde jättää osan pois.", + "Finding the subject… The first time on a photo takes a while.": "Etsitään kohdetta… Ensimmäisellä kerralla kuvassa tämä kestää hetken.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "Kohdetta ei valittu. Kokeile Etsi kohde uudelleen tai napsauta sitä Maskauksessa (M).", + "Transition": "Siirtymä", + "Selection": "Valinta", + "Outside": "Ulkopuoli", + "Shape": "Muoto", + "Round": "Pyöreä", + "Auto: the photo's subject, estimated": "Automaattinen: kuvan arvioitu kohde", + "Horizontal oval": "Vaakasuora soikio", + "Vertical oval": "Pystysuora soikio", + "Move & Resize": "Siirrä ja muuta kokoa", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Siirrä muotoa vetämällä sitä kuvassa; venytä sitä vetämällä sivu- tai alakahvasta. Muotopainike palauttaa sen keskelle.", "All Mask Overlays": "Kaikki maskien peittokuvat", "Before/After Side by Side": "Ennen/jälkeen vierekkäin", "Clipping": "Leikkautuminen", diff --git a/crates/ui-egui/locales/fr.json b/crates/ui-egui/locales/fr.json index 31e6814b6d..e946c67346 100644 --- a/crates/ui-egui/locales/fr.json +++ b/crates/ui-egui/locales/fr.json @@ -2353,6 +2353,34 @@ "CPU": "Processeur", "A face model uses the GPU only when a check shows it gives the same results faster.": "Un modèle de visages n’utilise le GPU que si une vérification montre qu’il donne les mêmes résultats plus vite.", "Checking the GPU for faces": "Vérification du GPU pour les visages", + "Blur Amount": "Quantité de flou", + "Boost": "Accentuation", + "In Focus": "Zone nette", + "Blur what is out of focus, as a wide aperture would": "Floute ce qui est hors de la mise au point, comme le ferait une grande ouverture", + "What stays sharp: the photo's subject, or the selection of one of its masks": "Ce qui reste net : le sujet de la photo ou la sélection de l'un de ses masques", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Astuce : sélectionnez précisément le sujet avec un masque Objet (Masquage, M), puis choisissez ce masque ici.", + "Brush Refinement": "Affinage au pinceau", + "Blur": "Flou", + "Paint what should stay sharp": "Peignez ce qui doit rester net", + "Paint what should be blurred": "Peignez ce qui doit être flou", + "Remove the strokes of both brushes": "Supprimer les traits des deux pinceaux", + "Find Subject": "Trouver le sujet", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Sélectionne le sujet par IA (SAM 3), à partir de l'endroit où l'appareil a fait la mise au point si le fichier l'indique, et le garde net", + "Selecting the subject at the camera's focus point…": "Sélection du sujet au point de mise au point de l'appareil…", + "Selecting the subject…": "Sélection du sujet…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "Affinez-le dans Masquage (M) : + → Objet ajoute ce qui manque, − → Objet exclut une partie.", + "Finding the subject… The first time on a photo takes a while.": "Recherche du sujet… La première fois sur une photo, cela prend un moment.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "Le sujet n'a pas été sélectionné. Réessayez Trouver le sujet, ou cliquez dessus dans Masquage (M).", + "Transition": "Transition", + "Selection": "Sélection", + "Outside": "Extérieur", + "Shape": "Forme", + "Round": "Ronde", + "Auto: the photo's subject, estimated": "Auto : le sujet estimé de la photo", + "Horizontal oval": "Ovale horizontal", + "Vertical oval": "Ovale vertical", + "Move & Resize": "Déplacer et redimensionner", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Faites glisser la forme sur la photo pour la déplacer ; faites glisser sa poignée latérale ou inférieure pour l'étirer. Un bouton de forme la remet au centre.", "Layout Image": "Image de mise en page", "Detail View": "Vue de détail", "Edit Panel": "Panneau Modifier", diff --git a/crates/ui-egui/locales/id.json b/crates/ui-egui/locales/id.json index 52705328cc..aac5874ad6 100644 --- a/crates/ui-egui/locales/id.json +++ b/crates/ui-egui/locales/id.json @@ -2353,6 +2353,34 @@ "≥ at least": "≥ minimal", "日本語": "日本語", "繁體中文(台灣)": "繁體中文(台灣)", + "Blur Amount": "Jumlah buram", + "Boost": "Penguatan", + "In Focus": "Dalam fokus", + "Blur what is out of focus, as a wide aperture would": "Memburamkan bagian di luar fokus, seperti bukaan lebar", + "What stays sharp: the photo's subject, or the selection of one of its masks": "Yang tetap tajam: subjek foto, atau seleksi salah satu maskernya", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Tips: pilih subjek dengan tepat memakai masker Objek (Masker, M), lalu pilih masker itu di sini.", + "Brush Refinement": "Penyempurnaan kuas", + "Blur": "Buram", + "Paint what should stay sharp": "Lukis bagian yang harus tetap tajam", + "Paint what should be blurred": "Lukis bagian yang harus diburamkan", + "Remove the strokes of both brushes": "Hapus goresan kedua kuas", + "Find Subject": "Cari subjek", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Pilih subjek dengan AI (SAM 3), mulai dari titik fokus kamera bila file mencatatnya, lalu jaga tetap fokus", + "Selecting the subject at the camera's focus point…": "Memilih subjek di titik fokus kamera…", + "Selecting the subject…": "Memilih subjek…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "Sempurnakan di Masker (M): + → Objek menambah yang terlewat, − → Objek mengecualikan bagian.", + "Finding the subject… The first time on a photo takes a while.": "Mencari subjek… Pertama kali pada sebuah foto butuh waktu.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "Subjek tidak terpilih. Coba Cari subjek lagi, atau klik subjeknya di Masker (M).", + "Transition": "Transisi", + "Selection": "Seleksi", + "Outside": "Luar", + "Shape": "Bentuk", + "Round": "Bulat", + "Auto: the photo's subject, estimated": "Otomatis: perkiraan subjek foto", + "Horizontal oval": "Oval horizontal", + "Vertical oval": "Oval vertikal", + "Move & Resize": "Pindah & ubah ukuran", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Seret bentuk di foto untuk memindahkannya; seret pegangan samping atau bawahnya untuk merenggangkannya. Tombol bentuk mengembalikannya ke tengah.", "All Mask Overlays": "Semua overlay masker", "Before/After Side by Side": "Sebelum/Sesudah berdampingan", "Clipping": "Clipping", diff --git a/crates/ui-egui/locales/it.json b/crates/ui-egui/locales/it.json index 5b0ccc0cee..743b94e890 100644 --- a/crates/ui-egui/locales/it.json +++ b/crates/ui-egui/locales/it.json @@ -2394,6 +2394,34 @@ "Show briefly when photo changes": "Mostra brevemente al cambio di foto", "Camera & Lens": "Fotocamera e obiettivo", "View → View Options chooses the lines Info 1 and Info 2 show.": "Visualizza → Opzioni di visualizzazione sceglie le righe di Info 1 e Info 2.", + "Blur Amount": "Quantità di sfocatura", + "Boost": "Enfasi", + "In Focus": "A fuoco", + "Blur what is out of focus, as a wide aperture would": "Sfoca ciò che è fuori fuoco, come farebbe un diaframma molto aperto", + "What stays sharp: the photo's subject, or the selection of one of its masks": "Cosa resta nitido: il soggetto della foto o la selezione di una delle sue maschere", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Suggerimento: seleziona con precisione il soggetto con una maschera Oggetto (Mascheratura, M), poi scegli qui quella maschera.", + "Brush Refinement": "Rifinitura a pennello", + "Blur": "Sfocatura", + "Paint what should stay sharp": "Dipingi ciò che deve restare nitido", + "Paint what should be blurred": "Dipingi ciò che deve essere sfocato", + "Remove the strokes of both brushes": "Rimuovi le pennellate di entrambi i pennelli", + "Find Subject": "Trova soggetto", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Seleziona il soggetto con l'IA (SAM 3), partendo dal punto di messa a fuoco della fotocamera se il file lo registra, e lo mantiene a fuoco", + "Selecting the subject at the camera's focus point…": "Selezione del soggetto nel punto di messa a fuoco della fotocamera…", + "Selecting the subject…": "Selezione del soggetto…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "Rifiniscilo in Mascheratura (M): + → Oggetto aggiunge ciò che manca, − → Oggetto esclude una parte.", + "Finding the subject… The first time on a photo takes a while.": "Ricerca del soggetto… La prima volta su una foto richiede un po' di tempo.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "Il soggetto non è stato selezionato. Riprova Trova soggetto, oppure fai clic su di esso in Mascheratura (M).", + "Transition": "Transizione", + "Selection": "Selezione", + "Outside": "Esterno", + "Shape": "Forma", + "Round": "Rotonda", + "Auto: the photo's subject, estimated": "Automatico: il soggetto stimato della foto", + "Horizontal oval": "Ovale orizzontale", + "Vertical oval": "Ovale verticale", + "Move & Resize": "Sposta e ridimensiona", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Trascina la forma sulla foto per spostarla; trascina la maniglia laterale o inferiore per allungarla. Un pulsante di forma la riporta al centro.", "Before/After Side by Side": "Prima/Dopo affiancati", "Clipping": "Tagli tonali", "Mask Overlay": "Sovrapposizione maschera", diff --git a/crates/ui-egui/locales/ja.json b/crates/ui-egui/locales/ja.json index 97c15dd019..6c83e8aaa3 100644 --- a/crates/ui-egui/locales/ja.json +++ b/crates/ui-egui/locales/ja.json @@ -2356,6 +2356,34 @@ "CPU": "CPU", "A face model uses the GPU only when a check shows it gives the same results faster.": "顔モデルが GPU を使うのは、同じ結果をより速く出せると確認できた場合だけです。", "Checking the GPU for faces": "顔用の GPU を確認中", + "Blur Amount": "ぼかしの量", + "Boost": "ブースト", + "In Focus": "ピントを合わせる対象", + "Blur what is out of focus, as a wide aperture would": "絞りを大きく開いたように、ピントの外側をぼかします", + "What stays sharp: the photo's subject, or the selection of one of its masks": "シャープに残す部分:写真の被写体、またはいずれかのマスクの選択範囲", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "ヒント:オブジェクトマスク(マスク、M)で被写体を正確に選択してから、ここでそのマスクを選びます。", + "Brush Refinement": "ブラシで調整", + "Blur": "ぼかし", + "Paint what should stay sharp": "シャープに残す部分をペイント", + "Paint what should be blurred": "ぼかす部分をペイント", + "Remove the strokes of both brushes": "両方のブラシのストロークを削除", + "Find Subject": "被写体を検出", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "AI(SAM 3)で被写体を選択します。ファイルにカメラのフォーカス位置があればそこから始め、被写体にピントを保ちます", + "Selecting the subject at the camera's focus point…": "カメラのフォーカス位置で被写体を選択しています…", + "Selecting the subject…": "被写体を選択しています…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "マスク(M)で調整:+ → オブジェクトで不足分を追加、− → オブジェクトで一部を除外します。", + "Finding the subject… The first time on a photo takes a while.": "被写体を探しています… 写真ごとに初回は時間がかかります。", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "被写体を選択できませんでした。もう一度「被写体を検出」を試すか、マスク(M)で被写体をクリックしてください。", + "Transition": "トランジション", + "Selection": "選択範囲", + "Outside": "外側", + "Shape": "形", + "Round": "円形", + "Auto: the photo's subject, estimated": "自動:推定した写真の被写体", + "Horizontal oval": "横長の楕円", + "Vertical oval": "縦長の楕円", + "Move & Resize": "移動とサイズ変更", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "写真上の形をドラッグして移動します。横または下のハンドルをドラッグして伸ばします。形のボタンで中央に戻ります。", "Layout Image": "レイアウト画像", "Detail View": "詳細表示", "Edit Panel": "編集パネル", diff --git a/crates/ui-egui/locales/ka.json b/crates/ui-egui/locales/ka.json index 30769b60fa..423ea1cc47 100644 --- a/crates/ui-egui/locales/ka.json +++ b/crates/ui-egui/locales/ka.json @@ -2751,6 +2751,34 @@ "⌘-drag to straighten, ⇧G Guided Upright, a grid while transforming.": "⌘-გადათრევა გასასწორებლად, ⇧G მართვადი Upright, ბადე გარდაქმნისას.", "日本語": "日本語", "繁體中文(台灣)": "繁體中文(台灣)", + "Blur Amount": "ბუნდოვნების რაოდენობა", + "Boost": "გაძლიერება", + "In Focus": "ფოკუსში", + "Blur what is out of focus, as a wide aperture would": "აბუნდოვნებს იმას, რაც ფოკუსის გარეთაა, როგორც ფართოდ გახსნილი დიაფრაგმა", + "What stays sharp: the photo's subject, or the selection of one of its masks": "რა რჩება მკვეთრი: ფოტოს საგანი ან მისი ერთ-ერთი ნიღბის მონიშვნა", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "რჩევა: საგანი ზუსტად მონიშნეთ ობიექტის ნიღბით (ნიღბვა, M), შემდეგ კი აქ აირჩიეთ ეს ნიღაბი.", + "Brush Refinement": "ფუნჯით დახვეწა", + "Blur": "ბუნდოვნება", + "Paint what should stay sharp": "მოხატეთ ის, რაც მკვეთრი უნდა დარჩეს", + "Paint what should be blurred": "მოხატეთ ის, რაც უნდა გაბუნდოვნდეს", + "Remove the strokes of both brushes": "ორივე ფუნჯის შტრიხების წაშლა", + "Find Subject": "საგნის პოვნა", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "საგნის მონიშვნა AI-ით (SAM 3) იქიდან, სადაც კამერამ ფოკუსირება მოახდინა, თუ ფაილი ამას ინახავს, და მისი ფოკუსში დატოვება", + "Selecting the subject at the camera's focus point…": "საგანი ინიშნება კამერის ფოკუსის წერტილში…", + "Selecting the subject…": "საგანი ინიშნება…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "დახვეწეთ ნიღბვაში (M): + → ობიექტი ამატებს გამორჩენილს, − → ობიექტი ნაწილს გამორიცხავს.", + "Finding the subject… The first time on a photo takes a while.": "საგანი იძებნება… ფოტოზე პირველად ამას გარკვეული დრო სჭირდება.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "საგანი ვერ მოინიშნა. სცადეთ „საგნის პოვნა“ ხელახლა, ან დააწკაპეთ მასზე ნიღბვაში (M).", + "Transition": "გადასვლა", + "Selection": "მონიშნული", + "Outside": "გარეთ", + "Shape": "ფორმა", + "Round": "მრგვალი", + "Auto: the photo's subject, estimated": "ავტომატური: ფოტოს სავარაუდო საგანი", + "Horizontal oval": "ჰორიზონტალური ოვალი", + "Vertical oval": "ვერტიკალური ოვალი", + "Move & Resize": "გადატანა და ზომის შეცვლა", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "გადაიტანეთ ფორმა ფოტოზე მის გადასაადგილებლად; გაწიეთ გვერდითი ან ქვედა სახელური მის გასაჭიმად. ფორმის ღილაკი მას შუაში დააბრუნებს.", "Focus Point": "ფოკუსის წერტილი", "Layout Image": "განლაგების სურათი", "Detail View": "დეტალური ხედი", diff --git a/crates/ui-egui/locales/pl.json b/crates/ui-egui/locales/pl.json index dbc29a1941..c4954eb3a8 100644 --- a/crates/ui-egui/locales/pl.json +++ b/crates/ui-egui/locales/pl.json @@ -2275,6 +2275,35 @@ "SDR Whites": "Biele SDR", "SDR Clarity": "Przejrzystość SDR", "Face Models on the GPU": "Modele twarzy przetwarzane na GPU", + "Blur Amount": "Siła rozmycia", + "Boost": "Wzmocnienie", + "In Focus": "Ostrość na", + "Blur what is out of focus, as a wide aperture would": "Rozmywa to, co jest poza ostrością, jak szeroko otwarta przysłona", + "What stays sharp: the photo's subject, or the selection of one of its masks": "Co pozostaje ostre: główny obiekt zdjęcia lub zaznaczenie jednej z jego masek", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Wskazówka: precyzyjnie zaznacz obiekt maską Obiekt (Maskowanie, M), a potem wybierz tę maskę tutaj.", + "Brush Refinement": "Korekta pędzlem", + "Focus": "Ostrość", + "Blur": "Rozmycie", + "Paint what should stay sharp": "Zamaluj to, co ma pozostać ostre", + "Paint what should be blurred": "Zamaluj to, co ma zostać rozmyte", + "Remove the strokes of both brushes": "Usuń pociągnięcia obu pędzli", + "Find Subject": "Znajdź obiekt", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Zaznacz główny obiekt za pomocą AI (SAM 3), zaczynając od punktu ostrości aparatu, jeśli plik go zapisuje, i zachowaj go ostrym", + "Selecting the subject at the camera's focus point…": "Zaznaczanie obiektu w punkcie ostrości aparatu…", + "Selecting the subject…": "Zaznaczanie obiektu…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "Popraw w Maskowaniu (M): + → Obiekt dodaje to, czego brakuje, − → Obiekt pomija część.", + "Finding the subject… The first time on a photo takes a while.": "Wyszukiwanie obiektu… Za pierwszym razem na zdjęciu trwa to chwilę.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "Nie zaznaczono obiektu. Spróbuj ponownie Znajdź obiekt lub kliknij go w Maskowaniu (M).", + "Transition": "Przejście", + "Selection": "Zaznaczenie", + "Outside": "Na zewnątrz", + "Shape": "Kształt", + "Round": "Okrągły", + "Auto: the photo's subject, estimated": "Automatycznie: szacowany główny obiekt zdjęcia", + "Horizontal oval": "Owal poziomy", + "Vertical oval": "Owal pionowy", + "Move & Resize": "Przesuń i zmień rozmiar", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Przeciągnij kształt na zdjęciu, aby go przesunąć; przeciągnij uchwyt boczny lub dolny, aby go rozciągnąć. Przycisk kształtu przywraca go na środek.", "Before/After Side by Side": "Przed/Po obok siebie", "Clipping": "Prześwietlenia i niedoświetlenia", "Mask Overlay": "Nakładka maski", diff --git a/crates/ui-egui/locales/pt-br-formats.json b/crates/ui-egui/locales/pt-br-formats.json index 4227fe328a..762eed671a 100644 --- a/crates/ui-egui/locales/pt-br-formats.json +++ b/crates/ui-egui/locales/pt-br-formats.json @@ -130,7 +130,7 @@ "{n} photo{} match · updates automatically as photos change": "{n} foto{0} correspondente{0} · atualiza automaticamente conforme as fotos mudam", "{n} photo{} now · updates automatically as photos change": "{n} foto{} agora · atualiza automaticamente conforme as fotos mudam", "{n} selected photo{} · {pages} page{}": "{n} foto{0} selecionada{0} · {pages} página{1}", - "{sel_n} selected · {counted}": "{sel_n} selecionadas · {counted}", + "{sel_n} selected · {counted}": "Selecionadas: {sel_n} · {counted}", "{stage}… {:.0}%": "{stage}… {:.0}%", "{verb} {n} Photo{}": "{verb} {n} foto{}", "{weekday}, {day}": "{weekday}, {day}", diff --git a/crates/ui-egui/locales/pt-br.json b/crates/ui-egui/locales/pt-br.json index 3aedd82906..c1e59c59a4 100644 --- a/crates/ui-egui/locales/pt-br.json +++ b/crates/ui-egui/locales/pt-br.json @@ -2202,6 +2202,34 @@ "Image on Black": "Imagem sobre preto", "Image on White": "Imagem sobre branco", "White on Black": "Branco sobre preto", + "Blur Amount": "Intensidade do desfoque", + "Boost": "Realce", + "In Focus": "Em foco", + "Blur what is out of focus, as a wide aperture would": "Desfoca o que está fora de foco, como faria uma abertura ampla", + "What stays sharp: the photo's subject, or the selection of one of its masks": "O que fica nítido: o assunto da foto ou a seleção de uma de suas máscaras", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Dica: selecione o assunto com precisão usando uma máscara Objeto (Máscara, M) e escolha essa máscara aqui.", + "Brush Refinement": "Refinamento com pincel", + "Blur": "Desfoque", + "Paint what should stay sharp": "Pinte o que deve ficar nítido", + "Paint what should be blurred": "Pinte o que deve ser desfocado", + "Remove the strokes of both brushes": "Remover os traços dos dois pincéis", + "Find Subject": "Encontrar assunto", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Seleciona o assunto com IA (SAM 3), começando onde a câmera focou quando o arquivo registra isso, e o mantém em foco", + "Selecting the subject at the camera's focus point…": "Selecionando o assunto no ponto de foco da câmera…", + "Selecting the subject…": "Selecionando o assunto…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "Refine em Máscara (M): + → Objeto acrescenta o que faltou, − → Objeto deixa uma parte de fora.", + "Finding the subject… The first time on a photo takes a while.": "Encontrando o assunto… Na primeira vez em uma foto, isso demora um pouco.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "O assunto não foi selecionado. Tente Encontrar assunto de novo ou clique nele em Máscara (M).", + "Transition": "Transição", + "Selection": "Seleção", + "Outside": "Fora", + "Shape": "Formato", + "Round": "Redondo", + "Auto: the photo's subject, estimated": "Automático: o assunto estimado da foto", + "Horizontal oval": "Oval horizontal", + "Vertical oval": "Oval vertical", + "Move & Resize": "Mover e redimensionar", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Arraste o formato na foto para movê-lo; arraste a alça lateral ou inferior para esticá-lo. Um botão de formato o devolve ao centro.", "Layout Image": "Imagem de layout", "Detail View": "Exibição de detalhe", "Edit Panel": "Painel Editar", diff --git a/crates/ui-egui/locales/ru.json b/crates/ui-egui/locales/ru.json index b3df02470e..258dae60bf 100644 --- a/crates/ui-egui/locales/ru.json +++ b/crates/ui-egui/locales/ru.json @@ -2564,6 +2564,34 @@ "CPU": "ЦП", "A face model uses the GPU only when a check shows it gives the same results faster.": "Модель лиц использует ГП, только если проверка показала, что он даёт те же результаты быстрее.", "Checking the GPU for faces": "Проверка ГП для лиц", + "Blur Amount": "Сила размытия", + "Boost": "Усиление", + "In Focus": "В фокусе", + "Blur what is out of focus, as a wide aperture would": "Размывает то, что вне фокуса, как широко открытая диафрагма", + "What stays sharp: the photo's subject, or the selection of one of its masks": "Что остаётся резким: главный объект фото или выделение одной из его масок", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Совет: точно выделите объект маской «Объект» (Маски, M), затем выберите эту маску здесь.", + "Brush Refinement": "Уточнение кистью", + "Blur": "Размытие", + "Paint what should stay sharp": "Закрасьте то, что должно остаться резким", + "Paint what should be blurred": "Закрасьте то, что нужно размыть", + "Remove the strokes of both brushes": "Удалить мазки обеих кистей", + "Find Subject": "Найти главный объект", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Выделить главный объект с помощью ИИ (SAM 3), начиная с точки фокусировки камеры, если файл её хранит, и оставить его в фокусе", + "Selecting the subject at the camera's focus point…": "Выделяется объект в точке фокусировки камеры…", + "Selecting the subject…": "Выделяется объект…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "Уточните в Масках (M): + → Объект добавляет пропущенное, − → Объект исключает часть.", + "Finding the subject… The first time on a photo takes a while.": "Поиск главного объекта… В первый раз на снимке это занимает время.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "Объект не выделен. Попробуйте «Найти главный объект» ещё раз или щёлкните по нему в Масках (M).", + "Transition": "Переход", + "Selection": "Выделение", + "Outside": "Снаружи", + "Shape": "Форма", + "Round": "Круг", + "Auto: the photo's subject, estimated": "Авто: предполагаемый главный объект снимка", + "Horizontal oval": "Горизонтальный овал", + "Vertical oval": "Вертикальный овал", + "Move & Resize": "Переместить и изменить размер", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Перетащите форму на снимке, чтобы переместить её; потяните за боковой или нижний маркер, чтобы растянуть. Кнопка формы вернёт её в центр.", "Layout Image": "Изображение макета", "Detail View": "Режим детализации", "Edit Panel": "Панель «Правка»", diff --git a/crates/ui-egui/locales/sv-formats.json b/crates/ui-egui/locales/sv-formats.json index 124fcb3a18..e205ea56a9 100644 --- a/crates/ui-egui/locales/sv-formats.json +++ b/crates/ui-egui/locales/sv-formats.json @@ -253,7 +253,7 @@ "{pct} %": "{pct} %", "{photos} catalog records processed; {collections} collections.": "{photos} katalogposter bearbetade; {collections} samlingar.", "{pid} (missing)": "{pid} (saknas)", - "{sel_n} selected · {counted}": "{sel_n} markerade · {counted}", + "{sel_n} selected · {counted}": "Markerade: {sel_n} · {counted}", "{size} GB": "{size} GB", "{stage}… {:.0}%": "{stage}… {:.0} %", "{s} s of AV1 video": "{s} s AV1-video", diff --git a/crates/ui-egui/locales/sv.json b/crates/ui-egui/locales/sv.json index 94b9e30333..8b454e0b2d 100644 --- a/crates/ui-egui/locales/sv.json +++ b/crates/ui-egui/locales/sv.json @@ -2437,6 +2437,35 @@ "★ Favorites": "★ Favoriter", "日本語": "日本語", "繁體中文(台灣)": "繁體中文(台灣)", + "Blur Amount": "Oskärpemängd", + "Boost": "Förstärkning", + "In Focus": "I fokus", + "Blur what is out of focus, as a wide aperture would": "Gör det som är utanför fokus oskarpt, som en stor bländaröppning", + "What stays sharp: the photo's subject, or the selection of one of its masks": "Det som förblir skarpt: fotots motiv eller markeringen i en av dess masker", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Tips: markera motivet exakt med en Objekt-mask (Maskering, M) och välj sedan den masken här.", + "Brush Refinement": "Penselförfining", + "Focus": "Fokus", + "Blur": "Oskärpa", + "Paint what should stay sharp": "Måla det som ska förbli skarpt", + "Paint what should be blurred": "Måla det som ska bli oskarpt", + "Remove the strokes of both brushes": "Ta bort penseldragen från båda penslarna", + "Find Subject": "Hitta motiv", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Markera motivet med AI (SAM 3) med start där kameran fokuserade, om filen anger det, och håll det i fokus", + "Selecting the subject at the camera's focus point…": "Markerar motivet vid kamerans fokuspunkt…", + "Selecting the subject…": "Markerar motivet…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "Förfina i Maskering (M): + → Objekt lägger till det som saknas, − → Objekt utesluter en del.", + "Finding the subject… The first time on a photo takes a while.": "Söker motivet… Första gången i ett foto tar det en stund.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "Motivet markerades inte. Försök med Hitta motiv igen, eller klicka på det i Maskering (M).", + "Transition": "Övergång", + "Selection": "Markering", + "Outside": "Utanför", + "Shape": "Form", + "Round": "Rund", + "Auto: the photo's subject, estimated": "Auto: fotots uppskattade motiv", + "Horizontal oval": "Vågrät oval", + "Vertical oval": "Lodrät oval", + "Move & Resize": "Flytta och ändra storlek", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Dra formen i fotot för att flytta den; dra i dess sido- eller nederhandtag för att sträcka den. En formknapp sätter tillbaka den i mitten.", "All Mask Overlays": "Alla masköverlägg", "Before/After Side by Side": "Före/efter sida vid sida", "Clipping": "Klippning", diff --git a/crates/ui-egui/locales/uk.json b/crates/ui-egui/locales/uk.json index 09b78fab1e..b4d3e50020 100644 --- a/crates/ui-egui/locales/uk.json +++ b/crates/ui-egui/locales/uk.json @@ -2354,6 +2354,34 @@ "CPU": "ЦП", "A face model uses the GPU only when a check shows it gives the same results faster.": "Модель облич використовує ГП, лише якщо перевірка показала, що він дає ті самі результати швидше.", "Checking the GPU for faces": "Перевірка ГП для облич", + "Blur Amount": "Сила розмиття", + "Boost": "Підсилення", + "In Focus": "У фокусі", + "Blur what is out of focus, as a wide aperture would": "Розмиває те, що поза фокусом, як широко відкрита діафрагма", + "What stays sharp: the photo's subject, or the selection of one of its masks": "Що лишається різким: головний об’єкт фото або виділення однієї з його масок", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Порада: точно виділіть об’єкт маскою «Об’єкт» (Маскування, M), а потім виберіть цю маску тут.", + "Brush Refinement": "Уточнення пензлем", + "Blur": "Розмиття", + "Paint what should stay sharp": "Зафарбуйте те, що має лишитися різким", + "Paint what should be blurred": "Зафарбуйте те, що треба розмити", + "Remove the strokes of both brushes": "Видалити мазки обох пензлів", + "Find Subject": "Знайти об’єкт", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Виділити головний об’єкт за допомогою ШІ (SAM 3), починаючи з точки фокусування камери, якщо файл її зберігає, і залишити його у фокусі", + "Selecting the subject at the camera's focus point…": "Виділяється об’єкт у точці фокусування камери…", + "Selecting the subject…": "Виділяється об’єкт…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "Уточніть у Маскуванні (M): + → Об’єкт додає пропущене, − → Об’єкт виключає частину.", + "Finding the subject… The first time on a photo takes a while.": "Пошук об’єкта… Уперше на знімку це триває певний час.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "Об’єкт не виділено. Спробуйте «Знайти об’єкт» ще раз або клацніть на ньому в Маскуванні (M).", + "Transition": "Перехід", + "Selection": "Виділення", + "Outside": "Зовні", + "Shape": "Форма", + "Round": "Коло", + "Auto: the photo's subject, estimated": "Авто: імовірний головний об’єкт фото", + "Horizontal oval": "Горизонтальний овал", + "Vertical oval": "Вертикальний овал", + "Move & Resize": "Перемістити й змінити розмір", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Перетягніть форму на фото, щоб перемістити її; потягніть боковий або нижній маркер, щоб розтягнути. Кнопка форми поверне її в центр.", "Layout Image": "Зображення макета", "Detail View": "Детальне подання", "Edit Panel": "Панель «Редагування»", diff --git a/crates/ui-egui/locales/vi.json b/crates/ui-egui/locales/vi.json index af27e9aa0a..5840e84b5f 100644 --- a/crates/ui-egui/locales/vi.json +++ b/crates/ui-egui/locales/vi.json @@ -2246,6 +2246,34 @@ "⌘-drag to straighten, ⇧G Guided Upright, a grid while transforming.": "⌘-kéo để chỉnh thẳng, ⇧G Chỉnh thẳng có hướng dẫn, lưới khi biến đổi.", "日本語": "日本語", "繁體中文(台灣)": "繁體中文(台灣)", + "Blur Amount": "Mức làm mờ", + "Boost": "Tăng cường", + "In Focus": "Vùng nét", + "Blur what is out of focus, as a wide aperture would": "Làm mờ phần nằm ngoài vùng lấy nét, như khi mở khẩu độ lớn", + "What stays sharp: the photo's subject, or the selection of one of its masks": "Phần giữ nét: chủ thể của ảnh hoặc vùng chọn của một trong các mặt nạ", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "Mẹo: chọn chính xác chủ thể bằng mặt nạ Đối tượng (Mặt nạ, M), rồi chọn mặt nạ đó tại đây.", + "Brush Refinement": "Tinh chỉnh bằng cọ", + "Blur": "Làm mờ", + "Paint what should stay sharp": "Tô vùng cần giữ nét", + "Paint what should be blurred": "Tô vùng cần làm mờ", + "Remove the strokes of both brushes": "Xóa nét vẽ của cả hai cọ", + "Find Subject": "Tìm chủ thể", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "Chọn chủ thể bằng AI (SAM 3), bắt đầu từ điểm lấy nét của máy ảnh nếu tệp có ghi, và giữ chủ thể nét", + "Selecting the subject at the camera's focus point…": "Đang chọn chủ thể tại điểm lấy nét của máy ảnh…", + "Selecting the subject…": "Đang chọn chủ thể…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "Tinh chỉnh trong Mặt nạ (M): + → Đối tượng thêm phần còn thiếu, − → Đối tượng bỏ một phần.", + "Finding the subject… The first time on a photo takes a while.": "Đang tìm chủ thể… Lần đầu trên một ảnh sẽ mất một lúc.", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "Chưa chọn được chủ thể. Thử Tìm chủ thể lần nữa, hoặc nhấp vào chủ thể trong Mặt nạ (M).", + "Transition": "Chuyển tiếp", + "Selection": "Vùng chọn", + "Outside": "Bên ngoài", + "Shape": "Hình dạng", + "Round": "Tròn", + "Auto: the photo's subject, estimated": "Tự động: chủ thể ước tính của ảnh", + "Horizontal oval": "Hình bầu dục ngang", + "Vertical oval": "Hình bầu dục dọc", + "Move & Resize": "Di chuyển & đổi kích thước", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "Kéo hình trên ảnh để di chuyển; kéo tay nắm bên hoặc dưới để kéo giãn. Nút hình dạng đưa nó về giữa.", "All Mask Overlays": "Mọi lớp phủ mặt nạ", "Before/After Side by Side": "Trước/Sau cạnh nhau", "Clipping": "Vùng cắt tông", diff --git a/crates/ui-egui/locales/zh-hans.json b/crates/ui-egui/locales/zh-hans.json index ca13d782a5..ec20786825 100644 --- a/crates/ui-egui/locales/zh-hans.json +++ b/crates/ui-egui/locales/zh-hans.json @@ -2194,6 +2194,34 @@ "This build cannot run recognition models: they can be added and chosen, not used.": "此版本无法运行识别模型:可以添加和选择,但无法使用。", "Detect Faces (Photo menu) needs it: a small one-time download.": "“检测人脸”(照片菜单)依赖它:一次性小文件下载。", "This build has nowhere to keep face models, so the model list is not available. (The desktop app has.)": "此版本无法存放人脸模型,因此模型列表不可用。(桌面版可以存放。)", + "Blur Amount": "模糊量", + "Boost": "增强", + "In Focus": "对焦范围", + "Blur what is out of focus, as a wide aperture would": "像大光圈一样模糊焦外区域", + "What stays sharp: the photo's subject, or the selection of one of its masks": "保持清晰的部分:照片的主体,或其某个蒙版的选区", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "提示:先用“对象”蒙版(蒙版,M)精确选择主体,再在此处选择该蒙版。", + "Brush Refinement": "画笔调整", + "Blur": "模糊", + "Paint what should stay sharp": "涂抹需要保持清晰的部分", + "Paint what should be blurred": "涂抹需要模糊的部分", + "Remove the strokes of both brushes": "移除两种画笔的笔触", + "Find Subject": "查找主体", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "使用 AI(SAM 3)选择主体:若文件记录了相机的对焦点,则从该处开始,并保持主体清晰", + "Selecting the subject at the camera's focus point…": "正在相机对焦点处选择主体…", + "Selecting the subject…": "正在选择主体…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "在蒙版(M)中细化:+ → 对象 添加遗漏部分,− → 对象 排除某部分。", + "Finding the subject… The first time on a photo takes a while.": "正在查找主体… 每张照片第一次需要一些时间。", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "未能选中主体。请再次尝试“查找主体”,或在蒙版(M)中单击主体。", + "Transition": "过渡", + "Selection": "选区", + "Outside": "外部", + "Shape": "形状", + "Round": "圆形", + "Auto: the photo's subject, estimated": "自动:估计的照片主体", + "Horizontal oval": "水平椭圆", + "Vertical oval": "垂直椭圆", + "Move & Resize": "移动和调整大小", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "在照片上拖动形状以移动它;拖动其侧边或底部手柄以拉伸。形状按钮会让它回到中间。", "Before/After Side by Side": "修改前/修改后左右排列", "Clipping": "溢出警告", "Mask Overlay": "蒙版叠加", diff --git a/crates/ui-egui/locales/zh-hant.json b/crates/ui-egui/locales/zh-hant.json index f3dfc422ec..f84f851102 100644 --- a/crates/ui-egui/locales/zh-hant.json +++ b/crates/ui-egui/locales/zh-hant.json @@ -2158,6 +2158,34 @@ "CPU": "CPU", "A face model uses the GPU only when a check shows it gives the same results faster.": "只有在檢查顯示 GPU 能更快得出相同結果時,臉孔模型才會使用 GPU。", "Checking the GPU for faces": "正在檢查用於臉孔的 GPU", + "Blur Amount": "模糊量", + "Boost": "增強", + "In Focus": "對焦範圍", + "Blur what is out of focus, as a wide aperture would": "像大光圈一樣模糊焦外區域", + "What stays sharp: the photo's subject, or the selection of one of its masks": "保持清晰的部分:相片的主體,或其某個遮色片的選取範圍", + "Tip: select the subject precisely with an Object mask (Masking, M), then choose that mask here.": "提示:先用「物件」遮色片(遮色片,M)精確選取主體,再於此處選擇該遮色片。", + "Brush Refinement": "筆刷調整", + "Blur": "模糊", + "Paint what should stay sharp": "塗抹需要保持清晰的部分", + "Paint what should be blurred": "塗抹需要模糊的部分", + "Remove the strokes of both brushes": "移除兩種筆刷的筆觸", + "Find Subject": "尋找主體", + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus": "使用 AI(SAM 3)選取主體:若檔案記錄了相機的對焦點,則從該處開始,並保持主體清晰", + "Selecting the subject at the camera's focus point…": "正在相機對焦點選取主體…", + "Selecting the subject…": "正在選取主體…", + "Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.": "在遮色片(M)中調整:+ → 物件 加入遺漏部分,− → 物件 排除某部分。", + "Finding the subject… The first time on a photo takes a while.": "正在尋找主體… 每張相片第一次需要一些時間。", + "The subject wasn't selected. Try Find Subject again, or click it in Masking (M).": "未能選取主體。請再次嘗試「尋找主體」,或在遮色片(M)中按一下主體。", + "Transition": "過渡", + "Selection": "選取範圍", + "Outside": "外部", + "Shape": "形狀", + "Round": "圓形", + "Auto: the photo's subject, estimated": "自動:估計的相片主體", + "Horizontal oval": "水平橢圓", + "Vertical oval": "垂直橢圓", + "Move & Resize": "移動和調整大小", + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.": "在相片上拖曳形狀以移動它;拖曳其側邊或底部控點以拉伸。形狀按鈕會讓它回到中間。", "Layout Image": "版面圖像", "Detail View": "詳細檢視", "Edit Panel": "編輯面板", diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 74425972d8..6d9faaafc9 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -108,6 +108,7 @@ impl LightcraftApp { compare_windows: [None; 2], compare_tiles: Default::default(), compare_hold_until: f64::NEG_INFINITY, + survey_motion: Default::default(), merge: merge::MergeState::default(), import: None, scan: None, diff --git a/crates/ui-egui/src/dialogs/lightroom_destination.rs b/crates/ui-egui/src/dialogs/lightroom_destination.rs index 5fd781cfdd..fb681c250e 100644 --- a/crates/ui-egui/src/dialogs/lightroom_destination.rs +++ b/crates/ui-egui/src/dialogs/lightroom_destination.rs @@ -99,7 +99,7 @@ pub fn default_location(app: &LightcraftApp) -> Option { } /// The new library's default name: the catalog's file name without `.lrcat` (`Travel.lrcat` → -/// `Travel`), or `Travel 2`, `Travel 3`… when `location` has something by that name already. +/// `Travel`; a backup's `Travel.zip` gives `Travel` too), or `Travel 2`, `Travel 3`… when `location` has something by that name already. pub fn default_name(catalog: &Path, location: Option<&Path>) -> String { let stem = catalog.file_stem().map(|s| s.to_string_lossy().trim().to_string()).filter(|s| !s.is_empty()); let stem = stem.unwrap_or_else(|| "Lightroom Catalog".to_string()); @@ -353,6 +353,7 @@ mod tests { fn the_new_library_is_named_after_the_catalog() { assert_eq!(default_name(Path::new("/x/Travel.lrcat"), None), "Travel"); assert_eq!(default_name(Path::new("/x/Lightroom Catalog-v13-3.lrcat"), None), "Lightroom Catalog-v13-3"); + assert_eq!(default_name(Path::new("/x/Backups/2026-10-22 1200/Travel.zip"), None), "Travel"); // nothing to name it after assert_eq!(default_name(Path::new(""), None), "Lightroom Catalog"); // a name taken in the location: the next free one diff --git a/crates/ui-egui/src/dialogs/settings/libraries.rs b/crates/ui-egui/src/dialogs/settings/libraries.rs index 5e615bfbcb..4e8d2a10d1 100644 --- a/crates/ui-egui/src/dialogs/settings/libraries.rs +++ b/crates/ui-egui/src/dialogs/settings/libraries.rs @@ -85,6 +85,9 @@ fn switch_library(app: &mut LightcraftApp, path: &str) -> Result app.ui.dialog = None; } app.renderer.forget_all(); + // photo ids are local to a catalog: a survey tile of the next library must not glide from a + // cell of this one, nor keep its render slot + app.survey_motion = Default::default(); if let Err(err) = opened { let Some(prev) = previous_dir else { return Err(err) }; return match app.session.open_library(&prev, false) { diff --git a/crates/ui-egui/src/dialogs/updates_ui.rs b/crates/ui-egui/src/dialogs/updates_ui.rs index 27f7d0c13b..ee42c37681 100644 --- a/crates/ui-egui/src/dialogs/updates_ui.rs +++ b/crates/ui-egui/src/dialogs/updates_ui.rs @@ -21,6 +21,7 @@ pub(crate) struct Runtime { #[cfg(not(target_arch = "wasm32"))] repaint: Option, /// A requested check's result dialog is showing. + #[cfg(target_arch = "wasm32")] pub(crate) manual_window_open: bool, #[cfg(target_arch = "wasm32")] pub(crate) status_message: Option, @@ -37,6 +38,7 @@ impl Default for Runtime { check_requested: false, #[cfg(not(target_arch = "wasm32"))] repaint: None, + #[cfg(target_arch = "wasm32")] manual_window_open: false, #[cfg(target_arch = "wasm32")] status_message: None, @@ -50,6 +52,13 @@ impl LightcraftApp { pub fn set_startup_update_check(&mut self, on: bool) { self.updates_rt.startup_check = on; } + + /// Starts the helper of a ready update, which installs it once the process has exited. Call it + /// in `on_exit` when the process ends there without dropping the app. + #[cfg(not(target_arch = "wasm32"))] + pub fn start_pending_update(&mut self) { + self.updates_rt.updater.start_pending_update(); + } } /// The update prompt owns the keyboard while it is open: shortcuts must not reach the library behind it. @@ -89,7 +98,6 @@ pub(crate) fn tick(app: &mut LightcraftApp, ctx: &egui::Context) { rt.repaint = Some(ctx.clone()); if std::mem::take(&mut rt.check_requested) { rt.updater.check_now(ctx); - rt.manual_window_open = true; } else if rt.startup_check { rt.updater.check_at_start(ctx); } @@ -99,10 +107,18 @@ pub(crate) fn tick(app: &mut LightcraftApp, ctx: &egui::Context) { let _ = (app, ctx); } +/// Whether the result dialog shows: for every status but idle (a requested check, and the first +/// start after an update, which reports that it installed or why it failed), and never over the +/// startup prompt. +#[cfg(not(target_arch = "wasm32"))] +fn result_window_shows(status: &Status, prompt_open: bool) -> bool { + !prompt_open && !matches!(status, Status::Idle) +} + pub(crate) fn show(app: &mut LightcraftApp, ctx: &egui::Context) { #[cfg(not(target_arch = "wasm32"))] { - // The restart helper is scheduled before the app closes, so no update starts over unsaved work. + // The close for a ready update waits until nothing is unsaved. let clean = app.session.unsaved().is_none(); match app.updates_rt.updater.show_if(ctx, clean) { Action::OpenUrl(url) => ctx.open_url(egui::OpenUrl::new_tab(url)), @@ -111,20 +127,16 @@ pub(crate) fn show(app: &mut LightcraftApp, ctx: &egui::Context) { } // A found update opens the startup prompt and leaves the status idle, which ends the result dialog. let rt = &mut app.updates_rt; - if rt.manual_window_open { + if result_window_shows(rt.updater.status(), rt.updater.prompt_open()) { let dialog = CheckResultDialog::new(&crate::UPDATE_APP, env!("CARGO_PKG_VERSION")); match dialog.show(ctx, rt.updater.status()) { CheckResultResponse::None => {} - CheckResultResponse::Close => rt.manual_window_open = false, + // Closing the dialog dismisses the outcome it showed. + CheckResultResponse::Close => rt.updater.dismiss_status(), CheckResultResponse::CheckAgain => rt.check_requested = true, + // The next `show_if` asks the window to close once nothing is unsaved. + CheckResultResponse::RestartToUpdate => rt.updater.restart_to_update(), } - if matches!(rt.updater.status(), Status::Idle) { - rt.manual_window_open = false; - } - } - // Closing the dialog dismisses the outcome it showed. - if !rt.manual_window_open { - rt.updater.dismiss_status(); } } @@ -163,6 +175,7 @@ mod tests { #[test] fn default_runtime_is_idle() { let rt = Runtime::default(); + #[cfg(target_arch = "wasm32")] assert!(!rt.manual_window_open); #[cfg(not(target_arch = "wasm32"))] { @@ -192,4 +205,24 @@ mod tests { output.textures_delta.clear(); assert!(app.updates_rt.updater.prompt_open()); } + + #[test] + #[cfg(not(target_arch = "wasm32"))] + fn result_window_shows_every_status_but_idle() { + let shown = [ + Status::Checking, + Status::UpToDate, + Status::Failed("no network".into()), + Status::Installing, + Status::Ready, + Status::Managed, + Status::Updated("0.2.0".into()), + Status::InstallFailed("disk full".into()), + ]; + for status in &shown { + assert!(result_window_shows(status, false), "{status:?}"); + assert!(!result_window_shows(status, true), "{status:?} over the prompt"); + } + assert!(!result_window_shows(&Status::Idle, false)); + } } diff --git a/crates/ui-egui/src/headless.rs b/crates/ui-egui/src/headless.rs index d9a90d5464..48a1161bfd 100644 --- a/crates/ui-egui/src/headless.rs +++ b/crates/ui-egui/src/headless.rs @@ -400,6 +400,7 @@ impl Headless { || self.app.import.is_some() || self.app.export.is_some() || self.app.session.denoise_busy() + || self.app.survey_motion.moving(self.time) || !self.app.tasks.is_empty() || !self.app.synthetic.is_empty() || !self.app.pending_gestures.is_empty() diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index e1b3410556..c40d16b8e9 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -320,6 +320,8 @@ pub struct LightcraftApp { pub(crate) compare_tiles: std::collections::HashMap<(u8, u64), region::RegionView>, /// Until when (s) a pinch or scroll in Compare / Reference holds the windows there are. pub(crate) compare_hold_until: f64, + /// Survey tiles gliding to new cells, and the render slot each surveyed photo keeps. + pub(crate) survey_motion: panels::compare::survey::SurveyMotion, /// Photo Merge dialog previews and background merges. pub merge: merge::MergeState, /// An import in progress (the import review dialog's batches). diff --git a/crates/ui-egui/src/lightroom_import.rs b/crates/ui-egui/src/lightroom_import.rs index 74db981078..895becd9b5 100644 --- a/crates/ui-egui/src/lightroom_import.rs +++ b/crates/ui-egui/src/lightroom_import.rs @@ -175,6 +175,7 @@ fn busy(app: &LightcraftApp) -> Result<(), String> { #[cfg(not(target_arch = "wasm32"))] fn spawn_inspect( path: PathBuf, + temp: Option, cancel: Arc, progress: LightroomProgress, tx: mpsc::Sender, @@ -184,7 +185,7 @@ fn spawn_inspect( .name("lc-lightroom-inspect".into()) .spawn(move || { let result = lightcraft_engine::guard::catch("Lightroom inspect", || { - lightcraft_engine::lightroom_catalog::read_with_progress(&path, &cancel, &progress.catalog_total, &progress.catalog_done) + lightcraft_engine::lightroom_catalog::read_source(&path, temp.as_deref(), &cancel, &progress.catalog_total, &progress.catalog_done) }) .and_then(|r| r.map(inspect_report)); let _ = tx.send(Message::Inspected(result)); @@ -385,7 +386,8 @@ fn start_inspect(app: &mut LightcraftApp, path: PathBuf, ctx: &egui::Context) -> let cancel = Arc::new(AtomicBool::new(false)); let progress = LightroomProgress::default(); let (tx, rx) = mpsc::channel(); - spawn_inspect(path.clone(), cancel.clone(), progress.clone(), tx, ctx.clone())?; + let temp = lightcraft_engine::lightroom_catalog::backup_temp_root(&app.session); + spawn_inspect(path.clone(), temp, cancel.clone(), progress.clone(), tx, ctx.clone())?; app.lightroom_last = None; app.lightroom = Some(LightroomTask::new(&app.session, Kind::Inspect, path.clone(), cancel, progress, rx)); Ok(json!({"running": true, "kind": "inspect", "path": path})) @@ -788,6 +790,8 @@ mod tests { fn inspect_report_keeps_empty_catalog_shape() { let report = inspect_report(CatalogImport { source: "synthetic.lrcat".into(), + catalog_id: None, + catalog_path: None, photos: Vec::new(), collections: Vec::new(), members: Vec::new(), diff --git a/crates/ui-egui/src/menus/run.rs b/crates/ui-egui/src/menus/run.rs index 9791f0cac3..23863c4a0b 100644 --- a/crates/ui-egui/src/menus/run.rs +++ b/crates/ui-egui/src/menus/run.rs @@ -1353,8 +1353,11 @@ fn ui_commands_10(app: &mut LightcraftApp, ctx: &egui::Context, id: &str, p: &Va let path = match p.get("path").and_then(Value::as_str).map(str::to_string) { Some(path) => Some(path), None => { - let req = - PickRequest::file(crate::i18n::tr("Import Lightroom Catalog"), crate::i18n::tr("Lightroom Classic Catalog"), &["lrcat"]); + let req = PickRequest::file( + crate::i18n::tr("Import Lightroom Catalog"), + crate::i18n::tr("Lightroom Classic Catalog"), + &["lrcat", "zip"], + ); match crate::pick::ask(app, id, p, "path", req, |s| s.pick_lightroom_catalog.as_mut().and_then(|f| f()).map(|x| vec![x])) { Picked::Now(v) => v.into_iter().next(), Picked::Later | Picked::Unavailable => None, diff --git a/crates/ui-egui/src/menus/ui_commands.rs b/crates/ui-egui/src/menus/ui_commands.rs index f4398d2842..e9deed4601 100644 --- a/crates/ui-egui/src/menus/ui_commands.rs +++ b/crates/ui-egui/src/menus/ui_commands.rs @@ -162,6 +162,7 @@ pub const UI_COMMANDS: &[UiCommand] = &[ ("section.effects", "Effects", Some("Cmd+3"), "Window>Edit Sections"), ("section.detail", "Detail", Some("Cmd+4"), "Window>Edit Sections"), ("section.optics", "Optics", Some("Cmd+5"), "Window>Edit Sections"), + ("section.lensBlur", "Lens Blur", Some("Cmd+6"), "Window>Edit Sections"), ("section.calibration", "Calibration", None, ""), // {on?}; no params toggles. Opening one Edit section closes the others. ("view.soloSections", "Solo Mode", None, "Window>Edit Sections"), diff --git a/crates/ui-egui/src/panels/compare.rs b/crates/ui-egui/src/panels/compare.rs index c6475a7017..4fc9e49554 100644 --- a/crates/ui-egui/src/panels/compare.rs +++ b/crates/ui-egui/src/panels/compare.rs @@ -13,6 +13,8 @@ use crate::state::{ViewMode, Zoom}; use crate::theme::Tokens; use crate::widgets::register; +pub mod survey; + /// Most photos a survey shows. pub const SURVEY_MAX: usize = 48; @@ -203,11 +205,27 @@ fn request_tile_renders(app: &mut LightcraftApp, id: PhotoId, slot: Slot, want: if let Some(job) = app.session.render_job(id, rw.max(8), rh.max(8), false, true) { app.renderer.request(slot, job, 60); } + request_thumb(app, id); +} + +/// Ask for the thumbnail a tile shows until its render is there. +fn request_thumb(app: &mut LightcraftApp, id: PhotoId) { if let Some(job) = app.session.thumb_job(id, 256) { app.renderer.request(Slot::Thumb(id), job, 30); } } +/// The photo's frame as shown: cropped, rotated and lens-corrected. +fn frame_of(photo: &lightcraft_catalog::Photo) -> lightcraft_pipeline::geometry::Frame { + lightcraft_pipeline::geometry::Frame::with_lens( + photo.width.max(1) as usize, + photo.height.max(1) as usize, + &photo.develop, + true, + photo.embedded_lens.as_ref(), + ) +} + /// Draw one photo fitted into `area` (rendered at its display size into `slot`), with its caption /// strip below. Returns the image rect and the click/drag response. fn photo_tile( @@ -226,13 +244,7 @@ fn photo_tile( let Some(photo) = app.session.catalog.photo(id).cloned() else { return (area, area, resp) }; let caption_h = 26.0; let img_area = Rect::from_min_max(area.min, pos2(area.right(), area.bottom() - caption_h)); - let frame = lightcraft_pipeline::geometry::Frame::with_lens( - photo.width.max(1) as usize, - photo.height.max(1) as usize, - &photo.develop, - true, - photo.embedded_lens.as_ref(), - ); + let frame = frame_of(&photo); let aspect = frame.aspect() as f32; let native = super::detail::output_px(&frame); let mut img = super::detail::fit_rect(img_area, aspect, zoom, native, ppp, app.ui.pan); @@ -249,7 +261,13 @@ fn photo_tile( let texture_side = super::detail::texture_side(ui.ctx()); let want = (img.width().max(img.height()).min(img_area.width().max(img_area.height()) * 4.0) * ppp).min(2560.0_f32.min(texture_side as f32)) as usize; - request_tile_renders(app, id, slot, want, aspect); + // a gliding survey tile asks for its render once it has landed, not one per size it passes on + // the way, and for its thumbnail (one size) meanwhile; tiles that stay put render as usual + if app.ui.view == ViewMode::Survey && app.survey_motion.gliding(id, ui.input(|i| i.time)) { + request_thumb(app, id); + } else { + request_tile_renders(app, id, slot, want, aspect); + } // zoomed past what that render holds (1:1 to check focus): the part on screen as a window, as // in the loupe. Compare and Reference only; Survey tiles are fit views. let pane = match slot { @@ -546,21 +564,6 @@ pub fn show_compare(app: &mut LightcraftApp, ui: &mut egui::Ui) { ui.painter().line_segment([pos2(mid, area.top()), pos2(mid, area.bottom())], Stroke::new(1.0, Tokens::get(ui.ctx()).divider)); } -/// Columns for `n` tiles of aspect ~3:2 in `area`, maximizing the tile size. -pub fn survey_columns(n: usize, area: Rect) -> usize { - let mut best = (1, 0.0f32); - for cols in 1..=n.max(1) { - let rows = n.div_ceil(cols); - let w = area.width() / cols as f32; - let h = area.height() / rows as f32; - let s = w.min(h * 1.5); - if s > best.1 { - best = (cols, s); - } - } - best.0 -} - pub fn show_survey(app: &mut LightcraftApp, ui: &mut egui::Ui) { let t = Tokens::get(ui.ctx()); let canvas = area_and_filmstrip(app, ui); @@ -570,14 +573,20 @@ pub fn show_survey(app: &mut LightcraftApp, ui: &mut egui::Ui) { return; } let area = canvas.shrink(14.0); - let cols = survey_columns(photos.len(), area); - let rows = photos.len().div_ceil(cols); - let (cw, ch) = (area.width() / cols as f32, area.height() / rows as f32); - for (i, id) in photos.iter().enumerate() { - let (c, r) = (i % cols, i / cols); - let cell = Rect::from_min_size(pos2(area.left() + c as f32 * cw, area.top() + r as f32 * ch), vec2(cw, ch)).shrink(6.0); + let aspects: Vec = photos.iter().map(|id| app.session.catalog.photo(*id).map_or(1.5, |p| frame_of(p).aspect() as f32)).collect(); + let cells = survey::cells(&aspects, area); + let now = ui.input(|i| i.time); + let placed = app.survey_motion.place(&photos, &cells, area, ui.ctx().cumulative_frame_nr(), now); + let (slots, freed) = app.survey_motion.assign_slots(&photos); + for s in freed { + app.renderer.release(Slot::Compare(s)); + } + if app.survey_motion.moving(now) { + ui.ctx().request_repaint(); + } + for ((id, cell), slot) in photos.iter().zip(placed).zip(slots) { // (Survey tiles are fit views with no windows: what counts is the photo as painted) - let (_, img, resp) = photo_tile(app, ui, *id, Slot::Compare(i as u8), cell, "", Zoom::Fit); + let (_, img, resp) = photo_tile(app, ui, *id, Slot::Compare(slot), cell.shrink(survey::GAP), "", Zoom::Fit); if app.session.selection.active == Some(*id) { app.image_rect = Some(img); } diff --git a/crates/ui-egui/src/panels/compare/survey.rs b/crates/ui-egui/src/panels/compare/survey.rs new file mode 100644 index 0000000000..c4cb3eb9cf --- /dev/null +++ b/crates/ui-egui/src/panels/compare/survey.rs @@ -0,0 +1,261 @@ +//! Survey layout: the cells the photos are tiled in, fitted to their shapes, and the glide that +//! moves the tiles left to their new cells when a photo leaves or joins the survey, so it is +//! plain which photo went where. + +use std::collections::HashMap; + +use egui::{Rect, pos2, vec2}; +use lightcraft_catalog::PhotoId; + +/// Space a tile keeps below its photo for the caption (as `photo_tile` draws it). +pub const CAPTION_H: f32 = 26.0; +/// Margin around each tile inside its cell. +pub const GAP: f32 = 6.0; +/// Seconds a tile takes to glide to its new cell. +pub const GLIDE_SECS: f64 = 0.2; + +/// A width / height ratio that layout arithmetic can use: the photo's own, as it is drawn (a +/// 100:1 panorama counts as one), or a square for a size that is none. +fn usable_aspect(a: f32) -> f32 { + if a.is_finite() && a > 0.0 { a } else { 1.0 } +} + +/// Image area photos with `aspects` show in a grid of `cols` equal cells in `area`. +fn shown_area(aspects: &[f32], cols: usize, area: Rect) -> f32 { + let rows = aspects.len().div_ceil(cols.max(1)); + let iw = area.width() / cols.max(1) as f32 - 2.0 * GAP; + let ih = area.height() / rows.max(1) as f32 - 2.0 * GAP - CAPTION_H; + if iw <= 0.0 || ih <= 0.0 { + return 0.0; + } + aspects + .iter() + .map(|&a| { + let a = usable_aspect(a); + let w = iw.min(ih * a); + w * w / a + }) + .sum() +} + +/// The cell of each photo (width / height `aspects`, in survey order) in `area`: equal cells in +/// rows, with the column count that shows the most of the photos, so the survey fits their shapes +/// to the screen as Lightroom Classic's does (four portrait photos side by side on a wide screen, +/// not two by two). An incomplete last row is centred. +pub fn cells(aspects: &[f32], area: Rect) -> Vec { + let n = aspects.len(); + if n == 0 { + return Vec::new(); + } + let mut best = (1, f32::NEG_INFINITY); + for cols in 1..=n { + let shown = shown_area(aspects, cols, area); + if shown > best.1 { + best = (cols, shown); + } + } + let cols = best.0; + let rows = n.div_ceil(cols); + let (cw, ch) = (area.width() / cols as f32, area.height() / rows as f32); + let last_row = n - (rows - 1) * cols; + (0..n) + .map(|i| { + let (c, r) = (i % cols, i / cols); + let indent = if r == rows - 1 { (cols - last_row) as f32 * cw / 2.0 } else { 0.0 }; + Rect::from_min_size(pos2(area.left() + indent + c as f32 * cw, area.top() + r as f32 * ch), vec2(cw, ch)) + }) + .collect() +} + +/// One tile's way from where it was drawn to its cell. +#[derive(Clone, Copy, Debug)] +struct Glide { + from: Rect, + to: Rect, + start: f64, +} + +impl Glide { + fn at(&self, now: f64) -> Rect { + let t = ((now - self.start) / GLIDE_SECS).clamp(0.0, 1.0) as f32; + // ease out: quick to leave, gentle to land + let e = 1.0 - (1.0 - t).powi(3); + self.from.lerp_towards(&self.to, e) + } + + fn on_its_way(&self, now: f64) -> bool { + !self.done(now) && self.from != self.to + } + + fn done(&self, now: f64) -> bool { + // (a hair early: the clock's rounding must not keep a landed tile moving) + now - self.start >= GLIDE_SECS - 1e-9 + } +} + +/// Where the survey's tiles are drawn while they glide, and which render slot each photo keeps +/// (a photo keeps its slot while it is shown, so the tiles that move keep their renders). +#[derive(Default)] +pub struct SurveyMotion { + /// The survey area of the last frame drawn, and its frame number. + last: Option<(Rect, u64)>, + tiles: HashMap, + slots: HashMap, +} + +impl SurveyMotion { + /// Where to draw `photos` (in survey order) at `now`, given their `cells` in `area` this frame + /// (`frame`, egui's cumulative frame number). A tile whose cell changed since the last frame glides + /// there from where it was drawn; a photo that joined appears in its cell. A survey just opened + /// or a canvas that changed size is laid out at once. + pub fn place(&mut self, photos: &[PhotoId], cells: &[Rect], area: Rect, frame: u64, now: f64) -> Vec { + let continued = self.last.is_some_and(|(a, p)| a == area && frame <= p.saturating_add(1)); + self.last = Some((area, frame)); + let mut next = HashMap::with_capacity(photos.len()); + let placed = photos + .iter() + .zip(cells) + .map(|(id, &to)| { + let glide = match self.tiles.get(id) { + Some(g) if continued && g.to == to => *g, + Some(g) if continued => Glide { from: g.at(now), to, start: now }, + _ => Glide { from: to, to, start: now }, + }; + next.insert(*id, glide); + glide.at(now) + }) + .collect(); + self.tiles = next; + placed + } + + /// Whether a tile is still on its way. + pub fn moving(&self, now: f64) -> bool { + self.tiles.values().any(|g| g.on_its_way(now)) + } + + /// Whether the tile of `id` is still on its way (a tile that stays put, or a photo that just + /// joined, is not, while others glide). + pub fn gliding(&self, id: PhotoId, now: f64) -> bool { + self.tiles.get(&id).is_some_and(|g| g.on_its_way(now)) + } + + /// The render slot of each of `photos`: the one it had, else the lowest free one. Returns the + /// slots that photos no longer shown gave up, whose renders can be freed. + pub fn assign_slots(&mut self, photos: &[PhotoId]) -> (Vec, Vec) { + let freed: Vec = self.slots.iter().filter(|(id, _)| !photos.contains(id)).map(|(_, s)| *s).collect(); + self.slots.retain(|id, _| photos.contains(id)); + let used: Vec = self.slots.values().copied().collect(); + let mut free = (0..=u8::MAX).filter(move |s| !used.contains(s)); + let slots = photos.iter().map(|id| *self.slots.entry(*id).or_insert_with(|| free.next().unwrap_or(u8::MAX))).collect(); + (slots, freed) + } + + /// The render slot `id` keeps, if it has one. + #[cfg(test)] + pub fn slot(&self, id: PhotoId) -> Option { + self.slots.get(&id).copied() + } + + /// Whether `id` has a tile here (its place or its render slot). + #[cfg(test)] + pub fn tracks(&self, id: PhotoId) -> bool { + self.tiles.contains_key(&id) || self.slots.contains_key(&id) + } +} + +#[cfg(test)] +mod tests { + use super::{CAPTION_H, GAP, GLIDE_SECS, SurveyMotion, cells}; + use egui::{Rect, pos2, vec2}; + use lightcraft_catalog::PhotoId; + + const WIDE: Rect = Rect { min: pos2(0.0, 0.0), max: pos2(1600.0, 800.0) }; + + fn cols(cells: &[Rect]) -> usize { + cells.iter().filter(|c| c.top() == cells[0].top()).count() + } + + #[test] + fn four_portraits_stand_side_by_side_on_a_wide_screen() { + let c = cells(&[2.0 / 3.0; 4], WIDE); + assert_eq!(cols(&c), 4, "{c:?}"); + } + + #[test] + fn four_landscapes_on_a_wide_screen_take_two_rows() { + let c = cells(&[1.5; 4], WIDE); + assert_eq!(cols(&c), 2, "{c:?}"); + } + + /// Column choice follows the ratio drawn, also past 20:1: four 100:1 panoramas and four + /// portraits show most side by side in four columns, not one tall column. + #[test] + fn panoramas_count_at_their_own_ratio() { + let area = Rect::from_min_size(pos2(0.0, 0.0), vec2(2560.0, 1440.0)); + let aspects = [100.0, 100.0, 100.0, 100.0, 2.0 / 3.0, 2.0 / 3.0, 2.0 / 3.0, 2.0 / 3.0]; + assert_eq!(cols(&cells(&aspects, area)), 4); + } + + #[test] + fn the_last_row_is_centred() { + let c = cells(&[1.5; 5], WIDE); + assert_eq!(cols(&c), 3, "{c:?}"); + let (row, last) = (&c[..3], &c[3..]); + assert_eq!(last.len(), 2); + let mid = |r: &[Rect]| (r[0].left() + r[r.len() - 1].right()) / 2.0; + assert!((mid(row) - mid(last)).abs() < 0.01, "{c:?}"); + } + + #[test] + fn cells_stay_inside_the_area_and_survive_hostile_sizes() { + for aspects in [vec![f32::NAN, 0.0, -1.0, f32::INFINITY, 1e9, f32::MAX, f32::MIN_POSITIVE], vec![1.5; 48]] { + let c = cells(&aspects, WIDE); + assert_eq!(c.len(), aspects.len()); + assert!(c.iter().all(|r| WIDE.expand(0.01).contains_rect(*r)), "{c:?}"); + } + assert!(cells(&[], WIDE).is_empty()); + // a canvas too small for any tile still lays them out, one column + let tiny = Rect::from_min_size(pos2(0.0, 0.0), vec2(2.0 * GAP, CAPTION_H)); + assert_eq!(cells(&[1.0; 3], tiny).len(), 3); + } + + #[test] + fn a_tile_glides_to_its_new_cell_and_lands_there() { + let (a, b) = (PhotoId(1), PhotoId(2)); + let mut m = SurveyMotion::default(); + let first = cells(&[1.5; 2], WIDE); + assert_eq!(m.place(&[a, b], &first, WIDE, 1, 0.0), first, "a new survey is laid out at once"); + assert!(!m.moving(0.0)); + // a leaves: b moves to the single cell + let one = cells(&[1.5], WIDE); + let mid = m.place(&[b], &one, WIDE, 2, 1.0); + assert_eq!(mid[0], first[1], "it starts where it was drawn"); + assert!(m.moving(1.0)); + let half = m.place(&[b], &one, WIDE, 3, 1.0 + GLIDE_SECS / 2.0)[0]; + assert!(half != first[1] && half != one[0], "on its way: {half:?}"); + assert_eq!(m.place(&[b], &one, WIDE, 4, 1.0 + GLIDE_SECS)[0], one[0], "it lands in its cell"); + assert!(!m.moving(1.0 + GLIDE_SECS)); + } + + #[test] + fn a_reopened_survey_or_a_resized_canvas_does_not_glide() { + let (a, b) = (PhotoId(1), PhotoId(2)); + let mut m = SurveyMotion::default(); + m.place(&[a, b], &cells(&[1.5; 2], WIDE), WIDE, 1, 0.0); + let one = cells(&[1.5], WIDE); + assert_eq!(m.place(&[b], &one, WIDE, 9, 1.0), one, "frames skipped: the survey was not shown"); + let narrow = Rect::from_min_size(pos2(0.0, 0.0), vec2(900.0, 800.0)); + let c = cells(&[1.5], narrow); + assert_eq!(m.place(&[b], &c, narrow, 10, 2.0), c, "a new canvas size"); + } + + #[test] + fn photos_keep_their_slots_and_new_ones_take_freed_slots() { + let (a, b, c, d) = (PhotoId(1), PhotoId(2), PhotoId(3), PhotoId(4)); + let mut m = SurveyMotion::default(); + assert_eq!(m.assign_slots(&[a, b, c]), (vec![0, 1, 2], vec![])); + assert_eq!(m.assign_slots(&[a, c]), (vec![0, 2], vec![1]), "c keeps its render"); + assert_eq!(m.assign_slots(&[a, c, d]), (vec![0, 2, 1], vec![]), "d takes the freed slot"); + } +} diff --git a/crates/ui-egui/src/panels/detail.rs b/crates/ui-egui/src/panels/detail.rs index 71b0ef8b60..2a6a83fd8e 100644 --- a/crates/ui-egui/src/panels/detail.rs +++ b/crates/ui-egui/src/panels/detail.rs @@ -18,6 +18,7 @@ mod crop; mod filmstrip; mod geometry; mod info; +mod lens_blur; mod look_ahead; mod masks; mod retouch; @@ -48,6 +49,7 @@ use info::{ preview_only_pill, quick_name, }; pub(crate) use info::{draw_focus_point, failed_pill, loupe_pill, rendering_overdue}; +use lens_blur::{lens_blur_brush_overlay, lens_blur_shape_overlay}; use look_ahead::BEHIND; pub use look_ahead::LookAhead; pub(crate) use look_ahead::{Prepare, look_ahead}; @@ -59,6 +61,12 @@ use window::{WindowCtx, WindowView, draw_window, request_window}; /// An in-progress on-canvas gesture. #[derive(Clone, Debug)] pub enum Gesture { + /// Lens Blur's fixed focus shape: moving it (`handle` 0) or stretching it sideways (1) or + /// up and down (2), from `original` (where it was when the drag began). + LensBlurArea { + handle: u8, + original: lightcraft_develop::FocusArea, + }, Brush { points: Vec, /// Pen pressure (0..1) at each point; empty when no pen pressure was reported (a mouse) or the @@ -758,6 +766,8 @@ fn tool_interaction(app: &mut LightcraftApp, ui: &mut egui::Ui, resp: &egui::Res RightPanel::Masking => mask_overlay(app, ui, resp, map, d), RightPanel::Remove => remove_overlay(app, ui, resp, map, d), RightPanel::RedEye => eye_overlay(app, ui, resp, map, d), + RightPanel::Edit if app.ui.tool == "lensBlurShape" => lens_blur_shape_overlay(app, ui, resp, map, d), + RightPanel::Edit if app.ui.tool.starts_with("lensBlur") => lens_blur_brush_overlay(app, ui, resp, map), _ => { // side by side / stacked: Before shows the same frame, so a click in its view (white // balance, colour samples, targeted adjustment) means the same point of the photo. diff --git a/crates/ui-egui/src/panels/detail/lens_blur.rs b/crates/ui-egui/src/panels/detail/lens_blur.rs new file mode 100644 index 0000000000..f3c5404ece --- /dev/null +++ b/crates/ui-egui/src/panels/detail/lens_blur.rs @@ -0,0 +1,148 @@ +//! Lens Blur's tools on the photo: moving and resizing its fixed focus shape, and painting its +//! Focus / Blur refinement brushes. + +use super::geometry::CanvasMap; +use super::masks::{frame_long_norm, pen_force, pin, record_pressure}; +use super::{Color32, DevelopSettings, Gesture, LightcraftApp, Point, Pos2, Rect, Stroke, json, register, vec2}; + +/// Lens Blur's fixed focus shape on the photo (Edit Shape): its outline, a centre pin that moves +/// it (or drag anywhere inside), and handles on its right and bottom edges that stretch it +/// sideways or up and down. One drag is one undo step. +pub(super) fn lens_blur_shape_overlay(app: &mut LightcraftApp, ui: &mut egui::Ui, resp: &egui::Response, map: &CanvasMap, d: &DevelopSettings) { + let Some(area) = d.lens_blur.focus_area(d.crop_center()) else { return }; + let clip = app.canvas_rect.unwrap_or(map.rect); + let p = &ui.painter_at(clip); + let l = frame_long_norm(map); + let at = |dx: f64, dy: f64| map.screen(Point::new(area.center.x + dx * l.0, area.center.y + dy * l.1)); + let outline: Vec = (0..72) + .map(|i| { + let a = i as f64 / 72.0 * std::f64::consts::TAU; + at(area.rx * a.cos(), area.ry * a.sin()) + }) + .collect(); + // a dark keyline under a light line: visible over bright and dark photos alike + p.add(egui::Shape::closed_line(outline.clone(), Stroke::new(3.0, Color32::from_black_alpha(140)))); + p.add(egui::Shape::closed_line(outline, Stroke::new(1.5, Color32::from_white_alpha(235)))); + let (c, hx, hy) = (at(0.0, 0.0), at(area.rx, 0.0), at(0.0, area.ry)); + pin(p, c, true); + for h in [hx, hy] { + p.circle_filled(h, 5.5, Color32::WHITE); + p.circle_stroke(h, 5.5, Stroke::new(1.5, Color32::from_black_alpha(160))); + } + for (name, q) in [("center", c), ("width", hx), ("height", hy)] { + register(ui, format!("lensBlurArea:{name}"), Rect::from_center_size(q, vec2(14.0, 14.0))); + } + // which part a point grabs: a handle, else the inside + let hit = |q: Pos2| -> Option { + if q.distance(hx) < 10.0 { + return Some(1); + } + if q.distance(hy) < 10.0 { + return Some(2); + } + let n = map.norm(q); + let (ex, ey) = ((n.x - area.center.x) / (area.rx * l.0).max(1e-9), (n.y - area.center.y) / (area.ry * l.1).max(1e-9)); + (q.distance(c) < 10.0 || ex * ex + ey * ey <= 1.0).then_some(0) + }; + if let Some(q) = resp.hover_pos() { + let icon = match hit(q) { + Some(1) => Some(egui::CursorIcon::ResizeHorizontal), + Some(2) => Some(egui::CursorIcon::ResizeVertical), + Some(_) => Some(egui::CursorIcon::Move), + None => None, + }; + if let Some(icon) = icon { + ui.ctx().set_cursor_icon(icon); + } + } + if resp.drag_started() + && let Some(q) = ui.input(|i| i.pointer.press_origin()).or(resp.interact_pointer_pos()) + && let Some(handle) = hit(q) + { + let _ = app.run("develop.beginInteraction", json!({"label": "Lens Blur Shape"})); + app.gesture = Some(Gesture::LensBlurArea { handle, original: area }); + } + if resp.dragged() + && let (Some(Gesture::LensBlurArea { handle, original }), Some(q)) = (app.gesture.clone(), resp.interact_pointer_pos()) + { + let n = map.norm(q); + let n0 = map.norm(ui.input(|i| i.pointer.press_origin()).unwrap_or(q)); + let moved = match handle { + 0 => lightcraft_develop::FocusArea { center: Point::new(original.center.x + n.x - n0.x, original.center.y + n.y - n0.y), ..original }, + 1 => lightcraft_develop::FocusArea { rx: (n.x - original.center.x).abs() / l.0.max(1e-9), ..original }, + _ => lightcraft_develop::FocusArea { ry: (n.y - original.center.y).abs() / l.1.max(1e-9), ..original }, + } + .sanitized(); + let _ = app.run("develop.merge", json!({"settings": {"lens_blur": {"area": moved}}, "label": "Lens Blur Shape"})); + } + if resp.drag_stopped() && matches!(app.gesture, Some(Gesture::LensBlurArea { .. })) { + app.gesture = None; + let _ = app.run("develop.endInteraction", json!({})); + } +} + +/// Lens Blur Brush Refinement on the photo: the brush circle (size and feather), the stroke being +/// painted (green: Focus, red: Blur), committed as `lensBlur.brushStroke` when the drag ends. +/// ⌥ (Alt) erases what the brush painted. +pub(super) fn lens_blur_brush_overlay(app: &mut LightcraftApp, ui: &mut egui::Ui, resp: &egui::Response, map: &CanvasMap) { + let long = (map.rect.width().max(map.rect.height())) as f64; + let clip = app.canvas_rect.unwrap_or(map.rect); + let p = &ui.painter_at(clip); + let focus = app.ui.tool == "lensBlurFocus"; + let r = (app.ui.brush_size as f64 * long) as f32; + if let Some(h) = resp.hover_pos() { + ui.ctx().set_cursor_icon(egui::CursorIcon::None); + p.circle_stroke(h, r, Stroke::new(1.0, Color32::WHITE)); + p.circle_stroke(h, r * (1.0 - app.ui.brush_feather / 100.0).max(0.05), Stroke::new(1.0, Color32::from_white_alpha(120))); + } + if let Some(q) = resp.interact_pointer_pos() + && (resp.drag_started() || resp.dragged() || resp.clicked()) + { + let n = map.norm(q); + let force = if app.ui.brush_pressure { ui.input(pen_force) } else { None }; + match &mut app.gesture { + Some(Gesture::Brush { points, .. }) if points.last() == Some(&n) => {} + Some(Gesture::Brush { points, pressure }) => { + record_pressure(pressure, points.len(), force); + points.push(n); + } + _ => { + let mut pressure = Vec::new(); + record_pressure(&mut pressure, 0, force); + app.gesture = Some(Gesture::Brush { points: vec![n], pressure }); + } + } + } + if let Some(Gesture::Brush { points, pressure }) = &app.gesture { + let screen: Vec = points.iter().map(|q| map.screen(*q)).collect(); + let params = crate::brush_live::LiveParams { + clip, + scale: ui.ctx().pixels_per_point(), + r, + hard: r * (1.0 - (app.ui.brush_feather / 100.0).clamp(0.0, 1.0)), + flow: (app.ui.brush_flow / 100.0).clamp(0.0, 1.0), + density: (app.ui.lens_blur_brush_amount / 100.0).clamp(0.0, 1.0), + color: if focus { [70, 200, 120] } else { [230, 70, 70] }, + opacity: 0.45, + }; + crate::brush_live::paint(&mut app.brush_live, ui.ctx(), p, params, &screen, pressure); + } else { + app.brush_live = None; + } + if (resp.drag_stopped() || resp.clicked()) + && let Some(Gesture::Brush { points, pressure }) = app.gesture.take() + { + app.brush_live = None; + let pts: Vec<[f64; 2]> = points.iter().map(|q| [q.x, q.y]).collect(); + let erase = resp.ctx.input(|i| i.modifiers.alt); + let r = app.run( + "lensBlur.brushStroke", + json!({"mode": if focus { "focus" } else { "blur" }, "points": pts, "size": app.ui.brush_size, "feather": app.ui.brush_feather, + "flow": app.ui.brush_flow, "amount": app.ui.lens_blur_brush_amount, "erase": erase, "autoMask": app.ui.brush_auto_mask, + "pressure": pressure}), + ); + if let Err(e) = r { + app.toast(ui.ctx(), e); + } + } +} diff --git a/crates/ui-egui/src/panels/detail/masks.rs b/crates/ui-egui/src/panels/detail/masks.rs index ebcb35e611..1148d9527b 100644 --- a/crates/ui-egui/src/panels/detail/masks.rs +++ b/crates/ui-egui/src/panels/detail/masks.rs @@ -879,7 +879,7 @@ fn current_on_photo(app: &LightcraftApp, mask: u32) -> Option { } /// Long-edge units → normalized units for x and y. -fn frame_long_norm(map: &CanvasMap) -> (f64, f64) { +pub(super) fn frame_long_norm(map: &CanvasMap) -> (f64, f64) { // derive from the mapping: 1 normalized unit in x and y in screen points (of the frame's // proportions, which a lens or perspective warp doesn't change) let map = map.for_mask(false); diff --git a/crates/ui-egui/src/panels/edit.rs b/crates/ui-egui/src/panels/edit.rs index 195bcb6488..15f2f277f3 100644 --- a/crates/ui-egui/src/panels/edit.rs +++ b/crates/ui-egui/src/panels/edit.rs @@ -21,6 +21,7 @@ mod curve; mod grading; mod hdr_flow; mod histogram; +mod lens_blur; mod mixer; mod sliders; @@ -31,6 +32,7 @@ pub(crate) use grading::wheel_pointer_frame_end; pub(crate) use grading::{WheelGrab, WheelLock, WheelMods, wheel_pick}; pub(crate) use histogram::histogram; use histogram::soft_proofing; +use lens_blur::lens_blur; use mixer::{mixer, point_color}; use sliders::{REL_TEMP, REL_TINT, hdr_row, header_buttons, k_to_rel, profile_menu, quick_develop, rel_to_k, reset_tip, section}; pub use sliders::{apply_slider_out, sub_title}; @@ -374,6 +376,10 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) { } ui.add_space(8.0); }); + section(app, ui, &d, "lensBlur", "Lens Blur", |app, ui, d| { + lens_blur(app, ui, d); + ui.add_space(8.0); + }); // Lightroom Classic's Calibration panel (the cloud app hides it): last, like there. section(app, ui, &d, "calibration", "Calibration", |app, ui, d| { sub_title(ui, crate::i18n::tr("Shadows")); diff --git a/crates/ui-egui/src/panels/edit/lens_blur.rs b/crates/ui-egui/src/panels/edit/lens_blur.rs new file mode 100644 index 0000000000..b7aa696736 --- /dev/null +++ b/crates/ui-egui/src/panels/edit/lens_blur.rs @@ -0,0 +1,257 @@ +//! The Lens Blur section: Apply, the sliders, what stays in focus (the subject, a mask or a fixed +//! shape that can be moved on the photo), Find Subject and Brush Refinement. + +use super::{ + Color32, ControlSpec, Icon, LightcraftApp, Pos2, Rect, RichText, Section, Sense, Stroke, Tokens, Track, control, paint, pos2, register, slider, + sub_title, text_button, vec2, +}; +use lightcraft_develop::DevelopSettings; +use serde_json::json; + +/// The Lens Blur section: Apply, Blur Amount, Boost, and what stays in focus (the photo's subject, +/// or one of its masks, e.g. an Object mask that selected it). +pub(super) fn lens_blur(app: &mut LightcraftApp, ui: &mut egui::Ui, d: &DevelopSettings) { + let t = Tokens::get(ui.ctx()); + let b = &d.lens_blur; + egui::Frame::NONE.inner_margin(egui::Margin { left: 24, right: 22, top: 4, bottom: 4 }).show(ui, |ui| { + let mut on = b.enabled; + let r = ui.checkbox(&mut on, crate::i18n::tr("Apply")); + register(ui, "check:lensBlur.enabled", r.rect); + if r.on_hover_text(crate::i18n::tr("Blur what is out of focus, as a wide aperture would")).changed() { + let mut patch = json!({"enabled": on}); + // switched on the first time: a visible starting amount, as in Lightroom + if on && b.amount == 0.0 { + patch["amount"] = json!(50.0); + } + let _ = app.run("develop.merge", json!({"settings": {"lens_blur": patch}, "label": "Lens Blur"})); + } + }); + control(app, ui, d, "lensBlur.amount", b.enabled); + control(app, ui, d, "lensBlur.boost", b.enabled); + control(app, ui, d, "lensBlur.transition", b.enabled); + // the focus controls do nothing while the blur is off: shown, but not usable + let has_mask = b.focus_mask.and_then(|id| d.masks.iter().find(|m| m.id == id)).is_some(); + if app.ui.tool == "lensBlurShape" && (!b.enabled || has_mask || b.shape == lightcraft_develop::FocusShape::Auto) { + app.ui.tool.clear(); + } + egui::Frame::NONE.inner_margin(egui::Margin { left: 24, right: 22, top: 6, bottom: 4 }).show(ui, |ui| ui.add_enabled_ui(b.enabled, |ui| { + ui.horizontal(|ui| { + ui.label(RichText::new(crate::i18n::tr("In Focus")).font(t.font(13.0)).color(t.text_dim)); + let chosen = b.focus_mask.and_then(|id| d.masks.iter().find(|m| m.id == id)); + // without a mask: the subject estimate, or the fixed shape chosen below + let auto = b.shape == lightcraft_develop::FocusShape::Auto; + let none = if auto { crate::i18n::tr("Subject") } else { crate::i18n::tr(b.shape.label()) }; + let shown = chosen.map_or_else(|| none.to_string(), |m| m.name.clone()); + let r = crate::widgets::dropdown(ui, "lensBlurFocus", &shown, t.font(14.0), t.text_label); + let r = r.on_hover_text(crate::i18n::tr("What stays sharp: the photo's subject, or the selection of one of its masks")); + let mut pick: Option> = None; + egui::Popup::menu(&r).show(|ui| { + let item = ui.selectable_label(chosen.is_none() && auto, crate::i18n::tr("Subject")); + register(ui, "lensBlurFocus:subject", item.rect); + if item.clicked() { + pick = Some(None); + } + for m in &d.masks { + let item = ui.selectable_label(chosen.is_some_and(|c| c.id == m.id), m.name.as_str()); + register(ui, format!("lensBlurFocus:mask:{}", m.id), item.rect); + if item.clicked() { + pick = Some(Some(m.id)); + } + } + }); + if let Some(focus) = pick { + // "Subject": the estimate again, not a fixed shape + let mut patch = json!({"focus_mask": focus}); + if focus.is_none() { + patch["shape"] = json!("auto"); + } + let _ = app.run("develop.merge", json!({"settings": {"lens_blur": patch}, "label": "Lens Blur Focus"})); + } + }); + // without a focus mask: the focus area's shape (the estimate, or a fixed one), which can + // be moved and resized on the photo + if !has_mask { + ui.add_space(4.0); + focus_shape_row(app, ui, b); + if b.shape != lightcraft_develop::FocusShape::Auto { + let active = app.ui.tool == "lensBlurShape"; + let r = text_button(ui, "lensBlurEditShape", "Move & Resize", active).on_hover_text(crate::i18n::tr( + "Drag the shape on the photo to move it; drag its side or bottom handle to stretch it. A shape button puts it back in the middle.", + )); + if r.clicked() { + if active { app.ui.tool.clear() } else { app.ui.tool = "lensBlurShape".into() } + } + } + } + // the AI subject search runs only when asked: it takes a while on the CPU, and the + // automatic estimate stays until then + ui.add_space(4.0); + let r = text_button(ui, "lensBlurFindSubject", "Find Subject", false).on_hover_text(crate::i18n::tr( + "Select the subject with AI (SAM 3), starting where the camera focused when the file records it, and keep it in focus", + )); + if r.clicked() { + match app.run_typed("lensBlur.findSubject", json!({})) { + Ok(v) => { + let msg = if v["from"] == "camera" { "Selecting the subject at the camera's focus point…" } else { "Selecting the subject…" }; + app.toast(ui.ctx(), crate::i18n::tr(msg)); + } + Err(e) => app.ai_error(ui.ctx(), e, None), + } + } + // a Find Subject still running: say so (meanwhile the automatic estimate stays in focus) + let waiting = b.focus_mask.and_then(|id| d.masks.iter().find(|m| m.id == id)).is_some_and(lightcraft_pipeline::lens_blur::awaiting_ai); + if waiting && app.session.segmenter.busy() { + ui.horizontal(|ui| { + ui.spinner(); + ui.label(RichText::new(crate::i18n::tr("Finding the subject… The first time on a photo takes a while.")).size(11.0).color(t.text)); + }); + ui.ctx().request_repaint_after(std::time::Duration::from_millis(250)); + } else if waiting { + ui.label( + RichText::new(crate::i18n::tr("The subject wasn't selected. Try Find Subject again, or click it in Masking (M).")) + .size(11.0) + .color(t.text), + ); + } + ui.label( + RichText::new(crate::i18n::tr("Refine it in Masking (M): + → Object adds what was missed, − → Object leaves a part out.")) + .size(11.0) + .color(t.text_dim), + ); + })); + lens_blur_brush(app, ui, b); +} + +/// The Lens Blur focus area's shape when no mask sets it: one button per shape, each showing it +/// (drawn in the theme's colours, so it reads on the light themes too). +fn focus_shape_row(app: &mut LightcraftApp, ui: &mut egui::Ui, b: &lightcraft_develop::LensBlur) { + use lightcraft_develop::FocusShape; + let t = Tokens::get(ui.ctx()); + let enabled = ui.is_enabled(); + ui.horizontal(|ui| { + ui.label(RichText::new(crate::i18n::tr("Shape")).font(t.font(13.0)).color(t.text_dim)); + ui.add_space(4.0); + for shape in FocusShape::ALL { + let selected = b.shape == shape; + let (r, resp) = ui.allocate_exact_size(vec2(36.0, 28.0), Sense::click()); + let name = serde_json::to_value(shape).ok().and_then(|v| v.as_str().map(str::to_string)).unwrap_or_default(); + register(ui, format!("lensBlurShape:{name}"), r); + crate::widgets::announce(&resp, crate::i18n::tr(shape.label())); + let resp = resp.on_hover_text(crate::i18n::tr(match shape { + FocusShape::Auto => "Auto: the photo's subject, estimated", + FocusShape::Round => "Round", + FocusShape::Wide => "Horizontal oval", + FocusShape::Tall => "Vertical oval", + })); + let p = ui.painter(); + let fill = if selected { + t.pressed + } else if resp.hovered() && enabled { + t.hover + } else { + t.button + }; + p.rect_filled(r, 4.0, fill); + if selected { + p.rect_stroke(r, 4.0, Stroke::new(1.0, if enabled { t.accent } else { t.text_dim }), egui::StrokeKind::Inside); + } + // (dimmed while the blur is off, in the theme's colours) + let colour = match (enabled, selected) { + (false, _) => t.icon.gamma_multiply(0.4), + (true, true) => t.accent, + (true, false) => t.icon, + }; + paint_focus_shape(p, r.shrink(5.0), shape, colour); + // a preset puts the shape back in the middle at its size (also the one in use, once moved) + if resp.clicked() && (!selected || b.area.is_some()) { + let _ = app.run("develop.merge", json!({"settings": {"lens_blur": {"shape": name, "area": null}}, "label": "Lens Blur Shape"})); + } + } + }); +} + +/// A focus shape's picture in `r`: the oval in its proportions (the largest fills the height), or +/// the subject icon for Auto. +fn paint_focus_shape(p: &egui::Painter, r: Rect, shape: lightcraft_develop::FocusShape, colour: Color32) { + let Some((rx, ry)) = shape.radii() else { + paint(p, r, Icon::Subject, colour); + return; + }; + // the shapes' largest radius spans the box's height (all shapes on one scale) + let k = r.height() / 2.0 / 0.32; + let (ax, ay) = (rx as f32 * k, ry as f32 * k); + let c = r.center(); + let pts: Vec = (0..48) + .map(|i| { + let a = i as f32 / 48.0 * std::f32::consts::TAU; + pos2(c.x + ax * a.cos(), c.y + ay * a.sin()) + }) + .collect(); + p.add(egui::Shape::convex_polygon(pts.clone(), colour.gamma_multiply(0.3), Stroke::NONE)); + p.add(egui::Shape::closed_line(pts, Stroke::new(1.5, colour))); +} + +/// Lens Blur Brush Refinement: the Focus and Blur brushes (painted on the photo, see +/// `detail::lens_blur_brush_overlay`), their Amount, the shared brush's Size, Feather, Flow and +/// Auto Mask, and Reset. +fn lens_blur_brush(app: &mut LightcraftApp, ui: &mut egui::Ui, b: &lightcraft_develop::LensBlur) { + let on = b.enabled; + sub_title(ui, crate::i18n::tr("Brush Refinement")); + egui::Frame::NONE.inner_margin(egui::Margin { left: 24, right: 22, top: 0, bottom: 4 }).show(ui, |ui| { + ui.add_enabled_ui(on, |ui| { + ui.horizontal(|ui| { + for (tool, label, tip) in + [("lensBlurFocus", "Focus", "Paint what should stay sharp"), ("lensBlurBlur", "Blur", "Paint what should be blurred")] + { + let active = app.ui.tool == tool; + let r = text_button(ui, tool, label, active).on_hover_text(crate::i18n::tr(tip)); + if r.clicked() { + if active { app.ui.tool.clear() } else { app.ui.tool = tool.to_string() } + } + } + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + let painted = !b.focus_strokes.is_empty() || !b.blur_strokes.is_empty(); + if text_button(ui, "lensBlurBrushReset", "Reset", false) + .on_hover_text(crate::i18n::tr("Remove the strokes of both brushes")) + .clicked() + && painted + { + let _ = app.run("lensBlur.brushReset", json!({})); + } + }); + }); + let mut auto = app.ui.brush_auto_mask; + if ui + .checkbox(&mut auto, crate::i18n::tr("Auto Mask")) + .on_hover_text(crate::i18n::tr("Paint only areas like the one under the brush")) + .changed() + { + app.ui.brush_auto_mask = auto; + } + }); + }); + // the brush is the Masking brush's (its size also follows [ and ]), with its own Amount + // (a reset puts the brush's Amount back to full strength, the others to their minimum) + for (id, label, min, max, get) in [ + ("ui.lensBlurBrushAmount", "Amount", 1.0, 100.0, app.ui.lens_blur_brush_amount as f64), + ("ui.brushSize", "Size", 1.0, 100.0, (app.ui.brush_size * 400.0) as f64), + ("ui.brushFeather", "Feather", 0.0, 100.0, app.ui.brush_feather as f64), + ("ui.brushFlow", "Flow", 1.0, 100.0, app.ui.brush_flow as f64), + ] { + let default = if id == "ui.lensBlurBrushAmount" { 100.0 } else { min }; + let s = ControlSpec { id, label, section: Section::LensBlur, min, max, default, step: 1.0, decimals: 0, track: Track::Plain }; + let out = slider(ui, &s, get, on, None); + if let Some(v) = out.value { + match id { + "ui.lensBlurBrushAmount" => app.ui.lens_blur_brush_amount = v as f32, + "ui.brushSize" => app.ui.brush_size = (v / 400.0) as f32, + "ui.brushFeather" => app.ui.brush_feather = v as f32, + _ => app.ui.brush_flow = v as f32, + } + } + } + // the brushes paint only while the blur is on + if !on && app.ui.tool.starts_with("lensBlur") { + app.ui.tool.clear(); + } +} diff --git a/crates/ui-egui/src/panels/grid.rs b/crates/ui-egui/src/panels/grid.rs index db4dfa1b83..556f809d27 100644 --- a/crates/ui-egui/src/panels/grid.rs +++ b/crates/ui-egui/src/panels/grid.rs @@ -291,35 +291,20 @@ pub fn rows_between(rows: &[GridRow], top: f32, bottom: f32) -> &[GridRow] { } fn show_inner(app: &mut LightcraftApp, ui: &mut egui::Ui) { - let t = Tokens::get(ui.ctx()); let (generation, ids) = app.session.visible_shared(); // header: source title + count let sel_n = app.session.selection.ids.len(); let chips = lightcraft_engine::filter_chips(&app.session.filter, &app.session.catalog); let total = app.session.source_total(); let counted = super::chips::count_text(ids.len(), total, !chips.is_empty()); - let cnt = if sel_n > 1 { crate::i18n::tr_format!("{sel_n} selected · {counted}", counted = counted, sel_n = sel_n) } else { counted }; + let cnt = if sel_n > 0 { crate::i18n::tr_format!("{sel_n} selected · {counted}", counted = counted, sel_n = sel_n) } else { counted }; match app.session.browse.clone().filter(|_| app.session.source == lightcraft_engine::LibrarySource::Folder) { Some(b) => { let local = app.caches.grid.local(&app.session.catalog, &ids, generation); folder_header(app, ui, &b, &ids, local, &cnt); } None => { - let (hr, _) = ui.allocate_exact_size(vec2(ui.available_width(), 44.0), Sense::hover()); - ui.painter().rect_filled(hr, 0.0, t.canvas); - let full = crate::i18n::source_title(&app.session); - let count_w = ui.painter().layout_no_wrap(cnt.clone(), t.font(12.5), t.text_dim).size().x; - // the title gives way to the count: a long album or folder name is trimmed, in full on hover - let room = (hr.width() - 20.0 - count_w - 36.0).max(0.0); - let font = t.semibold(17.0); - let title = crate::widgets::elide_head(&full, room, |s| ui.painter().layout_no_wrap(s.to_string(), font.clone(), t.text).size().x); - let title_rect = ui.painter().text(pos2(hr.left() + 20.0, hr.center().y), Align2::LEFT_CENTER, &title, font, t.text); - let count_rect = ui.painter().text(pos2(hr.right() - 20.0, hr.center().y), Align2::RIGHT_CENTER, cnt, t.font(12.5), t.text_dim); - crate::widgets::register(ui, "grid:title", title_rect); - crate::widgets::register(ui, "grid:count", count_rect); - if title != full { - ui.interact(title_rect, ui.scope_id().with("grid-title"), Sense::hover()).on_hover_text(full); - } + library_header(app, ui, &cnt); } } super::chips::show(app, ui, &chips); @@ -1016,6 +1001,35 @@ struct BadgeFlags { hovered: bool, } +/// The header of a library view (All Photos, an album, a library folder…): the source's title and +/// the photo count. +fn library_header(app: &LightcraftApp, ui: &mut egui::Ui, cnt: &str) -> Rect { + let t = Tokens::get(ui.ctx()); + let (hr, _) = ui.allocate_exact_size(vec2(ui.available_width(), 44.0), Sense::hover()); + ui.painter().rect_filled(hr, 0.0, t.canvas); + let full = crate::i18n::source_title(&app.session); + // the title gives way to the count: a long album or folder name is trimmed, in full on hover; + // a count wider than all but the title's ellipsis (a narrow centre pane) is cut, as a Local + // folder's is, and reads in full on hover + let count_w = ui.painter().layout_no_wrap(cnt.to_owned(), t.font(12.5), t.text_dim).size().x.ceil().min((hr.width() - 96.0).max(0.0).floor()); + let room = (hr.width() - 20.0 - count_w - 36.0).max(0.0); + let font = t.semibold(17.0); + let title = crate::widgets::elide_head(&full, room, |s| ui.painter().layout_no_wrap(s.to_string(), font.clone(), t.text).size().x); + let title_rect = ui.painter().text(pos2(hr.left() + 20.0, hr.center().y), Align2::LEFT_CENTER, &title, font, t.text); + let count_box = Rect::from_min_max(pos2(hr.right() - 20.0 - count_w, hr.top()), pos2(hr.right() - 20.0, hr.bottom())); + let label = egui::Label::new(egui::RichText::new(cnt).font(t.font(12.5)).color(t.text_dim)).truncate().show_tooltip_when_elided(false); + let count = ui + .scope_builder(egui::UiBuilder::new().max_rect(count_box).layout(egui::Layout::right_to_left(egui::Align::Center)), |ui| ui.add(label)) + .inner; + crate::widgets::register(ui, "grid:title", title_rect); + crate::widgets::register(ui, "grid:count", count.rect); + count.on_hover_text(cnt); + if title != full { + ui.interact(title_rect, ui.scope_id().with("grid-title"), Sense::hover()).on_hover_text(full); + } + hr +} + /// The header of a Local folder view: the path as a breadcrumb (each part opens that folder), /// Include subfolders, Add to My Photos. fn folder_header(app: &mut LightcraftApp, ui: &mut egui::Ui, b: &lightcraft_engine::Browse, ids: &[PhotoId], local_n: usize, cnt: &str) -> Rect { diff --git a/crates/ui-egui/src/panels/grid/header_tests.rs b/crates/ui-egui/src/panels/grid/header_tests.rs index 463991625e..db5cd37ffe 100644 --- a/crates/ui-egui/src/panels/grid/header_tests.rs +++ b/crates/ui-egui/src/panels/grid/header_tests.rs @@ -17,6 +17,122 @@ fn breadcrumbs_open_their_folder_as_spelled() { assert!(to("").is_empty() && to("/").is_empty() && to(r"\\").is_empty()); } +/// Issue #3776: one selected photo is counted in the header as two or more are ("1 selected · 3 +/// photos"); with nothing selected the header shows the photo count alone. +#[test] +fn the_header_counts_a_single_selected_photo() { + use lightcraft_catalog::{Op, Photo, Source}; + for (selected, folder) in [(0, false), (1, false), (2, false), (0, true), (1, true)] { + let mut session = lightcraft_engine::Session::new(); + for i in 0..3 { + let id = session.catalog.alloc_photo_id(); + let p = Photo::new(id, Source::File { path: format!("/pics/{i}.jpg") }, "x.jpg", "JPEG", 60, 40, "2026-01-01T10:00:00"); + session.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap(); + } + session.selection.ids = session.catalog.photos().take(selected).map(|p| p.id).collect(); + if folder { + // a Local folder view: the same count, in the breadcrumb header + session.source = lightcraft_engine::LibrarySource::Folder; + session.browse = Some(lightcraft_engine::Browse { path: "/pics".into(), subfolders: false }); + } + let mut app = LightcraftApp::new(session, Services { png: None, ..Default::default() }); + app.ui.view = crate::state::ViewMode::PhotoGrid; + let mut h = crate::headless::Headless::new(app, [1400.0, 900.0], 1.0); + h.step(); + h.step(); + assert_eq!(h.app.session.selection.ids.len(), selected, "the selection is kept"); + let has = |id: &str| h.app.widgets.iter().any(|(w, _)| w == id); + assert!(has("grid:count") && has("grid:folderHeader") == folder, "folder {folder}: {:?}", h.app.widgets); + let painted = h.painted_text(); + let photos = crate::panels::chips::count_text(h.app.session.visible_cloned().len(), h.app.session.source_total(), false); + assert!(folder || photos == "3 photos", "{photos}"); + let expected = if selected == 0 { photos } else { format!("{selected} selected · {photos}") }; + assert!(painted.contains(&expected), "folder {folder}, {selected} selected: the header shows {expected:?}: {painted:?}"); + if selected == 0 { + assert!(!painted.iter().any(|s| s.contains("selected ·")), "nothing selected, no count: {painted:?}"); + } + } + // a language whose word for "selected" agrees with the number reads "noun: number", right for one as for many + for (locale, expected) in [(Locale::Es, "Seleccionadas: 1 · x"), (Locale::PtBr, "Selecionadas: 1 · x"), (Locale::Sv, "Markerade: 1 · x")] { + crate::i18n::set_language(locale); + let one = crate::i18n::tr_format!("{sel_n} selected · {counted}", sel_n = 1, counted = "x"); + crate::i18n::set_language(Locale::En); + assert_eq!(one, expected, "{locale:?}"); + } +} + +/// Every text the frame painted as it reads on screen (a cut one with its "…"), with where it was painted. +fn texts_painted(shape: &egui::Shape, out: &mut Vec<(String, Rect)>) { + match shape { + egui::Shape::Text(text) => { + let shown = text.galley.rows.iter().flat_map(|row| row.glyphs.iter().map(|g| g.chr)).collect(); + out.push((shown, text.visual_bounding_rect())); + } + egui::Shape::Vec(shapes) => shapes.iter().for_each(|s| texts_painted(s, out)), + _ => {} + } +} + +/// A library view's count too long for a narrow centre pane (German, at the narrowest the photo +/// area gets) is cut to stay inside the header, beside the title, and reads in full on hover. +#[test] +fn a_long_library_count_stays_inside_a_narrow_header() { + for width in [crate::state::MIN_PHOTO_WIDTH, 590.0] { + for scale in [1.0, 2.0] { + let app = LightcraftApp::new(lightcraft_engine::Session::new(), Services::default()); + let ctx = egui::Context::default(); + crate::i18n::set_language(Locale::De); + crate::theme::install_fonts(&ctx); + crate::theme::apply(&ctx); + let counted = crate::i18n::tr_format!("{matching} of {t} photos", matching = 123_456_789, t = 987_654_321); + let cnt = crate::i18n::tr_format!("{sel_n} selected · {counted}", sel_n = 123_456_789, counted = counted); + let frame = |time: f64, events: Vec| { + let mut header = Rect::NOTHING; + let raw = HeadlessView::raw_input(vec2(width, 300.0), scale, time, events); + let mut out = ctx.run_ui(raw, |ui| { + egui::CentralPanel::default().frame(egui::Frame::NONE).show(ui, |ui| { + header = super::library_header(&app, ui, &cnt); + }); + }); + out.textures_delta.clear(); + let mut texts = Vec::new(); + for shape in &out.shapes { + texts_painted(&shape.shape, &mut texts); + } + (texts, crate::widgets::take_registry(&ctx), header) + }; + let (mut texts, mut widgets, mut header) = (Vec::new(), Vec::new(), Rect::NOTHING); + for f in 0..3 { + (texts, widgets, header) = frame(f as f64 / 60.0, vec![]); + } + let widget = |id: &str| widgets.iter().find(|(w, _)| w == id).map(|(_, r)| *r); + let (Some(count), Some(title)) = (widget("grid:count"), widget("grid:title")) else { panic!("no count or title: {widgets:?}") }; + let at = format!("{width} {scale}"); + assert!(header.left() >= 0.0 && header.right() <= width, "{at}: header must not expand past the centre pane: {header:?}"); + assert!(header.contains_rect(count), "{at}: the count stays inside the header: {count:?} in {header:?}"); + assert!(title.right() <= count.left(), "{at}: the title stays left of the count: {title:?} {count:?}"); + for (i, (text, a)) in texts.iter().enumerate() { + assert!(header.expand(1.0).contains_rect(*a), "{at}: {text:?} painted outside the header: {a:?}"); + for (other, b) in texts.iter().skip(i + 1) { + assert!(!a.intersects(*b), "{at}: {text:?} overlaps {other:?}"); + } + } + // cut where it doesn't fit, and in full on hover + let fits = width > 500.0; + assert_eq!(texts.iter().any(|(t, _)| *t == cnt), fits, "{at}: {texts:?}"); + let cut = cnt.split(" von ").next().unwrap_or_default(); + assert!(fits || texts.iter().any(|(t, _)| t.ends_with('…') && t.starts_with(cut)), "{at}: the count is cut at its end: {texts:?}"); + let mut shown = Vec::new(); + for f in 3..120 { + let events = if f == 3 { vec![egui::Event::PointerMoved(count.center())] } else { vec![] }; + (shown, _, _) = frame(f as f64 / 60.0, events); + } + assert!(shown.iter().any(|(t, _)| *t == cnt), "{at}: the full count shows on hover: {shown:?}"); + crate::i18n::set_language(Locale::En); + } + } +} + fn text_bounds(shape: &egui::Shape, out: &mut Vec) { match shape { egui::Shape::Text(text) => out.push(text.visual_bounding_rect()), diff --git a/crates/ui-egui/src/panels/masking.rs b/crates/ui-egui/src/panels/masking.rs index ce28482c46..d833fbeb11 100644 --- a/crates/ui-egui/src/panels/masking.rs +++ b/crates/ui-egui/src/panels/masking.rs @@ -35,6 +35,7 @@ pub const LOCAL: &[ControlSpec] = &[ spec("noise", "Noise", -100.0, 100.0, 1.0, 0, Track::Centered), spec("moire", "Moiré", -100.0, 100.0, 1.0, 0, Track::Centered), spec("defringe", "Defringe", -100.0, 100.0, 1.0, 0, Track::Centered), + spec("lens_blur", "Lens Blur", 0.0, 100.0, 1.0, 0, Track::Plain), ]; pub fn local_get(a: &LocalAdjustments, key: &str) -> f64 { @@ -383,7 +384,8 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) { } divider(ui); super::mask_effects::effect_row(app, ui, &m); - for s in LOCAL { + // (Lens Blur has its own block below) + for s in LOCAL.iter().filter(|s| s.id != "lens_blur") { let v = local_get(&m.adjust, s.id); let out = slider(ui, s, v, true, None); apply_slider_out(app, s, out, |app, v| app.run("mask.adjust", json!({"values": {s.id: v}}))); @@ -414,10 +416,63 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) { }; let out = slider(ui, &refine, m.refine, true, None); apply_slider_out(app, &refine, out, |app, v| app.run("mask.refine", json!({"value": v}))); + divider(ui); + lens_blur_block(app, ui, &m); ui.add_space(30.0); let _ = Stroke::NONE; } +/// The mask's Lens Blur block (`section:maskLensBlur`): a header that folds it and whose eye +/// switches it off (the values kept), which side of the mask it blurs (`button:maskLensBlurSide-*`), +/// then its Amount, Boost and Transition (`lens_blur*` local adjustments, `mask.adjust`). +fn lens_blur_block(app: &mut LightcraftApp, ui: &mut egui::Ui, m: &lightcraft_develop::Mask) { + let a = &m.adjust; + let open = !app.ui.section_folded("maskLensBlur"); + let (resp, toggled) = crate::widgets::section_header(ui, "maskLensBlur", "Lens Blur", open, Some(!a.lens_blur_off)); + if let Some(on) = toggled { + // switched on with nothing to blur: a visible amount, as the Edit section's Apply does + let mut v = json!({"lens_blur_off": !on}); + if on && a.lens_blur <= 0.0 { + v["lens_blur"] = json!(50.0); + } + let _ = app.run("mask.adjust", json!({"values": v})); + } else if resp.clicked() { + app.ui.toggle_folded("maskLensBlur"); + } + if !open { + return; + } + ui.add_enabled_ui(!a.lens_blur_off, |ui| { + egui::Frame::NONE.inner_margin(egui::Margin { left: 24, right: 22, top: 0, bottom: 6 }).show(ui, |ui| { + let t = Tokens::get(ui.ctx()); + ui.label(egui::RichText::new(crate::i18n::tr("Blur")).color(t.text_dim).size(11.5)); + let items = [("Selection", "inside"), ("Outside", "outside")]; + let side = if a.lens_blur_outside { 1 } else { 0 }; + if let Some(i) = crate::widgets::segmented(ui, "maskLensBlurSide", &items, Some(side), 2) + && i != side + { + let mut v = json!({"lens_blur_outside": i == 1}); + if a.lens_blur <= 0.0 { + v["lens_blur"] = json!(50.0); + } + let _ = app.run("mask.adjust", json!({"values": v})); + } + }); + for s in LENS_BLUR { + let v = local_get(a, s.id); + let out = slider(ui, s, v, true, None); + apply_slider_out(app, s, out, |app, v| app.run("mask.adjust", json!({"values": {s.id: v}}))); + } + }); +} + +/// The sliders of a mask's Lens Blur block (key = `LocalAdjustments` field name). +const LENS_BLUR: &[ControlSpec] = &[ + spec("lens_blur", "Amount", 0.0, 100.0, 1.0, 0, Track::Plain), + spec("lens_blur_boost", "Boost", 0.0, 100.0, 1.0, 0, Track::Plain), + spec("lens_blur_transition", "Transition", 0.0, 100.0, 1.0, 0, Track::Plain), +]; + /// Translate fixed action labels, but never mutate or translate user-entered mask names. fn menu_label(source: &str) -> &str { crate::i18n::tr(source) diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 37a8c2d4bb..203ede571e 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -246,7 +246,8 @@ pub struct UiState { /// the photo that applies to: not an edit, and forgotten on moving to another photo or quitting. #[serde(skip)] pub grading_hidden: Option<(lightcraft_catalog::PhotoId, [bool; 3])>, - /// Active on-canvas tool: "", "brush", "linear", "radial", "wbPicker", "straighten", "remove". + /// Active on-canvas tool: "", "brush", "linear", "radial", "wbPicker", "straighten", "remove", + /// "lensBlurFocus" / "lensBlurBlur" (Lens Blur Brush Refinement, in the Edit panel). pub tool: String, /// Guided Upright: the selected guide (photo id + index) while the tool is open. A Delete / /// Backspace on it removes that guide alone; a click away or leaving the tool clears it. @@ -260,6 +261,9 @@ pub struct UiState { pub brush_auto_mask: bool, /// Brush Pen Pressure: a pen's pressure scales each dab's size and flow (a mouse paints at full pressure). pub brush_pressure: bool, + /// Lens Blur Brush Refinement: how far a stroke refines the focus, 1..100 (size, feather, + /// flow, Auto Mask and pressure are the brush's above). + pub lens_blur_brush_amount: f32, /// Remove tool brush: size (fraction of the long edge), feather and opacity (0..100). pub remove_size: f32, pub remove_feather: f32, @@ -484,6 +488,7 @@ impl Default for UiState { brush_erase: false, brush_auto_mask: false, brush_pressure: true, + lens_blur_brush_amount: 100.0, remove_size: 0.02, remove_feather: 50.0, remove_opacity: 100.0, diff --git a/crates/ui-egui/src/tests/compare.rs b/crates/ui-egui/src/tests/compare.rs index 4ce0341fb1..ee86f5322f 100644 --- a/crates/ui-egui/src/tests/compare.rs +++ b/crates/ui-egui/src/tests/compare.rs @@ -485,6 +485,110 @@ fn survey_command_click_takes_the_photo_out() { assert_eq!((sel(&h), active(&h)), (vec![a], Some(a)), "the last photo stays in the survey"); } +/// A photo taken out of the survey leaves the others to glide to their new cells, so it shows +/// which photo went where: a frame later a tile is between its old and new place, then it lands. +#[test] +fn survey_tiles_glide_to_their_new_cells() { + let mut h = demo(); + let [a, _, c] = survey_of_three(&mut h); + let tile = |h: &Headless| h.app.widgets.iter().find(|(w, _)| *w == format!("cull:{c}")).map(|(_, r)| *r).expect("the tile is laid out"); + let before = tile(&h); + h.app.run("library.select", json!({"ids": [a], "mode": "toggle"})).unwrap(); + h.step(); + h.step(); + let moving = tile(&h); + assert!(h.app.survey_motion.moving(h.time()), "the tiles are on their way"); + h.settle(SETTLE); + let after = tile(&h); + assert!(!h.app.survey_motion.moving(h.time())); + assert_ne!(before, after, "the photo has a new cell"); + assert!(moving != before && moving != after, "between {before:?} and {after:?}: {moving:?}"); +} + +/// A photo that joins the survey makes room: the tiles already there glide to their new cells too. +#[test] +fn survey_tiles_glide_when_a_photo_joins() { + let mut h = demo(); + let ids = survey_of_three(&mut h); + let joining = h.app.session.visible_cloned().iter().map(|p| p.0).find(|p| !ids.contains(p)).expect("a fourth photo"); + let tile = |h: &Headless, id: u64| h.app.widgets.iter().find(|(w, _)| *w == format!("cull:{id}")).map(|(_, r)| *r).expect("the tile is laid out"); + let before = ids.map(|id| tile(&h, id)); + h.app.run("library.select", json!({"ids": [joining], "mode": "toggle"})).unwrap(); + h.step(); + h.step(); + let moving = ids.map(|id| tile(&h, id)); + assert!(h.app.survey_motion.moving(h.time()), "the tiles are on their way"); + h.settle(SETTLE); + let after = ids.map(|id| tile(&h, id)); + let moved: Vec = (0..3).filter(|&i| before[i] != after[i]).collect(); + assert!(!moved.is_empty(), "a photo joining changes the grid: {before:?} → {after:?}"); + for i in moved { + assert!(moving[i] != before[i] && moving[i] != after[i], "tile {i} between {:?} and {:?}: {:?}", before[i], after[i], moving[i]); + } +} + +/// A photo that joins takes its cell at once, so it asks for its own render while the others are +/// still gliding (it doesn't wait on the thumbnail until they land). +#[test] +fn a_photo_that_joins_renders_while_the_others_glide() { + let mut h = demo(); + let ids = survey_of_three(&mut h); + let joining = h.app.session.visible_cloned().into_iter().find(|p| !ids.contains(&p.0)).expect("a fourth photo"); + h.app.run("library.select", json!({"ids": [joining.0], "mode": "toggle"})).unwrap(); + h.step(); + h.step(); + let now = h.time(); + assert!(h.app.survey_motion.moving(now), "the others are on their way"); + assert!(!h.app.survey_motion.gliding(joining, now), "the photo that joined stays put"); + let slot = h.app.survey_motion.slot(joining).expect("the photo that joined has a slot"); + assert!(h.app.renderer.wanted(crate::render::Slot::Compare(slot)).is_some(), "its render is asked for"); +} + +/// Photo ids are local to a library: opening another one forgets the survey's tiles, so a photo +/// there with the same id doesn't glide from a cell of the library left. +#[test] +fn opening_another_library_forgets_the_survey_tiles() { + let dir = std::env::temp_dir().join(format!("lc-ui-survey-switch-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let mut h = demo(); + let ids = survey_of_three(&mut h); + let tracked = |h: &Headless| ids.iter().filter(|id| h.app.survey_motion.tracks(lightcraft_catalog::PhotoId(**id))).count(); + assert_eq!(tracked(&h), 3); + let r = h.app.run("app.openLibrary", json!({"path": dir.join("second").to_string_lossy()})); + assert!(r.is_ok(), "{r:?}"); + assert_eq!(tracked(&h), 0, "nothing of the library left is remembered"); + drop(h); + let _ = std::fs::remove_dir_all(&dir); +} + +/// Every surveyed photo gets its own render, and keeps it when another photo leaves the survey +/// (each photo keeps its render slot, so the tiles that move don't fall back to thumbnails). +#[test] +fn survey_tiles_keep_their_renders_when_a_photo_leaves() { + let mut h = demo(); + let [a, b, c] = survey_of_three(&mut h); + let rendered = |h: &Headless, ids: &[u64]| { + ids.iter().all(|id| h.app.renderer.textures.iter().any(|(s, t)| matches!(s, crate::render::Slot::Compare(_)) && t.photo.0 == *id)) + }; + assert!(h.step_until(SETTLE, |h| rendered(h, &[a, b, c])), "each tile shows its render"); + let slot_of = |h: &Headless, id: u64| { + h.app.renderer.textures.iter().find(|(s, t)| matches!(s, crate::render::Slot::Compare(_)) && t.photo.0 == id).map(|(s, _)| *s) + }; + let c_slot = slot_of(&h, c).expect("c is rendered"); + let c_tex = h.app.renderer.textures.get(&c_slot).map(|t| t.tex.id()).expect("c's texture"); + h.app.run("library.select", json!({"ids": [a], "mode": "toggle"})).unwrap(); + h.step(); + h.step(); + assert!(h.app.survey_motion.moving(h.time())); + let drawn = h.view.painted_shapes().iter().any(|s| match s { + egui::epaint::Shape::Rect(r) => r.fill_texture_id() == c_tex, + egui::epaint::Shape::Mesh(m) => m.texture_id == c_tex, + _ => false, + }); + assert!(drawn, "c's render is drawn while it moves, not its thumbnail"); + assert!(h.step_until(SETTLE, |h| rendered(h, &[b, c]))); +} + /// Hover photo `id`'s survey tile, as a person does, so its × appears; then click the × in its corner. fn click_x(h: &mut Headless, id: u64) { let name = format!("cull:{id}"); diff --git a/crates/ui-egui/src/tests/masking.rs b/crates/ui-egui/src/tests/masking.rs index f7fbc60536..6bf491c37d 100644 --- a/crates/ui-egui/src/tests/masking.rs +++ b/crates/ui-egui/src/tests/masking.rs @@ -2155,3 +2155,157 @@ fn effect_dropdown_applies_saves_and_shows_the_matching_effect() { assert_eq!((a.exposure, a.shadows, a.clarity, a.amount), (0.7, 25.0, 0.0, 50.0)); assert_eq!(exec(&mut h, "mask.effects", json!({}))["current"], "Lift"); } + +/// Lens Blur Brush Refinement: Focus / Blur pick a brush that paints on the photo from the Edit +/// panel (one `lensBlur.brushStroke` per drag, with the brush's settings); ⌥ erases; Reset clears. +#[test] +fn lens_blur_focus_and_blur_brushes_paint_on_the_photo() { + // (`panel.edit` toggles the panel, which a session opens on: set it instead) + let mut h = detail("panel.masking"); + // tall enough for the Lens Blur section under the closed sections + assert_eq!(h.request("ui.resize", json!({"width": 1200, "height": 1400}), T)["ok"], true); + let r = h.request("ui.set", json!({"right": "edit", "openSections": ["lensBlur"], "histogram": false}), T); + assert_eq!(r["ok"], true, "{r}"); + // off: the brushes don't paint + click(&mut h, "button:lensBlurFocus"); + assert_eq!(h.app.ui.tool, "", "no brush while the blur is off"); + click(&mut h, "check:lensBlur.enabled"); + assert!(develop(&h).lens_blur.enabled); + click(&mut h, "button:lensBlurFocus"); + assert_eq!(h.app.ui.tool, "lensBlurFocus"); + h.app.ui.lens_blur_brush_amount = 60.0; + pointer( + &mut h, + json!([{"kind": "down", "x": 0.3, "y": 0.5}, {"kind": "drag", "x": 0.4, "y": 0.5}, {"kind": "drag", "x": 0.5, "y": 0.5}, {"kind": "up", "x": 0.5, "y": 0.5}]), + ); + let b = develop(&h).lens_blur; + assert_eq!(b.focus_strokes.len(), 1, "{b:?}"); + assert!(b.focus_strokes[0].points.len() >= 2 && b.focus_strokes[0].density == 60.0 && b.blur_strokes.is_empty()); + assert!((b.focus_strokes[0].size - h.app.ui.brush_size as f64).abs() < 1e-6, "the shared brush's size"); + click(&mut h, "button:lensBlurBlur"); + assert_eq!(h.app.ui.tool, "lensBlurBlur"); + brush_at(&mut h, 0.6, 0.4); + let r = h.request("ui.pointer", json!({"events": [{"kind": "down", "x": 0.7, "y": 0.4}, {"kind": "up", "x": 0.7, "y": 0.4}], "alt": true}), T); + assert_eq!(r["ok"], true, "{r}"); + let b = develop(&h).lens_blur; + assert_eq!(b.blur_strokes.len(), 2); + assert!(!b.blur_strokes[0].erase && b.blur_strokes[1].erase, "⌥ erases"); + click(&mut h, "button:lensBlurBrushReset"); + let b = develop(&h).lens_blur; + assert!(b.focus_strokes.is_empty() && b.blur_strokes.is_empty()); + // switching the blur off puts the brush down + click(&mut h, "check:lensBlur.enabled"); + assert_eq!(h.app.ui.tool, ""); + h.settle(SETTLE); +} + +/// Lens Blur focus shape: without a focus mask a button per shape sets it; with one they're gone. +#[test] +fn lens_blur_shape_buttons_pick_the_focus_shape() { + use lightcraft_develop::FocusShape; + let mut h = detail("panel.masking"); + assert_eq!(h.request("ui.resize", json!({"width": 1200, "height": 1400}), T)["ok"], true); + let r = h.request("ui.set", json!({"right": "edit", "openSections": ["lensBlur"], "histogram": false}), T); + assert_eq!(r["ok"], true, "{r}"); + click(&mut h, "check:lensBlur.enabled"); + assert_eq!(develop(&h).lens_blur.shape, FocusShape::Auto); + click(&mut h, "lensBlurShape:wide"); + assert_eq!(develop(&h).lens_blur.shape, FocusShape::Wide); + click(&mut h, "lensBlurShape:tall"); + assert_eq!(develop(&h).lens_blur.shape, FocusShape::Tall); + click(&mut h, "lensBlurShape:auto"); + assert_eq!(develop(&h).lens_blur.shape, FocusShape::Auto); + // a focus mask: the shape is the mask's, no buttons + exec(&mut h, "mask.add", json!({"kind": "radial"})); + let id = develop(&h).masks[0].id; + exec(&mut h, "develop.merge", json!({"settings": {"lens_blur": {"focus_mask": id}}})); + let r = h.request("ui.set", json!({"right": "edit"}), T); + assert_eq!(r["ok"], true, "{r}"); + let r = h.request("ui.clickWidget", json!({"id": "lensBlurShape:round"}), T); + assert_eq!(r["ok"], false, "no shape buttons with a focus mask: {r}"); + h.settle(SETTLE); +} + +/// While Apply is off the focus controls don't respond; on, Move & Resize drags the fixed shape on +/// the photo (one undo step per drag) and a shape button puts it back in the middle. +#[test] +fn lens_blur_focus_controls_follow_apply_and_the_shape_moves() { + use lightcraft_develop::FocusShape; + let mut h = detail("panel.masking"); + assert_eq!(h.request("ui.resize", json!({"width": 1200, "height": 1400}), T)["ok"], true); + let r = h.request("ui.set", json!({"right": "edit", "openSections": ["lensBlur"], "histogram": false}), T); + assert_eq!(r["ok"], true, "{r}"); + // off: shape buttons, Find Subject and In Focus are inert + let before = develop(&h).lens_blur; + click(&mut h, "lensBlurShape:wide"); + click(&mut h, "button:lensBlurFindSubject"); + click(&mut h, "dropdown:lensBlurFocus"); + let r = h.request("ui.clickWidget", json!({"id": "lensBlurFocus:subject"}), T); + assert_eq!(r["ok"], false, "the In Focus menu doesn't open: {r}"); + assert_eq!(develop(&h).lens_blur, before, "nothing changes while Apply is off"); + // on: a round shape, then Move & Resize + click(&mut h, "check:lensBlur.enabled"); + click(&mut h, "lensBlurShape:round"); + assert_eq!(develop(&h).lens_blur.shape, FocusShape::Round); + click(&mut h, "button:lensBlurEditShape"); + assert_eq!(h.app.ui.tool, "lensBlurShape"); + // drag the middle of the photo (the shape's centre) to the right + pointer( + &mut h, + json!([{"kind": "down", "x": 0.5, "y": 0.5}, {"kind": "drag", "x": 0.55, "y": 0.5}, {"kind": "drag", "x": 0.6, "y": 0.5}, {"kind": "up", "x": 0.6, "y": 0.5}]), + ); + let a = develop(&h).lens_blur.area.expect("moved"); + assert!(a.center.x > 0.57 && (a.center.y - 0.5).abs() < 0.02, "{a:?}"); + assert!((a.rx - 0.22).abs() < 1e-9 && (a.ry - 0.22).abs() < 1e-9, "same size"); + // one drag, one undo step + exec(&mut h, "edit.undo", json!({})); + assert!(develop(&h).lens_blur.area.is_none(), "{:?}", develop(&h).lens_blur.area); + exec(&mut h, "edit.redo", json!({})); + assert!(develop(&h).lens_blur.area.is_some()); + // the preset button puts it back in the middle + click(&mut h, "lensBlurShape:round"); + assert!(develop(&h).lens_blur.area.is_none()); + // switching the blur off puts the tool down + click(&mut h, "check:lensBlur.enabled"); + assert_eq!(h.app.ui.tool, ""); + h.settle(SETTLE); +} + +/// A mask's Lens Blur is a block of its own under the mask's sliders: which side it blurs, its +/// sliders, an eye that switches it off (its controls then inert) and a header that folds it. +#[test] +fn a_masks_lens_blur_block_picks_the_side_and_switches_off() { + let mut h = detail("panel.masking"); + assert_eq!(h.request("ui.resize", json!({"width": 1200, "height": 3200}), T)["ok"], true); + exec(&mut h, "mask.add", json!({"kind": "linear", "start": [0.5, 0.2], "end": [0.5, 0.7]})); + h.settle(SETTLE); + let adjust = |h: &Headless| develop(h).masks[0].adjust; + // the block's own sliders, not one more in the list + assert!(h.app.widgets.iter().any(|(id, _)| id == "section:maskLensBlur")); + assert!(h.app.widgets.iter().any(|(id, _)| id == "slider:lens_blur_transition")); + // blurring the outside starts with a visible amount + click(&mut h, "button:maskLensBlurSide-outside"); + let a = adjust(&h); + assert!(a.lens_blur_outside && a.lens_blur == 50.0, "{a:?}"); + let r = h.request("ui.clickWidget", json!({"id": "slider:lens_blur_boost", "fx": 0.6}), T); + assert_eq!(r["ok"], true, "{r}"); + assert!(adjust(&h).lens_blur_boost > 30.0, "{:?}", adjust(&h)); + // the eye (shown over the header) switches it off and keeps the values + h.request("ui.hoverWidget", json!({"id": "section:maskLensBlur"}), T); + h.settle(SETTLE); + click(&mut h, "sectionEye:maskLensBlur"); + let off = adjust(&h); + assert!(off.lens_blur_off && off.lens_blur == 50.0 && off.lens_blur_outside, "{off:?}"); + // off: its controls don't respond + click(&mut h, "button:maskLensBlurSide-inside"); + assert_eq!(adjust(&h), off); + // on again + click(&mut h, "sectionEye:maskLensBlur"); + assert!(!adjust(&h).lens_blur_off); + // the header folds it + click(&mut h, "section:maskLensBlur"); + assert!(h.app.ui.section_folded("maskLensBlur")); + click(&mut h, "section:maskLensBlur"); + assert!(!h.app.ui.section_folded("maskLensBlur")); + h.settle(SETTLE); +} diff --git a/crates/ui-egui/tests/flatpak_updates.rs b/crates/ui-egui/tests/flatpak_updates.rs new file mode 100644 index 0000000000..1708ee68c9 --- /dev/null +++ b/crates/ui-egui/tests/flatpak_updates.rs @@ -0,0 +1,27 @@ +//! Release and source Flatpaks must allow release checks and host update helpers. +fn finish_args(manifest: &str) -> Vec<&str> { + manifest + .split("finish-args:") + .nth(1) + .unwrap_or_default() + .lines() + .take_while(|line| line.is_empty() || line.starts_with(' ') || line.starts_with('#')) + .filter_map(|line| line.trim().strip_prefix("- ")) + .map(|arg| arg.trim_matches(['\'', '"'])) + .collect() +} + +#[test] +fn flatpak_update_permissions() { + let manifests = [ + include_str!("../../../packaging/linux/flatpak/ai.storyteller.lightcraft.bundle.yml"), + include_str!("../../../packaging/linux/flatpak/ai.storyteller.lightcraft.yml"), + ]; + let args = manifests.map(finish_args); + assert_eq!(args[0], args[1], "source and release permissions must agree"); + for permissions in args { + for required in ["--share=network", "--talk-name=org.freedesktop.Flatpak"] { + assert!(permissions.contains(&required), "missing update permission: {required}"); + } + } +} diff --git a/docs/ai-masks.md b/docs/ai-masks.md index 41873cb5a6..c24ea64cb3 100644 --- a/docs/ai-masks.md +++ b/docs/ai-masks.md @@ -6,8 +6,13 @@ through Metal on macOS, on the CPU elsewhere (for now). If the Mac's GPU can't r the CPU (see the worker below). **The model is optional.** It is not part of LightCraft, and nothing requires it: without it, -Object and Describe offer to download it, every other feature (and every existing mask, see -below) works, and no render, export or command ever waits for it. +Object, Describe and Lens Blur's **Find Subject** offer to download it, every other feature (and +every existing mask, see below) works, and no render, export or command ever waits for it. + +Lens Blur reuses this same optional SAM 3 model for **Find Subject**; it does not add or download a +separate model. The automatic subject estimate used before Find Subject finishes (or when **Auto** +is selected) uses existing person mattes when available, otherwise a local saliency heuristic, so +that fallback does not require SAM 3. ## Using them @@ -74,7 +79,9 @@ ssh -N -T -o ExitOnForwardFailure=yes -L 127.0.0.1:8793:127.0.0.1:8793 user@your LIGHTCRAFT_SAM3_REMOTE=127.0.0.1:8793 lightcraft ``` -Object clicks, Describe prompts, and detail refinement use the remote worker. Only the +Object clicks, Describe prompts, detail refinement and Lens Blur's Find Subject (people and animals, +from the camera's focus point when the file records it) use the remote worker; a worker older than the +app rejects a request it doesn't know with an error, so update both together. Only the rendered model input (long edge at most 1008 pixels) and prompts cross the tunnel. The worker caches the current photo; detail crops do not replace that cache. Returned masks stay in the local library, so later adjustments and exports work without the connection. @@ -96,9 +103,9 @@ not RAW development, denoising, or upscaling. The model licence below still appl ### In the app (recommended) -The first time you pick Object or Describe without the model, LightCraft asks: +The first time you pick Object, Describe or Lens Blur's Find Subject without the model, LightCraft asks: -> **Download the SAM 3 model?** Object and Describe masks use SAM 3, Meta's segmentation model. +> **Download the SAM 3 model?** Object, Describe and Lens Blur's Find Subject use SAM 3, Meta's segmentation model. > It isn't part of LightCraft, and everything else works without it. Download it once (about > 3.4 GB) · *folder*. Licence: SAM License (Meta) — Meta's terms, not LightCraft's. Downloading > it means accepting them. [Read the SAM License] — **Not Now** / **Download** diff --git a/docs/contributors.md b/docs/contributors.md index faf47dc716..e8b23d02f2 100644 --- a/docs/contributors.md +++ b/docs/contributors.md @@ -8,7 +8,8 @@ the AI models named in `Co-Authored-By` trailers. - Two files, both **compiled into the binary** by the UI crate's `build.rs` (nothing is read from disk or the network at run time; the web build has them too): - [`contributors/contributors.json`](../contributors/contributors.json): commit stats per GitHub - username. + username. Release builds regenerate it for the release commit + (`.github/workflows/release.yml`, `credits` job); the copy in the repository is not updated. - [`contributors/people.toml`](../contributors/people.toml): the names people chose for themselves. **Each contributor adds only their own entry**, in their own PR. - Each contributor gets one line: GitHub username (always), display name (opt-in, or their synced diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 6c5f74ae22..7f82adf2e9 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -19,7 +19,7 @@ import and export formats. Ours come from `crates/codecs/src/sniff.rs`, `crates/ | TIFF | ✅ 8/16/32-bit | ✅ 8/16-bit, LZW/ZIP | ✅ 1–64-bit integer, 16–64-bit float, bilevel, CCITT fax, WhiteIsZero | ✅ 8/16-bit, 32-bit float linear, none / LZW / ZIP | `crates/tiff`, `crates/codecs/src/tiff_codec.rs` | | PNG | ✅ | ✅ | ✅ | ✅ | | | PSD / PSB | ✅ (maximized-compatibility composite) | ✅ (Classic: PSD export, edit-in round trip) | ✅ merged composite | ⬜ (external-editor round trip writes 16-bit TIFF) | `crates/codecs/src/psd.rs` | -| HEIC / HEIF | ✅ | ⬜ (Classic) / HDR only | ✅ in every release build (`crates/heif`, heic-rs) | ⬜ | `heif` feature on by packaging scripts | +| HEIC / HEIF | ✅ | ⬜ (Classic) / HDR only | ✅ in every release build (`craft-heif` from craft-libs, heic-rs) | ⬜ | `heif` feature on by packaging scripts | | AVIF | ✅ | ✅ (HDR) | ⬜ recognised, not decoded (no permissive pure-Rust AV1 decoder) | ✅ 8/10-bit, PQ HDR | | | JPEG XL | ✅ | ✅ | ✅ (`jxl-oxide`) incl. DNG 1.7 JXL tiles | ✅ lossy / lossless, 8/16-bit, PQ HDR (`jixel`, native only; ⬜ web) | `crates/engine/src/tests/export.rs` | | WebP | not in its document types | ⬜ | ✅ | ✅ | beyond target | @@ -27,7 +27,7 @@ import and export formats. Ours come from `crates/codecs/src/sniff.rs`, `crates/ | DNG (from raw) | ✅ | ✅ incl. lossy, embedded original | ✅ | ✅ uncompressed, LJ92, Deflate or lossy (JPEG tiles, optional long edge); ⬜ embedded original | `crates/raw/src/dngwrite.rs` | | Render to DNG (15.5) | — | ✅ | — | 🟡 export format "Rendered DNG" (linear float DNG, edit baked in); not yet added to the catalog | `crates/raw/src/dngwrite.rs` (`write_rendered_dng`) | | Original + sidecar | — | ✅ | — | ✅ | | -| HDR gain map JPEG (ISO 21496-1) | ✅ | ✅ | ✅ (gain map read) | ✅ | `crates/codecs/src/gainmap.rs` | +| HDR gain map JPEG (ISO 21496-1) | ✅ | ✅ | ✅ (gain map read) | ✅ | `crates/codecs/src/gainmap.rs` (JPEG layout), `craft-gainmap` (metadata, compute, apply) | | PQ / HLG HDR | ✅ | ✅ AVIF, JXL, TIFF | — | 🟡 PQ AVIF, PQ JPEG XL, float TIFF; no HLG | | | Contact sheet / print PDF | — | ✅ (Print module: PDF / JPEG / printer) | — | 🟡 paginated contact-sheet PDF (A4 / Letter, 150 dpi, sRGB) | `crates/engine/src/contact_sheet.rs` | | Video (MP4, MOV, AVCHD…) | ✅ playback, trim, edit | ✅ export | 🟡 catalogued (kind, duration) | ⬜ | see video gap | diff --git a/docs/gaps.md b/docs/gaps.md index f11acd4747..72ff6d72f8 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -27,7 +27,7 @@ Hours are Opus 5.5 agent wall-clock hours (see [target-app-parity.md](target-app | 10 B | **Per-model verification thin, plus cheap per-body decode fixes**: 557 of 1,446 Adobe models verified (39%), ~86% of photographers on a verified body; Nikon 5 bodies verified, Canon 34, Leica 14 (the Q3 by a recorded check of 38 private DNGs). Cheap fixes: Z 6II packed 14-bit NEF (#769, PR #671), Sony A100 regression (#702); F828 SRF now discovered and imported as preview-only, sensor decoding still missing, DSLR-A200…A390 / A850 12-bit ARW (#535) | measured: [raw/coverage.tsv](raw/coverage.tsv) | per-body bugs (levels, crops, WB) surface only from users | 25–45 | [raw-parity](raw-parity.md#by-maker-measured) | | 11 | **Enhance: Raw Details, Super Resolution; trained denoise weights and X-Trans** | ENH rows 0%; denoise Bayer only; the built-in denoiser is classical (block-matching collaborative filtering), RawNIND weights GPL and opt-in | Fujifilm users get no AI denoise; very noisy files gain less than from a trained model | 30–60 + model decision | [raw-parity](raw-parity.md#demosaic-levels-crops-and-lens-metadata) | | 12 | **Content-aware / generative remove, detect distractions, reflections removal** | REM rows 75%; patch synthesis missing | cleanup work goes back to Photoshop | 25–45 (+ generative model) | [target-app-parity](target-app-parity.md#ai) | -| 13 | **Lightroom catalog migration is one-way and approximate**: unsupported Adobe profiles / AI settings archived, history kept as data; main-file pages read on demand up to 16 GiB, with dense-table and WAL bounds retained (#798); offline record counts and destination are now shown in a lasting import result; catalog VIDEO records match the Videos filter, with older classification repaired on reimport; before an import the app asks whether the catalog merges into the open library or goes into a new library (opt-out in Settings, #2672) | lightroom-catalog-import.md | switching users lose some looks | 8–12 | [file-format-parity](file-format-parity.md) | +| 13 | **Lightroom catalog migration is one-way and approximate**: unsupported Adobe profiles / AI settings archived, history kept as data; main-file pages read on demand up to 16 GiB, with dense-table and WAL bounds retained (#798); offline record counts and destination are now shown in a lasting import result; catalog VIDEO records match the Videos filter, with older classification repaired on reimport; before an import the app asks whether the catalog merges into the open library or goes into a new library (opt-out in Settings, #2672); Lightroom's backup zips import directly (#2592) | lightroom-catalog-import.md | switching users lose some looks | 8–12 | [file-format-parity](file-format-parity.md) | | 14 | **HDR display** (EDR / Windows HDR); the headroom limit and SDR rendition from `crs` XMP (the HDR switch, `HDREditMode`, is read). HLG AVIF export done; the display's SDR/HDR decision is tested, blocked on eframe's 8-bit surface | HDR rows 70%; LR-VIEW-HDR-DISPLAY ⬜ | HDR editing works blind on HDR monitors | 12–25 | [hardware-parity](hardware-parity.md) | | 15 | **Print module**: identity plate / watermark / photo info overlays, Cut Guides for the packages, Custom Package Auto Layout, printer-driver Page Setup and direct printing, JPEG file output (the Print dialog writes a colour-managed PDF) | LRC-PRINT rows | Classic users who print | 15–25 | [target-app-parity](target-app-parity.md#features) | | 16 | **Map view, Book, Slideshow module, publish services** | LRC-MAP / BOOK / SS rows ⬜ | Classic users; publish services matter for Flickr / SmugMug workflows | 60–100 | [target-app-parity](target-app-parity.md#features) | @@ -90,6 +90,7 @@ Beta-blocking gaps (B) total **~230–430 h**; with the stability and platform w | Date | Change | Summary | |---|---|---| +| 2026-11-01 | minor | Gap 13: Lightroom catalog backup zips import directly, and a backup of an imported catalog adds no collection twice (#2592); the gap stays open | | 2026-11-07 | correction | Compressed ORF sensor support withdrawn pending provenance review; embedded preview fallback remains. Coverage regenerated without the removed decoder: 557 verified, 665 unverified, 187 preview-only, 37 unsupported; 94% use-weighted coverage. | | 2026-11-04 | minor | Compressed ORF support was introduced in #3775; subsequently withdrawn pending provenance review. Earlier clean-room and verification claims are withdrawn; compressed files use embedded previews when available. See [ORF status](orf-compression.md). | | 2026-11-05 | minor | Gap 3: process V5 rolls a raw's tone curve off into white in stops, so raising Exposure keeps the highlights the sensor recorded (#1632); the gap stays open (no Lightroom comparison) and hours are unchanged | diff --git a/docs/gpu-pipeline.md b/docs/gpu-pipeline.md index 0e9816b7bb..2d68b51999 100644 --- a/docs/gpu-pipeline.md +++ b/docs/gpu-pipeline.md @@ -310,7 +310,7 @@ real failure still stops the whole process. Such a test asserts on its own rende `crates/gpu/tests/it/equivalence.rs` renders the same settings on both and compares 8-bit sRGB: bounds **mean |Δ| < 0.5 LSB and max |Δ| ≤ 3 LSB** per channel. Measured (Apple M4 Pro, Metal): **max 1 LSB, mean ≤ 0.0003 LSB** for every case — 26 settings cases (all tone/colour/effects tools, -3 vignette styles, grain, curves, grading, B&W, WB, NR, crop/straighten/flip, lens + perspective, +3 vignette styles, grain (value noise and crystal layers), curves, grading, B&W, WB, NR, crop/straighten/flip, lens + perspective, three mask sets incl. CPU-evaluated shapes, spots + defringe), all 8 orientations ± crop, embedded DNG lens data, display-referred sources, draft quality, full-size renders; the 24 MP export in `render_bench` also differs by max 1 LSB. Cached (slider-drag) GPU renders are bit-identical to diff --git a/docs/grain.md b/docs/grain.md new file mode 100644 index 0000000000..5e66405c6f --- /dev/null +++ b/docs/grain.md @@ -0,0 +1,147 @@ +# Film grain + +The Effects panel's Grain (`ctl:grain.amount`, `ctl:grain.size`, `ctl:grain.roughness`; XMP +`crs:GrainAmount`, `crs:GrainSize`, `crs:GrainFrequency`) draws grain from a simulated emulsion of +crystal layers: `crates/pipeline/src/crystal.rs` on the CPU, the same tables read by the GPU +kernel (`crates/gpu/src/wgsl/finish.wgsl`). + +This is the grain of **process V7** and later ([process-versions.md](process-versions.md)). Photos on +V1 to V6 keep the two-octave value noise they were edited with (`finish::ValueGrain`), so opening an +older catalog never changes a photo's grain; Update to Current Process moves a photo to the crystal +grain. The sliders, their ranges and their XMP names are unchanged. + +## Model + +After Aurélien Pierre, [*Stochastic photographic grain synthesis from crystallographic structure +simulation*](https://eng.aurelienpierre.com/2023/07/stochastic-photographic-grain-synthesis-from-crystallographic-structure-simulation/) +(2023). Implemented from the article's description of the method; no code was taken from it or from +any other implementation. + +- **Emulsion.** A stack of layers. Each layer has one crystal shape: a regular polygon of + `round(4.5 + z)` vertices (3–8), a random rotation, and a width (circumscribed diameter) drawn from a + log-normal distribution. The layers' widths are the quantiles of that distribution, dealt out in an + order the seed shuffles, so every seed has the same mix of sizes (the same grain strength and + width) in a different arrangement. +- **Crystals.** Each layer is a Boolean model: Poisson seeds grown into crystals, at the density + `λ = −ln(1 − F)/A` (crystal area `A`) that leaves a fraction `F = ½` of the layer covered. +- **Capture.** A point's grain is the number of layers covering it less the mean: many independent + layers of different crystal sizes add up to dense, soft, cloudy grain with no hard particle edges. +- **Print.** The light a crystal captures is proportional to the light falling on it, and a print + shows less grain towards paper white: the grain is `Y·(1 − Y)·g` in linear light (`Y` the pixel's + luminance), zero at black and at white. It is applied in the display-encoded domain, to first + order: each channel `v` moves by `k·lift(v)·(1 − Y)·g`, where `lift(v) = Y·dv/dY` of the sRGB curve. + In that domain the grain is symmetric, so it neither darkens shadows (the article's model in + linear light does, through the concave encoding) nor greys blown highlights, and it keeps hue. +- **Colour:** monochrome, as in Lightroom Classic. + +## Resolution independence + +The emulsion is drawn in frame coordinates at twice the resolution of a 6000 px export (a tile pixel +is half a reference pixel), on two periodic tiles (tori) of 640 and 567 reference pixels; layers +alternate between them. The tiles are stored as summed-area tables, so each output pixel takes the +**exact mean of the emulsion under its footprint** (16 table reads): a fit-to-window preview shows +the export's grain box-downscaled to its size, and an export larger than 6000 px (a 45 MP camera) +still shows crystals rather than enlarged pixels. The tori share no small factor: the two patterns +only line up again after 362,880 reference pixels, so the grain has no visible period. + +In a rotated frame (a straightened or rotated crop) the footprint is approximated: each pixel takes +the mean over an axis-aligned square of its size centred on it, not over the rotated pixel (summed +area tables only give axis-aligned boxes). The grain strength is the same; the exact match with a +box-downscaled export holds for unrotated frames in any orientation. + +Measured on the comparison renders (four photos, six regions each, 1500 px preview against the +6000 px export box-downscaled to 1500 px): preview / export grain strength **1.00–1.04** at the +default, finest and coarsest settings. `crystal::tests::small_render_is_the_downscaled_reference` +checks it exactly at 1/2, 1/4 and 1/8 size; `tests_grain::small_render_matches_the_downscaled_large_render` +through the whole pipeline. + +## Mapping and calibration + +Amount, Size and Roughness keep Lightroom Classic's meaning: strength and width at mid grey match its +exports (the measurements of open PR #1060, `docs/grain.md` on that branch). + +| Quantity | Value | +|---|---| +| strength (levels of 255 at mid grey, 6000 px) | `Amount/100 · exp(2.447 + 1.386·exp(−Size/19.88)) · (1 − 0.084d + 1.6384d²)`, `d = Roughness/100 − ½`: 8.55 at 50 / 25 / 50 | +| median crystal width (reference px) | by Size: 0.55, 1.39, 2.95, 5.17, 8.78, 12.38 at 0, 10, 25, 50, 75, 100; × 0.734 / 1 / 1.422 at Roughness 0 / 50 / 100 (`fit_widths`) | +| width spread (log std) | `hypot(0.1 + 0.6·Roughness/100, 0.45)` | +| layers | by Size: 16, 31, 54, 70 at 0, 25, 50, 100 | + +- **Amount only scales the strength**: the amplitude is measured from the drawn emulsion itself (its + variance at the reference resolution), so every setting and seed has exactly its target strength, + and dragging Amount draws nothing new. In the article the layer count sets the strength; here it + sets only how dense the texture is (coarser grain averages more layers, so it stays a soft clump + texture). +- **Texture.** The width spread was chosen so the anti-aliased crystals have the texture of the + first, whole-pixel prototype at every scale, not just its width: autocorrelation at 1–5 px and the + strength kept by box downscales of 2–16 match it within a few hundredths + (`texture_report`). +- **Size 0** is as fine as the model gets: crystals narrower than a pixel, uncorrelated pixels + (width 0.51 px; Lightroom Classic's 0.43 needs anti-correlated grain). + +Measured (chart, 6000 px, 8-bit, against the same build without grain): + +| Amount / Size / Roughness | Strength at grey 20 · 128 · 245 | Width | Lightroom Classic at 128 | +|---|---|---|---| +| 25 / 25 / 50 | 1.66 · 4.29 · 1.67 | 1.11 | 4.29, 1.13 | +| 50 / 25 / 50 | 3.27 · 8.57 · 3.30 | 1.12 | 8.55, 1.13 | +| 100 / 25 / 50 | 6.53 · 17.14 · 6.36 | 1.12 | 17.01, 1.14 | +| 50 / 0 / 50 | 8.80 · 23.10 · 8.55 | 0.53 | 23.08, 0.43 | +| 50 / 50 / 50 | 2.46 · 6.42 · 2.43 | 1.70 | — | +| 50 / 100 / 50 | 2.24 · 5.83 · 2.19 | 3.39 | 5.83, 3.38 | +| 50 / 25 / 0 | 4.73 · 12.44 · 4.67 | 0.85 | 12.42, 0.86 | +| 50 / 25 / 100 | 4.46 · 11.75 · 4.37 | 1.53 | 11.68, 1.54 | + +Unlike Lightroom Classic's, the grain fades towards black and white (the print model). Mean +brightness change on 24 photo regions: within ±0.1 level on every dark and mid-tone region at every +setting (#1060: up to +3.8 on dark fabric at 50 / 0 / 50); up to −0.55 on sunlit skin at the +strongest settings, where grain clips at white; blown highlights stay clean (−0.01 on a blown +curtain; #1060: −9.3). Fine detail under the grain is kept (detail gain +0.96–1.00). + +## Performance + +- **Drawing the emulsion** (per Size, Roughness and seed; cached, so Amount drags and re-renders + draw nothing): every crystal is stamped from a per-layer table of its rasterisation at 16 × 16 + sub-pixel phases, as one 64-bit mask per row (a fixed number of rows per layer, those outside a + band landing in scratch rows around it, so a crystal costs no branch per row), layers are counted + in bit-sliced counters, and the tiles are drawn in parallel bands. 7–15 ms on an M2 Max (12 + threads), 15–33 ms on a Ryzen 5 7545U (6 cores, 12 threads; 10–20 % less than before the row + masks) at any setting (`tile_timing_report`). +- **Drags.** A draft render (a slider drag) doesn't wait for a tile it hasn't got if one for the + photo's seed is cached: the last one drawn stands in while the new one is drawn in the background, + on a pool of half the cores so the drag's renders don't queue behind it, and the next render takes + it. With none cached for the seed there is nothing to stand in, so the draft waits for a draw + already running or draws the tile itself. The full-quality render when the drag ends waits for the + exact tile. A Size drag on the Ryzen with its + Radeon 740M (GPU, 2.5 MP preview): 13–14 ms a frame instead of 62–66 ms. (On wasm, without + threads, drafts wait.) +- **Drag benchmark** (`FRAMES=100 cargo run --release -p lightcraft-gpu --example grain_drag_bench`: GPU + draft renders of the 1936 × 1290 demo photo, median ms per frame of 100; the example has no + dependency on this model, so it can be copied unchanged into another branch). Ryzen 5 7545U / + Radeon 740M: + + | Build | No grain (Exposure drag) | Amount drag | Size drag | + |---|---|---|---| + | main (value noise) | 8–12 | 10–13 | 10–13 | + | #1060 (particles) | 8 | 11–14 | 151–179 (17–312) | + | this model | 8–11 | 14–15 | 13 | + + M2 Max (Metal), same benchmark, the medians of three runs: + + | Build | No grain (Exposure drag) | Amount drag | Size drag | + |---|---|---|---| + | main (value noise) | 3.2–3.4 | 2.9–3.0 | 2.9 | + | #1060 (particles) | 3.3 | 3.0–3.1 | 35.6–36.0 | + | this model | 3.2–3.3 | 3.4–3.5 | 5.9–6.0 | + +- **Per pixel:** two box sums, on the CPU and on the GPU. The tables (12 MB) stay on the GPU + between renders, for the last tile used (`Gpu::grain_tables`), and are copied after the finish + kernel's other tables in its `aux` buffer on the GPU: an Amount drag uploads nothing. On the + Radeon 740M an Amount drag frame (2.5 MP) went from 16.9 to 11–15 ms (no grain: 8 ms). +- **24 MP NEF → PNG** (`lightcraft-cli render`, M2 Max, median of three): no grain 1.06 s, grain + 50 / 25 / 50 1.05 s on the GPU; 1.28 s and 1.43 s CPU-only (the finish stage 0.26 → 0.42 s). + Grain costs main ≈ 0 on the GPU and +0.08 s CPU-only, #1060 +0.29 s and +1.71 s. +- **24 MP NEF → PNG on the Ryzen 5 7545U / Radeon 740M** (median of three): no grain 2.07 s, grain + 50 / 25 / 50 2.10 s on the GPU; 2.72 s and 2.88 s CPU-only. Grain costs main +0.03 s on the GPU + and +0.10 s CPU-only, #1060 +1.81 s and +3.54 s. diff --git a/docs/lightroom-catalog-import.md b/docs/lightroom-catalog-import.md index 80169dc859..d2316d17bc 100644 --- a/docs/lightroom-catalog-import.md +++ b/docs/lightroom-catalog-import.md @@ -42,6 +42,9 @@ being the choice the window opens with) or merges when the setting says so. A ca refused before a new library is made. The engine command `library.importLightroom` always imports into the library open now. +A Lightroom Classic catalog backup is imported the same way, as the `.zip` Lightroom writes +(`Backups/YYYY-MM-DD HHMM/.zip`; see *Catalog backups* below). + Equivalent commands: ```text @@ -164,6 +167,39 @@ photo and its virtual copies out of the library. To import originals on a share, from that share. A path that is not a path on this computer (a Windows drive path read on macOS or Linux) is not looked at either and stays catalogued as missing. +## Catalog backups + +Lightroom Classic backs a catalog up as `/Backups/YYYY-MM-DD HHMM/.zip`: one `.lrcat` (plus, +since Classic 12, a small `.lrcat-data/` folder the import does not need). Someone who has removed Lightroom +and its catalog may have nothing else, so `library.importLightroom` / `library.inspectLightroom` and File ▸ Import +Lightroom Catalog… take the zip directly (issue #2592, `crates/engine/src/lightroom_backup.rs`): + +- The `.lrcat` entry is inflated by streaming (stored or deflated, ZIP64 included) into a private folder, + `Interop/lc-lrbackup--