/// <summary> /// Tries to parse the JFIF APP0 header /// See http://en.wikipedia.org/wiki/JFIF /// </summary> private bool TryParseJFIF(byte[] data) { IO.BinaryReader reader = new IO.BinaryReader(new MemoryStream(data)); int length = data.Length + 2; // Data & length if (!(length >= JFIF_FIXED_LENGTH)) { return(false); // Header's too small. } byte[] identifier = new byte[5]; reader.Read(identifier, 0, identifier.Length); if (identifier[0] != JPEGMarker.JFIF_J || identifier[1] != JPEGMarker.JFIF_F || identifier[2] != JPEGMarker.JFIF_I || identifier[3] != JPEGMarker.JFIF_F || identifier[4] != JPEGMarker.X00) { return(false); // Incorrect bytes } majorVersion = reader.ReadByte(); minorVersion = reader.ReadByte(); if (majorVersion != MAJOR_VERSION || (majorVersion == MAJOR_VERSION && minorVersion > MINOR_VERSION)) // changed from < { return(false); // Unsupported version } Units = (UnitType)reader.ReadByte(); if (Units != UnitType.None && Units != UnitType.Inches && Units != UnitType.Centimeters) { return(false); // Invalid units } XDensity = reader.ReadShort(); YDensity = reader.ReadShort(); Xthumbnail = reader.ReadByte(); Ythumbnail = reader.ReadByte(); // 3 * for RGB data int thumbnailLength = 3 * Xthumbnail * Ythumbnail; if (length > JFIF_FIXED_LENGTH && thumbnailLength != length - JFIF_FIXED_LENGTH) { return(false); // Thumbnail fields invalid } if (thumbnailLength > 0) { thumbnail = new byte[thumbnailLength]; if (reader.Read(thumbnail, 0, thumbnailLength) != thumbnailLength) { return(false); // Thumbnail data was missing! } } return(true); }
public void Read(IO.BinaryReader br) { var _with1 = br; m_FormatTag = _with1.ReadInt16; m_nChannels = _with1.ReadInt16; m_SamplesPerSecond = _with1.ReadInt32; m_AvgBytesPerSecond = _with1.ReadInt32; m_BlockAlign = _with1.ReadInt16; m_BitsPerSample = _with1.ReadInt16; }
private void ParseFmt(Riff.RiffReader reader) { Ibasa.IO.BinaryReader binreader = new IO.BinaryReader(reader.Data); FormatTag tag; int channels, bytesPerSecond, blockAlignment, bitsPerSample, size; if (reader.Length < 16) { throw new System.IO.InvalidDataException("Invalid fmt chunk."); } else { tag = (FormatTag)binreader.ReadInt16(); channels = binreader.ReadInt16(); Frequency = binreader.ReadInt32(); bytesPerSecond = binreader.ReadInt32(); blockAlignment = binreader.ReadInt16(); bitsPerSample = binreader.ReadInt16(); } if (reader.Length >= 18) { size = binreader.ReadInt16(); } if (tag == FormatTag.Pcm) { if (bitsPerSample == 8) { Format = new Ibasa.SharpAL.Formats.Pcm8(channels); } else if (bitsPerSample == 16) { Format = new Ibasa.SharpAL.Formats.Pcm16(channels); } else { throw new NotSupportedException(string.Format("PCM {0} bit data is not supported.", bitsPerSample)); } } else { throw new NotSupportedException(string.Format("{0} is not supported.", tag)); } }