示例#1
0
        public override Bitmap GetBitmap()
        {
            PvrTexture pvrt = new PvrTexture(Data);

            if (pvrt.NeedsExternalPalette)
            {
                pvrt.SetPalette(Palette);
            }
            return(pvrt.ToBitmap());
        }
示例#2
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);
        }
示例#3
0
        public PvrTextureInfo(string name, MemoryStream str, PvpPalette pvp = null)
        {
            TextureData = str;
            PvrTexture texture = new PvrTexture(str);

            if (pvp != null)
            {
                texture.SetPalette(pvp);
            }
            Name        = name;
            GlobalIndex = texture.GlobalIndex;
            DataFormat  = texture.DataFormat;
            Mipmap      = DataFormat == PvrDataFormat.SquareTwiddledMipmaps || DataFormat == PvrDataFormat.SquareTwiddledMipmapsAlt;
            PixelFormat = texture.PixelFormat;
            Image       = texture.ToBitmap();
        }
示例#4
0
        /// <summary>
        /// 从图片数据中获取图片
        /// </summary>
        /// <param name="byData"></param>
        /// <returns></returns>
        public override Image[] ImageDecode(byte[] byData, string fileInfo)
        {
            PvrTexture pvrDecode = new PvrTexture(byData);

            if (pvrDecode.NeedsExternalPalette)
            {
                // 取得调色板数据
                KeyValuePair <string, byte[]> paletteKeyValue = this.paletteData.FirstOrDefault(p => p.Key.IndexOf(fileInfo) >= 0);
                if (string.IsNullOrEmpty(paletteKeyValue.Key))
                {
                    throw new Exception("没有找到调色板数据");
                }

                PvpPalette palette = new PvpPalette(paletteKeyValue.Value);
                pvrDecode.SetPalette(palette);
            }

            return(new Image[] { pvrDecode.ToBitmap() });
        }
示例#5
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.");
        }
