Compressing a bank

BlitzMax Forums/BlitzMax Programming/Compressing a bank

MattVonFat(Posted 2005) [#1]
I have a bank full of data from an image i made and i want to compress it. I'm not really sure though if i have even started out correctly.

I so far have:
compress(,,bank,BankSize(bank),6)


and i'm not sure where the 6 comes from.

Can the first bit be a bank and if so how do i know how large it should be?

I have a feeling that i've already messed things up but i guess theres no harm in trying.


taxlerendiosk(Posted 2005) [#2]
These are some functions I made for compression a while ago, I hope they're of help.

Strict

'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
'  BYTES
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------

Function CompressBytes:Byte[](Bytes:Byte[],CompressLevel=9)

	Local length = Len(Bytes) * 2

	Local Compressed:Byte[length]
	
	Compressed[0] = Len(Bytes) & $FF
	Compressed[1] = (Len(Bytes) Shr 8) & $FF
	Compressed[2] = (Len(Bytes) Shr 16) & $FF

	Local DataPointer:Byte Ptr
	Local CompPointer:Byte Ptr
	
	DataPointer = Bytes
	CompPointer = Compressed
	CompPointer :+ 3
	
	compress2(CompPointer,length, DataPointer,Len(Bytes), CompressLevel)
		
	Return Compressed[..(length-1+3)]

End Function

Function DecompressBytes:Byte[](Compressed:Byte[])

	Local Size = Compressed[0] + (Compressed[1] Shl 8) + (Compressed[2] Shl 16)

	Local length = Size+1

	Local Uncompressed:Byte[length]

	Local CompPointer:Byte Ptr
	Local DataPointer:Byte Ptr

	CompPointer = Compressed
	CompPointer :+ 3
	DataPointer = Uncompressed
		
	uncompress(DataPointer,length, CompPointer,Len(Compressed))
		
	Return Uncompressed[..(length-1)]

End Function

'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
'  INTS
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------

Function CompressInts:Byte[](Ints:Int[],CompressLevel=9)

	Local length = Len(Ints) * 6

	Local Compressed:Byte[length]

	Compressed[0] = Len(Ints) & $FF
	Compressed[1] = (Len(Ints) Shr 8) & $FF
	Compressed[2] = (Len(Ints) Shr 16) & $FF

	Local DataPointer:Byte Ptr
	Local CompPointer:Byte Ptr
	
	DataPointer = Ints
	CompPointer = Compressed
	CompPointer :+ 3
	
	compress2(CompPointer,length, DataPointer,Len(Ints) * 4, CompressLevel)
		
	Return Compressed[..(length-1+3)]

End Function

Function DecompressInts:Int[](Compressed:Byte[])

	Local Size = Compressed[0] + (Compressed[1] Shl 8) + (Compressed[2] Shl 16)

	Local length = Size * 4

	Local Uncompressed:Int[Size]

	Local CompPointer:Byte Ptr
	Local DataPointer:Byte Ptr

	CompPointer = Compressed
	CompPointer :+ 3
	DataPointer = Uncompressed
		
	uncompress(DataPointer,length, CompPointer,Len(Compressed))
		
	Return Uncompressed

End Function

'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
'  STRINGS
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------

Function CompressStrings:Byte[](Strings:String[],CompressLevel=9)

	Local TotalSize = 2

	TotalSize :+ Len(Strings) * 2

	Local i:Int

	For i = 0 To Len(Strings)-1
		TotalSize :+ Len(Strings[i])
	Next

	Local StringsBank:TBank = CreateBank(TotalSize)

	Local StringBankStream:TBankStream = CreateBankStream(StringsBank)

	WriteShort StringBankStream, Len(Strings)

	For i = 0 To Len(Strings)-1
		WriteShort StringBankStream, Len(Strings[i])
		WriteString StringBankStream, Strings[i]
	Next

	SeekStream StringBankStream,0

	Local Byted:Byte[] = LoadStream(StringBankStream)

	CloseStream(StringBankStream)

	Return CompressBytes(Byted)

End Function

Function DecompressStrings:String[](Compressed:Byte[])

	Local Byted:Byte[] = DecompressBytes(Compressed)

	Local StrCount = Byted[0] + (Byted[1] Shl 8)

	Local Strings:String[StrCount]

	Local p = 2
	Local i:Int, j:Int, Length:Int

	For i = 0 To (StrCount-1)
		Length = Byted[p] + (Byted[p+1] Shl 8)
		p :+ 2
		Strings[i] = ""
		For j = 0 To (Length-1)
			Strings[i] :+ Chr$(Byted[p+j])
		Next
		p :+ Length
	Next

	Return Strings

End Function



Filax(Posted 2005) [#3]
Can you post an example please ? i want compress a memory bank but i don't find any example :( snif :)


marksibly(Posted 2005) [#4]
From BMK!

Import Pub.ZLib

Function CompressBank:TBank( bank:TBank )
	Local size=bank.Size()
	Local out_size=size+size/10+32
	Local out:TBank=TBank.Create( out_size )
	compress out.Buf()+4,out_size,bank.Buf(),size
	out.PokeByte 0,size
	out.PokeByte 1,size Shr 8
	out.PokeByte 2,size Shr 16
	out.PokeByte 3,size Shr 24
	out.Resize out_size+4
	Return out
End Function

Function UncompressBank:TBank( bank:TBank )
	Local out_size
	out_size:|bank.PeekByte(0)
	out_size:|bank.PeekByte(1) Shl 8
	out_size:|bank.PeekByte(2) Shl 16
	out_size:|bank.PeekByte(3) Shl 24
	Local out:TBank=TBank.Create( out_size )
	uncompress out.Buf(),out_size,bank.Buf()+4,bank.Size()-4
	Return out
End Function



Filax(Posted 2005) [#5]
many thanks mark :) i love you now .... lol :)


ImaginaryHuman(Posted 2005) [#6]
How does it actually compresS?


Drago(Posted 2005) [#7]
it uses the zLib Module, which is a compression Library. goto www.Zlib.org for more info


Filax(Posted 2005) [#8]
AngelDaniel : Less than winrar, more than winzip