C++实现对RGB图片进行编码的示例代码
作者:咩~~
依据上一篇的JPEG编码所得到的RGB信息,我们可以重新对RGB图片进行编码,也可对其他图片如BMP所得到的RGB信息进行编码,来得到*.jpg文件,注意我这里实现的JPEG编码不知道为啥编码出来的文件比原来大了好多。 还有要注意的地方,下面会着重写的
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字节的量化表的解析
到此这篇关于C++实现对RGB图片进行编码的示例代码的文章就介绍到这了,更多相关C++图片编码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!