1 //========================================================================
5 // Copyright 2002-2003 Glyph & Cog, LLC
7 //========================================================================
14 #ifdef USE_GCC_PRAGMAS
22 class JArithmeticDecoderStats;
24 //------------------------------------------------------------------------
26 enum JPXColorSpaceType {
47 struct JPXColorSpecCIELab {
48 Guint rl, ol, ra, oa, rb, ob, il;
51 struct JPXColorSpecEnumerated {
52 JPXColorSpaceType type; // color space type
54 JPXColorSpecCIELab cieLab;
60 int prec; // precedence
62 JPXColorSpecEnumerated enumerated;
66 //------------------------------------------------------------------------
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
76 //------------------------------------------------------------------------
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
85 //------------------------------------------------------------------------
87 struct JPXChannelDefn {
88 Guint nChannels; // number of channels
89 Guint *idx; // channel indexes
90 Guint *type; // channel types
91 Guint *assoc; // channel associations
94 //------------------------------------------------------------------------
96 struct JPXTagTreeNode {
97 GBool finished; // true if this node is finished
98 Guint val; // current value
101 //------------------------------------------------------------------------
104 Gushort flags; // flag bits
105 Gushort len; // number of significant bits in mag
106 Guint mag; // magnitude value
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)
119 //------------------------------------------------------------------------
121 struct JPXCodeBlock {
123 Guint x0, y0, x1, y1; // bounds
125 //----- persistent state
126 GBool seen; // true if this code-block has already
128 Guint lBlock; // base number of bits used for pkt data length
129 Guint nextPass; // next coding pass
131 //---- info from first packet
132 Guint nZeroBitPlanes; // number of zero bit planes
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
140 //----- coefficient data
141 JPXCoeff *coeffs; // the coefficients
142 JArithmeticDecoder // arithmetic decoder
144 JArithmeticDecoderStats // arithmetic decoder stats
148 //------------------------------------------------------------------------
152 Guint x0, y0, x1, y1; // bounds
153 Guint nXCBs, nYCBs; // number of code-blocks in the x and y
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
163 JPXCodeBlock *cbs; // the code-blocks (len = nXCBs * nYCBs)
166 //------------------------------------------------------------------------
170 Guint x0, y0, x1, y1; // bounds of the precinct
173 JPXSubband *subbands; // the subbands
176 //------------------------------------------------------------------------
179 //----- from the COD and COC segments (main and tile)
180 Guint precinctWidth; // log2(precinct width)
181 Guint precinctHeight; // log2(precinct height)
184 Guint x0, y0, x1, y1; // bounds of the tile-comp (for this res level)
185 Guint bx0[3], by0[3], // subband bounds
189 JPXPrecinct *precincts; // the precincts
192 //------------------------------------------------------------------------
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
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
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
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
220 int *data; // the decoded image data
221 int *buf; // intermediate buffer for the inverse
225 JPXResLevel *resLevels; // the resolution levels
226 // (len = nDecompLevels + 1)
229 //------------------------------------------------------------------------
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
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
242 //----- progression order loop counters
243 Guint comp; // component
244 Guint res; // resolution level
245 Guint precinct; // precinct
246 Guint layer; // layer
249 JPXTileComp *tileComps; // the tile-components (len = JPXImage.nComps)
252 //------------------------------------------------------------------------
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
261 Guint nComps; // number of components
264 Guint nXTiles; // number of tiles in x direction
265 Guint nYTiles; // number of tiles in y direction
268 JPXTile *tiles; // the tiles (len = nXTiles * nYTiles)
271 //------------------------------------------------------------------------
273 class JPXStream: public FilterStream {
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);
290 void getImageParams2(int *bitsPerComponent, StreamColorSpaceMode *csMode);
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,
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,
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);
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
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
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
344 Guint curX, curY, curComp; // current position for lookChar/getChar
345 Guint readBuf; // read buffer
346 Guint readBufLen; // number of valid bits in readBuf