public TGA(string fileName, ImageSize imgSize, string format, byte[] rawData) // used when extracting raw data from me3 archive
: base(fileName, imgSize, format)
{
TGAHeader.imagetype = 0x2; // RGB Image type
TGAHeader.bits = (byte)(rawData.Length / (imgSize.width * imgSize.height) * 8);
TGAHeader.width = (short)imgSize.width;
TGAHeader.height = (short)imgSize.height;
//TGAHeader.colourmapbits = 32;
TGAHeader.descriptor = 0x00; // 0x20 = flips the image vertically
BPP = TGAHeader.bits / 8;
switch (format)
{
case "PF_G8":
case "G8":
{
imgData = new byte[rawData.Length];
for (int i = 0; i < imgSize.height; i++)
{
for (int j = 0; j < imgSize.width; j++)
{
imgData[(imgSize.width * i) + j] = rawData[(imgSize.width * i) + j];
}
}
TGAHeader.imagetype = 0x3;
}
break;
case "PF_A8R8G8B8":
case "A8R8G8B8":
{
TGAHeader.descriptor |= 0x08; // 0x08 = 8-bit alpha bits
imgData = new byte[rawData.Length];
int intBPP = (int)BPP;
for (int i = 0; i < imgSize.height; i++)
{
for (int j = 0; j < imgSize.width; j++)
{
for (int k = 0; k < intBPP; k++)
imgData[(intBPP * imgSize.width * (imgSize.height - 1 - i)) + (j * intBPP) + k] = rawData[(imgSize.width * intBPP * i) + (j * intBPP) + k];
/*imgData[(4 * imgSize.width * (imgSize.height - 1 - i)) + (j * 4) + 1] = rawData[(imgSize.width * 4 * i) + (j * 4) + 1];
imgData[(4 * imgSize.width * (imgSize.height - 1 - i)) + (j * 4) + 2] = rawData[(imgSize.width * 4 * i) + (j * 4) + 2];
imgData[(4 * imgSize.width * (imgSize.height - 1 - i)) + (j * 4) + 3] = rawData[(imgSize.width * 4 * i) + (j * 4) + 3];*/
}
}
verticalFlipped = true;
flipVertically();
}
break;
default: throw new FormatException("Invalid TGA format");
}
// convert TGAHeader to byte array
int headSize = Marshal.SizeOf(TGAHeader);
headData = new byte[headSize];
IntPtr ptr = Marshal.AllocHGlobal(headSize);
Marshal.StructureToPtr(TGAHeader, ptr, true);
Marshal.Copy(ptr, headData, 0, headData.Length);
Marshal.FreeHGlobal(ptr);
}