LoadImageArea?

BlitzMax Forums/BlitzMax Programming/LoadImageArea?

plash(Posted 2007) [#1]
Is there a way to load a x, y, w, h area of an image? (Instead of loading the whole image and grabbing a part of it)


H&K(Posted 2007) [#2]
no... well you could open it as a stream, and then parse though it and just load the bits that you wanted. But that would be stupid


ninjarat(Posted 2007) [#3]
Why not have a look at how Max2D does image loading, and use that study as a guide to write that function yourself.


ImaginaryHuman(Posted 2007) [#4]
For the most part you have to load all of the image data in order to decode it. To load a row of pixels in a PNG file for example, if there is filtering and compression present, you have to decode all of the pixels in every row in order to calculate the decompression. Likwise with JPEG, you have to read chunks in order to decompress. The only thing you'd be able to do any different is only output the parts you want to a buffer rather than all of it, but either way you still have to load the whole file and decompress all of it. The only exception to that is to use a file format that doesn't have any compression that relies on any information from adjacent pixels, which is pretty impossible, ie like a RAW file. You could load a window within RAW with no problem.


GfK(Posted 2007) [#5]
You can use LoadAnimImage for this - PROVIDED you just want to grab a single frame out of an animstrip (for example) without loading all of it.


Fabian.(Posted 2007) [#6]
LoadAnimImage loads the whole image and then extracts the frames using TPixmap's Window and Copy method.

Here's the source code of LoadAnimImage:
	Function LoadAnim:TImage( url:Object,cell_width,cell_height,first,count,flags,mr,mg,mb )
		Local pixmap:TPixmap=TPixmap(url)
		If Not pixmap pixmap=LoadPixmap(url) 'this loads the whole image
		If Not pixmap Return

		Local x_cells=pixmap.width/cell_width
		Local y_cells=pixmap.height/cell_height
		If first+count>x_cells*y_cells Return
		
		Local t:TImage=Create( cell_width,cell_height,count,flags,mr,mg,mb )

		For Local cell=first To first+count-1
			Local x=cell Mod x_cells * cell_width
			Local y=cell / x_cells * cell_height
			Local window:TPixmap=pixmap.Window( x,y,cell_width,cell_height ) 'selects a specific part of the image into a new pixmap
			t.SetPixmap cell-first,window.Copy() 'copies the pixmap
		Next
		Return t
	End Function
So at execution time LoadAnimImage wouldn't be faster than manually loading the image and then copying a window. However, at the time of development LoadAnimImage will be faster, since it's only one single function you have to call, not a set of multiple functions and methods needed to do this task manually.


tonyg(Posted 2007) [#7]
Plash, the answer might be 'what do you want to do?' as there must be a better solution than loading a portion of an image on disk.


klepto2(Posted 2007) [#8]
Maybe this something you're looking for?
Function LoadImagePart:TImage(url:Object , x:Int , y:Int , w:Int , h:Int)
	Local Pix:TPixMap = LoadPixmap(url)
	Local width:Int = PixmapWidth(Pix)
	Local height:Int = PixmapHeight(Pix)
	If X + W > width Then W = Width - X
	If Y + H > Height Then H = Height - Y
	
	Return LoadImage(PixmapWindow(Pix , x , y , w , h))
End Function

Graphics 800 , 600 , 0 , - 1


Local part1:TImage = LoadImagePart("Test.png" , 0 , 0 , 100 , 200)
Local part2:TImage = LoadImagePart("Test.png" , 50 , 320 , 450 , 255)

While Not KeyHit(Key_Escape)
	Cls
	DrawImage(part1 , 0 , 0)
	DrawImage(part2 , 50 , 150)
	Flip
Wend


In fact it does the ame as grabimage but without the need of drawing the whole image before.


plash(Posted 2007) [#9]
Well, I need to draw a part of an image, but the image is 40mb (bmp format), and I'd rather it not take up so much memory, so parsing through a RAW X,Y, RGB file would be the way to go?