CSJ2K.j2k.codestream.writer.HeaderEncoder.writeCOD C# (CSharp) Method

writeCOD() protected method

Writes COD marker segment. COD is a functional marker segment containing the code style default (coding style, decomposition, layering) used for compressing all the components in an image.

The values can be overriden for an individual component by a COC marker in either the main or the tile header.

protected writeCOD ( bool mh, int tileIdx ) : void
mh bool Flag indicating whether this marker belongs to the main /// header /// ///
tileIdx int Tile index if the marker belongs to a tile-part header /// ///
return void
        protected internal virtual void writeCOD(bool mh, int tileIdx)
        {
            AnWTFilter[][] filt;
            bool precinctPartitionUsed;
            int tmp;
            int mrl = 0, a = 0;
            int ppx = 0, ppy = 0;
            Progression[] prog;

            if (mh)
            {
                mrl = ((System.Int32) encSpec.dls.getDefault());
                // get default precinct size
                ppx = encSpec.pss.getPPX(- 1, - 1, mrl);
                ppy = encSpec.pss.getPPY(- 1, - 1, mrl);
                prog = (Progression[]) (encSpec.pocs.getDefault());
            }
            else
            {
                mrl = ((System.Int32) encSpec.dls.getTileDef(tileIdx));
                // get precinct size for specified tile
                ppx = encSpec.pss.getPPX(tileIdx, - 1, mrl);
                ppy = encSpec.pss.getPPY(tileIdx, - 1, mrl);
                prog = (Progression[]) (encSpec.pocs.getTileDef(tileIdx));
            }

            if (ppx != CSJ2K.j2k.codestream.Markers.PRECINCT_PARTITION_DEF_SIZE || ppy != CSJ2K.j2k.codestream.Markers.PRECINCT_PARTITION_DEF_SIZE)
            {
                precinctPartitionUsed = true;
            }
            else
            {
                precinctPartitionUsed = false;
            }

            if (precinctPartitionUsed)
            {
                // If precinct partition is used we add one byte per resolution
                // level i.e. mrl+1 (+1 for resolution 0).
                a = mrl + 1;
            }

            // Write COD marker
            hbuf.Write((System.Int16) CSJ2K.j2k.codestream.Markers.COD);

            // Lcod (marker segment length (in bytes)) Basic : Lcod(2
            // bytes)+Scod(1)+SGcod(4)+SPcod(5+a)  where:
            // a=0 if no precinct partition is used
            // a=mrl+1 if precinct partition used
            int markSegLen = 12 + a;
            hbuf.Write((System.Int16) markSegLen);

            // Scod (coding style parameter)
            tmp = 0;
            if (precinctPartitionUsed)
            {
                tmp = CSJ2K.j2k.codestream.Markers.SCOX_PRECINCT_PARTITION;
            }

            // Are SOP markers used ?
            if (mh)
            {
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                if (((System.String) encSpec.sops.getDefault().ToString()).ToUpper().Equals("on".ToUpper()))
                {
                    tmp |= CSJ2K.j2k.codestream.Markers.SCOX_USE_SOP;
                }
            }
            else
            {
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                if (((System.String) encSpec.sops.getTileDef(tileIdx).ToString()).ToUpper().Equals("on".ToUpper()))
                {
                    tmp |= CSJ2K.j2k.codestream.Markers.SCOX_USE_SOP;
                }
            }

            // Are EPH markers used ?
            if (mh)
            {
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                if (((System.String) encSpec.ephs.getDefault().ToString()).ToUpper().Equals("on".ToUpper()))
                {
                    tmp |= CSJ2K.j2k.codestream.Markers.SCOX_USE_EPH;
                }
            }
            else
            {
                //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                if (((System.String) encSpec.ephs.getTileDef(tileIdx).ToString()).ToUpper().Equals("on".ToUpper()))
                {
                    tmp |= CSJ2K.j2k.codestream.Markers.SCOX_USE_EPH;
                }
            }
            if (dwt.CbULX != 0)
                tmp |= CSJ2K.j2k.codestream.Markers.SCOX_HOR_CB_PART;
            if (dwt.CbULY != 0)
                tmp |= CSJ2K.j2k.codestream.Markers.SCOX_VER_CB_PART;
            hbuf.Write((System.Byte) tmp);

            // SGcod
            // Progression order
            hbuf.Write((System.Byte) prog[0].type);

            // Number of layers
            hbuf.Write((System.Int16) ralloc.NumLayers);

            // Multiple component transform
            // CSsiz (Color transform)
            System.String str = null;
            if (mh)
            {
                str = ((System.String) encSpec.cts.getDefault());
            }
            else
            {
                str = ((System.String) encSpec.cts.getTileDef(tileIdx));
            }

            if (str.Equals("none"))
            {
                hbuf.Write((System.Byte) 0);
            }
            else
            {
                hbuf.Write((System.Byte) 1);
            }

            // SPcod
            // Number of decomposition levels
            hbuf.Write((System.Byte) mrl);

            // Code-block width and height
            if (mh)
            {
                // main header, get default values
                tmp = encSpec.cblks.getCBlkWidth(ModuleSpec.SPEC_DEF, - 1, - 1);
                hbuf.Write((System.Byte) (MathUtil.log2(tmp) - 2));
                tmp = encSpec.cblks.getCBlkHeight(ModuleSpec.SPEC_DEF, - 1, - 1);
                hbuf.Write((System.Byte) (MathUtil.log2(tmp) - 2));
            }
            else
            {
                // tile header, get tile default values
                tmp = encSpec.cblks.getCBlkWidth(ModuleSpec.SPEC_TILE_DEF, tileIdx, - 1);
                hbuf.Write((System.Byte) (MathUtil.log2(tmp) - 2));
                tmp = encSpec.cblks.getCBlkHeight(ModuleSpec.SPEC_TILE_DEF, tileIdx, - 1);
                hbuf.Write((System.Byte) (MathUtil.log2(tmp) - 2));
            }

            // Style of the code-block coding passes
            tmp = 0;
            if (mh)
            {
                // Main header
                // Selective arithmetic coding bypass ?
                if (((System.String) encSpec.bms.getDefault()).Equals("on"))
                {
                    tmp |= CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS;
                }
                // MQ reset after each coding pass ?
                if (((System.String) encSpec.mqrs.getDefault()).Equals("on"))
                {
                    tmp |= CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_RESET_MQ;
                }
                // MQ termination after each arithmetically coded coding pass ?
                if (((System.String) encSpec.rts.getDefault()).Equals("on"))
                {
                    tmp |= CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS;
                }
                // Vertically stripe-causal context mode ?
                if (((System.String) encSpec.css.getDefault()).Equals("on"))
                {
                    tmp |= CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_VERT_STR_CAUSAL;
                }
                // Predictable termination ?
                if (((System.String) encSpec.tts.getDefault()).Equals("predict"))
                {
                    tmp |= CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_PRED_TERM;
                }
                // Error resilience segmentation symbol insertion ?
                if (((System.String) encSpec.sss.getDefault()).Equals("on"))
                {
                    tmp |= CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_SEG_SYMBOLS;
                }
            }
            else
            {
                // Tile header
                // Selective arithmetic coding bypass ?
                if (((System.String) encSpec.bms.getTileDef(tileIdx)).Equals("on"))
                {
                    tmp |= CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_BYPASS;
                }
                // MQ reset after each coding pass ?
                if (((System.String) encSpec.mqrs.getTileDef(tileIdx)).Equals("on"))
                {
                    tmp |= CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_RESET_MQ;
                }
                // MQ termination after each arithmetically coded coding pass ?
                if (((System.String) encSpec.rts.getTileDef(tileIdx)).Equals("on"))
                {
                    tmp |= CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_TERM_PASS;
                }
                // Vertically stripe-causal context mode ?
                if (((System.String) encSpec.css.getTileDef(tileIdx)).Equals("on"))
                {
                    tmp |= CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_VERT_STR_CAUSAL;
                }
                // Predictable termination ?
                if (((System.String) encSpec.tts.getTileDef(tileIdx)).Equals("predict"))
                {
                    tmp |= CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_PRED_TERM;
                }
                // Error resilience segmentation symbol insertion ?
                if (((System.String) encSpec.sss.getTileDef(tileIdx)).Equals("on"))
                {
                    tmp |= CSJ2K.j2k.entropy.StdEntropyCoderOptions.OPT_SEG_SYMBOLS;
                }
            }
            hbuf.Write((System.Byte) tmp);

            // Wavelet transform
            // Wavelet Filter
            if (mh)
            {
                filt = ((AnWTFilter[][]) encSpec.wfs.getDefault());
                hbuf.Write((System.Byte) filt[0][0].FilterType);
            }
            else
            {
                filt = ((AnWTFilter[][]) encSpec.wfs.getTileDef(tileIdx));
                hbuf.Write((System.Byte) filt[0][0].FilterType);
            }

            // Precinct partition
            if (precinctPartitionUsed)
            {
                // Write the precinct size for each resolution level + 1
                // (resolution 0) if precinct partition is used.
                System.Collections.Generic.List<System.Int32>[] v = null;
                if (mh)
                {
                    v = (System.Collections.Generic.List<System.Int32>[])encSpec.pss.getDefault();
                }
                else
                {
                    v = (System.Collections.Generic.List<System.Int32>[])encSpec.pss.getTileDef(tileIdx);
                }
                for (int r = mrl; r >= 0; r--)
                {
                    if (r >= v[1].Count)
                    {
                        tmp = ((System.Int32) v[1][v[1].Count - 1]);
                    }
                    else
                    {
                        tmp = ((System.Int32) v[1][r]);
                    }
                    int yExp = (MathUtil.log2(tmp) << 4) & 0x00F0;

                    if (r >= v[0].Count)
                    {
                        tmp = ((System.Int32) v[0][v[0].Count - 1]);
                    }
                    else
                    {
                        tmp = ((System.Int32) v[0][r]);
                    }
                    int xExp = MathUtil.log2(tmp) & 0x000F;
                    hbuf.Write((System.Byte) (yExp | xExp));
                }
            }
        }