C++實(shí)現(xiàn)對(duì)RGB圖片進(jìn)行編碼的示例代碼
依據(jù)上一篇的JPEG編碼所得到的RGB信息,我們可以重新對(duì)RGB圖片進(jìn)行編碼,也可對(duì)其他圖片如BMP所得到的RGB信息進(jìn)行編碼,來(lái)得到*.jpg文件,注意我這里實(shí)現(xiàn)的JPEG編碼不知道為啥編碼出來(lái)的文件比原來(lái)大了好多。 還有要注意的地方,下面會(huì)著重寫(xiě)的
1.轉(zhuǎn)換色彩空間
看了挺多博客,說(shuō)是有不同的公式分別對(duì)應(yīng)不同用途的圖片。我這里使用的RGB轉(zhuǎn)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的結(jié)構(gòu)如下,如果要是還有Alpha這個(gè)值也可以添加進(jìn)去
struct RGB{
uint8_t red;
uint8_t green;
uint8_t blue;
};2.離散余弦變化
使用矩陣乘法,轉(zhuǎn)化那個(gè)DCT變換的公式,最后會(huì)得到 Y=AXA’ 這個(gè)公式A’是指A的轉(zhuǎn)置,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;
}計(jì)算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聚到一起,從而壓縮文件大?。ㄎ掖驍帱c(diǎn)發(fā)現(xiàn)我這里0里面經(jīng)常會(huì)出現(xiàn)1)。編碼方式是將88的矩陣變成164的矩陣。

