Ok, I compressed. Now how do I decompress it?

Just starting out? Need help? Post your questions and find answers here.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Ok, I compressed. Now how do I decompress it?

Post by Inf0Byt3 »

I managed to make a small compression routine based on lzw after a paper. It works, but I can't find a way to decompress the output. Can anyone help? The output is in Outp.s.

Code: Select all

Declare GetNextChar()
Declare StringTable_Init()
Declare StringTable_Add(cp.s)
Declare StringTable_Find(cp.s)

Global TestString.s
Global CurrPos.l
Global Dim StringTable.s(4096)
Global StringTable_Count.l

TestString = "^WED<256>E<260><261><257>B<260>T"

Procedure GetNextChar()
  CurrPos = CurrPos + 1
  If CurrPos > Len(TestString)
    ProcedureReturn -1
  Else
    ProcedureReturn Asc(Mid(TestString, CurrPos, 1))
  EndIf
EndProcedure
 
Procedure StringTable_Init()
  Dim UsedChars.l(256)
    For i = 1 To Len(TestString)
      UsedChars(Asc(Mid(TestString, i, 1))) = 1
    Next i
    For i = 0 To 255
     If UsedChars(i) = 1 
      StringTable_Add(Chr(i))
     EndIf
    Next i
EndProcedure

Procedure StringTable_Add(cp.s)
  StringTable(StringTable_Count) = cp
  StringTable_Count = StringTable_Count + 1 
EndProcedure

Procedure StringTable_Find(cp.s)
    For i = 0 To StringTable_Count - 1
      If StringTable(i) = cp
        ProcedureReturn i
      EndIf
    Next i
    ProcedureReturn -1
EndProcedure

Procedure OutputCode(cp.s)
   Global OUTp.s
   oCode = StringTable_Find(cp)
   OUTp.s = OUTp.s + Chr(oCode)
EndProcedure

CurrentPrefix.s
K.l
StringTable_Init()                                     
CurrentPrefix = ""                                       
 Repeat
  K = GetNextChar()                                     
  If StringTable_Find(CurrentPrefix + Chr(K)) <> -1 
   CurrentPrefix = CurrentPrefix + Chr(K)               
  Else
   StringTable_Add(CurrentPrefix + Chr(K)) 
   OutputCode(CurrentPrefix)                                    
   CurrentPrefix = Chr(K)                            
  EndIf
 Until K = -1  
 
 Debug Str(Len(TestString))+" > "+ Str(Len(OUTp.s))
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post by superadnim »

why dont you just keep on reading the paper then?, unless you didnt understand what you were reading, you should be able to pull this out.
Phoenix
Enthusiast
Enthusiast
Posts: 141
Joined: Sun Sep 04, 2005 2:25 am

Post by Phoenix »

superadnim wrote:why dont you just keep on reading the paper then?, unless you didnt understand what you were reading, you should be able to pull this out.
:lol: What a great newbie response!!!!
RoxBox
New User
New User
Posts: 4
Joined: Mon Jul 24, 2006 4:51 am

LZW

Post by RoxBox »

I found this, it may help and has c code for the lzw too.

http://marknelson.us/1989/10/01/lzw-data-compression/

Frank
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Post by citystate »

If I'm reading your code correctly, it looks like your output will need to include the StringTable in some format as well. Otherwise, you'll be left with a list of pointers and nothing to point it to.

To decompress you'll need to replace each occurence of a character in the output string with the corresponding string in the StringTable.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Well ok, I'll try it... About the paper, I am afraid it was treating only the compression part and that's why I didn't get it done. Thanks for the links :D.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Post Reply