Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add partial support for avif #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion get_image_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os
import io
import struct
import time

FILE_UNKNOWN = "Sorry, don't know how to get size for this file."

Expand Down Expand Up @@ -119,7 +120,7 @@ def get_image_metadata_from_bytesio(input, size, file_path=None):
"""
height = -1
width = -1
data = input.read(26)
data = input.read(28)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added 2 bytes because I thought it would be fine to read extra 2 bytes and it would allow an extra opportunity to identify if it's AVIF

msg = " raised while trying to decode as JPEG."

if (size >= 10) and data[:6] in (b'GIF87a', b'GIF89a'):
Expand Down Expand Up @@ -245,6 +246,49 @@ def get_image_metadata_from_bytesio(input, size, file_path=None):
break
except Exception as e:
raise UnknownImageFormat(str(e))
elif (size >= 28) and data[4:8] == b'ftyp' and \
any(bytes(brand) in (b'avif', b'avis') for brand in zip(*((iter(data[8:]),) * 4))):
MSG = 'Failed to parse image as avif. '
HEADER_PATH = ('meta', 'iprp', 'ipco', 'ispe')
MAX_HEAD_SEARCH = 2 << 10 # Don't waste time if missed 'mdat' & it's processing mdat as part of the header
try:
header_size = struct.unpack('>I', data[0:4])[0]
input.seek(header_size, 0)

def _find(what, up_to_bytes):
while input.tell() < up_to_bytes:
block_size_bytes = input.read(4)
# https://docs.python.org/3/library/io.html#io.RawIOBase.read
if block_size_bytes is None:
time.sleep(0.1)
continue
if not block_size_bytes:
raise UnknownImageFormat(MSG + "Reached end of file")

block_size = struct.unpack('>I', block_size_bytes)[0]
block_type = input.read(4).decode('ascii')
if block_type == 'mdat':
raise UnknownImageFormat(MSG + "Reached image data without finding '{}' data".format(HEADER_PATH[-1]))
if block_type == what:
return input.tell() + block_size

input.seek(block_size - 8, 1)

raise UnknownImageFormat(MSG + "Reached end of block but couldn't find '{}'".format(what))

sub_block_end = _find(HEADER_PATH[0], MAX_HEAD_SEARCH)
input.seek(4, 1) # Jumping over meta's version and flags.
for find in HEADER_PATH[1:]:
sub_block_end = _find(find, sub_block_end)

input.seek(4, 1) # Jumping over ispe's version and flags.
width = struct.unpack('>I', input.read(4))[0]
height = struct.unpack('>I', input.read(4))[0]

except struct.error:
raise UnknownImageFormat(MSG + "See cause exception for details")
except UnicodeDecodeError:
raise UnknownImageFormat(MSG + "Header name not ASCII convertible")
elif size >= 2:
# see http://en.wikipedia.org/wiki/ICO_(file_format)
imgtype = 'ICO'
Expand Down