Beispiel #1
0
        public override void Write(Stream source, Stream destination)
        {
            // Writing GVR textures is done through VrSharp, so just pass it to that
            GvrTextureEncoder texture = new GvrTextureEncoder(source, PaletteFormat, DataFormat);

            if (!texture.Initalized)
            {
                throw new TextureNotInitalizedException("Unable to initalize texture.");
            }

            texture.HasGlobalIndex = HasGlobalIndex;
            if (texture.HasGlobalIndex)
            {
                texture.GlobalIndex = GlobalIndex;
                texture.GbixType    = GbixType;
            }

            texture.HasMipmaps           = HasMipmaps;
            texture.NeedsExternalPalette = needsExternalPalette;

            // If we have an external palette file, save it
            if (texture.NeedsExternalPalette)
            {
                needsExternalPalette = true;

                PaletteStream = new MemoryStream();
                texture.PaletteEncoder.Save(PaletteStream);
            }
            else
            {
                needsExternalPalette = false;
            }

            texture.Save(destination);
        }
Beispiel #2
0
        public static void Encode(string[] args)
        {
            string outPath        = Path.ChangeExtension(args[1], ".gvr");
            string outPalettePath = Path.ChangeExtension(args[1], ".gvp");

            GvrTextureEncoder texture = new GvrTextureEncoder(args[1], StringToPixelFormat(args[3]), StringToDataFormat(args[4]));

            // Was this texture initalized successfully
            if (!texture.Initalized)
            {
                Console.WriteLine("Error: Unable to encode using the specified texture.");
                return;
            }

            // Get arguments
            for (int i = 5; 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++;
                }

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

                // No global index
                if (arg == "-nogbix")
                {
                    texture.HasGlobalIndex = false;
                }

                // GBIX type is GCIX
                if (arg == "-gcix")
                {
                    texture.GbixType = GvrGbixType.Gcix;
                }

                // Set global index
                if (arg == "-gi" && args.Length > i + 1)
                {
                    uint globalIndex;
                    if (!uint.TryParse(args[i + 1], out globalIndex))
                    {
                        globalIndex = 0;
                    }
                    texture.GlobalIndex = globalIndex;

                    i++;
                }

                // Set that the texture has mipmaps
                if (arg == "-mipmaps")
                {
                    texture.HasMipmaps = true;
                }

                // Set that the texture has an external palette
                if (arg == "-ep")
                {
                    texture.NeedsExternalPalette = true;
                }
            }

            Console.WriteLine("Texture Information");
            Console.WriteLine("--------------------");
            Console.WriteLine("Format         : GVR");
            if (texture.HasGlobalIndex)
            {
                Console.WriteLine("Global Index   : {0}", texture.GlobalIndex);
            }
            Console.WriteLine("Dimensions     : {0}x{1}", texture.TextureWidth, texture.TextureHeight);
            if (texture.PixelFormat != GvrPixelFormat.Unknown)
            {
                Console.WriteLine("Palette Format : {0}", PixelFormatToString(texture.PixelFormat));
            }
            Console.WriteLine("Data Format    : {0}", DataFormatToString(texture.DataFormat));
            if (texture.DataFlags != GvrDataFlags.None)
            {
                Console.WriteLine("Data Flags     : {0}", DataFlagsToString(texture.DataFlags));
            }

            texture.Save(outPath);

            if (texture.NeedsExternalPalette)
            {
                texture.PaletteEncoder.Save(outPalettePath);
            }

            Console.WriteLine("\nTexture encoded successfully.");
        }
