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 JPEG XL Open/Read support via libjxl #7848

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8e0c5db
add tests
olokelo Mar 1, 2024
a57ebea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 2, 2024
23fb57d
minor fixes, linting corrections
olokelo Mar 2, 2024
2eb5987
Added type hints
radarhere Mar 6, 2024
37b58f3
Removed feature
radarhere Mar 6, 2024
eeaecb4
Merge pull request #1 from radarhere/jxl-support2
olokelo Mar 11, 2024
24b63ad
fix goto labels for clang
olokelo Mar 11, 2024
0b50410
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 11, 2024
f403672
Merge branch 'main' into jxl-support2
hugovk Mar 19, 2024
f6086d4
modify leak test
olokelo Mar 19, 2024
5320450
fix _jxl_decoder_count_frames
olokelo Mar 19, 2024
1b049ab
minor plugin code tweaks
olokelo Mar 19, 2024
6048520
add type hints
olokelo Mar 19, 2024
8fa280f
rename jxl -> jpegxl
olokelo Mar 19, 2024
58c37bf
add test case for seeking to the same frame
olokelo Mar 19, 2024
48bbc2e
flip cases in metadata test
olokelo Mar 19, 2024
443a352
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 19, 2024
62c58c2
fix some type hinting mistakes
olokelo Mar 19, 2024
8cab1c1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 19, 2024
e5003ff
change Optional to python 3.10+ syntax
olokelo Mar 19, 2024
fa5bfac
add more metadata test cases
olokelo Mar 20, 2024
0b71605
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 20, 2024
1f00fb8
add 16-bits grayscale support for jpeg xl images
olokelo May 18, 2024
08270a7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added Tests/images/flower.jxl
Binary file not shown.
Binary file added Tests/images/flower2.jxl
Binary file not shown.
Binary file added Tests/images/hopper.jxl
Binary file not shown.
Binary file added Tests/images/hopper_jxl_bits.ppm
Binary file not shown.
Binary file added Tests/images/iss634.jxl
Binary file not shown.
Binary file added Tests/images/jxl/traffic_light.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/jxl/traffic_light.jxl
Binary file not shown.
Binary file added Tests/images/transparent.jxl
Binary file not shown.
72 changes: 72 additions & 0 deletions Tests/test_file_jxl.py
@@ -0,0 +1,72 @@
from __future__ import annotations

import re

import pytest

from PIL import Image, JxlImagePlugin, features

from .helper import (
assert_image_similar_tofile,
skip_unless_feature,
)

try:
from PIL import _jxl

HAVE_JXL = True
except ImportError:
HAVE_JXL = False

# cjxl v0.9.2 41b8cdab
# hopper.jxl: cjxl hopper.png hopper.jxl -q 75 -e 8


class TestUnsupportedJxl:
def test_unsupported(self) -> None:
if HAVE_JXL:
JxlImagePlugin.SUPPORTED = False

file_path = "Tests/images/hopper.jxl"
with pytest.warns(UserWarning):
with pytest.raises(OSError):
with Image.open(file_path):
pass

if HAVE_JXL:
JxlImagePlugin.SUPPORTED = True


@skip_unless_feature("jxl")
class TestFileJxl:
def setup_method(self) -> None:
self.rgb_mode = "RGB"

def test_version(self) -> None:
_jxl.JxlDecoderVersion()
assert re.search(r"\d+\.\d+\.\d+$", features.version_module("jxl"))

def test_read_rgb(self) -> None:
"""
Can we read a RGB mode Jpeg XL file without error?
Does it have the bits we expect?
"""

with Image.open("Tests/images/hopper.jxl") as image:
assert image.mode == self.rgb_mode
assert image.size == (128, 128)
assert image.format == "JPEG XL"
image.load()
image.getdata()

# generated with:
# djxl hopper.jxl hopper_jxl_bits.ppm
assert_image_similar_tofile(image, "Tests/images/hopper_jxl_bits.ppm", 1.0)

def test_JxlDecode_with_invalid_args(self) -> None:
"""
Calling decoder functions with no arguments should result in an error.
"""

with pytest.raises(TypeError):
_jxl.PILJxlDecoder()
29 changes: 29 additions & 0 deletions Tests/test_file_jxl_alpha.py
@@ -0,0 +1,29 @@
from __future__ import annotations

import pytest

from PIL import Image

from .helper import assert_image_similar_tofile

_webp = pytest.importorskip("PIL._jxl", reason="JXL support not installed")


def test_read_rgba() -> None:
"""
Can we read an RGBA mode file without error?
Does it have the bits we expect?
"""

