Beispiel #1
0
        private void ReadPalette(BinaryReader reader)
        {
            int paletteWH;

            if (PixelFormat == PS2PixelFormat.PSMT4 || PixelFormat == PS2PixelFormat.PSMT4HL || PixelFormat == PS2PixelFormat.PSMT4HH)
            {
                PaletteColorCount = 16;
                paletteWH         = 4;
            }
            else
            {
                PaletteColorCount = 256;
                paletteWH         = 16;
            }

            Palettes = new Color[PaletteCount][];

            for (int i = 0; i < PaletteCount; i++)
            {
                PS2PixelFormatHelper.ReadPixelData(PaletteFormat, reader, paletteWH, paletteWH, out Palettes[i]);

                if (PaletteColorCount == 256)
                {
                    PS2PixelFormatHelper.TilePalette(Palettes[i], out Palettes[i]);
                }
            }
        }
        /// <summary>
        /// Initializer only to be called by the <see cref="RwNodeFactory"/>.
        /// </summary>
        internal RwRasterDataStructNode(RwNodeFactory.RwNodeHeader header, BinaryReader reader)
            : base(header)
        {
            long start = reader.BaseStream.Position;

            if (RasterInfoStructNode.Format.HasFlagUnchecked(RwRasterFormats.HasHeaders))
            {
                mImageHeader = new PS2StandardImageHeader(reader);
            }

            if (PS2PixelFormatHelper.IsIndexedPixelFormat(RasterInfoStructNode.Tex0Register.TexturePixelFormat))
            {
                ReadIndices(reader);

                if (RasterInfoStructNode.Format.HasFlagUnchecked(RwRasterFormats.HasHeaders))
                {
                    mPaletteHeader = new PS2StandardImageHeader(reader);
                }

                ReadPalette(reader);
            }
            else
            {
                ReadPixels(reader);
            }

            long end = reader.BaseStream.Position;

            mMipMapData = reader.ReadBytes((int)((start + Size) - end));
        }
Beispiel #3
0
        private void WritePalette(BinaryWriter writer)
        {
            Color[][] outPaletteEntries = null;

            int paletteWH = PS2PixelFormatHelper.GetPaletteDimension(PaletteFormat);

            if (PaletteColorCount == 256)
            {
                outPaletteEntries = new Color[PaletteCount][];

                // Tile the palette
                for (int i = 0; i < PaletteCount; i++)
                {
                    PS2PixelFormatHelper.TilePalette(Palettes[i], out outPaletteEntries[i]);
                }
            }
            else
            {
                outPaletteEntries = Palettes;
            }

            for (int i = 0; i < PaletteCount; i++)
            {
                PS2PixelFormatHelper.WritePixelData(PaletteFormat, writer, paletteWH, paletteWH, outPaletteEntries[i]);
            }
        }
        /// <summary>
        /// Inherited from <see cref="RwNode"/>. Writes the data beyond the header.
        /// </summary>
        /// <param name="writer">The <see cref="BinaryWriter"/> to write the data with.</param>
        protected internal override void WriteBody(BinaryWriter writer)
        {
            if (RasterInfoStructNode.Format.HasFlagUnchecked(RwRasterFormats.HasHeaders))
            {
                writer.Write(mImageHeader.GetBytes());
            }

            if (PS2PixelFormatHelper.IsIndexedPixelFormat(RasterInfoStructNode.Tex0Register.TexturePixelFormat))
            {
                WriteIndices(writer);

                if (RasterInfoStructNode.Format.HasFlagUnchecked(RwRasterFormats.HasHeaders))
                {
                    writer.Write(mPaletteHeader.GetBytes());
                }

                WritePalette(writer);
            }
            else
            {
                WritePixels(writer);
            }

            writer.Write(mMipMapData);
        }
