Skip to content

Commit

Permalink
Merge pull request #180 from scientificworld/master
Browse files Browse the repository at this point in the history
feat: support JPEG XL
  • Loading branch information
h2non authored Aug 28, 2024
2 parents 5ddaef8 + 10cd913 commit b846e7e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Image
- **xcf** - ``image/x-xcf``
- **jpg** - ``image/jpeg``
- **jpx** - ``image/jpx``
- **jxl** - ``image/jxl``
- **png** - ``image/png``
- **apng** - ``image/apng``
- **gif** - ``image/gif``
Expand Down Expand Up @@ -161,7 +162,7 @@ Font
- **otf** - ``application/font-sfnt``

Application
^^^^^^^^^^^
^^^^^^^^^^^

- **wasm** - ``application/wasm``

Expand Down
1 change: 1 addition & 0 deletions filetype/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
image.Xcf(),
image.Jpeg(),
image.Jpx(),
image.Jxl(),
image.Apng(),
image.Png(),
image.Gif(),
Expand Down
32 changes: 32 additions & 0 deletions filetype/types/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@ def match(self, buf):
)


class Jxl(Type):
"""
Implements the JPEG XL image type matcher.
"""

MIME = "image/jxl"
EXTENSION = "jxl"

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

def match(self, buf):
return (
(len(buf) > 1 and
buf[0] == 0xFF and
buf[1] == 0x0A) or
(len(buf) > 11 and
buf[0] == 0x00 and
buf[1] == 0x00 and
buf[2] == 0x00 and
buf[3] == 0x00 and
buf[4] == 0x0C and
buf[5] == 0x4A and
buf[6] == 0x58 and
buf[7] == 0x4C and
buf[8] == 0x20 and
buf[9] == 0x0D and
buf[10] == 0x87 and
buf[11] == 0x0A)
)


class Apng(Type):
"""
Implements the APNG image type matcher.
Expand Down
Binary file added tests/fixtures/sample.jxl
Binary file not shown.
7 changes: 6 additions & 1 deletion tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def test_guess_jpx(self):
self.assertEqual(kind.mime, 'image/jpx')
self.assertEqual(kind.extension, 'jpx')

def test_guess_jxl(self):
kind = filetype.guess(FIXTURES + '/sample.jxl')
self.assertTrue(kind is not None)
self.assertEqual(kind.mime, 'image/jxl')
self.assertEqual(kind.extension, 'jxl')

def test_guess_gif(self):
kind = filetype.guess(FIXTURES + '/sample.gif')
self.assertTrue(kind is not None)
Expand All @@ -56,7 +62,6 @@ def test_guess_m4a(self):
self.assertEqual(kind.mime, 'audio/mp4')
self.assertEqual(kind.extension, 'm4a')


def test_guess_mp4(self):
kind = filetype.guess(FIXTURES + '/sample.mp4')
self.assertTrue(kind is not None)
Expand Down

0 comments on commit b846e7e

Please sign in to comment.