Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to account for the header bmp in my program?

C#
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");
        }
    }
}
Posted
Updated 26-Mar-15 3:45am
v2
Comments
ZurdoDev 26-Mar-15 9:42am    
What do you mean?
Member 11489430 26-Mar-15 9:52am    
My programm do RLE, but I need to accout that it is BMP file. Structure of BMP.
Richard MacCutchan 26-Mar-15 10:08am    
Google will find you details on the bitmap structure.
Joan Magnet 26-Mar-15 9:59am    
Account == Keep in mind?
Member 11489430 26-Mar-15 10:03am    
Sorry for my English, Account=consider=keep in mind

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900