Beispiel #3
0
        private void SaveTextures()
        {
            byte[] data;
            using (MemoryStream str = new MemoryStream())
            {
                ArchiveWriter writer = null;
                switch (format)
                {
                case TextureFormat.PVM:
                    writer = new PvmArchiveWriter(str);
                    foreach (PvrTextureInfo tex in textures)
                    {
                        if (tex.DataFormat != PvrDataFormat.Index4 && tex.DataFormat != PvrDataFormat.Index8)
                        {
                            System.Drawing.Imaging.BitmapData bmpd = tex.Image.LockBits(new Rectangle(Point.Empty, tex.Image.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                            int    stride = bmpd.Stride;
                            byte[] bits   = new byte[Math.Abs(stride) * bmpd.Height];
                            System.Runtime.InteropServices.Marshal.Copy(bmpd.Scan0, bits, 0, bits.Length);
                            tex.Image.UnlockBits(bmpd);
                            int tlevels = 0;
                            for (int y = 0; y < tex.Image.Height; y++)
                            {
                                int srcaddr = y * Math.Abs(stride);
                                for (int x = 0; x < tex.Image.Width; x++)
                                {
                                    Color c = Color.FromArgb(BitConverter.ToInt32(bits, srcaddr + (x * 4)));
                                    if (c.A == 0)
                                    {
                                        tlevels = 1;
                                    }
                                    else if (c.A < 255)
                                    {
                                        tlevels = 2;
                                        break;
                                    }
                                }
                                if (tlevels == 2)
                                {
                                    break;
                                }
                            }
                            if (tlevels == 0)
                            {
                                tex.PixelFormat = PvrPixelFormat.Rgb565;
                            }
                            else if (tlevels == 1)
                            {
                                tex.PixelFormat = PvrPixelFormat.Argb1555;
                            }
                            else if (tlevels == 2)
                            {
                                tex.PixelFormat = PvrPixelFormat.Argb4444;
                            }
                            if (tex.Image.Width == tex.Image.Height)
                            {
                                if (tex.Mipmap)
                                {
                                    tex.DataFormat = PvrDataFormat.SquareTwiddledMipmaps;
                                }
                                else
                                {
                                    tex.DataFormat = PvrDataFormat.SquareTwiddled;
                                }
                            }
                            else
                            {
                                tex.DataFormat = PvrDataFormat.Rectangle;
                            }
                        }
                        PvrTextureEncoder encoder = new PvrTextureEncoder(tex.Image, tex.PixelFormat, tex.DataFormat);
                        encoder.GlobalIndex = tex.GlobalIndex;
                        MemoryStream pvr = new MemoryStream();
                        encoder.Save(pvr);
                        pvr.Seek(0, SeekOrigin.Begin);
                        writer.CreateEntry(pvr, tex.Name);
                    }
                    break;

                case TextureFormat.GVM:
                    writer = new GvmArchiveWriter(str);
                    foreach (GvrTextureInfo tex in textures)
                    {
                        if (tex.DataFormat != GvrDataFormat.Index4 && tex.DataFormat != GvrDataFormat.Index8)
                        {
                            System.Drawing.Imaging.BitmapData bmpd = tex.Image.LockBits(new Rectangle(Point.Empty, tex.Image.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                            int    stride = bmpd.Stride;
                            byte[] bits   = new byte[Math.Abs(stride) * bmpd.Height];
                            System.Runtime.InteropServices.Marshal.Copy(bmpd.Scan0, bits, 0, bits.Length);
                            tex.Image.UnlockBits(bmpd);
                            int tlevels = 0;
                            for (int y = 0; y < tex.Image.Height; y++)
                            {
                                int srcaddr = y * Math.Abs(stride);
                                for (int x = 0; x < tex.Image.Width; x++)
                                {
                                    Color c = Color.FromArgb(BitConverter.ToInt32(bits, srcaddr + (x * 4)));
                                    if (c.A == 0)
                                    {
                                        tlevels = 1;
                                    }
                                    else if (c.A < 255)
                                    {
                                        tlevels = 2;
                                        break;
                                    }
                                }
                                if (tlevels == 2)
                                {
                                    break;
                                }
                            }
                            if (!tex.Mipmap)
                            {
                                tex.DataFormat = GvrDataFormat.Argb8888;
                            }
                            else if (tlevels == 0)
                            {
                                tex.DataFormat = GvrDataFormat.Rgb565;
                            }
                            else
                            {
                                tex.DataFormat = GvrDataFormat.Rgb5a3;
                            }
                        }
                        GvrTextureEncoder encoder = new GvrTextureEncoder(tex.Image, tex.PixelFormat, tex.DataFormat);
                        encoder.GlobalIndex = tex.GlobalIndex;
                        MemoryStream gvr = new MemoryStream();
                        encoder.Save(gvr);
                        gvr.Seek(0, SeekOrigin.Begin);
                        writer.CreateEntry(gvr, tex.Name);
                    }
                    break;

                case TextureFormat.PVMX:
                    PvmxArchive.Save(str, textures.Cast <PvmxTextureInfo>());
                    break;

                case TextureFormat.PAK:
                    PAKFile     pak       = new PAKFile();
                    string      filenoext = Path.GetFileNameWithoutExtension(filename).ToLowerInvariant();
                    string      longdir   = "..\\..\\..\\sonic2\\resource\\gd_pc\\prs\\" + filenoext;
                    List <byte> inf       = new List <byte>(textures.Count * 0x3C);
                    foreach (TextureInfo item in textures)
                    {
                        Stream tex = TextureLoader.SaveToStream(ImageFileFormat.Dds, Texture.FromBitmap(d3ddevice, item.Image, Usage.SoftwareProcessing, Pool.Managed));
                        byte[] tb  = new byte[tex.Length];
                        tex.Read(tb, 0, tb.Length);
                        string name = item.Name.ToLowerInvariant();
                        if (name.Length > 0x1C)
                        {
                            name = name.Substring(0, 0x1C);
                        }
                        pak.Files.Add(new PAKFile.File(filenoext + '\\' + name + ".dds", longdir + '\\' + name + ".dds", tb));
                        inf.AddRange(Encoding.ASCII.GetBytes(name));
                        if (name.Length != 0x1C)
                        {
                            inf.AddRange(new byte[0x1C - name.Length]);
                        }
                        inf.AddRange(BitConverter.GetBytes(item.GlobalIndex));
                        inf.AddRange(new byte[0xC]);
                        inf.AddRange(BitConverter.GetBytes(item.Image.Width));
                        inf.AddRange(BitConverter.GetBytes(item.Image.Height));
                        inf.AddRange(new byte[4]);
                        inf.AddRange(BitConverter.GetBytes(0x80000000));
                    }
                    pak.Files.Insert(0, new PAKFile.File(filenoext + '\\' + filenoext + ".inf", longdir + '\\' + filenoext + ".inf", inf.ToArray()));
                    pak.Save(filename);

                    return;
                }
                writer?.Flush();
                data = str.ToArray();
                str.Close();
            }
            if (Path.GetExtension(filename).Equals(".prs", StringComparison.OrdinalIgnoreCase))
            {
                FraGag.Compression.Prs.Compress(data, filename);
            }
            else
            {
                File.WriteAllBytes(filename, data);
            }
        }
Beispiel #4
0
        private void SaveTextures()
        {
            byte[] data;
            using (MemoryStream str = new MemoryStream())
            {
                ArchiveWriter writer = null;
                switch (format)
                {
                case TextureFormat.PVM:
                    writer = new PvmArchiveWriter(str);
                    foreach (PvrTextureInfo tex in textures)
                    {
                        if (tex.DataFormat != PvrDataFormat.Index4 && tex.DataFormat != PvrDataFormat.Index8)
                        {
                            System.Drawing.Imaging.BitmapData bmpd = tex.Image.LockBits(new Rectangle(Point.Empty, tex.Image.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                            int    stride = bmpd.Stride;
                            byte[] bits   = new byte[Math.Abs(stride) * bmpd.Height];
                            System.Runtime.InteropServices.Marshal.Copy(bmpd.Scan0, bits, 0, bits.Length);
                            tex.Image.UnlockBits(bmpd);
                            int tlevels = 0;
                            for (int y = 0; y < tex.Image.Height; y++)
                            {
                                int srcaddr = y * Math.Abs(stride);
                                for (int x = 0; x < tex.Image.Width; x++)
                                {
                                    Color c = Color.FromArgb(BitConverter.ToInt32(bits, srcaddr + (x * 4)));
                                    if (c.A == 0)
                                    {
                                        tlevels = 1;
                                    }
                                    else if (c.A < 255)
                                    {
                                        tlevels = 2;
                                        break;
                                    }
                                }
                                if (tlevels == 2)
                                {
                                    break;
                                }
                            }
                            if (tlevels == 0)
                            {
                                tex.PixelFormat = PvrPixelFormat.Rgb565;
                            }
                            else if (tlevels == 1)
                            {
                                tex.PixelFormat = PvrPixelFormat.Argb1555;
                            }
                            else if (tlevels == 2)
                            {
                                tex.PixelFormat = PvrPixelFormat.Argb4444;
                            }
                            if (tex.Image.Width == tex.Image.Height)
                            {
                                if (tex.Mipmap)
                                {
                                    tex.DataFormat = PvrDataFormat.SquareTwiddledMipmaps;
                                }
                                else
                                {
                                    tex.DataFormat = PvrDataFormat.SquareTwiddled;
                                }
                            }
                            else
                            {
                                tex.DataFormat = PvrDataFormat.Rectangle;
                            }
                        }
                        PvrTextureEncoder encoder = new PvrTextureEncoder(tex.Image, tex.PixelFormat, tex.DataFormat);
                        encoder.GlobalIndex = tex.GlobalIndex;
                        MemoryStream pvr = new MemoryStream();
                        encoder.Save(pvr);
                        pvr.Seek(0, SeekOrigin.Begin);
                        writer.CreateEntry(pvr, tex.Name);
                    }
                    break;

                case TextureFormat.GVM:
                    writer = new GvmArchiveWriter(str);
                    foreach (GvrTextureInfo tex in textures)
                    {
                        if (tex.DataFormat != GvrDataFormat.Index4 && tex.DataFormat != GvrDataFormat.Index8)
                        {
                            System.Drawing.Imaging.BitmapData bmpd = tex.Image.LockBits(new Rectangle(Point.Empty, tex.Image.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                            int    stride = bmpd.Stride;
                            byte[] bits   = new byte[Math.Abs(stride) * bmpd.Height];
                            System.Runtime.InteropServices.Marshal.Copy(bmpd.Scan0, bits, 0, bits.Length);
                            tex.Image.UnlockBits(bmpd);
                            int tlevels = 0;
                            for (int y = 0; y < tex.Image.Height; y++)
                            {
                                int srcaddr = y * Math.Abs(stride);
                                for (int x = 0; x < tex.Image.Width; x++)
                                {
                                    Color c = Color.FromArgb(BitConverter.ToInt32(bits, srcaddr + (x * 4)));
                                    if (c.A == 0)
                                    {
                                        tlevels = 1;
                                    }
                                    else if (c.A < 255)
                                    {
                                        tlevels = 2;
                                        break;
                                    }
                                }
                                if (tlevels == 2)
                                {
                                    break;
                                }
                            }
                            if (!tex.Mipmap)
                            {
                                tex.DataFormat = GvrDataFormat.Argb8888;
                            }
                            else if (tlevels == 0)
                            {
                                tex.DataFormat = GvrDataFormat.Rgb565;
                            }
                            else
                            {
                                tex.DataFormat = GvrDataFormat.Rgb5a3;
                            }
                        }
                        GvrTextureEncoder encoder = new GvrTextureEncoder(tex.Image, tex.PixelFormat, tex.DataFormat);
                        encoder.GlobalIndex = tex.GlobalIndex;
                        MemoryStream pvr = new MemoryStream();
                        encoder.Save(pvr);
                        pvr.Seek(0, SeekOrigin.Begin);
                        writer.CreateEntry(pvr, tex.Name);
                    }
                    break;
                }
                writer.Flush();
                data = str.ToArray();
                str.Close();
            }
            if (Path.GetExtension(filename).Equals(".prs", StringComparison.OrdinalIgnoreCase))
            {
                FraGag.Compression.Prs.Compress(data, filename);
            }
            else
            {
                File.WriteAllBytes(filename, data);
            }
        }