-
Notifications
You must be signed in to change notification settings - Fork 0
/
FourCC.cs
135 lines (122 loc) · 4.51 KB
/
FourCC.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Taken from Vasili Maslov's SharpAvi repo.
https://github.com/bobahml/SharpAvi/
*/
using System.Diagnostics.Contracts;
using System.Linq;
public struct FourCC
{
private readonly uint valueDWord;
private readonly string valueString;
/// <summary>
/// Creates a new instance of <see cref="FourCC"/> with an integer value.
/// </summary>
/// <param name="value">Integer value of FOURCC.</param>
public FourCC(uint value)
{
valueDWord = value;
valueString = new string
(
new[]
{
(char)(value & 0xFF),
(char)((value & 0xFF00) >> 8),
(char)((value & 0xFF0000) >> 16),
(char)((value & 0xFF000000U) >> 24)
}
);
}
/// <summary>
/// Creates a new instance of <see cref="FourCC"/> with a string value.
/// </summary>
/// <param name="value">
/// String value of FOURCC.
/// Should be not longer than 4 characters, all of them are printable ASCII characters.
/// </param>
/// <remarks>
/// If the value of <paramref name="value"/> is shorter than 4 characters, it is right-padded with spaces.
/// </remarks>
public FourCC(string value)
{
Contract.Requires(value != null);
Contract.Requires(value.Length <= 4);
// Allow only printable ASCII characters
Contract.Requires(Contract.ForAll(value, c => ' ' <= c && c <= '~'));
valueString = value.PadRight(4);
valueDWord = (uint)valueString[0] + ((uint)valueString[1] << 8) + ((uint)valueString[2] << 16) + ((uint)valueString[3] << 24);
}
/// <summary>
/// Returns string representation of this instance.
/// </summary>
/// <returns>
/// String value if all bytes are printable ASCII characters. Otherwise, the hexadecimal representation of integer value.
/// </returns>
public override string ToString()
{
var isPrintable = valueString.All(c => ' ' <= c && c <= '~');
return isPrintable ? valueString : valueDWord.ToString("X8");
}
/// <summary>
/// Gets hash code of this instance.
/// </summary>
public override int GetHashCode()
{
return valueDWord.GetHashCode();
}
/// <summary>
/// Determines whether this instance is equal to other object.
/// </summary>
public override bool Equals(object obj)
{
if (obj is FourCC)
{
return (FourCC)obj == this;
}
else
{
return base.Equals(obj);
}
}
/// <summary>
/// Converts an integer value to <see cref="FourCC"/>.
/// </summary>
public static implicit operator FourCC(uint value)
{
return new FourCC(value);
}
/// <summary>
/// Converts a string value to <see cref="FourCC"/>.
/// </summary>
public static implicit operator FourCC(string value)
{
return new FourCC(value);
}
/// <summary>
/// Gets the integer value of <see cref="FourCC"/> instance.
/// </summary>
public static explicit operator uint(FourCC value)
{
return value.valueDWord;
}
/// <summary>
/// Gets the string value of <see cref="FourCC"/> instance.
/// </summary>
public static explicit operator string(FourCC value)
{
return value.valueString;
}
/// <summary>
/// Determines whether two instances of <see cref="FourCC"/> are equal.
/// </summary>
public static bool operator ==(FourCC value1, FourCC value2)
{
return value1.valueDWord == value2.valueDWord;
}
/// <summary>
/// Determines whether two instances of <see cref="FourCC"/> are not equal.
/// </summary>
public static bool operator !=(FourCC value1, FourCC value2)
{
return !(value1 == value2);
}
}