Exemplo n.º 1
0
        public static WiiImage Create(EndianReader stream)
        {
            byte id = stream.ReadByte();

            if (id != 1 && id != 2)
            {
                throw new FormatException();                 // ID byte
            }
            WiiImage image = new WiiImage();

            image.BitsPerPixel = stream.ReadByte(); // If this isn't 4... I think we're f****d.
            stream.Position   += 5;                 // 5 bytes of who knows what
            image.Width        = stream.ReadUInt16();
            image.Height       = stream.ReadUInt16();

            stream.ReadByte();             // Dunno...

            stream.PadReadTo(0x20);
            image.Bitmap = PixelEncoding.GetEncoding <CMPR>().DecodeImage(stream.Base, image.Width, image.Height);

            return(image);
        }
Exemplo n.º 2
0
        public static NgcImage Create(EndianReader stream)
        {
            NgcImage image = new NgcImage();

            image.BitsPerPixel = stream.ReadByte();             // If this isn't 4... I think we're f****d.
            stream.Position    = 0x0A;
            image.Width        = (int)Math.Pow(2, stream.ReadByte());
            image.Height       = (int)Math.Pow(2, stream.ReadByte());

            stream.PadReadTo(0x20);
            Bitmap bitmap = PixelEncoding.GetEncoding <CMPR>().DecodeImage(stream.Base, (uint)image.Width, (uint)image.Height);

            image.Bitmap = new Bitmap(image.Width, image.Height);
            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    image.Bitmap.SetPixel(x, y, bitmap.GetPixel(x, image.Height - y - 1));
                }
            }

            return(image);
        }
Exemplo n.º 3
0
        private static void OperationPng2Tex()
        {
            string filename = ConsolePath("Full path to tex or txm/txv or png file..?");

            if (filename.IsEmpty())
            {
                return;
            }

            if (Path.GetExtension(filename).ToLower() == ".txm" || Path.GetExtension(filename).ToLower() == ".txv")
            {
                filename = filename.Substring(0, filename.Length - 4);
            }
            else if (Path.GetExtension(filename).ToLower() != ".tex")
            {
                filename = Path.GetDirectoryName(filename);
                if (filename.EndsWith(".ext"))
                {
                    filename = filename.Substring(0, filename.Length - 4);
                }
            }

            DelayedStreamCache streams = new DelayedStreamCache();
            Txm    tex = OpenTex(filename, streams);
            Stream txmstream;
            Stream txvstream;

            if (File.Exists(filename))
            {
                txmstream = new TemporaryStream();
                txvstream = new TemporaryStream();
            }
            else
            {
                txmstream = new FileStream(filename + ".txm.new", FileMode.Create);
                txvstream = new FileStream(filename + ".txv.new", FileMode.Create);
            }

            streams.AddStream(txmstream);
            streams.AddStream(txvstream);

            string[] files = Directory.GetFiles(filename + ".ext");
            files = files.OrderBy(f => f).ToArray();             // Want to make sure the .dat files come before the pngs (hacky, but meh).
            foreach (string file in files)
            {
                string name      = Path.GetFileName(file);
                string basename  = Path.GetFileNameWithoutExtension(name);
                string extension = Path.GetExtension(name);

                Txm.Image image = tex.Images.FirstOrDefault(i => i.Name == basename);
                if (image == null)
                {
                    if (extension != ".dat" && extension != ".png")
                    {
                        continue;
                    }
                    image                 = new Txm.Image();
                    image.Name            = basename;
                    image.PrimaryEncoding = PixelEncoding.GetEncoding <CI8>();
                    image.SecondaryData   = new MemoryStream();
                    image.Unknown1        = 1;
                    image.Unknown2        = 2 << 4;
                    image.Unknown3        = 1;
                    tex.Images.Add(image);
                }

                Stream stream = new FileStream(file, FileMode.Open);
                if (extension == ".dat")
                {
                    image.SecondaryData = new MemoryStream((int)stream.Length);
                    Util.StreamCopy(image.SecondaryData, stream);
                }
                else if (extension == ".png")
                {
                    Bitmap bitmap = new Bitmap(stream);
                    stream.Close();
                    stream = new MemoryStream();
                    if (image.SecondaryData is Substream || image.SecondaryData == null)
                    {
                        image.SecondaryData = new MemoryStream();
                    }
                    image.SecondaryData.Position = 0;
                    try {
                        image.PrimaryEncoding.EncodeImage(stream, bitmap, image.SecondaryData, image.Unknown2 >> 4);
                    } catch (NotImplementedException ex) {
                        if (image.PrimaryEncoding.ID == 0xAAE4)
                        {
                            image.PrimaryEncoding = new ARGB();
                            image.Unknown1        = 5;
                            image.Unknown2        = 0xa5;
                            image.Unknown3        = 4;
                        }
                        else
                        {
                            image.PrimaryEncoding = PixelEncoding.GetEncoding <CI8>();
                            image.Unknown1        = 1;
                            image.Unknown2        = 2 << 4;
                            image.Unknown3        = 1;
                        }

                        try {
                            image.PrimaryEncoding.EncodeImage(stream, bitmap, image.SecondaryData, image.Unknown2 >> 4);
                        } catch {
                            throw ex;
                        }
                    }
                    image.Width       = (uint)bitmap.Width;
                    image.Height      = (uint)bitmap.Height;
                    image.PrimaryData = stream;
                }

                streams.AddStream(stream);
            }

            tex.Save(txmstream, txvstream);

            if (File.Exists(filename))
            {
                Stream texstream = new FileStream(filename + ".new", FileMode.Create);
                streams.AddStream(texstream);
                FPS4Base fps = new FPS4Base();
                fps.DataOffset = 0x60;
                fps.BlockSize  = 0x0C;
                fps.Files      = 3;
                fps.Flags      = FPS4Base.NodeFlags.SectionSize | FPS4Base.NodeFlags.Filesize | FPS4Base.NodeFlags.Offset;
                fps.Type       = new byte[] { 0x74, 0x78, 0x6D, 0x76 };           // "txmv"
                fps.TypeOffset = 0x40;
                uint txmlen = (uint)Util.RoundUp(txmstream.Length, 0x20);
                uint txvlen = (uint)Util.RoundUp(txvstream.Length, 0x20);
                fps.Nodes.Add(new FPS4Base.Node(0x60, txmlen, (uint)txmstream.Length, "", 0, 0, txmstream));
                fps.Nodes.Add(new FPS4Base.Node(0x60 + txmlen, txvlen, (uint)txvstream.Length, "", 0, 0, txvstream));
                fps.Nodes.Add(new FPS4Base.Node(0x60 + txmlen + txvlen, 0, 0, "", 0, 0, null));
                fps.Save(texstream);
            }

            streams.Dispose();
        }
