Esempio n. 1
0
        void CopyTexelsClut(BinaryReader br, BinaryWriter bw, TxmHeader pakTxm, TxmHeader textureTxm)
        {
            if (pakTxm.ClutPixelFormat != TxmPixelFormat.None)
            {
                throw new ArgumentException("Cannot operate on source TXM with CLUT.", nameof(pakTxm));
            }
            if (textureTxm.ClutPixelFormat == TxmPixelFormat.None)
            {
                return;
            }

            var destColumnParams = GsMemoryUtils.GetColumnParams(textureTxm.ClutPixelFormat);
            int copyLength       = textureTxm.GetClutByteSize();
            int baseBlockNumber  = textureTxm.ClutBufferBase - pakTxm.ImageBufferBase;
            int srcBase          = 0x10 + pakTxm.GetClutByteSize();
            var destBase         = 0x10;
            int bytesPerSrcLine  = pakTxm.GetImageByteSize() / pakTxm.ImageHeight;
            int bytesPerDestLine = textureTxm.GetClutByteSize() / textureTxm.ClutHeight;

            bw.Write(new byte[copyLength]);
            int numXBlocks = textureTxm.ClutWidth / destColumnParams.Width;

            if (numXBlocks == 0)
            {
                numXBlocks = 1;
            }
            int numYBlocks = textureTxm.ClutHeight / (destColumnParams.Height * GsMemoryUtils.COLUMNS_PER_BLOCK);

            if (numYBlocks == 0)
            {
                numYBlocks = 1;
            }
            int destBlock = 0;

            for (int blockY = 0; blockY < numYBlocks; ++blockY)
            {
                for (int blockX = 0; blockX < numXBlocks; ++blockX)
                {
                    int blockNumber = baseBlockNumber + GsMemoryUtils.CalcBlockNumber(textureTxm.ClutPixelFormat, blockX, blockY, 1);
                    br.BaseStream.Seek(srcBase + GsMemoryUtils.CalcBlockMemoryOffset(pakTxm.ImageSourcePixelFormat, blockNumber),
                                       SeekOrigin.Begin);
                    bw.BaseStream.Seek(destBase + GsMemoryUtils.CalcTxmImageOffset(destColumnParams, destBlock, textureTxm.ClutWidth),
                                       SeekOrigin.Begin);
                    for (int i = 0; i < GsMemoryUtils.COLUMNS_PER_BLOCK; ++i)
                    {
                        byte[] col = GsMemoryUtils.ReadColumn(br, pakTxm.ImageSourcePixelFormat, bytesPerSrcLine);
                        GsMemoryUtils.WriteColumn(bw, textureTxm.ClutPixelFormat, bytesPerDestLine, col);
                    }
                    ++destBlock;
                }
            }

            // Dump palette
            //bw.BaseStream.Seek(destBase, SeekOrigin.Begin);
            //BinaryReader palBr = new BinaryReader(bw.BaseStream);
            //using (var palette = TxmConversion.ConvertTxmRgba32(palBr, textureTxm.ClutWidth, textureTxm.ClutHeight))
            //{
            //    palette.SaveAsPng($"palette_{numWrittenTextures}.png");
            //}
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        void CopyTexels(BinaryReader br, BinaryWriter bw, TxmHeader pakTxm, TxmHeader textureTxm)
        {
            if (pakTxm.ClutPixelFormat != TxmPixelFormat.None)
            {
                throw new ArgumentException("Cannot operate on source TXM with CLUT.", nameof(pakTxm));
            }

            var destColumnParams = GsMemoryUtils.GetColumnParams(textureTxm.ImageSourcePixelFormat);
            int copyLength       = textureTxm.GetImageByteSize();
            int srcBase          = 0x10 + pakTxm.GetClutByteSize();
            int baseBlockNumber  = textureTxm.ImageBufferBase - pakTxm.ImageBufferBase;
            int destBase         = 0x10 + textureTxm.GetClutByteSize();
            int bytesPerSrcLine  = pakTxm.GetImageByteSize() / pakTxm.ImageHeight;
            int bytesPerDestLine = copyLength / textureTxm.ImageHeight;

            bw.Write(new byte[copyLength]);
            int numXBlocks = textureTxm.ImageWidth / destColumnParams.Width;

            if (numXBlocks == 0)
            {
                numXBlocks = 1;
            }
            int numYBlocks = textureTxm.ImageHeight / (destColumnParams.Height * GsMemoryUtils.COLUMNS_PER_BLOCK);

            if (numYBlocks == 0)
            {
                numYBlocks = 1;
            }
            int destBlock = 0;

            for (int blockY = 0; blockY < numYBlocks; ++blockY)
            {
                for (int blockX = 0; blockX < numXBlocks; ++blockX)
                {
                    int blockNumber = baseBlockNumber + GsMemoryUtils.CalcBlockNumber(textureTxm.ImageSourcePixelFormat, blockX, blockY, textureTxm.Misc);
                    br.BaseStream.Seek(srcBase + GsMemoryUtils.CalcBlockMemoryOffset(pakTxm.ImageSourcePixelFormat, blockNumber),
                                       SeekOrigin.Begin);
                    bw.BaseStream.Seek(destBase + GsMemoryUtils.CalcTxmImageOffset(destColumnParams, destBlock, textureTxm.ImageWidth),
                                       SeekOrigin.Begin);
                    for (int i = 0; i < GsMemoryUtils.COLUMNS_PER_BLOCK; ++i)
                    {
                        byte[] col = GsMemoryUtils.ReadColumn(br, pakTxm.ImageSourcePixelFormat, bytesPerSrcLine);
                        if (pakTxm.ImageSourcePixelFormat != textureTxm.ImageSourcePixelFormat)
                        {
                            col = PsmtMixer.MixColumn(col, pakTxm.ImageSourcePixelFormat, textureTxm.ImageSourcePixelFormat, i % 2 != 0);
                        }
                        GsMemoryUtils.WriteColumn(bw, textureTxm.ImageSourcePixelFormat, bytesPerDestLine, col);
                    }

                    ++destBlock;
                }
            }
        }
Esempio n. 4
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);
                            }
                        }
                    }
                }
            }
        }