Merge branch 'main' into sam3-gpu-4-detr

This commit is contained in:
FredApps authored and GitHub committed 2026-11-18 16:23:49 +07:00
commit 35e245600a
53 files changed
+4083 -2148

No files matched your search

+4
View File
@@ -229,3 +229,7 @@ evaluates it (`.github/workflows/nix.yml`), so the build may lag behind the work
- Don't add or change CI in a feature or fix PR. If the existing workflows can't build or test a change, link an existing craft-common-actions issue or PR, or open a new issue, or a PR only for a small fix that is safe for every app and does not increase CI time.
- `ci.yml` is the Linux gate, on every pull request. `ci-windows.yml` adds a Windows leg (clippy and tests of the crates with `cfg(windows)` code) when a pull request changes the paths it lists; add the `full-ci` label to run it on any other pull request. When you add `cfg(windows)` code in a new file, add the file to its `paths` in a CI PR.
- Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it.
## Shared libraries
Before adding or changing file-format, codec, protocol, or reusable UI code, check `storytold/craft-libs` and its open PRs first; reuse or extend the shared library rather than duplicating it locally, keep app-specific policy and model mapping in the app, and preserve behavior, security/resource limits, attribution, and regression coverage during cutovers.
Generated
+157 -37
View File
@@ -1190,7 +1190,16 @@ dependencies = [
[[package]]
name = "craft-cms"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
source = "git+https://github.com/storytold/craft-libs?rev=7f30d664695cd5ce14fc30659d36c3e32f234247#7f30d664695cd5ce14fc30659d36c3e32f234247"
dependencies = [
"md-5",
"rayon",
]
[[package]]
name = "craft-cms"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=eeb134def2daa7e03512b4946bd59d6b419b9484#eeb134def2daa7e03512b4946bd59d6b419b9484"
dependencies = [
"md-5",
"rayon",
@@ -1204,7 +1213,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=f0f4758c01581d79577b9b
[[package]]
name = "craft-crw"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
dependencies = [
"thiserror 2.0.21",
]
@@ -1239,7 +1248,7 @@ dependencies = [
[[package]]
name = "craft-heif"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93"
source = "git+https://github.com/storytold/craft-libs?rev=4ffa307f1a0a6fbd8897318cc719440ab06e9c63#4ffa307f1a0a6fbd8897318cc719440ab06e9c63"
dependencies = [
"heic-rs",
"rayon",
@@ -1271,10 +1280,41 @@ dependencies = [
[[package]]
name = "craft-image-codecs"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f#478c50560d3c6fad3d613af2a30b35c3bc9f4c0f"
source = "git+https://github.com/storytold/craft-libs?rev=5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a#5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a"
dependencies = [
"craft-jpeg",
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f)",
"avif-serialize",
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a)",
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a)",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a)",
"crc32fast",
"exr",
"flate2",
"half",
"hayro-jpeg2000",
"image",
"image-webp",
"jpeg-encoder 0.6.1",
"png",
"rav1e",
"ravif",
"rayon",
"serde_json",
"thiserror 2.0.21",
"tiff",
"zune-core",
"zune-jpeg",
"zune-jpegxl",
]
[[package]]
name = "craft-image-codecs"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
dependencies = [
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"craft-video 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"crc32fast",
"exr",
"flate2",
@@ -1302,24 +1342,53 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-jpeg"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a#5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-jpeg"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-jxl"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=b77af389f30bb10de10d8436ca9b39fe94e3bb67#b77af389f30bb10de10d8436ca9b39fe94e3bb67"
source = "git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f#478c50560d3c6fad3d613af2a30b35c3bc9f4c0f"
dependencies = [
"jixel",
"jxl-oxide",
"thiserror 2.0.21",
]
[[package]]
name = "craft-media"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=5c91598ccc86537a779f438d520fa6406c26efe6#5c91598ccc86537a779f438d520fa6406c26efe6"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-media"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
[[package]]
name = "craft-metadata"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f#478c50560d3c6fad3d613af2a30b35c3bc9f4c0f"
source = "git+https://github.com/storytold/craft-libs?rev=5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a#5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a"
dependencies = [
"brotli-decompressor 6.0.1",
"chrono",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f)",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a)",
"quick-xml 0.41.0",
"serde",
]
@@ -1327,11 +1396,11 @@ dependencies = [
[[package]]
name = "craft-metadata"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
dependencies = [
"brotli-decompressor 6.0.1",
"chrono",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"quick-xml 0.41.0",
"serde",
]
@@ -1346,10 +1415,27 @@ dependencies = [
"encoding_rs",
]
[[package]]
name = "craft-pdf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=0fd30aeb3e02207d9d4c7c601540d9da45abf8de#0fd30aeb3e02207d9d4c7c601540d9da45abf8de"
dependencies = [
"flate2",
"roxmltree",
]
[[package]]
name = "craft-plist"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=bea6ed1ff031b81df2d64be470485c1ca808847e#bea6ed1ff031b81df2d64be470485c1ca808847e"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-psd"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=9dcbd90c7d16e1a668b3afa057e940e2db328b93#9dcbd90c7d16e1a668b3afa057e940e2db328b93"
source = "git+https://github.com/storytold/craft-libs?rev=16d838105b07965832bb43132880176f299634d5#16d838105b07965832bb43132880176f299634d5"
dependencies = [
"encoding_rs",
"flate2",
@@ -1360,12 +1446,12 @@ dependencies = [
[[package]]
name = "craft-raw"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
dependencies = [
"chrono",
"craft-crw",
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"jxl-oxide",
"miniz_oxide 0.9.1",
"rayon",
@@ -1378,7 +1464,7 @@ dependencies = [
[[package]]
name = "craft-tiff"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f#478c50560d3c6fad3d613af2a30b35c3bc9f4c0f"
source = "git+https://github.com/storytold/craft-libs?rev=5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a#5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a"
dependencies = [
"serde",
"thiserror 2.0.21",
@@ -1387,7 +1473,7 @@ dependencies = [
[[package]]
name = "craft-tiff"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2#94777dcf7f8962476e96b049d08d22e25b9330c2"
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
dependencies = [
"serde",
"thiserror 2.0.21",
@@ -1441,6 +1527,26 @@ dependencies = [
"zip 9.1.3",
]
[[package]]
name = "craft-video"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=5c91598ccc86537a779f438d520fa6406c26efe6#5c91598ccc86537a779f438d520fa6406c26efe6"
dependencies = [
"craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5c91598ccc86537a779f438d520fa6406c26efe6)",
"rav1e",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "craft-video"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845#c28356d400aed7575b04ec1551b4665107cfe845"
dependencies = [
"craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"thiserror 2.0.21",
]
[[package]]
name = "craft-window"
version = "0.1.0"
@@ -1455,9 +1561,10 @@ dependencies = [
[[package]]
name = "craft-xml"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=f0f4758c01581d79577b9bf5046c06f4e77b1f18#f0f4758c01581d79577b9bf5046c06f4e77b1f18"
source = "git+https://github.com/storytold/craft-libs?rev=2d4b8b8ecd79f87c847961901c40305e7f448d6f#2d4b8b8ecd79f87c847961901c40305e7f448d6f"
dependencies = [
"quick-xml 0.41.0",
"serde",
"thiserror 2.0.21",
]
@@ -3333,6 +3440,8 @@ dependencies = [
"num-traits",
"png",
"qoi",
"ravif",
"rgb",
"tiff",
"zune-core",
"zune-jpeg",
@@ -3879,27 +3988,26 @@ dependencies = [
name = "lightcraft-codecs"
version = "0.21.1"
dependencies = [
"avif-serialize",
"bytemuck",
"craft-cms",
"craft-cms 0.1.0 (git+https://github.com/storytold/craft-libs?rev=7f30d664695cd5ce14fc30659d36c3e32f234247)",
"craft-gainmap",
"craft-gif",
"craft-heif",
"craft-image-codecs",
"craft-jpeg",
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a)",
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=478c50560d3c6fad3d613af2a30b35c3bc9f4c0f)",
"craft-jxl",
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"craft-psd",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"craft-video 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5c91598ccc86537a779f438d520fa6406c26efe6)",
"dhat",
"jpeg-encoder 0.7.1",
"jxl-oxide",
"lightcraft-color",
"lightcraft-geom",
"lightcraft-raster",
"lightcraft-testkit",
"proptest",
"rav1e",
"ravif",
"rayon",
"serde",
"thiserror 2.0.21",
@@ -3968,12 +4076,14 @@ dependencies = [
"chrono",
"craft-docpath",
"craft-heif",
"craft-jxl",
"craft-mime",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
"craft-pdf",
"craft-plist",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"crc32fast",
"fs4",
"harfrust 0.13.3",
"jxl-oxide",
"libc",
"lightcraft-catalog",
"lightcraft-codecs",
@@ -4129,10 +4239,10 @@ version = "0.21.1"
dependencies = [
"chrono",
"craft-heif",
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"craft-psd",
"craft-raw",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"lightcraft-geom",
"proptest",
"serde",
@@ -4143,7 +4253,8 @@ dependencies = [
name = "lightcraft-pipeline"
version = "0.21.1"
dependencies = [
"craft-image-codecs",
"craft-cms 0.1.0 (git+https://github.com/storytold/craft-libs?rev=eeb134def2daa7e03512b4946bd59d6b419b9484)",
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"dhat",
"lightcraft-color",
"lightcraft-develop",
@@ -4162,7 +4273,7 @@ dependencies = [
name = "lightcraft-preview"
version = "0.21.1"
dependencies = [
"craft-image-codecs",
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"lightcraft-raster",
"log",
"siphasher",
@@ -4185,10 +4296,12 @@ name = "lightcraft-raw"
version = "0.21.1"
dependencies = [
"chrono",
"craft-image-codecs",
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"craft-jxl",
"craft-raw",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=94777dcf7f8962476e96b049d08d22e25b9330c2)",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"dhat",
"jixel",
"lightcraft-color",
"lightcraft-geom",
"lightcraft-meta",
@@ -4200,15 +4313,13 @@ dependencies = [
"serde",
"sha2",
"thiserror 2.0.21",
"zune-core",
"zune-jpegxl",
]
[[package]]
name = "lightcraft-scenes"
version = "0.21.1"
dependencies = [
"craft-image-codecs",
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c28356d400aed7575b04ec1551b4665107cfe845)",
"lightcraft-color",
"lightcraft-raster",
"rayon",
@@ -6098,6 +6209,15 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "roxmltree"
version = "0.21.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9da7f59328b09405b181fee1018925fad50f157afb43c0c0f5c522c256dd5907"
dependencies = [
"memchr",
]
[[package]]
name = "rsa"
version = "0.9.10"
+17 -19
View File
@@ -10,26 +10,33 @@ rust-version = "1.95"
repository = "https://github.com/storytold/lightcraft"
[workspace.dependencies]
craft-cms-profile = { package = "craft-cms", git = "https://github.com/storytold/craft-libs", rev = "eeb134def2daa7e03512b4946bd59d6b419b9484" }
craft-plist = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "bea6ed1ff031b81df2d64be470485c1ca808847e" }
craft-pdf-print = { package = "craft-pdf", git = "https://github.com/storytold/craft-libs", rev = "0fd30aeb3e02207d9d4c7c601540d9da45abf8de", default-features = false, features = ["write-raster"] }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", features = ["serde"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7" }
# Canon CRW decoder shared with the other Craft apps
craft-crw = { git = "https://github.com/storytold/craft-libs", rev = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
craft-cms = { git = "https://github.com/storytold/craft-libs", rev = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
craft-crw = { git = "https://github.com/storytold/craft-libs", rev = "2d4b8b8ecd79f87c847961901c40305e7f448d6f" }
craft-cms = { git = "https://github.com/storytold/craft-libs", rev = "7f30d664695cd5ce14fc30659d36c3e32f234247" }
craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "f0f4758c01581d79577b9bf5046c06f4e77b1f18", version = "0.1.0" }
craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "b77af389f30bb10de10d8436ca9b39fe94e3bb67", features = ["frames"] }
craft-jxl = { git = "https://github.com/storytold/craft-libs", rev = "b77af389f30bb10de10d8436ca9b39fe94e3bb67", default-features = false, features = ["decode", "encode"] }
craft-jxl = { git = "https://github.com/storytold/craft-libs", rev = "478c50560d3c6fad3d613af2a30b35c3bc9f4c0f", default-features = false, features = ["decode", "encode"] }
jixel = "=0.3.4"
# Photo metadata: EXIF, IPTC-IIM, XMP packets and GPX track logs (craft-libs#949)
craft-metadata = { git = "https://github.com/storytold/craft-libs", rev = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
craft-metadata = { git = "https://github.com/storytold/craft-libs", rev = "c28356d400aed7575b04ec1551b4665107cfe845" }
craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "478c50560d3c6fad3d613af2a30b35c3bc9f4c0f" }
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "478c50560d3c6fad3d613af2a30b35c3bc9f4c0f" }
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "f0f4758c01581d79577b9bf5046c06f4e77b1f18" }
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "c28356d400aed7575b04ec1551b4665107cfe845" }
craft-video = { git = "https://github.com/storytold/craft-libs", rev = "5c91598ccc86537a779f438d520fa6406c26efe6", features = ["av1", "av1-large-encode", "threads"] }
# Isolate AVIF threading from the existing PNG/TIFF/WebP shared revision.
craft-avif-encode = { package = "craft-image-codecs", git = "https://github.com/storytold/craft-libs", rev = "5ac7c9664c8d54aaf69d1bb826e187686c1a4e1a", default-features = false, features = ["avif", "threads"] }
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "2d4b8b8ecd79f87c847961901c40305e7f448d6f" }
# 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 = "6e03f65bf45181db77ba6229716e6624db9eb25b", features = ["egui"] }
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", features = ["egui"] }
# HDR gain maps: ISO 21496-1 and hdrgm metadata, compute and apply (moved from crates/codecs; craft-libs#904).
craft-gainmap = { git = "https://github.com/storytold/craft-libs", rev = "9dcbd90c7d16e1a668b3afa057e940e2db328b93" }
# PSD/PSB: the header, colour mode data and image resources (craft-libs#885).
craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "9dcbd90c7d16e1a668b3afa057e940e2db328b93" }
craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "16d838105b07965832bb43132880176f299634d5" }
jpeg-encoder = { version = "0.7", default-features = false, features = ["std"] }
# internal
lightcraft-denoise-core = { path = "crates/denoise-core" }
@@ -41,10 +48,8 @@ lightcraft-wincolor = { path = "crates/wincolor" }
lightcraft-color = { path = "crates/color" }
lightcraft-raster = { path = "crates/raster" }
# TIFF/IFD: the shared reader and writer (moved from crates/tiff; craft-libs#783).
craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
# Camera raw files, the format layer: every maker but CR3, ARW, RAF, RW2 and ORF (craft-libs#862 and the maker PRs).
# On the same commit as craft-tiff: its errors and IFDs are shared.
craft-raw = { git = "https://github.com/storytold/craft-libs", rev = "94777dcf7f8962476e96b049d08d22e25b9330c2" }
craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "c28356d400aed7575b04ec1551b4665107cfe845" }
craft-raw = { git = "https://github.com/storytold/craft-libs", rev = "c28356d400aed7575b04ec1551b4665107cfe845" }
lightcraft-raw = { path = "crates/raw" }
lightcraft-codecs = { path = "crates/codecs" }
lightcraft-meta = { path = "crates/meta" }
@@ -63,7 +68,7 @@ lightcraft-ui-egui = { path = "crates/ui-egui" }
lightcraft-mcp = { path = "crates/mcp" }
lightcraft-fetch = { path = "crates/fetch" }
# HEIC/HEIF: the shared decoder (moved from crates/heif; craft-libs#784).
craft-heif = { git = "https://github.com/storytold/craft-libs", rev = "9dcbd90c7d16e1a668b3afa057e940e2db328b93" }
craft-heif = { git = "https://github.com/storytold/craft-libs", rev = "4ffa307f1a0a6fbd8897318cc719440ab06e9c63", default-features = false }
lightcraft-segment = { path = "crates/segment" }
lightcraft-geocode = { path = "crates/geocode" }
lightcraft-slideshow = { path = "crates/slideshow" }
@@ -95,13 +100,6 @@ miniz_oxide = "0.9"
# codecs
ab_glyph = { version = "0.2", default-features = false, features = ["std"] } # Apache-2.0, pure Rust glyph rasterizer (export watermarks)
moxcms = { version = "0.9", default-features = false, features = ["lut"] }
jxl-oxide = { version = "0.12", default-features = false }
zune-core = { version = "0.5", default-features = false, features = ["std"] }
zune-jpegxl = { version = "0.5", default-features = false, features = ["std"] } # MIT/Apache-2.0/Zlib lossless JXL encoder (test fixtures only)
ravif = { version = "0.13", default-features = false, features = ["threading"] } # no "asm": it needs nasm (non-Rust)
# HDR AVIF (PQ): ravif fixes the sRGB transfer curve, so HDR files drive its rav1e + avif-serialize directly
rav1e = { version = "0.8.1", default-features = false, features = ["threading"] }
avif-serialize = "0.8.6"
# gpu (crates/gpu; native backends only, no GL: everything stays pure Rust)
# SAM 3 inference (lightcraft-segment). 0.9.2: later releases pull `tokenizers` with the
+2 -1
View File
@@ -2,7 +2,7 @@
**Stage: alpha** · next: beta, ~14 points and ~340–600 h away
> **Last reviewed:** 2026-10-10 · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6 · **Changes:** [revision history](#revision-history)
> **Last reviewed:** 2026-11-15 · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6 · **Changes:** [revision history](#revision-history)
One-page summary of how close LightCraft is to Adobe Lightroom. Agents: read this, then pick work
from [docs/gaps.md](docs/gaps.md). The full assessment, method and evidence are in
@@ -199,6 +199,7 @@ Ranked toward beta; detail in [docs/roadmap.md](docs/roadmap.md), gaps in [docs/
| Date | Change | Summary |
|---|---|---|
| 2026-11-15 | minor | Lens Blur with AI subject focus and per-mask bokeh; survey tiles fit the photos' shapes and glide to their new places; direct import of Lightroom backup zips (#2592); legacy shutter repair in virtual copies; the opened-library folder-safety fix; v0.21.0 and v0.21.1 released; craft-libs cutover for TIFF/JPEG/JXL/GIF/PSD/HEIF/raw, metadata and gain maps, plus a security-hardening batch; headline numbers not re-measured |
| 2026-10-24 | minor | Priority 7 (lens profile database) links its tracking issue #329; headline numbers unchanged |
| 2026-10-24 | patch | Alpha rationale: tests run in CI on Linux x64 (#2667) and FreeBSD nightly, not on macOS or Windows; ranked item 8 now reads "Tests in CI on macOS and Windows, and the GPU tests in CI"; percentages and hours unchanged |
| 2026-10-22 | minor | Print: Japanese photo and postcard paper sizes (L判, 2L判, KG判, はがき, 往復はがき, 六切, 四切, 名刺) in the page setup, Print dialog and PDF output; headline numbers unchanged |
+1
View File
@@ -45,6 +45,7 @@ covers a whole directory), or if a licence file referenced in the Licence column
| `crates/ui-egui/locales/sv.json`, `crates/ui-egui/locales/sv-formats.json` | Swedish interface catalogs | LightCraft contributors | original work: clean-room Swedish translations of English source phrases | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | 2026-10-15 | none |
| `crates/ui-egui/locales/fi.json`, `crates/ui-egui/locales/fi-formats.json` | Finnish interface catalogs | @roopeg with Claude Code (AI-assisted; native-speaker review by @roopeg) | original work: translations of the English source phrases, using common Finnish photo-editing vocabulary; no external translation material | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | 2026-10-17 | count-neutral labels for Finnish partitive agreement, Finnish dates and times (`20.9.2026`, `16:04`), reviewed placeholders |
| `crates/ui-egui/locales/vi.json`, `crates/ui-egui/locales/vi-formats.json` | Vietnamese interface catalogs | @duydev (DUY Tran Nhat) with AI assistance (Cursor) | original work: translations of English source phrases; no external translation material | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | 2026-10-13 | plural-neutral counts, Vietnamese dates and weekdays, reviewed placeholders |
| `crates/codecs/tests/fixtures/quad-444-gbr-lossless.avif` | AVIF decode test fixture: a 33×17 lossless 8-bit 4:4:4 AV1 still with the identity (GBR) matrix, four flat colour quadrants | craft-libs contributors | [craft-libs 59b4f1e8 `crates/image-codecs/tests/fixtures/avif/quad-444-gbr-lossless.avif`](https://github.com/storytold/craft-libs/blob/59b4f1e8c2909fbcacba491b219958f4a976d5f8/crates/image-codecs/tests/fixtures/avif/quad-444-gbr-lossless.avif) (storytold/craft-libs#1601), encoded with FFmpeg/libaom from a synthetic RGB source as described in `crates/codecs/tests/fixtures/avif-README.md`; no third-party image content | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | 2026-11-16 | none: byte-identical copy of the craft-libs fixture; decodes exactly to its four-colour `quad-source.rgb` |
Notes
- egui's default fonts (Hack, Ubuntu Light, Noto Emoji, egui-icons and emoji-icon-font) come from the
+6 -8
View File
@@ -16,10 +16,11 @@ craft-gainmap = { workspace = true }
craft-cms = { workspace = true }
craft-jpeg = { workspace = true }
craft-image-codecs = { workspace = true }
craft-metadata = { workspace = true }
craft-tiff = { workspace = true }
craft-gif = { workspace = true }
craft-psd = { workspace = true }
craft-heif = { workspace = true, optional = true }
craft-heif = { workspace = true, optional = true, features = ["decode"] }
serde = { workspace = true }
bytemuck = { workspace = true }
thiserror = { workspace = true }
@@ -27,9 +28,8 @@ rayon = { workspace = true, optional = true }
craft-jxl = { workspace = true, optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ravif = { workspace = true, optional = true }
rav1e = { workspace = true, optional = true }
avif-serialize = { workspace = true, optional = true }
craft-video = { workspace = true, optional = true }
craft-avif-encode = { workspace = true, optional = true }
[features]
default = ["parallel", "jxl", "avif"]
@@ -40,16 +40,14 @@ jxl = ["dep:craft-jxl"]
# 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: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"]
## AVIF/AV1 encoding via craft-image-codecs / craft-video (pure Rust), plus the shared AVIF decoder. Native targets only; a no-op on wasm32.
avif = ["dep:craft-video", "dep:craft-avif-encode", "craft-image-codecs/avif-decode"]
[dev-dependencies]
# tests record why they skip (`cargo xtask ci` lists the skips)
lightcraft-testkit = { workspace = true }
# JPEG parity tests and the benchmark use the prior/reference encoder only in development.
jpeg-encoder = { workspace = true }
# JPEG XL tests read encoded samples without our colour conversion
jxl-oxide = { workspace = true }
proptest = { workspace = true }
# Lossless synthetic HEIC files for the `heif` feature's tests.
craft-heif = { workspace = true, features = ["testdata"] }
+2 -2
View File
@@ -114,8 +114,8 @@ Run `cargo test --release -p lightcraft-codecs --test it bench:: -- --ignored --
## Gaps / limitations
- **AVIF decode**: no permissively licensed, pure-Rust AV1 decoder exists today (dav1d is C;
rav1d-safe is AGPL). Files are sniffed and rejected with `Error::Unsupported`.
- **AVIF decode** uses the shared pure-Rust AV1 decoder on native builds with the `avif` feature.
Animated AVIF, depth and gain-map auxiliary images, and progressive/layered images are unsupported.
- **HEIC** needs the `heif` feature (off by default: HEVC patents are the distributor's call);
without it, `Error::Unsupported` says the build has no HEIC support. 4:2:2 and 4:4:4 HEIF
(Canon/Sony HIF) is refused: heic-rs 0.1.1 loses sync on those bitstreams.
+262
View File
@@ -0,0 +1,262 @@
//! AVIF container and pixel decoding via shared image codecs.
use crate::{DecodeOptions, Decoded, Error, Format, Result};
const F: Format = Format::Avif;
#[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))]
pub(crate) fn header(_: &[u8]) -> Result<(u32, u32, u16)> {
Err(Error::Unsupported(F, "AVIF decoding is not included in this build"))
}
#[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))]
pub(crate) fn decode(_: &[u8], _: &DecodeOptions) -> Result<Decoded> {
Err(Error::Unsupported(F, "AVIF decoding is not included in this build"))
}
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
fn err(e: craft_image_codecs::CodecError) -> Error {
match e {
craft_image_codecs::CodecError::LimitExceeded(_) => Error::TooLarge(0, 0),
craft_image_codecs::CodecError::Unsupported { .. } => Error::Unsupported(F, "unsupported AVIF"),
other => Error::Malformed(F, other.to_string()),
}
}
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
pub(crate) fn header(bytes: &[u8]) -> Result<(u32, u32, u16)> {
let (w, h) = craft_image_codecs::probe_size(bytes).map_err(err)?;
Ok((w, h, 1))
}
// Match craft-video's AV1 store: 128-sample superblocks plus 160 samples of edge padding,
// three u16 planes, eight references, the current picture, and at most eight workers + two
// in-flight pictures. Keep a hard byte ceiling even when the caller permits many pixels.
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
fn picture_store_budget(width: u32, height: u32) -> u64 {
let padded = |n: u32| u64::from(n).div_ceil(128) * 128 + 160;
padded(width).saturating_mul(padded(height)).saturating_mul(PICTURE_STORE_BYTES_PER_SAMPLE).min(PICTURE_STORE_CAP)
}
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
const PICTURE_STORE_BYTES_PER_SAMPLE: u64 = 3 * 2 * (8 + 1 + 8 + 2);
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
const PICTURE_STORE_CAP: u64 = if cfg!(target_pointer_width = "64") { 4 << 30 } else { 1 << 30 };
/// The picture-store budget for any stored AV1 canvas the pixel limit admits. `probe_size` reports
/// the size after `clap`, but the decoder builds the stored (pre-crop) canvas, which the shared
/// decoder checks against `max_pixels` from its `ispe` before decoding. A canvas of at most
/// `max_pixels` samples with AV1's 65,536-sample edge limit has `w + h <= min(max_pixels + 1, 2^17)`,
/// and each padded edge is under `edge + 288`, so the padded area is below
/// `max_pixels + 288 * (w + h) + 288^2`.
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
fn stored_picture_store_budget(max_pixels: u64) -> u64 {
let edge_sum = max_pixels.saturating_add(1).min(2 * 65_536);
let area = max_pixels.saturating_add(edge_sum.saturating_mul(288)).saturating_add(288 * 288);
area.saturating_mul(PICTURE_STORE_BYTES_PER_SAMPLE).min(PICTURE_STORE_CAP)
}
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
use crate::convert::{Buf, Meta, Model, Raw, check_size, finish};
use crate::space::SourceSpace;
let (w, h, _) = header(bytes)?;
check_size(F, w as u64, h as u64, opts)?;
let options = craft_image_codecs::DecodeOptions {
limits: craft_image_codecs::Limits {
max_width: u32::MAX,
max_height: u32::MAX,
max_pixels: opts.max_pixels,
max_alloc: stored_picture_store_budget(opts.max_pixels).max(picture_store_budget(w, h)),
},
keep_orientation: false,
};
let (img, bit_depth) = craft_image_codecs::decode_avif_with_depth(bytes, &options).map_err(err)?;
let (w, h) = (img.width(), img.height());
check_size(F, w as u64, h as u64, opts)?;
let buf = match img.sample_type() {
craft_image_codecs::SampleType::U8 => Buf::U8(img.as_u8().ok_or_else(|| Error::Malformed(F, "expected 8-bit samples".into()))?.to_vec()),
craft_image_codecs::SampleType::U16 => Buf::U16(img.to_u16_samples().ok_or_else(|| Error::Malformed(F, "expected 16-bit samples".into()))?),
craft_image_codecs::SampleType::F32 => Buf::F32(img.to_f32_samples().ok_or_else(|| Error::Malformed(F, "expected float samples".into()))?),
_ => return Err(Error::Unsupported(F, "unsupported AVIF sample type")),
};
let hint = img.cicp.and_then(|c| SourceSpace::from_cicp(c.primaries, c.transfer));
let raw = Raw { width: w as usize, height: h as usize, model: Model::Rgb, alpha: img.layout().has_alpha(), premultiplied: false, buf, bit_depth };
let meta = Meta { icc: img.icc, exif: img.meta.exif, xmp: img.meta.xmp, orientation: Some(1), hint, ..Default::default() };
let mut decoded = finish(F, raw, meta, (w, h), opts)?;
// AV1 stores integer samples even when HDR transfer decoding produces linear float pixels.
decoded.float = false;
Ok(decoded)
}
#[cfg(all(test, feature = "avif", not(target_arch = "wasm32")))]
mod tests {
use super::*;
const FIXTURE: &[u8] = include_bytes!("../tests/fixtures/quad-444-gbr-lossless.avif");
#[test]
fn production_entrypoints_decode_avif_and_enforce_pixel_budget() {
assert!(F.can_decode());
assert_eq!(crate::sniff(FIXTURE), Some(F));
let header = crate::read_header(FIXTURE).unwrap();
assert_eq!((header.width, header.height, header.orientation), (33, 17, 1));
let decoded = crate::decode(FIXTURE, DecodeOptions::default()).unwrap();
assert_eq!((decoded.source_width, decoded.source_height), (33, 17));
assert_eq!(decoded.bit_depth, 8);
let exact = DecodeOptions { max_pixels: 33 * 17, ..DecodeOptions::default() };
let decoded = crate::decode(FIXTURE, exact).unwrap();
assert_eq!((decoded.source_width, decoded.source_height), (33, 17));
assert!(!decoded.float);
let opts = DecodeOptions { max_pixels: 33 * 17 - 1, ..DecodeOptions::default() };
assert!(matches!(crate::decode(FIXTURE, opts), Err(Error::TooLarge(..))));
}
#[test]
fn picture_store_budget_accounts_for_padding_and_stays_bounded() {
assert_eq!(picture_store_budget(33, 17), 288 * 288 * 6 * 19);
let cap = if cfg!(target_pointer_width = "64") { 4 << 30 } else { 1 << 30 };
assert_eq!(picture_store_budget(u32::MAX, u32::MAX), cap);
assert_eq!(stored_picture_store_budget(u64::MAX), cap);
// Every canvas the pixel limit admits fits, whatever its shape.
for max_pixels in [1u64, 561, 512 * 512, 1 << 20] {
let budget = stored_picture_store_budget(max_pixels);
for (w, h) in [(1, max_pixels), (max_pixels, 1), (max_pixels.isqrt(), max_pixels.isqrt())] {
let (w, h) = (w.min(65_536) as u32, h.min(65_536) as u32);
assert!(picture_store_budget(w, h) <= budget, "{w}x{h} under {max_pixels}");
}
}
}
/// `probe_size` reports the `clap`-cropped size; the decoder still builds the stored canvas.
/// A 512×512 picture cropped to 16×16 must decode with the default options and at a pixel
/// limit that covers the stored canvas, and fail as too large below it.
#[test]
fn cropped_avif_decodes_with_a_budget_for_its_stored_canvas() {
let pixels = [90u8, 140, 200].repeat(512 * 512);
let image = crate::encode::EncodeImage::new(512, 512, 3, crate::encode::Samples::U8(&pixels));
let full = crate::encode::encode_avif(&image, 60, 10, &crate::encode::EncodeMeta::default()).unwrap();
let cropped = with_clap(&full, 16, 16);
assert_eq!(crate::read_header(&cropped).map(|h| (h.width, h.height)).unwrap(), (16, 16));
// The old budget, sized from the cropped 16×16, is below what the stored canvas needs.
assert!(picture_store_budget(16, 16) < picture_store_budget(512, 512));
let decoded = crate::decode(&cropped, DecodeOptions::default()).unwrap();
assert_eq!((decoded.source_width, decoded.source_height), (16, 16));
let exact = DecodeOptions { max_pixels: 512 * 512, ..DecodeOptions::default() };
assert_eq!(crate::decode(&cropped, exact).map(|d| (d.source_width, d.source_height)).unwrap(), (16, 16));
let below = DecodeOptions { max_pixels: 512 * 512 - 1, ..DecodeOptions::default() };
assert!(crate::decode(&cropped, below).is_err());
}
fn be32(b: &[u8], at: usize) -> usize {
u32::from_be_bytes([b[at], b[at + 1], b[at + 2], b[at + 3]]) as usize
}
fn put32(b: &mut [u8], at: usize, v: usize) {
b[at..at + 4].copy_from_slice(&(v as u32).to_be_bytes());
}
/// Child boxes of `b[start..end]`: (offset, size, type).
fn boxes(b: &[u8], start: usize, end: usize) -> Vec<(usize, usize, [u8; 4])> {
let mut out = Vec::new();
let mut at = start;
while at + 8 <= end {
let size = be32(b, at);
out.push((at, size, [b[at + 4], b[at + 5], b[at + 6], b[at + 7]]));
at += size;
}
out
}
fn find(b: &[u8], start: usize, end: usize, kind: &[u8; 4]) -> (usize, usize) {
boxes(b, start, end).into_iter().find(|x| &x.2 == kind).map_or((0, 0), |x| (x.0, x.1))
}
/// Adds a centred `clap` of `w × h` to the primary item of a single-item AVIF from the encoder,
/// fixing the enclosing box sizes and the `iloc` offsets into `mdat`.
fn with_clap(file: &[u8], w: u32, h: u32) -> Vec<u8> {
let mut b = file.to_vec();
let (meta, meta_size) = find(&b, 0, b.len(), b"meta");
let children = meta + 12;
let (iprp, _) = find(&b, children, meta + meta_size, b"iprp");
let (ipco, ipco_size) = find(&b, iprp + 8, iprp + be32(&b, iprp), b"ipco");
let index = boxes(&b, ipco + 8, ipco + ipco_size).len() + 1;
let mut clap = Vec::new();
for v in [w, 1, h, 1, 0, 1, 0, 1] {
clap.extend_from_slice(&v.to_be_bytes());
}
let clap = [&((8 + clap.len()) as u32).to_be_bytes()[..], b"clap", &clap].concat();
// ipma: version/flags, entry count, then item id, association count, associations.
let (ipma, ipma_size) = find(&b, iprp + 8, iprp + be32(&b, iprp), b"ipma");
let (version, flags) = (b[ipma + 8], b[ipma + 11]);
assert_eq!(be32(&b, ipma + 12), 1, "one item");
let id_len = if version < 1 { 2 } else { 4 };
let count_at = ipma + 16 + id_len;
let wide = flags & 1 == 1;
let assoc_end = count_at + 1 + b[count_at] as usize * if wide { 2 } else { 1 };
assert_eq!(assoc_end, ipma + ipma_size);
let assoc: Vec<u8> = if wide { (0x8000u16 | index as u16).to_be_bytes().to_vec() } else { vec![0x80 | index as u8] };
let grow = clap.len() + assoc.len();
b[count_at] += 1;
b.splice(assoc_end..assoc_end, assoc.iter().copied());
b.splice(ipco + ipco_size..ipco + ipco_size, clap.iter().copied());
// ipco precedes ipma inside iprp, so ipma moved by the clap's length.
for (at, by) in [(meta, grow), (iprp, grow), (ipco, clap.len()), (ipma + clap.len(), assoc.len())] {
let size = be32(&b, at);
put32(&mut b, at, size + by);
}
// iloc (version 0 or 1): shift every absolute extent offset by the growth of `meta`.
let (iloc, _) = find(&b, children, meta + be32(&b, meta), b"iloc");
let v = b[iloc + 8];
let (offset_size, length_size, base_size) = ((b[iloc + 12] >> 4) as usize, (b[iloc + 12] & 15) as usize, (b[iloc + 13] >> 4) as usize);
let index_size = if v >= 1 { (b[iloc + 13] & 15) as usize } else { 0 };
let mut at = iloc + 14;
let items = if v < 2 {
let n = u16::from_be_bytes([b[at], b[at + 1]]) as usize;
at += 2;
n
} else {
let n = be32(&b, at);
at += 4;
n
};
let read = |b: &[u8], at: usize, n: usize| b[at..at + n].iter().fold(0usize, |acc, &x| acc << 8 | x as usize);
let write = |b: &mut [u8], at: usize, n: usize, v: usize| {
for i in 0..n {
b[at + i] = (v >> (8 * (n - 1 - i))) as u8;
}
};
for _ in 0..items {
at += if v < 2 { 2 } else { 4 };
if v >= 1 {
at += 2;
}
at += 2;
let base = read(&b, at, base_size);
if base_size > 0 && base > 0 {
write(&mut b, at, base_size, base + grow);
}
at += base_size;
let extents = u16::from_be_bytes([b[at], b[at + 1]]) as usize;
at += 2;
for _ in 0..extents {
at += index_size;
let off = read(&b, at, offset_size);
if base == 0 && offset_size > 0 {
write(&mut b, at, offset_size, off + grow);
}
at += offset_size + length_size;
}
}
b
}
#[test]
fn hdr_avif_round_trips_highlights_through_public_decoder() {
let pixels = vec![2.0f32; 16 * 16 * 3];
for transfer in [crate::encode::HdrTransfer::Pq, crate::encode::HdrTransfer::Hlg] {
let bytes = crate::encode::encode_avif_hdr(16, 16, &pixels, 95, 10, &crate::encode::EncodeMeta::default(), transfer).unwrap();
let decoded = crate::decode(&bytes, DecodeOptions::default()).unwrap();
assert_eq!(decoded.bit_depth, 10);
assert!(!decoded.float, "AV1 source samples are integer for {transfer:?}");
assert!(decoded.image.data.iter().any(|pixel| pixel[0] > 1.0), "HDR highlights were lost");
}
}
#[test]
fn avif_alpha_survives_public_round_trip() {
let pixels = [200u8, 100, 50, 96].repeat(16 * 16);
let image = crate::encode::EncodeImage::new(16, 16, 4, crate::encode::Samples::U8(&pixels));
let bytes = crate::encode::encode_avif(&image, 95, 10, &crate::encode::EncodeMeta::default()).unwrap();
let decoded = crate::decode(&bytes, DecodeOptions::default()).unwrap();
let alpha = decoded.alpha.expect("alpha plane");
assert!((alpha.data[0] - 96.0 / 255.0).abs() < 0.04);
}
}
+38 -182
View File
@@ -54,10 +54,6 @@ impl<'a> EncodeImage<'a> {
}
Ok(())
}
fn has_alpha(&self) -> bool {
self.channels == 2 || self.channels == 4
}
}
/// Metadata to embed.
@@ -148,9 +144,9 @@ pub fn encode_jpeg(img: &EncodeImage, quality: u8, subsampling: ChromaSubsamplin
.map_err(|e| Error::Encode(e.to_string()))
}
/// Encode PNG (8- or 16-bit; float input is rejected — quantize first).
pub fn encode_png(img: &EncodeImage, meta: &EncodeMeta) -> Result<Vec<u8>> {
img.validate()?;
/// `img` (already validated) as a `craft-image-codecs` image. `refuse_float` is the error for
/// 32-bit float samples, for a format that cannot hold them; `None` converts them too.
fn shared_image(img: &EncodeImage, refuse_float: Option<&str>) -> Result<craft_image_codecs::Image> {
let n = img.width as usize * img.height as usize * img.channels as usize;
let layout = match img.channels {
1 => craft_image_codecs::ChannelLayout::Gray,
@@ -158,12 +154,19 @@ pub fn encode_png(img: &EncodeImage, meta: &EncodeMeta) -> Result<Vec<u8>> {
3 => craft_image_codecs::ChannelLayout::Rgb,
_ => craft_image_codecs::ChannelLayout::Rgba,
};
let mut ic_img = match img.samples {
Samples::U8(s) => craft_image_codecs::Image::from_u8(img.width, img.height, layout, s[..n].to_vec()),
Samples::U16(s) => craft_image_codecs::Image::from_u16(img.width, img.height, layout, &s[..n]),
Samples::F32(_) => return Err(Error::Encode("PNG needs 8- or 16-bit samples".into())),
match (img.samples, refuse_float) {
(Samples::U8(s), _) => craft_image_codecs::Image::from_u8(img.width, img.height, layout, s[..n].to_vec()),
(Samples::U16(s), _) => craft_image_codecs::Image::from_u16(img.width, img.height, layout, &s[..n]),
(Samples::F32(_), Some(refusal)) => return Err(Error::Encode(refusal.into())),
(Samples::F32(s), None) => craft_image_codecs::Image::from_f32(img.width, img.height, layout, &s[..n]),
}
.map_err(|e| Error::Encode(e.to_string()))?;
.map_err(|e| Error::Encode(e.to_string()))
}
/// Encode PNG (8- or 16-bit; float input is rejected — quantize first).
pub fn encode_png(img: &EncodeImage, meta: &EncodeMeta) -> Result<Vec<u8>> {
img.validate()?;
let mut ic_img = shared_image(img, Some("PNG needs 8- or 16-bit samples"))?;
ic_img.icc = meta.icc.map(|b| b.to_vec());
ic_img.meta.exif = meta.exif.map(|b| b.to_vec());
ic_img.meta.xmp = meta.xmp.map(|x| x.to_string());
@@ -208,19 +211,7 @@ pub enum TiffCompression {
/// ICC and XMP are embedded; EXIF is not (write it through the TIFF writer in `craft-tiff`).
pub fn encode_tiff(img: &EncodeImage, compression: TiffCompression, meta: &EncodeMeta) -> Result<Vec<u8>> {
img.validate()?;
let n = img.width as usize * img.height as usize * img.channels as usize;
let layout = match img.channels {
1 => craft_image_codecs::ChannelLayout::Gray,
2 => craft_image_codecs::ChannelLayout::GrayA,
3 => craft_image_codecs::ChannelLayout::Rgb,
_ => craft_image_codecs::ChannelLayout::Rgba,
};
let mut ic_img = match img.samples {
Samples::U8(s) => craft_image_codecs::Image::from_u8(img.width, img.height, layout, s[..n].to_vec()),
Samples::U16(s) => craft_image_codecs::Image::from_u16(img.width, img.height, layout, &s[..n]),
Samples::F32(s) => craft_image_codecs::Image::from_f32(img.width, img.height, layout, &s[..n]),
}
.map_err(|e| Error::Encode(e.to_string()))?;
let mut ic_img = shared_image(img, None)?;
ic_img.icc = meta.icc.map(|b| b.to_vec());
ic_img.meta.xmp = meta.xmp.map(|x| x.to_string());
ic_img.meta.dpi = meta.ppi.filter(|p| *p > 0).map(|p| (p as f32, p as f32));
@@ -299,37 +290,27 @@ pub fn encode_avif(img: &EncodeImage, quality: u8, speed: u8, meta: &EncodeMeta)
img.validate()?;
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
{
let mut enc = ravif::Encoder::new()
.with_quality(quality.clamp(1, 100) as f32)
.with_alpha_quality(quality.clamp(1, 100) as f32)
.with_speed(speed.clamp(1, 10));
if let Some(exif) = meta.exif {
enc = enc.with_exif(exif.to_vec());
}
let s = match img.samples {
Samples::U8(s) => s,
Samples::U16(s) => return avif_10bit(&enc, img, s),
Samples::F32(_) => return Err(Error::Encode("AVIF encoder needs 8- or 16-bit samples".into())),
// Keep AVIF on its threading-capable shared revision without changing other codecs.
use craft_avif_encode::{ChannelLayout, Image};
let n = img.width as usize * img.height as usize * img.channels as usize;
let layout = match img.channels {
1 => ChannelLayout::Gray,
2 => ChannelLayout::GrayA,
3 => ChannelLayout::Rgb,
_ => ChannelLayout::Rgba,
};
let n = img.width as usize * img.height as usize;
let ch = img.channels as usize;
let px: Vec<ravif::RGBA8> = s
.chunks_exact(ch)
.take(n)
.map(|c| match ch {
1 => ravif::RGBA8::new(c[0], c[0], c[0], 255),
2 => ravif::RGBA8::new(c[0], c[0], c[0], c[1]),
3 => ravif::RGBA8::new(c[0], c[1], c[2], 255),
_ => ravif::RGBA8::new(c[0], c[1], c[2], c[3]),
})
.collect();
let r = enc.encode_rgba(ravif::Img::new(&px[..], img.width as usize, img.height as usize)).map_err(|e| Error::Encode(e.to_string()))?;
let _ = img.has_alpha();
Ok(r.avif_file)
let mut ic_img = match img.samples {
Samples::U8(s) => Image::from_u8(img.width, img.height, layout, s[..n].to_vec()),
Samples::U16(s) => Image::from_u16(img.width, img.height, layout, &s[..n]),
Samples::F32(_) => return Err(Error::Encode("AVIF encoder needs 8- or 16-bit samples".into())),
}
.map_err(|e| Error::Encode(e.to_string()))?;
ic_img.meta.exif = meta.exif.map(|e| e.to_vec());
craft_avif_encode::encode_avif_with_speed(&ic_img, quality, speed).map_err(|e| Error::Encode(e.to_string()))
}
#[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))]
{
let _ = (quality, speed, meta, img.has_alpha());
let _ = (quality, speed, meta);
Err(Error::Encode("AVIF encoding is not available in this build".into()))
}
}
@@ -401,108 +382,12 @@ pub fn encode_avif_hdr(width: u32, height: u32, rgb: &[f32], quality: u8, speed:
}
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
{
use rav1e::prelude::*;
const K: [f32; 3] = [0.2627, 0.6780, 0.0593]; // BT.2020 NCL
let mut max_nits = 0f32;
let mut sum_nits = 0f64;
let planes: Vec<[u16; 3]> = rgb
.as_chunks::<3>()
.0
.iter()
.take(n)
.map(|c| {
let nits = c.map(|v| (if v.is_finite() { v.max(0.0) } else { 0.0 }) * HDR_REFERENCE_WHITE_NITS);
let y_nits = K[0] * nits[0] + K[1] * nits[1] + K[2] * nits[2];
max_nits = max_nits.max(nits[0].max(nits[1]).max(nits[2]));
sum_nits += y_nits as f64;
let [r, g, b] = match transfer {
HdrTransfer::Pq => nits.map(pq_encode),
HdrTransfer::Hlg => hlg_signal([c[0], c[1], c[2]]),
};
let y = K[0] * r + K[1] * g + K[2] * b;
let cb = (b - y) / (2.0 * (1.0 - K[2])) + 0.5;
let cr = (r - y) / (2.0 * (1.0 - K[0])) + 0.5;
[y, cb, cr].map(|v| (v * 1023.0).round().clamp(0.0, 1023.0) as u16)
})
.collect();
let q = quality.clamp(1, 100) as f32 / 100.0;
// ravif's quality → quantizer curve, so qualities match the SDR AVIF path
let x = if q >= 0.82 {
(1.0 - q) * 2.6
} else if q > 0.25 {
1.0 - 0.125 - q * 0.5
} else {
1.0 - q
let _ = (w, h, n);
let t = match transfer {
HdrTransfer::Pq => craft_avif_encode::HdrTransfer::Pq,
HdrTransfer::Hlg => craft_avif_encode::HdrTransfer::Hlg,
};
let quantizer = (x * 255.0).round() as usize;
let mut ec = EncoderConfig::with_speed_preset(speed.clamp(1, 10));
ec.width = w;
ec.height = h;
ec.time_base = Rational::new(1, 1);
ec.bit_depth = 10;
ec.chroma_sampling = ChromaSampling::Cs444;
ec.pixel_range = PixelRange::Full;
ec.color_description = Some(ColorDescription {
color_primaries: ColorPrimaries::BT2020,
transfer_characteristics: match transfer {
HdrTransfer::Pq => TransferCharacteristics::SMPTE2084,
HdrTransfer::Hlg => TransferCharacteristics::HLG,
},
matrix_coefficients: MatrixCoefficients::BT2020NCL,
});
let cll = (max_nits.ceil().clamp(1.0, 10_000.0) as u16, ((sum_nits / n as f64).ceil().clamp(1.0, 10_000.0)) as u16);
ec.content_light =
(transfer == HdrTransfer::Pq).then_some(ContentLight { max_content_light_level: cll.0, max_frame_average_light_level: cll.1 });
ec.still_picture = true;
ec.quantizer = quantizer;
ec.min_quantizer = quantizer.min(255) as u8;
ec.speed_settings.multiref = false;
ec.speed_settings.rdo_lookahead_frames = 1;
ec.speed_settings.scene_detection_mode = SceneDetectionSpeed::None;
let e = |e: &dyn std::fmt::Display| Error::Encode(format!("AV1: {e}"));
let mut ctx: Context<u16> = Config::new().with_encoder_config(ec).new_context().map_err(|x| e(&x))?;
let mut frame = ctx.new_frame();
{
let mut it = planes.iter();
let mut fp = frame.planes.iter_mut();
let (Some(py), Some(pu), Some(pv)) = (fp.next(), fp.next(), fp.next()) else {
return Err(Error::Encode("AV1: frame has no planes".into()));
};
let (mut sy, mut su, mut sv) = (py.mut_slice(Default::default()), pu.mut_slice(Default::default()), pv.mut_slice(Default::default()));
for ((ry, ru), rv) in sy.rows_iter_mut().zip(su.rows_iter_mut()).zip(sv.rows_iter_mut()).take(h) {
for ((y, u), v) in ry.iter_mut().zip(ru.iter_mut()).zip(rv.iter_mut()).take(w) {
let Some(p) = it.next() else { break };
(*y, *u, *v) = (p[0], p[1], p[2]);
}
}
}
ctx.send_frame(frame).map_err(|x| e(&x))?;
ctx.flush();
let mut av1 = Vec::new();
loop {
match ctx.receive_packet() {
Ok(mut p) if p.frame_type == FrameType::KEY => av1.append(&mut p.data),
Ok(_) => {}
Err(EncoderStatus::Encoded | EncoderStatus::LimitReached) => break,
Err(x) => return Err(e(&x)),
}
}
use avif_serialize::constants as c;
let mut a = avif_serialize::Aviffy::new();
a.set_matrix_coefficients(c::MatrixCoefficients::Bt2020Ncl)
.set_transfer_characteristics(match transfer {
HdrTransfer::Pq => c::TransferCharacteristics::Smpte2084,
HdrTransfer::Hlg => c::TransferCharacteristics::Hlg,
})
.set_color_primaries(c::ColorPrimaries::Bt2020)
.set_full_color_range(true);
if transfer == HdrTransfer::Pq {
a.set_content_light_level(cll.0, cll.1);
}
if let Some(exif) = meta.exif {
a.set_exif(exif.to_vec());
}
Ok(a.to_vec(&av1, None, width, height, 10))
craft_avif_encode::encode_avif_hdr(width, height, rgb, quality, speed, meta.exif, t).map_err(|e| Error::Encode(e.to_string()))
}
#[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))]
{
@@ -555,35 +440,6 @@ fn png_chunk(kind: [u8; 4], data: &[u8]) -> Vec<u8> {
out
}
/// 16-bit RGB(A) → 10-bit BT.601 full-range YCbCr planes (the matrix `ravif` uses for 8-bit input).
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
fn avif_10bit(enc: &ravif::Encoder, img: &EncodeImage, s: &[u16]) -> Result<Vec<u8>> {
const K: [f32; 3] = [0.299, 0.587, 0.114];
let n = img.width as usize * img.height as usize;
let ch = img.channels as usize;
let rgb = |c: &[u16]| -> [f32; 3] {
let v = |x: u16| x as f32 * (1023.0 / 65535.0);
match ch {
1 | 2 => [v(c[0]); 3],
_ => [v(c[0]), v(c[1]), v(c[2])],
}
};
let planes = s.chunks_exact(ch).take(n).map(|c| {
let [r, g, b] = rgb(c);
let y = K[0] * r + K[1] * g + K[2] * b;
let cb = (b - y) * (0.5 / (1.0 - K[2])) + 512.0;
let cr = (r - y) * (0.5 / (1.0 - K[0])) + 512.0;
[y, cb, cr].map(|v| v.round().clamp(0.0, 1023.0) as u16)
});
let alpha_at = |c: &[u16]| if img.has_alpha() { c[ch - 1] } else { u16::MAX };
let has_alpha = s.chunks_exact(ch).take(n).any(|c| alpha_at(c) != u16::MAX);
let alpha = has_alpha.then(|| s.chunks_exact(ch).take(n).map(move |c| ((alpha_at(c) as u32 * 1023 + 32767) / 65535) as u16));
let r = enc
.encode_raw_planes_10_bit(img.width as usize, img.height as usize, planes, alpha, ravif::PixelRange::Full, ravif::MatrixCoefficients::BT601)
.map_err(|e| Error::Encode(e.to_string()))?;
Ok(r.avif_file)
}
#[cfg(test)]
mod hlg_tests {
use super::*;
+9 -20
View File
@@ -57,6 +57,7 @@ impl<'a> Tiff<'a> {
Some((v, next))
}
/// First value of a SHORT/LONG entry.
#[cfg(test)]
pub fn uint(&self, e: &Entry) -> Option<u32> {
match e.typ {
3 => self.u16_at(e.value_pos).map(u32::from),
@@ -80,6 +81,10 @@ impl<'a> Tiff<'a> {
}
}
pub use craft_metadata::compat_orientation::minimal_exif;
pub(crate) use craft_metadata::compat_orientation::summarize;
#[cfg(test)]
/// What the decoder cares about from an EXIF (TIFF-structured) blob.
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct ExifSummary {
@@ -90,7 +95,8 @@ pub(crate) struct ExifSummary {
pub adobe_rgb_hint: bool,
}
pub(crate) fn summarize(blob: &[u8]) -> ExifSummary {
#[cfg(test)]
pub(crate) fn legacy_reference(blob: &[u8]) -> ExifSummary {
let mut s = ExifSummary::default();
let Some(t) = Tiff::new(blob) else { return s };
let Some(ifd0) = t.first_ifd() else { return s };
@@ -130,23 +136,6 @@ pub(crate) fn summarize(blob: &[u8]) -> ExifSummary {
s
}
/// Build a tiny EXIF blob (little-endian TIFF) with just an orientation tag. Used by tests and by
/// callers that need to stamp orientation on export when no source EXIF exists.
pub fn minimal_exif(orientation: u16) -> Vec<u8> {
let mut v = Vec::with_capacity(26);
v.extend_from_slice(b"II");
v.extend_from_slice(&42u16.to_le_bytes());
v.extend_from_slice(&8u32.to_le_bytes());
v.extend_from_slice(&1u16.to_le_bytes());
v.extend_from_slice(&0x0112u16.to_le_bytes());
v.extend_from_slice(&3u16.to_le_bytes());
v.extend_from_slice(&1u32.to_le_bytes());
v.extend_from_slice(&orientation.to_le_bytes());
v.extend_from_slice(&[0, 0]);
v.extend_from_slice(&0u32.to_le_bytes());
v
}
#[cfg(test)]
mod tests {
use super::*;
@@ -160,7 +149,7 @@ mod tests {
#[test]
fn garbage_ok() {
assert_eq!(summarize(b"II*\0\xff\xff\xff\xff"), ExifSummary::default());
assert_eq!(summarize(&[]), ExifSummary::default());
assert_eq!(legacy_reference(b"II*\0\xff\xff\xff\xff"), ExifSummary::default());
assert_eq!(legacy_reference(&[]), ExifSummary::default());
}
}
+1 -52
View File
@@ -132,58 +132,7 @@ pub fn write_matrix_trc(space: &RgbSpace, trc: &Trc) -> Vec<u8> {
};
p.copyright = "No copyright, use freely".to_string();
let encoded = p.with_encoded_bytes();
align_tags(&fixed_date(encoded.to_bytes().to_vec()))
}
/// Zero the header's creation date/time (ICC.1 §7.2.8, bytes 24..36) so a profile's bytes depend
/// only on its colour space and curve. `moxcms` stamps the wall clock there on every `encode`
/// (ignoring `ColorProfile::creation_date_time`), which made two exports of one photo differ in
/// the embedded profile and nowhere else, so output hashes could not be compared (#180). Readers
/// do not interpret the field; all zeros is the conventional "unspecified" value.
fn fixed_date(mut b: Vec<u8>) -> Vec<u8> {
if let Some(date) = b.get_mut(24..36) {
date.fill(0);
}
b
}
/// Re-lay a profile's tag data on 4-byte boundaries (ICC.1 §7.3.1). The encoder packs tags back to
/// back, so a tag of odd length (e.g. a `mluc` description with an odd number of characters)
/// misaligns every following tag — which some readers (macOS ImageIO for PNG `iCCP`) reject.
fn align_tags(b: &[u8]) -> Vec<u8> {
let be = |i: usize| b.get(i..i + 4).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]]) as usize);
let Some(n) = be(128) else { return b.to_vec() };
let table_end = 132 + 12 * n;
let tags: Option<Vec<(usize, usize)>> = (0..n).map(|t| Some((be(132 + 12 * t + 4)?, be(132 + 12 * t + 8)?))).collect();
let Some(tags) = tags.filter(|t| table_end <= b.len() && t.iter().all(|&(o, l)| o >= table_end && o + l <= b.len())) else {
return b.to_vec();
};
let mut out = b[..table_end].to_vec();
let mut placed: Vec<((usize, usize), usize)> = Vec::new();
for (t, &(off, len)) in tags.iter().enumerate() {
let new_off = match placed.iter().find(|(k, _)| *k == (off, len)) {
// tags sharing data keep sharing it
Some(&(_, o)) => o,
None => {
while !out.len().is_multiple_of(4) {
out.push(0);
}
let o = out.len();
out.extend_from_slice(&b[off..off + len]);
placed.push(((off, len), o));
o
}
};
out[132 + 12 * t + 4..132 + 12 * t + 8].copy_from_slice(&(new_off as u32).to_be_bytes());
}
while !out.len().is_multiple_of(4) {
out.push(0);
}
let size = out.len() as u32;
out[0..4].copy_from_slice(&size.to_be_bytes());
// the profile ID (MD5) would no longer match: clear it (= "not computed")
out[84..100].fill(0);
out
craft_cms::normalize_for_embedding(encoded.to_bytes().to_vec())
}
/// A profile for one of the well-known spaces with its standard curve.
+4 -1
View File
@@ -18,6 +18,7 @@
#![forbid(unsafe_code)]
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
mod avif_decode;
mod convert;
pub mod display;
pub mod encode;
@@ -221,6 +222,7 @@ pub fn read_header_unguarded(bytes: &[u8], format: Format) -> Result<Header> {
Format::WebP => webp::header(bytes)?,
Format::Psd => psd::header(bytes)?,
Format::Heif => heif::header(bytes)?,
Format::Avif => avif_decode::header(bytes)?,
Format::Gif | Format::Bmp => other::header(bytes, format)?,
// rare here (or, for JPEG XL, without a header reader yet): a small decode, as before
_ => {
@@ -257,7 +259,8 @@ pub fn decode_unguarded(bytes: &[u8], format: Format, opts: &DecodeOptions) -> R
#[cfg(not(feature = "jxl"))]
Format::Jxl => unsupported(),
Format::Heif => heif::decode(bytes, &opts),
Format::Avif | Format::RawTiffLike | Format::RawOther => unsupported(),
Format::Avif => avif_decode::decode(bytes, &opts),
Format::RawTiffLike | Format::RawOther => unsupported(),
}
}
+13 -105
View File
@@ -27,7 +27,7 @@ pub(crate) fn header(bytes: &[u8], format: Format) -> Result<(u32, u32, u16)> {
return Ok((w, h, 1));
}
if format == Format::Bmp {
let (w, h) = bmp_dims(bytes).ok_or_else(|| Error::Malformed(format, "invalid BMP header".into()))?;
let (w, h) = craft_image_codecs::bmp_probe::dimensions(bytes).ok_or_else(|| Error::Malformed(format, "invalid BMP header".into()))?;
if w == 0 || h == 0 {
return Err(Error::Malformed(format, "zero dimension".into()));
}
@@ -91,21 +91,18 @@ fn check_fillable(format: Format, bytes: &[u8], w: u32, h: u32) -> Result<()> {
}
craft_gif::first_frame_decoded_pixels(bytes).unwrap_or(0)
}
_ => match bmp_layout(bytes) {
BmpLayout::Rle { offset, rle4 } => bmp_rle_pixels(bytes, offset, rle4, w, h),
BmpLayout::Rows { offset, bits } => {
let row = u64::from(w).saturating_mul(u64::from(bits)).div_ceil(32).saturating_mul(4);
let needed = row.saturating_mul(u64::from(h));
let held = bytes.len().saturating_sub(offset) as u64;
if held < needed {
return Err(Error::Malformed(
format,
format!("a {w} × {h} canvas is larger than its {held} bytes of pixel data can hold ({needed} needed)"),
));
}
canvas
_ => {
let coverage = craft_image_codecs::bmp_probe::coverage(bytes, w, h);
if let Some((held, needed)) = coverage.row_bytes
&& held < needed
{
return Err(Error::Malformed(
format,
format!("a {w} × {h} canvas is larger than its {held} bytes of pixel data can hold ({needed} needed)"),
));
}
},
coverage.pixels
}
};
if canvas > covered && canvas > SPARSE_MAX_PIXELS {
return Err(Error::Malformed(
@@ -118,95 +115,6 @@ fn check_fillable(format: Format, bytes: &[u8], w: u32, h: u32) -> Result<()> {
Ok(())
}
/// Where a BMP's pixels start (the offset in its file header, which the decoder seeks to) and how
/// they are stored.
enum BmpLayout {
/// RLE8 or RLE4 commands.
Rle { offset: usize, rle4: bool },
/// Rows of `bits` per pixel, each padded to 4 bytes.
Rows { offset: usize, bits: u16 },
}
/// The layout the decoder reads, from the file header and the bitmap header: a 12-byte core header
/// (bits per pixel at 24, never compressed) or a 40-byte or larger one (bits at 28, compression at
/// 30). Fields the decoder would have refused are read as 1 bit per pixel at offset 0, the most
/// lenient layout.
fn bmp_layout(bytes: &[u8]) -> BmpLayout {
let u16_at = |i: usize| bytes.get(i..i.checked_add(2)?).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes);
let u32_at = |i: usize| bytes.get(i..i.checked_add(4)?).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes);
let offset = u32_at(10).and_then(|o| usize::try_from(o).ok()).unwrap_or(0);
let (bits, compression) = match u32_at(14) {
Some(12) => (u16_at(24), Some(0)),
_ => (u16_at(28), u32_at(30)),
};
match compression {
Some(1) => BmpLayout::Rle { offset, rle4: false },
Some(2) => BmpLayout::Rle { offset, rle4: true },
_ => BmpLayout::Rows { offset, bits: bits.filter(|&b| b > 0).unwrap_or(1) },
}
}
/// The pixels an RLE8 or RLE4 BMP's commands draw on a `w` × `h` canvas, read the way the decoder
/// paints them: a run is clipped at the end of its row, an end-of-row code moves to the next row, a
/// delta skips pixels (left blank), and an end-of-bitmap code, a truncated command or the last row
/// ends the picture. Every command takes at least 2 bytes, so the walk is linear in the file.
fn bmp_rle_pixels(bytes: &[u8], offset: usize, rle4: bool, w: u32, h: u32) -> u64 {
let (w, h) = (u64::from(w), u64::from(h));
let (mut pos, mut x, mut y, mut covered) = (offset, 0u64, 0u64, 0u64);
while y < h {
let (Some(&count), Some(&value)) = (bytes.get(pos), bytes.get(pos.saturating_add(1))) else {
break;
};
pos = pos.saturating_add(2);
match (count, value) {
// end of row
(0, 0) => (x, y) = (0, y.saturating_add(1)),
// end of bitmap
(0, 1) => break,
// delta: right and down
(0, 2) => {
let (Some(&dx), Some(&dy)) = (bytes.get(pos), bytes.get(pos.saturating_add(1))) else {
break;
};
pos = pos.saturating_add(2);
(x, y) = (x.saturating_add(u64::from(dx)), y.saturating_add(u64::from(dy)));
}
// absolute: `value` pixels stored one per byte (RLE8) or nibble (RLE4), padded to 2 bytes
(0, n) => {
let len = if rle4 { usize::from(n).div_ceil(2) } else { usize::from(n) };
pos = pos.saturating_add(len + (len & 1));
if pos > bytes.len() {
break;
}
covered = covered.saturating_add(u64::from(n).min(w.saturating_sub(x)));
x = x.saturating_add(u64::from(n));
}
// a run of `count` pixels
(n, _) => {
covered = covered.saturating_add(u64::from(n).min(w.saturating_sub(x)));
x = x.saturating_add(u64::from(n));
}
}
}
covered
}
/// Reads BMP `(width, height)` from the DIB header without allocating.
fn bmp_dims(bytes: &[u8]) -> Option<(u32, u32)> {
if bytes.get(0..2) != Some(b"BM") {
return None;
}
let dib = u32::from_le_bytes(bytes.get(14..18)?.try_into().ok()?);
let (w, h) = if dib == 12 {
(u16::from_le_bytes(bytes.get(18..20)?.try_into().ok()?) as u32, u16::from_le_bytes(bytes.get(20..22)?.try_into().ok()?) as u32)
} else {
let w = i32::from_le_bytes(bytes.get(18..22)?.try_into().ok()?);
let h = i32::from_le_bytes(bytes.get(22..26)?.try_into().ok()?);
(u32::try_from(w).ok()?, h.unsigned_abs())
};
Some((w, h))
}
pub(crate) fn decode(bytes: &[u8], format: Format, opts: &DecodeOptions) -> Result<Decoded> {
if format == Format::Gif {
return decode_gif(bytes, opts);
@@ -214,7 +122,7 @@ pub(crate) fn decode(bytes: &[u8], format: Format, opts: &DecodeOptions) -> Resu
if format != Format::Bmp {
return Err(Error::Unsupported(format, "not handled by this decoder"));
}
let (w, h) = bmp_dims(bytes).ok_or_else(|| Error::Malformed(format, "invalid BMP header".into()))?;
let (w, h) = craft_image_codecs::bmp_probe::dimensions(bytes).ok_or_else(|| Error::Malformed(format, "invalid BMP header".into()))?;
check_size(format, w as u64, h as u64, opts)?;
check_fillable(format, bytes, w, h)?;
let (w, h) = craft_image_codecs::probe_size(bytes).map_err(|e| Error::Malformed(format, e.to_string()))?;
+58 -98
View File
@@ -16,9 +16,7 @@ fn err(e: impl std::fmt::Display) -> Error {
}
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
if bytes.starts_with(b"\x89PNG\r\n\x1a\n") && bytes.get(12..16) == Some(b"IHDR") && bytes.len() >= 24 {
let w = u32::from_be_bytes([bytes[16], bytes[17], bytes[18], bytes[19]]);
let h = u32::from_be_bytes([bytes[20], bytes[21], bytes[22], bytes[23]]);
if let Some((w, h)) = craft_image_codecs::png_probe::declared_dimensions(bytes) {
check_size(F, w as u64, h as u64, opts)?;
}
let ic_opts = craft_image_codecs::DecodeOptions {
@@ -52,106 +50,24 @@ pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
finish(F, raw, meta, (w, h), opts)
}
/// Stored dimensions and EXIF orientation (`eXIf`, wherever it is) from the chunk headers, without
/// inflating the image data. Refused, as a decode would refuse it: an invalid IHDR, an indexed
/// image without a palette, no IDAT, or a file that ends before the chunk after the last IDAT
/// begins (truncated image data). Chunk CRCs and the deflate stream are not checked.
/// Stored dimensions and EXIF orientation without inflating pixels.
pub(crate) fn header(b: &[u8]) -> Result<(u32, u32, u16)> {
if !b.starts_with(b"\x89PNG\r\n\x1a\n") {
return Err(err("bad signature"));
}
let be32 = |p: usize| b.get(p..p.checked_add(4)?).map(|v| u32::from_be_bytes([v[0], v[1], v[2], v[3]]));
if be32(8) != Some(13) || b.get(12..16) != Some(b"IHDR") {
return Err(err("missing IHDR"));
}
let ihdr = b.get(16..29).ok_or_else(|| err("truncated IHDR"))?;
let (w, h) = (u32::from_be_bytes([ihdr[0], ihdr[1], ihdr[2], ihdr[3]]), u32::from_be_bytes([ihdr[4], ihdr[5], ihdr[6], ihdr[7]]));
let (depth, color, compression, filter, interlace) = (ihdr[8], ihdr[9], ihdr[10], ihdr[11], ihdr[12]);
if w > i32::MAX as u32 || h > i32::MAX as u32 {
return Err(err("dimensions out of range"));
}
check_size(F, w as u64, h as u64, &DecodeOptions::default())?;
let depth_ok = match color {
0 => matches!(depth, 1 | 2 | 4 | 8 | 16),
3 => matches!(depth, 1 | 2 | 4 | 8),
2 | 4 | 6 => matches!(depth, 8 | 16),
_ => false,
};
if !depth_ok || compression != 0 || filter != 0 || interlace > 1 {
return Err(err("invalid IHDR"));
}
let (mut p, mut palette, mut idat, mut after_idat, mut exif) = (8usize, false, false, false, None);
while let Some(len) = be32(p) {
let Some(kind) = p.checked_add(4).and_then(|k| b.get(k..k.checked_add(4)?)) else { break };
if idat && kind != b"IDAT" {
// a chunk follows the image data: the IDAT run is complete
after_idat = true;
}
if kind == b"IEND" {
break;
}
let start = p.saturating_add(8);
let data = (len <= i32::MAX as u32).then(|| start.checked_add(len as usize).and_then(|end| b.get(start..end))).flatten();
let Some(data) = data else {
if after_idat {
break; // a damaged chunk after the image data: a decode ignores it too
let (w, h, orientation) =
craft_image_codecs::png_probe::header_with_max_pixels(b, DecodeOptions::default().max_pixels).map_err(|error| match error {
craft_image_codecs::CodecError::LimitExceeded(_) => {
let (w, h) = craft_image_codecs::png_probe::declared_dimensions(b).unwrap_or((0, 0));
Error::TooLarge(w as u64, h as u64)
}
return Err(err("truncated"));
};
match kind {
b"PLTE" => palette = true,
b"IDAT" if after_idat => {} // a stray IDAT after other chunks: the first run is the image
b"IDAT" => {
if color == 3 && !palette {
return Err(err("indexed image without a palette"));
}
idat = true;
}
b"eXIf" if exif.is_none() => exif = Some(data.strip_prefix(b"Exif\0\0").unwrap_or(data)),
_ => {}
}
p = start.saturating_add(data.len()).saturating_add(4);
}
if !idat {
return Err(err("no image data"));
}
if !after_idat {
return Err(err("truncated image data"));
}
Ok((w, h, exif.map(crate::exif::summarize).unwrap_or_default().orientation.unwrap_or(1)))
other => err(other),
})?;
Ok((w, h, orientation))
}
fn container_hint_from_chunks(b: &[u8]) -> Option<SourceSpace> {
if !b.starts_with(b"\x89PNG\r\n\x1a\n") {
return None;
}
let mut has_srgb = false;
let mut gamma: Option<f32> = None;
let mut chrm: Option<[f64; 8]> = None;
let mut p = 8usize;
while let Some(len_bytes) = b.get(p..p + 4) {
let len = u32::from_be_bytes([len_bytes[0], len_bytes[1], len_bytes[2], len_bytes[3]]) as usize;
let Some(kind) = b.get(p + 4..p + 8) else { break };
let Some(payload) = b.get(p + 8..p + 8 + len) else { break };
if kind == b"sRGB" {
has_srgb = true;
} else if kind == b"gAMA" && payload.len() == 4 {
let v = u32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]) as f32 / 100_000.0;
if v > 0.0 && v.is_finite() {
gamma = Some(v);
}
} else if kind == b"cHRM" && payload.len() == 32 {
let mut vals = [0.0f64; 8];
for (i, dst) in vals.iter_mut().enumerate() {
let raw = i32::from_be_bytes([payload[i * 4], payload[i * 4 + 1], payload[i * 4 + 2], payload[i * 4 + 3]]);
*dst = raw as f64 / 100_000.0;
}
chrm = Some(vals);
} else if kind == b"IDAT" || kind == b"IEND" {
break;
}
p = p.saturating_add(12).saturating_add(len);
}
let chunks = craft_image_codecs::png_probe::color_chunks(b)?;
let has_srgb = chunks.srgb;
let gamma = chunks.gamma;
let chrm = chunks.chromaticities;
if has_srgb {
return Some(SourceSpace::named(NamedSpace::Srgb, SpaceOrigin::Container));
}
@@ -190,6 +106,50 @@ fn rgb_to_xyz_d50_checked(space: &RgbSpace) -> Option<Mat3> {
mod tests {
use crate::DecodeOptions;
/// An IFD0 with two Orientation entries (1, then 6): `read_header` and `decode` read it with
/// the same reader, so they agree on the orientation.
#[test]
fn header_and_decode_agree_on_conflicting_orientation_entries() {
let image = craft_image_codecs::Image::from_u8(2, 3, craft_image_codecs::ChannelLayout::Gray, vec![0; 6]).unwrap();
let base = craft_image_codecs::encode(&image, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let mut exif = b"II*\0\x08\0\0\0\x02\0".to_vec();
for value in [1u16, 6] {
exif.extend_from_slice(&0x0112u16.to_le_bytes());
exif.extend_from_slice(&3u16.to_le_bytes());
exif.extend_from_slice(&1u32.to_le_bytes());
exif.extend_from_slice(&value.to_le_bytes());
exif.extend_from_slice(&[0, 0]);
}
exif.extend_from_slice(&0u32.to_le_bytes());
let mut png = base.clone();
let iend = png.len() - 12;
png.splice(iend..iend, png_chunk(*b"eXIf", &exif));
let header = crate::read_header(&png).unwrap();
let decoded = crate::decode(&png, DecodeOptions::default()).unwrap();
assert_eq!((header.width, header.height), (2, 3));
assert_eq!(header.orientation, decoded.orientation);
}
#[test]
fn exif_orientation_mutation_parity() {
let image = craft_image_codecs::Image::from_u8(1, 1, craft_image_codecs::ChannelLayout::Gray, vec![0]).unwrap();
let base = craft_image_codecs::encode(&image, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
let exif = crate::exif::minimal_exif(6);
for at in 0..exif.len() {
for value in [0, 255] {
let mut data = exif.clone();
data[at] = value;
let mut png = base.clone();
let iend = png.len() - 12;
png.splice(iend..iend, png_chunk(*b"eXIf", &data));
let old = crate::exif::legacy_reference(&data);
let shared = crate::exif::summarize(&data);
assert_eq!((shared.orientation, shared.thumbnail, shared.adobe_rgb_hint), (old.orientation, old.thumbnail, old.adobe_rgb_hint));
assert_eq!(super::header(&png).unwrap().2, old.orientation.unwrap_or(1), "at {at}, value {value}");
}
}
}
/// A compressed text chunk is expanded by `png` through our own cap: an XMP document
/// bigger than it is dropped, not inflated. Compressing an 8 MiB string is a few
/// kilobytes — the reverse of what the unbounded `get_text()` path allowed.
+32 -128
View File
@@ -40,7 +40,7 @@ impl<'a> Rd<'a> {
}
}
/// The header, colour mode data and image resources (`craft-psd`), with a reader at the layer and mask section.
/// The header (`craft-psd`), colour mode data and image resources, with a reader at the layer and mask section.
struct Start<'a> {
psb: bool,
channels: usize,
@@ -48,7 +48,8 @@ struct Start<'a> {
height: usize,
depth: u16,
mode: u16,
head: craft_psd::Head,
color_mode_data: &'a [u8],
resources: Vec<craft_psd::ImageResource>,
r: Rd<'a>,
}
@@ -57,13 +58,17 @@ fn start<'a>(bytes: &'a [u8], opts: &DecodeOptions) -> Result<Start<'a>> {
if let (Some(&[h0, h1, h2, h3]), Some(&[w0, w1, w2, w3])) = (bytes.get(14..18), bytes.get(18..22)) {
check_size(F, u64::from(u32::from_be_bytes([w0, w1, w2, w3])), u64::from(u32::from_be_bytes([h0, h1, h2, h3])), opts)?;
}
let head = craft_psd::Head::from_bytes(bytes).map_err(|e| match e {
let h = craft_psd::Header::from_bytes(bytes).map_err(|e| match e {
craft_psd::PsdError::UnexpectedEof { .. } => err("truncated"),
e => err(&e.to_string()),
})?;
let h = &head.header;
check_size(F, u64::from(h.width), u64::from(h.height), opts)?;
let p = usize::try_from(head.layer_section_offset).map_err(|_| err("truncated"))?;
let mut r = Rd { b: bytes, p: 26 };
let cm_len = r.u32()? as usize;
let color_mode_data = r.take(cm_len)?;
let res_len = r.u32()? as usize;
r.take(res_len)?;
// a resource section that doesn't parse is left out: the picture still decodes, without its profile and metadata
let resources = craft_psd::Head::from_bytes(bytes).map(|head| head.resources).unwrap_or_default();
Ok(Start {
psb: h.version.is_psb(),
channels: usize::from(h.channels),
@@ -71,15 +76,15 @@ fn start<'a>(bytes: &'a [u8], opts: &DecodeOptions) -> Result<Start<'a>> {
height: h.height as usize,
depth: h.depth,
mode: h.color_mode.as_u16(),
r: Rd { b: bytes, p },
head,
color_mode_data,
resources,
r,
})
}
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
let Start { psb, channels, width, height, depth, mode, head, mut r } = start(bytes, opts)?;
let color_mode_data = head.color_mode_data.as_slice();
let (icc, exif, xmp) = resources(&head);
let Start { psb, channels, width, height, depth, mode, color_mode_data, resources: res, mut r } = start(bytes, opts)?;
let (icc, exif, xmp) = resources(&res);
// Layer and mask information: only the layer count sign matters (merged transparency).
let lm_len = if psb { r.u64()? } else { u64::from(r.u32()?) };
@@ -237,114 +242,13 @@ pub(crate) fn encode(img: &EncodeImage, meta: &EncodeMeta) -> Result<Vec<u8>> {
if w > MAX_SIDE || h > MAX_SIDE {
return Err(Error::Encode(format!("{w} × {h} is larger than a PSD can hold ({MAX_SIDE} px a side): use TIFF")));
}
let ch = usize::from(img.channels);
let color = if ch <= 2 { 1 } else { 3 };
let (depth, bpc) = match img.samples {
Samples::U8(_) => (8u16, 1usize),
Samples::U16(_) => (16, 2),
let samples = match img.samples {
Samples::U8(s) => craft_psd::flat::Samples::U8(s),
Samples::U16(s) => craft_psd::flat::Samples::U16(s),
Samples::F32(_) => return Err(Error::Encode("32-bit float PSD is not supported: use TIFF".into())),
};
let (wu, hu) = (w as usize, h as usize);
let mut b = Vec::new();
b.extend_from_slice(b"8BPS");
b.extend_from_slice(&1u16.to_be_bytes());
b.extend_from_slice(&[0; 6]);
b.extend_from_slice(&(color as u16).to_be_bytes());
b.extend_from_slice(&h.to_be_bytes());
b.extend_from_slice(&w.to_be_bytes());
b.extend_from_slice(&depth.to_be_bytes());
b.extend_from_slice(&(if color == 1 { 1u16 } else { 3 }).to_be_bytes());
b.extend_from_slice(&0u32.to_be_bytes()); // colour mode data
let res = resource_section(meta);
b.extend_from_slice(&u32::try_from(res.len()).map_err(|_| Error::Encode("metadata too large for PSD".into()))?.to_be_bytes());
b.extend_from_slice(&res);
b.extend_from_slice(&0u32.to_be_bytes()); // layer and mask information: none
// planar samples, one plane per colour channel (big-endian)
let plane = |c: usize| -> Vec<u8> {
let mut p = Vec::with_capacity(wu * hu * bpc);
match img.samples {
Samples::U8(s) => p.extend(s.iter().skip(c).step_by(ch).take(wu * hu)),
Samples::U16(s) => s.iter().skip(c).step_by(ch).take(wu * hu).for_each(|v| p.extend_from_slice(&v.to_be_bytes())),
Samples::F32(_) => {}
}
p
};
if bpc == 1 {
b.extend_from_slice(&1u16.to_be_bytes());
let mut counts = Vec::with_capacity(color * hu * 2);
let mut data = Vec::new();
for c in 0..color {
for row in plane(c).chunks_exact(wu) {
let start = data.len();
packbits(row, &mut data);
let n = u16::try_from(data.len() - start).map_err(|_| Error::Encode("PSD row too long".into()))?;
counts.extend_from_slice(&n.to_be_bytes());
}
}
b.extend_from_slice(&counts);
b.extend_from_slice(&data);
} else {
b.extend_from_slice(&0u16.to_be_bytes());
for c in 0..color {
b.extend_from_slice(&plane(c));
}
}
Ok(b)
}
/// The image resources section for `meta`.
fn resource_section(meta: &EncodeMeta) -> Vec<u8> {
let mut out = Vec::new();
let mut add = |id: u16, data: &[u8]| {
out.extend_from_slice(b"8BIM");
out.extend_from_slice(&id.to_be_bytes());
out.extend_from_slice(&[0, 0]); // empty Pascal name, padded to even
out.extend_from_slice(&(data.len() as u32).to_be_bytes());
out.extend_from_slice(data);
if data.len() % 2 == 1 {
out.push(0);
}
};
if let Some(ppi) = meta.ppi.filter(|p| *p > 0) {
// ResolutionInfo: 16.16 fixed pixels per inch, unit 1 (ppi), display unit 1 (inches), twice
let fixed = (u32::from(ppi) << 16).to_be_bytes();
let one = 1u16.to_be_bytes();
add(1005, &[&fixed[..], &one, &one, &fixed, &one, &one].concat());
}
if let Some(icc) = meta.icc {
add(1039, icc);
}
if let Some(exif) = meta.exif {
add(1058, exif);
}
if let Some(xmp) = meta.xmp {
add(1060, xmp.as_bytes());
}
out
}
/// PackBits: append one encoded row of `src` to `out` (runs of 2+ equal bytes as repeats, the
/// rest as literals of at most 128 bytes).
fn packbits(src: &[u8], out: &mut Vec<u8>) {
let mut rest = src;
while let Some(&b) = rest.first() {
// a run of 2 to 128 equal bytes
let run = rest.iter().take(128).take_while(|&&x| x == b).count();
if run >= 2 {
// 1 - run in 2..=128: -1..=-127, as a signed byte
out.push((1 - run as i16) as i8 as u8);
out.push(b);
rest = rest.get(run..).unwrap_or_default();
continue;
}
// a literal of 1 to 128 bytes: up to the next pair of equal bytes, or the end of the row
let unpaired = rest.windows(2).take(128).take_while(|w| w.first() != w.get(1)).count();
let len = if unpaired.saturating_add(1) == rest.len() { rest.len() } else { unpaired };
let (lit, tail) = rest.split_at_checked(len.clamp(1, 128)).unwrap_or((rest, &[][..]));
out.push(lit.len().saturating_sub(1) as u8);
out.extend_from_slice(lit);
rest = tail;
}
let metadata = craft_psd::flat::Metadata { icc: meta.icc, exif: meta.exif, xmp: meta.xmp, ppi: meta.ppi };
craft_psd::flat::write(w, h, img.channels, samples, metadata).map_err(|error| Error::Encode(error.to_string()))
}
/// Stored dimensions and EXIF orientation (resource 1058) from the header and image resources,
@@ -352,9 +256,8 @@ fn packbits(src: &[u8], out: &mut Vec<u8>) {
/// depth, colour mode or compression, or merged image data that runs past the end of the file
/// (truncated). The run-length data itself is not unpacked.
pub(crate) fn header(bytes: &[u8]) -> Result<(u32, u32, u16)> {
let Start { psb, channels, width, height, depth, mode, head, mut r } = start(bytes, &DecodeOptions::default())?;
let color_mode_data = head.color_mode_data.as_slice();
let (_, exif, _) = resources(&head);
let Start { psb, channels, width, height, depth, mode, color_mode_data, resources: res, mut r } = start(bytes, &DecodeOptions::default())?;
let (_, exif, _) = resources(&res);
let lm_len = if psb { r.u64()? as usize } else { r.u32()? as usize };
r.take(lm_len)?;
let compression = r.u16()?;
@@ -435,8 +338,8 @@ fn unpackbits(mut src: &[u8], row: &mut [u8]) -> Result<()> {
}
/// ICC (1039), EXIF (1058, without its optional `Exif\0\0` prefix) and XMP (1060) resources; the last of each id.
fn resources(head: &craft_psd::Head) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<String>) {
let last = |id: u16| head.resources.iter().rev().find(|r| r.id == id).map(|r| r.data.as_slice());
fn resources(res: &[craft_psd::ImageResource]) -> (Option<Vec<u8>>, Option<Vec<u8>>, Option<String>) {
let last = |id: u16| res.iter().rev().find(|r| r.id == id).map(|r| r.data.as_slice());
let icc = last(1039).map(<[u8]>::to_vec);
let exif = last(1058).map(|d| d.strip_prefix(b"Exif\0\0").unwrap_or(d).to_vec());
let xmp = last(1060).map(|d| String::from_utf8_lossy(d).trim_end_matches('\0').to_string());
@@ -510,14 +413,15 @@ mod tests {
}
#[test]
fn a_damaged_resource_section_is_malformed() {
// a resource section of 4 bytes that is not an `8BIM` record
fn a_damaged_resource_section_is_left_out() {
// a resource section of 4 bytes that is not an `8BIM` record: the picture decodes without it
let mut b = header(3, 1, 1, 8, 3, 0);
b[30..34].copy_from_slice(&4u32.to_be_bytes());
b.splice(34..34, *b"JUNK");
let e = decode(&b, &DecodeOptions::default()).unwrap_err();
assert!(matches!(e, Error::Malformed(_, ref m) if m.contains("signature")), "{e:?}");
assert!(super::header(&b).is_err());
b.extend_from_slice(&[1, 2, 3]);
let d = decode(&b, &DecodeOptions::default()).unwrap();
assert_eq!((d.width, d.height, d.icc.is_none()), (1, 1, true));
assert_eq!(super::header(&b).unwrap(), (1, 1, 1));
}
#[test]
@@ -550,7 +454,7 @@ mod tests {
let rows: [&[u8]; 6] = [&[], &[7], &[1, 1], &[1, 2, 3, 3, 3, 4, 5, 5], &[9; 300], &(0..=255u8).cycle().take(700).collect::<Vec<_>>()];
for row in rows {
let mut enc = Vec::new();
super::packbits(row, &mut enc);
craft_psd::compression::packbits::encode(row, &mut enc);
let mut out = vec![0u8; row.len()];
unpackbits(&enc, &mut out).unwrap();
assert_eq!(out, row);
+27 -2
View File
@@ -1,5 +1,6 @@
//! Format detection from leading bytes.
#[cfg(test)]
use crate::exif::Tiff;
use serde::{Deserialize, Serialize};
@@ -75,7 +76,8 @@ impl Format {
Format::Jxl => Some("built without the `jxl` feature"),
#[cfg(not(feature = "heif"))]
Format::Heif => Some(crate::heif::NOT_IN_BUILD),
Format::Avif => Some("no pure-Rust, permissively licensed AV1 decoder yet"),
#[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))]
Format::Avif => Some("AVIF decoding is not included in this build"),
Format::RawTiffLike | Format::RawOther => Some("camera raw: decode with lightcraft-raw"),
_ => None,
}
@@ -125,7 +127,7 @@ pub fn sniff(b: &[u8]) -> Option<Format> {
return Some(Format::RawTiffLike);
}
if b.starts_with(b"II*\0") || b.starts_with(b"MM\0*") {
return Some(if tiff_is_raw(b) { Format::RawTiffLike } else { Format::Tiff });
return Some(if craft_image_codecs::legacy_tiff_raw_hint(b) { Format::RawTiffLike } else { Format::Tiff });
}
if b.starts_with(b"II+\0") || b.starts_with(b"MM\0+") {
return Some(Format::Tiff);
@@ -167,6 +169,7 @@ fn sniff_bmff(b: &[u8]) -> Option<Format> {
/// Heuristic camera-raw detection for classic TIFF: DNGVersion, the CR2 signature, CFA/LinearRaw
/// photometric or raw-only compressions in IFD0 or its SubIFDs, or a camera Make with 10–16-bit data
/// and no ordinary photometric.
#[cfg(test)]
fn tiff_is_raw(b: &[u8]) -> bool {
if b.len() >= 10 && &b[8..10] == b"CR" {
return true;
@@ -221,6 +224,28 @@ fn tiff_is_raw(b: &[u8]) -> bool {
mod tests {
use super::*;
#[test]
fn raw_tiff_classifier_matches_legacy_on_mutations() {
let fixtures: &[&[u8]] = &[
b"II*\0\x08\0\0\0\x01\0\x12\xc6\x01\0\x04\0\0\0\x01\x04\0\0\0\0\0\0",
b"II*\0\x08\0\0\0\x01\0\x03\x01\x03\0\x01\0\0\0\x09\x80\0\0\0\0\0\0",
b"II*\0\x10\0\0\0CR\x02\0",
];
for fixture in fixtures {
for end in 0..=fixture.len() {
let bytes = &fixture[..end];
assert_eq!(craft_image_codecs::legacy_tiff_raw_hint(bytes), tiff_is_raw(bytes), "truncation {end}");
for i in 0..bytes.len() {
for value in [0, 255] {
let mut changed = bytes.to_vec();
changed[i] = value;
assert_eq!(craft_image_codecs::legacy_tiff_raw_hint(&changed), tiff_is_raw(&changed), "byte {i} value {value} length {end}");
}
}
}
}
}
#[test]
fn magic_numbers() {
assert_eq!(sniff(b"\xFF\xD8\xFF\xE0\0\x10JFIF"), Some(Format::Jpeg));
+12 -35
View File
@@ -1,4 +1,4 @@
//! TIFF decode via `craft-image-codecs` and header/IFD inspection via `craft-tiff`.
//! TIFF decode and header inspection via `craft-image-codecs`.
use crate::convert::{Buf, Meta, Model, Raw, check_size, finish};
use crate::{DecodeOptions, Decoded, Error, Format, Result};
@@ -10,15 +10,9 @@ fn err(e: impl std::fmt::Display) -> Error {
}
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(err)?;
if !complete {
return Err(err("truncated TIFF"));
}
let ifd = tiff.ifds.first().ok_or_else(|| err("no IFD"))?;
let info = ifd.image().map_err(err)?;
check_size(F, info.width as u64, info.height as u64, opts)?;
let orientation = ifd.u16(craft_tiff::tags::ORIENTATION);
let bit_depth = info.bits_per_sample.first().copied().unwrap_or(8) as u8;
let info = craft_image_codecs::tiff_probe::decode_info(bytes, opts.max_pixels).map_err(probe_error)?;
let orientation = info.orientation;
let bit_depth = info.bit_depth;
let ic_opts = craft_image_codecs::DecodeOptions {
limits: craft_image_codecs::Limits {
@@ -68,30 +62,13 @@ pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
/// as a decode would refuse it: a layout or compression we can't decode, or strips/tiles that
/// start past the end of the file (truncated). The compressed data itself is not checked.
pub(crate) fn header(bytes: &[u8]) -> Result<(u32, u32, u16)> {
let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(err)?;
if !complete {
return Err(err("truncated TIFF"));
craft_image_codecs::tiff_probe::header(bytes, DecodeOptions::default().max_pixels).map_err(probe_error)
}
fn probe_error(error: craft_image_codecs::tiff_probe::Error) -> Error {
match error {
craft_image_codecs::tiff_probe::Error::TooLarge { width, height } => Error::TooLarge(u64::from(width), u64::from(height)),
craft_image_codecs::tiff_probe::Error::Unsupported(reason) => Error::Unsupported(F, reason),
craft_image_codecs::tiff_probe::Error::Malformed(message) => err(message),
}
let ifd = tiff.ifds.first().ok_or_else(|| err("no IFD"))?;
let info = ifd.image().map_err(err)?;
check_size(F, info.width as u64, info.height as u64, &DecodeOptions::default())?;
let bits = info.bits_per_sample.first().copied().unwrap_or(1);
if bits < 8 && info.planar == 2 && info.samples_per_pixel > 1 {
return Err(Error::Unsupported(F, "planar TIFF with samples narrower than a byte"));
}
if !(matches!(info.photometric, 0 | 1 | 2 | 3 | 5) || info.photometric == 6 && info.compression == 7) {
return Err(Error::Unsupported(F, "colour type"));
}
// what `craft-image-codecs`'s TIFF decoder decompresses: none, CCITT G4, LZW, JPEG, Deflate, PackBits
if !matches!(info.compression, 1 | 4 | 5 | 7 | 8 | 32773 | 32946) {
return Err(Error::Unsupported(F, "compression method"));
}
let len = bytes.len() as u64;
if info.offsets.is_empty()
|| info.offsets.iter().zip(&info.byte_counts).any(|(&o, &c)| c > 0 && (o >= len || (info.compression == 1 && o.saturating_add(c) > len)))
{
return Err(err("truncated: image data past the end of the file"));
}
let orientation = ifd.u16(craft_tiff::tags::ORIENTATION).filter(|o| (1..=8).contains(o)).unwrap_or(1);
Ok((info.width, info.height, orientation))
}
+129 -61
View File
@@ -831,13 +831,15 @@ pub fn quantizer(quality: u8) -> usize {
/// the samples as they come out (in order: no frame reordering).
pub struct Av1Encoder {
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
ctx: rav1e::Context<u8>,
/// Each frame sent so far: its duration (by input frame number).
#[cfg_attr(not(all(feature = "avif", not(target_arch = "wasm32"))), allow(dead_code))]
enc: craft_video::av1::LargeEncoder,
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
durations: Vec<u64>,
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
returned: usize,
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
failed: bool,
width: usize,
height: usize,
seq_header: Option<Vec<u8>>,
}
impl Av1Encoder {
@@ -848,30 +850,12 @@ impl Av1Encoder {
}
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
{
use rav1e::prelude::*;
let mut ec = EncoderConfig::with_speed_preset(c.speed.clamp(1, 10));
ec.width = w;
ec.height = h;
ec.time_base = Rational::new(1, u64::from(c.fps));
ec.bit_depth = 8;
ec.chroma_sampling = ChromaSampling::Cs420;
ec.pixel_range = PixelRange::Limited;
ec.color_description = Some(ColorDescription {
color_primaries: ColorPrimaries::BT709,
transfer_characteristics: TransferCharacteristics::BT709,
matrix_coefficients: MatrixCoefficients::BT709,
});
// in order, one shown frame per packet: samples map 1:1 to the frames sent
ec.low_latency = true;
let q = quantizer(c.quality);
ec.quantizer = q;
ec.min_quantizer = q.min(255) as u8;
ec.min_key_frame_interval = 1;
ec.max_key_frame_interval = 120;
// tiles are what rav1e encodes in parallel (16 measured best for 1080p on 32 threads)
ec.tiles = std::thread::available_parallelism().map_or(4, |n| n.get()).clamp(2, 16);
let ctx: Context<u8> = Config::new().with_encoder_config(ec).new_context().map_err(|e| bad(format!("AV1: {e}")))?;
Ok(Av1Encoder { ctx, durations: Vec::new(), width: w, height: h, seq_header: None })
use craft_video::av1::{EncoderConfig, RateControl};
let mut ec = EncoderConfig::new(w as u32, h as u32, c.fps, 1);
ec.rate = RateControl::ConstantQ(quantizer(c.quality) as u8);
ec.keyint = 120;
let enc = craft_video::av1::LargeEncoder::with_exact_quantizer(&ec, c.speed).map_err(|e| bad(format!("AV1: {e}")))?;
Ok(Av1Encoder { enc, durations: Vec::new(), returned: 0, failed: false, width: w, height: h })
}
#[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))]
{
@@ -885,10 +869,16 @@ impl Av1Encoder {
(self.width, self.height)
}
/// The configuration record for the container ([`av1_config`]), once the first sample (with
/// the sequence header) has come out.
/// The configuration record for the container (`av1C`), once the first sample has come out.
pub fn config_record(&self) -> Option<Vec<u8>> {
self.seq_header.as_ref().map(|s| av1_config(s))
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
{
self.enc.av1c()
}
#[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))]
{
None
}
}
/// Encode `rgba` (`width × height` of [`Self::size`]) shown for `periods` frame periods;
@@ -898,17 +888,19 @@ impl Av1Encoder {
let (y, u, v) = yuv420(rgba, self.width, self.height)?;
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
{
let mut frame = self.ctx.new_frame();
for (plane, (data, w)) in frame.planes.iter_mut().zip([(&y, self.width), (&u, self.width / 2), (&v, self.width / 2)]) {
plane.copy_from_raw_u8(data, w, 1);
if self.failed {
return Err(bad("AV1 encoder failed earlier"));
}
let params = rav1e::prelude::FrameParameters {
frame_type_override: if key { rav1e::prelude::FrameTypeOverride::Key } else { rav1e::prelude::FrameTypeOverride::No },
..Default::default()
};
self.ctx.send_frame((frame, params)).map_err(|e| bad(format!("AV1: {e}")))?;
let y16: Vec<u16> = y.into_iter().map(u16::from).collect();
let u16_: Vec<u16> = u.into_iter().map(u16::from).collect();
let v16: Vec<u16> = v.into_iter().map(u16::from).collect();
let frame = craft_video::av1::Frame { y: &y16, u: &u16_, v: &v16, y_stride: self.width, uv_stride: self.width / 2 };
self.durations.push(periods.max(1));
self.drain()
let packets = self.enc.push(&frame, key).map_err(|e| {
self.failed = true;
bad(format!("AV1: {e}"))
})?;
self.samples(packets)
}
#[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))]
{
@@ -921,33 +913,38 @@ impl Av1Encoder {
pub fn finish(&mut self) -> Result<Vec<Sample>> {
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
{
self.ctx.flush();
self.drain()
if self.failed {
return Err(bad("AV1 encoder failed earlier"));
}
let packets = self.enc.finish().map_err(|e| {
self.failed = true;
bad(format!("AV1: {e}"))
})?;
let samples = self.samples(packets)?;
if self.returned != self.durations.len() {
self.failed = true;
return Err(bad("AV1 encoder did not return every input frame"));
}
Ok(samples)
}
#[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))]
Err(bad("AV1 video encoding is not available in this build"))
}
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
fn drain(&mut self) -> Result<Vec<Sample>> {
use rav1e::prelude::*;
let mut out = Vec::new();
loop {
match self.ctx.receive_packet() {
Ok(p) => {
let (data, seq) = to_sample_data(&p.data)?;
if self.seq_header.is_none() {
self.seq_header = seq;
}
let duration = usize::try_from(p.input_frameno).ok().and_then(|i| self.durations.get(i)).copied().unwrap_or(1);
out.push(Sample { data, key: p.frame_type == FrameType::KEY, duration });
}
Err(EncoderStatus::Encoded) => {}
Err(EncoderStatus::NeedMoreData | EncoderStatus::LimitReached) => break,
Err(e) => return Err(bad(format!("AV1: {e}"))),
}
fn samples(&mut self, packets: Vec<craft_video::av1::BufferedPacket>) -> Result<Vec<Sample>> {
let mut samples = Vec::with_capacity(packets.len());
for packet in packets {
let index = usize::try_from(packet.input_frame_number).ok();
let duration = index.filter(|&i| i == self.returned).and_then(|i| self.durations.get(i)).copied();
let Some(duration) = duration else {
self.failed = true;
return Err(bad("AV1 encoder returned an unknown or out-of-order frame"));
};
self.returned += 1;
samples.push(Sample { data: packet.packet.data, key: packet.packet.keyframe, duration });
}
Ok(out)
Ok(samples)
}
}
@@ -1141,7 +1138,8 @@ mod tests {
out.extend(e.finish().unwrap());
assert_eq!(out.len(), 4, "one sample per frame sent");
assert!(out[0].key);
assert_eq!(out.iter().map(|s| s.duration).sum::<u64>(), 56);
assert!(out[3].key, "requested key frame");
assert_eq!(out.iter().map(|s| s.duration).collect::<Vec<_>>(), [24, 1, 1, 30]);
for s in &out {
assert!(obus(&s.data).unwrap().iter().all(|(k, _)| *k != OBU_TEMPORAL_DELIMITER));
}
@@ -1155,4 +1153,74 @@ mod tests {
let d = m.finish().unwrap().into_inner();
assert_eq!(boxes(&d, 0..d.len()).len(), 3);
}
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
#[test]
fn av1_buffers_a_large_frame_and_preserves_its_duration() {
let c = VideoConfig { width: 4112, height: 16, fps: 30, quality: 60, speed: 10 };
let mut encoder = Av1Encoder::new(&c).unwrap();
let mut samples = encoder.push(&vec![[80, 120, 180, 255]; 4112 * 16], 7, true).unwrap();
samples.extend(encoder.finish().unwrap());
assert_eq!(samples.len(), 1);
assert_eq!(samples[0].duration, 7);
assert!(samples[0].key);
assert!(encoder.config_record().is_some());
}
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
#[test]
fn av1_preserves_frame_sizes_above_the_old_area_limit() {
for (width, height) in [(5120, 2160), (4096, 4096)] {
let cfg = VideoConfig { width, height, fps: 30, quality: 60, speed: 10 };
let mut encoder = Av1Encoder::new(&cfg).unwrap();
let mut samples = encoder.push(&vec![[80, 120, 180, 255]; width as usize * height as usize], 7, true).unwrap();
samples.extend(encoder.finish().unwrap());
assert_eq!(samples.len(), 1, "{width}x{height}");
assert!(!samples[0].data.is_empty());
assert_eq!(samples[0].duration, 7);
assert!(samples[0].key);
assert!(encoder.config_record().is_some());
}
}
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
#[test]
fn av1_quality_100_preserves_the_exact_zero_quantizer() {
assert_eq!(quantizer(100), 0);
let image: Vec<[u8; 4]> = (0..64 * 48).map(|i| [(i * 13) as u8, (i * 41) as u8, (i * 73) as u8, 255]).collect();
let cfg = VideoConfig { width: 64, height: 48, fps: 24, quality: 100, speed: 10 };
let mut actual = Av1Encoder::new(&cfg).unwrap();
let mut samples = actual.push(&image, 7, true).unwrap();
samples.extend(actual.finish().unwrap());
let (y, u, v) = yuv420(&image, 64, 48).unwrap();
let y: Vec<u16> = y.into_iter().map(u16::from).collect();
let u: Vec<u16> = u.into_iter().map(u16::from).collect();
let v: Vec<u16> = v.into_iter().map(u16::from).collect();
let frame = craft_video::av1::Frame { y: &y, u: &u, v: &v, y_stride: 64, uv_stride: 32 };
let mut ec = craft_video::av1::EncoderConfig::new(64, 48, 24, 1);
ec.rate = craft_video::av1::RateControl::ConstantQ(0);
ec.keyint = 120;
let mut exact = craft_video::av1::LargeEncoder::with_exact_quantizer(&ec, 10).unwrap();
let mut expected = exact.push(&frame, true).unwrap();
expected.extend(exact.finish().unwrap());
assert_eq!(samples.len(), 1);
assert_eq!(samples[0].data, expected[0].packet.data);
assert_eq!(samples[0].duration, 7);
assert!(samples[0].key);
}
#[cfg(all(feature = "avif", not(target_arch = "wasm32")))]
#[test]
fn av1_speed_affects_normal_sized_frames() {
let image: Vec<[u8; 4]> = (0..64 * 48).map(|i| [(i * 13) as u8, (i * 41) as u8, (i * 73) as u8, 255]).collect();
let encode = |speed| {
let c = VideoConfig { width: 64, height: 48, fps: 24, quality: 60, speed };
let mut encoder = Av1Encoder::new(&c).unwrap();
let mut samples = encoder.push(&image, 1, true).unwrap();
samples.extend(encoder.finish().unwrap());
samples.remove(0).data
};
assert_ne!(encode(1), encode(10), "AV1 speed must reach the shared encoder");
}
}
+12
View File
@@ -0,0 +1,12 @@
# AVIF decode fixtures
Small AVIF files from an independent encoder (libaom through FFmpeg 7.1), for `src/codecs/avif_decode.rs`.
- `quad-source.rgb`: the 33×17 source, raw RGB24 four-colour quadrants (red, green, blue, near-white).
- `quad-420-bt709.avif`: 8-bit 4:2:0, limited range, BT.709 matrix, odd size (FFmpeg leaves the primaries and transfer unspecified):
`ffmpeg -f rawvideo -pix_fmt rgb24 -s 33x17 -i quad-source.rgb -c:v libaom-av1 -crf 12 -still-picture 1 -pix_fmt yuv420p -colorspace bt709 -color_primaries bt709 -color_trc iec61966-2-1 -color_range tv quad-420-bt709.avif`
- `quad-420-bt709.dav1d.yuv`: that file's AV1 picture as libdav1d decodes it, planar 8-bit 4:2:0 (Y, then Cb, then Cr):
`ffmpeg -c:v libdav1d -i quad-420-bt709.avif -f rawvideo -pix_fmt yuv420p quad-420-bt709.dav1d.yuv`
- `quad-444-gbr-lossless.avif`: lossless 8-bit 4:4:4 with the identity (GBR) matrix, full range:
`ffmpeg -f rawvideo -pix_fmt rgb24 -s 33x17 -i quad-source.rgb -c:v libaom-av1 -crf 0 -still-picture 1 -pix_fmt gbrp -colorspace rgb -color_range pc quad-444-gbr-lossless.avif`.
It decodes to `quad-source.rgb` exactly.
Binary file not shown.

After

Width:  |  Height:  |  Size: 345 B

+13 -17
View File
@@ -51,23 +51,14 @@ fn metadata_in_final_feed_matches_builder() {
box_bytes(&mut bytes, b"Exif", &exif_box);
box_bytes(&mut bytes, b"xml ", b"<xmp/>");
assert!(bytes.len() < 8192, "metadata must fit in the completing input chunk");
let old = jxl_oxide::JxlImage::builder().read(bytes.as_slice()).unwrap();
let old_exif = match old.aux_boxes().first_exif().unwrap() {
jxl_oxide::AuxBoxData::Data(ex) => ex.payload().to_vec(),
_ => panic!("builder must preserve Exif in its final feed"),
};
let old_xml = match old.aux_boxes().first_xml() {
jxl_oxide::AuxBoxData::Data(xml) => xml.to_vec(),
_ => panic!("builder must preserve XMP in its final feed"),
};
let decoded = decode(&bytes, DecodeOptions::default()).unwrap();
assert_eq!(decoded.exif.as_deref(), Some(old_exif.as_slice()));
assert_eq!(decoded.xmp.as_deref(), Some(std::str::from_utf8(&old_xml).unwrap()));
assert_eq!(decoded.exif.as_deref(), Some(exif.as_slice()));
assert_eq!(decoded.xmp.as_deref(), Some("<xmp/>"));
assert_eq!(decoded.to_srgb8(), pixels);
}
#[test]
fn progressive_truncation_matches_builder() {
fn progressive_truncation_matches_shared_decoder() {
let pixels = Rgba8::from_fn(96, 64, |x, y| {
let mut seed = (y * 96 + x + 1) as u32;
let mut rgb = [0; 4];
@@ -86,11 +77,16 @@ fn progressive_truncation_matches_builder() {
.unwrap();
let raw = codestream(&encoded);
assert!(raw.len() > 4096, "fixture must exercise post-header feeds");
for cut in [0, 1, 2, 12, 4095, 4096, 4097, raw.len() / 2, raw.len() - 1, raw.len()] {
let bytes = &raw[..cut];
// `finalize` permits partial images: rendering determines whether frame 0 is usable.
let old_ok = jxl_oxide::JxlImage::builder().read(bytes).is_ok_and(|image| image.render_frame(0).is_ok());
assert_eq!(decode(bytes, DecodeOptions::default()).is_ok(), old_ok, "lossless={lossless}, cut={cut}/{}", raw.len());
for cut in [0, 1, 2, 12] {
assert!(decode(&raw[..cut], DecodeOptions::default()).is_err(), "lossless={lossless}, cut={cut}/{}", raw.len());
}
// Progressive streams may contain a renderable partial frame. Preserve the
// post-header feed-boundary cases instead of requiring every truncation to fail.
for cut in [4095, 4096, 4097, raw.len() / 2, raw.len() - 1, raw.len()] {
let bytes = &raw[..cut];
let shared_ok = craft_jxl::decode_with_color(bytes, craft_jxl::DecodeLimits::default(), craft_jxl::OutputColor::LinearRec2020).is_ok();
assert_eq!(decode(bytes, DecodeOptions::default()).is_ok(), shared_ok, "lossless={lossless}, cut={cut}/{}", raw.len());
}
assert!(decode(&raw, DecodeOptions::default()).is_ok(), "lossless={lossless}");
}
}
+30 -13
View File
@@ -664,12 +664,31 @@ fn avif_encode_native() {
if cfg!(feature = "avif") {
let b = r.unwrap();
assert_eq!(sniff(&b), Some(Format::Avif));
assert!(matches!(decode(&b, DecodeOptions::default()), Err(Error::Unsupported(Format::Avif, _))));
let decoded = decode(&b, DecodeOptions::default()).unwrap();
assert_eq!((decoded.source_width, decoded.source_height), (64, 48));
} else {
assert!(r.is_err());
}
}
#[test]
fn avif_keeps_exif() {
// The ravif path wrote EXIF; the shared encoder must keep it, for 8- and 16-bit input.
let img = smooth(16, 12);
let exif = minimal_exif(3);
let meta = EncodeMeta { exif: Some(&exif), ..Default::default() };
let wide: Vec<u16> = img.data.iter().flat_map(|p| p.map(|c| u16::from(c) * 257)).collect();
for image in [EncodeImage::rgba8(&img), EncodeImage::new(16, 12, 4, Samples::U16(&wide))] {
let r = encode_avif(&image, 70, 10, &meta);
if cfg!(feature = "avif") {
let b = r.unwrap();
assert!(b.windows(exif.len()).any(|w| w == &exif[..]), "EXIF missing from the AVIF");
} else {
assert!(r.is_err());
}
}
}
#[test]
fn raw_routing() {
let mut b = b"II*\0\x08\0\0\0".to_vec();
@@ -962,23 +981,21 @@ fn jxl_alpha_and_gray() {
#[test]
#[cfg(feature = "jxl")]
fn jxl_hdr_pq() {
// linear Rec. 2020, SDR white = 1.0: the left half at SDR white, the right half four times brighter
// linear Rec. 2020 input, with SDR white = 203 cd/m²; samples are encoded as BT.2020 PQ.
let (w, h) = (64usize, 32usize);
let px: Vec<f32> = (0..w * h).flat_map(|i| [if i % w < w / 2 { 1.0 } else { 4.0 }; 3]).collect();
for lossless in [false, true] {
let o = JxlOptions { lossless, quality: 95, colour: JxlColour::Rec2020Pq, ..Default::default() };
let b = encode_jxl(&EncodeImage::new(w as u32, h as u32, 3, Samples::F32(&px)), &o, &EncodeMeta::default()).unwrap();
// the file's own samples: PQ code values for 203 and 812 cd/m²
let img = jxl_oxide::JxlImage::builder().read(std::io::Cursor::new(&b)).unwrap();
let m = &img.image_header().metadata;
assert_eq!(m.tone_mapping.intensity_target, 10_000.0);
assert!(format!("{:?}", m.colour_encoding).contains("Pq"), "{:?}", m.colour_encoding);
let r = img.render_frame(0).unwrap();
let fb = r.image_all_channels();
let at = |x: usize| fb.buf()[((h / 2) * w + x) * fb.channels() + 1];
for (x, nits) in [(w / 4, HDR_REFERENCE_WHITE_NITS), (w * 3 / 4, 4.0 * HDR_REFERENCE_WHITE_NITS)] {
let (got, want) = (at(x), lightcraft_codecs::encode::pq_encode(nits));
assert!((got - want).abs() < 0.005, "lossless={lossless}: {nits} cd/m² written as {got}, want {want}");
// DecoderDefault keeps the file's PQ transfer encoding, so compare against the PQ codes
// for 203 and 812 cd/m² rather than linear-light input values.
let dec = craft_jxl::decode(&b, craft_jxl::DecodeLimits::default()).unwrap();
assert_eq!(dec.rendered_cicp, Some([9, 16, 0, 1]));
let ch = dec.layout.channels();
let at = |x: usize| dec.pixels[((h / 2) * w + x) * ch + 1];
for (x, want) in [(w / 4, 0.5807f32), (w * 3 / 4, 0.7292f32)] {
let got = at(x);
assert!((got - want).abs() < 0.01, "lossless={lossless}: PQ code {got}, want {want}");
}
}
// float samples are HDR only
+3 -1
View File
@@ -9,6 +9,8 @@ description = "LightCraft engine: session, command registry, history, render job
workspace = true
[dependencies]
craft-plist = { workspace = true }
craft-pdf-print.workspace = true
craft-mime = { workspace = true }
# Paths stored in a Lightroom catalog are followed only to local files or the catalog's share.
craft-docpath = { workspace = true }
@@ -65,7 +67,7 @@ rawnind-model = ["denoise", "lightcraft-denoise/rawnind-model", "lightcraft-deno
# tests record why they skip (`cargo xtask ci` lists the skips)
lightcraft-testkit = { workspace = true }
# HDR JPEG XL tests read the file's colour encoding
jxl-oxide = { workspace = true }
craft-jxl = { workspace = true }
# Lossless synthetic HEIC files (the import test; decoded when the codecs' `heif` feature is on).
craft-heif = { workspace = true, features = ["testdata"] }
libc = { workspace = true }
+3 -2
View File
@@ -249,9 +249,10 @@ impl PreparedExport {
.into_iter()
.flatten()
.any(|value| !value.trim().is_empty());
if metadata.gps.is_some() || embeds_place_name {
if metadata.gps.is_some() || embeds_place_name || lightcraft_meta::embeds_location(&bytes) {
let instead = if lightcraft_raw::probe(&bytes).is_some() { "JPEG, TIFF or DNG" } else { "JPEG or TIFF" };
return Err(format!(
"{label}: can't remove the location from an original file that embeds GPS or place names; export as JPEG or TIFF, or as DNG for a raw photo"
"{label}: can't remove the location from this original file: it contains GPS or place names, or its location metadata could not be fully checked; export as {instead} instead"
));
}
}
+2 -2
View File
@@ -16,8 +16,8 @@ pub const EXTENSIONS: &[&str] = &[
"srw", "psd", "psb", "jxl", "gif", "bmp",
// Decoded when built with the codecs' `heif` feature; otherwise the import reports them as
// failed with the reason ("HEIC/HEIF support isn't included in this build").
"heic", "heif", "avif", // no AV1 decoder yet: reported as failed, like a HEIC without the feature
"iiq", "crw", "mrw", "x3f", "kdc", "mos", "erf", "3fr", "fff", "mef",
"heic", "heif", // AVIF decoding is available in native builds with the codecs' `avif` feature.
"avif", "iiq", "crw", "mrw", "x3f", "kdc", "mos", "erf", "3fr", "fff", "mef",
];
pub fn is_supported(path: &Path) -> bool {
+55 -188
View File
@@ -62,198 +62,43 @@ impl Plist {
}
}
struct Parser<'a> {
s: &'a str,
i: usize,
/// Nesting of the element being read (bounded: a hostile file must not overflow the stack).
depth: usize,
}
/// Deepest `<dict>` / `<array>` nesting read (real looks nest a handful of levels).
const MAX_DEPTH: usize = 64;
/// Index of the `;` that ends a reference starting at `rest` (its `&`). The scan stops at the first
/// byte that cannot appear in a name or a numeric reference, so zero-padded references such as
/// `&#000000097;` are found and each run of name bytes is read once, not once per `&`.
fn entity_end(rest: &str) -> Option<usize> {
let n = rest.bytes().skip(1).take_while(|b| b.is_ascii_alphanumeric() || *b == b'#').count();
(rest.as_bytes().get(n + 1) == Some(&b';')).then_some(n + 1)
}
fn unescape(t: &str) -> String {
let mut out = String::with_capacity(t.len());
let mut rest = t;
while let Some(p) = rest.find('&') {
out.push_str(&rest[..p]);
rest = &rest[p..];
// 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('&'),
"lt" => Some('<'),
"gt" => Some('>'),
"quot" => Some('"'),
"apos" => Some('\''),
_ => ent
.strip_prefix("#x")
.or_else(|| ent.strip_prefix("#X"))
.filter(|h| h.bytes().all(|b| b.is_ascii_hexdigit()))
.and_then(|h| u32::from_str_radix(h, 16).ok())
.or_else(|| ent.strip_prefix('#').filter(|d| d.bytes().all(|b| b.is_ascii_digit())).and_then(|d| d.parse().ok()))
.and_then(char::from_u32),
};
match c {
Some(c) => {
out.push(c);
rest = &rest[end + 1..];
}
None => {
out.push('&');
rest = &rest[1..];
}
}
}
out.push_str(rest);
out
}
impl Parser<'_> {
fn err<T>(&self, what: &str) -> Result<T, String> {
Err(format!("property list: {what} at byte {}", self.i))
}
/// Skip whitespace, the XML declaration, the doctype and comments.
fn skip(&mut self) {
loop {
let r = &self.s[self.i..];
let t = r.trim_start();
self.i += r.len() - t.len();
let close = if t.starts_with("<?") {
"?>"
} else if t.starts_with("<!--") {
"-->"
} else if t.starts_with("<!") {
">"
} else {
return;
};
match t.find(close) {
Some(e) => self.i += e + close.len(),
None => {
self.i = self.s.len();
return;
}
}
}
}
/// The next tag: (name, closing, self-closing).
fn tag(&mut self) -> Result<(String, bool, bool), String> {
self.skip();
let r = &self.s[self.i..];
if !r.starts_with('<') {
return self.err("expected a tag");
}
let Some(end) = r.find('>') else { return self.err("unterminated tag") };
let inner = &r[1..end];
self.i += end + 1;
let closing = inner.starts_with('/');
let selfc = inner.ends_with('/');
let name = inner.trim_start_matches('/').trim_end_matches('/').split_whitespace().next().unwrap_or("").to_string();
Ok((name, closing, selfc))
}
fn peek_close(&mut self, name: &str) -> bool {
self.skip();
let r = &self.s[self.i..];
let t = format!("</{name}>");
if r.starts_with(&t) {
self.i += t.len();
true
} else {
false
}
}
fn text(&mut self, name: &str) -> Result<String, String> {
let r = &self.s[self.i..];
let t = format!("</{name}>");
let Some(end) = r.find(&t) else { return self.err(&format!("missing {t}")) };
self.i += end + t.len();
Ok(unescape(&r[..end]))
}
fn value(&mut self) -> Result<Plist, String> {
if self.depth >= MAX_DEPTH {
return self.err("nested too deeply");
}
self.depth += 1;
let v = self.value_inner();
self.depth -= 1;
v
}
fn value_inner(&mut self) -> Result<Plist, String> {
let (name, closing, selfc) = self.tag()?;
if closing {
return self.err(&format!("unexpected </{name}>"));
}
let leaf = |p: &mut Self, f: &dyn Fn(String) -> Result<Plist, String>| if selfc { f(String::new()) } else { p.text(&name).and_then(f) };
match name.as_str() {
"plist" => {
if selfc {
return Ok(Plist::Dict(Vec::new()));
}
let v = self.value()?;
self.peek_close("plist");
Ok(v)
}
"dict" => {
let mut m = Vec::new();
if !selfc {
while !self.peek_close("dict") {
let (k, c, sc) = self.tag()?;
if k != "key" || c {
return self.err("expected <key>");
}
let key = if sc { String::new() } else { self.text("key")? };
m.push((key, self.value()?));
}
}
Ok(Plist::Dict(m))
}
"array" => {
let mut a = Vec::new();
if !selfc {
while !self.peek_close("array") {
a.push(self.value()?);
}
}
Ok(Plist::Array(a))
}
"string" | "date" => leaf(self, &|t| Ok(Plist::Str(t))),
"real" | "integer" => {
leaf(self, &|t| t.trim().parse::<f64>().map(Plist::Num).map_err(|_| format!("property list: bad number {:?}", t.trim())))
}
"data" => leaf(self, &|_| Ok(Plist::Data)),
"true" | "false" => {
if !selfc {
self.text(&name)?;
}
Ok(Plist::Bool(name == "true"))
}
_ => self.err(&format!("unknown element <{name}>")),
}
}
}
/// Parse an XML property list.
/// Parse XML through the shared bounded codec; binary presets remain unsupported.
pub fn parse_plist(bytes: &[u8]) -> Result<Plist, String> {
if bytes.starts_with(b"bplist") {
return Err("binary property lists are not supported".into());
}
let text = String::from_utf8_lossy(bytes);
let mut p = Parser { s: text.trim_start_matches('\u{feff}'), i: 0, depth: 0 };
p.value()
// The existing wrapped-plist limit counts the <plist> wrapper and root value.
// ZIP expansion remains charged separately by preset_import before this parser runs.
let limits = craft_plist::Limits {
max_input_bytes: MAX_ZIP_ENTRY,
max_expanded_bytes: MAX_ZIP_ENTRY.saturating_mul(2),
max_depth: 62,
..craft_plist::Limits::default()
};
let value =
craft_plist::parse_xml_with_options(bytes, limits, craft_plist::XmlOptions { trim_date_text: false }).map_err(|error| match error {
craft_plist::FormatError::TooLarge(_) => format!("property list: nested too deeply or exceeds resource limits: {error}"),
_ => format!("property list: {error}"),
})?;
map_plist(value)
}
// Luminar mapping deliberately keeps its existing value policy: dates are strings, integer
// settings are f64, and data is a presence marker rather than an app-owned binary payload.
fn map_plist(value: craft_plist::Value) -> Result<Plist, String> {
use craft_plist::{DateValue, Value};
Ok(match value {
Value::Dict(entries) => {
Plist::Dict(entries.into_iter().map(|(key, value)| map_plist(value).map(|value| (key, value))).collect::<Result<_, _>>()?)
}
Value::Array(values) => Plist::Array(values.into_iter().map(map_plist).collect::<Result<_, _>>()?),
Value::String(value) | Value::Date(DateValue::Text(value)) => Plist::Str(value),
Value::Integer(value) => Plist::Num(value as f64),
Value::Real(value) => Plist::Num(value),
Value::Bool(value) => Plist::Bool(value),
Value::Data(_) => Plist::Data,
_ => return Err("property list: unsupported XML value".into()),
})
}
// ------------------------------------------------------------------------------------ mapping
@@ -508,6 +353,10 @@ pub(crate) fn read_mplumpack_within(path: &str, bytes: &[u8], budget: &mut usize
#[cfg(test)]
mod tests {
fn unescape(text: &str) -> String {
let xml = format!("<string>{text}</string>");
super::parse_plist(xml.as_bytes()).unwrap().str().unwrap().to_string()
}
use super::*;
use crate::preset_import::read_presets;
use serde_json::json;
@@ -731,4 +580,22 @@ mod tests {
assert_eq!(r["imported"][0]["group"], json!("Imported Presets"), "{r}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn shared_plist_adapter_keeps_luminar_value_mapping() {
let p = parse_plist(
b"<dict><key>date</key><date> 2026-01-01T00:00:00Z </date><key>integer</key><integer>-3</integer><key>data</key><data>AAEC</data></dict>",
)
.unwrap();
assert_eq!(p.get("date").and_then(Plist::str), Some(" 2026-01-01T00:00:00Z "));
assert_eq!(p.get("integer").and_then(Plist::num), Some(-3.0));
assert_eq!(p.get("data"), Some(&Plist::Data));
}
#[test]
fn shared_plist_refuses_malformed_values_instead_of_repairing_them() {
for xml in ["<data>not base64!</data>", "<integer>1.5</integer>", "<real>NaN</real>", "<plist><string>x</string>", "<string>x</string>junk"] {
assert!(parse_plist(xml.as_bytes()).is_err(), "{xml}");
}
assert!(parse_plist(b"<string>\xff</string>").is_err());
}
}
+13 -328
View File
@@ -1,334 +1,30 @@
//! A small PDF writer for print output: pages of a given size in points, each with raster images
//! placed on it, tagged with their ICC profile (`ICCBased`) so the numbers mean the same colour in
//! every reader and RIP, Flate-compressed (lossless) at 8 or 16 bits per component, or carried as
//! JPEG (`DCTDecode`) where small files matter more than exact samples. Optionally an output intent:
//! the printing condition (the printer / paper profile) the file was made for, as PDF/X readers
//! expect it in the catalog. Profiles shared by several images are written once.
//!
//! The file is PDF 1.6 (16-bit images need 1.5): classic cross-reference table, no object streams,
//! deterministic (no dates or IDs), so the same pages always give the same bytes.
//!
//! Sources: ISO 32000-1:2008 (§7.3 objects, §7.5 file structure, §8.6.5.5 ICCBased colour spaces,
//! §8.9 images, §14.11.5 output intents). Own design.
use std::collections::HashMap;
//! Print-page policy and adaptation to shared PDF raster serialization.
//! Layout, rendering, profile choice and job limits stay in the app.
pub use craft_pdf_print::write_raster::print::{ImageData, OutputIntent, PdfImage, PdfPage, Placement};
#[cfg(test)]
use std::sync::Arc;
/// Pages per document: a print job beyond this is a mistake, not a book.
pub const MAX_PAGES: usize = 1000;
/// Images per page.
pub const MAX_IMAGES_PER_PAGE: usize = 256;
/// Largest page side in points (200 inches): more than any printer's roll.
pub const MAX_PAGE_POINTS: f64 = 14_400.0;
/// Largest image, in pixels per side.
pub const MAX_IMAGE_SIDE: u32 = 65_535;
/// How an image's samples are stored.
#[derive(Clone, Debug, PartialEq)]
pub enum ImageData {
/// Interleaved samples, `bits` (8 or 16) per component, 16-bit values big-endian: compressed
/// losslessly in the file.
Raw { bits: u8, samples: Vec<u8> },
/// A baseline JPEG stream, copied into the file as it is.
Jpeg(Vec<u8>),
/// Samples already compressed with zlib (see [`PdfImage::deflated`]), `bits` per component.
Flate { bits: u8, data: Vec<u8> },
}
/// A raster image and the colour space its samples are in.
#[derive(Clone, Debug, PartialEq)]
pub struct PdfImage {
pub width: u32,
pub height: u32,
/// Components per pixel: 1 (grey), 3 (RGB) or 4 (CMYK).
pub channels: u8,
pub data: ImageData,
/// The ICC profile the samples are in; without one the image is device colour.
pub icc: Option<Arc<Vec<u8>>>,
}
impl PdfImage {
/// An image from interleaved 16-bit samples.
pub fn from_u16(width: u32, height: u32, channels: u8, samples: &[u16], icc: Option<Arc<Vec<u8>>>) -> PdfImage {
let bytes = samples.iter().flat_map(|v| v.to_be_bytes()).collect();
PdfImage { width, height, channels, data: ImageData::Raw { bits: 16, samples: bytes }, icc }
}
/// The same image with its samples compressed now (checked first), so a long print doesn't
/// hold every page's uncompressed samples until the file is written.
pub fn deflated(self) -> Result<PdfImage, String> {
self.check()?;
let data = match self.data {
ImageData::Raw { bits, samples } => ImageData::Flate { bits, data: deflate(&samples) },
other => other,
};
Ok(PdfImage { data, ..self })
}
fn check(&self) -> Result<(), String> {
if self.width == 0 || self.height == 0 || self.width > MAX_IMAGE_SIDE || self.height > MAX_IMAGE_SIDE {
return Err(format!("an image of {} × {} pixels can't be written", self.width, self.height));
}
if !matches!(self.channels, 1 | 3 | 4) {
return Err(format!("images have 1, 3 or 4 components, not {}", self.channels));
}
match &self.data {
ImageData::Raw { bits, samples } => {
if !matches!(bits, 8 | 16) {
return Err(format!("images have 8 or 16 bits per component, not {bits}"));
}
let want = u64::from(self.width) * u64::from(self.height) * u64::from(self.channels) * u64::from(bits / 8);
if samples.len() as u64 != want {
return Err(format!("the image needs {want} bytes of samples, not {}", samples.len()));
}
}
ImageData::Jpeg(data) => {
if !data.starts_with(&[0xFF, 0xD8]) {
return Err("the image isn't a JPEG stream".into());
}
}
ImageData::Flate { bits, data } => {
if !matches!(bits, 8 | 16) || data.is_empty() {
return Err("a compressed image has 8 or 16 bits per component and some data".into());
}
}
}
Ok(())
}
}
/// Where an image goes on its page, in points from the page's bottom-left corner.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Placement {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
/// One page: its size in points and the images on it, drawn in order (later ones on top).
#[derive(Clone, Debug, PartialEq)]
pub struct PdfPage {
pub width: f64,
pub height: f64,
pub images: Vec<(PdfImage, Placement)>,
}
/// The printing condition the document is made for (PDF/X-style output intent).
#[derive(Clone, Debug, PartialEq)]
pub struct OutputIntent {
/// The printer / paper profile.
pub icc: Arc<Vec<u8>>,
/// Its components: 1, 3 or 4.
pub channels: u8,
/// A name for the condition (the profile's description).
pub condition: String,
}
/// Document-level settings.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct PdfInfo {
pub title: Option<String>,
pub output_intent: Option<OutputIntent>,
}
fn finite(v: f64) -> bool {
v.is_finite()
}
/// A number as PDF writes it: up to four decimals, no exponent, no trailing zeros.
fn num(v: f64) -> String {
let s = format!("{:.4}", if finite(v) { v } else { 0.0 });
let s = s.trim_end_matches('0').trim_end_matches('.');
if s == "-0" || s.is_empty() { "0".into() } else { s.into() }
}
/// A text string: plain ASCII as a literal string, anything else as UTF-16BE with a byte order mark.
fn text(s: &str) -> String {
if s.bytes().all(|b| (0x20..0x7f).contains(&b)) {
let mut out = String::with_capacity(s.len() + 2);
out.push('(');
for c in s.chars() {
if matches!(c, '(' | ')' | '\\') {
out.push('\\');
}
out.push(c);
}
out.push(')');
out
} else {
let mut out = String::from("<FEFF");
for unit in s.encode_utf16() {
out.push_str(&format!("{unit:04X}"));
}
out.push('>');
out
}
}
struct Writer {
out: Vec<u8>,
offsets: Vec<usize>,
}
impl Writer {
/// Reserve an object number (filled later by [`Writer::object`]).
fn reserve(&mut self) -> usize {
self.offsets.push(0);
self.offsets.len()
}
fn object(&mut self, id: usize, dict: &str, stream: Option<&[u8]>) {
if let Some(slot) = self.offsets.get_mut(id.wrapping_sub(1)) {
*slot = self.out.len();
}
self.out.extend_from_slice(format!("{id} 0 obj\n{dict}").as_bytes());
if let Some(data) = stream {
self.out.extend_from_slice(b"\nstream\n");
self.out.extend_from_slice(data);
self.out.extend_from_slice(b"\nendstream");
}
self.out.extend_from_slice(b"\nendobj\n");
}
}
fn deflate(data: &[u8]) -> Vec<u8> {
miniz_oxide::deflate::compress_to_vec_zlib(data, 6)
}
/// Write a document. Errors name the first page or image that can't be written.
pub fn write(pages: &[PdfPage], info: &PdfInfo) -> Result<Vec<u8>, String> {
if pages.is_empty() {
return Err("there are no pages to write".into());
}
if pages.len() > MAX_PAGES {
return Err(format!("a document holds at most {MAX_PAGES} pages"));
}
for (n, page) in pages.iter().enumerate() {
let n = n + 1;
if !(finite(page.width) && finite(page.height))
|| page.width <= 0.0
|| page.height <= 0.0
|| page.width > MAX_PAGE_POINTS
|| page.height > MAX_PAGE_POINTS
{
return Err(format!("page {n} has no usable size"));
}
if page.images.len() > MAX_IMAGES_PER_PAGE {
return Err(format!("page {n} has more than {MAX_IMAGES_PER_PAGE} images"));
}
for (image, at) in &page.images {
image.check().map_err(|e| format!("page {n}: {e}"))?;
if ![at.x, at.y, at.width, at.height].into_iter().all(finite) || at.width <= 0.0 || at.height <= 0.0 {
return Err(format!("page {n}: an image has no usable placement"));
}
}
}
if let Some(intent) = &info.output_intent
&& !matches!(intent.channels, 1 | 3 | 4)
{
return Err("the output intent's profile has 1, 3 or 4 components".into());
}
let mut w = Writer { out: Vec::new(), offsets: Vec::new() };
w.out.extend_from_slice(b"%PDF-1.6\n%\xE2\xE3\xCF\xD3\n");
let catalog = w.reserve();
let pages_id = w.reserve();
let info_id = w.reserve();
// ICC profiles, each written once (keyed by content) with its component count
let mut profiles: HashMap<Vec<u8>, usize> = HashMap::new();
let mut profile = |w: &mut Writer, icc: &Arc<Vec<u8>>, n: u8| -> usize {
if let Some(&id) = profiles.get(icc.as_slice()) {
return id;
}
let id = w.reserve();
let data = deflate(icc);
let alternate = match n {
1 => "/DeviceGray",
4 => "/DeviceCMYK",
_ => "/DeviceRGB",
};
w.object(id, &format!("<< /N {n} /Alternate {alternate} /Filter /FlateDecode /Length {} >>", data.len()), Some(&data));
profiles.insert(icc.to_vec(), id);
id
};
let intent_id = info.output_intent.as_ref().map(|intent| profile(&mut w, &intent.icc, intent.channels));
let mut kids = Vec::with_capacity(pages.len());
for page in pages {
let page_id = w.reserve();
let mut xobjects = String::new();
let mut content = String::new();
for (k, (image, at)) in page.images.iter().enumerate() {
let space = match &image.icc {
Some(icc) => format!("[/ICCBased {} 0 R]", profile(&mut w, icc, image.channels)),
None => match image.channels {
1 => "/DeviceGray".into(),
4 => "/DeviceCMYK".into(),
_ => "/DeviceRGB".into(),
},
};
let id = w.reserve();
let (bits, filter, data): (u8, &str, std::borrow::Cow<[u8]>) = match &image.data {
ImageData::Raw { bits, samples } => (*bits, "/FlateDecode", deflate(samples).into()),
ImageData::Flate { bits, data } => (*bits, "/FlateDecode", data.as_slice().into()),
ImageData::Jpeg(jpeg) => (8, "/DCTDecode", jpeg.as_slice().into()),
};
w.object(
id,
&format!(
"<< /Type /XObject /Subtype /Image /Width {} /Height {} /ColorSpace {space} /BitsPerComponent {bits} /Filter {filter} /Length {} >>",
image.width,
image.height,
data.len()
),
Some(&data),
);
xobjects.push_str(&format!("/Im{k} {id} 0 R "));
content.push_str(&format!("q {} 0 0 {} {} {} cm /Im{k} Do Q\n", num(at.width), num(at.height), num(at.x), num(at.y)));
}
let content_id = w.reserve();
let stream = deflate(content.as_bytes());
w.object(content_id, &format!("<< /Filter /FlateDecode /Length {} >>", stream.len()), Some(&stream));
w.object(
page_id,
&format!(
"<< /Type /Page /Parent {pages_id} 0 R /MediaBox [0 0 {} {}] /Resources << /XObject << {xobjects}>> >> /Contents {content_id} 0 R >>",
num(page.width),
num(page.height)
),
None,
);
kids.push(format!("{page_id} 0 R"));
}
w.object(pages_id, &format!("<< /Type /Pages /Count {} /Kids [{}] >>", kids.len(), kids.join(" ")), None);
let intents = match (&info.output_intent, intent_id) {
(Some(intent), Some(id)) => format!(
" /OutputIntents [<< /Type /OutputIntent /S /GTS_PDFX /OutputConditionIdentifier (Custom) /Info {} /DestOutputProfile {id} 0 R >>]",
text(&intent.condition)
),
_ => String::new(),
};
w.object(catalog, &format!("<< /Type /Catalog /Pages {pages_id} 0 R{intents} >>"), None);
let title = info.title.as_deref().map(|t| format!(" /Title {}", text(t))).unwrap_or_default();
w.object(info_id, &format!("<< /Producer (LightCraft){title} >>"), None);
let xref = w.out.len();
let mut table = format!("xref\n0 {}\n0000000000 65535 f \n", w.offsets.len() + 1);
for offset in &w.offsets {
table.push_str(&format!("{offset:010} 00000 n \n"));
}
table.push_str(&format!("trailer\n<< /Size {} /Root {catalog} 0 R /Info {info_id} 0 R >>\nstartxref\n{xref}\n%%EOF\n", w.offsets.len() + 1));
w.out.extend_from_slice(table.as_bytes());
Ok(w.out)
craft_pdf_print::write_raster::print::write(
pages,
&craft_pdf_print::write_raster::print::PdfInfo {
title: info.title.clone(),
output_intent: info.output_intent.clone(),
producer: "LightCraft".into(),
},
)
}
#[cfg(test)]
mod tests {
use super::*;
use craft_pdf_print::write_raster::print::MAX_PAGES;
use lightcraft_codecs::NamedSpace;
/// The body of object `id` (dictionary and raw stream bytes), found through the xref table.
@@ -506,15 +202,4 @@ mod tests {
let info = PdfInfo { title: None, output_intent: Some(OutputIntent { icc: srgb(), channels: 2, condition: String::new() }) };
assert!(write(&[page(vec![])], &info).is_err());
}
#[test]
fn numbers_and_strings() {
assert_eq!(num(612.0), "612");
assert_eq!(num(72.5), "72.5");
assert_eq!(num(-0.00001), "0");
assert_eq!(num(f64::NAN), "0");
assert_eq!(num(1.0 / 3.0), "0.3333");
assert_eq!(text("a(b)c\\"), "(a\\(b\\)c\\\\)");
assert_eq!(text("é"), "<FEFF00E9>");
}
}
+165 -8
View File
@@ -731,6 +731,23 @@ fn hdr_avif_export_hlg() {
assert!(!hdr.bytes.windows(4).any(|w| w == b"clli"));
}
/// An exported AVIF's metadata reads back (issue #693: HEIF-family files had no metadata reader).
/// avif-serialize writes the EXIF as an `Exif` item in two extents, its offset field and the TIFF.
#[test]
fn exported_avif_metadata_reads_back() {
use lightcraft_catalog::{Catalog, Photo, PhotoId, Source};
let mut p = Photo::new(PhotoId(1), Source::Demo { scene: 0 }, "a.jpg", "jpeg", 10, 10, "2026-09-30T00:00:00");
p.meta.camera = "Synthetic X2".into();
p.captured = Some("2026-10-12T12:34:56".into());
let m = crate::export::export_metadata(&p, &Catalog::new(), &ExportOptions::default()).unwrap();
let img = lightcraft_raster::Rgba8::new(48, 32);
let avif = crate::export::encode_with_metadata(&img, &ExportOptions::from_json(&json!({"format": "avif", "quality": 60})), Some(&m)).unwrap();
assert_eq!(lightcraft_codecs::sniff(&avif), Some(lightcraft_codecs::Format::Avif));
let back = lightcraft_meta::extract(&avif);
assert_eq!(back.model.as_deref(), Some("Synthetic X2"));
assert_eq!(back.capture_time.map(|d| d.to_iso()).as_deref(), Some("2026-10-12T12:34:56"));
}
/// "Remove location info" holds for the file-copying formats too: the sidecar and the DNG's
/// packet carry no location, and an original that embeds GPS is refused rather than copied.
#[test]
@@ -774,6 +791,7 @@ fn remove_location_holds_for_original_and_dng_exports() {
assert!(lightcraft_meta::extract(&e.bytes).gps.is_some());
let err = export_photo(&mut s, jid, &opts("original", true), 1).err().unwrap();
assert!(err.contains("can't remove the location") && err.contains("export as JPEG"), "{err}");
assert!(!err.contains("DNG"), "a JPEG can't be exported as DNG: {err}");
let _ = std::fs::remove_dir_all(&dir);
}
@@ -1000,7 +1018,147 @@ fn remove_location_refuses_a_raw_that_embeds_gps() {
s.execute("library.import", &json!({"paths": [raw.to_string_lossy()]})).unwrap();
let id = s.catalog.photos().next().unwrap().id;
let err = export_photo(&mut s, id, &ExportOptions::from_json(&json!({"format": "original", "removeLocation": true})), 1).err().unwrap();
assert!(err.contains("can't remove the location"), "{err}");
assert!(err.contains("can't remove the location") && err.contains("DNG"), "a raw can go out as DNG: {err}");
let _ = std::fs::remove_dir_all(&dir);
}
/// A HEIC whose only GPS is in its thumbnail's EXIF (the pictures have no data: an original's
/// export never decodes it). Item 1 is the photo, 2 its thumbnail, 3 the thumbnail's EXIF, kept in
/// the `meta` box's `idat`.
fn heic_with_gps_in_its_thumbnail() -> Vec<u8> {
let gps = lightcraft_meta::Gps { latitude: 48.85, longitude: 2.35, altitude: None };
let exif = [&[0u8, 0, 0, 0][..], &lightcraft_meta::write_exif(&lightcraft_meta::Metadata { gps: Some(gps), ..Default::default() })].concat();
heic_with_metadata_in_its_thumbnail(b"Exif", &exif)
}
fn heic_with_metadata_in_its_thumbnail(kind: &[u8; 4], payload: &[u8]) -> Vec<u8> {
let bx = |kind: &[u8], body: &[u8]| [&((body.len() + 8) as u32).to_be_bytes()[..], kind, body].concat();
let full = |kind: &[u8], body: &[u8]| bx(kind, &[&[0u8, 0, 0, 0][..], body].concat());
let infe = |id: u8, kind: &[u8]| {
let fields = if kind == b"mime" { b"\0application/rdf+xml\0".as_slice() } else { b"\0" };
bx(b"infe", &[&[2, 0, 0, 0, 0, id, 0, 0][..], kind, fields].concat())
};
let iinf = full(b"iinf", &[&[0, 3][..], &infe(1, b"hvc1"), &infe(2, b"hvc1"), &infe(3, kind)].concat());
let iref = full(b"iref", &[bx(b"thmb", &[0, 2, 0, 1, 0, 1]), bx(b"cdsc", &[0, 3, 0, 1, 0, 2])].concat());
// `iloc` version 1, 4-byte offsets and lengths: item 3 from `idat` (construction method 1)
let mut iloc = vec![1, 0, 0, 0, 0x44, 0, 0, 1, 0, 3, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0];
iloc.extend_from_slice(&(payload.len() as u32).to_be_bytes());
let meta = full(b"meta", &[full(b"pitm", &[0, 1]), iinf, iref, bx(b"iloc", &iloc), bx(b"idat", payload)].concat());
[bx(b"ftyp", b"heic\0\0\0\0mif1heic"), meta].concat()
}
/// "Remove location info" refuses an original that embeds GPS anywhere it can be read, here only
/// in a HEIC's thumbnail (#693 review), and doesn't offer DNG for a photo that isn't a raw.
#[test]
fn remove_location_refuses_a_heic_whose_thumbnail_has_gps() {
use lightcraft_catalog::{Op, Photo, Source};
let dir = temp_dir("export-noloc-heic");
let path = dir.join("IMG_0003.HEIC");
std::fs::write(&path, heic_with_gps_in_its_thumbnail()).unwrap();
let mut s = Session::new().with_fs();
let id = s.catalog.alloc_photo_id();
let p = Photo::new(id, Source::File { path: path.to_string_lossy().into() }, "IMG_0003.HEIC", "HEIC", 32, 32, "2026-10-12T00:00:00");
s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
let opts = |remove: bool| ExportOptions::from_json(&json!({"format": "original", "removeLocation": remove}));
assert!(export_photo(&mut s, id, &opts(false), 1).is_ok(), "copied as is when the location may stay");
let err = export_photo(&mut s, id, &opts(true), 1).err().expect("refused: the thumbnail's EXIF has GPS");
assert!(err.contains("can't remove the location") && err.contains("export as JPEG or TIFF"), "{err}");
let _ = std::fs::remove_dir_all(&dir);
}
/// A listed metadata item with an unreadable placement is not proof that an original has no GPS.
/// Exercise the real file-copy path; these damaged containers do not need a working decoder.
#[test]
fn remove_location_refuses_heic_with_unresolved_iloc() {
use lightcraft_catalog::{Op, Photo, Source};
let dir = temp_dir("export-noloc-heic-iloc");
let path = dir.join("Placement.HEIC");
let mut s = Session::new().with_fs();
let id = s.catalog.alloc_photo_id();
let p = Photo::new(id, Source::File { path: path.to_string_lossy().into() }, "Placement.HEIC", "HEIC", 32, 32, "2026-10-12T00:00:00");
s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
let opts = |remove: bool| ExportOptions::from_json(&json!({"format": "original", "removeLocation": remove}));
let original = heic_with_gps_in_its_thumbnail();
assert!(lightcraft_meta::embeds_location(&original), "the intact fixture has GPS");
let kind_at = original.windows(4).position(|w| w == b"iloc").unwrap();
let body = kind_at + 4;
for damage in ["none", "missing", "version", "field size", "truncated entry"] {
let mut bytes = original.clone();
match damage {
"missing" => bytes[kind_at..body].copy_from_slice(b"free"),
"version" => bytes[body] = 3,
"field size" => bytes[body + 4] = 0x24,
"truncated entry" => bytes[body + 15] = 2, // two extents declared, only one present
_ => {}
}
std::fs::write(&path, &bytes).unwrap();
assert_eq!(export_photo(&mut s, id, &opts(false), 1).unwrap().bytes, bytes, "location may stay: {damage}");
let err = export_photo(&mut s, id, &opts(true), 1).err().expect("unresolved GPS must not be copied");
assert!(err.contains("can't remove the location") && err.contains("export as JPEG or TIFF"), "{damage}: {err}");
}
// Ordinary files without GPS still export, with and without an EXIF item.
for exif in [None, Some(lightcraft_meta::write_exif(&lightcraft_meta::Metadata { model: Some("Synthetic".into()), ..Default::default() }))] {
let mut spec = craft_heif::testdata::Spec::new(32, 32, &|_, _| [0, 0, 0]);
spec.exif = exif;
let bytes = craft_heif::testdata::build(&spec);
std::fs::write(&path, &bytes).unwrap();
assert_eq!(export_photo(&mut s, id, &opts(true), 1).unwrap().bytes, bytes);
}
let _ = std::fs::remove_dir_all(&dir);
}
/// A damaged item catalog cannot authorize copying the GPS bytes that `iloc` still references.
#[test]
fn remove_location_refuses_heic_with_incomplete_exif_catalog() {
let original = heic_with_gps_in_its_thumbnail();
let idat = original.windows(4).position(|w| w == b"idat").unwrap();
assert!(lightcraft_meta::try_read_exif(&original[idat + 8..]).unwrap().gps.is_some(), "real EXIF payload");
assert_incomplete_catalog_refused(&original, "exif");
}
#[test]
fn remove_location_refuses_heic_with_incomplete_xmp_catalog() {
let gps =
lightcraft_meta::Metadata { gps: Some(lightcraft_meta::Gps { latitude: 48.85, longitude: 2.35, altitude: None }), ..Default::default() };
let xmp = lightcraft_meta::write_xmp(&gps, None);
assert!(lightcraft_meta::parse_xmp(&xmp).unwrap().metadata.gps.is_some(), "real XMP payload");
assert_incomplete_catalog_refused(&heic_with_metadata_in_its_thumbnail(b"mime", xmp.as_bytes()), "xmp");
}
fn assert_incomplete_catalog_refused(original: &[u8], label: &str) {
use lightcraft_catalog::{Op, Photo, Source};
let dir = temp_dir(&format!("export-noloc-heic-catalog-{label}"));
let path = dir.join("Catalog.HEIC");
let mut s = Session::new().with_fs();
let id = s.catalog.alloc_photo_id();
let p = Photo::new(id, Source::File { path: path.to_string_lossy().into() }, "Catalog.HEIC", "HEIC", 32, 32, "2026-10-12T00:00:00");
s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
let opts = |remove: bool| ExportOptions::from_json(&json!({"format": "original", "removeLocation": remove}));
let iinf = original.windows(4).position(|w| w == b"iinf").unwrap();
let infe = original.windows(4).rposition(|w| w == b"infe").unwrap();
let idat = original.windows(4).position(|w| w == b"idat").unwrap();
assert!(lightcraft_meta::embeds_location(original), "intact GPS container");
for damage in ["truncated type", "missing catalog", "unsupported table", "unsupported entry", "unknown entry", "truncated catalog"] {
let mut bytes = original.to_vec();
match damage {
"truncated type" => bytes[infe - 4..infe].copy_from_slice(&18u32.to_be_bytes()),
"missing catalog" => bytes[iinf..iinf + 4].copy_from_slice(b"free"),
"unsupported table" => bytes[iinf + 4] = 2,
"unsupported entry" => bytes[infe + 4] = 4,
"unknown entry" => bytes[infe..infe + 4].copy_from_slice(b"free"),
"truncated catalog" => bytes[iinf - 4..iinf].copy_from_slice(&u32::MAX.to_be_bytes()),
other => panic!("unknown damage {other}"),
}
assert_eq!(&bytes[idat..], &original[idat..], "GPS bytes retained");
std::fs::write(&path, &bytes).unwrap();
assert_eq!(export_photo(&mut s, id, &opts(false), 1).unwrap().bytes, bytes, "location may stay: {damage}");
let result = export_photo(&mut s, id, &opts(true), 1);
assert!(result.is_err(), "Remove location copied the original with {damage}");
let err = result.err().unwrap();
assert!(err.contains("can't remove the location") && err.contains("export as JPEG or TIFF"), "{damage}: {err}");
}
let _ = std::fs::remove_dir_all(&dir);
}
@@ -1128,16 +1286,15 @@ fn hdr_jxl_export_is_pq_rec2020() {
s.execute("develop.hdr", &json!({"enabled": true, "maxEv": 2})).unwrap();
let hdr = export_photo(&mut s, id, &o, 1).unwrap();
let encoding = |b: &[u8]| {
let img = jxl_oxide::JxlImage::builder().read(std::io::Cursor::new(b)).unwrap();
let m = &img.image_header().metadata;
(format!("{:?}", m.colour_encoding), m.bit_depth.bits_per_sample())
let dec = craft_jxl::decode(b, craft_jxl::DecodeLimits::default()).unwrap();
(dec.rendered_cicp, dec.bit_depth)
};
let (c, bits) = encoding(&hdr.bytes);
assert!(c.contains("Bt2100") && c.contains("Pq"), "{c}");
let (cicp, bits) = encoding(&hdr.bytes);
assert_eq!(cicp, Some([9, 16, 0, 1]), "{cicp:?}");
assert_eq!(bits, 16);
// a photo without an HDR edit exports as an ordinary SDR file
let (c, _) = encoding(&sdr.bytes);
assert!(!c.contains("Pq"), "{c}");
let (cicp, _) = encoding(&sdr.bytes);
assert_ne!(cicp.map(|c| c[1]), Some(16), "{cicp:?}");
}
#[test]
+66
View File
@@ -3173,6 +3173,72 @@ fn a_file_arriving_beside_its_raws_stem_reads_its_own_sidecar() {
let _ = std::fs::remove_dir_all(&src);
}
#[test]
fn heic_exif_reaches_the_catalog_and_all_metadata() {
use craft_heif::testdata::{Spec, build};
let photo = |x: u32, y: u32| [((x * 7 + y * 13) % 200 + 30) as u16, 110, 150];
let exif = lightcraft_meta::write_exif(&lightcraft_meta::Metadata {
make: Some("Synthetic".into()),
model: Some("Issue sweep camera".into()),
capture_time: lightcraft_meta::DateTime::parse_iso("2026-10-12T12:34:56"),
..Default::default()
});
let bytes = build(&Spec { exif: Some(exif), ..Spec::new(96, 64, &photo) });
let m = lightcraft_meta::extract(&bytes);
assert_eq!(m.model.as_deref(), Some("Issue sweep camera"));
assert_eq!(m.capture_time.map(|d| d.to_iso()).as_deref(), Some("2026-10-12T12:34:56"));
if !lightcraft_codecs::Format::Heif.can_decode() {
return; // importing needs the decoder; `heic_imports_as_a_normal_photo_or_says_why_not` checks the error
}
let src = temp_dir("heic-exif");
std::fs::write(src.join("IMG_0002.HEIC"), &bytes).unwrap();
let mut s = Session::new().with_fs();
let r = s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
assert_eq!(ids(&r, "imported"), 1, "{r}");
let p = s.catalog.photos().next().unwrap().clone();
assert_eq!((p.meta.camera.as_str(), p.captured.as_deref()), ("Synthetic Issue sweep camera", Some("2026-10-12T12:34:56")));
let all = s.execute("photo.allMetadata", &json!({"id": p.id.0})).unwrap();
assert!(all["exif"].as_array().unwrap().iter().any(|r| r["name"] == "Model" && r["value"] == "Issue sweep camera"), "{all}");
let _ = std::fs::remove_dir_all(&src);
}
fn heic_camera_exif(date: &str) -> Vec<u8> {
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t};
let mut exif = IfdBuilder::new();
exif.set(t::DATE_TIME_ORIGINAL, Value::Ascii(date.into()));
exif.set(t::OFFSET_TIME_ORIGINAL, Value::Ascii("-05:00".into()));
let mut ifd0 = IfdBuilder::new();
ifd0.set(t::MAKE, Value::Ascii("Apple".into()));
ifd0.set(t::MODEL, Value::Ascii("iPhone 15 Pro".into()));
ifd0.set(t::ORIENTATION, Value::Short(vec![6]));
ifd0.set_child(t::EXIF_IFD, exif);
TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap()
}
#[test]
fn heic_exif_orientation_does_not_rotate_the_picture_twice() {
use craft_heif::testdata::{Prop, Spec, build};
let photo = |x: u32, y: u32| [((x * 7 + y * 13) % 200 + 30) as u16, 110, 150];
for (props, size) in [(vec![], (96, 64)), (vec![Prop::Irot(3)], (64, 96))] {
let plain = build(&Spec { props: props.clone(), ..Spec::new(96, 64, &photo) });
let bytes = build(&Spec { props, exif: Some(heic_camera_exif("2026:10:18 13:00:00")), ..Spec::new(96, 64, &photo) });
assert_eq!(lightcraft_meta::extract(&bytes).orientation, Some(lightcraft_geom::Orientation::Rotate90));
if lightcraft_codecs::Format::Heif.can_decode() {
let header = lightcraft_codecs::read_header(&bytes).unwrap();
assert_eq!((header.width, header.height, header.orientation), (size.0, size.1, 1));
let info = crate::files::probe_bytes("rotated.HEIC", &bytes).unwrap();
assert_eq!((info.width, info.height), size);
let (image, _) = crate::files::load_bytes(&bytes, 96).unwrap();
let (without_exif, _) = crate::files::load_bytes(&plain, 96).unwrap();
assert_eq!((image.width, image.height), (size.0 as usize, size.1 as usize));
assert_eq!(image.data, without_exif.data, "Exif must not turn the decoded pixels again");
} else {
assert!(crate::files::probe_bytes("rotated.HEIC", &bytes).unwrap_err().contains("isn't included in this build"));
assert!(crate::files::load_bytes(&bytes, 96).unwrap_err().contains("isn't included in this build"));
}
}
}
/// A batch-edit recipe changes the imported photos' actual develop settings (not just an option
/// label), renders, is one undo step, and can be saved as a preset that stores only what it names.
#[test]
+1
View File
@@ -13,6 +13,7 @@ craft-raw = { workspace = true }
craft-tiff = { workspace = true }
craft-metadata = { workspace = true }
craft-psd = { workspace = true }
craft-heif = { workspace = true, features = ["metadata"] }
lightcraft-geom = { workspace = true }
serde = { workspace = true }
chrono = { workspace = true }
+9 -2
View File
@@ -8,12 +8,13 @@ Photo metadata: EXIF, XMP (read + write), IPTC-IIM, and the containers that carr
|---|---|
| `read_exif(&[u8]) -> Metadata` | TIFF-structured Exif block (whole TIFF/DNG/raw file, or JPEG APP1 payload with/without `Exif\0\0`). `try_read_exif` reports header errors. |
| `from_tiff(&Tiff) -> Metadata` | Same, from an already parsed stream (used by `lightcraft-raw`). |
| `embedded(&[u8]) -> Embedded` | Sniffs JPEG / PNG / WebP / TIFF / PSD / PSB and returns the Exif, XMP (+ extended XMP), ICC and IPTC blocks. PSD/PSB EXIF resource 1058 and XMP resource 1060 are read from the bounded image-resource section without decoding pixels. `jpeg_segments`, `png_chunks`, `webp_chunks` are the per-container walkers. |
| `embedded(&[u8]) -> Embedded` | Sniffs JPEG / PNG / WebP / TIFF / PSD / PSB / Fujifilm RAF / Canon CR3 / HEIF (HEIC, AVIF) and returns the Exif, XMP (+ extended XMP), ICC and IPTC blocks. PSD/PSB EXIF resource 1058 and XMP resource 1060 are read from the bounded image-resource section without decoding pixels. `jpeg_segments`, `png_chunks`, `webp_chunks` are the per-container walkers; HEIF gives the primary image's `Exif` and XMP items, read without decoding (`src/heif.rs`). |
| `parse_xmp(&str) -> Result<XmpData>` | Metadata + the opaque `lc:settings` JSON + every property (`prefix:name`, structs flattened as `a:b/c:d`). |
| `write_xmp(&Metadata, Option<&str>) -> String` | Complete `<?xpacket?>` packet; the second argument is LightCraft's full develop settings JSON. |
| `parse_iptc(&[u8]) -> Metadata` | IIM record 2: title (2:5), keywords (2:25), by-line (2:80), copyright (2:116), caption (2:120), date/time created (2:55/2:60). |
| `extract(&[u8]) -> Metadata` | Everything for a whole file. Precedence: EXIF → IPTC fills gaps → XMP overrides user fields (rating, label, title, caption, artist, copyright, keywords, GPS) and fills the rest. |
| `try_extract(&[u8]) -> Result<Metadata, ExtractError>` | Like `extract`, but reports malformed PSD/PSB headers, image-resource sections, or embedded EXIF/XMP structure (including duplicate EXIF/XMP resources) so callers can fail closed when location absence cannot be established. Other formats remain best-effort. |
| `embeds_location(&[u8]) -> bool` | Whether a file carries GPS anywhere it can be read from (export's "remove location" check): for HEIF every Exif and XMP item, whichever image it describes, with unreadable items and incomplete item catalogs counted as a location; otherwise what `extract` reads. |
`Metadata` carries camera (make, model, serial, software), lens (make, model, serial, specification), capture
(date-time with UTC offset and milliseconds, exposure time, f-number, ISO incl. extended ISO, focal length and
@@ -46,6 +47,7 @@ No panics on malformed input: proptests feed random bytes to every container wal
XMP parser; truncation tests cut a sample JPEG at every byte and exercise truncated PSD/PSB headers and
image-resource records with hostile declared lengths.
PSD/PSB XMP resources require valid UTF-8; trailing NUL padding is trimmed after validation.
The HEIF truncation and byte-damage tests cover bounded box and item walks; the reader walks `iloc` once.
ISO capture-time parsing requires a complete clock and consumes every clock and fractional-second character. Invalid
XMP capture-time values remain available in the raw property map but are not projected into `Metadata`; a later valid
@@ -57,4 +59,9 @@ CIPA DC-008 (Exif 2.32), TIFF 6.0, Adobe DNG 1.7 (tag numbers), ISO 16684-1 / Ad
Parts 1–3 (packet, RDF serialisation, standard schemas, JPEG extended XMP), IPTC-IIM 4.2 and the IPTC Photo
Metadata Standard, ITU-T T.81 (JPEG markers), ICC.1 (APP2 embedding), PNG Specification 3rd ed. (`eXIf`,
`iTXt`), the WebP RIFF container specification, Adobe Photoshop File Format Specification (image resource
blocks). XML parsing: `quick-xml` (MIT). No third-party metadata code was consulted.
blocks), ISO/IEC 14496-12 (ISO base media file format: boxes, `meta`, `iinf`, `iloc`, `iref`, `idat`) and
ISO/IEC 23008-12 (HEIF: items, `pitm`, `cdsc` and `dimg` references, the `Exif` item of annex A). XML parsing:
`quick-xml` (MIT). No third-party metadata reader was consulted (heic-rs's metadata test file, MIT / Apache-2.0,
was glanced at only while looking for sample files; its reader was not read). For a HEIF test, the code of avif-serialize
(BSD-3-Clause, the AVIF writer behind our AVIF export) was read only to learn how it lays out the `Exif` item it
writes; files from Apple ImageIO, libheif and ExifTool were examined as files only.
+392 -238
View File
@@ -1,257 +1,67 @@
//! HEIF / HEIC / AVIF metadata: the `Exif` and XMP items of the ISO base media file container
//! (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
//! `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
//! type, and an item location box (`iloc`) saying where its bytes are: in the file, or in the
//! `idat` box next to it. The `Exif` item is a four-byte offset followed by a TIFF stream; the XMP
//! packet is a `mime` item whose content type is `application/rdf+xml`. The ICC profile is an item
//! *property*, not an item, and is read by the decoder instead.
//! HEIF, HEIC and AVIF metadata through the shared decoder-independent item reader.
use crate::Embedded;
pub use craft_heif::items::is_heif;
const MAX_BOXES: usize = 4096;
#[derive(Clone, Copy)]
struct BoxRef {
kind: [u8; 4],
body: usize,
end: usize,
}
fn be16(b: &[u8], at: usize) -> Option<u16> {
let s = b.get(at..at + 2)?;
Some(u16::from_be_bytes([s[0], s[1]]))
}
fn be32(b: &[u8], at: usize) -> Option<u32> {
let s = b.get(at..at + 4)?;
Some(u32::from_be_bytes([s[0], s[1], s[2], s[3]]))
}
fn be64(b: &[u8], at: usize) -> Option<u64> {
let s = b.get(at..at + 8)?;
Some(u64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]]))
}
fn boxes(bytes: &[u8], mut at: usize, end: usize, budget: &mut usize) -> Vec<BoxRef> {
let mut out = Vec::new();
let end = end.min(bytes.len());
while at + 8 <= end && *budget > 0 {
*budget -= 1;
let Some(size32) = be32(bytes, at) else { break };
let Some(kind_slice) = bytes.get(at + 4..at + 8) else { break };
let mut kind = [0u8; 4];
kind.copy_from_slice(kind_slice);
let (hdr, box_end) = match size32 {
0 => (8, end),
1 => {
let Some(s64) = be64(bytes, at + 8) else { break };
let Ok(s) = usize::try_from(s64) else { break };
if s < 16 {
break;
}
(16, at.saturating_add(s).min(end))
}
s if s >= 8 => (8, at.saturating_add(s as usize).min(end)),
_ => break,
};
if at + hdr > box_end {
break;
}
out.push(BoxRef { kind, body: at + hdr, end: box_end });
if box_end <= at {
break;
}
at = box_end;
}
out
}
/// Brands that declare a still image in the ISO base media container.
const IMAGE_BRANDS: [&[u8]; 12] = [b"heic", b"heix", b"hevc", b"hevx", b"heim", b"heis", b"hevm", b"hevs", b"mif1", b"msf1", b"avif", b"avis"];
/// Longest `ftyp` box this reads brands from.
const MAX_FTYP: usize = 4096;
/// Most items one file's `iinf` / `iloc` describes (an iPhone HEIC has a handful).
const MAX_ITEMS: usize = 1024;
/// Most extents one item's bytes are split into.
const MAX_EXTENTS: u16 = 1024;
/// Largest metadata item this returns; a real photo's Exif and XMP are a few hundred kilobytes.
const MAX_ITEM: usize = 32 << 20;
/// Does this file declare an ISO base media still image (HEIF / HEIC / AVIF)?
pub fn is_heif(bytes: &[u8]) -> bool {
if bytes.get(4..8) != Some(b"ftyp".as_slice()) {
return false;
}
let Some(size) = be32(bytes, 0).map(|v| v as usize) else {
return false;
};
let end = size.min(bytes.len()).min(MAX_FTYP);
let brand = |at: usize| bytes.get(at..at.saturating_add(4)).is_some_and(|b| IMAGE_BRANDS.contains(&b));
// the major brand, then (after the minor version) the compatible brands
brand(8) || (16..end).step_by(4).any(brand)
}
/// The Exif and XMP blocks of a HEIF file. Anything malformed is left out rather than reported:
/// a file whose items can't be located simply has no metadata, as before this reader existed.
/// EXIF and XMP of the primary image, or empty metadata when no readable items exist.
pub fn metadata(bytes: &[u8]) -> Embedded {
let mut out = Embedded::default();
let mut budget = MAX_BOXES;
let Some(meta) = boxes(bytes, 0, bytes.len(), &mut budget).into_iter().find(|b| &b.kind == b"meta") else {
return out;
};
// `meta` is a FullBox: a version and flags before its children
let (mut items, mut locs, mut idat) = (Vec::new(), Vec::new(), None);
for b in boxes(bytes, meta.body.saturating_add(4), meta.end, &mut budget) {
match &b.kind {
b"iinf" => items = item_infos(bytes, &b, &mut budget),
b"iloc" => locs = item_locations(bytes.get(b.body..b.end).unwrap_or_default()),
b"idat" => idat = bytes.get(b.body..b.end),
_ => {}
}
}
for (id, kind, content_type) in items {
let want_exif = &kind == b"Exif" && out.exif.is_none();
let want_xmp = &kind == b"mime" && content_type == "application/rdf+xml" && out.xmp.is_none();
if !want_exif && !want_xmp {
continue;
}
let Some(loc) = locs.iter().find(|l| l.id == id) else { continue };
let Some(block) = item_bytes(bytes, idat, loc) else { continue };
if want_exif {
// ISO/IEC 23008-12 A.2.1: a four-byte offset from the end of itself to the TIFF header
let skip = be32(&block, 0).and_then(|v| usize::try_from(v).ok()).and_then(|v| v.checked_add(4));
out.exif = skip.and_then(|at| block.get(at..)).filter(|b| !b.is_empty()).map(<[u8]>::to_vec);
} else {
out.xmp = Some(String::from_utf8_lossy(&block).trim_end_matches('\0').to_string());
}
}
out
let (exif, xmp) = craft_heif::items::read(bytes).unwrap_or_default();
Embedded { exif, xmp, ..Default::default() }
}
/// `iinf` → one `(item_ID, item_type, content_type)` per `infe` entry. Entries of version 0 and 1
/// name no item type and are skipped; HEIF files use version 2 or 3.
fn item_infos(bytes: &[u8], iinf: &BoxRef, budget: &mut usize) -> Vec<(u32, [u8; 4], String)> {
// FullBox version and flags, then a 16- or 32-bit entry count before the `infe` boxes
let version = *bytes.get(iinf.body).unwrap_or(&0);
let entries_at = iinf.body.saturating_add(if version == 0 { 6 } else { 8 });
let mut out = Vec::new();
for b in boxes(bytes, entries_at, iinf.end, budget) {
if &b.kind != b"infe" || out.len() >= MAX_ITEMS {
continue;
}
let Some(e) = bytes.get(b.body..b.end) else { continue };
let (id, kind_at) = match *e.first().unwrap_or(&0) {
2 => (be16(e, 4).map(u32::from), 8),
3 => (be32(e, 4), 10),
_ => continue,
/// Whether any HEIF metadata item contains location; uncertain items count as location.
pub(crate) fn embeds_location(bytes: &[u8]) -> Option<bool> {
use craft_heif::items::{Kind, exif_tiff, scan};
let scan = scan(bytes).ok()?;
if !scan.complete() {
return Some(true);
}
for item in scan {
let bytes = match item.data {
Ok(bytes) => bytes,
Err(_) => return Some(true),
};
let (Some(id), Some(kind)) = (id, e.get(kind_at..kind_at + 4)) else { continue };
let mut kind4 = [0u8; 4];
kind4.copy_from_slice(kind);
// the item name, then (for a `mime` item) the content type, both NUL-terminated
let mut strings = e.get(kind_at + 4..).unwrap_or_default().split(|&c| c == 0).skip(1);
let content_type = String::from_utf8_lossy(strings.next().unwrap_or_default()).into_owned();
out.push((id, kind4, content_type));
}
out
}
/// Where one item's bytes are.
struct Loc {
id: u32,
/// 0 = offsets into the file, 1 = into the `idat` box, 2 = another item (not read).
construction: u8,
base: u64,
/// `(offset, length)` per extent, in order.
extents: Vec<(u64, u64)>,
}
/// `iloc` (payload after the box header) → one [`Loc`] per entry, stopping at the first
/// unreadable one.
fn item_locations(p: &[u8]) -> Vec<Loc> {
let version = *p.first().unwrap_or(&0);
let (Some(sizes), Some(base_index)) = (p.get(4), p.get(5)) else { return Vec::new() };
let (offset_size, length_size) = ((sizes >> 4) as usize, (sizes & 0xf) as usize);
let (base_size, index_size) = ((base_index >> 4) as usize, if version > 0 { (base_index & 0xf) as usize } else { 0 });
let (count, mut at) = match version {
0 | 1 => (be16(p, 6).map(usize::from), 8),
2 => (be32(p, 6).and_then(|v| usize::try_from(v).ok()), 10),
_ => return Vec::new(),
};
let mut out = Vec::new();
for _ in 0..count.unwrap_or(0).min(MAX_ITEMS) {
let (id, after_id) = match version {
2 => (be32(p, at), at + 4),
_ => (be16(p, at).map(u32::from), at + 2),
let located = match item.kind {
Kind::Exif => match exif_tiff(&bytes) {
Some(tiff) => exif_has_location(tiff),
None => !bytes.is_empty(),
},
Kind::Xmp => xmp_has_location(&String::from_utf8_lossy(&bytes)),
_ => true,
};
// version 1 and 2 put the construction method in the low nibble of a reserved 16-bit field
let construction = if version == 0 { Some(0) } else { be16(p, after_id).map(|v| (v & 0xf) as u8) };
let after_method = after_id + if version == 0 { 0 } else { 2 };
// a data reference index of 0 means this file; an external reference has no bytes here
let external = be16(p, after_method).is_some_and(|v| v != 0);
let base = uint(p, after_method + 2, base_size);
let extent_at = after_method + 2 + base_size;
let (Some(id), Some(construction), Some(base), Some(extent_count)) = (id, construction, base, be16(p, extent_at)) else {
break;
};
let stride = index_size + offset_size + length_size;
let mut extents = Vec::new();
let mut e = extent_at + 2;
for _ in 0..extent_count.min(MAX_EXTENTS) {
let (Some(offset), Some(length)) = (uint(p, e + index_size, offset_size), uint(p, e + index_size + offset_size, length_size)) else {
return out;
};
extents.push((offset, length));
e += stride;
}
at = e;
if !external {
out.push(Loc { id, construction, base, extents });
if located {
return Some(true);
}
}
out
Some(false)
}
/// A big-endian unsigned integer of `size` bytes; `size` 0 is the value 0, and the item boxes
/// allow no other width than 4 or 8.
fn uint(b: &[u8], at: usize, size: usize) -> Option<u64> {
match size {
0 => Some(0),
4 => be32(b, at).map(u64::from),
8 => be64(b, at),
_ => None,
/// Any GPS tag counts, not only a complete coordinate pair: the projected [`crate::Metadata`] keeps
/// GPS only when latitude and longitude both read, yet a lone latitude is still a location. A GPS
/// IFD pointer whose directory can't be read counts too, and so does any directory the reader
/// couldn't read in full: an entry past its entry limit can be the GPS pointer.
fn exif_has_location(tiff: &[u8]) -> bool {
use craft_tiff::{ParseOptions, Tiff, tags as t};
let Ok((parsed, complete)) = Tiff::parse_with_status(tiff, &ParseOptions::default()) else { return true };
if !complete {
return true;
}
let gps = parsed.all_ifds().into_iter().any(|ifd| match &ifd.gps {
Some(gps) => gps.entries.iter().any(|e| e.tag != t::GPS_VERSION_ID),
None => ifd.contains(t::GPS_IFD),
});
gps || crate::try_read_exif(tiff).map_or(true, |m| crate::has_location(&m))
}
/// An item's bytes, its extents concatenated. `None` when they don't fit the file (or the `idat`
/// box), when the item is built by referencing another one, or when it is implausibly large.
fn item_bytes(bytes: &[u8], idat: Option<&[u8]>, loc: &Loc) -> Option<Vec<u8>> {
let src = match loc.construction {
0 => bytes,
1 => idat?,
_ => return None,
};
let mut out = Vec::new();
for &(offset, length) in &loc.extents {
let at = loc.base.checked_add(offset)?;
let (start, end) = (usize::try_from(at).ok()?, usize::try_from(at.checked_add(length)?).ok()?);
let extent = src.get(start..end)?;
if out.len().checked_add(extent.len())? > MAX_ITEM {
return None;
}
out.extend_from_slice(extent);
}
(!out.is_empty()).then_some(out)
/// The packet's whole location inventory (IPTC `LocationShown` / `LocationCreated`, GPS
/// properties), not only the fields projected into [`crate::Metadata`]. A packet whose property
/// list was cut short can't prove it has none.
fn xmp_has_location(packet: &str) -> bool {
let Ok(xmp) = crate::xmp::parse_xmp_with(packet, &[]) else { return true };
xmp.properties_truncated || xmp.has_location_metadata() || crate::parse_xmp(packet).map_or(true, |x| crate::has_location(&x.metadata))
}
#[cfg(test)]
mod tests {
#[allow(clippy::indexing_slicing, clippy::arithmetic_side_effects)]
mod upstream_tests {
use super::*;
// the whole routing, not just this module: a HEIC must reach the HEIF branch of `embedded`
use crate::embedded;
@@ -460,3 +270,347 @@ mod tests {
assert_eq!(crate::extract(&file).capture_time.map(|d| d.to_iso()).as_deref(), Some("2026-10-18T13:00:00-05:00"));
}
}
#[cfg(test)]
#[allow(clippy::arithmetic_side_effects, clippy::indexing_slicing, dead_code)]
mod location_tests {
use craft_heif::items::MAX_ITEM;
fn bx(kind: &[u8; 4], payload: &[u8]) -> Vec<u8> {
let mut v = ((payload.len() + 8) as u32).to_be_bytes().to_vec();
v.extend_from_slice(kind);
v.extend_from_slice(payload);
v
}
fn full(kind: &[u8; 4], version: u8, payload: &[u8]) -> Vec<u8> {
bx(kind, &[&[version, 0, 0, 0][..], payload].concat())
}
fn put(v: &mut Vec<u8>, x: u64, size: u8) {
match size {
0 => assert_eq!(x, 0, "a value needs a field"),
2 => v.extend_from_slice(&(x as u16).to_be_bytes()),
4 => v.extend_from_slice(&(x as u32).to_be_bytes()),
8 => v.extend_from_slice(&x.to_be_bytes()),
_ => panic!("no {size}-byte fields"),
}
}
/// An EXIF item as HEIF stores it: the 4-byte offset, `skip` (which the offset jumps), the TIFF.
fn exif_item(skip: &[u8], tiff: &[u8]) -> Vec<u8> {
[&(skip.len() as u32).to_be_bytes()[..], skip, tiff].concat()
}
fn tiff_with(model: &str) -> Vec<u8> {
crate::write_exif(&crate::Metadata { model: Some(model.into()), ..Default::default() })
}
#[derive(Clone, Copy, PartialEq)]
enum Store {
File,
Idat,
}
struct Item {
id: u32,
kind: &'static [u8; 4],
/// `mime` items: content type and content encoding.
mime: (&'static str, &'static str),
data: Vec<u8>,
store: Store,
/// Extents the data is split into.
extents: usize,
}
/// A HEIF file for the tests: `ftyp`, `meta` (`hdlr`, `pitm`, `iinf`, `iref`, `iloc`, `idat`)
/// and `mdat`.
struct Heif {
ftyp: Vec<u8>,
primary: u32,
items: Vec<Item>,
/// `iref` references: type, from, to.
refs: Vec<(&'static [u8; 4], u32, Vec<u32>)>,
/// `iloc` version and its offset, length, base-offset and index field sizes.
iloc: (u8, [u8; 4]),
/// 32-bit item ids: `infe` version 3, `pitm` and `iref` version 1 (`iloc` must be version 2).
wide: bool,
}
impl Heif {
/// A primary HEVC picture, item 1 (its bytes don't matter here).
fn new() -> Heif {
let picture = Item { id: 1, kind: b"hvc1", mime: ("", ""), data: b"coded picture".to_vec(), store: Store::File, extents: 1 };
Heif { ftyp: b"heic\0\0\0\0mif1heic".to_vec(), primary: 1, items: vec![picture], refs: Vec::new(), iloc: (1, [4, 4, 0, 0]), wide: false }
}
fn item(mut self, id: u32, kind: &'static [u8; 4], data: Vec<u8>, store: Store, extents: usize) -> Heif {
self.items.push(Item { id, kind, mime: ("", ""), data, store, extents });
self
}
fn xmp(mut self, id: u32, mime: (&'static str, &'static str), data: Vec<u8>) -> Heif {
self.items.push(Item { id, kind: b"mime", mime, data, store: Store::File, extents: 1 });
self
}
fn cdsc(self, from: u32, to: &[u32]) -> Heif {
self.iref(b"cdsc", from, to)
}
fn iref(mut self, kind: &'static [u8; 4], from: u32, to: &[u32]) -> Heif {
self.refs.push((kind, from, to.to_vec()));
self
}
fn meta(&self, mdat_at: usize) -> Vec<u8> {
let id = |v: &mut Vec<u8>, id: u32| put(v, u64::from(id), if self.wide { 4 } else { 2 });
let mut hdlr = vec![0u8; 4];
hdlr.extend_from_slice(b"pict");
hdlr.extend_from_slice(&[0; 13]);
let mut pitm = Vec::new();
id(&mut pitm, self.primary);
let mut iinf = Vec::new();
put(&mut iinf, self.items.len() as u64, if self.wide { 4 } else { 2 });
for it in &self.items {
let mut e = Vec::new();
id(&mut e, it.id);
e.extend_from_slice(&[0, 0]); // not protected
e.extend_from_slice(it.kind);
e.push(0); // no name
if it.kind == b"mime" {
e.extend_from_slice(it.mime.0.as_bytes());
e.push(0);
e.extend_from_slice(it.mime.1.as_bytes());
e.push(0);
}
iinf.extend(full(b"infe", if self.wide { 3 } else { 2 }, &e));
}
let mut iref = Vec::new();
for (kind, from, to) in &self.refs {
let mut r = Vec::new();
id(&mut r, *from);
put(&mut r, to.len() as u64, 2);
to.iter().for_each(|t| id(&mut r, *t));
iref.extend(bx(kind, &r));
}
let (version, [o, l, b, i]) = self.iloc;
let mut iloc = vec![o << 4 | l, b << 4 | i];
put(&mut iloc, self.items.len() as u64, if version == 2 { 4 } else { 2 });
let (mut in_file, mut in_idat) = (mdat_at as u64, 0u64);
let mut idat = Vec::new();
for it in &self.items {
put(&mut iloc, u64::from(it.id), if version == 2 { 4 } else { 2 });
let at = match it.store {
Store::File => &mut in_file,
Store::Idat => &mut in_idat,
};
if version > 0 {
put(&mut iloc, if it.store == Store::Idat { 1 } else { 0 }, 2);
}
put(&mut iloc, 0, 2); // this file
let base = if b > 0 { *at } else { 0 };
put(&mut iloc, base, b);
let chunk = it.data.len().div_ceil(it.extents.max(1)).max(1);
let pieces: Vec<&[u8]> = it.data.chunks(chunk).collect();
put(&mut iloc, pieces.len() as u64, 2);
for piece in pieces {
if version > 0 {
put(&mut iloc, 0, i);
}
put(&mut iloc, *at - base, o);
put(&mut iloc, piece.len() as u64, l);
*at += piece.len() as u64;
}
if it.store == Store::Idat {
idat.extend_from_slice(&it.data);
}
}
let ref_version = if self.wide { 1 } else { 0 };
let boxes = [
full(b"hdlr", 0, &hdlr),
full(b"pitm", ref_version, &pitm),
full(b"iinf", ref_version, &iinf),
full(b"iref", ref_version, &iref),
full(b"iloc", version, &iloc),
bx(b"idat", &idat),
];
full(b"meta", 0, &boxes.concat())
}
fn build(&self) -> Vec<u8> {
let ftyp = bx(b"ftyp", &self.ftyp);
let mdat_at = ftyp.len() + self.meta(0).len() + 8;
let mdat: Vec<u8> = self.items.iter().filter(|i| i.store == Store::File).flat_map(|i| i.data.iter().copied()).collect();
[ftyp, self.meta(mdat_at), bx(b"mdat", &mdat)].concat()
}
}
fn tiff_at(latitude: f64) -> Vec<u8> {
crate::write_exif(&crate::Metadata { gps: Some(crate::Gps { latitude, longitude: 2.0, altitude: None }), ..Default::default() })
}
/// Export's "remove location" check sees GPS anywhere in a HEIF's metadata, not only in the
/// photo's own first EXIF (#693 review): a thumbnail's EXIF, the primary image's second EXIF
/// item, an XMP item. What it can't read counts as a location, so the original isn't copied
/// out on a guess.
#[test]
fn a_location_anywhere_in_the_metadata_is_seen() {
let plain = tiff_with("Primary");
let thumb = |h: Heif| h.item(2, b"hvc1", b"thumbnail".to_vec(), Store::File, 1);
// only the thumbnail's EXIF has GPS
let f = thumb(Heif::new())
.item(10, b"Exif", exif_item(&[], &plain), Store::File, 1)
.item(11, b"Exif", exif_item(&[], &tiff_at(1.0)), Store::File, 1)
.cdsc(10, &[1])
.cdsc(11, &[2])
.build();
assert_eq!(crate::extract(&f).gps, None, "the photo's own EXIF has none");
assert!(crate::embeds_location(&f), "thumbnail");
// the primary image's second EXIF item has it
let f = Heif::new()
.item(10, b"Exif", exif_item(&[], &plain), Store::File, 1)
.item(11, b"Exif", exif_item(&[], &tiff_at(1.0)), Store::File, 1)
.cdsc(10, &[1])
.cdsc(11, &[1])
.build();
assert_eq!(crate::extract(&f).gps, None);
assert!(crate::embeds_location(&f), "second EXIF item");
// a thumbnail's XMP has it
let rdf = ("application/rdf+xml", "");
let xmp = crate::write_xmp(
&crate::Metadata { gps: Some(crate::Gps { latitude: 1.0, longitude: 2.0, altitude: None }), ..Default::default() },
None,
);
let f = thumb(Heif::new())
.item(10, b"Exif", exif_item(&[], &plain), Store::File, 1)
.cdsc(10, &[1])
.xmp(20, rdf, xmp.into_bytes())
.cdsc(20, &[2])
.build();
assert!(crate::embeds_location(&f), "thumbnail XMP");
// no location anywhere
let f = thumb(Heif::new())
.item(10, b"Exif", exif_item(&[], &plain), Store::File, 1)
.item(11, b"Exif", exif_item(&[], &tiff_with("Thumbnail")), Store::File, 1)
.cdsc(10, &[1])
.cdsc(11, &[2])
.build();
assert!(!crate::embeds_location(&f));
// unsure counts as yes: a compressed XMP item, an EXIF item cut off by the end of the file,
// one larger than the reader takes, more items than it looks at
let f = Heif::new().xmp(20, ("application/rdf+xml", "deflate"), b"compressed".to_vec()).build();
assert!(crate::embeds_location(&f), "compressed XMP");
let mut f = Heif::new().item(10, b"Exif", exif_item(&[], &plain), Store::File, 1).build();
f.truncate(f.len() - 1);
assert!(crate::embeds_location(&f), "cut off");
let f = Heif::new().item(10, b"Exif", exif_item(&[], &vec![0; MAX_ITEM]), Store::File, 1).build();
assert!(crate::embeds_location(&f), "too large");
let mut h = Heif::new();
for id in 0..2000 {
h = h.item(10 + id, b"Exif", exif_item(&[], &plain), Store::File, 1);
}
assert!(crate::embeds_location(&h.build()), "too many items");
// other files: what `extract` reads
assert!(crate::embeds_location(&tiff_at(1.0)));
assert!(!crate::embeds_location(&plain));
}
/// A GPS IFD with a latitude but no longitude reads as no `Gps`, but it is still a location
/// (#4315 review).
#[test]
fn a_lone_gps_latitude_in_a_thumbnail_is_seen() {
use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value, tags as t};
let tiff = |gps: Option<IfdBuilder>| {
let mut ifd0 = IfdBuilder::new();
ifd0.set(t::MODEL, Value::Ascii("Thumbnail".into()));
if let Some(gps) = gps {
ifd0.set_child(t::GPS_IFD, gps);
}
TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap_or_default()
};
let mut latitude = IfdBuilder::new();
latitude.set(t::GPS_VERSION_ID, Value::Byte(vec![2, 3, 0, 0]));
latitude.set(t::GPS_LATITUDE_REF, Value::Ascii("N".into()));
latitude.set(t::GPS_LATITUDE, Value::Rational(vec![(48, 1), (51, 1), (0, 1)]));
let mut version_only = IfdBuilder::new();
version_only.set(t::GPS_VERSION_ID, Value::Byte(vec![2, 3, 0, 0]));
for (gps, located) in [(Some(latitude), true), (Some(version_only), false), (None, false)] {
let tiff = tiff(gps);
assert_eq!(crate::read_exif(&tiff).gps, None, "no coordinate pair");
let f = Heif::new()
.item(2, b"hvc1", b"thumbnail".to_vec(), Store::File, 1)
.item(3, b"Exif", exif_item(&[], &tiff), Store::File, 1)
.cdsc(3, &[2])
.build();
assert_eq!(crate::embeds_location(&f), located);
}
}
/// A GPS pointer the reader never reaches, after its 8,192-entry limit, still counts (#4315
/// review): an incompletely read EXIF item is a possible location.
#[test]
fn a_gps_pointer_past_the_entry_limit_counts_as_a_location() {
let entries = 8193u32;
let gps_at = 8 + 2 + 12 * entries + 4;
let mut tiff = b"II\x2a\0\x08\0\0\0".to_vec();
tiff.extend_from_slice(&(entries as u16).to_le_bytes());
for tag in 1..entries as u16 {
// SHORT, count 1, value 0
tiff.extend_from_slice(&[&tag.to_le_bytes()[..], &[3, 0, 1, 0, 0, 0, 0, 0, 0, 0]].concat());
}
tiff.extend_from_slice(&[&34853u16.to_le_bytes()[..], &[4, 0, 1, 0, 0, 0], &gps_at.to_le_bytes()].concat());
tiff.extend_from_slice(&0u32.to_le_bytes());
// GPS IFD: GPSLatitudeRef "N", then no next IFD
tiff.extend_from_slice(&[1, 0, 1, 0, 2, 0, 2, 0, 0, 0, b'N', 0, 0, 0, 0, 0, 0, 0]);
let f = Heif::new()
.item(2, b"hvc1", b"thumbnail".to_vec(), Store::File, 1)
.item(3, b"Exif", exif_item(&[], &tiff), Store::File, 1)
.cdsc(3, &[2])
.build();
assert!(crate::embeds_location(&f));
}
/// An IPTC `LocationShown` place is a location even though it isn't projected into
/// `Metadata`'s city or country (#4315 review).
#[test]
fn an_iptc_location_shown_in_a_thumbnail_is_seen() {
let packet = |inner: &str| {
format!(
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:Iptc4xmpExt="http://iptc.org/std/Iptc4xmpExt/2008-02-29/" xmlns:dc="http://purl.org/dc/elements/1.1/">{inner}</rdf:Description></rdf:RDF></x:xmpmeta>"#
)
};
let shown = packet(
r#"<Iptc4xmpExt:LocationShown><rdf:Bag><rdf:li rdf:parseType="Resource"><Iptc4xmpExt:City>Paris</Iptc4xmpExt:City></rdf:li></rdf:Bag></Iptc4xmpExt:LocationShown>"#,
);
let plain = packet(r#"<dc:format>image/heic</dc:format>"#);
for (xmp, located) in [(shown, true), (plain, false)] {
assert!(crate::parse_xmp(&xmp).is_ok_and(|x| !crate::has_location(&x.metadata)), "not projected into Metadata");
let f = Heif::new()
.item(2, b"hvc1", b"thumbnail".to_vec(), Store::File, 1)
.xmp(3, ("application/rdf+xml", ""), xmp.into_bytes())
.cdsc(3, &[2])
.build();
assert_eq!(crate::embeds_location(&f), located);
}
}
/// Original export checks place names in every metadata item, like GPS coordinates.
#[test]
fn place_names_in_a_thumbnail_are_seen_for_original_export() {
for (metadata, located) in [
(crate::Metadata { sublocation: Some("Test place".into()), ..Default::default() }, true),
(crate::Metadata { city: Some("Test city".into()), ..Default::default() }, true),
(crate::Metadata { state: Some("Test state".into()), ..Default::default() }, true),
(crate::Metadata { country: Some("Test country".into()), ..Default::default() }, true),
(crate::Metadata { title: Some("A title without location".into()), ..Default::default() }, false),
] {
let xmp = crate::write_xmp(&metadata, None);
assert!(crate::parse_xmp(&xmp).is_ok());
let f = Heif::new()
.item(2, b"hvc1", b"thumbnail".to_vec(), Store::File, 1)
.xmp(3, ("application/rdf+xml", ""), xmp.into_bytes())
.cdsc(3, &[2])
.build();
assert!(crate::embedded(&f).xmp.is_none(), "the thumbnail's packet is not the primary image's");
assert_eq!(crate::embeds_location(&f), located, "{metadata:?}");
}
}
}
+17
View File
@@ -407,6 +407,23 @@ pub fn lens_spec_name(spec: [f64; 4]) -> Option<String> {
Some(format!("{focal} mm f/{aperture}"))
}
/// Does the file embed GPS or a place name anywhere it can be read from? Export's "remove location"
/// asks before copying an original byte for byte. For HEIF, HEIC and AVIF that is any EXIF or XMP
/// item, whichever image it describes (a thumbnail's too), and an item that can't be read counts
/// as a location; for other files, what [`extract`] reads.
pub fn embeds_location(bytes: &[u8]) -> bool {
heif::embeds_location(bytes).unwrap_or_else(|| has_location(&extract(bytes)))
}
/// Location includes the place names removed by export, as well as GPS coordinates.
fn has_location(metadata: &Metadata) -> bool {
metadata.gps.is_some()
|| [metadata.sublocation.as_deref(), metadata.city.as_deref(), metadata.state.as_deref(), metadata.country.as_deref()]
.into_iter()
.flatten()
.any(|value| !value.trim().is_empty())
}
/// Lenient number parsing used by XMP/IPTC: `"28/10"`, `"2.8"`, `"+0.7"`.
#[cfg(test)]
pub(crate) fn parse_number(s: &str) -> Option<f64> {
+1
View File
@@ -9,6 +9,7 @@ description = "LightCraft develop pipeline: scene-referred, float, resolution in
workspace = true
[dependencies]
craft-cms-profile = { workspace = true }
lightcraft-geom = { workspace = true }
lightcraft-color = { workspace = true }
lightcraft-raster = { workspace = true }
+21 -42
View File
@@ -19,48 +19,8 @@ pub struct Lut3d {
impl Lut3d {
/// Parse a `.cube` file.
pub fn parse_cube(text: &str) -> Result<Lut3d, String> {
let (mut size, mut title) = (0usize, None);
let (mut min, mut max) = ([0.0f32; 3], [1.0f32; 3]);
let mut data = Vec::new();
let three = |rest: &str| -> Option<[f32; 3]> {
let v: Vec<f32> = rest.split_whitespace().filter_map(|x| x.parse().ok()).collect();
(v.len() == 3 && v.iter().all(|x| x.is_finite())).then(|| [v[0], v[1], v[2]])
};
for line in text.lines() {
let l = line.trim();
if l.is_empty() || l.starts_with('#') {
continue;
}
if let Some(r) = l.strip_prefix("TITLE") {
title = Some(r.trim().trim_matches('"').to_string());
} else if let Some(r) = l.strip_prefix("LUT_3D_SIZE") {
size = r.trim().parse().map_err(|_| format!("bad LUT_3D_SIZE `{}`", r.trim()))?;
if !(2..=256).contains(&size) {
return Err(format!("LUT_3D_SIZE {size} out of range"));
}
} else if l.starts_with("LUT_1D_SIZE") {
return Err("1D LUTs are not supported (only 3D .cube files)".into());
} else if let Some(r) = l.strip_prefix("DOMAIN_MIN") {
min = three(r).ok_or("bad DOMAIN_MIN")?;
} else if let Some(r) = l.strip_prefix("DOMAIN_MAX") {
max = three(r).ok_or("bad DOMAIN_MAX")?;
} else if l.starts_with(|c: char| c.is_ascii_alphabetic()) {
// a data line of `nan` / `inf` starts with a letter too: it is bad data, not a keyword
if l.split_whitespace().next().is_some_and(|w| w.parse::<f32>().is_ok()) {
return Err(format!("bad line `{l}`"));
}
// other keywords (LUT_IN_VIDEO_RANGE…) are ignored
} else {
data.push(three(l).ok_or_else(|| format!("bad line `{l}`"))?);
}
}
if size == 0 {
return Err("not a 3D .cube LUT (no LUT_3D_SIZE)".into());
}
if data.len() != size * size * size {
return Err(format!("expected {} entries, found {}", size * size * size, data.len()));
}
Ok(Lut3d { size, data, domain_min: min, domain_max: max, title })
let table = craft_cms_profile::lutfile::parse_cube_3d(text, 256).map_err(|error| error.to_string())?;
Ok(Lut3d { size: table.size, data: table.data, domain_min: table.domain_min, domain_max: table.domain_max, title: table.title })
}
/// Look up `c` (trilinear).
@@ -132,6 +92,25 @@ mod tests {
s
}
#[test]
fn shared_profile_parser_keeps_domain_and_title_policy() {
let text = format!(
"TITLE \"literal # title\"\nDOMAIN_MIN 1 0 2\nDOMAIN_MAX 0 0 1\n{}",
cube(|c| c, 2).lines().skip(1).collect::<Vec<_>>().join("\n")
);
let profile = Lut3d::parse_cube(&text).unwrap();
assert_eq!(profile.title.as_deref(), Some("literal # title"));
assert_eq!(profile.domain_min, [1.0, 0.0, 2.0]);
assert_eq!(profile.domain_max, [0.0, 0.0, 1.0]);
assert!(profile.apply([0.0; 3]).iter().all(|v| v.is_finite()));
let unlabelled = cube(|c| c, 2).lines().skip(1).collect::<Vec<_>>().join("\n");
assert_eq!(Lut3d::parse_cube(&unlabelled).unwrap().title, None);
assert!(Lut3d::parse_cube("LUT_3D_SIZE 256").unwrap_err().contains("16777216"));
for bad in ["0 ignored 0 0", "0 0 0 ignored", "NaN 0 0"] {
assert!(Lut3d::parse_cube(&format!("LUT_3D_SIZE 2\n{bad}\n{}", "0 0 0\n".repeat(7))).is_err());
}
}
#[test]
fn identity_and_swap() {
let id = Lut3d::parse_cube(&cube(|c| c, 17)).unwrap();
+2 -2
View File
@@ -26,8 +26,8 @@ craft-image-codecs = { workspace = true }
# tests record why they skip (`cargo xtask ci` lists the skips)
lightcraft-testkit = { workspace = true }
proptest = { workspace = true }
zune-jpegxl = { workspace = true }
zune-core = { workspace = true }
craft-jxl = { workspace = true }
jixel = { workspace = true }
# Pinned CC0 corpus identities; not a product dependency.
sha2 = { version = "0.10", default-features = false, features = ["std"] }
# Peak-heap regression tests (tests/sr2_heap.rs); not a product dependency.
+1 -1
View File
@@ -120,7 +120,7 @@ fn orientation(o: craft_raw::Orientation) -> Orientation {
Orientation::from_exif(o.to_exif())
}
fn color(c: craft_raw::ColorData) -> ColorData {
pub(crate) fn color(c: craft_raw::ColorData) -> ColorData {
let m = |p: [Option<craft_raw::Matrix3>; 2]| p.map(|m| m.map(Mat3));
ColorData {
illuminant: c.illuminant,
+53 -193
View File
@@ -4,15 +4,13 @@
//! `UniqueCameraModel`, `ProfileName`, the colour/forward matrices and calibration illuminants, the
//! hue/saturation map, look table and tone curve. Nothing here ships a profile.
use craft_tiff::{ByteOrder, Ifd, ParseOptions, Tiff, tags as t};
use lightcraft_color::Xy;
#[cfg(test)]
use craft_tiff::tags as t;
use crate::gaintable::GainTableMap;
use crate::profile::{HsvTable, ProfileLook, ToneCurve};
use crate::{ColorData, Mat3};
use crate::ColorData;
/// The magic number of a DNG camera profile file (`IIRC` / `MMCR`).
pub const DCP_MAGIC: u16 = 0x4352;
pub use craft_raw::dcp::DCP_MAGIC;
/// A parsed camera profile.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
@@ -49,201 +47,30 @@ impl DcpProfile {
}
}
/// Errors reading a `.dcp`.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum DcpError {
#[error("not a DNG camera profile (.dcp)")]
NotDcp,
#[error("damaged camera profile: {0}")]
Damaged(String),
#[error("the camera profile has no usable colour matrix")]
NoMatrix,
}
pub use craft_raw::dcp::{DcpError, MAX_DCP_BYTES, is_dcp, read_dcp};
/// Is `bytes` a DCP file (by its header)?
pub fn is_dcp(bytes: &[u8]) -> bool {
matches!(bytes.get(0..4), Some([b'I', b'I', b'R', b'C'] | [b'M', b'M', b'C', b'R']))
}
/// The largest `.dcp` file read or parsed (real profiles are kilobytes to a few MB).
pub const MAX_DCP_BYTES: u64 = 64 << 20;
/// Read the `.dcp` file at `path`, reading at most [`MAX_DCP_BYTES`] + 1 bytes: a larger file is refused
/// before it is loaded into memory.
pub fn read_dcp(path: &std::path::Path) -> std::io::Result<Vec<u8>> {
use std::io::Read;
let mut bytes = Vec::new();
std::fs::File::open(path)?.take(MAX_DCP_BYTES.saturating_add(1)).read_to_end(&mut bytes)?;
if bytes.len() as u64 > MAX_DCP_BYTES {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "the camera profile is larger than 64 MiB"));
}
Ok(bytes)
}
/// Parse a `.dcp` file. Files above 64 MiB ([`MAX_DCP_BYTES`]) are refused.
/// Parse the shared file model, keeping application color types at the boundary.
pub fn parse_dcp(bytes: &[u8]) -> Result<DcpProfile, DcpError> {
if !is_dcp(bytes) {
return Err(DcpError::NotDcp);
}
if bytes.len() as u64 > MAX_DCP_BYTES {
return Err(DcpError::Damaged("larger than 64 MiB".into()));
}
// the TIFF reader only knows 42 / BigTIFF / raw magics: give it a header with 42 in place of RC
let mut data = bytes.to_vec();
let forty_two: [u8; 2] = if bytes.first() == Some(&b'I') { [42, 0] } else { [0, 42] };
if let Some(m) = data.get_mut(2..4) {
m.copy_from_slice(&forty_two);
}
let opts = ParseOptions { follow_children: false, max_ifds: 4, value_budget: Some(64 << 20), ..Default::default() };
let tiff = Tiff::parse_with(&data, &opts).map_err(|e| DcpError::Damaged(e.to_string()))?;
let ifd: &Ifd = tiff.ifds.first().ok_or_else(|| DcpError::Damaged("no IFD".into()))?;
let mut color = color_data(ifd, ifd, tiff.order);
// a profile carries no white balance or calibration of a particular shot
color.as_shot_neutral = None;
color.as_shot_white_xy = None;
color.analog_balance = None;
color.camera_calibration = [None, None];
color.baseline_sharpness = None;
color.profile.gain_table_map = None;
// only BaselineExposureOffset belongs to a profile (BaselineExposure is the camera's)
color.baseline_exposure =
ifd.f64s(t::BASELINE_EXPOSURE_OFFSET).and_then(|v| v.first().copied()).filter(|v| v.is_finite()).unwrap_or(0.0).clamp(-4.0, 4.0);
// a single-illuminant profile names one illuminant; its second matrix slot stays empty
if color.color_matrix[0].is_none() {
// the first slot is the one every profile must fill (DNG: ColorMatrix1 is required)
return Err(DcpError::NoMatrix);
}
let text = |tag: u16| ifd.string(tag).map(|s| s.trim_matches(char::from(0)).trim().to_string()).unwrap_or_default();
Ok(DcpProfile { name: text(t::PROFILE_NAME), camera_model: text(t::UNIQUE_CAMERA_MODEL), color })
let p = craft_raw::dcp::parse_dcp(bytes)?;
Ok(DcpProfile { name: p.name, camera_model: p.camera_model, color: crate::convert::color(p.color) })
}
/// Write `p` as a `.dcp` file (little-endian): its name, camera, illuminants, matrices, hue/saturation
/// map, look table, tone curve and baseline exposure offset.
/// Write profile wire fields through craft-raw; shot/application policy is not serialized.
pub fn write_dcp(p: &DcpProfile) -> Result<Vec<u8>, DcpError> {
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()));
ifd.set(t::PROFILE_NAME, Value::Ascii(p.name.clone()));
let mat = |m: &lightcraft_color::Mat3| Value::SRational(m.0.iter().flatten().map(|v| srational(*v)).collect());
for i in 0..2 {
if let Some(m) = &c.color_matrix[i] {
ifd.set([t::CALIBRATION_ILLUMINANT_1, t::CALIBRATION_ILLUMINANT_2][i], Value::Short(vec![c.illuminant[i]]));
ifd.set([t::COLOR_MATRIX_1, t::COLOR_MATRIX_2][i], mat(m));
}
if let Some(m) = &c.forward_matrix[i] {
ifd.set([t::FORWARD_MATRIX_1, t::FORWARD_MATRIX_2][i], mat(m));
}
}
let flat = |table: &crate::profile::HsvTable| Value::Float(table.data.iter().flatten().copied().collect());
let dims =
|table: &crate::profile::HsvTable| Value::Long(vec![table.hue_divisions as u32, table.sat_divisions as u32, table.val_divisions as u32]);
if let Some(a) = c.profile.hue_sat_map[0].as_ref() {
ifd.set(t::PROFILE_HUE_SAT_MAP_DIMS, dims(a));
ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_1, flat(a));
if a.srgb_value {
ifd.set(t::PROFILE_HUE_SAT_MAP_ENCODING, Value::Long(vec![1]));
}
if let Some(b) = c.profile.hue_sat_map[1]
.as_ref()
.filter(|b| (b.hue_divisions, b.sat_divisions, b.val_divisions) == (a.hue_divisions, a.sat_divisions, a.val_divisions))
{
ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_2, flat(b));
}
}
if let Some(l) = &c.profile.look_table {
ifd.set(t::PROFILE_LOOK_TABLE_DIMS, dims(l));
ifd.set(t::PROFILE_LOOK_TABLE_DATA, flat(l));
if l.srgb_value {
ifd.set(t::PROFILE_LOOK_TABLE_ENCODING, Value::Long(vec![1]));
}
}
if let Some(tc) = &c.profile.tone_curve {
ifd.set(t::PROFILE_TONE_CURVE, Value::Float(tc.points.iter().flatten().copied().collect()));
}
if c.baseline_exposure != 0.0 {
ifd.set(t::BASELINE_EXPOSURE_OFFSET, Value::SRational(vec![srational(c.baseline_exposure)]));
}
let mut out = TiffWriter::new(ByteOrder::Little, false).write(&[ifd]).map_err(|e| DcpError::Damaged(e.to_string()))?;
// the DCP magic in place of 42
if let Some(m) = out.get_mut(2..4) {
m.copy_from_slice(&DCP_MAGIC.to_le_bytes());
}
Ok(out)
}
// The colour tags a profile shares with DNG, read as DNG reads them (a DNG's own are read by `craft-raw`).
fn mat3(v: Option<Vec<f64>>) -> Option<Mat3> {
let v = v?;
if v.len() != 9 || v.iter().any(|x| !x.is_finite()) {
return None;
}
let m = Mat3([[v[0], v[1], v[2]], [v[3], v[4], v[5]], [v[6], v[7], v[8]]]);
(m.determinant().abs() > 1e-12).then_some(m)
}
fn vec3(v: Option<Vec<f64>>) -> Option<[f64; 3]> {
let v = v?;
(v.len() == 3 && v.iter().all(|x| x.is_finite() && *x > 0.0)).then(|| [v[0], v[1], v[2]])
}
/// Colour tags: DNG puts them in IFD0, but some writers use the raw IFD; prefer the raw IFD.
/// `order` is the file's byte order (for the binary gain-table-map tags).
fn color_data(ifd0: &Ifd, raw: &Ifd, order: ByteOrder) -> ColorData {
let get = |tag: u16| raw.f64s(tag).or_else(|| ifd0.f64s(tag));
let geti = |tag: u16| raw.u16(tag).or_else(|| ifd0.u16(tag));
ColorData {
illuminant: [geti(t::CALIBRATION_ILLUMINANT_1).unwrap_or(0), geti(t::CALIBRATION_ILLUMINANT_2).unwrap_or(0)],
color_matrix: [mat3(get(t::COLOR_MATRIX_1)), mat3(get(t::COLOR_MATRIX_2))],
forward_matrix: [mat3(get(t::FORWARD_MATRIX_1)), mat3(get(t::FORWARD_MATRIX_2))],
camera_calibration: [mat3(get(t::CAMERA_CALIBRATION_1)), mat3(get(t::CAMERA_CALIBRATION_2))],
analog_balance: vec3(get(t::ANALOG_BALANCE)),
as_shot_neutral: vec3(get(t::AS_SHOT_NEUTRAL)),
as_shot_white_xy: get(t::AS_SHOT_WHITE_XY).filter(|v| v.len() == 2 && v[0] > 0.0 && v[1] > 0.0).map(|v| Xy::new(v[0], v[1])),
baseline_exposure: get(t::BASELINE_EXPOSURE).and_then(|v| v.first().copied()).filter(|v| v.is_finite()).unwrap_or(0.0)
+ get(t::BASELINE_EXPOSURE_OFFSET).and_then(|v| v.first().copied()).filter(|v| v.is_finite()).unwrap_or(0.0),
// DNG 1.7 BaselineSharpness (IFD 0 or the enhanced IFD); implausible values are ignored
baseline_sharpness: get(t::BASELINE_SHARPNESS).and_then(|v| v.first().copied()).filter(|v| v.is_finite() && *v > 0.0 && *v <= 16.0),
profile: profile_look(ifd0, raw, order),
kelvin_presets: None,
}
}
/// The profile look tags (`ProfileHueSatMap*`, `ProfileLookTable*`, `ProfileToneCurve`,
/// `ProfileGainTableMap*`); malformed ones are ignored. Like the colour tags, read from the raw IFD
/// first, else IFD 0.
fn profile_look(ifd0: &Ifd, raw: &Ifd, order: ByteOrder) -> ProfileLook {
let pick = |tag: u16| if raw.contains(tag) { raw } else { ifd0 };
let table = |dims: u16, data: u16, enc: u16| {
let ifd = pick(dims);
let dims = ifd.u64s(dims)?;
let data = ifd.f64s(data)?;
HsvTable::from_tags(&dims, &data, ifd.u64s(enc).and_then(|v| v.first().copied()).unwrap_or(0))
let color = craft_raw::ColorData {
illuminant: p.color.illuminant,
color_matrix: p.color.color_matrix.map(|m| m.map(|m| m.0)),
forward_matrix: p.color.forward_matrix.map(|m| m.map(|m| m.0)),
baseline_exposure: p.color.baseline_exposure,
profile: p.color.profile.clone(),
..Default::default()
};
ProfileLook {
hue_sat_map: [
table(t::PROFILE_HUE_SAT_MAP_DIMS, t::PROFILE_HUE_SAT_MAP_DATA_1, t::PROFILE_HUE_SAT_MAP_ENCODING),
table(t::PROFILE_HUE_SAT_MAP_DIMS, t::PROFILE_HUE_SAT_MAP_DATA_2, t::PROFILE_HUE_SAT_MAP_ENCODING),
],
look_table: table(t::PROFILE_LOOK_TABLE_DIMS, t::PROFILE_LOOK_TABLE_DATA, t::PROFILE_LOOK_TABLE_ENCODING),
tone_curve: pick(t::PROFILE_TONE_CURVE).f64s(t::PROFILE_TONE_CURVE).and_then(|v| ToneCurve::from_tag(&v)),
gain_table_map: gain_table_map(ifd0, raw, order),
}
craft_raw::dcp::write_dcp(&craft_raw::dcp::DcpProfile { name: p.name.clone(), camera_model: p.camera_model.clone(), color })
}
/// The gain table map (DNG 1.7 precedence): `ProfileGainTableMap2` from IFD 0 (the embedded
/// camera profile), else from the raw IFD; else `ProfileGainTableMap` from the raw IFD (where DNG
/// 1.6 put it, and where Apple writes it), else from IFD 0 (where DNG 1.7 says it was meant to be).
/// A malformed tag falls through to the next one.
fn gain_table_map(ifd0: &Ifd, raw: &Ifd, order: ByteOrder) -> Option<GainTableMap> {
let parse = |ifd: &Ifd, tag: u16| ifd.bytes(tag).and_then(|b| GainTableMap::parse(b, order, tag == t::PROFILE_GAIN_TABLE_MAP_2));
parse(ifd0, t::PROFILE_GAIN_TABLE_MAP_2)
.or_else(|| parse(raw, t::PROFILE_GAIN_TABLE_MAP_2))
.or_else(|| parse(raw, t::PROFILE_GAIN_TABLE_MAP))
.or_else(|| parse(ifd0, t::PROFILE_GAIN_TABLE_MAP))
}
#[cfg(test)]
#[path = "dcp_legacy_tests.rs"]
mod legacy;
#[cfg(test)]
mod tests {
@@ -407,3 +234,36 @@ mod tests {
assert!((c.baseline_exposure - 0.25).abs() < 1e-12);
}
}
#[cfg(test)]
mod shared_parity {
use super::*;
fn check(bytes: &[u8]) {
let old = legacy::parse_dcp(bytes);
let new = parse_dcp(bytes);
match (old, new) {
(Ok(a), Ok(b)) => {
assert_eq!((a.name.as_str(), a.camera_model.as_str(), &a.color), (b.name.as_str(), b.camera_model.as_str(), &b.color));
assert_eq!(legacy::write_dcp(&a).map_err(|e| e.to_string()), write_dcp(&b).map_err(|e| e.to_string()));
}
(Err(a), Err(b)) => assert_eq!(a.to_string(), b.to_string()),
other => panic!("different DCP acceptance: {other:?}"),
}
}
#[test]
fn shared_reader_and_writer_match_original_on_both_endian_mutations() {
for little in [false, true] {
let bytes = super::tests::synthetic_dcp(little, true);
for n in 0..=bytes.len() {
check(&bytes[..n]);
}
for at in 0..bytes.len() {
for value in [0, 0xff, 0x80] {
let mut mutated = bytes.clone();
mutated[at] = value;
check(&mutated);
}
}
}
}
}
+247
View File
@@ -0,0 +1,247 @@
#![allow(dead_code)]
//! DNG Camera Profiles (`.dcp`): the user's own camera colour profiles, read at run time from
//! files they supply (DNG specification, chapter 6 "Camera Profiles"). A `.dcp` is a TIFF-structured
//! file whose header carries the magic `RC` (0x4352) in place of 42, with the profile tags in IFD 0:
//! `UniqueCameraModel`, `ProfileName`, the colour/forward matrices and calibration illuminants, the
//! hue/saturation map, look table and tone curve. Nothing here ships a profile.
use craft_tiff::{ByteOrder, Ifd, ParseOptions, Tiff, tags as t};
use lightcraft_color::Xy;
use crate::gaintable::GainTableMap;
use crate::profile::{HsvTable, ProfileLook, ToneCurve};
use crate::{ColorData, Mat3};
/// The magic number of a DNG camera profile file (`IIRC` / `MMCR`).
pub const DCP_MAGIC: u16 = 0x4352;
/// A parsed camera profile.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct DcpProfile {
/// `ProfileName` (empty when the file has none).
pub name: String,
/// `UniqueCameraModel`: the camera the profile was made for (empty when absent).
pub camera_model: String,
/// The colour model: illuminants, matrices and the profile look (hue/sat map, look table, tone curve).
pub color: ColorData,
}
impl DcpProfile {
/// `color` (a raw file's own colour facts) with this profile's colour model in place of the
/// file's: its matrices, illuminants and look. The file's white balance (as-shot neutral or
/// white, and its white-balance presets), analog balance and baseline exposure stay, plus its
/// camera calibration when the profile has the same calibration illuminants (DNG 6.3:
/// calibration belongs to the camera, the profile to the look).
pub fn applied_to(&self, color: &ColorData) -> ColorData {
let calibration = if self.color.illuminant == color.illuminant { color.camera_calibration } else { [None, None] };
ColorData {
illuminant: self.color.illuminant,
color_matrix: self.color.color_matrix,
forward_matrix: self.color.forward_matrix,
camera_calibration: calibration,
analog_balance: color.analog_balance,
as_shot_neutral: color.as_shot_neutral,
as_shot_white_xy: color.as_shot_white_xy,
baseline_exposure: color.baseline_exposure + self.color.baseline_exposure,
baseline_sharpness: color.baseline_sharpness,
profile: crate::profile::ProfileLook { gain_table_map: color.profile.gain_table_map.clone(), ..self.color.profile.clone() },
kelvin_presets: color.kelvin_presets.clone(),
}
}
}
/// Errors reading a `.dcp`.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum DcpError {
#[error("not a DNG camera profile (.dcp)")]
NotDcp,
#[error("damaged camera profile: {0}")]
Damaged(String),
#[error("the camera profile has no usable colour matrix")]
NoMatrix,
}
/// Is `bytes` a DCP file (by its header)?
pub fn is_dcp(bytes: &[u8]) -> bool {
matches!(bytes.get(0..4), Some([b'I', b'I', b'R', b'C'] | [b'M', b'M', b'C', b'R']))
}
/// The largest `.dcp` file read or parsed (real profiles are kilobytes to a few MB).
pub const MAX_DCP_BYTES: u64 = 64 << 20;
/// Read the `.dcp` file at `path`, reading at most [`MAX_DCP_BYTES`] + 1 bytes: a larger file is refused
/// before it is loaded into memory.
pub fn read_dcp(path: &std::path::Path) -> std::io::Result<Vec<u8>> {
use std::io::Read;
let mut bytes = Vec::new();
std::fs::File::open(path)?.take(MAX_DCP_BYTES.saturating_add(1)).read_to_end(&mut bytes)?;
if bytes.len() as u64 > MAX_DCP_BYTES {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "the camera profile is larger than 64 MiB"));
}
Ok(bytes)
}
/// Parse a `.dcp` file. Files above 64 MiB ([`MAX_DCP_BYTES`]) are refused.
pub fn parse_dcp(bytes: &[u8]) -> Result<DcpProfile, DcpError> {
if !is_dcp(bytes) {
return Err(DcpError::NotDcp);
}
if bytes.len() as u64 > MAX_DCP_BYTES {
return Err(DcpError::Damaged("larger than 64 MiB".into()));
}
// the TIFF reader only knows 42 / BigTIFF / raw magics: give it a header with 42 in place of RC
let mut data = bytes.to_vec();
let forty_two: [u8; 2] = if bytes.first() == Some(&b'I') { [42, 0] } else { [0, 42] };
if let Some(m) = data.get_mut(2..4) {
m.copy_from_slice(&forty_two);
}
let opts = ParseOptions { follow_children: false, max_ifds: 4, value_budget: Some(64 << 20), ..Default::default() };
let tiff = Tiff::parse_with(&data, &opts).map_err(|e| DcpError::Damaged(e.to_string()))?;
let ifd: &Ifd = tiff.ifds.first().ok_or_else(|| DcpError::Damaged("no IFD".into()))?;
let mut color = color_data(ifd, ifd, tiff.order);
// a profile carries no white balance or calibration of a particular shot
color.as_shot_neutral = None;
color.as_shot_white_xy = None;
color.analog_balance = None;
color.camera_calibration = [None, None];
color.baseline_sharpness = None;
color.profile.gain_table_map = None;
// only BaselineExposureOffset belongs to a profile (BaselineExposure is the camera's)
color.baseline_exposure =
ifd.f64s(t::BASELINE_EXPOSURE_OFFSET).and_then(|v| v.first().copied()).filter(|v| v.is_finite()).unwrap_or(0.0).clamp(-4.0, 4.0);
// a single-illuminant profile names one illuminant; its second matrix slot stays empty
if color.color_matrix[0].is_none() {
// the first slot is the one every profile must fill (DNG: ColorMatrix1 is required)
return Err(DcpError::NoMatrix);
}
let text = |tag: u16| ifd.string(tag).map(|s| s.trim_matches(char::from(0)).trim().to_string()).unwrap_or_default();
Ok(DcpProfile { name: text(t::PROFILE_NAME), camera_model: text(t::UNIQUE_CAMERA_MODEL), color })
}
/// 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<Vec<u8>, DcpError> {
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()));
ifd.set(t::PROFILE_NAME, Value::Ascii(p.name.clone()));
let mat = |m: &lightcraft_color::Mat3| Value::SRational(m.0.iter().flatten().map(|v| srational(*v)).collect());
for i in 0..2 {
if let Some(m) = &c.color_matrix[i] {
ifd.set([t::CALIBRATION_ILLUMINANT_1, t::CALIBRATION_ILLUMINANT_2][i], Value::Short(vec![c.illuminant[i]]));
ifd.set([t::COLOR_MATRIX_1, t::COLOR_MATRIX_2][i], mat(m));
}
if let Some(m) = &c.forward_matrix[i] {
ifd.set([t::FORWARD_MATRIX_1, t::FORWARD_MATRIX_2][i], mat(m));
}
}
let flat = |table: &crate::profile::HsvTable| Value::Float(table.data.iter().flatten().copied().collect());
let dims =
|table: &crate::profile::HsvTable| Value::Long(vec![table.hue_divisions as u32, table.sat_divisions as u32, table.val_divisions as u32]);
if let Some(a) = c.profile.hue_sat_map[0].as_ref() {
ifd.set(t::PROFILE_HUE_SAT_MAP_DIMS, dims(a));
ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_1, flat(a));
if a.srgb_value {
ifd.set(t::PROFILE_HUE_SAT_MAP_ENCODING, Value::Long(vec![1]));
}
if let Some(b) = c.profile.hue_sat_map[1]
.as_ref()
.filter(|b| (b.hue_divisions, b.sat_divisions, b.val_divisions) == (a.hue_divisions, a.sat_divisions, a.val_divisions))
{
ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_2, flat(b));
}
}
if let Some(l) = &c.profile.look_table {
ifd.set(t::PROFILE_LOOK_TABLE_DIMS, dims(l));
ifd.set(t::PROFILE_LOOK_TABLE_DATA, flat(l));
if l.srgb_value {
ifd.set(t::PROFILE_LOOK_TABLE_ENCODING, Value::Long(vec![1]));
}
}
if let Some(tc) = &c.profile.tone_curve {
ifd.set(t::PROFILE_TONE_CURVE, Value::Float(tc.points.iter().flatten().copied().collect()));
}
if c.baseline_exposure != 0.0 {
ifd.set(t::BASELINE_EXPOSURE_OFFSET, Value::SRational(vec![srational(c.baseline_exposure)]));
}
let mut out = TiffWriter::new(ByteOrder::Little, false).write(&[ifd]).map_err(|e| DcpError::Damaged(e.to_string()))?;
// the DCP magic in place of 42
if let Some(m) = out.get_mut(2..4) {
m.copy_from_slice(&DCP_MAGIC.to_le_bytes());
}
Ok(out)
}
// The colour tags a profile shares with DNG, read as DNG reads them (a DNG's own are read by `craft-raw`).
fn mat3(v: Option<Vec<f64>>) -> Option<Mat3> {
let v = v?;
if v.len() != 9 || v.iter().any(|x| !x.is_finite()) {
return None;
}
let m = Mat3([[v[0], v[1], v[2]], [v[3], v[4], v[5]], [v[6], v[7], v[8]]]);
(m.determinant().abs() > 1e-12).then_some(m)
}
fn vec3(v: Option<Vec<f64>>) -> Option<[f64; 3]> {
let v = v?;
(v.len() == 3 && v.iter().all(|x| x.is_finite() && *x > 0.0)).then(|| [v[0], v[1], v[2]])
}
/// Colour tags: DNG puts them in IFD0, but some writers use the raw IFD; prefer the raw IFD.
/// `order` is the file's byte order (for the binary gain-table-map tags).
fn color_data(ifd0: &Ifd, raw: &Ifd, order: ByteOrder) -> ColorData {
let get = |tag: u16| raw.f64s(tag).or_else(|| ifd0.f64s(tag));
let geti = |tag: u16| raw.u16(tag).or_else(|| ifd0.u16(tag));
ColorData {
illuminant: [geti(t::CALIBRATION_ILLUMINANT_1).unwrap_or(0), geti(t::CALIBRATION_ILLUMINANT_2).unwrap_or(0)],
color_matrix: [mat3(get(t::COLOR_MATRIX_1)), mat3(get(t::COLOR_MATRIX_2))],
forward_matrix: [mat3(get(t::FORWARD_MATRIX_1)), mat3(get(t::FORWARD_MATRIX_2))],
camera_calibration: [mat3(get(t::CAMERA_CALIBRATION_1)), mat3(get(t::CAMERA_CALIBRATION_2))],
analog_balance: vec3(get(t::ANALOG_BALANCE)),
as_shot_neutral: vec3(get(t::AS_SHOT_NEUTRAL)),
as_shot_white_xy: get(t::AS_SHOT_WHITE_XY).filter(|v| v.len() == 2 && v[0] > 0.0 && v[1] > 0.0).map(|v| Xy::new(v[0], v[1])),
baseline_exposure: get(t::BASELINE_EXPOSURE).and_then(|v| v.first().copied()).filter(|v| v.is_finite()).unwrap_or(0.0)
+ get(t::BASELINE_EXPOSURE_OFFSET).and_then(|v| v.first().copied()).filter(|v| v.is_finite()).unwrap_or(0.0),
// DNG 1.7 BaselineSharpness (IFD 0 or the enhanced IFD); implausible values are ignored
baseline_sharpness: get(t::BASELINE_SHARPNESS).and_then(|v| v.first().copied()).filter(|v| v.is_finite() && *v > 0.0 && *v <= 16.0),
profile: profile_look(ifd0, raw, order),
kelvin_presets: None,
}
}
/// The profile look tags (`ProfileHueSatMap*`, `ProfileLookTable*`, `ProfileToneCurve`,
/// `ProfileGainTableMap*`); malformed ones are ignored. Like the colour tags, read from the raw IFD
/// first, else IFD 0.
fn profile_look(ifd0: &Ifd, raw: &Ifd, order: ByteOrder) -> ProfileLook {
let pick = |tag: u16| if raw.contains(tag) { raw } else { ifd0 };
let table = |dims: u16, data: u16, enc: u16| {
let ifd = pick(dims);
let dims = ifd.u64s(dims)?;
let data = ifd.f64s(data)?;
HsvTable::from_tags(&dims, &data, ifd.u64s(enc).and_then(|v| v.first().copied()).unwrap_or(0))
};
ProfileLook {
hue_sat_map: [
table(t::PROFILE_HUE_SAT_MAP_DIMS, t::PROFILE_HUE_SAT_MAP_DATA_1, t::PROFILE_HUE_SAT_MAP_ENCODING),
table(t::PROFILE_HUE_SAT_MAP_DIMS, t::PROFILE_HUE_SAT_MAP_DATA_2, t::PROFILE_HUE_SAT_MAP_ENCODING),
],
look_table: table(t::PROFILE_LOOK_TABLE_DIMS, t::PROFILE_LOOK_TABLE_DATA, t::PROFILE_LOOK_TABLE_ENCODING),
tone_curve: pick(t::PROFILE_TONE_CURVE).f64s(t::PROFILE_TONE_CURVE).and_then(|v| ToneCurve::from_tag(&v)),
gain_table_map: gain_table_map(ifd0, raw, order),
}
}
/// The gain table map (DNG 1.7 precedence): `ProfileGainTableMap2` from IFD 0 (the embedded
/// camera profile), else from the raw IFD; else `ProfileGainTableMap` from the raw IFD (where DNG
/// 1.6 put it, and where Apple writes it), else from IFD 0 (where DNG 1.7 says it was meant to be).
/// A malformed tag falls through to the next one.
fn gain_table_map(ifd0: &Ifd, raw: &Ifd, order: ByteOrder) -> Option<GainTableMap> {
let parse = |ifd: &Ifd, tag: u16| ifd.bytes(tag).and_then(|b| GainTableMap::parse(b, order, tag == t::PROFILE_GAIN_TABLE_MAP_2));
parse(ifd0, t::PROFILE_GAIN_TABLE_MAP_2)
.or_else(|| parse(raw, t::PROFILE_GAIN_TABLE_MAP_2))
.or_else(|| parse(raw, t::PROFILE_GAIN_TABLE_MAP))
.or_else(|| parse(ifd0, t::PROFILE_GAIN_TABLE_MAP))
}
+7 -4
View File
@@ -101,14 +101,17 @@ fn cr3_file(coding: &[u8], sample: &[u8]) -> Vec<u8> {
let mut co64 = vec![0u8; 4];
co64.extend(1u32.to_be_bytes()); // entry count
co64.extend([0u8; 8]); // sample offset, patched once the layout is known
let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat();
// One sample in chunk 1, using sample description 1. The shared parser
// deliberately refuses sample access without this mapping.
let stsc: Vec<u8> = [0u32, 1, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect();
let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsc", &stsc), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat();
let trak = box_bytes(b"trak", &box_bytes(b"mdia", &box_bytes(b"minf", &box_bytes(b"stbl", &tables))));
let mut file = box_bytes(b"ftyp", b"crx \0\0\0\x01crx isom");
file.extend(box_bytes(b"moov", &trak));
let offset = file.len();
file.extend_from_slice(sample);
let at = file.len() - sample.len() - 8; // the co64 offset field ends the moov
let at = file.len() - 8; // the co64 offset field ends the moov
let offset = file.len() + 8; // samples belong to the mdat payload, after its header
file[at..at + 8].copy_from_slice(&u64::try_from(offset).unwrap().to_be_bytes());
file.extend(box_bytes(b"mdat", sample));
file
}
+6 -4
View File
@@ -399,7 +399,10 @@ fn track_box(entry: &[u8], offset: u64, size: u32) -> Vec<u8> {
let mut co64 = vec![0u8; 4];
co64.extend(1u32.to_be_bytes());
co64.extend(offset.to_be_bytes());
let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat();
// One sample in chunk 1, using sample description 1. The shared parser
// deliberately refuses sample access without this mapping.
let stsc: Vec<u8> = [0u32, 1, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect();
let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsc", &stsc), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat();
box_bytes(b"trak", &box_bytes(b"mdia", &box_bytes(b"minf", &box_bytes(b"stbl", &tables))))
}
@@ -410,11 +413,10 @@ fn cr3_file(entry: &[u8], sample: &[u8]) -> Vec<u8> {
// The track length does not depend on the offset value (always a 64-bit field), so it can be
// measured with a placeholder before the real offset is known.
let track_len = track_box(entry, 0, sample.len() as u32).len();
let offset = (ftyp.len() + 8 + track_len + 7) & !7;
let offset = ftyp.len() + 8 + track_len + 8; // moov and mdat headers
let mut file = ftyp;
file.extend(box_bytes(b"moov", &track_box(entry, offset as u64, sample.len() as u32)));
file.resize(offset, 0);
file.extend_from_slice(sample);
file.extend(box_bytes(b"mdat", sample));
file
}
+53 -21
View File
@@ -631,18 +631,47 @@ fn malformed_profile_look_tags_are_ignored() {
/// Lossless JPEG XL codestream of `px` (`w × h × cpp` interleaved), 16-bit unless `eight`.
fn jxl_encode(px: &[u16], w: usize, h: usize, cpp: usize, eight: bool) -> Vec<u8> {
use zune_core::bit_depth::BitDepth;
use zune_core::colorspace::ColorSpace;
use zune_core::options::EncoderOptions;
let cs = if cpp == 1 { ColorSpace::Luma } else { ColorSpace::RGB };
let (bytes, depth): (Vec<u8>, _) = if eight {
(px.iter().map(|&v| v as u8).collect(), BitDepth::Eight)
} else {
(px.iter().flat_map(|v| v.to_ne_bytes()).collect(), BitDepth::Sixteen)
};
let mut out = Vec::new();
zune_jpegxl::JxlSimpleEncoder::new(&bytes, EncoderOptions::new(w, h, cs, depth)).encode(&mut out).unwrap();
out
if cpp == 1 {
// craft-jxl's public image encoder deliberately widens Gray to RGB. DNG RAW tiles need
// a genuine single-plane codestream, so use jixel's grayscale encoder for these fixtures.
let config = jixel::EncodeConfig::default().with_lossless(true);
let u8_buf: Vec<u8> = if eight { px.iter().map(|&v| v as u8).collect() } else { Vec::new() };
return if eight {
jixel::encode_image_gray(&u8_buf, w, h, &config).unwrap()
} else {
jixel::encode_image_gray_16bit(px, w, h, &config).unwrap()
};
}
let layout = craft_jxl::Layout::Rgb;
let u8_buf: Vec<u8> = if eight { px.iter().map(|&v| v as u8).collect() } else { Vec::new() };
let samples = if eight { craft_jxl::Samples::U8(&u8_buf) } else { craft_jxl::Samples::U16(px) };
let img = craft_jxl::ImageRef { width: w as u32, height: h as u32, layout, samples };
let opts = craft_jxl::EncodeOptions { lossless: true, ..Default::default() };
craft_jxl::encode(&img, opts, &craft_jxl::Metadata::default()).unwrap()
}
/// The encoder may return an ISO-BMFF container for a level-10 codestream. Extract its `jxlc`
/// payload so the tile fixture can deliberately exercise the bare-codestream form too.
fn bare_jxl_codestream(bytes: &[u8]) -> Vec<u8> {
const SIGNATURE: &[u8] = &[0, 0, 0, 0x0c, b'J', b'X', b'L', b' ', 0x0d, 0x0a, 0x87, 0x0a];
if !bytes.starts_with(SIGNATURE) {
return bytes.to_vec();
}
let mut at = SIGNATURE.len();
while let Some(end_header) = at.checked_add(8) {
let Some(header) = bytes.get(at..end_header) else { break };
let size = u32::from_be_bytes([header[0], header[1], header[2], header[3]]) as usize;
if size < 8 {
break;
}
let Some(end) = at.checked_add(size) else { break };
let Some(box_bytes) = bytes.get(at..end) else { break };
if &header[4..8] == b"jxlc" {
return box_bytes[8..].to_vec();
}
at = end;
}
panic!("the JPEG XL encoder returned a container without a complete jxlc box");
}
/// A bare codestream wrapped in the ISO-BMFF container (signature, `ftyp`, `jxlc`).
@@ -674,7 +703,7 @@ fn jxl_tiled(px: &[u16], w: usize, h: usize, cpp: usize, bits: u16, cfa: bool, t
}
}
}
let cs = jxl_encode(&t, cw, ch, cpp, bits == 8);
let cs = bare_jxl_codestream(&jxl_encode(&t, cw, ch, cpp, bits == 8));
tiles.push(if i % 3 == 1 { jxl_container(&cs) } else { cs });
}
}
@@ -773,21 +802,21 @@ fn a_jpeg_xl_tile_that_fails_is_an_error_for_the_image() {
#[test]
fn jpeg_xl_tiles_beyond_their_decode_budget_are_errors() {
use lightcraft_raw::RawError;
use zune_core::bit_depth::BitDepth;
use zune_core::colorspace::ColorSpace;
use zune_core::options::EncoderOptions;
let (w, h) = (512, 512);
let grey: Vec<u16> = (0..w * h).map(|i| ((i % w) * 97 + (i / w) * 31) as u16 & 0x0fff).collect();
let with_alpha: Vec<u8> = grey.iter().flat_map(|&v| [v, 4095 - v]).flat_map(u16::to_ne_bytes).collect();
let mut tile = Vec::new();
zune_jpegxl::JxlSimpleEncoder::new(&with_alpha, EncoderOptions::new(w, h, ColorSpace::LumaA, BitDepth::Sixteen)).encode(&mut tile).unwrap();
let with_alpha: Vec<u16> = grey.iter().flat_map(|&v| [v, 4095 - v]).collect();
// The public craft-jxl image encoder widens GrayAlpha to RGBA. This budget fixture
// specifically needs two encoded planes, not a four-plane layout mismatch.
let config = jixel::EncodeConfig::default().with_lossless(true);
let tile = jixel::encode_image_gray_alpha_16bit(&with_alpha, w, h, &config).unwrap();
let one = |tile: Vec<u8>| {
let mut raw = base_ifd(w, h, 12, 1, true);
raw.set(t::COMPRESSION, Value::Short(vec![compression::JPEG_XL]));
raw.set_image(ImageData::Tiles { tile_width: w as u32, tile_height: h as u32, tiles: vec![tile] });
dng(raw, ByteOrder::Little)
};
assert!(matches!(decode(&one(tile)), Err(RawError::Corrupt(_))));
let Err(RawError::Corrupt(error)) = decode(&one(tile)) else { panic!("the extra plane must exceed the tile budget") };
assert!(!error.contains("colour planes"), "must fail the budget, not the fixture layout: {error}");
assert_eq!(decode(&one(jxl_encode(&grey, w, h, 1, false))).unwrap().data, RawData::U16(grey));
}
@@ -1016,7 +1045,10 @@ fn malformed_semantic_masks_are_skipped() {
assert_eq!(decode(&bytes).unwrap().cpp, 3);
let masks = lightcraft_raw::semantic_masks(&bytes);
let names: Vec<&str> = masks.iter().map(|m| m.name.as_str()).collect();
assert_eq!(names, ["short strip", "sub area too small"]);
// A 4×4, 8-bit uncompressed mask needs 16 bytes: the three-byte strip
// is truncated, not a valid mask with zero-filled missing pixels. Keep the
// separate complete mask to test invalid MaskSubArea handling.
assert_eq!(names, ["sub area too small"]);
assert!(masks.iter().all(|m| m.sub_area.is_none()), "sub areas the mask doesn't fit are ignored");
for m in &masks {
let d = m.developed(Rect::new(0, 0, 8, 6), Rect::new(0, 0, 8, 6)).unwrap();
+6 -4
View File
@@ -202,7 +202,10 @@ mod cr3 {
let mut co64 = vec![0u8; 4];
co64.extend(1u32.to_be_bytes());
co64.extend(offset.to_be_bytes());
let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat();
// One sample in chunk 1, using sample description 1. The shared parser
// deliberately refuses sample access without this mapping.
let stsc: Vec<u8> = [0u32, 1, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect();
let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsc", &stsc), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat();
box_bytes(b"trak", &box_bytes(b"mdia", &box_bytes(b"minf", &box_bytes(b"stbl", &tables))))
}
@@ -231,11 +234,10 @@ mod cr3 {
let entry = box_bytes(b"CRAW", &craw);
let ftyp = box_bytes(b"ftyp", b"crx \0\0\0\x01crx isom");
let track_len = track(&entry, 0, sample.len() as u32).len();
let offset = (ftyp.len() + 8 + track_len + 7) & !7;
let offset = ftyp.len() + 8 + track_len + 8; // moov and mdat headers
let mut out = ftyp;
out.extend(box_bytes(b"moov", &track(&entry, offset as u64, sample.len() as u32)));
out.resize(offset, 0);
out.extend_from_slice(sample);
out.extend(box_bytes(b"mdat", sample));
out
}
+1 -1
View File
@@ -2343,7 +2343,7 @@ fn preview_only_raw_shows_badge_and_notices() {
// …which re-reads the file and says what came of it (a demo photo has none to re-read)
let r = h.request("ui.clickWidget", json!({"id": "button:reloadPreviewOnly:info"}), t);
assert_eq!(r["ok"], true, "{r}");
h.settle(SETTLE);
// Read the timed toast before settling advances the simulated clock past its expiry.
assert_eq!(h.app.ui.toast.clone().map(|x| x.0).as_deref(), Some("This raw variant still can't be decoded"));
// a regular photo: no notice
h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [other.0]}}), t);
+3 -3
View File
@@ -452,7 +452,7 @@ fn merge_dialog_retries_a_failed_preview_when_it_opens_again() {
std::fs::rename(&file, &away).unwrap();
crate::merge::open(&mut h.app, "merge.panorama").unwrap();
h.step();
assert!(h.step_until(T, |h| h.app.merge.preview_task.is_none()));
assert!(h.step_until(super::ui_settle_timeout(), |h| h.app.merge.preview_task.is_none()));
assert!(h.app.merge.error.is_some(), "the preview failed");
h.app.ui.dialog = None;
h.step();
@@ -464,7 +464,7 @@ fn merge_dialog_retries_a_failed_preview_when_it_opens_again() {
h.app.merge.preview.is_some() || h.app.merge.preview_task.is_some() || h.app.merge.error.is_some(),
"the dialog shows a preview, one being made, or why not"
);
assert!(h.step_until(T, |h| h.app.merge.preview_task.is_none()));
assert!(h.step_until(super::ui_settle_timeout(), |h| h.app.merge.preview_task.is_none()));
assert!(h.app.merge.preview.is_some(), "{:?}", h.app.merge.error);
h.app.ui.dialog = None;
h.step();
@@ -509,7 +509,7 @@ fn batch_merge_runs_each_set_in_turn() {
let rows = h.app.session.activity.list();
assert_eq!((rows.len(), rows.first().map(|r| r.kind)), (1, Some("merge")));
// the first merge finishes and the second starts by itself
assert!(h.step_until(T, |h| h.app.merge.batch.len() == 1 && h.app.merge.final_task.is_some()));
assert!(h.step_until(super::ui_settle_timeout(), |h| h.app.merge.batch.len() == 1 && h.app.merge.final_task.is_some()));
assert_eq!(h.app.session.catalog.len(), 10);
// cancelling the second drops the third
let rows = h.app.session.activity.list();
+23 -4
View File
@@ -2,9 +2,9 @@
//! responding for as long as that took (issue #3468): the photos are read on a worker thread under a
//! row of the activity stack.
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::mpsc::Receiver;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use lightcraft_catalog::{Op, Photo, PhotoId, Source};
@@ -27,6 +27,8 @@ const FRAMES: Duration = Duration::from_secs(15);
struct Drive {
open: Arc<AtomicBool>,
reads: Arc<AtomicUsize>,
/// The files the search read, in order.
read: Arc<Mutex<Vec<String>>>,
on_ui: Arc<AtomicUsize>,
}
@@ -37,14 +39,21 @@ fn app_on_a_slow_drive(guard: Duration) -> (Headless, Drive) {
let p = Photo::new(PhotoId(i + 1), Source::File { path }, &format!("IMG_{i:04}.arw"), "ARW", 64, 64, "2026-01-01T00:00:00");
app.session.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();
}
let drive = Drive { open: Arc::new(AtomicBool::new(false)), reads: Arc::new(AtomicUsize::new(0)), on_ui: Arc::new(AtomicUsize::new(0)) };
let (ui, open, reads, on_ui) = (std::thread::current().id(), drive.open.clone(), drive.reads.clone(), drive.on_ui.clone());
let drive = Drive {
open: Arc::new(AtomicBool::new(false)),
reads: Arc::new(AtomicUsize::new(0)),
read: Arc::new(Mutex::new(Vec::new())),
on_ui: Arc::new(AtomicUsize::new(0)),
};
let (ui, open, reads, read, on_ui) =
(std::thread::current().id(), drive.open.clone(), drive.reads.clone(), drive.read.clone(), drive.on_ui.clone());
let deadline = Instant::now() + guard;
app.session.media.file_loader = Some(Arc::new(move |path: &str, _, _| {
// (the grid reads the photos for its thumbnails too, on threads of its own)
let thread = std::thread::current();
if thread.name().is_some_and(|n| n.starts_with("similar-") || n.contains(LABEL)) {
reads.fetch_add(1, Ordering::SeqCst);
read.lock().unwrap().push(path.to_string());
}
if thread.id() == ui {
on_ui.fetch_add(1, Ordering::SeqCst);
@@ -167,7 +176,17 @@ fn stopping_leaves_the_view_alone_and_the_next_search_reads_only_the_rest() {
let r = h.app.run("library.findSimilar", json!({"id": 1})).unwrap();
assert_eq!(r["count"], PHOTOS - 1, "what was read isn't read again");
until_done(&mut h);
assert_eq!(drive.reads.load(Ordering::SeqCst), PHOTOS as usize);
// A photo the loupe or the filmstrip decoded meanwhile is measured from memory, without a read
// (issue #3823: 10 or 11 reads of 12 on a loaded machine), so what is checked is that no file
// was read twice, the searched photo's above all; the answer below needs every photo.
let read = drive.read.lock().unwrap().clone();
let mut once = read.clone();
once.sort();
once.dedup();
assert_eq!(once.len(), read.len(), "a file was read twice: {read:?}");
assert_eq!(read.iter().filter(|p| p.ends_with("IMG_0000.arw")).count(), 1, "the searched photo was read again: {read:?}");
// and every photo was measured, from a read or from memory, not only the ones alike
assert_eq!(h.app.session.signatures.len(), PHOTOS as usize, "a photo was not measured");
assert_eq!(h.app.session.filter.only.len(), PHOTOS as usize / 2);
}
+4 -4
View File
@@ -162,7 +162,7 @@ mod in_the_loupe {
let mut h = Headless::new(app, size, 1.0);
let r = h.request("ui.set", json!({"view": "detail", "right": "none", "filmstrip": false}), T);
assert_eq!(r["ok"], true, "{r}");
h.settle(SETTLE);
h.settle(super::super::ui_settle_timeout());
let native = {
let id = h.app.session.active().expect("a photo is open");
let p = h.app.session.catalog.photo(id).expect("it is in the catalog");
@@ -811,7 +811,7 @@ mod in_the_loupe {
fn pinch_display_fit_refreshes_main_and_before_after_a_short_pinch() {
let (mut h, _) = demo_detail_in([1400.0, 900.0]);
h.app.ui.before_after = crate::state::BeforeAfter::SideBySide;
assert!(h.settle(SETTLE));
assert!(h.settle(super::super::ui_settle_timeout()));
let slots = [Slot::Main, Slot::Before];
let keys = slots.map(|slot| h.app.renderer.wanted(slot).unwrap());
let initial_fit = drawn_fit(&h);
@@ -848,7 +848,7 @@ mod in_the_loupe {
#[test]
fn pinch_display_fit_does_not_follow_the_old_photo() {
let (mut h, _) = demo_detail_in([1400.0, 900.0]);
assert!(h.settle(SETTLE));
assert!(h.settle(super::super::ui_settle_timeout()));
let old = h.app.session.active().unwrap();
let next = h.app.session.visible_cloned().into_iter().find(|id| *id != old).unwrap();
let initial_fit = drawn_fit(&h);
@@ -874,7 +874,7 @@ mod in_the_loupe {
fn pinch_display_fit_is_shared_with_a_hover_during_the_hold() {
let (mut h, _) = demo_detail_in([1400.0, 900.0]);
assert_eq!(h.request("engine.execute", json!({"command": "panel.profiles"}), T)["ok"], true);
assert!(h.settle(SETTLE));
assert!(h.settle(super::super::ui_settle_timeout()));
let initial_fit = drawn_fit(&h);
let work = std::rc::Rc::new(std::cell::RefCell::new(PinchWork::default()));
h.app.renderer.set_offload(Box::new(PinchOffload(work.clone())));
+3 -3
View File
@@ -457,7 +457,7 @@ fn a_photo_deleted_while_it_plays_does_not_stall_the_slideshow() {
run(&mut h, "photo.delete", json!({"ids": [next.0]}));
run(&mut h, "photo.deletePermanently", json!({"ids": [next.0]}));
assert!(h.app.session.catalog.photo(next).is_none());
assert!(h.step_until(SETTLE, |h| h.app.ui.slideshow.as_ref().is_some_and(|r| r.play.position().0 >= 3)), "it plays on");
assert!(h.step_until(super::ui_settle_timeout(), |h| h.app.ui.slideshow.as_ref().is_some_and(|r| r.play.position().0 >= 3)), "it plays on");
assert!(photos.len() > 3);
h.settle(SETTLE);
}
@@ -630,7 +630,7 @@ fn settings_shows_the_slide_layout_and_a_live_preview() {
assert!(has(&h, w), "{w}");
}
// the preview is the composed slide, rendered off the UI thread
assert!(h.step_until(SETTLE, |h| h.app.renderer.textures.get(&Slot::SlidePreview).is_some_and(|t| t.photo == first)));
assert!(h.step_until(super::ui_settle_timeout(), |h| h.app.renderer.textures.get(&Slot::SlidePreview).is_some_and(|t| t.photo == first)));
// a checkbox goes through slideshow.layout, and its controls appear (scrolled into view)
reveal(&mut h, "check:settings.slideshowShadow");
let r = h.request("ui.clickWidget", json!({"id": "check:settings.slideshowShadow"}), T);
@@ -641,7 +641,7 @@ fn settings_shows_the_slide_layout_and_a_live_preview() {
// a changed layout renders the preview again (once it has settled)
let before = h.app.renderer.textures.get(&Slot::SlidePreview).map(|t| t.key);
run(&mut h, "slideshow.layout", json!({"margins": 0.2, "backdropColor": "#00ff00"}));
assert!(h.step_until(SETTLE, |h| h.app.renderer.textures.get(&Slot::SlidePreview).map(|t| t.key) != before));
assert!(h.step_until(super::ui_settle_timeout(), |h| h.app.renderer.textures.get(&Slot::SlidePreview).map(|t| t.key) != before));
h.step();
// (the preview is pinned above the page: in view already)
reveal(&mut h, "slideshow:preview");
+3 -2
View File
@@ -19,8 +19,8 @@ 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 (`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 | |
| HEIC / HEIF | ✅ | ⬜ (Classic) / HDR only | ✅ in every release build (`craft-heif` from craft-libs, heic-rs) | ⬜ | `heif` feature on by packaging scripts; EXIF and XMP read from the container in every build (`crates/meta/src/heif.rs`, #693) |
| AVIF | ✅ | ✅ (HDR) | ⬜ recognised, not decoded (no permissive pure-Rust AV1 decoder) | ✅ 8/10-bit, PQ HDR | EXIF and XMP read from the container (`crates/meta/src/heif.rs`); exported EXIF reads back |
| 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 |
| GIF, BMP | ⬜ | ⬜ | ✅ | ⬜ | beyond target |
@@ -79,5 +79,6 @@ Remaining: **26–47 h** (AVIF decode 8–15 h if a permissive decoder is writte
| 2026-10-10 | major | Created from the code and the installed Lightroom's document types |
| 2026-10-10 | minor | JPEG XL export (#247): JXL write ✅ (native only), PQ JPEG XL HDR |
| 2026-10-12 | minor | Render to DNG: "Rendered DNG" export format (linear 16-bit float DNG of the rendered photo) |
| 2026-10-12 | minor | HEIC / HEIF and AVIF: EXIF and XMP read from the container, without the decoder (#693); statuses unchanged |
| 2026-10-12 | minor | Lossy DNG export (JPEG tiles, `dngCompression: "lossy"`); PSD write had already landed (#953), so it leaves the remaining hours |
| 2026-10-13 | patch | JPEG write (#1373): Huffman tables made for each image, 12–14 % smaller files at the same pixels; statuses unchanged |
+4 -2
View File
@@ -1,6 +1,6 @@
# Target-app parity: LightCraft vs Adobe Lightroom
> **Last reviewed:** 2026-10-10 · **Target:** Adobe Lightroom Classic 15.5 (August 2026) and Lightroom 9.6 (desktop) · **Changes:** [revision history](#revision-history)
> **Last reviewed:** 2026-11-15 · **Target:** Adobe Lightroom Classic 15.5 (August 2026) and Lightroom 9.6 (desktop) · **Changes:** [revision history](#revision-history)
The authoritative assessment of how close LightCraft is to Lightroom. The row-by-row checklist is
[parity-checklist.md](parity-checklist.md); the ranked work list is [gaps.md](gaps.md); deep areas
@@ -159,7 +159,7 @@ essentials replaces its guessed 45% raw look with 84% × 0.75 (+1 point).
|---|---:|---:|---|
| Interaction fidelity / UI clarity | ×0.95 | ×0.95 | brush live preview gaps and feathering (#517, #518); crop artefacts and limits (#731, #742, #770, #771); Compare white-balance selector (#743–#745); low-res slider previews (#330); dialogs taller than the window (#781); mouse-wheel scroll (#714) |
| Stability on real machines | ×0.92 | ×0.92 | launch and quit crashes or silent startup failures (#250 macOS 12 Intel, #315 Windows Intel GPU, #431 Intel Mac, #260, #462, #783 Linux / Nouveau, #790 Windows installer, fixed in 0.9.1); tests run in CI on Linux x64 and FreeBSD (nightly), not on macOS or Windows (gap 9) |
| File exchange with Lightroom users (mainstream) / opening files people send (essentials) | ×0.92 | ×0.95 | Adobe XMP sidecars change colours (#290), XMP CameraProfile and Upright ignored (#204, #205), catalog import fails (#599), same Light values render differently (#523, #209), no write-back to `.lrcat`; for casual users: CR3 beyond verified bodies, HE NEF open as previews (#473, #556), HEIC trouble (#693) |
| File exchange with Lightroom users (mainstream) / opening files people send (essentials) | ×0.92 | ×0.95 | Adobe XMP sidecars change colours (#290), XMP CameraProfile and Upright ignored (#204, #205), catalog import fails (#599), same Light values render differently (#523, #209), no write-back to `.lrcat`; for casual users: CR3 beyond verified bodies, HE NEF open as previews (#473, #556) |
| **Product** | **×0.804** | **×0.830** | |
### Mainstream practitioner: ~51%
@@ -299,6 +299,7 @@ P2 items 12–25 h; look tuning, performance, packaging, hardening 25–45 h.
| Date | Change | Summary |
|---|---|---|
| 2026-11-15 | minor | Lens Blur renders with AI subject focus and per-mask bokeh (the AI table's 🟡 and the missing depth map are unchanged); survey layout, opened-library folder safety, direct Lightroom backup-zip import, legacy shutter repair and the Highlights/Shadows block fix landed; craft-libs cutover (TIFF/JPEG/JXL/GIF/PSD/HEIF/raw, metadata, gain maps); v0.21.0 and v0.21.1 released. Headline numbers, coverage and estimates were not re-measured. |
| 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-05 | minor | AI table: Lens Blur ⬜ → 🟡 (subject- or mask-focused disc blur, no depth map); headline numbers unchanged |
| 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). |
@@ -317,6 +318,7 @@ P2 items 12–25 h; look tuning, performance, packaging, hardening 25–45 h.
| 2026-10-14 | minor | Camera-coverage sweep (#1327): 30 models verified on 40 CC0 files (`coverage_sweep_corpus.rs`, `coverage-sweep.tsv`; `docs/raw/coverage.py` 515 → 545 verified rows); the counts above are not re-measured here. Sensor verification does not establish colour parity (#1277); headline numbers unchanged |
| 2026-10-12 | minor | 22 more CR3 bodies verified on one CC0 file each (raw-parity.md): use-weighted share whose camera decodes 85% → 92%, RAW decoding 77% → 83%; full 60.7% → 61.2% (~61%), mainstream depth 72.1% → 74.0% (~49% → ~51%), essentials depth 77.5% → 78.0% (~64% → ~65%) |
| 2026-10-12 | minor | 117 Nikon and Fujifilm bodies verified (raw-parity.md): use-weighted share whose camera decodes 85% → 86%, RAW decoding 77% (unchanged); mainstream depth 72.1% → 72.4%, essentials depth 77.5% → 77.6%; headline numbers unchanged |
| 2026-10-12 | minor | File-exchange discount: "HEIC trouble (#693)" is no longer among its reasons (HEIC decodes in release builds since #609 and keeps its EXIF and XMP since #693); the factor (×0.92 / ×0.95) is not re-measured and stays as it is |
| 2026-10-11 | minor | EOS R5 Mark II CR3 verified (raw-parity.md): use-weighted share whose camera decodes 84% → 85%, RAW decoding 76% → 77%; full 60.6% → 60.7%, mainstream depth 71.8% → 72.1%, essentials depth 77.4% → 77.5%; the rounded numbers (~61%, ~49%, ~64%) are unchanged |
| 2026-10-10 | minor | RAW measured model by model (raw-parity.md): RAW decoding (76%) and colour (35%) are explicit rows of the full sum (59% → 61%); mainstream uses the use-weighted 84% plus a ×0.85 colour discount (51% → 49%); essentials uses 84% × colour 0.75 (63% → 64%); beta ~14 points |
| 2026-10-10 | minor | Full number restored to an additive weighted sum over the dimensions (~59%; no new evidence); hours to ~95% for each of the three audiences; beta distance ~16 points |
File diff suppressed because it is too large. Load diff
@@ -2,13 +2,14 @@
"""Regenerate cargo-sources.json for the from-source Flatpak manifest.
The upstream flatpak-cargo-generator is downloaded from a pinned commit and
verified before use; the full script is intentionally not vendored here. Two
small, reviewed patches are applied in memory:
verified before use; the full script is intentionally not vendored here. The following
small patches are applied in memory:
1. Key cargo source replacements by the full source URL (including ?rev=...)
instead of only the repository URL, so two different revisions of the same
repository stay distinct.
2. Include Cargo.lock's [patch.unused] entries, so crates such as egui_demo_lib
2. Isolate Git package directories and replacement sources by locked identity.
3. Include Cargo.lock's [patch.unused] entries, so crates such as egui_demo_lib
are vendored even though they are not in the resolved package graph.
This only produces and validates the source inventory; a full Flatpak SDK
@@ -42,7 +43,7 @@ UPSTREAM_URL = (
f"{UPSTREAM_COMMIT}/cargo/flatpak-cargo-generator.py"
)
UPSTREAM_SHA256 = "0a2db6be87d75910facef28ab46d4d6460802e8419ab850d0caa6a364d26b380"
PATCHED_SHA256 = "1e7c61d98d18e88156fb8cd4b91f526e04a6c504b185ae7b12f775ec1dd48512"
PATCHED_SHA256 = "56e06136037ca106a690c3c6d50d5b385462f6567ca63afdd7efb257049efe86"
# Allow cold git caches while bounding stalled inventory regeneration.
GENERATOR_TIMEOUT_SECONDS = 30 * 60
@@ -85,6 +86,48 @@ PATCHES = (
),
)
# Isolate same-name/version Git packages by complete locked source identity.
PATCHES += (
(
'isolate Git directory sources',
""" logging.info("Adding package %s from %s", name, repo_url)
git_pkg = git_repo["commits"][commit][name]
""",
""" # Directory sources cannot distinguish equal name/version packages from
# different Git revisions. Isolate both the files and replacement source.
identity = hashlib.sha256(source.encode("utf-8")).hexdigest()
vendor_name = f"git-vendor-{identity}"
vendor_root = f"{CARGO_HOME}/vendor-git/{identity}"
for entry in cargo_vendored_entry.values():
entry["replace-with"] = vendor_name
cargo_vendored_entry[vendor_name] = {"directory": vendor_root}
logging.info("Adding package %s from %s", name, repo_url)
git_pkg = git_repo["commits"][commit][name]
""",
),
(
'create isolated Git package directory',
""" f'cp -r --reflink=auto "{pkg_repo_dir}" "{CARGO_CRATES}/{name}"'""",
""" f'mkdir -p "{vendor_root}" && '
f'cp -r --reflink=auto "{pkg_repo_dir}" "{vendor_root}/{name}"'""",
),
(
'isolate Git Cargo.toml destination',
""" "dest": f"{CARGO_CRATES}/{name}", # -{version}',
"dest-filename": "Cargo.toml",""",
""" "dest": f"{vendor_root}/{name}",
"dest-filename": "Cargo.toml",""",
),
(
'isolate Git .cargo-checksum.json destination',
""" "dest": f"{CARGO_CRATES}/{name}", # -{version}',
"dest-filename": ".cargo-checksum.json",""",
""" "dest": f"{vendor_root}/{name}",
"dest-filename": ".cargo-checksum.json",""",
),
)
def sha256(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()
@@ -479,5 +479,49 @@ class InventoryFailures(unittest.TestCase):
self.assertEqual(list(root.glob('.sources.json.*.tmp')), [])
class GitSourceIsolationTests(unittest.TestCase):
"""Exercise the identity block inserted into the hash-pinned generator."""
def identity(self, source, name='same-crate'):
import logging
import textwrap
from urllib.parse import urlparse
repo = 'https://github.com/example/formats'
commit = urlparse(source).fragment
key = source.removeprefix('git+').split('#', 1)[0]
scope = {
'hashlib': hashlib, 'source': source, 'CARGO_HOME': 'cargo',
'cargo_vendored_entry': {key: {'git': repo, 'replace-with': 'vendored-sources'}},
'logging': logging, 'name': name, 'repo_url': repo, 'commit': commit,
'git_repo': {'commits': {commit: {name: object()}}},
}
block = next(new for label, _, new in wrapper.PATCHES if label == 'isolate Git directory sources')
exec(textwrap.dedent(block), scope)
mapping = scope['cargo_vendored_entry']
replacement = mapping[key]['replace-with']
self.assertEqual(mapping[replacement]['directory'], scope['vendor_root'])
self.assertNotEqual(replacement, 'vendored-sources')
return scope['vendor_root'], replacement
def test_same_package_and_version_at_two_revisions_are_isolated(self):
prefix = 'git+https://github.com/example/formats?rev='
first = self.identity(prefix + 'a' * 40 + '#' + 'a' * 40)
second = self.identity(prefix + 'b' * 40 + '#' + 'b' * 40)
self.assertNotEqual(first[0], second[0])
self.assertNotEqual(first[1], second[1])
self.assertTrue(first[0].startswith('cargo/vendor-git/'))
def test_packages_from_one_locked_source_share_one_root(self):
source = 'git+https://github.com/example/formats?rev=' + 'a' * 40 + '#' + 'a' * 40
self.assertEqual(self.identity(source, 'metadata'), self.identity(source, 'tiff'))
def test_distinct_source_aliases_do_not_repeat_copy_into_same_directory(self):
commit = 'a' * 40
first = self.identity('git+https://github.com/example/formats?rev=' + commit + '#' + commit)
second = self.identity('git+https://github.com/example/formats?tag=release#' + commit)
self.assertNotEqual(first, second)
if __name__ == '__main__':
unittest.main(verbosity=2)