public static void DebugMsg(string msg)
{
MessageBox.Show(msg, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
public string ReadString(byte[] buffer, int count)
{
str.Read(buffer, 0, count);
return Encoding.ASCII.GetString(buffer, 0, 4);
}
public bool ReadBool(byte[] buffer, int count)
{
str.Read(buffer, 0, count);
return BitConverter.ToBoolean(buffer, 0);
}
public uint ReadUInt32(byte[] buffer, int count)
{
str.Read(buffer, 0, count);
return BitConverter.ToUInt32(buffer, 0);
}
public int ReadInt32(byte[] buffer, int count)
{
str.Read(buffer, 0, count);
return BitConverter.ToInt32(buffer, 0);
}
public BlpFile(Stream stream)
{
str = stream;
byte[] buffer = new byte[4];
type = ReadString(buffer, 4);
compression = ReadUInt32(buffer, 4);
alphaFlags = ReadUInt32(buffer, 4);
width = ReadUInt32(buffer, 4);
height = ReadUInt32(buffer, 4);
alphaEncoding = ReadUInt32(buffer, 4);
//read unused flags
hasMipmaps = ReadBool(buffer, 4);
for (int i = 0; i < 16; i++)
mipmapOffset[i] = ReadUInt32(buffer, 4);
for (int i = 0; i < 16; i++)
mipmapSize[i] = ReadUInt32(buffer, 4);
while (mipmapOffset[mipmapCount] != 0 && mipmapSize[mipmapCount] != 0)
mipmapCount++;
if (0 == compression)
{
jpegHeaderSize = ReadUInt32(buffer, 4);
jpegHeader = new byte[jpegHeaderSize];
str.Read(jpegHeader, 0, Convert.ToInt32(jpegHeaderSize));
if (type == "BLP1")
{
imageData = new byte[mipmapSize[0]];
str.Position = mipmapOffset[0];
str.Read(imageData, 0, imageData.Length);
//My problem begins here for blp compressed:
int w = Convert.ToInt32(width);
int h = Convert.ToInt32(height);
bitmap = new Bitmap(w, h);
BitmapData bmpdata = bitmap.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(imageData, 0, bmpdata.Scan0, imageData.Length);
bitmap.UnlockBits(bmpdata);
}
}
else if (1 == compression)
{
// Reading palette
for (int i = 0; i < 256; i++)
{
Palette[i].blue = ReadInt32(buffer, 4);
Palette[i].green = ReadInt32(buffer, 4);
Palette[i].red = ReadInt32(buffer, 4);
Palette[i].alpha = ReadInt32(buffer, 4);
}
int[] indexList = new int[width * height];
int[] alphaList = new int[width * height];
for (int i = 0; i < width * height; i++)
indexList[i] = ReadInt32(buffer, 4);
if (alphaFlags == 8)
{
for (int i = 0; i < width * height; i++)
alphaList[i] = ReadInt32(buffer, 4);
}
// Whats Next?
}