Hello again! I found this article written back in May 2019. This is a continuation of a series of articles about WAVE and JPEG. Here . This post will include information about the image encoding algorithm and the format itself in general.
A pinch of history
A tablespoon of Wikipedia:
JPEG (Joint Photographic Experts Group) is one of the popular raster graphic formats used for storing photographs and similar images.
This standard was developed by the Joint Photographic Experts Group back in 1991 for effective image compression.
What journey do images take from raw to JPEG
Some consider JPEG images to be raw data compressed using Huffman coding, but that's not true. Before the final compression, the data undergoes a long process.
First, the color model is converted from RGB to YCbCr. There is even a special algorithm for this— . The Y component remains unchanged because it represents brightness, and altering it would be noticeable.
The first step taken with the image is «subsampling» (subsampling). This is simple to understand: a 2x2 array of pixels is taken, and then the Cb and Cr components are averaged from each of the YCbCr components of these 4 pixels. Thus, we save 6 bytes, instead of 4 Y, 4 Cb, and 4 Cr we get 4 Y and the same Cb and Cr for each (4 + 4 + 4 = 12; 4 + 1 + 1 = 6; 12 - 6 = 6). Even in the case of 2x2, loss compression with a compression ratio of 2:1 sounds solid. This is applied to the entire image. So, we reduced half the size. Such a method can be used thanks to our color perception. A person can easily notice differences in brightness but not in color if it is averaged in a small block of pixels. Additionally, subsampling can be done in a line, 4 pixels horizontally and vertically. The first variant is used more often. If image quality is important, subsampling is not performed at all.
A visual illustration of subsampling (Habr didn't allow inserting a GIF) —
The main part of the preparation
DCT
Now for the most complex and necessary part. The entire image is divided into 8x8 blocks (padding is used if the resolution is not a multiple of the block size).
Now the DCT (Discrete Cosine Transform) is applied to each block.In this section, everything unnecessary is removed from the image. Using DCT, we need to determine whether this block (8×8) describes some monotonous part of the image: the sky, walls; or if it contains a complex structure (hair, symbols, etc.). It makes sense that 64 similar-colored pixels can be described by just one, since the size of the block is already known. This is where compression happens: 64 to 1.
DCT transforms the block into a spectrum, and where the readings change sharply, the coefficient becomes positive, and the sharper the transition, the higher the output will be. Where the coefficient is higher, the image depicts clear transitions in color and brightness, while where it is lower, the changes in the YCbCr component values within the block are weak (smooth).
Quantization
At this point, compression settings are applied. Each of the coefficients in each of the 8×8 matrices is divided by a specific number. If the quality of the image after all modifications will not be reduced further, then the divisor should be one. If you care more about the memory occupied by this photograph, the divisor will be greater than 1, and the quotient is rounded. This results in many zeros after rounding.
Quantization is done to create the possibility of even greater compression. Here’s how it looks with the example of quantization of the graph y = sin(x):

Compression
First, we traverse the matrix in a zig-zag manner:

We obtain a one-dimensional array of numbers. We see that there are many zeros in it, which can be removed. To do this, instead of a sequence of numerous zeros, we write 1 zero and then a number indicating their count in the sequence. This way, we can reduce the total size of the array to 1/3. After that, we simply compress this array using Huffman coding and write it into the file.
Where Used
Everywhere. Like PNG, JPEG is used in cameras, operating systems (for company logos, application icons, thumbnails) and in all possible fields where efficient image storage is needed.
Output
Currently, knowledge about JPEG is only valuable for educational purposes, as it is already built-in and optimized by large groups of people, yet the core of science remains appealing.
file — continuous reading of events from one or more local files;
Source: habr.com
