1 | /* Copyright (C) 2001-2019 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., 1305 Grant Avenue - Suite 200, Novato, |
13 | CA 94945, U.S.A., +1(415)492-9861, for further information. |
14 | */ |
15 | |
16 | /* |
17 | jbig2dec |
18 | */ |
19 | |
20 | #ifndef _JBIG2_HUFFMAN_H |
21 | #define _JBIG2_HUFFMAN_H |
22 | |
23 | /* Huffman coder interface */ |
24 | |
25 | typedef struct _Jbig2HuffmanEntry Jbig2HuffmanEntry; |
26 | typedef struct _Jbig2HuffmanState Jbig2HuffmanState; |
27 | typedef struct _Jbig2HuffmanTable Jbig2HuffmanTable; |
28 | typedef struct _Jbig2HuffmanParams Jbig2HuffmanParams; |
29 | |
30 | struct _Jbig2HuffmanEntry { |
31 | union { |
32 | int32_t RANGELOW; |
33 | Jbig2HuffmanTable *ext_table; |
34 | } u; |
35 | byte PREFLEN; |
36 | byte RANGELEN; |
37 | byte flags; |
38 | }; |
39 | |
40 | struct _Jbig2HuffmanTable { |
41 | int log_table_size; |
42 | Jbig2HuffmanEntry *entries; |
43 | }; |
44 | |
45 | typedef struct _Jbig2HuffmanLine Jbig2HuffmanLine; |
46 | |
47 | struct _Jbig2HuffmanLine { |
48 | int PREFLEN; |
49 | int RANGELEN; |
50 | int RANGELOW; |
51 | }; |
52 | |
53 | struct _Jbig2HuffmanParams { |
54 | bool HTOOB; |
55 | int n_lines; |
56 | const Jbig2HuffmanLine *lines; |
57 | }; |
58 | |
59 | Jbig2HuffmanState *jbig2_huffman_new(Jbig2Ctx *ctx, Jbig2WordStream *ws); |
60 | |
61 | void jbig2_huffman_free(Jbig2Ctx *ctx, Jbig2HuffmanState *hs); |
62 | |
63 | int jbig2_huffman_skip(Jbig2HuffmanState *hs); |
64 | |
65 | int jbig2_huffman_advance(Jbig2HuffmanState *hs, size_t advance); |
66 | |
67 | uint32_t jbig2_huffman_offset(Jbig2HuffmanState *hs); |
68 | |
69 | int32_t jbig2_huffman_get(Jbig2HuffmanState *hs, const Jbig2HuffmanTable *table, bool *oob); |
70 | |
71 | int32_t jbig2_huffman_get_bits(Jbig2HuffmanState *hs, const int bits, int *err); |
72 | |
73 | #ifdef JBIG2_DEBUG |
74 | void jbig2_dump_huffman_state(Jbig2HuffmanState *hs); |
75 | void jbig2_dump_huffman_binary(Jbig2HuffmanState *hs); |
76 | #endif |
77 | |
78 | Jbig2HuffmanTable *jbig2_build_huffman_table(Jbig2Ctx *ctx, const Jbig2HuffmanParams *params); |
79 | |
80 | void jbig2_release_huffman_table(Jbig2Ctx *ctx, Jbig2HuffmanTable *table); |
81 | |
82 | /* standard Huffman templates defined by the specification */ |
83 | extern const Jbig2HuffmanParams jbig2_huffman_params_A; /* Table B.1 */ |
84 | extern const Jbig2HuffmanParams jbig2_huffman_params_B; /* Table B.2 */ |
85 | extern const Jbig2HuffmanParams jbig2_huffman_params_C; /* Table B.3 */ |
86 | extern const Jbig2HuffmanParams jbig2_huffman_params_D; /* Table B.4 */ |
87 | extern const Jbig2HuffmanParams jbig2_huffman_params_E; /* Table B.5 */ |
88 | extern const Jbig2HuffmanParams jbig2_huffman_params_F; /* Table B.6 */ |
89 | extern const Jbig2HuffmanParams jbig2_huffman_params_G; /* Table B.7 */ |
90 | extern const Jbig2HuffmanParams jbig2_huffman_params_H; /* Table B.8 */ |
91 | extern const Jbig2HuffmanParams jbig2_huffman_params_I; /* Table B.9 */ |
92 | extern const Jbig2HuffmanParams jbig2_huffman_params_J; /* Table B.10 */ |
93 | extern const Jbig2HuffmanParams jbig2_huffman_params_K; /* Table B.11 */ |
94 | extern const Jbig2HuffmanParams jbig2_huffman_params_L; /* Table B.12 */ |
95 | extern const Jbig2HuffmanParams jbig2_huffman_params_M; /* Table B.13 */ |
96 | extern const Jbig2HuffmanParams jbig2_huffman_params_N; /* Table B.14 */ |
97 | extern const Jbig2HuffmanParams jbig2_huffman_params_O; /* Table B.15 */ |
98 | |
99 | /* Routines to handle "code table segment (53)" */ |
100 | |
101 | /* Parse a code table segment, store Jbig2HuffmanParams in segment->result */ |
102 | int jbig2_table(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data); |
103 | |
104 | /* free Jbig2HuffmanParams allocated by jbig2_huffman_table() */ |
105 | void jbig2_table_free(Jbig2Ctx *ctx, Jbig2HuffmanParams *params); |
106 | |
107 | /* find a user supplied table used by 'segment' and by 'index' */ |
108 | const Jbig2HuffmanParams *jbig2_find_table(Jbig2Ctx *ctx, Jbig2Segment *segment, int index); |
109 | |
110 | #endif /* _JBIG2_HUFFMAN_H */ |
111 | |