static void ExtractFontPack(string pakPath, string txmPath, string basePath) { if (basePath == null) { basePath = pakPath + "_extracted"; } using (Stream fs = Utils.CheckDecompress(File.OpenRead(txmPath))) using (Stream datFs = Utils.CheckDecompress(File.OpenRead(pakPath))) { var fontTxmHeader = new TxmHeader(); BinaryReader br = new BinaryReader(fs); fontTxmHeader.Read(br); var palette = TxmConversion.ReadRgba32Palette(br, fontTxmHeader.ClutWidth, fontTxmHeader.ClutHeight); var fontPak = new FontPack(); fontPak.Read(datFs); Directory.CreateDirectory(basePath); foreach (var ch in fontPak.Characters) { using MemoryStream ms = new MemoryStream(fontPak[ch]); BinaryReader charBr = new BinaryReader(ms); using (var img = TxmConversion.ConvertTxmIndexed4bpp(charBr, 24, 22, palette)) { img.SaveAsPng(Path.Combine(basePath, $"{ch}.png")); } } } }
public static Image <Rgba32> ConvertTxmToImage(Stream stream) { BinaryReader br = new BinaryReader(stream); TxmHeader imageHeader = new TxmHeader(); imageHeader.Read(br); Console.WriteLine(imageHeader); //if (imageHeader.Misc != 1) // Console.WriteLine("Different level!"); Image <Rgba32> image; if (imageHeader.ImageSourcePixelFormat == TxmPixelFormat.PSMT8 || imageHeader.ImageSourcePixelFormat == TxmPixelFormat.PSMT4) { Rgba32[] palette = null; if (imageHeader.ClutPixelFormat == TxmPixelFormat.PSMCT32) { stream.Seek(16, SeekOrigin.Begin); palette = ReadRgba32Palette(br, imageHeader.ClutWidth, imageHeader.ClutHeight); //fs.Seek(16, SeekOrigin.Begin); //using (var palImage = ConvertTxmRgba32(br, imageHeader.ClutWidth, imageHeader.ClutHeight)) //{ // palImage.SaveAsPng(Path.ChangeExtension(outPath, ".pal.png")); //} } else { throw new NotSupportedException("Unsupported pixel format from second texture"); } stream.Seek(16 + (imageHeader.GetClutByteSize() + 15) / 16 * 16, SeekOrigin.Begin); if (imageHeader.ImageSourcePixelFormat == TxmPixelFormat.PSMT8) { image = ConvertTxmIndexed8bpp(br, imageHeader.ImageWidth, imageHeader.ImageHeight, palette); } else { image = ConvertTxmIndexed4bpp(br, imageHeader.ImageWidth, imageHeader.ImageHeight, palette); } } else if (imageHeader.ImageSourcePixelFormat == TxmPixelFormat.PSMCT32) { stream.Seek(16, SeekOrigin.Begin); image = ConvertTxmRgba32(br, imageHeader.ImageWidth, imageHeader.ImageHeight); } else { throw new NotSupportedException("Unsupported pixel format"); } return(image); }
static void ReplaceDatImages(string srcDatPath, string destDatPath, string replacementList) { List <string> tempPaths = new List <string>(); try { using (DatReader dat = new DatReader(File.OpenRead(srcDatPath))) { DatBuilder builder = new DatBuilder(dat); using (StreamReader sr = File.OpenText(replacementList)) { while (!sr.EndOfStream) { var line = sr.ReadLine().Trim(); if (line.Length == 0 || line.StartsWith("#")) { continue; } var lineSplit = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries); if (lineSplit.Length != 2) { throw new InvalidDataException($"Invalid line \"{line}\"."); } if (!int.TryParse(lineSplit[0], out var imageIndex)) { throw new InvalidDataException($"Invalid index on line \"{line}\"."); } byte level = 1; ushort bufferBase = 0; ushort paletteBufferBase = 0; if (imageIndex < dat.EntriesCount) { using (MemoryStream ms = new MemoryStream(dat.GetData(imageIndex))) { TxmHeader txm = new TxmHeader(); txm.Read(new BinaryReader(ms)); level = (byte)(txm.Misc & 0x0f); bufferBase = txm.ImageBufferBase; paletteBufferBase = txm.ClutBufferBase; } } string tempPath = Path.GetTempFileName(); tempPaths.Add(tempPath); using (FileStream fs = File.Create(tempPath)) { TxmConversion.ConvertImageToTxm(lineSplit[1], fs, level, bufferBase, paletteBufferBase); } builder.ReplacementEntries.Add(new DatBuilder.ReplacementEntry { Index = imageIndex, SourceFile = tempPath }); } } using (FileStream fs = File.Create(destDatPath)) { builder.Build(fs); } } } finally { foreach (var path in tempPaths) { File.Delete(path); } } }
public void ExportTextures(StreamWriter mtlWriter, string outputPath, bool forceDirect = false) { if (disposedValue) { throw new ObjectDisposedException(GetType().FullName); } if (textureDat == null) { throw new InvalidOperationException("No texture pack supplied."); } int i = 0; numWrittenTextures = 0; foreach (var pair in textureCache.OrderBy(p => p.Key)) { string pngPath = $"{outputPath}{i}.png"; string alphaPath = $"{outputPath}{i}_alpha.png"; TxmHeader textureTxm = pair.Value; int txmIndex = (int)(pair.Key >> 32); using (var txmMs = new MemoryStream(textureDat.GetData(txmIndex))) { BinaryReader txmBr = new BinaryReader(txmMs); TxmHeader pakTxm = new TxmHeader(); pakTxm.Read(txmBr); Image <Rgba32> img = null; try { // Check if TXM is already suitable if (forceDirect || /*pakTxm.ImageSourcePixelFormat == textureTxm.ImageSourcePixelFormat &&*/ pakTxm.ImageBufferBase == textureTxm.ImageBufferBase && pakTxm.ClutPixelFormat == textureTxm.ClutPixelFormat && pakTxm.ClutBufferBase == textureTxm.ClutBufferBase) { // Use TXM as-is txmMs.Seek(0, SeekOrigin.Begin); if (new string(txmBr.ReadChars(4)) == "DAT\0") { // Unwrap DAT txmMs.Seek(0, SeekOrigin.Begin); using (DatReader txmDat = new DatReader(txmMs)) { if (txmDat.EntriesCount != 1) { throw new InvalidDataException("Nested texture DAT contains more than one file."); } using (MemoryStream innerStream = new MemoryStream(txmDat.GetData(0))) { img = TxmConversion.ConvertTxmToImage(innerStream); } } } else { txmMs.Seek(0, SeekOrigin.Begin); img = TxmConversion.ConvertTxmToImage(txmMs); // Dump palette //if (pakTxm.ClutPixelFormat != TxmPixelFormat.None) //{ // txmMs.Seek(0x10, SeekOrigin.Begin); // using (var palette = TxmConversion.ConvertTxmRgba32(txmBr, pakTxm.ClutWidth, pakTxm.ClutHeight)) // { // palette.SaveAsPng($"palette_{numWrittenTextures}.png"); // } //} } } else { // Generate new TXM using (MemoryStream ms = new MemoryStream()) { BinaryWriter bw = new BinaryWriter(ms); textureTxm.Write(bw); CopyTexelsClut(txmBr, bw, pakTxm, textureTxm); CopyTexels(txmBr, bw, pakTxm, textureTxm); bw.Flush(); ms.Seek(0, SeekOrigin.Begin); img = TxmConversion.ConvertTxmToImage(ms); } } // Save out color texture using (var img24bpp = img.CloneAs <Rgb24>()) { img24bpp.SaveAsPng(pngPath); } // Extract alpha channel as a separate image using (var alphaImg = new Image <L8>(img.Width, img.Height)) { for (int y = 0; y < alphaImg.Height; ++y) { var srcSpan = img.GetPixelRowSpan(y); var destSpan = alphaImg.GetPixelRowSpan(y); for (int x = 0; x < alphaImg.Width; ++x) { var srcAlpha = srcSpan[x].A; destSpan[x] = new L8(srcAlpha); } } alphaImg.SaveAsPng(alphaPath); } } finally { if (img != null) { img.Dispose(); } } } mtlWriter.WriteLine($"newmtl tex_{pair.Key:x12}"); mtlWriter.WriteLine("Kd 0.80000000 0.80000000 0.80000000"); mtlWriter.WriteLine("Ka 0 0 0"); mtlWriter.WriteLine("Ke 0 0 0"); mtlWriter.WriteLine("Ks 0 0 0"); mtlWriter.WriteLine("d 1"); mtlWriter.WriteLine("illum 2"); mtlWriter.WriteLine($"map_Kd {Path.GetFileName(pngPath)}"); mtlWriter.WriteLine($"map_d {Path.GetFileName(alphaPath)}"); mtlWriter.WriteLine(); ++i; ++numWrittenTextures; } }