Using ImagePluginPNG to (un)compress a memorybuffer

Share your advanced PureBasic knowledge/code with the community.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Using ImagePluginPNG to (un)compress a memorybuffer

Post 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
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: Using ImagePluginPNG to (un)compress a memorybuffer

Post 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?
javabean
User
User
Posts: 60
Joined: Sat Nov 08, 2003 10:29 am
Location: Austria

Re: Using ImagePluginPNG to (un)compress a memorybuffer

Post 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.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Using ImagePluginPNG to (un)compress a memorybuffer

Post 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.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Post Reply