/// <summary>
        /// Opens a texture to encode from a byte array.
        /// </summary>
        /// <param name="source">Byte array that contains the texture data.</param>
        /// <param name="offset">Offset of the texture in the array.</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 GimTextureEncoder(byte[] source, int offset, int length, GimPaletteFormat paletteFormat, GimDataFormat dataFormat)
        {
            MemoryStream buffer = new MemoryStream();
            buffer.Write(source, offset, length);

            initalized = Initalize(new Bitmap(buffer), paletteFormat, dataFormat);
        }
示例#2
0
        public static GimDataCodec GetDataCodec(GimDataFormat format)
        {
            switch (format)
            {
            case GimDataFormat.Rgb565:
                return(new Rgb565());

            case GimDataFormat.Argb1555:
                return(new Argb1555());

            case GimDataFormat.Argb4444:
                return(new Argb8888());

            case GimDataFormat.Argb8888:
                return(new Argb8888());

            case GimDataFormat.Index4:
                return(new Index4());

            case GimDataFormat.Index8:
                return(new Index8());
            }

            return(null);
        }
示例#3
0
        private bool Initalize(Bitmap source, GimPaletteFormat paletteFormat, GimDataFormat dataFormat)
        {
            // Make sure this bitmap's dimensions are valid
            if (!HasValidDimensions(source.Width, source.Height))
            {
                return(false);
            }

            textureWidth  = (ushort)source.Width;
            textureHeight = (ushort)source.Height;

            // Set default values
            hasMetadata = true;
            metadata    = new GimMetadata();

            // Set the data format and palette format and load the appropiate codecs
            this.dataFormat = dataFormat;
            dataCodec       = GimDataCodec.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.paletteFormat = paletteFormat;
                pixelCodec         = GimPixelCodec.GetPixelCodec(paletteFormat);

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

                dataCodec.PixelCodec = pixelCodec;

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

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

            return(true);
        }
示例#4
0
        private static string DataFormatToString(GimDataFormat format)
        {
            switch (format)
            {
            case GimDataFormat.Rgb565:   return("RGB565");

            case GimDataFormat.Argb1555: return("ARGB1555");

            case GimDataFormat.Argb4444: return("ARGB4444");

            case GimDataFormat.Argb8888: return("ARGB8888");

            case GimDataFormat.Index4:   return("4-bit Indexed");

            case GimDataFormat.Index8:   return("8-bit Indexed");
            }

            return("Unknown");
        }
