Merge remote-tracking branch 'radarhere/jp2k_options' into jp2k_options

This commit is contained in:
Andrew Murray 2023-02-08 17:46:50 +11:00
commit fd6874a385
3 changed files with 39 additions and 33 deletions

View File

@ -1,17 +1,24 @@
import os
import re
import struct
from io import BytesIO
import pytest
from PIL import Image, ImageFile, Jpeg2KImagePlugin, UnidentifiedImageError, features
from PIL import (
Image,
ImageFile,
Jpeg2KImagePlugin,
UnidentifiedImageError,
_binary,
features,
)
from .helper import (
assert_image_equal,
assert_image_similar,
assert_image_similar_tofile,
skip_unless_feature,
skip_unless_feature_version,
)
EXTRA_DIR = "Tests/images/jpeg2000"
@ -383,35 +390,36 @@ def test_custom_comment():
# Lazy method to determine if the comment is in the image generated
assert bytes(unique_comment, "utf-8") in data
too_long_comment = " " * 65532
with pytest.raises(ValueError):
test_card.save(output_stream, "JPEG2000", comment=too_long_comment)
@skip_unless_feature_version("jpg_2000", "2.4.0")
def test_plt_marker():
# Search the start of the codesteam for the PLT box (id 0xFF58)
opj_version = re.search(r"(\d+\.\d+)\.\d+$", features.version_codec("jpg_2000"))
assert opj_version is not None
out = BytesIO()
test_card.save(out, "JPEG2000", no_jp2=True, add_plt=True)
out.seek(0)
while True:
box_bytes = out.read(2)
if not box_bytes:
# End of steam encountered and no PLT or SOD
break
if float(opj_version[1]) >= 2.4:
out = BytesIO()
test_card.save(out, "JPEG2000", no_jp2=True, add_plt=True)
out.seek(0)
while True:
box_bytes = out.read(2)
if len(box_bytes) == 0:
# End of steam encountered and no PLT or SOD
break
jp2_boxid = struct.unpack(">H", box_bytes)[0]
jp2_boxid = _binary.i16be(box_bytes)
if jp2_boxid == 0xFF4F:
# No length specifier for main header
continue
elif jp2_boxid == 0xFF58:
# This is the PLT box we're looking for
return
elif jp2_boxid == 0xFF93:
# SOD box encountered and no PLT, so it wasn't found
break
if jp2_boxid == 0xFF4F:
# No length specifier for main header
continue
elif jp2_boxid == 0xFF58:
# This is the PLT box we're looking for
return
elif jp2_boxid == 0xFF93:
break
# SOD box encountered and no PLT, so it wasn't found
jp2_boxlength = _binary.i16be(out.read(2))
out.seek(jp2_boxlength - 2, os.SEEK_CUR)
jp2_boxlength = struct.unpack(">H", out.read(2))[0]
out.seek(jp2_boxlength - 2, os.SEEK_CUR)
# The PLT box wasn't found
raise ValueError
# The PLT box wasn't found
raise ValueError

View File

@ -1214,7 +1214,7 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) {
char mct = 0;
int sgnd = 0;
Py_ssize_t fd = -1;
char * comment = NULL;
char *comment = NULL;
int add_plt = 0;
if (!PyArg_ParseTuple(
@ -1326,7 +1326,7 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) {
PyExc_ValueError,
"JPEG 2000 comment is too long");
Py_DECREF(encoder);
return NULL;
return NULL;
}
context->comment = strdup(comment);
@ -1338,8 +1338,6 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) {
Py_DECREF(encoder);
return NULL;
}
} else {
context->comment = NULL;
}
if (quality_layers && PySequence_Check(quality_layers)) {

View File

@ -98,7 +98,7 @@ typedef struct {
const char *error_msg;
/* Custom comment */
char * comment;
char *comment;
/* Include PLT marker segment */
int add_plt;