Inheritance: VrTexture
Beispiel #1
0
        /// <summary>
        /// Decodes a texture from a stream.
        /// </summary>
        /// <param name="source">The stream to read from.</param>
        /// <param name="destination">The stream to write to.</param>
        /// <param name="length">Number of bytes to read.</param>
        public override void Read(Stream source, Stream destination)
        {
            // Reading PVR textures is done through VrSharp, so just pass it to that
            VrSharp.PvrTexture.PvrTexture texture = new VrSharp.PvrTexture.PvrTexture(source);

            // Check to see if this texture requires an external palette and throw an exception
            // if we do not have one defined
            if (texture.NeedsExternalPalette)
            {
                if (PaletteStream != null)
                {
                    if (PaletteLength == -1)
                    {
                        texture.SetPalette(new PvpPalette(PaletteStream));
                    }
                    else
                    {
                        texture.SetPalette(new PvpPalette(PaletteStream, PaletteLength));
                    }

                    PaletteStream = null;
                    PaletteLength = -1;
                }
                else
                {
                    throw new TextureNeedsPaletteException();
                }
            }

            texture.Save(destination);
        }
Beispiel #2
0
        // Convert the texture to a bitmap
        public override Bitmap Unpack(ref Stream data)
        {
            try
            {
                PvrTexture TextureInput = new PvrTexture(data.Copy());
                if (TextureInput.NeedsExternalClut())
                {
                    if (PaletteData != null)
                        TextureInput.SetClut(new PvpClut(PaletteData.Copy())); // Texture has an external clut; set it
                    else
                        throw new GraphicFormatNeedsPalette(); // Texture needs an external clut; throw an exception
                }

                return TextureInput.GetTextureAsBitmap();
            }
            catch (GraphicFormatNeedsPalette)
            {
                throw new GraphicFormatNeedsPalette(); // Throw it again
            }
            //catch   { return null; }
            finally { PaletteData = null; }
        }
Beispiel #3
0
        public static void Information(string[] args)
        {
            PvrTexture texture;

            // Initalize the texture
            try
            {
                texture = new PvrTexture(args[1]);
            }
            catch (NotAValidTextureException)
            {
                Console.WriteLine("Error: This is not a valid PVR texture.");
                return;
            }

            Console.WriteLine("Texture Information");
            Console.WriteLine("--------------------");
            Console.WriteLine("Format       : PVR");
            if (texture.HasGlobalIndex)
            {
                Console.WriteLine("Global Index : {0}", texture.GlobalIndex);
            }
            Console.WriteLine("Dimensions   : {0}x{1}", texture.TextureWidth, texture.TextureHeight);
            Console.WriteLine("Pixel Format : {0}", PixelFormatToString(texture.PixelFormat));
            Console.WriteLine("Data Format  : {0}", DataFormatToString(texture.DataFormat));
            if (texture.CompressionFormat != PvrCompressionFormat.None)
            {
                Console.WriteLine("Compression  : {0}", CompressionFormatToString(texture.CompressionFormat));
            }
        }
Beispiel #4
0
        public static void Decode(string[] args)
        {
            string inPalettePath = Path.ChangeExtension(args[1], ".pvp");
            string outPath = Path.ChangeExtension(args[1], ".png");

            // Get arguments
            for (int i = 2; i < args.Length; i++)
            {
                string arg = args[i].ToLower();

                // Change the output path
                if (arg == "-o" && args.Length > i + 1)
                {
                    outPath = args[i + 1];
                    i++;
                }

                // Palette path
                if (arg == "-p" && args.Length > i + 1)
                {
                    inPalettePath = args[i + 1];
                    i++;
                }
            }

            PvrTexture texture;

            // Initalize the texture
            try
            {
                texture = new PvrTexture(args[1]);
            }
            catch (NotAValidTextureException)
            {
                Console.WriteLine("Error: This is not a valid PVR texture.");
                return;
            }

            // Does this texture need an external palette file?
            if (texture.NeedsExternalPalette)
            {
                if (!File.Exists(inPalettePath))
                {
                    Console.WriteLine("Error: This texture requires an external palette file.");
                    return;
                }

                PvpPalette palette = new PvpPalette(inPalettePath);
                if (!palette.Initalized)
                {
                    Console.WriteLine("Error: {0} is not a valid PVR palette file.", inPalettePath);
                }

                texture.SetPalette(palette);
            }

            Console.WriteLine("Texture Information");
            Console.WriteLine("--------------------");
            Console.WriteLine("Format       : PVR");
            if (texture.HasGlobalIndex)
            {
                Console.WriteLine("Global Index : {0}", texture.GlobalIndex);
            }
            Console.WriteLine("Dimensions   : {0}x{1}", texture.TextureWidth, texture.TextureHeight);
            Console.WriteLine("Pixel Format : {0}", PixelFormatToString(texture.PixelFormat));
            Console.WriteLine("Data Format  : {0}", DataFormatToString(texture.DataFormat));
            if (texture.CompressionFormat != PvrCompressionFormat.None)
            {
                Console.WriteLine("Compression  : {0}", CompressionFormatToString(texture.CompressionFormat));
            }

            // Decode the texture
            try
            {
                texture.Save(outPath);
            }
            catch (CannotDecodeTextureException)
            {
                Console.WriteLine("Error: Unable to decode this texture. The texture's pixel format or data format may not be supported.");
                return;
            }

            Console.WriteLine("\nTexture decoded successfully.");
        }
