using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace A { class Program { static int flush(FileStream output, byte currentX, int counter) { if (counter > 1) { output.WriteByte(currentX); output.WriteByte((byte)counter); } return 1; } static public void compress(String source, String dis) { FileStream input = new FileStream(source, FileMode.Open, FileAccess.Read); FileStream output = new FileStream(dis, FileMode.Create, FileAccess.Write); byte currentX = (byte)input.ReadByte(); output.WriteByte(currentX); int counter = 1; while (input.Position != input.Length) { byte nextX = (byte)input.ReadByte(); if (nextX == currentX) { if (counter++ > 250) { counter = flush(output, currentX, counter); currentX = (byte)input.ReadByte(); output.WriteByte(currentX); } continue; } counter = flush(output, currentX, counter); output.WriteByte(nextX); currentX = nextX; counter = 1; } counter = flush(output, currentX, counter); input.Close(); output.Close(); } static public void decompress(String source, String dis) { FileStream input = new FileStream(source, FileMode.Open, FileAccess.Read); FileStream output = new FileStream(dis, FileMode.Create, FileAccess.Write); byte currentX = (byte)input.ReadByte(); output.WriteByte(currentX); while (input.Position < input.Length) { byte nextX = (byte)input.ReadByte(); if (nextX == currentX) { byte cnt = (byte)input.ReadByte(); for (int i = 2; i <= cnt; i++) output.WriteByte(currentX); currentX = (byte)input.ReadByte(); output.WriteByte(currentX); } else { output.WriteByte(nextX); currentX = nextX; } } } static void Main(string[] args) { compress("input.bmp", "compressed.rle"); decompress("compressed.rle", "inputO.bmp"); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)