/// <summary> /// Adds a Texture to the TPL. /// </summary> /// <param name="img"></param> /// <param name="tplFormat"></param> public void AddTexture(Image img, TPL_TextureFormat tplFormat, TPL_PaletteFormat paletteFormat = TPL_PaletteFormat.RGB5A3) { TPL_TextureEntry tempTexture = new TPL_TextureEntry(); TPL_TextureHeader tempTextureHeader = new TPL_TextureHeader(); TPL_PaletteHeader tempPaletteHeader = new TPL_PaletteHeader(); byte[] tempTextureData = imageToTpl(img, tplFormat); byte[] tempPaletteData = new byte[0]; tempTextureHeader.TextureHeight = (ushort)img.Height; tempTextureHeader.TextureWidth = (ushort)img.Width; tempTextureHeader.TextureFormat = (uint)tplFormat; if (tplFormat == TPL_TextureFormat.CI4 || tplFormat == TPL_TextureFormat.CI8 || tplFormat == TPL_TextureFormat.CI14X2) { ColorIndexConverter cic = new ColorIndexConverter(imageToRgba(img), img.Width, img.Height, tplFormat, paletteFormat); tempTextureData = cic.Data; tempPaletteData = cic.Palette; tempPaletteHeader.NumberOfItems = (ushort)(tempPaletteData.Length / 2); tempPaletteHeader.PaletteFormat = (uint)paletteFormat; } tplTextureEntries.Add(tempTexture); tplTextureHeaders.Add(tempTextureHeader); tplPaletteHeaders.Add(tempPaletteHeader); textureData.Add(tempTextureData); paletteData.Add(tempPaletteData); tplHeader.NumOfTextures++; }
private void createFromImages(Image[] images, TPL_TextureFormat[] tplFormats, TPL_PaletteFormat[] paletteFormats) { tplHeader = new TPL_Header(); tplTextureEntries = new List<TPL_TextureEntry>(); tplTextureHeaders = new List<TPL_TextureHeader>(); tplPaletteHeaders = new List<TPL_PaletteHeader>(); textureData = new List<byte[]>(); paletteData = new List<byte[]>(); tplHeader.NumOfTextures = (uint)images.Length; for (int i = 0; i < images.Length; i++) { Image img = images[i]; TPL_TextureEntry tempTexture = new TPL_TextureEntry(); TPL_TextureHeader tempTextureHeader = new TPL_TextureHeader(); TPL_PaletteHeader tempPaletteHeader = new TPL_PaletteHeader(); byte[] tempTextureData = imageToTpl(img, tplFormats[i]); byte[] tempPaletteData = new byte[0]; tempTextureHeader.TextureHeight = (ushort)img.Height; tempTextureHeader.TextureWidth = (ushort)img.Width; tempTextureHeader.TextureFormat = (uint)tplFormats[i]; if (tplFormats[i] == TPL_TextureFormat.CI4 || tplFormats[i] == TPL_TextureFormat.CI8 || tplFormats[i] == TPL_TextureFormat.CI14X2) { ColorIndexConverter cic = new ColorIndexConverter(imageToRgba(img), img.Width, img.Height, tplFormats[i], paletteFormats[i]); tempTextureData = cic.Data; tempPaletteData = cic.Palette; tempPaletteHeader.NumberOfItems = (ushort)(tempPaletteData.Length / 2); tempPaletteHeader.PaletteFormat = (uint)paletteFormats[i]; } tplTextureEntries.Add(tempTexture); tplTextureHeaders.Add(tempTextureHeader); tplPaletteHeaders.Add(tempPaletteHeader); textureData.Add(tempTextureData); paletteData.Add(tempPaletteData); } }
private void parseTpl(Stream tplFile) { fireDebug("Parsing TPL..."); tplHeader = new TPL_Header(); tplTextureEntries = new List<TPL_TextureEntry>(); tplTextureHeaders = new List<TPL_TextureHeader>(); tplPaletteHeaders = new List<TPL_PaletteHeader>(); textureData = new List<byte[]>(); paletteData = new List<byte[]>(); tplFile.Seek(0, SeekOrigin.Begin); byte[] temp = new byte[4]; fireDebug(" Reading TPL Header: Magic... (Offset: 0x{0})", tplFile.Position); tplFile.Read(temp, 0, 4); if (Shared.Swap(BitConverter.ToUInt32(temp, 0)) != tplHeader.TplMagic) { fireDebug(" -> Invalid Magic: 0x{0}", Shared.Swap(BitConverter.ToUInt32(temp, 0))); throw new Exception("TPL Header: Invalid Magic!"); } fireDebug(" Reading TPL Header: NumOfTextures... (Offset: 0x{0})", tplFile.Position); tplFile.Read(temp, 0, 4); tplHeader.NumOfTextures = Shared.Swap(BitConverter.ToUInt32(temp, 0)); fireDebug(" Reading TPL Header: Headersize... (Offset: 0x{0})", tplFile.Position); tplFile.Read(temp, 0, 4); if (Shared.Swap(BitConverter.ToUInt32(temp, 0)) != tplHeader.HeaderSize) { fireDebug(" -> Invalid Headersize: 0x{0}", Shared.Swap(BitConverter.ToUInt32(temp, 0))); throw new Exception("TPL Header: Invalid Headersize!"); } for (int i = 0; i < tplHeader.NumOfTextures; i++) { fireDebug(" Reading Texture Entry #{1} of {2}... (Offset: 0x{0})", tplFile.Position, i+1, tplHeader.NumOfTextures); TPL_TextureEntry tempTexture = new TPL_TextureEntry(); tplFile.Read(temp, 0, 4); tempTexture.TextureHeaderOffset = Shared.Swap(BitConverter.ToUInt32(temp, 0)); tplFile.Read(temp, 0, 4); tempTexture.PaletteHeaderOffset = Shared.Swap(BitConverter.ToUInt32(temp, 0)); tplTextureEntries.Add(tempTexture); } for (int i = 0; i < tplHeader.NumOfTextures; i++) { fireDebug(" Reading Texture Header #{1} of {2}... (Offset: 0x{0})", tplFile.Position, i + 1, tplHeader.NumOfTextures); TPL_TextureHeader tempTextureHeader = new TPL_TextureHeader(); TPL_PaletteHeader tempPaletteHeader = new TPL_PaletteHeader(); tplFile.Seek(tplTextureEntries[i].TextureHeaderOffset, SeekOrigin.Begin); tplFile.Read(temp, 0, 4); tempTextureHeader.TextureHeight = Shared.Swap(BitConverter.ToUInt16(temp, 0)); tempTextureHeader.TextureWidth = Shared.Swap(BitConverter.ToUInt16(temp, 2)); tplFile.Read(temp, 0, 4); tempTextureHeader.TextureFormat = Shared.Swap(BitConverter.ToUInt32(temp, 0)); tplFile.Read(temp, 0, 4); tempTextureHeader.TextureDataOffset = Shared.Swap(BitConverter.ToUInt32(temp, 0)); tplFile.Read(temp, 0, 4); tempTextureHeader.WrapS = Shared.Swap(BitConverter.ToUInt32(temp, 0)); tplFile.Read(temp, 0, 4); tempTextureHeader.WrapT = Shared.Swap(BitConverter.ToUInt32(temp, 0)); tplFile.Read(temp, 0, 4); tempTextureHeader.MinFilter = Shared.Swap(BitConverter.ToUInt32(temp, 0)); tplFile.Read(temp, 0, 4); tempTextureHeader.MagFilter = Shared.Swap(BitConverter.ToUInt32(temp, 0)); tplFile.Read(temp, 0, 4); tempTextureHeader.LodBias = Shared.Swap(BitConverter.ToUInt32(temp, 0)); tplFile.Read(temp, 0, 4); tempTextureHeader.EdgeLod = temp[0]; tempTextureHeader.MinLod = temp[1]; tempTextureHeader.MaxLod = temp[2]; tempTextureHeader.Unpacked = temp[3]; if (tplTextureEntries[i].PaletteHeaderOffset != 0) { fireDebug(" Reading Palette Header #{1} of {2}... (Offset: 0x{0})", tplFile.Position, i + 1, tplHeader.NumOfTextures); tplFile.Seek(tplTextureEntries[i].PaletteHeaderOffset, SeekOrigin.Begin); tplFile.Read(temp, 0, 4); tempPaletteHeader.NumberOfItems = Shared.Swap(BitConverter.ToUInt16(temp, 0)); tempPaletteHeader.Unpacked = temp[2]; tempPaletteHeader.Pad = temp[3]; tplFile.Read(temp, 0, 4); tempPaletteHeader.PaletteFormat = Shared.Swap(BitConverter.ToUInt32(temp, 0)); tplFile.Read(temp, 0, 4); tempPaletteHeader.PaletteDataOffset = Shared.Swap(BitConverter.ToUInt32(temp, 0)); } tplFile.Seek(tempTextureHeader.TextureDataOffset, SeekOrigin.Begin); byte[] tempTextureData = new byte[textureByteSize((TPL_TextureFormat)tempTextureHeader.TextureFormat, tempTextureHeader.TextureWidth, tempTextureHeader.TextureHeight)]; byte[] tempPaletteData = new byte[tempPaletteHeader.NumberOfItems * 2]; fireDebug(" Reading Texture #{1} of {2}... (Offset: 0x{0})", tplFile.Position, i + 1, tplHeader.NumOfTextures); tplFile.Read(tempTextureData, 0, tempTextureData.Length); if (tplTextureEntries[i].PaletteHeaderOffset > 0) { fireDebug(" Reading Palette #{1} of {2}... (Offset: 0x{0})", tplFile.Position, i + 1, tplHeader.NumOfTextures); tplFile.Seek(tempPaletteHeader.PaletteDataOffset, SeekOrigin.Begin); tplFile.Read(tempPaletteData, 0, tempPaletteData.Length); } else tempPaletteData = new byte[0]; tplTextureHeaders.Add(tempTextureHeader); tplPaletteHeaders.Add(tempPaletteHeader); textureData.Add(tempTextureData); paletteData.Add(tempPaletteData); } }