1 //========================================================================
5 // Copyright 2002-2003 Glyph & Cog, LLC
7 //========================================================================
12 #ifdef USE_GCC_PRAGMAS
16 #include "goo/gtypes.h"
20 class JArithmeticDecoder
;
21 class JArithmeticDecoderStats
;
23 //------------------------------------------------------------------------
25 enum JPXColorSpaceType
{
46 struct JPXColorSpecCIELab
{
47 Guint rl
, ol
, ra
, oa
, rb
, ob
, il
;
50 struct JPXColorSpecEnumerated
{
51 JPXColorSpaceType type
; // color space type
53 JPXColorSpecCIELab cieLab
;
59 int prec
; // precedence
61 JPXColorSpecEnumerated enumerated
;
65 //------------------------------------------------------------------------
68 Guint nEntries
; // number of entries in the palette
69 Guint nComps
; // number of components in each entry
70 Guint
*bpc
; // bits per component, for each component
71 int *c
; // color data:
72 // c[i*nComps+j] = entry i, component j
75 //------------------------------------------------------------------------
78 Guint nChannels
; // number of channels
79 Guint
*comp
; // codestream components mapped to each channel
80 Guint
*type
; // 0 for direct use, 1 for palette mapping
81 Guint
*pComp
; // palette components to use
84 //------------------------------------------------------------------------
86 struct JPXChannelDefn
{
87 Guint nChannels
; // number of channels
88 Guint
*idx
; // channel indexes
89 Guint
*type
; // channel types
90 Guint
*assoc
; // channel associations
93 //------------------------------------------------------------------------
95 struct JPXTagTreeNode
{
96 GBool finished
; // true if this node is finished
97 Guint val
; // current value
100 //------------------------------------------------------------------------
102 struct JPXCodeBlock
{
104 Guint x0
, y0
, x1
, y1
; // bounds
106 //----- persistent state
107 GBool seen
; // true if this code-block has already
109 Guint lBlock
; // base number of bits used for pkt data length
110 Guint nextPass
; // next coding pass
112 //---- info from first packet
113 Guint nZeroBitPlanes
; // number of zero bit planes
115 //----- info for the current packet
116 Guint included
; // code-block inclusion in this packet:
117 // 0=not included, 1=included
118 Guint nCodingPasses
; // number of coding passes in this pkt
119 Guint
*dataLen
; // data lengths (one per codeword segment)
120 Guint dataLenSize
; // size of the dataLen array
122 //----- coefficient data
124 char *touched
; // coefficient 'touched' flags
125 Gushort len
; // coefficient length
126 JArithmeticDecoder
// arithmetic decoder
128 JArithmeticDecoderStats
// arithmetic decoder stats
132 //------------------------------------------------------------------------
136 Guint x0
, y0
, x1
, y1
; // bounds
137 Guint nXCBs
, nYCBs
; // number of code-blocks in the x and y
141 Guint maxTTLevel
; // max tag tree level
142 JPXTagTreeNode
*inclusion
; // inclusion tag tree for each subband
143 JPXTagTreeNode
*zeroBitPlane
; // zero-bit plane tag tree for each
147 JPXCodeBlock
*cbs
; // the code-blocks (len = nXCBs * nYCBs)
150 //------------------------------------------------------------------------
154 Guint x0
, y0
, x1
, y1
; // bounds of the precinct
157 JPXSubband
*subbands
; // the subbands
160 //------------------------------------------------------------------------
163 //----- from the COD and COC segments (main and tile)
164 Guint precinctWidth
; // log2(precinct width)
165 Guint precinctHeight
; // log2(precinct height)
168 Guint x0
, y0
, x1
, y1
; // bounds of the tile-comp (for this res level)
169 Guint bx0
[3], by0
[3], // subband bounds
173 JPXPrecinct
*precincts
; // the precincts
176 //------------------------------------------------------------------------
179 //----- from the SIZ segment
180 GBool sgned
; // 1 for signed, 0 for unsigned
181 Guint prec
; // precision, in bits
182 Guint hSep
; // horizontal separation of samples
183 Guint vSep
; // vertical separation of samples
185 //----- from the COD and COC segments (main and tile)
186 Guint style
; // coding style parameter (Scod / Scoc)
187 Guint nDecompLevels
; // number of decomposition levels
188 Guint codeBlockW
; // log2(code-block width)
189 Guint codeBlockH
; // log2(code-block height)
190 Guint codeBlockStyle
; // code-block style
191 Guint transform
; // wavelet transformation
193 //----- from the QCD and QCC segments (main and tile)
194 Guint quantStyle
; // quantization style
195 Guint
*quantSteps
; // quantization step size for each subband
196 Guint nQuantSteps
; // number of entries in quantSteps
199 Guint x0
, y0
, x1
, y1
; // bounds of the tile-comp, in ref coords
201 Guint cbW
; // code-block width
202 Guint cbH
; // code-block height
205 int *data
; // the decoded image data
206 int *buf
; // intermediate buffer for the inverse
210 JPXResLevel
*resLevels
; // the resolution levels
211 // (len = nDecompLevels + 1)
214 //------------------------------------------------------------------------
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
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
229 //----- progression order loop counters
230 Guint comp
; // component
231 Guint res
; // resolution level
232 Guint precinct
; // precinct
233 Guint layer
; // layer
236 JPXTileComp
*tileComps
; // the tile-components (len = JPXImage.nComps)
239 //------------------------------------------------------------------------
242 //----- from the SIZ segment
243 Guint xSize
, ySize
; // size of reference grid
244 Guint xOffset
, yOffset
; // image offset
245 Guint xTileSize
, yTileSize
; // size of tiles
246 Guint xTileOffset
, // offset of first tile
248 Guint nComps
; // number of components
251 Guint nXTiles
; // number of tiles in x direction
252 Guint nYTiles
; // number of tiles in y direction
255 JPXTile
*tiles
; // the tiles (len = nXTiles * nYTiles)
258 //------------------------------------------------------------------------
260 class JPXStream
: public FilterStream
{
263 JPXStream(Stream
*strA
);
264 virtual ~JPXStream();
265 virtual StreamKind
getKind() { return strJPX
; }
266 virtual void reset();
267 virtual void close();
268 virtual int getChar();
269 virtual int lookChar();
270 virtual GooString
*getPSFilter(int psLevel
, const char *indent
);
271 virtual GBool
isBinary(GBool last
= gTrue
);
272 virtual void getImageParams(int *bitsPerComponent
,
273 StreamColorSpaceMode
*csMode
);
278 void getImageParams2(int *bitsPerComponent
, StreamColorSpaceMode
*csMode
);
280 GBool
readColorSpecBox(Guint dataLen
);
281 GBool
readCodestream(Guint len
);
282 GBool
readTilePart();
283 GBool
readTilePartData(Guint tileIdx
,
284 Guint tilePartLen
, GBool tilePartToEOC
);
285 GBool
readCodeBlockData(JPXTileComp
*tileComp
,
286 JPXResLevel
*resLevel
,
287 JPXPrecinct
*precinct
,
291 void inverseTransform(JPXTileComp
*tileComp
);
292 void inverseTransformLevel(JPXTileComp
*tileComp
,
293 Guint r
, JPXResLevel
*resLevel
);
294 void inverseTransform1D(JPXTileComp
*tileComp
, int *data
,
295 Guint offset
, Guint n
);
296 GBool
inverseMultiCompAndDC(JPXTile
*tile
);
297 GBool
readBoxHdr(Guint
*boxType
, Guint
*boxLen
, Guint
*dataLen
);
298 int readMarkerHdr(int *segType
, Guint
*segLen
);
299 GBool
readUByte(Guint
*x
);
300 GBool
readByte(int *x
);
301 GBool
readUWord(Guint
*x
);
302 GBool
readULong(Guint
*x
);
303 GBool
readNBytes(int nBytes
, GBool signd
, int *x
);
304 void startBitBuf(Guint byteCountA
);
305 GBool
readBits(int nBits
, Guint
*x
);
308 Guint
finishBitBuf();
310 BufStream
*bufStr
; // buffered stream (for lookahead)
312 Guint nComps
; // number of components
313 Guint
*bpc
; // bits per component, for each component
314 Guint width
, height
; // image size
315 GBool haveImgHdr
; // set if a JP2/JPX image header has been
317 JPXColorSpec cs
; // color specification
318 GBool haveCS
; // set if a color spec has been found
319 JPXPalette palette
; // the palette
320 GBool havePalette
; // set if a palette has been found
321 JPXCompMap compMap
; // the component mapping
322 GBool haveCompMap
; // set if a component mapping has been found
323 JPXChannelDefn channelDefn
; // channel definition
324 GBool haveChannelDefn
; // set if a channel defn has been found
326 JPXImage img
; // JPEG2000 decoder data
327 Guint bitBuf
; // buffer for bit reads
328 int bitBufLen
; // number of bits in bitBuf
329 GBool bitBufSkip
; // true if next bit should be skipped
330 // (for bit stuffing)
331 Guint byteCount
; // number of available bytes left
333 Guint curX
, curY
, curComp
; // current position for lookChar/getChar
334 Guint readBuf
; // read buffer
335 Guint readBufLen
; // number of valid bits in readBuf