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); }
/** * Reads file header into the struct. * * @param fs input file stream * @see WaveFile::hdr */ private void LoadHeader(ref BinaryFile fs) { hdr.RIFF = fs.ReadString(4); hdr.DataLength = fs.ReadUInt32(); hdr.WAVE = fs.ReadString(4); hdr.fmt_ = fs.ReadString(4); hdr.SubBlockLength = fs.ReadUInt32(); hdr.formatTag = fs.ReadUInt16(); hdr.Channels = fs.ReadUInt16(); hdr.SampFreq = fs.ReadUInt32(); hdr.BytesPerSec = fs.ReadUInt32(); hdr.BytesPerSamp = fs.ReadUInt16(); hdr.BitsPerSamp = fs.ReadUInt16(); hdr.data = fs.ReadString(4); hdr.WaveSize = fs.ReadUInt32(); }
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; } }
// read from file a 32-bit integer in little endian public static UInt32 ReadUInt32(BinaryFile file) { return file.ReadUInt32(); }