Ejemplo n.º 1
0
        /// <summary>
        /// Saves the encoded texture to the specified stream.
        /// </summary>
        /// <param name="destination">The stream to save the texture to.</param>
        public void Save(Stream destination)
        {
            MemoryStream textureStream = EncodeTexture();

            textureStream.Position = 0;
            PTStream.CopyTo(textureStream, destination);
        }
        public MemoryStream EncodePalette()
        {
            // Calculate what the length of the palette will be
            int paletteLength = 16 + (m_paletteEntries * m_pixelCodec.Bpp / 8);

            MemoryStream destination = new MemoryStream(paletteLength);

            // Write out the PVPL header
            destination.WriteByte((byte)'P');
            destination.WriteByte((byte)'V');
            destination.WriteByte((byte)'P');
            destination.WriteByte((byte)'L');

            PTStream.WriteInt32(destination, paletteLength - 8);

            destination.WriteByte((byte)m_pixelFormat);
            destination.WriteByte(0);

            PTStream.WriteUInt32(destination, 0);

            PTStream.WriteUInt16(destination, m_paletteEntries);

            // Write the palette data
            byte[] palette = m_pixelCodec.EncodePalette(m_decodedPalette, m_paletteEntries);
            destination.Write(palette, 0, palette.Length);

            return(destination);
        }
        /// <summary>
        /// Saves the encoded palette to the specified stream.
        /// </summary>
        /// <param name="destination">The stream to save the texture to.</param>
        public void Save(Stream destination)
        {
            MemoryStream paletteStream = EncodePalette();

            paletteStream.Position = 0;
            PTStream.CopyTo(paletteStream, destination);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Saves the encoded texture to the specified path.
 /// </summary>
 /// <param name="path">Name of the file to save the data to.</param>
 public void Save(string path)
 {
     using (FileStream destination = File.Create(path))
     {
         MemoryStream textureStream = EncodeTexture();
         textureStream.Position = 0;
         PTStream.CopyTo(textureStream, destination);
     }
 }
 /// <summary>
 /// Saves the encoded palette to the specified path.
 /// </summary>
 /// <param name="path">Name of the file to save the data to.</param>
 public void Save(string path)
 {
     using (FileStream destination = File.Create(path))
     {
         MemoryStream paletteStream = EncodePalette();
         paletteStream.Position = 0;
         PTStream.CopyTo(paletteStream, destination);
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Opens a texture to encode from a stream.
        /// </summary>
        /// <param name="source">Stream that contains the texture data.</param>
        /// <param name="length">Number of bytes to read.</param>
        /// <param name="pixelFormat">Pixel format to encode the texture to.</param>
        /// <param name="dataFormat">Data format to encode the texture to.</param>
        public PvrTextureEncoder(Stream source, int length, PvrPixelFormat pixelFormat, PvrDataFormat dataFormat)
        {
            MemoryStream buffer = new MemoryStream();

            PTStream.CopyPartTo(source, buffer, length);
            Initalize(new Bitmap(buffer));

            if (m_decodedBitmap != null)
            {
                Initalize(pixelFormat, dataFormat);
            }
        }
Ejemplo n.º 7
0
        public MemoryStream EncodeTexture()
        {
            // Calculate what the length of the texture will be
            int textureLength = 16 + (TextureWidth * TextureHeight * DataCodec.Bpp / 8);

            if (HasGlobalIndex)
            {
                textureLength += 16;
            }
            if (DataCodec.PaletteEntries != 0 && !DataCodec.NeedsExternalPalette)
            {
                textureLength += (DataCodec.PaletteEntries * PixelCodec.Bpp / 8);
            }

            // Calculate the mipmap padding (if the texture contains mipmaps)
            int mipmapPadding = 0;

            if (DataCodec.HasMipmaps)
            {
                if (DataFormat == PvrDataFormat.SQUARE_TWIDDLED_MIPMAP)
                {
                    // A 1x1 mipmap takes up as much space as a 2x1 mipmap
                    // There are also 4 extra bytes at the end of the file
                    mipmapPadding  = (DataCodec.Bpp) >> 3;
                    textureLength += 4;
                }
                else if (DataFormat == PvrDataFormat.SQUARE_TWIDDLED_MIPMAP_ALT)
                {
                    // A 1x1 mipmap takes up as much space as a 2x2 mipmap
                    mipmapPadding = (3 * DataCodec.Bpp) >> 3;
                }

                textureLength += mipmapPadding;

                for (int size = 1; size < TextureWidth; size <<= 1)
                {
                    textureLength += Math.Max((size * size * DataCodec.Bpp) >> 3, 1);
                }
            }

            MemoryStream destination = new MemoryStream(textureLength);

            // Write out the GBIX header (if we are including one)
            if (HasGlobalIndex)
            {
                destination.WriteByte((byte)'G');
                destination.WriteByte((byte)'B');
                destination.WriteByte((byte)'I');
                destination.WriteByte((byte)'X');

                PTStream.WriteUInt32(destination, 8);
                PTStream.WriteUInt32(destination, GlobalIndex);
                PTStream.WriteUInt32(destination, 0);
            }

            // Write out the PVRT header
            destination.WriteByte((byte)'P');
            destination.WriteByte((byte)'V');
            destination.WriteByte((byte)'R');
            destination.WriteByte((byte)'T');

            if (HasGlobalIndex)
            {
                PTStream.WriteInt32(destination, textureLength - 24);
            }
            else
            {
                PTStream.WriteInt32(destination, textureLength - 8);
            }

            destination.WriteByte((byte)PixelFormat);
            destination.WriteByte((byte)DataFormat);
            PTStream.WriteUInt16(destination, 0);

            PTStream.WriteUInt16(destination, TextureWidth);
            PTStream.WriteUInt16(destination, TextureHeight);

            // If we have an internal palette, write it
            if (DataCodec.PaletteEntries != 0 && !DataCodec.NeedsExternalPalette)
            {
                byte[] palette = PixelCodec.EncodePalette(m_texturePalette, DataCodec.PaletteEntries);
                destination.Write(palette, 0, palette.Length);
            }

            // Write out any mipmaps
            if (DataCodec.HasMipmaps)
            {
                // Write out any padding bytes before the 1x1 mipmap
                for (int i = 0; i < mipmapPadding; i++)
                {
                    destination.WriteByte(0);
                }

                for (int size = 1; size < TextureWidth; size <<= 1)
                {
                    byte[] mipmapDecodedData = null;
                    if (DataCodec.NeedsExternalPalette)
                    {
                        if (DataCodec.VQ)
                        {
                            mipmapDecodedData = BitmapToRawVQResized(m_decodedBitmap, size, 1, m_codeBook);
                        }
                        else
                        {
                            mipmapDecodedData = BitmapToRawIndexedResized(m_decodedBitmap, size, 1, m_palette);
                        }
                    }
                    else
                    {
                        mipmapDecodedData = BitmapToRawResized(m_decodedBitmap, size, 1);
                    }

                    byte[] mipmapTextureData = DataCodec.Encode(mipmapDecodedData, 0, size, size);
                    destination.Write(mipmapTextureData, 0, mipmapTextureData.Length);
                }
            }

            // Write the texture data
            byte[] textureData = DataCodec.Encode(m_decodedData, TextureWidth, TextureHeight, null);
            destination.Write(textureData, 0, textureData.Length);

            // If the data format is square twiddled with mipmaps, write out the extra bytes.
            if (DataFormat == PvrDataFormat.SQUARE_TWIDDLED_MIPMAP)
            {
                destination.Write(new byte[] { 0, 0, 0, 0 }, 0, 4);
            }

            // Compress the texture
            if (CompressionFormat != PvrCompressionFormat.NONE)
            {
                CompressionCodec = PvrCompressionCodec.GetCompressionCodec(CompressionFormat);

                if (CompressionCodec != null)
                {
                    // Ok, we need to convert the current stream to an array, compress it, then write it back to a new stream
                    byte[] buffer = destination.ToArray();
                    buffer = CompressionCodec.Compress(buffer, (HasGlobalIndex ? 0x20 : 0x10), PixelCodec, DataCodec);

                    destination = new MemoryStream();
                    destination.Write(buffer, 0, buffer.Length);
                }
            }

            return(destination);
        }