Example #1
0
        private static string CompressionFormatToString(PvrCompressionFormat format)
        {
            switch (format)
            {
            case PvrCompressionFormat.Rle: return("RLE");
            }

            return("None");
        }
        public static PvrCompressionCodec GetCompressionCodec(PvrCompressionFormat format)
        {
            switch (format)
            {
                case PvrCompressionFormat.Rle:
                    return new Rle();
            }

            return null;
        }
Example #3
0
        public static PvrCompressionCodec GetCompressionCodec(PvrCompressionFormat format)
        {
            switch (format)
            {
            case PvrCompressionFormat.Rle:
                return(new Rle());
            }

            return(null);
        }
Example #4
0
        public PvrTexture()
        {
            // Set default values
            HasGlobalIndex = true;
            GlobalIndex = 0;

            PixelFormat = PvrPixelFormat.Argb1555;
            DataFormat = PvrDataFormat.SquareTwiddled;

            compressionFormat = PvrCompressionFormat.None;
        }
        private bool Initalize(PvrPixelFormat pixelFormat, PvrDataFormat dataFormat)
        {
            // Set the default values
            hasGlobalIndex    = true;
            globalIndex       = 0;
            compressionFormat = PvrCompressionFormat.None;

            // Set the data format and pixel format and load the appropiate codecs
            this.pixelFormat = pixelFormat;
            pixelCodec       = PvrPixelCodec.GetPixelCodec(pixelFormat);

            this.dataFormat = dataFormat;
            dataCodec       = PvrDataCodec.GetDataCodec(dataFormat);

            // Make sure the pixel and data codecs exists and we can encode to it
            if (pixelCodec == null || !pixelCodec.CanEncode)
            {
                return(false);
            }
            if (dataCodec == null || !dataCodec.CanEncode)
            {
                return(false);
            }
            dataCodec.PixelCodec = pixelCodec;

            if (dataCodec.PaletteEntries != 0)
            {
                // Convert the bitmap to an array containing indicies.
                decodedData = BitmapToRawIndexed(decodedBitmap, dataCodec.PaletteEntries, out texturePalette);

                // If this texture has an external palette file, set up the palette encoder
                if (dataCodec.NeedsExternalPalette)
                {
                    paletteEncoder = new PvpPaletteEncoder(texturePalette, (ushort)dataCodec.PaletteEntries, pixelFormat, pixelCodec);
                }
            }
            else
            {
                // Convert the bitmap to an array
                decodedData = BitmapToRaw(decodedBitmap);
            }

            return(true);
        }
