How to extract max width and height metadata from AVIF files in Java?

There is a rapid method to extract image metadata, specifically the maximum width and height, from AVIF files in my Java application. Currently, I analyze the raw header bytes for various image formats to obtain this information. I can identify an AVIF file using the ‘ftypavif’ magic byte sequence at the start of the file, similar to how I locate the SIZ marker (0xFF51) in JPEG2000 files to parse dimensions. However, despite extensive research and reading through AVIF specifications, I haven’t found a way to parse dimensions for AVIF files. Am I mistaken in thinking that dimensions can be parsed without fully decoding the images?

Is there any documentation or library that I’ve overlooked which could aid in retrieving the width and height from an AVIF file? Any guidance or suggestions would be highly valuable.

Hey! Welcome back to the Gumlet community.

Extracting specific metadata like image dimensions directly from AVIF files is a bit more complex than formats like JPEG2000. AVIF is based on the HEIF format, built on the ISOBMFF (ISO Base Media File Format). This means the image data, including dimensions, is stored in ‘boxes’ - a structure used within these file formats.

You will have to parse these boxes to retrieve the width and height from an AVIF file. Specifically, the ‘ispe’ (Image Spatial Extents) box within the ‘meta’ box contains the width and height information. However, unlike JPEG2000, which has a straightforward marker, AVIF requires parsing through a nested structure.

I hope this answer may help you, do let me know if you have more queries on this.

Is there an existing Java library that can handle this parsing, or do I need to implement it from scratch?

There is not a widely-used Java library specifically for AVIF metadata parsing as of now, unlike more common formats like JPEG or PNG. You might need to implement the parsing logic yourself, following the specifications for HEIF/ISOBMFF and AVIF. The key is to iterate through the file’s boxes until you find the ‘ispe’ box within the ‘meta’ box and then extract the width and height from there.

If you are not keen on implementing this from scratch, keep an eye on popular image processing libraries, as they may add support for AVIF metadata in the future due to the format’s growing popularity.