示例#5
0
        public static GimDataCodec GetDataCodec(GimDataFormat format)
        {
            switch (format)
            {
                case GimDataFormat.Rgb565:
                    return new Rgb565();
                case GimDataFormat.Argb1555:
                    return new Argb1555();
                case GimDataFormat.Argb4444:
                    return new Argb8888();
                case GimDataFormat.Argb8888:
                    return new Argb8888();
                case GimDataFormat.Index4:
                    return new Index4();
                case GimDataFormat.Index8:
                    return new Index8();
            }

            return null;
        }
 /// <summary>
 /// Opens a texture to encode from a stream.
 /// </summary>
 /// <param name="source">Stream that contains the texture data.</param>
 /// <param name="pixelFormat">Pixel format to encode the texture to.</param>
 /// <param name="dataFormat">Data format to encode the texture to.</param>
 public GimTextureEncoder(Stream source, GimPaletteFormat paletteFormat, GimDataFormat dataFormat)
     : this(source, (int)(source.Length - source.Position), paletteFormat, dataFormat)
 {
 }
        private bool Initalize(Bitmap source, GimPaletteFormat paletteFormat, GimDataFormat dataFormat)
        {
            // Make sure this bitmap's dimensions are valid
            if (!HasValidDimensions(source.Width, source.Height))
                return false;

            textureWidth = (ushort)source.Width;
            textureHeight = (ushort)source.Height;

            // Set default values
            hasMetadata = true;
            metadata = new GimMetadata();

            // Set the data format and palette format and load the appropiate codecs
            this.dataFormat = dataFormat;
            dataCodec = GimDataCodec.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.paletteFormat = paletteFormat;
                pixelCodec = GimPixelCodec.GetPixelCodec(paletteFormat);

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

                dataCodec.PixelCodec = pixelCodec;

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

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

            return true;
        }
 /// <summary>
 /// Opens a texture to encode from a byte array.
 /// </summary>
 /// <param name="source">Byte array that contains the texture data.</param>
 /// <param name="pixelFormat">Pixel format to encode the texture to.</param>
 /// <param name="dataFormat">Data format to encode the texture to.</param>
 public GimTextureEncoder(byte[] source, GimPaletteFormat paletteFormat, GimDataFormat dataFormat)
     : this(source, 0, source.Length, paletteFormat, dataFormat)
 {
 }
 /// <summary>
 /// Opens a texture to encode from a file.
 /// </summary>
 /// <param name="file">Filename of the file that contains the texture data.</param>
 /// <param name="pixelFormat">Pixel format to encode the texture to.</param>
 /// <param name="dataFormat">Data format to encode the texture to.</param>
 public GimTextureEncoder(string file, GimPaletteFormat paletteFormat, GimDataFormat dataFormat)
 {
     initalized = Initalize(new Bitmap(file), paletteFormat, dataFormat);
 }
 /// <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 GimTextureEncoder(Bitmap source, GimPaletteFormat paletteFormat, GimDataFormat dataFormat)
 {
     initalized = Initalize(source, paletteFormat, dataFormat);
 }
