From 9a1b6966b51dc5b2e07d95faa27c0c433b66a088 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Fri, 14 Mar 2014 15:40:30 +0000 Subject: [PATCH] Added precinct size option. Also added the jp2klib_version symbol on the _imaging module. --- PIL/Jpeg2KImagePlugin.py | 2 ++ _imaging.c | 7 +++++++ encode.c | 10 +++++++--- libImaging/Jpeg2K.h | 3 +++ libImaging/Jpeg2KEncode.c | 9 +++++++++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/PIL/Jpeg2KImagePlugin.py b/PIL/Jpeg2KImagePlugin.py index 6b1a36f3f..26eec237d 100644 --- a/PIL/Jpeg2KImagePlugin.py +++ b/PIL/Jpeg2KImagePlugin.py @@ -196,6 +196,7 @@ def _save(im, fp, filename): quality_layers = info.get('quality_layers', None) num_resolutions = info.get('num_resolutions', 0) cblk_size = info.get('codeblock_size', None) + precinct_size = info.get('precinct_size', None) irreversible = info.get('irreversible', False) progression = info.get('progression', 'LRCP') cinema_mode = info.get('cinema_mode', 'no') @@ -212,6 +213,7 @@ def _save(im, fp, filename): quality_layers, num_resolutions, cblk_size, + precinct_size, irreversible, progression, cinema_mode, diff --git a/_imaging.c b/_imaging.c index fe623e780..987bd0383 100644 --- a/_imaging.c +++ b/_imaging.c @@ -3461,6 +3461,13 @@ setup_module(PyObject* m) { } #endif +#ifdef HAVE_OPENJPEG + { + extern const char *ImagingJpeg2KVersion(void); + PyDict_SetItemString(d, "jp2klib_version", PyUnicode_FromString(ImagingJpeg2KVersion())); + } +#endif + #ifdef HAVE_LIBZ /* zip encoding strategies */ PyModule_AddIntConstant(m, "DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY); diff --git a/encode.c b/encode.c index 52777cc0c..0c444bfc3 100644 --- a/encode.c +++ b/encode.c @@ -836,7 +836,7 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) char *quality_mode = "rates"; PyObject *quality_layers = NULL; int num_resolutions = 0; - PyObject *cblk_size = NULL; + PyObject *cblk_size = NULL, *precinct_size = NULL; PyObject *irreversible = NULL; char *progression = "LRCP"; OPJ_PROG_ORDER prog_order; @@ -844,10 +844,11 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) OPJ_CINEMA_MODE cine_mode; int fd = -1; - if (!PyArg_ParseTuple(args, "ss|OOOsOIOOssi", &mode, &format, + if (!PyArg_ParseTuple(args, "ss|OOOsOIOOOssi", &mode, &format, &offset, &tile_offset, &tile_size, &quality_mode, &quality_layers, &num_resolutions, - &cblk_size, &irreversible, &progression, &cinema_mode, + &cblk_size, &precinct_size, + &irreversible, &progression, &cinema_mode, &fd)) return NULL; @@ -916,6 +917,9 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) j2k_decode_coord_tuple(cblk_size, &context->cblk_width, &context->cblk_height); + j2k_decode_coord_tuple(precinct_size, + &context->precinct_width, + &context->precinct_height); context->irreversible = PyObject_IsTrue(irreversible); context->progression = prog_order; diff --git a/libImaging/Jpeg2K.h b/libImaging/Jpeg2K.h index ba4b53a7f..be6e0770b 100644 --- a/libImaging/Jpeg2K.h +++ b/libImaging/Jpeg2K.h @@ -65,6 +65,9 @@ typedef struct { /* Code block size */ int cblk_width, cblk_height; + /* Precinct size */ + int precinct_width, precinct_height; + /* Compression style */ int irreversible; diff --git a/libImaging/Jpeg2KEncode.c b/libImaging/Jpeg2KEncode.c index f5feaf72e..6bf25def8 100644 --- a/libImaging/Jpeg2KEncode.c +++ b/libImaging/Jpeg2KEncode.c @@ -364,6 +364,15 @@ j2k_encode_entry(Imaging im, ImagingCodecState state, params.cblockh_init = context->cblk_height; } + if (context->precinct_width >= 4 && context->precinct_height >= 4 + && context->precinct_width >= context->cblk_width + && context->precinct_height > context->cblk_height) { + params.prcw_init[0] = context->precinct_width; + params.prch_init[0] = context->precinct_height; + params.res_spec = 1; + params.csty |= 0x01; + } + params.irreversible = context->irreversible; params.prog_order = context->progression;