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

extractMainMarkSeg() private method

This method extract a marker segment from the main header and stores it into a byte buffer for the second pass. The marker segment is first identified. Then its flag is activated. Finally, its content is buffered into a byte array stored in an hashTable.

If the marker is not recognized, it prints a warning and skips it according to its length.

SIZ marker segment shall be the first encountered marker segment.

private extractMainMarkSeg ( short marker, RandomAccessIO ehs ) : void
marker short The marker segment to process /// ///
ehs RandomAccessIO The encoded header stream /// ///
return void
        private void extractMainMarkSeg(short marker, RandomAccessIO ehs)
        {
            if (nfMarkSeg == 0)
            {
                // First non-delimiting marker of the header
                // JPEG 2000 part 1 specify that it must be SIZ
                if (marker != CSJ2K.j2k.codestream.Markers.SIZ)
                {
                    throw new CorruptedCodestreamException("First marker after " + "SOC " + "must be SIZ " + System.Convert.ToString(marker, 16));
                }
            }

            System.String htKey = ""; // Name used as a key for the hash-table
            if (ht == null)
            {
                ht = new Dictionary<string, byte[]>();
            }

            switch (marker)
            {

                case CSJ2K.j2k.codestream.Markers.SIZ:
                    if ((nfMarkSeg & SIZ_FOUND) != 0)
                    {
                        throw new CorruptedCodestreamException("More than one SIZ marker " + "segment found in main " + "header");
                    }
                    nfMarkSeg |= SIZ_FOUND;
                    htKey = "SIZ";
                    break;

                case CSJ2K.j2k.codestream.Markers.SOD:
                    throw new CorruptedCodestreamException("SOD found in main header");

                case CSJ2K.j2k.codestream.Markers.EOC:
                    throw new CorruptedCodestreamException("EOC found in main header");

                case CSJ2K.j2k.codestream.Markers.SOT:
                    if ((nfMarkSeg & SOT_FOUND) != 0)
                    {
                        throw new CorruptedCodestreamException("More than one SOT " + "marker " + "found right after " + "main " + "or tile header");
                    }
                    nfMarkSeg |= SOT_FOUND;
                    return ;

                case CSJ2K.j2k.codestream.Markers.COD:
                    if ((nfMarkSeg & COD_FOUND) != 0)
                    {
                        throw new CorruptedCodestreamException("More than one COD " + "marker " + "found in main header");
                    }
                    nfMarkSeg |= COD_FOUND;
                    htKey = "COD";
                    break;

                case CSJ2K.j2k.codestream.Markers.COC:
                    nfMarkSeg |= COC_FOUND;
                    htKey = "COC" + (nCOCMarkSeg++);
                    break;

                case CSJ2K.j2k.codestream.Markers.QCD:
                    if ((nfMarkSeg & QCD_FOUND) != 0)
                    {
                        throw new CorruptedCodestreamException("More than one QCD " + "marker " + "found in main header");
                    }
                    nfMarkSeg |= QCD_FOUND;
                    htKey = "QCD";
                    break;

                case CSJ2K.j2k.codestream.Markers.QCC:
                    nfMarkSeg |= QCC_FOUND;
                    htKey = "QCC" + (nQCCMarkSeg++);
                    break;

                case CSJ2K.j2k.codestream.Markers.RGN:
                    nfMarkSeg |= RGN_FOUND;
                    htKey = "RGN" + (nRGNMarkSeg++);
                    break;

                case CSJ2K.j2k.codestream.Markers.COM:
                    nfMarkSeg |= COM_FOUND;
                    htKey = "COM" + (nCOMMarkSeg++);
                    break;

                case CSJ2K.j2k.codestream.Markers.CRG:
                    if ((nfMarkSeg & CRG_FOUND) != 0)
                    {
                        throw new CorruptedCodestreamException("More than one CRG " + "marker " + "found in main header");
                    }
                    nfMarkSeg |= CRG_FOUND;
                    htKey = "CRG";
                    break;

                case CSJ2K.j2k.codestream.Markers.PPM:
                    nfMarkSeg |= PPM_FOUND;
                    htKey = "PPM" + (nPPMMarkSeg++);
                    break;

                case CSJ2K.j2k.codestream.Markers.TLM:
                    if ((nfMarkSeg & TLM_FOUND) != 0)
                    {
                        throw new CorruptedCodestreamException("More than one TLM " + "marker " + "found in main header");
                    }
                    nfMarkSeg |= TLM_FOUND;
                    break;

                case CSJ2K.j2k.codestream.Markers.PLM:
                    if ((nfMarkSeg & PLM_FOUND) != 0)
                    {
                        throw new CorruptedCodestreamException("More than one PLM " + "marker " + "found in main header");
                    }
                    FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "PLM marker segment found but " + "not used by by JJ2000 decoder.");
                    nfMarkSeg |= PLM_FOUND;
                    htKey = "PLM";
                    break;

                case CSJ2K.j2k.codestream.Markers.POC:
                    if ((nfMarkSeg & POC_FOUND) != 0)
                    {
                        throw new CorruptedCodestreamException("More than one POC " + "marker segment found " + "in main header");
                    }
                    nfMarkSeg |= POC_FOUND;
                    htKey = "POC";
                    break;

                case CSJ2K.j2k.codestream.Markers.PLT:
                    throw new CorruptedCodestreamException("PLT found in main header");

                case CSJ2K.j2k.codestream.Markers.PPT:
                    throw new CorruptedCodestreamException("PPT found in main header");

                default:
                    htKey = "UNKNOWN";
                    FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, "Non recognized marker segment (0x" + System.Convert.ToString(marker, 16) + ") in main header!");
                    break;

            }

            if (marker < unchecked((short)0xffffff30) || marker > unchecked((short)0xffffff3f))
            {
                // Read marker segment length and create corresponding byte buffer
                int markSegLen = ehs.readUnsignedShort();
                byte[] buf = new byte[markSegLen];

                // Copy data (after re-insertion of the marker segment length);
                buf[0] = (byte) ((markSegLen >> 8) & 0xFF);
                buf[1] = (byte) (markSegLen & 0xFF);
                ehs.readFully(buf, 2, markSegLen - 2);

                if (!htKey.Equals("UNKNOWN"))
                {
                    // Store array in hashTable
                    ht[htKey] = buf;
                }
            }
        }