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 | **************************************************************************/
|
---|
21 |
|
---|
22 | #ifndef H_HUFFMAN
|
---|
23 | #define H_HUFFMAN
|
---|
24 |
|
---|
25 | #include "sysfuncs.h"
|
---|
26 |
|
---|
27 | #include "filestats.h"
|
---|
28 |
|
---|
29 |
|
---|
30 | #define MAX_HUFFCODE_LEN 31
|
---|
31 |
|
---|
32 | typedef struct huff_data
|
---|
33 | {
|
---|
34 | int num_codes;
|
---|
35 | int mincodelen;
|
---|
36 | int maxcodelen;
|
---|
37 | int lencount[MAX_HUFFCODE_LEN + 1];
|
---|
38 | unsigned long min_code[MAX_HUFFCODE_LEN + 1];
|
---|
39 | char *clens;
|
---|
40 | }
|
---|
41 | huff_data;
|
---|
42 |
|
---|
43 |
|
---|
44 | huff_data *Generate_Huffman_Data (int num, long *freqs, huff_data * data,
|
---|
45 | u_long * mem);
|
---|
46 |
|
---|
47 | unsigned long *Generate_Huffman_Codes (huff_data * data, u_long * mem);
|
---|
48 |
|
---|
49 | unsigned long **Generate_Huffman_Vals (huff_data * data, u_long * mem);
|
---|
50 |
|
---|
51 | int Write_Huffman_Data (FILE * f, huff_data * hd);
|
---|
52 |
|
---|
53 | int Read_Huffman_Data (FILE * f, huff_data * hd, u_long * mem, u_long * disk);
|
---|
54 |
|
---|
55 | int F_Read_Huffman_Data (File * f, huff_data * hd, u_long * mem, u_long * disk);
|
---|
56 |
|
---|
57 | /* Calculate the number of bits required to code the data with the
|
---|
58 | specified frequencies. Normally freqs and counts should point to
|
---|
59 | the same array. */
|
---|
60 | double Calculate_Huffman_Size (int num, long *freqs, long *counts);
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | #define HUFF_ENCODE(x, codes, lens) \
|
---|
65 | do { \
|
---|
66 | register int __i; \
|
---|
67 | register int __clen = (lens)[x]; \
|
---|
68 | register unsigned long __code = (codes)[x]; \
|
---|
69 | for (__i=__clen-1; __i>=0; --__i) \
|
---|
70 | ENCODE_BIT((__code >> __i) & 1); \
|
---|
71 | } while(0)
|
---|
72 |
|
---|
73 | #define HUFF_ENCODE_L(x, codes, lens, count) \
|
---|
74 | do { \
|
---|
75 | HUFF_ENCODE(x, codes, lens); \
|
---|
76 | (count) += (lens)[x]; \
|
---|
77 | } while(0)
|
---|
78 |
|
---|
79 |
|
---|
80 | #define HUFF_DECODE(x, mcodes, values) \
|
---|
81 | do { \
|
---|
82 | register unsigned long *__min_code = (mcodes); \
|
---|
83 | register unsigned long *__mclen = __min_code; \
|
---|
84 | register unsigned long **__values = (values); \
|
---|
85 | register unsigned long __code = 0; \
|
---|
86 | do \
|
---|
87 | { \
|
---|
88 | DECODE_ADD(__code); \
|
---|
89 | } \
|
---|
90 | while (__code < *++__mclen); \
|
---|
91 | (x) = __values[__mclen - __min_code][__code - *__mclen]; \
|
---|
92 | } while(0);
|
---|
93 |
|
---|
94 |
|
---|
95 | #define HUFF_DECODE_L(x, mcodes, values, count) \
|
---|
96 | do { \
|
---|
97 | register unsigned long *__min_code = (mcodes); \
|
---|
98 | register unsigned long *__mclen = __min_code; \
|
---|
99 | register unsigned long **__values = (values); \
|
---|
100 | register unsigned long __code = 0; \
|
---|
101 | do \
|
---|
102 | { \
|
---|
103 | DECODE_ADD(__code); \
|
---|
104 | (count)++; \
|
---|
105 | } \
|
---|
106 | while (__code < *++__mclen); \
|
---|
107 | (x) = __values[__mclen - __min_code][__code - *__mclen]; \
|
---|
108 | } while(0);
|
---|
109 |
|
---|
110 |
|
---|
111 |
|
---|
112 | #endif
|
---|