;vividgl-3d lite.
;
;(c)antony wells 2004.
;
;--[ image module ]-[core]

Type bitmap
	Field buf,w,h
	Field bf[255]
End Type

Type image
	Field rgba,w,h
	Field pbufid 
End Type

Function Image_Draw(image,x,y)
	Camera_FlatScreen()
	glDisable(gl_depth_test)
	glEnable(gl_alpha_test)
	glEnable(gl_blend)
	glBlendFunc(gl_src_alpha,gl_one_minus_src_alpha);
	img.image=Object.image(image)
	glRasterPos2i x,GraphicsHeight()-(y+img\h)
	If img\pbufid
		glBindBufferObj(gl_pixel_unpack_buffer_arb,img\pbufid)
		glDrawPixels2 img\w,img\h,gl_rgba ,gl_float,0
		glBindBufferObj(gl_pixel_unpack_buffer_arb,0)
	Else
		glDrawPixels img\w,img\h,gl_rgba,gl_float,img\rgba
	EndIf
	glEnable(gl_depth_test)
	glBlendFunc(gl_one,gl_zero)
	Camera_Project()
End Function

Function Image_Grab(image,x,y)
	in.image=Object.image(image)
	glReadPixels x,y,in\w,in\h,gl_rgba,gl_float,in\rgba
End Function

Function Image_GrabBlitz(image,x,y)
	in.image=Object.image(image)
	LockBuffer
	iw=in\w-1
	ih=in\h-1
	rgba=in\rgba
	For ry=y+ih To y Step-1
	For rx=x To x+iw
		rgb=ReadPixel(rx,ry)
		r#=(rgb Shr 8) And 255
		g#=(rgb Shr 16) And 255
		b#=(rgb And 255)
	    If (r+g+b)>10 alpha#=1 Else alpha=0
		PokeFloat rgba,p,r/255.
		PokeFloat rgba,p+4,g/255.
		PokeFloat rgba,p+8,b/255.
		PokeFloat rgba,p+12,alpha
		p=p+16
	Next
	Next
	UnlockBuffer
	FreeImage image
End Function

 
Function Image_Create(width,height)
	out.image = New image
	out\w=width
	out\h=height
	out\rgba =CreateBank(width*height*16)
	Return Handle(out)
End Function

Const glvideoram=1
Function Image_Load(file$,flag=glvideoram) 
	img =LoadImage(file)
	If Not img Return False
	out.image=New image
	w=ImageWidth(img)
	h=ImageHeight(img)
	out\w=w
	out\h=h
	sb=ImageBuffer(img)
	out\rgba =CreateBank(w*h*3)
	rgba=out\rgba
	SetBuffer sb
	LockBuffer
	For y=h-1 To 0 Step -1
	For x=0 To w-1
		rgb=ReadPixelFast(x,y)
		PokeByte rgba,p,(rgb Shr 16) And 255
		PokeByte rgba,p+1,(rgb Shr 8) And 255
		PokeByte rgba,p+2,(rgb And 255)
		p=p+3
	Next	
	Next
	UnlockBuffer
	FreeImage img
	If flag And glvideoram ;use pixel buffer streams. requires nvidia60+ drivers.
		glGenBuffersObj(1,tempbank)
		out\pbufid=PeekInt(tempbank,0)
		
		glBindBufferObj(gl_pixel_pack_buffer_arb,tempbank)
		glBufferDataObj(gl_pixel_pack_buffer_arb,BankSize(out\rgba),out\rgba,gl_static_draw_arb)
		glBindBufferObj(gl_pixel_pack_buffer_arb,0)
	EndIf
	Return Handle(out)
End Function
Function Graphics_PixelBuffersupport()
	Return glusepixelbuffer
End Function

Function Graphics_PixelBufferson(force=False) 
	If force
		Graphics_PixelBufferstate=True
	Else If glusepixelbuffer
		Graphics_PixelBufferstate=True
	EndIf
End Function

Function Graphics_PixelBuffersoff()
	Graphics_PixelBufferstate=False
End Function

;Function glcreatebitmap.bitmap(width=0,h=0)
;	o.bitmap=New bitmap
;	o\buf=CreateBank(width*h*8)
	;o\w=width
;	o\h=h
;	Return o
;End Function

Function glscanblitzbuffer(in.bitmap,x,y)
	For rx=x To x+in\w
	For ry=y To y+in\h-1
		rgb=ReadPixel(rx,ry)
		r=(rgb Shr 16) And 255
		g=(rgb Shr 8) And 255
		b=(rgb And 255)
		If (r+g+b)>128 PokeInt in\buf,off,True
		off=off+1
	Next
	Next
	;unlockbuffer
End Function


