public static void saveCompressedFile(byte[] izlaz, string fileName, Bitmap bitmap) { StreamWriter sw = new StreamWriter(fileName); sw.WriteLine(bitmap.Width); sw.WriteLine(bitmap.Height); byte[] outByteArray = new byte[izlaz.Length]; int len = ShannonFano.Compress(izlaz, outByteArray, (uint)izlaz.Length); sw.WriteLine(len); sw.Close(); FileStream fs = new FileStream(fileName, FileMode.Append); fs.Write(outByteArray, 0, len); fs.Close(); }
//opening compressed files public static Bitmap openCompressedFile() { OpenFileDialog o = new OpenFileDialog(); o.Filter = "Compressed files(*.r, *.g, *.b)|*.r;*.g;*.b"; if (DialogResult.OK == o.ShowDialog() && o.FileName != null) { StreamReader sr = new StreamReader(o.FileName); string channel = Path.GetExtension(o.FileName); int w = Int32.Parse(sr.ReadLine()); int h = Int32.Parse(sr.ReadLine()); int len = Int32.Parse(sr.ReadLine()); sr.Close(); FileStream fs = File.OpenRead(o.FileName); fs.Seek(3 * sizeof(int) + 5, SeekOrigin.Begin); byte[] inA = new byte[len]; fs.Read(inA, 0, len); fs.Close(); byte[] byteArray = new byte[w * h * 2 + ((w % 2 == 1) ? 2 * h : 0)]; ShannonFano.Decompress(inA, byteArray, (uint)len, (uint)(w * h * 2 + ((w % 2 == 1) ? 2 * h : 0))); Bitmap outB = new Bitmap(w, h); switch (channel) { case ".r": outB = openR(w, h, byteArray); break; case ".g": outB = openG(w, h, byteArray); break; case ".b": outB = openB(w, h, byteArray); break; } return((Bitmap)outB.Clone()); } else { return(null); } }