-
Notifications
You must be signed in to change notification settings - Fork 2
/
matio.gen4.pas
243 lines (220 loc) · 7.5 KB
/
matio.gen4.pas
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
unit matio.gen4;
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/matio
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Uses
Classes, SysUtils, Types, ZLib, ArrayBld, matio, matio.gen4.float16;
Type
T4GCompression = (cpNone,cpGZip);
T4GMatrixReader = Class(TMatrixReader)
private
FilePrecision: TFloatType;
DecompressionStream: TStream;
BinaryReader: TBinaryReader;
Function ReadFloat16: Float32;
protected
Procedure Read(const CurrentRow: Integer; const Rows: TCustomMatrixRows); override;
public
Constructor Create(const FileName: String); overload;
Destructor Destroy; override;
end;
T4GMatrixWriter = Class(TMatrixWriter)
private
FilePrecision: TFloatType;
CompressionStream: TStream;
BinaryWriter: TBinaryWriter;
Procedure WriteChar(Value: Char);
Procedure WriteByte(Value: Byte);
Procedure WriteUInt16(Value: UInt16);
Procedure WriteUInt32(Value: UInt32);
Procedure WriteFloat16(Value: Float32);
Procedure WriteFloat32(Value: Float32);
Procedure WriteFloat64(Value: Float64);
strict protected
Procedure Write(const CurrentRow: Integer; const Rows: TVirtualMatrixRows); override;
public
Constructor Create(const FileName,FileLabel: String;
const MatrixLabels: array of String;
const Size: Integer;
const Precision: TFloatType = ftFloat32;
const Compression: T4GCompression = cpGzip); overload;
Destructor Destroy; override;
end;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Constructor T4GMatrixReader.Create(const FileName: String);
Var
FileCompression: T4GCompression;
begin
inherited Create(FileName);
// Read header
var HeaderReader := TBinaryReader.Create(FileStream,TEncoding.UTF8);
try
if (HeaderReader.ReadChar = '4') and (HeaderReader.ReadChar = 'G')
and (HeaderReader.ReadByte = 20) and (HeaderReader.ReadByte = 1) then
begin
// Read file header
FilePrecision := TFloatType(HeaderReader.ReadByte);
FileCompression := T4GCompression(HeaderReader.ReadByte);
SetCount(HeaderReader.ReadByte);
SetSize(HeaderReader.ReadUInt16);
HeaderReader.ReadUInt32; // Skip user bytes
var NChar := HeaderReader.ReadByte;
var Lbl := '';
for var Chr := 1 to NChar do Lbl := Lbl + HeaderReader.ReadChar;
SetFileLabel(Lbl);
// Read matrix headers
for var Mtrx := 0 to Count-1 do
begin
HeaderReader.ReadUInt16; // Skip user bytes
NChar := HeaderReader.ReadByte;
Lbl := '';
for var Chr := 1 to NChar do Lbl := Lbl + HeaderReader.ReadChar;
SetMatrixLabels(Mtrx,Lbl);
end;
end else raise Exception.Create('Invalid 4G file header (' + FileName+ ')');
finally
HeaderReader.Free;
end;
// Set decompression stream
case FileCompression of
cpNone: DecompressionStream := FileStream; // No compression
cpGZip: DecompressionStream := TZDecompressionStream.Create(FileStream,15+16); // GZip compression
end;
BinaryReader := TBinaryReader.Create(DecompressionStream);
end;
Function T4GMatrixReader.ReadFloat16: Float32;
Var
FloatValue: Float16;
begin
FloatValue.Bytes := BinaryReader.ReadUInt16;
Result := FloatValue;
end;
Procedure T4GMatrixReader.Read(const CurrentRow: Integer; const Rows: TCustomMatrixRows);
begin
if CurrentRow < Size then
case FilePrecision of
ftFloat16: for var Mtrx := 0 to Count-1 do
for var Col := 0 to Size-1 do
Rows[Mtrx,Col] := ReadFloat16;
ftFloat32: for var Mtrx := 0 to Count-1 do
for var Col := 0 to Size-1 do
Rows[Mtrx,Col] := BinaryReader.ReadSingle;
ftFloat64: for var Mtrx := 0 to Count-1 do
for var Col := 0 to Size-1 do
Rows[Mtrx,Col] := BinaryReader.ReadDouble;
end
else
for var Mtrx := 0 to Count-1 do
for var Col := 0 to Rows.Size-1 do
Rows[Mtrx,Col] := 0.0;
end;
Destructor T4GMatrixReader.Destroy;
begin
BinaryReader.Free;
if DecompressionStream <> FileStream then DecompressionStream.Free;
inherited Destroy;
end;
////////////////////////////////////////////////////////////////////////////////
Constructor T4GMatrixWriter.Create(const FileName,FileLabel: String;
const MatrixLabels: array of String;
const Size: Integer;
const Precision: TFloatType = ftFloat32;
const Compression: T4GCompression = cpGzip);
begin
inherited Create(FileName,Length(MatrixLabels),Size);
FilePrecision := Precision;
// Write file header
BinaryWriter := TBinaryWriter.Create(FileStream,TEncoding.UTF8);
try
WriteChar('4');
WriteChar('G');
WriteByte(20);
WriteByte(1);
WriteByte(Ord(Precision));
WriteByte(ord(Compression));
WriteByte(Count);
WriteUInt16(Size);
WriteUInt32(0); // User bytes
var Lbl := FileLabel;
if Length(Lbl) > 255 then Lbl := Copy(Lbl,1,255);
WriteByte(Length(Lbl));
for var Chr := 1 to Length(Lbl) do WriteChar(Lbl[Chr]);
// Write matrix headers
for var Mtrx := 0 to Count-1 do
begin
WriteUInt16(0); // User bytes
Lbl := MatrixLabels[Mtrx];
if Length(Lbl) > 255 then Lbl := Copy(Lbl,1,255);
WriteByte(Length(Lbl));
for var Chr := 1 to Length(Lbl) do WriteChar(Lbl[Chr]);
end;
finally
BinaryWriter.Free;
end;
// Set compression stream
case Compression of
cpNone: CompressionStream := FileStream; // No compression
cpGZip: CompressionStream := TZCompressionStream.Create(FileStream,zcDefault,15+16);
end;
BinaryWriter := TBinaryWriter.Create(CompressionStream);
end;
Procedure T4GMatrixWriter.WriteChar(Value: Char);
begin
BinaryWriter.Write(Value);
end;
Procedure T4GMatrixWriter.WriteByte(Value: Byte);
begin
BinaryWriter.Write(Value);
end;
Procedure T4GMatrixWriter.WriteUInt16(Value: UInt16);
begin
BinaryWriter.Write(Value);
end;
Procedure T4GMatrixWriter.WriteUInt32(Value: UInt32);
begin
BinaryWriter.Write(Value);
end;
Procedure T4GMatrixWriter.WriteFloat16(Value: Float32);
Var
FloatValue: Float16;
begin
FloatValue := Value;
BinaryWriter.Write(FloatValue.Bytes);
end;
Procedure T4GMatrixWriter.WriteFloat32(Value: Float32);
begin
BinaryWriter.Write(Value);
end;
Procedure T4GMatrixWriter.WriteFloat64(Value: Float64);
begin
BinaryWriter.Write(Value);
end;
Procedure T4GMatrixWriter.Write(const CurrentRow: Integer; const Rows: TVirtualMatrixRows);
begin
case FilePrecision of
ftFloat16: for var Mtrx := 0 to Count-1 do
for var Col := 0 to Rows.Size-1 do
WriteFloat16(Rows[Mtrx,Col]);
ftFloat32: for var Mtrx := 0 to Count-1 do
for var Col := 0 to Rows.Size-1 do
WriteFloat32(Rows[Mtrx,Col]);
ftFloat64: for var Mtrx := 0 to Count-1 do
for var Col := 0 to Rows.Size-1 do
WriteFloat64(Rows[Mtrx,Col]);
end;
end;
Destructor T4GMatrixWriter.Destroy;
begin
BinaryWriter.Free;
if CompressionStream <> FileStream then CompressionStream.Free;
inherited Destroy;
end;
end.