This topic will discuss JPEG, a popular format for storing and sharing images online. It's great for saving photos on computers and uploading them to the internet. With JPEG, images can take up less space without losing much quality. Let's learn more about this popular format.
Creating JPEG
In 1986, a group of experts from different parts of the world came together to improve how images are sent and saved on the internet. They formed the "Joint Photographic Experts Group," or JPEG for short. In 1992, they introduced the JPEG standard, which uses a clever algorithm to make digital images take up less space.
It's important to note that the JPEG standard doesn't specify the exact way images are stored in a file. Instead, it focuses on advanced techniques for compressing images. To store JPEG-encoded data, a format called "JPEG File Interchange Format," or JFIF, was developed.
When you save an image using JPEG, the most common file extensions are .jpg and .jpeg. However, you might also come across .jpe, .jfif, or .jif extensions. These extensions indicate that the image uses JPEG compression. Now, let's look at the structure of files with JPEG compression.
Structure of file
Like most other bitmap files, a JFIF file stores image data as a series of blocks, but here, each block is assigned a marker. Look at the structure of a JFIF file in the picture below.
The beginning of every JPEG file is marked by the Start Of Image (SOI) marker. Then, a JFIF APP0 (Application) marker. The combination of the SOI and JFIF APP0 markers serves as an image header, providing all the basic information about the image. Additionally, there can be optional extension markers. After these markers, the file contains quantization and Huffman coding tables and the actual image data stored in blocks with scans. The End of Image (EOI) marker shows the end of the file.
Now, let's explore what JPEG is exactly — compression algorithm.
JPEG compression
JPEG is mostly a lossy method of compression. As you know, lossy schemes throw some data away during encoding. That's how lossy approaches are better in saving space and making files smaller. JPEG focuses on removing data that the human eye cannot see easily.
The process of JPEG compression involves a few steps:
- Color space conversion to the YCbCr color space and downsampling.
- Discrete Cosine Transform (DCT).
- Quantization.
- Encoding the resulting image data using Run-Length Encoding (RLE) and a Huffman encoding.
Let's look at some of the steps in more detail.
YCbCr
The human eye is more sensitive to the details in the black-and-white parts of an image, which are related to its brightness. The color information in an image is less important to our eyes. Therefore, you can discard much of the color information without sacrificing much quality for your eyes.
YCbCr is a color space commonly used in digital image and video compression systems, and it consists of three components: luminance (Y), chrominance blue (Cb), and chrominance red (Cr). This color model separates the brightness information (luminance) from the color information (chrominance) in an image.
In the following downsampling, the algorithm compresses the colored parts of an image, chrominance blue (Cb) and chrominance red (Cr), while keeping the luminance intact because, as we said earlier, humans are more sensitive to changes in brightness than in color.
DCT and quantization
DCT stands for Discrete Cosine Transform. It is actually the main part of the JPEG compression. In essence, DCT converts the image's pixel values into frequency coefficients, representing different levels of changes in the image. For that, It splits the image into small 8x8 pixel blocks and applies mathematical calculations to transform them into a combination of 64 coefficients.
The general meaning is that human eyes overlook high-frequency information, for example, sharp transitions in intensity and color hue. So, you can separate out the high- and low-frequency information present in the image and remove high-frequency data without losing low-frequency information that you see more clearly.
After DCT comes the quantization step, which is all about dividing and rounding. During this step, the DCT coefficients are divided by specific values from a quantization table and rounded to the nearest integer. This process helps remove some of the high-frequency details in the image. Information is lost during this step. By the way, this step is controlled by the "quality" setting in JPEG compressors, which is actually adjusting values in the quantization table. The higher the values — the more data is lost.
Advantages of JPEG
Let's now explore the benefits of the JPEG file format.
One major advantage is its ability to compress images while preserving a good level of quality. If you're saving illustrations or photos with smooth transitions of brightness and shadows, JPEG is a great choice.
In addition, JPEG files generally have smaller sizes than formats like PNG. This is especially convenient when you need to save storage space or quickly upload images to the internet. You can also adjust the file size and image quality when saving files in JPEG.
Another advantage of JPEG is its wide compatibility with various software programs. JPEG files work very well with all browsers and image editors.
Furthermore, JPEG supports a wide range of color spaces, including such as RGB and CMYK.
Disadvantages of JPEG
JPEG yet does have a few drawbacks.
One drawback is that JPEG compression is still lossy, meaning it sacrifices some image quality to reduce file size. If preserving every detail is important for you, you should consider alternative formats like PNG or BMP.
JPEG is also not ideal for saving images with text or images with sharp edges. Heavily compressed JPEGs can show artifacts like blurriness or blocky areas.
Another downside is that each time you open and save a JPEG file, it loses a bit of its original quality. If you often edit and save a JPEG file, it can gradually lose details and sharpness. It's best to work with the original uncompressed version or save changes in a different file format to avoid this.
Lastly, JPEG does not support transparency.
Conclusion
Let's recap the main things about JPEG:
- JPEG is a lossy method of compression;
- JPEG compression scheme includes color conversion with downsampling, DCT, quantization, Huffman coding, and RLE;
- Discrete Cosine Transform is the most important step in JPEG compression. It converts the pixel values into frequency coefficients;
- quantization is a step of JPEG where data is lost;
- main advantage of JPEG is removing a lot of information while still keeping images looking good enough;
- You have to remember that JPEG does not have an alpha channel, and it is not a good choice for images with sharp edges, like text.