HOME

TheInfoList



OR:

Run-length encoding (RLE) is a form of
lossless data compression Lossless compression is a class of data compression that allows the original data to be perfectly reconstructed from the compressed data with no loss of information. Lossless compression is possible because most real-world data exhibits statistic ...
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 CompuServe (CompuServe Information Service, also known by its initialism CIS) was an American online service provider, the first major commercial one in the world – described in 1994 as "the oldest of the Big Three information services (the oth ...
for compressing black and white images, that was widely supplanted by their later
Graphics Interchange Format The Graphics Interchange Format (GIF; or , see pronunciation) is a bitmap image format that was developed by a team at the online services provider CompuServe led by American computer scientist Steve Wilhite and released on 15 June 1987. ...
(GIF). RLE also refers to a little-used image format in
Windows 3.x Windows 3.x means either of, or all of the following versions of Microsoft Windows: * Windows 3.0 * Windows 3.1x Windows 3.1 is a major release of Microsoft Windows. It was released to manufacturing on April 6, 1992, as a successor to Windo ...
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

Run-length encoding (RLE) schemes were employed in the transmission of analog television signals as far back as 1967. In 1983, run-length encoding was
patent A patent is a type of intellectual property that gives its owner the legal right to exclude others from making, using, or selling an invention for a limited period of time in exchange for publishing an enabling disclosure of the invention."A p ...
ed by
Hitachi () is a Japanese multinational corporation, multinational Conglomerate (company), conglomerate corporation headquartered in Chiyoda, Tokyo, Japan. It is the parent company of the Hitachi Group (''Hitachi Gurūpu'') and had formed part of the Ni ...
. RLE is particularly well suited to palette-based bitmap images (which use relatively few colours) such as
computer icons In computing, an icon is a pictogram or ideogram displayed on a computer screen in order to help the user navigate a computer system. The icon itself is a quickly comprehensible symbol of a software tool, function, or a data file, accessible on th ...
, and was a popular image compression method on early
online service An online service provider (OSP) can, for example, be an Internet service provider, an email provider, a news provider (press), an entertainment provider (music, movies), a search engine, an e-commerce site, an online banking site, a health site, ...
s such as
CompuServe CompuServe (CompuServe Information Service, also known by its initialism CIS) was an American online service provider, the first major commercial one in the world – described in 1994 as "the oldest of the Big Three information services (the oth ...
before the advent of more sophisticated formats such as
GIF The Graphics Interchange Format (GIF; or , see pronunciation) is a bitmap image format that was developed by a team at the online services provider CompuServe led by American computer scientist Steve Wilhite and released on 15 June 1987. ...
. It does not work well on continuous-tone images (which use very many colours) such as photographs, although
JPEG JPEG ( ) is a commonly used method of lossy compression for digital images, particularly for those images produced by digital photography. The degree of compression can be adjusted, allowing a selectable tradeoff between storage size and imag ...
uses it on the coefficients that remain after transforming and quantizing image blocks. Common formats for run-length encoded data include
Truevision TGA Truevision TGA, often referred to as TARGA, is a raster graphics file format created by Truevision Inc. (now part of Avid Technology). It was the native format of TARGA and VISTA boards, which were the first graphic cards for IBM-compatible P ...
,
PackBits PackBits is a fast, simple lossless compression scheme for run-length encoding of data. Apple introduced the PackBits format with the release of MacPaint on the Macintosh computer. This compression scheme can be used in TIFF files. TGA files also ...
(by Apple, used in
MacPaint MacPaint is a raster graphics editor developed by Apple Computer and released with the original Macintosh personal computer on January 24, 1984. It was sold separately for US$195 with its word processing counterpart, MacWrite. MacPaint was nota ...
),
PCX PCX, standing for ''PiCture eXchange'', was an image file format developed by the now-defunct ZSoft Corporation of Marietta, Georgia, United States. It was the native file format for PC Paintbrush and became one of the first widely accepted DOS ...
and
ILBM Interleaved Bitmap (ILBM) is an image file format conforming to the Interchange File Format (IFF) standard. The format originated on the Amiga platform, and on IBM-compatible systems, files in this format or the related PBM (Planar Bitmap) forma ...
. The
International Telecommunication Union The International Telecommunication Union is a specialized agency of the United Nations responsible for many matters related to information and communication technologies. It was established on 17 May 1865 as the International Telegraph Unio ...
also describes a standard to encode run-length colour for
fax Fax (short for facsimile), sometimes called telecopying or telefax (the latter short for telefacsimile), is the telephonic transmission of scanned printed material (both text and images), normally to a telephone number connected to a printer o ...
machines, known as T.45. That fax colour coding standard, which along with other techniques is incorporated into
Modified Huffman coding Modified Huffman coding is used in fax machines to encode black-on-white images (bitmaps). It combines the variable-length codes of Huffman coding with the coding of repetitive data in run-length encoding. The basic Huffman coding provides a way to ...
, is relatively efficient because most faxed documents are primarily white space, with occasional interruptions of black.


Methodology

Run-length encoding compresses data by reducing the physical size of a repeating string of characters. The algorithm works as follows # Read the data from the input sequence. # Count the number of consecutive repeating characters (run length). # Store the character and its run length. # Repeat the process for the entire input data sequence.


Algorithm


Encoding Algorithm

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: # Initialize an empty result string. # Traverse the input data. # For each character in the data, count its consecutive occurrences from left to right. # Append the count followed by the character to the result string.