Beispiel #5
0
        private void ReadPixels(BinaryReader reader)
        {
            if (UsesPalette)
            {
                byte[] temp;
                PS2PixelFormatHelper.ReadPixelData(PixelFormat, reader, Width, Height, out temp);
                PixelIndices = temp;

                MipMapPixelIndices = new byte[MipMapCount][];
                for (int i = 0; i < MipMapCount; i++)
                {
                    int div = 2 * (2 * i);
                    PS2PixelFormatHelper.ReadPixelData(PixelFormat, reader, Width / div, Height / div, out MipMapPixelIndices[i]);
                }
            }
            else
            {
                Color[] temp;
                PS2PixelFormatHelper.ReadPixelData(PixelFormat, reader, Width, Height, out temp);
                Pixels = temp;

                MipMapPixels = new Color[MipMapCount][];
                for (int i = 0; i < MipMapCount; i++)
                {
                    int div = 2 * (2 * i);
                    PS2PixelFormatHelper.ReadPixelData(PixelFormat, reader, Width / div, Height / div, out MipMapPixels[i]);
                }
            }
        }
Beispiel #6
0
 private static void ScalePSMCT32PixelsToHalfAlphaRange(Color[] colors)
 {
     for (int c = 0; c < colors.Length; c++)
     {
         colors[c] = Color.FromArgb(
             PS2PixelFormatHelper.ScaleFullRangeAlphaToHalfRange(colors[c].A),
             colors[c].R,
             colors[c].G,
             colors[c].B);
     }
 }
        /// <summary>
        /// Read the palette from the stream using given pixel format, untile it, and set the data as the palette member value.
        /// </summary>
        private void ReadPalette(BinaryReader reader)
        {
            int paletteWH = PS2PixelFormatHelper.GetPaletteDimension(RasterInfoStructNode.Tex0Register.TexturePixelFormat);

            PS2PixelFormatHelper.ReadPixelData(RasterInfoStructNode.Tex0Register.ClutPixelFormat, reader, paletteWH, paletteWH, out mPalette);

            if (paletteWH == 16)
            {
                PS2PixelFormatHelper.TilePalette(mPalette, out mPalette);
            }
        }
        /// <summary>
        /// Initialize a new instanc eof <see cref="RwRasterDataStructNode"/> using a given bitmap and a PS2 <see cref="PS2.Graphics.PS2PixelFormat"/> to encode the bitmap to.
        /// </summary>
        /// <param name="bitmap">Bitmap to be encoded using the given pixel format.</param>
        /// <param name="pixelFormat">The pixel format the bitmap will be encoded to and stored in the texture data.</param>
        public RwRasterDataStructNode(Bitmap bitmap, PS2PixelFormat pixelFormat)
            : base(RwNodeId.RwStructNode)
        {
            if (PS2PixelFormatHelper.IsIndexedPixelFormat(pixelFormat))
            {
                BitmapHelper.QuantizeBitmap(bitmap, PS2PixelFormatHelper.GetIndexedColorCount(pixelFormat), out mIndices, out mPalette);
            }
            else
            {
                mPixels = BitmapHelper.GetColors(bitmap)
                          .Select(x => Color.FromArgb(PS2PixelFormatHelper.ScaleFullRangeAlphaToHalfRange(x.A), x.R, x.G, x.B))
                          .ToArray();
            }

            mMipMapData = new byte[0];
        }
Beispiel #9
0
        private static Color[] ScalePSMCT32PixelsToFullAlphaRange(Color[] colors)
        {
            int colorCount = colors.Length;

            Color[] scaledColors = new Color[colorCount];

            for (int c = 0; c < colorCount; c++)
            {
                scaledColors[c] = Color.FromArgb(
                    PS2PixelFormatHelper.ScaleHalfRangeAlphaToFullRange(colors[c].A),
                    colors[c].R,
                    colors[c].G,
                    colors[c].B);
            }

            return(scaledColors);
        }
