October 28, 2020, 11:25:35 PM

### Author Topic: [bb] Run LengtRun Length En/Decoding by ozzi789 [ 1+ years ago ]  (Read 1527 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Run LengtRun Length En/Decoding by ozzi789 [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Run LengtRun Length En/Decoding
Author : ozzi789
Posted : 1+ years ago

Description : Great to store big maps with many returning chars!

<a href="http://en.wikipedia.org/wiki/Run-length_encoding" target="_blank">http://en.wikipedia.org/wiki/Run-length_encoding[/url]

Screenshot of demo:

Demo:

Code :
Code: BlitzBasic
1. Function RLE_ENCODE\$(strng\$,offset=4,marker=1)
2.         ;Reset Variables
3.         x=0 ;Pointer for String
4.         count=1 ;Runcount
5.         strng_len=Len(strng\$)
6.         While (x<strng_len) ;Check if we havent run through the whole string
7.                 x=x+1 ;increase pointer
8.
9.                 cursign\$= Mid(strng\$,x,1) ;read char at pointer
10.                 nextsign\$= Mid(strng\$,x+1,1) ;read next char
11.
12.                 If cursign\$=nextsign\$ ; if the current and the next sign are the smae
13.                         count=count+1 ; increase runcount
14.                 Else
15.                         If count>=offset ; If the chars werent identic, but the runcount was bigger or same
16.                                 newstrng\$=newstrng\$+Chr(marker)+Len(Str(count))+count ;Add coded string to marker & runcount char and runcount
17.                         Else If count>1 ; if the count of repeating chars was smaller then the offset
18.                                 newstrng\$=newstrng\$+String(cursign\$,count-1) ; just clone them into new string
19.                         EndIf
20.                         count=1 ; reset count
21.                         newstrng\$=newstrng+cursign\$ ;add the actuall char
22.                 EndIf
23.         Wend
24. Return newstrng\$ ;return finished string
25. End Function
26.
27.
28.
29. Function RLE_DECODE\$(strng\$,marker=1)
30.         x=0 ;pointer for the string
31.         strng_len=Len(strng\$)
32.         While (x=<strng_len) ;Check if we havent run through the whole string
33.
34.                 cursign\$= Mid(strng\$,x,1) ;get current char
35.                 nextsign\$= Mid(strng\$,x+1,1) ;get next char
36.
37.                 If cursign\$=Chr(marker) ;if current char is the marker
38.                         countlenght=Int(nextsign\$) ; Get the runcount char, example if its 1 the number has then lenght 1 - example 5 . if the number is 4 the number has a lenght of 4 - example 1234
39.                         count=Int(Mid(strng\$,x+2,countlenght)) ; get the count via our runcount char
41.                         x=x+2+countlenght ; increase pointer
42.                 Else
43.                         newstrng\$=newstrng\$+cursign\$ ;add the original char
44.                         x=x+1 ; increase pointer
45.                 EndIf
46.         Wend
47. Return newstrng\$ ;return string
48. End Function

psychicbottle(Posted 1+ years ago)

this is very interesting...

ozzi789(Posted 1+ years ago)

thanks :)i use it to compress my map files of my rpg (big dims with many repeating numbers)