資源簡介
實(shí)現(xiàn)了對一幅BMP格式的圖像進(jìn)行二元霍夫曼編碼和譯碼。
對一幅BMP格式的圖像進(jìn)行二元Fano編碼、譯碼。

代碼片段和文件信息
#include
#include
#include
#include
#include?
#include
#include?
#include?
#include?
using?namespace?std;
unsigned?char*?pBmpBuf; //image?read?pointer
int bmpWidth; //bmpwidth
int bmpHeight; //bmpheight
int BibitCount; //bits?per?pixel
RGBQUAD*?pColorTable; //palette?pointer
BITMAPFILEHEADER*?filehead;
BITMAPINFOHEADER*?infohead;
char?str[100]; //image?file?name/path
int?lineByte; //bits?per?row
int?k; //huffman?point
//build?the?struct?for?Huffman?coding
struct?node
{
short?lson?=?-1;
short?rson?=?-1;
short?num?=?0;
short?parent?=?-1;
int?frequency?=?0;
string??coded;
};
node?anode[2?*?256?-?1];
//get?scale?map??information
bool?readBmp(char*?bmpname)?{
FILE*?fp?=?fopen(bmpname?“rb“);
if?(fp?==?0)
{
cout?<“未能找到指定文件“;
return?false;
}
//skip?file?header
fseek(fp?sizeof(BITMAPFILEHEADER)?0);
//read?the?bitmap?information?header
infohead?=?new?BITMAPINFOHEADER;
fread(infohead?sizeof(BITMAPINFOHEADER)?1?fp);
bmpHeight?=?infohead->biHeight;
bmpWidth?=?infohead->biWidth;
BibitCount?=?infohead->biBitCount;
lineByte?=?ceil(bmpWidth?*?BibitCount?/?8?+?3)?/?4?*?4;
//read?the?palette
if?(BibitCount?==?8)
{
pColorTable?=?new?RGBQUAD[256];
fread(pColorTable?sizeof(RGBQUAD)?256?fp);
}
cout?<“1.頭文件處理完成“< //Read?pixel?encoding
int?n;
n?=?lineByte?*?bmpHeight;
pBmpBuf?=?new?unsigned?char[n];
fread(pBmpBuf?1?n?fp);
fclose(fp);
cout?<“2.成功讀取“< return?true;
}
//provide?a?sort?and?huffmam?method
bool?complare(node??&anode?node?&b)?{
return?anode.frequency?>?b.frequency;
}
//Traverses?the?image?pixels?that?the?pointer?points?
bool?sortnode(unsigned?char?*pBmpBufnode?anode[])
{
for?(int?i?=?0;?i?<=?255;?i++)
{
anode[i].num?=?i;
}
for?(int?i?=?0;?i? {
anode[int(pBmpBuf[i])].frequency++;
}
sort(anodeanode+256complare);
cout?<“3.排序中。。。。。“;
return?true;
}
//find?the?two?points?with?the?least?weight?to?construct?Huffman
int?selectmin(int?aint?k)?{
for?(int?i?=?0;?i?256+k;?i++)
{
if?(anode[i].parent?==?-1)?{
a?=?i;
break;
}
}
for?(int?i?=?0;?i?256+k?;?i++)
{
if?(anode[i].parent?==?-1?&&??anode[a].frequency?>?anode[i].frequency?)
{
a?=?i;
}
}
anode[a].parent?=?-2;
return?a;
}
//construct?Huffmam
bool?huffman(node?anode[])?{
for?(k?=?256;?k?256*2-1;?k++)
{
int?a?=?0;
int?b?=?0;
a?=?selectmin(a?k?-?256);
b?=?selectmin(b?k?-?256);
anode[k].frequency?=?anode[a].frequency?+?anode[b].frequency;
anode[a].parent?=?k;
anode[b].parent?=?k;
anode[k].lson?=?a;
anode[k].rson?=?b;
}
return?true?;
}
string?Coded[256];
//code?the?pixel?by?Huffman
bool?coding(node?anode[]string?Coded[])?{
for?(int?i?=?0;?i?256;?i++)
{
in
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????1756088??2019-05-25?22:41??哈夫曼編碼bmp圖像,費(fèi)諾編碼bmp圖像.docx
?????文件??????11246??2019-05-25?22:30??readbmp.cpp
-----------?---------??----------?-----??----
??????????????1767334????????????????????2
評論
共有 條評論