0
votes

I'm dealing with CIP3 files - they're files with mixed content (binary\text). I've got a file that its image data is compressed with RunLengthDecode filter (from PostScript) and encoded with ASCIIHexDecode.

ASCIIHexDecode its okay (just convert binary to hex), but i can't find a decent RLE Decompression Algorithm. I've searched on google a lot, StackOverflow, everywhere - before asking. Can anyone give me any input on this subject ?

Until now i've tried to decompress by using this algorithm:

public enum EncodingFormat
{
    Old,

    New,
}

public static class Rle
{
    public const char EOF = '\u007F';
    public const char ESCAPE = '\\';
    private static bool HasChar(StringBuilder input)
    {
        for (var i = 0; i < input.Length; i++)
        {
            if (char.IsLetter(input[i]))
            {
                return true;
            }
        }

        return false;
    }


    internal static void Decode(string input)
    {
        int fileCounter = 0;            
        //var runLengthEncodedString = new StringBuilder();
        var baseString = input;

        var radix = 0;

        for (var i = 0; i < baseString.Length; i++)
        {
            if (char.IsNumber(baseString[i]))
            {
                radix++;
            }
            else
            {
                if (radix > 0)
                {
                    try
                    {

                        var valueRepeat = Convert.ToInt64(baseString.Substring(i - radix, radix));

                        for (var j = 0; j < valueRepeat; j++)
                        {
                            File.AppendAllText(@"C:\test\hexdecoded.txt",baseString[i].ToString());

                        }

                        radix = 0;
                    }
                    catch(Exception e)
                    {
                        var checkI = i;

                    }


                }
                else if (radix == 0)
                {
                    File.AppendAllText(@"C:\test\hexdecoded.txt", baseString[i].ToString());

                }
            }
        }



    }

    internal static double GetPercentage(double x, double y)
    {
        return (100 * (x - y)) / x;
    }
}

Must say that i've found this code on StackOverflow but can't remember where now. I changed to output the contents to the disk because i was getting a memory exception error. But it takes years to decompress my image (waited for one hour and still decompressing). If i manage to correct decompress this RLE image, i can convert it into bytes and then create a proper TIFF file.

I've uploaded a sample of the file i'm using for tests: CIP3 File Sample

Any input is appreciated. Thanks.

1
Why did you delete you most recent question? It was a pretty good one. - Enigmativity
@Enigmativity I couldn’t format it to suit the format used on stackoverflow (an code someone can copy:paste and compile). Since I don’t want to get downvoted i preferred to delete and try to find the solution myself - paboobhzx
It's usually not hard to get the code to be a minimal reproducible example. Just open a new project, drop your code in and then fix the syntax. You just need to let us know what libraries, NuGet, etc, that you're using. If you don't do it then I sit there trying to reconstruct your code and that's tedious. I'd rather you do it so that I can answer 10 questions instead of spending hours on one. It gives you the best chance of an exceptional answer. - Enigmativity

1 Answers

1
votes

From the PostScript Language Reference, third edition:

The RunLengthEncode filter encodes data in a simple byte-oriented format based on run length. The compressed data format is a sequence of runs, where each run consists of a length byte followed by 1 to 128 bytes of data. If the length byte is in the range 0 to 127, the following length + 1 bytes (1 to 128 bytes) are to be copied literally upon decompression. If length is in the range 129 to 255, the following single byte is to be replicated 257 − length times (2 to 128 times) upon decompression.

the system of length-bytes where the highest bit determines whether to perform a copy or repeat command would be what is commonly referred to as "code-based RLE". More information on RLE, including explanation on the code-based type, can be found here:

http://www.shikadi.net/moddingwiki/RLE_Compression