C++实现对RGB图片进行编码

1.转换色彩空间

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);

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

2.离散余弦变化

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;
}

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;
}
}
}

4.量化

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
};

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

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    //开始
};

|
3月前
|

97 2
|
3月前
|

78 2
|
3月前
|

72 1
|
3月前
|
C++

35 0
|
3月前
|

【编码狂想】探索C++ STL：提升编程效率的强大工具集
【编码狂想】探索C++ STL：提升编程效率的强大工具集
51 0
|
3月前
|

Linux C/C++ 开发（学习笔记八）：Mysql数据库图片存储
Linux C/C++ 开发（学习笔记八）：Mysql数据库图片存储
108 0
|
1月前
|

Qt(C++)开发一款图片防盗用水印制作小工具

87 1
|
3天前
|

【C++】C++ QT实现Huffman编码器与解码器（源码+课程论文+文件）【独一无二】
【C++】C++ QT实现Huffman编码器与解码器（源码+课程论文+文件）【独一无二】
14 4
|
3月前
|

【编码狂想】深度探索C++编程之旅：“数组、字符串、函数与KMP算法解密“
【编码狂想】深度探索C++编程之旅：“数组、字符串、函数与KMP算法解密“
90 0
|
3月前
|

153 1