4.量化
我看到很多博客都沒(méi)有詳細(xì)說(shuō)明,我這里說(shuō)一下:
此步量化是對(duì)已經(jīng)zigzag編碼后的量化,就是對(duì)已經(jīng)變成1×64的數(shù)組的變換,而對(duì)應(yīng)的量化表如下,也是1×64的數(shù)組,對(duì)應(yīng)項(xiàng)相乘即可
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
};依據(jù)這幾個(gè)表可以得出4個(gè)Huffman表,兩個(gè)直流和兩個(gè)交流表。
然后就要寫(xiě)數(shù)據(jù)了,真正壓縮數(shù)據(jù)前面的部分我就不寫(xiě)了,看后面的代碼吧,直接開(kāi)始?jí)嚎s數(shù)據(jù)如何編碼。
到此步驟你已經(jīng)得到了一個(gè)MCU的數(shù)據(jù)一次要寫(xiě)一個(gè)MCU,直流分量重置間隔默認(rèn)為圖片一行的MCU數(shù)量
這里要注意幾點(diǎn):
第一,要對(duì)直流分量進(jìn)行差分編碼,對(duì)交流分量進(jìn)行形成編碼
第二,如果寫(xiě)入的一個(gè)字節(jié)是0xFF,這是段標(biāo)識(shí)所以要在此字節(jié)后面加入一個(gè)字節(jié)的0x00
第三,如果一行的MCU都寫(xiě)入完畢,接下來(lái)寫(xiě)入DRI,那就需要把上一個(gè)MCU沒(méi)寫(xiě)入的部分寫(xiě)入,例如上一個(gè)MCU寫(xiě)入的字節(jié)多了兩位11,那就寫(xiě)入一個(gè)字節(jié)0b11000000,然后寫(xiě)入兩個(gè)字節(jié)的DRI標(biāo)識(shí)
第四,寫(xiě)入的數(shù)據(jù)中有負(fù)數(shù),比如寫(xiě)入-2,那對(duì)應(yīng)就應(yīng)該寫(xiě)入0b01,可使用如下方法得到對(duì)應(yīng)的值
len = bitLength((int)abs(val)) pow(2, len) + val - 1
數(shù)據(jù)編碼部分,要用寫(xiě)入數(shù)據(jù)有多少位查找對(duì)應(yīng)的Huffman編碼,先寫(xiě)入Huffman編碼,再寫(xiě)入數(shù)據(jù),直流交流均如此。
直流部分要使用差分編碼,寫(xiě)入的數(shù)據(jù)是上一個(gè)DC值減去此DC值的差值
交流部分要使用行程編碼,因?yàn)橹暗牟僮鞔蟛糠謹(jǐn)?shù)據(jù)都變成0了,所以行程編碼很合適,然后將有多少0和數(shù)據(jù)有多少位作為權(quán)重查找Huffman編碼
代碼如下
我將上述步驟都寫(xiě)到一起了,一次處理一個(gè)MCU并寫(xiě)入
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); // 得到的是一整個(gè)mcu,但是要把它分成多個(gè)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];// 進(jìn)行差分編碼
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; // 當(dāng)前字節(jié)在哪個(gè)bit位
static int curBitValue = 0; // 當(dāng)前值是多少
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;
}
//段類(lèi)型
enum JPEGPType{
SOF0 = 0xC0, //幀開(kāi)始
SOF1 = 0xC1, //幀開(kāi)始
SOF2 = 0xC2, //幀開(kāi)始
DHT = 0xC4, //哈夫曼表
SOI = 0xD8, //文件頭
EOI = 0xD9, //文件尾
SOS = 0xDA, //掃描行開(kāi)始
DQT = 0xDB, //定義量化表
DRI = 0xDD, //定義重新開(kāi)始間隔
APP0 = 0xE0, //定義交換格式和圖像識(shí)別信息
APP1 = 0xE1, //定義交換格式和圖像識(shí)別信息
APP2 = 0xE2, //定義交換格式和圖像識(shí)別信息
COM = 0xFE, //注釋
FLAG = 0xFF //開(kāi)始
};如果有問(wèn)題可以去這里看源碼這里的代碼是好使的,里面的圖片解析器只是實(shí)現(xiàn)了精度為1字節(jié)的量化表的解析
到此這篇關(guān)于C++實(shí)現(xiàn)對(duì)RGB圖片進(jìn)行編碼的示例代碼的文章就介紹到這了,更多相關(guān)C++圖片編碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++使用expected實(shí)現(xiàn)優(yōu)雅的錯(cuò)誤處理
C++ 中提供了很多中方式進(jìn)行錯(cuò)誤處理。無(wú)論是通過(guò)拋異常還是通過(guò)錯(cuò)誤碼,標(biāo)準(zhǔn)庫(kù)都提供相應(yīng)的調(diào)用,今天本文為大家介紹的是使用expected進(jìn)行錯(cuò)誤處理,感興趣的可以了解一下2023-06-06
C語(yǔ)言實(shí)現(xiàn)掃雷游戲詳細(xì)流程
windows自帶的游戲《掃雷》是陪伴了無(wú)數(shù)人的經(jīng)典游戲,本文將利用C語(yǔ)言實(shí)現(xiàn)這一經(jīng)典的游戲,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2022-05-05
詳解QListWidget如何實(shí)現(xiàn)自定義Item效果
這篇文章主要為大家介紹了如何通過(guò)QListWidget實(shí)現(xiàn)自定義Item效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2022-04-04
C語(yǔ)言lidar_align雷達(dá)里程計(jì)校準(zhǔn)功能詳解
這篇文章主要為大家介紹了C語(yǔ)言lidar_align雷達(dá)里程計(jì)校準(zhǔn)功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
C++ Easylogging++日志庫(kù)配置使用超詳細(xì)講解
這篇文章主要介紹了C++ Easylogging++日志庫(kù)配置使用,Easylogging++是用于C++應(yīng)用程序的單頭高效日志庫(kù)。它非常強(qiáng)大,高度可擴(kuò)展并且可以根據(jù)用戶(hù)的要求進(jìn)行配置2022-11-11
C++ STL 內(nèi) std::{bind/tuple/function} 簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了C++ STL 內(nèi) std::{bind/tuple/function} 簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

