예제 #1
0
        /// <summary>
        /// Seek in BinaryFile until pattern is found, return start index
        /// </summary>
        /// <param name="binaryFile">binaryFile</param>
        /// <param name="pattern">byte pattern to find</param>
        /// <param name="offset">offset to seek to (if > 0)</param>
        /// <param name="maxEndIndex">optional end index within the BinaryFile</param>
        /// <returns>index where found (BinaryFile position will be at this index)</returns>
        public static int IndexOf(this BinaryFile binaryFile, byte[] pattern, int offset, int maxEndIndex = -1)
        {
            // seek to offset
            if (offset > 0 && offset < binaryFile.Length)
            {
                binaryFile.Seek(offset, SeekOrigin.Begin);
            }

            int  success        = 0;
            long remainingBytes = binaryFile.Length - binaryFile.Position;

            if (maxEndIndex > 0)
            {
                remainingBytes = Math.Min(maxEndIndex, remainingBytes);
            }

            for (int i = 0; i < (int)remainingBytes; i++)
            {
                var b = binaryFile.ReadByte();
                if (b == pattern[success])
                {
                    success++;
                }
                else
                {
                    success = 0;
                }

                if (pattern.Length == success)
                {
                    int index = (int)(binaryFile.Position - pattern.Length);
                    binaryFile.Seek(index, SeekOrigin.Begin);
                    return(index);
                }
            }
            return(-1);
        }
예제 #2
0
    public static double[][] BMPRead(BinaryFile bmpfile, ref int y, ref int x)
    {
        int iy = 0; // various iterators
        int ix = 0;
        int ic = 0;
        int offset = 0;
        double[][] image;
        byte zerobytes = new byte();
        byte val = new byte();

        #if DEBUG
        Console.Write("BMPRead...\n");
        #endif

        if (GlobalMembersUtil.ReadUInt16(bmpfile) != 19778) // "BM" format tag check
        {
            Console.Error.WriteLine("This file is not in BMP format\n");
            GlobalMembersUtil.WinReturn();
            Environment.Exit(1);
        }

        bmpfile.Seek(8, SeekOrigin.Current); // skipping useless tags

        offset = (int) GlobalMembersUtil.ReadUInt32(bmpfile) - 54; // header offset

        bmpfile.Seek(4, SeekOrigin.Current); // skipping useless tags

        x = (int) GlobalMembersUtil.ReadUInt32(bmpfile);
        y = (int) GlobalMembersUtil.ReadUInt32(bmpfile);

        bmpfile.Seek(2, SeekOrigin.Current); // skipping useless tags

        if (GlobalMembersUtil.ReadUInt16(bmpfile) != 24) // Only format supported
        {
            Console.Error.WriteLine("Wrong BMP format, BMP images must be in 24-bit colour\n");
            GlobalMembersUtil.WinReturn();
            Environment.Exit(1);
        }

        bmpfile.Seek(24+offset, SeekOrigin.Current); // skipping useless tags

        // image allocation
        image = new double[y][];

        for (iy = 0; iy< y; iy++) {
            image[iy] = new double[x];
        }

        zerobytes = (byte)(4 - ((x *3) & 3));
        if (zerobytes == 4) {
            zerobytes = 0;
        }

        for (iy = y-1; iy!=-1; iy--) { // backwards reading
            for (ix = 0; ix< x; ix++) {
                for (ic = 2;ic!=-1;ic--) {
                    val = bmpfile.ReadByte();
                    image[iy][ix] += (double) val * (1.0/(255.0 * 3.0)); // Conversion to grey by averaging the three channels
                }
            }

            bmpfile.Seek(zerobytes, SeekOrigin.Current); // skipping padding bytes
        }

        bmpfile.Close();
        return image;
    }
