
PNG is a graphic format that appeared back in 1995 and is still widely used. Its main properties are lossless compression and support for transparent pixels. However, the Japanese programmer and artist under the nickname UCNV is not interested in the unique properties of this format, but in how to make it artistically buggy.
To learn how to implement glitches, you need to more carefully examine the details of the format.
Check sum
First of all, you should pay attention to the mechanism for generating a checksum using the CRC32 code. It is used to confirm damaged images. If damage is detected, the viewing application refuses to display it. Therefore, it is impossible to generate glitches using simple methods just by damaging a binary file. In other words, the PNG format is difficult to expose to glitches.
')
We will have to generate distortions in accordance with the PNG specifications. That is, we have to decode the CRC32, then rewrite the data, recalculate the checksum and add it to the edited data.
condition
Then we look at the process of transcoding in PNG.

Each of the four states indicated in the diagram may be the target of a glitch. But the introduction of distortions in the first state (raw data) is the same as distorting BMP, it makes no sense, and technically it is not a PNG bug. The distortion in the latter state does not work due to the above-mentioned checksum calculation system. The introduction of distortions in the second and third states, that is, in the filtered data and compressed data, remains. Manipulations with filters are most effective, they really give a good effect. With the help of hacks you can visually show the differences between the filters. Introducing glitches to the compressed data state also makes little sense. PNG uses Deflate compression, and some distortion looks just like white snow.
There are other elements besides the named ones that may affect the appearance of the final image. These are transparent pixels, etc.
Five filters
The factor that most influences the appearance of the picture after the introduction of the hack is filters. They convert uncompressed pixel data for each line, using certain algorithms to increase compression efficiency. There are a total of five types of filters, including four algorithms called Sub, Up, Average, and Paeth, as well as None (that is, filters are not applied). PNG images are usually compressed after the most effective filter is applied along each line. Therefore, usually all five filters are used in the final PNG image.
Five filters affect only the degree of compression, that is, the picture itself does not change depending on which filter was used. However, a clear distortion of the final result occurs in case of damage to the filtered data. It is difficult to understand the difference between filters if all five of them are used. But this difference becomes obvious when we introduce a glitch, and at the same time the same filter was used for each line.
Each filter is responsible for its part of the beauty that we see on the distorted PNG.
Glitches in practice
Original image
Distorted image
At the top, we see an example of a filter glitch. In the original image, the compression used optimized filters for each line. All five filters were applied.
The difference between the filters
Here is a distorted image, pre-filtered by the
None filter (that is, without filters).

Since no filters are applied, this distortion equates to a distortion in the raw data. Each pixel is isolated and has no relation to its neighbors. Therefore, the only modified byte has almost no effect.
Enlarged image fragment
Now the distorted image, pre-filtered
Sub .

If you apply this filter, the pixel rewrites itself, referring to the adjacent pixel. That is why the deviation to the right side is manifested.
Enlarged image fragment
Next, the distorted image, pre-filtered
Up .

The effect is similar to Sub, only directed vertically.
Enlarged image fragment
Distorted image, pre-filtered
Average .

The Average filter is responsible for the diagonal direction. It generates meteor-like shadows that start with a damaged pixel. The effect of a soft transition is also one of the consequences. The most beautiful glitch.
Enlarged image fragment
Distorted image, pre-filtered by
Paeth .

The Paeth filter implements the most complex algorithm, compared to all other filters. The effect affects large areas of the image even with a small number of distorted pixels. The original image is preserved, but it is intensively destroyed.
Enlarged image fragment
PNG is a relatively simple format, compared to JPEG and more modern formats. Filter algorithms are like toys, and the compression method is also pretty simple. Nevertheless, it shows a rather interesting variety of glitches.
A Japanese programmer laid out the
PNGlitch Ruby library, which helps embed glitches and overwrite checksums. On
its page you can see the catalog of works and get instructions on how to use the library.