# Generated with `cjxl transparent.png transparent.jxl -q 100 -e 8`
file_path = "Tests/images/transparent.jxl"
with Image.open(file_path) as image:
assert image.mode == "RGBA"
assert image.size == (200, 150)
assert image.format == "JPEG XL"
image.load()
image.getdata()

image.tobytes()

assert_image_similar_tofile(image, "Tests/images/transparent.png", 1.0)
69 changes: 69 additions & 0 deletions Tests/test_file_jxl_animated.py
@@ -0,0 +1,69 @@
from __future__ import annotations

import pytest

from PIL import Image

from .helper import (
assert_image_equal,
skip_unless_feature,
)

pytestmark = [
skip_unless_feature("jxl"),
]


def test_n_frames() -> None:
"""Ensure that jxl format sets n_frames and is_animated attributes correctly."""

with Image.open("Tests/images/hopper.jxl") as im:
assert im.n_frames == 1
assert not im.is_animated

with Image.open("Tests/images/iss634.jxl") as im:
assert im.n_frames == 41
assert im.is_animated


def test_float_duration() -> None:

with Image.open("Tests/images/iss634.jxl") as im:
im.load()
assert im.info["duration"] == 70


def test_seeking() -> None:
"""
Open an animated jxl file, and then try seeking through frames in reverse-order,
verifying the durations are correct.
"""

with Image.open("Tests/images/jxl/traffic_light.jxl") as im1:
with Image.open("Tests/images/jxl/traffic_light.gif") as im2:
assert im1.n_frames == im2.n_frames
assert im1.is_animated

# Traverse frames in reverse, checking timestamps and durations
total_dur = 0
for frame in reversed(range(im1.n_frames)):
im1.seek(frame)
im1.load()
im2.seek(frame)
im2.load()

assert_image_equal(im1.convert("RGB"), im2.convert("RGB"))

total_dur += im1.info["duration"]
assert im1.info["duration"] == im2.info["duration"]
assert im1.info["timestamp"] == im1.info["timestamp"]
assert total_dur == 8000


def test_seek_errors() -> None:
with Image.open("Tests/images/iss634.jxl") as im:
with pytest.raises(EOFError):
im.seek(-1)

with pytest.raises(EOFError):
im.seek(47)
89 changes: 89 additions & 0 deletions Tests/test_file_jxl_metadata.py
@@ -0,0 +1,89 @@
from __future__ import annotations

from types import ModuleType

import pytest

from PIL import Image

from .helper import skip_unless_feature

pytestmark = [
skip_unless_feature("jxl"),
]

ElementTree: ModuleType | None
try:
from defusedxml import ElementTree
except ImportError:
ElementTree = None


# cjxl flower.jpg flower.jxl --lossless_jpeg=0 -q 75 -e 8

# >>> from PIL import Image
# >>> with Image.open('Tests/images/flower2.webp') as im:
# >>> with open('/tmp/xmp.xml', 'wb') as f:
# >>> f.write(im.info['xmp'])
# cjxl flower2.jpg flower2.jxl --lossless_jpeg=0 -q 75 -e 8 -x xmp=/tmp/xmp.xml


def test_read_exif_metadata() -> None:
file_path = "Tests/images/flower.jxl"
with Image.open(file_path) as image:
assert image.format == "JPEG XL"
exif_data = image.info.get("exif", None)
assert exif_data

exif = image._getexif()

# Camera make
assert exif[271] == "Canon"

with Image.open("Tests/images/flower.jpg") as jpeg_image:
expected_exif = jpeg_image.info["exif"]

# jpeg xl always returns exif without 'Exif\0\0' prefix
assert exif_data == expected_exif[6:]


def test_read_exif_metadata_without_prefix() -> None:
with Image.open("Tests/images/flower2.jxl") as im:
# Assert prefix is not present
assert im.info["exif"][:6] != b"Exif\x00\x00"

exif = im.getexif()
assert exif[305] == "Adobe Photoshop CS6 (Macintosh)"


def test_read_icc_profile() -> None:
file_path = "Tests/images/flower2.jxl"
with Image.open(file_path) as image:
assert image.format == "JPEG XL"
assert image.info.get("icc_profile", None)

icc = image.info["icc_profile"]

with Image.open("Tests/images/flower2.jxl") as jpeg_image:
expected_icc = jpeg_image.info["icc_profile"]

assert icc == expected_icc


def test_getxmp() -> None:
with Image.open("Tests/images/flower.jxl") as im:
assert "xmp" not in im.info
assert im.getxmp() == {}

