1、LZSS压缩算法实验报告实验名称:LZSS压缩算法实验报告一、实验内容使用Visual 6.0 C+编程实现LZ77压缩算法。二、实验目的用LZSS实现文件的压缩。三、实验原理LZSS压缩算法是词典编码无损压缩技术的一种。LZSS压缩算法的字典模型使用了自适应的方式,也就是说,将已经编码过的信息作为字典,四、实验环境1、软件环境:Visual C+ 6.02、编程语言:C+五、实验代码#include #include #include #include /* size of ring buffer */#define N 4096/* index for root of binary sea
2、rch trees */#define NIL N/* upper limit for g_match_len. Changed from 18 to 16 for binarycompatability with Microsoft COMPRESS.EXE and EXPAND.EXE#define F 18 */#define F 16/* encode string into position and lengthif match_length is greater than this: */#define THRESHOLD 2/* these assume little-endia
3、n CPU like Intel x86- need byte-swap function for big endian CPU */#define READ_LE32(X) *(uint32_t *)(X)#define WRITE_LE32(X,Y) *(uint32_t *)(X) = (Y)/* this assumes sizeof(long)=4 */typedef unsigned long uint32_t;/* text (input) size counter, code (output) size counter,and counter for reporting pro
4、gress every 1K bytes */static unsigned long g_text_size, g_code_size, g_print_count;/* ring buffer of size N, with extra F-1 bytesto facilitate string comparison */static unsigned char g_ring_bufferN + F - 1;/* position and length of longest match; set by insert_node() */static unsigned g_match_pos,
5、 g_match_len;/* left & right children & parent - these constitute binary search tree */static unsigned g_left_childN + 1, g_right_childN + 257, g_parentN + 1;/* input & output files */static FILE *g_infile, *g_outfile;/*initialize trees*/static void init_tree(void) unsigned i;/* For i = 0 to N - 1,
6、g_right_childi and g_left_childi will be the right and left children of node i. These nodes need not be initialized. Also, g_parenti is the parent of node i. These are initialized to NIL (= N), which stands for not used. For i = 0 to 255, g_right_childN + i + 1 is the root of the tree for strings th
7、at begin with character i. These are initialized to NIL. Note there are 256 trees. */ for(i = N + 1; i = N + 256; i+) g_right_childi = NIL; for(i = 0; i = 0) if(g_right_childp != NIL) p = g_right_childp; else g_right_childp = r; g_parentr = p; return; else if(g_left_childp != NIL) p = g_left_childp;
8、 else g_left_childp = r; g_parentr = p; return; for(i = 1; i g_match_len) g_match_pos = p; g_match_len = i; if(g_match_len = F) break; g_parentr = g_parentp; g_left_childr = g_left_childp; g_right_childr = g_right_childp; g_parentg_left_childp = r; g_parentg_right_childp = r; if(g_right_childg_paren
9、tp = p) g_right_childg_parentp = r; else g_left_childg_parentp = r; g_parentp = NIL; /* remove p */*deletes node p from tree*/static void delete_node(unsigned p) unsigned q; if(g_parentp = NIL) return; /* not in tree */ if(g_right_childp = NIL) q = g_left_childp; else if(g_left_childp = NIL) q = g_r
10、ight_childp; else q = g_left_childp; if(g_right_childq != NIL) do q = g_right_childq; while(g_right_childq != NIL); g_right_childg_parentq = g_left_childq; g_parentg_left_childq = g_parentq; g_left_childq = g_left_childp; g_parentg_left_childp = q; g_right_childq = g_right_childp; g_parentg_right_ch
11、ildp = q; g_parentq = g_parentp; if(g_right_childg_parentp = p) g_right_childg_parentp = q; else g_left_childg_parentp = q; g_parentp = NIL;/*/static void compress(void) unsigned i, len, r, s, last_match_length, code_buf_ptr; unsigned char code_buf17, mask; int c; init_tree(); /* initialize trees */
12、* code_buf1.16 saves eight units of code, and code_buf0 works aseight flags, 1 representing that the unit is an unencoded letter (1 byte),0 a position-and-length pair (2 bytes). Thus, eight units require at most16 bytes of code. */ code_buf0 = 0; code_buf_ptr = mask = 1; s = 0; r = N - F;/* Clear th
13、e buffer with any character that will appear often. */ memset(g_ring_buffer + s, , r - s);/* Read F bytes into the last F bytes of the buffer */ for(len = 0; len F; len+) c = getc(g_infile); if(c = EOF) break; g_ring_bufferr + len = c; g_text_size = len; if(g_text_size = 0) /* text of size zero */ r
14、eturn;/* Insert the F strings, each of which begins with one or more spacecharacters. Note the order in which these strings are inserted.This way, degenerate trees will be less likely to occur. */ for(i = 1; i len) g_match_len = len;/* Not long enough match. Send one byte. */ if(g_match_len THRESHOL
15、D. */ else code_bufcode_buf_ptr = (unsigned char)g_match_pos; code_buf_ptr+; code_bufcode_buf_ptr = (unsigned char) (g_match_pos 4) & 0xF0) | (g_match_len - (THRESHOLD + 1); code_buf_ptr+; /* Shift mask left one bit. */ mask = 1; if(mask = 0) /* Send at most 8 units of code together */ for(i = 0; i
16、code_buf_ptr; i+) putc(code_bufi, g_outfile); g_code_size += code_buf_ptr; code_buf0 = 0; code_buf_ptr = mask = 1; last_match_length = g_match_len; for(i = 0; i last_match_length; i+) c = getc(g_infile); break;/* Delete old strings and read new bytes */ delete_node(s); g_ring_buffers = c;/* If the p
17、osition is near the end of buffer, extend the bufferto make string comparison easier. */ if(s g_print_count) printf(%12ldr, g_text_size); g_print_count += 1024; while(i+ 0); /* until length of string to be processed is zero */* Send remaining code. */ if(code_buf_ptr 1) for(i = 0; i = 1) /* Get a by
18、te. For each bit of this byte:1=copy one byte literally, from input to output0=get two more bytes describing length and position of previously-seen data, and copy that data from the ring buffer to output */ if(flags & 0x100) = 0) c = getc(g_infile); if(c = EOF) break;/* uses higher byte cleverly to
19、count eight */ flags = c | 0xFF00; /* 1=copy one byte literally, from input to output */ if(flags & 1) c = getc(g_infile); if(c = EOF) break; putc(c, g_outfile); g_ring_bufferr = c; r = (r + 1) & (N - 1); /* 0=get two more bytes describing length and position of previously-seen data, and copy that data from the ring buffer to output */ else i = getc(g_infile); if(i = EOF) break; j = getc(g_infile); if(j = EOF) break; i |= (j & 0xF0) 4); j = (j & 0x0F) + THRESHOL
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1