admin管理员组

文章数量:1532330

2023年12月22日发(作者:)

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

}

printf("/n◎压缩数据(%d字节):/n ", code_size);

for (i=0; i

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

}

//打印码表

printf("/n/n◎码表-编码字典(%d项)/n", data_num);

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

if (code_table[i].codelength) {

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

for (j=0; j

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

}

printf("/t");

}

}

printf("/n/n◎压缩率:%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<>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

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<

thebyte |= (unsigned char)(0x01<

}

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

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

}

}

本文标签: 码表数据压缩编写程序实现