LZO fast compress/decompress

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

LZO fast compress/decompress

Post 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)
SPAMINATOR NR.1
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: LZO fast compress/decompress

Post by idle »

thanks, haven't heard of it before.
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: LZO fast compress/decompress

Post by netmaestro »

Thanks for sharing your work on this, it's quite interesting!
BERESHEIT
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: LZO fast compress/decompress

Post by infratec »

idle wrote:thanks, haven't heard of it before.
Hi idle,

never heard about UPX :?:


Btw.: nice work :!:

Beard
marroh
User
User
Posts: 72
Joined: Wed Aug 06, 2008 8:21 am

Re: LZO fast compress/decompress

Post 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?
PureBASIC v5.41 LTS , Windows v8.1 x64
Forget UNICODE - Keep it BASIC !
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: LZO fast compress/decompress

Post by flaith »

Thanks for sharing, i always appreciate when someone shared his work, it is really a great community here :D
“Fear is a reaction. Courage is a decision.” - WC
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: LZO fast compress/decompress

Post 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....
SPAMINATOR NR.1
marroh
User
User
Posts: 72
Joined: Wed Aug 06, 2008 8:21 am

Re: LZO fast compress/decompress

Post by marroh »

Hi Rings

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

lg Martin
PureBASIC v5.41 LTS , Windows v8.1 x64
Forget UNICODE - Keep it BASIC !
Post Reply