// 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.Globalization;
using System.Runtime.InteropServices;
namespace SharpDX.Multimedia
{
///
/// A FourCC descriptor.
///
[StructLayout(LayoutKind.Sequential, Size = 4)]
public struct FourCC : IEquatable, IFormattable
{
///
/// Empty FourCC.
///
public static readonly FourCC Empty = new FourCC(0);
private uint value;
///
/// Initializes a new instance of the struct.
///
/// The fourCC value as a string .
public FourCC(string fourCC)
{
if (fourCC.Length != 4)
throw new ArgumentException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Invalid length for FourCC(\"{0}\". Must be be 4 characters long ", fourCC), "fourCC");
this.value = ((uint)fourCC[3]) << 24 | ((uint)fourCC[2]) << 16 | ((uint)fourCC[1]) << 8 | ((uint)fourCC[0]);
}
///
/// Initializes a new instance of the struct.
///
/// The byte1.
/// The byte2.
/// The byte3.
/// The byte4.
public FourCC(char byte1, char byte2, char byte3, char byte4)
{
this.value = ((uint)byte4) << 24 | ((uint)byte3) << 16 | ((uint)byte2) << 8 | ((uint)byte1);
}
///
/// Initializes a new instance of the struct.
///
/// The fourCC value as an uint.
public FourCC(uint fourCC)
{
this.value = fourCC;
}
///
/// Initializes a new instance of the struct.
///
/// The fourCC value as an int.
public FourCC(int fourCC)
{
this.value = unchecked((uint)fourCC);
}
///
/// Performs an implicit conversion from to .
///
/// The d.
///
/// The result of the conversion.
///
public static implicit operator uint(FourCC d)
{
return d.value;
}
///
/// Performs an implicit conversion from to .
///
/// The d.
///
/// The result of the conversion.
///
public static implicit operator int(FourCC d)
{
return unchecked((int)d.value);
}
///
/// Performs an implicit conversion from to .
///
/// The d.
///
/// The result of the conversion.
///
public static implicit operator FourCC(uint d)
{
return new FourCC(d);
}
///
/// Performs an implicit conversion from to .
///
/// The d.
///
/// The result of the conversion.
///
public static implicit operator FourCC(int d)
{
return new FourCC(d);
}
///
/// Performs an implicit conversion from to .
///
/// The d.
///
/// The result of the conversion.
///
public static implicit operator string(FourCC d)
{
return d.ToString();
}
///
/// Performs an implicit conversion from to .
///
/// The d.
///
/// The result of the conversion.
///
public static implicit operator FourCC(string d)
{
return new FourCC(d);
}
public override string ToString()
{
return string.Format("{0}", new string(new[]
{
(char) (value & 0xFF),
(char) ((value >> 8) & 0xFF),
(char) ((value >> 16) & 0xFF),
(char) ((value >> 24) & 0xFF),
}));
}
public bool Equals(FourCC other)
{
return value == other.value;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is FourCC && Equals((FourCC) obj);
}
public override int GetHashCode()
{
return (int) value;
}
///
/// Provides a custom string representation of the FourCC descriptor.
///
///
/// The general format "G" is equivalent to the parameterless.
/// . The special format "I" returns a
/// string representation which can be used to construct a Media
/// Foundation format GUID. It is equivalent to "X08".
///
/// The format descriptor, which can be "G" (empty
/// or null is equivalent to "G"), "I" or any valid standard
/// number format.
/// The format provider for formatting
/// numbers.
/// The requested string representation.
/// In case of
/// is not "G", "I" or a valid number
/// format.
public string ToString(string format, IFormatProvider formatProvider)
{
if (string.IsNullOrEmpty(format))
{
format = "G";
}
if (formatProvider == null)
{
formatProvider = CultureInfo.CurrentCulture;
}
switch (format.ToUpperInvariant())
{
case "G":
return this.ToString();
case "I":
return this.value.ToString("X08", formatProvider);
default:
return this.value.ToString(format, formatProvider);
}
}
public static bool operator ==(FourCC left, FourCC right)
{
return left.Equals(right);
}
public static bool operator !=(FourCC left, FourCC right)
{
return !left.Equals(right);
}
}
}