Advertisement
Guest User

Keen LZW Compressor in VB.NET

a guest
Dec 25th, 2013
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.97 KB | None | 0 0
  1. Function CompressLZW(Data As IO.Stream, MaxBits As Byte) As IO.MemoryStream
  2.         Dim out As New IO.MemoryStream
  3.         Dim bits As New List(Of Byte)
  4.         Dim dict As New List(Of Byte())
  5.         For x = 0 To 255
  6.             dict.Add({x})
  7.         Next
  8.         dict.Add({})
  9.         dict.Add({})
  10.         Dim usebits As Byte = 9
  11.         Dim PutCode = Sub(Code As UInteger)
  12.                           For x = usebits To 1 Step -1
  13.                               If Code - (2 ^ (x - 1)) >= 0 Then
  14.                                   Code -= 2 ^ (x - 1)
  15.                                   bits.Add(1)
  16.                               Else
  17.                                   bits.Add(0)
  18.                               End If
  19.                           Next
  20.                       End Sub
  21.         Dim AddToDict = Function(Entry As Byte()) As Boolean
  22.                             If dict.Count < 2 ^ MaxBits Then
  23.                                 dict.Add(Entry)
  24.                                 If dict.Count = 2 ^ (usebits - 1) Then usebits = Math.Min(usebits + 1, MaxBits)
  25.                                 Return True
  26.                             Else
  27.                                 Return False
  28.                             End If
  29.                         End Function
  30.         Dim FindCode = Function(Bytes As Byte()) As UInteger
  31.                            For x = 1 To dict.Count
  32.                                If dict(x - 1).SequenceEqual(Bytes) Then Return x - 1
  33.                            Next
  34.                            Throw New NotFiniteNumberException
  35.                        End Function
  36.         Dim DictContains = Function(Bytes As Byte()) As Boolean
  37.                                For x = 1 To dict.Count
  38.                                    If dict(x - 1).Length = Bytes.Length Then
  39.                                        If dict(x - 1).SequenceEqual(Bytes) Then Return True
  40.                                    End If
  41.                                Next
  42.                                Return False
  43.                            End Function
  44.         Dim match As Byte() = {Data.ReadByte}
  45.         Do Until Data.Position = Data.Length
  46.             Dim nbyte As Byte = Data.ReadByte
  47.             Dim nmatch As Byte() = match.Concat({nbyte}).ToArray
  48.             If DictContains(nmatch) Then
  49.                 match = nmatch
  50.             Else
  51.                 If AddToDict(nmatch) Then
  52.                     PutCode(dict.Count - 1)
  53.                 Else
  54.                     PutCode(FindCode(match))
  55.                 End If
  56.                 match = {nbyte}
  57.             End If
  58.         Loop
  59.         PutCode(FindCode(match))
  60.         PutCode(257)
  61.         Do Until bits.LongCount Mod 8L = 0L
  62.             bits.Add(0)
  63.         Loop
  64.         For x = 1 To CInt(bits.LongCount / 8L)
  65.             Dim b As Byte = 0
  66.             For y = 0 To 7
  67.                 b += bits((x - 1) * 8 + y) * (2 ^ (7 - y))
  68.             Next
  69.             out.WriteByte(b)
  70.         Next
  71.         out.Position = 0
  72.         Return out
  73.     End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement