1 //========================================================================
5 // Copyright 2002-2003 Glyph & Cog, LLC
7 //========================================================================
14 #ifdef USE_GCC_PRAGMAS
22 class JArithmeticDecoderStats
;
24 //------------------------------------------------------------------------
26 enum JPXColorSpaceType
{
49 int prec
; // precedence
52 JPXColorSpaceType type
; // color space type
55 Guint rl
, ol
, ra
, oa
, rb
, ob
, il
;
62 //------------------------------------------------------------------------
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
72 //------------------------------------------------------------------------
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
81 //------------------------------------------------------------------------
83 struct JPXChannelDefn
{
84 Guint nChannels
; // number of channels
85 Guint
*idx
; // channel indexes
86 Guint
*type
; // channel types
87 Guint
*assoc
; // channel associations
90 //------------------------------------------------------------------------
92 struct JPXTagTreeNode
{
93 GBool finished
; // true if this node is finished
94 Guint val
; // current value
97 //------------------------------------------------------------------------
100 Gushort flags
; // flag bits
101 Gushort len
; // number of significant bits in mag
102 Guint mag
; // magnitude value
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)
115 //------------------------------------------------------------------------
117 struct JPXCodeBlock
{
119 Guint x0
, y0
, x1
, y1
; // bounds
121 //----- persistent state
122 GBool seen
; // true if this code-block has already
124 Guint lBlock
; // base number of bits used for pkt data length
125 Guint nextPass
; // next coding pass
127 //---- info from first packet
128 Guint nZeroBitPlanes
; // number of zero bit planes
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
136 //----- coefficient data
137 JPXCoeff
*coeffs
; // the coefficients
138 JArithmeticDecoderStats
// arithmetic decoder stats
142 //------------------------------------------------------------------------
146 Guint x0
, y0
, x1
, y1
; // bounds
147 Guint nXCBs
, nYCBs
; // number of code-blocks in the x and y
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
157 JPXCodeBlock
*cbs
; // the code-blocks (len = nXCBs * nYCBs)
160 //------------------------------------------------------------------------
164 Guint x0
, y0
, x1
, y1
; // bounds of the precinct
167 JPXSubband
*subbands
; // the subbands
170 //------------------------------------------------------------------------
173 //----- from the COD and COC segments (main and tile)
174 Guint precinctWidth
; // log2(precinct width)
175 Guint precinctHeight
; // log2(precinct height)
178 Guint x0
, y0
, x1
, y1
; // bounds of the tile-comp (for this res level)
179 Guint bx0
[3], by0
[3], // subband bounds
183 JPXPrecinct
*precincts
; // the precincts
186 //------------------------------------------------------------------------
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
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
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
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
214 int *data
; // the decoded image data
215 int *buf
; // intermediate buffer for the inverse
219 JPXResLevel
*resLevels
; // the resolution levels
220 // (len = nDecompLevels + 1)
223 //------------------------------------------------------------------------
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
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
236 //----- progression order loop counters
237 Guint comp
; // component
238 Guint res
; // resolution level
239 Guint precinct
; // precinct
240 Guint layer
; // layer
243 JPXTileComp
*tileComps
; // the tile-components (len = JPXImage.nComps)
246 //------------------------------------------------------------------------
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
255 Guint nComps
; // number of components
258 Guint nXTiles
; // number of tiles in x direction
259 Guint nYTiles
; // number of tiles in y direction
262 JPXTile
*tiles
; // the tiles (len = nXTiles * nYTiles)
265 //------------------------------------------------------------------------
267 class JPXStream
: public FilterStream
{
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
);
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
,
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
,
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
);
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
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
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
335 Guint curX
, curY
, curComp
; // current position for lookChar/getChar
336 Guint readBuf
; // read buffer
337 Guint readBufLen
; // number of valid bits in readBuf