1 | //======================================================================== |
2 | // |
3 | // JBIG2Stream.h |
4 | // |
5 | // Copyright 2002-2003 Glyph & Cog, LLC |
6 | // |
7 | //======================================================================== |
8 | |
9 | //======================================================================== |
10 | // |
11 | // Modified under the Poppler project - http://poppler.freedesktop.org |
12 | // |
13 | // All changes made under the Poppler project to this file are licensed |
14 | // under GPL version 2 or later |
15 | // |
16 | // Copyright (C) 2009 David Benjamin <davidben@mit.edu> |
17 | // Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com> |
18 | // Copyright (C) 2015 Suzuki Toshiya <mpsuzuki@hiroshima-u.ac.jp> |
19 | // Copyright (C) 2019-2021 Oliver Sander <oliver.sander@tu-dresden.de> |
20 | // Copyright (C) 2019 Volker Krause <vkrause@kde.org> |
21 | // Copyright (C) 2019, 2021 Albert Astals Cid <aacid@kde.org> |
22 | // Copyright (C) 2019, 2020 Even Rouault <even.rouault@spatialys.com> |
23 | // |
24 | // To see a description of the changes please see the Changelog file that |
25 | // came with your tarball or type make ChangeLog if you are building from git |
26 | // |
27 | //======================================================================== |
28 | |
29 | #ifndef JBIG2STREAM_H |
30 | #define JBIG2STREAM_H |
31 | |
32 | #include "Object.h" |
33 | #include "Stream.h" |
34 | |
35 | class JBIG2Segment; |
36 | class JBIG2Bitmap; |
37 | class JArithmeticDecoder; |
38 | class JArithmeticDecoderStats; |
39 | class JBIG2HuffmanDecoder; |
40 | struct JBIG2HuffmanTable; |
41 | class JBIG2MMRDecoder; |
42 | |
43 | //------------------------------------------------------------------------ |
44 | |
45 | class JBIG2Stream : public FilterStream |
46 | { |
47 | public: |
48 | JBIG2Stream(Stream *strA, Object &&globalsStreamA, Object *globalsStreamRefA); |
49 | ~JBIG2Stream() override; |
50 | StreamKind getKind() const override { return strJBIG2; } |
51 | void reset() override; |
52 | void close() override; |
53 | Goffset getPos() override; |
54 | int getChar() override; |
55 | int lookChar() override; |
56 | GooString *getPSFilter(int psLevel, const char *indent) override; |
57 | bool isBinary(bool last = true) const override; |
58 | virtual Object *getGlobalsStream() { return &globalsStream; } |
59 | virtual Ref getGlobalsStreamRef() { return globalsStreamRef; } |
60 | |
61 | private: |
62 | bool hasGetChars() override { return true; } |
63 | int getChars(int nChars, unsigned char *buffer) override; |
64 | |
65 | void readSegments(); |
66 | bool readSymbolDictSeg(unsigned int segNum, unsigned int length, unsigned int *refSegs, unsigned int nRefSegs); |
67 | void readTextRegionSeg(unsigned int segNum, bool imm, bool lossless, unsigned int length, unsigned int *refSegs, unsigned int nRefSegs); |
68 | std::unique_ptr<JBIG2Bitmap> readTextRegion(bool huff, bool refine, int w, int h, unsigned int numInstances, unsigned int logStrips, int numSyms, const JBIG2HuffmanTable *symCodeTab, unsigned int symCodeLen, JBIG2Bitmap **syms, |
69 | unsigned int defPixel, unsigned int combOp, unsigned int transposed, unsigned int refCorner, int sOffset, const JBIG2HuffmanTable *huffFSTable, const JBIG2HuffmanTable *huffDSTable, |
70 | const JBIG2HuffmanTable *huffDTTable, const JBIG2HuffmanTable *huffRDWTable, const JBIG2HuffmanTable *huffRDHTable, const JBIG2HuffmanTable *huffRDXTable, |
71 | const JBIG2HuffmanTable *huffRDYTable, const JBIG2HuffmanTable *huffRSizeTable, unsigned int templ, int *atx, int *aty); |
72 | void readPatternDictSeg(unsigned int segNum, unsigned int length); |
73 | void readHalftoneRegionSeg(unsigned int segNum, bool imm, bool lossless, unsigned int length, unsigned int *refSegs, unsigned int nRefSegs); |
74 | void readGenericRegionSeg(unsigned int segNum, bool imm, bool lossless, unsigned int length); |
75 | void mmrAddPixels(int a1, int blackPixels, int *codingLine, int *a0i, int w); |
76 | void mmrAddPixelsNeg(int a1, int blackPixels, int *codingLine, int *a0i, int w); |
77 | std::unique_ptr<JBIG2Bitmap> readGenericBitmap(bool mmr, int w, int h, int templ, bool tpgdOn, bool useSkip, JBIG2Bitmap *skip, int *atx, int *aty, int mmrDataLength); |
78 | void readGenericRefinementRegionSeg(unsigned int segNum, bool imm, bool lossless, unsigned int length, unsigned int *refSegs, unsigned int nRefSegs); |
79 | std::unique_ptr<JBIG2Bitmap> readGenericRefinementRegion(int w, int h, int templ, bool tpgrOn, JBIG2Bitmap *refBitmap, int refDX, int refDY, int *atx, int *aty); |
80 | void readPageInfoSeg(unsigned int length); |
81 | void readEndOfStripeSeg(unsigned int length); |
82 | void readProfilesSeg(unsigned int length); |
83 | void readCodeTableSeg(unsigned int segNum, unsigned int length); |
84 | void readExtensionSeg(unsigned int length); |
85 | JBIG2Segment *findSegment(unsigned int segNum); |
86 | void discardSegment(unsigned int segNum); |
87 | void resetGenericStats(unsigned int templ, JArithmeticDecoderStats *prevStats); |
88 | void resetRefinementStats(unsigned int templ, JArithmeticDecoderStats *prevStats); |
89 | bool resetIntStats(int symCodeLen); |
90 | bool readUByte(unsigned int *x); |
91 | bool readByte(int *x); |
92 | bool readUWord(unsigned int *x); |
93 | bool readULong(unsigned int *x); |
94 | bool readLong(int *x); |
95 | |
96 | Object globalsStream; |
97 | Ref globalsStreamRef; |
98 | unsigned int pageW, pageH, curPageH; |
99 | unsigned int pageDefPixel; |
100 | JBIG2Bitmap *pageBitmap; |
101 | unsigned int defCombOp; |
102 | std::vector<std::unique_ptr<JBIG2Segment>> segments; |
103 | std::vector<std::unique_ptr<JBIG2Segment>> globalSegments; |
104 | Stream *curStr; |
105 | unsigned char *dataPtr; |
106 | unsigned char *dataEnd; |
107 | unsigned int byteCounter; |
108 | |
109 | JArithmeticDecoder *arithDecoder; |
110 | JArithmeticDecoderStats *genericRegionStats; |
111 | JArithmeticDecoderStats *refinementRegionStats; |
112 | JArithmeticDecoderStats *iadhStats; |
113 | JArithmeticDecoderStats *iadwStats; |
114 | JArithmeticDecoderStats *iaexStats; |
115 | JArithmeticDecoderStats *iaaiStats; |
116 | JArithmeticDecoderStats *iadtStats; |
117 | JArithmeticDecoderStats *iaitStats; |
118 | JArithmeticDecoderStats *iafsStats; |
119 | JArithmeticDecoderStats *iadsStats; |
120 | JArithmeticDecoderStats *iardxStats; |
121 | JArithmeticDecoderStats *iardyStats; |
122 | JArithmeticDecoderStats *iardwStats; |
123 | JArithmeticDecoderStats *iardhStats; |
124 | JArithmeticDecoderStats *iariStats; |
125 | JArithmeticDecoderStats *iaidStats; |
126 | JBIG2HuffmanDecoder *huffDecoder; |
127 | JBIG2MMRDecoder *mmrDecoder; |
128 | }; |
129 | |
130 | #endif |
131 | |