JPEG algorithm Overview

Source: Internet
Author: User
Tags coding standards

JPEG (joint photographic Experts Group) is an expert group composed of two organizations, ISO and IEC, responsible for developing static digital image data compression coding standards, the algorithm developed by this expert group is known as the JPEG algorithm and has become an international standard. Therefore, it is also known as the JPEG standard. JPEG is a widely used static image data compression standard. It can be used for both grayscale images and color images.
The JPEG Expert Group has developed two basic compression algorithms, one of which is a Lossy Compression Algorithm Based on the discrete cosine transform (discrete cosine transform, DCT, the other is a lossless compression algorithm based on prediction technology. When the lossy compression algorithm is used and the compression ratio is, it is difficult for non-image experts to identify the differences between the restored images after compression and the original images, therefore, it has been widely used. For example, in VCD and DVD-Video video image compression technology, JPEG lossy compression algorithms are used to cancel redundant data in the spatial direction. In order to further improve the compression ratio while ensuring image quality, the JPEG expert group has been developing the 2000standard in recent years, which will adopt wavelet algorithms.
JPEG compression is lossy compression. It utilizes the characteristics of a human's Perspective system and uses the combination of quantization and lossless compression encoding to remove the redundant information from the perspective and the redundant information of the data itself. JPEG algorithm Diagram

The compression code is roughly divided into three steps:
1. Use the forward discrete cosine transformation (Forward discrete cosine transform, FDCT) to change the image represented by the spatial field to the image represented by the frequency field.
2. Use the weighting function to quantify the DCT coefficients. This weighting function is optimal for human visual systems.
3. Use the Hoffmann variable-length encoder to encode the quantization coefficient.

The decoding or decompression process is the opposite of the compression and encoding process.

The JPEG algorithm is independent of the color space. Therefore, "RGB to YUV Conversion" and "YUV to RGB conversion" are not included in the JPEG algorithm. The JPEG algorithm processes color images with separate color components. Therefore, it can compress data from different color spaces, such as RGB, YCbCr, and CMYK.

Ii. Main JPEG algorithm calculation steps
The main calculation steps of the JPEG compression algorithm are as follows:
(1) Forward discrete cosine transformation (FDCT ).
(2) quantization ).
(3) Z-coded (zigzag scan ).
(4) use differential pulse coding modulation (DPCM) to encode the DC coefficient (DC.
(5) Run-length encoding (RLE) is used to encode the AC coefficient.
(6) entropy eoding ).

1. Forward discrete cosine transformation
The following describes the positive discrete cosine transform (FDCT) transformation.
(1) For each color image component, the entire component image is divided into several 8x8 image blocks and used as the input of two-dimensional discrete cosine transform DCT. Through DCT transformation, the energy is concentrated on a few coefficients.
 

(2) The following formula is used for DCT Transformation:

Its Inverse Transformation uses the following formula for calculation:

In the above two formula,
C (u), C (v) = (2)-1/2, when u, v = 0;
C (u), C (v) = 1, others.
After f (I, j) is transformed by DCT, F (0, 0) is the DC coefficient, and others are the AC coefficient.

(3) When calculating two-dimensional DCT transformation, you can use the following formula to convert the two-dimensional DCT transformation into one-dimensional DCT Transformation:


2. Quantization
Quantization quantifies the frequency coefficients after FDCT transformation. The purpose of quantification is to reduce the magnitude of the non-"0" coefficient and increase the number of "0" coefficient. Quantification is the main cause of image quality decline.
For lossy compression algorithms, the JPEG algorithm quantifies data using the uniform quantizer shown in. The Quantizing stride is determined based on the position of the coefficient and the tone value of each color component. Because the human eye is more sensitive to the Brightness Signal than the chromatic aberration signal, two quantization tables are used: the brightness quantization value and the chromatic aberration quantization value. In addition, because the human eye is more sensitive to low-frequency images than those of high-frequency images, the quantization stride in the upper left corner of the image is smaller than that in the lower right corner. The values in the following two tables are already the best for CCIR 601 standard TV images. If you do not use these two tables, you can replace them with your own quantified tables.

Brightness quantization value table and Color quantization value table

3. Z-shaped orchestration
The quantified coefficient must be re-arranged to increase the number of consecutive "0" coefficients, that is, the length of the "0" run. The method is to arrange the coefficients according to the Z-shaped style, as shown in. In this way, an 8x8 matrix is converted into a 1x64 vector, and the low frequency coefficient is placed on the top of the vector.

Quantize the DCT coefficient serial number

0

1

5

6

14

15

27

25

2

4

7

13

16

26

29

42

3

8

12

17

25

30

41

43

9

11

18

24

31

40

44

53

10

19

23

32

39

45

52

54

20

22

33

38

46

51

55

60

21

34

37

47

50

56

59

61

35

36

48

49

57

58

62

63