示例#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();
            List <TextureInfo> newtextures;

            if (PvmxArchive.Is(pvmdata))
            {
                format      = TextureFormat.PVMX;
                newtextures = new List <TextureInfo>(PvmxArchive.GetTextures(pvmdata).Cast <TextureInfo>());
            }
            else if (PAKFile.Is(filename))
            {
                format = TextureFormat.PAK;
                PAKFile pak       = new PAKFile(filename);
                string  filenoext = Path.GetFileNameWithoutExtension(filename).ToLowerInvariant();
                byte[]  inf       = pak.Files.Single((file) => file.Name.Equals(filenoext + '\\' + filenoext + ".inf", StringComparison.OrdinalIgnoreCase)).Data;
                newtextures = new List <TextureInfo>(inf.Length / 0x3C);
                for (int i = 0; i < inf.Length; i += 0x3C)
                {
                    StringBuilder sb = new StringBuilder(0x1C);
                    for (int j = 0; j < 0x1C; j++)
                    {
                        if (inf[i + j] != 0)
                        {
                            sb.Append((char)inf[i + j]);
                        }
                        else
                        {
                            break;
                        }
                    }
                    byte[] dds = pak.Files.First((file) => file.Name.Equals(filenoext + '\\' + sb.ToString() + ".dds", StringComparison.OrdinalIgnoreCase)).Data;
                    using (MemoryStream str = new MemoryStream(dds))
                        using (Texture tex = TextureLoader.FromStream(d3ddevice, str))
                            using (Stream bmp = TextureLoader.SaveToStream(ImageFileFormat.Png, tex))
                                newtextures.Add(new PakTextureInfo(sb.ToString(), BitConverter.ToUInt32(inf, i + 0x1C), new Bitmap(bmp)));
                }
            }
            else
            {
                if (pvmfile.Is(pvmdata, filename))
                {
                    format = TextureFormat.PVM;
                }
                else
                {
                    pvmfile = new GvmArchive();
                    if (!pvmfile.Is(pvmdata, filename))
                    {
                        MessageBox.Show(this, "Could not open file \"" + filename + "\".", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return(false);
                    }
                    format = TextureFormat.GVM;
                }
                ArchiveEntryCollection pvmentries = pvmfile.Open(pvmdata).Entries;
                newtextures = new List <TextureInfo>(pvmentries.Count);
                switch (format)
                {
                case TextureFormat.PVM:
                    PvpPalette pvp = null;
                    foreach (ArchiveEntry file in pvmentries)
                    {
                        PvrTexture vrfile = new PvrTexture(file.Open());
                        if (vrfile.NeedsExternalPalette)
                        {
                            if (pvp == null)
                            {
                                using (OpenFileDialog a = new 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 PvrTextureInfo(Path.GetFileNameWithoutExtension(file.Name), vrfile));
                    }
                    break;

                case TextureFormat.GVM:
                    GvpPalette gvp = null;
                    foreach (ArchiveEntry file in pvmentries)
                    {
                        GvrTexture vrfile = new GvrTexture(file.Open());
                        if (vrfile.NeedsExternalPalette)
                        {
                            if (gvp == null)
                            {
                                using (OpenFileDialog a = new OpenFileDialog
                                {
                                    DefaultExt = "gvp",
                                    Filter = "GVP Files|*.gvp",
                                    InitialDirectory = Path.GetDirectoryName(filename),
                                    Title = "External palette file"
                                })
                                    if (a.ShowDialog(this) == DialogResult.OK)
                                    {
                                        gvp = new GvpPalette(a.FileName);
                                    }
                                    else
                                    {
                                        MessageBox.Show(this, "Could not open file \"" + Program.Arguments[0] + "\".", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                        return(false);
                                    }
                            }
                            vrfile.SetPalette(gvp);
                        }
                        newtextures.Add(new GvrTextureInfo(Path.GetFileNameWithoutExtension(file.Name), vrfile));
                    }
                    break;
                }
            }
            textures.Clear();
            textures.AddRange(newtextures);
            listBox1.Items.Clear();
            listBox1.Items.AddRange(textures.Select((item) => item.Name).ToArray());
            UpdateTextureCount();
            SetFilename(Path.GetFullPath(filename));
            return(true);
        }
示例#7
0
        private void addTextureButton_Click(object sender, EventArgs e)
        {
            string defext = null;
            string filter = null;

            switch (format)
            {
            case TextureFormat.PVM:
                defext = "pvr";
                filter = "Texture Files|*.prs;*.pvm;*.pvr;*.png;*.jpg;*.jpeg;*.gif;*.bmp";
                break;

            case TextureFormat.GVM:
                defext = "gvr";
                filter = "Texture Files|*.prs;*.gvm;*.gvr;*.png;*.jpg;*.jpeg;*.gif;*.bmp";
                break;
            }
            using (OpenFileDialog dlg = new OpenFileDialog()
            {
                DefaultExt = defext, Filter = filter, Multiselect = true
            })
            {
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    uint gbix = textures.Count == 0 ? 0 : textures.Max((item) => item.GlobalIndex);
                    if (gbix != uint.MaxValue)
                    {
                        gbix++;
                    }
                    listBox1.BeginUpdate();
                    foreach (string file in dlg.FileNames)
                    {
                        switch (Path.GetExtension(file).ToLowerInvariant())
                        {
                        case ".prs":
                        case ".pvm":
                        case ".gvm":
                            byte[] pvmdata = File.ReadAllBytes(file);
                            if (Path.GetExtension(file).Equals(".prs", StringComparison.OrdinalIgnoreCase))
                            {
                                pvmdata = FraGag.Compression.Prs.Decompress(pvmdata);
                            }
                            ArchiveBase pvmfile = null;
                            switch (format)
                            {
                            case TextureFormat.PVM:
                                pvmfile = new PvmArchive();
                                break;

                            case TextureFormat.GVM:
                                pvmfile = new GvmArchive();
                                break;
                            }
                            if (!pvmfile.Is(pvmdata, file))
                            {
                                MessageBox.Show(this, "Could not open file \"" + file + "\".", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                continue;
                            }
                            ArchiveEntryCollection pvmentries  = pvmfile.Open(pvmdata).Entries;
                            List <PvrTextureInfo>  newtextures = new List <PvrTextureInfo>(pvmentries.Count);
                            switch (format)
                            {
                            case TextureFormat.PVM:
                                PvpPalette pvp = null;
                                foreach (ArchiveEntry entry in pvmentries)
                                {
                                    PvrTexture vrfile = new PvrTexture(entry.Open());
                                    if (vrfile.NeedsExternalPalette)
                                    {
                                        if (pvp == null)
                                        {
                                            using (OpenFileDialog a = new OpenFileDialog
                                            {
                                                DefaultExt = "pvp",
                                                Filter = "PVP Files|*.pvp",
                                                InitialDirectory = Path.GetDirectoryName(file),
                                                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);
                                                    continue;
                                                }
                                        }
                                        vrfile.SetPalette(pvp);
                                    }
                                    string name = Path.GetFileNameWithoutExtension(entry.Name);
                                    textures.Add(new PvrTextureInfo(name, vrfile));
                                    listBox1.Items.Add(name);
                                }
                                break;

                            case TextureFormat.GVM:
                                GvpPalette gvp = null;
                                foreach (ArchiveEntry entry in pvmentries)
                                {
                                    GvrTexture vrfile = new GvrTexture(entry.Open());
                                    if (vrfile.NeedsExternalPalette)
                                    {
                                        if (gvp == null)
                                        {
                                            using (OpenFileDialog a = new OpenFileDialog
                                            {
                                                DefaultExt = "gvp",
                                                Filter = "GVP Files|*.gvp",
                                                InitialDirectory = Path.GetDirectoryName(file),
                                                Title = "External palette file"
                                            })
                                                if (a.ShowDialog(this) == DialogResult.OK)
                                                {
                                                    gvp = new GvpPalette(a.FileName);
                                                }
                                                else
                                                {
                                                    MessageBox.Show(this, "Could not open file \"" + Program.Arguments[0] + "\".", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                                    continue;
                                                }
                                        }
                                        vrfile.SetPalette(gvp);
                                    }
                                    string name = Path.GetFileNameWithoutExtension(entry.Name);
                                    textures.Add(new GvrTextureInfo(name, vrfile));
                                    listBox1.Items.Add(name);
                                }
                                break;
                            }
                            break;

                        default:
                        {
                            string name = Path.GetFileNameWithoutExtension(file);
                            if (format == TextureFormat.PVM && PvrTexture.Is(file))
                            {
                                textures.Add(new PvrTextureInfo(name, new PvrTexture(file)));
                            }
                            else if (format == TextureFormat.GVM && GvrTexture.Is(file))
                            {
                                textures.Add(new GvrTextureInfo(name, new GvrTexture(file)));
                            }
                            else
                            {
                                switch (format)
                                {
                                case TextureFormat.PVM:
                                    textures.Add(new PvrTextureInfo(name, gbix, new Bitmap(dlg.FileName)));
                                    break;

                                case TextureFormat.GVM:
                                    textures.Add(new GvrTextureInfo(name, gbix, new Bitmap(dlg.FileName)));
                                    break;
                                }
                                if (gbix != uint.MaxValue)
                                {
                                    gbix++;
                                }
                            }
                            listBox1.Items.Add(name);
                        }
                        break;
                        }
                    }
                    listBox1.EndUpdate();
                    listBox1.SelectedIndex = textures.Count - 1;
                    UpdateTextureCount();
                }
            }
        }