LIKELY SPAM: [SDL] RLE acceleration

nbs nbs at
Wed Jul 17 10:01:01 PDT 2002

On Wed, Jul 17, 2002 at 05:22:12PM +0800, Jin.bin [?e??] wrote:
> what means RLE acceleration ?

RLE means Run Length Encoding.

It's a way of compressing data (and, in SDL, making writing chunks of
data quicker) by simply encoding how many times pieces of data are

For example, if you use the RLE-encoding on a colorkey (the single-color
transparency you can assign to a surface - similar to the transparent color
in the GIF file format), a picture like this:


...which would normally just be stored as:

  blank blank blank dot dot blank blank blank
    blank blank dot dot dot dot blank blank
    blank blank dot dot dot dot blank blank
  blank blank blank dot dot blank blank blank

...would instead be stored as:

  3 blanks, 2 dots, 5 blanks, 4 dots, 4 blanks, 4 dots, 5 blanks,
  2 dots, 3 blanks.

If each "blank" or "dot" color were 3 bytes (R,G,B), the normal storage
would require (8 across x 4 down) x (3 bytes per pixel) = 96 bytes.

The RLE "compressed" would require
(9 encoded pixels) x (3 bytes per pixel) + (9 counter bytes) = 36.

The important part comes when you go to blit.

Instead of saying:

  for (y = 0; y < height; y = y + 1)
    for (x =0; x < width; x = x + 1)
      if ( pixel[y][x] is not blank )
        draw( pixel[y][x] );

You can use pointer (or index) arithmetic to simply SKIP the entire chunks
of blank pixels.  So instead of examining (8 across x 4 down) = 32 pixels
to see whether or not they're blank, you'll only examine 9 "chunks" of
pixels.  (The 'blanks' or 'dots' part of "3 blanks, 2 dots, ..." above)

At least, this is my understanding of it.  It's not I wrote (or even looked at)
the RLE code in SDL.

But it should give you the jist of how it works.


More information about the SDL mailing list