C++ JPEG编码

news2024/9/27 19:14:24

依据上一篇的JPEG编码所得到的RGB信息,我们可以重新对RGB图片进行编码,也可对其他图片如BMP所得到的RGB信息进行编码,来得到*.jpg文件,注意我这里实现的JPEG编码不知道为啥编码出来的文件比原来大了好多。 还有要注意的地方,下面会着重写的

步骤

  • 1.转换色彩空间
  • 2.离散余弦变化
  • 3.zigzag编码
  • 4.量化
  • 5.Huffman编码,(之前要差分编码和行程编码)
  • 代码如下

1.转换色彩空间

看了挺多博客,说是有不同的公式分别对应不同用途的图片。我这里使用的RGB转YCbCr的公式如下:

double y  = (0.299 * t.red + 0.587 * t.green + 0.114 * t.blue - 128);
double cb = (-0.1687 * t.red - 0.3313 * t.green + 0.500 * t.blue);
double cr = (0.500 * t.red - 0.4187 * t.green - 0.0813 * t.blue);

这里t的结构如下,如果要是还有Alpha这个值也可以添加进去

struct RGB{
	uint8_t red;
	uint8_t green;
	uint8_t blue;
};

2.离散余弦变化

使用矩阵乘法,转化那个DCT变换的公式,最后会得到 Y=AXA’ 这个公式A’是指A的转置,X是输入,Y是输出
矩阵A的获取方式如下:

double** JPEGData::createDCTAndIDCTArray(int row){
    double** res=new double*[row];
    for(int i=0;i<row;i++) res[i]=new double[row];
    for(int i=0;i<row;i++){
        for(int j=0;j<row;j++){
            double t=0;
            if(i==0) t=sqrt(1.0/row);
            else t=sqrt(2.0/row);
            res[i][j]=t*cos(M_PI*(j+0.5)*i/row);
        }
    }
    return res;
}

计算Y的方法如下:

void JPEGData::DCT(double** originMatrix){
    //原理 Y=A*X*A'
    vector<vector<double>> temp(ROW,vector<double>(COL,0));
    for(int i=0;i<ROW;i++){
        for(int j=0;j<COL;j++){
            double sum=0;
            for(int k=0;k<COL;k++){
                sum+=DCTAndIDCTArray[i][k]*originMatrix[k][j];
            }
            temp[i][j]=sum;
        }
    }
    for(int i=0;i<ROW;i++){
        for(int j=0;j<COL;j++){
            double sum=0;
            for(int k=0;k<COL;k++){
                sum+=temp[i][k]*DCTAndIDCTArray[j][k];
            }
            originMatrix[i][j]=sum;
        }
    }
}

3.zigzag编码

此编码的目的是为了使更多的0聚到一起,从而压缩文件大小(我打断点发现我这里0里面经常会出现1)。编码方式是将88的矩阵变成164的矩阵。
编码方式

4.量化

我看到很多博客都没有详细说明,我这里说一下:
此步量化是对已经zigzag编码后的量化,就是对已经变成1×64的数组的变换,而对应的量化表如下,也是1×64的数组,对应项相乘即可

static uint8_t YQualityTable[64] = {
    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};

static uint8_t CQualityTable[64]={
	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
};

5.Huffman编码,(之前要差分编码和行程编码)

Huffman编码的直流和交流表如下:

static const uint8_t bits_dc_luminance[16] = {//亮度表
	0, 1, 5, 1, 1, 1, 1, 1, 
	1, 0, 0, 0, 0, 0, 0, 0 
};
static const uint8_t val_dc_luminance[] = { 
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 
};

static const uint8_t bits_dc_chrominance[16] = {//色度表
	0, 3, 1, 1, 1, 1, 1, 1, 
	1, 1, 1, 0, 0, 0, 0, 0 
};
static const uint8_t val_dc_chrominance[] = { 
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 
};

static const uint8_t bits_ac_luminance[16] = {
	0, 2, 1, 3, 3, 2, 4, 3, 
	5, 5, 4, 4, 0, 0, 1, 0x7d 
};
static const uint8_t val_ac_luminance[] = { 
	0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
	0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
	0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
	0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
	0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
	0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
	0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
	0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
	0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
	0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
	0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
	0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
	0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
	0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
	0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
	0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
	0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
	0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
	0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
	0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
	0xf9, 0xfa 
};

