-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIrbFileReader.cs
385 lines (287 loc) · 9.45 KB
/
IrbFileReader.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
namespace IrbImgFormat
{
class IrbFileReader
{
public enum enumBlockType
{
enumBlockTypeUnknown = -1,
enumBlockTypeEmpty = 0,
enumBlockTypeImage = 1,
enumBlockTypePreview = 2,
enumBlockTypeTextInfo = 3,
enumBlockTypeHeader = 4,
enumBlockTypeAudio = 7
};
public enum enumFileType
{
enumFileTypeImage = 1,
enumFileTypeSequenz = 2
};
private struct tyBlock
{
public enumBlockType BlockType;
public int DWord2;
public int FrameIndex;
public int offset;
public int size;
public int DWord6;
public int DWord7;
public int DWord8;
public int headerOffset;
public int headerSize;
public int imageOffset;
public int imageSize;
};
private struct tyHead
{
public string MagicNumber;
public string FileType;
public string FileType2;
public enumFileType FileType2enum;
public int Flag1;
public int FirstBlockCount;
public int BlockOffset;
public int BlockCount;
public tyBlock[] Block;
public int BlockCountMax;
};
private static Logging logging = new Logging("IrbFileReader");
private tyHead Head;
private StreamReader reader;
private int m_imageCount;
//-------------------------------------//
public IrbFileReader(string filename)
{
this.m_imageCount = 0;
reader = new StreamReader(filename);
Head.MagicNumber = reader.ReadStr(5);
//- ID
if (string.Compare(Head.MagicNumber, "\xFFIRB\0") != 0) //-- soll "\xFF" "IRB" "\0" aber C schneidet das \0 weg!
{
logging.addError("Irb File - ''Magische Number'' wrong");
return;
}
//- FileType
Head.FileType = reader.ReadStr(8);
if (string.Compare(Head.FileType, "IRBACS\0\0") != 0)
{
Head.FileType2enum = enumFileType.enumFileTypeImage;
}
else if (string.Compare(Head.FileType, "IRBIS 3\0") != 0)
{
Head.FileType2enum = enumFileType.enumFileTypeSequenz;
}
else
{
logging.addError("Unknown Irbis File Type");
return;
}
Head.FileType2 = reader.ReadStr(8);
Head.Flag1 = reader.ReadIntBE();
Head.BlockOffset = reader.ReadIntBE(); //- starts at 0
Head.FirstBlockCount = reader.ReadIntBE();
Head.BlockCount = 0;
this.AddHead(Head.BlockOffset, Head.FirstBlockCount);
int i = 0;
while (i < Head.BlockCount)
{
if (Head.Block[i].BlockType == enumBlockType.enumBlockTypeHeader)
{
this.AddHead(Head.Block[i].offset, 2);
}
i++;
}
}
//-------------------------------------------------------//
public void Close()
{
if (reader != null)
{
reader.Close();
}
}
//-------------------------------------//
private void AddHead(int offset, int count)
{
if ((Head.BlockCount + count) > Head.BlockCountMax)
{
Head.BlockCountMax = Head.BlockCountMax + count + 100;
System.Array.Resize(ref Head.Block, Head.BlockCountMax);
}
reader.SetOffset(offset);
if (reader.Eof) return;
for (int i = 0; i < count; i++)
{
if (reader.Eof) return;
SetHeadBlockVars(ref Head.Block[Head.BlockCount]);
Head.BlockCount++;
}
}
//-------------------------------------//
private void SetHeadBlockVars(ref tyBlock block)
{
block.BlockType = (enumBlockType)reader.ReadIntBE();
block.DWord2 = reader.ReadIntBE();
block.FrameIndex = reader.ReadIntBE();
block.offset = reader.ReadIntBE(); // starts at 0
block.size = reader.ReadIntBE();
//- head is wlways 0x6C0 Byte in lengtg
block.headerSize = 0x6C0;
if (block.headerSize > block.size) block.headerSize = block.size;
block.headerOffset = 0;
block.imageOffset = block.headerSize;
block.imageSize = block.size - block.imageOffset;
block.DWord6 = reader.ReadIntBE();
block.DWord7 = reader.ReadIntBE();
block.DWord8 = reader.ReadIntBE();
if (block.BlockType == enumBlockType.enumBlockTypeImage)
{
this.m_imageCount++;
}
}
//-------------------------------------//
public int GetImageCount()
{
return this.m_imageCount;
}
//-------------------------------------//
public int GetBlockCount()
{
return Head.BlockCount;
}
//-------------------------------------//
public int GetBlockSize(int index)
{
if ((index >= 0) && (index < Head.BlockCount))
{
return Head.Block[index].size;
}
else
{
return 0;
}
}
//-------------------------------------//
public bool IsBlockImage(int index)
{
if ((index >= 0) && (index < Head.BlockCount))
{
return (Head.Block[index].BlockType == enumBlockType.enumBlockTypeImage);
}
else
{
return false;
}
}
//-------------------------------------//
public bool IsBlockTextInfo(int index)
{
if ((index >= 0) && (index < Head.BlockCount))
{
return (Head.Block[index].BlockType == enumBlockType.enumBlockTypeTextInfo);
}
else
{
return false;
}
}
//-------------------------------------//
public enumBlockType GetBlockType(int index)
{
if ((index >= 0) && (index < Head.BlockCount))
{
return Head.Block[index].BlockType;
}
else
{
return enumBlockType.enumBlockTypeUnknown;
}
}
//-------------------------------------//
public bool IsBlockPreview(int index)
{
if ((index >= 0) && (index < Head.BlockCount))
{
return (Head.Block[index].BlockType == enumBlockType.enumBlockTypePreview);
}
else
{
return false;
}
}
/// <summary>
/// Return the info text of this file
/// </summary>
public string GetTextInfo(int index = 0)
{
var blockIndex = FindBlockIndexByType(enumBlockType.enumBlockTypeTextInfo, index);
if (blockIndex < 0)
{
return string.Empty;
}
tyBlock block = Head.Block[blockIndex];
return reader.ReadStr(block.size, block.offset);
}
/// <summary>
/// return the data of an image
/// </summary>
public byte[] GetImageData(int imageIndex)
{
int blockIndex = GetImageBlockIndex(imageIndex);
if (blockIndex < 0)
{
logging.addError("getImageData(imageIndex) fail - ImageIndex: " + imageIndex + " not found");
return null;
}
//- Header zurückgeben
tyBlock block = Head.Block[blockIndex];
return reader.ReadByte(block.size, block.offset);
}
/// <summary>
/// Return the n-th index of a given data block type
/// </summary>
private int FindBlockIndexByType(enumBlockType type, int number)
{
if ((number < 0) || (number >= Head.BlockCount))
{
return -1;
}
for (int i = 0; i < Head.BlockCount; i++)
{
tyBlock block = Head.Block[i];
//- find all images
if (block.BlockType == type)
{
/// return image if
number--;
if (number < 0)
{
return i;
}
}
}
return -1;
}
/// <summary>
/// Retrun the data block for a given image index
/// </summary>
private int GetImageBlockIndex(int imageIndex)
{
return FindBlockIndexByType(enumBlockType.enumBlockTypeImage, imageIndex);
}
/// <summary>
/// Return the file offset for a given data block
/// </summary>
public int GetBlockOffset(int index)
{
if ((index >= 0) && (index < Head.BlockCount))
{
return Head.Block[index].offset;
}
else
{
return 0;
}
}
}
}