// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
namespace SharpDX.Multimedia
{
///
/// A chunk of a Riff stream.
///
public class RiffChunk
{
///
/// Initializes a new instance of the class.
///
/// The stream holding this chunk
/// The type.
/// The size.
/// The data offset.
/// if set to true [is list].
/// if set to true [is header].
public RiffChunk(Stream stream, FourCC type, uint size, uint dataPosition, bool isList = false, bool isHeader = false)
{
Stream = stream;
Type = type;
Size = size;
DataPosition = dataPosition;
IsList = isList;
IsHeader = isHeader;
}
///
/// Gets the type.
///
public Stream Stream{ get; private set; }
///
/// Gets the of this chunk.
///
public FourCC Type { get; private set; }
///
/// Gets the size of the data embedded by this chunk.
///
public uint Size { get; private set; }
///
/// Gets the position of the data embedded by this chunk relative to the stream.
///
public uint DataPosition { get; private set; }
///
/// Gets or sets a value indicating whether this instance is a list chunk.
///
///
/// true if this instance is list; otherwise, false.
///
public bool IsList { get; private set; }
///
/// Gets a value indicating whether this instance is a header chunk.
///
///
/// true if this instance is a header; otherwise, false.
///
public bool IsHeader { get; private set; }
///
/// Gets the raw data contained in this chunk.
///
///
public byte[] GetData()
{
var data = new byte[Size];
Stream.Position = DataPosition;
Stream.Read(data, 0, (int)Size);
return data;
}
///
/// Gets structured data contained in this chunk.
///
/// The type of the data to return
///
/// A structure filled with the chunk data
///
public unsafe T GetDataAs() where T : struct
{
var value = new T();
var data = GetData();
fixed (void* ptr = data)
{
Utilities.Read((IntPtr) ptr, ref value);
}
return value;
}
///
/// Gets structured data contained in this chunk.
///
/// The type of the data to return
/// A structure filled with the chunk data
public unsafe T[] GetDataAsArray() where T : struct
{
int sizeOfT = Utilities.SizeOf();
if ((Size % sizeOfT) != 0)
throw new ArgumentException("Size of T is incompatible with size of chunk");
var values = new T[Size/sizeOfT];
var data = GetData();
fixed (void* ptr = data)
{
Utilities.Read((IntPtr)ptr, values, 0, values.Length);
}
return values;
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return string.Format(System.Globalization.CultureInfo.InvariantCulture, "Type: {0}, Size: {1}, Position: {2}, IsList: {3}, IsHeader: {4}", Type, Size, DataPosition, IsList, IsHeader);
}
}
}