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