Exemple #1
0
        private static bool SaveFrameV16(
            PegFileV13 peg,
            Peg.FrameV13 frame,
            byte[] buffer,
            string basePath,
            out string finalPath,
            out string type)
        {
            finalPath = Path.ChangeExtension(basePath, ".png");
            type      = "png";

            switch (frame.Format)
            {
            case Peg.PixelFormat.A8R8G8B8:
            {
                var bitmap = ImageHelper.ExportA8R8G8B8(frame.Width, frame.Height, buffer);
                bitmap.Save(finalPath, System.Drawing.Imaging.ImageFormat.Png);
                return(true);
            }

            case Peg.PixelFormat.DXT1:
            case Peg.PixelFormat.DXT3:
            case Peg.PixelFormat.DXT5:
            {
                var bitmap = ImageHelper.ExportDXT(frame.Format, frame.Width, frame.Height, buffer);
                bitmap.Save(finalPath, System.Drawing.Imaging.ImageFormat.Png);
                return(true);
            }
            }

            return(false);
        }
Exemple #2
0
        public void Deserialize(Stream input)
        {
            var baseOffset = input.Position;

            var magic = input.ReadValueU32(Endian.Little);

            if (magic != Signature && magic.Swap() != Signature)
            {
                throw new FormatException("not a peg file");
            }

            var endian = magic == Signature ? Endian.Little : Endian.Big;

            var version = input.ReadValueU16(endian);

            if (version != 13)
            {
                throw new FormatException("unsupported peg version");
            }

            var platform = input.ReadValueU16(endian);

            if (Enum.IsDefined(typeof(Platform), platform) == false)
            {
                throw new FormatException("unsupported peg platform");
            }

            var headerSize = input.ReadValueU32(endian);

            if (baseOffset + headerSize > input.Length)
            {
                throw new EndOfStreamException();
            }

            var dataSize     = input.ReadValueU32(endian);
            var textureCount = input.ReadValueU16(endian);
            var unknown12    = input.ReadValueU16(endian);
            var frameCount   = input.ReadValueU16(endian);
            var unknown16    = input.ReadValueU8();
            var unknown17    = input.ReadValueU8();

            if (unknown12 != 0 ||
                unknown16 != 16 ||
                unknown17 != 0)
            {
                throw new FormatException("unexpected unknown values");
            }

            var textures    = new Peg.Texture <Peg.FrameV13> [textureCount];
            int totalFrames = 0;

            for (int i = 0; i < textures.Length; i++)
            {
                textures[i] = new Peg.Texture <Peg.FrameV13>();

                var frame = new Peg.FrameV13();
                frame.Deserialize(input, endian);
                textures[i].Frames.Add(frame);
                totalFrames++;

                if (frame.FrameCount == 0)
                {
                    throw new FormatException("frame count is 0");
                }

                for (int j = 1; j < frame.FrameCount; j++)
                {
                    var extraFrame = new Peg.FrameV13();
                    extraFrame.Deserialize(input, endian);

                    if (extraFrame.FrameCount != 1)
                    {
                        throw new FormatException("frame count is not 1");
                    }

                    textures[i].Frames.Add(extraFrame);
                    totalFrames++;
                }
            }

            if (totalFrames != frameCount)
            {
                throw new FormatException("did not read correct amount of frames");
            }

            foreach (var texture in textures)
            {
                texture.Name = input.ReadStringZ(Encoding.ASCII);
            }

            this._Endian   = endian;
            this._Version  = version;
            this._Platform = (Platform)platform;
            this._DataSize = dataSize;
            this._Textures.Clear();
            this._Textures.AddRange(textures);
        }