diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index 2173d245f..20280a579 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -127,6 +127,16 @@ def test_prog_res_rt(): assert_image_equal(im, test_card) +def test_default_num_resolutions(): + for num_resolutions in range(2, 6): + d = 1 << (num_resolutions - 1) + im = test_card.resize((d - 1, d - 1)) + with pytest.raises(OSError): + roundtrip(im, num_resolutions=num_resolutions) + reloaded = roundtrip(im) + assert_image_equal(im, reloaded) + + def test_reduce(): with Image.open("Tests/images/test-card-lossless.jp2") as im: assert callable(im.reduce) diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index b4107e8e3..892087916 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -248,4 +248,4 @@ class TestPyDecoder: def test_oserror(self): im = Image.new("RGB", (1, 1)) with pytest.raises(OSError): - im.save(BytesIO(), "JPEG2000") + im.save(BytesIO(), "JPEG2000", num_resolutions=2) diff --git a/src/libImaging/Jpeg2KEncode.c b/src/libImaging/Jpeg2KEncode.c index 2e6b5daf0..701853159 100644 --- a/src/libImaging/Jpeg2KEncode.c +++ b/src/libImaging/Jpeg2KEncode.c @@ -458,6 +458,12 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) { break; } + if (!context->num_resolutions) { + while (tile_width < (1 << (params.numresolution - 1U)) || tile_height < (1 << (params.numresolution - 1U))) { + params.numresolution -= 1; + } + } + if (context->cinema_mode != OPJ_OFF) { j2k_set_cinema_params(im, components, ¶ms); }