Skip to content

Commit

Permalink
Reduce memory reallocation when initializing.
Browse files Browse the repository at this point in the history
  • Loading branch information
TsudaKageyu committed Oct 29, 2014
1 parent f1d39d6 commit b181667
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions IconExtractor/IconExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,27 @@ private void Initialize(string fileName)

ENUMRESNAMEPROC callback = (h, t, name, l) =>
{
// Refer the following URL for the data structures used here:
// http://msdn.microsoft.com/en-us/library/ms997538.aspx

// RT_GROUP_ICON resource consists of a GRPICONDIR and GRPICONDIRENTRY's.

var dir = GetDataFromResource(hModule, RT_GROUP_ICON, name);

using (var writer = new BinaryWriter(new MemoryStream()))
{
// Refer the following URL for the data structures:
// http://msdn.microsoft.com/en-us/library/ms997538.aspx
// Calculate the size of an entire .icon file.

int count = BitConverter.ToUInt16(dir, 4); // GRPICONDIR.idCount
int len = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
for (int i = 0; i < count; ++i)
len += BitConverter.ToInt32(dir, 6 + 14 * i + 8); // GRPICONDIRENTRY.dwBytesInRes

using (var writer = new BinaryWriter(new MemoryStream(len)))
{
// Copy GRPICONDIR to ICONDIR.

writer.Write(dir, 0, 6);

int count = BitConverter.ToUInt16(dir, 4); // GRPICONDIR.idCount
int offset = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
int offset = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
var pics = new byte[count][];

for (int i = 0; i < count; ++i)
Expand Down

0 comments on commit b181667

Please sign in to comment.