private static bool SaveFrameV10( PegFileV10 peg, Peg.FrameV10 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); }
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 != 10) { 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.FrameV10> [textureCount]; int totalFrames = 0; for (int i = 0; i < textures.Length; i++) { textures[i] = new Peg.Texture <Peg.FrameV10>(); var frame = new Peg.FrameV10(); frame.Deserialize(input, endian); textures[i].Frames.Add(frame); totalFrames++; /* some pegs in SR2 seem to have been saved incorrectly? * or there is some sort of flag to mark that data is in * the same file as the header * * see: * ige_regular.peg_pc * ige_small.peg_pc * ige_smallest.peg_pc */ /* * if (i == 0 && * frame.DataOffset != 0) * { * throw new FormatException("strange data offset"); * } */ if (frame.FrameCount == 0) { throw new FormatException("frame count is 0"); } for (int j = 1; j < frame.FrameCount; j++) { var extraFrame = new Peg.FrameV10(); 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); }