Beispiel #1
0
        private bool Initalize(GvrPixelFormat pixelFormat, GvrDataFormat dataFormat)
        {
            // Set the default values
            dataFlags      = GvrDataFlags.None;
            hasGlobalIndex = true;
            gbixType       = GvrGbixType.Gbix;
            globalIndex    = 0;

            // Set the data format and pixel format and load the appropiate codecs
            this.dataFormat = dataFormat;
            dataCodec       = GvrDataCodec.GetDataCodec(dataFormat);

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

            // Only palettized formats require a pixel codec.
            if (dataCodec.PaletteEntries != 0)
            {
                this.pixelFormat = pixelFormat;
                pixelCodec       = GvrPixelCodec.GetPixelCodec(pixelFormat);

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

                dataCodec.PixelCodec = pixelCodec;

                dataFlags |= GvrDataFlags.InternalPalette;

                // Convert the bitmap to an array containing indicies.
                decodedData = BitmapToRawIndexed(decodedBitmap, dataCodec.PaletteEntries, out texturePalette);
            }
            else
            {
                this.pixelFormat = GvrPixelFormat.Unknown;
                pixelCodec       = null;

                // Convert the bitmap to an array
                decodedData = BitmapToRaw(decodedBitmap);
            }

            return(true);
        }
Beispiel #2
0
        protected override bool Initalize()
        {
            // Check to see if what we are dealing with is a GVP palette
            if (!Is(encodedData))
            {
                return(false);
            }

            // Get the pixel format and the codec and make sure we can decode using them
            pixelFormat = (GvrPixelFormat)encodedData[0x09];
            pixelCodec  = GvrPixelCodec.GetPixelCodec(pixelFormat);
            if (pixelCodec == null)
            {
                return(false);
            }

            // Get the number of colors contained in the palette
            paletteEntries = PTMethods.ToUInt16BE(encodedData, 0x0E);

            return(true);
        }
Beispiel #3
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;
        }