Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-10 10:23:47

0001 //========================================================================
0002 //
0003 // JPXStream.h
0004 //
0005 // Copyright 2002-2003 Glyph & Cog, LLC
0006 //
0007 //========================================================================
0008 
0009 //========================================================================
0010 //
0011 // Modified under the Poppler project - http://poppler.freedesktop.org
0012 //
0013 // All changes made under the Poppler project to this file are licensed
0014 // under GPL version 2 or later
0015 //
0016 // Copyright (C) 2019, 2021 Albert Astals Cid <aacid@kde.org>
0017 //
0018 // To see a description of the changes please see the Changelog file that
0019 // came with your tarball or type make ChangeLog if you are building from git
0020 //
0021 //========================================================================
0022 
0023 #ifndef JPXSTREAM_H
0024 #define JPXSTREAM_H
0025 
0026 #include "Object.h"
0027 #include "Stream.h"
0028 
0029 class JArithmeticDecoder;
0030 class JArithmeticDecoderStats;
0031 
0032 //------------------------------------------------------------------------
0033 
0034 enum JPXColorSpaceType
0035 {
0036     jpxCSBiLevel = 0,
0037     jpxCSYCbCr1 = 1,
0038     jpxCSYCbCr2 = 3,
0039     jpxCSYCBCr3 = 4,
0040     jpxCSPhotoYCC = 9,
0041     jpxCSCMY = 11,
0042     jpxCSCMYK = 12,
0043     jpxCSYCCK = 13,
0044     jpxCSCIELab = 14,
0045     jpxCSsRGB = 16,
0046     jpxCSGrayscale = 17,
0047     jpxCSBiLevel2 = 18,
0048     jpxCSCIEJab = 19,
0049     jpxCSCISesRGB = 20,
0050     jpxCSROMMRGB = 21,
0051     jpxCSsRGBYCbCr = 22,
0052     jpxCSYPbPr1125 = 23,
0053     jpxCSYPbPr1250 = 24
0054 };
0055 
0056 struct JPXColorSpecCIELab
0057 {
0058     unsigned int rl, ol, ra, oa, rb, ob, il;
0059 };
0060 
0061 struct JPXColorSpecEnumerated
0062 {
0063     JPXColorSpaceType type; // color space type
0064     union {
0065         JPXColorSpecCIELab cieLab;
0066     };
0067 };
0068 
0069 struct JPXColorSpec
0070 {
0071     unsigned int meth; // method
0072     int prec; // precedence
0073     union {
0074         JPXColorSpecEnumerated enumerated;
0075     };
0076 };
0077 
0078 //------------------------------------------------------------------------
0079 
0080 struct JPXPalette
0081 {
0082     unsigned int nEntries; // number of entries in the palette
0083     unsigned int nComps; // number of components in each entry
0084     unsigned int *bpc; // bits per component, for each component
0085     int *c; // color data:
0086             //   c[i*nComps+j] = entry i, component j
0087 };
0088 
0089 //------------------------------------------------------------------------
0090 
0091 struct JPXCompMap
0092 {
0093     unsigned int nChannels; // number of channels
0094     unsigned int *comp; // codestream components mapped to each channel
0095     unsigned int *type; // 0 for direct use, 1 for palette mapping
0096     unsigned int *pComp; // palette components to use
0097 };
0098 
0099 //------------------------------------------------------------------------
0100 
0101 struct JPXChannelDefn
0102 {
0103     unsigned int nChannels; // number of channels
0104     unsigned int *idx; // channel indexes
0105     unsigned int *type; // channel types
0106     unsigned int *assoc; // channel associations
0107 };
0108 
0109 //------------------------------------------------------------------------
0110 
0111 struct JPXTagTreeNode
0112 {
0113     bool finished; // true if this node is finished
0114     unsigned int val; // current value
0115 };
0116 
0117 //------------------------------------------------------------------------
0118 
0119 struct JPXCodeBlock
0120 {
0121     //----- size
0122     unsigned int x0, y0, x1, y1; // bounds
0123 
0124     //----- persistent state
0125     bool seen; // true if this code-block has already
0126                //   been seen
0127     unsigned int lBlock; // base number of bits used for pkt data length
0128     unsigned int nextPass; // next coding pass
0129 
0130     //---- info from first packet
0131     unsigned int nZeroBitPlanes; // number of zero bit planes
0132 
0133     //----- info for the current packet
0134     unsigned int included; // code-block inclusion in this packet:
0135                            //   0=not included, 1=included
0136     unsigned int nCodingPasses; // number of coding passes in this pkt
0137     unsigned int *dataLen; // data lengths (one per codeword segment)
0138     unsigned int dataLenSize; // size of the dataLen array
0139 
0140     //----- coefficient data
0141     int *coeffs;
0142     char *touched; // coefficient 'touched' flags
0143     unsigned short len; // coefficient length
0144     JArithmeticDecoder // arithmetic decoder
0145             *arithDecoder;
0146     JArithmeticDecoderStats // arithmetic decoder stats
0147             *stats;
0148 };
0149 
0150 //------------------------------------------------------------------------
0151 
0152 struct JPXSubband
0153 {
0154     //----- computed
0155     unsigned int x0, y0, x1, y1; // bounds
0156     unsigned int nXCBs, nYCBs; // number of code-blocks in the x and y
0157                                //   directions
0158 
0159     //----- tag trees
0160     unsigned int maxTTLevel; // max tag tree level
0161     JPXTagTreeNode *inclusion; // inclusion tag tree for each subband
0162     JPXTagTreeNode *zeroBitPlane; // zero-bit plane tag tree for each
0163                                   //   subband
0164 
0165     //----- children
0166     JPXCodeBlock *cbs; // the code-blocks (len = nXCBs * nYCBs)
0167 };
0168 
0169 //------------------------------------------------------------------------
0170 
0171 struct JPXPrecinct
0172 {
0173     //----- computed
0174     unsigned int x0, y0, x1, y1; // bounds of the precinct
0175 
0176     //----- children
0177     JPXSubband *subbands; // the subbands
0178 };
0179 
0180 //------------------------------------------------------------------------
0181 
0182 struct JPXResLevel
0183 {
0184     //----- from the COD and COC segments (main and tile)
0185     unsigned int precinctWidth; // log2(precinct width)
0186     unsigned int precinctHeight; // log2(precinct height)
0187 
0188     //----- computed
0189     unsigned int x0, y0, x1, y1; // bounds of the tile-comp (for this res level)
0190     unsigned int bx0[3], by0[3], // subband bounds
0191             bx1[3], by1[3];
0192 
0193     //---- children
0194     JPXPrecinct *precincts; // the precincts
0195 };
0196 
0197 //------------------------------------------------------------------------
0198 
0199 struct JPXTileComp
0200 {
0201     //----- from the SIZ segment
0202     bool sgned; // 1 for signed, 0 for unsigned
0203     unsigned int prec; // precision, in bits
0204     unsigned int hSep; // horizontal separation of samples
0205     unsigned int vSep; // vertical separation of samples
0206 
0207     //----- from the COD and COC segments (main and tile)
0208     unsigned int style; // coding style parameter (Scod / Scoc)
0209     unsigned int nDecompLevels; // number of decomposition levels
0210     unsigned int codeBlockW; // log2(code-block width)
0211     unsigned int codeBlockH; // log2(code-block height)
0212     unsigned int codeBlockStyle; // code-block style
0213     unsigned int transform; // wavelet transformation
0214 
0215     //----- from the QCD and QCC segments (main and tile)
0216     unsigned int quantStyle; // quantization style
0217     unsigned int *quantSteps; // quantization step size for each subband
0218     unsigned int nQuantSteps; // number of entries in quantSteps
0219 
0220     //----- computed
0221     unsigned int x0, y0, x1, y1; // bounds of the tile-comp, in ref coords
0222     unsigned int w; // x1 - x0
0223     unsigned int cbW; // code-block width
0224     unsigned int cbH; // code-block height
0225 
0226     //----- image data
0227     int *data; // the decoded image data
0228     int *buf; // intermediate buffer for the inverse
0229               //   transform
0230 
0231     //----- children
0232     JPXResLevel *resLevels; // the resolution levels
0233                             //   (len = nDecompLevels + 1)
0234 };
0235 
0236 //------------------------------------------------------------------------
0237 
0238 struct JPXTile
0239 {
0240     bool init;
0241 
0242     //----- from the COD segments (main and tile)
0243     unsigned int progOrder; // progression order
0244     unsigned int nLayers; // number of layers
0245     unsigned int multiComp; // multiple component transformation
0246 
0247     //----- computed
0248     unsigned int x0, y0, x1, y1; // bounds of the tile, in ref coords
0249     unsigned int maxNDecompLevels; // max number of decomposition levels used
0250                                    //   in any component in this tile
0251 
0252     //----- progression order loop counters
0253     unsigned int comp; //   component
0254     unsigned int res; //   resolution level
0255     unsigned int precinct; //   precinct
0256     unsigned int layer; //   layer
0257 
0258     //----- children
0259     JPXTileComp *tileComps; // the tile-components (len = JPXImage.nComps)
0260 };
0261 
0262 //------------------------------------------------------------------------
0263 
0264 struct JPXImage
0265 {
0266     //----- from the SIZ segment
0267     unsigned int xSize, ySize; // size of reference grid
0268     unsigned int xOffset, yOffset; // image offset
0269     unsigned int xTileSize, yTileSize; // size of tiles
0270     unsigned int xTileOffset, // offset of first tile
0271             yTileOffset;
0272     unsigned int nComps; // number of components
0273 
0274     //----- computed
0275     unsigned int nXTiles; // number of tiles in x direction
0276     unsigned int nYTiles; // number of tiles in y direction
0277 
0278     //----- children
0279     JPXTile *tiles; // the tiles (len = nXTiles * nYTiles)
0280 };
0281 
0282 //------------------------------------------------------------------------
0283 
0284 class JPXStream : public FilterStream
0285 {
0286 public:
0287     JPXStream(Stream *strA);
0288     virtual ~JPXStream();
0289     StreamKind getKind() const override { return strJPX; }
0290     void reset() override;
0291     void close() override;
0292     int getChar() override;
0293     int lookChar() override;
0294     GooString *getPSFilter(int psLevel, const char *indent) override;
0295     bool isBinary(bool last = true) const override;
0296     void getImageParams(int *bitsPerComponent, StreamColorSpaceMode *csMode) override;
0297 
0298 private:
0299     void fillReadBuf();
0300     void getImageParams2(int *bitsPerComponent, StreamColorSpaceMode *csMode);
0301     bool readBoxes();
0302     bool readColorSpecBox(unsigned int dataLen);
0303     bool readCodestream(unsigned int len);
0304     bool readTilePart();
0305     bool readTilePartData(unsigned int tileIdx, unsigned int tilePartLen, bool tilePartToEOC);
0306     bool readCodeBlockData(JPXTileComp *tileComp, JPXResLevel *resLevel, JPXPrecinct *precinct, JPXSubband *subband, unsigned int res, unsigned int sb, JPXCodeBlock *cb);
0307     void inverseTransform(JPXTileComp *tileComp);
0308     void inverseTransformLevel(JPXTileComp *tileComp, unsigned int r, JPXResLevel *resLevel);
0309     void inverseTransform1D(JPXTileComp *tileComp, int *data, unsigned int offset, unsigned int n);
0310     bool inverseMultiCompAndDC(JPXTile *tile);
0311     bool readBoxHdr(unsigned int *boxType, unsigned int *boxLen, unsigned int *dataLen);
0312     int readMarkerHdr(int *segType, unsigned int *segLen);
0313     bool readUByte(unsigned int *x);
0314     bool readByte(int *x);
0315     bool readUWord(unsigned int *x);
0316     bool readULong(unsigned int *x);
0317     bool readNBytes(int nBytes, bool signd, int *x);
0318     void startBitBuf(unsigned int byteCountA);
0319     bool readBits(int nBits, unsigned int *x);
0320     void skipSOP();
0321     void skipEPH();
0322     unsigned int finishBitBuf();
0323 
0324     BufStream *bufStr; // buffered stream (for lookahead)
0325 
0326     unsigned int nComps; // number of components
0327     unsigned int *bpc; // bits per component, for each component
0328     unsigned int width, height; // image size
0329     bool haveImgHdr; // set if a JP2/JPX image header has been
0330                      //   found
0331     JPXColorSpec cs; // color specification
0332     bool haveCS; // set if a color spec has been found
0333     JPXPalette palette; // the palette
0334     bool havePalette; // set if a palette has been found
0335     JPXCompMap compMap; // the component mapping
0336     bool haveCompMap; // set if a component mapping has been found
0337     JPXChannelDefn channelDefn; // channel definition
0338     bool haveChannelDefn; // set if a channel defn has been found
0339 
0340     JPXImage img; // JPEG2000 decoder data
0341     unsigned int bitBuf; // buffer for bit reads
0342     int bitBufLen; // number of bits in bitBuf
0343     bool bitBufSkip; // true if next bit should be skipped
0344                      //   (for bit stuffing)
0345     unsigned int byteCount; // number of available bytes left
0346 
0347     unsigned int curX, curY, curComp; // current position for lookChar/getChar
0348     unsigned int readBuf; // read buffer
0349     unsigned int readBufLen; // number of valid bits in readBuf
0350 };
0351 
0352 #endif