# What is the purpose of the filtering the image data in the PNG file format

I am reading the W3C description of the PNG file format, in particular section $$9$$ on filtering. The filter functions:

• None(x) : the identity filter
• Sub(x) : subtract the pixel value of $$x$$ by the pixel value in the position immediately before x
• Up(x): subtract the pixel value of $$x$$ by the pixel value in the scanline before the scanline containing $$x$$
• Average(x): Subtract the pixel value of $$x$$ by the mean of the subtracted values described in Su and Up
• Path(x) : Substract the pixel value of $$x$$ by the result of the Paeth Predicator at three values, see the spec.

are applied to different scanlines of an image; the filter function is applied to each pixel in a line.

It is stated without justification that:

Filtering transforms the PNG image with the goal of improving compression

My question is why does applying these filters improve compression?

The usage of a word filtering here is somewhat of a misnomer. The filtering operation is typically associated with waveform processing for various applications, as pulse shaping in the transmitter or noise suppression in the receiver. Image filters are also used, but primarily not for better compressibility of image files. In the context of your reference of PNG file format standard, it is possible that this operation would be more aptly named data preprocessing.

A Wikipedia article on PNG file format is more explicit on the compressibility of filtered PNG image data:

An image line filtered in this way is often more compressible than the raw image line would be, especially if it is similar to the line above, since the differences from prediction will generally be clustered around 0, rather than spread over all possible image values.

To further clarify the effect of PNG data filtering on data compressibility, I cite the LZ77 algorithm article:

LZ77 algorithms achieve compression by replacing repeated occurrences of data with references to a single copy of that data existing earlier in the uncompressed data stream.

The less variability of "the differences from prediction" that are "clustered around 0", the more "repeated occurrences of data" exist "in the uncompressed data stream". Therefore, the more effective is the LZ77 algorithm's work.

The LZ77 is an algorithm the PNG standard uses for compression.

Notice also the statement from W3C description of the PNG file format:

Filters are applied to bytes, not to pixels, regardless of the bit depth or colour type of the image.

Again, the Wikipedia article is helpful to better understand this statement w.r.t. compressibility:

An image line filtered in this way is often more compressible than the raw image line would be, especially if it is similar to the line above, since the differences from prediction will generally be clustered around 0, rather than spread over all possible image values. This is particularly important in relating separate rows, since DEFLATE has no understanding that an image is a 2D entity, and instead just sees the image data as a stream of bytes.