Easiest way to drawn an image on another?

BlitzMax Forums/BlitzMax Programming/Easiest way to drawn an image on another?

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.

ok ... I think its not perfect (compared the "smoothed" lines of drawimage and mine drawn on the image) ... a bit darker anti-alias i think, but this may be also "their" (graphicscard) fault, not mine ;D - or just a bunch of rounding and so on.

For normal use you wont see a big difference.

The whole thing took so long to make it possible to draw an alpha-channel-image onto another image with alpha-channel.

The code below is similar to your example, but uses images from the samples, don't forget you may use light and alpha params to e.g. show "not reached" awards and so on.



Strict

Function ARGB_Alpha:Int(ARGB:Int)
 Return (argb Shr 24) & $ff
 'Return Int((ARGB & $FF000000:Int) / $1000000:Int) 
End Function

Function ARGB_Red:Int(ARGB:Int)
  Return (argb Shr 16) & $ff
' Return Int((ARGB & $00FF0000:Int) / $10000:Int)
End Function

Function ARGB_Green:Int(ARGB:Int)
  Return (argb Shr 8) & $ff
' Return Int((ARGB & $0000FF00:Int) / $100:Int)
End Function

Function ARGB_Blue:Int(ARGB:Int)
 Return (argb & $ff) 
' Return (ARGB & $000000FF:Int)
End Function

Function ARGB_Color:Int(alpha:Int,red:Int,green:Int,blue:Int)
 Return (Int(alpha * $1000000) + Int(RED * $10000) + Int(green * $100) + Int(blue)) 
End Function

Function DrawOnPixmap(image:TImage, framenr:Int = 0, Pixmap:TPixmap, x:Int, y:Int, alpha:Float = 1.0, light:Float = 1.0) 
      Local TempPix:TPixmap = Null
	  If image = Null Then Throw "image doesnt exist"
	  If framenr = 0 Then TempPix = LockImage(image) 
      If framenr > 0 Then TempPix = LockImage(image, Framenr) 
	  For Local i:Int = 0 To ImageWidth(image) - 1
	    For Local j:Int = 0 To ImageHeight(image) - 1
		  If x + i < pixmap.width And y + j < pixmap.Height
			Local sourcepixel:Int = ReadPixel(TempPix, i,j)
			Local destpixel:Int = ReadPixel(pixmap, x+i,y+j)
			Local destA:Float = ARGB_Alpha(destpixel) 
			Local sourceA:Float = ARGB_Alpha(sourcepixel) * alpha
			If sourceA = 255 Then destA = 0
			'remove comment to remove unneeded calculations 
			'but only when light/alpha not used!
'			If sourceA <> 255 And sourceA <> 0
				Local destR:Float = ARGB_Red(destpixel) 
				Local destG:Float = ARGB_Green(destpixel) 
				Local destB:Float = ARGB_Blue(destpixel) 
				Local SourceR:Float = ARGB_Red(Sourcepixel) 
				Local SourceG:Float = ARGB_Green(Sourcepixel) 
				Local SourceB:Float = ARGB_Blue(Sourcepixel) 
					Local AlphaSum:Int = destA + sourceA

					sourceR = (sourceR * light * sourceA / AlphaSum) + destA / AlphaSum * (destR * destA / AlphaSum) 
					sourceG = (sourceG * light * sourceA / AlphaSum) + destA / AlphaSum * (destG * destA / AlphaSum) 
					sourceB = (sourceB * light * sourceA / AlphaSum) + destA / AlphaSum * (destB * destA / AlphaSum) 
					If AlphaSum > 255 Then AlphaSum = 255
					sourcepixel = ARGB_Color(AlphaSum, SourceR, sourceG, sourceB) 
'			EndIf
			If SourceA <> 0 Then WritePixel(Pixmap, x + i, y + j, sourcepixel) 
		  EndIf
		Next
	  Next
	  If framenr = 0 UnlockImage(image)
	  If framenr > 0 UnlockImage(image, framenr)
End Function

Graphics 800, 600, 0

'Local Img1:TImage = LoadImage("colortest.png") 
Local Img1:TImage = LoadImage("samples\digesteroids\graphics\digestive.png") 
Local Img2:TImage = LoadImage("samples\breakout\media\B-Max.png") 
'Local Img2:TImage = LoadImage("colortestbg.png") 
Local PixMap:TPixmap = LockImage(Img2)
DrawOnPixmap(Img1, 0, PixMap, 170, 120, 0.5, 1.0) 
DrawOnPixmap(Img1, 0, PixMap, 70, 20, 1.0, 1.0) 
DrawOnPixmap(Img1, 0, PixMap, 270, 220, 1.0, 0.5) 
UnlockImage(Img2)
While Not KeyHit(key_Escape)
	Cls	
	SetBlend alphablend
	SetColor 255, 0, 255;DrawRect(10, 10, 300, 300) 
	SetColor 255, 0, 0;DrawRect(200, 200, 450, 300) 
	SetColor 0, 0, 255;DrawRect(300, 100, 200, 450) 
	SetColor 255,255,255
	DrawImage(Img2, MouseX() - ImageWidth(Img2) / 2, MouseY() - ImageWidth(Img2) / 2) 
	DrawImage(Img1, MouseX() - ImageWidth(Img2) / 2, MouseY() - ImageWidth(Img2) / 2) 
	Flip
Wend


bye
MB