Example #6
0
        protected override void Initalize()
        {
            // Check to see if what we are dealing with is a PVR texture
            if (!Is(encodedData))
            {
                throw new NotAValidTextureException("This is not a valid PVR texture.");
            }

            // Determine the offsets of the GBIX (if present) and PVRT header chunks.
            if (PTMethods.Contains(encodedData, 0x00, gbixFourCC))
            {
                gbixOffset = 0x00;
                pvrtOffset = 0x08 + BitConverter.ToInt32(encodedData, gbixOffset + 4);
            }
            else if (PTMethods.Contains(encodedData, 0x04, gbixFourCC))
            {
                gbixOffset = 0x04;
                pvrtOffset = 0x0C + BitConverter.ToInt32(encodedData, gbixOffset + 4);
            }
            else if (PTMethods.Contains(encodedData, 0x04, pvrtFourCC))
            {
                gbixOffset = -1;
                pvrtOffset = 0x04;
            }
            else
            {
                gbixOffset = -1;
                pvrtOffset = 0x00;
            }

            // Read the global index (if it is present). If it is not present, just set it to 0.
            if (gbixOffset != -1)
            {
                globalIndex = BitConverter.ToUInt32(encodedData, gbixOffset + 0x08);
            }
            else
            {
                globalIndex = 0;
            }

            // Read information about the texture
            textureWidth  = BitConverter.ToUInt16(encodedData, pvrtOffset + 0x0C);
            textureHeight = BitConverter.ToUInt16(encodedData, pvrtOffset + 0x0E);

            pixelFormat = (PvrPixelFormat)encodedData[pvrtOffset + 0x08];
            dataFormat  = (PvrDataFormat)encodedData[pvrtOffset + 0x09];

            // Get the codecs and make sure we can decode using them
            pixelCodec = PvrPixelCodec.GetPixelCodec(pixelFormat);
            dataCodec = PvrDataCodec.GetDataCodec(dataFormat);

            if (dataCodec != null && pixelCodec != null)
            {
                dataCodec.PixelCodec = pixelCodec;
                canDecode = true;
            }

            // Set the number of palette entries
            // The number in a Small Vq encoded texture various based on its size
            paletteEntries = dataCodec.PaletteEntries;
            if (dataFormat == PvrDataFormat.SmallVq || dataFormat == PvrDataFormat.SmallVqMipmaps)
            {
                if (textureWidth <= 16)
                {
                    paletteEntries = 64; // Actually 16
                }
                else if (textureWidth <= 32)
                {
                    paletteEntries = 256; // Actually 64
                }
                else if (textureWidth <= 64)
                {
                    paletteEntries = 512; // Actually 128
                }
                else
                {
                    paletteEntries = 1024; // Actually 256
                }
            }

            // Set the palette and data offsets
            if (!canDecode || paletteEntries == 0 || dataCodec.NeedsExternalPalette)
            {
                paletteOffset = -1;
                dataOffset = pvrtOffset + 0x10;
            }
            else
            {
                paletteOffset = pvrtOffset + 0x10;
                dataOffset = paletteOffset + (paletteEntries * (pixelCodec.Bpp >> 3));
            }

            // Get the compression format and determine if we need to decompress this texture
            compressionFormat = GetCompressionFormat(encodedData, pvrtOffset, dataOffset);
            compressionCodec = PvrCompressionCodec.GetCompressionCodec(compressionFormat);

            if (compressionFormat != PvrCompressionFormat.None && compressionCodec != null)
            {
                encodedData = compressionCodec.Decompress(encodedData, dataOffset, pixelCodec, dataCodec);

                // Now place the offsets in the appropiate area
                if (compressionFormat == PvrCompressionFormat.Rle)
                {
                    if (gbixOffset != -1) gbixOffset -= 4;
                    pvrtOffset -= 4;
                    if (paletteOffset != -1) paletteOffset -= 4;
                    dataOffset -= 4;
                }
            }

            // If the texture contains mipmaps, gets the offsets of them
            if (canDecode && dataCodec.HasMipmaps)
            {
                mipmapOffsets = new int[(int)Math.Log(textureWidth, 2) + 1];

                int mipmapOffset = 0;

                // Calculate the padding for the first mipmap offset
                if (dataFormat == PvrDataFormat.SquareTwiddledMipmaps)
                {
                    // A 1x1 mipmap takes up as much space as a 2x1 mipmap
                    mipmapOffset = (dataCodec.Bpp) >> 3;
                }
                else if (dataFormat == PvrDataFormat.SquareTwiddledMipmapsAlt)
                {
                    // A 1x1 mipmap takes up as much space as a 2x2 mipmap
                    mipmapOffset = (3 * dataCodec.Bpp) >> 3;
                }

                for (int i = mipmapOffsets.Length - 1, size = 1; i >= 0; i--, size <<= 1)
                {
                    mipmapOffsets[i] = mipmapOffset;

                    mipmapOffset += Math.Max((size * size * dataCodec.Bpp) >> 3, 1);
                }
            }

            initalized = true;
        }
