-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIrbImg.cs
346 lines (238 loc) · 9.23 KB
/
IrbImg.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System;
namespace IrbImgFormat
{
class IrbImg
{
private static Logging logging = new Logging("IrbImg");
public double ShotRangeMin { get; protected set; }
public double ShotRangeMax { get; protected set; }
public double CalibRangeMin { get; protected set; }
public double CalibRangeMax { get; protected set; }
IrbFileReader reader;
int BytePerPixel;
int Compressed;
float Emissivity;
float EnvironmentalTemp;
float Distanz;
float PathTemperature;
float CenterWavelength;
float CalibRange_min;
float CalibRange_max;
float ShotRange_start_ERROR;
float ShotRange_size;
double TimeStamp_Raw;
DateTime TimeStamp;
DateTime TimeStampOffsetTime;
int TimeStampOffsetMilliseconds;
int TimeStampMilliseconds;
string Device;
string DeviceSerial;
string Optics;
string OpticsResolution;
string OpticsText;
public float[] Data;
private int Width;
private int Height;
public IrbImg(IrbFileReader FileReader, int imageIndex = 0)
{
reader = FileReader;
ReadImage(imageIndex);
}
/// <summary>
/// Width of the image
/// </summary>
/// <returns></returns>
public int GetWidth()
{
return Width;
}
/// <summary>
/// Height of the image
/// </summary>
public int GetHeight()
{
return Height;
}
/// <summary>
/// Return the data of the image as float array
/// </summary>
/// <returns></returns>
public float[] GetData()
{
if (Data == null) logging.addError("getData() Accessing non initialised data!");
return Data;
}
/// <summary>
/// Read a image from the file
/// </summary>
public bool ReadImage(int imageIndex)
{
System.DateTime FrameTime = System.DateTime.Now;
var reader = new BufferReader(this.reader.GetImageData(imageIndex));
if (reader.Eof)
{
return false;
}
Width = 0;
Height = 0;
//- Image header
BytePerPixel = reader.ReadWordBE();
Compressed = reader.ReadWordBE();
Width = reader.ReadWordBE();
Height = reader.ReadWordBE();
reader.ReadIntBE(); //-- don't know - alway 0
reader.ReadWordBE(); //-- don't know - alway 0
//- dont know why but it is alwasy the width -1
if (reader.ReadWordBE() != (Width - 1))
{
logging.addError("??? value != (Height - 1)");
}
reader.ReadWordBE(); //-- don't know - alway 0
//- dont know why but it is alwasy the height -1
if (reader.ReadWordBE() != (Height - 1))
{
logging.addError("??? value != (Height - 1)");
}
reader.ReadWordBE(); //-- don't know - alway 0
reader.ReadWordBE(); //-- don't know - alway 0
Emissivity = reader.ReadSingleBE();
Distanz = reader.ReadSingleBE();
EnvironmentalTemp = reader.ReadSingleBE();
reader.ReadWordBE(); //-- don't know - always 0
reader.ReadWordBE(); //-- don't know - always 0
PathTemperature = reader.ReadSingleBE();
reader.ReadWordBE(); //-- don't know - always 0x65
reader.ReadWordBE(); //-- don't know - always 0
CenterWavelength = reader.ReadSingleBE();
reader.ReadWordBE(); //-- don't know - always 0
reader.ReadWordBE(); //-- don't know - always 0xH4080
reader.ReadWordBE(); //-- don't know - always 0x9
reader.ReadWordBE(); //-- don't know - always 0x101
if ((Width > 10000) || (Height > 10000))
{
logging.addError("Defect Irbis Image File: Image Width(" + Width + ") or Height(" + Height + ") is out of range!");
Width = 1;
Height = 1;
return false;
}
//- liest weitere Bildinforationen aus
this.ReadFlags(reader, 1084);
Data = ReadImageData(reader, 0x6C0, Width, Height, 60, Compressed);
if (reader.Eof) logging.addError("end of file!");
return true;
}
/// <summary>
/// Read image flags from the file
/// </summary>
public void ReadFlags(BufferReader reader, int offset)
{
CalibRange_min = reader.ReadSingleBE(offset + 92);
CalibRange_max = reader.ReadSingleBE(offset + 96);
Device = reader.ReadNullTerminatedString(offset + 142, 12);
DeviceSerial = reader.ReadNullTerminatedString(offset + 186, 16);
Optics = reader.ReadNullTerminatedString(offset + 202, 32);
OpticsResolution = reader.ReadNullTerminatedString(offset + 234, 32);
OpticsText = reader.ReadNullTerminatedString(offset + 554, 48);
ShotRange_start_ERROR = reader.ReadSingleBE(offset + 532);
ShotRange_size = reader.ReadSingleBE(offset + 536);
TimeStamp_Raw = reader.ReadDoubleBE(8, offset + 540);
TimeStampMilliseconds = reader.ReadIntBE(offset + 548);
TimeStamp = Double2DateTime(TimeStamp_Raw, TimeStampMilliseconds);
}
/// <summary>
/// Read the compressing "pallet" from file
/// </summary>
private float[] ReadPallet(BufferReader reader, int offset)
{
float[] palette = new float[256];
int pos = offset;
for (int i = 0; i < 256; i++)
{
palette[i] = reader.ReadSingleBE(pos);
pos += 4;
}
return palette;
}
/// <summary>
/// Read the image data from file
/// </summary>
/// <returns></returns>
private float[] ReadImageData(BufferReader reader, int bindata_offset, int width, int height, int palette_offset, int useCompression)
{
int data_size = width * height; //- count of pixles
bool useComp = (useCompression == 1);
int pixelCount = data_size;
float[] matrixData = new float[pixelCount];
int matrixDataPos = 0;
int v1_pos = bindata_offset;
int v2_pos = v1_pos + width * height; //- used if data are compressed
//byte data_v1 = &bindata[v1_pos];
//unsigned char* data_v2 = &bindata[v2_pos];
int v1 = 0;
int v2 = 0;
float[] Palette = ReadPallet(reader, palette_offset);
int v2_count = 0;
float v = 0;
float f;
if (!useComp)
{
for (int i = pixelCount; i > 0; i--)
{
//- read values
v1 = reader.ReadByte(v1_pos);
v1_pos++;
v2 = reader.ReadByte(v1_pos);
v1_pos++;
f = (float)v1 * (1.0f / 256.0f);
//- lineare interpolation
v = Palette[v2 + 1] * f + Palette[v2] * (1.0f - f);
if (v < 0) v = 0; //- oder 255
matrixData[matrixDataPos] = v;
matrixDataPos++;
}
}
else
{
for (int i = pixelCount; i > 0; i--)
{
//- werte lesen
if (v2_count-- < 1) //- ok... neuen wert für V2 lesen
{
v2_count = reader.ReadByte(v2_pos) - 1;
v2_pos++;
v2 = reader.ReadByte(v2_pos);
v2_pos++;
}
v1 = reader.ReadByte(v1_pos);
v1_pos++;
f = (float)v1 * (1.0f / 256.0f);
//- lineare interpolation
v = Palette[v2 + 1] * f + Palette[v2] * (1.0f - f);
if (v < 0) v = 0; //- oder 255
matrixData[matrixDataPos] = v;
matrixDataPos++;
}
}
return matrixData;
}
/// <summary>
/// convert a double value to a date time
/// </summary>
private System.DateTime Double2DateTime(double date, int Milliseconds = 0)
{
System.DateTime d = DateTime.FromOADate(date);
//- calc the time from the Date + Milliseconds
if ((TimeStampOffsetTime != DateTime.MinValue) && (Milliseconds > 0) && (Milliseconds > TimeStampOffsetMilliseconds) && (d >= TimeStampOffsetTime))
{
return TimeStampOffsetTime.AddMilliseconds(Milliseconds - TimeStampOffsetMilliseconds);
}
else
{
//- never set so save start-date/time
TimeStampOffsetMilliseconds = Milliseconds;
TimeStampOffsetTime = d;
return d;
}
}
}
}