Hacker News new | ask | show | jobs
by SideQuark 1245 days ago
> The CRC and the compression are non-trivial. CRC is a table and 5 lines of code. That's trivial.

>zlib is 23k lines

It's not needed to make a PNG reader/writer. zlib is massive overkill for only making a PNG reader or writer. Here's a tiny deflate/inflate code [2] under 1k lines (and could be much smaller if needed).

stb[0] has single headers of ~7k lines total including all of the formats PNG, JPG, BMP,. PSD, GIF, HDR, and PIC. Here's [1] a 3k lines single file PNG version with tons if #ifdefs for all sorts of platforms. Removing those and I'd not be surprised if you could not do it in ~1k lines (which I'd consider quite simple compared to most of todays' media formats).

>Of course they're not common formats so you're stuck with complex formats like PNG

BMP is super common and easy to use anywhere.

I use flat image files all the time for quick and dirty stuff. They quickly saturate disk speeds and networking speeds (say recording a few decent speed cameras), and I've found PNG compression to alleviate those saturate CPU speeds (some libs are super slow, some are vastly faster). I've many times made custom compression formats to balance these for high performance tools when neither things like BMPs or things like PNG would suffice.

[0] https://github.com/nothings/stb

[1] https://github.com/richgel999/fpng/blob/main/src/fpng.cpp

[2] https://github.com/jibsen/tinf/tree/master/src