Fixed default numresolution for small JPEG2000 images

This commit is contained in:
Andrew Murray 2021-06-14 11:23:56 +10:00
parent 85d009c697
commit bd298b128c
3 changed files with 17 additions and 1 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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);
}