public ShpD2Frame(Stream s) { var flags = (FormatFlags)s.ReadUInt16(); s.Position += 1; var width = s.ReadUInt16(); var height = s.ReadUInt8(); Size = new Size(width, height); // Subtract header size var dataLeft = s.ReadUInt16() - 10; var dataSize = s.ReadUInt16(); byte[] table; if ((flags & FormatFlags.PaletteTable) != 0) { var n = (flags & FormatFlags.VariableLengthTable) != 0 ? s.ReadUInt8() : (byte)16; table = new byte[n]; for (var i = 0; i < n; i++) { table[i] = s.ReadUInt8(); } dataLeft -= n; } else { table = new byte[256]; for (var i = 0; i < 256; i++) { table[i] = (byte)i; } table[1] = 0x7f; table[2] = 0x7e; table[3] = 0x7d; table[4] = 0x7c; } Data = new byte[width * height]; // Decode image data var compressed = s.ReadBytes(dataLeft); if ((flags & FormatFlags.SkipFormat80) == 0) { var temp = new byte[dataSize]; Format80.DecodeInto(compressed, temp); compressed = temp; } Format2.DecodeInto(compressed, Data, 0); // Lookup values in lookup table for (var j = 0; j < Data.Length; j++) { Data[j] = table[Data[j]]; } }
static MemoryStream ReadPackedSection(IniSection mapPackSection) { StringBuilder sb = new StringBuilder(); for (int i = 1; ; i++) { string line = mapPackSection.GetValue(i.ToString(), null); if (line == null) { break; } sb.Append(line.Trim()); } byte[] data = Convert.FromBase64String(sb.ToString()); List <byte[]> chunks = new List <byte[]>(); BinaryReader reader = new BinaryReader(new MemoryStream(data)); try { while (true) { uint length = reader.ReadUInt32() & 0xdfffffff; byte[] dest = new byte[8192]; byte[] src = reader.ReadBytes((int)length); /*int actualLength =*/ Format80.DecodeInto(src, dest); chunks.Add(dest); } } catch (EndOfStreamException) { } MemoryStream ms = new MemoryStream(); foreach (byte[] chunk in chunks) { ms.Write(chunk, 0, chunk.Length); } ms.Position = 0; return(ms); }
void Decompress(ImageHeader h) { // No extra work is required for empty frames if (h.Size.Width == 0 || h.Size.Height == 0) { return; } if (recurseDepth > imageCount) { throw new InvalidDataException("Format20/40 headers contain infinite loop"); } switch (h.Format) { case Format.Format20: case Format.Format40: { if (h.RefImage.Data == null) { ++recurseDepth; Decompress(h.RefImage); --recurseDepth; } h.Data = CopyImageData(h.RefImage.Data); Format40.DecodeInto(shpBytes, h.Data, (int)(h.FileOffset - shpBytesFileOffset)); break; } case Format.Format80: { var imageBytes = new byte[Size.Width * Size.Height]; Format80.DecodeInto(shpBytes, imageBytes, (int)(h.FileOffset - shpBytesFileOffset)); h.Data = imageBytes; break; } default: throw new InvalidDataException(); } }