-
Notifications
You must be signed in to change notification settings - Fork 0
/
EPF.cs
321 lines (282 loc) · 8.59 KB
/
EPF.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
using Capricorn.IO;
namespace Capricorn.Drawing;
/// <summary>
/// EPF Image Class
/// </summary>
public class EPFImage
{
private int expectedFrames;
private int width;
private int height;
private int unknown;
private long tocAddress;
private EPFFrame[] frames;
/// <summary>
/// Gets or sets a frame from this image.
/// </summary>
/// <param name="index">Zero-based index of the frame.</param>
/// <returns>EPF frame.</returns>
public EPFFrame this[int index]
{
get { return frames[index]; }
set { frames[index] = value; }
}
/// <summary>
/// Image frames.
/// </summary>
public EPFFrame[] Frames
{
get { return frames; }
}
/// <summary>
/// Gets the Table of Contents starting address.
/// </summary>
public long TOCAddress
{
get { return tocAddress; }
}
/// <summary>
/// Gets the unknown variable value.
/// </summary>
public int Unknown
{
get { return unknown; }
}
/// <summary>
/// Gets the height of the frames' containing canvas, in pixels.
/// </summary>
public int Height
{
get { return height; }
}
/// <summary>
/// Gets the width of the frames' containing canvas, in pixels.
/// </summary>
public int Width
{
get { return width; }
}
/// <summary>
/// Gets the number of frames expected in this image.
/// </summary>
public int ExpectedFrames
{
get { return expectedFrames; }
}
/// <summary>
/// Loads an EPF image file from disk.
/// </summary>
/// <param name="file">EPF image to load.</param>
/// <returns>EPF image.</returns>
public static EPFImage FromFile(string file)
{
// Create File Stream
FileStream stream = new FileStream(file,
FileMode.Open, FileAccess.Read, FileShare.Read);
// Load EPF from File Stream
return LoadEPF(stream);
}
/// <summary>
/// Loads an EPF image file from raw data bytes.
/// </summary>
/// <param name="data">Raw data to use.</param>
/// <returns>EPF image.</returns>
public static EPFImage FromRawData(byte[] data)
{
// Create Memory Stream
MemoryStream stream = new MemoryStream(data);
// Load HPF from Memory Stream
return LoadEPF(stream);
}
/// <summary>
/// Loads an EPF image file from an archive (case-sensitive).
/// </summary>
/// <param name="file">EPF image to load.</param>
/// <param name="archive">Data archive to load from.</param>
/// <returns>EPF image.</returns>
public static EPFImage FromArchive(string file, DATArchive archive)
{
// Check if File Exists
if (!archive.Contains(file))
return null;
// Extract and Create File
return FromRawData(archive.ExtractFile(file));
}
/// <summary>
/// Loads an EPF image file from an archive.
/// </summary>
/// <param name="file">EPF image to load.</param>
/// <param name="ignoreCase">Ignore case (noncase-sensitive).</param>
/// <param name="archive">Data archive to load from.</param>
/// <returns>EPF image.</returns>
public static EPFImage FromArchive(string file, bool ignoreCase, DATArchive archive)
{
// Check if File Exists
if (!archive.Contains(file, ignoreCase))
return null;
// Extract and Create File
return FromRawData(archive.ExtractFile(file, ignoreCase));
}
/// <summary>
/// Internal function that loads an EPF image from a given data stream.
/// </summary>
/// <param name="stream">Data stream.</param>
/// <returns></returns>
private static EPFImage LoadEPF(Stream stream)
{
// Create Binary Reader
stream.Seek(0, SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream);
// Create EPF Image
EPFImage epf = new EPFImage();
#region Get Header Values
epf.expectedFrames = reader.ReadUInt16();
epf.width = reader.ReadUInt16();
epf.height = reader.ReadUInt16();
epf.unknown = reader.ReadUInt16();
epf.tocAddress = reader.ReadUInt32() + 0x0C;
#endregion
// Create Frames
if (epf.expectedFrames > 0)
epf.frames = new EPFFrame[epf.expectedFrames];
else
return epf;
#region Get Each Frame
for (int i = 0; i < epf.expectedFrames; i++)
{
// Seek to Table of Contents
reader.BaseStream.Seek(epf.tocAddress + i * 16, SeekOrigin.Begin);
#region Get Frame Header
int left = reader.ReadUInt16();
int top = reader.ReadUInt16();
int right = reader.ReadUInt16();
int bottom = reader.ReadUInt16();
int width = right - left;
int height = bottom - top;
uint startAddress = reader.ReadUInt32() + 0x0C;
uint endAddress = reader.ReadUInt32() + 0x0C;
#endregion
#region Get Frame Data
// Seek to Address
reader.BaseStream.Seek(startAddress, SeekOrigin.Begin);
byte[] frameBytes = null;
if ((endAddress - startAddress) != (width * height))
{
// Get Whole File as Single Frame
frameBytes = reader.ReadBytes((int)(epf.tocAddress - startAddress));
}
else
{
// Get Frame Data
frameBytes = reader.ReadBytes((int)(endAddress - startAddress));
}
#endregion
// Create Frame
epf.frames[i] = new EPFFrame(left, top, width, height, frameBytes);
}
#endregion
// Return EPF
return epf;
}
/// <summary>
/// Gets the string representation of the object.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "{Frames = " + expectedFrames.ToString() + ", Width = " +
width.ToString() + ", Height = " +
height.ToString() + ", TOC Address = 0x" +
tocAddress.ToString("X").PadLeft(8, '0') + "}";
}
}
/// <summary>
/// EPF Image Frame Class
/// </summary>
public class EPFFrame
{
private int left;
private int top;
private int width;
private int height;
private byte[] rawData;
/// <summary>
/// Gets whether the frame is renderable or not.
/// </summary>
public bool IsValid
{
get
{
if (rawData == null || rawData.Length < 1)
return false;
else if (width < 1 || height < 1)
return false;
else if (width * height != rawData.Length)
return false;
else
return true;
}
}
/// <summary>
/// Gets the frame's raw data bytes.
/// </summary>
public byte[] RawData
{
get { return rawData; }
}
/// <summary>
/// Gets the height of the image, in pixels.
/// </summary>
public int Height
{
get { return height; }
}
/// <summary>
/// Gets the width of the image, in pixels.
/// </summary>
public int Width
{
get { return width; }
}
/// <summary>
/// Gets or sets the vertical location of the frame's origin.
/// </summary>
public int Top
{
get { return top; }
set { top = value; }
}
/// <summary>
/// Gets or sets the horizontal location of the frame's origin.
/// </summary>
public int Left
{
get { return left; }
set { left = value; }
}
/// <summary>
/// Creates an EPF frame with the specified dimensions and data.
/// </summary>
/// <param name="left">X coordinate position of the frame's origin.</param>
/// <param name="top">Y coordinate position of the frame's origin.</param>
/// <param name="width">Width of the frame.</param>
/// <param name="height">Height of the frame.</param>
/// <param name="rawData">Raw frame data bytes.</param>
public EPFFrame(int left, int top, int width, int height, byte[] rawData)
{
this.left = left;
this.top = top;
this.width = width;
this.height = height;
this.rawData = rawData;
}
/// <summary>
/// Gets the string representation of the object.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "{X = " + left.ToString() + ", Y = " + top.ToString() + ", Width = " +
width.ToString() + ", Height = " + height.ToString() + "}";
}
}