Beispiel #5
0
 public TextureInfo(string name, PvrTexture texture)
 {
     Name = name;
     GlobalIndex = texture.GlobalIndex;
     DataFormat = texture.DataFormat;
     Mipmap = DataFormat == PvrDataFormat.SquareTwiddledMipmaps || DataFormat == PvrDataFormat.SquareTwiddledMipmapsAlt;
     PixelFormat = texture.PixelFormat;
     Image = texture.ToBitmap();
 }
Beispiel #6
0
 private bool GetTextures(string filename)
 {
     byte[] pvmdata = File.ReadAllBytes(filename);
     if (Path.GetExtension(filename).Equals(".prs", StringComparison.OrdinalIgnoreCase))
         pvmdata = FraGag.Compression.Prs.Decompress(pvmdata);
     ArchiveBase pvmfile = new PvmArchive();
     if (!pvmfile.Is(pvmdata, filename))
     {
         MessageBox.Show(this, "Could not open file \"" + filename + "\".", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
         return false;
     }
     PvpPalette pvp = null;
     ArchiveEntryCollection pvmentries = pvmfile.Open(pvmdata).Entries;
     List<TextureInfo> newtextures = new List<TextureInfo>(pvmentries.Count);
     foreach (ArchiveEntry file in pvmentries)
     {
         PvrTexture vrfile = new PvrTexture(file.Open());
         if (vrfile.NeedsExternalPalette)
         {
             if (pvp == null)
                 using (System.Windows.Forms.OpenFileDialog a = new System.Windows.Forms.OpenFileDialog
                 {
                     DefaultExt = "pvp",
                     Filter = "PVP Files|*.pvp",
                     InitialDirectory = Path.GetDirectoryName(filename),
                     Title = "External palette file"
                 })
                     if (a.ShowDialog(this) == DialogResult.OK)
                         pvp = new PvpPalette(a.FileName);
                     else
                     {
                         MessageBox.Show(this, "Could not open file \"" + Program.Arguments[0] + "\".", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                         return false;
                     }
             vrfile.SetPalette(pvp);
         }
         newtextures.Add(new TextureInfo(Path.GetFileNameWithoutExtension(file.Name), vrfile));
     }
     textures.Clear();
     textures.AddRange(newtextures);
     listBox1.Items.Clear();
     listBox1.Items.AddRange(textures.Select((item) => item.Name).ToArray());
     SetFilename(Path.GetFullPath(filename));
     return true;
 }
Beispiel #7
0
        /// <summary>
        /// Decodes a texture from a stream.
        /// </summary>
        /// <param name="source">The stream to read from.</param>
        /// <param name="destination">The stream to write to.</param>
        /// <param name="length">Number of bytes to read.</param>
        public override void Read(Stream source, Stream destination)
        {
            // Reading PVR textures is done through VrSharp, so just pass it to that
            VrSharp.PvrTexture.PvrTexture texture = new VrSharp.PvrTexture.PvrTexture(source);

            // Check to see if this texture requires an external palette and throw an exception
            // if we do not have one defined
            if (texture.NeedsExternalPalette)
            {
                if (PaletteStream != null)
                {
                    if (PaletteLength == -1)
                    {
                        texture.SetPalette(new PvpPalette(PaletteStream));
                    }
                    else
                    {
                        texture.SetPalette(new PvpPalette(PaletteStream, PaletteLength));
                    }

                    PaletteStream = null;
                    PaletteLength = -1;
                }
                else
                {
                    throw new TextureNeedsPaletteException();
                }
            }

            texture.Save(destination);
        }
Beispiel #8
0
            public bool DecodeTexture(byte[] VrData, string ClutFile, out MemoryStream BitmapData)
            {
                BitmapData = null; // Set the bitmap data to null for now

                // Load the Pvr texture
                PvrTexture PvrTexture = new PvrTexture(VrData);
                if (!PvrTexture.LoadSuccess())
                {
                    Console.WriteLine("ERROR: Unsupported textue format or unable to load texture.");
                    return false;
                }

                // Set the external clut file
                if (PvrTexture.NeedsExternalClut())
                {
                    if (ClutFile == null || !File.Exists(ClutFile))
                    {
                        Console.WriteLine("ERROR: Texture needs an external clut file.");
                        return false;
                    }
                    PvpClut PvpClut = new PvpClut(ClutFile);
                    if (!PvpClut.LoadSuccess())
                    {
                        Console.WriteLine("ERROR: Unable to load clut file.");
                        return false;
                    }
                    PvrTexture.SetClut(PvpClut);
                }

                // Output information to the console
                PvrTextureInfo TextureInfo = (PvrTextureInfo)PvrTexture.GetTextureInfo();
                Console.WriteLine();
                Console.WriteLine("Texture Type : Pvr");
                if (TextureInfo.CompressionFormat != PvrCompressionFormat.None)
                    Console.WriteLine("Compression  : {0}",   TextureInfo.CompressionFormat);
                Console.WriteLine("Dimensions   : {0}x{1}",   TextureInfo.TextureWidth, TextureInfo.TextureHeight);
                Console.WriteLine("Pixel Format : {0} ({1})", TextureInfo.PixelFormat.ToString("X2"), GetPixelFormatAsText(TextureInfo.PixelFormat));
                Console.WriteLine("Data Format  : {0} ({1})", TextureInfo.DataFormat.ToString("X2"),  GetDataFormatAsText(TextureInfo.DataFormat));
                Console.WriteLine();

                // Decode the texture
                try { BitmapData = PvrTexture.GetTextureAsStream(); }
                catch
                {
                    Console.WriteLine("ERROR: Unable to decode texture.");
                    return false;
                }

                return true;
            }