Example #7
0
        protected override void Initalize()
        {
            // Check to see if what we are dealing with is a PVR texture
            if (!Is(encodedData))
            {
                throw new NotAValidTextureException("This is not a valid PVR texture.");
            }

            // Determine the offsets of the GBIX (if present) and PVRT header chunks.
            if (PTMethods.Contains(encodedData, 0x00, Encoding.UTF8.GetBytes("GBIX")))
            {
                gbixOffset = 0x00;
                pvrtOffset = 0x10;
            }
            else if (PTMethods.Contains(encodedData, 0x04, Encoding.UTF8.GetBytes("GBIX")))
            {
                gbixOffset = 0x04;
                pvrtOffset = 0x14;
            }
            else if (PTMethods.Contains(encodedData, 0x04, Encoding.UTF8.GetBytes("PVRT")))
            {
                gbixOffset = -1;
                pvrtOffset = 0x04;
            }
            else
            {
                gbixOffset = -1;
                pvrtOffset = 0x00;
            }

            // Read the global index (if it is present). If it is not present, just set it to 0.
            if (gbixOffset != -1)
            {
                globalIndex = BitConverter.ToUInt32(encodedData, gbixOffset + 0x08);
            }
            else
            {
                globalIndex = 0;
            }

            // Read information about the texture
            textureWidth  = BitConverter.ToUInt16(encodedData, pvrtOffset + 0x0C);
            textureHeight = BitConverter.ToUInt16(encodedData, pvrtOffset + 0x0E);

            pixelFormat = (PvrPixelFormat)encodedData[pvrtOffset + 0x08];
            dataFormat  = (PvrDataFormat)encodedData[pvrtOffset + 0x09];

            // Get the codecs and make sure we can decode using them
            pixelCodec = PvrPixelCodec.GetPixelCodec(pixelFormat);
            dataCodec  = PvrDataCodec.GetDataCodec(dataFormat);

            if (dataCodec != null && pixelCodec != null)
            {
                dataCodec.PixelCodec = pixelCodec;
                canDecode            = true;
            }

            // Set the palette and data offsets
            if (!canDecode || dataCodec.PaletteEntries == 0 || dataCodec.NeedsExternalPalette)
            {
                paletteOffset = -1;
                dataOffset    = pvrtOffset + 0x10;
            }
            else
            {
                paletteOffset = pvrtOffset + 0x10;
                dataOffset    = paletteOffset + (dataCodec.PaletteEntries * (pixelCodec.Bpp >> 3));
            }

            // Get the compression format and determine if we need to decompress this texture
            compressionFormat = GetCompressionFormat(encodedData, pvrtOffset, dataOffset);
            compressionCodec  = PvrCompressionCodec.GetCompressionCodec(compressionFormat);

            if (compressionFormat != PvrCompressionFormat.None && compressionCodec != null)
            {
                encodedData = compressionCodec.Decompress(encodedData, dataOffset, pixelCodec, dataCodec);

                // Now place the offsets in the appropiate area
                if (compressionFormat == PvrCompressionFormat.Rle)
                {
                    if (gbixOffset != -1)
                    {
                        gbixOffset -= 4;
                    }
                    pvrtOffset -= 4;
                    if (paletteOffset != -1)
                    {
                        paletteOffset -= 4;
                    }
                    dataOffset -= 4;
                }
            }

            // If the texture contains mipmaps, gets the offsets of them
            if (canDecode && dataCodec.HasMipmaps)
            {
                mipmapOffsets = new int[(int)Math.Log(textureWidth, 2) + 1];

                int mipmapOffset = 0;

                // Calculate the padding for the first mipmap offset
                if (dataFormat == PvrDataFormat.SquareTwiddledMipmaps)
                {
                    // A 1x1 mipmap takes up as much space as a 2x1 mipmap
                    mipmapOffset = (dataCodec.Bpp) >> 3;
                }
                if (dataFormat == PvrDataFormat.SquareTwiddledMipmapsAlt)
                {
                    // A 1x1 mipmap takes up as much space as a 2x2 mipmap
                    mipmapOffset = (3 * dataCodec.Bpp) >> 3;
                }

                for (int i = mipmapOffsets.Length - 1, size = 1; i >= 0; i--, size <<= 1)
                {
                    mipmapOffsets[i] = mipmapOffset;

                    mipmapOffset += Math.Max((size * size * dataCodec.Bpp) >> 3, 1);
                }
            }

            initalized = true;
        }