static const uint8_t bits_ac_chrominance[16] = {
	0, 2, 1, 2, 4, 4, 3, 4, 
	7, 5, 4, 4, 0, 1, 2, 0x77 
};
static const uint8_t val_ac_chrominance[] = { 
	0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
	0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
	0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
	0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
	0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
	0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
	0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
	0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
	0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
	0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
	0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
	0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
	0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
	0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
	0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
	0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
	0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
	0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
	0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
	0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
	0xf9, 0xfa 
};

依据这几个表可以得出4个Huffman表,两个直流和两个交流表。
然后就要写数据了,真正压缩数据前面的部分我就不写了,看后面的代码吧,直接开始压缩数据如何编码。
到此步骤你已经得到了一个MCU的数据一次要写一个MCU,直流分量重置间隔默认为图片一行的MCU数量
这里要注意几点:
第一,要对直流分量进行差分编码,对交流分量进行形成编码
第二,如果写入的一个字节是0xFF,这是段标识所以要在此字节后面加入一个字节的0x00
第三,如果一行的MCU都写入完毕,接下来写入DRI,那就需要把上一个MCU没写入的部分写入,例如上一个MCU写入的字节多了两位11,那就写入一个字节0b11000000,然后写入两个字节的DRI标识
第四,写入的数据中有负数,比如写入-2,那对应就应该写入0b01,可使用如下方法得到对应的值

len = bitLength((int)abs(val))
pow(2, len) + val - 1

数据编码部分,要用写入数据有多少位查找对应的Huffman编码,先写入Huffman编码,再写入数据,直流交流均如此。
直流部分要使用差分编码,写入的数据是上一个DC值减去此DC值的差值
交流部分要使用行程编码,因为之前的操作大部分数据都变成0了,所以行程编码很合适,然后将有多少0和数据有多少位作为权重查找Huffman编码

代码如下

我将上述步骤都写到一起了,一次处理一个MCU并写入

