Skip to content

Commit

Permalink
Fixed default numresolution for small JPEG2000 images
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jun 13, 2021
1 parent 85d009c commit d86458d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
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)
roundtrip(im)


def test_reduce():
with Image.open("Tests/images/test-card-lossless.jp2") as im:
assert callable(im.reduce)
Expand Down
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) {
do {
params.numresolution -= 1;
} while (tile_width < (1 << (params.numresolution - 1U)) || tile_height < (1 << (params.numresolution - 1U)));
}

if (context->cinema_mode != OPJ_OFF) {
j2k_set_cinema_params(im, components, &params);
}
Expand Down

0 comments on commit d86458d

Please sign in to comment.