Skip to main content

ai_imagesize/container/
heif.rs

1use crate::util::*;
2use crate::{ImageError, ImageResult, ImageSize};
3
4use no_std_io::io::{BufRead, Seek, SeekFrom};
5
6// REFS: https://github.com/strukturag/libheif/blob/f0c1a863cabbccb2d280515b7ecc73e6717702dc/libheif/heif.h#L600
7#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub enum Compression {
9    Av1,
10    Hevc,
11    Jpeg,
12    Unknown,
13    // unused(reuse in the future?)
14    // Avc,
15    // Vvc,
16    // Evc,
17}
18
19pub fn size<R: BufRead + Seek>(reader: &mut R) -> ImageResult<ImageSize> {
20    reader.seek(SeekFrom::Start(0))?;
21    //  Read the ftyp header size
22    let ftyp_size = read_u32(reader, &Endian::Big)?;
23
24    //  Jump to the first actual box offset
25    reader.seek(SeekFrom::Start(ftyp_size.into()))?;
26
27    //  Skip to meta tag which contains all the metadata
28    skip_to_tag(reader, b"meta")?;
29    read_u32(reader, &Endian::Big)?; //  Meta has a junk value after it
30    skip_to_tag(reader, b"iprp")?; //  Find iprp tag
31
32    let mut ipco_size = skip_to_tag(reader, b"ipco")? as usize; //  Find ipco tag
33
34    //  Keep track of the max size of ipco tag
35    let mut max_width = 0usize;
36    let mut max_height = 0usize;
37    let mut found_ispe = false;
38    let mut rotation = 0u8;
39
40    while let Ok((tag, size)) = read_tag(reader) {
41        //  Size of tag length + tag cannot be under 8 (4 bytes each)
42        if size < 8 {
43            return Err(ImageError::CorruptedImage);
44        }
45
46        //  ispe tag has a junk value followed by width and height as u32
47        if tag == "ispe" {
48            found_ispe = true;
49            read_u32(reader, &Endian::Big)?; //  Discard junk value
50            let width = read_u32(reader, &Endian::Big)? as usize;
51            let height = read_u32(reader, &Endian::Big)? as usize;
52
53            //  Assign new largest size by area
54            if width * height > max_width * max_height {
55                max_width = width;
56                max_height = height;
57            }
58        } else if tag == "irot" {
59            // irot is 9 bytes total: size, tag, 1 byte for rotation (0-3)
60            rotation = read_u8(reader)?;
61        } else if size >= ipco_size {
62            // If we've gone past the ipco boundary, then break
63            break;
64        } else {
65            // If we're still inside ipco, consume all bytes for
66            // the current tag, minus the bytes already read in `read_tag`
67            ipco_size -= size;
68            reader.seek(SeekFrom::Current(size as i64 - 8))?;
69        }
70    }
71
72    //  If no ispe found, then we have no actual dimension data to use
73    if !found_ispe {
74        return Err(
75            no_std_io::io::Error::new(no_std_io::io::ErrorKind::UnexpectedEof, "Not enough data").into(),
76        );
77    }
78
79    //  Rotation can only be 0-3. 1 and 3 are 90 and 270 degrees respectively (anti-clockwise)
80    //  If we have 90 or 270 rotation, flip width and height
81    if rotation == 1 || rotation == 3 {
82        core::mem::swap(&mut max_width, &mut max_height);
83    }
84
85    Ok(ImageSize {
86        width: max_width,
87        height: max_height,
88    })
89}
90
91pub fn matches<R: BufRead + Seek>(header: &[u8], reader: &mut R) -> Option<Compression> {
92    if header.len() < 12 || &header[4..8] != b"ftyp" {
93        return None;
94    }
95
96    let brand: [u8; 4] = header[8..12].try_into().unwrap();
97
98    if let Some(compression) = inner_matches(&brand) {
99        // case 1: { heic, ... }
100        return Some(compression);
101    }
102
103    // REFS: https://github.com/nokiatech/heif/blob/be43efdf273ae9cf90e552b99f16ac43983f3d19/srcs/reader/heifreaderimpl.cpp#L738
104    let brands = [b"mif1", b"msf1", b"mif2", b"miaf"];
105
106    if brands.contains(&&brand) {
107        let mut buf = [0; 12];
108
109        if reader.read_exact(&mut buf).is_err() {
110            return Some(Compression::Unknown);
111        }
112
113        let brand2: [u8; 4] = buf[4..8].try_into().unwrap();
114
115        if let Some(compression) = inner_matches(&brand2) {
116            // case 2: { msf1, version, heic,  msf1, ... }
117            //           brand          brand2 brand3
118            return Some(compression);
119        }
120
121        if brands.contains(&&brand2) {
122            // case 3: { msf1, version, msf1,  heic, ... }
123            //           brand          brand2 brand3
124            let brand3: [u8; 4] = buf[8..12].try_into().unwrap();
125
126            if let Some(compression) = inner_matches(&brand3) {
127                return Some(compression);
128            }
129        }
130    }
131
132    Some(Compression::Unknown)
133}
134
135fn inner_matches(brand: &[u8; 4]) -> Option<Compression> {
136    // Since other non-heif files may contain ftype in the header
137    // we try to use brands to distinguish image files specifically.
138    // List of brands from here: https://mp4ra.org/#/brands
139    let hevc_brands = [
140        b"heic", b"heix", b"heis", b"hevs", b"heim", b"hevm", b"hevc", b"hevx",
141    ];
142    let av1_brands = [
143        b"avif", b"avio", b"avis",
144        // AVIF only
145        // REFS: https://rawcdn.githack.com/AOMediaCodec/av1-avif/67a92add6cd642a8863e386fa4db87954a6735d1/index.html#advanced-profile
146        b"MA1A", b"MA1B",
147    ];
148    let jpeg_brands = [b"jpeg", b"jpgs"];
149
150    // unused
151    // REFS: https://github.com/MPEGGroup/FileFormatConformance/blob/6eef4e4c8bc70e2af9aeb1d62e764a6235f9d6a6/data/standard_features/23008-12/brands.json
152    // let avc_brands = [b"avci", b"avcs"];
153    // let vvc_brands = [b"vvic", b"vvis"];
154    // let evc_brands = [b"evbi", b"evbs", b"evmi", b"evms"];
155
156    // Maybe unnecessary
157    // REFS: https://github.com/nokiatech/heif/blob/be43efdf273ae9cf90e552b99f16ac43983f3d19/srcs/reader/heifreaderimpl.cpp#L1415
158    // REFS: https://github.com/nokiatech/heif/blob/be43efdf273ae9cf90e552b99f16ac43983f3d19/srcs/api-cpp/ImageItem.h#L37
159    // let feature_brands = [b"pred", b"auxl", b"thmb", b"base", b"dimg"];
160    if hevc_brands.contains(&brand) {
161        return Some(Compression::Hevc);
162    }
163
164    if av1_brands.contains(&brand) {
165        return Some(Compression::Av1);
166    }
167
168    if jpeg_brands.contains(&brand) {
169        return Some(Compression::Jpeg);
170    }
171
172    None
173}
174
175fn skip_to_tag<R: BufRead + Seek>(reader: &mut R, tag: &[u8]) -> ImageResult<u32> {
176    let mut tag_buf = [0; 4];
177
178    loop {
179        let size = read_u32(reader, &Endian::Big)?;
180        reader.read_exact(&mut tag_buf)?;
181
182        if tag_buf == tag {
183            return Ok(size);
184        }
185
186        if size >= 8 {
187            reader.seek(SeekFrom::Current(size as i64 - 8))?;
188        } else {
189            return Err(no_std_io::io::Error::new(
190                no_std_io::io::ErrorKind::InvalidData,
191                "Invalid heif box size",
192            )
193            .into());
194        }
195    }
196}