1 /* Copyright (C) 2001-2012 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
13    CA  94903, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* Definitions for BoundedHuffman filters */
18 /* Requires strimpl.h */
19 
20 #ifndef sbhc_INCLUDED
21 #  define sbhc_INCLUDED
22 
23 #include "shc.h"
24 
25 /*
26  * The BoundedHuffman filters extend the basic Huffman coding model by
27  * providing the ability to encode runs of zeros as a single data item,
28  * and by providing an end-of-data (EOD) marker.
29  */
30 #define max_zero_run 100
31 
32 /* Common state */
33 #define stream_BHC_state_common\
34         stream_hc_state_common;\
35         hc_definition definition;\
36                 /* The client sets the following before initialization. */\
37         bool EndOfData;\
38         uint EncodeZeroRuns;\
39                 /* The following are updated dynamically. */\
40         int zeros		/* # of zeros scanned or left to output */
41 typedef struct stream_BHC_state_s {
42     stream_BHC_state_common;
43 } stream_BHC_state;
44 
45 /* BoundedHuffmanEncode */
46 typedef struct stream_BHCE_state_s {
47     stream_BHC_state_common;
48     hce_table encode;
49 } stream_BHCE_state;
50 
51 #define private_st_BHCE_state()	/* in sbhc.c */\
52   gs_private_st_ptrs3(st_BHCE_state, stream_BHCE_state,\
53     "BoundedHuffmanEncode state", bhce_enum_ptrs, bhce_reloc_ptrs,\
54     definition.counts, definition.values, encode.codes)
55 extern const stream_template s_BHCE_template;
56 
57 #define s_bhce_init_inline(ss)\
58   (s_hce_init_inline(ss), (ss)->zeros = 0)
59 
60 /* BoundedHuffmanDecode */
61 typedef struct stream_BHCD_state_s {
62     stream_BHC_state_common;
63     hcd_table decode;
64 } stream_BHCD_state;
65 
66 #define private_st_BHCD_state()	/* in sbhc.c */\
67   gs_private_st_ptrs3(st_BHCD_state, stream_BHCD_state,\
68     "BoundedHuffmanDecode state", bhcd_enum_ptrs, bhcd_reloc_ptrs,\
69     definition.counts, definition.values, decode.codes)
70 extern const stream_template s_BHCD_template;
71 
72 #define s_bhcd_init_inline(ss)\
73   (s_hcd_init_inline(ss), (ss)->zeros = 0)
74 
75 /* Declare variables that hold the decoder state. */
76 #define bhcd_declare_state\
77         hcd_declare_state;\
78         int zeros
79 
80 /* Load the state from the stream. */
81 /* Free variables: pr, ss, p, rlimit, bits, bits_left, zeros. */
82 #define bhcd_load_state()\
83         hcd_load_state(), zeros = ss->zeros
84 
85 /* Store the state back in the stream. */
86 /* Free variables: pr, ss, p, bits, bits_left, zeros. */
87 #define bhcd_store_state()\
88         hcd_store_state(), ss->zeros = zeros
89 
90 #endif /* sbhc_INCLUDED */
91