mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 19:13:28 +00:00
image-codecs: share ISO-BMFF brand prefix framing (#2638)
Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
This commit is contained in:
@@ -454,10 +454,11 @@ pub fn detect(bytes: &[u8]) -> Option<Format> {
|
||||
return Some(Format::Hdr);
|
||||
}
|
||||
if b.len() >= 12 && &b[4..8] == b"ftyp" {
|
||||
if is_avif_ftyp(b) {
|
||||
let prefix = &b[..b.len().min(64)];
|
||||
if is_avif_ftyp(prefix) {
|
||||
return Some(Format::Avif);
|
||||
}
|
||||
if is_heif_ftyp(b) {
|
||||
if is_heif_ftyp(prefix) {
|
||||
return Some(Format::Heif);
|
||||
}
|
||||
}
|
||||
@@ -505,11 +506,21 @@ pub fn detect(bytes: &[u8]) -> Option<Format> {
|
||||
None
|
||||
}
|
||||
|
||||
/// The `ftyp` brands: the major brand, then the compatible ones (the minor version between them is skipped).
|
||||
fn ftyp_brands(b: &[u8]) -> impl Iterator<Item = &[u8; 4]> {
|
||||
let size = u32::from_be_bytes([b[0], b[1], b[2], b[3]]) as usize;
|
||||
let end = size.clamp(12, b.len().min(64));
|
||||
b[8..end].as_chunks::<4>().0.iter().enumerate().filter(|(i, _)| *i != 1).map(|(_, c)| c)
|
||||
/// The major brand and complete compatible brands in a leading ISO-BMFF `ftyp` box.
|
||||
///
|
||||
/// Pass at most the bytes the caller wants to inspect. A missing box or incomplete major
|
||||
/// brand yields an empty iterator. For sniffing, a declared size below 12 still permits the
|
||||
/// major brand. Compatible brands stop at the declared box end or the available prefix,
|
||||
/// whichever comes first. This does not validate the full box.
|
||||
pub fn ftyp_brands(prefix: &[u8]) -> impl Iterator<Item = &[u8; 4]> + Clone {
|
||||
let brands = match prefix.first_chunk::<12>() {
|
||||
Some(header) if &header[4..8] == b"ftyp" => {
|
||||
let size = u32::from_be_bytes([header[0], header[1], header[2], header[3]]) as usize;
|
||||
&prefix[8..size.clamp(12, prefix.len())]
|
||||
}
|
||||
_ => &[],
|
||||
};
|
||||
brands.as_chunks::<4>().0.iter().enumerate().filter(|(i, _)| *i != 1).map(|(_, brand)| brand)
|
||||
}
|
||||
|
||||
fn is_avif_ftyp(b: &[u8]) -> bool {
|
||||
|
||||
@@ -59,7 +59,9 @@ pub use crate::codecs::tiff_ifd::{ExtraSampleKind, InkSet, TiffInfo, TiffPage, T
|
||||
pub use crate::error::CodecError;
|
||||
pub use crate::exif_color::{EXIF_COLOR_SPACE_SRGB, exif_color_space};
|
||||
pub use crate::fidelity::{FidelityWarning, fidelity_warnings, fidelity_warnings_with};
|
||||
pub use crate::format::{ASYMMETRIC_EXCEPTIONS, Detection, Format, FormatCaps, PrefixDetector, caps, detect, detect_prefix, from_extension};
|
||||
pub use crate::format::{
|
||||
ASYMMETRIC_EXCEPTIONS, Detection, Format, FormatCaps, PrefixDetector, caps, detect, detect_prefix, from_extension, ftyp_brands,
|
||||
};
|
||||
pub use crate::image::{
|
||||
ChannelLayout, CryptomatteBuffer, CryptomatteLayer, DecodeWarning, DeepChannel, DeepImage, ExrChannelInfo, ExrPartInfo, ExtraSample, Image,
|
||||
MAX_EXTRA_SAMPLES, Metadata, SampleType, convert_samples,
|
||||
|
||||
@@ -108,6 +108,34 @@ fn detect_heif_brands() {
|
||||
assert_eq!(detect(b"\0\0\0\x14ftypqt \0\0\0\0qt "), None, "QuickTime is not HEIF");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ftyp_brands_respect_prefix_and_declared_bounds() {
|
||||
let mut prefix = *b"\0\0\0\x1cftypisomavifavismif1xx";
|
||||
for end in 0..12 {
|
||||
assert!(ftyp_brands(&prefix[..end]).next().is_none(), "prefix length {end}");
|
||||
}
|
||||
assert!(ftyp_brands(b"\0\0\0\x0cmdatheic").next().is_none());
|
||||
assert!(ftyp_brands(&prefix).eq([b"isom", b"avis", b"mif1"]));
|
||||
assert!(ftyp_brands(&prefix[..18]).eq([b"isom"]));
|
||||
|
||||
prefix[..4].copy_from_slice(&20u32.to_be_bytes());
|
||||
assert!(ftyp_brands(&prefix).eq([b"isom", b"avis"]));
|
||||
prefix[..4].copy_from_slice(&8u32.to_be_bytes());
|
||||
assert!(ftyp_brands(&prefix).eq([b"isom"]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detect_ftyp_keeps_its_short_brand_scan() {
|
||||
let mut prefix = [0; 68];
|
||||
prefix[..12].copy_from_slice(b"\0\0\0\x44ftypheic");
|
||||
prefix[64..68].copy_from_slice(b"avif");
|
||||
assert!(ftyp_brands(&prefix).any(|brand| brand == b"avif"));
|
||||
assert_eq!(detect(&prefix), Some(Format::Heif));
|
||||
|
||||
prefix[60..64].copy_from_slice(b"avif");
|
||||
assert_eq!(detect(&prefix), Some(Format::Avif));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn caps_are_internally_consistent() {
|
||||
for f in Format::ALL {
|
||||
|
||||
Reference in New Issue
Block a user