Is std zlib usable for png

I know png images have their zlib compression scheme, which caused me some headaches in the past in C#. because there seems to be a tiny difference in definitions of zlib.
Is the std version (zlib.zig) the one which is usable for compressing png images?

I am unsure, though so long as the DEFLATE algorithm is compatible, it should be trivial to workaround. If memory serves me correct, Zlib is just a header, DEFLATE compressed data, and then a checksum. It has been a long time, but I seem to remember that there was something different with .NET’s implementation than what is commonly seen in the wild, and I had to implement a basic ZlibStream class when doing some Minecraft NBT project years ago.

zigimg uses a pure Zig reader/writer implementation, so it might be worth a peek to see how they did it. If they don’t use std.zlib, it may be an indicator that it suffers from the same issue.

1 Like

They use I think this. If I remember correctly the only difference was a checksum thingy.

I believe so, and TIL Zig has an adler32 implementation in std, which makes it even easier if you opt for a custom implementation. This does make me curious what the incompatibility issue is with Zlib implementations, as I assume zigimg didn’t implement this for fun.

Most programs open / show “unoffical” png’s without a problem. That is why I was so confused a few years ago. It is indeed a tiny difference. Yes it was this adler thing.
Telegram for example requires a fully qualified png.

Thanks :slight_smile:

Oh I didn’t know the std lib provided those functions.
Over the last few weeks I wrote my own PNG encoder/decoder over at

I will see if I can remove some stuff in favor of the std lib. But I’m worried it might break. Right now it’s pretty self contained.

4 Likes

I would keep it, if you’re confident of its correctness. There’s nothing wrong with reimplementing certain functions that exist in the std lib for your own purposes.

1 Like

I see in deflate.zig

// Compression algorithm explained in rfc-1951 (slightly edited for this case)