private static Bitmap getIMG(BCHTexture bchtex, byte[] data)
{
// New Image
Bitmap img = new Bitmap(nlpo2(gcm(bchtex.Width, 8)), nlpo2(gcm(bchtex.Height, 8)));
int f = (int)bchtex.Format;
int area = img.Width * img.Height;
if (f == 0 && area > bchtex.Length / 4 || (f == 3 && area > bchtex.Length / 4))
{
img = new Bitmap(gcm(bchtex.Width, 8), gcm(bchtex.Height, 8));
area = img.Width * img.Height;
}
byte[] temp = new byte[(int)bchtex.Length];
Array.Copy(data, bchtex.DataOffset, temp, 0, temp.Length);
data = temp;
// Coordinates
// Colors
// Tiles Per Width
int p = gcm(img.Width, 8) / 8;
if (p == 0) p = 1;
// Build Image
using (Stream BitmapStream = new MemoryStream(data))
using (BinaryReader br = new BinaryReader(BitmapStream))
for (uint i = 0; i < area; i++) // for every pixel
{
uint x, y;
d2xy(i % 64, out x, out y);
uint tile = i / 64;
// Shift Tile Coordinate into Tilemap
x += (uint)(tile % p) * 8;
y += (uint)(tile / p) * 8;
// Get Color
Color c;
switch (f)
{
case 0: //RGBA8 - 4 bytes
c = DecodeColor(br.ReadUInt32(), f);
break;
case 1: //RGB8
byte[] data1 = br.ReadBytes(3); Array.Resize(ref data1, 4);
c = DecodeColor(BitConverter.ToUInt32(data1, 0), f);
break;
case 2: //RGBA5551
case 3: //RGB565
case 4: //RGBA4
case 5: //LA8
c = DecodeColor(br.ReadUInt16(), f);
break;
case 6: //HILO8
case 7: //L8
case 8: //A8
case 9: //LA4
c = DecodeColor(br.ReadByte(), f);
break;
case 0xA: //L4
case 0xB: //A4
uint val = br.ReadByte();
img.SetPixel((int)x, (int)y, DecodeColor(val & 0xF, f));
i++; x++;
c = DecodeColor(val >> 4, f);
break;
case 0xC: // ETC1
case 0xD: // ETC1A4
default:
throw new Exception("Invalid FileFormat.");
}
img.SetPixel((int)x, (int)y, c);
}
return img;
}