From 7725d281a52feac4d13fdfa749d090750a367b99 Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 11 Aug 2017 04:07:54 +0300 Subject: [PATCH] create separate ImagingJpegUseJCSExtensions function --- decode.c | 25 +------------------------ libImaging/Imaging.h | 1 + libImaging/JpegDecode.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/decode.c b/decode.c index 2ac4bb083..022912ffb 100644 --- a/decode.c +++ b/decode.c @@ -809,17 +809,6 @@ PyImaging_ZipDecoderNew(PyObject* self, PyObject* args) #include "Jpeg.h" -#define STRINGIFY(x) #x -#define TOSTRING(x) STRINGIFY(x) - -// There is no way to compare versions on compile time, -// so we have to do that in runtime. -#ifdef LIBJPEG_TURBO_VERSION -char *libjpeg_turbo_version = TOSTRING(LIBJPEG_TURBO_VERSION); -#else -char *libjpeg_turbo_version = NULL; -#endif - PyObject* PyImaging_JpegDecoderNew(PyObject* self, PyObject* args) { @@ -830,7 +819,6 @@ PyImaging_JpegDecoderNew(PyObject* self, PyObject* args) char* jpegmode; /* what's in the file */ int scale = 1; int draft = 0; - int use_jcs_extensions = 0; if (!PyArg_ParseTuple(args, "ssz|ii", &mode, &rawmode, &jpegmode, &scale, &draft)) @@ -843,23 +831,12 @@ PyImaging_JpegDecoderNew(PyObject* self, PyObject* args) if (decoder == NULL) return NULL; -#ifdef JCS_EXTENSIONS - #if defined(LIBJPEG_TURBO_VERSION_NUMBER) - #if LIBJPEG_TURBO_VERSION_NUMBER >= 1002010 - use_jcs_extensions = 1; - #endif - #else - if (libjpeg_turbo_version) { - use_jcs_extensions = strcmp(libjpeg_turbo_version, "1.2.1") >= 0; - } - #endif // libjpeg-turbo supports different output formats. // We are choosing Pillow's native format (3 color bytes + 1 padding) // to avoid extra conversion in Unpack.c. - if (use_jcs_extensions && strcmp(rawmode, "RGB") == 0) { + if (ImagingJpegUseJCSExtensions() && strcmp(rawmode, "RGB") == 0) { rawmode = "RGBX"; } -#endif if (get_unpacker(decoder, mode, rawmode) < 0) return NULL; diff --git a/libImaging/Imaging.h b/libImaging/Imaging.h index 99fff7f67..308bb35ba 100644 --- a/libImaging/Imaging.h +++ b/libImaging/Imaging.h @@ -406,6 +406,7 @@ extern int ImagingHexDecode(Imaging im, ImagingCodecState state, extern int ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buffer, int bytes); extern int ImagingJpegDecodeCleanup(ImagingCodecState state); +extern int ImagingJpegUseJCSExtensions(void); extern int ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buffer, int bytes); diff --git a/libImaging/JpegDecode.c b/libImaging/JpegDecode.c index 1045642a8..deeeea2a3 100644 --- a/libImaging/JpegDecode.c +++ b/libImaging/JpegDecode.c @@ -38,6 +38,35 @@ #include "Jpeg.h" +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) + +// There is no way to compare versions on compile time, +// so we have to do that in runtime. +#ifdef LIBJPEG_TURBO_VERSION +char *libjpeg_turbo_version = TOSTRING(LIBJPEG_TURBO_VERSION); +#else +char *libjpeg_turbo_version = NULL; +#endif + +int +ImagingJpegUseJCSExtensions() +{ + int use_jcs_extensions = 0; + #ifdef JCS_EXTENSIONS + #if defined(LIBJPEG_TURBO_VERSION_NUMBER) + #if LIBJPEG_TURBO_VERSION_NUMBER >= 1002010 + use_jcs_extensions = 1; + #endif + #else + if (libjpeg_turbo_version) { + use_jcs_extensions = strcmp(libjpeg_turbo_version, "1.2.1") >= 0; + } + #endif + #endif + return use_jcs_extensions; +} + /* -------------------------------------------------------------------- */ /* Suspending input handler */ /* -------------------------------------------------------------------- */