示例#11
0
        private void Initalize()
        {
            // Check to see if what we are dealing with is a GIM texture
            if (!Is(encodedData))
            {
                throw new NotAValidTextureException("This is not a valid GIM texture.");
            }

            // Initalize some things
            paletteFormat = GimPaletteFormat.Unknown;
            dataFormat    = GimDataFormat.Unknown;

            paletteEntries = 0;
            paletteOffset  = -1;
            dataOffset     = -1;

            hasMetadata = false;

            // A GIM is constructed of different chunks. They do not necessarily have to be in order.
            int eofOffset = -1;

            int offset         = 0x10;
            int previousOffset = offset;

            while (offset < encodedData.Length)
            {
                // EOF offset chunk
                if (encodedData[offset] == 0x02)
                {
                    eofOffset = BitConverter.ToInt32(encodedData, offset + 0x04) + 16;

                    // Go to the next chunk
                    offset += BitConverter.ToInt32(encodedData, offset + 0x08);
                }

                // Metadata offset chunk
                else if (encodedData[offset] == 0x03)
                {
                    // Skip this chunk. It's not necessary for decoding this texture.
                    offset += BitConverter.ToInt32(encodedData, offset + 0x08);
                }

                // Texture data
                else if (encodedData[offset] == 0x04)
                {
                    // Get the data format
                    dataFormat = (GimDataFormat)encodedData[offset + 0x14];

                    // Get the data codec and make sure we can decode using it
                    dataCodec = GimDataCodec.GetDataCodec(dataFormat);

                    // Check to see if the texture data is swizzled
                    swizzled = (BitConverter.ToUInt16(encodedData, offset + 0x16) == 1);

                    // Get the texture's width and height
                    textureWidth  = BitConverter.ToUInt16(encodedData, offset + 0x18);
                    textureHeight = BitConverter.ToUInt16(encodedData, offset + 0x1A);

                    // Some textures do not have a width that is a multiple or 16 or a height that is a multiple of 8.
                    // We'll just do it the lazy way and set their width/height to a multiple of 16/8.
                    if (textureWidth % 16 != 0)
                    {
                        textureWidth = (ushort)PTMethods.RoundUp(textureWidth, 16);
                    }
                    if (textureHeight % 8 != 0)
                    {
                        textureHeight = (ushort)PTMethods.RoundUp(textureHeight, 8);
                    }

                    // Set the offset of the texture data
                    dataOffset = offset + 0x50;

                    // Go to the next chunk
                    offset += BitConverter.ToInt32(encodedData, offset + 0x08);
                }

                // Palette data
                else if (encodedData[offset] == 0x05)
                {
                    // Get the palette format
                    paletteFormat = (GimPaletteFormat)encodedData[offset + 0x14];

                    // Get the pixel codec and make sure we can decode using it
                    pixelCodec = GimPixelCodec.GetPixelCodec(paletteFormat);

                    // Get the number of entries in the palette
                    paletteEntries = BitConverter.ToUInt16(encodedData, offset + 0x18);

                    // Set the offset of the palette data
                    paletteOffset = offset + 0x50;

                    // Go to the next chunk
                    offset += BitConverter.ToInt32(encodedData, offset + 0x08);
                }

                // Metadata chunk
                else if (encodedData[offset] == 0xFF)
                {
                    // Read in some metadata
                    hasMetadata = true;
                    metadata    = new GimMetadata();

                    int metadataOffset = 0x10;

                    metadata.OriginalFilename = PTMethods.StringFromBytes(encodedData, offset + metadataOffset);
                    metadataOffset           += metadata.OriginalFilename.Length + 1;

                    metadata.User   = PTMethods.StringFromBytes(encodedData, offset + metadataOffset);
                    metadataOffset += metadata.User.Length + 1;

                    metadata.Timestamp = PTMethods.StringFromBytes(encodedData, offset + metadataOffset);
                    metadataOffset    += metadata.Timestamp.Length + 1;

                    metadata.Program = PTMethods.StringFromBytes(encodedData, offset + metadataOffset);
                    metadataOffset  += metadata.Program.Length + 1;

                    // Go to the next chunk
                    offset += BitConverter.ToInt32(encodedData, offset + 0x08);
                }

                // Invalid chunk
                else
                {
                    return;
                }

                // Make sure we are actually advancing in the file, so we don't end up reaching a negative offset
                // or ending up in an infinite loop
                if (offset <= previousOffset)
                {
                    return;
                }
                previousOffset = offset;
            }

            // If all went well, offset should be equal to eofOffset
            if (offset != eofOffset)
            {
                return;
            }

            // If this is a non-palettized format, make sure we have a data codec
            if (dataCodec != null && dataCodec.PaletteEntries == 0)
            {
                canDecode = true;
            }

            // If this is a palettized format, make sure we have a palette codec, a data codec,
            // and that the number of palette entires is less than or equal to what this data format supports
            if (dataCodec != null && dataCodec.PaletteEntries != 0 && pixelCodec != null && paletteEntries <= dataCodec.PaletteEntries)
            {
                // Set the data format's pixel codec
                dataCodec.PixelCodec = pixelCodec;

                canDecode = true;
            }

            initalized = true;
        }
示例#12
0
 /// <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 GimTextureEncoder(Bitmap source, GimPaletteFormat paletteFormat, GimDataFormat dataFormat)
 {
     initalized = Initalize(source, paletteFormat, dataFormat);
 }
        /// <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 GimTextureEncoder(Stream source, int length, GimPaletteFormat paletteFormat, GimDataFormat dataFormat)
        {
            MemoryStream buffer = new MemoryStream();
            PTStream.CopyPartTo(source, buffer, length);

            initalized = Initalize(new Bitmap(buffer), paletteFormat, dataFormat);
        }
示例#14
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 GimTextureEncoder(Stream source, int length, GimPaletteFormat paletteFormat, GimDataFormat dataFormat)
        {
            MemoryStream buffer = new MemoryStream();

            PTStream.CopyPartTo(source, buffer, length);

            initalized = Initalize(new Bitmap(buffer), paletteFormat, dataFormat);
        }
