日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 工業(yè)控制 > 電子設(shè)計(jì)自動化
[導(dǎo)讀]以下是C語言實(shí)現(xiàn)無損壓縮算法的代碼:#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語言實(shí)現(xiàn)無損壓縮算法的代碼:
#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)最小的兩個節(jié)點(diǎn)

void HuffmanCodeTable(HuffNode*, HuffCode*); //構(gòu)造huffman樹,生成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壓縮時間:%.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

//編碼從葉讀到根,按位從高往低壓入一個字節(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)] ++;

}

}



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

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

在工業(yè)控制系統(tǒng)中,Modbus RTU協(xié)議的CRC校驗(yàn)如同通信網(wǎng)絡(luò)的"免疫系統(tǒng)",某石化廠DCS系統(tǒng)曾因CRC計(jì)算錯誤導(dǎo)致0.3%的數(shù)據(jù)包丟失,引發(fā)連鎖控制故障。本文將深入解析CRC-16/MODBUS算法原理,對比軟件...

關(guān)鍵字: Modbus RTU CRC 算法

加密算法分對稱加密和非對稱算法,其中對稱加密算法的加密與解密密鑰相同,非對稱加密算法的加密密鑰與解密密鑰不同,此外,還有一類不需要密鑰的散列算法。

關(guān)鍵字: 算法 嵌入式

在現(xiàn)代數(shù)字系統(tǒng)設(shè)計(jì)中,將算法高效地轉(zhuǎn)化為 RTL(寄存器傳輸級)實(shí)現(xiàn)是 FPGA 工程師的核心任務(wù)之一。這一過程不僅需要對算法有深入理解,還需掌握 FPGA 的硬件特性和設(shè)計(jì)技巧。本文將詳細(xì)介紹從算法到 RTL 實(shí)現(xiàn)的關(guān)...

關(guān)鍵字: 算法 寄存器傳輸級 數(shù)字系統(tǒng)

從本質(zhì)上講,算法是一種有條不紊、分步驟解決問題或完成任務(wù)的方法。無論是簡單的數(shù)字相加公式,還是復(fù)雜的機(jī)器學(xué)習(xí)協(xié)議,算法都是軟件應(yīng)用的基礎(chǔ),確保任務(wù)能夠高效有效地執(zhí)行。

關(guān)鍵字: 算法 嵌入式

在自動駕駛技術(shù)的發(fā)展歷程中,激光雷達(dá)(LiDAR)宛如一顆備受矚目的新星,其獨(dú)特的技術(shù)特性使其成為追求高安全性、高可靠性自動駕駛方案的首選。然而,這顆新星并非毫無爭議,“價格昂貴、結(jié)構(gòu)復(fù)雜、算法難度高” 等標(biāo)簽,也讓一些...

關(guān)鍵字: 自動駕駛 激光雷達(dá) 算法

4月2日消息,近日,有關(guān)智能駕駛而引發(fā)的交通事故在網(wǎng)絡(luò)上引起了大家的熱烈討論,對此,央視網(wǎng)評指出,“智能駕駛”,也請握緊方向盤。

關(guān)鍵字: 算法 智能駕駛

所謂排序算法,即通過特定的算法因式將一組或多組數(shù)據(jù)按照既定模式進(jìn)行重新排序。這種新序列遵循著一定的規(guī)則,體現(xiàn)出一定的規(guī)律,因此,經(jīng)處理后的數(shù)據(jù)便于篩選和計(jì)算,大大提高了計(jì)算效率。對于排序,我們首先要求其具有一定的穩(wěn)定性,...

關(guān)鍵字: 排序算法 算法

快速排序通過一趟排序?qū)⒋判蛄蟹指畛瑟?dú)立的兩部分,其中一部分序列的關(guān)鍵字均比另一部分序列的關(guān)鍵字小,則可分別對這兩部分序列繼續(xù)進(jìn)行排序,以達(dá)到整個序列有序的目的。

關(guān)鍵字: 快速排序 算法

算法,作為解決問題的精確描述,是描述策略機(jī)制的系統(tǒng)方法。讓我們在周末輕松探討五個具有深遠(yuǎn)影響的算法:Metropolis-Hastings算法、單純形法、快速傅立葉變換、快速排序算法,以及計(jì)算特征值的QR算法。這些算法在...

關(guān)鍵字: 算法 快速排序算法

服務(wù)需要保護(hù)自己,以免被太多的請求淹沒(無論是惡意或無意的),從而保持可用性。舉個生活中的例子,某個景區(qū),平時可能根本沒什么人前往,但是一旦到了國慶假日就人滿為患,這時景區(qū)管理人員就會實(shí)施一系列的限流舉措,來限制進(jìn)入的人...

關(guān)鍵字: 限流 算法
關(guān)閉