How to get image dimensions from an avif file?

1.5k Views Asked by At

I'm looking for a (fast) way to retrieve image metadata from an avif-file in my java-application. This is currently done for many image formats by analyzing the raw header bytes of a file.

I can recognize a file as avif through the magic byte sequence at the beginning of the file: ftypavif. But I'm not able to find any information how to parse the (max) image width & height of contained images. For JPEG2000 for example I can search for SIZ marker 0xFF51 and parse dimensions afterwards, but even after spending a lot of time reading through specifications of avif, I'm not able to find something similar for it.

Is my approach incorrect and I can't parse the image dimensions without decoding the image(s)? Or is there any documentation (or even usable library) I missed to retrieve width & height from an avif-file?

Any hints appreciated!

2

There are 2 best solutions below

0
Daniel a Simona Novomeská On

Some of the AVIF files have ftypavis (contains animation) and I also saw AVIF with ftypmif1

If you want to parse AVIF alone, you need to understand ISOBMFF format and HEIF container first. There are various properties (like Image spatial Extents (ispe)), then various transformations (crop, rotation, mirror). Images can be stored in a form of image grid...

3
Dimitri Podborski On

Search for 'ispe' box (this is a mandatory box/item), skip 4 bytes after the type (FullBox version and flags) read 4 bytes for image width, read 4 bytes for image height. Profit ;)

enter image description here