home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 6 Unleashed…sional Reference Edition) / Visual_Basic_6_Unleashed_Professional_Reference_Edition_Sams_1999.iso / Source / CHAP36 / 309X3803.TXT < prev   
Encoding:
Text File  |  1998-04-29  |  5.2 KB  |  152 lines

  1.  
  2. Public Sub RLECompress(txtDataIn As TextBox, _
  3.     txtDataOut As TextBox)
  4.  
  5. Dim intCharCount As Integer
  6. Dim intNewChar As Integer
  7. Dim intLastChar As Integer
  8. Dim intCharLoop As Integer
  9. Dim lonCharPtr As Long
  10. Dim lonCharStrLen As Long
  11. Dim strCompressed As String
  12. Dim bytMgrChar As Byte
  13.  
  14. intCharCount = -1
  15. lonCharStrLen = Len(txtDataIn.Text)
  16.  
  17. ' Loop through the contents of the TextBox
  18. ' character by character. The loop is executed
  19. ' an additional time to process the last
  20. ' character.
  21. For lonCharPtr = 1 To (lonCharStrLen + 1)
  22.     ' If this is not the last loop, then
  23.     ' intNewChar is assigned the character that
  24.     ' is read in. Otherwise, a -1 value indicates
  25.     ' that it is the last loop.
  26.     If lonCharPtr < lonCharStrLen + 1 Then
  27.         intNewChar = Asc(Mid(txtDataIn.Text, _
  28.             lonCharPtr, 1))
  29.     Else
  30.         intNewChar = -1
  31.     End If
  32.     ' Add one to the character count.
  33.     intCharCount = intCharCount + 1
  34.     If lonCharPtr > 1 Then
  35.         ' Same character as last time?
  36.         If intNewChar = intLastChar Then
  37.             ' Is the character's high bit set?
  38.             If intCharCount = 128 Then
  39.                 ' If high bit is set, then the
  40.                 ' character will get its own
  41.                 ' manager byte. Assign the count
  42.                 ' to zero (count is always one
  43.                 ' less than actual).
  44.                 bytMgrChar = 128
  45.                 bytMgrChar = bytMgrChar Or 127
  46.                 ' Add the manager byte and the
  47.                 ' character to the output string.
  48.                 strCompressed = strCompressed _
  49.                     & Chr(bytMgrChar) & Chr(intLastChar)
  50.                 ' Set the character count to zero.
  51.                 intCharCount = 0
  52.             End If
  53.         Else
  54.             ' The character read this time is
  55.             ' different than the one read last time,
  56.             ' so check to see if the character count
  57.             ' is more than two.
  58.             If intCharCount > 2 Then
  59.                 ' The character count is more than
  60.                 ' two, so this character sequence
  61.                 ' can get a manager byte. Set the
  62.                 ' count to one less than actual.
  63.                 bytMgrChar = 128
  64.                 bytMgrChar = bytMgrChar Or (intCharCount - 1)
  65.                 ' Add the manager byte and the
  66.                 ' character to the output string.
  67.                 strCompressed = strCompressed _
  68.                     & Chr(bytMgrChar) & Chr(intLastChar)
  69.             Else
  70.                 ' Two of the same characters have
  71.                 ' been encountered, but that's not
  72.                 ' enough to warrant a manager byte.
  73.                 ' Add the two characters to the
  74.                 ' output string.
  75.                 For intCharLoop = 1 To intCharCount
  76.                     ' Check to see if the character
  77.                     ' has its high bit set. If
  78.                     ' it does, it'll have to have a
  79.                     ' manager byte.
  80.                     If bytLastChar > 127 Then
  81.                         bytOutChar = 128
  82.                         bytOutChar = bytOutChar Or (intCharCount - 1)
  83.                         strCompressed = strCompressed _
  84.                             & Chr(bytMgrChar) & Chr(intLastChar)
  85.                     Else
  86.                         ' The character does not have
  87.                         ' its high bits set, so it
  88.                         ' can be added to the output.
  89.                         strCompressed = strCompressed _
  90.                             & Chr(intLastChar)
  91.                     End If
  92.                 Next intCharLoop
  93.             End If
  94.             ' Reset the character count.
  95.             intCharCount = 0
  96.         End If
  97.     End If
  98.     ' Make the most recently read character the
  99.     ' last character so it can be checked in
  100.     ' the next loop iteration.
  101.     intLastChar = intNewChar
  102. Next lonCharPtr
  103.  
  104. ' Assign the compressed string to the output
  105. ' TextBox.
  106. txtDataOut.Text = strCompressed
  107.  
  108. End Sub
  109.  
  110.  
  111. Public Sub RLEUncompress(txtDataIn As TextBox, _
  112.     txtDataOut As TextBox)
  113.  
  114. Dim bytNewChar As Byte
  115. Dim intCharCount As Integer
  116. Dim lonCharPtr As Long
  117. Dim strUncompressed As String
  118.  
  119. lonCharPtr = 0
  120. Do
  121.     ' Increment the character pointer.
  122.     lonCharPtr = lonCharPtr + 1
  123.     ' Get the next character.
  124.     bytNewChar = Asc(Mid(txtDataIn.Text, lonCharPtr, 1))
  125.     ' Is the high bit set?
  126.     If bytNewChar > 127 Then
  127.         ' The high bit is set, so it must be a
  128.         ' manager byte. Get the character count.
  129.         intCharCount = (bytNewChar And 127) + 1
  130.         ' Get the next character.
  131.         lonCharPtr = lonCharPtr + 1
  132.         bytNewChar = Asc(Mid(txtDataIn.Text, lonCharPtr, 1))
  133.         ' Add the string of characters to the
  134.         ' output string.
  135.         strUncompressed = strUncompressed _
  136.             & String(intCharCount, bytNewChar)
  137.     Else
  138.         ' This is a solo character (no manager
  139.         ' byte), so add it to the output string.
  140.         strUncompressed = strUncompressed _
  141.             & Chr(bytNewChar)
  142.     End If
  143. ' Keep looping until the last character has
  144. ' been processed.
  145. Loop Until (lonCharPtr >= Len(txtDataIn.Text))
  146.  
  147. ' Assign the uncompressed string to the output
  148. ' TextBox.
  149. txtDataOut.Text = strUncompressed
  150.  
  151. End Sub
  152.