RLE Toolkit for CC65

RLE Toolkit consists of:

Compression API

There are two functions, rle_pack and rle_unpack, that can be called from both C and assembler.

C interface:

  unsigned int __fastcall__ rle_pack(unsigned char *dest,
                                     unsigned char *src,
                                     unsigned int length);

  unsigned int __fastcall__ rle_unpack(unsigned char *dest,
				       unsigned char *src);

Assembler interface:

	.import rle_pack
	.importzp src, dest
	.import srclen, destlen

	; set source pointer
	lda #<sourcedata
	sta src
	lda #>sourcedata
	sta src + 1

	; set destination pointer
	lda #<destbuffer
	sta dest
	lda #>destbuffer
	sta dest + 1

	; set length of source data when packing
	; parameter is ignored when unpacking
	lda #<datalen
	sta srclen
	lda #>datalen
	sta srclen + 1

	jsr rle_pack
	; or
	jsr rle_unpack

	; length of output is returned in destlen

Or, as Wildstar would say, "alot of LDA and STA and JSR usage".

Packed stream format

When two or more consecutive bytes are identical, they are replaced by <BYTE> <BYTE> <COUNT>. A COUNT of 0 indicates end of stream (EOS). A COUNT of 1 indicates that two bytes should be written, a COUNT of 2 indicates three bytes, and so on. BYTE/COUNT pairs can be chained for runs of more than 255 characters: 'x', 'x', 255, 'x', 10 would output 266 bytes of 'x'. This is also used to set the EOS when the last bytes are a run: 'x', 'x', 9, 'x', 0 would terminate after 10 bytes of 'x'.

Commandline tools

  rlepack infile outfile
  rleunpack infile outfile

Self explanatory. Right?

Sample code

  loadimage.prg

Loads a packed koala image and displays it. Thanks, again, to TMR for the image.

Downloads

rle-toolkit-1.0.zip

License

The RLE Toolkit is released under a BSD license:

Copyright (c) 2004, Per Olofsson
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.