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();
}