bool JPEGData::writeJPEG(const char* filePath, int samp_factor[3][2], int quality_scale){
    auto _rgb = getRGBMatrix();
    max_h_samp_factor=0;
    max_v_samp_factor=0;
    for(int i=0;i<3;i++){
        write_samp_factor[i][0]=samp_factor[i][0];
        write_samp_factor[i][1]=samp_factor[i][1];
        max_h_samp_factor=max(max_h_samp_factor,write_samp_factor[i][0]);
        max_v_samp_factor=max(max_v_samp_factor,write_samp_factor[i][1]);
    }
    initQualityTable(quality_scale);
    fstream file(filePath,ios::out|ios::binary);
    if(file.fail()) return false;
    try{
        createDCEnHuffman();
        createACEnHuffman();
        writeTwoByte(file, (uint16_t)((FLAG << 8) + SOI));  // SOI
        for(int i=0;i<18;i++){              //APP
            writeByte(file, APP[i]);
        }
        JPEGQuality::write(file);                           // DQT
        writeTwoByte(file, (uint16_t)((FLAG << 8) + SOF0)); // SOF
        writeTwoByte(file, (uint16_t)0x0011);
        writeByte(file, (uint8_t)0x08);
        writeTwoByte(file, (uint16_t)height);
        writeTwoByte(file, (uint16_t)width);
        writeByte(file, (uint8_t)0x03);

        writeByte(file, (uint8_t)0x01);
        writeByte(file, (uint8_t)((samp_factor[0][0] << 4) | samp_factor[0][1]));
        writeByte(file, (uint8_t)0x00);

        writeByte(file, (uint8_t)0x02);
        writeByte(file, (uint8_t)((samp_factor[1][0] << 4) | samp_factor[1][1]));
        writeByte(file, (uint8_t)0x01);

        writeByte(file, (uint8_t)0x03);
        writeByte(file, (uint8_t)((samp_factor[2][0] << 4) | samp_factor[2][1]));
        writeByte(file, (uint8_t)0x01);

        JPEGHuffmanCode::write(file);   // DHT
        writeTwoByte(file, (uint16_t)((FLAG << 8) + DRI)); // DRI
        writeTwoByte(file, (uint16_t)0x0004);
        writeTwoByte(file, (uint16_t)ceil(_rgb.col * 1.0 / (ROW * max_h_samp_factor)));
        writeTwoByte(file, (uint16_t)((FLAG<<8)+SOS)); // SOS
        writeTwoByte(file, (uint16_t)0x000C);
        writeByte(file, (uint8_t)0x03);

        writeByte(file, (uint8_t)0x01);
        writeByte(file, (uint8_t)0x00);
        
        writeByte(file, (uint8_t)0x02);
        writeByte(file, (uint8_t)0x11);

        writeByte(file, (uint8_t)0x03);
        writeByte(file, (uint8_t)0x11);

        writeByte(file, (uint8_t)0);
        writeByte(file, (uint8_t)0x3F);
        writeByte(file, (uint8_t)0);
        RGBToYCbCr(_rgb, file);
        writeTwoByte(file, (uint16_t)((FLAG << 8) + (uint8_t)JPEGPType::EOI));  // EOI
    }
    catch(...){
        file.close();
        return false;
    }
    file.close();
    return true;
}
void JPEGData::RGBToYCbCr(Matrix<RGB> _rgb, fstream& file){
    // vector<double*> res;
    int mcu_width  = COL * max_h_samp_factor,
        mcu_height = ROW * max_v_samp_factor;
    int YUV[3] = {write_samp_factor[0][0] * write_samp_factor[0][1],
                  write_samp_factor[1][0] * write_samp_factor[1][1],
                  write_samp_factor[2][0] * write_samp_factor[2][1]};
    int y_h_param  = max_h_samp_factor * 1.0 / write_samp_factor[0][0],
        y_v_param  = max_v_samp_factor * 1.0 / write_samp_factor[0][1],
        cb_h_param = max_h_samp_factor * 1.0 / write_samp_factor[1][0],
        cb_v_param = max_v_samp_factor * 1.0 / write_samp_factor[1][1],
        cr_h_param = max_h_samp_factor * 1.0 / write_samp_factor[2][0],
        cr_v_param = max_v_samp_factor * 1.0 / write_samp_factor[2][1];
    double cb_h_samp_scale=write_samp_factor[1][0]*1.0/max_h_samp_factor,
           cb_v_samp_scale=write_samp_factor[1][1]*1.0/max_v_samp_factor,
           cr_h_samp_scale=write_samp_factor[2][0]*1.0/max_h_samp_factor,
           cr_v_samp_scale=write_samp_factor[2][1]*1.0/max_v_samp_factor;
    preDCValue[0] = preDCValue[1] = preDCValue[2] = 0;
    mcu_rows = ceil(_rgb.row * 1.0 / mcu_height);
    mcu_cols = ceil(_rgb.col * 1.0 / mcu_width);
    int DRI = mcu_cols, DriFLAG = 0xD0;
    for (int mcu_y = 0; mcu_y < mcu_rows; mcu_y++) {
        for (int mcu_x = 0; mcu_x < mcu_cols; mcu_x++) {
            double ***yuv = new double **[YUV[0] + YUV[1] + YUV[2]];
            int count=0;
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < YUV[i]; j++) {
                    yuv[count] = new double *[ROW];
                    for (int k = 0; k < ROW; k++) {
                        yuv[count][k] = new double[COL];
                        memset(yuv[count][k], 0, sizeof(double) * COL);
                    }
                    count++;
                }
            }
            int row = mcu_y * mcu_height,
                col = mcu_x * mcu_width;
            // cout<<dec<<"("<<row<<","<<col<<") ";
            for (int i = 0; i < mcu_height; i++) {
                for (int j = 0; j < mcu_width; j++) {
                    RGB t = _rgb.getValue(row + i, col + j); // 得到的是一整个mcu,但是要把它分成多个8*8矩阵
                    double y  = YCbCrValueLimit(0.299 * t.red + 0.587 * t.green + 0.114 * t.blue - 128);
                    double cb = YCbCrValueLimit(-0.1687 * t.red - 0.3313 * t.green + 0.500 * t.blue);
                    double cr = YCbCrValueLimit(0.500 * t.red - 0.4187 * t.green - 0.0813 * t.blue);
                    int yPos = (i / ROW) * max_h_samp_factor + (j / COL);
                    int cbPos = YUV[0] + (int)((j / COL) * cb_v_samp_scale) +
                                (int)((i / ROW) * cb_h_samp_scale);
                    int crPos = YUV[0] + YUV[1] +
                                (int)((j / COL) * cr_v_samp_scale) +
                                (int)((i / ROW) * cr_h_samp_scale);

                    if (i % y_v_param == 0 && j % y_h_param == 0)
                        yuv[yPos][i % ROW][j % COL] = y;
                    if (i % cb_v_param == 0 && j % cb_h_param == 0)
                        yuv[cbPos][i / cb_v_param][j / cb_h_param] = cb;
                    if (i % cr_v_param == 0 && j % cr_h_param == 0)
                        yuv[crPos][i / cr_v_param][j / cr_h_param] = cr;
                    // cout<<dec<<"("<<(int)y<<","<<(int)cb<<","<<(int)cr<<")"<<" ";
                }
                // cout<<endl;
            }
            // cout<<endl;
            int pos = 0;
            for (int i = 0; i < 3; i++) {
                int huffmanID = i == 0 ? 0 : 1;
                for (int j = 0; j < YUV[i]; j++, pos++) {
                    DCT(yuv[pos]);
                    double *temp = ZigZag(yuv[pos]);
                    //encode DC
                    temp[0] = round(temp[0] / (i == 0 ? YQualityTable[0] : CQualityTable[0])); //quality
                    int dcDiff = temp[0] - preDCValue[i];// 进行差分编码
                    int lenDC=getBitLength((int)abs(dcDiff));
                    preDCValue[i] = temp[0];
                    if (dcDiff < 0) dcDiff = pow(2, lenDC) + dcDiff - 1;
                    writeBit(file, lenDC, dcDiff, en_dc_huffman[huffmanID].table[lenDC]);
                    //encode AC
                    int endPos = 63;
                    while(endPos>0&&temp[endPos]==0) endPos--;
                    for(int k=1;k<=endPos;k++){
                        int zeroCount = 0;
                        temp[k] = round(temp[k] / (i == 0 ? YQualityTable[k] : CQualityTable[k]));//quality
                        while (k < endPos && temp[k] == 0) {
                            temp[k + 1] = round(temp[k + 1] / (i == 0 ? YQualityTable[k + 1] : CQualityTable[k + 1]));
                            zeroCount++;
                            k++;
                        }
                        int lenAC = getBitLength((int)abs(temp[k]));
                        if (temp[k] < 0) temp[k] = pow(2, lenAC) + temp[k] - 1;
                        while(zeroCount>=16){
                            writeBitToFile(file,en_ac_huffman[huffmanID].table[0xf0].second, en_ac_huffman[huffmanID].table[0xf0].first);
                            zeroCount -= 16;
                        }
                        writeBit(file, lenAC, temp[k], en_ac_huffman[huffmanID].table[(zeroCount << 4) | lenAC]);
                        zeroCount = 0;
                    }
                    if(endPos!=63) writeBitToFile(file, en_ac_huffman[huffmanID].table[0x00].second, en_ac_huffman[huffmanID].table[0x00].first);
                }
            }
            FREE_LP_3(yuv, YUV[0] + YUV[1] + YUV[2], ROW)
        }
        // cout<<endl;
        if (bitCurPos != 0) writeByte(file, (uint8_t)curBitValue);
        bitCurPos = curBitValue = 0;
        preDCValue[0] = preDCValue[1] = preDCValue[2] = 0;
        writeTwoByte(file, (uint16_t)((FLAG << 8) + DriFLAG++));
        if (DriFLAG > 0xD7)
            DriFLAG = 0xD0;
    }
}
void JPEGQuality::write(fstream &file){
    writeByte(file, FLAG);
    writeByte(file, DQT);
    writeTwoByte(file, (uint16_t)0x0043);
    writeByte(file, (uint8_t)0x00);
    for(int i=0;i<64;i++){
        writeByte(file, YQualityTable[i]);
    }
    writeByte(file, FLAG);
    writeByte(file, DQT);
    writeTwoByte(file, (uint16_t)0x0043);
    writeByte(file, (uint8_t)0x01);
    for(int i=0;i<64;i++){
        writeByte(file, CQualityTable[i]);
    }
}
void JPEGHuffmanCode::write(fstream &file){
    // Y dc
    writeByte(file, (uint8_t)FLAG);
    writeByte(file, (uint8_t)DHT);
    writeTwoByte(file, (uint16_t)0x001F);
    writeByte(file, (uint8_t)0x00);
    for(int i=0;i<16;i++) writeByte(file, bits_dc_luminance[i]);
    for(int i=0;i<12;i++) writeByte(file, val_dc_luminance[i]);
    // Y ac
    writeByte(file, (uint8_t)FLAG);
    writeByte(file, (uint8_t)DHT);
    writeTwoByte(file, (uint16_t)0x00B5);
    writeByte(file, (uint8_t)0x10);
    for(int i=0;i<16;i++) writeByte(file, bits_ac_luminance[i]);
    for(int i=0;i<162;i++) writeByte(file, val_ac_luminance[i]);
    // UV dc
    writeByte(file, (uint8_t)FLAG);
    writeByte(file, (uint8_t)DHT);
    writeTwoByte(file, (uint16_t)0x001F);
    writeByte(file, (uint8_t)0x01);
    for(int i=0;i<16;i++) writeByte(file, bits_dc_chrominance[i]);
    for(int i=0;i<12;i++) writeByte(file, val_dc_chrominance[i]);
    // UV ac
    writeByte(file, (uint8_t)FLAG);
    writeByte(file, (uint8_t)DHT);
    writeTwoByte(file, (uint16_t)0x00B5);
    writeByte(file, (uint8_t)0x11);
    for(int i=0;i<16;i++) writeByte(file, bits_ac_chrominance[i]);
    for(int i=0;i<162;i++) writeByte(file, val_ac_chrominance[i]);
}
static int bitCurPos = 0;   // 当前字节在哪个bit位
static int curBitValue = 0;    // 当前值是多少
void writeBitToFile(fstream& file,int len,int realData){
    while (len > (8 - bitCurPos))
    {
        int rightMoveBit = len + bitCurPos - 8;
        int bitValue = realData >> rightMoveBit;
        curBitValue |= bitValue;
        writeByte(file, (uint8_t)curBitValue);
        if (curBitValue == 0xFF)
            writeByte(file, (uint8_t)0);
        realData -= bitValue << rightMoveBit;
        len -= 8 - bitCurPos;
        curBitValue = bitCurPos = 0;
    }
    curBitValue |= realData << (8 - bitCurPos - len);
    bitCurPos += len;
    if (bitCurPos >= 8)
    {
        writeByte(file, (uint8_t)curBitValue);
        if (curBitValue == 0xFF)
            writeByte(file, (uint8_t)0);
        curBitValue = bitCurPos = 0;
    }
}
void writeBit(fstream& file,int len, int realData,pair<uint16_t,uint8_t>& huffmanCode){

    int codeLen=huffmanCode.second,code=huffmanCode.first;
    writeBitToFile(file, codeLen, code);

    writeBitToFile(file, len, realData);
}

