-
Notifications
You must be signed in to change notification settings - Fork 0
/
LbmLoader.cs
294 lines (266 loc) · 9.11 KB
/
LbmLoader.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BmArrayLoader
{
public class LbmLoader : Loader
{
private int m_width;
private int m_height;
private int m_compressed;
private int m_offset;
public LbmLoader() : base()
{
m_offset = 0;
}
public override bool Load(string fileName)
{
if (base.Load(fileName))
{
if (ReadChunks())
{
ConfirmReady();
return true;
}
}
return false;
}
private bool ReadChunks()
{
bool status = false;
bool first = true;
if (m_bytes.Length > 8)
{
do
{
int chunkId = ReadInt32();
if (first && chunkId != (int)LbmChunk.FORM) //first chunk must be FORM
{
Console.WriteLine("LbmLoader ERROR: Unsupported image format found.");
return false;
}
first = false;
int length = ReadInt32();
int padding = length % 2;
if (m_offset + length + padding <= m_bytes.Length)
{
switch (chunkId)
{
case (int)LbmChunk.FORM:
status = ReadForm(length);
//FORM is container for all other chunks, therefore don't apply padding here - it is at eof
break;
case (int)LbmChunk.BMHD:
status = ReadHeader(length);
m_offset += padding;
break;
case (int)LbmChunk.CMAP:
status = ReadColormap(length);
m_offset += padding;
break;
case (int)LbmChunk.BODY:
status = ReadBody(length);
m_offset += padding;
//BODY is optional - file might only contain color palette
break;
default:
status = SkipChunk(length);
m_offset += padding;
break;
}
}
else
{
Console.WriteLine("LbmLoader ERROR: Truncated chunk found.");
status = false;
}
} while (status && (m_bytes.Length - m_offset > 4)); //make sure padding byte does not start another iteration
}
return status;
}
private bool ReadForm(int length)
{
int type = ReadInt32();
switch (type)
{
case (int)LbmType.PBM:
return true;
case (int)LbmType.ILBM:
Console.WriteLine("LbmLoader ERROR: ILBM format not supported.");
return false;
default:
Console.WriteLine("LbmLoader ERROR: Unknown format identifier found.");
return false;
}
}
private bool ReadHeader(int length)
{
m_width = ReadInt16();
m_height = ReadInt16();
m_offset += 4; //skip stuff
int depth = ReadByte();
m_offset++; //skip stuff
m_compressed = ReadByte();
m_offset += 9; //skip stuff
if (depth == 0 || depth == 8) //only palette or indexed color supported
{
return true;
}
else
{
Console.WriteLine("LbmLoader ERROR: Unsupported color depth found.");
return false;
}
}
private bool ReadBody(int length)
{
m_master = new Indexmap(m_width, m_height);
switch (m_compressed)
{
case (int)LbmCompression.None:
return ReadUncompressed(length);
case (int)LbmCompression.ByteRun1:
return ReadCompressed(length);
default:
Console.WriteLine("LbmLoader ERROR: Unknown compression found.");
return false;
}
}
private bool ReadUncompressed(int length)
{
//LBM applies padding to get lines with n*16 bit length
//parser only supports depth of 8 bit (=1 byte), therefore just pad odd width with 1 byte
int linePadding = m_width % 2;
// if ((m_width % 16) != 0)
// linePadding = 16 - (m_width % 16); //TODO this is wrong - BIT not byte, index buffer will always have 8 bit -> check even/odd width only
int lineIdx;
int tgtIdx = 0;
for (int i = 0; i < length; i++)
{
if (tgtIdx < m_master.Data.Length - 1)
{
lineIdx = i % (m_width + linePadding);
if (lineIdx < m_width)
{
m_master.Data[tgtIdx++] = ReadByte();
}
else
m_offset++; //ignore padding
}
else
{
Console.WriteLine("LbmLoader ERROR: Corrupted body data found.");
return false;
}
}
return true;
}
private bool ReadCompressed(int length)
{
byte selector;
byte value;
int count;
int tgtIdx = 0;
for (int i = 0; i < length; i++)
{
selector = ReadByte();
if (selector < 128)
{
count = selector + 1;
if ((tgtIdx < m_master.Data.Length - count) && (i + 1 < length - count))
{
for (int j = 0; j < count; j++)
m_master.Data[tgtIdx++] = ReadByte();
i += count; //update iterator
}
else
{
Console.WriteLine("LbmLoader ERROR: Corrupted body data found.");
return false;
}
}
else if (selector > 128)
{
count = 257 - selector;
if (tgtIdx < m_master.Data.Length - 1)
{
value = ReadByte();
for (int j = 0; j < count; j++)
m_master.Data[tgtIdx++] = value;
i++; //update iterator
}
else
{
Console.WriteLine("LbmLoader ERROR: Corrupted body data found.");
return false;
}
}
else //ignore value 128
{
m_offset++;
}
}
return true;
}
private bool ReadColormap(int length)
{
if (length % 3 == 0)
{
for (int i = length; i > 0; i -= 3)
{
byte[] rgb = new byte[3];
rgb[0] = ReadByte(); //R
rgb[1] = ReadByte(); //G
rgb[2] = ReadByte(); //B
m_palette.Add(rgb);
}
return true;
}
else //truncated chunk
return false;
}
private bool SkipChunk(int length)
{
m_offset += length; //just ignore contents
return true;
}
private int ReadInt32()
{
int value = (m_bytes[m_offset] << 24) | (m_bytes[m_offset + 1] << 16) | (m_bytes[m_offset + 2] << 8) | m_bytes[m_offset + 3];
m_offset += 4;
return value;
}
private int ReadInt16()
{
int value = (m_bytes[m_offset] << 8) | m_bytes[m_offset + 1];
m_offset += 2;
return value;
}
private byte ReadByte()
{
byte value = m_bytes[m_offset];
m_offset++;
return value;
}
private enum LbmType : int
{
ILBM = 0x494C424D,
PBM = 0x50424D20,
}
private enum LbmCompression : int
{
None = 0,
ByteRun1 = 1,
}
private enum LbmChunk : int
{
FORM = 0x464F524D,
BMHD = 0x424D4844,
BODY = 0x424F4459,
CMAP = 0x434D4150,
}
}
}