forked from esteevens/MatlabFileDotNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatlabFileHelper.cs
More file actions
253 lines (222 loc) · 8.02 KB
/
MatlabFileHelper.cs
File metadata and controls
253 lines (222 loc) · 8.02 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ionic.Zlib;
using System.Reflection;
namespace MatlabFileIO
{
public class Variable
{
public Type dataType;
public String name;
public object data;
public object Image;
}
internal enum MatfileVersion
{
MATFILE_5
};
internal class Tag
{
public Type dataType;
public UInt32 length;
public object data; //In case of small data format, otherwise null
}
internal class Flag
{
public Tag Tag;
public bool Complex = false;
public bool Global = false;
public bool Logical = false;
public Type dataClass;
}
internal class Header
{
public String text;
public MatfileVersion version;
public Header(byte[] bytes)
{
if (bytes.Length != 128)
throw new IOException("Matlab header should be 128 charachters");
char[] textChars = new char[116];
Array.Copy(bytes, textChars, 116);
this.text = new String(textChars);
ushort version = (ushort)(bytes[125] << 8 + bytes[124]);
if (version != 0x0100)
throw new IOException("Unsupported version of matlab file");
this.version = MatfileVersion.MATFILE_5;
}
}
public static class MatfileIO
{
public static bool IsTypeSupported(Type t)
{
return t != null && MatfileHelper.ArrayTypes.Contains(t);
}
}
internal static class MatfileHelper
{
public const int SZ_TAG = 8; //Tag size in bytes
public static Type[] DataType = new Type[]
{
null, //0
typeof(SByte), //1
typeof(Byte), //2
typeof(Int16), //3
typeof(UInt16), //4
typeof(Int32), //5
typeof(UInt32), //6
typeof(Single), //7
null, //8 - reserved
typeof(Double), //9
null, //10 - reserved
null, //11 - reserved
typeof(Int64), //12
typeof(UInt64), //13
typeof(Array), //14 MiMatrix
typeof(ZlibStream), //15 Compressed Zlib data
null, //16 UTF-8 - not supported
null, //17 UTF-16 - not supported
null //18 UTF-32 - not supported
};
public static Tag ReadTag(this BinaryReader reader)
{
byte[] bytes = reader.ReadBytes(SZ_TAG);
Tag t = new Tag();
t.dataType = DataType[BitConverter.ToInt16(bytes, 0)];
if (BitConverter.ToUInt16(bytes, 2) != 0) //Small tag fmt
{
t.length = BitConverter.ToUInt16(bytes, 2);
t.data = CastToMatlabType(t.dataType, bytes, 4, (int)t.length);
}
else
{
t.length = BitConverter.ToUInt32(bytes, 4);
}
return t;
}
public static Flag ReadFlag(this BinaryReader reader)
{
Flag f = new Flag() { Complex = false, Global = false, Logical = false };
f.Tag = reader.ReadTag();
UInt32 flagsClass = reader.ReadUInt32();
byte flags = (byte)(flagsClass >> 8);
if ((flags & 0x08) == 0x08)
f.Complex = true;
if ((flags & 0x04) == 0x04)
f.Global = true;
if ((flags & 0x02) == 0x02)
f.Logical = true;
f.dataClass = MatfileHelper.parseArrayType((byte)flagsClass);
reader.ReadUInt32();//unused flags
//Flag f = matrixStream.ReadFlag();
return f;
}
public static void AdvanceTo8ByteBoundary(this BinaryReader r)
{
long offset = (8 - (r.BaseStream.Position % 8)) % 8;
r.BaseStream.Seek(offset, SeekOrigin.Current);
}
public static int AdvanceTo8ByteBoundary(this BinaryWriter w, byte stuffing = 0x00)
{
long offset = (8 - (w.BaseStream.Position % 8)) % 8;
for(int i =0; i < offset; i ++)
w.Write(stuffing);
return (int)offset;
}
public static void WriteMatlabHeader(this BinaryWriter writeStream)
{
string descriptiveText = "MATLAB MAT-file v5, Platform: " + Environment.OSVersion.Platform + ", CREATED on: " + DateTime.Now.ToString();
//write text
for (int i = 0; i < descriptiveText.Length; i++)
writeStream.Write(descriptiveText[i]);
//pad to 124 bytes
for (int i = 0; i < 124 - descriptiveText.Length; i++)
writeStream.Write((byte)0);
//write version into 2 bytes
writeStream.Write((short)0x0100);
//write endian indicator
writeStream.Write((byte)'I');
writeStream.Write((byte)'M');
}
public static int MatlabBytesPerType(Type T)
{
switch (Type.GetTypeCode(T))
{
case TypeCode.Double:
return 8;
case TypeCode.Single:
return 4;
case TypeCode.Int32:
case TypeCode.UInt32:
return 4;
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Char:
return 2;
case TypeCode.SByte:
case TypeCode.Byte:
return 1;
default:
throw new NotImplementedException("Writing arrays of type " + Enum.GetName(typeof(TypeCode), T).ToString() + " to .mat file not implemented");
}
}
internal static Type[] ArrayTypes = new Type[] {
null, //0
null, //1
null, //2
null, //3
typeof(Char), //4
null, //5
typeof(Double), //6
typeof(Single), //7
typeof(SByte), //8
typeof(Byte), //9
typeof(Int16), //10
typeof(UInt16), //11
typeof(Int32), //12
typeof(UInt32), //13
typeof(Int64), //14
typeof(UInt64) //15
};
public static Type parseArrayType(byte contentTypeInt)
{
Type t = ArrayTypes[contentTypeInt];
if (t != null) return t;
throw new Exception("Content of array not supported");
}
public static int MatlabDataTypeNumber(Type t)
{
if(t == null)
throw new Exception("Matlab data type can't be null");
int i = Array.IndexOf(DataType, t);
if (i > 0) return i;
throw new NotImplementedException("Arrays of " + t.ToString() + " to .mat file not implemented");
}
public static int MatlabArrayTypeNumber(Type t)
{
int i = Array.IndexOf(ArrayTypes, t);
if (i > 0) return i;
throw new NotImplementedException("Arrays of " + t.ToString() + " to .mat file not implemented");
}
public static Array CastToMatlabType(Type t, byte[] data, int offset = 0, int length = -1)
{
if (length < 0)
length = data.Length - offset;
Array result = Array.CreateInstance(t, length / MatlabBytesPerType(t));
Buffer.BlockCopy(data, offset, result, 0, length);
return result;
}
public static Array SliceRow(this Array array, int row)
{
Array output = Array.CreateInstance(array.GetValue(0,0).GetType(), array.GetLength(1));
for (var i = 0; i < array.GetLength(1); i++)
{
output.SetValue(array.GetValue(new int[] { row, i }), i);
}
return output;
}
}
}