This annex describes how to apply RLE Compression to an i mage or an individual frame of a multi-frame image. This method can be used for any image, independent of the values of the data elements that describe the image (i.e: Photometric Interpretation (0028,0004) and Bits Stored (0028,0101)).
RLE Compression consists of the following steps:
1. The image is converted to a sequence of Composite Pixel Codes (see PS 3.3).
2. The Composite Pixel Codes are used to generate a set of Byte Segments (see section G.2)
3. Each Byte Segment is RLE compressed to produce a RLE Segment (see section G.4)
4. The RLE Header is appended in front of the concatenated RLE Segments (see section G.5)
A Byte Segment is a series of bytes generated by decomposing the Composite Pixel Code (see PS 3.3).
If the Composite Pixel Code is not an integral number of bytes in size, sufficient Most Significant zero bits are added to make it an integral byte size. This is known as the Padded Composite Pixel Code.
The first Segment is generated by stripping off the most significant byte of each Padded Composite Pixel Code and ordering these bytes sequentially. The second Segment is generated by repeating this process on the stripped Padded Composite Pixel Code continuing until the last Pixel Segment is generated by ordering the least significant byte of each Padded Component Pixel Code sequentially.
Note: If Photometric Interpretation (0028, 0004) equals RGB and Bits Stored equals 8, then three Segments are generated. The first one holds all the Red values, the second all the Green values, and the third all the Blue values.
The RLE algorithm described in this section is used to compress Byte Segments into RLE Segments. There is a one-to-one correspondence between Byte Segments and RLE Segments. Each RLE segment must be an even number of bytes or padded at its end with zero to make it even.
A sequence of identical bytes (Replicate Run) is encoded as a two-byte code:
< -count + 1 > <byte value>, where
count = the number of bytes in the run, and
2 <= count <= 128
and a non-repetitive sequence of bytes (Literal Run) is encoded as:
< count - 1 > <Iiteral sequence of bytes>, where
count = number of bytes in the sequence, and
1 <= count <= 128.
The value of -128 may not be used to prefix a byte value.
Note: It is common to encode a 2-byte repeat run as a Replicate Run except when preceded and followed by a Literal Run, in which case it's best to merge the three runs into a Literal Run.
Three-byte repeats shall be encoded as Replicate Runs. Each row of the image shall be encoded separately and not cross a row boundary.
Pseudo code for the RLE decoder is shown below:
Loop until the number of output bytes equals the uncompressed segment size
Read the next source byte into n
If n> =0 and n <= 127 then
output the next n+1 bytes literally
Elseif n <= - 1 and n >= -127 then
output the next byte -n+1 times
Elseif n = - 128 then
The RLE Segments are ordered as described in section G.2. They are preceded by the RLE Header which contains offsets to the start of each RLE Segment. The RLE Header is described in G.5.
The first RLE Segment immediately follows the RLE Header and the remaining RLE Segments immediately follow each other. This is illustrated in the diagram below.
|RLE Segment 1|
|RLE Segment 2|
|. . .|
|. . .|
|RLE Segment n|
The RLE Header contains the number of RLE Segments for the image, and the starting offset of each of the RLE Segments. Each of these numbers is represented by a UL (unsigned long) value stored in little-endian format. The RLE Header is 16 long words in length. This allows it to describe a compressed image with up to 15 RLE Segments. All unused segments offsets shall be set to zero.
Each of the starting locations for the RLE Segments are byte offsets relative to the beginning of the RLE Header. Since the RLE Header is 16 unsigned longs or 64 bytes, the offset of RLE Segment One is 64.
The following diagram illustrates the ordering of the offsets within the RLE Header.
|number of RLE Segments|
|offset of RLE Segment 1 = 64|
|offset of RLE Segment 2|
|. . .|
|. . .|
|offset of RLE Segment n|
Figure G.6.1 is an example of encoding of RLE Compressed Frames (described in Section G.4) with the basic offset table. Figure G.6.2 is an example of Item Value data for one frame.
Figure G.6.1EXAMPLE OF ELEMENTS FOR AN ENCODED YC B C R RLE THREE-FRAME IMAGE WITH BASIC OFFSET TABLE
|Pixel Data Element Tag||Value Representation||Data Element Length||Data Element|
|Basic Offset Table with Item Value||First Fragment (Frame 1) of Pixel Data|
|Second Fragment (Frame 2) of Pixel Data||Third Fragment (Frame 3) of Pixel Data||Sequence Delimiter Item|
|Item Tag||Item Length||Item Value||Item Tag||Item Length||Item Value||Sequence Delimiter Tag||Item Length|
|(FFFE, E000)||0000 036AH||RLE Compressed Frame||(FFFE, E000)||0000 0BC8H||RLE Compressed Frame||(FFFE, E0DD)||0000 0000H|
|4 bytes||2 bytes||036AH bytes||4 bytes||4 bytes||0BC8H bytes||4 bytes||4 bytes|
Figure G.6.2EXAMPLE OF ENCODED YC B C R RLE COMPRESSED FRAME ITEM VALUE
|Offset||Data||Description of Data|
|0000 0000H||0000 0003H||number of RLE Segments||(Header)|
|0000 0040H||location of RLE Segment 1 (Y component)|
|0000 0140H||location of RLE Segment 2 (CB component)|
|0000 01C0H||location of RLE Segment 3 (CR component)|
|0000 0040H||Y - RLE Segment Data||(DATA)|
|0000 0140H||CB - RLE Segment Data||(DATA)|
|0000 01COH||CR - RLE Segment Data||(DATA)|