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