source: trunk/gsdl3/src/packages/mg/lib/huffman.h@ 3745

Last change on this file since 3745 was 3745, checked in by mdewsnip, 21 years ago

Addition of MG package for search and retrieval

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1/**************************************************************************
2 *
3 * huffman.h -- Huffman coding functions
4 * Copyright (C) 1994 Neil Sharman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * $Id: huffman.h 3745 2003-02-20 21:20:24Z mdewsnip $
21 *
22 **************************************************************************/
23
24#ifndef H_HUFFMAN
25#define H_HUFFMAN
26
27#include "sysfuncs.h"
28
29#include "filestats.h"
30
31
32#define MAX_HUFFCODE_LEN 31
33
34typedef struct huff_data
35 {
36 int num_codes;
37 int mincodelen;
38 int maxcodelen;
39 int lencount[MAX_HUFFCODE_LEN + 1];
40 unsigned long min_code[MAX_HUFFCODE_LEN + 1];
41 char *clens;
42 }
43huff_data;
44
45
46huff_data *Generate_Huffman_Data (int num, long *freqs, huff_data * data,
47 u_long * mem);
48
49unsigned long *Generate_Huffman_Codes (huff_data * data, u_long * mem);
50
51unsigned long **Generate_Huffman_Vals (huff_data * data, u_long * mem);
52
53int Write_Huffman_Data (FILE * f, huff_data * hd);
54
55int Read_Huffman_Data (FILE * f, huff_data * hd, u_long * mem, u_long * disk);
56
57int F_Read_Huffman_Data (File * f, huff_data * hd, u_long * mem, u_long * disk);
58
59/* Calculate the number of bits required to code the data with the
60 specified frequencies. Normally freqs and counts should point to
61 the same array. */
62double Calculate_Huffman_Size (int num, long *freqs, long *counts);
63
64
65
66#define HUFF_ENCODE(x, codes, lens) \
67 do { \
68 register int __i; \
69 register int __clen = (lens)[x]; \
70 register unsigned long __code = (codes)[x]; \
71 for (__i=__clen-1; __i>=0; __i--) \
72 ENCODE_BIT((__code >> __i) & 1); \
73 } while(0)
74
75#define HUFF_ENCODE_L(x, codes, lens, count) \
76 do { \
77 HUFF_ENCODE(x, codes, lens); \
78 (count) += (lens)[x]; \
79 } while(0)
80
81
82#define HUFF_DECODE(x, mcodes, values) \
83 do { \
84 register unsigned long *__min_code = (mcodes); \
85 register unsigned long *__mclen = __min_code; \
86 register unsigned long **__values = (values); \
87 register unsigned long __code = 0; \
88 do \
89 { \
90 DECODE_ADD(__code); \
91 } \
92 while (__code < *++__mclen); \
93 (x) = __values[__mclen - __min_code][__code - *__mclen]; \
94 } while(0);
95
96
97#define HUFF_DECODE_L(x, mcodes, values, count) \
98 do { \
99 register unsigned long *__min_code = (mcodes); \
100 register unsigned long *__mclen = __min_code; \
101 register unsigned long **__values = (values); \
102 register unsigned long __code = 0; \
103 do \
104 { \
105 DECODE_ADD(__code); \
106 (count)++; \
107 } \
108 while (__code < *++__mclen); \
109 (x) = __values[__mclen - __min_code][__code - *__mclen]; \
110 } while(0);
111
112
113
114#endif
Note: See TracBrowser for help on using the repository browser.