/src/poppler/poppler/JArithmeticDecoder.h
Line | Count | Source |
1 | | //======================================================================== |
2 | | // |
3 | | // JArithmeticDecoder.h |
4 | | // |
5 | | // Arithmetic decoder used by the JBIG2 and JPEG2000 decoders. |
6 | | // |
7 | | // Copyright 2002-2004 Glyph & Cog, LLC |
8 | | // |
9 | | //======================================================================== |
10 | | |
11 | | //======================================================================== |
12 | | // |
13 | | // Modified under the Poppler project - http://poppler.freedesktop.org |
14 | | // |
15 | | // All changes made under the Poppler project to this file are licensed |
16 | | // under GPL version 2 or later |
17 | | // |
18 | | // Copyright (C) 2018, 2021, 2026 Albert Astals Cid <aacid@kde.org> |
19 | | // Copyright (C) 2019 Volker Krause <vkrause@kde.org> |
20 | | // Copyright (C) 2020 Even Rouault <even.rouault@spatialys.com> |
21 | | // |
22 | | // To see a description of the changes please see the Changelog file that |
23 | | // came with your tarball or type make ChangeLog if you are building from git |
24 | | // |
25 | | //======================================================================== |
26 | | |
27 | | #ifndef JARITHMETICDECODER_H |
28 | | #define JARITHMETICDECODER_H |
29 | | |
30 | | class Stream; |
31 | | |
32 | | //------------------------------------------------------------------------ |
33 | | // JArithmeticDecoderStats |
34 | | //------------------------------------------------------------------------ |
35 | | |
36 | | class JArithmeticDecoderStats |
37 | | { |
38 | | public: |
39 | | explicit JArithmeticDecoderStats(int contextSizeA); |
40 | | ~JArithmeticDecoderStats(); |
41 | | JArithmeticDecoderStats(const JArithmeticDecoderStats &) = delete; |
42 | | JArithmeticDecoderStats &operator=(const JArithmeticDecoderStats &) = delete; |
43 | | std::unique_ptr<JArithmeticDecoderStats> copy() const; |
44 | | void resetContext(); |
45 | 0 | int getContextSize() const { return contextSize; } |
46 | | void copyFrom(const JArithmeticDecoderStats &stats); |
47 | | void setEntry(unsigned int cx, int i, int mps); |
48 | 0 | bool isValid() const { return cxTab != nullptr; } |
49 | | |
50 | | private: |
51 | | unsigned char *cxTab; // cxTab[cx] = (i[cx] << 1) + mps[cx] |
52 | | int contextSize; |
53 | | |
54 | | friend class JArithmeticDecoder; |
55 | | }; |
56 | | |
57 | | //------------------------------------------------------------------------ |
58 | | // JArithmeticDecoder |
59 | | //------------------------------------------------------------------------ |
60 | | |
61 | | class JArithmeticDecoder |
62 | | { |
63 | | public: |
64 | | JArithmeticDecoder(); |
65 | | ~JArithmeticDecoder(); |
66 | | JArithmeticDecoder(const JArithmeticDecoder &) = delete; |
67 | | JArithmeticDecoder &operator=(const JArithmeticDecoder &) = delete; |
68 | | |
69 | | void setStream(Stream *strA) |
70 | 18 | { |
71 | 18 | str = strA; |
72 | 18 | dataLen = 0; |
73 | 18 | limitStream = false; |
74 | 18 | } |
75 | | void setStream(Stream *strA, int dataLenA) |
76 | 0 | { |
77 | 0 | str = strA; |
78 | 0 | dataLen = dataLenA; |
79 | 0 | limitStream = true; |
80 | 0 | } |
81 | | |
82 | | // Start decoding on a new stream. This fills the byte buffers and |
83 | | // runs INITDEC. |
84 | | void start(); |
85 | | |
86 | | // Restart decoding on an interrupted stream. This refills the |
87 | | // buffers if needed, but does not run INITDEC. (This is used in |
88 | | // JPEG 2000 streams when codeblock data is split across multiple |
89 | | // packets/layers.) |
90 | | void restart(int dataLenA); |
91 | | |
92 | | // Read any leftover data in the stream. |
93 | | void cleanup(); |
94 | | |
95 | | // Decode one bit. |
96 | | int decodeBit(unsigned int context, JArithmeticDecoderStats *stats); |
97 | | |
98 | | // Decode eight bits. |
99 | | int decodeByte(unsigned int context, JArithmeticDecoderStats *stats); |
100 | | |
101 | | // Returns false for OOB, otherwise sets *<x> and returns true. |
102 | | bool decodeInt(int *x, JArithmeticDecoderStats *stats); |
103 | | |
104 | | unsigned int decodeIAID(unsigned int codeLen, JArithmeticDecoderStats *stats); |
105 | | |
106 | 15 | void resetByteCounter() { nBytesRead = 0; } |
107 | 0 | unsigned int getByteCounter() const { return nBytesRead; } |
108 | | |
109 | 0 | bool getReadPastEndOfStream() const { return readPastEndOfStream; } |
110 | | |
111 | | private: |
112 | | unsigned int readByte(); |
113 | | int decodeIntBit(JArithmeticDecoderStats *stats); |
114 | | void byteIn(); |
115 | | |
116 | | static const unsigned int qeTab[47]; |
117 | | static const int nmpsTab[47]; |
118 | | static const int nlpsTab[47]; |
119 | | static const int switchTab[47]; |
120 | | |
121 | | unsigned int buf0, buf1; |
122 | | unsigned int c, a; |
123 | | int ct; |
124 | | |
125 | | unsigned int prev; // for the integer decoder |
126 | | |
127 | | Stream *str; |
128 | | unsigned int nBytesRead; |
129 | | int dataLen; |
130 | | bool limitStream; |
131 | | bool readPastEndOfStream = false; |
132 | | }; |
133 | | |
134 | | #endif |