double* ZigZag(double** originArray){
    double* res=new double[ROW*COL];
    // for(int i=0;i<64;i++){
    //     res[Zig[i]]=originArray[i/ROW][i%COL];
    // }
    int cur=0,x=0,y=0;
    bool flag = true;//true是右上 false是左下
    while (cur < 64) {
        res[cur++] = round(originArray[y][x]);
        if (flag) { x++; y--; }
        else { x--; y++; }
        if (x < 0 || y < 0 || x>7 || y>7) flag = !flag;
        if (x < 0 && y>7) { x = 1; y = 7; }
        if (x < 0) x = 0;
        else if (x > 7) { x = 7; y += 2; }
        if (y < 0) y = 0;
        else if (y > 7) { y = 7; x += 2; }
    }
    return res;
}

void writeByte(fstream& file,uint8_t val){
    file.write((char*)&val, 1);
    // cout<<hex<<(int)val<<" ";
}

void writeTwoByte(fstream& file,uint16_t val){
    writeByte(file, val>>8);
    writeByte(file, val&0xFF);
}
int getBitLength(int num){
    int res=0;
    while(num){
        res+=1;
        num>>=1;
    }
    return res;
}
double YCbCrValueLimit(double input){
    if(input<-128) return -128;
    else if(input>128) return 128;
    return input;
}
//段类型
enum JPEGPType{
    SOF0    = 0xC0,     //帧开始
    SOF1    = 0xC1,     //帧开始
    SOF2    = 0xC2,     //帧开始
    DHT     = 0xC4,     //哈夫曼表
    SOI     = 0xD8,     //文件头
    EOI     = 0xD9,     //文件尾
    SOS     = 0xDA,     //扫描行开始
    DQT     = 0xDB,     //定义量化表
    DRI     = 0xDD,     //定义重新开始间隔
    APP0    = 0xE0,     //定义交换格式和图像识别信息
    APP1    = 0xE1,     //定义交换格式和图像识别信息
    APP2    = 0xE2,     //定义交换格式和图像识别信息
    COM     = 0xFE,     //注释
	FLAG	= 0xFF		//开始
};