示例#15
0
 /// <summary>
 /// Opens a texture to encode from a stream.
 /// </summary>
 /// <param name="source">Stream that contains the texture data.</param>
 /// <param name="pixelFormat">Pixel format to encode the texture to.</param>
 /// <param name="dataFormat">Data format to encode the texture to.</param>
 public GimTextureEncoder(Stream source, GimPaletteFormat paletteFormat, GimDataFormat dataFormat) : this(source, (int)(source.Length - source.Position), paletteFormat, dataFormat)
 {
 }
示例#16
0
        /// <summary>
        /// Opens a texture to encode from a byte array.
        /// </summary>
        /// <param name="source">Byte array that contains the texture data.</param>
        /// <param name="offset">Offset of the texture in the array.</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 GimTextureEncoder(byte[] source, int offset, int length, GimPaletteFormat paletteFormat, GimDataFormat dataFormat)
        {
            MemoryStream buffer = new MemoryStream();

            buffer.Write(source, offset, length);

            initalized = Initalize(new Bitmap(buffer), paletteFormat, dataFormat);
        }
示例#17
0
 /// <summary>
 /// Opens a texture to encode from a byte array.
 /// </summary>
 /// <param name="source">Byte array that contains the texture data.</param>
 /// <param name="pixelFormat">Pixel format to encode the texture to.</param>
 /// <param name="dataFormat">Data format to encode the texture to.</param>
 public GimTextureEncoder(byte[] source, GimPaletteFormat paletteFormat, GimDataFormat dataFormat) : this(source, 0, source.Length, paletteFormat, dataFormat)
 {
 }
示例#18
0
 /// <summary>
 /// Opens a texture to encode from a file.
 /// </summary>
 /// <param name="file">Filename of the file that contains the texture data.</param>
 /// <param name="pixelFormat">Pixel format to encode the texture to.</param>
 /// <param name="dataFormat">Data format to encode the texture to.</param>
 public GimTextureEncoder(string file, GimPaletteFormat paletteFormat, GimDataFormat dataFormat)
 {
     initalized = Initalize(new Bitmap(file), paletteFormat, dataFormat);
 }
