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 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
30class Stream;
31
32//------------------------------------------------------------------------
33// JArithmeticDecoderStats
34//------------------------------------------------------------------------
35
36class JArithmeticDecoderStats
37{
38public:
39 explicit JArithmeticDecoderStats(int contextSizeA);
40 ~JArithmeticDecoderStats();
41 JArithmeticDecoderStats(const JArithmeticDecoderStats &) = delete;
42 JArithmeticDecoderStats &operator=(const JArithmeticDecoderStats &) = delete;
43 JArithmeticDecoderStats *copy();
44 void reset();
45 int getContextSize() { return contextSize; }
46 void copyFrom(JArithmeticDecoderStats *stats);
47 void setEntry(unsigned int cx, int i, int mps);
48 bool isValid() const { return cxTab != nullptr; }
49
50private:
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
61class JArithmeticDecoder
62{
63public:
64 JArithmeticDecoder();
65 ~JArithmeticDecoder();
66 JArithmeticDecoder(const JArithmeticDecoder &) = delete;
67 JArithmeticDecoder &operator=(const JArithmeticDecoder &) = delete;
68
69 void setStream(Stream *strA)
70 {
71 str = strA;
72 dataLen = 0;
73 limitStream = false;
74 }
75 void setStream(Stream *strA, int dataLenA)
76 {
77 str = strA;
78 dataLen = dataLenA;
79 limitStream = true;
80 }
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 void resetByteCounter() { nBytesRead = 0; }
107 unsigned int getByteCounter() { return nBytesRead; }
108
109private:
110 unsigned int readByte();
111 int decodeIntBit(JArithmeticDecoderStats *stats);
112 void byteIn();
113
114 static const unsigned int qeTab[47];
115 static const int nmpsTab[47];
116 static const int nlpsTab[47];
117 static const int switchTab[47];
118
119 unsigned int buf0, buf1;
120 unsigned int c, a;
121 int ct;
122
123 unsigned int prev; // for the integer decoder
124
125 Stream *str;
126 unsigned int nBytesRead;
127 int dataLen;
128 bool limitStream;
129};
130
131#endif
132

source code of poppler/poppler/JArithmeticDecoder.h