Example #8
0
        private static string CompressionFormatToString(PvrCompressionFormat format)
        {
            switch (format)
            {
                case PvrCompressionFormat.Rle: return "RLE";
            }

            return "None";
        }
Example #9
0
        public void Initalize()
        {
            // Determine the offsets of the GBIX (if present) and PVRT header chunks.
            if (PTMethods.Contains(m_encodedData, 0x00, gbixFourCC))
            {
                GbixOffset = 0x00;
                PvrtOffset = 0x08 + BitConverter.ToInt32(m_encodedData, GbixOffset + 4);
            }
            else if (PTMethods.Contains(m_encodedData, 0x04, gbixFourCC))
            {
                GbixOffset = 0x04;
                PvrtOffset = 0x0C + BitConverter.ToInt32(m_encodedData, GbixOffset + 4);
            }
            else if (PTMethods.Contains(m_encodedData, 0x04, pvrtFourCC))
            {
                GbixOffset = -1;
                PvrtOffset = 0x04;
            }
            else
            {
                GbixOffset = -1;
                PvrtOffset = 0x00;
            }

            // Read the global index (if it is present). If it is not present, just set it to 0.
            if (GbixOffset != -1)
            {
                GlobalIndex = BitConverter.ToUInt32(m_encodedData, GbixOffset + 0x08);
            }
            else
            {
                GlobalIndex = 0;
            }

            // Read information about the texture
            TextureWidth  = BitConverter.ToUInt16(m_encodedData, PvrtOffset + 0x0C);
            TextureHeight = BitConverter.ToUInt16(m_encodedData, PvrtOffset + 0x0E);

            PixelFormat = (PvrPixelFormat)m_encodedData[PvrtOffset + 0x08];
            DataFormat  = (PvrDataFormat)m_encodedData[PvrtOffset + 0x09];

            // Get the codecs and make sure we can decode using them
            PixelCodec = PvrPixelCodec.GetPixelCodec(PixelFormat);
            DataCodec  = PvrDataCodec.GetDataCodec(DataFormat);

            if (DataCodec != null && PixelCodec != null)
            {
                DataCodec.PixelCodec = PixelCodec;
            }

            // Set the number of palette entries
            // The number in a Small Vq encoded texture various based on its size
            m_paletteEntries = DataCodec.PaletteEntries;
            if (DataFormat == PvrDataFormat.VECTOR_QUANTIZATION_SMALL || DataFormat == PvrDataFormat.VECTOR_QUANTIZATION_SMALL_MIPMAP)
            {
                if (TextureWidth <= 16)
                {
                    m_paletteEntries = 64; // Actually 16
                }
                else if (TextureWidth <= 32)
                {
                    m_paletteEntries = 256; // Actually 64
                }
                else if (TextureWidth <= 64)
                {
                    m_paletteEntries = 512; // Actually 128
                }
                else
                {
                    m_paletteEntries = 1024; // Actually 256
                }
            }

            // Set the palette and data offsets
            if (m_paletteEntries == 0 || DataCodec.NeedsExternalPalette)
            {
                m_paletteOffset = -1;
                m_dataOffset    = PvrtOffset + 0x10;
            }
            else
            {
                m_paletteOffset = PvrtOffset + 0x10;
                m_dataOffset    = m_paletteOffset + (m_paletteEntries * (PixelCodec.Bpp >> 3));
            }

            // Get the compression format and determine if we need to decompress this texture
            CompressionFormat = GetCompressionFormat(m_encodedData, PvrtOffset, m_dataOffset);
            CompressionCodec  = PvrCompressionCodec.GetCompressionCodec(CompressionFormat);

            if (CompressionFormat != PvrCompressionFormat.NONE && CompressionCodec != null)
            {
                m_encodedData = CompressionCodec.Decompress(m_encodedData, m_dataOffset, PixelCodec, DataCodec);

                // Now place the offsets in the appropiate area
                if (CompressionFormat == PvrCompressionFormat.RLE)
                {
                    if (GbixOffset != -1)
                    {
                        GbixOffset -= 4;
                    }
                    PvrtOffset -= 4;
                    if (m_paletteOffset != -1)
                    {
                        m_paletteOffset -= 4;
                    }
                    m_dataOffset -= 4;
                }
            }

            // If the texture contains mipmaps, gets the offsets of them
            if (DataCodec.HasMipmaps)
            {
                m_mipmapOffsets = new int[(int)Math.Log(TextureWidth, 2) + 1];

                int mipmapOffset = 0;

                // Calculate the padding for the first mipmap offset
                if (DataFormat == PvrDataFormat.SQUARE_TWIDDLED_MIPMAP)
                {
                    // A 1x1 mipmap takes up as much space as a 2x1 mipmap
                    mipmapOffset = (DataCodec.Bpp) >> 3;
                }
                else if (DataFormat == PvrDataFormat.SQUARE_TWIDDLED_MIPMAP_ALT)
                {
                    // A 1x1 mipmap takes up as much space as a 2x2 mipmap
                    mipmapOffset = (3 * DataCodec.Bpp) >> 3;
                }

                for (int i = m_mipmapOffsets.Length - 1, size = 1; i >= 0; i--, size <<= 1)
                {
                    m_mipmapOffsets[i] = mipmapOffset;

                    mipmapOffset += Math.Max((size * size * DataCodec.Bpp) >> 3, 1);
                }
            }
        }
