Debian Codegraph @

src/jb2/zp.h

raw
1 /*
2 * zp.h - ZP-coder from DjVuLibre, an adaptive arithmetical binary coder
3 */
4
5 /* The DjVuLibre code is a bit reorganized, but the algorithms are the same.
6 * Z-coder was stripped.
7 */
8
9
10 #ifndef ZP_H
11 #define ZP_H
12
13
14 #include <minidjvu/minidjvu.h>
15 #include <stdio.h>
16 #include <assert.h>
17
18 typedef int Bit;
19
20
21 /* ZPBitContext is an adaptation variable.
22 * A ZP-coder works together with a context,
23 * and both are changed in the process,
24 * but the context is detachable.
25 */
26
27 class ZPBitContext
28 {
29 public:
30 ZPBitContext();
31 private:
32 unsigned char value;
33
34 friend class ZPNumContext;
35 friend class ZPEncoder;
36 friend class ZPDecoder;
37
38 public:
39 inline Bit get_more_probable_bit() {return value & 1;}
40 inline void assign(ZPBitContext &c) {value = c.value;}
41 };
42
43 inline ZPBitContext::ZPBitContext(): value(0) {}
44
45
46 /* ZPMemoryWatcher is a purely abstract class.
47 * Its only goal is to receive notifications
48 * when a ZPNumContext has allocated a bit context.
49 * This unit contains no implementation of ZPMemoryWatcher.
50 */
51 class ZPMemoryWatcher
52 {
53 public:
54 virtual void handle_allocation() = 0;
55 virtual ~ZPMemoryWatcher();
56 };
57
58
59 /* ZPNumContext is a tree of ZPBitContexts.
60 * Its nodes are accessed by indices.
61 * Root node always has the index of 0.
62 *
63 * Nodes are created automatically.
64 */
65 class ZPNumContext
66 {
67 public:
68 ZPNumContext(int32 amin, int32 amax, ZPMemoryWatcher *w = NULL);
69 ~ZPNumContext();
70 void set_interval(int32 new_min, int32 new_max);
71 void reset();
72 private:
73 int32 min, max;
74 ZPMemoryWatcher *watcher;
75 ZPBitContext *nodes;
76 uint16 n;
77 uint16 allocated;
78 uint16 *left, *right; // 0 means nil
79 uint16 new_node();
80 uint16 get_left(uint16);
81 uint16 get_right(uint16);
82 void init();
83 friend class ZPEncoder;
84 friend class ZPDecoder;
85 };
86
87
88 class ZPEncoder
89 {
90 public:
91 ZPEncoder(FILE *); // does not close it on destruction
92 virtual ~ZPEncoder();
93 void encode_without_context(Bit);
94 void encode(Bit, ZPBitContext &);
95 void encode(int32, ZPNumContext &);
96 void close();
97
98 private:
99 FILE *file;
100 void emit_byte(unsigned char);
101 uint32 a, nrun, subend, buffer;
102 unsigned char delay, byte, scount;
103 bool closed;
104
105 void outbit(Bit);
106 void zemit(Bit);
107 void export_bits();
108 void encode_mps(ZPBitContext &, uint32);
109 void encode_lps(ZPBitContext &, uint32);
110 void encode_mps_simple(uint32);
111 void encode_lps_simple(uint32);
112 };
113
114
115 class ZPDecoder
116 {
117 public:
118 ZPDecoder(FILE *, int32 length); // does not close it on destruction
119 Bit decode_without_context();
120 Bit decode(ZPBitContext &);
121 int32 decode(ZPNumContext &);
122 private:
123 FILE *file;
124 uint32 a, code, fence, buffer;
125 int32 bytes_left;
126 unsigned char byte, scount, delay;
127 bool next_byte(unsigned char &);
128 void open();
129 void preload();
130 int32 ffz(uint32);
131 Bit decode_sub(ZPBitContext &, uint32);
132 Bit decode_sub_simple(uint32);
133 };
134
135
136 #endif