How to GrabImage from ImageBuffer?

Blitz3D Forums/Blitz3D Beginners Area/How to GrabImage from ImageBuffer?

NewtSoup(Posted 2007) [#1]
Hi, I have this code which I think _should_ work (obviously) but it doesn't.

It does work if I dont set the buffer to ImageBuffer() and just draw the mainImage to the screen and then grab it. But I don't want to do that. I want to do it "behind the scenes" as it were. Looking at the documentation I should be able to set the current buffer to ImageBuffer(mainImage) and then use GrabImage(boardImage,x,y,framindex)

I'll post the code as it's not long.

Const ROWS =4
Const COLS =4

Graphics 800,600,32,2
Dim board(ROWS,COLS)

;test.jpg is actually 800x600
mainImage=LoadImage("test.jpg")
ResizeImage mainImage,640,480

boardImage = CreateImage (160,120,ROWS*COLS)



;set the current buffer to the image I want to grab frames from
SetBuffer=ImageBuffer(mainImage)
For row=0 To ROWS-1
	For col= 0 To COLS-1
		idx=4*row + col
		;grab a portion of the mainImage into a frame of boardImage
		GrabImage (boardImage,row*160,col*120,idx)
		
	Next
Next

;just use the front buffer as a test
Text 0,500,"Press A Key"
WaitKey
Cls
SetBuffer FrontBuffer()
Text 0,500,"Press A Key"
WaitKey
;recompile the mainImage using the grabbed frames in boardImage
For row=0 To ROWS-1
	For col= 0 To COLS-1
		idx=4*row+col
		DrawImage boardImage,row*160,col*120,idx
		
	Next
Next

WaitKey



NewtSoup(Posted 2007) [#2]
It's ok.. I've spotted what was wrong.. the = after SetBuffer. I'd delete this thread if I could. But I can't.


JazzieB(Posted 2007) [#3]
There is also the CopyRect command that let's you copy from one image buffer to another without having to set any buffers.


NewtSoup(Posted 2007) [#4]
As penance for not spotting my newbie style syntax error I'll post the entire code of what I was trying to achieve. I've not coded anything in 4 years so pretty my all my base are yours atm. Still, it works and it stands alone needing only a random image to be supplied.

Naturally it could (and probably should) be prettied up with button graphics, some better event handling and a more sophisticated interface to choose images and puzzle dimensioins (rows colums)

Global ROWS =4
Global COLS =4
SeedRnd MilliSecs()

Dim board(ROWS,COLS)

;Include "puzzlefuncs.bb"

Graphics 800,600,32,3
mainImage=LoadImage("default.jpg")
If Not (mainimage) Then RuntimeError("No image to load, default.jpg needed in the same directory as this executable")
ResizeImage mainImage,640,480

cellx=640/COLS
celly=480/ROWS

ox=(800-640)/2
oy=(600-480)/2

boardImage = CreateImage (cellx,celly,ROWS*COLS)

;set the current buffer to the image I want to grab frames from
SetBuffer ImageBuffer(mainImage)

For row=0 To ROWS-1
	For col= 0 To COLS-1
		idx=COLS*row + col
		;grab a portion of the mainImage into a frame of boardImage
		GrabImage (boardImage,col*cellx,row*celly,idx)
		;set up the game board matrix while we're at it
		board(row,col)=idx
		
	Next
Next


SetBuffer FrontBuffer()
drawbuttons(ox,oy,cellx,celly)
drawboard(ox,oy,boardImage,cellx,celly)

While Not KeyHit(1)
	WaitMouse()
	mx=MouseX()
	my=MouseY()
	If my<oy And my>(oy-30) Then
		mx=mx-ox
		If mx>0 And mx< COLS*(cellx+1) Then
			column = Floor(mx/cellx)
			columnShiftUp(column)
			drawboard(ox,oy,boardImage,cellx,celly)
		End If
	End If

	If (my>oy+(ROWS*(celly+1))) And my<(oy+(rows*(celly+1)+20)) Then
		mx=mx-ox
		If mx>0 And mx< COLS*(cellx+1) Then
			column = Floor(mx/cellx)
			columnShiftDown(column)
			drawboard(ox,oy,boardImage,cellx,celly)
		End If
	End If

	If mx<ox And mx>(ox-30) Then
		my=my-oy
		If my>0 And my < ROWS*(celly+1) Then
			row=Floor (my/celly)
			rowShiftLeft(row)
			drawboard(ox,oy,boardImage,cellx,celly)
		End If
	End If

	If (mx>ox+(ROWS*(cellx+1))) And (mx<(ox+(ROWS*(cellx+1))+20)) Then
		my=my-oy
		If my>0 And my < ROWS*(celly+1) Then
			row=Floor (my/celly)
			rowShiftRight(row)
			drawboard(ox,oy,boardImage,cellx,celly)
		End If
	End If	

	If my<30 And my>10 Then
		If mx>170 And mx< 230 Then
			mixIt(ox,oy,boardImage,cellx,celly)
		End If

		If mx>570 And mx<630 Then
			End
		End If

		If mx>370 And mx<430 Then
			fixIt()
			drawboard(ox,oy,boardImage,cellx,celly)
		End If
	End If

Wend

Function drawbuttons(ox,oy,cellx,celly)
Color 150,100,100
;draw buttons along the top for each column of cells
For col=0 To COLS-1
	Rect ox+2+(cellx+1)*col,oy-20,cellx-4,10,1
	Rect ox+2+(cellx+1)*col,rows*(celly+1)+oy+10,cellx-4,10,1
Next

For row=0 To rows-1
	
	Rect ox-20,oy+2+(celly+1)*row,10,celly-4,1
	Rect ox+10+(cellx+1)*cols,oy+2+(celly+1)*row,10,celly-4,1
Next

;Draw mix it, fix it and quit it


Rect 170,10,60,16,1
Rect 370,10,60,16,1
Rect 570,10,60,16,1

Color 100,50,50
Text 200,18,"Mix It",1,1
Text 400,18,"Fix It",1,1
Text 600,18,"Quit It",1,1

End Function

Function drawboard(ox,oy,boardImage,cellx,celly)
	For row=0 To ROWS-1
		For col= 0 To COLS-1
			idx=board(row,col)
			DrawImage boardImage,col*(cellx+1)+ox,row*(celly+1)+oy,idx
		
		Next
	Next

End Function

Function drawboardimage(ox,oy,bI,cellx,celly)

	For row=0 To ROWS-1
		For col= 0 To COLS-1
			idx=4*row+col
			DrawImage bI,col*(cellx+1)+ox,row*(celly+1)+oy,idx
		
		Next
	Next

End Function

Function mixIt(ox,oy,boardImage,cellx,celly)
	For f = 1 To 100	
		rc=Rnd(1,2)
		If rc=1 Then
			c=Rnd(0,cols-1)
			ud=Rnd(1,2)
			If ud=1 Then
				columnShiftUp(c)
				
			Else
				columnShiftDown(c)
				
			End If
		Else If rc= 2 Then
			r=Rnd(0,rows-1)
			lr=Rnd(1,2)
			If lr=1 Then
				rowShiftRight(r)
				
			Else
				rowShiftleft(r)
				
			End If
		End If
		drawboard(ox,oy,boardImage,cellx,celly)
		Delay 50
	Next

	

End Function

Function fixIt()
	For row=0 To ROWS-1
		For col=0 To COLS-1
			board(row,col)=COLS*row + col
		Next
	Next
End Function

Function RowShiftRight(row)
	board(row,COLS)=board(row,COLS-1)
	For f=(COLS-1) To 1 Step -1
	  board(row,f)=board(row,f-1)
	Next
	board(row,0)=board(row,COLS)
	board(row,COLS)=0
		

End Function

Function RowShiftLeft(row)
	board(row,COLS)=board(row,0)
	For f=0To COLS-2
	  board(row,f)=board(row,f+1)
	Next
	board(row,COLS-1)=board(row,COLS)
	board(row,COLS)=0
		

End Function

Function ColumnShiftUp(col)
	board(ROWS,col)=board(0,col)
	For f=0 To ROWS-2
		board(f,col)=board(f+1,col)
	Next
	board(ROWS-1,col)=board(ROWS,col)
	board(ROWS,col)=0

End Function

Function ColumnShiftDown(col)
	board(ROWS,col)=board(ROWS-1,col)
	For f=(ROWS-1) To 1 Step -1
	  board(f,col)=board(f-1,col)
	Next
	board(0,col)=board(ROWS,col)
	board(ROWS,col)=0
		

End Function