www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁(yè) > 工業(yè)控制 > 電子設(shè)計(jì)自動(dòng)化
[導(dǎo)讀]以下是C語(yǔ)言實(shí)現(xiàn)無(wú)損壓縮算法的代碼:#include <stdio.h> #include <stdlib.h> #include <time.h> #define DNUM 64//define data number 8*8 #define LOOP 10000 //times of compression typedef struct { unsigne

以下是C語(yǔ)言實(shí)現(xiàn)無(wú)損壓縮算法的代碼:
#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define DNUM 64//define data number 8*8

#define LOOP 10000 //times of compression

typedef struct

{

unsigned short weight, data;

unsigned short parent, lchild, rchild;

} HuffNode;

typedef struct

{

unsigned char code;

unsigned short codelength;

} HuffCode;

unsigned int fCount[256] = {0};

unsigned int data_num;

unsigned int code_size;

unsigned int last_bit;

void FrequencyCount(unsigned char*);//頻率統(tǒng)計(jì)

void HuffSelect(HuffNode*, int, int*, int*); //從結(jié)點(diǎn)中選出權(quán)最小的兩個(gè)節(jié)點(diǎn)

void HuffmanCodeTable(HuffNode*, HuffCode*); //構(gòu)造huffman樹(shù),生成huffman編碼表

void HuffmanCompress(unsigned char*, unsigned char *, HuffCode*); //壓縮數(shù)據(jù)

void BitPrint(unsigned char*);//按位打印結(jié)果,用于調(diào)試

void main()

{

int i, j, loop;//variable for loop

HuffNode hfdata[2*DNUM] = {{0, 0, 0, 0, 0}}; //Huffman node

HuffCode code_table[256] = {{0, 0}};//code table will be searched by subscript

unsigned char hfcode[2*DNUM];//output code

time_t time1, time2;

/* unsigned char pixel[DNUM] = {

1,2,3,4, 1,2,3,4, 1,2,3,4, 1,1,1,1};

*/

/* unsigned char pixel[DNUM] = {

139,144,149,153,155,155,155,155,

144,151,153,156,159,156,156,156,

150,155,160,163,158,156,156,156,

159,161,162,160,160,159,159,159,

159,160,161,162,162,155,155,155,

161,161,161,161,160,157,157,157,

162,162,161,163,162,157,157,157,

162,162,161,161,163,158,158,158};

*/

unsigned char pixel[DNUM] = { //random data

141, 101, 126, 111, 163, 112, 133, 156,

103, 144, 111, 176, 117, 120, 188, 187,

175, 164, 190, 156, 112, 179, 142, 119,

140, 111, 127, 186, 196, 190, 189, 127,

185, 103, 185, 110, 192, 139, 159, 104,

151, 193, 178, 198, 114, 170, 179, 149,

124, 149, 165, 108, 141, 176, 113, 164,

101, 140, 120, 126, 173, 189, 158, 184};

/* unsigned char pixel[DNUM] = {

202, 221, 159, 183, 41, 136, 247, 66,

146, 29, 101, 108, 45, 61, 210, 236,

90, 130, 54, 66, 132, 206, 119, 232,

184, 135, 96, 78, 120, 41, 231, 203,

150, 94, 172, 142, 122, 180, 150, 204,

232, 121, 180, 221, 3, 207, 115, 147,

72, 149, 169, 121, 76, 208, 235, 43,

107, 58, 0, 237, 197, 7, 210, 89};

*/

FrequencyCount(pixel);

time1 = time(NULL);

for (loop=0; loop<LOOP; loop++) {

//set huffman nodes data and weight, i=0:255, j=1:64

for (i=0, j=1, data_num=0; i<256; i++) {

if (fCount[i]) {

hfdata[j].weight = fCount[i];

hfdata[j++].data = i;

data_num ++;

}

}

//build huffman tree and generate huffman code table

HuffmanCodeTable(hfdata, code_table);

//compress source data to huffman code using code table

HuffmanCompress(pixel, hfcode, code_table);

//initial hfdata and code_table

for (j=0; j<2*DNUM; j++) {

hfdata[j].data=0;

hfdata[j].lchild=0;

hfdata[j].parent=0;

hfdata[j].rchild=0;

hfdata[j].weight=0;

}

}

time2 = time(NULL);

//conclude

printf("n哈夫曼編碼壓縮圖塊,壓縮報(bào)告n華中科技大學(xué)力學(xué)系:李美之n");

printf("n◎源數(shù)據(jù)(%d字節(jié)):n ", DNUM);

for (i=0; i<DNUM; i++) {

printf(i%8==7 ? "%02Xn " : "%02X ", pixel[i]);

}

printf("n◎壓縮數(shù)據(jù)(%d字節(jié)):n ", code_size);

for (i=0; i<code_size; i++) {

printf(i%8==7 ? "%02Xn " : "%02X ", hfcode[i]);

}

//打印碼表

printf("nn◎碼表-編碼字典(%d項(xiàng))n", data_num);

for (i=0; i<256; i++) {

if (code_table[i].codelength) {

printf("%3d|%02X: ", i, i);

for (j=0; j<code_table[i].codelength; j++) {

printf("%d", ((code_table[i].code << j)&0x80)>>7);

}

printf("t");

}

}

printf("nn◎壓縮率:%2.0f%% t壓縮時(shí)間:%.3f毫秒n",(float)code_size/DNUM * 100, 1E3*(time2-time1)/LOOP);

}

