Alpha Image overlay

Blitz3D Forums/Blitz3D Beginners Area/Alpha Image overlay

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.

Get your original image. Extract the alpha channel. Save it as a separate image. Then...


original=loadimage("yourimage.bmp")
alpha=loadimage("alphachannel.bmp")
DrawAlphaImage original,0,0,alpha

Function DrawAlphaImage(image,px,py,alphaimage)
; AlphaBlit Function by Simon Harrison (simon@...)
	w=ImageWidth(image)
	h=ImageHeight(image)
	gw=GraphicsWidth()
	gh=GraphicsWidth()
; clip
	x0=px:y0=py
	If x0<0 w=w+x0 x0=0
	If y0<0 h=h+y0 y0=0
	If x0+w>gw w=gw-x0
	If y0+h>gh h=gh-y0
	If w<=0 Or h<=0 Return
	x1=x0+w-1
	y1=y0+h-1
;	x0=px:y0=py
;	x1=px+w-1
;	y1=py+h-1
	ibuffer=ImageBuffer(image)
	abuffer=ImageBuffer(alphaimage)
	gbuffer=GraphicsBuffer()
	LockBuffer ibuffer
	LockBuffer abuffer
	LockBuffer gbuffer
	For y=y0 To y1
		For x=x0 To x1
			alpha=ReadPixelFast(x-px,y-py,abuffer) And 255
			If alpha>1
				rgb0=ReadPixelFast(x-px,y-py,ibuffer)
				rgb1=ReadPixelFast(x,y,gbuffer)
				bit=$80
				rgb=0
				While bit>1
					rgb0=(rgb0 Shr 1) And $7f7f7f
					rgb1=(rgb1 Shr 1) And $7f7f7f
					If (alpha And bit) rgb=rgb+rgb0 Else rgb=rgb+rgb1
					bit=bit Shr 1
				Wend
				WritePixelFast x,y,rgb
			EndIf
		Next
	Next
	UnlockBuffer gbuffer	
	UnlockBuffer ibuffer
	UnlockBuffer abuffer
End Function


Since you wanted to do it in a "2D-way", there ya go. Its heavy on the resources so BackBuffer()/Flip and don't use it for moving animated sprites. ;)

+BlackD