// Get PX0 file image as a byte array. If the image contains compressed data, // decompress it now, so returned bytes always reflect uncompressed state. private static byte[] GetPX0ImageBytes(string fname) { const int teleGetHdrBytes = 22; using (var binReader = new BinaryReader(File.Open(fname, FileMode.Open))) { // Read first 'TeleGetHdrBytes' bytes. These are never compressed. byte[] firstBytes; try { firstBytes = binReader.ReadBytes(teleGetHdrBytes); } catch { throw new Exception(string.Format("'{0}' NOT a TeleGet file! (Missing header?)", fname)); } byte[] buf = null; if (firstBytes[0] == 't' && firstBytes[1] == 'd') { // If 1st 2 bytes are lower case 'td' then data following // header has been compressed. Decompress now. buf = Decompress.GetDecompressedBytes(binReader); } else if (firstBytes[0] == 'T' && firstBytes[1] == 'D') { // If 1st 2 bytes are upper case 'TD' then data following // header has NOT compressed. Just read it. var buffer = new byte[16 * 1024]; using (var ms = new MemoryStream()) { int read; while ((read = binReader.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } buf = ms.ToArray(); } } else { throw new Exception(string.Format("'{0}' NOT a TeleGet file!", fname)); } if (null == buf || buf.Length < 1) { return(null); // No data after header? } var decodedData = new byte[buf.Length + firstBytes.Length]; firstBytes.CopyTo(decodedData, 0); buf.CopyTo(decodedData, firstBytes.Length); return(decodedData); } }
/* * Get decompressed bytes. Note that this is not implemented with a view for speed. */ public static byte[] GetDecompressedBytes(BinaryReader reader) { using (var dcomp = new Decompress(reader)) using (var memStream = new MemoryStream()) { for (;;) { int c = dcomp.Getbyte(); if (c < 0) { break; // EOF found! } memStream.WriteByte((byte)c); } return(memStream.ToArray()); } }