source: gsdl/trunk/trunk/mg/lib/huffman.h@ 16583

Last change on this file since 16583 was 16583, checked in by davidb, 16 years ago

Undoing change commited in r16582

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