Add 'mct' and 'use_jp2' options for J2K saving

This commit is contained in:
scaramallion 2021-05-11 20:01:35 +10:00 committed by Hugo van Kemenade
parent 785faa8747
commit de26f78eb1
6 changed files with 58 additions and 4 deletions

View File

@ -209,6 +209,37 @@ def test_layers():
assert_image_similar(im, test_card, 0.4)
def test_use_jp2():
out = BytesIO()
test_card.save(out, "JPEG2000", use_jp2=False)
out.seek(0)
assert out.read(2) == b"\xff\x4f"
def test_mct():
# Three component
for val in (0, 1):
out = BytesIO()
test_card.save(out, "JPEG2000", mct=val, use_jp2=False)
out.seek(0)
with Image.open(out) as im:
im.load()
assert_image_similar(im, test_card, 1.0e-3)
assert out.getvalue()[59] == val
# Single component
for val in (0, 1):
out = BytesIO()
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
jp2.save(out, "JPEG2000", mct=val, use_jp2=False)
out.seek(0)
with Image.open(out) as im:
im.load()
assert_image_similar(im, jp2, 1.0e-3)
assert out.getvalue()[53] == 0
def test_rgba():
# Arrange
with Image.open("Tests/images/rgb_trns_ycbc.j2k") as j2k:

View File

@ -502,9 +502,14 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options:
and must be greater than the code-block size.
**irreversible**
If ``True``, use the lossy Irreversible Color Transformation
followed by DWT 9-7. Defaults to ``False``, which means to use the
Reversible Color Transformation with DWT 5-3.
If ``True``, use the lossy discrete waveform transformation DWT 9-7.
Defaults to ``False``, which uses the lossless DWT 5-3.
**mct**
If ``0`` then don't use multiple component transformation when encoding,
otherwise use ``1`` to apply to components 0, 1 and 2. MCT works best
with a ``mode`` of ``RGB`` and is only available for 3 component image
data.
**progression**
Controls the progression order; must be one of ``"LRCP"``, ``"RLCP"``,
@ -524,6 +529,10 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options:
for compliant 4K files, *at least one* of the dimensions must match
4096 x 2160.
**use_jp2**
If ``False`` then don't wrap the codestream in the JP2 file format when
saving. Defaults to ``True``.
.. note::
To enable JPEG 2000 support, you need to build and install the OpenJPEG

View File

@ -320,6 +320,10 @@ def _save(im, fp, filename):
irreversible = info.get("irreversible", False)
progression = info.get("progression", "LRCP")
cinema_mode = info.get("cinema_mode", "no")
mct = info.get("mct", 0)
kind = "jp2" if info.get("use_jp2", True) else "j2k"
fd = -1
if hasattr(fp, "fileno"):
@ -340,6 +344,7 @@ def _save(im, fp, filename):
irreversible,
progression,
cinema_mode,
mct,
fd,
)

View File

@ -1187,11 +1187,12 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) {
OPJ_PROG_ORDER prog_order;
char *cinema_mode = "no";
OPJ_CINEMA_MODE cine_mode;
char *mct = 0;
Py_ssize_t fd = -1;
if (!PyArg_ParseTuple(
args,
"ss|OOOsOnOOOssn",
"ss|OOOsOnOOOssbn",
&mode,
&format,
&offset,
@ -1205,6 +1206,7 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) {
&irreversible,
&progression,
&cinema_mode,
&mct,
&fd)) {
return NULL;
}
@ -1302,6 +1304,7 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) {
context->irreversible = PyObject_IsTrue(irreversible);
context->progression = prog_order;
context->cinema_mode = cine_mode;
context->mct = mct;
return (PyObject *)encoder;
}

View File

@ -82,6 +82,9 @@ typedef struct {
/* Compression style */
int irreversible;
/* Set multiple component transformation */
char mct;
/* Progression order (LRCP/RLCP/RPCL/PCRL/CPRL) */
OPJ_PROG_ORDER progression;

View File

@ -435,6 +435,9 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) {
}
params.irreversible = context->irreversible;
if (components == 3) {
params.tcp_mct = context->mct;
}
params.prog_order = context->progression;