示例#19
0
        private void Initalize()
        {
            // Check to see if what we are dealing with is a GIM texture
            if (!Is(encodedData))
            {
                throw new NotAValidTextureException("This is not a valid GIM texture.");
            }

            // Initalize some things
            paletteFormat = GimPaletteFormat.Unknown;
            dataFormat = GimDataFormat.Unknown;

            paletteEntries = 0;
            paletteOffset = -1;
            dataOffset = -1;

            hasMetadata = false;

            // A GIM is constructed of different chunks. They do not necessarily have to be in order.
            int eofOffset = -1;

            int offset = 0x10;
            int previousOffset = offset;
            while (offset < encodedData.Length)
            {
                // EOF offset chunk
                if (encodedData[offset] == 0x02)
                {
                    eofOffset = BitConverter.ToInt32(encodedData, offset + 0x04) + 16;

                    // Go to the next chunk
                    offset += BitConverter.ToInt32(encodedData, offset + 0x08);
                }

                // Metadata offset chunk
                else if (encodedData[offset] == 0x03)
                {
                    // Skip this chunk. It's not necessary for decoding this texture.
                    offset += BitConverter.ToInt32(encodedData, offset + 0x08);
                }

                // Texture data
                else if (encodedData[offset] == 0x04)
                {
                    // Get the data format
                    dataFormat = (GimDataFormat)encodedData[offset + 0x14];

                    // Get the data codec and make sure we can decode using it
                    dataCodec = GimDataCodec.GetDataCodec(dataFormat);

                    // Check to see if the texture data is swizzled
                    swizzled = (BitConverter.ToUInt16(encodedData, offset + 0x16) == 1);

                    // Get the texture's width and height
                    textureWidth  = BitConverter.ToUInt16(encodedData, offset + 0x18);
                    textureHeight = BitConverter.ToUInt16(encodedData, offset + 0x1A);

                    // Some textures do not have a width that is a multiple or 16 or a height that is a multiple of 8.
                    // We'll just do it the lazy way and set their width/height to a multiple of 16/8.
                    if (textureWidth % 16 != 0)
                    {
                        textureWidth = (ushort)PTMethods.RoundUp(textureWidth, 16);
                    }
                    if (textureHeight % 8 != 0)
                    {
                        textureHeight = (ushort)PTMethods.RoundUp(textureHeight, 8);
                    }

                    // Set the offset of the texture data
                    dataOffset = offset + 0x50;

                    // Go to the next chunk
                    offset += BitConverter.ToInt32(encodedData, offset + 0x08);
                }

                // Palette data
                else if (encodedData[offset] == 0x05)
                {
                    // Get the palette format
                    paletteFormat = (GimPaletteFormat)encodedData[offset + 0x14];

                    // Get the pixel codec and make sure we can decode using it
                    pixelCodec = GimPixelCodec.GetPixelCodec(paletteFormat);

                    // Get the number of entries in the palette
                    paletteEntries = BitConverter.ToUInt16(encodedData, offset + 0x18);

                    // Set the offset of the palette data
                    paletteOffset = offset + 0x50;

                    // Go to the next chunk
                    offset += BitConverter.ToInt32(encodedData, offset + 0x08);
                }

                // Metadata chunk
                else if (encodedData[offset] == 0xFF)
                {
                    // Read in some metadata
                    hasMetadata = true;
                    metadata = new GimMetadata();

                    int metadataOffset = 0x10;

                    metadata.OriginalFilename = PTMethods.StringFromBytes(encodedData, offset + metadataOffset);
                    metadataOffset += metadata.OriginalFilename.Length + 1;

                    metadata.User = PTMethods.StringFromBytes(encodedData, offset + metadataOffset);
                    metadataOffset += metadata.User.Length + 1;

                    metadata.Timestamp = PTMethods.StringFromBytes(encodedData, offset + metadataOffset);
                    metadataOffset += metadata.Timestamp.Length + 1;

                    metadata.Program = PTMethods.StringFromBytes(encodedData, offset + metadataOffset);
                    metadataOffset += metadata.Program.Length + 1;

                    // Go to the next chunk
                    offset += BitConverter.ToInt32(encodedData, offset + 0x08);
                }

                // Invalid chunk
                else
                {
                    return;
                }

                // Make sure we are actually advancing in the file, so we don't end up reaching a negative offset
                // or ending up in an infinite loop
                if (offset <= previousOffset)
                {
                    return;
                }
                previousOffset = offset;
            }

            // If all went well, offset should be equal to eofOffset
            if (offset != eofOffset)
            {
                return;
            }

            // If this is a non-palettized format, make sure we have a data codec
            if (dataCodec != null && dataCodec.PaletteEntries == 0)
            {
                canDecode = true;
            }

            // If this is a palettized format, make sure we have a palette codec, a data codec,
            // and that the number of palette entires is less than or equal to what this data format supports
            if (dataCodec != null && dataCodec.PaletteEntries != 0 && pixelCodec != null && paletteEntries <= dataCodec.PaletteEntries)
            {
                // Set the data format's pixel codec
                dataCodec.PixelCodec = pixelCodec;

                canDecode = true;
            }

            initalized = true;
        }
示例#20
0
        private static string DataFormatToString(GimDataFormat format)
        {
            switch (format)
            {
                case GimDataFormat.Rgb565:   return "RGB565";
                case GimDataFormat.Argb1555: return "ARGB1555";
                case GimDataFormat.Argb4444: return "ARGB4444";
                case GimDataFormat.Argb8888: return "ARGB8888";
                case GimDataFormat.Index4:   return "4-bit Indexed";
                case GimDataFormat.Index8:   return "8-bit Indexed";
            }

            return "Unknown";
        }