Coverage Report

Created: 2025-07-12 06:54

/src/xpdf-4.05/xpdf/JPXStream.h
Line
Count
Source (jump to first uncovered line)
1
//========================================================================
2
//
3
// JPXStream.h
4
//
5
// Copyright 2002-2003 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#ifndef JPXSTREAM_H
10
#define JPXSTREAM_H
11
12
#include <aconf.h>
13
14
#include "gtypes.h"
15
#include "Object.h"
16
#include "Stream.h"
17
18
class JArithmeticDecoder;
19
class JArithmeticDecoderStats;
20
21
//------------------------------------------------------------------------
22
23
enum JPXColorSpaceType {
24
  jpxCSBiLevel = 0,
25
  jpxCSYCbCr1 = 1,
26
  jpxCSYCbCr2 = 3,
27
  jpxCSYCBCr3 = 4,
28
  jpxCSPhotoYCC = 9,
29
  jpxCSCMY = 11,
30
  jpxCSCMYK = 12,
31
  jpxCSYCCK = 13,
32
  jpxCSCIELab = 14,
33
  jpxCSsRGB = 16,
34
  jpxCSGrayscale = 17,
35
  jpxCSBiLevel2 = 18,
36
  jpxCSCIEJab = 19,
37
  jpxCSCISesRGB = 20,
38
  jpxCSROMMRGB = 21,
39
  jpxCSsRGBYCbCr = 22,
40
  jpxCSYPbPr1125 = 23,
41
  jpxCSYPbPr1250 = 24
42
};
43
44
struct JPXColorSpecCIELab {
45
  Guint rl, ol, ra, oa, rb, ob, il;
46
};
47
48
struct JPXColorSpecEnumerated {
49
  JPXColorSpaceType type; // color space type
50
  union {
51
    JPXColorSpecCIELab cieLab;
52
  };
53
};
54
55
struct JPXColorSpec {
56
  Guint meth;     // method
57
  int prec;     // precedence
58
  union {
59
    JPXColorSpecEnumerated enumerated;
60
  };
61
};
62
63
//------------------------------------------------------------------------
64
65
struct JPXPalette {
66
  Guint nEntries;   // number of entries in the palette
67
  Guint nComps;     // number of components in each entry
68
  Guint *bpc;     // bits per component, for each component
69
  int *c;     // color data:
70
        //   c[i*nComps+j] = entry i, component j
71
};
72
73
//------------------------------------------------------------------------
74
75
struct JPXCompMap {
76
  Guint nChannels;    // number of channels
77
  Guint *comp;      // codestream components mapped to each channel
78
  Guint *type;      // 0 for direct use, 1 for palette mapping
79
  Guint *pComp;     // palette components to use
80
};
81
82
//------------------------------------------------------------------------
83
84
struct JPXChannelDefn {
85
  Guint nChannels;    // number of channels
86
  Guint *idx;     // channel indexes
87
  Guint *type;      // channel types
88
  Guint *assoc;     // channel associations
89
};
90
91
//------------------------------------------------------------------------
92
93
struct JPXTagTreeNode {
94
  GBool finished;   // true if this node is finished
95
  Guint val;      // current value
96
};
97
98
//------------------------------------------------------------------------
99
100
struct JPXCodeBlock {
101
  //----- size
102
  Guint x0, y0, x1, y1;   // bounds
103
104
  //----- persistent state
105
  GBool seen;     // true if this code-block has already
106
        //   been seen
107
  Guint lBlock;     // base number of bits used for pkt data length
108
  Guint nextPass;   // next coding pass
109
110
  //---- info from first packet
111
  Guint nZeroBitPlanes;   // number of zero bit planes
112
113
  //----- info for the current packet
114
  Guint included;   // code-block inclusion in this packet:
115
        //   0=not included, 1=included
116
  Guint nCodingPasses;    // number of coding passes in this pkt
117
  Guint *dataLen;   // data lengths (one per codeword segment)
118
  Guint dataLenSize;    // size of the dataLen array
119
120
  //----- coefficient data
121
  int *coeffs;
122
  char *touched;    // coefficient 'touched' flags
123
  Gushort len;      // coefficient length
124
  JArithmeticDecoder    // arithmetic decoder
125
    *arithDecoder;
126
  JArithmeticDecoderStats // arithmetic decoder stats
127
    *stats;
128
};
129
130
//------------------------------------------------------------------------
131
132
struct JPXSubband {
133
  //----- computed
134
  Guint nXCBs, nYCBs;   // number of code-blocks in the x and y
135
        //   directions
136
137
  //----- tag trees
138
  Guint maxTTLevel;   // max tag tree level
139
  JPXTagTreeNode *inclusion;  // inclusion tag tree for each subband
140
  JPXTagTreeNode *zeroBitPlane; // zero-bit plane tag tree for each
141
        //   subband
142
143
  //----- children
144
  JPXCodeBlock *cbs;    // the code-blocks (len = nXCBs * nYCBs)
145
};
146
147
//------------------------------------------------------------------------
148
149
struct JPXPrecinct {
150
  //----- children
151
  JPXSubband *subbands;   // the subbands
152
};
153
154
//------------------------------------------------------------------------
155
156
struct JPXResLevel {
157
  //----- from the COD and COC segments (main and tile)
158
  Guint precinctWidth;    // log2(precinct width)
159
  Guint precinctHeight;   // log2(precinct height)
160
  Guint nPrecincts;
161
162
  //----- computed
163
  Guint x0, y0, x1, y1;   // bounds of this tile-comp at this res level
164
  Guint bx0[3], by0[3],   // subband bounds
165
        bx1[3], by1[3];
166
  Guint codeBlockW;   // log2(code-block width)
167
  Guint codeBlockH;   // log2(code-block height)
168
  Guint cbW;      // code-block width
169
  Guint cbH;      // code-block height
170
  GBool empty;      // true if all subbands and precincts are
171
        //   zero width or height
172
173
  //---- children
174
  JPXPrecinct *precincts; // the precincts
175
};
176
177
//------------------------------------------------------------------------
178
179
struct JPXTileComp {
180
  //----- from the SIZ segment
181
  GBool sgned;      // 1 for signed, 0 for unsigned
182
  Guint prec;     // precision, in bits
183
  Guint hSep;     // horizontal separation of samples
184
  Guint vSep;     // vertical separation of samples
185
186
  //----- from the COD and COC segments (main and tile)
187
  Guint style;      // coding style parameter (Scod / Scoc)
188
  Guint nDecompLevels;    // number of decomposition levels
189
  Guint codeBlockW;   // log2(code-block width)
190
  Guint codeBlockH;   // log2(code-block height)
191
  Guint codeBlockStyle;   // code-block style
192
  Guint transform;    // wavelet transformation
193
194
  //----- from the QCD and QCC segments (main and tile)
195
  Guint quantStyle;   // quantization style
196
  Guint *quantSteps;    // quantization step size for each subband
197
  Guint nQuantSteps;    // number of entries in quantSteps
198
199
  //----- computed
200
  Guint x0, y0, x1, y1;   // bounds of the tile-comp, in ref coords
201
  Guint x0r, y0r;   // x0 >> reduction, y0 >> reduction
202
  Guint w, h;     // data size = {x1 - x0, y1 - y0} >> reduction
203
204
  //----- image data
205
  int *data;      // the decoded image data
206
  int *buf;     // intermediate buffer for the inverse
207
        //   transform
208
209
  //----- children
210
  JPXResLevel *resLevels; // the resolution levels
211
        //   (len = nDecompLevels + 1)
212
};
213
214
//------------------------------------------------------------------------
215
216
struct JPXTile {
217
  GBool init;
218
219
  //----- from the COD segments (main and tile)
220
  Guint progOrder;    // progression order
221
  Guint nLayers;    // number of layers
222
  Guint multiComp;    // multiple component transformation
223
224
  //----- computed
225
  Guint x0, y0, x1, y1;   // bounds of the tile, in ref coords
226
  Guint maxNDecompLevels; // max number of decomposition levels used
227
        //   in any component in this tile
228
  Guint maxNPrecincts;    // max number of precints in any
229
        //   component/res level in this tile
230
231
  //----- progression order loop counters
232
  Guint comp;     // component
233
  Guint res;      // resolution level
234
  Guint precinct;   // precinct
235
  Guint layer;      // layer
236
  GBool done;     // set when this tile is done
237
238
  //----- tile part info
239
  Guint nextTilePart;   // next expected tile-part
240
241
  //----- children
242
  JPXTileComp *tileComps; // the tile-components (len = JPXImage.nComps)
243
};
244
245
//------------------------------------------------------------------------
246
247
struct JPXImage {
248
  //----- from the SIZ segment
249
  Guint xSize, ySize;   // size of reference grid
250
  Guint xOffset, yOffset; // image offset
251
  Guint xTileSize, yTileSize; // size of tiles
252
  Guint xTileOffset,    // offset of first tile
253
        yTileOffset;
254
  Guint xSizeR, ySizeR;   // size of reference grid >> reduction
255
  Guint xOffsetR, yOffsetR; // image offset >> reduction
256
  Guint nComps;     // number of components
257
258
  //----- computed
259
  Guint nXTiles;    // number of tiles in x direction
260
  Guint nYTiles;    // number of tiles in y direction
261
262
  //----- children
263
  JPXTile *tiles;   // the tiles (len = nXTiles * nYTiles)
264
};
265
266
//------------------------------------------------------------------------
267
268
enum JPXDecodeResult {
269
  jpxDecodeOk,
270
  jpxDecodeNonFatalError,
271
  jpxDecodeFatalError
272
};
273
274
//------------------------------------------------------------------------
275
276
class JPXStream: public FilterStream {
277
public:
278
279
  JPXStream(Stream *strA);
280
  virtual ~JPXStream();
281
  virtual Stream *copy();
282
0
  virtual StreamKind getKind() { return strJPX; }
283
  virtual void reset();
284
  virtual void close();
285
  virtual int getChar();
286
  virtual int lookChar();
287
  virtual GString *getPSFilter(int psLevel, const char *indent,
288
             GBool okToReadStream);
289
  virtual GBool isBinary(GBool last = gTrue);
290
0
  virtual GBool hasStrongCompression() { return gTrue; }
291
  virtual void getImageParams(int *bitsPerComponent,
292
            StreamColorSpaceMode *csMode);
293
0
  void reduceResolution(int reductionA) { reduction = reductionA; }
294
295
private:
296
297
  void decodeImage();
298
  void fillReadBuf();
299
  void getImageParams2(int *bitsPerComponent, StreamColorSpaceMode *csMode);
300
  JPXDecodeResult readBoxes();
301
  GBool readColorSpecBox(Guint dataLen);
302
  JPXDecodeResult readCodestream(Guint len);
303
  GBool readTilePart();
304
  GBool readTilePartData(Guint tileIdx,
305
       Guint tilePartLen, GBool tilePartToEOC);
306
  GBool readCodeBlockData(JPXTileComp *tileComp,
307
        JPXResLevel *resLevel,
308
        JPXPrecinct *precinct,
309
        JPXSubband *subband,
310
        Guint res, Guint sb,
311
        JPXCodeBlock *cb);
312
  void inverseTransform(JPXTileComp *tileComp);
313
  void inverseTransformLevel(JPXTileComp *tileComp,
314
           Guint r, JPXResLevel *resLevel);
315
  void inverseTransform1D(JPXTileComp *tileComp, int *data,
316
        Guint offset, Guint n);
317
  GBool inverseMultiCompAndDC(JPXTile *tile);
318
  GBool readBoxHdr(Guint *boxType, Guint *boxLen, Guint *dataLen);
319
  int readMarkerHdr(int *segType, Guint *segLen);
320
  GBool readUByte(Guint *x);
321
  GBool readByte(int *x);
322
  GBool readUWord(Guint *x);
323
  GBool readULong(Guint *x);
324
  GBool readNBytes(int nBytes, GBool signd, int *x);
325
  void startBitBuf(Guint byteCountA);
326
  GBool readBits(int nBits, Guint *x);
327
  void skipSOP();
328
  void skipEPH();
329
  Guint finishBitBuf();
330
331
  BufStream *bufStr;    // buffered stream (for lookahead)
332
333
  GBool decoded;    // set when the image has been decoded
334
  Guint nComps;     // number of components
335
  Guint *bpc;     // bits per component, for each component
336
  Guint width, height;    // image size
337
  int reduction;    // log2(reduction in resolution)
338
  GBool haveImgHdr;   // set if a JP2/JPX image header has been
339
        //   found
340
  JPXColorSpec cs;    // color specification
341
  GBool haveCS;     // set if a color spec has been found
342
  JPXPalette palette;   // the palette
343
  GBool havePalette;    // set if a palette has been found
344
  JPXCompMap compMap;   // the component mapping
345
  GBool haveCompMap;    // set if a component mapping has been found
346
  JPXChannelDefn channelDefn; // channel definition
347
  GBool haveChannelDefn;  // set if a channel defn has been found
348
349
  JPXImage img;     // JPEG2000 decoder data
350
  Guint bitBuf;     // buffer for bit reads
351
  int bitBufLen;    // number of bits in bitBuf
352
  GBool bitBufSkip;   // true if next bit should be skipped
353
        //   (for bit stuffing)
354
  Guint byteCount;    // number of available bytes left
355
356
  Guint curX, curY, curComp;  // current position for lookChar/getChar
357
  Guint readBuf;    // read buffer
358
  Guint readBufLen;   // number of valid bits in readBuf
359
};
360
361
#endif