예제 #3
0
        public static double[][] ReadBMPGrayscale(string filePath)
        {
            double[][] image;
            var        bmpfile = new BinaryFile(filePath);

            // "BM" format tag check
            if (!"BM".Equals(bmpfile.ReadString(2)))
            {
                Console.Error.WriteLine("This file is not in BMP format");
                return(null);
            }

            bmpfile.Seek(8, SeekOrigin.Current);             // skipping useless tags

            int offset = (int)bmpfile.ReadUInt32() - 54;     // header offset

            bmpfile.Seek(4, SeekOrigin.Current);             // skipping useless tags

            int x = (int)bmpfile.ReadUInt32();
            int y = (int)bmpfile.ReadUInt32();

            bmpfile.Seek(2, SeekOrigin.Current);             // skipping useless tags

            int bitDepth = bmpfile.ReadUInt16();
            int numBytes = bitDepth / 8;

            bmpfile.Seek(24 + offset, SeekOrigin.Current);           // skipping useless tags

            // image allocation
            image = new double[y][];

            // initialise jagged array
            for (int iy = 0; iy < y; iy++)
            {
                image[iy] = new double[x];
            }

            // calculate zero bytes (bytes to skip at the end of each bitmap line)
            int zerobytes = (byte)(4 - ((x * numBytes) & numBytes));

            if (zerobytes == 4)
            {
                zerobytes = 0;
            }

            // backwards reading
            for (int iy = y - 1; iy != -1; iy--)
            {
                for (int ix = 0; ix < x; ix++)
                {
                    for (int ic = numBytes - 1; ic != -1; ic--)
                    {
                        int val = bmpfile.ReadByte();
                        if (ic == 0 && numBytes == 4)
                        {
                            // if reading 32 bit, ignore the alpha bit
                        }
                        else
                        {
                            // Conversion to grey by averaging the three channels
                            image[iy][ix] += (double)val * (1.0 / (255.0 * 3.0));
                        }
                    }
                }

                bmpfile.Seek(zerobytes, SeekOrigin.Current);                 // skipping padding bytes
            }

            bmpfile.Close();
            return(image);
        }
예제 #4
0
파일: SdirPreset.cs 프로젝트: perivar/Code
        public bool Read(string filePath)
        {
            if (File.Exists(filePath)) {
                string fileName = Path.GetFileNameWithoutExtension(filePath);
                PresetName = fileName;

                BinaryFile bFile = new BinaryFile(filePath, BinaryFile.ByteOrder.BigEndian);

                string firstChunkID = bFile.ReadString(4); // chunk ID	= "FORM"
                int ckSize = bFile.ReadInt32();
                string formType = bFile.ReadString(4);

                // read first data chunk
                string chunkID = bFile.ReadString(4);

                // if chunkID == "COMT" then CommentsChunk
                if (chunkID.Equals("COMT")) {
                    long curposTmpComt = bFile.GetPosition();
                    bFile.Seek(curposTmpComt-4);

                    // CommentsChunk
                    string chunkIDComt = bFile.ReadString(4); // chunk ID	= "COMT"
                    int chunkSizeComt = bFile.ReadInt32();
                    int numComments = bFile.ReadUInt16();
                    long curposTmpComt2 = bFile.GetPosition();

                    //comments = bFile.ReadString(chunkSizeComt-2);
                    for (int i = 0; i < numComments; i++) {
                        int commentTimestamp = (int) bFile.ReadUInt32();
                        string marker = bFile.ReadString(4);
                        int count = (int) bFile.ReadByte();
                        comments.Add(bFile.ReadString(count));
                    }

                    bFile.Seek(curposTmpComt2+chunkSizeComt-2);
                }

                string chunkID2 = bFile.ReadString(4);

                // if chunkID2 == "COMM" then CommonChunk
                if (chunkID2.Equals("COMM")) {
                    long curposTmpComm = bFile.GetPosition();
                    bFile.Seek(curposTmpComm-4);

                    // CommonChunk
                    string chunkIDComm = bFile.ReadString(4); // chunk ID = "COMM"
                    int chunkSizeComm = bFile.ReadInt32();

                    channels = bFile.ReadInt16();
                    numSampleFrames = (int) bFile.ReadUInt32();
                    bitsPerSample = bFile.ReadInt16();

                    // read IEEE 80-bit extended double precision
                    byte[] sampleRateBytes = bFile.ReadBytes(0, 10, BinaryFile.ByteOrder.LittleEndian);
                    double sampleRateDouble = NAudio.Utils.IEEE.ConvertFromIeeeExtended(sampleRateBytes);
                    sampleRate = (int) sampleRateDouble;
                }

                string chunkID3 = bFile.ReadString(4);

                // if chunkID3 == "SSND" then SoundDataChunk
                if (chunkID3.Equals("SSND")) {
                    long curposTmpSsnd = bFile.GetPosition();
                    bFile.Seek(curposTmpSsnd-4);

                    // SoundDataChunk
                    string chunkIDSsnd = bFile.ReadString(4); // chunk ID = "SSND"
                    int chunkSizeSsnd = bFile.ReadInt32();

                    int offset = (int) bFile.ReadUInt32();
                    int blocksize = (int) bFile.ReadUInt32();
                    byte[] data = bFile.ReadBytes(offset, chunkSizeSsnd-8, BinaryFile.ByteOrder.LittleEndian);

                    // swap waveform data
                    WaveformData = SwapAiffEndian(data);
                }

                bFile.Close();
                return true;

            } else {
                return false;
            }
        }