Page 1 of 1

LZO fast compress/decompress

Posted: Fri Mar 25, 2011 4:05 pm
by Rings
i download the source from http://www.oberhumer.com/opensource/lzo/
made a lib with pellesC (from the mini_lzo files) and did some short testcode:
Result: very fast and good results for the speed of compression.
so i like to share my work now here .
Complete package (C-source,PellesC-Projectfile and Purebasic-Example) here

The C-code is portable, if anyone can port that also to linux&mac ?

Code: Select all

Import "LZO.lib"
  
  
   ;lzo_init.l() 
  
  ;__lzo_init_v2(sizeofunsigned,int,int,int,int,int,int,int,int,int);
  
  
;   #define lzo_init() __lzo_init_v2(LZO_VERSION,(int)SizeOf(short),(int)SizeOf(int),\
;     (int)SizeOf(long),(int)SizeOf(lzo_uint32),(int)SizeOf(lzo_uint),\
;     (int)lzo_sizeof_dict_t,(int)SizeOf(char *),(int)SizeOf(lzo_voidp),\
;     (int)SizeOf(lzo_callback_t))
; LZO_EXTERN(int) __lzo_init_v2(unsigned,int,int,int,int,int,int,int,int,int);
__lzo_init_v2.l(version,SizeofShort,SizeofInt,SizeofLong,SizeofInt32,Sizeofuint,sizeofdict,sizeofchar,sizeofvoid,sizeofcallback)


  lzo_initV2.l() ;my own
  
  lzo_version_string.l()
  lzo_version.l()
  lzo_version_date.l()
  
  lzo_copyright.l()
  
  lzo1x_1_compress.l(in,in_len,out,out_len,wrkmem)

  lzo1x_decompress(compressed,compressed_len,uncompressed,uncompressed_len,NULL);
  
  
  
EndImport
  

#LZO_E_ERROR  =-1
 #LZO_E_OK     =0
; #define LZO_E_OUT_OF_MEMORY         (-2)    /* [Not used right now] */
; #define LZO_E_NOT_COMPRESSIBLE      (-3)    /* [Not used right now] */
; #define LZO_E_INPUT_OVERRUN         (-4)
; #define LZO_E_OUTPUT_OVERRUN        (-5)
; #define LZO_E_LOOKBEHIND_OVERRUN    (-6)
; #define LZO_E_EOF_NOT_FOUND         (-7)
; #define LZO_E_INPUT_NOT_CONSUMED    (-8)
; #define LZO_E_NOT_YET_IMPLEMENTED   (-9)    /* [Not used right now] */




result=lzo_version_string()
If result
 Debug "Version= " + PeekS(Result) ;debug version string
EndIf

result=lzo_version_date()
If result
 Debug "Date=" + PeekS(Result) ;debug date string
EndIf


; result=lzo_version()
; Debug result


; result=lzo_copyright()
; If result
;  Debug "Copyright ? " + PeekS(result) ;debug copyright
; EndIf


;result = lzo_init() ;init the lib
;result=lzo_init() 
result=lzo_initV2()
If result<> #LZO_E_OK
  Debug "init failed"  
  End  
EndIf  
    

a.s="Hello world , Hello Purebasic , Hi to the folks out there !"
a+a
a+a

pIn=@a
in_len=Len(A)

b.s=Space(Len(a)*2)
pOut=@b

out_len=0
wrkmem=AllocateMemory(16384  *  8) ;prepare a working buffer


result= lzo1x_1_compress(pIn,in_len,pOut,@out_len,wrkmem)

If result= #LZO_E_OK
  
  Debug "compressed to " + Str(out_len)
  c.s=Space(In_len)
  
  result = lzo1x_decompress(pOut,Out,@c,@new_len,0);
  
  Debug "Decpompressed lenght=" + Str(new_len)
  Debug c
  
EndIf  

;End


;Create a Bitmap (Screenshot)
Filename.s="c:\test.bmp"
ExamineDesktops()
w=DesktopWidth(0)
h=DesktopHeight(0)
res=CreateImage(1,w,h) 
hDC = StartDrawing(ImageOutput(1))
hwnd=GetDesktopWindow_()
hwnd=0
;Debug hwnd
DC=GetDC_(hwnd)
BitBlt_(hDC, 0, 0, w, h, DC, x, y, #SRCCOPY)
StopDrawing() 
SaveImage(1,Filename,#PB_ImagePlugin_BMP)

Result= ReadFile(0, Filename)

FileLength=FileSize(Filename)
BitMapData=AllocateMemory(FileLength)
Compressed=AllocateMemory(FileLength)

Readed= ReadData(0, BitMapData, FileLength)
Debug readed

t1=ElapsedMilliseconds() 
result= lzo1x_1_compress(BitMapData,FileLength,Compressed,@out_len,wrkmem)
t2=ElapsedMilliseconds() 

Debug "length compressed=" + Str(out_len) + " in " + Str(t2-T1) +" msecs"

t1=ElapsedMilliseconds() 
result = lzo1x_decompress(Compressed,out_len,BitMapData,@new_len,0);

t2=ElapsedMilliseconds() 

Debug "length decompressed=" + Str(new_len) + " in " + Str(t2-T1) +" msecs"




FreeMemory(BitMapData)
FreeMemory(Compressed)

FreeMemory(wrkmem)

Re: LZO fast compress/decompress

Posted: Sat Mar 26, 2011 9:37 am
by idle
thanks, haven't heard of it before.

Re: LZO fast compress/decompress

Posted: Sat Mar 26, 2011 10:18 am
by netmaestro
Thanks for sharing your work on this, it's quite interesting!

Re: LZO fast compress/decompress

Posted: Sat Mar 26, 2011 10:56 am
by infratec
idle wrote:thanks, haven't heard of it before.
Hi idle,

never heard about UPX :?:


Btw.: nice work :!:

Beard

Re: LZO fast compress/decompress

Posted: Sat Apr 09, 2011 12:17 pm
by marroh
Rings wrote: The C-code is portable, if anyone can port that also to linux&mac ?
When this is windows only, why not use simple the native built-in compression/function from windows? Or are the LZNT1, NS3 and NS15 algorytm not efficiently as LZO?

Re: LZO fast compress/decompress

Posted: Sat Apr 09, 2011 3:14 pm
by flaith
Thanks for sharing, i always appreciate when someone shared his work, it is really a great community here :D

Re: LZO fast compress/decompress

Posted: Sat Apr 09, 2011 5:13 pm
by Rings
marroh wrote:When this is windows only, why not use simple the native built-in compression/function from windows? Or are the LZNT1, NS3 and NS15 algorytm not efficiently as LZO?
This (LZO) comes with complete source, so its not limited to windows.
(beside that i'm not so familar with linux/mac c-compilers)
and yes, LZO is more efficiently in Speed than those implemented in ntkernel.dll .
Also faster than the BRIEFLZ algo, which is implemented as native Pack
algo in the linux&Mac Purebasic versions.
flaith wrote:Thanks for sharing, i always appreciate when someone shared his work, it is really a great community here :D
we all profit....

Re: LZO fast compress/decompress

Posted: Sun Apr 10, 2011 11:39 am
by marroh
Hi Rings

Thank you for your explanation, I have to test it. :) Thank you!

lg Martin