Example #10
0
        private bool Initalize(PvrPixelFormat pixelFormat, PvrDataFormat dataFormat)
        {
            // Set the default values
            hasGlobalIndex = true;
            globalIndex = 0;
            compressionFormat = PvrCompressionFormat.None;

            // Set the data format and pixel format and load the appropiate codecs
            this.pixelFormat = pixelFormat;
            pixelCodec = PvrPixelCodec.GetPixelCodec(pixelFormat);

            this.dataFormat = dataFormat;
            dataCodec = PvrDataCodec.GetDataCodec(dataFormat);

            // Make sure the pixel and data codecs exists and we can encode to it
            if (pixelCodec == null || !pixelCodec.CanEncode) return false;
            if (dataCodec == null || !dataCodec.CanEncode) return false;
            dataCodec.PixelCodec = pixelCodec;

            if (dataCodec.PaletteEntries != 0)
            {
                // Convert the bitmap to an array containing indicies.
                decodedData = BitmapToRawIndexed(decodedBitmap, dataCodec.PaletteEntries, out texturePalette);

                // If this texture has an external palette file, set up the palette encoder
                if (dataCodec.NeedsExternalPalette)
                {
                    paletteEncoder = new PvpPaletteEncoder(texturePalette, (ushort)dataCodec.PaletteEntries, pixelFormat, pixelCodec);
                }
            }
            else
            {
                // Convert the bitmap to an array
                decodedData = BitmapToRaw(decodedBitmap);
            }

            return true;
        }
Example #11
0
        // Initalize the bitmap
        private bool Initalize()
        {
            // Make sure the width and height are correct
            if (TextureWidth < 8 || TextureHeight < 8) return false;
            if ((TextureWidth & (TextureWidth - 1)) != 0 || (TextureHeight & (TextureHeight - 1)) != 0)
                return false;

            PixelCodec = PvrCodecList.GetPixelCodec((PvrPixelFormat)PixelFormat);
            DataCodec  = PvrCodecList.GetDataCodec((PvrDataFormat)DataFormat);

            CompressionFormat = PvrCompressionFormat.None;
            CompressionCodec  = null;

            if (PixelCodec == null || DataCodec == null)           return false;
            if (!PixelCodec.CanEncode() || !DataCodec.CanEncode()) return false;
            if (!CanEncode((PvrPixelFormat)PixelFormat, (PvrDataFormat)DataFormat, TextureWidth, TextureHeight)) return false;

            GbixOffset = 0x00;
            PvrtOffset = 0x10;

            // See if we need to palettize the bitmap and raw image data
            if (DataCodec.GetNumClutEntries() != 0)
                PalettizeBitmap();

            return true;
        }
