Adding Antialiasing To An Image

Miscellaneous Forums/General Discussion/Adding Antialiasing To An Image

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.

This antialias function created by Leadwerks, might do the trick :-)
taken from here:
http://www.blitzbasic.co.nz/Community/posts.php?topic=39429

Just change the path to your image:

Graphics3D 800,600,32,2

image=LoadImage("YourImageHere.bmp")


DrawBlock image,0,0

Color 0,0,0

Text 400,50,"Image Not Antialiased, press space to antialias it",True,True


Flip
Repeat

	If KeyDown(1)
		End
	EndIf

Until KeyHit(57)
FlushKeys

DrawBlock image,0,0

Text 400,50,"Antialiasing.... please wait",True,True

Flip

AntiAliasBuffer(ImageBuffer(image),ImageWidth(image)-1,ImageHeight(image)-1,1,40)


DrawBlock image,0,0

Text 400,50,"Image Antialiased!",True,True

Flip
Repeat

	If KeyDown(1)
		End
	EndIf

Until KeyHit(57)
FlushKeys



;function by Leadwerks: www.leadwerks.com
;taken from here: 
;http://www.blitzbasic.co.nz/Community/posts.php?topic=39429
Function AntiAliasBuffer(buffer,w,h,passes=1,threshhold=20)
gfxbuffer=GraphicsBuffer()
SetBuffer buffer
LockBuffer buffer
For p=1 To passes
	For x=0 To w-1
		For y=0 To h-1
			hue=ReadPixelFast(x,y)
			r=Red(hue)
			g=Green(hue)
			b=Blue(hue)
			count=BottomLeftPixelsAntialiased(buffer,w,h,x,y,threshhold)
			For n=1 To count
				perc#=Float(n)/Float(count)
				hue=ReadPixelFast(x-n,y)
				lr=Red(hue)*perc+r*(1.0-perc)
				lg=Green(hue)*perc+g*(1.0-perc)
				lb=Blue(hue)*perc+b*(1.0-perc)
				WritePixelFast x-n,y,RGB(lr,lg,lb)
				Next
			count=BottomRightPixelsAntialiased(buffer,w,h,x,y,threshhold)
			For n=1 To count
				perc#=Float(n)/Float(count)
				hue=ReadPixelFast(x+n,y)
				lr=Red(hue)*perc+r*(1.0-perc)
				lg=Green(hue)*perc+g*(1.0-perc)
				lb=Blue(hue)*perc+b*(1.0-perc)
				WritePixelFast x+n,y,RGB(lr,lg,lb)
				Next
			count=VerticalBottomLeftPixelsAntialiased(buffer,w,h,x,y,threshhold)
			For n=1 To count
				perc#=Float(n)/Float(count)
				hue=ReadPixelFast(x,y-n)
				lr=Red(hue)*perc+r*(1.0-perc)
				lg=Green(hue)*perc+g*(1.0-perc)
				lb=Blue(hue)*perc+b*(1.0-perc)
				WritePixelFast x,y-n,RGB(lr,lg,lb)
				Next
			count=VerticalBottomRightPixelsAntialiased(buffer,w,h,x,y,threshhold)
			For n=1 To count
				perc#=Float(n)/Float(count)
				hue=ReadPixelFast(x,y+n)
				lr=Red(hue)*perc+r*(1.0-perc)
				lg=Green(hue)*perc+g*(1.0-perc)
				lb=Blue(hue)*perc+b*(1.0-perc)
				WritePixelFast x,y+n,RGB(lr,lg,lb)
				Next
			Next
		Next
	Next
UnlockBuffer buffer
SetBuffer gfxbuffer
End Function

