picture from stream

BlitzMax Forums/BlitzMax Programming/picture from stream

The question has probably already been asked a couple of times but i couldn't find anything with the "search" method... is it possible to read some binary data from a file and, knowing that this data is in fact a png for example, to convert it in a TImage thing? One "ugly" way to do so would be reading the wanted part as a binary stream, then save it as a temp file wich then would be loaded with the LoadPNG function... this is "ugly" and probably slow since it requires 2 hard drive access which could be time consuming if the pictures I want to load are big or numerous or both.
So i m looking for a cleanert way, the objective being to encapsulate my game data in a single binary file

Thanks in advance :)

Load it into a bank and then use a bank stream as the source of LoadImage()

?

Funny you should ask this question, as I was recently working on a similar problem.

It's as AngelDaniel suggests.

Here's some sample code ripped from an encoder/decoder I wrote.

SuperStrict

Graphics 800, 600

Local Bank:TBank = unencode()

Local img:TImage = LoadImage(Bank)

HideMouse

While Not KeyHit(KEY_ESCAPE)

	Cls
		
	DrawImage img, MouseX(), MouseY()
	
	Flip True
	
Wend

Bank = Null
GCCollect

End

Function unencode:TBank()
	
	Local url:String
	Local size:Long
	Local lines:Long
	
	Print "Unencoding..."
	
	RestoreData media
	
	ReadData url
	ReadData size
	ReadData lines
	
	Local bank:TBank = CreateBank(size)
	Local str:String
	Local bpos:Long = 0
	
	For Local n:Long = 1 To lines	
		ReadData str
		For Local i:Int = 0 To (str.length - 1) Step 2
			Local ch:String = "$" + Chr(str[i]) + Chr(str[i + 1])	
			Local val:Byte = Byte(ch)
			PokeByte bank, bpos, val
			bpos :+ 1
		Next
	Next

	Print "Done."
	
	Return bank
		
EndFunction


#media
DefData "pointer.png", 302, 3
DefData "89504E470D0A1A0A0000000D4948445200000020000000200806000000737A7AF400000006624B474400FF00FF00FFA0BDA793000000097048597300000B1300000B1301009A9C180000000774494D4507D703050113336C6C4666000000BB4944415478DAED96DB0EC2300C43E368FFFFCBE6050402B1368E4B25B4487B5B"
DefData "9BD338374404C36BA8FC7C4444907D0600D2B9B43CF9E9BC4C71989C433D9F3BC2EE96005B001CAF97013A49B7A40A7E0EF0D237D86D64720448BE83EC91A0DB452D39708790E4B025A12A87B50A14883CABF5C7B71222671A4D03A225010C102C037C71D485E014C0A0C72F91238B03468218CD720AB39DB3C936DA98525C"
DefData "2C6C91C866F470063103E810F3438ECAC2E2CA262EBEFFB2CBFED86E99AC3D5251DD9D610000000049454E44AE426082"


Hope it helps.

EDIT: Simplified initial example.

This might also help:
File nibbler

Just to clarify the simplest solution...

http://www.blitzbasic.com/Community/posts.php?topic=50729#565158

wow, thanks for all the replies :)