如果有问题可以去这里看源码这里的代码是好使的,里面的图片解析器只是实现了精度为1字节的量化表的解析

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/504519.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【计算机三级网络技术】 第六篇 交换机及其配置

文章目录 IPS&#xff08;入侵防护系统&#xff09;相关知识点蓝牙服务器技术DNS 服务器WWW 服务器FTP 服务器邮件&#xff08;Winmail 邮件服务器&#xff09;生成树协议IEEEVLAN 标识的描述DHCP 服务器 IPS&#xff08;入侵防护系统&#xff09;相关知识点 1、入侵防护系统&…

迅为i.MX6ULL开发板生成 KEY 文件,并安装

使用“ssh-keygen” 生成个四个 key 文件“ssh_host_rsa_key” “ssh_host_dsa_key” “ssh_host_ecdsa_key” 和“ssh_host_ed25519_key” 。 1 在虚拟机 Ubuntu 控制台&#xff0c; “ /home/ssh/openssh-4.6p1” 目录下&#xff0c; 使用命 令“ssh-keygen -t rsa -f ssh…

帮助客户实现自助服务,企业可以打造产品知识库来解决

随着科技的不断发展&#xff0c;越来越多的企业开始将自助服务作为一种解决客户问题的方式。自助服务不仅可以提高客户满意度&#xff0c;还可以减少企业的工作量和成本。为了帮助客户实现自助服务&#xff0c;企业可以打造产品知识库来解决客户问题。本文将介绍产品知识库的定…

