B4J Question RLE encode a Byte Array?

techknight

Well-Known Member
Licensed User
Longtime User
So I have some hardware with a display on it that has limited RAM, so I was going to store my binary bitmaps in an RLE encoded format, but having a tough time finding an implementation of it anywhere.

Is there an easy way to encode a binary byte array (loaded from a file) into an RLE encoded byte array? (ill have to decode it on the other end in C)

not sure what the standard is on formatting, but I was assuming something like number of runs of the same value as the first word, and the byte being the value of the runs after this.

But not sure. Any ideas?
 

techknight

Well-Known Member
Licensed User
Longtime User
Actually I think I got it.

B4X:
Sub EncodeRLE
    Dim OriginalBinary() As Byte = FileToBytes(File.DirApp, "test.bin")
    Dim FileHex As String
    Dim I, RunCount As Int
    Dim LastValue As Byte
    
    'Begin with first byte. Runcount of 0.
    RunCount = 0
    LastValue = OriginalBinary(0)
    
    'Loop through entire file. 
    For I = 0 To OriginalBinary.Length-1    
        If OriginalBinary(I) = LastValue Then 'If the value is currently equal to the last one, its considered of the same run. 
            RunCount = RunCount + 1
            If I = (OriginalBinary.Length-1) Then 'We have reached the end of file. Save current value and runlength.
                FileHex = FileHex & GetHexWord(RunCount, False)
                FileHex = FileHex & HexString(LastValue)
                Exit
            End If
        Else                                    'The detected value is not of the same run, so lets write the last run and reset. 
            'Store the run length and value. 
            FileHex = FileHex & GetHexWord(RunCount, False)
            FileHex = FileHex & HexString(LastValue)
            'Reset to accomodate for new value. 
            LastValue = OriginalBinary(I)
            RunCount = 1    
            If I = (OriginalBinary.Length-1) Then 'We have reached the end of file. Save current value and runlength.
                Exit
            End If
        End If
    Next
    
    Dim EncodedBinary() As Byte = Convert.HexToBytes(FileHex)
    BytesToFile(File.DirApp, "Test.rle", EncodedBinary, False)
    
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
it depends on your data what is optimal.

what is GetHexWord returning?

2 bytes might be overkill if you know that you run lenghts are never big.

Make a coding challenge out of it. Things like this are always fun.
 
Last edited:
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Well I can have alot of the same color indexes (like a USA Basemap). its an Indexed color system.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
In case anyone is curious, This is more of a hobby project. Anyone who grew up in the 90s here in the USA, would undoubtly have seen this before. or know of it.

Here is what ive been working on with this:

Basically, its Modern meets Vintage. I use a Raspberry Pi with B4J, on a custom-designed bus interface card so the Pi GPIOs actually bootstrap, load and run the 68K system so it can do its thing. :)

Fun little project, childhood memories.

The original program were downloaded over satellite and that was analog. That signal is long since gone, so I wrote a re-creation between C and B4J along with some ASM, to run the relic of the original hardware.
 
Upvote 0
Top