Jpg learning notes 4 (with complete code)

Time:2021-2-24

The third step of JPG coding is quantization. For the 8 * 8 block data after DCT change, we need to quantify the 8 * 8 data. In JPEG, quantization is to divide the data V by a certain number Q to get round (V / Q) instead of the original data. Then, when JPG is decoded, it is multiplied by m to get v.

It should be noted that quantization will lose image accuracy and is irreversible.

The size of m also affects the compression effect. The larger the m, the better the compression effect, but the greater the loss of image accuracy.

The picture is quoted from “compressed image file formats, JPEG, PNG, GIF, XBM, BMP – John Miano” [1]

1. The quantization process of JPEG

First of all, we will have an 8 * 8 quantization table, which can be customized, or we can use the quantization table provided by JPEG standard.

Just divide our 8 * 8 block data by the corresponding quantification table data.

Jpg learning notes 4 (with complete code)Jpg learning notes 4 (with complete code)

const Block QUANTIZATION_TABLE_Y = {16,  11,  10,  16,  24,   40,   51,   61,   
                                    12,  12,  14,  19,  26,   58,   60,   55,   
                                    14,  13,  16,  24,  40,   57,   69,   56,   
                                    14,  17,  22,  29,  51,   87,   80,   62,   
                                    18,  22,  37,  56,  68,   109,  103,  77,   
                                    24,  35,  55,  64,  81,   104,  113,  92,   
                                    49,  64,  78,  87,  103,  121,  120,  101,  
                                    72,  92,  95,  98,  112,  100,  103,  99  };

const Block QUANTIZATION_TABLE_CBCR = { 17,  18,  24,  47,  99, 99,  99,  99,   
                                        18,  21,  26,  66,  99, 99,  99,  99,   
                                        24,  26,  56,  99,  99, 99,  99,  99,   
                                        47,  66,  99,  99,  99, 99,  99,  99,   
                                        99,  99,  99,  99,  99, 99,  99,  99,   
                                        99,  99,  99,  99,  99, 99,  99,  99,   
                                        99,  99,  99,  99,  99, 99,  99,  99,  
                                        99,  99,  99,  99,  99, 99,  99,  99  };

Quantitative table

 

 

Suppose that our data is lower left and the quantitative table is lower right

 

Then round (V / a) is the value we need

 

2. Code

void JPG::quantization() {
    for (uint i = 0; i < mcuHeight; i++) {
        for (uint j = 0; j < mcuWidth; j++) {
            MCU& currentMCU = data[i * mcuWidth + j];
            //Iterate over every component Y, CB, Cr
            for (uint componentID = 1; componentID <= 3; componentID++) {
                //Traversing block
                for(uint ii = 0; ii < getVerticalSamplingFrequency(componentID); ii++) {
                    for(uint jj = 0; jj < getHorizontalSamplingFrequency(componentID); jj++) {
                        Block& currentBlock = currentMCU[componentID][ii * getHorizontalSamplingFrequency(componentID) + jj];
                        const Block& quantizationTable = getQuantizationTableByID(componentID);
                        //This step is to quantize 64 pixels
                        for(uint index = 0; index < 64; index++) {
                             currentBlock[index] = currentBlock[index] / quantizationTable[index];
                        }
                    }
                }             
            }
        }
    }  
}

All of the above code in the https://github.com/Cheemion/JPEG_ COMPRESS/tree/main/Day4

end

  Thanks for reading,

  Wish you have a good day.

>>>>Jpg learning notes 5


 

reference material

[1]https://github.com/Cheemion/JPEG_COMPRESS/blob/main/resource/Compressed%20Image%20File%20Formats%20JPEG%2C%20PNG%2C%20GIF%2C%20XBM%2C%20BMP%20-%20John%20Miano.pdf

Recommended Today

Practice analysis of rust built-in trait: partialeq and EQ

Abstract:Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. This article is shared from Huawei cloud community《Analysis of rust built-in trait: partialeq and EQ》Author: debugzhang Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. Some traits can be automatically […]