RLE Compression

Discussion in 'Utilities Archive' started by Oerg866, Apr 28, 2011.

Thread Status:
Not open for further replies.
  1. Oerg866

    Oerg866 Well-Known Member Member

    Joined:
    Aug 29, 2008
    Messages:
    299
    Location:
    Frankfurt, Germany
    Hi. Figured I'd post some of my tools I posted on other sites here also. They are more generally oriented to coders.


    This compression format compresses basically any data that has a lot of repeated bytes in it very well.


    This is a flavor of RLE that jorge created. This is some tools and stuff I did for it. The file format works like this.



    Code:
    Header is a word. This word contains the size of compressed content. 
    
    
    
    The next are RLE streams. There are two "special" cases:
    
    
    
    Single byte, next byte is not the same. RLE output would be just that single byte.
    
    
    
    Two identical bytes following each other. RLE output would actually be three bytes. n is the byte: n n $0. that means two times n with 0 following repetitions.
    
    
    
    The rest (longer ocurrances of repeated bytes) will be compressed as such:
    
    
    
    n n (length of how many repetitions - 2)
    
    
    
    what if there are more than 257 bytes in succession? Well this is what you do:
    
    
    
    n n $FF (repeat that as many times as 257 fits in the length count) + n n (rest).


    I hope that made..sense?



    Anyway, here are:



    RLEComp



    RLE Compressor for Windows. Works fine as far as I am aware. (Tested with ~ 400 images...). Source nicely commented because I was bored.



    DOWNLOAD BINARY



    DOWNLOAD SOURCE



    RLEDecomp



    RLE Decompressor for Windows.





    Code:
    Usage&#58; rledec.exe <source> <destination>
    
    
    
    Example&#58; rledec.exe test.rle test.bin

    DOWNLOAD BINARY


    DOWNLOAD SOURCE


    RLEDecomp68K


    Of course if you have RLE Compressed data you would want to decompress it and use it, wouldn't you? Well this is my efficient and tiny decompressor for this file format. This should work perfectly. If it doesn't, I might have screwed up while reverting a special modification I've made for a special thingy....



    ; Run length decoder written by Oerg866
    ;


    ; ARGUMENTS: A6 = SOURCE (the stuff you want to decode)


    ; A5 = DESTINATION (the location where you want


    ; the decompressed data to be in RAM)


    RunLengthDecoding:


    moveq #0, d0 ; Clear out d0


    move.b (a6)+, d0 ; Read one byte, in case compressed data is unaligned


    lsl.w #8, d0 ; Make it the upper byte inside a word


    move.b (a6)+, d0 ; move next byte into the lower byte of that word, which is the size word


    move.b (a6)+, d1 ; Initialize "last read" byte


    subq.w #1, d0 ; Decrease remaining size counter


    RunLengthDecoding_LOOP: ; DECODING LOOP START


    subq.w #1, d0 ; Decrease remaining size counter


    beq.s RLD_End ; If remaining size counter is 0, quit


    move.b (a6)+, d2 ; Fetch next byte into d2


    move.b d1,(a5)+ ; Write the old byte to destination (since one occurance gets written no matter what)


    cmp.b d2, d1 ; Check if the old and new byte are identical


    beq.s RLD_Identical ; If yes, branch


    RLD_NotIdentical: ; if not, leave it at that


    move.b d2, d1 ; and make d2 the old byte.


    bra.s RunLengthDecoding_LOOP ; Go back to main loop


    RLD_Identical: ; if the bytes are identical...


    moveq #0, d2 ; Clearing out d2 here is practical since it will only be done if we have a loop (dbra)


    move.b (a6)+, d2 ; Grab the amount of bytes to be written in succession into d2 (saves a few cycles...)


    subq.w #1, d0 ; decrease remaining size counter


    RLD_WriteLoop: ; Start writing the sequence into the destination...


    move.b d1, (a5)+ ; Write the amount of bytes in succession...


    dbra d2, RLD_WriteLoop ; Looping until d2 is 0....


    move.b (a6)+, d1 ; Grab the next byte


    subq.w #1, d0 ; decrease remaining size counter


    bra.s RunLengthDecoding_LOOP ; and go back to the loop.


    RLD_End:


    rts



    After decompression, just load them into VRAM as usual.


    Gonna post more tools here.


    Have fun!
     
Thread Status:
Not open for further replies.