-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHPF.cs
187 lines (161 loc) · 5.28 KB
/
HPF.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
using Capricorn.IO;
using Capricorn.IO.Compression;
namespace Capricorn.Drawing;
/// <summary>
/// HPF Image Class
/// </summary>
public class HPFImage
{
private int width;
private int height;
private byte[] rawData;
private byte[] headerData;
/// <summary>
/// Gets the raw header bytes of the image.
/// </summary>
public byte[] HeaderData
{
get { return headerData; }
}
/// <summary>
/// Gets the raw data bytes of the image.
/// </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>
/// Loads a HPF image file from disk.
/// </summary>
/// <param name="file">HPF image to load.</param>
/// <returns>HPF image.</returns>
public static HPFImage FromFile(string file)
{
// Create File Stream
FileStream stream = new FileStream(file,
FileMode.Open, FileAccess.Read, FileShare.Read);
// Load HPF from File Stream
return LoadHPF(stream);
}
/// <summary>
/// Loads a HPF image file from raw data bytes.
/// </summary>
/// <param name="data">Raw data to use.</param>
/// <returns>HPF image.</returns>
public static HPFImage FromRawData(byte[] data)
{
// Create Memory Stream
MemoryStream stream = new MemoryStream(data);
// Load HPF from Memory Stream
return LoadHPF(stream);
}
/// <summary>
/// Loads a HPF image file from an archive (case-sensitive).
/// </summary>
/// <param name="file">HPF image to load.</param>
/// <param name="archive">Data archive to load from.</param>
/// <returns>HPF image.</returns>
public static HPFImage 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 a HPF image file from an archive.
/// </summary>
/// <param name="file">HPF image to load.</param>
/// <param name="ignoreCase">Ignore case (noncase-sensitive).</param>
/// <param name="archive">Data archive to load from.</param>
/// <returns>HPF image.</returns>
public static HPFImage 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 HPF image from a given data stream.
/// </summary>
/// <param name="stream">Data stream to read from.</param>
/// <returns>HPF image.</returns>
private static HPFImage LoadHPF(Stream stream)
{
// Create Reader from Stream
stream.Seek(0, SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream);
#region Check HPF Signature
uint signature = reader.ReadUInt32();
reader.BaseStream.Seek(-4, SeekOrigin.Current);
bool decompress = true;
if (signature != 0xFF02AA55)
{
//throw new ArgumentException("Invalid file format, does not contain HPF signature bytes.");
decompress = false;
}
#endregion
//else
{
// Create HPF Image
HPFImage hpf = new HPFImage();
if (decompress)
{
// Decompress HPF
byte[] hpfData = HPFCompression.Decompress(
reader.ReadBytes((int)reader.BaseStream.Length));
// Closer Reader
reader.Close();
#region Open Memory Stream and Reader
MemoryStream memStream = new MemoryStream(hpfData);
reader = new BinaryReader(memStream);
#endregion
// Get Header and Body Data
hpf.headerData = reader.ReadBytes(8);
hpf.rawData = reader.ReadBytes(hpfData.Length - 8);
reader.Close();
}
else
{
reader.BaseStream.Seek(0, SeekOrigin.Begin);
hpf.headerData = reader.ReadBytes(8);
byte[] hpfData = reader.ReadBytes((int)(reader.BaseStream.Length - 8));
hpf.rawData = hpfData;
reader.Close();
}
#region Get Dimensions
hpf.width = 28;
if (hpf.rawData.Length % hpf.width != 0)
throw new ArgumentException("HPF file does not use the standard 28 pixel width.");
hpf.height = hpf.rawData.Length / hpf.width;
#endregion
return hpf;
}
return null;
}
/// <summary>
/// Gets the string representation of the object;
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "{Width = " + width.ToString() + ", Height = " + height.ToString() + "}";
}
}