Sunrast's Implementation of Run length Encoding
Sun Rasterfile also known as Sunrast is an image format developed by Sun Microsystems, It uses a version of Run Length Encoding to compress images.
In this tutorial we'll be discussing how Sun Rasterfile's implementation of RLE is different from the basic vanila version and how is it used to reduce the size of images. The format implements a Run length encoding trigger, which is used to reduce the wastage of bytes in case of a non redudant data.
0x80 (RLE Trigger) is a trigger to read a runlength for the next value, otherwise the bytes are written unchanged (as in original).
Note: This is a continuation of my previous article on Run Length Encoding
The sunrast format uses RLE in the following format :-
RUN is the run length (i.e length of repetition)
and Byte is the value to be repeated.
Lets look at some examples and try to understand How Sunrast encodes them :-
Original : [0x11 0x22 0x33]
Encoded : [0x11 0x22 0x33]
Here there is no difference between the original and the encoded data because the data is non redudant.
Original : [0x11 0x11 0x11]
Encoded : [0x80 0x02 0x11]
Original : [0x80 0x11 0x11]
Encoded : [0x80 0x00 0x02 0x11]
This example is a bit tricky, this involves [0x80] to be encoded to [0x80 0x00].
Q: Why is that?
A: Basically its a way to indicate that the byte value is same as the RLE trigger and without RLE. Its just a way sunrast handles it and its predefined in the format's specifications.
Original : [0x80 0x80 0x80]
Encoded : [0x80 0x03 0x80]
What so ever be the byte value, if its being repeated, then the format follows the normal RLE format i.e [RLE TRIGGER] [RUN – 1] [BYTE].
Libav already has a working Sunrast Encoder (which is written by me :pleased:)
Now that we understand the Sunrast's version of RLE lets try to solve some examples to check our knowledge :-
Here's the Python Code to Check your answers :-
Sunrast's implementation of RLE reduces the wastage of bytes in case of non redudant data, but some times it can be a little expensive if a file contains a lot of 0x80 bytes at different places, encoding a 0x80 byte (non-redudant) would take 2 bytes [0x80 0x00] which will cost more bytes than the original data. The worst case i.e a file with alternate 0x80 bytes can cost double the bytes as used in the original data.
|All times are GMT +5.5. The time now is 09:00.|