实现大体思路遵循上述的原理,关于比特位的处理,如果直接用位运算的话,编写起来较复杂。于是我改用0和1的字符串来逐位表示一个个比特。也就是说,编码过程中经过像素值转成,再由字符串转成比特位,解码过程中经过比特位转成字符串,再由字符串转成像素值。通过字符串作为桥梁,可以避免复杂的位运算,而运行效率也不会下降多少。而至于像素值和字符串怎样转换,还要用到现成的bitset<32>类型。
HuffmanEncode函数对外使用。调用函数HuffmanEncode,输入图像的二维矩阵,图像的宽高,以及另存的压缩后的文件名,执行后得到压缩后的文件以及头部信息文件。
在HuffmanEncode函数内,findMinNode函数用于找到当前最少出现的节点;buildTree函数用于构建哈夫曼树;每个节点的下标为像素值,存有出现次数,指向左右子节点的下标,以及用来判断是否已删除的位;buildTable函数用于建立像素值和编码值的映射关系。
#include
#include
#include
#include
#include
using namespace std;
struct Node {
int count;
int left;
int right;
int removed;
};
int findMinNode(Node* nodes, int length) {
int index = -1;
for (int i = 0; i < length; i++) {
if ((index == -1 || nodes[i].count < nodes[index].count) && !nodes[i].removed && nodes[i].count > 0) {
index = i;
}
}
if (index != -1) {
nodes[index].removed = 1;
}
return index;
}
int buildTree(Node* nodes, int* counts) {
for (int i = 0; i < 256; i++) {
nodes[i].left = -1;
nodes[i].right = -1;
nodes[i].count = counts[i];
nodes[i].removed = 0;
}
int length = 256;
while (1) {
int l = findMinNode(nodes, length);
if (l == -1) {
break;
}
int r = findMinNode(nodes, length);
if (r == -1) {
break;
}
nodes[length].left = l;
nodes[length].right = r;
nodes[length].count = nodes[l].count + nodes[r].count;
nodes[length].removed = 0;
length++;
}
return length;
}
void buildTable(Node* nodes, int pos, string bits, string * table) {
int l = nodes[pos].left;
int r = nodes[pos].right;
if (nodes[pos].left == -1 && nodes[pos].right == -1) {
table[pos] = bits;
return;
}
buildTable(nodes, r, bits + "1", table);
buildTable(nodes, l, bits + "0", table);
}
void HuffmanEncode(unsigned char ** data, int height, int width, const char *writepath) {
FILE* fp;
int counts[256];
memset(counts, 0, sizeof(int) * 256);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
counts[data[i][j]]++;
}
}
Node nodes[256 * 2];
int length = buildTree(nodes, counts);
string table[256];
buildTable(nodes, length - 1, "", table);
string table_path = "";
table_path = table_path + writepath + "_table";
fp = fopen(table_path.c_str(), "w");
for (int i = 0; i < 256; i++) {
if (table[i].size() == 0) {
fprintf(fp, "2\n");
} else {
fprintf(fp, "%s\n", table[i].c_str());
}
}
fclose(fp);
int total_bit_length = 0;
for (int i = 0; i < 256; i++) {
total_bit_length += counts[i] * table[i].size();
}
char * str = new char[total_bit_length];
int cur = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
for (int k = 0; k < table[data[i][j]].size(); k++) {
str[cur] = table[data[i][j]][k];
cur++;
}
}
}
fp = fopen(writepath, "wb");
int times = total_bit_length / 32 + 1;
string total = "";
total = total + str;
for (int i = 0; i < 32 * times - total_bit_length; i++) {
total = total + "0";
}
fwrite(&total_bit_length, sizeof(int), 1, fp);
for (int i = 0; i < times; i++) {
bitset<32> byte(total.substr(32 * i, 32));
unsigned long tmp = byte.to_ulong();
fwrite(&tmp, sizeof(int), 1, fp);
}
fclose(fp);
}
函数HuffmanDecode直接调用,传入字符类型的数组指针作为解码后数据用于返回到主函数,图像的宽高以及编码文件的路径名。
decodeString函数用于解析由比特位转成的字符串,并将之解析为最终用于返回的解码图像矩阵。其中,传入的map类型参数是读取编码映射表后建立的编码值和像素值的解码映射表,map类型可以极大地减少查表的时间,降低时间复杂度。由于编码值的前缀唯一不会是其它的编码值,所以不会出现错误识别编码字符串的情况。
#include
#include
#include
#include
#include
using namespace std;
void decodeString(string & total, map<string, int> & table_map, int total_bit_length, unsigned char * data) {
int index = 0;
int cur = 1;
int head = 0;
while (head < total_bit_length) {
if (total[head] != '1' && total[head] != '0') {
head++;
cur = head + 1;
continue;
}
if (table_map.count(total.substr(head, cur - head))) {
data[index++] = table_map[total.substr(head, cur - head)];
head = cur;
cur = head + 1;
} else {
cur++;
}
}
}
void HuffmanDecode(unsigned char * decoded_data, int height, int width, const char *readpath) {
FILE* fp;
string table[256];
string path = "";
path = path + readpath + "_table";
fp = fopen(path.c_str(), "rb");
for (int i = 0; i < 256; i++) {
char tmp[30];
fscanf(fp, "%s", tmp);
table[i] = table[i] + tmp;
}
fclose(fp);
map<string, int> table_map;
for (int i = 0; i < 256; i++) {
table_map[table[i]] = i;
}
fp = fopen(readpath, "rb");
int total_bit_length;
fread(&total_bit_length, sizeof(int), 1, fp);
int times = total_bit_length / 32 + 1;
string total = "";
char * str = new char[total_bit_length];
int *words = new int[times];
fread(words, sizeof(int), times, fp);
int cur = 0;
for (int i = 0; i < times; i++) {
bitset<32> bits(words[i]);
string tmp = bits.to_string();
for (int j = 0; j < 32; j++) {
str[cur] = tmp[j];
cur++;
}
}
fclose(fp);
total = total + str;
decodeString(total, table_map, total_bit_length, decoded_data);
}
这个文件装的是主函数,以及主函数调用的关于压缩文件文件、头部信息文件和映射表文件等的读写处理的函数。
#include
#include
#include
#include
#include "HuffmanEncode.cpp"
#include "HuffmanDecode.cpp"
using namespace std;
struct ImageHeader {
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
int rgb[256];
};
int ReadImage(string path, ImageHeader & ih, unsigned char ** & data) {
FILE * fp;
fp = fopen(path.c_str(), "rb");
if (fp == NULL) {
return 0;
}
fread(&ih.bf, sizeof(BITMAPFILEHEADER), 1, fp);
fread(&ih.bi, sizeof(BITMAPINFOHEADER), 1, fp);
fread(&ih.rgb, sizeof(int), 256, fp);
if (ih.bi.biBitCount != 8) {
printf("Gray image only!\n");
return 0;
}
data = new unsigned char*[ih.bi.biHeight];
int row_width = ih.bi.biWidth + (4 - ih.bi.biWidth % 4);
for (int i = 0; i < ih.bi.biHeight; i++) {
data[i] = new unsigned char[ih.bi.biWidth];
}
for (int i = ih.bi.biHeight - 1; i >= 0; i--) {
for (int j = 0; j < ih.bi.biWidth; j++) {
fread(&data[i][j], 1, 1, fp);
}
if (ih.bi.biWidth % 4 > 0) {
fseek(fp, 4 - ih.bi.biWidth % 4, SEEK_CUR);
}
}
fclose(fp);
return 1;
}
int CopyHeader(string path, ImageHeader & ih) {
FILE * fp;
fp = fopen(path.c_str(), "wb");
if (fp == NULL) {
return 0;
}
fwrite(&ih.bf, sizeof(BITMAPFILEHEADER), 1, fp);
fwrite(&ih.bi, sizeof(BITMAPINFOHEADER), 1, fp);
fwrite(&ih.rgb, sizeof(int), 256, fp);
fclose(fp);
return 1;
}
int ReaderHeader(string path, ImageHeader & ih2) {
FILE * fp;
fp = fopen(path.c_str(), "rb");
if (fp == NULL) {
return 0;
}
fread(&ih2.bf, sizeof(BITMAPFILEHEADER), 1, fp);
fread(&ih2.bi, sizeof(BITMAPINFOHEADER), 1, fp);
fread(&ih2.rgb, sizeof(int), 256, fp);
fclose(fp);
return 1;
}
int WriteImage(string path, ImageHeader & ih2, unsigned char * & decoded_data) {
FILE * fp;
fp = fopen(path.c_str(), "wb");
if (fp == NULL) {
return 0;
}
fwrite(&ih2.bf, sizeof(BITMAPFILEHEADER), 1, fp);
fwrite(&ih2.bi, sizeof(BITMAPINFOHEADER), 1, fp);
fwrite(&ih2.rgb, sizeof(int), 256, fp);
for (int i = ih2.bi.biHeight - 1; i >= 0; i--) {
fwrite(&decoded_data[i * ih2.bi.biWidth], ih2.bi.biWidth, 1, fp);
char tmp = 0;
if (ih2.bi.biWidth % 4 > 0) {
fwrite(&tmp, 1, 4 - ih2.bi.biWidth % 4, fp);
}
}
fclose(fp);
return 1;
}
int main() {
char readpath[50];
printf("BMP format image name:");
scanf("%s", readpath);
ImageHeader ih;
unsigned char ** data;
string path = "";
path = path + readpath + ".bmp";
if (ReadImage(path, ih, data)) {
printf("Image %s read successful.\n", readpath);
} else {
printf("Image %s reading failed.\n", readpath);
return 0;
}
path = "";
path = path + readpath + "_head";
if (CopyHeader(path, ih)) {
printf("Header file copied.\n");
} else {
printf("Header file copying failed.\n");
return 0;
}
path = "";
path = path + readpath;
HuffmanEncode(data, ih.bi.biHeight, ih.bi.biWidth, path.c_str());
printf("Image encoded.\n");
path = "";
path = path + readpath + "_head";
ImageHeader ih2;
if (ReaderHeader(path, ih2)) {
printf("Header file read successful.\n");
} else {
printf("Header file reading failed.\n");
return 0;
}
path = "";
path = path + readpath;
unsigned char * decoded_data = new unsigned char[ih2.bi.biHeight * ih2.bi.biWidth];
HuffmanDecode(decoded_data, ih2.bi.biHeight, ih2.bi.biWidth, path.c_str());
printf("Image decoded.\n");
path = "";
path = path + readpath + "_decode.bmp";
if (WriteImage(path, ih2, decoded_data)) {
printf("Decoded image saved successful.\n");
} else {
printf("Decoded image saving failed.\n");
return 0;
}
}
原图像test.bmp:
处理过程:
原图像文件test.bmp与压缩后文件test的大小对比:
压缩率约为15.27%。
解码后的图像test_decode.bmp:
原图像文件test.bmp与解码后图像test_decode.bmp的大小对比:
可以看到,解码后图像能一个字节都不差地被恢复出来。因为是无损压缩,失真率为0。
另再测试原图像test1.bmp:
处理过程:
原图像文件test1.bmp与压缩后文件test1的大小对比:
压缩率约为6.19%。
解码后的图像test1_decode.bmp:
原图像文件test1.bmp与解码后图像test1_decode.bmp的大小对比:
可以看到,解码后图像还是能一个字节都不差地被恢复出来。因为是无损压缩,失真率为0。
这次作业,一开始我并没打算纯用C/C++来实现Huffman压缩图像,因为原先不清楚bmp格式的文件怎么读取。但Matlab可以调用函数直接获得图像矩阵,这样不不用去处理文件头部信息了。但是对于Matlab很高级的编程工具,要做到Huffman建树、建表,以及按位来写文件的话却并不知道如何着手,反而是用惯了的C/C++更适合这种工作。于是我开始寻求两种优势兼具的解决方法:先用Matlab读取出图像矩阵,再将图像矩阵通过面向C语言的接口传给C代码压缩处理,最后返回解码后的图像矩阵到Matlab,让Matlab封装图像矩阵成bmp格式文件。这样一来,我既能不去考虑图像文件头部处理,又能用C/C++来从底层实现编码压缩和解码。
通过网上查阅资料,我学到了Matlab的C语言接口mex的基本使用,包括如何在Matlab传递参数,如何在C代码内解析参数。解析的过程有点绕,不过熟练掌握指针的知识的话也能正确处理好参数。
这份代码能正确进行Huffman编码和解码,不过最后我还是改用C++重新实现了一次,因为绕了这么久好像也就差个bmp格式文件的读写罢了,干脆再差点关于bmp的资料,把这个问题也用C语言解决了罢。
以上的代码我花了两天时间来编写,测试起来很痛苦,因为用对着图像矩阵检查像素值是否算对,甚至还要用BinaryViewer来检查二进制位是否正确,如果不正确又会是哪里出错了。每晚都要debug知道三四点,那几天感觉严重缺乏睡眠。
但总体来说,这次大作业,我能应用学到的多媒体技术知识来解决问题并温习了学过的知识,虽然过程不轻松,但最终能正确丝毫无误地解码出图像时,成就感还是满满的,结果还是很让自己满意,并且编程也算是能练练手了。
以下是我一开始用这种方法实现的代码:
Matlab脚本Huffman.m:
img = imread('test.bmp');
%img = rgb2gray(img);
[height, width] = size(img);
mex HuffmanEncode.cpp
HuffmanEncode(int32(img), 'test.txt');
mex HuffmanDecode.cpp
mat = uint8(HuffmanDecode('test.txt', height, width));
imwrite(mat, 'test_decoded.bmp', 'bmp');
HuffmanEncode.cpp:
#include "mex.h"
#include
#include
#include
#include
#include
using namespace std;
struct Node {
int count;
int left;
int right;
int removed;
};
int findMinNode(Node* nodes, int length) {
int index = -1;
for (int i = 0; i < length; i++) {
if ((index == -1 || nodes[i].count < nodes[index].count) && !nodes[i].removed && nodes[i].count > 0) {
index = i;
}
}
if (index != -1) {
nodes[index].removed = 1;
}
return index;
}
int buildTree(Node* nodes, int* counts) {
for (int i = 0; i < 256; i++) {
nodes[i].left = -1;
nodes[i].right = -1;
nodes[i].count = counts[i];
nodes[i].removed = 0;
}
int length = 256;
while (1) {
int l = findMinNode(nodes, length);
if (l == -1) {
break;
}
int r = findMinNode(nodes, length);
if (r == -1) {
break;
}
nodes[length].left = l;
nodes[length].right = r;
nodes[length].count = nodes[l].count + nodes[r].count;
nodes[length].removed = 0;
length++;
}
return length;
}
void buildTable(Node* nodes, int pos, string bits, string * table) {
int l = nodes[pos].left;
int r = nodes[pos].right;
if (nodes[pos].left == -1 && nodes[pos].right == -1) {
table[pos] = bits;
return;
}
buildTable(nodes, r, bits + "1", table);
buildTable(nodes, l, bits + "0", table);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
double* dataCursor = mxGetPr(prhs[0]);
int height = mxGetM(prhs[0]);
int width = mxGetN(prhs[0]);
char *writepath = mxArrayToString(prhs[1]);
FILE* fp;
int * data = (int *)dataCursor;
int counts[256];
memset(counts, 0, sizeof(int) * 256);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
counts[data[i + height * j]]++;
}
}
Node nodes[256 * 2];
int length = buildTree(nodes, counts);
string table[256];
buildTable(nodes, length - 1, "", table);
string table_path = "table_of_";
table_path = table_path + writepath;
fp = fopen(table_path.c_str(), "w");
for (int i = 0; i < 256; i++) {
if (table[i].size() == 0) {
fprintf(fp, "2\n");
} else {
fprintf(fp, "%s\n", table[i].c_str());
}
}
fclose(fp);
int total_bit_length = 0;
for (int i = 0; i < 256; i++) {
total_bit_length += counts[i] * table[i].size();
}
char * str = new char[total_bit_length];
int cur = 0;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
for (int k = 0; k < table[data[height * i + j]].size(); k++) {
str[cur] = table[data[height * i + j]][k];
cur++;
}
}
}
fp = fopen(writepath, "wb");
int times = total_bit_length / 32 + 1;
string total = "";
total = total + str;
for (int i = 0; i < 32 * times - total_bit_length; i++) {
total = total + "0";
}
fwrite(&total_bit_length, sizeof(int), 1, fp);
for (int i = 0; i < times; i++) {
bitset<32> byte(total.substr(32 * i, 32));
unsigned long tmp = byte.to_ulong();
fwrite(&tmp, sizeof(int), 1, fp);
}
fclose(fp);
}
HuffmanDecode.cpp:
#include "mex.h"
#include
#include
#include
#include
#include
using namespace std;
void decodeString(string & total, map<string, int> & table_map, int total_bit_length, double* res) {
int index = 0;
int cur = 1;
int head = 0;
while (head < total_bit_length) {
if (total[head] != '1' && total[head] != '0') {
head++;
cur = head + 1;
continue;
}
if (table_map.count(total.substr(head, cur - head))) {
res[index++] = table_map[total.substr(head, cur - head)];
head = cur;
cur = head + 1;
} else {
cur++;
}
}
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
const char *readpath = mxArrayToString(prhs[0]);
int height = mxGetScalar(prhs[1]);
int width = mxGetScalar(prhs[2]);
FILE* fp;
string table[256];
string table_path = "table_of_";
table_path = table_path + readpath;
fp = fopen(table_path.c_str(), "rb");
for (int i = 0; i < 256; i++) {
char tmp[30];
fscanf(fp, "%s", tmp);
table[i] = table[i] + tmp;
}
fclose(fp);
map<string, int> table_map;
for (int i = 0; i < 256; i++) {
table_map[table[i]] = i;
}
fp = fopen(readpath, "rb");
int buffer;
int total_bit_length;
fread(&total_bit_length, sizeof(int), 1, fp);
int times = total_bit_length / 32 + 1;
string total = "";
char * str = new char[total_bit_length];
int *words = new int[times];
fread(words, sizeof(int), times, fp);
int cur = 0;
for (int i = 0; i < times; i++) {
bitset<32> bits(words[i]);
string tmp = bits.to_string();
for (int j = 0; j < 32; j++) {
str[cur] = tmp[j];
cur++;
}
}
fclose(fp);
total = total + str;
double *res;
plhs[0] = mxCreateDoubleMatrix(height, width, mxREAL);
res = mxGetPr(plhs[0]);
decodeString(total, table_map, total_bit_length, res);
}