Esempio n. 1
0
        protected override void Initalize()
        {
            // Check to see if what we are dealing with is a GVR texture
            if (!Is(encodedData))
            {
                throw new NotAValidTextureException("This is not a valid GVR texture.");
            }

            // Determine the offsets of the GBIX/GCIX (if present) and GCIX header chunks.
            if (PTMethods.Contains(encodedData, 0, Encoding.UTF8.GetBytes("GBIX")) ||
                PTMethods.Contains(encodedData, 0, Encoding.UTF8.GetBytes("GCIX")))
            {
                gbixOffset = 0x00;
                pvrtOffset = 0x10;
            }
            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 = PTMethods.ToUInt32BE(encodedData, gbixOffset + 0x08);
            }
            else
            {
                globalIndex = 0;
            }

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

            pixelFormat = (GvrPixelFormat)(encodedData[pvrtOffset + 0x0A] >> 4); // Only the first 4 bits matter
            dataFlags   = (GvrDataFlags)(encodedData[pvrtOffset + 0x0A] & 0x0F); // Only the last 4 bits matter
            dataFormat  = (GvrDataFormat)encodedData[pvrtOffset + 0x0B];

            // Get the codecs and make sure we can decode using them
            dataCodec = GvrDataCodec.GetDataCodec(dataFormat);

            // We need a pixel codec if this is a palettized texture
            if (dataCodec != null && dataCodec.PaletteEntries != 0)
            {
                pixelCodec = GvrPixelCodec.GetPixelCodec(pixelFormat);

                if (pixelCodec != null)
                {
                    dataCodec.PixelCodec = pixelCodec;
                    canDecode            = true;
                }
            }
            else
            {
                pixelFormat = GvrPixelFormat.Unknown;

                if (dataCodec != null)
                {
                    canDecode = true;
                }
            }

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

            // If the texture contains mipmaps, gets the offsets of them
            if (canDecode && paletteEntries == 0 && (dataFlags & GvrDataFlags.Mipmaps) != 0)
            {
                mipmapOffsets = new int[(int)Math.Log(textureWidth, 2) + 1];

                int mipmapOffset = 0;
                for (int i = 0, size = textureWidth; i < mipmapOffsets.Length; i++, size >>= 1)
                {
                    mipmapOffsets[i] = mipmapOffset;
                    mipmapOffset    += Math.Max(size * size * (dataCodec.Bpp >> 3), 32);
                }
            }

            initalized = true;
        }