.ZNG image file format

BlitzMax Forums/BlitzMax Programming/.ZNG image file format

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

i have try this code and you are right, the program loop forever. The function xor seem to be the cause of the problem.

I see that you XOR a string with a key, but a png file is
a binary file? Maybe you must xor with bytes and not with
string.

Example :
'read bank data to buffer string
Local buffer$
For i = 0 To bank.size()-1
Local b:Byte = PeekByte(bank,i)
buffer :+ Chr$(b)
Next

Must be IMO:

'read bank data
For i = 0 To bank.size()-1
Local b:Byte = PeekByte(bank,i)

' Xor the byte with one char of the key
' then write the char generated by the xor
Next


xlsior : You are right, Chroma you need to compress the file before encrypting.


'.ZNG ImageLoader

'Author: Noel Cole (Chroma)
'Email:  noel@...

Global ZNG_Key$
ZNG_Key$ ="abc123"

SetZNGKey("abc123")

Local inFile$ = "Fond Deviant Art_00009.png"
Local outFile$ = StripExt(inFile)

Local ibank:TBank = LoadBank(inFile)

' De activate this function made loop forever?
'If ZNG_Key Then ibank = XorBank(ibank,ZNG_Key)
Local cbank:TBank = CompressBank(ibank)

Local out:TStream = WriteFile(outFile + ".zng")
out.writebytes(cbank.buf(), cbank.size())
CloseStream(out)
End



Graphics 800,600

Local img:TImage = LoadImageZNG("Fond Deviant Art_00009.zng")

SetBlend ALPHABLEND

SetClsColor 100,100,100

While Not KeyHit(KEY_ESCAPE)
Cls

DrawImage img,100,100


Flip
Wend
End




'And here's the ZNG_Converter again with the encryption option:

'Zipped Network Graphic [.zng] - Image File Format Converter

'Encryption Supported

'Build into an .exe and drag and drop your image files to convert them to .zng

'by Noel Cole (Chroma)

Function XorBank:TBank(bank:TBank, key$)
	'read bank data to buffer string
	Local buffer$
	For i = 0 To bank.size()-1
		Local b:Byte = PeekByte(bank,i)
		buffer :+ Chr$(b)
	Next
	
	buffer = xor(buffer,key$)

	Local rbank:TBank = CreateBank(buffer.length)
	For i = 0 To rbank.size()-1
		PokeByte(rbank,i,Asc(Mid$(buffer,i+1,1)))
	Next
	Return rbank
End Function

Function xor:String(str$, key$)
	Local ml%, pl%, i%, result$
	ml% = Len(str)
	pl% = Len(key)
	For i = 0 Until ml
		Local c:Byte = str$[i]
		Local k = key$[i Mod pl]
		c = c ~ k
		result$ :+ Chr(c)
	Next 
	Return result$
EndFunction

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 LoadImageZNG:TImage(zngFile$, flags%=-1)
	Local cbank:TBank = LoadBank(zngFile)
	Local ubank:TBank = UncompressBank(cbank)
	If ZNG_Key Then ubank = XorBank(ubank,ZNG_Key)
	Return LoadImage(ubank, flags)
End Function

Function LoadPixmapZNG:TPixmap(zngFile$)
	Local cbank:TBank = LoadBank(zngFile)
	Local ubank:TBank = UncompressBank(cbank)
	Return LoadPixmap(ubank)
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

Function SetZNGKey(key$)
	ZNG_Key = key
End Function