Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Image
- **jpg** - ``image/jpeg``
- **jpx** - ``image/jpx``
- **png** - ``image/png``
- **apng** - ``image/apng``
- **gif** - ``image/gif``
- **webp** - ``image/webp``
- **cr2** - ``image/x-canon-cr2``
Expand Down
1 change: 1 addition & 0 deletions filetype/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
image.Xcf(),
image.Jpeg(),
image.Jpx(),
image.Apng(),
image.Png(),
image.Gif(),
image.Webp(),
Expand Down
40 changes: 40 additions & 0 deletions filetype/types/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,46 @@ def match(self, buf):
)


class Apng(Type):
"""
Implements the APNG image type matcher.
"""
MIME = 'image/apng'
EXTENSION = 'apng'

def __init__(self):
super(Apng, self).__init__(
mime=Apng.MIME,
extension=Apng.EXTENSION
)

def match(self, buf):
if(len(buf) > 8 and
buf[:8] == bytearray([0x89, 0x50, 0x4e, 0x47,
0x0d, 0x0a, 0x1a, 0x0a])):
#cursor in buf, skip already readed 8 bytes
i = 8
while len(buf) > i:
data_length = int.from_bytes(buf[i:i+4], byteorder="big")
i += 4

chunk_type = buf[i:i+4].decode("ascii")
i += 4

#acTL chunk in APNG should appears first than IDAT
#IEND is end of PNG
if (chunk_type == "IDAT" or chunk_type == "IEND"):
return 0
elif (chunk_type == "acTL"):
return 1

#move to the next chunk by skipping data and crc (4 bytes)
i += data_length + 4
return 0
else:
return 0


class Png(Type):
"""
Implements the PNG image type matcher.
Expand Down