Skip to content

PNG Libraries Info

Yi Xie edited this page Apr 14, 2021 · 11 revisions

system

Built-in PNG encoder on iOS/macOS. It has the following major drawbacks:

  • Image must be fully processed before encoding, which requires extra time and memory usage after processing. The amount of memory needed is not predictable, which results in a lot of crashes during Generating image stage.
  • Image data must be provided in full memory buffer. If the output image does not fit into memory, there is no luck processing it.

The main reason iOS/macOS does not provide a better PNG encoder interface is that it is not needed. Mobile apps hardly need to handle excessively large images. However, it is not the case for waifu2x-ios. Most people are upscaling images to 4x. The No.1 crash reason of this app is due to no enough memory to hold output image buffer.

libpng

To resolve these issues in the system PNG encoder, libpng was added since 4.3.3. A brand-new streamlined PNG encoder was built on this library. It has become the default PNG encoder. It has many advantages over the system encoder:

  • Encode image while processing, saving encoding time after processing.
  • Encode only a fraction of the image at a time. There is no longer a need to store the full image buffer in memory, which means you can process images much larger than the device memory can hold. For example, you can save images with 3GB pixel data with less than 1GB available memory, which requires at least 4GB memory with the system encoder.
  • Write output file data directly to the file. Encoded PNG data no longer needs to be stored in memory as well.
  • No large spikes of memory usage at encoding stage. Memory usage is more predictable.
  • Option to use a higher compression level as well as filter, resulting in smaller file size than the system encoder.
  • Ability to write image orientation to Exif data. This is disabled for now because it is a fairly new specification. Most programs including Photoshop and GIMP (as of Mar 2021) cannot read it.

It also supports many already-supported features in the system encoder:

  • 16-bit color support.
  • Color space support. Non-sRGB color spaces are preserved.

It also has some major disadvantages over the system encoder, which are not an issue in most cases:

  • Increase CPU usage during processing. This means the device will heat up faster.
  • If the image is processed faster than it is encoded, the progress will be slowed down by encoding. If this happens (most likely with Neural Engine or 16-bit color depth), try to enable fastest compression option if you do not mind losing compressing ratio.

How is this effective?

No-crash users ratio increased from 99.5% to 99.9%.

Compression options

To understand the following content, please take a quick look at this documentation first. Most of the time you don't need to change this.

default

  • zlib level 4, all filters enabled

faster

  • zlib level 3, all filters disabled

better

  • zlib level 6, all filters enabled