Use shared ISO-BMFF brand framing for image sniffing (#4617)

* Stage the shared brand reader dependency

* Use shared ISO-BMFF brand framing for image sniffing
This commit is contained in:
Terra854
2026-11-21 13:04:34 -05:00
committed by GitHub
parent 35527de6a3
commit 81db84ab43
3 changed files with 70 additions and 22 deletions
Generated
+51 -8
View File
@@ -1239,7 +1239,7 @@ dependencies = [
[[package]]
name = "craft-dds"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3#23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3"
source = "git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba#494fceac863aa36eb1a6d1ecc2c95acd26319cba"
dependencies = [
"rayon",
]
@@ -1342,14 +1342,14 @@ dependencies = [
[[package]]
name = "craft-image-codecs"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3#23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3"
source = "git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba#494fceac863aa36eb1a6d1ecc2c95acd26319cba"
dependencies = [
"avif-serialize",
"craft-dds",
"craft-jpeg",
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3)",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3)",
"craft-video",
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba)",
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba)",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba)",
"craft-video 0.1.0 (git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba)",
"crc32fast",
"exr",
"flate2",
@@ -1379,6 +1379,14 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-jpeg"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba#494fceac863aa36eb1a6d1ecc2c95acd26319cba"
dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-jxl"
version = "0.1.0"
@@ -1405,6 +1413,11 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-media"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba#494fceac863aa36eb1a6d1ecc2c95acd26319cba"
[[package]]
name = "craft-metadata"
version = "0.1.0"
@@ -1417,6 +1430,18 @@ dependencies = [
"serde",
]
[[package]]
name = "craft-metadata"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba#494fceac863aa36eb1a6d1ecc2c95acd26319cba"
dependencies = [
"brotli-decompressor 6.0.1",
"chrono",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba)",
"quick-xml 0.41.0",
"serde",
]
[[package]]
name = "craft-metadata"
version = "0.1.0"
@@ -1511,6 +1536,15 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-tiff"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba#494fceac863aa36eb1a6d1ecc2c95acd26319cba"
dependencies = [
"serde",
"thiserror 2.0.21",
]
[[package]]
name = "craft-tiff"
version = "0.1.0"
@@ -1588,6 +1622,15 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-video"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba#494fceac863aa36eb1a6d1ecc2c95acd26319cba"
dependencies = [
"craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=494fceac863aa36eb1a6d1ecc2c95acd26319cba)",
"thiserror 2.0.21",
]
[[package]]
name = "craft-window"
version = "0.1.0"
@@ -4045,13 +4088,13 @@ dependencies = [
"craft-gif",
"craft-heif",
"craft-image-codecs",
"craft-jpeg",
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3)",
"craft-jxl",
"craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=057b0e3e01aed74ecc5d18eb047ddd7328b46b1d)",
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3)",
"craft-psd",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3)",
"craft-video",
"craft-video 0.1.0 (git+https://github.com/storytold/craft-libs?rev=23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3)",
"dhat",
"jpeg-encoder 0.7.1",
"lightcraft-color",
+1 -1
View File
@@ -27,7 +27,7 @@ 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 = "23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3" }
craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3" }
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3" }
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "494fceac863aa36eb1a6d1ecc2c95acd26319cba" }
craft-media = { git = "https://github.com/storytold/craft-libs", rev = "057b0e3e01aed74ecc5d18eb047ddd7328b46b1d", default-features = false, features = ["isobmff", "matroska"] }
craft-video = { git = "https://github.com/storytold/craft-libs", rev = "23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3", features = ["av1", "av1-large-encode", "threads"] }
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3" }
+18 -13
View File
@@ -133,15 +133,8 @@ pub fn sniff(b: &[u8]) -> Option<Format> {
}
fn sniff_bmff(b: &[u8]) -> Option<Format> {
let size = u32::from_be_bytes([b[0], b[1], b[2], b[3]]) as usize;
let end = size.clamp(12, b.len().min(4096));
let mut brands: Vec<&[u8]> = vec![&b[8..12]];
let mut p = 16;
while p + 4 <= end {
brands.push(&b[p..p + 4]);
p += 4;
}
let has = |x: &[u8]| brands.contains(&x);
let mut brands = craft_image_codecs::ftyp_brands(&b[..b.len().min(4096)]);
let has = |brand: &[u8; 4]| brands.clone().any(|candidate| candidate == brand);
if has(b"crx ") {
return Some(Format::RawOther);
}
@@ -151,10 +144,9 @@ fn sniff_bmff(b: &[u8]) -> Option<Format> {
if has(b"avif") || has(b"avis") {
return Some(Format::Avif);
}
if [&b"heic"[..], b"heix", b"hevc", b"hevx", b"heim", b"heis", b"hevm", b"hevs", b"mif1", b"msf1"].iter().any(|x| has(x)) {
return Some(Format::Heif);
}
None
brands
.any(|brand| matches!(brand, b"heic" | b"heix" | b"hevc" | b"hevx" | b"heim" | b"heis" | b"hevm" | b"hevs" | b"mif1" | b"msf1"))
.then_some(Format::Heif)
}
/// Heuristic camera-raw detection for classic TIFF: DNGVersion, the CR2 signature, CFA/LinearRaw
@@ -367,6 +359,19 @@ mod tests {
b
}
#[test]
fn bmff_probe_reads_the_last_complete_brand_in_its_prefix() {
let mut compatible = vec![b"zzzz"; 1021];
compatible[1019] = b"avif";
compatible[1020] = b"crx ";
let bytes = ftyp_box(None, b"heic", &compatible);
// AVIF starts at 4092, well beyond the shared image detector's 64-byte scan.
// CR3 starts at 4096, just past LightCraft's scan, so it cannot take precedence.
assert_eq!(sniff(&bytes), Some(Format::Avif));
assert_eq!(sniff(&bytes[..4095]), Some(Format::Heif));
}
#[test]
fn bmff_probe_precedence_cr3_jxl_avif_heif() {
// CR3 (`crx `) wins over JXL, AVIF, and HEIF regardless of brand position.