Xbox 360 Bitmap Research
Posted: Tue Jun 26, 2007 8:15 pm
I figured I would release my deswizzle method for A8R8G8B8 so people can do more research on bitmaps. This applies to not only H3 but most game's resources for Xbox 360 and could probably be easily adjusted to deal with different argb formats by changing some of the math in my code. BTW this is using my own modified BinaryReader class so you will have to change that if you want to use this code. also keep in mind the data is in big endian format and need to 32 bit swapped in order to be in a format the pc can understand.
Code: Select all
public static byte[] Deswizzle(byte[] raw, int width, int height)
{
MemoryStream ms = new MemoryStream();
core.IO.EndianIO.EndianWriter ew = new core.IO.EndianIO.EndianWriter(ms, core.IO.EndianType.LittleEndian);
ew.BaseStream.Position = 0;
int realsize = width * height * 4;
int lines = realsize / 16;
int templine = 0;
int rowcount = 0;
int offsetby = 0;
int i = 0;
int sections = width / 32;
for (int x = 0; x < lines; x++)
{
if (rowcount % 32 == 0 && rowcount != 0)
{
templine = (i+1)*((width * 32) /4);
i++;
}
if (rowcount % 8 == 0 && rowcount != 0)
{
if (offsetby == 0)
{
offsetby = 8 * 16;
}
else
{
offsetby = 0;
}
}
if (rowcount == height) break;
///get row 1 - 224 pixels(one row) = 7 * 32
for (int y = 0; y < sections; y++)
{
//get 32 pixels
for (int z = 0; z < 8; z++)
{
if (z < 4)
{
int offset = (templine * 16) + ((y * 256) * 16) + ((z * 2) * 16) + offsetby;
int oi1 = BitConverter.ToInt32(raw, offset + 0);
int oi2 = BitConverter.ToInt32(raw, offset + 4);
int oi3 = BitConverter.ToInt32(raw, offset + 8);
int oi4 = BitConverter.ToInt32(raw, offset + 12);
ew.Write(oi1);
ew.Write(oi2);
ew.Write(oi3);
ew.Write(oi4);
}
else
{
int offset = (templine * 16) + ((y * 256) * 16) + ((z * 2) * 16) - offsetby;
int oi1 = BitConverter.ToInt32(raw, offset + 0);
int oi2 = BitConverter.ToInt32(raw, offset + 4);
int oi3 = BitConverter.ToInt32(raw, offset + 8);
int oi4 = BitConverter.ToInt32(raw, offset + 12);
ew.Write(oi1);
ew.Write(oi2);
ew.Write(oi3);
ew.Write(oi4);
}
}
}
rowcount++;
if (rowcount == height) break;
///get row 2
for (int y = 0; y < sections; y++)
{
//get 32 pixels
for (int z = 0; z < 8; z++)
{
if (offsetby != 0)
{
}
if (z < 4)
{
int offset = (templine * 16) + 16 + ((y * 256) * 16) + ((z * 2) * 16) + offsetby;
int oi1 = BitConverter.ToInt32(raw, offset + 0);
int oi2 = BitConverter.ToInt32(raw, offset + 4);
int oi3 = BitConverter.ToInt32(raw, offset + 8);
int oi4 = BitConverter.ToInt32(raw, offset + 12);
ew.Write(oi1);
ew.Write(oi2);
ew.Write(oi3);
ew.Write(oi4);
}
else
{
int offset = (templine * 16) + 16 + ((y * 256) * 16) + ((z * 2) * 16) - offsetby;
int oi1 = BitConverter.ToInt32(raw, offset + 0);
int oi2 = BitConverter.ToInt32(raw, offset + 4);
int oi3 = BitConverter.ToInt32(raw, offset + 8);
int oi4 = BitConverter.ToInt32(raw, offset + 12);
ew.Write(oi1);
ew.Write(oi2);
ew.Write(oi3);
ew.Write(oi4);
}
}
}
rowcount++;
templine += 16;
}
return ms.ToArray();
}