Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-10 10:23:47

0001 //========================================================================
0002 //
0003 // JArithmeticDecoder.h
0004 //
0005 // Arithmetic decoder used by the JBIG2 and JPEG2000 decoders.
0006 //
0007 // Copyright 2002-2004 Glyph & Cog, LLC
0008 //
0009 //========================================================================
0010 
0011 //========================================================================
0012 //
0013 // Modified under the Poppler project - http://poppler.freedesktop.org
0014 //
0015 // All changes made under the Poppler project to this file are licensed
0016 // under GPL version 2 or later
0017 //
0018 // Copyright (C) 2018, 2021 Albert Astals Cid <aacid@kde.org>
0019 // Copyright (C) 2019 Volker Krause <vkrause@kde.org>
0020 // Copyright (C) 2020 Even Rouault <even.rouault@spatialys.com>
0021 //
0022 // To see a description of the changes please see the Changelog file that
0023 // came with your tarball or type make ChangeLog if you are building from git
0024 //
0025 //========================================================================
0026 
0027 #ifndef JARITHMETICDECODER_H
0028 #define JARITHMETICDECODER_H
0029 
0030 class Stream;
0031 
0032 //------------------------------------------------------------------------
0033 // JArithmeticDecoderStats
0034 //------------------------------------------------------------------------
0035 
0036 class JArithmeticDecoderStats
0037 {
0038 public:
0039     explicit JArithmeticDecoderStats(int contextSizeA);
0040     ~JArithmeticDecoderStats();
0041     JArithmeticDecoderStats(const JArithmeticDecoderStats &) = delete;
0042     JArithmeticDecoderStats &operator=(const JArithmeticDecoderStats &) = delete;
0043     JArithmeticDecoderStats *copy();
0044     void reset();
0045     int getContextSize() { return contextSize; }
0046     void copyFrom(JArithmeticDecoderStats *stats);
0047     void setEntry(unsigned int cx, int i, int mps);
0048     bool isValid() const { return cxTab != nullptr; }
0049 
0050 private:
0051     unsigned char *cxTab; // cxTab[cx] = (i[cx] << 1) + mps[cx]
0052     int contextSize;
0053 
0054     friend class JArithmeticDecoder;
0055 };
0056 
0057 //------------------------------------------------------------------------
0058 // JArithmeticDecoder
0059 //------------------------------------------------------------------------
0060 
0061 class JArithmeticDecoder
0062 {
0063 public:
0064     JArithmeticDecoder();
0065     ~JArithmeticDecoder();
0066     JArithmeticDecoder(const JArithmeticDecoder &) = delete;
0067     JArithmeticDecoder &operator=(const JArithmeticDecoder &) = delete;
0068 
0069     void setStream(Stream *strA)
0070     {
0071         str = strA;
0072         dataLen = 0;
0073         limitStream = false;
0074     }
0075     void setStream(Stream *strA, int dataLenA)
0076     {
0077         str = strA;
0078         dataLen = dataLenA;
0079         limitStream = true;
0080     }
0081 
0082     // Start decoding on a new stream.  This fills the byte buffers and
0083     // runs INITDEC.
0084     void start();
0085 
0086     // Restart decoding on an interrupted stream.  This refills the
0087     // buffers if needed, but does not run INITDEC.  (This is used in
0088     // JPEG 2000 streams when codeblock data is split across multiple
0089     // packets/layers.)
0090     void restart(int dataLenA);
0091 
0092     // Read any leftover data in the stream.
0093     void cleanup();
0094 
0095     // Decode one bit.
0096     int decodeBit(unsigned int context, JArithmeticDecoderStats *stats);
0097 
0098     // Decode eight bits.
0099     int decodeByte(unsigned int context, JArithmeticDecoderStats *stats);
0100 
0101     // Returns false for OOB, otherwise sets *<x> and returns true.
0102     bool decodeInt(int *x, JArithmeticDecoderStats *stats);
0103 
0104     unsigned int decodeIAID(unsigned int codeLen, JArithmeticDecoderStats *stats);
0105 
0106     void resetByteCounter() { nBytesRead = 0; }
0107     unsigned int getByteCounter() { return nBytesRead; }
0108 
0109 private:
0110     unsigned int readByte();
0111     int decodeIntBit(JArithmeticDecoderStats *stats);
0112     void byteIn();
0113 
0114     static const unsigned int qeTab[47];
0115     static const int nmpsTab[47];
0116     static const int nlpsTab[47];
0117     static const int switchTab[47];
0118 
0119     unsigned int buf0, buf1;
0120     unsigned int c, a;
0121     int ct;
0122 
0123     unsigned int prev; // for the integer decoder
0124 
0125     Stream *str;
0126     unsigned int nBytesRead;
0127     int dataLen;
0128     bool limitStream;
0129 };
0130 
0131 #endif