/// <summary> /// When a segment's data is compressed, it follows the following run-length algorithm: /// If a byte of data has a value of (int)1 thru (int) 127, then the following x bytes are uncompressed. /// If a byte of data has a value of (int)129 thru (int) 255, then the following byte is repeated 127 less times. /// </summary> internal override void ParseSegment(System.IO.FileStream file) { Length = file.Read4ByteInt(); RawDataFileOffset = (int)file.Position; List<byte> bytes = new List<byte>(); while (file.Position < RawDataFileOffset + Length) { byte b = (byte)file.ReadByte(); if (b > 0 && b < 128) { for (int i = 0; i < b; ++i) { bytes.Add((byte)file.ReadByte()); } } else if (b > 128) { byte nextByte = (byte)file.ReadByte(); for (int i = 0; i < b - 127; ++i) { bytes.Add(nextByte); } } else { throw new Exception("Bad compressed data byte encountered at file offset " + file.Position); } } UncompressedLength = bytes.Count; Data = bytes.ToArray(); }
internal override void ParseSection(System.IO.FileStream file) { // Call base parser to populate RawData, then we can parse the structures Length = file.Read4ByteInt(); RawDataFileOffset = (int)file.Position; List<byte> bytes = new List<byte>(); while (file.Position < RawDataFileOffset + Length) { // Read a byte, n. If it's < 128, read the next n bytes into the buffer. // If it's > 128, subtract 127 and then read the next byte and duplicate n - 127 times byte b = (byte)file.ReadByte(); if (b > 0 && b < 128) { //byte nextByte = (byte)file.ReadByte(); for (int i = 0; i < b; ++i) { bytes.Add((byte)file.ReadByte()); } } else if (b > 128) { byte nextByte = (byte)file.ReadByte(); for (int i = 0; i < b - 127; ++i) { bytes.Add(nextByte); } } else { throw new Exception("Bad run-length encoding"); } } RawData = bytes.ToArray(); }
internal override void ParseSection(System.IO.FileStream file) { Length = file.Read4ByteInt(); RawDataFileOffset = (int)file.Position; List<AltitudeDescriptor> data = new List<AltitudeDescriptor>(); while (file.Position < RawDataFileOffset + Length) { data.Add(new AltitudeDescriptor(file)); } AltitudeData = new AltitudeDescriptor[128,128]; for (int i = 0; i < 128; i++) { for (int j = 0; j < 128; j++) { AltitudeData[i, 127 - j] = data[i * 128 + j]; } } }