Алгоритм RLE не сжимает, а увеличивает изображение - C#

Узнай цену своей работы

Формулировка задачи:

Доброго времени суток! Разбираю алгоритм RLE для сжатия растровых изображений. По своей природе он является самым простым, однако возникли трудности. Он работает..вот только не сжимает, а наоборот увеличивается размер исходного .bmp изображения. В чем проблема?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace RLE
{
    class Program
    {
 
        static public void compress(String source, String dis) {
            try
            {
 
                FileStream input = new FileStream(source, FileMode.Open, FileAccess.Read);
 
                int next = 0;
                int numBytesRead = 0;
                byte[] bytes = new byte[input.Length];
                int numBytesToRead = (int)input.Length;
                int count = input.Read(bytes, numBytesRead, numBytesToRead);
 
                while (numBytesToRead > 0)
                {
                    int n = input.Read(bytes, numBytesRead, numBytesToRead);
                    if (n == 0)
                        break;
 
                    numBytesRead += n;
                    numBytesToRead -= n;
                }
                numBytesToRead = bytes.Length;
 
                FileStream output = new FileStream(dis, FileMode.Create, FileAccess.Write);
                output.Write(bytes, 0, numBytesToRead);

                while ((next = input.Read(bytes, numBytesRead, numBytesToRead)) >= 0)
                {
                    int counter = 1;
                    if (count == next)
                    {
                        counter++;
                        while (next == (count = input.Read(bytes, numBytesRead, numBytesToRead)))
                        {
                            counter++;
                        }
                        while (counter >= 63)
                        {
                            output.Write(bytes, 0, 255);
                            output.Write(bytes, 0, next);
                            counter -= 63;
                        }
                        if (counter > 1)
                        {
                            output.Write(bytes, 0, 0xc0 + counter);
                            output.Write(bytes, 0, next);
                        }
                    }
                    else
                    {
                        if (count <= 0xc0)
                        {
                            output.Write(bytes, 0, count);
                            count = next;
                        }
                        else
                        {
                            output.Write(bytes, 0, 0xc1);
                            output.Write(bytes, 0, count);
                            count = next;
                        }
                    }
                }
                if (count <= 0xc0)
                {
                    output.Write(bytes, 0, count);
                }
                else
                {
                    output.Write(bytes, 0, 0xc1);
                    output.Write(bytes, 0, count);
                }
                input.Close();
                output.Close();
            }
            catch (IOException e)
            {
            }
    }
 
        static public void decompress(String source, String dis)
        {
            try
            {
 
                FileStream input = new FileStream(source, FileMode.Open, FileAccess.Read);
                byte[] bytes = new byte[input.Length];
                int numBytesToRead = (int)input.Length;
                FileStream output = new FileStream(dis, FileMode.Create, FileAccess.Write);
                output.Write(bytes, 0, numBytesToRead);
 
                int count = 0;
                int numBytesRead = 0;
                while ((count = input.Read(bytes, numBytesRead, numBytesToRead)) >= 0)
                {
                    if (count == 0xc1)
                    {
                        output.Write(bytes, 0, input.Read(bytes, numBytesRead, numBytesToRead));
                    }
                    else if (count <= 0xc0)
                    {
                        output.Write(bytes, 0, count);
                    }
                    else if (count > 0xc1)
                    {
                        int next = input.Read(bytes, numBytesRead, numBytesToRead);
                        for (int i = 0; i < (count - 0xc0); i++)
                        {
                            output.Write(bytes, 0, next);
                        }
                    }
                }
            }
            catch (Exception e)
            {
            }
        }
    
        static void Main(string[] args)
        {
            compress("input.bmp", "compressed.rle");
            decompress("compressed.rle", "output.bmp");
        }
    }
}

Решение задачи: «Алгоритм RLE не сжимает, а увеличивает изображение»

textual
Листинг программы
Initialization(...);
do {
    byte = ImageFile.ReadNextByte();
    if(является счетчиком(byte)) {
        counter = Low6bits(byte)+1;
        value = ImageFile.ReadNextByte();
        for(i=1 to counter)
            DecompressedFile.WriteByte(value)
    }
    else {
    DecompressedFile.WriteByte(byte)
} while(ImageFile.EOF());

Оцени полезность:

7   голосов , оценка 3.571 из 5
Похожие ответы