Example #12
0
        /// <summary>
        /// Set the compression format.
        /// </summary>
        /// <param name="CompressionFormat">Compression Format</param>
        public void SetCompressionFormat(PvrCompressionFormat CompressionFormat)
        {
            if (!InitSuccess) return;

            if (CompressionFormat == PvrCompressionFormat.Rle && DataCodec.GetBpp(PixelCodec) >= 8)
            {
                // We want to use Rle compression and our texture has a bpp of at least 8.
                CompressionCodec = PvrCodecList.GetCompressionCodec(CompressionFormat);
                if (CompressionCodec != null)
                    this.CompressionFormat = PvrCompressionFormat.Rle;
                else
                    return; // Can't compress!
            }
        }
Example #13
0
        // Read the header and sets up the appropiate values.
        // Returns true if successful, otherwise false
        private bool ReadHeader()
        {
            // Make sure this is a Pvr Texture
            if (!IsPvrTexture(TextureData))
                return false;

            // Get the header offsets
            if (Compare(TextureData, "GBIX", 0x00))
            {
                GbixOffset = 0x00;
                PvrtOffset = 0x10;
            }
            else if (Compare(TextureData, "GBIX", 0x04))
            {
                GbixOffset = 0x04;
                PvrtOffset = 0x14;
            }
            else if (Compare(TextureData, "PVRT", 0x04))
            {
                GbixOffset = -1;
                PvrtOffset = 0x04;
            }
            else
            {
                GbixOffset = -1;
                PvrtOffset = 0x00;
            }

            // Read the file information
            TextureWidth  = BitConverter.ToUInt16(TextureData, PvrtOffset + 0x0C);
            TextureHeight = BitConverter.ToUInt16(TextureData, PvrtOffset + 0x0E);

            PixelFormat = TextureData[PvrtOffset + 0x08];
            DataFormat  = TextureData[PvrtOffset + 0x09];

            // Get the codecs and make sure we can decode using them
            PixelCodec = PvrCodecList.GetPixelCodec((PvrPixelFormat)PixelFormat);
            DataCodec  = PvrCodecList.GetDataCodec((PvrDataFormat)DataFormat);
            if (PixelCodec == null || !PixelCodec.CanDecode()) return false;
            if (DataCodec == null  || !DataCodec.CanDecode())  return false;

            // Set the clut and data offsets
            if (DataCodec.GetNumClutEntries() == 0 || DataCodec.NeedsExternalClut())
            {
                ClutOffset = -1;
                DataOffset = PvrtOffset + 0x10;
            }
            else
            {
                ClutOffset = PvrtOffset + 0x10;
                DataOffset = ClutOffset + (DataCodec.GetNumClutEntries() * (PixelCodec.GetBpp() / 8));
            }

            // Get the compression format & decompress the pvr
            CompressionFormat = GetCompressionFormat(TextureData, PvrtOffset, DataOffset);
            CompressionCodec  = PvrCodecList.GetCompressionCodec(CompressionFormat);

            if (CompressionFormat != PvrCompressionFormat.None && CompressionCodec != null)
            {
                TextureData = CompressionCodec.Decompress(TextureData, DataOffset, PixelCodec, DataCodec);

                // Now place the offsets in the appropiate area
                if (CompressionFormat == PvrCompressionFormat.Rle)
                {
                    if (GbixOffset != -1) GbixOffset -= 4;
                    PvrtOffset -= 4;
                    if (ClutOffset != -1) ClutOffset -= 4;
                    DataOffset -= 4;
                }
            }

            RawImageData = new byte[TextureWidth * TextureHeight * 4];
            return true;
        }
 /// <summary>
 /// Opens a texture to encode from a bitmap.
 /// </summary>
 /// <param name="source">Bitmap to encode.</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(Bitmap source, PvrPixelFormat pixelFormat, PvrDataFormat dataFormat, uint globalIndex, PvrCompressionFormat compression)
     : base(source)
 {
     if (decodedBitmap != null)
     {
         initalized = Initalize(pixelFormat, dataFormat, globalIndex, compressionFormat);
     }
 }