CSJ2K.j2k.codestream.reader.HeaderDecoder.readCOC C# (CSharp) Method

readCOC() private method

Reads the COC marker segment and realigns the codestream where the next marker should be found.
If an I/O error occurs while reading from the /// encoder header stream /// ///
private readCOC ( System ehs, bool mainh, int tileIdx, int tpIdx ) : void
ehs System The encoder header stream. /// ///
mainh bool Flag indicating whether or not this marker segment is read /// from the main header. /// ///
tileIdx int The index of the current tile /// ///
tpIdx int Tile-part index /// ///
return void
        private void readCOC(System.IO.BinaryReader ehs, bool mainh, int tileIdx, int tpIdx)
        {
            int cComp; // current component
            SynWTFilter[] hfilters, vfilters;
            //int tmp, l;
            int ecOptions;
            System.Int32[] cblk;
            System.String errMsg;
            HeaderInfo.COC ms = hi.NewCOC;

            // Lcoc (marker length)
            ms.lcoc = ehs.ReadUInt16();

            // Ccoc
            if (nComp < 257)
            {
                cComp = ms.ccoc = ehs.ReadByte();
            }
            else
            {
                cComp = ms.ccoc = ehs.ReadUInt16();
            }
            if (cComp >= nComp)
            {
                throw new CorruptedCodestreamException("Invalid component index " + "in QCC marker");
            }

            // Scoc (block style)
            int cstyle = ms.scoc = ehs.ReadByte();
            if ((cstyle & CSJ2K.j2k.codestream.Markers.SCOX_PRECINCT_PARTITION) != 0)
            {
                precinctPartitionIsUsed = true;
                // Remove flag
                cstyle &= ~ (CSJ2K.j2k.codestream.Markers.SCOX_PRECINCT_PARTITION);
            }
            else
            {
                precinctPartitionIsUsed = false;
            }

            // SPcoc

            // decomposition levels
            int mrl = ms.spcoc_ndl = ehs.ReadByte();

            // Read the code-blocks dimensions
            cblk = new System.Int32[2];
            ms.spcoc_cw = ehs.ReadByte();
            cblk[0] = (System.Int32) (1 << (ms.spcoc_cw + 2));
            if (cblk[0] < CSJ2K.j2k.entropy.StdEntropyCoderOptions.MIN_CB_DIM || cblk[0] > CSJ2K.j2k.entropy.StdEntropyCoderOptions.MAX_CB_DIM)
            {
                errMsg = "Non-valid code-block width in SPcod field, " + "COC marker";
                throw new CorruptedCodestreamException(errMsg);
            }
            ms.spcoc_ch = ehs.ReadByte();
            cblk[1] = (System.Int32) (1 << (ms.spcoc_ch + 2));
            if (cblk[1] < CSJ2K.j2k.entropy.StdEntropyCoderOptions.MIN_CB_DIM || cblk[1] > CSJ2K.j2k.entropy.StdEntropyCoderOptions.MAX_CB_DIM)
            {
                errMsg = "Non-valid code-block height in SPcod field, " + "COC marker";
                throw new CorruptedCodestreamException(errMsg);
            }
            if ((cblk[0] * cblk[1]) > CSJ2K.j2k.entropy.StdEntropyCoderOptions.MAX_CB_AREA)
            {
                errMsg = "Non-valid code-block area in SPcod field, " + "COC marker";
                throw new CorruptedCodestreamException(errMsg);
            }
            if (mainh)
            {
                decSpec.cblks.setCompDef(cComp, (System.Object) (cblk));
            }
            else
            {
                decSpec.cblks.setTileCompVal(tileIdx, cComp, (System.Object) (cblk));
            }

            // Read entropy block mode options
            // NOTE: currently OPT_SEG_SYMBOLS is not included here
            ecOptions = ms.spcoc_cs = ehs.ReadByte();
            if ((ecOptions & ~ (CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS | CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_RESET_MQ | CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS | CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_VERT_STR_CAUSAL | CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_PRED_TERM | CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_SEG_SYMBOLS)) != 0)
            {
                throw new CorruptedCodestreamException("Unknown \"code-block " + "context\" in SPcoc field, " + "COC marker: 0x" + System.Convert.ToString(ecOptions, 16));
            }

            // Read wavelet filter for tile or image
            hfilters = new SynWTFilter[1];
            vfilters = new SynWTFilter[1];
            hfilters[0] = readFilter(ehs, ms.spcoc_t);
            vfilters[0] = hfilters[0];

            // Fill the filter spec
            // If this is the main header, set the default value, if it is the
            // tile header, set default for this tile
            SynWTFilter[][] hvfilters = new SynWTFilter[2][];
            hvfilters[0] = hfilters;
            hvfilters[1] = vfilters;

            // Get precinct partition sizes
            System.Collections.Generic.List<System.Int32>[] v = new System.Collections.Generic.List<System.Int32>[2];
            v[0] = new List<int>(10);
            v[1] = new List<int>(10);
            int val = CSJ2K.j2k.codestream.Markers.PRECINCT_PARTITION_DEF_SIZE;
            if (!precinctPartitionIsUsed)
            {
                System.Int32 w, h;
                w = (System.Int32) (1 << (val & 0x000F));
                v[0].Add(w);
                h = (System.Int32) (1 << (((val & 0x00F0) >> 4)));
                v[1].Add(h);
            }
            else
            {
                ms.spcoc_ps = new int[mrl + 1];
                for (int rl = mrl; rl >= 0; rl--)
                {
                    System.Int32 w, h;
                    val = ms.spcoc_ps[rl] = ehs.ReadByte();
                    w = (System.Int32) (1 << (val & 0x000F));
                    v[0].Insert(0, w);
                    h = (System.Int32) (1 << (((val & 0x00F0) >> 4)));
                    v[1].Insert(0, h);
                }
            }
            if (mainh)
            {
                decSpec.pss.setCompDef(cComp, v);
            }
            else
            {
                decSpec.pss.setTileCompVal(tileIdx, cComp, v);
            }
            precinctPartitionIsUsed = true;

            // Check marker length
            checkMarkerLength(ehs, "COD marker");

            if (mainh)
            {
                hi.cocValue["main_c" + cComp] = ms;
                decSpec.wfs.setCompDef(cComp, hvfilters);
                decSpec.dls.setCompDef(cComp, (System.Object) mrl);
                decSpec.ecopts.setCompDef(cComp, (System.Object) ecOptions);
            }
            else
            {
                hi.cocValue["t" + tileIdx + "_c" + cComp] = ms;
                decSpec.wfs.setTileCompVal(tileIdx, cComp, hvfilters);
                decSpec.dls.setTileCompVal(tileIdx, cComp, (System.Object) mrl);
                decSpec.ecopts.setTileCompVal(tileIdx, cComp, (System.Object) ecOptions);
            }
        }