shell脚本----函数

文章目录 一、函数的定义1.1 shell函数:1.2函数如何定义 二、函数的返回值三、函数的传参四、函数变量的作用范围五、函数的递归六、函数库 一、函数的定义 1.1 shell函数: 使用函数可以避免代码重复使用函数可以将大的工程分割为若干小的功能模块&#xff0c;代码的可读性更…

数字农业农村解决方案(ppt可编辑)

本资料来源公开网络&#xff0c;仅供个人学习&#xff0c;请勿商用&#xff0c;如有侵权请联系删除。 数字农业农村发展现状 数据基础薄弱&#xff1a;数据资源分散&#xff0c;天空地一体化数据获取能力弱&#xff1b;资源数字化、产业数字化水平不高&#xff0c;部分农业数…

“玲珑”编解码融合架构助力视频多元化需求

随着近年来 AI 技术的兴起&#xff0c;视频监控、汽车、智能家居、移动设备及数据中心等对高清视频处理有了越来越高的要求。安谋科技全新视频处理器——“玲珑”V6/V8&#xff0c;针对主流市场的视频流媒体技术进行了大量投入&#xff0c;通过一系列智能权衡实现了极大优化&am…

常用数据处理方式

文章目录 缺失值处理删除法填充法基于统计学变量填充基于插值填充基于模型填充基于预测填充 不处理 异常值处理基于统计分析的方法基于聚类的方法基于树的方法基于预测的方法 数据重采样标准化min-max标准化&#xff08;归一化&#xff09;z-score标准化&#xff08;规范化&…

基于Web的电竞赛事管理系统的设计与实现(论文+源码)_kaic

摘要 迅猛发展并日益成熟的网络已经彻底的影响了我们的方方面面。人们也确实真切的体会到了网络带给我们的便捷。本网站的设计理念在于作为一个大学生电竞赛事联盟推广网&#xff0c;就是能够尽可能详细地展示、介绍电竞赛事联盟资讯信息&#xff0c;播放视频&#xff0c;同时…

WhatsApp 营销:获得更多潜在客户和销售(一)

你需要了解客户的世界观才能进行有效的营销&#xff0c;你应该投入时间和精力来学习和实施你的业务WhatsApp营销 -因为你的客户出现在WhatsApp上&#xff0c;他们希望在那里联系&#xff0c;而不是在他们讨厌被打断的电子邮件或电话中。 SaleSmartly&#xff08;ss客服&#x…

基于磁盘的Kafka为什么这么快

