Hello Everyone.
I am trying to create a code to decode an PostScript RLE image and save it.

However, my output is always invalid (the raw image). I try to open it on photoshop but its always an invalid image or something like that.

I've wrote at least three versions of it, all of them with the same result. So i'm asking for any help here.

My code goes on the comments, and this is how it should be decompressed (i think my code follows what the book says).
The method i created:

public static List<byte> RLETest(byte[] input)
    {
        List<byte> final = new List<byte>();
        for(int i=0; i < input.Length; i++)
        {
            var lengthByte = input[i];
            if(input[i] <= 127)
            {
                int currLen = input[i] + 1;
                for(int j=0; j < currLen; j++)
                {
                    final.Add(input[i]);
                }                    
            }
            else
            {
                int currLen = 257 - input[i];                    
                for(int j=0; j< currLen; j++)
                {
                    final.Add(input[i]);
                }
            }
        }
        return final;
    }

Sample file: https://drive.google.com/open?id=1-E4flwNsY6UE2ve-Ycg6Z4cwLeoKIQRu
Width: 212
Height: 154
Channels: 1
(For opening in Photoshop)
What PostScript Red Book says about its RLE Encoding:
"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 the 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."

Any help is appreciated. Thanks

Home
C# Language
Create a method to decode a RLE byte-oriented image
Pablo Costa
Pablo Costa
027
Create a method to decode a RLE byte-oriented image
Aug 4 2019 6:40 PM
Hello Everyone.
I am trying to create a code to decode an PostScript RLE image and save it.

However, my output is always invalid (the raw image). I try to open it on photoshop but its always an invalid image or something like that.

I've wrote at least three versions of it, all of them with the same result. So i'm asking for any help here.

My code goes on the comments, and this is how it should be decompressed (i think my code follows what the book says).
The method i created:
public static List<byte> RLETest(byte[] input)  
        {  
            List<byte> final = new List<byte>();  
            for(int i=0; i < input.Length; i++)  
            {  
                var lengthByte = input[i];  
                if(input[i] <= 127)  
                {  
                    int currLen = input[i] + 1;  
                    for(int j=0; j < currLen; j++)  
                    {  
                        final.Add(input[i]);  
                    }                      
                }  
                else  
                {  
                    int currLen = 257 - input[i];                      
                    for(int j=0; j< currLen; j++)  
                    {  
                        final.Add(input[i]);  
                    }  
                }  
            }  
            return final;  
        }  
Sample file: https://drive.google.com/open?id=1-E4flwNsY6UE2ve-Ycg6Z4cwLeoKIQRu
Width: 212
Height: 154
Channels: 1
(For opening in Photoshop)
What PostScript Red Book says about its RLE Encoding:
"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 the 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."

Any help is appreciated. Thanks

Recommended Answers

All 2 Replies

Sorry but this is where I'd use ImageMagick or another library. You left out the language tag and I don't know "code" as a language. I think you are using C# so here's the two googles I'd use to find a way to use ImageMagick.

  1. http://fileformats.archiveteam.org/wiki/ImageMagick
  2. https://www.google.com/search?q=using+imagemagick+in+C%23&gl=US
commented: May not go down too well if this is (as appears) a homework assignment! +15

That's really close, but
in copying (length+1) bytes (lines 26-31): you copy the same byte repeatedly.
in replicating one byte (lines34-38) you don't skip the byte you are copying.

Testing code on real data is easy - it either works or it doesn't. But debugging it is a nightmare because it's too complex and there's too much data. You know it's wromg but you don't know why.
So
Before you try to fix the code, just create a tiny test case so simple that you can immeditately see what's happening.
Maybe as simple as a byte array 0, 'a', 2, b', c','d', 255 ,'e' which should give abcdee as output

commented: Thanks for this. As to homework, we would be guessing. For such work I will admit to using ImageMagick. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.