|
| 1 | +// Copyright (c) Amer Koleci and contributors. |
| 2 | +// Distributed under the MIT license. See the LICENSE file in the project root for more information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Globalization; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | + |
| 8 | +namespace Vortice |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// A FourCC descriptor. |
| 12 | + /// </summary> |
| 13 | + [StructLayout(LayoutKind.Sequential, Size = 4)] |
| 14 | + public readonly struct FourCC : IEquatable<FourCC>, IFormattable |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Empty FourCC. |
| 18 | + /// </summary> |
| 19 | + public static readonly FourCC Empty = new(0); |
| 20 | + |
| 21 | + private readonly uint _value; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes a new instance of the <see cref="FourCC" /> struct. |
| 25 | + /// </summary> |
| 26 | + /// <param name="fourCC">The fourCC value as a string .</param> |
| 27 | + public FourCC(string fourCC) |
| 28 | + { |
| 29 | + if (fourCC.Length != 4) |
| 30 | + { |
| 31 | + throw new ArgumentException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Invalid length for FourCC(\"{0}\". Must be be 4 characters long ", fourCC), "fourCC"); |
| 32 | + } |
| 33 | + |
| 34 | + _value = ((uint)fourCC[3]) << 24 | ((uint)fourCC[2]) << 16 | ((uint)fourCC[1]) << 8 | ((uint)fourCC[0]); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Initializes a new instance of the <see cref="FourCC" /> struct. |
| 39 | + /// </summary> |
| 40 | + /// <param name="byte1">The byte1.</param> |
| 41 | + /// <param name="byte2">The byte2.</param> |
| 42 | + /// <param name="byte3">The byte3.</param> |
| 43 | + /// <param name="byte4">The byte4.</param> |
| 44 | + public FourCC(char byte1, char byte2, char byte3, char byte4) |
| 45 | + { |
| 46 | + _value = ((uint)byte4) << 24 | ((uint)byte3) << 16 | ((uint)byte2) << 8 | ((uint)byte1); |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Initializes a new instance of the <see cref="FourCC" /> struct. |
| 51 | + /// </summary> |
| 52 | + /// <param name="fourCC">The fourCC value as an uint.</param> |
| 53 | + public FourCC(uint fourCC) |
| 54 | + { |
| 55 | + _value = fourCC; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Initializes a new instance of the <see cref="FourCC" /> struct. |
| 60 | + /// </summary> |
| 61 | + /// <param name="fourCC">The fourCC value as an int.</param> |
| 62 | + public FourCC(int fourCC) |
| 63 | + { |
| 64 | + _value = unchecked((uint)fourCC); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Performs an implicit conversion from <see cref="FourCC"/> to <see cref="int"/>. |
| 69 | + /// </summary> |
| 70 | + /// <param name="d">The d.</param> |
| 71 | + /// <returns> |
| 72 | + /// The result of the conversion. |
| 73 | + /// </returns> |
| 74 | + public static implicit operator uint(FourCC d) |
| 75 | + { |
| 76 | + return d._value; |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Performs an implicit conversion from <see cref="FourCC"/> to <see cref="int"/>. |
| 81 | + /// </summary> |
| 82 | + /// <param name="d">The d.</param> |
| 83 | + /// <returns> |
| 84 | + /// The result of the conversion. |
| 85 | + /// </returns> |
| 86 | + public static implicit operator int(FourCC d) |
| 87 | + { |
| 88 | + return unchecked((int)d._value); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Performs an implicit conversion from <see cref="int"/> to <see cref="FourCC"/>. |
| 93 | + /// </summary> |
| 94 | + /// <param name="d">The d.</param> |
| 95 | + /// <returns> |
| 96 | + /// The result of the conversion. |
| 97 | + /// </returns> |
| 98 | + public static implicit operator FourCC(uint d) |
| 99 | + { |
| 100 | + return new FourCC(d); |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Performs an implicit conversion from <see cref="int"/> to <see cref="FourCC"/>. |
| 105 | + /// </summary> |
| 106 | + /// <param name="d">The d.</param> |
| 107 | + /// <returns> |
| 108 | + /// The result of the conversion. |
| 109 | + /// </returns> |
| 110 | + public static implicit operator FourCC(int d) |
| 111 | + { |
| 112 | + return new FourCC(d); |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Performs an implicit conversion from <see cref="FourCC"/> to <see cref="string"/>. |
| 117 | + /// </summary> |
| 118 | + /// <param name="value">The <see cref="FourCC"/> value.</param> |
| 119 | + /// <returns> |
| 120 | + /// The result of the conversion. |
| 121 | + /// </returns> |
| 122 | + public static implicit operator string(FourCC value) => value.ToString(); |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Performs an implicit conversion from <see cref="string"/> to <see cref="FourCC"/>. |
| 126 | + /// </summary> |
| 127 | + /// <param name="d">The d.</param> |
| 128 | + /// <returns> |
| 129 | + /// The result of the conversion. |
| 130 | + /// </returns> |
| 131 | + public static implicit operator FourCC(string d) => new(d); |
| 132 | + |
| 133 | + public override string ToString() |
| 134 | + { |
| 135 | + return string.Format("{0}", new string(new[] |
| 136 | + { |
| 137 | + (char) (_value & 0xFF), |
| 138 | + (char) ((_value >> 8) & 0xFF), |
| 139 | + (char) ((_value >> 16) & 0xFF), |
| 140 | + (char) ((_value >> 24) & 0xFF), |
| 141 | + })); |
| 142 | + } |
| 143 | + |
| 144 | + public bool Equals(FourCC other) => _value == other._value; |
| 145 | + |
| 146 | + /// <inheritdoc/> |
| 147 | + public override bool Equals(object? obj) => obj is FourCC value && Equals(value); |
| 148 | + |
| 149 | + public override int GetHashCode() => (int)_value; |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Provides a custom string representation of the FourCC descriptor. |
| 153 | + /// </summary> |
| 154 | + /// <remarks> |
| 155 | + /// The general format "G" is equivalent to the parameterless. |
| 156 | + /// <see cref="FourCC.ToString()"/>. The special format "I" returns a |
| 157 | + /// string representation which can be used to construct a Media |
| 158 | + /// Foundation format GUID. It is equivalent to "X08". |
| 159 | + /// </remarks> |
| 160 | + /// <param name="format">The format descriptor, which can be "G" (empty |
| 161 | + /// or <c>null</c> is equivalent to "G"), "I" or any valid standard |
| 162 | + /// number format.</param> |
| 163 | + /// <param name="formatProvider">The format provider for formatting |
| 164 | + /// numbers.</param> |
| 165 | + /// <returns>The requested string representation.</returns> |
| 166 | + /// <exception cref="System.FormatException">In case of |
| 167 | + /// <paramref name="format"/> is not "G", "I" or a valid number |
| 168 | + /// format.</exception> |
| 169 | + public string ToString(string format, IFormatProvider formatProvider) |
| 170 | + { |
| 171 | + if (string.IsNullOrEmpty(format)) |
| 172 | + { |
| 173 | + format = "G"; |
| 174 | + } |
| 175 | + if (formatProvider == null) |
| 176 | + { |
| 177 | + formatProvider = CultureInfo.CurrentCulture; |
| 178 | + } |
| 179 | + |
| 180 | + return format.ToUpperInvariant() switch |
| 181 | + { |
| 182 | + "G" => ToString(), |
| 183 | + "I" => _value.ToString("X08", formatProvider), |
| 184 | + _ => _value.ToString(format, formatProvider), |
| 185 | + }; |
| 186 | + } |
| 187 | + |
| 188 | + public static bool operator ==(FourCC left, FourCC right) => left.Equals(right); |
| 189 | + |
| 190 | + public static bool operator !=(FourCC left, FourCC right) => !left.Equals(right); |
| 191 | + } |
| 192 | +} |
0 commit comments