Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,12 @@ def build_extensions(self) -> None:

if feature.want("zlib"):
_dbg("Looking for zlib")
if _find_include_file(self, "zlib.h"):
if _find_include_file(self, "zlib-ng.h"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces an automagic dependency on zlib-ng when both zlib-ng and zlib are installed. As such, Gentoo (as well as anyone who doesn't build in an entirely isolated environment) would end up having to patch this out in order to use plain zlib. I'm not opposed to having zlib-ng as an option, but then it should be an option, one that can be explicitly disabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for bringing this up. it is true. it is "priority for zlib-ng".

I'm happy to add an explicit "switch" between the two.

I'll add that to the todo list if the general strategy is agreeable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still working on the general strategy being agreeable. That is usually determined by interest and the number of folks looking for a fix for this issue. At present, we depend on zlib and zlib-ng satisfies that dependency via compat mode. How many folks are looking for co-existence ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s a good question.

I’m. Happy to let this PR sit

if _find_library_file(self, "z-ng"):
feature.set("zlib", "z-ng")
elif sys.platform == "win32" and _find_library_file(self, "zlib-ng"):
feature.set("zlib", "zlib-ng")
elif _find_include_file(self, "zlib.h"):
if _find_library_file(self, "z"):
feature.set("zlib", "z")
elif sys.platform == "win32" and _find_library_file(self, "zlib"):
Expand Down Expand Up @@ -923,9 +928,11 @@ def build_extensions(self) -> None:
defs.append(("HAVE_OPENJPEG", None))
if sys.platform == "win32" and not PLATFORM_MINGW:
defs.append(("OPJ_STATIC", None))
if feature.get("zlib"):
libs.append(feature.get("zlib"))
if zlib := feature.get("zlib"):
libs.append(zlib)
defs.append(("HAVE_LIBZ", None))
if zlib in ["z-ng", "zlib-ng"]:
defs.append(("HAVE_ZLIBNG", None))
if feature.get("imagequant"):
libs.append(feature.get("imagequant"))
defs.append(("HAVE_LIBIMAGEQUANT", None))
Expand Down
6 changes: 4 additions & 2 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@
#endif
#endif

#ifdef HAVE_LIBZ
#include "zlib.h"
#ifdef HAVE_ZLIBNG
#include <zlib-ng.h>
#else
#include <zlib.h>
#endif

#ifdef HAVE_LIBTIFF
Expand Down
10 changes: 9 additions & 1 deletion src/libImaging/ZipCodecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
* Copyright (c) Fredrik Lundh 1996.
*/

#include "zlib.h"
#ifdef HAVE_ZLIBNG
#include <zlib-ng.h>
#else
#include <zlib.h>
#endif

/* modes */
#define ZIP_PNG 0 /* continuous, filtered image data */
Expand Down Expand Up @@ -35,7 +39,11 @@ typedef struct {

/* PRIVATE CONTEXT (set by decoder/encoder) */

#ifdef HAVE_ZLIBNG
zng_stream z_stream; /* (de)compression stream */
#else
z_stream z_stream; /* (de)compression stream */
#endif

UINT8 *previous; /* previous line (allocated) */

Expand Down
24 changes: 24 additions & 0 deletions src/libImaging/ZipDecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt
context->z_stream.zfree = (free_func)NULL;
context->z_stream.opaque = (voidpf)NULL;

#ifdef HAVE_ZLIBNG
err = zng_inflateInit(&context->z_stream);
#else
err = inflateInit(&context->z_stream);
#endif
if (err < 0) {
state->errcode = IMAGING_CODEC_CONFIG;
free(context->previous);
Expand Down Expand Up @@ -112,7 +116,11 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt
context->z_stream.next_out = state->buffer + context->last_output;
context->z_stream.avail_out = row_len + context->prefix - context->last_output;

#ifdef HAVE_ZLIBNG
err = zng_inflate(&context->z_stream, Z_NO_FLUSH);
#else
err = inflate(&context->z_stream, Z_NO_FLUSH);
#endif

if (err < 0) {
/* Something went wrong inside the compression library */
Expand All @@ -125,7 +133,11 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt
}
free(context->previous);
context->previous = NULL;
#ifdef HAVE_ZLIBNG
zng_inflateEnd(&context->z_stream);
#else
inflateEnd(&context->z_stream);
#endif
return -1;
}

Expand Down Expand Up @@ -196,7 +208,11 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt
state->errcode = IMAGING_CODEC_UNKNOWN;
free(context->previous);
context->previous = NULL;
#ifdef HAVE_ZLIBNG
zng_inflateEnd(&context->z_stream);
#else
inflateEnd(&context->z_stream);
#endif
return -1;
}
break;
Expand Down Expand Up @@ -270,7 +286,11 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt

free(context->previous);
context->previous = NULL;
#ifdef HAVE_ZLIBNG
zng_inflateEnd(&context->z_stream);
#else
inflateEnd(&context->z_stream);
#endif
return -1; /* end of file (errcode=0) */
}

Expand All @@ -292,7 +312,11 @@ ImagingZipDecodeCleanup(ImagingCodecState state) {

/* Clean up */
if (context->previous) {
#ifdef HAVE_ZLIBNG
zng_inflateEnd(&context->z_stream);
#else
inflateEnd(&context->z_stream);
#endif
free(context->previous);
context->previous = NULL;
}
Expand Down
51 changes: 51 additions & 0 deletions src/libImaging/ZipEncode.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
compress_type = context->compress_type;
}

#ifdef HAVE_ZLIBNG
err = zng_deflateInit2(
&context->z_stream,
/* compression level */
compress_level,
/* compression method */
Z_DEFLATED,
/* compression memory resources */
15,
9,
/* compression strategy (image data are filtered)*/
compress_type
);
#else
err = deflateInit2(
&context->z_stream,
/* compression level */
Expand All @@ -100,17 +114,26 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
/* compression strategy (image data are filtered)*/
compress_type
);
#endif
if (err < 0) {
state->errcode = IMAGING_CODEC_CONFIG;
return -1;
}

if (context->dictionary && context->dictionary_size > 0) {
#ifdef HAVE_ZLIBNG
err = zng_deflateSetDictionary(
&context->z_stream,
(unsigned char *)context->dictionary,
context->dictionary_size
);
#else
err = deflateSetDictionary(
&context->z_stream,
(unsigned char *)context->dictionary,
context->dictionary_size
);
#endif
if (err < 0) {
state->errcode = IMAGING_CODEC_CONFIG;
return -1;
Expand All @@ -126,7 +149,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
context->z_stream.avail_out = bytes;
if (context->z_stream.next_in && context->z_stream.avail_in > 0) {
/* We have some data from previous round, deflate it first */
#ifdef HAVE_ZLIBNG
err = zng_deflate(&context->z_stream, Z_NO_FLUSH);
#else
err = deflate(&context->z_stream, Z_NO_FLUSH);
#endif

if (err < 0) {
/* Something went wrong inside the compression library */
Expand All @@ -142,7 +169,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
free(context->up);
free(context->prior);
free(context->previous);
#ifdef HAVE_ZLIBNG
zng_deflateEnd(&context->z_stream);
#else
deflateEnd(&context->z_stream);
#endif
return -1;
}
}
Expand Down Expand Up @@ -279,7 +310,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
context->z_stream.next_in = context->output;
context->z_stream.avail_in = state->bytes + 1;

#ifdef HAVE_ZLIBNG
err = zng_deflate(&context->z_stream, Z_NO_FLUSH);
#else
err = deflate(&context->z_stream, Z_NO_FLUSH);
#endif

if (err < 0) {
/* Something went wrong inside the compression library */
Expand All @@ -295,7 +330,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
free(context->up);
free(context->prior);
free(context->previous);
#ifdef HAVE_ZLIBNG
zng_deflateEnd(&context->z_stream);
#else
deflateEnd(&context->z_stream);
#endif
ImagingSectionLeave(&cookie);
return -1;
}
Expand All @@ -315,7 +354,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
/* End of image data; flush compressor buffers */

while (context->z_stream.avail_out > 0) {
#ifdef HAVE_ZLIBNG
err = zng_deflate(&context->z_stream, Z_FINISH);
#else
err = deflate(&context->z_stream, Z_FINISH);
#endif

if (err == Z_STREAM_END) {
free(context->paeth);
Expand All @@ -324,7 +367,11 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
free(context->prior);
free(context->previous);

#ifdef HAVE_ZLIBNG
zng_deflateEnd(&context->z_stream);
#else
deflateEnd(&context->z_stream);
#endif

state->errcode = IMAGING_CODEC_END;

Expand Down Expand Up @@ -364,7 +411,11 @@ ImagingZipEncodeCleanup(ImagingCodecState state) {

const char *
ImagingZipVersion(void) {
#ifdef HAVE_ZLIBNG
return zlibng_version();
#else
return zlibVersion();
#endif
}

#endif
Loading