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 os
import re import re
import struct
from io import BytesIO from io import BytesIO
import pytest import pytest
from PIL import Image, ImageFile, Jpeg2KImagePlugin, UnidentifiedImageError, features from PIL import (
Image,
ImageFile,
Jpeg2KImagePlugin,
UnidentifiedImageError,
_binary,
features,
)
from .helper import ( from .helper import (
assert_image_equal, assert_image_equal,
assert_image_similar, assert_image_similar,
assert_image_similar_tofile, assert_image_similar_tofile,
skip_unless_feature, skip_unless_feature,
skip_unless_feature_version,
) )
EXTRA_DIR = "Tests/images/jpeg2000" 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 # Lazy method to determine if the comment is in the image generated
assert bytes(unique_comment, "utf-8") in data 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(): def test_plt_marker():
# Search the start of the codesteam for the PLT box (id 0xFF58) # 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")) out = BytesIO()
assert opj_version is not None 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: jp2_boxid = _binary.i16be(box_bytes)
out = BytesIO() if jp2_boxid == 0xFF4F:
test_card.save(out, "JPEG2000", no_jp2=True, add_plt=True) # No length specifier for main header
out.seek(0) continue
while True: elif jp2_boxid == 0xFF58:
box_bytes = out.read(2) # This is the PLT box we're looking for
if len(box_bytes) == 0: return
# End of steam encountered and no PLT or SOD elif jp2_boxid == 0xFF93:
break # SOD box encountered and no PLT, so it wasn't found
jp2_boxid = struct.unpack(">H", box_bytes)[0] break
if jp2_boxid == 0xFF4F: jp2_boxlength = _binary.i16be(out.read(2))
# No length specifier for main header out.seek(jp2_boxlength - 2, os.SEEK_CUR)
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 = struct.unpack(">H", out.read(2))[0] # The PLT box wasn't found
out.seek(jp2_boxlength - 2, os.SEEK_CUR) 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; char mct = 0;
int sgnd = 0; int sgnd = 0;
Py_ssize_t fd = -1; Py_ssize_t fd = -1;
char * comment = NULL; char *comment = NULL;
int add_plt = 0; int add_plt = 0;
if (!PyArg_ParseTuple( if (!PyArg_ParseTuple(
@ -1338,8 +1338,6 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) {
Py_DECREF(encoder); Py_DECREF(encoder);
return NULL; return NULL;
} }
} else {
context->comment = NULL;
} }
if (quality_layers && PySequence_Check(quality_layers)) { if (quality_layers && PySequence_Check(quality_layers)) {

View File

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