Decoding Algorithm

The decoding process involves reconstructing the original data from the encoded format by repeating characters according to their counts. The steps are as follows: # Initialize an empty result string. # Traverse the encoded data. # For each count-character pair, repeat the character count times. # Append these characters to the result string. # Here’s a step-by-step example:


Code

def rle_encode(data): encoding = "" i = 0 while i < len(data): count = 1 while i + 1 < len(data) and data

data + 1 i += 1 count += 1 encoding += str(count) + data i += 1 return encoding def rle_decode(data): decoding = "" i = 0 while i < len(data): count = int(data char = data + 1 decoding += char * count i += 2 return decoding # Example usage encoded = rle_encode("AAAABBBCCDAA") print(f"Encoded: ") decoded = rle_decode(encoded) print(f"Decoded: ") RLE has a space complexity of O(n), where n is the size of the input data.


Example

Consider a screen containing plain black text on a solid white background. There will be many long runs of white
pixel In digital imaging, a pixel (abbreviated px), pel, or picture element is the smallest addressable element in a raster image, or the smallest point in an all points addressable display device. In most digital display devices, pixels are the smal ...
s in the blank space, and many short runs of black pixels within the text. A hypothetical
scan line A scan line (also scanline) is one line, or row, in a raster scanning pattern, such as a line of video on a cathode ray tube (CRT) display of a television set or computer monitor. On CRT screens the horizontal scan lines are visually discernible ...
, 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 ASCII ( ), abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Because of ...
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 LZ77 and LZ78 are the two lossless data compression algorithms published in papers by Abraham Lempel and Jacob Ziv in 1977 and 1978. They are also known as LZ1 and LZ2 respectively. These two algorithms form the basis for many variations includin ...
-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

* Sequential RLE: This method processes data one line at a time, scanning from left to right. It is commonly employed in image compression. Other variations of this technique include scanning the data vertically, diagonally, or in blocks. * Lossy RLE: In this variation, some bits are intentionally discarded during compression (often by setting one or two significant bits of each pixel to 0). This leads to higher compression rates while minimally impacting the visual quality of the image. *Adaptive RLE: Uses different encoding schemes depending on the length of runs to optimize compression ratios. For example, short runs might use a different encoding format than long runs.


See also

*
Kolakoski sequence In mathematics, the Kolakoski sequence, sometimes also known as the Oldenburger–Kolakoski sequence, is an infinite sequence of symbols that is the sequence of run lengths in its own run-length encoding. It is named after the recreational mathe ...
*
Look-and-say sequence In mathematics, the look-and-say sequence is the integer sequence, sequence of integers beginning as follows: : 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, 31131211131221, ... . To generate a member of the sequence from the previous m ...
*
Comparison of graphics file formats This is a comparison of image file formats (graphics file formats). This comparison primarily features file formats for 2D images. General Ownership of the format and related information. Technical details See also * List of codecs Referen ...
*
Golomb coding Golomb coding is a lossless data compression method using a family of data compression codes invented by Solomon W. Golomb in the 1960s. Alphabets following a geometric distribution will have a Golomb code as an optimal prefix code, making Golomb ...
*
Burrows–Wheeler transform The Burrows–Wheeler transform (BWT, also called block-sorting compression) rearranges a character string into runs of similar characters. This is useful for compression, since it tends to be easy to compress a string that has runs of repeated c ...
*
Recursive indexing {{no footnotes, date=June 2020 Recursive indexing is an algorithm used to represent large numberic values using members of a relatively small set. Recursive indexing writes the successive differences of the number after extracting the maximum valu ...
*
Run-length limited Run-length limited or RLL coding is a line coding technique that is used to send arbitrary data over a communications channel with bandwidth limits. RLL codes are defined by four main parameters: ''m'', ''n'', ''d'', ''k''. The first two, ''m''/ ...
* Bitmap index *
Forsyth–Edwards Notation Forsyth–Edwards Notation (FEN) is a standard notation for describing a particular board position of a chess game. The purpose of FEN is to provide all the necessary information to restart a game from a particular position. FEN is based on a sys ...
, which uses run-length-encoding for empty spaces in chess positions. * DEFLATE *
Convolution In mathematics (in particular, functional analysis), convolution is a operation (mathematics), mathematical operation on two function (mathematics), functions ( and ) that produces a third function (f*g) that expresses how the shape of one is ...
*
Huffman coding In computer science and information theory, a Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression. The process of finding or using such a code proceeds by means of Huffman coding, an algori ...
*
Arithmetic coding Arithmetic coding (AC) is a form of entropy encoding used in lossless data compression. Normally, a string of characters is represented using a fixed number of bits per character, as in the ASCII code. When a string is converted to arithmetic e ...


References


External links


Run-length encoding implemented in different programming languages
(on
Rosetta Code Rosetta Code is a wiki-based programming website with implementations of common algorithms and solutions to various programming problems in many different programming languages. It is named for the Rosetta Stone, which has the same text inscribe ...
)
Single Header Run-Length Encoding Library
smallest possible implementation (about 20 SLoC) in ANSI C. FOSS, compatible with
Truevision TGA Truevision TGA, often referred to as TARGA, is a raster graphics file format created by Truevision Inc. (now part of Avid Technology). It was the native format of TARGA and VISTA boards, which were the first graphic cards for IBM-compatible P ...
, supports 8, 16, 24 and 32 bit elements too. {{Compression formats Lossless compression algorithms