win/linux static libs to compress /decompress as zlib

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

win/linux static libs to compress /decompress as zlib

Postby marpon » Mar 25, 2019 17:47

miniz is providing a very interresting code in c to compress/decompress with comparable compress ratio/speed as zlib,
but with very little size : on windows 25ko compared to zlib static lib 128ko

original code here https://github.com/richgel999/miniz

I wanted to convert to freebasic but it is too complex (and too long) for me, so i've changed my mind
and simplified strongly the c code to buid a static lib to work easily from freebasic, with only the compress, uncompress needed functions.

Here under is the link to get my resulting static libs for win or linux in 32/64 versions

https://github.com/marpon/Compression_tools/tree/master/minz

the code to test

Code: Select all

'minz_test2.bas to test minz static libs, equivalent compression/time as zlib

/' tested under win32 win64 and linux64 (mint) '/

'  change parameters under
'=====================================================================================================
#define _FILE_TO_COMPRESS_      "plan.bmp"        /' change here for your own files '/

'#define _FILE_TO_COMPRESS_     "blabla.txt"     /' interesting random txt file with very few repetitive words '/


#define _COMPRESSION_LEVEL_      6             /' from 1 to 10,  1 low compression, but fast   ...   10 high compression, but very slow  '/
'=====================================================================================================

#ifdef __FB_WIN32__
    #Ifdef __FB_64BIT__
        #INCLIB "minz_64"                        ' the lib name is : libminz_64.a
    #Else
        #INCLIB "minz_32"                        ' the lib name is : libminz_32.a
    #Endif
#endif

#ifdef __FB_LINUX__
    #Ifdef __FB_64BIT__
        #INCLIB "lminz_64"                       ' the lib name is : liblminz_64.a
    #Else
        #INCLIB "lminz_32"                       ' the lib name is : liblminz_32.a,  not tested : c compiled on mint64 / gcc64 with -m32
    #Endif
#endif


extern "C"
    declare Function z_compress alias "z_compress"(byref pDest as ubyte ptr , byval pInitial as ubyte ptr , byval Initial_len as ulong , byval level as ulong) as long
    declare Function z_uncompress alias "z_uncompress"(byref pDest as ubyte ptr , byval pComp as ubyte ptr , byval comp_len as ulong) as long
   
    declare Function z_compressBound alias "z_compressBound"(byval Initial_len as ulong) as ulong ' conservative information
    declare Function z_UncompLen alias "z_UncompLen"(byval pComp as ubyte ptr) as ulong ' precise information, gets from compressed header file
end extern


'================ helper ===========
type mystring
        data1                      as zstring ptr
        len1                       as integer
        size1                      as integer
END TYPE

function tostring(byval ub as ubyte ptr , byval ilen as long) as string
    dim         as mystring s_my
    s_my.data1 = cast(zstring ptr , ub)
    s_my.len1 = ilen
    s_my.size1 = ilen + 1
    dim as string ptr pret = cast(string ptr , @s_my)
    return * pret
END FUNCTION

Private Function FileToString(byref Nom_File as String) as String
    Dim         as uinteger Filesize
    Dim         as long result
    Dim         as long myHandle
   
    myHandle = Freefile()
    Function = ""
    result = Open(Nom_File For Binary Access Read as #myHandle)
    If result <> 0 Then Exit Function
    Filesize = LOF(myHandle)
    If Filesize = 0 Then
        Close #myHandle
        Exit Function
    End If
    dim as string s1 = string(Filesize , 0)
    Get #myHandle , 0 , s1
    Close #myHandle
    function = s1
End Function


'============== end helper =========



 /'  test code  '/

dim as string sin0 = FileToString(_FILE_TO_COMPRESS_) ' _FILE_TO_COMPRESS_  file to test compression, defined at the top

'sin0=sin0 &"end"
dim as long i0 = len(sin0)
print : print "original size  " & i0



dim as string s = sin0
'print "original string"
'print s
print

dim         as double d0
dim         as long i2
dim         as long i3
dim         as ubyte ptr enc1
dim         as ubyte ptr dec1

d0 = timer()
'estimates compressed size needed to allocate buffer ( conservative value)
i2 = z_compressBound(len(sin0))
i3 = i2
i2 = z_compress(enc1 , cast(ubyte ptr , strptr(sin0)) , i0 , _COMPRESSION_LEVEL_) ' _COMPRESSION_LEVEL_ from 1 to 10, defined at the top

d0 = timer() - d0
print "Compression time : " & int(d0 * 1000000) / 1000 & " ms"
print : print "sizes :   original" , i0 , "compressed" , i2 : print
print "         ratio  " & i2 / i0 : print
print "Buffer allocated size for compress : " & i3 : print
'print tostring(enc1, i2)
print
i3 = 0

'gives right size uncompressed needed to allocate buffer
i3 = z_UncompLen(enc1)
print : print : print "z_UncompLen  : needed for decompress buffer    " & i3


d0 = timer()
i3 = z_uncompress(dec1 , enc1 , i2)
d0 = timer() - d0
print : print "Decompression time : " & int(d0 * 1000000) / 1000 & " ms"

print : print "sizes :   compressed " , i2 , "uncompressed" , i3
dec1[i3] = 0

print : print
dim as string mess = tostring(dec1 , i3)
'print mess
print


if mess = sin0 THEN
    print "  conclusion : decompressed = initial    it's  OK!"
else
    print "  conclusion : decompressed <> initial   it has FAILED!"
END IF

deallocate enc1
deallocate dec1


print : print : print "Press any key to  exit"
sleep



I did not tested the linux32 static lib , I've compiled it under mint 64 with gcc 64 using -m32 flag , is it working ?
if some of you can confirm it will be nice.
jj2007
Posts: 1233
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: win/linux static libs to compress /decompress as zlib

Postby jj2007 » Mar 25, 2019 19:52

Looks good, compliments:

Code: Select all

Compression time : 73.145 ms

sizes :   original           977412       compressed     191839

         ratio  0.1962724009936444
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: win/linux static libs to compress /decompress as zlib

Postby marpon » Mar 26, 2019 8:36

Thank's jj2007 for testing.

What version did you test?
I'am interrested on the one i am not tested at all the linux32.

Because i've created that specific version on gcc64 just using the -m32 flag , is it correct?
jj2007
Posts: 1233
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: win/linux static libs to compress /decompress as zlib

Postby jj2007 » Mar 26, 2019 8:49

marpon wrote:What version did you test?
Win32
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: win/linux static libs to compress /decompress as zlib

Postby marpon » Mar 29, 2019 13:11

In my quest of very litle compression tools, a new one : aPLib v1.1.1 from Joergen Ibsen
http://www.ibsensoftware.com very complete static libs /dll ...

static libs easy to interface with freebasic win or linux in 32/64 bits
not as efficient on compression ratio /speed as minz lib but very light lib with fast decompression

have a look again on my light test, i've put in my github
https://github.com/marpon/Compression_tools/tree/master/aPlib
jj2007
Posts: 1233
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: win/linux static libs to compress /decompress as zlib

Postby jj2007 » Mar 29, 2019 15:14

JibZ has great stuff. I use his TinfLib for my UnzipFile macro.

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 11 guests