Exemplo n.º 4
0
        public Txm(Stream stream, Stream data) : this()
        {
            EndianReader reader = new EndianReader(stream, Endianness.BigEndian);

            if (reader.ReadUInt32() != Magic)
            {
                throw new FormatException();
            }

            uint filesize = reader.ReadUInt32();

            Unknown = reader.ReadUInt32();
            uint  files = reader.ReadUInt32();
            ulong zero  = reader.ReadUInt64();

            if (zero != 0)
            {
                throw new FormatException();
            }

            Dictionary <Image, long> nameoffsets = new Dictionary <Image, long>();

            for (uint i = 0; i < files; i++)
            {
                Image image = new Image();
                image.Width    = reader.ReadUInt32();
                image.Height   = reader.ReadUInt32();
                image.Unknown1 = reader.ReadUInt32();
                image.Unknown2 = reader.ReadByte();
                image.Unknown3 = reader.ReadByte();

                ushort encoding = reader.ReadUInt16();

                if (encoding == 0xAAE4)                   // PS3 ToGf
                {
                    if (image.Unknown2 == 0x88 || image.Unknown2 == 0xa8)
                    {
                        image.PrimaryEncoding = new DXT5();
                    }
                    else if (image.Unknown2 == 0xa5)
                    {
                        image.PrimaryEncoding = new ARGB();
                    }
                    else if (image.Unknown2 == 0x85)
                    {
                        image.PrimaryEncoding = new TiledARGB();
                    }
                    else if (image.Unknown2 == 0xa6 || image.Unknown2 == 0x86)
                    {
                        image.PrimaryEncoding = new DXT1();
                    }
                    else
                    {
                        Console.WriteLine("WARNING: Unknown PS3 encoding 0x" + image.Unknown2.ToString("x"));
                        image.PrimaryEncoding = new NullEncoding();
                    }
                }
                else
                {
                    image.PrimaryEncoding = PixelEncoding.GetEncoding(encoding);
                }
                nameoffsets[image] = reader.Position + reader.ReadUInt32();
                uint offset1 = reader.ReadUInt32();
                uint offset2 = reader.ReadUInt32();

                image.PrimaryData = new Substream(data, offset1, image.PrimaryLength);
                if (offset2 > 0)                 // Best hack I have for determining whether there's a secondary image or not
                {
                    image.SecondaryData = new Substream(data, offset2, image.SecondaryLength);
                }

                Images.Add(image);
            }

            foreach (var offset in nameoffsets)
            {
                stream.Position = offset.Value;
                offset.Key.Name = stream.ReadCString();
            }
        }