protected ShockwaveFlash(FlashReader input) : this(false) { Compression = (CompressionKind)input.ReadString(3)[0]; Version = input.ReadByte(); FileLength = input.ReadUInt32(); switch (Compression) { case CompressionKind.LZMA: { byte[] decompressed = LZMA.Decompress(input.BaseStream, ((int)FileLength - 8)); _input = new FlashReader(decompressed); break; } case CompressionKind.ZLIB: { _input = ZLIB.WrapDecompressor(input.BaseStream); break; } case CompressionKind.None: { _input = input; break; } } Frame = new FrameRecord(_input); }
public void SetImage(Image <Rgba32> image) { // Save image. using var stream = new MemoryStream(); image.Save(stream, new JpegEncoder { Quality = 100 }); Data = stream.ToArray(); Format = ImageFormat.JPEG; // Save alpha channel. var alpha = ArrayPool <byte> .Shared.Rent(image.Width *image.Height); for (var y = 0; y < image.Height; y++) { var offset = image.Width * y; var row = image.GetPixelRowSpan(y); for (var x = 0; x < image.Width; x++) { alpha[offset + x] = row[x].A; } } AlphaData = ZLIB.Compress(alpha); }
protected void Compressor(CompressionMode mode) { switch (mode) { case CompressionMode.Compress: { if (_compressedBitmapData == null) { _compressedBitmapData = ZLIB.Compress(_pixelData); } break; } case CompressionMode.Decompress: { if (_pixelData == null) { _pixelData = ZLIB.Decompress(_compressedBitmapData); } break; } } }
public Image <Rgba32> GetImage(Configuration configuration = null) { var decompressedSize = Width * Height * 4; var decompressedData = ArrayPool <byte> .Shared.Rent(decompressedSize); try { ZLIB.DecompressFast(_zlibData, decompressedData, decompressedSize); var image = new Image <Rgba32>(configuration, Width, Height); switch (Format) { case Format32BitArgb: for (var y = 0; y < image.Height; y++) { var row = image.GetPixelRowSpan(y); for (var x = 0; x < image.Width; x++) { // Alpha values are premultiplied, recover original values. var pixel = y * Width * 4 + x * 4; var alpha = decompressedData[pixel]; if (alpha != 0) { var alphaChange = 255.0d / alpha; row[x] = new Rgba32( (byte)(decompressedData[pixel + 1] * alphaChange), (byte)(decompressedData[pixel + 2] * alphaChange), (byte)(decompressedData[pixel + 3] * alphaChange), alpha); } else { row[x] = new Rgba32( decompressedData[pixel + 1], decompressedData[pixel + 2], decompressedData[pixel + 3], alpha); } } } break; default: image.Dispose(); throw new NotSupportedException($"Unsupported losless format {Format}"); } return(image); } finally { ArrayPool <byte> .Shared.Return(decompressedData); } }
public static Stream Compress(Stream stream, bool isBigEndian = true) { uint decompSize = (uint)stream.Length; uint[] section_sizes; uint sectionCount = 0; var mem = new MemoryStream(); using (var reader = new FileReader(stream, true)) using (var writer = new FileWriter(mem, true)) { writer.SetByteOrder(isBigEndian); if (!(decompSize % 0x10000 != 0)) { sectionCount = decompSize / 0x10000; } else { sectionCount = (decompSize / 0x10000) + 1; } writer.Write(0x10000); writer.Write(sectionCount); writer.Write(decompSize); writer.Write(new uint[sectionCount]); writer.Align(128); reader.SeekBegin(0); section_sizes = new uint[sectionCount]; for (int i = 0; i < sectionCount; i++) { byte[] chunk = ZLIB.Compress(reader.ReadBytes(0x10000)); section_sizes[i] = (uint)chunk.Length; writer.Write(chunk.Length); writer.Write(chunk); writer.Align(128); } writer.SeekBegin(12); for (int i = 0; i < sectionCount; i++) { writer.Write(section_sizes[i] + 4); } } return(mem); }
public virtual void Assemble(FlashWriter output, CompressionKind compression, Action <TagItem> callback) { output.Write(((char)compression) + "WS", true); output.Write(Version); output.Write(uint.MinValue); int fileLength = 8; FlashWriter compressor = null; switch (compression) { case CompressionKind.LZMA: { compressor = new FlashWriter((int)FileLength); break; } case CompressionKind.ZLIB: { compressor = ZLIB.WrapCompressor(output.BaseStream, true); break; } } /* Body Start */ Frame.WriteTo(compressor ?? output); fileLength += (Frame.Area.GetByteSize() + 4); for (int i = 0; i < Tags.Count; i++) { TagItem tag = Tags[i]; callback?.Invoke(tag); WriteTag(tag, compressor ?? output); fileLength += tag.Header.Length; fileLength += (tag.Header.IsLongTag ? 6 : 2); } if (compression == CompressionKind.LZMA) { byte[] uncompressedBody = ((MemoryStream)compressor.BaseStream).ToArray(); byte[] compressedBody = LZMA.Compress(uncompressedBody); output.Write(compressedBody); } compressor?.Dispose(); /* Body End */ output.Position = 4; output.Write((uint)fileLength); output.Position = output.Length; }
protected void Compressor(CompressionMode mode) { switch (mode) { case CompressionMode.Compress: { _compressedData = ZLIB.Compress(_uncompressedData); break; } case CompressionMode.Decompress: { _uncompressedData = ZLIB.Decompress(_compressedData); break; } } }
/// <summary> /// Loads the data for the given entry /// </summary> public byte[] LoadEntry(Entry entry) { lock (Reader) { Reader.BaseStream.Position = entry.Offset; switch (entry.Flags[0] & 0x0F) { case 0: return(Reader.ReadBytes((int)entry.CompressedSize)); case 1: return(ZLIB.Decompress(Reader.ReadBytes((int)entry.CompressedSize), -15)); case 2: return(ZStandard.Decompress(Reader.ReadBytes((int)entry.CompressedSize))); default: throw new Exception("Invalid Entry Compression"); } } }
public Image <Rgba32> GetImage(Configuration configuration = null) { if (Format != ImageFormat.JPEG) { throw new NotSupportedException("Only JPEG is supported for GetImage"); } // Load image. var image = Image.Load <Rgba32>(configuration, Data); // Apply alpha channel. var alphaSize = image.Width * image.Height; var alpha = ArrayPool <byte> .Shared.Rent(alphaSize); try { ZLIB.DecompressFast(AlphaData, alpha, alphaSize); for (var y = 0; y < image.Height; y++) { var offset = image.Width * y; var row = image.GetPixelRowSpan(y); for (var x = 0; x < image.Width; x++) { row[x].A = alpha[offset + x]; } } return(image); } finally { ArrayPool <byte> .Shared.Return(alpha); } }
public unsafe static Map ReadMap3(FileInfo fi) { byte[] m3s = new byte[] { (byte)'V', (byte)'3', (byte)'M', (byte)'A', (byte)'P', (byte)0 }; FileStream fs = fi.OpenRead(); BinaryReader br = new BinaryReader(fs); Map map = new Map(); map.FileOnDisk = fi; // Directory.SetCurrentDirectory(map.FileOnDisk.Directory.FullName); byte[] sig = br.ReadBytes(m3s.Length); for (int i = 0; i < m3s.Length; i++) { if (sig[i] != m3s[i]) { return(null); } } int version = br.ReadInt32(); int vcofs = br.ReadInt32(); byte[] formal = br.ReadBytes(256); byte[] vspname = br.ReadBytes(256); byte[] musicname = br.ReadBytes(256); byte[] renderstring = br.ReadBytes(256); byte[] aexec = br.ReadBytes(256); string vspf = Helper.BytesToString(vspname); FileInfo vspfile; try { vspfile = new FileInfo(vspf); } catch (ArgumentException) { throw new Exception("VSP file is inaccessible. Requested file was '" + vspf + "'"); } if (!vspfile.Exists) { System.Windows.Forms.DialogResult dr = System.Windows.Forms.MessageBox.Show("Unable to load requested VSP file. Create a blank vsp?", "Load Error", System.Windows.Forms.MessageBoxButtons.YesNoCancel, System.Windows.Forms.MessageBoxIcon.Information); if (dr == System.Windows.Forms.DialogResult.OK) { map.vsp = new Vsp24(); } else if (dr == System.Windows.Forms.DialogResult.Cancel) { return(null); } else { return(null); } } else { map.vsp = ReadVsp(vspfile.FullName); } map.FormalName = Helper.BytesToString(formal); map.MusicFileName = Helper.BytesToString(musicname); //map.RenderString = Helper.BytesToString(renderstring); string rs = Helper.BytesToString(renderstring); map.AutoExecEvent = Helper.BytesToString(aexec); map.PlayerStartX = br.ReadInt16(); map.PlayerStartY = br.ReadInt16(); int layercount = br.ReadInt32(); for (int i = 0; i < layercount; i++) { MapLayer ml = new MapLayer(map); ml.name = Helper.BytesToString(br.ReadBytes(256)); ml.type = LayerType.Tile; ml.parallaxInfo = new ParallaxInfo(); ml.parallaxInfo.MultipleX = br.ReadDouble(); ml.parallaxInfo.MultipleY = br.ReadDouble(); int w = br.ReadInt16(); int h = br.ReadInt16(); ml.size(w, h); ml.Translucency = br.ReadByte(); int len = br.ReadInt32(); int zlen = br.ReadInt32(); ml.Data = ZLIB.DecodeShorts(br.ReadBytes(zlen), len); map.Layers.Add(ml); } MapLayer ol = new MapLayer(map); ol.type = LayerType.Obs; ol.name = "Obstructions"; ol.size(((MapLayer)map.Layers[0]).Width, ((MapLayer)map.Layers[0]).Height); int ol_lenn = br.ReadInt32(); int ol_len = br.ReadInt32(); byte[] obsdata = ZLIB.Decode(br.ReadBytes(ol_len), ol_lenn); for (int i = 0; i < obsdata.Length; i++) { ol.Data[i] = obsdata[i]; } map.ObsLayer = ol; int zl_lenn = br.ReadInt32(); int zl_len = br.ReadInt32(); MapLayer zl = new MapLayer(map); zl.type = LayerType.Zone; zl.name = "Zones"; zl.size(((MapLayer)map.Layers[0]).Width, ((MapLayer)map.Layers[0]).Height); zl.Data = ZLIB.DecodeShorts(br.ReadBytes(zl_len), zl_lenn); map.ZoneLayer = zl; MapLayer el = new MapLayer(map); el.type = LayerType.Entity; el.name = "Entities"; map.EntLayer = el; map.Layers.Add(el); MapLayerSpecial rl = new MapLayerSpecial(map); rl.type = LayerType.Special_Retrace; map.Layers.Add(rl); map.Layers.Add(ol); map.Layers.Add(zl); int zonecount = br.ReadInt32(); for (int i = 0; i < zonecount; i++) { MapZone mz = new MapZone(); byte[] zname = br.ReadBytes(256); byte[] pscript = br.ReadBytes(256); // byte[] escript = br.ReadBytes(256); mz.Name = Helper.BytesToString(zname); mz.PlayerScript = Helper.BytesToString(pscript); // mz.EntityScript = Helper.BytesToString(escript); mz.Rate = br.ReadByte(); mz.Delay = br.ReadByte(); mz.AdjAct = br.ReadByte(); mz.ID = i; map.Zones.Add(mz); } if (zonecount == 0) { MapZone mz = new MapZone(); mz.ID = 0; mz.Name = "NULL_ZONE"; map.Zones.Add(mz); } int entcount = br.ReadInt32(); for (int i = 0; i < entcount; i++) { MapEntity me = new MapEntity(); me.TileX = br.ReadInt16(); me.TileY = br.ReadInt16(); me.Facing = br.ReadByte(); me.ObeyObstruction = br.ReadByte(); me.IsObstruction = br.ReadByte(); me.AutoFace = br.ReadByte(); me.Speed = br.ReadInt16(); me.ActivationMode = br.ReadByte(); me.MoveType = br.ReadByte(); me.WanderRectangle.x0 = br.ReadInt16(); me.WanderRectangle.y0 = br.ReadInt16(); me.WanderRectangle.x1 = br.ReadInt16(); me.WanderRectangle.y1 = br.ReadInt16(); me.WanderDelay = br.ReadInt16(); int expand = br.ReadInt32(); me.MoveScript = Helper.BytesToString(br.ReadBytes(256)); me.ChrName = Helper.BytesToString(br.ReadBytes(256)); me.Description = Helper.BytesToString(br.ReadBytes(256)); me.onActivate = Helper.BytesToString(br.ReadBytes(256)); me.ID = map.Entities.Add(me); } br.Close(); string rs2 = ""; if (version == 1) { foreach (char c in rs) { if (rs2.Length != 0) { rs2 += ","; } rs2 += c; } map.RenderString = rs2; } else { map.RenderString = rs; } return(map); }
public unsafe static int WriteMap(FileInfo fi, Map map) { byte[] map3_signature = new byte[] { (byte)'V', (byte)'3', (byte)'M', (byte)'A', (byte)'P', (byte)0 }; if (map == null) { return(-1); } string vspname = Helper.GetRelativePath(map.FileOnDisk.Directory.FullName, map.vsp.FileOnDisk.FullName); if (vspname == null) { throw new Exception("Unable to resolve VSP path."); } fi = new FileInfo(fi.Name); if (fi.Exists) { fi.Delete(); } FileStream fs = fi.OpenWrite(); MemoryStream ms_outbuf = new MemoryStream(); BinaryWriter bw_o = new BinaryWriter(ms_outbuf); MemoryStream ms_outbuf2 = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms_outbuf2); bw_o.Write(map3_signature); bw_o.Write(Global.VERSIONINFO.MAPVERSION); bw.Write(Helper.StringToPaddedByteArray(map.FormalName, 256)); bw.Write(Helper.StringToPaddedByteArray(vspname, 256)); bw.Write(Helper.StringToPaddedByteArray(map.MusicFileName, 256)); bw.Write(Helper.StringToPaddedByteArray(map.RenderString, 256)); bw.Write(Helper.StringToPaddedByteArray(map.AutoExecEvent, 256)); bw.Write((short)map.PlayerStartX); bw.Write((short)map.PlayerStartY); int special_count = 0; foreach (MapLayer ml in map.Layers) { if (ml.type != LayerType.Tile) { special_count++; } } bw.Write(map.Layers.Count - special_count); foreach (MapLayer ml in map.Layers) { if (ml.type != LayerType.Tile) { continue; } bw.Write(Helper.StringToPaddedByteArray(ml.name, 256)); bw.Write(ml.parallaxInfo.MultipleX); bw.Write(ml.parallaxInfo.MultipleY); bw.Write((short)ml.Width); bw.Write((short)ml.Height); bw.Write((byte)ml.Translucency); fixed(short *ptr = ml.Data) { byte[] zdata = ZLIB.Encode((byte *)ptr, ml.Data.Length * 2); bw.Write(ml.Data.Length * 2); bw.Write(zdata.Length); bw.Write(zdata); } } MapLayer zl = map.ZoneLayer, ol = map.ObsLayer; byte[] obsdata = new byte[ol.Data.Length]; for (int j = 0; j < ol.Data.Length; j++) { obsdata[j] = (byte)ol.Data[j]; fixed(byte *ptr = obsdata) { byte[] zdata = ZLIB.Encode(ptr, obsdata.Length); bw.Write(obsdata.Length); bw.Write(zdata.Length); bw.Write(zdata); } fixed(short *ptr = zl.Data) { byte[] zdata = ZLIB.Encode((byte *)ptr, zl.Data.Length * 2); bw.Write(zl.Data.Length * 2); bw.Write(zdata.Length); bw.Write(zdata); } bw.Write(map.Zones.Count); foreach (MapZone mz in map.Zones) { bw.Write(Helper.StringToPaddedByteArray(mz.Name, 256)); bw.Write(Helper.StringToPaddedByteArray(mz.PlayerScript, 256)); // bw.Write(Helper.StringToPaddedByteArray(mz.EntityScript,256)); bw.Write((byte)mz.Rate); bw.Write((byte)mz.Delay); bw.Write((byte)mz.AdjAct); } bw.Write(map.Entities.Count); foreach (MapEntity me in map.Entities) { bw.Write((short)me.TileX); bw.Write((short)me.TileY); bw.Write((byte)me.Facing); bw.Write((byte)me.ObeyObstruction); bw.Write((byte)me.IsObstruction); bw.Write((byte)me.AutoFace); bw.Write((short)me.Speed); bw.Write((byte)me.ActivationMode); bw.Write((byte)me.MoveType); bw.Write((short)me.WanderRectangle.x0); bw.Write((short)me.WanderRectangle.y0); bw.Write((short)me.WanderRectangle.x1); bw.Write((short)me.WanderRectangle.y1); bw.Write((short)me.WanderDelay); bw.Write((int)0);//expand bw.Write(Helper.StringToPaddedByteArray(me.MoveScript, 256)); bw.Write(Helper.StringToPaddedByteArray(me.ChrName, 256)); bw.Write(Helper.StringToPaddedByteArray(me.Description, 256)); bw.Write(Helper.StringToPaddedByteArray(me.onActivate, 256)); } bw_o.Write((int)14 + (int)ms_outbuf2.Length); bw.Close(); bw_o.Close(); bw = new BinaryWriter(fs); bw.Write(ms_outbuf.ToArray()); bw.Write(ms_outbuf2.ToArray()); //write number of compiled vc functions bw.Write((int)0); bw.Close(); return(0); }
public unsafe static int WriteVsp(FileInfo fi, Vsp24 vsp) { if (fi.Exists) { fi.Delete(); } FileStream fs = fi.OpenWrite(); BinaryWriter bw = new BinaryWriter(fs); vsp.FileOnDisk = fi; bw.Write(VSP_SIGNATURE); bw.Write(VSP_VERSION); bw.Write(VSP_TILESIZE); bw.Write(VSP_FORMAT); bw.Write(vsp.Tiles.Count); bw.Write(VSP_COMPRESSION); // build byte array of all tiles in our vsp MemoryStream ms_tiles = new MemoryStream(); BinaryWriter bw_tiles = new BinaryWriter(ms_tiles); foreach (Vsp24Tile tile in vsp.Tiles) { for (int y = 0; y < 16; y++) { for (int x = 0; x < 16; x++) { int p = tile.Image.GetPixel(x, y); bw_tiles.Write((byte)((p & 0x00FF0000) >> 16)); bw_tiles.Write((byte)((p & 0x0000FF00) >> 8)); bw_tiles.Write((byte)((p & 0x000000FF))); } } } bw_tiles.Close(); byte[] zdata = ZLIB.Encode(ms_tiles.ToArray()); bw.Write((int)16 * 16 * 3 * vsp.Tiles.Count); bw.Write(zdata.Length); bw.Write(zdata); bw.Write(vsp.Animations.Count); foreach (VspAnimation va in vsp.Animations) { bw.Write(Helper.StringToPaddedByteArray(va.Name, 256)); bw.Write(va.Start); bw.Write(va.End); bw.Write(va.Delay); bw.Write(va.Mode); } bw.Write(vsp.ObstructionTiles.Count); byte[] odata = new byte[vsp.ObstructionTiles.Count * 256]; for (int i = 0; i < vsp.ObstructionTiles.Count; i++) { int[] pixels = ((VspObstructionTile)vsp.ObstructionTiles[i]).Image.GetArray(); for (int j = 0; j < 256; j++) { odata[i * 256 + j] = (byte)pixels[j]; } } byte[] ozdata = ZLIB.Encode(odata); bw.Write((int)odata.Length); bw.Write((int)ozdata.Length); bw.Write(ozdata); Global.pluginManager.tilesetSave(fs); bw.Close(); return(0); }
public unsafe static Vsp24 ReadVsp24(FileInfo fi) { //Errors.Error("loading vsp"); Vsp24 vsp = new Vsp24(); vsp.FileOnDisk = fi; FileStream fs = fi.OpenRead(); BinaryReader br = new BinaryReader(fs); int sig = br.ReadInt32(); int version = br.ReadInt32(); if (sig != VSP_SIGNATURE || version != VSP_VERSION) { return(null); } // assume 16 int tilesize = br.ReadInt32(); // assume 24-bit int format = br.ReadInt32(); int tilecount = br.ReadInt32(); // assume zlib int compression = br.ReadInt32(); int datalen = br.ReadInt32(); int zdatalen = br.ReadInt32(); byte[] zdata = br.ReadBytes(zdatalen); byte[] data = ZLIB.Decode(zdata, datalen); for (int tile = 0; tile < tilecount; tile++) { int[] px = new int[256]; for (int i = 0; i < 256; i++) { int idx = tile * 256 + i; int c = unchecked ((int)0xFF000000); c = c | (data[idx * 3] << 16); c = c | (data[idx * 3 + 1] << 8); c = c | data[idx * 3 + 2]; px[i] = c; } vsp.Tiles.Add(new Vsp24Tile(vsp, new pr2.BufferImage(16, 16, px))); } int animcount = br.ReadInt32(); for (int i = 0; i < animcount; i++) { VspAnimation va = new VspAnimation(); va.Name = Helper.BytesToString(br.ReadBytes(256)); va.Start = br.ReadInt32(); va.End = br.ReadInt32(); va.Delay = br.ReadInt32(); va.Mode = br.ReadInt32(); va.ID = i; vsp.Animations.Add(va); } int obscount = br.ReadInt32(); int od_len = br.ReadInt32(); int od_zlen = br.ReadInt32(); byte[] zd = br.ReadBytes(od_zlen); byte[] od = ZLIB.Decode(zd, od_len); for (int i = 0; i < obscount; i++) { int[] tile = new int[256]; for (int j = 0; j < 256; j++) { tile[j] = od[i * 256 + j]; } vsp.ObstructionTiles.Add(new VspObstructionTile(vsp, tile)); } br.Close(); return(vsp); }
/// <summary> /// Loads entries from a binary file /// </summary> /// <param name="fileName">File to load from</param> public void LoadBinary(string fileName) { try { using (var fileReader = new BinaryReader(File.OpenRead(fileName))) { var magic = fileReader.ReadUInt64(); var compression = fileReader.ReadUInt64(); var compressedSize = fileReader.ReadInt64(); var decompressedSize = fileReader.ReadInt64(); byte[] buffer = null; switch (compression) { case 0: buffer = fileReader.ReadBytes((int)compressedSize); break; case 1: buffer = ZLIB.Decompress(fileReader.ReadBytes((int)compressedSize)); break; case 2: buffer = ZStandard.Decompress(fileReader.ReadBytes((int)compressedSize)); break; case 3: buffer = LZ4.Decompress(fileReader.ReadBytes((int)compressedSize), (int)decompressedSize); break; default: throw new Exception("Invalid cache compression"); } using (var reader = new BinaryReader(new MemoryStream(buffer))) { var count = (int)reader.ReadUInt64(); switch (magic) { // 32Bit Cache case 0x3130454843414354: { for (int i = 0; i < count; i++) { Entries[reader.ReadUInt32()] = reader.ReadNullTerminatedString(); } break; } // 64Bit Cache case 0x3230454843414354: { for (int i = 0; i < count; i++) { Entries[reader.ReadUInt64()] = reader.ReadNullTerminatedString(); } break; } } } } } #if DEBUG catch (Exception e) { Console.WriteLine("Failed to load {0}: {1}", fileName, e); } #else catch { } #endif }