/// <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 SvrTextureEncoder(string file, SvrPixelFormat pixelFormat, SvrDataFormat dataFormat) : base(file) { if (decodedBitmap != null) { initalized = Initalize(pixelFormat, dataFormat); } }
public static SvrDataCodec GetDataCodec(SvrDataFormat format) { switch (format) { case SvrDataFormat.Rectangle: return(new Rectangle()); case SvrDataFormat.Index4ExternalPalette: return(new Index4ExtClut()); case SvrDataFormat.Index8ExternalPalette: return(new Index8ExtClut()); case SvrDataFormat.Index4Rgb5a3Rectangle: case SvrDataFormat.Index4Rgb5a3Square: case SvrDataFormat.Index4Argb8Rectangle: case SvrDataFormat.Index4Argb8Square: return(new Index4()); case SvrDataFormat.Index8Rgb5a3Rectangle: case SvrDataFormat.Index8Rgb5a3Square: case SvrDataFormat.Index8Argb8Rectangle: case SvrDataFormat.Index8Argb8Square: return(new Index8()); } return(null); }
/// <summary> /// Open a bitmap from a file. /// </summary> /// <param name="file">Filename of the file that contains the bitmap data.</param> /// <param name="PixelFormat">Pixel Format</param> /// <param name="DataFormat">Data Format</param> public SvrTextureEncoder(string file, SvrPixelFormat PixelFormat, SvrDataFormat DataFormat) : base(file) { this.PixelFormat = (byte)PixelFormat; this.DataFormat = (byte)DataFormat; InitSuccess = Initalize(); }
/// <summary> /// Open a bitmap from a stream. /// </summary> /// <param name="stream">Stream that contains the bitmap data.</param> /// <param name="PixelFormat">Pixel Format</param> /// <param name="DataFormat">Data Format</param> public SvrTextureEncoder(Stream stream, SvrPixelFormat PixelFormat, SvrDataFormat DataFormat) : base(stream) { this.PixelFormat = (byte)PixelFormat; this.DataFormat = (byte)DataFormat; InitSuccess = Initalize(); }
/// <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 SvrTextureEncoder(Stream source, SvrPixelFormat pixelFormat, SvrDataFormat dataFormat) : base(source) { if (decodedBitmap != null) { initalized = Initalize(pixelFormat, dataFormat); } }
/// <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 SvrTextureEncoder(byte[] source, int offset, int length, SvrPixelFormat pixelFormat, SvrDataFormat dataFormat) : base(source, offset, length) { if (decodedBitmap != null) { initalized = Initalize(pixelFormat, dataFormat); } }
/// <summary> /// Open a bitmap from a byte array. /// </summary> /// <param name="array">Byte array that contains the bitmap data.</param> /// <param name="PixelFormat">Pixel Format</param> /// <param name="DataFormat">Data Format</param> public SvrTextureEncoder(byte[] array, SvrPixelFormat PixelFormat, SvrDataFormat DataFormat) : base(array) { this.PixelFormat = (byte)PixelFormat; this.DataFormat = (byte)DataFormat; InitSuccess = Initalize(); }
/// <summary> /// Open a bitmap from a System.Drawing.Bitmap. /// </summary> /// <param name="bitmap">A System.Drawing.Bitmap instance.</param> /// <param name="PixelFormat">Pixel Format</param> /// <param name="DataFormat">Data Format</param> public SvrTextureEncoder(Bitmap bitmap, SvrPixelFormat PixelFormat, SvrDataFormat DataFormat) : base(bitmap) { this.PixelFormat = (byte)PixelFormat; this.DataFormat = (byte)DataFormat; InitSuccess = Initalize(); }
private static string DataFormatToString(SvrDataFormat format) { switch (format) { case SvrDataFormat.Rectangle: return("Rectangle"); case SvrDataFormat.Index4ExternalPalette: return("4-bit Indexed with External Palette"); case SvrDataFormat.Index8ExternalPalette: return("8-bit Indexed with External Palette"); case SvrDataFormat.Index4Rgb5a3Rectangle: case SvrDataFormat.Index4Rgb5a3Square: case SvrDataFormat.Index4Argb8Rectangle: case SvrDataFormat.Index4Argb8Square: return("4-bit Indexed"); case SvrDataFormat.Index8Rgb5a3Rectangle: case SvrDataFormat.Index8Rgb5a3Square: case SvrDataFormat.Index8Argb8Rectangle: case SvrDataFormat.Index8Argb8Square: return("8-bit Indexed"); } return("Unknown"); }
public static SvrDataCodec GetDataCodec(SvrDataFormat format) { switch (format) { case SvrDataFormat.Rectangle: return new Rectangle(); case SvrDataFormat.Index4ExternalPalette: return new Index4ExtClut(); case SvrDataFormat.Index8ExternalPalette: return new Index8ExtClut(); case SvrDataFormat.Index4Rgb5a3Rectangle: case SvrDataFormat.Index4Rgb5a3Square: case SvrDataFormat.Index4Argb8Rectangle: case SvrDataFormat.Index4Argb8Square: return new Index4(); case SvrDataFormat.Index8Rgb5a3Rectangle: case SvrDataFormat.Index8Rgb5a3Square: case SvrDataFormat.Index8Argb8Rectangle: case SvrDataFormat.Index8Argb8Square: return new Index8(); } return null; }
// Gets the data codec public static SvrDataCodec GetDataCodec(SvrDataFormat format) { switch (format) { case SvrDataFormat.Rectangle: return new SvrDataCodec.Rectangle(); case SvrDataFormat.Index4ExtClut: return new SvrDataCodec.Index4ExtClut(); case SvrDataFormat.Index8ExtClut: return new SvrDataCodec.Index8ExtClut(); case SvrDataFormat.Index4RectRgb5a3: case SvrDataFormat.Index4SqrRgb5a3: case SvrDataFormat.Index4RectArgb8: case SvrDataFormat.Index4SqrArgb8: return new SvrDataCodec.Index4(); case SvrDataFormat.Index8RectRgb5a3: case SvrDataFormat.Index8SqrRgb5a3: case SvrDataFormat.Index8RectArgb8: case SvrDataFormat.Index8SqrArgb8: return new SvrDataCodec.Index8(); } return null; }
private bool Initalize(SvrPixelFormat pixelFormat, SvrDataFormat dataFormat) { // Set the default values hasGlobalIndex = true; globalIndex = 0; // Set the data format and pixel format and load the appropiate codecs this.pixelFormat = pixelFormat; pixelCodec = SvrPixelCodec.GetPixelCodec(pixelFormat); this.dataFormat = dataFormat; dataCodec = SvrDataCodec.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; // Set the correct data format (it's ok to do it after getting the codecs). if (dataFormat == SvrDataFormat.Index4Rgb5a3Rectangle || dataFormat == SvrDataFormat.Index4Rgb5a3Square || dataFormat == SvrDataFormat.Index4Argb8Rectangle || dataFormat == SvrDataFormat.Index4Argb8Square) { if (textureWidth == textureHeight) // Square texture { if (pixelFormat == SvrPixelFormat.Rgb5a3) { dataFormat = SvrDataFormat.Index4Rgb5a3Square; } else { dataFormat = SvrDataFormat.Index4Argb8Square; } } else // Rectangular texture { if (pixelFormat == SvrPixelFormat.Rgb5a3) { dataFormat = SvrDataFormat.Index4Rgb5a3Rectangle; } else { dataFormat = SvrDataFormat.Index4Argb8Rectangle; } } } else if (dataFormat == SvrDataFormat.Index8Rgb5a3Rectangle || dataFormat == SvrDataFormat.Index8Rgb5a3Square || dataFormat == SvrDataFormat.Index8Argb8Rectangle || dataFormat == SvrDataFormat.Index8Argb8Square) { if (textureWidth == textureHeight) // Square texture { if (pixelFormat == SvrPixelFormat.Rgb5a3) { dataFormat = SvrDataFormat.Index8Rgb5a3Square; } else { dataFormat = SvrDataFormat.Index8Argb8Square; } } else // Rectangular texture { if (pixelFormat == SvrPixelFormat.Rgb5a3) { dataFormat = SvrDataFormat.Index8Rgb5a3Rectangle; } else { dataFormat = SvrDataFormat.Index8Argb8Rectangle; } } } 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 SvpPaletteEncoder(texturePalette, (ushort)dataCodec.PaletteEntries, pixelFormat, pixelCodec); } } else { // Convert the bitmap to an array decodedData = BitmapToRaw(decodedBitmap); } return true; }
private bool Initalize(SvrPixelFormat pixelFormat, SvrDataFormat dataFormat) { // Set the default values hasGlobalIndex = true; globalIndex = 0; // Set the data format and pixel format and load the appropiate codecs this.pixelFormat = pixelFormat; pixelCodec = SvrPixelCodec.GetPixelCodec(pixelFormat); this.dataFormat = dataFormat; dataCodec = SvrDataCodec.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); } // Set the correct data format (it's ok to do it after getting the codecs). if (dataFormat == SvrDataFormat.Index4Rgb5a3Rectangle || dataFormat == SvrDataFormat.Index4Rgb5a3Square || dataFormat == SvrDataFormat.Index4Argb8Rectangle || dataFormat == SvrDataFormat.Index4Argb8Square) { if (textureWidth == textureHeight) // Square texture { if (pixelFormat == SvrPixelFormat.Rgb5a3) { dataFormat = SvrDataFormat.Index4Rgb5a3Square; } else { dataFormat = SvrDataFormat.Index4Argb8Square; } } else // Rectangular texture { if (pixelFormat == SvrPixelFormat.Rgb5a3) { dataFormat = SvrDataFormat.Index4Rgb5a3Rectangle; } else { dataFormat = SvrDataFormat.Index4Argb8Rectangle; } } } else if (dataFormat == SvrDataFormat.Index8Rgb5a3Rectangle || dataFormat == SvrDataFormat.Index8Rgb5a3Square || dataFormat == SvrDataFormat.Index8Argb8Rectangle || dataFormat == SvrDataFormat.Index8Argb8Square) { if (textureWidth == textureHeight) // Square texture { if (pixelFormat == SvrPixelFormat.Rgb5a3) { dataFormat = SvrDataFormat.Index8Rgb5a3Square; } else { dataFormat = SvrDataFormat.Index8Argb8Square; } } else // Rectangular texture { if (pixelFormat == SvrPixelFormat.Rgb5a3) { dataFormat = SvrDataFormat.Index8Rgb5a3Rectangle; } else { dataFormat = SvrDataFormat.Index8Argb8Rectangle; } } } 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 SvpPaletteEncoder(texturePalette, (ushort)dataCodec.PaletteEntries, pixelFormat, pixelCodec); } } else { // Convert the bitmap to an array decodedData = BitmapToRaw(decodedBitmap); } return(true); }
protected override void Initalize() { // Check to see if what we are dealing with is a SVR texture if (!Is(encodedData)) { throw new NotAValidTextureException("This is not a valid GVR texture."); } // Determine the offsets of the GBIX (if present) and PVRT header chunks. if (PTMethods.Contains(encodedData, 0, Encoding.UTF8.GetBytes("GBIX"))) { 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 = 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 = (SvrPixelFormat)encodedData[pvrtOffset + 0x08]; dataFormat = (SvrDataFormat)encodedData[pvrtOffset + 0x09]; // Get the codecs and make sure we can decode using them pixelCodec = SvrPixelCodec.GetPixelCodec(pixelFormat); dataCodec = SvrDataCodec.GetDataCodec(dataFormat); if (dataCodec != null && pixelCodec != null) { dataCodec.PixelCodec = pixelCodec; canDecode = true; } // Set the palette and data offsets paletteEntries = dataCodec.PaletteEntries; if (!canDecode || paletteEntries == 0 || dataCodec.NeedsExternalPalette) { paletteOffset = -1; dataOffset = pvrtOffset + 0x10; } else { paletteOffset = pvrtOffset + 0x10; dataOffset = paletteOffset + (paletteEntries * (pixelCodec.Bpp >> 3)); } initalized = true; }
// Checks to see if we can encode the texture based on data format specific things private bool CanEncode(SvrPixelFormat PixelFormat, SvrDataFormat DataFormat, int width, int height) { // The converter should check to see that a pixel codec and data codec exists, // along with checking that width >= 8 and height >= 8. switch (DataFormat) { case SvrDataFormat.Index4RectRgb5a3: case SvrDataFormat.Index8RectRgb5a3: return (PixelFormat == SvrPixelFormat.Rgb5a3); case SvrDataFormat.Index4SqrRgb5a3: case SvrDataFormat.Index8SqrRgb5a3: return (width == height && PixelFormat == SvrPixelFormat.Rgb5a3); case SvrDataFormat.Index4RectArgb8: case SvrDataFormat.Index8RectArgb8: return (PixelFormat == SvrPixelFormat.Argb8888); case SvrDataFormat.Index4SqrArgb8: case SvrDataFormat.Index8SqrArgb8: return (width == height && PixelFormat == SvrPixelFormat.Argb8888); } return true; }
private static string DataFormatToString(SvrDataFormat format) { switch (format) { case SvrDataFormat.Rectangle: return "Rectangle"; case SvrDataFormat.Index4ExternalPalette: return "4-bit Indexed with External Palette"; case SvrDataFormat.Index8ExternalPalette: return "8-bit Indexed with External Palette"; case SvrDataFormat.Index4Rgb5a3Rectangle: case SvrDataFormat.Index4Rgb5a3Square: case SvrDataFormat.Index4Argb8Rectangle: case SvrDataFormat.Index4Argb8Square: return "4-bit Indexed"; case SvrDataFormat.Index8Rgb5a3Rectangle: case SvrDataFormat.Index8Rgb5a3Square: case SvrDataFormat.Index8Argb8Rectangle: case SvrDataFormat.Index8Argb8Square: return "8-bit Indexed"; } return "Unknown"; }