4. DC coefficient Encoding
The DC coefficient obtained by the 8x8 image block after DCT transformation has two characteristics: first, the coefficient value is relatively large, second, the DC series values of adjacent 8x8 image blocks do not change much. Based on this feature, the JPEG algorithm uses the differential pulse modulation coding (DPCM) technology to encode the difference (DELTA) between quantization DC coefficients between adjacent image blocks.
Delta = Dc () K-DC () K-1

5. AC coefficient Encoding
Quantization the AC coefficient is characterized by many "0" coefficients in the 1 × 64 vector, and many "0" are continuous, therefore, it is very simple and intuitive to use RLE to encode them.
JPEG uses the 4-bit height of 1 byte to represent the number of consecutive "0", and uses its 4-bit low to represent the number of digits required to encode the next non-"0" coefficient, what follows it is the numeric value that quantifies the AC coefficient.

6. Entropy Encoding
Entropy encoding further compresses the DC coefficient after DPCM encoding and the AC coefficient after RLE encoding.
In JPEG lossy compression algorithms, the Hoffman encoder is used to reduce entropy. The reason for using the Hoffmann encoder is that you can use a simple lookup table method for encoding. When compressing data symbols, the Hoffmann encoder allocates short codes to symbols with high frequencies, while allocating long codes to symbols with low frequencies. Such a variable-length Hoffman code table can be defined in advance.

7. Form a BIT data stream
The last step of JPEG encoding is to combine various labeled codes and encoded image data into a frame-by-frame data. This aims to facilitate transmission, storage, and decoder decoding, the data of such an organization is usually called a jpeg bitstream ).
 

How to organize image information in JPG files (byte-level)
-----------------------------------
Note that the JPEG/jfif file format uses the Motorola format instead of the Intel format,
That is to say, if it is a word, the high byte is in front and the low byte is in the back.

JPG files are composed of segments. The length of each segment is <= 65535.
Start of a markup word. All the markup words start with 0xff and end with non-0 bytes and 0xff. For example:
'Ffda ', 'ffc4', 'ffc0 '. Each tag has a specific meaning, which is indicated by 2nd bytes.
For example, SOS (startof scan = 'ffda') indicates that you should start decoding. Another tag
Dqt (define quantizationtable = 0 xffdb) is followed by a 64-byte
Quantization table

When processing JPG files, if you encounter a 0xff, And the byte after it is not 0, and
Bytes are meaningless. Therefore, the 0xff bytes you encounter must be ignored.
0xff for some filling purposes) If you happen to generate a 0xff during the Huffman encoding
Instead of 0xff 0x00. That is to say, when the JPEG image is decoded, it is treated as ff.

At the end of the Huffman Encoding Area, when a few bits are useless, we should fill them with 1.
Then follow ff.

Below are several important marks
--------------------

SoI = start of image = 'ffd8'
This mark appears only once at the beginning of the file
EOI = end of image = 'ffd9'
All JPG files end with ffd9

Rsti = ffdi (I = 0 .. 7) [rst0 = ffd0, rst7 = ffd7]
= Reset mark
Usually interspersed in the data stream, I think it is a concern about JPG decoding problems (should be used with dRI ).
However, many JPG files do not use it.

(SOS --- rst0 --- rst1 -- rst2 --...
... -- Rst6 --- rst7 -- rst0 --...)

----
Mark
----
The following are the tags that must be processed.

Sof0 = start of frame 0 = ffc0
SOS = start of scan = ffda
App0 = It's the marker used to identify a JPG file which uses the jfif
Specification = ffe0
COM = comment = fffe
DNL = define number of lines = ffdc
DRI = define restart interval = ffdd
Dqt = define quantization table = ffdb
DHT = define Huffman Table = ffc4

 

A (hmax * 8, Vmax * 8) block is called the MCU (minimun coded UNIX ).
One MCU is ydu, ydu, cbdu, and crdu.

If Hy = 1, Vy = 1
HCB = 1, VCB = 1
HCR = 1, VCR = 1
In this way (hmax = 1, Vmax = 1), the MCU is only 8x8, MCU = ydu, cbdu, crdu

For grayscale JPG, the MCU only has one du (MCU = ydu)

In the JPG file, the sampling coefficient of each component of the image is defined after the sof0 (ffc0) Mark.

Briefly decode JPG files
-------------------------
The decoder first reads the sampling coefficient from the JPG file, so that the MCU size is known and the integer is calculated.
Image contains several MCU. the decoder program recycles MCU decoding one by one until EOI mark is checked.
Note: For each MCU, calculate each DU in the regular order, and then combine and convert it to (R, G, B ).

Appendix: JPEG file format
~~~~~~~~~~~~~~~~

-File Header (2 bytes): $ ff, $ D8 (SOI) (JPEG file ID)
-For any number of segments, see the following section.
-End of file (2 bytes): $ ff, $ D9 (EOI)

Segment format:
~~~~~~~~~

-Header (4 bytes ):
$ FF segment ID
Type of N segments (1 byte)
Sh, SL length, including the two bytes, but not the previous $ ff and N.
Note: The length is not in Intel order, but Motorola's, and the high byte is in front,
The lower byte is behind!
-The content of this segment, up to 65533 bytes

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.