void BitPrint(unsigned char *hfcode)

{

int i, j;

int endbit = last_bit;

unsigned char thebyte;

for (i=0; i < code_size-1; i++) {

thebyte = hfcode[i];

for (j=0; j<8; j++) {

printf("%d", ((thebyte<<j)&0x80)>>7);

}

}

if (last_bit == 7) {

endbit = -1;

}

thebyte = hfcode[i];

for (j=7; j>endbit; j--) {

printf("%d", ((thebyte<<(7-j))&0x80)>>7);

}

}

void HuffmanCompress(unsigned char *pixel, unsigned char *hfcode, HuffCode * code_table)

{

int i, j;

int curbit=7; //current bit in _thebyte_

unsigned int bytenum=0; //number of destination code can also be position of byte processed in destination

unsigned int ptbyte=0; //position of byte processed in destination

unsigned int curlength; //code's length of _curcode_

unsigned char curcode; //current byte's huffman code

unsigned char thebyte=0; //destination byte write

unsigned char value; //current byte's value (pixel[])

//process every byte

for (i=0; i<DNUM; i++) {

value = pixel[i];

curcode = (code_table[value]).code;

curlength = (code_table[value]).codelength;

//move out every bit from curcode to destination

for (j=0;j<=curlength;j++) {

if ((curcode<<j)&0x80) {

thebyte |= (unsigned char)(0x01<<curbit);

}

curbit --;

if (curbit < 0) {

hfcode[ptbyte++] = thebyte;

thebyte = 0;

curbit = 7;

bytenum ++;

}

}

}

//think about which bit is the end

if (curbit != 7) {

hfcode[ptbyte] = thebyte;

bytenum ++;

}

code_size = bytenum;

last_bit = curbit;

}

void HuffmanCodeTable(HuffNode *hfdata, HuffCode *code_table)

{

int i, j; //variable for loop

int tree_num = 2*data_num - 1; //node of huffman tree

int min1, min2; //two minimum weight

int p; //the id of parent node

unsigned char curcode; //current code being processing

int curlength; //current code's length

//build huffman tree

for (i=data_num; i<tree_num; i++) {

HuffSelect(hfdata, i, &min1, &min2);

hfdata[min1].parent = i+1;

hfdata[min2].parent = i+1;

hfdata[i+1].lchild = min1;

hfdata[i+1].rchild = min2;

hfdata[i+1].weight = hfdata[min1].weight + hfdata[min2].weight;

}

//generate huffman code

//i present the i th code, j present from leaf to root in huffman tree

//hfdata[i].data (0:255) is a byte number

//編碼從葉讀到根,按位從高往低壓入一個(gè)字節(jié),讀編碼從左向右

for (i=1; i<=data_num; i++) {

curcode = 0;

curlength = 0;

for (j=i, p=hfdata[j].parent; p!=0; j=p, p=hfdata[j].parent) {

curlength ++;

if (j==hfdata[p].lchild) curcode >>= 1;

else curcode = (curcode >> 1) | 0x80; //0x80 = 128 = B1000 0000

}

code_table[hfdata[i].data].code = curcode;

code_table[hfdata[i].data].codelength = curlength;

}

}

void HuffSelect(HuffNode *hfdata, int end, int *min1, int *min2)

{

int i; //variable for loop

int s1, s2;

HuffNode wath[30];

for (i=0; i<30; i++) {

wath[i] = hfdata[i];

}

s1 = s2 = 1;

while (hfdata[s1].parent) {

s1++;

}

for (i=2; i<=end; i++) {

if (hfdata[i].parent == 0 && hfdata[i].weight < hfdata[s1].weight) {

s1 = i;

}

}

while (hfdata[s2].parent || s1 == s2) {

s2++;

}

for (i=1; i<=end; i++) {

if (hfdata[i].parent ==0 && hfdata[i].weight < hfdata[s2].weight && (i - s1)) {

s2 = i;

}

}

*min1 = s1;

*min2 = s2;

}

void FrequencyCount(unsigned char *chs)

{

int i;

for (i=0; i<DNUM; i++) {

fCount[*(chs+i)] ++;

}

}



來(lái)源:向明天進(jìn)軍0次

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開(kāi)發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開(kāi)幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