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