CSJ2K.j2k.util.CodestreamManipulator.parseAndFind C# (CSharp) Method

parseAndFind() private method

This method parses the codestream for SOT, SOP and EPH markers and removes header header bits signalling SOP and EPH markers if packed packet headers are used
If an I/O error ocurred. /// ///
private parseAndFind ( BufferedRandomAccessFile fi ) : void
fi CSJ2K.j2k.io.BufferedRandomAccessFile The file to parse the markers from /// ///
return void
        private void parseAndFind(BufferedRandomAccessFile fi)
        {
            int length, pos, i, t, sop = 0, eph = 0;
            short marker;
            int halfMarker;
            int tileEnd;
            System.Collections.Generic.List<System.Int32> markPos = new List<int>(10);

            // Find position of first SOT marker
            marker = (short) fi.readUnsignedShort(); // read SOC marker
            marker = (short) fi.readUnsignedShort();
            while (marker != CSJ2K.j2k.codestream.Markers.SOT)
            {
                pos = fi.Pos;
                length = fi.readUnsignedShort();

                // If SOP and EPH markers were only used for parsing in this
                // class remove SOP and EPH markers from Scod field
                if (marker == CSJ2K.j2k.codestream.Markers.COD)
                {
                    int scod = fi.readUnsignedByte();
                    if (tempSop)
                        scod &= 0xfd; // Remove bits indicating SOP
                    if (tempEph)
                        scod &= 0xfb; // Remove bits indicating SOP
                    fi.seek(pos + 2);
                    fi.write(scod);
                }

                fi.seek(pos + length);
                marker = (short) fi.readUnsignedShort();
            }
            pos = fi.Pos;
            fi.seek(pos - 2);

            // Find all packet headers, packed data and tile headers
            for (t = 0; t < nt; t++)
            {
                // Read SOT marker
                fi.readUnsignedShort(); // Skip SOT
                pos = fi.Pos;
                markPos.Add((System.Int32) fi.Pos);
                fi.readInt(); // Skip Lsot and Isot
                length = fi.readInt(); // Read Psot
                fi.readUnsignedShort(); // Skip TPsot & TNsot
                tileEnd = pos + length - 2; // Last byte of tile

                // Find position of SOD marker
                marker = (short) fi.readUnsignedShort();
                while (marker != CSJ2K.j2k.codestream.Markers.SOD)
                {
                    pos = fi.Pos;
                    length = fi.readUnsignedShort();

                    // If SOP and EPH markers were only used for parsing in this
                    // class remove SOP and EPH markers from Scod field
                    if (marker == CSJ2K.j2k.codestream.Markers.COD)
                    {
                        int scod = fi.readUnsignedByte();
                        if (tempSop)
                            scod &= 0xfd; // Remove bits indicating SOP
                        if (tempEph)
                            scod &= 0xfb; // Remove bits indicating SOP
                        fi.seek(pos + 2);
                        fi.write(scod);
                    }
                    fi.seek(pos + length);
                    marker = (short) fi.readUnsignedShort();
                }

                // Find all SOP and EPH markers in tile
                sop = 0;
                eph = 0;

                i = fi.Pos;
                while (i < tileEnd)
                {
                    halfMarker = (short) fi.readUnsignedByte();
                    if (halfMarker == (short) 0xff)
                    {
                        marker = (short) ((halfMarker << 8) + fi.readUnsignedByte());
                        i++;
                        if (marker == CSJ2K.j2k.codestream.Markers.SOP)
                        {
                            markPos.Add((System.Int32) fi.Pos);
                            ppt[t]++;
                            sop++;
                            fi.skipBytes(4);
                            i += 4;
                        }

                        if (marker == CSJ2K.j2k.codestream.Markers.EPH)
                        {
                            markPos.Add((System.Int32) fi.Pos);
                            eph++;
                        }
                    }
                    i++;
                }
            }
            markPos.Add((System.Int32) (fi.Pos + 2));
            positions = new System.Int32[markPos.Count];
            markPos.CopyTo(positions);
        }