Beispiel #10
0
        public Color[] GetPixels()
        {
            if (IsIndexed && mRasterStructNode.DataStructNode.Pixels == null)
            {
                mRasterStructNode.DataStructNode.Pixels = new Color[Width * Height];
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        var color = Palette[PixelIndices[x + y * Width]];
                        mRasterStructNode.DataStructNode.Pixels[x + y * Width] =
                            Color.FromArgb(PS2PixelFormatHelper.ScaleHalfRangeAlphaToFullRange(color.A), color.R, color.G, color.B);
                    }
                }
            }

            return(mRasterStructNode.DataStructNode.Pixels);
        }
        /// <summary>
        /// Make a copy of the palette, tile it, and write the palette to the stream using set pixel format.
        /// </summary>
        private void WritePalette(BinaryWriter writer)
        {
            int paletteWH = PS2PixelFormatHelper.GetPaletteDimension(RasterInfoStructNode.Tex0Register.TexturePixelFormat);

            Color[] outPalette;

            if (paletteWH == 16)
            {
                PS2PixelFormatHelper.TilePalette(mPalette, out outPalette);
            }
            else
            {
                outPalette = mPalette;
            }

            switch (RasterInfoStructNode.Tex0Register.ClutPixelFormat)
            {
            case PS2PixelFormat.PSMZ32:
            case PS2PixelFormat.PSMTC32:
                PS2PixelFormatHelper.WritePSMCT32(writer, paletteWH, paletteWH, outPalette);
                break;

            case PS2PixelFormat.PSMZ24:
            case PS2PixelFormat.PSMTC24:
                PS2PixelFormatHelper.WritePSMCT24(writer, paletteWH, paletteWH, outPalette);
                break;

            case PS2PixelFormat.PSMZ16:
            case PS2PixelFormat.PSMTC16:
                PS2PixelFormatHelper.WritePSMCT16(writer, paletteWH, paletteWH, outPalette);
                break;

            case PS2PixelFormat.PSMZ16S:
            case PS2PixelFormat.PSMTC16S:
                PS2PixelFormatHelper.WritePSMCT16S(writer, paletteWH, paletteWH, outPalette);
                break;

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #12
0
 private void WritePixels(BinaryWriter writer)
 {
     if (UsesPalette)
     {
         PS2PixelFormatHelper.WritePixelData(PixelFormat, writer, Width, Height, PixelIndices);
         for (int i = 0; i < MipMapCount; i++)
         {
             int div = 2 * (2 * i);
             PS2PixelFormatHelper.WritePixelData(PixelFormat, writer, Width / div, Height / div, MipMapPixelIndices[i]);
         }
     }
     else
     {
         PS2PixelFormatHelper.WritePixelData(PixelFormat, writer, Width, Height, Pixels);
         for (int i = 0; i < MipMapCount; i++)
         {
             int div = 2 * (2 * i);
             PS2PixelFormatHelper.WritePixelData(PixelFormat, writer, Width / div, Height / div, MipMapPixels[i]);
         }
     }
 }
        /// <summary>
        /// Make a copy of the pixel indices, swizzle it, and write it to the stream.
        /// </summary>
        private void WriteIndices(BinaryWriter writer)
        {
            byte[] outIndices;
            switch (RasterInfoStructNode.Tex0Register.TexturePixelFormat)
            {
            case PS2PixelFormat.PSMT8H:
            case PS2PixelFormat.PSMT8:
                PS2PixelFormatHelper.Swizzle8(RasterInfoStructNode.Width, RasterInfoStructNode.Height, mIndices, out outIndices);
                PS2PixelFormatHelper.WritePSMT8(writer, RasterInfoStructNode.Width, RasterInfoStructNode.Height, outIndices);
                break;

            case PS2PixelFormat.PSMT4HL:
            case PS2PixelFormat.PSMT4HH:
            case PS2PixelFormat.PSMT4:
                PS2PixelFormatHelper.Swizzle8(RasterInfoStructNode.Width, RasterInfoStructNode.Height, mIndices, out outIndices);
                PS2PixelFormatHelper.WritePSMT4(writer, RasterInfoStructNode.Width, RasterInfoStructNode.Height, outIndices);
                break;

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #14
0
        /// <summary>
        /// <para>Construct a bitmap from the data in this instance.</para>
        /// <para>Subsequent calls to this method will return the same bitmap instance without constructing a new one.</para>
        /// </summary>
        public Bitmap GetBitmap()
        {
            if (mBitmap != null)
            {
                return(mBitmap);
            }

            if (IsIndexed)
            {
                mBitmap =
                    BitmapHelper.Create(Palette.Select(x => Color.FromArgb(PS2PixelFormatHelper.ScaleHalfRangeAlphaToFullRange(x.A), x.R, x.G, x.B)).ToArray(),
                                        PixelIndices, Width, Height);
            }
            else
            {
                mBitmap =
                    BitmapHelper.Create(Pixels.Select(x => Color.FromArgb(PS2PixelFormatHelper.ScaleHalfRangeAlphaToFullRange(x.A), x.R, x.G, x.B)).ToArray(),
                                        Width, Height);
            }

            return(mBitmap);
        }
        /// <summary>
        /// Sets the appropriate rasterStructNode info values based on the width, height and pixel format of the texture.
        /// </summary>
        /// <param name="width">Width of the texture.</param>
        /// <param name="height">Height of the texture.</param>
        /// <param name="pixelFormat">PS2 pixel format of the given texture data.</param>
        private void CreateRasterInfo(int width, int height, PS2PixelFormat pixelFormat)
        {
            Width  = width;
            Height = height;
            Depth  = PS2PixelFormatHelper.GetPixelFormatDepth(pixelFormat);
            Format = GetRasterFormatFromDepth(Depth);

            Tex0Register = new Tex0Register(pixelFormat, width, height);
            Tex1Register = new Tex1Register(); // default settings

            int texSize = width * height;

            if (texSize < 16384) // because yes
            {
                Tex1Register.MaxMipLevel  = 7;
                Tex1Register.MipMinFilter = PS2FilterMode.None;

                if (texSize <= 4096)
                {
                    Tex1Register.MipMaxFilter = PS2FilterMode.None;
                }
                else
                {
                    Tex1Register.MipMaxFilter = PS2FilterMode.Nearest;
                }
            }

            MipTBP1Register = new MipTbpRegister(); // default settings
            MipTBP2Register = new MipTbpRegister(); // default settings

            TexelDataLength = (uint)PS2PixelFormatHelper.GetTexelDataSize(pixelFormat, width, height);

            // Add image header size to the length if there is one
            if (Format.HasFlagUnchecked(RwRasterFormats.HasHeaders))
            {
                TexelDataLength += PS2StandardImageHeader.Size;
            }

            PaletteDataLength = 0; // set to 0 if there's no palette present

            // division by 2 might only apply for 8 bit..
            int transferWidth  = width / 2;
            int transferHeight = height / 2;

            GpuAlignedLength = (uint)(transferWidth * transferHeight);

            // Calculate the palette data length for indexed textures
            if (Depth == 4 || Depth == 8)
            {
                int numColors = 256;

                if (Depth == 4)
                {
                    numColors = 16;
                }

                PaletteDataLength = (uint)(numColors * 4);

                // division by 4 might only apply for 8 bit..
                GpuAlignedLength += PaletteDataLength / 4;

                // Add image header size to the length if there is one
                if (Format.HasFlagUnchecked(RwRasterFormats.HasHeaders))
                {
                    PaletteDataLength += PS2StandardImageHeader.Size;
                }
            }

            // align to pages of 2048
            GpuAlignedLength = (uint)AlignmentHelper.Align(GpuAlignedLength, 2048);
        }
 private void WritePixels(BinaryWriter writer)
 {
     PS2PixelFormatHelper.WritePixelData(RasterInfoStructNode.Tex0Register.TexturePixelFormat, writer, RasterInfoStructNode.Width, RasterInfoStructNode.Height, mPixels);
 }
Beispiel #17
0
 public RwTextureNativeNode(string name, Bitmap bitmap)
     : this(name, bitmap, PS2PixelFormatHelper.GetBestPixelFormat(bitmap))
 {
 }
 /// <summary>
 /// Read the pixel indices from the stream using given info, unswizzle it, and set the data as the pixel indices member value.
 /// </summary>
 private void ReadIndices(BinaryReader reader)
 {
     PS2PixelFormatHelper.ReadPixelData(RasterInfoStructNode.Tex0Register.TexturePixelFormat, reader, RasterInfoStructNode.Width, RasterInfoStructNode.Height, out mIndices);
     PS2PixelFormatHelper.UnSwizzle8(RasterInfoStructNode.Width, RasterInfoStructNode.Height, mIndices, out mIndices);
 }
 /// <summary>
 /// Read the pixel data from the stream using given info.
 /// </summary>
 /// <param name="reader"></param>
 private void ReadPixels(BinaryReader reader)
 {
     PS2PixelFormatHelper.ReadPixelData(RasterInfoStructNode.Tex0Register.TexturePixelFormat, reader, RasterInfoStructNode.Width, RasterInfoStructNode.Height, out mPixels);
 }