Skip to content

Commit

Permalink
Merge pull request #5540 from radarhere/numresolution
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jun 20, 2021
2 parents 384a4bf + bd298b1 commit 8b0244a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Tests/test_file_jpeg2k.py
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_imagefile.py
Expand Up @@ -248,4 +248,4 @@ def test_no_format(self):
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)
6 changes: 6 additions & 0 deletions src/libImaging/Jpeg2KEncode.c
Expand Up @@ -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, &params);
}
Expand Down

0 comments on commit 8b0244a

Please sign in to comment.