基于磁盘的Kafka为什么这么快 原创 Wyman 大数据技术架构 2019-05-23 18:04 Kafka是大数据领域无处不在的消息中间件&#xff0c;目前广泛使用在企业内部的实时数据管道&#xff0c;并帮助企业构建自己的流计算应用程序。Kafka虽然是基于磁盘做的数据存储&#xff0c;但却具有…

代码随想录算法训练营day27 | 39. 组合总和,40.组合总和II,131.分割回文串

代码随想录算法训练营day27 | 39. 组合总和&#xff0c;40.组合总和II&#xff0c;131.分割回文串 39. 组合总和解法一&#xff1a;回溯解法二&#xff1a;回溯排序剪枝 40.组合总和II解法一&#xff1a;回溯&#xff08;使用used标记数组&#xff09;解法一&#xff1a;回溯&a…

1688获取商品api接口

作为一名技术爱好者&#xff0c;我们总会遇到各种各样的技术问题&#xff0c;需要寻找合适的技术解决方案。而在互联网时代&#xff0c;我们可以快速通过搜索引擎获取丰富的技术资源和解决方案。然而&#xff0c;在不同的技术分享中&#xff0c;我们常常会遇到质量参差不齐的文…

虹科方案 | 视频和广播专业人士的存储和存档解决方案

虹科HK & Overland-Tandberg 为所有视频和广播工作流阶段提供全面的数字媒体存储解决方案组合&#xff0c;包括创建、复制、传输、存储、保护和归档数据和内容。 一、后期制作工作流程 后期制作是一个多任务过程&#xff0c;通过掌握处理原始视频和声音元素。 这个过程的前…

c++ 11标准模板(STL) std::vector (十)

定义于头文件 <vector> template< class T, class Allocator std::allocator<T> > class vector;(1)namespace pmr { template <class T> using vector std::vector<T, std::pmr::polymorphic_allocator<T>>; }(2)(C17…

adb bugreport 与adb shell getprop 详解

&#x1f604;作者简介&#xff1a; 小曾同学.com,一个致力于测试开发的博主⛽️&#xff0c; 如果文章知识点有错误的地方&#xff0c;还请大家指正&#xff0c;让我们一起学习&#xff0c;一起进步。&#x1f60a; 座右铭&#xff1a;不想当开发的测试&#xff0c;不是一个好…

低代码行业未来如何?大家都真的看好低代码开发吗?

低代码行业未来如何&#xff1f;大家都真的看好低代码开发吗&#xff1f; 是否一定需要开发人员&#xff1f;低代码和无代码平台会取代传统编程吗&#xff1f;低代码/无代码真的是未来吗&#xff1f; 无疑是需要且重要的。今天就来解答为什么低/零代码工具越来越受欢迎&#xf…

第1章计算机系统漫游

文章目录 1、信息就是位上下文2、程序被其他程序翻译成不同的格式3、了解编译系统如何工作的益处4、处理器读并解释储存在存储器中的指令4.1 系统的硬件组成4.2 执行 hello 程序 5、高速缓存6、形成层次结构的存储设备7、操作系统管理硬件7.1 进程7.2 线程7.3 虚拟存储器7.4 文…

领英退出中国,谷歌Bard集成进安卓,陆奇最新演讲,HuggingFace网传遭禁|今日科技圈要闻

夕小瑶科技说 原创 作者 | 智商掉了一地、兔子酱 AI 新闻速递来咯&#xff01;搬好小板凳&#xff0c;一起了解近期发生了什么新鲜事~ 领英职场退出中国 领英是一个专注于职业发展、招聘和营销等方面的社交平台。Linkdein 官方公众号发布公告称&#xff0c;由于面临日趋激烈的…

Spring MVC——Rest风格

REST&#xff08;Representational State Transfer&#xff09; 当我们想表示一个网络资源的时候&#xff0c;可以使用两种方式: 我们分别用查询id为1的用户信息与保存用户信息举例传统风格资源描述形式 http://localhost/user/getById?id1http://localhost/user/saveUser RES…

第五届河南省CCPC河南省省赛题解+复盘

第五届河南省CCPC河南省省赛题解复盘 今年省赛相当有意思的一点&#xff0c;是20级第一次线下省赛&#xff0c;对于部分队也可能是最后一次&#xff0c;看队名就能看出来很多 考研就业的选手&#xff0c;一群老年人在这PK&#xff0c;氛围挺不错。 A - 小水獭游河南 — 签到 …