Page 1 of 2

Re: Using ImagePluginPNG to (un)compress a memorybuffer

Posted: Tue Feb 16, 2010 8:16 pm
by ts-soft
a bit shorter and crossplattform:

Code: Select all

; Autor: Thomas (ts-soft) Schulz
; PB-Version: 4.xx
; OS: windows/linux/MacOS

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux
    ImportC #PB_Compiler_Home + "purelibraries/linux/libraries/zlib.a"
CompilerCase #PB_OS_MacOS
  ImportC "/usr/lib/libz.dylib"
  CompilerCase #PB_OS_Windows
    ImportC "zlib.lib"
CompilerEndSelect
  compress2(*dest, *destLen, *source, sourceLen, level)
  uncompress(*dest, *destLen, *source, sourceLen)
EndImport

Procedure zipPackMemory(*source, sourceLen = #PB_Default, level = #PB_Default)
  Protected *dest, destLen
  
  If level < #PB_Default Or level > 9 : level = #PB_Default : EndIf
  If *source
    If sourceLen = #PB_Default : sourceLen = MemorySize(*source) : EndIf
    destLen = sourceLen + 13 + (Int(sourceLen / 100))
    *dest = AllocateMemory(destLen)
    If *dest
      If Not compress2(*dest, @destLen, *source, sourceLen, level)
        *dest = ReAllocateMemory(*dest, destLen)
        ProcedureReturn *dest
      EndIf
    EndIf
  EndIf
EndProcedure

Procedure zipUnpackMemory(*source, *dest)
  Protected sourceLen = MemorySize(*source)
  Protected destLen = MemorySize(*dest)

  If Not uncompress(*dest, @destLen, *source, sourceLen)
    ProcedureReturn destLen
  EndIf
EndProcedure
Greetings
Thomas

Re: Using ImagePluginPNG to (un)compress a memorybuffer

Posted: Sat Feb 20, 2010 7:29 pm
by Rescator
Have to give thumbs up to the last two posts here. zlib.lib is the best solution and I really wish PureBasic had that as it's default compression. (4.5+ maybe?)

Btw! Do you really need to go into usr/lib/ ? I kinda assumed that zlib.lib was distributed with all PureBasic versions on all platforms?

Re: Using ImagePluginPNG to (un)compress a memorybuffer

Posted: Sat Feb 20, 2010 10:35 pm
by javabean
PureBasic uses the deflate and inflate functions (according to the PNG specification) for its PNG ImagePlugin. So the zlib library is most probably part of each distribution.

Re: Using ImagePluginPNG to (un)compress a memorybuffer

Posted: Fri May 07, 2010 1:12 pm
by c4s
The solution from ts-soft doesn't work anymore for PB 4.50 Beta 4?! Can anyone confirm this and hopefully say what I can do to fix it?

And by the way:
For decompression I alway get -3 as a result but the compression worked fine.


Edit:
Sorry, I'm not fully sure but I think it was an error on my side.