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 9596 2005-04-07 05:06:26Z kjdon $
|
---|
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 |
|
---|
34 | typedef 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 | }
|
---|
43 | huff_data;
|
---|
44 |
|
---|
45 |
|
---|
46 | huff_data *Generate_Huffman_Data (int num, long *freqs, huff_data * data,
|
---|
47 | u_long * mem);
|
---|
48 |
|
---|
49 | unsigned long *Generate_Huffman_Codes (huff_data * data, u_long * mem);
|
---|
50 |
|
---|
51 | unsigned long **Generate_Huffman_Vals (huff_data * data, u_long * mem);
|
---|
52 |
|
---|
53 | int Write_Huffman_Data (FILE * f, huff_data * hd);
|
---|
54 |
|
---|
55 | int Read_Huffman_Data (FILE * f, huff_data * hd, u_long * mem, u_long * disk);
|
---|
56 |
|
---|
57 | int 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. */
|
---|
62 | double 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
|
---|