Exemple #1
0
        public static void ConvertImageToTxm(string inPath, Stream outStream, byte level = 1, ushort bufferBase = 0, ushort paletteBufferBase = 0)
        {
            using (var image = Image.Load <Rgba32>(inPath))
            {
                // Gather all colors to see if it would fit in PSMT8
                TxmPixelFormat   pixelFormat = TxmPixelFormat.None;
                HashSet <Rgba32> colorSet    = new HashSet <Rgba32>();
                List <Rgba32>    palette     = null;
                for (int y = 0; y < image.Height; ++y)
                {
                    var row = image.GetPixelRowSpan(y);
                    for (int x = 0; x < image.Width; ++x)
                    {
                        colorSet.Add(row[x]);
                        if (colorSet.Count > 256)
                        {
                            pixelFormat = TxmPixelFormat.PSMCT32;
                            y           = image.Height;
                            break;
                        }
                    }
                }

                short paletteWidth  = 0;
                short paletteHeight = 0;
                if (pixelFormat == TxmPixelFormat.None)
                {
                    // Palette check passed, assign palettized pixel format
                    if (colorSet.Count > 16)
                    {
                        pixelFormat   = TxmPixelFormat.PSMT8;
                        paletteWidth  = 16;
                        paletteHeight = 16;
                    }
                    else
                    {
                        pixelFormat   = TxmPixelFormat.PSMT4;
                        paletteWidth  = 8;
                        paletteHeight = 2;
                    }
                    palette = new List <Rgba32>(colorSet);
                }

                // Write header
                BinaryWriter bw        = new BinaryWriter(outStream);
                TxmHeader    txmHeader = new TxmHeader
                {
                    ImageSourcePixelFormat = pixelFormat,
                    ImageVideoPixelFormat  = pixelFormat,
                    ImageWidth             = (short)image.Width,
                    ImageHeight            = (short)image.Height,
                    ImageBufferBase        = bufferBase,
                    ClutPixelFormat        = palette != null ? TxmPixelFormat.PSMCT32 : TxmPixelFormat.None,
                    Misc           = (byte)(level & 0x0f),
                    ClutWidth      = paletteWidth,
                    ClutHeight     = paletteHeight,
                    ClutBufferBase = paletteBufferBase
                };
                txmHeader.Write(bw);

                // Write palette
                int palettePixelsWritten = 0;
                if (pixelFormat == TxmPixelFormat.PSMT4)
                {
                    foreach (var color in palette)
                    {
                        bw.Write(color.R);
                        bw.Write(color.G);
                        bw.Write(color.B);
                        bw.Write((byte)((color.A + 1) >> 1));
                        ++palettePixelsWritten;
                    }
                }
                else if (pixelFormat == TxmPixelFormat.PSMT8)
                {
                    int    baseOffset = 0;
                    Rgba32 black      = new Rgba32();

                    int[] order = new int[]
                    {
                        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
                        0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
                    };

                    while (palettePixelsWritten < palette.Count)
                    {
                        foreach (var offset in order)
                        {
                            var palOffset = baseOffset + offset;
                            var color     = palOffset < palette.Count ? palette[palOffset] : black;
                            bw.Write(color.R);
                            bw.Write(color.G);
                            bw.Write(color.B);
                            bw.Write((byte)((color.A + 1) >> 1));
                            ++palettePixelsWritten;
                        }

                        baseOffset += order.Length;
                    }
                }

                // Pad out rest of palette
                int targetOffset = 16 + (txmHeader.GetClutByteSize() + 15) / 16 * 16;
                while (outStream.Position < targetOffset)
                {
                    bw.Write((byte)0);
                }

                // Write main image data
                byte pal4BppBuffer = 0;
                bool odd           = false;
                for (int y = 0; y < image.Height; ++y)
                {
                    var row = image.GetPixelRowSpan(y);
                    for (int x = 0; x < image.Width; ++x)
                    {
                        var pixel = row[x];
                        if (pixelFormat == TxmPixelFormat.PSMCT32)
                        {
                            bw.Write(pixel.R);
                            bw.Write(pixel.G);
                            bw.Write(pixel.B);
                            bw.Write(pixel.A); // Should be halved, but full range is used on PC
                        }
                        else
                        {
                            var palIndex = palette.IndexOf(pixel);
                            if (pixelFormat == TxmPixelFormat.PSMT4)
                            {
                                pal4BppBuffer <<= 4;
                                pal4BppBuffer  |= (byte)(palIndex & 0x0f);
                                odd             = !odd;
                                if (!odd)
                                {
                                    bw.Write(pal4BppBuffer);
                                    pal4BppBuffer = 0;
                                }
                            }
                            else
                            {
                                bw.Write((byte)palIndex);
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        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;
            }
        }