with Image.open("Tests/images/flower2.jxl") as im:
if ElementTree is None:
hugovk marked this conversation as resolved.
Show resolved Hide resolved
with pytest.warns(

Check warning on line 80 in Tests/test_file_jxl_metadata.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_file_jxl_metadata.py#L80

Added line #L80 was not covered by tests
UserWarning,
match="XMP data cannot be read without defusedxml dependency",
):
assert im.getxmp() == {}

Check warning on line 84 in Tests/test_file_jxl_metadata.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_file_jxl_metadata.py#L84

Added line #L84 was not covered by tests
else:
assert (
im.getxmp()["xmpmeta"]["xmptk"]
== "Adobe XMP Core 5.3-c011 66.145661, 2012/02/06-14:56:27 "
)
27 changes: 27 additions & 0 deletions Tests/test_jxl_leaks.py
@@ -0,0 +1,27 @@
from __future__ import annotations

from io import BytesIO

from PIL import Image

from .helper import PillowLeakTestCase, skip_unless_feature

test_file = "Tests/images/hopper.jxl"
hugovk marked this conversation as resolved.
Show resolved Hide resolved


@skip_unless_feature("jxl")
class TestJxlLeaks(PillowLeakTestCase):
# TODO: lower the limit, I'm not sure what is correct limit
# since I have libjxl debug system-wide
mem_limit = 16 * 1024 # kb
Copy link
Member

Choose a reason for hiding this comment

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

How can we find out the correct limit?

Copy link
Author

Choose a reason for hiding this comment

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

I tested it on release version of libjxl and I've set it to 6 megabytes which seem to work well, that's also similar to memory usage of djxl for decoding hopper.jxl.
I think libjxl memory usage could depend also on number of decoding threads used. By default we use all available cores.

iterations = 100

def test_leak_load(self) -> None:
with open(test_file, "rb") as f:
hugovk marked this conversation as resolved.
Show resolved Hide resolved
im_data = f.read()

def core() -> None:
with Image.open(BytesIO(im_data)) as im:
im.load()

self._test_leak(core)
20 changes: 20 additions & 0 deletions setup.py
Expand Up @@ -286,6 +286,7 @@ class feature:
features = [
"zlib",
"jpeg",
"jxl",
hugovk marked this conversation as resolved.
Show resolved Hide resolved
"tiff",
"freetype",
"raqm",
Expand Down Expand Up @@ -691,6 +692,14 @@ def build_extensions(self):
feature.jpeg2000 = "openjp2"
feature.openjpeg_version = ".".join(str(x) for x in best_version)

if feature.want("jxl"):
_dbg("Looking for jxl")
if _find_include_file(self, "jxl/encode.h") and _find_include_file(
self, "jxl/decode.h"
):
if _find_library_file(self, "jxl"):
feature.jxl = "jxl jxl_threads"

if feature.want("imagequant"):
_dbg("Looking for imagequant")
if _find_include_file(self, "libimagequant.h"):
Expand Down Expand Up @@ -774,6 +783,15 @@ def build_extensions(self):
# alternate Windows name.
feature.lcms = "lcms2_static"

if feature.jxl:
# jxl and jxl_threads are required
libs = feature.jxl.split()
defs = []

self._update_extension("PIL._jxl", libs, defs)
else:
self._remove_extension("PIL._jxl")

if feature.want("webp"):
_dbg("Looking for webp")
if _find_include_file(self, "webp/encode.h") and _find_include_file(
Expand Down Expand Up @@ -934,6 +952,7 @@ def summary_report(self, feature):
(feature.freetype, "FREETYPE2"),
(feature.raqm, "RAQM (Text shaping)", raqm_extra_info),
(feature.lcms, "LITTLECMS2"),
(feature.jxl, "JXL"),
hugovk marked this conversation as resolved.
Show resolved Hide resolved
(feature.webp, "WEBP"),
(feature.webpmux, "WEBPMUX"),
(feature.xcb, "XCB (X protocol)"),
Expand Down Expand Up @@ -978,6 +997,7 @@ def debug_build():
Extension("PIL._imaging", files),
Extension("PIL._imagingft", ["src/_imagingft.c"]),
Extension("PIL._imagingcms", ["src/_imagingcms.c"]),
Extension("PIL._jxl", ["src/_jxl.c"]),
Extension("PIL._webp", ["src/_webp.c"]),
Extension("PIL._imagingtk", ["src/_imagingtk.c", "src/Tk/tkImaging.c"]),
Extension("PIL._imagingmath", ["src/_imagingmath.c"]),
Expand Down