Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 History and applications  





2 Methodology  





3 Algorithm  



3.1  Encoding Algorithm  





3.2  Decoding Algorithm  







4 Code  





5 Example  





6 Variants  





7 See also  





8 References  





9 External links  














Run-length encoding






العربية
Català
Čeština
Deutsch
Español
فارسی
Français

Italiano
Magyar
Nederlands

Polski
Português
Русский
Simple English
Suomi
Svenska
Українська

 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


Run-length encoding (RLE) is a form of lossless data compression in which runs of data (consecutive occurrences of the same data value) are stored as a single occurrence of that data value and a count of its consecutive occurrences, rather than as the original run. As an imaginary example of the concept, when encoding an image built up from colored dots, the sequence "green green green green green green green green green" is shortened to "green x 9". This is most efficient on data that contains many such runs, for example, simple graphic images such as icons, line drawings, games, and animations. For files that do not have many runs, encoding them with RLE could increase the file size.

RLE may also refer in particular to an early graphics file format supported by CompuServe for compressing black and white images, that was widely supplanted by their later Graphics Interchange Format (GIF).

RLE also refers to a little-used image format in Windows 3.x that is saved with the file extension rle; it is a run-length encoded bitmap, and the format was used for the Windows 3.x startup screen.

History and applications

[edit]

Run-length encoding (RLE) schemes were employed in the transmission of analog television signals as far back as 1967.[1] In 1983, run-length encoding was patentedbyHitachi.[2][3][4] RLE is particularly well suited to palette-based bitmap images (which use relatively few colours) such as computer icons, and was a popular image compression method on early online services such as CompuServe before the advent of more sophisticated formats such as GIF.[5] It does not work well on continuous-tone images (which use very many colours) such as photographs, although JPEG uses it on the coefficients that remain after transforming and quantizing image blocks.

Common formats for run-length encoded data include Truevision TGA, PackBits (by Apple, used in MacPaint), PCX and ILBM. The International Telecommunication Union also describes a standard to encode run-length colour for fax machines, known as T.45.[6] That fax colour coding standard, which along with other techniques is incorporated into Modified Huffman coding,[citation needed] is relatively efficient because most faxed documents are primarily white space, with occasional interruptions of black.

Methodology

[edit]

Run-length encoding compresses data by reducing the physical size of a repeating string of characters. The algorithm works as follows

  1. Read the data from the input sequence.
  2. Count the number of consecutive repeating characters (run length).
  3. Store the character and its run length.
  4. Repeat the process for the entire input data sequence.

Algorithm

[edit]

Encoding Algorithm

[edit]

The encoding process involves converting the input data into a compressed format by identifying and counting consecutive occurrences of each character. The steps are as follows:

  1. Initialize an empty result string.
  2. Traverse the input data.
  3. For each character in the data, count its consecutive occurrences from left to right.
  4. Append the count followed by the character to the result string.

Decoding Algorithm

[edit]

The decoding process involves reconstructing the original data from the encoded format by repeating characters according to their counts. The steps are as follows:

  1. Initialize an empty result string.
  2. Traverse the encoded data.
  3. For each count-character pair, repeat the character count times.
  4. Append these characters to the result string.
  5. Here’s a step-by-step example:

Code

[edit]
def rle_encode(data):
    encoding = ""
    i = 0

    while i < len(data):
        count = 1

        while i + 1 < len(data) and data[i] == data[i + 1]:
            i += 1
            count += 1

        encoding += str(count) + data[i]
        i += 1

    return encoding

def rle_decode(data):
    decoding = ""
    i = 0

    while i < len(data):
        count = int(data[i])
        char = data[i + 1]
        decoding += char * count
        i += 2

    return decoding

# Example usage
encoded = rle_encode("AAAABBBCCDAA")
print(f"Encoded: {encoded}")

decoded = rle_decode("4A3B2C1D2A")
print(f"Decoded: {decoded}")

RLE has a space complexity of O(n), where n is the size of the input data.

Example

[edit]

Consider a screen containing plain black text on a solid white background. There will be many long runs of white pixels in the blank space, and many short runs of black pixels within the text. A hypothetical scan line, with B representing a black pixel and W representing white, might read as follows:

WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW

With a run-length encoding (RLE) data compression algorithm applied to the above hypothetical scan line, it can be rendered as follows:

12W1B12W3B24W1B14W


This can be interpreted as a sequence of twelve Ws, one B, twelve Ws, three Bs, etc., and represents the original 67 characters in only 18. While the actual format used for the storage of images is generally binary rather than ASCII characters like this, the principle remains the same. Even binary data files can be compressed with this method; file format specifications often dictate repeated bytes in files as padding space. However, newer compression methods such as DEFLATE often use LZ77-based algorithms, a generalization of run-length encoding that can take advantage of runs of strings of characters (such as BWWBWWBWWBWW).

Run-length encoding can be expressed in multiple ways to accommodate data properties as well as additional compression algorithms. For instance, one popular method encodes run lengths for runs of two or more characters only, using an "escape" symbol to identify runs, or using the character itself as the escape, so that any time a character appears twice it denotes a run. On the previous example, this would give the following:

WW12BWW12BB3WW24BWW14

This would be interpreted as a run of twelve Ws, a B, a run of twelve Ws, a run of three Bs, etc. In data where runs are less frequent, this can significantly improve the compression rate.

One other matter is the application of additional compression algorithms. Even with the runs extracted, the frequencies of different characters may be large, allowing for further compression; however, if the run lengths are written in the file in the locations where the runs occurred, the presence of these numbers interrupts the normal flow and makes it harder to compress. To overcome this, some run-length encoders separate the data and escape symbols from the run lengths, so that the two can be handled independently. For the example data, this would result in two outputs, the string "WWBWWBBWWBWW" and the numbers (12,12,3,24,14).

Variants

[edit]

See also

[edit]

References

[edit]
  1. ^ Robinson, A. H.; Cherry, C. (1967). "Results of a prototype television bandwidth compression scheme". Proceedings of the IEEE. 55 (3). IEEE: 356–364. doi:10.1109/PROC.1967.5493.
  • ^ "Run Length Encoding Patents". Internet FAQ Consortium. 21 March 1996. Retrieved 14 July 2019.
  • ^ "Method and system for data compression and restoration". Google Patents. 7 August 1984. Retrieved 14 July 2019.
  • ^ "Data recording method". Google Patents. 8 August 1983. Retrieved 14 July 2019.
  • ^ Dunn, Christopher (1987). "Smile! You're on RLE!" (PDF). The Transactor. 7 (6). Transactor Publishing: 16–18. Retrieved 2015-12-06.
  • ^ Recommendation T.45 (02/00): Run-length colour encoding. International Telecommunication Union. 2000. Retrieved 2015-12-06.
  • [edit]
    Retrieved from "https://en.wikipedia.org/w/index.php?title=Run-length_encoding&oldid=1235538564"

    Categories: 
    Data compression
    Lossless compression algorithms
    Hidden categories: 
    Pages using deprecated source tags
    Articles with short description
    Short description is different from Wikidata
    All articles with unsourced statements
    Articles with unsourced statements from December 2015
     



    This page was last edited on 19 July 2024, at 20:16 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki