Using C language Program to implement a ZIP or RAR lossless compression algorithm
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * *huff. C Huffman encode for multimedia application 8*8 Pixel Ver 3 * * * *ver 1:complied in Borland Turbo C + + 3.0 * *ver 2:complied in Microsoft Visual C + + 6 .0 * *ver 3:complied in Microsoft Visual C + + 6.0 * * Add code to Prin T code table of the compression * * Print output in Chinese * * * *by Lee Meitz, Solid mechanics, Huazhong Univ of Sci and Tech * *2001.11.15-2001.12.27 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #include <stdio.h> #include <stdlib.h> #include <time.h> #define Dnum//defi NE 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*); Frequency statistics void Huffselect (huffnode*, int, int*, int*); The two node void huffmancodetable (huffnode*, huffcode*) with the smallest weights is selected from the nodes. Constructs the Huffman tree, generates the Huffman encoded table void huffmancompress (unsigned char*, unsigned char *, huffcode*); Compressed data void Bitprint (unsigned char*); Print results in bits for debugging 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}}; The code table is 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, 23 6, 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, 5
8, 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, D ata_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 Huffman coding compression block, compressed report \ Huazhong University of Engineering Mechanics Department: Li Mei");
printf ("\ n source data (%d bytes): \ n", dnum); For (i=0 i<dnum; i++) {printf (i%8==7?)
"%02x\n": "%02x", Pixel[i]);
printf ("\ n compressed data (%d bytes): \ n", code_size); For (i=0 i<code_size; i++) {printf (i%8==7?)
"%02x\n": "%02x", Hfcode[i]);
//Print Code table printf ("\ n Code table-coded dictionary (%d) \ n", data_num); For (i=0 i<256; i++) {if (code_table[i].codelength) {printf ("%3d|%0
2 x: ", I, I); For (j=0 j<code_table[i].codelength; j + +) {printf ("%d", (Code_table[i].code << j) &0x80)
; >7);
printf ("\ t");
} printf ("\ n \ nthe compression rate:%2.0f%% \ t Compressed time:%.3f MS \ n", (float) code_size/dnum *, 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 is position of byte processed in destination int unsigned; 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*d Ata_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 A 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].d
ATA (0:255) is a byte//encoding reads from leaf to root, bitwise from high to low voltage into a byte, read encoding from left to right 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)] + +; }
}