home *** CD-ROM | disk | FTP | other *** search
-
- Public Sub RLECompress(txtDataIn As TextBox, _
- txtDataOut As TextBox)
-
- Dim intCharCount As Integer
- Dim intNewChar As Integer
- Dim intLastChar As Integer
- Dim intCharLoop As Integer
- Dim lonCharPtr As Long
- Dim lonCharStrLen As Long
- Dim strCompressed As String
- Dim bytMgrChar As Byte
-
- intCharCount = -1
- lonCharStrLen = Len(txtDataIn.Text)
-
- ' Loop through the contents of the TextBox
- ' character by character. The loop is executed
- ' an additional time to process the last
- ' character.
- For lonCharPtr = 1 To (lonCharStrLen + 1)
- ' If this is not the last loop, then
- ' intNewChar is assigned the character that
- ' is read in. Otherwise, a -1 value indicates
- ' that it is the last loop.
- If lonCharPtr < lonCharStrLen + 1 Then
- intNewChar = Asc(Mid(txtDataIn.Text, _
- lonCharPtr, 1))
- Else
- intNewChar = -1
- End If
- ' Add one to the character count.
- intCharCount = intCharCount + 1
- If lonCharPtr > 1 Then
- ' Same character as last time?
- If intNewChar = intLastChar Then
- ' Is the character's high bit set?
- If intCharCount = 128 Then
- ' If high bit is set, then the
- ' character will get its own
- ' manager byte. Assign the count
- ' to zero (count is always one
- ' less than actual).
- bytMgrChar = 128
- bytMgrChar = bytMgrChar Or 127
- ' Add the manager byte and the
- ' character to the output string.
- strCompressed = strCompressed _
- & Chr(bytMgrChar) & Chr(intLastChar)
- ' Set the character count to zero.
- intCharCount = 0
- End If
- Else
- ' The character read this time is
- ' different than the one read last time,
- ' so check to see if the character count
- ' is more than two.
- If intCharCount > 2 Then
- ' The character count is more than
- ' two, so this character sequence
- ' can get a manager byte. Set the
- ' count to one less than actual.
- bytMgrChar = 128
- bytMgrChar = bytMgrChar Or (intCharCount - 1)
- ' Add the manager byte and the
- ' character to the output string.
- strCompressed = strCompressed _
- & Chr(bytMgrChar) & Chr(intLastChar)
- Else
- ' Two of the same characters have
- ' been encountered, but that's not
- ' enough to warrant a manager byte.
- ' Add the two characters to the
- ' output string.
- For intCharLoop = 1 To intCharCount
- ' Check to see if the character
- ' has its high bit set. If
- ' it does, it'll have to have a
- ' manager byte.
- If bytLastChar > 127 Then
- bytOutChar = 128
- bytOutChar = bytOutChar Or (intCharCount - 1)
- strCompressed = strCompressed _
- & Chr(bytMgrChar) & Chr(intLastChar)
- Else
- ' The character does not have
- ' its high bits set, so it
- ' can be added to the output.
- strCompressed = strCompressed _
- & Chr(intLastChar)
- End If
- Next intCharLoop
- End If
- ' Reset the character count.
- intCharCount = 0
- End If
- End If
- ' Make the most recently read character the
- ' last character so it can be checked in
- ' the next loop iteration.
- intLastChar = intNewChar
- Next lonCharPtr
-
- ' Assign the compressed string to the output
- ' TextBox.
- txtDataOut.Text = strCompressed
-
- End Sub
-
-
- Public Sub RLEUncompress(txtDataIn As TextBox, _
- txtDataOut As TextBox)
-
- Dim bytNewChar As Byte
- Dim intCharCount As Integer
- Dim lonCharPtr As Long
- Dim strUncompressed As String
-
- lonCharPtr = 0
- Do
- ' Increment the character pointer.
- lonCharPtr = lonCharPtr + 1
- ' Get the next character.
- bytNewChar = Asc(Mid(txtDataIn.Text, lonCharPtr, 1))
- ' Is the high bit set?
- If bytNewChar > 127 Then
- ' The high bit is set, so it must be a
- ' manager byte. Get the character count.
- intCharCount = (bytNewChar And 127) + 1
- ' Get the next character.
- lonCharPtr = lonCharPtr + 1
- bytNewChar = Asc(Mid(txtDataIn.Text, lonCharPtr, 1))
- ' Add the string of characters to the
- ' output string.
- strUncompressed = strUncompressed _
- & String(intCharCount, bytNewChar)
- Else
- ' This is a solo character (no manager
- ' byte), so add it to the output string.
- strUncompressed = strUncompressed _
- & Chr(bytNewChar)
- End If
- ' Keep looping until the last character has
- ' been processed.
- Loop Until (lonCharPtr >= Len(txtDataIn.Text))
-
- ' Assign the uncompressed string to the output
- ' TextBox.
- txtDataOut.Text = strUncompressed
-
- End Sub
-