Function BottomLeftPixelsAntialiased(buffer,w,h,x,y,threshhold)
If x=0 Return
hue=ReadPixelFast(x,y)
r=Red(hue)
g=Green(hue)
b=Blue(hue)
Repeat
	found=False
	hue=ReadPixelFast(x-1,y)
	lr=Red(hue)
	lg=Green(hue)
	lb=Blue(hue)
	If Abs(lr-r)>threshhold Or Abs(lg-g)>threshhold Or Abs(lb-b)>threshhold
		If y<h
			hue=ReadPixelFast(x-1,y+1)
			blr=Red(hue)
			blg=Green(hue)
			blb=Blue(hue)
			If Abs(blr-r)<threshhold And Abs(blg-g)<threshhold And Abs(blb-b)<threshhold
				found=True
				EndIf				
			EndIf
		EndIf
	If found
		count=count+1
		Else
		Exit
		EndIf
	If x=0 Exit
	x=x-1
	Forever
Return count
End Function

Function BottomRightPixelsAntialiased(buffer,w,h,x,y,threshhold)
hue=ReadPixelFast(x,y)
r=Red(hue)
g=Green(hue)
b=Blue(hue)
If x=w Return
Repeat
	found=False
	hue=ReadPixelFast(x+1,y)
	lr=Red(hue)
	lg=Green(hue)
	lb=Blue(hue)	
	If Abs(lr-r)>threshhold Or Abs(lg-g)>threshhold Or Abs(lb-b)>threshhold
		If y<h
			hue=ReadPixelFast(x+1,y+1)
			blr=Red(hue)
			blg=Green(hue)
			blb=Blue(hue)
			If Abs(blr-r)<threshhold And Abs(blg-g)<threshhold And Abs(blb-b)<threshhold
				found=True
				EndIf				
			EndIf
		EndIf
	If found
		count=count+1
		Else
		Exit
		EndIf
	If x=w-1 Exit
	x=x+1
	Forever
Return count
End Function

Function VerticalBottomLeftPixelsAntialiased(buffer,w,h,x,y,threshhold)
If y=0 Return
hue=ReadPixelFast(x,y)
r=Red(hue)
g=Green(hue)
b=Blue(hue)
Repeat
	found=False
	hue=ReadPixelFast(x,y-1)
	lr=Red(hue)
	lg=Green(hue)
	lb=Blue(hue)
	If Abs(lr-r)>threshhold Or Abs(lg-g)>threshhold Or Abs(lb-b)>threshhold
		If x>0
			hue=ReadPixelFast(x-1,y-1)
			blr=Red(hue)
			blg=Green(hue)
			blb=Blue(hue)
			If Abs(blr-r)<threshhold And Abs(blg-g)<threshhold And Abs(blb-b)<threshhold
				found=True
				EndIf				
			EndIf
		EndIf
	If found
		count=count+1
		Else
		Exit
		EndIf
	If y=0 Exit
	y=y-1
	Forever
Return count
End Function

Function VerticalBottomRightPixelsAntialiased(buffer,w,h,x,y,threshhold)
If y=h Return
hue=ReadPixelFast(x,y)
r=Red(hue)
g=Green(hue)
b=Blue(hue)
Repeat
	found=False
	hue=ReadPixelFast(x,y+1)
	lr=Red(hue)
	lg=Green(hue)
	lb=Blue(hue)
	If Abs(lr-r)>threshhold Or Abs(lg-g)>threshhold Or Abs(lb-b)>threshhold
		If x>0
			hue=ReadPixelFast(x-1,y+1)
			blr=Red(hue)
			blg=Green(hue)
			blb=Blue(hue)
			If Abs(blr-r)<threshhold And Abs(blg-g)<threshhold And Abs(blb-b)<threshhold
				found=True
				EndIf				
			EndIf
		EndIf
	If found
		count=count+1
		Else
		Exit
		EndIf
	If y=h Exit
	y=y+1
	Forever
Return count
End Function


Function rgb(r,g,b)
	Return b + g Shl 8 + r Shl 16
End Function


Function Red(rgb)
	Return rgb Shr 16
End Function

Function Green(rgb)
	Return rgb Shr 8 - ((rgb Shr 16) Shl 8)
End Function

Function Blue(rgb)
	Return rgb-red(rgb) Shl 16-green(rgb) Shl 8
End Function